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:
2026-05-31 05:37:04 +08:00
parent f6cb51b40a
commit ba7382db2a
61 changed files with 163 additions and 147 deletions

View File

@@ -0,0 +1,12 @@
add_library(plugin-tools SHARED src/tools_plugin.cpp)
target_link_libraries(plugin-tools PRIVATE dstalk)
find_package(Boost REQUIRED CONFIG)
target_link_libraries(plugin-tools PRIVATE boost::boost dstalk_boost_config)
set_target_properties(plugin-tools PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
)

View File

@@ -0,0 +1,388 @@
/*
* @file tools_plugin.cpp
* @brief Tools plugin: tool registration, schema management, and execution registry.
* 工具插件工具注册、schema 管理和执行注册表。
* Copyright (c) 2026 dstalk contributors. GPLv3.
*/
// 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"
#include "dstalk/dstalk_types.h"
#include "dstalk/dstalk_services.h"
#include <boost/json.hpp>
#include <boost/json/src.hpp>
#include <atomic>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <filesystem>
#include <mutex>
#include <string>
#include <vector>
namespace json = boost::json;
// ============================================================
// 路径安全校验 (W14.3: 防止路径遍历攻击) / Path safety validation (W14.3: prevent path traversal attacks)
// ============================================================
// 验证文件路径是否安全(无绝对路径、无 ".." 遍历、非空) / Validate that a file path is safe (no absolute paths, no ".." traversal, no empty).
static bool is_safe_path(const std::string& path) {
// 拒绝空路径 / Reject empty path
if (path.empty()) return false;
// 拒绝绝对路径: Unix '/' 开头 或 Windows 盘符 (第二字符 ':') / Reject absolute paths: Unix '/' prefix or Windows drive letter (second char ':')
if (path[0] == '/' || path[0] == '\\') return false;
if (path.size() >= 2 && path[1] == ':') return false;
// 拒绝含 ".." 段的目录遍历 / Reject directory traversal with ".." segments
if (path.find("..") != std::string::npos) return false;
// lexical_normal 消解相对组件后再次校验 / Re-validate after resolving relative components with lexical_normal
std::string norm = std::filesystem::path(path).lexically_normal().string();
if (norm.empty()) return false;
if (norm[0] == '/' || norm[0] == '\\') return false;
if (norm.size() >= 2 && norm[1] == ':') return false;
if (norm.find("..") != std::string::npos) return false;
return true;
}
// ============================================================
// 内部数据结构 / Internal data structures
// ============================================================
// W14.3: g_host / g_file_io 使用 atomic 指针,写入 acquire/release读取无锁 / g_host / g_file_io use atomic pointers, write with acquire/release, read lock-free
static std::atomic<const dstalk_host_api_t*> g_host{nullptr};
static std::atomic<const dstalk_file_io_service_t*> g_file_io{nullptr};
struct ToolDef {
std::string name;
std::string description;
std::string parameters_schema;
dstalk_tool_handler_fn handler;
};
// W14.3: g_tools 使用 mutex 保护读写 / g_tools uses mutex to protect read/write
static std::vector<ToolDef> g_tools;
static std::mutex g_tools_mutex;
// ============================================================
// 内置工具: file_read, file_write / Built-in tools: file_read, file_write
// ============================================================
// 内置工具处理器:读取文件并以 JSON 字符串返回内容 / Built-in tool handler: read a file and return its contents as a JSON string.
static char* builtin_file_read(const char* args_json) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
const dstalk_file_io_service_t* fio = g_file_io.load(std::memory_order_acquire);
if (!fio) {
return host ? host->strdup("{\"error\":\"file_io service not available\"}") : nullptr;
}
try {
auto args = json::parse(args_json).as_object();
auto* path_j = args.if_contains("path");
if (!path_j || !path_j->is_string()) {
return host ? host->strdup("{\"error\":\"missing 'path' argument\"}") : nullptr;
}
std::string path = json::value_to<std::string>(*path_j);
// W14.3: 路径遍历防护 / Path traversal protection
if (!is_safe_path(path)) {
if (host) host->log(DSTALK_LOG_ERROR, "builtin_file_read: unsafe path rejected");
return host ? host->strdup("{\"error\":\"access denied: unsafe path\"}") : nullptr;
}
char* content = nullptr;
int ret = fio->read(path.c_str(), &content);
if (ret != 0 || !content) {
return host ? host->strdup("{\"error\":\"failed to read file\"}") : nullptr;
}
std::string escaped_content = json::serialize(json::string(content));
if (host) host->free(content);
std::string result = "{\"content\":" + escaped_content + "}";
return host ? host->strdup(result.c_str()) : nullptr;
} catch (const std::exception& e) {
if (host) host->log(DSTALK_LOG_ERROR, "builtin_file_read: %s", e.what());
std::string err = "{\"error\":\"file_read internal error\"}";
return host ? host->strdup(err.c_str()) : nullptr;
} catch (...) {
if (host) host->log(DSTALK_LOG_ERROR, "builtin_file_read: unknown exception");
return host ? host->strdup("{\"error\":\"file_read internal error\"}") : nullptr;
}
}
// 内置工具处理器:将内容写入文件,返回成功/错误 JSON 对象 / Built-in tool handler: write content to a file, returning a success/error JSON object.
static char* builtin_file_write(const char* args_json) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
const dstalk_file_io_service_t* fio = g_file_io.load(std::memory_order_acquire);
if (!fio) {
return host ? host->strdup("{\"error\":\"file_io service not available\"}") : nullptr;
}
try {
auto args = json::parse(args_json).as_object();
auto* path_j = args.if_contains("path");
auto* content_j = args.if_contains("content");
if (!path_j || !path_j->is_string()) {
return host ? host->strdup("{\"error\":\"missing 'path' argument\"}") : nullptr;
}
if (!content_j || !content_j->is_string()) {
return host ? host->strdup("{\"error\":\"missing 'content' argument\"}") : nullptr;
}
std::string path = json::value_to<std::string>(*path_j);
std::string content = json::value_to<std::string>(*content_j);
// W14.3: 路径遍历防护 / Path traversal protection
if (!is_safe_path(path)) {
if (host) host->log(DSTALK_LOG_ERROR, "builtin_file_write: unsafe path rejected");
return host ? host->strdup("{\"error\":\"access denied: unsafe path\"}") : nullptr;
}
int ret = fio->write(path.c_str(), content.c_str());
if (ret != 0) {
return host ? host->strdup("{\"error\":\"failed to write file\"}") : nullptr;
}
return host ? host->strdup("{\"success\":true}") : nullptr;
} catch (const std::exception& e) {
if (host) host->log(DSTALK_LOG_ERROR, "builtin_file_write: %s", e.what());
std::string err = "{\"error\":\"file_write internal error\"}";
return host ? host->strdup(err.c_str()) : nullptr;
} catch (...) {
if (host) host->log(DSTALK_LOG_ERROR, "builtin_file_write: unknown exception");
return host ? host->strdup("{\"error\":\"file_write internal error\"}") : nullptr;
}
}
// ============================================================
// Tools 服务 vtable 实现 (W14.3: try/catch + mutex) / Tools service vtable implementation (W14.3: try/catch + mutex)
// ============================================================
static void tools_unregister_tool(const char* name);
// 注册命名工具及其描述、JSON Schema 参数和处理函数 / Register a named tool with its description, JSON Schema parameters, and handler function.
static int tools_register_tool(const char* name, const char* desc,
const char* params_schema,
dstalk_tool_handler_fn handler) {
try {
if (!name || !handler) return -1;
// 如果已存在同名工具,先注销 / If a tool with the same name exists, unregister first
tools_unregister_tool(name);
ToolDef td;
td.name = name;
td.description = desc ? desc : "";
td.parameters_schema = params_schema ? params_schema : "";
td.handler = handler;
std::lock_guard<std::mutex> lock(g_tools_mutex);
g_tools.push_back(std::move(td));
return 0;
} catch (const std::exception& e) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_register_tool: %s", e.what());
return -1;
} catch (...) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_register_tool: unknown exception");
return -1;
}
}
// 按名称注销之前注册的工具 / Unregister a previously registered tool by name.
static void tools_unregister_tool(const char* name) {
try {
if (!name) return;
std::string n(name);
std::lock_guard<std::mutex> lock(g_tools_mutex);
g_tools.erase(
std::remove_if(g_tools.begin(), g_tools.end(),
[&n](const ToolDef& t) { return t.name == n; }),
g_tools.end());
} catch (const std::exception& e) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_unregister_tool: %s", e.what());
} catch (...) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_unregister_tool: unknown exception");
}
}
// 将所有已注册工具序列化为 OpenAI function-calling 格式的 JSON 数组 / Serialize all registered tools into a JSON array in OpenAI function-calling format.
static char* tools_get_tools_json() {
try {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
json::array tools_arr;
{
std::lock_guard<std::mutex> lock(g_tools_mutex);
for (const auto& t : g_tools) {
json::object tool_obj;
tool_obj["type"] = "function";
json::object func_obj;
func_obj["name"] = t.name;
func_obj["description"] = t.description;
if (!t.parameters_schema.empty()) {
func_obj["parameters"] = json::parse(t.parameters_schema);
} else {
json::object empty_params;
empty_params["type"] = "object";
empty_params["properties"] = json::object{};
func_obj["parameters"] = empty_params;
}
tool_obj["function"] = func_obj;
tools_arr.push_back(tool_obj);
}
}
std::string result = json::serialize(tools_arr);
return host ? host->strdup(result.c_str()) : nullptr;
} catch (const std::exception& e) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_get_tools_json: %s", e.what());
return nullptr;
} catch (...) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_get_tools_json: unknown exception");
return nullptr;
}
}
// 按名称查找工具并分派执行到注册的处理器 / Look up a tool by name and dispatch execution to its registered handler.
static char* tools_execute(const char* name, const char* args_json) {
try {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (!name) {
return host ? host->strdup("{\"error\":\"tool name is null\"}") : nullptr;
}
std::string n(name);
ToolDef* found = nullptr;
{
std::lock_guard<std::mutex> lock(g_tools_mutex);
for (auto& t : g_tools) {
if (t.name == n) {
found = &t;
break;
}
}
}
if (!found) {
json::object err_obj;
err_obj["error"] = "unknown tool: " + n;
return host ? host->strdup(json::serialize(err_obj).c_str()) : nullptr;
}
const char* args = args_json ? args_json : "{}";
return found->handler(args);
} catch (const std::exception& e) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_execute: %s", e.what());
json::object err_obj;
err_obj["error"] = "tool execution internal error";
return host ? host->strdup(json::serialize(err_obj).c_str()) : nullptr;
} catch (...) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "tools_execute: unknown exception");
return host ? host->strdup("{\"error\":\"tool execution internal error\"}") : nullptr;
}
}
static dstalk_tools_service_t g_tools_service = {
tools_register_tool,
tools_unregister_tool,
tools_get_tools_json,
tools_execute
};
// ============================================================
// 插件生命周期 / Plugin lifecycle
// ============================================================
// 插件初始化:查询 file_io 依赖,注册内置文件工具,注册 tools 服务 / Plugin init: query file_io dependency, register built-in file tools, register tools service.
static int on_init(const dstalk_host_api_t* host) {
try {
g_host.store(host, std::memory_order_release);
// 查询依赖服务: 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");
return -1;
}
g_file_io.store(static_cast<const dstalk_file_io_service_t*>(raw), std::memory_order_release);
// 向自身注册内置工具 / Register built-in tools with self
tools_register_tool(
"file_read",
"Read the contents of a file at the given path",
"{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Path to the file to read\"}},\"required\":[\"path\"]}",
builtin_file_read
);
tools_register_tool(
"file_write",
"Write content to a file at the given path",
"{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Path to the file to write\"},\"content\":{\"type\":\"string\",\"description\":\"Content to write to the file\"}},\"required\":[\"path\",\"content\"]}",
builtin_file_write
);
return host->register_service("tools", 1, &g_tools_service);
} catch (const std::exception& e) {
const dstalk_host_api_t* h = g_host.load(std::memory_order_acquire);
if (h) h->log(DSTALK_LOG_ERROR, "on_init[tools]: %s", e.what());
return -1;
} catch (...) {
const dstalk_host_api_t* h = g_host.load(std::memory_order_acquire);
if (h) h->log(DSTALK_LOG_ERROR, "on_init[tools]: unknown exception");
return -1;
}
}
// 插件关闭:清空所有已注册工具并清空服务指针 / Plugin shutdown: clear all registered tools and null out service pointers.
static void on_shutdown() {
try {
std::lock_guard<std::mutex> lock(g_tools_mutex);
g_tools.clear();
} catch (const std::exception& e) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "on_shutdown[tools]: %s", e.what());
} catch (...) {
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
if (host) host->log(DSTALK_LOG_ERROR, "on_shutdown[tools]: unknown exception");
}
g_file_io.store(nullptr, std::memory_order_release);
g_host.store(nullptr, std::memory_order_release);
}
static dstalk_plugin_info_t g_info = {
"tools",
"1.0.0",
"Tool registration and execution plugin with built-in file tools / 内置文件工具的工具注册和执行插件",
DSTALK_API_VERSION,
{"file_io", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
on_init,
on_shutdown,
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;
}