@openclaw/ai 2026.10.1 → 2026.10.5-beta.1
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-BDfXKgF1.mjs → anthropic-BAHKgWam.mjs} +30 -59
- package/dist/{anthropic-payload-policy-BZUAchqb.d.mts → anthropic-payload-policy-DGblkHL5.d.mts} +19 -16
- package/dist/{anthropic-stream-reducer-DHt9uFWX.mjs → anthropic-stream-reducer-g6w1YVii.mjs} +195 -283
- package/dist/{api-registry-DcEP_o4h.d.mts → api-registry-BhXXYdRA.d.mts} +2 -2
- package/dist/assistant-message-events-CHMJgbhs.mjs +18 -0
- package/dist/{assistant-output-mJ9cL4Qg.mjs → assistant-output-CUEeSotf.mjs} +2 -2
- package/dist/{assistant-text-phase-DAv1BdTm.mjs → assistant-text-phase-BrV8KdFO.mjs} +7 -6
- package/dist/{azure-openai-responses-gn9hMf8x.mjs → azure-openai-responses-C7qBuR23.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-Boz2QcpK.mjs +20 -0
- package/dist/{google-interactions-DXu4IYGb.mjs → google-interactions-tjAb1mc5.mjs} +52 -84
- package/dist/{google-messages-Bmheetdg.mjs → google-messages-C-RZzloH.mjs} +25 -35
- package/dist/google-provider-stream-CMImyxbX.mjs +58 -0
- package/dist/{google-shared-DE_ddfsC.mjs → google-shared-OyAtMTZv.mjs} +6 -15
- package/dist/google-vertex-hCxTJea7.mjs +40 -0
- package/dist/{host-BjIYEjwT.d.mts → host-CTDz7LXD.d.mts} +6 -3
- package/dist/{host-JpNulOOs.mjs → host-JWXZWa-c.mjs} +3 -2
- package/dist/{host-policy-CC5H0iRZ.mjs → host-policy-q4Azym9F.mjs} +1 -1
- package/dist/{index-DJrNQnKw.d.mts → index-BYDvopUB.d.mts} +8 -5
- 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 +8 -1
- package/dist/internal/openai-responses-payload-policy.mjs +3 -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-1Bnbneg1.mjs → mistral-DUveP4hF.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-CB1JwNFF.mjs → openai-chatgpt-responses-OCwckGhY.mjs} +164 -234
- package/dist/{openai-completions-BPadHHKe.mjs → openai-completions-DOltBUe9.mjs} +7 -6
- package/dist/{openai-completions-compat-CJ0ktmGy.d.mts → openai-completions-compat-i-nLZxFy.d.mts} +10 -11
- package/dist/{openai-completions-compat-D7hWYB0G.mjs → openai-completions-compat-pg1q4uNG.mjs} +10 -6
- package/dist/{openai-completions-transport-DITBEALX.mjs → openai-completions-transport-OMrn7gnZ.mjs} +1429 -294
- package/dist/{openai-prompt-cache-Dj8qeOHT.mjs → openai-prompt-cache-Cr2xKVrS.mjs} +1 -1
- package/dist/{openai-prompt-cache-C-Q1Ksfs.d.mts → openai-prompt-cache-DIPMrMFy.d.mts} +2 -2
- package/dist/{openai-responses-1_TvcWI3.mjs → openai-responses-BSBJUA19.mjs} +9 -10
- package/dist/{openai-responses-compaction-replay-BB2SWdsx.d.mts → openai-responses-compaction-replay-B2FE4Nw8.d.mts} +4 -4
- package/dist/{openai-responses-contracts-sAvdqNjS.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-m878aVpU.mjs → openai-responses-endpoint-CinGh4VZ.mjs} +14 -4
- package/dist/{openai-responses-prompt-observer-internal-CbH84xIS.mjs → openai-responses-request-lifecycle-D2IFiI0-.mjs} +39 -4
- package/dist/{openai-responses-shared-TCJRAoMW.mjs → openai-responses-shared-DVGhq5xF.mjs} +1036 -327
- package/dist/{openai-responses-terminal-usage-BVvyXZ8u.d.mts → openai-responses-terminal-usage-BygjiAdi.d.mts} +2 -2
- package/dist/{openai-tool-schema-DYNePiF8.mjs → openai-tool-schema-DDPXsvJP.mjs} +236 -399
- 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-Dk4PeQV3.d.mts → provider-options-CVrXZBtA.d.mts} +3 -2
- package/dist/{provider-transport-turn-state-8c_Nj2NS.mjs → provider-transport-turn-state-LuCaTsSO.mjs} +2 -2
- package/dist/{provider-types-De8PRdR0.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-0-Rsezog.mjs} +13 -12
- package/dist/{required-api-key-D4kn3252.mjs → required-api-key-CFJvcFy_.mjs} +1 -1
- package/dist/{simple-options-wW3117Fu.mjs → simple-options-BARFAzk9.mjs} +9 -11
- package/dist/{src-DWJq14pn.mjs → src-C_wfIUmV.mjs} +19 -4
- package/dist/{stream-CElngO8h.mjs → stream-DwrETW0_.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-rOn31nxc.mjs → text-signature-B_MB6U_9.mjs} +32 -5
- package/dist/{tool-schema-json-projection-Ct8SeBeJ.mjs → tool-schema-json-projection-B8sG1I9v.mjs} +29 -23
- package/dist/{transport-stream-shared-CAxkgOSj.d.mts → transport-stream-shared-BmpzDtdQ.d.mts} +4 -4
- package/dist/{transport-stream-shared-C5TAc3U_.mjs → transport-stream-shared-DH08b7Il.mjs} +8 -16
- package/dist/transports.d.mts +33 -22
- package/dist/transports.mjs +73 -1234
- 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-opC2pjk9.mjs} +2 -3
- 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-CyMz2jF_.mjs +0 -54
- package/dist/google-vertex-BEW0OQIA.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-request-lifecycle-Fsag-IGZ.mjs +0 -36
- package/dist/openai-transport-params-ildrm49s.mjs +0 -735
- package/dist/openai-web-search-tools-Cfu0uLyN.mjs +0 -14
- package/dist/provider-replay-context-ywPKKLjL.mjs +0 -25
- package/dist/types-Dlz-8qcy.d.mts +0 -1
package/dist/{anthropic-stream-reducer-DHt9uFWX.mjs → anthropic-stream-reducer-g6w1YVii.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, m as resolveClaudeFable5ModelIdentity, u as bindsClaudeThinkingPrefix, w as supportsClaudeAdaptiveThinking, x as resolveClaudeSonnet55ModelIdentity, y as resolveClaudeOpus55ModelIdentity } from "./src-
|
|
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-C_wfIUmV.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-JWXZWa-c.mjs";
|
|
8
|
+
import { i as providerReplayContextMatches, n as buildProviderReplayContext, r as isProviderReplayContext } from "./text-signature-B_MB6U_9.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-q4Azym9F.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-DH08b7Il.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-BARFAzk9.mjs";
|
|
14
|
+
import { t as parsePositiveInteger } from "./positive-integer-CqWjqSi9.mjs";
|
|
15
|
+
import { n as transformProviderMessages } from "./assistant-output-CUEeSotf.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-BrV8KdFO.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;
|
|
@@ -148,7 +143,7 @@ function isDirectAnthropicModel(model, env = process.env) {
|
|
|
148
143
|
baseUrl
|
|
149
144
|
};
|
|
150
145
|
const endpointClass = resolveProviderEndpoint(endpointModel).endpointClass;
|
|
151
|
-
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"));
|
|
152
147
|
}
|
|
153
148
|
function supportsAnthropicServerSideFallback(model) {
|
|
154
149
|
return (usesClaudeFable5MessagesContract(model) || resolveClaudeOpus5ModelIdentity(model) !== void 0 || resolveClaudeSonnet55ModelIdentity(model) !== void 0) && isDirectAnthropicModel(model);
|
|
@@ -157,12 +152,9 @@ function isAnthropicServerToolClearingEnabled(model, apiKey) {
|
|
|
157
152
|
const resolvedApiKey = apiKey && getAiTransportHost().resolveSecretSentinel(apiKey);
|
|
158
153
|
return Boolean(resolvedApiKey?.trim()) && !isAnthropicOAuthApiKey(resolvedApiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
|
|
159
154
|
}
|
|
160
|
-
function resolveBaseUrlHostname(baseUrl) {
|
|
161
|
-
return URL.parse(baseUrl)?.hostname;
|
|
162
|
-
}
|
|
163
155
|
function isLongTtlEligibleEndpoint(baseUrl) {
|
|
164
156
|
if (typeof baseUrl !== "string") return false;
|
|
165
|
-
const hostname =
|
|
157
|
+
const hostname = URL.parse(baseUrl)?.hostname;
|
|
166
158
|
if (!hostname) return false;
|
|
167
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");
|
|
168
160
|
}
|
|
@@ -189,8 +181,7 @@ function buildAnthropicSystemBlocks(systemPrompt, isOAuthToken, cacheControl, cl
|
|
|
189
181
|
type: "text",
|
|
190
182
|
text: sanitizeSurrogates(systemPrompt)
|
|
191
183
|
}] : [];
|
|
192
|
-
|
|
193
|
-
else stripAnthropicSystemPromptBoundary(blocks);
|
|
184
|
+
normalizeAnthropicSystemBlocks(blocks, cacheControl);
|
|
194
185
|
if (systemPrompt && blocks.length === 0) blocks.push({
|
|
195
186
|
type: "text",
|
|
196
187
|
text: ""
|
|
@@ -218,7 +209,7 @@ function applyAnthropicRequestCacheControl(payload, cacheControl, supportsCacheC
|
|
|
218
209
|
const usedMarkers = countAnthropicCacheControlMarkers(payload.system) + countAnthropicCacheControlMarkers(payload.tools);
|
|
219
210
|
applyAnthropicCacheControlToMessages(payload.messages, cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
|
|
220
211
|
}
|
|
221
|
-
function
|
|
212
|
+
function normalizeAnthropicSystemBlocks(system, cacheControl) {
|
|
222
213
|
if (!Array.isArray(system)) return;
|
|
223
214
|
const normalizedBlocks = [];
|
|
224
215
|
for (const block of system) {
|
|
@@ -232,6 +223,10 @@ function applyAnthropicCacheControlToSystem(system, cacheControl) {
|
|
|
232
223
|
normalizedBlocks.push(block);
|
|
233
224
|
continue;
|
|
234
225
|
}
|
|
226
|
+
if (!cacheControl) {
|
|
227
|
+
record.text = stripSystemPromptCacheBoundary(blockText);
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
235
230
|
const text = stripSystemPromptRelocatableBoundary(blockText);
|
|
236
231
|
record.text = text;
|
|
237
232
|
const split = splitSystemPromptCacheBoundary(text);
|
|
@@ -251,15 +246,7 @@ function applyAnthropicCacheControlToSystem(system, cacheControl) {
|
|
|
251
246
|
text: split.dynamicSuffix
|
|
252
247
|
});
|
|
253
248
|
}
|
|
254
|
-
system.splice(0, system.length, ...normalizedBlocks);
|
|
255
|
-
}
|
|
256
|
-
function stripAnthropicSystemPromptBoundary(system) {
|
|
257
|
-
if (!Array.isArray(system)) return;
|
|
258
|
-
for (const block of system) {
|
|
259
|
-
if (!block || typeof block !== "object") continue;
|
|
260
|
-
const record = block;
|
|
261
|
-
if (record.type === "text" && typeof record.text === "string") record.text = stripSystemPromptCacheBoundary(record.text);
|
|
262
|
-
}
|
|
249
|
+
if (cacheControl) system.splice(0, system.length, ...normalizedBlocks);
|
|
263
250
|
}
|
|
264
251
|
/** Apply one shared deepest-stable-message cache breakpoint policy. */
|
|
265
252
|
function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimit, cacheBreakpointOptOutMessageIndexes) {
|
|
@@ -271,22 +258,13 @@ function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimi
|
|
|
271
258
|
const record = message;
|
|
272
259
|
if (record.role !== "user" || cacheBreakpointOptOutMessageIndexes.has(i)) continue;
|
|
273
260
|
const content = record.content;
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
text: content,
|
|
282
|
-
cache_control: cacheControl
|
|
283
|
-
}];
|
|
284
|
-
if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
if (!Array.isArray(content)) continue;
|
|
288
|
-
for (let j = content.length - 1; j >= 0; j--) {
|
|
289
|
-
const block = content[j];
|
|
261
|
+
const blocks = typeof content === "string" ? [{
|
|
262
|
+
type: "text",
|
|
263
|
+
text: content
|
|
264
|
+
}] : content;
|
|
265
|
+
if (!Array.isArray(blocks)) continue;
|
|
266
|
+
for (let j = blocks.length - 1; j >= 0; j--) {
|
|
267
|
+
const block = blocks[j];
|
|
290
268
|
if (!block || typeof block !== "object") continue;
|
|
291
269
|
const blockRecord = block;
|
|
292
270
|
if (blockRecord.type === "text" || blockRecord.type === "image") {
|
|
@@ -295,6 +273,7 @@ function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimi
|
|
|
295
273
|
return;
|
|
296
274
|
}
|
|
297
275
|
blockRecord.cache_control = cacheControl;
|
|
276
|
+
if (typeof content === "string") record.content = blocks;
|
|
298
277
|
if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
|
|
299
278
|
return;
|
|
300
279
|
}
|
|
@@ -417,8 +396,7 @@ function logAnthropicContextEdits(event) {
|
|
|
417
396
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
418
397
|
function applyAnthropicPayloadPolicyToParams(payloadObj, policy, cacheBreakpointOptOutMessageIndexes) {
|
|
419
398
|
if (policy.allowsServiceTier && policy.serviceTier !== void 0 && payloadObj.service_tier === void 0) payloadObj.service_tier = policy.serviceTier;
|
|
420
|
-
|
|
421
|
-
else stripAnthropicSystemPromptBoundary(payloadObj.system);
|
|
399
|
+
normalizeAnthropicSystemBlocks(payloadObj.system, policy.cacheControl);
|
|
422
400
|
applyAnthropicContextManagementEdits(payloadObj, policy);
|
|
423
401
|
applyAnthropicRequestCacheControl(payloadObj, policy.cacheControl, false, cacheBreakpointOptOutMessageIndexes);
|
|
424
402
|
}
|
|
@@ -628,9 +606,6 @@ function logAnthropicThinkingDrops(transformations) {
|
|
|
628
606
|
const paths = drops.slice(0, 5).map(({ reason, path }) => `${reason} at ${path}`);
|
|
629
607
|
getAiTransportHost().logWarn("anthropic", `replayed thinking dropped: ${drops.length} block(s) (${paths.join(", ")}${drops.length > 5 ? ", …" : ""})`);
|
|
630
608
|
}
|
|
631
|
-
function asReplayMessage(value) {
|
|
632
|
-
return value && typeof value === "object" ? value : void 0;
|
|
633
|
-
}
|
|
634
609
|
/**
|
|
635
610
|
* Anthropic tool results continue the preceding assistant turn. Preserve that
|
|
636
611
|
* turn's signed thinking even when the next request disables new thinking.
|
|
@@ -639,7 +614,7 @@ function findActiveAnthropicToolTurnAssistantIndex(messages) {
|
|
|
639
614
|
const toolResultIds = /* @__PURE__ */ new Set();
|
|
640
615
|
let index = messages.length - 1;
|
|
641
616
|
while (index >= 0) {
|
|
642
|
-
const message =
|
|
617
|
+
const message = asOptionalObjectRecord(messages[index]);
|
|
643
618
|
if (message?.role === "user" && isRecord(message.operatorMessage)) {
|
|
644
619
|
index -= 1;
|
|
645
620
|
continue;
|
|
@@ -649,12 +624,12 @@ function findActiveAnthropicToolTurnAssistantIndex(messages) {
|
|
|
649
624
|
index -= 1;
|
|
650
625
|
}
|
|
651
626
|
if (toolResultIds.size === 0) return -1;
|
|
652
|
-
const assistant =
|
|
627
|
+
const assistant = asOptionalObjectRecord(messages[index]);
|
|
653
628
|
if (assistant?.role !== "assistant" || !Array.isArray(assistant.content)) return -1;
|
|
654
629
|
const toolCallIds = /* @__PURE__ */ new Set();
|
|
655
630
|
for (const block of assistant.content) {
|
|
656
|
-
|
|
657
|
-
|
|
631
|
+
const record = asOptionalObjectRecord(block);
|
|
632
|
+
if (!record) continue;
|
|
658
633
|
if ((record.type === "toolCall" || record.type === "tool_use" || record.type === "function_call") && typeof record.id === "string") toolCallIds.add(record.id);
|
|
659
634
|
}
|
|
660
635
|
return [...toolResultIds].every((toolCallId) => toolCallIds.has(toolCallId)) ? index : -1;
|
|
@@ -804,12 +779,7 @@ function reconcileAnthropicToolChoice(choice, projection) {
|
|
|
804
779
|
if (projection.inputToolCount === 0) return choice;
|
|
805
780
|
if (choice.type === "tool") {
|
|
806
781
|
const requestedName = choice.name;
|
|
807
|
-
const
|
|
808
|
-
if (originalMatch) return {
|
|
809
|
-
...choice,
|
|
810
|
-
name: originalMatch.wireName
|
|
811
|
-
};
|
|
812
|
-
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));
|
|
813
783
|
if (!matchedTool) throw new Error(`Anthropic tool_choice requested unavailable tool "${requestedName}" after schema conversion`);
|
|
814
784
|
return {
|
|
815
785
|
...choice,
|
|
@@ -852,9 +822,6 @@ function resolveAnthropicTransportOptions(model, options, apiKey) {
|
|
|
852
822
|
useModelDefault: resolveClaudeHaiku55ModelIdentity(model) !== void 0 || resolveClaudeSonnet5ModelIdentity(model) !== void 0 || resolveClaudeOpus5ModelIdentity(model) !== void 0
|
|
853
823
|
});
|
|
854
824
|
if (baseMaxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
|
|
855
|
-
const reasoningModelMaxTokens = resolvePositiveAnthropicTokenLimit(model.maxTokens) ?? baseMaxTokens;
|
|
856
|
-
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
857
|
-
const reasoning = options?.reasoning === "off" && mandatoryAdaptiveThinking ? "low" : options?.reasoning;
|
|
858
825
|
const resolved = copyProviderAcceptanceObserver(options, {
|
|
859
826
|
temperature: options?.temperature,
|
|
860
827
|
stop: options?.stop,
|
|
@@ -872,56 +839,71 @@ function resolveAnthropicTransportOptions(model, options, apiKey) {
|
|
|
872
839
|
toolChoice: options?.toolChoice,
|
|
873
840
|
thinkingBudgets: options?.thinkingBudgets,
|
|
874
841
|
thinkingDisplay: options?.thinkingDisplay,
|
|
875
|
-
reasoning,
|
|
842
|
+
reasoning: options?.reasoning,
|
|
876
843
|
anthropicServerCompaction: options?.anthropicServerCompaction,
|
|
877
844
|
anthropicCompactThreshold: options?.anthropicCompactThreshold,
|
|
878
845
|
cacheTtlPruning: options?.cacheTtlPruning,
|
|
879
846
|
...options?.authProfileId ? { authProfileId: options.authProfileId } : {}
|
|
880
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;
|
|
881
855
|
if (reasoning === "off") {
|
|
882
856
|
resolved.thinkingEnabled = false;
|
|
883
|
-
return
|
|
857
|
+
return reasoning;
|
|
884
858
|
}
|
|
885
|
-
if (!reasoning) {
|
|
886
|
-
resolved.thinkingEnabled = defaultsClaudeAdaptiveThinking(model);
|
|
859
|
+
if (!reasoning || supportsClaudeAdaptiveThinking(model) || !managed && defaultsClaudeAdaptiveThinking(model)) {
|
|
860
|
+
resolved.thinkingEnabled = Boolean(reasoning) || defaultsClaudeAdaptiveThinking(model);
|
|
887
861
|
if (resolved.thinkingEnabled) resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
888
|
-
return
|
|
889
|
-
}
|
|
890
|
-
if (supportsClaudeAdaptiveThinking(model)) {
|
|
891
|
-
resolved.thinkingEnabled = true;
|
|
892
|
-
resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
893
|
-
return resolved;
|
|
862
|
+
return reasoning;
|
|
894
863
|
}
|
|
895
|
-
const adjusted = adjustMaxTokensForThinking(
|
|
864
|
+
const adjusted = adjustMaxTokensForThinking(resolved.maxTokens, (managed ? resolvePositiveAnthropicTokenLimit(model.maxTokens) : model.maxTokens) ?? resolved.maxTokens, managed && reasoning === "max" ? "high" : reasoning, options?.thinkingBudgets);
|
|
896
865
|
const thinkingEnabled = adjusted.thinkingBudget >= 1024;
|
|
897
|
-
resolved.maxTokens = adjusted.maxTokens;
|
|
866
|
+
if (managed || thinkingEnabled) resolved.maxTokens = adjusted.maxTokens;
|
|
898
867
|
resolved.thinkingEnabled = thinkingEnabled;
|
|
899
868
|
resolved.thinkingBudgetTokens = thinkingEnabled ? adjusted.thinkingBudget : void 0;
|
|
900
|
-
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;
|
|
901
884
|
}
|
|
902
885
|
//#endregion
|
|
903
886
|
//#region packages/ai/src/transports/anthropic-messages.ts
|
|
904
887
|
const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
905
888
|
async function convertContentBlocks(content, model, imageBudget, profile, isError) {
|
|
906
889
|
const mediaPlaceholder = describeToolResultMediaPlaceholder(content);
|
|
907
|
-
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
|
+
}
|
|
908
894
|
const blocks = [];
|
|
909
|
-
let hasTextBlock = false;
|
|
910
895
|
for (const block of content) {
|
|
911
896
|
const record = asOptionalObjectRecord(block);
|
|
912
897
|
if (!record) continue;
|
|
913
898
|
const blockText = extractToolResultBlockText(block);
|
|
914
|
-
if (blockText) {
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
});
|
|
919
|
-
hasTextBlock = true;
|
|
920
|
-
}
|
|
899
|
+
if (blockText) blocks.push({
|
|
900
|
+
type: "text",
|
|
901
|
+
text: blockText
|
|
902
|
+
});
|
|
921
903
|
if (!isImageWithMediaPayload(record)) continue;
|
|
922
904
|
const [normalizedImage] = await normalizeAnthropicInlineContent([{
|
|
923
905
|
type: "image",
|
|
924
|
-
data:
|
|
906
|
+
data: record.data,
|
|
925
907
|
mimeType: typeof record.mimeType === "string" ? record.mimeType : profile === "provider" ? "image/jpeg" : "image/png"
|
|
926
908
|
}], imageBudget);
|
|
927
909
|
if (normalizedImage?.type !== "image") continue;
|
|
@@ -934,7 +916,7 @@ async function convertContentBlocks(content, model, imageBudget, profile, isErro
|
|
|
934
916
|
}
|
|
935
917
|
});
|
|
936
918
|
}
|
|
937
|
-
if (!
|
|
919
|
+
if (!blocks.some((block) => block.type === "text")) blocks.unshift({
|
|
938
920
|
type: "text",
|
|
939
921
|
text: mediaPlaceholder ?? "(see attached image)"
|
|
940
922
|
});
|
|
@@ -1092,12 +1074,17 @@ async function convertAnthropicMessages(transformedMessages, model, isOAuthToken
|
|
|
1092
1074
|
return params;
|
|
1093
1075
|
}
|
|
1094
1076
|
/** Shared generation contract, after each entry point resolves its defaults and tool policy. */
|
|
1095
|
-
function buildAnthropicGenerationParams({ model, options,
|
|
1077
|
+
function buildAnthropicGenerationParams({ model, options, toolProjection, eagerToolInputStreaming, profile }) {
|
|
1096
1078
|
const params = {};
|
|
1097
1079
|
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
1098
1080
|
if (options?.temperature !== void 0 && !options?.thinkingEnabled && !supportsClaudeNativeXhighEffort(model)) params.temperature = options.temperature;
|
|
1099
1081
|
if (options?.stop !== void 0 && options.stop.length > 0) params.stop_sequences = options.stop;
|
|
1100
|
-
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
|
+
}));
|
|
1101
1088
|
if (mandatoryAdaptiveThinking || model.reasoning || supportsClaudeAdaptiveThinking(model)) {
|
|
1102
1089
|
if (mandatoryAdaptiveThinking || options?.thinkingEnabled) {
|
|
1103
1090
|
const display = options?.thinkingDisplay ?? "summarized";
|
|
@@ -1126,21 +1113,6 @@ function buildAnthropicGenerationParams({ model, options, tools, toolProjection,
|
|
|
1126
1113
|
}
|
|
1127
1114
|
return params;
|
|
1128
1115
|
}
|
|
1129
|
-
function convertAnthropicTools(tools, isOAuthTokenLocal, supportsEagerToolInputStreaming = false) {
|
|
1130
|
-
const projection = projectAnthropicTools(tools, (name) => isOAuthTokenLocal ? toClaudeCodeToolName(name) : name);
|
|
1131
|
-
return {
|
|
1132
|
-
projection,
|
|
1133
|
-
tools: projection.tools.map((tool) => {
|
|
1134
|
-
const projected = {
|
|
1135
|
-
name: tool.wireName,
|
|
1136
|
-
description: tool.description,
|
|
1137
|
-
input_schema: tool.inputSchema
|
|
1138
|
-
};
|
|
1139
|
-
if (supportsEagerToolInputStreaming) projected.eager_input_streaming = true;
|
|
1140
|
-
return projected;
|
|
1141
|
-
})
|
|
1142
|
-
};
|
|
1143
|
-
}
|
|
1144
1116
|
/** Assemble both public Messages routes without changing their replay/default profiles. */
|
|
1145
1117
|
async function buildAnthropicRequest(model, context, options, profile, isOAuthToken, serverSideFallback, claudeCodeVersion) {
|
|
1146
1118
|
const managed = profile === "transport";
|
|
@@ -1152,8 +1124,13 @@ async function buildAnthropicRequest(model, context, options, profile, isOAuthTo
|
|
|
1152
1124
|
if (managed && maxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
|
|
1153
1125
|
const { cacheControl, supportsCacheControlOnTools } = resolveAnthropicCacheOptions(model, options?.cacheRetention);
|
|
1154
1126
|
const system = buildAnthropicSystemBlocks(context.systemPrompt, isOAuthToken, cacheControl, claudeCodeVersion);
|
|
1155
|
-
|
|
1156
|
-
|
|
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
|
+
}
|
|
1157
1134
|
const replayPlan = buildAnthropicReplayPlan(context.messages, model, {
|
|
1158
1135
|
enabled: !isOAuthToken && options?.anthropicServerCompaction === true,
|
|
1159
1136
|
authProfileId: options?.authProfileId,
|
|
@@ -1183,8 +1160,8 @@ async function buildAnthropicRequest(model, context, options, profile, isOAuthTo
|
|
|
1183
1160
|
Object.assign(params, buildAnthropicGenerationParams({
|
|
1184
1161
|
model,
|
|
1185
1162
|
options,
|
|
1186
|
-
tools: convertedTools?.tools,
|
|
1187
1163
|
toolProjection,
|
|
1164
|
+
eagerToolInputStreaming,
|
|
1188
1165
|
profile
|
|
1189
1166
|
}));
|
|
1190
1167
|
applyAnthropicRequestCacheControl(params, cacheControl, supportsCacheControlOnTools, cacheBreakpointOptOutMessageIndexes);
|
|
@@ -1210,19 +1187,12 @@ async function prepareAnthropicRequest(initialParams, model, options, directApiK
|
|
|
1210
1187
|
}
|
|
1211
1188
|
//#endregion
|
|
1212
1189
|
//#region packages/ai/src/providers/anthropic-refusal.ts
|
|
1213
|
-
function readAnthropicRefusalDetails(value) {
|
|
1214
|
-
if (!value || typeof value !== "object") return {
|
|
1215
|
-
category: null,
|
|
1216
|
-
explanation: null
|
|
1217
|
-
};
|
|
1218
|
-
const details = value;
|
|
1219
|
-
return {
|
|
1220
|
-
category: normalizeNullableString(details.category),
|
|
1221
|
-
explanation: normalizeNullableString(details.explanation)
|
|
1222
|
-
};
|
|
1223
|
-
}
|
|
1224
1190
|
function applyAnthropicRefusal(output, stopDetails, provider) {
|
|
1225
|
-
const
|
|
1191
|
+
const record = asOptionalObjectRecord(stopDetails);
|
|
1192
|
+
const details = {
|
|
1193
|
+
category: normalizeNullableString(record?.category),
|
|
1194
|
+
explanation: normalizeNullableString(record?.explanation)
|
|
1195
|
+
};
|
|
1226
1196
|
const category = details.category ? ` (category: ${details.category})` : "";
|
|
1227
1197
|
const explanation = details.explanation ? `: ${details.explanation}` : ".";
|
|
1228
1198
|
output.stopReason = "error";
|
|
@@ -1239,6 +1209,41 @@ function applyAnthropicRefusal(output, stopDetails, provider) {
|
|
|
1239
1209
|
}
|
|
1240
1210
|
//#endregion
|
|
1241
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
|
+
}
|
|
1242
1247
|
function readAnthropicUsageTokenCount(value) {
|
|
1243
1248
|
return asNonNegativeFiniteNumber(value);
|
|
1244
1249
|
}
|
|
@@ -1266,20 +1271,14 @@ function readAnthropicPromptUsageSnapshot(usage) {
|
|
|
1266
1271
|
function readLastAnthropicIterationUsage(usage) {
|
|
1267
1272
|
if (usage.iterations == null) return { state: "absent" };
|
|
1268
1273
|
if (!Array.isArray(usage.iterations) || usage.iterations.length === 0) return { state: "invalid" };
|
|
1269
|
-
const iteration = usage.iterations.at(-1);
|
|
1270
|
-
if (!iteration
|
|
1271
|
-
const
|
|
1272
|
-
const input = readAnthropicUsageTokenCount(record.input_tokens);
|
|
1273
|
-
const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
|
|
1274
|
-
const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
|
|
1275
|
-
const outputTokens = readAnthropicUsageTokenCount(record.output_tokens);
|
|
1276
|
-
if (input === void 0 || cacheRead === void 0 || cacheWrite === void 0 || outputTokens === void 0) return { state: "invalid" };
|
|
1277
|
-
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;
|
|
1278
1277
|
return {
|
|
1279
1278
|
state: "valid",
|
|
1280
1279
|
usage: {
|
|
1281
1280
|
contextPromptTokens,
|
|
1282
|
-
totalTokens: contextPromptTokens +
|
|
1281
|
+
totalTokens: contextPromptTokens + iteration.output
|
|
1283
1282
|
}
|
|
1284
1283
|
};
|
|
1285
1284
|
}
|
|
@@ -1294,19 +1293,10 @@ function readAnthropicCompactionBilledUsage(iterations) {
|
|
|
1294
1293
|
cacheWrite1h: 0
|
|
1295
1294
|
};
|
|
1296
1295
|
for (const iteration of iterations) {
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
const
|
|
1301
|
-
const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
|
|
1302
|
-
const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
|
|
1303
|
-
if (input === void 0 || output === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
|
|
1304
|
-
sawCompaction ||= record.type === "compaction";
|
|
1305
|
-
billed.input += input;
|
|
1306
|
-
billed.output += output;
|
|
1307
|
-
billed.cacheRead += cacheRead;
|
|
1308
|
-
billed.cacheWrite += cacheWrite;
|
|
1309
|
-
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];
|
|
1310
1300
|
}
|
|
1311
1301
|
return sawCompaction ? billed : void 0;
|
|
1312
1302
|
}
|
|
@@ -1314,18 +1304,9 @@ function readAnthropicCompactionBilledUsage(iterations) {
|
|
|
1314
1304
|
function applyAnthropicMessageStartUsage(target, payload) {
|
|
1315
1305
|
const promptUsage = readAnthropicPromptUsageSnapshot(payload);
|
|
1316
1306
|
const promptTokens = promptUsage ? promptUsage.input + promptUsage.cacheRead + promptUsage.cacheWrite : 0;
|
|
1317
|
-
const
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
if (outputTokens !== void 0) target.output = outputTokens;
|
|
1321
|
-
const cacheReadTokens = payload.cache_read_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_read_input_tokens);
|
|
1322
|
-
if (cacheReadTokens !== void 0) target.cacheRead = cacheReadTokens;
|
|
1323
|
-
const cacheWriteTokens = payload.cache_creation_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_creation_input_tokens);
|
|
1324
|
-
if (cacheWriteTokens !== void 0) target.cacheWrite = cacheWriteTokens;
|
|
1325
|
-
const { cacheWrite1h } = readAnthropicCacheWriteUsage(payload);
|
|
1326
|
-
if (cacheWrite1h !== void 0) target.cacheWrite1h = cacheWrite1h;
|
|
1327
|
-
target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
|
|
1328
|
-
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 = {
|
|
1329
1310
|
state: "available",
|
|
1330
1311
|
promptTokens,
|
|
1331
1312
|
totalTokens: promptTokens + target.output
|
|
@@ -1336,28 +1317,8 @@ function applyAnthropicMessageStartUsage(target, payload) {
|
|
|
1336
1317
|
function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage) {
|
|
1337
1318
|
if (!usage) return;
|
|
1338
1319
|
const billedIterations = readAnthropicCompactionBilledUsage(usage.iterations);
|
|
1339
|
-
const
|
|
1340
|
-
|
|
1341
|
-
const cacheReadTokens = readAnthropicUsageTokenCount(usage.cache_read_input_tokens);
|
|
1342
|
-
const cacheWriteTokens = readAnthropicUsageTokenCount(usage.cache_creation_input_tokens);
|
|
1343
|
-
const resolved = billedIterations ?? {
|
|
1344
|
-
input: inputTokens,
|
|
1345
|
-
output: outputTokens,
|
|
1346
|
-
cacheRead: cacheReadTokens,
|
|
1347
|
-
cacheWrite: cacheWriteTokens,
|
|
1348
|
-
cacheWrite1h: readAnthropicCacheWriteUsage(usage).cacheWrite1h
|
|
1349
|
-
};
|
|
1350
|
-
for (const key of [
|
|
1351
|
-
"input",
|
|
1352
|
-
"output",
|
|
1353
|
-
"cacheRead",
|
|
1354
|
-
"cacheWrite",
|
|
1355
|
-
"cacheWrite1h"
|
|
1356
|
-
]) {
|
|
1357
|
-
const value = resolved[key];
|
|
1358
|
-
if (value !== void 0) target[key] = value;
|
|
1359
|
-
}
|
|
1360
|
-
target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
|
|
1320
|
+
const reported = readAnthropicBilledUsage(usage);
|
|
1321
|
+
applyAnthropicBilledUsage(target, billedIterations ?? reported);
|
|
1361
1322
|
const iterationUsage = readLastAnthropicIterationUsage(usage);
|
|
1362
1323
|
if (iterationUsage.state === "valid") target.contextUsage = {
|
|
1363
1324
|
state: "available",
|
|
@@ -1365,7 +1326,7 @@ function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage)
|
|
|
1365
1326
|
totalTokens: iterationUsage.usage.totalTokens
|
|
1366
1327
|
};
|
|
1367
1328
|
else if (iterationUsage.state === "invalid") target.contextUsage = { state: "unavailable" };
|
|
1368
|
-
else if (
|
|
1329
|
+
else if (reported.output !== void 0 && (messageStartPromptUsage !== void 0 || (reported.cacheRead !== void 0 || reported.cacheWrite !== void 0) && readAnthropicPromptUsageSnapshot(usage) !== void 0)) {
|
|
1369
1330
|
const promptTokens = target.input + target.cacheRead + target.cacheWrite;
|
|
1370
1331
|
target.contextUsage = {
|
|
1371
1332
|
state: "available",
|
|
@@ -1401,14 +1362,44 @@ async function consumeAnthropicStream(params) {
|
|
|
1401
1362
|
let sawMessageStop = false;
|
|
1402
1363
|
let sawStopReason = false;
|
|
1403
1364
|
const pendingTextEnds = [];
|
|
1404
|
-
const flushPendingTextEnds = () => {
|
|
1365
|
+
const flushPendingTextEnds = (classifyToolTurn = false) => {
|
|
1366
|
+
if (managed && classifyToolTurn && (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall"))) tagPendingCommentaryText(output.content);
|
|
1405
1367
|
for (const event of pendingTextEnds) eventSink.push(event);
|
|
1406
1368
|
pendingTextEnds.length = 0;
|
|
1407
1369
|
};
|
|
1408
|
-
const
|
|
1409
|
-
|
|
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);
|
|
1410
1378
|
else eventSink.push(event);
|
|
1411
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
|
+
};
|
|
1412
1403
|
const eventIndexKey = (eventIndex) => typeof eventIndex === "number" ? eventIndex : -1;
|
|
1413
1404
|
const appendReasoningContentDelta = (kind, eventIndex, rawText) => {
|
|
1414
1405
|
if (typeof rawText !== "string") return false;
|
|
@@ -1430,22 +1421,10 @@ async function consumeAnthropicStream(params) {
|
|
|
1430
1421
|
output.content.push(block);
|
|
1431
1422
|
contentIndex = output.content.length - 1;
|
|
1432
1423
|
indexes.set(key, contentIndex);
|
|
1433
|
-
|
|
1434
|
-
type: `${kind}_start`,
|
|
1435
|
-
contentIndex,
|
|
1436
|
-
partial: output
|
|
1437
|
-
});
|
|
1424
|
+
emitContentStart(kind, contentIndex);
|
|
1438
1425
|
}
|
|
1439
|
-
if (block.type === "thinking")
|
|
1440
|
-
|
|
1441
|
-
block.thinkingSignature = "reasoning_content";
|
|
1442
|
-
} else if (block.type === "text") block.text += text;
|
|
1443
|
-
eventSink.push({
|
|
1444
|
-
type: `${kind}_delta`,
|
|
1445
|
-
contentIndex,
|
|
1446
|
-
delta: text,
|
|
1447
|
-
partial: output
|
|
1448
|
-
});
|
|
1426
|
+
if (block.type === "thinking") block.thinkingSignature = "reasoning_content";
|
|
1427
|
+
appendContentDelta(block, contentIndex, text);
|
|
1449
1428
|
return true;
|
|
1450
1429
|
};
|
|
1451
1430
|
const finishReasoningContentSidecars = (eventIndex) => {
|
|
@@ -1456,12 +1435,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1456
1435
|
if (contentIndex === void 0) continue;
|
|
1457
1436
|
indexes.delete(key);
|
|
1458
1437
|
const block = output.content[contentIndex];
|
|
1459
|
-
if (block?.type === kind)
|
|
1460
|
-
type: `${kind}_end`,
|
|
1461
|
-
contentIndex,
|
|
1462
|
-
content: block.type === "thinking" ? block.thinking : block.text,
|
|
1463
|
-
partial: output
|
|
1464
|
-
});
|
|
1438
|
+
if (block?.type === kind) emitContentEnd(block, contentIndex, false);
|
|
1465
1439
|
}
|
|
1466
1440
|
};
|
|
1467
1441
|
for await (const rawEvent of iterateModelStream(params.events, options.signal)) {
|
|
@@ -1522,23 +1496,8 @@ async function consumeAnthropicStream(params) {
|
|
|
1522
1496
|
for (const [i, block] of blocks.entries()) {
|
|
1523
1497
|
if (block.type !== "text") continue;
|
|
1524
1498
|
delete block.index;
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
contentIndex: i,
|
|
1528
|
-
partial: output
|
|
1529
|
-
});
|
|
1530
|
-
if (block.text) eventSink.push({
|
|
1531
|
-
type: "text_delta",
|
|
1532
|
-
contentIndex: i,
|
|
1533
|
-
delta: block.text,
|
|
1534
|
-
partial: output
|
|
1535
|
-
});
|
|
1536
|
-
emitTextEnd({
|
|
1537
|
-
type: "text_end",
|
|
1538
|
-
contentIndex: i,
|
|
1539
|
-
content: block.text,
|
|
1540
|
-
partial: output
|
|
1541
|
-
});
|
|
1499
|
+
emitContentStart("text", i, block.text);
|
|
1500
|
+
emitContentEnd(block, i, true);
|
|
1542
1501
|
}
|
|
1543
1502
|
continue;
|
|
1544
1503
|
}
|
|
@@ -1560,22 +1519,11 @@ async function consumeAnthropicStream(params) {
|
|
|
1560
1519
|
redacted: true,
|
|
1561
1520
|
index
|
|
1562
1521
|
};
|
|
1563
|
-
const kind = block.type;
|
|
1564
1522
|
const delta = block.type === "text" ? block.text : block.redacted ? "" : block.thinking;
|
|
1565
1523
|
output.content.push(block);
|
|
1566
1524
|
const contentIndex = output.content.length - 1;
|
|
1567
1525
|
blockIndexes.set(index, contentIndex);
|
|
1568
|
-
|
|
1569
|
-
type: `${kind}_start`,
|
|
1570
|
-
contentIndex,
|
|
1571
|
-
partial: output
|
|
1572
|
-
});
|
|
1573
|
-
if (delta.length > 0) eventSink.push({
|
|
1574
|
-
type: `${kind}_delta`,
|
|
1575
|
-
contentIndex,
|
|
1576
|
-
delta,
|
|
1577
|
-
partial: output
|
|
1578
|
-
});
|
|
1526
|
+
emitContentStart(block.type, contentIndex, delta);
|
|
1579
1527
|
continue;
|
|
1580
1528
|
}
|
|
1581
1529
|
if (contentBlock?.type === "tool_use") {
|
|
@@ -1615,13 +1563,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1615
1563
|
const text = sanitizeTransportPayloadText(delta.content);
|
|
1616
1564
|
if (text.length > 0) {
|
|
1617
1565
|
if (block?.type === "text" && index !== void 0) {
|
|
1618
|
-
block
|
|
1619
|
-
eventSink.push({
|
|
1620
|
-
type: "text_delta",
|
|
1621
|
-
contentIndex: index,
|
|
1622
|
-
delta: text,
|
|
1623
|
-
partial: output
|
|
1624
|
-
});
|
|
1566
|
+
appendContentDelta(block, index, text);
|
|
1625
1567
|
appendedContent = true;
|
|
1626
1568
|
} else appendedContent = appendReasoningContentDelta("text", event.index, text);
|
|
1627
1569
|
}
|
|
@@ -1637,31 +1579,15 @@ async function consumeAnthropicStream(params) {
|
|
|
1637
1579
|
output.content.push(block);
|
|
1638
1580
|
index = output.content.length - 1;
|
|
1639
1581
|
if (typeof event.index === "number") blockIndexes.set(event.index, index);
|
|
1640
|
-
|
|
1641
|
-
type: "text_start",
|
|
1642
|
-
contentIndex: index,
|
|
1643
|
-
partial: output
|
|
1644
|
-
});
|
|
1582
|
+
emitContentStart("text", index);
|
|
1645
1583
|
}
|
|
1646
1584
|
if (index === void 0) continue;
|
|
1647
1585
|
if (block?.type === "text" && delta?.type === "text_delta" && typeof delta.text === "string") {
|
|
1648
|
-
block
|
|
1649
|
-
eventSink.push({
|
|
1650
|
-
type: "text_delta",
|
|
1651
|
-
contentIndex: index,
|
|
1652
|
-
delta: delta.text,
|
|
1653
|
-
partial: output
|
|
1654
|
-
});
|
|
1586
|
+
appendContentDelta(block, index, delta.text);
|
|
1655
1587
|
continue;
|
|
1656
1588
|
}
|
|
1657
1589
|
if (block?.type === "thinking" && delta?.type === "thinking_delta" && typeof delta.thinking === "string") {
|
|
1658
|
-
|
|
1659
|
-
eventSink.push({
|
|
1660
|
-
type: "thinking_delta",
|
|
1661
|
-
contentIndex: index,
|
|
1662
|
-
delta: delta.thinking,
|
|
1663
|
-
partial: output
|
|
1664
|
-
});
|
|
1590
|
+
appendContentDelta(block, index, delta.thinking);
|
|
1665
1591
|
continue;
|
|
1666
1592
|
}
|
|
1667
1593
|
if (block?.type === "toolCall" && delta?.type === "input_json_delta" && typeof delta.partial_json === "string") {
|
|
@@ -1699,24 +1625,12 @@ async function consumeAnthropicStream(params) {
|
|
|
1699
1625
|
}
|
|
1700
1626
|
blockIndexes.delete(eventIndex);
|
|
1701
1627
|
delete block.index;
|
|
1702
|
-
if (block.type === "
|
|
1703
|
-
|
|
1704
|
-
contentIndex: index,
|
|
1705
|
-
content: block.text,
|
|
1706
|
-
partial: output
|
|
1707
|
-
});
|
|
1708
|
-
else if (block.type === "thinking") {
|
|
1709
|
-
if (pendingSignature !== void 0) block.thinkingSignature = pendingSignature;
|
|
1710
|
-
eventSink.push({
|
|
1711
|
-
type: "thinking_end",
|
|
1712
|
-
contentIndex: index,
|
|
1713
|
-
content: block.thinking,
|
|
1714
|
-
partial: output
|
|
1715
|
-
});
|
|
1716
|
-
} 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({
|
|
1717
1630
|
block,
|
|
1718
1631
|
contentIndex: index
|
|
1719
1632
|
});
|
|
1633
|
+
else emitContentEnd(block, index, true);
|
|
1720
1634
|
finishReasoningContentSidecars(event.index);
|
|
1721
1635
|
continue;
|
|
1722
1636
|
}
|
|
@@ -1731,8 +1645,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1731
1645
|
}
|
|
1732
1646
|
applyAnthropicMessageDeltaUsage(output.usage, usage, messageStartPromptUsage);
|
|
1733
1647
|
calculateCost(costModel, output.usage);
|
|
1734
|
-
|
|
1735
|
-
flushPendingTextEnds();
|
|
1648
|
+
flushPendingTextEnds(true);
|
|
1736
1649
|
}
|
|
1737
1650
|
}
|
|
1738
1651
|
if ((isDirectAnthropicModel(model) || !managed && refusalBuffer) && !sawMessageStop) throw new Error("Anthropic stream ended before message_stop");
|
|
@@ -1751,11 +1664,10 @@ async function consumeAnthropicStream(params) {
|
|
|
1751
1664
|
});
|
|
1752
1665
|
}
|
|
1753
1666
|
refusalBuffer?.flush();
|
|
1754
|
-
|
|
1755
|
-
flushPendingTextEnds();
|
|
1667
|
+
flushPendingTextEnds(true);
|
|
1756
1668
|
} finally {
|
|
1757
1669
|
logAnthropicThinkingDrops(inputTransformations);
|
|
1758
1670
|
}
|
|
1759
1671
|
}
|
|
1760
1672
|
//#endregion
|
|
1761
|
-
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 };
|