feat: plugin system

This commit is contained in:
leookun committed 2026-08-30 20:00:53 +08:00
1 parent 1609b57433
commit e6130e01a7
78 files changed
+9010 -643

No files matched your search

+33
View File
@@ -137,12 +137,45 @@ pub fn api_router(service: ControlService) -> Router {
)
.route("/__byok-api__/api/llm-calls", get(calls::list))
.route("/__byok-api__/api/llm-calls/{call_id}", get(calls::detail))
.route("/__byok-api__/api/plugins", get(plugins::list))
.route(
"/__byok-api__/api/plugins/runtime",
get(plugins::runtime_status)
.post(plugins::initialize_runtime)
.delete(plugins::cancel_runtime_initialization),
)
.route(
"/__byok-api__/api/plugins/oauth/{session_id}/poll",
post(plugins::oauth_poll),
)
.route(
"/__byok-api__/api/plugins/{plugin_id}",
axum::routing::delete(plugins::remove),
)
.route(
"/__byok-api__/api/plugins/{plugin_id}/resources/{resource_type}/add/{method_id}/begin",
post(plugins::oauth_begin),
)
.route(
"/__byok-api__/api/plugins/{plugin_id}/resources/{resource_type}/import",
post(plugins::import),
)
.route(
"/__byok-api__/api/plugins/{plugin_id}/resources/{resource_type}/export",
get(plugins::export_resources),
)
.route(
"/__byok-api__/api/plugins/{plugin_id}/resources/{resource_type}/{resource_id}",
axum::routing::delete(plugins::delete_resource),
)
.route(
"/__byok-api__/api/plugins/{plugin_id}/resources/{resource_type}/{resource_id}/refresh",
post(plugins::refresh_resource),
)
.route(
"/__byok-api__/api/plugins/{plugin_id}/providers/{provider_id}/models/sync",
post(plugins::sync_models),
)
.route(
"/__byok-api__/api/settings/observability",
get(settings::get).put(settings::update),
+103 -3
View File
@@ -1,10 +1,110 @@
//! Exposes plugin runtime initialization and status endpoints.
use axum::{extract::State, Json};
//! Exposes plugin discovery, resource lifecycle, model sync, and runtime endpoints.
use axum::{
extract::{Path, State},
http::StatusCode,
Json,
};
use crate::{plugin::PluginRuntimeStatus, Result};
use crate::{
plugin::{
ImportResponse, OAuthBeginResponse, OAuthPollResponse, PluginDescriptor,
PluginRuntimeStatus,
},
Result,
};
use super::ControlService;
pub async fn list(State(service): State<ControlService>) -> Result<Json<Vec<PluginDescriptor>>> {
Ok(Json(service.plugins().await))
}
pub async fn remove(
State(service): State<ControlService>,
Path(plugin_id): Path<String>,
) -> Result<StatusCode> {
service.remove_plugin_configuration(&plugin_id).await?;
Ok(StatusCode::NO_CONTENT)
}
pub async fn oauth_begin(
State(service): State<ControlService>,
Path((plugin_id, resource_type, method_id)): Path<(String, String, String)>,
) -> Result<Json<OAuthBeginResponse>> {
Ok(Json(
service
.plugin_oauth_begin(&plugin_id, &resource_type, &method_id)
.await?,
))
}
pub async fn oauth_poll(
State(service): State<ControlService>,
Path(session_id): Path<String>,
) -> Result<Json<OAuthPollResponse>> {
Ok(Json(service.plugin_oauth_poll(&session_id).await?))
}
pub async fn import(
State(service): State<ControlService>,
Path((plugin_id, resource_type)): Path<(String, String)>,
Json(files): Json<serde_json::Value>,
) -> Result<Json<ImportResponse>> {
Ok(Json(
service
.plugin_import(&plugin_id, &resource_type, files)
.await?,
))
}
/// 以附件形式返回账号资源导出文件,便于浏览器直接下载。
pub async fn export_resources(
State(service): State<ControlService>,
Path((plugin_id, resource_type)): Path<(String, String)>,
) -> Result<axum::response::Response> {
let value = service
.plugin_export_resources(&plugin_id, &resource_type)
.await?;
let body = serde_json::to_vec_pretty(&value)?;
let response = axum::response::Response::builder()
.header(axum::http::header::CONTENT_TYPE, "application/json")
.header(
axum::http::header::CONTENT_DISPOSITION,
format!("attachment; filename=\"{plugin_id}-{resource_type}.json\""),
)
.body(axum::body::Body::from(body))
.expect("static export response");
Ok(response)
}
pub async fn refresh_resource(
State(service): State<ControlService>,
Path((plugin_id, resource_type, resource_id)): Path<(String, String, String)>,
) -> Result<StatusCode> {
service
.plugin_refresh_resource(&plugin_id, &resource_type, &resource_id)
.await?;
Ok(StatusCode::NO_CONTENT)
}
pub async fn delete_resource(
State(service): State<ControlService>,
Path((plugin_id, resource_type, resource_id)): Path<(String, String, String)>,
) -> Result<StatusCode> {
service
.plugin_delete_resource(&plugin_id, &resource_type, &resource_id)
.await?;
Ok(StatusCode::NO_CONTENT)
}
pub async fn sync_models(
State(service): State<ControlService>,
Path((plugin_id, provider_id)): Path<(String, String)>,
) -> Result<Json<serde_json::Value>> {
let count = service.plugin_sync_models(&plugin_id, &provider_id).await?;
Ok(Json(serde_json::json!({ "models": count })))
}
pub async fn runtime_status(
State(service): State<ControlService>,
) -> Result<Json<PluginRuntimeStatus>> {
+102 -10
View File
@@ -25,7 +25,7 @@ use crate::{
ModelRequest, ModelSpec, ModelType, Overview, ProjectedContent, ProjectedMessage,
PromptSpec, ProviderType, Role,
},
plugin::{PluginRuntime, PluginRuntimeStatus},
plugin::{PluginDescriptor, PluginRegistry, PluginRuntime, PluginRuntimeStatus},
provider::{is_valid_response_event, ModelEvent, Provider},
store::{
DesktopSettings, PortSettings, ProxySettings, ProxySettingsInput, StatisticsStorage, Store,
@@ -40,6 +40,7 @@ pub struct ControlService {
cursor_harness: CursorHarness,
provider: Arc<dyn Provider>,
plugin_runtime: PluginRuntime,
plugins: PluginRegistry,
model_tests: Arc<Mutex<BTreeMap<String, CancellationToken>>>,
}
@@ -145,12 +146,18 @@ pub struct ObservabilitySettings {
}
impl ControlService {
pub fn new(store: Store, provider: Arc<dyn Provider>) -> Result<Self> {
pub fn new(
store: Store,
provider: Arc<dyn Provider>,
plugin_runtime: PluginRuntime,
plugins: PluginRegistry,
) -> Result<Self> {
Ok(Self {
cursor_harness: CursorHarness::new(store.clone())?,
store,
provider,
plugin_runtime: PluginRuntime::managed()?,
plugin_runtime,
plugins,
model_tests: Arc::new(Mutex::new(BTreeMap::new())),
})
}
@@ -159,6 +166,79 @@ impl ControlService {
&self.cursor_harness
}
pub async fn plugins(&self) -> Vec<PluginDescriptor> {
self.plugins.plugins().await
}
pub async fn plugin_oauth_begin(
&self,
plugin_id: &str,
resource_type: &str,
method_id: &str,
) -> Result<crate::plugin::OAuthBeginResponse> {
self.plugins
.oauth_begin(plugin_id, resource_type, method_id)
.await
}
pub async fn plugin_oauth_poll(
&self,
session_id: &str,
) -> Result<crate::plugin::OAuthPollResponse> {
self.plugins.oauth_poll(session_id).await
}
pub async fn plugin_import(
&self,
plugin_id: &str,
resource_type: &str,
files: serde_json::Value,
) -> Result<crate::plugin::ImportResponse> {
self.plugins
.import_resources(plugin_id, resource_type, files)
.await
}
pub async fn plugin_export_resources(
&self,
plugin_id: &str,
resource_type: &str,
) -> Result<serde_json::Value> {
self.plugins
.export_resources(plugin_id, resource_type)
.await
}
pub async fn plugin_refresh_resource(
&self,
plugin_id: &str,
resource_type: &str,
resource_id: &str,
) -> Result<()> {
self.plugins
.refresh_resource(plugin_id, resource_type, resource_id)
.await
}
pub async fn plugin_delete_resource(
&self,
plugin_id: &str,
resource_type: &str,
resource_id: &str,
) -> Result<()> {
self.plugins
.delete_resource(plugin_id, resource_type, resource_id)
.await
}
pub async fn plugin_sync_models(&self, plugin_id: &str, provider_id: &str) -> Result<usize> {
self.plugins.sync_models(plugin_id, provider_id).await
}
pub async fn remove_plugin_configuration(&self, plugin_id: &str) -> Result<()> {
self.plugins.remove(plugin_id).await
}
pub fn plugin_runtime_status(&self) -> PluginRuntimeStatus {
self.plugin_runtime.status()
}
@@ -308,14 +388,21 @@ impl ControlService {
const TEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(45);
const TEST_PROMPT: &str = "Output the numbers 1 through 120 separated by a single space. No commas, no newlines, no explanation.";
let configured = self
.store
.model(model_hash)
.await?
.ok_or_else(|| Error::RunNotFound(format!("model {model_hash}")))?;
let mut model = ModelSpec::new(model_hash);
configured.configure(&mut model);
model.max_output_tokens = Some(configured.max_output_tokens().unwrap_or(65_536));
if model_hash.starts_with(crate::plugin::ADAPTER_ID_PREFIX) {
let descriptor = self.plugins.model_descriptor(model_hash).await?;
model.display_name = Some(descriptor.display_name);
model.context_window_tokens = descriptor.context_window_tokens;
model.max_output_tokens = Some(descriptor.max_output_tokens.unwrap_or(65_536));
} else {
let configured = self
.store
.model(model_hash)
.await?
.ok_or_else(|| Error::RunNotFound(format!("model {model_hash}")))?;
configured.configure(&mut model);
model.max_output_tokens = Some(configured.max_output_tokens().unwrap_or(65_536));
}
let call_id = format!("model-test-{}", uuid::Uuid::new_v4());
let invocation = ModelInvocation {
call_id: call_id.clone(),
@@ -714,6 +801,11 @@ async fn discover_models_from_endpoint(
ProviderType::Anthropic => {
anthropic_models(client, base_url, api_key, custom_headers).await?
}
ProviderType::Plugin => {
return Err(Error::Config(
"plugin providers discover models through their plugin".into(),
))
}
};
models.sort();
models.dedup();