@omnicross/core 0.1.0 → 0.1.1
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/ApiConverter.cjs +799 -0
- package/dist/ApiConverter.d.cts +82 -0
- package/dist/ApiConverter.d.ts +82 -0
- package/dist/ApiConverter.js +763 -0
- package/dist/BuiltinToolExecutor-BluWyeob.d.ts +81 -0
- package/dist/BuiltinToolExecutor-CS2WpXhM.d.cts +81 -0
- package/dist/CompletionService-7fCmKAP3.d.ts +212 -0
- package/dist/CompletionService-DtOF_War.d.cts +212 -0
- package/dist/{ProviderProxy-f_8ziIhW.d.cts → ProviderProxy-C-xqrkKi.d.ts} +7 -2
- package/dist/{ProviderProxy-vjt8sQQk.d.ts → ProviderProxy-CnMQYN59.d.cts} +7 -2
- package/dist/completion/BuiltinToolExecutor.cjs +327 -0
- package/dist/completion/BuiltinToolExecutor.d.cts +4 -0
- package/dist/completion/BuiltinToolExecutor.d.ts +4 -0
- package/dist/completion/BuiltinToolExecutor.js +296 -0
- package/dist/completion/CompletionService.cjs +3487 -0
- package/dist/completion/CompletionService.d.cts +21 -0
- package/dist/completion/CompletionService.d.ts +21 -0
- package/dist/completion/CompletionService.js +3461 -0
- package/dist/completion/NativeSearchInjector.cjs +196 -0
- package/dist/completion/NativeSearchInjector.d.cts +42 -0
- package/dist/completion/NativeSearchInjector.d.ts +42 -0
- package/dist/completion/NativeSearchInjector.js +167 -0
- package/dist/completion/ProviderSearchInjector.cjs +87 -0
- package/dist/completion/ProviderSearchInjector.d.cts +47 -0
- package/dist/completion/ProviderSearchInjector.d.ts +47 -0
- package/dist/completion/ProviderSearchInjector.js +60 -0
- package/dist/completion/native-search-types.cjs +67 -0
- package/dist/completion/native-search-types.d.cts +3 -0
- package/dist/completion/native-search-types.d.ts +3 -0
- package/dist/completion/native-search-types.js +38 -0
- package/dist/completion/openrouter-headers.cjs +72 -0
- package/dist/completion/openrouter-headers.d.cts +44 -0
- package/dist/completion/openrouter-headers.d.ts +44 -0
- package/dist/completion/openrouter-headers.js +42 -0
- package/dist/completion/openrouter-models.cjs +86 -0
- package/dist/completion/openrouter-models.d.cts +27 -0
- package/dist/completion/openrouter-models.d.ts +27 -0
- package/dist/completion/openrouter-models.js +59 -0
- package/dist/completion/types.cjs +18 -0
- package/dist/completion/types.d.cts +3 -0
- package/dist/completion/types.d.ts +3 -0
- package/dist/completion/types.js +0 -0
- package/dist/completion/url-builder.cjs +138 -0
- package/dist/completion/url-builder.d.cts +87 -0
- package/dist/completion/url-builder.d.ts +87 -0
- package/dist/completion/url-builder.js +104 -0
- package/dist/completion.d.cts +148 -7
- package/dist/completion.d.ts +148 -7
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +27 -90
- package/dist/index.d.ts +27 -90
- package/dist/index.js +1 -0
- package/dist/outbound-api/routeResolver.cjs +221 -0
- package/dist/outbound-api/routeResolver.d.cts +18 -0
- package/dist/outbound-api/routeResolver.d.ts +18 -0
- package/dist/outbound-api/routeResolver.js +192 -0
- package/dist/outbound-api/subscriptionRegistryPort.d.cts +5 -2
- package/dist/outbound-api/subscriptionRegistryPort.d.ts +5 -2
- package/dist/outbound-api/types.cjs +18 -0
- package/dist/{types-CbCN2NQP.d.ts → outbound-api/types.d.cts} +17 -3
- package/dist/{types-CGGrKqC_.d.cts → outbound-api/types.d.ts} +17 -3
- package/dist/outbound-api/types.js +0 -0
- package/dist/outbound-api.cjs +1 -0
- package/dist/outbound-api.d.cts +14 -87
- package/dist/outbound-api.d.ts +14 -87
- package/dist/outbound-api.js +1 -0
- package/dist/pipeline/AuthSource.cjs +18 -0
- package/dist/pipeline/AuthSource.d.cts +101 -0
- package/dist/pipeline/AuthSource.d.ts +101 -0
- package/dist/pipeline/AuthSource.js +0 -0
- package/dist/pipeline/LlmConfigProviderAuth.cjs +169 -0
- package/dist/pipeline/LlmConfigProviderAuth.d.cts +86 -0
- package/dist/pipeline/LlmConfigProviderAuth.d.ts +86 -0
- package/dist/pipeline/LlmConfigProviderAuth.js +142 -0
- package/dist/pipeline/SubscriptionAuthSource.d.cts +165 -3
- package/dist/pipeline/SubscriptionAuthSource.d.ts +165 -3
- package/dist/pipeline/executeProviderCall.cjs +70 -0
- package/dist/pipeline/executeProviderCall.d.cts +149 -0
- package/dist/pipeline/executeProviderCall.d.ts +149 -0
- package/dist/pipeline/executeProviderCall.js +45 -0
- package/dist/pipeline/resolveProviderChain.cjs +47 -0
- package/dist/pipeline/resolveProviderChain.d.cts +58 -0
- package/dist/pipeline/resolveProviderChain.d.ts +58 -0
- package/dist/pipeline/resolveProviderChain.js +22 -0
- package/dist/pipeline/resolveSubscriptionChain.cjs +68 -0
- package/dist/pipeline/resolveSubscriptionChain.d.cts +68 -0
- package/dist/pipeline/resolveSubscriptionChain.d.ts +68 -0
- package/dist/pipeline/resolveSubscriptionChain.js +43 -0
- package/dist/ports/provider-config-source.cjs +18 -0
- package/dist/ports/provider-config-source.d.cts +51 -0
- package/dist/ports/provider-config-source.d.ts +51 -0
- package/dist/ports/provider-config-source.js +0 -0
- package/dist/ports/web-search-backend.cjs +18 -0
- package/dist/ports/web-search-backend.d.cts +29 -0
- package/dist/ports/web-search-backend.d.ts +29 -0
- package/dist/ports/web-search-backend.js +0 -0
- package/dist/ports.d.cts +10 -7
- package/dist/ports.d.ts +10 -7
- package/dist/provider-proxy/ProviderProxy.cjs +4643 -0
- package/dist/provider-proxy/ProviderProxy.d.cts +16 -0
- package/dist/provider-proxy/ProviderProxy.d.ts +16 -0
- package/dist/provider-proxy/ProviderProxy.js +4618 -0
- package/dist/provider-proxy/ingress/providerProxyShared.d.cts +5 -2
- package/dist/provider-proxy/ingress/providerProxyShared.d.ts +5 -2
- package/dist/provider-proxy/types.d.cts +406 -8
- package/dist/provider-proxy/types.d.ts +406 -8
- package/dist/provider-proxy.cjs +1 -0
- package/dist/provider-proxy.d.cts +8 -5
- package/dist/provider-proxy.d.ts +8 -5
- package/dist/provider-proxy.js +1 -0
- package/dist/routeResolver-BrbK6ja9.d.cts +88 -0
- package/dist/routeResolver-HE-ZO0fO.d.ts +88 -0
- package/dist/transformer/anthropicBetaInject.cjs +51 -0
- package/dist/transformer/anthropicBetaInject.d.cts +20 -0
- package/dist/transformer/anthropicBetaInject.d.ts +20 -0
- package/dist/transformer/anthropicBetaInject.js +25 -0
- package/dist/transformer/transformers/AnthropicTransformer.cjs +1017 -0
- package/dist/transformer/transformers/AnthropicTransformer.d.cts +148 -0
- package/dist/transformer/transformers/AnthropicTransformer.d.ts +148 -0
- package/dist/transformer/transformers/AnthropicTransformer.js +990 -0
- package/dist/transformer/transformers/ReasoningTransformer.cjs +273 -0
- package/dist/transformer/transformers/ReasoningTransformer.d.cts +47 -0
- package/dist/transformer/transformers/ReasoningTransformer.d.ts +47 -0
- package/dist/transformer/transformers/ReasoningTransformer.js +253 -0
- package/dist/transformer/transformers.cjs +3206 -0
- package/dist/transformer/transformers.d.cts +100 -0
- package/dist/transformer/transformers.d.ts +100 -0
- package/dist/transformer/transformers.js +3174 -0
- package/dist/transformer.d.cts +8 -31
- package/dist/transformer.d.ts +8 -31
- package/dist/types-BScIHmPr.d.cts +153 -0
- package/dist/types-BScIHmPr.d.ts +153 -0
- package/package.json +3 -3
- package/dist/SubscriptionAuthSource-Cr4fVEYY.d.cts +0 -264
- package/dist/SubscriptionAuthSource-D89zmiSS.d.ts +0 -264
- package/dist/index-BTSmc9Sm.d.ts +0 -645
- package/dist/index-DXazdTzZ.d.cts +0 -645
- package/dist/types-DCzHkhJt.d.ts +0 -467
- package/dist/types-DZIQbgp0.d.cts +0 -467
|
@@ -0,0 +1,3174 @@
|
|
|
1
|
+
// src/transformer/transformers/AnthropicToolHandling.ts
|
|
2
|
+
function isServerSideTool(tool) {
|
|
3
|
+
const type = String(tool.type || "");
|
|
4
|
+
return type.startsWith("web_search_") || type.startsWith("code_execution_") || type.startsWith("text_editor_") || type.startsWith("memory_") || type.startsWith("web_fetch_") || type.startsWith("search_tool_");
|
|
5
|
+
}
|
|
6
|
+
function convertAnthropicToolsToOpenAI(tools) {
|
|
7
|
+
return tools.filter((tool) => !isServerSideTool(tool)).map((tool) => ({
|
|
8
|
+
type: "function",
|
|
9
|
+
function: {
|
|
10
|
+
name: String(tool.name),
|
|
11
|
+
description: String(tool.description || ""),
|
|
12
|
+
parameters: tool.input_schema
|
|
13
|
+
}
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/transformer/transformers/AnthropicTypes.ts
|
|
18
|
+
function getThinkLevel(budgetTokens) {
|
|
19
|
+
if (!budgetTokens || budgetTokens <= 0) return "none";
|
|
20
|
+
if (budgetTokens < 4096) return "low";
|
|
21
|
+
if (budgetTokens < 16384) return "medium";
|
|
22
|
+
return "high";
|
|
23
|
+
}
|
|
24
|
+
function formatBase64(data, mediaType) {
|
|
25
|
+
if (data.startsWith("data:")) return data;
|
|
26
|
+
return `data:${mediaType || "image/png"};base64,${data}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/transformer/transformers/AnthropicRequestBuilder.ts
|
|
30
|
+
function buildAnthropicRequestBody(request) {
|
|
31
|
+
let systemContent;
|
|
32
|
+
const anthropicMessages = [];
|
|
33
|
+
for (let i = 0; i < request.messages.length; i++) {
|
|
34
|
+
const msg = request.messages[i];
|
|
35
|
+
if (msg.role === "system") {
|
|
36
|
+
if (typeof msg.content === "string") {
|
|
37
|
+
systemContent = msg.content;
|
|
38
|
+
} else if (Array.isArray(msg.content)) {
|
|
39
|
+
systemContent = msg.content.filter((c) => c.type === "text").map((c) => ({
|
|
40
|
+
type: "text",
|
|
41
|
+
text: c.text,
|
|
42
|
+
...c.cache_control ? { cache_control: c.cache_control } : {}
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (msg.role === "assistant") {
|
|
48
|
+
const content = [];
|
|
49
|
+
if (msg.thinking?.content) {
|
|
50
|
+
const block = {
|
|
51
|
+
type: "thinking",
|
|
52
|
+
thinking: msg.thinking.content
|
|
53
|
+
};
|
|
54
|
+
if (msg.thinking.signature) {
|
|
55
|
+
block.signature = msg.thinking.signature;
|
|
56
|
+
}
|
|
57
|
+
content.push(block);
|
|
58
|
+
}
|
|
59
|
+
if (msg.content) {
|
|
60
|
+
const text = typeof msg.content === "string" ? msg.content : msg.content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
61
|
+
if (text) {
|
|
62
|
+
content.push({ type: "text", text });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (msg.tool_calls?.length) {
|
|
66
|
+
for (const tc of msg.tool_calls) {
|
|
67
|
+
let input;
|
|
68
|
+
try {
|
|
69
|
+
input = typeof tc.function.arguments === "string" ? JSON.parse(tc.function.arguments) : tc.function.arguments;
|
|
70
|
+
} catch {
|
|
71
|
+
input = { text: tc.function.arguments || "" };
|
|
72
|
+
}
|
|
73
|
+
content.push({
|
|
74
|
+
type: "tool_use",
|
|
75
|
+
id: tc.id,
|
|
76
|
+
name: tc.function.name,
|
|
77
|
+
input
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
anthropicMessages.push({
|
|
82
|
+
role: "assistant",
|
|
83
|
+
content: content.length > 0 ? content : ""
|
|
84
|
+
});
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (msg.role === "tool") {
|
|
88
|
+
const toolResults = [];
|
|
89
|
+
let j = i;
|
|
90
|
+
while (j < request.messages.length && request.messages[j].role === "tool") {
|
|
91
|
+
const t = request.messages[j];
|
|
92
|
+
toolResults.push({
|
|
93
|
+
type: "tool_result",
|
|
94
|
+
tool_use_id: t.tool_call_id || "",
|
|
95
|
+
content: typeof t.content === "string" ? t.content : JSON.stringify(t.content),
|
|
96
|
+
...t.cache_control ? { cache_control: t.cache_control } : {}
|
|
97
|
+
});
|
|
98
|
+
j++;
|
|
99
|
+
}
|
|
100
|
+
anthropicMessages.push({ role: "user", content: toolResults });
|
|
101
|
+
i = j - 1;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (typeof msg.content === "string") {
|
|
105
|
+
anthropicMessages.push({ role: "user", content: msg.content });
|
|
106
|
+
} else if (Array.isArray(msg.content)) {
|
|
107
|
+
const content = msg.content.map((part) => {
|
|
108
|
+
if (part.type === "image_url") {
|
|
109
|
+
const url = part.image_url.url;
|
|
110
|
+
if (url.startsWith("data:")) {
|
|
111
|
+
const match = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
112
|
+
if (match) {
|
|
113
|
+
return {
|
|
114
|
+
type: "image",
|
|
115
|
+
source: { type: "base64", media_type: match[1], data: match[2] }
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
type: "image",
|
|
121
|
+
source: { type: "url", url }
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return { type: "text", text: part.text };
|
|
125
|
+
});
|
|
126
|
+
anthropicMessages.push({ role: "user", content });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const body = {
|
|
130
|
+
model: request.model,
|
|
131
|
+
messages: anthropicMessages,
|
|
132
|
+
max_tokens: request.max_tokens || 4096,
|
|
133
|
+
stream: request.stream ?? false
|
|
134
|
+
};
|
|
135
|
+
if (request.temperature !== void 0) {
|
|
136
|
+
body.temperature = request.temperature;
|
|
137
|
+
}
|
|
138
|
+
if (systemContent !== void 0) {
|
|
139
|
+
body.system = systemContent;
|
|
140
|
+
}
|
|
141
|
+
if (request.tools?.length) {
|
|
142
|
+
body.tools = request.tools.map((tool) => ({
|
|
143
|
+
name: tool.function.name,
|
|
144
|
+
description: tool.function.description || "",
|
|
145
|
+
input_schema: tool.function.parameters
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
const serverSideTools = request._serverSideTools;
|
|
149
|
+
if (serverSideTools?.length) {
|
|
150
|
+
body.tools = [...body.tools || [], ...serverSideTools];
|
|
151
|
+
}
|
|
152
|
+
if (request.tool_choice) {
|
|
153
|
+
if (typeof request.tool_choice === "string") {
|
|
154
|
+
if (request.tool_choice === "required") {
|
|
155
|
+
body.tool_choice = { type: "any" };
|
|
156
|
+
} else if (request.tool_choice !== "none") {
|
|
157
|
+
body.tool_choice = { type: request.tool_choice };
|
|
158
|
+
}
|
|
159
|
+
} else if (typeof request.tool_choice === "object" && "function" in request.tool_choice) {
|
|
160
|
+
body.tool_choice = { type: "tool", name: request.tool_choice.function.name };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (request.reasoning?.enabled) {
|
|
164
|
+
const budgetMap = { low: 2048, medium: 8192, high: 32768 };
|
|
165
|
+
const budget = request.reasoning.max_tokens || budgetMap[request.reasoning.effort || "medium"] || 8192;
|
|
166
|
+
body.thinking = { type: "enabled", budget_tokens: budget };
|
|
167
|
+
body.temperature = 1;
|
|
168
|
+
}
|
|
169
|
+
return body;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/transformer/transformers/AnthropicResponseConversion.ts
|
|
173
|
+
function convertAnthropicResponseToOpenAI(anthropicResponse) {
|
|
174
|
+
const content = anthropicResponse.content || [];
|
|
175
|
+
const textParts = content.filter((c) => c.type === "text").map((c) => c.text);
|
|
176
|
+
const toolUses = content.filter((c) => c.type === "tool_use" || c.type === "server_tool_use");
|
|
177
|
+
const thinkingBlock = content.find((c) => c.type === "thinking");
|
|
178
|
+
const searchResults = content.filter((c) => c.type === "web_search_tool_result");
|
|
179
|
+
for (const sr of searchResults) {
|
|
180
|
+
const searches = sr.content;
|
|
181
|
+
if (searches?.length) {
|
|
182
|
+
const formatted = searches.map((s) => `[${s.title}](${s.url}): ${s.page_content || s.snippet || ""}`).join("\n");
|
|
183
|
+
textParts.push(`
|
|
184
|
+
|
|
185
|
+
**Search Results:**
|
|
186
|
+
${formatted}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const message = {
|
|
190
|
+
role: "assistant",
|
|
191
|
+
content: textParts.join("") || null
|
|
192
|
+
};
|
|
193
|
+
if (toolUses.length > 0) {
|
|
194
|
+
message.tool_calls = toolUses.map((tc) => ({
|
|
195
|
+
id: tc.id,
|
|
196
|
+
type: "function",
|
|
197
|
+
function: {
|
|
198
|
+
name: tc.name,
|
|
199
|
+
arguments: JSON.stringify(tc.input || {})
|
|
200
|
+
}
|
|
201
|
+
}));
|
|
202
|
+
}
|
|
203
|
+
if (thinkingBlock) {
|
|
204
|
+
message.thinking = {
|
|
205
|
+
content: thinkingBlock.thinking,
|
|
206
|
+
signature: thinkingBlock.signature
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
const stopReasonMapping = {
|
|
210
|
+
end_turn: "stop",
|
|
211
|
+
max_tokens: "length",
|
|
212
|
+
tool_use: "tool_calls",
|
|
213
|
+
stop_sequence: "stop"
|
|
214
|
+
};
|
|
215
|
+
const usage = anthropicResponse.usage;
|
|
216
|
+
return {
|
|
217
|
+
id: anthropicResponse.id || `chatcmpl-${Date.now()}`,
|
|
218
|
+
object: "chat.completion",
|
|
219
|
+
created: Math.floor(Date.now() / 1e3),
|
|
220
|
+
model: anthropicResponse.model || "unknown",
|
|
221
|
+
choices: [{
|
|
222
|
+
index: 0,
|
|
223
|
+
message,
|
|
224
|
+
finish_reason: stopReasonMapping[anthropicResponse.stop_reason] || "stop"
|
|
225
|
+
}],
|
|
226
|
+
usage: usage ? {
|
|
227
|
+
prompt_tokens: usage.input_tokens || 0,
|
|
228
|
+
completion_tokens: usage.output_tokens || 0,
|
|
229
|
+
total_tokens: (usage.input_tokens || 0) + (usage.output_tokens || 0)
|
|
230
|
+
} : void 0
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function convertOpenAIResponseToAnthropic(openaiResponse) {
|
|
234
|
+
const choice = openaiResponse.choices?.[0];
|
|
235
|
+
if (!choice) {
|
|
236
|
+
throw new Error("No choices found in OpenAI response");
|
|
237
|
+
}
|
|
238
|
+
const message = choice.message;
|
|
239
|
+
const content = [];
|
|
240
|
+
if (message.content) {
|
|
241
|
+
content.push({
|
|
242
|
+
type: "text",
|
|
243
|
+
text: message.content
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
const toolCalls = message.tool_calls;
|
|
247
|
+
if (toolCalls?.length) {
|
|
248
|
+
for (const toolCall of toolCalls) {
|
|
249
|
+
const func = toolCall.function;
|
|
250
|
+
let parsedInput = {};
|
|
251
|
+
try {
|
|
252
|
+
const args = func.arguments;
|
|
253
|
+
parsedInput = typeof args === "string" ? JSON.parse(args) : args;
|
|
254
|
+
} catch {
|
|
255
|
+
parsedInput = { text: func.arguments || "" };
|
|
256
|
+
}
|
|
257
|
+
content.push({
|
|
258
|
+
type: "tool_use",
|
|
259
|
+
id: toolCall.id,
|
|
260
|
+
name: func.name,
|
|
261
|
+
input: parsedInput
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const thinking = message.thinking;
|
|
266
|
+
if (thinking?.content) {
|
|
267
|
+
content.push({
|
|
268
|
+
type: "thinking",
|
|
269
|
+
thinking: thinking.content,
|
|
270
|
+
signature: thinking.signature
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
const finishReason = choice.finish_reason;
|
|
274
|
+
const stopReasonMapping = {
|
|
275
|
+
stop: "end_turn",
|
|
276
|
+
length: "max_tokens",
|
|
277
|
+
tool_calls: "tool_use",
|
|
278
|
+
content_filter: "stop_sequence"
|
|
279
|
+
};
|
|
280
|
+
const usage = openaiResponse.usage;
|
|
281
|
+
const usageDetails = usage?.prompt_tokens_details;
|
|
282
|
+
return {
|
|
283
|
+
id: openaiResponse.id,
|
|
284
|
+
type: "message",
|
|
285
|
+
role: "assistant",
|
|
286
|
+
model: openaiResponse.model,
|
|
287
|
+
content,
|
|
288
|
+
stop_reason: stopReasonMapping[finishReason] || "end_turn",
|
|
289
|
+
stop_sequence: null,
|
|
290
|
+
usage: {
|
|
291
|
+
input_tokens: (usage?.prompt_tokens || 0) - (usageDetails?.cached_tokens || 0),
|
|
292
|
+
output_tokens: usage?.completion_tokens || 0,
|
|
293
|
+
cache_read_input_tokens: usageDetails?.cached_tokens || 0
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// src/transformer/transformers/AnthropicConversion.ts
|
|
299
|
+
function transformAnthropicRequestToUnified(request) {
|
|
300
|
+
const anthropicRequest = request;
|
|
301
|
+
const messages = [];
|
|
302
|
+
if (anthropicRequest.system) {
|
|
303
|
+
if (typeof anthropicRequest.system === "string") {
|
|
304
|
+
messages.push({
|
|
305
|
+
role: "system",
|
|
306
|
+
content: anthropicRequest.system
|
|
307
|
+
});
|
|
308
|
+
} else if (Array.isArray(anthropicRequest.system)) {
|
|
309
|
+
const textParts = anthropicRequest.system.filter((item) => item.type === "text" && item.text).map((item) => ({
|
|
310
|
+
type: "text",
|
|
311
|
+
text: item.text,
|
|
312
|
+
cache_control: item.cache_control
|
|
313
|
+
}));
|
|
314
|
+
if (textParts.length > 0) {
|
|
315
|
+
messages.push({
|
|
316
|
+
role: "system",
|
|
317
|
+
content: textParts
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const requestMessages = JSON.parse(JSON.stringify(anthropicRequest.messages || []));
|
|
323
|
+
for (const msg of requestMessages) {
|
|
324
|
+
if (msg.role !== "user" && msg.role !== "assistant") continue;
|
|
325
|
+
if (typeof msg.content === "string") {
|
|
326
|
+
messages.push({
|
|
327
|
+
role: msg.role,
|
|
328
|
+
content: msg.content
|
|
329
|
+
});
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (Array.isArray(msg.content)) {
|
|
333
|
+
if (msg.role === "user") {
|
|
334
|
+
const toolParts = msg.content.filter(
|
|
335
|
+
(c) => c.type === "tool_result" && c.tool_use_id
|
|
336
|
+
);
|
|
337
|
+
for (const tool of toolParts) {
|
|
338
|
+
messages.push({
|
|
339
|
+
role: "tool",
|
|
340
|
+
content: typeof tool.content === "string" ? tool.content : JSON.stringify(tool.content),
|
|
341
|
+
tool_call_id: tool.tool_use_id,
|
|
342
|
+
cache_control: tool.cache_control
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
const textAndMediaParts = msg.content.filter(
|
|
346
|
+
(c) => c.type === "text" && c.text || c.type === "image" && c.source
|
|
347
|
+
);
|
|
348
|
+
if (textAndMediaParts.length > 0) {
|
|
349
|
+
messages.push({
|
|
350
|
+
role: "user",
|
|
351
|
+
content: textAndMediaParts.map((part) => {
|
|
352
|
+
if (part.type === "image") {
|
|
353
|
+
const imagePart = part;
|
|
354
|
+
return {
|
|
355
|
+
type: "image_url",
|
|
356
|
+
image_url: {
|
|
357
|
+
url: imagePart.source.type === "base64" ? formatBase64(imagePart.source.data || "", imagePart.source.media_type) : imagePart.source.url || ""
|
|
358
|
+
},
|
|
359
|
+
media_type: imagePart.source.media_type
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
return {
|
|
363
|
+
type: "text",
|
|
364
|
+
text: part.text
|
|
365
|
+
};
|
|
366
|
+
})
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
} else if (msg.role === "assistant") {
|
|
370
|
+
const assistantMessage = {
|
|
371
|
+
role: "assistant",
|
|
372
|
+
content: ""
|
|
373
|
+
};
|
|
374
|
+
const textParts = msg.content.filter(
|
|
375
|
+
(c) => c.type === "text" && c.text
|
|
376
|
+
);
|
|
377
|
+
if (textParts.length > 0) {
|
|
378
|
+
assistantMessage.content = textParts.map((t) => t.text).join("\n");
|
|
379
|
+
}
|
|
380
|
+
const toolCallParts = msg.content.filter(
|
|
381
|
+
(c) => c.type === "tool_use" && c.id
|
|
382
|
+
);
|
|
383
|
+
if (toolCallParts.length > 0) {
|
|
384
|
+
assistantMessage.tool_calls = toolCallParts.map((tool) => ({
|
|
385
|
+
id: tool.id,
|
|
386
|
+
type: "function",
|
|
387
|
+
function: {
|
|
388
|
+
name: tool.name,
|
|
389
|
+
arguments: JSON.stringify(tool.input || {})
|
|
390
|
+
}
|
|
391
|
+
}));
|
|
392
|
+
}
|
|
393
|
+
const thinkingPart = msg.content.find(
|
|
394
|
+
(c) => c.type === "thinking"
|
|
395
|
+
);
|
|
396
|
+
if (thinkingPart?.thinking) {
|
|
397
|
+
assistantMessage.thinking = {
|
|
398
|
+
content: thinkingPart.thinking,
|
|
399
|
+
signature: thinkingPart.signature
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
messages.push(assistantMessage);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const rawTools = anthropicRequest.tools || [];
|
|
407
|
+
const serverSideTools = rawTools.filter((t) => isServerSideTool(t));
|
|
408
|
+
const functionTools = rawTools.length > 0 ? convertAnthropicToolsToOpenAI(rawTools) : void 0;
|
|
409
|
+
const result = {
|
|
410
|
+
messages,
|
|
411
|
+
model: anthropicRequest.model,
|
|
412
|
+
max_tokens: anthropicRequest.max_tokens,
|
|
413
|
+
temperature: anthropicRequest.temperature,
|
|
414
|
+
stream: anthropicRequest.stream,
|
|
415
|
+
tools: functionTools?.length ? functionTools : void 0
|
|
416
|
+
};
|
|
417
|
+
if (serverSideTools.length > 0) {
|
|
418
|
+
result._serverSideTools = serverSideTools;
|
|
419
|
+
}
|
|
420
|
+
if (anthropicRequest.thinking) {
|
|
421
|
+
result.reasoning = {
|
|
422
|
+
effort: getThinkLevel(anthropicRequest.thinking.budget_tokens),
|
|
423
|
+
enabled: anthropicRequest.thinking.type === "enabled"
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
if (anthropicRequest.tool_choice) {
|
|
427
|
+
if (anthropicRequest.tool_choice.type === "tool" && anthropicRequest.tool_choice.name) {
|
|
428
|
+
result.tool_choice = {
|
|
429
|
+
type: "function",
|
|
430
|
+
function: { name: anthropicRequest.tool_choice.name }
|
|
431
|
+
};
|
|
432
|
+
} else {
|
|
433
|
+
result.tool_choice = anthropicRequest.tool_choice.type;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return result;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// src/transformer/transformers/AnthropicAnthropicToOpenAIStream.ts
|
|
440
|
+
function convertAnthropicStreamToOpenAI(anthropicStream, logger) {
|
|
441
|
+
const decoder = new TextDecoder();
|
|
442
|
+
const encoder = new TextEncoder();
|
|
443
|
+
const activeToolCalls = /* @__PURE__ */ new Map();
|
|
444
|
+
let toolCallCounter = 0;
|
|
445
|
+
let model = "unknown";
|
|
446
|
+
let messageId = `chatcmpl-${Date.now()}`;
|
|
447
|
+
return new ReadableStream({
|
|
448
|
+
start: async (controller) => {
|
|
449
|
+
const reader = anthropicStream.getReader();
|
|
450
|
+
let buffer = "";
|
|
451
|
+
let isClosed = false;
|
|
452
|
+
const safeEnqueue = (data) => {
|
|
453
|
+
if (!isClosed) {
|
|
454
|
+
try {
|
|
455
|
+
controller.enqueue(encoder.encode(data));
|
|
456
|
+
} catch {
|
|
457
|
+
isClosed = true;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
try {
|
|
462
|
+
while (true) {
|
|
463
|
+
const { done, value } = await reader.read();
|
|
464
|
+
if (done) break;
|
|
465
|
+
buffer += decoder.decode(value, { stream: true });
|
|
466
|
+
const lines = buffer.split("\n");
|
|
467
|
+
buffer = lines.pop() || "";
|
|
468
|
+
for (const line of lines) {
|
|
469
|
+
if (isClosed) break;
|
|
470
|
+
if (!line.startsWith("data:")) continue;
|
|
471
|
+
const data = line.slice(5).trim();
|
|
472
|
+
if (!data || data === "[DONE]") continue;
|
|
473
|
+
try {
|
|
474
|
+
const event = JSON.parse(data);
|
|
475
|
+
if (event.type === "message_start" && event.message) {
|
|
476
|
+
model = event.message.model || model;
|
|
477
|
+
messageId = event.message.id || messageId;
|
|
478
|
+
}
|
|
479
|
+
if (event.type === "content_block_delta" && event.delta) {
|
|
480
|
+
const chunk = {
|
|
481
|
+
id: messageId,
|
|
482
|
+
object: "chat.completion.chunk",
|
|
483
|
+
created: Math.floor(Date.now() / 1e3),
|
|
484
|
+
model,
|
|
485
|
+
choices: [{ index: 0, delta: {}, finish_reason: null }]
|
|
486
|
+
};
|
|
487
|
+
const delta = chunk.choices[0].delta;
|
|
488
|
+
if (event.delta.type === "text_delta") {
|
|
489
|
+
delta.content = event.delta.text;
|
|
490
|
+
} else if (event.delta.type === "input_json_delta") {
|
|
491
|
+
const toolInfo = activeToolCalls.get(event.index);
|
|
492
|
+
if (toolInfo) {
|
|
493
|
+
delta.tool_calls = [{
|
|
494
|
+
index: toolInfo.index,
|
|
495
|
+
function: { arguments: event.delta.partial_json }
|
|
496
|
+
}];
|
|
497
|
+
}
|
|
498
|
+
} else if (event.delta.type === "thinking_delta") {
|
|
499
|
+
delta.thinking = { content: event.delta.thinking };
|
|
500
|
+
} else if (event.delta.type === "signature_delta") {
|
|
501
|
+
delta.thinking = { signature: event.delta.signature };
|
|
502
|
+
} else {
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
safeEnqueue(`data: ${JSON.stringify(chunk)}
|
|
506
|
+
|
|
507
|
+
`);
|
|
508
|
+
}
|
|
509
|
+
if (event.type === "content_block_start" && event.content_block) {
|
|
510
|
+
if (event.content_block.type === "tool_use" || event.content_block.type === "server_tool_use") {
|
|
511
|
+
const tcIndex = toolCallCounter++;
|
|
512
|
+
activeToolCalls.set(event.index, {
|
|
513
|
+
id: event.content_block.id,
|
|
514
|
+
name: event.content_block.name,
|
|
515
|
+
index: tcIndex
|
|
516
|
+
});
|
|
517
|
+
const chunk = {
|
|
518
|
+
id: messageId,
|
|
519
|
+
object: "chat.completion.chunk",
|
|
520
|
+
created: Math.floor(Date.now() / 1e3),
|
|
521
|
+
model,
|
|
522
|
+
choices: [{
|
|
523
|
+
index: 0,
|
|
524
|
+
delta: {
|
|
525
|
+
tool_calls: [{
|
|
526
|
+
index: tcIndex,
|
|
527
|
+
id: event.content_block.id,
|
|
528
|
+
type: "function",
|
|
529
|
+
function: { name: event.content_block.name, arguments: "" }
|
|
530
|
+
}]
|
|
531
|
+
},
|
|
532
|
+
finish_reason: null
|
|
533
|
+
}]
|
|
534
|
+
};
|
|
535
|
+
safeEnqueue(`data: ${JSON.stringify(chunk)}
|
|
536
|
+
|
|
537
|
+
`);
|
|
538
|
+
} else if (event.content_block.type === "web_search_tool_result") {
|
|
539
|
+
const searches = event.content_block.content;
|
|
540
|
+
if (searches?.length) {
|
|
541
|
+
const formatted = searches.map((s) => `[${s.title}](${s.url}): ${s.page_content || s.snippet || ""}`).join("\n");
|
|
542
|
+
const chunk = {
|
|
543
|
+
id: messageId,
|
|
544
|
+
object: "chat.completion.chunk",
|
|
545
|
+
created: Math.floor(Date.now() / 1e3),
|
|
546
|
+
model,
|
|
547
|
+
choices: [{
|
|
548
|
+
index: 0,
|
|
549
|
+
delta: { content: `
|
|
550
|
+
|
|
551
|
+
**Search Results:**
|
|
552
|
+
${formatted}` },
|
|
553
|
+
finish_reason: null
|
|
554
|
+
}]
|
|
555
|
+
};
|
|
556
|
+
safeEnqueue(`data: ${JSON.stringify(chunk)}
|
|
557
|
+
|
|
558
|
+
`);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
if (event.type === "message_delta" && event.delta) {
|
|
563
|
+
const stopReasonMapping = {
|
|
564
|
+
end_turn: "stop",
|
|
565
|
+
max_tokens: "length",
|
|
566
|
+
tool_use: "tool_calls",
|
|
567
|
+
stop_sequence: "stop"
|
|
568
|
+
};
|
|
569
|
+
const chunk = {
|
|
570
|
+
id: messageId,
|
|
571
|
+
object: "chat.completion.chunk",
|
|
572
|
+
created: Math.floor(Date.now() / 1e3),
|
|
573
|
+
model,
|
|
574
|
+
choices: [{
|
|
575
|
+
index: 0,
|
|
576
|
+
delta: {},
|
|
577
|
+
finish_reason: stopReasonMapping[event.delta.stop_reason] || "stop"
|
|
578
|
+
}]
|
|
579
|
+
};
|
|
580
|
+
if (event.usage) {
|
|
581
|
+
chunk.usage = {
|
|
582
|
+
prompt_tokens: event.usage.input_tokens || 0,
|
|
583
|
+
completion_tokens: event.usage.output_tokens || 0,
|
|
584
|
+
total_tokens: (event.usage.input_tokens || 0) + (event.usage.output_tokens || 0)
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
safeEnqueue(`data: ${JSON.stringify(chunk)}
|
|
588
|
+
|
|
589
|
+
`);
|
|
590
|
+
}
|
|
591
|
+
} catch (e) {
|
|
592
|
+
logger?.error("Error parsing Anthropic stream event:", e);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
} catch (e) {
|
|
597
|
+
if (!isClosed) {
|
|
598
|
+
controller.error(e);
|
|
599
|
+
}
|
|
600
|
+
} finally {
|
|
601
|
+
safeEnqueue("data: [DONE]\n\n");
|
|
602
|
+
if (!isClosed) {
|
|
603
|
+
try {
|
|
604
|
+
controller.close();
|
|
605
|
+
} catch {
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
reader.releaseLock();
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// src/transformer/transformers/AnthropicOpenAIToAnthropicStream.ts
|
|
615
|
+
function convertOpenAIStreamToAnthropic(openaiStream, _context, logger) {
|
|
616
|
+
const decoder = new TextDecoder();
|
|
617
|
+
const encoder = new TextEncoder();
|
|
618
|
+
let hasStarted = false;
|
|
619
|
+
let hasTextContentStarted = false;
|
|
620
|
+
let isThinkingStarted = false;
|
|
621
|
+
let contentIndex = 0;
|
|
622
|
+
let currentContentBlockIndex = -1;
|
|
623
|
+
const toolCallIndexToContentBlockIndex = /* @__PURE__ */ new Map();
|
|
624
|
+
return new ReadableStream({
|
|
625
|
+
start: async (controller) => {
|
|
626
|
+
const reader = openaiStream.getReader();
|
|
627
|
+
let buffer = "";
|
|
628
|
+
const messageId = `msg_${Date.now()}`;
|
|
629
|
+
let model = "unknown";
|
|
630
|
+
let isClosed = false;
|
|
631
|
+
let stopReasonDelta = null;
|
|
632
|
+
const safeEnqueue = (data) => {
|
|
633
|
+
if (!isClosed) {
|
|
634
|
+
try {
|
|
635
|
+
controller.enqueue(encoder.encode(data));
|
|
636
|
+
} catch (_e) {
|
|
637
|
+
isClosed = true;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
const assignContentBlockIndex = () => {
|
|
642
|
+
return contentIndex++;
|
|
643
|
+
};
|
|
644
|
+
const safeClose = () => {
|
|
645
|
+
if (isClosed) return;
|
|
646
|
+
if (currentContentBlockIndex >= 0) {
|
|
647
|
+
safeEnqueue(`event: content_block_stop
|
|
648
|
+
data: ${JSON.stringify({
|
|
649
|
+
type: "content_block_stop",
|
|
650
|
+
index: currentContentBlockIndex
|
|
651
|
+
})}
|
|
652
|
+
|
|
653
|
+
`);
|
|
654
|
+
}
|
|
655
|
+
if (stopReasonDelta) {
|
|
656
|
+
safeEnqueue(`event: message_delta
|
|
657
|
+
data: ${JSON.stringify(stopReasonDelta)}
|
|
658
|
+
|
|
659
|
+
`);
|
|
660
|
+
} else {
|
|
661
|
+
safeEnqueue(`event: message_delta
|
|
662
|
+
data: ${JSON.stringify({
|
|
663
|
+
type: "message_delta",
|
|
664
|
+
delta: { stop_reason: "end_turn", stop_sequence: null },
|
|
665
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
666
|
+
})}
|
|
667
|
+
|
|
668
|
+
`);
|
|
669
|
+
}
|
|
670
|
+
safeEnqueue(`event: message_stop
|
|
671
|
+
data: ${JSON.stringify({ type: "message_stop" })}
|
|
672
|
+
|
|
673
|
+
`);
|
|
674
|
+
try {
|
|
675
|
+
controller.close();
|
|
676
|
+
} catch (_e) {
|
|
677
|
+
}
|
|
678
|
+
isClosed = true;
|
|
679
|
+
};
|
|
680
|
+
try {
|
|
681
|
+
while (true) {
|
|
682
|
+
const { done, value } = await reader.read();
|
|
683
|
+
if (done) break;
|
|
684
|
+
buffer += decoder.decode(value, { stream: true });
|
|
685
|
+
const lines = buffer.split("\n");
|
|
686
|
+
buffer = lines.pop() || "";
|
|
687
|
+
for (const line of lines) {
|
|
688
|
+
if (isClosed) break;
|
|
689
|
+
if (!line.startsWith("data:")) continue;
|
|
690
|
+
const data = line.slice(5).trim();
|
|
691
|
+
if (data === "[DONE]") continue;
|
|
692
|
+
try {
|
|
693
|
+
const chunk = JSON.parse(data);
|
|
694
|
+
if (chunk.error) {
|
|
695
|
+
safeEnqueue(`event: error
|
|
696
|
+
data: ${JSON.stringify({
|
|
697
|
+
type: "error",
|
|
698
|
+
message: { type: "api_error", message: JSON.stringify(chunk.error) }
|
|
699
|
+
})}
|
|
700
|
+
|
|
701
|
+
`);
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
model = chunk.model || model;
|
|
705
|
+
if (!hasStarted) {
|
|
706
|
+
hasStarted = true;
|
|
707
|
+
safeEnqueue(`event: message_start
|
|
708
|
+
data: ${JSON.stringify({
|
|
709
|
+
type: "message_start",
|
|
710
|
+
message: {
|
|
711
|
+
id: messageId,
|
|
712
|
+
type: "message",
|
|
713
|
+
role: "assistant",
|
|
714
|
+
content: [],
|
|
715
|
+
model,
|
|
716
|
+
stop_reason: null,
|
|
717
|
+
stop_sequence: null,
|
|
718
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
719
|
+
}
|
|
720
|
+
})}
|
|
721
|
+
|
|
722
|
+
`);
|
|
723
|
+
}
|
|
724
|
+
const choice = chunk.choices?.[0];
|
|
725
|
+
if (!choice) continue;
|
|
726
|
+
if (chunk.usage) {
|
|
727
|
+
stopReasonDelta = {
|
|
728
|
+
type: "message_delta",
|
|
729
|
+
delta: { stop_reason: "end_turn", stop_sequence: null },
|
|
730
|
+
usage: {
|
|
731
|
+
input_tokens: (chunk.usage.prompt_tokens || 0) - (chunk.usage.prompt_tokens_details?.cached_tokens || 0),
|
|
732
|
+
output_tokens: chunk.usage.completion_tokens || 0,
|
|
733
|
+
cache_read_input_tokens: chunk.usage.prompt_tokens_details?.cached_tokens || 0
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
if (choice.delta?.thinking) {
|
|
738
|
+
if (!isThinkingStarted) {
|
|
739
|
+
const thinkingBlockIndex = assignContentBlockIndex();
|
|
740
|
+
safeEnqueue(`event: content_block_start
|
|
741
|
+
data: ${JSON.stringify({
|
|
742
|
+
type: "content_block_start",
|
|
743
|
+
index: thinkingBlockIndex,
|
|
744
|
+
content_block: { type: "thinking", thinking: "" }
|
|
745
|
+
})}
|
|
746
|
+
|
|
747
|
+
`);
|
|
748
|
+
currentContentBlockIndex = thinkingBlockIndex;
|
|
749
|
+
isThinkingStarted = true;
|
|
750
|
+
}
|
|
751
|
+
if (choice.delta.thinking.signature) {
|
|
752
|
+
safeEnqueue(`event: content_block_delta
|
|
753
|
+
data: ${JSON.stringify({
|
|
754
|
+
type: "content_block_delta",
|
|
755
|
+
index: currentContentBlockIndex,
|
|
756
|
+
delta: { type: "signature_delta", signature: choice.delta.thinking.signature }
|
|
757
|
+
})}
|
|
758
|
+
|
|
759
|
+
`);
|
|
760
|
+
safeEnqueue(`event: content_block_stop
|
|
761
|
+
data: ${JSON.stringify({
|
|
762
|
+
type: "content_block_stop",
|
|
763
|
+
index: currentContentBlockIndex
|
|
764
|
+
})}
|
|
765
|
+
|
|
766
|
+
`);
|
|
767
|
+
currentContentBlockIndex = -1;
|
|
768
|
+
} else if (choice.delta.thinking.content) {
|
|
769
|
+
safeEnqueue(`event: content_block_delta
|
|
770
|
+
data: ${JSON.stringify({
|
|
771
|
+
type: "content_block_delta",
|
|
772
|
+
index: currentContentBlockIndex,
|
|
773
|
+
delta: { type: "thinking_delta", thinking: choice.delta.thinking.content }
|
|
774
|
+
})}
|
|
775
|
+
|
|
776
|
+
`);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
if (choice.delta?.content) {
|
|
780
|
+
if (!hasTextContentStarted) {
|
|
781
|
+
if (currentContentBlockIndex >= 0 && isThinkingStarted) {
|
|
782
|
+
safeEnqueue(`event: content_block_stop
|
|
783
|
+
data: ${JSON.stringify({
|
|
784
|
+
type: "content_block_stop",
|
|
785
|
+
index: currentContentBlockIndex
|
|
786
|
+
})}
|
|
787
|
+
|
|
788
|
+
`);
|
|
789
|
+
}
|
|
790
|
+
hasTextContentStarted = true;
|
|
791
|
+
const textBlockIndex = assignContentBlockIndex();
|
|
792
|
+
safeEnqueue(`event: content_block_start
|
|
793
|
+
data: ${JSON.stringify({
|
|
794
|
+
type: "content_block_start",
|
|
795
|
+
index: textBlockIndex,
|
|
796
|
+
content_block: { type: "text", text: "" }
|
|
797
|
+
})}
|
|
798
|
+
|
|
799
|
+
`);
|
|
800
|
+
currentContentBlockIndex = textBlockIndex;
|
|
801
|
+
}
|
|
802
|
+
safeEnqueue(`event: content_block_delta
|
|
803
|
+
data: ${JSON.stringify({
|
|
804
|
+
type: "content_block_delta",
|
|
805
|
+
index: currentContentBlockIndex,
|
|
806
|
+
delta: { type: "text_delta", text: choice.delta.content }
|
|
807
|
+
})}
|
|
808
|
+
|
|
809
|
+
`);
|
|
810
|
+
}
|
|
811
|
+
if (choice.delta?.tool_calls) {
|
|
812
|
+
for (const toolCall of choice.delta.tool_calls) {
|
|
813
|
+
const toolCallIndex = toolCall.index ?? 0;
|
|
814
|
+
if (!toolCallIndexToContentBlockIndex.has(toolCallIndex)) {
|
|
815
|
+
if (currentContentBlockIndex >= 0) {
|
|
816
|
+
safeEnqueue(`event: content_block_stop
|
|
817
|
+
data: ${JSON.stringify({
|
|
818
|
+
type: "content_block_stop",
|
|
819
|
+
index: currentContentBlockIndex
|
|
820
|
+
})}
|
|
821
|
+
|
|
822
|
+
`);
|
|
823
|
+
hasTextContentStarted = false;
|
|
824
|
+
}
|
|
825
|
+
const newBlockIndex = assignContentBlockIndex();
|
|
826
|
+
toolCallIndexToContentBlockIndex.set(toolCallIndex, newBlockIndex);
|
|
827
|
+
safeEnqueue(`event: content_block_start
|
|
828
|
+
data: ${JSON.stringify({
|
|
829
|
+
type: "content_block_start",
|
|
830
|
+
index: newBlockIndex,
|
|
831
|
+
content_block: {
|
|
832
|
+
type: "tool_use",
|
|
833
|
+
id: toolCall.id || `call_${Date.now()}_${toolCallIndex}`,
|
|
834
|
+
name: toolCall.function?.name || `tool_${toolCallIndex}`,
|
|
835
|
+
input: {}
|
|
836
|
+
}
|
|
837
|
+
})}
|
|
838
|
+
|
|
839
|
+
`);
|
|
840
|
+
currentContentBlockIndex = newBlockIndex;
|
|
841
|
+
}
|
|
842
|
+
if (toolCall.function?.arguments) {
|
|
843
|
+
const blockIndex = toolCallIndexToContentBlockIndex.get(toolCallIndex);
|
|
844
|
+
if (blockIndex !== void 0) {
|
|
845
|
+
safeEnqueue(`event: content_block_delta
|
|
846
|
+
data: ${JSON.stringify({
|
|
847
|
+
type: "content_block_delta",
|
|
848
|
+
index: blockIndex,
|
|
849
|
+
delta: { type: "input_json_delta", partial_json: toolCall.function.arguments }
|
|
850
|
+
})}
|
|
851
|
+
|
|
852
|
+
`);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if (choice.finish_reason) {
|
|
858
|
+
const stopReasonMapping = {
|
|
859
|
+
stop: "end_turn",
|
|
860
|
+
length: "max_tokens",
|
|
861
|
+
tool_calls: "tool_use",
|
|
862
|
+
content_filter: "stop_sequence"
|
|
863
|
+
};
|
|
864
|
+
stopReasonDelta = {
|
|
865
|
+
type: "message_delta",
|
|
866
|
+
delta: {
|
|
867
|
+
stop_reason: stopReasonMapping[choice.finish_reason] || "end_turn",
|
|
868
|
+
stop_sequence: null
|
|
869
|
+
},
|
|
870
|
+
usage: {
|
|
871
|
+
input_tokens: (chunk.usage?.prompt_tokens || 0) - (chunk.usage?.prompt_tokens_details?.cached_tokens || 0),
|
|
872
|
+
output_tokens: chunk.usage?.completion_tokens || 0,
|
|
873
|
+
cache_read_input_tokens: chunk.usage?.prompt_tokens_details?.cached_tokens || 0
|
|
874
|
+
}
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
} catch (e) {
|
|
878
|
+
logger?.error("Error parsing stream chunk:", e);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
} catch (e) {
|
|
883
|
+
if (!isClosed) {
|
|
884
|
+
controller.error(e);
|
|
885
|
+
}
|
|
886
|
+
} finally {
|
|
887
|
+
safeClose();
|
|
888
|
+
reader.releaseLock();
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// src/transformer/transformers/AnthropicTransformer.ts
|
|
895
|
+
var AnthropicTransformer = class {
|
|
896
|
+
static TransformerName = "anthropic";
|
|
897
|
+
name = "anthropic";
|
|
898
|
+
logger;
|
|
899
|
+
endPoint = "/v1/messages";
|
|
900
|
+
useBearer;
|
|
901
|
+
constructor(options) {
|
|
902
|
+
this.useBearer = options?.UseBearer ?? false;
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Handle authentication - Anthropic uses x-api-key header
|
|
906
|
+
*/
|
|
907
|
+
async auth(request, provider, _context) {
|
|
908
|
+
const headers = {};
|
|
909
|
+
if (this.useBearer) {
|
|
910
|
+
headers["authorization"] = `Bearer ${provider.apiKey}`;
|
|
911
|
+
headers["x-api-key"] = void 0;
|
|
912
|
+
} else {
|
|
913
|
+
headers["x-api-key"] = provider.apiKey;
|
|
914
|
+
headers["authorization"] = void 0;
|
|
915
|
+
}
|
|
916
|
+
return {
|
|
917
|
+
body: request,
|
|
918
|
+
config: { headers }
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* Transform Anthropic request to unified format.
|
|
923
|
+
*/
|
|
924
|
+
async transformRequestOut(request, _context) {
|
|
925
|
+
return transformAnthropicRequestToUnified(request);
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Transform OpenAI/unified response back to Anthropic format
|
|
929
|
+
* (auto-detects stream vs JSON via Content-Type).
|
|
930
|
+
*/
|
|
931
|
+
async transformResponseIn(response, context) {
|
|
932
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
933
|
+
if (contentType.includes("text/event-stream")) {
|
|
934
|
+
if (!response.body) {
|
|
935
|
+
throw new Error("Stream response body is null");
|
|
936
|
+
}
|
|
937
|
+
const convertedStream = convertOpenAIStreamToAnthropic(response.body, context, this.logger);
|
|
938
|
+
return new Response(convertedStream, {
|
|
939
|
+
headers: {
|
|
940
|
+
"Content-Type": "text/event-stream",
|
|
941
|
+
"Cache-Control": "no-cache",
|
|
942
|
+
Connection: "keep-alive"
|
|
943
|
+
}
|
|
944
|
+
});
|
|
945
|
+
} else {
|
|
946
|
+
const data = await response.json();
|
|
947
|
+
const anthropicResponse = convertOpenAIResponseToAnthropic(data);
|
|
948
|
+
return new Response(JSON.stringify(anthropicResponse), {
|
|
949
|
+
headers: { "Content-Type": "application/json" }
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Transform unified request to Anthropic Messages API format.
|
|
955
|
+
* This is the reverse of transformRequestOut — converts OpenAI/unified format
|
|
956
|
+
* to Anthropic's expected request body structure.
|
|
957
|
+
*/
|
|
958
|
+
async transformRequestIn(request, _provider, _context) {
|
|
959
|
+
return buildAnthropicRequestBody(request);
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* Transform Anthropic response to OpenAI/unified format
|
|
963
|
+
* (auto-detects stream vs JSON via Content-Type).
|
|
964
|
+
*/
|
|
965
|
+
async transformResponseOut(response, _context) {
|
|
966
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
967
|
+
if (contentType.includes("text/event-stream")) {
|
|
968
|
+
if (!response.body) {
|
|
969
|
+
throw new Error("Stream response body is null");
|
|
970
|
+
}
|
|
971
|
+
const convertedStream = convertAnthropicStreamToOpenAI(response.body, this.logger);
|
|
972
|
+
return new Response(convertedStream, {
|
|
973
|
+
headers: {
|
|
974
|
+
"Content-Type": "text/event-stream",
|
|
975
|
+
"Cache-Control": "no-cache",
|
|
976
|
+
Connection: "keep-alive"
|
|
977
|
+
}
|
|
978
|
+
});
|
|
979
|
+
} else {
|
|
980
|
+
const data = await response.json();
|
|
981
|
+
const openaiResponse = convertAnthropicResponseToOpenAI(data);
|
|
982
|
+
return new Response(JSON.stringify(openaiResponse), {
|
|
983
|
+
headers: { "Content-Type": "application/json" }
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
};
|
|
988
|
+
|
|
989
|
+
// src/transformer/transformers/DeepseekTransformer.ts
|
|
990
|
+
var DeepseekTransformer = class {
|
|
991
|
+
static TransformerName = "deepseek";
|
|
992
|
+
name = "deepseek";
|
|
993
|
+
logger;
|
|
994
|
+
/**
|
|
995
|
+
* Transform request: limit max_tokens to 8192 for legacy models only
|
|
996
|
+
* V4 models support up to 384K output tokens
|
|
997
|
+
*/
|
|
998
|
+
async transformRequestIn(request, _provider, _context) {
|
|
999
|
+
const isV4 = request.model?.startsWith("deepseek-v4");
|
|
1000
|
+
if (!isV4 && request.max_tokens && request.max_tokens > 8192) {
|
|
1001
|
+
this.logger?.debug("DeepSeek: Limiting max_tokens from %d to 8192", request.max_tokens);
|
|
1002
|
+
request.max_tokens = 8192;
|
|
1003
|
+
}
|
|
1004
|
+
return request;
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Transform response: convert reasoning_content to thinking blocks
|
|
1008
|
+
*/
|
|
1009
|
+
async transformResponseOut(response, _context) {
|
|
1010
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
1011
|
+
if (contentType.includes("application/json")) {
|
|
1012
|
+
return this.handleJsonResponse(response);
|
|
1013
|
+
} else if (contentType.includes("stream") || contentType.includes("text/event-stream")) {
|
|
1014
|
+
return this.handleStreamResponse(response);
|
|
1015
|
+
}
|
|
1016
|
+
return response;
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* Handle JSON (non-streaming) response
|
|
1020
|
+
*/
|
|
1021
|
+
async handleJsonResponse(response) {
|
|
1022
|
+
const jsonResponse = await response.json();
|
|
1023
|
+
if (jsonResponse.choices?.[0]?.message?.reasoning_content) {
|
|
1024
|
+
jsonResponse.choices[0].message.thinking = {
|
|
1025
|
+
content: jsonResponse.choices[0].message.reasoning_content
|
|
1026
|
+
};
|
|
1027
|
+
delete jsonResponse.choices[0].message.reasoning_content;
|
|
1028
|
+
}
|
|
1029
|
+
return new Response(JSON.stringify(jsonResponse), {
|
|
1030
|
+
status: response.status,
|
|
1031
|
+
statusText: response.statusText,
|
|
1032
|
+
headers: response.headers
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Handle streaming response - convert reasoning_content to thinking blocks
|
|
1037
|
+
*/
|
|
1038
|
+
async handleStreamResponse(response) {
|
|
1039
|
+
if (!response.body) {
|
|
1040
|
+
return response;
|
|
1041
|
+
}
|
|
1042
|
+
const decoder = new TextDecoder();
|
|
1043
|
+
const encoder = new TextEncoder();
|
|
1044
|
+
let reasoningContent = "";
|
|
1045
|
+
let isReasoningComplete = false;
|
|
1046
|
+
let buffer = "";
|
|
1047
|
+
const stream = new ReadableStream({
|
|
1048
|
+
start: async (controller) => {
|
|
1049
|
+
const reader = response.body.getReader();
|
|
1050
|
+
const processLine = (line) => {
|
|
1051
|
+
if (line.startsWith("data: ") && line.trim() !== "data: [DONE]") {
|
|
1052
|
+
try {
|
|
1053
|
+
const data = JSON.parse(line.slice(6));
|
|
1054
|
+
if (data.choices?.[0]?.delta?.reasoning_content) {
|
|
1055
|
+
reasoningContent += data.choices[0].delta.reasoning_content;
|
|
1056
|
+
const thinkingChunk = {
|
|
1057
|
+
...data,
|
|
1058
|
+
choices: [
|
|
1059
|
+
{
|
|
1060
|
+
...data.choices[0],
|
|
1061
|
+
delta: {
|
|
1062
|
+
...data.choices[0].delta,
|
|
1063
|
+
thinking: {
|
|
1064
|
+
content: data.choices[0].delta.reasoning_content
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
]
|
|
1069
|
+
};
|
|
1070
|
+
delete thinkingChunk.choices[0].delta.reasoning_content;
|
|
1071
|
+
const thinkingLine = `data: ${JSON.stringify(thinkingChunk)}
|
|
1072
|
+
|
|
1073
|
+
`;
|
|
1074
|
+
controller.enqueue(encoder.encode(thinkingLine));
|
|
1075
|
+
return;
|
|
1076
|
+
}
|
|
1077
|
+
if (data.choices?.[0]?.delta?.content && reasoningContent && !isReasoningComplete) {
|
|
1078
|
+
isReasoningComplete = true;
|
|
1079
|
+
const signature = Date.now().toString();
|
|
1080
|
+
const thinkingChunk = {
|
|
1081
|
+
...data,
|
|
1082
|
+
choices: [
|
|
1083
|
+
{
|
|
1084
|
+
...data.choices[0],
|
|
1085
|
+
delta: {
|
|
1086
|
+
...data.choices[0].delta,
|
|
1087
|
+
content: null,
|
|
1088
|
+
thinking: {
|
|
1089
|
+
signature
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
]
|
|
1094
|
+
};
|
|
1095
|
+
const thinkingLine = `data: ${JSON.stringify(thinkingChunk)}
|
|
1096
|
+
|
|
1097
|
+
`;
|
|
1098
|
+
controller.enqueue(encoder.encode(thinkingLine));
|
|
1099
|
+
}
|
|
1100
|
+
if (data.choices?.[0]?.delta?.reasoning_content) {
|
|
1101
|
+
delete data.choices[0].delta.reasoning_content;
|
|
1102
|
+
}
|
|
1103
|
+
if (data.choices?.[0]?.delta && Object.keys(data.choices[0].delta).length > 0) {
|
|
1104
|
+
if (isReasoningComplete) {
|
|
1105
|
+
data.choices[0].index++;
|
|
1106
|
+
}
|
|
1107
|
+
const modifiedLine = `data: ${JSON.stringify(data)}
|
|
1108
|
+
|
|
1109
|
+
`;
|
|
1110
|
+
controller.enqueue(encoder.encode(modifiedLine));
|
|
1111
|
+
}
|
|
1112
|
+
} catch (_e) {
|
|
1113
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
1114
|
+
}
|
|
1115
|
+
} else {
|
|
1116
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
try {
|
|
1120
|
+
while (true) {
|
|
1121
|
+
const { done, value } = await reader.read();
|
|
1122
|
+
if (done) {
|
|
1123
|
+
if (buffer.trim()) {
|
|
1124
|
+
const lines2 = buffer.split("\n");
|
|
1125
|
+
for (const line of lines2) {
|
|
1126
|
+
if (line.trim()) {
|
|
1127
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
break;
|
|
1132
|
+
}
|
|
1133
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
1134
|
+
buffer += chunk;
|
|
1135
|
+
const lines = buffer.split("\n");
|
|
1136
|
+
buffer = lines.pop() || "";
|
|
1137
|
+
for (const line of lines) {
|
|
1138
|
+
if (!line.trim()) continue;
|
|
1139
|
+
try {
|
|
1140
|
+
processLine(line);
|
|
1141
|
+
} catch (error) {
|
|
1142
|
+
this.logger?.error("Error processing DeepSeek stream line:", error);
|
|
1143
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
} catch (error) {
|
|
1148
|
+
this.logger?.error("DeepSeek stream error:", error);
|
|
1149
|
+
controller.error(error);
|
|
1150
|
+
} finally {
|
|
1151
|
+
try {
|
|
1152
|
+
reader.releaseLock();
|
|
1153
|
+
} catch (e) {
|
|
1154
|
+
this.logger?.error("Error releasing reader lock:", e);
|
|
1155
|
+
}
|
|
1156
|
+
controller.close();
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
});
|
|
1160
|
+
return new Response(stream, {
|
|
1161
|
+
status: response.status,
|
|
1162
|
+
statusText: response.statusText,
|
|
1163
|
+
headers: new Headers({
|
|
1164
|
+
"Content-Type": "text/event-stream",
|
|
1165
|
+
"Cache-Control": "no-cache",
|
|
1166
|
+
Connection: "keep-alive"
|
|
1167
|
+
})
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
|
|
1172
|
+
// src/transformer/transformers/utils/gemini.response-in.ts
|
|
1173
|
+
var FINISH_REASON_TO_GEMINI = {
|
|
1174
|
+
stop: "STOP",
|
|
1175
|
+
length: "MAX_TOKENS",
|
|
1176
|
+
tool_calls: "STOP",
|
|
1177
|
+
content_filter: "SAFETY",
|
|
1178
|
+
// Already-lowercased Gemini reasons (pass-through from another transformer)
|
|
1179
|
+
max_tokens: "MAX_TOKENS",
|
|
1180
|
+
safety: "SAFETY"
|
|
1181
|
+
};
|
|
1182
|
+
function toGeminiFinishReason(openaiReason) {
|
|
1183
|
+
if (!openaiReason) return null;
|
|
1184
|
+
return FINISH_REASON_TO_GEMINI[openaiReason] || openaiReason.toUpperCase();
|
|
1185
|
+
}
|
|
1186
|
+
function convertOpenAIResponseToGemini(openaiData) {
|
|
1187
|
+
const choice = openaiData.choices?.[0];
|
|
1188
|
+
const message = choice?.message ?? {};
|
|
1189
|
+
const usage = openaiData.usage;
|
|
1190
|
+
const usagePromptDetails = usage?.prompt_tokens_details;
|
|
1191
|
+
const usageOutputDetails = usage?.output_tokens_details;
|
|
1192
|
+
const parts = [];
|
|
1193
|
+
const thinking = message.thinking;
|
|
1194
|
+
if (thinking?.content) {
|
|
1195
|
+
parts.push({ text: thinking.content, thought: true });
|
|
1196
|
+
}
|
|
1197
|
+
if (thinking?.signature) {
|
|
1198
|
+
parts.push({ thoughtSignature: thinking.signature });
|
|
1199
|
+
}
|
|
1200
|
+
if (message.content) {
|
|
1201
|
+
parts.push({ text: message.content });
|
|
1202
|
+
}
|
|
1203
|
+
const toolCalls = message.tool_calls;
|
|
1204
|
+
if (toolCalls?.length) {
|
|
1205
|
+
for (const tc of toolCalls) {
|
|
1206
|
+
const func = tc.function;
|
|
1207
|
+
let args = {};
|
|
1208
|
+
try {
|
|
1209
|
+
const raw = func.arguments;
|
|
1210
|
+
args = typeof raw === "string" ? JSON.parse(raw) : raw;
|
|
1211
|
+
} catch {
|
|
1212
|
+
args = {};
|
|
1213
|
+
}
|
|
1214
|
+
parts.push({
|
|
1215
|
+
functionCall: {
|
|
1216
|
+
id: tc.id,
|
|
1217
|
+
name: func.name,
|
|
1218
|
+
args
|
|
1219
|
+
}
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
if (parts.length === 0) {
|
|
1224
|
+
parts.push({ text: "" });
|
|
1225
|
+
}
|
|
1226
|
+
return {
|
|
1227
|
+
responseId: openaiData.id || "",
|
|
1228
|
+
modelVersion: openaiData.model || "",
|
|
1229
|
+
candidates: [{
|
|
1230
|
+
content: { parts },
|
|
1231
|
+
finishReason: toGeminiFinishReason(choice?.finish_reason)
|
|
1232
|
+
}],
|
|
1233
|
+
usageMetadata: usage ? {
|
|
1234
|
+
promptTokenCount: usage.prompt_tokens || 0,
|
|
1235
|
+
candidatesTokenCount: usage.completion_tokens || 0,
|
|
1236
|
+
totalTokenCount: usage.total_tokens || 0,
|
|
1237
|
+
cachedContentTokenCount: usagePromptDetails?.cached_tokens || 0,
|
|
1238
|
+
thoughtsTokenCount: usageOutputDetails?.reasoning_tokens || 0
|
|
1239
|
+
} : void 0
|
|
1240
|
+
};
|
|
1241
|
+
}
|
|
1242
|
+
function convertOpenAIStreamToGemini(openaiStream, logger) {
|
|
1243
|
+
const decoder = new TextDecoder();
|
|
1244
|
+
const encoder = new TextEncoder();
|
|
1245
|
+
const pendingToolCalls = /* @__PURE__ */ new Map();
|
|
1246
|
+
let model = "";
|
|
1247
|
+
let responseId = "";
|
|
1248
|
+
return new ReadableStream({
|
|
1249
|
+
start: async (controller) => {
|
|
1250
|
+
const reader = openaiStream.getReader();
|
|
1251
|
+
let buffer = "";
|
|
1252
|
+
let isClosed = false;
|
|
1253
|
+
const emit = (data) => {
|
|
1254
|
+
if (isClosed) return;
|
|
1255
|
+
try {
|
|
1256
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}
|
|
1257
|
+
|
|
1258
|
+
`));
|
|
1259
|
+
} catch {
|
|
1260
|
+
isClosed = true;
|
|
1261
|
+
}
|
|
1262
|
+
};
|
|
1263
|
+
const flushToolCalls = () => {
|
|
1264
|
+
if (pendingToolCalls.size === 0) return;
|
|
1265
|
+
const parts = [];
|
|
1266
|
+
for (const tc of pendingToolCalls.values()) {
|
|
1267
|
+
let args = {};
|
|
1268
|
+
try {
|
|
1269
|
+
args = JSON.parse(tc.args || "{}");
|
|
1270
|
+
} catch {
|
|
1271
|
+
}
|
|
1272
|
+
parts.push({
|
|
1273
|
+
functionCall: { id: tc.id, name: tc.name, args }
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
pendingToolCalls.clear();
|
|
1277
|
+
emit({
|
|
1278
|
+
responseId,
|
|
1279
|
+
modelVersion: model,
|
|
1280
|
+
candidates: [{ content: { parts }, finishReason: null }]
|
|
1281
|
+
});
|
|
1282
|
+
};
|
|
1283
|
+
try {
|
|
1284
|
+
while (true) {
|
|
1285
|
+
const { done, value } = await reader.read();
|
|
1286
|
+
if (done) break;
|
|
1287
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1288
|
+
const lines = buffer.split("\n");
|
|
1289
|
+
buffer = lines.pop() || "";
|
|
1290
|
+
for (const line of lines) {
|
|
1291
|
+
if (isClosed) break;
|
|
1292
|
+
if (!line.startsWith("data:")) continue;
|
|
1293
|
+
const data = line.slice(5).trim();
|
|
1294
|
+
if (!data || data === "[DONE]") continue;
|
|
1295
|
+
try {
|
|
1296
|
+
const chunk = JSON.parse(data);
|
|
1297
|
+
if (!responseId && chunk.id) responseId = chunk.id;
|
|
1298
|
+
if (!model && chunk.model) model = chunk.model;
|
|
1299
|
+
const choice = chunk.choices?.[0];
|
|
1300
|
+
if (!choice) continue;
|
|
1301
|
+
const delta = choice.delta ?? {};
|
|
1302
|
+
if (delta.thinking) {
|
|
1303
|
+
const parts = [];
|
|
1304
|
+
if (delta.thinking.content) {
|
|
1305
|
+
parts.push({ text: delta.thinking.content, thought: true });
|
|
1306
|
+
}
|
|
1307
|
+
if (delta.thinking.signature) {
|
|
1308
|
+
parts.push({ thoughtSignature: delta.thinking.signature });
|
|
1309
|
+
}
|
|
1310
|
+
if (parts.length > 0) {
|
|
1311
|
+
emit({
|
|
1312
|
+
responseId,
|
|
1313
|
+
modelVersion: model,
|
|
1314
|
+
candidates: [{ content: { parts }, finishReason: null }]
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
if (delta.content) {
|
|
1319
|
+
emit({
|
|
1320
|
+
responseId,
|
|
1321
|
+
modelVersion: model,
|
|
1322
|
+
candidates: [{
|
|
1323
|
+
content: { parts: [{ text: delta.content }] },
|
|
1324
|
+
finishReason: null
|
|
1325
|
+
}]
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
if (delta.tool_calls) {
|
|
1329
|
+
for (const tc of delta.tool_calls) {
|
|
1330
|
+
const idx = tc.index ?? 0;
|
|
1331
|
+
const existing = pendingToolCalls.get(idx);
|
|
1332
|
+
const func = tc.function;
|
|
1333
|
+
if (existing) {
|
|
1334
|
+
if (func?.arguments) {
|
|
1335
|
+
existing.args += func.arguments;
|
|
1336
|
+
}
|
|
1337
|
+
} else {
|
|
1338
|
+
pendingToolCalls.set(idx, {
|
|
1339
|
+
id: tc.id || `tool_${Date.now()}_${idx}`,
|
|
1340
|
+
name: func?.name || "",
|
|
1341
|
+
args: func?.arguments || ""
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
if (choice.finish_reason) {
|
|
1347
|
+
flushToolCalls();
|
|
1348
|
+
const geminiUsage = chunk.usage ? {
|
|
1349
|
+
promptTokenCount: chunk.usage.prompt_tokens || 0,
|
|
1350
|
+
candidatesTokenCount: chunk.usage.completion_tokens || 0,
|
|
1351
|
+
totalTokenCount: chunk.usage.total_tokens || 0,
|
|
1352
|
+
cachedContentTokenCount: chunk.usage.prompt_tokens_details?.cached_tokens || 0,
|
|
1353
|
+
thoughtsTokenCount: chunk.usage.output_tokens_details?.reasoning_tokens || 0
|
|
1354
|
+
} : void 0;
|
|
1355
|
+
emit({
|
|
1356
|
+
responseId,
|
|
1357
|
+
modelVersion: model,
|
|
1358
|
+
candidates: [{
|
|
1359
|
+
content: { parts: [{ text: "" }] },
|
|
1360
|
+
finishReason: toGeminiFinishReason(choice.finish_reason) || "STOP"
|
|
1361
|
+
}],
|
|
1362
|
+
usageMetadata: geminiUsage
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
} catch (e) {
|
|
1366
|
+
logger?.error(`Error parsing OpenAI stream chunk for Gemini conversion: ${e}`);
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
flushToolCalls();
|
|
1371
|
+
} catch (e) {
|
|
1372
|
+
if (!isClosed) controller.error(e);
|
|
1373
|
+
} finally {
|
|
1374
|
+
if (!isClosed) {
|
|
1375
|
+
try {
|
|
1376
|
+
controller.close();
|
|
1377
|
+
} catch {
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
reader.releaseLock();
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
async function transformResponseIn(response, logger) {
|
|
1386
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
1387
|
+
if (contentType.includes("text/event-stream")) {
|
|
1388
|
+
if (!response.body) {
|
|
1389
|
+
throw new Error("Stream response body is null");
|
|
1390
|
+
}
|
|
1391
|
+
const geminiStream = convertOpenAIStreamToGemini(response.body, logger);
|
|
1392
|
+
return new Response(geminiStream, {
|
|
1393
|
+
headers: {
|
|
1394
|
+
"Content-Type": "text/event-stream",
|
|
1395
|
+
"Cache-Control": "no-cache",
|
|
1396
|
+
Connection: "keep-alive"
|
|
1397
|
+
}
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
const data = await response.json();
|
|
1401
|
+
const geminiResponse = convertOpenAIResponseToGemini(data);
|
|
1402
|
+
return new Response(JSON.stringify(geminiResponse), {
|
|
1403
|
+
status: response.status,
|
|
1404
|
+
statusText: response.statusText,
|
|
1405
|
+
headers: { "Content-Type": "application/json" }
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
// src/transformer/transformers/utils/gemini.stream.ts
|
|
1410
|
+
async function transformResponseOut(response, providerName, logger) {
|
|
1411
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
1412
|
+
if (contentType.includes("application/json")) {
|
|
1413
|
+
return handleJsonResponse(response, providerName, logger);
|
|
1414
|
+
} else if (contentType.includes("stream") || contentType.includes("text/event-stream")) {
|
|
1415
|
+
return handleStreamResponse(response, providerName, logger);
|
|
1416
|
+
}
|
|
1417
|
+
return response;
|
|
1418
|
+
}
|
|
1419
|
+
async function handleJsonResponse(response, providerName, logger) {
|
|
1420
|
+
const jsonResponse = await response.json();
|
|
1421
|
+
logger?.debug(`${providerName} JSON response received`);
|
|
1422
|
+
const parts = jsonResponse.candidates?.[0]?.content?.parts || [];
|
|
1423
|
+
let thinkingContent = "";
|
|
1424
|
+
let thinkingSignature = "";
|
|
1425
|
+
const nonThinkingParts = [];
|
|
1426
|
+
for (const part of parts) {
|
|
1427
|
+
if (part.text && part.thought === true) {
|
|
1428
|
+
thinkingContent += part.text;
|
|
1429
|
+
} else {
|
|
1430
|
+
nonThinkingParts.push(part);
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
thinkingSignature = parts.find((part) => part.thoughtSignature)?.thoughtSignature ?? "";
|
|
1434
|
+
const toolCalls = nonThinkingParts.filter((part) => part.functionCall).map((part) => ({
|
|
1435
|
+
id: part.functionCall?.id || `tool_${Math.random().toString(36).substring(2, 15)}`,
|
|
1436
|
+
type: "function",
|
|
1437
|
+
function: {
|
|
1438
|
+
name: part.functionCall?.name ?? "",
|
|
1439
|
+
arguments: JSON.stringify(part.functionCall?.args || {})
|
|
1440
|
+
}
|
|
1441
|
+
}));
|
|
1442
|
+
const textContent = nonThinkingParts.filter((part) => part.text).map((part) => part.text).join("\n");
|
|
1443
|
+
const openAIResponse = {
|
|
1444
|
+
id: jsonResponse.responseId ?? "",
|
|
1445
|
+
choices: [
|
|
1446
|
+
{
|
|
1447
|
+
finish_reason: (jsonResponse.candidates?.[0]?.finishReason ?? "").toLowerCase() || null,
|
|
1448
|
+
index: 0,
|
|
1449
|
+
message: {
|
|
1450
|
+
content: textContent,
|
|
1451
|
+
role: "assistant",
|
|
1452
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1453
|
+
...thinkingSignature && {
|
|
1454
|
+
thinking: {
|
|
1455
|
+
content: thinkingContent || "(no content)",
|
|
1456
|
+
signature: thinkingSignature
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
],
|
|
1462
|
+
created: Math.floor(Date.now() / 1e3),
|
|
1463
|
+
model: jsonResponse.modelVersion ?? "",
|
|
1464
|
+
object: "chat.completion",
|
|
1465
|
+
usage: {
|
|
1466
|
+
completion_tokens: jsonResponse.usageMetadata?.candidatesTokenCount || 0,
|
|
1467
|
+
prompt_tokens: jsonResponse.usageMetadata?.promptTokenCount || 0,
|
|
1468
|
+
prompt_tokens_details: {
|
|
1469
|
+
cached_tokens: jsonResponse.usageMetadata?.cachedContentTokenCount || 0
|
|
1470
|
+
},
|
|
1471
|
+
total_tokens: jsonResponse.usageMetadata?.totalTokenCount || 0,
|
|
1472
|
+
output_tokens_details: {
|
|
1473
|
+
reasoning_tokens: jsonResponse.usageMetadata?.thoughtsTokenCount || 0
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
};
|
|
1477
|
+
return new Response(JSON.stringify(openAIResponse), {
|
|
1478
|
+
status: response.status,
|
|
1479
|
+
statusText: response.statusText,
|
|
1480
|
+
headers: response.headers
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
function handleStreamResponse(response, providerName, logger) {
|
|
1484
|
+
if (!response.body) {
|
|
1485
|
+
return response;
|
|
1486
|
+
}
|
|
1487
|
+
const decoder = new TextDecoder();
|
|
1488
|
+
const encoder = new TextEncoder();
|
|
1489
|
+
let signatureSent = false;
|
|
1490
|
+
let contentSent = false;
|
|
1491
|
+
let hasThinkingContent = false;
|
|
1492
|
+
let pendingContent = "";
|
|
1493
|
+
let contentIndex = 0;
|
|
1494
|
+
let toolCallIndex = -1;
|
|
1495
|
+
const stream = new ReadableStream({
|
|
1496
|
+
async start(controller) {
|
|
1497
|
+
const reader = response.body.getReader();
|
|
1498
|
+
let buffer = "";
|
|
1499
|
+
const processLine = async (line) => {
|
|
1500
|
+
if (!line.startsWith("data: ")) return;
|
|
1501
|
+
const chunkStr = line.slice(6).trim();
|
|
1502
|
+
if (!chunkStr) return;
|
|
1503
|
+
logger?.debug(`${providerName} chunk: ${chunkStr.substring(0, 100)}...`);
|
|
1504
|
+
try {
|
|
1505
|
+
const chunk = JSON.parse(chunkStr);
|
|
1506
|
+
if (!chunk.candidates?.[0]) {
|
|
1507
|
+
logger?.debug("Invalid chunk structure");
|
|
1508
|
+
return;
|
|
1509
|
+
}
|
|
1510
|
+
const candidate = chunk.candidates[0];
|
|
1511
|
+
const parts = candidate.content?.parts || [];
|
|
1512
|
+
parts.filter((part) => part.text && part.thought === true).forEach((part) => {
|
|
1513
|
+
hasThinkingContent = true;
|
|
1514
|
+
const thinkingChunk = createChunk({
|
|
1515
|
+
responseId: chunk.responseId,
|
|
1516
|
+
modelVersion: chunk.modelVersion,
|
|
1517
|
+
contentIndex,
|
|
1518
|
+
delta: {
|
|
1519
|
+
role: "assistant",
|
|
1520
|
+
content: null,
|
|
1521
|
+
thinking: { content: part.text }
|
|
1522
|
+
}
|
|
1523
|
+
});
|
|
1524
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(thinkingChunk)}
|
|
1525
|
+
|
|
1526
|
+
`));
|
|
1527
|
+
});
|
|
1528
|
+
const signature = parts.find((part) => part.thoughtSignature)?.thoughtSignature;
|
|
1529
|
+
if (signature && !signatureSent) {
|
|
1530
|
+
if (!hasThinkingContent) {
|
|
1531
|
+
const thinkingChunk = createChunk({
|
|
1532
|
+
responseId: chunk.responseId,
|
|
1533
|
+
modelVersion: chunk.modelVersion,
|
|
1534
|
+
contentIndex,
|
|
1535
|
+
delta: {
|
|
1536
|
+
role: "assistant",
|
|
1537
|
+
content: null,
|
|
1538
|
+
thinking: { content: "" }
|
|
1539
|
+
}
|
|
1540
|
+
});
|
|
1541
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(thinkingChunk)}
|
|
1542
|
+
|
|
1543
|
+
`));
|
|
1544
|
+
}
|
|
1545
|
+
const signatureChunk = createChunk({
|
|
1546
|
+
responseId: chunk.responseId,
|
|
1547
|
+
modelVersion: chunk.modelVersion,
|
|
1548
|
+
contentIndex,
|
|
1549
|
+
delta: {
|
|
1550
|
+
role: "assistant",
|
|
1551
|
+
content: null,
|
|
1552
|
+
thinking: { signature }
|
|
1553
|
+
}
|
|
1554
|
+
});
|
|
1555
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(signatureChunk)}
|
|
1556
|
+
|
|
1557
|
+
`));
|
|
1558
|
+
signatureSent = true;
|
|
1559
|
+
contentIndex++;
|
|
1560
|
+
if (pendingContent) {
|
|
1561
|
+
const pendingChunk = createChunk({
|
|
1562
|
+
responseId: chunk.responseId,
|
|
1563
|
+
modelVersion: chunk.modelVersion,
|
|
1564
|
+
contentIndex,
|
|
1565
|
+
delta: { role: "assistant", content: pendingContent }
|
|
1566
|
+
});
|
|
1567
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(pendingChunk)}
|
|
1568
|
+
|
|
1569
|
+
`));
|
|
1570
|
+
pendingContent = "";
|
|
1571
|
+
contentSent = true;
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
const toolCalls = parts.filter((part) => part.functionCall).map((part) => ({
|
|
1575
|
+
id: part.functionCall?.id || `ccr_tool_${Math.random().toString(36).substring(2, 15)}`,
|
|
1576
|
+
type: "function",
|
|
1577
|
+
function: {
|
|
1578
|
+
name: part.functionCall?.name ?? "",
|
|
1579
|
+
arguments: JSON.stringify(part.functionCall?.args || {})
|
|
1580
|
+
}
|
|
1581
|
+
}));
|
|
1582
|
+
const textContent = parts.filter((part) => part.text && part.thought !== true).map((part) => part.text).join("\n");
|
|
1583
|
+
if (!textContent && signatureSent && !contentSent) {
|
|
1584
|
+
contentSent = true;
|
|
1585
|
+
}
|
|
1586
|
+
if (hasThinkingContent && textContent && !signatureSent) {
|
|
1587
|
+
if (chunk.modelVersion?.includes("3")) {
|
|
1588
|
+
pendingContent += textContent;
|
|
1589
|
+
return;
|
|
1590
|
+
} else {
|
|
1591
|
+
const signatureChunk = createChunk({
|
|
1592
|
+
responseId: chunk.responseId,
|
|
1593
|
+
modelVersion: chunk.modelVersion,
|
|
1594
|
+
contentIndex,
|
|
1595
|
+
delta: {
|
|
1596
|
+
role: "assistant",
|
|
1597
|
+
content: null,
|
|
1598
|
+
thinking: { signature: `ccr_${Date.now()}` }
|
|
1599
|
+
}
|
|
1600
|
+
});
|
|
1601
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(signatureChunk)}
|
|
1602
|
+
|
|
1603
|
+
`));
|
|
1604
|
+
signatureSent = true;
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
if (textContent) {
|
|
1608
|
+
if (!pendingContent) contentIndex++;
|
|
1609
|
+
const contentChunk = createChunk({
|
|
1610
|
+
responseId: chunk.responseId,
|
|
1611
|
+
modelVersion: chunk.modelVersion,
|
|
1612
|
+
contentIndex,
|
|
1613
|
+
delta: { role: "assistant", content: textContent },
|
|
1614
|
+
finishReason: candidate.finishReason,
|
|
1615
|
+
usageMetadata: chunk.usageMetadata,
|
|
1616
|
+
groundingMetadata: candidate.groundingMetadata
|
|
1617
|
+
});
|
|
1618
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(contentChunk)}
|
|
1619
|
+
|
|
1620
|
+
`));
|
|
1621
|
+
contentSent = true;
|
|
1622
|
+
}
|
|
1623
|
+
if (toolCalls.length > 0) {
|
|
1624
|
+
for (const tool of toolCalls) {
|
|
1625
|
+
contentIndex++;
|
|
1626
|
+
toolCallIndex++;
|
|
1627
|
+
const toolChunk = createChunk({
|
|
1628
|
+
responseId: chunk.responseId,
|
|
1629
|
+
modelVersion: chunk.modelVersion,
|
|
1630
|
+
contentIndex,
|
|
1631
|
+
delta: {
|
|
1632
|
+
role: "assistant",
|
|
1633
|
+
tool_calls: [{ ...tool, index: toolCallIndex }]
|
|
1634
|
+
},
|
|
1635
|
+
finishReason: candidate.finishReason,
|
|
1636
|
+
usageMetadata: chunk.usageMetadata,
|
|
1637
|
+
groundingMetadata: candidate.groundingMetadata
|
|
1638
|
+
});
|
|
1639
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(toolChunk)}
|
|
1640
|
+
|
|
1641
|
+
`));
|
|
1642
|
+
}
|
|
1643
|
+
contentSent = true;
|
|
1644
|
+
}
|
|
1645
|
+
} catch (_error) {
|
|
1646
|
+
logger?.error(`Error parsing ${providerName} stream chunk: ${chunkStr}`);
|
|
1647
|
+
}
|
|
1648
|
+
};
|
|
1649
|
+
try {
|
|
1650
|
+
while (true) {
|
|
1651
|
+
const { done, value } = await reader.read();
|
|
1652
|
+
if (done) {
|
|
1653
|
+
if (buffer) await processLine(buffer);
|
|
1654
|
+
break;
|
|
1655
|
+
}
|
|
1656
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1657
|
+
const lines = buffer.split("\n");
|
|
1658
|
+
buffer = lines.pop() || "";
|
|
1659
|
+
for (const line of lines) {
|
|
1660
|
+
await processLine(line);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
} catch (error) {
|
|
1664
|
+
controller.error(error);
|
|
1665
|
+
} finally {
|
|
1666
|
+
controller.close();
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
});
|
|
1670
|
+
return new Response(stream, {
|
|
1671
|
+
status: response.status,
|
|
1672
|
+
statusText: response.statusText,
|
|
1673
|
+
headers: response.headers
|
|
1674
|
+
});
|
|
1675
|
+
}
|
|
1676
|
+
function createChunk(options) {
|
|
1677
|
+
const {
|
|
1678
|
+
responseId,
|
|
1679
|
+
modelVersion,
|
|
1680
|
+
contentIndex,
|
|
1681
|
+
delta,
|
|
1682
|
+
finishReason,
|
|
1683
|
+
usageMetadata,
|
|
1684
|
+
groundingMetadata
|
|
1685
|
+
} = options;
|
|
1686
|
+
const chunk = {
|
|
1687
|
+
choices: [
|
|
1688
|
+
{
|
|
1689
|
+
delta,
|
|
1690
|
+
finish_reason: finishReason?.toLowerCase() || null,
|
|
1691
|
+
index: contentIndex,
|
|
1692
|
+
logprobs: null
|
|
1693
|
+
}
|
|
1694
|
+
],
|
|
1695
|
+
created: Math.floor(Date.now() / 1e3),
|
|
1696
|
+
id: responseId || "",
|
|
1697
|
+
model: modelVersion || "",
|
|
1698
|
+
object: "chat.completion.chunk",
|
|
1699
|
+
system_fingerprint: "fp_a49d71b8a1"
|
|
1700
|
+
};
|
|
1701
|
+
if (usageMetadata) {
|
|
1702
|
+
chunk.usage = {
|
|
1703
|
+
completion_tokens: usageMetadata.candidatesTokenCount || 0,
|
|
1704
|
+
prompt_tokens: usageMetadata.promptTokenCount || 0,
|
|
1705
|
+
prompt_tokens_details: {
|
|
1706
|
+
cached_tokens: usageMetadata.cachedContentTokenCount || 0
|
|
1707
|
+
},
|
|
1708
|
+
total_tokens: usageMetadata.totalTokenCount || 0,
|
|
1709
|
+
output_tokens_details: {
|
|
1710
|
+
reasoning_tokens: usageMetadata.thoughtsTokenCount || 0
|
|
1711
|
+
}
|
|
1712
|
+
};
|
|
1713
|
+
}
|
|
1714
|
+
if (groundingMetadata?.groundingChunks?.length) {
|
|
1715
|
+
const annotations = groundingMetadata.groundingChunks.map((groundingChunk, index) => {
|
|
1716
|
+
const support = groundingMetadata.groundingSupports?.find(
|
|
1717
|
+
(s) => s.groundingChunkIndices?.includes(index)
|
|
1718
|
+
);
|
|
1719
|
+
return {
|
|
1720
|
+
type: "url_citation",
|
|
1721
|
+
url_citation: {
|
|
1722
|
+
url: groundingChunk.web?.uri || "",
|
|
1723
|
+
title: groundingChunk.web?.title || "",
|
|
1724
|
+
content: support?.segment?.text || "",
|
|
1725
|
+
start_index: support?.segment?.startIndex || 0,
|
|
1726
|
+
end_index: support?.segment?.endIndex || 0
|
|
1727
|
+
}
|
|
1728
|
+
};
|
|
1729
|
+
});
|
|
1730
|
+
chunk.choices[0].delta.annotations = annotations;
|
|
1731
|
+
}
|
|
1732
|
+
return chunk;
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
// src/transformer/transformers/utils/gemini.schema.ts
|
|
1736
|
+
var GeminiType = {
|
|
1737
|
+
TYPE_UNSPECIFIED: "TYPE_UNSPECIFIED",
|
|
1738
|
+
STRING: "STRING",
|
|
1739
|
+
NUMBER: "NUMBER",
|
|
1740
|
+
INTEGER: "INTEGER",
|
|
1741
|
+
BOOLEAN: "BOOLEAN",
|
|
1742
|
+
ARRAY: "ARRAY",
|
|
1743
|
+
OBJECT: "OBJECT",
|
|
1744
|
+
NULL: "NULL"
|
|
1745
|
+
};
|
|
1746
|
+
function flattenTypeArrayToAnyOf(typeList, resultingSchema) {
|
|
1747
|
+
if (typeList.includes("null")) {
|
|
1748
|
+
resultingSchema.nullable = true;
|
|
1749
|
+
}
|
|
1750
|
+
const listWithoutNull = typeList.filter((type) => type !== "null");
|
|
1751
|
+
if (listWithoutNull.length === 1) {
|
|
1752
|
+
const upperCaseType = listWithoutNull[0].toUpperCase();
|
|
1753
|
+
resultingSchema.type = Object.values(GeminiType).includes(upperCaseType) ? upperCaseType : GeminiType.TYPE_UNSPECIFIED;
|
|
1754
|
+
} else {
|
|
1755
|
+
resultingSchema.anyOf = listWithoutNull.map((typeName) => {
|
|
1756
|
+
const upperCaseType = typeName.toUpperCase();
|
|
1757
|
+
return {
|
|
1758
|
+
type: Object.values(GeminiType).includes(upperCaseType) ? upperCaseType : GeminiType.TYPE_UNSPECIFIED
|
|
1759
|
+
};
|
|
1760
|
+
});
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
function processJsonSchema(jsonSchema) {
|
|
1764
|
+
const genAISchema = {};
|
|
1765
|
+
const schemaFieldNames = ["items"];
|
|
1766
|
+
const listSchemaFieldNames = ["anyOf"];
|
|
1767
|
+
const dictSchemaFieldNames = ["properties"];
|
|
1768
|
+
let workingSchema = jsonSchema;
|
|
1769
|
+
if (workingSchema.type && workingSchema.anyOf) {
|
|
1770
|
+
throw new Error("type and anyOf cannot be both populated.");
|
|
1771
|
+
}
|
|
1772
|
+
const incomingAnyOf = workingSchema.anyOf;
|
|
1773
|
+
if (incomingAnyOf && Array.isArray(incomingAnyOf) && incomingAnyOf.length === 2) {
|
|
1774
|
+
if (incomingAnyOf[0]?.type === "null") {
|
|
1775
|
+
genAISchema.nullable = true;
|
|
1776
|
+
workingSchema = incomingAnyOf[1];
|
|
1777
|
+
} else if (incomingAnyOf[1]?.type === "null") {
|
|
1778
|
+
genAISchema.nullable = true;
|
|
1779
|
+
workingSchema = incomingAnyOf[0];
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
if (workingSchema.type && Array.isArray(workingSchema.type)) {
|
|
1783
|
+
flattenTypeArrayToAnyOf(workingSchema.type, genAISchema);
|
|
1784
|
+
}
|
|
1785
|
+
for (const [fieldName, fieldValue] of Object.entries(workingSchema)) {
|
|
1786
|
+
if (fieldValue == null) {
|
|
1787
|
+
continue;
|
|
1788
|
+
}
|
|
1789
|
+
if (fieldName === "type") {
|
|
1790
|
+
if (fieldValue === "null") {
|
|
1791
|
+
throw new Error("type: null cannot be the only possible type for the field.");
|
|
1792
|
+
}
|
|
1793
|
+
if (Array.isArray(fieldValue)) {
|
|
1794
|
+
continue;
|
|
1795
|
+
}
|
|
1796
|
+
const upperCaseValue = fieldValue.toUpperCase();
|
|
1797
|
+
genAISchema.type = Object.values(GeminiType).includes(upperCaseValue) ? upperCaseValue : GeminiType.TYPE_UNSPECIFIED;
|
|
1798
|
+
} else if (schemaFieldNames.includes(fieldName)) {
|
|
1799
|
+
genAISchema[fieldName] = processJsonSchema(fieldValue);
|
|
1800
|
+
} else if (listSchemaFieldNames.includes(fieldName)) {
|
|
1801
|
+
const listValue = [];
|
|
1802
|
+
for (const item of fieldValue) {
|
|
1803
|
+
if (item.type === "null") {
|
|
1804
|
+
genAISchema.nullable = true;
|
|
1805
|
+
continue;
|
|
1806
|
+
}
|
|
1807
|
+
listValue.push(processJsonSchema(item));
|
|
1808
|
+
}
|
|
1809
|
+
genAISchema[fieldName] = listValue;
|
|
1810
|
+
} else if (dictSchemaFieldNames.includes(fieldName)) {
|
|
1811
|
+
const dictValue = {};
|
|
1812
|
+
for (const [key, value] of Object.entries(fieldValue)) {
|
|
1813
|
+
dictValue[key] = processJsonSchema(value);
|
|
1814
|
+
}
|
|
1815
|
+
genAISchema[fieldName] = dictValue;
|
|
1816
|
+
} else {
|
|
1817
|
+
if (fieldName === "additionalProperties") {
|
|
1818
|
+
continue;
|
|
1819
|
+
}
|
|
1820
|
+
genAISchema[fieldName] = fieldValue;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
return genAISchema;
|
|
1824
|
+
}
|
|
1825
|
+
function transformTool(tool) {
|
|
1826
|
+
const functionDeclarations = tool.functionDeclarations;
|
|
1827
|
+
if (functionDeclarations) {
|
|
1828
|
+
for (const functionDeclaration of functionDeclarations) {
|
|
1829
|
+
if (functionDeclaration.parameters) {
|
|
1830
|
+
const params = functionDeclaration.parameters;
|
|
1831
|
+
if (!Object.keys(params).includes("$schema")) {
|
|
1832
|
+
functionDeclaration.parameters = processJsonSchema(params);
|
|
1833
|
+
} else {
|
|
1834
|
+
if (!functionDeclaration.parametersJsonSchema) {
|
|
1835
|
+
functionDeclaration.parametersJsonSchema = functionDeclaration.parameters;
|
|
1836
|
+
delete functionDeclaration.parameters;
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
if (functionDeclaration.response) {
|
|
1841
|
+
const response = functionDeclaration.response;
|
|
1842
|
+
if (!Object.keys(response).includes("$schema")) {
|
|
1843
|
+
functionDeclaration.response = processJsonSchema(response);
|
|
1844
|
+
} else {
|
|
1845
|
+
if (!functionDeclaration.responseJsonSchema) {
|
|
1846
|
+
functionDeclaration.responseJsonSchema = functionDeclaration.response;
|
|
1847
|
+
delete functionDeclaration.response;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
return tool;
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
// src/transformer/transformers/utils/gemini.util.ts
|
|
1857
|
+
function buildRequestBody(request) {
|
|
1858
|
+
const tools = [];
|
|
1859
|
+
const functionDeclarations = request.tools?.filter((tool) => tool.function.name !== "web_search")?.map((tool) => ({
|
|
1860
|
+
name: tool.function.name,
|
|
1861
|
+
description: tool.function.description,
|
|
1862
|
+
parametersJsonSchema: tool.function.parameters
|
|
1863
|
+
}));
|
|
1864
|
+
if (functionDeclarations?.length) {
|
|
1865
|
+
tools.push(
|
|
1866
|
+
transformTool({
|
|
1867
|
+
functionDeclarations
|
|
1868
|
+
})
|
|
1869
|
+
);
|
|
1870
|
+
}
|
|
1871
|
+
const webSearch = request.tools?.find((tool) => tool.function.name === "web_search");
|
|
1872
|
+
if (webSearch) {
|
|
1873
|
+
tools.push({ googleSearch: {} });
|
|
1874
|
+
}
|
|
1875
|
+
const contents = [];
|
|
1876
|
+
const toolResponses = request.messages.filter((item) => item.role === "tool");
|
|
1877
|
+
request.messages.filter((item) => item.role !== "tool").forEach((message) => {
|
|
1878
|
+
let role;
|
|
1879
|
+
if (message.role === "assistant") {
|
|
1880
|
+
role = "model";
|
|
1881
|
+
} else if (["user", "system"].includes(message.role)) {
|
|
1882
|
+
role = "user";
|
|
1883
|
+
} else {
|
|
1884
|
+
role = "user";
|
|
1885
|
+
}
|
|
1886
|
+
const parts = [];
|
|
1887
|
+
if (typeof message.content === "string") {
|
|
1888
|
+
const part = { text: message.content };
|
|
1889
|
+
if (message.thinking?.signature) {
|
|
1890
|
+
part.thoughtSignature = message.thinking.signature;
|
|
1891
|
+
}
|
|
1892
|
+
parts.push(part);
|
|
1893
|
+
} else if (Array.isArray(message.content)) {
|
|
1894
|
+
for (const content of message.content) {
|
|
1895
|
+
if (content.type === "text") {
|
|
1896
|
+
parts.push({ text: content.text || "" });
|
|
1897
|
+
} else if (content.type === "image_url") {
|
|
1898
|
+
const imageUrl = content.image_url?.url ?? "";
|
|
1899
|
+
if (imageUrl.startsWith("http")) {
|
|
1900
|
+
parts.push({
|
|
1901
|
+
file_data: {
|
|
1902
|
+
mime_type: content.media_type,
|
|
1903
|
+
file_uri: imageUrl
|
|
1904
|
+
}
|
|
1905
|
+
});
|
|
1906
|
+
} else {
|
|
1907
|
+
const data = imageUrl.split(",").pop() || imageUrl;
|
|
1908
|
+
parts.push({
|
|
1909
|
+
inlineData: {
|
|
1910
|
+
mime_type: content.media_type || "image/png",
|
|
1911
|
+
data
|
|
1912
|
+
}
|
|
1913
|
+
});
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
} else if (message.content && typeof message.content === "object") {
|
|
1918
|
+
const contentObj = message.content;
|
|
1919
|
+
if (contentObj.text) {
|
|
1920
|
+
parts.push({ text: contentObj.text });
|
|
1921
|
+
} else {
|
|
1922
|
+
parts.push({ text: JSON.stringify(message.content) });
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
if (Array.isArray(message.tool_calls)) {
|
|
1926
|
+
for (let index = 0; index < message.tool_calls.length; index++) {
|
|
1927
|
+
const toolCall = message.tool_calls[index];
|
|
1928
|
+
const functionCallPart = {
|
|
1929
|
+
functionCall: {
|
|
1930
|
+
id: toolCall.id || `tool_${Math.random().toString(36).substring(2, 15)}`,
|
|
1931
|
+
name: toolCall.function.name,
|
|
1932
|
+
args: JSON.parse(toolCall.function.arguments || "{}")
|
|
1933
|
+
}
|
|
1934
|
+
};
|
|
1935
|
+
if (index === 0 && message.thinking?.signature) {
|
|
1936
|
+
functionCallPart.thoughtSignature = message.thinking.signature;
|
|
1937
|
+
}
|
|
1938
|
+
parts.push(functionCallPart);
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
if (parts.length === 0) {
|
|
1942
|
+
parts.push({ text: "" });
|
|
1943
|
+
}
|
|
1944
|
+
contents.push({ role, parts });
|
|
1945
|
+
if (role === "model" && message.tool_calls) {
|
|
1946
|
+
const functionResponses = message.tool_calls.map(
|
|
1947
|
+
(tool) => {
|
|
1948
|
+
const response = toolResponses.find((item) => item.tool_call_id === tool.id);
|
|
1949
|
+
return {
|
|
1950
|
+
functionResponse: {
|
|
1951
|
+
name: tool.function?.name ?? "",
|
|
1952
|
+
response: { result: response?.content }
|
|
1953
|
+
}
|
|
1954
|
+
};
|
|
1955
|
+
}
|
|
1956
|
+
);
|
|
1957
|
+
contents.push({
|
|
1958
|
+
role: "user",
|
|
1959
|
+
parts: functionResponses
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
});
|
|
1963
|
+
const generationConfig = {};
|
|
1964
|
+
if (request.reasoning?.effort && request.reasoning.effort !== "none") {
|
|
1965
|
+
generationConfig.thinkingConfig = {
|
|
1966
|
+
includeThoughts: true
|
|
1967
|
+
};
|
|
1968
|
+
if (request.model.includes("gemini-3")) {
|
|
1969
|
+
generationConfig.thinkingConfig.thinkingLevel = request.reasoning.effort;
|
|
1970
|
+
} else {
|
|
1971
|
+
const thinkingBudgets = request.model.includes("pro") ? [128, 32768] : [0, 24576];
|
|
1972
|
+
const maxTokens = request.reasoning.max_tokens;
|
|
1973
|
+
if (typeof maxTokens !== "undefined") {
|
|
1974
|
+
let thinkingBudget;
|
|
1975
|
+
if (maxTokens >= thinkingBudgets[0] && maxTokens <= thinkingBudgets[1]) {
|
|
1976
|
+
thinkingBudget = maxTokens;
|
|
1977
|
+
} else if (maxTokens < thinkingBudgets[0]) {
|
|
1978
|
+
thinkingBudget = thinkingBudgets[0];
|
|
1979
|
+
} else {
|
|
1980
|
+
thinkingBudget = thinkingBudgets[1];
|
|
1981
|
+
}
|
|
1982
|
+
generationConfig.thinkingConfig.thinkingBudget = thinkingBudget;
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
const body = {
|
|
1987
|
+
contents,
|
|
1988
|
+
tools: tools.length > 0 ? tools : void 0,
|
|
1989
|
+
generationConfig: Object.keys(generationConfig).length > 0 ? generationConfig : void 0
|
|
1990
|
+
};
|
|
1991
|
+
if (request.tool_choice) {
|
|
1992
|
+
const toolConfig = {
|
|
1993
|
+
functionCallingConfig: {}
|
|
1994
|
+
};
|
|
1995
|
+
if (request.tool_choice === "auto") {
|
|
1996
|
+
toolConfig.functionCallingConfig.mode = "auto";
|
|
1997
|
+
} else if (request.tool_choice === "none") {
|
|
1998
|
+
toolConfig.functionCallingConfig.mode = "none";
|
|
1999
|
+
} else if (request.tool_choice === "required") {
|
|
2000
|
+
toolConfig.functionCallingConfig.mode = "any";
|
|
2001
|
+
} else if (typeof request.tool_choice === "object" && request.tool_choice.function?.name) {
|
|
2002
|
+
toolConfig.functionCallingConfig.mode = "any";
|
|
2003
|
+
toolConfig.functionCallingConfig.allowedFunctionNames = [
|
|
2004
|
+
request.tool_choice.function.name
|
|
2005
|
+
];
|
|
2006
|
+
}
|
|
2007
|
+
body.toolConfig = toolConfig;
|
|
2008
|
+
}
|
|
2009
|
+
return body;
|
|
2010
|
+
}
|
|
2011
|
+
function transformRequestOut(request) {
|
|
2012
|
+
const contents = request.contents;
|
|
2013
|
+
const tools = request.tools;
|
|
2014
|
+
const model = request.model;
|
|
2015
|
+
const maxTokens = request.max_tokens;
|
|
2016
|
+
const temperature = request.temperature;
|
|
2017
|
+
const stream = request.stream;
|
|
2018
|
+
const toolChoice = request.tool_choice;
|
|
2019
|
+
const unifiedRequest = {
|
|
2020
|
+
messages: [],
|
|
2021
|
+
model,
|
|
2022
|
+
max_tokens: maxTokens,
|
|
2023
|
+
temperature,
|
|
2024
|
+
stream,
|
|
2025
|
+
tool_choice: toolChoice
|
|
2026
|
+
};
|
|
2027
|
+
if (Array.isArray(contents)) {
|
|
2028
|
+
for (const content of contents) {
|
|
2029
|
+
if (typeof content === "string") {
|
|
2030
|
+
unifiedRequest.messages.push({
|
|
2031
|
+
role: "user",
|
|
2032
|
+
content
|
|
2033
|
+
});
|
|
2034
|
+
} else if ("text" in content && typeof content.text === "string") {
|
|
2035
|
+
unifiedRequest.messages.push({
|
|
2036
|
+
role: "user",
|
|
2037
|
+
content: content.text || null
|
|
2038
|
+
});
|
|
2039
|
+
} else if ("role" in content && content.role === "user") {
|
|
2040
|
+
const geminiContent = content;
|
|
2041
|
+
unifiedRequest.messages.push({
|
|
2042
|
+
role: "user",
|
|
2043
|
+
content: geminiContent.parts?.map((part) => ({
|
|
2044
|
+
type: "text",
|
|
2045
|
+
text: part.text || ""
|
|
2046
|
+
})) || []
|
|
2047
|
+
});
|
|
2048
|
+
} else if (content.role === "model") {
|
|
2049
|
+
unifiedRequest.messages.push({
|
|
2050
|
+
role: "assistant",
|
|
2051
|
+
content: content.parts?.map((part) => ({
|
|
2052
|
+
type: "text",
|
|
2053
|
+
text: part.text || ""
|
|
2054
|
+
})) || []
|
|
2055
|
+
});
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
if (Array.isArray(tools)) {
|
|
2060
|
+
unifiedRequest.tools = [];
|
|
2061
|
+
for (const tool of tools) {
|
|
2062
|
+
if (Array.isArray(tool.functionDeclarations)) {
|
|
2063
|
+
for (const funcDecl of tool.functionDeclarations) {
|
|
2064
|
+
unifiedRequest.tools.push({
|
|
2065
|
+
type: "function",
|
|
2066
|
+
function: {
|
|
2067
|
+
name: funcDecl.name,
|
|
2068
|
+
description: funcDecl.description ?? "",
|
|
2069
|
+
parameters: funcDecl.parameters ?? {}
|
|
2070
|
+
}
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
return unifiedRequest;
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
// src/transformer/transformers/GeminiCodeAssistTransformer.ts
|
|
2080
|
+
var DEFAULT_CODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com";
|
|
2081
|
+
var DEFAULT_CODE_ASSIST_API_VERSION = "v1internal";
|
|
2082
|
+
function resolveCodeAssistEndpoint() {
|
|
2083
|
+
return (process.env.CODE_ASSIST_ENDPOINT || DEFAULT_CODE_ASSIST_ENDPOINT).replace(/\/+$/, "");
|
|
2084
|
+
}
|
|
2085
|
+
function resolveCodeAssistApiVersion() {
|
|
2086
|
+
return process.env.CODE_ASSIST_API_VERSION || DEFAULT_CODE_ASSIST_API_VERSION;
|
|
2087
|
+
}
|
|
2088
|
+
function buildCodeAssistUrl(stream) {
|
|
2089
|
+
const base = resolveCodeAssistEndpoint();
|
|
2090
|
+
const version = resolveCodeAssistApiVersion();
|
|
2091
|
+
const method = stream ? "streamGenerateContent?alt=sse" : "generateContent";
|
|
2092
|
+
return `${base}/${version}:${method}`;
|
|
2093
|
+
}
|
|
2094
|
+
function generateUserPromptId() {
|
|
2095
|
+
const c = globalThis.crypto;
|
|
2096
|
+
if (c?.randomUUID) return c.randomUUID();
|
|
2097
|
+
return `omnicross-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
2098
|
+
}
|
|
2099
|
+
function peelResponseEnvelope(parsed) {
|
|
2100
|
+
if (parsed && typeof parsed === "object" && "response" in parsed) {
|
|
2101
|
+
return parsed.response;
|
|
2102
|
+
}
|
|
2103
|
+
return parsed;
|
|
2104
|
+
}
|
|
2105
|
+
async function unwrapCodeAssistResponse(response) {
|
|
2106
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
2107
|
+
if (contentType.includes("stream") || contentType.includes("text/event-stream")) {
|
|
2108
|
+
const sourceBody = response.body;
|
|
2109
|
+
if (!sourceBody) return response;
|
|
2110
|
+
const decoder = new TextDecoder();
|
|
2111
|
+
const encoder = new TextEncoder();
|
|
2112
|
+
const peeled = new ReadableStream({
|
|
2113
|
+
async start(controller) {
|
|
2114
|
+
const reader = sourceBody.getReader();
|
|
2115
|
+
let buffer = "";
|
|
2116
|
+
const processLine = (line) => {
|
|
2117
|
+
if (!line.startsWith("data:")) {
|
|
2118
|
+
if (line.length > 0) controller.enqueue(encoder.encode(`${line}
|
|
2119
|
+
`));
|
|
2120
|
+
return;
|
|
2121
|
+
}
|
|
2122
|
+
const payload = line.slice(line.indexOf(":") + 1).trim();
|
|
2123
|
+
if (!payload || payload === "[DONE]") {
|
|
2124
|
+
controller.enqueue(encoder.encode(`${line}
|
|
2125
|
+
`));
|
|
2126
|
+
return;
|
|
2127
|
+
}
|
|
2128
|
+
try {
|
|
2129
|
+
const parsed = JSON.parse(payload);
|
|
2130
|
+
const inner2 = peelResponseEnvelope(parsed);
|
|
2131
|
+
controller.enqueue(encoder.encode(`data: ${JSON.stringify(inner2)}
|
|
2132
|
+
`));
|
|
2133
|
+
} catch {
|
|
2134
|
+
controller.enqueue(encoder.encode(`${line}
|
|
2135
|
+
`));
|
|
2136
|
+
}
|
|
2137
|
+
};
|
|
2138
|
+
try {
|
|
2139
|
+
while (true) {
|
|
2140
|
+
const { done, value } = await reader.read();
|
|
2141
|
+
if (done) {
|
|
2142
|
+
if (buffer) processLine(buffer);
|
|
2143
|
+
break;
|
|
2144
|
+
}
|
|
2145
|
+
buffer += decoder.decode(value, { stream: true });
|
|
2146
|
+
const lines = buffer.split("\n");
|
|
2147
|
+
buffer = lines.pop() || "";
|
|
2148
|
+
for (const line of lines) processLine(line);
|
|
2149
|
+
}
|
|
2150
|
+
} catch (err) {
|
|
2151
|
+
controller.error(err);
|
|
2152
|
+
} finally {
|
|
2153
|
+
controller.close();
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
});
|
|
2157
|
+
return new Response(peeled, {
|
|
2158
|
+
status: response.status,
|
|
2159
|
+
statusText: response.statusText,
|
|
2160
|
+
headers: response.headers
|
|
2161
|
+
});
|
|
2162
|
+
}
|
|
2163
|
+
const raw = await response.json().catch(() => null);
|
|
2164
|
+
const inner = peelResponseEnvelope(raw);
|
|
2165
|
+
return new Response(JSON.stringify(inner), {
|
|
2166
|
+
status: response.status,
|
|
2167
|
+
statusText: response.statusText,
|
|
2168
|
+
headers: response.headers
|
|
2169
|
+
});
|
|
2170
|
+
}
|
|
2171
|
+
var GeminiCodeAssistTransformer = class {
|
|
2172
|
+
static TransformerName = "gemini-code-assist";
|
|
2173
|
+
name = "gemini-code-assist";
|
|
2174
|
+
logger;
|
|
2175
|
+
/** Code Assist has no fixed `/models/:modelAndAction` endpoint pattern — the
|
|
2176
|
+
* URL is built per-request in `transformRequestIn`. Left undefined so the
|
|
2177
|
+
* TransformerService does NOT treat this as an endpoint (reverse) transformer. */
|
|
2178
|
+
endPoint = void 0;
|
|
2179
|
+
/**
|
|
2180
|
+
* unified → Code Assist envelope.
|
|
2181
|
+
*
|
|
2182
|
+
* Builds the inner public-Gemini body via the shared `buildRequestBody`, then
|
|
2183
|
+
* wraps it as `{ model, project, user_prompt_id, request: <inner> }` and sets
|
|
2184
|
+
* the Code Assist URL + Bearer-only headers.
|
|
2185
|
+
*
|
|
2186
|
+
* The resolved Code Assist `project` is threaded in via `provider.geminiProject`
|
|
2187
|
+
* (stashed by the subscription dispatch seam — see SubscriptionDispatcher /
|
|
2188
|
+
* openaiResponsesIngress). `undefined` is the valid fresh free-tier value.
|
|
2189
|
+
*/
|
|
2190
|
+
async transformRequestIn(request, provider, _context) {
|
|
2191
|
+
const inner = buildRequestBody(request);
|
|
2192
|
+
const envelope = {
|
|
2193
|
+
model: request.model,
|
|
2194
|
+
project: provider.geminiProject,
|
|
2195
|
+
user_prompt_id: generateUserPromptId(),
|
|
2196
|
+
request: inner
|
|
2197
|
+
};
|
|
2198
|
+
const url = buildCodeAssistUrl(Boolean(request.stream));
|
|
2199
|
+
const headers = {
|
|
2200
|
+
"x-goog-api-key": void 0,
|
|
2201
|
+
"X-Goog-Api-Key": void 0
|
|
2202
|
+
};
|
|
2203
|
+
return {
|
|
2204
|
+
body: envelope,
|
|
2205
|
+
config: { url, headers }
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2208
|
+
/**
|
|
2209
|
+
* Code Assist request → unified (endpoint-decode parity with GeminiTransformer).
|
|
2210
|
+
* Peels the top-level `request` envelope first, then delegates to the shared
|
|
2211
|
+
* gemini request decoder. Not used on the subscription dispatch path (the
|
|
2212
|
+
* endpoint transformer there is Anthropic/OpenAI-Response), but provided for
|
|
2213
|
+
* completeness so this transformer is a drop-in for the gemini one.
|
|
2214
|
+
*/
|
|
2215
|
+
async transformRequestOut(request, _context) {
|
|
2216
|
+
const r = request;
|
|
2217
|
+
const inner = r && typeof r === "object" && "request" in r ? r.request : r;
|
|
2218
|
+
if (inner && typeof inner === "object" && !("model" in inner) && "model" in r) {
|
|
2219
|
+
inner.model = r.model;
|
|
2220
|
+
}
|
|
2221
|
+
return transformRequestOut(inner);
|
|
2222
|
+
}
|
|
2223
|
+
/**
|
|
2224
|
+
* Code Assist response → OpenAI-compatible. PEEL the `.response` envelope from
|
|
2225
|
+
* the body / each SSE chunk, then DELEGATE to the existing gemini parser.
|
|
2226
|
+
*/
|
|
2227
|
+
async transformResponseOut(response, _context) {
|
|
2228
|
+
const unwrapped = await unwrapCodeAssistResponse(response);
|
|
2229
|
+
return transformResponseOut(unwrapped, this.name, this.logger);
|
|
2230
|
+
}
|
|
2231
|
+
/**
|
|
2232
|
+
* OpenAI-compatible response → Code Assist (endpoint-encode parity). Re-wraps
|
|
2233
|
+
* the standard gemini response under the top-level `response` key after the
|
|
2234
|
+
* shared gemini encoder produces a public-Gemini body. Symmetric with
|
|
2235
|
+
* `transformResponseOut`'s peel.
|
|
2236
|
+
*/
|
|
2237
|
+
async transformResponseIn(response, _context) {
|
|
2238
|
+
const geminiResponse = await transformResponseIn(response, this.logger);
|
|
2239
|
+
const contentType = geminiResponse.headers.get("Content-Type") ?? "";
|
|
2240
|
+
if (contentType.includes("text/event-stream")) {
|
|
2241
|
+
return geminiResponse;
|
|
2242
|
+
}
|
|
2243
|
+
const data = await geminiResponse.json().catch(() => null);
|
|
2244
|
+
return new Response(JSON.stringify({ response: data }), {
|
|
2245
|
+
status: geminiResponse.status,
|
|
2246
|
+
statusText: geminiResponse.statusText,
|
|
2247
|
+
headers: { "Content-Type": "application/json" }
|
|
2248
|
+
});
|
|
2249
|
+
}
|
|
2250
|
+
};
|
|
2251
|
+
|
|
2252
|
+
// src/transformer/transformers/GeminiTransformer.ts
|
|
2253
|
+
var GeminiTransformer = class {
|
|
2254
|
+
static TransformerName = "gemini";
|
|
2255
|
+
name = "gemini";
|
|
2256
|
+
logger;
|
|
2257
|
+
/**
|
|
2258
|
+
* API endpoint pattern for Gemini
|
|
2259
|
+
* :modelAndAction will be replaced with actual model and action
|
|
2260
|
+
*/
|
|
2261
|
+
endPoint = "/v1beta/models/:modelAndAction";
|
|
2262
|
+
/** Use Bearer token instead of x-goog-api-key (for relay providers) */
|
|
2263
|
+
useBearer;
|
|
2264
|
+
constructor(options) {
|
|
2265
|
+
this.useBearer = options?.UseBearer ?? false;
|
|
2266
|
+
}
|
|
2267
|
+
/**
|
|
2268
|
+
* Handle authentication
|
|
2269
|
+
* - Official Gemini: x-goog-api-key header
|
|
2270
|
+
* - Relay providers: Authorization: Bearer header
|
|
2271
|
+
*/
|
|
2272
|
+
async auth(request, provider, _context) {
|
|
2273
|
+
const headers = {};
|
|
2274
|
+
if (this.useBearer) {
|
|
2275
|
+
headers["authorization"] = `Bearer ${provider.apiKey}`;
|
|
2276
|
+
headers["x-goog-api-key"] = void 0;
|
|
2277
|
+
} else {
|
|
2278
|
+
headers["x-goog-api-key"] = provider.apiKey;
|
|
2279
|
+
headers["authorization"] = void 0;
|
|
2280
|
+
}
|
|
2281
|
+
return {
|
|
2282
|
+
body: request,
|
|
2283
|
+
config: { headers }
|
|
2284
|
+
};
|
|
2285
|
+
}
|
|
2286
|
+
/**
|
|
2287
|
+
* Transform request from unified format to Gemini format
|
|
2288
|
+
* Also builds the correct URL for the Gemini API
|
|
2289
|
+
*/
|
|
2290
|
+
async transformRequestIn(request, provider, _context) {
|
|
2291
|
+
const body = buildRequestBody(request);
|
|
2292
|
+
const action = request.stream ? "streamGenerateContent?alt=sse" : "generateContent";
|
|
2293
|
+
const url = new URL(`./${request.model}:${action}`, provider.baseUrl);
|
|
2294
|
+
const headers = {};
|
|
2295
|
+
if (this.useBearer) {
|
|
2296
|
+
headers["authorization"] = `Bearer ${provider.apiKey}`;
|
|
2297
|
+
headers["x-goog-api-key"] = void 0;
|
|
2298
|
+
} else {
|
|
2299
|
+
headers["x-goog-api-key"] = provider.apiKey;
|
|
2300
|
+
headers["Authorization"] = void 0;
|
|
2301
|
+
}
|
|
2302
|
+
return {
|
|
2303
|
+
body,
|
|
2304
|
+
config: { url, headers }
|
|
2305
|
+
};
|
|
2306
|
+
}
|
|
2307
|
+
/**
|
|
2308
|
+
* Transform incoming request to unified format
|
|
2309
|
+
* (For requests coming into the Gemini endpoint)
|
|
2310
|
+
*/
|
|
2311
|
+
async transformRequestOut(request, _context) {
|
|
2312
|
+
return transformRequestOut(request);
|
|
2313
|
+
}
|
|
2314
|
+
/**
|
|
2315
|
+
* Transform Gemini response to OpenAI-compatible format
|
|
2316
|
+
*/
|
|
2317
|
+
async transformResponseOut(response, _context) {
|
|
2318
|
+
return transformResponseOut(response, this.name, this.logger);
|
|
2319
|
+
}
|
|
2320
|
+
/**
|
|
2321
|
+
* Transform OpenAI-compatible response back to Gemini format
|
|
2322
|
+
* (For endpoint mode — returning Gemini-format responses to the client)
|
|
2323
|
+
*/
|
|
2324
|
+
async transformResponseIn(response, _context) {
|
|
2325
|
+
return transformResponseIn(response, this.logger);
|
|
2326
|
+
}
|
|
2327
|
+
};
|
|
2328
|
+
|
|
2329
|
+
// src/transformer/transformers/OpenAIResponseTransformer.ts
|
|
2330
|
+
var OpenAIResponseTransformer = class {
|
|
2331
|
+
static TransformerName = "openai-response";
|
|
2332
|
+
name = "openai-response";
|
|
2333
|
+
endPoint = "/v1/responses";
|
|
2334
|
+
logger;
|
|
2335
|
+
/**
|
|
2336
|
+
* Handle authentication - Bearer token
|
|
2337
|
+
*/
|
|
2338
|
+
async auth(request, provider, _context) {
|
|
2339
|
+
return {
|
|
2340
|
+
body: request,
|
|
2341
|
+
config: {
|
|
2342
|
+
headers: {
|
|
2343
|
+
Authorization: `Bearer ${provider.apiKey}`,
|
|
2344
|
+
"Content-Type": "application/json"
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
};
|
|
2348
|
+
}
|
|
2349
|
+
/**
|
|
2350
|
+
* Transform unified request → Response API format
|
|
2351
|
+
*/
|
|
2352
|
+
async transformRequestIn(request, provider, _context) {
|
|
2353
|
+
const input = [];
|
|
2354
|
+
for (const msg of request.messages) {
|
|
2355
|
+
if (msg.role === "system") {
|
|
2356
|
+
input.push({
|
|
2357
|
+
role: "developer",
|
|
2358
|
+
content: typeof msg.content === "string" ? msg.content : flattenContent(msg.content)
|
|
2359
|
+
});
|
|
2360
|
+
} else if (msg.role === "tool") {
|
|
2361
|
+
input.push({
|
|
2362
|
+
type: "function_call_output",
|
|
2363
|
+
call_id: msg.tool_call_id,
|
|
2364
|
+
output: typeof msg.content === "string" ? msg.content : ""
|
|
2365
|
+
});
|
|
2366
|
+
} else {
|
|
2367
|
+
const entry = {
|
|
2368
|
+
role: msg.role,
|
|
2369
|
+
content: typeof msg.content === "string" ? msg.content : flattenContent(msg.content)
|
|
2370
|
+
};
|
|
2371
|
+
if (msg.role === "assistant" && msg.tool_calls?.length) {
|
|
2372
|
+
input.push(entry);
|
|
2373
|
+
for (const tc of msg.tool_calls) {
|
|
2374
|
+
input.push({
|
|
2375
|
+
type: "function_call",
|
|
2376
|
+
id: tc.id,
|
|
2377
|
+
call_id: tc.id,
|
|
2378
|
+
name: tc.function.name,
|
|
2379
|
+
arguments: tc.function.arguments
|
|
2380
|
+
});
|
|
2381
|
+
}
|
|
2382
|
+
continue;
|
|
2383
|
+
}
|
|
2384
|
+
input.push(entry);
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
const body = {
|
|
2388
|
+
model: request.model,
|
|
2389
|
+
input,
|
|
2390
|
+
stream: request.stream ?? false,
|
|
2391
|
+
...request.max_tokens ? { max_output_tokens: request.max_tokens } : {},
|
|
2392
|
+
...request.temperature !== void 0 ? { temperature: request.temperature } : {}
|
|
2393
|
+
};
|
|
2394
|
+
if (request.reasoning?.effort && request.reasoning.effort !== "none") {
|
|
2395
|
+
body.reasoning = { effort: request.reasoning.effort, summary: "auto" };
|
|
2396
|
+
}
|
|
2397
|
+
if (request.tools?.length) {
|
|
2398
|
+
body.tools = request.tools.map((tool) => ({
|
|
2399
|
+
type: "function",
|
|
2400
|
+
name: tool.function.name,
|
|
2401
|
+
description: tool.function.description,
|
|
2402
|
+
parameters: tool.function.parameters
|
|
2403
|
+
}));
|
|
2404
|
+
}
|
|
2405
|
+
if (request.tool_choice) {
|
|
2406
|
+
if (typeof request.tool_choice === "string") {
|
|
2407
|
+
body.tool_choice = request.tool_choice;
|
|
2408
|
+
} else if (typeof request.tool_choice === "object" && "function" in request.tool_choice) {
|
|
2409
|
+
body.tool_choice = { type: "function", name: request.tool_choice.function.name };
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
const url = new URL("/v1/responses", provider.baseUrl);
|
|
2413
|
+
return { body, config: { url } };
|
|
2414
|
+
}
|
|
2415
|
+
/**
|
|
2416
|
+
* Transform Response API request → unified format
|
|
2417
|
+
*/
|
|
2418
|
+
async transformRequestOut(request, _context) {
|
|
2419
|
+
const req = request;
|
|
2420
|
+
const messages = [];
|
|
2421
|
+
if (req.input) {
|
|
2422
|
+
for (const item of req.input) {
|
|
2423
|
+
const entry = item;
|
|
2424
|
+
if (entry.type === "function_call_output") {
|
|
2425
|
+
messages.push({
|
|
2426
|
+
role: "tool",
|
|
2427
|
+
content: entry.output || "",
|
|
2428
|
+
tool_call_id: entry.call_id || void 0
|
|
2429
|
+
});
|
|
2430
|
+
continue;
|
|
2431
|
+
}
|
|
2432
|
+
if (entry.type === "function_call") {
|
|
2433
|
+
const toolCall = {
|
|
2434
|
+
id: (entry.call_id ?? entry.id) || "",
|
|
2435
|
+
type: "function",
|
|
2436
|
+
function: {
|
|
2437
|
+
name: entry.name || "",
|
|
2438
|
+
arguments: typeof entry.arguments === "string" ? entry.arguments : ""
|
|
2439
|
+
}
|
|
2440
|
+
};
|
|
2441
|
+
const last = messages[messages.length - 1];
|
|
2442
|
+
if (last && last.role === "assistant") {
|
|
2443
|
+
(last.tool_calls ??= []).push(toolCall);
|
|
2444
|
+
} else {
|
|
2445
|
+
messages.push({ role: "assistant", content: null, tool_calls: [toolCall] });
|
|
2446
|
+
}
|
|
2447
|
+
continue;
|
|
2448
|
+
}
|
|
2449
|
+
const role = entry.role;
|
|
2450
|
+
if (role === "developer") {
|
|
2451
|
+
messages.push({
|
|
2452
|
+
role: "system",
|
|
2453
|
+
content: typeof entry.content === "string" ? entry.content : JSON.stringify(entry.content)
|
|
2454
|
+
});
|
|
2455
|
+
} else if (role === "user" || role === "assistant") {
|
|
2456
|
+
messages.push({
|
|
2457
|
+
role,
|
|
2458
|
+
content: typeof entry.content === "string" ? entry.content : JSON.stringify(entry.content)
|
|
2459
|
+
});
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
const result = {
|
|
2464
|
+
messages,
|
|
2465
|
+
model: req.model,
|
|
2466
|
+
max_tokens: req.max_output_tokens,
|
|
2467
|
+
temperature: req.temperature,
|
|
2468
|
+
stream: req.stream
|
|
2469
|
+
};
|
|
2470
|
+
if (req.reasoning?.effort) {
|
|
2471
|
+
result.reasoning = {
|
|
2472
|
+
effort: req.reasoning.effort,
|
|
2473
|
+
enabled: true
|
|
2474
|
+
};
|
|
2475
|
+
}
|
|
2476
|
+
if (req.tools?.length) {
|
|
2477
|
+
result.tools = req.tools.filter((t) => t.type === "function").map((t) => ({
|
|
2478
|
+
type: "function",
|
|
2479
|
+
function: {
|
|
2480
|
+
name: t.name || "",
|
|
2481
|
+
description: t.description || "",
|
|
2482
|
+
parameters: t.parameters || {}
|
|
2483
|
+
}
|
|
2484
|
+
}));
|
|
2485
|
+
}
|
|
2486
|
+
return result;
|
|
2487
|
+
}
|
|
2488
|
+
/**
|
|
2489
|
+
* Transform Response API response → unified (OpenAI CC) format
|
|
2490
|
+
*/
|
|
2491
|
+
async transformResponseOut(response, _context) {
|
|
2492
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
2493
|
+
if (contentType.includes("text/event-stream")) {
|
|
2494
|
+
if (!response.body) {
|
|
2495
|
+
throw new Error("Stream response body is null");
|
|
2496
|
+
}
|
|
2497
|
+
return new Response(convertResponseApiStreamToOpenAI(response.body), {
|
|
2498
|
+
headers: {
|
|
2499
|
+
"Content-Type": "text/event-stream",
|
|
2500
|
+
"Cache-Control": "no-cache",
|
|
2501
|
+
Connection: "keep-alive"
|
|
2502
|
+
}
|
|
2503
|
+
});
|
|
2504
|
+
}
|
|
2505
|
+
const data = await response.json();
|
|
2506
|
+
return new Response(JSON.stringify(convertResponseApiJsonToOpenAI(data)), {
|
|
2507
|
+
headers: { "Content-Type": "application/json" }
|
|
2508
|
+
});
|
|
2509
|
+
}
|
|
2510
|
+
/**
|
|
2511
|
+
* Transform OpenAI CC response → Response API format
|
|
2512
|
+
*/
|
|
2513
|
+
async transformResponseIn(response, _context) {
|
|
2514
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
2515
|
+
if (contentType.includes("text/event-stream")) {
|
|
2516
|
+
if (!response.body) {
|
|
2517
|
+
throw new Error("Stream response body is null");
|
|
2518
|
+
}
|
|
2519
|
+
return new Response(convertOpenAIStreamToResponseApi(response.body), {
|
|
2520
|
+
headers: {
|
|
2521
|
+
"Content-Type": "text/event-stream",
|
|
2522
|
+
"Cache-Control": "no-cache",
|
|
2523
|
+
Connection: "keep-alive"
|
|
2524
|
+
}
|
|
2525
|
+
});
|
|
2526
|
+
}
|
|
2527
|
+
const data = await response.json();
|
|
2528
|
+
return new Response(JSON.stringify(convertOpenAIJsonToResponseApi(data)), {
|
|
2529
|
+
headers: { "Content-Type": "application/json" }
|
|
2530
|
+
});
|
|
2531
|
+
}
|
|
2532
|
+
};
|
|
2533
|
+
function flattenContent(content) {
|
|
2534
|
+
if (typeof content === "string") return content;
|
|
2535
|
+
if (!content) return "";
|
|
2536
|
+
if (Array.isArray(content)) {
|
|
2537
|
+
return content.filter((c) => c.type === "text").map((c) => c.text || "").join("\n");
|
|
2538
|
+
}
|
|
2539
|
+
return "";
|
|
2540
|
+
}
|
|
2541
|
+
function convertResponseApiJsonToOpenAI(data) {
|
|
2542
|
+
let textContent = "";
|
|
2543
|
+
const toolCalls = [];
|
|
2544
|
+
const output = data.output;
|
|
2545
|
+
if (output) {
|
|
2546
|
+
for (const item of output) {
|
|
2547
|
+
if (item.type === "message") {
|
|
2548
|
+
const content = item.content;
|
|
2549
|
+
if (content) {
|
|
2550
|
+
for (const part of content) {
|
|
2551
|
+
if (part.type === "output_text" && typeof part.text === "string") {
|
|
2552
|
+
textContent += part.text;
|
|
2553
|
+
}
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
} else if (item.type === "function_call") {
|
|
2557
|
+
toolCalls.push({
|
|
2558
|
+
id: item.call_id || item.id || `call_${Date.now()}`,
|
|
2559
|
+
type: "function",
|
|
2560
|
+
function: {
|
|
2561
|
+
name: item.name,
|
|
2562
|
+
arguments: typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments || {})
|
|
2563
|
+
}
|
|
2564
|
+
});
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
const usage = data.usage;
|
|
2569
|
+
const message = {
|
|
2570
|
+
role: "assistant",
|
|
2571
|
+
content: textContent || null
|
|
2572
|
+
};
|
|
2573
|
+
if (toolCalls.length > 0) {
|
|
2574
|
+
message.tool_calls = toolCalls;
|
|
2575
|
+
}
|
|
2576
|
+
return {
|
|
2577
|
+
id: data.id || `chatcmpl-${Date.now()}`,
|
|
2578
|
+
object: "chat.completion",
|
|
2579
|
+
created: Math.floor(Date.now() / 1e3),
|
|
2580
|
+
model: data.model || "unknown",
|
|
2581
|
+
choices: [
|
|
2582
|
+
{
|
|
2583
|
+
index: 0,
|
|
2584
|
+
message,
|
|
2585
|
+
finish_reason: toolCalls.length > 0 ? "tool_calls" : "stop"
|
|
2586
|
+
}
|
|
2587
|
+
],
|
|
2588
|
+
usage: usage ? {
|
|
2589
|
+
prompt_tokens: usage.input_tokens || 0,
|
|
2590
|
+
completion_tokens: usage.output_tokens || 0,
|
|
2591
|
+
total_tokens: (usage.input_tokens || 0) + (usage.output_tokens || 0)
|
|
2592
|
+
} : void 0
|
|
2593
|
+
};
|
|
2594
|
+
}
|
|
2595
|
+
function convertOpenAIJsonToResponseApi(data) {
|
|
2596
|
+
const choices = data.choices;
|
|
2597
|
+
const message = choices?.[0]?.message;
|
|
2598
|
+
const output = [];
|
|
2599
|
+
if (message) {
|
|
2600
|
+
const contentParts = [];
|
|
2601
|
+
if (message.content) {
|
|
2602
|
+
contentParts.push({ type: "output_text", text: message.content });
|
|
2603
|
+
}
|
|
2604
|
+
if (contentParts.length > 0) {
|
|
2605
|
+
output.push({ type: "message", role: "assistant", content: contentParts });
|
|
2606
|
+
}
|
|
2607
|
+
const toolCalls = message.tool_calls;
|
|
2608
|
+
if (toolCalls?.length) {
|
|
2609
|
+
for (const tc of toolCalls) {
|
|
2610
|
+
const func = tc.function;
|
|
2611
|
+
output.push({
|
|
2612
|
+
type: "function_call",
|
|
2613
|
+
id: tc.id,
|
|
2614
|
+
call_id: tc.id,
|
|
2615
|
+
name: func?.name,
|
|
2616
|
+
arguments: func?.arguments
|
|
2617
|
+
});
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
const usage = data.usage;
|
|
2622
|
+
return {
|
|
2623
|
+
id: data.id || `resp_${Date.now()}`,
|
|
2624
|
+
object: "response",
|
|
2625
|
+
status: "completed",
|
|
2626
|
+
model: data.model || "unknown",
|
|
2627
|
+
output,
|
|
2628
|
+
usage: usage ? {
|
|
2629
|
+
input_tokens: usage.prompt_tokens || 0,
|
|
2630
|
+
output_tokens: usage.completion_tokens || 0,
|
|
2631
|
+
total_tokens: usage.total_tokens || (usage.prompt_tokens || 0) + (usage.completion_tokens || 0)
|
|
2632
|
+
} : void 0
|
|
2633
|
+
};
|
|
2634
|
+
}
|
|
2635
|
+
function convertResponseApiStreamToOpenAI(responseApiStream) {
|
|
2636
|
+
const decoder = new TextDecoder();
|
|
2637
|
+
const encoder = new TextEncoder();
|
|
2638
|
+
return new ReadableStream({
|
|
2639
|
+
start: async (controller) => {
|
|
2640
|
+
const reader = responseApiStream.getReader();
|
|
2641
|
+
let buffer = "";
|
|
2642
|
+
let isClosed = false;
|
|
2643
|
+
const messageId = `chatcmpl-${Date.now()}`;
|
|
2644
|
+
let model = "unknown";
|
|
2645
|
+
let hasEmittedRole = false;
|
|
2646
|
+
const safeEnqueue = (str) => {
|
|
2647
|
+
if (!isClosed) {
|
|
2648
|
+
try {
|
|
2649
|
+
controller.enqueue(encoder.encode(str));
|
|
2650
|
+
} catch {
|
|
2651
|
+
isClosed = true;
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
};
|
|
2655
|
+
const emitChunk = (choices, usage) => {
|
|
2656
|
+
const chunk = {
|
|
2657
|
+
id: messageId,
|
|
2658
|
+
object: "chat.completion.chunk",
|
|
2659
|
+
created: Math.floor(Date.now() / 1e3),
|
|
2660
|
+
model,
|
|
2661
|
+
choices
|
|
2662
|
+
};
|
|
2663
|
+
if (usage) chunk.usage = usage;
|
|
2664
|
+
safeEnqueue(`data: ${JSON.stringify(chunk)}
|
|
2665
|
+
|
|
2666
|
+
`);
|
|
2667
|
+
};
|
|
2668
|
+
try {
|
|
2669
|
+
while (true) {
|
|
2670
|
+
const { done, value } = await reader.read();
|
|
2671
|
+
if (done) break;
|
|
2672
|
+
buffer += decoder.decode(value, { stream: true });
|
|
2673
|
+
const lines = buffer.split("\n");
|
|
2674
|
+
buffer = lines.pop() || "";
|
|
2675
|
+
for (const line of lines) {
|
|
2676
|
+
if (isClosed) break;
|
|
2677
|
+
if (!line.startsWith("data:")) continue;
|
|
2678
|
+
const data = line.slice(5).trim();
|
|
2679
|
+
if (data === "[DONE]") continue;
|
|
2680
|
+
try {
|
|
2681
|
+
const event = JSON.parse(data);
|
|
2682
|
+
model = event.model || event.response?.model || model;
|
|
2683
|
+
switch (event.type) {
|
|
2684
|
+
case "response.output_text.delta":
|
|
2685
|
+
if (event.delta) {
|
|
2686
|
+
if (!hasEmittedRole) {
|
|
2687
|
+
emitChunk([{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }]);
|
|
2688
|
+
hasEmittedRole = true;
|
|
2689
|
+
}
|
|
2690
|
+
emitChunk([{ index: 0, delta: { content: event.delta }, finish_reason: null }]);
|
|
2691
|
+
}
|
|
2692
|
+
break;
|
|
2693
|
+
case "response.reasoning_summary_text.delta":
|
|
2694
|
+
if (event.delta) {
|
|
2695
|
+
emitChunk([{
|
|
2696
|
+
index: 0,
|
|
2697
|
+
delta: { thinking: { content: event.delta } },
|
|
2698
|
+
finish_reason: null
|
|
2699
|
+
}]);
|
|
2700
|
+
}
|
|
2701
|
+
break;
|
|
2702
|
+
case "response.completed": {
|
|
2703
|
+
const resp = event.response;
|
|
2704
|
+
const respUsage = resp?.usage;
|
|
2705
|
+
const usage = respUsage ? {
|
|
2706
|
+
prompt_tokens: respUsage.input_tokens || 0,
|
|
2707
|
+
completion_tokens: respUsage.output_tokens || 0,
|
|
2708
|
+
total_tokens: (respUsage.input_tokens || 0) + (respUsage.output_tokens || 0)
|
|
2709
|
+
} : void 0;
|
|
2710
|
+
emitChunk([{ index: 0, delta: {}, finish_reason: "stop" }], usage);
|
|
2711
|
+
safeEnqueue("data: [DONE]\n\n");
|
|
2712
|
+
break;
|
|
2713
|
+
}
|
|
2714
|
+
case "error":
|
|
2715
|
+
emitChunk([{
|
|
2716
|
+
index: 0,
|
|
2717
|
+
delta: { content: `[Error: ${event.error?.message || "Unknown error"}]` },
|
|
2718
|
+
finish_reason: "stop"
|
|
2719
|
+
}]);
|
|
2720
|
+
break;
|
|
2721
|
+
default:
|
|
2722
|
+
break;
|
|
2723
|
+
}
|
|
2724
|
+
} catch {
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
} catch (e) {
|
|
2729
|
+
if (!isClosed) controller.error(e);
|
|
2730
|
+
} finally {
|
|
2731
|
+
if (!isClosed) {
|
|
2732
|
+
try {
|
|
2733
|
+
controller.close();
|
|
2734
|
+
} catch {
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
reader.releaseLock();
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
});
|
|
2741
|
+
}
|
|
2742
|
+
function convertOpenAIStreamToResponseApi(openaiStream) {
|
|
2743
|
+
const decoder = new TextDecoder();
|
|
2744
|
+
const encoder = new TextEncoder();
|
|
2745
|
+
return new ReadableStream({
|
|
2746
|
+
start: async (controller) => {
|
|
2747
|
+
const reader = openaiStream.getReader();
|
|
2748
|
+
let buffer = "";
|
|
2749
|
+
let isClosed = false;
|
|
2750
|
+
let accumulatedContent = "";
|
|
2751
|
+
let model = "unknown";
|
|
2752
|
+
const responseId = `resp_${Date.now()}`;
|
|
2753
|
+
const safeEnqueue = (str) => {
|
|
2754
|
+
if (!isClosed) {
|
|
2755
|
+
try {
|
|
2756
|
+
controller.enqueue(encoder.encode(str));
|
|
2757
|
+
} catch {
|
|
2758
|
+
isClosed = true;
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
};
|
|
2762
|
+
const emitEvent = (event) => {
|
|
2763
|
+
safeEnqueue(`data: ${JSON.stringify(event)}
|
|
2764
|
+
|
|
2765
|
+
`);
|
|
2766
|
+
};
|
|
2767
|
+
emitEvent({
|
|
2768
|
+
type: "response.created",
|
|
2769
|
+
response: { id: responseId, status: "in_progress" }
|
|
2770
|
+
});
|
|
2771
|
+
try {
|
|
2772
|
+
while (true) {
|
|
2773
|
+
const { done, value } = await reader.read();
|
|
2774
|
+
if (done) break;
|
|
2775
|
+
buffer += decoder.decode(value, { stream: true });
|
|
2776
|
+
const lines = buffer.split("\n");
|
|
2777
|
+
buffer = lines.pop() || "";
|
|
2778
|
+
for (const line of lines) {
|
|
2779
|
+
if (isClosed) break;
|
|
2780
|
+
if (!line.startsWith("data:")) continue;
|
|
2781
|
+
const data = line.slice(5).trim();
|
|
2782
|
+
if (data === "[DONE]") continue;
|
|
2783
|
+
try {
|
|
2784
|
+
const chunk = JSON.parse(data);
|
|
2785
|
+
const choice = chunk.choices?.[0];
|
|
2786
|
+
model = chunk.model || model;
|
|
2787
|
+
if (!choice) continue;
|
|
2788
|
+
if (choice.delta?.content) {
|
|
2789
|
+
accumulatedContent += choice.delta.content;
|
|
2790
|
+
emitEvent({ type: "response.output_text.delta", delta: choice.delta.content });
|
|
2791
|
+
}
|
|
2792
|
+
if (choice.delta?.thinking?.content) {
|
|
2793
|
+
emitEvent({
|
|
2794
|
+
type: "response.reasoning_summary_text.delta",
|
|
2795
|
+
delta: choice.delta.thinking.content
|
|
2796
|
+
});
|
|
2797
|
+
}
|
|
2798
|
+
if (choice.finish_reason) {
|
|
2799
|
+
emitEvent({ type: "response.output_text.done", text: accumulatedContent });
|
|
2800
|
+
emitEvent({
|
|
2801
|
+
type: "response.completed",
|
|
2802
|
+
response: {
|
|
2803
|
+
id: responseId,
|
|
2804
|
+
status: "completed",
|
|
2805
|
+
model,
|
|
2806
|
+
output: [
|
|
2807
|
+
{
|
|
2808
|
+
type: "message",
|
|
2809
|
+
role: "assistant",
|
|
2810
|
+
content: [{ type: "output_text", text: accumulatedContent }]
|
|
2811
|
+
}
|
|
2812
|
+
],
|
|
2813
|
+
usage: chunk.usage ? {
|
|
2814
|
+
input_tokens: chunk.usage.prompt_tokens || 0,
|
|
2815
|
+
output_tokens: chunk.usage.completion_tokens || 0,
|
|
2816
|
+
total_tokens: chunk.usage.total_tokens || 0
|
|
2817
|
+
} : void 0
|
|
2818
|
+
}
|
|
2819
|
+
});
|
|
2820
|
+
}
|
|
2821
|
+
} catch {
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
} catch (e) {
|
|
2826
|
+
if (!isClosed) controller.error(e);
|
|
2827
|
+
} finally {
|
|
2828
|
+
if (!isClosed) {
|
|
2829
|
+
try {
|
|
2830
|
+
controller.close();
|
|
2831
|
+
} catch {
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
reader.releaseLock();
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
});
|
|
2838
|
+
}
|
|
2839
|
+
|
|
2840
|
+
// src/transformer/transformers/OpenCodeGoTransformer.ts
|
|
2841
|
+
var OpenCodeGoTransformer = class {
|
|
2842
|
+
static TransformerName = "opencodego";
|
|
2843
|
+
name = "opencodego";
|
|
2844
|
+
endPoint = "/v1/chat/completions";
|
|
2845
|
+
logger;
|
|
2846
|
+
async auth(request, _provider, _context) {
|
|
2847
|
+
return { body: request, config: { headers: {} } };
|
|
2848
|
+
}
|
|
2849
|
+
/**
|
|
2850
|
+
* Unified → OpenAI Chat Completions.
|
|
2851
|
+
* Unified IS chat completions shape; this is mostly stripping `meta` and
|
|
2852
|
+
* normalizing string-content single-block messages.
|
|
2853
|
+
*/
|
|
2854
|
+
async transformRequestIn(request, _provider, _context) {
|
|
2855
|
+
const messages = request.messages.map((m) => {
|
|
2856
|
+
const content = m.content;
|
|
2857
|
+
if (Array.isArray(content) && content.length === 1 && content[0]?.type === "text") {
|
|
2858
|
+
return { ...m, content: content[0].text };
|
|
2859
|
+
}
|
|
2860
|
+
return m;
|
|
2861
|
+
});
|
|
2862
|
+
const out = {
|
|
2863
|
+
model: request.model,
|
|
2864
|
+
messages,
|
|
2865
|
+
stream: request.stream
|
|
2866
|
+
};
|
|
2867
|
+
if (request.temperature !== void 0) out.temperature = request.temperature;
|
|
2868
|
+
if (request.max_tokens !== void 0) out.max_tokens = request.max_tokens;
|
|
2869
|
+
if (request.tools && request.tools.length > 0) out.tools = request.tools;
|
|
2870
|
+
if (request.tool_choice !== void 0) out.tool_choice = request.tool_choice;
|
|
2871
|
+
if (request.reasoning?.effort && request.reasoning.effort !== "none") {
|
|
2872
|
+
out.reasoning_effort = request.reasoning.effort;
|
|
2873
|
+
}
|
|
2874
|
+
return out;
|
|
2875
|
+
}
|
|
2876
|
+
/**
|
|
2877
|
+
* OpenAI Chat Completions response → Unified.
|
|
2878
|
+
* The upstream's response shape already matches Unified — pass through.
|
|
2879
|
+
* The endpoint AnthropicTransformer re-encodes to Anthropic for the SDK.
|
|
2880
|
+
*/
|
|
2881
|
+
async transformResponseOut(response, _context) {
|
|
2882
|
+
return response;
|
|
2883
|
+
}
|
|
2884
|
+
};
|
|
2885
|
+
|
|
2886
|
+
// src/transformer/transformers/ReasoningTransformer.ts
|
|
2887
|
+
import {
|
|
2888
|
+
buildAnthropicThinking,
|
|
2889
|
+
buildQwenThinkingConfig,
|
|
2890
|
+
calculateThinkingBudget,
|
|
2891
|
+
getOpenAIReasoningEffort
|
|
2892
|
+
} from "@omnicross/contracts/thinking-config";
|
|
2893
|
+
var ReasoningTransformer = class {
|
|
2894
|
+
static TransformerName = "reasoning";
|
|
2895
|
+
name = "reasoning";
|
|
2896
|
+
logger;
|
|
2897
|
+
enabled;
|
|
2898
|
+
constructor(options) {
|
|
2899
|
+
this.enabled = options?.enable !== false;
|
|
2900
|
+
}
|
|
2901
|
+
/**
|
|
2902
|
+
* Transform request: convert reasoning config to thinking parameters
|
|
2903
|
+
* Uses model-specific budget calculation based on effort level
|
|
2904
|
+
*/
|
|
2905
|
+
async transformRequestIn(request, provider, _context) {
|
|
2906
|
+
const extendedRequest = request;
|
|
2907
|
+
const modelId = request.model;
|
|
2908
|
+
if (!this.enabled) {
|
|
2909
|
+
extendedRequest.thinking = {
|
|
2910
|
+
type: "disabled",
|
|
2911
|
+
budget_tokens: -1
|
|
2912
|
+
};
|
|
2913
|
+
extendedRequest.enable_thinking = false;
|
|
2914
|
+
return extendedRequest;
|
|
2915
|
+
}
|
|
2916
|
+
if (request.reasoning) {
|
|
2917
|
+
const effortLevel = request.reasoning.effort || "none";
|
|
2918
|
+
const userMaxTokens = request.reasoning.max_tokens || request.max_tokens;
|
|
2919
|
+
const calculatedBudget = calculateThinkingBudget(modelId, effortLevel, userMaxTokens);
|
|
2920
|
+
const providerName = provider.name?.toLowerCase() || "";
|
|
2921
|
+
if (providerName === "anthropic" || providerName.includes("claude")) {
|
|
2922
|
+
const thinkingConfig = buildAnthropicThinking(modelId, effortLevel, userMaxTokens);
|
|
2923
|
+
if (thinkingConfig) {
|
|
2924
|
+
extendedRequest.thinking = thinkingConfig;
|
|
2925
|
+
} else {
|
|
2926
|
+
extendedRequest.thinking = {
|
|
2927
|
+
type: "disabled"
|
|
2928
|
+
};
|
|
2929
|
+
}
|
|
2930
|
+
} else if (providerName === "openai" || modelId.match(/^o[134]/i)) {
|
|
2931
|
+
const reasoningEffort = getOpenAIReasoningEffort(effortLevel);
|
|
2932
|
+
if (reasoningEffort) {
|
|
2933
|
+
extendedRequest.reasoning_effort = reasoningEffort;
|
|
2934
|
+
}
|
|
2935
|
+
extendedRequest.thinking = {
|
|
2936
|
+
type: effortLevel === "none" ? "disabled" : "enabled",
|
|
2937
|
+
budget_tokens: calculatedBudget
|
|
2938
|
+
};
|
|
2939
|
+
} else if (providerName === "deepseek" && modelId?.startsWith("deepseek-v4")) {
|
|
2940
|
+
extendedRequest.thinking = {
|
|
2941
|
+
type: effortLevel === "none" ? "disabled" : "enabled",
|
|
2942
|
+
budget_tokens: calculatedBudget
|
|
2943
|
+
};
|
|
2944
|
+
if (effortLevel !== "none") {
|
|
2945
|
+
extendedRequest.reasoning_effort = effortLevel === "high" ? "max" : "high";
|
|
2946
|
+
}
|
|
2947
|
+
} else if (providerName === "deepseek" || providerName === "qwen" || providerName === "alibaba") {
|
|
2948
|
+
const qwenConfig = buildQwenThinkingConfig(effortLevel, userMaxTokens);
|
|
2949
|
+
extendedRequest.enable_thinking = qwenConfig.enable_thinking;
|
|
2950
|
+
if (qwenConfig.thinking_budget) {
|
|
2951
|
+
extendedRequest.thinking_budget = qwenConfig.thinking_budget;
|
|
2952
|
+
}
|
|
2953
|
+
extendedRequest.thinking = {
|
|
2954
|
+
type: qwenConfig.enable_thinking ? "enabled" : "disabled",
|
|
2955
|
+
budget_tokens: calculatedBudget
|
|
2956
|
+
};
|
|
2957
|
+
} else {
|
|
2958
|
+
extendedRequest.thinking = {
|
|
2959
|
+
type: effortLevel === "none" ? "disabled" : "enabled",
|
|
2960
|
+
budget_tokens: calculatedBudget
|
|
2961
|
+
};
|
|
2962
|
+
extendedRequest.enable_thinking = effortLevel !== "none";
|
|
2963
|
+
}
|
|
2964
|
+
this.logger?.debug(`[ReasoningTransformer] Model: ${modelId}, Effort: ${effortLevel}, Budget: ${calculatedBudget}`);
|
|
2965
|
+
}
|
|
2966
|
+
return extendedRequest;
|
|
2967
|
+
}
|
|
2968
|
+
/**
|
|
2969
|
+
* Transform response: convert reasoning_content to thinking blocks
|
|
2970
|
+
*/
|
|
2971
|
+
async transformResponseOut(response, _context) {
|
|
2972
|
+
if (!this.enabled) return response;
|
|
2973
|
+
const contentType = response.headers.get("Content-Type") ?? "";
|
|
2974
|
+
if (contentType.includes("application/json")) {
|
|
2975
|
+
return this.handleJsonResponse(response);
|
|
2976
|
+
} else if (contentType.includes("stream") || contentType.includes("text/event-stream")) {
|
|
2977
|
+
return this.handleStreamResponse(response);
|
|
2978
|
+
}
|
|
2979
|
+
return response;
|
|
2980
|
+
}
|
|
2981
|
+
/**
|
|
2982
|
+
* Handle JSON (non-streaming) response
|
|
2983
|
+
*/
|
|
2984
|
+
async handleJsonResponse(response) {
|
|
2985
|
+
const jsonResponse = await response.json();
|
|
2986
|
+
if (jsonResponse.choices?.[0]?.message?.reasoning_content) {
|
|
2987
|
+
jsonResponse.thinking = {
|
|
2988
|
+
content: jsonResponse.choices[0].message.reasoning_content
|
|
2989
|
+
};
|
|
2990
|
+
delete jsonResponse.choices[0].message.reasoning_content;
|
|
2991
|
+
}
|
|
2992
|
+
return new Response(JSON.stringify(jsonResponse), {
|
|
2993
|
+
status: response.status,
|
|
2994
|
+
statusText: response.statusText,
|
|
2995
|
+
headers: response.headers
|
|
2996
|
+
});
|
|
2997
|
+
}
|
|
2998
|
+
/**
|
|
2999
|
+
* Handle streaming response - convert reasoning_content to thinking blocks
|
|
3000
|
+
*/
|
|
3001
|
+
handleStreamResponse(response) {
|
|
3002
|
+
if (!response.body) {
|
|
3003
|
+
return response;
|
|
3004
|
+
}
|
|
3005
|
+
const decoder = new TextDecoder();
|
|
3006
|
+
const encoder = new TextEncoder();
|
|
3007
|
+
let reasoningContent = "";
|
|
3008
|
+
let isReasoningComplete = false;
|
|
3009
|
+
let buffer = "";
|
|
3010
|
+
const stream = new ReadableStream({
|
|
3011
|
+
start: async (controller) => {
|
|
3012
|
+
const reader = response.body.getReader();
|
|
3013
|
+
const processLine = (line) => {
|
|
3014
|
+
this.logger?.debug("Processing reasoning line:", line);
|
|
3015
|
+
if (line.startsWith("data: ") && line.trim() !== "data: [DONE]") {
|
|
3016
|
+
try {
|
|
3017
|
+
const data = JSON.parse(line.slice(6));
|
|
3018
|
+
if (data.choices?.[0]?.delta?.reasoning_content) {
|
|
3019
|
+
reasoningContent += data.choices[0].delta.reasoning_content;
|
|
3020
|
+
const thinkingChunk = {
|
|
3021
|
+
...data,
|
|
3022
|
+
choices: [
|
|
3023
|
+
{
|
|
3024
|
+
...data.choices[0],
|
|
3025
|
+
delta: {
|
|
3026
|
+
...data.choices[0].delta,
|
|
3027
|
+
thinking: {
|
|
3028
|
+
content: data.choices[0].delta.reasoning_content
|
|
3029
|
+
}
|
|
3030
|
+
}
|
|
3031
|
+
}
|
|
3032
|
+
]
|
|
3033
|
+
};
|
|
3034
|
+
delete thinkingChunk.choices[0].delta.reasoning_content;
|
|
3035
|
+
const thinkingLine = `data: ${JSON.stringify(thinkingChunk)}
|
|
3036
|
+
|
|
3037
|
+
`;
|
|
3038
|
+
controller.enqueue(encoder.encode(thinkingLine));
|
|
3039
|
+
return;
|
|
3040
|
+
}
|
|
3041
|
+
if ((data.choices?.[0]?.delta?.content || data.choices?.[0]?.delta?.tool_calls) && reasoningContent && !isReasoningComplete) {
|
|
3042
|
+
isReasoningComplete = true;
|
|
3043
|
+
const signature = Date.now().toString();
|
|
3044
|
+
const thinkingChunk = {
|
|
3045
|
+
...data,
|
|
3046
|
+
choices: [
|
|
3047
|
+
{
|
|
3048
|
+
...data.choices[0],
|
|
3049
|
+
delta: {
|
|
3050
|
+
...data.choices[0].delta,
|
|
3051
|
+
content: null,
|
|
3052
|
+
thinking: {
|
|
3053
|
+
signature
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
]
|
|
3058
|
+
};
|
|
3059
|
+
delete thinkingChunk.choices[0].delta.reasoning_content;
|
|
3060
|
+
const thinkingLine = `data: ${JSON.stringify(thinkingChunk)}
|
|
3061
|
+
|
|
3062
|
+
`;
|
|
3063
|
+
controller.enqueue(encoder.encode(thinkingLine));
|
|
3064
|
+
}
|
|
3065
|
+
if (data.choices?.[0]?.delta?.reasoning_content) {
|
|
3066
|
+
delete data.choices[0].delta.reasoning_content;
|
|
3067
|
+
}
|
|
3068
|
+
if (data.choices?.[0]?.delta && Object.keys(data.choices[0].delta).length > 0) {
|
|
3069
|
+
if (isReasoningComplete) {
|
|
3070
|
+
data.choices[0].index++;
|
|
3071
|
+
}
|
|
3072
|
+
const modifiedLine = `data: ${JSON.stringify(data)}
|
|
3073
|
+
|
|
3074
|
+
`;
|
|
3075
|
+
controller.enqueue(encoder.encode(modifiedLine));
|
|
3076
|
+
}
|
|
3077
|
+
} catch (_e) {
|
|
3078
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
3079
|
+
}
|
|
3080
|
+
} else {
|
|
3081
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
3082
|
+
}
|
|
3083
|
+
};
|
|
3084
|
+
try {
|
|
3085
|
+
while (true) {
|
|
3086
|
+
const { done, value } = await reader.read();
|
|
3087
|
+
if (done) {
|
|
3088
|
+
if (buffer.trim()) {
|
|
3089
|
+
const lines2 = buffer.split("\n");
|
|
3090
|
+
for (const line of lines2) {
|
|
3091
|
+
if (line.trim()) {
|
|
3092
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
3093
|
+
}
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
3096
|
+
break;
|
|
3097
|
+
}
|
|
3098
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
3099
|
+
buffer += chunk;
|
|
3100
|
+
const lines = buffer.split("\n");
|
|
3101
|
+
buffer = lines.pop() || "";
|
|
3102
|
+
for (const line of lines) {
|
|
3103
|
+
if (!line.trim()) continue;
|
|
3104
|
+
try {
|
|
3105
|
+
processLine(line);
|
|
3106
|
+
} catch (error) {
|
|
3107
|
+
this.logger?.error("Error processing reasoning stream line:", error);
|
|
3108
|
+
controller.enqueue(encoder.encode(line + "\n"));
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
}
|
|
3112
|
+
} catch (error) {
|
|
3113
|
+
this.logger?.error("Reasoning stream error:", error);
|
|
3114
|
+
controller.error(error);
|
|
3115
|
+
} finally {
|
|
3116
|
+
try {
|
|
3117
|
+
reader.releaseLock();
|
|
3118
|
+
} catch (e) {
|
|
3119
|
+
this.logger?.error("Error releasing reader lock:", e);
|
|
3120
|
+
}
|
|
3121
|
+
controller.close();
|
|
3122
|
+
}
|
|
3123
|
+
}
|
|
3124
|
+
});
|
|
3125
|
+
return new Response(stream, {
|
|
3126
|
+
status: response.status,
|
|
3127
|
+
statusText: response.statusText,
|
|
3128
|
+
headers: new Headers({
|
|
3129
|
+
"Content-Type": "text/event-stream",
|
|
3130
|
+
"Cache-Control": "no-cache",
|
|
3131
|
+
Connection: "keep-alive"
|
|
3132
|
+
})
|
|
3133
|
+
});
|
|
3134
|
+
}
|
|
3135
|
+
};
|
|
3136
|
+
|
|
3137
|
+
// src/transformer/transformers/index.ts
|
|
3138
|
+
var BuiltinTransformers = {
|
|
3139
|
+
DeepseekTransformer,
|
|
3140
|
+
ReasoningTransformer,
|
|
3141
|
+
GeminiTransformer,
|
|
3142
|
+
GeminiCodeAssistTransformer,
|
|
3143
|
+
AnthropicTransformer,
|
|
3144
|
+
OpenAIResponseTransformer,
|
|
3145
|
+
OpenCodeGoTransformer
|
|
3146
|
+
};
|
|
3147
|
+
function getBuiltinTransformers() {
|
|
3148
|
+
return BuiltinTransformers;
|
|
3149
|
+
}
|
|
3150
|
+
var BUILTIN_TRANSFORMER_NAMES = [
|
|
3151
|
+
"deepseek",
|
|
3152
|
+
"reasoning",
|
|
3153
|
+
"gemini",
|
|
3154
|
+
"gemini-code-assist",
|
|
3155
|
+
"anthropic",
|
|
3156
|
+
"openai-response",
|
|
3157
|
+
"opencodego"
|
|
3158
|
+
];
|
|
3159
|
+
async function registerBuiltinTransformers(service) {
|
|
3160
|
+
await service.initialize(getBuiltinTransformers());
|
|
3161
|
+
}
|
|
3162
|
+
export {
|
|
3163
|
+
AnthropicTransformer,
|
|
3164
|
+
BUILTIN_TRANSFORMER_NAMES,
|
|
3165
|
+
BuiltinTransformers,
|
|
3166
|
+
DeepseekTransformer,
|
|
3167
|
+
GeminiCodeAssistTransformer,
|
|
3168
|
+
GeminiTransformer,
|
|
3169
|
+
OpenAIResponseTransformer,
|
|
3170
|
+
OpenCodeGoTransformer,
|
|
3171
|
+
ReasoningTransformer,
|
|
3172
|
+
getBuiltinTransformers,
|
|
3173
|
+
registerBuiltinTransformers
|
|
3174
|
+
};
|