knight-agent 1.1.0 → 1.1.2

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.
Files changed (173) hide show
  1. package/Host.d.ts +3 -11
  2. package/Host.d.ts.map +1 -1
  3. package/Host.js +4 -5
  4. package/Host.js.map +1 -1
  5. package/base/AbstractAgent.d.ts +1 -99
  6. package/base/AbstractAgent.d.ts.map +1 -1
  7. package/base/AbstractAgent.js +94 -20
  8. package/base/AbstractAgent.js.map +1 -1
  9. package/base/AbstractLLM.d.ts +43 -0
  10. package/base/AbstractLLM.d.ts.map +1 -0
  11. package/base/AbstractLLM.js +121 -0
  12. package/base/AbstractLLM.js.map +1 -0
  13. package/base/AbstractModule.d.ts +1 -0
  14. package/base/AbstractModule.d.ts.map +1 -1
  15. package/base/AbstractModule.js +3 -0
  16. package/base/AbstractModule.js.map +1 -1
  17. package/base/AbstractObject.d.ts +6 -4
  18. package/base/AbstractObject.d.ts.map +1 -1
  19. package/base/AbstractObject.js +6 -2
  20. package/base/AbstractObject.js.map +1 -1
  21. package/base/index.d.ts +1 -4
  22. package/base/index.d.ts.map +1 -1
  23. package/base/index.js +1 -4
  24. package/base/index.js.map +1 -1
  25. package/index.d.ts +2 -1
  26. package/index.d.ts.map +1 -1
  27. package/index.js +2 -6
  28. package/index.js.map +1 -1
  29. package/module/AgentModule/Agent.d.ts +1 -54
  30. package/module/AgentModule/Agent.d.ts.map +1 -1
  31. package/module/AgentModule/Agent.js +22 -207
  32. package/module/AgentModule/Agent.js.map +1 -1
  33. package/module/AgentModule/AgentModule.d.ts +1 -25
  34. package/module/AgentModule/AgentModule.d.ts.map +1 -1
  35. package/module/AgentModule/AgentModule.js +78 -63
  36. package/module/AgentModule/AgentModule.js.map +1 -1
  37. package/module/AgentModule/index.d.ts +1 -1
  38. package/module/AgentModule/index.d.ts.map +1 -1
  39. package/module/AgentModule/index.js +2 -15
  40. package/module/AgentModule/index.js.map +1 -1
  41. package/module/LLMModule/ClaudeLLM.d.ts +23 -0
  42. package/module/LLMModule/ClaudeLLM.d.ts.map +1 -0
  43. package/module/LLMModule/ClaudeLLM.js +138 -0
  44. package/module/LLMModule/ClaudeLLM.js.map +1 -0
  45. package/module/LLMModule/DeepSeekLLM.d.ts +24 -0
  46. package/module/LLMModule/DeepSeekLLM.d.ts.map +1 -0
  47. package/module/LLMModule/DeepSeekLLM.js +136 -0
  48. package/module/LLMModule/DeepSeekLLM.js.map +1 -0
  49. package/module/LLMModule/GeminiLLM.d.ts +23 -0
  50. package/module/LLMModule/GeminiLLM.d.ts.map +1 -0
  51. package/module/LLMModule/GeminiLLM.js +168 -0
  52. package/module/LLMModule/GeminiLLM.js.map +1 -0
  53. package/module/LLMModule/LLMModule.d.ts +33 -0
  54. package/module/LLMModule/LLMModule.d.ts.map +1 -0
  55. package/module/LLMModule/LLMModule.js +92 -0
  56. package/module/LLMModule/LLMModule.js.map +1 -0
  57. package/module/LLMModule/OpenAILLM.d.ts +23 -0
  58. package/module/LLMModule/OpenAILLM.d.ts.map +1 -0
  59. package/module/LLMModule/OpenAILLM.js +145 -0
  60. package/module/LLMModule/OpenAILLM.js.map +1 -0
  61. package/module/LLMModule/QWenLLM.d.ts +23 -0
  62. package/module/LLMModule/QWenLLM.d.ts.map +1 -0
  63. package/module/LLMModule/QWenLLM.js +137 -0
  64. package/module/LLMModule/QWenLLM.js.map +1 -0
  65. package/module/LLMModule/index.d.ts +7 -0
  66. package/module/LLMModule/index.d.ts.map +1 -0
  67. package/module/{SkillModule → LLMModule}/index.js +6 -1
  68. package/module/LLMModule/index.js.map +1 -0
  69. package/module/index.d.ts +1 -4
  70. package/module/index.d.ts.map +1 -1
  71. package/module/index.js +1 -4
  72. package/module/index.js.map +1 -1
  73. package/package.json +1 -1
  74. package/utility/AgentUtility.d.ts +38 -0
  75. package/utility/AgentUtility.d.ts.map +1 -0
  76. package/{base/AbstractTool.js → utility/AgentUtility.js} +1 -1
  77. package/utility/AgentUtility.js.map +1 -0
  78. package/utility/LLMUtility.d.ts +1328 -0
  79. package/utility/LLMUtility.d.ts.map +1 -0
  80. package/utility/LLMUtility.js +114 -0
  81. package/utility/LLMUtility.js.map +1 -0
  82. package/utility/index.d.ts +2 -1
  83. package/utility/index.d.ts.map +1 -1
  84. package/utility/index.js +2 -1
  85. package/utility/index.js.map +1 -1
  86. package/base/AbstractDialog.d.ts +0 -54
  87. package/base/AbstractDialog.d.ts.map +0 -1
  88. package/base/AbstractDialog.js +0 -86
  89. package/base/AbstractDialog.js.map +0 -1
  90. package/base/AbstractReason.d.ts +0 -44
  91. package/base/AbstractReason.d.ts.map +0 -1
  92. package/base/AbstractReason.js +0 -23
  93. package/base/AbstractReason.js.map +0 -1
  94. package/base/AbstractSkill.d.ts +0 -25
  95. package/base/AbstractSkill.d.ts.map +0 -1
  96. package/base/AbstractSkill.js +0 -3
  97. package/base/AbstractSkill.js.map +0 -1
  98. package/base/AbstractTool.d.ts +0 -34
  99. package/base/AbstractTool.d.ts.map +0 -1
  100. package/base/AbstractTool.js.map +0 -1
  101. package/docs/ARCHITECTURE.md +0 -601
  102. package/docs/README.md +0 -562
  103. package/module/AgentModule/Memory.d.ts +0 -55
  104. package/module/AgentModule/Memory.d.ts.map +0 -1
  105. package/module/AgentModule/Memory.js +0 -288
  106. package/module/AgentModule/Memory.js.map +0 -1
  107. package/module/DialogModule/Dialog.d.ts +0 -14
  108. package/module/DialogModule/Dialog.d.ts.map +0 -1
  109. package/module/DialogModule/Dialog.js +0 -27
  110. package/module/DialogModule/Dialog.js.map +0 -1
  111. package/module/DialogModule/DialogModule.d.ts +0 -39
  112. package/module/DialogModule/DialogModule.d.ts.map +0 -1
  113. package/module/DialogModule/DialogModule.js +0 -118
  114. package/module/DialogModule/DialogModule.js.map +0 -1
  115. package/module/DialogModule/index.d.ts +0 -3
  116. package/module/DialogModule/index.d.ts.map +0 -1
  117. package/module/DialogModule/index.js +0 -19
  118. package/module/DialogModule/index.js.map +0 -1
  119. package/module/ReasonModule/ChainReason.d.ts +0 -11
  120. package/module/ReasonModule/ChainReason.d.ts.map +0 -1
  121. package/module/ReasonModule/ChainReason.js +0 -34
  122. package/module/ReasonModule/ChainReason.js.map +0 -1
  123. package/module/ReasonModule/CommanderReason.d.ts +0 -15
  124. package/module/ReasonModule/CommanderReason.d.ts.map +0 -1
  125. package/module/ReasonModule/CommanderReason.js +0 -151
  126. package/module/ReasonModule/CommanderReason.js.map +0 -1
  127. package/module/ReasonModule/CompetitiveReason.d.ts +0 -13
  128. package/module/ReasonModule/CompetitiveReason.d.ts.map +0 -1
  129. package/module/ReasonModule/CompetitiveReason.js +0 -133
  130. package/module/ReasonModule/CompetitiveReason.js.map +0 -1
  131. package/module/ReasonModule/PlanReason.d.ts +0 -25
  132. package/module/ReasonModule/PlanReason.d.ts.map +0 -1
  133. package/module/ReasonModule/PlanReason.js +0 -133
  134. package/module/ReasonModule/PlanReason.js.map +0 -1
  135. package/module/ReasonModule/ReasonModule.d.ts +0 -41
  136. package/module/ReasonModule/ReasonModule.d.ts.map +0 -1
  137. package/module/ReasonModule/ReasonModule.js +0 -83
  138. package/module/ReasonModule/ReasonModule.js.map +0 -1
  139. package/module/ReasonModule/ReflectReason.d.ts +0 -15
  140. package/module/ReasonModule/ReflectReason.d.ts.map +0 -1
  141. package/module/ReasonModule/ReflectReason.js +0 -86
  142. package/module/ReasonModule/ReflectReason.js.map +0 -1
  143. package/module/ReasonModule/TreeReason.d.ts +0 -16
  144. package/module/ReasonModule/TreeReason.d.ts.map +0 -1
  145. package/module/ReasonModule/TreeReason.js +0 -139
  146. package/module/ReasonModule/TreeReason.js.map +0 -1
  147. package/module/ReasonModule/index.d.ts +0 -8
  148. package/module/ReasonModule/index.d.ts.map +0 -1
  149. package/module/ReasonModule/index.js +0 -24
  150. package/module/ReasonModule/index.js.map +0 -1
  151. package/module/SkillModule/SkillModule.d.ts +0 -42
  152. package/module/SkillModule/SkillModule.d.ts.map +0 -1
  153. package/module/SkillModule/SkillModule.js +0 -134
  154. package/module/SkillModule/SkillModule.js.map +0 -1
  155. package/module/SkillModule/index.d.ts +0 -2
  156. package/module/SkillModule/index.d.ts.map +0 -1
  157. package/module/SkillModule/index.js.map +0 -1
  158. package/module/ToolModule/ToolModule.d.ts +0 -57
  159. package/module/ToolModule/ToolModule.d.ts.map +0 -1
  160. package/module/ToolModule/ToolModule.js +0 -220
  161. package/module/ToolModule/ToolModule.js.map +0 -1
  162. package/module/ToolModule/ToolRAG.d.ts +0 -38
  163. package/module/ToolModule/ToolRAG.d.ts.map +0 -1
  164. package/module/ToolModule/ToolRAG.js +0 -124
  165. package/module/ToolModule/ToolRAG.js.map +0 -1
  166. package/module/ToolModule/index.d.ts +0 -3
  167. package/module/ToolModule/index.d.ts.map +0 -1
  168. package/module/ToolModule/index.js +0 -19
  169. package/module/ToolModule/index.js.map +0 -1
  170. package/utility/OpenAIUtility.d.ts +0 -333
  171. package/utility/OpenAIUtility.d.ts.map +0 -1
  172. package/utility/OpenAIUtility.js +0 -95
  173. package/utility/OpenAIUtility.js.map +0 -1
