@huyooo/ai-chat-core 0.3.7 → 0.3.9

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 (222) hide show
  1. package/dist/adapter/index.d.ts +0 -1
  2. package/dist/adapter/model-adapter.d.ts +2 -1
  3. package/dist/adapter/model-options.d.ts +0 -1
  4. package/dist/adapter/types.d.ts +0 -1
  5. package/dist/chat-runtime.d.ts +0 -1
  6. package/dist/constants.d.ts +0 -1
  7. package/dist/events.d.ts +0 -1
  8. package/dist/extension/index.d.ts +0 -1
  9. package/dist/extension/types.d.ts +0 -1
  10. package/dist/families/index.d.ts +0 -1
  11. package/dist/families/presets.d.ts +0 -1
  12. package/dist/families/resolver.d.ts +0 -1
  13. package/dist/families/types.d.ts +0 -1
  14. package/dist/governance/command-safety.d.ts +0 -1
  15. package/dist/governance/governance.d.ts +0 -1
  16. package/dist/governance/index.d.ts +0 -1
  17. package/dist/governance/types.d.ts +0 -1
  18. package/dist/index.d.ts +1 -2
  19. package/dist/index.js +9 -9
  20. package/dist/internal/management-args.d.ts +0 -1
  21. package/dist/internal/management-results.d.ts +0 -1
  22. package/dist/llm-config.d.ts +8 -3
  23. package/dist/logger/core.d.ts +0 -1
  24. package/dist/logger/index.d.ts +0 -1
  25. package/dist/orchestrator/compression-handler.d.ts +0 -1
  26. package/dist/orchestrator/context-compressor.d.ts +0 -1
  27. package/dist/orchestrator/context-summarizer.d.ts +0 -1
  28. package/dist/orchestrator/index.d.ts +0 -1
  29. package/dist/orchestrator/orchestrator.d.ts +0 -1
  30. package/dist/orchestrator/types.d.ts +0 -1
  31. package/dist/parts/index.d.ts +0 -1
  32. package/dist/parts/registry.d.ts +0 -1
  33. package/dist/parts/summaries.d.ts +0 -1
  34. package/dist/parts/types.d.ts +0 -1
  35. package/dist/platform.d.ts +0 -1
  36. package/dist/protocols/anthropic.d.ts +0 -1
  37. package/dist/protocols/ark.d.ts +0 -1
  38. package/dist/protocols/deepseek.d.ts +0 -1
  39. package/dist/protocols/error-utils.d.ts +0 -1
  40. package/dist/protocols/gemini.d.ts +0 -1
  41. package/dist/protocols/glm.d.ts +0 -1
  42. package/dist/protocols/grok.d.ts +0 -1
  43. package/dist/protocols/index.d.ts +2 -1
  44. package/dist/protocols/minimax.d.ts +0 -1
  45. package/dist/protocols/moonshot.d.ts +0 -1
  46. package/dist/protocols/openai-chat.d.ts +18 -0
  47. package/dist/protocols/openai-sse.d.ts +0 -1
  48. package/dist/protocols/openai.d.ts +0 -1
  49. package/dist/protocols/qwen.d.ts +0 -1
  50. package/dist/protocols/responses-sse.d.ts +0 -1
  51. package/dist/protocols/sse-reader.d.ts +0 -1
  52. package/dist/protocols/tool-arguments.d.ts +0 -1
  53. package/dist/protocols/types.d.ts +2 -2
  54. package/dist/protocols/vercel-gateway.d.ts +0 -1
  55. package/dist/runtime.d.ts +0 -1
  56. package/dist/skills/index.d.ts +0 -1
  57. package/dist/skills/management/admin.d.ts +0 -1
  58. package/dist/skills/management/index.d.ts +0 -1
  59. package/dist/skills/management/inputs.d.ts +0 -1
  60. package/dist/skills/management/operations.d.ts +0 -1
  61. package/dist/skills/management/types.d.ts +0 -1
  62. package/dist/skills/registry.d.ts +0 -1
  63. package/dist/skills/summaries.d.ts +0 -1
  64. package/dist/skills/types.d.ts +0 -1
  65. package/dist/test-utils/mock-sse.d.ts +0 -1
  66. package/dist/tool-manager/define-tool.d.ts +0 -1
  67. package/dist/tool-manager/formats.d.ts +0 -1
  68. package/dist/tool-manager/identity.d.ts +0 -1
  69. package/dist/tool-manager/in-process-provider.d.ts +0 -1
  70. package/dist/tool-manager/index.d.ts +0 -1
  71. package/dist/tool-manager/manager.d.ts +0 -1
  72. package/dist/tool-manager/mcp-provider.d.ts +0 -1
  73. package/dist/tool-manager/summaries.d.ts +0 -1
  74. package/dist/tool-manager/types.d.ts +0 -1
  75. package/dist/types.d.ts +0 -1
  76. package/package.json +2 -3
  77. package/dist/adapter/index.d.ts.map +0 -1
  78. package/dist/adapter/model-adapter.d.ts.map +0 -1
  79. package/dist/adapter/model-options.d.ts.map +0 -1
  80. package/dist/adapter/types.d.ts.map +0 -1
  81. package/dist/chat-runtime.d.ts.map +0 -1
  82. package/dist/constants.d.ts.map +0 -1
  83. package/dist/events.d.ts.map +0 -1
  84. package/dist/extension/index.d.ts.map +0 -1
  85. package/dist/extension/types.d.ts.map +0 -1
  86. package/dist/families/index.d.ts.map +0 -1
  87. package/dist/families/presets.d.ts.map +0 -1
  88. package/dist/families/resolver.d.ts.map +0 -1
  89. package/dist/families/types.d.ts.map +0 -1
  90. package/dist/governance/command-safety.d.ts.map +0 -1
  91. package/dist/governance/governance.d.ts.map +0 -1
  92. package/dist/governance/index.d.ts.map +0 -1
  93. package/dist/governance/types.d.ts.map +0 -1
  94. package/dist/index.d.ts.map +0 -1
  95. package/dist/internal/management-args.d.ts.map +0 -1
  96. package/dist/internal/management-results.d.ts.map +0 -1
  97. package/dist/llm-config.d.ts.map +0 -1
  98. package/dist/logger/core.d.ts.map +0 -1
  99. package/dist/logger/index.d.ts.map +0 -1
  100. package/dist/orchestrator/compression-handler.d.ts.map +0 -1
  101. package/dist/orchestrator/context-compressor.d.ts.map +0 -1
  102. package/dist/orchestrator/context-summarizer.d.ts.map +0 -1
  103. package/dist/orchestrator/index.d.ts.map +0 -1
  104. package/dist/orchestrator/orchestrator.d.ts.map +0 -1
  105. package/dist/orchestrator/types.d.ts.map +0 -1
  106. package/dist/parts/index.d.ts.map +0 -1
  107. package/dist/parts/registry.d.ts.map +0 -1
  108. package/dist/parts/summaries.d.ts.map +0 -1
  109. package/dist/parts/types.d.ts.map +0 -1
  110. package/dist/platform.d.ts.map +0 -1
  111. package/dist/protocols/anthropic.d.ts.map +0 -1
  112. package/dist/protocols/ark.d.ts.map +0 -1
  113. package/dist/protocols/deepseek.d.ts.map +0 -1
  114. package/dist/protocols/error-utils.d.ts.map +0 -1
  115. package/dist/protocols/gemini.d.ts.map +0 -1
  116. package/dist/protocols/glm.d.ts.map +0 -1
  117. package/dist/protocols/grok.d.ts.map +0 -1
  118. package/dist/protocols/index.d.ts.map +0 -1
  119. package/dist/protocols/minimax.d.ts.map +0 -1
  120. package/dist/protocols/moonshot.d.ts.map +0 -1
  121. package/dist/protocols/openai-sse.d.ts.map +0 -1
  122. package/dist/protocols/openai.d.ts.map +0 -1
  123. package/dist/protocols/qwen.d.ts.map +0 -1
  124. package/dist/protocols/responses-sse.d.ts.map +0 -1
  125. package/dist/protocols/sse-reader.d.ts.map +0 -1
  126. package/dist/protocols/tool-arguments.d.ts.map +0 -1
  127. package/dist/protocols/types.d.ts.map +0 -1
  128. package/dist/protocols/vercel-gateway.d.ts.map +0 -1
  129. package/dist/runtime.d.ts.map +0 -1
  130. package/dist/skills/index.d.ts.map +0 -1
  131. package/dist/skills/management/admin.d.ts.map +0 -1
  132. package/dist/skills/management/index.d.ts.map +0 -1
  133. package/dist/skills/management/inputs.d.ts.map +0 -1
  134. package/dist/skills/management/operations.d.ts.map +0 -1
  135. package/dist/skills/management/types.d.ts.map +0 -1
  136. package/dist/skills/registry.d.ts.map +0 -1
  137. package/dist/skills/summaries.d.ts.map +0 -1
  138. package/dist/skills/types.d.ts.map +0 -1
  139. package/dist/test-utils/mock-sse.d.ts.map +0 -1
  140. package/dist/tool-manager/define-tool.d.ts.map +0 -1
  141. package/dist/tool-manager/formats.d.ts.map +0 -1
  142. package/dist/tool-manager/identity.d.ts.map +0 -1
  143. package/dist/tool-manager/in-process-provider.d.ts.map +0 -1
  144. package/dist/tool-manager/index.d.ts.map +0 -1
  145. package/dist/tool-manager/manager.d.ts.map +0 -1
  146. package/dist/tool-manager/mcp-provider.d.ts.map +0 -1
  147. package/dist/tool-manager/summaries.d.ts.map +0 -1
  148. package/dist/tool-manager/types.d.ts.map +0 -1
  149. package/dist/types.d.ts.map +0 -1
  150. package/src/adapter/index.ts +0 -25
  151. package/src/adapter/model-adapter.ts +0 -196
  152. package/src/adapter/model-options.ts +0 -143
  153. package/src/adapter/types.ts +0 -41
  154. package/src/chat-runtime.ts +0 -515
  155. package/src/constants.ts +0 -32
  156. package/src/events.ts +0 -1084
  157. package/src/extension/index.ts +0 -24
  158. package/src/extension/types.ts +0 -49
  159. package/src/families/index.ts +0 -28
  160. package/src/families/presets.ts +0 -124
  161. package/src/families/resolver.ts +0 -22
  162. package/src/families/types.ts +0 -55
  163. package/src/governance/command-safety.ts +0 -224
  164. package/src/governance/governance.ts +0 -125
  165. package/src/governance/index.ts +0 -38
  166. package/src/governance/types.ts +0 -44
  167. package/src/index.ts +0 -426
  168. package/src/internal/management-args.ts +0 -39
  169. package/src/internal/management-results.ts +0 -60
  170. package/src/llm-config.ts +0 -137
  171. package/src/logger/core.ts +0 -96
  172. package/src/logger/index.ts +0 -8
  173. package/src/orchestrator/compression-handler.ts +0 -137
  174. package/src/orchestrator/context-compressor.ts +0 -249
  175. package/src/orchestrator/context-summarizer.ts +0 -123
  176. package/src/orchestrator/index.ts +0 -20
  177. package/src/orchestrator/orchestrator.ts +0 -1002
  178. package/src/orchestrator/types.ts +0 -70
  179. package/src/parts/index.ts +0 -20
  180. package/src/parts/registry.ts +0 -95
  181. package/src/parts/summaries.ts +0 -40
  182. package/src/parts/types.ts +0 -63
  183. package/src/platform.ts +0 -73
  184. package/src/protocols/anthropic.ts +0 -377
  185. package/src/protocols/ark.ts +0 -300
  186. package/src/protocols/deepseek.ts +0 -192
  187. package/src/protocols/error-utils.ts +0 -71
  188. package/src/protocols/gemini.ts +0 -352
  189. package/src/protocols/glm.ts +0 -212
  190. package/src/protocols/grok.ts +0 -98
  191. package/src/protocols/index.ts +0 -48
  192. package/src/protocols/minimax.ts +0 -308
  193. package/src/protocols/moonshot.ts +0 -186
  194. package/src/protocols/openai-sse.ts +0 -156
  195. package/src/protocols/openai.ts +0 -97
  196. package/src/protocols/qwen.ts +0 -358
  197. package/src/protocols/responses-sse.ts +0 -224
  198. package/src/protocols/sse-reader.ts +0 -54
  199. package/src/protocols/tool-arguments.ts +0 -32
  200. package/src/protocols/types.ts +0 -198
  201. package/src/protocols/vercel-gateway.ts +0 -391
  202. package/src/runtime.ts +0 -167
  203. package/src/skills/index.ts +0 -29
  204. package/src/skills/management/admin.ts +0 -170
  205. package/src/skills/management/index.ts +0 -27
  206. package/src/skills/management/inputs.ts +0 -79
  207. package/src/skills/management/operations.ts +0 -256
  208. package/src/skills/management/types.ts +0 -57
  209. package/src/skills/registry.ts +0 -120
  210. package/src/skills/summaries.ts +0 -48
  211. package/src/skills/types.ts +0 -65
  212. package/src/test-utils/mock-sse.ts +0 -32
  213. package/src/tool-manager/define-tool.ts +0 -201
  214. package/src/tool-manager/formats.ts +0 -146
  215. package/src/tool-manager/identity.ts +0 -80
  216. package/src/tool-manager/in-process-provider.ts +0 -164
  217. package/src/tool-manager/index.ts +0 -63
  218. package/src/tool-manager/manager.ts +0 -562
  219. package/src/tool-manager/mcp-provider.ts +0 -509
  220. package/src/tool-manager/summaries.ts +0 -136
  221. package/src/tool-manager/types.ts +0 -389
  222. package/src/types.ts +0 -1142
