@openclaw/ai 2026.8.1-beta.1 → 2026.8.1-beta.3
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-CE-7SkF6.mjs → anthropic-B8KwTtlb.mjs} +62 -37
- package/dist/{anthropic-SrGtwsJu.d.mts → anthropic-BECQCNdF.d.mts} +0 -2
- package/dist/{src-QkygScBs.mjs → anthropic-BQXQudmA.mjs} +0 -2
- package/dist/{anthropic-usage-Ma4iX6uG.mjs → anthropic-compaction-replay-4Zx7-aCr.mjs} +225 -103
- package/dist/anthropic-payload-policy-CSx7MoKH.d.mts +50 -0
- package/dist/{api-registry-EpJoVwM1.d.mts → api-registry-DFxSnFqG.d.mts} +1 -1
- package/dist/{azure-openai-responses-Cc_kasye.mjs → azure-openai-responses-Aa4mVcYG.mjs} +23 -29
- package/dist/diagnostics.d.mts +24 -1
- package/dist/diagnostics.mjs +2 -1
- package/dist/event-stream-BVVgDSdq.d.mts +1 -0
- package/dist/{event-stream-DRmDMRHH.d.mts → event-stream-D91-ohD_.d.mts} +3 -1
- package/dist/{event-stream-CEV9t6da.mjs → event-stream-uSMZJ3FA.mjs} +16 -3
- package/dist/event-stream.d.mts +1 -1
- package/dist/event-stream.mjs +1 -1
- package/dist/{google-shared-Cq2UKx1l.mjs → google-shared-M1dl64nz.mjs} +17 -19
- package/dist/google-thinking-level-C-V3tecN.mjs +9 -0
- package/dist/{google-vertex-CusxWzvE.mjs → google-vertex-DCJHM_a5.mjs} +5 -4
- package/dist/{google-CGQu21aw.mjs → google-yDRnipeq.mjs} +4 -4
- package/dist/host-DF_yxOsA.mjs +477 -0
- package/dist/{host-Dn4_SZI2.d.mts → host-DJq_FGcz.d.mts} +13 -8
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +7 -5
- package/dist/internal/anthropic.d.mts +21 -7
- package/dist/internal/anthropic.mjs +5 -5
- package/dist/internal/openai-responses-payload-policy.d.mts +3 -0
- package/dist/internal/openai-responses-payload-policy.mjs +3 -0
- package/dist/internal/openai.d.mts +6 -6
- package/dist/internal/openai.mjs +9 -7
- package/dist/internal/runtime.d.mts +19 -5
- package/dist/internal/runtime.mjs +85 -72
- package/dist/internal/shared.d.mts +17 -8
- package/dist/internal/shared.mjs +4 -5
- package/dist/{json-parse-BvXNt1-7.mjs → json-parse-CDnesDM_.mjs} +4 -6
- package/dist/{mistral-Dx6VzNjf.mjs → mistral-D6hRXI7A.mjs} +67 -72
- package/dist/{number-coercion-1Miyb5MO.mjs → number-coercion-H9qHik3g.mjs} +7 -2
- package/dist/openai-chatgpt-jwt-KWcgd0d_.mjs +19 -0
- package/dist/{openai-chatgpt-responses-SC9m0FSf.mjs → openai-chatgpt-responses-C339VIfT.mjs} +235 -146
- package/dist/{openai-completions-BaIpl93j.mjs → openai-completions-D88orSJ9.mjs} +88 -67
- package/dist/openai-completions-compat-B0KTleXp.d.mts +43 -0
- package/dist/{openai-responses-Bdx3V_Yu.mjs → openai-responses-BXEcr-Tp.mjs} +17 -24
- package/dist/openai-responses-contracts-CD75pQ0m.d.mts +68 -0
- package/dist/openai-responses-contracts-DvILoqBk.mjs +247 -0
- package/dist/openai-responses-payload-policy-BDo8CQsx.mjs +208 -0
- package/dist/openai-responses-payload-policy-CNGFT9zr.d.mts +41 -0
- package/dist/{openai-responses-prompt-observer-internal-DDZPxRfr.mjs → openai-responses-prompt-observer-internal-BG-1AsBI.mjs} +2 -3
- package/dist/{openai-responses-stream-internal-Bl8YyVtF.mjs → openai-responses-shared-Cvt5MgEm.mjs} +1287 -1039
- package/dist/{openai-reasoning-compat-DKebnIBL.mjs → openai-stop-reason-9OONKyAm.mjs} +222 -180
- package/dist/{openai-transport-shared-Cipt7egQ.mjs → openai-tool-projection-CHGLt03m.mjs} +239 -128
- package/dist/provider-error-C8mBRJDl.mjs +424 -0
- package/dist/provider-error-CzNw4BWX.d.mts +12 -0
- package/dist/provider-options-AvldWZt8.mjs +21 -0
- package/dist/{provider-options-C5kYML7i.d.mts → provider-options-Cc5ZQ6Aa.d.mts} +19 -3
- package/dist/provider-transcript-transform--gtf0ZbR.mjs +32 -0
- package/dist/provider-types.d.mts +31 -0
- package/dist/provider-types.mjs +8 -0
- package/dist/providers.d.mts +1 -1
- package/dist/providers.mjs +17 -19
- package/dist/{reasoning-tag-text-partitioner-CGDyLWUR.mjs → reasoning-tag-text-partitioner-5ygO2rZc.mjs} +17 -8
- package/dist/record-coerce-DdXsgUd_.mjs +23 -0
- package/dist/{model-utils-Dau5dlgm.mjs → sanitize-unicode-BP7HLLZn.mjs} +28 -2
- package/dist/session-resources-CkR4WWy1.mjs +21 -0
- package/dist/simple-options-oqsXB6U3.mjs +450 -0
- package/dist/src-D2H6yKkH.mjs +2 -0
- package/dist/{stream-first-event-timeout-BIBomOGq.mjs → stream-first-event-timeout-MK28puvq.mjs} +2 -2
- package/dist/string-coerce-fsri9iCu.mjs +34 -0
- package/dist/{tool-schema-json-projection-B1b-XCn5.mjs → tool-schema-json-projection-FEr-jOoZ.mjs} +20 -13
- package/dist/transport-utils-Bv0FHkd0.mjs +138 -0
- package/dist/transports.d.mts +199 -209
- package/dist/transports.mjs +1842 -1364
- package/dist/types-BDdaOVi2.mjs +6 -0
- package/dist/{types-CH7ReIcU.d.mts → types-DTAP49HF.d.mts} +35 -2
- package/dist/types.d.mts +5 -5
- package/dist/types.mjs +6 -4
- package/dist/utf16-slice-CvGodqok.mjs +29 -0
- package/dist/{validation-DAa_yFOM.mjs → validation-B61OhAio.mjs} +6 -6
- package/dist/{validation-CcPcuEbR.d.mts → validation-DYNNrU9u.d.mts} +1 -1
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +15 -1
- package/dist/host-Bl7Kgddo.mjs +0 -407
- package/dist/openai-chatgpt-jwt-DhAAzLkj.mjs +0 -39
- package/dist/openai-responses-contracts-BBKBfAqR.d.mts +0 -116
- package/dist/openai-responses-shared-CCyMvg7M.mjs +0 -403
- package/dist/provider-error-DI0Ts28U.mjs +0 -47
- package/dist/sanitize-unicode-DT5o51ur.mjs +0 -26
- package/dist/tool-result-text-Dvkp2Dus.mjs +0 -274
- package/dist/transform-messages-DfjpNXNQ.mjs +0 -2
- package/dist/transport-stream-shared-CPNv7A3r.mjs +0 -297
|
@@ -1,8 +1,242 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as
|
|
1
|
+
import { n as applyProviderReportedUsageCost, r as calculateCost } from "./sanitize-unicode-BP7HLLZn.mjs";
|
|
2
|
+
import { n as getAiTransportHost } from "./host-DF_yxOsA.mjs";
|
|
3
|
+
import { a as isRecord } from "./record-coerce-DdXsgUd_.mjs";
|
|
3
4
|
import { n as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-mZTCdRPo.mjs";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
5
|
+
import { g as notifyProviderHttpResponse, x as transportAbortError } from "./simple-options-oqsXB6U3.mjs";
|
|
6
|
+
import { t as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-FEr-jOoZ.mjs";
|
|
7
|
+
import { t as headersToRecord } from "./headers-B_e4-1J0.mjs";
|
|
8
|
+
//#region packages/ai/src/transports/openai-transport-shared.ts
|
|
9
|
+
/** Shared options, usage shape, cache identity, ordering, and stream scheduling for OpenAI APIs. */
|
|
10
|
+
const MODEL_STREAM_COOPERATIVE_YIELD_INTERVAL_MS = 12;
|
|
11
|
+
const MODEL_STREAM_COOPERATIVE_YIELD_MAX_EVENTS = 64;
|
|
12
|
+
const GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP = "skip_thought_signature_validator";
|
|
13
|
+
const log = {
|
|
14
|
+
debug(message, data) {
|
|
15
|
+
getAiTransportHost().logDebug("openai-transport", () => ({
|
|
16
|
+
message,
|
|
17
|
+
data
|
|
18
|
+
}));
|
|
19
|
+
},
|
|
20
|
+
info(message, data) {
|
|
21
|
+
getAiTransportHost().logInfo("openai-transport", message, data);
|
|
22
|
+
},
|
|
23
|
+
warn(message, data) {
|
|
24
|
+
getAiTransportHost().logWarn("openai-transport", message, data);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const EMPTY_OPENAI_COMPLETIONS_REASONING_BATCH = {
|
|
28
|
+
deltas: [],
|
|
29
|
+
mirroredThinking: [],
|
|
30
|
+
hasThinking: false,
|
|
31
|
+
hasVisibleText: false
|
|
32
|
+
};
|
|
33
|
+
const OPENAI_COMPLETIONS_REASONING_FIELDS = [
|
|
34
|
+
"reasoning_content",
|
|
35
|
+
"reasoning",
|
|
36
|
+
"reasoning_text"
|
|
37
|
+
];
|
|
38
|
+
function appendOpenAICompletionsReasoningDelta(batch, next) {
|
|
39
|
+
if (next.kind === "thinking") batch.hasThinking = true;
|
|
40
|
+
else batch.hasVisibleText = true;
|
|
41
|
+
const previous = batch.deltas[batch.deltas.length - 1];
|
|
42
|
+
if (!previous || previous.kind !== next.kind) {
|
|
43
|
+
batch.deltas.push(next);
|
|
44
|
+
if (next.kind === "thinking") batch.mirroredThinking.push(next.text);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (next.kind === "thinking" && previous.kind === "thinking") {
|
|
48
|
+
if (previous.signature !== next.signature) {
|
|
49
|
+
batch.deltas.push(next);
|
|
50
|
+
batch.mirroredThinking.push(next.text);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
previous.text += next.text;
|
|
54
|
+
batch.mirroredThinking[batch.mirroredThinking.length - 1] += next.text;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
previous.text += next.text;
|
|
58
|
+
}
|
|
59
|
+
function createOpenAICompletionsReasoningBatch() {
|
|
60
|
+
return {
|
|
61
|
+
deltas: [],
|
|
62
|
+
mirroredThinking: [],
|
|
63
|
+
hasThinking: false,
|
|
64
|
+
hasVisibleText: false
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function readOpenAICompletionsReasoningBatch(delta, visibleReasoningDetailTypes) {
|
|
68
|
+
let batch;
|
|
69
|
+
const reasoningDetails = delta.reasoning_details;
|
|
70
|
+
let usedReasoningThinkingDetails = false;
|
|
71
|
+
if (Array.isArray(reasoningDetails)) for (const item of reasoningDetails) {
|
|
72
|
+
if (!isRecord(item)) continue;
|
|
73
|
+
const detail = item;
|
|
74
|
+
if (typeof detail.text !== "string" || !detail.text) continue;
|
|
75
|
+
if (detail.type === "reasoning.text") {
|
|
76
|
+
usedReasoningThinkingDetails = true;
|
|
77
|
+
batch ??= createOpenAICompletionsReasoningBatch();
|
|
78
|
+
appendOpenAICompletionsReasoningDelta(batch, {
|
|
79
|
+
kind: "thinking",
|
|
80
|
+
signature: "reasoning_details",
|
|
81
|
+
text: detail.text
|
|
82
|
+
});
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (typeof detail.type === "string" && visibleReasoningDetailTypes.has(detail.type)) {
|
|
86
|
+
batch ??= createOpenAICompletionsReasoningBatch();
|
|
87
|
+
appendOpenAICompletionsReasoningDelta(batch, {
|
|
88
|
+
kind: "text",
|
|
89
|
+
text: detail.text,
|
|
90
|
+
source: "reasoning_detail"
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (!usedReasoningThinkingDetails) for (const field of OPENAI_COMPLETIONS_REASONING_FIELDS) {
|
|
95
|
+
const value = delta[field];
|
|
96
|
+
if (typeof value === "string" && value.length > 0) {
|
|
97
|
+
batch ??= createOpenAICompletionsReasoningBatch();
|
|
98
|
+
appendOpenAICompletionsReasoningDelta(batch, {
|
|
99
|
+
kind: "thinking",
|
|
100
|
+
signature: field,
|
|
101
|
+
text: value
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return batch ?? EMPTY_OPENAI_COMPLETIONS_REASONING_BATCH;
|
|
107
|
+
}
|
|
108
|
+
function parseOpenAICompletionsUsage(rawUsage, model, options) {
|
|
109
|
+
const cacheRead = rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.prompt_cache_hit_tokens ?? 0;
|
|
110
|
+
const cacheWrite = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;
|
|
111
|
+
const input = Math.max(0, (rawUsage.prompt_tokens || 0) - cacheRead - cacheWrite);
|
|
112
|
+
const output = rawUsage.completion_tokens || 0;
|
|
113
|
+
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens;
|
|
114
|
+
const usage = {
|
|
115
|
+
input,
|
|
116
|
+
output,
|
|
117
|
+
cacheRead,
|
|
118
|
+
cacheWrite,
|
|
119
|
+
...options?.includeReasoningTokens !== false && typeof reasoningTokens === "number" && Number.isFinite(reasoningTokens) ? { reasoningTokens } : {},
|
|
120
|
+
totalTokens: input + output + cacheRead + cacheWrite,
|
|
121
|
+
cost: {
|
|
122
|
+
input: 0,
|
|
123
|
+
output: 0,
|
|
124
|
+
cacheRead: 0,
|
|
125
|
+
cacheWrite: 0,
|
|
126
|
+
total: 0
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
calculateCost(model, usage);
|
|
130
|
+
applyProviderReportedUsageCost(usage, rawUsage.cost);
|
|
131
|
+
return usage;
|
|
132
|
+
}
|
|
133
|
+
function createOpenAIResponseHook(onResponse, response, model) {
|
|
134
|
+
return onResponse ? () => onResponse({
|
|
135
|
+
status: response.status,
|
|
136
|
+
headers: headersToRecord(response.headers)
|
|
137
|
+
}, model) : void 0;
|
|
138
|
+
}
|
|
139
|
+
function createOpenAIProviderAcceptanceHook(options, response, model) {
|
|
140
|
+
return () => notifyProviderHttpResponse({
|
|
141
|
+
options,
|
|
142
|
+
response,
|
|
143
|
+
model
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function throwIfModelStreamAborted(signal) {
|
|
147
|
+
if (signal?.aborted) throw transportAbortError(signal);
|
|
148
|
+
}
|
|
149
|
+
/** Measure one UTF-8 append without double-counting a surrogate pair split across chunks. */
|
|
150
|
+
function measureUtf8AppendBytes(bufferEndsWithHighSurrogate, chunk) {
|
|
151
|
+
let bytes = Buffer.byteLength(chunk, "utf8");
|
|
152
|
+
if (!chunk) return {
|
|
153
|
+
bytes,
|
|
154
|
+
endsWithHighSurrogate: bufferEndsWithHighSurrogate
|
|
155
|
+
};
|
|
156
|
+
const nextCodeUnit = chunk.charCodeAt(0);
|
|
157
|
+
if (bufferEndsWithHighSurrogate && nextCodeUnit >= 56320 && nextCodeUnit <= 57343) bytes -= 2;
|
|
158
|
+
const finalCodeUnit = chunk.charCodeAt(chunk.length - 1);
|
|
159
|
+
return {
|
|
160
|
+
bytes,
|
|
161
|
+
endsWithHighSurrogate: finalCodeUnit >= 55296 && finalCodeUnit <= 56319
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function createModelStreamCooperativeScheduler(signal) {
|
|
165
|
+
let lastYieldedAt = Date.now();
|
|
166
|
+
let eventsSinceYield = 0;
|
|
167
|
+
return { async afterEvent() {
|
|
168
|
+
throwIfModelStreamAborted(signal);
|
|
169
|
+
eventsSinceYield += 1;
|
|
170
|
+
const now = Date.now();
|
|
171
|
+
if (eventsSinceYield < MODEL_STREAM_COOPERATIVE_YIELD_MAX_EVENTS && now - lastYieldedAt < MODEL_STREAM_COOPERATIVE_YIELD_INTERVAL_MS) return;
|
|
172
|
+
eventsSinceYield = 0;
|
|
173
|
+
lastYieldedAt = now;
|
|
174
|
+
await new Promise((resolve) => {
|
|
175
|
+
setTimeout(resolve, 0);
|
|
176
|
+
});
|
|
177
|
+
throwIfModelStreamAborted(signal);
|
|
178
|
+
} };
|
|
179
|
+
}
|
|
180
|
+
function resolvePromptCacheKey(options, cacheRetention) {
|
|
181
|
+
if (cacheRetention === "none") return;
|
|
182
|
+
return clampOpenAIPromptCacheKey(options?.promptCacheKey ?? options?.sessionId);
|
|
183
|
+
}
|
|
184
|
+
function isOpenAICompletionsThinkingEnabled(effort) {
|
|
185
|
+
const normalized = effort.trim().toLowerCase();
|
|
186
|
+
return normalized !== "off" && normalized !== "none";
|
|
187
|
+
}
|
|
188
|
+
function readOpenAICompletionsContentDeltas(content, topLevelRefusal, mirroredThinking = []) {
|
|
189
|
+
let deltas = readOpenAICompletionsContentPartDeltas(content);
|
|
190
|
+
if (mirroredThinking.length > 0) {
|
|
191
|
+
const structuredThinking = deltas.filter((delta) => delta.kind === "thinking").map((delta) => delta.text);
|
|
192
|
+
const mirrorsCombinedThinking = structuredThinking.length > 1 && mirroredThinking.includes(structuredThinking.join(""));
|
|
193
|
+
deltas = deltas.filter((delta) => delta.kind !== "thinking" || !mirrorsCombinedThinking && !mirroredThinking.includes(delta.text));
|
|
194
|
+
}
|
|
195
|
+
if (typeof topLevelRefusal !== "string" || !topLevelRefusal) return deltas;
|
|
196
|
+
const structuredRefusals = deltas.filter((delta) => delta.kind === "text" && delta.source === "refusal").map((delta) => delta.text);
|
|
197
|
+
if (structuredRefusals.some((refusal) => refusal === topLevelRefusal) || structuredRefusals.length > 1 && structuredRefusals.join("") === topLevelRefusal) return deltas;
|
|
198
|
+
return [...deltas, {
|
|
199
|
+
kind: "text",
|
|
200
|
+
text: topLevelRefusal,
|
|
201
|
+
source: "refusal"
|
|
202
|
+
}];
|
|
203
|
+
}
|
|
204
|
+
function readOpenAICompletionsContentPartDeltas(content) {
|
|
205
|
+
if (typeof content === "string") return content ? [{
|
|
206
|
+
kind: "text",
|
|
207
|
+
text: content
|
|
208
|
+
}] : [];
|
|
209
|
+
if (Array.isArray(content)) return content.flatMap(readOpenAICompletionsContentPartDeltas);
|
|
210
|
+
if (!content || typeof content !== "object") return [];
|
|
211
|
+
const record = content;
|
|
212
|
+
const type = typeof record.type === "string" ? record.type.toLowerCase() : "";
|
|
213
|
+
const extractText = (value) => {
|
|
214
|
+
if (typeof value === "string") return value;
|
|
215
|
+
if (Array.isArray(value)) return value.map(extractText).join("");
|
|
216
|
+
if (value && typeof value === "object") {
|
|
217
|
+
const nested = value;
|
|
218
|
+
return extractText(nested.text ?? nested.content ?? nested.thinking ?? nested.refusal);
|
|
219
|
+
}
|
|
220
|
+
return "";
|
|
221
|
+
};
|
|
222
|
+
const text = extractText(record.text ?? record.content ?? record.thinking ?? record.refusal);
|
|
223
|
+
if (!text) return [];
|
|
224
|
+
if (type.includes("thinking") || type.includes("reasoning")) return [{
|
|
225
|
+
kind: "thinking",
|
|
226
|
+
text
|
|
227
|
+
}];
|
|
228
|
+
if (type === "refusal") return [{
|
|
229
|
+
kind: "text",
|
|
230
|
+
text,
|
|
231
|
+
source: "refusal"
|
|
232
|
+
}];
|
|
233
|
+
if (["text", "output_text"].includes(type) || type.endsWith(".output_text")) return [{
|
|
234
|
+
kind: "text",
|
|
235
|
+
text
|
|
236
|
+
}];
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
6
240
|
//#region packages/ai/src/providers/openai-tool-projection.ts
|
|
7
241
|
function unreadableToolDiagnostic(toolIndex) {
|
|
8
242
|
return {
|
|
@@ -189,127 +423,4 @@ function reconcileOpenAICompletionsToolChoice(choice, projection) {
|
|
|
189
423
|
};
|
|
190
424
|
}
|
|
191
425
|
//#endregion
|
|
192
|
-
|
|
193
|
-
/** Shared options, usage shape, cache identity, ordering, and stream scheduling for OpenAI APIs. */
|
|
194
|
-
const MODEL_STREAM_COOPERATIVE_YIELD_INTERVAL_MS = 12;
|
|
195
|
-
const MODEL_STREAM_COOPERATIVE_YIELD_MAX_EVENTS = 64;
|
|
196
|
-
const GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP = "skip_thought_signature_validator";
|
|
197
|
-
const log = {
|
|
198
|
-
debug(message, data) {
|
|
199
|
-
getAiTransportHost().logDebug("openai-transport", () => ({
|
|
200
|
-
message,
|
|
201
|
-
data
|
|
202
|
-
}));
|
|
203
|
-
},
|
|
204
|
-
info(message, data) {
|
|
205
|
-
getAiTransportHost().logInfo("openai-transport", message, data);
|
|
206
|
-
},
|
|
207
|
-
warn(message, data) {
|
|
208
|
-
getAiTransportHost().logWarn("openai-transport", message, data);
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
function parseOpenAICompletionsUsage(rawUsage, model, options) {
|
|
212
|
-
const cacheRead = rawUsage.prompt_tokens_details?.cached_tokens ?? rawUsage.prompt_cache_hit_tokens ?? 0;
|
|
213
|
-
const cacheWrite = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;
|
|
214
|
-
const input = Math.max(0, (rawUsage.prompt_tokens || 0) - cacheRead - cacheWrite);
|
|
215
|
-
const output = rawUsage.completion_tokens || 0;
|
|
216
|
-
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens;
|
|
217
|
-
const usage = {
|
|
218
|
-
input,
|
|
219
|
-
output,
|
|
220
|
-
cacheRead,
|
|
221
|
-
cacheWrite,
|
|
222
|
-
...options?.includeReasoningTokens !== false && typeof reasoningTokens === "number" && Number.isFinite(reasoningTokens) ? { reasoningTokens } : {},
|
|
223
|
-
totalTokens: input + output + cacheRead + cacheWrite,
|
|
224
|
-
cost: {
|
|
225
|
-
input: 0,
|
|
226
|
-
output: 0,
|
|
227
|
-
cacheRead: 0,
|
|
228
|
-
cacheWrite: 0,
|
|
229
|
-
total: 0
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
calculateCost(model, usage);
|
|
233
|
-
applyProviderReportedUsageCost(usage, rawUsage.cost);
|
|
234
|
-
return usage;
|
|
235
|
-
}
|
|
236
|
-
function throwIfModelStreamAborted(signal) {
|
|
237
|
-
if (signal?.aborted) throw transportAbortError(signal);
|
|
238
|
-
}
|
|
239
|
-
function createModelStreamCooperativeScheduler(signal) {
|
|
240
|
-
let lastYieldedAt = Date.now();
|
|
241
|
-
let eventsSinceYield = 0;
|
|
242
|
-
return { async afterEvent() {
|
|
243
|
-
throwIfModelStreamAborted(signal);
|
|
244
|
-
eventsSinceYield += 1;
|
|
245
|
-
const now = Date.now();
|
|
246
|
-
if (eventsSinceYield < MODEL_STREAM_COOPERATIVE_YIELD_MAX_EVENTS && now - lastYieldedAt < MODEL_STREAM_COOPERATIVE_YIELD_INTERVAL_MS) return;
|
|
247
|
-
eventsSinceYield = 0;
|
|
248
|
-
lastYieldedAt = now;
|
|
249
|
-
await new Promise((resolve) => {
|
|
250
|
-
setTimeout(resolve, 0);
|
|
251
|
-
});
|
|
252
|
-
throwIfModelStreamAborted(signal);
|
|
253
|
-
} };
|
|
254
|
-
}
|
|
255
|
-
function resolvePromptCacheKey(options, cacheRetention) {
|
|
256
|
-
if (cacheRetention === "none") return;
|
|
257
|
-
return clampOpenAIPromptCacheKey(options?.promptCacheKey ?? options?.sessionId);
|
|
258
|
-
}
|
|
259
|
-
function isOpenAICompletionsThinkingEnabled(effort) {
|
|
260
|
-
const normalized = effort.trim().toLowerCase();
|
|
261
|
-
return normalized !== "off" && normalized !== "none";
|
|
262
|
-
}
|
|
263
|
-
function readOpenAICompletionsContentDeltas(content, topLevelRefusal, mirroredThinking = []) {
|
|
264
|
-
let deltas = readOpenAICompletionsContentPartDeltas(content);
|
|
265
|
-
if (mirroredThinking.length > 0) {
|
|
266
|
-
const structuredThinking = deltas.filter((delta) => delta.kind === "thinking").map((delta) => delta.text);
|
|
267
|
-
const mirrorsCombinedThinking = structuredThinking.length > 1 && mirroredThinking.includes(structuredThinking.join(""));
|
|
268
|
-
deltas = deltas.filter((delta) => delta.kind !== "thinking" || !mirrorsCombinedThinking && !mirroredThinking.includes(delta.text));
|
|
269
|
-
}
|
|
270
|
-
if (typeof topLevelRefusal !== "string" || !topLevelRefusal) return deltas;
|
|
271
|
-
const structuredRefusals = deltas.filter((delta) => delta.kind === "text" && delta.source === "refusal").map((delta) => delta.text);
|
|
272
|
-
if (structuredRefusals.some((refusal) => refusal === topLevelRefusal) || structuredRefusals.length > 1 && structuredRefusals.join("") === topLevelRefusal) return deltas;
|
|
273
|
-
return [...deltas, {
|
|
274
|
-
kind: "text",
|
|
275
|
-
text: topLevelRefusal,
|
|
276
|
-
source: "refusal"
|
|
277
|
-
}];
|
|
278
|
-
}
|
|
279
|
-
function readOpenAICompletionsContentPartDeltas(content) {
|
|
280
|
-
if (typeof content === "string") return content ? [{
|
|
281
|
-
kind: "text",
|
|
282
|
-
text: content
|
|
283
|
-
}] : [];
|
|
284
|
-
if (Array.isArray(content)) return content.flatMap(readOpenAICompletionsContentPartDeltas);
|
|
285
|
-
if (!content || typeof content !== "object") return [];
|
|
286
|
-
const record = content;
|
|
287
|
-
const type = typeof record.type === "string" ? record.type.toLowerCase() : "";
|
|
288
|
-
const extractText = (value) => {
|
|
289
|
-
if (typeof value === "string") return value;
|
|
290
|
-
if (Array.isArray(value)) return value.map(extractText).join("");
|
|
291
|
-
if (value && typeof value === "object") {
|
|
292
|
-
const nested = value;
|
|
293
|
-
return extractText(nested.text ?? nested.content ?? nested.thinking ?? nested.refusal);
|
|
294
|
-
}
|
|
295
|
-
return "";
|
|
296
|
-
};
|
|
297
|
-
const text = extractText(record.text ?? record.content ?? record.thinking ?? record.refusal);
|
|
298
|
-
if (!text) return [];
|
|
299
|
-
if (type.includes("thinking") || type.includes("reasoning")) return [{
|
|
300
|
-
kind: "thinking",
|
|
301
|
-
text
|
|
302
|
-
}];
|
|
303
|
-
if (type === "refusal") return [{
|
|
304
|
-
kind: "text",
|
|
305
|
-
text,
|
|
306
|
-
source: "refusal"
|
|
307
|
-
}];
|
|
308
|
-
if (["text", "output_text"].includes(type) || type.endsWith(".output_text")) return [{
|
|
309
|
-
kind: "text",
|
|
310
|
-
text
|
|
311
|
-
}];
|
|
312
|
-
return [];
|
|
313
|
-
}
|
|
314
|
-
//#endregion
|
|
315
|
-
export { parseOpenAICompletionsUsage as a, throwIfModelStreamAborted as c, reconcileOpenAIResponsesToolChoice as d, log as i, projectOpenAITools as l, createModelStreamCooperativeScheduler as n, readOpenAICompletionsContentDeltas as o, isOpenAICompletionsThinkingEnabled as r, resolvePromptCacheKey as s, GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP as t, reconcileOpenAICompletionsToolChoice as u };
|
|
426
|
+
export { createModelStreamCooperativeScheduler as a, isOpenAICompletionsThinkingEnabled as c, parseOpenAICompletionsUsage as d, readOpenAICompletionsContentDeltas as f, throwIfModelStreamAborted as h, GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP as i, log as l, resolvePromptCacheKey as m, reconcileOpenAICompletionsToolChoice as n, createOpenAIProviderAcceptanceHook as o, readOpenAICompletionsReasoningBatch as p, reconcileOpenAIResponsesToolChoice as r, createOpenAIResponseHook as s, projectOpenAITools as t, measureUtf8AppendBytes as u };
|