@openclaw/ai 2026.10.2 → 2026.10.5-beta.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/README.md +6 -0
- package/dist/{anthropic-C4qygbtf.mjs → anthropic-BalhAzs-.mjs} +33 -63
- package/dist/{anthropic-payload-policy-DD7vzpxE.d.mts → anthropic-payload-policy-DGblkHL5.d.mts} +19 -19
- package/dist/{anthropic-stream-reducer-CWyWE21W.mjs → anthropic-stream-reducer-DkKeXXx4.mjs} +215 -297
- package/dist/{api-registry-B3rC2qu3.d.mts → api-registry-BhXXYdRA.d.mts} +2 -2
- package/dist/assistant-message-events-CHMJgbhs.mjs +18 -0
- package/dist/{assistant-output-D5762GPH.mjs → assistant-output-BuhRpLeD.mjs} +2 -2
- package/dist/{assistant-text-phase-DVZOxbuR.mjs → assistant-text-phase-Bn1hbvK9.mjs} +7 -6
- package/dist/{azure-openai-responses-DO-Fv8ha.mjs → azure-openai-responses-DfT9Ndd8.mjs} +8 -9
- package/dist/{credential-redaction-BKv49aiv.d.mts → credential-redaction-BHxruEH6.d.mts} +3 -1
- package/dist/{diagnostics-Caj-4RBA.mjs → diagnostics-tQasYct1.mjs} +10 -3
- package/dist/diagnostics.d.mts +7 -7
- package/dist/diagnostics.mjs +3 -3
- package/dist/{env-api-keys-CyX4VKGO.mjs → env-api-keys-CrxwdylR.mjs} +24 -35
- package/dist/event-stream-6UNs-AhN.d.mts +1 -0
- package/dist/{event-stream-DrRM_90c.d.mts → event-stream-BcqWLo_u.d.mts} +1 -1
- package/dist/{event-stream-C5AbCAaZ.mjs → event-stream-X2gmsUWv.mjs} +5 -8
- package/dist/event-stream.d.mts +2 -2
- package/dist/event-stream.mjs +1 -1
- package/dist/{google-interactions-DNXzPPwQ.mjs → google-interactions-9SSqsJg9.mjs} +52 -84
- package/dist/{google-messages-l7wgIF5I.mjs → google-messages-DsB7fmoQ.mjs} +26 -35
- package/dist/google-provider-stream-CcEI2-MC.mjs +58 -0
- package/dist/google-sYI2wnY6.mjs +20 -0
- package/dist/{google-shared-V8jogVPq.mjs → google-shared-zGR0QUyS.mjs} +6 -15
- package/dist/google-vertex-Dw_abY1w.mjs +40 -0
- package/dist/{host-u3o8XiAp.mjs → host-Bf3NjYNj.mjs} +3 -2
- package/dist/{host-DiPhRjrc.d.mts → host-CTDz7LXD.d.mts} +6 -3
- package/dist/{host-policy-BwhNNGQR.mjs → host-policy-BzZluO6_.mjs} +1 -1
- package/dist/{index-D27wAoOr.d.mts → index-BYDvopUB.d.mts} +8 -7
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +7 -6
- package/dist/internal/anthropic.d.mts +5 -5
- package/dist/internal/anthropic.mjs +4 -4
- package/dist/internal/openai-completions-compat.d.mts +1 -1
- package/dist/internal/openai-completions-compat.mjs +1 -1
- package/dist/internal/openai-responses-payload-policy.d.mts +2 -2
- package/dist/internal/openai-responses-payload-policy.mjs +2 -3
- package/dist/internal/openai.d.mts +9 -9
- package/dist/internal/openai.mjs +9 -11
- package/dist/internal/runtime.d.mts +31 -13
- package/dist/internal/runtime.mjs +10 -13
- package/dist/internal/shared.d.mts +5 -5
- package/dist/internal/shared.mjs +6 -7
- package/dist/internal/tool-schema.mjs +2 -2
- package/dist/{llm-request-activity-D8f_oxHj.mjs → llm-request-activity-CfMuh5cC.mjs} +2 -2
- package/dist/{mistral-cnoBDSIg.mjs → mistral-dCDCR5ur.mjs} +71 -146
- package/dist/{model-transport-url-C5f59L3Y.d.mts → model-transport-url-BrsjjdjR.d.mts} +5 -1
- package/dist/{number-coercion-V8-BhME4.mjs → number-coercion-C3hztWoY.mjs} +5 -4
- package/dist/{openai-chatgpt-responses-CHdn8YJ_.mjs → openai-chatgpt-responses-V4iP0dv5.mjs} +118 -194
- package/dist/{openai-completions-BZfHIaZ7.mjs → openai-completions-BY-kr5IS.mjs} +7 -6
- package/dist/{openai-completions-compat-GMN8A0fm.mjs → openai-completions-compat-CYX18uDk.mjs} +10 -6
- package/dist/{openai-completions-compat-BgPg1BRs.d.mts → openai-completions-compat-i-nLZxFy.d.mts} +10 -11
- package/dist/{openai-completions-transport-CuoQfnKV.mjs → openai-completions-transport-v38m71Oq.mjs} +1449 -294
- package/dist/{openai-prompt-cache-CFviG_D4.d.mts → openai-prompt-cache-DIPMrMFy.d.mts} +2 -2
- package/dist/{openai-prompt-cache-BK6E2g4Q.mjs → openai-prompt-cache-r93BJhjm.mjs} +1 -1
- package/dist/{openai-responses-BhAZFsL9.mjs → openai-responses-D5oiAUOG.mjs} +9 -10
- package/dist/{openai-responses-compaction-replay-BBNo098-.d.mts → openai-responses-compaction-replay-B2FE4Nw8.d.mts} +4 -4
- package/dist/{openai-responses-contracts-D5yVZBp-.d.mts → openai-responses-contracts-DKqMJ1Ct.d.mts} +9 -4
- package/dist/{openai-responses-contracts-BKu8O7l4.mjs → openai-responses-contracts-DlKMMTxK.mjs} +26 -6
- package/dist/{openai-responses-compaction-window-tA4QkU0o.mjs → openai-responses-endpoint-CinGh4VZ.mjs} +17 -7
- package/dist/{openai-responses-payload-policy-s1apQlZZ.d.mts → openai-responses-payload-policy-C7GsA1y0.d.mts} +2 -4
- package/dist/{openai-responses-prompt-observer-internal-CefUzh8M.mjs → openai-responses-request-lifecycle-NSIJo9jY.mjs} +39 -4
- package/dist/{openai-responses-shared-CKNmC3vC.mjs → openai-responses-shared-D-aDYAkJ.mjs} +1037 -328
- package/dist/{openai-responses-terminal-usage-CempLedn.d.mts → openai-responses-terminal-usage-BygjiAdi.d.mts} +2 -2
- package/dist/{openai-tool-schema-BIrI0mrA.mjs → openai-tool-schema-DDPXsvJP.mjs} +220 -400
- package/dist/{positive-integer-CwLi8LUL.mjs → positive-integer-CqWjqSi9.mjs} +1 -1
- package/dist/{provider-error-DQBE1bUQ.mjs → provider-error-CF9GWCs2.mjs} +5 -3
- package/dist/{provider-options-CkieKvNJ.d.mts → provider-options-CVrXZBtA.d.mts} +6 -10
- package/dist/{provider-transport-turn-state-BX6TfCyW.mjs → provider-transport-turn-state-BOpxgm-J.mjs} +2 -2
- package/dist/{provider-types-v3bmqsfg.d.mts → provider-types-CHtRx0dI.d.mts} +3 -3
- package/dist/provider-types.d.mts +6 -6
- package/dist/providers.d.mts +2 -2
- package/dist/providers.mjs +12 -12
- package/dist/{reasoning-tag-text-partitioner-Ju9VpUGl.mjs → reasoning-tag-text-partitioner-CpOy9-IQ.mjs} +374 -235
- package/dist/{required-api-key-D4kn3252.mjs → required-api-key-CFJvcFy_.mjs} +1 -1
- package/dist/{simple-options-7QuSKriJ.mjs → simple-options-BAu4B54D.mjs} +9 -11
- package/dist/{src-VWy9E7DZ.mjs → src-C44MKrW5.mjs} +20 -9
- package/dist/{stream-DFaRxi2D.mjs → stream-Dd8DwRFL.mjs} +12 -21
- package/dist/{stream-first-event-timeout-D9ZmL_s9.mjs → stream-first-event-timeout-Dc42hKv6.mjs} +8 -21
- package/dist/{streaming-byte-guard-CC-HMn_u.mjs → streaming-byte-guard-DBXr1XYn.mjs} +16 -1
- package/dist/{string-normalization-PN10hf6d.mjs → string-normalization-I3ux50s9.mjs} +1 -1
- package/dist/{text-signature-D_vRrx-z.mjs → text-signature-Drb4Erb8.mjs} +32 -5
- package/dist/{tool-schema-json-projection-Ct8SeBeJ.mjs → tool-schema-json-projection-B8sG1I9v.mjs} +29 -23
- package/dist/{transport-stream-shared-DW-7-qLR.d.mts → transport-stream-shared-BmpzDtdQ.d.mts} +4 -4
- package/dist/{transport-stream-shared-CCR7hSW0.mjs → transport-stream-shared-CWGOuLcA.mjs} +8 -16
- package/dist/transports.d.mts +34 -23
- package/dist/transports.mjs +76 -1235
- package/dist/{types-DvzmWW8J.d.mts → types-Bd2T86VJ.d.mts} +5 -23
- package/dist/types-Kdfw_04V.d.mts +1 -0
- package/dist/types.d.mts +6 -6
- package/dist/types.mjs +5 -4
- package/dist/{validation-B3lQt_VE.d.mts → validation-DdGB1yy3.d.mts} +1 -1
- package/dist/{validation-DbqlIfDn.mjs → validation-ZX_sPVV0.mjs} +3 -9
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +4 -9
- package/dist/env-api-keys-BR0MUYYD.d.mts +0 -17
- package/dist/event-stream-DqDQWqiy.d.mts +0 -1
- package/dist/google-B2g_Xn4d.mjs +0 -54
- package/dist/google-vertex-_khoip47.mjs +0 -88
- package/dist/internal/env-api-keys.d.mts +0 -2
- package/dist/internal/env-api-keys.mjs +0 -2
- package/dist/openai-responses-endpoint-TV0y50fb.mjs +0 -11
- package/dist/openai-responses-request-lifecycle-Fsag-IGZ.mjs +0 -36
- package/dist/openai-transport-params-D7JMUPjf.mjs +0 -735
- package/dist/openai-web-search-tools-Cfu0uLyN.mjs +0 -14
- package/dist/provider-replay-context-CO_Eplnc.mjs +0 -25
- package/dist/types-UlNana-2.d.mts +0 -1
package/dist/{anthropic-stream-reducer-CWyWE21W.mjs → anthropic-stream-reducer-DkKeXXx4.mjs}
RENAMED
|
@@ -1,33 +1,28 @@
|
|
|
1
|
-
import { E as supportsClaudeInHistorySystemMessages, O as supportsClaudeNativeXhighEffort, S as resolveClaudeSonnet5ModelIdentity, b as resolveClaudeOpus5ModelIdentity, d as requiresClaudeBetweenToolsThinking, g as resolveClaudeModelIdentity, h as resolveClaudeHaiku55ModelIdentity,
|
|
1
|
+
import { E as supportsClaudeInHistorySystemMessages, O as supportsClaudeNativeXhighEffort, S as resolveClaudeSonnet5ModelIdentity, b as resolveClaudeOpus5ModelIdentity, d as requiresClaudeBetweenToolsThinking, g as resolveClaudeModelIdentity, h as resolveClaudeHaiku55ModelIdentity, m as resolveClaudeFable5ModelIdentity, u as bindsClaudeThinkingPrefix, w as supportsClaudeAdaptiveThinking, x as resolveClaudeSonnet55ModelIdentity, y as resolveClaudeOpus55ModelIdentity } from "./src-C44MKrW5.mjs";
|
|
2
2
|
import { f as hasRuntimeContextMarker } from "./types-CQc6JNG4.mjs";
|
|
3
|
-
import { r as appendAssistantThinking } from "./event-stream-
|
|
3
|
+
import { r as appendAssistantThinking } from "./event-stream-X2gmsUWv.mjs";
|
|
4
4
|
import { a as asRecord, o as isRecord, r as asOptionalObjectRecord, s as readStringField } from "./record-coerce-CNDovvft.mjs";
|
|
5
5
|
import { i as normalizeOptionalLowercaseString, r as normalizeNullableString } from "./string-coerce-CtgOVOSv.mjs";
|
|
6
|
-
import { n as asNonNegativeFiniteNumber } from "./number-coercion-
|
|
7
|
-
import { N as isImageWithMediaPayload, O as calculateCost, S as resolveAnthropicThinkingEffort, g as applyClaudeRequestContract, h as ANTHROPIC_CLAUDE_CODE_VERSION, r as getAiTransportHost, u as STREAM_ERROR_FALLBACK_TEXT, v as defaultsClaudeAdaptiveThinking, w as usesClaudeFable5MessagesContract, x as requiresClaudeAdaptiveThinking, y as mapAnthropicStopReason } from "./host-
|
|
8
|
-
import "./text-signature-
|
|
6
|
+
import { n as asNonNegativeFiniteNumber } from "./number-coercion-C3hztWoY.mjs";
|
|
7
|
+
import { N as isImageWithMediaPayload, O as calculateCost, S as resolveAnthropicThinkingEffort, g as applyClaudeRequestContract, h as ANTHROPIC_CLAUDE_CODE_VERSION, r as getAiTransportHost, u as STREAM_ERROR_FALLBACK_TEXT, v as defaultsClaudeAdaptiveThinking, w as usesClaudeFable5MessagesContract, x as requiresClaudeAdaptiveThinking, y as mapAnthropicStopReason } from "./host-Bf3NjYNj.mjs";
|
|
8
|
+
import { i as providerReplayContextMatches, n as buildProviderReplayContext, r as isProviderReplayContext } from "./text-signature-Drb4Erb8.mjs";
|
|
9
9
|
import { n as estimateBase64DecodedBytes } from "./base64-C8CdcsjU.mjs";
|
|
10
|
-
import { a as resolveProviderRequestCapabilities, i as resolveProviderEndpoint } from "./host-policy-
|
|
10
|
+
import { a as resolveProviderRequestCapabilities, i as resolveProviderEndpoint } from "./host-policy-BzZluO6_.mjs";
|
|
11
11
|
import { i as resolveCacheRetention } from "./cloudflare-5qSGvIUH.mjs";
|
|
12
|
-
import { C as parseJsonObjectPreservingUnsafeIntegers, E as createToolArgumentPreviewSchedule, O as parseStreamingJson, P as sanitizeSurrogates,
|
|
13
|
-
import { C as stripSystemPromptCacheBoundary, D as sortPromptCacheToolsByName, d as describeToolResultMediaPlaceholder, m as extractToolResultText, p as extractToolResultBlockText, t as adjustMaxTokensForThinking, w as stripSystemPromptRelocatableBoundary, x as splitSystemPromptCacheBoundary } from "./simple-options-
|
|
14
|
-
import { t as parsePositiveInteger } from "./positive-integer-
|
|
15
|
-
import { n as
|
|
16
|
-
import { n as
|
|
17
|
-
import { i as
|
|
18
|
-
import {
|
|
19
|
-
import { t as notifyLlmRequestActivity } from "./llm-request-activity-D8f_oxHj.mjs";
|
|
12
|
+
import { C as parseJsonObjectPreservingUnsafeIntegers, E as createToolArgumentPreviewSchedule, O as parseStreamingJson, P as sanitizeSurrogates, b as transportAbortError, d as iterateModelStream, i as copyProviderAcceptanceObserver, l as finalizeTerminalToolCallArguments, r as coerceTransportToolCallArguments, v as sanitizeTransportPayloadText } from "./transport-stream-shared-CWGOuLcA.mjs";
|
|
13
|
+
import { C as stripSystemPromptCacheBoundary, D as sortPromptCacheToolsByName, d as describeToolResultMediaPlaceholder, m as extractToolResultText, p as extractToolResultBlockText, t as adjustMaxTokensForThinking, w as stripSystemPromptRelocatableBoundary, x as splitSystemPromptCacheBoundary } from "./simple-options-BAu4B54D.mjs";
|
|
14
|
+
import { t as parsePositiveInteger } from "./positive-integer-CqWjqSi9.mjs";
|
|
15
|
+
import { n as transformProviderMessages } from "./assistant-output-BuhRpLeD.mjs";
|
|
16
|
+
import { i as SCHEMA_MAP_KEYS, n as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-B8sG1I9v.mjs";
|
|
17
|
+
import { i as tagPendingCommentaryText } from "./assistant-text-phase-Bn1hbvK9.mjs";
|
|
18
|
+
import { t as notifyLlmRequestActivity } from "./llm-request-activity-CfMuh5cC.mjs";
|
|
20
19
|
//#region packages/ai/src/providers/anthropic-auth-headers.ts
|
|
21
20
|
/** Inspect resolved credential shape for auth routing without exposing its value. */
|
|
22
21
|
function isAnthropicOAuthApiKey(apiKey) {
|
|
23
22
|
return typeof apiKey === "string" && getAiTransportHost().resolveSecretSentinel(apiKey).includes("sk-ant-oat");
|
|
24
23
|
}
|
|
25
24
|
function usesFoundryBearerAuth(model) {
|
|
26
|
-
return model.provider === "microsoft-foundry" && (model.authHeader === true ||
|
|
27
|
-
}
|
|
28
|
-
function hasBearerAuthorizationHeader(headers) {
|
|
29
|
-
if (!headers) return false;
|
|
30
|
-
return Object.entries(headers).some(([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim()));
|
|
25
|
+
return model.provider === "microsoft-foundry" && (model.authHeader === true || Object.entries(model.headers ?? {}).some(([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim())));
|
|
31
26
|
}
|
|
32
27
|
function omitFoundryBearerCredentialHeaders(headers) {
|
|
33
28
|
if (!headers) return;
|
|
@@ -126,22 +121,19 @@ function applyAnthropicFallbackBoundary(params) {
|
|
|
126
121
|
//#region packages/ai/src/transports/anthropic-payload-policy.ts
|
|
127
122
|
const ANTHROPIC_CACHE_CONTROL_LIMIT = 4;
|
|
128
123
|
const ANTHROPIC_COMPACT_THRESHOLD_MIN = 5e4;
|
|
129
|
-
const ANTHROPIC_COMPACTION_INSTRUCTIONS = "Summarize the transcript inside <summary></summary> tags. Include the current state, decisions, and next steps needed to continue the task in the next context window. Do not call any tools while writing this summary; respond with text only.";
|
|
130
124
|
/** Resolve the Anthropic input-token trigger, including the API's minimum. */
|
|
131
|
-
function resolveAnthropicCompactThreshold(
|
|
125
|
+
function resolveAnthropicCompactThreshold(contextWindow, configured) {
|
|
132
126
|
const configuredThreshold = parsePositiveInteger(configured);
|
|
133
127
|
if (configuredThreshold !== void 0) return Math.max(ANTHROPIC_COMPACT_THRESHOLD_MIN, configuredThreshold);
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
return Math.max(ANTHROPIC_COMPACT_THRESHOLD_MIN, Math.floor((contextTokens && contextWindow ? Math.min(contextTokens, contextWindow) : contextTokens ?? contextWindow ?? 0) * .7));
|
|
128
|
+
const resolvedContextWindow = parsePositiveInteger(contextWindow);
|
|
129
|
+
return Math.max(ANTHROPIC_COMPACT_THRESHOLD_MIN, Math.floor((resolvedContextWindow ?? 0) * .7));
|
|
137
130
|
}
|
|
138
131
|
/** Resolve the server-compaction gate and effective threshold for an Anthropic route. */
|
|
139
132
|
function resolveAnthropicServerCompactionPlan(model, extraParams, apiKey) {
|
|
140
|
-
const
|
|
141
|
-
const enabled = configured !== false && (configured === true || supportsClaudeServerCompaction(model)) && !isAnthropicOAuthApiKey(apiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
|
|
133
|
+
const enabled = extraParams?.anthropicServerCompaction === true && !isAnthropicOAuthApiKey(apiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
|
|
142
134
|
return {
|
|
143
135
|
enabled,
|
|
144
|
-
...enabled ? { threshold: resolveAnthropicCompactThreshold(model, extraParams?.anthropicCompactThreshold) } : {}
|
|
136
|
+
...enabled ? { threshold: resolveAnthropicCompactThreshold(model.contextWindow, extraParams?.anthropicCompactThreshold) } : {}
|
|
145
137
|
};
|
|
146
138
|
}
|
|
147
139
|
function isDirectAnthropicModel(model, env = process.env) {
|
|
@@ -151,7 +143,7 @@ function isDirectAnthropicModel(model, env = process.env) {
|
|
|
151
143
|
baseUrl
|
|
152
144
|
};
|
|
153
145
|
const endpointClass = resolveProviderEndpoint(endpointModel).endpointClass;
|
|
154
|
-
return normalizeOptionalLowercaseString(model.provider) === "anthropic" && (endpointClass === "anthropic-public" || endpointClass === "default" && (!baseUrl ||
|
|
146
|
+
return normalizeOptionalLowercaseString(model.provider) === "anthropic" && (endpointClass === "anthropic-public" || endpointClass === "default" && (!baseUrl || URL.parse(baseUrl)?.hostname === "api.anthropic.com"));
|
|
155
147
|
}
|
|
156
148
|
function supportsAnthropicServerSideFallback(model) {
|
|
157
149
|
return (usesClaudeFable5MessagesContract(model) || resolveClaudeOpus5ModelIdentity(model) !== void 0 || resolveClaudeSonnet55ModelIdentity(model) !== void 0) && isDirectAnthropicModel(model);
|
|
@@ -160,12 +152,9 @@ function isAnthropicServerToolClearingEnabled(model, apiKey) {
|
|
|
160
152
|
const resolvedApiKey = apiKey && getAiTransportHost().resolveSecretSentinel(apiKey);
|
|
161
153
|
return Boolean(resolvedApiKey?.trim()) && !isAnthropicOAuthApiKey(resolvedApiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
|
|
162
154
|
}
|
|
163
|
-
function resolveBaseUrlHostname(baseUrl) {
|
|
164
|
-
return URL.parse(baseUrl)?.hostname;
|
|
165
|
-
}
|
|
166
155
|
function isLongTtlEligibleEndpoint(baseUrl) {
|
|
167
156
|
if (typeof baseUrl !== "string") return false;
|
|
168
|
-
const hostname =
|
|
157
|
+
const hostname = URL.parse(baseUrl)?.hostname;
|
|
169
158
|
if (!hostname) return false;
|
|
170
159
|
return hostname === "api.anthropic.com" || hostname === "aiplatform.googleapis.com" || hostname === "aiplatform.us.rep.googleapis.com" || hostname === "aiplatform.eu.rep.googleapis.com" || hostname.endsWith("-aiplatform.googleapis.com");
|
|
171
160
|
}
|
|
@@ -192,8 +181,7 @@ function buildAnthropicSystemBlocks(systemPrompt, isOAuthToken, cacheControl, cl
|
|
|
192
181
|
type: "text",
|
|
193
182
|
text: sanitizeSurrogates(systemPrompt)
|
|
194
183
|
}] : [];
|
|
195
|
-
|
|
196
|
-
else stripAnthropicSystemPromptBoundary(blocks);
|
|
184
|
+
normalizeAnthropicSystemBlocks(blocks, cacheControl);
|
|
197
185
|
if (systemPrompt && blocks.length === 0) blocks.push({
|
|
198
186
|
type: "text",
|
|
199
187
|
text: ""
|
|
@@ -221,7 +209,7 @@ function applyAnthropicRequestCacheControl(payload, cacheControl, supportsCacheC
|
|
|
221
209
|
const usedMarkers = countAnthropicCacheControlMarkers(payload.system) + countAnthropicCacheControlMarkers(payload.tools);
|
|
222
210
|
applyAnthropicCacheControlToMessages(payload.messages, cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
|
|
223
211
|
}
|
|
224
|
-
function
|
|
212
|
+
function normalizeAnthropicSystemBlocks(system, cacheControl) {
|
|
225
213
|
if (!Array.isArray(system)) return;
|
|
226
214
|
const normalizedBlocks = [];
|
|
227
215
|
for (const block of system) {
|
|
@@ -235,6 +223,10 @@ function applyAnthropicCacheControlToSystem(system, cacheControl) {
|
|
|
235
223
|
normalizedBlocks.push(block);
|
|
236
224
|
continue;
|
|
237
225
|
}
|
|
226
|
+
if (!cacheControl) {
|
|
227
|
+
record.text = stripSystemPromptCacheBoundary(blockText);
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
238
230
|
const text = stripSystemPromptRelocatableBoundary(blockText);
|
|
239
231
|
record.text = text;
|
|
240
232
|
const split = splitSystemPromptCacheBoundary(text);
|
|
@@ -254,25 +246,17 @@ function applyAnthropicCacheControlToSystem(system, cacheControl) {
|
|
|
254
246
|
text: split.dynamicSuffix
|
|
255
247
|
});
|
|
256
248
|
}
|
|
257
|
-
system.splice(0, system.length, ...normalizedBlocks);
|
|
258
|
-
}
|
|
259
|
-
function stripAnthropicSystemPromptBoundary(system) {
|
|
260
|
-
if (!Array.isArray(system)) return;
|
|
261
|
-
for (const block of system) {
|
|
262
|
-
if (!block || typeof block !== "object") continue;
|
|
263
|
-
const record = block;
|
|
264
|
-
if (record.type === "text" && typeof record.text === "string") record.text = stripSystemPromptCacheBoundary(record.text);
|
|
265
|
-
}
|
|
249
|
+
if (cacheControl) system.splice(0, system.length, ...normalizedBlocks);
|
|
266
250
|
}
|
|
267
251
|
/** Apply one shared deepest-stable-message cache breakpoint policy. */
|
|
268
252
|
function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimit, cacheBreakpointOptOutMessageIndexes) {
|
|
269
253
|
if (!Array.isArray(messages) || messages.length === 0 || markerLimit <= 0) return;
|
|
270
|
-
let
|
|
271
|
-
for (
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const record =
|
|
275
|
-
if (
|
|
254
|
+
let fallbackToolResult;
|
|
255
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
256
|
+
const message = messages[i];
|
|
257
|
+
if (!message || typeof message !== "object") continue;
|
|
258
|
+
const record = message;
|
|
259
|
+
if (record.role !== "user" || cacheBreakpointOptOutMessageIndexes.has(i)) continue;
|
|
276
260
|
const content = record.content;
|
|
277
261
|
const blocks = typeof content === "string" ? [{
|
|
278
262
|
type: "text",
|
|
@@ -280,16 +264,23 @@ function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimi
|
|
|
280
264
|
}] : content;
|
|
281
265
|
if (!Array.isArray(blocks)) continue;
|
|
282
266
|
for (let j = blocks.length - 1; j >= 0; j--) {
|
|
283
|
-
const
|
|
284
|
-
if (!
|
|
285
|
-
|
|
267
|
+
const block = blocks[j];
|
|
268
|
+
if (!block || typeof block !== "object") continue;
|
|
269
|
+
const blockRecord = block;
|
|
270
|
+
if (blockRecord.type === "text" || blockRecord.type === "image") {
|
|
271
|
+
if (fallbackToolResult && markerLimit === 1) {
|
|
272
|
+
fallbackToolResult.cache_control = cacheControl;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
286
275
|
blockRecord.cache_control = cacheControl;
|
|
287
276
|
if (typeof content === "string") record.content = blocks;
|
|
288
|
-
|
|
289
|
-
|
|
277
|
+
if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
|
|
278
|
+
return;
|
|
290
279
|
}
|
|
280
|
+
if (blockRecord.type === "tool_result" && fallbackToolResult === void 0) fallbackToolResult = blockRecord;
|
|
291
281
|
}
|
|
292
282
|
}
|
|
283
|
+
if (fallbackToolResult) fallbackToolResult.cache_control = cacheControl;
|
|
293
284
|
}
|
|
294
285
|
function countAnthropicCacheControlMarkers(blocks) {
|
|
295
286
|
if (!Array.isArray(blocks)) return 0;
|
|
@@ -310,7 +301,7 @@ function resolveAnthropicPayloadPolicy(input, model) {
|
|
|
310
301
|
return {
|
|
311
302
|
allowsServiceTier: capabilities.allowsAnthropicServiceTier,
|
|
312
303
|
cacheControl: input.enableCacheControl === true ? resolveAnthropicEphemeralCacheControl(input.baseUrl, input.cacheRetention) : void 0,
|
|
313
|
-
compactThreshold: serverCompactionPlan.threshold ?? resolveAnthropicCompactThreshold(input, input.extraParams?.anthropicCompactThreshold),
|
|
304
|
+
compactThreshold: serverCompactionPlan.threshold ?? resolveAnthropicCompactThreshold(input.contextWindow, input.extraParams?.anthropicCompactThreshold),
|
|
314
305
|
serviceTier: input.serviceTier,
|
|
315
306
|
useServerCompaction: input.enableServerCompaction === true && serverCompactionPlan.enabled,
|
|
316
307
|
...input.cacheTtlPruning && normalizeOptionalLowercaseString(input.api) === "anthropic-messages" && isDirectAnthropicModel(input) ? { toolClearing: {
|
|
@@ -363,8 +354,7 @@ function applyAnthropicContextManagementEdits(payload, policy) {
|
|
|
363
354
|
trigger: {
|
|
364
355
|
type: "input_tokens",
|
|
365
356
|
value: policy.compactThreshold
|
|
366
|
-
}
|
|
367
|
-
instructions: ANTHROPIC_COMPACTION_INSTRUCTIONS
|
|
357
|
+
}
|
|
368
358
|
});
|
|
369
359
|
if (edits.length > 0) payload.context_management = { edits };
|
|
370
360
|
}
|
|
@@ -373,7 +363,7 @@ function applyAnthropicContextManagementToRequest(payload, model, options, direc
|
|
|
373
363
|
applyAnthropicContextManagementEdits(payload, resolveAnthropicPayloadPolicy({
|
|
374
364
|
...model,
|
|
375
365
|
api: "anthropic-messages",
|
|
376
|
-
enableServerCompaction:
|
|
366
|
+
enableServerCompaction: true,
|
|
377
367
|
extraParams: { ...options },
|
|
378
368
|
cacheTtlPruning: options?.cacheTtlPruning
|
|
379
369
|
}));
|
|
@@ -406,8 +396,7 @@ function logAnthropicContextEdits(event) {
|
|
|
406
396
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
407
397
|
function applyAnthropicPayloadPolicyToParams(payloadObj, policy, cacheBreakpointOptOutMessageIndexes) {
|
|
408
398
|
if (policy.allowsServiceTier && policy.serviceTier !== void 0 && payloadObj.service_tier === void 0) payloadObj.service_tier = policy.serviceTier;
|
|
409
|
-
|
|
410
|
-
else stripAnthropicSystemPromptBoundary(payloadObj.system);
|
|
399
|
+
normalizeAnthropicSystemBlocks(payloadObj.system, policy.cacheControl);
|
|
411
400
|
applyAnthropicContextManagementEdits(payloadObj, policy);
|
|
412
401
|
applyAnthropicRequestCacheControl(payloadObj, policy.cacheControl, false, cacheBreakpointOptOutMessageIndexes);
|
|
413
402
|
}
|
|
@@ -498,11 +487,7 @@ function captureAnthropicCompaction(output, capture, model, options) {
|
|
|
498
487
|
baseUrlHash: context.baseUrlHash
|
|
499
488
|
};
|
|
500
489
|
}
|
|
501
|
-
|
|
502
|
-
* Record a rejected replay on the failed output and notify the transcript owner, which must
|
|
503
|
-
* durably strip the checkpoint: hosts may persist failed turns without their replay state.
|
|
504
|
-
*/
|
|
505
|
-
function suppressAnthropicCompaction(output, model, options, rejected) {
|
|
490
|
+
function suppressAnthropicCompaction(output, model, options) {
|
|
506
491
|
const context = buildProviderReplayContext(model, options);
|
|
507
492
|
if (!context.baseUrlHash) return;
|
|
508
493
|
output.providerReplay = {
|
|
@@ -512,7 +497,6 @@ function suppressAnthropicCompaction(output, model, options, rejected) {
|
|
|
512
497
|
...context,
|
|
513
498
|
baseUrlHash: context.baseUrlHash
|
|
514
499
|
};
|
|
515
|
-
options?.onCompactionRejected?.({ data: rejected.content });
|
|
516
500
|
}
|
|
517
501
|
function resolveNewestAnthropicCompaction(messages, model, options) {
|
|
518
502
|
const context = buildProviderReplayContext(model, options);
|
|
@@ -622,9 +606,6 @@ function logAnthropicThinkingDrops(transformations) {
|
|
|
622
606
|
const paths = drops.slice(0, 5).map(({ reason, path }) => `${reason} at ${path}`);
|
|
623
607
|
getAiTransportHost().logWarn("anthropic", `replayed thinking dropped: ${drops.length} block(s) (${paths.join(", ")}${drops.length > 5 ? ", …" : ""})`);
|
|
624
608
|
}
|
|
625
|
-
function asReplayMessage(value) {
|
|
626
|
-
return value && typeof value === "object" ? value : void 0;
|
|
627
|
-
}
|
|
628
609
|
/**
|
|
629
610
|
* Anthropic tool results continue the preceding assistant turn. Preserve that
|
|
630
611
|
* turn's signed thinking even when the next request disables new thinking.
|
|
@@ -633,7 +614,7 @@ function findActiveAnthropicToolTurnAssistantIndex(messages) {
|
|
|
633
614
|
const toolResultIds = /* @__PURE__ */ new Set();
|
|
634
615
|
let index = messages.length - 1;
|
|
635
616
|
while (index >= 0) {
|
|
636
|
-
const message =
|
|
617
|
+
const message = asOptionalObjectRecord(messages[index]);
|
|
637
618
|
if (message?.role === "user" && isRecord(message.operatorMessage)) {
|
|
638
619
|
index -= 1;
|
|
639
620
|
continue;
|
|
@@ -643,12 +624,12 @@ function findActiveAnthropicToolTurnAssistantIndex(messages) {
|
|
|
643
624
|
index -= 1;
|
|
644
625
|
}
|
|
645
626
|
if (toolResultIds.size === 0) return -1;
|
|
646
|
-
const assistant =
|
|
627
|
+
const assistant = asOptionalObjectRecord(messages[index]);
|
|
647
628
|
if (assistant?.role !== "assistant" || !Array.isArray(assistant.content)) return -1;
|
|
648
629
|
const toolCallIds = /* @__PURE__ */ new Set();
|
|
649
630
|
for (const block of assistant.content) {
|
|
650
|
-
|
|
651
|
-
|
|
631
|
+
const record = asOptionalObjectRecord(block);
|
|
632
|
+
if (!record) continue;
|
|
652
633
|
if ((record.type === "toolCall" || record.type === "tool_use" || record.type === "function_call") && typeof record.id === "string") toolCallIds.add(record.id);
|
|
653
634
|
}
|
|
654
635
|
return [...toolResultIds].every((toolCallId) => toolCallIds.has(toolCallId)) ? index : -1;
|
|
@@ -798,12 +779,7 @@ function reconcileAnthropicToolChoice(choice, projection) {
|
|
|
798
779
|
if (projection.inputToolCount === 0) return choice;
|
|
799
780
|
if (choice.type === "tool") {
|
|
800
781
|
const requestedName = choice.name;
|
|
801
|
-
const
|
|
802
|
-
if (originalMatch) return {
|
|
803
|
-
...choice,
|
|
804
|
-
name: originalMatch.wireName
|
|
805
|
-
};
|
|
806
|
-
const matchedTool = projection.unavailableOriginalNames.has(requestedName) ? void 0 : projection.tools.find((tool) => tool.wireName === requestedName);
|
|
782
|
+
const matchedTool = projection.tools.find((tool) => tool.originalName === requestedName) ?? (projection.unavailableOriginalNames.has(requestedName) ? void 0 : projection.tools.find((tool) => tool.wireName === requestedName));
|
|
807
783
|
if (!matchedTool) throw new Error(`Anthropic tool_choice requested unavailable tool "${requestedName}" after schema conversion`);
|
|
808
784
|
return {
|
|
809
785
|
...choice,
|
|
@@ -846,9 +822,6 @@ function resolveAnthropicTransportOptions(model, options, apiKey) {
|
|
|
846
822
|
useModelDefault: resolveClaudeHaiku55ModelIdentity(model) !== void 0 || resolveClaudeSonnet5ModelIdentity(model) !== void 0 || resolveClaudeOpus5ModelIdentity(model) !== void 0
|
|
847
823
|
});
|
|
848
824
|
if (baseMaxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
|
|
849
|
-
const reasoningModelMaxTokens = resolvePositiveAnthropicTokenLimit(model.maxTokens) ?? baseMaxTokens;
|
|
850
|
-
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
851
|
-
const reasoning = options?.reasoning === "off" && mandatoryAdaptiveThinking ? "low" : options?.reasoning;
|
|
852
825
|
const resolved = copyProviderAcceptanceObserver(options, {
|
|
853
826
|
temperature: options?.temperature,
|
|
854
827
|
stop: options?.stop,
|
|
@@ -866,56 +839,71 @@ function resolveAnthropicTransportOptions(model, options, apiKey) {
|
|
|
866
839
|
toolChoice: options?.toolChoice,
|
|
867
840
|
thinkingBudgets: options?.thinkingBudgets,
|
|
868
841
|
thinkingDisplay: options?.thinkingDisplay,
|
|
869
|
-
reasoning,
|
|
842
|
+
reasoning: options?.reasoning,
|
|
870
843
|
anthropicServerCompaction: options?.anthropicServerCompaction,
|
|
871
844
|
anthropicCompactThreshold: options?.anthropicCompactThreshold,
|
|
872
845
|
cacheTtlPruning: options?.cacheTtlPruning,
|
|
873
846
|
...options?.authProfileId ? { authProfileId: options.authProfileId } : {}
|
|
874
847
|
});
|
|
848
|
+
resolved.reasoning = applyAnthropicThinkingOptions(model, resolved, options, "transport");
|
|
849
|
+
return resolved;
|
|
850
|
+
}
|
|
851
|
+
/** Keep both entry points on one thinking mode while retaining their output-budget policies. */
|
|
852
|
+
function applyAnthropicThinkingOptions(model, resolved, options, profile) {
|
|
853
|
+
const managed = profile === "transport";
|
|
854
|
+
const reasoning = options?.reasoning === "off" && requiresClaudeAdaptiveThinking(model) ? "low" : options?.reasoning;
|
|
875
855
|
if (reasoning === "off") {
|
|
876
856
|
resolved.thinkingEnabled = false;
|
|
877
|
-
return
|
|
857
|
+
return reasoning;
|
|
878
858
|
}
|
|
879
|
-
if (!reasoning) {
|
|
880
|
-
resolved.thinkingEnabled = defaultsClaudeAdaptiveThinking(model);
|
|
859
|
+
if (!reasoning || supportsClaudeAdaptiveThinking(model) || !managed && defaultsClaudeAdaptiveThinking(model)) {
|
|
860
|
+
resolved.thinkingEnabled = Boolean(reasoning) || defaultsClaudeAdaptiveThinking(model);
|
|
881
861
|
if (resolved.thinkingEnabled) resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
882
|
-
return
|
|
883
|
-
}
|
|
884
|
-
if (supportsClaudeAdaptiveThinking(model)) {
|
|
885
|
-
resolved.thinkingEnabled = true;
|
|
886
|
-
resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
887
|
-
return resolved;
|
|
862
|
+
return reasoning;
|
|
888
863
|
}
|
|
889
|
-
const adjusted = adjustMaxTokensForThinking(
|
|
864
|
+
const adjusted = adjustMaxTokensForThinking(resolved.maxTokens, (managed ? resolvePositiveAnthropicTokenLimit(model.maxTokens) : model.maxTokens) ?? resolved.maxTokens, managed && reasoning === "max" ? "high" : reasoning, options?.thinkingBudgets);
|
|
890
865
|
const thinkingEnabled = adjusted.thinkingBudget >= 1024;
|
|
891
|
-
resolved.maxTokens = adjusted.maxTokens;
|
|
866
|
+
if (managed || thinkingEnabled) resolved.maxTokens = adjusted.maxTokens;
|
|
892
867
|
resolved.thinkingEnabled = thinkingEnabled;
|
|
893
868
|
resolved.thinkingBudgetTokens = thinkingEnabled ? adjusted.thinkingBudget : void 0;
|
|
894
|
-
return
|
|
869
|
+
return reasoning;
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Largest completion a request may ask for once transport thinking options
|
|
873
|
+
* apply, for callers that size the prompt before dispatch. Claude transports
|
|
874
|
+
* (Anthropic Messages, Vertex, Bedrock, Bedrock Mantle) may add a thinking
|
|
875
|
+
* budget to `maxTokens`, capped at the model's output limit; some add it even
|
|
876
|
+
* for adaptive models, and their Claude detection is plugin-owned, so every
|
|
877
|
+
* request on these APIs reserves the default budget for its level. Other
|
|
878
|
+
* transports keep `maxTokens` as the total output limit. Over-reserving only
|
|
879
|
+
* shrinks the prompt; it never overflows the window.
|
|
880
|
+
*/
|
|
881
|
+
function resolveCompletionTokenReservation(model, maxTokens, reasoning) {
|
|
882
|
+
if (!reasoning || reasoning === "off" || model.api !== "anthropic-messages" && model.api !== "bedrock-converse-stream") return maxTokens;
|
|
883
|
+
return adjustMaxTokensForThinking(maxTokens, resolvePositiveAnthropicTokenLimit(model.maxTokens) ?? Number.POSITIVE_INFINITY, reasoning).maxTokens;
|
|
895
884
|
}
|
|
896
885
|
//#endregion
|
|
897
886
|
//#region packages/ai/src/transports/anthropic-messages.ts
|
|
898
887
|
const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
899
888
|
async function convertContentBlocks(content, model, imageBudget, profile, isError) {
|
|
900
889
|
const mediaPlaceholder = describeToolResultMediaPlaceholder(content);
|
|
901
|
-
if (!((profile === "provider" || model.input.includes("image")) && content.some(isImageWithMediaPayload)))
|
|
890
|
+
if (!((profile === "provider" || model.input.includes("image")) && content.some(isImageWithMediaPayload))) {
|
|
891
|
+
const text = extractToolResultText(content);
|
|
892
|
+
return text.trim() ? text : mediaPlaceholder ?? (profile === "transport" ? "(no output)" : isError ? "[tool error with no output]" : "");
|
|
893
|
+
}
|
|
902
894
|
const blocks = [];
|
|
903
|
-
let hasTextBlock = false;
|
|
904
895
|
for (const block of content) {
|
|
905
896
|
const record = asOptionalObjectRecord(block);
|
|
906
897
|
if (!record) continue;
|
|
907
898
|
const blockText = extractToolResultBlockText(block);
|
|
908
|
-
if (blockText) {
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
});
|
|
913
|
-
hasTextBlock = true;
|
|
914
|
-
}
|
|
899
|
+
if (blockText) blocks.push({
|
|
900
|
+
type: "text",
|
|
901
|
+
text: blockText
|
|
902
|
+
});
|
|
915
903
|
if (!isImageWithMediaPayload(record)) continue;
|
|
916
904
|
const [normalizedImage] = await normalizeAnthropicInlineContent([{
|
|
917
905
|
type: "image",
|
|
918
|
-
data:
|
|
906
|
+
data: record.data,
|
|
919
907
|
mimeType: typeof record.mimeType === "string" ? record.mimeType : profile === "provider" ? "image/jpeg" : "image/png"
|
|
920
908
|
}], imageBudget);
|
|
921
909
|
if (normalizedImage?.type !== "image") continue;
|
|
@@ -928,7 +916,7 @@ async function convertContentBlocks(content, model, imageBudget, profile, isErro
|
|
|
928
916
|
}
|
|
929
917
|
});
|
|
930
918
|
}
|
|
931
|
-
if (!
|
|
919
|
+
if (!blocks.some((block) => block.type === "text")) blocks.unshift({
|
|
932
920
|
type: "text",
|
|
933
921
|
text: mediaPlaceholder ?? "(see attached image)"
|
|
934
922
|
});
|
|
@@ -1086,12 +1074,17 @@ async function convertAnthropicMessages(transformedMessages, model, isOAuthToken
|
|
|
1086
1074
|
return params;
|
|
1087
1075
|
}
|
|
1088
1076
|
/** Shared generation contract, after each entry point resolves its defaults and tool policy. */
|
|
1089
|
-
function buildAnthropicGenerationParams({ model, options,
|
|
1077
|
+
function buildAnthropicGenerationParams({ model, options, toolProjection, eagerToolInputStreaming, profile }) {
|
|
1090
1078
|
const params = {};
|
|
1091
1079
|
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
1092
1080
|
if (options?.temperature !== void 0 && !options?.thinkingEnabled && !supportsClaudeNativeXhighEffort(model)) params.temperature = options.temperature;
|
|
1093
1081
|
if (options?.stop !== void 0 && options.stop.length > 0) params.stop_sequences = options.stop;
|
|
1094
|
-
if (
|
|
1082
|
+
if (toolProjection && toolProjection.tools.length > 0) params.tools = toolProjection.tools.map((tool) => ({
|
|
1083
|
+
name: tool.wireName,
|
|
1084
|
+
description: tool.description,
|
|
1085
|
+
input_schema: tool.inputSchema,
|
|
1086
|
+
...eagerToolInputStreaming ? { eager_input_streaming: true } : {}
|
|
1087
|
+
}));
|
|
1095
1088
|
if (mandatoryAdaptiveThinking || model.reasoning || supportsClaudeAdaptiveThinking(model)) {
|
|
1096
1089
|
if (mandatoryAdaptiveThinking || options?.thinkingEnabled) {
|
|
1097
1090
|
const display = options?.thinkingDisplay ?? "summarized";
|
|
@@ -1120,21 +1113,6 @@ function buildAnthropicGenerationParams({ model, options, tools, toolProjection,
|
|
|
1120
1113
|
}
|
|
1121
1114
|
return params;
|
|
1122
1115
|
}
|
|
1123
|
-
function convertAnthropicTools(tools, isOAuthTokenLocal, supportsEagerToolInputStreaming = false) {
|
|
1124
|
-
const projection = projectAnthropicTools(tools, (name) => isOAuthTokenLocal ? toClaudeCodeToolName(name) : name);
|
|
1125
|
-
return {
|
|
1126
|
-
projection,
|
|
1127
|
-
tools: projection.tools.map((tool) => {
|
|
1128
|
-
const projected = {
|
|
1129
|
-
name: tool.wireName,
|
|
1130
|
-
description: tool.description,
|
|
1131
|
-
input_schema: tool.inputSchema
|
|
1132
|
-
};
|
|
1133
|
-
if (supportsEagerToolInputStreaming) projected.eager_input_streaming = true;
|
|
1134
|
-
return projected;
|
|
1135
|
-
})
|
|
1136
|
-
};
|
|
1137
|
-
}
|
|
1138
1116
|
/** Assemble both public Messages routes without changing their replay/default profiles. */
|
|
1139
1117
|
async function buildAnthropicRequest(model, context, options, profile, isOAuthToken, serverSideFallback, claudeCodeVersion) {
|
|
1140
1118
|
const managed = profile === "transport";
|
|
@@ -1146,8 +1124,13 @@ async function buildAnthropicRequest(model, context, options, profile, isOAuthTo
|
|
|
1146
1124
|
if (managed && maxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
|
|
1147
1125
|
const { cacheControl, supportsCacheControlOnTools } = resolveAnthropicCacheOptions(model, options?.cacheRetention);
|
|
1148
1126
|
const system = buildAnthropicSystemBlocks(context.systemPrompt, isOAuthToken, cacheControl, claudeCodeVersion);
|
|
1149
|
-
|
|
1150
|
-
|
|
1127
|
+
let toolProjection;
|
|
1128
|
+
let eagerToolInputStreaming = false;
|
|
1129
|
+
if (context.tools) {
|
|
1130
|
+
const tools = context.tools;
|
|
1131
|
+
eagerToolInputStreaming = !managed && (model.compat?.supportsEagerToolInputStreaming ?? model.provider !== "fireworks");
|
|
1132
|
+
toolProjection = projectAnthropicTools(tools, (name) => isOAuthToken ? toClaudeCodeToolName(name) : name);
|
|
1133
|
+
}
|
|
1151
1134
|
const replayPlan = buildAnthropicReplayPlan(context.messages, model, {
|
|
1152
1135
|
enabled: !isOAuthToken && options?.anthropicServerCompaction === true,
|
|
1153
1136
|
authProfileId: options?.authProfileId,
|
|
@@ -1177,15 +1160,15 @@ async function buildAnthropicRequest(model, context, options, profile, isOAuthTo
|
|
|
1177
1160
|
Object.assign(params, buildAnthropicGenerationParams({
|
|
1178
1161
|
model,
|
|
1179
1162
|
options,
|
|
1180
|
-
tools: convertedTools?.tools,
|
|
1181
1163
|
toolProjection,
|
|
1164
|
+
eagerToolInputStreaming,
|
|
1182
1165
|
profile
|
|
1183
1166
|
}));
|
|
1184
1167
|
applyAnthropicRequestCacheControl(params, cacheControl, supportsCacheControlOnTools, cacheBreakpointOptOutMessageIndexes);
|
|
1185
1168
|
return {
|
|
1186
1169
|
params,
|
|
1187
1170
|
toolProjection,
|
|
1188
|
-
|
|
1171
|
+
usedCompactionReplay: replayPlan.compaction !== void 0
|
|
1189
1172
|
};
|
|
1190
1173
|
}
|
|
1191
1174
|
/** Apply caller payload edits before restoring required request contracts and beta headers. */
|
|
@@ -1204,19 +1187,12 @@ async function prepareAnthropicRequest(initialParams, model, options, directApiK
|
|
|
1204
1187
|
}
|
|
1205
1188
|
//#endregion
|
|
1206
1189
|
//#region packages/ai/src/providers/anthropic-refusal.ts
|
|
1207
|
-
function readAnthropicRefusalDetails(value) {
|
|
1208
|
-
if (!value || typeof value !== "object") return {
|
|
1209
|
-
category: null,
|
|
1210
|
-
explanation: null
|
|
1211
|
-
};
|
|
1212
|
-
const details = value;
|
|
1213
|
-
return {
|
|
1214
|
-
category: normalizeNullableString(details.category),
|
|
1215
|
-
explanation: normalizeNullableString(details.explanation)
|
|
1216
|
-
};
|
|
1217
|
-
}
|
|
1218
1190
|
function applyAnthropicRefusal(output, stopDetails, provider) {
|
|
1219
|
-
const
|
|
1191
|
+
const record = asOptionalObjectRecord(stopDetails);
|
|
1192
|
+
const details = {
|
|
1193
|
+
category: normalizeNullableString(record?.category),
|
|
1194
|
+
explanation: normalizeNullableString(record?.explanation)
|
|
1195
|
+
};
|
|
1220
1196
|
const category = details.category ? ` (category: ${details.category})` : "";
|
|
1221
1197
|
const explanation = details.explanation ? `: ${details.explanation}` : ".";
|
|
1222
1198
|
output.stopReason = "error";
|
|
@@ -1233,6 +1209,41 @@ function applyAnthropicRefusal(output, stopDetails, provider) {
|
|
|
1233
1209
|
}
|
|
1234
1210
|
//#endregion
|
|
1235
1211
|
//#region packages/ai/src/providers/anthropic-usage.ts
|
|
1212
|
+
const BILLED_USAGE_KEYS = [
|
|
1213
|
+
"input",
|
|
1214
|
+
"output",
|
|
1215
|
+
"cacheRead",
|
|
1216
|
+
"cacheWrite",
|
|
1217
|
+
"cacheWrite1h"
|
|
1218
|
+
];
|
|
1219
|
+
function readAnthropicBilledUsage(usage, missingCacheTokens) {
|
|
1220
|
+
return {
|
|
1221
|
+
input: readAnthropicUsageTokenCount(usage.input_tokens),
|
|
1222
|
+
output: readAnthropicUsageTokenCount(usage.output_tokens),
|
|
1223
|
+
cacheRead: readAnthropicUsageTokenCount(usage.cache_read_input_tokens ?? missingCacheTokens),
|
|
1224
|
+
cacheWrite: readAnthropicUsageTokenCount(usage.cache_creation_input_tokens ?? missingCacheTokens),
|
|
1225
|
+
cacheWrite1h: readAnthropicCacheWriteUsage(usage).cacheWrite1h
|
|
1226
|
+
};
|
|
1227
|
+
}
|
|
1228
|
+
function applyAnthropicBilledUsage(target, resolved) {
|
|
1229
|
+
for (const key of BILLED_USAGE_KEYS) {
|
|
1230
|
+
const value = resolved[key];
|
|
1231
|
+
if (value !== void 0) target[key] = value;
|
|
1232
|
+
}
|
|
1233
|
+
target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
|
|
1234
|
+
}
|
|
1235
|
+
function readAnthropicIterationUsage(value) {
|
|
1236
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return;
|
|
1237
|
+
const { input, output, cacheRead, cacheWrite, cacheWrite1h } = readAnthropicBilledUsage(value);
|
|
1238
|
+
if (input === void 0 || output === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
|
|
1239
|
+
return {
|
|
1240
|
+
input,
|
|
1241
|
+
output,
|
|
1242
|
+
cacheRead,
|
|
1243
|
+
cacheWrite,
|
|
1244
|
+
cacheWrite1h: cacheWrite1h ?? 0
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1236
1247
|
function readAnthropicUsageTokenCount(value) {
|
|
1237
1248
|
return asNonNegativeFiniteNumber(value);
|
|
1238
1249
|
}
|
|
@@ -1260,20 +1271,14 @@ function readAnthropicPromptUsageSnapshot(usage) {
|
|
|
1260
1271
|
function readLastAnthropicIterationUsage(usage) {
|
|
1261
1272
|
if (usage.iterations == null) return { state: "absent" };
|
|
1262
1273
|
if (!Array.isArray(usage.iterations) || usage.iterations.length === 0) return { state: "invalid" };
|
|
1263
|
-
const iteration = usage.iterations.at(-1);
|
|
1264
|
-
if (!iteration
|
|
1265
|
-
const
|
|
1266
|
-
const input = readAnthropicUsageTokenCount(record.input_tokens);
|
|
1267
|
-
const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
|
|
1268
|
-
const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
|
|
1269
|
-
const outputTokens = readAnthropicUsageTokenCount(record.output_tokens);
|
|
1270
|
-
if (input === void 0 || cacheRead === void 0 || cacheWrite === void 0 || outputTokens === void 0) return { state: "invalid" };
|
|
1271
|
-
const contextPromptTokens = input + cacheRead + cacheWrite;
|
|
1274
|
+
const iteration = readAnthropicIterationUsage(usage.iterations.at(-1));
|
|
1275
|
+
if (!iteration) return { state: "invalid" };
|
|
1276
|
+
const contextPromptTokens = iteration.input + iteration.cacheRead + iteration.cacheWrite;
|
|
1272
1277
|
return {
|
|
1273
1278
|
state: "valid",
|
|
1274
1279
|
usage: {
|
|
1275
1280
|
contextPromptTokens,
|
|
1276
|
-
totalTokens: contextPromptTokens +
|
|
1281
|
+
totalTokens: contextPromptTokens + iteration.output
|
|
1277
1282
|
}
|
|
1278
1283
|
};
|
|
1279
1284
|
}
|
|
@@ -1288,19 +1293,10 @@ function readAnthropicCompactionBilledUsage(iterations) {
|
|
|
1288
1293
|
cacheWrite1h: 0
|
|
1289
1294
|
};
|
|
1290
1295
|
for (const iteration of iterations) {
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
const
|
|
1295
|
-
const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
|
|
1296
|
-
const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
|
|
1297
|
-
if (input === void 0 || output === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
|
|
1298
|
-
sawCompaction ||= record.type === "compaction";
|
|
1299
|
-
billed.input += input;
|
|
1300
|
-
billed.output += output;
|
|
1301
|
-
billed.cacheRead += cacheRead;
|
|
1302
|
-
billed.cacheWrite += cacheWrite;
|
|
1303
|
-
billed.cacheWrite1h += readAnthropicCacheWriteUsage(record).cacheWrite1h ?? 0;
|
|
1296
|
+
const resolved = readAnthropicIterationUsage(iteration);
|
|
1297
|
+
if (!resolved) return;
|
|
1298
|
+
sawCompaction ||= iteration.type === "compaction";
|
|
1299
|
+
for (const key of BILLED_USAGE_KEYS) billed[key] += resolved[key];
|
|
1304
1300
|
}
|
|
1305
1301
|
return sawCompaction ? billed : void 0;
|
|
1306
1302
|
}
|
|
@@ -1308,18 +1304,9 @@ function readAnthropicCompactionBilledUsage(iterations) {
|
|
|
1308
1304
|
function applyAnthropicMessageStartUsage(target, payload) {
|
|
1309
1305
|
const promptUsage = readAnthropicPromptUsageSnapshot(payload);
|
|
1310
1306
|
const promptTokens = promptUsage ? promptUsage.input + promptUsage.cacheRead + promptUsage.cacheWrite : 0;
|
|
1311
|
-
const
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
if (outputTokens !== void 0) target.output = outputTokens;
|
|
1315
|
-
const cacheReadTokens = payload.cache_read_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_read_input_tokens);
|
|
1316
|
-
if (cacheReadTokens !== void 0) target.cacheRead = cacheReadTokens;
|
|
1317
|
-
const cacheWriteTokens = payload.cache_creation_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_creation_input_tokens);
|
|
1318
|
-
if (cacheWriteTokens !== void 0) target.cacheWrite = cacheWriteTokens;
|
|
1319
|
-
const { cacheWrite1h } = readAnthropicCacheWriteUsage(payload);
|
|
1320
|
-
if (cacheWrite1h !== void 0) target.cacheWrite1h = cacheWrite1h;
|
|
1321
|
-
target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
|
|
1322
|
-
if (promptTokens > 0 && outputTokens !== void 0) target.contextUsage = {
|
|
1307
|
+
const resolved = readAnthropicBilledUsage(payload, 0);
|
|
1308
|
+
applyAnthropicBilledUsage(target, resolved);
|
|
1309
|
+
if (promptTokens > 0 && resolved.output !== void 0) target.contextUsage = {
|
|
1323
1310
|
state: "available",
|
|
1324
1311
|
promptTokens,
|
|
1325
1312
|
totalTokens: promptTokens + target.output
|
|
@@ -1330,28 +1317,8 @@ function applyAnthropicMessageStartUsage(target, payload) {
|
|
|
1330
1317
|
function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage) {
|
|
1331
1318
|
if (!usage) return;
|
|
1332
1319
|
const billedIterations = readAnthropicCompactionBilledUsage(usage.iterations);
|
|
1333
|
-
const
|
|
1334
|
-
|
|
1335
|
-
const cacheReadTokens = readAnthropicUsageTokenCount(usage.cache_read_input_tokens);
|
|
1336
|
-
const cacheWriteTokens = readAnthropicUsageTokenCount(usage.cache_creation_input_tokens);
|
|
1337
|
-
const resolved = billedIterations ?? {
|
|
1338
|
-
input: inputTokens,
|
|
1339
|
-
output: outputTokens,
|
|
1340
|
-
cacheRead: cacheReadTokens,
|
|
1341
|
-
cacheWrite: cacheWriteTokens,
|
|
1342
|
-
cacheWrite1h: readAnthropicCacheWriteUsage(usage).cacheWrite1h
|
|
1343
|
-
};
|
|
1344
|
-
for (const key of [
|
|
1345
|
-
"input",
|
|
1346
|
-
"output",
|
|
1347
|
-
"cacheRead",
|
|
1348
|
-
"cacheWrite",
|
|
1349
|
-
"cacheWrite1h"
|
|
1350
|
-
]) {
|
|
1351
|
-
const value = resolved[key];
|
|
1352
|
-
if (value !== void 0) target[key] = value;
|
|
1353
|
-
}
|
|
1354
|
-
target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
|
|
1320
|
+
const reported = readAnthropicBilledUsage(usage);
|
|
1321
|
+
applyAnthropicBilledUsage(target, billedIterations ?? reported);
|
|
1355
1322
|
const iterationUsage = readLastAnthropicIterationUsage(usage);
|
|
1356
1323
|
if (iterationUsage.state === "valid") target.contextUsage = {
|
|
1357
1324
|
state: "available",
|
|
@@ -1359,7 +1326,7 @@ function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage)
|
|
|
1359
1326
|
totalTokens: iterationUsage.usage.totalTokens
|
|
1360
1327
|
};
|
|
1361
1328
|
else if (iterationUsage.state === "invalid") target.contextUsage = { state: "unavailable" };
|
|
1362
|
-
else if (
|
|
1329
|
+
else if (reported.output !== void 0 && (messageStartPromptUsage !== void 0 || (reported.cacheRead !== void 0 || reported.cacheWrite !== void 0) && readAnthropicPromptUsageSnapshot(usage) !== void 0)) {
|
|
1363
1330
|
const promptTokens = target.input + target.cacheRead + target.cacheWrite;
|
|
1364
1331
|
target.contextUsage = {
|
|
1365
1332
|
state: "available",
|
|
@@ -1395,14 +1362,44 @@ async function consumeAnthropicStream(params) {
|
|
|
1395
1362
|
let sawMessageStop = false;
|
|
1396
1363
|
let sawStopReason = false;
|
|
1397
1364
|
const pendingTextEnds = [];
|
|
1398
|
-
const flushPendingTextEnds = () => {
|
|
1365
|
+
const flushPendingTextEnds = (classifyToolTurn = false) => {
|
|
1366
|
+
if (managed && classifyToolTurn && (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall"))) tagPendingCommentaryText(output.content);
|
|
1399
1367
|
for (const event of pendingTextEnds) eventSink.push(event);
|
|
1400
1368
|
pendingTextEnds.length = 0;
|
|
1401
1369
|
};
|
|
1402
|
-
const
|
|
1403
|
-
|
|
1370
|
+
const emitContentEnd = (block, contentIndex, deferText) => {
|
|
1371
|
+
const event = {
|
|
1372
|
+
type: `${block.type}_end`,
|
|
1373
|
+
contentIndex,
|
|
1374
|
+
content: block.type === "thinking" ? block.thinking : block.text,
|
|
1375
|
+
partial: output
|
|
1376
|
+
};
|
|
1377
|
+
if (managed && deferText && event.type === "text_end") pendingTextEnds.push(event);
|
|
1404
1378
|
else eventSink.push(event);
|
|
1405
1379
|
};
|
|
1380
|
+
const emitContentStart = (kind, contentIndex, delta = "") => {
|
|
1381
|
+
eventSink.push({
|
|
1382
|
+
type: `${kind}_start`,
|
|
1383
|
+
contentIndex,
|
|
1384
|
+
partial: output
|
|
1385
|
+
});
|
|
1386
|
+
if (delta.length > 0) eventSink.push({
|
|
1387
|
+
type: `${kind}_delta`,
|
|
1388
|
+
contentIndex,
|
|
1389
|
+
delta,
|
|
1390
|
+
partial: output
|
|
1391
|
+
});
|
|
1392
|
+
};
|
|
1393
|
+
const appendContentDelta = (block, contentIndex, delta) => {
|
|
1394
|
+
if (block.type === "thinking") appendAssistantThinking(block, delta);
|
|
1395
|
+
else block.text += delta;
|
|
1396
|
+
eventSink.push({
|
|
1397
|
+
type: `${block.type}_delta`,
|
|
1398
|
+
contentIndex,
|
|
1399
|
+
delta,
|
|
1400
|
+
partial: output
|
|
1401
|
+
});
|
|
1402
|
+
};
|
|
1406
1403
|
const eventIndexKey = (eventIndex) => typeof eventIndex === "number" ? eventIndex : -1;
|
|
1407
1404
|
const appendReasoningContentDelta = (kind, eventIndex, rawText) => {
|
|
1408
1405
|
if (typeof rawText !== "string") return false;
|
|
@@ -1424,22 +1421,10 @@ async function consumeAnthropicStream(params) {
|
|
|
1424
1421
|
output.content.push(block);
|
|
1425
1422
|
contentIndex = output.content.length - 1;
|
|
1426
1423
|
indexes.set(key, contentIndex);
|
|
1427
|
-
|
|
1428
|
-
type: `${kind}_start`,
|
|
1429
|
-
contentIndex,
|
|
1430
|
-
partial: output
|
|
1431
|
-
});
|
|
1424
|
+
emitContentStart(kind, contentIndex);
|
|
1432
1425
|
}
|
|
1433
|
-
if (block.type === "thinking")
|
|
1434
|
-
|
|
1435
|
-
block.thinkingSignature = "reasoning_content";
|
|
1436
|
-
} else if (block.type === "text") block.text += text;
|
|
1437
|
-
eventSink.push({
|
|
1438
|
-
type: `${kind}_delta`,
|
|
1439
|
-
contentIndex,
|
|
1440
|
-
delta: text,
|
|
1441
|
-
partial: output
|
|
1442
|
-
});
|
|
1426
|
+
if (block.type === "thinking") block.thinkingSignature = "reasoning_content";
|
|
1427
|
+
appendContentDelta(block, contentIndex, text);
|
|
1443
1428
|
return true;
|
|
1444
1429
|
};
|
|
1445
1430
|
const finishReasoningContentSidecars = (eventIndex) => {
|
|
@@ -1450,12 +1435,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1450
1435
|
if (contentIndex === void 0) continue;
|
|
1451
1436
|
indexes.delete(key);
|
|
1452
1437
|
const block = output.content[contentIndex];
|
|
1453
|
-
if (block?.type === kind)
|
|
1454
|
-
type: `${kind}_end`,
|
|
1455
|
-
contentIndex,
|
|
1456
|
-
content: block.type === "thinking" ? block.thinking : block.text,
|
|
1457
|
-
partial: output
|
|
1458
|
-
});
|
|
1438
|
+
if (block?.type === kind) emitContentEnd(block, contentIndex, false);
|
|
1459
1439
|
}
|
|
1460
1440
|
};
|
|
1461
1441
|
for await (const rawEvent of iterateModelStream(params.events, options.signal)) {
|
|
@@ -1516,23 +1496,8 @@ async function consumeAnthropicStream(params) {
|
|
|
1516
1496
|
for (const [i, block] of blocks.entries()) {
|
|
1517
1497
|
if (block.type !== "text") continue;
|
|
1518
1498
|
delete block.index;
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
contentIndex: i,
|
|
1522
|
-
partial: output
|
|
1523
|
-
});
|
|
1524
|
-
if (block.text) eventSink.push({
|
|
1525
|
-
type: "text_delta",
|
|
1526
|
-
contentIndex: i,
|
|
1527
|
-
delta: block.text,
|
|
1528
|
-
partial: output
|
|
1529
|
-
});
|
|
1530
|
-
emitTextEnd({
|
|
1531
|
-
type: "text_end",
|
|
1532
|
-
contentIndex: i,
|
|
1533
|
-
content: block.text,
|
|
1534
|
-
partial: output
|
|
1535
|
-
});
|
|
1499
|
+
emitContentStart("text", i, block.text);
|
|
1500
|
+
emitContentEnd(block, i, true);
|
|
1536
1501
|
}
|
|
1537
1502
|
continue;
|
|
1538
1503
|
}
|
|
@@ -1554,22 +1519,11 @@ async function consumeAnthropicStream(params) {
|
|
|
1554
1519
|
redacted: true,
|
|
1555
1520
|
index
|
|
1556
1521
|
};
|
|
1557
|
-
const kind = block.type;
|
|
1558
1522
|
const delta = block.type === "text" ? block.text : block.redacted ? "" : block.thinking;
|
|
1559
1523
|
output.content.push(block);
|
|
1560
1524
|
const contentIndex = output.content.length - 1;
|
|
1561
1525
|
blockIndexes.set(index, contentIndex);
|
|
1562
|
-
|
|
1563
|
-
type: `${kind}_start`,
|
|
1564
|
-
contentIndex,
|
|
1565
|
-
partial: output
|
|
1566
|
-
});
|
|
1567
|
-
if (delta.length > 0) eventSink.push({
|
|
1568
|
-
type: `${kind}_delta`,
|
|
1569
|
-
contentIndex,
|
|
1570
|
-
delta,
|
|
1571
|
-
partial: output
|
|
1572
|
-
});
|
|
1526
|
+
emitContentStart(block.type, contentIndex, delta);
|
|
1573
1527
|
continue;
|
|
1574
1528
|
}
|
|
1575
1529
|
if (contentBlock?.type === "tool_use") {
|
|
@@ -1609,13 +1563,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1609
1563
|
const text = sanitizeTransportPayloadText(delta.content);
|
|
1610
1564
|
if (text.length > 0) {
|
|
1611
1565
|
if (block?.type === "text" && index !== void 0) {
|
|
1612
|
-
block
|
|
1613
|
-
eventSink.push({
|
|
1614
|
-
type: "text_delta",
|
|
1615
|
-
contentIndex: index,
|
|
1616
|
-
delta: text,
|
|
1617
|
-
partial: output
|
|
1618
|
-
});
|
|
1566
|
+
appendContentDelta(block, index, text);
|
|
1619
1567
|
appendedContent = true;
|
|
1620
1568
|
} else appendedContent = appendReasoningContentDelta("text", event.index, text);
|
|
1621
1569
|
}
|
|
@@ -1631,31 +1579,15 @@ async function consumeAnthropicStream(params) {
|
|
|
1631
1579
|
output.content.push(block);
|
|
1632
1580
|
index = output.content.length - 1;
|
|
1633
1581
|
if (typeof event.index === "number") blockIndexes.set(event.index, index);
|
|
1634
|
-
|
|
1635
|
-
type: "text_start",
|
|
1636
|
-
contentIndex: index,
|
|
1637
|
-
partial: output
|
|
1638
|
-
});
|
|
1582
|
+
emitContentStart("text", index);
|
|
1639
1583
|
}
|
|
1640
1584
|
if (index === void 0) continue;
|
|
1641
1585
|
if (block?.type === "text" && delta?.type === "text_delta" && typeof delta.text === "string") {
|
|
1642
|
-
block
|
|
1643
|
-
eventSink.push({
|
|
1644
|
-
type: "text_delta",
|
|
1645
|
-
contentIndex: index,
|
|
1646
|
-
delta: delta.text,
|
|
1647
|
-
partial: output
|
|
1648
|
-
});
|
|
1586
|
+
appendContentDelta(block, index, delta.text);
|
|
1649
1587
|
continue;
|
|
1650
1588
|
}
|
|
1651
1589
|
if (block?.type === "thinking" && delta?.type === "thinking_delta" && typeof delta.thinking === "string") {
|
|
1652
|
-
|
|
1653
|
-
eventSink.push({
|
|
1654
|
-
type: "thinking_delta",
|
|
1655
|
-
contentIndex: index,
|
|
1656
|
-
delta: delta.thinking,
|
|
1657
|
-
partial: output
|
|
1658
|
-
});
|
|
1590
|
+
appendContentDelta(block, index, delta.thinking);
|
|
1659
1591
|
continue;
|
|
1660
1592
|
}
|
|
1661
1593
|
if (block?.type === "toolCall" && delta?.type === "input_json_delta" && typeof delta.partial_json === "string") {
|
|
@@ -1693,24 +1625,12 @@ async function consumeAnthropicStream(params) {
|
|
|
1693
1625
|
}
|
|
1694
1626
|
blockIndexes.delete(eventIndex);
|
|
1695
1627
|
delete block.index;
|
|
1696
|
-
if (block.type === "
|
|
1697
|
-
|
|
1698
|
-
contentIndex: index,
|
|
1699
|
-
content: block.text,
|
|
1700
|
-
partial: output
|
|
1701
|
-
});
|
|
1702
|
-
else if (block.type === "thinking") {
|
|
1703
|
-
if (pendingSignature !== void 0) block.thinkingSignature = pendingSignature;
|
|
1704
|
-
eventSink.push({
|
|
1705
|
-
type: "thinking_end",
|
|
1706
|
-
contentIndex: index,
|
|
1707
|
-
content: block.thinking,
|
|
1708
|
-
partial: output
|
|
1709
|
-
});
|
|
1710
|
-
} else if (block.type === "toolCall") sealedToolCalls.push({
|
|
1628
|
+
if (block.type === "thinking" && pendingSignature !== void 0) block.thinkingSignature = pendingSignature;
|
|
1629
|
+
if (block.type === "toolCall") sealedToolCalls.push({
|
|
1711
1630
|
block,
|
|
1712
1631
|
contentIndex: index
|
|
1713
1632
|
});
|
|
1633
|
+
else emitContentEnd(block, index, true);
|
|
1714
1634
|
finishReasoningContentSidecars(event.index);
|
|
1715
1635
|
continue;
|
|
1716
1636
|
}
|
|
@@ -1725,8 +1645,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1725
1645
|
}
|
|
1726
1646
|
applyAnthropicMessageDeltaUsage(output.usage, usage, messageStartPromptUsage);
|
|
1727
1647
|
calculateCost(costModel, output.usage);
|
|
1728
|
-
|
|
1729
|
-
flushPendingTextEnds();
|
|
1648
|
+
flushPendingTextEnds(true);
|
|
1730
1649
|
}
|
|
1731
1650
|
}
|
|
1732
1651
|
if ((isDirectAnthropicModel(model) || !managed && refusalBuffer) && !sawMessageStop) throw new Error("Anthropic stream ended before message_stop");
|
|
@@ -1745,11 +1664,10 @@ async function consumeAnthropicStream(params) {
|
|
|
1745
1664
|
});
|
|
1746
1665
|
}
|
|
1747
1666
|
refusalBuffer?.flush();
|
|
1748
|
-
|
|
1749
|
-
flushPendingTextEnds();
|
|
1667
|
+
flushPendingTextEnds(true);
|
|
1750
1668
|
} finally {
|
|
1751
1669
|
logAnthropicThinkingDrops(inputTransformations);
|
|
1752
1670
|
}
|
|
1753
1671
|
}
|
|
1754
1672
|
//#endregion
|
|
1755
|
-
export {
|
|
1673
|
+
export { applyAnthropicPayloadPolicyToParams as A, resolveAnthropicServerCompactionPlan as B, logAnthropicThinkingDrops as C, suppressAnthropicCompaction as D, resolveNewestAnthropicCompaction as E, logAnthropicContextEdits as F, applyAnthropicFallbackBoundary as G, ANTHROPIC_SERVER_SIDE_FALLBACKS as H, resolveAnthropicCacheOptions as I, isAnthropicOAuthApiKey as J, readAnthropicFallbackBoundary as K, resolveAnthropicEphemeralCacheControl as L, buildAnthropicSystemBlocks as M, isAnthropicServerToolClearingEnabled as N, applyAnthropicContextManagementToRequest as O, isDirectAnthropicModel as P, resolveAnthropicPayloadPolicy as R, findActiveAnthropicToolTurnAssistantIndex as S, isAnthropicReplayRejection as T, ANTHROPIC_SERVER_SIDE_FALLBACK_BETA as U, supportsAnthropicServerSideFallback as V, CLAUDE_OPUS_FALLBACK_MODEL_COST as W, usesFoundryBearerAuth as X, omitFoundryBearerCredentialHeaders as Y, reconcileAnthropicToolChoice as _, readAnthropicPromptUsageSnapshot as a, ANTHROPIC_OMITTED_REASONING_TEXT as b, applyAnthropicRefusal as c, applyAnthropicThinkingOptions as d, resolveAnthropicTransportOptions as f, projectAnthropicTools as g, normalizeAnthropicToolChoice as h, readAnthropicCacheWriteUsage as i, applyAnthropicRequestCacheControl as j, applyAnthropicEphemeralCacheControlMarkers as k, buildAnthropicRequest as l, normalizeAnthropicToolCallId as m, applyAnthropicMessageDeltaUsage as n, readAnthropicUsageTokenCount as o, resolveCompletionTokenReservation as p, resolveAnthropicFallbackServingModelCost as q, applyAnthropicMessageStartUsage as r, readLastAnthropicIterationUsage as s, consumeAnthropicStream as t, prepareAnthropicRequest as u, resolveOriginalAnthropicToolName as v, readAnthropicInputTransformations as w, applyAnthropicThinkingBindingControls as x, toClaudeCodeToolName as y, resolveAnthropicRequestBetaHeader as z };
|