@openclaw/ai 2026.7.2-beta.4 → 2026.7.2-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{anthropic-CrUDIBpM.mjs → anthropic-CH4UUnZr.mjs} +26 -441
- package/dist/{anthropic-BoTnz8cv.d.mts → anthropic-SrGtwsJu.d.mts} +27 -1
- package/dist/anthropic-usage-DWU-x8MI.mjs +459 -0
- package/dist/{api-registry-BMphkFf1.d.mts → api-registry-DlMgPR39.d.mts} +1 -1
- package/dist/{azure-openai-responses-CbrpVeEv.mjs → azure-openai-responses-CImcwB83.mjs} +4 -4
- package/dist/cache-retention-0x979a5V.mjs +12 -0
- package/dist/deferred-event-buffer-DAvyP7qA.mjs +19 -0
- package/dist/error-coercion-DgxlWC0n.mjs +15 -0
- package/dist/{event-stream-Douf9dob.d.mts → event-stream-YjaPW20U.d.mts} +1 -1
- package/dist/event-stream.d.mts +1 -1
- package/dist/{github-copilot-headers-BsH5cqGj.mjs → github-copilot-headers-NCJtz9i0.mjs} +1 -12
- package/dist/{google-Cs62KA7t.mjs → google-CtSg0iTS.mjs} +3 -3
- package/dist/{google-shared-CMLI-tCZ.mjs → google-shared-DNBz5rcD.mjs} +7 -4
- package/dist/{google-vertex-B4SD3U0f.mjs → google-vertex-31f1uS9L.mjs} +3 -3
- package/dist/{host-WvWBo4h8.d.mts → host-B9GUmcra.d.mts} +2 -2
- package/dist/host-Dog2WQiR.mjs +369 -0
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +3 -3
- package/dist/internal/anthropic.d.mts +16 -62
- package/dist/internal/anthropic.mjs +5 -4
- package/dist/internal/openai.d.mts +260 -2
- package/dist/internal/openai.mjs +7 -5
- package/dist/internal/runtime.d.mts +8 -32
- package/dist/internal/runtime.mjs +7 -6
- package/dist/internal/shared.d.mts +14 -9
- package/dist/internal/shared.mjs +6 -2
- package/dist/{llm-request-activity-CehVkZP-.mjs → llm-request-activity-BjtkplhG.mjs} +1 -19
- package/dist/{mistral-D5Ps7Led.mjs → mistral-CWmpvWYh.mjs} +7 -4
- package/dist/number-coercion-DvG7SNMg.mjs +129 -0
- package/dist/{openai-chatgpt-responses-h0o5yV8Y.mjs → openai-chatgpt-responses-B84Ibtrd.mjs} +33 -29
- package/dist/{openai-completions-exO8NFQf.mjs → openai-completions-DsOxhOD1.mjs} +28 -482
- package/dist/openai-completions-compat-DBWjXoMZ.d.mts +43 -0
- package/dist/openai-prompt-cache-2uo_1OR1.d.mts +7 -0
- package/dist/openai-prompt-cache-mZTCdRPo.mjs +12 -0
- package/dist/openai-reasoning-compat-YgeLncHw.mjs +396 -0
- package/dist/{openai-responses-BHpmtUKo.mjs → openai-responses-BT7A3sLu.mjs} +7 -5
- package/dist/openai-responses-shared-pXl6Wd8S.mjs +392 -0
- package/dist/{openai-responses-shared-CzCurmY1.mjs → openai-responses-stream-internal-Cw5txaGW.mjs} +1609 -1037
- package/dist/{openai-tool-projection-ITOU9bG1.mjs → openai-tool-projection-OhX64DoP.mjs} +26 -11
- package/dist/prompt-cache-stability-Cwcjv_fx.d.mts +13 -0
- package/dist/{provider-error-C4VvV_3t.mjs → provider-error-CAEvRjry.mjs} +8 -2
- package/dist/provider-options-D8bB3z9b.d.mts +144 -0
- package/dist/providers.d.mts +3 -2
- package/dist/providers.mjs +10 -9
- package/dist/reasoning-tag-text-partitioner-CGDyLWUR.mjs +12209 -0
- package/dist/simple-options-9lhRrN73.mjs +50 -0
- package/dist/{src-CXno1H5g.mjs → src-QkygScBs.mjs} +23 -4
- package/dist/stream-first-event-timeout-BBys9hSb.mjs +86 -0
- package/dist/stream-first-event-timeout-DvDeSucC.d.mts +29 -0
- package/dist/tls-certificate-errors-DXSpluKI.mjs +93 -0
- package/dist/tool-result-text-CTpIRbYd.mjs +225 -0
- package/dist/transform-messages-C8mBqZxF.mjs +2 -0
- package/dist/transport-stream-shared-D81p90xq.mjs +297 -0
- package/dist/transports.d.mts +115 -132
- package/dist/transports.mjs +400 -1991
- package/dist/{types-AFwwWium.d.mts → types-bzp5k29J.d.mts} +7 -0
- package/dist/types.d.mts +5 -5
- package/dist/types.mjs +2 -2
- package/dist/{validation-sxvxC8J-.d.mts → validation-B-j7cOYp.d.mts} +1 -1
- package/dist/validation.d.mts +1 -1
- package/package.json +4 -5
- package/dist/host-XYGZcgO8.mjs +0 -98
- package/dist/model-utils-1GiZ2_rr.mjs +0 -64
- package/dist/openai-CoGicoDt.d.mts +0 -332
- package/dist/reasoning-tag-text-partitioner-BlVe67g6.mjs +0 -400
- package/dist/stream-first-event-timeout-DP4xEyBY.mjs +0 -150
- package/dist/system-prompt-cache-boundary-CbHeV4_l.mjs +0 -526
- package/npm-shrinkwrap.json +0 -638
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { i as transformMessages, n as getAiTransportHost } from "./host-Dog2WQiR.mjs";
|
|
2
|
+
import { t as sanitizeSurrogates } from "./sanitize-unicode-DT5o51ur.mjs";
|
|
3
|
+
import { a as isImageWithMediaPayload, d as stripSystemPromptCacheBoundary, m as sortPromptCacheToolsByName, r as extractToolResultText, t as describeToolResultMediaPlaceholder } from "./tool-result-text-CTpIRbYd.mjs";
|
|
4
|
+
import { t as projectOpenAITools } from "./openai-tool-projection-OhX64DoP.mjs";
|
|
5
|
+
import { l as clampThinkingLevel } from "./number-coercion-DvG7SNMg.mjs";
|
|
6
|
+
import { d as transportAbortError } from "./transport-stream-shared-D81p90xq.mjs";
|
|
7
|
+
import { Y as normalizeOpenAIStrictToolParameters, Z as resolveOpenAIProjectedToolsStrictToolFlag, _t as supportsOpenAITemperature, gt as supportsOpenAIReasoningEffort, mt as resolveOpenAIReasoningEffortForModel, n as processResponsesStream, q as findOpenAIStrictToolProjectionDiagnostics } from "./openai-responses-stream-internal-Cw5txaGW.mjs";
|
|
8
|
+
import { i as getFirstStreamEventTimeoutMs, r as getFirstStreamEventTimeoutHandler, t as createFirstStreamEventAbortController } from "./stream-first-event-timeout-BBys9hSb.mjs";
|
|
9
|
+
import { t as shortHash } from "./hash-CHgqbJmD.mjs";
|
|
10
|
+
import { t as headersToRecord } from "./headers-B_e4-1J0.mjs";
|
|
11
|
+
import "./transform-messages-C8mBqZxF.mjs";
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
//#region packages/ai/src/providers/openai-responses-tools.ts
|
|
14
|
+
const LOG_SUBSYSTEM = "llm/openai-responses";
|
|
15
|
+
const MAX_STRICT_TOOL_DOWNGRADE_DIAGNOSTIC_KEYS = 64;
|
|
16
|
+
const loggedStrictToolDowngradeDiagnosticKeys = /* @__PURE__ */ new Set();
|
|
17
|
+
/** Converts and returns the projection used to reconcile tool choices. */
|
|
18
|
+
function convertResponsesToolPayload(tools, options) {
|
|
19
|
+
const projection = projectOpenAITools(tools);
|
|
20
|
+
const strict = resolveResponsesStrictToolFlag(projection, resolveResponsesStrictToolSetting(options), options?.model);
|
|
21
|
+
return {
|
|
22
|
+
projection,
|
|
23
|
+
tools: sortPromptCacheToolsByName(projection.tools).map((tool) => {
|
|
24
|
+
const result = {
|
|
25
|
+
type: "function",
|
|
26
|
+
name: tool.name,
|
|
27
|
+
description: tool.description,
|
|
28
|
+
parameters: normalizeOpenAIStrictToolParameters(tool.parameters, strict === true, options?.model?.compat)
|
|
29
|
+
};
|
|
30
|
+
if (strict !== void 0) result.strict = strict;
|
|
31
|
+
return result;
|
|
32
|
+
})
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function resolveResponsesStrictToolSetting(options) {
|
|
36
|
+
if (options?.strict !== void 0) return options.strict;
|
|
37
|
+
if (options?.model) return getAiTransportHost().resolveOpenAIStrictToolSetting(options.model, {
|
|
38
|
+
transport: "stream",
|
|
39
|
+
supportsStrictMode: options.supportsStrictMode
|
|
40
|
+
});
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
function resolveResponsesStrictToolFlag(projection, strictSetting, model) {
|
|
44
|
+
const strict = resolveOpenAIProjectedToolsStrictToolFlag(projection, strictSetting);
|
|
45
|
+
if (strictSetting === true && strict === false && model) getAiTransportHost().logDebug(LOG_SUBSYSTEM, () => {
|
|
46
|
+
const diagnostics = findOpenAIStrictToolProjectionDiagnostics(projection);
|
|
47
|
+
if (!shouldLogStrictToolDowngradeDiagnostic(diagnostics, model)) return null;
|
|
48
|
+
const sample = diagnostics.slice(0, 5).map((entry) => ({
|
|
49
|
+
tool: entry.toolName ?? `tool[${entry.toolIndex}]`,
|
|
50
|
+
violations: entry.violations.slice(0, 8)
|
|
51
|
+
}));
|
|
52
|
+
return {
|
|
53
|
+
message: `OpenAI responses tool schema strict mode downgraded to strict=false for ${model.provider ?? "unknown"}/${model.id ?? "unknown"} because ${diagnostics.length} tool schema(s) are not strict-compatible`,
|
|
54
|
+
data: {
|
|
55
|
+
provider: model.provider,
|
|
56
|
+
model: model.id,
|
|
57
|
+
incompatibleToolCount: diagnostics.length,
|
|
58
|
+
sample
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
return strict;
|
|
63
|
+
}
|
|
64
|
+
function shouldLogStrictToolDowngradeDiagnostic(diagnostics, model) {
|
|
65
|
+
const key = createHash("sha256").update(JSON.stringify({
|
|
66
|
+
provider: model.provider,
|
|
67
|
+
model: model.id,
|
|
68
|
+
diagnostics: diagnostics.map((entry) => ({
|
|
69
|
+
toolIndex: entry.toolIndex,
|
|
70
|
+
toolName: entry.toolName ?? null,
|
|
71
|
+
violations: entry.violations
|
|
72
|
+
}))
|
|
73
|
+
})).digest("hex");
|
|
74
|
+
if (loggedStrictToolDowngradeDiagnosticKeys.has(key)) return false;
|
|
75
|
+
if (loggedStrictToolDowngradeDiagnosticKeys.size >= MAX_STRICT_TOOL_DOWNGRADE_DIAGNOSTIC_KEYS) loggedStrictToolDowngradeDiagnosticKeys.clear();
|
|
76
|
+
loggedStrictToolDowngradeDiagnosticKeys.add(key);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region packages/ai/src/providers/openai-responses-shared.ts
|
|
81
|
+
const EMPTY_TOOL_RESULT_TEXT = "(no output)";
|
|
82
|
+
function splitResponsesToolCallId(id) {
|
|
83
|
+
const separatorIndex = id.indexOf("|");
|
|
84
|
+
return separatorIndex === -1 ? [id, void 0] : [id.slice(0, separatorIndex), id.slice(separatorIndex + 1)];
|
|
85
|
+
}
|
|
86
|
+
function sanitizeToolResultText(text, fallback) {
|
|
87
|
+
const sanitized = sanitizeSurrogates(text);
|
|
88
|
+
return sanitized.trim().length > 0 ? sanitized : fallback;
|
|
89
|
+
}
|
|
90
|
+
function normalizeResponsesReasoningReplayItem(params) {
|
|
91
|
+
const next = { ...params.item };
|
|
92
|
+
if (!Array.isArray(next.summary)) next.summary = [];
|
|
93
|
+
if (!params.replayResponsesItemIds) delete next.id;
|
|
94
|
+
return next;
|
|
95
|
+
}
|
|
96
|
+
function parseTextSignature(signature) {
|
|
97
|
+
if (!signature) return;
|
|
98
|
+
if (signature.startsWith("{")) try {
|
|
99
|
+
const parsed = JSON.parse(signature);
|
|
100
|
+
if (parsed.v === 1) {
|
|
101
|
+
const id = typeof parsed.id === "string" ? parsed.id : void 0;
|
|
102
|
+
const phase = parsed.phase === "commentary" || parsed.phase === "final_answer" ? parsed.phase : void 0;
|
|
103
|
+
if (id !== void 0 || phase !== void 0) return {
|
|
104
|
+
id,
|
|
105
|
+
phase
|
|
106
|
+
};
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
} catch {}
|
|
110
|
+
return { id: signature };
|
|
111
|
+
}
|
|
112
|
+
function resolveReplayableResponsesMessageId(params) {
|
|
113
|
+
if (!params.textSignatureId) return params.fallbackOrdinal === 0 ? params.fallbackId : `${params.fallbackId}_${params.fallbackOrdinal}`;
|
|
114
|
+
return params.previousReplayItemWasReasoning ? params.textSignatureId : void 0;
|
|
115
|
+
}
|
|
116
|
+
function isResponsesReasoningEffort(effort) {
|
|
117
|
+
return effort === "minimal" || effort === "low" || effort === "medium" || effort === "high" || effort === "xhigh" || effort === "max";
|
|
118
|
+
}
|
|
119
|
+
function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
|
|
120
|
+
const messages = [];
|
|
121
|
+
const shouldReplayResponsesItemIds = options?.replayResponsesItemIds ?? true;
|
|
122
|
+
const normalizeIdPart = (part) => {
|
|
123
|
+
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
124
|
+
return (sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized).replace(/_+$/, "");
|
|
125
|
+
};
|
|
126
|
+
const buildForeignResponsesItemId = (itemId) => {
|
|
127
|
+
const normalized = `fc_${shortHash(itemId)}`;
|
|
128
|
+
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
|
|
129
|
+
};
|
|
130
|
+
const normalizeToolCallId = (id, targetModel, source) => {
|
|
131
|
+
if (!allowedToolCallProviders.has(model.provider)) return normalizeIdPart(id);
|
|
132
|
+
if (!id.includes("|")) return normalizeIdPart(id);
|
|
133
|
+
const [callId, itemId = ""] = splitResponsesToolCallId(id);
|
|
134
|
+
const normalizedCallId = normalizeIdPart(callId);
|
|
135
|
+
let normalizedItemId = source.provider !== model.provider || source.api !== model.api ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
|
|
136
|
+
if (!normalizedItemId.startsWith("fc_")) normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
|
|
137
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
138
|
+
};
|
|
139
|
+
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
140
|
+
if ((options?.includeSystemPrompt ?? true) && context.systemPrompt) {
|
|
141
|
+
const compat = model.compat;
|
|
142
|
+
const role = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
|
|
143
|
+
messages.push({
|
|
144
|
+
type: "message",
|
|
145
|
+
role,
|
|
146
|
+
content: [{
|
|
147
|
+
type: "input_text",
|
|
148
|
+
text: sanitizeSurrogates(stripSystemPromptCacheBoundary(context.systemPrompt))
|
|
149
|
+
}]
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
let msgIndex = 0;
|
|
153
|
+
for (const msg of transformedMessages) {
|
|
154
|
+
if (msg.role === "user") if (typeof msg.content === "string") messages.push({
|
|
155
|
+
type: "message",
|
|
156
|
+
role: "user",
|
|
157
|
+
content: [{
|
|
158
|
+
type: "input_text",
|
|
159
|
+
text: sanitizeSurrogates(msg.content)
|
|
160
|
+
}]
|
|
161
|
+
});
|
|
162
|
+
else {
|
|
163
|
+
const content = msg.content.map((item) => {
|
|
164
|
+
if (item.type === "text") return {
|
|
165
|
+
type: "input_text",
|
|
166
|
+
text: sanitizeSurrogates(item.text)
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
type: "input_image",
|
|
170
|
+
detail: "auto",
|
|
171
|
+
image_url: `data:${item.mimeType};base64,${item.data}`
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
if (content.length === 0) continue;
|
|
175
|
+
messages.push({
|
|
176
|
+
type: "message",
|
|
177
|
+
role: "user",
|
|
178
|
+
content
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
else if (msg.role === "assistant") {
|
|
182
|
+
const output = [];
|
|
183
|
+
let textFallbackOrdinal = 0;
|
|
184
|
+
const assistantMsg = msg;
|
|
185
|
+
let previousReplayItemWasReasoning = false;
|
|
186
|
+
const isDifferentModel = assistantMsg.model !== model.id && assistantMsg.provider === model.provider && assistantMsg.api === model.api;
|
|
187
|
+
for (const block of msg.content) if (block.type === "thinking") {
|
|
188
|
+
if (block.thinkingSignature) {
|
|
189
|
+
const reasoningItem = normalizeResponsesReasoningReplayItem({
|
|
190
|
+
item: JSON.parse(block.thinkingSignature),
|
|
191
|
+
replayResponsesItemIds: shouldReplayResponsesItemIds
|
|
192
|
+
});
|
|
193
|
+
output.push(reasoningItem);
|
|
194
|
+
previousReplayItemWasReasoning = true;
|
|
195
|
+
}
|
|
196
|
+
} else if (block.type === "text") {
|
|
197
|
+
const textBlock = block;
|
|
198
|
+
const parsedSignature = parseTextSignature(textBlock.textSignature);
|
|
199
|
+
let msgId = shouldReplayResponsesItemIds ? resolveReplayableResponsesMessageId({
|
|
200
|
+
textSignatureId: parsedSignature?.id,
|
|
201
|
+
fallbackId: `msg_${msgIndex}`,
|
|
202
|
+
fallbackOrdinal: textFallbackOrdinal,
|
|
203
|
+
previousReplayItemWasReasoning
|
|
204
|
+
}) : void 0;
|
|
205
|
+
if (!parsedSignature?.id) textFallbackOrdinal += 1;
|
|
206
|
+
if (msgId && msgId.length > 64) msgId = `msg_${shortHash(msgId)}`;
|
|
207
|
+
const messageItem = {
|
|
208
|
+
type: "message",
|
|
209
|
+
role: "assistant",
|
|
210
|
+
content: [{
|
|
211
|
+
type: "output_text",
|
|
212
|
+
text: sanitizeSurrogates(textBlock.text),
|
|
213
|
+
annotations: []
|
|
214
|
+
}],
|
|
215
|
+
status: "completed",
|
|
216
|
+
...msgId ? { id: msgId } : {},
|
|
217
|
+
phase: parsedSignature?.phase
|
|
218
|
+
};
|
|
219
|
+
output.push(messageItem);
|
|
220
|
+
previousReplayItemWasReasoning = false;
|
|
221
|
+
} else if (block.type === "toolCall") {
|
|
222
|
+
const toolCall = block;
|
|
223
|
+
const [callId, itemIdRaw] = splitResponsesToolCallId(toolCall.id);
|
|
224
|
+
let itemId = shouldReplayResponsesItemIds ? itemIdRaw : void 0;
|
|
225
|
+
if (shouldReplayResponsesItemIds && isDifferentModel && itemId?.startsWith("fc_")) itemId = void 0;
|
|
226
|
+
output.push({
|
|
227
|
+
type: "function_call",
|
|
228
|
+
...itemId ? { id: itemId } : {},
|
|
229
|
+
call_id: callId,
|
|
230
|
+
name: toolCall.name,
|
|
231
|
+
arguments: JSON.stringify(toolCall.arguments)
|
|
232
|
+
});
|
|
233
|
+
previousReplayItemWasReasoning = false;
|
|
234
|
+
}
|
|
235
|
+
if (output.length === 0) continue;
|
|
236
|
+
messages.push(...output);
|
|
237
|
+
} else if (msg.role === "toolResult") {
|
|
238
|
+
const textResult = extractToolResultText(msg.content);
|
|
239
|
+
const sanitizedTextResult = sanitizeSurrogates(textResult);
|
|
240
|
+
const hasImages = msg.content.some(isImageWithMediaPayload);
|
|
241
|
+
const mediaPlaceholder = describeToolResultMediaPlaceholder(msg.content);
|
|
242
|
+
const hasText = sanitizedTextResult.trim().length > 0;
|
|
243
|
+
const [callId] = splitResponsesToolCallId(msg.toolCallId);
|
|
244
|
+
let output;
|
|
245
|
+
if (hasImages && model.input.includes("image")) {
|
|
246
|
+
const contentParts = [];
|
|
247
|
+
if (hasText) contentParts.push({
|
|
248
|
+
type: "input_text",
|
|
249
|
+
text: sanitizedTextResult
|
|
250
|
+
});
|
|
251
|
+
else if (mediaPlaceholder === "(see attached media)") contentParts.push({
|
|
252
|
+
type: "input_text",
|
|
253
|
+
text: mediaPlaceholder
|
|
254
|
+
});
|
|
255
|
+
for (const block of msg.content) if (isImageWithMediaPayload(block)) contentParts.push({
|
|
256
|
+
type: "input_image",
|
|
257
|
+
detail: "auto",
|
|
258
|
+
image_url: `data:${block.mimeType};base64,${block.data}`
|
|
259
|
+
});
|
|
260
|
+
output = contentParts;
|
|
261
|
+
} else output = sanitizeToolResultText(textResult, mediaPlaceholder ?? EMPTY_TOOL_RESULT_TEXT);
|
|
262
|
+
messages.push({
|
|
263
|
+
type: "function_call_output",
|
|
264
|
+
call_id: callId,
|
|
265
|
+
output
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
msgIndex++;
|
|
269
|
+
}
|
|
270
|
+
return messages;
|
|
271
|
+
}
|
|
272
|
+
function createResponsesAssistantOutput(model, api = model.api) {
|
|
273
|
+
return {
|
|
274
|
+
role: "assistant",
|
|
275
|
+
content: [],
|
|
276
|
+
api,
|
|
277
|
+
provider: model.provider,
|
|
278
|
+
model: model.id,
|
|
279
|
+
usage: {
|
|
280
|
+
input: 0,
|
|
281
|
+
output: 0,
|
|
282
|
+
cacheRead: 0,
|
|
283
|
+
cacheWrite: 0,
|
|
284
|
+
totalTokens: 0,
|
|
285
|
+
cost: {
|
|
286
|
+
input: 0,
|
|
287
|
+
output: 0,
|
|
288
|
+
cacheRead: 0,
|
|
289
|
+
cacheWrite: 0,
|
|
290
|
+
total: 0
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
stopReason: "stop",
|
|
294
|
+
timestamp: Date.now()
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
function resolveResponsesReasoningEffort(model, reasoning) {
|
|
298
|
+
const clampedReasoning = reasoning ? clampThinkingLevel(model, reasoning) : void 0;
|
|
299
|
+
if (!clampedReasoning || clampedReasoning === "off") return;
|
|
300
|
+
if (clampedReasoning === "max") return supportsOpenAIReasoningEffort(model, "max") ? "max" : "xhigh";
|
|
301
|
+
if (clampedReasoning === "minimal" && model.provider === "openai" && supportsOpenAIReasoningEffort(model, "max")) {
|
|
302
|
+
const effort = resolveOpenAIReasoningEffortForModel({
|
|
303
|
+
model,
|
|
304
|
+
effort: "minimal"
|
|
305
|
+
});
|
|
306
|
+
return isResponsesReasoningEffort(effort) ? effort : void 0;
|
|
307
|
+
}
|
|
308
|
+
return clampedReasoning;
|
|
309
|
+
}
|
|
310
|
+
function applyCommonResponsesParams(params, model, context, options, config) {
|
|
311
|
+
if (options?.maxTokens) params.max_output_tokens = Math.max(options.maxTokens, 16);
|
|
312
|
+
if (options?.temperature !== void 0 && supportsOpenAITemperature(model)) params.temperature = options.temperature;
|
|
313
|
+
if (context.tools) {
|
|
314
|
+
const converted = convertResponsesToolPayload(context.tools, { model });
|
|
315
|
+
if (converted.tools.length > 0) params.tools = converted.tools;
|
|
316
|
+
}
|
|
317
|
+
if (!model.reasoning) return;
|
|
318
|
+
if (options?.reasoningEffort || options?.reasoningSummary) {
|
|
319
|
+
params.reasoning = {
|
|
320
|
+
effort: options?.reasoningEffort ? model.thinkingLevelMap?.[options.reasoningEffort] ?? options.reasoningEffort : "medium",
|
|
321
|
+
summary: options?.reasoningSummary || "auto"
|
|
322
|
+
};
|
|
323
|
+
params.include = ["reasoning.encrypted_content"];
|
|
324
|
+
} else if ((config?.setDefaultReasoningOff ?? true) && model.thinkingLevelMap?.off !== null) params.reasoning = { effort: model.thinkingLevelMap?.off ?? "none" };
|
|
325
|
+
}
|
|
326
|
+
function buildResponsesRequestOptions(options) {
|
|
327
|
+
return {
|
|
328
|
+
...options?.signal ? { signal: options.signal } : {},
|
|
329
|
+
...options?.timeoutMs !== void 0 ? { timeout: options.timeoutMs } : {},
|
|
330
|
+
maxRetries: options?.maxRetries ?? 0
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function cleanStreamingScratchBuffers(output) {
|
|
334
|
+
for (const block of output.content) {
|
|
335
|
+
delete block.index;
|
|
336
|
+
delete block.partialJson;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async function runResponsesStreamLifecycle(params) {
|
|
340
|
+
const { stream, model, output, options } = params;
|
|
341
|
+
let firstEventAbort;
|
|
342
|
+
try {
|
|
343
|
+
const client = params.createClient();
|
|
344
|
+
let requestParams = params.buildParams();
|
|
345
|
+
const nextParams = await options?.onPayload?.(requestParams, model);
|
|
346
|
+
if (nextParams !== void 0) requestParams = nextParams;
|
|
347
|
+
firstEventAbort = createFirstStreamEventAbortController(options?.signal);
|
|
348
|
+
const { data: openaiStream, response } = await client.responses.create(requestParams, {
|
|
349
|
+
...buildResponsesRequestOptions(options),
|
|
350
|
+
signal: firstEventAbort.signal
|
|
351
|
+
}).withResponse();
|
|
352
|
+
await options?.onResponse?.({
|
|
353
|
+
status: response.status,
|
|
354
|
+
headers: headersToRecord(response.headers)
|
|
355
|
+
}, model);
|
|
356
|
+
stream.push({
|
|
357
|
+
type: "start",
|
|
358
|
+
partial: output
|
|
359
|
+
});
|
|
360
|
+
const firstEventTimeoutMs = getFirstStreamEventTimeoutMs(options);
|
|
361
|
+
const onFirstEventTimeout = getFirstStreamEventTimeoutHandler(options);
|
|
362
|
+
await processResponsesStream(openaiStream, output, stream, model, params.processStreamOptions || firstEventTimeoutMs !== void 0 || onFirstEventTimeout !== void 0 ? {
|
|
363
|
+
...params.processStreamOptions,
|
|
364
|
+
firstEventTimeoutMs: params.processStreamOptions?.firstEventTimeoutMs ?? firstEventTimeoutMs,
|
|
365
|
+
abortFirstEventStream: params.processStreamOptions?.abortFirstEventStream ?? firstEventAbort.abort,
|
|
366
|
+
onFirstEventTimeout: params.processStreamOptions?.onFirstEventTimeout ?? onFirstEventTimeout,
|
|
367
|
+
signal: params.processStreamOptions?.signal ?? options?.signal
|
|
368
|
+
} : void 0);
|
|
369
|
+
if (options?.signal?.aborted) throw transportAbortError(options.signal);
|
|
370
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage ?? "An unknown error occurred");
|
|
371
|
+
stream.push({
|
|
372
|
+
type: "done",
|
|
373
|
+
reason: output.stopReason,
|
|
374
|
+
message: output
|
|
375
|
+
});
|
|
376
|
+
stream.end();
|
|
377
|
+
} catch (error) {
|
|
378
|
+
cleanStreamingScratchBuffers(output);
|
|
379
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
380
|
+
output.errorMessage = params.formatError(error);
|
|
381
|
+
stream.push({
|
|
382
|
+
type: "error",
|
|
383
|
+
reason: output.stopReason,
|
|
384
|
+
error: output
|
|
385
|
+
});
|
|
386
|
+
stream.end();
|
|
387
|
+
} finally {
|
|
388
|
+
firstEventAbort?.dispose();
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
//#endregion
|
|
392
|
+
export { runResponsesStreamLifecycle as a, resolveResponsesReasoningEffort as i, convertResponsesMessages as n, convertResponsesToolPayload as o, createResponsesAssistantOutput as r, applyCommonResponsesParams as t };
|