Repair streaming callback/error handling and make file/session handling safer so the core API behaves correctly under real usage. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
53 lines
1.8 KiB
C
53 lines
1.8 KiB
C
#ifndef DSTALK_API_H
|
||
#define DSTALK_API_H
|
||
|
||
#ifdef __cplusplus
|
||
extern "C" {
|
||
#endif
|
||
|
||
/* ---- DLL 导出 / 导入宏 ---- */
|
||
#if defined(_WIN32)
|
||
#ifdef DSTALK_BUILD_DLL
|
||
#define DSTALK_API __declspec(dllexport)
|
||
#else
|
||
#define DSTALK_API __declspec(dllimport)
|
||
#endif
|
||
#else
|
||
#define DSTALK_API __attribute__((visibility("default")))
|
||
#endif
|
||
|
||
/* ---- 初始化和配置 ---- */
|
||
DSTALK_API int dstalk_init(const char* config_path);
|
||
DSTALK_API void dstalk_destroy(void);
|
||
|
||
/* 在 init 之后可修改 API 参数 (init 也会从配置文件读取) */
|
||
DSTALK_API void dstalk_set_api_key(const char* api_key);
|
||
DSTALK_API void dstalk_set_base_url(const char* base_url);
|
||
DSTALK_API void dstalk_set_model(const char* model);
|
||
|
||
/* ---- AI 对话 ---- */
|
||
/* 同步对话: 发送 input,返回完整 AI 回复 (调用方通过 dstalk_free_string 释放) */
|
||
DSTALK_API int dstalk_chat(const char* input, char** output);
|
||
|
||
/* 流式对话: 每收到一个 token 调用回调,回调返回 0 继续,非 0 取消 */
|
||
typedef int (*dstalk_stream_cb)(const char* token, void* userdata);
|
||
DSTALK_API int dstalk_chat_stream(const char* input, dstalk_stream_cb cb, void* userdata);
|
||
|
||
/* 释放由 dstalk_chat / dstalk_file_read 分配的字符串 */
|
||
DSTALK_API void dstalk_free_string(char* str);
|
||
|
||
/* ---- 会话管理 ---- */
|
||
DSTALK_API void dstalk_session_clear(void); /* 清空对话历史 */
|
||
DSTALK_API int dstalk_session_save(const char* path); /* 保存会话到文件 */
|
||
DSTALK_API int dstalk_session_load(const char* path); /* 从文件恢复会话 */
|
||
|
||
/* ---- 文件操作 ---- */
|
||
DSTALK_API int dstalk_file_read(const char* path, char** content);
|
||
DSTALK_API int dstalk_file_write(const char* path, const char* content);
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|
||
|
||
#endif /* DSTALK_API_H */
|