@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,832 @@
|
|
|
1
|
+
import { _ as getProviderEnvValue, c as hasCopilotVisionInput, d as getGrammarToolInput, f as getJsonSchemaToolParameters, g as retryProviderRequest, h as sanitizeSurrogates, l as appendGrammarToolInputJsonDelta, m as resolveJsonSchemaStrictSampling, 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-responses-shared.js
|
|
5
|
+
function encodeTextSignatureV1(id, phase) {
|
|
6
|
+
const payload = {
|
|
7
|
+
v: 1,
|
|
8
|
+
id
|
|
9
|
+
};
|
|
10
|
+
if (phase) payload.phase = phase;
|
|
11
|
+
return JSON.stringify(payload);
|
|
12
|
+
}
|
|
13
|
+
function parseTextSignature(signature) {
|
|
14
|
+
if (!signature) return void 0;
|
|
15
|
+
if (signature.startsWith("{")) try {
|
|
16
|
+
const parsed = JSON.parse(signature);
|
|
17
|
+
if (parsed.v === 1 && typeof parsed.id === "string") {
|
|
18
|
+
if (parsed.phase === "commentary" || parsed.phase === "final_answer") return {
|
|
19
|
+
id: parsed.id,
|
|
20
|
+
phase: parsed.phase
|
|
21
|
+
};
|
|
22
|
+
return { id: parsed.id };
|
|
23
|
+
}
|
|
24
|
+
} catch {}
|
|
25
|
+
return { id: signature };
|
|
26
|
+
}
|
|
27
|
+
function convertToolResultOutput(model, content) {
|
|
28
|
+
const textResult = content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
29
|
+
const images = content.filter((c) => c.type === "image");
|
|
30
|
+
const hasText = textResult.length > 0;
|
|
31
|
+
if (images.length === 0 || !model.input.includes("image")) return sanitizeSurrogates(hasText ? textResult : images.length > 0 ? "(see attached image)" : "(no tool output)");
|
|
32
|
+
const output = [];
|
|
33
|
+
if (hasText) output.push({
|
|
34
|
+
type: "input_text",
|
|
35
|
+
text: sanitizeSurrogates(textResult)
|
|
36
|
+
});
|
|
37
|
+
for (const image of images) output.push({
|
|
38
|
+
type: "input_image",
|
|
39
|
+
detail: "auto",
|
|
40
|
+
image_url: `data:${image.mimeType};base64,${image.data}`
|
|
41
|
+
});
|
|
42
|
+
return output;
|
|
43
|
+
}
|
|
44
|
+
function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
|
|
45
|
+
const normalizedContext = resolveTranscript(context, options?.supportsMidConvoSystemMessages);
|
|
46
|
+
const messages = [];
|
|
47
|
+
const normalizeIdPart = (part) => {
|
|
48
|
+
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
49
|
+
return (sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized).replace(/_+$/, "");
|
|
50
|
+
};
|
|
51
|
+
const buildForeignResponsesItemId = (itemId) => {
|
|
52
|
+
const normalized = `fc_${shortHash(itemId)}`;
|
|
53
|
+
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
|
|
54
|
+
};
|
|
55
|
+
const normalizeToolCallId = (id, _targetModel, source) => {
|
|
56
|
+
if (!allowedToolCallProviders.has(model.provider)) return normalizeIdPart(id);
|
|
57
|
+
if (!id.includes("|")) return normalizeIdPart(id);
|
|
58
|
+
const [callId, itemId] = id.split("|");
|
|
59
|
+
const normalizedCallId = normalizeIdPart(callId);
|
|
60
|
+
let normalizedItemId = source.provider !== model.provider || source.api !== model.api ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
|
|
61
|
+
if (!normalizedItemId.startsWith("fc_")) normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
|
|
62
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
63
|
+
};
|
|
64
|
+
const transformedMessages = transformMessages(normalizedContext.messages, model, normalizeToolCallId);
|
|
65
|
+
const transcriptTools = resolveTranscriptTools(normalizedContext.messages, (options?.supportsAdditionalTools ?? false) || (options?.supportsToolSearch ?? false));
|
|
66
|
+
const appendSystemToolAdditions = (message, seed) => {
|
|
67
|
+
const tools = transcriptTools.anchorsAdditions ? message.toolsAdded ?? [] : [];
|
|
68
|
+
if (tools.length === 0) return;
|
|
69
|
+
if (options?.supportsAdditionalTools) {
|
|
70
|
+
messages.push({
|
|
71
|
+
type: "additional_tools",
|
|
72
|
+
role: "developer",
|
|
73
|
+
tools: convertResponsesTools(tools, options.toolOptions)
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (!options?.supportsToolSearch) return;
|
|
78
|
+
const names = tools.map((tool) => tool.name);
|
|
79
|
+
const callId = `pi_tool_load_${shortHash(`${seed}:${names.join(",")}`)}`;
|
|
80
|
+
messages.push({
|
|
81
|
+
type: "tool_search_call",
|
|
82
|
+
call_id: callId,
|
|
83
|
+
execution: "client",
|
|
84
|
+
status: "completed",
|
|
85
|
+
arguments: {
|
|
86
|
+
query: names.join(" "),
|
|
87
|
+
limit: names.length
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
messages.push({
|
|
91
|
+
type: "tool_search_output",
|
|
92
|
+
call_id: callId,
|
|
93
|
+
execution: "client",
|
|
94
|
+
status: "completed",
|
|
95
|
+
tools: convertResponsesTools(tools, {
|
|
96
|
+
...options.toolOptions,
|
|
97
|
+
toolSearchResult: true
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const includeInitialSystemMessage = options?.includeSystemPrompt ?? true;
|
|
102
|
+
const compat = model.compat;
|
|
103
|
+
const instructionRole = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
|
|
104
|
+
let msgIndex = 0;
|
|
105
|
+
let sourceIndex = 0;
|
|
106
|
+
for (const msg of transformedMessages) {
|
|
107
|
+
const isLeadingSystemMessage = sourceIndex++ === 0 && msg.role === "system";
|
|
108
|
+
if (msg.role === "system") {
|
|
109
|
+
if (!isLeadingSystemMessage) appendSystemToolAdditions(msg, `system:${msgIndex}`);
|
|
110
|
+
if (!isLeadingSystemMessage || includeInitialSystemMessage) {
|
|
111
|
+
const text = isLeadingSystemMessage ? getSystemMessageText(msg) : renderSystemMessageUpdate(msg);
|
|
112
|
+
if (text.length > 0) messages.push({
|
|
113
|
+
role: instructionRole,
|
|
114
|
+
content: sanitizeSurrogates(text)
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
} else if (msg.role === "user") {
|
|
118
|
+
if (typeof msg.content === "string") messages.push({
|
|
119
|
+
role: "user",
|
|
120
|
+
content: [{
|
|
121
|
+
type: "input_text",
|
|
122
|
+
text: sanitizeSurrogates(msg.content)
|
|
123
|
+
}]
|
|
124
|
+
});
|
|
125
|
+
else {
|
|
126
|
+
const content = msg.content.map((item) => {
|
|
127
|
+
if (item.type === "text") return {
|
|
128
|
+
type: "input_text",
|
|
129
|
+
text: sanitizeSurrogates(item.text)
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
type: "input_image",
|
|
133
|
+
detail: "auto",
|
|
134
|
+
image_url: `data:${item.mimeType};base64,${item.data}`
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
if (content.length === 0) continue;
|
|
138
|
+
messages.push({
|
|
139
|
+
role: "user",
|
|
140
|
+
content
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
} else if (msg.role === "assistant") {
|
|
144
|
+
const output = [];
|
|
145
|
+
const assistantMsg = msg;
|
|
146
|
+
const isSameProviderAndApi = assistantMsg.provider === model.provider && assistantMsg.api === model.api;
|
|
147
|
+
const isSameModel = isSameProviderAndApi && assistantMsg.model === model.id;
|
|
148
|
+
const isDifferentModel = isSameProviderAndApi && assistantMsg.model !== model.id;
|
|
149
|
+
let textBlockIndex = 0;
|
|
150
|
+
for (const block of msg.content) if (block.type === "thinking") {
|
|
151
|
+
if (block.thinkingSignature) {
|
|
152
|
+
const reasoningItem = JSON.parse(block.thinkingSignature);
|
|
153
|
+
output.push(reasoningItem);
|
|
154
|
+
}
|
|
155
|
+
} else if (block.type === "text") {
|
|
156
|
+
const textBlock = block;
|
|
157
|
+
const parsedSignature = parseTextSignature(textBlock.textSignature);
|
|
158
|
+
const fallbackMessageId = textBlockIndex === 0 ? `msg_pi_${msgIndex}` : `msg_pi_${msgIndex}_${textBlockIndex}`;
|
|
159
|
+
textBlockIndex++;
|
|
160
|
+
let msgId = parsedSignature?.id;
|
|
161
|
+
if (!msgId) msgId = fallbackMessageId;
|
|
162
|
+
else if (msgId.length > 64) msgId = `msg_${shortHash(msgId)}`;
|
|
163
|
+
output.push({
|
|
164
|
+
type: "message",
|
|
165
|
+
role: "assistant",
|
|
166
|
+
content: [{
|
|
167
|
+
type: "output_text",
|
|
168
|
+
text: sanitizeSurrogates(textBlock.text),
|
|
169
|
+
annotations: []
|
|
170
|
+
}],
|
|
171
|
+
status: "completed",
|
|
172
|
+
id: msgId,
|
|
173
|
+
phase: parsedSignature?.phase
|
|
174
|
+
});
|
|
175
|
+
} else if (block.type === "toolCall") {
|
|
176
|
+
const toolCall = block;
|
|
177
|
+
const [callId, itemIdRaw] = toolCall.id.split("|");
|
|
178
|
+
const customInputProperty = options?.grammarToolInputProperties?.get(toolCall.name);
|
|
179
|
+
let itemId = itemIdRaw;
|
|
180
|
+
if (isDifferentModel && itemId?.startsWith("fc_") || customInputProperty === void 0 && !itemId?.startsWith("fc_")) itemId = void 0;
|
|
181
|
+
if (customInputProperty !== void 0) output.push({
|
|
182
|
+
type: "custom_tool_call",
|
|
183
|
+
id: itemId,
|
|
184
|
+
call_id: callId,
|
|
185
|
+
name: toolCall.name,
|
|
186
|
+
input: sanitizeSurrogates(getGrammarToolInput(toolCall.name, toolCall.arguments, customInputProperty)),
|
|
187
|
+
...isSameModel && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
|
|
188
|
+
});
|
|
189
|
+
else output.push({
|
|
190
|
+
type: "function_call",
|
|
191
|
+
id: itemId,
|
|
192
|
+
call_id: callId,
|
|
193
|
+
name: toolCall.name,
|
|
194
|
+
arguments: JSON.stringify(toolCall.arguments),
|
|
195
|
+
...isSameModel && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
if (output.length === 0) continue;
|
|
199
|
+
messages.push(...output);
|
|
200
|
+
} else if (msg.role === "toolResult") {
|
|
201
|
+
const [callId] = msg.toolCallId.split("|");
|
|
202
|
+
const output = convertToolResultOutput(model, msg.content);
|
|
203
|
+
if (options?.grammarToolInputProperties?.has(msg.toolName)) messages.push({
|
|
204
|
+
type: "custom_tool_call_output",
|
|
205
|
+
call_id: callId,
|
|
206
|
+
output
|
|
207
|
+
});
|
|
208
|
+
else messages.push({
|
|
209
|
+
type: "function_call_output",
|
|
210
|
+
call_id: callId,
|
|
211
|
+
output
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
if (!isLeadingSystemMessage) msgIndex++;
|
|
215
|
+
}
|
|
216
|
+
return messages;
|
|
217
|
+
}
|
|
218
|
+
function convertResponsesTools(tools, options) {
|
|
219
|
+
const defaultStrict = options?.strict === void 0 ? false : options.strict;
|
|
220
|
+
const supportsStrictMode = options?.supportsStrictMode ?? true;
|
|
221
|
+
const supportsOpenAIGrammarTools = options?.supportsOpenAIGrammarTools ?? false;
|
|
222
|
+
return tools.map((tool) => {
|
|
223
|
+
const grammar = resolveGrammarConstrainedSampling(tool, supportsOpenAIGrammarTools);
|
|
224
|
+
if (grammar) return {
|
|
225
|
+
type: "custom",
|
|
226
|
+
name: tool.name,
|
|
227
|
+
description: tool.description,
|
|
228
|
+
format: {
|
|
229
|
+
type: "grammar",
|
|
230
|
+
syntax: grammar.format,
|
|
231
|
+
definition: grammar.definition
|
|
232
|
+
},
|
|
233
|
+
...options?.toolSearchResult ? { defer_loading: true } : {}
|
|
234
|
+
};
|
|
235
|
+
const strict = resolveJsonSchemaStrictSampling(tool, supportsStrictMode) ?? defaultStrict;
|
|
236
|
+
const functionTool = {
|
|
237
|
+
type: "function",
|
|
238
|
+
name: tool.name,
|
|
239
|
+
description: tool.description,
|
|
240
|
+
parameters: getJsonSchemaToolParameters(tool, strict === true),
|
|
241
|
+
...options?.toolSearchResult ? { defer_loading: true } : {}
|
|
242
|
+
};
|
|
243
|
+
if (supportsStrictMode) functionTool.strict = strict;
|
|
244
|
+
return functionTool;
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
function getCustomToolCallInput(block) {
|
|
248
|
+
const property = block.customInput?.property;
|
|
249
|
+
if (property === void 0) return "";
|
|
250
|
+
const value = block.arguments[property];
|
|
251
|
+
return typeof value === "string" ? value : "";
|
|
252
|
+
}
|
|
253
|
+
function appendCustomToolCallInput(block, nextInput, close) {
|
|
254
|
+
const customInput = block.customInput;
|
|
255
|
+
if (!customInput) return void 0;
|
|
256
|
+
const delta = appendGrammarToolInputJsonDelta(customInput.jsonBuffer, customInput.property, nextInput, close);
|
|
257
|
+
block.arguments = { [customInput.property]: nextInput };
|
|
258
|
+
return delta;
|
|
259
|
+
}
|
|
260
|
+
async function processResponsesStream(openaiStream, output, stream, model, options) {
|
|
261
|
+
let sawTerminalResponseEvent = false;
|
|
262
|
+
const outputSlots = /* @__PURE__ */ new Map();
|
|
263
|
+
const reasoningBlocksById = /* @__PURE__ */ new Map();
|
|
264
|
+
const applyMessagePhaseStopReason = (item) => {
|
|
265
|
+
if (item.type === "message" && item.phase === "final_answer") output.stopReason = "stop";
|
|
266
|
+
};
|
|
267
|
+
const getSlot = (outputIndex, type) => {
|
|
268
|
+
const slot = outputSlots.get(outputIndex);
|
|
269
|
+
return slot?.type === type ? slot : void 0;
|
|
270
|
+
};
|
|
271
|
+
const pushToolCallDelta = (slot, delta) => {
|
|
272
|
+
if (delta === void 0) return;
|
|
273
|
+
stream.push({
|
|
274
|
+
type: "toolcall_delta",
|
|
275
|
+
contentIndex: slot.contentIndex,
|
|
276
|
+
delta,
|
|
277
|
+
partial: output
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
const createSlot = (outputIndex, item) => {
|
|
281
|
+
if (item.type === "reasoning") {
|
|
282
|
+
const block = {
|
|
283
|
+
type: "thinking",
|
|
284
|
+
thinking: ""
|
|
285
|
+
};
|
|
286
|
+
output.content.push(block);
|
|
287
|
+
const slot = {
|
|
288
|
+
type: "thinking",
|
|
289
|
+
block,
|
|
290
|
+
contentIndex: output.content.length - 1
|
|
291
|
+
};
|
|
292
|
+
outputSlots.set(outputIndex, slot);
|
|
293
|
+
stream.push({
|
|
294
|
+
type: "thinking_start",
|
|
295
|
+
contentIndex: slot.contentIndex,
|
|
296
|
+
partial: output
|
|
297
|
+
});
|
|
298
|
+
return slot;
|
|
299
|
+
}
|
|
300
|
+
if (item.type === "message") {
|
|
301
|
+
applyMessagePhaseStopReason(item);
|
|
302
|
+
const block = {
|
|
303
|
+
type: "text",
|
|
304
|
+
text: ""
|
|
305
|
+
};
|
|
306
|
+
output.content.push(block);
|
|
307
|
+
const slot = {
|
|
308
|
+
type: "text",
|
|
309
|
+
block,
|
|
310
|
+
contentIndex: output.content.length - 1
|
|
311
|
+
};
|
|
312
|
+
outputSlots.set(outputIndex, slot);
|
|
313
|
+
stream.push({
|
|
314
|
+
type: "text_start",
|
|
315
|
+
contentIndex: slot.contentIndex,
|
|
316
|
+
partial: output
|
|
317
|
+
});
|
|
318
|
+
return slot;
|
|
319
|
+
}
|
|
320
|
+
if (item.type === "function_call") {
|
|
321
|
+
const block = {
|
|
322
|
+
type: "toolCall",
|
|
323
|
+
id: `${item.call_id}|${item.id}`,
|
|
324
|
+
name: item.name,
|
|
325
|
+
arguments: {},
|
|
326
|
+
...item.namespace !== void 0 ? { namespace: item.namespace } : {},
|
|
327
|
+
partialJson: item.arguments || ""
|
|
328
|
+
};
|
|
329
|
+
output.content.push(block);
|
|
330
|
+
const slot = {
|
|
331
|
+
type: "toolCall",
|
|
332
|
+
block,
|
|
333
|
+
contentIndex: output.content.length - 1
|
|
334
|
+
};
|
|
335
|
+
outputSlots.set(outputIndex, slot);
|
|
336
|
+
stream.push({
|
|
337
|
+
type: "toolcall_start",
|
|
338
|
+
contentIndex: slot.contentIndex,
|
|
339
|
+
partial: output
|
|
340
|
+
});
|
|
341
|
+
return slot;
|
|
342
|
+
}
|
|
343
|
+
if (item.type === "custom_tool_call") {
|
|
344
|
+
const inputProperty = options?.grammarToolInputProperties?.get(item.name) ?? "input";
|
|
345
|
+
const input = item.input || "";
|
|
346
|
+
const block = {
|
|
347
|
+
type: "toolCall",
|
|
348
|
+
id: `${item.call_id}|${item.id}`,
|
|
349
|
+
name: item.name,
|
|
350
|
+
arguments: { [inputProperty]: input },
|
|
351
|
+
...item.namespace !== void 0 ? { namespace: item.namespace } : {},
|
|
352
|
+
customInput: {
|
|
353
|
+
property: inputProperty,
|
|
354
|
+
jsonBuffer: {
|
|
355
|
+
input: "",
|
|
356
|
+
started: false,
|
|
357
|
+
closed: false
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
output.content.push(block);
|
|
362
|
+
const slot = {
|
|
363
|
+
type: "toolCall",
|
|
364
|
+
block,
|
|
365
|
+
contentIndex: output.content.length - 1
|
|
366
|
+
};
|
|
367
|
+
outputSlots.set(outputIndex, slot);
|
|
368
|
+
stream.push({
|
|
369
|
+
type: "toolcall_start",
|
|
370
|
+
contentIndex: slot.contentIndex,
|
|
371
|
+
partial: output
|
|
372
|
+
});
|
|
373
|
+
return slot;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
const getOrCreateSlot = (outputIndex, item) => {
|
|
377
|
+
return outputSlots.get(outputIndex) ?? createSlot(outputIndex, item);
|
|
378
|
+
};
|
|
379
|
+
const backfillReasoningSignatures = (responseOutput) => {
|
|
380
|
+
for (const item of responseOutput) {
|
|
381
|
+
if (item.type !== "reasoning" || !item.encrypted_content) continue;
|
|
382
|
+
const block = reasoningBlocksById.get(item.id);
|
|
383
|
+
if (!block?.thinkingSignature) continue;
|
|
384
|
+
const storedItem = JSON.parse(block.thinkingSignature);
|
|
385
|
+
if (storedItem.encrypted_content) continue;
|
|
386
|
+
block.thinkingSignature = JSON.stringify({
|
|
387
|
+
...storedItem,
|
|
388
|
+
encrypted_content: item.encrypted_content
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
const finalizeResponse = (response) => {
|
|
393
|
+
sawTerminalResponseEvent = true;
|
|
394
|
+
backfillReasoningSignatures(response.output ?? []);
|
|
395
|
+
if (response?.id) output.responseId = response.id;
|
|
396
|
+
if (response?.usage) {
|
|
397
|
+
const inputDetails = response.usage.input_tokens_details;
|
|
398
|
+
const cachedTokens = inputDetails?.cached_tokens || 0;
|
|
399
|
+
const cacheWriteTokens = inputDetails?.cache_write_tokens || 0;
|
|
400
|
+
output.usage = {
|
|
401
|
+
input: Math.max(0, (response.usage.input_tokens || 0) - cachedTokens - cacheWriteTokens),
|
|
402
|
+
output: response.usage.output_tokens || 0,
|
|
403
|
+
cacheRead: cachedTokens,
|
|
404
|
+
cacheWrite: cacheWriteTokens,
|
|
405
|
+
reasoning: response.usage.output_tokens_details?.reasoning_tokens || 0,
|
|
406
|
+
totalTokens: response.usage.total_tokens || 0,
|
|
407
|
+
cost: {
|
|
408
|
+
input: 0,
|
|
409
|
+
output: 0,
|
|
410
|
+
cacheRead: 0,
|
|
411
|
+
cacheWrite: 0,
|
|
412
|
+
total: 0
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
calculateCost(model, output.usage);
|
|
417
|
+
if (options?.applyServiceTierPricing) {
|
|
418
|
+
const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response?.service_tier, options.serviceTier) : response?.service_tier ?? options.serviceTier;
|
|
419
|
+
options.applyServiceTierPricing(output.usage, serviceTier);
|
|
420
|
+
}
|
|
421
|
+
const status = response?.status;
|
|
422
|
+
const incompleteDetails = response?.incomplete_details;
|
|
423
|
+
const incompleteReason = typeof incompleteDetails?.reason === "string" ? incompleteDetails.reason : void 0;
|
|
424
|
+
output.rawStopReason = incompleteReason ? `${status}.${incompleteReason}` : status;
|
|
425
|
+
const mappedStop = mapStopReason(status, incompleteReason);
|
|
426
|
+
output.stopReason = mappedStop.stopReason;
|
|
427
|
+
if (mappedStop.errorMessage === void 0) delete output.errorMessage;
|
|
428
|
+
else output.errorMessage = mappedStop.errorMessage;
|
|
429
|
+
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") output.stopReason = "toolUse";
|
|
430
|
+
};
|
|
431
|
+
for await (const event of openaiStream) if (event.type === "response.created") output.responseId = event.response.id;
|
|
432
|
+
else if (event.type === "response.output_item.added") createSlot(event.output_index, event.item);
|
|
433
|
+
else if (event.type === "response.reasoning_summary_text.delta") {
|
|
434
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
435
|
+
if (!slot) continue;
|
|
436
|
+
slot.block.thinking += event.delta;
|
|
437
|
+
stream.push({
|
|
438
|
+
type: "thinking_delta",
|
|
439
|
+
contentIndex: slot.contentIndex,
|
|
440
|
+
delta: event.delta,
|
|
441
|
+
partial: output
|
|
442
|
+
});
|
|
443
|
+
} else if (event.type === "response.reasoning_summary_part.done") {
|
|
444
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
445
|
+
if (!slot) continue;
|
|
446
|
+
slot.block.thinking += "\n\n";
|
|
447
|
+
stream.push({
|
|
448
|
+
type: "thinking_delta",
|
|
449
|
+
contentIndex: slot.contentIndex,
|
|
450
|
+
delta: "\n\n",
|
|
451
|
+
partial: output
|
|
452
|
+
});
|
|
453
|
+
} else if (event.type === "response.reasoning_text.delta") {
|
|
454
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
455
|
+
if (!slot) continue;
|
|
456
|
+
slot.block.thinking += event.delta;
|
|
457
|
+
stream.push({
|
|
458
|
+
type: "thinking_delta",
|
|
459
|
+
contentIndex: slot.contentIndex,
|
|
460
|
+
delta: event.delta,
|
|
461
|
+
partial: output
|
|
462
|
+
});
|
|
463
|
+
} else if (event.type === "response.output_text.delta") {
|
|
464
|
+
const slot = getSlot(event.output_index, "text");
|
|
465
|
+
if (!slot) continue;
|
|
466
|
+
slot.block.text += event.delta;
|
|
467
|
+
stream.push({
|
|
468
|
+
type: "text_delta",
|
|
469
|
+
contentIndex: slot.contentIndex,
|
|
470
|
+
delta: event.delta,
|
|
471
|
+
partial: output
|
|
472
|
+
});
|
|
473
|
+
} else if (event.type === "response.refusal.delta") {
|
|
474
|
+
const slot = getSlot(event.output_index, "text");
|
|
475
|
+
if (!slot) continue;
|
|
476
|
+
slot.block.text += event.delta;
|
|
477
|
+
stream.push({
|
|
478
|
+
type: "text_delta",
|
|
479
|
+
contentIndex: slot.contentIndex,
|
|
480
|
+
delta: event.delta,
|
|
481
|
+
partial: output
|
|
482
|
+
});
|
|
483
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
484
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
485
|
+
if (!slot || slot.block.partialJson === void 0) continue;
|
|
486
|
+
slot.block.partialJson += event.delta;
|
|
487
|
+
pushToolCallDelta(slot, event.delta);
|
|
488
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
489
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
490
|
+
if (!slot || slot.block.partialJson === void 0) continue;
|
|
491
|
+
const previousPartialJson = slot.block.partialJson;
|
|
492
|
+
slot.block.partialJson = event.arguments;
|
|
493
|
+
slot.block.arguments = parseStreamingJson(slot.block.partialJson);
|
|
494
|
+
if (event.arguments.startsWith(previousPartialJson)) {
|
|
495
|
+
const delta = event.arguments.slice(previousPartialJson.length);
|
|
496
|
+
if (delta.length > 0) pushToolCallDelta(slot, delta);
|
|
497
|
+
}
|
|
498
|
+
} else if (event.type === "response.custom_tool_call_input.delta") {
|
|
499
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
500
|
+
if (!slot || !slot.block.customInput) continue;
|
|
501
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, getCustomToolCallInput(slot.block) + event.delta, false));
|
|
502
|
+
} else if (event.type === "response.custom_tool_call_input.done") {
|
|
503
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
504
|
+
if (!slot || !slot.block.customInput) continue;
|
|
505
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, event.input, true));
|
|
506
|
+
} else if (event.type === "response.output_item.done") {
|
|
507
|
+
const item = event.item;
|
|
508
|
+
applyMessagePhaseStopReason(item);
|
|
509
|
+
const slot = getOrCreateSlot(event.output_index, item);
|
|
510
|
+
if (item.type === "reasoning" && slot?.type === "thinking") {
|
|
511
|
+
const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
|
|
512
|
+
const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
|
|
513
|
+
slot.block.thinking = summaryText || contentText || slot.block.thinking;
|
|
514
|
+
slot.block.thinkingSignature = JSON.stringify(item);
|
|
515
|
+
reasoningBlocksById.set(item.id, slot.block);
|
|
516
|
+
stream.push({
|
|
517
|
+
type: "thinking_end",
|
|
518
|
+
contentIndex: slot.contentIndex,
|
|
519
|
+
content: slot.block.thinking,
|
|
520
|
+
partial: output
|
|
521
|
+
});
|
|
522
|
+
outputSlots.delete(event.output_index);
|
|
523
|
+
} else if (item.type === "message" && slot?.type === "text") {
|
|
524
|
+
slot.block.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
|
|
525
|
+
slot.block.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
|
|
526
|
+
stream.push({
|
|
527
|
+
type: "text_end",
|
|
528
|
+
contentIndex: slot.contentIndex,
|
|
529
|
+
content: slot.block.text,
|
|
530
|
+
partial: output
|
|
531
|
+
});
|
|
532
|
+
outputSlots.delete(event.output_index);
|
|
533
|
+
} else if (item.type === "function_call" && slot?.type === "toolCall" && slot.block.partialJson !== void 0) {
|
|
534
|
+
slot.block.arguments = parseStreamingJson(item.arguments || slot.block.partialJson || "{}");
|
|
535
|
+
if (item.namespace !== void 0) slot.block.namespace = item.namespace;
|
|
536
|
+
delete slot.block.partialJson;
|
|
537
|
+
stream.push({
|
|
538
|
+
type: "toolcall_end",
|
|
539
|
+
contentIndex: slot.contentIndex,
|
|
540
|
+
toolCall: slot.block,
|
|
541
|
+
partial: output
|
|
542
|
+
});
|
|
543
|
+
outputSlots.delete(event.output_index);
|
|
544
|
+
} else if (item.type === "custom_tool_call" && slot?.type === "toolCall" && slot.block.customInput) {
|
|
545
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, item.input ?? getCustomToolCallInput(slot.block), true));
|
|
546
|
+
if (item.namespace !== void 0) slot.block.namespace = item.namespace;
|
|
547
|
+
delete slot.block.customInput;
|
|
548
|
+
stream.push({
|
|
549
|
+
type: "toolcall_end",
|
|
550
|
+
contentIndex: slot.contentIndex,
|
|
551
|
+
toolCall: slot.block,
|
|
552
|
+
partial: output
|
|
553
|
+
});
|
|
554
|
+
outputSlots.delete(event.output_index);
|
|
555
|
+
}
|
|
556
|
+
} else if (event.type === "response.completed" || event.type === "response.incomplete") finalizeResponse(event.response);
|
|
557
|
+
else if (event.type === "error") throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
|
|
558
|
+
else if (event.type === "response.failed") {
|
|
559
|
+
sawTerminalResponseEvent = true;
|
|
560
|
+
output.rawStopReason = event.response?.status;
|
|
561
|
+
const error = event.response?.error;
|
|
562
|
+
const details = event.response?.incomplete_details;
|
|
563
|
+
const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
|
|
564
|
+
throw new Error(msg);
|
|
565
|
+
}
|
|
566
|
+
if (!sawTerminalResponseEvent) throw new Error("OpenAI Responses stream ended before a terminal response event");
|
|
567
|
+
}
|
|
568
|
+
function mapStopReason(status, incompleteReason) {
|
|
569
|
+
if (!status) return { stopReason: "stop" };
|
|
570
|
+
switch (status) {
|
|
571
|
+
case "completed": return { stopReason: "stop" };
|
|
572
|
+
case "incomplete":
|
|
573
|
+
if (incompleteReason === "max_output_tokens") return { stopReason: "length" };
|
|
574
|
+
return {
|
|
575
|
+
stopReason: "error",
|
|
576
|
+
errorMessage: incompleteReason ? `Response incomplete: ${incompleteReason}` : "Response incomplete without a provider reason"
|
|
577
|
+
};
|
|
578
|
+
case "failed":
|
|
579
|
+
case "cancelled": return { stopReason: "error" };
|
|
580
|
+
case "in_progress":
|
|
581
|
+
case "queued": return { stopReason: "stop" };
|
|
582
|
+
default: throw new Error(`Unhandled stop reason: ${status}`);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
//#endregion
|
|
586
|
+
//#region node_modules/@earendil-works/pi-ai/dist/api/openai-responses.js
|
|
587
|
+
const OPENAI_TOOL_CALL_PROVIDERS = /* @__PURE__ */ new Set([
|
|
588
|
+
"openai",
|
|
589
|
+
"openai-codex",
|
|
590
|
+
"opencode"
|
|
591
|
+
]);
|
|
592
|
+
const OPENAI_RESPONSES_MIN_OUTPUT_TOKENS = 16;
|
|
593
|
+
function hasHeader(headers, name) {
|
|
594
|
+
if (!headers) return false;
|
|
595
|
+
const expected = name.toLowerCase();
|
|
596
|
+
for (const [key, value] of Object.entries(headers)) if (key.toLowerCase() === expected && value !== null && value.trim().length > 0) return true;
|
|
597
|
+
return false;
|
|
598
|
+
}
|
|
599
|
+
function getClientApiKey(provider, apiKey, headers) {
|
|
600
|
+
if (apiKey) return apiKey;
|
|
601
|
+
if (hasHeader(headers, "authorization") || hasHeader(headers, "cf-aig-authorization")) return "unused";
|
|
602
|
+
throw new Error(`No API key for provider: ${provider}`);
|
|
603
|
+
}
|
|
604
|
+
function detectSessionAffinityFormat(model) {
|
|
605
|
+
return model.provider === "openrouter" || model.baseUrl.includes("openrouter.ai") ? "openrouter" : "openai";
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Resolve cache retention preference.
|
|
609
|
+
* Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
|
|
610
|
+
*/
|
|
611
|
+
function resolveCacheRetention(cacheRetention, env) {
|
|
612
|
+
if (cacheRetention) return cacheRetention;
|
|
613
|
+
if (getProviderEnvValue("PI_CACHE_RETENTION", env) === "long") return "long";
|
|
614
|
+
return "short";
|
|
615
|
+
}
|
|
616
|
+
function getCompat(model) {
|
|
617
|
+
return {
|
|
618
|
+
supportsDeveloperRole: model.compat?.supportsDeveloperRole ?? true,
|
|
619
|
+
supportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,
|
|
620
|
+
sessionAffinityFormat: model.compat?.sessionAffinityFormat ?? detectSessionAffinityFormat(model),
|
|
621
|
+
supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true,
|
|
622
|
+
supportsStrictMode: model.compat?.supportsStrictMode ?? false,
|
|
623
|
+
supportsOpenAIGrammarTools: model.compat?.supportsOpenAIGrammarTools ?? false,
|
|
624
|
+
supportsAdditionalTools: model.compat?.supportsAdditionalTools ?? false,
|
|
625
|
+
supportsToolSearch: model.compat?.supportsToolSearch ?? false,
|
|
626
|
+
supportsExplicitPromptCacheMode: model.compat?.supportsExplicitPromptCacheMode ?? false,
|
|
627
|
+
supportsMaxOutputTokens: model.compat?.supportsMaxOutputTokens ?? true
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
function getPromptCacheRetention(compat, cacheRetention) {
|
|
631
|
+
return cacheRetention === "long" && compat.supportsLongCacheRetention && !compat.supportsExplicitPromptCacheMode ? "24h" : void 0;
|
|
632
|
+
}
|
|
633
|
+
function getPromptCacheOptions(compat, cacheRetention) {
|
|
634
|
+
if (!compat.supportsExplicitPromptCacheMode) return void 0;
|
|
635
|
+
if (cacheRetention === "none") return { mode: "explicit" };
|
|
636
|
+
if (cacheRetention === "long" && compat.supportsLongCacheRetention) return { ttl: "30m" };
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Generate function for OpenAI Responses API
|
|
640
|
+
*/
|
|
641
|
+
const stream = (model, context, options) => {
|
|
642
|
+
const stream = new AssistantMessageEventStream();
|
|
643
|
+
const normalizedContext = resolveTranscript(context, getCompat(model).supportsMidConvoSystemMessages);
|
|
644
|
+
(async () => {
|
|
645
|
+
const output = {
|
|
646
|
+
role: "assistant",
|
|
647
|
+
content: [],
|
|
648
|
+
api: model.api,
|
|
649
|
+
provider: model.provider,
|
|
650
|
+
model: model.id,
|
|
651
|
+
usage: {
|
|
652
|
+
input: 0,
|
|
653
|
+
output: 0,
|
|
654
|
+
cacheRead: 0,
|
|
655
|
+
cacheWrite: 0,
|
|
656
|
+
totalTokens: 0,
|
|
657
|
+
cost: {
|
|
658
|
+
input: 0,
|
|
659
|
+
output: 0,
|
|
660
|
+
cacheRead: 0,
|
|
661
|
+
cacheWrite: 0,
|
|
662
|
+
total: 0
|
|
663
|
+
}
|
|
664
|
+
},
|
|
665
|
+
stopReason: "pending",
|
|
666
|
+
timestamp: Date.now()
|
|
667
|
+
};
|
|
668
|
+
try {
|
|
669
|
+
const apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
670
|
+
const cacheSessionId = resolveCacheRetention(options?.cacheRetention, options?.env) === "none" ? void 0 : options?.sessionId;
|
|
671
|
+
const compat = getCompat(model);
|
|
672
|
+
const grammarToolInputProperties = createGrammarToolInputProperties(getDeclaredTools(normalizedContext.messages), compat.supportsOpenAIGrammarTools);
|
|
673
|
+
const client = createClient(model, normalizedContext, apiKey, options?.headers, options?.fetch, cacheSessionId);
|
|
674
|
+
let params = buildParams(model, normalizedContext, options, compat, grammarToolInputProperties);
|
|
675
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
676
|
+
if (nextParams !== void 0) params = nextParams;
|
|
677
|
+
const requestOptions = {
|
|
678
|
+
...options?.signal ? { signal: options.signal } : {},
|
|
679
|
+
...options?.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {},
|
|
680
|
+
maxRetries: 0
|
|
681
|
+
};
|
|
682
|
+
const { data: openaiStream, response } = await retryProviderRequest(() => client.responses.create(params, requestOptions).withResponse(), {
|
|
683
|
+
maxRetries: options?.maxRetries,
|
|
684
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
685
|
+
signal: options?.signal
|
|
686
|
+
});
|
|
687
|
+
await options?.onResponse?.({
|
|
688
|
+
status: response.status,
|
|
689
|
+
headers: headersToRecord(response.headers)
|
|
690
|
+
}, model);
|
|
691
|
+
stream.push({
|
|
692
|
+
type: "start",
|
|
693
|
+
partial: output
|
|
694
|
+
});
|
|
695
|
+
await processResponsesStream(openaiStream, output, stream, model, {
|
|
696
|
+
serviceTier: options?.serviceTier,
|
|
697
|
+
grammarToolInputProperties,
|
|
698
|
+
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model)
|
|
699
|
+
});
|
|
700
|
+
if (options?.signal?.aborted) throw new Error("Request was aborted");
|
|
701
|
+
if (output.stopReason === "pending") throw new Error("OpenAI Responses stream ended without a stop reason");
|
|
702
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage || "An unknown error occurred");
|
|
703
|
+
stream.push({
|
|
704
|
+
type: "done",
|
|
705
|
+
reason: output.stopReason,
|
|
706
|
+
message: output
|
|
707
|
+
});
|
|
708
|
+
stream.end();
|
|
709
|
+
} catch (error) {
|
|
710
|
+
for (const block of output.content) {
|
|
711
|
+
delete block.index;
|
|
712
|
+
delete block.partialJson;
|
|
713
|
+
delete block.customInput;
|
|
714
|
+
}
|
|
715
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
716
|
+
output.errorMessage = formatProviderError(normalizeProviderError(error), `${model.provider === "openai" ? "OpenAI" : model.provider} API error`);
|
|
717
|
+
stream.push({
|
|
718
|
+
type: "error",
|
|
719
|
+
reason: output.stopReason,
|
|
720
|
+
error: output
|
|
721
|
+
});
|
|
722
|
+
stream.end();
|
|
723
|
+
}
|
|
724
|
+
})();
|
|
725
|
+
return stream;
|
|
726
|
+
};
|
|
727
|
+
const streamSimple = (model, context, options) => {
|
|
728
|
+
getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
729
|
+
const base = {
|
|
730
|
+
...buildBaseOptions(model, context, options, options?.apiKey),
|
|
731
|
+
toolChoice: options?.toolChoice
|
|
732
|
+
};
|
|
733
|
+
const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : void 0;
|
|
734
|
+
const reasoningEffort = clampedReasoning === "off" ? void 0 : clampedReasoning;
|
|
735
|
+
return stream(model, context, {
|
|
736
|
+
...base,
|
|
737
|
+
reasoningEffort
|
|
738
|
+
});
|
|
739
|
+
};
|
|
740
|
+
function createClient(model, context, apiKey, optionsHeaders, fetch, sessionId) {
|
|
741
|
+
const compat = getCompat(model);
|
|
742
|
+
const headers = {
|
|
743
|
+
"User-Agent": getPiUserAgent(),
|
|
744
|
+
...model.headers
|
|
745
|
+
};
|
|
746
|
+
if (model.provider === "github-copilot") {
|
|
747
|
+
const hasImages = hasCopilotVisionInput(context.messages);
|
|
748
|
+
const copilotHeaders = buildCopilotDynamicHeaders({
|
|
749
|
+
messages: context.messages,
|
|
750
|
+
hasImages
|
|
751
|
+
});
|
|
752
|
+
Object.assign(headers, copilotHeaders);
|
|
753
|
+
}
|
|
754
|
+
if (sessionId) {
|
|
755
|
+
if (compat.sessionAffinityFormat === "openrouter") headers["x-session-id"] = sessionId;
|
|
756
|
+
else {
|
|
757
|
+
if (compat.sessionAffinityFormat === "openai") headers.session_id = sessionId;
|
|
758
|
+
headers["x-client-request-id"] = sessionId;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (optionsHeaders) Object.assign(headers, optionsHeaders);
|
|
762
|
+
return new OpenAI({
|
|
763
|
+
apiKey,
|
|
764
|
+
baseURL: model.baseUrl,
|
|
765
|
+
dangerouslyAllowBrowser: true,
|
|
766
|
+
fetch,
|
|
767
|
+
defaultHeaders: headers
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
function buildParams(model, context, options, compat = getCompat(model), grammarToolInputProperties = createGrammarToolInputProperties(getDeclaredTools(context.messages), compat.supportsOpenAIGrammarTools)) {
|
|
771
|
+
const transcriptTools = resolveTranscriptTools(context.messages, compat.supportsAdditionalTools || compat.supportsToolSearch);
|
|
772
|
+
const messages = convertResponsesMessages(model, context, OPENAI_TOOL_CALL_PROVIDERS, {
|
|
773
|
+
grammarToolInputProperties,
|
|
774
|
+
supportsMidConvoSystemMessages: compat.supportsMidConvoSystemMessages,
|
|
775
|
+
supportsAdditionalTools: compat.supportsAdditionalTools,
|
|
776
|
+
supportsToolSearch: compat.supportsToolSearch,
|
|
777
|
+
toolOptions: {
|
|
778
|
+
supportsStrictMode: compat.supportsStrictMode,
|
|
779
|
+
supportsOpenAIGrammarTools: compat.supportsOpenAIGrammarTools
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
783
|
+
const params = {
|
|
784
|
+
model: model.id,
|
|
785
|
+
input: messages,
|
|
786
|
+
stream: true,
|
|
787
|
+
prompt_cache_key: cacheRetention === "none" ? void 0 : clampOpenAIPromptCacheKey(options?.sessionId),
|
|
788
|
+
prompt_cache_retention: getPromptCacheRetention(compat, cacheRetention),
|
|
789
|
+
prompt_cache_options: getPromptCacheOptions(compat, cacheRetention),
|
|
790
|
+
store: false
|
|
791
|
+
};
|
|
792
|
+
if (options?.maxTokens && compat.supportsMaxOutputTokens) params.max_output_tokens = Math.max(options.maxTokens, OPENAI_RESPONSES_MIN_OUTPUT_TOKENS);
|
|
793
|
+
if (options?.temperature !== void 0) params.temperature = options?.temperature;
|
|
794
|
+
if (options?.serviceTier !== void 0) params.service_tier = options.serviceTier;
|
|
795
|
+
if (transcriptTools.requestTools.length > 0) params.tools = convertResponsesTools(transcriptTools.requestTools, {
|
|
796
|
+
supportsStrictMode: compat.supportsStrictMode,
|
|
797
|
+
supportsOpenAIGrammarTools: compat.supportsOpenAIGrammarTools
|
|
798
|
+
});
|
|
799
|
+
if (options?.toolChoice !== void 0) params.tool_choice = options.toolChoice;
|
|
800
|
+
if (model.reasoning) {
|
|
801
|
+
if (options?.reasoningEffort || options?.reasoningSummary) {
|
|
802
|
+
params.reasoning = {
|
|
803
|
+
effort: options?.reasoningEffort ? model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort : "medium",
|
|
804
|
+
summary: options?.reasoningSummary || "auto"
|
|
805
|
+
};
|
|
806
|
+
params.include = ["reasoning.encrypted_content"];
|
|
807
|
+
} else if (model.provider !== "github-copilot" && model.thinkingLevelMap?.off !== null) params.reasoning = { effort: model.thinkingLevelMap?.off ?? "none" };
|
|
808
|
+
if (model.provider === "xai") params.include = ["reasoning.encrypted_content"];
|
|
809
|
+
}
|
|
810
|
+
if (options?.samplingParams) Object.assign(params, options.samplingParams);
|
|
811
|
+
return params;
|
|
812
|
+
}
|
|
813
|
+
function getServiceTierCostMultiplier(model, serviceTier) {
|
|
814
|
+
switch (serviceTier) {
|
|
815
|
+
case "flex": return .5;
|
|
816
|
+
case "priority": return model.id === "gpt-5.5" ? 2.5 : 2;
|
|
817
|
+
default: return 1;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
function applyServiceTierPricing(usage, serviceTier, model) {
|
|
821
|
+
const multiplier = getServiceTierCostMultiplier(model, serviceTier);
|
|
822
|
+
if (multiplier === 1) return;
|
|
823
|
+
usage.cost.input *= multiplier;
|
|
824
|
+
usage.cost.output *= multiplier;
|
|
825
|
+
usage.cost.cacheRead *= multiplier;
|
|
826
|
+
usage.cost.cacheWrite *= multiplier;
|
|
827
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
828
|
+
}
|
|
829
|
+
//#endregion
|
|
830
|
+
export { stream, streamSimple };
|
|
831
|
+
|
|
832
|
+
//# sourceMappingURL=openai-responses-DygWnsye.js.map
|