@opentiny/tiny-robot-kit 0.4.2-alpha.1 → 0.4.2-alpha.3
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/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 +28 -243
- package/dist/index.d.ts +28 -243
- package/dist/index.js +3 -3
- package/dist/index.mjs +3 -3
- package/dist/types-D0E0rOVi.d.mts +212 -0
- package/dist/types-D0E0rOVi.d.ts +212 -0
- package/package.json +21 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,216 +1,7 @@
|
|
|
1
|
+
import { A as AIModelConfig, C as ChatCompletionRequest, a as ChatCompletionResponse, S as StreamHandler, B as BaseModelProvider, b as ChatMessage, M as MaybePromise, T as ToolCall } from './types-D0E0rOVi.mjs';
|
|
2
|
+
export { c as AIAdapterError, d as AIProvider, e as ChatCompletionOptions, f as ChatCompletionResponseChoice, g as ChatCompletionResponseMessage, h as ChatCompletionResponseUsage, i as ChatCompletionStreamResponse, j as ChatCompletionStreamResponseChoice, k as ChatCompletionStreamResponseDelta, l as ChatHistory, E as ErrorType, m as MessageMetadata, n as MessageRole, o as StreamEventType } from './types-D0E0rOVi.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
|
* 负责根据配置选择合适的提供商并处理请求
|
|
@@ -353,6 +144,7 @@ interface ChatCompletion {
|
|
|
353
144
|
choices: CompletionChoice[];
|
|
354
145
|
usage?: Usage;
|
|
355
146
|
}
|
|
147
|
+
type ResponseProvider<T = ChatCompletion> = (requestBody: MessageRequestBody, abortSignal: AbortSignal) => Promise<T> | AsyncGenerator<T> | Promise<AsyncGenerator<T>>;
|
|
356
148
|
interface UseMessageOptions {
|
|
357
149
|
initialMessages?: ChatMessage[];
|
|
358
150
|
/**
|
|
@@ -366,7 +158,7 @@ interface UseMessageOptions {
|
|
|
366
158
|
*/
|
|
367
159
|
requestMessageFieldsExclude?: string[];
|
|
368
160
|
plugins?: UseMessagePlugin[];
|
|
369
|
-
responseProvider:
|
|
161
|
+
responseProvider: ResponseProvider;
|
|
370
162
|
/**
|
|
371
163
|
* 全局的数据块处理钩子,在接收到每个响应数据块时触发。
|
|
372
164
|
* 注意:此钩子与插件中的 onCompletionChunk 有区别。
|
|
@@ -664,21 +456,30 @@ declare function sseStreamToGenerator<T = any>(response: Response, options?: {
|
|
|
664
456
|
|
|
665
457
|
declare const useConversation: (options: UseConversationOptions) => UseConversationReturn;
|
|
666
458
|
|
|
667
|
-
declare const fallbackRolePlugin: (options?: UseMessagePlugin & {
|
|
668
|
-
fallbackRole?: string;
|
|
669
|
-
}) => UseMessagePlugin;
|
|
670
|
-
|
|
671
459
|
declare const lengthPlugin: (options?: UseMessagePlugin & {
|
|
672
460
|
continueContent?: string;
|
|
673
461
|
}) => UseMessagePlugin;
|
|
674
462
|
|
|
675
463
|
declare const thinkingPlugin: (options?: UseMessagePlugin) => UseMessagePlugin;
|
|
676
464
|
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
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
|
+
}
|
|
682
483
|
declare const toolPlugin: (options: UseMessagePlugin & {
|
|
683
484
|
/**
|
|
684
485
|
* 获取工具列表的函数。
|
|
@@ -687,25 +488,18 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
687
488
|
/**
|
|
688
489
|
* 在处理包含 tool_calls 的响应前调用。
|
|
689
490
|
*/
|
|
690
|
-
beforeCallTools?: (toolCalls: ToolCall[], context:
|
|
691
|
-
currentMessage: ChatMessage;
|
|
692
|
-
}) => Promise<void>;
|
|
491
|
+
beforeCallTools?: (toolCalls: ToolCall[], context: UseMessageToolActionContext) => Promise<void>;
|
|
693
492
|
/**
|
|
694
493
|
* 执行单个工具调用并返回其文本结果的函数。
|
|
695
494
|
*/
|
|
696
|
-
callTool: (toolCall: ToolCall, context:
|
|
697
|
-
currentMessage: ChatMessage;
|
|
698
|
-
}) => 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>>;
|
|
699
496
|
/**
|
|
700
497
|
* 工具调用开始时的回调函数。
|
|
701
498
|
* 触发时机:工具消息已创建并追加后,调用 callTool 之前触发。
|
|
702
499
|
* @param toolCall - 工具调用对象
|
|
703
500
|
* @param context - 插件上下文,包含当前工具消息
|
|
704
501
|
*/
|
|
705
|
-
onToolCallStart?: (toolCall: ToolCall, context:
|
|
706
|
-
primaryMessage: ChatMessage;
|
|
707
|
-
toolMessage: ChatMessage;
|
|
708
|
-
}) => void;
|
|
502
|
+
onToolCallStart?: (toolCall: ToolCall, context: UseMessageToolCallContext) => void;
|
|
709
503
|
/**
|
|
710
504
|
* 工具调用结束时的回调函数。
|
|
711
505
|
* 触发时机:工具调用完成(成功、失败或取消)时触发。
|
|
@@ -714,9 +508,7 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
714
508
|
* @param context.status - 工具调用状态:'success' | 'failed' | 'cancelled'
|
|
715
509
|
* @param context.error - 当状态为 'failed' 或 'cancelled' 时,可能包含错误信息
|
|
716
510
|
*/
|
|
717
|
-
onToolCallEnd?: (toolCall: ToolCall, context:
|
|
718
|
-
primaryMessage: ChatMessage;
|
|
719
|
-
toolMessage: ChatMessage;
|
|
511
|
+
onToolCallEnd?: (toolCall: ToolCall, context: UseMessageToolCallContext & {
|
|
720
512
|
status: "success" | "failed" | "cancelled";
|
|
721
513
|
error?: Error;
|
|
722
514
|
}) => void;
|
|
@@ -729,20 +521,13 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
729
521
|
*/
|
|
730
522
|
toolCallFailedContent?: string;
|
|
731
523
|
/**
|
|
732
|
-
*
|
|
524
|
+
* 是否在请求前自动补充缺失的 tool 消息。
|
|
733
525
|
* 当 assistant 响应了 tool_calls 但未追加对应的 tool 消息时,
|
|
734
526
|
* 插件将自动补充"工具调用已取消"的 tool 消息。默认:false。
|
|
735
527
|
*/
|
|
736
528
|
autoFillMissingToolMessages?: boolean;
|
|
737
|
-
/**
|
|
738
|
-
* 是否在下一轮对话中,排除包含 tool_calls 的 assistant 消息和对应的 tool 消息,只保留结果。
|
|
739
|
-
* - 当为 `true` 时,这些消息会被标记为不发送,不会包含在下一次请求的 messages 中。
|
|
740
|
-
* - 当为 `'remove'` 时,这些消息会直接从 messages 数组中移除。
|
|
741
|
-
* 默认:false。
|
|
742
|
-
*/
|
|
743
|
-
excludeToolMessagesNextTurn?: boolean | typeof EXCLUDE_MODE_REMOVE;
|
|
744
529
|
}) => UseMessagePlugin;
|
|
745
530
|
|
|
746
531
|
declare const useMessage: (options: UseMessageOptions) => UseMessageReturn;
|
|
747
532
|
|
|
748
|
-
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,216 +1,7 @@
|
|
|
1
|
+
import { A as AIModelConfig, C as ChatCompletionRequest, a as ChatCompletionResponse, S as StreamHandler, B as BaseModelProvider, b as ChatMessage, M as MaybePromise, T as ToolCall } from './types-D0E0rOVi.js';
|
|
2
|
+
export { c as AIAdapterError, d as AIProvider, e as ChatCompletionOptions, f as ChatCompletionResponseChoice, g as ChatCompletionResponseMessage, h as ChatCompletionResponseUsage, i as ChatCompletionStreamResponse, j as ChatCompletionStreamResponseChoice, k as ChatCompletionStreamResponseDelta, l as ChatHistory, E as ErrorType, m as MessageMetadata, n as MessageRole, o as StreamEventType } from './types-D0E0rOVi.js';
|
|
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
|
* 负责根据配置选择合适的提供商并处理请求
|
|
@@ -353,6 +144,7 @@ interface ChatCompletion {
|
|
|
353
144
|
choices: CompletionChoice[];
|
|
354
145
|
usage?: Usage;
|
|
355
146
|
}
|
|
147
|
+
type ResponseProvider<T = ChatCompletion> = (requestBody: MessageRequestBody, abortSignal: AbortSignal) => Promise<T> | AsyncGenerator<T> | Promise<AsyncGenerator<T>>;
|
|
356
148
|
interface UseMessageOptions {
|
|
357
149
|
initialMessages?: ChatMessage[];
|
|
358
150
|
/**
|
|
@@ -366,7 +158,7 @@ interface UseMessageOptions {
|
|
|
366
158
|
*/
|
|
367
159
|
requestMessageFieldsExclude?: string[];
|
|
368
160
|
plugins?: UseMessagePlugin[];
|
|
369
|
-
responseProvider:
|
|
161
|
+
responseProvider: ResponseProvider;
|
|
370
162
|
/**
|
|
371
163
|
* 全局的数据块处理钩子,在接收到每个响应数据块时触发。
|
|
372
164
|
* 注意:此钩子与插件中的 onCompletionChunk 有区别。
|
|
@@ -664,21 +456,30 @@ declare function sseStreamToGenerator<T = any>(response: Response, options?: {
|
|
|
664
456
|
|
|
665
457
|
declare const useConversation: (options: UseConversationOptions) => UseConversationReturn;
|
|
666
458
|
|
|
667
|
-
declare const fallbackRolePlugin: (options?: UseMessagePlugin & {
|
|
668
|
-
fallbackRole?: string;
|
|
669
|
-
}) => UseMessagePlugin;
|
|
670
|
-
|
|
671
459
|
declare const lengthPlugin: (options?: UseMessagePlugin & {
|
|
672
460
|
continueContent?: string;
|
|
673
461
|
}) => UseMessagePlugin;
|
|
674
462
|
|
|
675
463
|
declare const thinkingPlugin: (options?: UseMessagePlugin) => UseMessagePlugin;
|
|
676
464
|
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
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
|
+
}
|
|
682
483
|
declare const toolPlugin: (options: UseMessagePlugin & {
|
|
683
484
|
/**
|
|
684
485
|
* 获取工具列表的函数。
|
|
@@ -687,25 +488,18 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
687
488
|
/**
|
|
688
489
|
* 在处理包含 tool_calls 的响应前调用。
|
|
689
490
|
*/
|
|
690
|
-
beforeCallTools?: (toolCalls: ToolCall[], context:
|
|
691
|
-
currentMessage: ChatMessage;
|
|
692
|
-
}) => Promise<void>;
|
|
491
|
+
beforeCallTools?: (toolCalls: ToolCall[], context: UseMessageToolActionContext) => Promise<void>;
|
|
693
492
|
/**
|
|
694
493
|
* 执行单个工具调用并返回其文本结果的函数。
|
|
695
494
|
*/
|
|
696
|
-
callTool: (toolCall: ToolCall, context:
|
|
697
|
-
currentMessage: ChatMessage;
|
|
698
|
-
}) => 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>>;
|
|
699
496
|
/**
|
|
700
497
|
* 工具调用开始时的回调函数。
|
|
701
498
|
* 触发时机:工具消息已创建并追加后,调用 callTool 之前触发。
|
|
702
499
|
* @param toolCall - 工具调用对象
|
|
703
500
|
* @param context - 插件上下文,包含当前工具消息
|
|
704
501
|
*/
|
|
705
|
-
onToolCallStart?: (toolCall: ToolCall, context:
|
|
706
|
-
primaryMessage: ChatMessage;
|
|
707
|
-
toolMessage: ChatMessage;
|
|
708
|
-
}) => void;
|
|
502
|
+
onToolCallStart?: (toolCall: ToolCall, context: UseMessageToolCallContext) => void;
|
|
709
503
|
/**
|
|
710
504
|
* 工具调用结束时的回调函数。
|
|
711
505
|
* 触发时机:工具调用完成(成功、失败或取消)时触发。
|
|
@@ -714,9 +508,7 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
714
508
|
* @param context.status - 工具调用状态:'success' | 'failed' | 'cancelled'
|
|
715
509
|
* @param context.error - 当状态为 'failed' 或 'cancelled' 时,可能包含错误信息
|
|
716
510
|
*/
|
|
717
|
-
onToolCallEnd?: (toolCall: ToolCall, context:
|
|
718
|
-
primaryMessage: ChatMessage;
|
|
719
|
-
toolMessage: ChatMessage;
|
|
511
|
+
onToolCallEnd?: (toolCall: ToolCall, context: UseMessageToolCallContext & {
|
|
720
512
|
status: "success" | "failed" | "cancelled";
|
|
721
513
|
error?: Error;
|
|
722
514
|
}) => void;
|
|
@@ -729,20 +521,13 @@ declare const toolPlugin: (options: UseMessagePlugin & {
|
|
|
729
521
|
*/
|
|
730
522
|
toolCallFailedContent?: string;
|
|
731
523
|
/**
|
|
732
|
-
*
|
|
524
|
+
* 是否在请求前自动补充缺失的 tool 消息。
|
|
733
525
|
* 当 assistant 响应了 tool_calls 但未追加对应的 tool 消息时,
|
|
734
526
|
* 插件将自动补充"工具调用已取消"的 tool 消息。默认:false。
|
|
735
527
|
*/
|
|
736
528
|
autoFillMissingToolMessages?: boolean;
|
|
737
|
-
/**
|
|
738
|
-
* 是否在下一轮对话中,排除包含 tool_calls 的 assistant 消息和对应的 tool 消息,只保留结果。
|
|
739
|
-
* - 当为 `true` 时,这些消息会被标记为不发送,不会包含在下一次请求的 messages 中。
|
|
740
|
-
* - 当为 `'remove'` 时,这些消息会直接从 messages 数组中移除。
|
|
741
|
-
* 默认:false。
|
|
742
|
-
*/
|
|
743
|
-
excludeToolMessagesNextTurn?: boolean | typeof EXCLUDE_MODE_REMOVE;
|
|
744
529
|
}) => UseMessagePlugin;
|
|
745
530
|
|
|
746
531
|
declare const useMessage: (options: UseMessageOptions) => UseMessageReturn;
|
|
747
532
|
|
|
748
|
-
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 };
|