@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
package/README.md
CHANGED
|
@@ -23,7 +23,8 @@ Host policy (request fetch guarding, secret redaction, strict-tool defaults,
|
|
|
23
23
|
provider plugin hooks, and diagnostics logging) can be injected with
|
|
24
24
|
`configureAiTransportHost`; the defaults are inert.
|
|
25
25
|
|
|
26
|
-
The explicit `@openclaw/ai/internal/anthropic`, `
|
|
27
|
-
`
|
|
26
|
+
The explicit `@openclaw/ai/internal/anthropic`, `google-model-family`, `openai`,
|
|
27
|
+
`openai-responses-payload-policy`, `retry-after`, `runtime`, `shared`, and
|
|
28
|
+
`tool-schema` subpaths exist for the OpenClaw application itself.
|
|
28
29
|
They carry no semver guarantee and can change or disappear in any release; do
|
|
29
30
|
not depend on them outside OpenClaw.
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-BhDjJH2R.mjs";
|
|
2
|
+
import { _ as supportsClaudeAdaptiveThinking, h as resolveClaudeSonnet5ModelIdentity, m as resolveClaudeOpus5ModelIdentity } from "./src-B2Q_6G8V.mjs";
|
|
3
|
+
import { t as AssistantMessageEventStream } from "./event-stream-D8PARQfL.mjs";
|
|
4
|
+
import { S as usesClaudeStreamingRefusalContract, _ as prepareClaudeNoPrefillRequestContext, m as applyClaudeRequestContract, n as getAiTransportHost, p as ANTHROPIC_CLAUDE_CODE_VERSION, r as resolveAiTransportHeaderSentinels, v as requiresClaudeAdaptiveThinking, x as usesClaudeFable5MessagesContract, y as resolveAnthropicThinkingEffort } from "./host-CWuF-sS3.mjs";
|
|
5
|
+
import { t as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "./transport-utils-CCooe-cr.mjs";
|
|
6
|
+
import { C as applyAnthropicThinkingBindingControls, D as ANTHROPIC_SERVER_SIDE_FALLBACKS, H as resolveAnthropicContextManagementBetaHeader, I as applyAnthropicRequestCacheControl, J as usesFoundryBearerAuth, K as isAnthropicOAuthApiKey, L as buildAnthropicSystemBlocks, N as applyAnthropicContextManagementToRequest, O as ANTHROPIC_SERVER_SIDE_FALLBACK_BETA, V as resolveAnthropicCacheOptions, d as convertAnthropicTools, f as buildAnthropicReplayPlan, g as normalizeAnthropicToolCallId, h as suppressAnthropicCompaction, l as buildAnthropicGenerationParams, p as isAnthropicReplayRejection, q as omitFoundryBearerCredentialHeaders, t as consumeAnthropicStream, u as convertAnthropicMessages, z as isDirectAnthropicModel } from "./anthropic-stream-reducer-B_yo_7pf.mjs";
|
|
7
|
+
import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
|
|
8
|
+
import { n as buildBaseOptions, r as clampMaxTokensToModel, t as adjustMaxTokensForThinking } from "./simple-options-tcKOqnpF.mjs";
|
|
9
|
+
import { n as getEnvApiKey } from "./env-api-keys-bktO00EJ.mjs";
|
|
10
|
+
import { c as finalizeTransportStream, f as notifyProviderHttpResponse, t as assignTransportErrorDetails } from "./transport-stream-shared-Cu3ZPhNW.mjs";
|
|
11
|
+
import { t as createDeferredEventBuffer } from "./deferred-event-buffer-DAvyP7qA.mjs";
|
|
12
|
+
import { n as parseJsonWithRepair } from "./json-parse-Dw_hnxsA.mjs";
|
|
13
|
+
import { t as notifyLlmRequestActivity } from "./llm-request-activity-BjtkplhG.mjs";
|
|
14
|
+
import { t as createAssistantOutput } from "./assistant-output-iqnlJCV2.mjs";
|
|
15
|
+
import { t as resolveOpencodeSessionHeaders } from "./session-affinity-CCH7eYdB.mjs";
|
|
16
|
+
import { t as transformProviderMessages } from "./provider-transcript-transform-pPmUIwKt.mjs";
|
|
17
|
+
import { i as resolveCloudflareBaseUrl, n as hasCopilotVisionInput, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-B37TB3rB.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 FINE_GRAINED_TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14";
|
|
26
|
+
const INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
|
|
27
|
+
const ANTHROPIC_MIN_THINKING_BUDGET_TOKENS = 1024;
|
|
28
|
+
function getAnthropicCompat(model) {
|
|
29
|
+
const isFireworks = model.provider === "fireworks";
|
|
30
|
+
const isCloudflareAiGatewayAnthropic = model.provider === "cloudflare-ai-gateway" && model.baseUrl.includes("anthropic");
|
|
31
|
+
return {
|
|
32
|
+
supportsEagerToolInputStreaming: model.compat?.supportsEagerToolInputStreaming ?? !isFireworks,
|
|
33
|
+
sendSessionAffinityHeaders: model.compat?.sendSessionAffinityHeaders ?? (isFireworks || isCloudflareAiGatewayAnthropic),
|
|
34
|
+
allowEmptySignature: model.compat?.allowEmptySignature ?? false
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function mergeHeaders(...headerSources) {
|
|
38
|
+
const merged = {};
|
|
39
|
+
for (const headers of headerSources) if (headers) Object.assign(merged, headers);
|
|
40
|
+
return merged;
|
|
41
|
+
}
|
|
42
|
+
const ANTHROPIC_MESSAGE_EVENTS = /* @__PURE__ */ new Set([
|
|
43
|
+
"message_start",
|
|
44
|
+
"message_delta",
|
|
45
|
+
"message_stop",
|
|
46
|
+
"content_block_start",
|
|
47
|
+
"content_block_delta",
|
|
48
|
+
"content_block_stop"
|
|
49
|
+
]);
|
|
50
|
+
async function* iterateAnthropicEvents(response, signal) {
|
|
51
|
+
if (!response.body) throw new Error("Attempted to iterate over an Anthropic response with no body");
|
|
52
|
+
for await (const sse of Stream.rawEvents(response)) {
|
|
53
|
+
if (sse.event === "error") throw new Error(sse.data);
|
|
54
|
+
notifyLlmRequestActivity(signal);
|
|
55
|
+
if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) continue;
|
|
56
|
+
try {
|
|
57
|
+
yield parseJsonWithRepair(sse.data);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (error instanceof SyntaxError) throw new Error(MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE, { cause: error });
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const streamAnthropic = (model, context, options) => {
|
|
65
|
+
const stream = new AssistantMessageEventStream();
|
|
66
|
+
const requestContext = prepareClaudeNoPrefillRequestContext(model, context);
|
|
67
|
+
const requestOptions = normalizeAnthropicThinkingOptions(model, options);
|
|
68
|
+
(async () => {
|
|
69
|
+
const output = createAssistantOutput(model);
|
|
70
|
+
const refusalBuffer = usesClaudeStreamingRefusalContract(model) ? createDeferredEventBuffer(stream) : void 0;
|
|
71
|
+
let usedCompactionReplay = false;
|
|
72
|
+
try {
|
|
73
|
+
let client;
|
|
74
|
+
let isOAuth;
|
|
75
|
+
let serverSideFallback = false;
|
|
76
|
+
let directApiKeyBetaHeader;
|
|
77
|
+
if (requestOptions?.client) {
|
|
78
|
+
client = requestOptions.client;
|
|
79
|
+
isOAuth = false;
|
|
80
|
+
} else {
|
|
81
|
+
const apiKey = requestOptions?.apiKey ?? getEnvApiKey(model.provider) ?? "";
|
|
82
|
+
let copilotDynamicHeaders;
|
|
83
|
+
if (model.provider === "github-copilot") {
|
|
84
|
+
const hasImages = hasCopilotVisionInput(requestContext.messages);
|
|
85
|
+
copilotDynamicHeaders = buildCopilotDynamicHeaders({
|
|
86
|
+
messages: requestContext.messages,
|
|
87
|
+
hasImages
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const cacheSessionId = (requestOptions?.cacheRetention ?? resolveCacheRetention()) === "none" ? void 0 : requestOptions?.sessionId;
|
|
91
|
+
const created = createClient(model, apiKey, requestOptions?.thinkingEnabled === true, requestOptions?.interleavedThinking ?? true, shouldUseFineGrainedToolStreamingBeta(model, requestContext), resolveOpencodeSessionHeaders(model, requestOptions), copilotDynamicHeaders, cacheSessionId);
|
|
92
|
+
client = created.client;
|
|
93
|
+
isOAuth = created.isOAuthToken;
|
|
94
|
+
serverSideFallback = created.serverSideFallback;
|
|
95
|
+
directApiKeyBetaHeader = created.directApiKeyBetaHeader;
|
|
96
|
+
}
|
|
97
|
+
const builtParams = await buildParams(model, requestContext, isOAuth, requestOptions, serverSideFallback);
|
|
98
|
+
usedCompactionReplay = builtParams.usedCompactionReplay;
|
|
99
|
+
let params = builtParams.params;
|
|
100
|
+
const toolProjection = builtParams.toolProjection;
|
|
101
|
+
applyAnthropicContextManagementToRequest(params, model, requestOptions, directApiKeyBetaHeader);
|
|
102
|
+
const nextParams = await requestOptions?.onPayload?.(params, model);
|
|
103
|
+
if (nextParams !== void 0) params = nextParams;
|
|
104
|
+
applyClaudeRequestContract(params, model);
|
|
105
|
+
const betaHeader = resolveAnthropicContextManagementBetaHeader(params, directApiKeyBetaHeader);
|
|
106
|
+
const sdkRequestOptions = {
|
|
107
|
+
...requestOptions?.signal ? { signal: requestOptions.signal } : {},
|
|
108
|
+
...requestOptions?.timeoutMs !== void 0 ? { timeout: requestOptions.timeoutMs } : {},
|
|
109
|
+
maxRetries: 0,
|
|
110
|
+
headers: applyAnthropicThinkingBindingControls(params, betaHeader) ?? (betaHeader !== void 0 ? { "anthropic-beta": betaHeader } : void 0)
|
|
111
|
+
};
|
|
112
|
+
const response = await client.messages.create({
|
|
113
|
+
...params,
|
|
114
|
+
stream: true
|
|
115
|
+
}, sdkRequestOptions).asResponse();
|
|
116
|
+
await notifyProviderHttpResponse({
|
|
117
|
+
options: requestOptions,
|
|
118
|
+
response,
|
|
119
|
+
model
|
|
120
|
+
});
|
|
121
|
+
await consumeAnthropicStream({
|
|
122
|
+
events: iterateAnthropicEvents(response, requestOptions?.signal),
|
|
123
|
+
model,
|
|
124
|
+
options: requestOptions ?? {},
|
|
125
|
+
output,
|
|
126
|
+
stream,
|
|
127
|
+
refusalBuffer,
|
|
128
|
+
isOAuthToken: isOAuth,
|
|
129
|
+
toolProjection,
|
|
130
|
+
profile: "provider"
|
|
131
|
+
});
|
|
132
|
+
finalizeTransportStream({
|
|
133
|
+
stream,
|
|
134
|
+
output
|
|
135
|
+
});
|
|
136
|
+
} catch (error) {
|
|
137
|
+
const terminal = assignTransportErrorDetails(output, error, requestOptions?.signal);
|
|
138
|
+
output.content = output.content.filter((block) => block.type !== "toolCall");
|
|
139
|
+
for (const block of output.content) {
|
|
140
|
+
delete block.index;
|
|
141
|
+
delete block.partialJson;
|
|
142
|
+
}
|
|
143
|
+
if (refusalBuffer) {
|
|
144
|
+
refusalBuffer.discard();
|
|
145
|
+
output.content = [];
|
|
146
|
+
}
|
|
147
|
+
if (usedCompactionReplay && isAnthropicReplayRejection(error)) suppressAnthropicCompaction(output, model, requestOptions);
|
|
148
|
+
stream.push({
|
|
149
|
+
type: "error",
|
|
150
|
+
reason: terminal.stopReason,
|
|
151
|
+
error: output
|
|
152
|
+
});
|
|
153
|
+
stream.end();
|
|
154
|
+
}
|
|
155
|
+
})();
|
|
156
|
+
return stream;
|
|
157
|
+
};
|
|
158
|
+
function normalizeAnthropicThinkingOptions(model, options) {
|
|
159
|
+
if (options?.thinkingEnabled !== true || supportsClaudeAdaptiveThinking(model)) return options;
|
|
160
|
+
const budgetTokens = options.thinkingBudgetTokens ?? ANTHROPIC_MIN_THINKING_BUDGET_TOKENS;
|
|
161
|
+
const maxTokens = options.maxTokens ?? model.maxTokens;
|
|
162
|
+
if (budgetTokens >= ANTHROPIC_MIN_THINKING_BUDGET_TOKENS && budgetTokens < maxTokens) return options;
|
|
163
|
+
return {
|
|
164
|
+
...options,
|
|
165
|
+
thinkingEnabled: false,
|
|
166
|
+
thinkingBudgetTokens: void 0
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const streamSimpleAnthropic = (model, context, options) => {
|
|
170
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
171
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
172
|
+
const base = {
|
|
173
|
+
...buildBaseOptions(model, options, apiKey),
|
|
174
|
+
anthropicServerCompaction: options?.anthropicServerCompaction,
|
|
175
|
+
anthropicCompactThreshold: options?.anthropicCompactThreshold,
|
|
176
|
+
cacheTtlPruning: options?.cacheTtlPruning,
|
|
177
|
+
authProfileId: options?.authProfileId,
|
|
178
|
+
maxTokens: clampMaxTokensToModel(model, options?.maxTokens ?? model.maxTokens),
|
|
179
|
+
toolChoice: options?.toolChoice
|
|
180
|
+
};
|
|
181
|
+
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
182
|
+
if (options?.reasoning === "off" && !mandatoryAdaptiveThinking) return streamAnthropic(model, context, {
|
|
183
|
+
...base,
|
|
184
|
+
thinkingEnabled: false
|
|
185
|
+
});
|
|
186
|
+
const reasoning = options?.reasoning === "off" ? mandatoryAdaptiveThinking ? "low" : "high" : options?.reasoning;
|
|
187
|
+
if (resolveClaudeOpus5ModelIdentity(model) || resolveClaudeSonnet5ModelIdentity(model)) return streamAnthropic(model, context, {
|
|
188
|
+
...base,
|
|
189
|
+
thinkingEnabled: true,
|
|
190
|
+
effort: resolveAnthropicThinkingEffort(model, reasoning ?? "high")
|
|
191
|
+
});
|
|
192
|
+
if (!reasoning) return streamAnthropic(model, context, {
|
|
193
|
+
...base,
|
|
194
|
+
thinkingEnabled: mandatoryAdaptiveThinking,
|
|
195
|
+
...mandatoryAdaptiveThinking ? { effort: "high" } : {}
|
|
196
|
+
});
|
|
197
|
+
if (supportsClaudeAdaptiveThinking(model)) {
|
|
198
|
+
const effort = resolveAnthropicThinkingEffort(model, reasoning);
|
|
199
|
+
return streamAnthropic(model, context, {
|
|
200
|
+
...base,
|
|
201
|
+
thinkingEnabled: true,
|
|
202
|
+
effort
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
const adjusted = adjustMaxTokensForThinking(base.maxTokens, model.maxTokens, reasoning, options?.thinkingBudgets);
|
|
206
|
+
const thinkingEnabled = adjusted.thinkingBudget >= ANTHROPIC_MIN_THINKING_BUDGET_TOKENS;
|
|
207
|
+
const maxTokens = thinkingEnabled ? adjusted.maxTokens : clampMaxTokensToModel(model, options?.maxTokens ?? model.maxTokens);
|
|
208
|
+
return streamAnthropic(model, context, {
|
|
209
|
+
...base,
|
|
210
|
+
maxTokens,
|
|
211
|
+
thinkingEnabled,
|
|
212
|
+
thinkingBudgetTokens: thinkingEnabled ? adjusted.thinkingBudget : void 0
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Server-side refusal fallback is a first-party Claude API beta: proxies and
|
|
217
|
+
* Bedrock/Vertex/Foundry reject the `fallbacks` param, and OAuth (Claude Code
|
|
218
|
+
* identity) requests are excluded until the beta is verified there.
|
|
219
|
+
*/
|
|
220
|
+
function supportsAnthropicServerSideFallback(model) {
|
|
221
|
+
if (!usesClaudeFable5MessagesContract(model) && resolveClaudeOpus5ModelIdentity(model) === void 0 || model.provider !== "anthropic") return false;
|
|
222
|
+
return isDirectAnthropicModel(model);
|
|
223
|
+
}
|
|
224
|
+
function createClient(model, apiKey, thinkingEnabled, interleavedThinking, useFineGrainedToolStreamingBeta, optionsHeaders, dynamicHeaders, sessionId) {
|
|
225
|
+
const needsInterleavedBeta = interleavedThinking && !supportsClaudeAdaptiveThinking(model);
|
|
226
|
+
const betaFeatures = [];
|
|
227
|
+
if (useFineGrainedToolStreamingBeta) betaFeatures.push(FINE_GRAINED_TOOL_STREAMING_BETA);
|
|
228
|
+
if (needsInterleavedBeta) betaFeatures.push(INTERLEAVED_THINKING_BETA);
|
|
229
|
+
const fetchOptions = /^kimi(?:-|$)/.test(model.provider) && thinkingEnabled ? { sanitizeSse: false } : void 0;
|
|
230
|
+
const fetch = getAiTransportHost().buildModelFetch(model, void 0, fetchOptions);
|
|
231
|
+
if (model.provider === "cloudflare-ai-gateway") return {
|
|
232
|
+
client: new Anthropic({
|
|
233
|
+
apiKey,
|
|
234
|
+
authToken: null,
|
|
235
|
+
baseURL: resolveCloudflareBaseUrl(model),
|
|
236
|
+
dangerouslyAllowBrowser: true,
|
|
237
|
+
defaultHeaders: mergeHeaders({
|
|
238
|
+
accept: "application/json",
|
|
239
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
240
|
+
Authorization: null,
|
|
241
|
+
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
242
|
+
}, model.headers, optionsHeaders),
|
|
243
|
+
fetch,
|
|
244
|
+
maxRetries: 0
|
|
245
|
+
}),
|
|
246
|
+
isOAuthToken: false,
|
|
247
|
+
serverSideFallback: false
|
|
248
|
+
};
|
|
249
|
+
if (model.provider === "github-copilot") return {
|
|
250
|
+
client: new Anthropic({
|
|
251
|
+
apiKey: null,
|
|
252
|
+
authToken: apiKey,
|
|
253
|
+
baseURL: model.baseUrl,
|
|
254
|
+
dangerouslyAllowBrowser: true,
|
|
255
|
+
defaultHeaders: mergeHeaders({
|
|
256
|
+
accept: "application/json",
|
|
257
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
258
|
+
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
259
|
+
}, model.headers, dynamicHeaders, optionsHeaders),
|
|
260
|
+
fetch,
|
|
261
|
+
maxRetries: 0
|
|
262
|
+
}),
|
|
263
|
+
isOAuthToken: false,
|
|
264
|
+
serverSideFallback: false
|
|
265
|
+
};
|
|
266
|
+
if (usesFoundryBearerAuth({
|
|
267
|
+
...model,
|
|
268
|
+
headers: resolveAiTransportHeaderSentinels(model.headers)
|
|
269
|
+
})) return {
|
|
270
|
+
client: new Anthropic({
|
|
271
|
+
apiKey: null,
|
|
272
|
+
authToken: apiKey,
|
|
273
|
+
baseURL: model.baseUrl,
|
|
274
|
+
dangerouslyAllowBrowser: true,
|
|
275
|
+
defaultHeaders: mergeHeaders({
|
|
276
|
+
accept: "application/json",
|
|
277
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
278
|
+
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
279
|
+
}, omitFoundryBearerCredentialHeaders(model.headers), dynamicHeaders, optionsHeaders),
|
|
280
|
+
fetch,
|
|
281
|
+
maxRetries: 0
|
|
282
|
+
}),
|
|
283
|
+
isOAuthToken: false,
|
|
284
|
+
serverSideFallback: false
|
|
285
|
+
};
|
|
286
|
+
if (isAnthropicOAuthApiKey(apiKey)) return {
|
|
287
|
+
client: new Anthropic({
|
|
288
|
+
apiKey: null,
|
|
289
|
+
authToken: apiKey,
|
|
290
|
+
baseURL: model.baseUrl,
|
|
291
|
+
dangerouslyAllowBrowser: true,
|
|
292
|
+
defaultHeaders: mergeHeaders({
|
|
293
|
+
accept: "application/json",
|
|
294
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
295
|
+
"anthropic-beta": [
|
|
296
|
+
"claude-code-20250219",
|
|
297
|
+
"oauth-2025-04-20",
|
|
298
|
+
...betaFeatures
|
|
299
|
+
].join(","),
|
|
300
|
+
"user-agent": `claude-cli/${ANTHROPIC_CLAUDE_CODE_VERSION}`,
|
|
301
|
+
"x-app": "cli"
|
|
302
|
+
}, model.headers, optionsHeaders),
|
|
303
|
+
fetch,
|
|
304
|
+
maxRetries: 0
|
|
305
|
+
}),
|
|
306
|
+
isOAuthToken: true,
|
|
307
|
+
serverSideFallback: false
|
|
308
|
+
};
|
|
309
|
+
const serverSideFallback = supportsAnthropicServerSideFallback(model);
|
|
310
|
+
if (serverSideFallback) betaFeatures.push(ANTHROPIC_SERVER_SIDE_FALLBACK_BETA);
|
|
311
|
+
const sessionAffinityHeaders = sessionId && getAnthropicCompat(model).sendSessionAffinityHeaders ? { "x-session-affinity": sessionId } : {};
|
|
312
|
+
const defaultHeaders = mergeHeaders({
|
|
313
|
+
accept: "application/json",
|
|
314
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
315
|
+
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
316
|
+
}, sessionAffinityHeaders, model.headers, optionsHeaders);
|
|
317
|
+
return {
|
|
318
|
+
client: new Anthropic({
|
|
319
|
+
apiKey,
|
|
320
|
+
authToken: null,
|
|
321
|
+
baseURL: model.baseUrl,
|
|
322
|
+
dangerouslyAllowBrowser: true,
|
|
323
|
+
defaultHeaders,
|
|
324
|
+
fetch,
|
|
325
|
+
maxRetries: 0
|
|
326
|
+
}),
|
|
327
|
+
isOAuthToken: false,
|
|
328
|
+
serverSideFallback,
|
|
329
|
+
directApiKeyBetaHeader: isDirectAnthropicModel(model) ? Object.entries(defaultHeaders).findLast(([name]) => name.toLowerCase() === "anthropic-beta")?.[1] ?? "" : void 0
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
async function buildParams(model, context, isOAuthTokenResult, options, serverSideFallback = false) {
|
|
333
|
+
const replayThinkingEnabled = requiresClaudeAdaptiveThinking(model) || options?.thinkingEnabled === true;
|
|
334
|
+
const { cacheControl, supportsCacheControlOnTools } = resolveAnthropicCacheOptions(model, options?.cacheRetention);
|
|
335
|
+
const system = buildAnthropicSystemBlocks(context.systemPrompt, isOAuthTokenResult, cacheControl);
|
|
336
|
+
const compat = getAnthropicCompat(model);
|
|
337
|
+
const convertedTools = context.tools ? convertAnthropicTools(context.tools, isOAuthTokenResult, compat.supportsEagerToolInputStreaming) : void 0;
|
|
338
|
+
const tools = convertedTools?.tools;
|
|
339
|
+
const toolProjection = convertedTools?.projection;
|
|
340
|
+
const replayPlan = buildAnthropicReplayPlan(context.messages, model, {
|
|
341
|
+
enabled: !isOAuthTokenResult && options?.anthropicServerCompaction === true,
|
|
342
|
+
authProfileId: options?.authProfileId,
|
|
343
|
+
sessionId: options?.sessionId
|
|
344
|
+
});
|
|
345
|
+
const params = {
|
|
346
|
+
model: model.id,
|
|
347
|
+
messages: await convertAnthropicMessages(transformProviderMessages(replayPlan.messages, model, normalizeAnthropicToolCallId), model, isOAuthTokenResult, {
|
|
348
|
+
profile: "provider",
|
|
349
|
+
allowEmptySignature: compat.allowEmptySignature,
|
|
350
|
+
compaction: replayPlan.compaction,
|
|
351
|
+
replayThinkingEnabled
|
|
352
|
+
}),
|
|
353
|
+
max_tokens: options?.maxTokens ?? model.maxTokens,
|
|
354
|
+
stream: true
|
|
355
|
+
};
|
|
356
|
+
if (system) params.system = system;
|
|
357
|
+
if (serverSideFallback) params.fallbacks = ANTHROPIC_SERVER_SIDE_FALLBACKS;
|
|
358
|
+
Object.assign(params, buildAnthropicGenerationParams({
|
|
359
|
+
model,
|
|
360
|
+
options,
|
|
361
|
+
tools,
|
|
362
|
+
toolProjection,
|
|
363
|
+
profile: "provider"
|
|
364
|
+
}));
|
|
365
|
+
applyAnthropicRequestCacheControl(params, cacheControl, supportsCacheControlOnTools);
|
|
366
|
+
return {
|
|
367
|
+
params,
|
|
368
|
+
toolProjection,
|
|
369
|
+
usedCompactionReplay: replayPlan.compaction !== void 0
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function shouldUseFineGrainedToolStreamingBeta(model, context) {
|
|
373
|
+
return Boolean(context.tools?.length) && !getAnthropicCompat(model).supportsEagerToolInputStreaming;
|
|
374
|
+
}
|
|
375
|
+
//#endregion
|
|
376
|
+
export { streamAnthropic as n, streamSimpleAnthropic as r, anthropic_exports as t };
|
package/dist/{anthropic-payload-policy-d46X2pR0.d.mts → anthropic-payload-policy-wuRCb6MH.d.mts}
RENAMED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import "./index-
|
|
2
|
-
import { D as Model } from "./types-
|
|
3
|
-
import { t as AnthropicContextManagementOptions } from "./provider-options-
|
|
1
|
+
import "./index-CQ6LTHw8.mjs";
|
|
2
|
+
import { D as Model } from "./types-Dy1q0CSu.mjs";
|
|
3
|
+
import { t as AnthropicContextManagementOptions } from "./provider-options-BXr9Ec83.mjs";
|
|
4
|
+
import { TextBlockParam } from "@anthropic-ai/sdk/resources/messages.js";
|
|
4
5
|
//#region packages/ai/src/transports/anthropic-payload-policy.d.ts
|
|
5
6
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
6
7
|
type AnthropicServiceTier = "auto" | "standard_only";
|
|
@@ -55,8 +56,17 @@ declare function isAnthropicServerToolClearingEnabled(model: {
|
|
|
55
56
|
}, apiKey?: string): boolean;
|
|
56
57
|
/** Resolve Anthropic cache-control marker retention for a request endpoint. */
|
|
57
58
|
declare function resolveAnthropicEphemeralCacheControl(baseUrl: string | undefined, cacheRetention: AnthropicPayloadPolicyInput["cacheRetention"]): AnthropicEphemeralCacheControl | undefined;
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
declare function resolveAnthropicCacheOptions(model: Model<"anthropic-messages">, cacheRetention?: "short" | "long" | "none"): {
|
|
60
|
+
cacheControl: AnthropicEphemeralCacheControl | undefined;
|
|
61
|
+
supportsCacheControlOnTools: boolean;
|
|
62
|
+
};
|
|
63
|
+
declare function buildAnthropicSystemBlocks(systemPrompt: string | undefined, isOAuthToken: boolean, cacheControl: AnthropicEphemeralCacheControl | undefined): TextBlockParam[] | undefined;
|
|
64
|
+
/** Allocate tool and history checkpoints around the prepared stable system blocks. */
|
|
65
|
+
declare function applyAnthropicRequestCacheControl(payload: {
|
|
66
|
+
system?: unknown;
|
|
67
|
+
tools?: unknown;
|
|
68
|
+
messages?: unknown;
|
|
69
|
+
}, cacheControl: AnthropicEphemeralCacheControl | undefined, supportsCacheControlOnTools: boolean, cacheBreakpointOptOutMessageIndexes?: ReadonlySet<number>): void;
|
|
60
70
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
61
71
|
declare function resolveAnthropicPayloadPolicy(input: AnthropicPayloadPolicyInput, model?: Model): AnthropicPayloadPolicy;
|
|
62
72
|
type AnthropicContextManagementPayload = {
|
|
@@ -72,4 +82,4 @@ declare function applyAnthropicPayloadPolicyToParams(payloadObj: Record<string,
|
|
|
72
82
|
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
73
83
|
declare function applyAnthropicEphemeralCacheControlMarkers(payloadObj: Record<string, unknown>, cacheControl?: AnthropicEphemeralCacheControl | null): void;
|
|
74
84
|
//#endregion
|
|
75
|
-
export {
|
|
85
|
+
export { buildAnthropicSystemBlocks as a, logAnthropicContextEdits as c, resolveAnthropicEphemeralCacheControl as d, resolveAnthropicPayloadPolicy as f, applyAnthropicRequestCacheControl as i, resolveAnthropicCacheOptions as l, applyAnthropicEphemeralCacheControlMarkers as n, isAnthropicServerToolClearingEnabled as o, resolveAnthropicServerCompactionPlan as p, applyAnthropicPayloadPolicyToParams as r, isDirectAnthropicModel as s, applyAnthropicContextManagementToRequest as t, resolveAnthropicContextManagementBetaHeader as u };
|