@openclaw/ai 2026.9.1-beta.1 → 2026.9.2
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-DI4PZKKB.mjs → anthropic-BDdqdVLK.mjs} +222 -216
- package/dist/{anthropic-compaction-replay-nhKaCXu6.mjs → anthropic-compaction-replay-DB2FGLQc.mjs} +160 -68
- package/dist/{anthropic-payload-policy-B_0n0enE.d.mts → anthropic-payload-policy-d46X2pR0.d.mts} +28 -4
- package/dist/{api-registry-CTd8NzcD.d.mts → api-registry-DWtPjzyn.d.mts} +2 -2
- package/dist/{azure-openai-responses-D8dI0ekg.mjs → azure-openai-responses-C5bZRfAP.mjs} +12 -9
- package/dist/{number-coercion-H9qHik3g.mjs → base64-CEFBpSkN.mjs} +73 -1
- package/dist/{diagnostics-COpOtRwq.mjs → diagnostics-CPeq9F7y.mjs} +5 -1
- package/dist/diagnostics-DfFyKeX_.mjs +136 -0
- package/dist/{diagnostics-BaTA9eVl.d.mts → diagnostics-DnPnOui6.d.mts} +5 -1
- package/dist/diagnostics.d.mts +8 -3
- package/dist/diagnostics.mjs +4 -3
- package/dist/{env-api-keys-DrgeBuva.mjs → env-api-keys-bktO00EJ.mjs} +2 -15
- package/dist/{event-stream-uSMZJ3FA.mjs → event-stream-BgDvQeum.mjs} +3 -9
- package/dist/{event-stream-LiXePESD.d.mts → event-stream-BhT5T1Ay.d.mts} +1 -1
- package/dist/event-stream-zctLx0yr.d.mts +1 -0
- package/dist/event-stream.d.mts +2 -2
- package/dist/event-stream.mjs +1 -1
- package/dist/{google-Od-LLdVK.mjs → google-4qeuE8iX.mjs} +9 -7
- package/dist/{google-shared-Bgr9lFPw.mjs → google-shared-CWeG8RIl.mjs} +27 -23
- package/dist/{google-vertex-aerRSXc4.mjs → google-vertex-O9BcnB6X.mjs} +8 -6
- package/dist/{host-D51fmkH6.mjs → host-CEvLw30U.mjs} +54 -9
- package/dist/{host-DsbPMFU6.d.mts → host-DjzGmdZ2.d.mts} +3 -3
- package/dist/index-FnHM2FcI.d.mts +117 -0
- package/dist/index.d.mts +10 -11
- package/dist/index.mjs +6 -6
- package/dist/internal/anthropic.d.mts +59 -55
- package/dist/internal/anthropic.mjs +5 -5
- package/dist/internal/openai-responses-payload-policy.d.mts +3 -3
- package/dist/internal/openai-responses-payload-policy.mjs +2 -3
- package/dist/internal/openai.d.mts +66 -64
- package/dist/internal/openai.mjs +10 -10
- package/dist/internal/retry-after.d.mts +2 -3
- package/dist/internal/runtime.d.mts +43 -43
- package/dist/internal/runtime.mjs +10 -8
- package/dist/internal/shared.d.mts +25 -25
- package/dist/internal/shared.mjs +95 -4
- package/dist/{mistral-EiHdymeb.mjs → mistral-Tb6oalqH.mjs} +30 -23
- package/dist/{openai-chatgpt-responses-DHpF9o1e.mjs → openai-chatgpt-responses-yUXjPNfu.mjs} +76 -137
- package/dist/{openai-completions-EFPiUGfS.mjs → openai-completions-BIUV3RDT.mjs} +26 -22
- package/dist/{openai-completions-compat-BefmHT26.d.mts → openai-completions-compat-CpjYUArk.d.mts} +5 -5
- package/dist/{openai-completions-stream-DUlGwRMz.mjs → openai-completions-stream-BsrBe3Gg.mjs} +103 -96
- package/dist/openai-prompt-cache-CGnVB74a.mjs +21 -0
- package/dist/openai-prompt-cache-CNoIfHYC.d.mts +16 -0
- package/dist/{openai-responses-BogP8OjX.mjs → openai-responses-CCL41ALM.mjs} +15 -14
- package/dist/openai-responses-compaction-window-BMVHFOtq.mjs +697 -0
- package/dist/{openai-responses-contracts-Dvj_UtPy.d.mts → openai-responses-contracts-rF5DDRNS.d.mts} +23 -6
- package/dist/{openai-responses-payload-policy-CNGFT9zr.d.mts → openai-responses-payload-policy-rLRPsSmB.d.mts} +1 -0
- package/dist/{openai-responses-prompt-observer-internal-Cd4hJG-S.mjs → openai-responses-prompt-observer-internal-D0bhBfgL.mjs} +4 -18
- package/dist/{openai-responses-shared-DJThc6jW.mjs → openai-responses-shared-Bmma_3Qc.mjs} +438 -309
- package/dist/{openai-tool-schema-ho-hIkQf.mjs → openai-tool-schema-_pTAJqKF.mjs} +30 -40
- package/dist/{provider-error-DDOs_Bz0.mjs → provider-error-9TraxGvt.mjs} +20 -92
- package/dist/{provider-options-MdMBLzlU.d.mts → provider-options-Bcc_p-TU.d.mts} +26 -6
- package/dist/provider-replay-context-BuSUaAk5.mjs +21 -0
- package/dist/{provider-transcript-transform-C01bQ5a3.mjs → provider-transcript-transform-BaMbI1hr.mjs} +1 -1
- package/dist/provider-types.d.mts +19 -20
- package/dist/providers.d.mts +6 -6
- package/dist/providers.mjs +11 -11
- package/dist/{reasoning-tag-text-partitioner-5ygO2rZc.mjs → reasoning-tag-text-partitioner-DLCNXki6.mjs} +158 -94
- package/dist/rolldown-runtime-BhDjJH2R.mjs +15 -0
- package/dist/{sanitize-unicode-DCkltN94.mjs → sanitize-unicode-D6xUvZaS.mjs} +2 -8
- package/dist/{simple-options-CFj7x3J4.mjs → simple-options-BjHCCh4v.mjs} +42 -6
- package/dist/{src-Bu3FiFAG.mjs → src-2qBGKg8O.mjs} +83 -2
- package/dist/{stream-first-event-timeout-MK28puvq.mjs → stream-first-event-timeout-DcNjoFQE.mjs} +1 -1
- package/dist/{tool-schema-json-projection-qvTEs5Am.mjs → tool-schema-json-projection-mJhXDcyz.mjs} +21 -31
- package/dist/{transport-stream-shared-ljShVIZB.mjs → transport-stream-shared-CZqMhfIw.mjs} +19 -6
- package/dist/{transport-stream-shared-rvaCyFYt.d.mts → transport-stream-shared-xnaqxmbP.d.mts} +5 -5
- package/dist/{transport-utils-LJ1_rbi-.mjs → transport-utils-7il795_9.mjs} +5 -8
- package/dist/transports.d.mts +121 -85
- package/dist/transports.mjs +747 -464
- package/dist/types-3Lnm-QSJ.d.mts +1 -0
- package/dist/{types-DhLoJbFm.d.mts → types-CJ1-Ht7A.d.mts} +55 -19
- package/dist/types.d.mts +7 -7
- package/dist/types.mjs +5 -5
- package/dist/{validation-Dk7q7NMN.d.mts → validation-AKZBDGQd.d.mts} +1 -1
- package/dist/{validation-B61OhAio.mjs → validation-CJZtym2g.mjs} +7 -5
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +5 -5
- package/dist/anthropic-BECQCNdF.d.mts +0 -98
- package/dist/event-stream-BKp4fOt_.d.mts +0 -1
- package/dist/index-Qbej8io0.d.mts +0 -3
- package/dist/openai-prompt-cache-2uo_1OR1.d.mts +0 -7
- package/dist/openai-prompt-cache-mZTCdRPo.mjs +0 -12
- package/dist/openai-responses-contracts-QUY12X0B.mjs +0 -233
- package/dist/openai-responses-payload-policy-D8EdimYe.mjs +0 -204
- package/dist/provider-options-AvldWZt8.mjs +0 -21
- package/dist/tls-certificate-errors-DXSpluKI.mjs +0 -93
- package/dist/types-GiAjXatj.d.mts +0 -1
package/dist/{anthropic-compaction-replay-nhKaCXu6.mjs → anthropic-compaction-replay-DB2FGLQc.mjs}
RENAMED
|
@@ -1,13 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as resolveClaudeModelIdentity, m as resolveClaudeOpus5ModelIdentity, u as resolveClaudeFable5ModelIdentity } from "./src-2qBGKg8O.mjs";
|
|
2
2
|
import { l as normalizeNullableString, o as isRecord, u as normalizeOptionalLowercaseString } from "./utf16-slice-qz3nsy87.mjs";
|
|
3
|
-
import { n as getAiTransportHost } from "./host-
|
|
4
|
-
import { a as estimateBase64DecodedBytes } from "./
|
|
5
|
-
import { r as asNonNegativeFiniteNumber } from "./number-coercion-H9qHik3g.mjs";
|
|
3
|
+
import { n as getAiTransportHost } from "./host-CEvLw30U.mjs";
|
|
4
|
+
import { a as asNonNegativeFiniteNumber, n as estimateBase64DecodedBytes } from "./base64-CEFBpSkN.mjs";
|
|
6
5
|
import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
|
|
7
|
-
import {
|
|
8
|
-
import { a as resolveProviderEndpoint, o as resolveProviderRequestCapabilities, t as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-
|
|
9
|
-
import { s as parsePositiveInteger } from "./transport-utils-
|
|
10
|
-
import { t as
|
|
6
|
+
import { d as stripSystemPromptCacheBoundary, m as sortPromptCacheToolsByName, u as splitSystemPromptCacheBoundary } from "./simple-options-BjHCCh4v.mjs";
|
|
7
|
+
import { a as resolveProviderEndpoint, o as resolveProviderRequestCapabilities, t as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-mJhXDcyz.mjs";
|
|
8
|
+
import { s as parsePositiveInteger } from "./transport-utils-7il795_9.mjs";
|
|
9
|
+
import { n as providerReplayContextMatches, t as buildProviderReplayContext } from "./provider-replay-context-BuSUaAk5.mjs";
|
|
10
|
+
//#region packages/ai/src/providers/anthropic-auth-headers.ts
|
|
11
|
+
/** Inspect resolved credential shape for auth routing without exposing its value. */
|
|
12
|
+
function isAnthropicOAuthApiKey(apiKey) {
|
|
13
|
+
return typeof apiKey === "string" && getAiTransportHost().resolveSecretSentinel(apiKey).includes("sk-ant-oat");
|
|
14
|
+
}
|
|
15
|
+
function usesFoundryBearerAuth(model) {
|
|
16
|
+
return model.provider === "microsoft-foundry" && (model.authHeader === true || hasBearerAuthorizationHeader(model.headers));
|
|
17
|
+
}
|
|
18
|
+
function hasBearerAuthorizationHeader(headers) {
|
|
19
|
+
if (!headers) return false;
|
|
20
|
+
return Object.entries(headers).some(([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim()));
|
|
21
|
+
}
|
|
22
|
+
function omitFoundryBearerCredentialHeaders(headers) {
|
|
23
|
+
if (!headers) return;
|
|
24
|
+
const next = {};
|
|
25
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
26
|
+
const lower = key.toLowerCase();
|
|
27
|
+
if (lower === "authorization" || lower === "x-api-key" || lower === "api-key") continue;
|
|
28
|
+
next[key] = value;
|
|
29
|
+
}
|
|
30
|
+
return Object.keys(next).length > 0 ? next : void 0;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
11
33
|
//#region packages/ai/src/transports/anthropic-payload-policy.ts
|
|
12
34
|
/**
|
|
13
35
|
* Anthropic-family request payload policy helpers.
|
|
@@ -24,16 +46,26 @@ function resolveAnthropicCompactThreshold(contextWindow, configured) {
|
|
|
24
46
|
return Math.max(ANTHROPIC_COMPACT_THRESHOLD_MIN, resolvedContextWindow === void 0 ? ANTHROPIC_COMPACT_THRESHOLD_MIN : Math.floor(resolvedContextWindow * .7));
|
|
25
47
|
}
|
|
26
48
|
/** Resolve the server-compaction gate and effective threshold for an Anthropic route. */
|
|
27
|
-
function resolveAnthropicServerCompactionPlan(model, extraParams) {
|
|
28
|
-
const
|
|
29
|
-
const api = normalizeOptionalLowercaseString(model.api);
|
|
30
|
-
const endpointClass = resolveProviderEndpoint(model).endpointClass;
|
|
31
|
-
const enabled = extraParams?.anthropicServerCompaction === true && provider === "anthropic" && api === "anthropic-messages" && (endpointClass === "default" || endpointClass === "anthropic-public");
|
|
49
|
+
function resolveAnthropicServerCompactionPlan(model, extraParams, apiKey) {
|
|
50
|
+
const enabled = extraParams?.anthropicServerCompaction === true && !isAnthropicOAuthApiKey(apiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
|
|
32
51
|
return {
|
|
33
52
|
enabled,
|
|
34
53
|
...enabled ? { threshold: resolveAnthropicCompactThreshold(model.contextWindow, extraParams?.anthropicCompactThreshold) } : {}
|
|
35
54
|
};
|
|
36
55
|
}
|
|
56
|
+
function isDirectAnthropicModel(model) {
|
|
57
|
+
const baseUrl = model.baseUrl?.trim() || process.env.ANTHROPIC_BASE_URL?.trim();
|
|
58
|
+
const endpointModel = baseUrl === model.baseUrl ? model : {
|
|
59
|
+
...model,
|
|
60
|
+
baseUrl
|
|
61
|
+
};
|
|
62
|
+
const endpointClass = resolveProviderEndpoint(endpointModel).endpointClass;
|
|
63
|
+
return normalizeOptionalLowercaseString(model.provider) === "anthropic" && (endpointClass === "anthropic-public" || endpointClass === "default" && (!baseUrl || resolveBaseUrlHostname(baseUrl) === "api.anthropic.com"));
|
|
64
|
+
}
|
|
65
|
+
function isAnthropicServerToolClearingEnabled(model, apiKey) {
|
|
66
|
+
const resolvedApiKey = apiKey && getAiTransportHost().resolveSecretSentinel(apiKey);
|
|
67
|
+
return Boolean(resolvedApiKey?.trim()) && !isAnthropicOAuthApiKey(resolvedApiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
|
|
68
|
+
}
|
|
37
69
|
function resolveBaseUrlHostname(baseUrl) {
|
|
38
70
|
try {
|
|
39
71
|
return new URL(baseUrl).hostname;
|
|
@@ -160,21 +192,100 @@ function resolveAnthropicPayloadPolicy(input, model) {
|
|
|
160
192
|
cacheControl: input.enableCacheControl === true ? resolveAnthropicEphemeralCacheControl(input.baseUrl, input.cacheRetention) : void 0,
|
|
161
193
|
compactThreshold: serverCompactionPlan.threshold ?? resolveAnthropicCompactThreshold(input.contextWindow, input.extraParams?.anthropicCompactThreshold),
|
|
162
194
|
serviceTier: input.serviceTier,
|
|
163
|
-
useServerCompaction: input.enableServerCompaction === true && serverCompactionPlan.enabled
|
|
195
|
+
useServerCompaction: input.enableServerCompaction === true && serverCompactionPlan.enabled,
|
|
196
|
+
...input.cacheTtlPruning && normalizeOptionalLowercaseString(input.api) === "anthropic-messages" && isDirectAnthropicModel(input) ? { toolClearing: {
|
|
197
|
+
trigger: Math.max(5e4, Math.floor((parsePositiveInteger(input.contextWindow) ?? 0) * .3)),
|
|
198
|
+
clearAtLeast: Math.max(12500, Math.floor((parsePositiveInteger(input.contextWindow) ?? 0) * .05)),
|
|
199
|
+
tools: input.cacheTtlPruning.tools
|
|
200
|
+
} } : {}
|
|
164
201
|
};
|
|
165
202
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
203
|
+
function resolveToolClearingExclusions(payload, filter) {
|
|
204
|
+
const compile = (patterns) => (patterns ?? []).map((pattern) => pattern.trim().toLowerCase()).filter(Boolean).map((pattern) => new RegExp(`^${pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replaceAll("\\*", ".*")}$`));
|
|
205
|
+
const allow = compile(filter?.allow);
|
|
206
|
+
const deny = compile(filter?.deny);
|
|
207
|
+
if (allow.length === 0 && deny.length === 0) return [];
|
|
208
|
+
const excluded = new Set(filter?.deny?.map((name) => name.trim()).filter((name) => name && !name.includes("*")));
|
|
209
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
210
|
+
for (const tool of Array.isArray(payload.tools) ? payload.tools : []) if (isRecord(tool) && typeof tool.name === "string") candidates.add(tool.name);
|
|
211
|
+
for (const message of Array.isArray(payload.messages) ? payload.messages : []) {
|
|
212
|
+
if (!isRecord(message) || !Array.isArray(message.content)) continue;
|
|
213
|
+
for (const block of message.content) if (isRecord(block) && block.type === "tool_use" && typeof block.name === "string") candidates.add(block.name);
|
|
214
|
+
}
|
|
215
|
+
for (const toolName of candidates) {
|
|
216
|
+
const name = toolName.trim().toLowerCase();
|
|
217
|
+
if (deny.some((pattern) => pattern.test(name)) || allow.length > 0 && !allow.some((pattern) => pattern.test(name))) excluded.add(toolName);
|
|
218
|
+
}
|
|
219
|
+
return [...excluded].toSorted();
|
|
220
|
+
}
|
|
221
|
+
function applyAnthropicContextManagementEdits(payload, policy) {
|
|
222
|
+
if (payload.context_management !== void 0) return;
|
|
223
|
+
const edits = [];
|
|
224
|
+
if (policy.toolClearing) edits.push({
|
|
225
|
+
type: "clear_tool_uses_20250919",
|
|
226
|
+
trigger: {
|
|
227
|
+
type: "input_tokens",
|
|
228
|
+
value: policy.toolClearing.trigger
|
|
229
|
+
},
|
|
230
|
+
keep: {
|
|
231
|
+
type: "tool_uses",
|
|
232
|
+
value: 3
|
|
233
|
+
},
|
|
234
|
+
clear_at_least: {
|
|
235
|
+
type: "input_tokens",
|
|
236
|
+
value: policy.toolClearing.clearAtLeast
|
|
237
|
+
},
|
|
238
|
+
exclude_tools: resolveToolClearingExclusions(payload, policy.toolClearing.tools),
|
|
239
|
+
clear_tool_inputs: false
|
|
240
|
+
});
|
|
241
|
+
if (policy.useServerCompaction) edits.push({
|
|
172
242
|
type: "compact_20260112",
|
|
173
243
|
trigger: {
|
|
174
244
|
type: "input_tokens",
|
|
175
245
|
value: policy.compactThreshold
|
|
176
246
|
}
|
|
177
|
-
}
|
|
247
|
+
});
|
|
248
|
+
if (edits.length > 0) payload.context_management = { edits };
|
|
249
|
+
}
|
|
250
|
+
function applyAnthropicContextManagementToRequest(payload, model, options, directApiKeyBetaHeader) {
|
|
251
|
+
if (directApiKeyBetaHeader === void 0 || !options?.cacheTtlPruning && !options?.anthropicServerCompaction) return;
|
|
252
|
+
applyAnthropicContextManagementEdits(payload, resolveAnthropicPayloadPolicy({
|
|
253
|
+
...model,
|
|
254
|
+
api: "anthropic-messages",
|
|
255
|
+
enableServerCompaction: true,
|
|
256
|
+
extraParams: { ...options },
|
|
257
|
+
cacheTtlPruning: options?.cacheTtlPruning
|
|
258
|
+
}));
|
|
259
|
+
}
|
|
260
|
+
function resolveAnthropicContextManagementBetaHeader(payload, directApiKeyBetaHeader) {
|
|
261
|
+
if (directApiKeyBetaHeader === void 0 || !isRecord(payload.context_management)) return directApiKeyBetaHeader;
|
|
262
|
+
const edits = payload.context_management.edits;
|
|
263
|
+
const betas = new Set(directApiKeyBetaHeader.split(",").map((beta) => beta.trim()).filter(Boolean));
|
|
264
|
+
for (const edit of Array.isArray(edits) ? edits : []) {
|
|
265
|
+
if (!isRecord(edit)) continue;
|
|
266
|
+
if (edit.type === "clear_tool_uses_20250919") betas.add("context-management-2025-06-27");
|
|
267
|
+
else if (edit.type === "compact_20260112") betas.add("compact-2026-01-12");
|
|
268
|
+
}
|
|
269
|
+
return [...betas].join(",");
|
|
270
|
+
}
|
|
271
|
+
function logAnthropicContextEdits(event) {
|
|
272
|
+
if (!isRecord(event) || !isRecord(event.context_management)) return;
|
|
273
|
+
const edits = event.context_management.applied_edits;
|
|
274
|
+
let clearedTools = 0;
|
|
275
|
+
let clearedTokens = 0;
|
|
276
|
+
for (const edit of Array.isArray(edits) ? edits : []) {
|
|
277
|
+
if (!isRecord(edit) || edit.type !== "clear_tool_uses_20250919" || typeof edit.cleared_tool_uses !== "number" || !Number.isSafeInteger(edit.cleared_tool_uses) || edit.cleared_tool_uses < 0 || typeof edit.cleared_input_tokens !== "number" || !Number.isSafeInteger(edit.cleared_input_tokens) || edit.cleared_input_tokens < 0) continue;
|
|
278
|
+
clearedTools = Math.min(Number.MAX_SAFE_INTEGER, clearedTools + edit.cleared_tool_uses);
|
|
279
|
+
clearedTokens = Math.min(Number.MAX_SAFE_INTEGER, clearedTokens + edit.cleared_input_tokens);
|
|
280
|
+
}
|
|
281
|
+
if (clearedTools > 0 || clearedTokens > 0) getAiTransportHost().logInfo("anthropic", `server-side context edit: cleared ${clearedTools} tool results (${clearedTokens} input tokens)`);
|
|
282
|
+
}
|
|
283
|
+
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
284
|
+
function applyAnthropicPayloadPolicyToParams(payloadObj, policy, cacheBreakpointOptOutMessageIndexes) {
|
|
285
|
+
if (policy.allowsServiceTier && policy.serviceTier !== void 0 && payloadObj.service_tier === void 0) payloadObj.service_tier = policy.serviceTier;
|
|
286
|
+
if (policy.cacheControl) applyAnthropicCacheControlToSystem(payloadObj.system, policy.cacheControl);
|
|
287
|
+
else stripAnthropicSystemPromptBoundary(payloadObj.system);
|
|
288
|
+
applyAnthropicContextManagementEdits(payloadObj, policy);
|
|
178
289
|
if (!policy.cacheControl) return;
|
|
179
290
|
const usedMarkers = countAnthropicCacheControlMarkers(payloadObj.system) + countAnthropicCacheControlMarkers(payloadObj.tools);
|
|
180
291
|
applyAnthropicCacheControlToMessages(payloadObj.messages, policy.cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
|
|
@@ -218,7 +329,7 @@ const ANTHROPIC_IMAGE_MEDIA_TYPE_SET = /* @__PURE__ */ new Set([
|
|
|
218
329
|
"image/gif",
|
|
219
330
|
"image/webp"
|
|
220
331
|
]);
|
|
221
|
-
const ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES =
|
|
332
|
+
const ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES = 67108864;
|
|
222
333
|
function createAnthropicInlineImageBudget() {
|
|
223
334
|
return { totalBytes: 0 };
|
|
224
335
|
}
|
|
@@ -245,25 +356,6 @@ async function normalizeAnthropicInlineContent(content, budget) {
|
|
|
245
356
|
return normalized;
|
|
246
357
|
}
|
|
247
358
|
//#endregion
|
|
248
|
-
//#region packages/ai/src/providers/anthropic-auth-headers.ts
|
|
249
|
-
function usesFoundryBearerAuth(model) {
|
|
250
|
-
return model.provider === "microsoft-foundry" && (model.authHeader === true || hasBearerAuthorizationHeader(model.headers));
|
|
251
|
-
}
|
|
252
|
-
function hasBearerAuthorizationHeader(headers) {
|
|
253
|
-
if (!headers) return false;
|
|
254
|
-
return Object.entries(headers).some(([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim()));
|
|
255
|
-
}
|
|
256
|
-
function omitFoundryBearerCredentialHeaders(headers) {
|
|
257
|
-
if (!headers) return;
|
|
258
|
-
const next = {};
|
|
259
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
260
|
-
const lower = key.toLowerCase();
|
|
261
|
-
if (lower === "authorization" || lower === "x-api-key" || lower === "api-key") continue;
|
|
262
|
-
next[key] = value;
|
|
263
|
-
}
|
|
264
|
-
return Object.keys(next).length > 0 ? next : void 0;
|
|
265
|
-
}
|
|
266
|
-
//#endregion
|
|
267
359
|
//#region packages/ai/src/providers/anthropic-refusal.ts
|
|
268
360
|
function readAnthropicRefusalDetails(value) {
|
|
269
361
|
if (!value || typeof value !== "object") return {
|
|
@@ -363,6 +455,23 @@ function applyAnthropicFallbackBoundary(params) {
|
|
|
363
455
|
//#endregion
|
|
364
456
|
//#region packages/ai/src/providers/anthropic-thinking-replay.ts
|
|
365
457
|
const ANTHROPIC_OMITTED_REASONING_TEXT = "[assistant reasoning omitted]";
|
|
458
|
+
function applyAnthropicThinkingBindingControls(params, directApiKeyBetaHeader) {
|
|
459
|
+
if (directApiKeyBetaHeader === void 0 || !isRecord(params.thinking) || params.thinking.type !== "adaptive") return;
|
|
460
|
+
params.thinking.block_binding = { prefix_mismatch_behavior: "drop_block" };
|
|
461
|
+
const betas = directApiKeyBetaHeader.split(",").map((beta) => beta.trim()).filter(Boolean);
|
|
462
|
+
return { "anthropic-beta": [.../* @__PURE__ */ new Set([...betas, "thinking-binding-controls-2026-08-01"])].join(",") };
|
|
463
|
+
}
|
|
464
|
+
function readAnthropicInputTransformations(event) {
|
|
465
|
+
if (!isRecord(event) || event.type !== "message_start" && event.type !== "message_delta") return;
|
|
466
|
+
const transformations = (isRecord(event.message) ? event.message : void 0)?.input_transformations ?? event.input_transformations;
|
|
467
|
+
return Array.isArray(transformations) ? transformations : void 0;
|
|
468
|
+
}
|
|
469
|
+
function logAnthropicThinkingDrops(transformations) {
|
|
470
|
+
const drops = transformations?.filter((entry) => isRecord(entry) && entry.type === "thinking_dropped" && typeof entry.reason === "string" && /^(?:prefix|model|organization|end_user)_binding_mismatch$/.test(entry.reason) && typeof entry.path === "string" && /^messages\.\d{1,10}\.content\.\d{1,10}$/.test(entry.path));
|
|
471
|
+
if (!drops?.length) return;
|
|
472
|
+
const paths = drops.slice(0, 5).map(({ reason, path }) => `${reason} at ${path}`);
|
|
473
|
+
getAiTransportHost().logWarn("anthropic", `replayed thinking dropped: ${drops.length} block(s) (${paths.join(", ")}${drops.length > 5 ? ", …" : ""})`);
|
|
474
|
+
}
|
|
366
475
|
function asReplayMessage(value) {
|
|
367
476
|
return value && typeof value === "object" ? value : void 0;
|
|
368
477
|
}
|
|
@@ -665,9 +774,9 @@ function applyAnthropicMessageStartUsage(target, payload) {
|
|
|
665
774
|
};
|
|
666
775
|
return promptTokens > 0 ? promptUsage : void 0;
|
|
667
776
|
}
|
|
668
|
-
/** Keep
|
|
669
|
-
function applyAnthropicMessageDeltaUsage(target,
|
|
670
|
-
|
|
777
|
+
/** Keep billing and context distinct; omitted usage preserves the last snapshot. */
|
|
778
|
+
function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage) {
|
|
779
|
+
if (!usage) return;
|
|
671
780
|
const billedIterations = readAnthropicCompactionBilledUsage(usage.iterations);
|
|
672
781
|
const inputTokens = readAnthropicUsageTokenCount(usage.input_tokens);
|
|
673
782
|
const outputTokens = readAnthropicUsageTokenCount(usage.output_tokens);
|
|
@@ -693,7 +802,7 @@ function applyAnthropicMessageDeltaUsage(target, payload, messageStartPromptUsag
|
|
|
693
802
|
totalTokens: iterationUsage.usage.totalTokens
|
|
694
803
|
};
|
|
695
804
|
else if (iterationUsage.state === "invalid") target.contextUsage = { state: "unavailable" };
|
|
696
|
-
else if (outputTokens !== void 0 && (messageStartPromptUsage !== void 0 ||
|
|
805
|
+
else if (outputTokens !== void 0 && (messageStartPromptUsage !== void 0 || (cacheReadTokens !== void 0 || cacheWriteTokens !== void 0) && readAnthropicPromptUsageSnapshot(usage) !== void 0)) {
|
|
697
806
|
const promptTokens = target.input + target.cacheRead + target.cacheWrite;
|
|
698
807
|
target.contextUsage = {
|
|
699
808
|
state: "available",
|
|
@@ -734,20 +843,6 @@ function createCompactionCapture(output, model, options) {
|
|
|
734
843
|
}
|
|
735
844
|
};
|
|
736
845
|
}
|
|
737
|
-
function hashReplayValue(value) {
|
|
738
|
-
const normalized = value?.trim();
|
|
739
|
-
return normalized ? shortHash(normalized) : void 0;
|
|
740
|
-
}
|
|
741
|
-
function buildAnthropicReplayContext(model, options) {
|
|
742
|
-
return {
|
|
743
|
-
provider: model.provider,
|
|
744
|
-
api: model.api,
|
|
745
|
-
model: model.id,
|
|
746
|
-
baseUrlHash: hashReplayValue(model.baseUrl),
|
|
747
|
-
sessionHash: hashReplayValue(options?.sessionId),
|
|
748
|
-
authProfileHash: hashReplayValue(options?.authProfileId)
|
|
749
|
-
};
|
|
750
|
-
}
|
|
751
846
|
function isAnthropicCompactionState(state) {
|
|
752
847
|
if (state.v !== 1 || typeof state.data !== "string" || typeof state.provider !== "string" || typeof state.api !== "string" || typeof state.model !== "string" || typeof state.baseUrlHash !== "string" || state.sessionHash !== void 0 && typeof state.sessionHash !== "string" || state.authProfileHash !== void 0 && typeof state.authProfileHash !== "string") return false;
|
|
753
848
|
if (state.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) return state.data === ANTHROPIC_COMPACTION_SUPPRESSION_DATA;
|
|
@@ -756,11 +851,8 @@ function isAnthropicCompactionState(state) {
|
|
|
756
851
|
function readAnthropicCompactionState(value) {
|
|
757
852
|
return isRecord(value) && isAnthropicCompactionState(value) ? value : void 0;
|
|
758
853
|
}
|
|
759
|
-
function replayContextMatches(state, context) {
|
|
760
|
-
return state.provider === context.provider && state.api === context.api && state.model === context.model && state.baseUrlHash === context.baseUrlHash && state.sessionHash === context.sessionHash && state.authProfileHash === context.authProfileHash;
|
|
761
|
-
}
|
|
762
854
|
function captureAnthropicCompaction(output, summary, replayIndex, model, options) {
|
|
763
|
-
const context =
|
|
855
|
+
const context = buildProviderReplayContext(model, options);
|
|
764
856
|
if (!summary || !context.baseUrlHash || !Number.isSafeInteger(replayIndex) || replayIndex < 0) return;
|
|
765
857
|
output.providerReplay = {
|
|
766
858
|
v: 1,
|
|
@@ -772,7 +864,7 @@ function captureAnthropicCompaction(output, summary, replayIndex, model, options
|
|
|
772
864
|
};
|
|
773
865
|
}
|
|
774
866
|
function suppressAnthropicCompaction(output, model, options) {
|
|
775
|
-
const context =
|
|
867
|
+
const context = buildProviderReplayContext(model, options);
|
|
776
868
|
if (!context.baseUrlHash) return;
|
|
777
869
|
output.providerReplay = {
|
|
778
870
|
v: 1,
|
|
@@ -783,18 +875,18 @@ function suppressAnthropicCompaction(output, model, options) {
|
|
|
783
875
|
};
|
|
784
876
|
}
|
|
785
877
|
function resolveNewestAnthropicCompaction(messages, model, options) {
|
|
786
|
-
const context =
|
|
878
|
+
const context = buildProviderReplayContext(model, options);
|
|
787
879
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
788
880
|
const message = messages[index];
|
|
789
881
|
if (message?.role !== "assistant") continue;
|
|
790
882
|
const replayState = readAnthropicCompactionState(message.providerReplay);
|
|
791
883
|
if (replayState?.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) {
|
|
792
|
-
if (
|
|
884
|
+
if (providerReplayContextMatches(replayState, context)) return;
|
|
793
885
|
continue;
|
|
794
886
|
}
|
|
795
887
|
if (message.providerReplay?.type !== ANTHROPIC_COMPACTION_REPLAY_TYPE) continue;
|
|
796
888
|
if (!replayState) return;
|
|
797
|
-
if (!
|
|
889
|
+
if (!providerReplayContextMatches(replayState, context)) return;
|
|
798
890
|
return {
|
|
799
891
|
owner: message,
|
|
800
892
|
replayIndex: replayState.replayIndex ?? 0,
|
|
@@ -832,4 +924,4 @@ function isAnthropicReplayRejection(error) {
|
|
|
832
924
|
return (record.status === 400 || text.startsWith("http 400:")) && (text.includes("compaction") || text.includes("context_management"));
|
|
833
925
|
}
|
|
834
926
|
//#endregion
|
|
835
|
-
export {
|
|
927
|
+
export { createAnthropicInlineImageBudget as A, resolveAnthropicContextManagementBetaHeader as B, ANTHROPIC_SERVER_SIDE_FALLBACKS as C, readAnthropicFallbackBoundary as D, applyAnthropicFallbackBoundary as E, applyAnthropicEphemeralCacheControlMarkers as F, omitFoundryBearerCredentialHeaders as G, resolveAnthropicPayloadPolicy as H, applyAnthropicPayloadPolicyToParams as I, usesFoundryBearerAuth as K, isAnthropicServerToolClearingEnabled as L, resolveAnthropicImageMediaType as M, applyAnthropicCacheControlToMessages as N, resolveAnthropicFallbackServingModelCost as O, applyAnthropicContextManagementToRequest as P, isDirectAnthropicModel as R, readAnthropicInputTransformations as S, CLAUDE_OPUS_FALLBACK_MODEL_COST as T, resolveAnthropicServerCompactionPlan as U, resolveAnthropicEphemeralCacheControl as V, isAnthropicOAuthApiKey as W, toClaudeCodeToolName as _, suppressAnthropicCompaction as a, findActiveAnthropicToolTurnAssistantIndex as b, readAnthropicCacheWriteUsage as c, readLastAnthropicIterationUsage as d, normalizeAnthropicToolCallId as f, resolveOriginalAnthropicToolName as g, reconcileAnthropicToolChoice as h, resolveNewestAnthropicCompaction as i, normalizeAnthropicInlineContent as j, applyAnthropicRefusal as k, readAnthropicPromptUsageSnapshot as l, projectAnthropicTools as m, createCompactionCapture as n, applyAnthropicMessageDeltaUsage as o, normalizeAnthropicToolChoice as p, isAnthropicReplayRejection as r, applyAnthropicMessageStartUsage as s, buildAnthropicReplayPlan as t, readAnthropicUsageTokenCount as u, ANTHROPIC_OMITTED_REASONING_TEXT as v, ANTHROPIC_SERVER_SIDE_FALLBACK_BETA as w, logAnthropicThinkingDrops as x, applyAnthropicThinkingBindingControls as y, logAnthropicContextEdits as z };
|
package/dist/{anthropic-payload-policy-B_0n0enE.d.mts → anthropic-payload-policy-d46X2pR0.d.mts}
RENAMED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "./
|
|
1
|
+
import "./index-FnHM2FcI.mjs";
|
|
2
|
+
import { D as Model } from "./types-CJ1-Ht7A.mjs";
|
|
3
|
+
import { t as AnthropicContextManagementOptions } from "./provider-options-Bcc_p-TU.mjs";
|
|
3
4
|
//#region packages/ai/src/transports/anthropic-payload-policy.d.ts
|
|
4
5
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
5
6
|
type AnthropicServiceTier = "auto" | "standard_only";
|
|
@@ -12,6 +13,7 @@ type AnthropicPayloadPolicyInput = {
|
|
|
12
13
|
api?: string;
|
|
13
14
|
baseUrl?: string;
|
|
14
15
|
cacheRetention?: "short" | "long" | "none";
|
|
16
|
+
cacheTtlPruning?: AnthropicContextManagementOptions["cacheTtlPruning"];
|
|
15
17
|
contextWindow?: unknown;
|
|
16
18
|
enableCacheControl?: boolean;
|
|
17
19
|
enableServerCompaction?: boolean;
|
|
@@ -26,6 +28,11 @@ type AnthropicPayloadPolicy = {
|
|
|
26
28
|
compactThreshold: number;
|
|
27
29
|
serviceTier: AnthropicServiceTier | undefined;
|
|
28
30
|
useServerCompaction: boolean;
|
|
31
|
+
toolClearing?: {
|
|
32
|
+
trigger: number;
|
|
33
|
+
clearAtLeast: number;
|
|
34
|
+
tools: NonNullable<AnthropicContextManagementOptions["cacheTtlPruning"]>["tools"];
|
|
35
|
+
};
|
|
29
36
|
};
|
|
30
37
|
/** Resolve the server-compaction gate and effective threshold for an Anthropic route. */
|
|
31
38
|
declare function resolveAnthropicServerCompactionPlan(model: {
|
|
@@ -33,19 +40,36 @@ declare function resolveAnthropicServerCompactionPlan(model: {
|
|
|
33
40
|
api?: unknown;
|
|
34
41
|
baseUrl?: string;
|
|
35
42
|
contextWindow?: unknown;
|
|
36
|
-
}, extraParams?: Record<string, unknown
|
|
43
|
+
}, extraParams?: Record<string, unknown>, apiKey?: string): {
|
|
37
44
|
enabled: boolean;
|
|
38
45
|
threshold?: number;
|
|
39
46
|
};
|
|
47
|
+
declare function isDirectAnthropicModel(model: {
|
|
48
|
+
provider?: unknown;
|
|
49
|
+
baseUrl?: string;
|
|
50
|
+
}): boolean;
|
|
51
|
+
declare function isAnthropicServerToolClearingEnabled(model: {
|
|
52
|
+
provider?: unknown;
|
|
53
|
+
api?: unknown;
|
|
54
|
+
baseUrl?: string;
|
|
55
|
+
}, apiKey?: string): boolean;
|
|
40
56
|
/** Resolve Anthropic cache-control marker retention for a request endpoint. */
|
|
41
57
|
declare function resolveAnthropicEphemeralCacheControl(baseUrl: string | undefined, cacheRetention: AnthropicPayloadPolicyInput["cacheRetention"]): AnthropicEphemeralCacheControl | undefined;
|
|
42
58
|
/** Apply one shared deepest-stable-message cache breakpoint policy. */
|
|
43
59
|
declare function applyAnthropicCacheControlToMessages(messages: unknown, cacheControl: AnthropicEphemeralCacheControl, markerLimit: number, cacheBreakpointOptOutMessageIndexes: ReadonlySet<number>): void;
|
|
44
60
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
45
61
|
declare function resolveAnthropicPayloadPolicy(input: AnthropicPayloadPolicyInput, model?: Model): AnthropicPayloadPolicy;
|
|
62
|
+
type AnthropicContextManagementPayload = {
|
|
63
|
+
context_management?: unknown;
|
|
64
|
+
tools?: unknown;
|
|
65
|
+
messages?: unknown;
|
|
66
|
+
};
|
|
67
|
+
declare function applyAnthropicContextManagementToRequest(payload: AnthropicContextManagementPayload, model: Model, options: AnthropicContextManagementOptions | undefined, directApiKeyBetaHeader: string | undefined): void;
|
|
68
|
+
declare function resolveAnthropicContextManagementBetaHeader(payload: AnthropicContextManagementPayload, directApiKeyBetaHeader: string | undefined): string | undefined;
|
|
69
|
+
declare function logAnthropicContextEdits(event: unknown): void;
|
|
46
70
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
47
71
|
declare function applyAnthropicPayloadPolicyToParams(payloadObj: Record<string, unknown>, policy: AnthropicPayloadPolicy, cacheBreakpointOptOutMessageIndexes: ReadonlySet<number>): void;
|
|
48
72
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
49
73
|
declare function applyAnthropicEphemeralCacheControlMarkers(payloadObj: Record<string, unknown>, cacheControl?: AnthropicEphemeralCacheControl | null): void;
|
|
50
74
|
//#endregion
|
|
51
|
-
export {
|
|
75
|
+
export { isAnthropicServerToolClearingEnabled as a, resolveAnthropicContextManagementBetaHeader as c, resolveAnthropicServerCompactionPlan as d, applyAnthropicPayloadPolicyToParams as i, resolveAnthropicEphemeralCacheControl as l, applyAnthropicContextManagementToRequest as n, isDirectAnthropicModel as o, applyAnthropicEphemeralCacheControlMarkers as r, logAnthropicContextEdits as s, applyAnthropicCacheControlToMessages as t, resolveAnthropicPayloadPolicy as u };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "./
|
|
1
|
+
import "./index-FnHM2FcI.mjs";
|
|
2
|
+
import { D as Model, J as StreamOptions, W as SimpleStreamOptions, d as Context, n as Api, q as StreamFunction, s as AssistantMessageEventStreamContract } from "./types-CJ1-Ht7A.mjs";
|
|
3
3
|
//#region packages/ai/src/api-registry.d.ts
|
|
4
4
|
/** Runtime stream adapter signature stored in the API provider registry. */
|
|
5
5
|
type ApiStreamFunction = (model: Model, context: Context, options?: StreamOptions) => AssistantMessageEventStreamContract;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { n as
|
|
4
|
-
import { n as
|
|
5
|
-
import { n as
|
|
6
|
-
import { a as resolveResponsesReasoningEffort, i as createResponsesAssistantOutput,
|
|
1
|
+
import { t as AssistantMessageEventStream } from "./event-stream-BgDvQeum.mjs";
|
|
2
|
+
import { n as getAiTransportHost } from "./host-CEvLw30U.mjs";
|
|
3
|
+
import { n as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-CGnVB74a.mjs";
|
|
4
|
+
import { n as buildBaseOptions } from "./simple-options-BjHCCh4v.mjs";
|
|
5
|
+
import { n as getEnvApiKey } from "./env-api-keys-bktO00EJ.mjs";
|
|
6
|
+
import { a as resolveResponsesReasoningEffort, i as createResponsesAssistantOutput, r as convertResponsesMessages, s as runResponsesStreamLifecycle, t as applyCommonResponsesParams } from "./openai-responses-shared-Bmma_3Qc.mjs";
|
|
7
7
|
import { i as resolveAzureDeploymentNameFromMap, t as isOpenAICompatibleAzureResponsesBaseUrl } from "./azure-openai-responses-client-compat-C7K7QfUE.mjs";
|
|
8
8
|
import OpenAI, { AzureOpenAI } from "openai";
|
|
9
9
|
//#region packages/ai/src/providers/azure-openai-responses.ts
|
|
@@ -25,10 +25,11 @@ function resolveDeploymentName(model, options) {
|
|
|
25
25
|
*/
|
|
26
26
|
const streamAzureOpenAIResponses = (model, context, options) => {
|
|
27
27
|
const stream = new AssistantMessageEventStream();
|
|
28
|
+
const output = createResponsesAssistantOutput(model, "azure-openai-responses");
|
|
28
29
|
runResponsesStreamLifecycle({
|
|
29
30
|
stream,
|
|
30
31
|
model,
|
|
31
|
-
output
|
|
32
|
+
output,
|
|
32
33
|
options,
|
|
33
34
|
resolveRequestModel: (requestModel) => {
|
|
34
35
|
const { baseUrl } = resolveAzureConfig(requestModel, options);
|
|
@@ -103,7 +104,8 @@ function createClient(model, apiKeyInput, options) {
|
|
|
103
104
|
dangerouslyAllowBrowser: true,
|
|
104
105
|
defaultHeaders: headers,
|
|
105
106
|
baseURL: baseUrl,
|
|
106
|
-
fetch: guardedFetch
|
|
107
|
+
fetch: guardedFetch,
|
|
108
|
+
maxRetries: 0
|
|
107
109
|
});
|
|
108
110
|
return new AzureOpenAI({
|
|
109
111
|
apiKey,
|
|
@@ -111,7 +113,8 @@ function createClient(model, apiKeyInput, options) {
|
|
|
111
113
|
dangerouslyAllowBrowser: true,
|
|
112
114
|
defaultHeaders: headers,
|
|
113
115
|
baseURL: baseUrl,
|
|
114
|
-
fetch: guardedFetch
|
|
116
|
+
fetch: guardedFetch,
|
|
117
|
+
maxRetries: 0
|
|
115
118
|
});
|
|
116
119
|
}
|
|
117
120
|
function buildParams(model, context, options, deploymentName, replayMode = "checkpoint") {
|
|
@@ -68,4 +68,76 @@ function parseStrictNonNegativeInteger(value) {
|
|
|
68
68
|
return parsed !== void 0 && parsed >= 0 ? parsed : void 0;
|
|
69
69
|
}
|
|
70
70
|
//#endregion
|
|
71
|
-
|
|
71
|
+
//#region packages/media-core/src/base64.ts
|
|
72
|
+
/** Estimates decoded bytes without allocating a cleaned copy of the base64 payload. */
|
|
73
|
+
function estimateBase64DecodedBytes(base64) {
|
|
74
|
+
let effectiveLen = 0;
|
|
75
|
+
for (let i = 0; i < base64.length; i += 1) {
|
|
76
|
+
if (base64.charCodeAt(i) <= 32) continue;
|
|
77
|
+
effectiveLen += 1;
|
|
78
|
+
}
|
|
79
|
+
if (effectiveLen === 0) return 0;
|
|
80
|
+
let padding = 0;
|
|
81
|
+
let end = base64.length - 1;
|
|
82
|
+
while (end >= 0 && base64.charCodeAt(end) <= 32) end -= 1;
|
|
83
|
+
if (end >= 0 && base64[end] === "=") {
|
|
84
|
+
padding = 1;
|
|
85
|
+
end -= 1;
|
|
86
|
+
while (end >= 0 && base64.charCodeAt(end) <= 32) end -= 1;
|
|
87
|
+
if (end >= 0 && base64[end] === "=") padding = 2;
|
|
88
|
+
}
|
|
89
|
+
const estimated = Math.floor(effectiveLen * 3 / 4) - padding;
|
|
90
|
+
return Math.max(0, estimated);
|
|
91
|
+
}
|
|
92
|
+
function isBase64DataChar(code) {
|
|
93
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122 || code >= 48 && code <= 57 || code === 43 || code === 47;
|
|
94
|
+
}
|
|
95
|
+
function base64DataValue(code) {
|
|
96
|
+
if (code >= 65 && code <= 90) return code - 65;
|
|
97
|
+
if (code >= 97 && code <= 122) return code - 97 + 26;
|
|
98
|
+
if (code >= 48 && code <= 57) return code - 48 + 52;
|
|
99
|
+
return code === 43 ? 62 : 63;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Normalizes and validates a base64 string, returning canonical no-whitespace
|
|
103
|
+
* base64 only when the input has valid alphabet, padding, and length.
|
|
104
|
+
*/
|
|
105
|
+
function canonicalizeBase64(base64) {
|
|
106
|
+
let out;
|
|
107
|
+
let outLen = 0;
|
|
108
|
+
let padding = 0;
|
|
109
|
+
let sawPadding = false;
|
|
110
|
+
let lastDataCode = 0;
|
|
111
|
+
for (let i = 0; i < base64.length; i += 1) {
|
|
112
|
+
const code = base64.charCodeAt(i);
|
|
113
|
+
if (code <= 32) {
|
|
114
|
+
if (out === void 0) {
|
|
115
|
+
out = Buffer.allocUnsafe(base64.length - 1);
|
|
116
|
+
for (let j = 0; j < i; j += 1) out[j] = base64.charCodeAt(j);
|
|
117
|
+
outLen = i;
|
|
118
|
+
}
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (code === 61) {
|
|
122
|
+
padding += 1;
|
|
123
|
+
if (padding > 2) return;
|
|
124
|
+
sawPadding = true;
|
|
125
|
+
} else if (sawPadding || !isBase64DataChar(code)) return;
|
|
126
|
+
else lastDataCode = code;
|
|
127
|
+
if (out !== void 0) {
|
|
128
|
+
out[outLen] = code;
|
|
129
|
+
outLen += 1;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const cleanedLength = out === void 0 ? base64.length : outLen;
|
|
133
|
+
if (cleanedLength === 0) return;
|
|
134
|
+
const remainder = cleanedLength % 4;
|
|
135
|
+
if (remainder !== 0 && (sawPadding || remainder === 1)) return;
|
|
136
|
+
const effectivePadding = remainder === 0 ? padding : 4 - remainder;
|
|
137
|
+
const padBitMask = effectivePadding === 2 ? 15 : effectivePadding === 1 ? 3 : 0;
|
|
138
|
+
if (padBitMask !== 0 && (base64DataValue(lastDataCode) & padBitMask) !== 0) return;
|
|
139
|
+
const cleaned = out === void 0 ? base64 : out.subarray(0, outLen).toString("latin1");
|
|
140
|
+
return remainder === 0 ? cleaned : cleaned + "=".repeat(4 - remainder);
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
export { asNonNegativeFiniteNumber as a, parseStrictNonNegativeInteger as c, asFiniteNumberInRange as i, parseStrictPositiveInteger as l, estimateBase64DecodedBytes as n, clampTimerTimeoutMs as o, asFiniteNumber as r, parseStrictFiniteNumber as s, canonicalizeBase64 as t, resolveTimerTimeoutMs as u };
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
//#region packages/llm-core/src/utils/diagnostics.ts
|
|
2
|
+
/** True when the provider explicitly refused the request payload. */
|
|
3
|
+
function isProviderRefusalAssistantError(message) {
|
|
4
|
+
return Boolean(message?.diagnostics?.some((diagnostic) => diagnostic.type === "provider_refusal"));
|
|
5
|
+
}
|
|
2
6
|
/** Formats arbitrary thrown values into diagnostic-safe text. */
|
|
3
7
|
function formatThrownValue(value) {
|
|
4
8
|
if (value instanceof Error) return value.message || value.name;
|
|
@@ -33,4 +37,4 @@ function appendAssistantMessageDiagnostic(message, diagnostic) {
|
|
|
33
37
|
message.diagnostics = [...message.diagnostics ?? [], diagnostic];
|
|
34
38
|
}
|
|
35
39
|
//#endregion
|
|
36
|
-
export { formatThrownValue as i, createAssistantMessageDiagnostic as n, extractDiagnosticError as r, appendAssistantMessageDiagnostic as t };
|
|
40
|
+
export { isProviderRefusalAssistantError as a, formatThrownValue as i, createAssistantMessageDiagnostic as n, extractDiagnosticError as r, appendAssistantMessageDiagnostic as t };
|