@@ -1,192 +0,0 @@
1
- /**
2
- * DeepSeek Protocol(原生 Chat Completions API)
3
- *
4
- * 基于 OpenAI 兼容格式:
5
- * - 端点:https://api.deepseek.com/chat/completions
6
- * - thinking:{ type: "enabled" }
7
- * - SSE delta:reasoning_content(思考)+ content(正文)
8
- * - 工具调用循环内需回传 reasoning_content
9
- * - finish_reason 额外值:content_filter / insufficient_system_resource
10
- *
11
- * 文档:https://api-docs.deepseek.com/zh-cn/api/create-chat-completion
12
- */
13
-
14
- import type {
15
- Protocol,
16
- ProtocolConfig,
17
- ProtocolMessage,
18
- ProtocolToolDefinition,
19
- ProtocolRequestOptions,
20
- RawEvent,
21
- } from './types';
22
- import { createModuleLogger } from '../logger';
23
- import { friendlyHttpError } from './error-utils';
24
- import { readSSEJsonStream } from './sse-reader';
25
- import { mapOpenAIStream } from './openai-sse';
26
- import type { OpenAIStreamConfig } from './openai-sse';
27
-
28
- const logger = createModuleLogger('DeepSeekProtocol');
29
-
30
- const DEFAULT_DEEPSEEK_URL = 'https://api.deepseek.com';
31
-
32
- const DEEPSEEK_SSE_CONFIG: OpenAIStreamConfig = {
33
- thinkingField: (delta) => delta.reasoning_content as string | undefined,
34
- errorFinishReasons: ['content_filter', 'insufficient_system_resource'],
35
- protocolName: 'DeepSeek',
36
- parseUsage: (usage) => ({
37
- promptTokens: (usage.prompt_tokens as number) ?? 0,
38
- completionTokens: (usage.completion_tokens as number) ?? 0,
39
- totalTokens: (usage.total_tokens as number) ?? 0,
40
- reasoningTokens: (usage.completion_tokens_details as Record<string, number> | undefined)?.reasoning_tokens ?? 0,
41
- cachedTokens: (usage.prompt_cache_hit_tokens as number) ?? 0,
42
- }),
43
- };
44
-
45
- export class DeepSeekProtocol implements Protocol {
46
- readonly name = 'deepseek';
47
-
48
- private apiKey: string;
49
- private apiUrl: string;
50
-
51
- constructor(config: ProtocolConfig) {
52
- this.apiKey = config.apiKey;
53
- this.apiUrl = config.apiUrl ?? DEFAULT_DEEPSEEK_URL;
54
- }
55
-
56
- async *stream(
57
- messages: ProtocolMessage[],
58
- tools: ProtocolToolDefinition[],
59
- options: ProtocolRequestOptions
60
- ): AsyncGenerator<RawEvent> {
61
- const requestBody = this.buildRequestBody(messages, tools, options);
62
- const url = `${this.apiUrl}/chat/completions`;
63
-
64
- logger.debug({
65
- url,
66
- model: options.model,
67
- enableThinking: options.enableThinking,
68
- toolsCount: tools.length,
69
- }, '发送 DeepSeek 请求');
70
-
71
- const response = await fetch(url, {
72
- method: 'POST',
73
- headers: {
74
- 'Authorization': `Bearer ${this.apiKey}`,
75
- 'Content-Type': 'application/json',
76
- },
77
- body: JSON.stringify(requestBody),
78
- signal: options.signal,
79
- });
80
-
81
- if (!response.ok) {
82
- const errorText = await response.text();
83
- logger.error({ status: response.status, body: errorText.slice(0, 500) }, 'DeepSeek API 错误');
84
- yield { type: 'error', error: friendlyHttpError(response.status, errorText, 'DeepSeek') };
85
- return;
86
- }
87
-
88
- const reader = response.body?.getReader();
89
- if (!reader) {
90
- yield { type: 'error', error: '无法获取响应流' };
91
- return;
92
- }
93
-
94
- yield* mapOpenAIStream(readSSEJsonStream(reader), DEEPSEEK_SSE_CONFIG);
95
- }
96
-
97
- private buildRequestBody(
98
- messages: ProtocolMessage[],
99
- tools: ProtocolToolDefinition[],
100
- options: ProtocolRequestOptions
101
- ): Record<string, unknown> {
102
- const convertedMessages = this.convertMessages(messages);
103
-
104
- const body: Record<string, unknown> = {
105
- model: options.model,
106
- messages: convertedMessages,
107
- stream: true,
108
- stream_options: { include_usage: true },
109
- max_tokens: options.maxOutputTokens,
110
- };
111
-
112
- // DeepSeek thinking 参数格式与 GLM 一致
113
- if (options.enableThinking) {
114
- body.thinking = { type: 'enabled' };
115
- }
116
-
117
- if (tools.length > 0) {
118
- body.tools = tools.map(t => ({
119
- type: 'function',
120
- function: {
121
- name: t.name,
122
- description: t.description,
123
- parameters: t.parameters,
124
- },
125
- }));
126
- }
127
-
128
- return body;
129
- }
130
-
131
- private convertMessages(messages: ProtocolMessage[]): unknown[] {
132
- const result: unknown[] = [];
133
-
134
- for (const msg of messages) {
135
- switch (msg.role) {
136
- case 'system':
137
- result.push({ role: 'system', content: msg.content });
138
- break;
139
-
140
- case 'user': {
141
- const textContent = msg.content || (msg.images?.length ? '请分析这张图片' : '');
142
- if (msg.images?.length) {
143
- const content: unknown[] = [{ type: 'text', text: textContent }];
144
- for (const img of msg.images) {
145
- content.push({
146
- type: 'image_url',
147
- image_url: { url: img.startsWith('data:') ? img : `data:image/jpeg;base64,${img}` },
148
- });
149
- }
150
- result.push({ role: 'user', content });
151
- } else {
152
- result.push({ role: 'user', content: textContent });
153
- }
154
- break;
155
- }
156
-
157
- case 'assistant':
158
- if (msg.toolCalls?.length) {
159
- // 工具调用循环内需回传 reasoning_content 给 API
160
- result.push({
161
- role: 'assistant',
162
- content: msg.content || null,
163
- ...(msg.thinkingContent ? { reasoning_content: msg.thinkingContent } : {}),
164
- tool_calls: msg.toolCalls.map(tc => ({
165
- id: tc.id,
166
- type: 'function',
167
- function: { name: tc.name, arguments: tc.arguments },
168
- })),
169
- });
170
- } else {
171
- result.push({ role: 'assistant', content: msg.content });
172
- }
173
- break;
174
-
175
- case 'tool':
176
- result.push({
177
- role: 'tool',
178
- tool_call_id: msg.toolCallId,
179
- content: msg.content,
180
- });
181
- break;
182
- }
183
- }
184
-
185
- return result;
186
- }
187
-
188
- }
189
-
190
- export function createDeepSeekProtocol(config: ProtocolConfig): DeepSeekProtocol {
191
- return new DeepSeekProtocol(config);
192
- }
@@ -1,71 +0,0 @@
1
- /**
2
- * Protocol 错误处理工具
3
- *
4
- * 将各种 API 错误(HTTP status + JSON body / SDK 异常)转换为用户友好的消息。
5
- */
6
-
7
- /** HTTP 状态码 → 友好消息 */
8
- const STATUS_MESSAGES: Record<number, string> = {
9
- 400: '请求参数错误',
10
- 401: 'API 认证失败,请检查 API Key 配置',
11
- 402: '账户余额不足,请充值后重试',
12
- 403: '没有权限访问此模型',
13
- 404: '请求的模型或接口不存在',
14
- 429: '请求过于频繁,请稍后重试',
15
- 500: 'API 服务器内部错误,请稍后重试',
16
- 502: 'API 网关错误,请稍后重试',
17
- 503: 'API 服务暂时不可用,请稍后重试',
18
- 504: 'API 请求超时,请稍后重试',
19
- };
20
-
21
- /**
22
- * 从 HTTP 响应中提取用户友好的错误消息
23
- *
24
- * @param status HTTP 状态码
25
- * @param body 原始响应体字符串
26
- * @param providerName 供应商名称(如 "Gemini"、"OpenAI")
27
- */
28
- export function friendlyHttpError(status: number, body: string, providerName: string): string {
29
- const parsed = tryParseErrorBody(body);
30
- const apiMessage = parsed?.message;
31
- const statusMessage = STATUS_MESSAGES[status];
32
-
33
- // API 返回了具体错误信息:始终附加,截断过长内容防止 UI 溢出
34
- if (apiMessage) {
35
- const trimmed = apiMessage.length > 300 ? apiMessage.slice(0, 300) + '...' : apiMessage;
36
- const prefix = statusMessage ?? `${providerName} 错误 (${status})`;
37
- return `${prefix}(${trimmed})`;
38
- }
39
-
40
- // 无 API 消息:已知 status 用友好文案,未知 status 兜底
41
- return statusMessage ?? `${providerName} 错误 (${status})`;
42
- }
43
-
44
- /**
45
- * 尝试从 JSON 响应体中提取错误消息
46
- *
47
- * 各供应商实测错误格式(2026-03 验证):
48
- * { "error": { "message": "..." } } — OpenAI / DeepSeek / Gemini / Claude / GLM / ARK / Moonshot
49
- * { "error": "字符串" } — Grok(error 直接是字符串,code 在顶层)
50
- * { "error_msg": "..." } — ARK 部分旧接口
51
- * { "message": "..." } — Qwen / DashScope(顶层 message + code)
52
- * HTML / 非 JSON — MiniMax 某些错误,返回 null 用 status 兜底
53
- */
54
- function tryParseErrorBody(body: string): { message?: string; type?: string } | null {
55
- try {
56
- const json = JSON.parse(body);
57
- if (json?.error?.message) {
58
- return { message: json.error.message, type: json.error.type };
59
- }
60
- if (typeof json?.error === 'string') {
61
- return { message: json.error, type: json.code };
62
- }
63
- if (typeof json?.error_msg === 'string') {
64
- return { message: json.error_msg };
65
- }
66
- if (typeof json?.message === 'string') {
67
- return { message: json.message };
68
- }
69
- } catch { /* not JSON */ }
70
- return null;
71
- }
@@ -1,352 +0,0 @@
1
- /**
2
- * Gemini Protocol(Google 原生 API)
3
- *
4
- * 支持两种部署模式:
5
- * - 直连 Google API:apiUrl 含 googleapis.com,Key 通过 URL 参数传递
6
- * - CF Worker 代理:apiUrl 指向代理地址(如 https://proxy/google-ai-studio/v1beta),
7
- * Key 通过 Authorization 头传递,代理负责注入厂商 Key
8
- */
9
-
10
- import type {
11
- Protocol,
12
- ProtocolConfig,
13
- ProtocolMessage,
14
- ProtocolToolDefinition,
15
- ProtocolRequestOptions,
16
- RawEvent,
17
- RawOutputPart,
18
- RawToolCall,
19
- } from './types';
20
- import { createModuleLogger } from '../logger';
21
- import { friendlyHttpError } from './error-utils';
22
- import { readSSEJsonStream } from './sse-reader';
23
- import { parseProtocolToolArguments } from './tool-arguments';
24
-
25
- const logger = createModuleLogger('GeminiProtocol');
26
-
27
- const DEFAULT_GEMINI_URL = 'https://generativelanguage.googleapis.com/v1beta';
28
-
29
- /**
30
- * 历史中无 thoughtSignature 时使用的占位值(跳过 Gemini 校验)。
31
- * 见 https://ai.google.dev/gemini-api/docs/thought-signatures
32
- */
33
- const THOUGHT_SIGNATURE_DUMMY = 'skip_thought_signature_validator';
34
-
35
- export class GeminiProtocol implements Protocol {
36
- readonly name = 'gemini';
37
-
38
- private apiKey: string;
39
- private apiUrl: string;
40
-
41
- constructor(config: ProtocolConfig) {
42
- this.apiKey = config.apiKey;
43
- this.apiUrl = config.apiUrl ?? DEFAULT_GEMINI_URL;
44
- }
45
-
46
- async *stream(
47
- messages: ProtocolMessage[],
48
- tools: ProtocolToolDefinition[],
49
- options: ProtocolRequestOptions,
50
- ): AsyncGenerator<RawEvent> {
51
- const requestBody = buildRequestBody(messages, tools, options);
52
-
53
- // 直连 Google:Key 走 URL 参数;代理模式:Key 走 Authorization 头
54
- const isDirect = this.apiUrl.includes('googleapis.com');
55
- const url = isDirect
56
- ? `${this.apiUrl}/models/${options.model}:streamGenerateContent?key=${this.apiKey}&alt=sse`
57
- : `${this.apiUrl}/models/${options.model}:streamGenerateContent?alt=sse`;
58
-
59
- logger.debug({
60
- url: url.replace(this.apiKey, '***'),
61
- model: options.model,
62
- enableThinking: options.enableThinking,
63
- toolsCount: tools.length,
64
- }, 'Gemini 请求');
65
-
66
- const headers: Record<string, string> = { 'Content-Type': 'application/json' };
67
- if (!isDirect) {
68
- headers['Authorization'] = `Bearer ${this.apiKey}`;
69
- }
70
-
71
- const response = await fetch(url, {
72
- method: 'POST',
73
- headers,
74
- body: JSON.stringify(requestBody),
75
- signal: options.signal,
76
- });
77
-
78
- if (!response.ok) {
79
- const errorText = await response.text();
80
- logger.error({ status: response.status, body: errorText.slice(0, 500) }, 'Gemini API 错误');
81
- yield { type: 'error', error: friendlyHttpError(response.status, errorText, 'Gemini') };
82
- return;
83
- }
84
-
85
- const reader = response.body?.getReader();
86
- if (!reader) {
87
- yield { type: 'error', error: '无法获取响应流' };
88
- return;
89
- }
90
-
91
- yield* parseGeminiSSE(reader);
92
- }
93
- }
94
-
95
- // ==================== 请求体构建 ====================
96
-
97
- function buildRequestBody(
98
- messages: ProtocolMessage[],
99
- tools: ProtocolToolDefinition[],
100
- options: ProtocolRequestOptions,
101
- ): Record<string, unknown> {
102
- const { systemInstruction, contents } = convertMessages(messages);
103
-
104
- const body: Record<string, unknown> = {
105
- contents,
106
- generationConfig: {
107
- maxOutputTokens: options.maxOutputTokens,
108
- },
109
- };
110
-
111
- if (systemInstruction) {
112
- body.systemInstruction = systemInstruction;
113
- }
114
-
115
- if (options.enableThinking) {
116
- (body.generationConfig as Record<string, unknown>).thinkingConfig = {
117
- thinkingBudget: 24576,
118
- includeThoughts: true,
119
- };
120
- }
121
-
122
- if (tools.length > 0) {
123
- const byName = new Map<string, unknown>();
124
- for (const t of tools) {
125
- if (!byName.has(t.name)) {
126
- byName.set(t.name, {
127
- name: t.name,
128
- description: t.description,
129
- // 使用 parametersJsonSchema 而非 parameters:
130
- // parameters 走 protobuf Schema 校验,字段受限(不支持 additionalProperties 等);
131
- // parametersJsonSchema 直接接受原始 JSON Schema,绕过 proto 限制。
132
- // 二者互斥,见 https://ai.google.dev/api/caching#FunctionDeclaration
133
- parametersJsonSchema: stripUnsupportedRefs(t.parameters),
134
- });
135
- }
136
- }
137
- body.tools = [{ functionDeclarations: Array.from(byName.values()) }];
138
- }
139
-
140
- return body;
141
- }
142
-
143
- // ==================== Schema 兼容 ====================
144
- //
145
- // 使用 parametersJsonSchema 而非 parameters 传递工具参数 schema。
146
- //
147
- // FunctionDeclaration 有两种互斥方式传参数 schema:
148
- // 1. parameters — 走 protobuf Schema 对象,字段严格受限
149
- // (不支持 additionalProperties、prefixItems、$ref/$defs 等)
150
- // 2. parametersJsonSchema — 直接接受原始 JSON Schema(google.protobuf.Value)
151
- // 绕过 proto 限制,支持 additionalProperties、oneOf、allOf、const 等标准特性
152
- //
153
- // 我们选方案 2,TypeBox 输出的标准 JSON Schema 几乎可以直接透传。
154
- //
155
- // 唯一已知限制:$ref 不被支持(googleapis/python-genai#1122)
156
- // 副作用:$ref/$defs 被递归剥离 → Type.Ref() / Type.Recursive() 的引用丢失,
157
- // 被引用的位置变为无约束的空 schema,AI 靠 description 补偿。
158
-
159
- /**
160
- * 递归剥离 $ref/$defs——Gemini parametersJsonSchema 唯一不支持的 JSON Schema 特性。
161
- * 其它所有标准 JSON Schema 关键字(const、oneOf、allOf、additionalProperties、
162
- * prefixItems 等)均直接透传,无需转换。
163
- */
164
- export function stripUnsupportedRefs(schema: unknown): unknown {
165
- if (schema == null || typeof schema !== 'object') return schema;
166
- if (Array.isArray(schema)) return schema.map(stripUnsupportedRefs);
167
-
168
- const obj = schema as Record<string, unknown>;
169
- const result: Record<string, unknown> = {};
170
-
171
- for (const [key, value] of Object.entries(obj)) {
172
- if (key === '$ref' || key === '$defs') continue;
173
- result[key] = stripUnsupportedRefs(value);
174
- }
175
-
176
- return result;
177
- }
178
-
179
- // ==================== 消息转换 ====================
180
-
181
- function convertMessages(messages: ProtocolMessage[]): {
182
- systemInstruction?: { parts: { text: string }[] };
183
- contents: unknown[];
184
- } {
185
- let systemInstruction: { parts: { text: string }[] } | undefined;
186
- const contents: unknown[] = [];
187
-
188
- for (const msg of messages) {
189
- switch (msg.role) {
190
- case 'system':
191
- systemInstruction = { parts: [{ text: msg.content }] };
192
- break;
193
-
194
- case 'user': {
195
- const hasMedia = Boolean(msg.images?.length || msg.attachments?.length);
196
- const textContent = msg.content || (hasMedia ? '请分析这个媒体文件' : '');
197
- const parts: unknown[] = [{ text: textContent }];
198
- if (msg.images?.length) {
199
- for (const img of msg.images) {
200
- if (img.startsWith('data:')) {
201
- const match = img.match(/^data:([^;]+);base64,(.+)$/);
202
- if (match) {
203
- parts.push({ inlineData: { mimeType: match[1], data: match[2] } });
204
- }
205
- } else {
206
- parts.push({ inlineData: { mimeType: 'image/jpeg', data: img } });
207
- }
208
- }
209
- }
210
- if (msg.attachments?.length) {
211
- for (const attachment of msg.attachments) {
212
- parts.push({ fileData: { mimeType: attachment.mimeType, fileUri: attachment.fileUri } });
213
- }
214
- }
215
- contents.push({ role: 'user', parts });
216
- break;
217
- }
218
-
219
- case 'assistant':
220
- if (msg.toolCalls?.length) {
221
- const parts: unknown[] = [];
222
- for (const tc of msg.toolCalls) {
223
- const funcPart: Record<string, unknown> = {
224
- functionCall: {
225
- name: tc.name,
226
- args: parseProtocolToolArguments(tc.arguments, {
227
- protocol: 'gemini',
228
- toolCallId: tc.id,
229
- toolName: tc.name,
230
- }),
231
- },
232
- };
233
- // Gemini 3:每个 functionCall part 必须带 thoughtSignature,否则 400
234
- funcPart.thoughtSignature = tc.thoughtSignature ?? THOUGHT_SIGNATURE_DUMMY;
235
- parts.push(funcPart);
236
- }
237
- contents.push({ role: 'model', parts });
238
- } else {
239
- contents.push({ role: 'model', parts: [{ text: msg.content }] });
240
- }
241
- break;
242
-
243
- case 'tool':
244
- contents.push({
245
- role: 'user',
246
- parts: [{
247
- functionResponse: {
248
- name: msg.toolName || 'unknown',
249
- response: { result: msg.content },
250
- },
251
- }],
252
- });
253
- break;
254
- }
255
- }
256
-
257
- return { systemInstruction, contents };
258
- }
259
-
260
- // ==================== SSE 流解析 ====================
261
-
262
- /**
263
- * 解析 Gemini SSE 流
264
- *
265
- * Layer 1 负责 bytes → JSON,此处做 Gemini 原生格式的语义映射。
266
- */
267
- async function* parseGeminiSSE(
268
- reader: ReadableStreamDefaultReader<Uint8Array>,
269
- ): AsyncGenerator<RawEvent> {
270
- const pendingToolCalls = new Map<string, RawToolCall>();
271
- const outputParts: RawOutputPart[] = [];
272
- let textStarted = false;
273
- let toolCallIndex = 0;
274
-
275
- for await (const json of readSSEJsonStream(reader)) {
276
- const candidates = json.candidates as Array<Record<string, unknown>> | undefined;
277
- const candidate = candidates?.[0];
278
- const content = candidate?.content as Record<string, unknown> | undefined;
279
- const parts = content?.parts as Array<Record<string, unknown>> | undefined;
280
- if (!parts) continue;
281
-
282
- for (const part of parts) {
283
- if (part.text && part.thought === true) {
284
- yield { type: 'thinking_delta', delta: part.text as string };
285
- continue;
286
- }
287
-
288
- if (part.text) {
289
- outputParts.push({ type: 'text', text: part.text as string });
290
- if (!textStarted) {
291
- textStarted = true;
292
- yield { type: 'thinking_done' };
293
- }
294
- yield { type: 'text_delta', delta: part.text as string };
295
- }
296
-
297
- if (part.inlineData) {
298
- const inlineData = part.inlineData as Record<string, unknown>;
299
- const mimeType = inlineData.mimeType;
300
- const data = inlineData.data;
301
- if (typeof mimeType === 'string' && typeof data === 'string') {
302
- outputParts.push({ type: 'inline_data', mimeType, data });
303
- }
304
- }
305
-
306
- if (part.functionCall) {
307
- const fc = part.functionCall as Record<string, unknown>;
308
- const callId = `gemini-${toolCallIndex++}`;
309
- const toolCall: RawToolCall = {
310
- id: callId,
311
- name: fc.name as string,
312
- arguments: JSON.stringify(fc.args || {}),
313
- };
314
- if (part.thoughtSignature) {
315
- toolCall.thoughtSignature = part.thoughtSignature as string;
316
- }
317
- pendingToolCalls.set(callId, toolCall);
318
- yield { type: 'tool_call_start', toolCall: { id: callId, name: toolCall.name } };
319
- yield { type: 'tool_call_done', toolCall };
320
- }
321
- }
322
-
323
- if (candidate?.finishReason) {
324
- const meta = json.usageMetadata as Record<string, number> | undefined;
325
- const usage = meta ? {
326
- promptTokens: meta.promptTokenCount ?? 0,
327
- completionTokens: meta.candidatesTokenCount ?? 0,
328
- totalTokens: meta.totalTokenCount ?? 0,
329
- reasoningTokens: meta.thoughtsTokenCount ?? 0,
330
- cachedTokens: meta.cachedContentTokenCount ?? 0,
331
- } : undefined;
332
-
333
- yield {
334
- type: 'done',
335
- finishReason: pendingToolCalls.size > 0 ? 'tool_calls' : 'stop',
336
- usage,
337
- outputParts,
338
- };
339
- return;
340
- }
341
- }
342
-
343
- yield {
344
- type: 'done',
345
- finishReason: pendingToolCalls.size > 0 ? 'tool_calls' : 'stop',
346
- outputParts,
347
- };
348
- }
349
-
350
- export function createGeminiProtocol(config: ProtocolConfig): GeminiProtocol {
351
- return new GeminiProtocol(config);
352
- }