feat: add AI endpoint manager plugin with configuration and routing capabilities
Some checks failed
Some checks failed
- Introduced `ai_endpoint_mgr` plugin to manage multiple AI provider endpoints. - Added configuration reference documentation for `config.toml`. - Implemented endpoint loading, active endpoint switching, and model mutation. - Included error handling for missing endpoints and configuration failures. - Developed unit tests covering various scenarios including error paths and concurrency.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
// 提供所有前端(CLI / GUI / Web)共享的启动逻辑:
|
||||
// - 配置文件发现(argv / 默认路径 / 平台 fopen)
|
||||
// - dstalk_init() 调用
|
||||
// - 常用服务查询(ai, session, file_io, tools, context)
|
||||
// - 常用服务查询(ai / endpoint_mgr / session / file_io / tools / context)
|
||||
// - AI 服务默认配置(从 config 读取,带 fallback)
|
||||
// ============================================================================
|
||||
|
||||
@@ -20,6 +20,7 @@ struct FrontendServices {
|
||||
const dstalk_session_service_t* session = nullptr;
|
||||
const dstalk_file_io_service_t* file_io = nullptr;
|
||||
const dstalk_tools_service_t* tools = nullptr;
|
||||
const dstalk_ai_endpoint_mgr_t* endpoint_mgr = nullptr; // I08: AI endpoint manager(可选)/ optional
|
||||
|
||||
std::string provider; // "ai.deepseek" / "ai.openai" / "ai.anthropic"
|
||||
std::string model; // e.g. "deepseek-v4-pro"
|
||||
@@ -35,8 +36,8 @@ struct FrontendServices {
|
||||
// 功能:
|
||||
// 1. 发现配置文件:优先 argv[1](跳过已知标志),其次 default_config(如 "config.toml")
|
||||
// 2. 调用 dstalk_init(config_path)
|
||||
// 3. 查询常用插件服务(ai / session / file_io / tools)
|
||||
// 4. 用 dstalk_config_get 读取 api.* 键并调用 ai->configure() 设置默认值
|
||||
// 3. 查询常用插件服务(ai / endpoint_mgr / session / file_io / tools)
|
||||
// 4. 用 dstalk_config_get 读取 api.* 键并调用 ai->configure() 设置旧单 provider 默认值
|
||||
//
|
||||
// 参数:
|
||||
// svc - [out] 填入查询到的服务指针和配置信息
|
||||
|
||||
@@ -79,12 +79,19 @@ int dstalk_frontend_init(FrontendServices& svc,
|
||||
svc.tools = static_cast<const dstalk_tools_service_t*>(
|
||||
dstalk_service_query("tools", 1));
|
||||
|
||||
// I08: 查询 AI endpoint manager(可选服务)/ query AI endpoint manager (optional service)
|
||||
svc.endpoint_mgr = static_cast<const dstalk_ai_endpoint_mgr_t*>(
|
||||
dstalk_service_query("ai_endpoint_mgr", 1));
|
||||
|
||||
const dstalk_context_service_t* ctx_svc =
|
||||
static_cast<const dstalk_context_service_t*>(
|
||||
dstalk_service_query("context", 1));
|
||||
(void)ctx_svc; // 不强制使用,保留以备将来使用
|
||||
|
||||
if (!svc.ai) {
|
||||
// endpoint_mgr 可作为 AI 后端;缺少旧 ai provider 时仍允许多 endpoint 配置工作。
|
||||
// endpoint_mgr can serve as the AI backend; allow multi-endpoint config even when legacy ai provider is absent.
|
||||
const bool endpoint_mgr_ready = svc.endpoint_mgr && svc.endpoint_mgr->count() > 0;
|
||||
if (!svc.ai && !endpoint_mgr_ready) {
|
||||
std::fprintf(stderr, "[dstalk] AI 服务未找到(请检查插件目录)\n");
|
||||
return 2;
|
||||
}
|
||||
@@ -93,7 +100,8 @@ int dstalk_frontend_init(FrontendServices& svc,
|
||||
return 3;
|
||||
}
|
||||
|
||||
// (3) 配置 AI 服务的默认值
|
||||
// (3) 配置 AI 服务的默认值;endpoint_mgr 路径由 endpoint.<name>.* 配置驱动。
|
||||
// Configure legacy AI defaults; endpoint_mgr is driven by endpoint.<name>.* config.
|
||||
const char* base_url = dstalk_config_get("api.base_url");
|
||||
const char* api_key = dstalk_config_get("api.api_key");
|
||||
const char* model = dstalk_config_get("api.model");
|
||||
@@ -105,9 +113,11 @@ int dstalk_frontend_init(FrontendServices& svc,
|
||||
svc.api_key = api_key ? api_key : "";
|
||||
svc.model = model;
|
||||
|
||||
svc.ai->configure(provider, base_url,
|
||||
api_key ? api_key : "",
|
||||
model, 4096, 0.7);
|
||||
if (svc.ai) {
|
||||
svc.ai->configure(provider, base_url,
|
||||
api_key ? api_key : "",
|
||||
model, 4096, 0.7);
|
||||
}
|
||||
|
||||
svc.initialized = true;
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user