@opentiny/tiny-robot-kit 0.4.0-alpha.0 → 0.4.0-alpha.10
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 +443 -178
- package/dist/index.d.ts +443 -178
- package/dist/index.js +3 -2
- package/dist/index.mjs +3 -2
- package/package.json +5 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Ref,
|
|
1
|
+
import { Ref, ComputedRef } from 'vue';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 模型Provider基类
|
|
@@ -38,21 +38,40 @@ declare abstract class BaseModelProvider {
|
|
|
38
38
|
protected validateRequest(request: ChatCompletionRequest): void;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
41
42
|
/**
|
|
42
43
|
* 消息角色类型
|
|
43
44
|
*/
|
|
44
45
|
type MessageRole = 'system' | 'user' | 'assistant';
|
|
46
|
+
interface ToolCall {
|
|
47
|
+
index: number;
|
|
48
|
+
id: string;
|
|
49
|
+
type: 'function';
|
|
50
|
+
function: {
|
|
51
|
+
name: string;
|
|
52
|
+
arguments: string;
|
|
53
|
+
result?: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
interface MessageMetadata {
|
|
57
|
+
createdAt?: number;
|
|
58
|
+
updatedAt?: number;
|
|
59
|
+
id?: string;
|
|
60
|
+
model?: string;
|
|
61
|
+
[key: string]: any;
|
|
62
|
+
}
|
|
45
63
|
/**
|
|
46
64
|
* 聊天消息接口
|
|
47
65
|
*/
|
|
48
66
|
interface ChatMessage {
|
|
49
|
-
role:
|
|
50
|
-
content: string
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
[
|
|
67
|
+
role: string;
|
|
68
|
+
content: string;
|
|
69
|
+
reasoning_content?: string;
|
|
70
|
+
metadata?: MessageMetadata;
|
|
71
|
+
tool_calls?: ToolCall[];
|
|
72
|
+
tool_call_id?: string;
|
|
73
|
+
[key: string]: any;
|
|
74
|
+
[key: symbol]: any;
|
|
56
75
|
}
|
|
57
76
|
/**
|
|
58
77
|
* 聊天历史记录
|
|
@@ -198,6 +217,7 @@ interface StreamHandler {
|
|
|
198
217
|
*/
|
|
199
218
|
|
|
200
219
|
/**
|
|
220
|
+
* @deprecated
|
|
201
221
|
* AI客户端类
|
|
202
222
|
*/
|
|
203
223
|
declare class AIClient {
|
|
@@ -270,213 +290,458 @@ declare class OpenAIProvider extends BaseModelProvider {
|
|
|
270
290
|
updateConfig(config: AIModelConfig): void;
|
|
271
291
|
}
|
|
272
292
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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"
|
|
293
|
+
interface Tool {
|
|
294
|
+
type: 'function';
|
|
295
|
+
function: {
|
|
296
|
+
name: string;
|
|
297
|
+
description: string;
|
|
298
|
+
/**
|
|
299
|
+
* function 的输入参数,以 JSON Schema 对象描述
|
|
300
|
+
*/
|
|
301
|
+
parameters: any;
|
|
302
|
+
};
|
|
303
|
+
[key: string]: any;
|
|
310
304
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
305
|
+
interface MessageRequestBody {
|
|
306
|
+
messages: Partial<ChatMessage>[];
|
|
307
|
+
[key: string]: any;
|
|
308
|
+
}
|
|
309
|
+
type RequestState = 'idle' | 'processing' | 'completed' | 'aborted' | 'error';
|
|
310
|
+
type RequestProcessingState = 'requesting' | 'completing' | string;
|
|
311
|
+
interface Usage {
|
|
312
|
+
prompt_tokens: number;
|
|
313
|
+
completion_tokens: number;
|
|
314
|
+
total_tokens: number;
|
|
315
|
+
prompt_tokens_details?: {
|
|
316
|
+
cached_tokens: number;
|
|
317
|
+
};
|
|
318
|
+
prompt_cache_hit_tokens?: number;
|
|
319
|
+
prompt_cache_miss_tokens?: number;
|
|
320
|
+
}
|
|
321
|
+
interface Choice {
|
|
322
|
+
index: number;
|
|
323
|
+
message: {
|
|
324
|
+
role: string;
|
|
325
|
+
content?: string;
|
|
326
|
+
tool_calls?: ToolCall[];
|
|
327
|
+
[key: string]: any;
|
|
328
|
+
};
|
|
329
|
+
delta: undefined;
|
|
330
|
+
logprobs: any;
|
|
331
|
+
finish_reason: string | null;
|
|
332
|
+
}
|
|
333
|
+
interface DeltaChoice {
|
|
334
|
+
index: number;
|
|
335
|
+
message: undefined;
|
|
336
|
+
delta: {
|
|
337
|
+
role?: string;
|
|
338
|
+
content?: string;
|
|
339
|
+
tool_calls?: ToolCall[];
|
|
340
|
+
[key: string]: any;
|
|
341
|
+
};
|
|
342
|
+
logprobs: any;
|
|
343
|
+
finish_reason: string | null;
|
|
344
|
+
}
|
|
345
|
+
type CompletionChoice = Choice | DeltaChoice;
|
|
346
|
+
interface ChatCompletion {
|
|
347
|
+
id: string;
|
|
348
|
+
object: string;
|
|
349
|
+
created: number;
|
|
350
|
+
model: string;
|
|
351
|
+
system_fingerprint: string | null;
|
|
352
|
+
choices: CompletionChoice[];
|
|
353
|
+
usage?: Usage;
|
|
319
354
|
}
|
|
320
|
-
/**
|
|
321
|
-
* useMessage选项接口
|
|
322
|
-
*/
|
|
323
355
|
interface UseMessageOptions {
|
|
324
|
-
/** AI客户端实例 */
|
|
325
|
-
client: AIClient;
|
|
326
|
-
/** 是否默认使用流式响应 */
|
|
327
|
-
useStreamByDefault?: boolean;
|
|
328
|
-
/** 错误消息模板 */
|
|
329
|
-
errorMessage?: string;
|
|
330
|
-
/** 初始消息列表 */
|
|
331
356
|
initialMessages?: ChatMessage[];
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
357
|
+
/**
|
|
358
|
+
* 请求消息时,要包含的字段(白名单)。默认包含所有字段。
|
|
359
|
+
* 如果 `requestMessageFieldsExclude` 存在,会先取 `requestMessageFields` 中的字段,再排除 `requestMessageFieldsExclude` 中的字段
|
|
360
|
+
*/
|
|
361
|
+
requestMessageFields?: string[];
|
|
362
|
+
/**
|
|
363
|
+
* 请求消息时,要排除的字段(黑名单)。默认会排除 `state`、`metadata`、`loading` 字段(这几个字段是给UI展示用的)。
|
|
364
|
+
* 如果 `requestMessageFields` 存在,会先取 `requestMessageFields` 中的字段,再排除 `requestMessageFieldsExclude` 中的字段
|
|
365
|
+
*/
|
|
366
|
+
requestMessageFieldsExclude?: string[];
|
|
367
|
+
plugins?: UseMessagePlugin[];
|
|
368
|
+
responseProvider: <T = ChatCompletion>(requestBody: MessageRequestBody, abortSignal: AbortSignal) => Promise<T> | AsyncGenerator<T> | Promise<AsyncGenerator<T>>;
|
|
369
|
+
/**
|
|
370
|
+
* 全局的数据块处理钩子,在接收到每个响应数据块时触发。
|
|
371
|
+
* 注意:此钩子与插件中的 onCompletionChunk 有区别。
|
|
372
|
+
* 如果传入了此参数,默认的 chunk 处理逻辑不会自动执行,需要手动调用 runDefault 来执行默认处理逻辑。
|
|
373
|
+
*/
|
|
374
|
+
onCompletionChunk?: (context: BasePluginContext & {
|
|
375
|
+
currentMessage: ChatMessage;
|
|
376
|
+
choice: CompletionChoice;
|
|
377
|
+
chunk: ChatCompletion;
|
|
378
|
+
}, runDefault: () => void) => void;
|
|
339
379
|
}
|
|
340
|
-
/**
|
|
341
|
-
* useMessage返回值接口
|
|
342
|
-
*/
|
|
343
380
|
interface UseMessageReturn {
|
|
381
|
+
requestState: Ref<RequestState>;
|
|
382
|
+
processingState: Ref<RequestProcessingState | undefined>;
|
|
344
383
|
messages: Ref<ChatMessage[]>;
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
384
|
+
responseProvider: Ref<UseMessageOptions['responseProvider']>;
|
|
385
|
+
isProcessing: ComputedRef<boolean>;
|
|
386
|
+
sendMessage: (content: string) => Promise<void>;
|
|
387
|
+
send: (...msgs: ChatMessage[]) => Promise<void>;
|
|
388
|
+
abortRequest: () => Promise<void>;
|
|
389
|
+
}
|
|
390
|
+
interface BasePluginContext {
|
|
391
|
+
messages: ChatMessage[];
|
|
392
|
+
currentTurn: ChatMessage[];
|
|
393
|
+
requestState: RequestState;
|
|
394
|
+
processingState?: RequestProcessingState;
|
|
395
|
+
plugins: UseMessagePlugin[];
|
|
396
|
+
setRequestState: (state: RequestState, processingState?: RequestProcessingState) => void;
|
|
397
|
+
abortSignal: AbortSignal;
|
|
398
|
+
/**
|
|
399
|
+
* Custom context data that can be set by plugins
|
|
400
|
+
*/
|
|
401
|
+
customContext: Record<string, unknown>;
|
|
402
|
+
/**
|
|
403
|
+
* Set custom context data. Can be used to store plugin-specific data that needs to be shared across hooks.
|
|
404
|
+
*/
|
|
405
|
+
setCustomContext: (data: Record<string, unknown>) => void;
|
|
406
|
+
}
|
|
407
|
+
interface UseMessagePlugin {
|
|
408
|
+
/**
|
|
409
|
+
* 插件名称。
|
|
410
|
+
*/
|
|
411
|
+
name?: string;
|
|
412
|
+
/**
|
|
413
|
+
* 是否禁用插件。useMessage 可能会内置一些默认插件,如果需要禁用,可以设置为 true。
|
|
414
|
+
*/
|
|
415
|
+
disabled?: boolean | ((context: BasePluginContext) => boolean);
|
|
416
|
+
/**
|
|
417
|
+
* 一次对话回合(turn)开始钩子:用户消息入队后、正式发起请求之前触发。
|
|
418
|
+
* 按插件注册顺序串行执行,便于做有序初始化/校验;出错则中断流程。
|
|
419
|
+
*/
|
|
420
|
+
onTurnStart?: (context: BasePluginContext) => MaybePromise<void>;
|
|
421
|
+
/**
|
|
422
|
+
* 一次对话回合(turn)结束的生命周期钩子。
|
|
423
|
+
* 触发时机:本轮对话完成(成功、被中止)后
|
|
424
|
+
* 执行策略:按插件注册顺序串行执行,有错误则中断流程
|
|
425
|
+
*/
|
|
426
|
+
onTurnEnd?: (context: BasePluginContext) => MaybePromise<void>;
|
|
427
|
+
/**
|
|
428
|
+
* 请求开始前的生命周期钩子。
|
|
429
|
+
* 触发时机:已组装 requestBody,正式发起请求之前。
|
|
430
|
+
* 执行策略:按插件注册顺序串行执行,避免并发修改 requestBody 产生冲突。
|
|
431
|
+
* 用途:增补 tools、注入上下文参数、进行参数校验等。
|
|
432
|
+
*/
|
|
433
|
+
onBeforeRequest?: (context: BasePluginContext & {
|
|
434
|
+
requestBody: MessageRequestBody;
|
|
435
|
+
}) => MaybePromise<void>;
|
|
436
|
+
/**
|
|
437
|
+
* 请求完成后的生命周期钩子(如收到 AI 响应或需要处理 tool_calls 等)。
|
|
438
|
+
* 触发时机:本次请求(含流式)结束后。
|
|
439
|
+
* 执行策略:并行执行(Promise.all),各插件通过 appendMessage 追加消息。
|
|
440
|
+
*/
|
|
441
|
+
onAfterRequest?: (context: BasePluginContext & {
|
|
442
|
+
currentMessage: ChatMessage;
|
|
443
|
+
lastChoice?: CompletionChoice;
|
|
444
|
+
appendMessage: (message: ChatMessage | ChatMessage[]) => void;
|
|
445
|
+
requestNext: () => void;
|
|
446
|
+
}) => MaybePromise<void>;
|
|
447
|
+
/**
|
|
448
|
+
* 数据块处理钩子,在接收到每个响应数据块时触发。
|
|
449
|
+
* 无论是流式响应(多个增量数据块)还是非流式响应(单个完整数据块),都会触发此钩子。
|
|
450
|
+
* 用途:自定义增量合并、实时 UI 效果等。
|
|
451
|
+
*/
|
|
452
|
+
onCompletionChunk?: (context: BasePluginContext & {
|
|
453
|
+
currentMessage: ChatMessage;
|
|
454
|
+
choice?: CompletionChoice;
|
|
455
|
+
chunk: ChatCompletion;
|
|
456
|
+
}) => void;
|
|
457
|
+
onError?: (context: BasePluginContext & {
|
|
458
|
+
error: unknown;
|
|
459
|
+
}) => void;
|
|
460
|
+
onFinally?: (context: BasePluginContext) => void;
|
|
461
|
+
}
|
|
377
462
|
|
|
378
|
-
|
|
379
|
-
* 会话接口
|
|
380
|
-
*/
|
|
381
|
-
interface Conversation {
|
|
463
|
+
interface ConversationInfo {
|
|
382
464
|
/** 会话ID */
|
|
383
465
|
id: string;
|
|
384
466
|
/** 会话标题 */
|
|
385
|
-
title
|
|
467
|
+
title?: string;
|
|
386
468
|
/** 创建时间 */
|
|
387
469
|
createdAt: number;
|
|
388
470
|
/** 更新时间 */
|
|
389
471
|
updatedAt: number;
|
|
390
472
|
/** 自定义元数据 */
|
|
391
473
|
metadata?: Record<string, unknown>;
|
|
392
|
-
/** 会话消息 */
|
|
393
|
-
messages: ChatMessage[];
|
|
394
474
|
}
|
|
475
|
+
interface Conversation extends ConversationInfo {
|
|
476
|
+
/**
|
|
477
|
+
* Message engine instance created by useMessage.
|
|
478
|
+
*/
|
|
479
|
+
engine: UseMessageReturn;
|
|
480
|
+
}
|
|
481
|
+
interface UseConversationOptions {
|
|
482
|
+
/**
|
|
483
|
+
* Base useMessage options for all conversations.
|
|
484
|
+
* Per-conversation options passed to createConversation will be merged on top of this.
|
|
485
|
+
*/
|
|
486
|
+
useMessageOptions: UseMessageOptions;
|
|
487
|
+
/**
|
|
488
|
+
* Whether to automatically save messages when they are changed.
|
|
489
|
+
* @default false
|
|
490
|
+
*/
|
|
491
|
+
autoSaveMessages?: boolean;
|
|
492
|
+
/**
|
|
493
|
+
* Throttle time in milliseconds for auto-save operations.
|
|
494
|
+
* Ensures messages are saved at most once per this interval during streaming updates.
|
|
495
|
+
* Only effective when autoSaveMessages is true.
|
|
496
|
+
* @default 1000
|
|
497
|
+
*/
|
|
498
|
+
autoSaveThrottle?: number;
|
|
499
|
+
/**
|
|
500
|
+
* Optional storage strategy for conversations and messages.
|
|
501
|
+
* When provided, conversation list and messages can be loaded and persisted.
|
|
502
|
+
*/
|
|
503
|
+
storage?: ConversationStorageStrategy;
|
|
504
|
+
/**
|
|
505
|
+
* Called when the initial conversation list has been loaded from storage.
|
|
506
|
+
* Only fired when storage.loadConversations is available and has completed.
|
|
507
|
+
*/
|
|
508
|
+
onLoad?: (conversations: ConversationInfo[]) => void;
|
|
509
|
+
}
|
|
510
|
+
interface UseConversationReturn {
|
|
511
|
+
conversations: Ref<ConversationInfo[]>;
|
|
512
|
+
activeConversationId: Ref<string | null>;
|
|
513
|
+
activeConversation: ComputedRef<Conversation | null>;
|
|
514
|
+
createConversation: (params?: {
|
|
515
|
+
/** 会话ID,不提供则自动生成 */
|
|
516
|
+
id?: string;
|
|
517
|
+
/** 会话标题 */
|
|
518
|
+
title?: string;
|
|
519
|
+
/** 自定义元数据 */
|
|
520
|
+
metadata?: Record<string, unknown>;
|
|
521
|
+
/** 覆盖默认的消息选项 */
|
|
522
|
+
useMessageOptions?: Partial<UseMessageOptions>;
|
|
523
|
+
}) => Conversation;
|
|
524
|
+
switchConversation: (id: string) => Promise<Conversation | null>;
|
|
525
|
+
deleteConversation: (id: string) => Promise<void>;
|
|
526
|
+
clear: () => void;
|
|
527
|
+
updateConversationTitle: (id: string, title?: string) => void;
|
|
528
|
+
saveMessages: (id?: string) => void;
|
|
529
|
+
sendMessage: (content: string) => void;
|
|
530
|
+
abortActiveRequest: () => Promise<void>;
|
|
531
|
+
}
|
|
532
|
+
|
|
395
533
|
/**
|
|
396
534
|
* 存储策略接口
|
|
397
535
|
*/
|
|
398
536
|
interface ConversationStorageStrategy {
|
|
399
|
-
/**
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
loadConversations: () =>
|
|
537
|
+
/**
|
|
538
|
+
* Load all conversations (id and title only).
|
|
539
|
+
*/
|
|
540
|
+
loadConversations: () => MaybePromise<ConversationInfo[]>;
|
|
541
|
+
/**
|
|
542
|
+
* Load all messages for a given conversation.
|
|
543
|
+
*/
|
|
544
|
+
loadMessages: (conversationId: string) => MaybePromise<ChatMessage[]>;
|
|
545
|
+
/**
|
|
546
|
+
* Persist conversation metadata (create or update).
|
|
547
|
+
*/
|
|
548
|
+
saveConversation: (conversation: ConversationInfo) => MaybePromise<void>;
|
|
549
|
+
/**
|
|
550
|
+
* Persist messages for a given conversation.
|
|
551
|
+
*/
|
|
552
|
+
saveMessages: (conversationId: string, messages: ChatMessage[]) => MaybePromise<void>;
|
|
553
|
+
/**
|
|
554
|
+
* Optional method to delete a conversation and its messages.
|
|
555
|
+
*/
|
|
556
|
+
deleteConversation?: (conversationId: string) => MaybePromise<void>;
|
|
403
557
|
}
|
|
558
|
+
|
|
404
559
|
/**
|
|
405
560
|
* 本地存储策略
|
|
406
561
|
*/
|
|
407
562
|
declare class LocalStorageStrategy implements ConversationStorageStrategy {
|
|
408
563
|
private storageKey;
|
|
409
564
|
constructor(storageKey?: string);
|
|
410
|
-
|
|
411
|
-
loadConversations():
|
|
565
|
+
saveConversation(conversation: ConversationInfo): void;
|
|
566
|
+
loadConversations(): ConversationInfo[];
|
|
567
|
+
saveMessages(conversationId: string, messages: ChatMessage[]): void;
|
|
568
|
+
loadMessages(conversationId: string): ChatMessage[];
|
|
569
|
+
deleteConversation(conversationId: string): void;
|
|
412
570
|
}
|
|
571
|
+
|
|
413
572
|
/**
|
|
414
|
-
*
|
|
573
|
+
* IndexedDB 存储策略
|
|
415
574
|
*/
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
|
|
575
|
+
declare class IndexedDBStrategy implements ConversationStorageStrategy {
|
|
576
|
+
private dbName;
|
|
577
|
+
private dbVersion;
|
|
578
|
+
private db;
|
|
579
|
+
constructor(dbName?: string, dbVersion?: number);
|
|
580
|
+
/**
|
|
581
|
+
* 获取或初始化数据库连接
|
|
582
|
+
*/
|
|
583
|
+
private getDB;
|
|
584
|
+
/**
|
|
585
|
+
* 加载所有会话(只包含元数据)
|
|
586
|
+
*/
|
|
587
|
+
loadConversations(): Promise<ConversationInfo[]>;
|
|
588
|
+
/**
|
|
589
|
+
* 加载指定会话的所有消息
|
|
590
|
+
*/
|
|
591
|
+
loadMessages(conversationId: string): Promise<ChatMessage[]>;
|
|
592
|
+
/**
|
|
593
|
+
* 保存或更新会话元数据
|
|
594
|
+
*/
|
|
595
|
+
saveConversation(conversation: ConversationInfo): Promise<void>;
|
|
596
|
+
/**
|
|
597
|
+
* 保存指定会话的消息
|
|
598
|
+
*/
|
|
599
|
+
saveMessages(conversationId: string, messages: ChatMessage[]): Promise<void>;
|
|
600
|
+
/**
|
|
601
|
+
* 删除会话及其所有消息
|
|
602
|
+
*/
|
|
603
|
+
deleteConversation(conversationId: string): Promise<void>;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
interface LocalStorageConfig {
|
|
607
|
+
/** 存储键名 (default: 'tiny-robot-ai-conversations') */
|
|
608
|
+
key?: string;
|
|
609
|
+
}
|
|
610
|
+
interface IndexedDBConfig {
|
|
611
|
+
/** 数据库名称 (default: 'tiny-robot-ai-db') */
|
|
612
|
+
dbName?: string;
|
|
613
|
+
/** 数据库版本 (default: 1) */
|
|
614
|
+
dbVersion?: number;
|
|
423
615
|
}
|
|
424
|
-
type UseConversationEvents = UseMessageOptions['events'] & {
|
|
425
|
-
onLoaded?: (conversations: Conversation[]) => void;
|
|
426
|
-
};
|
|
427
616
|
/**
|
|
428
|
-
*
|
|
617
|
+
* LocalStorage 策略工厂函数
|
|
429
618
|
*/
|
|
430
|
-
|
|
431
|
-
/** AI客户端实例 */
|
|
432
|
-
client: AIClient;
|
|
433
|
-
/** 存储策略 */
|
|
434
|
-
storage?: ConversationStorageStrategy;
|
|
435
|
-
/** 是否自动保存 */
|
|
436
|
-
autoSave?: boolean;
|
|
437
|
-
/** 是否允许空会话 */
|
|
438
|
-
allowEmpty?: boolean;
|
|
439
|
-
/** 是否默认使用流式响应 */
|
|
440
|
-
useStreamByDefault?: boolean;
|
|
441
|
-
/** 错误消息模板 */
|
|
442
|
-
errorMessage?: string;
|
|
443
|
-
/** 事件回调 */
|
|
444
|
-
events?: UseConversationEvents;
|
|
445
|
-
}
|
|
619
|
+
declare function localStorageStrategyFactory(config?: LocalStorageConfig): ConversationStorageStrategy;
|
|
446
620
|
/**
|
|
447
|
-
*
|
|
621
|
+
* IndexedDB 策略工厂函数
|
|
448
622
|
*/
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
623
|
+
declare function indexedDBStorageStrategyFactory(config?: IndexedDBConfig): ConversationStorageStrategy;
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* 工具函数模块
|
|
627
|
+
* 提供一些实用的辅助函数
|
|
628
|
+
*/
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* 处理SSE流式响应
|
|
632
|
+
* @param response fetch响应对象
|
|
633
|
+
* @param handler 流处理器
|
|
634
|
+
*/
|
|
635
|
+
declare function handleSSEStream(response: Response, handler: StreamHandler, signal?: AbortSignal): Promise<void>;
|
|
636
|
+
/**
|
|
637
|
+
* 格式化消息
|
|
638
|
+
* 将各种格式的消息转换为标准的ChatMessage格式
|
|
639
|
+
* @param messages 消息数组
|
|
640
|
+
* @returns 标准格式的消息数组
|
|
641
|
+
*/
|
|
642
|
+
declare function formatMessages(messages: Array<ChatMessage | string>): ChatMessage[];
|
|
643
|
+
/**
|
|
644
|
+
* 从响应中提取文本内容
|
|
645
|
+
* @param response 聊天完成响应
|
|
646
|
+
* @returns 文本内容
|
|
647
|
+
*/
|
|
648
|
+
declare function extractTextFromResponse(response: ChatCompletionResponse): string;
|
|
649
|
+
/**
|
|
650
|
+
* 将 SSE 流转换为异步生成器。
|
|
651
|
+
* 将服务器发送事件(SSE)流式响应转换为异步生成器,逐个产出解析后的数据
|
|
476
652
|
*
|
|
477
|
-
*
|
|
478
|
-
* @
|
|
653
|
+
* 当取消信号被触发时,会抛出 name 为 'AbortError' 的错误
|
|
654
|
+
* @param response fetch 响应对象
|
|
655
|
+
* @param options 配置选项
|
|
656
|
+
* @param options.signal 可选的取消信号,用于中断流处理
|
|
657
|
+
* @returns 异步生成器,产出类型为 T 的数据
|
|
658
|
+
* @template T 生成器产出的数据类型,默认为 any
|
|
659
|
+
*/
|
|
660
|
+
declare function sseStreamToGenerator<T = any>(response: Response, options?: {
|
|
661
|
+
signal?: AbortSignal;
|
|
662
|
+
}): AsyncGenerator<T, void, unknown>;
|
|
663
|
+
|
|
664
|
+
declare const useConversation: (options: UseConversationOptions) => UseConversationReturn;
|
|
665
|
+
|
|
666
|
+
declare const fallbackRolePlugin: (options?: UseMessagePlugin & {
|
|
667
|
+
fallbackRole?: string;
|
|
668
|
+
}) => UseMessagePlugin;
|
|
669
|
+
|
|
670
|
+
declare const lengthPlugin: (options?: UseMessagePlugin & {
|
|
671
|
+
continueContent?: string;
|
|
672
|
+
}) => UseMessagePlugin;
|
|
673
|
+
|
|
674
|
+
declare const thinkingPlugin: (options?: UseMessagePlugin) => UseMessagePlugin;
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* 消息排除模式:从 messages 数组中直接移除消息。
|
|
678
|
+
* 使用此模式时,消息会被完全从数组中移除,不会保留在 messages 中。
|
|
479
679
|
*/
|
|
480
|
-
declare
|
|
680
|
+
declare const EXCLUDE_MODE_REMOVE: "remove";
|
|
681
|
+
declare const toolPlugin: (options: UseMessagePlugin & {
|
|
682
|
+
/**
|
|
683
|
+
* 获取工具列表的函数。
|
|
684
|
+
*/
|
|
685
|
+
getTools: () => Promise<Tool[]>;
|
|
686
|
+
/**
|
|
687
|
+
* 在处理包含 tool_calls 的响应前调用。
|
|
688
|
+
*/
|
|
689
|
+
beforeCallTools?: (toolCalls: ToolCall[], context: BasePluginContext & {
|
|
690
|
+
currentMessage: ChatMessage;
|
|
691
|
+
}) => Promise<void>;
|
|
692
|
+
/**
|
|
693
|
+
* 执行单个工具调用并返回其文本结果的函数。
|
|
694
|
+
*/
|
|
695
|
+
callTool: (toolCall: ToolCall, context: BasePluginContext & {
|
|
696
|
+
currentMessage: ChatMessage;
|
|
697
|
+
}) => Promise<string | Record<string, any>> | AsyncGenerator<string | Record<string, any>>;
|
|
698
|
+
/**
|
|
699
|
+
* 工具调用开始时的回调函数。
|
|
700
|
+
* 触发时机:工具消息已创建并追加后,调用 callTool 之前触发。
|
|
701
|
+
* @param toolCall - 工具调用对象
|
|
702
|
+
* @param context - 插件上下文,包含当前工具消息
|
|
703
|
+
*/
|
|
704
|
+
onToolCallStart?: (toolCall: ToolCall, context: BasePluginContext & {
|
|
705
|
+
primaryMessage: ChatMessage;
|
|
706
|
+
toolMessage: ChatMessage;
|
|
707
|
+
}) => void;
|
|
708
|
+
/**
|
|
709
|
+
* 工具调用结束时的回调函数。
|
|
710
|
+
* 触发时机:工具调用完成(成功、失败或取消)时触发。
|
|
711
|
+
* @param toolCall - 工具调用对象
|
|
712
|
+
* @param context - 插件上下文,包含当前工具消息、状态和错误信息
|
|
713
|
+
* @param context.status - 工具调用状态:'success' | 'failed' | 'cancelled'
|
|
714
|
+
* @param context.error - 当状态为 'failed' 或 'cancelled' 时,可能包含错误信息
|
|
715
|
+
*/
|
|
716
|
+
onToolCallEnd?: (toolCall: ToolCall, context: BasePluginContext & {
|
|
717
|
+
primaryMessage: ChatMessage;
|
|
718
|
+
toolMessage: ChatMessage;
|
|
719
|
+
status: "success" | "failed" | "cancelled";
|
|
720
|
+
error?: Error;
|
|
721
|
+
}) => void;
|
|
722
|
+
/**
|
|
723
|
+
* 当请求被中止时用于工具调用取消的消息内容。
|
|
724
|
+
*/
|
|
725
|
+
toolCallCancelledContent?: string;
|
|
726
|
+
/**
|
|
727
|
+
* 当工具调用执行失败(抛错或拒绝)时使用的消息内容。
|
|
728
|
+
*/
|
|
729
|
+
toolCallFailedContent?: string;
|
|
730
|
+
/**
|
|
731
|
+
* 是否在请求被中止时自动补充缺失的 tool 消息。
|
|
732
|
+
* 当 assistant 响应了 tool_calls 但未追加对应的 tool 消息时,
|
|
733
|
+
* 插件将自动补充"工具调用已取消"的 tool 消息。默认:false。
|
|
734
|
+
*/
|
|
735
|
+
autoFillMissingToolMessages?: boolean;
|
|
736
|
+
/**
|
|
737
|
+
* 是否在下一轮对话中,排除包含 tool_calls 的 assistant 消息和对应的 tool 消息,只保留结果。
|
|
738
|
+
* - 当为 `true` 时,这些消息会被标记为不发送,不会包含在下一次请求的 messages 中。
|
|
739
|
+
* - 当为 `'remove'` 时,这些消息会直接从 messages 数组中移除。
|
|
740
|
+
* 默认:false。
|
|
741
|
+
*/
|
|
742
|
+
excludeToolMessagesNextTurn?: boolean | typeof EXCLUDE_MODE_REMOVE;
|
|
743
|
+
}) => UseMessagePlugin;
|
|
744
|
+
|
|
745
|
+
declare const useMessage: (options: UseMessageOptions) => UseMessageReturn;
|
|
481
746
|
|
|
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
|
|
747
|
+
export { type AIAdapterError, AIClient, type AIModelConfig, type AIProvider, BaseModelProvider, type BasePluginContext, type ChatCompletion, type ChatCompletionOptions, type ChatCompletionRequest, type ChatCompletionResponse, type ChatCompletionResponseChoice, type ChatCompletionResponseMessage, type ChatCompletionResponseUsage, type ChatCompletionStreamResponse, type ChatCompletionStreamResponseChoice, type ChatCompletionStreamResponseDelta, type ChatHistory, type ChatMessage, type Choice, type CompletionChoice, type Conversation, type ConversationInfo, type ConversationStorageStrategy, type DeltaChoice, EXCLUDE_MODE_REMOVE, ErrorType, type IndexedDBConfig, IndexedDBStrategy, type LocalStorageConfig, LocalStorageStrategy, type MaybePromise, type MessageMetadata, type MessageRequestBody, type MessageRole, OpenAIProvider, type RequestProcessingState, type RequestState, StreamEventType, type StreamHandler, type Tool, type ToolCall, type Usage, type UseConversationOptions, type UseConversationReturn, type UseMessageOptions, type UseMessagePlugin, type UseMessageReturn, extractTextFromResponse, fallbackRolePlugin, formatMessages, handleSSEStream, indexedDBStorageStrategyFactory, lengthPlugin, localStorageStrategyFactory, sseStreamToGenerator, thinkingPlugin, toolPlugin, useConversation, useMessage };
|