diff --git a/.gitignore b/.gitignore index 749ff7f..cfd19fc 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,10 @@ tools/ninja/ tools/llvm/ tools/.venv/ +# Python bytecode +__pycache__/ +*.pyc + # IDE .vs/ .vscode/ diff --git a/README.md b/README.md index e0b29ba..824839e 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,9 @@ dstalk 是一款 AI 编程助手命令行工具, 通过调用大模型在终端 | 提供商 | 模型 | 插件 | |--------|------|------| -| OpenAI-compatible | gpt-4o | `ai.openai` | -| Anthropic | claude-opus-4 | `ai.anthropic` | -| OpenAI 兼容 | GPT 系列 | `ai.openai` | +| OpenAI-compatible | gpt-4o | `ai_openai` | +| Anthropic | claude-opus-4 | `ai_anthropic` | +| OpenAI 兼容 | GPT 系列 | `ai_openai` | 通过 `config.toml` 中 `ai.provider` 一键切换。 @@ -61,11 +61,14 @@ dstalk 是一款 AI 编程助手命令行工具, 通过调用大模型在终端 ## 快速开始 +| 平台 | 安装工具链 | 编译 | 运行 | +|------|-----------|------|------| +| Windows | `cd tools && setup.bat` | `build.bat` | `build\bin\dstalk_cli.exe` | +| Mac / Linux | `cd tools && bash setup.sh` | `bash build.sh` | `build/bin/dstalk_cli` | + ```bash -cd tools && setup.bat # 1. 安装工具链 (CMake / Ninja / LLVM / Conan2) -build.bat # 2. 编译 # 3. 创建 config.toml # 见教程: docs/tutorial/quick-start.md -build/dstalk_cli/dstalk_cli.exe # 4. 运行 +# 4. 运行 (路径见上表) # 5. 输入自然语言 # "帮我写一个 C 程序" ``` diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..0233bc1 --- /dev/null +++ b/build.sh @@ -0,0 +1,130 @@ +#!/usr/bin/env bash +set -euo pipefail + +# ============================================================ +# build.sh — dstalk cross-platform build script (Linux / macOS) +# Mirrors build.bat; installs tools into tools/ if missing, +# then runs Conan + CMake + Ninja without modifying system PATH. +# ============================================================ + +ROOT="$(cd "$(dirname "$0")" && pwd)" +TOOLS="${ROOT}/tools" +BUILD_DIR="${ROOT}/build" +BUILD_TYPE="${1:-Release}" + +echo "[dstalk] Build type: ${BUILD_TYPE}" +echo "[dstalk] Project root: ${ROOT}" + +# ============================================================ +# 1. Detect / resolve tool paths (prefer tools/, fallback PATH) +# ============================================================ + +find_tool() { + local name="$1" + shift + for candidate in "$@"; do + if [ -x "$candidate" ]; then + echo "$candidate" + return 0 + fi + done + local sys + sys=$(command -v "$name" 2>/dev/null || true) + if [ -n "$sys" ]; then + echo "$sys" + return 0 + fi + return 1 +} + +CMAKE_EXE=$(find_tool cmake "${TOOLS}/cmake/bin/cmake") || { + echo " [ERROR] CMake not found. Run: tools/setup.sh" + exit 1 +} +NINJA_EXE=$(find_tool ninja "${TOOLS}/ninja/ninja") || { + echo " [ERROR] Ninja not found. Run: tools/setup.sh" + exit 1 +} +CONAN_EXE=$(find_tool conan "${TOOLS}/.venv/bin/conan") || { + echo " [ERROR] Conan2 not found. Run: tools/setup.sh" + exit 1 +} + +# Compiler: prefer clang, fallback gcc +if [ "$(uname)" = "Darwin" ]; then + CC_EXE=$(find_tool clang "${TOOLS}/llvm/bin/clang") || CC_EXE="clang" + CXX_EXE=$(find_tool clang++ "${TOOLS}/llvm/bin/clang++") || CXX_EXE="clang++" +else + CC_EXE=$(find_tool clang "${TOOLS}/llvm/bin/clang") || \ + CC_EXE=$(find_tool gcc) || { echo " [ERROR] No C compiler found"; exit 1; } + CXX_EXE=$(find_tool clang++ "${TOOLS}/llvm/bin/clang++") || \ + CXX_EXE=$(find_tool g++) || { echo " [ERROR] No C++ compiler found"; exit 1; } +fi + +echo "[dstalk] CMake: ${CMAKE_EXE}" +echo "[dstalk] Ninja: ${NINJA_EXE}" +echo "[dstalk] Conan: ${CONAN_EXE}" +echo "[dstalk] CC: ${CC_EXE}" +echo "[dstalk] CXX: ${CXX_EXE}" + +# ============================================================ +# 2. Conan install dependencies +# ============================================================ +echo "[dstalk] Conan: installing dependencies..." +"${CONAN_EXE}" install deps/ -of "${BUILD_DIR}" --build=missing \ + -s build_type="${BUILD_TYPE}" \ + -c tools.cmake.cmaketoolchain:generator=Ninja + +# Locate the conan toolchain +CONAN_TOOLCHAIN="" +for candidate in \ + "${BUILD_DIR}/build/${BUILD_TYPE}/generators/conan_toolchain.cmake" \ + "${BUILD_DIR}/${BUILD_TYPE}/generators/conan_toolchain.cmake" \ + "${BUILD_DIR}/${BUILD_TYPE}/conan_toolchain.cmake" \ + "${BUILD_DIR}/conan_toolchain.cmake"; do + if [ -f "$candidate" ]; then + CONAN_TOOLCHAIN="$candidate" + break + fi +done + +if [ -z "${CONAN_TOOLCHAIN}" ]; then + echo " [ERROR] Could not find conan_toolchain.cmake" + echo " Searched in: ${BUILD_DIR}/build/${BUILD_TYPE}/generators/" + echo " ${BUILD_DIR}/${BUILD_TYPE}/generators/" + exit 1 +fi +echo "[dstalk] Conan toolchain: ${CONAN_TOOLCHAIN}" +CONAN_PREFIX_PATH="$(dirname "${CONAN_TOOLCHAIN}")" + +# Patch conan toolchain if it hardcodes 'cl' (Windows-generated) +if grep -q 'set(CMAKE_C_COMPILER "cl")' "${CONAN_TOOLCHAIN}" 2>/dev/null; then + echo "[dstalk] Patching conan toolchain: replacing cl -> detected compiler" + sed -i.bak \ + -e "s|set(CMAKE_C_COMPILER \"cl\")|set(CMAKE_C_COMPILER \"${CC_EXE}\")|" \ + -e "s|set(CMAKE_CXX_COMPILER \"cl\")|set(CMAKE_CXX_COMPILER \"${CXX_EXE}\")|" \ + "${CONAN_TOOLCHAIN}" +fi + +# ============================================================ +# 3. CMake configure + Ninja build +# ============================================================ +echo "[dstalk] CMake configure..." +"${CMAKE_EXE}" -S "${ROOT}" -B "${BUILD_DIR}" -G Ninja \ + -DCMAKE_MAKE_PROGRAM="${NINJA_EXE}" \ + -DCMAKE_C_COMPILER="${CC_EXE}" \ + -DCMAKE_CXX_COMPILER="${CXX_EXE}" \ + -DCMAKE_TOOLCHAIN_FILE="${CONAN_TOOLCHAIN}" \ + -DCMAKE_PREFIX_PATH="${CONAN_PREFIX_PATH}" \ + -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \ + -DDSTALK_BUILD_GUI=OFF + +echo "[dstalk] Building..." +"${CMAKE_EXE}" --build "${BUILD_DIR}" + +echo "" +echo "============================================" +echo " Build succeeded!" +echo " ${BUILD_DIR}/bin/dstalk$([ "$(uname)" = 'Darwin' ] && echo '.dylib' || echo '.so')" +echo " ${BUILD_DIR}/bin/dstalk_cli" +echo "============================================" diff --git a/docs/explanation/architecture.md b/docs/explanation/architecture.md index 5d63d13..bae6210 100644 --- a/docs/explanation/architecture.md +++ b/docs/explanation/architecture.md @@ -90,7 +90,7 @@ struct dstalk_ai_service_t { 每种服务都有一个预定义的 vtable 结构体(定义在 `dstalk_services.h`)。第三方也可以扩展自己的服务 vtable,版本号随注册一起提供,允许消费者做最低版本检查。 服务注册采用 **name + version** 两要素: -- 全局唯一名称(如 `"ai.openai"`、`"http"`、`"file_io"`)。 +- 全局唯一名称(如 `"ai_openai"`、`"http"`、`"file_io"`)。 - 版本号(消费者可以要求 `min_version`)。 --- diff --git a/docs/explanation/plugin-lifecycle.md b/docs/explanation/plugin-lifecycle.md index 5e08734..2f4a710 100644 --- a/docs/explanation/plugin-lifecycle.md +++ b/docs/explanation/plugin-lifecycle.md @@ -87,7 +87,7 @@ Host 收到非零返回值后,会跳过后续插件的初始化并报告警告 **契约 3:register_service 注册自己的服务。** 插件将自己的 vtable 注册到服务注册表后,其他依赖它的插件才能在后续的 `on_init` 中通过 `query_service` 找到它。 ```c -return host->register_service("ai.openai", 1, &g_service); +return host->register_service("ai_openai", 1, &g_service); ``` 注册表内的 vtable 是原始指针,不拷贝。因此 vtable 指向的结构体必须是**静态生命周期**(全局变量或 static 局部变量)。 diff --git a/docs/reference/plugin-abi.md b/docs/reference/plugin-abi.md index b4c025c..5ba43c0 100644 --- a/docs/reference/plugin-abi.md +++ b/docs/reference/plugin-abi.md @@ -110,7 +110,7 @@ Linux/macOS 通常共享 libc,但静态链接或不同 libc 版本时同样可 ### 4.2 重复注册 同一 `name` 不可重复注册:第二次调用返回 `-2`(`service_registry.cpp:13`)。插件应检查返回 -值,在共享服务名(如 `"ai.openai"`)的场景中避免冲突。 +值,在共享服务名(如 `"ai_openai"`)的场景中避免冲突。 ### 4.3 版本协商 diff --git a/docs/tutorial/quick-start.md b/docs/tutorial/quick-start.md index 27bb8ee..5b89cc7 100644 --- a/docs/tutorial/quick-start.md +++ b/docs/tutorial/quick-start.md @@ -6,35 +6,65 @@ ## 1. 安装工具链 -dstalk 需要 CMake、Ninja、LLVM/Clang 和 Conan2。`setup.bat` 全自动下载安装到 `tools/` 目录。 +dstalk 需要 CMake、Ninja、LLVM/Clang 和 Conan2。`tools/` 下的脚本全自动安装。 +**Windows:** ```bash cd tools setup.bat ``` +**Mac:** +```bash +# 方式 A: 手动安装后运行脚本 +brew install cmake ninja +cd tools && bash setup.sh + +# 方式 B: 脚本全自动 (推荐) +cd tools && bash setup.sh +``` + +**Linux (Ubuntu/Debian):** +```bash +# 方式 A: 手动安装后运行脚本 +sudo apt install cmake ninja-build +cd tools && bash setup.sh + +# 方式 B: 脚本全自动 (推荐) +cd tools && bash setup.sh +``` + > **前提**: 系统需已安装 Python 3.10+。 > > 网络不畅时, 可手动下载放入对应目录: -> - [Ninja](https://github.com/ninja-build/ninja/releases) → `tools/ninja/ninja.exe` -> - [CMake](https://cmake.org/download/) → `tools/cmake/bin/cmake.exe` -> - [LLVM](https://github.com/llvm/llvm-project/releases) → `tools/llvm/bin/clang.exe` -> - Conan2 通过 pip 安装到 `tools/.venv/Scripts/conan.exe` +> - [Ninja](https://github.com/ninja-build/ninja/releases) → `tools/ninja/ninja` (或 `ninja.exe`) +> - [CMake](https://cmake.org/download/) → `tools/cmake/bin/cmake` (或 `cmake.exe`) +> - [LLVM](https://github.com/llvm/llvm-project/releases) → `tools/llvm/bin/clang` (或 `clang.exe`) +> - Conan2 通过 pip 安装到 `tools/.venv/Scripts/conan.exe` (Win) 或 `tools/.venv/bin/conan` (Mac/Linux) --- ## 2. 编译 -项目根目录提供 `build.bat`, 一键完成: Conan 拉取依赖 -> CMake 配置 -> Ninja 编译。 +项目根目录提供 `build.bat` (Windows) / `build.sh` (Mac/Linux), 一键完成: Conan 拉取依赖 -> CMake 配置 -> Ninja 编译。 +**Windows:** ```bash build.bat ``` -编译产物输出到 `build/` 目录。核心产物: -- `build/dstalk_core/dstalk.dll` —— 核心 DLL -- `build/dstalk_cli/dstalk_cli.exe` —— 命令行前端 -- `build/plugins/*.dll` —— 功能插件 +**Mac / Linux:** +```bash +bash build.sh +``` + +编译产物输出到 `build/` 目录: + +| 产物 | Windows | Mac | Linux | +|------|---------|-----|-------| +| 核心 DLL | `build/bin/dstalk.dll` | `build/bin/libdstalk.dylib` | `build/bin/libdstalk.so` | +| CLI 前端 | `build/bin/dstalk_cli.exe` | `build/bin/dstalk_cli` | `build/bin/dstalk_cli` | +| 功能插件 | `build/plugins/*.dll` | `build/plugins/*.dylib` | `build/plugins/*.so` | --- @@ -49,15 +79,15 @@ build.bat **config.toml 示例:** ```toml -# 选择 AI 后端插件: ai.openai 或 ai.anthropic -ai.provider = "ai.openai" +# 选择 AI 后端插件: ai_openai 或 ai_anthropic +ai.provider = "ai_openai" # OpenAI-compatible api.base_url = "https://api.openai.com/v1" api.api_key = "sk-xxxxxxxx" api.model = "gpt-4o" -# Anthropic Claude (切换 ai.provider 为 "ai.anthropic" 即可) +# Anthropic Claude (切换 ai.provider 为 "ai_anthropic" 即可) # api.base_url = "https://api.anthropic.com/v1" # api.api_key = "sk-ant-xxxxxxxx" # api.model = "claude-opus-4-20250514" @@ -71,8 +101,14 @@ api.model = "gpt-4o" ## 4. 运行 dstalk_cli +**Windows:** ```bash -build/dstalk_cli/dstalk_cli.exe +build\bin\dstalk_cli.exe +``` + +**Mac / Linux:** +```bash +build/bin/dstalk_cli ``` 启动后显示欢迎横幅: diff --git a/dstalk_cli/src/main.cpp b/dstalk_cli/src/main.cpp index 931b47b..e0c181b 100644 --- a/dstalk_cli/src/main.cpp +++ b/dstalk_cli/src/main.cpp @@ -220,7 +220,7 @@ static void handle_command(const char* line) // /status —— 脱敏显示当前运行状态 / Display current runtime status (desensitized) if (std::strcmp(line, "/status") == 0) { const char* provider = dstalk_config_get("ai.provider"); - if (!provider) provider = "ai.openai"; + if (!provider) provider = "ai_openai"; const char* base_url = dstalk_config_get("api.base_url"); if (!base_url) base_url = "https://api.openai.com/v1"; const char* api_key = dstalk_config_get("api.api_key"); @@ -488,7 +488,7 @@ int main(int argc, char* argv[]) // 查询插件服务 / Query plugin services const char* ai_provider = dstalk_config_get("ai.provider"); - if (!ai_provider) ai_provider = "ai.openai"; + if (!ai_provider) ai_provider = "ai_openai"; g_ai = static_cast(dstalk_service_query(ai_provider, 1)); g_session = static_cast(dstalk_service_query("session", 1)); g_file_io = static_cast(dstalk_service_query("file_io", 1)); diff --git a/dstalk_core/include/dstalk/dstalk_host.h b/dstalk_core/include/dstalk/dstalk_host.h index 3c0d7d7..d815079 100644 --- a/dstalk_core/include/dstalk/dstalk_host.h +++ b/dstalk_core/include/dstalk/dstalk_host.h @@ -65,6 +65,7 @@ typedef struct { /* --- 服务注册表 / service registry --- */ int (*register_service)(const char* name, int version, void* vtable); void*(*query_service)(const char* name, int min_version); + void (*unregister_service)(const char* name); /* --- 事件总线 / event bus --- */ int (*event_subscribe)(int event_type, dstalk_event_handler_fn handler, void* userdata); diff --git a/dstalk_core/include/dstalk/dstalk_services.h b/dstalk_core/include/dstalk/dstalk_services.h index a0b3b14..aa1f771 100644 --- a/dstalk_core/include/dstalk/dstalk_services.h +++ b/dstalk_core/include/dstalk/dstalk_services.h @@ -15,7 +15,7 @@ extern "C" { #endif /* ---- AI 服务 vtable / AI service vtable ---- */ -/* 以名称如 "ai.openai" 或 "ai.anthropic" 注册 / Registered under names such as "ai.openai" or "ai.anthropic" */ +/* 以名称如 "ai_openai" 或 "ai_anthropic" 注册 / Registered under names such as "ai_openai" or "ai_anthropic" */ typedef struct { /* 配置服务商连接 (base_url, api_key, model 等) / Configure provider connection (base_url, api_key, model, etc.) */ int (*configure)(const char* provider, const char* base_url, diff --git a/dstalk_core/src/host.cpp b/dstalk_core/src/host.cpp index b32a6f7..9fbb297 100644 --- a/dstalk_core/src/host.cpp +++ b/dstalk_core/src/host.cpp @@ -94,6 +94,11 @@ namespace { return g_service_registry ? g_service_registry->query_service(name, min_version) : nullptr; } + // Unregister a service by name from the global registry (no-op if name is null). + void api_unregister_service(const char* name) { + if (g_service_registry) g_service_registry->unregister_service(name); + } + // 通过全局事件总线订阅指定事件类型的处理函数。 // Subscribe a handler to a given event type via the global event bus. int api_event_subscribe(int event_type, dstalk_event_handler_fn handler, void* userdata) { @@ -150,6 +155,7 @@ namespace { dstalk_host_api_t g_host_api = { api_register_service, api_query_service, + api_unregister_service, api_event_subscribe, api_event_emit, api_event_unsubscribe, @@ -218,6 +224,9 @@ DSTALK_API int dstalk_init(const char* config_path) g_service_registry = new dstalk::ServiceRegistry(); g_plugin_loader = new dstalk::PluginLoader(); + // 连接 PluginLoader 和 ServiceRegistry,以便插件卸载时清理服务注册表 / Wire PluginLoader to ServiceRegistry for service cleanup on plugin unload + g_plugin_loader->set_service_registry(g_service_registry); + // 加载配置 / Load config if (config_path && config_path[0]) { if (g_config->load_file(config_path) != 0) { diff --git a/dstalk_core/src/plugin_loader.cpp b/dstalk_core/src/plugin_loader.cpp index d749a3b..1f052d0 100644 --- a/dstalk_core/src/plugin_loader.cpp +++ b/dstalk_core/src/plugin_loader.cpp @@ -6,6 +6,7 @@ */ #include "plugin_loader.hpp" +#include "service_registry.hpp" #include @@ -234,6 +235,17 @@ int PluginLoader::unload_plugin(int plugin_id) } } + // 清理该插件在服务注册表中的条目,避免 dll 卸载后 vtable 悬空指针 / Clean up service registry entries to avoid dangling vtable pointers after DLL unload + if (service_registry_) { + auto svc_it = plugin_services_.find(plugin.name); + if (svc_it != plugin_services_.end()) { + for (const auto& svc_name : svc_it->second) { + service_registry_->unregister_service(svc_name.c_str()); + } + plugin_services_.erase(svc_it); + } + } + // 卸载DLL / Unload DLL #ifdef _WIN32 FreeLibrary((HMODULE)plugin.handle); @@ -422,6 +434,13 @@ int PluginLoader::initialize_all(const dstalk_host_api_t* host_api) if (plugin.info->on_init) { int result; + + // 记录 on_init 前的服务名称快照,用于 on_init 后 diff 出该插件注册的服务 / Snapshot service names before on_init to discover newly registered services after + std::vector before_svcs; + if (service_registry_) { + before_svcs = service_registry_->list_service_names(); + } + try { result = plugin.info->on_init(host_api); } catch (const std::exception& e) { @@ -444,6 +463,17 @@ int PluginLoader::initialize_all(const dstalk_host_api_t* host_api) failed_count++; continue; } + + // 将 on_init 期间新注册的服务归因到当前插件 / Attribute newly registered services to this plugin + if (service_registry_) { + std::vector after_svcs = service_registry_->list_service_names(); + std::unordered_set before_set(before_svcs.begin(), before_svcs.end()); + for (const auto& svc_name : after_svcs) { + if (before_set.find(svc_name) == before_set.end()) { + plugin_services_[plugin.name].push_back(svc_name); + } + } + } } plugin.initialized = true; } @@ -477,6 +507,13 @@ int PluginLoader::initialize_pending(const dstalk_host_api_t* host_api) if (plugin.info->on_init) { int result; + + // 记录 on_init 前的服务名称快照,用于 on_init 后 diff 出该插件注册的服务 / Snapshot service names before on_init to discover newly registered services after + std::vector before_svcs; + if (service_registry_) { + before_svcs = service_registry_->list_service_names(); + } + try { result = plugin.info->on_init(host_api); } catch (const std::exception& e) { @@ -491,6 +528,17 @@ int PluginLoader::initialize_pending(const dstalk_host_api_t* host_api) if (result != 0) { return -1; } + + // 将 on_init 期间新注册的服务归因到当前插件 / Attribute newly registered services to this plugin + if (service_registry_) { + std::vector after_svcs = service_registry_->list_service_names(); + std::unordered_set before_set(before_svcs.begin(), before_svcs.end()); + for (const auto& svc_name : after_svcs) { + if (before_set.find(svc_name) == before_set.end()) { + plugin_services_[plugin.name].push_back(svc_name); + } + } + } } plugin.initialized = true; count++; @@ -537,6 +585,12 @@ void PluginLoader::shutdown_all() plugin.initialized = false; } + // 清空服务注册表,避免后续 DLL 卸载后 vtable 悬空指针 / Clear the service registry to prevent dangling vtable pointers after DLL unload + if (service_registry_) { + service_registry_->clear(); + } + plugin_services_.clear(); + // 释放所有 DLL 句柄 / Free all DLL handles for (auto& [id, plugin] : plugins_) { if (plugin.handle) { diff --git a/dstalk_core/src/plugin_loader.hpp b/dstalk_core/src/plugin_loader.hpp index d8b1390..25f540d 100644 --- a/dstalk_core/src/plugin_loader.hpp +++ b/dstalk_core/src/plugin_loader.hpp @@ -11,10 +11,13 @@ #include #include #include +#include #include namespace dstalk { +class ServiceRegistry; + // 描述单个已加载插件:标识、DLL 句柄、信息 vtable 和初始化状态。 // Describes a single loaded plugin: identity, DLL handle, info vtable, and init state. struct PluginInfo { @@ -60,6 +63,9 @@ public: // 获取插件信息 / Get plugin info const PluginInfo* get_plugin(int plugin_id) const; + // 设置服务注册表引用,供卸载插件时清理服务 / Set service registry reference for service cleanup during plugin unload + void set_service_registry(ServiceRegistry* sr) { service_registry_ = sr; } + private: // 拓扑排序(按依赖顺序) / Topological sort (by dependency order) std::vector topological_sort() const; @@ -71,6 +77,10 @@ private: std::unordered_map plugins_; std::atomic next_id_{1}; const dstalk_host_api_t* host_api_ = nullptr; + + ServiceRegistry* service_registry_ = nullptr; // 用于卸载时清理服务 / for service cleanup during unload + // 插件名称 -> 该插件注册的服务名称列表 / plugin name -> list of service names registered by that plugin + std::unordered_map> plugin_services_; }; } // namespace dstalk diff --git a/dstalk_core/src/service_registry.cpp b/dstalk_core/src/service_registry.cpp index 06c2365..2820eb0 100644 --- a/dstalk_core/src/service_registry.cpp +++ b/dstalk_core/src/service_registry.cpp @@ -48,4 +48,23 @@ void ServiceRegistry::unregister_service(const char* name) services_.erase(name); } +// 列出当前所有已注册服务名称(shared_lock)/ List all currently registered service names (shared_lock). +std::vector ServiceRegistry::list_service_names() const +{ + std::shared_lock lock(mutex_); + std::vector names; + names.reserve(services_.size()); + for (const auto& [name, _] : services_) { + names.push_back(name); + } + return names; +} + +// 移除所有已注册服务(unique_lock)/ Remove all registered services (unique_lock). +void ServiceRegistry::clear() +{ + std::unique_lock lock(mutex_); + services_.clear(); +} + } // namespace dstalk diff --git a/dstalk_core/src/service_registry.hpp b/dstalk_core/src/service_registry.hpp index 808d0b8..a313360 100644 --- a/dstalk_core/src/service_registry.hpp +++ b/dstalk_core/src/service_registry.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace dstalk { @@ -32,6 +33,12 @@ public: // 注销服务 / Unregister a named service void unregister_service(const char* name); + // 列出所有已注册服务名称(用于 diff/遍历)/ List all currently registered service names (for diff / iteration) + std::vector list_service_names() const; + + // 清空所有注册服务 / Remove all registered services + void clear(); + private: struct ServiceEntry { std::string name; diff --git a/dstalk_gui/CMakeLists.txt b/dstalk_gui/CMakeLists.txt index aa66207..013904e 100644 --- a/dstalk_gui/CMakeLists.txt +++ b/dstalk_gui/CMakeLists.txt @@ -9,6 +9,10 @@ add_executable(dstalk_gui src/main.cpp ) +set_target_properties(dstalk_gui PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +) + target_link_libraries(dstalk_gui PRIVATE dstalk diff --git a/dstalk_gui/src/main.cpp b/dstalk_gui/src/main.cpp index b6d6bbc..27ed57c 100644 --- a/dstalk_gui/src/main.cpp +++ b/dstalk_gui/src/main.cpp @@ -814,7 +814,7 @@ int main(int argc, char* argv[]) { } const char* ai_provider = dstalk_config_get("ai.provider"); - if (!ai_provider) ai_provider = "ai.openai"; + if (!ai_provider) ai_provider = "ai_openai"; g_ai_svc = static_cast(dstalk_service_query(ai_provider, 1)); g_session_svc = static_cast(dstalk_service_query("session", 1)); if (!g_ai_svc) dstalk_log(3, "AI service not found (check plugins directory)"); diff --git a/dstalk_web/src/main.cpp b/dstalk_web/src/main.cpp index 49b8053..66d155e 100644 --- a/dstalk_web/src/main.cpp +++ b/dstalk_web/src/main.cpp @@ -507,7 +507,7 @@ int main(int argc, char* argv[]) // 查询插件服务 / Query plugin services const char* ai_provider = dstalk_config_get("ai.provider"); - if (!ai_provider) ai_provider = "ai.openai"; + if (!ai_provider) ai_provider = "ai_openai"; g_ai = static_cast(dstalk_service_query(ai_provider, 1)); g_session = static_cast(dstalk_service_query("session", 1)); diff --git a/plugins_base/config/CMakeLists.txt b/plugins_base/config/CMakeLists.txt index ee4d0c5..ee55997 100644 --- a/plugins_base/config/CMakeLists.txt +++ b/plugins_base/config/CMakeLists.txt @@ -1,8 +1,8 @@ -add_library(plugin-config SHARED src/config_plugin.cpp) +add_library(plugin_config SHARED src/config_plugin.cpp) -target_link_libraries(plugin-config PRIVATE dstalk) +target_link_libraries(plugin_config PRIVATE dstalk) -set_target_properties(plugin-config PROPERTIES +set_target_properties(plugin_config PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_base/file_io/CMakeLists.txt b/plugins_base/file_io/CMakeLists.txt index 384c03f..f3b8888 100644 --- a/plugins_base/file_io/CMakeLists.txt +++ b/plugins_base/file_io/CMakeLists.txt @@ -1,8 +1,8 @@ -add_library(plugin-file_io SHARED src/file_io_plugin.cpp) +add_library(plugin_file_io SHARED src/file_io_plugin.cpp) -target_link_libraries(plugin-file_io PRIVATE dstalk) +target_link_libraries(plugin_file_io PRIVATE dstalk) -set_target_properties(plugin-file_io PROPERTIES +set_target_properties(plugin_file_io PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_base/lsp/CMakeLists.txt b/plugins_base/lsp/CMakeLists.txt index b299fdd..eba7c45 100644 --- a/plugins_base/lsp/CMakeLists.txt +++ b/plugins_base/lsp/CMakeLists.txt @@ -1,27 +1,27 @@ cmake_minimum_required(VERSION 3.21) # ============================================================ -# plugin-lsp — LSP (Language Server Protocol) 服务 +# plugin_lsp — LSP (Language Server Protocol) 服务 # 自行管理子进程,无外部服务依赖 # ============================================================ -add_library(plugin-lsp SHARED +add_library(plugin_lsp SHARED src/lsp_plugin.cpp ) -target_link_libraries(plugin-lsp PRIVATE dstalk) +target_link_libraries(plugin_lsp PRIVATE dstalk) # Boost.JSON 用于 JSON-RPC 消息构建/解析 find_package(Boost REQUIRED CONFIG) -target_link_libraries(plugin-lsp PRIVATE boost::boost dstalk_boost_config) +target_link_libraries(plugin_lsp PRIVATE boost::boost dstalk_boost_config) # POSIX 平台需要 pthread (用于 std::thread) if(NOT WIN32) find_package(Threads REQUIRED) - target_link_libraries(plugin-lsp PRIVATE Threads::Threads) + target_link_libraries(plugin_lsp PRIVATE Threads::Threads) endif() -set_target_properties(plugin-lsp PROPERTIES +set_target_properties(plugin_lsp PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_base/lsp/src/lsp_plugin.cpp b/plugins_base/lsp/src/lsp_plugin.cpp index e7ae056..46851a8 100644 --- a/plugins_base/lsp/src/lsp_plugin.cpp +++ b/plugins_base/lsp/src/lsp_plugin.cpp @@ -5,7 +5,7 @@ * Copyright (c) 2026 dstalk contributors. GPLv3. */ -// plugin-lsp — LSP (Language Server Protocol) 服务 / LSP (Language Server Protocol) service +// plugin_lsp — LSP (Language Server Protocol) 服务 / LSP (Language Server Protocol) service // // 自行管理语言服务器子进程,使用 JSON-RPC 2.0 over stdio 通信 / Self-manages language server subprocess, communicates via JSON-RPC 2.0 over stdio. // 无外部服务依赖(不依赖 http/config 等其他插件) / No external service dependencies (does not depend on http/config or other plugins). diff --git a/plugins_middle/network/CMakeLists.txt b/plugins_middle/network/CMakeLists.txt index 021a0d1..abb9e1e 100644 --- a/plugins_middle/network/CMakeLists.txt +++ b/plugins_middle/network/CMakeLists.txt @@ -1,15 +1,15 @@ find_package(Boost REQUIRED CONFIG) find_package(OpenSSL REQUIRED CONFIG) -add_library(plugin-network SHARED src/network_plugin.cpp) +add_library(plugin_network SHARED src/network_plugin.cpp) -target_link_libraries(plugin-network PRIVATE +target_link_libraries(plugin_network PRIVATE dstalk boost::boost openssl::openssl ) -set_target_properties(plugin-network PROPERTIES +set_target_properties(plugin_network PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_middle/network/src/network_plugin.cpp b/plugins_middle/network/src/network_plugin.cpp index 496733f..631fec7 100644 --- a/plugins_middle/network/src/network_plugin.cpp +++ b/plugins_middle/network/src/network_plugin.cpp @@ -278,7 +278,13 @@ static int do_post_stream( beast::get_lowest_layer(stream).expires_after( std::chrono::seconds(ctx.request_timeout)); http::read_some(stream, buffer, parser, ec); - if (ec) break; + if (ec) { + if (ec != http::error::end_of_stream && ec != asio::error::eof) { + if (g_host) g_host->log(DSTALK_LOG_WARN, + "[http] stream read error: %s", ec.message().c_str()); + } + break; + } const std::string& full_body = parser.get().body(); if (full_body.size() > processed) { @@ -301,7 +307,13 @@ static int do_post_stream( beast::get_lowest_layer(stream).expires_after( std::chrono::seconds(ctx.request_timeout)); http::read_some(stream, buffer, parser, ec); - if (ec) break; + if (ec) { + if (ec != http::error::end_of_stream && ec != asio::error::eof) { + if (g_host) g_host->log(DSTALK_LOG_WARN, + "[http] stream read error: %s", ec.message().c_str()); + } + break; + } } } diff --git a/plugins_middle/session/CMakeLists.txt b/plugins_middle/session/CMakeLists.txt index 3c68210..abc4fe5 100644 --- a/plugins_middle/session/CMakeLists.txt +++ b/plugins_middle/session/CMakeLists.txt @@ -1,11 +1,11 @@ -add_library(plugin-session SHARED src/session_plugin.cpp) +add_library(plugin_session SHARED src/session_plugin.cpp) -target_link_libraries(plugin-session PRIVATE dstalk) +target_link_libraries(plugin_session PRIVATE dstalk) find_package(Boost REQUIRED CONFIG) -target_link_libraries(plugin-session PRIVATE boost::boost dstalk_boost_config) +target_link_libraries(plugin_session PRIVATE boost::boost dstalk_boost_config) -set_target_properties(plugin-session PROPERTIES +set_target_properties(plugin_session PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_middle/session/src/session_plugin.cpp b/plugins_middle/session/src/session_plugin.cpp index 6e90140..6bf6bb7 100644 --- a/plugins_middle/session/src/session_plugin.cpp +++ b/plugins_middle/session/src/session_plugin.cpp @@ -5,7 +5,7 @@ * Copyright (c) 2026 dstalk contributors. GPLv3. */ -// plugin-session: 会话管理服务插件 / Session management service plugin +// plugin_session: 会话管理服务插件 / Session management service plugin // 提供 dstalk_session_service_t vtable 实现 / Provides dstalk_session_service_t vtable implementation // 依赖: file_io (save/load 需要文件操作) / Depends on: file_io (save/load needs file operations) #include "dstalk/dstalk_host.h" @@ -356,7 +356,7 @@ static int on_init(const dstalk_host_api_t* host) { // 查询依赖服务: file_io / Query dependency service: file_io void* raw = host->query_service("file_io", 1); if (!raw) { - host->log(DSTALK_LOG_ERROR, "[plugin-session] required service 'file_io' not found"); + host->log(DSTALK_LOG_ERROR, "[plugin_session] required service 'file_io' not found"); return -1; } g_file_io.store(static_cast(raw), std::memory_order_release); diff --git a/plugins_middle/tools/CMakeLists.txt b/plugins_middle/tools/CMakeLists.txt index 8d16bd5..ef418fc 100644 --- a/plugins_middle/tools/CMakeLists.txt +++ b/plugins_middle/tools/CMakeLists.txt @@ -1,11 +1,11 @@ -add_library(plugin-tools SHARED src/tools_plugin.cpp) +add_library(plugin_tools SHARED src/tools_plugin.cpp) -target_link_libraries(plugin-tools PRIVATE dstalk) +target_link_libraries(plugin_tools PRIVATE dstalk) find_package(Boost REQUIRED CONFIG) -target_link_libraries(plugin-tools PRIVATE boost::boost dstalk_boost_config) +target_link_libraries(plugin_tools PRIVATE boost::boost dstalk_boost_config) -set_target_properties(plugin-tools PROPERTIES +set_target_properties(plugin_tools PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_middle/tools/src/tools_plugin.cpp b/plugins_middle/tools/src/tools_plugin.cpp index 18f87db..f55dfb7 100644 --- a/plugins_middle/tools/src/tools_plugin.cpp +++ b/plugins_middle/tools/src/tools_plugin.cpp @@ -5,7 +5,7 @@ * Copyright (c) 2026 dstalk contributors. GPLv3. */ -// plugin-tools: 工具注册服务插件 / Tool registration service plugin +// plugin_tools: 工具注册服务插件 / Tool registration service plugin // 提供 dstalk_tools_service_t vtable 实现 / Provides dstalk_tools_service_t vtable implementation // 依赖: file_io (内置 file_read / file_write 工具) / Depends on: file_io (built-in file_read / file_write tools) #include "dstalk/dstalk_host.h" @@ -323,7 +323,7 @@ static int on_init(const dstalk_host_api_t* host) { // 查询依赖服务: file_io / Query dependency service: file_io void* raw = host->query_service("file_io", 1); if (!raw) { - host->log(DSTALK_LOG_ERROR, "[plugin-tools] required service 'file_io' not found"); + host->log(DSTALK_LOG_ERROR, "[plugin_tools] required service 'file_io' not found"); return -1; } g_file_io.store(static_cast(raw), std::memory_order_release); diff --git a/plugins_upper/CMakeLists.txt b/plugins_upper/CMakeLists.txt index cd1de25..f274066 100644 --- a/plugins_upper/CMakeLists.txt +++ b/plugins_upper/CMakeLists.txt @@ -2,6 +2,7 @@ # 依赖其他插件的插件 / Plugins depending on non-base plugins # ============================================================ +add_subdirectory(ai_common) # 共享 AI 工具库(静态库)/ shared AI utility library (static) add_subdirectory(context) # 依赖 session / depends on session -add_subdirectory(openai) # 依赖 http, config / depends on http, config -add_subdirectory(anthropic) # 依赖 http, config / depends on http, config +add_subdirectory(openai) # 依赖 http, config, ai_common / depends on http, config, ai_common +add_subdirectory(anthropic) # 依赖 http, config, ai_common / depends on http, config, ai_common diff --git a/plugins_upper/ai_common/CMakeLists.txt b/plugins_upper/ai_common/CMakeLists.txt new file mode 100644 index 0000000..ac52266 --- /dev/null +++ b/plugins_upper/ai_common/CMakeLists.txt @@ -0,0 +1,20 @@ +# ============================================================ +# ai_common — 共享 AI 插件工具库(静态库)/ Shared AI plugin utility library (static) +# ============================================================ + +find_package(Boost REQUIRED CONFIG) + +add_library(ai_common STATIC + src/ai_common.cpp +) + +target_include_directories(ai_common PUBLIC include) + +target_compile_features(ai_common PUBLIC cxx_std_20) + +target_link_libraries(ai_common + PUBLIC + dstalk + dstalk_boost_config + boost::boost +) diff --git a/plugins_upper/ai_common/include/ai_common.hpp b/plugins_upper/ai_common/include/ai_common.hpp new file mode 100644 index 0000000..bf4f782 --- /dev/null +++ b/plugins_upper/ai_common/include/ai_common.hpp @@ -0,0 +1,118 @@ +/* + * @file ai_common.hpp + * @brief Shared types and utilities for AI provider plugins (OpenAI / Anthropic). + * AI 提供者插件(OpenAI / Anthropic)的共享类型和工具函数。 + * Copyright (c) 2026 dstalk contributors. GPLv3. + */ + +#pragma once + +#include "dstalk/dstalk_host.h" +#include "dstalk/dstalk_services.h" + +#include +#include +#include + +namespace dstalk_ai { + +namespace json = boost::json; + +// ============================================================================ +// 共享类型 / Shared types +// ============================================================================ + +/// Provider connection configuration / 服务商连接配置 +struct PluginConfig { + std::string provider; + std::string base_url; + std::string api_key; + std::string model; + int max_tokens = 4096; + double temperature = 0.7; +}; + +/// Per-index tool-call accumulator for SSE streaming / SSE 流式传输的按索引工具调用累积器 +struct ToolCallAccum { + int index = -1; + std::string id; + std::string name; + std::string arguments; // 增量拼接的 JSON arguments / incrementally concatenated JSON arguments +}; + +/// Streaming context passed through SSE callbacks / 通过 SSE 回调传递的流式上下文 +struct StreamContext { + const dstalk_host_api_t* host = nullptr; + dstalk_stream_cb user_cb = nullptr; + void* userdata = nullptr; + std::string accumulated; + std::vector tool_calls; + int sse_parse_errors = 0; // 连续 SSE 解析错误计数器 / consecutive SSE parse error counter + bool streaming_ok = true; // OpenAI: tracks stream health + bool saw_data_line = false; // Anthropic: tracks if any SSE data received +}; + +/// Maximum consecutive SSE parse errors before aborting the stream / 中止流之前的最大连续 SSE 解析错误数 +inline constexpr int kMaxSseParseErrors = 5; + +// ============================================================================ +// 函数声明(实现于 ai_common.cpp) / Function declarations (implemented in ai_common.cpp) +// ============================================================================ + +/// Securely zero memory through volatile write to prevent compiler optimization. +/// 通过 volatile 写入零来安全擦除内存,防止编译器优化。 +void secure_zero(void* p, size_t n); + +/// Parse a URL into scheme, host, port, and target path components. +/// 将 URL 解析为 scheme、host、port 和 target path 组件。 +bool extract_host_port(const std::string& url, + std::string& scheme_out, std::string& host_out, + std::string& port_out, std::string& target_out); + +// ============================================================================ +// 内联工具函数 / Inline utility functions +// ============================================================================ + +/// Free all host-allocated string fields in a chat result struct. +/// 释放 chat result 结构体中所有主机分配的字符串字段。 +inline void free_chat_result(const dstalk_host_api_t* host, dstalk_chat_result_t* result) { + if (!result || !host) return; + if (result->content) { host->free((void*)result->content); result->content = nullptr; } + if (result->error) { host->free((void*)result->error); result->error = nullptr; } + if (result->tool_calls_json) { host->free((void*)result->tool_calls_json); result->tool_calls_json = nullptr; } +} + +/// Cache tools_json from the tools service for reuse in chat/chat_stream. +/// 从 tools service 缓存 tools_json,供 chat/chat_stream 复用。 +inline void cache_tools_json(const dstalk_host_api_t* host, std::string& tools_json) { + if (!host) return; + auto* tools_svc = reinterpret_cast( + host->query_service("tools", 1)); + if (tools_svc && tools_svc->get_tools_json) { + char* j = tools_svc->get_tools_json(); + if (j) { + tools_json = j; + host->free(j); + } + } +} + +/// Serialize accumulated tool_calls into OpenAI-compatible JSON array. +/// 将累积的 tool_calls 序列化为兼容 OpenAI 格式的 JSON 数组。 +inline std::string serialize_tool_calls(const std::vector& tool_calls) { + json::array tc_array; + for (const auto& tc : tool_calls) { + json::object tc_obj; + tc_obj["index"] = tc.index; + if (!tc.id.empty()) tc_obj["id"] = tc.id; + tc_obj["type"] = "function"; + json::object func; + if (!tc.name.empty()) func["name"] = tc.name; + func["arguments"] = tc.arguments; + tc_obj["function"] = func; + tc_array.push_back(std::move(tc_obj)); + } + return json::serialize(tc_array); +} + +} // namespace dstalk_ai diff --git a/plugins_upper/ai_common/src/ai_common.cpp b/plugins_upper/ai_common/src/ai_common.cpp new file mode 100644 index 0000000..1100f30 --- /dev/null +++ b/plugins_upper/ai_common/src/ai_common.cpp @@ -0,0 +1,39 @@ +/* + * @file ai_common.cpp + * @brief Shared utility implementations for AI provider plugins. + * AI 提供者插件的共享工具函数实现。 + * Copyright (c) 2026 dstalk contributors. GPLv3. + */ + +#include "ai_common.hpp" + +namespace dstalk_ai { + +void secure_zero(void* p, size_t n) { + volatile char* vp = static_cast(p); + while (n--) *vp++ = 0; +} + +bool extract_host_port(const std::string& url, + std::string& scheme_out, std::string& host_out, + std::string& port_out, std::string& target_out) +{ + size_t scheme_end = url.find("://"); + if (scheme_end == std::string::npos) return false; + scheme_out = url.substr(0, scheme_end); + std::string rest = url.substr(scheme_end + 3); + size_t slash = rest.find('/'); + std::string authority = (slash != std::string::npos) ? rest.substr(0, slash) : rest; + target_out = (slash != std::string::npos) ? rest.substr(slash) : "/"; + size_t colon = authority.rfind(':'); + if (colon != std::string::npos) { + host_out = authority.substr(0, colon); + port_out = authority.substr(colon + 1); + } else { + host_out = authority; + port_out = (scheme_out == "https") ? "443" : "80"; + } + return true; +} + +} // namespace dstalk_ai diff --git a/plugins_upper/anthropic/CMakeLists.txt b/plugins_upper/anthropic/CMakeLists.txt index d02d5c7..d0b90f5 100644 --- a/plugins_upper/anthropic/CMakeLists.txt +++ b/plugins_upper/anthropic/CMakeLists.txt @@ -1,21 +1,21 @@ cmake_minimum_required(VERSION 3.21) # ============================================================ -# plugin-anthropic — Anthropic Claude AI 服务 +# plugin_anthropic — Anthropic Claude AI 服务 # 依赖: http 服务 (查询), config 服务 (查询) # ============================================================ -add_library(plugin-anthropic SHARED +add_library(plugin_anthropic SHARED src/anthropic_plugin.cpp ) -target_link_libraries(plugin-anthropic PRIVATE dstalk) +target_link_libraries(plugin_anthropic PRIVATE dstalk ai_common) # Boost.JSON 用于构建/解析请求和响应 find_package(Boost REQUIRED CONFIG) -target_link_libraries(plugin-anthropic PRIVATE boost::boost dstalk_boost_config) +target_link_libraries(plugin_anthropic PRIVATE boost::boost dstalk_boost_config) -set_target_properties(plugin-anthropic PROPERTIES +set_target_properties(plugin_anthropic PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_upper/anthropic/src/anthropic_plugin.cpp b/plugins_upper/anthropic/src/anthropic_plugin.cpp index 432367d..afbff5b 100644 --- a/plugins_upper/anthropic/src/anthropic_plugin.cpp +++ b/plugins_upper/anthropic/src/anthropic_plugin.cpp @@ -7,6 +7,7 @@ #include "dstalk/dstalk_host.h" #include "dstalk/dstalk_services.h" +#include "ai_common.hpp" #include #include @@ -19,60 +20,18 @@ namespace json = boost::json; // ============================================================================ // 全局指针 — W17.4: std::atomic 保护 on_shutdown 与 service 函数并发读写 / Global pointers — W17.4: std::atomic protects concurrent read/write between on_shutdown and service functions +// W21.5: g_config 改为 atomic,修复数据竞争 / g_config changed to atomic, fixing data race // ============================================================================ static std::atomic g_host{nullptr}; static std::atomic g_http{nullptr}; -static dstalk_config_service_t* g_config = nullptr; +static std::atomic g_config{nullptr}; // ============================================================================ // 配置数据 / Config data // ============================================================================ -struct PluginConfig { - std::string provider; - std::string base_url; - std::string api_key; - std::string model; - int max_tokens = 4096; - double temperature = 0.7; -}; -static PluginConfig g_cfg; +static dstalk_ai::PluginConfig g_cfg; static std::string g_tools_json; // W21.2: 由 configure() 缓存,供 chat/chat_stream 使用 / cached by configure(), consumed by chat/chat_stream -// ============================================================================ -// 安全擦除:用 volatile 写零循环防止编译器优化 / Secure erase: write zero loop through volatile to prevent compiler optimization -// ============================================================================ -// 通过 volatile 写入零来安全擦除内存,防止编译器优化 / Securely zero out memory by writing through volatile to prevent compiler optimization. -static void secure_zero(void* p, size_t n) { - volatile char* vp = (volatile char*)p; - while (n--) *vp++ = 0; -} - -// ============================================================================ -// 辅助:提取 host / target / Helper: extract host / target -// ============================================================================ -// 将 URL 解析为 scheme、host、port 和 target path 组件 / Parse a URL into scheme, host, port, and target path components. -static bool extract_host_port(const std::string& url, - std::string& scheme_out, std::string& host_out, - std::string& port_out, std::string& target_out) -{ - size_t scheme_end = url.find("://"); - if (scheme_end == std::string::npos) return false; - scheme_out = url.substr(0, scheme_end); - std::string rest = url.substr(scheme_end + 3); - size_t slash = rest.find('/'); - std::string authority = (slash != std::string::npos) ? rest.substr(0, slash) : rest; - target_out = (slash != std::string::npos) ? rest.substr(slash) : "/"; - size_t colon = authority.rfind(':'); - if (colon != std::string::npos) { - host_out = authority.substr(0, colon); - port_out = authority.substr(colon + 1); - } else { - host_out = authority; - port_out = (scheme_out == "https") ? "443" : "80"; - } - return true; -} - // ============================================================================ // 构建 Anthropic headers JSON / Build Anthropic headers JSON // ============================================================================ @@ -151,10 +110,11 @@ static std::string build_request_json( // 将非流式 JSON 响应体解析为 dstalk_chat_result_t。 // 处理 text 和 tool_use content block,将 tool_use 转换为 OpenAI 格式 / Parse a non-streaming JSON response body into a dstalk_chat_result_t. // Handles text and tool_use content blocks, converting tool_use to OpenAI format. -static void parse_response(const char* body, int http_status, +// W21.5: 添加 host nullptr 守卫,防止空指针解引用 / Added host nullptr guard to prevent null dereference +static void parse_response(const dstalk_host_api_t* host, + const char* body, int http_status, dstalk_chat_result_t& r) { - const auto* h = g_host.load(std::memory_order_acquire); r.http_status = http_status; if (http_status < 200 || http_status >= 300) { @@ -164,16 +124,16 @@ static void parse_response(const char* body, int http_status, auto obj = jv.as_object(); if (obj.contains("error")) { auto err = obj["error"].as_object(); - r.error = h->strdup( - json::value_to(err["message"]).c_str()); + r.error = host ? host->strdup( + json::value_to(err["message"]).c_str()) : nullptr; } } catch (...) { std::string msg = "HTTP " + std::to_string(http_status); - r.error = h->strdup(msg.c_str()); + r.error = host ? host->strdup(msg.c_str()) : nullptr; } - if (!r.error) { + if (!r.error && host) { std::string msg = "HTTP " + std::to_string(http_status); - r.error = h->strdup(msg.c_str()); + r.error = host->strdup(msg.c_str()); } r.content = nullptr; r.tool_calls_json = nullptr; @@ -210,14 +170,14 @@ static void parse_response(const char* body, int http_status, } if (!tool_use_blocks.empty()) { - r.tool_calls_json = h->strdup( - json::serialize(tool_use_blocks).c_str()); + r.tool_calls_json = host ? host->strdup( + json::serialize(tool_use_blocks).c_str()) : nullptr; } else { r.tool_calls_json = nullptr; } if (!text_content.empty()) { - r.content = h->strdup(text_content.c_str()); + r.content = host ? host->strdup(text_content.c_str()) : nullptr; r.ok = 1; r.error = nullptr; return; @@ -229,22 +189,22 @@ static void parse_response(const char* body, int http_status, return; } r.ok = 0; - r.error = h->strdup("no text or tool_use content block found"); + r.error = host ? host->strdup("no text or tool_use content block found") : nullptr; } else { r.ok = 0; - r.error = h->strdup("empty response"); + r.error = host ? host->strdup("empty response") : nullptr; } r.content = nullptr; r.tool_calls_json = nullptr; } catch (std::exception& e) { r.ok = 0; std::string msg = std::string("json parse: ") + e.what(); - r.error = h->strdup(msg.c_str()); + r.error = host ? host->strdup(msg.c_str()) : nullptr; r.content = nullptr; r.tool_calls_json = nullptr; } catch (...) { r.ok = 0; - r.error = h->strdup("json parse error"); + r.error = host ? host->strdup("json parse error") : nullptr; r.content = nullptr; r.tool_calls_json = nullptr; } @@ -254,23 +214,6 @@ static void parse_response(const char* body, int http_status, // SSE 事件解析(Anthropic 格式: event/content_block_delta) / SSE event parsing (Anthropic format: event/content_block_delta) // ============================================================================ -// W21.2: 按 content_block index 累积 Anthropic tool_use 增量 / Accumulate Anthropic tool_use increments by content_block index -struct ToolCallAccum { - int index = -1; - std::string id; - std::string name; - std::string arguments; // 从 input_json_delta.partial_json 累积 / accumulated from input_json_delta.partial_json -}; - -struct StreamContext { - const dstalk_host_api_t* host; - dstalk_stream_cb user_cb; - void* userdata; - std::string accumulated; - bool saw_data_line = false; - std::vector tool_calls; // W21.2: 按 index 累积 tool_use content blocks / accumulate tool_use content blocks by index -}; - // W21.2: 解析 Anthropic SSE 事件,含 tool_use content_block 增量解析 / Parse Anthropic SSE events with tool_use content_block incremental parsing // 解析单个 Anthropic SSE "data:" JSON 事件。处理 content_block_start、 // content_block_delta (text_delta/input_json_delta) 和 message_stop。 @@ -278,7 +221,7 @@ struct StreamContext { // content_block_delta (text_delta/input_json_delta), and message_stop. // Returns true if a content token was produced, false otherwise. static bool parse_sse_data(const std::string& data, std::string& token_out, - StreamContext* ctx) + dstalk_ai::StreamContext* ctx) { try { auto jv = json::parse(data); @@ -313,6 +256,7 @@ static bool parse_sse_data(const std::string& data, std::string& token_out, if (cb_obj.contains("name") && cb_obj["name"].is_string()) acc.name = json::value_to(cb_obj["name"]); } + if (ctx) ctx->sse_parse_errors = 0; // 成功解析 / successful parse return false; } @@ -329,6 +273,7 @@ static bool parse_sse_data(const std::string& data, std::string& token_out, auto* text = dobj.if_contains("text"); if (text && text->is_string()) { token_out = json::value_to(*text); + if (ctx) ctx->sse_parse_errors = 0; // 成功解析 / successful parse return true; } } else if (delta_type == "input_json_delta" && ctx) { @@ -343,15 +288,29 @@ static bool parse_sse_data(const std::string& data, std::string& token_out, json::value_to(*pj); } } + ctx->sse_parse_errors = 0; // 成功解析 / successful parse return false; } } else if (type == "message_stop") { token_out.clear(); + if (ctx) ctx->sse_parse_errors = 0; // 成功解析 / successful parse return true; // 流结束 / stream end } // 忽略: message_start, content_block_stop, ping, message_delta / Ignore: message_start, content_block_stop, ping, message_delta + // 已知事件类型但无需处理 — 重置计数器 / known event type but no processing needed — reset counter + if (ctx) ctx->sse_parse_errors = 0; } catch (...) { - // 解析失败忽略 / Ignore parse failures + if (ctx) { + ctx->sse_parse_errors++; + const auto* log_host = g_host.load(std::memory_order_acquire); + if (log_host) { + if (ctx->sse_parse_errors == 1 || ctx->sse_parse_errors % 5 == 0) { + log_host->log(DSTALK_LOG_WARN, + "[anthropic] SSE parse error (#%d consecutive)", + ctx->sse_parse_errors); + } + } + } } return false; } @@ -375,15 +334,7 @@ static int my_configure(const char* provider, const char* base_url, const auto* h = g_host.load(std::memory_order_acquire); if (h) { // W21.2: 从 tools service 缓存 tools_json,供 chat/chat_stream 复用 / Cache tools_json from tools service for reuse in chat/chat_stream - auto* tools_svc = reinterpret_cast( - h->query_service("tools", 1)); - if (tools_svc && tools_svc->get_tools_json) { - char* json = tools_svc->get_tools_json(); - if (json) { - g_tools_json = json; - h->free(json); - } - } + dstalk_ai::cache_tools_json(h, g_tools_json); h->log(DSTALK_LOG_INFO, "[anthropic] configured: model=%s base_url=%s max_tokens=%d temperature=%.2f", @@ -419,12 +370,12 @@ static dstalk_chat_result_t my_chat( const auto* http = g_http.load(std::memory_order_acquire); if (!http) { - r.error = host->strdup("http service not available"); + r.error = host ? host->strdup("http service not available") : nullptr; return r; } std::string scheme, hostname, port, target; - extract_host_port(g_cfg.base_url, scheme, hostname, port, target); + dstalk_ai::extract_host_port(g_cfg.base_url, scheme, hostname, port, target); std::string target_path = target + "/v1/messages"; std::string body = build_request_json(history, history_len, @@ -441,15 +392,15 @@ static dstalk_chat_result_t my_chat( headers_json.c_str(), &response_body, &status_code); if (ret != 0) { - r.error = host->strdup("http request failed"); - if (response_body) host->free(response_body); + r.error = host ? host->strdup("http request failed") : nullptr; + if (response_body && host) host->free(response_body); return r; } - parse_response(response_body, status_code, r); + parse_response(host, response_body, status_code, r); if (response_body) { - host->free(response_body); + if (host) host->free(response_body); } return r; } catch (const std::exception& e) { @@ -473,12 +424,11 @@ static dstalk_chat_result_t my_chat( // chat_stream / chat_stream // ============================================================================ -// 行回调 / SSE line callback // SSE 行回调:解析每个 Anthropic SSE 行并将文本 token 转发给用户 / SSE line callback: parses each Anthropic SSE line and forwards text tokens to user. static int sse_line_callback(const char* line, void* userdata) { try { - auto* ctx = static_cast(userdata); + auto* ctx = static_cast(userdata); if (!line || !line[0]) return 1; // 空行,继续 / empty line, continue std::string line_str(line); @@ -489,6 +439,16 @@ static int sse_line_callback(const char* line, void* userdata) std::string token; if (parse_sse_data(data, token, ctx)) { ctx->saw_data_line = true; + + // W21.5: 连续 SSE 解析错误超过阈值,中止流 / consecutive SSE parse errors exceed threshold, abort stream + if (ctx->sse_parse_errors >= dstalk_ai::kMaxSseParseErrors) { + const auto* h = g_host.load(std::memory_order_acquire); + if (h) h->log(DSTALK_LOG_ERROR, + "[anthropic] SSE stream aborted: %d consecutive parse errors", + ctx->sse_parse_errors); + return 0; + } + if (token.empty()) { // message_stop / message_stop return 0; @@ -528,12 +488,12 @@ static dstalk_chat_result_t my_chat_stream( const auto* http = g_http.load(std::memory_order_acquire); if (!http) { - r.error = host->strdup("http service not available"); + r.error = host ? host->strdup("http service not available") : nullptr; return r; } std::string scheme, hostname, port, target; - extract_host_port(g_cfg.base_url, scheme, hostname, port, target); + dstalk_ai::extract_host_port(g_cfg.base_url, scheme, hostname, port, target); std::string target_path = target + "/v1/messages"; std::string body = build_request_json(history, history_len, @@ -541,7 +501,7 @@ static dstalk_chat_result_t my_chat_stream( std::string headers_json = build_headers_json(); - StreamContext ctx; + dstalk_ai::StreamContext ctx; ctx.host = host; ctx.user_cb = cb; ctx.userdata = userdata; @@ -567,25 +527,27 @@ static dstalk_chat_result_t my_chat_stream( auto obj = jv.as_object(); if (obj.contains("error")) { auto err = obj["error"].as_object(); - r.error = host->strdup( - json::value_to(err["message"]).c_str()); + r.error = host ? host->strdup( + json::value_to(err["message"]).c_str()) : nullptr; } - } catch (...) {} + } catch (...) { + if (host) host->log(DSTALK_LOG_WARN, "[anthropic] SSE error body parse error (ignored)"); + } } - if (!r.error) { + if (!r.error && host) { if (status_code <= 0) r.error = host->strdup("transport error"); else r.error = host->strdup( ("HTTP " + std::to_string(status_code)).c_str()); } - if (response_body) host->free(response_body); + if (response_body && host) host->free(response_body); r.content = nullptr; r.tool_calls_json = nullptr; return r; } - if (response_body) host->free(response_body); + if (response_body && host) host->free(response_body); // W21.2: 成功条件 = 有内容 OR 有 tool_calls(tool-only 响应如 function calling) / Success = has content OR has tool_calls (tool-only responses like function calling) bool has_content = !ctx.accumulated.empty(); @@ -593,7 +555,7 @@ static dstalk_chat_result_t my_chat_stream( if (!has_content && !has_tool_calls) { r.ok = 0; - r.error = host->strdup("no content received"); + r.error = host ? host->strdup("no content received") : nullptr; r.content = nullptr; r.tool_calls_json = nullptr; } else { @@ -604,19 +566,7 @@ static dstalk_chat_result_t my_chat_stream( // W21.2: 序列化累积的 tool_calls 为 JSON(兼容 OpenAI tool_calls 格式) / Serialize accumulated tool_calls to JSON (OpenAI-compatible format) if (has_tool_calls) { - json::array tc_array; - for (auto& tc : ctx.tool_calls) { - json::object tc_obj; - tc_obj["index"] = tc.index; - if (!tc.id.empty()) tc_obj["id"] = tc.id; - tc_obj["type"] = "function"; - json::object func; - if (!tc.name.empty()) func["name"] = tc.name; - func["arguments"] = tc.arguments; - tc_obj["function"] = func; - tc_array.push_back(std::move(tc_obj)); - } - std::string tc_json = json::serialize(tc_array); + std::string tc_json = dstalk_ai::serialize_tool_calls(ctx.tool_calls); r.tool_calls_json = host ? host->strdup(tc_json.c_str()) : nullptr; } else { r.tool_calls_json = nullptr; @@ -647,10 +597,7 @@ static dstalk_chat_result_t my_chat_stream( static void my_free_result(dstalk_chat_result_t* result) { const auto* h = g_host.load(std::memory_order_acquire); - if (!result || !h) return; - if (result->content) { h->free((void*)result->content); result->content = nullptr; } - if (result->error) { h->free((void*)result->error); result->error = nullptr; } - if (result->tool_calls_json) { h->free((void*)result->tool_calls_json); result->tool_calls_json = nullptr; } + dstalk_ai::free_chat_result(h, result); } // ============================================================================ @@ -674,7 +621,9 @@ static int on_init(const dstalk_host_api_t* host) auto* http_svc = (dstalk_http_service_t*)host->query_service("http", 1); g_http.store(http_svc, std::memory_order_release); - g_config = (dstalk_config_service_t*)host->query_service("config", 1); + // W21.5: atomic store 替代裸指针赋值 / atomic store replaces raw pointer assignment + auto* cfg_svc = (dstalk_config_service_t*)host->query_service("config", 1); + g_config.store(cfg_svc, std::memory_order_release); if (!http_svc) { if (host) host->log(DSTALK_LOG_ERROR, "[anthropic] http service not found"); @@ -683,7 +632,7 @@ static int on_init(const dstalk_host_api_t* host) if (host) host->log(DSTALK_LOG_INFO, "[anthropic] initializing Anthropic AI plugin"); - return host->register_service("ai.anthropic", 1, &g_service); + return host->register_service("ai_anthropic", 1, &g_service); } catch (const std::exception& e) { const auto* h = g_host.load(std::memory_order_acquire); if (h && h->log) h->log(DSTALK_LOG_ERROR, "[anthropic] on_init exception: %s", e.what()); @@ -701,10 +650,11 @@ static void on_shutdown() try { const auto* h = g_host.load(std::memory_order_acquire); if (h) h->log(DSTALK_LOG_INFO, "[anthropic] shutdown"); - secure_zero(g_cfg.api_key.data(), g_cfg.api_key.size()); + dstalk_ai::secure_zero(g_cfg.api_key.data(), g_cfg.api_key.size()); g_cfg.api_key.clear(); g_http.store(nullptr, std::memory_order_release); - g_config = nullptr; + // W21.5: atomic store 替代裸指针赋值,消除数据竞争 / atomic store replaces raw pointer assignment, eliminates data race + g_config.store(nullptr, std::memory_order_release); g_host.store(nullptr, std::memory_order_release); } catch (const std::exception& e) { const auto* h = g_host.load(std::memory_order_acquire); @@ -719,7 +669,7 @@ static void on_shutdown() // 插件描述符 / Plugin descriptor // ============================================================================ static dstalk_plugin_info_t g_info = { - /* .name = */ "anthropic-ai", + /* .name = */ "anthropic_ai", /* .version = */ "1.0.0", /* .description = */ "Anthropic Claude AI provider (Messages API) / Anthropic Claude AI 提供者 (Messages API)", /* .api_version = */ DSTALK_API_VERSION, diff --git a/plugins_upper/context/CMakeLists.txt b/plugins_upper/context/CMakeLists.txt index 5029e9a..0099ee9 100644 --- a/plugins_upper/context/CMakeLists.txt +++ b/plugins_upper/context/CMakeLists.txt @@ -1,8 +1,8 @@ -add_library(plugin-context SHARED src/context_plugin.cpp) +add_library(plugin_context SHARED src/context_plugin.cpp) -target_link_libraries(plugin-context PRIVATE dstalk) +target_link_libraries(plugin_context PRIVATE dstalk) -set_target_properties(plugin-context PROPERTIES +set_target_properties(plugin_context PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins" diff --git a/plugins_upper/context/src/context_plugin.cpp b/plugins_upper/context/src/context_plugin.cpp index d268f3a..e6daab7 100644 --- a/plugins_upper/context/src/context_plugin.cpp +++ b/plugins_upper/context/src/context_plugin.cpp @@ -5,7 +5,7 @@ * Copyright (c) 2026 dstalk contributors. GPLv3. */ -// plugin-context: 上下文管理服务插件 / Context management service plugin +// plugin_context: 上下文管理服务插件 / Context management service plugin // 提供 dstalk_context_service_t vtable 实现 / Provides dstalk_context_service_t vtable implementation // 依赖: session (获取历史消息做 token 计数) / Depends on: session (get history messages for token counting) #include "dstalk/dstalk_host.h" @@ -263,14 +263,21 @@ static int trim_impl(const dstalk_message_t* in, int in_count, system_tokens, max_tokens); } - // 检查是否有单条消息超过限制 / Check if any single message exceeds the limit + // Precompute per-message token counts for non_system_msgs so that the + // trim pass below is O(N) instead of O(N*K) (no re-counting per iteration). + std::vector ns_token_counts; + ns_token_counts.reserve(non_system_msgs.size()); for (const auto& msg : non_system_msgs) { - size_t msg_tokens = count_tokens_trim(msg); - if (msg_tokens > max_tokens) { + ns_token_counts.push_back(count_tokens_trim(msg)); + } + + // 检查是否有单条消息超过限制 / Check if any single message exceeds the limit + for (size_t i = 0; i < non_system_msgs.size(); ++i) { + if (ns_token_counts[i] > max_tokens) { std::fprintf(stderr, "[context] WARNING: single message " "(%s, %zu tokens) exceeds max_context_tokens (%zu). " "Returning empty list.\n", - msg.role.c_str(), msg_tokens, max_tokens); + non_system_msgs[i].role.c_str(), ns_token_counts[i], max_tokens); *out = nullptr; *out_count = 0; return -1; @@ -278,31 +285,53 @@ static int trim_impl(const dstalk_message_t* in, int in_count, } // 从最早的非 system 消息开始裁剪,确保 user/assistant 成对移除 / Trim from earliest non-system messages, ensuring user/assistant pairs are removed together - while (!non_system_msgs.empty()) { - current = system_tokens + count_tokens_trim_vec(non_system_msgs); - if (current <= max_tokens) break; + // O(N): precompute token counts once, then mark removal candidates in a single forward pass + { + size_t ns_total = 0; + for (size_t t : ns_token_counts) ns_total += t; + current = system_tokens + ns_total; - // 找第一个 "user" 消息 / Find first "user" message - auto user_it = non_system_msgs.begin(); - while (user_it != non_system_msgs.end() && user_it->role != "user") { - ++user_it; - } - if (user_it == non_system_msgs.end()) break; + if (current > max_tokens) { + std::vector keep(non_system_msgs.size(), true); + size_t idx = 0; + while (idx < non_system_msgs.size() && current > max_tokens) { + // 找第一个 "user" 消息 / Find first "user" message + while (idx < non_system_msgs.size() && non_system_msgs[idx].role != "user") { + ++idx; + } + if (idx >= non_system_msgs.size()) break; - // 找下一个 "assistant" / Find next "assistant" - auto assistant_it = user_it + 1; - while (assistant_it != non_system_msgs.end() && assistant_it->role != "assistant") { - ++assistant_it; - } + size_t user_idx = idx; + ++idx; - if (assistant_it == non_system_msgs.end()) { - non_system_msgs.erase(user_it); - } else { - // 先删 assistant 再删 user 避免迭代器失效 / Delete assistant first then user to avoid iterator invalidation - non_system_msgs.erase(assistant_it); - user_it = non_system_msgs.begin(); - while (user_it != non_system_msgs.end() && user_it->role != "user") ++user_it; - if (user_it != non_system_msgs.end()) non_system_msgs.erase(user_it); + // 找下一个 "assistant" / Find next "assistant" + while (idx < non_system_msgs.size() && non_system_msgs[idx].role != "assistant") { + ++idx; + } + + if (idx >= non_system_msgs.size()) { + // 没有配对的 assistant,只移除 user / No paired assistant, remove user only + keep[user_idx] = false; + current -= ns_token_counts[user_idx]; + idx = user_idx + 1; // restart search after the removed message + } else { + // 移除 user + assistant 对 / Remove user + assistant pair + keep[user_idx] = false; + keep[idx] = false; + current -= ns_token_counts[user_idx] + ns_token_counts[idx]; + ++idx; + } + } + + // Rebuild non_system_msgs with only kept messages (single O(N) pass) + std::vector kept; + kept.reserve(non_system_msgs.size()); + for (size_t i = 0; i < non_system_msgs.size(); ++i) { + if (keep[i]) { + kept.push_back(std::move(non_system_msgs[i])); + } + } + non_system_msgs = std::move(kept); } } @@ -310,8 +339,10 @@ static int trim_impl(const dstalk_message_t* in, int in_count, { size_t max_msg_count = (max_tokens + 99) / 100; // ceil(max_tokens / 100) if (max_msg_count < 1) max_msg_count = 1; - while (non_system_msgs.size() > max_msg_count) { - non_system_msgs.erase(non_system_msgs.begin()); + // O(N) single range-erase instead of O(N²) repeated erase(begin()) + if (non_system_msgs.size() > max_msg_count) { + size_t to_remove = non_system_msgs.size() - max_msg_count; + non_system_msgs.erase(non_system_msgs.begin(), non_system_msgs.begin() + to_remove); } } @@ -397,17 +428,17 @@ static int on_init(const dstalk_host_api_t* host) { // 查询依赖服务: session / Query dependency service: session void* raw = host->query_service("session", 1); if (!raw) { - host->log(DSTALK_LOG_ERROR, "[plugin-context] required service 'session' not found"); + host->log(DSTALK_LOG_ERROR, "[plugin_context] required service 'session' not found"); return -1; } g_session = static_cast(raw); return host->register_service("context", 1, &g_context_service); } catch (const std::exception& e) { - if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin-context] on_init exception: %s", e.what()); + if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin_context] on_init exception: %s", e.what()); return -1; } catch (...) { - if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin-context] on_init unknown exception"); + if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin_context] on_init unknown exception"); return -1; } } @@ -419,11 +450,11 @@ static void on_shutdown() { g_session = nullptr; g_host = nullptr; } catch (const std::exception& e) { - if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin-context] on_shutdown: %s", e.what()); + if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin_context] on_shutdown: %s", e.what()); g_session = nullptr; g_host = nullptr; } catch (...) { - if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin-context] on_shutdown: unknown exception"); + if (g_host) g_host->log(DSTALK_LOG_ERROR, "[plugin_context] on_shutdown: unknown exception"); g_session = nullptr; g_host = nullptr; } diff --git a/plugins_upper/openai/CMakeLists.txt b/plugins_upper/openai/CMakeLists.txt index 8b4782c..242d00e 100644 --- a/plugins_upper/openai/CMakeLists.txt +++ b/plugins_upper/openai/CMakeLists.txt @@ -1,19 +1,19 @@ # ============================================================ -# plugin-openai — OpenAI 兼容 AI 服务 / OpenAI-compatible AI service +# plugin_openai — OpenAI 兼容 AI 服务 / OpenAI-compatible AI service # ============================================================ find_package(Boost REQUIRED CONFIG) -add_library(plugin-openai SHARED +add_library(plugin_openai SHARED src/openai_plugin.cpp ) -target_link_libraries(plugin-openai PRIVATE dstalk) +target_link_libraries(plugin_openai PRIVATE dstalk ai_common) # Boost.JSON (header-only) -target_link_libraries(plugin-openai PRIVATE boost::boost dstalk_boost_config) +target_link_libraries(plugin_openai PRIVATE boost::boost dstalk_boost_config) -set_target_properties(plugin-openai PROPERTIES +set_target_properties(plugin_openai PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins diff --git a/plugins_upper/openai/src/openai_plugin.cpp b/plugins_upper/openai/src/openai_plugin.cpp index 3aff8d9..ce2e5ff 100644 --- a/plugins_upper/openai/src/openai_plugin.cpp +++ b/plugins_upper/openai/src/openai_plugin.cpp @@ -7,6 +7,7 @@ #include "dstalk/dstalk_host.h" #include "dstalk/dstalk_services.h" +#include "ai_common.hpp" #include #include @@ -18,7 +19,7 @@ namespace json = boost::json; // ============================================================================ -// 全局指针:从 on_init 获取(W14.3: atomic acquire/release 保护读写竞态) / Global pointers: obtained from on_init (W14.3: atomic acquire/release protects read/write races) +// 全局指针:从 on_init 获取(atomic acquire/release 保护读写竞态) / Global pointers: obtained from on_init (atomic acquire/release protects read/write races) // ============================================================================ static std::atomic g_host{nullptr}; static std::atomic g_http{nullptr}; @@ -27,52 +28,9 @@ static std::atomic g_config{nullptr}; // ============================================================================ // 配置数据(由 configure() 设置) / Config data (set by configure()) // ============================================================================ -struct PluginConfig { - std::string provider; - std::string base_url; - std::string api_key; - std::string model; - int max_tokens = 4096; - double temperature = 0.7; -}; -static PluginConfig g_cfg; +static dstalk_ai::PluginConfig g_cfg; static std::string g_tools_json; // W20.2: 由 configure() 缓存,供 chat/chat_stream 使用 / cached by configure(), consumed by chat/chat_stream -// ============================================================================ -// 安全擦除:用 volatile 写零循环防止编译器优化 / Secure erase: write zero loop through volatile to prevent compiler optimization -// ============================================================================ -// 通过 volatile 写入零来安全擦除内存,防止编译器优化 / Securely zero out memory by writing through volatile to prevent compiler optimization. -static void secure_zero(void* p, size_t n) { - volatile char* vp = (volatile char*)p; - while (n--) *vp++ = 0; -} - -// ============================================================================ -// 辅助:从 base_url 提取 host 和 target / Helper: extract host and target from base_url -// ============================================================================ -// 将 URL 解析为 scheme、host、port 和 target path 组件 / Parse a URL into scheme, host, port, and target path components. -static bool extract_host_port(const std::string& url, - std::string& scheme_out, std::string& host_out, - std::string& port_out, std::string& target_out) -{ - size_t scheme_end = url.find("://"); - if (scheme_end == std::string::npos) return false; - scheme_out = url.substr(0, scheme_end); - std::string rest = url.substr(scheme_end + 3); - size_t slash = rest.find('/'); - std::string authority = (slash != std::string::npos) ? rest.substr(0, slash) : rest; - target_out = (slash != std::string::npos) ? rest.substr(slash) : "/"; - size_t colon = authority.rfind(':'); - if (colon != std::string::npos) { - host_out = authority.substr(0, colon); - port_out = authority.substr(colon + 1); - } else { - host_out = authority; - port_out = (scheme_out == "https") ? "443" : "80"; - } - return true; -} - // ============================================================================ // 辅助:构建 headers JSON 字符串 / Helper: build headers JSON string // ============================================================================ @@ -219,25 +177,6 @@ static void parse_response(const dstalk_host_api_t* host, } } -// ============================================================================ -// 流式上下文:在 SSE 回调间累积内容和 tool_calls / Stream context: accumulate content and tool_calls across SSE callbacks -// ============================================================================ -struct ToolCallAccum { - int index = -1; - std::string id; - std::string name; - std::string arguments; // 增量拼接的 JSON arguments 字符串 / incrementally concatenated JSON arguments string -}; - -struct StreamContext { - const dstalk_host_api_t* host; - dstalk_stream_cb user_cb; - void* userdata; - std::string accumulated; - bool streaming_ok = true; - std::vector tool_calls; // W20.2: 按 index 累积 delta tool_calls / accumulate delta tool_calls by index -}; - // ============================================================================ // SSE 行解析(OpenAI 兼容格式) / SSE line parsing (OpenAI-compatible format) // ============================================================================ @@ -248,7 +187,7 @@ struct StreamContext { // to token_out. If it contains tool_calls delta, accumulates into ctx->tool_calls. // Returns true if a content token was produced, false otherwise (tool_calls or unknown). static bool parse_sse_line(const std::string& line, std::string& token_out, - StreamContext* ctx) + dstalk_ai::StreamContext* ctx) { if (line.rfind("data: ", 0) != 0) return false; @@ -263,6 +202,7 @@ static bool parse_sse_line(const std::string& line, std::string& token_out, } if (data == "[DONE]") { token_out.clear(); + if (ctx) ctx->sse_parse_errors = 0; // 成功解析,重置错误计数 / successful parse, reset error counter return true; // 流结束信号 / stream end signal } @@ -307,16 +247,31 @@ static bool parse_sse_line(const std::string& line, std::string& token_out, } } } + if (ctx) ctx->sse_parse_errors = 0; // 成功解析 / successful parse return false; // tool_calls 已处理,无内容 token 给用户回调 / tool_calls processed, no content token for user callback } if (delta.contains("content")) { token_out = json::value_to(delta["content"]); + if (ctx) ctx->sse_parse_errors = 0; // 成功解析 / successful parse return true; } } + // 有效 JSON 但不是已知格式 — 非错误,只是未知事件类型 / valid JSON but unknown format — not an error, just unknown event type + // 重置计数器:JSON 本身解析成功 / reset counter: JSON itself parsed successfully + if (ctx) ctx->sse_parse_errors = 0; } catch (...) { - // 忽略解析失败 / Ignore parse failures + if (ctx) { + ctx->sse_parse_errors++; + const dstalk_host_api_t* log_host = g_host.load(std::memory_order_acquire); + if (log_host) { + if (ctx->sse_parse_errors == 1 || ctx->sse_parse_errors % 5 == 0) { + log_host->log(DSTALK_LOG_WARN, + "[openai] SSE parse error (#%d consecutive)", + ctx->sse_parse_errors); + } + } + } } return false; } @@ -340,15 +295,7 @@ static int my_configure(const char* provider, const char* base_url, const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); if (host) { // W20.2: 从 tools service 缓存 tools_json,供 chat/chat_stream 复用 / Cache tools_json from tools service for reuse in chat/chat_stream - auto* tools_svc = reinterpret_cast( - host->query_service("tools", 1)); - if (tools_svc && tools_svc->get_tools_json) { - char* json = tools_svc->get_tools_json(); - if (json) { - g_tools_json = json; - host->free(json); - } - } + dstalk_ai::cache_tools_json(host, g_tools_json); host->log(DSTALK_LOG_INFO, "[openai] configured: model=%s base_url=%s max_tokens=%d temperature=%.2f", @@ -376,6 +323,8 @@ static dstalk_chat_result_t my_chat( const char* user_input, const char* tools_json) { + char* response_body = nullptr; + int status_code = 0; try { dstalk_chat_result_t r = {}; r.ok = 0; @@ -389,7 +338,7 @@ static dstalk_chat_result_t my_chat( } std::string scheme, host_name, port, target; - extract_host_port(g_cfg.base_url, scheme, host_name, port, target); + dstalk_ai::extract_host_port(g_cfg.base_url, scheme, host_name, port, target); std::string target_path = target + "/chat/completions"; std::string body = build_request_json(history, history_len, @@ -397,15 +346,13 @@ static dstalk_chat_result_t my_chat( std::string headers_json = build_headers_json(g_cfg.api_key); - char* response_body = nullptr; - int status_code = 0; - int ret = http->post_json( host_name.c_str(), port.c_str(), target_path.c_str(), body.c_str(), headers_json.c_str(), &response_body, &status_code); if (ret != 0) { r.error = host ? host->strdup("http request failed") : nullptr; + if (response_body) host->free(response_body); return r; } @@ -418,6 +365,7 @@ static dstalk_chat_result_t my_chat( } catch (const std::exception& e) { const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); if (host && host->log) host->log(DSTALK_LOG_ERROR, "[openai] my_chat exception: %s", e.what()); + if (response_body && host) host->free(response_body); dstalk_chat_result_t r = {}; r.ok = 0; r.error = host ? host->strdup(e.what()) : nullptr; @@ -425,6 +373,7 @@ static dstalk_chat_result_t my_chat( } catch (...) { const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); if (host && host->log) host->log(DSTALK_LOG_ERROR, "[openai] my_chat unknown exception"); + if (response_body && host) host->free(response_body); dstalk_chat_result_t r = {}; r.ok = 0; r.error = host ? host->strdup("unknown exception") : nullptr; @@ -440,7 +389,7 @@ static dstalk_chat_result_t my_chat( static int sse_line_callback(const char* line, void* userdata) { try { - auto* ctx = static_cast(userdata); + auto* ctx = static_cast(userdata); if (!line || !line[0]) return 1; // 空行,继续 / empty line, continue std::string line_str(line); @@ -448,6 +397,15 @@ static int sse_line_callback(const char* line, void* userdata) if (!parse_sse_line(line_str, token, ctx)) return 1; // 非 data/tool_calls 行,继续 / not a data/tool_calls line, continue + // W21.5: 连续 SSE 解析错误超过阈值,中止流 / consecutive SSE parse errors exceed threshold, abort stream + if (ctx && ctx->sse_parse_errors >= dstalk_ai::kMaxSseParseErrors) { + const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); + if (host) host->log(DSTALK_LOG_ERROR, + "[openai] SSE stream aborted: %d consecutive parse errors", + ctx->sse_parse_errors); + return 0; + } + if (token.empty()) return 0; // [DONE],停止 / [DONE], stop ctx->accumulated += token; @@ -475,6 +433,8 @@ static dstalk_chat_result_t my_chat_stream( const char* user_input, dstalk_stream_cb cb, void* userdata) { + char* response_body = nullptr; + int status_code = 0; try { dstalk_chat_result_t r = {}; r.ok = 0; @@ -488,7 +448,7 @@ static dstalk_chat_result_t my_chat_stream( } std::string scheme, host_name, port, target; - extract_host_port(g_cfg.base_url, scheme, host_name, port, target); + dstalk_ai::extract_host_port(g_cfg.base_url, scheme, host_name, port, target); std::string target_path = target + "/chat/completions"; std::string body = build_request_json(history, history_len, @@ -496,14 +456,11 @@ static dstalk_chat_result_t my_chat_stream( std::string headers_json = build_headers_json(g_cfg.api_key); - StreamContext ctx; + dstalk_ai::StreamContext ctx; ctx.host = host; ctx.user_cb = cb; ctx.userdata = userdata; - char* response_body = nullptr; - int status_code = 0; - int ret = http->post_stream( host_name.c_str(), port.c_str(), target_path.c_str(), body.c_str(), headers_json.c_str(), @@ -525,7 +482,9 @@ static dstalk_chat_result_t my_chat_stream( r.error = host ? host->strdup( json::value_to(err["message"]).c_str()) : nullptr; } - } catch (...) {} + } catch (...) { + if (host) host->log(DSTALK_LOG_WARN, "[openai] SSE error body parse error (ignored)"); + } } if (!r.error && host) { if (status_code <= 0) @@ -559,19 +518,7 @@ static dstalk_chat_result_t my_chat_stream( // 序列化累积的 tool_calls 为 JSON(兼容 OpenAI tool_calls 格式) / Serialize accumulated tool_calls to JSON (OpenAI-compatible tool_calls format) if (has_tool_calls) { - json::array tc_array; - for (auto& tc : ctx.tool_calls) { - json::object tc_obj; - tc_obj["index"] = tc.index; - if (!tc.id.empty()) tc_obj["id"] = tc.id; - tc_obj["type"] = "function"; - json::object func; - if (!tc.name.empty()) func["name"] = tc.name; - func["arguments"] = tc.arguments; - tc_obj["function"] = func; - tc_array.push_back(std::move(tc_obj)); - } - std::string tc_json = json::serialize(tc_array); + std::string tc_json = dstalk_ai::serialize_tool_calls(ctx.tool_calls); r.tool_calls_json = host ? host->strdup(tc_json.c_str()) : nullptr; } else { r.tool_calls_json = nullptr; @@ -581,6 +528,7 @@ static dstalk_chat_result_t my_chat_stream( } catch (const std::exception& e) { const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); if (host && host->log) host->log(DSTALK_LOG_ERROR, "[openai] my_chat_stream exception: %s", e.what()); + if (response_body && host) host->free(response_body); dstalk_chat_result_t r = {}; r.ok = 0; r.error = host ? host->strdup(e.what()) : nullptr; @@ -588,6 +536,7 @@ static dstalk_chat_result_t my_chat_stream( } catch (...) { const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); if (host && host->log) host->log(DSTALK_LOG_ERROR, "[openai] my_chat_stream unknown exception"); + if (response_body && host) host->free(response_body); dstalk_chat_result_t r = {}; r.ok = 0; r.error = host ? host->strdup("unknown exception") : nullptr; @@ -602,10 +551,7 @@ static dstalk_chat_result_t my_chat_stream( static void my_free_result(dstalk_chat_result_t* result) { const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); - if (!result || !host) return; - if (result->content) { host->free((void*)result->content); result->content = nullptr; } - if (result->error) { host->free((void*)result->error); result->error = nullptr; } - if (result->tool_calls_json) { host->free((void*)result->tool_calls_json); result->tool_calls_json = nullptr; } + dstalk_ai::free_chat_result(host, result); } // ============================================================================ @@ -638,7 +584,7 @@ static int on_init(const dstalk_host_api_t* host) if (host) host->log(DSTALK_LOG_INFO, "[openai] initializing OpenAI-compatible AI plugin"); - return host->register_service("ai.openai", 1, &g_service); + return host->register_service("ai_openai", 1, &g_service); } catch (const std::exception& e) { const dstalk_host_api_t* h = g_host.load(std::memory_order_acquire); if (h && h->log) h->log(DSTALK_LOG_ERROR, "[openai] on_init exception: %s", e.what()); @@ -656,7 +602,7 @@ static void on_shutdown() try { const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire); if (host) host->log(DSTALK_LOG_INFO, "[openai] shutdown"); - secure_zero(g_cfg.api_key.data(), g_cfg.api_key.size()); + dstalk_ai::secure_zero(g_cfg.api_key.data(), g_cfg.api_key.size()); g_cfg.api_key.clear(); g_http.store(nullptr, std::memory_order_release); g_config.store(nullptr, std::memory_order_release); @@ -674,7 +620,7 @@ static void on_shutdown() // 插件描述符 / Plugin descriptor // ============================================================================ static dstalk_plugin_info_t g_info = { - /* .name = */ "openai-compat", + /* .name = */ "openai_compat", /* .version = */ "1.0.0", /* .description = */ "OpenAI-compatible AI provider (OpenAI-compatible API) / OpenAI-compatible AI 提供者 (OpenAI 兼容 API)", /* .api_version = */ DSTALK_API_VERSION, diff --git a/scripts/ci-build.bat b/scripts/ci-build.bat index b61daf4..84fd431 100644 --- a/scripts/ci-build.bat +++ b/scripts/ci-build.bat @@ -7,17 +7,80 @@ set BUILD_DIR=%PROJECT_DIR%\build echo === dstalk CI Build === echo Project: %PROJECT_DIR% -if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%" -cd /d "%BUILD_DIR%" - -echo --- CMake Configure --- -cmake "%PROJECT_DIR%" -G Ninja -DCMAKE_BUILD_TYPE=Release -DDSTALK_BUILD_TESTS=ON -DDSTALK_BUILD_GUI=OFF +:: --- Find vcvarsall.bat for MSVC environment --- +set VCVARSALL= +for %%d in ( + "C:\Program Files\Microsoft Visual Studio\2022\Enterprise" + "C:\Program Files\Microsoft Visual Studio\2022\Professional" + "C:\Program Files\Microsoft Visual Studio\2022\Community" + "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise" + "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional" + "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community" +) do ( + if exist "%%~d\VC\Auxiliary\Build\vcvarsall.bat" ( + set "VCVARSALL=%%~d\VC\Auxiliary\Build\vcvarsall.bat" + goto :found_vcvars + ) +) +:found_vcvars +if "%VCVARSALL%"=="" ( + echo ERROR: vcvarsall.bat not found - install Visual Studio or set VCVARSALL + exit /b 1 +) +echo Using vcvarsall: %VCVARSALL% +call "%VCVARSALL%" x64 if errorlevel 1 exit /b 1 +:: --- Find conan: prefer tools\ local install, fallback to PATH --- +set CONAN= +if exist "%PROJECT_DIR%\tools\.venv\Scripts\conan.exe" ( + set "CONAN=%PROJECT_DIR%\tools\.venv\Scripts\conan.exe" +) else ( + where conan >nul 2>&1 + if not errorlevel 1 set "CONAN=conan" +) +if "%CONAN%"=="" ( + echo ERROR: conan not found in tools\ or PATH + exit /b 1 +) +echo Using conan: %CONAN% + +:: --- Detect conan profile --- +echo --- Conan Profile Detect --- +"%CONAN%" profile detect --force +if errorlevel 1 exit /b 1 + +:: --- Install dependencies --- +echo --- Conan Install --- +if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%" +cd /d "%BUILD_DIR%" +"%CONAN%" install "%PROJECT_DIR%\deps\" --build=missing -s build_type=Release +if errorlevel 1 exit /b 1 + +:: --- Find conan toolchain --- +set TOOLCHAIN= +if exist "%BUILD_DIR%\build\Release\generators\conan_toolchain.cmake" ( + set "TOOLCHAIN=%BUILD_DIR%\build\Release\generators\conan_toolchain.cmake" +) else if exist "%BUILD_DIR%\Release\generators\conan_toolchain.cmake" ( + set "TOOLCHAIN=%BUILD_DIR%\Release\generators\conan_toolchain.cmake" +) +if "%TOOLCHAIN%"=="" ( + echo ERROR: conan_toolchain.cmake not found + exit /b 1 +) +echo Toolchain: %TOOLCHAIN% + +:: --- CMake configure --- +echo --- CMake Configure --- +cmake "%PROJECT_DIR%" -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE="%TOOLCHAIN%" -DDSTALK_BUILD_TESTS=ON -DDSTALK_BUILD_GUI=OFF +if errorlevel 1 exit /b 1 + +:: --- Build --- echo --- Build --- cmake --build . --parallel if errorlevel 1 exit /b 1 +:: --- Test --- echo --- Test --- ctest --output-on-failure --parallel 4 if errorlevel 1 exit /b 1 diff --git a/scripts/ci-build.sh b/scripts/ci-build.sh index 7ebf803..e83df45 100644 --- a/scripts/ci-build.sh +++ b/scripts/ci-build.sh @@ -7,22 +7,65 @@ BUILD_DIR="${PROJECT_DIR}/build" echo "=== dstalk CI Build ===" echo "Project: ${PROJECT_DIR}" -# 创建构建目录 +# --- Find conan: prefer tools/ local install, fallback to PATH --- +CONAN="" +for candidate in \ + "${PROJECT_DIR}/tools/.venv/bin/conan" \ + "${PROJECT_DIR}/tools/.venv/Scripts/conan" \ + "${PROJECT_DIR}/tools/.venv/Scripts/conan.exe"; do + if [ -x "${candidate}" ]; then + CONAN="${candidate}" + break + fi +done +if [ -z "${CONAN}" ] && command -v conan &>/dev/null; then + CONAN="conan" +fi +if [ -z "${CONAN}" ]; then + echo "ERROR: conan not found in tools/ or PATH" >&2 + exit 1 +fi +echo "Using conan: ${CONAN}" + +# --- Detect conan profile --- +echo "--- Conan Profile Detect ---" +"${CONAN}" profile detect --force + +# --- Install dependencies --- +echo "--- Conan Install ---" mkdir -p "${BUILD_DIR}" cd "${BUILD_DIR}" +"${CONAN}" install "${PROJECT_DIR}/deps/" --build=missing -s build_type=Release -# CMake 配置 +# --- Find conan toolchain --- +TOOLCHAIN="" +for candidate in \ + "Release/generators/conan_toolchain.cmake" \ + "build/Release/generators/conan_toolchain.cmake"; do + if [ -f "${candidate}" ]; then + TOOLCHAIN="$(pwd)/${candidate}" + break + fi +done +if [ -z "${TOOLCHAIN}" ]; then + echo "ERROR: conan_toolchain.cmake not found" >&2 + exit 1 +fi +echo "Toolchain: ${TOOLCHAIN}" + +# --- CMake configure --- echo "--- CMake Configure ---" cmake "${PROJECT_DIR}" -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN}" \ -DDSTALK_BUILD_TESTS=ON \ -DDSTALK_BUILD_GUI=OFF -# 编译 +# --- Build --- echo "--- Build ---" cmake --build . --parallel -# 运行测试 +# --- Test --- echo "--- Test ---" ctest --output-on-failure --parallel 4 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d2720f2..0dfc0d5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,210 +2,215 @@ # tests — 单元测试 # ============================================================ -add_executable(dstalk-smoke-test +add_executable(dstalk_smoke_test smoke_test.cpp ) -target_link_libraries(dstalk-smoke-test +target_link_libraries(dstalk_smoke_test PRIVATE dstalk ) -add_test(NAME dstalk-smoke-test COMMAND dstalk-smoke-test) +add_test(NAME dstalk_smoke_test COMMAND dstalk_smoke_test) # ============================================================ -# dstalk-host-api-test — host API 单元测试 +# dstalk_host_api_test — host API 单元测试 # ============================================================ -add_executable(dstalk-host-api-test +add_executable(dstalk_host_api_test host_api_test.cpp ${CMAKE_SOURCE_DIR}/dstalk_core/src/service_registry.cpp ) -target_include_directories(dstalk-host-api-test +target_include_directories(dstalk_host_api_test PRIVATE ${CMAKE_SOURCE_DIR}/dstalk_core/src ) -target_compile_features(dstalk-host-api-test +target_compile_features(dstalk_host_api_test PRIVATE cxx_std_17 ) -target_link_libraries(dstalk-host-api-test +target_link_libraries(dstalk_host_api_test PRIVATE dstalk ) -add_test(NAME dstalk-host-api-test COMMAND dstalk-host-api-test) +add_test(NAME dstalk_host_api_test COMMAND dstalk_host_api_test) # ============================================================ -# dstalk-event-bus-test — EventBus 单元测试 +# dstalk_event_bus_test — EventBus 单元测试 # ============================================================ -add_executable(dstalk-event-bus-test +add_executable(dstalk_event_bus_test event_bus_test.cpp ${CMAKE_SOURCE_DIR}/dstalk_core/src/event_bus.cpp ) -target_include_directories(dstalk-event-bus-test +target_include_directories(dstalk_event_bus_test PRIVATE ${CMAKE_SOURCE_DIR}/dstalk_core/src ) -target_compile_features(dstalk-event-bus-test +target_compile_features(dstalk_event_bus_test PRIVATE cxx_std_17 ) -add_test(NAME dstalk-event-bus-test COMMAND dstalk-event-bus-test) +add_test(NAME dstalk_event_bus_test COMMAND dstalk_event_bus_test) # ============================================================ -# dstalk-service-registry-test — ServiceRegistry 补充单元测试 +# dstalk_service_registry_test — ServiceRegistry 补充单元测试 # ============================================================ -add_executable(dstalk-service-registry-test +add_executable(dstalk_service_registry_test service_registry_test.cpp ${CMAKE_SOURCE_DIR}/dstalk_core/src/service_registry.cpp ) -target_include_directories(dstalk-service-registry-test +target_include_directories(dstalk_service_registry_test PRIVATE ${CMAKE_SOURCE_DIR}/dstalk_core/src ) -target_compile_features(dstalk-service-registry-test +target_compile_features(dstalk_service_registry_test PRIVATE cxx_std_17 ) -add_test(NAME dstalk-service-registry-test COMMAND dstalk-service-registry-test) +add_test(NAME dstalk_service_registry_test COMMAND dstalk_service_registry_test) # ============================================================ -# dstalk-context-plugin-test — Context 插件单元测试 +# dstalk_context_plugin_test — Context 插件单元测试 # W18.1 (qa-wang + architect-lin): 覆盖 token 计数/trim/UTF-8 边界 # ============================================================ -add_executable(dstalk-context-plugin-test +add_executable(dstalk_context_plugin_test context_plugin_test.cpp ) -target_link_libraries(dstalk-context-plugin-test +target_link_libraries(dstalk_context_plugin_test PRIVATE dstalk ) -add_test(NAME dstalk-context-plugin-test COMMAND dstalk-context-plugin-test) +add_test(NAME dstalk_context_plugin_test COMMAND dstalk_context_plugin_test) # ============================================================ -# dstalk-plugin-loader-test — PluginLoader 安全回归测试 +# dstalk_plugin_loader_test — PluginLoader 安全回归测试 # W20.3 (qa-xu): 覆盖 W19 F-18.3-1~5 修复验证 # ============================================================ -add_executable(dstalk-plugin-loader-test +add_executable(dstalk_plugin_loader_test plugin_loader_test.cpp ${CMAKE_SOURCE_DIR}/dstalk_core/src/plugin_loader.cpp + ${CMAKE_SOURCE_DIR}/dstalk_core/src/service_registry.cpp ${CMAKE_SOURCE_DIR}/dstalk_core/src/boost_json.cpp ) -target_include_directories(dstalk-plugin-loader-test +target_include_directories(dstalk_plugin_loader_test PRIVATE ${CMAKE_SOURCE_DIR}/dstalk_core/src ) -target_compile_features(dstalk-plugin-loader-test +target_compile_features(dstalk_plugin_loader_test PRIVATE cxx_std_17 ) find_package(Boost REQUIRED CONFIG) -target_compile_definitions(dstalk-plugin-loader-test +target_compile_definitions(dstalk_plugin_loader_test PRIVATE BOOST_JSON_HEADER_ONLY BOOST_ALL_NO_LIB DSTALK_TEST_PLUGINS_DIR="${CMAKE_BINARY_DIR}/plugins" ) -target_link_libraries(dstalk-plugin-loader-test +target_link_libraries(dstalk_plugin_loader_test PRIVATE dstalk boost::boost ) -add_test(NAME dstalk-plugin-loader-test COMMAND dstalk-plugin-loader-test) +add_test(NAME dstalk_plugin_loader_test COMMAND dstalk_plugin_loader_test) # ============================================================ -# dstalk-anthropic-plugin-test — Anthropic AI 插件单元测试 +# dstalk_anthropic_plugin_test — Anthropic AI 插件单元测试 # W21.6 (qa-wang): 通过 #include source 访问 static 函数 # ============================================================ -add_executable(dstalk-anthropic-plugin-test +add_executable(dstalk_anthropic_plugin_test anthropic_plugin_test.cpp ) -target_include_directories(dstalk-anthropic-plugin-test +target_include_directories(dstalk_anthropic_plugin_test PRIVATE ${CMAKE_SOURCE_DIR}/dstalk_core/include + PRIVATE ${CMAKE_SOURCE_DIR}/plugins_upper/ai_common/include ) -target_compile_definitions(dstalk-anthropic-plugin-test +target_compile_definitions(dstalk_anthropic_plugin_test PRIVATE BOOST_JSON_HEADER_ONLY BOOST_ALL_NO_LIB ) -target_link_libraries(dstalk-anthropic-plugin-test +target_link_libraries(dstalk_anthropic_plugin_test PRIVATE dstalk + ai_common boost::boost ) -add_test(NAME dstalk-anthropic-plugin-test COMMAND dstalk-anthropic-plugin-test) +add_test(NAME dstalk_anthropic_plugin_test COMMAND dstalk_anthropic_plugin_test) # ============================================================ -# dstalk-openai-plugin-test — OpenAI 兼容 AI 插件单元测试 +# dstalk_openai_plugin_test — OpenAI 兼容 AI 插件单元测试 # W21.6 (qa-wang): 通过 #include source 访问 static 函数 # ============================================================ -add_executable(dstalk-openai-plugin-test +add_executable(dstalk_openai_plugin_test openai_plugin_test.cpp ) -target_include_directories(dstalk-openai-plugin-test +target_include_directories(dstalk_openai_plugin_test PRIVATE ${CMAKE_SOURCE_DIR}/dstalk_core/include + PRIVATE ${CMAKE_SOURCE_DIR}/plugins_upper/ai_common/include ) -target_compile_definitions(dstalk-openai-plugin-test +target_compile_definitions(dstalk_openai_plugin_test PRIVATE BOOST_JSON_HEADER_ONLY BOOST_ALL_NO_LIB ) -target_link_libraries(dstalk-openai-plugin-test +target_link_libraries(dstalk_openai_plugin_test PRIVATE dstalk + ai_common boost::boost ) -add_test(NAME dstalk-openai-plugin-test COMMAND dstalk-openai-plugin-test) +add_test(NAME dstalk_openai_plugin_test COMMAND dstalk_openai_plugin_test) # ============================================================ -# dstalk-network-plugin-test — Network 插件单元测试 +# dstalk_network_plugin_test — Network 插件单元测试 # W22.2 (qa-xu): 通过 #include source 访问 static 函数 # ============================================================ find_package(OpenSSL REQUIRED CONFIG) -add_executable(dstalk-network-plugin-test +add_executable(dstalk_network_plugin_test network_plugin_test.cpp ) -target_include_directories(dstalk-network-plugin-test +target_include_directories(dstalk_network_plugin_test PRIVATE ${CMAKE_SOURCE_DIR}/dstalk_core/include ) -target_compile_definitions(dstalk-network-plugin-test +target_compile_definitions(dstalk_network_plugin_test PRIVATE BOOST_ALL_NO_LIB ) -target_link_libraries(dstalk-network-plugin-test +target_link_libraries(dstalk_network_plugin_test PRIVATE dstalk boost::boost openssl::openssl ) -add_test(NAME dstalk-network-plugin-test COMMAND dstalk-network-plugin-test) +add_test(NAME dstalk_network_plugin_test COMMAND dstalk_network_plugin_test) # ============================================================ # coverage — gcovr 覆盖率报告 (HTML + 终端摘要) diff --git a/tests/anthropic_plugin_test.cpp b/tests/anthropic_plugin_test.cpp index 036b421..a5fdbc8 100644 --- a/tests/anthropic_plugin_test.cpp +++ b/tests/anthropic_plugin_test.cpp @@ -10,6 +10,7 @@ #define BOOST_JSON_HEADER_ONLY #define BOOST_ALL_NO_LIB #include "../plugins_upper/anthropic/src/anthropic_plugin.cpp" +using namespace dstalk_ai; #include #include diff --git a/tests/openai_plugin_test.cpp b/tests/openai_plugin_test.cpp index b25c046..74b544f 100644 --- a/tests/openai_plugin_test.cpp +++ b/tests/openai_plugin_test.cpp @@ -10,6 +10,7 @@ #define BOOST_JSON_HEADER_ONLY #define BOOST_ALL_NO_LIB #include "../plugins_upper/openai/src/openai_plugin.cpp" +using namespace dstalk_ai; #include #include diff --git a/tests/plugin_loader_test.cpp b/tests/plugin_loader_test.cpp index 79b1cdd..bd68a8a 100644 --- a/tests/plugin_loader_test.cpp +++ b/tests/plugin_loader_test.cpp @@ -55,6 +55,7 @@ static void mock_log(int level, const char* fmt, ...) { // Stub host_api 函数:除 log 外所有操作均返回失败/默认值 static int stub_reg(const char*, int, void*) { return -1; } static void* stub_query(const char*, int) { return nullptr; } +static void stub_unreg(const char*) {} static int stub_sub(int, dstalk_event_handler_fn, void*) { return -1; } static int stub_emit(int, const void*) { return -1; } static void stub_unsub(int) {} @@ -67,7 +68,7 @@ static char* stub_strdup(const char*) { return nullptr; } // Mock host_api vtable: all stubs except mock_log for capturing error-path diagnostics // Mock host_api 虚表:除 mock_log 外全部 stub,用于捕获错误路径诊断 static dstalk_host_api_t g_mock_host_api = { - stub_reg, stub_query, + stub_reg, stub_query, stub_unreg, stub_sub, stub_emit, stub_unsub, stub_cget, stub_cset, mock_log, @@ -148,8 +149,8 @@ int main() dstalk::PluginLoader loader; fs::path plugins_dir = get_plugins_dir(); - fs::path dll_config = plugins_dir / "plugin-config.dll"; - fs::path dll_fileio = plugins_dir / "plugin-file_io.dll"; + fs::path dll_config = plugins_dir / "plugin_config.dll"; + fs::path dll_fileio = plugins_dir / "plugin_file_io.dll"; bool have_plugins = fs::exists(dll_config) && fs::exists(dll_fileio); @@ -206,8 +207,8 @@ int main() fs::path plugins_dir = get_plugins_dir(); std::vector dlls; - for (auto name : {"plugin-config.dll", "plugin-file_io.dll", - "plugin-context.dll", "plugin-session.dll"}) { + for (auto name : {"plugin_config.dll", "plugin_file_io.dll", + "plugin_context.dll", "plugin_session.dll"}) { fs::path p = plugins_dir / name; if (fs::exists(p)) dlls.push_back(p); } diff --git a/tests/smoke_test.cpp b/tests/smoke_test.cpp index 96ea57e..dd7b129 100644 --- a/tests/smoke_test.cpp +++ b/tests/smoke_test.cpp @@ -186,7 +186,7 @@ int main() // 测试服务查询: ai(可能因为没有真实 API key 而失败,但服务应存在) // Test service query: ai (may fail without real API key, but service should exist) const char* ai_provider = dstalk_config_get("ai.provider"); - if (!ai_provider) ai_provider = "ai.openai"; + if (!ai_provider) ai_provider = "ai_openai"; auto* ai = static_cast( dstalk_service_query(ai_provider, 1)); if (ai) { @@ -598,12 +598,12 @@ int main() std::cout << "[OK] R3: http error path, no response body (connection refused)\n"; } } else { - // 回退:测 AI 服务 (ai.openai) 错误路径 - // Fallback: test AI service (ai.openai) error path + // 回退:测 AI 服务 (ai_openai) 错误路径 + // Fallback: test AI service (ai_openai) error path auto* ai_svc = static_cast( - dstalk_service_query("ai.openai", 1)); + dstalk_service_query("ai_openai", 1)); if (ai_svc) { - std::cout << "[OK] R3: ai.openai service found (http fallback)\n"; + std::cout << "[OK] R3: ai_openai service found (http fallback)\n"; dstalk_message_t msg = {"user", "hi", nullptr, nullptr}; dstalk_chat_result_t r = ai_svc->chat(&msg, 1, "", nullptr); // api_key="test-key" 为无效 key,应返回 error result 而非崩溃 diff --git a/tools/env.bat b/tools/env.bat index b8827cc..2d6658a 100644 --- a/tools/env.bat +++ b/tools/env.bat @@ -8,8 +8,8 @@ set "TOOLS=%~dp0" if exist "%TOOLS%cmake\bin" set "PATH=%TOOLS%cmake\bin;%PATH%" if exist "%TOOLS%ninja" set "PATH=%TOOLS%ninja;%PATH%" if exist "%TOOLS%llvm\bin" set "PATH=%TOOLS%llvm\bin;%PATH%" -if exist "%TOOLS%llvm\bin\clang.exe" set "CC=%TOOLS%llvm\bin\clang.exe" -if exist "%TOOLS%llvm\bin\clang++.exe" set "CXX=%TOOLS%llvm\bin\clang++.exe" +if exist "%TOOLS%llvm\bin\clang-cl.exe" set "CC=%TOOLS%llvm\bin\clang-cl.exe" +if exist "%TOOLS%llvm\bin\clang-cl.exe" set "CXX=%TOOLS%llvm\bin\clang-cl.exe" if exist "%TOOLS%.venv\Scripts" set "PATH=%TOOLS%.venv\Scripts;%PATH%" echo [dstalk] tools\ 工具链已加载 diff --git a/tools/setup.sh b/tools/setup.sh new file mode 100644 index 0000000..0d09700 --- /dev/null +++ b/tools/setup.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env bash +set -euo pipefail + +# ============================================================ +# setup.sh — dstalk toolchain portable installer (Linux / macOS) +# Installs CMake, Ninja, and Conan2 into tools/ without +# modifying system PATH. Mirrors tools/setup.bat for Windows. +# ============================================================ + +TOOLS="$(cd "$(dirname "$0")" && pwd)" +OS=$(uname -s) +ARCH=$(uname -m) + +echo "============================================" +echo " dstalk Toolchain — Portable Install" +echo " Target: ${TOOLS}" +echo " OS: ${OS} / ${ARCH}" +echo "============================================" +echo "" + +# ============================================================ +# 0. Check for a C/C++ compiler +# ============================================================ +echo "[0/4] Checking for C/C++ compiler..." +if command -v clang &>/dev/null; then + echo " [OK] clang found: $(command -v clang)" +elif command -v gcc &>/dev/null; then + echo " [OK] gcc found: $(command -v gcc)" +else + echo " [WARN] No C compiler found in PATH." + if [ "$OS" = "Darwin" ]; then + echo " Install Xcode Command Line Tools: xcode-select --install" + else + echo " Install build-essential: sudo apt install build-essential" + echo " Or clang: sudo apt install clang" + fi + echo " You can continue setup, but build.sh will fail without a compiler." +fi + +# ============================================================ +# 1. CMake +# ============================================================ +echo "[1/4] CMake..." +if [ -x "${TOOLS}/cmake/bin/cmake" ]; then + echo " [OK] Already installed" +else + mkdir -p "${TOOLS}/cmake" + CMAKE_VER="3.31.6" + if [ "$OS" = "Darwin" ]; then + if [ "$ARCH" = "arm64" ]; then + CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v${CMAKE_VER}/cmake-${CMAKE_VER}-macos-universal.tar.gz" + else + CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v${CMAKE_VER}/cmake-${CMAKE_VER}-macos-universal.tar.gz" + fi + else + CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v${CMAKE_VER}/cmake-${CMAKE_VER}-linux-${ARCH}.tar.gz" + fi + echo " Downloading CMake ${CMAKE_VER}..." + curl -L -o "${TOOLS}/cmake/cmake.tar.gz" "${CMAKE_URL}" 2>/dev/null || { + echo " [FAIL] Download failed. Install CMake manually:" + echo " macOS: brew install cmake" + echo " Linux: sudo apt install cmake" + } + if [ -f "${TOOLS}/cmake/cmake.tar.gz" ]; then + tar -xzf "${TOOLS}/cmake/cmake.tar.gz" -C "${TOOLS}/cmake" --strip-components=1 + rm -f "${TOOLS}/cmake/cmake.tar.gz" + # macOS bundle: CMake.app/Contents/bin/cmake → cmake/bin/cmake + if [ -d "${TOOLS}/cmake/CMake.app" ]; then + cp -R "${TOOLS}/cmake/CMake.app/Contents/"* "${TOOLS}/cmake/" + rm -rf "${TOOLS}/cmake/CMake.app" + fi + echo " [OK] CMake" + fi +fi + +# ============================================================ +# 2. Ninja +# ============================================================ +echo "[2/4] Ninja..." +if [ -x "${TOOLS}/ninja/ninja" ]; then + echo " [OK] Already installed" +else + mkdir -p "${TOOLS}/ninja" + NINJA_VER="1.12.1" + if [ "$OS" = "Darwin" ]; then + NINJA_URL="https://github.com/ninja-build/ninja/releases/download/v${NINJA_VER}/ninja-mac.zip" + else + NINJA_URL="https://github.com/ninja-build/ninja/releases/download/v${NINJA_VER}/ninja-linux.zip" + fi + echo " Downloading Ninja ${NINJA_VER}..." + curl -L -o "${TOOLS}/ninja/ninja.zip" "${NINJA_URL}" 2>/dev/null || { + echo " [FAIL] Download failed. Install Ninja manually:" + echo " macOS: brew install ninja" + echo " Linux: sudo apt install ninja-build" + } + if [ -f "${TOOLS}/ninja/ninja.zip" ]; then + (cd "${TOOLS}/ninja" && unzip -o ninja.zip && rm -f ninja.zip) + chmod +x "${TOOLS}/ninja/ninja" + echo " [OK] Ninja" + fi +fi + +# ============================================================ +# 3. Python venv + Conan2 +# ============================================================ +echo "[3/4] Python venv + Conan2..." +PYTHON="" +for py in python3 python; do + if command -v "$py" &>/dev/null; then + PYTHON="$py" + break + fi +done +if [ -z "$PYTHON" ]; then + echo " [FAIL] Python 3.10+ not found." + echo " macOS: brew install python" + echo " Linux: sudo apt install python3 python3-venv" + exit 1 +fi + +if [ ! -f "${TOOLS}/.venv/bin/python" ]; then + "$PYTHON" -m venv "${TOOLS}/.venv" +fi +"${TOOLS}/.venv/bin/python" -m pip install --upgrade pip -q +"${TOOLS}/.venv/bin/python" -m pip install 'conan>=2,<3' -q +echo " [OK] Conan2" + +# ============================================================ +# 4. Configure Conan profile +# ============================================================ +echo "[4/4] Configuring Conan profile..." +"${TOOLS}/.venv/bin/conan" profile detect --force 2>/dev/null || { + echo " [WARN] Conan profile auto-detect failed. You may need to configure manually." +} + +echo "" +echo "============================================" +echo " Done! All tools are ready." +echo "============================================" +echo "" +echo " Next step: run ./build.sh from the project root." diff --git a/模块目录和功能说明.md b/模块目录和功能说明.md index 614436e..4a9c259 100644 --- a/模块目录和功能说明.md +++ b/模块目录和功能说明.md @@ -45,8 +45,8 @@ | 目录 | 服务名 | 功能说明 | 依赖 | |------|--------|---------|------| | `plugins_upper/context/` | `"context"` | Token 计数(UTF-8 字节估算)、上下文窗口裁剪 | `session` | -| `plugins_upper/openai/` | `"ai.openai"` | OpenAI 兼容格式 AI 接入(chat/stream/tools、SSE 解析) | `http`、`config` | -| `plugins_upper/anthropic/` | `"ai.anthropic"` | Anthropic Claude Messages API 接入(chat/stream、SSE 解析) | `http`、`config` | +| `plugins_upper/openai/` | `"ai_openai"` | OpenAI 兼容格式 AI 接入(chat/stream/tools、SSE 解析) | `http`、`config` | +| `plugins_upper/anthropic/` | `"ai_anthropic"` | Anthropic Claude Messages API 接入(chat/stream、SSE 解析) | `http`、`config` | ### 3.4 规划中的插件(尚未实现) diff --git a/编码规范和命名规范.md b/编码规范和命名规范.md index fecb83a..f2d01c5 100644 --- a/编码规范和命名规范.md +++ b/编码规范和命名规范.md @@ -101,8 +101,8 @@ int ConfigStore::load_file(const char* path) |------|------|------| | 插件目录 | 功能名,小写 + 下划线 | `plugins_upper/openai/`、`plugins_base/file_io/` | | 插件源文件 | `<功能名>_plugin.cpp` | `openai_plugin.cpp`、`session_plugin.cpp` | -| CMake 目标 | `plugin-<功能名>` | `plugin-openai`、`plugin-network` | -| 服务注册名 | 小写 + 点号分级 | `"ai.openai"`、`"http"`、`"session"` | +| CMake 目标 | `plugin_<功能名>` | `plugin_openai`、`plugin_network` | +| 服务注册名 | 小写 + 下划线 | `"ai_openai"`、`"http"`、`"session"` | | 插件入口函数 | 固定为 `dstalk_plugin_init` | — | ### 3.5 禁止项