@omnicross/core 0.1.0

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