@openclaw/ai 2026.9.8 → 2026.10.1-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 +4 -1
- package/dist/{anthropic-CTXJlfnS.mjs → anthropic-CZv6XFER.mjs} +52 -147
- package/dist/{anthropic-payload-policy-NIPVYXPq.d.mts → anthropic-payload-policy-BhCcgNdm.d.mts} +7 -4
- package/dist/{anthropic-stream-reducer-DrsBCsFY.mjs → anthropic-stream-reducer-BQ1tjd7x.mjs} +369 -233
- package/dist/{api-registry-D-nbmSqr.d.mts → api-registry-DpU0Rj-I.d.mts} +4 -4
- package/dist/{provider-transcript-transform-DjA5Fk2P.mjs → assistant-output-OLm-Oln0.mjs} +17 -17
- package/dist/{assistant-text-phase-C20rxWwP.mjs → assistant-text-phase-Bl1aDCB7.mjs} +2 -8
- package/dist/{azure-openai-responses-BBxv4QVI.mjs → azure-openai-responses-DH7bd3QT.mjs} +12 -17
- package/dist/{azure-openai-responses-client-compat-C7K7QfUE.mjs → azure-openai-responses-client-compat-D0dvJMbZ.mjs} +3 -10
- package/dist/{base64-C1PsYcRQ.mjs → base64-C8CdcsjU.mjs} +3 -45
- package/dist/cloudflare-5qSGvIUH.mjs +39 -0
- package/dist/{diagnostics-CJaB6g8J.mjs → diagnostics-Caj-4RBA.mjs} +19 -37
- package/dist/diagnostics.d.mts +4 -4
- package/dist/diagnostics.mjs +2 -2
- package/dist/{event-stream-vd9UnCAU.mjs → event-stream-C5AbCAaZ.mjs} +29 -1
- package/dist/event-stream-DqDQWqiy.d.mts +1 -0
- package/dist/{event-stream-DOPFYVqs.d.mts → event-stream-DrRM_90c.d.mts} +1 -1
- package/dist/event-stream.d.mts +2 -2
- package/dist/event-stream.mjs +1 -1
- package/dist/{github-copilot-request-facts-BTEBMeOv.mjs → github-copilot-request-facts-DEE6gcB9.mjs} +3 -1
- package/dist/{google-B3HEK0Ag.mjs → google-1WLOtb8w.mjs} +12 -9
- package/dist/{google-interactions-vMlEfPAA.mjs → google-interactions-wrnE6MLw.mjs} +39 -49
- package/dist/{google-messages-Dz2dAnGo.mjs → google-messages-BwPPycw6.mjs} +11 -17
- package/dist/{google-shared-B58i3c8H.mjs → google-shared-Eql1OMPU.mjs} +5 -6
- package/dist/{google-vertex-DYFXzEbh.mjs → google-vertex-0LrlRoO3.mjs} +11 -11
- package/dist/{host-rxlNaBwm.d.mts → host-BinBeGcQ.d.mts} +12 -4
- package/dist/{host-DwBmNMCC.mjs → host-DCW52iHe.mjs} +57 -9
- package/dist/{host-policy-wK-ay_RK.mjs → host-policy-B3jc4Rpn.mjs} +9 -2
- package/dist/{index-D6KsSE74.d.mts → index-a8e_oKnu.d.mts} +7 -5
- package/dist/index.d.mts +23 -7
- package/dist/index.mjs +7 -7
- package/dist/internal/anthropic.d.mts +6 -6
- package/dist/internal/anthropic.mjs +5 -5
- package/dist/internal/google-model-family.mjs +1 -1
- 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 +1 -1
- package/dist/internal/openai-responses-payload-policy.mjs +2 -2
- package/dist/internal/openai.d.mts +14 -10
- package/dist/internal/openai.mjs +12 -12
- package/dist/internal/runtime.d.mts +18 -9
- package/dist/internal/runtime.mjs +9 -9
- package/dist/internal/shared.d.mts +6 -4
- package/dist/internal/shared.mjs +6 -6
- package/dist/internal/tool-schema.mjs +1 -1
- package/dist/json-schema-Bp07Qui3.mjs +22 -0
- package/dist/{llm-request-activity-BjtkplhG.mjs → llm-request-activity-D8f_oxHj.mjs} +1 -2
- package/dist/{mistral-7wDWcjfF.mjs → mistral-DSt4vQAO.mjs} +18 -29
- package/dist/number-coercion-V8-BhME4.mjs +47 -0
- package/dist/{openai-chatgpt-responses-Bc7NZrQR.mjs → openai-chatgpt-responses-BC2x0Oqx.mjs} +215 -164
- package/dist/openai-completions-DNyHnXKg.mjs +25 -0
- package/dist/{openai-completions-compat-qJjmqQ71.d.mts → openai-completions-compat-BYdYpXhU.d.mts} +3 -3
- package/dist/{openai-completions-compat-BJf57ZxN.mjs → openai-completions-compat-BdmlhD4u.mjs} +30 -23
- package/dist/{openai-completions-stream-dju_5F3P.mjs → openai-completions-transport-DWM2mQDB.mjs} +389 -76
- package/dist/{openai-prompt-cache-CouErLh7.d.mts → openai-prompt-cache-45-AH_xv.d.mts} +2 -2
- package/dist/{openai-prompt-cache-BTMs80_O.mjs → openai-prompt-cache-I8YQTzf8.mjs} +1 -1
- package/dist/{openai-responses-compaction-replay-DiYn-gFM.d.mts → openai-responses-compaction-replay-BClApUIf.d.mts} +4 -4
- package/dist/{openai-responses-compaction-window-CYGCjAQz.mjs → openai-responses-compaction-window-m878aVpU.mjs} +25 -83
- package/dist/{openai-reasoning-effort-C_0p3psG.mjs → openai-responses-contracts-BKu8O7l4.mjs} +103 -4
- package/dist/{openai-responses-contracts-D1iZ0njF.d.mts → openai-responses-contracts-Br5IIw_h.d.mts} +16 -5
- package/dist/openai-responses-prompt-observer-internal-CCg7B6l4.mjs +32 -0
- package/dist/{openai-responses-shared-CIEHiKx7.mjs → openai-responses-shared-C7oVyjED.mjs} +297 -312
- package/dist/{openai-responses-terminal-usage-BE7lf7nW.d.mts → openai-responses-terminal-usage-DgImcyJu.d.mts} +2 -2
- package/dist/{openai-responses-DbPe3-Py.mjs → openai-responses-uZ5oWSp7.mjs} +12 -21
- package/dist/{openai-tool-schema-vsl-cE-n.mjs → openai-tool-schema-DYNePiF8.mjs} +66 -36
- package/dist/{openai-transport-params-DyFJ4cgR.mjs → openai-transport-params-JFsnIyto.mjs} +25 -30
- package/dist/openai-web-search-tools-Cfu0uLyN.mjs +14 -0
- package/dist/{positive-integer-CjN5OJbH.mjs → positive-integer-CwLi8LUL.mjs} +1 -1
- package/dist/{provider-error-CwT_j2v0.mjs → provider-error-DQBE1bUQ.mjs} +17 -28
- package/dist/{provider-options-Df7ov0K1.d.mts → provider-options-D5ABCV_l.d.mts} +2 -2
- package/dist/{provider-replay-context-Bs5aWE1d.mjs → provider-replay-context-C7Bmi84a.mjs} +6 -2
- package/dist/{provider-transport-turn-state-Dw7OtYUs.mjs → provider-transport-turn-state-_M2kLdUE.mjs} +25 -3
- package/dist/{provider-types-d4XnoZMd.d.mts → provider-types-F8Mqc9Ma.d.mts} +3 -3
- package/dist/provider-types.d.mts +6 -6
- package/dist/providers.d.mts +2 -2
- package/dist/providers.mjs +14 -26
- package/dist/{reasoning-tag-text-partitioner-B6FIIBt6.mjs → reasoning-tag-text-partitioner-Ju9VpUGl.mjs} +23 -37
- package/dist/required-api-key-D4kn3252.mjs +9 -0
- package/dist/sanitize-unicode-CjDG6jJx.d.mts +5 -0
- package/dist/{session-affinity-Bcunsn4I.mjs → session-affinity-DA4WHv1h.mjs} +2 -6
- package/dist/session-resources-D1wgDQ8C.mjs +40 -0
- package/dist/{simple-options-BkDPMuxw.mjs → simple-options-BkKTXxhp.mjs} +7 -11
- package/dist/{src-H4aXcFuX.mjs → src-B0NIt2UK.mjs} +7 -4
- package/dist/stream-DSdzKsDK.mjs +275 -0
- package/dist/{stream-first-event-timeout-B9o0S4SZ.mjs → stream-first-event-timeout-D9ZmL_s9.mjs} +3 -3
- package/dist/{string-coerce-fsri9iCu.mjs → string-coerce-CtgOVOSv.mjs} +5 -1
- package/dist/{string-normalization-J9ZiLfGO.mjs → string-normalization-PN10hf6d.mjs} +1 -1
- package/dist/{transport-utils-CqXzku3R.mjs → text-signature-MLwfi6fq.mjs} +18 -18
- package/dist/{transport-stream-shared-yYhXa4kX.d.mts → transport-stream-shared-BcAKh2XH.d.mts} +6 -19
- package/dist/{transport-stream-shared-BU5pwY6W.mjs → transport-stream-shared-CxXXz_Dd.mjs} +171 -56
- package/dist/transports.d.mts +22 -23
- package/dist/transports.mjs +251 -495
- package/dist/types-BuiEBM3u.d.mts +1 -0
- package/dist/types-CQc6JNG4.mjs +69 -0
- package/dist/{types-CLLR3eB4.d.mts → types-DvzmWW8J.d.mts} +58 -6
- package/dist/types.d.mts +6 -6
- package/dist/types.mjs +5 -5
- package/dist/usage-CJH5D6Ea.mjs +19 -0
- package/dist/{validation-Cat2wQsc.d.mts → validation-B3lQt_VE.d.mts} +1 -1
- package/dist/{validation-BFZc40Ig.mjs → validation-DbqlIfDn.mjs} +15 -30
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +4 -4
- package/dist/cache-retention-0x979a5V.mjs +0 -12
- package/dist/event-stream-CMQfX_yr.d.mts +0 -1
- package/dist/github-copilot-headers-B37TB3rB.mjs +0 -30
- package/dist/openai-completions-CHnwIV-W.mjs +0 -188
- package/dist/openai-provider-client-G8Otsq2Q.mjs +0 -24
- package/dist/openai-responses-contracts-CLxnqS9p.mjs +0 -81
- package/dist/openai-responses-prompt-observer-internal-BX3QTNSv.mjs +0 -198
- package/dist/sanitize-unicode-BZiVbGwK.d.mts +0 -24
- package/dist/session-resources-CkR4WWy1.mjs +0 -21
- package/dist/stream-DDsCBgd-.mjs +0 -68
- package/dist/types-CB-_vNyI.d.mts +0 -1
- package/dist/types-LFWwv0cF.mjs +0 -8
package/dist/{anthropic-stream-reducer-DrsBCsFY.mjs → anthropic-stream-reducer-BQ1tjd7x.mjs}
RENAMED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { E as supportsClaudeNativeXhighEffort, S as supportsClaudeAdaptiveThinking, _ as resolveClaudeOpus55ModelIdentity, b as resolveClaudeSonnet5ModelIdentity, l as bindsClaudeThinkingPrefix, m as resolveClaudeModelIdentity, p as resolveClaudeFable5ModelIdentity, u as requiresClaudeBetweenToolsThinking, v as resolveClaudeOpus5ModelIdentity, w as supportsClaudeInHistorySystemMessages, y as resolveClaudeSonnet55ModelIdentity } from "./src-B0NIt2UK.mjs";
|
|
2
|
+
import { f as hasRuntimeContextMarker } from "./types-CQc6JNG4.mjs";
|
|
3
|
+
import { r as appendAssistantThinking } from "./event-stream-C5AbCAaZ.mjs";
|
|
3
4
|
import { a as asRecord, o as isRecord, r as asOptionalObjectRecord, s as readStringField } from "./record-coerce-CNDovvft.mjs";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import "./
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { C as
|
|
12
|
-
import { t as
|
|
5
|
+
import { i as normalizeOptionalLowercaseString, r as normalizeNullableString } from "./string-coerce-CtgOVOSv.mjs";
|
|
6
|
+
import { n as asNonNegativeFiniteNumber } from "./number-coercion-V8-BhME4.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-DCW52iHe.mjs";
|
|
8
|
+
import "./text-signature-MLwfi6fq.mjs";
|
|
9
|
+
import { n as estimateBase64DecodedBytes } from "./base64-C8CdcsjU.mjs";
|
|
10
|
+
import { a as resolveProviderRequestCapabilities, i as resolveProviderEndpoint } from "./host-policy-B3jc4Rpn.mjs";
|
|
11
|
+
import { i as resolveCacheRetention } from "./cloudflare-5qSGvIUH.mjs";
|
|
12
|
+
import { C as parseJsonObjectPreservingUnsafeIntegers, E as createToolArgumentPreviewSchedule, O as parseStreamingJson, P as sanitizeSurrogates, _ as sanitizeNonEmptyTransportPayloadText, b as transportAbortError, d as iterateModelStream, i as copyProviderAcceptanceObserver, l as finalizeTerminalToolCallArguments, r as coerceTransportToolCallArguments, v as sanitizeTransportPayloadText } from "./transport-stream-shared-CxXXz_Dd.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-BkKTXxhp.mjs";
|
|
14
|
+
import { t as parsePositiveInteger } from "./positive-integer-CwLi8LUL.mjs";
|
|
15
|
+
import { n as isProviderReplayContext, r as providerReplayContextMatches, t as buildProviderReplayContext } from "./provider-replay-context-C7Bmi84a.mjs";
|
|
16
|
+
import { n as transformProviderMessages } from "./assistant-output-OLm-Oln0.mjs";
|
|
13
17
|
import { i as SCHEMA_MAP_KEYS, n as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-Ct8SeBeJ.mjs";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import { t as notifyLlmRequestActivity } from "./llm-request-activity-BjtkplhG.mjs";
|
|
18
|
+
import { i as tagPendingCommentaryText } from "./assistant-text-phase-Bl1aDCB7.mjs";
|
|
19
|
+
import { t as notifyLlmRequestActivity } from "./llm-request-activity-D8f_oxHj.mjs";
|
|
17
20
|
//#region packages/ai/src/providers/anthropic-auth-headers.ts
|
|
18
21
|
/** Inspect resolved credential shape for auth routing without exposing its value. */
|
|
19
22
|
function isAnthropicOAuthApiKey(apiKey) {
|
|
@@ -138,8 +141,8 @@ function resolveAnthropicServerCompactionPlan(model, extraParams, apiKey) {
|
|
|
138
141
|
...enabled ? { threshold: resolveAnthropicCompactThreshold(model.contextWindow, extraParams?.anthropicCompactThreshold) } : {}
|
|
139
142
|
};
|
|
140
143
|
}
|
|
141
|
-
function isDirectAnthropicModel(model) {
|
|
142
|
-
const baseUrl = model.baseUrl?.trim() ||
|
|
144
|
+
function isDirectAnthropicModel(model, env = process.env) {
|
|
145
|
+
const baseUrl = model.baseUrl?.trim() || env.ANTHROPIC_BASE_URL?.trim();
|
|
143
146
|
const endpointModel = baseUrl === model.baseUrl ? model : {
|
|
144
147
|
...model,
|
|
145
148
|
baseUrl
|
|
@@ -147,16 +150,15 @@ function isDirectAnthropicModel(model) {
|
|
|
147
150
|
const endpointClass = resolveProviderEndpoint(endpointModel).endpointClass;
|
|
148
151
|
return normalizeOptionalLowercaseString(model.provider) === "anthropic" && (endpointClass === "anthropic-public" || endpointClass === "default" && (!baseUrl || resolveBaseUrlHostname(baseUrl) === "api.anthropic.com"));
|
|
149
152
|
}
|
|
153
|
+
function supportsAnthropicServerSideFallback(model) {
|
|
154
|
+
return (usesClaudeFable5MessagesContract(model) || resolveClaudeOpus5ModelIdentity(model) !== void 0 || resolveClaudeSonnet55ModelIdentity(model) !== void 0) && isDirectAnthropicModel(model);
|
|
155
|
+
}
|
|
150
156
|
function isAnthropicServerToolClearingEnabled(model, apiKey) {
|
|
151
157
|
const resolvedApiKey = apiKey && getAiTransportHost().resolveSecretSentinel(apiKey);
|
|
152
158
|
return Boolean(resolvedApiKey?.trim()) && !isAnthropicOAuthApiKey(resolvedApiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
|
|
153
159
|
}
|
|
154
160
|
function resolveBaseUrlHostname(baseUrl) {
|
|
155
|
-
|
|
156
|
-
return new URL(baseUrl).hostname;
|
|
157
|
-
} catch {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
161
|
+
return URL.parse(baseUrl)?.hostname;
|
|
160
162
|
}
|
|
161
163
|
function isLongTtlEligibleEndpoint(baseUrl) {
|
|
162
164
|
if (typeof baseUrl !== "string") return false;
|
|
@@ -392,6 +394,7 @@ function resolveAnthropicRequestBetaHeader(payload, directApiKeyBetaHeader) {
|
|
|
392
394
|
const edits = isRecord(payload.context_management) ? payload.context_management.edits : void 0;
|
|
393
395
|
const betas = new Set(directApiKeyBetaHeader.split(",").map((beta) => beta.trim()).filter(Boolean));
|
|
394
396
|
if (payload.fallbacks === "default") betas.add(ANTHROPIC_SERVER_SIDE_FALLBACK_BETA);
|
|
397
|
+
if (Array.isArray(payload.messages) && payload.messages.some((message) => isRecord(message) && message.role === "system" && message.clear_at === "next_user_message")) betas.add("mid-conversation-system-clear-at-2026-08-21");
|
|
395
398
|
for (const edit of Array.isArray(edits) ? edits : []) {
|
|
396
399
|
if (!isRecord(edit)) continue;
|
|
397
400
|
if (edit.type === "clear_tool_uses_20250919") betas.add("context-management-2025-06-27");
|
|
@@ -451,6 +454,161 @@ function applyAnthropicEphemeralCacheControlMarkers(payloadObj, cacheControl = {
|
|
|
451
454
|
}
|
|
452
455
|
}
|
|
453
456
|
//#endregion
|
|
457
|
+
//#region packages/ai/src/transports/anthropic-compaction-replay.ts
|
|
458
|
+
const ANTHROPIC_COMPACTION_REPLAY_TYPE = "anthropic-compaction";
|
|
459
|
+
const ANTHROPIC_COMPACTION_SUPPRESSION_TYPE = "anthropic-compaction-suppression";
|
|
460
|
+
const ANTHROPIC_COMPACTION_SUPPRESSION_DATA = "rejected";
|
|
461
|
+
function createCompactionCapture(output, model, options) {
|
|
462
|
+
const pending = /* @__PURE__ */ new Map();
|
|
463
|
+
return {
|
|
464
|
+
begin(index, block, replayIndex) {
|
|
465
|
+
if (block?.type !== "compaction") return false;
|
|
466
|
+
const content = typeof block.content === "string" ? block.content : "";
|
|
467
|
+
pending.set(index, {
|
|
468
|
+
content,
|
|
469
|
+
replayIndex,
|
|
470
|
+
...typeof block.encrypted_content === "string" || block.encrypted_content === null ? { encryptedContent: block.encrypted_content } : {}
|
|
471
|
+
});
|
|
472
|
+
return true;
|
|
473
|
+
},
|
|
474
|
+
delta(index, delta) {
|
|
475
|
+
const capture = pending.get(index);
|
|
476
|
+
if (!capture || delta?.type !== "compaction_delta") return false;
|
|
477
|
+
if (typeof delta.content === "string") capture.content += delta.content;
|
|
478
|
+
if (typeof delta.encrypted_content === "string" || delta.encrypted_content === null) capture.encryptedContent = delta.encrypted_content;
|
|
479
|
+
return true;
|
|
480
|
+
},
|
|
481
|
+
complete(index) {
|
|
482
|
+
const capture = pending.get(index);
|
|
483
|
+
if (!capture) return false;
|
|
484
|
+
pending.delete(index);
|
|
485
|
+
captureAnthropicCompaction(output, capture, model, options);
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
function isAnthropicCompactionState(state) {
|
|
491
|
+
if (state.v !== 1 || typeof state.data !== "string" || typeof state.baseUrlHash !== "string" || !isProviderReplayContext(state)) return false;
|
|
492
|
+
if (state.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) return state.data === ANTHROPIC_COMPACTION_SUPPRESSION_DATA;
|
|
493
|
+
return state.type === ANTHROPIC_COMPACTION_REPLAY_TYPE && state.data.length > 0 && (state.encryptedContent === void 0 || state.encryptedContent === null || typeof state.encryptedContent === "string") && (state.replayIndex === void 0 || typeof state.replayIndex === "number" && Number.isSafeInteger(state.replayIndex) && state.replayIndex >= 0);
|
|
494
|
+
}
|
|
495
|
+
function readAnthropicCompactionState(value) {
|
|
496
|
+
return isRecord(value) && isAnthropicCompactionState(value) ? value : void 0;
|
|
497
|
+
}
|
|
498
|
+
function captureAnthropicCompaction(output, capture, model, options) {
|
|
499
|
+
const { content: summary, replayIndex, encryptedContent } = capture;
|
|
500
|
+
const context = buildProviderReplayContext(model, options);
|
|
501
|
+
if (!summary || !context.baseUrlHash || !Number.isSafeInteger(replayIndex) || replayIndex < 0) return;
|
|
502
|
+
output.providerReplay = {
|
|
503
|
+
v: 1,
|
|
504
|
+
type: ANTHROPIC_COMPACTION_REPLAY_TYPE,
|
|
505
|
+
data: summary,
|
|
506
|
+
...encryptedContent !== void 0 ? { encryptedContent } : {},
|
|
507
|
+
replayIndex,
|
|
508
|
+
...context,
|
|
509
|
+
baseUrlHash: context.baseUrlHash
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
function suppressAnthropicCompaction(output, model, options) {
|
|
513
|
+
const context = buildProviderReplayContext(model, options);
|
|
514
|
+
if (!context.baseUrlHash) return;
|
|
515
|
+
output.providerReplay = {
|
|
516
|
+
v: 1,
|
|
517
|
+
type: ANTHROPIC_COMPACTION_SUPPRESSION_TYPE,
|
|
518
|
+
data: ANTHROPIC_COMPACTION_SUPPRESSION_DATA,
|
|
519
|
+
...context,
|
|
520
|
+
baseUrlHash: context.baseUrlHash
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
function resolveNewestAnthropicCompaction(messages, model, options) {
|
|
524
|
+
const context = buildProviderReplayContext(model, options);
|
|
525
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
526
|
+
const message = messages[index];
|
|
527
|
+
if (message?.role !== "assistant") continue;
|
|
528
|
+
const replayState = readAnthropicCompactionState(message.providerReplay);
|
|
529
|
+
if (replayState?.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) {
|
|
530
|
+
if (providerReplayContextMatches(replayState, context)) return;
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
if (message.providerReplay?.type !== ANTHROPIC_COMPACTION_REPLAY_TYPE) continue;
|
|
534
|
+
if (!replayState) return;
|
|
535
|
+
if (!providerReplayContextMatches(replayState, context)) return;
|
|
536
|
+
return {
|
|
537
|
+
owner: message,
|
|
538
|
+
replayIndex: replayState.replayIndex ?? 0,
|
|
539
|
+
summary: replayState.data,
|
|
540
|
+
...replayState.encryptedContent !== void 0 ? { encryptedContent: replayState.encryptedContent } : {}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
function buildAnthropicReplayPlan(messages, model, options) {
|
|
545
|
+
if (options?.enabled !== true) return { messages };
|
|
546
|
+
const checkpoint = resolveNewestAnthropicCompaction(messages, model, options);
|
|
547
|
+
if (!checkpoint) return { messages };
|
|
548
|
+
const ownerIndex = messages.indexOf(checkpoint.owner);
|
|
549
|
+
return {
|
|
550
|
+
messages: [{
|
|
551
|
+
...checkpoint.owner,
|
|
552
|
+
content: checkpoint.owner.content.slice(checkpoint.replayIndex)
|
|
553
|
+
}, ...messages.slice(ownerIndex + 1)],
|
|
554
|
+
compaction: {
|
|
555
|
+
type: "compaction",
|
|
556
|
+
content: checkpoint.summary,
|
|
557
|
+
...checkpoint.encryptedContent !== void 0 ? { encrypted_content: checkpoint.encryptedContent } : {}
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
/** Match Anthropic's 400 response when a replay block is no longer accepted. */
|
|
562
|
+
function isAnthropicReplayRejection(error) {
|
|
563
|
+
if (!error || typeof error !== "object") return false;
|
|
564
|
+
const record = error;
|
|
565
|
+
const nested = record.error && typeof record.error === "object" && !Array.isArray(record.error) ? record.error : void 0;
|
|
566
|
+
const text = [
|
|
567
|
+
record.errorMessage,
|
|
568
|
+
record.message,
|
|
569
|
+
record.code,
|
|
570
|
+
nested?.message,
|
|
571
|
+
nested?.code
|
|
572
|
+
].filter((value) => typeof value === "string").join(" ").toLowerCase();
|
|
573
|
+
return (record.status === 400 || record.errorCode === "400" || text.startsWith("http 400:")) && (text.includes("compaction") || text.includes("context_management"));
|
|
574
|
+
}
|
|
575
|
+
//#endregion
|
|
576
|
+
//#region packages/ai/src/internal/anthropic-inline-images.ts
|
|
577
|
+
const ANTHROPIC_IMAGE_MEDIA_TYPE_SET = /* @__PURE__ */ new Set([
|
|
578
|
+
"image/jpeg",
|
|
579
|
+
"image/png",
|
|
580
|
+
"image/gif",
|
|
581
|
+
"image/webp"
|
|
582
|
+
]);
|
|
583
|
+
const ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES = 67108864;
|
|
584
|
+
function createAnthropicInlineImageBudget() {
|
|
585
|
+
return { totalBytes: 0 };
|
|
586
|
+
}
|
|
587
|
+
function resolveAnthropicImageMediaType(value) {
|
|
588
|
+
if (ANTHROPIC_IMAGE_MEDIA_TYPE_SET.has(value)) return value;
|
|
589
|
+
throw new Error(`Unsupported Anthropic image media type after normalization: ${value}`);
|
|
590
|
+
}
|
|
591
|
+
function measureInlineImagesWithinBudget(content, budget) {
|
|
592
|
+
const bytes = content.reduce((total, block) => block.type === "image" ? total + estimateBase64DecodedBytes(block.data) : total, 0);
|
|
593
|
+
if (budget.totalBytes + bytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
|
|
594
|
+
return bytes;
|
|
595
|
+
}
|
|
596
|
+
async function normalizeAnthropicInlineContent(content, budget) {
|
|
597
|
+
if (!content.some((block) => block.type === "image")) return content.filter((block) => block.type === "text");
|
|
598
|
+
measureInlineImagesWithinBudget(content, budget);
|
|
599
|
+
const normalized = [];
|
|
600
|
+
for (const block of content) {
|
|
601
|
+
if (block.type !== "image") {
|
|
602
|
+
normalized.push(block);
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
const normalizedBlocks = await getAiTransportHost().normalizeAnthropicInlineContentBlocks([block]);
|
|
606
|
+
budget.totalBytes += measureInlineImagesWithinBudget(normalizedBlocks, budget);
|
|
607
|
+
normalized.push(...normalizedBlocks);
|
|
608
|
+
}
|
|
609
|
+
return normalized;
|
|
610
|
+
}
|
|
611
|
+
//#endregion
|
|
454
612
|
//#region packages/ai/src/providers/anthropic-thinking-replay.ts
|
|
455
613
|
const ANTHROPIC_OMITTED_REASONING_TEXT = "[assistant reasoning omitted]";
|
|
456
614
|
function applyAnthropicThinkingBindingControls(params, directApiKeyBetaHeader) {
|
|
@@ -482,6 +640,10 @@ function findActiveAnthropicToolTurnAssistantIndex(messages) {
|
|
|
482
640
|
let index = messages.length - 1;
|
|
483
641
|
while (index >= 0) {
|
|
484
642
|
const message = asReplayMessage(messages[index]);
|
|
643
|
+
if (message?.role === "user" && isRecord(message.operatorMessage)) {
|
|
644
|
+
index -= 1;
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
485
647
|
if (message?.role !== "toolResult") break;
|
|
486
648
|
if (typeof message.toolCallId === "string") toolResultIds.add(message.toolCallId);
|
|
487
649
|
index -= 1;
|
|
@@ -665,157 +827,77 @@ function resolveOriginalAnthropicToolName(name, projection) {
|
|
|
665
827
|
return projection?.tools.find((tool) => tool.wireName === name)?.originalName ?? name;
|
|
666
828
|
}
|
|
667
829
|
//#endregion
|
|
668
|
-
//#region packages/ai/src/transports/anthropic-
|
|
669
|
-
const
|
|
670
|
-
const
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
const
|
|
674
|
-
return
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
...
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
}
|
|
723
|
-
function suppressAnthropicCompaction(output, model, options) {
|
|
724
|
-
const context = buildProviderReplayContext(model, options);
|
|
725
|
-
if (!context.baseUrlHash) return;
|
|
726
|
-
output.providerReplay = {
|
|
727
|
-
v: 1,
|
|
728
|
-
type: ANTHROPIC_COMPACTION_SUPPRESSION_TYPE,
|
|
729
|
-
data: ANTHROPIC_COMPACTION_SUPPRESSION_DATA,
|
|
730
|
-
...context,
|
|
731
|
-
baseUrlHash: context.baseUrlHash
|
|
732
|
-
};
|
|
733
|
-
}
|
|
734
|
-
function resolveNewestAnthropicCompaction(messages, model, options) {
|
|
735
|
-
const context = buildProviderReplayContext(model, options);
|
|
736
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
737
|
-
const message = messages[index];
|
|
738
|
-
if (message?.role !== "assistant") continue;
|
|
739
|
-
const replayState = readAnthropicCompactionState(message.providerReplay);
|
|
740
|
-
if (replayState?.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) {
|
|
741
|
-
if (providerReplayContextMatches(replayState, context)) return;
|
|
742
|
-
continue;
|
|
743
|
-
}
|
|
744
|
-
if (message.providerReplay?.type !== ANTHROPIC_COMPACTION_REPLAY_TYPE) continue;
|
|
745
|
-
if (!replayState) return;
|
|
746
|
-
if (!providerReplayContextMatches(replayState, context)) return;
|
|
747
|
-
return {
|
|
748
|
-
owner: message,
|
|
749
|
-
replayIndex: replayState.replayIndex ?? 0,
|
|
750
|
-
summary: replayState.data,
|
|
751
|
-
...replayState.encryptedContent !== void 0 ? { encryptedContent: replayState.encryptedContent } : {}
|
|
752
|
-
};
|
|
830
|
+
//#region packages/ai/src/transports/anthropic-transport-options.ts
|
|
831
|
+
const ANTHROPIC_MESSAGES_DEFAULT_MAX_TOKENS = 4096;
|
|
832
|
+
const ANTHROPIC_MESSAGES_FALLBACK_CONTEXT_DIVISOR = 4;
|
|
833
|
+
function resolvePositiveAnthropicTokenLimit(value) {
|
|
834
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return;
|
|
835
|
+
const floored = Math.floor(value);
|
|
836
|
+
return floored > 0 ? floored : void 0;
|
|
837
|
+
}
|
|
838
|
+
function resolveAnthropicMessagesMaxTokens(params) {
|
|
839
|
+
const requested = resolvePositiveAnthropicTokenLimit(params.requestedMaxTokens);
|
|
840
|
+
if (requested !== void 0) return requested;
|
|
841
|
+
const modelMax = resolvePositiveAnthropicTokenLimit(params.modelMaxTokens);
|
|
842
|
+
if (modelMax !== void 0) return params.useModelDefault ? modelMax : Math.min(modelMax, 32e3);
|
|
843
|
+
if (params.modelMaxTokens !== void 0) return;
|
|
844
|
+
const contextWindow = resolvePositiveAnthropicTokenLimit(params.modelContextWindow);
|
|
845
|
+
return contextWindow === void 0 ? ANTHROPIC_MESSAGES_DEFAULT_MAX_TOKENS : Math.max(1, Math.min(ANTHROPIC_MESSAGES_DEFAULT_MAX_TOKENS, Math.floor(contextWindow / ANTHROPIC_MESSAGES_FALLBACK_CONTEXT_DIVISOR)));
|
|
846
|
+
}
|
|
847
|
+
function resolveAnthropicTransportOptions(model, options, apiKey) {
|
|
848
|
+
const baseMaxTokens = resolveAnthropicMessagesMaxTokens({
|
|
849
|
+
modelContextWindow: model.contextWindow,
|
|
850
|
+
modelMaxTokens: model.maxTokens,
|
|
851
|
+
requestedMaxTokens: options?.maxTokens,
|
|
852
|
+
useModelDefault: resolveClaudeSonnet5ModelIdentity(model) !== void 0 || resolveClaudeOpus5ModelIdentity(model) !== void 0
|
|
853
|
+
});
|
|
854
|
+
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
|
+
const resolved = copyProviderAcceptanceObserver(options, {
|
|
859
|
+
temperature: options?.temperature,
|
|
860
|
+
stop: options?.stop,
|
|
861
|
+
maxTokens: baseMaxTokens,
|
|
862
|
+
signal: options?.signal,
|
|
863
|
+
apiKey,
|
|
864
|
+
cacheRetention: options?.cacheRetention,
|
|
865
|
+
sessionId: options?.sessionId,
|
|
866
|
+
headers: options?.headers,
|
|
867
|
+
onPayload: options?.onPayload,
|
|
868
|
+
onResponse: options?.onResponse,
|
|
869
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
870
|
+
metadata: options?.metadata,
|
|
871
|
+
interleavedThinking: options?.interleavedThinking,
|
|
872
|
+
toolChoice: options?.toolChoice,
|
|
873
|
+
thinkingBudgets: options?.thinkingBudgets,
|
|
874
|
+
thinkingDisplay: options?.thinkingDisplay,
|
|
875
|
+
reasoning,
|
|
876
|
+
anthropicServerCompaction: options?.anthropicServerCompaction,
|
|
877
|
+
anthropicCompactThreshold: options?.anthropicCompactThreshold,
|
|
878
|
+
cacheTtlPruning: options?.cacheTtlPruning,
|
|
879
|
+
...options?.authProfileId ? { authProfileId: options.authProfileId } : {}
|
|
880
|
+
});
|
|
881
|
+
if (reasoning === "off") {
|
|
882
|
+
resolved.thinkingEnabled = false;
|
|
883
|
+
return resolved;
|
|
753
884
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
if (!checkpoint) return { messages };
|
|
759
|
-
const ownerIndex = messages.indexOf(checkpoint.owner);
|
|
760
|
-
return {
|
|
761
|
-
messages: [{
|
|
762
|
-
...checkpoint.owner,
|
|
763
|
-
content: checkpoint.owner.content.slice(checkpoint.replayIndex)
|
|
764
|
-
}, ...messages.slice(ownerIndex + 1)],
|
|
765
|
-
compaction: {
|
|
766
|
-
type: "compaction",
|
|
767
|
-
content: checkpoint.summary,
|
|
768
|
-
...checkpoint.encryptedContent !== void 0 ? { encrypted_content: checkpoint.encryptedContent } : {}
|
|
769
|
-
}
|
|
770
|
-
};
|
|
771
|
-
}
|
|
772
|
-
/** Match Anthropic's 400 response when a replay block is no longer accepted. */
|
|
773
|
-
function isAnthropicReplayRejection(error) {
|
|
774
|
-
if (!error || typeof error !== "object") return false;
|
|
775
|
-
const record = error;
|
|
776
|
-
const nested = record.error && typeof record.error === "object" && !Array.isArray(record.error) ? record.error : void 0;
|
|
777
|
-
const text = [
|
|
778
|
-
record.errorMessage,
|
|
779
|
-
record.message,
|
|
780
|
-
record.code,
|
|
781
|
-
nested?.message,
|
|
782
|
-
nested?.code
|
|
783
|
-
].filter((value) => typeof value === "string").join(" ").toLowerCase();
|
|
784
|
-
return (record.status === 400 || record.errorCode === "400" || text.startsWith("http 400:")) && (text.includes("compaction") || text.includes("context_management"));
|
|
785
|
-
}
|
|
786
|
-
//#endregion
|
|
787
|
-
//#region packages/ai/src/internal/anthropic-inline-images.ts
|
|
788
|
-
const ANTHROPIC_IMAGE_MEDIA_TYPE_SET = /* @__PURE__ */ new Set([
|
|
789
|
-
"image/jpeg",
|
|
790
|
-
"image/png",
|
|
791
|
-
"image/gif",
|
|
792
|
-
"image/webp"
|
|
793
|
-
]);
|
|
794
|
-
const ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES = 67108864;
|
|
795
|
-
function createAnthropicInlineImageBudget() {
|
|
796
|
-
return { totalBytes: 0 };
|
|
797
|
-
}
|
|
798
|
-
function resolveAnthropicImageMediaType(value) {
|
|
799
|
-
if (ANTHROPIC_IMAGE_MEDIA_TYPE_SET.has(value)) return value;
|
|
800
|
-
throw new Error(`Unsupported Anthropic image media type after normalization: ${value}`);
|
|
801
|
-
}
|
|
802
|
-
async function normalizeAnthropicInlineContent(content, budget) {
|
|
803
|
-
if (!content.some((block) => block.type === "image")) return content.filter((block) => block.type === "text");
|
|
804
|
-
const inputBytes = content.reduce((total, block) => block.type === "image" ? total + estimateBase64DecodedBytes(block.data) : total, 0);
|
|
805
|
-
if (budget.totalBytes + inputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
|
|
806
|
-
const normalized = [];
|
|
807
|
-
for (const block of content) {
|
|
808
|
-
if (block.type !== "image") {
|
|
809
|
-
normalized.push(block);
|
|
810
|
-
continue;
|
|
811
|
-
}
|
|
812
|
-
const normalizedBlocks = await getAiTransportHost().normalizeAnthropicInlineContentBlocks([block]);
|
|
813
|
-
const outputBytes = normalizedBlocks.reduce((total, normalizedBlock) => normalizedBlock.type === "image" ? total + estimateBase64DecodedBytes(normalizedBlock.data) : total, 0);
|
|
814
|
-
if (budget.totalBytes + outputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
|
|
815
|
-
budget.totalBytes += outputBytes;
|
|
816
|
-
normalized.push(...normalizedBlocks);
|
|
885
|
+
if (!reasoning) {
|
|
886
|
+
resolved.thinkingEnabled = defaultsClaudeAdaptiveThinking(model);
|
|
887
|
+
if (resolved.thinkingEnabled) resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
888
|
+
return resolved;
|
|
817
889
|
}
|
|
818
|
-
|
|
890
|
+
if (supportsClaudeAdaptiveThinking(model)) {
|
|
891
|
+
resolved.thinkingEnabled = true;
|
|
892
|
+
resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
893
|
+
return resolved;
|
|
894
|
+
}
|
|
895
|
+
const adjusted = adjustMaxTokensForThinking(baseMaxTokens, reasoningModelMaxTokens, reasoning === "max" ? "high" : reasoning, options?.thinkingBudgets);
|
|
896
|
+
const thinkingEnabled = adjusted.thinkingBudget >= 1024;
|
|
897
|
+
resolved.maxTokens = adjusted.maxTokens;
|
|
898
|
+
resolved.thinkingEnabled = thinkingEnabled;
|
|
899
|
+
resolved.thinkingBudgetTokens = thinkingEnabled ? adjusted.thinkingBudget : void 0;
|
|
900
|
+
return resolved;
|
|
819
901
|
}
|
|
820
902
|
//#endregion
|
|
821
903
|
//#region packages/ai/src/transports/anthropic-messages.ts
|
|
@@ -860,7 +942,18 @@ async function convertContentBlocks(content, model, imageBudget, profile, isErro
|
|
|
860
942
|
}
|
|
861
943
|
async function convertAnthropicMessages(transformedMessages, model, isOAuthToken, options) {
|
|
862
944
|
const params = [];
|
|
945
|
+
const appendMessage = (message) => {
|
|
946
|
+
if (message.role === "user" && params.at(-1)?.role === "system") params.push({
|
|
947
|
+
role: "assistant",
|
|
948
|
+
content: [{
|
|
949
|
+
type: "text",
|
|
950
|
+
text: STREAM_ERROR_FALLBACK_TEXT
|
|
951
|
+
}]
|
|
952
|
+
});
|
|
953
|
+
params.push(message);
|
|
954
|
+
};
|
|
863
955
|
const modelRetainsRuntimeContext = bindsClaudeThinkingPrefix(model);
|
|
956
|
+
const inHistorySystemUpdates = !isOAuthToken && isDirectAnthropicModel(model) && supportsClaudeInHistorySystemMessages(model);
|
|
864
957
|
const imageBudget = createAnthropicInlineImageBudget();
|
|
865
958
|
const allowReasoningContentReplay = options.allowReasoningContentReplay === true;
|
|
866
959
|
const replayThinkingEnabled = options.replayThinkingEnabled !== false;
|
|
@@ -870,12 +963,14 @@ async function convertAnthropicMessages(transformedMessages, model, isOAuthToken
|
|
|
870
963
|
const msg = transformedMessages[i];
|
|
871
964
|
if (!msg) continue;
|
|
872
965
|
if (msg.role === "user") {
|
|
966
|
+
const operatorMessage = inHistorySystemUpdates ? msg.operatorMessage : void 0;
|
|
967
|
+
const sourceContent = msg.content;
|
|
873
968
|
let content;
|
|
874
|
-
if (typeof
|
|
875
|
-
if (
|
|
876
|
-
content = sanitizeTransportPayloadText(
|
|
969
|
+
if (typeof sourceContent === "string") {
|
|
970
|
+
if (sourceContent.trim().length === 0) continue;
|
|
971
|
+
content = sanitizeTransportPayloadText(sourceContent);
|
|
877
972
|
} else {
|
|
878
|
-
content = (!managed || model.input.includes("image") ? await normalizeAnthropicInlineContent(
|
|
973
|
+
content = (!managed || model.input.includes("image") ? await normalizeAnthropicInlineContent(sourceContent, imageBudget) : sourceContent.map((item) => item.type === "image" ? {
|
|
879
974
|
type: "text",
|
|
880
975
|
text: NON_VISION_USER_IMAGE_PLACEHOLDER
|
|
881
976
|
} : item)).map((item) => item.type === "text" ? {
|
|
@@ -891,11 +986,16 @@ async function convertAnthropicMessages(transformedMessages, model, isOAuthToken
|
|
|
891
986
|
}).filter((block) => block.type === "text" ? block.text.trim().length > 0 : !managed || model.input.includes("image"));
|
|
892
987
|
if (content.length === 0) continue;
|
|
893
988
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
989
|
+
appendMessage({
|
|
990
|
+
role: operatorMessage ? "system" : "user",
|
|
991
|
+
content: operatorMessage ? typeof content === "string" ? [{
|
|
992
|
+
type: "text",
|
|
993
|
+
text: content
|
|
994
|
+
}] : content.filter((block) => block.type === "text") : content,
|
|
995
|
+
...operatorMessage?.turnScoped ? { clear_at: "next_user_message" } : {}
|
|
898
996
|
});
|
|
997
|
+
const runtimeContextRetained = msg.runtimeContext !== void 0 ? msg.runtimeContext.retained : msg.runtimeContextCarrierRetained;
|
|
998
|
+
if (hasRuntimeContextMarker(msg) && !(runtimeContextRetained ?? modelRetainsRuntimeContext)) options.cacheBreakpointOptOutMessageIndexes?.add(params.length - 1);
|
|
899
999
|
continue;
|
|
900
1000
|
}
|
|
901
1001
|
if (msg.role === "assistant") {
|
|
@@ -983,7 +1083,7 @@ async function convertAnthropicMessages(transformedMessages, model, isOAuthToken
|
|
|
983
1083
|
j += 1;
|
|
984
1084
|
}
|
|
985
1085
|
i = j - 1;
|
|
986
|
-
|
|
1086
|
+
appendMessage({
|
|
987
1087
|
role: "user",
|
|
988
1088
|
content: toolResults
|
|
989
1089
|
});
|
|
@@ -1041,6 +1141,73 @@ function convertAnthropicTools(tools, isOAuthTokenLocal, supportsEagerToolInputS
|
|
|
1041
1141
|
})
|
|
1042
1142
|
};
|
|
1043
1143
|
}
|
|
1144
|
+
/** Assemble both public Messages routes without changing their replay/default profiles. */
|
|
1145
|
+
async function buildAnthropicRequest(model, context, options, profile, isOAuthToken, serverSideFallback, claudeCodeVersion) {
|
|
1146
|
+
const managed = profile === "transport";
|
|
1147
|
+
const maxTokens = managed ? resolveAnthropicMessagesMaxTokens({
|
|
1148
|
+
modelContextWindow: model.contextWindow,
|
|
1149
|
+
modelMaxTokens: model.maxTokens,
|
|
1150
|
+
requestedMaxTokens: options?.maxTokens
|
|
1151
|
+
}) : options?.maxTokens ?? model.maxTokens;
|
|
1152
|
+
if (managed && maxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
|
|
1153
|
+
const { cacheControl, supportsCacheControlOnTools } = resolveAnthropicCacheOptions(model, options?.cacheRetention);
|
|
1154
|
+
const system = buildAnthropicSystemBlocks(context.systemPrompt, isOAuthToken, cacheControl, claudeCodeVersion);
|
|
1155
|
+
const convertedTools = context.tools ? convertAnthropicTools(context.tools, isOAuthToken, !managed && (model.compat?.supportsEagerToolInputStreaming ?? model.provider !== "fireworks")) : void 0;
|
|
1156
|
+
const toolProjection = convertedTools?.projection;
|
|
1157
|
+
const replayPlan = buildAnthropicReplayPlan(context.messages, model, {
|
|
1158
|
+
enabled: !isOAuthToken && options?.anthropicServerCompaction === true,
|
|
1159
|
+
authProfileId: options?.authProfileId,
|
|
1160
|
+
sessionId: options?.sessionId
|
|
1161
|
+
});
|
|
1162
|
+
const cacheBreakpointOptOutMessageIndexes = /* @__PURE__ */ new Set();
|
|
1163
|
+
const messages = await convertAnthropicMessages(managed ? getAiTransportHost().transformTransportMessages(replayPlan.messages, model, normalizeAnthropicToolCallId, void 0) : transformProviderMessages(replayPlan.messages, model, normalizeAnthropicToolCallId), model, isOAuthToken, {
|
|
1164
|
+
profile,
|
|
1165
|
+
allowReasoningContentReplay: managed && resolveProviderEndpoint(model).endpointClass === "xiaomi-native",
|
|
1166
|
+
allowEmptySignature: model.compat?.allowEmptySignature,
|
|
1167
|
+
compaction: replayPlan.compaction,
|
|
1168
|
+
replayThinkingEnabled: requiresClaudeAdaptiveThinking(model) || options?.thinkingEnabled === true,
|
|
1169
|
+
cacheBreakpointOptOutMessageIndexes
|
|
1170
|
+
});
|
|
1171
|
+
if (managed && messages.length === 0) messages.push({
|
|
1172
|
+
role: "user",
|
|
1173
|
+
content: "."
|
|
1174
|
+
});
|
|
1175
|
+
const params = {
|
|
1176
|
+
model: managed && isDirectAnthropicModel(model) ? model.id.replace(/^anthropic\//i, "") : model.id,
|
|
1177
|
+
messages,
|
|
1178
|
+
max_tokens: maxTokens ?? model.maxTokens,
|
|
1179
|
+
stream: true
|
|
1180
|
+
};
|
|
1181
|
+
if (system) params.system = system;
|
|
1182
|
+
if (serverSideFallback) Object.assign(params, { fallbacks: ANTHROPIC_SERVER_SIDE_FALLBACKS });
|
|
1183
|
+
Object.assign(params, buildAnthropicGenerationParams({
|
|
1184
|
+
model,
|
|
1185
|
+
options,
|
|
1186
|
+
tools: convertedTools?.tools,
|
|
1187
|
+
toolProjection,
|
|
1188
|
+
profile
|
|
1189
|
+
}));
|
|
1190
|
+
applyAnthropicRequestCacheControl(params, cacheControl, supportsCacheControlOnTools, cacheBreakpointOptOutMessageIndexes);
|
|
1191
|
+
return {
|
|
1192
|
+
params,
|
|
1193
|
+
toolProjection,
|
|
1194
|
+
usedCompactionReplay: replayPlan.compaction !== void 0
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
/** Apply caller payload edits before restoring required request contracts and beta headers. */
|
|
1198
|
+
async function prepareAnthropicRequest(initialParams, model, options, directApiKeyBetaHeader) {
|
|
1199
|
+
let params = initialParams;
|
|
1200
|
+
applyAnthropicContextManagementToRequest(params, model, options, directApiKeyBetaHeader);
|
|
1201
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
1202
|
+
if (nextParams !== void 0) params = nextParams;
|
|
1203
|
+
applyClaudeRequestContract(params, model);
|
|
1204
|
+
const betaHeader = resolveAnthropicRequestBetaHeader(params, directApiKeyBetaHeader);
|
|
1205
|
+
const headers = applyAnthropicThinkingBindingControls(params, betaHeader) ?? (betaHeader ? { "anthropic-beta": betaHeader } : void 0);
|
|
1206
|
+
return {
|
|
1207
|
+
params,
|
|
1208
|
+
headers
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1044
1211
|
//#endregion
|
|
1045
1212
|
//#region packages/ai/src/providers/anthropic-refusal.ts
|
|
1046
1213
|
function readAnthropicRefusalDetails(value) {
|
|
@@ -1054,13 +1221,12 @@ function readAnthropicRefusalDetails(value) {
|
|
|
1054
1221
|
explanation: normalizeNullableString(details.explanation)
|
|
1055
1222
|
};
|
|
1056
1223
|
}
|
|
1057
|
-
function formatAnthropicRefusalMessage(details) {
|
|
1058
|
-
return `Anthropic refusal${details.category ? ` (category: ${details.category})` : ""}${details.explanation ? `: ${details.explanation}` : "."}`;
|
|
1059
|
-
}
|
|
1060
1224
|
function applyAnthropicRefusal(output, stopDetails, provider) {
|
|
1061
1225
|
const details = readAnthropicRefusalDetails(stopDetails);
|
|
1226
|
+
const category = details.category ? ` (category: ${details.category})` : "";
|
|
1227
|
+
const explanation = details.explanation ? `: ${details.explanation}` : ".";
|
|
1062
1228
|
output.stopReason = "error";
|
|
1063
|
-
output.errorMessage =
|
|
1229
|
+
output.errorMessage = `Anthropic refusal${category}${explanation}`;
|
|
1064
1230
|
output.diagnostics = [...output.diagnostics ?? [], {
|
|
1065
1231
|
type: "provider_refusal",
|
|
1066
1232
|
timestamp: Date.now(),
|
|
@@ -1252,7 +1418,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1252
1418
|
const key = eventIndexKey(eventIndex);
|
|
1253
1419
|
let contentIndex = indexes.get(key);
|
|
1254
1420
|
let block = contentIndex === void 0 ? void 0 : blocks[contentIndex];
|
|
1255
|
-
if (!block || block.type !== kind) {
|
|
1421
|
+
if (contentIndex === void 0 || !block || block.type !== kind) {
|
|
1256
1422
|
block = kind === "thinking" ? {
|
|
1257
1423
|
type: "thinking",
|
|
1258
1424
|
thinking: "",
|
|
@@ -1270,7 +1436,6 @@ async function consumeAnthropicStream(params) {
|
|
|
1270
1436
|
partial: output
|
|
1271
1437
|
});
|
|
1272
1438
|
}
|
|
1273
|
-
if (contentIndex === void 0) return false;
|
|
1274
1439
|
if (block.type === "thinking") {
|
|
1275
1440
|
appendAssistantThinking(block, text);
|
|
1276
1441
|
block.thinkingSignature = "reasoning_content";
|
|
@@ -1305,7 +1470,7 @@ async function consumeAnthropicStream(params) {
|
|
|
1305
1470
|
if (managed) notifyLlmRequestActivity(options.signal);
|
|
1306
1471
|
if (event.type === "error") {
|
|
1307
1472
|
const error = asOptionalObjectRecord(event.error);
|
|
1308
|
-
throw new Error(readStringField(error, "message") || "Anthropic Messages stream failed");
|
|
1473
|
+
throw Object.assign(new Error(readStringField(error, "message") || "Anthropic Messages stream failed"), { error });
|
|
1309
1474
|
}
|
|
1310
1475
|
if (event.type === "message_start") {
|
|
1311
1476
|
sawMessageStart = true;
|
|
@@ -1378,66 +1543,37 @@ async function consumeAnthropicStream(params) {
|
|
|
1378
1543
|
continue;
|
|
1379
1544
|
}
|
|
1380
1545
|
pendingThinkingSignatures.delete(index);
|
|
1381
|
-
if (contentBlock?.type === "text") {
|
|
1382
|
-
const
|
|
1383
|
-
const block = {
|
|
1546
|
+
if (contentBlock?.type === "text" || contentBlock?.type === "thinking" || contentBlock?.type === "redacted_thinking") {
|
|
1547
|
+
const block = contentBlock.type === "text" ? {
|
|
1384
1548
|
type: "text",
|
|
1385
|
-
text,
|
|
1549
|
+
text: typeof contentBlock.text === "string" ? sanitizeTransportPayloadText(contentBlock.text) : "",
|
|
1386
1550
|
index
|
|
1387
|
-
}
|
|
1388
|
-
output.content.push(block);
|
|
1389
|
-
const contentIndex = output.content.length - 1;
|
|
1390
|
-
blockIndexes.set(index, contentIndex);
|
|
1391
|
-
eventSink.push({
|
|
1392
|
-
type: "text_start",
|
|
1393
|
-
contentIndex,
|
|
1394
|
-
partial: output
|
|
1395
|
-
});
|
|
1396
|
-
if (text.length > 0) eventSink.push({
|
|
1397
|
-
type: "text_delta",
|
|
1398
|
-
contentIndex,
|
|
1399
|
-
delta: text,
|
|
1400
|
-
partial: output
|
|
1401
|
-
});
|
|
1402
|
-
continue;
|
|
1403
|
-
}
|
|
1404
|
-
if (contentBlock?.type === "thinking") {
|
|
1405
|
-
const thinking = typeof contentBlock.thinking === "string" ? contentBlock.thinking : "";
|
|
1406
|
-
const block = {
|
|
1551
|
+
} : contentBlock.type === "thinking" ? {
|
|
1407
1552
|
type: "thinking",
|
|
1408
|
-
thinking,
|
|
1553
|
+
thinking: typeof contentBlock.thinking === "string" ? contentBlock.thinking : "",
|
|
1409
1554
|
thinkingSignature: typeof contentBlock.signature === "string" ? contentBlock.signature : "",
|
|
1410
1555
|
index
|
|
1556
|
+
} : {
|
|
1557
|
+
type: "thinking",
|
|
1558
|
+
thinking: "[Reasoning redacted]",
|
|
1559
|
+
thinkingSignature: typeof contentBlock.data === "string" ? contentBlock.data : "",
|
|
1560
|
+
redacted: true,
|
|
1561
|
+
index
|
|
1411
1562
|
};
|
|
1563
|
+
const kind = block.type;
|
|
1564
|
+
const delta = block.type === "text" ? block.text : block.redacted ? "" : block.thinking;
|
|
1412
1565
|
output.content.push(block);
|
|
1413
1566
|
const contentIndex = output.content.length - 1;
|
|
1414
1567
|
blockIndexes.set(index, contentIndex);
|
|
1415
1568
|
eventSink.push({
|
|
1416
|
-
type:
|
|
1569
|
+
type: `${kind}_start`,
|
|
1417
1570
|
contentIndex,
|
|
1418
1571
|
partial: output
|
|
1419
1572
|
});
|
|
1420
|
-
if (
|
|
1421
|
-
type:
|
|
1573
|
+
if (delta.length > 0) eventSink.push({
|
|
1574
|
+
type: `${kind}_delta`,
|
|
1422
1575
|
contentIndex,
|
|
1423
|
-
delta
|
|
1424
|
-
partial: output
|
|
1425
|
-
});
|
|
1426
|
-
continue;
|
|
1427
|
-
}
|
|
1428
|
-
if (contentBlock?.type === "redacted_thinking") {
|
|
1429
|
-
const block = {
|
|
1430
|
-
type: "thinking",
|
|
1431
|
-
thinking: "[Reasoning redacted]",
|
|
1432
|
-
thinkingSignature: typeof contentBlock.data === "string" ? contentBlock.data : "",
|
|
1433
|
-
redacted: true,
|
|
1434
|
-
index
|
|
1435
|
-
};
|
|
1436
|
-
output.content.push(block);
|
|
1437
|
-
blockIndexes.set(index, output.content.length - 1);
|
|
1438
|
-
eventSink.push({
|
|
1439
|
-
type: "thinking_start",
|
|
1440
|
-
contentIndex: output.content.length - 1,
|
|
1576
|
+
delta,
|
|
1441
1577
|
partial: output
|
|
1442
1578
|
});
|
|
1443
1579
|
continue;
|
|
@@ -1622,4 +1758,4 @@ async function consumeAnthropicStream(params) {
|
|
|
1622
1758
|
}
|
|
1623
1759
|
}
|
|
1624
1760
|
//#endregion
|
|
1625
|
-
export {
|
|
1761
|
+
export { buildAnthropicSystemBlocks as A, ANTHROPIC_SERVER_SIDE_FALLBACKS as B, isAnthropicReplayRejection as C, applyAnthropicEphemeralCacheControlMarkers as D, applyAnthropicContextManagementToRequest as E, resolveAnthropicEphemeralCacheControl as F, resolveAnthropicFallbackServingModelCost as G, CLAUDE_OPUS_FALLBACK_MODEL_COST as H, resolveAnthropicPayloadPolicy as I, usesFoundryBearerAuth as J, isAnthropicOAuthApiKey as K, resolveAnthropicRequestBetaHeader as L, isDirectAnthropicModel as M, logAnthropicContextEdits as N, applyAnthropicPayloadPolicyToParams as O, resolveAnthropicCacheOptions as P, resolveAnthropicServerCompactionPlan as R, readAnthropicInputTransformations as S, suppressAnthropicCompaction as T, applyAnthropicFallbackBoundary as U, ANTHROPIC_SERVER_SIDE_FALLBACK_BETA as V, readAnthropicFallbackBoundary as W, toClaudeCodeToolName as _, readAnthropicPromptUsageSnapshot as a, findActiveAnthropicToolTurnAssistantIndex as b, applyAnthropicRefusal as c, resolveAnthropicTransportOptions as d, normalizeAnthropicToolCallId as f, resolveOriginalAnthropicToolName as g, reconcileAnthropicToolChoice as h, readAnthropicCacheWriteUsage as i, isAnthropicServerToolClearingEnabled as j, applyAnthropicRequestCacheControl as k, buildAnthropicRequest as l, projectAnthropicTools as m, applyAnthropicMessageDeltaUsage as n, readAnthropicUsageTokenCount as o, normalizeAnthropicToolChoice as p, omitFoundryBearerCredentialHeaders as q, applyAnthropicMessageStartUsage as r, readLastAnthropicIterationUsage as s, consumeAnthropicStream as t, prepareAnthropicRequest as u, ANTHROPIC_OMITTED_REASONING_TEXT as v, resolveNewestAnthropicCompaction as w, logAnthropicThinkingDrops as x, applyAnthropicThinkingBindingControls as y, supportsAnthropicServerSideFallback as z };
|