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,9 @@
add_library(plugin-file_io SHARED src/file_io_plugin.cpp)
target_link_libraries(plugin-file_io PRIVATE dstalk)
set_target_properties(plugin-file_io PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
)

View File

@@ -0,0 +1,107 @@
/*
* @file file_io_plugin.cpp
* @brief File I/O plugin: basic file read/write service.
* 文件 I/O 插件:基本文件读写服务。
* Copyright (c) 2026 dstalk contributors. GPLv3.
*/
#include "dstalk/dstalk_host.h"
#include "dstalk/dstalk_services.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
// ============================================================
// 全局状态 / Global state
// ============================================================
static const dstalk_host_api_t* g_host = nullptr;
// ============================================================
// 服务实现 / Service implementations
// ============================================================
// 读取文件全部内容到主机分配的缓冲区,调用方须通过 host->free 释放 / Read the entire contents of a file into a host-allocated buffer. Caller must free via host->free.
static int file_read(const char* path, char** content) {
if (!path || !content) return -1;
FILE* fp = fopen(path, "rb");
if (!fp) return -1;
// 获取文件大小 / Get file size
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fsize < 0) {
fclose(fp);
return -1;
}
// 通过主机分配器分配缓冲区(+1 用于空终止符) / Allocate buffer via host allocator (+1 for null terminator)
char* buf = (char*)g_host->alloc((size_t)fsize + 1);
if (!buf) {
fclose(fp);
return -1;
}
size_t read_bytes = fread(buf, 1, (size_t)fsize, fp);
fclose(fp);
if (read_bytes != (size_t)fsize) {
g_host->free(buf);
return -1;
}
buf[read_bytes] = '\0';
*content = buf;
return 0;
}
// 将字符串写入文件,覆盖已有内容 / Write a string to a file, overwriting any existing content.
static int file_write(const char* path, const char* content) {
if (!path || !content) return -1;
FILE* fp = fopen(path, "wb");
if (!fp) return -1;
size_t len = strlen(content);
size_t written = fwrite(content, 1, len, fp);
fclose(fp);
return (written == len) ? 0 : -1;
}
static dstalk_file_io_service_t g_service = {
file_read,
file_write
};
// ============================================================
// 插件生命周期 / Plugin lifecycle
// ============================================================
// 插件初始化:保存主机指针并注册 file_io 服务 / Plugin init: store host pointer and register the file_io service.
static int on_init(const dstalk_host_api_t* host) {
g_host = host;
return host->register_service("file_io", 1, &g_service);
}
// 插件关闭:无需清理 / Plugin shutdown: nothing to clean up.
static void on_shutdown() {
// 无需清理 / nothing to clean up
}
static dstalk_plugin_info_t g_info = {
"file_io", // name 名称
"1.0.0", // version 版本
"Basic file I/O service", // description 描述
DSTALK_API_VERSION, // api_version
{nullptr}, // dependencies 依赖 (none)
on_init, // on_init
on_shutdown, // on_shutdown
nullptr // on_event
};
// 必须入口点:返回插件描述符给主机 / 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;
}