@openclaw/ai 2026.7.2-beta.7 → 2026.8.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{anthropic-CH4UUnZr.mjs → anthropic-B6dLpq5L.mjs} +55 -203
- package/dist/{src-QkygScBs.mjs → anthropic-JsNA5KCu.mjs} +0 -1
- package/dist/anthropic-compaction-replay-8lJNKXOE.mjs +840 -0
- package/dist/anthropic-payload-policy-CiEuQS72.d.mts +49 -0
- package/dist/{api-registry-DlMgPR39.d.mts → api-registry-k3zTz0cV.d.mts} +1 -1
- package/dist/{azure-openai-responses-CImcwB83.mjs → azure-openai-responses-mxIOtUnn.mjs} +23 -29
- package/dist/diagnostics.d.mts +24 -1
- package/dist/diagnostics.mjs +2 -1
- package/dist/{event-stream-YjaPW20U.d.mts → event-stream-BP6AWT8j.d.mts} +4 -1
- package/dist/{event-stream-D8n2uFee.mjs → event-stream-uSMZJ3FA.mjs} +26 -5
- package/dist/event-stream.d.mts +1 -1
- package/dist/event-stream.mjs +1 -1
- package/dist/{google-CtSg0iTS.mjs → google-C7h2QzDX.mjs} +5 -5
- package/dist/{google-shared-DNBz5rcD.mjs → google-shared-B0Qr9OR7.mjs} +146 -89
- package/dist/google-thinking-level-C-V3tecN.mjs +9 -0
- package/dist/{google-vertex-31f1uS9L.mjs → google-vertex-Bhc3TjDl.mjs} +11 -7
- package/dist/{llm-request-activity-BjtkplhG.mjs → headers-DdOQtGuU.mjs} +9 -1
- package/dist/host-DTqNc7ad.mjs +466 -0
- package/dist/{host-B9GUmcra.d.mts → host-vWgMMhiJ.d.mts} +9 -4
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +7 -5
- package/dist/internal/anthropic.d.mts +29 -5
- package/dist/internal/anthropic.mjs +5 -5
- package/dist/internal/openai-responses-payload-policy.d.mts +3 -0
- package/dist/internal/openai-responses-payload-policy.mjs +3 -0
- package/dist/internal/openai.d.mts +6 -6
- package/dist/internal/openai.mjs +8 -7
- package/dist/internal/runtime.d.mts +17 -5
- package/dist/internal/runtime.mjs +85 -73
- package/dist/internal/shared.d.mts +1 -6
- package/dist/internal/shared.mjs +3 -5
- package/dist/{json-parse-BvXNt1-7.mjs → json-parse-CDnesDM_.mjs} +4 -6
- package/dist/{mistral-CWmpvWYh.mjs → mistral-CEWoQI_g.mjs} +45 -48
- package/dist/number-coercion-H9qHik3g.mjs +71 -0
- package/dist/openai-chatgpt-jwt-KWcgd0d_.mjs +19 -0
- package/dist/{openai-chatgpt-responses-B84Ibtrd.mjs → openai-chatgpt-responses-CrPmqERt.mjs} +284 -240
- package/dist/{openai-completions-DsOxhOD1.mjs → openai-completions-BPnt4Sml.mjs} +65 -99
- package/dist/{openai-completions-compat-DBWjXoMZ.d.mts → openai-completions-compat-Dt3dcawL.d.mts} +2 -2
- package/dist/{openai-responses-BT7A3sLu.mjs → openai-responses-DhIKtOup.mjs} +18 -41
- package/dist/openai-responses-contracts-CyfIkQi5.mjs +253 -0
- package/dist/openai-responses-contracts-XpZJxrRG.d.mts +68 -0
- package/dist/openai-responses-payload-policy-BDxV-W0c.mjs +206 -0
- package/dist/openai-responses-payload-policy-BSs371VM.d.mts +40 -0
- package/dist/openai-responses-prompt-observer-internal-DgNTYnRY.mjs +46 -0
- package/dist/{openai-responses-stream-internal-Cw5txaGW.mjs → openai-responses-shared-DXIt3iY5.mjs} +1406 -940
- package/dist/{openai-reasoning-compat-YgeLncHw.mjs → openai-stop-reason-BkFkqqK0.mjs} +204 -37
- package/dist/openai-tool-projection-CY04OcvQ.mjs +338 -0
- package/dist/provider-error-BUwEnjXq.mjs +429 -0
- package/dist/provider-error-CzNw4BWX.d.mts +12 -0
- package/dist/{provider-options-D8bB3z9b.d.mts → provider-options-B96RdNpH.d.mts} +9 -3
- package/dist/provider-transcript-transform-ePx-Bbfr.mjs +155 -0
- package/dist/provider-types.d.mts +31 -0
- package/dist/provider-types.mjs +8 -0
- package/dist/providers.d.mts +1 -1
- package/dist/providers.mjs +17 -19
- package/dist/{reasoning-tag-text-partitioner-CGDyLWUR.mjs → reasoning-tag-text-partitioner-rnPwX2pg.mjs} +14 -8
- package/dist/record-coerce-DdXsgUd_.mjs +23 -0
- package/dist/sanitize-unicode-BYqrYtC_.mjs +90 -0
- package/dist/session-resources-CkR4WWy1.mjs +21 -0
- package/dist/simple-options-D58D5Kvw.mjs +117 -0
- package/dist/src-D2H6yKkH.mjs +2 -0
- package/dist/{stream-first-event-timeout-BBys9hSb.mjs → stream-first-event-timeout-MK28puvq.mjs} +2 -2
- package/dist/string-coerce-fsri9iCu.mjs +34 -0
- package/dist/{tool-schema-json-projection-BwNu3nDi.mjs → tool-schema-json-projection-q5d7QX5c.mjs} +32 -7
- package/dist/transport-utils-DJqkxbhC.mjs +138 -0
- package/dist/transports.d.mts +166 -241
- package/dist/transports.mjs +1979 -1825
- package/dist/types-BDdaOVi2.mjs +6 -0
- package/dist/{types-bzp5k29J.d.mts → types-BHNrPS1l.d.mts} +23 -1
- package/dist/types.d.mts +4 -4
- package/dist/types.mjs +6 -4
- package/dist/utf16-slice-CvGodqok.mjs +29 -0
- package/dist/{validation-DAa_yFOM.mjs → validation-B61OhAio.mjs} +6 -6
- package/dist/{validation-B-j7cOYp.d.mts → validation-DT9SrFn3.d.mts} +1 -1
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +15 -1
- package/dist/anthropic-usage-DWU-x8MI.mjs +0 -459
- package/dist/error-coercion-DgxlWC0n.mjs +0 -15
- package/dist/headers-B_e4-1J0.mjs +0 -9
- package/dist/host-Dog2WQiR.mjs +0 -369
- package/dist/number-coercion-DvG7SNMg.mjs +0 -129
- package/dist/openai-chatgpt-jwt-DhAAzLkj.mjs +0 -39
- package/dist/openai-responses-shared-pXl6Wd8S.mjs +0 -392
- package/dist/openai-tool-projection-OhX64DoP.mjs +0 -215
- package/dist/provider-error-CAEvRjry.mjs +0 -47
- package/dist/sanitize-unicode-DT5o51ur.mjs +0 -26
- package/dist/simple-options-9lhRrN73.mjs +0 -50
- package/dist/tool-result-text-CTpIRbYd.mjs +0 -225
- package/dist/transform-messages-C8mBqZxF.mjs +0 -2
- package/dist/transport-stream-shared-D81p90xq.mjs +0 -297
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
import { n as getEnvApiKey, r as __exportAll } from "./env-api-keys-DrgeBuva.mjs";
|
|
2
|
-
import { d as resolveClaudeSonnet5ModelIdentity, g as supportsClaudeNativeXhighEffort,
|
|
3
|
-
import { t as AssistantMessageEventStream } from "./event-stream-
|
|
4
|
-
import {
|
|
2
|
+
import { d as resolveClaudeSonnet5ModelIdentity, g as supportsClaudeNativeXhighEffort, p as supportsClaudeAdaptiveThinking, u as resolveClaudeOpus5ModelIdentity } from "./anthropic-JsNA5KCu.mjs";
|
|
3
|
+
import { t as AssistantMessageEventStream } from "./event-stream-uSMZJ3FA.mjs";
|
|
4
|
+
import { r as calculateCost, t as sanitizeSurrogates } from "./sanitize-unicode-BYqrYtC_.mjs";
|
|
5
|
+
import { _ as resolveAnthropicThinkingEffort, a as describeToolResultMediaPlaceholder, b as usesClaudeStreamingRefusalContract, d as ANTHROPIC_CLAUDE_CODE_VERSION, f as applyClaudeRequestContract, g as requiresClaudeAdaptiveThinking, h as prepareClaudeNoPrefillRequestContext, l as isImageWithMediaPayload, m as mapAnthropicStopReason, n as getAiTransportHost, o as extractToolResultBlockText, r as resolveAiTransportHeaderSentinels, s as extractToolResultText, u as ANTHROPIC_CLAUDE_CODE_BILLING_SYSTEM_BLOCK, y as usesClaudeFable5MessagesContract } from "./host-DTqNc7ad.mjs";
|
|
6
|
+
import { n as projectProviderError } from "./provider-error-BUwEnjXq.mjs";
|
|
5
7
|
import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
|
|
6
|
-
import { t as
|
|
7
|
-
import { a as
|
|
8
|
-
import { _ as omitFoundryBearerCredentialHeaders, a as projectAnthropicTools, b as normalizeAnthropicInlineContent, c as ANTHROPIC_OMITTED_REASONING_TEXT, d as ANTHROPIC_SERVER_SIDE_FALLBACK_BETA, g as applyAnthropicRefusal, h as resolveAnthropicFallbackServingModelCost, i as readLastAnthropicIterationUsage, l as findActiveAnthropicToolTurnAssistantIndex, m as readAnthropicFallbackBoundary, n as readAnthropicPromptUsageSnapshot, o as reconcileAnthropicToolChoice, p as applyAnthropicFallbackBoundary, r as readAnthropicUsageTokenCount, s as resolveOriginalAnthropicToolName, t as readAnthropicCacheWriteUsage, u as ANTHROPIC_SERVER_SIDE_FALLBACKS, v as usesFoundryBearerAuth, x as resolveAnthropicImageMediaType, y as createAnthropicInlineImageBudget } from "./anthropic-usage-DWU-x8MI.mjs";
|
|
9
|
-
import { c as calculateCost, l as clampThinkingLevel } from "./number-coercion-DvG7SNMg.mjs";
|
|
8
|
+
import { c as splitSystemPromptCacheBoundary, l as stripSystemPromptCacheBoundary, n as buildBaseOptions, r as clampMaxTokensToModel, t as adjustMaxTokensForThinking } from "./simple-options-D58D5Kvw.mjs";
|
|
9
|
+
import { A as resolveAnthropicImageMediaType, C as readAnthropicFallbackBoundary, D as usesFoundryBearerAuth, E as omitFoundryBearerCredentialHeaders, O as createAnthropicInlineImageBudget, S as applyAnthropicFallbackBoundary, T as applyAnthropicRefusal, _ as ANTHROPIC_OMITTED_REASONING_TEXT, a as applyAnthropicMessageDeltaUsage, b as ANTHROPIC_SERVER_SIDE_FALLBACK_BETA, d as normalizeAnthropicToolCallId, f as normalizeAnthropicToolChoice, g as toClaudeCodeToolName, h as resolveOriginalAnthropicToolName, i as suppressAnthropicCompaction, j as applyAnthropicCacheControlToMessages, k as normalizeAnthropicInlineContent, m as reconcileAnthropicToolChoice, n as createCompactionCapture, o as applyAnthropicMessageStartUsage, p as projectAnthropicTools, r as isAnthropicReplayRejection, t as buildAnthropicReplayPlan, v as findActiveAnthropicToolTurnAssistantIndex, w as resolveAnthropicFallbackServingModelCost, y as ANTHROPIC_SERVER_SIDE_FALLBACKS } from "./anthropic-compaction-replay-8lJNKXOE.mjs";
|
|
10
10
|
import { t as createDeferredEventBuffer } from "./deferred-event-buffer-DAvyP7qA.mjs";
|
|
11
|
-
import { n as parseStreamingJson, t as parseJsonWithRepair } from "./json-parse-
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { t as
|
|
15
|
-
import { t as formatProviderError } from "./provider-error-CAEvRjry.mjs";
|
|
11
|
+
import { n as parseStreamingJson, t as parseJsonWithRepair } from "./json-parse-CDnesDM_.mjs";
|
|
12
|
+
import { n as notifyLlmRequestActivity, t as headersToRecord } from "./headers-DdOQtGuU.mjs";
|
|
13
|
+
import { f as transportAbortError, t as transformProviderMessages } from "./provider-transcript-transform-ePx-Bbfr.mjs";
|
|
14
|
+
import { t as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "./transport-utils-DJqkxbhC.mjs";
|
|
16
15
|
import { i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-NCJtz9i0.mjs";
|
|
17
|
-
import { n as buildBaseOptions, r as clampMaxTokensToModel, t as adjustMaxTokensForThinking } from "./simple-options-9lhRrN73.mjs";
|
|
18
|
-
import "./transform-messages-C8mBqZxF.mjs";
|
|
19
16
|
import Anthropic from "@anthropic-ai/sdk";
|
|
20
17
|
import { Stream } from "@anthropic-ai/sdk/core/streaming.js";
|
|
21
18
|
//#region packages/ai/src/providers/anthropic.ts
|
|
@@ -37,28 +34,6 @@ function getCacheControl(model, cacheRetention) {
|
|
|
37
34
|
}
|
|
38
35
|
};
|
|
39
36
|
}
|
|
40
|
-
const claudeCodeVersion = "2.1.75";
|
|
41
|
-
const claudeCodeBillingSystemBlock = `x-anthropic-billing-header: cc_version=${claudeCodeVersion}; cc_entrypoint=sdk-cli;`;
|
|
42
|
-
const ccToolLookup = new Map([
|
|
43
|
-
"Read",
|
|
44
|
-
"Write",
|
|
45
|
-
"Edit",
|
|
46
|
-
"Bash",
|
|
47
|
-
"Grep",
|
|
48
|
-
"Glob",
|
|
49
|
-
"AskUserQuestion",
|
|
50
|
-
"EnterPlanMode",
|
|
51
|
-
"ExitPlanMode",
|
|
52
|
-
"KillShell",
|
|
53
|
-
"NotebookEdit",
|
|
54
|
-
"Skill",
|
|
55
|
-
"Task",
|
|
56
|
-
"TaskOutput",
|
|
57
|
-
"TodoWrite",
|
|
58
|
-
"WebFetch",
|
|
59
|
-
"WebSearch"
|
|
60
|
-
].map((t) => [t.toLowerCase(), t]));
|
|
61
|
-
const toClaudeCodeName = (name) => ccToolLookup.get(name.toLowerCase()) ?? name;
|
|
62
37
|
/**
|
|
63
38
|
* Convert content blocks to Anthropic API format
|
|
64
39
|
*/
|
|
@@ -144,8 +119,8 @@ async function* iterateAnthropicEvents(response, requireMessageStop = false) {
|
|
|
144
119
|
else if (event.type === "message_stop") sawMessageEnd = true;
|
|
145
120
|
yield event;
|
|
146
121
|
} catch (error) {
|
|
147
|
-
|
|
148
|
-
throw
|
|
122
|
+
if (error instanceof SyntaxError) throw new Error(MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE, { cause: error });
|
|
123
|
+
throw error;
|
|
149
124
|
}
|
|
150
125
|
}
|
|
151
126
|
if ((sawMessageStart || requireMessageStop) && !sawMessageEnd) throw new Error("Anthropic stream ended before message_stop");
|
|
@@ -182,6 +157,7 @@ const streamAnthropic = (model, context, options) => {
|
|
|
182
157
|
const eventSink = refusalBuffer ?? stream;
|
|
183
158
|
let costModel = model;
|
|
184
159
|
let messageStartPromptUsage;
|
|
160
|
+
let usedCompactionReplay = false;
|
|
185
161
|
try {
|
|
186
162
|
let client;
|
|
187
163
|
let isOAuth;
|
|
@@ -206,6 +182,7 @@ const streamAnthropic = (model, context, options) => {
|
|
|
206
182
|
serverSideFallback = created.serverSideFallback;
|
|
207
183
|
}
|
|
208
184
|
const builtParams = await buildParams(model, requestContext, isOAuth, requestOptions, serverSideFallback);
|
|
185
|
+
usedCompactionReplay = builtParams.usedCompactionReplay;
|
|
209
186
|
let params = builtParams.params;
|
|
210
187
|
const toolProjection = builtParams.toolProjection;
|
|
211
188
|
const nextParams = await requestOptions?.onPayload?.(params, model);
|
|
@@ -226,34 +203,19 @@ const streamAnthropic = (model, context, options) => {
|
|
|
226
203
|
}, model);
|
|
227
204
|
const blocks = output.content;
|
|
228
205
|
const blockIndexes = /* @__PURE__ */ new Map();
|
|
206
|
+
const compactionCapture = createCompactionCapture(output, model, requestOptions);
|
|
229
207
|
for await (const event of iterateAnthropicEvents(response, refusalBuffer !== void 0)) if (event.type === "message_start") {
|
|
230
208
|
output.responseId = event.message.id;
|
|
231
209
|
output.responseModel = event.message.model;
|
|
232
|
-
|
|
233
|
-
const messageStartPromptTokens = promptUsage ? promptUsage.input + promptUsage.cacheRead + promptUsage.cacheWrite : 0;
|
|
234
|
-
messageStartPromptUsage = messageStartPromptTokens > 0 ? promptUsage : void 0;
|
|
235
|
-
const inputTokens = readAnthropicUsageTokenCount(event.message.usage.input_tokens);
|
|
236
|
-
if (inputTokens !== void 0) output.usage.input = inputTokens;
|
|
237
|
-
const outputTokens = readAnthropicUsageTokenCount(event.message.usage.output_tokens);
|
|
238
|
-
if (outputTokens !== void 0) output.usage.output = outputTokens;
|
|
239
|
-
const cacheReadTokens = event.message.usage.cache_read_input_tokens == null ? 0 : readAnthropicUsageTokenCount(event.message.usage.cache_read_input_tokens);
|
|
240
|
-
if (cacheReadTokens !== void 0) output.usage.cacheRead = cacheReadTokens;
|
|
241
|
-
const cacheWriteTokens = event.message.usage.cache_creation_input_tokens == null ? 0 : readAnthropicUsageTokenCount(event.message.usage.cache_creation_input_tokens);
|
|
242
|
-
if (cacheWriteTokens !== void 0) output.usage.cacheWrite = cacheWriteTokens;
|
|
243
|
-
const cacheWriteUsage = readAnthropicCacheWriteUsage(event.message.usage);
|
|
244
|
-
if (cacheWriteUsage.cacheWrite1h !== void 0) output.usage.cacheWrite1h = cacheWriteUsage.cacheWrite1h;
|
|
245
|
-
output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
246
|
-
if (messageStartPromptUsage && outputTokens !== void 0) output.usage.contextUsage = {
|
|
247
|
-
state: "available",
|
|
248
|
-
promptTokens: messageStartPromptTokens,
|
|
249
|
-
totalTokens: messageStartPromptTokens + output.usage.output
|
|
250
|
-
};
|
|
210
|
+
messageStartPromptUsage = applyAnthropicMessageStartUsage(output.usage, event.message.usage);
|
|
251
211
|
calculateCost(costModel, output.usage);
|
|
252
212
|
eventSink.push({
|
|
253
213
|
type: "start",
|
|
254
214
|
partial: output
|
|
255
215
|
});
|
|
256
216
|
} else if (event.type === "content_block_start") {
|
|
217
|
+
const rawContentBlock = event.content_block;
|
|
218
|
+
if (requestOptions?.anthropicServerCompaction === true && compactionCapture.begin(event.index, rawContentBlock, output.content.length)) continue;
|
|
257
219
|
const fallbackBoundary = refusalBuffer ? readAnthropicFallbackBoundary(event.content_block) : null;
|
|
258
220
|
if (fallbackBoundary) {
|
|
259
221
|
refusalBuffer?.discard();
|
|
@@ -357,7 +319,9 @@ const streamAnthropic = (model, context, options) => {
|
|
|
357
319
|
});
|
|
358
320
|
}
|
|
359
321
|
} else if (event.type === "content_block_delta") {
|
|
360
|
-
|
|
322
|
+
const rawDelta = event.delta;
|
|
323
|
+
if (compactionCapture.delta(event.index, rawDelta)) continue;
|
|
324
|
+
else if (event.delta.type === "text_delta") {
|
|
361
325
|
const index = blockIndexes.get(event.index);
|
|
362
326
|
const block = index === void 0 ? void 0 : blocks[index];
|
|
363
327
|
if (index !== void 0 && block?.type === "text") {
|
|
@@ -403,6 +367,7 @@ const streamAnthropic = (model, context, options) => {
|
|
|
403
367
|
}
|
|
404
368
|
}
|
|
405
369
|
} else if (event.type === "content_block_stop") {
|
|
370
|
+
if (compactionCapture.complete(event.index)) continue;
|
|
406
371
|
const index = blockIndexes.get(event.index);
|
|
407
372
|
const block = index === void 0 ? void 0 : blocks[index];
|
|
408
373
|
if (index !== void 0 && block) {
|
|
@@ -433,35 +398,8 @@ const streamAnthropic = (model, context, options) => {
|
|
|
433
398
|
}
|
|
434
399
|
} else if (event.type === "message_delta") {
|
|
435
400
|
if (event.delta.stop_reason) if (event.delta.stop_reason === "refusal") applyAnthropicRefusal(output, event.delta.stop_details, model.provider);
|
|
436
|
-
else output.stopReason =
|
|
437
|
-
if (event.usage)
|
|
438
|
-
const inputTokens = readAnthropicUsageTokenCount(event.usage.input_tokens);
|
|
439
|
-
if (inputTokens !== void 0) output.usage.input = inputTokens;
|
|
440
|
-
const outputTokens = readAnthropicUsageTokenCount(event.usage.output_tokens);
|
|
441
|
-
if (outputTokens !== void 0) output.usage.output = outputTokens;
|
|
442
|
-
const cacheReadTokens = readAnthropicUsageTokenCount(event.usage.cache_read_input_tokens);
|
|
443
|
-
if (cacheReadTokens !== void 0) output.usage.cacheRead = cacheReadTokens;
|
|
444
|
-
const cacheWriteTokens = readAnthropicUsageTokenCount(event.usage.cache_creation_input_tokens);
|
|
445
|
-
if (cacheWriteTokens !== void 0) output.usage.cacheWrite = cacheWriteTokens;
|
|
446
|
-
const cacheWriteUsage = readAnthropicCacheWriteUsage(event.usage);
|
|
447
|
-
if (cacheWriteUsage.cacheWrite1h !== void 0) output.usage.cacheWrite1h = cacheWriteUsage.cacheWrite1h;
|
|
448
|
-
output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
449
|
-
const iterationUsage = readLastAnthropicIterationUsage(event.usage);
|
|
450
|
-
if (iterationUsage.state === "valid") output.usage.contextUsage = {
|
|
451
|
-
state: "available",
|
|
452
|
-
promptTokens: iterationUsage.usage.contextPromptTokens,
|
|
453
|
-
totalTokens: iterationUsage.usage.totalTokens
|
|
454
|
-
};
|
|
455
|
-
else if (iterationUsage.state === "invalid") output.usage.contextUsage = { state: "unavailable" };
|
|
456
|
-
else if (outputTokens !== void 0 && (messageStartPromptUsage !== void 0 || inputTokens !== void 0 && cacheReadTokens !== void 0 && cacheWriteTokens !== void 0)) {
|
|
457
|
-
const promptTokens = output.usage.input + output.usage.cacheRead + output.usage.cacheWrite;
|
|
458
|
-
output.usage.contextUsage = {
|
|
459
|
-
state: "available",
|
|
460
|
-
promptTokens,
|
|
461
|
-
totalTokens: promptTokens + output.usage.output
|
|
462
|
-
};
|
|
463
|
-
} else output.usage.contextUsage = { state: "unavailable" };
|
|
464
|
-
}
|
|
401
|
+
else output.stopReason = mapAnthropicStopReason(event.delta.stop_reason);
|
|
402
|
+
if (event.usage) applyAnthropicMessageDeltaUsage(output.usage, event.usage, messageStartPromptUsage);
|
|
465
403
|
calculateCost(costModel, output.usage);
|
|
466
404
|
}
|
|
467
405
|
if (requestOptions?.signal?.aborted) throw transportAbortError(requestOptions.signal);
|
|
@@ -482,11 +420,12 @@ const streamAnthropic = (model, context, options) => {
|
|
|
482
420
|
refusalBuffer.discard();
|
|
483
421
|
output.content = [];
|
|
484
422
|
}
|
|
485
|
-
|
|
486
|
-
|
|
423
|
+
if (usedCompactionReplay && isAnthropicReplayRejection(error)) suppressAnthropicCompaction(output, model, requestOptions);
|
|
424
|
+
const terminal = projectProviderError(error, requestOptions?.signal);
|
|
425
|
+
Object.assign(output, terminal);
|
|
487
426
|
stream.push({
|
|
488
427
|
type: "error",
|
|
489
|
-
reason:
|
|
428
|
+
reason: terminal.stopReason,
|
|
490
429
|
error: output
|
|
491
430
|
});
|
|
492
431
|
stream.end();
|
|
@@ -494,18 +433,8 @@ const streamAnthropic = (model, context, options) => {
|
|
|
494
433
|
})();
|
|
495
434
|
return stream;
|
|
496
435
|
};
|
|
497
|
-
function normalizeAnthropicToolChoice(thinkingEnabled, toolChoice) {
|
|
498
|
-
if (thinkingEnabled && (toolChoice === "any" || typeof toolChoice === "object" && toolChoice.type === "tool")) return { type: "auto" };
|
|
499
|
-
return typeof toolChoice === "string" ? { type: toolChoice } : toolChoice;
|
|
500
|
-
}
|
|
501
|
-
/**
|
|
502
|
-
* Check if a model supports adaptive thinking (Fable 5, Opus 4.6+, Sonnet 4.6).
|
|
503
|
-
*/
|
|
504
|
-
function supportsAdaptiveThinking(model) {
|
|
505
|
-
return supportsClaudeAdaptiveThinking(model);
|
|
506
|
-
}
|
|
507
436
|
function normalizeAnthropicThinkingOptions(model, options) {
|
|
508
|
-
if (options?.thinkingEnabled !== true ||
|
|
437
|
+
if (options?.thinkingEnabled !== true || supportsClaudeAdaptiveThinking(model)) return options;
|
|
509
438
|
const budgetTokens = options.thinkingBudgetTokens ?? ANTHROPIC_MIN_THINKING_BUDGET_TOKENS;
|
|
510
439
|
const maxTokens = options.maxTokens ?? model.maxTokens;
|
|
511
440
|
if (budgetTokens >= ANTHROPIC_MIN_THINKING_BUDGET_TOKENS && budgetTokens < maxTokens) return options;
|
|
@@ -515,41 +444,13 @@ function normalizeAnthropicThinkingOptions(model, options) {
|
|
|
515
444
|
thinkingBudgetTokens: void 0
|
|
516
445
|
};
|
|
517
446
|
}
|
|
518
|
-
function supportsNativeXhighEffort(model) {
|
|
519
|
-
return supportsClaudeNativeXhighEffort(model);
|
|
520
|
-
}
|
|
521
|
-
/**
|
|
522
|
-
* Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
|
|
523
|
-
* Model metadata owns the provider-specific extended effort mapping.
|
|
524
|
-
*/
|
|
525
|
-
function mapThinkingLevelToEffort(model, level) {
|
|
526
|
-
const requestedLevel = level;
|
|
527
|
-
const hasCanonicalAlias = typeof model.params?.canonicalModelId === "string";
|
|
528
|
-
const thinkingLevelMap = resolveClaudeNativeThinkingLevelMap(model);
|
|
529
|
-
const clampModel = {
|
|
530
|
-
...model,
|
|
531
|
-
...hasCanonicalAlias ? { reasoning: true } : {},
|
|
532
|
-
...thinkingLevelMap ? { thinkingLevelMap } : {}
|
|
533
|
-
};
|
|
534
|
-
const clampedLevel = requestedLevel ? clampThinkingLevel(clampModel, requestedLevel) : requestedLevel;
|
|
535
|
-
const mapped = clampedLevel ? thinkingLevelMap?.[clampedLevel] : void 0;
|
|
536
|
-
if (typeof mapped === "string") return mapped;
|
|
537
|
-
switch (clampedLevel) {
|
|
538
|
-
case "off":
|
|
539
|
-
case "minimal":
|
|
540
|
-
case "low": return "low";
|
|
541
|
-
case "medium": return "medium";
|
|
542
|
-
case "high": return "high";
|
|
543
|
-
case "xhigh": return supportsNativeXhighEffort(model) ? "xhigh" : "high";
|
|
544
|
-
case "max": return supportsClaudeNativeMaxEffort(model) ? "max" : "high";
|
|
545
|
-
default: return "high";
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
447
|
const streamSimpleAnthropic = (model, context, options) => {
|
|
549
448
|
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
550
449
|
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
551
450
|
const base = {
|
|
552
451
|
...buildBaseOptions(model, options, apiKey),
|
|
452
|
+
anthropicServerCompaction: options?.anthropicServerCompaction,
|
|
453
|
+
authProfileId: options?.authProfileId,
|
|
553
454
|
maxTokens: clampMaxTokensToModel(model, options?.maxTokens ?? model.maxTokens),
|
|
554
455
|
toolChoice: options?.toolChoice
|
|
555
456
|
};
|
|
@@ -562,15 +463,15 @@ const streamSimpleAnthropic = (model, context, options) => {
|
|
|
562
463
|
if (resolveClaudeOpus5ModelIdentity(model) || resolveClaudeSonnet5ModelIdentity(model)) return streamAnthropic(model, context, {
|
|
563
464
|
...base,
|
|
564
465
|
thinkingEnabled: true,
|
|
565
|
-
effort:
|
|
466
|
+
effort: resolveAnthropicThinkingEffort(model, reasoning ?? "high")
|
|
566
467
|
});
|
|
567
468
|
if (!reasoning) return streamAnthropic(model, context, {
|
|
568
469
|
...base,
|
|
569
470
|
thinkingEnabled: mandatoryAdaptiveThinking,
|
|
570
471
|
...mandatoryAdaptiveThinking ? { effort: "high" } : {}
|
|
571
472
|
});
|
|
572
|
-
if (
|
|
573
|
-
const effort =
|
|
473
|
+
if (supportsClaudeAdaptiveThinking(model)) {
|
|
474
|
+
const effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
574
475
|
return streamAnthropic(model, context, {
|
|
575
476
|
...base,
|
|
576
477
|
thinkingEnabled: true,
|
|
@@ -608,7 +509,7 @@ function supportsAnthropicServerSideFallback(model) {
|
|
|
608
509
|
return isAnthropicPublicEndpoint(model.baseUrl);
|
|
609
510
|
}
|
|
610
511
|
function createClient(model, apiKey, thinkingEnabled, interleavedThinking, useFineGrainedToolStreamingBeta, optionsHeaders, dynamicHeaders, sessionId) {
|
|
611
|
-
const needsInterleavedBeta = interleavedThinking && !
|
|
512
|
+
const needsInterleavedBeta = interleavedThinking && !supportsClaudeAdaptiveThinking(model);
|
|
612
513
|
const betaFeatures = [];
|
|
613
514
|
if (useFineGrainedToolStreamingBeta) betaFeatures.push(FINE_GRAINED_TOOL_STREAMING_BETA);
|
|
614
515
|
if (needsInterleavedBeta) betaFeatures.push(INTERLEAVED_THINKING_BETA);
|
|
@@ -680,7 +581,7 @@ function createClient(model, apiKey, thinkingEnabled, interleavedThinking, useFi
|
|
|
680
581
|
"oauth-2025-04-20",
|
|
681
582
|
...betaFeatures
|
|
682
583
|
].join(","),
|
|
683
|
-
"user-agent": `claude-cli/${
|
|
584
|
+
"user-agent": `claude-cli/${ANTHROPIC_CLAUDE_CODE_VERSION}`,
|
|
684
585
|
"x-app": "cli"
|
|
685
586
|
}, model.headers, optionsHeaders),
|
|
686
587
|
fetch
|
|
@@ -720,21 +621,26 @@ async function buildParams(model, context, isOAuthTokenResult, options, serverSi
|
|
|
720
621
|
const systemCacheControlCount = countNativeCacheControlMarkers(system);
|
|
721
622
|
const toolCacheControlCount = countNativeCacheControlMarkers(tools);
|
|
722
623
|
const messageCacheControlLimit = Math.max(0, ANTHROPIC_CACHE_CONTROL_LIMIT - systemCacheControlCount - toolCacheControlCount);
|
|
624
|
+
const replayPlan = buildAnthropicReplayPlan(context.messages, model, {
|
|
625
|
+
enabled: !isOAuthTokenResult && options?.anthropicServerCompaction === true,
|
|
626
|
+
authProfileId: options?.authProfileId,
|
|
627
|
+
sessionId: options?.sessionId
|
|
628
|
+
});
|
|
723
629
|
const params = {
|
|
724
630
|
model: model.id,
|
|
725
|
-
messages: await convertMessages(
|
|
631
|
+
messages: await convertMessages(replayPlan.messages, model, isOAuthTokenResult, cacheControl, messageCacheControlLimit, replayThinkingEnabled, compat.allowEmptySignature, replayPlan.compaction),
|
|
726
632
|
max_tokens: options?.maxTokens ?? model.maxTokens,
|
|
727
633
|
stream: true
|
|
728
634
|
};
|
|
729
635
|
if (system) params.system = system;
|
|
730
636
|
if (serverSideFallback) params.fallbacks = ANTHROPIC_SERVER_SIDE_FALLBACKS;
|
|
731
|
-
if (options?.temperature !== void 0 && !options?.thinkingEnabled && !
|
|
637
|
+
if (options?.temperature !== void 0 && !options?.thinkingEnabled && !supportsClaudeNativeXhighEffort(model)) params.temperature = options.temperature;
|
|
732
638
|
if (options?.stop !== void 0 && options.stop.length > 0) params.stop_sequences = options.stop;
|
|
733
639
|
if (tools && tools.length > 0) params.tools = tools;
|
|
734
|
-
if (mandatoryAdaptiveThinking || model.reasoning ||
|
|
640
|
+
if (mandatoryAdaptiveThinking || model.reasoning || supportsClaudeAdaptiveThinking(model)) {
|
|
735
641
|
if (mandatoryAdaptiveThinking || options?.thinkingEnabled) {
|
|
736
642
|
const display = options?.thinkingDisplay ?? "summarized";
|
|
737
|
-
if (
|
|
643
|
+
if (supportsClaudeAdaptiveThinking(model)) {
|
|
738
644
|
params.thinking = {
|
|
739
645
|
type: "adaptive",
|
|
740
646
|
display
|
|
@@ -759,17 +665,15 @@ async function buildParams(model, context, isOAuthTokenResult, options, serverSi
|
|
|
759
665
|
}
|
|
760
666
|
return {
|
|
761
667
|
params,
|
|
762
|
-
toolProjection
|
|
668
|
+
toolProjection,
|
|
669
|
+
usedCompactionReplay: replayPlan.compaction !== void 0
|
|
763
670
|
};
|
|
764
671
|
}
|
|
765
|
-
function
|
|
766
|
-
return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
|
|
767
|
-
}
|
|
768
|
-
async function convertMessages(messages, model, isOAuthTokenValue, cacheControl, messageCacheControlLimit = 4, replayThinkingEnabled = true, allowEmptySignature = false) {
|
|
672
|
+
async function convertMessages(messages, model, isOAuthTokenValue, cacheControl, messageCacheControlLimit = 4, replayThinkingEnabled = true, allowEmptySignature = false, compaction) {
|
|
769
673
|
const params = [];
|
|
770
674
|
const imageBudget = createAnthropicInlineImageBudget();
|
|
771
675
|
const cacheBreakpointOptOutParamIndexes = /* @__PURE__ */ new Set();
|
|
772
|
-
const transformedMessages =
|
|
676
|
+
const transformedMessages = transformProviderMessages(messages, model, normalizeAnthropicToolCallId);
|
|
773
677
|
const activeToolTurnAssistantIndex = replayThinkingEnabled ? -1 : findActiveAnthropicToolTurnAssistantIndex(transformedMessages);
|
|
774
678
|
for (let i = 0; i < transformedMessages.length; i++) {
|
|
775
679
|
const msg = transformedMessages[i];
|
|
@@ -810,7 +714,7 @@ async function convertMessages(messages, model, isOAuthTokenValue, cacheControl,
|
|
|
810
714
|
});
|
|
811
715
|
}
|
|
812
716
|
} else if (msg.role === "assistant") {
|
|
813
|
-
const blocks = [];
|
|
717
|
+
const blocks = i === 0 && compaction ? [compaction] : [];
|
|
814
718
|
let omittedThinking = false;
|
|
815
719
|
for (const block of msg.content) if (block.type === "text") {
|
|
816
720
|
if (block.text.trim().length === 0) continue;
|
|
@@ -849,7 +753,7 @@ async function convertMessages(messages, model, isOAuthTokenValue, cacheControl,
|
|
|
849
753
|
} else if (block.type === "toolCall") blocks.push({
|
|
850
754
|
type: "tool_use",
|
|
851
755
|
id: block.id,
|
|
852
|
-
name: isOAuthTokenValue ?
|
|
756
|
+
name: isOAuthTokenValue ? toClaudeCodeToolName(block.name) : block.name,
|
|
853
757
|
input: block.arguments ?? {}
|
|
854
758
|
});
|
|
855
759
|
if (blocks.length === 0 && omittedThinking) blocks.push({
|
|
@@ -888,55 +792,15 @@ async function convertMessages(messages, model, isOAuthTokenValue, cacheControl,
|
|
|
888
792
|
});
|
|
889
793
|
}
|
|
890
794
|
}
|
|
891
|
-
if (cacheControl
|
|
892
|
-
let fallbackToolResult;
|
|
893
|
-
for (let i = params.length - 1; i >= 0; i--) {
|
|
894
|
-
const message = params[i];
|
|
895
|
-
if (!message || message.role !== "user" || cacheBreakpointOptOutParamIndexes.has(i)) continue;
|
|
896
|
-
if (Array.isArray(message.content)) {
|
|
897
|
-
for (let j = message.content.length - 1; j >= 0; j--) {
|
|
898
|
-
const block = message.content[j];
|
|
899
|
-
if (!block) continue;
|
|
900
|
-
if (block.type === "text" || block.type === "image") {
|
|
901
|
-
if (fallbackToolResult && messageCacheControlLimit === 1) {
|
|
902
|
-
applyContentBlockCacheControl(fallbackToolResult, cacheControl);
|
|
903
|
-
return params;
|
|
904
|
-
}
|
|
905
|
-
applyContentBlockCacheControl(block, cacheControl);
|
|
906
|
-
if (fallbackToolResult && messageCacheControlLimit > 1) applyContentBlockCacheControl(fallbackToolResult, cacheControl);
|
|
907
|
-
return params;
|
|
908
|
-
}
|
|
909
|
-
if (block.type === "tool_result" && fallbackToolResult === void 0) fallbackToolResult = block;
|
|
910
|
-
}
|
|
911
|
-
continue;
|
|
912
|
-
}
|
|
913
|
-
if (typeof message.content === "string") {
|
|
914
|
-
if (fallbackToolResult && messageCacheControlLimit === 1) {
|
|
915
|
-
applyContentBlockCacheControl(fallbackToolResult, cacheControl);
|
|
916
|
-
return params;
|
|
917
|
-
}
|
|
918
|
-
message.content = [{
|
|
919
|
-
type: "text",
|
|
920
|
-
text: message.content,
|
|
921
|
-
cache_control: cacheControl
|
|
922
|
-
}];
|
|
923
|
-
if (fallbackToolResult && messageCacheControlLimit > 1) applyContentBlockCacheControl(fallbackToolResult, cacheControl);
|
|
924
|
-
return params;
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
if (fallbackToolResult) applyContentBlockCacheControl(fallbackToolResult, cacheControl);
|
|
928
|
-
}
|
|
795
|
+
if (cacheControl) applyAnthropicCacheControlToMessages(params, cacheControl, messageCacheControlLimit, cacheBreakpointOptOutParamIndexes);
|
|
929
796
|
return params;
|
|
930
797
|
}
|
|
931
|
-
function applyContentBlockCacheControl(block, cacheControl) {
|
|
932
|
-
block.cache_control = cacheControl;
|
|
933
|
-
}
|
|
934
798
|
function buildAnthropicSystemBlocks(systemPrompt, isOAuthTokenResult, cacheControl) {
|
|
935
799
|
const blocks = [];
|
|
936
800
|
if (isOAuthTokenResult) {
|
|
937
801
|
blocks.push({
|
|
938
802
|
type: "text",
|
|
939
|
-
text:
|
|
803
|
+
text: ANTHROPIC_CLAUDE_CODE_BILLING_SYSTEM_BLOCK
|
|
940
804
|
});
|
|
941
805
|
blocks.push({
|
|
942
806
|
type: "text",
|
|
@@ -983,7 +847,7 @@ function shouldUseFineGrainedToolStreamingBeta(model, context) {
|
|
|
983
847
|
return Boolean(context.tools?.length) && !getAnthropicCompat(model).supportsEagerToolInputStreaming;
|
|
984
848
|
}
|
|
985
849
|
function convertTools(tools, isOAuthTokenLocal, supportsEagerToolInputStreaming, cacheControl) {
|
|
986
|
-
const projection = projectAnthropicTools(tools, (name) => isOAuthTokenLocal ?
|
|
850
|
+
const projection = projectAnthropicTools(tools, (name) => isOAuthTokenLocal ? toClaudeCodeToolName(name) : name);
|
|
987
851
|
const convertedTools = [];
|
|
988
852
|
for (const [index, tool] of projection.tools.entries()) {
|
|
989
853
|
const convertedTool = {
|
|
@@ -1000,17 +864,5 @@ function convertTools(tools, isOAuthTokenLocal, supportsEagerToolInputStreaming,
|
|
|
1000
864
|
tools: convertedTools
|
|
1001
865
|
};
|
|
1002
866
|
}
|
|
1003
|
-
function mapStopReason(reason) {
|
|
1004
|
-
switch (reason) {
|
|
1005
|
-
case "end_turn": return "stop";
|
|
1006
|
-
case "max_tokens": return "length";
|
|
1007
|
-
case "tool_use": return "toolUse";
|
|
1008
|
-
case "refusal": return "error";
|
|
1009
|
-
case "pause_turn": return "stop";
|
|
1010
|
-
case "stop_sequence": return "stop";
|
|
1011
|
-
case "sensitive": return "error";
|
|
1012
|
-
default: throw new Error(`Unhandled stop reason: ${reason}`);
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
867
|
//#endregion
|
|
1016
868
|
export { streamAnthropic as n, streamSimpleAnthropic as r, anthropic_exports as t };
|