@vibedev-si/dsh-vibedev 0.2.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/LICENSE +21 -0
- package/README.md +155 -0
- package/THIRD-PARTY-NOTICES.txt +566 -0
- package/client/client.js +1368 -0
- package/cordis.patch.yml +5 -0
- package/icon.svg +7 -0
- package/lib/account/index.d.ts +118 -0
- package/lib/anthropic-messages-KOS0z8Fy.js +13472 -0
- package/lib/auth/credentials.d.ts +47 -0
- package/lib/auth/login.d.ts +154 -0
- package/lib/gateway/assets.d.ts +61 -0
- package/lib/gateway/catalog.d.ts +126 -0
- package/lib/gateway/errors.d.ts +76 -0
- package/lib/gateway/http.d.ts +94 -0
- package/lib/index.d.ts +105 -0
- package/lib/index.js +7301 -0
- package/lib/llm/adapter.d.ts +129 -0
- package/lib/llm/catalog.d.ts +75 -0
- package/lib/llm/context.d.ts +54 -0
- package/lib/llm/index.d.ts +69 -0
- package/lib/llm/limits.d.ts +19 -0
- package/lib/llm/models.d.ts +67 -0
- package/lib/llm/replay.d.ts +66 -0
- package/lib/llm/stream.d.ts +46 -0
- package/lib/media/chat.d.ts +49 -0
- package/lib/media/sources.d.ts +46 -0
- package/lib/models-CAcLevEN.js +1045 -0
- package/lib/node-Cujw_a4_.js +2403 -0
- package/lib/node-vkDR3GtK.js +25 -0
- package/lib/openai-completions-DqTCiqpS.js +983 -0
- package/lib/openai-prompt-cache-kzRZVokq.js +9567 -0
- package/lib/openai-responses-DygWnsye.js +832 -0
- package/lib/pricing.d.ts +49 -0
- package/lib/runtime.d.ts +150 -0
- package/lib/search/provider.d.ts +86 -0
- package/lib/service.d.ts +99 -0
- package/lib/tasks/remote.d.ts +47 -0
- package/lib/tasks/store.d.ts +132 -0
- package/lib/tasks/tracker.d.ts +127 -0
- package/lib/tools/account.d.ts +21 -0
- package/lib/tools/audio.d.ts +13 -0
- package/lib/tools/common.d.ts +77 -0
- package/lib/tools/image.d.ts +50 -0
- package/lib/tools/models.d.ts +13 -0
- package/lib/tools/tasks.d.ts +13 -0
- package/lib/tools/transcribe.d.ts +19 -0
- package/lib/tools/video.d.ts +48 -0
- package/lib/transform-messages-Csvf2ItW.js +1023 -0
- package/lib/util/files.d.ts +80 -0
- package/lib/video/plan.d.ts +96 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +127 -0
|
@@ -0,0 +1,983 @@
|
|
|
1
|
+
import { _ as getProviderEnvValue, a as clampThinkingBudgetToAnswerRoom, c as hasCopilotVisionInput, d as getGrammarToolInput, f as getJsonSchemaToolParameters, g as retryProviderRequest, h as sanitizeSurrogates, l as appendGrammarToolInputJsonDelta, m as resolveJsonSchemaStrictSampling, o as thinkingBudgetForLevel, p as resolveGrammarConstrainedSampling, r as buildBaseOptions, s as buildCopilotDynamicHeaders, t as transformMessages, u as createGrammarToolInputProperties, v as getPiUserAgent, x as parseStreamingJson, y as headersToRecord } from "./transform-messages-Csvf2ItW.js";
|
|
2
|
+
import { a as getDeclaredTools, c as resolveTranscript, d as renderSystemMessageUpdate, l as resolveTranscriptTools, m as AssistantMessageEventStream, n as clampThinkingLevel, t as calculateCost, u as getSystemMessageText } from "./models-CAcLevEN.js";
|
|
3
|
+
import { a as OpenAI, i as normalizeProviderError, n as shortHash, r as formatProviderError, t as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-kzRZVokq.js";
|
|
4
|
+
//#region node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js
|
|
5
|
+
/**
|
|
6
|
+
* Check if conversation messages contain tool calls or tool results.
|
|
7
|
+
* This is needed because Anthropic (via proxy) requires the tools param
|
|
8
|
+
* to be present when messages include tool_calls or tool role messages.
|
|
9
|
+
*/
|
|
10
|
+
function hasHeader(headers, name) {
|
|
11
|
+
if (!headers) return false;
|
|
12
|
+
const expected = name.toLowerCase();
|
|
13
|
+
for (const [key, value] of Object.entries(headers)) if (key.toLowerCase() === expected && value !== null && value.trim().length > 0) return true;
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
function getClientApiKey(provider, apiKey, headers) {
|
|
17
|
+
if (apiKey) return apiKey;
|
|
18
|
+
if (hasHeader(headers, "authorization") || hasHeader(headers, "cf-aig-authorization")) return "unused";
|
|
19
|
+
throw new Error(`No API key for provider: ${provider}`);
|
|
20
|
+
}
|
|
21
|
+
function hasToolHistory(messages) {
|
|
22
|
+
for (const msg of messages) {
|
|
23
|
+
if (msg.role === "toolResult") return true;
|
|
24
|
+
if (msg.role === "assistant") {
|
|
25
|
+
if (msg.content.some((block) => block.type === "toolCall")) return true;
|
|
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 isReasoningDetailObject(detail) {
|
|
43
|
+
return typeof detail === "object" && detail !== null && !Array.isArray(detail);
|
|
44
|
+
}
|
|
45
|
+
function hasValidCommonReasoningDetailFields(candidate) {
|
|
46
|
+
return (candidate.id === void 0 || candidate.id === null || typeof candidate.id === "string") && (candidate.format === void 0 || typeof candidate.format === "string") && (candidate.index === void 0 || typeof candidate.index === "number");
|
|
47
|
+
}
|
|
48
|
+
function isOpenAIReasoningDetail(detail) {
|
|
49
|
+
if (!isReasoningDetailObject(detail) || !hasValidCommonReasoningDetailFields(detail)) return false;
|
|
50
|
+
switch (detail.type) {
|
|
51
|
+
case "reasoning.summary": return typeof detail.summary === "string";
|
|
52
|
+
case "reasoning.encrypted": return typeof detail.data === "string";
|
|
53
|
+
case "reasoning.text": return typeof detail.text === "string" && (detail.signature === void 0 || detail.signature === null || typeof detail.signature === "string");
|
|
54
|
+
default: return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function parseOpenAIReasoningDetails(signature) {
|
|
58
|
+
if (!signature) return void 0;
|
|
59
|
+
try {
|
|
60
|
+
const parsed = JSON.parse(signature);
|
|
61
|
+
return Array.isArray(parsed) && parsed.length > 0 && parsed.every(isOpenAIReasoningDetail) ? parsed : void 0;
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function parseLegacyEncryptedReasoningDetail(signature) {
|
|
67
|
+
if (!signature) return void 0;
|
|
68
|
+
try {
|
|
69
|
+
const parsed = JSON.parse(signature);
|
|
70
|
+
return isOpenAIReasoningDetail(parsed) && parsed.type === "reasoning.encrypted" && typeof parsed.id === "string" && parsed.id.length > 0 && parsed.data.length > 0 ? parsed : void 0;
|
|
71
|
+
} catch {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function fillMissingCommonReasoningDetailFields(target, source) {
|
|
76
|
+
target.id ??= source.id;
|
|
77
|
+
target.format ||= source.format;
|
|
78
|
+
target.index ??= source.index;
|
|
79
|
+
}
|
|
80
|
+
function appendOpenAIReasoningDetail(details, detail) {
|
|
81
|
+
const lastDetail = details[details.length - 1];
|
|
82
|
+
if (detail.type === "reasoning.text" && lastDetail?.type === "reasoning.text") {
|
|
83
|
+
lastDetail.text += detail.text;
|
|
84
|
+
lastDetail.signature ||= detail.signature;
|
|
85
|
+
fillMissingCommonReasoningDetailFields(lastDetail, detail);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (detail.type === "reasoning.summary" && lastDetail?.type === "reasoning.summary") {
|
|
89
|
+
lastDetail.summary += detail.summary;
|
|
90
|
+
fillMissingCommonReasoningDetailFields(lastDetail, detail);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
details.push({ ...detail });
|
|
94
|
+
}
|
|
95
|
+
const OPENAI_COMPLETIONS_REASONING_FIELDS = [
|
|
96
|
+
"reasoning",
|
|
97
|
+
"reasoning_content",
|
|
98
|
+
"reasoning_text"
|
|
99
|
+
];
|
|
100
|
+
function isOpenAICompletionsReasoningField(field) {
|
|
101
|
+
return OPENAI_COMPLETIONS_REASONING_FIELDS.includes(field);
|
|
102
|
+
}
|
|
103
|
+
function resolveCacheRetention(cacheRetention, env) {
|
|
104
|
+
if (cacheRetention) return cacheRetention;
|
|
105
|
+
if (getProviderEnvValue("PI_CACHE_RETENTION", env) === "long") return "long";
|
|
106
|
+
return "short";
|
|
107
|
+
}
|
|
108
|
+
const stream = (model, context, options) => {
|
|
109
|
+
const stream = new AssistantMessageEventStream();
|
|
110
|
+
const normalizedContext = resolveTranscript(context, getCompat(model).supportsMidConvoSystemMessages);
|
|
111
|
+
(async () => {
|
|
112
|
+
const output = {
|
|
113
|
+
role: "assistant",
|
|
114
|
+
content: [],
|
|
115
|
+
api: model.api,
|
|
116
|
+
provider: model.provider,
|
|
117
|
+
model: model.id,
|
|
118
|
+
usage: {
|
|
119
|
+
input: 0,
|
|
120
|
+
output: 0,
|
|
121
|
+
cacheRead: 0,
|
|
122
|
+
cacheWrite: 0,
|
|
123
|
+
totalTokens: 0,
|
|
124
|
+
cost: {
|
|
125
|
+
input: 0,
|
|
126
|
+
output: 0,
|
|
127
|
+
cacheRead: 0,
|
|
128
|
+
cacheWrite: 0,
|
|
129
|
+
total: 0
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
stopReason: "pending",
|
|
133
|
+
timestamp: Date.now()
|
|
134
|
+
};
|
|
135
|
+
let streamedReasoningDetails;
|
|
136
|
+
const applyStreamedReasoningDetails = (block) => {
|
|
137
|
+
if (streamedReasoningDetails !== void 0) block.thinkingSignature = JSON.stringify(streamedReasoningDetails);
|
|
138
|
+
};
|
|
139
|
+
try {
|
|
140
|
+
const apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
141
|
+
const compat = getCompat(model);
|
|
142
|
+
const grammarToolInputProperties = createGrammarToolInputProperties(getDeclaredTools(normalizedContext.messages), compat.supportsOpenAIGrammarTools);
|
|
143
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
144
|
+
const cacheSessionId = cacheRetention === "none" ? void 0 : options?.sessionId;
|
|
145
|
+
const client = createClient(model, normalizedContext, apiKey, options?.headers, options?.fetch, cacheSessionId, compat);
|
|
146
|
+
let params = buildParams(model, normalizedContext, options, compat, cacheRetention, grammarToolInputProperties);
|
|
147
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
148
|
+
if (nextParams !== void 0) params = nextParams;
|
|
149
|
+
const requestOptions = {
|
|
150
|
+
...options?.signal ? { signal: options.signal } : {},
|
|
151
|
+
...options?.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {},
|
|
152
|
+
maxRetries: 0
|
|
153
|
+
};
|
|
154
|
+
const { data: openaiStream, response } = await retryProviderRequest(() => client.chat.completions.create(params, requestOptions).withResponse(), {
|
|
155
|
+
maxRetries: options?.maxRetries,
|
|
156
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
157
|
+
signal: options?.signal
|
|
158
|
+
});
|
|
159
|
+
await options?.onResponse?.({
|
|
160
|
+
status: response.status,
|
|
161
|
+
headers: headersToRecord(response.headers)
|
|
162
|
+
}, model);
|
|
163
|
+
stream.push({
|
|
164
|
+
type: "start",
|
|
165
|
+
partial: output
|
|
166
|
+
});
|
|
167
|
+
let textBlock = null;
|
|
168
|
+
let thinkingBlock = null;
|
|
169
|
+
let hasFinishReason = false;
|
|
170
|
+
const toolCallBlocksByIndex = /* @__PURE__ */ new Map();
|
|
171
|
+
const toolCallBlocksById = /* @__PURE__ */ new Map();
|
|
172
|
+
const blocks = output.content;
|
|
173
|
+
const getContentIndex = (block) => blocks.indexOf(block);
|
|
174
|
+
const getCustomToolCallInput = (block) => {
|
|
175
|
+
const property = block.customInput?.property;
|
|
176
|
+
if (property === void 0) return "";
|
|
177
|
+
const value = block.arguments[property];
|
|
178
|
+
return typeof value === "string" ? value : "";
|
|
179
|
+
};
|
|
180
|
+
const appendCustomToolCallInput = (block, nextInput, close) => {
|
|
181
|
+
const customInput = block.customInput;
|
|
182
|
+
if (!customInput) return void 0;
|
|
183
|
+
const delta = appendGrammarToolInputJsonDelta(customInput.jsonBuffer, customInput.property, nextInput, close);
|
|
184
|
+
block.arguments = { [customInput.property]: nextInput };
|
|
185
|
+
return delta;
|
|
186
|
+
};
|
|
187
|
+
const finishBlock = (block) => {
|
|
188
|
+
const contentIndex = getContentIndex(block);
|
|
189
|
+
if (contentIndex === -1) return;
|
|
190
|
+
if (block.type === "text") stream.push({
|
|
191
|
+
type: "text_end",
|
|
192
|
+
contentIndex,
|
|
193
|
+
content: block.text,
|
|
194
|
+
partial: output
|
|
195
|
+
});
|
|
196
|
+
else if (block.type === "thinking") {
|
|
197
|
+
applyStreamedReasoningDetails(block);
|
|
198
|
+
stream.push({
|
|
199
|
+
type: "thinking_end",
|
|
200
|
+
contentIndex,
|
|
201
|
+
content: block.thinking,
|
|
202
|
+
partial: output
|
|
203
|
+
});
|
|
204
|
+
} else if (block.type === "toolCall") {
|
|
205
|
+
if (block.customInput) {
|
|
206
|
+
const delta = appendCustomToolCallInput(block, getCustomToolCallInput(block), true);
|
|
207
|
+
if (delta !== void 0) stream.push({
|
|
208
|
+
type: "toolcall_delta",
|
|
209
|
+
contentIndex,
|
|
210
|
+
delta,
|
|
211
|
+
partial: output
|
|
212
|
+
});
|
|
213
|
+
} else block.arguments = parseStreamingJson(block.partialArgs);
|
|
214
|
+
delete block.partialArgs;
|
|
215
|
+
delete block.customInput;
|
|
216
|
+
delete block.streamIndex;
|
|
217
|
+
stream.push({
|
|
218
|
+
type: "toolcall_end",
|
|
219
|
+
contentIndex,
|
|
220
|
+
toolCall: block,
|
|
221
|
+
partial: output
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const ensureTextBlock = () => {
|
|
226
|
+
if (!textBlock) {
|
|
227
|
+
textBlock = {
|
|
228
|
+
type: "text",
|
|
229
|
+
text: ""
|
|
230
|
+
};
|
|
231
|
+
blocks.push(textBlock);
|
|
232
|
+
stream.push({
|
|
233
|
+
type: "text_start",
|
|
234
|
+
contentIndex: getContentIndex(textBlock),
|
|
235
|
+
partial: output
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
return textBlock;
|
|
239
|
+
};
|
|
240
|
+
const ensureThinkingBlock = (thinkingSignature) => {
|
|
241
|
+
if (!thinkingBlock) {
|
|
242
|
+
thinkingBlock = {
|
|
243
|
+
type: "thinking",
|
|
244
|
+
thinking: "",
|
|
245
|
+
thinkingSignature
|
|
246
|
+
};
|
|
247
|
+
blocks.push(thinkingBlock);
|
|
248
|
+
stream.push({
|
|
249
|
+
type: "thinking_start",
|
|
250
|
+
contentIndex: getContentIndex(thinkingBlock),
|
|
251
|
+
partial: output
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
return thinkingBlock;
|
|
255
|
+
};
|
|
256
|
+
const ensureToolCallBlock = (toolCall) => {
|
|
257
|
+
const streamIndex = typeof toolCall.index === "number" ? toolCall.index : void 0;
|
|
258
|
+
const name = toolCall.function?.name ?? toolCall.custom?.name ?? "";
|
|
259
|
+
let block = streamIndex !== void 0 ? toolCallBlocksByIndex.get(streamIndex) : void 0;
|
|
260
|
+
if (!block && toolCall.id) block = toolCallBlocksById.get(toolCall.id);
|
|
261
|
+
if (!block) {
|
|
262
|
+
const customInputProperty = toolCall.custom && !toolCall.function ? grammarToolInputProperties.get(name) ?? "input" : void 0;
|
|
263
|
+
const hasCustomInput = customInputProperty !== void 0;
|
|
264
|
+
block = {
|
|
265
|
+
type: "toolCall",
|
|
266
|
+
id: toolCall.id || "",
|
|
267
|
+
name,
|
|
268
|
+
arguments: hasCustomInput ? { [customInputProperty]: "" } : {},
|
|
269
|
+
partialArgs: hasCustomInput ? void 0 : "",
|
|
270
|
+
customInput: hasCustomInput ? {
|
|
271
|
+
property: customInputProperty,
|
|
272
|
+
jsonBuffer: {
|
|
273
|
+
input: "",
|
|
274
|
+
started: false,
|
|
275
|
+
closed: false
|
|
276
|
+
}
|
|
277
|
+
} : void 0,
|
|
278
|
+
streamIndex
|
|
279
|
+
};
|
|
280
|
+
if (streamIndex !== void 0) toolCallBlocksByIndex.set(streamIndex, block);
|
|
281
|
+
if (toolCall.id) toolCallBlocksById.set(toolCall.id, block);
|
|
282
|
+
blocks.push(block);
|
|
283
|
+
stream.push({
|
|
284
|
+
type: "toolcall_start",
|
|
285
|
+
contentIndex: getContentIndex(block),
|
|
286
|
+
partial: output
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
if (streamIndex !== void 0 && block.streamIndex === void 0) {
|
|
290
|
+
block.streamIndex = streamIndex;
|
|
291
|
+
toolCallBlocksByIndex.set(streamIndex, block);
|
|
292
|
+
}
|
|
293
|
+
if (toolCall.id) toolCallBlocksById.set(toolCall.id, block);
|
|
294
|
+
if (!block.name && name) block.name = name;
|
|
295
|
+
if (toolCall.custom && !toolCall.function && !block.customInput) {
|
|
296
|
+
const customInputProperty = grammarToolInputProperties.get(block.name) ?? "input";
|
|
297
|
+
block.arguments = { [customInputProperty]: "" };
|
|
298
|
+
block.customInput = {
|
|
299
|
+
property: customInputProperty,
|
|
300
|
+
jsonBuffer: {
|
|
301
|
+
input: "",
|
|
302
|
+
started: false,
|
|
303
|
+
closed: false
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
delete block.partialArgs;
|
|
307
|
+
}
|
|
308
|
+
return block;
|
|
309
|
+
};
|
|
310
|
+
for await (const chunk of openaiStream) {
|
|
311
|
+
if (!chunk || typeof chunk !== "object") continue;
|
|
312
|
+
output.responseId ||= chunk.id;
|
|
313
|
+
if (typeof chunk.model === "string" && chunk.model.length > 0 && chunk.model !== model.id) output.responseModel ||= chunk.model;
|
|
314
|
+
if (chunk.usage) output.usage = parseChunkUsage(chunk.usage, model);
|
|
315
|
+
const choice = Array.isArray(chunk.choices) ? chunk.choices[0] : void 0;
|
|
316
|
+
if (!choice) continue;
|
|
317
|
+
if (!chunk.usage && choice.usage) output.usage = parseChunkUsage(choice.usage, model);
|
|
318
|
+
if (choice.finish_reason) {
|
|
319
|
+
output.rawStopReason = choice.finish_reason;
|
|
320
|
+
const finishReasonResult = mapStopReason(choice.finish_reason);
|
|
321
|
+
output.stopReason = finishReasonResult.stopReason;
|
|
322
|
+
if (finishReasonResult.errorMessage) output.errorMessage = finishReasonResult.errorMessage;
|
|
323
|
+
hasFinishReason = true;
|
|
324
|
+
}
|
|
325
|
+
if (choice.delta) {
|
|
326
|
+
if (choice.delta.content !== null && choice.delta.content !== void 0 && choice.delta.content.length > 0) {
|
|
327
|
+
const block = ensureTextBlock();
|
|
328
|
+
block.text += choice.delta.content;
|
|
329
|
+
stream.push({
|
|
330
|
+
type: "text_delta",
|
|
331
|
+
contentIndex: getContentIndex(block),
|
|
332
|
+
delta: choice.delta.content,
|
|
333
|
+
partial: output
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
const reasoningFields = [
|
|
337
|
+
"reasoning_content",
|
|
338
|
+
"reasoning",
|
|
339
|
+
"reasoning_text"
|
|
340
|
+
];
|
|
341
|
+
const deltaFields = choice.delta;
|
|
342
|
+
let foundReasoningField = null;
|
|
343
|
+
for (const field of reasoningFields) {
|
|
344
|
+
const value = deltaFields[field];
|
|
345
|
+
if (typeof value === "string" && value.length > 0) {
|
|
346
|
+
foundReasoningField = field;
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (foundReasoningField) {
|
|
351
|
+
const delta = deltaFields[foundReasoningField];
|
|
352
|
+
if (typeof delta === "string" && delta.length > 0) {
|
|
353
|
+
const block = ensureThinkingBlock(model.provider === "opencode-go" && foundReasoningField === "reasoning" ? "reasoning_content" : foundReasoningField);
|
|
354
|
+
block.thinking += delta;
|
|
355
|
+
stream.push({
|
|
356
|
+
type: "thinking_delta",
|
|
357
|
+
contentIndex: getContentIndex(block),
|
|
358
|
+
delta,
|
|
359
|
+
partial: output
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (choice?.delta?.tool_calls) for (const toolCall of choice.delta.tool_calls) {
|
|
364
|
+
const block = ensureToolCallBlock(toolCall);
|
|
365
|
+
if (!block.id && toolCall.id) {
|
|
366
|
+
block.id = toolCall.id;
|
|
367
|
+
toolCallBlocksById.set(toolCall.id, block);
|
|
368
|
+
}
|
|
369
|
+
const name = toolCall.function?.name ?? toolCall.custom?.name;
|
|
370
|
+
if (!block.name && name) block.name = name;
|
|
371
|
+
let delta = "";
|
|
372
|
+
if (toolCall.function?.arguments) {
|
|
373
|
+
delta = toolCall.function.arguments;
|
|
374
|
+
block.partialArgs = (block.partialArgs ?? "") + toolCall.function.arguments;
|
|
375
|
+
} else if (toolCall.custom?.input) delta = appendCustomToolCallInput(block, getCustomToolCallInput(block) + toolCall.custom.input, false) ?? "";
|
|
376
|
+
stream.push({
|
|
377
|
+
type: "toolcall_delta",
|
|
378
|
+
contentIndex: getContentIndex(block),
|
|
379
|
+
delta,
|
|
380
|
+
partial: output
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
const reasoningDetails = choice.delta.reasoning_details;
|
|
384
|
+
if (Array.isArray(reasoningDetails)) for (const detail of reasoningDetails) {
|
|
385
|
+
if (!isOpenAIReasoningDetail(detail)) continue;
|
|
386
|
+
ensureThinkingBlock("");
|
|
387
|
+
streamedReasoningDetails ??= [];
|
|
388
|
+
appendOpenAIReasoningDetail(streamedReasoningDetails, detail);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
for (const block of blocks) finishBlock(block);
|
|
393
|
+
if (options?.signal?.aborted) throw new Error("Request was aborted");
|
|
394
|
+
if (output.stopReason === "aborted") throw new Error("Request was aborted");
|
|
395
|
+
if (!hasFinishReason && !compat.supportsFinishReason) output.stopReason = output.content.some((block) => block.type === "toolCall") ? "toolUse" : "stop";
|
|
396
|
+
if (output.stopReason === "error") throw new Error(output.errorMessage || "Provider returned an error stop reason");
|
|
397
|
+
if (compat.supportsFinishReason && !hasFinishReason || output.stopReason === "pending") throw new Error("Stream ended without finish_reason");
|
|
398
|
+
stream.push({
|
|
399
|
+
type: "done",
|
|
400
|
+
reason: output.stopReason,
|
|
401
|
+
message: output
|
|
402
|
+
});
|
|
403
|
+
stream.end();
|
|
404
|
+
} catch (error) {
|
|
405
|
+
for (const block of output.content) {
|
|
406
|
+
if (block.type === "thinking") applyStreamedReasoningDetails(block);
|
|
407
|
+
delete block.index;
|
|
408
|
+
delete block.partialArgs;
|
|
409
|
+
delete block.customInput;
|
|
410
|
+
delete block.streamIndex;
|
|
411
|
+
}
|
|
412
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
413
|
+
output.errorMessage = formatProviderError(normalizeProviderError(error));
|
|
414
|
+
const rawMetadata = error?.error?.metadata?.raw;
|
|
415
|
+
if (rawMetadata && !output.errorMessage.includes(String(rawMetadata))) output.errorMessage += `\n${rawMetadata}`;
|
|
416
|
+
stream.push({
|
|
417
|
+
type: "error",
|
|
418
|
+
reason: output.stopReason,
|
|
419
|
+
error: output
|
|
420
|
+
});
|
|
421
|
+
stream.end();
|
|
422
|
+
}
|
|
423
|
+
})();
|
|
424
|
+
return stream;
|
|
425
|
+
};
|
|
426
|
+
const streamSimple = (model, context, options) => {
|
|
427
|
+
getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
428
|
+
const base = {
|
|
429
|
+
...buildBaseOptions(model, context, options, options?.apiKey),
|
|
430
|
+
toolChoice: options?.toolChoice
|
|
431
|
+
};
|
|
432
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : void 0;
|
|
433
|
+
const reasoningEffort = clampedReasoning === "off" ? void 0 : clampedReasoning;
|
|
434
|
+
return stream(model, context, {
|
|
435
|
+
...base,
|
|
436
|
+
reasoningEffort,
|
|
437
|
+
thinkingBudgets: options?.thinkingBudgets
|
|
438
|
+
});
|
|
439
|
+
};
|
|
440
|
+
function createClient(model, context, apiKey, optionsHeaders, fetch, sessionId, compat = getCompat(model)) {
|
|
441
|
+
const headers = {
|
|
442
|
+
"User-Agent": getPiUserAgent(),
|
|
443
|
+
...model.headers
|
|
444
|
+
};
|
|
445
|
+
if (model.provider === "github-copilot") {
|
|
446
|
+
const hasImages = hasCopilotVisionInput(context.messages);
|
|
447
|
+
const copilotHeaders = buildCopilotDynamicHeaders({
|
|
448
|
+
messages: context.messages,
|
|
449
|
+
hasImages
|
|
450
|
+
});
|
|
451
|
+
Object.assign(headers, copilotHeaders);
|
|
452
|
+
}
|
|
453
|
+
if (sessionId && compat.sendSessionAffinityHeaders) {
|
|
454
|
+
if (compat.sessionAffinityFormat === "openrouter") headers["x-session-id"] = sessionId;
|
|
455
|
+
else {
|
|
456
|
+
if (compat.sessionAffinityFormat === "openai") headers.session_id = sessionId;
|
|
457
|
+
headers["x-client-request-id"] = sessionId;
|
|
458
|
+
headers["x-session-affinity"] = sessionId;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (optionsHeaders) Object.assign(headers, optionsHeaders);
|
|
462
|
+
return new OpenAI({
|
|
463
|
+
apiKey,
|
|
464
|
+
baseURL: model.baseUrl,
|
|
465
|
+
dangerouslyAllowBrowser: true,
|
|
466
|
+
fetch,
|
|
467
|
+
defaultHeaders: headers
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
function buildParams(model, context, options, compat = getCompat(model), cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env), grammarToolInputProperties = createGrammarToolInputProperties(getDeclaredTools(context.messages), compat.supportsOpenAIGrammarTools)) {
|
|
471
|
+
const transcriptTools = resolveTranscriptTools(context.messages, compat.supportsMidConvoSystemMessages === true && compat.supportsMidConvoToolAdditions === true);
|
|
472
|
+
const messages = convertMessages(model, context, compat, { grammarToolInputProperties });
|
|
473
|
+
const cacheControl = getCompatCacheControl(compat, cacheRetention);
|
|
474
|
+
const params = {
|
|
475
|
+
model: model.id,
|
|
476
|
+
messages,
|
|
477
|
+
stream: true,
|
|
478
|
+
prompt_cache_key: model.baseUrl.includes("api.openai.com") && cacheRetention !== "none" || cacheRetention === "long" && compat.supportsLongCacheRetention ? clampOpenAIPromptCacheKey(options?.sessionId) : void 0,
|
|
479
|
+
prompt_cache_retention: cacheRetention === "long" && compat.supportsLongCacheRetention ? "24h" : void 0
|
|
480
|
+
};
|
|
481
|
+
if (compat.supportsUsageInStreaming !== false) params.stream_options = { include_usage: true };
|
|
482
|
+
if (compat.supportsStore) params.store = false;
|
|
483
|
+
if (options?.maxTokens) {
|
|
484
|
+
if (compat.maxTokensField === "max_tokens") params.max_tokens = options.maxTokens;
|
|
485
|
+
else params.max_completion_tokens = options.maxTokens;
|
|
486
|
+
}
|
|
487
|
+
if (options?.temperature !== void 0) params.temperature = options.temperature;
|
|
488
|
+
if (transcriptTools.requestTools.length > 0) {
|
|
489
|
+
params.tools = convertTools(transcriptTools.requestTools, compat);
|
|
490
|
+
if (compat.zaiToolStream) params.tool_stream = true;
|
|
491
|
+
} else if (hasToolHistory(context.messages)) params.tools = [];
|
|
492
|
+
if (cacheControl) applyAnthropicCacheControl(messages, params.tools, cacheControl);
|
|
493
|
+
if (options?.toolChoice) params.tool_choice = options.toolChoice;
|
|
494
|
+
if (compat.vllmPriority !== void 0) params.priority = compat.vllmPriority;
|
|
495
|
+
const thinkingTokenBudgetField = resolveThinkingTokenBudgetField(compat);
|
|
496
|
+
const thinkingBudget = resolveClampedThinkingBudget(model, options, params);
|
|
497
|
+
if (compat.thinkingFormat === "zai" && model.reasoning) {
|
|
498
|
+
const zaiParams = params;
|
|
499
|
+
zaiParams.thinking = options?.reasoningEffort ? {
|
|
500
|
+
type: "enabled",
|
|
501
|
+
clear_thinking: false
|
|
502
|
+
} : { type: "disabled" };
|
|
503
|
+
if (options?.reasoningEffort && compat.supportsReasoningEffort) {
|
|
504
|
+
const mappedEffort = model.thinkingLevelMap?.[options.reasoningEffort];
|
|
505
|
+
const effort = mappedEffort === void 0 ? options.reasoningEffort : mappedEffort;
|
|
506
|
+
if (typeof effort === "string") zaiParams.reasoning_effort = effort;
|
|
507
|
+
}
|
|
508
|
+
} else if (compat.thinkingFormat === "qwen" && model.reasoning) {
|
|
509
|
+
params.enable_thinking = !!options?.reasoningEffort;
|
|
510
|
+
if (options?.reasoningEffort && compat.supportsReasoningEffort) {
|
|
511
|
+
const effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
512
|
+
if (typeof effort === "string") params.reasoning_effort = effort;
|
|
513
|
+
}
|
|
514
|
+
} else if (compat.thinkingFormat === "qwen-chat-template" && model.reasoning) params.chat_template_kwargs = {
|
|
515
|
+
enable_thinking: !!options?.reasoningEffort,
|
|
516
|
+
preserve_thinking: true
|
|
517
|
+
};
|
|
518
|
+
else if (compat.thinkingFormat === "chat-template" && model.reasoning) {
|
|
519
|
+
const chatTemplateKwargs = buildChatTemplateValues(model, options, compat.chatTemplateKwargs, thinkingBudget);
|
|
520
|
+
if (chatTemplateKwargs) params.chat_template_kwargs = chatTemplateKwargs;
|
|
521
|
+
} else if (compat.thinkingFormat === "baseten" && model.reasoning) {
|
|
522
|
+
const basetenParams = params;
|
|
523
|
+
const chatTemplateArgs = buildChatTemplateValues(model, options, compat.chatTemplateArgs, thinkingBudget);
|
|
524
|
+
if (chatTemplateArgs) basetenParams.chat_template_args = chatTemplateArgs;
|
|
525
|
+
if (compat.supportsReasoningEffort) {
|
|
526
|
+
const requestedEffort = options?.reasoningEffort;
|
|
527
|
+
const mappedEffort = requestedEffort ? model.thinkingLevelMap?.[requestedEffort] : model.thinkingLevelMap?.off;
|
|
528
|
+
const effort = mappedEffort === void 0 ? requestedEffort : mappedEffort;
|
|
529
|
+
if (typeof effort === "string") basetenParams.reasoning_effort = effort;
|
|
530
|
+
}
|
|
531
|
+
} else if (compat.thinkingFormat === "deepseek" && model.reasoning) {
|
|
532
|
+
if (options?.reasoningEffort) params.thinking = { type: "enabled" };
|
|
533
|
+
else if (model.thinkingLevelMap?.off !== null) params.thinking = { type: "disabled" };
|
|
534
|
+
if (options?.reasoningEffort && compat.supportsReasoningEffort) params.reasoning_effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
535
|
+
} else if (compat.thinkingFormat === "openrouter" && model.reasoning) {
|
|
536
|
+
const openRouterParams = params;
|
|
537
|
+
if (options?.reasoningEffort) openRouterParams.reasoning = { effort: model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort };
|
|
538
|
+
else if (model.thinkingLevelMap?.off !== null) openRouterParams.reasoning = { effort: model.thinkingLevelMap?.off ?? "none" };
|
|
539
|
+
} else if (compat.thinkingFormat === "ant-ling" && model.reasoning && options?.reasoningEffort) {
|
|
540
|
+
const effort = model.thinkingLevelMap?.[options.reasoningEffort];
|
|
541
|
+
if (typeof effort === "string") params.reasoning = { effort };
|
|
542
|
+
} else if (compat.thinkingFormat === "together" && model.reasoning) {
|
|
543
|
+
const togetherParams = params;
|
|
544
|
+
togetherParams.reasoning = { enabled: !!options?.reasoningEffort };
|
|
545
|
+
if (options?.reasoningEffort && compat.supportsReasoningEffort) togetherParams.reasoning_effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
546
|
+
} else if (compat.thinkingFormat === "string-thinking" && model.reasoning) {
|
|
547
|
+
const stringThinkingParams = params;
|
|
548
|
+
if (options?.reasoningEffort) stringThinkingParams.thinking = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
549
|
+
else if (model.thinkingLevelMap?.off !== null) stringThinkingParams.thinking = model.thinkingLevelMap?.off ?? "none";
|
|
550
|
+
} else if (options?.reasoningEffort && model.reasoning && compat.supportsReasoningEffort) params.reasoning_effort = model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort;
|
|
551
|
+
else if (!options?.reasoningEffort && model.reasoning && compat.supportsReasoningEffort) {
|
|
552
|
+
const offValue = model.thinkingLevelMap?.off;
|
|
553
|
+
if (typeof offValue === "string") params.reasoning_effort = offValue;
|
|
554
|
+
}
|
|
555
|
+
if (thinkingTokenBudgetField && thinkingBudget !== void 0) Object.assign(params, { [thinkingTokenBudgetField]: thinkingBudget });
|
|
556
|
+
if (model.compat?.openRouterRouting) params.provider = model.compat.openRouterRouting;
|
|
557
|
+
if (model.compat?.vercelGatewayRouting) {
|
|
558
|
+
const routing = model.compat.vercelGatewayRouting;
|
|
559
|
+
if (routing.only || routing.order) {
|
|
560
|
+
const gatewayOptions = {};
|
|
561
|
+
if (routing.only) gatewayOptions.only = routing.only;
|
|
562
|
+
if (routing.order) gatewayOptions.order = routing.order;
|
|
563
|
+
params.providerOptions = { gateway: gatewayOptions };
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if (options?.samplingParams) Object.assign(params, options.samplingParams);
|
|
567
|
+
return params;
|
|
568
|
+
}
|
|
569
|
+
function resolveThinkingTokenBudgetField(compat) {
|
|
570
|
+
if (compat.thinkingTokenBudgetField) return compat.thinkingTokenBudgetField;
|
|
571
|
+
if (compat.supportsThinkingTokenBudget) return "thinking_token_budget";
|
|
572
|
+
}
|
|
573
|
+
function resolveClampedThinkingBudget(model, options, params) {
|
|
574
|
+
if (!options?.reasoningEffort || !model.reasoning) return void 0;
|
|
575
|
+
const ceiling = params.max_tokens ?? params.max_completion_tokens ?? model.maxTokens;
|
|
576
|
+
const budget = clampThinkingBudgetToAnswerRoom(thinkingBudgetForLevel(options.reasoningEffort, options.thinkingBudgets), ceiling);
|
|
577
|
+
return budget > 0 ? budget : void 0;
|
|
578
|
+
}
|
|
579
|
+
function buildChatTemplateValues(model, options, values, thinkingBudget) {
|
|
580
|
+
const resolvedValues = {};
|
|
581
|
+
for (const [key, value] of Object.entries(values)) {
|
|
582
|
+
const resolved = resolveChatTemplateKwargValue(model, options, value, thinkingBudget);
|
|
583
|
+
if (resolved !== void 0) resolvedValues[key] = resolved;
|
|
584
|
+
}
|
|
585
|
+
return Object.keys(resolvedValues).length > 0 ? resolvedValues : void 0;
|
|
586
|
+
}
|
|
587
|
+
function resolveChatTemplateKwargValue(model, options, value, thinkingBudget) {
|
|
588
|
+
if (typeof value !== "object" || value === null) return value;
|
|
589
|
+
const reasoningEffort = options?.reasoningEffort;
|
|
590
|
+
if (!reasoningEffort && value.omitWhenOff) return;
|
|
591
|
+
if (value.$var === "thinking.enabled") return !!reasoningEffort;
|
|
592
|
+
if (value.$var === "thinking.budget") return thinkingBudget;
|
|
593
|
+
const mappedValue = reasoningEffort ? model.thinkingLevelMap?.[reasoningEffort] : model.thinkingLevelMap?.off;
|
|
594
|
+
return mappedValue === void 0 ? reasoningEffort : typeof mappedValue === "string" ? mappedValue : void 0;
|
|
595
|
+
}
|
|
596
|
+
function getCompatCacheControl(compat, cacheRetention) {
|
|
597
|
+
if (compat.cacheControlFormat !== "anthropic" || cacheRetention === "none") return;
|
|
598
|
+
const ttl = cacheRetention === "long" && compat.supportsLongCacheRetention ? "1h" : void 0;
|
|
599
|
+
return {
|
|
600
|
+
type: "ephemeral",
|
|
601
|
+
...ttl ? { ttl } : {}
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
function applyAnthropicCacheControl(messages, tools, cacheControl) {
|
|
605
|
+
addCacheControlToSystemPrompt(messages, cacheControl);
|
|
606
|
+
addCacheControlToLastTool(tools, cacheControl);
|
|
607
|
+
addCacheControlToLastConversationMessage(messages, cacheControl);
|
|
608
|
+
}
|
|
609
|
+
function addCacheControlToSystemPrompt(messages, cacheControl) {
|
|
610
|
+
for (const message of messages) if (message.role === "system" || message.role === "developer") {
|
|
611
|
+
addCacheControlToInstructionMessage(message, cacheControl);
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
function addCacheControlToLastConversationMessage(messages, cacheControl) {
|
|
616
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
617
|
+
const message = messages[i];
|
|
618
|
+
if (message.role === "user" || message.role === "assistant" || message.role === "tool") {
|
|
619
|
+
if (addCacheControlToMessage(message, cacheControl)) return;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
function addCacheControlToLastTool(tools, cacheControl) {
|
|
624
|
+
if (!tools || tools.length === 0) return;
|
|
625
|
+
const lastTool = tools[tools.length - 1];
|
|
626
|
+
lastTool.cache_control = cacheControl;
|
|
627
|
+
}
|
|
628
|
+
function addCacheControlToInstructionMessage(message, cacheControl) {
|
|
629
|
+
return addCacheControlToTextContent(message, cacheControl);
|
|
630
|
+
}
|
|
631
|
+
function addCacheControlToMessage(message, cacheControl) {
|
|
632
|
+
if (message.role === "user" || message.role === "assistant" || message.role === "tool") return addCacheControlToTextContent(message, cacheControl);
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
function addCacheControlToTextContent(message, cacheControl) {
|
|
636
|
+
const content = message.content;
|
|
637
|
+
if (typeof content === "string") {
|
|
638
|
+
if (content.length === 0) return false;
|
|
639
|
+
message.content = [{
|
|
640
|
+
type: "text",
|
|
641
|
+
text: content,
|
|
642
|
+
cache_control: cacheControl
|
|
643
|
+
}];
|
|
644
|
+
return true;
|
|
645
|
+
}
|
|
646
|
+
if (!Array.isArray(content)) return false;
|
|
647
|
+
for (let i = content.length - 1; i >= 0; i--) {
|
|
648
|
+
const part = content[i];
|
|
649
|
+
if (part?.type === "text") {
|
|
650
|
+
const textPart = part;
|
|
651
|
+
textPart.cache_control = cacheControl;
|
|
652
|
+
return true;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return false;
|
|
656
|
+
}
|
|
657
|
+
function convertMessages(model, context, compat, options) {
|
|
658
|
+
const normalizedContext = resolveTranscript(context, compat.supportsMidConvoSystemMessages);
|
|
659
|
+
const params = [];
|
|
660
|
+
const normalizeToolCallId = (id) => {
|
|
661
|
+
if (id.includes("|")) {
|
|
662
|
+
const separatorIndex = id.indexOf("|");
|
|
663
|
+
const callId = id.slice(0, separatorIndex).replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
664
|
+
const itemId = id.slice(separatorIndex + 1).replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
665
|
+
const combinedId = itemId.length > 0 ? `${callId}_${itemId}` : callId;
|
|
666
|
+
if (combinedId.length <= 40) return combinedId;
|
|
667
|
+
const hash = shortHash(id).slice(0, 8);
|
|
668
|
+
return `${callId.slice(0, Math.max(1, 40 - hash.length - 1))}_${hash}`;
|
|
669
|
+
}
|
|
670
|
+
if (model.provider === "openai") return id.length > 40 ? id.slice(0, 40) : id;
|
|
671
|
+
return id;
|
|
672
|
+
};
|
|
673
|
+
const transformedMessages = transformMessages(normalizedContext.messages, model, (id) => normalizeToolCallId(id));
|
|
674
|
+
const transcriptTools = resolveTranscriptTools(normalizedContext.messages, compat.supportsMidConvoSystemMessages === true && compat.supportsMidConvoToolAdditions === true);
|
|
675
|
+
const instructionRole = model.reasoning && compat.supportsDeveloperRole ? "developer" : "system";
|
|
676
|
+
let lastRole = null;
|
|
677
|
+
for (let i = 0; i < transformedMessages.length; i++) {
|
|
678
|
+
const msg = transformedMessages[i];
|
|
679
|
+
if (compat.requiresAssistantAfterToolResult && lastRole === "toolResult" && msg.role === "user") params.push({
|
|
680
|
+
role: "assistant",
|
|
681
|
+
content: "I have processed the tool results."
|
|
682
|
+
});
|
|
683
|
+
if (msg.role === "system") {
|
|
684
|
+
const addedTools = i > 0 && transcriptTools.anchorsAdditions ? msg.toolsAdded ?? [] : [];
|
|
685
|
+
if (addedTools.length > 0) {
|
|
686
|
+
const kimiToolMessage = {
|
|
687
|
+
role: "system",
|
|
688
|
+
tools: convertTools(addedTools, compat)
|
|
689
|
+
};
|
|
690
|
+
params.push(kimiToolMessage);
|
|
691
|
+
}
|
|
692
|
+
const text = i === 0 ? getSystemMessageText(msg) : renderSystemMessageUpdate(msg);
|
|
693
|
+
if (text.length > 0) params.push({
|
|
694
|
+
role: instructionRole,
|
|
695
|
+
content: sanitizeSurrogates(text)
|
|
696
|
+
});
|
|
697
|
+
} else if (msg.role === "user") {
|
|
698
|
+
if (typeof msg.content === "string") params.push({
|
|
699
|
+
role: "user",
|
|
700
|
+
content: sanitizeSurrogates(msg.content)
|
|
701
|
+
});
|
|
702
|
+
else {
|
|
703
|
+
const content = msg.content.filter((item) => item.type !== "text" || item.text.length > 0).map((item) => {
|
|
704
|
+
if (item.type === "text") return {
|
|
705
|
+
type: "text",
|
|
706
|
+
text: sanitizeSurrogates(item.text)
|
|
707
|
+
};
|
|
708
|
+
else return {
|
|
709
|
+
type: "image_url",
|
|
710
|
+
image_url: { url: `data:${item.mimeType};base64,${item.data}` }
|
|
711
|
+
};
|
|
712
|
+
});
|
|
713
|
+
if (content.length === 0) continue;
|
|
714
|
+
params.push({
|
|
715
|
+
role: "user",
|
|
716
|
+
content
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
} else if (msg.role === "assistant") {
|
|
720
|
+
const assistantMsg = {
|
|
721
|
+
role: "assistant",
|
|
722
|
+
content: compat.requiresAssistantAfterToolResult ? "" : null
|
|
723
|
+
};
|
|
724
|
+
const assistantTextParts = msg.content.filter(isTextContentBlock).filter((block) => block.text.trim().length > 0).map((block) => ({
|
|
725
|
+
type: "text",
|
|
726
|
+
text: sanitizeSurrogates(block.text)
|
|
727
|
+
}));
|
|
728
|
+
const assistantText = assistantTextParts.map((part) => part.text).join("");
|
|
729
|
+
const thinkingBlocks = msg.content.filter(isThinkingContentBlock);
|
|
730
|
+
const toolCalls = msg.content.filter(isToolCallBlock);
|
|
731
|
+
const signedReasoningDetails = thinkingBlocks.map((block) => parseOpenAIReasoningDetails(block.thinkingSignature)).find((details) => details !== void 0);
|
|
732
|
+
const legacyReasoningDetails = toolCalls.map((toolCall) => parseLegacyEncryptedReasoningDetail(toolCall.thoughtSignature)).filter((detail) => detail !== void 0);
|
|
733
|
+
const preservedReasoningDetails = signedReasoningDetails ?? (legacyReasoningDetails.length > 0 ? legacyReasoningDetails : void 0);
|
|
734
|
+
const nonEmptyThinkingBlocks = thinkingBlocks.filter((block) => block.thinking.trim().length > 0);
|
|
735
|
+
if (nonEmptyThinkingBlocks.length > 0) {
|
|
736
|
+
if (compat.requiresThinkingAsText) assistantMsg.content = [{
|
|
737
|
+
type: "text",
|
|
738
|
+
text: nonEmptyThinkingBlocks.map((block) => sanitizeSurrogates(block.thinking)).join("\n\n")
|
|
739
|
+
}, ...assistantTextParts];
|
|
740
|
+
else {
|
|
741
|
+
if (assistantText.length > 0) assistantMsg.content = assistantText;
|
|
742
|
+
if (!preservedReasoningDetails) {
|
|
743
|
+
let signature = nonEmptyThinkingBlocks[0].thinkingSignature;
|
|
744
|
+
if (model.provider === "opencode-go" && signature === "reasoning") signature = "reasoning_content";
|
|
745
|
+
if (signature && isOpenAICompletionsReasoningField(signature)) assistantMsg[signature] = nonEmptyThinkingBlocks.map((block) => block.thinking).join("\n");
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
} else if (assistantText.length > 0) assistantMsg.content = assistantText;
|
|
749
|
+
if (toolCalls.length > 0) assistantMsg.tool_calls = toolCalls.map((tc) => {
|
|
750
|
+
const customInputProperty = options?.grammarToolInputProperties?.get(tc.name);
|
|
751
|
+
if (customInputProperty !== void 0) return {
|
|
752
|
+
id: tc.id,
|
|
753
|
+
type: "custom",
|
|
754
|
+
custom: {
|
|
755
|
+
name: tc.name,
|
|
756
|
+
input: sanitizeSurrogates(getGrammarToolInput(tc.name, tc.arguments, customInputProperty))
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
return {
|
|
760
|
+
id: tc.id,
|
|
761
|
+
type: "function",
|
|
762
|
+
function: {
|
|
763
|
+
name: tc.name,
|
|
764
|
+
arguments: JSON.stringify(tc.arguments)
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
});
|
|
768
|
+
if (preservedReasoningDetails) assistantMsg.reasoning_details = preservedReasoningDetails;
|
|
769
|
+
if (compat.requiresReasoningContentOnAssistantMessages && model.reasoning && assistantMsg.reasoning_content === void 0) assistantMsg.reasoning_content = "";
|
|
770
|
+
const content = assistantMsg.content;
|
|
771
|
+
if (!(content !== null && content !== void 0 && (typeof content === "string" ? content.length > 0 : content.length > 0)) && !assistantMsg.tool_calls) continue;
|
|
772
|
+
params.push(assistantMsg);
|
|
773
|
+
} else if (msg.role === "toolResult") {
|
|
774
|
+
const imageBlocks = [];
|
|
775
|
+
let j = i;
|
|
776
|
+
for (; j < transformedMessages.length && transformedMessages[j].role === "toolResult"; j++) {
|
|
777
|
+
const toolMsg = transformedMessages[j];
|
|
778
|
+
const textResult = toolMsg.content.filter(isTextContentBlock).map((block) => block.text).join("\n");
|
|
779
|
+
const hasImages = toolMsg.content.some((c) => c.type === "image");
|
|
780
|
+
const toolResultText = textResult.length > 0 ? textResult : hasImages ? "(see attached image)" : "(no tool output)";
|
|
781
|
+
const toolResultMsg = {
|
|
782
|
+
role: "tool",
|
|
783
|
+
content: sanitizeSurrogates(toolResultText),
|
|
784
|
+
tool_call_id: toolMsg.toolCallId
|
|
785
|
+
};
|
|
786
|
+
if (compat.requiresToolResultName && toolMsg.toolName) toolResultMsg.name = toolMsg.toolName;
|
|
787
|
+
params.push(toolResultMsg);
|
|
788
|
+
if (hasImages && model.input.includes("image")) {
|
|
789
|
+
for (const block of toolMsg.content) if (isImageContentBlock(block)) imageBlocks.push({
|
|
790
|
+
type: "image_url",
|
|
791
|
+
image_url: { url: `data:${block.mimeType};base64,${block.data}` }
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
i = j - 1;
|
|
796
|
+
if (imageBlocks.length > 0) {
|
|
797
|
+
if (compat.requiresAssistantAfterToolResult) params.push({
|
|
798
|
+
role: "assistant",
|
|
799
|
+
content: "I have processed the tool results."
|
|
800
|
+
});
|
|
801
|
+
params.push({
|
|
802
|
+
role: "user",
|
|
803
|
+
content: [{
|
|
804
|
+
type: "text",
|
|
805
|
+
text: "Attached image(s) from tool result:"
|
|
806
|
+
}, ...imageBlocks]
|
|
807
|
+
});
|
|
808
|
+
lastRole = "user";
|
|
809
|
+
} else lastRole = "toolResult";
|
|
810
|
+
continue;
|
|
811
|
+
}
|
|
812
|
+
lastRole = msg.role;
|
|
813
|
+
}
|
|
814
|
+
return params;
|
|
815
|
+
}
|
|
816
|
+
function convertTools(tools, compat) {
|
|
817
|
+
return tools.map((tool) => {
|
|
818
|
+
const grammar = resolveGrammarConstrainedSampling(tool, compat.supportsOpenAIGrammarTools);
|
|
819
|
+
if (grammar) return {
|
|
820
|
+
type: "custom",
|
|
821
|
+
custom: {
|
|
822
|
+
name: tool.name,
|
|
823
|
+
description: tool.description,
|
|
824
|
+
format: {
|
|
825
|
+
type: "grammar",
|
|
826
|
+
grammar: {
|
|
827
|
+
syntax: grammar.format,
|
|
828
|
+
definition: grammar.definition
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
const strict = resolveJsonSchemaStrictSampling(tool, compat.supportsStrictMode !== false);
|
|
834
|
+
return {
|
|
835
|
+
type: "function",
|
|
836
|
+
function: {
|
|
837
|
+
name: tool.name,
|
|
838
|
+
description: tool.description,
|
|
839
|
+
parameters: getJsonSchemaToolParameters(tool, strict),
|
|
840
|
+
...compat.supportsStrictMode !== false && { strict: strict ?? false }
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
function parseChunkUsage(rawUsage, model) {
|
|
846
|
+
const promptTokens = rawUsage.prompt_tokens || 0;
|
|
847
|
+
const cacheReadTokens = rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.prompt_cache_hit_tokens ?? rawUsage.cached_tokens ?? 0;
|
|
848
|
+
const cacheWriteTokens = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;
|
|
849
|
+
const input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);
|
|
850
|
+
const outputTokens = rawUsage.completion_tokens || 0;
|
|
851
|
+
const usage = {
|
|
852
|
+
input,
|
|
853
|
+
output: outputTokens,
|
|
854
|
+
cacheRead: cacheReadTokens,
|
|
855
|
+
cacheWrite: cacheWriteTokens,
|
|
856
|
+
reasoning: rawUsage.completion_tokens_details?.reasoning_tokens || 0,
|
|
857
|
+
totalTokens: input + outputTokens + cacheReadTokens + cacheWriteTokens,
|
|
858
|
+
cost: {
|
|
859
|
+
input: 0,
|
|
860
|
+
output: 0,
|
|
861
|
+
cacheRead: 0,
|
|
862
|
+
cacheWrite: 0,
|
|
863
|
+
total: 0
|
|
864
|
+
}
|
|
865
|
+
};
|
|
866
|
+
calculateCost(model, usage);
|
|
867
|
+
return usage;
|
|
868
|
+
}
|
|
869
|
+
function mapStopReason(reason) {
|
|
870
|
+
if (reason === null) return { stopReason: "stop" };
|
|
871
|
+
switch (reason) {
|
|
872
|
+
case "stop":
|
|
873
|
+
case "end": return { stopReason: "stop" };
|
|
874
|
+
case "length": return { stopReason: "length" };
|
|
875
|
+
case "function_call":
|
|
876
|
+
case "tool_calls": return { stopReason: "toolUse" };
|
|
877
|
+
case "content_filter": return {
|
|
878
|
+
stopReason: "error",
|
|
879
|
+
errorMessage: "Provider finish_reason: content_filter"
|
|
880
|
+
};
|
|
881
|
+
case "network_error": return {
|
|
882
|
+
stopReason: "error",
|
|
883
|
+
errorMessage: "Provider finish_reason: network_error"
|
|
884
|
+
};
|
|
885
|
+
default: return {
|
|
886
|
+
stopReason: "error",
|
|
887
|
+
errorMessage: `Provider finish_reason: ${reason}`
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Auto-detect compatibility settings from provider name and baseUrl.
|
|
893
|
+
* Used as the base when model.compat is not set; explicit model.compat
|
|
894
|
+
* entries override these detected values.
|
|
895
|
+
*/
|
|
896
|
+
function detectCompat(model) {
|
|
897
|
+
const provider = model.provider;
|
|
898
|
+
const baseUrl = model.baseUrl;
|
|
899
|
+
const isZai = provider === "zai" || provider === "zai-coding-cn" || baseUrl.includes("api.z.ai") || baseUrl.includes("open.bigmodel.cn");
|
|
900
|
+
const isTogether = provider === "together" || baseUrl.includes("api.together.ai") || baseUrl.includes("api.together.xyz");
|
|
901
|
+
const isMoonshot = provider === "moonshotai" || provider === "moonshotai-cn" || baseUrl.includes("api.moonshot.");
|
|
902
|
+
const isOpenRouter = provider === "openrouter" || baseUrl.includes("openrouter.ai");
|
|
903
|
+
const isCloudflareWorkersAI = provider === "cloudflare-workers-ai" || baseUrl.includes("api.cloudflare.com");
|
|
904
|
+
const isCloudflareAiGateway = provider === "cloudflare-ai-gateway" || baseUrl.includes("gateway.ai.cloudflare.com");
|
|
905
|
+
const isNvidia = provider === "nvidia" || baseUrl.includes("integrate.api.nvidia.com");
|
|
906
|
+
const isAntLing = provider === "ant-ling" || baseUrl.includes("api.ant-ling.com");
|
|
907
|
+
const isCerebras = provider === "cerebras" || baseUrl.includes("cerebras.ai");
|
|
908
|
+
const isDeepSeek = provider === "deepseek" || baseUrl.toLowerCase().includes("deepseek.com");
|
|
909
|
+
const isNonStandard = isNvidia || isCerebras || provider === "xai" || baseUrl.includes("api.x.ai") || isTogether || baseUrl.includes("chutes.ai") || isDeepSeek || isZai || isMoonshot || provider === "opencode" || baseUrl.includes("opencode.ai") || isCloudflareWorkersAI || isCloudflareAiGateway || isAntLing;
|
|
910
|
+
const useMaxTokens = baseUrl.includes("chutes.ai") || isDeepSeek || isMoonshot || isCloudflareAiGateway || isTogether || isNvidia || isAntLing || isZai;
|
|
911
|
+
const isGrok = provider === "xai" || baseUrl.includes("api.x.ai");
|
|
912
|
+
const isOpenRouterDeveloperRoleModel = isOpenRouter && (model.id.startsWith("anthropic/") || model.id.startsWith("openai/"));
|
|
913
|
+
const cacheControlFormat = provider === "openrouter" && model.id.startsWith("anthropic/") ? "anthropic" : void 0;
|
|
914
|
+
return {
|
|
915
|
+
supportsStore: !isNonStandard,
|
|
916
|
+
supportsDeveloperRole: isOpenRouterDeveloperRoleModel || !isNonStandard && !isOpenRouter,
|
|
917
|
+
supportsReasoningEffort: !isGrok && !isZai && !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia && !isAntLing,
|
|
918
|
+
supportsUsageInStreaming: true,
|
|
919
|
+
supportsFinishReason: true,
|
|
920
|
+
maxTokensField: useMaxTokens ? "max_tokens" : "max_completion_tokens",
|
|
921
|
+
requiresToolResultName: false,
|
|
922
|
+
requiresAssistantAfterToolResult: false,
|
|
923
|
+
requiresThinkingAsText: false,
|
|
924
|
+
requiresReasoningContentOnAssistantMessages: isDeepSeek,
|
|
925
|
+
thinkingFormat: isDeepSeek ? "deepseek" : isZai ? "zai" : isTogether ? "together" : isAntLing ? "ant-ling" : isOpenRouter ? "openrouter" : "openai",
|
|
926
|
+
openRouterRouting: {},
|
|
927
|
+
vercelGatewayRouting: {},
|
|
928
|
+
chatTemplateKwargs: {},
|
|
929
|
+
chatTemplateArgs: {},
|
|
930
|
+
zaiToolStream: false,
|
|
931
|
+
supportsThinkingTokenBudget: false,
|
|
932
|
+
thinkingTokenBudgetField: void 0,
|
|
933
|
+
supportsStrictMode: false,
|
|
934
|
+
supportsOpenAIGrammarTools: false,
|
|
935
|
+
supportsMidConvoSystemMessages: false,
|
|
936
|
+
supportsMidConvoToolAdditions: false,
|
|
937
|
+
cacheControlFormat,
|
|
938
|
+
sendSessionAffinityHeaders: isOpenRouter,
|
|
939
|
+
sessionAffinityFormat: isOpenRouter ? "openrouter" : "openai",
|
|
940
|
+
supportsLongCacheRetention: !(isTogether || isCloudflareWorkersAI || isCloudflareAiGateway || isNvidia || isAntLing)
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Get resolved compatibility settings for a model.
|
|
945
|
+
* Auto-detects from provider/URL then overrides with explicit model.compat.
|
|
946
|
+
*/
|
|
947
|
+
function getCompat(model) {
|
|
948
|
+
const detected = detectCompat(model);
|
|
949
|
+
if (!model.compat) return detected;
|
|
950
|
+
return {
|
|
951
|
+
supportsStore: model.compat.supportsStore ?? detected.supportsStore,
|
|
952
|
+
supportsDeveloperRole: model.compat.supportsDeveloperRole ?? detected.supportsDeveloperRole,
|
|
953
|
+
supportsReasoningEffort: model.compat.supportsReasoningEffort ?? detected.supportsReasoningEffort,
|
|
954
|
+
supportsUsageInStreaming: model.compat.supportsUsageInStreaming ?? detected.supportsUsageInStreaming,
|
|
955
|
+
supportsFinishReason: model.compat.supportsFinishReason ?? detected.supportsFinishReason,
|
|
956
|
+
maxTokensField: model.compat.maxTokensField ?? detected.maxTokensField,
|
|
957
|
+
requiresToolResultName: model.compat.requiresToolResultName ?? detected.requiresToolResultName,
|
|
958
|
+
requiresAssistantAfterToolResult: model.compat.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,
|
|
959
|
+
requiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,
|
|
960
|
+
requiresReasoningContentOnAssistantMessages: model.compat.requiresReasoningContentOnAssistantMessages ?? detected.requiresReasoningContentOnAssistantMessages,
|
|
961
|
+
thinkingFormat: model.compat.thinkingFormat ?? detected.thinkingFormat,
|
|
962
|
+
openRouterRouting: model.compat.openRouterRouting ?? {},
|
|
963
|
+
vercelGatewayRouting: model.compat.vercelGatewayRouting ?? detected.vercelGatewayRouting,
|
|
964
|
+
chatTemplateKwargs: model.compat.chatTemplateKwargs ?? detected.chatTemplateKwargs,
|
|
965
|
+
chatTemplateArgs: model.compat.chatTemplateArgs ?? detected.chatTemplateArgs,
|
|
966
|
+
zaiToolStream: model.compat.zaiToolStream ?? detected.zaiToolStream,
|
|
967
|
+
supportsThinkingTokenBudget: model.compat.supportsThinkingTokenBudget ?? detected.supportsThinkingTokenBudget,
|
|
968
|
+
thinkingTokenBudgetField: model.compat.thinkingTokenBudgetField ?? detected.thinkingTokenBudgetField,
|
|
969
|
+
supportsStrictMode: model.compat.supportsStrictMode ?? detected.supportsStrictMode,
|
|
970
|
+
supportsOpenAIGrammarTools: model.compat.supportsOpenAIGrammarTools ?? detected.supportsOpenAIGrammarTools,
|
|
971
|
+
supportsMidConvoSystemMessages: model.compat.supportsMidConvoSystemMessages ?? detected.supportsMidConvoSystemMessages,
|
|
972
|
+
supportsMidConvoToolAdditions: model.compat.supportsMidConvoToolAdditions ?? detected.supportsMidConvoToolAdditions,
|
|
973
|
+
cacheControlFormat: model.compat.cacheControlFormat ?? detected.cacheControlFormat,
|
|
974
|
+
sendSessionAffinityHeaders: model.compat.sendSessionAffinityHeaders ?? detected.sendSessionAffinityHeaders,
|
|
975
|
+
sessionAffinityFormat: model.compat.sessionAffinityFormat ?? detected.sessionAffinityFormat,
|
|
976
|
+
supportsLongCacheRetention: model.compat.supportsLongCacheRetention ?? detected.supportsLongCacheRetention,
|
|
977
|
+
vllmPriority: model.compat.vllmPriority
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
//#endregion
|
|
981
|
+
export { convertMessages, stream, streamSimple };
|
|
982
|
+
|
|
983
|
+
//# sourceMappingURL=openai-completions-DqTCiqpS.js.map
|