@opentiny/tiny-robot-kit 0.4.0-beta.0 → 0.4.1-alpha.0
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/README.md +59 -262
- package/dist/chunk-EES4JUCD.mjs +1 -0
- package/dist/core.d.mts +281 -0
- package/dist/core.d.ts +281 -0
- package/dist/core.js +1 -0
- package/dist/core.mjs +1 -0
- package/dist/index.d.mts +36 -245
- package/dist/index.d.ts +36 -245
- package/dist/index.js +3 -3
- package/dist/index.mjs +3 -3
- package/dist/types-CePh-Jcx.d.mts +212 -0
- package/dist/types-CePh-Jcx.d.ts +212 -0
- package/package.json +47 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,216 +1,7 @@
|
|
|
1
|
+
import { a as AIModelConfig, c as ChatCompletionRequest, d as ChatCompletionResponse, o as StreamHandler, B as BaseModelProvider, l as ChatMessage, M as MaybePromise, T as ToolCall } from './types-CePh-Jcx.mjs';
|
|
2
|
+
export { A as AIAdapterError, b as AIProvider, C as ChatCompletionOptions, e as ChatCompletionResponseChoice, f as ChatCompletionResponseMessage, g as ChatCompletionResponseUsage, h as ChatCompletionStreamResponse, i as ChatCompletionStreamResponseChoice, j as ChatCompletionStreamResponseDelta, k as ChatHistory, E as ErrorType, m as MessageMetadata, n as MessageRole, S as StreamEventType } from './types-CePh-Jcx.mjs';
|
|
1
3
|
import { Ref, ComputedRef } from 'vue';
|
|
2
4
|
|
|
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
|
-
}
|
|
40
|
-
|
|
41
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
42
|
-
/**
|
|
43
|
-
* 消息角色类型
|
|
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
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* 聊天消息接口
|
|
65
|
-
*/
|
|
66
|
-
interface ChatMessage {
|
|
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;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* 聊天历史记录
|
|
78
|
-
*/
|
|
79
|
-
type ChatHistory = ChatMessage[];
|
|
80
|
-
/**
|
|
81
|
-
* 聊天完成请求选项
|
|
82
|
-
*/
|
|
83
|
-
interface ChatCompletionOptions {
|
|
84
|
-
model?: string;
|
|
85
|
-
temperature?: number;
|
|
86
|
-
top_p?: number;
|
|
87
|
-
n?: number;
|
|
88
|
-
stream?: boolean;
|
|
89
|
-
max_tokens?: number;
|
|
90
|
-
signal?: AbortSignal;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* 聊天完成请求参数
|
|
94
|
-
*/
|
|
95
|
-
interface ChatCompletionRequest {
|
|
96
|
-
messages: ChatMessage[];
|
|
97
|
-
options?: ChatCompletionOptions;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* 聊天完成响应消息
|
|
101
|
-
*/
|
|
102
|
-
interface ChatCompletionResponseMessage {
|
|
103
|
-
role: MessageRole;
|
|
104
|
-
content: string;
|
|
105
|
-
[x: string]: unknown;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* 聊天完成响应选择
|
|
109
|
-
*/
|
|
110
|
-
interface ChatCompletionResponseChoice {
|
|
111
|
-
index: number;
|
|
112
|
-
message: ChatCompletionResponseMessage;
|
|
113
|
-
finish_reason: string;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* 聊天完成响应使用情况
|
|
117
|
-
*/
|
|
118
|
-
interface ChatCompletionResponseUsage {
|
|
119
|
-
prompt_tokens: number;
|
|
120
|
-
completion_tokens: number;
|
|
121
|
-
total_tokens: number;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* 聊天完成响应
|
|
125
|
-
*/
|
|
126
|
-
interface ChatCompletionResponse {
|
|
127
|
-
id: string;
|
|
128
|
-
object: string;
|
|
129
|
-
created: number;
|
|
130
|
-
model: string;
|
|
131
|
-
choices: ChatCompletionResponseChoice[];
|
|
132
|
-
usage: ChatCompletionResponseUsage;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* 流式聊天完成响应增量
|
|
136
|
-
*/
|
|
137
|
-
interface ChatCompletionStreamResponseDelta {
|
|
138
|
-
content?: string;
|
|
139
|
-
role?: MessageRole;
|
|
140
|
-
[x: string]: unknown;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* 流式聊天完成响应选择
|
|
144
|
-
*/
|
|
145
|
-
interface ChatCompletionStreamResponseChoice {
|
|
146
|
-
index: number;
|
|
147
|
-
delta: ChatCompletionStreamResponseDelta;
|
|
148
|
-
finish_reason: string | null;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* 流式聊天完成响应
|
|
152
|
-
*/
|
|
153
|
-
interface ChatCompletionStreamResponse {
|
|
154
|
-
id: string;
|
|
155
|
-
object: string;
|
|
156
|
-
created: number;
|
|
157
|
-
model: string;
|
|
158
|
-
choices: ChatCompletionStreamResponseChoice[];
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* AI模型提供商类型
|
|
162
|
-
*/
|
|
163
|
-
type AIProvider = 'openai' | 'deepseek' | 'custom';
|
|
164
|
-
/**
|
|
165
|
-
* AI模型配置接口
|
|
166
|
-
*/
|
|
167
|
-
interface AIModelConfig {
|
|
168
|
-
provider: AIProvider;
|
|
169
|
-
providerImplementation?: BaseModelProvider;
|
|
170
|
-
apiKey?: string;
|
|
171
|
-
apiUrl?: string;
|
|
172
|
-
apiVersion?: string;
|
|
173
|
-
defaultModel?: string;
|
|
174
|
-
defaultOptions?: ChatCompletionOptions;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* 错误类型
|
|
178
|
-
*/
|
|
179
|
-
declare enum ErrorType {
|
|
180
|
-
NETWORK_ERROR = "network_error",
|
|
181
|
-
AUTHENTICATION_ERROR = "authentication_error",
|
|
182
|
-
RATE_LIMIT_ERROR = "rate_limit_error",
|
|
183
|
-
SERVER_ERROR = "server_error",
|
|
184
|
-
MODEL_ERROR = "model_error",
|
|
185
|
-
TIMEOUT_ERROR = "timeout_error",
|
|
186
|
-
UNKNOWN_ERROR = "unknown_error"
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* AI适配器错误
|
|
190
|
-
*/
|
|
191
|
-
interface AIAdapterError {
|
|
192
|
-
type: ErrorType;
|
|
193
|
-
message: string;
|
|
194
|
-
statusCode?: number;
|
|
195
|
-
originalError?: object;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* 流式响应事件类型
|
|
199
|
-
*/
|
|
200
|
-
declare enum StreamEventType {
|
|
201
|
-
DATA = "data",
|
|
202
|
-
ERROR = "error",
|
|
203
|
-
DONE = "done"
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* 流式响应处理器
|
|
207
|
-
*/
|
|
208
|
-
interface StreamHandler {
|
|
209
|
-
onData: (data: ChatCompletionStreamResponse) => void;
|
|
210
|
-
onError: (error: AIAdapterError) => void;
|
|
211
|
-
onDone: (finishReason?: string) => void;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
5
|
/**
|
|
215
6
|
* AI客户端类
|
|
216
7
|
* 负责根据配置选择合适的提供商并处理请求
|
|
@@ -294,11 +85,12 @@ interface Tool {
|
|
|
294
85
|
type: 'function';
|
|
295
86
|
function: {
|
|
296
87
|
name: string;
|
|
297
|
-
description
|
|
88
|
+
description?: string;
|
|
298
89
|
/**
|
|
299
90
|
* function 的输入参数,以 JSON Schema 对象描述
|
|
300
91
|
*/
|
|
301
|
-
parameters
|
|
92
|
+
parameters?: any;
|
|
93
|
+
inputSchema?: any;
|
|
302
94
|
};
|
|
303
95
|
[key: string]: any;
|
|
304
96
|
}
|
|
@@ -352,6 +144,7 @@ interface ChatCompletion {
|
|
|
352
144
|
choices: CompletionChoice[];
|
|
353
145
|
usage?: Usage;
|
|
354
146
|
}
|
|
147
|
+
type ResponseProvider<T = ChatCompletion> = (requestBody: MessageRequestBody, abortSignal: AbortSignal) => Promise<T> | AsyncGenerator<T> | Promise<AsyncGenerator<T>>;
|
|
355
148
|
interface UseMessageOptions {
|
|
356
149
|
initialMessages?: ChatMessage[];
|
|
357
150
|
/**
|
|
@@ -365,7 +158,7 @@ interface UseMessageOptions {
|
|
|
365
158
|
*/
|
|
366
159
|
requestMessageFieldsExclude?: string[];
|
|
367
160
|
plugins?: UseMessagePlugin[];
|
|
368
|
-
responseProvider:
|
|
161
|
+
responseProvider: ResponseProvider;
|
|
369
162
|
/**
|
|
370
163
|
* 全局的数据块处理钩子,在接收到每个响应数据块时触发。
|
|
371
164
|
* 注意:此钩子与插件中的 onCompletionChunk 有区别。
|
|
@@ -501,6 +294,11 @@ interface UseConversationOptions {
|
|
|
501
294
|
* When provided, conversation list and messages can be loaded and persisted.
|
|
502
295
|
*/
|
|
503
296
|
storage?: ConversationStorageStrategy;
|
|
297
|
+
/**
|
|
298
|
+
* Called when the initial conversation list has been loaded from storage.
|
|
299
|
+
* Only fired when storage.loadConversations is available and has completed.
|
|
300
|
+
*/
|
|
301
|
+
onLoad?: (conversations: ConversationInfo[]) => void;
|
|
504
302
|
}
|
|
505
303
|
interface UseConversationReturn {
|
|
506
304
|
conversations: Ref<ConversationInfo[]>;
|
|
@@ -658,21 +456,30 @@ declare function sseStreamToGenerator<T = any>(response: Response, options?: {
|
|
|
658
456
|
|
|
659
457
|
declare const useConversation: (options: UseConversationOptions) => UseConversationReturn;
|
|
660
458
|
|
|
661
|
-
declare const fallbackRolePlugin: (options?: UseMessagePlugin & {
|
|
662
|
-
fallbackRole?: string;
|
|
663
|
-
}) => UseMessagePlugin;
|
|
664
|
-
|
|
665
459
|
declare const lengthPlugin: (options?: UseMessagePlugin & {
|
|
666
460
|
continueContent?: string;
|
|
667
461
|
}) => UseMessagePlugin;
|
|
668
462
|
|
|
669
463
|
declare const thinkingPlugin: (options?: UseMessagePlugin) => UseMessagePlugin;
|
|
670
464
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
465
|
+
interface UseMessageToolActionContext extends BasePluginContext {
|
|
466
|
+
assistantMessage: ChatMessage;
|
|
467
|
+
/**
|
|
468
|
+
* @deprecated use `assistantMessage` instead
|
|
469
|
+
*/
|
|
470
|
+
currentMessage: ChatMessage;
|
|
471
|
+
}
|
|
472
|
+
interface UseMessageCallToolContext extends UseMessageToolActionContext {
|
|
473
|
+
toolMessage: ChatMessage;
|
|
474
|
+
}
|
|
475
|
+
interface UseMessageToolCallContext extends BasePluginContext {
|
|
476
|
+
assistantMessage: ChatMessage;
|
|
477
|
+
/**
|
|
478
|
+
* @deprecated use `assistantMessage` instead
|
|
479
|
+
*/
|
|
480
|
+
primaryMessage: ChatMessage;
|
|
481
|
+
toolMessage: ChatMessage;
|
|
482
|
+
}
|
|
676
483
|
declare const toolPlugin: (options: UseMessagePlugin & {
|
|
677
484
|
/**
|
|
678
485
|
* 获取工具列表的函数。
|
|
@@ -681,25 +488,18 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
681
488
|
/**
|
|
682
489
|
* 在处理包含 tool_calls 的响应前调用。
|
|
683
490
|
*/
|
|
684
|
-
beforeCallTools?: (toolCalls: ToolCall[], context:
|
|
685
|
-
currentMessage: ChatMessage;
|
|
686
|
-
}) => Promise<void>;
|
|
491
|
+
beforeCallTools?: (toolCalls: ToolCall[], context: UseMessageToolActionContext) => Promise<void>;
|
|
687
492
|
/**
|
|
688
493
|
* 执行单个工具调用并返回其文本结果的函数。
|
|
689
494
|
*/
|
|
690
|
-
callTool: (toolCall: ToolCall, context:
|
|
691
|
-
currentMessage: ChatMessage;
|
|
692
|
-
}) => Promise<string | Record<string, any>> | AsyncGenerator<string | Record<string, any>>;
|
|
495
|
+
callTool: (toolCall: ToolCall, context: UseMessageCallToolContext) => Promise<string | Record<string, any>> | AsyncGenerator<string | Record<string, any>>;
|
|
693
496
|
/**
|
|
694
497
|
* 工具调用开始时的回调函数。
|
|
695
498
|
* 触发时机:工具消息已创建并追加后,调用 callTool 之前触发。
|
|
696
499
|
* @param toolCall - 工具调用对象
|
|
697
500
|
* @param context - 插件上下文,包含当前工具消息
|
|
698
501
|
*/
|
|
699
|
-
onToolCallStart?: (toolCall: ToolCall, context:
|
|
700
|
-
primaryMessage: ChatMessage;
|
|
701
|
-
toolMessage: ChatMessage;
|
|
702
|
-
}) => void;
|
|
502
|
+
onToolCallStart?: (toolCall: ToolCall, context: UseMessageToolCallContext) => void;
|
|
703
503
|
/**
|
|
704
504
|
* 工具调用结束时的回调函数。
|
|
705
505
|
* 触发时机:工具调用完成(成功、失败或取消)时触发。
|
|
@@ -708,9 +508,7 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
708
508
|
* @param context.status - 工具调用状态:'success' | 'failed' | 'cancelled'
|
|
709
509
|
* @param context.error - 当状态为 'failed' 或 'cancelled' 时,可能包含错误信息
|
|
710
510
|
*/
|
|
711
|
-
onToolCallEnd?: (toolCall: ToolCall, context:
|
|
712
|
-
primaryMessage: ChatMessage;
|
|
713
|
-
toolMessage: ChatMessage;
|
|
511
|
+
onToolCallEnd?: (toolCall: ToolCall, context: UseMessageToolCallContext & {
|
|
714
512
|
status: "success" | "failed" | "cancelled";
|
|
715
513
|
error?: Error;
|
|
716
514
|
}) => void;
|
|
@@ -723,20 +521,13 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
723
521
|
*/
|
|
724
522
|
toolCallFailedContent?: string;
|
|
725
523
|
/**
|
|
726
|
-
*
|
|
524
|
+
* 是否在请求前自动补充缺失的 tool 消息。
|
|
727
525
|
* 当 assistant 响应了 tool_calls 但未追加对应的 tool 消息时,
|
|
728
526
|
* 插件将自动补充"工具调用已取消"的 tool 消息。默认:false。
|
|
729
527
|
*/
|
|
730
528
|
autoFillMissingToolMessages?: boolean;
|
|
731
|
-
/**
|
|
732
|
-
* 是否在下一轮对话中,排除包含 tool_calls 的 assistant 消息和对应的 tool 消息,只保留结果。
|
|
733
|
-
* - 当为 `true` 时,这些消息会被标记为不发送,不会包含在下一次请求的 messages 中。
|
|
734
|
-
* - 当为 `'remove'` 时,这些消息会直接从 messages 数组中移除。
|
|
735
|
-
* 默认:false。
|
|
736
|
-
*/
|
|
737
|
-
excludeToolMessagesNextTurn?: boolean | typeof EXCLUDE_MODE_REMOVE;
|
|
738
529
|
}) => UseMessagePlugin;
|
|
739
530
|
|
|
740
531
|
declare const useMessage: (options: UseMessageOptions) => UseMessageReturn;
|
|
741
532
|
|
|
742
|
-
export {
|
|
533
|
+
export { AIClient, AIModelConfig, BaseModelProvider, type BasePluginContext, type ChatCompletion, ChatCompletionRequest, ChatCompletionResponse, ChatMessage, type Choice, type CompletionChoice, type Conversation, type ConversationInfo, type ConversationStorageStrategy, type DeltaChoice, type IndexedDBConfig, IndexedDBStrategy, type LocalStorageConfig, LocalStorageStrategy, MaybePromise, type MessageRequestBody, OpenAIProvider, type RequestProcessingState, type RequestState, type ResponseProvider, StreamHandler, type Tool, ToolCall, type Usage, type UseConversationOptions, type UseConversationReturn, type UseMessageCallToolContext, type UseMessageOptions, type UseMessagePlugin, type UseMessageReturn, type UseMessageToolActionContext, type UseMessageToolCallContext, extractTextFromResponse, formatMessages, handleSSEStream, indexedDBStorageStrategyFactory, lengthPlugin, localStorageStrategyFactory, sseStreamToGenerator, thinkingPlugin, toolPlugin, useConversation, useMessage };
|