@openclaw/ai 2026.9.1 → 2026.9.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-hk7F7ptG.mjs → anthropic-BDdqdVLK.mjs} +208 -205
- package/dist/{anthropic-compaction-replay-OMJZ0uyo.mjs → anthropic-compaction-replay-DB2FGLQc.mjs} +130 -41
- package/dist/{anthropic-payload-policy-DBT1itQ-.d.mts → anthropic-payload-policy-d46X2pR0.d.mts} +27 -3
- package/dist/{api-registry-Cs6HGNqY.d.mts → api-registry-DWtPjzyn.d.mts} +2 -2
- package/dist/{azure-openai-responses-CN4Fy5zV.mjs → azure-openai-responses-C5bZRfAP.mjs} +5 -5
- package/dist/{diagnostics-KhXK-QJI.mjs → diagnostics-DfFyKeX_.mjs} +1 -1
- package/dist/diagnostics.mjs +2 -2
- package/dist/{event-stream-uSMZJ3FA.mjs → event-stream-BgDvQeum.mjs} +3 -9
- package/dist/{event-stream-vK_7r3bj.d.mts → event-stream-BhT5T1Ay.d.mts} +1 -1
- package/dist/event-stream-zctLx0yr.d.mts +1 -0
- package/dist/event-stream.d.mts +2 -2
- package/dist/event-stream.mjs +1 -1
- package/dist/{google-CC-nrwXg.mjs → google-4qeuE8iX.mjs} +4 -4
- package/dist/{google-shared-CjPY0hZM.mjs → google-shared-CWeG8RIl.mjs} +7 -5
- package/dist/{google-vertex-DCr0pyzQ.mjs → google-vertex-O9BcnB6X.mjs} +4 -4
- package/dist/{host-BIaiBURL.mjs → host-CEvLw30U.mjs} +54 -9
- package/dist/{host-BztR4tQj.d.mts → host-DjzGmdZ2.d.mts} +3 -3
- package/dist/{index-AfaxKT8w.d.mts → index-FnHM2FcI.d.mts} +11 -5
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +4 -4
- package/dist/internal/anthropic.d.mts +12 -9
- package/dist/internal/anthropic.mjs +5 -5
- package/dist/internal/openai-responses-payload-policy.d.mts +1 -1
- package/dist/internal/openai-responses-payload-policy.mjs +1 -1
- package/dist/internal/openai.d.mts +7 -7
- package/dist/internal/openai.mjs +10 -10
- package/dist/internal/runtime.d.mts +5 -5
- package/dist/internal/runtime.mjs +3 -3
- package/dist/internal/shared.d.mts +2 -2
- package/dist/internal/shared.mjs +3 -3
- package/dist/{mistral-B7etBd_H.mjs → mistral-Tb6oalqH.mjs} +10 -8
- package/dist/{openai-chatgpt-responses-BAJ4gq3i.mjs → openai-chatgpt-responses-yUXjPNfu.mjs} +26 -28
- package/dist/{openai-completions-D0QZ0AyB.mjs → openai-completions-BIUV3RDT.mjs} +8 -8
- package/dist/{openai-completions-compat-4IjSBpr6.d.mts → openai-completions-compat-CpjYUArk.d.mts} +3 -3
- package/dist/{openai-completions-stream-DZjwK8vp.mjs → openai-completions-stream-BsrBe3Gg.mjs} +16 -16
- package/dist/openai-prompt-cache-CGnVB74a.mjs +21 -0
- package/dist/openai-prompt-cache-CNoIfHYC.d.mts +16 -0
- package/dist/{openai-responses-BEUlxfDu.mjs → openai-responses-CCL41ALM.mjs} +8 -11
- package/dist/{openai-responses-compaction-window-DO7yV7az.mjs → openai-responses-compaction-window-BMVHFOtq.mjs} +18 -7
- package/dist/{openai-responses-contracts-bPzSN_Ba.d.mts → openai-responses-contracts-rF5DDRNS.d.mts} +12 -4
- package/dist/{openai-responses-prompt-observer-internal-C7x4IK7r.mjs → openai-responses-prompt-observer-internal-D0bhBfgL.mjs} +3 -3
- package/dist/{openai-responses-shared-DiAdpNAM.mjs → openai-responses-shared-Bmma_3Qc.mjs} +185 -109
- package/dist/{openai-tool-schema-BMiHFH36.mjs → openai-tool-schema-_pTAJqKF.mjs} +20 -32
- package/dist/{provider-error-B6EGq6gg.mjs → provider-error-9TraxGvt.mjs} +6 -7
- package/dist/{provider-options-CxFPtvh7.d.mts → provider-options-Bcc_p-TU.d.mts} +18 -6
- package/dist/provider-replay-context-BuSUaAk5.mjs +21 -0
- package/dist/{provider-transcript-transform-WvJmFUAf.mjs → provider-transcript-transform-BaMbI1hr.mjs} +1 -1
- package/dist/provider-types.d.mts +6 -6
- package/dist/providers.d.mts +2 -2
- package/dist/providers.mjs +11 -11
- package/dist/{reasoning-tag-text-partitioner-C-4uedDb.mjs → reasoning-tag-text-partitioner-DLCNXki6.mjs} +72 -24
- package/dist/{sanitize-unicode-S6binQG-.mjs → sanitize-unicode-D6xUvZaS.mjs} +1 -1
- package/dist/{simple-options-0PLDyJ-d.mjs → simple-options-BjHCCh4v.mjs} +2 -2
- package/dist/{src-C8U7lkoa.mjs → src-2qBGKg8O.mjs} +9 -1
- package/dist/{tool-schema-json-projection-BtZiml7r.mjs → tool-schema-json-projection-mJhXDcyz.mjs} +1 -1
- package/dist/{transport-stream-shared-CytPVLIg.mjs → transport-stream-shared-CZqMhfIw.mjs} +4 -4
- package/dist/{transport-stream-shared-BrvFTkoO.d.mts → transport-stream-shared-xnaqxmbP.d.mts} +4 -4
- package/dist/{transport-utils-CtuS1Upe.mjs → transport-utils-7il795_9.mjs} +1 -1
- package/dist/transports.d.mts +11 -10
- package/dist/transports.mjs +567 -253
- package/dist/types-3Lnm-QSJ.d.mts +1 -0
- package/dist/{types-DbrhszyQ.d.mts → types-CJ1-Ht7A.d.mts} +29 -7
- package/dist/types.d.mts +6 -6
- package/dist/types.mjs +3 -3
- package/dist/{validation-BOwtcl9X.d.mts → validation-AKZBDGQd.d.mts} +1 -1
- package/dist/validation.d.mts +1 -1
- package/package.json +2 -2
- package/dist/event-stream-DeDhbCc5.d.mts +0 -1
- package/dist/openai-prompt-cache-2uo_1OR1.d.mts +0 -7
- package/dist/openai-prompt-cache-mZTCdRPo.mjs +0 -12
- package/dist/types-B5EFUmXs.d.mts +0 -1
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import { t as AssistantMessageEventStream } from "./event-stream-
|
|
2
|
+
import { _ as supportsClaudeAdaptiveThinking, b as supportsClaudeNativeXhighEffort, h as resolveClaudeSonnet5ModelIdentity, m as resolveClaudeOpus5ModelIdentity } from "./src-2qBGKg8O.mjs";
|
|
3
|
+
import { t as AssistantMessageEventStream } from "./event-stream-BgDvQeum.mjs";
|
|
4
4
|
import { o as isRecord } from "./utf16-slice-qz3nsy87.mjs";
|
|
5
|
-
import { r as calculateCost, t as sanitizeSurrogates } from "./sanitize-unicode-
|
|
6
|
-
import { S as usesClaudeStreamingRefusalContract, _ as prepareClaudeNoPrefillRequestContext, a as describeToolResultMediaPlaceholder, c as extractToolResultText, d as isImageWithMediaPayload, f as ANTHROPIC_CLAUDE_CODE_BILLING_SYSTEM_BLOCK, g as mapAnthropicStopReason, m as applyClaudeRequestContract, n as getAiTransportHost, p as ANTHROPIC_CLAUDE_CODE_VERSION, r as resolveAiTransportHeaderSentinels, s as extractToolResultBlockText, v as requiresClaudeAdaptiveThinking, x as usesClaudeFable5MessagesContract, y as resolveAnthropicThinkingEffort } from "./host-
|
|
7
|
-
import { A as
|
|
5
|
+
import { r as calculateCost, t as sanitizeSurrogates } from "./sanitize-unicode-D6xUvZaS.mjs";
|
|
6
|
+
import { S as usesClaudeStreamingRefusalContract, _ as prepareClaudeNoPrefillRequestContext, a as describeToolResultMediaPlaceholder, c as extractToolResultText, d as isImageWithMediaPayload, f as ANTHROPIC_CLAUDE_CODE_BILLING_SYSTEM_BLOCK, g as mapAnthropicStopReason, m as applyClaudeRequestContract, n as getAiTransportHost, p as ANTHROPIC_CLAUDE_CODE_VERSION, r as resolveAiTransportHeaderSentinels, s as extractToolResultBlockText, v as requiresClaudeAdaptiveThinking, x as usesClaudeFable5MessagesContract, y as resolveAnthropicThinkingEffort } from "./host-CEvLw30U.mjs";
|
|
7
|
+
import { A as createAnthropicInlineImageBudget, B as resolveAnthropicContextManagementBetaHeader, C as ANTHROPIC_SERVER_SIDE_FALLBACKS, D as readAnthropicFallbackBoundary, E as applyAnthropicFallbackBoundary, G as omitFoundryBearerCredentialHeaders, K as usesFoundryBearerAuth, M as resolveAnthropicImageMediaType, N as applyAnthropicCacheControlToMessages, O as resolveAnthropicFallbackServingModelCost, P as applyAnthropicContextManagementToRequest, R as isDirectAnthropicModel, S as readAnthropicInputTransformations, W as isAnthropicOAuthApiKey, _ as toClaudeCodeToolName, a as suppressAnthropicCompaction, b as findActiveAnthropicToolTurnAssistantIndex, f as normalizeAnthropicToolCallId, g as resolveOriginalAnthropicToolName, h as reconcileAnthropicToolChoice, j as normalizeAnthropicInlineContent, k as applyAnthropicRefusal, m as projectAnthropicTools, n as createCompactionCapture, o as applyAnthropicMessageDeltaUsage, p as normalizeAnthropicToolChoice, r as isAnthropicReplayRejection, s as applyAnthropicMessageStartUsage, t as buildAnthropicReplayPlan, v as ANTHROPIC_OMITTED_REASONING_TEXT, w as ANTHROPIC_SERVER_SIDE_FALLBACK_BETA, x as logAnthropicThinkingDrops, y as applyAnthropicThinkingBindingControls, z as logAnthropicContextEdits } from "./anthropic-compaction-replay-DB2FGLQc.mjs";
|
|
8
8
|
import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
|
|
9
|
-
import { d as stripSystemPromptCacheBoundary, n as buildBaseOptions, r as clampMaxTokensToModel, t as adjustMaxTokensForThinking, u as splitSystemPromptCacheBoundary } from "./simple-options-
|
|
10
|
-
import { t as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "./transport-utils-
|
|
9
|
+
import { d as stripSystemPromptCacheBoundary, n as buildBaseOptions, r as clampMaxTokensToModel, t as adjustMaxTokensForThinking, u as splitSystemPromptCacheBoundary } from "./simple-options-BjHCCh4v.mjs";
|
|
10
|
+
import { t as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "./transport-utils-7il795_9.mjs";
|
|
11
11
|
import { n as getEnvApiKey } from "./env-api-keys-bktO00EJ.mjs";
|
|
12
|
-
import { _ as transportAbortError, f as notifyProviderHttpResponse, s as finalizeTerminalToolCallArguments, t as assignTransportErrorDetails } from "./transport-stream-shared-
|
|
12
|
+
import { _ as transportAbortError, f as notifyProviderHttpResponse, s as finalizeTerminalToolCallArguments, t as assignTransportErrorDetails } from "./transport-stream-shared-CZqMhfIw.mjs";
|
|
13
13
|
import { t as createDeferredEventBuffer } from "./deferred-event-buffer-DAvyP7qA.mjs";
|
|
14
14
|
import { n as parseJsonWithRepair, r as parseStreamingJson, t as createToolArgumentPreviewSchedule } from "./json-parse-BuAJEbdW.mjs";
|
|
15
15
|
import { t as notifyLlmRequestActivity } from "./llm-request-activity-BjtkplhG.mjs";
|
|
16
|
-
import { t as transformProviderMessages } from "./provider-transcript-transform-
|
|
16
|
+
import { t as transformProviderMessages } from "./provider-transcript-transform-BaMbI1hr.mjs";
|
|
17
17
|
import { i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-NCJtz9i0.mjs";
|
|
18
18
|
import Anthropic from "@anthropic-ai/sdk";
|
|
19
19
|
import { Stream } from "@anthropic-ai/sdk/core/streaming.js";
|
|
@@ -108,11 +108,12 @@ const ANTHROPIC_MESSAGE_EVENTS = /* @__PURE__ */ new Set([
|
|
|
108
108
|
"content_block_delta",
|
|
109
109
|
"content_block_stop"
|
|
110
110
|
]);
|
|
111
|
-
async function* iterateAnthropicEvents(response, requireMessageStop = false) {
|
|
111
|
+
async function* iterateAnthropicEvents(response, requireMessageStop = false, signal) {
|
|
112
112
|
if (!response.body) throw new Error("Attempted to iterate over an Anthropic response with no body");
|
|
113
113
|
let sawMessageEnd = false;
|
|
114
114
|
for await (const sse of Stream.rawEvents(response)) {
|
|
115
115
|
if (sse.event === "error") throw new Error(sse.data);
|
|
116
|
+
notifyLlmRequestActivity(signal);
|
|
116
117
|
if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) continue;
|
|
117
118
|
try {
|
|
118
119
|
const event = parseJsonWithRepair(sse.data);
|
|
@@ -153,15 +154,17 @@ const streamAnthropic = (model, context, options) => {
|
|
|
153
154
|
stopReason: "stop",
|
|
154
155
|
timestamp: Date.now()
|
|
155
156
|
};
|
|
156
|
-
const refusalBuffer = usesClaudeStreamingRefusalContract(model) ? createDeferredEventBuffer(stream
|
|
157
|
+
const refusalBuffer = usesClaudeStreamingRefusalContract(model) ? createDeferredEventBuffer(stream) : void 0;
|
|
157
158
|
const eventSink = refusalBuffer ?? stream;
|
|
158
159
|
let costModel = model;
|
|
159
160
|
let messageStartPromptUsage;
|
|
160
161
|
let usedCompactionReplay = false;
|
|
162
|
+
let inputTransformations;
|
|
161
163
|
try {
|
|
162
164
|
let client;
|
|
163
165
|
let isOAuth;
|
|
164
166
|
let serverSideFallback = false;
|
|
167
|
+
let directApiKeyBetaHeader;
|
|
165
168
|
if (requestOptions?.client) {
|
|
166
169
|
client = requestOptions.client;
|
|
167
170
|
isOAuth = false;
|
|
@@ -180,18 +183,22 @@ const streamAnthropic = (model, context, options) => {
|
|
|
180
183
|
client = created.client;
|
|
181
184
|
isOAuth = created.isOAuthToken;
|
|
182
185
|
serverSideFallback = created.serverSideFallback;
|
|
186
|
+
directApiKeyBetaHeader = created.directApiKeyBetaHeader;
|
|
183
187
|
}
|
|
184
188
|
const builtParams = await buildParams(model, requestContext, isOAuth, requestOptions, serverSideFallback);
|
|
185
189
|
usedCompactionReplay = builtParams.usedCompactionReplay;
|
|
186
190
|
let params = builtParams.params;
|
|
187
191
|
const toolProjection = builtParams.toolProjection;
|
|
192
|
+
applyAnthropicContextManagementToRequest(params, model, requestOptions, directApiKeyBetaHeader);
|
|
188
193
|
const nextParams = await requestOptions?.onPayload?.(params, model);
|
|
189
194
|
if (nextParams !== void 0) params = nextParams;
|
|
190
195
|
applyClaudeRequestContract(params, model);
|
|
196
|
+
const betaHeader = resolveAnthropicContextManagementBetaHeader(params, directApiKeyBetaHeader);
|
|
191
197
|
const sdkRequestOptions = {
|
|
192
198
|
...requestOptions?.signal ? { signal: requestOptions.signal } : {},
|
|
193
199
|
...requestOptions?.timeoutMs !== void 0 ? { timeout: requestOptions.timeoutMs } : {},
|
|
194
|
-
maxRetries: 0
|
|
200
|
+
maxRetries: 0,
|
|
201
|
+
headers: applyAnthropicThinkingBindingControls(params, betaHeader) ?? (betaHeader !== void 0 ? { "anthropic-beta": betaHeader } : void 0)
|
|
195
202
|
};
|
|
196
203
|
const response = await client.messages.create({
|
|
197
204
|
...params,
|
|
@@ -207,202 +214,206 @@ const streamAnthropic = (model, context, options) => {
|
|
|
207
214
|
const toolArgumentPreviewSchedules = /* @__PURE__ */ new WeakMap();
|
|
208
215
|
const sealedToolCalls = [];
|
|
209
216
|
const compactionCapture = createCompactionCapture(output, model, requestOptions);
|
|
210
|
-
const requireMessageStop = refusalBuffer !== void 0 ||
|
|
211
|
-
for await (const event of iterateAnthropicEvents(response, requireMessageStop))
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
type: "start",
|
|
218
|
-
partial: output
|
|
219
|
-
});
|
|
220
|
-
} else if (event.type === "content_block_start") {
|
|
221
|
-
const rawContentBlock = isRecord(event.content_block) ? event.content_block : void 0;
|
|
222
|
-
if (requestOptions?.anthropicServerCompaction === true && compactionCapture.begin(event.index, rawContentBlock, output.content.length)) continue;
|
|
223
|
-
const fallbackBoundary = refusalBuffer ? readAnthropicFallbackBoundary(event.content_block) : null;
|
|
224
|
-
if (fallbackBoundary) {
|
|
225
|
-
refusalBuffer?.discard();
|
|
226
|
-
sealedToolCalls.length = 0;
|
|
227
|
-
blockIndexes.clear();
|
|
228
|
-
applyAnthropicFallbackBoundary({
|
|
229
|
-
output,
|
|
230
|
-
boundary: fallbackBoundary,
|
|
231
|
-
provider: model.provider
|
|
232
|
-
});
|
|
233
|
-
costModel = {
|
|
234
|
-
...model,
|
|
235
|
-
cost: resolveAnthropicFallbackServingModelCost({
|
|
236
|
-
requestedModelId: model.id,
|
|
237
|
-
servingModelId: fallbackBoundary.toModel,
|
|
238
|
-
requestedCost: model.cost
|
|
239
|
-
})
|
|
240
|
-
};
|
|
217
|
+
const requireMessageStop = refusalBuffer !== void 0 || isDirectAnthropicModel(model);
|
|
218
|
+
for await (const event of iterateAnthropicEvents(response, requireMessageStop, requestOptions?.signal)) {
|
|
219
|
+
inputTransformations = readAnthropicInputTransformations(event) ?? inputTransformations;
|
|
220
|
+
if (event.type === "message_start") {
|
|
221
|
+
output.responseId = event.message.id;
|
|
222
|
+
output.responseModel = event.message.model;
|
|
223
|
+
messageStartPromptUsage = applyAnthropicMessageStartUsage(output.usage, event.message.usage);
|
|
241
224
|
calculateCost(costModel, output.usage);
|
|
242
225
|
eventSink.push({
|
|
243
226
|
type: "start",
|
|
244
227
|
partial: output
|
|
245
228
|
});
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
229
|
+
} else if (event.type === "content_block_start") {
|
|
230
|
+
const rawContentBlock = isRecord(event.content_block) ? event.content_block : void 0;
|
|
231
|
+
if (requestOptions?.anthropicServerCompaction === true && compactionCapture.begin(event.index, rawContentBlock, output.content.length)) continue;
|
|
232
|
+
const fallbackBoundary = refusalBuffer ? readAnthropicFallbackBoundary(event.content_block) : null;
|
|
233
|
+
if (fallbackBoundary) {
|
|
234
|
+
refusalBuffer?.discard();
|
|
235
|
+
sealedToolCalls.length = 0;
|
|
236
|
+
blockIndexes.clear();
|
|
237
|
+
applyAnthropicFallbackBoundary({
|
|
238
|
+
output,
|
|
239
|
+
boundary: fallbackBoundary,
|
|
240
|
+
provider: model.provider
|
|
241
|
+
});
|
|
242
|
+
costModel = {
|
|
243
|
+
...model,
|
|
244
|
+
cost: resolveAnthropicFallbackServingModelCost({
|
|
245
|
+
requestedModelId: model.id,
|
|
246
|
+
servingModelId: fallbackBoundary.toModel,
|
|
247
|
+
requestedCost: model.cost
|
|
248
|
+
})
|
|
249
|
+
};
|
|
250
|
+
calculateCost(costModel, output.usage);
|
|
251
|
+
eventSink.push({
|
|
252
|
+
type: "start",
|
|
253
|
+
partial: output
|
|
254
|
+
});
|
|
255
|
+
for (const [i, block] of blocks.entries()) {
|
|
256
|
+
if (block.type !== "text") continue;
|
|
257
|
+
delete block.index;
|
|
258
|
+
eventSink.push({
|
|
259
|
+
type: "text_start",
|
|
260
|
+
contentIndex: i,
|
|
261
|
+
partial: output
|
|
262
|
+
});
|
|
263
|
+
if (block.text) eventSink.push({
|
|
264
|
+
type: "text_delta",
|
|
265
|
+
contentIndex: i,
|
|
266
|
+
delta: block.text,
|
|
267
|
+
partial: output
|
|
268
|
+
});
|
|
269
|
+
eventSink.push({
|
|
270
|
+
type: "text_end",
|
|
271
|
+
contentIndex: i,
|
|
272
|
+
content: block.text,
|
|
273
|
+
partial: output
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
} else if (event.content_block.type === "text") {
|
|
277
|
+
const block = {
|
|
278
|
+
type: "text",
|
|
279
|
+
text: "",
|
|
280
|
+
index: event.index
|
|
281
|
+
};
|
|
282
|
+
output.content.push(block);
|
|
283
|
+
blockIndexes.set(event.index, output.content.length - 1);
|
|
249
284
|
eventSink.push({
|
|
250
285
|
type: "text_start",
|
|
251
|
-
contentIndex:
|
|
286
|
+
contentIndex: output.content.length - 1,
|
|
252
287
|
partial: output
|
|
253
288
|
});
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
289
|
+
} else if (event.content_block.type === "thinking") {
|
|
290
|
+
const block = {
|
|
291
|
+
type: "thinking",
|
|
292
|
+
thinking: "",
|
|
293
|
+
thinkingSignature: "",
|
|
294
|
+
index: event.index
|
|
295
|
+
};
|
|
296
|
+
output.content.push(block);
|
|
297
|
+
blockIndexes.set(event.index, output.content.length - 1);
|
|
298
|
+
eventSink.push({
|
|
299
|
+
type: "thinking_start",
|
|
300
|
+
contentIndex: output.content.length - 1,
|
|
258
301
|
partial: output
|
|
259
302
|
});
|
|
303
|
+
} else if (event.content_block.type === "redacted_thinking") {
|
|
304
|
+
const block = {
|
|
305
|
+
type: "thinking",
|
|
306
|
+
thinking: "[Reasoning redacted]",
|
|
307
|
+
thinkingSignature: event.content_block.data,
|
|
308
|
+
redacted: true,
|
|
309
|
+
index: event.index
|
|
310
|
+
};
|
|
311
|
+
output.content.push(block);
|
|
312
|
+
blockIndexes.set(event.index, output.content.length - 1);
|
|
260
313
|
eventSink.push({
|
|
261
|
-
type: "
|
|
262
|
-
contentIndex:
|
|
263
|
-
content: block.text,
|
|
314
|
+
type: "thinking_start",
|
|
315
|
+
contentIndex: output.content.length - 1,
|
|
264
316
|
partial: output
|
|
265
317
|
});
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
partial: output
|
|
279
|
-
});
|
|
280
|
-
} else if (event.content_block.type === "thinking") {
|
|
281
|
-
const block = {
|
|
282
|
-
type: "thinking",
|
|
283
|
-
thinking: "",
|
|
284
|
-
thinkingSignature: "",
|
|
285
|
-
index: event.index
|
|
286
|
-
};
|
|
287
|
-
output.content.push(block);
|
|
288
|
-
blockIndexes.set(event.index, output.content.length - 1);
|
|
289
|
-
eventSink.push({
|
|
290
|
-
type: "thinking_start",
|
|
291
|
-
contentIndex: output.content.length - 1,
|
|
292
|
-
partial: output
|
|
293
|
-
});
|
|
294
|
-
} else if (event.content_block.type === "redacted_thinking") {
|
|
295
|
-
const block = {
|
|
296
|
-
type: "thinking",
|
|
297
|
-
thinking: "[Reasoning redacted]",
|
|
298
|
-
thinkingSignature: event.content_block.data,
|
|
299
|
-
redacted: true,
|
|
300
|
-
index: event.index
|
|
301
|
-
};
|
|
302
|
-
output.content.push(block);
|
|
303
|
-
blockIndexes.set(event.index, output.content.length - 1);
|
|
304
|
-
eventSink.push({
|
|
305
|
-
type: "thinking_start",
|
|
306
|
-
contentIndex: output.content.length - 1,
|
|
307
|
-
partial: output
|
|
308
|
-
});
|
|
309
|
-
} else if (event.content_block.type === "tool_use") {
|
|
310
|
-
const block = {
|
|
311
|
-
type: "toolCall",
|
|
312
|
-
id: event.content_block.id,
|
|
313
|
-
name: isOAuth ? resolveOriginalAnthropicToolName(event.content_block.name, toolProjection) : event.content_block.name,
|
|
314
|
-
arguments: event.content_block.input ?? {},
|
|
315
|
-
partialJson: "",
|
|
316
|
-
index: event.index
|
|
317
|
-
};
|
|
318
|
-
output.content.push(block);
|
|
319
|
-
blockIndexes.set(event.index, output.content.length - 1);
|
|
320
|
-
toolArgumentPreviewSchedules.set(block, createToolArgumentPreviewSchedule());
|
|
321
|
-
eventSink.push({
|
|
322
|
-
type: "toolcall_start",
|
|
323
|
-
contentIndex: output.content.length - 1,
|
|
324
|
-
partial: output
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
} else if (event.type === "content_block_delta") {
|
|
328
|
-
const rawDelta = isRecord(event.delta) ? event.delta : void 0;
|
|
329
|
-
if (compactionCapture.delta(event.index, rawDelta)) continue;
|
|
330
|
-
else if (event.delta.type === "text_delta") {
|
|
331
|
-
const index = blockIndexes.get(event.index);
|
|
332
|
-
const block = index === void 0 ? void 0 : blocks[index];
|
|
333
|
-
if (index !== void 0 && block?.type === "text") {
|
|
334
|
-
block.text += event.delta.text;
|
|
318
|
+
} else if (event.content_block.type === "tool_use") {
|
|
319
|
+
const block = {
|
|
320
|
+
type: "toolCall",
|
|
321
|
+
id: event.content_block.id,
|
|
322
|
+
name: isOAuth ? resolveOriginalAnthropicToolName(event.content_block.name, toolProjection) : event.content_block.name,
|
|
323
|
+
arguments: event.content_block.input ?? {},
|
|
324
|
+
partialJson: "",
|
|
325
|
+
index: event.index
|
|
326
|
+
};
|
|
327
|
+
output.content.push(block);
|
|
328
|
+
blockIndexes.set(event.index, output.content.length - 1);
|
|
329
|
+
toolArgumentPreviewSchedules.set(block, createToolArgumentPreviewSchedule());
|
|
335
330
|
eventSink.push({
|
|
336
|
-
type: "
|
|
337
|
-
contentIndex:
|
|
338
|
-
delta: event.delta.text,
|
|
331
|
+
type: "toolcall_start",
|
|
332
|
+
contentIndex: output.content.length - 1,
|
|
339
333
|
partial: output
|
|
340
334
|
});
|
|
341
335
|
}
|
|
342
|
-
} else if (event.
|
|
336
|
+
} else if (event.type === "content_block_delta") {
|
|
337
|
+
const rawDelta = isRecord(event.delta) ? event.delta : void 0;
|
|
338
|
+
if (compactionCapture.delta(event.index, rawDelta)) continue;
|
|
339
|
+
else if (event.delta.type === "text_delta") {
|
|
340
|
+
const index = blockIndexes.get(event.index);
|
|
341
|
+
const block = index === void 0 ? void 0 : blocks[index];
|
|
342
|
+
if (index !== void 0 && block?.type === "text") {
|
|
343
|
+
block.text += event.delta.text;
|
|
344
|
+
eventSink.push({
|
|
345
|
+
type: "text_delta",
|
|
346
|
+
contentIndex: index,
|
|
347
|
+
delta: event.delta.text,
|
|
348
|
+
partial: output
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
} else if (event.delta.type === "thinking_delta") {
|
|
352
|
+
const index = blockIndexes.get(event.index);
|
|
353
|
+
const block = index === void 0 ? void 0 : blocks[index];
|
|
354
|
+
if (index !== void 0 && block?.type === "thinking") {
|
|
355
|
+
block.thinking += event.delta.thinking;
|
|
356
|
+
eventSink.push({
|
|
357
|
+
type: "thinking_delta",
|
|
358
|
+
contentIndex: index,
|
|
359
|
+
delta: event.delta.thinking,
|
|
360
|
+
partial: output
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
} else if (event.delta.type === "input_json_delta") {
|
|
364
|
+
const index = blockIndexes.get(event.index);
|
|
365
|
+
const block = index === void 0 ? void 0 : blocks[index];
|
|
366
|
+
if (index !== void 0 && block?.type === "toolCall") {
|
|
367
|
+
block.partialJson = (block.partialJson ?? "") + event.delta.partial_json;
|
|
368
|
+
if (toolArgumentPreviewSchedules.get(block)?.(block.partialJson.length)) block.arguments = parseStreamingJson(block.partialJson);
|
|
369
|
+
eventSink.push({
|
|
370
|
+
type: "toolcall_delta",
|
|
371
|
+
contentIndex: index,
|
|
372
|
+
delta: event.delta.partial_json,
|
|
373
|
+
partial: output
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
} else if (event.delta.type === "signature_delta") {
|
|
377
|
+
const index = blockIndexes.get(event.index);
|
|
378
|
+
const block = index === void 0 ? void 0 : blocks[index];
|
|
379
|
+
if (index !== void 0 && block?.type === "thinking") {
|
|
380
|
+
block.thinkingSignature = block.thinkingSignature || "";
|
|
381
|
+
block.thinkingSignature += event.delta.signature;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
} else if (event.type === "content_block_stop") {
|
|
385
|
+
if (compactionCapture.complete(event.index)) continue;
|
|
343
386
|
const index = blockIndexes.get(event.index);
|
|
344
387
|
const block = index === void 0 ? void 0 : blocks[index];
|
|
345
|
-
if (index !== void 0 && block
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
388
|
+
if (index !== void 0 && block) {
|
|
389
|
+
blockIndexes.delete(event.index);
|
|
390
|
+
delete block.index;
|
|
391
|
+
if (block.type === "text") eventSink.push({
|
|
392
|
+
type: "text_end",
|
|
349
393
|
contentIndex: index,
|
|
350
|
-
|
|
394
|
+
content: block.text,
|
|
351
395
|
partial: output
|
|
352
396
|
});
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
const index = blockIndexes.get(event.index);
|
|
356
|
-
const block = index === void 0 ? void 0 : blocks[index];
|
|
357
|
-
if (index !== void 0 && block?.type === "toolCall") {
|
|
358
|
-
block.partialJson = (block.partialJson ?? "") + event.delta.partial_json;
|
|
359
|
-
if (toolArgumentPreviewSchedules.get(block)?.(block.partialJson.length)) block.arguments = parseStreamingJson(block.partialJson);
|
|
360
|
-
eventSink.push({
|
|
361
|
-
type: "toolcall_delta",
|
|
397
|
+
else if (block.type === "thinking") eventSink.push({
|
|
398
|
+
type: "thinking_end",
|
|
362
399
|
contentIndex: index,
|
|
363
|
-
|
|
400
|
+
content: block.thinking,
|
|
364
401
|
partial: output
|
|
365
402
|
});
|
|
403
|
+
else if (block.type === "toolCall") sealedToolCalls.push({
|
|
404
|
+
block,
|
|
405
|
+
contentIndex: index
|
|
406
|
+
});
|
|
366
407
|
}
|
|
367
|
-
} else if (event.
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
block.thinkingSignature += event.delta.signature;
|
|
408
|
+
} else if (event.type === "message_delta") {
|
|
409
|
+
logAnthropicContextEdits(event);
|
|
410
|
+
if (event.delta.stop_reason) {
|
|
411
|
+
if (event.delta.stop_reason === "refusal") applyAnthropicRefusal(output, event.delta.stop_details, model.provider);
|
|
412
|
+
else output.stopReason = mapAnthropicStopReason(event.delta.stop_reason);
|
|
373
413
|
}
|
|
414
|
+
applyAnthropicMessageDeltaUsage(output.usage, event.usage, messageStartPromptUsage);
|
|
415
|
+
calculateCost(costModel, output.usage);
|
|
374
416
|
}
|
|
375
|
-
} else if (event.type === "content_block_stop") {
|
|
376
|
-
if (compactionCapture.complete(event.index)) continue;
|
|
377
|
-
const index = blockIndexes.get(event.index);
|
|
378
|
-
const block = index === void 0 ? void 0 : blocks[index];
|
|
379
|
-
if (index !== void 0 && block) {
|
|
380
|
-
blockIndexes.delete(event.index);
|
|
381
|
-
delete block.index;
|
|
382
|
-
if (block.type === "text") eventSink.push({
|
|
383
|
-
type: "text_end",
|
|
384
|
-
contentIndex: index,
|
|
385
|
-
content: block.text,
|
|
386
|
-
partial: output
|
|
387
|
-
});
|
|
388
|
-
else if (block.type === "thinking") eventSink.push({
|
|
389
|
-
type: "thinking_end",
|
|
390
|
-
contentIndex: index,
|
|
391
|
-
content: block.thinking,
|
|
392
|
-
partial: output
|
|
393
|
-
});
|
|
394
|
-
else if (block.type === "toolCall") sealedToolCalls.push({
|
|
395
|
-
block,
|
|
396
|
-
contentIndex: index
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
} else if (event.type === "message_delta") {
|
|
400
|
-
if (event.delta.stop_reason) {
|
|
401
|
-
if (event.delta.stop_reason === "refusal") applyAnthropicRefusal(output, event.delta.stop_details, model.provider);
|
|
402
|
-
else output.stopReason = mapAnthropicStopReason(event.delta.stop_reason);
|
|
403
|
-
}
|
|
404
|
-
applyAnthropicMessageDeltaUsage(output.usage, event.usage, messageStartPromptUsage);
|
|
405
|
-
calculateCost(costModel, output.usage);
|
|
406
417
|
}
|
|
407
418
|
if (requestOptions?.signal?.aborted) throw transportAbortError(requestOptions.signal);
|
|
408
419
|
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage ?? "An unknown error occurred");
|
|
@@ -442,6 +453,8 @@ const streamAnthropic = (model, context, options) => {
|
|
|
442
453
|
error: output
|
|
443
454
|
});
|
|
444
455
|
stream.end();
|
|
456
|
+
} finally {
|
|
457
|
+
logAnthropicThinkingDrops(inputTransformations);
|
|
445
458
|
}
|
|
446
459
|
})();
|
|
447
460
|
return stream;
|
|
@@ -463,6 +476,8 @@ const streamSimpleAnthropic = (model, context, options) => {
|
|
|
463
476
|
const base = {
|
|
464
477
|
...buildBaseOptions(model, options, apiKey),
|
|
465
478
|
anthropicServerCompaction: options?.anthropicServerCompaction,
|
|
479
|
+
anthropicCompactThreshold: options?.anthropicCompactThreshold,
|
|
480
|
+
cacheTtlPruning: options?.cacheTtlPruning,
|
|
466
481
|
authProfileId: options?.authProfileId,
|
|
467
482
|
maxTokens: clampMaxTokensToModel(model, options?.maxTokens ?? model.maxTokens),
|
|
468
483
|
toolChoice: options?.toolChoice
|
|
@@ -501,14 +516,6 @@ const streamSimpleAnthropic = (model, context, options) => {
|
|
|
501
516
|
thinkingBudgetTokens: thinkingEnabled ? adjusted.thinkingBudget : void 0
|
|
502
517
|
});
|
|
503
518
|
};
|
|
504
|
-
function isAnthropicPublicEndpoint(baseUrl) {
|
|
505
|
-
if (!baseUrl) return true;
|
|
506
|
-
try {
|
|
507
|
-
return new URL(baseUrl).hostname.toLowerCase() === "api.anthropic.com";
|
|
508
|
-
} catch {
|
|
509
|
-
return false;
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
519
|
/**
|
|
513
520
|
* Server-side refusal fallback is a first-party Claude API beta: proxies and
|
|
514
521
|
* Bedrock/Vertex/Foundry reject the `fallbacks` param, and OAuth (Claude Code
|
|
@@ -516,7 +523,7 @@ function isAnthropicPublicEndpoint(baseUrl) {
|
|
|
516
523
|
*/
|
|
517
524
|
function supportsAnthropicServerSideFallback(model) {
|
|
518
525
|
if (!usesClaudeFable5MessagesContract(model) && resolveClaudeOpus5ModelIdentity(model) === void 0 || model.provider !== "anthropic") return false;
|
|
519
|
-
return
|
|
526
|
+
return isDirectAnthropicModel(model);
|
|
520
527
|
}
|
|
521
528
|
function createClient(model, apiKey, thinkingEnabled, interleavedThinking, useFineGrainedToolStreamingBeta, optionsHeaders, dynamicHeaders, sessionId) {
|
|
522
529
|
const needsInterleavedBeta = interleavedThinking && !supportsClaudeAdaptiveThinking(model);
|
|
@@ -606,22 +613,24 @@ function createClient(model, apiKey, thinkingEnabled, interleavedThinking, useFi
|
|
|
606
613
|
const serverSideFallback = supportsAnthropicServerSideFallback(model);
|
|
607
614
|
if (serverSideFallback) betaFeatures.push(ANTHROPIC_SERVER_SIDE_FALLBACK_BETA);
|
|
608
615
|
const sessionAffinityHeaders = sessionId && getAnthropicCompat(model).sendSessionAffinityHeaders ? { "x-session-affinity": sessionId } : {};
|
|
616
|
+
const defaultHeaders = mergeHeaders({
|
|
617
|
+
accept: "application/json",
|
|
618
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
619
|
+
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
620
|
+
}, sessionAffinityHeaders, model.headers, optionsHeaders);
|
|
609
621
|
return {
|
|
610
622
|
client: new Anthropic({
|
|
611
623
|
apiKey,
|
|
612
624
|
authToken: null,
|
|
613
625
|
baseURL: model.baseUrl,
|
|
614
626
|
dangerouslyAllowBrowser: true,
|
|
615
|
-
defaultHeaders
|
|
616
|
-
accept: "application/json",
|
|
617
|
-
"anthropic-dangerous-direct-browser-access": "true",
|
|
618
|
-
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
619
|
-
}, sessionAffinityHeaders, model.headers, optionsHeaders),
|
|
627
|
+
defaultHeaders,
|
|
620
628
|
fetch,
|
|
621
629
|
maxRetries: 0
|
|
622
630
|
}),
|
|
623
631
|
isOAuthToken: false,
|
|
624
|
-
serverSideFallback
|
|
632
|
+
serverSideFallback,
|
|
633
|
+
directApiKeyBetaHeader: isDirectAnthropicModel(model) ? Object.entries(defaultHeaders).findLast(([name]) => name.toLowerCase() === "anthropic-beta")?.[1] ?? "" : void 0
|
|
625
634
|
};
|
|
626
635
|
}
|
|
627
636
|
async function buildParams(model, context, isOAuthTokenResult, options, serverSideFallback = false) {
|
|
@@ -687,22 +696,17 @@ async function buildParams(model, context, isOAuthTokenResult, options, serverSi
|
|
|
687
696
|
async function convertMessages(messages, model, isOAuthTokenValue, cacheControl, messageCacheControlLimit = 4, replayThinkingEnabled = true, allowEmptySignature = false, compaction) {
|
|
688
697
|
const params = [];
|
|
689
698
|
const imageBudget = createAnthropicInlineImageBudget();
|
|
690
|
-
const cacheBreakpointOptOutParamIndexes = /* @__PURE__ */ new Set();
|
|
691
699
|
const transformedMessages = transformProviderMessages(messages, model, normalizeAnthropicToolCallId);
|
|
692
700
|
const activeToolTurnAssistantIndex = replayThinkingEnabled ? -1 : findActiveAnthropicToolTurnAssistantIndex(transformedMessages);
|
|
693
701
|
for (let i = 0; i < transformedMessages.length; i++) {
|
|
694
702
|
const msg = transformedMessages[i];
|
|
695
703
|
if (!msg) continue;
|
|
696
704
|
if (msg.role === "user") {
|
|
697
|
-
const isRuntimeContextCarrier = msg.runtimeContextCarrier === true;
|
|
698
705
|
if (typeof msg.content === "string") {
|
|
699
|
-
if (msg.content.trim().length > 0) {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
content: sanitizeSurrogates(msg.content)
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
|
+
if (msg.content.trim().length > 0) params.push({
|
|
707
|
+
role: "user",
|
|
708
|
+
content: sanitizeSurrogates(msg.content)
|
|
709
|
+
});
|
|
706
710
|
} else {
|
|
707
711
|
const filteredBlocks = (await normalizeAnthropicInlineContent(msg.content, imageBudget)).map((item) => {
|
|
708
712
|
if (item.type === "text") return {
|
|
@@ -722,7 +726,6 @@ async function convertMessages(messages, model, isOAuthTokenValue, cacheControl,
|
|
|
722
726
|
return true;
|
|
723
727
|
});
|
|
724
728
|
if (filteredBlocks.length === 0) continue;
|
|
725
|
-
if (isRuntimeContextCarrier) cacheBreakpointOptOutParamIndexes.add(params.length);
|
|
726
729
|
params.push({
|
|
727
730
|
role: "user",
|
|
728
731
|
content: filteredBlocks
|
|
@@ -807,7 +810,7 @@ async function convertMessages(messages, model, isOAuthTokenValue, cacheControl,
|
|
|
807
810
|
});
|
|
808
811
|
}
|
|
809
812
|
}
|
|
810
|
-
if (cacheControl) applyAnthropicCacheControlToMessages(params, cacheControl, messageCacheControlLimit,
|
|
813
|
+
if (cacheControl) applyAnthropicCacheControlToMessages(params, cacheControl, messageCacheControlLimit, /* @__PURE__ */ new Set());
|
|
811
814
|
return params;
|
|
812
815
|
}
|
|
813
816
|
function buildAnthropicSystemBlocks(systemPrompt, isOAuthTokenResult, cacheControl) {
|