@openclaw/ai 2026.7.2-beta.3 → 2026.7.2-beta.5
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 +6 -5
- package/dist/{anthropic-D7SyZQ5v.mjs → anthropic-CQVj3le6.mjs} +174 -33
- package/dist/{index-BoTnz8cv.d.mts → anthropic-SrGtwsJu.d.mts} +27 -1
- package/dist/{api-registry-DXBIXISf.d.mts → api-registry-DlMgPR39.d.mts} +1 -2
- package/dist/{azure-openai-responses-BkZgyZ3I.mjs → azure-openai-responses-DIYgsqFM.mjs} +7 -11
- package/dist/{env-api-keys-DnhYpc27.mjs → env-api-keys-DrgeBuva.mjs} +1 -1
- package/dist/error-coercion-DgxlWC0n.mjs +15 -0
- package/dist/{event-stream-BPemFrFW.d.mts → event-stream-YjaPW20U.d.mts} +1 -1
- package/dist/event-stream.d.mts +1 -1
- package/dist/{tool-schema-json-projection-BwNu3nDi.mjs → github-copilot-headers-BCoBNmL7.mjs} +48 -1
- package/dist/{google-C_biGbV7.mjs → google-f-A8xrae.mjs} +4 -4
- package/dist/{google-shared-BK-NoHl8.mjs → google-shared-J6qvYINH.mjs} +5 -4
- package/dist/{google-vertex-CvdzF1U7.mjs → google-vertex-D3yMVXIY.mjs} +3 -3
- package/dist/host-B9GUmcra.d.mts +173 -0
- package/dist/host-XYGZcgO8.mjs +98 -0
- package/dist/index-BVVgDSdq.d.mts +1 -0
- package/dist/index.d.mts +7 -50
- package/dist/index.mjs +3 -3
- package/dist/internal/anthropic.d.mts +15 -11
- package/dist/internal/anthropic.mjs +4 -4
- package/dist/internal/openai.d.mts +3 -280
- package/dist/internal/openai.mjs +5 -7
- package/dist/internal/runtime.d.mts +13 -34
- package/dist/internal/runtime.mjs +19 -11
- package/dist/internal/shared.d.mts +13 -8
- package/dist/internal/shared.mjs +2 -2
- package/dist/{mistral-BdrQmDWp.mjs → mistral-CKV-TOQj.mjs} +7 -6
- package/dist/number-coercion-DvG7SNMg.mjs +129 -0
- package/dist/openai-BPor_3WI.d.mts +358 -0
- package/dist/{openai-responses-shared-CwnGU0KW.mjs → openai-D3PD6PE-.mjs} +1890 -705
- package/dist/{openai-chatgpt-responses-DP63Qt01.mjs → openai-chatgpt-responses-CedIj0hk.mjs} +54 -58
- package/dist/{openai-completions-zLDfebnN.mjs → openai-completions-CiSutyu0.mjs} +459 -104
- package/dist/openai-prompt-cache-2uo_1OR1.d.mts +7 -0
- package/dist/openai-prompt-cache-mZTCdRPo.mjs +12 -0
- package/dist/prompt-cache-stability-Cwcjv_fx.d.mts +13 -0
- package/dist/{provider-error-LMfTEkfO.mjs → provider-error-apVOZI6G.mjs} +9 -2
- package/dist/providers.d.mts +3 -2
- package/dist/providers.mjs +10 -9
- package/dist/{transform-messages-eb6lxDLB.mjs → shared-CdjNZd35.mjs} +273 -161
- package/dist/{src-CXno1H5g.mjs → src-QkygScBs.mjs} +23 -4
- package/dist/stream-first-event-timeout-C3OgBjIk.mjs +12294 -0
- package/dist/stream-first-event-timeout-DvDeSucC.d.mts +29 -0
- package/dist/transport-stream-shared-BbMELSI4.mjs +297 -0
- package/dist/transports.d.mts +593 -0
- package/dist/transports.mjs +4069 -0
- package/dist/{types-CYORveZ7.d.mts → types-bzp5k29J.d.mts} +8 -0
- package/dist/types.d.mts +5 -5
- package/dist/types.mjs +2 -2
- package/dist/{validation-BX8euiDv.d.mts → validation-B-j7cOYp.d.mts} +1 -1
- package/dist/validation.d.mts +1 -1
- package/package.json +36 -11
- package/dist/azure-openai-responses-client-compat-C7K7QfUE.mjs +0 -62
- package/dist/github-copilot-headers-BsH5cqGj.mjs +0 -48
- package/dist/host-4t713IeR.mjs +0 -37
- package/dist/model-utils-1GiZ2_rr.mjs +0 -64
- package/dist/openai-responses-ClspWt4I.mjs +0 -136
- package/dist/openai-tool-projection-WHf2nuOI.mjs +0 -200
- package/dist/reasoning-tag-text-partitioner-BlVe67g6.mjs +0 -400
- package/dist/stream-first-event-timeout-RjWszj8c.mjs +0 -106
- package/npm-shrinkwrap.json +0 -645
|
@@ -0,0 +1,4069 @@
|
|
|
1
|
+
import { n as getEnvApiKey } from "./env-api-keys-DrgeBuva.mjs";
|
|
2
|
+
import { d as resolveClaudeSonnet5ModelIdentity, g as supportsClaudeNativeXhighEffort, h as supportsClaudeNativeMaxEffort, l as resolveClaudeNativeThinkingLevelMap, p as supportsClaudeAdaptiveThinking, u as resolveClaudeOpus5ModelIdentity } from "./src-QkygScBs.mjs";
|
|
3
|
+
import { r as createAssistantMessageEventStream } from "./event-stream-D8n2uFee.mjs";
|
|
4
|
+
import { n as getAiTransportHost, r as resolveAiTransportHeaderSentinels } from "./host-XYGZcgO8.mjs";
|
|
5
|
+
import { n as clampOpenAIPromptCacheKey } from "./openai-prompt-cache-mZTCdRPo.mjs";
|
|
6
|
+
import { C as extractToolResultBlockText, E as isImageWithMediaPayload, O as isRecord, S as describeToolResultMediaPlaceholder, _ as usesClaudeStreamingRefusalContract, a as splitSystemPromptCacheBoundary, d as applyClaudeRequestContract, f as defaultsClaudeAdaptiveThinking, g as usesClaudeFable5MessagesContract, l as sortPromptCacheToolsByName, m as requiresClaudeAdaptiveThinking, o as stripSystemPromptCacheBoundary, p as prepareClaudeNoPrefillRequestContext, v as hasNonEmptyString, w as extractToolResultText, x as readStringValue, y as normalizeLowercaseStringOrEmpty } from "./shared-CdjNZd35.mjs";
|
|
7
|
+
import { C as transformTransportMessages, S as resolveProviderRequestPolicyConfig, _ as buildGuardedModelFetch, a as projectOpenAITools, b as resolveProviderEndpoint, c as mapOpenAIStopReason, d as resolveOpenAICompletionsCompat, f as resolveOpenAICompletionsResponseFormat, g as tagPendingCommentaryText, h as rememberPendingCommentaryTags, l as resolveOpenAIReasoningEffortMap, m as clearPendingCommentaryText, o as reconcileOpenAICompletionsToolChoice, p as shouldOmitOllamaCompatResponseFormat, s as reconcileOpenAIResponsesToolChoice, t as convertMessages, u as detectOpenAICompletionsCompat, v as resolveModelRequestTimeoutMs, x as resolveProviderRequestCapabilities, y as resolveOpenAIStrictToolSetting } from "./openai-completions-CiSutyu0.mjs";
|
|
8
|
+
import { t as toErrorObject } from "./error-coercion-DgxlWC0n.mjs";
|
|
9
|
+
import { C as normalizeAnthropicInlineContent, S as createAnthropicInlineImageBudget, T as canonicalizeBase64, _ as readAnthropicFallbackBoundary, a as readAnthropicPromptUsageSnapshot, b as omitFoundryBearerCredentialHeaders, c as projectAnthropicTools, d as ANTHROPIC_OMITTED_REASONING_TEXT, f as findActiveAnthropicToolTurnAssistantIndex, g as applyAnthropicFallbackBoundary, l as reconcileAnthropicToolChoice, m as ANTHROPIC_SERVER_SIDE_FALLBACK_BETA, o as readAnthropicUsageTokenCount, p as ANTHROPIC_SERVER_SIDE_FALLBACKS, s as readLastAnthropicIterationUsage, u as resolveOriginalAnthropicToolName, v as resolveAnthropicFallbackServingModelCost, w as resolveAnthropicImageMediaType, x as usesFoundryBearerAuth, y as applyAnthropicRefusal } from "./anthropic-CQVj3le6.mjs";
|
|
10
|
+
import { a as parseStrictPositiveInteger, c as calculateCost, l as clampThinkingLevel, s as applyProviderReportedUsageCost } from "./number-coercion-DvG7SNMg.mjs";
|
|
11
|
+
import { C as resolveSecretSentinel, S as resolveModelHeaderSentinels$1, T as supportsModelTools, _ as isGoogleGemini3ProModel, a as failTransportStream, c as mergeTransportMetadata, d as transportAbortError, f as MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE, g as isGoogleGemini3FlashModel, h as isCodeModeModelVisibleToolName, i as createWritableTransportEventStream, l as sanitizeNonEmptyTransportPayloadText, m as estimateStringChars, n as coerceTransportToolCallArguments, o as finalizeTransportStream, p as createAbortError$1, r as createEmptyTransportUsage, s as mergeTransportHeaders, t as assignTransportErrorDetails, u as sanitizeTransportPayloadText, v as parseRetryAfterSeconds, w as sha256Hex, y as readResponseTextSnippet } from "./transport-stream-shared-BbMELSI4.mjs";
|
|
12
|
+
import { r as createDeferredEventBuffer, t as notifyLlmRequestActivity } from "./llm-request-activity-CehVkZP-.mjs";
|
|
13
|
+
import { n as parseStreamingJson } from "./json-parse-BvXNt1-7.mjs";
|
|
14
|
+
import "./internal/anthropic.mjs";
|
|
15
|
+
import { a as withFirstStreamEventTimeout, i as getFirstStreamEventTimeoutMs, o as createReasoningTagTextPartitioner, r as getFirstStreamEventTimeoutHandler, t as createFirstStreamEventAbortController } from "./stream-first-event-timeout-C3OgBjIk.mjs";
|
|
16
|
+
import "./internal/runtime.mjs";
|
|
17
|
+
import { A as resolveAzureOpenAIApiVersion, At as emitModelTransportDebug, B as stringifyRedactedPayload, C as observeResponsesStream, D as createResponsesStreamWithEncryptedContentRetry, E as convertResponsesMessages, F as buildResponsesFailedNoDetailsObservation, G as GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP, H as summarizeResponsesFailedNoDetailsObservation, I as logResponsesFailedNoDetails, J as resolveCacheRetention, K as createModelStreamCooperativeScheduler, L as normalizeResponsesFailedEvent, M as tagOpenAIResponsesReasoningReplayItem, Mt as resolveModelSseDebugMode, N as resolveReplayableResponsesMessageId, O as isInvalidEncryptedContentError, P as applyServiceTierPricing, Q as OPENAI_CODEX_RESPONSES_DEFAULT_INSTRUCTIONS, R as safeDebugValue, T as buildResponsesInputMessage, U as summarizeResponsesPayload, V as summarizeOpenAITransportError, W as summarizeResponsesTools, X as throwIfModelStreamAborted, Y as resolvePromptCacheKey, Z as AZURE_RESPONSES_FIRST_EVENT_TIMEOUT_MS, d as findOpenAIStrictToolProjectionDiagnostics, dt as isOpenAIGpt55Model, ft as isOpenAIGpt56Model, gt as supportsOpenAIReasoningEffort, h as resolveOpenAIProjectedToolsStrictToolFlag, j as stripResponsesRequestEncryptedContent, jt as resolveModelPayloadDebugMode, k as prepareOpenAIResponsesReasoningItemForReplay, kt as uniqueStrings, mt as resolveOpenAIReasoningEffortForModel, p as normalizeOpenAIStrictToolParameters, pt as normalizeOpenAIReasoningEffort, q as log, ut as isOpenAIGpt54MiniModel, v as ResponsesStreamFailure, vt as isOpenAICompatibleAzureResponsesBaseUrl, w as buildOpenAIResponsesReasoningReplayMetadata, xt as resolveAzureDeploymentNameFromMap, y as processResponsesStream, z as stringifyRedactedEvent } from "./openai-D3PD6PE-.mjs";
|
|
18
|
+
import { randomUUID } from "node:crypto";
|
|
19
|
+
import OpenAI, { AzureOpenAI } from "openai";
|
|
20
|
+
//#region packages/ai/src/transports/anthropic-payload-policy.ts
|
|
21
|
+
/**
|
|
22
|
+
* Anthropic-family request payload policy helpers.
|
|
23
|
+
* Applies service-tier and cache-control markers only when provider endpoint
|
|
24
|
+
* capabilities allow them.
|
|
25
|
+
*/
|
|
26
|
+
const ANTHROPIC_CACHE_CONTROL_LIMIT = 4;
|
|
27
|
+
function resolveBaseUrlHostname(baseUrl) {
|
|
28
|
+
try {
|
|
29
|
+
return new URL(baseUrl).hostname;
|
|
30
|
+
} catch {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function isLongTtlEligibleEndpoint(baseUrl) {
|
|
35
|
+
if (typeof baseUrl !== "string") return false;
|
|
36
|
+
const hostname = resolveBaseUrlHostname(baseUrl);
|
|
37
|
+
if (!hostname) return false;
|
|
38
|
+
return hostname === "api.anthropic.com" || hostname === "aiplatform.googleapis.com" || hostname.endsWith("-aiplatform.googleapis.com");
|
|
39
|
+
}
|
|
40
|
+
/** Resolve Anthropic cache-control marker retention for a request endpoint. */
|
|
41
|
+
function resolveAnthropicEphemeralCacheControl(baseUrl, cacheRetention) {
|
|
42
|
+
const retention = cacheRetention ?? (process.env.OPENCLAW_CACHE_RETENTION === "long" ? "long" : "short");
|
|
43
|
+
if (retention === "none") return;
|
|
44
|
+
const ttl = retention === "long" && (cacheRetention === "long" || isLongTtlEligibleEndpoint(baseUrl)) ? "1h" : void 0;
|
|
45
|
+
return {
|
|
46
|
+
type: "ephemeral",
|
|
47
|
+
...ttl ? { ttl } : {}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function applyAnthropicCacheControlToSystem(system, cacheControl) {
|
|
51
|
+
if (!Array.isArray(system)) return;
|
|
52
|
+
const normalizedBlocks = [];
|
|
53
|
+
for (const block of system) {
|
|
54
|
+
if (!block || typeof block !== "object") {
|
|
55
|
+
normalizedBlocks.push(block);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const record = block;
|
|
59
|
+
if (record.type !== "text" || typeof record.text !== "string") {
|
|
60
|
+
normalizedBlocks.push(block);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const split = splitSystemPromptCacheBoundary(record.text);
|
|
64
|
+
if (!split) {
|
|
65
|
+
if (record.cache_control === void 0) record.cache_control = cacheControl;
|
|
66
|
+
normalizedBlocks.push(record);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const { cache_control: existingCacheControl, ...rest } = record;
|
|
70
|
+
if (split.stablePrefix) normalizedBlocks.push({
|
|
71
|
+
...rest,
|
|
72
|
+
text: split.stablePrefix,
|
|
73
|
+
cache_control: existingCacheControl ?? cacheControl
|
|
74
|
+
});
|
|
75
|
+
if (split.dynamicSuffix) normalizedBlocks.push({
|
|
76
|
+
...rest,
|
|
77
|
+
text: split.dynamicSuffix
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
system.splice(0, system.length, ...normalizedBlocks);
|
|
81
|
+
}
|
|
82
|
+
function stripAnthropicSystemPromptBoundary(system) {
|
|
83
|
+
if (!Array.isArray(system)) return;
|
|
84
|
+
for (const block of system) {
|
|
85
|
+
if (!block || typeof block !== "object") continue;
|
|
86
|
+
const record = block;
|
|
87
|
+
if (record.type === "text" && typeof record.text === "string") record.text = stripSystemPromptCacheBoundary(record.text);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimit, cacheBreakpointOptOutMessageIndexes) {
|
|
91
|
+
if (!Array.isArray(messages) || messages.length === 0 || markerLimit <= 0) return;
|
|
92
|
+
let fallbackToolResult;
|
|
93
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
94
|
+
const message = messages[i];
|
|
95
|
+
if (!message || typeof message !== "object") continue;
|
|
96
|
+
const record = message;
|
|
97
|
+
if (record.role !== "user" || cacheBreakpointOptOutMessageIndexes.has(i)) continue;
|
|
98
|
+
const content = record.content;
|
|
99
|
+
if (typeof content === "string") {
|
|
100
|
+
if (fallbackToolResult && markerLimit === 1) {
|
|
101
|
+
fallbackToolResult.cache_control = cacheControl;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
record.content = [{
|
|
105
|
+
type: "text",
|
|
106
|
+
text: content,
|
|
107
|
+
cache_control: cacheControl
|
|
108
|
+
}];
|
|
109
|
+
if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (!Array.isArray(content)) continue;
|
|
113
|
+
for (let j = content.length - 1; j >= 0; j--) {
|
|
114
|
+
const block = content[j];
|
|
115
|
+
if (!block || typeof block !== "object") continue;
|
|
116
|
+
const blockRecord = block;
|
|
117
|
+
if (blockRecord.type === "text" || blockRecord.type === "image") {
|
|
118
|
+
if (fallbackToolResult && markerLimit === 1) {
|
|
119
|
+
fallbackToolResult.cache_control = cacheControl;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
blockRecord.cache_control = cacheControl;
|
|
123
|
+
if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (blockRecord.type === "tool_result" && fallbackToolResult === void 0) fallbackToolResult = blockRecord;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (fallbackToolResult) fallbackToolResult.cache_control = cacheControl;
|
|
130
|
+
}
|
|
131
|
+
function countAnthropicCacheControlMarkers(blocks) {
|
|
132
|
+
if (!Array.isArray(blocks)) return 0;
|
|
133
|
+
let count = 0;
|
|
134
|
+
for (const block of blocks) if (block && typeof block === "object" && "cache_control" in block) count += 1;
|
|
135
|
+
return count;
|
|
136
|
+
}
|
|
137
|
+
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
138
|
+
function resolveAnthropicPayloadPolicy(input) {
|
|
139
|
+
return {
|
|
140
|
+
allowsServiceTier: resolveProviderRequestCapabilities({
|
|
141
|
+
provider: input.provider,
|
|
142
|
+
api: input.api,
|
|
143
|
+
baseUrl: input.baseUrl,
|
|
144
|
+
capability: "llm",
|
|
145
|
+
transport: "stream"
|
|
146
|
+
}).allowsAnthropicServiceTier,
|
|
147
|
+
cacheControl: input.enableCacheControl === true ? resolveAnthropicEphemeralCacheControl(input.baseUrl, input.cacheRetention) : void 0,
|
|
148
|
+
serviceTier: input.serviceTier
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
152
|
+
function applyAnthropicPayloadPolicyToParams(payloadObj, policy, cacheBreakpointOptOutMessageIndexes) {
|
|
153
|
+
if (policy.allowsServiceTier && policy.serviceTier !== void 0 && payloadObj.service_tier === void 0) payloadObj.service_tier = policy.serviceTier;
|
|
154
|
+
if (policy.cacheControl) applyAnthropicCacheControlToSystem(payloadObj.system, policy.cacheControl);
|
|
155
|
+
else stripAnthropicSystemPromptBoundary(payloadObj.system);
|
|
156
|
+
if (!policy.cacheControl) return;
|
|
157
|
+
const usedMarkers = countAnthropicCacheControlMarkers(payloadObj.system) + countAnthropicCacheControlMarkers(payloadObj.tools);
|
|
158
|
+
applyAnthropicCacheControlToMessages(payloadObj.messages, policy.cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
|
|
159
|
+
}
|
|
160
|
+
/** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
|
|
161
|
+
function applyAnthropicEphemeralCacheControlMarkers(payloadObj, cacheControl = { type: "ephemeral" }) {
|
|
162
|
+
const messages = payloadObj.messages;
|
|
163
|
+
if (!Array.isArray(messages)) return;
|
|
164
|
+
for (const message of messages) {
|
|
165
|
+
if (message.role === "system" || message.role === "developer") {
|
|
166
|
+
if (!cacheControl) continue;
|
|
167
|
+
if (typeof message.content === "string") {
|
|
168
|
+
message.content = [{
|
|
169
|
+
type: "text",
|
|
170
|
+
text: message.content,
|
|
171
|
+
cache_control: cacheControl
|
|
172
|
+
}];
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (Array.isArray(message.content) && message.content.length > 0) {
|
|
176
|
+
const last = message.content[message.content.length - 1];
|
|
177
|
+
if (last && typeof last === "object") {
|
|
178
|
+
const record = last;
|
|
179
|
+
if (record.type !== "thinking" && record.type !== "redacted_thinking") record.cache_control = cacheControl;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (message.role === "assistant" && Array.isArray(message.content)) for (const block of message.content) {
|
|
185
|
+
if (!block || typeof block !== "object") continue;
|
|
186
|
+
const record = block;
|
|
187
|
+
if (record.type === "thinking" || record.type === "redacted_thinking") delete record.cache_control;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region packages/ai/src/transports/json-unsafe-integers.ts
|
|
193
|
+
/**
|
|
194
|
+
* JSON parsing helpers that preserve integer literals larger than
|
|
195
|
+
* Number.MAX_SAFE_INTEGER as strings before JSON.parse can round them.
|
|
196
|
+
*/
|
|
197
|
+
const MAX_SAFE_INTEGER_ABS_STR = String(Number.MAX_SAFE_INTEGER);
|
|
198
|
+
function isAsciiDigit(ch) {
|
|
199
|
+
return ch !== void 0 && ch >= "0" && ch <= "9";
|
|
200
|
+
}
|
|
201
|
+
function parseJsonNumberToken(input, start) {
|
|
202
|
+
let idx = start;
|
|
203
|
+
if (input[idx] === "-") idx += 1;
|
|
204
|
+
if (idx >= input.length) return null;
|
|
205
|
+
if (input[idx] === "0") idx += 1;
|
|
206
|
+
else if (isAsciiDigit(input[idx]) && input[idx] !== "0") while (isAsciiDigit(input[idx])) idx += 1;
|
|
207
|
+
else return null;
|
|
208
|
+
let isInteger = true;
|
|
209
|
+
if (input[idx] === ".") {
|
|
210
|
+
isInteger = false;
|
|
211
|
+
idx += 1;
|
|
212
|
+
if (!isAsciiDigit(input[idx])) return null;
|
|
213
|
+
while (isAsciiDigit(input[idx])) idx += 1;
|
|
214
|
+
}
|
|
215
|
+
if (input[idx] === "e" || input[idx] === "E") {
|
|
216
|
+
isInteger = false;
|
|
217
|
+
idx += 1;
|
|
218
|
+
if (input[idx] === "+" || input[idx] === "-") idx += 1;
|
|
219
|
+
if (!isAsciiDigit(input[idx])) return null;
|
|
220
|
+
while (isAsciiDigit(input[idx])) idx += 1;
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
token: input.slice(start, idx),
|
|
224
|
+
end: idx,
|
|
225
|
+
isInteger
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function isUnsafeIntegerLiteral(token) {
|
|
229
|
+
const digits = token[0] === "-" ? token.slice(1) : token;
|
|
230
|
+
if (digits.length < MAX_SAFE_INTEGER_ABS_STR.length) return false;
|
|
231
|
+
if (digits.length > MAX_SAFE_INTEGER_ABS_STR.length) return true;
|
|
232
|
+
return digits > MAX_SAFE_INTEGER_ABS_STR;
|
|
233
|
+
}
|
|
234
|
+
/** Quotes integer literals above Number.MAX_SAFE_INTEGER before JSON.parse. */
|
|
235
|
+
function quoteUnsafeIntegerLiterals(input) {
|
|
236
|
+
let out = "";
|
|
237
|
+
let inString = false;
|
|
238
|
+
let escaped = false;
|
|
239
|
+
let idx = 0;
|
|
240
|
+
while (idx < input.length) {
|
|
241
|
+
const ch = input[idx] ?? "";
|
|
242
|
+
if (inString) {
|
|
243
|
+
out += ch;
|
|
244
|
+
if (escaped) escaped = false;
|
|
245
|
+
else if (ch === "\\") escaped = true;
|
|
246
|
+
else if (ch === "\"") inString = false;
|
|
247
|
+
idx += 1;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (ch === "\"") {
|
|
251
|
+
inString = true;
|
|
252
|
+
out += ch;
|
|
253
|
+
idx += 1;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (ch === "-" || isAsciiDigit(ch)) {
|
|
257
|
+
const parsed = parseJsonNumberToken(input, idx);
|
|
258
|
+
if (parsed) {
|
|
259
|
+
if (parsed.isInteger && isUnsafeIntegerLiteral(parsed.token)) out += `"${parsed.token}"`;
|
|
260
|
+
else out += parsed.token;
|
|
261
|
+
idx = parsed.end;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
out += ch;
|
|
266
|
+
idx += 1;
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
/** Parses JSON while preserving unsafe integer literals as strings. */
|
|
271
|
+
function parseJsonPreservingUnsafeIntegers(input) {
|
|
272
|
+
return JSON.parse(quoteUnsafeIntegerLiterals(input));
|
|
273
|
+
}
|
|
274
|
+
/** Parses or accepts an object while preserving unsafe integer literals in string input. */
|
|
275
|
+
function parseJsonObjectPreservingUnsafeIntegers(value) {
|
|
276
|
+
if (typeof value === "string") {
|
|
277
|
+
try {
|
|
278
|
+
const parsed = parseJsonPreservingUnsafeIntegers(value);
|
|
279
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) return parsed;
|
|
280
|
+
} catch {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return value;
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
//#endregion
|
|
289
|
+
//#region packages/ai/src/transports/anthropic-transport-stream.ts
|
|
290
|
+
/**
|
|
291
|
+
* Native Anthropic Messages streaming transport.
|
|
292
|
+
* Converts OpenClaw contexts/tools into Anthropic payloads, streams SSE events
|
|
293
|
+
* back into runtime output blocks, and applies provider request policy.
|
|
294
|
+
*/
|
|
295
|
+
const CLAUDE_CODE_VERSION = "2.1.75";
|
|
296
|
+
const CLAUDE_CODE_BILLING_SYSTEM_BLOCK = `x-anthropic-billing-header: cc_version=${CLAUDE_CODE_VERSION}; cc_entrypoint=sdk-cli;`;
|
|
297
|
+
const ANTHROPIC_MESSAGES_ERROR_BODY_MAX_BYTES = 8 * 1024;
|
|
298
|
+
const ANTHROPIC_MESSAGES_ERROR_BODY_MAX_CHARS = 400;
|
|
299
|
+
const ANTHROPIC_MESSAGES_ERROR_BODY_READ_IDLE_TIMEOUT_MS = 1e4;
|
|
300
|
+
const ANTHROPIC_MESSAGES_DEFAULT_MAX_TOKENS = 4096;
|
|
301
|
+
const ANTHROPIC_MESSAGES_FALLBACK_CONTEXT_DIVISOR = 4;
|
|
302
|
+
const ANTHROPIC_MESSAGES_SSE_PENDING_BUFFER_MAX_CHARS = 16 * 1024 * 1024;
|
|
303
|
+
const CLAUDE_CODE_TOOL_LOOKUP = new Map([
|
|
304
|
+
"Read",
|
|
305
|
+
"Write",
|
|
306
|
+
"Edit",
|
|
307
|
+
"Bash",
|
|
308
|
+
"Grep",
|
|
309
|
+
"Glob",
|
|
310
|
+
"AskUserQuestion",
|
|
311
|
+
"EnterPlanMode",
|
|
312
|
+
"ExitPlanMode",
|
|
313
|
+
"KillShell",
|
|
314
|
+
"NotebookEdit",
|
|
315
|
+
"Skill",
|
|
316
|
+
"Task",
|
|
317
|
+
"TaskOutput",
|
|
318
|
+
"TodoWrite",
|
|
319
|
+
"WebFetch",
|
|
320
|
+
"WebSearch"
|
|
321
|
+
].map((tool) => [normalizeLowercaseStringOrEmpty(tool), tool]));
|
|
322
|
+
function resolveAnthropicRequestModelId(model) {
|
|
323
|
+
if (isDirectAnthropicModel(model) && /^anthropic\//i.test(model.id)) return model.id.replace(/^anthropic\//i, "");
|
|
324
|
+
return model.id;
|
|
325
|
+
}
|
|
326
|
+
const EMPTY_ANTHROPIC_MESSAGES_FALLBACK_TEXT = ".";
|
|
327
|
+
function normalizeAnthropicToolChoice(thinkingEnabled, toolChoice) {
|
|
328
|
+
if (thinkingEnabled && (toolChoice === "any" || typeof toolChoice === "object" && toolChoice.type === "tool")) return { type: "auto" };
|
|
329
|
+
return typeof toolChoice === "string" ? { type: toolChoice } : toolChoice;
|
|
330
|
+
}
|
|
331
|
+
function supportsNativeXhighEffort(model) {
|
|
332
|
+
return supportsClaudeNativeXhighEffort(model);
|
|
333
|
+
}
|
|
334
|
+
function supportsAdaptiveThinking(model) {
|
|
335
|
+
return supportsClaudeAdaptiveThinking(model);
|
|
336
|
+
}
|
|
337
|
+
function mapThinkingLevelToEffort(level, model) {
|
|
338
|
+
const thinkingLevelMap = resolveClaudeNativeThinkingLevelMap(model);
|
|
339
|
+
const resolvedLevel = clampThinkingLevel({
|
|
340
|
+
...model,
|
|
341
|
+
...typeof model.params?.canonicalModelId === "string" ? { reasoning: true } : {},
|
|
342
|
+
...thinkingLevelMap ? { thinkingLevelMap } : {}
|
|
343
|
+
}, level);
|
|
344
|
+
const mapped = thinkingLevelMap?.[resolvedLevel];
|
|
345
|
+
if (typeof mapped === "string") return mapped;
|
|
346
|
+
switch (resolvedLevel) {
|
|
347
|
+
case "off":
|
|
348
|
+
case "minimal":
|
|
349
|
+
case "low": return "low";
|
|
350
|
+
case "medium": return "medium";
|
|
351
|
+
case "xhigh": return supportsNativeXhighEffort(model) ? "xhigh" : "high";
|
|
352
|
+
case "max": return supportsClaudeNativeMaxEffort(model) ? "max" : "high";
|
|
353
|
+
default: return "high";
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function clampReasoningLevel(level) {
|
|
357
|
+
return level === "xhigh" || level === "max" ? "high" : level;
|
|
358
|
+
}
|
|
359
|
+
function resolvePositiveAnthropicTokenLimit(value) {
|
|
360
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return;
|
|
361
|
+
const floored = Math.floor(value);
|
|
362
|
+
return floored > 0 ? floored : void 0;
|
|
363
|
+
}
|
|
364
|
+
function resolveAnthropicMessagesMaxTokens(params) {
|
|
365
|
+
const requested = resolvePositiveAnthropicTokenLimit(params.requestedMaxTokens);
|
|
366
|
+
if (requested !== void 0) return requested;
|
|
367
|
+
const modelMax = resolvePositiveAnthropicTokenLimit(params.modelMaxTokens);
|
|
368
|
+
if (modelMax !== void 0) return params.useModelDefault ? modelMax : Math.min(modelMax, 32e3);
|
|
369
|
+
if (params.modelMaxTokens !== void 0) return;
|
|
370
|
+
const contextWindow = resolvePositiveAnthropicTokenLimit(params.modelContextWindow);
|
|
371
|
+
return contextWindow === void 0 ? ANTHROPIC_MESSAGES_DEFAULT_MAX_TOKENS : Math.max(1, Math.min(ANTHROPIC_MESSAGES_DEFAULT_MAX_TOKENS, Math.floor(contextWindow / ANTHROPIC_MESSAGES_FALLBACK_CONTEXT_DIVISOR)));
|
|
372
|
+
}
|
|
373
|
+
function adjustMaxTokensForThinking(params) {
|
|
374
|
+
const budgets = {
|
|
375
|
+
minimal: 1024,
|
|
376
|
+
low: 2048,
|
|
377
|
+
medium: 8192,
|
|
378
|
+
high: 16384,
|
|
379
|
+
...params.customBudgets
|
|
380
|
+
};
|
|
381
|
+
const minOutputTokens = 1024;
|
|
382
|
+
let thinkingBudget = budgets[clampReasoningLevel(params.reasoningLevel)];
|
|
383
|
+
const maxTokens = Math.min(params.baseMaxTokens + thinkingBudget, params.modelMaxTokens);
|
|
384
|
+
if (maxTokens <= thinkingBudget) thinkingBudget = Math.max(0, maxTokens - minOutputTokens);
|
|
385
|
+
return {
|
|
386
|
+
maxTokens,
|
|
387
|
+
thinkingBudget
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function isAnthropicOAuthToken(apiKey) {
|
|
391
|
+
return (resolveSecretSentinel(apiKey) ?? apiKey).includes("sk-ant-oat");
|
|
392
|
+
}
|
|
393
|
+
function isDirectAnthropicModel(model) {
|
|
394
|
+
if (normalizeLowercaseStringOrEmpty(model.provider) !== "anthropic") return false;
|
|
395
|
+
const endpointClass = resolveProviderEndpoint(model.baseUrl).endpointClass;
|
|
396
|
+
return endpointClass === "default" || endpointClass === "anthropic-public";
|
|
397
|
+
}
|
|
398
|
+
function isKimiAnthropicProvider(provider) {
|
|
399
|
+
return /^kimi(?:-|$)/.test(normalizeLowercaseStringOrEmpty(provider ?? ""));
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Server-side refusal fallback is a first-party Claude API beta: proxies and
|
|
403
|
+
* Bedrock/Vertex/Foundry reject the `fallbacks` param, and OAuth (Claude Code
|
|
404
|
+
* identity) requests are excluded until the beta is verified there.
|
|
405
|
+
*/
|
|
406
|
+
function useAnthropicServerSideFallback(model) {
|
|
407
|
+
return (usesClaudeFable5MessagesContract(model) || resolveClaudeOpus5ModelIdentity(model) !== void 0) && isDirectAnthropicModel(model);
|
|
408
|
+
}
|
|
409
|
+
function supportsReasoningContentReplay(model) {
|
|
410
|
+
return resolveProviderEndpoint(model.baseUrl).endpointClass === "xiaomi-native";
|
|
411
|
+
}
|
|
412
|
+
function buildAnthropicBetaHeader(model, betaFeatures, params) {
|
|
413
|
+
if (!isDirectAnthropicModel(model)) return;
|
|
414
|
+
return params.oauth ? `claude-code-20250219,oauth-2025-04-20,${betaFeatures.join(",")}` : betaFeatures.join(",");
|
|
415
|
+
}
|
|
416
|
+
function toClaudeCodeName(name) {
|
|
417
|
+
return CLAUDE_CODE_TOOL_LOOKUP.get(normalizeLowercaseStringOrEmpty(name)) ?? name;
|
|
418
|
+
}
|
|
419
|
+
const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
420
|
+
async function convertContentBlocks(content, model, imageBudget) {
|
|
421
|
+
const text = extractToolResultText(content);
|
|
422
|
+
const mediaPlaceholder = describeToolResultMediaPlaceholder(content);
|
|
423
|
+
if (!(model.input.includes("image") && content.some(isImageWithMediaPayload))) return sanitizeNonEmptyTransportPayloadText(text, mediaPlaceholder ?? "(no output)");
|
|
424
|
+
const blocks = [];
|
|
425
|
+
let hasTextBlock = false;
|
|
426
|
+
for (const block of content) {
|
|
427
|
+
if (!block || typeof block !== "object") continue;
|
|
428
|
+
const record = block;
|
|
429
|
+
const blockText = extractToolResultBlockText(block);
|
|
430
|
+
if (blockText) {
|
|
431
|
+
blocks.push({
|
|
432
|
+
type: "text",
|
|
433
|
+
text: sanitizeTransportPayloadText(blockText)
|
|
434
|
+
});
|
|
435
|
+
hasTextBlock = true;
|
|
436
|
+
}
|
|
437
|
+
if (!isImageWithMediaPayload(record)) continue;
|
|
438
|
+
const [normalizedImage] = await normalizeAnthropicInlineContent([{
|
|
439
|
+
type: "image",
|
|
440
|
+
data: typeof record.data === "string" ? record.data : "",
|
|
441
|
+
mimeType: typeof record.mimeType === "string" ? record.mimeType : "image/png"
|
|
442
|
+
}], imageBudget);
|
|
443
|
+
if (normalizedImage?.type !== "image") continue;
|
|
444
|
+
blocks.push({
|
|
445
|
+
type: "image",
|
|
446
|
+
source: {
|
|
447
|
+
type: "base64",
|
|
448
|
+
media_type: resolveAnthropicImageMediaType(normalizedImage.mimeType),
|
|
449
|
+
data: normalizedImage.data
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
if (!hasTextBlock) blocks.unshift({
|
|
454
|
+
type: "text",
|
|
455
|
+
text: mediaPlaceholder ?? "(see attached image)"
|
|
456
|
+
});
|
|
457
|
+
return blocks;
|
|
458
|
+
}
|
|
459
|
+
function normalizeToolCallId(id) {
|
|
460
|
+
return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
|
|
461
|
+
}
|
|
462
|
+
async function convertAnthropicMessages(messages, model, isOAuthToken, options) {
|
|
463
|
+
const params = [];
|
|
464
|
+
const imageBudget = createAnthropicInlineImageBudget();
|
|
465
|
+
const allowReasoningContentReplay = options.allowReasoningContentReplay === true;
|
|
466
|
+
const replayThinkingEnabled = options.replayThinkingEnabled !== false;
|
|
467
|
+
const transformedMessages = transformTransportMessages(messages, model, normalizeToolCallId);
|
|
468
|
+
const activeToolTurnAssistantIndex = replayThinkingEnabled ? -1 : findActiveAnthropicToolTurnAssistantIndex(transformedMessages);
|
|
469
|
+
for (let i = 0; i < transformedMessages.length; i += 1) {
|
|
470
|
+
const msg = transformedMessages[i];
|
|
471
|
+
if (!msg) continue;
|
|
472
|
+
if (msg.role === "user") {
|
|
473
|
+
const isRuntimeContextCarrier = msg.runtimeContextCarrier === true;
|
|
474
|
+
if (typeof msg.content === "string") {
|
|
475
|
+
if (msg.content.trim().length > 0) {
|
|
476
|
+
const userParam = {
|
|
477
|
+
role: "user",
|
|
478
|
+
content: sanitizeTransportPayloadText(msg.content)
|
|
479
|
+
};
|
|
480
|
+
if (isRuntimeContextCarrier) options.cacheBreakpointOptOutMessageIndexes.add(params.length);
|
|
481
|
+
params.push(userParam);
|
|
482
|
+
}
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
const blocks = (model.input.includes("image") ? await normalizeAnthropicInlineContent(msg.content, imageBudget) : msg.content.map((item) => item.type === "image" ? {
|
|
486
|
+
type: "text",
|
|
487
|
+
text: NON_VISION_USER_IMAGE_PLACEHOLDER
|
|
488
|
+
} : item)).map((item) => item.type === "text" ? {
|
|
489
|
+
type: "text",
|
|
490
|
+
text: sanitizeTransportPayloadText(item.text)
|
|
491
|
+
} : {
|
|
492
|
+
type: "image",
|
|
493
|
+
source: {
|
|
494
|
+
type: "base64",
|
|
495
|
+
media_type: resolveAnthropicImageMediaType(item.mimeType),
|
|
496
|
+
data: item.data
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
let filteredBlocks = model.input.includes("image") ? blocks : blocks.filter((block) => block.type !== "image");
|
|
500
|
+
filteredBlocks = filteredBlocks.filter((block) => block.type !== "text" || block.text.trim().length > 0);
|
|
501
|
+
if (filteredBlocks.length === 0) continue;
|
|
502
|
+
const userParam = {
|
|
503
|
+
role: "user",
|
|
504
|
+
content: filteredBlocks
|
|
505
|
+
};
|
|
506
|
+
if (isRuntimeContextCarrier) options.cacheBreakpointOptOutMessageIndexes.add(params.length);
|
|
507
|
+
params.push(userParam);
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (msg.role === "assistant") {
|
|
511
|
+
const blocks = [];
|
|
512
|
+
const reasoningContent = [];
|
|
513
|
+
let omittedThinking = false;
|
|
514
|
+
for (const block of msg.content) {
|
|
515
|
+
if (block.type === "text") {
|
|
516
|
+
if (block.text.trim().length > 0) blocks.push({
|
|
517
|
+
type: "text",
|
|
518
|
+
text: sanitizeTransportPayloadText(block.text)
|
|
519
|
+
});
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (block.type === "thinking") {
|
|
523
|
+
const thinkingSignature = block.thinkingSignature?.trim();
|
|
524
|
+
const isReasoningContent = thinkingSignature === "reasoning_content";
|
|
525
|
+
if (!replayThinkingEnabled && i !== activeToolTurnAssistantIndex && !isReasoningContent) {
|
|
526
|
+
omittedThinking = true;
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
if (block.redacted) {
|
|
530
|
+
blocks.push({
|
|
531
|
+
type: "redacted_thinking",
|
|
532
|
+
data: block.thinkingSignature
|
|
533
|
+
});
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const hasNativeThinkingSignature = Boolean(thinkingSignature) && !isReasoningContent;
|
|
537
|
+
if (block.thinking.trim().length === 0 && !hasNativeThinkingSignature) continue;
|
|
538
|
+
if (!thinkingSignature) blocks.push({
|
|
539
|
+
type: "text",
|
|
540
|
+
text: sanitizeTransportPayloadText(block.thinking)
|
|
541
|
+
});
|
|
542
|
+
else {
|
|
543
|
+
const thinking = thinkingSignature === "reasoning_content" ? sanitizeTransportPayloadText(block.thinking) : block.thinking;
|
|
544
|
+
if (thinkingSignature === "reasoning_content") {
|
|
545
|
+
if (allowReasoningContentReplay) {
|
|
546
|
+
blocks.push({
|
|
547
|
+
type: "thinking",
|
|
548
|
+
thinking,
|
|
549
|
+
signature: thinkingSignature
|
|
550
|
+
});
|
|
551
|
+
reasoningContent.push(thinking);
|
|
552
|
+
}
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
blocks.push({
|
|
556
|
+
type: "thinking",
|
|
557
|
+
thinking,
|
|
558
|
+
signature: thinkingSignature
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
if (block.type === "toolCall") blocks.push({
|
|
564
|
+
type: "tool_use",
|
|
565
|
+
id: block.id,
|
|
566
|
+
name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
|
|
567
|
+
input: coerceTransportToolCallArguments(block.arguments)
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
if (blocks.length === 0 && omittedThinking) blocks.push({
|
|
571
|
+
type: "text",
|
|
572
|
+
text: ANTHROPIC_OMITTED_REASONING_TEXT
|
|
573
|
+
});
|
|
574
|
+
if (blocks.length > 0) {
|
|
575
|
+
const assistantMsg = {
|
|
576
|
+
role: "assistant",
|
|
577
|
+
content: blocks
|
|
578
|
+
};
|
|
579
|
+
if (reasoningContent.length > 0) assistantMsg.reasoning_content = reasoningContent.join("\n");
|
|
580
|
+
else if (allowReasoningContentReplay) blocks.unshift({
|
|
581
|
+
type: "thinking",
|
|
582
|
+
thinking: "",
|
|
583
|
+
signature: "reasoning_content"
|
|
584
|
+
});
|
|
585
|
+
params.push(assistantMsg);
|
|
586
|
+
}
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
if (msg.role === "toolResult") {
|
|
590
|
+
const toolResult = msg;
|
|
591
|
+
const toolResults = [{
|
|
592
|
+
type: "tool_result",
|
|
593
|
+
tool_use_id: toolResult.toolCallId,
|
|
594
|
+
content: await convertContentBlocks(toolResult.content, model, imageBudget),
|
|
595
|
+
is_error: toolResult.isError
|
|
596
|
+
}];
|
|
597
|
+
let j = i + 1;
|
|
598
|
+
while (j < transformedMessages.length) {
|
|
599
|
+
const nextMsg = transformedMessages.at(j);
|
|
600
|
+
if (nextMsg?.role !== "toolResult") break;
|
|
601
|
+
toolResults.push({
|
|
602
|
+
type: "tool_result",
|
|
603
|
+
tool_use_id: nextMsg.toolCallId,
|
|
604
|
+
content: await convertContentBlocks(nextMsg.content, model, imageBudget),
|
|
605
|
+
is_error: nextMsg.isError
|
|
606
|
+
});
|
|
607
|
+
j += 1;
|
|
608
|
+
}
|
|
609
|
+
i = j - 1;
|
|
610
|
+
params.push({
|
|
611
|
+
role: "user",
|
|
612
|
+
content: toolResults
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return params;
|
|
617
|
+
}
|
|
618
|
+
function ensureNonEmptyAnthropicMessages(messages) {
|
|
619
|
+
return messages.length > 0 ? messages : [{
|
|
620
|
+
role: "user",
|
|
621
|
+
content: EMPTY_ANTHROPIC_MESSAGES_FALLBACK_TEXT
|
|
622
|
+
}];
|
|
623
|
+
}
|
|
624
|
+
function convertAnthropicTools(tools, isOAuthToken) {
|
|
625
|
+
const projection = projectAnthropicTools(tools ?? [], (name) => isOAuthToken ? toClaudeCodeName(name) : name);
|
|
626
|
+
const converted = [];
|
|
627
|
+
for (const tool of projection.tools) converted.push({
|
|
628
|
+
name: tool.wireName,
|
|
629
|
+
description: tool.description,
|
|
630
|
+
input_schema: tool.inputSchema
|
|
631
|
+
});
|
|
632
|
+
return {
|
|
633
|
+
projection,
|
|
634
|
+
tools: converted
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
function parseAnthropicToolCallArguments(inputJson) {
|
|
638
|
+
return parseJsonObjectPreservingUnsafeIntegers(inputJson) ?? parseStreamingJson(inputJson);
|
|
639
|
+
}
|
|
640
|
+
function mapStopReason(reason) {
|
|
641
|
+
switch (reason) {
|
|
642
|
+
case "end_turn": return "stop";
|
|
643
|
+
case "max_tokens": return "length";
|
|
644
|
+
case "tool_use": return "toolUse";
|
|
645
|
+
case "pause_turn": return "stop";
|
|
646
|
+
case "refusal":
|
|
647
|
+
case "sensitive": return "error";
|
|
648
|
+
case "stop_sequence": return "stop";
|
|
649
|
+
default: throw new Error(`Unhandled stop reason: ${String(reason)}`);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
const DEFAULT_ANTHROPIC_BASE_URL = "https://api.anthropic.com";
|
|
653
|
+
/** Resolve the effective Anthropic API base URL from model or environment. */
|
|
654
|
+
function resolveAnthropicBaseUrl(baseUrl) {
|
|
655
|
+
return baseUrl?.trim() || process.env.ANTHROPIC_BASE_URL?.trim() || DEFAULT_ANTHROPIC_BASE_URL;
|
|
656
|
+
}
|
|
657
|
+
/** Resolve the Anthropic Messages endpoint URL for the effective base URL. */
|
|
658
|
+
function resolveAnthropicMessagesUrl(baseUrl) {
|
|
659
|
+
const normalized = resolveAnthropicBaseUrl(baseUrl).replace(/\/+$/, "");
|
|
660
|
+
return normalized.endsWith("/v1") ? `${normalized}/messages` : `${normalized}/v1/messages`;
|
|
661
|
+
}
|
|
662
|
+
function withEffectiveAnthropicBaseUrl(model) {
|
|
663
|
+
const baseUrl = resolveAnthropicBaseUrl(model.baseUrl);
|
|
664
|
+
return baseUrl === model.baseUrl ? model : {
|
|
665
|
+
...model,
|
|
666
|
+
baseUrl
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
function createAbortError(signal) {
|
|
670
|
+
const reason = signal.reason;
|
|
671
|
+
if (reason instanceof Error) return reason;
|
|
672
|
+
return createAbortError$1("Request was aborted", reason === void 0 ? void 0 : { cause: reason });
|
|
673
|
+
}
|
|
674
|
+
function readAnthropicSseChunk(reader, signal) {
|
|
675
|
+
if (!signal) return reader.read();
|
|
676
|
+
return new Promise((resolve, reject) => {
|
|
677
|
+
let settled = false;
|
|
678
|
+
const onAbort = () => {
|
|
679
|
+
if (settled) return;
|
|
680
|
+
settled = true;
|
|
681
|
+
signal.removeEventListener("abort", onAbort);
|
|
682
|
+
reader.cancel(signal.reason).catch(() => void 0);
|
|
683
|
+
reject(createAbortError(signal));
|
|
684
|
+
};
|
|
685
|
+
if (signal.aborted) {
|
|
686
|
+
onAbort();
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
690
|
+
reader.read().then((result) => {
|
|
691
|
+
if (settled) return;
|
|
692
|
+
settled = true;
|
|
693
|
+
signal.removeEventListener("abort", onAbort);
|
|
694
|
+
resolve(result);
|
|
695
|
+
}, (error) => {
|
|
696
|
+
if (settled) return;
|
|
697
|
+
settled = true;
|
|
698
|
+
signal.removeEventListener("abort", onAbort);
|
|
699
|
+
reject(toErrorObject(error, "Non-Error rejection"));
|
|
700
|
+
});
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
function parseAnthropicSseEventData(data) {
|
|
704
|
+
try {
|
|
705
|
+
return JSON.parse(data);
|
|
706
|
+
} catch (error) {
|
|
707
|
+
if (error instanceof SyntaxError) throw new Error(MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE, { cause: error });
|
|
708
|
+
throw error;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
function assertAnthropicSsePendingBufferWithinLimit(pendingChars) {
|
|
712
|
+
if (pendingChars <= ANTHROPIC_MESSAGES_SSE_PENDING_BUFFER_MAX_CHARS) return;
|
|
713
|
+
throw new Error(`Anthropic Messages SSE response exceeded max pending buffer size (${ANTHROPIC_MESSAGES_SSE_PENDING_BUFFER_MAX_CHARS} chars) without event boundary`);
|
|
714
|
+
}
|
|
715
|
+
async function* parseAnthropicSseBody(body, signal) {
|
|
716
|
+
const reader = body.getReader();
|
|
717
|
+
const decoder = new TextDecoder();
|
|
718
|
+
let buffer = "";
|
|
719
|
+
let completed = false;
|
|
720
|
+
try {
|
|
721
|
+
while (true) {
|
|
722
|
+
const { done, value } = await readAnthropicSseChunk(reader, signal);
|
|
723
|
+
if (done) {
|
|
724
|
+
completed = true;
|
|
725
|
+
break;
|
|
726
|
+
}
|
|
727
|
+
buffer = `${buffer}${decoder.decode(value, { stream: true })}`.replaceAll("\r\n", "\n");
|
|
728
|
+
let frameEnd = buffer.indexOf("\n\n");
|
|
729
|
+
while (frameEnd >= 0) {
|
|
730
|
+
assertAnthropicSsePendingBufferWithinLimit(frameEnd);
|
|
731
|
+
const frame = buffer.slice(0, frameEnd);
|
|
732
|
+
buffer = buffer.slice(frameEnd + 2);
|
|
733
|
+
const data = frame.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
|
|
734
|
+
if (data && data !== "[DONE]") yield parseAnthropicSseEventData(data);
|
|
735
|
+
frameEnd = buffer.indexOf("\n\n");
|
|
736
|
+
}
|
|
737
|
+
assertAnthropicSsePendingBufferWithinLimit(buffer.length);
|
|
738
|
+
}
|
|
739
|
+
const tailBuffer = `${buffer}${decoder.decode()}`.replaceAll("\r\n", "\n");
|
|
740
|
+
assertAnthropicSsePendingBufferWithinLimit(tailBuffer.length);
|
|
741
|
+
const tail = tailBuffer.trim();
|
|
742
|
+
if (tail) {
|
|
743
|
+
const data = tail.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
|
|
744
|
+
if (data && data !== "[DONE]") yield parseAnthropicSseEventData(data);
|
|
745
|
+
}
|
|
746
|
+
} finally {
|
|
747
|
+
if (!completed) await reader.cancel(signal?.reason).catch(() => void 0);
|
|
748
|
+
reader.releaseLock();
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
function createAnthropicMessagesClient(params) {
|
|
752
|
+
const url = resolveAnthropicMessagesUrl(params.baseURL);
|
|
753
|
+
return { messages: { async *stream(body, options) {
|
|
754
|
+
const headers = mergeTransportHeaders({
|
|
755
|
+
"content-type": "application/json",
|
|
756
|
+
"anthropic-version": "2023-06-01",
|
|
757
|
+
...params.apiKey ? { "x-api-key": params.apiKey } : {},
|
|
758
|
+
...params.authToken ? { authorization: `Bearer ${params.authToken}` } : {}
|
|
759
|
+
}, params.defaultHeaders);
|
|
760
|
+
const response = await params.fetch(url, {
|
|
761
|
+
method: "POST",
|
|
762
|
+
headers,
|
|
763
|
+
body: JSON.stringify(body),
|
|
764
|
+
signal: options?.signal
|
|
765
|
+
});
|
|
766
|
+
if (!response.ok) {
|
|
767
|
+
const detail = await readAnthropicMessagesErrorBodySnippet(response);
|
|
768
|
+
throw new Error(formatAnthropicMessagesHttpError(response, detail));
|
|
769
|
+
}
|
|
770
|
+
if (!response.body) return;
|
|
771
|
+
yield* parseAnthropicSseBody(response.body, options?.signal);
|
|
772
|
+
} } };
|
|
773
|
+
}
|
|
774
|
+
function formatAnthropicMessagesHttpError(response, detail) {
|
|
775
|
+
const retryAfterSeconds = parseRetryAfterSeconds(response.headers);
|
|
776
|
+
const retryAfterSuffix = Number.isFinite(retryAfterSeconds) ? `; Retry-After: ${Math.ceil(retryAfterSeconds ?? 0)} seconds` : "";
|
|
777
|
+
return `HTTP ${response.status}: ${detail || "Anthropic Messages request failed"}${retryAfterSuffix}`;
|
|
778
|
+
}
|
|
779
|
+
async function readAnthropicMessagesErrorBodySnippet(response) {
|
|
780
|
+
try {
|
|
781
|
+
return await readResponseTextSnippet(response, {
|
|
782
|
+
maxBytes: ANTHROPIC_MESSAGES_ERROR_BODY_MAX_BYTES,
|
|
783
|
+
maxChars: ANTHROPIC_MESSAGES_ERROR_BODY_MAX_CHARS,
|
|
784
|
+
chunkTimeoutMs: ANTHROPIC_MESSAGES_ERROR_BODY_READ_IDLE_TIMEOUT_MS,
|
|
785
|
+
onIdleTimeout: ({ chunkTimeoutMs }) => /* @__PURE__ */ new Error(`Anthropic Messages error response stalled: no data received for ${chunkTimeoutMs}ms`)
|
|
786
|
+
}) ?? "";
|
|
787
|
+
} catch (error) {
|
|
788
|
+
if (error instanceof Error && error.message.startsWith("Anthropic Messages error response stalled:")) return error.message;
|
|
789
|
+
return "";
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
function createAnthropicTransportClient(params) {
|
|
793
|
+
const { model, context, apiKey, options } = params;
|
|
794
|
+
const needsInterleavedBeta = (options?.interleavedThinking ?? true) && !supportsAdaptiveThinking(model);
|
|
795
|
+
const fetch = isKimiAnthropicProvider(model.provider) && options?.thinkingEnabled === true ? buildGuardedModelFetch(model, void 0, { sanitizeSse: false }) : buildGuardedModelFetch(model);
|
|
796
|
+
if (model.provider === "github-copilot") {
|
|
797
|
+
const betaFeatures = needsInterleavedBeta ? ["interleaved-thinking-2025-05-14"] : [];
|
|
798
|
+
return {
|
|
799
|
+
client: createAnthropicMessagesClient({
|
|
800
|
+
apiKey: null,
|
|
801
|
+
authToken: apiKey,
|
|
802
|
+
baseURL: model.baseUrl,
|
|
803
|
+
defaultHeaders: mergeTransportHeaders({
|
|
804
|
+
accept: "application/json",
|
|
805
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
806
|
+
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
807
|
+
}, model.headers, getAiTransportHost().buildCopilotDynamicHeaders(context.messages), options?.headers),
|
|
808
|
+
fetch
|
|
809
|
+
}),
|
|
810
|
+
isOAuthToken: false
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
if (usesFoundryBearerAuth(resolveModelHeaderSentinels$1(model))) {
|
|
814
|
+
const betaFeatures = needsInterleavedBeta ? ["interleaved-thinking-2025-05-14"] : [];
|
|
815
|
+
return {
|
|
816
|
+
client: createAnthropicMessagesClient({
|
|
817
|
+
apiKey: null,
|
|
818
|
+
authToken: apiKey,
|
|
819
|
+
baseURL: model.baseUrl,
|
|
820
|
+
defaultHeaders: mergeTransportHeaders({
|
|
821
|
+
accept: "application/json",
|
|
822
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
823
|
+
...betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}
|
|
824
|
+
}, omitFoundryBearerCredentialHeaders(model.headers), options?.headers),
|
|
825
|
+
fetch
|
|
826
|
+
}),
|
|
827
|
+
isOAuthToken: false
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
const betaFeatures = ["fine-grained-tool-streaming-2025-05-14"];
|
|
831
|
+
if (needsInterleavedBeta) betaFeatures.push("interleaved-thinking-2025-05-14");
|
|
832
|
+
if (isAnthropicOAuthToken(apiKey)) {
|
|
833
|
+
const betaHeader = buildAnthropicBetaHeader(model, betaFeatures, { oauth: true });
|
|
834
|
+
return {
|
|
835
|
+
client: createAnthropicMessagesClient({
|
|
836
|
+
apiKey: null,
|
|
837
|
+
authToken: apiKey,
|
|
838
|
+
baseURL: model.baseUrl,
|
|
839
|
+
defaultHeaders: mergeTransportHeaders({
|
|
840
|
+
accept: "application/json",
|
|
841
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
842
|
+
...betaHeader ? { "anthropic-beta": betaHeader } : {},
|
|
843
|
+
"user-agent": `claude-cli/${CLAUDE_CODE_VERSION}`,
|
|
844
|
+
"x-app": "cli"
|
|
845
|
+
}, model.headers, options?.headers),
|
|
846
|
+
fetch
|
|
847
|
+
}),
|
|
848
|
+
isOAuthToken: true
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
if (useAnthropicServerSideFallback(model)) betaFeatures.push(ANTHROPIC_SERVER_SIDE_FALLBACK_BETA);
|
|
852
|
+
const betaHeader = buildAnthropicBetaHeader(model, betaFeatures, { oauth: false });
|
|
853
|
+
return {
|
|
854
|
+
client: createAnthropicMessagesClient({
|
|
855
|
+
apiKey,
|
|
856
|
+
baseURL: model.baseUrl,
|
|
857
|
+
defaultHeaders: mergeTransportHeaders({
|
|
858
|
+
accept: "application/json",
|
|
859
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
860
|
+
...betaHeader ? { "anthropic-beta": betaHeader } : {}
|
|
861
|
+
}, model.headers, options?.headers),
|
|
862
|
+
fetch
|
|
863
|
+
}),
|
|
864
|
+
isOAuthToken: false
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
async function buildAnthropicParams(model, context, isOAuthToken, options) {
|
|
868
|
+
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
869
|
+
const replayThinkingEnabled = mandatoryAdaptiveThinking || options?.thinkingEnabled === true;
|
|
870
|
+
const maxTokens = resolveAnthropicMessagesMaxTokens({
|
|
871
|
+
modelContextWindow: model.contextWindow,
|
|
872
|
+
modelMaxTokens: model.maxTokens,
|
|
873
|
+
requestedMaxTokens: options?.maxTokens
|
|
874
|
+
});
|
|
875
|
+
if (maxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
|
|
876
|
+
const payloadPolicy = resolveAnthropicPayloadPolicy({
|
|
877
|
+
provider: model.provider,
|
|
878
|
+
api: model.api,
|
|
879
|
+
baseUrl: model.baseUrl,
|
|
880
|
+
cacheRetention: options?.cacheRetention,
|
|
881
|
+
enableCacheControl: true
|
|
882
|
+
});
|
|
883
|
+
const cacheBreakpointOptOutMessageIndexes = /* @__PURE__ */ new Set();
|
|
884
|
+
const messages = await convertAnthropicMessages(context.messages, model, isOAuthToken, {
|
|
885
|
+
allowReasoningContentReplay: supportsReasoningContentReplay(model),
|
|
886
|
+
cacheBreakpointOptOutMessageIndexes,
|
|
887
|
+
replayThinkingEnabled
|
|
888
|
+
});
|
|
889
|
+
const params = {
|
|
890
|
+
model: resolveAnthropicRequestModelId(model),
|
|
891
|
+
messages: ensureNonEmptyAnthropicMessages(messages),
|
|
892
|
+
max_tokens: maxTokens,
|
|
893
|
+
stream: true
|
|
894
|
+
};
|
|
895
|
+
if (!isOAuthToken && useAnthropicServerSideFallback(model)) params.fallbacks = ANTHROPIC_SERVER_SIDE_FALLBACKS;
|
|
896
|
+
if (isOAuthToken) params.system = [
|
|
897
|
+
{
|
|
898
|
+
type: "text",
|
|
899
|
+
text: CLAUDE_CODE_BILLING_SYSTEM_BLOCK
|
|
900
|
+
},
|
|
901
|
+
{
|
|
902
|
+
type: "text",
|
|
903
|
+
text: "You are Claude Code, Anthropic's official CLI for Claude."
|
|
904
|
+
},
|
|
905
|
+
...context.systemPrompt ? [{
|
|
906
|
+
type: "text",
|
|
907
|
+
text: sanitizeTransportPayloadText(context.systemPrompt)
|
|
908
|
+
}] : []
|
|
909
|
+
];
|
|
910
|
+
else if (context.systemPrompt) params.system = [{
|
|
911
|
+
type: "text",
|
|
912
|
+
text: sanitizeTransportPayloadText(context.systemPrompt)
|
|
913
|
+
}];
|
|
914
|
+
if (options?.temperature !== void 0 && !options.thinkingEnabled && !supportsNativeXhighEffort(model)) params.temperature = options.temperature;
|
|
915
|
+
if (options?.stop !== void 0 && options.stop.length > 0) params.stop_sequences = options.stop;
|
|
916
|
+
let toolProjection;
|
|
917
|
+
if (context.tools) {
|
|
918
|
+
const convertedTools = convertAnthropicTools(context.tools, isOAuthToken);
|
|
919
|
+
toolProjection = convertedTools.projection;
|
|
920
|
+
if (convertedTools.tools.length > 0) params.tools = convertedTools.tools;
|
|
921
|
+
}
|
|
922
|
+
if (mandatoryAdaptiveThinking || model.reasoning || supportsAdaptiveThinking(model)) {
|
|
923
|
+
if (mandatoryAdaptiveThinking || options?.thinkingEnabled) if (supportsAdaptiveThinking(model)) {
|
|
924
|
+
params.thinking = {
|
|
925
|
+
type: "adaptive",
|
|
926
|
+
display: options?.thinkingDisplay ?? "summarized"
|
|
927
|
+
};
|
|
928
|
+
const effort = options?.effort ?? (mandatoryAdaptiveThinking ? "high" : void 0);
|
|
929
|
+
if (effort) params.output_config = { effort };
|
|
930
|
+
} else params.thinking = {
|
|
931
|
+
type: "enabled",
|
|
932
|
+
budget_tokens: options?.thinkingBudgetTokens ?? 1024
|
|
933
|
+
};
|
|
934
|
+
else if (options?.thinkingEnabled === false) params.thinking = { type: "disabled" };
|
|
935
|
+
}
|
|
936
|
+
if (options?.metadata && typeof options.metadata.user_id === "string") params.metadata = { user_id: options.metadata.user_id };
|
|
937
|
+
if (options?.toolChoice) {
|
|
938
|
+
const normalizedToolChoice = normalizeAnthropicToolChoice(replayThinkingEnabled, options.toolChoice);
|
|
939
|
+
const projectedToolChoice = toolProjection ? reconcileAnthropicToolChoice(normalizedToolChoice, toolProjection) : normalizedToolChoice;
|
|
940
|
+
if (projectedToolChoice) params.tool_choice = projectedToolChoice;
|
|
941
|
+
}
|
|
942
|
+
applyAnthropicPayloadPolicyToParams(params, payloadPolicy, cacheBreakpointOptOutMessageIndexes);
|
|
943
|
+
return {
|
|
944
|
+
params,
|
|
945
|
+
toolProjection
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
function resolveAnthropicTransportOptions(model, options, apiKey) {
|
|
949
|
+
const baseMaxTokens = resolveAnthropicMessagesMaxTokens({
|
|
950
|
+
modelContextWindow: model.contextWindow,
|
|
951
|
+
modelMaxTokens: model.maxTokens,
|
|
952
|
+
requestedMaxTokens: options?.maxTokens,
|
|
953
|
+
useModelDefault: resolveClaudeSonnet5ModelIdentity(model) !== void 0 || resolveClaudeOpus5ModelIdentity(model) !== void 0
|
|
954
|
+
});
|
|
955
|
+
if (baseMaxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
|
|
956
|
+
const reasoningModelMaxTokens = resolvePositiveAnthropicTokenLimit(model.maxTokens) ?? baseMaxTokens;
|
|
957
|
+
const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
|
|
958
|
+
const reasoning = options?.reasoning === "off" && mandatoryAdaptiveThinking ? "low" : options?.reasoning;
|
|
959
|
+
const resolved = {
|
|
960
|
+
temperature: options?.temperature,
|
|
961
|
+
stop: options?.stop,
|
|
962
|
+
maxTokens: baseMaxTokens,
|
|
963
|
+
signal: options?.signal,
|
|
964
|
+
apiKey,
|
|
965
|
+
cacheRetention: options?.cacheRetention,
|
|
966
|
+
sessionId: options?.sessionId,
|
|
967
|
+
headers: options?.headers,
|
|
968
|
+
onPayload: options?.onPayload,
|
|
969
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
970
|
+
metadata: options?.metadata,
|
|
971
|
+
interleavedThinking: options?.interleavedThinking,
|
|
972
|
+
toolChoice: options?.toolChoice,
|
|
973
|
+
thinkingBudgets: options?.thinkingBudgets,
|
|
974
|
+
reasoning
|
|
975
|
+
};
|
|
976
|
+
if (reasoning === "off") {
|
|
977
|
+
resolved.thinkingEnabled = false;
|
|
978
|
+
return resolved;
|
|
979
|
+
}
|
|
980
|
+
if (!reasoning) {
|
|
981
|
+
resolved.thinkingEnabled = defaultsClaudeAdaptiveThinking(model);
|
|
982
|
+
if (resolved.thinkingEnabled) resolved.effort = "high";
|
|
983
|
+
return resolved;
|
|
984
|
+
}
|
|
985
|
+
if (supportsAdaptiveThinking(model)) {
|
|
986
|
+
resolved.thinkingEnabled = true;
|
|
987
|
+
resolved.effort = mapThinkingLevelToEffort(reasoning, model);
|
|
988
|
+
return resolved;
|
|
989
|
+
}
|
|
990
|
+
const adjusted = adjustMaxTokensForThinking({
|
|
991
|
+
baseMaxTokens,
|
|
992
|
+
modelMaxTokens: reasoningModelMaxTokens,
|
|
993
|
+
reasoningLevel: reasoning,
|
|
994
|
+
customBudgets: options?.thinkingBudgets
|
|
995
|
+
});
|
|
996
|
+
const thinkingEnabled = adjusted.thinkingBudget >= 1024;
|
|
997
|
+
resolved.maxTokens = adjusted.maxTokens;
|
|
998
|
+
resolved.thinkingEnabled = thinkingEnabled;
|
|
999
|
+
resolved.thinkingBudgetTokens = thinkingEnabled ? adjusted.thinkingBudget : void 0;
|
|
1000
|
+
return resolved;
|
|
1001
|
+
}
|
|
1002
|
+
/** Create the stream function used by Anthropic Messages transport models. */
|
|
1003
|
+
function createAnthropicMessagesTransportStreamFn() {
|
|
1004
|
+
return (rawModel, context, rawOptions) => {
|
|
1005
|
+
const model = withEffectiveAnthropicBaseUrl(rawModel);
|
|
1006
|
+
const options = rawOptions;
|
|
1007
|
+
const { eventStream, stream } = createWritableTransportEventStream();
|
|
1008
|
+
(async () => {
|
|
1009
|
+
const output = {
|
|
1010
|
+
role: "assistant",
|
|
1011
|
+
content: [],
|
|
1012
|
+
api: "anthropic-messages",
|
|
1013
|
+
provider: model.provider,
|
|
1014
|
+
model: model.id,
|
|
1015
|
+
usage: createEmptyTransportUsage(),
|
|
1016
|
+
stopReason: "stop",
|
|
1017
|
+
timestamp: Date.now()
|
|
1018
|
+
};
|
|
1019
|
+
const refusalBuffer = usesClaudeStreamingRefusalContract(model) ? createDeferredEventBuffer(stream, () => notifyLlmRequestActivity(options?.signal)) : void 0;
|
|
1020
|
+
const eventSink = refusalBuffer ?? stream;
|
|
1021
|
+
let costModel = model;
|
|
1022
|
+
let messageStartPromptUsage;
|
|
1023
|
+
try {
|
|
1024
|
+
const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
|
|
1025
|
+
if (!apiKey) throw new Error(`No API key for provider: ${model.provider}`);
|
|
1026
|
+
const transportOptions = resolveAnthropicTransportOptions(model, options, apiKey);
|
|
1027
|
+
const requestContext = prepareClaudeNoPrefillRequestContext(model, context);
|
|
1028
|
+
const { client, isOAuthToken } = createAnthropicTransportClient({
|
|
1029
|
+
model,
|
|
1030
|
+
context: requestContext,
|
|
1031
|
+
apiKey,
|
|
1032
|
+
options: transportOptions
|
|
1033
|
+
});
|
|
1034
|
+
const builtParams = await buildAnthropicParams(model, requestContext, isOAuthToken, transportOptions);
|
|
1035
|
+
let params = builtParams.params;
|
|
1036
|
+
const toolProjection = builtParams.toolProjection;
|
|
1037
|
+
const nextParams = await transportOptions.onPayload?.(params, model);
|
|
1038
|
+
if (nextParams !== void 0) params = nextParams;
|
|
1039
|
+
applyClaudeRequestContract(params, model);
|
|
1040
|
+
const anthropicStream = client.messages.stream({
|
|
1041
|
+
...params,
|
|
1042
|
+
stream: true
|
|
1043
|
+
}, transportOptions.signal ? { signal: transportOptions.signal } : void 0);
|
|
1044
|
+
const blocks = output.content;
|
|
1045
|
+
const blockIndexes = /* @__PURE__ */ new Map();
|
|
1046
|
+
const pendingThinkingSignatures = /* @__PURE__ */ new Map();
|
|
1047
|
+
const allowReasoningContentReplay = supportsReasoningContentReplay(model);
|
|
1048
|
+
const reasoningContentThinkingBlocks = /* @__PURE__ */ new Map();
|
|
1049
|
+
const reasoningContentTextBlocks = /* @__PURE__ */ new Map();
|
|
1050
|
+
let sawMessageStop = false;
|
|
1051
|
+
const pendingTextEnds = [];
|
|
1052
|
+
const flushPendingTextEnds = () => {
|
|
1053
|
+
for (const event of pendingTextEnds) eventSink.push(event);
|
|
1054
|
+
pendingTextEnds.length = 0;
|
|
1055
|
+
};
|
|
1056
|
+
const eventIndexKey = (eventIndex) => typeof eventIndex === "number" ? eventIndex : -1;
|
|
1057
|
+
const appendReasoningContentThinkingDelta = (eventIndex, rawText) => {
|
|
1058
|
+
if (typeof rawText !== "string") return false;
|
|
1059
|
+
const text = sanitizeTransportPayloadText(rawText);
|
|
1060
|
+
if (text.length === 0) return false;
|
|
1061
|
+
const key = eventIndexKey(eventIndex);
|
|
1062
|
+
let contentIndex = reasoningContentThinkingBlocks.get(key);
|
|
1063
|
+
let block = contentIndex === void 0 ? void 0 : output.content[contentIndex];
|
|
1064
|
+
if (!block || block.type !== "thinking") {
|
|
1065
|
+
block = {
|
|
1066
|
+
type: "thinking",
|
|
1067
|
+
thinking: "",
|
|
1068
|
+
thinkingSignature: "reasoning_content"
|
|
1069
|
+
};
|
|
1070
|
+
output.content.push(block);
|
|
1071
|
+
contentIndex = output.content.length - 1;
|
|
1072
|
+
reasoningContentThinkingBlocks.set(key, contentIndex);
|
|
1073
|
+
eventSink.push({
|
|
1074
|
+
type: "thinking_start",
|
|
1075
|
+
contentIndex,
|
|
1076
|
+
partial: output
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
block.thinking += text;
|
|
1080
|
+
block.thinkingSignature = "reasoning_content";
|
|
1081
|
+
eventSink.push({
|
|
1082
|
+
type: "thinking_delta",
|
|
1083
|
+
contentIndex,
|
|
1084
|
+
delta: text,
|
|
1085
|
+
partial: output
|
|
1086
|
+
});
|
|
1087
|
+
return true;
|
|
1088
|
+
};
|
|
1089
|
+
const appendReasoningContentTextDelta = (eventIndex, rawText) => {
|
|
1090
|
+
if (typeof rawText !== "string") return false;
|
|
1091
|
+
const text = sanitizeTransportPayloadText(rawText);
|
|
1092
|
+
if (text.length === 0) return false;
|
|
1093
|
+
const key = eventIndexKey(eventIndex);
|
|
1094
|
+
let contentIndex = reasoningContentTextBlocks.get(key);
|
|
1095
|
+
let block = contentIndex === void 0 ? void 0 : output.content[contentIndex];
|
|
1096
|
+
if (!block || block.type !== "text") {
|
|
1097
|
+
block = {
|
|
1098
|
+
type: "text",
|
|
1099
|
+
text: ""
|
|
1100
|
+
};
|
|
1101
|
+
output.content.push(block);
|
|
1102
|
+
contentIndex = output.content.length - 1;
|
|
1103
|
+
reasoningContentTextBlocks.set(key, contentIndex);
|
|
1104
|
+
eventSink.push({
|
|
1105
|
+
type: "text_start",
|
|
1106
|
+
contentIndex,
|
|
1107
|
+
partial: output
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
block.text += text;
|
|
1111
|
+
eventSink.push({
|
|
1112
|
+
type: "text_delta",
|
|
1113
|
+
contentIndex,
|
|
1114
|
+
delta: text,
|
|
1115
|
+
partial: output
|
|
1116
|
+
});
|
|
1117
|
+
return true;
|
|
1118
|
+
};
|
|
1119
|
+
const finishReasoningContentSidecars = (eventIndex) => {
|
|
1120
|
+
const key = eventIndexKey(eventIndex);
|
|
1121
|
+
const thinkingContentIndex = reasoningContentThinkingBlocks.get(key);
|
|
1122
|
+
if (thinkingContentIndex !== void 0) {
|
|
1123
|
+
reasoningContentThinkingBlocks.delete(key);
|
|
1124
|
+
const block = output.content[thinkingContentIndex];
|
|
1125
|
+
if (block?.type === "thinking") eventSink.push({
|
|
1126
|
+
type: "thinking_end",
|
|
1127
|
+
contentIndex: thinkingContentIndex,
|
|
1128
|
+
content: block.thinking,
|
|
1129
|
+
partial: output
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
const textContentIndex = reasoningContentTextBlocks.get(key);
|
|
1133
|
+
if (textContentIndex === void 0) return;
|
|
1134
|
+
reasoningContentTextBlocks.delete(key);
|
|
1135
|
+
const block = output.content[textContentIndex];
|
|
1136
|
+
if (block?.type === "text") eventSink.push({
|
|
1137
|
+
type: "text_end",
|
|
1138
|
+
contentIndex: textContentIndex,
|
|
1139
|
+
content: block.text,
|
|
1140
|
+
partial: output
|
|
1141
|
+
});
|
|
1142
|
+
};
|
|
1143
|
+
for await (const event of anthropicStream) {
|
|
1144
|
+
if (event.type === "error") {
|
|
1145
|
+
const error = event.error;
|
|
1146
|
+
throw new Error(error?.message || "Anthropic Messages stream failed");
|
|
1147
|
+
}
|
|
1148
|
+
if (event.type === "message_start") {
|
|
1149
|
+
const message = event.message;
|
|
1150
|
+
const usage = message?.usage ?? {};
|
|
1151
|
+
output.responseId = typeof message?.id === "string" ? message.id : void 0;
|
|
1152
|
+
output.responseModel = typeof message?.model === "string" ? message.model : void 0;
|
|
1153
|
+
const promptUsage = readAnthropicPromptUsageSnapshot(usage);
|
|
1154
|
+
const messageStartPromptTokens = promptUsage ? promptUsage.input + promptUsage.cacheRead + promptUsage.cacheWrite : 0;
|
|
1155
|
+
messageStartPromptUsage = messageStartPromptTokens > 0 ? promptUsage : void 0;
|
|
1156
|
+
const inputTokens = readAnthropicUsageTokenCount(usage.input_tokens);
|
|
1157
|
+
if (inputTokens !== void 0) output.usage.input = inputTokens;
|
|
1158
|
+
const outputTokens = readAnthropicUsageTokenCount(usage.output_tokens);
|
|
1159
|
+
if (outputTokens !== void 0) output.usage.output = outputTokens;
|
|
1160
|
+
const cacheReadTokens = usage.cache_read_input_tokens == null ? 0 : readAnthropicUsageTokenCount(usage.cache_read_input_tokens);
|
|
1161
|
+
if (cacheReadTokens !== void 0) output.usage.cacheRead = cacheReadTokens;
|
|
1162
|
+
const cacheWriteTokens = usage.cache_creation_input_tokens == null ? 0 : readAnthropicUsageTokenCount(usage.cache_creation_input_tokens);
|
|
1163
|
+
if (cacheWriteTokens !== void 0) output.usage.cacheWrite = cacheWriteTokens;
|
|
1164
|
+
output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
1165
|
+
if (messageStartPromptUsage && outputTokens !== void 0) output.usage.contextUsage = {
|
|
1166
|
+
state: "available",
|
|
1167
|
+
promptTokens: messageStartPromptTokens,
|
|
1168
|
+
totalTokens: messageStartPromptTokens + output.usage.output
|
|
1169
|
+
};
|
|
1170
|
+
calculateCost(costModel, output.usage);
|
|
1171
|
+
eventSink.push({
|
|
1172
|
+
type: "start",
|
|
1173
|
+
partial: output
|
|
1174
|
+
});
|
|
1175
|
+
continue;
|
|
1176
|
+
}
|
|
1177
|
+
if (event.type === "message_stop") {
|
|
1178
|
+
sawMessageStop = true;
|
|
1179
|
+
continue;
|
|
1180
|
+
}
|
|
1181
|
+
if (event.type === "content_block_start") {
|
|
1182
|
+
const contentBlock = event.content_block;
|
|
1183
|
+
const index = typeof event.index === "number" ? event.index : -1;
|
|
1184
|
+
const fallbackBoundary = refusalBuffer ? readAnthropicFallbackBoundary(contentBlock) : null;
|
|
1185
|
+
if (fallbackBoundary) {
|
|
1186
|
+
refusalBuffer?.discard();
|
|
1187
|
+
pendingTextEnds.length = 0;
|
|
1188
|
+
blockIndexes.clear();
|
|
1189
|
+
pendingThinkingSignatures.clear();
|
|
1190
|
+
applyAnthropicFallbackBoundary({
|
|
1191
|
+
output,
|
|
1192
|
+
boundary: fallbackBoundary,
|
|
1193
|
+
provider: model.provider
|
|
1194
|
+
});
|
|
1195
|
+
costModel = {
|
|
1196
|
+
...model,
|
|
1197
|
+
cost: resolveAnthropicFallbackServingModelCost({
|
|
1198
|
+
requestedModelId: model.id,
|
|
1199
|
+
servingModelId: fallbackBoundary.toModel,
|
|
1200
|
+
requestedCost: model.cost
|
|
1201
|
+
})
|
|
1202
|
+
};
|
|
1203
|
+
calculateCost(costModel, output.usage);
|
|
1204
|
+
eventSink.push({
|
|
1205
|
+
type: "start",
|
|
1206
|
+
partial: output
|
|
1207
|
+
});
|
|
1208
|
+
for (const [i, block] of output.content.entries()) {
|
|
1209
|
+
if (block.type !== "text") continue;
|
|
1210
|
+
delete block.index;
|
|
1211
|
+
eventSink.push({
|
|
1212
|
+
type: "text_start",
|
|
1213
|
+
contentIndex: i,
|
|
1214
|
+
partial: output
|
|
1215
|
+
});
|
|
1216
|
+
if (block.text) eventSink.push({
|
|
1217
|
+
type: "text_delta",
|
|
1218
|
+
contentIndex: i,
|
|
1219
|
+
delta: block.text,
|
|
1220
|
+
partial: output
|
|
1221
|
+
});
|
|
1222
|
+
pendingTextEnds.push({
|
|
1223
|
+
type: "text_end",
|
|
1224
|
+
contentIndex: i,
|
|
1225
|
+
content: block.text,
|
|
1226
|
+
partial: output
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
pendingThinkingSignatures.delete(index);
|
|
1232
|
+
if (contentBlock?.type === "text") {
|
|
1233
|
+
const text = typeof contentBlock.text === "string" ? sanitizeTransportPayloadText(contentBlock.text) : "";
|
|
1234
|
+
const block = {
|
|
1235
|
+
type: "text",
|
|
1236
|
+
text,
|
|
1237
|
+
index
|
|
1238
|
+
};
|
|
1239
|
+
output.content.push(block);
|
|
1240
|
+
const contentIndex = output.content.length - 1;
|
|
1241
|
+
blockIndexes.set(index, contentIndex);
|
|
1242
|
+
eventSink.push({
|
|
1243
|
+
type: "text_start",
|
|
1244
|
+
contentIndex,
|
|
1245
|
+
partial: output
|
|
1246
|
+
});
|
|
1247
|
+
if (text.length > 0) eventSink.push({
|
|
1248
|
+
type: "text_delta",
|
|
1249
|
+
contentIndex,
|
|
1250
|
+
delta: text,
|
|
1251
|
+
partial: output
|
|
1252
|
+
});
|
|
1253
|
+
continue;
|
|
1254
|
+
}
|
|
1255
|
+
if (contentBlock?.type === "thinking") {
|
|
1256
|
+
const thinking = typeof contentBlock.thinking === "string" ? contentBlock.thinking : "";
|
|
1257
|
+
const block = {
|
|
1258
|
+
type: "thinking",
|
|
1259
|
+
thinking,
|
|
1260
|
+
thinkingSignature: typeof contentBlock.signature === "string" ? contentBlock.signature : "",
|
|
1261
|
+
index
|
|
1262
|
+
};
|
|
1263
|
+
output.content.push(block);
|
|
1264
|
+
const contentIndex = output.content.length - 1;
|
|
1265
|
+
blockIndexes.set(index, contentIndex);
|
|
1266
|
+
eventSink.push({
|
|
1267
|
+
type: "thinking_start",
|
|
1268
|
+
contentIndex,
|
|
1269
|
+
partial: output
|
|
1270
|
+
});
|
|
1271
|
+
if (thinking.length > 0) eventSink.push({
|
|
1272
|
+
type: "thinking_delta",
|
|
1273
|
+
contentIndex,
|
|
1274
|
+
delta: thinking,
|
|
1275
|
+
partial: output
|
|
1276
|
+
});
|
|
1277
|
+
continue;
|
|
1278
|
+
}
|
|
1279
|
+
if (contentBlock?.type === "redacted_thinking") {
|
|
1280
|
+
const block = {
|
|
1281
|
+
type: "thinking",
|
|
1282
|
+
thinking: "[Reasoning redacted]",
|
|
1283
|
+
thinkingSignature: typeof contentBlock.data === "string" ? contentBlock.data : "",
|
|
1284
|
+
redacted: true,
|
|
1285
|
+
index
|
|
1286
|
+
};
|
|
1287
|
+
output.content.push(block);
|
|
1288
|
+
blockIndexes.set(index, output.content.length - 1);
|
|
1289
|
+
eventSink.push({
|
|
1290
|
+
type: "thinking_start",
|
|
1291
|
+
contentIndex: output.content.length - 1,
|
|
1292
|
+
partial: output
|
|
1293
|
+
});
|
|
1294
|
+
continue;
|
|
1295
|
+
}
|
|
1296
|
+
if (contentBlock?.type === "tool_use") {
|
|
1297
|
+
tagPendingCommentaryText(output.content);
|
|
1298
|
+
flushPendingTextEnds();
|
|
1299
|
+
const block = {
|
|
1300
|
+
type: "toolCall",
|
|
1301
|
+
id: typeof contentBlock.id === "string" ? contentBlock.id : "",
|
|
1302
|
+
name: typeof contentBlock.name === "string" ? isOAuthToken ? resolveOriginalAnthropicToolName(contentBlock.name, toolProjection) : contentBlock.name : "",
|
|
1303
|
+
arguments: contentBlock.input && typeof contentBlock.input === "object" ? contentBlock.input : {},
|
|
1304
|
+
partialJson: "",
|
|
1305
|
+
index
|
|
1306
|
+
};
|
|
1307
|
+
output.content.push(block);
|
|
1308
|
+
blockIndexes.set(index, output.content.length - 1);
|
|
1309
|
+
eventSink.push({
|
|
1310
|
+
type: "toolcall_start",
|
|
1311
|
+
contentIndex: output.content.length - 1,
|
|
1312
|
+
partial: output
|
|
1313
|
+
});
|
|
1314
|
+
}
|
|
1315
|
+
continue;
|
|
1316
|
+
}
|
|
1317
|
+
if (event.type === "content_block_delta") {
|
|
1318
|
+
const delta = event.delta;
|
|
1319
|
+
const eventIndex = typeof event.index === "number" ? event.index : void 0;
|
|
1320
|
+
let index = eventIndex === void 0 ? void 0 : blockIndexes.get(eventIndex);
|
|
1321
|
+
let block = index === void 0 ? void 0 : blocks[index];
|
|
1322
|
+
if (allowReasoningContentReplay) {
|
|
1323
|
+
const appendedThinking = appendReasoningContentThinkingDelta(event.index, delta?.reasoning_content);
|
|
1324
|
+
const hasNativeAnthropicDelta = delta?.type === "text_delta" && typeof delta.text === "string" || delta?.type === "thinking_delta" && typeof delta.thinking === "string" || delta?.type === "input_json_delta" && typeof delta.partial_json === "string" || delta?.type === "signature_delta" && typeof delta.signature === "string";
|
|
1325
|
+
let appendedContent = false;
|
|
1326
|
+
if (!hasNativeAnthropicDelta && typeof delta?.content === "string" && delta.content.length > 0) {
|
|
1327
|
+
const text = sanitizeTransportPayloadText(delta.content);
|
|
1328
|
+
if (text.length > 0) if (block?.type === "text") {
|
|
1329
|
+
block.text += text;
|
|
1330
|
+
eventSink.push({
|
|
1331
|
+
type: "text_delta",
|
|
1332
|
+
contentIndex: index,
|
|
1333
|
+
delta: text,
|
|
1334
|
+
partial: output
|
|
1335
|
+
});
|
|
1336
|
+
appendedContent = true;
|
|
1337
|
+
} else appendedContent = appendReasoningContentTextDelta(event.index, text);
|
|
1338
|
+
}
|
|
1339
|
+
if ((appendedThinking || appendedContent) && !hasNativeAnthropicDelta) continue;
|
|
1340
|
+
}
|
|
1341
|
+
if (!block && delta?.type === "text_delta" && typeof delta.text === "string") {
|
|
1342
|
+
block = {
|
|
1343
|
+
type: "text",
|
|
1344
|
+
text: "",
|
|
1345
|
+
index: typeof event.index === "number" ? event.index : blocks.length
|
|
1346
|
+
};
|
|
1347
|
+
output.content.push(block);
|
|
1348
|
+
index = output.content.length - 1;
|
|
1349
|
+
if (typeof event.index === "number") blockIndexes.set(event.index, index);
|
|
1350
|
+
eventSink.push({
|
|
1351
|
+
type: "text_start",
|
|
1352
|
+
contentIndex: index,
|
|
1353
|
+
partial: output
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
if (block?.type === "text" && delta?.type === "text_delta" && typeof delta.text === "string") {
|
|
1357
|
+
block.text += delta.text;
|
|
1358
|
+
eventSink.push({
|
|
1359
|
+
type: "text_delta",
|
|
1360
|
+
contentIndex: index,
|
|
1361
|
+
delta: delta.text,
|
|
1362
|
+
partial: output
|
|
1363
|
+
});
|
|
1364
|
+
continue;
|
|
1365
|
+
}
|
|
1366
|
+
if (block?.type === "thinking" && delta?.type === "thinking_delta" && typeof delta.thinking === "string") {
|
|
1367
|
+
block.thinking += delta.thinking;
|
|
1368
|
+
eventSink.push({
|
|
1369
|
+
type: "thinking_delta",
|
|
1370
|
+
contentIndex: index,
|
|
1371
|
+
delta: delta.thinking,
|
|
1372
|
+
partial: output
|
|
1373
|
+
});
|
|
1374
|
+
continue;
|
|
1375
|
+
}
|
|
1376
|
+
if (block?.type === "toolCall" && delta?.type === "input_json_delta" && typeof delta.partial_json === "string") {
|
|
1377
|
+
const partialJson = `${block.partialJson ?? ""}${delta.partial_json}`;
|
|
1378
|
+
block.partialJson = partialJson;
|
|
1379
|
+
block.arguments = parseAnthropicToolCallArguments(partialJson);
|
|
1380
|
+
eventSink.push({
|
|
1381
|
+
type: "toolcall_delta",
|
|
1382
|
+
contentIndex: index,
|
|
1383
|
+
delta: delta.partial_json,
|
|
1384
|
+
partial: output
|
|
1385
|
+
});
|
|
1386
|
+
continue;
|
|
1387
|
+
}
|
|
1388
|
+
if (block?.type === "thinking" && delta?.type === "signature_delta" && typeof delta.signature === "string") {
|
|
1389
|
+
const signatureIndex = eventIndexKey(event.index);
|
|
1390
|
+
const pendingSignature = pendingThinkingSignatures.get(signatureIndex);
|
|
1391
|
+
if (pendingSignature === void 0) {
|
|
1392
|
+
block.thinkingSignature = "";
|
|
1393
|
+
pendingThinkingSignatures.set(signatureIndex, delta.signature);
|
|
1394
|
+
} else pendingThinkingSignatures.set(signatureIndex, pendingSignature + delta.signature);
|
|
1395
|
+
}
|
|
1396
|
+
continue;
|
|
1397
|
+
}
|
|
1398
|
+
if (event.type === "content_block_stop") {
|
|
1399
|
+
const eventIndex = typeof event.index === "number" ? event.index : void 0;
|
|
1400
|
+
const pendingSignature = eventIndex === void 0 ? void 0 : pendingThinkingSignatures.get(eventIndex);
|
|
1401
|
+
if (eventIndex !== void 0) pendingThinkingSignatures.delete(eventIndex);
|
|
1402
|
+
const index = eventIndex === void 0 ? void 0 : blockIndexes.get(eventIndex);
|
|
1403
|
+
const block = index === void 0 ? void 0 : blocks[index];
|
|
1404
|
+
if (eventIndex === void 0 || index === void 0 || !block) {
|
|
1405
|
+
finishReasoningContentSidecars(event.index);
|
|
1406
|
+
continue;
|
|
1407
|
+
}
|
|
1408
|
+
blockIndexes.delete(eventIndex);
|
|
1409
|
+
delete block.index;
|
|
1410
|
+
if (block.type === "text") {
|
|
1411
|
+
pendingTextEnds.push({
|
|
1412
|
+
type: "text_end",
|
|
1413
|
+
contentIndex: index,
|
|
1414
|
+
content: block.text,
|
|
1415
|
+
partial: output
|
|
1416
|
+
});
|
|
1417
|
+
finishReasoningContentSidecars(event.index);
|
|
1418
|
+
continue;
|
|
1419
|
+
}
|
|
1420
|
+
if (block.type === "thinking") {
|
|
1421
|
+
if (pendingSignature !== void 0) block.thinkingSignature = pendingSignature;
|
|
1422
|
+
eventSink.push({
|
|
1423
|
+
type: "thinking_end",
|
|
1424
|
+
contentIndex: index,
|
|
1425
|
+
content: block.thinking,
|
|
1426
|
+
partial: output
|
|
1427
|
+
});
|
|
1428
|
+
finishReasoningContentSidecars(event.index);
|
|
1429
|
+
continue;
|
|
1430
|
+
}
|
|
1431
|
+
if (block.type === "toolCall") {
|
|
1432
|
+
delete block.partialJson;
|
|
1433
|
+
eventSink.push({
|
|
1434
|
+
type: "toolcall_end",
|
|
1435
|
+
contentIndex: index,
|
|
1436
|
+
toolCall: block,
|
|
1437
|
+
partial: output
|
|
1438
|
+
});
|
|
1439
|
+
finishReasoningContentSidecars(event.index);
|
|
1440
|
+
}
|
|
1441
|
+
continue;
|
|
1442
|
+
}
|
|
1443
|
+
if (event.type === "message_delta") {
|
|
1444
|
+
const delta = event.delta;
|
|
1445
|
+
const usage = event.usage;
|
|
1446
|
+
if (delta?.stop_reason) if (delta.stop_reason === "refusal") applyAnthropicRefusal(output, delta.stop_details, model.provider);
|
|
1447
|
+
else output.stopReason = mapStopReason(delta.stop_reason);
|
|
1448
|
+
const inputTokens = readAnthropicUsageTokenCount(usage?.input_tokens);
|
|
1449
|
+
if (inputTokens !== void 0) output.usage.input = inputTokens;
|
|
1450
|
+
const outputTokens = readAnthropicUsageTokenCount(usage?.output_tokens);
|
|
1451
|
+
if (outputTokens !== void 0) output.usage.output = outputTokens;
|
|
1452
|
+
const cacheReadTokens = readAnthropicUsageTokenCount(usage?.cache_read_input_tokens);
|
|
1453
|
+
if (cacheReadTokens !== void 0) output.usage.cacheRead = cacheReadTokens;
|
|
1454
|
+
const cacheWriteTokens = readAnthropicUsageTokenCount(usage?.cache_creation_input_tokens);
|
|
1455
|
+
if (cacheWriteTokens !== void 0) output.usage.cacheWrite = cacheWriteTokens;
|
|
1456
|
+
output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
1457
|
+
const iterationUsage = readLastAnthropicIterationUsage(usage ?? {});
|
|
1458
|
+
if (iterationUsage.state === "valid") output.usage.contextUsage = {
|
|
1459
|
+
state: "available",
|
|
1460
|
+
promptTokens: iterationUsage.usage.contextPromptTokens,
|
|
1461
|
+
totalTokens: iterationUsage.usage.totalTokens
|
|
1462
|
+
};
|
|
1463
|
+
else if (iterationUsage.state === "invalid") output.usage.contextUsage = { state: "unavailable" };
|
|
1464
|
+
else if (outputTokens !== void 0 && (messageStartPromptUsage !== void 0 || inputTokens !== void 0 && cacheReadTokens !== void 0 && cacheWriteTokens !== void 0)) {
|
|
1465
|
+
const promptTokens = output.usage.input + output.usage.cacheRead + output.usage.cacheWrite;
|
|
1466
|
+
output.usage.contextUsage = {
|
|
1467
|
+
state: "available",
|
|
1468
|
+
promptTokens,
|
|
1469
|
+
totalTokens: promptTokens + output.usage.output
|
|
1470
|
+
};
|
|
1471
|
+
} else output.usage.contextUsage = { state: "unavailable" };
|
|
1472
|
+
calculateCost(costModel, output.usage);
|
|
1473
|
+
if (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall")) tagPendingCommentaryText(output.content);
|
|
1474
|
+
flushPendingTextEnds();
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
if (refusalBuffer && !sawMessageStop) throw new Error("Anthropic stream ended before message_stop");
|
|
1478
|
+
if (transportOptions.signal?.aborted) throw transportAbortError(transportOptions.signal);
|
|
1479
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error(output.errorMessage ?? "An unknown error occurred");
|
|
1480
|
+
refusalBuffer?.flush();
|
|
1481
|
+
if (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall")) tagPendingCommentaryText(output.content);
|
|
1482
|
+
flushPendingTextEnds();
|
|
1483
|
+
finalizeTransportStream({
|
|
1484
|
+
stream,
|
|
1485
|
+
output
|
|
1486
|
+
});
|
|
1487
|
+
} catch (error) {
|
|
1488
|
+
if (refusalBuffer) {
|
|
1489
|
+
refusalBuffer.discard();
|
|
1490
|
+
output.content = [];
|
|
1491
|
+
}
|
|
1492
|
+
failTransportStream({
|
|
1493
|
+
stream,
|
|
1494
|
+
output,
|
|
1495
|
+
signal: options?.signal,
|
|
1496
|
+
error,
|
|
1497
|
+
cleanup: () => {
|
|
1498
|
+
for (const block of output.content) delete block.index;
|
|
1499
|
+
}
|
|
1500
|
+
});
|
|
1501
|
+
}
|
|
1502
|
+
})();
|
|
1503
|
+
return eventStream;
|
|
1504
|
+
};
|
|
1505
|
+
}
|
|
1506
|
+
//#endregion
|
|
1507
|
+
//#region packages/ai/src/transports/deepseek-text-filter.ts
|
|
1508
|
+
/**
|
|
1509
|
+
* DeepSeek DSML streaming text filter.
|
|
1510
|
+
* Removes provider-emitted DSML tool markup while buffering split tag prefixes
|
|
1511
|
+
* across streamed chunks.
|
|
1512
|
+
*/
|
|
1513
|
+
const DSML_KINDS = [
|
|
1514
|
+
"tool_use_error",
|
|
1515
|
+
"tool_calls",
|
|
1516
|
+
"tool_call",
|
|
1517
|
+
"function_calls"
|
|
1518
|
+
];
|
|
1519
|
+
const DSML_BARS = ["|", "|"];
|
|
1520
|
+
const DSML_OPEN_TOKENS = DSML_BARS.flatMap((bar) => DSML_KINDS.map((kind) => `<${bar}DSML${bar}${kind}>`));
|
|
1521
|
+
const DSML_CLOSE_TOKENS = DSML_BARS.flatMap((bar) => DSML_KINDS.map((kind) => `</${bar}DSML${bar}${kind}>`));
|
|
1522
|
+
const MAX_OPEN_TOKEN_LEN = Math.max(...DSML_OPEN_TOKENS.map((token) => token.length));
|
|
1523
|
+
const MAX_CLOSE_TOKEN_LEN = Math.max(...DSML_CLOSE_TOKENS.map((token) => token.length));
|
|
1524
|
+
/** Create an incremental text filter that strips DeepSeek DSML tool blocks. */
|
|
1525
|
+
function createDeepSeekTextFilter() {
|
|
1526
|
+
let buffer = "";
|
|
1527
|
+
let insideDsml = false;
|
|
1528
|
+
const consume = (final) => {
|
|
1529
|
+
const output = [];
|
|
1530
|
+
const emit = (text) => {
|
|
1531
|
+
if (text) output.push(text);
|
|
1532
|
+
};
|
|
1533
|
+
while (buffer) {
|
|
1534
|
+
if (insideDsml) {
|
|
1535
|
+
const close = findEarliestToken(buffer, DSML_CLOSE_TOKENS);
|
|
1536
|
+
if (close) {
|
|
1537
|
+
buffer = buffer.slice(close.index + close.token.length);
|
|
1538
|
+
insideDsml = false;
|
|
1539
|
+
continue;
|
|
1540
|
+
}
|
|
1541
|
+
const keep = final ? 0 : Math.min(buffer.length, MAX_CLOSE_TOKEN_LEN - 1);
|
|
1542
|
+
buffer = buffer.slice(buffer.length - keep);
|
|
1543
|
+
if (final) insideDsml = false;
|
|
1544
|
+
return output;
|
|
1545
|
+
}
|
|
1546
|
+
const open = findEarliestToken(buffer, DSML_OPEN_TOKENS);
|
|
1547
|
+
if (open) {
|
|
1548
|
+
emit(buffer.slice(0, open.index));
|
|
1549
|
+
buffer = buffer.slice(open.index + open.token.length);
|
|
1550
|
+
insideDsml = true;
|
|
1551
|
+
continue;
|
|
1552
|
+
}
|
|
1553
|
+
if (final) {
|
|
1554
|
+
emit(buffer);
|
|
1555
|
+
buffer = "";
|
|
1556
|
+
return output;
|
|
1557
|
+
}
|
|
1558
|
+
const keep = longestDsmlOpenPrefixSuffixLength(buffer);
|
|
1559
|
+
const emitLength = buffer.length - keep;
|
|
1560
|
+
if (emitLength <= 0) return output;
|
|
1561
|
+
emit(buffer.slice(0, emitLength));
|
|
1562
|
+
buffer = buffer.slice(emitLength);
|
|
1563
|
+
return output;
|
|
1564
|
+
}
|
|
1565
|
+
return output;
|
|
1566
|
+
};
|
|
1567
|
+
return {
|
|
1568
|
+
push(chunk) {
|
|
1569
|
+
buffer += chunk;
|
|
1570
|
+
return consume(false);
|
|
1571
|
+
},
|
|
1572
|
+
flush() {
|
|
1573
|
+
return consume(true);
|
|
1574
|
+
}
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
|
+
function findEarliestToken(text, tokens) {
|
|
1578
|
+
let best = null;
|
|
1579
|
+
for (const token of tokens) {
|
|
1580
|
+
const index = text.indexOf(token);
|
|
1581
|
+
if (index !== -1 && (!best || index < best.index)) best = {
|
|
1582
|
+
index,
|
|
1583
|
+
token
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
return best;
|
|
1587
|
+
}
|
|
1588
|
+
function longestDsmlOpenPrefixSuffixLength(text) {
|
|
1589
|
+
const maxLength = Math.min(text.length, MAX_OPEN_TOKEN_LEN - 1);
|
|
1590
|
+
for (let length = maxLength; length > 0; length--) {
|
|
1591
|
+
const suffix = text.slice(text.length - length);
|
|
1592
|
+
if (DSML_OPEN_TOKENS.some((token) => token.startsWith(suffix))) return length;
|
|
1593
|
+
}
|
|
1594
|
+
return 0;
|
|
1595
|
+
}
|
|
1596
|
+
//#endregion
|
|
1597
|
+
//#region packages/ai/src/transports/model-max-tokens-params.ts
|
|
1598
|
+
/**
|
|
1599
|
+
* Max-token parameter normalization across provider/native naming variants.
|
|
1600
|
+
* Callers canonicalize aliases before dispatch so payloads cannot carry
|
|
1601
|
+
* conflicting limits.
|
|
1602
|
+
*/
|
|
1603
|
+
const MAX_TOKENS_PARAM_KEYS = [
|
|
1604
|
+
"maxTokens",
|
|
1605
|
+
"max_completion_tokens",
|
|
1606
|
+
"max_tokens"
|
|
1607
|
+
];
|
|
1608
|
+
/** Return a finite non-negative max-token value, or undefined for invalid input. */
|
|
1609
|
+
function resolveNonNegativeMaxTokensParam(value) {
|
|
1610
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
1611
|
+
}
|
|
1612
|
+
/** Resolve the first supported max-token parameter present in a params object. */
|
|
1613
|
+
function resolveMaxTokensParam(params) {
|
|
1614
|
+
if (!params) return;
|
|
1615
|
+
for (const key of MAX_TOKENS_PARAM_KEYS) {
|
|
1616
|
+
const resolved = resolveNonNegativeMaxTokensParam(params[key]);
|
|
1617
|
+
if (resolved !== void 0) return resolved;
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* Canonicalize merged params to `maxTokens`, preserving source precedence from
|
|
1622
|
+
* left to right across the provided source objects.
|
|
1623
|
+
*/
|
|
1624
|
+
function canonicalizeMaxTokensParam(params) {
|
|
1625
|
+
let resolved;
|
|
1626
|
+
for (const source of params.sources) {
|
|
1627
|
+
const sourceValue = resolveMaxTokensParam(source);
|
|
1628
|
+
if (sourceValue !== void 0) resolved = sourceValue;
|
|
1629
|
+
}
|
|
1630
|
+
if (resolved === void 0) return;
|
|
1631
|
+
for (const key of MAX_TOKENS_PARAM_KEYS) delete params.merged[key];
|
|
1632
|
+
params.merged.maxTokens = resolved;
|
|
1633
|
+
}
|
|
1634
|
+
//#endregion
|
|
1635
|
+
//#region packages/ai/src/transports/model-transport-url.ts
|
|
1636
|
+
/**
|
|
1637
|
+
* Debug formatting helpers for model transport endpoints.
|
|
1638
|
+
* Keeps logs useful without exposing credentials, request params, or fragments.
|
|
1639
|
+
*/
|
|
1640
|
+
/** Return a sanitized URL suitable for logs and diagnostics. */
|
|
1641
|
+
function formatModelTransportDebugUrl(rawUrl) {
|
|
1642
|
+
try {
|
|
1643
|
+
const parsed = new URL(rawUrl);
|
|
1644
|
+
parsed.username = "";
|
|
1645
|
+
parsed.password = "";
|
|
1646
|
+
parsed.search = "";
|
|
1647
|
+
parsed.hash = "";
|
|
1648
|
+
return parsed.toString();
|
|
1649
|
+
} catch {
|
|
1650
|
+
return "<invalid-url>";
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
/** Format a configured base URL for debug output, or the implicit default. */
|
|
1654
|
+
function formatModelTransportDebugBaseUrl(rawUrl) {
|
|
1655
|
+
return rawUrl ? formatModelTransportDebugUrl(rawUrl) : "default";
|
|
1656
|
+
}
|
|
1657
|
+
//#endregion
|
|
1658
|
+
//#region packages/ai/src/transports/openai-compatible-conversation-turn.ts
|
|
1659
|
+
/**
|
|
1660
|
+
* OpenAI-compatible conversation turn detector.
|
|
1661
|
+
*
|
|
1662
|
+
* Some providers reject requests without a non-empty user/assistant turn; this
|
|
1663
|
+
* helper checks the loose message payload shape before transport submission.
|
|
1664
|
+
*/
|
|
1665
|
+
function hasNonEmptyContentPart(part) {
|
|
1666
|
+
if (!part || typeof part !== "object") return false;
|
|
1667
|
+
const record = part;
|
|
1668
|
+
if (record.type === "text") return hasNonEmptyString(record.text);
|
|
1669
|
+
return true;
|
|
1670
|
+
}
|
|
1671
|
+
function hasNonEmptyMessageContent(content) {
|
|
1672
|
+
if (hasNonEmptyString(content)) return true;
|
|
1673
|
+
if (!Array.isArray(content)) return false;
|
|
1674
|
+
return content.some(hasNonEmptyContentPart);
|
|
1675
|
+
}
|
|
1676
|
+
function hasAssistantToolCall(message) {
|
|
1677
|
+
const toolCalls = message.tool_calls;
|
|
1678
|
+
return Array.isArray(toolCalls) && toolCalls.some((toolCall) => {
|
|
1679
|
+
return Boolean(toolCall && typeof toolCall === "object");
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
/** Returns whether an OpenAI-compatible messages payload contains a usable turn. */
|
|
1683
|
+
function hasOpenAICompatibleConversationTurn(messages) {
|
|
1684
|
+
if (!Array.isArray(messages)) return false;
|
|
1685
|
+
return messages.some((message) => {
|
|
1686
|
+
if (!message || typeof message !== "object") return false;
|
|
1687
|
+
const record = message;
|
|
1688
|
+
if (record.role === "user") return hasNonEmptyMessageContent(record.content);
|
|
1689
|
+
if (record.role === "assistant") return hasNonEmptyMessageContent(record.content) || hasAssistantToolCall(record);
|
|
1690
|
+
return false;
|
|
1691
|
+
});
|
|
1692
|
+
}
|
|
1693
|
+
//#endregion
|
|
1694
|
+
//#region packages/ai/src/transports/openai-completions-string-content.ts
|
|
1695
|
+
/**
|
|
1696
|
+
* OpenAI Chat Completions compatibility helpers. Some providers only accept
|
|
1697
|
+
* role/content messages with plain string content instead of text block arrays.
|
|
1698
|
+
*/
|
|
1699
|
+
function flattenStringOnlyCompletionContent(content) {
|
|
1700
|
+
if (!Array.isArray(content)) return content;
|
|
1701
|
+
const textParts = [];
|
|
1702
|
+
for (const item of content) {
|
|
1703
|
+
if (!item || typeof item !== "object" || item.type !== "text" || typeof item.text !== "string") return content;
|
|
1704
|
+
textParts.push(item.text);
|
|
1705
|
+
}
|
|
1706
|
+
return textParts.join("\n");
|
|
1707
|
+
}
|
|
1708
|
+
/** Flatten string-only text block content arrays into newline-joined strings. */
|
|
1709
|
+
function flattenCompletionMessagesToStringContent(messages) {
|
|
1710
|
+
return messages.map((message) => {
|
|
1711
|
+
if (!message || typeof message !== "object") return message;
|
|
1712
|
+
const content = message.content;
|
|
1713
|
+
const flattenedContent = flattenStringOnlyCompletionContent(content);
|
|
1714
|
+
if (flattenedContent === content) return message;
|
|
1715
|
+
return {
|
|
1716
|
+
...message,
|
|
1717
|
+
content: flattenedContent
|
|
1718
|
+
};
|
|
1719
|
+
});
|
|
1720
|
+
}
|
|
1721
|
+
/** Strip completion messages to role/content fields for strict providers. */
|
|
1722
|
+
function stripCompletionMessagesToRoleContent(messages) {
|
|
1723
|
+
return messages.map((message) => {
|
|
1724
|
+
if (!message || typeof message !== "object" || Array.isArray(message)) return message;
|
|
1725
|
+
const record = message;
|
|
1726
|
+
const stripped = {};
|
|
1727
|
+
if (Object.hasOwn(record, "role")) stripped.role = record.role;
|
|
1728
|
+
if (Object.hasOwn(record, "content")) stripped.content = record.content;
|
|
1729
|
+
return stripped;
|
|
1730
|
+
});
|
|
1731
|
+
}
|
|
1732
|
+
//#endregion
|
|
1733
|
+
//#region packages/ai/src/transports/openai-transport-params.ts
|
|
1734
|
+
const MAX_OPENAI_STRICT_TOOL_DOWNGRADE_DIAGNOSTIC_KEYS = 256;
|
|
1735
|
+
const OPENAI_CODEX_RESPONSES_PROVIDERS = /* @__PURE__ */ new Set(["openai"]);
|
|
1736
|
+
const loggedOpenAIStrictToolDowngradeDiagnosticKeys = /* @__PURE__ */ new Set();
|
|
1737
|
+
function readToolPayloadField(record, field) {
|
|
1738
|
+
try {
|
|
1739
|
+
return record[field];
|
|
1740
|
+
} catch {
|
|
1741
|
+
return;
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
function transportPayloadToolName(tool) {
|
|
1745
|
+
if (!isRecord(tool)) return;
|
|
1746
|
+
const name = readToolPayloadField(tool, "name");
|
|
1747
|
+
if (typeof name === "string") return name;
|
|
1748
|
+
const fn = readToolPayloadField(tool, "function");
|
|
1749
|
+
if (!isRecord(fn)) return;
|
|
1750
|
+
const fnName = readToolPayloadField(fn, "name");
|
|
1751
|
+
return typeof fnName === "string" ? fnName : void 0;
|
|
1752
|
+
}
|
|
1753
|
+
function enforceCodeModeResponsesToolSurface(payload) {
|
|
1754
|
+
if (!isRecord(payload) || !Array.isArray(payload.tools)) return;
|
|
1755
|
+
payload.tools = payload.tools.filter((tool) => {
|
|
1756
|
+
const name = transportPayloadToolName(tool);
|
|
1757
|
+
return typeof name === "string" && isCodeModeModelVisibleToolName(name);
|
|
1758
|
+
});
|
|
1759
|
+
}
|
|
1760
|
+
function assertCodeModeResponsesToolSurface(payload) {
|
|
1761
|
+
if (!isRecord(payload) || !Array.isArray(payload.tools)) throw new Error("Code mode payload tool surface violation: expected exec,wait; got no tools");
|
|
1762
|
+
const names = payload.tools.map(transportPayloadToolName).filter((name) => typeof name === "string" && name.length > 0).toSorted((left, right) => left.localeCompare(right));
|
|
1763
|
+
if (names.length >= 2 && new Set(names).size === names.length && names.filter((name) => name === "exec").length === 1 && names.filter((name) => name === "wait").length === 1 && names.every(isCodeModeModelVisibleToolName)) return;
|
|
1764
|
+
throw new Error(`Code mode payload tool surface violation: expected exec,wait plus direct-only tools; got ${names.length > 0 ? names.join(",") : "none"}`);
|
|
1765
|
+
}
|
|
1766
|
+
function buildOpenAIStrictToolDowngradeDiagnosticKey(diagnostics, context) {
|
|
1767
|
+
return sha256Hex(JSON.stringify({
|
|
1768
|
+
transport: context.transport,
|
|
1769
|
+
provider: context.model.provider ?? null,
|
|
1770
|
+
model: context.model.id ?? null,
|
|
1771
|
+
diagnostics: diagnostics.map((entry) => ({
|
|
1772
|
+
toolIndex: entry.toolIndex,
|
|
1773
|
+
toolName: entry.toolName ?? null,
|
|
1774
|
+
violations: entry.violations
|
|
1775
|
+
}))
|
|
1776
|
+
}));
|
|
1777
|
+
}
|
|
1778
|
+
function shouldLogOpenAIStrictToolDowngradeDiagnostic(diagnostics, context) {
|
|
1779
|
+
const key = buildOpenAIStrictToolDowngradeDiagnosticKey(diagnostics, context);
|
|
1780
|
+
if (loggedOpenAIStrictToolDowngradeDiagnosticKeys.has(key)) return false;
|
|
1781
|
+
if (loggedOpenAIStrictToolDowngradeDiagnosticKeys.size >= MAX_OPENAI_STRICT_TOOL_DOWNGRADE_DIAGNOSTIC_KEYS) loggedOpenAIStrictToolDowngradeDiagnosticKeys.clear();
|
|
1782
|
+
loggedOpenAIStrictToolDowngradeDiagnosticKeys.add(key);
|
|
1783
|
+
return true;
|
|
1784
|
+
}
|
|
1785
|
+
function resolveOpenAIStrictToolFlagWithDiagnostics(projection, strictSetting, context) {
|
|
1786
|
+
const strict = resolveOpenAIProjectedToolsStrictToolFlag(projection, strictSetting);
|
|
1787
|
+
if (strictSetting === true && strict === false) {
|
|
1788
|
+
const diagnostics = findOpenAIStrictToolProjectionDiagnostics(projection);
|
|
1789
|
+
getAiTransportHost().logDebug("openai-transport", () => {
|
|
1790
|
+
if (!shouldLogOpenAIStrictToolDowngradeDiagnostic(diagnostics, context)) return null;
|
|
1791
|
+
const sample = diagnostics.slice(0, 5).map((entry) => ({
|
|
1792
|
+
tool: entry.toolName ?? `tool[${entry.toolIndex}]`,
|
|
1793
|
+
violations: entry.violations.slice(0, 8)
|
|
1794
|
+
}));
|
|
1795
|
+
return {
|
|
1796
|
+
message: `OpenAI ${context.transport} tool schema strict mode downgraded to strict=false for ${context.model.provider ?? "unknown"}/${context.model.id ?? "unknown"} because ${diagnostics.length} tool schema(s) are not strict-compatible`,
|
|
1797
|
+
data: {
|
|
1798
|
+
transport: context.transport,
|
|
1799
|
+
provider: context.model.provider,
|
|
1800
|
+
model: context.model.id,
|
|
1801
|
+
incompatibleToolCount: diagnostics.length,
|
|
1802
|
+
sample
|
|
1803
|
+
}
|
|
1804
|
+
};
|
|
1805
|
+
});
|
|
1806
|
+
}
|
|
1807
|
+
return strict;
|
|
1808
|
+
}
|
|
1809
|
+
function isOpenAICodexResponsesModel(model) {
|
|
1810
|
+
return OPENAI_CODEX_RESPONSES_PROVIDERS.has(model.provider) && (model.api === "openai-chatgpt-responses" || model.api === "openclaw-openai-chatgpt-responses-transport");
|
|
1811
|
+
}
|
|
1812
|
+
function isNativeOpenAICodexResponsesBaseUrl(baseUrl) {
|
|
1813
|
+
const trimmed = typeof baseUrl === "string" ? baseUrl.trim() : "";
|
|
1814
|
+
if (!trimmed) return false;
|
|
1815
|
+
try {
|
|
1816
|
+
const url = new URL(trimmed);
|
|
1817
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
|
|
1818
|
+
if (url.hostname.toLowerCase() !== "chatgpt.com") return false;
|
|
1819
|
+
const pathname = url.pathname.replace(/\/+$/u, "").toLowerCase();
|
|
1820
|
+
return [
|
|
1821
|
+
"/backend-api",
|
|
1822
|
+
"/backend-api/v1",
|
|
1823
|
+
"/backend-api/codex",
|
|
1824
|
+
"/backend-api/codex/v1"
|
|
1825
|
+
].includes(pathname);
|
|
1826
|
+
} catch {
|
|
1827
|
+
return false;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
function usesNativeOpenAICodexResponsesBackend(model) {
|
|
1831
|
+
return isOpenAICodexResponsesModel(model) && isNativeOpenAICodexResponsesBaseUrl(model.baseUrl);
|
|
1832
|
+
}
|
|
1833
|
+
function buildOpenAIClientHeaders(model, context, optionHeaders, turnHeaders, sessionId) {
|
|
1834
|
+
const providerHeaders = { ...model.headers };
|
|
1835
|
+
if (model.provider === "github-copilot") Object.assign(providerHeaders, getAiTransportHost().buildCopilotDynamicHeaders(context.messages));
|
|
1836
|
+
const callerHeaders = {
|
|
1837
|
+
...optionHeaders,
|
|
1838
|
+
...turnHeaders
|
|
1839
|
+
};
|
|
1840
|
+
const resolvedHeaders = resolveProviderRequestPolicyConfig({
|
|
1841
|
+
provider: model.provider,
|
|
1842
|
+
api: model.api,
|
|
1843
|
+
baseUrl: model.baseUrl,
|
|
1844
|
+
capability: "llm",
|
|
1845
|
+
transport: "stream",
|
|
1846
|
+
providerHeaders,
|
|
1847
|
+
callerHeaders: Object.keys(callerHeaders).length > 0 ? callerHeaders : void 0,
|
|
1848
|
+
precedence: "caller-wins"
|
|
1849
|
+
}).headers ?? {};
|
|
1850
|
+
if (sessionId && !Object.keys(resolvedHeaders).some((key) => normalizeLowercaseStringOrEmpty(key) === "session_id") && usesNativeOpenAICodexResponsesBackend(model)) resolvedHeaders.session_id = clampOpenAIPromptCacheKey(sessionId) ?? sessionId;
|
|
1851
|
+
return resolvedHeaders;
|
|
1852
|
+
}
|
|
1853
|
+
function resolveOpenAISdkTimeoutMs(model) {
|
|
1854
|
+
return resolveModelRequestTimeoutMs(model, void 0);
|
|
1855
|
+
}
|
|
1856
|
+
function buildOpenAISdkClientOptions(model) {
|
|
1857
|
+
const timeout = resolveOpenAISdkTimeoutMs(model);
|
|
1858
|
+
return timeout === void 0 ? {} : { timeout };
|
|
1859
|
+
}
|
|
1860
|
+
function buildOpenAISdkRequestOptions(model, signal, options) {
|
|
1861
|
+
const timeout = resolveOpenAISdkTimeoutMs(model);
|
|
1862
|
+
const headers = options?.stream === true && usesNativeOpenAICodexResponsesBackend(model) ? { Accept: "text/event-stream" } : void 0;
|
|
1863
|
+
if (timeout === void 0 && !signal && !headers) return;
|
|
1864
|
+
return {
|
|
1865
|
+
...headers ? { headers } : {},
|
|
1866
|
+
...signal ? { signal } : {},
|
|
1867
|
+
...timeout !== void 0 ? { timeout } : {}
|
|
1868
|
+
};
|
|
1869
|
+
}
|
|
1870
|
+
function getCompat(model) {
|
|
1871
|
+
const resolved = resolveOpenAICompletionsCompat(model);
|
|
1872
|
+
const compat = model.compat ?? {};
|
|
1873
|
+
return {
|
|
1874
|
+
...resolved,
|
|
1875
|
+
cacheControlFormat: resolved.cacheControlFormat,
|
|
1876
|
+
reasoningEffortMap: resolveOpenAIReasoningEffortMap(model, {}),
|
|
1877
|
+
openRouterRouting: resolved.openRouterRouting ?? {},
|
|
1878
|
+
vercelGatewayRouting: resolved.vercelGatewayRouting,
|
|
1879
|
+
requiresStringContent: compat.requiresStringContent ?? false,
|
|
1880
|
+
strictMessageKeys: compat.strictMessageKeys === true
|
|
1881
|
+
};
|
|
1882
|
+
}
|
|
1883
|
+
//#endregion
|
|
1884
|
+
//#region packages/ai/src/transports/openai-completions-transport.ts
|
|
1885
|
+
function hasToolHistory(messages) {
|
|
1886
|
+
return messages.some((message) => message.role === "toolResult" || message.role === "assistant" && Array.isArray(message.content) && message.content.some((block) => block.type === "toolCall"));
|
|
1887
|
+
}
|
|
1888
|
+
function assertOpenAICompletionsPayloadHasConversationTurn(params, model) {
|
|
1889
|
+
const messages = params.messages;
|
|
1890
|
+
if (!Array.isArray(messages) || hasOpenAICompatibleConversationTurn(messages)) return;
|
|
1891
|
+
throw new Error(`OpenAI-compatible chat payload for ${model.provider}/${model.id} contains no non-empty user or assistant messages after compaction and transport transforms; refusing to send a system/tool-only request. Start a new user turn or repair the compacted session history.`);
|
|
1892
|
+
}
|
|
1893
|
+
const SSE_DONE_LINE_RE = /^data:[ \t]*\[DONE\][ \t]*$/i;
|
|
1894
|
+
const SSE_DONE_MAX_LINE_CHARS = 1024;
|
|
1895
|
+
function createSseDoneDetector() {
|
|
1896
|
+
const decoder = new TextDecoder();
|
|
1897
|
+
let line = "";
|
|
1898
|
+
let lineOverflowed = false;
|
|
1899
|
+
let sawDone = false;
|
|
1900
|
+
const finishLine = () => {
|
|
1901
|
+
if (!lineOverflowed && SSE_DONE_LINE_RE.test(line)) sawDone = true;
|
|
1902
|
+
line = "";
|
|
1903
|
+
lineOverflowed = false;
|
|
1904
|
+
};
|
|
1905
|
+
const observeText = (text) => {
|
|
1906
|
+
for (const char of text) {
|
|
1907
|
+
if (char === "\n" || char === "\r") {
|
|
1908
|
+
finishLine();
|
|
1909
|
+
continue;
|
|
1910
|
+
}
|
|
1911
|
+
if (!lineOverflowed && line.length < SSE_DONE_MAX_LINE_CHARS) line += char;
|
|
1912
|
+
else lineOverflowed = true;
|
|
1913
|
+
}
|
|
1914
|
+
};
|
|
1915
|
+
return {
|
|
1916
|
+
observe(chunk) {
|
|
1917
|
+
if (!sawDone) observeText(decoder.decode(chunk, { stream: true }));
|
|
1918
|
+
},
|
|
1919
|
+
finish() {
|
|
1920
|
+
if (sawDone) return;
|
|
1921
|
+
observeText(decoder.decode());
|
|
1922
|
+
if (line || lineOverflowed) finishLine();
|
|
1923
|
+
},
|
|
1924
|
+
sawDone: () => sawDone
|
|
1925
|
+
};
|
|
1926
|
+
}
|
|
1927
|
+
function createOpenAICompletionsClient(model, context, apiKey, optionHeaders, opts) {
|
|
1928
|
+
const clientConfig = buildOpenAICompletionsClientConfig(model, context, optionHeaders);
|
|
1929
|
+
return new OpenAI({
|
|
1930
|
+
apiKey,
|
|
1931
|
+
baseURL: clientConfig.baseURL,
|
|
1932
|
+
dangerouslyAllowBrowser: true,
|
|
1933
|
+
defaultHeaders: clientConfig.defaultHeaders,
|
|
1934
|
+
defaultQuery: clientConfig.defaultQuery,
|
|
1935
|
+
fetch: opts?.fetch ?? buildGuardedModelFetch(model),
|
|
1936
|
+
...buildOpenAISdkClientOptions(model)
|
|
1937
|
+
});
|
|
1938
|
+
}
|
|
1939
|
+
function isAzureOpenAICompatibleHost(hostname) {
|
|
1940
|
+
return hostname.endsWith(".openai.azure.com") || hostname.endsWith(".services.ai.azure.com") || hostname.endsWith(".cognitiveservices.azure.com");
|
|
1941
|
+
}
|
|
1942
|
+
function isKnownOpenAICompletionsEndpoint(model) {
|
|
1943
|
+
if (!model.baseUrl.trim()) return true;
|
|
1944
|
+
const endpointClass = resolveProviderEndpoint(model.baseUrl).endpointClass;
|
|
1945
|
+
if (endpointClass === "openai-public" || endpointClass === "azure-openai") return true;
|
|
1946
|
+
try {
|
|
1947
|
+
return isAzureOpenAICompatibleHost(new URL(model.baseUrl).hostname.toLowerCase());
|
|
1948
|
+
} catch {
|
|
1949
|
+
return false;
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
function buildOpenAICompletionsClientConfig(model, context, optionHeaders) {
|
|
1953
|
+
const headers = buildOpenAIClientHeaders(model, context, optionHeaders);
|
|
1954
|
+
const defaultQuery = {};
|
|
1955
|
+
let baseURL = model.baseUrl;
|
|
1956
|
+
let isAzureHost = false;
|
|
1957
|
+
try {
|
|
1958
|
+
const parsed = new URL(model.baseUrl);
|
|
1959
|
+
isAzureHost = isAzureOpenAICompatibleHost(parsed.hostname.toLowerCase());
|
|
1960
|
+
parsed.searchParams.forEach((value, key) => {
|
|
1961
|
+
if (value) defaultQuery[key] = value;
|
|
1962
|
+
});
|
|
1963
|
+
parsed.search = "";
|
|
1964
|
+
baseURL = parsed.toString().replace(/\/$/, "");
|
|
1965
|
+
} catch {}
|
|
1966
|
+
if (isAzureHost) {
|
|
1967
|
+
const apiVersionHeader = Object.keys(headers).find((key) => key.toLowerCase() === "api-version");
|
|
1968
|
+
if (apiVersionHeader) {
|
|
1969
|
+
const apiVersion = headers[apiVersionHeader]?.trim();
|
|
1970
|
+
delete headers[apiVersionHeader];
|
|
1971
|
+
if (apiVersion && !defaultQuery["api-version"]) defaultQuery["api-version"] = apiVersion;
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
return {
|
|
1975
|
+
baseURL,
|
|
1976
|
+
defaultHeaders: headers,
|
|
1977
|
+
defaultQuery: Object.keys(defaultQuery).length > 0 ? defaultQuery : void 0
|
|
1978
|
+
};
|
|
1979
|
+
}
|
|
1980
|
+
function createOpenAICompletionsTransportStreamFn() {
|
|
1981
|
+
return (model, context, options) => {
|
|
1982
|
+
const eventStream = createAssistantMessageEventStream();
|
|
1983
|
+
const stream = eventStream;
|
|
1984
|
+
(async () => {
|
|
1985
|
+
const output = {
|
|
1986
|
+
role: "assistant",
|
|
1987
|
+
content: [],
|
|
1988
|
+
api: model.api,
|
|
1989
|
+
provider: model.provider,
|
|
1990
|
+
model: model.id,
|
|
1991
|
+
usage: {
|
|
1992
|
+
input: 0,
|
|
1993
|
+
output: 0,
|
|
1994
|
+
cacheRead: 0,
|
|
1995
|
+
cacheWrite: 0,
|
|
1996
|
+
totalTokens: 0,
|
|
1997
|
+
cost: {
|
|
1998
|
+
input: 0,
|
|
1999
|
+
output: 0,
|
|
2000
|
+
cacheRead: 0,
|
|
2001
|
+
cacheWrite: 0,
|
|
2002
|
+
total: 0
|
|
2003
|
+
}
|
|
2004
|
+
},
|
|
2005
|
+
stopReason: "stop",
|
|
2006
|
+
timestamp: Date.now()
|
|
2007
|
+
};
|
|
2008
|
+
let firstEventAbort;
|
|
2009
|
+
try {
|
|
2010
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
|
|
2011
|
+
const doneDetector = createSseDoneDetector();
|
|
2012
|
+
const baseFetch = buildGuardedModelFetch(model);
|
|
2013
|
+
const doneDetectingFetch = async (url, init) => {
|
|
2014
|
+
const response = await baseFetch(url, init);
|
|
2015
|
+
if (!response.body || !response.ok) return response;
|
|
2016
|
+
if (typeof TransformStream === "undefined" || !response.body.pipeThrough) return response;
|
|
2017
|
+
const transformed = response.body.pipeThrough(new TransformStream({
|
|
2018
|
+
transform(chunk, controller) {
|
|
2019
|
+
doneDetector.observe(chunk);
|
|
2020
|
+
controller.enqueue(chunk);
|
|
2021
|
+
},
|
|
2022
|
+
flush() {
|
|
2023
|
+
doneDetector.finish();
|
|
2024
|
+
}
|
|
2025
|
+
}));
|
|
2026
|
+
return new Response(transformed, {
|
|
2027
|
+
headers: response.headers,
|
|
2028
|
+
status: response.status,
|
|
2029
|
+
statusText: response.statusText
|
|
2030
|
+
});
|
|
2031
|
+
};
|
|
2032
|
+
const client = createOpenAICompletionsClient(model, context, apiKey, options?.headers, { fetch: doneDetectingFetch });
|
|
2033
|
+
let params = buildOpenAICompletionsParams(model, context, options);
|
|
2034
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
2035
|
+
if (nextParams !== void 0) params = nextParams;
|
|
2036
|
+
if (options?.openclawCodeModeToolSurface === true) {
|
|
2037
|
+
enforceCodeModeResponsesToolSurface(params);
|
|
2038
|
+
assertCodeModeResponsesToolSurface(params);
|
|
2039
|
+
}
|
|
2040
|
+
if (getCompat(model).requiresNonEmptyUserOrAssistantMessage) assertOpenAICompletionsPayloadHasConversationTurn(params, model);
|
|
2041
|
+
const emitReasoning = shouldEmitOpenAICompletionsReasoning(model, options);
|
|
2042
|
+
firstEventAbort = createFirstStreamEventAbortController(options?.signal);
|
|
2043
|
+
const responseStream = await client.chat.completions.create(params, buildOpenAISdkRequestOptions(model, firstEventAbort.signal));
|
|
2044
|
+
stream.push({
|
|
2045
|
+
type: "start",
|
|
2046
|
+
partial: output
|
|
2047
|
+
});
|
|
2048
|
+
await processOpenAICompletionsStream(responseStream, output, model, stream, {
|
|
2049
|
+
signal: options?.signal,
|
|
2050
|
+
emitReasoning,
|
|
2051
|
+
firstEventTimeoutMs: getFirstStreamEventTimeoutMs(options),
|
|
2052
|
+
abortFirstEventStream: firstEventAbort.abort,
|
|
2053
|
+
onFirstEventTimeout: getFirstStreamEventTimeoutHandler(options),
|
|
2054
|
+
sawStreamDONE: doneDetector.sawDone
|
|
2055
|
+
});
|
|
2056
|
+
finalizeTransportStream({
|
|
2057
|
+
stream,
|
|
2058
|
+
output,
|
|
2059
|
+
signal: options?.signal
|
|
2060
|
+
});
|
|
2061
|
+
} catch (error) {
|
|
2062
|
+
failTransportStream({
|
|
2063
|
+
stream,
|
|
2064
|
+
output,
|
|
2065
|
+
signal: options?.signal,
|
|
2066
|
+
error
|
|
2067
|
+
});
|
|
2068
|
+
} finally {
|
|
2069
|
+
firstEventAbort?.dispose();
|
|
2070
|
+
}
|
|
2071
|
+
})();
|
|
2072
|
+
return eventStream;
|
|
2073
|
+
};
|
|
2074
|
+
}
|
|
2075
|
+
async function processOpenAICompletionsStream(responseStream, output, model, stream, options) {
|
|
2076
|
+
const MAX_POST_TOOL_CALL_BUFFER_BYTES = 256e3;
|
|
2077
|
+
const MAX_TOOL_CALL_ARGUMENT_BUFFER_BYTES = 256e3;
|
|
2078
|
+
const emitReasoning = options?.emitReasoning ?? true;
|
|
2079
|
+
const compat = getCompat(model);
|
|
2080
|
+
const deepSeekTextFilter = shouldFilterDeepSeekDsmlText(compat) ? createDeepSeekTextFilter() : null;
|
|
2081
|
+
const deepSeekToolCallRecoverer = shouldFilterDeepSeekDsmlText(compat) ? createDeepSeekDsmlToolCallRecoverer() : null;
|
|
2082
|
+
const reasoningTagTextPartitioner = createReasoningTagTextPartitioner();
|
|
2083
|
+
let currentBlock = null;
|
|
2084
|
+
let pendingPostToolCallDeltas = [];
|
|
2085
|
+
let pendingPostToolCallBytes = 0;
|
|
2086
|
+
let isFlushingPendingPostToolCallDeltas = false;
|
|
2087
|
+
const toolCallBlocksByIndex = /* @__PURE__ */ new Map();
|
|
2088
|
+
const toolCallBlocksById = /* @__PURE__ */ new Map();
|
|
2089
|
+
const provisionalCommentaryTags = /* @__PURE__ */ new Map();
|
|
2090
|
+
const toolCallBlockBytes = /* @__PURE__ */ new WeakMap();
|
|
2091
|
+
const toolCallBlockIndices = /* @__PURE__ */ new WeakMap();
|
|
2092
|
+
let sawStopFinishReason = false;
|
|
2093
|
+
let sawNativeToolCallDelta = false;
|
|
2094
|
+
const blockIndex = () => output.content.length - 1;
|
|
2095
|
+
const measureUtf8Bytes = (text) => Buffer.byteLength(text, "utf8");
|
|
2096
|
+
let chunkPushedEvent = false;
|
|
2097
|
+
const pushStreamEvent = (event) => {
|
|
2098
|
+
chunkPushedEvent = true;
|
|
2099
|
+
stream.push(event);
|
|
2100
|
+
};
|
|
2101
|
+
const queuePostToolCallDelta = (next) => {
|
|
2102
|
+
const nextBytes = measureUtf8Bytes(next.text);
|
|
2103
|
+
if (pendingPostToolCallBytes + nextBytes > MAX_POST_TOOL_CALL_BUFFER_BYTES) throw new Error("Exceeded post-tool-call delta buffer limit");
|
|
2104
|
+
pendingPostToolCallBytes += nextBytes;
|
|
2105
|
+
const previous = pendingPostToolCallDeltas[pendingPostToolCallDeltas.length - 1];
|
|
2106
|
+
if (!previous || previous.kind !== next.kind) {
|
|
2107
|
+
pendingPostToolCallDeltas.push(next);
|
|
2108
|
+
return;
|
|
2109
|
+
}
|
|
2110
|
+
if (next.kind === "thinking" && previous.kind === "thinking") {
|
|
2111
|
+
if (previous.signature !== next.signature) {
|
|
2112
|
+
pendingPostToolCallDeltas.push(next);
|
|
2113
|
+
return;
|
|
2114
|
+
}
|
|
2115
|
+
previous.text += next.text;
|
|
2116
|
+
return;
|
|
2117
|
+
}
|
|
2118
|
+
previous.text += next.text;
|
|
2119
|
+
};
|
|
2120
|
+
const appendThinkingDeltaInternal = (reasoningDelta) => {
|
|
2121
|
+
if (!currentBlock || currentBlock.type !== "thinking") {
|
|
2122
|
+
currentBlock = {
|
|
2123
|
+
type: "thinking",
|
|
2124
|
+
thinking: "",
|
|
2125
|
+
...reasoningDelta.signature ? { thinkingSignature: reasoningDelta.signature } : {}
|
|
2126
|
+
};
|
|
2127
|
+
output.content.push(currentBlock);
|
|
2128
|
+
pushStreamEvent({
|
|
2129
|
+
type: "thinking_start",
|
|
2130
|
+
contentIndex: blockIndex(),
|
|
2131
|
+
partial: output
|
|
2132
|
+
});
|
|
2133
|
+
}
|
|
2134
|
+
currentBlock.thinking += reasoningDelta.text;
|
|
2135
|
+
pushStreamEvent({
|
|
2136
|
+
type: "thinking_delta",
|
|
2137
|
+
contentIndex: blockIndex(),
|
|
2138
|
+
delta: reasoningDelta.text,
|
|
2139
|
+
partial: output
|
|
2140
|
+
});
|
|
2141
|
+
};
|
|
2142
|
+
const appendTextDeltaInternal = (text) => {
|
|
2143
|
+
if (!currentBlock || currentBlock.type !== "text") {
|
|
2144
|
+
currentBlock = {
|
|
2145
|
+
type: "text",
|
|
2146
|
+
text: ""
|
|
2147
|
+
};
|
|
2148
|
+
output.content.push(currentBlock);
|
|
2149
|
+
pushStreamEvent({
|
|
2150
|
+
type: "text_start",
|
|
2151
|
+
contentIndex: blockIndex(),
|
|
2152
|
+
partial: output
|
|
2153
|
+
});
|
|
2154
|
+
}
|
|
2155
|
+
currentBlock.text += text;
|
|
2156
|
+
pushStreamEvent({
|
|
2157
|
+
type: "text_delta",
|
|
2158
|
+
contentIndex: blockIndex(),
|
|
2159
|
+
delta: text
|
|
2160
|
+
});
|
|
2161
|
+
};
|
|
2162
|
+
const flushPendingPostToolCallDeltas = () => {
|
|
2163
|
+
if (isFlushingPendingPostToolCallDeltas || currentBlock?.type === "toolCall" || pendingPostToolCallDeltas.length === 0) return;
|
|
2164
|
+
isFlushingPendingPostToolCallDeltas = true;
|
|
2165
|
+
const bufferedDeltas = pendingPostToolCallDeltas;
|
|
2166
|
+
pendingPostToolCallDeltas = [];
|
|
2167
|
+
pendingPostToolCallBytes = 0;
|
|
2168
|
+
for (const delta of bufferedDeltas) if (delta.kind === "text") appendTextDeltaInternal(delta.text);
|
|
2169
|
+
else if (emitReasoning) appendThinkingDeltaInternal(delta);
|
|
2170
|
+
isFlushingPendingPostToolCallDeltas = false;
|
|
2171
|
+
};
|
|
2172
|
+
const appendThinkingDelta = (reasoningDelta) => {
|
|
2173
|
+
flushPendingPostToolCallDeltas();
|
|
2174
|
+
appendThinkingDeltaInternal(reasoningDelta);
|
|
2175
|
+
};
|
|
2176
|
+
const appendTextDelta = (text) => {
|
|
2177
|
+
flushPendingPostToolCallDeltas();
|
|
2178
|
+
appendTextDeltaInternal(text);
|
|
2179
|
+
};
|
|
2180
|
+
const appendVisibleTextDelta = (text) => {
|
|
2181
|
+
if (!text) return;
|
|
2182
|
+
if (currentBlock?.type === "toolCall") queuePostToolCallDelta({
|
|
2183
|
+
kind: "text",
|
|
2184
|
+
text
|
|
2185
|
+
});
|
|
2186
|
+
else appendTextDelta(text);
|
|
2187
|
+
};
|
|
2188
|
+
const appendRecoveredToolCall = (toolCall) => {
|
|
2189
|
+
if (currentBlock?.type === "toolCall") {
|
|
2190
|
+
currentBlock = null;
|
|
2191
|
+
flushPendingPostToolCallDeltas();
|
|
2192
|
+
}
|
|
2193
|
+
rememberPendingCommentaryTags(provisionalCommentaryTags, tagPendingCommentaryText(output.content));
|
|
2194
|
+
const block = {
|
|
2195
|
+
type: "toolCall",
|
|
2196
|
+
id: `call_${randomUUID().replaceAll("-", "").slice(0, 24)}`,
|
|
2197
|
+
name: toolCall.name,
|
|
2198
|
+
arguments: toolCall.arguments,
|
|
2199
|
+
partialArgs: toolCall.partialArgs
|
|
2200
|
+
};
|
|
2201
|
+
currentBlock = block;
|
|
2202
|
+
output.content.push(block);
|
|
2203
|
+
toolCallBlockIndices.set(block, output.content.length - 1);
|
|
2204
|
+
pushStreamEvent({
|
|
2205
|
+
type: "toolcall_start",
|
|
2206
|
+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
|
|
2207
|
+
partial: output
|
|
2208
|
+
});
|
|
2209
|
+
pushStreamEvent({
|
|
2210
|
+
type: "toolcall_delta",
|
|
2211
|
+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
|
|
2212
|
+
delta: toolCall.partialArgs,
|
|
2213
|
+
partial: output
|
|
2214
|
+
});
|
|
2215
|
+
};
|
|
2216
|
+
const appendFilteredVisibleTextDelta = (text) => {
|
|
2217
|
+
const recoveredParts = deepSeekToolCallRecoverer?.push(text) ?? [{
|
|
2218
|
+
kind: "text",
|
|
2219
|
+
text
|
|
2220
|
+
}];
|
|
2221
|
+
for (const recoveredPart of recoveredParts) {
|
|
2222
|
+
if (recoveredPart.kind === "toolCall") {
|
|
2223
|
+
appendRecoveredToolCall(recoveredPart);
|
|
2224
|
+
continue;
|
|
2225
|
+
}
|
|
2226
|
+
const parts = deepSeekTextFilter?.push(recoveredPart.text) ?? [recoveredPart.text];
|
|
2227
|
+
for (const part of parts) appendVisibleTextDelta(part);
|
|
2228
|
+
}
|
|
2229
|
+
};
|
|
2230
|
+
const flushDeepSeekToolCallRecovererAtEnd = () => {
|
|
2231
|
+
const recoveredParts = deepSeekToolCallRecoverer?.flush();
|
|
2232
|
+
if (!recoveredParts) return;
|
|
2233
|
+
for (const recoveredPart of recoveredParts) {
|
|
2234
|
+
if (recoveredPart.kind === "toolCall") {
|
|
2235
|
+
appendRecoveredToolCall(recoveredPart);
|
|
2236
|
+
continue;
|
|
2237
|
+
}
|
|
2238
|
+
const parts = deepSeekTextFilter?.push(recoveredPart.text) ?? [recoveredPart.text];
|
|
2239
|
+
for (const part of parts) appendVisibleTextDelta(part);
|
|
2240
|
+
}
|
|
2241
|
+
};
|
|
2242
|
+
const flushDeepSeekTextFilterAtEnd = () => {
|
|
2243
|
+
const parts = deepSeekTextFilter?.flush();
|
|
2244
|
+
if (!parts) return;
|
|
2245
|
+
for (const part of parts) appendVisibleTextDelta(part);
|
|
2246
|
+
};
|
|
2247
|
+
const appendRoutedContentDelta = (delta) => {
|
|
2248
|
+
if (delta.kind === "text") {
|
|
2249
|
+
appendFilteredVisibleTextDelta(delta.text);
|
|
2250
|
+
return;
|
|
2251
|
+
}
|
|
2252
|
+
if (!emitReasoning) return;
|
|
2253
|
+
if (currentBlock?.type === "toolCall") queuePostToolCallDelta(delta);
|
|
2254
|
+
else appendThinkingDelta(delta);
|
|
2255
|
+
};
|
|
2256
|
+
const appendPartitionedVisibleDelta = (delta) => {
|
|
2257
|
+
if (delta.kind === "text") appendFilteredVisibleTextDelta(delta.text);
|
|
2258
|
+
};
|
|
2259
|
+
const emitReasoningUsageActivity = (hasReasoningUsageActivity) => {
|
|
2260
|
+
if (!hasReasoningUsageActivity || chunkPushedEvent || !emitReasoning) return;
|
|
2261
|
+
const latestBlock = output.content[output.content.length - 1];
|
|
2262
|
+
if (currentBlock?.type === "text" || currentBlock?.type === "toolCall") return;
|
|
2263
|
+
if (latestBlock?.type === "text" || latestBlock?.type === "toolCall") return;
|
|
2264
|
+
appendThinkingDelta({
|
|
2265
|
+
signature: "",
|
|
2266
|
+
text: ""
|
|
2267
|
+
});
|
|
2268
|
+
};
|
|
2269
|
+
const flushReasoningTagTextPartitionerAtEnd = () => {
|
|
2270
|
+
for (const delta of reasoningTagTextPartitioner.flush()) appendPartitionedVisibleDelta(delta);
|
|
2271
|
+
};
|
|
2272
|
+
const cooperativeScheduler = createModelStreamCooperativeScheduler(options?.signal);
|
|
2273
|
+
const guardedStream = withFirstStreamEventTimeout(responseStream, {
|
|
2274
|
+
provider: model.provider,
|
|
2275
|
+
api: model.api,
|
|
2276
|
+
model: model.id,
|
|
2277
|
+
timeoutMs: options?.firstEventTimeoutMs ?? 0,
|
|
2278
|
+
stage: "completions",
|
|
2279
|
+
abort: options?.abortFirstEventStream,
|
|
2280
|
+
onTimeout: options?.onFirstEventTimeout,
|
|
2281
|
+
hint: "The provider may be stalled while parsing the tool payload; retry with a smaller tool surface or enable OPENCLAW_DEBUG_MODEL_PAYLOAD=tools to inspect exposed tools."
|
|
2282
|
+
});
|
|
2283
|
+
for await (const rawChunk of guardedStream) {
|
|
2284
|
+
throwIfModelStreamAborted(options?.signal);
|
|
2285
|
+
chunkPushedEvent = false;
|
|
2286
|
+
if (!rawChunk || typeof rawChunk !== "object") {
|
|
2287
|
+
await cooperativeScheduler.afterEvent();
|
|
2288
|
+
continue;
|
|
2289
|
+
}
|
|
2290
|
+
const chunk = rawChunk;
|
|
2291
|
+
output.responseId ||= chunk.id;
|
|
2292
|
+
let hasReasoningUsageActivity = false;
|
|
2293
|
+
if (chunk.usage) {
|
|
2294
|
+
output.usage = parseTransportChunkUsage(chunk.usage, model);
|
|
2295
|
+
hasReasoningUsageActivity = hasOpenAICompletionsReasoningUsageActivity(chunk.usage);
|
|
2296
|
+
}
|
|
2297
|
+
const choice = Array.isArray(chunk.choices) ? chunk.choices[0] : void 0;
|
|
2298
|
+
if (!choice) {
|
|
2299
|
+
emitReasoningUsageActivity(hasReasoningUsageActivity);
|
|
2300
|
+
await cooperativeScheduler.afterEvent();
|
|
2301
|
+
continue;
|
|
2302
|
+
}
|
|
2303
|
+
const choiceUsage = choice.usage;
|
|
2304
|
+
if (!chunk.usage && choiceUsage) {
|
|
2305
|
+
output.usage = parseTransportChunkUsage(choiceUsage, model);
|
|
2306
|
+
hasReasoningUsageActivity = hasOpenAICompletionsReasoningUsageActivity(choiceUsage);
|
|
2307
|
+
}
|
|
2308
|
+
if (choice.finish_reason) {
|
|
2309
|
+
const finishReasonResult = mapOpenAIStopReason(choice.finish_reason, { allowSingularToolCall: true });
|
|
2310
|
+
output.stopReason = finishReasonResult.stopReason;
|
|
2311
|
+
if (finishReasonResult.stopReason === "stop") sawStopFinishReason = true;
|
|
2312
|
+
if (finishReasonResult.errorMessage) output.errorMessage = finishReasonResult.errorMessage;
|
|
2313
|
+
}
|
|
2314
|
+
const choiceDelta = choice.delta ?? choice.message;
|
|
2315
|
+
if (!choiceDelta) {
|
|
2316
|
+
emitReasoningUsageActivity(hasReasoningUsageActivity);
|
|
2317
|
+
await cooperativeScheduler.afterEvent();
|
|
2318
|
+
continue;
|
|
2319
|
+
}
|
|
2320
|
+
const reasoningDeltas = getCompletionsReasoningDeltas(choiceDelta, compat.visibleReasoningDetailTypes);
|
|
2321
|
+
const hasMirroredReasoning = reasoningDeltas.some((delta) => delta.kind === "thinking");
|
|
2322
|
+
if (hasMirroredReasoning) reasoningTagTextPartitioner.markStrict();
|
|
2323
|
+
if (choiceDelta.content) {
|
|
2324
|
+
const contentDeltas = getCompletionsContentDeltas(choiceDelta.content);
|
|
2325
|
+
for (const contentDelta of contentDeltas) if (contentDelta.kind === "text") {
|
|
2326
|
+
const routedDeltas = hasMirroredReasoning ? reasoningTagTextPartitioner.push(contentDelta.text) : reasoningTagTextPartitioner.pushVisible(contentDelta.text);
|
|
2327
|
+
for (const routedDelta of routedDeltas) appendPartitionedVisibleDelta(routedDelta);
|
|
2328
|
+
} else {
|
|
2329
|
+
reasoningTagTextPartitioner.markStrict();
|
|
2330
|
+
appendRoutedContentDelta(contentDelta);
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
const refusalText = typeof choiceDelta.refusal === "string" ? choiceDelta.refusal : "";
|
|
2334
|
+
if (refusalText) {
|
|
2335
|
+
const routedDeltas = hasMirroredReasoning ? reasoningTagTextPartitioner.push(refusalText) : reasoningTagTextPartitioner.pushVisible(refusalText);
|
|
2336
|
+
for (const routedDelta of routedDeltas) appendPartitionedVisibleDelta(routedDelta);
|
|
2337
|
+
}
|
|
2338
|
+
for (const reasoningDelta of reasoningDeltas) {
|
|
2339
|
+
if (reasoningDelta.kind === "thinking" && !emitReasoning) continue;
|
|
2340
|
+
if (currentBlock?.type === "toolCall") {
|
|
2341
|
+
queuePostToolCallDelta({ ...reasoningDelta });
|
|
2342
|
+
continue;
|
|
2343
|
+
}
|
|
2344
|
+
if (reasoningDelta.kind === "text") appendTextDelta(reasoningDelta.text);
|
|
2345
|
+
else if (emitReasoning) appendThinkingDelta(reasoningDelta);
|
|
2346
|
+
}
|
|
2347
|
+
if (choiceDelta.tool_calls && choiceDelta.tool_calls.length > 0) {
|
|
2348
|
+
sawNativeToolCallDelta = true;
|
|
2349
|
+
flushReasoningTagTextPartitionerAtEnd();
|
|
2350
|
+
rememberPendingCommentaryTags(provisionalCommentaryTags, tagPendingCommentaryText(output.content));
|
|
2351
|
+
for (const toolCall of choiceDelta.tool_calls) {
|
|
2352
|
+
const streamIndex = typeof toolCall.index === "number" ? toolCall.index : void 0;
|
|
2353
|
+
let block = streamIndex !== void 0 ? toolCallBlocksByIndex.get(streamIndex) : void 0;
|
|
2354
|
+
if (!block && toolCall.id) block = toolCallBlocksById.get(toolCall.id);
|
|
2355
|
+
if (!block) {
|
|
2356
|
+
if (currentBlock?.type === "toolCall") {
|
|
2357
|
+
currentBlock = null;
|
|
2358
|
+
flushPendingPostToolCallDeltas();
|
|
2359
|
+
}
|
|
2360
|
+
const initialSig = extractGoogleThoughtSignature(toolCall);
|
|
2361
|
+
block = {
|
|
2362
|
+
type: "toolCall",
|
|
2363
|
+
id: toolCall.id || "",
|
|
2364
|
+
name: toolCall.function?.name || "",
|
|
2365
|
+
arguments: {},
|
|
2366
|
+
partialArgs: "",
|
|
2367
|
+
...initialSig ? { thoughtSignature: initialSig } : {}
|
|
2368
|
+
};
|
|
2369
|
+
output.content.push(block);
|
|
2370
|
+
toolCallBlockIndices.set(block, output.content.length - 1);
|
|
2371
|
+
pushStreamEvent({
|
|
2372
|
+
type: "toolcall_start",
|
|
2373
|
+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
|
|
2374
|
+
partial: output
|
|
2375
|
+
});
|
|
2376
|
+
}
|
|
2377
|
+
if (streamIndex !== void 0 && !toolCallBlocksByIndex.has(streamIndex)) toolCallBlocksByIndex.set(streamIndex, block);
|
|
2378
|
+
if (toolCall.id) {
|
|
2379
|
+
block.id = toolCall.id;
|
|
2380
|
+
toolCallBlocksById.set(toolCall.id, block);
|
|
2381
|
+
}
|
|
2382
|
+
currentBlock = block;
|
|
2383
|
+
if (toolCall.function?.name) block.name = toolCall.function.name;
|
|
2384
|
+
const deltaSig = extractGoogleThoughtSignature(toolCall);
|
|
2385
|
+
if (deltaSig) block.thoughtSignature = deltaSig;
|
|
2386
|
+
if (toolCall.function?.arguments) {
|
|
2387
|
+
const nextArgumentBytes = measureUtf8Bytes(toolCall.function.arguments);
|
|
2388
|
+
const currentBlockArgBytes = toolCallBlockBytes.get(block) ?? 0;
|
|
2389
|
+
if (currentBlockArgBytes + nextArgumentBytes > MAX_TOOL_CALL_ARGUMENT_BUFFER_BYTES) throw new Error("Exceeded tool-call argument buffer limit");
|
|
2390
|
+
toolCallBlockBytes.set(block, currentBlockArgBytes + nextArgumentBytes);
|
|
2391
|
+
block.partialArgs += toolCall.function.arguments;
|
|
2392
|
+
block.arguments = parseStreamingJson(block.partialArgs);
|
|
2393
|
+
pushStreamEvent({
|
|
2394
|
+
type: "toolcall_delta",
|
|
2395
|
+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
|
|
2396
|
+
delta: toolCall.function.arguments,
|
|
2397
|
+
partial: output
|
|
2398
|
+
});
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
flushPendingPostToolCallDeltas();
|
|
2403
|
+
emitReasoningUsageActivity(hasReasoningUsageActivity);
|
|
2404
|
+
await cooperativeScheduler.afterEvent();
|
|
2405
|
+
}
|
|
2406
|
+
flushReasoningTagTextPartitionerAtEnd();
|
|
2407
|
+
flushDeepSeekToolCallRecovererAtEnd();
|
|
2408
|
+
flushDeepSeekTextFilterAtEnd();
|
|
2409
|
+
currentBlock = null;
|
|
2410
|
+
flushPendingPostToolCallDeltas();
|
|
2411
|
+
const hasToolCalls = output.content.some((block) => block.type === "toolCall");
|
|
2412
|
+
const hasVisibleText = output.content.some((block) => block.type === "text" && typeof block.text === "string" && block.text.trim().length > 0);
|
|
2413
|
+
if (output.stopReason === "toolUse" && !hasToolCalls) output.stopReason = "stop";
|
|
2414
|
+
if (output.stopReason === "stop" && hasToolCalls && !hasVisibleText && (sawStopFinishReason || sawNativeToolCallDelta && (options?.sawStreamDONE?.() ?? false))) output.stopReason = "toolUse";
|
|
2415
|
+
if (hasToolCalls && output.stopReason !== "toolUse") output.content = output.content.filter((block) => block.type !== "toolCall");
|
|
2416
|
+
if (output.stopReason !== "toolUse") clearPendingCommentaryText(provisionalCommentaryTags);
|
|
2417
|
+
if (output.stopReason === "toolUse") tagPendingCommentaryText(output.content);
|
|
2418
|
+
}
|
|
2419
|
+
function shouldFilterDeepSeekDsmlText(compat) {
|
|
2420
|
+
return compat.thinkingFormat === "deepseek";
|
|
2421
|
+
}
|
|
2422
|
+
const DEEPSEEK_DSML_BARS = ["|", "|"];
|
|
2423
|
+
const DEEPSEEK_DSML_TOOL_KINDS = [
|
|
2424
|
+
"tool_calls",
|
|
2425
|
+
"tool_call",
|
|
2426
|
+
"function_calls"
|
|
2427
|
+
];
|
|
2428
|
+
const DEEPSEEK_DSML_TOOL_OPEN_TOKENS = DEEPSEEK_DSML_BARS.flatMap((bar) => DEEPSEEK_DSML_TOOL_KINDS.map((kind) => `<${bar}DSML${bar}${kind}>`));
|
|
2429
|
+
const DEEPSEEK_DSML_TOOL_CLOSE_TOKENS = DEEPSEEK_DSML_BARS.flatMap((bar) => DEEPSEEK_DSML_TOOL_KINDS.map((kind) => `</${bar}DSML${bar}${kind}>`));
|
|
2430
|
+
const DEEPSEEK_DSML_TOOL_MAX_OPEN_TOKEN_LEN = Math.max(...DEEPSEEK_DSML_TOOL_OPEN_TOKENS.map((token) => token.length));
|
|
2431
|
+
function createDeepSeekDsmlToolCallRecoverer() {
|
|
2432
|
+
let buffer = "";
|
|
2433
|
+
const consume = (final) => {
|
|
2434
|
+
const output = [];
|
|
2435
|
+
while (buffer) {
|
|
2436
|
+
const open = findEarliestStringToken(buffer, DEEPSEEK_DSML_TOOL_OPEN_TOKENS);
|
|
2437
|
+
if (!open) {
|
|
2438
|
+
if (final) {
|
|
2439
|
+
output.push({
|
|
2440
|
+
kind: "text",
|
|
2441
|
+
text: buffer
|
|
2442
|
+
});
|
|
2443
|
+
buffer = "";
|
|
2444
|
+
return output;
|
|
2445
|
+
}
|
|
2446
|
+
const keep = longestDeepSeekDsmlToolOpenPrefixSuffixLength(buffer);
|
|
2447
|
+
const emitLength = buffer.length - keep;
|
|
2448
|
+
if (emitLength > 0) {
|
|
2449
|
+
output.push({
|
|
2450
|
+
kind: "text",
|
|
2451
|
+
text: buffer.slice(0, emitLength)
|
|
2452
|
+
});
|
|
2453
|
+
buffer = buffer.slice(emitLength);
|
|
2454
|
+
}
|
|
2455
|
+
return output;
|
|
2456
|
+
}
|
|
2457
|
+
if (open.index > 0) {
|
|
2458
|
+
output.push({
|
|
2459
|
+
kind: "text",
|
|
2460
|
+
text: buffer.slice(0, open.index)
|
|
2461
|
+
});
|
|
2462
|
+
buffer = buffer.slice(open.index);
|
|
2463
|
+
}
|
|
2464
|
+
const afterOpen = buffer.slice(open.token.length);
|
|
2465
|
+
const close = findEarliestStringToken(afterOpen, DEEPSEEK_DSML_TOOL_CLOSE_TOKENS);
|
|
2466
|
+
if (!close) {
|
|
2467
|
+
if (final) {
|
|
2468
|
+
output.push({
|
|
2469
|
+
kind: "text",
|
|
2470
|
+
text: buffer
|
|
2471
|
+
});
|
|
2472
|
+
buffer = "";
|
|
2473
|
+
}
|
|
2474
|
+
return output;
|
|
2475
|
+
}
|
|
2476
|
+
const body = afterOpen.slice(0, close.index);
|
|
2477
|
+
const blockLength = open.token.length + close.index + close.token.length;
|
|
2478
|
+
const recoveredToolCalls = parseDeepSeekDsmlToolCallBlock(body);
|
|
2479
|
+
if (recoveredToolCalls.length > 0) output.push(...recoveredToolCalls);
|
|
2480
|
+
else output.push({
|
|
2481
|
+
kind: "text",
|
|
2482
|
+
text: buffer.slice(0, blockLength)
|
|
2483
|
+
});
|
|
2484
|
+
buffer = buffer.slice(blockLength);
|
|
2485
|
+
}
|
|
2486
|
+
return output;
|
|
2487
|
+
};
|
|
2488
|
+
return {
|
|
2489
|
+
push(chunk) {
|
|
2490
|
+
buffer += chunk;
|
|
2491
|
+
return consume(false);
|
|
2492
|
+
},
|
|
2493
|
+
flush() {
|
|
2494
|
+
return consume(true);
|
|
2495
|
+
}
|
|
2496
|
+
};
|
|
2497
|
+
}
|
|
2498
|
+
function parseDeepSeekDsmlToolCallBlock(body) {
|
|
2499
|
+
const toolCalls = [];
|
|
2500
|
+
const invokeOpenRegex = /<[||]DSML[||]invoke\b([^>]*)>/g;
|
|
2501
|
+
let openMatch;
|
|
2502
|
+
while ((openMatch = invokeOpenRegex.exec(body)) !== null) {
|
|
2503
|
+
const invokeName = parseXmlAttribute(openMatch[1] ?? "", "name");
|
|
2504
|
+
if (!invokeName) continue;
|
|
2505
|
+
const invokeBodyStart = openMatch.index + openMatch[0].length;
|
|
2506
|
+
const invokeClose = findEarliestStringToken(body.slice(invokeBodyStart), ["</|DSML|invoke>", "</|DSML|invoke>"]);
|
|
2507
|
+
if (!invokeClose) continue;
|
|
2508
|
+
const invokeBody = body.slice(invokeBodyStart, invokeBodyStart + invokeClose.index);
|
|
2509
|
+
invokeOpenRegex.lastIndex = invokeBodyStart + invokeClose.index + invokeClose.token.length;
|
|
2510
|
+
const parsedArguments = parseDeepSeekDsmlInvokeArguments(invokeBody);
|
|
2511
|
+
if (!parsedArguments) continue;
|
|
2512
|
+
toolCalls.push({
|
|
2513
|
+
kind: "toolCall",
|
|
2514
|
+
name: invokeName,
|
|
2515
|
+
arguments: parsedArguments,
|
|
2516
|
+
partialArgs: JSON.stringify(parsedArguments)
|
|
2517
|
+
});
|
|
2518
|
+
}
|
|
2519
|
+
return toolCalls;
|
|
2520
|
+
}
|
|
2521
|
+
function parseDeepSeekDsmlInvokeArguments(body) {
|
|
2522
|
+
const args = {};
|
|
2523
|
+
const parameterRegex = /<[||]DSML[||]parameter\b([^>]*)>([\s\S]*?)<\/[||]DSML[||]parameter>/g;
|
|
2524
|
+
let parameterMatch;
|
|
2525
|
+
while ((parameterMatch = parameterRegex.exec(body)) !== null) {
|
|
2526
|
+
const name = parseXmlAttribute(parameterMatch[1] ?? "", "name");
|
|
2527
|
+
if (!name) continue;
|
|
2528
|
+
const rawValue = parameterMatch[2] ?? "";
|
|
2529
|
+
if (rawValue.length === 0) continue;
|
|
2530
|
+
args[name] = decodeDeepSeekDsmlText(rawValue);
|
|
2531
|
+
}
|
|
2532
|
+
if (Object.keys(args).length > 0) return args;
|
|
2533
|
+
const trimmed = body.trim();
|
|
2534
|
+
if (!trimmed.startsWith("{")) return null;
|
|
2535
|
+
try {
|
|
2536
|
+
const parsed = JSON.parse(trimmed);
|
|
2537
|
+
if (isRecord(parsed) && Object.keys(parsed).length > 0) return parsed;
|
|
2538
|
+
} catch {
|
|
2539
|
+
return null;
|
|
2540
|
+
}
|
|
2541
|
+
return null;
|
|
2542
|
+
}
|
|
2543
|
+
const xmlAttributeRegexCache = /* @__PURE__ */ new Map();
|
|
2544
|
+
function xmlAttributeRegex(name) {
|
|
2545
|
+
const cached = xmlAttributeRegexCache.get(name);
|
|
2546
|
+
if (cached) return cached;
|
|
2547
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2548
|
+
const pattern = new RegExp(`\\b${escaped}=("([^"]*)"|'([^']*)'|([^\\s>]+))`);
|
|
2549
|
+
xmlAttributeRegexCache.set(name, pattern);
|
|
2550
|
+
return pattern;
|
|
2551
|
+
}
|
|
2552
|
+
function parseXmlAttribute(attributes, name) {
|
|
2553
|
+
const match = xmlAttributeRegex(name).exec(attributes);
|
|
2554
|
+
const value = match?.[2] ?? match?.[3] ?? match?.[4];
|
|
2555
|
+
return value ? decodeDeepSeekDsmlText(value) : null;
|
|
2556
|
+
}
|
|
2557
|
+
function decodeDeepSeekDsmlText(value) {
|
|
2558
|
+
return value.replaceAll(""", "\"").replaceAll("'", "'").replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&");
|
|
2559
|
+
}
|
|
2560
|
+
function findEarliestStringToken(text, tokens) {
|
|
2561
|
+
let best = null;
|
|
2562
|
+
for (const token of tokens) {
|
|
2563
|
+
const index = text.indexOf(token);
|
|
2564
|
+
if (index !== -1 && (!best || index < best.index)) best = {
|
|
2565
|
+
index,
|
|
2566
|
+
token
|
|
2567
|
+
};
|
|
2568
|
+
}
|
|
2569
|
+
return best;
|
|
2570
|
+
}
|
|
2571
|
+
function longestDeepSeekDsmlToolOpenPrefixSuffixLength(text) {
|
|
2572
|
+
const maxLength = Math.min(text.length, DEEPSEEK_DSML_TOOL_MAX_OPEN_TOKEN_LEN - 1);
|
|
2573
|
+
for (let length = maxLength; length > 0; length -= 1) {
|
|
2574
|
+
const suffix = text.slice(text.length - length);
|
|
2575
|
+
if (DEEPSEEK_DSML_TOOL_OPEN_TOKENS.some((token) => token.startsWith(suffix))) return length;
|
|
2576
|
+
}
|
|
2577
|
+
return 0;
|
|
2578
|
+
}
|
|
2579
|
+
function getCompletionsContentDeltas(content) {
|
|
2580
|
+
if (typeof content === "string") return content ? [{
|
|
2581
|
+
kind: "text",
|
|
2582
|
+
text: content
|
|
2583
|
+
}] : [];
|
|
2584
|
+
if (Array.isArray(content)) return content.flatMap((item) => getCompletionsContentDeltas(item));
|
|
2585
|
+
if (!content || typeof content !== "object") return [];
|
|
2586
|
+
const record = content;
|
|
2587
|
+
const type = typeof record.type === "string" ? record.type.toLowerCase() : "";
|
|
2588
|
+
const extractText = (value) => {
|
|
2589
|
+
if (typeof value === "string") return value;
|
|
2590
|
+
if (Array.isArray(value)) return value.map((item) => extractText(item)).join("");
|
|
2591
|
+
if (value && typeof value === "object") {
|
|
2592
|
+
const nested = value;
|
|
2593
|
+
return extractText(nested.text ?? nested.content ?? nested.thinking);
|
|
2594
|
+
}
|
|
2595
|
+
return "";
|
|
2596
|
+
};
|
|
2597
|
+
const text = extractText(record.text ?? record.content ?? record.thinking);
|
|
2598
|
+
if (!text) return [];
|
|
2599
|
+
if (type.includes("thinking") || type.includes("reasoning")) return [{
|
|
2600
|
+
kind: "thinking",
|
|
2601
|
+
signature: "content",
|
|
2602
|
+
text
|
|
2603
|
+
}];
|
|
2604
|
+
if (type === "text" || type === "output_text" || type.endsWith(".output_text")) return [{
|
|
2605
|
+
kind: "text",
|
|
2606
|
+
text
|
|
2607
|
+
}];
|
|
2608
|
+
return [];
|
|
2609
|
+
}
|
|
2610
|
+
function getCompletionsReasoningDeltas(delta, visibleReasoningDetailTypes) {
|
|
2611
|
+
const output = [];
|
|
2612
|
+
const pushDelta = (next) => {
|
|
2613
|
+
const previous = output[output.length - 1];
|
|
2614
|
+
if (!previous || previous.kind !== next.kind) {
|
|
2615
|
+
output.push(next);
|
|
2616
|
+
return;
|
|
2617
|
+
}
|
|
2618
|
+
if (next.kind === "thinking" && previous.kind === "thinking") {
|
|
2619
|
+
if (previous.signature !== next.signature) {
|
|
2620
|
+
output.push(next);
|
|
2621
|
+
return;
|
|
2622
|
+
}
|
|
2623
|
+
previous.text += next.text;
|
|
2624
|
+
return;
|
|
2625
|
+
}
|
|
2626
|
+
previous.text += next.text;
|
|
2627
|
+
};
|
|
2628
|
+
const reasoningDetails = delta.reasoning_details;
|
|
2629
|
+
let usedReasoningThinkingDetails = false;
|
|
2630
|
+
if (Array.isArray(reasoningDetails)) {
|
|
2631
|
+
const visibleTypes = new Set(visibleReasoningDetailTypes);
|
|
2632
|
+
for (const item of reasoningDetails) {
|
|
2633
|
+
const detail = item;
|
|
2634
|
+
if (typeof detail.text !== "string" || !detail.text) continue;
|
|
2635
|
+
if (detail.type === "reasoning.text") {
|
|
2636
|
+
usedReasoningThinkingDetails = true;
|
|
2637
|
+
pushDelta({
|
|
2638
|
+
kind: "thinking",
|
|
2639
|
+
signature: "reasoning_details",
|
|
2640
|
+
text: detail.text
|
|
2641
|
+
});
|
|
2642
|
+
continue;
|
|
2643
|
+
}
|
|
2644
|
+
if (typeof detail.type === "string" && visibleTypes.has(detail.type)) pushDelta({
|
|
2645
|
+
kind: "text",
|
|
2646
|
+
text: detail.text
|
|
2647
|
+
});
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
if (!usedReasoningThinkingDetails) for (const field of [
|
|
2651
|
+
"reasoning_content",
|
|
2652
|
+
"reasoning",
|
|
2653
|
+
"reasoning_text"
|
|
2654
|
+
]) {
|
|
2655
|
+
const value = delta[field];
|
|
2656
|
+
if (typeof value === "string" && value.length > 0) {
|
|
2657
|
+
pushDelta({
|
|
2658
|
+
kind: "thinking",
|
|
2659
|
+
signature: field,
|
|
2660
|
+
text: value
|
|
2661
|
+
});
|
|
2662
|
+
break;
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
return output;
|
|
2666
|
+
}
|
|
2667
|
+
function resolveOpenAICompletionsReasoningEffort(options) {
|
|
2668
|
+
return options?.reasoningEffort ?? options?.reasoning ?? "high";
|
|
2669
|
+
}
|
|
2670
|
+
function shouldEmitOpenAICompletionsReasoning(model, options) {
|
|
2671
|
+
if (!model.reasoning) return false;
|
|
2672
|
+
const effort = resolveOpenAICompletionsReasoningEffort(options);
|
|
2673
|
+
if (!effort || !isOpenAICompletionsThinkingEnabled(effort)) return false;
|
|
2674
|
+
return true;
|
|
2675
|
+
}
|
|
2676
|
+
function shouldEmitOpenAICompletionsReasoningForModel(model, options) {
|
|
2677
|
+
return shouldEmitOpenAICompletionsReasoning(model, options);
|
|
2678
|
+
}
|
|
2679
|
+
function resolveOpenAICompletionsMaxTokens(model, options) {
|
|
2680
|
+
if (options?.maxTokens) return {
|
|
2681
|
+
maxTokens: options.maxTokens,
|
|
2682
|
+
clampToModelMaxTokens: true
|
|
2683
|
+
};
|
|
2684
|
+
const paramsMaxTokens = resolveMaxTokensParam(model.params);
|
|
2685
|
+
if (paramsMaxTokens) return {
|
|
2686
|
+
maxTokens: paramsMaxTokens,
|
|
2687
|
+
clampToModelMaxTokens: false
|
|
2688
|
+
};
|
|
2689
|
+
return {
|
|
2690
|
+
maxTokens: model.maxTokens,
|
|
2691
|
+
clampToModelMaxTokens: false
|
|
2692
|
+
};
|
|
2693
|
+
}
|
|
2694
|
+
function resolveOpenAICompletionsModelMaxTokens(model) {
|
|
2695
|
+
return typeof model.maxTokens === "number" && Number.isFinite(model.maxTokens) && model.maxTokens > 0 ? Math.floor(model.maxTokens) : void 0;
|
|
2696
|
+
}
|
|
2697
|
+
const OPENAI_COMPLETIONS_INPUT_TOKEN_SAFETY_MARGIN = 1.25;
|
|
2698
|
+
const OPENAI_COMPLETIONS_IMAGE_CHAR_ESTIMATE = 8e3;
|
|
2699
|
+
function estimateOpenAICompletionsInputTokens(payload) {
|
|
2700
|
+
let adjustedChars = 0;
|
|
2701
|
+
adjustedChars += estimateOpenAICompletionsMessagesChars(payload.messages);
|
|
2702
|
+
if (Array.isArray(payload.tools) && payload.tools.length > 0) try {
|
|
2703
|
+
adjustedChars += estimateStringChars(JSON.stringify(payload.tools));
|
|
2704
|
+
} catch {
|
|
2705
|
+
adjustedChars += 1024;
|
|
2706
|
+
}
|
|
2707
|
+
if (payload.response_format !== void 0) try {
|
|
2708
|
+
adjustedChars += estimateStringChars(JSON.stringify(payload.response_format));
|
|
2709
|
+
} catch {
|
|
2710
|
+
adjustedChars += 256;
|
|
2711
|
+
}
|
|
2712
|
+
return Math.ceil(adjustedChars / 4 * OPENAI_COMPLETIONS_INPUT_TOKEN_SAFETY_MARGIN);
|
|
2713
|
+
}
|
|
2714
|
+
function estimateOpenAICompletionsMessagesChars(messages) {
|
|
2715
|
+
if (!Array.isArray(messages)) return 0;
|
|
2716
|
+
let adjustedChars = 0;
|
|
2717
|
+
for (const message of messages) {
|
|
2718
|
+
if (!message || typeof message !== "object") continue;
|
|
2719
|
+
const record = message;
|
|
2720
|
+
adjustedChars += estimateOpenAICompletionsContentChars(record.content);
|
|
2721
|
+
for (const field of COMPLETIONS_REASONING_REPLAY_FIELDS) adjustedChars += estimateOpenAICompletionsContentChars(record[field]);
|
|
2722
|
+
if (record.tool_calls !== void 0) try {
|
|
2723
|
+
adjustedChars += estimateStringChars(JSON.stringify(record.tool_calls));
|
|
2724
|
+
} catch {
|
|
2725
|
+
adjustedChars += 256;
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
return adjustedChars;
|
|
2729
|
+
}
|
|
2730
|
+
function estimateOpenAICompletionsContentChars(value) {
|
|
2731
|
+
if (typeof value === "string") return estimateStringChars(value);
|
|
2732
|
+
if (!Array.isArray(value)) return 0;
|
|
2733
|
+
let adjustedChars = 0;
|
|
2734
|
+
for (const block of value) {
|
|
2735
|
+
if (!block || typeof block !== "object") continue;
|
|
2736
|
+
const record = block;
|
|
2737
|
+
if (record.type === "image_url" || record.type === "input_image") {
|
|
2738
|
+
adjustedChars += OPENAI_COMPLETIONS_IMAGE_CHAR_ESTIMATE;
|
|
2739
|
+
continue;
|
|
2740
|
+
}
|
|
2741
|
+
const text = record.text;
|
|
2742
|
+
if (typeof text === "string") {
|
|
2743
|
+
adjustedChars += estimateStringChars(text);
|
|
2744
|
+
continue;
|
|
2745
|
+
}
|
|
2746
|
+
try {
|
|
2747
|
+
adjustedChars += estimateStringChars(JSON.stringify(block));
|
|
2748
|
+
} catch {
|
|
2749
|
+
adjustedChars += 256;
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
return adjustedChars;
|
|
2753
|
+
}
|
|
2754
|
+
function resolveOpenAICompletionsEffectiveContextTokens(model) {
|
|
2755
|
+
const contextTokens = model.contextTokens;
|
|
2756
|
+
if (typeof contextTokens === "number" && Number.isFinite(contextTokens) && contextTokens > 0) return contextTokens;
|
|
2757
|
+
return typeof model.contextWindow === "number" && Number.isFinite(model.contextWindow) && model.contextWindow > 0 ? model.contextWindow : void 0;
|
|
2758
|
+
}
|
|
2759
|
+
function isQwenOpenAICompletionsThinkingFormat(format) {
|
|
2760
|
+
return format === "qwen" || format === "qwen-chat-template";
|
|
2761
|
+
}
|
|
2762
|
+
function isOpenAICompletionsThinkingEnabled(effort) {
|
|
2763
|
+
const normalized = effort.trim().toLowerCase();
|
|
2764
|
+
return normalized !== "off" && normalized !== "none";
|
|
2765
|
+
}
|
|
2766
|
+
function setQwenChatTemplateThinking(params, enabled) {
|
|
2767
|
+
const existing = params.chat_template_kwargs;
|
|
2768
|
+
params.chat_template_kwargs = existing && typeof existing === "object" && !Array.isArray(existing) ? {
|
|
2769
|
+
...existing,
|
|
2770
|
+
enable_thinking: enabled
|
|
2771
|
+
} : { enable_thinking: enabled };
|
|
2772
|
+
}
|
|
2773
|
+
function applyQwenOpenAICompletionsThinkingParams(params) {
|
|
2774
|
+
if (!params.modelReasoning || !isQwenOpenAICompletionsThinkingFormat(params.compatThinkingFormat)) return false;
|
|
2775
|
+
const enabled = isOpenAICompletionsThinkingEnabled(params.requestedEffort);
|
|
2776
|
+
if (params.compatThinkingFormat === "qwen-chat-template") setQwenChatTemplateThinking(params.payload, enabled);
|
|
2777
|
+
else params.payload.enable_thinking = enabled;
|
|
2778
|
+
return true;
|
|
2779
|
+
}
|
|
2780
|
+
function applyTogetherOpenAICompletionsThinkingParams(params) {
|
|
2781
|
+
if (!params.modelReasoning || params.compatThinkingFormat !== "together") return false;
|
|
2782
|
+
params.payload.reasoning = { enabled: isOpenAICompletionsThinkingEnabled(params.requestedEffort) };
|
|
2783
|
+
return true;
|
|
2784
|
+
}
|
|
2785
|
+
function convertTools(tools, compat, model) {
|
|
2786
|
+
const projection = projectOpenAITools(tools);
|
|
2787
|
+
const strict = resolveOpenAIStrictToolFlagWithDiagnostics(projection, resolveOpenAIStrictToolSetting(model, {
|
|
2788
|
+
transport: "stream",
|
|
2789
|
+
supportsStrictMode: compat?.supportsStrictMode
|
|
2790
|
+
}), {
|
|
2791
|
+
transport: "completions",
|
|
2792
|
+
model
|
|
2793
|
+
});
|
|
2794
|
+
return {
|
|
2795
|
+
projection,
|
|
2796
|
+
tools: sortPromptCacheToolsByName(projection.tools).map((tool) => {
|
|
2797
|
+
const functionTool = {
|
|
2798
|
+
name: tool.name,
|
|
2799
|
+
description: tool.description,
|
|
2800
|
+
parameters: normalizeOpenAIStrictToolParameters(tool.parameters, strict === true, model.compat)
|
|
2801
|
+
};
|
|
2802
|
+
if (strict !== void 0) functionTool.strict = strict;
|
|
2803
|
+
return {
|
|
2804
|
+
type: "function",
|
|
2805
|
+
function: functionTool
|
|
2806
|
+
};
|
|
2807
|
+
})
|
|
2808
|
+
};
|
|
2809
|
+
}
|
|
2810
|
+
function extractGoogleThoughtSignature(toolCall) {
|
|
2811
|
+
const tc = toolCall;
|
|
2812
|
+
if (!tc) return;
|
|
2813
|
+
const fromExtra = (tc.extra_content?.google)?.thought_signature;
|
|
2814
|
+
if (typeof fromExtra === "string" && fromExtra.length > 0) return fromExtra;
|
|
2815
|
+
const fromFunction = tc.function?.thought_signature;
|
|
2816
|
+
return typeof fromFunction === "string" && fromFunction.length > 0 ? fromFunction : void 0;
|
|
2817
|
+
}
|
|
2818
|
+
function isGoogleOpenAICompatModel(model) {
|
|
2819
|
+
const endpointClass = detectOpenAICompletionsCompat(model).capabilities.endpointClass;
|
|
2820
|
+
return model.provider === "google" || endpointClass === "google-generative-ai" || endpointClass === "google-vertex";
|
|
2821
|
+
}
|
|
2822
|
+
function requiresGoogleCompatToolCallThoughtSignature(model) {
|
|
2823
|
+
return isGoogleGemini3ProModel(model.id) || isGoogleGemini3FlashModel(model.id);
|
|
2824
|
+
}
|
|
2825
|
+
const GOOGLE_COMPAT_THOUGHT_SIGNATURE_ELLIPSIS_RE = /[\u2026]|\.\.\./;
|
|
2826
|
+
const GOOGLE_COMPAT_THOUGHT_SIGNATURE_BASE64_RE = /^[A-Za-z0-9+/=]+$/;
|
|
2827
|
+
function hasGoogleCompatThoughtSignatureTruncationFootprint(value) {
|
|
2828
|
+
return GOOGLE_COMPAT_THOUGHT_SIGNATURE_ELLIPSIS_RE.test(value) || GOOGLE_COMPAT_THOUGHT_SIGNATURE_BASE64_RE.test(value) && value.length % 4 !== 0;
|
|
2829
|
+
}
|
|
2830
|
+
function injectToolCallThoughtSignatures(outgoingMessages, context, model) {
|
|
2831
|
+
if (!isGoogleOpenAICompatModel(model)) return;
|
|
2832
|
+
const sigById = /* @__PURE__ */ new Map();
|
|
2833
|
+
const fallbackSig = requiresGoogleCompatToolCallThoughtSignature(model) ? GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP : void 0;
|
|
2834
|
+
for (const msg of context.messages ?? []) {
|
|
2835
|
+
if (msg.role !== "assistant") continue;
|
|
2836
|
+
const source = msg;
|
|
2837
|
+
if (!Array.isArray(source.content)) continue;
|
|
2838
|
+
for (const block of source.content) {
|
|
2839
|
+
if (block.type !== "toolCall") continue;
|
|
2840
|
+
const id = block.id;
|
|
2841
|
+
const sig = block.thoughtSignature;
|
|
2842
|
+
if (typeof id === "string" && typeof sig === "string" && sig.length > 0) {
|
|
2843
|
+
const isSameRoute = source.api === model.api && source.provider === model.provider && source.model === model.id;
|
|
2844
|
+
if (!isSameRoute && !fallbackSig) continue;
|
|
2845
|
+
sigById.set(id, isSameRoute ? sig : fallbackSig ?? sig);
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
}
|
|
2849
|
+
if (sigById.size === 0 && !fallbackSig) return;
|
|
2850
|
+
for (const message of outgoingMessages) {
|
|
2851
|
+
const toolCalls = message.tool_calls;
|
|
2852
|
+
if (!Array.isArray(toolCalls)) continue;
|
|
2853
|
+
for (const toolCall of toolCalls) {
|
|
2854
|
+
const id = toolCall.id;
|
|
2855
|
+
if (typeof id !== "string") continue;
|
|
2856
|
+
let sig = sigById.get(id) ?? fallbackSig;
|
|
2857
|
+
if (typeof sig === "string" && sig.length > 0) {
|
|
2858
|
+
if (hasGoogleCompatThoughtSignatureTruncationFootprint(sig.trim())) sig = fallbackSig;
|
|
2859
|
+
}
|
|
2860
|
+
if (typeof sig !== "string" || sig.length === 0) continue;
|
|
2861
|
+
const extra = toolCall.extra_content && typeof toolCall.extra_content === "object" ? toolCall.extra_content : {};
|
|
2862
|
+
toolCall.extra_content = extra;
|
|
2863
|
+
const google = extra.google && typeof extra.google === "object" ? extra.google : {};
|
|
2864
|
+
extra.google = google;
|
|
2865
|
+
google.thought_signature = sig;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
const COMPLETIONS_REASONING_REPLAY_FIELDS = [
|
|
2870
|
+
"reasoning_details",
|
|
2871
|
+
"reasoning_content",
|
|
2872
|
+
"reasoning",
|
|
2873
|
+
"reasoning_text"
|
|
2874
|
+
];
|
|
2875
|
+
function stripCompletionsReasoningReplayFields(record) {
|
|
2876
|
+
for (const field of COMPLETIONS_REASONING_REPLAY_FIELDS) if (field in record) delete record[field];
|
|
2877
|
+
}
|
|
2878
|
+
function sanitizeOpenRouterReasoningReplayFields(record) {
|
|
2879
|
+
const reasoningDetails = record.reasoning_details;
|
|
2880
|
+
if (typeof reasoningDetails === "string") {
|
|
2881
|
+
if (reasoningDetails.length > 0 && typeof record.reasoning !== "string") record.reasoning = reasoningDetails;
|
|
2882
|
+
delete record.reasoning_details;
|
|
2883
|
+
} else if (reasoningDetails !== void 0 && !Array.isArray(reasoningDetails)) delete record.reasoning_details;
|
|
2884
|
+
if ("reasoning" in record && (typeof record.reasoning !== "string" || record.reasoning === "")) delete record.reasoning;
|
|
2885
|
+
if ("reasoning_content" in record && (typeof record.reasoning_content !== "string" || record.reasoning_content === "")) delete record.reasoning_content;
|
|
2886
|
+
const reasoningText = record.reasoning_text;
|
|
2887
|
+
if (typeof reasoningText === "string" && reasoningText.length > 0 && typeof record.reasoning !== "string" && typeof record.reasoning_content !== "string") record.reasoning = reasoningText;
|
|
2888
|
+
if ("reasoning_text" in record) delete record.reasoning_text;
|
|
2889
|
+
}
|
|
2890
|
+
function sanitizeReasoningContentReplayFields(record) {
|
|
2891
|
+
if ("reasoning_content" in record && typeof record.reasoning_content !== "string") delete record.reasoning_content;
|
|
2892
|
+
delete record.reasoning_details;
|
|
2893
|
+
delete record.reasoning;
|
|
2894
|
+
delete record.reasoning_text;
|
|
2895
|
+
}
|
|
2896
|
+
const REASONING_CONTENT_REPLAY_MODEL_IDS = /* @__PURE__ */ new Set([
|
|
2897
|
+
"deepseek-v4-flash",
|
|
2898
|
+
"deepseek-v4-pro",
|
|
2899
|
+
"kimi-for-coding",
|
|
2900
|
+
"kimi-k2.5",
|
|
2901
|
+
"kimi-k2.6",
|
|
2902
|
+
"kimi-k2.7-code",
|
|
2903
|
+
"kimi-k2.7-code-highspeed",
|
|
2904
|
+
"kimi-k3",
|
|
2905
|
+
"kimi-k2-thinking",
|
|
2906
|
+
"kimi-k2-thinking-turbo",
|
|
2907
|
+
"mimo-v2-pro",
|
|
2908
|
+
"mimo-v2-omni",
|
|
2909
|
+
"mimo-v2.5",
|
|
2910
|
+
"mimo-v2.5-pro",
|
|
2911
|
+
"mimo-v2.6-pro"
|
|
2912
|
+
]);
|
|
2913
|
+
const REASONING_CONTENT_REPLAY_TIER_SUFFIXES = [
|
|
2914
|
+
"-free",
|
|
2915
|
+
"-paid",
|
|
2916
|
+
"-trial"
|
|
2917
|
+
];
|
|
2918
|
+
function stripReasoningContentReplayTierSuffix(modelId) {
|
|
2919
|
+
for (const suffix of REASONING_CONTENT_REPLAY_TIER_SUFFIXES) if (modelId.length > suffix.length && modelId.endsWith(suffix)) return modelId.slice(0, -suffix.length);
|
|
2920
|
+
return modelId;
|
|
2921
|
+
}
|
|
2922
|
+
function getReasoningContentReplayModelIdCandidates(modelId) {
|
|
2923
|
+
if (typeof modelId !== "string") return [];
|
|
2924
|
+
const normalized = modelId.trim().toLowerCase();
|
|
2925
|
+
if (!normalized) return [];
|
|
2926
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
2927
|
+
const finalPart = parts[parts.length - 1] ?? normalized;
|
|
2928
|
+
const candidates = [finalPart];
|
|
2929
|
+
const colonParts = finalPart.split(":").filter(Boolean);
|
|
2930
|
+
if (colonParts.length > 1) candidates.push(colonParts[0] ?? "", colonParts[colonParts.length - 1] ?? "");
|
|
2931
|
+
const baseCount = candidates.length;
|
|
2932
|
+
for (let index = 0; index < baseCount; index += 1) {
|
|
2933
|
+
const candidate = candidates[index];
|
|
2934
|
+
if (typeof candidate !== "string") continue;
|
|
2935
|
+
const stripped = stripReasoningContentReplayTierSuffix(candidate);
|
|
2936
|
+
if (stripped !== candidate) candidates.push(stripped);
|
|
2937
|
+
}
|
|
2938
|
+
return uniqueStrings(candidates.filter(Boolean));
|
|
2939
|
+
}
|
|
2940
|
+
function shouldPreserveReasoningContentReplay(model, compat) {
|
|
2941
|
+
if (compat.requiresReasoningContentOnAssistantMessages || compat.thinkingFormat === "deepseek" || compat.thinkingFormat === "zai" || shouldTrustReasoningContentReplayMetadata(model)) return true;
|
|
2942
|
+
return getReasoningContentReplayModelIdCandidates(model.id).some((modelId) => REASONING_CONTENT_REPLAY_MODEL_IDS.has(modelId));
|
|
2943
|
+
}
|
|
2944
|
+
function shouldPreserveOpenRouterReasoningReplay(model) {
|
|
2945
|
+
if (model.provider !== "openrouter") return true;
|
|
2946
|
+
const normalizedModelId = model.id.trim().toLowerCase();
|
|
2947
|
+
return !(normalizedModelId.startsWith("anthropic/") || normalizedModelId.startsWith("x-ai/"));
|
|
2948
|
+
}
|
|
2949
|
+
function shouldTrustReasoningContentReplayMetadata(model) {
|
|
2950
|
+
if (!model.reasoning) return false;
|
|
2951
|
+
if (model.provider.trim().toLowerCase() === "openai") return false;
|
|
2952
|
+
return shouldPreserveOpenRouterReasoningReplay(model);
|
|
2953
|
+
}
|
|
2954
|
+
function sanitizeCompletionsReasoningReplayFields(messages, options) {
|
|
2955
|
+
if (!Array.isArray(messages)) return;
|
|
2956
|
+
for (const msg of messages) {
|
|
2957
|
+
if (!msg || typeof msg !== "object") continue;
|
|
2958
|
+
const record = msg;
|
|
2959
|
+
if (record.role !== "assistant") continue;
|
|
2960
|
+
if (options.preserveOpenRouterReasoning) sanitizeOpenRouterReasoningReplayFields(record);
|
|
2961
|
+
else if (options.preserveReasoningContent) sanitizeReasoningContentReplayFields(record);
|
|
2962
|
+
else stripCompletionsReasoningReplayFields(record);
|
|
2963
|
+
}
|
|
2964
|
+
}
|
|
2965
|
+
function buildOpenAICompletionsParams(model, context, options) {
|
|
2966
|
+
const compat = getCompat(model);
|
|
2967
|
+
const compatDetection = detectOpenAICompletionsCompat(model);
|
|
2968
|
+
let messages = convertMessages(model, context.systemPrompt ? {
|
|
2969
|
+
...context,
|
|
2970
|
+
systemPrompt: stripSystemPromptCacheBoundary(context.systemPrompt)
|
|
2971
|
+
} : context, compat);
|
|
2972
|
+
injectToolCallThoughtSignatures(messages, context, model);
|
|
2973
|
+
sanitizeCompletionsReasoningReplayFields(messages, {
|
|
2974
|
+
preserveOpenRouterReasoning: compat.thinkingFormat === "openrouter" && shouldPreserveOpenRouterReasoningReplay(model),
|
|
2975
|
+
preserveReasoningContent: shouldPreserveReasoningContentReplay(model, compat)
|
|
2976
|
+
});
|
|
2977
|
+
if (compat.strictMessageKeys) messages = stripCompletionMessagesToRoleContent(messages);
|
|
2978
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
2979
|
+
const promptCacheKey = resolvePromptCacheKey(options, cacheRetention);
|
|
2980
|
+
const params = {
|
|
2981
|
+
model: model.id,
|
|
2982
|
+
messages: compat.requiresStringContent ? flattenCompletionMessagesToStringContent(messages) : messages,
|
|
2983
|
+
stream: true
|
|
2984
|
+
};
|
|
2985
|
+
if (compat.supportsUsageInStreaming) params.stream_options = { include_usage: true };
|
|
2986
|
+
if (compat.supportsStore) params.store = false;
|
|
2987
|
+
if (compat.supportsPromptCacheKey && promptCacheKey) {
|
|
2988
|
+
params.prompt_cache_key = promptCacheKey;
|
|
2989
|
+
if (cacheRetention === "long" && compat.supportsLongCacheRetention) params.prompt_cache_retention = "24h";
|
|
2990
|
+
}
|
|
2991
|
+
if (options?.temperature !== void 0) params.temperature = options.temperature;
|
|
2992
|
+
if (options?.topP !== void 0) params.top_p = options.topP;
|
|
2993
|
+
const responseFormat = resolveOpenAICompletionsResponseFormat(shouldOmitOllamaCompatResponseFormat({
|
|
2994
|
+
provider: model.provider,
|
|
2995
|
+
baseUrl: model.baseUrl,
|
|
2996
|
+
hasTools: () => Boolean(context.tools?.length)
|
|
2997
|
+
}) ? void 0 : options?.responseFormat, compat.supportsJsonSchemaResponseFormat);
|
|
2998
|
+
if (responseFormat !== void 0) params.response_format = responseFormat;
|
|
2999
|
+
if (options?.frequencyPenalty !== void 0) params.frequency_penalty = options.frequencyPenalty;
|
|
3000
|
+
if (options?.presencePenalty !== void 0) params.presence_penalty = options.presencePenalty;
|
|
3001
|
+
if (options?.seed !== void 0) params.seed = options.seed;
|
|
3002
|
+
if (options?.stop !== void 0 && options.stop.length > 0) params.stop = options.stop;
|
|
3003
|
+
if (supportsModelTools(model)) {
|
|
3004
|
+
if (context.tools) {
|
|
3005
|
+
const converted = convertTools(context.tools, compat, model);
|
|
3006
|
+
if (converted.tools.length > 0 || converted.projection.inputToolCount === 0 && converted.projection.diagnostics.length === 0) params.tools = converted.tools;
|
|
3007
|
+
else if (hasToolHistory(context.messages)) params.tools = [];
|
|
3008
|
+
if (options?.toolChoice) {
|
|
3009
|
+
const toolChoice = reconcileOpenAICompletionsToolChoice(options.toolChoice, converted.projection);
|
|
3010
|
+
if (toolChoice !== void 0) params.tool_choice = toolChoice;
|
|
3011
|
+
} else if (compatDetection.capabilities.usesExplicitProxyLikeEndpoint && Array.isArray(params.tools) && params.tools.length > 0) params.tool_choice = "auto";
|
|
3012
|
+
} else if (hasToolHistory(context.messages)) params.tools = [];
|
|
3013
|
+
if (compatDetection.capabilities.usesExplicitProxyLikeEndpoint && Array.isArray(params.tools) && params.tools.length === 0) {
|
|
3014
|
+
delete params.tools;
|
|
3015
|
+
delete params.tool_choice;
|
|
3016
|
+
}
|
|
3017
|
+
}
|
|
3018
|
+
{
|
|
3019
|
+
const maxTokenBudget = resolveOpenAICompletionsMaxTokens(model, options);
|
|
3020
|
+
const effectiveMaxTokens = maxTokenBudget.maxTokens;
|
|
3021
|
+
const effectiveContextTokens = resolveOpenAICompletionsEffectiveContextTokens(model);
|
|
3022
|
+
let clampedMaxTokens = effectiveMaxTokens;
|
|
3023
|
+
const modelMaxTokens = resolveOpenAICompletionsModelMaxTokens(model);
|
|
3024
|
+
if (maxTokenBudget.clampToModelMaxTokens && clampedMaxTokens !== void 0 && modelMaxTokens !== void 0 && clampedMaxTokens > modelMaxTokens) {
|
|
3025
|
+
clampedMaxTokens = modelMaxTokens;
|
|
3026
|
+
emitModelTransportDebug(log, `[completions] clamp_max_tokens provider=${model.provider} api=${model.api} model=${model.id} requested=${effectiveMaxTokens} output=${clampedMaxTokens} modelMaxTokens=${modelMaxTokens}`);
|
|
3027
|
+
}
|
|
3028
|
+
if (compatDetection.capabilities.usesExplicitProxyLikeEndpoint && clampedMaxTokens !== void 0 && effectiveContextTokens !== void 0) {
|
|
3029
|
+
const estimatedInputTokens = estimateOpenAICompletionsInputTokens(params);
|
|
3030
|
+
const remainingBudget = Math.max(1, effectiveContextTokens - estimatedInputTokens - 1);
|
|
3031
|
+
if (clampedMaxTokens > remainingBudget) {
|
|
3032
|
+
clampedMaxTokens = remainingBudget;
|
|
3033
|
+
emitModelTransportDebug(log, `[completions] clamp_max_tokens provider=${model.provider} api=${model.api} model=${model.id} requested=${effectiveMaxTokens} output=${clampedMaxTokens} effectiveContext=${effectiveContextTokens} estimatedInput=${estimatedInputTokens}`);
|
|
3034
|
+
}
|
|
3035
|
+
}
|
|
3036
|
+
if (clampedMaxTokens) if (compat.maxTokensField === "max_tokens") params.max_tokens = clampedMaxTokens;
|
|
3037
|
+
else params.max_completion_tokens = clampedMaxTokens;
|
|
3038
|
+
}
|
|
3039
|
+
const completionsReasoningEffort = resolveOpenAICompletionsReasoningEffort(options);
|
|
3040
|
+
const resolvedCompletionsReasoningEffort = completionsReasoningEffort ? resolveOpenAIReasoningEffortForModel({
|
|
3041
|
+
model,
|
|
3042
|
+
effort: completionsReasoningEffort,
|
|
3043
|
+
fallbackMap: compat.reasoningEffortMap
|
|
3044
|
+
}) : void 0;
|
|
3045
|
+
const omitChatCompletionsToolReasoningEffort = Array.isArray(params.tools) && params.tools.length > 0 && (isOpenAIGpt54MiniModel(model) || isOpenAIGpt55Model(model) && isKnownOpenAICompletionsEndpoint(model));
|
|
3046
|
+
const disableChatCompletionsToolReasoning = Array.isArray(params.tools) && params.tools.length > 0 && isOpenAIGpt56Model(model) && isKnownOpenAICompletionsEndpoint(model);
|
|
3047
|
+
const handledQwenThinkingFormat = applyQwenOpenAICompletionsThinkingParams({
|
|
3048
|
+
compatThinkingFormat: compat.thinkingFormat,
|
|
3049
|
+
modelReasoning: model.reasoning,
|
|
3050
|
+
payload: params,
|
|
3051
|
+
requestedEffort: completionsReasoningEffort
|
|
3052
|
+
});
|
|
3053
|
+
applyTogetherOpenAICompletionsThinkingParams({
|
|
3054
|
+
compatThinkingFormat: compat.thinkingFormat,
|
|
3055
|
+
modelReasoning: model.reasoning,
|
|
3056
|
+
payload: params,
|
|
3057
|
+
requestedEffort: completionsReasoningEffort
|
|
3058
|
+
});
|
|
3059
|
+
if (disableChatCompletionsToolReasoning) params.reasoning_effort = "none";
|
|
3060
|
+
else if (compat.thinkingFormat === "openrouter" && model.reasoning && resolvedCompletionsReasoningEffort) params.reasoning = { effort: resolvedCompletionsReasoningEffort };
|
|
3061
|
+
else if (resolvedCompletionsReasoningEffort && model.reasoning && compat.supportsReasoningEffort && !handledQwenThinkingFormat && !omitChatCompletionsToolReasoningEffort) params.reasoning_effort = resolvedCompletionsReasoningEffort;
|
|
3062
|
+
return params;
|
|
3063
|
+
}
|
|
3064
|
+
function parseTransportChunkUsage(rawUsage, model) {
|
|
3065
|
+
const cacheWriteTokens = rawUsage.prompt_tokens_details?.cache_write_tokens || 0;
|
|
3066
|
+
const cachedTokens = rawUsage.prompt_tokens_details?.cached_tokens || 0;
|
|
3067
|
+
const promptTokens = rawUsage.prompt_tokens || 0;
|
|
3068
|
+
const input = Math.max(0, promptTokens - cachedTokens - cacheWriteTokens);
|
|
3069
|
+
const outputTokens = rawUsage.completion_tokens || 0;
|
|
3070
|
+
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens;
|
|
3071
|
+
const usage = {
|
|
3072
|
+
input,
|
|
3073
|
+
output: outputTokens,
|
|
3074
|
+
cacheRead: cachedTokens,
|
|
3075
|
+
cacheWrite: cacheWriteTokens,
|
|
3076
|
+
...typeof reasoningTokens === "number" && Number.isFinite(reasoningTokens) ? { reasoningTokens } : {},
|
|
3077
|
+
totalTokens: input + outputTokens + cachedTokens + cacheWriteTokens,
|
|
3078
|
+
cost: {
|
|
3079
|
+
input: 0,
|
|
3080
|
+
output: 0,
|
|
3081
|
+
cacheRead: 0,
|
|
3082
|
+
cacheWrite: 0,
|
|
3083
|
+
total: 0
|
|
3084
|
+
}
|
|
3085
|
+
};
|
|
3086
|
+
calculateCost(model, usage);
|
|
3087
|
+
applyProviderReportedUsageCost(usage, rawUsage.cost);
|
|
3088
|
+
return usage;
|
|
3089
|
+
}
|
|
3090
|
+
function hasOpenAICompletionsReasoningUsageActivity(rawUsage) {
|
|
3091
|
+
const reasoningTokens = rawUsage.completion_tokens_details?.reasoning_tokens;
|
|
3092
|
+
return typeof reasoningTokens === "number" && Number.isFinite(reasoningTokens) && reasoningTokens > 0;
|
|
3093
|
+
}
|
|
3094
|
+
const completionsTesting = {
|
|
3095
|
+
getCompat,
|
|
3096
|
+
createSseDoneDetector,
|
|
3097
|
+
createOpenAICompletionsClient,
|
|
3098
|
+
buildOpenAICompletionsClientConfig,
|
|
3099
|
+
parseTransportChunkUsage,
|
|
3100
|
+
processOpenAICompletionsStream,
|
|
3101
|
+
shouldEmitOpenAICompletionsReasoningForModel
|
|
3102
|
+
};
|
|
3103
|
+
if (process.env.VITEST || false) globalThis.openclawOpenAICompletionsTransportTestApi = completionsTesting;
|
|
3104
|
+
//#endregion
|
|
3105
|
+
//#region packages/ai/src/transports/openai-responses-payload-policy.ts
|
|
3106
|
+
/**
|
|
3107
|
+
* OpenAI Responses payload policy.
|
|
3108
|
+
* Classifies endpoint capabilities and applies store, prompt-cache,
|
|
3109
|
+
* server-compaction, service-tier, and reasoning payload rules.
|
|
3110
|
+
*/
|
|
3111
|
+
const OPENAI_RESPONSES_APIS = /* @__PURE__ */ new Set([
|
|
3112
|
+
"openai-responses",
|
|
3113
|
+
"azure-openai-responses",
|
|
3114
|
+
"openai-chatgpt-responses",
|
|
3115
|
+
"openclaw-openai-responses-transport",
|
|
3116
|
+
"openclaw-openai-chatgpt-responses-transport"
|
|
3117
|
+
]);
|
|
3118
|
+
const OPENAI_RESPONSES_PROVIDERS = /* @__PURE__ */ new Set([
|
|
3119
|
+
"openai",
|
|
3120
|
+
"azure-openai",
|
|
3121
|
+
"azure-openai-responses"
|
|
3122
|
+
]);
|
|
3123
|
+
const LOCAL_ENDPOINT_HOSTS = /* @__PURE__ */ new Set([
|
|
3124
|
+
"localhost",
|
|
3125
|
+
"127.0.0.1",
|
|
3126
|
+
"::1",
|
|
3127
|
+
"[::1]"
|
|
3128
|
+
]);
|
|
3129
|
+
const MODELSTUDIO_NATIVE_BASE_URLS = /* @__PURE__ */ new Set([
|
|
3130
|
+
"https://coding-intl.dashscope.aliyuncs.com/v1",
|
|
3131
|
+
"https://coding.dashscope.aliyuncs.com/v1",
|
|
3132
|
+
"https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
3133
|
+
"https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
|
|
3134
|
+
]);
|
|
3135
|
+
const MOONSHOT_NATIVE_BASE_URLS = /* @__PURE__ */ new Set(["https://api.moonshot.ai/v1", "https://api.moonshot.cn/v1"]);
|
|
3136
|
+
function normalizeLowercaseString(value) {
|
|
3137
|
+
const stringValue = readStringValue(value)?.trim().toLowerCase();
|
|
3138
|
+
return stringValue ? stringValue : void 0;
|
|
3139
|
+
}
|
|
3140
|
+
function normalizeComparableBaseUrl(value) {
|
|
3141
|
+
const trimmed = readStringValue(value)?.trim();
|
|
3142
|
+
if (!trimmed) return;
|
|
3143
|
+
const parsedValue = /^[a-z0-9.[\]-]+(?::\d+)?(?:[/?#].*)?$/i.test(trimmed) ? `https://${trimmed}` : trimmed;
|
|
3144
|
+
try {
|
|
3145
|
+
const url = new URL(parsedValue);
|
|
3146
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return;
|
|
3147
|
+
url.hash = "";
|
|
3148
|
+
url.search = "";
|
|
3149
|
+
return url.toString().replace(/\/+$/, "").toLowerCase();
|
|
3150
|
+
} catch {
|
|
3151
|
+
return;
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
function resolveUrlHostname(value) {
|
|
3155
|
+
const trimmed = readStringValue(value)?.trim();
|
|
3156
|
+
if (!trimmed) return;
|
|
3157
|
+
try {
|
|
3158
|
+
return new URL(trimmed).hostname.toLowerCase();
|
|
3159
|
+
} catch {
|
|
3160
|
+
try {
|
|
3161
|
+
return new URL(`https://${trimmed}`).hostname.toLowerCase();
|
|
3162
|
+
} catch {
|
|
3163
|
+
return;
|
|
3164
|
+
}
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
function hostMatchesSuffix(host, suffix) {
|
|
3168
|
+
return suffix.startsWith(".") || suffix.startsWith("-") ? host.endsWith(suffix) : host === suffix || host.endsWith(`.${suffix}`);
|
|
3169
|
+
}
|
|
3170
|
+
function isLocalEndpointHost(host) {
|
|
3171
|
+
return LOCAL_ENDPOINT_HOSTS.has(host) || host.endsWith(".localhost") || host.endsWith(".local") || host.endsWith(".internal");
|
|
3172
|
+
}
|
|
3173
|
+
function resolveBundledOpenAIResponsesEndpointClass(baseUrl) {
|
|
3174
|
+
const trimmed = readStringValue(baseUrl)?.trim();
|
|
3175
|
+
if (!trimmed) return "default";
|
|
3176
|
+
const host = resolveUrlHostname(trimmed);
|
|
3177
|
+
if (!host) return "invalid";
|
|
3178
|
+
const comparableBaseUrl = normalizeComparableBaseUrl(trimmed);
|
|
3179
|
+
switch (host) {
|
|
3180
|
+
case "api.anthropic.com": return "anthropic-public";
|
|
3181
|
+
case "api.cerebras.ai": return "cerebras-native";
|
|
3182
|
+
case "llm.chutes.ai": return "chutes-native";
|
|
3183
|
+
case "api.deepseek.com": return "deepseek-native";
|
|
3184
|
+
case "api.groq.com": return "groq-native";
|
|
3185
|
+
case "api.mistral.ai": return "mistral-public";
|
|
3186
|
+
case "api.openai.com": return "openai-public";
|
|
3187
|
+
case "chatgpt.com": return "openai";
|
|
3188
|
+
case "generativelanguage.googleapis.com": return "google-generative-ai";
|
|
3189
|
+
case "aiplatform.googleapis.com": return "google-vertex";
|
|
3190
|
+
case "api.x.ai": return "xai-native";
|
|
3191
|
+
case "api.z.ai": return "zai-native";
|
|
3192
|
+
}
|
|
3193
|
+
if (hostMatchesSuffix(host, ".githubcopilot.com")) return "github-copilot-native";
|
|
3194
|
+
if (hostMatchesSuffix(host, ".openai.azure.com")) return "azure-openai";
|
|
3195
|
+
if (hostMatchesSuffix(host, "openrouter.ai")) return "openrouter";
|
|
3196
|
+
if (hostMatchesSuffix(host, "opencode.ai")) return "opencode-native";
|
|
3197
|
+
if (hostMatchesSuffix(host, "-aiplatform.googleapis.com")) return "google-vertex";
|
|
3198
|
+
if (comparableBaseUrl && MOONSHOT_NATIVE_BASE_URLS.has(comparableBaseUrl)) return "moonshot-native";
|
|
3199
|
+
if (comparableBaseUrl && MODELSTUDIO_NATIVE_BASE_URLS.has(comparableBaseUrl)) return "modelstudio-native";
|
|
3200
|
+
if (isLocalEndpointHost(host)) return "local";
|
|
3201
|
+
return "custom";
|
|
3202
|
+
}
|
|
3203
|
+
function isOpenAIResponsesApi(api) {
|
|
3204
|
+
return api !== void 0 && OPENAI_RESPONSES_APIS.has(api);
|
|
3205
|
+
}
|
|
3206
|
+
function readCompatPayloadBoolean(compat, key) {
|
|
3207
|
+
if (!compat || typeof compat !== "object") return;
|
|
3208
|
+
const value = compat[key];
|
|
3209
|
+
return typeof value === "boolean" ? value : void 0;
|
|
3210
|
+
}
|
|
3211
|
+
function resolveOpenAIResponsesPayloadCapabilities(model) {
|
|
3212
|
+
const provider = normalizeLowercaseString(model.provider);
|
|
3213
|
+
const api = normalizeLowercaseString(model.api);
|
|
3214
|
+
const isOpenAIProvider = provider === "openai";
|
|
3215
|
+
const endpointClass = resolveBundledOpenAIResponsesEndpointClass(model.baseUrl);
|
|
3216
|
+
const isResponsesApi = isOpenAIResponsesApi(api);
|
|
3217
|
+
const usesConfiguredBaseUrl = endpointClass !== "default";
|
|
3218
|
+
const usesKnownNativeOpenAIEndpoint = endpointClass === "openai-public" || endpointClass === "openai" || endpointClass === "azure-openai";
|
|
3219
|
+
const usesKnownNativeOpenAIRoute = endpointClass === "default" ? provider === "openai" : usesKnownNativeOpenAIEndpoint;
|
|
3220
|
+
const usesExplicitProxyLikeEndpoint = usesConfiguredBaseUrl && !usesKnownNativeOpenAIEndpoint;
|
|
3221
|
+
const promptCacheKeySupport = readCompatPayloadBoolean(model.compat, "supportsPromptCacheKey");
|
|
3222
|
+
const shouldStripResponsesPromptCache = promptCacheKeySupport === true ? false : promptCacheKeySupport === false ? isResponsesApi : isResponsesApi && usesExplicitProxyLikeEndpoint;
|
|
3223
|
+
const supportsResponsesStoreField = readCompatPayloadBoolean(model.compat, "supportsStore") !== false && isResponsesApi;
|
|
3224
|
+
return {
|
|
3225
|
+
allowsOpenAIServiceTier: provider === "openai" && (api === "openai-responses" || api === "openclaw-openai-responses-transport") && endpointClass === "openai-public" || isOpenAIProvider && (api === "openai-chatgpt-responses" || api === "openclaw-openai-chatgpt-responses-transport" || api === "openai-responses" || api === "openclaw-openai-responses-transport") && endpointClass === "openai",
|
|
3226
|
+
allowsResponsesStore: supportsResponsesStoreField && api !== "openai-chatgpt-responses" && api !== "openclaw-openai-chatgpt-responses-transport" && provider !== void 0 && OPENAI_RESPONSES_PROVIDERS.has(provider) && usesKnownNativeOpenAIEndpoint,
|
|
3227
|
+
shouldStripResponsesPromptCache,
|
|
3228
|
+
supportsResponsesStoreField,
|
|
3229
|
+
usesKnownNativeOpenAIRoute
|
|
3230
|
+
};
|
|
3231
|
+
}
|
|
3232
|
+
function parsePositiveInteger(value) {
|
|
3233
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) return Math.floor(value);
|
|
3234
|
+
if (typeof value === "string") return parseStrictPositiveInteger(value);
|
|
3235
|
+
}
|
|
3236
|
+
function resolveOpenAIResponsesCompactThreshold(model) {
|
|
3237
|
+
const contextWindow = parsePositiveInteger(model.contextWindow);
|
|
3238
|
+
if (contextWindow) return Math.max(1e3, Math.floor(contextWindow * .7));
|
|
3239
|
+
return 8e4;
|
|
3240
|
+
}
|
|
3241
|
+
function shouldEnableOpenAIResponsesServerCompaction(explicitStore, provider, extraParams) {
|
|
3242
|
+
const configured = extraParams?.responsesServerCompaction;
|
|
3243
|
+
if (configured === false) return false;
|
|
3244
|
+
if (explicitStore !== true) return false;
|
|
3245
|
+
if (configured === true) return true;
|
|
3246
|
+
return provider === "openai";
|
|
3247
|
+
}
|
|
3248
|
+
function stripDisabledOpenAIReasoningPayload(payloadObj) {
|
|
3249
|
+
const reasoning = payloadObj.reasoning;
|
|
3250
|
+
if (reasoning === "none") {
|
|
3251
|
+
delete payloadObj.reasoning;
|
|
3252
|
+
return;
|
|
3253
|
+
}
|
|
3254
|
+
if (!reasoning || typeof reasoning !== "object" || Array.isArray(reasoning)) return;
|
|
3255
|
+
if (reasoning.effort === "none") delete payloadObj.reasoning;
|
|
3256
|
+
}
|
|
3257
|
+
/** Strip returned-item metadata rejected by strict Responses-compatible endpoints. */
|
|
3258
|
+
function stripInputItemStatuses(input) {
|
|
3259
|
+
if (!Array.isArray(input)) return;
|
|
3260
|
+
for (const item of input) if (item && typeof item === "object" && !Array.isArray(item)) delete item.status;
|
|
3261
|
+
}
|
|
3262
|
+
/** Resolve payload mutation policy for one OpenAI Responses-style model endpoint. */
|
|
3263
|
+
function resolveOpenAIResponsesPayloadPolicy(model, options = {}) {
|
|
3264
|
+
const capabilities = resolveOpenAIResponsesPayloadCapabilities(model);
|
|
3265
|
+
const storeMode = options.storeMode ?? "provider-policy";
|
|
3266
|
+
const explicitStore = storeMode === "preserve" ? void 0 : storeMode === "disable" ? capabilities.supportsResponsesStoreField ? false : void 0 : capabilities.allowsResponsesStore ? true : void 0;
|
|
3267
|
+
const isResponsesApi = isOpenAIResponsesApi(normalizeLowercaseString(model.api));
|
|
3268
|
+
const shouldStripDisabledReasoningPayload = isResponsesApi && (!capabilities.usesKnownNativeOpenAIRoute || !supportsOpenAIReasoningEffort(model, "none"));
|
|
3269
|
+
const shouldStripInputStatus = isResponsesApi && !capabilities.usesKnownNativeOpenAIRoute;
|
|
3270
|
+
return {
|
|
3271
|
+
allowsServiceTier: capabilities.allowsOpenAIServiceTier,
|
|
3272
|
+
compactThreshold: parsePositiveInteger(options.extraParams?.responsesCompactThreshold) ?? resolveOpenAIResponsesCompactThreshold(model),
|
|
3273
|
+
explicitStore,
|
|
3274
|
+
shouldStripDisabledReasoningPayload,
|
|
3275
|
+
shouldStripInputStatus,
|
|
3276
|
+
shouldStripPromptCache: options.enablePromptCacheStripping === true && capabilities.shouldStripResponsesPromptCache,
|
|
3277
|
+
shouldStripStore: explicitStore !== true && readCompatPayloadBoolean(model.compat, "supportsStore") === false && isResponsesApi,
|
|
3278
|
+
useServerCompaction: options.enableServerCompaction === true && shouldEnableOpenAIResponsesServerCompaction(explicitStore, model.provider, options.extraParams)
|
|
3279
|
+
};
|
|
3280
|
+
}
|
|
3281
|
+
/** Mutate a Responses request payload according to the resolved endpoint policy. */
|
|
3282
|
+
function applyOpenAIResponsesPayloadPolicy(payloadObj, policy) {
|
|
3283
|
+
if (policy.explicitStore !== void 0) payloadObj.store = policy.explicitStore;
|
|
3284
|
+
if (policy.shouldStripStore) delete payloadObj.store;
|
|
3285
|
+
if (policy.shouldStripPromptCache) {
|
|
3286
|
+
delete payloadObj.prompt_cache_key;
|
|
3287
|
+
delete payloadObj.prompt_cache_retention;
|
|
3288
|
+
}
|
|
3289
|
+
if (policy.useServerCompaction && payloadObj.context_management === void 0) payloadObj.context_management = [{
|
|
3290
|
+
type: "compaction",
|
|
3291
|
+
compact_threshold: policy.compactThreshold
|
|
3292
|
+
}];
|
|
3293
|
+
if (policy.shouldStripDisabledReasoningPayload) stripDisabledOpenAIReasoningPayload(payloadObj);
|
|
3294
|
+
if (policy.shouldStripInputStatus) stripInputItemStatuses(payloadObj.input);
|
|
3295
|
+
}
|
|
3296
|
+
//#endregion
|
|
3297
|
+
//#region packages/ai/src/transports/openai-responses-params-internal.ts
|
|
3298
|
+
function convertResponsesTools(tools, model, options) {
|
|
3299
|
+
const projection = projectOpenAITools(tools);
|
|
3300
|
+
const strict = resolveOpenAIStrictToolFlagWithDiagnostics(projection, options?.strict, {
|
|
3301
|
+
transport: "responses",
|
|
3302
|
+
model
|
|
3303
|
+
});
|
|
3304
|
+
return {
|
|
3305
|
+
projection,
|
|
3306
|
+
tools: sortPromptCacheToolsByName(projection.tools).map((tool) => {
|
|
3307
|
+
const result = {
|
|
3308
|
+
type: "function",
|
|
3309
|
+
name: tool.name,
|
|
3310
|
+
description: tool.description,
|
|
3311
|
+
parameters: normalizeOpenAIStrictToolParameters(tool.parameters, strict === true, model.compat)
|
|
3312
|
+
};
|
|
3313
|
+
if (strict !== void 0) result.strict = strict;
|
|
3314
|
+
return result;
|
|
3315
|
+
})
|
|
3316
|
+
};
|
|
3317
|
+
}
|
|
3318
|
+
function getPromptCacheRetention(baseUrl, cacheRetention) {
|
|
3319
|
+
if (cacheRetention !== "long") return;
|
|
3320
|
+
return baseUrl?.includes("api.openai.com") ? "24h" : void 0;
|
|
3321
|
+
}
|
|
3322
|
+
function resolveOpenAIReasoningEffort(options) {
|
|
3323
|
+
return normalizeOpenAIReasoningEffort(options?.reasoningEffort ?? options?.reasoning ?? "high");
|
|
3324
|
+
}
|
|
3325
|
+
function hasResponsesWebSearchTool(tools) {
|
|
3326
|
+
if (!Array.isArray(tools)) return false;
|
|
3327
|
+
return tools.some((tool) => {
|
|
3328
|
+
if (!isRecord(tool)) return false;
|
|
3329
|
+
if (tool.type === "web_search") return true;
|
|
3330
|
+
if (tool.type === "function" && tool.name === "web_search") return true;
|
|
3331
|
+
const fn = tool.function;
|
|
3332
|
+
return isRecord(fn) && fn.name === "web_search";
|
|
3333
|
+
});
|
|
3334
|
+
}
|
|
3335
|
+
function raiseMinimalReasoningForResponsesWebSearch(params) {
|
|
3336
|
+
if (params.effort !== "minimal" || !hasResponsesWebSearchTool(params.tools)) return params.effort;
|
|
3337
|
+
for (const effort of [
|
|
3338
|
+
"low",
|
|
3339
|
+
"medium",
|
|
3340
|
+
"high"
|
|
3341
|
+
]) {
|
|
3342
|
+
const resolved = resolveOpenAIReasoningEffortForModel({
|
|
3343
|
+
model: params.model,
|
|
3344
|
+
effort
|
|
3345
|
+
});
|
|
3346
|
+
if (resolved && resolved !== "none" && resolved !== "minimal") return resolved;
|
|
3347
|
+
}
|
|
3348
|
+
return params.effort;
|
|
3349
|
+
}
|
|
3350
|
+
const OPENAI_CODEX_RESPONSES_UNSUPPORTED_PARAMS = [
|
|
3351
|
+
"max_output_tokens",
|
|
3352
|
+
"metadata",
|
|
3353
|
+
"prompt_cache_retention",
|
|
3354
|
+
"service_tier",
|
|
3355
|
+
"temperature",
|
|
3356
|
+
"top_p"
|
|
3357
|
+
];
|
|
3358
|
+
function stripOpenAICodexResponsesUnsupportedTextFields(params) {
|
|
3359
|
+
const text = params.text;
|
|
3360
|
+
if (!text || typeof text !== "object" || Array.isArray(text)) return;
|
|
3361
|
+
const sanitizedText = { ...text };
|
|
3362
|
+
delete sanitizedText.format;
|
|
3363
|
+
if (Object.keys(sanitizedText).length > 0) params.text = sanitizedText;
|
|
3364
|
+
else delete params.text;
|
|
3365
|
+
}
|
|
3366
|
+
function sanitizeOpenAICodexResponsesParams(model, params) {
|
|
3367
|
+
if (!usesNativeOpenAICodexResponsesBackend(model)) return params;
|
|
3368
|
+
for (const key of OPENAI_CODEX_RESPONSES_UNSUPPORTED_PARAMS) delete params[key];
|
|
3369
|
+
stripOpenAICodexResponsesUnsupportedTextFields(params);
|
|
3370
|
+
return params;
|
|
3371
|
+
}
|
|
3372
|
+
function buildOpenAICodexResponsesInstructions(context) {
|
|
3373
|
+
if (!context.systemPrompt) return;
|
|
3374
|
+
return sanitizeTransportPayloadText(stripSystemPromptCacheBoundary(context.systemPrompt));
|
|
3375
|
+
}
|
|
3376
|
+
function resolveOpenAICodexResponsesInstructions(model, context) {
|
|
3377
|
+
const instructions = buildOpenAICodexResponsesInstructions(context);
|
|
3378
|
+
if (instructions && instructions.trim().length > 0) return instructions;
|
|
3379
|
+
return usesNativeOpenAICodexResponsesBackend(model) ? OPENAI_CODEX_RESPONSES_DEFAULT_INSTRUCTIONS : void 0;
|
|
3380
|
+
}
|
|
3381
|
+
function ensureOpenAICodexResponsesInput(messages, context) {
|
|
3382
|
+
if (messages.length > 0 || !context.systemPrompt) return;
|
|
3383
|
+
if (!buildOpenAICodexResponsesInstructions(context)) throw new Error("OpenAI Codex Responses requires non-empty input when only systemPrompt is provided.");
|
|
3384
|
+
messages.push(buildResponsesInputMessage("user", [{
|
|
3385
|
+
type: "input_text",
|
|
3386
|
+
text: " "
|
|
3387
|
+
}]));
|
|
3388
|
+
}
|
|
3389
|
+
function resolveOpenAIResponsesTextFormat(responseFormat) {
|
|
3390
|
+
if (responseFormat.type === "json_schema" && responseFormat.json_schema && typeof responseFormat.json_schema === "object" && !Array.isArray(responseFormat.json_schema)) return {
|
|
3391
|
+
...responseFormat.json_schema,
|
|
3392
|
+
type: "json_schema"
|
|
3393
|
+
};
|
|
3394
|
+
return responseFormat;
|
|
3395
|
+
}
|
|
3396
|
+
function buildOpenAIResponsesParams(model, context, options, metadata) {
|
|
3397
|
+
const isCodexResponses = isOpenAICodexResponsesModel(model);
|
|
3398
|
+
const isNativeCodexResponses = usesNativeOpenAICodexResponsesBackend(model);
|
|
3399
|
+
const compat = getCompat(model);
|
|
3400
|
+
const supportsDeveloperRole = typeof compat.supportsDeveloperRole === "boolean" ? compat.supportsDeveloperRole : void 0;
|
|
3401
|
+
const payloadPolicy = resolveOpenAIResponsesPayloadPolicy(model, { storeMode: "disable" });
|
|
3402
|
+
const policyAllowsReplayIds = payloadPolicy.explicitStore !== false && !payloadPolicy.shouldStripStore;
|
|
3403
|
+
const replayResponsesItemIds = !isNativeCodexResponses && (options?.replayResponsesItemIds ?? policyAllowsReplayIds);
|
|
3404
|
+
const messages = convertResponsesMessages(model, context, /* @__PURE__ */ new Set([
|
|
3405
|
+
"openai",
|
|
3406
|
+
"opencode",
|
|
3407
|
+
"azure-openai-responses",
|
|
3408
|
+
"github-copilot"
|
|
3409
|
+
]), {
|
|
3410
|
+
includeSystemPrompt: !isCodexResponses,
|
|
3411
|
+
supportsDeveloperRole,
|
|
3412
|
+
replayReasoningItems: true,
|
|
3413
|
+
replayResponsesItemIds,
|
|
3414
|
+
authProfileId: options?.authProfileId,
|
|
3415
|
+
sessionId: options?.sessionId
|
|
3416
|
+
});
|
|
3417
|
+
if (isCodexResponses) ensureOpenAICodexResponsesInput(messages, context);
|
|
3418
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
3419
|
+
const promptCacheKey = resolvePromptCacheKey(options, cacheRetention);
|
|
3420
|
+
const params = {
|
|
3421
|
+
model: model.id,
|
|
3422
|
+
input: messages,
|
|
3423
|
+
stream: true,
|
|
3424
|
+
prompt_cache_key: promptCacheKey,
|
|
3425
|
+
prompt_cache_retention: getPromptCacheRetention(model.baseUrl, cacheRetention),
|
|
3426
|
+
...isCodexResponses ? { instructions: resolveOpenAICodexResponsesInstructions(model, context) } : {},
|
|
3427
|
+
...metadata ? { metadata } : {}
|
|
3428
|
+
};
|
|
3429
|
+
const effectiveMaxTokens = options?.maxTokens || model.maxTokens;
|
|
3430
|
+
if (effectiveMaxTokens) params.max_output_tokens = effectiveMaxTokens;
|
|
3431
|
+
if (options?.temperature !== void 0) params.temperature = options.temperature;
|
|
3432
|
+
if (options?.topP !== void 0) params.top_p = options.topP;
|
|
3433
|
+
if (options?.responseFormat !== void 0) params.text = {
|
|
3434
|
+
...params.text,
|
|
3435
|
+
format: resolveOpenAIResponsesTextFormat(options.responseFormat)
|
|
3436
|
+
};
|
|
3437
|
+
if (options?.serviceTier !== void 0 && payloadPolicy.allowsServiceTier) params.service_tier = options.serviceTier;
|
|
3438
|
+
if (context.tools) {
|
|
3439
|
+
const converted = convertResponsesTools(context.tools, model, { strict: resolveOpenAIStrictToolSetting(model, { transport: "stream" }) });
|
|
3440
|
+
if (converted.tools.length > 0 || converted.projection.inputToolCount === 0 && converted.projection.diagnostics.length === 0) params.tools = converted.tools;
|
|
3441
|
+
if (options?.toolChoice) {
|
|
3442
|
+
const toolChoice = reconcileOpenAIResponsesToolChoice(options.toolChoice, converted.projection);
|
|
3443
|
+
if (toolChoice !== void 0) params.tool_choice = toolChoice;
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
if (model.reasoning) {
|
|
3447
|
+
if (options?.reasoningEffort || options?.reasoning || options?.reasoningSummary) {
|
|
3448
|
+
const resolvedReasoningEffort = resolveOpenAIReasoningEffortForModel({
|
|
3449
|
+
model,
|
|
3450
|
+
effort: resolveOpenAIReasoningEffort(options)
|
|
3451
|
+
});
|
|
3452
|
+
const reasoningEffort = resolvedReasoningEffort ? raiseMinimalReasoningForResponsesWebSearch({
|
|
3453
|
+
model,
|
|
3454
|
+
effort: resolvedReasoningEffort,
|
|
3455
|
+
tools: params.tools
|
|
3456
|
+
}) : void 0;
|
|
3457
|
+
if (reasoningEffort) {
|
|
3458
|
+
params.reasoning = {
|
|
3459
|
+
effort: reasoningEffort,
|
|
3460
|
+
...reasoningEffort === "none" ? {} : { summary: options?.reasoningSummary || "auto" }
|
|
3461
|
+
};
|
|
3462
|
+
if (reasoningEffort !== "none") params.include = ["reasoning.encrypted_content"];
|
|
3463
|
+
}
|
|
3464
|
+
} else if (model.provider !== "github-copilot") {
|
|
3465
|
+
const reasoningEffort = resolveOpenAIReasoningEffortForModel({
|
|
3466
|
+
model,
|
|
3467
|
+
effort: "none"
|
|
3468
|
+
});
|
|
3469
|
+
if (reasoningEffort) params.reasoning = { effort: reasoningEffort };
|
|
3470
|
+
}
|
|
3471
|
+
}
|
|
3472
|
+
applyOpenAIResponsesPayloadPolicy(params, payloadPolicy);
|
|
3473
|
+
return sanitizeOpenAICodexResponsesParams(model, params);
|
|
3474
|
+
}
|
|
3475
|
+
//#endregion
|
|
3476
|
+
//#region packages/media-core/src/inline-image-data-url.ts
|
|
3477
|
+
/** Prefix used to distinguish inline data URLs from remote/local image references. */
|
|
3478
|
+
const INLINE_IMAGE_DATA_URL_PREFIX = "data:";
|
|
3479
|
+
const IMAGE_SIGNATURES = [
|
|
3480
|
+
{
|
|
3481
|
+
mime: "image/png",
|
|
3482
|
+
matches: (buffer) => buffer.length >= 8 && buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71 && buffer[4] === 13 && buffer[5] === 10 && buffer[6] === 26 && buffer[7] === 10
|
|
3483
|
+
},
|
|
3484
|
+
{
|
|
3485
|
+
mime: "image/jpeg",
|
|
3486
|
+
matches: (buffer) => buffer.length >= 3 && buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255
|
|
3487
|
+
},
|
|
3488
|
+
{
|
|
3489
|
+
mime: "image/webp",
|
|
3490
|
+
matches: (buffer) => buffer.length >= 12 && buffer.subarray(0, 4).toString("ascii") === "RIFF" && buffer.subarray(8, 12).toString("ascii") === "WEBP"
|
|
3491
|
+
},
|
|
3492
|
+
{
|
|
3493
|
+
mime: "image/gif",
|
|
3494
|
+
matches: (buffer) => buffer.length >= 6 && (buffer.subarray(0, 6).toString("ascii") === "GIF87a" || buffer.subarray(0, 6).toString("ascii") === "GIF89a")
|
|
3495
|
+
},
|
|
3496
|
+
{
|
|
3497
|
+
mime: "image/bmp",
|
|
3498
|
+
matches: (buffer) => buffer.length >= 2 && buffer[0] === 66 && buffer[1] === 77
|
|
3499
|
+
}
|
|
3500
|
+
];
|
|
3501
|
+
const HEIC_BRANDS = /* @__PURE__ */ new Set([
|
|
3502
|
+
"heic",
|
|
3503
|
+
"heix",
|
|
3504
|
+
"hevc",
|
|
3505
|
+
"hevx",
|
|
3506
|
+
"heis",
|
|
3507
|
+
"heim",
|
|
3508
|
+
"hevm",
|
|
3509
|
+
"hevs"
|
|
3510
|
+
]);
|
|
3511
|
+
const HEIF_BRANDS = /* @__PURE__ */ new Set(["mif1", "msf1"]);
|
|
3512
|
+
const IMAGE_SIGNATURE_PREFIX_BASE64_CHARS = 128;
|
|
3513
|
+
const INLINE_IMAGE_DATA_URL_MIMES = /* @__PURE__ */ new Set([
|
|
3514
|
+
"image/png",
|
|
3515
|
+
"image/jpeg",
|
|
3516
|
+
"image/webp",
|
|
3517
|
+
"image/gif"
|
|
3518
|
+
]);
|
|
3519
|
+
function startsWithDataUrl(value) {
|
|
3520
|
+
return value.slice(0, 5).toLowerCase() === INLINE_IMAGE_DATA_URL_PREFIX;
|
|
3521
|
+
}
|
|
3522
|
+
function sniffIsoBmffImageMime(buffer) {
|
|
3523
|
+
if (buffer.length < 12 || buffer.subarray(4, 8).toString("ascii") !== "ftyp") return;
|
|
3524
|
+
const brands = [buffer.subarray(8, 12).toString("ascii")];
|
|
3525
|
+
for (let offset = 16; offset + 4 <= buffer.length; offset += 4) brands.push(buffer.subarray(offset, offset + 4).toString("ascii"));
|
|
3526
|
+
if (brands.some((brand) => HEIC_BRANDS.has(brand))) return "image/heic";
|
|
3527
|
+
if (brands.some((brand) => HEIF_BRANDS.has(brand))) return "image/heif";
|
|
3528
|
+
}
|
|
3529
|
+
/** Sniffs supported inline image formats from decoded bytes. */
|
|
3530
|
+
function sniffInlineImageMime(buffer) {
|
|
3531
|
+
return IMAGE_SIGNATURES.find((signature) => signature.matches(buffer))?.mime ?? sniffIsoBmffImageMime(buffer);
|
|
3532
|
+
}
|
|
3533
|
+
function isImageMimeType(value) {
|
|
3534
|
+
return value.trim().toLowerCase().startsWith("image/");
|
|
3535
|
+
}
|
|
3536
|
+
/** Canonicalizes trusted inline image base64 and rejects malformed or non-image payloads. */
|
|
3537
|
+
function sanitizeInlineImageBase64(params) {
|
|
3538
|
+
if (!isImageMimeType(params.mimeType)) return;
|
|
3539
|
+
const canonicalPayload = canonicalizeBase64(params.base64);
|
|
3540
|
+
if (!canonicalPayload) return;
|
|
3541
|
+
const sniffedMimeType = sniffInlineImageMime(Buffer.from(canonicalPayload.slice(0, IMAGE_SIGNATURE_PREFIX_BASE64_CHARS), "base64"));
|
|
3542
|
+
if (!sniffedMimeType) return;
|
|
3543
|
+
return {
|
|
3544
|
+
mimeType: sniffedMimeType,
|
|
3545
|
+
base64: canonicalPayload
|
|
3546
|
+
};
|
|
3547
|
+
}
|
|
3548
|
+
function parseInlineImageDataUrl(value) {
|
|
3549
|
+
if (!startsWithDataUrl(value)) return {
|
|
3550
|
+
metadata: [],
|
|
3551
|
+
payload: value
|
|
3552
|
+
};
|
|
3553
|
+
const commaIndex = value.indexOf(",");
|
|
3554
|
+
if (commaIndex < 0) return;
|
|
3555
|
+
return {
|
|
3556
|
+
metadata: value.slice(5, commaIndex).split(";").map((part) => part.trim()),
|
|
3557
|
+
payload: value.slice(commaIndex + 1)
|
|
3558
|
+
};
|
|
3559
|
+
}
|
|
3560
|
+
function metadataAllowsImageBase64(metadata) {
|
|
3561
|
+
const [mimeType, ...options] = metadata;
|
|
3562
|
+
return mimeType !== void 0 && isImageMimeType(mimeType) && options.some((part) => part.toLowerCase() === "base64");
|
|
3563
|
+
}
|
|
3564
|
+
function sanitizeInlineImageDataUrlWithAllowedMimes(imageUrl, allowedMimes) {
|
|
3565
|
+
const parsed = parseInlineImageDataUrl(imageUrl);
|
|
3566
|
+
if (!parsed) return;
|
|
3567
|
+
if (parsed.metadata.length === 0) return imageUrl;
|
|
3568
|
+
if (!metadataAllowsImageBase64(parsed.metadata)) return;
|
|
3569
|
+
const [mimeType] = parsed.metadata;
|
|
3570
|
+
const sanitized = sanitizeInlineImageBase64({
|
|
3571
|
+
mimeType: mimeType ?? "",
|
|
3572
|
+
base64: parsed.payload
|
|
3573
|
+
});
|
|
3574
|
+
if (!sanitized) return;
|
|
3575
|
+
if (allowedMimes && !allowedMimes.has(sanitized.mimeType)) return;
|
|
3576
|
+
return `data:${sanitized.mimeType};base64,${sanitized.base64}`;
|
|
3577
|
+
}
|
|
3578
|
+
/** Canonicalizes provider-safe inline image data URLs and rejects unsupported formats. */
|
|
3579
|
+
function sanitizeInlineImageDataUrl(imageUrl) {
|
|
3580
|
+
return sanitizeInlineImageDataUrlWithAllowedMimes(imageUrl, INLINE_IMAGE_DATA_URL_MIMES);
|
|
3581
|
+
}
|
|
3582
|
+
//#endregion
|
|
3583
|
+
//#region packages/ai/src/transports/responses-image-payload-sanitizer.ts
|
|
3584
|
+
/**
|
|
3585
|
+
* Sanitizes OpenAI Responses payloads before transport. Invalid inline images
|
|
3586
|
+
* are replaced with text placeholders so the request remains valid and
|
|
3587
|
+
* auditable.
|
|
3588
|
+
*/
|
|
3589
|
+
const IMAGE_OMITTED_TEXT = "omitted image payload: invalid inline image data";
|
|
3590
|
+
function invalidSnakeImage() {
|
|
3591
|
+
return {
|
|
3592
|
+
type: "input_text",
|
|
3593
|
+
text: `[${IMAGE_OMITTED_TEXT}]`
|
|
3594
|
+
};
|
|
3595
|
+
}
|
|
3596
|
+
function sanitizeValue(value) {
|
|
3597
|
+
if (Array.isArray(value)) return value.map(sanitizeValue);
|
|
3598
|
+
if (!isRecord(value)) return value;
|
|
3599
|
+
if (value.type === "input_image" && typeof value.image_url === "string") {
|
|
3600
|
+
const imageUrl = sanitizeInlineImageDataUrl(value.image_url);
|
|
3601
|
+
return imageUrl ? {
|
|
3602
|
+
...value,
|
|
3603
|
+
image_url: imageUrl
|
|
3604
|
+
} : invalidSnakeImage();
|
|
3605
|
+
}
|
|
3606
|
+
const next = {};
|
|
3607
|
+
for (const [key, child] of Object.entries(value)) next[key] = sanitizeValue(child);
|
|
3608
|
+
return next;
|
|
3609
|
+
}
|
|
3610
|
+
/** Sanitize inline image fields inside a Responses API payload. */
|
|
3611
|
+
function sanitizeResponsesImagePayload(params) {
|
|
3612
|
+
if (!Array.isArray(params.input)) return params;
|
|
3613
|
+
return {
|
|
3614
|
+
...params,
|
|
3615
|
+
input: sanitizeValue(params.input)
|
|
3616
|
+
};
|
|
3617
|
+
}
|
|
3618
|
+
//#endregion
|
|
3619
|
+
//#region packages/ai/src/transports/openai-responses-client.ts
|
|
3620
|
+
function resolveProviderTransportTurnState(model, params) {
|
|
3621
|
+
const normalizedProvider = model.provider.trim().toLowerCase();
|
|
3622
|
+
const allowRuntimePluginLoad = normalizedProvider === "openai" || normalizedProvider === "azure-openai" || normalizedProvider === "azure-openai-responses";
|
|
3623
|
+
return getAiTransportHost().plugin.resolveTransportTurnState({
|
|
3624
|
+
provider: model.provider,
|
|
3625
|
+
modelId: model.id,
|
|
3626
|
+
allowRuntimePluginLoad,
|
|
3627
|
+
context: {
|
|
3628
|
+
provider: model.provider,
|
|
3629
|
+
modelId: model.id,
|
|
3630
|
+
model,
|
|
3631
|
+
sessionId: params.sessionId,
|
|
3632
|
+
turnId: params.turnId,
|
|
3633
|
+
attempt: params.attempt,
|
|
3634
|
+
transport: params.transport
|
|
3635
|
+
}
|
|
3636
|
+
});
|
|
3637
|
+
}
|
|
3638
|
+
function createOpenAIResponsesClient(model, context, apiKey, optionHeaders, turnHeaders, sessionId) {
|
|
3639
|
+
return new OpenAI({
|
|
3640
|
+
apiKey,
|
|
3641
|
+
baseURL: model.baseUrl,
|
|
3642
|
+
dangerouslyAllowBrowser: true,
|
|
3643
|
+
defaultHeaders: buildOpenAIClientHeaders(model, context, optionHeaders, turnHeaders, sessionId),
|
|
3644
|
+
fetch: buildGuardedModelFetch(model),
|
|
3645
|
+
...buildOpenAISdkClientOptions(model)
|
|
3646
|
+
});
|
|
3647
|
+
}
|
|
3648
|
+
function createResponsesTransportExecutor(config) {
|
|
3649
|
+
return (model, context, options) => {
|
|
3650
|
+
const responsesOptions = options;
|
|
3651
|
+
const eventStream = createAssistantMessageEventStream();
|
|
3652
|
+
const stream = eventStream;
|
|
3653
|
+
(async () => {
|
|
3654
|
+
const output = {
|
|
3655
|
+
role: "assistant",
|
|
3656
|
+
content: [],
|
|
3657
|
+
api: config.outputApi ?? model.api,
|
|
3658
|
+
provider: model.provider,
|
|
3659
|
+
model: model.id,
|
|
3660
|
+
usage: {
|
|
3661
|
+
input: 0,
|
|
3662
|
+
output: 0,
|
|
3663
|
+
cacheRead: 0,
|
|
3664
|
+
cacheWrite: 0,
|
|
3665
|
+
totalTokens: 0,
|
|
3666
|
+
cost: {
|
|
3667
|
+
input: 0,
|
|
3668
|
+
output: 0,
|
|
3669
|
+
cacheRead: 0,
|
|
3670
|
+
cacheWrite: 0,
|
|
3671
|
+
total: 0
|
|
3672
|
+
}
|
|
3673
|
+
},
|
|
3674
|
+
stopReason: "stop",
|
|
3675
|
+
timestamp: Date.now()
|
|
3676
|
+
};
|
|
3677
|
+
let firstEventAbort;
|
|
3678
|
+
try {
|
|
3679
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
|
|
3680
|
+
const turnState = resolveProviderTransportTurnState(model, {
|
|
3681
|
+
sessionId: options?.sessionId,
|
|
3682
|
+
turnId: randomUUID(),
|
|
3683
|
+
attempt: 1,
|
|
3684
|
+
transport: "stream"
|
|
3685
|
+
});
|
|
3686
|
+
const client = config.createClient(model, context, apiKey, options?.headers, turnState?.headers, options?.sessionId);
|
|
3687
|
+
let params = config.buildRequest(model, context, responsesOptions, turnState?.metadata);
|
|
3688
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
3689
|
+
if (nextParams !== void 0) params = nextParams;
|
|
3690
|
+
if (!isOpenAICodexResponsesModel(model)) params = mergeTransportMetadata(params, turnState?.metadata);
|
|
3691
|
+
params = sanitizeOpenAICodexResponsesParams(model, params);
|
|
3692
|
+
params = sanitizeResponsesImagePayload(params);
|
|
3693
|
+
if (options?.openclawCodeModeToolSurface === true) {
|
|
3694
|
+
enforceCodeModeResponsesToolSurface(params);
|
|
3695
|
+
assertCodeModeResponsesToolSurface(params);
|
|
3696
|
+
}
|
|
3697
|
+
const requestStartedAt = Date.now();
|
|
3698
|
+
firstEventAbort = createFirstStreamEventAbortController(options?.signal);
|
|
3699
|
+
const requestOptions = buildOpenAISdkRequestOptions(model, firstEventAbort.signal, config.streamRequest ? { stream: true } : void 0);
|
|
3700
|
+
emitModelTransportDebug(log, `[responses] start provider=${model.provider} api=${model.api} model=${model.id} baseUrl=${formatModelTransportDebugBaseUrl(model.baseUrl)} timeoutMs=${safeDebugValue(requestOptions?.timeout)} apiKey=${apiKey ? "present" : "missing"} ${summarizeResponsesPayload(params)}`);
|
|
3701
|
+
const responseStream = await config.createResponseStream({
|
|
3702
|
+
client,
|
|
3703
|
+
request: params,
|
|
3704
|
+
requestOptions,
|
|
3705
|
+
model
|
|
3706
|
+
});
|
|
3707
|
+
emitModelTransportDebug(log, `[responses] headers provider=${model.provider} api=${model.api} model=${model.id} elapsedMs=${Date.now() - requestStartedAt}`);
|
|
3708
|
+
stream.push({
|
|
3709
|
+
type: "start",
|
|
3710
|
+
partial: output
|
|
3711
|
+
});
|
|
3712
|
+
await processResponsesStream(observeResponsesStream(responseStream, model), output, stream, model, {
|
|
3713
|
+
...config.pricingOptions?.(responsesOptions),
|
|
3714
|
+
firstEventTimeoutMs: getFirstStreamEventTimeoutMs(options) ?? config.firstEventTimeoutMs,
|
|
3715
|
+
abortFirstEventStream: firstEventAbort.abort,
|
|
3716
|
+
onFirstEventTimeout: getFirstStreamEventTimeoutHandler(options),
|
|
3717
|
+
signal: options?.signal,
|
|
3718
|
+
reasoningReplayMetadata: buildOpenAIResponsesReasoningReplayMetadata(model, {
|
|
3719
|
+
authProfileId: responsesOptions?.authProfileId,
|
|
3720
|
+
sessionId: options?.sessionId
|
|
3721
|
+
})
|
|
3722
|
+
});
|
|
3723
|
+
if (options?.signal?.aborted) throw transportAbortError(options.signal);
|
|
3724
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") throw new Error("An unknown error occurred");
|
|
3725
|
+
stream.push({
|
|
3726
|
+
type: "done",
|
|
3727
|
+
reason: output.stopReason,
|
|
3728
|
+
message: output
|
|
3729
|
+
});
|
|
3730
|
+
stream.end();
|
|
3731
|
+
} catch (error) {
|
|
3732
|
+
if (error instanceof ResponsesStreamFailure && error.observation) logResponsesFailedNoDetails(error.observation);
|
|
3733
|
+
log.warn(`[responses] error provider=${model.provider} api=${model.api} model=${model.id} ` + summarizeOpenAITransportError(error));
|
|
3734
|
+
assignTransportErrorDetails(output, error, options?.signal);
|
|
3735
|
+
stream.push({
|
|
3736
|
+
type: "error",
|
|
3737
|
+
reason: output.stopReason,
|
|
3738
|
+
error: output
|
|
3739
|
+
});
|
|
3740
|
+
stream.end();
|
|
3741
|
+
} finally {
|
|
3742
|
+
firstEventAbort?.dispose();
|
|
3743
|
+
}
|
|
3744
|
+
})();
|
|
3745
|
+
return eventStream;
|
|
3746
|
+
};
|
|
3747
|
+
}
|
|
3748
|
+
function createOpenAIResponsesTransportStreamFn() {
|
|
3749
|
+
return createResponsesTransportExecutor({
|
|
3750
|
+
streamRequest: true,
|
|
3751
|
+
createClient: createOpenAIResponsesClient,
|
|
3752
|
+
buildRequest: buildOpenAIResponsesParams,
|
|
3753
|
+
createResponseStream: createResponsesStreamWithEncryptedContentRetry,
|
|
3754
|
+
pricingOptions: (options) => ({
|
|
3755
|
+
serviceTier: options?.serviceTier,
|
|
3756
|
+
applyServiceTierPricing
|
|
3757
|
+
})
|
|
3758
|
+
});
|
|
3759
|
+
}
|
|
3760
|
+
function createAzureOpenAIResponsesTransportStreamFn() {
|
|
3761
|
+
return createResponsesTransportExecutor({
|
|
3762
|
+
outputApi: "azure-openai-responses",
|
|
3763
|
+
firstEventTimeoutMs: AZURE_RESPONSES_FIRST_EVENT_TIMEOUT_MS,
|
|
3764
|
+
createClient: createAzureOpenAIClient,
|
|
3765
|
+
buildRequest: (model, context, options, metadata) => buildAzureOpenAIResponsesParams(model, context, options, resolveAzureDeploymentName(model), metadata),
|
|
3766
|
+
createResponseStream: async ({ client, request, requestOptions }) => await client.responses.create(request, requestOptions)
|
|
3767
|
+
});
|
|
3768
|
+
}
|
|
3769
|
+
function normalizeAzureBaseUrl(baseUrl) {
|
|
3770
|
+
return baseUrl.replace(/\/+$/, "");
|
|
3771
|
+
}
|
|
3772
|
+
function resolveAzureDeploymentName(model) {
|
|
3773
|
+
return resolveAzureDeploymentNameFromMap({
|
|
3774
|
+
modelId: model.id,
|
|
3775
|
+
deploymentMap: process.env.AZURE_OPENAI_DEPLOYMENT_NAME_MAP
|
|
3776
|
+
});
|
|
3777
|
+
}
|
|
3778
|
+
function createAzureOpenAIClient(model, context, apiKey, optionHeaders, turnHeaders) {
|
|
3779
|
+
const baseURL = normalizeAzureBaseUrl(model.baseUrl);
|
|
3780
|
+
const clientOptions = {
|
|
3781
|
+
apiKey,
|
|
3782
|
+
dangerouslyAllowBrowser: true,
|
|
3783
|
+
defaultHeaders: buildOpenAIClientHeaders(model, context, optionHeaders, turnHeaders),
|
|
3784
|
+
baseURL,
|
|
3785
|
+
fetch: buildGuardedModelFetch(model),
|
|
3786
|
+
...buildOpenAISdkClientOptions(model)
|
|
3787
|
+
};
|
|
3788
|
+
if (isOpenAICompatibleAzureResponsesBaseUrl(baseURL)) return new OpenAI(clientOptions);
|
|
3789
|
+
return new AzureOpenAI({
|
|
3790
|
+
...clientOptions,
|
|
3791
|
+
apiVersion: resolveAzureOpenAIApiVersion()
|
|
3792
|
+
});
|
|
3793
|
+
}
|
|
3794
|
+
function buildAzureOpenAIResponsesParams(model, context, options, deploymentName, metadata) {
|
|
3795
|
+
const params = buildOpenAIResponsesParams(model, context, options, metadata);
|
|
3796
|
+
params.model = deploymentName;
|
|
3797
|
+
delete params.store;
|
|
3798
|
+
return params;
|
|
3799
|
+
}
|
|
3800
|
+
//#endregion
|
|
3801
|
+
//#region packages/ai/src/transports/openai-responses-transport.ts
|
|
3802
|
+
/** OpenAI Responses transport facade. */
|
|
3803
|
+
const responsesTesting = {
|
|
3804
|
+
getCompat,
|
|
3805
|
+
assertCodeModeResponsesToolSurface,
|
|
3806
|
+
buildOpenAIResponsesParams,
|
|
3807
|
+
buildOpenAIClientHeaders,
|
|
3808
|
+
buildOpenAISdkClientOptions,
|
|
3809
|
+
buildOpenAISdkRequestOptions,
|
|
3810
|
+
createAzureOpenAIClient,
|
|
3811
|
+
createOpenAIResponsesClient,
|
|
3812
|
+
enforceCodeModeResponsesToolSurface,
|
|
3813
|
+
sanitizeOpenAICodexResponsesParams,
|
|
3814
|
+
processResponsesStream,
|
|
3815
|
+
formatModelTransportDebugBaseUrl,
|
|
3816
|
+
buildResponsesFailedNoDetailsObservation,
|
|
3817
|
+
buildOpenAIResponsesReasoningReplayMetadata,
|
|
3818
|
+
isInvalidEncryptedContentError,
|
|
3819
|
+
normalizeResponsesFailedEvent,
|
|
3820
|
+
prepareOpenAIResponsesReasoningItemForReplay,
|
|
3821
|
+
createResponsesStreamWithEncryptedContentRetry,
|
|
3822
|
+
resolveAzureOpenAIApiVersion,
|
|
3823
|
+
stripResponsesRequestEncryptedContent,
|
|
3824
|
+
tagOpenAIResponsesReasoningReplayItem,
|
|
3825
|
+
summarizeResponsesFailedNoDetailsObservation,
|
|
3826
|
+
summarizeResponsesPayload,
|
|
3827
|
+
summarizeResponsesTools,
|
|
3828
|
+
stringifyRedactedEvent,
|
|
3829
|
+
stringifyRedactedPayload
|
|
3830
|
+
};
|
|
3831
|
+
if (process.env.VITEST || false) globalThis.openclawOpenAIResponsesTransportTestApi = responsesTesting;
|
|
3832
|
+
//#endregion
|
|
3833
|
+
//#region packages/ai/src/transports/provider-transport-stream.ts
|
|
3834
|
+
const SUPPORTED_TRANSPORT_APIS = /* @__PURE__ */ new Set([
|
|
3835
|
+
"openai-responses",
|
|
3836
|
+
"openai-chatgpt-responses",
|
|
3837
|
+
"openai-completions",
|
|
3838
|
+
"azure-openai-responses",
|
|
3839
|
+
"anthropic-messages",
|
|
3840
|
+
"google-generative-ai"
|
|
3841
|
+
]);
|
|
3842
|
+
const SIMPLE_TRANSPORT_API_ALIAS = {
|
|
3843
|
+
"openai-responses": "openclaw-openai-responses-transport",
|
|
3844
|
+
"openai-chatgpt-responses": "openclaw-openai-chatgpt-responses-transport",
|
|
3845
|
+
"openai-completions": "openclaw-openai-completions-transport",
|
|
3846
|
+
"azure-openai-responses": "openclaw-azure-openai-responses-transport",
|
|
3847
|
+
"anthropic-messages": "openclaw-anthropic-messages-transport",
|
|
3848
|
+
"google-generative-ai": "openclaw-google-generative-ai-transport"
|
|
3849
|
+
};
|
|
3850
|
+
function createProviderOwnedGoogleTransportStreamFn(model, ctx) {
|
|
3851
|
+
return getAiTransportHost().plugin.resolveProviderStream({
|
|
3852
|
+
provider: model.provider,
|
|
3853
|
+
config: ctx?.cfg,
|
|
3854
|
+
workspaceDir: ctx?.workspaceDir,
|
|
3855
|
+
env: ctx?.env,
|
|
3856
|
+
context: {
|
|
3857
|
+
config: ctx?.cfg,
|
|
3858
|
+
agentDir: ctx?.agentDir,
|
|
3859
|
+
workspaceDir: ctx?.workspaceDir,
|
|
3860
|
+
provider: model.provider,
|
|
3861
|
+
modelId: model.id,
|
|
3862
|
+
model
|
|
3863
|
+
}
|
|
3864
|
+
}) ?? getAiTransportHost().plugin.resolveProviderStream({
|
|
3865
|
+
provider: "google",
|
|
3866
|
+
config: ctx?.cfg,
|
|
3867
|
+
workspaceDir: ctx?.workspaceDir,
|
|
3868
|
+
env: ctx?.env,
|
|
3869
|
+
context: {
|
|
3870
|
+
config: ctx?.cfg,
|
|
3871
|
+
agentDir: ctx?.agentDir,
|
|
3872
|
+
workspaceDir: ctx?.workspaceDir,
|
|
3873
|
+
provider: model.provider,
|
|
3874
|
+
modelId: model.id,
|
|
3875
|
+
model
|
|
3876
|
+
}
|
|
3877
|
+
}) ?? void 0;
|
|
3878
|
+
}
|
|
3879
|
+
function createSupportedTransportStreamFn(model, ctx) {
|
|
3880
|
+
switch (model.api) {
|
|
3881
|
+
case "openai-responses":
|
|
3882
|
+
case "openai-chatgpt-responses": return createOpenAIResponsesTransportStreamFn();
|
|
3883
|
+
case "openai-completions": return createOpenAICompletionsTransportStreamFn();
|
|
3884
|
+
case "azure-openai-responses": return createAzureOpenAIResponsesTransportStreamFn();
|
|
3885
|
+
case "anthropic-messages": return createAnthropicMessagesTransportStreamFn();
|
|
3886
|
+
case "google-generative-ai": return createProviderOwnedGoogleTransportStreamFn(model, ctx);
|
|
3887
|
+
default: return;
|
|
3888
|
+
}
|
|
3889
|
+
}
|
|
3890
|
+
function hasOpenClawTransportRequirement(model) {
|
|
3891
|
+
return getAiTransportHost().requiresManagedTransport(model);
|
|
3892
|
+
}
|
|
3893
|
+
/** Returns whether OpenClaw has a managed transport implementation for this API. */
|
|
3894
|
+
function isTransportAwareApiSupported(api) {
|
|
3895
|
+
return SUPPORTED_TRANSPORT_APIS.has(api);
|
|
3896
|
+
}
|
|
3897
|
+
/** Maps public model APIs to the internal transport API id used by simple runtime dispatch. */
|
|
3898
|
+
function resolveTransportAwareSimpleApi(api) {
|
|
3899
|
+
return SIMPLE_TRANSPORT_API_ALIAS[api];
|
|
3900
|
+
}
|
|
3901
|
+
/** Creates a managed transport stream only when request overrides require it. */
|
|
3902
|
+
function createTransportAwareStreamFnForModel(model, ctx) {
|
|
3903
|
+
if (!hasOpenClawTransportRequirement(model)) return;
|
|
3904
|
+
if (!isTransportAwareApiSupported(model.api)) throw new Error(`Model-provider request.proxy/request.tls/localService is not yet supported for api "${model.api}"`);
|
|
3905
|
+
const streamFn = createSupportedTransportStreamFn(model, ctx);
|
|
3906
|
+
if (!streamFn) throw new Error(`Managed transport stream is unavailable for api "${model.api}"`);
|
|
3907
|
+
return streamFn;
|
|
3908
|
+
}
|
|
3909
|
+
/** Creates a managed OpenClaw transport stream for explicit fallback/runtime callers. */
|
|
3910
|
+
function createOpenClawTransportStreamFnForModel(model, ctx) {
|
|
3911
|
+
if (!isTransportAwareApiSupported(model.api)) return;
|
|
3912
|
+
return createSupportedTransportStreamFn(model, ctx);
|
|
3913
|
+
}
|
|
3914
|
+
function createBoundaryAwareStreamFnForModel(model, ctx) {
|
|
3915
|
+
if (!isTransportAwareApiSupported(model.api)) return;
|
|
3916
|
+
return createSupportedTransportStreamFn(model, ctx);
|
|
3917
|
+
}
|
|
3918
|
+
function prepareTransportAwareSimpleModel(model, ctx) {
|
|
3919
|
+
const streamFn = createTransportAwareStreamFnForModel(model, ctx);
|
|
3920
|
+
const alias = resolveTransportAwareSimpleApi(model.api);
|
|
3921
|
+
if (!streamFn || !alias) return model;
|
|
3922
|
+
return getAiTransportHost().inheritManagedTransport(model, {
|
|
3923
|
+
...model,
|
|
3924
|
+
api: alias
|
|
3925
|
+
});
|
|
3926
|
+
}
|
|
3927
|
+
function buildTransportAwareSimpleStreamFn(model, ctx) {
|
|
3928
|
+
return createTransportAwareStreamFnForModel(model, ctx);
|
|
3929
|
+
}
|
|
3930
|
+
//#endregion
|
|
3931
|
+
//#region packages/ai/src/transports/simple-completion-transport.ts
|
|
3932
|
+
const PROVIDER_SIMPLE_COMPLETION_API_PREFIX = "openclaw-provider-simple:";
|
|
3933
|
+
const INVALID_CODEX_BASE_URL_MESSAGE = "OpenAI Codex Responses baseUrl must not include query parameters or fragments";
|
|
3934
|
+
function registerCustomApi(registry, api, streamFn) {
|
|
3935
|
+
getAiTransportHost().registerCustomApi(registry, api, streamFn);
|
|
3936
|
+
return registry.getApiProvider(api) !== void 0;
|
|
3937
|
+
}
|
|
3938
|
+
function projectModel(model, patch) {
|
|
3939
|
+
return getAiTransportHost().inheritManagedTransport(model, {
|
|
3940
|
+
...model,
|
|
3941
|
+
...patch
|
|
3942
|
+
});
|
|
3943
|
+
}
|
|
3944
|
+
function resolveAnthropicVertexSimpleApi(baseUrl) {
|
|
3945
|
+
return `openclaw-anthropic-vertex-simple:${baseUrl?.trim() ? encodeURIComponent(baseUrl.trim()) : "default"}`;
|
|
3946
|
+
}
|
|
3947
|
+
function normalizeCodexResponsesBaseUrlForOpenAISdk(baseUrl) {
|
|
3948
|
+
const normalized = baseUrl?.trim() || "https://chatgpt.com/backend-api";
|
|
3949
|
+
try {
|
|
3950
|
+
const parsed = new URL(normalized);
|
|
3951
|
+
const pathname = parsed.pathname.replace(/\/+$/u, "");
|
|
3952
|
+
const path = pathname.toLowerCase();
|
|
3953
|
+
if (parsed.hostname.toLowerCase() === "chatgpt.com" && [
|
|
3954
|
+
"/backend-api",
|
|
3955
|
+
"/backend-api/v1",
|
|
3956
|
+
"/backend-api/codex",
|
|
3957
|
+
"/backend-api/codex/v1",
|
|
3958
|
+
"/backend-api/codex/responses"
|
|
3959
|
+
].includes(path)) {
|
|
3960
|
+
parsed.pathname = "/backend-api/codex";
|
|
3961
|
+
parsed.search = "";
|
|
3962
|
+
parsed.hash = "";
|
|
3963
|
+
return parsed.toString().replace(/\/$/u, "");
|
|
3964
|
+
}
|
|
3965
|
+
if (normalized.includes("?") || normalized.includes("#")) throw new Error(INVALID_CODEX_BASE_URL_MESSAGE);
|
|
3966
|
+
parsed.pathname = path.endsWith("/codex/responses") ? pathname.slice(0, -10) : path.endsWith("/codex") ? pathname : `${pathname}/codex`;
|
|
3967
|
+
return parsed.toString();
|
|
3968
|
+
} catch (error) {
|
|
3969
|
+
if (error instanceof Error && error.message === INVALID_CODEX_BASE_URL_MESSAGE) throw error;
|
|
3970
|
+
}
|
|
3971
|
+
if (normalized.includes("?") || normalized.includes("#")) throw new Error(INVALID_CODEX_BASE_URL_MESSAGE);
|
|
3972
|
+
const path = normalized.replace(/\/+$/u, "");
|
|
3973
|
+
if (path.endsWith("/codex/responses")) return path.slice(0, -10);
|
|
3974
|
+
return path.endsWith("/codex") ? path : `${path}/codex`;
|
|
3975
|
+
}
|
|
3976
|
+
function resolveProviderSimpleCompletionApi(model) {
|
|
3977
|
+
const parts = [
|
|
3978
|
+
model.provider,
|
|
3979
|
+
model.id,
|
|
3980
|
+
model.api,
|
|
3981
|
+
model.baseUrl || "default"
|
|
3982
|
+
];
|
|
3983
|
+
return `${PROVIDER_SIMPLE_COMPLETION_API_PREFIX}${parts.map((part) => encodeURIComponent(part)).join(":")}`;
|
|
3984
|
+
}
|
|
3985
|
+
function applyProviderSimpleCompletionWrapper(registry, model, cfg) {
|
|
3986
|
+
if (model.api.startsWith(PROVIDER_SIMPLE_COMPLETION_API_PREFIX)) return model;
|
|
3987
|
+
const sourceProvider = registry.getApiProvider(model.api);
|
|
3988
|
+
if (!sourceProvider) return model;
|
|
3989
|
+
const sourceApi = model.api;
|
|
3990
|
+
const sourceStreamFn = (runtimeModel, context, options) => sourceProvider.streamSimple(projectModel(runtimeModel, { api: sourceApi }), context, options);
|
|
3991
|
+
const streamFn = getAiTransportHost().plugin.wrapSimpleCompletionStream({
|
|
3992
|
+
provider: model.provider,
|
|
3993
|
+
config: cfg,
|
|
3994
|
+
context: {
|
|
3995
|
+
config: cfg,
|
|
3996
|
+
provider: model.provider,
|
|
3997
|
+
modelId: model.id,
|
|
3998
|
+
model,
|
|
3999
|
+
streamFn: sourceStreamFn
|
|
4000
|
+
}
|
|
4001
|
+
});
|
|
4002
|
+
if (!streamFn) return model;
|
|
4003
|
+
const api = resolveProviderSimpleCompletionApi(model);
|
|
4004
|
+
return registerCustomApi(registry, api, streamFn) ? projectModel(model, { api }) : model;
|
|
4005
|
+
}
|
|
4006
|
+
function prepareCodexSimpleTransportModel(registry, model, cfg) {
|
|
4007
|
+
if (model.provider !== "openai" || model.api !== "openai-chatgpt-responses") return;
|
|
4008
|
+
const transportModel = projectModel(model, { baseUrl: normalizeCodexResponsesBaseUrlForOpenAISdk(model.baseUrl) });
|
|
4009
|
+
const api = resolveTransportAwareSimpleApi(model.api);
|
|
4010
|
+
const streamFn = createOpenClawTransportStreamFnForModel(transportModel, { cfg });
|
|
4011
|
+
if (!api || !streamFn) return;
|
|
4012
|
+
if (!registerCustomApi(registry, api, streamFn)) return;
|
|
4013
|
+
return projectModel(transportModel, { api });
|
|
4014
|
+
}
|
|
4015
|
+
function resolveModelHeaderSentinels(model) {
|
|
4016
|
+
const headers = resolveAiTransportHeaderSentinels(model.headers);
|
|
4017
|
+
return headers === model.headers ? model : projectModel(model, { headers });
|
|
4018
|
+
}
|
|
4019
|
+
function wrapPluginProviderStream(streamFn) {
|
|
4020
|
+
return (model, context, options) => {
|
|
4021
|
+
const host = getAiTransportHost();
|
|
4022
|
+
const apiKey = options?.apiKey ? host.resolveSecretSentinel(options.apiKey) : options?.apiKey;
|
|
4023
|
+
const headers = resolveAiTransportHeaderSentinels(options?.headers);
|
|
4024
|
+
return streamFn(resolveModelHeaderSentinels(model), context, apiKey === options?.apiKey && headers === options?.headers ? options : {
|
|
4025
|
+
...options,
|
|
4026
|
+
apiKey,
|
|
4027
|
+
headers
|
|
4028
|
+
});
|
|
4029
|
+
};
|
|
4030
|
+
}
|
|
4031
|
+
function registerProviderStreamForModel(params) {
|
|
4032
|
+
const pluginModel = resolveModelHeaderSentinels(params.model);
|
|
4033
|
+
const providerStreamFn = getAiTransportHost().plugin.resolveProviderStream({
|
|
4034
|
+
provider: params.model.provider,
|
|
4035
|
+
config: params.cfg,
|
|
4036
|
+
context: {
|
|
4037
|
+
config: params.cfg,
|
|
4038
|
+
provider: params.model.provider,
|
|
4039
|
+
modelId: params.model.id,
|
|
4040
|
+
model: pluginModel
|
|
4041
|
+
}
|
|
4042
|
+
});
|
|
4043
|
+
const transportFallback = providerStreamFn ? void 0 : createTransportAwareStreamFnForModel(params.model.api === "google-generative-ai" ? pluginModel : params.model, { cfg: params.cfg });
|
|
4044
|
+
const streamFn = providerStreamFn ? wrapPluginProviderStream(providerStreamFn) : transportFallback && params.model.api === "google-generative-ai" ? wrapPluginProviderStream(transportFallback) : transportFallback;
|
|
4045
|
+
return streamFn && registerCustomApi(params.apiRegistry, params.model.api, streamFn) ? streamFn : void 0;
|
|
4046
|
+
}
|
|
4047
|
+
function prepareModelForSimpleCompletion(params) {
|
|
4048
|
+
const { apiRegistry, model, cfg } = params;
|
|
4049
|
+
if (!apiRegistry.getApiProvider(model.api) && registerProviderStreamForModel({
|
|
4050
|
+
model,
|
|
4051
|
+
cfg,
|
|
4052
|
+
apiRegistry
|
|
4053
|
+
})) return applyProviderSimpleCompletionWrapper(apiRegistry, model, cfg);
|
|
4054
|
+
const codexTransportModel = prepareCodexSimpleTransportModel(apiRegistry, model, cfg);
|
|
4055
|
+
if (codexTransportModel) return applyProviderSimpleCompletionWrapper(apiRegistry, codexTransportModel, cfg);
|
|
4056
|
+
const transportAwareModel = prepareTransportAwareSimpleModel(model, { cfg });
|
|
4057
|
+
if (transportAwareModel !== model) {
|
|
4058
|
+
const streamFn = buildTransportAwareSimpleStreamFn(model, { cfg });
|
|
4059
|
+
if (streamFn && registerCustomApi(apiRegistry, transportAwareModel.api, streamFn)) return applyProviderSimpleCompletionWrapper(apiRegistry, transportAwareModel, cfg);
|
|
4060
|
+
}
|
|
4061
|
+
if (model.api === "google-generative-ai") return applyProviderSimpleCompletionWrapper(apiRegistry, getAiTransportHost().prepareGoogleSimpleCompletionModel(apiRegistry, model), cfg);
|
|
4062
|
+
if (model.provider === "anthropic-vertex") {
|
|
4063
|
+
const api = resolveAnthropicVertexSimpleApi(model.baseUrl);
|
|
4064
|
+
if (registerCustomApi(apiRegistry, api, getAiTransportHost().plugin.createAnthropicVertexStream(model))) return applyProviderSimpleCompletionWrapper(apiRegistry, projectModel(model, { api }), cfg);
|
|
4065
|
+
}
|
|
4066
|
+
return applyProviderSimpleCompletionWrapper(apiRegistry, model, cfg);
|
|
4067
|
+
}
|
|
4068
|
+
//#endregion
|
|
4069
|
+
export { GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP, applyAnthropicEphemeralCacheControlMarkers, applyAnthropicPayloadPolicyToParams, applyOpenAIResponsesPayloadPolicy, assertCodeModeResponsesToolSurface, assignTransportErrorDetails, buildOpenAIClientHeaders, buildOpenAICompletionsParams, buildOpenAISdkClientOptions, buildOpenAISdkRequestOptions, buildTransportAwareSimpleStreamFn, canonicalizeMaxTokensParam, coerceTransportToolCallArguments, createAnthropicMessagesTransportStreamFn, createAzureOpenAIResponsesTransportStreamFn, createBoundaryAwareStreamFnForModel, createDeepSeekTextFilter, createEmptyTransportUsage, createModelStreamCooperativeScheduler, createOpenAICompletionsTransportStreamFn, createOpenAIResponsesTransportStreamFn, createOpenClawTransportStreamFnForModel, createTransportAwareStreamFnForModel, createWritableTransportEventStream, detectOpenAICompletionsCompat, emitModelTransportDebug, enforceCodeModeResponsesToolSurface, failTransportStream, finalizeTransportStream, flattenCompletionMessagesToStringContent, formatModelTransportDebugBaseUrl, formatModelTransportDebugUrl, getCompat, hasOpenAICompatibleConversationTurn, isCodeModeModelVisibleToolName, isOpenAICodexResponsesModel, log, mergeTransportHeaders, mergeTransportMetadata, normalizeCodexResponsesBaseUrlForOpenAISdk, parseJsonObjectPreservingUnsafeIntegers, parseJsonPreservingUnsafeIntegers, prepareModelForSimpleCompletion, prepareTransportAwareSimpleModel, quoteUnsafeIntegerLiterals, resolveAnthropicEphemeralCacheControl, resolveAnthropicMessagesUrl, resolveAnthropicPayloadPolicy, resolveCacheRetention, resolveMaxTokensParam, resolveModelPayloadDebugMode, resolveModelSseDebugMode, resolveOpenAICompletionsCompat, resolveOpenAIReasoningEffortMap, resolveOpenAIResponsesPayloadPolicy, resolveOpenAIStrictToolFlagWithDiagnostics, resolvePromptCacheKey, resolveReplayableResponsesMessageId, resolveTransportAwareSimpleApi, sanitizeNonEmptyTransportPayloadText, sanitizeResponsesImagePayload, sanitizeTransportPayloadText, sortPromptCacheToolsByName as sortTransportToolsByName, stripCompletionMessagesToRoleContent, throwIfModelStreamAborted, transportAbortError, usesNativeOpenAICodexResponsesBackend };
|