@opentiny/tiny-robot-kit 0.4.0-alpha.0 → 0.4.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,482 +1,369 @@
1
- import { Ref, Reactive } from 'vue';
1
+ import { Ref, ComputedRef } from 'vue';
2
2
 
3
- /**
4
- * 模型Provider基类
5
- */
6
- declare abstract class BaseModelProvider {
7
- protected config: AIModelConfig;
8
- /**
9
- * @param config AI模型配置
10
- */
11
- constructor(config: AIModelConfig);
12
- /**
13
- * 发送聊天请求并获取响应
14
- * @param request 聊天请求参数
15
- * @returns 聊天响应
16
- */
17
- abstract chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse>;
18
- /**
19
- * 发送流式聊天请求并通过处理器处理响应
20
- * @param request 聊天请求参数
21
- * @param handler 流式响应处理器
22
- */
23
- abstract chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void>;
24
- /**
25
- * 更新配置
26
- * @param config 新的AI模型配置
27
- */
28
- updateConfig(config: AIModelConfig): void;
29
- /**
30
- * 获取当前配置
31
- * @returns AI模型配置
32
- */
33
- getConfig(): AIModelConfig;
34
- /**
35
- * 验证请求参数
36
- * @param request 聊天请求参数
37
- */
38
- protected validateRequest(request: ChatCompletionRequest): void;
39
- }
3
+ type MaybePromise<T> = T | Promise<T>;
40
4
 
41
5
  /**
42
- * 消息角色类型
43
- */
44
- type MessageRole = 'system' | 'user' | 'assistant';
45
- /**
46
- * 聊天消息接口
47
- */
48
- interface ChatMessage {
49
- role: MessageRole;
50
- content: string | {
51
- type: string;
52
- [x: string]: unknown;
53
- }[];
54
- name?: string;
55
- [x: string]: unknown;
56
- }
57
- /**
58
- * 聊天历史记录
59
- */
60
- type ChatHistory = ChatMessage[];
61
- /**
62
- * 聊天完成请求选项
6
+ * 工具函数模块
7
+ * 提供一些实用的辅助函数
63
8
  */
