@eminent337/aery-ai 0.67.77 → 0.67.78
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/README.md +0 -0
- package/package.json +110 -110
- package/dist/api-registry.d.ts +0 -20
- package/dist/api-registry.d.ts.map +0 -1
- package/dist/api-registry.js +0 -44
- package/dist/api-registry.js.map +0 -1
- package/dist/bedrock-provider.d.ts +0 -5
- package/dist/bedrock-provider.d.ts.map +0 -1
- package/dist/bedrock-provider.js +0 -6
- package/dist/bedrock-provider.js.map +0 -1
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -116
- package/dist/cli.js.map +0 -1
- package/dist/env-api-keys.d.ts +0 -18
- package/dist/env-api-keys.d.ts.map +0 -1
- package/dist/env-api-keys.js +0 -162
- package/dist/env-api-keys.js.map +0 -1
- package/dist/index.d.ts +0 -26
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -14
- package/dist/index.js.map +0 -1
- package/dist/models.d.ts +0 -25
- package/dist/models.d.ts.map +0 -1
- package/dist/models.generated.d.ts +0 -15860
- package/dist/models.generated.d.ts.map +0 -1
- package/dist/models.generated.js +0 -15589
- package/dist/models.generated.js.map +0 -1
- package/dist/models.js +0 -63
- package/dist/models.js.map +0 -1
- package/dist/oauth.d.ts +0 -2
- package/dist/oauth.d.ts.map +0 -1
- package/dist/oauth.js +0 -2
- package/dist/oauth.js.map +0 -1
- package/dist/providers/amazon-bedrock.d.ts +0 -38
- package/dist/providers/amazon-bedrock.d.ts.map +0 -1
- package/dist/providers/amazon-bedrock.js +0 -759
- package/dist/providers/amazon-bedrock.js.map +0 -1
- package/dist/providers/anthropic.d.ts +0 -54
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js +0 -931
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/azure-openai-responses.d.ts +0 -15
- package/dist/providers/azure-openai-responses.d.ts.map +0 -1
- package/dist/providers/azure-openai-responses.js +0 -202
- package/dist/providers/azure-openai-responses.js.map +0 -1
- package/dist/providers/cloudflare.d.ts +0 -7
- package/dist/providers/cloudflare.d.ts.map +0 -1
- package/dist/providers/cloudflare.js +0 -19
- package/dist/providers/cloudflare.js.map +0 -1
- package/dist/providers/faux.d.ts +0 -56
- package/dist/providers/faux.d.ts.map +0 -1
- package/dist/providers/faux.js +0 -368
- package/dist/providers/faux.js.map +0 -1
- package/dist/providers/github-copilot-headers.d.ts +0 -8
- package/dist/providers/github-copilot-headers.d.ts.map +0 -1
- package/dist/providers/github-copilot-headers.js +0 -29
- package/dist/providers/github-copilot-headers.js.map +0 -1
- package/dist/providers/google-gemini-cli.d.ts +0 -74
- package/dist/providers/google-gemini-cli.d.ts.map +0 -1
- package/dist/providers/google-gemini-cli.js +0 -779
- package/dist/providers/google-gemini-cli.js.map +0 -1
- package/dist/providers/google-shared.d.ts +0 -65
- package/dist/providers/google-shared.d.ts.map +0 -1
- package/dist/providers/google-shared.js +0 -338
- package/dist/providers/google-shared.js.map +0 -1
- package/dist/providers/google-vertex.d.ts +0 -15
- package/dist/providers/google-vertex.d.ts.map +0 -1
- package/dist/providers/google-vertex.js +0 -441
- package/dist/providers/google-vertex.js.map +0 -1
- package/dist/providers/google.d.ts +0 -13
- package/dist/providers/google.d.ts.map +0 -1
- package/dist/providers/google.js +0 -399
- package/dist/providers/google.js.map +0 -1
- package/dist/providers/mistral.d.ts +0 -25
- package/dist/providers/mistral.d.ts.map +0 -1
- package/dist/providers/mistral.js +0 -534
- package/dist/providers/mistral.js.map +0 -1
- package/dist/providers/openai-codex-responses.d.ts +0 -11
- package/dist/providers/openai-codex-responses.d.ts.map +0 -1
- package/dist/providers/openai-codex-responses.js +0 -779
- package/dist/providers/openai-codex-responses.js.map +0 -1
- package/dist/providers/openai-completions.d.ts +0 -19
- package/dist/providers/openai-completions.d.ts.map +0 -1
- package/dist/providers/openai-completions.js +0 -909
- package/dist/providers/openai-completions.js.map +0 -1
- package/dist/providers/openai-responses-shared.d.ts +0 -18
- package/dist/providers/openai-responses-shared.d.ts.map +0 -1
- package/dist/providers/openai-responses-shared.js +0 -479
- package/dist/providers/openai-responses-shared.js.map +0 -1
- package/dist/providers/openai-responses.d.ts +0 -13
- package/dist/providers/openai-responses.d.ts.map +0 -1
- package/dist/providers/openai-responses.js +0 -206
- package/dist/providers/openai-responses.js.map +0 -1
- package/dist/providers/register-builtins.d.ts +0 -38
- package/dist/providers/register-builtins.d.ts.map +0 -1
- package/dist/providers/register-builtins.js +0 -261
- package/dist/providers/register-builtins.js.map +0 -1
- package/dist/providers/simple-options.d.ts +0 -8
- package/dist/providers/simple-options.d.ts.map +0 -1
- package/dist/providers/simple-options.js +0 -38
- package/dist/providers/simple-options.js.map +0 -1
- package/dist/providers/transform-messages.d.ts +0 -8
- package/dist/providers/transform-messages.d.ts.map +0 -1
- package/dist/providers/transform-messages.js +0 -184
- package/dist/providers/transform-messages.js.map +0 -1
- package/dist/stream.d.ts +0 -8
- package/dist/stream.d.ts.map +0 -1
- package/dist/stream.js +0 -27
- package/dist/stream.js.map +0 -1
- package/dist/types.d.ts +0 -397
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/utils/event-stream.d.ts +0 -21
- package/dist/utils/event-stream.d.ts.map +0 -1
- package/dist/utils/event-stream.js +0 -81
- package/dist/utils/event-stream.js.map +0 -1
- package/dist/utils/hash.d.ts +0 -3
- package/dist/utils/hash.d.ts.map +0 -1
- package/dist/utils/hash.js +0 -14
- package/dist/utils/hash.js.map +0 -1
- package/dist/utils/headers.d.ts +0 -2
- package/dist/utils/headers.d.ts.map +0 -1
- package/dist/utils/headers.js +0 -8
- package/dist/utils/headers.js.map +0 -1
- package/dist/utils/json-parse.d.ts +0 -16
- package/dist/utils/json-parse.d.ts.map +0 -1
- package/dist/utils/json-parse.js +0 -113
- package/dist/utils/json-parse.js.map +0 -1
- package/dist/utils/oauth/anthropic.d.ts +0 -25
- package/dist/utils/oauth/anthropic.d.ts.map +0 -1
- package/dist/utils/oauth/anthropic.js +0 -335
- package/dist/utils/oauth/anthropic.js.map +0 -1
- package/dist/utils/oauth/github-copilot.d.ts +0 -30
- package/dist/utils/oauth/github-copilot.d.ts.map +0 -1
- package/dist/utils/oauth/github-copilot.js +0 -292
- package/dist/utils/oauth/github-copilot.js.map +0 -1
- package/dist/utils/oauth/google-antigravity.d.ts +0 -26
- package/dist/utils/oauth/google-antigravity.d.ts.map +0 -1
- package/dist/utils/oauth/google-antigravity.js +0 -377
- package/dist/utils/oauth/google-antigravity.js.map +0 -1
- package/dist/utils/oauth/google-gemini-cli.d.ts +0 -26
- package/dist/utils/oauth/google-gemini-cli.d.ts.map +0 -1
- package/dist/utils/oauth/google-gemini-cli.js +0 -482
- package/dist/utils/oauth/google-gemini-cli.js.map +0 -1
- package/dist/utils/oauth/index.d.ts +0 -61
- package/dist/utils/oauth/index.d.ts.map +0 -1
- package/dist/utils/oauth/index.js +0 -131
- package/dist/utils/oauth/index.js.map +0 -1
- package/dist/utils/oauth/oauth-page.d.ts +0 -3
- package/dist/utils/oauth/oauth-page.d.ts.map +0 -1
- package/dist/utils/oauth/oauth-page.js +0 -105
- package/dist/utils/oauth/oauth-page.js.map +0 -1
- package/dist/utils/oauth/openai-codex.d.ts +0 -34
- package/dist/utils/oauth/openai-codex.d.ts.map +0 -1
- package/dist/utils/oauth/openai-codex.js +0 -374
- package/dist/utils/oauth/openai-codex.js.map +0 -1
- package/dist/utils/oauth/pkce.d.ts +0 -13
- package/dist/utils/oauth/pkce.d.ts.map +0 -1
- package/dist/utils/oauth/pkce.js +0 -31
- package/dist/utils/oauth/pkce.js.map +0 -1
- package/dist/utils/oauth/types.d.ts +0 -47
- package/dist/utils/oauth/types.d.ts.map +0 -1
- package/dist/utils/oauth/types.js +0 -2
- package/dist/utils/oauth/types.js.map +0 -1
- package/dist/utils/overflow.d.ts +0 -53
- package/dist/utils/overflow.d.ts.map +0 -1
- package/dist/utils/overflow.js +0 -132
- package/dist/utils/overflow.js.map +0 -1
- package/dist/utils/sanitize-unicode.d.ts +0 -22
- package/dist/utils/sanitize-unicode.d.ts.map +0 -1
- package/dist/utils/sanitize-unicode.js +0 -26
- package/dist/utils/sanitize-unicode.js.map +0 -1
- package/dist/utils/typebox-helpers.d.ts +0 -17
- package/dist/utils/typebox-helpers.d.ts.map +0 -1
- package/dist/utils/typebox-helpers.js +0 -21
- package/dist/utils/typebox-helpers.js.map +0 -1
- package/dist/utils/validation.d.ts +0 -18
- package/dist/utils/validation.d.ts.map +0 -1
- package/dist/utils/validation.js +0 -281
- package/dist/utils/validation.js.map +0 -1
|
@@ -1,909 +0,0 @@
|
|
|
1
|
-
import OpenAI from "openai";
|
|
2
|
-
import { getEnvApiKey } from "../env-api-keys.js";
|
|
3
|
-
import { calculateCost, supportsXhigh } from "../models.js";
|
|
4
|
-
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
5
|
-
import { headersToRecord } from "../utils/headers.js";
|
|
6
|
-
import { parseStreamingJson } from "../utils/json-parse.js";
|
|
7
|
-
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
8
|
-
import { isCloudflareProvider, resolveCloudflareBaseUrl } from "./cloudflare.js";
|
|
9
|
-
import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
|
|
10
|
-
import { buildBaseOptions, clampReasoning } from "./simple-options.js";
|
|
11
|
-
import { transformMessages } from "./transform-messages.js";
|
|
12
|
-
/**
|
|
13
|
-
* Check if conversation messages contain tool calls or tool results.
|
|
14
|
-
* This is needed because Anthropic (via proxy) requires the tools param
|
|
15
|
-
* to be present when messages include tool_calls or tool role messages.
|
|
16
|
-
*/
|
|
17
|
-
function hasToolHistory(messages) {
|
|
18
|
-
for (const msg of messages) {
|
|
19
|
-
if (msg.role === "toolResult") {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
if (msg.role === "assistant") {
|
|
23
|
-
if (msg.content.some((block) => block.type === "toolCall")) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
function isTextContentBlock(block) {
|
|
31
|
-
return block.type === "text";
|
|
32
|
-
}
|
|
33
|
-
function isThinkingContentBlock(block) {
|
|
34
|
-
return block.type === "thinking";
|
|
35
|
-
}
|
|
36
|
-
function isToolCallBlock(block) {
|
|
37
|
-
return block.type === "toolCall";
|
|
38
|
-
}
|
|
39
|
-
function isImageContentBlock(block) {
|
|
40
|
-
return block.type === "image";
|
|
41
|
-
}
|
|
42
|
-
function resolveCacheRetention(cacheRetention) {
|
|
43
|
-
if (cacheRetention) {
|
|
44
|
-
return cacheRetention;
|
|
45
|
-
}
|
|
46
|
-
if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
|
|
47
|
-
return "long";
|
|
48
|
-
}
|
|
49
|
-
return "short";
|
|
50
|
-
}
|
|
51
|
-
export const streamOpenAICompletions = (model, context, options) => {
|
|
52
|
-
const stream = new AssistantMessageEventStream();
|
|
53
|
-
(async () => {
|
|
54
|
-
const output = {
|
|
55
|
-
role: "assistant",
|
|
56
|
-
content: [],
|
|
57
|
-
api: model.api,
|
|
58
|
-
provider: model.provider,
|
|
59
|
-
model: model.id,
|
|
60
|
-
usage: {
|
|
61
|
-
input: 0,
|
|
62
|
-
output: 0,
|
|
63
|
-
cacheRead: 0,
|
|
64
|
-
cacheWrite: 0,
|
|
65
|
-
totalTokens: 0,
|
|
66
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
67
|
-
},
|
|
68
|
-
stopReason: "stop",
|
|
69
|
-
timestamp: Date.now(),
|
|
70
|
-
};
|
|
71
|
-
try {
|
|
72
|
-
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
|
|
73
|
-
const compat = getCompat(model);
|
|
74
|
-
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
75
|
-
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
|
|
76
|
-
const client = createClient(model, context, apiKey, options?.headers, cacheSessionId, compat);
|
|
77
|
-
let params = buildParams(model, context, options, compat, cacheRetention);
|
|
78
|
-
const nextParams = await options?.onPayload?.(params, model);
|
|
79
|
-
if (nextParams !== undefined) {
|
|
80
|
-
params = nextParams;
|
|
81
|
-
}
|
|
82
|
-
const requestOptions = {
|
|
83
|
-
...(options?.signal ? { signal: options.signal } : {}),
|
|
84
|
-
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
|
|
85
|
-
...(options?.maxRetries !== undefined ? { maxRetries: options.maxRetries } : {}),
|
|
86
|
-
};
|
|
87
|
-
const { data: openaiStream, response } = await client.chat.completions
|
|
88
|
-
.create(params, requestOptions)
|
|
89
|
-
.withResponse();
|
|
90
|
-
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
91
|
-
stream.push({ type: "start", partial: output });
|
|
92
|
-
let currentBlock = null;
|
|
93
|
-
const blocks = output.content;
|
|
94
|
-
const getContentIndex = (block) => (block ? blocks.indexOf(block) : -1);
|
|
95
|
-
const currentContentIndex = () => getContentIndex(currentBlock);
|
|
96
|
-
const finishCurrentBlock = (block) => {
|
|
97
|
-
if (block) {
|
|
98
|
-
const contentIndex = getContentIndex(block);
|
|
99
|
-
if (contentIndex === -1) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (block.type === "text") {
|
|
103
|
-
stream.push({
|
|
104
|
-
type: "text_end",
|
|
105
|
-
contentIndex,
|
|
106
|
-
content: block.text,
|
|
107
|
-
partial: output,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
else if (block.type === "thinking") {
|
|
111
|
-
stream.push({
|
|
112
|
-
type: "thinking_end",
|
|
113
|
-
contentIndex,
|
|
114
|
-
content: block.thinking,
|
|
115
|
-
partial: output,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
else if (block.type === "toolCall") {
|
|
119
|
-
block.arguments = parseStreamingJson(block.partialArgs);
|
|
120
|
-
// Finalize in-place and strip the scratch buffers so replay only
|
|
121
|
-
// carries parsed arguments.
|
|
122
|
-
delete block.partialArgs;
|
|
123
|
-
delete block.streamIndex;
|
|
124
|
-
stream.push({
|
|
125
|
-
type: "toolcall_end",
|
|
126
|
-
contentIndex,
|
|
127
|
-
toolCall: block,
|
|
128
|
-
partial: output,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
for await (const chunk of openaiStream) {
|
|
134
|
-
if (!chunk || typeof chunk !== "object")
|
|
135
|
-
continue;
|
|
136
|
-
// OpenAI documents ChatCompletionChunk.id as the unique chat completion identifier,
|
|
137
|
-
// and each chunk in a streamed completion carries the same id.
|
|
138
|
-
output.responseId ||= chunk.id;
|
|
139
|
-
if (chunk.usage) {
|
|
140
|
-
output.usage = parseChunkUsage(chunk.usage, model);
|
|
141
|
-
}
|
|
142
|
-
const choice = Array.isArray(chunk.choices) ? chunk.choices[0] : undefined;
|
|
143
|
-
if (!choice)
|
|
144
|
-
continue;
|
|
145
|
-
// Fallback: some providers (e.g., Moonshot) return usage
|
|
146
|
-
// in choice.usage instead of the standard chunk.usage
|
|
147
|
-
if (!chunk.usage && choice.usage) {
|
|
148
|
-
output.usage = parseChunkUsage(choice.usage, model);
|
|
149
|
-
}
|
|
150
|
-
if (choice.finish_reason) {
|
|
151
|
-
const finishReasonResult = mapStopReason(choice.finish_reason);
|
|
152
|
-
output.stopReason = finishReasonResult.stopReason;
|
|
153
|
-
if (finishReasonResult.errorMessage) {
|
|
154
|
-
output.errorMessage = finishReasonResult.errorMessage;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
if (choice.delta) {
|
|
158
|
-
if (choice.delta.content !== null &&
|
|
159
|
-
choice.delta.content !== undefined &&
|
|
160
|
-
choice.delta.content.length > 0) {
|
|
161
|
-
if (!currentBlock || currentBlock.type !== "text") {
|
|
162
|
-
finishCurrentBlock(currentBlock);
|
|
163
|
-
currentBlock = { type: "text", text: "" };
|
|
164
|
-
output.content.push(currentBlock);
|
|
165
|
-
stream.push({ type: "text_start", contentIndex: currentContentIndex(), partial: output });
|
|
166
|
-
}
|
|
167
|
-
if (currentBlock.type === "text") {
|
|
168
|
-
currentBlock.text += choice.delta.content;
|
|
169
|
-
stream.push({
|
|
170
|
-
type: "text_delta",
|
|
171
|
-
contentIndex: currentContentIndex(),
|
|
172
|
-
delta: choice.delta.content,
|
|
173
|
-
partial: output,
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
// Some endpoints return reasoning in reasoning_content (llama.cpp),
|
|
178
|
-
// or reasoning (other openai compatible endpoints)
|
|
179
|
-
// Use the first non-empty reasoning field to avoid duplication
|
|
180
|
-
// (e.g., chutes.ai returns both reasoning_content and reasoning with same content)
|
|
181
|
-
const reasoningFields = ["reasoning_content", "reasoning", "reasoning_text"];
|
|
182
|
-
let foundReasoningField = null;
|
|
183
|
-
for (const field of reasoningFields) {
|
|
184
|
-
if (choice.delta[field] !== null &&
|
|
185
|
-
choice.delta[field] !== undefined &&
|
|
186
|
-
choice.delta[field].length > 0) {
|
|
187
|
-
if (!foundReasoningField) {
|
|
188
|
-
foundReasoningField = field;
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
if (foundReasoningField) {
|
|
194
|
-
if (!currentBlock || currentBlock.type !== "thinking") {
|
|
195
|
-
finishCurrentBlock(currentBlock);
|
|
196
|
-
currentBlock = {
|
|
197
|
-
type: "thinking",
|
|
198
|
-
thinking: "",
|
|
199
|
-
thinkingSignature: foundReasoningField,
|
|
200
|
-
};
|
|
201
|
-
output.content.push(currentBlock);
|
|
202
|
-
stream.push({ type: "thinking_start", contentIndex: currentContentIndex(), partial: output });
|
|
203
|
-
}
|
|
204
|
-
if (currentBlock.type === "thinking") {
|
|
205
|
-
const delta = choice.delta[foundReasoningField];
|
|
206
|
-
currentBlock.thinking += delta;
|
|
207
|
-
stream.push({
|
|
208
|
-
type: "thinking_delta",
|
|
209
|
-
contentIndex: currentContentIndex(),
|
|
210
|
-
delta,
|
|
211
|
-
partial: output,
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
if (choice?.delta?.tool_calls) {
|
|
216
|
-
for (const toolCall of choice.delta.tool_calls) {
|
|
217
|
-
const streamIndex = typeof toolCall.index === "number" ? toolCall.index : undefined;
|
|
218
|
-
const sameToolCall = currentBlock?.type === "toolCall" &&
|
|
219
|
-
((streamIndex !== undefined && currentBlock.streamIndex === streamIndex) ||
|
|
220
|
-
(streamIndex === undefined && toolCall.id && currentBlock.id === toolCall.id));
|
|
221
|
-
if (!sameToolCall) {
|
|
222
|
-
finishCurrentBlock(currentBlock);
|
|
223
|
-
currentBlock = {
|
|
224
|
-
type: "toolCall",
|
|
225
|
-
id: toolCall.id || "",
|
|
226
|
-
name: toolCall.function?.name || "",
|
|
227
|
-
arguments: {},
|
|
228
|
-
partialArgs: "",
|
|
229
|
-
streamIndex,
|
|
230
|
-
};
|
|
231
|
-
output.content.push(currentBlock);
|
|
232
|
-
stream.push({
|
|
233
|
-
type: "toolcall_start",
|
|
234
|
-
contentIndex: getContentIndex(currentBlock),
|
|
235
|
-
partial: output,
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
const currentToolCallBlock = currentBlock?.type === "toolCall" ? currentBlock : null;
|
|
239
|
-
if (currentToolCallBlock) {
|
|
240
|
-
if (!currentToolCallBlock.id && toolCall.id)
|
|
241
|
-
currentToolCallBlock.id = toolCall.id;
|
|
242
|
-
if (!currentToolCallBlock.name && toolCall.function?.name) {
|
|
243
|
-
currentToolCallBlock.name = toolCall.function.name;
|
|
244
|
-
}
|
|
245
|
-
if (currentToolCallBlock.streamIndex === undefined && streamIndex !== undefined) {
|
|
246
|
-
currentToolCallBlock.streamIndex = streamIndex;
|
|
247
|
-
}
|
|
248
|
-
let delta = "";
|
|
249
|
-
if (toolCall.function?.arguments) {
|
|
250
|
-
delta = toolCall.function.arguments;
|
|
251
|
-
currentToolCallBlock.partialArgs += toolCall.function.arguments;
|
|
252
|
-
currentToolCallBlock.arguments = parseStreamingJson(currentToolCallBlock.partialArgs);
|
|
253
|
-
}
|
|
254
|
-
stream.push({
|
|
255
|
-
type: "toolcall_delta",
|
|
256
|
-
contentIndex: getContentIndex(currentToolCallBlock),
|
|
257
|
-
delta,
|
|
258
|
-
partial: output,
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
const reasoningDetails = choice.delta.reasoning_details;
|
|
264
|
-
if (reasoningDetails && Array.isArray(reasoningDetails)) {
|
|
265
|
-
for (const detail of reasoningDetails) {
|
|
266
|
-
if (detail.type === "reasoning.encrypted" && detail.id && detail.data) {
|
|
267
|
-
const matchingToolCall = output.content.find((b) => b.type === "toolCall" && b.id === detail.id);
|
|
268
|
-
if (matchingToolCall) {
|
|
269
|
-
matchingToolCall.thoughtSignature = JSON.stringify(detail);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
finishCurrentBlock(currentBlock);
|
|
277
|
-
if (options?.signal?.aborted) {
|
|
278
|
-
throw new Error("Request was aborted");
|
|
279
|
-
}
|
|
280
|
-
if (output.stopReason === "aborted") {
|
|
281
|
-
throw new Error("Request was aborted");
|
|
282
|
-
}
|
|
283
|
-
if (output.stopReason === "error") {
|
|
284
|
-
throw new Error(output.errorMessage || "Provider returned an error stop reason");
|
|
285
|
-
}
|
|
286
|
-
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
287
|
-
stream.end();
|
|
288
|
-
}
|
|
289
|
-
catch (error) {
|
|
290
|
-
for (const block of output.content) {
|
|
291
|
-
delete block.index;
|
|
292
|
-
// Streaming scratch buffers are only used during parsing; never persist them.
|
|
293
|
-
delete block.partialArgs;
|
|
294
|
-
delete block.streamIndex;
|
|
295
|
-
}
|
|
296
|
-
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
297
|
-
output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
|
|
298
|
-
// Some providers via OpenRouter give additional information in this field.
|
|
299
|
-
const rawMetadata = error?.error?.metadata?.raw;
|
|
300
|
-
if (rawMetadata)
|
|
301
|
-
output.errorMessage += `\n${rawMetadata}`;
|
|
302
|
-
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
303
|
-
stream.end();
|
|
304
|
-
}
|
|
305
|
-
})();
|
|
306
|
-
return stream;
|
|
307
|
-
};
|
|
308
|
-
export const streamSimpleOpenAICompletions = (model, context, options) => {
|
|
309
|
-
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
310
|
-
if (!apiKey) {
|
|
311
|
-
throw new Error(`No API key for provider: ${model.provider}`);
|
|
312
|
-
}
|
|
313
|
-
const base = buildBaseOptions(model, options, apiKey);
|
|
314
|
-
const reasoningEffort = supportsXhigh(model) ? options?.reasoning : clampReasoning(options?.reasoning);
|
|
315
|
-
const toolChoice = options?.toolChoice;
|
|
316
|
-
return streamOpenAICompletions(model, context, {
|
|
317
|
-
...base,
|
|
318
|
-
reasoningEffort,
|
|
319
|
-
toolChoice,
|
|
320
|
-
});
|
|
321
|
-
};
|
|
322
|
-
function createClient(model, context, apiKey, optionsHeaders, sessionId, compat = getCompat(model)) {
|
|
323
|
-
if (!apiKey) {
|
|
324
|
-
if (!process.env.OPENAI_API_KEY) {
|
|
325
|
-
throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");
|
|
326
|
-
}
|
|
327
|
-
apiKey = process.env.OPENAI_API_KEY;
|
|
328
|
-
}
|
|
329
|
-
const headers = { ...model.headers };
|
|
330
|
-
if (model.provider === "github-copilot") {
|
|
331
|
-
const hasImages = hasCopilotVisionInput(context.messages);
|
|
332
|
-
const copilotHeaders = buildCopilotDynamicHeaders({
|
|
333
|
-
messages: context.messages,
|
|
334
|
-
hasImages,
|
|
335
|
-
});
|
|
336
|
-
Object.assign(headers, copilotHeaders);
|
|
337
|
-
}
|
|
338
|
-
if (sessionId && compat.sendSessionAffinityHeaders) {
|
|
339
|
-
headers.session_id = sessionId;
|
|
340
|
-
headers["x-client-request-id"] = sessionId;
|
|
341
|
-
headers["x-session-affinity"] = sessionId;
|
|
342
|
-
}
|
|
343
|
-
// Merge options headers last so they can override defaults
|
|
344
|
-
if (optionsHeaders) {
|
|
345
|
-
Object.assign(headers, optionsHeaders);
|
|
346
|
-
}
|
|
347
|
-
return new OpenAI({
|
|
348
|
-
apiKey,
|
|
349
|
-
baseURL: isCloudflareProvider(model.provider) ? resolveCloudflareBaseUrl(model) : model.baseUrl,
|
|
350
|
-
dangerouslyAllowBrowser: true,
|
|
351
|
-
defaultHeaders: headers,
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
function buildParams(model, context, options, compat = getCompat(model), cacheRetention = resolveCacheRetention(options?.cacheRetention)) {
|
|
355
|
-
const messages = convertMessages(model, context, compat);
|
|
356
|
-
const cacheControl = getCompatCacheControl(compat, cacheRetention);
|
|
357
|
-
const params = {
|
|
358
|
-
model: model.id,
|
|
359
|
-
messages,
|
|
360
|
-
stream: true,
|
|
361
|
-
prompt_cache_key: (model.baseUrl.includes("api.openai.com") && cacheRetention !== "none") ||
|
|
362
|
-
(cacheRetention === "long" && compat.supportsLongCacheRetention)
|
|
363
|
-
? options?.sessionId
|
|
364
|
-
: undefined,
|
|
365
|
-
prompt_cache_retention: cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : undefined,
|
|
366
|
-
};
|
|
367
|
-
if (compat.supportsUsageInStreaming !== false) {
|
|
368
|
-
params.stream_options = { include_usage: true };
|
|
369
|
-
}
|
|
370
|
-
if (compat.supportsStore) {
|
|
371
|
-
params.store = false;
|
|
372
|
-
}
|
|
373
|
-
if (options?.maxTokens) {
|
|
374
|
-
if (compat.maxTokensField === "max_tokens") {
|
|
375
|
-
params.max_tokens = options.maxTokens;
|
|
376
|
-
}
|
|
377
|
-
else {
|
|
378
|
-
params.max_completion_tokens = options.maxTokens;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
if (options?.temperature !== undefined) {
|
|
382
|
-
params.temperature = options.temperature;
|
|
383
|
-
}
|
|
384
|
-
if (context.tools && context.tools.length > 0) {
|
|
385
|
-
params.tools = convertTools(context.tools, compat);
|
|
386
|
-
if (compat.zaiToolStream) {
|
|
387
|
-
params.tool_stream = true;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
else if (hasToolHistory(context.messages)) {
|
|
391
|
-
// Anthropic (via LiteLLM/proxy) requires tools param when conversation has tool_calls/tool_results
|
|
392
|
-
params.tools = [];
|
|
393
|
-
}
|
|
394
|
-
if (cacheControl) {
|
|
395
|
-
applyAnthropicCacheControl(messages, params.tools, cacheControl);
|
|
396
|
-
}
|
|
397
|
-
if (options?.toolChoice) {
|
|
398
|
-
params.tool_choice = options.toolChoice;
|
|
399
|
-
}
|
|
400
|
-
if (compat.thinkingFormat === "zai" && model.reasoning) {
|
|
401
|
-
params.enable_thinking = !!options?.reasoningEffort;
|
|
402
|
-
}
|
|
403
|
-
else if (compat.thinkingFormat === "qwen" && model.reasoning) {
|
|
404
|
-
params.enable_thinking = !!options?.reasoningEffort;
|
|
405
|
-
}
|
|
406
|
-
else if (compat.thinkingFormat === "qwen-chat-template" && model.reasoning) {
|
|
407
|
-
params.chat_template_kwargs = {
|
|
408
|
-
enable_thinking: !!options?.reasoningEffort,
|
|
409
|
-
preserve_thinking: true,
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
else if (compat.thinkingFormat === "deepseek" && model.reasoning) {
|
|
413
|
-
params.thinking = { type: options?.reasoningEffort ? "enabled" : "disabled" };
|
|
414
|
-
if (options?.reasoningEffort) {
|
|
415
|
-
params.reasoning_effort = mapReasoningEffort(options.reasoningEffort, compat.reasoningEffortMap);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
else if (compat.thinkingFormat === "openrouter" && model.reasoning) {
|
|
419
|
-
// OpenRouter normalizes reasoning across providers via a nested reasoning object.
|
|
420
|
-
const openRouterParams = params;
|
|
421
|
-
if (options?.reasoningEffort) {
|
|
422
|
-
openRouterParams.reasoning = {
|
|
423
|
-
effort: mapReasoningEffort(options.reasoningEffort, compat.reasoningEffortMap),
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
else {
|
|
427
|
-
openRouterParams.reasoning = { effort: "none" };
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
else if (options?.reasoningEffort && model.reasoning && compat.supportsReasoningEffort) {
|
|
431
|
-
// OpenAI-style reasoning_effort
|
|
432
|
-
params.reasoning_effort = mapReasoningEffort(options.reasoningEffort, compat.reasoningEffortMap);
|
|
433
|
-
}
|
|
434
|
-
// OpenRouter provider routing preferences
|
|
435
|
-
if (model.baseUrl.includes("openrouter.ai") && model.compat?.openRouterRouting) {
|
|
436
|
-
params.provider = model.compat.openRouterRouting;
|
|
437
|
-
}
|
|
438
|
-
// Vercel AI Gateway provider routing preferences
|
|
439
|
-
if (model.baseUrl.includes("ai-gateway.vercel.sh") && model.compat?.vercelGatewayRouting) {
|
|
440
|
-
const routing = model.compat.vercelGatewayRouting;
|
|
441
|
-
if (routing.only || routing.order) {
|
|
442
|
-
const gatewayOptions = {};
|
|
443
|
-
if (routing.only)
|
|
444
|
-
gatewayOptions.only = routing.only;
|
|
445
|
-
if (routing.order)
|
|
446
|
-
gatewayOptions.order = routing.order;
|
|
447
|
-
params.providerOptions = { gateway: gatewayOptions };
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
return params;
|
|
451
|
-
}
|
|
452
|
-
function mapReasoningEffort(effort, reasoningEffortMap) {
|
|
453
|
-
return reasoningEffortMap[effort] ?? effort;
|
|
454
|
-
}
|
|
455
|
-
function getCompatCacheControl(compat, cacheRetention) {
|
|
456
|
-
if (compat.cacheControlFormat !== "anthropic" || cacheRetention === "none") {
|
|
457
|
-
return undefined;
|
|
458
|
-
}
|
|
459
|
-
const ttl = cacheRetention === "long" && compat.supportsLongCacheRetention ? "1h" : undefined;
|
|
460
|
-
return { type: "ephemeral", ...(ttl ? { ttl } : {}) };
|
|
461
|
-
}
|
|
462
|
-
function applyAnthropicCacheControl(messages, tools, cacheControl) {
|
|
463
|
-
addCacheControlToSystemPrompt(messages, cacheControl);
|
|
464
|
-
addCacheControlToLastTool(tools, cacheControl);
|
|
465
|
-
addCacheControlToLastConversationMessage(messages, cacheControl);
|
|
466
|
-
}
|
|
467
|
-
function addCacheControlToSystemPrompt(messages, cacheControl) {
|
|
468
|
-
for (const message of messages) {
|
|
469
|
-
if (message.role === "system" || message.role === "developer") {
|
|
470
|
-
addCacheControlToInstructionMessage(message, cacheControl);
|
|
471
|
-
return;
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
function addCacheControlToLastConversationMessage(messages, cacheControl) {
|
|
476
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
477
|
-
const message = messages[i];
|
|
478
|
-
if (message.role === "user" || message.role === "assistant") {
|
|
479
|
-
if (addCacheControlToMessage(message, cacheControl)) {
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
function addCacheControlToLastTool(tools, cacheControl) {
|
|
486
|
-
if (!tools || tools.length === 0) {
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
const lastTool = tools[tools.length - 1];
|
|
490
|
-
lastTool.cache_control = cacheControl;
|
|
491
|
-
}
|
|
492
|
-
function addCacheControlToInstructionMessage(message, cacheControl) {
|
|
493
|
-
return addCacheControlToTextContent(message, cacheControl);
|
|
494
|
-
}
|
|
495
|
-
function addCacheControlToMessage(message, cacheControl) {
|
|
496
|
-
if (message.role === "user" || message.role === "assistant") {
|
|
497
|
-
return addCacheControlToTextContent(message, cacheControl);
|
|
498
|
-
}
|
|
499
|
-
return false;
|
|
500
|
-
}
|
|
501
|
-
function addCacheControlToTextContent(message, cacheControl) {
|
|
502
|
-
const content = message.content;
|
|
503
|
-
if (typeof content === "string") {
|
|
504
|
-
if (content.length === 0) {
|
|
505
|
-
return false;
|
|
506
|
-
}
|
|
507
|
-
message.content = [
|
|
508
|
-
{
|
|
509
|
-
type: "text",
|
|
510
|
-
text: content,
|
|
511
|
-
cache_control: cacheControl,
|
|
512
|
-
},
|
|
513
|
-
];
|
|
514
|
-
return true;
|
|
515
|
-
}
|
|
516
|
-
if (!Array.isArray(content)) {
|
|
517
|
-
return false;
|
|
518
|
-
}
|
|
519
|
-
for (let i = content.length - 1; i >= 0; i--) {
|
|
520
|
-
const part = content[i];
|
|
521
|
-
if (part?.type === "text") {
|
|
522
|
-
const textPart = part;
|
|
523
|
-
textPart.cache_control = cacheControl;
|
|
524
|
-
return true;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
return false;
|
|
528
|
-
}
|
|
529
|
-
export function convertMessages(model, context, compat) {
|
|
530
|
-
const params = [];
|
|
531
|
-
const normalizeToolCallId = (id) => {
|
|
532
|
-
// Handle pipe-separated IDs from OpenAI Responses API
|
|
533
|
-
// Format: {call_id}|{id} where {id} can be 400+ chars with special chars (+, /, =)
|
|
534
|
-
// These come from providers like github-copilot, openai-codex, opencode
|
|
535
|
-
// Extract just the call_id part and normalize it
|
|
536
|
-
if (id.includes("|")) {
|
|
537
|
-
const [callId] = id.split("|");
|
|
538
|
-
// Sanitize to allowed chars and truncate to 40 chars (OpenAI limit)
|
|
539
|
-
return callId.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 40);
|
|
540
|
-
}
|
|
541
|
-
if (model.provider === "openai")
|
|
542
|
-
return id.length > 40 ? id.slice(0, 40) : id;
|
|
543
|
-
return id;
|
|
544
|
-
};
|
|
545
|
-
const transformedMessages = transformMessages(context.messages, model, (id) => normalizeToolCallId(id));
|
|
546
|
-
if (context.systemPrompt) {
|
|
547
|
-
const useDeveloperRole = model.reasoning && compat.supportsDeveloperRole;
|
|
548
|
-
const role = useDeveloperRole ? "developer" : "system";
|
|
549
|
-
params.push({ role: role, content: sanitizeSurrogates(context.systemPrompt) });
|
|
550
|
-
}
|
|
551
|
-
let lastRole = null;
|
|
552
|
-
for (let i = 0; i < transformedMessages.length; i++) {
|
|
553
|
-
const msg = transformedMessages[i];
|
|
554
|
-
// Some providers don't allow user messages directly after tool results
|
|
555
|
-
// Insert a synthetic assistant message to bridge the gap
|
|
556
|
-
if (compat.requiresAssistantAfterToolResult && lastRole === "toolResult" && msg.role === "user") {
|
|
557
|
-
params.push({
|
|
558
|
-
role: "assistant",
|
|
559
|
-
content: "I have processed the tool results.",
|
|
560
|
-
});
|
|
561
|
-
}
|
|
562
|
-
if (msg.role === "user") {
|
|
563
|
-
if (typeof msg.content === "string") {
|
|
564
|
-
params.push({
|
|
565
|
-
role: "user",
|
|
566
|
-
content: sanitizeSurrogates(msg.content),
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
else {
|
|
570
|
-
const content = msg.content.map((item) => {
|
|
571
|
-
if (item.type === "text") {
|
|
572
|
-
return {
|
|
573
|
-
type: "text",
|
|
574
|
-
text: sanitizeSurrogates(item.text),
|
|
575
|
-
};
|
|
576
|
-
}
|
|
577
|
-
else {
|
|
578
|
-
return {
|
|
579
|
-
type: "image_url",
|
|
580
|
-
image_url: {
|
|
581
|
-
url: `data:${item.mimeType};base64,${item.data}`,
|
|
582
|
-
},
|
|
583
|
-
};
|
|
584
|
-
}
|
|
585
|
-
});
|
|
586
|
-
if (content.length === 0)
|
|
587
|
-
continue;
|
|
588
|
-
params.push({
|
|
589
|
-
role: "user",
|
|
590
|
-
content,
|
|
591
|
-
});
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
else if (msg.role === "assistant") {
|
|
595
|
-
// Some providers don't accept null content, use empty string instead
|
|
596
|
-
const assistantMsg = {
|
|
597
|
-
role: "assistant",
|
|
598
|
-
content: compat.requiresAssistantAfterToolResult ? "" : null,
|
|
599
|
-
};
|
|
600
|
-
const assistantTextParts = msg.content
|
|
601
|
-
.filter(isTextContentBlock)
|
|
602
|
-
.filter((block) => block.text.trim().length > 0)
|
|
603
|
-
.map((block) => ({
|
|
604
|
-
type: "text",
|
|
605
|
-
text: sanitizeSurrogates(block.text),
|
|
606
|
-
}));
|
|
607
|
-
const assistantText = assistantTextParts.map((part) => part.text).join("");
|
|
608
|
-
const nonEmptyThinkingBlocks = msg.content
|
|
609
|
-
.filter(isThinkingContentBlock)
|
|
610
|
-
.filter((block) => block.thinking.trim().length > 0);
|
|
611
|
-
if (nonEmptyThinkingBlocks.length > 0) {
|
|
612
|
-
if (compat.requiresThinkingAsText) {
|
|
613
|
-
// Convert thinking blocks to plain text (no tags to avoid model mimicking them)
|
|
614
|
-
const thinkingText = nonEmptyThinkingBlocks
|
|
615
|
-
.map((block) => sanitizeSurrogates(block.thinking))
|
|
616
|
-
.join("\n\n");
|
|
617
|
-
assistantMsg.content = [{ type: "text", text: thinkingText }, ...assistantTextParts];
|
|
618
|
-
}
|
|
619
|
-
else {
|
|
620
|
-
// Always send assistant content as a plain string (OpenAI Chat Completions
|
|
621
|
-
// API standard format). Sending as an array of {type:"text", text:"..."}
|
|
622
|
-
// objects is non-standard and causes some models (e.g. DeepSeek V3.2 via
|
|
623
|
-
// NVIDIA NIM) to mirror the content-block structure literally in their
|
|
624
|
-
// output, producing recursive nesting like [{'type':'text','text':'[{...}]'}].
|
|
625
|
-
if (assistantText.length > 0) {
|
|
626
|
-
assistantMsg.content = assistantText;
|
|
627
|
-
}
|
|
628
|
-
// Use the signature from the first thinking block if available (for llama.cpp server + gpt-oss)
|
|
629
|
-
const signature = nonEmptyThinkingBlocks[0].thinkingSignature;
|
|
630
|
-
if (signature && signature.length > 0) {
|
|
631
|
-
assistantMsg[signature] = nonEmptyThinkingBlocks.map((block) => block.thinking).join("\n");
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
else if (assistantText.length > 0) {
|
|
636
|
-
// Always send assistant content as a plain string (OpenAI Chat Completions
|
|
637
|
-
// API standard format). Sending as an array of {type:"text", text:"..."}
|
|
638
|
-
// objects is non-standard and causes some models (e.g. DeepSeek V3.2 via
|
|
639
|
-
// NVIDIA NIM) to mirror the content-block structure literally in their
|
|
640
|
-
// output, producing recursive nesting like [{'type':'text','text':'[{...}]'}].
|
|
641
|
-
assistantMsg.content = assistantText;
|
|
642
|
-
}
|
|
643
|
-
const toolCalls = msg.content.filter(isToolCallBlock);
|
|
644
|
-
if (toolCalls.length > 0) {
|
|
645
|
-
assistantMsg.tool_calls = toolCalls.map((tc) => ({
|
|
646
|
-
id: tc.id,
|
|
647
|
-
type: "function",
|
|
648
|
-
function: {
|
|
649
|
-
name: tc.name,
|
|
650
|
-
arguments: JSON.stringify(tc.arguments),
|
|
651
|
-
},
|
|
652
|
-
}));
|
|
653
|
-
const reasoningDetails = toolCalls
|
|
654
|
-
.filter((tc) => tc.thoughtSignature)
|
|
655
|
-
.map((tc) => {
|
|
656
|
-
try {
|
|
657
|
-
return JSON.parse(tc.thoughtSignature);
|
|
658
|
-
}
|
|
659
|
-
catch {
|
|
660
|
-
return null;
|
|
661
|
-
}
|
|
662
|
-
})
|
|
663
|
-
.filter(Boolean);
|
|
664
|
-
if (reasoningDetails.length > 0) {
|
|
665
|
-
assistantMsg.reasoning_details = reasoningDetails;
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
if (compat.requiresReasoningContentOnAssistantMessages &&
|
|
669
|
-
model.reasoning &&
|
|
670
|
-
assistantMsg.reasoning_content === undefined) {
|
|
671
|
-
assistantMsg.reasoning_content = "";
|
|
672
|
-
}
|
|
673
|
-
// Skip assistant messages that have no content and no tool calls.
|
|
674
|
-
// Some providers require "either content or tool_calls, but not none".
|
|
675
|
-
// Other providers also don't accept empty assistant messages.
|
|
676
|
-
// This handles aborted assistant responses that got no content.
|
|
677
|
-
const content = assistantMsg.content;
|
|
678
|
-
const hasContent = content !== null &&
|
|
679
|
-
content !== undefined &&
|
|
680
|
-
(typeof content === "string" ? content.length > 0 : content.length > 0);
|
|
681
|
-
if (!hasContent && !assistantMsg.tool_calls) {
|
|
682
|
-
continue;
|
|
683
|
-
}
|
|
684
|
-
params.push(assistantMsg);
|
|
685
|
-
}
|
|
686
|
-
else if (msg.role === "toolResult") {
|
|
687
|
-
const imageBlocks = [];
|
|
688
|
-
let j = i;
|
|
689
|
-
for (; j < transformedMessages.length && transformedMessages[j].role === "toolResult"; j++) {
|
|
690
|
-
const toolMsg = transformedMessages[j];
|
|
691
|
-
// Extract text and image content
|
|
692
|
-
const textResult = toolMsg.content
|
|
693
|
-
.filter(isTextContentBlock)
|
|
694
|
-
.map((block) => block.text)
|
|
695
|
-
.join("\n");
|
|
696
|
-
const hasImages = toolMsg.content.some((c) => c.type === "image");
|
|
697
|
-
// Always send tool result with text (or placeholder if only images)
|
|
698
|
-
const hasText = textResult.length > 0;
|
|
699
|
-
// Some providers require the 'name' field in tool results
|
|
700
|
-
const toolResultMsg = {
|
|
701
|
-
role: "tool",
|
|
702
|
-
content: sanitizeSurrogates(hasText ? textResult : "(see attached image)"),
|
|
703
|
-
tool_call_id: toolMsg.toolCallId,
|
|
704
|
-
};
|
|
705
|
-
if (compat.requiresToolResultName && toolMsg.toolName) {
|
|
706
|
-
toolResultMsg.name = toolMsg.toolName;
|
|
707
|
-
}
|
|
708
|
-
params.push(toolResultMsg);
|
|
709
|
-
if (hasImages && model.input.includes("image")) {
|
|
710
|
-
for (const block of toolMsg.content) {
|
|
711
|
-
if (isImageContentBlock(block)) {
|
|
712
|
-
imageBlocks.push({
|
|
713
|
-
type: "image_url",
|
|
714
|
-
image_url: {
|
|
715
|
-
url: `data:${block.mimeType};base64,${block.data}`,
|
|
716
|
-
},
|
|
717
|
-
});
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
i = j - 1;
|
|
723
|
-
if (imageBlocks.length > 0) {
|
|
724
|
-
if (compat.requiresAssistantAfterToolResult) {
|
|
725
|
-
params.push({
|
|
726
|
-
role: "assistant",
|
|
727
|
-
content: "I have processed the tool results.",
|
|
728
|
-
});
|
|
729
|
-
}
|
|
730
|
-
params.push({
|
|
731
|
-
role: "user",
|
|
732
|
-
content: [
|
|
733
|
-
{
|
|
734
|
-
type: "text",
|
|
735
|
-
text: "Attached image(s) from tool result:",
|
|
736
|
-
},
|
|
737
|
-
...imageBlocks,
|
|
738
|
-
],
|
|
739
|
-
});
|
|
740
|
-
lastRole = "user";
|
|
741
|
-
}
|
|
742
|
-
else {
|
|
743
|
-
lastRole = "toolResult";
|
|
744
|
-
}
|
|
745
|
-
continue;
|
|
746
|
-
}
|
|
747
|
-
lastRole = msg.role;
|
|
748
|
-
}
|
|
749
|
-
return params;
|
|
750
|
-
}
|
|
751
|
-
function convertTools(tools, compat) {
|
|
752
|
-
return tools.map((tool) => ({
|
|
753
|
-
type: "function",
|
|
754
|
-
function: {
|
|
755
|
-
name: tool.name,
|
|
756
|
-
description: tool.description,
|
|
757
|
-
parameters: tool.parameters, // TypeBox already generates JSON Schema
|
|
758
|
-
// Only include strict if provider supports it. Some reject unknown fields.
|
|
759
|
-
...(compat.supportsStrictMode !== false && { strict: false }),
|
|
760
|
-
},
|
|
761
|
-
}));
|
|
762
|
-
}
|
|
763
|
-
function parseChunkUsage(rawUsage, model) {
|
|
764
|
-
const promptTokens = rawUsage.prompt_tokens || 0;
|
|
765
|
-
const reportedCachedTokens = rawUsage.prompt_tokens_details?.cached_tokens || 0;
|
|
766
|
-
const cacheWriteTokens = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;
|
|
767
|
-
// Normalize to pi-ai semantics:
|
|
768
|
-
// - cacheRead: hits from cache created by previous requests only
|
|
769
|
-
// - cacheWrite: tokens written to cache in this request
|
|
770
|
-
// Some OpenAI-compatible providers (observed on OpenRouter) report cached_tokens
|
|
771
|
-
// as (previous hits + current writes). In that case, remove cacheWrite from cacheRead.
|
|
772
|
-
const cacheReadTokens = cacheWriteTokens > 0 ? Math.max(0, reportedCachedTokens - cacheWriteTokens) : reportedCachedTokens;
|
|
773
|
-
const input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);
|
|
774
|
-
// OpenAI completion_tokens already includes reasoning_tokens.
|
|
775
|
-
const outputTokens = rawUsage.completion_tokens || 0;
|
|
776
|
-
const usage = {
|
|
777
|
-
input,
|
|
778
|
-
output: outputTokens,
|
|
779
|
-
cacheRead: cacheReadTokens,
|
|
780
|
-
cacheWrite: cacheWriteTokens,
|
|
781
|
-
totalTokens: input + outputTokens + cacheReadTokens + cacheWriteTokens,
|
|
782
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
783
|
-
};
|
|
784
|
-
calculateCost(model, usage);
|
|
785
|
-
return usage;
|
|
786
|
-
}
|
|
787
|
-
function mapStopReason(reason) {
|
|
788
|
-
if (reason === null)
|
|
789
|
-
return { stopReason: "stop" };
|
|
790
|
-
switch (reason) {
|
|
791
|
-
case "stop":
|
|
792
|
-
case "end":
|
|
793
|
-
return { stopReason: "stop" };
|
|
794
|
-
case "length":
|
|
795
|
-
return { stopReason: "length" };
|
|
796
|
-
case "function_call":
|
|
797
|
-
case "tool_calls":
|
|
798
|
-
return { stopReason: "toolUse" };
|
|
799
|
-
case "content_filter":
|
|
800
|
-
return { stopReason: "error", errorMessage: "Provider finish_reason: content_filter" };
|
|
801
|
-
case "network_error":
|
|
802
|
-
return { stopReason: "error", errorMessage: "Provider finish_reason: network_error" };
|
|
803
|
-
default:
|
|
804
|
-
return {
|
|
805
|
-
stopReason: "error",
|
|
806
|
-
errorMessage: `Provider finish_reason: ${reason}`,
|
|
807
|
-
};
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
/**
|
|
811
|
-
* Detect compatibility settings from provider and baseUrl for known providers.
|
|
812
|
-
* Provider takes precedence over URL-based detection since it's explicitly configured.
|
|
813
|
-
* Returns a fully resolved OpenAICompletionsCompat object with all fields set.
|
|
814
|
-
*/
|
|
815
|
-
function detectCompat(model) {
|
|
816
|
-
const provider = model.provider;
|
|
817
|
-
const baseUrl = model.baseUrl;
|
|
818
|
-
const isZai = provider === "zai" || baseUrl.includes("api.z.ai");
|
|
819
|
-
const isCloudflareWorkersAI = provider === "cloudflare-workers-ai" || baseUrl.includes("api.cloudflare.com");
|
|
820
|
-
const isNonStandard = provider === "cerebras" ||
|
|
821
|
-
baseUrl.includes("cerebras.ai") ||
|
|
822
|
-
provider === "xai" ||
|
|
823
|
-
baseUrl.includes("api.x.ai") ||
|
|
824
|
-
baseUrl.includes("chutes.ai") ||
|
|
825
|
-
baseUrl.includes("deepseek.com") ||
|
|
826
|
-
isZai ||
|
|
827
|
-
provider === "opencode" ||
|
|
828
|
-
baseUrl.includes("opencode.ai") ||
|
|
829
|
-
isCloudflareWorkersAI;
|
|
830
|
-
const useMaxTokens = baseUrl.includes("chutes.ai");
|
|
831
|
-
const isGrok = provider === "xai" || baseUrl.includes("api.x.ai");
|
|
832
|
-
const isGroq = provider === "groq" || baseUrl.includes("groq.com");
|
|
833
|
-
const isDeepSeek = provider === "deepseek" || baseUrl.includes("deepseek.com");
|
|
834
|
-
const cacheControlFormat = provider === "openrouter" && model.id.startsWith("anthropic/") ? "anthropic" : undefined;
|
|
835
|
-
const reasoningEffortMap = isDeepSeek
|
|
836
|
-
? {
|
|
837
|
-
minimal: "high",
|
|
838
|
-
low: "high",
|
|
839
|
-
medium: "high",
|
|
840
|
-
high: "high",
|
|
841
|
-
xhigh: "max",
|
|
842
|
-
}
|
|
843
|
-
: isGroq && model.id === "qwen/qwen3-32b"
|
|
844
|
-
? {
|
|
845
|
-
minimal: "default",
|
|
846
|
-
low: "default",
|
|
847
|
-
medium: "default",
|
|
848
|
-
high: "default",
|
|
849
|
-
xhigh: "default",
|
|
850
|
-
}
|
|
851
|
-
: {};
|
|
852
|
-
return {
|
|
853
|
-
supportsStore: !isNonStandard,
|
|
854
|
-
supportsDeveloperRole: !isNonStandard,
|
|
855
|
-
supportsReasoningEffort: !isGrok && !isZai,
|
|
856
|
-
reasoningEffortMap,
|
|
857
|
-
supportsUsageInStreaming: true,
|
|
858
|
-
maxTokensField: useMaxTokens ? "max_tokens" : "max_completion_tokens",
|
|
859
|
-
requiresToolResultName: false,
|
|
860
|
-
requiresAssistantAfterToolResult: false,
|
|
861
|
-
requiresThinkingAsText: false,
|
|
862
|
-
requiresReasoningContentOnAssistantMessages: isDeepSeek,
|
|
863
|
-
thinkingFormat: isDeepSeek
|
|
864
|
-
? "deepseek"
|
|
865
|
-
: isZai
|
|
866
|
-
? "zai"
|
|
867
|
-
: provider === "openrouter" || baseUrl.includes("openrouter.ai")
|
|
868
|
-
? "openrouter"
|
|
869
|
-
: "openai",
|
|
870
|
-
openRouterRouting: {},
|
|
871
|
-
vercelGatewayRouting: {},
|
|
872
|
-
zaiToolStream: false,
|
|
873
|
-
supportsStrictMode: true,
|
|
874
|
-
cacheControlFormat,
|
|
875
|
-
sendSessionAffinityHeaders: false,
|
|
876
|
-
supportsLongCacheRetention: true,
|
|
877
|
-
};
|
|
878
|
-
}
|
|
879
|
-
/**
|
|
880
|
-
* Get resolved compatibility settings for a model.
|
|
881
|
-
* Uses explicit model.compat if provided, otherwise auto-detects from provider/URL.
|
|
882
|
-
*/
|
|
883
|
-
function getCompat(model) {
|
|
884
|
-
const detected = detectCompat(model);
|
|
885
|
-
if (!model.compat)
|
|
886
|
-
return detected;
|
|
887
|
-
return {
|
|
888
|
-
supportsStore: model.compat.supportsStore ?? detected.supportsStore,
|
|
889
|
-
supportsDeveloperRole: model.compat.supportsDeveloperRole ?? detected.supportsDeveloperRole,
|
|
890
|
-
supportsReasoningEffort: model.compat.supportsReasoningEffort ?? detected.supportsReasoningEffort,
|
|
891
|
-
reasoningEffortMap: model.compat.reasoningEffortMap ?? detected.reasoningEffortMap,
|
|
892
|
-
supportsUsageInStreaming: model.compat.supportsUsageInStreaming ?? detected.supportsUsageInStreaming,
|
|
893
|
-
maxTokensField: model.compat.maxTokensField ?? detected.maxTokensField,
|
|
894
|
-
requiresToolResultName: model.compat.requiresToolResultName ?? detected.requiresToolResultName,
|
|
895
|
-
requiresAssistantAfterToolResult: model.compat.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,
|
|
896
|
-
requiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,
|
|
897
|
-
requiresReasoningContentOnAssistantMessages: model.compat.requiresReasoningContentOnAssistantMessages ??
|
|
898
|
-
detected.requiresReasoningContentOnAssistantMessages,
|
|
899
|
-
thinkingFormat: model.compat.thinkingFormat ?? detected.thinkingFormat,
|
|
900
|
-
openRouterRouting: model.compat.openRouterRouting ?? {},
|
|
901
|
-
vercelGatewayRouting: model.compat.vercelGatewayRouting ?? detected.vercelGatewayRouting,
|
|
902
|
-
zaiToolStream: model.compat.zaiToolStream ?? detected.zaiToolStream,
|
|
903
|
-
supportsStrictMode: model.compat.supportsStrictMode ?? detected.supportsStrictMode,
|
|
904
|
-
cacheControlFormat: model.compat.cacheControlFormat ?? detected.cacheControlFormat,
|
|
905
|
-
sendSessionAffinityHeaders: model.compat.sendSessionAffinityHeaders ?? detected.sendSessionAffinityHeaders,
|
|
906
|
-
supportsLongCacheRetention: model.compat.supportsLongCacheRetention ?? detected.supportsLongCacheRetention,
|
|
907
|
-
};
|
|
908
|
-
}
|
|
909
|
-
//# sourceMappingURL=openai-completions.js.map
|