Refactor to plugin architecture with B3 CLI UX, C2 smoke tests, C3 CI scripts
Architecture overhaul (Wave 1-4 collaborative work): - Migrated dstalk-core from monolithic api.cpp to plugin-based design with host/service_registry/event_bus/plugin_loader and topological initialization. - Split public headers into dstalk_host.h / dstalk_services.h / dstalk_lsp.h / dstalk_types.h; deleted obsolete dstalk_api.h and inlined TLS/file/net code now provided by plugins. - Added 9 plugins: deepseek, anthropic, network, session, context, tools, config, file-io, lsp; AI plugins register as "ai.<provider>" services. B3 CLI interaction enhancement: - Prompt now shows current model name (A1). - /status command prints model/base_url/api_key (sanitized: shown only as set/unset)/services readiness (A2). - SIGINT/Ctrl+C handled on POSIX (signal) and Windows (SetConsoleCtrlHandler); /quit no longer std::exit(0) but sets a quit flag so dstalk_shutdown runs exactly once via natural control flow (B1+B2). - Cross-DLL free fixed: print_file uses dstalk_free instead of std::free (B4). - --batch mode plus isatty auto-detection for piped stdin (C1). - fgets truncation detection with friendly error and stdin draining (C3). - Distinct exit codes (init/AI/service-unavailable) (C4). - /model rejects empty model name (C5). C2 smoke test extension: - 4 new test blocks: null-safety (file_io/session/tools/config), escape-boundary round-trip, tools->execute call chain, session robustness (add(nullptr), clear -> token_count == 0). C3 CI build scripts: - scripts/ci-build.sh and scripts/ci-build.bat invoke cmake configure + parallel build + ctest, suitable for GitHub Actions. Build verified: dstalk-cli compiles, smoke test passes via ctest. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
18
plugins/session/CMakeLists.txt
Normal file
18
plugins/session/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
add_library(plugin-session SHARED src/session_plugin.cpp)
|
||||
|
||||
target_include_directories(plugin-session PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/dstalk-core/include
|
||||
)
|
||||
|
||||
target_link_libraries(plugin-session PRIVATE dstalk)
|
||||
|
||||
find_package(Boost REQUIRED CONFIG)
|
||||
target_link_libraries(plugin-session PRIVATE boost::boost)
|
||||
target_compile_definitions(plugin-session PRIVATE
|
||||
BOOST_ALL_NO_LIB BOOST_ERROR_CODE_HEADER_ONLY BOOST_JSON_HEADER_ONLY)
|
||||
|
||||
set_target_properties(plugin-session PROPERTIES
|
||||
PREFIX ""
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
|
||||
)
|
||||
263
plugins/session/src/session_plugin.cpp
Normal file
263
plugins/session/src/session_plugin.cpp
Normal file
@@ -0,0 +1,263 @@
|
||||
// plugin-session: 会话管理服务插件
|
||||
// 提供 dstalk_session_service_t vtable 实现
|
||||
// 依赖: file_io (save/load 需要文件操作)
|
||||
#include "dstalk/dstalk_host.h"
|
||||
#include "dstalk/dstalk_types.h"
|
||||
#include "dstalk/dstalk_services.h"
|
||||
|
||||
#include <boost/json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace json = boost::json;
|
||||
|
||||
// ============================================================
|
||||
// 内部 C++ 数据结构
|
||||
// ============================================================
|
||||
|
||||
static const dstalk_host_api_t* g_host = nullptr;
|
||||
|
||||
// 缓存 file_io 服务指针
|
||||
static const dstalk_file_io_service_t* g_file_io = nullptr;
|
||||
|
||||
// 内部消息结构(C++ 易用,外部暴露 C struct)
|
||||
struct InternalMessage {
|
||||
std::string role;
|
||||
std::string content;
|
||||
std::string tool_call_id;
|
||||
std::string tool_calls_json;
|
||||
};
|
||||
|
||||
// 会话历史
|
||||
static std::vector<InternalMessage> g_history;
|
||||
|
||||
// history() 返回的 C 数组缓存(生命周期到下次 history() 或 shutdown)
|
||||
static std::vector<dstalk_message_t> g_cached_history;
|
||||
|
||||
// ============================================================
|
||||
// Token 计数工具(内联,避免硬依赖 context 头文件)
|
||||
// ============================================================
|
||||
|
||||
static bool is_ascii(unsigned char c) { return c < 0x80; }
|
||||
|
||||
static bool starts_cjk(unsigned char c) {
|
||||
return c >= 0xE4 && c <= 0xE9;
|
||||
}
|
||||
|
||||
static size_t count_tokens_one(const std::string& text) {
|
||||
size_t ascii_chars = 0;
|
||||
size_t chinese_chars = 0;
|
||||
size_t other_chars = 0;
|
||||
|
||||
size_t i = 0;
|
||||
while (i < text.size()) {
|
||||
unsigned char c = static_cast<unsigned char>(text[i]);
|
||||
|
||||
if (is_ascii(c)) {
|
||||
ascii_chars++;
|
||||
i += 1;
|
||||
} else if (starts_cjk(c)) {
|
||||
chinese_chars++;
|
||||
i += 3;
|
||||
} else if (c >= 0xC0 && c < 0xE0) {
|
||||
other_chars++;
|
||||
i += 2;
|
||||
} else if (c >= 0xE0 && c < 0xF0) {
|
||||
other_chars++;
|
||||
i += 3;
|
||||
} else if (c >= 0xF0 && c < 0xF8) {
|
||||
other_chars++;
|
||||
i += 4;
|
||||
} else {
|
||||
other_chars++;
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
size_t content_tokens = (ascii_chars / 4) + (chinese_chars / 2) + (other_chars / 3);
|
||||
return content_tokens + 4; // +4 per message overhead
|
||||
}
|
||||
|
||||
static size_t count_tokens_all(const std::vector<InternalMessage>& msgs) {
|
||||
size_t total = 0;
|
||||
for (const auto& m : msgs) {
|
||||
total += count_tokens_one(m.content);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 辅助:刷新 C 缓存数组
|
||||
// ============================================================
|
||||
|
||||
static void rebuild_cached_history() {
|
||||
// 释放旧的字符串
|
||||
for (auto& m : g_cached_history) {
|
||||
if (m.role) { g_host->free(const_cast<char*>(m.role)); }
|
||||
if (m.content) { g_host->free(const_cast<char*>(m.content)); }
|
||||
if (m.tool_call_id) { g_host->free(const_cast<char*>(m.tool_call_id)); }
|
||||
if (m.tool_calls_json){ g_host->free(const_cast<char*>(m.tool_calls_json)); }
|
||||
}
|
||||
g_cached_history.clear();
|
||||
|
||||
// 重建
|
||||
g_cached_history.reserve(g_history.size());
|
||||
for (const auto& im : g_history) {
|
||||
dstalk_message_t cm;
|
||||
cm.role = im.role.empty() ? nullptr : g_host->strdup(im.role.c_str());
|
||||
cm.content = im.content.empty() ? nullptr : g_host->strdup(im.content.c_str());
|
||||
cm.tool_call_id = im.tool_call_id.empty() ? nullptr : g_host->strdup(im.tool_call_id.c_str());
|
||||
cm.tool_calls_json = im.tool_calls_json.empty() ? nullptr : g_host->strdup(im.tool_calls_json.c_str());
|
||||
g_cached_history.push_back(cm);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Session 服务 vtable 实现
|
||||
// ============================================================
|
||||
|
||||
static void session_add(const dstalk_message_t* msg) {
|
||||
if (!msg) return;
|
||||
InternalMessage im;
|
||||
if (msg->role) im.role = msg->role;
|
||||
if (msg->content) im.content = msg->content;
|
||||
if (msg->tool_call_id) im.tool_call_id = msg->tool_call_id;
|
||||
if (msg->tool_calls_json) im.tool_calls_json = msg->tool_calls_json;
|
||||
g_history.push_back(std::move(im));
|
||||
}
|
||||
|
||||
static void session_clear() {
|
||||
g_history.clear();
|
||||
}
|
||||
|
||||
static int session_save(const char* path) {
|
||||
if (!path || !g_file_io) return -1;
|
||||
|
||||
std::string data;
|
||||
for (const auto& m : g_history) {
|
||||
json::object entry;
|
||||
entry["role"] = m.role;
|
||||
entry["content"] = m.content;
|
||||
if (!m.tool_call_id.empty())
|
||||
entry["tool_call_id"] = m.tool_call_id;
|
||||
if (!m.tool_calls_json.empty())
|
||||
entry["tool_calls_json"] = m.tool_calls_json;
|
||||
data += json::serialize(entry);
|
||||
data += '\n';
|
||||
}
|
||||
return g_file_io->write(path, data.c_str());
|
||||
}
|
||||
|
||||
static int session_load(const char* path) {
|
||||
if (!path || !g_file_io) return -1;
|
||||
|
||||
char* content = nullptr;
|
||||
int ret = g_file_io->read(path, &content);
|
||||
if (ret != 0 || !content) return -1;
|
||||
|
||||
std::string data(content);
|
||||
std::free(content);
|
||||
|
||||
std::vector<InternalMessage> parsed;
|
||||
size_t pos = 0;
|
||||
while (pos < data.size()) {
|
||||
size_t nl = data.find('\n', pos);
|
||||
std::string line = (nl != std::string::npos)
|
||||
? data.substr(pos, nl - pos) : data.substr(pos);
|
||||
pos = (nl != std::string::npos) ? nl + 1 : data.size();
|
||||
if (line.empty()) continue;
|
||||
|
||||
try {
|
||||
auto obj = json::parse(line).as_object();
|
||||
auto* role_j = obj.if_contains("role");
|
||||
auto* content_j = obj.if_contains("content");
|
||||
if (role_j && content_j && role_j->is_string() && content_j->is_string()) {
|
||||
InternalMessage im;
|
||||
im.role = json::value_to<std::string>(*role_j);
|
||||
im.content = json::value_to<std::string>(*content_j);
|
||||
auto* tci = obj.if_contains("tool_call_id");
|
||||
if (tci && tci->is_string())
|
||||
im.tool_call_id = json::value_to<std::string>(*tci);
|
||||
auto* tcj = obj.if_contains("tool_calls_json");
|
||||
if (tcj && tcj->is_string())
|
||||
im.tool_calls_json = json::value_to<std::string>(*tcj);
|
||||
parsed.push_back(std::move(im));
|
||||
}
|
||||
} catch (const std::exception&) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.empty()) return -1;
|
||||
g_history = std::move(parsed);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const dstalk_message_t* session_history(int* out_count) {
|
||||
rebuild_cached_history();
|
||||
if (out_count) *out_count = static_cast<int>(g_cached_history.size());
|
||||
return g_cached_history.empty() ? nullptr : g_cached_history.data();
|
||||
}
|
||||
|
||||
static int session_token_count() {
|
||||
return static_cast<int>(count_tokens_all(g_history));
|
||||
}
|
||||
|
||||
static dstalk_session_service_t g_session_service = {
|
||||
session_add,
|
||||
session_clear,
|
||||
session_save,
|
||||
session_load,
|
||||
session_history,
|
||||
session_token_count
|
||||
};
|
||||
|
||||
// ============================================================
|
||||
// 插件生命周期
|
||||
// ============================================================
|
||||
|
||||
static int on_init(const dstalk_host_api_t* host) {
|
||||
g_host = host;
|
||||
|
||||
// 查询依赖服务: 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");
|
||||
return -1;
|
||||
}
|
||||
g_file_io = static_cast<const dstalk_file_io_service_t*>(raw);
|
||||
|
||||
// 注册自身服务
|
||||
return host->register_service("session", 1, &g_session_service);
|
||||
}
|
||||
|
||||
static void on_shutdown() {
|
||||
// 释放缓存
|
||||
rebuild_cached_history(); // 这会先清理旧字符串再清空
|
||||
g_cached_history.clear(); // 确保空
|
||||
g_history.clear();
|
||||
g_file_io = nullptr;
|
||||
g_host = nullptr;
|
||||
}
|
||||
|
||||
static dstalk_plugin_info_t g_info = {
|
||||
"session",
|
||||
"1.0.0",
|
||||
"Session management plugin with save/load support",
|
||||
DSTALK_API_VERSION,
|
||||
{"file_io", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
|
||||
on_init,
|
||||
on_shutdown,
|
||||
nullptr
|
||||
};
|
||||
|
||||
extern "C" DSTALK_PLUGIN_EXPORT dstalk_plugin_info_t* dstalk_plugin_init(void) {
|
||||
return &g_info;
|
||||
}
|
||||
Reference in New Issue
Block a user