64
- interface ChatCompletionOptions {
65
- model?: string;
66
- temperature?: number;
67
- top_p?: number;
68
- n?: number;
69
- stream?: boolean;
70
- max_tokens?: number;
9
+
10
+ declare function createSSEStreamGenerator<T = any>(response: Response, options?: {
71
11
  signal?: AbortSignal;
12
+ }): AsyncGenerator<T, void, unknown>;
13
+
14
+ interface Tool {
15
+ type: 'function';
16
+ function: {
17
+ name: string;
18
+ description: string;
19
+ /**
20
+ * function 的输入参数,以 JSON Schema 对象描述
21
+ */
22
+ parameters: any;
23
+ };
24
+ [key: string]: any;
72
25
  }
73
- /**
74
- * 聊天完成请求参数
75
- */
76
- interface ChatCompletionRequest {
77
- messages: ChatMessage[];
78
- options?: ChatCompletionOptions;
26
+ interface MessageMetadata {
27
+ createdAt?: number;
28
+ updatedAt?: number;
29
+ id?: string;
30
+ model?: string;
31
+ [key: string]: any;
79
32
  }
80
- /**
81
- * 聊天完成响应消息
82
- */
83
- interface ChatCompletionResponseMessage {
84
- role: MessageRole;
33
+ interface ChatMessage {
34
+ role: string;
85
35
  content: string;
86
- [x: string]: unknown;
36
+ metadata?: MessageMetadata;
37
+ tool_calls?: ToolCall[];
38
+ tool_call_id?: string;
39
+ [key: string]: any;
40
+ [key: symbol]: any;
87
41
  }
88
- /**
89
- * 聊天完成响应选择
90
- */
91
- interface ChatCompletionResponseChoice {
42
+ interface MessageRequestBody {
43
+ messages: Partial<ChatMessage>[];
44
+ [key: string]: any;
45
+ }
46
+ type RequestState = 'idle' | 'processing' | 'completed' | 'aborted' | 'error';
47
+ type RequestProcessingState = 'requesting' | 'completing' | string;
48
+ interface ToolCall {
92
49
  index: number;
93
- message: ChatCompletionResponseMessage;
94
- finish_reason: string;
50
+ id: string;
51
+ type: 'function';
52
+ function: {
53
+ name: string;
54
+ arguments: string;
55
+ result?: string;
56
+ };
95
57
  }
96
- /**
97
- * 聊天完成响应使用情况
98
- */
99
- interface ChatCompletionResponseUsage {
58
+ interface Usage {
100
59
  prompt_tokens: number;
101
60
  completion_tokens: number;
102
61
  total_tokens: number;
62
+ prompt_tokens_details?: {
63
+ cached_tokens: number;
64
+ };
65
+ prompt_cache_hit_tokens?: number;
66
+ prompt_cache_miss_tokens?: number;
103
67
  }
104
- /**
105
- * 聊天完成响应
106
- */
107
- interface ChatCompletionResponse {
108
- id: string;
109
- object: string;
110
- created: number;
111
- model: string;
112
- choices: ChatCompletionResponseChoice[];
113
- usage: ChatCompletionResponseUsage;
114
- }
115
- /**
116
- * 流式聊天完成响应增量
117
- */
118
- interface ChatCompletionStreamResponseDelta {
119
- content?: string;
120
- role?: MessageRole;
121
- [x: string]: unknown;
68
+ interface Choice {
69
+ index: number;
70
+ message: {
71
+ role: string;
72
+ content?: string;
73
+ tool_calls?: ToolCall[];
74
+ [key: string]: any;
75
+ };
76
+ delta: undefined;
77
+ logprobs: any;
78
+ finish_reason: string | null;
122
79
  }
123
- /**
124
- * 流式聊天完成响应选择
125
- */
126
- interface ChatCompletionStreamResponseChoice {
80
+ interface DeltaChoice {
127
81
  index: number;
128
- delta: ChatCompletionStreamResponseDelta;
82
+ message: undefined;
83
+ delta: {
84
+ role?: string;
85
+ content?: string;
86
+ tool_calls?: ToolCall[];
87
+ [key: string]: any;
88
+ };
89
+ logprobs: any;
129
90
  finish_reason: string | null;
130
91
  }
131
- /**
132
- * 流式聊天完成响应
133
- */
134
- interface ChatCompletionStreamResponse {
92
+ type CompletionChoice = Choice | DeltaChoice;
93
+ interface ChatCompletion {
135
94
  id: string;
136
95
  object: string;
137
96
  created: number;
138
97
  model: string;
139
- choices: ChatCompletionStreamResponseChoice[];
98
+ system_fingerprint: string | null;
99
+ choices: CompletionChoice[];
100
+ usage?: Usage;
140
101
  }
141
- /**
142
- * AI模型提供商类型
143
- */
144
- type AIProvider = 'openai' | 'deepseek' | 'custom';
145
- /**
146
- * AI模型配置接口
147
- */
148
- interface AIModelConfig {
149
- provider: AIProvider;
150
- providerImplementation?: BaseModelProvider;
151
- apiKey?: string;
152
- apiUrl?: string;
153
- apiVersion?: string;
154
- defaultModel?: string;
155
- defaultOptions?: ChatCompletionOptions;
156
- }
157
- /**
158
- * 错误类型
159
- */
160
- declare enum ErrorType {
161
- NETWORK_ERROR = "network_error",
162
- AUTHENTICATION_ERROR = "authentication_error",
163
- RATE_LIMIT_ERROR = "rate_limit_error",
164
- SERVER_ERROR = "server_error",
165
- MODEL_ERROR = "model_error",
166
- TIMEOUT_ERROR = "timeout_error",
167
- UNKNOWN_ERROR = "unknown_error"
168
- }
169
- /**
170
- * AI适配器错误
171
- */
172
- interface AIAdapterError {
173
- type: ErrorType;
174
- message: string;
175
- statusCode?: number;
176
- originalError?: object;
177
- }
178
- /**
179
- * 流式响应事件类型
180
- */
181
- declare enum StreamEventType {
182
- DATA = "data",
183
- ERROR = "error",
184
- DONE = "done"
102
+ interface UseMessageOptions {
103
+ initialMessages?: ChatMessage[];
104
+ requestMessageFields?: (keyof ChatMessage)[];
105
+ plugins?: UseMessagePlugin[];
106
+ responseProvider: <T = ChatCompletion>(requestBody: MessageRequestBody, abortSignal: AbortSignal) => Promise<T> | AsyncGenerator<T> | Promise<AsyncGenerator<T>>;
107
+ /**
108
+ * 全局的数据块处理钩子,在接收到每个响应数据块时触发。
109
+ * 注意:此钩子与插件中的 onCompletionChunk 有区别。
110
+ * 如果传入了此参数,默认的 chunk 处理逻辑不会自动执行,需要手动调用 runDefault 来执行默认处理逻辑。
111
+ */
112
+ onCompletionChunk?: (context: BasePluginContext & {
113
+ currentMessage: ChatMessage;
114
+ chunk: ChatCompletion;
115
+ }, runDefault: () => void) => void;
185
116
  }
186
- /**
187
- * 流式响应处理器
188
- */
189
- interface StreamHandler {
190
- onData: (data: ChatCompletionStreamResponse) => void;
191
- onError: (error: AIAdapterError) => void;
192
- onDone: (finishReason?: string) => void;
117
+ interface UseMessageReturn {
118
+ requestState: Ref<RequestState>;
119
+ processingState: Ref<RequestProcessingState | undefined>;
120
+ messages: Ref<ChatMessage[]>;
121
+ responseProvider: Ref<UseMessageOptions['responseProvider']>;
122
+ isProcessing: ComputedRef<boolean>;
123
+ sendMessage: (content: string) => Promise<void>;
124
+ send: (...msgs: ChatMessage[]) => Promise<void>;
125
+ abortRequest: () => Promise<void>;
193
126
  }
194
-
195
- /**
196
- * AI客户端类
197
- * 负责根据配置选择合适的提供商并处理请求
198
- */
199
-
200
- /**
201
- * AI客户端类
202
- */
203
- declare class AIClient {
204
- private provider;
205
- private config;
127
+ interface BasePluginContext {
128
+ messages: ChatMessage[];
129
+ currentTurn: ChatMessage[];
130
+ requestState: RequestState;
131
+ processingState?: RequestProcessingState;
132
+ requestMessageFields: (keyof ChatMessage)[];
133
+ plugins: UseMessagePlugin[];
134
+ setRequestState: (state: RequestState, processingState?: RequestProcessingState) => void;
135
+ abortSignal: AbortSignal;
206
136
  /**
207
- * 构造函数
208
- * @param config AI模型配置
137
+ * Custom context data that can be set by plugins
209
138
  */
210
- constructor(config: AIModelConfig);
139
+ customContext: Record<string, unknown>;
211
140
  /**
212
- * 创建提供商实例
213
- * @param config AI模型配置
214
- * @returns 提供商实例
141
+ * Set custom context data. Can be used to store plugin-specific data that needs to be shared across hooks.
215
142
  */
216
- private createProvider;
143
+ setCustomContext: (data: Record<string, unknown>) => void;
144
+ }
145
+ interface UseMessagePlugin {
217
146
  /**
218
- * 发送聊天请求并获取响应
219
- * @param request 聊天请求参数
220
- * @returns 聊天响应
147
+ * 插件名称。
221
148
  */
222
- chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse>;
149
+ name?: string;
223
150
  /**
224
- * 发送流式聊天请求并通过处理器处理响应
225
- * @param request 聊天请求参数
226
- * @param handler 流式响应处理器
151
+ * 是否禁用插件。useMessage 可能会内置一些默认插件,如果需要禁用,可以设置为 true。
227
152
  */
228
- chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void>;
153
+ disabled?: boolean;
229
154
  /**
230
- * 获取当前配置
231
- * @returns AI模型配置
155
+ * 一次对话回合(turn)开始钩子:用户消息入队后、正式发起请求之前触发。
156
+ * 按插件注册顺序串行执行,便于做有序初始化/校验;出错则中断流程。
232
157
  */
233
- getConfig(): AIModelConfig;
158
+ onTurnStart?: (context: BasePluginContext) => MaybePromise<void>;
234
159
  /**
235
- * 更新配置
236
- * @param config 新的AI模型配置
160
+ * 一次对话回合(turn)结束的生命周期钩子。
161
+ * 触发时机:本轮对话完成(成功、被中止)后
162
+ * 执行策略:按插件注册顺序串行执行,有错误则中断流程
237
163
  */
238
- updateConfig(config: Partial<AIModelConfig>): void;
239
- }
240
-
241
- /**
242
- * OpenAI提供商
243
- * 用于与OpenAI API进行交互
244
- */
245
-
246
- declare class OpenAIProvider extends BaseModelProvider {
247
- private baseURL;
248
- private apiKey;
249
- private defaultModel;
164
+ onTurnEnd?: (context: BasePluginContext) => MaybePromise<void>;
250
165
  /**
251
- * @param config AI模型配置
166
+ * 请求开始前的生命周期钩子。
167
+ * 触发时机:已组装 requestBody,正式发起请求之前。
168
+ * 执行策略:按插件注册顺序串行执行,避免并发修改 requestBody 产生冲突。
169
+ * 用途:增补 tools、注入上下文参数、进行参数校验等。
252
170
  */
253
- constructor(config: AIModelConfig);
171
+ onBeforeRequest?: (context: BasePluginContext & {
172
+ requestBody: MessageRequestBody;
173
+ }) => MaybePromise<void>;
254
174
  /**
255
- * 发送聊天请求并获取响应
256
- * @param request 聊天请求参数
257
- * @returns 聊天响应
175
+ * 请求完成后的生命周期钩子(如收到 AI 响应或需要处理 tool_calls 等)。
176
+ * 触发时机:本次请求(含流式)结束后。
177
+ * 执行策略:并行执行(Promise.all),各插件通过 appendMessage 追加消息。
258
178
  */
259
- chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse>;
179
+ onAfterRequest?: (context: BasePluginContext & {
180
+ currentMessage: ChatMessage;
181
+ lastChoice?: CompletionChoice;
182
+ appendMessage: (message: ChatMessage | ChatMessage[]) => void;
183
+ requestNext: () => void;
184
+ }) => MaybePromise<void>;
260
185
  /**
261
- * 发送流式聊天请求并通过处理器处理响应
262
- * @param request 聊天请求参数
263
- * @param handler 流式响应处理器
186
+ * 数据块处理钩子,在接收到每个响应数据块时触发。
187
+ * 无论是流式响应(多个增量数据块)还是非流式响应(单个完整数据块),都会触发此钩子。
188
+ * 用途:自定义增量合并、实时 UI 效果等。
264
189
  */
265
- chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void>;
266
- /**
267
- * 更新配置
268
- * @param config 新的AI模型配置
269
- */
270
- updateConfig(config: AIModelConfig): void;
190
+ onCompletionChunk?: (context: BasePluginContext & {
191
+ currentMessage: ChatMessage;
192
+ choice?: CompletionChoice;
193
+ chunk: ChatCompletion;
194
+ }) => void;
195
+ onError?: (context: BasePluginContext & {
196
+ error: unknown;
197
+ }) => void;
271
198
  }
272
199
 
273
- /**
274
- * 工具函数模块
275
- * 提供一些实用的辅助函数
276
- */
277
-
278
- /**
279
- * 处理SSE流式响应
280
- * @param response fetch响应对象
281
- * @param handler 流处理器
282
- */
283
- declare function handleSSEStream(response: Response, handler: StreamHandler, signal?: AbortSignal): Promise<void>;
284
- /**
285
- * 格式化消息
286
- * 将各种格式的消息转换为标准的ChatMessage格式
287
- * @param messages 消息数组
288
- * @returns 标准格式的消息数组
289
- */
290
- declare function formatMessages(messages: Array<ChatMessage | string>): ChatMessage[];
291
- /**
292
- * 从响应中提取文本内容
293
- * @param response 聊天完成响应
294
- * @returns 文本内容
295
- */
296
- declare function extractTextFromResponse(response: ChatCompletionResponse): string;
297
-
298
- /**
299
- * useMessage composable
300
- * 提供消息管理和状态控制功能
301
- */
302
-
303
- declare enum STATUS {
304
- INIT = "init",// 初始状态
305
- PROCESSING = "processing",// AI请求正在处理中, 还未响应,显示加载动画
306
- STREAMING = "streaming",// 流式响应中分块数据返回中
307
- FINISHED = "finished",// AI请求已完成
308
- ABORTED = "aborted",// 用户中止请求
309
- ERROR = "error"
310
- }
311
- declare const GeneratingStatus: STATUS[];
312
- declare const FinalStatus: STATUS[];
313
- /**
314
- * 消息状态接口
315
- */
316
- interface MessageState {
317
- status: STATUS;
318
- errorMsg: string | null;
319
- }
320
- /**
321
- * useMessage选项接口
322
- */
323
- interface UseMessageOptions {
324
- /** AI客户端实例 */
325
- client: AIClient;
326
- /** 是否默认使用流式响应 */
327
- useStreamByDefault?: boolean;
328
- /** 错误消息模板 */
329
- errorMessage?: string;
330
- /** 初始消息列表 */
331
- initialMessages?: ChatMessage[];
332
- events?: {
333
- onReceiveData?: <T = any>(data: T, messages: Ref<ChatMessage[]>, preventDefault: () => void) => void;
334
- onFinish?: (finishReason: string | undefined, context: {
335
- messages: Ref<ChatMessage[]>;
336
- messageState: Reactive<MessageState>;
337
- }, preventDefault: () => void) => void;
338
- };
339
- }
340
- /**
341
- * useMessage返回值接口
342
- */
343
- interface UseMessageReturn {
344
- messages: Ref<ChatMessage[]>;
345
- /** 消息状态 */
346
- messageState: Reactive<MessageState>;
347
- /** 输入消息 */
348
- inputMessage: Ref<string>;
349
- /** 是否使用流式响应 */
350
- useStream: Ref<boolean>;
351
- /** 发送消息 */
352
- sendMessage: (content?: ChatMessage['content'], clearInput?: boolean) => Promise<void>;
353
- /** 手动执行addMessage添加消息后,可以执行send发送消息 */
354
- send: () => Promise<void>;
355
- /** 清空消息 */
356
- clearMessages: () => void;
357
- /** 添加消息 */
358
- addMessage: (message: ChatMessage | ChatMessage[]) => void;
359
- /** 中止请求 */
360
- abortRequest: () => void;
361
- /** 重试请求 */
362
- retryRequest: (msgIndex: number) => Promise<void>;
363
- }
364
- /**
365
- * useMessage composable
366
- * 提供消息管理和状态控制功能
367
- *
368
- * @param options useMessage选项
369
- * @returns UseMessageReturn
370
- */
371
- declare function useMessage(options: UseMessageOptions): UseMessageReturn;
372
-
373
- /**
374
- * useConversation composable
375
- * 提供会话管理和持久化功能
376
- */
377
-
378
- /**
379
- * 会话接口
380
- */
381
- interface Conversation {
200
+ interface ConversationInfo {
382
201
  /** 会话ID */
383
202
  id: string;
384
203
  /** 会话标题 */
385
- title: string;
204
+ title?: string;
386
205
  /** 创建时间 */
387
206
  createdAt: number;
388
207
  /** 更新时间 */
389
208
  updatedAt: number;
390
209
  /** 自定义元数据 */
391
210
  metadata?: Record<string, unknown>;
392
- /** 会话消息 */
393
- messages: ChatMessage[];
394
- }
395
- /**
396
- * 存储策略接口
397
- */
398
- interface ConversationStorageStrategy {
399
- /** 保存会话列表 */
400
- saveConversations: (conversations: Conversation[]) => Promise<void> | void;
401
- /** 加载会话列表 */
402
- loadConversations: () => Promise<Conversation[]> | Conversation[];
403
211
  }
404
- /**
405
- * 本地存储策略
406
- */
407
- declare class LocalStorageStrategy implements ConversationStorageStrategy {
408
- private storageKey;
409
- constructor(storageKey?: string);
410
- saveConversations(conversations: Conversation[]): void;
411
- loadConversations(): Conversation[];
212
+ interface Conversation extends ConversationInfo {
213
+ /**
214
+ * Message engine instance created by useMessage.
215
+ */
216
+ engine: UseMessageReturn;
412
217
  }
413
- /**
414
- * 会话状态接口
415
- */
416
- interface ConversationState {
417
- /** 会话列表 */
418
- conversations: Conversation[];
419
- /** 当前会话ID */
420
- currentId: string | null;
421
- /** 是否正在加载 */
422
- loading: boolean;
218
+ interface ConversationStorageStrategy {
219
+ /**
220
+ * Load all conversations (id and title only).
221
+ */
222
+ loadConversations?: () => MaybePromise<ConversationInfo[]>;
223
+ /**
224
+ * Load all messages for a given conversation.
225
+ */
226
+ loadMessages?: (conversationId: string) => MaybePromise<ChatMessage[]>;
227
+ /**
228
+ * Persist conversation metadata (create or update).
229
+ */
230
+ saveConversation?: (conversation: ConversationInfo) => MaybePromise<void>;
231
+ /**
232
+ * Persist messages for a given conversation.
233
+ */
234
+ saveMessages?: (conversationId: string, messages: ChatMessage[]) => MaybePromise<void>;
235
+ /**
236
+ * Optional method to delete a conversation and its messages.
237
+ */
238
+ deleteConversation?: (conversationId: string) => MaybePromise<void>;
423
239
  }
424
- type UseConversationEvents = UseMessageOptions['events'] & {
425
- onLoaded?: (conversations: Conversation[]) => void;
426
- };
427
- /**
428
- * useConversation选项接口
429
- */
430
240
  interface UseConversationOptions {
431
- /** AI客户端实例 */
432
- client: AIClient;
433
- /** 存储策略 */
241
+ /**
242
+ * Base useMessage options for all conversations.
243
+ * Per-conversation options passed to createConversation will be merged on top of this.
244
+ */
245
+ useMessageOptions: UseMessageOptions;
246
+ /**
247
+ * Whether to automatically save messages when they are changed.
248
+ * @default false
249
+ */
250
+ autoSaveMessages?: boolean;
251
+ /**
252
+ * Throttle time in milliseconds for auto-save operations.
253
+ * Ensures messages are saved at most once per this interval during streaming updates.
254
+ * Only effective when autoSaveMessages is true.
255
+ * @default 1000
256
+ */
257
+ autoSaveThrottle?: number;
258
+ /**
259
+ * Optional storage strategy for conversations and messages.
260
+ * When provided, conversation list and messages can be loaded and persisted.
261
+ */
434
262
  storage?: ConversationStorageStrategy;
435
- /** 是否自动保存 */
436
- autoSave?: boolean;
437
- /** 是否允许空会话 */
438
- allowEmpty?: boolean;
439
- /** 是否默认使用流式响应 */
440
- useStreamByDefault?: boolean;
441
- /** 错误消息模板 */
442
- errorMessage?: string;
443
- /** 事件回调 */
444
- events?: UseConversationEvents;
445
263
  }
446
- /**
447
- * useConversation返回值接口
448
- */
449
264
  interface UseConversationReturn {
450
- /** 会话状态 */
451
- state: ConversationState;
452
- /** 消息管理 */
453
- messageManager: UseMessageReturn;
454
- /** 创建新会话 */
455
- createConversation: (title?: string, metadata?: Record<string, unknown>) => string;
456
- /** 切换会话 */
457
- switchConversation: (id: string) => void;
458
- /** 删除会话 */
459
- deleteConversation: (id: string) => void;
460
- /** 更新会话标题 */
461
- updateTitle: (id: string, title: string) => void;
462
- /** 更新会话元数据 */
463
- updateMetadata: (id: string, metadata: Record<string, unknown>) => void;
464
- /** 保存会话 */
465
- saveConversations: () => Promise<void>;
466
- /** 加载会话 */
467
- loadConversations: () => Promise<void>;
468
- /** 生成会话标题 */
469
- generateTitle: (id: string) => Promise<string>;
470
- /** 获取当前会话 */
471
- getCurrentConversation: () => Conversation | null;
265
+ conversations: Ref<ConversationInfo[]>;
266
+ activeConversationId: Ref<string | null>;
267
+ activeConversation: ComputedRef<Conversation | null>;
268
+ createConversation: (params?: {
269
+ /** 会话ID,不提供则自动生成 */
270
+ id?: string;
271
+ /** 会话标题 */
272
+ title?: string;
273
+ /** 自定义元数据 */
274
+ metadata?: Record<string, unknown>;
275
+ /** 覆盖默认的消息选项 */
276
+ useMessageOptions?: Partial<UseMessageOptions>;
277
+ }) => Conversation;
278
+ switchConversation: (id: string) => Promise<Conversation | null>;
279
+ deleteConversation: (id: string) => Promise<void>;
280
+ updateConversationTitle: (id: string, title?: string) => void;
281
+ saveMessages: (id?: string) => void;
282
+ sendMessage: (content: string) => void;
283
+ abortActiveRequest: () => Promise<void>;
472
284
  }
285
+
286
+ declare const useConversation: (options: UseConversationOptions) => UseConversationReturn;
287
+
288
+ declare const fallbackRolePlugin: (options?: UseMessagePlugin & {
289
+ fallbackRole?: string;
290
+ }) => UseMessagePlugin;
291
+
292
+ declare const lengthPlugin: (options?: UseMessagePlugin & {
293
+ continueContent?: string;
294
+ }) => UseMessagePlugin;
295
+
296
+ declare const thinkingPlugin: (options?: UseMessagePlugin) => UseMessagePlugin;
297
+
473
298
  /**
474
- * useConversation composable
475
- * 提供会话管理和持久化功能
476
- *
477
- * @param options useConversation选项
478
- * @returns UseConversationReturn
299
+ * 消息排除模式:从 messages 数组中直接移除消息。
300
+ * 使用此模式时,消息会被完全从数组中移除,不会保留在 messages 中。
479
301
  */
480
- declare function useConversation(options: UseConversationOptions): UseConversationReturn;
302
+ declare const EXCLUDE_MODE_REMOVE: "remove";
303
+ declare const toolPlugin: (options: UseMessagePlugin & {
304
+ /**
305
+ * 获取工具列表的函数。
306
+ */
307
+ getTools: () => Promise<Tool[]>;
308
+ /**
309
+ * 在处理包含 tool_calls 的响应前调用。
310
+ */
311
+ beforeCallTools?: (toolCalls: ToolCall[], context: BasePluginContext & {
312
+ currentMessage: ChatMessage;
313
+ }) => Promise<void>;
314
+ /**
315
+ * 执行单个工具调用并返回其文本结果的函数。
316
+ */
317
+ callTool: (toolCall: ToolCall, context: BasePluginContext & {
318
+ currentMessage: ChatMessage;
319
+ }) => Promise<string | Record<string, any>> | AsyncGenerator<string | Record<string, any>>;
320
+ /**
321
+ * 工具调用开始时的回调函数。
322
+ * 触发时机:工具消息已创建并追加后,调用 callTool 之前触发。
323
+ * @param toolCall - 工具调用对象
324
+ * @param context - 插件上下文,包含当前工具消息
325
+ */
326
+ onToolCallStart?: (toolCall: ToolCall, context: BasePluginContext & {
327
+ primaryMessage: ChatMessage;
328
+ toolMessage: ChatMessage;
329
+ }) => void;
330
+ /**
331
+ * 工具调用结束时的回调函数。
332
+ * 触发时机:工具调用完成(成功、失败或取消)时触发。
333
+ * @param toolCall - 工具调用对象
334
+ * @param context - 插件上下文,包含当前工具消息、状态和错误信息
335
+ * @param context.status - 工具调用状态:'success' | 'failed' | 'cancelled'
336
+ * @param context.error - 当状态为 'failed' 或 'cancelled' 时,可能包含错误信息
337
+ */
338
+ onToolCallEnd?: (toolCall: ToolCall, context: BasePluginContext & {
339
+ primaryMessage: ChatMessage;
340
+ toolMessage: ChatMessage;
341
+ status: "success" | "failed" | "cancelled";
342
+ error?: Error;
343
+ }) => void;
344
+ /**
345
+ * 当请求被中止时用于工具调用取消的消息内容。
346
+ */
347
+ toolCallCancelledContent?: string;
348
+ /**
349
+ * 当工具调用执行失败(抛错或拒绝)时使用的消息内容。
350
+ */
351
+ toolCallFailedContent?: string;
352
+ /**
353
+ * 是否在请求被中止时自动补充缺失的 tool 消息。
354
+ * 当 assistant 响应了 tool_calls 但未追加对应的 tool 消息时,
355
+ * 插件将自动补充"工具调用已取消"的 tool 消息。默认:false。
356
+ */
357
+ autoFillMissingToolMessages?: boolean;
358
+ /**
359
+ * 是否在下一轮对话中,排除包含 tool_calls 的 assistant 消息和对应的 tool 消息,只保留结果。
360
+ * - 当为 `true` 时,这些消息会被标记为不发送,不会包含在下一次请求的 messages 中。
361
+ * - 当为 `'remove'` 时,这些消息会直接从 messages 数组中移除。
362
+ * 默认:false。
363
+ */
364
+ excludeToolMessagesNextTurn?: boolean | typeof EXCLUDE_MODE_REMOVE;
365
+ }) => UseMessagePlugin;
366
+
367
+ declare const useMessage: (options: UseMessageOptions) => UseMessageReturn;
481
368
 
482
- export { type AIAdapterError, AIClient, type AIModelConfig, type AIProvider, BaseModelProvider, type ChatCompletionOptions, type ChatCompletionRequest, type ChatCompletionResponse, type ChatCompletionResponseChoice, type ChatCompletionResponseMessage, type ChatCompletionResponseUsage, type ChatCompletionStreamResponse, type ChatCompletionStreamResponseChoice, type ChatCompletionStreamResponseDelta, type ChatHistory, type ChatMessage, type Conversation, type ConversationState, type ConversationStorageStrategy, ErrorType, FinalStatus, GeneratingStatus, LocalStorageStrategy, type MessageRole, type MessageState, OpenAIProvider, STATUS, StreamEventType, type StreamHandler, type UseConversationEvents, type UseConversationOptions, type UseConversationReturn, type UseMessageOptions, type UseMessageReturn, extractTextFromResponse, formatMessages, handleSSEStream, useConversation, useMessage };
369
+ export { type BasePluginContext, type ChatCompletion, type ChatMessage, type Choice, type CompletionChoice, type Conversation, type ConversationInfo, type ConversationStorageStrategy, type DeltaChoice, EXCLUDE_MODE_REMOVE, type MessageMetadata, type MessageRequestBody, type RequestProcessingState, type RequestState, type Tool, type ToolCall, type Usage, type UseConversationOptions, type UseConversationReturn, type UseMessageOptions, type UseMessagePlugin, type UseMessageReturn, createSSEStreamGenerator, fallbackRolePlugin, lengthPlugin, thinkingPlugin, toolPlugin, useConversation, useMessage };