@openclaw/ai 2026.9.2 → 2026.9.3
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 +3 -2
- package/dist/anthropic-CZy5U0NY.mjs +376 -0
- package/dist/{anthropic-payload-policy-d46X2pR0.d.mts → anthropic-payload-policy-wuRCb6MH.d.mts} +16 -6
- package/dist/{anthropic-compaction-replay-DB2FGLQc.mjs → anthropic-stream-reducer-B_yo_7pf.mjs} +923 -181
- package/dist/{api-registry-DWtPjzyn.d.mts → api-registry-CB1-1oQ2.d.mts} +2 -2
- package/dist/assistant-output-iqnlJCV2.mjs +16 -0
- package/dist/assistant-text-phase-C20rxWwP.mjs +55 -0
- package/dist/{azure-openai-responses-C5bZRfAP.mjs → azure-openai-responses-Crpa40QR.mjs} +5 -5
- package/dist/{base64-CEFBpSkN.mjs → base64-D-su8YVo.mjs} +1 -27
- package/dist/{diagnostics-DfFyKeX_.mjs → diagnostics-dV98PqIy.mjs} +96 -21
- package/dist/diagnostics.d.mts +3 -1
- package/dist/diagnostics.mjs +3 -3
- package/dist/{event-stream-BhT5T1Ay.d.mts → event-stream-C1Z2piyk.d.mts} +11 -3
- package/dist/{event-stream-BgDvQeum.mjs → event-stream-D8PARQfL.mjs} +45 -1
- package/dist/event-stream-I_GlBsuZ.d.mts +1 -0
- package/dist/event-stream.d.mts +2 -2
- package/dist/event-stream.mjs +1 -1
- package/dist/{github-copilot-headers-NCJtz9i0.mjs → github-copilot-headers-B37TB3rB.mjs} +3 -10
- package/dist/github-copilot-request-facts-BTEBMeOv.mjs +16 -0
- package/dist/{google-4qeuE8iX.mjs → google-BDPriaVe.mjs} +10 -10
- package/dist/google-messages-CVn9eFpF.mjs +449 -0
- package/dist/google-shared-BedY23XS.mjs +185 -0
- package/dist/{google-vertex-O9BcnB6X.mjs → google-vertex-DMz8XQCn.mjs} +7 -6
- package/dist/{host-CEvLw30U.mjs → host-CWuF-sS3.mjs} +44 -39
- package/dist/{host-DjzGmdZ2.d.mts → host-DK3wmS3e.d.mts} +3 -3
- package/dist/host-policy-CAopLRKA.mjs +37 -0
- package/dist/{index-FnHM2FcI.d.mts → index-CQ6LTHw8.d.mts} +3 -3
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +4 -4
- package/dist/internal/anthropic.d.mts +7 -7
- package/dist/internal/anthropic.mjs +4 -4
- package/dist/internal/google-model-family.d.mts +5 -0
- package/dist/internal/google-model-family.mjs +15 -0
- 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 +8 -103
- package/dist/internal/openai.mjs +10 -10
- package/dist/internal/retry-after.d.mts +2 -4
- package/dist/internal/retry-after.mjs +57 -8
- package/dist/internal/runtime.d.mts +7 -6
- package/dist/internal/runtime.mjs +8 -7
- package/dist/internal/shared.d.mts +14 -3
- package/dist/internal/shared.mjs +6 -4
- package/dist/internal/tool-schema.d.mts +63 -0
- package/dist/internal/tool-schema.mjs +3 -0
- package/dist/{json-parse-BuAJEbdW.mjs → json-parse-Dw_hnxsA.mjs} +1 -1
- package/dist/{mistral-Tb6oalqH.mjs → mistral--m-Jm6VZ.mjs} +13 -37
- package/dist/model-transport-url-DKocrEsb.d.mts +28 -0
- package/dist/{openai-chatgpt-responses-yUXjPNfu.mjs → openai-chatgpt-responses-nDZccFW-.mjs} +48 -38
- package/dist/openai-completions-KuoZyx0d.mjs +187 -0
- package/dist/{openai-completions-compat-CpjYUArk.d.mts → openai-completions-compat-CXn3T1we.d.mts} +26 -4
- package/dist/{openai-completions-stream-BsrBe3Gg.mjs → openai-completions-stream-BQk3SkLD.mjs} +622 -452
- package/dist/openai-prompt-cache-BI0rkM-5.mjs +220 -0
- package/dist/openai-prompt-cache-rrutvqxq.d.mts +16 -0
- package/dist/openai-provider-client-ha_WxTyj.mjs +24 -0
- package/dist/openai-reasoning-effort-BK7FbcLT.mjs +167 -0
- package/dist/{openai-responses-CCL41ALM.mjs → openai-responses-D99dOzKI.mjs} +14 -29
- package/dist/{openai-responses-compaction-window-BMVHFOtq.mjs → openai-responses-compaction-window-D5jbzCi3.mjs} +7 -170
- package/dist/{openai-responses-contracts-rF5DDRNS.d.mts → openai-responses-contracts-B55afwRo.d.mts} +3 -3
- package/dist/{openai-responses-prompt-observer-internal-D0bhBfgL.mjs → openai-responses-prompt-observer-internal-tApyTLVu.mjs} +3 -3
- package/dist/{openai-responses-shared-Bmma_3Qc.mjs → openai-responses-shared-ZyQEzS5i.mjs} +44 -115
- package/dist/openai-tool-projection-793cE3QX.d.mts +37 -0
- package/dist/{openai-tool-schema-_pTAJqKF.mjs → openai-tool-schema-CzjyYXun.mjs} +38 -556
- package/dist/openai-tool-schema-ynZBgqW-.d.mts +38 -0
- package/dist/openai-transport-params-DNasp2fU.mjs +674 -0
- package/dist/{provider-error-9TraxGvt.mjs → provider-error-C6TbKiey.mjs} +23 -6
- package/dist/{provider-options-Bcc_p-TU.d.mts → provider-options-BXr9Ec83.d.mts} +4 -39
- package/dist/{provider-transcript-transform-BaMbI1hr.mjs → provider-transcript-transform-pPmUIwKt.mjs} +1 -1
- package/dist/provider-types-CAV0Og3m.d.mts +29 -0
- package/dist/provider-types.d.mts +6 -31
- package/dist/providers.d.mts +2 -2
- package/dist/providers.mjs +11 -11
- package/dist/{reasoning-tag-text-partitioner-DLCNXki6.mjs → reasoning-tag-text-partitioner-BQBi4B9d.mjs} +19 -12
- package/dist/record-coerce-DwRYMj3t.mjs +32 -0
- package/dist/retry-after-CdCURCVg.d.mts +15 -0
- package/dist/{sanitize-unicode-D6xUvZaS.mjs → sanitize-unicode-Bb-v9meu.mjs} +1 -1
- package/dist/session-affinity-CCH7eYdB.mjs +20 -0
- package/dist/{simple-options-BjHCCh4v.mjs → simple-options-tcKOqnpF.mjs} +3 -3
- package/dist/{src-2qBGKg8O.mjs → src-B2Q_6G8V.mjs} +1 -1
- package/dist/{stream-first-event-timeout-DcNjoFQE.mjs → stream-first-event-timeout-2hfrquuz.mjs} +1 -1
- package/dist/string-coerce-fsri9iCu.mjs +34 -0
- package/dist/{string-normalization--fwJ4S2q.mjs → string-normalization-CmLIasuf.mjs} +1 -1
- package/dist/{tool-schema-json-projection-mJhXDcyz.mjs → tool-schema-json-projection-ClptDdAO.mjs} +2 -38
- package/dist/{transport-stream-shared-CZqMhfIw.mjs → transport-stream-shared-Cu3ZPhNW.mjs} +5 -5
- package/dist/{transport-stream-shared-xnaqxmbP.d.mts → transport-stream-shared-DOxpGJ-H.d.mts} +4 -4
- package/dist/transport-utils-CCooe-cr.mjs +121 -0
- package/dist/transports.d.mts +113 -41
- package/dist/transports.mjs +162 -1276
- package/dist/types-BADKjDBI.d.mts +1 -0
- package/dist/{types-CJ1-Ht7A.d.mts → types-Dy1q0CSu.d.mts} +72 -57
- package/dist/types.d.mts +5 -5
- package/dist/types.mjs +3 -3
- package/dist/utf16-slice-CvGodqok.mjs +29 -0
- package/dist/{validation-CJZtym2g.mjs → validation-BDzVDnTs.mjs} +9 -1
- package/dist/{validation-AKZBDGQd.d.mts → validation-CaFUZN9B.d.mts} +1 -1
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/package.json +13 -3
- package/dist/anthropic-BDdqdVLK.mjs +0 -886
- package/dist/event-stream-zctLx0yr.d.mts +0 -1
- package/dist/google-shared-CWeG8RIl.mjs +0 -636
- package/dist/google-thinking-level-C-V3tecN.mjs +0 -9
- package/dist/openai-completions-BIUV3RDT.mjs +0 -403
- package/dist/openai-prompt-cache-CGnVB74a.mjs +0 -21
- package/dist/openai-prompt-cache-CNoIfHYC.d.mts +0 -16
- package/dist/transport-utils-7il795_9.mjs +0 -138
- package/dist/types-3Lnm-QSJ.d.mts +0 -1
- package/dist/utf16-slice-qz3nsy87.mjs +0 -84
|
@@ -1,886 +0,0 @@
|
|
|
1
|
-
import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
|
|
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
|
-
import { o as isRecord } from "./utf16-slice-qz3nsy87.mjs";
|
|
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
|
-
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-BjHCCh4v.mjs";
|
|
10
|
-
import { t as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "./transport-utils-7il795_9.mjs";
|
|
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-CZqMhfIw.mjs";
|
|
13
|
-
import { t as createDeferredEventBuffer } from "./deferred-event-buffer-DAvyP7qA.mjs";
|
|
14
|
-
import { n as parseJsonWithRepair, r as parseStreamingJson, t as createToolArgumentPreviewSchedule } from "./json-parse-BuAJEbdW.mjs";
|
|
15
|
-
import { t as notifyLlmRequestActivity } from "./llm-request-activity-BjtkplhG.mjs";
|
|
16
|
-
import { t as transformProviderMessages } from "./provider-transcript-transform-BaMbI1hr.mjs";
|
|
17
|
-
import { i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-NCJtz9i0.mjs";
|
|
18
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
19
|
-
import { Stream } from "@anthropic-ai/sdk/core/streaming.js";
|
|
20
|
-
//#region packages/ai/src/providers/anthropic.ts
|
|
21
|
-
var anthropic_exports = /* @__PURE__ */ __exportAll({
|
|
22
|
-
streamAnthropic: () => streamAnthropic,
|
|
23
|
-
streamSimpleAnthropic: () => streamSimpleAnthropic
|
|
24
|
-
});
|
|
25
|
-
const ANTHROPIC_CACHE_CONTROL_LIMIT = 4;
|
|
26
|
-
const EMPTY_ERROR_TOOL_RESULT_TEXT = "[tool error with no output]";
|
|
27
|
-
function getCacheControl(model, cacheRetention) {
|
|
28
|
-
const retention = resolveCacheRetention(cacheRetention);
|
|
29
|
-
if (retention === "none") return { retention };
|
|
30
|
-
const ttl = retention === "long" && getAnthropicCompat(model).supportsLongCacheRetention ? "1h" : void 0;
|
|
31
|
-
return {
|
|
32
|
-
retention,
|
|
33
|
-
cacheControl: {
|
|
34
|
-
type: "ephemeral",
|
|
35
|
-
...ttl && { ttl }
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Convert content blocks to Anthropic API format
|
|
41
|
-
*/
|
|
42
|
-
async function convertContentBlocks(content, isError, imageBudget) {
|
|
43
|
-
const text = extractToolResultText(content);
|
|
44
|
-
const mediaPlaceholder = describeToolResultMediaPlaceholder(content);
|
|
45
|
-
if (!content.some(isImageWithMediaPayload)) {
|
|
46
|
-
const sanitized = sanitizeSurrogates(text);
|
|
47
|
-
return sanitized.trim().length > 0 ? sanitized : mediaPlaceholder ?? (isError ? EMPTY_ERROR_TOOL_RESULT_TEXT : "");
|
|
48
|
-
}
|
|
49
|
-
const blocks = [];
|
|
50
|
-
let hasTextBlock = false;
|
|
51
|
-
for (const block of content) {
|
|
52
|
-
if (!block || typeof block !== "object") continue;
|
|
53
|
-
const record = block;
|
|
54
|
-
const blockText = extractToolResultBlockText(block);
|
|
55
|
-
if (blockText) {
|
|
56
|
-
blocks.push({
|
|
57
|
-
type: "text",
|
|
58
|
-
text: sanitizeSurrogates(blockText)
|
|
59
|
-
});
|
|
60
|
-
hasTextBlock = true;
|
|
61
|
-
}
|
|
62
|
-
if (!isImageWithMediaPayload(record)) continue;
|
|
63
|
-
const [normalizedImage] = await normalizeAnthropicInlineContent([{
|
|
64
|
-
type: "image",
|
|
65
|
-
data: typeof record.data === "string" ? record.data : "",
|
|
66
|
-
mimeType: typeof record.mimeType === "string" ? record.mimeType : "image/jpeg"
|
|
67
|
-
}], imageBudget);
|
|
68
|
-
if (normalizedImage?.type !== "image") continue;
|
|
69
|
-
blocks.push({
|
|
70
|
-
type: "image",
|
|
71
|
-
source: {
|
|
72
|
-
type: "base64",
|
|
73
|
-
media_type: resolveAnthropicImageMediaType(normalizedImage.mimeType),
|
|
74
|
-
data: normalizedImage.data
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
if (!hasTextBlock) blocks.unshift({
|
|
79
|
-
type: "text",
|
|
80
|
-
text: mediaPlaceholder ?? "(see attached image)"
|
|
81
|
-
});
|
|
82
|
-
return blocks;
|
|
83
|
-
}
|
|
84
|
-
const FINE_GRAINED_TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14";
|
|
85
|
-
const INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
|
|
86
|
-
const ANTHROPIC_MIN_THINKING_BUDGET_TOKENS = 1024;
|
|
87
|
-
function getAnthropicCompat(model) {
|
|
88
|
-
const isFireworks = model.provider === "fireworks";
|
|
89
|
-
const isCloudflareAiGatewayAnthropic = model.provider === "cloudflare-ai-gateway" && model.baseUrl.includes("anthropic");
|
|
90
|
-
return {
|
|
91
|
-
supportsEagerToolInputStreaming: model.compat?.supportsEagerToolInputStreaming ?? !isFireworks,
|
|
92
|
-
supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? !isFireworks,
|
|
93
|
-
sendSessionAffinityHeaders: model.compat?.sendSessionAffinityHeaders ?? (isFireworks || isCloudflareAiGatewayAnthropic),
|
|
94
|
-
supportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? !isFireworks,
|
|
95
|
-
allowEmptySignature: model.compat?.allowEmptySignature ?? false
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
function mergeHeaders(...headerSources) {
|
|
99
|
-
const merged = {};
|
|
100
|
-
for (const headers of headerSources) if (headers) Object.assign(merged, headers);
|
|
101
|
-
return merged;
|
|
102
|
-
}
|
|
103
|
-
const ANTHROPIC_MESSAGE_EVENTS = /* @__PURE__ */ new Set([
|
|
104
|
-
"message_start",
|
|
105
|
-
"message_delta",
|
|
106
|
-
"message_stop",
|
|
107
|
-
"content_block_start",
|
|
108
|
-
"content_block_delta",
|
|
109
|
-
"content_block_stop"
|
|
110
|
-
]);
|
|
111
|
-
async function* iterateAnthropicEvents(response, requireMessageStop = false, signal) {
|
|
112
|
-
if (!response.body) throw new Error("Attempted to iterate over an Anthropic response with no body");
|
|
113
|
-
let sawMessageEnd = false;
|
|
114
|
-
for await (const sse of Stream.rawEvents(response)) {
|
|
115
|
-
if (sse.event === "error") throw new Error(sse.data);
|
|
116
|
-
notifyLlmRequestActivity(signal);
|
|
117
|
-
if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) continue;
|
|
118
|
-
try {
|
|
119
|
-
const event = parseJsonWithRepair(sse.data);
|
|
120
|
-
if (event.type === "message_stop") sawMessageEnd = true;
|
|
121
|
-
yield event;
|
|
122
|
-
} catch (error) {
|
|
123
|
-
if (error instanceof SyntaxError) throw new Error(MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE, { cause: error });
|
|
124
|
-
throw error;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (requireMessageStop && !sawMessageEnd) throw new Error("Anthropic stream ended before message_stop");
|
|
128
|
-
}
|
|
129
|
-
const streamAnthropic = (model, context, options) => {
|
|
130
|
-
const stream = new AssistantMessageEventStream();
|
|
131
|
-
const requestContext = prepareClaudeNoPrefillRequestContext(model, context);
|
|
132
|
-
const requestOptions = normalizeAnthropicThinkingOptions(model, options);
|
|
133
|
-
(async () => {
|
|
134
|
-
const output = {
|
|
135
|
-
role: "assistant",
|
|
136
|
-
content: [],
|
|
137
|
-
api: model.api,
|
|
138
|
-
provider: model.provider,
|
|
139
|
-
model: model.id,
|
|
140
|
-
usage: {
|
|
141
|
-
input: 0,
|
|
142
|
-
output: 0,
|
|
143
|
-
cacheRead: 0,
|
|
144
|
-
cacheWrite: 0,
|
|
145
|
-
totalTokens: 0,
|
|
146
|
-
cost: {
|
|
147
|
-
input: 0,
|
|
148
|
-
output: 0,
|
|
149
|
-
cacheRead: 0,
|
|
150
|
-
cacheWrite: 0,
|
|
151
|
-
total: 0
|
|
152
|
-
}
|
|
153
|
-
},
|
|
154
|
-
stopReason: "stop",
|
|
155
|
-
timestamp: Date.now()
|
|
156
|
-
};
|
|
157
|
-
const refusalBuffer = usesClaudeStreamingRefusalContract(model) ? createDeferredEventBuffer(stream) : void 0;
|
|
158
|
-
const eventSink = refusalBuffer ?? stream;
|
|
159
|
-
let costModel = model;
|
|
160
|
-
let messageStartPromptUsage;
|
|
161
|
-
let usedCompactionReplay = false;
|
|
162
|
-
let inputTransformations;
|
|
163
|
-
try {
|
|
164
|
-
let client;
|
|
165
|
-
let isOAuth;
|
|
166
|
-
let serverSideFallback = false;
|
|
167
|
-
let directApiKeyBetaHeader;
|
|
168
|
-
if (requestOptions?.client) {
|
|
169
|
-
client = requestOptions.client;
|
|
170
|
-
isOAuth = false;
|
|
171
|
-
} else {
|
|
172
|
-
const apiKey = requestOptions?.apiKey ?? getEnvApiKey(model.provider) ?? "";
|
|
173
|
-
let copilotDynamicHeaders;
|
|
174
|
-
if (model.provider === "github-copilot") {
|
|
175
|
-
const hasImages = hasCopilotVisionInput(requestContext.messages);
|
|
176
|
-
copilotDynamicHeaders = buildCopilotDynamicHeaders({
|
|
177
|
-
messages: requestContext.messages,
|
|
178
|
-
hasImages
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
const cacheSessionId = (requestOptions?.cacheRetention ?? resolveCacheRetention()) === "none" ? void 0 : requestOptions?.sessionId;
|
|
182
|
-
const created = createClient(model, apiKey, requestOptions?.thinkingEnabled === true, requestOptions?.interleavedThinking ?? true, shouldUseFineGrainedToolStreamingBeta(model, requestContext), requestOptions?.headers, copilotDynamicHeaders, cacheSessionId);
|
|
183
|
-
client = created.client;
|
|
184
|
-
isOAuth = created.isOAuthToken;
|
|
185
|
-
serverSideFallback = created.serverSideFallback;
|
|
186
|
-
directApiKeyBetaHeader = created.directApiKeyBetaHeader;
|
|
187
|
-
}
|
|
188
|
-
const builtParams = await buildParams(model, requestContext, isOAuth, requestOptions, serverSideFallback);
|
|
189
|
-
usedCompactionReplay = builtParams.usedCompactionReplay;
|
|
190
|
-
let params = builtParams.params;
|
|
191
|
-
const toolProjection = builtParams.toolProjection;
|
|
192
|
-
applyAnthropicContextManagementToRequest(params, model, requestOptions, directApiKeyBetaHeader);
|
|
193
|
-
const nextParams = await requestOptions?.onPayload?.(params, model);
|
|
194
|
-
if (nextParams !== void 0) params = nextParams;
|
|
195
|
-
applyClaudeRequestContract(params, model);
|
|
196
|
-
const betaHeader = resolveAnthropicContextManagementBetaHeader(params, directApiKeyBetaHeader);
|
|
197
|
-
const sdkRequestOptions = {
|
|
198
|
-
...requestOptions?.signal ? { signal: requestOptions.signal } : {},
|
|
199
|
-
...requestOptions?.timeoutMs !== void 0 ? { timeout: requestOptions.timeoutMs } : {},
|
|
200
|
-
maxRetries: 0,
|
|
201
|
-
headers: applyAnthropicThinkingBindingControls(params, betaHeader) ?? (betaHeader !== void 0 ? { "anthropic-beta": betaHeader } : void 0)
|
|
202
|
-
};
|
|
203
|
-
const response = await client.messages.create({
|
|
204
|
-
...params,
|
|
205
|
-
stream: true
|
|
206
|
-
}, sdkRequestOptions).asResponse();
|
|
207
|
-
await notifyProviderHttpResponse({
|
|
208
|
-
options: requestOptions,
|
|
209
|
-
response,
|
|
210
|
-
model
|
|
211
|
-
});
|
|
212
|
-
const blocks = output.content;
|
|
213
|
-
const blockIndexes = /* @__PURE__ */ new Map();
|
|
214
|
-
const toolArgumentPreviewSchedules = /* @__PURE__ */ new WeakMap();
|
|
215
|
-
const sealedToolCalls = [];
|
|
216
|
-
const compactionCapture = createCompactionCapture(output, model, requestOptions);
|
|
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);
|
|
224
|
-
calculateCost(costModel, output.usage);
|
|
225
|
-
eventSink.push({
|
|
226
|
-
type: "start",
|
|
227
|
-
partial: output
|
|
228
|
-
});
|
|
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);
|
|
284
|
-
eventSink.push({
|
|
285
|
-
type: "text_start",
|
|
286
|
-
contentIndex: output.content.length - 1,
|
|
287
|
-
partial: output
|
|
288
|
-
});
|
|
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,
|
|
301
|
-
partial: output
|
|
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);
|
|
313
|
-
eventSink.push({
|
|
314
|
-
type: "thinking_start",
|
|
315
|
-
contentIndex: output.content.length - 1,
|
|
316
|
-
partial: output
|
|
317
|
-
});
|
|
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());
|
|
330
|
-
eventSink.push({
|
|
331
|
-
type: "toolcall_start",
|
|
332
|
-
contentIndex: output.content.length - 1,
|
|
333
|
-
partial: output
|
|
334
|
-
});
|
|
335
|
-
}
|
|
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;
|
|
386
|
-
const index = blockIndexes.get(event.index);
|
|
387
|
-
const block = index === void 0 ? void 0 : blocks[index];
|
|
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",
|
|
393
|
-
contentIndex: index,
|
|
394
|
-
content: block.text,
|
|
395
|
-
partial: output
|
|
396
|
-
});
|
|
397
|
-
else if (block.type === "thinking") eventSink.push({
|
|
398
|
-
type: "thinking_end",
|
|
399
|
-
contentIndex: index,
|
|
400
|
-
content: block.thinking,
|
|
401
|
-
partial: output
|
|
402
|
-
});
|
|
403
|
-
else if (block.type === "toolCall") sealedToolCalls.push({
|
|
404
|
-
block,
|
|
405
|
-
contentIndex: index
|
|
406
|
-
});
|
|
407
|
-
}
|
|
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);
|
|
413
|
-
}
|
|
414
|
-
applyAnthropicMessageDeltaUsage(output.usage, event.usage, messageStartPromptUsage);
|
|
415
|
-
calculateCost(costModel, output.usage);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
if (requestOptions?.signal?.aborted) throw transportAbortError(requestOptions.signal);
|
|
419
|
-
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage ?? "An unknown error occurred");
|
|
420
|
-
if ([...blockIndexes.values()].some((index) => blocks[index]?.type === "toolCall")) throw new Error("Provider completed stream with an incomplete tool call");
|
|
421
|
-
finalizeTerminalToolCallArguments(sealedToolCalls.map(({ block }) => block), (block) => block.partialJson && block.partialJson.length > 0 ? block.partialJson : block.arguments);
|
|
422
|
-
for (const sealed of sealedToolCalls) {
|
|
423
|
-
delete sealed.block.partialJson;
|
|
424
|
-
eventSink.push({
|
|
425
|
-
type: "toolcall_end",
|
|
426
|
-
contentIndex: sealed.contentIndex,
|
|
427
|
-
toolCall: sealed.block,
|
|
428
|
-
partial: output
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
refusalBuffer?.flush();
|
|
432
|
-
stream.push({
|
|
433
|
-
type: "done",
|
|
434
|
-
reason: output.stopReason,
|
|
435
|
-
message: output
|
|
436
|
-
});
|
|
437
|
-
stream.end();
|
|
438
|
-
} catch (error) {
|
|
439
|
-
const terminal = assignTransportErrorDetails(output, error, requestOptions?.signal);
|
|
440
|
-
output.content = output.content.filter((block) => block.type !== "toolCall");
|
|
441
|
-
for (const block of output.content) {
|
|
442
|
-
delete block.index;
|
|
443
|
-
delete block.partialJson;
|
|
444
|
-
}
|
|
445
|
-
if (refusalBuffer) {
|
|
446
|
-
refusalBuffer.discard();
|
|
447
|
-
output.content = [];
|
|
448
|
-
}
|
|
449
|
-
if (usedCompactionReplay && isAnthropicReplayRejection(error)) suppressAnthropicCompaction(output, model, requestOptions);
|
|
450
|
-
stream.push({
|
|
451
|
-
type: "error",
|
|
452
|
-
reason: terminal.stopReason,
|
|
453
|
-
error: output
|
|
454
|
-
});
|
|
455
|
-
stream.end();
|
|
456
|
-
} finally {
|
|
457
|
-
logAnthropicThinkingDrops(inputTransformations);
|
|
458
|
-
}
|
|
459
|
-
})();
|
|
460
|
-
return stream;
|
|
461
|
-
};
|
|
462
|
-
function normalizeAnthropicThinkingOptions(model, options) {
|
|
463
|
-
if (options?.thinkingEnabled !== true || supportsClaudeAdaptiveThinking(model)) return options;
|
|
464
|
-
const budgetTokens = options.thinkingBudgetTokens ?? ANTHROPIC_MIN_THINKING_BUDGET_TOKENS;
|
|
465
|
-
const maxTokens = options.maxTokens ?? model.maxTokens;
|
|
466
|
-
if (budgetTokens >= ANTHROPIC_MIN_THINKING_BUDGET_TOKENS && budgetTokens < maxTokens) return options;
|
|
467
|
-
return {
|
|
468
|
-
...options,
|
|
469
|
-
thinkingEnabled: false,
|
|
470
|
-
thinkingBudgetTokens: void 0
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
const streamSimpleAnthropic = (model, context, options) => {
|
|
474
|
-
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
475
|
-
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
476
|
-
const base = {
|
|
477
|
-
...buildBaseOptions(model, options, apiKey),
|
|
478
|
-
anthropicServerCompaction: options?.anthropicServerCompaction,
|
|
479
|
-
anthropicCompactThreshold: options?.anthropicCompactThreshold,
|
|
480
|
-
cacheTtlPruning: options?.cacheTtlPruning,
|
|
481
|
-
authProfileId: options?.authProfileId,
|
|
482
|
-
maxTokens: clampMaxTokensToModel(model, options?.maxTokens ?? model.maxTokens),
|
|
483
|
-
toolChoice: options?.toolChoice
|
|
484
|
-
};
|
|
485
|
-
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
486
|
-
if (options?.reasoning === "off" && !mandatoryAdaptiveThinking) return streamAnthropic(model, context, {
|
|
487
|
-
...base,
|
|
488
|
-
thinkingEnabled: false
|
|
489
|
-
});
|
|
490
|
-
const reasoning = options?.reasoning === "off" ? mandatoryAdaptiveThinking ? "low" : "high" : options?.reasoning;
|
|
491
|
-
if (resolveClaudeOpus5ModelIdentity(model) || resolveClaudeSonnet5ModelIdentity(model)) return streamAnthropic(model, context, {
|
|
492
|
-
...base,
|
|
493
|
-
thinkingEnabled: true,
|
|
494
|
-
effort: resolveAnthropicThinkingEffort(model, reasoning ?? "high")
|
|
495
|
-
});
|
|
496
|
-
if (!reasoning) return streamAnthropic(model, context, {
|
|
497
|
-
...base,
|
|
498
|
-
thinkingEnabled: mandatoryAdaptiveThinking,
|
|
499
|
-
...mandatoryAdaptiveThinking ? { effort: "high" } : {}
|
|
500
|
-
});
|
|
501
|
-
if (supportsClaudeAdaptiveThinking(model)) {
|
|
502
|
-
const effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
503
|
-
return streamAnthropic(model, context, {
|
|
504
|
-
...base,
|
|
505
|
-
thinkingEnabled: true,
|
|
506
|
-
effort
|
|
507
|
-
});
|
|
508
|
-
}
|
|
509
|
-
const adjusted = adjustMaxTokensForThinking(base.maxTokens, model.maxTokens, reasoning, options?.thinkingBudgets);
|
|
510
|
-
const thinkingEnabled = adjusted.thinkingBudget >= ANTHROPIC_MIN_THINKING_BUDGET_TOKENS;
|
|
511
|
-
const maxTokens = thinkingEnabled ? adjusted.maxTokens : clampMaxTokensToModel(model, options?.maxTokens ?? model.maxTokens);
|
|
512
|
-
return streamAnthropic(model, context, {
|
|
513
|
-
...base,
|
|
514
|
-
maxTokens,
|
|
515
|
-
thinkingEnabled,
|
|
516
|
-
thinkingBudgetTokens: thinkingEnabled ? adjusted.thinkingBudget : void 0
|
|
517
|
-
});
|
|
518
|
-
};
|
|
519
|
-
/**
|
|
520
|
-
* Server-side refusal fallback is a first-party Claude API beta: proxies and
|
|
521
|
-
* Bedrock/Vertex/Foundry reject the `fallbacks` param, and OAuth (Claude Code
|
|
522
|
-
* identity) requests are excluded until the beta is verified there.
|
|
523
|
-
*/
|
|
524
|
-
function supportsAnthropicServerSideFallback(model) {
|
|
525
|
-
if (!usesClaudeFable5MessagesContract(model) && resolveClaudeOpus5ModelIdentity(model) === void 0 || model.provider !== "anthropic") return false;
|
|
526
|
-
return isDirectAnthropicModel(model);
|
|
527
|
-
}
|
|
528
|
-
function createClient(model, apiKey, thinkingEnabled, interleavedThinking, useFineGrainedToolStreamingBeta, optionsHeaders, dynamicHeaders, sessionId) {
|
|
529
|
-
const needsInterleavedBeta = interleavedThinking && !supportsClaudeAdaptiveThinking(model);
|
|
530
|
-
const betaFeatures = [];
|
|
531
|
-
if (useFineGrainedToolStreamingBeta) betaFeatures.push(FINE_GRAINED_TOOL_STREAMING_BETA);
|
|
532
|
-
if (needsInterleavedBeta) betaFeatures.push(INTERLEAVED_THINKING_BETA);
|
|
533
|
-
const fetchOptions = /^kimi(?:-|$)/.test(model.provider) && thinkingEnabled ? { sanitizeSse: false } : void 0;
|
|
534
|
-
const fetch = getAiTransportHost().buildModelFetch(model, void 0, fetchOptions);
|
|
535
|
-
if (model.provider === "cloudflare-ai-gateway") return {
|
|
536
|
-
client: new Anthropic({
|
|
537
|
-
apiKey,
|
|
538
|
-
authToken: null,
|
|
539
|
-
baseURL: resolveCloudflareBaseUrl(model),
|
|
540
|
-
dangerouslyAllowBrowser: true,
|
|
541
|
-
defaultHeaders: mergeHeaders({
|
|
542
|
-
accept: "application/json",
|
|
543
|
-
"anthropic-dangerous-direct-browser-access": "true",
|
|
544
|
-
Authorization: null,
|
|
545
|
-
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
546
|
-
}, model.headers, optionsHeaders),
|
|
547
|
-
fetch,
|
|
548
|
-
maxRetries: 0
|
|
549
|
-
}),
|
|
550
|
-
isOAuthToken: false,
|
|
551
|
-
serverSideFallback: false
|
|
552
|
-
};
|
|
553
|
-
if (model.provider === "github-copilot") return {
|
|
554
|
-
client: new Anthropic({
|
|
555
|
-
apiKey: null,
|
|
556
|
-
authToken: apiKey,
|
|
557
|
-
baseURL: model.baseUrl,
|
|
558
|
-
dangerouslyAllowBrowser: true,
|
|
559
|
-
defaultHeaders: mergeHeaders({
|
|
560
|
-
accept: "application/json",
|
|
561
|
-
"anthropic-dangerous-direct-browser-access": "true",
|
|
562
|
-
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
563
|
-
}, model.headers, dynamicHeaders, optionsHeaders),
|
|
564
|
-
fetch,
|
|
565
|
-
maxRetries: 0
|
|
566
|
-
}),
|
|
567
|
-
isOAuthToken: false,
|
|
568
|
-
serverSideFallback: false
|
|
569
|
-
};
|
|
570
|
-
if (usesFoundryBearerAuth({
|
|
571
|
-
...model,
|
|
572
|
-
headers: resolveAiTransportHeaderSentinels(model.headers)
|
|
573
|
-
})) return {
|
|
574
|
-
client: new Anthropic({
|
|
575
|
-
apiKey: null,
|
|
576
|
-
authToken: apiKey,
|
|
577
|
-
baseURL: model.baseUrl,
|
|
578
|
-
dangerouslyAllowBrowser: true,
|
|
579
|
-
defaultHeaders: mergeHeaders({
|
|
580
|
-
accept: "application/json",
|
|
581
|
-
"anthropic-dangerous-direct-browser-access": "true",
|
|
582
|
-
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
583
|
-
}, omitFoundryBearerCredentialHeaders(model.headers), dynamicHeaders, optionsHeaders),
|
|
584
|
-
fetch,
|
|
585
|
-
maxRetries: 0
|
|
586
|
-
}),
|
|
587
|
-
isOAuthToken: false,
|
|
588
|
-
serverSideFallback: false
|
|
589
|
-
};
|
|
590
|
-
if (isAnthropicOAuthApiKey(apiKey)) return {
|
|
591
|
-
client: new Anthropic({
|
|
592
|
-
apiKey: null,
|
|
593
|
-
authToken: apiKey,
|
|
594
|
-
baseURL: model.baseUrl,
|
|
595
|
-
dangerouslyAllowBrowser: true,
|
|
596
|
-
defaultHeaders: mergeHeaders({
|
|
597
|
-
accept: "application/json",
|
|
598
|
-
"anthropic-dangerous-direct-browser-access": "true",
|
|
599
|
-
"anthropic-beta": [
|
|
600
|
-
"claude-code-20250219",
|
|
601
|
-
"oauth-2025-04-20",
|
|
602
|
-
...betaFeatures
|
|
603
|
-
].join(","),
|
|
604
|
-
"user-agent": `claude-cli/${ANTHROPIC_CLAUDE_CODE_VERSION}`,
|
|
605
|
-
"x-app": "cli"
|
|
606
|
-
}, model.headers, optionsHeaders),
|
|
607
|
-
fetch,
|
|
608
|
-
maxRetries: 0
|
|
609
|
-
}),
|
|
610
|
-
isOAuthToken: true,
|
|
611
|
-
serverSideFallback: false
|
|
612
|
-
};
|
|
613
|
-
const serverSideFallback = supportsAnthropicServerSideFallback(model);
|
|
614
|
-
if (serverSideFallback) betaFeatures.push(ANTHROPIC_SERVER_SIDE_FALLBACK_BETA);
|
|
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);
|
|
621
|
-
return {
|
|
622
|
-
client: new Anthropic({
|
|
623
|
-
apiKey,
|
|
624
|
-
authToken: null,
|
|
625
|
-
baseURL: model.baseUrl,
|
|
626
|
-
dangerouslyAllowBrowser: true,
|
|
627
|
-
defaultHeaders,
|
|
628
|
-
fetch,
|
|
629
|
-
maxRetries: 0
|
|
630
|
-
}),
|
|
631
|
-
isOAuthToken: false,
|
|
632
|
-
serverSideFallback,
|
|
633
|
-
directApiKeyBetaHeader: isDirectAnthropicModel(model) ? Object.entries(defaultHeaders).findLast(([name]) => name.toLowerCase() === "anthropic-beta")?.[1] ?? "" : void 0
|
|
634
|
-
};
|
|
635
|
-
}
|
|
636
|
-
async function buildParams(model, context, isOAuthTokenResult, options, serverSideFallback = false) {
|
|
637
|
-
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
638
|
-
const replayThinkingEnabled = mandatoryAdaptiveThinking || options?.thinkingEnabled === true;
|
|
639
|
-
const { cacheControl } = getCacheControl(model, options?.cacheRetention);
|
|
640
|
-
const system = buildAnthropicSystemBlocks(context.systemPrompt, isOAuthTokenResult, cacheControl);
|
|
641
|
-
const compat = getAnthropicCompat(model);
|
|
642
|
-
const convertedTools = context.tools ? convertTools(context.tools, isOAuthTokenResult, compat.supportsEagerToolInputStreaming, compat.supportsCacheControlOnTools ? cacheControl : void 0) : void 0;
|
|
643
|
-
const tools = convertedTools?.tools;
|
|
644
|
-
const toolProjection = convertedTools?.projection;
|
|
645
|
-
const systemCacheControlCount = countNativeCacheControlMarkers(system);
|
|
646
|
-
const toolCacheControlCount = countNativeCacheControlMarkers(tools);
|
|
647
|
-
const messageCacheControlLimit = Math.max(0, ANTHROPIC_CACHE_CONTROL_LIMIT - systemCacheControlCount - toolCacheControlCount);
|
|
648
|
-
const replayPlan = buildAnthropicReplayPlan(context.messages, model, {
|
|
649
|
-
enabled: !isOAuthTokenResult && options?.anthropicServerCompaction === true,
|
|
650
|
-
authProfileId: options?.authProfileId,
|
|
651
|
-
sessionId: options?.sessionId
|
|
652
|
-
});
|
|
653
|
-
const params = {
|
|
654
|
-
model: model.id,
|
|
655
|
-
messages: await convertMessages(replayPlan.messages, model, isOAuthTokenResult, cacheControl, messageCacheControlLimit, replayThinkingEnabled, compat.allowEmptySignature, replayPlan.compaction),
|
|
656
|
-
max_tokens: options?.maxTokens ?? model.maxTokens,
|
|
657
|
-
stream: true
|
|
658
|
-
};
|
|
659
|
-
if (system) params.system = system;
|
|
660
|
-
if (serverSideFallback) params.fallbacks = ANTHROPIC_SERVER_SIDE_FALLBACKS;
|
|
661
|
-
if (options?.temperature !== void 0 && !options?.thinkingEnabled && !supportsClaudeNativeXhighEffort(model)) params.temperature = options.temperature;
|
|
662
|
-
if (options?.stop !== void 0 && options.stop.length > 0) params.stop_sequences = options.stop;
|
|
663
|
-
if (tools && tools.length > 0) params.tools = tools;
|
|
664
|
-
if (mandatoryAdaptiveThinking || model.reasoning || supportsClaudeAdaptiveThinking(model)) {
|
|
665
|
-
if (mandatoryAdaptiveThinking || options?.thinkingEnabled) {
|
|
666
|
-
const display = options?.thinkingDisplay ?? "summarized";
|
|
667
|
-
if (supportsClaudeAdaptiveThinking(model)) {
|
|
668
|
-
params.thinking = {
|
|
669
|
-
type: "adaptive",
|
|
670
|
-
display
|
|
671
|
-
};
|
|
672
|
-
const effort = options?.effort ?? (mandatoryAdaptiveThinking ? "high" : void 0);
|
|
673
|
-
if (effort) params.output_config = { effort };
|
|
674
|
-
} else params.thinking = {
|
|
675
|
-
type: "enabled",
|
|
676
|
-
budget_tokens: options?.thinkingBudgetTokens ?? ANTHROPIC_MIN_THINKING_BUDGET_TOKENS,
|
|
677
|
-
display
|
|
678
|
-
};
|
|
679
|
-
} else if (options?.thinkingEnabled === false) params.thinking = { type: "disabled" };
|
|
680
|
-
}
|
|
681
|
-
if (options?.metadata) {
|
|
682
|
-
const userId = options.metadata.user_id;
|
|
683
|
-
if (typeof userId === "string") params.metadata = { user_id: userId };
|
|
684
|
-
}
|
|
685
|
-
if (options?.toolChoice) {
|
|
686
|
-
const normalizedToolChoice = normalizeAnthropicToolChoice(replayThinkingEnabled, options.toolChoice);
|
|
687
|
-
const projectedToolChoice = toolProjection ? reconcileAnthropicToolChoice(normalizedToolChoice, toolProjection) : normalizedToolChoice;
|
|
688
|
-
if (projectedToolChoice) params.tool_choice = projectedToolChoice;
|
|
689
|
-
}
|
|
690
|
-
return {
|
|
691
|
-
params,
|
|
692
|
-
toolProjection,
|
|
693
|
-
usedCompactionReplay: replayPlan.compaction !== void 0
|
|
694
|
-
};
|
|
695
|
-
}
|
|
696
|
-
async function convertMessages(messages, model, isOAuthTokenValue, cacheControl, messageCacheControlLimit = 4, replayThinkingEnabled = true, allowEmptySignature = false, compaction) {
|
|
697
|
-
const params = [];
|
|
698
|
-
const imageBudget = createAnthropicInlineImageBudget();
|
|
699
|
-
const transformedMessages = transformProviderMessages(messages, model, normalizeAnthropicToolCallId);
|
|
700
|
-
const activeToolTurnAssistantIndex = replayThinkingEnabled ? -1 : findActiveAnthropicToolTurnAssistantIndex(transformedMessages);
|
|
701
|
-
for (let i = 0; i < transformedMessages.length; i++) {
|
|
702
|
-
const msg = transformedMessages[i];
|
|
703
|
-
if (!msg) continue;
|
|
704
|
-
if (msg.role === "user") {
|
|
705
|
-
if (typeof msg.content === "string") {
|
|
706
|
-
if (msg.content.trim().length > 0) params.push({
|
|
707
|
-
role: "user",
|
|
708
|
-
content: sanitizeSurrogates(msg.content)
|
|
709
|
-
});
|
|
710
|
-
} else {
|
|
711
|
-
const filteredBlocks = (await normalizeAnthropicInlineContent(msg.content, imageBudget)).map((item) => {
|
|
712
|
-
if (item.type === "text") return {
|
|
713
|
-
type: "text",
|
|
714
|
-
text: sanitizeSurrogates(item.text)
|
|
715
|
-
};
|
|
716
|
-
return {
|
|
717
|
-
type: "image",
|
|
718
|
-
source: {
|
|
719
|
-
type: "base64",
|
|
720
|
-
media_type: resolveAnthropicImageMediaType(item.mimeType),
|
|
721
|
-
data: item.data
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
}).filter((b) => {
|
|
725
|
-
if (b.type === "text") return b.text.trim().length > 0;
|
|
726
|
-
return true;
|
|
727
|
-
});
|
|
728
|
-
if (filteredBlocks.length === 0) continue;
|
|
729
|
-
params.push({
|
|
730
|
-
role: "user",
|
|
731
|
-
content: filteredBlocks
|
|
732
|
-
});
|
|
733
|
-
}
|
|
734
|
-
} else if (msg.role === "assistant") {
|
|
735
|
-
const blocks = i === 0 && compaction ? [compaction] : [];
|
|
736
|
-
let omittedThinking = false;
|
|
737
|
-
for (const block of msg.content) if (block.type === "text") {
|
|
738
|
-
if (block.text.trim().length === 0) continue;
|
|
739
|
-
blocks.push({
|
|
740
|
-
type: "text",
|
|
741
|
-
text: sanitizeSurrogates(block.text)
|
|
742
|
-
});
|
|
743
|
-
} else if (block.type === "thinking") {
|
|
744
|
-
if (!replayThinkingEnabled && i !== activeToolTurnAssistantIndex) {
|
|
745
|
-
omittedThinking = true;
|
|
746
|
-
continue;
|
|
747
|
-
}
|
|
748
|
-
if (block.redacted) {
|
|
749
|
-
if (!block.thinkingSignature) throw new Error("redacted thinking block is missing its opaque signature");
|
|
750
|
-
blocks.push({
|
|
751
|
-
type: "redacted_thinking",
|
|
752
|
-
data: block.thinkingSignature
|
|
753
|
-
});
|
|
754
|
-
continue;
|
|
755
|
-
}
|
|
756
|
-
const thinkingSignature = block.thinkingSignature?.trim();
|
|
757
|
-
const hasNativeThinkingSignature = Boolean(thinkingSignature) && thinkingSignature !== "reasoning_content";
|
|
758
|
-
if (block.thinking.trim().length === 0 && !hasNativeThinkingSignature) continue;
|
|
759
|
-
if (!thinkingSignature && !allowEmptySignature) blocks.push({
|
|
760
|
-
type: "text",
|
|
761
|
-
text: sanitizeSurrogates(block.thinking)
|
|
762
|
-
});
|
|
763
|
-
else {
|
|
764
|
-
if (thinkingSignature === "reasoning_content") continue;
|
|
765
|
-
blocks.push({
|
|
766
|
-
type: "thinking",
|
|
767
|
-
thinking: block.thinking,
|
|
768
|
-
signature: thinkingSignature ?? ""
|
|
769
|
-
});
|
|
770
|
-
}
|
|
771
|
-
} else if (block.type === "toolCall") blocks.push({
|
|
772
|
-
type: "tool_use",
|
|
773
|
-
id: block.id,
|
|
774
|
-
name: isOAuthTokenValue ? toClaudeCodeToolName(block.name) : block.name,
|
|
775
|
-
input: block.arguments ?? {}
|
|
776
|
-
});
|
|
777
|
-
if (blocks.length === 0 && omittedThinking) blocks.push({
|
|
778
|
-
type: "text",
|
|
779
|
-
text: ANTHROPIC_OMITTED_REASONING_TEXT
|
|
780
|
-
});
|
|
781
|
-
if (blocks.length === 0) continue;
|
|
782
|
-
params.push({
|
|
783
|
-
role: "assistant",
|
|
784
|
-
content: blocks
|
|
785
|
-
});
|
|
786
|
-
} else if (msg.role === "toolResult") {
|
|
787
|
-
const toolResults = [];
|
|
788
|
-
toolResults.push({
|
|
789
|
-
type: "tool_result",
|
|
790
|
-
tool_use_id: msg.toolCallId,
|
|
791
|
-
content: await convertContentBlocks(msg.content, msg.isError, imageBudget),
|
|
792
|
-
is_error: msg.isError
|
|
793
|
-
});
|
|
794
|
-
let j = i + 1;
|
|
795
|
-
while (j < transformedMessages.length) {
|
|
796
|
-
const nextMsg = transformedMessages.at(j);
|
|
797
|
-
if (nextMsg?.role !== "toolResult") break;
|
|
798
|
-
toolResults.push({
|
|
799
|
-
type: "tool_result",
|
|
800
|
-
tool_use_id: nextMsg.toolCallId,
|
|
801
|
-
content: await convertContentBlocks(nextMsg.content, nextMsg.isError, imageBudget),
|
|
802
|
-
is_error: nextMsg.isError
|
|
803
|
-
});
|
|
804
|
-
j++;
|
|
805
|
-
}
|
|
806
|
-
i = j - 1;
|
|
807
|
-
params.push({
|
|
808
|
-
role: "user",
|
|
809
|
-
content: toolResults
|
|
810
|
-
});
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
if (cacheControl) applyAnthropicCacheControlToMessages(params, cacheControl, messageCacheControlLimit, /* @__PURE__ */ new Set());
|
|
814
|
-
return params;
|
|
815
|
-
}
|
|
816
|
-
function buildAnthropicSystemBlocks(systemPrompt, isOAuthTokenResult, cacheControl) {
|
|
817
|
-
const blocks = [];
|
|
818
|
-
if (isOAuthTokenResult) {
|
|
819
|
-
blocks.push({
|
|
820
|
-
type: "text",
|
|
821
|
-
text: ANTHROPIC_CLAUDE_CODE_BILLING_SYSTEM_BLOCK
|
|
822
|
-
});
|
|
823
|
-
blocks.push({
|
|
824
|
-
type: "text",
|
|
825
|
-
text: "You are Claude Code, Anthropic's official CLI for Claude.",
|
|
826
|
-
...cacheControl ? { cache_control: cacheControl } : {}
|
|
827
|
-
});
|
|
828
|
-
}
|
|
829
|
-
if (systemPrompt) blocks.push(...buildSystemPromptBlocks(systemPrompt, cacheControl));
|
|
830
|
-
return blocks.length > 0 ? blocks : void 0;
|
|
831
|
-
}
|
|
832
|
-
function buildSystemPromptBlocks(systemPrompt, cacheControl) {
|
|
833
|
-
if (!cacheControl) return [{
|
|
834
|
-
type: "text",
|
|
835
|
-
text: sanitizeSurrogates(stripSystemPromptCacheBoundary(systemPrompt))
|
|
836
|
-
}];
|
|
837
|
-
const split = splitSystemPromptCacheBoundary(systemPrompt);
|
|
838
|
-
if (!split) return [{
|
|
839
|
-
type: "text",
|
|
840
|
-
text: sanitizeSurrogates(systemPrompt),
|
|
841
|
-
cache_control: cacheControl
|
|
842
|
-
}];
|
|
843
|
-
const blocks = [];
|
|
844
|
-
if (split.stablePrefix) blocks.push({
|
|
845
|
-
type: "text",
|
|
846
|
-
text: sanitizeSurrogates(split.stablePrefix),
|
|
847
|
-
cache_control: cacheControl
|
|
848
|
-
});
|
|
849
|
-
if (split.dynamicSuffix) blocks.push({
|
|
850
|
-
type: "text",
|
|
851
|
-
text: sanitizeSurrogates(split.dynamicSuffix)
|
|
852
|
-
});
|
|
853
|
-
return blocks.length > 0 ? blocks : [{
|
|
854
|
-
type: "text",
|
|
855
|
-
text: ""
|
|
856
|
-
}];
|
|
857
|
-
}
|
|
858
|
-
function countNativeCacheControlMarkers(blocks) {
|
|
859
|
-
if (!Array.isArray(blocks)) return 0;
|
|
860
|
-
let count = 0;
|
|
861
|
-
for (const block of blocks) if (block && typeof block === "object" && "cache_control" in block) count += 1;
|
|
862
|
-
return count;
|
|
863
|
-
}
|
|
864
|
-
function shouldUseFineGrainedToolStreamingBeta(model, context) {
|
|
865
|
-
return Boolean(context.tools?.length) && !getAnthropicCompat(model).supportsEagerToolInputStreaming;
|
|
866
|
-
}
|
|
867
|
-
function convertTools(tools, isOAuthTokenLocal, supportsEagerToolInputStreaming, cacheControl) {
|
|
868
|
-
const projection = projectAnthropicTools(tools, (name) => isOAuthTokenLocal ? toClaudeCodeToolName(name) : name);
|
|
869
|
-
const convertedTools = [];
|
|
870
|
-
for (const [index, tool] of projection.tools.entries()) {
|
|
871
|
-
const convertedTool = {
|
|
872
|
-
name: tool.wireName,
|
|
873
|
-
description: tool.description,
|
|
874
|
-
input_schema: tool.inputSchema
|
|
875
|
-
};
|
|
876
|
-
if (supportsEagerToolInputStreaming) convertedTool.eager_input_streaming = true;
|
|
877
|
-
if (cacheControl && index === projection.tools.length - 1) convertedTool.cache_control = cacheControl;
|
|
878
|
-
convertedTools.push(convertedTool);
|
|
879
|
-
}
|
|
880
|
-
return {
|
|
881
|
-
projection,
|
|
882
|
-
tools: convertedTools
|
|
883
|
-
};
|
|
884
|
-
}
|
|
885
|
-
//#endregion
|
|
886
|
-
export { streamAnthropic as n, streamSimpleAnthropic as r, anthropic_exports as t };
|