feat: add OpenAI-compatible AI provider plugin with SSE streaming support
- Implemented the OpenAI-compatible AI provider plugin, including configuration, chat, and chat_stream functionalities. - Added support for SSE streaming and tool calls. - Integrated Boost.JSON for JSON handling. - Created CMake configuration for the plugin. - Added error handling and logging throughout the plugin.
This commit is contained in:
22
plugins_upper/anthropic/CMakeLists.txt
Normal file
22
plugins_upper/anthropic/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
cmake_minimum_required(VERSION 3.21)
|
||||
|
||||
# ============================================================
|
||||
# plugin-anthropic — Anthropic Claude AI 服务
|
||||
# 依赖: http 服务 (查询), config 服务 (查询)
|
||||
# ============================================================
|
||||
|
||||
add_library(plugin-anthropic SHARED
|
||||
src/anthropic_plugin.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(plugin-anthropic PRIVATE dstalk)
|
||||
|
||||
# Boost.JSON 用于构建/解析请求和响应
|
||||
find_package(Boost REQUIRED CONFIG)
|
||||
target_link_libraries(plugin-anthropic PRIVATE boost::boost dstalk_boost_config)
|
||||
|
||||
set_target_properties(plugin-anthropic PROPERTIES
|
||||
PREFIX ""
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
|
||||
)
|
||||
736
plugins_upper/anthropic/src/anthropic_plugin.cpp
Normal file
736
plugins_upper/anthropic/src/anthropic_plugin.cpp
Normal file
@@ -0,0 +1,736 @@
|
||||
/*
|
||||
* @file anthropic_plugin.cpp
|
||||
* @brief Anthropic Claude Messages API provider plugin with streaming support.
|
||||
* Anthropic Claude Messages API 提供者插件,支持流式输出。
|
||||
* Copyright (c) 2026 dstalk contributors. GPLv3.
|
||||
*/
|
||||
|
||||
#include "dstalk/dstalk_host.h"
|
||||
#include "dstalk/dstalk_services.h"
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <boost/json/src.hpp>
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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
|
||||
// ============================================================================
|
||||
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;
|
||||
|
||||
// ============================================================================
|
||||
// 配置数据 / 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 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
|
||||
// ============================================================================
|
||||
// 构建包含 x-api-key 和 anthropic-version 的 JSON headers 对象 / Build the JSON headers object containing x-api-key and anthropic-version.
|
||||
static std::string build_headers_json()
|
||||
{
|
||||
json::object h;
|
||||
h["x-api-key"] = g_cfg.api_key;
|
||||
h["anthropic-version"] = "2023-06-01";
|
||||
return json::serialize(h);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 构建 Anthropic JSON 请求体 / Build Anthropic JSON request body
|
||||
// ============================================================================
|
||||
// 构建 Anthropic Messages API 的完整 JSON 请求体。
|
||||
// 按 Anthropic 规范将 system 消息提取为顶层 system 字段 / Build the full JSON request body for the Anthropic Messages API.
|
||||
// Extracts system messages as a top-level "system" field per Anthropic spec.
|
||||
static std::string build_request_json(
|
||||
const dstalk_message_t* history, int history_len,
|
||||
const std::string& user_input,
|
||||
const std::string& tools_json,
|
||||
bool stream)
|
||||
{
|
||||
json::object root;
|
||||
root["model"] = g_cfg.model;
|
||||
root["max_tokens"] = g_cfg.max_tokens;
|
||||
root["stream"] = stream;
|
||||
|
||||
// 提取 system 消息作为顶层字段 / Extract system messages as top-level field
|
||||
std::string system_prompt;
|
||||
json::array msgs;
|
||||
|
||||
for (int i = 0; i < history_len; ++i) {
|
||||
const auto& m = history[i];
|
||||
if (m.role && std::strcmp(m.role, "system") == 0) {
|
||||
if (!system_prompt.empty()) system_prompt += "\n\n";
|
||||
system_prompt += m.content ? m.content : "";
|
||||
continue;
|
||||
}
|
||||
json::object obj;
|
||||
obj["role"] = m.role ? m.role : "";
|
||||
obj["content"] = m.content ? m.content : "";
|
||||
msgs.push_back(obj);
|
||||
}
|
||||
|
||||
// 追加当前用户输入 / Append current user input
|
||||
{
|
||||
json::object obj;
|
||||
obj["role"] = "user";
|
||||
obj["content"] = user_input;
|
||||
msgs.push_back(obj);
|
||||
}
|
||||
|
||||
root["messages"] = msgs;
|
||||
|
||||
if (!system_prompt.empty()) {
|
||||
root["system"] = system_prompt;
|
||||
}
|
||||
|
||||
if (g_cfg.temperature >= 0.0 && g_cfg.temperature <= 1.0) {
|
||||
root["temperature"] = g_cfg.temperature;
|
||||
}
|
||||
|
||||
// W21.2: tools 定义传递给 API / Pass tools definition to API
|
||||
if (!tools_json.empty()) {
|
||||
root["tools"] = json::parse(tools_json);
|
||||
}
|
||||
|
||||
return json::serialize(root);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 解析非流式响应 / Parse non-streaming response
|
||||
// ============================================================================
|
||||
// 将非流式 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,
|
||||
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) {
|
||||
r.ok = 0;
|
||||
try {
|
||||
auto jv = json::parse(body ? body : "{}");
|
||||
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());
|
||||
}
|
||||
} catch (...) {
|
||||
std::string msg = "HTTP " + std::to_string(http_status);
|
||||
r.error = h->strdup(msg.c_str());
|
||||
}
|
||||
if (!r.error) {
|
||||
std::string msg = "HTTP " + std::to_string(http_status);
|
||||
r.error = h->strdup(msg.c_str());
|
||||
}
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
auto jv = json::parse(body ? body : "{}");
|
||||
auto obj = jv.as_object();
|
||||
auto content = obj["content"].as_array();
|
||||
if (!content.empty()) {
|
||||
// W21.2: 提取 text 和 tool_use content blocks / Extract text and tool_use content blocks
|
||||
std::string text_content;
|
||||
json::array tool_use_blocks;
|
||||
|
||||
for (const auto& block : content) {
|
||||
auto bobj = block.as_object();
|
||||
if (!bobj.contains("type")) continue;
|
||||
|
||||
std::string btype = json::value_to<std::string>(bobj["type"]);
|
||||
if (btype == "text") {
|
||||
text_content = json::value_to<std::string>(bobj["text"]);
|
||||
} else if (btype == "tool_use") {
|
||||
// 转换为 OpenAI 兼容格式: {id, type:"function", function:{name, arguments}} / Convert to OpenAI-compatible format: {id, type:"function", function:{name, arguments}}
|
||||
json::object tc;
|
||||
tc["id"] = bobj["id"];
|
||||
tc["type"] = "function";
|
||||
json::object func;
|
||||
func["name"] = bobj["name"];
|
||||
func["arguments"] = json::serialize(bobj["input"]);
|
||||
tc["function"] = func;
|
||||
tool_use_blocks.push_back(std::move(tc));
|
||||
}
|
||||
}
|
||||
|
||||
if (!tool_use_blocks.empty()) {
|
||||
r.tool_calls_json = h->strdup(
|
||||
json::serialize(tool_use_blocks).c_str());
|
||||
} else {
|
||||
r.tool_calls_json = nullptr;
|
||||
}
|
||||
|
||||
if (!text_content.empty()) {
|
||||
r.content = h->strdup(text_content.c_str());
|
||||
r.ok = 1;
|
||||
r.error = nullptr;
|
||||
return;
|
||||
} else if (!tool_use_blocks.empty()) {
|
||||
// tool-only 响应 / tool-only response
|
||||
r.content = nullptr;
|
||||
r.ok = 1;
|
||||
r.error = nullptr;
|
||||
return;
|
||||
}
|
||||
r.ok = 0;
|
||||
r.error = h->strdup("no text or tool_use content block found");
|
||||
} else {
|
||||
r.ok = 0;
|
||||
r.error = h->strdup("empty response");
|
||||
}
|
||||
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.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
} catch (...) {
|
||||
r.ok = 0;
|
||||
r.error = h->strdup("json parse error");
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 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。
|
||||
// 如果产生了 content token 则返回 true,否则返回 false / Parse a single Anthropic SSE "data:" JSON event. Handles content_block_start,
|
||||
// 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)
|
||||
{
|
||||
try {
|
||||
auto jv = json::parse(data);
|
||||
auto obj = jv.as_object();
|
||||
|
||||
auto* type_ptr = obj.if_contains("type");
|
||||
if (!type_ptr || !type_ptr->is_string()) return false;
|
||||
std::string type = json::value_to<std::string>(*type_ptr);
|
||||
|
||||
if (type == "content_block_start") {
|
||||
// content_block_start 可能为 tool_use / content_block_start may be tool_use
|
||||
auto* cb = obj.if_contains("content_block");
|
||||
if (!cb || !cb->is_object()) return false;
|
||||
auto& cb_obj = cb->as_object();
|
||||
auto* cb_type = cb_obj.if_contains("type");
|
||||
if (!cb_type || !cb_type->is_string()) return false;
|
||||
std::string cb_type_str = json::value_to<std::string>(*cb_type);
|
||||
|
||||
if (cb_type_str == "tool_use" && ctx) {
|
||||
auto* idx_ptr = obj.if_contains("index");
|
||||
int idx = idx_ptr ? static_cast<int>(
|
||||
json::value_to<int64_t>(*idx_ptr)) : -1;
|
||||
if (idx < 0) return false;
|
||||
|
||||
while (static_cast<int>(ctx->tool_calls.size()) <= idx) {
|
||||
ctx->tool_calls.push_back({});
|
||||
}
|
||||
auto& acc = ctx->tool_calls[idx];
|
||||
acc.index = idx;
|
||||
if (cb_obj.contains("id") && cb_obj["id"].is_string())
|
||||
acc.id = json::value_to<std::string>(cb_obj["id"]);
|
||||
if (cb_obj.contains("name") && cb_obj["name"].is_string())
|
||||
acc.name = json::value_to<std::string>(cb_obj["name"]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == "content_block_delta") {
|
||||
auto* delta = obj.if_contains("delta");
|
||||
if (!delta || !delta->is_object()) return false;
|
||||
auto& dobj = delta->as_object();
|
||||
|
||||
auto* dtype = dobj.if_contains("type");
|
||||
if (!dtype || !dtype->is_string()) return false;
|
||||
std::string delta_type = json::value_to<std::string>(*dtype);
|
||||
|
||||
if (delta_type == "text_delta") {
|
||||
auto* text = dobj.if_contains("text");
|
||||
if (text && text->is_string()) {
|
||||
token_out = json::value_to<std::string>(*text);
|
||||
return true;
|
||||
}
|
||||
} else if (delta_type == "input_json_delta" && ctx) {
|
||||
// W21.2: 累积 tool_use arguments 分片 / Accumulate tool_use arguments fragments
|
||||
auto* pj = dobj.if_contains("partial_json");
|
||||
if (pj && pj->is_string()) {
|
||||
auto* idx_ptr = obj.if_contains("index");
|
||||
int idx = idx_ptr ? static_cast<int>(
|
||||
json::value_to<int64_t>(*idx_ptr)) : -1;
|
||||
if (idx >= 0 && idx < static_cast<int>(ctx->tool_calls.size())) {
|
||||
ctx->tool_calls[idx].arguments +=
|
||||
json::value_to<std::string>(*pj);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} else if (type == "message_stop") {
|
||||
token_out.clear();
|
||||
return true; // 流结束 / stream end
|
||||
}
|
||||
// 忽略: message_start, content_block_stop, ping, message_delta / Ignore: message_start, content_block_stop, ping, message_delta
|
||||
} catch (...) {
|
||||
// 解析失败忽略 / Ignore parse failures
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// configure / configure
|
||||
// ============================================================================
|
||||
// 配置插件:provider、endpoint、auth、model 和生成参数 / Configure the plugin with provider, endpoint, auth, model, and generation parameters.
|
||||
static int my_configure(const char* provider, const char* base_url,
|
||||
const char* api_key, const char* model,
|
||||
int max_tokens, double temperature)
|
||||
{
|
||||
try {
|
||||
if (provider) g_cfg.provider = provider;
|
||||
if (base_url) g_cfg.base_url = base_url;
|
||||
if (api_key) g_cfg.api_key = api_key;
|
||||
if (model) g_cfg.model = model;
|
||||
g_cfg.max_tokens = max_tokens;
|
||||
g_cfg.temperature = temperature;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
h->log(DSTALK_LOG_INFO,
|
||||
"[anthropic] configured: model=%s base_url=%s max_tokens=%d temperature=%.2f",
|
||||
g_cfg.model.c_str(), g_cfg.base_url.c_str(),
|
||||
g_cfg.max_tokens, g_cfg.temperature);
|
||||
}
|
||||
return 0;
|
||||
} 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] my_configure exception: %s", e.what());
|
||||
return -1;
|
||||
} catch (...) {
|
||||
const auto* h = g_host.load(std::memory_order_acquire);
|
||||
if (h && h->log) h->log(DSTALK_LOG_ERROR, "[anthropic] my_configure unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// chat / chat
|
||||
// ============================================================================
|
||||
// 非流式 chat completion:发送 history + user input,返回完整响应 / Non-streaming chat completion: send history + user input, return full response.
|
||||
static dstalk_chat_result_t my_chat(
|
||||
const dstalk_message_t* history, int history_len,
|
||||
const char* user_input,
|
||||
const char* tools_json)
|
||||
{
|
||||
try {
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
|
||||
const auto* host = g_host.load(std::memory_order_acquire);
|
||||
const auto* http = g_http.load(std::memory_order_acquire);
|
||||
|
||||
if (!http) {
|
||||
r.error = host->strdup("http service not available");
|
||||
return r;
|
||||
}
|
||||
|
||||
std::string scheme, hostname, port, target;
|
||||
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,
|
||||
user_input ? user_input : "",
|
||||
tools_json ? tools_json : g_tools_json, false);
|
||||
|
||||
std::string headers_json = build_headers_json();
|
||||
|
||||
char* response_body = nullptr;
|
||||
int status_code = 0;
|
||||
|
||||
int ret = http->post_json(
|
||||
hostname.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->strdup("http request failed");
|
||||
if (response_body) host->free(response_body);
|
||||
return r;
|
||||
}
|
||||
|
||||
parse_response(response_body, status_code, r);
|
||||
|
||||
if (response_body) {
|
||||
host->free(response_body);
|
||||
}
|
||||
return r;
|
||||
} catch (const std::exception& e) {
|
||||
const auto* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[anthropic] my_chat exception: %s", e.what());
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = host ? host->strdup(e.what()) : nullptr;
|
||||
return r;
|
||||
} catch (...) {
|
||||
const auto* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[anthropic] my_chat unknown exception");
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = host ? host->strdup("unknown exception") : nullptr;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 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);
|
||||
if (!line || !line[0]) return 1; // 空行,继续 / empty line, continue
|
||||
|
||||
std::string line_str(line);
|
||||
|
||||
// SSE 格式: "data: <json>" / SSE format: "data: <json>"
|
||||
if (line_str.rfind("data: ", 0) == 0) {
|
||||
std::string data = line_str.substr(6);
|
||||
std::string token;
|
||||
if (parse_sse_data(data, token, ctx)) {
|
||||
ctx->saw_data_line = true;
|
||||
if (token.empty()) {
|
||||
// message_stop / message_stop
|
||||
return 0;
|
||||
}
|
||||
ctx->accumulated += token;
|
||||
if (ctx->user_cb) {
|
||||
return ctx->user_cb(token.c_str(), ctx->userdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
// "event: ..." 行和其他 -> 忽略 / "event: ..." lines and others -> ignored
|
||||
return 1;
|
||||
} 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] sse_line_callback exception: %s", e.what());
|
||||
return 0;
|
||||
} catch (...) {
|
||||
const auto* h = g_host.load(std::memory_order_acquire);
|
||||
if (h && h->log) h->log(DSTALK_LOG_ERROR, "[anthropic] sse_line_callback unknown exception");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 流式 chat completion:以 stream=true 发送 history + user input,通过回调传递 token。
|
||||
// 累积 tool_use blocks 并在结束时序列化 / Streaming chat completion: send history + user input with stream=true, deliver tokens
|
||||
// via callback. Accumulates tool_use blocks and serializes them at end.
|
||||
static dstalk_chat_result_t my_chat_stream(
|
||||
const dstalk_message_t* history, int history_len,
|
||||
const char* user_input,
|
||||
dstalk_stream_cb cb, void* userdata)
|
||||
{
|
||||
try {
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
|
||||
const auto* host = g_host.load(std::memory_order_acquire);
|
||||
const auto* http = g_http.load(std::memory_order_acquire);
|
||||
|
||||
if (!http) {
|
||||
r.error = host->strdup("http service not available");
|
||||
return r;
|
||||
}
|
||||
|
||||
std::string scheme, hostname, port, target;
|
||||
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,
|
||||
user_input ? user_input : "", g_tools_json, true);
|
||||
|
||||
std::string headers_json = build_headers_json();
|
||||
|
||||
StreamContext ctx;
|
||||
ctx.host = host;
|
||||
ctx.user_cb = cb;
|
||||
ctx.userdata = userdata;
|
||||
ctx.saw_data_line = false;
|
||||
|
||||
char* response_body = nullptr;
|
||||
int status_code = 0;
|
||||
|
||||
int ret = http->post_stream(
|
||||
hostname.c_str(), port.c_str(), target_path.c_str(), body.c_str(),
|
||||
headers_json.c_str(),
|
||||
sse_line_callback, &ctx,
|
||||
&response_body, &status_code);
|
||||
|
||||
r.http_status = status_code;
|
||||
|
||||
// 检查错误状态 / Check error status
|
||||
if (status_code < 200 || status_code >= 300) {
|
||||
r.ok = 0;
|
||||
if (response_body && response_body[0]) {
|
||||
try {
|
||||
auto jv = json::parse(response_body);
|
||||
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());
|
||||
}
|
||||
} catch (...) {}
|
||||
}
|
||||
if (!r.error) {
|
||||
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);
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
return r;
|
||||
}
|
||||
|
||||
if (response_body) 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();
|
||||
bool has_tool_calls = !ctx.tool_calls.empty();
|
||||
|
||||
if (!has_content && !has_tool_calls) {
|
||||
r.ok = 0;
|
||||
r.error = host->strdup("no content received");
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
} else {
|
||||
r.ok = 1;
|
||||
r.error = nullptr;
|
||||
r.content = has_content
|
||||
? host->strdup(ctx.accumulated.c_str()) : nullptr;
|
||||
|
||||
// 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);
|
||||
r.tool_calls_json = host ? host->strdup(tc_json.c_str()) : nullptr;
|
||||
} else {
|
||||
r.tool_calls_json = nullptr;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
} catch (const std::exception& e) {
|
||||
const auto* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[anthropic] my_chat_stream exception: %s", e.what());
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = host ? host->strdup(e.what()) : nullptr;
|
||||
return r;
|
||||
} catch (...) {
|
||||
const auto* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[anthropic] my_chat_stream unknown exception");
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = host ? host->strdup("unknown exception") : nullptr;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// free_result / free_result
|
||||
// ============================================================================
|
||||
// 释放 chat result 结构体中所有主机分配的字符串字段 / Free all host-allocated string fields in a chat result struct.
|
||||
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; }
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 服务 vtable / Service vtable
|
||||
// ============================================================================
|
||||
static dstalk_ai_service_t g_service = {
|
||||
&my_configure,
|
||||
&my_chat,
|
||||
&my_chat_stream,
|
||||
&my_free_result,
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// 生命周期 / Lifecycle
|
||||
// ============================================================================
|
||||
// 插件初始化:查询 http 和 config 服务,注册 ai.anthropic 服务 / Plugin init: query http and config services, register ai.anthropic service.
|
||||
static int on_init(const dstalk_host_api_t* host)
|
||||
{
|
||||
try {
|
||||
g_host.store(host, std::memory_order_release);
|
||||
|
||||
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);
|
||||
|
||||
if (!http_svc) {
|
||||
if (host) host->log(DSTALK_LOG_ERROR, "[anthropic] http service not found");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (host) host->log(DSTALK_LOG_INFO, "[anthropic] initializing Anthropic AI plugin");
|
||||
|
||||
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());
|
||||
return -1;
|
||||
} catch (...) {
|
||||
const auto* h = g_host.load(std::memory_order_acquire);
|
||||
if (h && h->log) h->log(DSTALK_LOG_ERROR, "[anthropic] on_init unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// 插件关闭:从内存安全擦除 API key,清空服务指针 / Plugin shutdown: securely erase API key from memory, null out service pointers.
|
||||
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());
|
||||
g_cfg.api_key.clear();
|
||||
g_http.store(nullptr, std::memory_order_release);
|
||||
g_config = nullptr;
|
||||
g_host.store(nullptr, std::memory_order_release);
|
||||
} 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_shutdown exception: %s", e.what());
|
||||
} catch (...) {
|
||||
const auto* h = g_host.load(std::memory_order_acquire);
|
||||
if (h && h->log) h->log(DSTALK_LOG_ERROR, "[anthropic] on_shutdown unknown exception");
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 插件描述符 / Plugin descriptor
|
||||
// ============================================================================
|
||||
static dstalk_plugin_info_t g_info = {
|
||||
/* .name = */ "anthropic-ai",
|
||||
/* .version = */ "1.0.0",
|
||||
/* .description = */ "Anthropic Claude AI provider (Messages API) / Anthropic Claude AI 提供者 (Messages API)",
|
||||
/* .api_version = */ DSTALK_API_VERSION,
|
||||
/* .dependencies = */ { "http", "config", NULL },
|
||||
/* .on_init = */ on_init,
|
||||
/* .on_shutdown = */ on_shutdown,
|
||||
/* .on_event = */ nullptr,
|
||||
};
|
||||
|
||||
// 必须入口点:返回插件描述符给主机 / Mandatory entry point: returns the plugin descriptor to the host.
|
||||
extern "C" DSTALK_PLUGIN_EXPORT dstalk_plugin_info_t* dstalk_plugin_init(void)
|
||||
{
|
||||
return &g_info;
|
||||
}
|
||||
Reference in New Issue
Block a user