@openclaw/ai 2026.9.2 → 2026.9.4
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/README.md +3 -2
- package/dist/anthropic-C4Qu4H0Z.mjs +377 -0
- package/dist/{anthropic-payload-policy-d46X2pR0.d.mts → anthropic-payload-policy-BZ8umAbk.d.mts} +16 -6
- package/dist/{anthropic-compaction-replay-DB2FGLQc.mjs → anthropic-stream-reducer-CILWF7JD.mjs} +931 -184
- package/dist/{api-registry-DWtPjzyn.d.mts → api-registry-ByUwIR0e.d.mts} +2 -2
- package/dist/assistant-output-tLt4H-iQ.mjs +16 -0
- package/dist/assistant-text-phase-C20rxWwP.mjs +55 -0
- package/dist/{azure-openai-responses-C5bZRfAP.mjs → azure-openai-responses-BAlqlKKc.mjs} +5 -5
- package/dist/{base64-CEFBpSkN.mjs → base64-D-su8YVo.mjs} +1 -27
- package/dist/{diagnostics-DfFyKeX_.mjs → diagnostics-QuErwCIl.mjs} +96 -21
- package/dist/diagnostics.d.mts +3 -1
- package/dist/diagnostics.mjs +3 -3
- package/dist/{event-stream-BhT5T1Ay.d.mts → event-stream-Bt5Y4Pav.d.mts} +11 -3
- package/dist/event-stream-C3WGFsum.d.mts +1 -0
- package/dist/{event-stream-BgDvQeum.mjs → event-stream-D8PARQfL.mjs} +45 -1
- package/dist/event-stream.d.mts +2 -2
- package/dist/event-stream.mjs +1 -1
- package/dist/{github-copilot-headers-NCJtz9i0.mjs → github-copilot-headers-B37TB3rB.mjs} +3 -10
- package/dist/github-copilot-request-facts-BTEBMeOv.mjs +16 -0
- package/dist/{google-4qeuE8iX.mjs → google-DPBAOaOW.mjs} +10 -10
- package/dist/google-messages-6JkpHrhJ.mjs +448 -0
- package/dist/google-shared-BvBeW9aq.mjs +185 -0
- package/dist/{google-vertex-O9BcnB6X.mjs → google-vertex-k-TMCAYD.mjs} +7 -6
- package/dist/{host-DjzGmdZ2.d.mts → host-4atIX-2V.d.mts} +5 -3
- package/dist/{host-CEvLw30U.mjs → host-B8YfDGd4.mjs} +181 -41
- package/dist/host-policy-Zcg_cNz8.mjs +37 -0
- package/dist/{index-FnHM2FcI.d.mts → index-DdD3qerf.d.mts} +4 -4
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +7 -6
- package/dist/internal/anthropic.d.mts +7 -7
- package/dist/internal/anthropic.mjs +4 -4
- package/dist/internal/google-model-family.d.mts +5 -0
- package/dist/internal/google-model-family.mjs +15 -0
- package/dist/internal/openai-responses-payload-policy.d.mts +3 -3
- package/dist/internal/openai-responses-payload-policy.mjs +3 -2
- package/dist/internal/openai.d.mts +8 -103
- package/dist/internal/openai.mjs +9 -10
- package/dist/internal/retry-after.d.mts +2 -4
- package/dist/internal/retry-after.mjs +57 -8
- package/dist/internal/runtime.d.mts +43 -7
- package/dist/internal/runtime.mjs +6 -7
- package/dist/internal/shared.d.mts +43 -3
- package/dist/internal/shared.mjs +5 -4
- package/dist/internal/tool-schema.d.mts +63 -0
- package/dist/internal/tool-schema.mjs +3 -0
- package/dist/{mistral-Tb6oalqH.mjs → mistral-CxUZ1jUb.mjs} +10 -37
- package/dist/model-transport-url-DKocrEsb.d.mts +28 -0
- package/dist/{openai-chatgpt-responses-yUXjPNfu.mjs → openai-chatgpt-responses-CgO6kZfo.mjs} +50 -41
- package/dist/{openai-completions-compat-CpjYUArk.d.mts → openai-completions-compat-eHgh5UPE.d.mts} +26 -4
- package/dist/{openai-completions-stream-BsrBe3Gg.mjs → openai-completions-stream-Da2vvl-S.mjs} +654 -453
- package/dist/openai-completions-yJuk7eis.mjs +187 -0
- package/dist/openai-prompt-cache-B4eYo2-I.d.mts +16 -0
- package/dist/openai-prompt-cache-Bds-n_9Q.mjs +220 -0
- package/dist/openai-provider-client-S2gCrM2Z.mjs +24 -0
- package/dist/{openai-responses-CCL41ALM.mjs → openai-responses-DaYwH05E.mjs} +14 -29
- package/dist/{openai-responses-compaction-window-BMVHFOtq.mjs → openai-responses-compaction-window-CIhBAkkq.mjs} +6 -246
- package/dist/{openai-responses-contracts-rF5DDRNS.d.mts → openai-responses-contracts-BjBAqAg_.d.mts} +5 -38
- package/dist/openai-responses-contracts-DDOHA62Y.mjs +245 -0
- package/dist/{openai-responses-prompt-observer-internal-D0bhBfgL.mjs → openai-responses-prompt-observer-internal-f8J7wpsk.mjs} +3 -3
- package/dist/{openai-responses-shared-Bmma_3Qc.mjs → openai-responses-shared-B8RdBPCv.mjs} +47 -118
- package/dist/openai-tool-projection-793cE3QX.d.mts +37 -0
- package/dist/{openai-tool-schema-_pTAJqKF.mjs → openai-tool-schema-CzjyYXun.mjs} +38 -556
- package/dist/openai-tool-schema-ynZBgqW-.d.mts +38 -0
- package/dist/openai-transport-params-9aPuV5YY.mjs +675 -0
- package/dist/positive-integer-41zhOdcV.mjs +8 -0
- package/dist/{provider-error-9TraxGvt.mjs → provider-error-BA-v_tKd.mjs} +70 -11
- package/dist/{provider-options-Bcc_p-TU.d.mts → provider-options-Ceqv1OKk.d.mts} +4 -39
- package/dist/{provider-replay-context-BuSUaAk5.mjs → provider-replay-context-CJ_YvcEW.mjs} +1 -1
- package/dist/{provider-transcript-transform-BaMbI1hr.mjs → provider-transcript-transform-V5YzU9zh.mjs} +1 -1
- package/dist/provider-transport-turn-state-D5EXOFL2.mjs +42 -0
- package/dist/provider-types-CVjKjsuq.d.mts +29 -0
- package/dist/provider-types.d.mts +6 -31
- package/dist/providers.d.mts +2 -2
- package/dist/providers.mjs +11 -11
- package/dist/{reasoning-tag-text-partitioner-DLCNXki6.mjs → reasoning-tag-text-partitioner-BcR5pztD.mjs} +19 -19
- package/dist/record-coerce-DwRYMj3t.mjs +32 -0
- package/dist/retry-after-CdCURCVg.d.mts +15 -0
- package/dist/session-affinity-Bcunsn4I.mjs +23 -0
- package/dist/{simple-options-BjHCCh4v.mjs → simple-options-BQbb4yQL.mjs} +24 -5
- package/dist/src-DDmEryvj.mjs +2 -0
- package/dist/{stream-first-event-timeout-DcNjoFQE.mjs → stream-first-event-timeout-2hfrquuz.mjs} +1 -1
- package/dist/string-coerce-fsri9iCu.mjs +34 -0
- package/dist/{string-normalization--fwJ4S2q.mjs → string-normalization-CmLIasuf.mjs} +1 -1
- package/dist/{tool-schema-json-projection-mJhXDcyz.mjs → tool-schema-json-projection-ClptDdAO.mjs} +2 -38
- package/dist/{transport-stream-shared-xnaqxmbP.d.mts → transport-stream-shared-DNvmoWnv.d.mts} +20 -36
- package/dist/{transport-stream-shared-CZqMhfIw.mjs → transport-stream-shared-zHll9BxO.mjs} +225 -13
- package/dist/transport-utils-zrYjICLZ.mjs +118 -0
- package/dist/transports.d.mts +137 -193
- package/dist/transports.mjs +204 -1334
- package/dist/types-DlfwzH3T.d.mts +1 -0
- package/dist/types-LFWwv0cF.mjs +8 -0
- package/dist/{types-CJ1-Ht7A.d.mts → types-Ntv5z2g2.d.mts} +74 -57
- package/dist/types.d.mts +6 -6
- package/dist/types.mjs +6 -5
- package/dist/{src-2qBGKg8O.mjs → usage-cost-BNWbbXav.mjs} +0 -1
- package/dist/utf16-slice-CvGodqok.mjs +29 -0
- package/dist/{validation-AKZBDGQd.d.mts → validation-B0t_G2H6.d.mts} +1 -1
- package/dist/{validation-CJZtym2g.mjs → validation-BDzVDnTs.mjs} +9 -1
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +13 -3
- package/dist/anthropic-BDdqdVLK.mjs +0 -886
- package/dist/event-stream-zctLx0yr.d.mts +0 -1
- package/dist/google-shared-CWeG8RIl.mjs +0 -636
- package/dist/google-thinking-level-C-V3tecN.mjs +0 -9
- package/dist/hash-CHgqbJmD.mjs +0 -16
- package/dist/json-parse-BuAJEbdW.mjs +0 -146
- package/dist/openai-completions-BIUV3RDT.mjs +0 -403
- package/dist/openai-prompt-cache-CGnVB74a.mjs +0 -21
- package/dist/openai-prompt-cache-CNoIfHYC.d.mts +0 -16
- package/dist/sanitize-unicode-D6xUvZaS.mjs +0 -83
- package/dist/transport-utils-7il795_9.mjs +0 -138
- package/dist/types-3Lnm-QSJ.d.mts +0 -1
- package/dist/types-BDdaOVi2.mjs +0 -6
- package/dist/utf16-slice-qz3nsy87.mjs +0 -84
package/dist/{anthropic-compaction-replay-DB2FGLQc.mjs → anthropic-stream-reducer-CILWF7JD.mjs}
RENAMED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import { d as resolveClaudeModelIdentity, m as resolveClaudeOpus5ModelIdentity, u as resolveClaudeFable5ModelIdentity } from "./
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { _ as supportsClaudeAdaptiveThinking, b as supportsClaudeNativeXhighEffort, d as resolveClaudeModelIdentity, m as resolveClaudeOpus5ModelIdentity, s as bindsClaudeThinkingPrefix, u as resolveClaudeFable5ModelIdentity } from "./usage-cost-BNWbbXav.mjs";
|
|
2
|
+
import { r as appendAssistantThinking } from "./event-stream-D8PARQfL.mjs";
|
|
3
|
+
import { i as normalizeOptionalLowercaseString, r as normalizeNullableString } from "./string-coerce-fsri9iCu.mjs";
|
|
4
|
+
import { C as mapAnthropicStopReason, T as requiresClaudeAdaptiveThinking, _ as isImageWithMediaPayload, d as describeToolResultMediaPlaceholder, j as calculateCost, m as extractToolResultText, n as getAiTransportHost, p as extractToolResultBlockText, v as sanitizeSurrogates, y as ANTHROPIC_CLAUDE_CODE_BILLING_SYSTEM_BLOCK } from "./host-B8YfDGd4.mjs";
|
|
5
|
+
import { a as asRecord, o as isRecord, r as asOptionalObjectRecord, s as readStringField } from "./record-coerce-DwRYMj3t.mjs";
|
|
6
|
+
import { i as asNonNegativeFiniteNumber, n as estimateBase64DecodedBytes } from "./base64-D-su8YVo.mjs";
|
|
7
|
+
import "./transport-utils-zrYjICLZ.mjs";
|
|
8
|
+
import { a as resolveProviderRequestCapabilities, i as resolveProviderEndpoint } from "./host-policy-Zcg_cNz8.mjs";
|
|
5
9
|
import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
+
import { f as splitSystemPromptCacheBoundary, h as stripSystemPromptRelocatableBoundary, m as stripSystemPromptCacheBoundary, v as sortPromptCacheToolsByName } from "./simple-options-BQbb4yQL.mjs";
|
|
11
|
+
import { t as parsePositiveInteger } from "./positive-integer-41zhOdcV.mjs";
|
|
12
|
+
import { t as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-ClptDdAO.mjs";
|
|
13
|
+
import { E as parseStreamingJson, _ as sanitizeTransportPayloadText, c as finalizeTerminalToolCallArguments, g as sanitizeNonEmptyTransportPayloadText, r as coerceTransportToolCallArguments, v as transportAbortError, w as createToolArgumentPreviewSchedule, x as parseJsonObjectPreservingUnsafeIntegers } from "./transport-stream-shared-zHll9BxO.mjs";
|
|
14
|
+
import { n as providerReplayContextMatches, t as buildProviderReplayContext } from "./provider-replay-context-CJ_YvcEW.mjs";
|
|
15
|
+
import { i as tagPendingCommentaryText } from "./assistant-text-phase-C20rxWwP.mjs";
|
|
16
|
+
import { t as notifyLlmRequestActivity } from "./llm-request-activity-BjtkplhG.mjs";
|
|
10
17
|
//#region packages/ai/src/providers/anthropic-auth-headers.ts
|
|
11
18
|
/** Inspect resolved credential shape for auth routing without exposing its value. */
|
|
12
19
|
function isAnthropicOAuthApiKey(apiKey) {
|
|
@@ -89,6 +96,48 @@ function resolveAnthropicEphemeralCacheControl(baseUrl, cacheRetention) {
|
|
|
89
96
|
...ttl ? { ttl } : {}
|
|
90
97
|
};
|
|
91
98
|
}
|
|
99
|
+
function resolveAnthropicCacheOptions(model, cacheRetention) {
|
|
100
|
+
const supportsLongCacheRetention = model.compat?.supportsLongCacheRetention ?? model.provider !== "fireworks";
|
|
101
|
+
const cacheControl = resolveAnthropicEphemeralCacheControl(model.baseUrl?.trim() || process.env.ANTHROPIC_BASE_URL?.trim() || "https://api.anthropic.com", cacheRetention);
|
|
102
|
+
return {
|
|
103
|
+
cacheControl: cacheControl && !supportsLongCacheRetention ? { type: "ephemeral" } : cacheControl,
|
|
104
|
+
supportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? model.provider !== "fireworks"
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function buildAnthropicSystemBlocks(systemPrompt, isOAuthToken, cacheControl) {
|
|
108
|
+
const blocks = systemPrompt ? [{
|
|
109
|
+
type: "text",
|
|
110
|
+
text: sanitizeSurrogates(systemPrompt)
|
|
111
|
+
}] : [];
|
|
112
|
+
if (cacheControl) applyAnthropicCacheControlToSystem(blocks, cacheControl);
|
|
113
|
+
else stripAnthropicSystemPromptBoundary(blocks);
|
|
114
|
+
if (systemPrompt && blocks.length === 0) blocks.push({
|
|
115
|
+
type: "text",
|
|
116
|
+
text: ""
|
|
117
|
+
});
|
|
118
|
+
if (isOAuthToken) {
|
|
119
|
+
const identityCacheControl = blocks.some((block) => block.cache_control) ? void 0 : cacheControl;
|
|
120
|
+
blocks.unshift({
|
|
121
|
+
type: "text",
|
|
122
|
+
text: ANTHROPIC_CLAUDE_CODE_BILLING_SYSTEM_BLOCK
|
|
123
|
+
}, {
|
|
124
|
+
type: "text",
|
|
125
|
+
text: "You are Claude Code, Anthropic's official CLI for Claude.",
|
|
126
|
+
...identityCacheControl ? { cache_control: identityCacheControl } : {}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return blocks.length > 0 ? blocks : void 0;
|
|
130
|
+
}
|
|
131
|
+
/** Allocate tool and history checkpoints around the prepared stable system blocks. */
|
|
132
|
+
function applyAnthropicRequestCacheControl(payload, cacheControl, supportsCacheControlOnTools, cacheBreakpointOptOutMessageIndexes = /* @__PURE__ */ new Set()) {
|
|
133
|
+
if (!cacheControl) return;
|
|
134
|
+
if (supportsCacheControlOnTools && Array.isArray(payload.tools)) {
|
|
135
|
+
const lastTool = payload.tools.at(-1);
|
|
136
|
+
if (isRecord(lastTool)) lastTool.cache_control = cacheControl;
|
|
137
|
+
}
|
|
138
|
+
const usedMarkers = countAnthropicCacheControlMarkers(payload.system) + countAnthropicCacheControlMarkers(payload.tools);
|
|
139
|
+
applyAnthropicCacheControlToMessages(payload.messages, cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
|
|
140
|
+
}
|
|
92
141
|
function applyAnthropicCacheControlToSystem(system, cacheControl) {
|
|
93
142
|
if (!Array.isArray(system)) return;
|
|
94
143
|
const normalizedBlocks = [];
|
|
@@ -98,11 +147,14 @@ function applyAnthropicCacheControlToSystem(system, cacheControl) {
|
|
|
98
147
|
continue;
|
|
99
148
|
}
|
|
100
149
|
const record = block;
|
|
101
|
-
|
|
150
|
+
const blockText = record.text;
|
|
151
|
+
if (record.type !== "text" || typeof blockText !== "string") {
|
|
102
152
|
normalizedBlocks.push(block);
|
|
103
153
|
continue;
|
|
104
154
|
}
|
|
105
|
-
const
|
|
155
|
+
const text = stripSystemPromptRelocatableBoundary(blockText);
|
|
156
|
+
record.text = text;
|
|
157
|
+
const split = splitSystemPromptCacheBoundary(text);
|
|
106
158
|
if (!split) {
|
|
107
159
|
if (record.cache_control === void 0) record.cache_control = cacheControl;
|
|
108
160
|
normalizedBlocks.push(record);
|
|
@@ -286,9 +338,7 @@ function applyAnthropicPayloadPolicyToParams(payloadObj, policy, cacheBreakpoint
|
|
|
286
338
|
if (policy.cacheControl) applyAnthropicCacheControlToSystem(payloadObj.system, policy.cacheControl);
|
|
287
339
|
else stripAnthropicSystemPromptBoundary(payloadObj.system);
|
|
288
340
|
applyAnthropicContextManagementEdits(payloadObj, policy);
|
|
289
|
-
|
|
290
|
-
const usedMarkers = countAnthropicCacheControlMarkers(payloadObj.system) + countAnthropicCacheControlMarkers(payloadObj.tools);
|
|
291
|
-
applyAnthropicCacheControlToMessages(payloadObj.messages, policy.cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
|
|
341
|
+
applyAnthropicRequestCacheControl(payloadObj, policy.cacheControl, false, cacheBreakpointOptOutMessageIndexes);
|
|
292
342
|
}
|
|
293
343
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
294
344
|
function applyAnthropicEphemeralCacheControlMarkers(payloadObj, cacheControl = { type: "ephemeral" }) {
|
|
@@ -322,70 +372,6 @@ function applyAnthropicEphemeralCacheControlMarkers(payloadObj, cacheControl = {
|
|
|
322
372
|
}
|
|
323
373
|
}
|
|
324
374
|
//#endregion
|
|
325
|
-
//#region packages/ai/src/internal/anthropic-inline-images.ts
|
|
326
|
-
const ANTHROPIC_IMAGE_MEDIA_TYPE_SET = /* @__PURE__ */ new Set([
|
|
327
|
-
"image/jpeg",
|
|
328
|
-
"image/png",
|
|
329
|
-
"image/gif",
|
|
330
|
-
"image/webp"
|
|
331
|
-
]);
|
|
332
|
-
const ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES = 67108864;
|
|
333
|
-
function createAnthropicInlineImageBudget() {
|
|
334
|
-
return { totalBytes: 0 };
|
|
335
|
-
}
|
|
336
|
-
function resolveAnthropicImageMediaType(value) {
|
|
337
|
-
if (ANTHROPIC_IMAGE_MEDIA_TYPE_SET.has(value)) return value;
|
|
338
|
-
throw new Error(`Unsupported Anthropic image media type after normalization: ${value}`);
|
|
339
|
-
}
|
|
340
|
-
async function normalizeAnthropicInlineContent(content, budget) {
|
|
341
|
-
if (!content.some((block) => block.type === "image")) return content.filter((block) => block.type === "text");
|
|
342
|
-
const inputBytes = content.reduce((total, block) => block.type === "image" ? total + estimateBase64DecodedBytes(block.data) : total, 0);
|
|
343
|
-
if (budget.totalBytes + inputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
|
|
344
|
-
const normalized = [];
|
|
345
|
-
for (const block of content) {
|
|
346
|
-
if (block.type !== "image") {
|
|
347
|
-
normalized.push(block);
|
|
348
|
-
continue;
|
|
349
|
-
}
|
|
350
|
-
const normalizedBlocks = await getAiTransportHost().normalizeAnthropicInlineContentBlocks([block]);
|
|
351
|
-
const outputBytes = normalizedBlocks.reduce((total, normalizedBlock) => normalizedBlock.type === "image" ? total + estimateBase64DecodedBytes(normalizedBlock.data) : total, 0);
|
|
352
|
-
if (budget.totalBytes + outputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
|
|
353
|
-
budget.totalBytes += outputBytes;
|
|
354
|
-
normalized.push(...normalizedBlocks);
|
|
355
|
-
}
|
|
356
|
-
return normalized;
|
|
357
|
-
}
|
|
358
|
-
//#endregion
|
|
359
|
-
//#region packages/ai/src/providers/anthropic-refusal.ts
|
|
360
|
-
function readAnthropicRefusalDetails(value) {
|
|
361
|
-
if (!value || typeof value !== "object") return {
|
|
362
|
-
category: null,
|
|
363
|
-
explanation: null
|
|
364
|
-
};
|
|
365
|
-
const details = value;
|
|
366
|
-
return {
|
|
367
|
-
category: normalizeNullableString(details.category),
|
|
368
|
-
explanation: normalizeNullableString(details.explanation)
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
|
-
function formatAnthropicRefusalMessage(details) {
|
|
372
|
-
return `Anthropic refusal${details.category ? ` (category: ${details.category})` : ""}${details.explanation ? `: ${details.explanation}` : "."}`;
|
|
373
|
-
}
|
|
374
|
-
function applyAnthropicRefusal(output, stopDetails, provider) {
|
|
375
|
-
const details = readAnthropicRefusalDetails(stopDetails);
|
|
376
|
-
output.stopReason = "error";
|
|
377
|
-
output.errorMessage = formatAnthropicRefusalMessage(details);
|
|
378
|
-
output.diagnostics = [...output.diagnostics ?? [], {
|
|
379
|
-
type: "provider_refusal",
|
|
380
|
-
timestamp: Date.now(),
|
|
381
|
-
details: {
|
|
382
|
-
provider,
|
|
383
|
-
category: details.category,
|
|
384
|
-
explanation: details.explanation
|
|
385
|
-
}
|
|
386
|
-
}];
|
|
387
|
-
}
|
|
388
|
-
//#endregion
|
|
389
375
|
//#region packages/ai/src/providers/anthropic-server-fallback.ts
|
|
390
376
|
/** Anthropic beta that re-serves safety refusals on an allowed fallback model. */
|
|
391
377
|
const ANTHROPIC_SERVER_SIDE_FALLBACK_BETA = "server-side-fallback-2026-07-01";
|
|
@@ -594,11 +580,13 @@ function normalizeAnthropicJsonSchema(schema) {
|
|
|
594
580
|
delete normalized.additionalItems;
|
|
595
581
|
changed = true;
|
|
596
582
|
}
|
|
583
|
+
if (changed) delete normalized.$schema;
|
|
597
584
|
return changed ? normalized : schema;
|
|
598
585
|
}
|
|
599
586
|
/** Snapshots direct/custom tool descriptors before Anthropic payload construction. */
|
|
600
587
|
function projectAnthropicTools(tools, toWireName) {
|
|
601
588
|
const projectedTools = [];
|
|
589
|
+
const originalNameByWireName = /* @__PURE__ */ new Map();
|
|
602
590
|
const unavailableOriginalNames = /* @__PURE__ */ new Set();
|
|
603
591
|
for (const tool of tools) {
|
|
604
592
|
let projectedTool;
|
|
@@ -632,6 +620,7 @@ function projectAnthropicTools(tools, toWireName) {
|
|
|
632
620
|
wireName: toWireName(name),
|
|
633
621
|
...description ? { description } : {},
|
|
634
622
|
inputSchema: {
|
|
623
|
+
...anthropicSchema,
|
|
635
624
|
type: "object",
|
|
636
625
|
properties: properties ?? {},
|
|
637
626
|
required: required ?? []
|
|
@@ -641,8 +630,9 @@ function projectAnthropicTools(tools, toWireName) {
|
|
|
641
630
|
if (originalName) unavailableOriginalNames.add(originalName);
|
|
642
631
|
continue;
|
|
643
632
|
}
|
|
644
|
-
const
|
|
645
|
-
if (
|
|
633
|
+
const conflictingName = originalNameByWireName.get(projectedTool.wireName);
|
|
634
|
+
if (conflictingName !== void 0 && conflictingName !== projectedTool.originalName) throw new Error(`Anthropic tool names "${conflictingName}" and "${projectedTool.originalName}" both map to "${projectedTool.wireName}"`);
|
|
635
|
+
originalNameByWireName.set(projectedTool.wireName, projectedTool.originalName);
|
|
646
636
|
projectedTools.push(projectedTool);
|
|
647
637
|
}
|
|
648
638
|
return {
|
|
@@ -680,6 +670,426 @@ function resolveOriginalAnthropicToolName(name, projection) {
|
|
|
680
670
|
return projection?.tools.find((tool) => tool.wireName === name)?.originalName ?? name;
|
|
681
671
|
}
|
|
682
672
|
//#endregion
|
|
673
|
+
//#region packages/ai/src/transports/anthropic-compaction-replay.ts
|
|
674
|
+
const ANTHROPIC_COMPACTION_REPLAY_TYPE = "anthropic-compaction";
|
|
675
|
+
const ANTHROPIC_COMPACTION_SUPPRESSION_TYPE = "anthropic-compaction-suppression";
|
|
676
|
+
const ANTHROPIC_COMPACTION_SUPPRESSION_DATA = "rejected";
|
|
677
|
+
function createCompactionCapture(output, model, options) {
|
|
678
|
+
const pending = /* @__PURE__ */ new Map();
|
|
679
|
+
return {
|
|
680
|
+
begin(index, block, replayIndex) {
|
|
681
|
+
if (block?.type !== "compaction") return false;
|
|
682
|
+
const content = typeof block.content === "string" ? block.content : "";
|
|
683
|
+
pending.set(index, {
|
|
684
|
+
content,
|
|
685
|
+
replayIndex
|
|
686
|
+
});
|
|
687
|
+
return true;
|
|
688
|
+
},
|
|
689
|
+
delta(index, delta) {
|
|
690
|
+
const capture = pending.get(index);
|
|
691
|
+
if (!capture || delta?.type !== "compaction_delta") return false;
|
|
692
|
+
capture.content = typeof delta.content === "string" ? delta.content : capture.content;
|
|
693
|
+
return true;
|
|
694
|
+
},
|
|
695
|
+
complete(index) {
|
|
696
|
+
const capture = pending.get(index);
|
|
697
|
+
if (!capture) return false;
|
|
698
|
+
pending.delete(index);
|
|
699
|
+
captureAnthropicCompaction(output, capture.content, capture.replayIndex, model, options);
|
|
700
|
+
return true;
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
function isAnthropicCompactionState(state) {
|
|
705
|
+
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;
|
|
706
|
+
if (state.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) return state.data === ANTHROPIC_COMPACTION_SUPPRESSION_DATA;
|
|
707
|
+
return state.type === ANTHROPIC_COMPACTION_REPLAY_TYPE && state.data.length > 0 && (state.replayIndex === void 0 || typeof state.replayIndex === "number" && Number.isSafeInteger(state.replayIndex) && state.replayIndex >= 0);
|
|
708
|
+
}
|
|
709
|
+
function readAnthropicCompactionState(value) {
|
|
710
|
+
return isRecord(value) && isAnthropicCompactionState(value) ? value : void 0;
|
|
711
|
+
}
|
|
712
|
+
function captureAnthropicCompaction(output, summary, replayIndex, model, options) {
|
|
713
|
+
const context = buildProviderReplayContext(model, options);
|
|
714
|
+
if (!summary || !context.baseUrlHash || !Number.isSafeInteger(replayIndex) || replayIndex < 0) return;
|
|
715
|
+
output.providerReplay = {
|
|
716
|
+
v: 1,
|
|
717
|
+
type: ANTHROPIC_COMPACTION_REPLAY_TYPE,
|
|
718
|
+
data: summary,
|
|
719
|
+
replayIndex,
|
|
720
|
+
...context,
|
|
721
|
+
baseUrlHash: context.baseUrlHash
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
function suppressAnthropicCompaction(output, model, options) {
|
|
725
|
+
const context = buildProviderReplayContext(model, options);
|
|
726
|
+
if (!context.baseUrlHash) return;
|
|
727
|
+
output.providerReplay = {
|
|
728
|
+
v: 1,
|
|
729
|
+
type: ANTHROPIC_COMPACTION_SUPPRESSION_TYPE,
|
|
730
|
+
data: ANTHROPIC_COMPACTION_SUPPRESSION_DATA,
|
|
731
|
+
...context,
|
|
732
|
+
baseUrlHash: context.baseUrlHash
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
function resolveNewestAnthropicCompaction(messages, model, options) {
|
|
736
|
+
const context = buildProviderReplayContext(model, options);
|
|
737
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
738
|
+
const message = messages[index];
|
|
739
|
+
if (message?.role !== "assistant") continue;
|
|
740
|
+
const replayState = readAnthropicCompactionState(message.providerReplay);
|
|
741
|
+
if (replayState?.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) {
|
|
742
|
+
if (providerReplayContextMatches(replayState, context)) return;
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
if (message.providerReplay?.type !== ANTHROPIC_COMPACTION_REPLAY_TYPE) continue;
|
|
746
|
+
if (!replayState) return;
|
|
747
|
+
if (!providerReplayContextMatches(replayState, context)) return;
|
|
748
|
+
return {
|
|
749
|
+
owner: message,
|
|
750
|
+
replayIndex: replayState.replayIndex ?? 0,
|
|
751
|
+
summary: replayState.data
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
function buildAnthropicReplayPlan(messages, model, options) {
|
|
756
|
+
if (options?.enabled !== true) return { messages };
|
|
757
|
+
const checkpoint = resolveNewestAnthropicCompaction(messages, model, options);
|
|
758
|
+
if (!checkpoint) return { messages };
|
|
759
|
+
const ownerIndex = messages.indexOf(checkpoint.owner);
|
|
760
|
+
return {
|
|
761
|
+
messages: [{
|
|
762
|
+
...checkpoint.owner,
|
|
763
|
+
content: checkpoint.owner.content.slice(checkpoint.replayIndex)
|
|
764
|
+
}, ...messages.slice(ownerIndex + 1)],
|
|
765
|
+
compaction: {
|
|
766
|
+
type: "compaction",
|
|
767
|
+
content: checkpoint.summary
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
/** Match Anthropic's 400 response when a replay block is no longer accepted. */
|
|
772
|
+
function isAnthropicReplayRejection(error) {
|
|
773
|
+
if (!error || typeof error !== "object") return false;
|
|
774
|
+
const record = error;
|
|
775
|
+
const nested = record.error && typeof record.error === "object" && !Array.isArray(record.error) ? record.error : void 0;
|
|
776
|
+
const text = [
|
|
777
|
+
record.message,
|
|
778
|
+
record.code,
|
|
779
|
+
nested?.message,
|
|
780
|
+
nested?.code
|
|
781
|
+
].filter((value) => typeof value === "string").join(" ").toLowerCase();
|
|
782
|
+
return (record.status === 400 || text.startsWith("http 400:")) && (text.includes("compaction") || text.includes("context_management"));
|
|
783
|
+
}
|
|
784
|
+
//#endregion
|
|
785
|
+
//#region packages/ai/src/internal/anthropic-inline-images.ts
|
|
786
|
+
const ANTHROPIC_IMAGE_MEDIA_TYPE_SET = /* @__PURE__ */ new Set([
|
|
787
|
+
"image/jpeg",
|
|
788
|
+
"image/png",
|
|
789
|
+
"image/gif",
|
|
790
|
+
"image/webp"
|
|
791
|
+
]);
|
|
792
|
+
const ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES = 67108864;
|
|
793
|
+
function createAnthropicInlineImageBudget() {
|
|
794
|
+
return { totalBytes: 0 };
|
|
795
|
+
}
|
|
796
|
+
function resolveAnthropicImageMediaType(value) {
|
|
797
|
+
if (ANTHROPIC_IMAGE_MEDIA_TYPE_SET.has(value)) return value;
|
|
798
|
+
throw new Error(`Unsupported Anthropic image media type after normalization: ${value}`);
|
|
799
|
+
}
|
|
800
|
+
async function normalizeAnthropicInlineContent(content, budget) {
|
|
801
|
+
if (!content.some((block) => block.type === "image")) return content.filter((block) => block.type === "text");
|
|
802
|
+
const inputBytes = content.reduce((total, block) => block.type === "image" ? total + estimateBase64DecodedBytes(block.data) : total, 0);
|
|
803
|
+
if (budget.totalBytes + inputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
|
|
804
|
+
const normalized = [];
|
|
805
|
+
for (const block of content) {
|
|
806
|
+
if (block.type !== "image") {
|
|
807
|
+
normalized.push(block);
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
const normalizedBlocks = await getAiTransportHost().normalizeAnthropicInlineContentBlocks([block]);
|
|
811
|
+
const outputBytes = normalizedBlocks.reduce((total, normalizedBlock) => normalizedBlock.type === "image" ? total + estimateBase64DecodedBytes(normalizedBlock.data) : total, 0);
|
|
812
|
+
if (budget.totalBytes + outputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
|
|
813
|
+
budget.totalBytes += outputBytes;
|
|
814
|
+
normalized.push(...normalizedBlocks);
|
|
815
|
+
}
|
|
816
|
+
return normalized;
|
|
817
|
+
}
|
|
818
|
+
//#endregion
|
|
819
|
+
//#region packages/ai/src/transports/anthropic-messages.ts
|
|
820
|
+
const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
821
|
+
async function convertContentBlocks(content, model, imageBudget, profile, isError) {
|
|
822
|
+
const mediaPlaceholder = describeToolResultMediaPlaceholder(content);
|
|
823
|
+
if (!((profile === "provider" || model.input.includes("image")) && content.some(isImageWithMediaPayload))) return sanitizeNonEmptyTransportPayloadText(extractToolResultText(content), mediaPlaceholder ?? (profile === "transport" ? "(no output)" : isError ? "[tool error with no output]" : ""));
|
|
824
|
+
const blocks = [];
|
|
825
|
+
let hasTextBlock = false;
|
|
826
|
+
for (const block of content) {
|
|
827
|
+
const record = asOptionalObjectRecord(block);
|
|
828
|
+
if (!record) continue;
|
|
829
|
+
const blockText = extractToolResultBlockText(block);
|
|
830
|
+
if (blockText) {
|
|
831
|
+
blocks.push({
|
|
832
|
+
type: "text",
|
|
833
|
+
text: sanitizeTransportPayloadText(blockText)
|
|
834
|
+
});
|
|
835
|
+
hasTextBlock = true;
|
|
836
|
+
}
|
|
837
|
+
if (!isImageWithMediaPayload(record)) continue;
|
|
838
|
+
const [normalizedImage] = await normalizeAnthropicInlineContent([{
|
|
839
|
+
type: "image",
|
|
840
|
+
data: typeof record.data === "string" ? record.data : "",
|
|
841
|
+
mimeType: typeof record.mimeType === "string" ? record.mimeType : profile === "provider" ? "image/jpeg" : "image/png"
|
|
842
|
+
}], imageBudget);
|
|
843
|
+
if (normalizedImage?.type !== "image") continue;
|
|
844
|
+
blocks.push({
|
|
845
|
+
type: "image",
|
|
846
|
+
source: {
|
|
847
|
+
type: "base64",
|
|
848
|
+
media_type: resolveAnthropicImageMediaType(normalizedImage.mimeType),
|
|
849
|
+
data: normalizedImage.data
|
|
850
|
+
}
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
if (!hasTextBlock) blocks.unshift({
|
|
854
|
+
type: "text",
|
|
855
|
+
text: mediaPlaceholder ?? "(see attached image)"
|
|
856
|
+
});
|
|
857
|
+
return blocks;
|
|
858
|
+
}
|
|
859
|
+
async function convertAnthropicMessages(transformedMessages, model, isOAuthToken, options) {
|
|
860
|
+
const params = [];
|
|
861
|
+
const retainRuntimeContext = bindsClaudeThinkingPrefix(model);
|
|
862
|
+
const imageBudget = createAnthropicInlineImageBudget();
|
|
863
|
+
const allowReasoningContentReplay = options.allowReasoningContentReplay === true;
|
|
864
|
+
const replayThinkingEnabled = options.replayThinkingEnabled !== false;
|
|
865
|
+
const managed = options.profile === "transport";
|
|
866
|
+
const activeToolTurnAssistantIndex = replayThinkingEnabled ? -1 : findActiveAnthropicToolTurnAssistantIndex(transformedMessages);
|
|
867
|
+
for (let i = 0; i < transformedMessages.length; i += 1) {
|
|
868
|
+
const msg = transformedMessages[i];
|
|
869
|
+
if (!msg) continue;
|
|
870
|
+
if (msg.role === "user") {
|
|
871
|
+
if (typeof msg.content === "string") {
|
|
872
|
+
if (msg.content.trim().length > 0) {
|
|
873
|
+
if (msg.runtimeContextCarrier && !retainRuntimeContext) options.cacheBreakpointOptOutMessageIndexes?.add(params.length);
|
|
874
|
+
const userParam = {
|
|
875
|
+
role: "user",
|
|
876
|
+
content: sanitizeTransportPayloadText(msg.content)
|
|
877
|
+
};
|
|
878
|
+
params.push(userParam);
|
|
879
|
+
}
|
|
880
|
+
continue;
|
|
881
|
+
}
|
|
882
|
+
const blocks = (!managed || model.input.includes("image") ? await normalizeAnthropicInlineContent(msg.content, imageBudget) : msg.content.map((item) => item.type === "image" ? {
|
|
883
|
+
type: "text",
|
|
884
|
+
text: NON_VISION_USER_IMAGE_PLACEHOLDER
|
|
885
|
+
} : item)).map((item) => item.type === "text" ? {
|
|
886
|
+
type: "text",
|
|
887
|
+
text: sanitizeTransportPayloadText(item.text)
|
|
888
|
+
} : {
|
|
889
|
+
type: "image",
|
|
890
|
+
source: {
|
|
891
|
+
type: "base64",
|
|
892
|
+
media_type: resolveAnthropicImageMediaType(item.mimeType),
|
|
893
|
+
data: item.data
|
|
894
|
+
}
|
|
895
|
+
});
|
|
896
|
+
let filteredBlocks = !managed || model.input.includes("image") ? blocks : blocks.filter((block) => block.type !== "image");
|
|
897
|
+
filteredBlocks = filteredBlocks.filter((block) => block.type !== "text" || block.text.trim().length > 0);
|
|
898
|
+
if (filteredBlocks.length === 0) continue;
|
|
899
|
+
if (msg.runtimeContextCarrier && !retainRuntimeContext) options.cacheBreakpointOptOutMessageIndexes?.add(params.length);
|
|
900
|
+
const userParam = {
|
|
901
|
+
role: "user",
|
|
902
|
+
content: filteredBlocks
|
|
903
|
+
};
|
|
904
|
+
params.push(userParam);
|
|
905
|
+
continue;
|
|
906
|
+
}
|
|
907
|
+
if (msg.role === "assistant") {
|
|
908
|
+
const blocks = i === 0 && options.compaction ? [options.compaction] : [];
|
|
909
|
+
const reasoningContent = [];
|
|
910
|
+
let omittedThinking = false;
|
|
911
|
+
for (const block of msg.content) {
|
|
912
|
+
if (block.type === "text") {
|
|
913
|
+
if (block.text.trim().length > 0) blocks.push({
|
|
914
|
+
type: "text",
|
|
915
|
+
text: sanitizeTransportPayloadText(block.text)
|
|
916
|
+
});
|
|
917
|
+
continue;
|
|
918
|
+
}
|
|
919
|
+
if (block.type === "thinking") {
|
|
920
|
+
const thinkingSignature = block.thinkingSignature?.trim();
|
|
921
|
+
const isReasoningContent = thinkingSignature === "reasoning_content";
|
|
922
|
+
if (!replayThinkingEnabled && i !== activeToolTurnAssistantIndex && (!managed || !isReasoningContent)) {
|
|
923
|
+
omittedThinking = true;
|
|
924
|
+
continue;
|
|
925
|
+
}
|
|
926
|
+
if (block.redacted) {
|
|
927
|
+
if (!managed && !block.thinkingSignature) throw new Error("redacted thinking block is missing its opaque signature");
|
|
928
|
+
blocks.push({
|
|
929
|
+
type: "redacted_thinking",
|
|
930
|
+
data: block.thinkingSignature
|
|
931
|
+
});
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
const hasNativeThinkingSignature = Boolean(thinkingSignature) && !isReasoningContent;
|
|
935
|
+
if (block.thinking.trim().length === 0 && !hasNativeThinkingSignature) continue;
|
|
936
|
+
if (!thinkingSignature && !options.allowEmptySignature) blocks.push({
|
|
937
|
+
type: "text",
|
|
938
|
+
text: sanitizeTransportPayloadText(block.thinking)
|
|
939
|
+
});
|
|
940
|
+
else {
|
|
941
|
+
const thinking = thinkingSignature === "reasoning_content" ? sanitizeTransportPayloadText(block.thinking) : block.thinking;
|
|
942
|
+
if (thinkingSignature === "reasoning_content") {
|
|
943
|
+
if (allowReasoningContentReplay) {
|
|
944
|
+
blocks.push({
|
|
945
|
+
type: "thinking",
|
|
946
|
+
thinking,
|
|
947
|
+
signature: thinkingSignature ?? ""
|
|
948
|
+
});
|
|
949
|
+
reasoningContent.push(thinking);
|
|
950
|
+
}
|
|
951
|
+
continue;
|
|
952
|
+
}
|
|
953
|
+
blocks.push({
|
|
954
|
+
type: "thinking",
|
|
955
|
+
thinking,
|
|
956
|
+
signature: thinkingSignature ?? ""
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (block.type === "toolCall") blocks.push({
|
|
962
|
+
type: "tool_use",
|
|
963
|
+
id: block.id,
|
|
964
|
+
name: isOAuthToken ? toClaudeCodeToolName(block.name) : block.name,
|
|
965
|
+
input: managed ? coerceTransportToolCallArguments(block.arguments) : block.arguments ?? {}
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
if (blocks.length === 0 && omittedThinking) blocks.push({
|
|
969
|
+
type: "text",
|
|
970
|
+
text: ANTHROPIC_OMITTED_REASONING_TEXT
|
|
971
|
+
});
|
|
972
|
+
if (blocks.length > 0) {
|
|
973
|
+
const assistantMsg = {
|
|
974
|
+
role: "assistant",
|
|
975
|
+
content: blocks
|
|
976
|
+
};
|
|
977
|
+
if (reasoningContent.length > 0) assistantMsg.reasoning_content = reasoningContent.join("\n");
|
|
978
|
+
else if (allowReasoningContentReplay) blocks.unshift({
|
|
979
|
+
type: "thinking",
|
|
980
|
+
thinking: "",
|
|
981
|
+
signature: "reasoning_content"
|
|
982
|
+
});
|
|
983
|
+
params.push(assistantMsg);
|
|
984
|
+
}
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
if (msg.role === "toolResult") {
|
|
988
|
+
const toolResults = [];
|
|
989
|
+
let j = i;
|
|
990
|
+
while (j < transformedMessages.length) {
|
|
991
|
+
const nextMsg = transformedMessages.at(j);
|
|
992
|
+
if (nextMsg?.role !== "toolResult") break;
|
|
993
|
+
toolResults.push({
|
|
994
|
+
type: "tool_result",
|
|
995
|
+
tool_use_id: nextMsg.toolCallId,
|
|
996
|
+
content: await convertContentBlocks(nextMsg.content, model, imageBudget, options.profile, nextMsg.isError),
|
|
997
|
+
is_error: nextMsg.isError
|
|
998
|
+
});
|
|
999
|
+
j += 1;
|
|
1000
|
+
}
|
|
1001
|
+
i = j - 1;
|
|
1002
|
+
params.push({
|
|
1003
|
+
role: "user",
|
|
1004
|
+
content: toolResults
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
return params;
|
|
1009
|
+
}
|
|
1010
|
+
/** Shared generation contract, after each entry point resolves its defaults and tool policy. */
|
|
1011
|
+
function buildAnthropicGenerationParams({ model, options, tools, toolProjection, profile }) {
|
|
1012
|
+
const params = {};
|
|
1013
|
+
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
1014
|
+
if (options?.temperature !== void 0 && !options?.thinkingEnabled && !supportsClaudeNativeXhighEffort(model)) params.temperature = options.temperature;
|
|
1015
|
+
if (options?.stop !== void 0 && options.stop.length > 0) params.stop_sequences = options.stop;
|
|
1016
|
+
if (tools && tools.length > 0) params.tools = tools;
|
|
1017
|
+
if (mandatoryAdaptiveThinking || model.reasoning || supportsClaudeAdaptiveThinking(model)) {
|
|
1018
|
+
if (mandatoryAdaptiveThinking || options?.thinkingEnabled) {
|
|
1019
|
+
const display = options?.thinkingDisplay ?? "summarized";
|
|
1020
|
+
if (supportsClaudeAdaptiveThinking(model)) {
|
|
1021
|
+
params.thinking = {
|
|
1022
|
+
type: "adaptive",
|
|
1023
|
+
display
|
|
1024
|
+
};
|
|
1025
|
+
const effort = options?.effort ?? (mandatoryAdaptiveThinking ? "high" : void 0);
|
|
1026
|
+
if (effort) params.output_config = { effort };
|
|
1027
|
+
} else params.thinking = {
|
|
1028
|
+
type: "enabled",
|
|
1029
|
+
budget_tokens: options?.thinkingBudgetTokens ?? 1024,
|
|
1030
|
+
...profile === "provider" ? { display } : {}
|
|
1031
|
+
};
|
|
1032
|
+
} else if (options?.thinkingEnabled === false) params.thinking = { type: "disabled" };
|
|
1033
|
+
}
|
|
1034
|
+
if (options?.metadata) {
|
|
1035
|
+
const userId = options.metadata.user_id;
|
|
1036
|
+
if (typeof userId === "string") params.metadata = { user_id: userId };
|
|
1037
|
+
}
|
|
1038
|
+
if (options?.toolChoice) {
|
|
1039
|
+
const normalizedToolChoice = normalizeAnthropicToolChoice(mandatoryAdaptiveThinking || options?.thinkingEnabled === true, options.toolChoice);
|
|
1040
|
+
const projectedToolChoice = toolProjection ? reconcileAnthropicToolChoice(normalizedToolChoice, toolProjection) : normalizedToolChoice;
|
|
1041
|
+
if (projectedToolChoice) params.tool_choice = projectedToolChoice;
|
|
1042
|
+
}
|
|
1043
|
+
return params;
|
|
1044
|
+
}
|
|
1045
|
+
function convertAnthropicTools(tools, isOAuthTokenLocal, supportsEagerToolInputStreaming = false) {
|
|
1046
|
+
const projection = projectAnthropicTools(tools, (name) => isOAuthTokenLocal ? toClaudeCodeToolName(name) : name);
|
|
1047
|
+
const convertedTools = [];
|
|
1048
|
+
for (const tool of projection.tools) {
|
|
1049
|
+
const convertedTool = {
|
|
1050
|
+
name: tool.wireName,
|
|
1051
|
+
description: tool.description,
|
|
1052
|
+
input_schema: tool.inputSchema
|
|
1053
|
+
};
|
|
1054
|
+
if (supportsEagerToolInputStreaming) convertedTool.eager_input_streaming = true;
|
|
1055
|
+
convertedTools.push(convertedTool);
|
|
1056
|
+
}
|
|
1057
|
+
return {
|
|
1058
|
+
projection,
|
|
1059
|
+
tools: convertedTools
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
//#endregion
|
|
1063
|
+
//#region packages/ai/src/providers/anthropic-refusal.ts
|
|
1064
|
+
function readAnthropicRefusalDetails(value) {
|
|
1065
|
+
if (!value || typeof value !== "object") return {
|
|
1066
|
+
category: null,
|
|
1067
|
+
explanation: null
|
|
1068
|
+
};
|
|
1069
|
+
const details = value;
|
|
1070
|
+
return {
|
|
1071
|
+
category: normalizeNullableString(details.category),
|
|
1072
|
+
explanation: normalizeNullableString(details.explanation)
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
function formatAnthropicRefusalMessage(details) {
|
|
1076
|
+
return `Anthropic refusal${details.category ? ` (category: ${details.category})` : ""}${details.explanation ? `: ${details.explanation}` : "."}`;
|
|
1077
|
+
}
|
|
1078
|
+
function applyAnthropicRefusal(output, stopDetails, provider) {
|
|
1079
|
+
const details = readAnthropicRefusalDetails(stopDetails);
|
|
1080
|
+
output.stopReason = "error";
|
|
1081
|
+
output.errorMessage = formatAnthropicRefusalMessage(details);
|
|
1082
|
+
output.diagnostics = [...output.diagnostics ?? [], {
|
|
1083
|
+
type: "provider_refusal",
|
|
1084
|
+
timestamp: Date.now(),
|
|
1085
|
+
details: {
|
|
1086
|
+
provider,
|
|
1087
|
+
category: details.category,
|
|
1088
|
+
explanation: details.explanation
|
|
1089
|
+
}
|
|
1090
|
+
}];
|
|
1091
|
+
}
|
|
1092
|
+
//#endregion
|
|
683
1093
|
//#region packages/ai/src/providers/anthropic-usage.ts
|
|
684
1094
|
function readAnthropicUsageTokenCount(value) {
|
|
685
1095
|
return asNonNegativeFiniteNumber(value);
|
|
@@ -812,116 +1222,453 @@ function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage)
|
|
|
812
1222
|
} else target.contextUsage = { state: "unavailable" };
|
|
813
1223
|
}
|
|
814
1224
|
//#endregion
|
|
815
|
-
//#region packages/ai/src/transports/anthropic-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
const
|
|
819
|
-
|
|
820
|
-
const
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
1225
|
+
//#region packages/ai/src/transports/anthropic-stream-reducer.ts
|
|
1226
|
+
/** One Messages protocol reducer; entry points retain their established preview/replay contracts. */
|
|
1227
|
+
async function consumeAnthropicStream(params) {
|
|
1228
|
+
const { model, options, output, stream, refusalBuffer, isOAuthToken, toolProjection } = params;
|
|
1229
|
+
const managed = params.profile === "transport";
|
|
1230
|
+
const eventSink = refusalBuffer ?? stream;
|
|
1231
|
+
let costModel = model;
|
|
1232
|
+
let messageStartPromptUsage;
|
|
1233
|
+
let inputTransformations;
|
|
1234
|
+
const anthropicStream = params.events;
|
|
1235
|
+
try {
|
|
1236
|
+
const blocks = output.content;
|
|
1237
|
+
const blockIndexes = /* @__PURE__ */ new Map();
|
|
1238
|
+
const toolArgumentPreviewSchedules = /* @__PURE__ */ new WeakMap();
|
|
1239
|
+
const seededToolArguments = /* @__PURE__ */ new WeakMap();
|
|
1240
|
+
const sealedToolCalls = [];
|
|
1241
|
+
const compactionCapture = createCompactionCapture(output, model, options);
|
|
1242
|
+
const pendingThinkingSignatures = /* @__PURE__ */ new Map();
|
|
1243
|
+
const allowReasoningContentReplay = managed && resolveProviderEndpoint(model).endpointClass === "xiaomi-native";
|
|
1244
|
+
const reasoningContentThinkingBlocks = /* @__PURE__ */ new Map();
|
|
1245
|
+
const reasoningContentTextBlocks = /* @__PURE__ */ new Map();
|
|
1246
|
+
let sawMessageStop = false;
|
|
1247
|
+
const pendingTextEnds = [];
|
|
1248
|
+
const flushPendingTextEnds = () => {
|
|
1249
|
+
for (const event of pendingTextEnds) eventSink.push(event);
|
|
1250
|
+
pendingTextEnds.length = 0;
|
|
1251
|
+
};
|
|
1252
|
+
const emitTextEnd = (event) => {
|
|
1253
|
+
if (managed) pendingTextEnds.push(event);
|
|
1254
|
+
else eventSink.push(event);
|
|
1255
|
+
};
|
|
1256
|
+
const eventIndexKey = (eventIndex) => typeof eventIndex === "number" ? eventIndex : -1;
|
|
1257
|
+
const appendReasoningContentThinkingDelta = (eventIndex, rawText) => {
|
|
1258
|
+
if (typeof rawText !== "string") return false;
|
|
1259
|
+
const text = sanitizeTransportPayloadText(rawText);
|
|
1260
|
+
if (text.length === 0) return false;
|
|
1261
|
+
const key = eventIndexKey(eventIndex);
|
|
1262
|
+
let contentIndex = reasoningContentThinkingBlocks.get(key);
|
|
1263
|
+
let block = contentIndex === void 0 ? void 0 : blocks[contentIndex];
|
|
1264
|
+
if (!block || block.type !== "thinking") {
|
|
1265
|
+
block = {
|
|
1266
|
+
type: "thinking",
|
|
1267
|
+
thinking: "",
|
|
1268
|
+
thinkingSignature: "reasoning_content"
|
|
1269
|
+
};
|
|
1270
|
+
output.content.push(block);
|
|
1271
|
+
contentIndex = output.content.length - 1;
|
|
1272
|
+
reasoningContentThinkingBlocks.set(key, contentIndex);
|
|
1273
|
+
eventSink.push({
|
|
1274
|
+
type: "thinking_start",
|
|
1275
|
+
contentIndex,
|
|
1276
|
+
partial: output
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
if (contentIndex === void 0) return false;
|
|
1280
|
+
appendAssistantThinking(block, text);
|
|
1281
|
+
block.thinkingSignature = "reasoning_content";
|
|
1282
|
+
eventSink.push({
|
|
1283
|
+
type: "thinking_delta",
|
|
1284
|
+
contentIndex,
|
|
1285
|
+
delta: text,
|
|
1286
|
+
partial: output
|
|
828
1287
|
});
|
|
829
1288
|
return true;
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
1289
|
+
};
|
|
1290
|
+
const appendReasoningContentTextDelta = (eventIndex, rawText) => {
|
|
1291
|
+
if (typeof rawText !== "string") return false;
|
|
1292
|
+
const text = sanitizeTransportPayloadText(rawText);
|
|
1293
|
+
if (text.length === 0) return false;
|
|
1294
|
+
const key = eventIndexKey(eventIndex);
|
|
1295
|
+
let contentIndex = reasoningContentTextBlocks.get(key);
|
|
1296
|
+
let block = contentIndex === void 0 ? void 0 : blocks[contentIndex];
|
|
1297
|
+
if (!block || block.type !== "text") {
|
|
1298
|
+
block = {
|
|
1299
|
+
type: "text",
|
|
1300
|
+
text: ""
|
|
1301
|
+
};
|
|
1302
|
+
output.content.push(block);
|
|
1303
|
+
contentIndex = output.content.length - 1;
|
|
1304
|
+
reasoningContentTextBlocks.set(key, contentIndex);
|
|
1305
|
+
eventSink.push({
|
|
1306
|
+
type: "text_start",
|
|
1307
|
+
contentIndex,
|
|
1308
|
+
partial: output
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1311
|
+
if (contentIndex === void 0) return false;
|
|
1312
|
+
block.text += text;
|
|
1313
|
+
eventSink.push({
|
|
1314
|
+
type: "text_delta",
|
|
1315
|
+
contentIndex,
|
|
1316
|
+
delta: text,
|
|
1317
|
+
partial: output
|
|
1318
|
+
});
|
|
842
1319
|
return true;
|
|
1320
|
+
};
|
|
1321
|
+
const finishReasoningContentSidecars = (eventIndex) => {
|
|
1322
|
+
const key = eventIndexKey(eventIndex);
|
|
1323
|
+
const thinkingContentIndex = reasoningContentThinkingBlocks.get(key);
|
|
1324
|
+
if (thinkingContentIndex !== void 0) {
|
|
1325
|
+
reasoningContentThinkingBlocks.delete(key);
|
|
1326
|
+
const block = output.content[thinkingContentIndex];
|
|
1327
|
+
if (block?.type === "thinking") eventSink.push({
|
|
1328
|
+
type: "thinking_end",
|
|
1329
|
+
contentIndex: thinkingContentIndex,
|
|
1330
|
+
content: block.thinking,
|
|
1331
|
+
partial: output
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
const textContentIndex = reasoningContentTextBlocks.get(key);
|
|
1335
|
+
if (textContentIndex === void 0) return;
|
|
1336
|
+
reasoningContentTextBlocks.delete(key);
|
|
1337
|
+
const block = output.content[textContentIndex];
|
|
1338
|
+
if (block?.type === "text") eventSink.push({
|
|
1339
|
+
type: "text_end",
|
|
1340
|
+
contentIndex: textContentIndex,
|
|
1341
|
+
content: block.text,
|
|
1342
|
+
partial: output
|
|
1343
|
+
});
|
|
1344
|
+
};
|
|
1345
|
+
for await (const rawEvent of anthropicStream) {
|
|
1346
|
+
const event = asRecord(rawEvent);
|
|
1347
|
+
inputTransformations = readAnthropicInputTransformations(event) ?? inputTransformations;
|
|
1348
|
+
if (managed) notifyLlmRequestActivity(options.signal);
|
|
1349
|
+
if (event.type === "error") {
|
|
1350
|
+
const error = asOptionalObjectRecord(event.error);
|
|
1351
|
+
throw new Error(readStringField(error, "message") || "Anthropic Messages stream failed");
|
|
1352
|
+
}
|
|
1353
|
+
if (event.type === "message_start") {
|
|
1354
|
+
const message = asOptionalObjectRecord(event.message);
|
|
1355
|
+
const usage = asRecord(message?.usage);
|
|
1356
|
+
output.responseId = typeof message?.id === "string" ? message.id : void 0;
|
|
1357
|
+
output.responseModel = typeof message?.model === "string" ? message.model : void 0;
|
|
1358
|
+
messageStartPromptUsage = applyAnthropicMessageStartUsage(output.usage, usage);
|
|
1359
|
+
calculateCost(costModel, output.usage);
|
|
1360
|
+
eventSink.push({
|
|
1361
|
+
type: "start",
|
|
1362
|
+
partial: output
|
|
1363
|
+
});
|
|
1364
|
+
continue;
|
|
1365
|
+
}
|
|
1366
|
+
if (event.type === "message_stop") {
|
|
1367
|
+
sawMessageStop = true;
|
|
1368
|
+
continue;
|
|
1369
|
+
}
|
|
1370
|
+
if (event.type === "content_block_start") {
|
|
1371
|
+
const contentBlock = asOptionalObjectRecord(event.content_block);
|
|
1372
|
+
const index = typeof event.index === "number" ? event.index : -1;
|
|
1373
|
+
if (options.anthropicServerCompaction === true && compactionCapture.begin(index, contentBlock, output.content.length)) continue;
|
|
1374
|
+
const fallbackBoundary = refusalBuffer ? readAnthropicFallbackBoundary(contentBlock) : null;
|
|
1375
|
+
if (fallbackBoundary) {
|
|
1376
|
+
refusalBuffer?.discard();
|
|
1377
|
+
sealedToolCalls.length = 0;
|
|
1378
|
+
pendingTextEnds.length = 0;
|
|
1379
|
+
blockIndexes.clear();
|
|
1380
|
+
pendingThinkingSignatures.clear();
|
|
1381
|
+
applyAnthropicFallbackBoundary({
|
|
1382
|
+
output,
|
|
1383
|
+
boundary: fallbackBoundary,
|
|
1384
|
+
provider: model.provider
|
|
1385
|
+
});
|
|
1386
|
+
costModel = {
|
|
1387
|
+
...model,
|
|
1388
|
+
cost: resolveAnthropicFallbackServingModelCost({
|
|
1389
|
+
requestedModelId: model.id,
|
|
1390
|
+
servingModelId: fallbackBoundary.toModel,
|
|
1391
|
+
requestedCost: model.cost
|
|
1392
|
+
})
|
|
1393
|
+
};
|
|
1394
|
+
calculateCost(costModel, output.usage);
|
|
1395
|
+
eventSink.push({
|
|
1396
|
+
type: "start",
|
|
1397
|
+
partial: output
|
|
1398
|
+
});
|
|
1399
|
+
for (const [i, block] of blocks.entries()) {
|
|
1400
|
+
if (block.type !== "text") continue;
|
|
1401
|
+
delete block.index;
|
|
1402
|
+
eventSink.push({
|
|
1403
|
+
type: "text_start",
|
|
1404
|
+
contentIndex: i,
|
|
1405
|
+
partial: output
|
|
1406
|
+
});
|
|
1407
|
+
if (block.text) eventSink.push({
|
|
1408
|
+
type: "text_delta",
|
|
1409
|
+
contentIndex: i,
|
|
1410
|
+
delta: block.text,
|
|
1411
|
+
partial: output
|
|
1412
|
+
});
|
|
1413
|
+
emitTextEnd({
|
|
1414
|
+
type: "text_end",
|
|
1415
|
+
contentIndex: i,
|
|
1416
|
+
content: block.text,
|
|
1417
|
+
partial: output
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
continue;
|
|
1421
|
+
}
|
|
1422
|
+
pendingThinkingSignatures.delete(index);
|
|
1423
|
+
if (contentBlock?.type === "text") {
|
|
1424
|
+
const text = typeof contentBlock.text === "string" ? sanitizeTransportPayloadText(contentBlock.text) : "";
|
|
1425
|
+
const block = {
|
|
1426
|
+
type: "text",
|
|
1427
|
+
text,
|
|
1428
|
+
index
|
|
1429
|
+
};
|
|
1430
|
+
output.content.push(block);
|
|
1431
|
+
const contentIndex = output.content.length - 1;
|
|
1432
|
+
blockIndexes.set(index, contentIndex);
|
|
1433
|
+
eventSink.push({
|
|
1434
|
+
type: "text_start",
|
|
1435
|
+
contentIndex,
|
|
1436
|
+
partial: output
|
|
1437
|
+
});
|
|
1438
|
+
if (text.length > 0) eventSink.push({
|
|
1439
|
+
type: "text_delta",
|
|
1440
|
+
contentIndex,
|
|
1441
|
+
delta: text,
|
|
1442
|
+
partial: output
|
|
1443
|
+
});
|
|
1444
|
+
continue;
|
|
1445
|
+
}
|
|
1446
|
+
if (contentBlock?.type === "thinking") {
|
|
1447
|
+
const thinking = typeof contentBlock.thinking === "string" ? contentBlock.thinking : "";
|
|
1448
|
+
const block = {
|
|
1449
|
+
type: "thinking",
|
|
1450
|
+
thinking,
|
|
1451
|
+
thinkingSignature: typeof contentBlock.signature === "string" ? contentBlock.signature : "",
|
|
1452
|
+
index
|
|
1453
|
+
};
|
|
1454
|
+
output.content.push(block);
|
|
1455
|
+
const contentIndex = output.content.length - 1;
|
|
1456
|
+
blockIndexes.set(index, contentIndex);
|
|
1457
|
+
eventSink.push({
|
|
1458
|
+
type: "thinking_start",
|
|
1459
|
+
contentIndex,
|
|
1460
|
+
partial: output
|
|
1461
|
+
});
|
|
1462
|
+
if (thinking.length > 0) eventSink.push({
|
|
1463
|
+
type: "thinking_delta",
|
|
1464
|
+
contentIndex,
|
|
1465
|
+
delta: thinking,
|
|
1466
|
+
partial: output
|
|
1467
|
+
});
|
|
1468
|
+
continue;
|
|
1469
|
+
}
|
|
1470
|
+
if (contentBlock?.type === "redacted_thinking") {
|
|
1471
|
+
const block = {
|
|
1472
|
+
type: "thinking",
|
|
1473
|
+
thinking: "[Reasoning redacted]",
|
|
1474
|
+
thinkingSignature: typeof contentBlock.data === "string" ? contentBlock.data : "",
|
|
1475
|
+
redacted: true,
|
|
1476
|
+
index
|
|
1477
|
+
};
|
|
1478
|
+
output.content.push(block);
|
|
1479
|
+
blockIndexes.set(index, output.content.length - 1);
|
|
1480
|
+
eventSink.push({
|
|
1481
|
+
type: "thinking_start",
|
|
1482
|
+
contentIndex: output.content.length - 1,
|
|
1483
|
+
partial: output
|
|
1484
|
+
});
|
|
1485
|
+
continue;
|
|
1486
|
+
}
|
|
1487
|
+
if (contentBlock?.type === "tool_use") {
|
|
1488
|
+
if (managed) tagPendingCommentaryText(output.content);
|
|
1489
|
+
flushPendingTextEnds();
|
|
1490
|
+
const block = {
|
|
1491
|
+
type: "toolCall",
|
|
1492
|
+
id: typeof contentBlock.id === "string" ? contentBlock.id : "",
|
|
1493
|
+
name: typeof contentBlock.name === "string" ? isOAuthToken ? resolveOriginalAnthropicToolName(contentBlock.name, toolProjection) : contentBlock.name : "",
|
|
1494
|
+
arguments: asRecord(contentBlock.input),
|
|
1495
|
+
partialJson: "",
|
|
1496
|
+
index
|
|
1497
|
+
};
|
|
1498
|
+
output.content.push(block);
|
|
1499
|
+
blockIndexes.set(index, output.content.length - 1);
|
|
1500
|
+
seededToolArguments.set(block, managed ? block.arguments : contentBlock.input ?? {});
|
|
1501
|
+
toolArgumentPreviewSchedules.set(block, createToolArgumentPreviewSchedule());
|
|
1502
|
+
eventSink.push({
|
|
1503
|
+
type: "toolcall_start",
|
|
1504
|
+
contentIndex: output.content.length - 1,
|
|
1505
|
+
partial: output
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
continue;
|
|
1509
|
+
}
|
|
1510
|
+
if (event.type === "content_block_delta") {
|
|
1511
|
+
const delta = asOptionalObjectRecord(event.delta);
|
|
1512
|
+
const eventIndex = typeof event.index === "number" ? event.index : void 0;
|
|
1513
|
+
if (eventIndex !== void 0 && compactionCapture.delta(eventIndex, delta)) continue;
|
|
1514
|
+
let index = eventIndex === void 0 ? void 0 : blockIndexes.get(eventIndex);
|
|
1515
|
+
let block = index === void 0 ? void 0 : blocks[index];
|
|
1516
|
+
if (allowReasoningContentReplay) {
|
|
1517
|
+
const appendedThinking = appendReasoningContentThinkingDelta(event.index, delta?.reasoning_content);
|
|
1518
|
+
const hasNativeAnthropicDelta = delta?.type === "text_delta" && typeof delta.text === "string" || delta?.type === "thinking_delta" && typeof delta.thinking === "string" || delta?.type === "input_json_delta" && typeof delta.partial_json === "string" || delta?.type === "signature_delta" && typeof delta.signature === "string";
|
|
1519
|
+
let appendedContent = false;
|
|
1520
|
+
if (!hasNativeAnthropicDelta && typeof delta?.content === "string" && delta.content.length > 0) {
|
|
1521
|
+
const text = sanitizeTransportPayloadText(delta.content);
|
|
1522
|
+
if (text.length > 0) {
|
|
1523
|
+
if (block?.type === "text" && index !== void 0) {
|
|
1524
|
+
block.text += text;
|
|
1525
|
+
eventSink.push({
|
|
1526
|
+
type: "text_delta",
|
|
1527
|
+
contentIndex: index,
|
|
1528
|
+
delta: text,
|
|
1529
|
+
partial: output
|
|
1530
|
+
});
|
|
1531
|
+
appendedContent = true;
|
|
1532
|
+
} else appendedContent = appendReasoningContentTextDelta(event.index, text);
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
if ((appendedThinking || appendedContent) && !hasNativeAnthropicDelta) continue;
|
|
1536
|
+
}
|
|
1537
|
+
if (managed && !block && delta?.type === "text_delta" && typeof delta.text === "string") {
|
|
1538
|
+
block = {
|
|
1539
|
+
type: "text",
|
|
1540
|
+
text: "",
|
|
1541
|
+
index: typeof event.index === "number" ? event.index : blocks.length
|
|
1542
|
+
};
|
|
1543
|
+
output.content.push(block);
|
|
1544
|
+
index = output.content.length - 1;
|
|
1545
|
+
if (typeof event.index === "number") blockIndexes.set(event.index, index);
|
|
1546
|
+
eventSink.push({
|
|
1547
|
+
type: "text_start",
|
|
1548
|
+
contentIndex: index,
|
|
1549
|
+
partial: output
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1552
|
+
if (index === void 0) continue;
|
|
1553
|
+
if (block?.type === "text" && delta?.type === "text_delta" && typeof delta.text === "string") {
|
|
1554
|
+
block.text += delta.text;
|
|
1555
|
+
eventSink.push({
|
|
1556
|
+
type: "text_delta",
|
|
1557
|
+
contentIndex: index,
|
|
1558
|
+
delta: delta.text,
|
|
1559
|
+
partial: output
|
|
1560
|
+
});
|
|
1561
|
+
continue;
|
|
1562
|
+
}
|
|
1563
|
+
if (block?.type === "thinking" && delta?.type === "thinking_delta" && typeof delta.thinking === "string") {
|
|
1564
|
+
appendAssistantThinking(block, delta.thinking);
|
|
1565
|
+
eventSink.push({
|
|
1566
|
+
type: "thinking_delta",
|
|
1567
|
+
contentIndex: index,
|
|
1568
|
+
delta: delta.thinking,
|
|
1569
|
+
partial: output
|
|
1570
|
+
});
|
|
1571
|
+
continue;
|
|
1572
|
+
}
|
|
1573
|
+
if (block?.type === "toolCall" && delta?.type === "input_json_delta" && typeof delta.partial_json === "string") {
|
|
1574
|
+
const partialJson = `${block.partialJson ?? ""}${delta.partial_json}`;
|
|
1575
|
+
block.partialJson = partialJson;
|
|
1576
|
+
if (toolArgumentPreviewSchedules.get(block)?.(partialJson.length)) block.arguments = managed ? coerceTransportToolCallArguments(parseJsonObjectPreservingUnsafeIntegers(partialJson) ?? parseStreamingJson(partialJson)) : parseStreamingJson(partialJson);
|
|
1577
|
+
eventSink.push({
|
|
1578
|
+
type: "toolcall_delta",
|
|
1579
|
+
contentIndex: index,
|
|
1580
|
+
delta: delta.partial_json,
|
|
1581
|
+
partial: output
|
|
1582
|
+
});
|
|
1583
|
+
continue;
|
|
1584
|
+
}
|
|
1585
|
+
if (block?.type === "thinking" && delta?.type === "signature_delta" && typeof delta.signature === "string") {
|
|
1586
|
+
const signatureIndex = eventIndexKey(event.index);
|
|
1587
|
+
const pendingSignature = pendingThinkingSignatures.get(signatureIndex);
|
|
1588
|
+
if (pendingSignature === void 0) {
|
|
1589
|
+
block.thinkingSignature = "";
|
|
1590
|
+
pendingThinkingSignatures.set(signatureIndex, delta.signature);
|
|
1591
|
+
} else pendingThinkingSignatures.set(signatureIndex, pendingSignature + delta.signature);
|
|
1592
|
+
}
|
|
1593
|
+
continue;
|
|
1594
|
+
}
|
|
1595
|
+
if (event.type === "content_block_stop") {
|
|
1596
|
+
const eventIndex = typeof event.index === "number" ? event.index : void 0;
|
|
1597
|
+
if (eventIndex !== void 0 && compactionCapture.complete(eventIndex)) continue;
|
|
1598
|
+
const pendingSignature = eventIndex === void 0 ? void 0 : pendingThinkingSignatures.get(eventIndex);
|
|
1599
|
+
if (eventIndex !== void 0) pendingThinkingSignatures.delete(eventIndex);
|
|
1600
|
+
const index = eventIndex === void 0 ? void 0 : blockIndexes.get(eventIndex);
|
|
1601
|
+
const block = index === void 0 ? void 0 : blocks[index];
|
|
1602
|
+
if (eventIndex === void 0 || index === void 0 || !block) {
|
|
1603
|
+
finishReasoningContentSidecars(event.index);
|
|
1604
|
+
continue;
|
|
1605
|
+
}
|
|
1606
|
+
blockIndexes.delete(eventIndex);
|
|
1607
|
+
delete block.index;
|
|
1608
|
+
if (block.type === "text") {
|
|
1609
|
+
emitTextEnd({
|
|
1610
|
+
type: "text_end",
|
|
1611
|
+
contentIndex: index,
|
|
1612
|
+
content: block.text,
|
|
1613
|
+
partial: output
|
|
1614
|
+
});
|
|
1615
|
+
finishReasoningContentSidecars(event.index);
|
|
1616
|
+
continue;
|
|
1617
|
+
}
|
|
1618
|
+
if (block.type === "thinking") {
|
|
1619
|
+
if (pendingSignature !== void 0) block.thinkingSignature = pendingSignature;
|
|
1620
|
+
eventSink.push({
|
|
1621
|
+
type: "thinking_end",
|
|
1622
|
+
contentIndex: index,
|
|
1623
|
+
content: block.thinking,
|
|
1624
|
+
partial: output
|
|
1625
|
+
});
|
|
1626
|
+
finishReasoningContentSidecars(event.index);
|
|
1627
|
+
continue;
|
|
1628
|
+
}
|
|
1629
|
+
if (block.type === "toolCall") {
|
|
1630
|
+
sealedToolCalls.push({
|
|
1631
|
+
block,
|
|
1632
|
+
contentIndex: index
|
|
1633
|
+
});
|
|
1634
|
+
finishReasoningContentSidecars(event.index);
|
|
1635
|
+
}
|
|
1636
|
+
continue;
|
|
1637
|
+
}
|
|
1638
|
+
if (event.type === "message_delta") {
|
|
1639
|
+
logAnthropicContextEdits(event);
|
|
1640
|
+
const delta = asOptionalObjectRecord(event.delta);
|
|
1641
|
+
const usage = asOptionalObjectRecord(event.usage);
|
|
1642
|
+
if (typeof delta?.stop_reason === "string" && delta.stop_reason) {
|
|
1643
|
+
if (delta.stop_reason === "refusal") applyAnthropicRefusal(output, delta.stop_details, model.provider);
|
|
1644
|
+
else output.stopReason = mapAnthropicStopReason(delta.stop_reason);
|
|
1645
|
+
}
|
|
1646
|
+
applyAnthropicMessageDeltaUsage(output.usage, usage, messageStartPromptUsage);
|
|
1647
|
+
calculateCost(costModel, output.usage);
|
|
1648
|
+
if (managed && (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall"))) tagPendingCommentaryText(output.content);
|
|
1649
|
+
flushPendingTextEnds();
|
|
1650
|
+
}
|
|
843
1651
|
}
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
output.providerReplay = {
|
|
858
|
-
v: 1,
|
|
859
|
-
type: ANTHROPIC_COMPACTION_REPLAY_TYPE,
|
|
860
|
-
data: summary,
|
|
861
|
-
replayIndex,
|
|
862
|
-
...context,
|
|
863
|
-
baseUrlHash: context.baseUrlHash
|
|
864
|
-
};
|
|
865
|
-
}
|
|
866
|
-
function suppressAnthropicCompaction(output, model, options) {
|
|
867
|
-
const context = buildProviderReplayContext(model, options);
|
|
868
|
-
if (!context.baseUrlHash) return;
|
|
869
|
-
output.providerReplay = {
|
|
870
|
-
v: 1,
|
|
871
|
-
type: ANTHROPIC_COMPACTION_SUPPRESSION_TYPE,
|
|
872
|
-
data: ANTHROPIC_COMPACTION_SUPPRESSION_DATA,
|
|
873
|
-
...context,
|
|
874
|
-
baseUrlHash: context.baseUrlHash
|
|
875
|
-
};
|
|
876
|
-
}
|
|
877
|
-
function resolveNewestAnthropicCompaction(messages, model, options) {
|
|
878
|
-
const context = buildProviderReplayContext(model, options);
|
|
879
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
880
|
-
const message = messages[index];
|
|
881
|
-
if (message?.role !== "assistant") continue;
|
|
882
|
-
const replayState = readAnthropicCompactionState(message.providerReplay);
|
|
883
|
-
if (replayState?.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) {
|
|
884
|
-
if (providerReplayContextMatches(replayState, context)) return;
|
|
885
|
-
continue;
|
|
1652
|
+
if ((isDirectAnthropicModel(model) || !managed && refusalBuffer) && !sawMessageStop) throw new Error("Anthropic stream ended before message_stop");
|
|
1653
|
+
if (options.signal?.aborted) throw transportAbortError(options.signal);
|
|
1654
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage ?? "An unknown error occurred");
|
|
1655
|
+
if ([...blockIndexes.values()].some((index) => blocks[index]?.type === "toolCall")) throw new Error("Provider completed stream with an incomplete tool call");
|
|
1656
|
+
finalizeTerminalToolCallArguments(sealedToolCalls.map(({ block }) => block), (block) => block.partialJson && block.partialJson.length > 0 ? block.partialJson : seededToolArguments.get(block), void 0, { repairStringLiterals: true });
|
|
1657
|
+
for (const sealed of sealedToolCalls) {
|
|
1658
|
+
delete sealed.block.partialJson;
|
|
1659
|
+
eventSink.push({
|
|
1660
|
+
type: "toolcall_end",
|
|
1661
|
+
contentIndex: sealed.contentIndex,
|
|
1662
|
+
toolCall: sealed.block,
|
|
1663
|
+
partial: output
|
|
1664
|
+
});
|
|
886
1665
|
}
|
|
887
|
-
|
|
888
|
-
if (
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
replayIndex: replayState.replayIndex ?? 0,
|
|
893
|
-
summary: replayState.data
|
|
894
|
-
};
|
|
1666
|
+
refusalBuffer?.flush();
|
|
1667
|
+
if (managed && (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall"))) tagPendingCommentaryText(output.content);
|
|
1668
|
+
flushPendingTextEnds();
|
|
1669
|
+
} finally {
|
|
1670
|
+
logAnthropicThinkingDrops(inputTransformations);
|
|
895
1671
|
}
|
|
896
1672
|
}
|
|
897
|
-
function buildAnthropicReplayPlan(messages, model, options) {
|
|
898
|
-
if (options?.enabled !== true) return { messages };
|
|
899
|
-
const checkpoint = resolveNewestAnthropicCompaction(messages, model, options);
|
|
900
|
-
if (!checkpoint) return { messages };
|
|
901
|
-
const ownerIndex = messages.indexOf(checkpoint.owner);
|
|
902
|
-
return {
|
|
903
|
-
messages: [{
|
|
904
|
-
...checkpoint.owner,
|
|
905
|
-
content: checkpoint.owner.content.slice(checkpoint.replayIndex)
|
|
906
|
-
}, ...messages.slice(ownerIndex + 1)],
|
|
907
|
-
compaction: {
|
|
908
|
-
type: "compaction",
|
|
909
|
-
content: checkpoint.summary
|
|
910
|
-
}
|
|
911
|
-
};
|
|
912
|
-
}
|
|
913
|
-
/** Match Anthropic's 400 response when a replay block is no longer accepted. */
|
|
914
|
-
function isAnthropicReplayRejection(error) {
|
|
915
|
-
if (!error || typeof error !== "object") return false;
|
|
916
|
-
const record = error;
|
|
917
|
-
const nested = record.error && typeof record.error === "object" && !Array.isArray(record.error) ? record.error : void 0;
|
|
918
|
-
const text = [
|
|
919
|
-
record.message,
|
|
920
|
-
record.code,
|
|
921
|
-
nested?.message,
|
|
922
|
-
nested?.code
|
|
923
|
-
].filter((value) => typeof value === "string").join(" ").toLowerCase();
|
|
924
|
-
return (record.status === 400 || text.startsWith("http 400:")) && (text.includes("compaction") || text.includes("context_management"));
|
|
925
|
-
}
|
|
926
1673
|
//#endregion
|
|
927
|
-
export {
|
|
1674
|
+
export { applyAnthropicFallbackBoundary as A, logAnthropicContextEdits as B, applyAnthropicThinkingBindingControls as C, ANTHROPIC_SERVER_SIDE_FALLBACKS as D, readAnthropicInputTransformations as E, applyAnthropicPayloadPolicyToParams as F, resolveAnthropicServerCompactionPlan as G, resolveAnthropicContextManagementBetaHeader as H, applyAnthropicRequestCacheControl as I, usesFoundryBearerAuth as J, isAnthropicOAuthApiKey as K, buildAnthropicSystemBlocks as L, resolveAnthropicFallbackServingModelCost as M, applyAnthropicContextManagementToRequest as N, ANTHROPIC_SERVER_SIDE_FALLBACK_BETA as O, applyAnthropicEphemeralCacheControlMarkers as P, isAnthropicServerToolClearingEnabled as R, ANTHROPIC_OMITTED_REASONING_TEXT as S, logAnthropicThinkingDrops as T, resolveAnthropicEphemeralCacheControl as U, resolveAnthropicCacheOptions as V, resolveAnthropicPayloadPolicy as W, normalizeAnthropicToolChoice as _, readAnthropicPromptUsageSnapshot as a, resolveOriginalAnthropicToolName as b, applyAnthropicRefusal as c, convertAnthropicTools as d, buildAnthropicReplayPlan as f, normalizeAnthropicToolCallId as g, suppressAnthropicCompaction as h, readAnthropicCacheWriteUsage as i, readAnthropicFallbackBoundary as j, CLAUDE_OPUS_FALLBACK_MODEL_COST as k, buildAnthropicGenerationParams as l, resolveNewestAnthropicCompaction as m, applyAnthropicMessageDeltaUsage as n, readAnthropicUsageTokenCount as o, isAnthropicReplayRejection as p, omitFoundryBearerCredentialHeaders as q, applyAnthropicMessageStartUsage as r, readLastAnthropicIterationUsage as s, consumeAnthropicStream as t, convertAnthropicMessages as u, projectAnthropicTools as v, findActiveAnthropicToolTurnAssistantIndex as w, toClaudeCodeToolName as x, reconcileAnthropicToolChoice as y, isDirectAnthropicModel as z };
|