@@ -0,0 +1,1328 @@
1
+ /** 大模型工具 */
2
+ export declare namespace LLMUtility {
3
+ /**
4
+ * JSON数据格式规范(符合MCP InputSchema规范)
5
+ */
6
+ interface JSONSchema {
7
+ /** 数据类型 */
8
+ type: "object" | "string" | "number" | "boolean" | "array" | "null" | string;
9
+ /** 对象类型的属性定义,当type为"object"时使用,键为属性名,值为该属性的JSONSchema */
10
+ properties?: Record<string, JSONSchema>;
11
+ /** 必需属性列表,当type为"object"时使用,列出必须包含的属性名 */
12
+ required?: string[];
13
+ /** 字段描述 */
14
+ description?: string;
15
+ /** 枚举值限制 */
16
+ enum?: any[];
17
+ /** 数值最小值(包含),仅用于number类型 */
18
+ minimum?: number;
19
+ /** 数值最大值(包含),仅用于number类型 */
20
+ maximum?: number;
21
+ /** 正则表达式模式,用于约束字符串格式 */
22
+ pattern?: string;
23
+ /** 数组元素类型定义,当type为"array"时使用,描述数组中每个元素的Schema */
24
+ items?: JSONSchema;
25
+ }
26
+ /** 大模型配置 */
27
+ interface Config {
28
+ /** 类型 */
29
+ type: string | any;
30
+ /** 名称 */
31
+ name: string;
32
+ /** 描述 */
33
+ description: string;
34
+ /** 请求地址 */
35
+ url: string;
36
+ /** 模型名称 */
37
+ model: string;
38
+ /** API密钥 */
39
+ apiKey: string;
40
+ /** 请求超时(ms) */
41
+ timeout?: number;
42
+ /** 采样温度 (0~2),Claude 4.7+ 弃用仅接受 1.0 */
43
+ temperature?: number;
44
+ /** 核采样 (0~1),与 temperature 二选一或配合 */
45
+ topP?: number;
46
+ /** 最大输出 token 数 */
47
+ maxTokens?: number;
48
+ /** 停止序列 (最多 4 条) */
49
+ stop?: string | string[];
50
+ /** 流式响应开关 */
51
+ stream?: boolean;
52
+ /** 可复现采样种子 (Anthropic 不支持) */
53
+ seed?: number;
54
+ /** 最大消息缓存数量 */
55
+ maxMessageCache?: number;
56
+ /** 工具输出最大字符数,超出部分截断 (默认 800) */
57
+ maxToolOutput?: number;
58
+ }
59
+ /** 发送数据接口 */
60
+ interface SendData {
61
+ /** 提示词 */
62
+ prompt: string;
63
+ /** 技能 */
64
+ skill: string;
65
+ /** 工具列表 */
66
+ tools?: Array<LLMUtility.ToolDefinition>;
67
+ /** 用量回调 */
68
+ onUsage?: LLMUtility.UsageCallback;
69
+ }
70
+ /** 缓存消息基础接口 — 消息须有 role 以支持轮次修剪 */
71
+ interface MessageData {
72
+ /** 角色 */
73
+ role?: string;
74
+ /** 保持 */
75
+ preserve?: boolean;
76
+ }
77
+ /** 通用消息缓存 — 各平台通过泛型 T 指定消息类型 */
78
+ class Message<T extends MessageData> {
79
+ /** 消息缓存池 */
80
+ private readonly messages;
81
+ /** 最大保留轮次数(以 role==="user" 为轮次起点) */
82
+ private readonly maxTurns;
83
+ /** 构造函数 */
84
+ constructor(maxTurns?: number);
85
+ /** 消息数量 */
86
+ get count(): number;
87
+ /**
88
+ * 添加单条消息
89
+ * @param preserve trim 时是否保留此消息(系统消息设为 true)
90
+ */
91
+ Add(message: T): this;
92
+ /** 批量添加消息 */
93
+ AddRange(list: Array<T>): this;
94
+ /** 获取全部消息(返回副本) */
95
+ All(): Array<T>;
96
+ /** 获取最后一条消息 */
97
+ Last(): T | undefined;
98
+ /** 获取最后N条消息 */
99
+ LastN(n: number): Array<T>;
100
+ /** 移除最后一条消息 */
101
+ RemoveLast(): T | undefined;
102
+ /** 清空所有消息 */
103
+ Clear(): this;
104
+ /** 导出全部消息(返回副本,供快照使用) */
105
+ Export(): Array<T>;
106
+ /** 导入消息(替换当前缓存,供快照恢复使用) */
107
+ Import(messages: Array<T>): void;
108
+ /**
109
+ * 按轮次修剪缓存 — 保留最近 maxTurns 轮,preserve=true 的消息不被裁剪
110
+ * @returns 被移除的消息数量
111
+ */
112
+ Trim(): number;
113
+ /**
114
+ * 对每条消息应用转换函数(如工具输出截断/观察屏蔽)
115
+ * @param transform 转换函数,返回新消息替换旧消息,返回原引用则跳过
116
+ * @returns 被修改的消息数量
117
+ */
118
+ Compact(transform: (msg: T) => T): number;
119
+ }
120
+ /**
121
+ * 大模型工具定义
122
+ * 各平台 request() 中转换为原生格式
123
+ */
124
+ interface ToolDefinition {
125
+ /** 工具名称 */
126
+ name: string;
127
+ /** 工具描述 */
128
+ description?: string;
129
+ /** 输入参数 JSON Schema */
130
+ inputSchema: JSONSchema;
131
+ /** 调用处理函数(业务层封装,内部可调 MCP 或任何逻辑) */
132
+ handler: (args: Record<string, any>) => Promise<string>;
133
+ }
134
+ /**
135
+ * 大模型响应
136
+ * 各平台 LLM 负责将原始响应映射为此结构
137
+ */
138
+ interface Response {
139
+ /** 响应ID */
140
+ id: string;
141
+ /** 模型名称 */
142
+ model: string;
143
+ /** 文本内容 */
144
+ content: string;
145
+ /** 工具调用 */
146
+ toolCalls?: Array<ToolCall>;
147
+ /** 结束原因 */
148
+ finishReason: FinishReason;
149
+ /** Token 用量 */
150
+ usage: Usage;
151
+ /** 原始响应(调试用) */
152
+ raw: object;
153
+ }
154
+ /** 大模型工具调用 */
155
+ interface ToolCall {
156
+ /** 调用ID */
157
+ id: string;
158
+ /** 工具名 */
159
+ name: string;
160
+ /** 参数 */
161
+ arguments: Record<string, any>;
162
+ }
163
+ /** 统一工具结果 — 工具执行后返回 */
164
+ interface ToolResult {
165
+ /** 对应的工具调用 ID */
166
+ id: string;
167
+ /** 工具名称 */
168
+ name: string;
169
+ /** 结果数据(已序列化) */
170
+ data: string;
171
+ }
172
+ /** 大模型结束原因 */
173
+ type FinishReason = "stop" | "length" | "tool_calls" | "content_filter" | "unknown";
174
+ /** 大模型Token用量 */
175
+ interface Usage {
176
+ /** 输入 token */
177
+ input: number;
178
+ /** 输出 token */
179
+ output: number;
180
+ /** 总计 token */
181
+ total: number;
182
+ }
183
+ /** 用量回调 — send() 结束时触发,携带单次指令的聚合 token 用量 */
184
+ type UsageCallback = (usage: Usage) => void;
185
+ /** 大模型快照 — 序列化消息缓存与配置,供恢复使用 */
186
+ interface Snapshot {
187
+ /** 原始实例 ID */
188
+ id: string;
189
+ /** 平台配置(用于 LLMModule.restore 重建) */
190
+ config: Config;
191
+ /** 消息缓存快照 */
192
+ messages: MessageData[];
193
+ /** 导出时间戳 */
194
+ timeStemp: number;
195
+ }
196
+ /** Anthropic Claude接口 */
197
+ namespace Claude {
198
+ /**
199
+ * Anthropic Claude 平台配置
200
+ * 参考: https://platform.claude.com/docs/en/build-with-claude/working-with-messages
201
+ * 注意: Opus 4.7+ temperature/topP/topK 已弃用
202
+ */
203
+ interface Config extends LLMUtility.Config {
204
+ /** 类型 */
205
+ type: "Claude";
206
+ /** 扩展思考: enabled(含budget_tokens) / disabled / adaptive */
207
+ thinking?: {
208
+ type: "enabled" | "disabled" | "adaptive";
209
+ budgetTokens?: number;
210
+ };
211
+ /** 输出控制: effort 取 low ~ xhigh */
212
+ outputConfig?: {
213
+ effort?: "low" | "medium" | "high" | "xhigh" | "max";
214
+ };
215
+ /** 推理速度: fast 高吞吐 */
216
+ speed?: "standard" | "fast";
217
+ /** 用户标识(限流/防滥用) */
218
+ userId?: string;
219
+ /** 输出格式控制 (text / json_object),通过 prefill 实现 */
220
+ responseFormat?: string;
221
+ }
222
+ /** 消息数据 */
223
+ interface MessageData extends LLMUtility.MessageData {
224
+ role: "user" | "assistant";
225
+ content: string | Array<ContentBlock>;
226
+ }
227
+ /** 内容块 */
228
+ type ContentBlock = TextBlock | ImageBlock | ToolUseBlock | ToolResultBlock | ThinkingBlock;
229
+ /** 文本块 */
230
+ interface TextBlock {
231
+ type: "text";
232
+ text: string;
233
+ }
234
+ /** 图片块 */
235
+ interface ImageBlock {
236
+ type: "image";
237
+ source: {
238
+ type: "base64";
239
+ media_type: "image/jpeg" | "image/png" | "image/gif" | "image/webp";
240
+ data: string;
241
+ };
242
+ }
243
+ /** 工具使用块(响应) */
244
+ interface ToolUseBlock {
245
+ type: "tool_use";
246
+ id: string;
247
+ name: string;
248
+ input: Record<string, unknown>;
249
+ }
250
+ /** 工具结果块(请求) */
251
+ interface ToolResultBlock {
252
+ type: "tool_result";
253
+ tool_use_id: string;
254
+ content: string;
255
+ is_error?: boolean;
256
+ }
257
+ /** 思考块(响应) */
258
+ interface ThinkingBlock {
259
+ type: "thinking";
260
+ thinking: string;
261
+ signature: string;
262
+ }
263
+ /** 思考配置 */
264
+ interface ThinkingConfig {
265
+ type: "enabled" | "disabled" | "adaptive";
266
+ budget_tokens?: number;
267
+ }
268
+ /** 工具定义 */
269
+ interface Tool {
270
+ name: string;
271
+ description?: string;
272
+ input_schema: {
273
+ type: "object";
274
+ properties?: Record<string, unknown>;
275
+ required?: Array<string>;
276
+ };
277
+ }
278
+ /** 工具选择 */
279
+ type ToolChoice = "auto" | "any" | "none" | {
280
+ type: "tool";
281
+ name: string;
282
+ disable_parallel_tool_use?: boolean;
283
+ };
284
+ /** Messages API请求结构 */
285
+ interface ChatRequest {
286
+ model: string;
287
+ messages: Array<MessageData>;
288
+ system?: string | Array<TextBlock>;
289
+ max_tokens: number;
290
+ temperature?: number;
291
+ top_p?: number;
292
+ top_k?: number;
293
+ stop_sequences?: Array<string>;
294
+ stream?: boolean;
295
+ thinking?: ThinkingConfig;
296
+ tools?: Array<Tool>;
297
+ tool_choice?: ToolChoice;
298
+ metadata?: {
299
+ user_id?: string;
300
+ };
301
+ }
302
+ /** Messages API响应结构 */
303
+ interface ChatResponse {
304
+ id: string;
305
+ type: "message";
306
+ role: "assistant";
307
+ model: string;
308
+ content: Array<ContentBlock>;
309
+ stop_reason: "end_turn" | "max_tokens" | "stop_sequence" | "tool_use" | null;
310
+ stop_sequence: string | null;
311
+ usage: {
312
+ input_tokens: number;
313
+ output_tokens: number;
314
+ cache_creation_input_tokens?: number;
315
+ cache_read_input_tokens?: number;
316
+ };
317
+ }
318
+ }
319
+ /** DeepSeek接口 */
320
+ namespace DeepSeek {
321
+ /**
322
+ * DeepSeek 平台配置
323
+ * 参考: https://api-docs.deepseek.com/api/create-chat-completion
324
+ * 注意: thinking 模式下 temperature/topP/logprobs 不生效
325
+ */
326
+ interface Config extends LLMUtility.Config {
327
+ /** 类型 */
328
+ type: "DeepSeek";
329
+ /** 思考模式: enabled 开启 / disabled 关闭 (DeepSeek V4 核心特性) */
330
+ thinking?: {
331
+ type: "enabled" | "disabled";
332
+ };
333
+ /** 输出格式: text 或 JSON 模式 */
334
+ responseFormat?: "text" | "json_object";
335
+ /** 用户标识(安全审查) */
336
+ userId?: string;
337
+ }
338
+ /** 消息结构 */
339
+ interface MessageData extends LLMUtility.MessageData {
340
+ /**
341
+ * 消息角色
342
+ * 系统-system、助手-assistant、用户-user、工具-tool
343
+ */
344
+ role: "system" | "user" | "assistant" | "tool";
345
+ /** 消息内容 */
346
+ content: string;
347
+ }
348
+ /** 系统消息 */
349
+ interface SystemMessage extends MessageData {
350
+ role: "system";
351
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
352
+ name?: string;
353
+ }
354
+ /** 用户消息 */
355
+ interface UserMessage extends MessageData {
356
+ role: "user";
357
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
358
+ name?: string;
359
+ }
360
+ /** 助手消息 */
361
+ interface AssistantMessage extends MessageData {
362
+ role: "assistant";
363
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
364
+ name?: string;
365
+ /**
366
+ * (Beta)设置此参数为 true,来强制模型在其回答中以此 assistant 消息中提供的前缀内容开始
367
+ * 注意:必须设置 base_url="https://api.deepseek.com/beta" 来使用此功能
368
+ */
369
+ prefix?: boolean;
370
+ /** 模型的思考内容(thinking mode时返回) */
371
+ thinking?: string;
372
+ /** 推理内容(Beta DeepSeek扩展,当启用thinking模式时返回)用于思考模式下在对话前缀续写功能下,作为最后一条 assistant 思维链内容的输入。使用此功能时,prefix 参数必须设置为 true */
373
+ reasoning_content?: string;
374
+ /** 模型生成的 tool 调用 */
375
+ tool_calls?: Array<ToolResponse>;
376
+ }
377
+ /** 工具消息 */
378
+ interface ToolMessage extends MessageData {
379
+ role: "tool";
380
+ /** 此消息所响应的 tool call 的 ID */
381
+ tool_call_id: string;
382
+ }
383
+ /** 工具调用函数(请求时为工具定义无 id,响应时有 id) */
384
+ interface ToolCall {
385
+ /** 工具调用唯一标识(仅响应) */
386
+ id?: string;
387
+ /** 调用类型,固定为"function" */
388
+ type: "function";
389
+ /** 函数调用详情 */
390
+ function: {
391
+ /** 函数名称 */
392
+ name: string;
393
+ /** 函数描述 */
394
+ description?: string;
395
+ /** 函数参数 */
396
+ parameters: JSONSchema;
397
+ /** 如果设置为 true,API 将在函数调用中使用 strict 模式,以确保输出始终符合函数的 JSON schema 定义。该功能为 Beta 功能 */
398
+ strict?: boolean;
399
+ };
400
+ }
401
+ /** 工具响应 */
402
+ interface ToolResponse {
403
+ /** 索引 */
404
+ index: number;
405
+ /** tool 调用的 ID */
406
+ id: string;
407
+ /** tool 的类型 */
408
+ type: "function";
409
+ /** 模型调用的 function */
410
+ function: {
411
+ /** 模型调用的 function 名 */
412
+ name: string;
413
+ /** 要调用的 function 的参数,由模型生成,格式为 JSON。请注意,模型并不总是生成有效的 JSON,并且可能会臆造出你函数模式中未定义的参数。在调用函数之前,请在代码中验证这些参数 */
414
+ arguments: string;
415
+ };
416
+ }
417
+ /** OpenAI Chat API请求结构 */
418
+ interface ChatRequest {
419
+ /** 模型 */
420
+ model: string;
421
+ /** 消息 */
422
+ messages: Array<MessageData>;
423
+ /** 思考模式开关(默认思考开关为 enabled) */
424
+ thinking: {
425
+ type: "enabled" | "disabled";
426
+ };
427
+ /**
428
+ * 控制模型的推理强度
429
+ * - 对普通请求,默认为 high
430
+ * - 对一些复杂 Agent 类请求(如 Claude Code、OpenCode),自动设置为 max
431
+ * - 出于兼容考虑 low、medium 会映射为 high, xhigh 会映射为 max
432
+ */
433
+ reasoning_effort?: "none" | "minimal" | "low" | "medium" | "high" | "xhigh";
434
+ /** 惩罚频率(-2.0 和 2.0 之间的数字)
435
+ * - 如果该值为正,那么新 token 会根据其在已有文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性
436
+ */
437
+ frequency_penalty?: number;
438
+ /** 限制一次请求中模型生成 completion 的最大 token 数。输入 token 和输出 token 的总长度受模型的上下文长度的限制 */
439
+ max_tokens?: number;
440
+ /** 实时惩罚(-2.0 和 2.0 之间的数字)
441
+ * - 如果该值为正,那么新 token 会根据其是否已在已有文本中出现受到相应的惩罚,从而增加模型谈论新主题的可能性。
442
+ */
443
+ presence_penalty?: number;
444
+ /** 指定模型必须输出的格式
445
+ * - 设置为 { "type": "json_object" } 以启用 JSON 模式,该模式保证模型生成的消息是有效的 JSON。
446
+ * - 注意: 使用 JSON 模式时,你还必须通过系统或用户消息指示模型生成 JSON。否则,模型可能会生成不断的空白字符,直到生成达到令牌限制,从而导致请求长时间运行并显得“卡住”。此外,如果 finish_reason="length",这表示生成超过了 max_tokens 或对话超过了最大上下文长度,消息内容可能会被部分截断。
447
+ */
448
+ response_format?: {
449
+ /** 类型(默认:text) */
450
+ type: "text" | "json_object";
451
+ };
452
+ /** 停止词
453
+ * - 在遇到这些词时,API将停止生成更多的token */
454
+ stop?: string | Array<string>;
455
+ /** 如果设置为 True,将会以 SSE(server-sent events)的形式以流式发送消息增量。消息流以 data: [DONE] 结尾。 */
456
+ stream?: boolean;
457
+ /** 流式输出相关选项(只有在stream参数为true时,才可设置此参数) */
458
+ stream_options?: {
459
+ /** 如果设置为 true,在流式消息最后的 data: [DONE] 之前将会传输一个额外的块。此块上的 usage 字段显示整个请求的 token 使用统计信息,而 choices 字段将始终是一个空数组。所有其他块也将包含一个 usage 字段,但其值为 null */
460
+ include_usage: boolean;
461
+ };
462
+ /** 采样温度(0 - 2之间,默认:1)
463
+ * - 更高的值,如 0.8,会使输出更随机,而更低的值,如 0.2,会使其更加集中和确定。 我们通常建议可以更改这个值或者更改 top_p,但不建议同时对两者进行修改。 */
464
+ temperature?: number;
465
+ /** 作为调节采样温度的替代方案,模型会考虑前 top_p 概率的 token 的结果(默认:1)
466
+ * - 所以 0.1 就意味着只有包括在最高 10% 概率中的 token 会被考虑。
467
+ * - 我们通常建议修改这个值或者更改 temperature,但不建议同时对两者进行修改 */
468
+ top_p?: number;
469
+ /** 模型可能会调用的 tool 的列表。
470
+ * - 目前,仅支持 function 作为工具。
471
+ * - 使用此参数来提供以 JSON 作为输入参数的 function 列表。最多支持 128 个 function */
472
+ tools?: Array<ToolCall>;
473
+ /** 控制模型调用 tool 的行为
474
+ * - none 意味着模型不会调用任何 tool,而是生成一条消息。
475
+ * - auto 意味着模型可以选择生成一条消息或调用一个或多个 tool。
476
+ * - required 意味着模型必须调用一个或多个 tool。
477
+ * - 通过 {"type": "function", "function": {"name": "my_function"}} 指定特定 tool,会强制模型调用该 tool。
478
+ * - 当没有 tool 时,默认值为 none。如果有 tool 存在,默认值为 auto。
479
+ */
480
+ tool_choice?: "none" | "auto" | "required" | {
481
+ /** 类型 */
482
+ type: "function" | string;
483
+ /** 要调用的函数 */
484
+ function: {
485
+ /** 要调用的函数名称 */
486
+ name: string;
487
+ };
488
+ };
489
+ /** 是否返回所输出 token 的对数概率。如果为 true,则在 message 的 content 中返回每个输出 token 的对数概率 */
490
+ logprobs?: boolean;
491
+ /** 一个介于 0 到 20 之间的整数 N,指定每个输出位置返回输出概率 top N 的 token,且返回这些 token 的对数概率。指定此参数时,logprobs 必须为 true */
492
+ top_logprobs?: number;
493
+ /** 采样种子(Anthropic 不支持) */
494
+ seed?: number;
495
+ }
496
+ /**
497
+ * OpenAI Chat API响应结构
498
+ * 参考: https://platform.openai.com/docs/api-reference/chat/object
499
+ */
500
+ interface ChatResponse extends ErrorReponse {
501
+ /** 唯一标识符(由服务端生成) */
502
+ id: string;
503
+ /** 对象类型,固定为 "chat.completion" */
504
+ object: "chat.completion";
505
+ /** 响应创建时的 Unix 时间戳(秒) */
506
+ created: number;
507
+ /** 使用的模型名称 */
508
+ model: string;
509
+ /** 系统指纹(用于识别服务端配置,调试用) */
510
+ system_fingerprint?: string;
511
+ /** 生成的回答选项列表(通常只有一个,但可配置n参数生成多个) */
512
+ choices: Array<Choice>;
513
+ /** Token 使用统计 */
514
+ usage: Usage;
515
+ }
516
+ /** 回答选项结构 */
517
+ interface Choice {
518
+ /** 选项索引(当n>1时用于区分不同回答) */
519
+ index: number;
520
+ /** 消息内容(助手回复的消息对象) */
521
+ message: AssistantMessage;
522
+ /** 日志概率信息(需要设置logprobs参数,默认null) */
523
+ logprobs: LogProbs | null;
524
+ /** 结束原因
525
+ * - stop:模型自然停止生成,或遇到 stop 序列中列出的字符串
526
+ * - length :输出长度达到了模型上下文长度限制,或达到了 max_tokens 的限制
527
+ * - content_filter:输出内容因触发过滤策略而被过滤
528
+ * - insufficient_system_resource:系统推理资源不足,生成被打断
529
+ */
530
+ finish_reason: "stop" | "length" | "content_filter" | "tool_calls" | "insufficient_system_resource";
531
+ }
532
+ /** 日志概率结构 */
533
+ interface LogProbs {
534
+ /** 一个包含输出 token 对数概率信息的列表 */
535
+ content: Array<{
536
+ /** 输出的 token */
537
+ token: string;
538
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
539
+ logprob: number;
540
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
541
+ bytes: Array<number>;
542
+ /** 一个包含在该输出位置上,输出概率 top N 的 token 的列表,以及它们的对数概率。在罕见情况下,返回的 token 数量可能少于请求参数中指定的 top_logprobs 值 */
543
+ top_logprobs: Array<{
544
+ /** 输出的 token */
545
+ token: string;
546
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
547
+ logprob: number;
548
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
549
+ bytes: Array<number>;
550
+ }>;
551
+ }>;
552
+ /** 一个包含输出 token 对数概率信息的列表 */
553
+ reasoning_content: Array<{
554
+ /** 输出的 token */
555
+ token: string;
556
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
557
+ logprob: number;
558
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
559
+ bytes: Array<number>;
560
+ /** 一个包含在该输出位置上,输出概率 top N 的 token 的列表,以及它们的对数概率。在罕见情况下,返回的 token 数量可能少于请求参数中指定的 top_logprobs 值 */
561
+ top_logprobs: Array<{
562
+ /** 输出的 token */
563
+ token: string;
564
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
565
+ logprob: number;
566
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
567
+ bytes: Array<number>;
568
+ }>;
569
+ }>;
570
+ }
571
+ /** Token使用统计 */
572
+ interface Usage {
573
+ /** 模型 completion 产生的 token 数 */
574
+ completion_tokens: number;
575
+ /** 用户 prompt 所包含的 token 数。该值等于 prompt_cache_hit_tokens + prompt_cache_miss_tokens */
576
+ prompt_tokens: number;
577
+ /** 用户 prompt 中,命中上下文缓存的 token 数 */
578
+ prompt_cache_hit_tokens?: number;
579
+ /** 用户 prompt 中,未命中上下文缓存的 token 数 */
580
+ prompt_cache_miss_tokens?: number;
581
+ /** 该请求中,所有 token 的数量(prompt + completion) */
582
+ total_tokens: number;
583
+ /** 回答token详细信息(DeepSeek 扩展) */
584
+ completion_tokens_details?: {
585
+ /** 推理(思考)过程消耗的 token 数量 */
586
+ reasoning_tokens?: number;
587
+ };
588
+ /** 提示token详情(非标准但常见) */
589
+ prompt_tokens_details?: {
590
+ /** 命中缓存的 token 数量 */
591
+ cached_tokens?: number;
592
+ };
593
+ }
594
+ /** 错误响应 */
595
+ interface ErrorReponse {
596
+ /** 错误信息 */
597
+ error: {
598
+ /** 错误码 */
599
+ code: string;
600
+ /** 错误类型 */
601
+ type: string;
602
+ /** 错误消息 */
603
+ message: string;
604
+ /** 错误参数 */
605
+ param: string | null;
606
+ };
607
+ }
608
+ }
609
+ /** Google Gemini接口 */
610
+ namespace Gemini {
611
+ /**
612
+ * Google Gemini 平台配置
613
+ * 参考: https://ai.google.dev/api
614
+ * 注意: 参数在官方 SDK 中嵌套于 generation_config 内
615
+ */
616
+ interface Config extends LLMUtility.Config {
617
+ /** 类型 */
618
+ type: "Gemini";
619
+ /** Top-K 采样 (1~40),OpenAI/DeepSeek 无此参数 */
620
+ topK?: number;
621
+ /** 候选数量 (1~8),等同于 OpenAI n */
622
+ candidateCount?: number;
623
+ /** 输出格式: text 或 JSON 模式 */
624
+ responseFormat?: "text" | "json_object";
625
+ /** 思考预算 (Gemini 3) */
626
+ thinkingConfig?: {
627
+ includeThoughts?: boolean;
628
+ thinkingBudget?: number;
629
+ };
630
+ /** 安全过滤阈值 */
631
+ safetySettings?: Array<{
632
+ category: string;
633
+ threshold: string;
634
+ }>;
635
+ /** 输出模态 */
636
+ responseModalities?: Array<"TEXT" | "IMAGE">;
637
+ }
638
+ /** 消息数据 */
639
+ interface MessageData {
640
+ role?: "user" | "model";
641
+ parts: Array<Part>;
642
+ }
643
+ /** 消息数据零件 */
644
+ type Part = TextPart | InlineDataPart | FileDataPart | FunctionCallPart | FunctionResponsePart | ThoughtPart;
645
+ /** 文本零件 */
646
+ interface TextPart {
647
+ text: string;
648
+ thought?: boolean;
649
+ }
650
+ /** 内联数据零件 */
651
+ interface InlineDataPart {
652
+ inlineData: {
653
+ mimeType: string;
654
+ data: string;
655
+ };
656
+ }
657
+ /** 文件数据零件 */
658
+ interface FileDataPart {
659
+ fileData: {
660
+ mimeType: string;
661
+ fileUri: string;
662
+ };
663
+ }
664
+ /** 函数调用零件(响应) */
665
+ interface FunctionCallPart {
666
+ functionCall: {
667
+ name: string;
668
+ args: Record<string, unknown>;
669
+ };
670
+ }
671
+ /** 函数响应零件(请求) */
672
+ interface FunctionResponsePart {
673
+ functionResponse: {
674
+ name: string;
675
+ response: Record<string, unknown>;
676
+ };
677
+ }
678
+ /** 思考零件 */
679
+ interface ThoughtPart {
680
+ thought: boolean;
681
+ text?: undefined;
682
+ }
683
+ /** 生成配置 */
684
+ interface GenerationConfig {
685
+ temperature?: number;
686
+ topP?: number;
687
+ topK?: number;
688
+ candidateCount?: number;
689
+ maxOutputTokens?: number;
690
+ stopSequences?: Array<string>;
691
+ responseMimeType?: string;
692
+ responseSchema?: Record<string, unknown>;
693
+ }
694
+ /** 安全设置 */
695
+ interface SafetySetting {
696
+ category: string;
697
+ threshold: "BLOCK_NONE" | "BLOCK_ONLY_HIGH" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_LOW_AND_ABOVE" | string;
698
+ }
699
+ /** 工具定义 */
700
+ interface Tool {
701
+ functionDeclarations?: Array<FunctionDeclaration>;
702
+ codeExecution?: object;
703
+ googleSearch?: object;
704
+ }
705
+ /** 函数声明 */
706
+ interface FunctionDeclaration {
707
+ name: string;
708
+ description?: string;
709
+ parameters?: {
710
+ type: "object";
711
+ properties?: Record<string, unknown>;
712
+ required?: Array<string>;
713
+ };
714
+ }
715
+ /** 工具配置 */
716
+ interface ToolConfig {
717
+ functionCallingConfig?: {
718
+ mode: "AUTO" | "ANY" | "NONE";
719
+ allowedFunctionNames?: Array<string>;
720
+ };
721
+ }
722
+ /** generateContent API请求结构 */
723
+ interface ChatRequest {
724
+ contents: Array<MessageData>;
725
+ systemInstruction?: {
726
+ parts: Array<Part>;
727
+ };
728
+ generationConfig?: GenerationConfig;
729
+ safetySettings?: Array<SafetySetting>;
730
+ tools?: Array<Tool>;
731
+ toolConfig?: ToolConfig;
732
+ }
733
+ /** 候选 */
734
+ interface Candidate {
735
+ content: MessageData;
736
+ finishReason?: "STOP" | "MAX_TOKENS" | "SAFETY" | "RECITATION" | "OTHER";
737
+ safetyRatings?: Array<SafetyRating>;
738
+ tokenCount?: number;
739
+ index?: number;
740
+ }
741
+ /** 安全评级 */
742
+ interface SafetyRating {
743
+ category: string;
744
+ probability: "NEGLIGIBLE" | "LOW" | "MEDIUM" | "HIGH";
745
+ blocked?: boolean;
746
+ }
747
+ /** Token用量 */
748
+ interface UsageMetadata {
749
+ promptTokenCount: number;
750
+ candidatesTokenCount: number;
751
+ totalTokenCount: number;
752
+ cachedContentTokenCount?: number;
753
+ }
754
+ /** generateContent API响应结构 */
755
+ interface ChatResponse {
756
+ responseId?: string;
757
+ candidates: Array<Candidate>;
758
+ usageMetadata?: UsageMetadata;
759
+ modelVersion?: string;
760
+ promptFeedback?: {
761
+ blockReason?: string;
762
+ safetyRatings?: Array<SafetyRating>;
763
+ };
764
+ }
765
+ }
766
+ /** OpenAI接口 */
767
+ namespace OpenAI {
768
+ /**
769
+ * OpenAI 平台配置
770
+ * 参考: https://developers.openai.com/api/reference
771
+ */
772
+ interface Config extends LLMUtility.Config {
773
+ /** 类型 */
774
+ type: "OpenAI";
775
+ /** 推理强度 (o-series / gpt-5),null 为禁用 */
776
+ reasoningEffort?: "none" | "minimal" | "low" | "medium" | "high" | "xhigh";
777
+ /** 结构化输出: json_schema 需配合 jsonSchema 字段 */
778
+ responseFormat?: "text" | "json_object" | "json_schema";
779
+ /** JSON Schema 定义(配合 responseFormat: "json_schema") */
780
+ jsonSchema?: {
781
+ name: string;
782
+ schema: Record<string, unknown>;
783
+ strict?: boolean;
784
+ };
785
+ /** 流式附带 usage 信息 */
786
+ streamOptions?: {
787
+ include_usage?: boolean;
788
+ };
789
+ /** 多模态输出 */
790
+ modalities?: Array<"text" | "audio">;
791
+ /** 存储 completion 供蒸馏/评估 */
792
+ store?: boolean;
793
+ /** 处理优先级 */
794
+ serviceTier?: "auto" | "default" | "flex" | "priority";
795
+ /** 安全审计标识(替代已弃用的 user) */
796
+ safetyIdentifier?: string;
797
+ /** 联网搜索(Beta) */
798
+ webSearchOptions?: {
799
+ searchContextSize?: "low" | "medium" | "high";
800
+ userLocation?: object;
801
+ };
802
+ /** 禁用并行工具调用 */
803
+ parallelToolCalls?: boolean;
804
+ }
805
+ /** 消息结构 */
806
+ interface MessageData extends LLMUtility.MessageData {
807
+ /**
808
+ * 消息角色
809
+ * 系统-system、助手-assistant、用户-user、工具-tool
810
+ */
811
+ role: "system" | "user" | "assistant" | "tool";
812
+ /** 消息内容 */
813
+ content: string | null;
814
+ }
815
+ /** 系统消息 */
816
+ interface SystemMessage extends MessageData {
817
+ role: "system";
818
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
819
+ name?: string;
820
+ }
821
+ /** 用户消息 */
822
+ interface UserMessage extends MessageData {
823
+ role: "user";
824
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
825
+ name?: string;
826
+ }
827
+ /** 助手消息 */
828
+ interface AssistantMessage extends MessageData {
829
+ role: "assistant";
830
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
831
+ name?: string;
832
+ /**
833
+ * (Beta)设置此参数为 true,来强制模型在其回答中以此 assistant 消息中提供的前缀内容开始
834
+ * 注意:必须设置 base_url="https://api.deepseek.com/beta" 来使用此功能
835
+ */
836
+ prefix?: boolean;
837
+ /** 模型的思考内容(thinking mode时返回) */
838
+ thinking?: string;
839
+ /** 推理内容(Beta DeepSeek扩展,当启用thinking模式时返回)用于思考模式下在对话前缀续写功能下,作为最后一条 assistant 思维链内容的输入。使用此功能时,prefix 参数必须设置为 true */
840
+ reasoning_content?: string;
841
+ /** 模型生成的 tool 调用 */
842
+ tool_calls?: Array<ToolResponse>;
843
+ }
844
+ /** 工具消息 */
845
+ interface ToolMessage extends MessageData {
846
+ role: "tool";
847
+ /** 此消息所响应的 tool call 的 ID */
848
+ tool_call_id: string;
849
+ }
850
+ /** 工具调用函数(请求时为工具定义无 id,响应时有 id) */
851
+ interface ToolCall {
852
+ /** 工具调用唯一标识(仅响应) */
853
+ id?: string;
854
+ /** 调用类型,固定为"function" */
855
+ type: "function";
856
+ /** 函数调用详情 */
857
+ function: {
858
+ /** 函数名称 */
859
+ name: string;
860
+ /** 函数描述 */
861
+ description?: string;
862
+ /** 函数参数 */
863
+ parameters: JSONSchema;
864
+ /** 如果设置为 true,API 将在函数调用中使用 strict 模式,以确保输出始终符合函数的 JSON schema 定义。该功能为 Beta 功能 */
865
+ strict?: boolean;
866
+ };
867
+ }
868
+ /** 工具响应 */
869
+ interface ToolResponse {
870
+ /** 索引 */
871
+ index: number;
872
+ /** tool 调用的 ID */
873
+ id: string;
874
+ /** tool 的类型 */
875
+ type: "function";
876
+ /** 模型调用的 function */
877
+ function: {
878
+ /** 模型调用的 function 名 */
879
+ name: string;
880
+ /** 要调用的 function 的参数,由模型生成,格式为 JSON。请注意,模型并不总是生成有效的 JSON,并且可能会臆造出你函数模式中未定义的参数。在调用函数之前,请在代码中验证这些参数 */
881
+ arguments: string;
882
+ };
883
+ }
884
+ /** OpenAI Chat API请求结构 */
885
+ interface ChatRequest {
886
+ /** 模型 */
887
+ model: string;
888
+ /** 消息 */
889
+ messages: Array<MessageData>;
890
+ /** 思考模式开关(默认思考开关为 enabled) */
891
+ thinking: {
892
+ type: "enabled" | "disabled";
893
+ };
894
+ /**
895
+ * 控制模型的推理强度
896
+ * - 对普通请求,默认为 high
897
+ * - 对一些复杂 Agent 类请求(如 Claude Code、OpenCode),自动设置为 max
898
+ * - 出于兼容考虑 low、medium 会映射为 high, xhigh 会映射为 max
899
+ */
900
+ reasoning_effort?: "none" | "minimal" | "low" | "medium" | "high" | "xhigh";
901
+ /** 惩罚频率(-2.0 和 2.0 之间的数字)
902
+ * - 如果该值为正,那么新 token 会根据其在已有文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性
903
+ */
904
+ frequency_penalty?: number;
905
+ /** 限制一次请求中模型生成 completion 的最大 token 数。输入 token 和输出 token 的总长度受模型的上下文长度的限制 */
906
+ max_tokens?: number;
907
+ /** 实时惩罚(-2.0 和 2.0 之间的数字)
908
+ * - 如果该值为正,那么新 token 会根据其是否已在已有文本中出现受到相应的惩罚,从而增加模型谈论新主题的可能性。
909
+ */
910
+ presence_penalty?: number;
911
+ /** 指定模型必须输出的格式
912
+ * - 设置为 { "type": "json_object" } 以启用 JSON 模式,该模式保证模型生成的消息是有效的 JSON。
913
+ * - 注意: 使用 JSON 模式时,你还必须通过系统或用户消息指示模型生成 JSON。否则,模型可能会生成不断的空白字符,直到生成达到令牌限制,从而导致请求长时间运行并显得“卡住”。此外,如果 finish_reason="length",这表示生成超过了 max_tokens 或对话超过了最大上下文长度,消息内容可能会被部分截断。
914
+ */
915
+ response_format?: {
916
+ /** 类型(默认:text) */
917
+ type: "text" | "json_object";
918
+ };
919
+ /** 停止词
920
+ * - 在遇到这些词时,API将停止生成更多的token */
921
+ stop?: string | Array<string>;
922
+ /** 如果设置为 True,将会以 SSE(server-sent events)的形式以流式发送消息增量。消息流以 data: [DONE] 结尾。 */
923
+ stream?: boolean;
924
+ /** 流式输出相关选项(只有在stream参数为true时,才可设置此参数) */
925
+ stream_options?: {
926
+ /** 如果设置为 true,在流式消息最后的 data: [DONE] 之前将会传输一个额外的块。此块上的 usage 字段显示整个请求的 token 使用统计信息,而 choices 字段将始终是一个空数组。所有其他块也将包含一个 usage 字段,但其值为 null */
927
+ include_usage: boolean;
928
+ };
929
+ /** 采样温度(0 - 2之间,默认:1)
930
+ * - 更高的值,如 0.8,会使输出更随机,而更低的值,如 0.2,会使其更加集中和确定。 我们通常建议可以更改这个值或者更改 top_p,但不建议同时对两者进行修改。 */
931
+ temperature?: number;
932
+ /** 作为调节采样温度的替代方案,模型会考虑前 top_p 概率的 token 的结果(默认:1)
933
+ * - 所以 0.1 就意味着只有包括在最高 10% 概率中的 token 会被考虑。
934
+ * - 我们通常建议修改这个值或者更改 temperature,但不建议同时对两者进行修改 */
935
+ top_p?: number;
936
+ /** 模型可能会调用的 tool 的列表。
937
+ * - 目前,仅支持 function 作为工具。
938
+ * - 使用此参数来提供以 JSON 作为输入参数的 function 列表。最多支持 128 个 function */
939
+ tools?: Array<ToolCall>;
940
+ /** 控制模型调用 tool 的行为
941
+ * - none 意味着模型不会调用任何 tool,而是生成一条消息。
942
+ * - auto 意味着模型可以选择生成一条消息或调用一个或多个 tool。
943
+ * - required 意味着模型必须调用一个或多个 tool。
944
+ * - 通过 {"type": "function", "function": {"name": "my_function"}} 指定特定 tool,会强制模型调用该 tool。
945
+ * - 当没有 tool 时,默认值为 none。如果有 tool 存在,默认值为 auto。
946
+ */
947
+ tool_choice?: "none" | "auto" | "required" | {
948
+ /** 类型 */
949
+ type: "function" | string;
950
+ /** 要调用的函数 */
951
+ function: {
952
+ /** 要调用的函数名称 */
953
+ name: string;
954
+ };
955
+ };
956
+ /** 是否返回所输出 token 的对数概率。如果为 true,则在 message 的 content 中返回每个输出 token 的对数概率 */
957
+ logprobs?: boolean;
958
+ /** 一个介于 0 到 20 之间的整数 N,指定每个输出位置返回输出概率 top N 的 token,且返回这些 token 的对数概率。指定此参数时,logprobs 必须为 true */
959
+ top_logprobs?: number;
960
+ /** 采样种子(Anthropic 不支持) */
961
+ seed?: number;
962
+ }
963
+ /**
964
+ * OpenAI Chat API响应结构
965
+ * 参考: https://platform.openai.com/docs/api-reference/chat/object
966
+ */
967
+ interface ChatResponse extends ErrorReponse {
968
+ /** 唯一标识符(由服务端生成) */
969
+ id: string;
970
+ /** 对象类型,固定为 "chat.completion" */
971
+ object: "chat.completion";
972
+ /** 响应创建时的 Unix 时间戳(秒) */
973
+ created: number;
974
+ /** 使用的模型名称 */
975
+ model: string;
976
+ /** 系统指纹(用于识别服务端配置,调试用) */
977
+ system_fingerprint?: string;
978
+ /** 生成的回答选项列表(通常只有一个,但可配置n参数生成多个) */
979
+ choices: Array<Choice>;
980
+ /** Token 使用统计 */
981
+ usage: Usage;
982
+ }
983
+ /** 回答选项结构 */
984
+ interface Choice {
985
+ /** 选项索引(当n>1时用于区分不同回答) */
986
+ index: number;
987
+ /** 消息内容(助手回复的消息对象) */
988
+ message: AssistantMessage;
989
+ /** 日志概率信息(需要设置logprobs参数,默认null) */
990
+ logprobs: LogProbs | null;
991
+ /** 结束原因
992
+ * - stop:模型自然停止生成,或遇到 stop 序列中列出的字符串
993
+ * - length :输出长度达到了模型上下文长度限制,或达到了 max_tokens 的限制
994
+ * - content_filter:输出内容因触发过滤策略而被过滤
995
+ * - insufficient_system_resource:系统推理资源不足,生成被打断
996
+ */
997
+ finish_reason: "stop" | "length" | "content_filter" | "tool_calls" | "insufficient_system_resource";
998
+ }
999
+ /** 日志概率结构 */
1000
+ interface LogProbs {
1001
+ /** 一个包含输出 token 对数概率信息的列表 */
1002
+ content: Array<{
1003
+ /** 输出的 token */
1004
+ token: string;
1005
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1006
+ logprob: number;
1007
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1008
+ bytes: Array<number>;
1009
+ /** 一个包含在该输出位置上,输出概率 top N 的 token 的列表,以及它们的对数概率。在罕见情况下,返回的 token 数量可能少于请求参数中指定的 top_logprobs 值 */
1010
+ top_logprobs: Array<{
1011
+ /** 输出的 token */
1012
+ token: string;
1013
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1014
+ logprob: number;
1015
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1016
+ bytes: Array<number>;
1017
+ }>;
1018
+ }>;
1019
+ /** 一个包含输出 token 对数概率信息的列表 */
1020
+ reasoning_content: Array<{
1021
+ /** 输出的 token */
1022
+ token: string;
1023
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1024
+ logprob: number;
1025
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1026
+ bytes: Array<number>;
1027
+ /** 一个包含在该输出位置上,输出概率 top N 的 token 的列表,以及它们的对数概率。在罕见情况下,返回的 token 数量可能少于请求参数中指定的 top_logprobs 值 */
1028
+ top_logprobs: Array<{
1029
+ /** 输出的 token */
1030
+ token: string;
1031
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1032
+ logprob: number;
1033
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1034
+ bytes: Array<number>;
1035
+ }>;
1036
+ }>;
1037
+ }
1038
+ /** Token使用统计 */
1039
+ interface Usage {
1040
+ /** 模型 completion 产生的 token 数 */
1041
+ completion_tokens: number;
1042
+ /** 用户 prompt 所包含的 token 数。该值等于 prompt_cache_hit_tokens + prompt_cache_miss_tokens */
1043
+ prompt_tokens: number;
1044
+ /** 用户 prompt 中,命中上下文缓存的 token 数 */
1045
+ prompt_cache_hit_tokens?: number;
1046
+ /** 用户 prompt 中,未命中上下文缓存的 token 数 */
1047
+ prompt_cache_miss_tokens?: number;
1048
+ /** 该请求中,所有 token 的数量(prompt + completion) */
1049
+ total_tokens: number;
1050
+ /** 回答token详细信息(DeepSeek 扩展) */
1051
+ completion_tokens_details?: {
1052
+ /** 推理(思考)过程消耗的 token 数量 */
1053
+ reasoning_tokens?: number;
1054
+ };
1055
+ /** 提示token详情(非标准但常见) */
1056
+ prompt_tokens_details?: {
1057
+ /** 命中缓存的 token 数量 */
1058
+ cached_tokens?: number;
1059
+ };
1060
+ }
1061
+ /** 错误响应 */
1062
+ interface ErrorReponse {
1063
+ /** 错误信息 */
1064
+ error: {
1065
+ /** 错误码 */
1066
+ code: string;
1067
+ /** 错误类型 */
1068
+ type: string;
1069
+ /** 错误消息 */
1070
+ message: string;
1071
+ /** 错误参数 */
1072
+ param: string | null;
1073
+ };
1074
+ }
1075
+ }
1076
+ /** 通义千问接口 */
1077
+ namespace QWen {
1078
+ /**
1079
+ * 通义千问 (DashScope) 平台配置
1080
+ * 参考: https://help.aliyun.com/zh/model-studio/getting-started/models
1081
+ * 注意: Qwen API 与 OpenAI 兼容,使用相同的消息格式
1082
+ */
1083
+ interface Config extends LLMUtility.Config {
1084
+ /** 类型 */
1085
+ type: "QWen";
1086
+ /** 联网搜索 (仅 qwen-max/qwen-plus 等支持) */
1087
+ enableSearch?: boolean;
1088
+ /** 高分辨率图像 (视觉模型) */
1089
+ vlHighResolutionImages?: boolean;
1090
+ /** 重复惩罚 (0~5),减少模型重复输出的倾向 */
1091
+ repetitionPenalty?: number;
1092
+ /** 思考模式: enabled 开启 / disabled 关闭 (Qwen3 系列) */
1093
+ thinking?: {
1094
+ type: "enabled" | "disabled";
1095
+ };
1096
+ /** 输出格式: text 或 JSON 模式 */
1097
+ responseFormat?: "text" | "json_object";
1098
+ }
1099
+ /** 消息结构 */
1100
+ interface MessageData extends LLMUtility.MessageData {
1101
+ /**
1102
+ * 消息角色
1103
+ * 系统-system、助手-assistant、用户-user、工具-tool
1104
+ */
1105
+ role: "system" | "user" | "assistant" | "tool";
1106
+ /** 消息内容 */
1107
+ content: string;
1108
+ }
1109
+ /** 系统消息 */
1110
+ interface SystemMessage extends MessageData {
1111
+ role: "system";
1112
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
1113
+ name?: string;
1114
+ }
1115
+ /** 用户消息 */
1116
+ interface UserMessage extends MessageData {
1117
+ role: "user";
1118
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
1119
+ name?: string;
1120
+ }
1121
+ /** 助手消息 */
1122
+ interface AssistantMessage extends MessageData {
1123
+ role: "assistant";
1124
+ /** (选填)参与者的名称,为模型提供信息以区分相同角色的参与者 */
1125
+ name?: string;
1126
+ /**
1127
+ * (Beta)设置此参数为 true,来强制模型在其回答中以此 assistant 消息中提供的前缀内容开始
1128
+ * 注意:必须设置 base_url="https://api.deepseek.com/beta" 来使用此功能
1129
+ */
1130
+ prefix?: boolean;
1131
+ /** 模型的思考内容(thinking mode时返回) */
1132
+ thinking?: string;
1133
+ /** 推理内容(Beta DeepSeek扩展,当启用thinking模式时返回)用于思考模式下在对话前缀续写功能下,作为最后一条 assistant 思维链内容的输入。使用此功能时,prefix 参数必须设置为 true */
1134
+ reasoning_content?: string;
1135
+ /** 模型生成的 tool 调用 */
1136
+ tool_calls?: Array<ToolResponse>;
1137
+ }
1138
+ /** 工具消息 */
1139
+ interface ToolMessage extends MessageData {
1140
+ role: "tool";
1141
+ /** 此消息所响应的 tool call 的 ID */
1142
+ tool_call_id: string;
1143
+ }
1144
+ /** 工具调用函数(请求时为工具定义无 id,响应时有 id) */
1145
+ interface ToolCall {
1146
+ /** 工具调用唯一标识(仅响应) */
1147
+ id?: string;
1148
+ /** 调用类型,固定为"function" */
1149
+ type: "function";
1150
+ /** 函数调用详情 */
1151
+ function: {
1152
+ /** 函数名称 */
1153
+ name: string;
1154
+ /** 函数描述 */
1155
+ description?: string;
1156
+ /** 函数参数 */
1157
+ parameters: JSONSchema;
1158
+ /** 如果设置为 true,API 将在函数调用中使用 strict 模式,以确保输出始终符合函数的 JSON schema 定义。该功能为 Beta 功能 */
1159
+ strict?: boolean;
1160
+ };
1161
+ }
1162
+ /** 工具响应 */
1163
+ interface ToolResponse {
1164
+ /** 索引 */
1165
+ index: number;
1166
+ /** tool 调用的 ID */
1167
+ id: string;
1168
+ /** tool 的类型 */
1169
+ type: "function";
1170
+ /** 模型调用的 function */
1171
+ function: {
1172
+ /** 模型调用的 function 名 */
1173
+ name: string;
1174
+ /** 要调用的 function 的参数,由模型生成,格式为 JSON。请注意,模型并不总是生成有效的 JSON,并且可能会臆造出你函数模式中未定义的参数。在调用函数之前,请在代码中验证这些参数 */
1175
+ arguments: string;
1176
+ };
1177
+ }
1178
+ /** OpenAI Chat API请求结构 */
1179
+ interface ChatRequest {
1180
+ model: string;
1181
+ messages: Array<MessageData>;
1182
+ thinking?: {
1183
+ type: "enabled" | "disabled";
1184
+ };
1185
+ enable_search?: boolean;
1186
+ search_options?: {
1187
+ enable_search_by_google?: boolean;
1188
+ enable_search_by_quark?: boolean;
1189
+ };
1190
+ vl_high_resolution_images?: boolean;
1191
+ repetition_penalty?: number;
1192
+ response_format?: {
1193
+ type: "text" | "json_object";
1194
+ };
1195
+ temperature?: number;
1196
+ top_p?: number;
1197
+ max_tokens?: number;
1198
+ stop?: string | Array<string>;
1199
+ stream?: boolean;
1200
+ stream_options?: {
1201
+ include_usage: boolean;
1202
+ };
1203
+ frequency_penalty?: number;
1204
+ presence_penalty?: number;
1205
+ tools?: Array<OpenAI.ToolCall>;
1206
+ tool_choice?: "none" | "auto" | "required" | {
1207
+ type: "function";
1208
+ function: {
1209
+ name: string;
1210
+ };
1211
+ };
1212
+ seed?: number;
1213
+ }
1214
+ /**
1215
+ * OpenAI Chat API响应结构
1216
+ * 参考: https://platform.openai.com/docs/api-reference/chat/object
1217
+ */
1218
+ interface ChatResponse extends ErrorReponse {
1219
+ /** 唯一标识符(由服务端生成) */
1220
+ id: string;
1221
+ /** 对象类型,固定为 "chat.completion" */
1222
+ object: "chat.completion";
1223
+ /** 响应创建时的 Unix 时间戳(秒) */
1224
+ created: number;
1225
+ /** 使用的模型名称 */
1226
+ model: string;
1227
+ /** 系统指纹(用于识别服务端配置,调试用) */
1228
+ system_fingerprint?: string;
1229
+ /** 生成的回答选项列表(通常只有一个,但可配置n参数生成多个) */
1230
+ choices: Array<Choice>;
1231
+ /** Token 使用统计 */
1232
+ usage: Usage;
1233
+ }
1234
+ /** 回答选项结构 */
1235
+ interface Choice {
1236
+ /** 选项索引(当n>1时用于区分不同回答) */
1237
+ index: number;
1238
+ /** 消息内容(助手回复的消息对象) */
1239
+ message: AssistantMessage;
1240
+ /** 日志概率信息(需要设置logprobs参数,默认null) */
1241
+ logprobs: LogProbs | null;
1242
+ /** 结束原因
1243
+ * - stop:模型自然停止生成,或遇到 stop 序列中列出的字符串
1244
+ * - length :输出长度达到了模型上下文长度限制,或达到了 max_tokens 的限制
1245
+ * - content_filter:输出内容因触发过滤策略而被过滤
1246
+ * - insufficient_system_resource:系统推理资源不足,生成被打断
1247
+ */
1248
+ finish_reason: "stop" | "length" | "content_filter" | "tool_calls" | "insufficient_system_resource";
1249
+ }
1250
+ /** 日志概率结构 */
1251
+ interface LogProbs {
1252
+ /** 一个包含输出 token 对数概率信息的列表 */
1253
+ content: Array<{
1254
+ /** 输出的 token */
1255
+ token: string;
1256
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1257
+ logprob: number;
1258
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1259
+ bytes: Array<number>;
1260
+ /** 一个包含在该输出位置上,输出概率 top N 的 token 的列表,以及它们的对数概率。在罕见情况下,返回的 token 数量可能少于请求参数中指定的 top_logprobs 值 */
1261
+ top_logprobs: Array<{
1262
+ /** 输出的 token */
1263
+ token: string;
1264
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1265
+ logprob: number;
1266
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1267
+ bytes: Array<number>;
1268
+ }>;
1269
+ }>;
1270
+ /** 一个包含输出 token 对数概率信息的列表 */
1271
+ reasoning_content: Array<{
1272
+ /** 输出的 token */
1273
+ token: string;
1274
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1275
+ logprob: number;
1276
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1277
+ bytes: Array<number>;
1278
+ /** 一个包含在该输出位置上,输出概率 top N 的 token 的列表,以及它们的对数概率。在罕见情况下,返回的 token 数量可能少于请求参数中指定的 top_logprobs 值 */
1279
+ top_logprobs: Array<{
1280
+ /** 输出的 token */
1281
+ token: string;
1282
+ /** 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中 */
1283
+ logprob: number;
1284
+ /** 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null */
1285
+ bytes: Array<number>;
1286
+ }>;
1287
+ }>;
1288
+ }
1289
+ /** Token使用统计 */
1290
+ interface Usage {
1291
+ /** 模型 completion 产生的 token 数 */
1292
+ completion_tokens: number;
1293
+ /** 用户 prompt 所包含的 token 数。该值等于 prompt_cache_hit_tokens + prompt_cache_miss_tokens */
1294
+ prompt_tokens: number;
1295
+ /** 用户 prompt 中,命中上下文缓存的 token 数 */
1296
+ prompt_cache_hit_tokens?: number;
1297
+ /** 用户 prompt 中,未命中上下文缓存的 token 数 */
1298
+ prompt_cache_miss_tokens?: number;
1299
+ /** 该请求中,所有 token 的数量(prompt + completion) */
1300
+ total_tokens: number;
1301
+ /** 回答token详细信息(DeepSeek 扩展) */
1302
+ completion_tokens_details?: {
1303
+ /** 推理(思考)过程消耗的 token 数量 */
1304
+ reasoning_tokens?: number;
1305
+ };
1306
+ /** 提示token详情(非标准但常见) */
1307
+ prompt_tokens_details?: {
1308
+ /** 命中缓存的 token 数量 */
1309
+ cached_tokens?: number;
1310
+ };
1311
+ }
1312
+ /** 错误响应 */
1313
+ interface ErrorReponse {
1314
+ /** 错误信息 */
1315
+ error: {
1316
+ /** 错误码 */
1317
+ code: string;
1318
+ /** 错误类型 */
1319
+ type: string;
1320
+ /** 错误消息 */
1321
+ message: string;
1322
+ /** 错误参数 */
1323
+ param: string | null;
1324
+ };
1325
+ }
1326
+ }
1327
+ }
1328
+ //# sourceMappingURL=LLMUtility.d.ts.map