@omnicross/core 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ApiConverter.cjs +799 -0
- package/dist/ApiConverter.d.cts +82 -0
- package/dist/ApiConverter.d.ts +82 -0
- package/dist/ApiConverter.js +763 -0
- package/dist/BuiltinToolExecutor-BluWyeob.d.ts +81 -0
- package/dist/BuiltinToolExecutor-CS2WpXhM.d.cts +81 -0
- package/dist/CompletionService-7fCmKAP3.d.ts +212 -0
- package/dist/CompletionService-DtOF_War.d.cts +212 -0
- package/dist/{ProviderProxy-f_8ziIhW.d.cts → ProviderProxy-C-xqrkKi.d.ts} +7 -2
- package/dist/{ProviderProxy-vjt8sQQk.d.ts → ProviderProxy-CnMQYN59.d.cts} +7 -2
- package/dist/completion/BuiltinToolExecutor.cjs +327 -0
- package/dist/completion/BuiltinToolExecutor.d.cts +4 -0
- package/dist/completion/BuiltinToolExecutor.d.ts +4 -0
- package/dist/completion/BuiltinToolExecutor.js +296 -0
- package/dist/completion/CompletionService.cjs +3487 -0
- package/dist/completion/CompletionService.d.cts +21 -0
- package/dist/completion/CompletionService.d.ts +21 -0
- package/dist/completion/CompletionService.js +3461 -0
- package/dist/completion/NativeSearchInjector.cjs +196 -0
- package/dist/completion/NativeSearchInjector.d.cts +42 -0
- package/dist/completion/NativeSearchInjector.d.ts +42 -0
- package/dist/completion/NativeSearchInjector.js +167 -0
- package/dist/completion/ProviderSearchInjector.cjs +87 -0
- package/dist/completion/ProviderSearchInjector.d.cts +47 -0
- package/dist/completion/ProviderSearchInjector.d.ts +47 -0
- package/dist/completion/ProviderSearchInjector.js +60 -0
- package/dist/completion/native-search-types.cjs +67 -0
- package/dist/completion/native-search-types.d.cts +3 -0
- package/dist/completion/native-search-types.d.ts +3 -0
- package/dist/completion/native-search-types.js +38 -0
- package/dist/completion/openrouter-headers.cjs +72 -0
- package/dist/completion/openrouter-headers.d.cts +44 -0
- package/dist/completion/openrouter-headers.d.ts +44 -0
- package/dist/completion/openrouter-headers.js +42 -0
- package/dist/completion/openrouter-models.cjs +86 -0
- package/dist/completion/openrouter-models.d.cts +27 -0
- package/dist/completion/openrouter-models.d.ts +27 -0
- package/dist/completion/openrouter-models.js +59 -0
- package/dist/completion/types.cjs +18 -0
- package/dist/completion/types.d.cts +3 -0
- package/dist/completion/types.d.ts +3 -0
- package/dist/completion/types.js +0 -0
- package/dist/completion/url-builder.cjs +138 -0
- package/dist/completion/url-builder.d.cts +87 -0
- package/dist/completion/url-builder.d.ts +87 -0
- package/dist/completion/url-builder.js +104 -0
- package/dist/completion.d.cts +148 -7
- package/dist/completion.d.ts +148 -7
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +27 -90
- package/dist/index.d.ts +27 -90
- package/dist/index.js +1 -0
- package/dist/outbound-api/routeResolver.cjs +221 -0
- package/dist/outbound-api/routeResolver.d.cts +18 -0
- package/dist/outbound-api/routeResolver.d.ts +18 -0
- package/dist/outbound-api/routeResolver.js +192 -0
- package/dist/outbound-api/subscriptionRegistryPort.d.cts +5 -2
- package/dist/outbound-api/subscriptionRegistryPort.d.ts +5 -2
- package/dist/outbound-api/types.cjs +18 -0
- package/dist/{types-CbCN2NQP.d.ts → outbound-api/types.d.cts} +17 -3
- package/dist/{types-CGGrKqC_.d.cts → outbound-api/types.d.ts} +17 -3
- package/dist/outbound-api/types.js +0 -0
- package/dist/outbound-api.cjs +1 -0
- package/dist/outbound-api.d.cts +14 -87
- package/dist/outbound-api.d.ts +14 -87
- package/dist/outbound-api.js +1 -0
- package/dist/pipeline/AuthSource.cjs +18 -0
- package/dist/pipeline/AuthSource.d.cts +101 -0
- package/dist/pipeline/AuthSource.d.ts +101 -0
- package/dist/pipeline/AuthSource.js +0 -0
- package/dist/pipeline/LlmConfigProviderAuth.cjs +169 -0
- package/dist/pipeline/LlmConfigProviderAuth.d.cts +86 -0
- package/dist/pipeline/LlmConfigProviderAuth.d.ts +86 -0
- package/dist/pipeline/LlmConfigProviderAuth.js +142 -0
- package/dist/pipeline/SubscriptionAuthSource.d.cts +165 -3
- package/dist/pipeline/SubscriptionAuthSource.d.ts +165 -3
- package/dist/pipeline/executeProviderCall.cjs +70 -0
- package/dist/pipeline/executeProviderCall.d.cts +149 -0
- package/dist/pipeline/executeProviderCall.d.ts +149 -0
- package/dist/pipeline/executeProviderCall.js +45 -0
- package/dist/pipeline/resolveProviderChain.cjs +47 -0
- package/dist/pipeline/resolveProviderChain.d.cts +58 -0
- package/dist/pipeline/resolveProviderChain.d.ts +58 -0
- package/dist/pipeline/resolveProviderChain.js +22 -0
- package/dist/pipeline/resolveSubscriptionChain.cjs +68 -0
- package/dist/pipeline/resolveSubscriptionChain.d.cts +68 -0
- package/dist/pipeline/resolveSubscriptionChain.d.ts +68 -0
- package/dist/pipeline/resolveSubscriptionChain.js +43 -0
- package/dist/ports/provider-config-source.cjs +18 -0
- package/dist/ports/provider-config-source.d.cts +51 -0
- package/dist/ports/provider-config-source.d.ts +51 -0
- package/dist/ports/provider-config-source.js +0 -0
- package/dist/ports/web-search-backend.cjs +18 -0
- package/dist/ports/web-search-backend.d.cts +29 -0
- package/dist/ports/web-search-backend.d.ts +29 -0
- package/dist/ports/web-search-backend.js +0 -0
- package/dist/ports.d.cts +10 -7
- package/dist/ports.d.ts +10 -7
- package/dist/provider-proxy/ProviderProxy.cjs +4643 -0
- package/dist/provider-proxy/ProviderProxy.d.cts +16 -0
- package/dist/provider-proxy/ProviderProxy.d.ts +16 -0
- package/dist/provider-proxy/ProviderProxy.js +4618 -0
- package/dist/provider-proxy/ingress/providerProxyShared.d.cts +5 -2
- package/dist/provider-proxy/ingress/providerProxyShared.d.ts +5 -2
- package/dist/provider-proxy/types.d.cts +406 -8
- package/dist/provider-proxy/types.d.ts +406 -8
- package/dist/provider-proxy.cjs +1 -0
- package/dist/provider-proxy.d.cts +8 -5
- package/dist/provider-proxy.d.ts +8 -5
- package/dist/provider-proxy.js +1 -0
- package/dist/routeResolver-BrbK6ja9.d.cts +88 -0
- package/dist/routeResolver-HE-ZO0fO.d.ts +88 -0
- package/dist/transformer/anthropicBetaInject.cjs +51 -0
- package/dist/transformer/anthropicBetaInject.d.cts +20 -0
- package/dist/transformer/anthropicBetaInject.d.ts +20 -0
- package/dist/transformer/anthropicBetaInject.js +25 -0
- package/dist/transformer/transformers/AnthropicTransformer.cjs +1017 -0
- package/dist/transformer/transformers/AnthropicTransformer.d.cts +148 -0
- package/dist/transformer/transformers/AnthropicTransformer.d.ts +148 -0
- package/dist/transformer/transformers/AnthropicTransformer.js +990 -0
- package/dist/transformer/transformers/ReasoningTransformer.cjs +273 -0
- package/dist/transformer/transformers/ReasoningTransformer.d.cts +47 -0
- package/dist/transformer/transformers/ReasoningTransformer.d.ts +47 -0
- package/dist/transformer/transformers/ReasoningTransformer.js +253 -0
- package/dist/transformer/transformers.cjs +3206 -0
- package/dist/transformer/transformers.d.cts +100 -0
- package/dist/transformer/transformers.d.ts +100 -0
- package/dist/transformer/transformers.js +3174 -0
- package/dist/transformer.d.cts +8 -31
- package/dist/transformer.d.ts +8 -31
- package/dist/types-BScIHmPr.d.cts +153 -0
- package/dist/types-BScIHmPr.d.ts +153 -0
- package/package.json +3 -3
- package/dist/SubscriptionAuthSource-Cr4fVEYY.d.cts +0 -264
- package/dist/SubscriptionAuthSource-D89zmiSS.d.ts +0 -264
- package/dist/index-BTSmc9Sm.d.ts +0 -645
- package/dist/index-DXazdTzZ.d.cts +0 -645
- package/dist/types-DCzHkhJt.d.ts +0 -467
- package/dist/types-DZIQbgp0.d.cts +0 -467
|
@@ -0,0 +1,799 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/ApiConverter.ts
|
|
21
|
+
var ApiConverter_exports = {};
|
|
22
|
+
__export(ApiConverter_exports, {
|
|
23
|
+
convertAnthropicRequestToOpenAI: () => convertAnthropicRequestToOpenAI,
|
|
24
|
+
convertAnthropicStreamToOpenAI: () => convertAnthropicStreamToOpenAI,
|
|
25
|
+
convertAnthropicToOpenAI: () => convertAnthropicToOpenAI,
|
|
26
|
+
convertAnthropicToOpenAIWithThinking: () => convertAnthropicToOpenAIWithThinking,
|
|
27
|
+
convertOpenAIResponseToAnthropic: () => convertOpenAIResponseToAnthropic,
|
|
28
|
+
convertOpenAIStreamToAnthropic: () => convertOpenAIStreamToAnthropic,
|
|
29
|
+
convertOpenAIToAnthropic: () => convertOpenAIToAnthropic,
|
|
30
|
+
convertOpenAIToAnthropicWithThinking: () => convertOpenAIToAnthropicWithThinking,
|
|
31
|
+
hasImageContent: () => hasImageContent,
|
|
32
|
+
hasThinkingEnabled: () => hasThinkingEnabled
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(ApiConverter_exports);
|
|
35
|
+
|
|
36
|
+
// src/api-converter/shared.ts
|
|
37
|
+
function convertOpenAITool(tool) {
|
|
38
|
+
return {
|
|
39
|
+
name: tool.function.name,
|
|
40
|
+
description: tool.function.description,
|
|
41
|
+
input_schema: tool.function.parameters
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function mapAnthropicStopReason(stopReason) {
|
|
45
|
+
switch (stopReason) {
|
|
46
|
+
case "end_turn":
|
|
47
|
+
return "stop";
|
|
48
|
+
case "max_tokens":
|
|
49
|
+
return "length";
|
|
50
|
+
case "tool_use":
|
|
51
|
+
return "tool_calls";
|
|
52
|
+
case "stop_sequence":
|
|
53
|
+
return "stop";
|
|
54
|
+
default:
|
|
55
|
+
return "stop";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function mapOpenAIFinishReason(finishReason) {
|
|
59
|
+
switch (finishReason) {
|
|
60
|
+
case "stop":
|
|
61
|
+
return "end_turn";
|
|
62
|
+
case "length":
|
|
63
|
+
return "max_tokens";
|
|
64
|
+
case "tool_calls":
|
|
65
|
+
return "tool_use";
|
|
66
|
+
case "content_filter":
|
|
67
|
+
return "end_turn";
|
|
68
|
+
default:
|
|
69
|
+
return "end_turn";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function hasImageContent(request) {
|
|
73
|
+
const messages = request.messages;
|
|
74
|
+
for (const msg of messages) {
|
|
75
|
+
if (Array.isArray(msg.content)) {
|
|
76
|
+
for (const part of msg.content) {
|
|
77
|
+
if ("type" in part && part.type === "image_url") {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
if ("type" in part && part.type === "image") {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
function hasThinkingEnabled(request) {
|
|
89
|
+
if ("thinking" in request && request.thinking?.type === "enabled") {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
if ("reasoning" in request) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/api-converter/anthropic-to-openai.ts
|
|
99
|
+
function convertAnthropicToOpenAI(response) {
|
|
100
|
+
const content = [];
|
|
101
|
+
const toolCalls = [];
|
|
102
|
+
let reasoningContent = "";
|
|
103
|
+
for (const block of response.content) {
|
|
104
|
+
if (block.type === "text") {
|
|
105
|
+
content.push(block.text);
|
|
106
|
+
} else if (block.type === "tool_use") {
|
|
107
|
+
toolCalls.push({
|
|
108
|
+
id: block.id,
|
|
109
|
+
type: "function",
|
|
110
|
+
function: {
|
|
111
|
+
name: block.name,
|
|
112
|
+
arguments: JSON.stringify(block.input)
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
} else if (block.type === "thinking") {
|
|
116
|
+
reasoningContent += block.thinking;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const finishReason = mapAnthropicStopReason(response.stop_reason);
|
|
120
|
+
const result = {
|
|
121
|
+
id: response.id,
|
|
122
|
+
object: "chat.completion",
|
|
123
|
+
created: Math.floor(Date.now() / 1e3),
|
|
124
|
+
model: response.model,
|
|
125
|
+
choices: [
|
|
126
|
+
{
|
|
127
|
+
index: 0,
|
|
128
|
+
message: {
|
|
129
|
+
role: "assistant",
|
|
130
|
+
content: content.join("\n") || null,
|
|
131
|
+
...toolCalls.length > 0 ? { tool_calls: toolCalls } : {}
|
|
132
|
+
},
|
|
133
|
+
finish_reason: finishReason
|
|
134
|
+
}
|
|
135
|
+
],
|
|
136
|
+
usage: {
|
|
137
|
+
prompt_tokens: response.usage.input_tokens,
|
|
138
|
+
completion_tokens: response.usage.output_tokens,
|
|
139
|
+
total_tokens: response.usage.input_tokens + response.usage.output_tokens
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
if (reasoningContent) {
|
|
143
|
+
result.reasoning_content = reasoningContent;
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
function convertAnthropicRequestToOpenAI(request) {
|
|
148
|
+
const messages = [];
|
|
149
|
+
if (request.system) {
|
|
150
|
+
if (typeof request.system === "string") {
|
|
151
|
+
messages.push({ role: "system", content: request.system });
|
|
152
|
+
} else if (Array.isArray(request.system)) {
|
|
153
|
+
const systemText = request.system.filter((s) => s.type === "text").map((s) => s.text).join("\n");
|
|
154
|
+
messages.push({ role: "system", content: systemText });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
for (const msg of request.messages) {
|
|
158
|
+
if (typeof msg.content === "string") {
|
|
159
|
+
messages.push({
|
|
160
|
+
role: msg.role,
|
|
161
|
+
content: msg.content
|
|
162
|
+
});
|
|
163
|
+
} else if (Array.isArray(msg.content)) {
|
|
164
|
+
const openaiContent = [];
|
|
165
|
+
const toolCalls = [];
|
|
166
|
+
let toolResultId;
|
|
167
|
+
let toolResultContent;
|
|
168
|
+
for (const part of msg.content) {
|
|
169
|
+
if (part.type === "text") {
|
|
170
|
+
openaiContent.push({ type: "text", text: part.text });
|
|
171
|
+
} else if (part.type === "image") {
|
|
172
|
+
if (part.source.type === "base64") {
|
|
173
|
+
openaiContent.push({
|
|
174
|
+
type: "image_url",
|
|
175
|
+
image_url: {
|
|
176
|
+
url: `data:${part.source.media_type};base64,${part.source.data}`
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
} else if (part.source.url) {
|
|
180
|
+
openaiContent.push({
|
|
181
|
+
type: "image_url",
|
|
182
|
+
image_url: { url: part.source.url }
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
} else if (part.type === "tool_use") {
|
|
186
|
+
toolCalls.push({
|
|
187
|
+
id: part.id,
|
|
188
|
+
type: "function",
|
|
189
|
+
function: {
|
|
190
|
+
name: part.name,
|
|
191
|
+
arguments: JSON.stringify(part.input)
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
} else if (part.type === "tool_result") {
|
|
195
|
+
toolResultId = part.tool_use_id;
|
|
196
|
+
toolResultContent = typeof part.content === "string" ? part.content : JSON.stringify(part.content);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (toolResultId && toolResultContent !== void 0) {
|
|
200
|
+
messages.push({
|
|
201
|
+
role: "tool",
|
|
202
|
+
tool_call_id: toolResultId,
|
|
203
|
+
content: toolResultContent
|
|
204
|
+
});
|
|
205
|
+
} else if (toolCalls.length > 0) {
|
|
206
|
+
messages.push({
|
|
207
|
+
role: "assistant",
|
|
208
|
+
content: openaiContent.length > 0 ? openaiContent.map((c) => c.type === "text" ? c.text : "").join("\n") : null,
|
|
209
|
+
tool_calls: toolCalls
|
|
210
|
+
});
|
|
211
|
+
} else {
|
|
212
|
+
messages.push({
|
|
213
|
+
role: msg.role,
|
|
214
|
+
content: openaiContent.length > 0 ? openaiContent : null
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const result = {
|
|
220
|
+
model: request.model,
|
|
221
|
+
messages,
|
|
222
|
+
max_tokens: request.max_tokens,
|
|
223
|
+
stream: request.stream
|
|
224
|
+
};
|
|
225
|
+
if (request.tools && request.tools.length > 0) {
|
|
226
|
+
result.tools = request.tools.map((tool) => ({
|
|
227
|
+
type: "function",
|
|
228
|
+
function: {
|
|
229
|
+
name: tool.name,
|
|
230
|
+
description: tool.description,
|
|
231
|
+
parameters: tool.input_schema
|
|
232
|
+
}
|
|
233
|
+
}));
|
|
234
|
+
if (request.tool_choice) {
|
|
235
|
+
if (request.tool_choice.type === "auto") {
|
|
236
|
+
result.tool_choice = "auto";
|
|
237
|
+
} else if (request.tool_choice.type === "any") {
|
|
238
|
+
result.tool_choice = "required";
|
|
239
|
+
} else if (request.tool_choice.type === "tool") {
|
|
240
|
+
result.tool_choice = {
|
|
241
|
+
type: "function",
|
|
242
|
+
function: { name: request.tool_choice.name }
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
function convertAnthropicToOpenAIWithThinking(response) {
|
|
250
|
+
const baseResult = convertAnthropicToOpenAI(response);
|
|
251
|
+
const thinkingBlocks = response.content.filter(
|
|
252
|
+
(block) => block.type === "thinking"
|
|
253
|
+
);
|
|
254
|
+
if (thinkingBlocks.length > 0) {
|
|
255
|
+
baseResult.reasoning_content = thinkingBlocks.map((block) => "thinking" in block ? block.thinking : "").join("\n");
|
|
256
|
+
}
|
|
257
|
+
return baseResult;
|
|
258
|
+
}
|
|
259
|
+
async function* convertAnthropicStreamToOpenAI(anthropicStream) {
|
|
260
|
+
let messageId = "";
|
|
261
|
+
let model = "unknown";
|
|
262
|
+
const toolCalls = /* @__PURE__ */ new Map();
|
|
263
|
+
for await (const line of anthropicStream) {
|
|
264
|
+
if (!line.startsWith("data:")) continue;
|
|
265
|
+
const data = line.slice(5).trim();
|
|
266
|
+
try {
|
|
267
|
+
const event = JSON.parse(data);
|
|
268
|
+
switch (event.type) {
|
|
269
|
+
case "message_start":
|
|
270
|
+
messageId = event.message?.id || `chatcmpl-${Date.now()}`;
|
|
271
|
+
model = event.message?.model || model;
|
|
272
|
+
break;
|
|
273
|
+
case "content_block_delta":
|
|
274
|
+
if (event.delta?.type === "text_delta") {
|
|
275
|
+
yield `data: ${JSON.stringify({
|
|
276
|
+
id: messageId,
|
|
277
|
+
object: "chat.completion.chunk",
|
|
278
|
+
created: Math.floor(Date.now() / 1e3),
|
|
279
|
+
model,
|
|
280
|
+
choices: [{
|
|
281
|
+
index: 0,
|
|
282
|
+
delta: { content: event.delta.text },
|
|
283
|
+
finish_reason: null
|
|
284
|
+
}]
|
|
285
|
+
})}
|
|
286
|
+
|
|
287
|
+
`;
|
|
288
|
+
} else if (event.delta?.type === "thinking_delta") {
|
|
289
|
+
yield `data: ${JSON.stringify({
|
|
290
|
+
id: messageId,
|
|
291
|
+
object: "chat.completion.chunk",
|
|
292
|
+
created: Math.floor(Date.now() / 1e3),
|
|
293
|
+
model,
|
|
294
|
+
choices: [{
|
|
295
|
+
index: 0,
|
|
296
|
+
delta: { reasoning_content: event.delta.thinking },
|
|
297
|
+
finish_reason: null
|
|
298
|
+
}]
|
|
299
|
+
})}
|
|
300
|
+
|
|
301
|
+
`;
|
|
302
|
+
} else if (event.delta?.type === "input_json_delta") {
|
|
303
|
+
const toolIndex = event.index || 0;
|
|
304
|
+
const tc = toolCalls.get(toolIndex);
|
|
305
|
+
if (tc) {
|
|
306
|
+
yield `data: ${JSON.stringify({
|
|
307
|
+
id: messageId,
|
|
308
|
+
object: "chat.completion.chunk",
|
|
309
|
+
created: Math.floor(Date.now() / 1e3),
|
|
310
|
+
model,
|
|
311
|
+
choices: [{
|
|
312
|
+
index: 0,
|
|
313
|
+
delta: {
|
|
314
|
+
tool_calls: [{
|
|
315
|
+
index: toolIndex,
|
|
316
|
+
function: { arguments: event.delta.partial_json }
|
|
317
|
+
}]
|
|
318
|
+
},
|
|
319
|
+
finish_reason: null
|
|
320
|
+
}]
|
|
321
|
+
})}
|
|
322
|
+
|
|
323
|
+
`;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
break;
|
|
327
|
+
case "content_block_start":
|
|
328
|
+
if (event.content_block?.type === "tool_use") {
|
|
329
|
+
const toolIndex = event.index || 0;
|
|
330
|
+
toolCalls.set(toolIndex, {
|
|
331
|
+
id: event.content_block.id,
|
|
332
|
+
name: event.content_block.name,
|
|
333
|
+
arguments: ""
|
|
334
|
+
});
|
|
335
|
+
yield `data: ${JSON.stringify({
|
|
336
|
+
id: messageId,
|
|
337
|
+
object: "chat.completion.chunk",
|
|
338
|
+
created: Math.floor(Date.now() / 1e3),
|
|
339
|
+
model,
|
|
340
|
+
choices: [{
|
|
341
|
+
index: 0,
|
|
342
|
+
delta: {
|
|
343
|
+
tool_calls: [{
|
|
344
|
+
index: toolIndex,
|
|
345
|
+
id: event.content_block.id,
|
|
346
|
+
type: "function",
|
|
347
|
+
function: { name: event.content_block.name, arguments: "" }
|
|
348
|
+
}]
|
|
349
|
+
},
|
|
350
|
+
finish_reason: null
|
|
351
|
+
}]
|
|
352
|
+
})}
|
|
353
|
+
|
|
354
|
+
`;
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
case "message_delta":
|
|
358
|
+
if (event.delta?.stop_reason) {
|
|
359
|
+
const finishReason = mapAnthropicStopReason(event.delta.stop_reason);
|
|
360
|
+
yield `data: ${JSON.stringify({
|
|
361
|
+
id: messageId,
|
|
362
|
+
object: "chat.completion.chunk",
|
|
363
|
+
created: Math.floor(Date.now() / 1e3),
|
|
364
|
+
model,
|
|
365
|
+
choices: [{
|
|
366
|
+
index: 0,
|
|
367
|
+
delta: {},
|
|
368
|
+
finish_reason: finishReason
|
|
369
|
+
}],
|
|
370
|
+
usage: event.usage ? {
|
|
371
|
+
prompt_tokens: event.usage.input_tokens || 0,
|
|
372
|
+
completion_tokens: event.usage.output_tokens || 0,
|
|
373
|
+
total_tokens: (event.usage.input_tokens || 0) + (event.usage.output_tokens || 0)
|
|
374
|
+
} : void 0
|
|
375
|
+
})}
|
|
376
|
+
|
|
377
|
+
`;
|
|
378
|
+
}
|
|
379
|
+
break;
|
|
380
|
+
case "message_stop":
|
|
381
|
+
yield "data: [DONE]\n\n";
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
} catch {
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// src/api-converter/openai-to-anthropic.ts
|
|
390
|
+
function convertOpenAIToAnthropic(request, config) {
|
|
391
|
+
const messages = [];
|
|
392
|
+
let system;
|
|
393
|
+
for (const msg of request.messages) {
|
|
394
|
+
if (msg.role === "system") {
|
|
395
|
+
if (typeof msg.content === "string") {
|
|
396
|
+
system = msg.content;
|
|
397
|
+
} else if (Array.isArray(msg.content)) {
|
|
398
|
+
system = msg.content.filter((p) => p.type === "text").map((p) => ({ type: "text", text: p.text || "" }));
|
|
399
|
+
}
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
if (msg.role === "tool" && msg.tool_call_id) {
|
|
403
|
+
const toolResult = {
|
|
404
|
+
type: "tool_result",
|
|
405
|
+
tool_use_id: msg.tool_call_id,
|
|
406
|
+
content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
|
|
407
|
+
};
|
|
408
|
+
const lastMsg = messages[messages.length - 1];
|
|
409
|
+
if (lastMsg && lastMsg.role === "user" && Array.isArray(lastMsg.content)) {
|
|
410
|
+
lastMsg.content.push(toolResult);
|
|
411
|
+
} else {
|
|
412
|
+
messages.push({ role: "user", content: [toolResult] });
|
|
413
|
+
}
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (msg.role === "assistant" && msg.tool_calls && msg.tool_calls.length > 0) {
|
|
417
|
+
const content = [];
|
|
418
|
+
if (msg.content) {
|
|
419
|
+
if (typeof msg.content === "string") {
|
|
420
|
+
content.push({ type: "text", text: msg.content });
|
|
421
|
+
} else if (Array.isArray(msg.content)) {
|
|
422
|
+
for (const part of msg.content) {
|
|
423
|
+
if (part.type === "text" && part.text) {
|
|
424
|
+
content.push({ type: "text", text: part.text });
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
for (const tc of msg.tool_calls) {
|
|
430
|
+
content.push({
|
|
431
|
+
type: "tool_use",
|
|
432
|
+
id: tc.id,
|
|
433
|
+
name: tc.function.name,
|
|
434
|
+
input: JSON.parse(tc.function.arguments || "{}")
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
messages.push({ role: "assistant", content });
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
if (msg.role === "user" || msg.role === "assistant") {
|
|
441
|
+
const content = convertOpenAIMessageContent(msg);
|
|
442
|
+
messages.push({ role: msg.role, content });
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
let model = request.model;
|
|
446
|
+
if (config.modelMapping && config.modelMapping[model]) {
|
|
447
|
+
model = config.modelMapping[model];
|
|
448
|
+
} else if (!model.startsWith("claude")) {
|
|
449
|
+
model = config.defaultModel;
|
|
450
|
+
}
|
|
451
|
+
const result = {
|
|
452
|
+
model,
|
|
453
|
+
// Anthropic requires max_tokens; use high default (16384) if not explicitly set
|
|
454
|
+
// This prevents output truncation for long responses
|
|
455
|
+
max_tokens: request.max_tokens || 16384,
|
|
456
|
+
messages,
|
|
457
|
+
stream: request.stream
|
|
458
|
+
};
|
|
459
|
+
if (system) {
|
|
460
|
+
result.system = system;
|
|
461
|
+
}
|
|
462
|
+
if (request.tools && request.tools.length > 0) {
|
|
463
|
+
result.tools = request.tools.map(convertOpenAITool);
|
|
464
|
+
if (request.tool_choice) {
|
|
465
|
+
if (request.tool_choice === "auto") {
|
|
466
|
+
result.tool_choice = { type: "auto" };
|
|
467
|
+
} else if (request.tool_choice === "required") {
|
|
468
|
+
result.tool_choice = { type: "any" };
|
|
469
|
+
} else if (typeof request.tool_choice === "object") {
|
|
470
|
+
result.tool_choice = {
|
|
471
|
+
type: "tool",
|
|
472
|
+
name: request.tool_choice.function.name
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return result;
|
|
478
|
+
}
|
|
479
|
+
function convertOpenAIMessageContent(msg) {
|
|
480
|
+
if (typeof msg.content === "string") {
|
|
481
|
+
return msg.content;
|
|
482
|
+
}
|
|
483
|
+
if (!msg.content || !Array.isArray(msg.content)) {
|
|
484
|
+
return "";
|
|
485
|
+
}
|
|
486
|
+
const parts = [];
|
|
487
|
+
for (const part of msg.content) {
|
|
488
|
+
if (part.type === "text" && part.text) {
|
|
489
|
+
parts.push({ type: "text", text: part.text });
|
|
490
|
+
} else if (part.type === "image_url" && part.image_url) {
|
|
491
|
+
const url = part.image_url.url;
|
|
492
|
+
if (url.startsWith("data:")) {
|
|
493
|
+
const match = url.match(/^data:([^;]+);base64,(.+)$/);
|
|
494
|
+
if (match) {
|
|
495
|
+
parts.push({
|
|
496
|
+
type: "image",
|
|
497
|
+
source: {
|
|
498
|
+
type: "base64",
|
|
499
|
+
media_type: match[1],
|
|
500
|
+
data: match[2]
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
} else {
|
|
505
|
+
parts.push({
|
|
506
|
+
type: "image",
|
|
507
|
+
source: { type: "url", url }
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return parts.length > 0 ? parts : "";
|
|
513
|
+
}
|
|
514
|
+
function convertOpenAIResponseToAnthropic(response) {
|
|
515
|
+
const content = [];
|
|
516
|
+
const choice = response.choices[0];
|
|
517
|
+
if (choice.message.content) {
|
|
518
|
+
content.push({ type: "text", text: choice.message.content });
|
|
519
|
+
}
|
|
520
|
+
if (choice.message.tool_calls) {
|
|
521
|
+
for (const tc of choice.message.tool_calls) {
|
|
522
|
+
content.push({
|
|
523
|
+
type: "tool_use",
|
|
524
|
+
id: tc.id,
|
|
525
|
+
name: tc.function.name,
|
|
526
|
+
input: JSON.parse(tc.function.arguments || "{}")
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (response.reasoning_content) {
|
|
531
|
+
content.unshift({
|
|
532
|
+
type: "thinking",
|
|
533
|
+
thinking: response.reasoning_content
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
return {
|
|
537
|
+
id: response.id,
|
|
538
|
+
type: "message",
|
|
539
|
+
role: "assistant",
|
|
540
|
+
model: response.model,
|
|
541
|
+
content,
|
|
542
|
+
stop_reason: mapOpenAIFinishReason(choice.finish_reason),
|
|
543
|
+
usage: {
|
|
544
|
+
input_tokens: response.usage.prompt_tokens,
|
|
545
|
+
output_tokens: response.usage.completion_tokens
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
function convertOpenAIToAnthropicWithThinking(request, config) {
|
|
550
|
+
const baseResult = convertOpenAIToAnthropic(request, config);
|
|
551
|
+
if (config.enableThinking) {
|
|
552
|
+
baseResult.thinking = {
|
|
553
|
+
type: "enabled",
|
|
554
|
+
budget_tokens: config.thinkingBudget || 1e4
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
return baseResult;
|
|
558
|
+
}
|
|
559
|
+
async function* convertOpenAIStreamToAnthropic(openaiStream) {
|
|
560
|
+
const messageId = `msg_${Date.now()}`;
|
|
561
|
+
let hasStarted = false;
|
|
562
|
+
let hasTextContentStarted = false;
|
|
563
|
+
let hasThinkingContentStarted = false;
|
|
564
|
+
let contentIndex = 0;
|
|
565
|
+
let model = "unknown";
|
|
566
|
+
const toolCalls = /* @__PURE__ */ new Map();
|
|
567
|
+
for await (const line of openaiStream) {
|
|
568
|
+
if (!line.startsWith("data:")) continue;
|
|
569
|
+
const data = line.slice(5).trim();
|
|
570
|
+
if (data === "[DONE]") {
|
|
571
|
+
if (hasTextContentStarted) {
|
|
572
|
+
yield `event: content_block_stop
|
|
573
|
+
data: ${JSON.stringify({
|
|
574
|
+
type: "content_block_stop",
|
|
575
|
+
index: contentIndex
|
|
576
|
+
})}
|
|
577
|
+
|
|
578
|
+
`;
|
|
579
|
+
contentIndex++;
|
|
580
|
+
hasTextContentStarted = false;
|
|
581
|
+
}
|
|
582
|
+
if (hasThinkingContentStarted) {
|
|
583
|
+
yield `event: content_block_stop
|
|
584
|
+
data: ${JSON.stringify({
|
|
585
|
+
type: "content_block_stop",
|
|
586
|
+
index: contentIndex
|
|
587
|
+
})}
|
|
588
|
+
|
|
589
|
+
`;
|
|
590
|
+
contentIndex++;
|
|
591
|
+
hasThinkingContentStarted = false;
|
|
592
|
+
}
|
|
593
|
+
for (const [, tc] of toolCalls) {
|
|
594
|
+
yield `event: content_block_stop
|
|
595
|
+
data: ${JSON.stringify({
|
|
596
|
+
type: "content_block_stop",
|
|
597
|
+
index: tc.contentIndex
|
|
598
|
+
})}
|
|
599
|
+
|
|
600
|
+
`;
|
|
601
|
+
}
|
|
602
|
+
yield `event: message_delta
|
|
603
|
+
data: ${JSON.stringify({
|
|
604
|
+
type: "message_delta",
|
|
605
|
+
delta: { stop_reason: "end_turn", stop_sequence: null },
|
|
606
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
607
|
+
})}
|
|
608
|
+
|
|
609
|
+
`;
|
|
610
|
+
yield `event: message_stop
|
|
611
|
+
data: ${JSON.stringify({ type: "message_stop" })}
|
|
612
|
+
|
|
613
|
+
`;
|
|
614
|
+
break;
|
|
615
|
+
}
|
|
616
|
+
try {
|
|
617
|
+
const chunk = JSON.parse(data);
|
|
618
|
+
model = chunk.model || model;
|
|
619
|
+
if (!hasStarted) {
|
|
620
|
+
hasStarted = true;
|
|
621
|
+
yield `event: message_start
|
|
622
|
+
data: ${JSON.stringify({
|
|
623
|
+
type: "message_start",
|
|
624
|
+
message: {
|
|
625
|
+
id: messageId,
|
|
626
|
+
type: "message",
|
|
627
|
+
role: "assistant",
|
|
628
|
+
content: [],
|
|
629
|
+
model,
|
|
630
|
+
stop_reason: null,
|
|
631
|
+
stop_sequence: null,
|
|
632
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
633
|
+
}
|
|
634
|
+
})}
|
|
635
|
+
|
|
636
|
+
`;
|
|
637
|
+
}
|
|
638
|
+
const choice = chunk.choices?.[0];
|
|
639
|
+
if (!choice) continue;
|
|
640
|
+
if (choice.delta?.content) {
|
|
641
|
+
if (!hasTextContentStarted) {
|
|
642
|
+
hasTextContentStarted = true;
|
|
643
|
+
yield `event: content_block_start
|
|
644
|
+
data: ${JSON.stringify({
|
|
645
|
+
type: "content_block_start",
|
|
646
|
+
index: contentIndex,
|
|
647
|
+
content_block: { type: "text", text: "" }
|
|
648
|
+
})}
|
|
649
|
+
|
|
650
|
+
`;
|
|
651
|
+
}
|
|
652
|
+
yield `event: content_block_delta
|
|
653
|
+
data: ${JSON.stringify({
|
|
654
|
+
type: "content_block_delta",
|
|
655
|
+
index: contentIndex,
|
|
656
|
+
delta: { type: "text_delta", text: choice.delta.content }
|
|
657
|
+
})}
|
|
658
|
+
|
|
659
|
+
`;
|
|
660
|
+
}
|
|
661
|
+
if (choice.delta?.reasoning_content) {
|
|
662
|
+
if (hasTextContentStarted) {
|
|
663
|
+
yield `event: content_block_stop
|
|
664
|
+
data: ${JSON.stringify({
|
|
665
|
+
type: "content_block_stop",
|
|
666
|
+
index: contentIndex
|
|
667
|
+
})}
|
|
668
|
+
|
|
669
|
+
`;
|
|
670
|
+
contentIndex++;
|
|
671
|
+
hasTextContentStarted = false;
|
|
672
|
+
}
|
|
673
|
+
if (!hasThinkingContentStarted) {
|
|
674
|
+
hasThinkingContentStarted = true;
|
|
675
|
+
yield `event: content_block_start
|
|
676
|
+
data: ${JSON.stringify({
|
|
677
|
+
type: "content_block_start",
|
|
678
|
+
index: contentIndex,
|
|
679
|
+
content_block: { type: "thinking", thinking: "" }
|
|
680
|
+
})}
|
|
681
|
+
|
|
682
|
+
`;
|
|
683
|
+
}
|
|
684
|
+
yield `event: content_block_delta
|
|
685
|
+
data: ${JSON.stringify({
|
|
686
|
+
type: "content_block_delta",
|
|
687
|
+
index: contentIndex,
|
|
688
|
+
delta: { type: "thinking_delta", thinking: choice.delta.reasoning_content }
|
|
689
|
+
})}
|
|
690
|
+
|
|
691
|
+
`;
|
|
692
|
+
}
|
|
693
|
+
if (choice.delta?.tool_calls) {
|
|
694
|
+
if (hasTextContentStarted) {
|
|
695
|
+
yield `event: content_block_stop
|
|
696
|
+
data: ${JSON.stringify({
|
|
697
|
+
type: "content_block_stop",
|
|
698
|
+
index: contentIndex
|
|
699
|
+
})}
|
|
700
|
+
|
|
701
|
+
`;
|
|
702
|
+
contentIndex++;
|
|
703
|
+
hasTextContentStarted = false;
|
|
704
|
+
}
|
|
705
|
+
for (const toolCall of choice.delta.tool_calls) {
|
|
706
|
+
const toolIndex = toolCall.index ?? 0;
|
|
707
|
+
if (!toolCalls.has(toolIndex)) {
|
|
708
|
+
const toolContentIndex = contentIndex++;
|
|
709
|
+
const toolId = toolCall.id || `call_${Date.now()}_${toolIndex}`;
|
|
710
|
+
const toolName = toolCall.function?.name || `tool_${toolIndex}`;
|
|
711
|
+
toolCalls.set(toolIndex, { id: toolId, name: toolName, contentIndex: toolContentIndex });
|
|
712
|
+
yield `event: content_block_start
|
|
713
|
+
data: ${JSON.stringify({
|
|
714
|
+
type: "content_block_start",
|
|
715
|
+
index: toolContentIndex,
|
|
716
|
+
content_block: { type: "tool_use", id: toolId, name: toolName, input: {} }
|
|
717
|
+
})}
|
|
718
|
+
|
|
719
|
+
`;
|
|
720
|
+
}
|
|
721
|
+
if (toolCall.function?.arguments) {
|
|
722
|
+
const tc = toolCalls.get(toolIndex);
|
|
723
|
+
yield `event: content_block_delta
|
|
724
|
+
data: ${JSON.stringify({
|
|
725
|
+
type: "content_block_delta",
|
|
726
|
+
index: tc.contentIndex,
|
|
727
|
+
delta: { type: "input_json_delta", partial_json: toolCall.function.arguments }
|
|
728
|
+
})}
|
|
729
|
+
|
|
730
|
+
`;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
if (choice.finish_reason) {
|
|
735
|
+
if (hasTextContentStarted) {
|
|
736
|
+
yield `event: content_block_stop
|
|
737
|
+
data: ${JSON.stringify({
|
|
738
|
+
type: "content_block_stop",
|
|
739
|
+
index: contentIndex
|
|
740
|
+
})}
|
|
741
|
+
|
|
742
|
+
`;
|
|
743
|
+
contentIndex++;
|
|
744
|
+
hasTextContentStarted = false;
|
|
745
|
+
}
|
|
746
|
+
if (hasThinkingContentStarted) {
|
|
747
|
+
yield `event: content_block_stop
|
|
748
|
+
data: ${JSON.stringify({
|
|
749
|
+
type: "content_block_stop",
|
|
750
|
+
index: contentIndex
|
|
751
|
+
})}
|
|
752
|
+
|
|
753
|
+
`;
|
|
754
|
+
contentIndex++;
|
|
755
|
+
hasThinkingContentStarted = false;
|
|
756
|
+
}
|
|
757
|
+
for (const [, tc] of toolCalls) {
|
|
758
|
+
yield `event: content_block_stop
|
|
759
|
+
data: ${JSON.stringify({
|
|
760
|
+
type: "content_block_stop",
|
|
761
|
+
index: tc.contentIndex
|
|
762
|
+
})}
|
|
763
|
+
|
|
764
|
+
`;
|
|
765
|
+
}
|
|
766
|
+
const stopReason = mapOpenAIFinishReason(choice.finish_reason);
|
|
767
|
+
yield `event: message_delta
|
|
768
|
+
data: ${JSON.stringify({
|
|
769
|
+
type: "message_delta",
|
|
770
|
+
delta: { stop_reason: stopReason, stop_sequence: null },
|
|
771
|
+
usage: {
|
|
772
|
+
input_tokens: chunk.usage?.prompt_tokens || 0,
|
|
773
|
+
output_tokens: chunk.usage?.completion_tokens || 0
|
|
774
|
+
}
|
|
775
|
+
})}
|
|
776
|
+
|
|
777
|
+
`;
|
|
778
|
+
yield `event: message_stop
|
|
779
|
+
data: ${JSON.stringify({ type: "message_stop" })}
|
|
780
|
+
|
|
781
|
+
`;
|
|
782
|
+
}
|
|
783
|
+
} catch {
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
788
|
+
0 && (module.exports = {
|
|
789
|
+
convertAnthropicRequestToOpenAI,
|
|
790
|
+
convertAnthropicStreamToOpenAI,
|
|
791
|
+
convertAnthropicToOpenAI,
|
|
792
|
+
convertAnthropicToOpenAIWithThinking,
|
|
793
|
+
convertOpenAIResponseToAnthropic,
|
|
794
|
+
convertOpenAIStreamToAnthropic,
|
|
795
|
+
convertOpenAIToAnthropic,
|
|
796
|
+
convertOpenAIToAnthropicWithThinking,
|
|
797
|
+
hasImageContent,
|
|
798
|
+
hasThinkingEnabled
|
|
799
|
+
});
|