@huyooo/ai-chat-core 0.2.45 → 0.3.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/adapter/index.d.ts +11 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/adapter/model-adapter.d.ts +25 -0
- package/dist/adapter/model-adapter.d.ts.map +1 -0
- package/dist/adapter/model-options.d.ts +53 -0
- package/dist/adapter/model-options.d.ts.map +1 -0
- package/dist/adapter/types.d.ts +28 -0
- package/dist/adapter/types.d.ts.map +1 -0
- package/dist/chat-runtime.d.ts +96 -0
- package/dist/chat-runtime.d.ts.map +1 -0
- package/dist/constants.d.ts +12 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/events.d.ts +605 -1
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +1 -1
- package/dist/extension/index.d.ts +9 -0
- package/dist/extension/index.d.ts.map +1 -0
- package/dist/extension/types.d.ts +46 -0
- package/dist/extension/types.d.ts.map +1 -0
- package/dist/families/index.d.ts +11 -0
- package/dist/families/index.d.ts.map +1 -0
- package/dist/families/presets.d.ts +31 -0
- package/dist/families/presets.d.ts.map +1 -0
- package/dist/families/resolver.d.ts +11 -0
- package/dist/families/resolver.d.ts.map +1 -0
- package/dist/families/types.d.ts +29 -0
- package/dist/families/types.d.ts.map +1 -0
- package/dist/governance/command-safety.d.ts +34 -0
- package/dist/governance/command-safety.d.ts.map +1 -0
- package/dist/governance/governance.d.ts +19 -0
- package/dist/governance/governance.d.ts.map +1 -0
- package/dist/governance/index.d.ts +12 -0
- package/dist/governance/index.d.ts.map +1 -0
- package/dist/governance/types.d.ts +29 -0
- package/dist/governance/types.d.ts.map +1 -0
- package/dist/index.d.ts +72 -804
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -1
- package/dist/internal/management-args.d.ts +13 -0
- package/dist/internal/management-args.d.ts.map +1 -0
- package/dist/internal/management-results.d.ts +21 -0
- package/dist/internal/management-results.d.ts.map +1 -0
- package/dist/llm-config.d.ts +108 -0
- package/dist/llm-config.d.ts.map +1 -0
- package/dist/logger/core.d.ts +31 -0
- package/dist/logger/core.d.ts.map +1 -0
- package/dist/logger/index.d.ts +9 -0
- package/dist/logger/index.d.ts.map +1 -0
- package/dist/orchestrator/compression-handler.d.ts +29 -0
- package/dist/orchestrator/compression-handler.d.ts.map +1 -0
- package/dist/orchestrator/context-compressor.d.ts +51 -0
- package/dist/orchestrator/context-compressor.d.ts.map +1 -0
- package/dist/orchestrator/context-summarizer.d.ts +41 -0
- package/dist/orchestrator/context-summarizer.d.ts.map +1 -0
- package/dist/orchestrator/index.d.ts +12 -0
- package/dist/orchestrator/index.d.ts.map +1 -0
- package/dist/orchestrator/orchestrator.d.ts +46 -0
- package/dist/orchestrator/orchestrator.d.ts.map +1 -0
- package/dist/orchestrator/types.d.ts +58 -0
- package/dist/orchestrator/types.d.ts.map +1 -0
- package/dist/parts/index.d.ts +13 -0
- package/dist/parts/index.d.ts.map +1 -0
- package/dist/parts/registry.d.ts +11 -0
- package/dist/parts/registry.d.ts.map +1 -0
- package/dist/parts/summaries.d.ts +9 -0
- package/dist/parts/summaries.d.ts.map +1 -0
- package/dist/parts/types.d.ts +61 -0
- package/dist/parts/types.d.ts.map +1 -0
- package/dist/platform.d.ts +17 -0
- package/dist/platform.d.ts.map +1 -0
- package/dist/platform.js +1 -0
- package/dist/protocols/anthropic.d.ts +20 -0
- package/dist/protocols/anthropic.d.ts.map +1 -0
- package/dist/protocols/ark.d.ts +36 -0
- package/dist/protocols/ark.d.ts.map +1 -0
- package/dist/protocols/deepseek.d.ts +24 -0
- package/dist/protocols/deepseek.d.ts.map +1 -0
- package/dist/protocols/error-utils.d.ts +14 -0
- package/dist/protocols/error-utils.d.ts.map +1 -0
- package/dist/protocols/gemini.d.ts +24 -0
- package/dist/protocols/gemini.d.ts.map +1 -0
- package/dist/protocols/glm.d.ts +20 -0
- package/dist/protocols/glm.d.ts.map +1 -0
- package/dist/protocols/grok.d.ts +20 -0
- package/dist/protocols/grok.d.ts.map +1 -0
- package/dist/protocols/index.d.ts +31 -0
- package/dist/protocols/index.d.ts.map +1 -0
- package/dist/protocols/minimax.d.ts +38 -0
- package/dist/protocols/minimax.d.ts.map +1 -0
- package/dist/protocols/moonshot.d.ts +20 -0
- package/dist/protocols/moonshot.d.ts.map +1 -0
- package/dist/protocols/openai-sse.d.ts +33 -0
- package/dist/protocols/openai-sse.d.ts.map +1 -0
- package/dist/protocols/openai.d.ts +19 -0
- package/dist/protocols/openai.d.ts.map +1 -0
- package/dist/protocols/qwen.d.ts +26 -0
- package/dist/protocols/qwen.d.ts.map +1 -0
- package/dist/protocols/responses-sse.d.ts +30 -0
- package/dist/protocols/responses-sse.d.ts.map +1 -0
- package/dist/protocols/sse-reader.d.ts +23 -0
- package/dist/protocols/sse-reader.d.ts.map +1 -0
- package/dist/protocols/tool-arguments.d.ts +8 -0
- package/dist/protocols/tool-arguments.d.ts.map +1 -0
- package/dist/protocols/types.d.ts +148 -0
- package/dist/protocols/types.d.ts.map +1 -0
- package/dist/protocols/vercel-gateway.d.ts +15 -0
- package/dist/protocols/vercel-gateway.d.ts.map +1 -0
- package/dist/runtime.d.ts +151 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/skills/index.d.ts +14 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/management/admin.d.ts +10 -0
- package/dist/skills/management/admin.d.ts.map +1 -0
- package/dist/skills/management/index.d.ts +11 -0
- package/dist/skills/management/index.d.ts.map +1 -0
- package/dist/skills/management/inputs.d.ts +44 -0
- package/dist/skills/management/inputs.d.ts.map +1 -0
- package/dist/skills/management/operations.d.ts +78 -0
- package/dist/skills/management/operations.d.ts.map +1 -0
- package/dist/skills/management/types.d.ts +70 -0
- package/dist/skills/management/types.d.ts.map +1 -0
- package/dist/skills/registry.d.ts +37 -0
- package/dist/skills/registry.d.ts.map +1 -0
- package/dist/skills/summaries.d.ts +9 -0
- package/dist/skills/summaries.d.ts.map +1 -0
- package/dist/skills/types.d.ts +61 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/test-utils/mock-sse.d.ts +13 -0
- package/dist/test-utils/mock-sse.d.ts.map +1 -0
- package/dist/tool-manager/define-tool.d.ts +35 -0
- package/dist/tool-manager/define-tool.d.ts.map +1 -0
- package/dist/tool-manager/formats.d.ts +46 -0
- package/dist/tool-manager/formats.d.ts.map +1 -0
- package/dist/tool-manager/identity.d.ts +18 -0
- package/dist/tool-manager/identity.d.ts.map +1 -0
- package/dist/tool-manager/in-process-provider.d.ts +15 -0
- package/dist/tool-manager/in-process-provider.d.ts.map +1 -0
- package/dist/tool-manager/index.d.ts +18 -0
- package/dist/tool-manager/index.d.ts.map +1 -0
- package/dist/tool-manager/manager.d.ts +18 -0
- package/dist/tool-manager/manager.d.ts.map +1 -0
- package/dist/tool-manager/mcp-provider.d.ts +21 -0
- package/dist/tool-manager/mcp-provider.d.ts.map +1 -0
- package/dist/tool-manager/summaries.d.ts +39 -0
- package/dist/tool-manager/summaries.d.ts.map +1 -0
- package/dist/tool-manager/types.d.ts +314 -0
- package/dist/tool-manager/types.d.ts.map +1 -0
- package/dist/types.d.ts +663 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +26 -15
- package/src/adapter/index.ts +25 -0
- package/src/adapter/model-adapter.ts +196 -0
- package/src/adapter/model-options.ts +143 -0
- package/src/adapter/types.ts +41 -0
- package/src/chat-runtime.ts +515 -0
- package/src/constants.ts +9 -102
- package/src/events.ts +364 -150
- package/src/extension/index.ts +24 -0
- package/src/extension/types.ts +49 -0
- package/src/families/index.ts +28 -0
- package/src/families/presets.ts +124 -0
- package/src/families/resolver.ts +22 -0
- package/src/families/types.ts +55 -0
- package/src/governance/command-safety.ts +224 -0
- package/src/governance/governance.ts +125 -0
- package/src/governance/index.ts +38 -0
- package/src/governance/types.ts +44 -0
- package/src/index.ts +250 -145
- package/src/internal/management-args.ts +39 -0
- package/src/internal/management-results.ts +60 -0
- package/src/llm-config.ts +137 -0
- package/src/logger/core.ts +96 -0
- package/src/logger/index.ts +8 -0
- package/src/orchestrator/compression-handler.ts +137 -0
- package/src/{providers → orchestrator}/context-compressor.ts +79 -47
- package/src/orchestrator/context-summarizer.ts +123 -0
- package/src/orchestrator/index.ts +20 -0
- package/src/orchestrator/orchestrator.ts +1002 -0
- package/src/orchestrator/types.ts +70 -0
- package/src/parts/index.ts +20 -0
- package/src/parts/registry.ts +95 -0
- package/src/parts/summaries.ts +40 -0
- package/src/parts/types.ts +63 -0
- package/src/platform.ts +73 -0
- package/src/protocols/anthropic.ts +377 -0
- package/src/protocols/ark.ts +300 -0
- package/src/protocols/deepseek.ts +192 -0
- package/src/{providers/protocols → protocols}/error-utils.ts +17 -20
- package/src/protocols/gemini.ts +352 -0
- package/src/protocols/glm.ts +212 -0
- package/src/protocols/grok.ts +98 -0
- package/src/protocols/index.ts +48 -0
- package/src/protocols/minimax.ts +308 -0
- package/src/protocols/moonshot.ts +186 -0
- package/src/protocols/openai-sse.ts +156 -0
- package/src/protocols/openai.ts +97 -0
- package/src/protocols/qwen.ts +358 -0
- package/src/protocols/responses-sse.ts +224 -0
- package/src/protocols/sse-reader.ts +54 -0
- package/src/protocols/tool-arguments.ts +32 -0
- package/src/{providers/protocols → protocols}/types.ts +46 -37
- package/src/protocols/vercel-gateway.ts +391 -0
- package/src/runtime.ts +167 -0
- package/src/skills/index.ts +29 -0
- package/src/skills/management/admin.ts +170 -0
- package/src/skills/management/index.ts +27 -0
- package/src/skills/management/inputs.ts +79 -0
- package/src/skills/management/operations.ts +256 -0
- package/src/skills/management/types.ts +57 -0
- package/src/skills/registry.ts +120 -0
- package/src/skills/summaries.ts +48 -0
- package/src/skills/types.ts +65 -0
- package/src/test-utils/mock-sse.ts +3 -3
- package/src/tool-manager/define-tool.ts +201 -0
- package/src/tool-manager/formats.ts +146 -0
- package/src/tool-manager/identity.ts +80 -0
- package/src/tool-manager/in-process-provider.ts +164 -0
- package/src/tool-manager/index.ts +63 -0
- package/src/tool-manager/manager.ts +562 -0
- package/src/tool-manager/mcp-provider.ts +509 -0
- package/src/tool-manager/summaries.ts +136 -0
- package/src/tool-manager/types.ts +389 -0
- package/src/types.ts +750 -191
- package/dist/events-CU5D5ray.d.ts +0 -1128
- package/src/agent.ts +0 -409
- package/src/internal/update-plan.ts +0 -2
- package/src/internal/web-search.ts +0 -77
- package/src/mcp/client-manager.ts +0 -302
- package/src/mcp/index.ts +0 -2
- package/src/mcp/types.ts +0 -43
- package/src/providers/context-summarizer.ts +0 -70
- package/src/providers/index.ts +0 -125
- package/src/providers/model-registry.ts +0 -466
- package/src/providers/orchestrator.ts +0 -839
- package/src/providers/protocols/anthropic.ts +0 -406
- package/src/providers/protocols/ark.ts +0 -362
- package/src/providers/protocols/deepseek.ts +0 -344
- package/src/providers/protocols/gemini.ts +0 -350
- package/src/providers/protocols/index.ts +0 -36
- package/src/providers/protocols/openai.ts +0 -420
- package/src/providers/protocols/qwen.ts +0 -315
- package/src/providers/types.ts +0 -264
- package/src/providers/unified-adapter.ts +0 -367
- package/src/router.ts +0 -72
- package/src/tools.ts +0 -162
- package/src/utils.ts +0 -86
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Gemini Protocol(Google Gemini API)
|
|
3
|
-
*
|
|
4
|
-
* 只负责:
|
|
5
|
-
* - HTTP 请求发送
|
|
6
|
-
* - SSE 流解析
|
|
7
|
-
* - 原始事件产出
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
Protocol,
|
|
12
|
-
ProtocolConfig,
|
|
13
|
-
ProtocolMessage,
|
|
14
|
-
ProtocolToolDefinition,
|
|
15
|
-
ProtocolRequestOptions,
|
|
16
|
-
RawEvent,
|
|
17
|
-
RawToolCall,
|
|
18
|
-
RawSearchResult,
|
|
19
|
-
} from './types';
|
|
20
|
-
import { DebugLogger } from '../../utils';
|
|
21
|
-
import { friendlyHttpError } from './error-utils';
|
|
22
|
-
|
|
23
|
-
const logger = DebugLogger.module('GeminiProtocol');
|
|
24
|
-
|
|
25
|
-
const DEFAULT_GEMINI_URL = 'https://generativelanguage.googleapis.com/v1beta';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 历史中无 thoughtSignature 时使用的占位值(跳过 Gemini 校验)。
|
|
29
|
-
* 见 https://ai.google.dev/gemini-api/docs/thought-signatures
|
|
30
|
-
*/
|
|
31
|
-
const GEMINI_THOUGHT_SIGNATURE_DUMMY = 'skip_thought_signature_validator';
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Gemini Protocol 实现
|
|
35
|
-
*/
|
|
36
|
-
export class GeminiProtocol implements Protocol {
|
|
37
|
-
readonly name = 'gemini';
|
|
38
|
-
|
|
39
|
-
private apiKey: string;
|
|
40
|
-
private apiUrl: string;
|
|
41
|
-
|
|
42
|
-
constructor(config: ProtocolConfig) {
|
|
43
|
-
this.apiKey = config.apiKey;
|
|
44
|
-
this.apiUrl = config.apiUrl ?? DEFAULT_GEMINI_URL;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 发送请求并返回原始事件流
|
|
49
|
-
*/
|
|
50
|
-
async *stream(
|
|
51
|
-
messages: ProtocolMessage[],
|
|
52
|
-
tools: ProtocolToolDefinition[],
|
|
53
|
-
options: ProtocolRequestOptions
|
|
54
|
-
): AsyncGenerator<RawEvent> {
|
|
55
|
-
const requestBody = this.buildRequestBody(messages, tools, options);
|
|
56
|
-
const url = `${this.apiUrl}/models/${options.model}:streamGenerateContent?key=${this.apiKey}&alt=sse`;
|
|
57
|
-
|
|
58
|
-
logger.debug('发送 Gemini 请求', {
|
|
59
|
-
url: url.replace(this.apiKey, '***'),
|
|
60
|
-
model: options.model,
|
|
61
|
-
enableSearch: options.enableSearch,
|
|
62
|
-
enableThinking: options.enableThinking,
|
|
63
|
-
toolsCount: tools.length,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const response = await fetch(url, {
|
|
67
|
-
method: 'POST',
|
|
68
|
-
headers: { 'Content-Type': 'application/json' },
|
|
69
|
-
body: JSON.stringify(requestBody),
|
|
70
|
-
signal: options.signal,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
if (!response.ok) {
|
|
74
|
-
const errorText = await response.text();
|
|
75
|
-
logger.error('Gemini API 错误', { status: response.status, body: errorText.slice(0, 500) });
|
|
76
|
-
yield { type: 'error', error: friendlyHttpError(response.status, errorText, 'Gemini') };
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const reader = response.body?.getReader();
|
|
81
|
-
if (!reader) {
|
|
82
|
-
yield { type: 'error', error: '无法获取响应流' };
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
yield* this.parseSSE(reader);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 构建请求体
|
|
91
|
-
*/
|
|
92
|
-
private buildRequestBody(
|
|
93
|
-
messages: ProtocolMessage[],
|
|
94
|
-
tools: ProtocolToolDefinition[],
|
|
95
|
-
options: ProtocolRequestOptions
|
|
96
|
-
): Record<string, unknown> {
|
|
97
|
-
const { systemInstruction, contents } = this.convertMessages(messages);
|
|
98
|
-
|
|
99
|
-
const body: Record<string, unknown> = {
|
|
100
|
-
contents,
|
|
101
|
-
generationConfig: {
|
|
102
|
-
maxOutputTokens: options.familyConfig.defaultMaxTokens ?? 65536,
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
if (systemInstruction) {
|
|
107
|
-
body.systemInstruction = systemInstruction;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// 启用 thinking(需要 includeThoughts 才能输出思考内容)
|
|
111
|
-
if (options.enableThinking) {
|
|
112
|
-
(body.generationConfig as Record<string, unknown>).thinkingConfig = {
|
|
113
|
-
thinkingBudget: 24576,
|
|
114
|
-
includeThoughts: true,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// 构建工具
|
|
119
|
-
// 注意:Gemini 的 googleSearch 工具不能与其他函数调用工具同时使用
|
|
120
|
-
// 因此:如果有其他工具,则通过 web_search 工具(Tavily)进行搜索
|
|
121
|
-
// 如果没有其他工具,可以使用原生 googleSearch
|
|
122
|
-
const geminiTools: unknown[] = [];
|
|
123
|
-
|
|
124
|
-
// 添加函数调用工具:按名称去重(混合模式可能传入重复)
|
|
125
|
-
if (tools.length > 0) {
|
|
126
|
-
const decls = tools.map(t => ({
|
|
127
|
-
name: t.name,
|
|
128
|
-
description: t.description,
|
|
129
|
-
parameters: t.parameters,
|
|
130
|
-
}));
|
|
131
|
-
const byName = new Map<string, { name: string; description: string; parameters: object }>();
|
|
132
|
-
for (const d of decls) {
|
|
133
|
-
if (!byName.has(d.name)) byName.set(d.name, d);
|
|
134
|
-
}
|
|
135
|
-
geminiTools.push({
|
|
136
|
-
functionDeclarations: Array.from(byName.values()),
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
// 仅在没有其他工具时使用原生 Google Search
|
|
140
|
-
else if (options.enableSearch) {
|
|
141
|
-
geminiTools.push({ googleSearch: {} });
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (geminiTools.length > 0) {
|
|
145
|
-
body.tools = geminiTools;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return body;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 转换消息格式
|
|
153
|
-
*/
|
|
154
|
-
private convertMessages(messages: ProtocolMessage[]): {
|
|
155
|
-
systemInstruction?: { parts: { text: string }[] };
|
|
156
|
-
contents: unknown[];
|
|
157
|
-
} {
|
|
158
|
-
let systemInstruction: { parts: { text: string }[] } | undefined;
|
|
159
|
-
const contents: unknown[] = [];
|
|
160
|
-
|
|
161
|
-
for (const msg of messages) {
|
|
162
|
-
switch (msg.role) {
|
|
163
|
-
case 'system':
|
|
164
|
-
systemInstruction = { parts: [{ text: msg.content }] };
|
|
165
|
-
break;
|
|
166
|
-
|
|
167
|
-
case 'user': {
|
|
168
|
-
// 当只有图片没有文字时提供默认提示
|
|
169
|
-
const textContent = msg.content || (msg.images?.length ? '请分析这张图片' : '');
|
|
170
|
-
const parts: unknown[] = [{ text: textContent }];
|
|
171
|
-
if (msg.images?.length) {
|
|
172
|
-
for (const img of msg.images) {
|
|
173
|
-
if (img.startsWith('data:')) {
|
|
174
|
-
const match = img.match(/^data:([^;]+);base64,(.+)$/);
|
|
175
|
-
if (match) {
|
|
176
|
-
parts.push({
|
|
177
|
-
inlineData: { mimeType: match[1], data: match[2] },
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
} else {
|
|
181
|
-
parts.push({
|
|
182
|
-
inlineData: { mimeType: 'image/jpeg', data: img },
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
contents.push({ role: 'user', parts });
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
case 'assistant':
|
|
192
|
-
if (msg.toolCalls?.length) {
|
|
193
|
-
const parts: unknown[] = [];
|
|
194
|
-
for (const tc of msg.toolCalls) {
|
|
195
|
-
const funcPart: Record<string, unknown> = {
|
|
196
|
-
functionCall: {
|
|
197
|
-
name: tc.name,
|
|
198
|
-
args: JSON.parse(tc.arguments || '{}'),
|
|
199
|
-
},
|
|
200
|
-
};
|
|
201
|
-
// Gemini 3:每个 functionCall part 必须带 thoughtSignature,否则 400。
|
|
202
|
-
// 历史/跨模型回放可能没有,用官方 dummy 跳过校验。见 thought-signatures 文档。
|
|
203
|
-
funcPart.thoughtSignature = tc.thoughtSignature ?? GEMINI_THOUGHT_SIGNATURE_DUMMY;
|
|
204
|
-
parts.push(funcPart);
|
|
205
|
-
}
|
|
206
|
-
contents.push({ role: 'model', parts });
|
|
207
|
-
} else {
|
|
208
|
-
contents.push({ role: 'model', parts: [{ text: msg.content }] });
|
|
209
|
-
}
|
|
210
|
-
break;
|
|
211
|
-
|
|
212
|
-
case 'tool':
|
|
213
|
-
contents.push({
|
|
214
|
-
role: 'user',
|
|
215
|
-
parts: [{
|
|
216
|
-
functionResponse: {
|
|
217
|
-
name: msg.toolName || 'unknown',
|
|
218
|
-
response: { result: msg.content },
|
|
219
|
-
},
|
|
220
|
-
}],
|
|
221
|
-
});
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return { systemInstruction, contents };
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* 解析 SSE 流
|
|
231
|
-
*/
|
|
232
|
-
private async *parseSSE(reader: ReadableStreamDefaultReader<Uint8Array>): AsyncGenerator<RawEvent> {
|
|
233
|
-
const decoder = new TextDecoder();
|
|
234
|
-
let buffer = '';
|
|
235
|
-
const pendingToolCalls = new Map<string, RawToolCall>();
|
|
236
|
-
let textStarted = false;
|
|
237
|
-
let toolCallIndex = 0;
|
|
238
|
-
|
|
239
|
-
while (true) {
|
|
240
|
-
const { done, value } = await reader.read();
|
|
241
|
-
if (done) break;
|
|
242
|
-
|
|
243
|
-
buffer += decoder.decode(value, { stream: true });
|
|
244
|
-
const lines = buffer.split('\n');
|
|
245
|
-
buffer = lines.pop() || '';
|
|
246
|
-
|
|
247
|
-
for (const line of lines) {
|
|
248
|
-
if (!line.startsWith('data:')) continue;
|
|
249
|
-
|
|
250
|
-
const data = line.slice(5).trim();
|
|
251
|
-
if (!data) continue;
|
|
252
|
-
|
|
253
|
-
try {
|
|
254
|
-
const json = JSON.parse(data);
|
|
255
|
-
const candidate = json.candidates?.[0];
|
|
256
|
-
if (!candidate?.content?.parts) continue;
|
|
257
|
-
|
|
258
|
-
for (const part of candidate.content.parts) {
|
|
259
|
-
// 处理文本(根据 thought 标志区分思考和回复)
|
|
260
|
-
if (part.text) {
|
|
261
|
-
if (part.thought === true) {
|
|
262
|
-
// 思考内容
|
|
263
|
-
yield { type: 'thinking_delta', delta: part.text };
|
|
264
|
-
} else {
|
|
265
|
-
// 正常回复
|
|
266
|
-
if (!textStarted) {
|
|
267
|
-
textStarted = true;
|
|
268
|
-
yield { type: 'thinking_done' };
|
|
269
|
-
}
|
|
270
|
-
yield { type: 'text_delta', delta: part.text };
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// 处理函数调用
|
|
275
|
-
if (part.functionCall) {
|
|
276
|
-
const callId = `gemini-${toolCallIndex++}`;
|
|
277
|
-
const toolCall: RawToolCall = {
|
|
278
|
-
id: callId,
|
|
279
|
-
name: part.functionCall.name,
|
|
280
|
-
arguments: JSON.stringify(part.functionCall.args || {}),
|
|
281
|
-
};
|
|
282
|
-
// 捕获 thoughtSignature
|
|
283
|
-
if (part.thoughtSignature) {
|
|
284
|
-
toolCall.thoughtSignature = part.thoughtSignature;
|
|
285
|
-
}
|
|
286
|
-
pendingToolCalls.set(callId, toolCall);
|
|
287
|
-
yield { type: 'tool_call_start', toolCall: { id: callId, name: toolCall.name } };
|
|
288
|
-
yield { type: 'tool_call_done', toolCall };
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
// 处理 grounding metadata(搜索结果)
|
|
293
|
-
const groundingMeta = candidate.groundingMetadata;
|
|
294
|
-
if (groundingMeta?.groundingChunks?.length) {
|
|
295
|
-
const searchResults: RawSearchResult[] = [];
|
|
296
|
-
for (const chunk of groundingMeta.groundingChunks) {
|
|
297
|
-
if (chunk.web?.uri) {
|
|
298
|
-
searchResults.push({
|
|
299
|
-
title: chunk.web.title || '',
|
|
300
|
-
url: chunk.web.uri,
|
|
301
|
-
snippet: '',
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (searchResults.length > 0) {
|
|
306
|
-
yield { type: 'search_result', searchResults };
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// 检查是否结束
|
|
311
|
-
if (candidate.finishReason) {
|
|
312
|
-
// 提取 Token 使用统计(Gemini usageMetadata)
|
|
313
|
-
const meta = json.usageMetadata;
|
|
314
|
-
const usage = meta ? {
|
|
315
|
-
promptTokens: meta.promptTokenCount || 0,
|
|
316
|
-
completionTokens: meta.candidatesTokenCount || 0,
|
|
317
|
-
totalTokens: meta.totalTokenCount || 0,
|
|
318
|
-
reasoningTokens: meta.thoughtsTokenCount || 0,
|
|
319
|
-
cachedTokens: meta.cachedContentTokenCount || 0,
|
|
320
|
-
} : undefined;
|
|
321
|
-
|
|
322
|
-
if (pendingToolCalls.size > 0) {
|
|
323
|
-
yield { type: 'done', finishReason: 'tool_calls', usage };
|
|
324
|
-
} else {
|
|
325
|
-
yield { type: 'done', finishReason: 'stop', usage };
|
|
326
|
-
}
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
} catch {
|
|
330
|
-
// 忽略解析错误
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// 兜底
|
|
336
|
-
if (pendingToolCalls.size > 0) {
|
|
337
|
-
yield { type: 'done', finishReason: 'tool_calls' };
|
|
338
|
-
} else {
|
|
339
|
-
yield { type: 'done', finishReason: 'stop' };
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* 创建 Gemini Protocol
|
|
346
|
-
*/
|
|
347
|
-
export function createGeminiProtocol(config: ProtocolConfig): GeminiProtocol {
|
|
348
|
-
return new GeminiProtocol(config);
|
|
349
|
-
}
|
|
350
|
-
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Protocol Layer 导出
|
|
3
|
-
*
|
|
4
|
-
* Protocol 只负责 HTTP/SSE 通信,不处理业务逻辑
|
|
5
|
-
*
|
|
6
|
-
* 每个 Protocol 对应一种 API 协议:
|
|
7
|
-
* - ArkProtocol: 火山引擎 Responses API(豆包/DeepSeek)
|
|
8
|
-
* - QwenProtocol: 通义千问 DashScope API
|
|
9
|
-
* - GeminiProtocol: Google Gemini API
|
|
10
|
-
* - OpenAIProtocol: OpenAI API(通过 OpenRouter)
|
|
11
|
-
* - AnthropicProtocol: Anthropic API(通过 OpenRouter)
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
// 类型
|
|
15
|
-
export type {
|
|
16
|
-
RawEventType,
|
|
17
|
-
RawEvent,
|
|
18
|
-
RawToolCall,
|
|
19
|
-
RawSearchResult,
|
|
20
|
-
ProtocolToolCall,
|
|
21
|
-
ProtocolMessage,
|
|
22
|
-
ProtocolToolDefinition,
|
|
23
|
-
ProtocolRequestOptions,
|
|
24
|
-
Protocol,
|
|
25
|
-
ProtocolConfig,
|
|
26
|
-
ProtocolFactory,
|
|
27
|
-
} from './types';
|
|
28
|
-
|
|
29
|
-
// 各 Protocol 实现
|
|
30
|
-
export { ArkProtocol, createArkProtocol } from './ark';
|
|
31
|
-
export { DeepSeekProtocol, createDeepSeekProtocol } from './deepseek';
|
|
32
|
-
export { QwenProtocol, createQwenProtocol } from './qwen';
|
|
33
|
-
export { GeminiProtocol, createGeminiProtocol } from './gemini';
|
|
34
|
-
export { OpenAIProtocol, createOpenAIProtocol } from './openai';
|
|
35
|
-
export { AnthropicProtocol, createAnthropicProtocol } from './anthropic';
|
|
36
|
-
|