W17: extract ai_common shared module + fix anthropic data race + brace bugs

- New plugins_upper/ai_common/ static library: shared PluginConfig, ToolCallAccum,
  StreamContext, secure_zero, extract_host_port, serialize_tool_calls, free_chat_result
- Refactored openai/anthropic plugins to use dstalk_ai:: namespace from ai_common
- Fixed anthropic g_config raw pointer → std::atomic (data race)
- Added SSE parse error counter with threshold abort (kMaxSseParseErrors=5)
- Fixed missing closing brace in both plugins' error-body catch block
- Updated test targets: ai_common include path + link, using namespace dstalk_ai
- plugin_loader_test: added stub_unreg + service_registry.cpp for unregister_service
- Includes pre-existing uncommitted changes from prior waves

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-31 16:58:25 +08:00
parent ba7382db2a
commit 8faa02c3d5
49 changed files with 1062 additions and 413 deletions

View File

@@ -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"

View File

@@ -7,6 +7,7 @@
#include "dstalk/dstalk_host.h"
#include "dstalk/dstalk_services.h"
#include "ai_common.hpp"
#include <boost/json.hpp>
#include <boost/json/src.hpp>
@@ -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<const dstalk_host_api_t*> g_host{nullptr};
static std::atomic<dstalk_http_service_t*> g_http{nullptr};
static dstalk_config_service_t* g_config = nullptr;
static std::atomic<dstalk_config_service_t*> 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<std::string>(err["message"]).c_str());
r.error = host ? host->strdup(
json::value_to<std::string>(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<ToolCallAccum> 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<std::string>(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<std::string>(*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<std::string>(*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<const dstalk_tools_service_t*>(
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<StreamContext*>(userdata);
auto* ctx = static_cast<dstalk_ai::StreamContext*>(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<std::string>(err["message"]).c_str());
r.error = host ? host->strdup(
json::value_to<std::string>(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_callstool-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,