@openclaw/ai 2026.7.2-beta.4 → 2026.7.2-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{anthropic-CrUDIBpM.mjs → anthropic-CH4UUnZr.mjs} +26 -441
- package/dist/{anthropic-BoTnz8cv.d.mts → anthropic-SrGtwsJu.d.mts} +27 -1
- package/dist/anthropic-usage-DWU-x8MI.mjs +459 -0
- package/dist/{api-registry-BMphkFf1.d.mts → api-registry-DlMgPR39.d.mts} +1 -1
- package/dist/{azure-openai-responses-CbrpVeEv.mjs → azure-openai-responses-CImcwB83.mjs} +4 -4
- package/dist/cache-retention-0x979a5V.mjs +12 -0
- package/dist/deferred-event-buffer-DAvyP7qA.mjs +19 -0
- package/dist/error-coercion-DgxlWC0n.mjs +15 -0
- package/dist/{event-stream-Douf9dob.d.mts → event-stream-YjaPW20U.d.mts} +1 -1
- package/dist/event-stream.d.mts +1 -1
- package/dist/{github-copilot-headers-BsH5cqGj.mjs → github-copilot-headers-NCJtz9i0.mjs} +1 -12
- package/dist/{google-Cs62KA7t.mjs → google-CtSg0iTS.mjs} +3 -3
- package/dist/{google-shared-CMLI-tCZ.mjs → google-shared-DNBz5rcD.mjs} +7 -4
- package/dist/{google-vertex-B4SD3U0f.mjs → google-vertex-31f1uS9L.mjs} +3 -3
- package/dist/{host-WvWBo4h8.d.mts → host-B9GUmcra.d.mts} +2 -2
- package/dist/host-Dog2WQiR.mjs +369 -0
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +3 -3
- package/dist/internal/anthropic.d.mts +16 -62
- package/dist/internal/anthropic.mjs +5 -4
- package/dist/internal/openai.d.mts +260 -2
- package/dist/internal/openai.mjs +7 -5
- package/dist/internal/runtime.d.mts +8 -32
- package/dist/internal/runtime.mjs +7 -6
- package/dist/internal/shared.d.mts +14 -9
- package/dist/internal/shared.mjs +6 -2
- package/dist/{llm-request-activity-CehVkZP-.mjs → llm-request-activity-BjtkplhG.mjs} +1 -19
- package/dist/{mistral-D5Ps7Led.mjs → mistral-CWmpvWYh.mjs} +7 -4
- package/dist/number-coercion-DvG7SNMg.mjs +129 -0
- package/dist/{openai-chatgpt-responses-h0o5yV8Y.mjs → openai-chatgpt-responses-B84Ibtrd.mjs} +33 -29
- package/dist/{openai-completions-exO8NFQf.mjs → openai-completions-DsOxhOD1.mjs} +28 -482
- package/dist/openai-completions-compat-DBWjXoMZ.d.mts +43 -0
- package/dist/openai-prompt-cache-2uo_1OR1.d.mts +7 -0
- package/dist/openai-prompt-cache-mZTCdRPo.mjs +12 -0
- package/dist/openai-reasoning-compat-YgeLncHw.mjs +396 -0
- package/dist/{openai-responses-BHpmtUKo.mjs → openai-responses-BT7A3sLu.mjs} +7 -5
- package/dist/openai-responses-shared-pXl6Wd8S.mjs +392 -0
- package/dist/{openai-responses-shared-CzCurmY1.mjs → openai-responses-stream-internal-Cw5txaGW.mjs} +1609 -1037
- package/dist/{openai-tool-projection-ITOU9bG1.mjs → openai-tool-projection-OhX64DoP.mjs} +26 -11
- package/dist/prompt-cache-stability-Cwcjv_fx.d.mts +13 -0
- package/dist/{provider-error-C4VvV_3t.mjs → provider-error-CAEvRjry.mjs} +8 -2
- package/dist/provider-options-D8bB3z9b.d.mts +144 -0
- package/dist/providers.d.mts +3 -2
- package/dist/providers.mjs +10 -9
- package/dist/reasoning-tag-text-partitioner-CGDyLWUR.mjs +12209 -0
- package/dist/simple-options-9lhRrN73.mjs +50 -0
- package/dist/{src-CXno1H5g.mjs → src-QkygScBs.mjs} +23 -4
- package/dist/stream-first-event-timeout-BBys9hSb.mjs +86 -0
- package/dist/stream-first-event-timeout-DvDeSucC.d.mts +29 -0
- package/dist/tls-certificate-errors-DXSpluKI.mjs +93 -0
- package/dist/tool-result-text-CTpIRbYd.mjs +225 -0
- package/dist/transform-messages-C8mBqZxF.mjs +2 -0
- package/dist/transport-stream-shared-D81p90xq.mjs +297 -0
- package/dist/transports.d.mts +115 -132
- package/dist/transports.mjs +400 -1991
- package/dist/{types-AFwwWium.d.mts → types-bzp5k29J.d.mts} +7 -0
- package/dist/types.d.mts +5 -5
- package/dist/types.mjs +2 -2
- package/dist/{validation-sxvxC8J-.d.mts → validation-B-j7cOYp.d.mts} +1 -1
- package/dist/validation.d.mts +1 -1
- package/package.json +4 -5
- package/dist/host-XYGZcgO8.mjs +0 -98
- package/dist/model-utils-1GiZ2_rr.mjs +0 -64
- package/dist/openai-CoGicoDt.d.mts +0 -332
- package/dist/reasoning-tag-text-partitioner-BlVe67g6.mjs +0 -400
- package/dist/stream-first-event-timeout-DP4xEyBY.mjs +0 -150
- package/dist/system-prompt-cache-boundary-CbHeV4_l.mjs +0 -526
- package/npm-shrinkwrap.json +0 -638
|
@@ -1,526 +0,0 @@
|
|
|
1
|
-
import { a as resolveClaudeFable5ModelIdentity, i as requiresClaudeMandatoryAdaptiveThinking, l as resolveClaudeSonnet5ModelIdentity, r as requiresClaudeDefaultSampling, s as resolveClaudeMythos5ModelIdentity } from "./src-CXno1H5g.mjs";
|
|
2
|
-
import { n as getAiTransportHost } from "./host-XYGZcgO8.mjs";
|
|
3
|
-
import { t as sanitizeSurrogates } from "./sanitize-unicode-DT5o51ur.mjs";
|
|
4
|
-
//#region packages/ai/src/providers/simple-options.ts
|
|
5
|
-
function buildBaseOptions(model, options, apiKey) {
|
|
6
|
-
const firstEventOptions = options;
|
|
7
|
-
return {
|
|
8
|
-
temperature: options?.temperature,
|
|
9
|
-
maxTokens: options?.maxTokens,
|
|
10
|
-
stop: options?.stop,
|
|
11
|
-
signal: options?.signal,
|
|
12
|
-
apiKey: apiKey || options?.apiKey,
|
|
13
|
-
transport: options?.transport,
|
|
14
|
-
cacheRetention: options?.cacheRetention,
|
|
15
|
-
sessionId: options?.sessionId,
|
|
16
|
-
promptCacheKey: options?.promptCacheKey,
|
|
17
|
-
headers: options?.headers,
|
|
18
|
-
onPayload: options?.onPayload,
|
|
19
|
-
onResponse: options?.onResponse,
|
|
20
|
-
timeoutMs: options?.timeoutMs,
|
|
21
|
-
firstEventTimeoutMs: firstEventOptions?.firstEventTimeoutMs,
|
|
22
|
-
onFirstEventTimeout: firstEventOptions?.onFirstEventTimeout,
|
|
23
|
-
maxRetries: options?.maxRetries,
|
|
24
|
-
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
25
|
-
metadata: options?.metadata
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function clampMaxTokensToModel(model, requestedMaxTokens) {
|
|
29
|
-
return requestedMaxTokens === void 0 ? void 0 : Math.max(1, Math.min(requestedMaxTokens, model.maxTokens));
|
|
30
|
-
}
|
|
31
|
-
function clampReasoning(effort) {
|
|
32
|
-
return effort === "xhigh" ? "high" : effort;
|
|
33
|
-
}
|
|
34
|
-
function adjustMaxTokensForThinking(baseMaxTokens, modelMaxTokens, reasoningLevel, customBudgets) {
|
|
35
|
-
const budgets = {
|
|
36
|
-
minimal: 1024,
|
|
37
|
-
low: 2048,
|
|
38
|
-
medium: 8192,
|
|
39
|
-
high: 16384,
|
|
40
|
-
max: 32768,
|
|
41
|
-
...customBudgets
|
|
42
|
-
};
|
|
43
|
-
const minOutputTokens = 1024;
|
|
44
|
-
let thinkingBudget = budgets[clampReasoning(reasoningLevel)];
|
|
45
|
-
const maxTokens = baseMaxTokens === void 0 ? modelMaxTokens : Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);
|
|
46
|
-
if (maxTokens <= thinkingBudget) thinkingBudget = Math.max(0, maxTokens - minOutputTokens);
|
|
47
|
-
return {
|
|
48
|
-
maxTokens,
|
|
49
|
-
thinkingBudget
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
//#endregion
|
|
53
|
-
//#region packages/normalization-core/src/record-coerce.ts
|
|
54
|
-
/** Type guard for non-array object records at browser-safe boundaries. */
|
|
55
|
-
function isRecord(value) {
|
|
56
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
57
|
-
}
|
|
58
|
-
//#endregion
|
|
59
|
-
//#region packages/normalization-core/src/utf16-slice.ts
|
|
60
|
-
function isHighSurrogate(codeUnit) {
|
|
61
|
-
return codeUnit >= 55296 && codeUnit <= 56319;
|
|
62
|
-
}
|
|
63
|
-
function isLowSurrogate(codeUnit) {
|
|
64
|
-
return codeUnit >= 56320 && codeUnit <= 57343;
|
|
65
|
-
}
|
|
66
|
-
/** Slices a UTF-16 string without returning dangling surrogate halves at either edge. */
|
|
67
|
-
function sliceUtf16Safe(input, start, end) {
|
|
68
|
-
const len = input.length;
|
|
69
|
-
let from = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
|
|
70
|
-
let to = end === void 0 ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
|
|
71
|
-
if (to <= from) return "";
|
|
72
|
-
if (from > 0 && from < len) {
|
|
73
|
-
if (isLowSurrogate(input.charCodeAt(from)) && isHighSurrogate(input.charCodeAt(from - 1))) from += 1;
|
|
74
|
-
}
|
|
75
|
-
if (to > 0 && to < len) {
|
|
76
|
-
if (isHighSurrogate(input.charCodeAt(to - 1)) && isLowSurrogate(input.charCodeAt(to))) to -= 1;
|
|
77
|
-
}
|
|
78
|
-
return input.slice(from, to);
|
|
79
|
-
}
|
|
80
|
-
/** Truncates a UTF-16 string without cutting a surrogate pair in half. */
|
|
81
|
-
function truncateUtf16Safe(input, maxLen) {
|
|
82
|
-
const limit = Math.max(0, Math.floor(maxLen));
|
|
83
|
-
if (input.length <= limit) return input;
|
|
84
|
-
return sliceUtf16Safe(input, 0, limit);
|
|
85
|
-
}
|
|
86
|
-
//#endregion
|
|
87
|
-
//#region packages/ai/src/providers/tool-result-text.ts
|
|
88
|
-
const PROVIDER_TOOL_RESULT_MAX_CHARS = 8e3;
|
|
89
|
-
const IMAGE_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([
|
|
90
|
-
"image",
|
|
91
|
-
"image_url",
|
|
92
|
-
"input_image"
|
|
93
|
-
]);
|
|
94
|
-
const AUDIO_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([
|
|
95
|
-
"audio",
|
|
96
|
-
"input_audio",
|
|
97
|
-
"output_audio"
|
|
98
|
-
]);
|
|
99
|
-
const MEDIA_ONLY_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([...IMAGE_TOOL_RESULT_TYPES, ...AUDIO_TOOL_RESULT_TYPES]);
|
|
100
|
-
const INLINE_DATA_URI_PATTERN = /(^|[^A-Za-z0-9_])data:([a-z][a-z0-9.+-]*\/[a-z0-9.+-]+(?:;[a-z0-9.+-]+=[^,;"'\s]+|;base64)*,[^\s"'<>)]+)/gi;
|
|
101
|
-
const MIME_KEY_CANDIDATES = [
|
|
102
|
-
"mimeType",
|
|
103
|
-
"mime_type",
|
|
104
|
-
"mediaType",
|
|
105
|
-
"media_type",
|
|
106
|
-
"contentType",
|
|
107
|
-
"content_type"
|
|
108
|
-
];
|
|
109
|
-
const TEXTUAL_MIME_PATTERN = /^(?:text\/|application\/(?:json|ld\+json|x-ndjson|xml|javascript|x-www-form-urlencoded)|[^/]+\/[^+]+\+(?:json|xml)$)/i;
|
|
110
|
-
const OPAQUE_OR_BINARY_FIELD_RE = /^(?:blob|buffer|bytes|encrypted_content|encrypted_stdout)$/i;
|
|
111
|
-
function readMimeType(value) {
|
|
112
|
-
if (!isRecord(value)) return;
|
|
113
|
-
for (const key of MIME_KEY_CANDIDATES) {
|
|
114
|
-
const mimeType = value[key];
|
|
115
|
-
if (typeof mimeType === "string" && mimeType.trim().length > 0) return mimeType;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function isBinaryMimeType(mimeType) {
|
|
119
|
-
const normalized = mimeType.split(";", 1)[0]?.trim().toLowerCase();
|
|
120
|
-
return normalized ? !TEXTUAL_MIME_PATTERN.test(normalized) : false;
|
|
121
|
-
}
|
|
122
|
-
function describeOmittedValue(value, label) {
|
|
123
|
-
const length = typeof value === "string" ? value.length : JSON.stringify(value)?.length;
|
|
124
|
-
return length ? `[${label} omitted: ${length} chars]` : `[${label} omitted]`;
|
|
125
|
-
}
|
|
126
|
-
function redactInlineDataUris(value) {
|
|
127
|
-
return value.replace(INLINE_DATA_URI_PATTERN, (_match, prefix, uri) => `${prefix}[inline data URI: ${uri.length} chars]`);
|
|
128
|
-
}
|
|
129
|
-
function redactStructuredTextValue(value) {
|
|
130
|
-
const host = getAiTransportHost();
|
|
131
|
-
const redacted = host.redactToolPayloadText(value);
|
|
132
|
-
const trimmed = redacted.trim();
|
|
133
|
-
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return redacted;
|
|
134
|
-
try {
|
|
135
|
-
const redactedWrapper = host.redactSecrets({ structuredTextValue: JSON.parse(redacted) });
|
|
136
|
-
return JSON.stringify(redactedWrapper.structuredTextValue);
|
|
137
|
-
} catch {
|
|
138
|
-
return redacted;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
function stringifyStructuredBlock(block) {
|
|
142
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
143
|
-
try {
|
|
144
|
-
const redactedBlock = getAiTransportHost().redactSecrets({ structuredToolResult: block }).structuredToolResult;
|
|
145
|
-
const serialized = JSON.stringify(redactedBlock, function structuredToolResultReplacer(key, value) {
|
|
146
|
-
if (OPAQUE_OR_BINARY_FIELD_RE.test(key)) return `[omitted ${key}]`;
|
|
147
|
-
if (key === "data") {
|
|
148
|
-
const mimeType = readMimeType(this);
|
|
149
|
-
if (mimeType && isBinaryMimeType(mimeType)) return describeOmittedValue(value, "binary data");
|
|
150
|
-
}
|
|
151
|
-
if (typeof value === "bigint") return value.toString();
|
|
152
|
-
if (typeof value === "string") return redactInlineDataUris(redactStructuredTextValue(value));
|
|
153
|
-
if (typeof value === "function" || typeof value === "symbol" || value === void 0) return;
|
|
154
|
-
if (!value || typeof value !== "object") return value;
|
|
155
|
-
if (seen.has(value)) return "[Circular]";
|
|
156
|
-
seen.add(value);
|
|
157
|
-
return value;
|
|
158
|
-
});
|
|
159
|
-
if (!serialized || serialized === "{}") return;
|
|
160
|
-
return serialized;
|
|
161
|
-
} catch {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
function truncateProviderToolText(text) {
|
|
166
|
-
if (text.length <= PROVIDER_TOOL_RESULT_MAX_CHARS) return text;
|
|
167
|
-
return `${truncateUtf16Safe(text, PROVIDER_TOOL_RESULT_MAX_CHARS)}\n…(truncated)…`;
|
|
168
|
-
}
|
|
169
|
-
/** Media metadata alone is not an attachment; provider emitters need inline bytes. */
|
|
170
|
-
function hasMediaPayload(block) {
|
|
171
|
-
return isRecord(block) && typeof block.data === "string" && block.data.trim().length > 0;
|
|
172
|
-
}
|
|
173
|
-
/** Image metadata alone is not an attachment; provider emitters need inline bytes. */
|
|
174
|
-
function isImageWithMediaPayload(block) {
|
|
175
|
-
return isRecord(block) && block.type === "image" && hasMediaPayload(block);
|
|
176
|
-
}
|
|
177
|
-
function describeToolResultMediaPlaceholder(blocks) {
|
|
178
|
-
let hasImage = false;
|
|
179
|
-
let hasAudio = false;
|
|
180
|
-
for (const block of blocks) {
|
|
181
|
-
if (!hasMediaPayload(block)) continue;
|
|
182
|
-
const record = block;
|
|
183
|
-
const type = typeof record.type === "string" ? record.type : void 0;
|
|
184
|
-
const mimeType = readMimeType(record);
|
|
185
|
-
if (type && IMAGE_TOOL_RESULT_TYPES.has(type) || mimeType?.toLowerCase().startsWith("image/")) hasImage = true;
|
|
186
|
-
if (type && AUDIO_TOOL_RESULT_TYPES.has(type) || mimeType?.toLowerCase().startsWith("audio/")) hasAudio = true;
|
|
187
|
-
}
|
|
188
|
-
if (hasImage && hasAudio) return "(see attached media)";
|
|
189
|
-
if (hasAudio) return "(see attached audio)";
|
|
190
|
-
if (hasImage) return "(see attached image)";
|
|
191
|
-
}
|
|
192
|
-
function extractToolResultBlockText(block) {
|
|
193
|
-
if (!block || typeof block !== "object") return;
|
|
194
|
-
const record = block;
|
|
195
|
-
if (typeof record.type === "string" && MEDIA_ONLY_TOOL_RESULT_TYPES.has(record.type)) return;
|
|
196
|
-
if (record.type === "text") {
|
|
197
|
-
const text = typeof record.text === "string" ? record.text : "";
|
|
198
|
-
return text ? sanitizeSurrogates(text) : void 0;
|
|
199
|
-
}
|
|
200
|
-
const structured = stringifyStructuredBlock(record);
|
|
201
|
-
return structured ? sanitizeSurrogates(truncateProviderToolText(structured)) : void 0;
|
|
202
|
-
}
|
|
203
|
-
function extractToolResultText(blocks) {
|
|
204
|
-
const explicitTexts = [];
|
|
205
|
-
const structuredTexts = [];
|
|
206
|
-
for (const block of blocks) {
|
|
207
|
-
const text = extractToolResultBlockText(block);
|
|
208
|
-
if (!text) continue;
|
|
209
|
-
if (block.type === "text") explicitTexts.push(text);
|
|
210
|
-
else structuredTexts.push(text);
|
|
211
|
-
}
|
|
212
|
-
if (explicitTexts.length > 0) return sanitizeSurrogates(explicitTexts.join("\n"));
|
|
213
|
-
return sanitizeSurrogates(truncateProviderToolText(structuredTexts.join("\n")));
|
|
214
|
-
}
|
|
215
|
-
//#endregion
|
|
216
|
-
//#region packages/normalization-core/src/string-coerce.ts
|
|
217
|
-
/** Reads a value only when it is already a string, preserving whitespace. */
|
|
218
|
-
function readStringValue(value) {
|
|
219
|
-
return typeof value === "string" ? value : void 0;
|
|
220
|
-
}
|
|
221
|
-
/** Trims string input and returns null for non-strings or empty strings. */
|
|
222
|
-
function normalizeNullableString(value) {
|
|
223
|
-
if (typeof value !== "string") return null;
|
|
224
|
-
const trimmed = value.trim();
|
|
225
|
-
return trimmed ? trimmed : null;
|
|
226
|
-
}
|
|
227
|
-
/** Trims string input and returns undefined for non-strings or empty strings. */
|
|
228
|
-
function normalizeOptionalString(value) {
|
|
229
|
-
return normalizeNullableString(value) ?? void 0;
|
|
230
|
-
}
|
|
231
|
-
/** Lowercases a normalized optional string. */
|
|
232
|
-
function normalizeOptionalLowercaseString(value) {
|
|
233
|
-
return normalizeOptionalString(value)?.toLowerCase();
|
|
234
|
-
}
|
|
235
|
-
/** Lowercases a normalized string or returns an empty string when absent. */
|
|
236
|
-
function normalizeLowercaseStringOrEmpty(value) {
|
|
237
|
-
return normalizeOptionalLowercaseString(value) ?? "";
|
|
238
|
-
}
|
|
239
|
-
//#endregion
|
|
240
|
-
//#region packages/ai/src/providers/anthropic-model-contract.ts
|
|
241
|
-
function normalizeModelId(modelId) {
|
|
242
|
-
const normalized = normalizeLowercaseStringOrEmpty(modelId);
|
|
243
|
-
return (normalized.startsWith("anthropic/") ? normalized.slice(10) : normalized).replace(/[._\s]+/g, "-");
|
|
244
|
-
}
|
|
245
|
-
function normalizeApi(api) {
|
|
246
|
-
const normalized = normalizeLowercaseStringOrEmpty(api);
|
|
247
|
-
return normalized === "openclaw-anthropic-messages-transport" ? "anthropic-messages" : normalized;
|
|
248
|
-
}
|
|
249
|
-
function hasConcreteResponseModel(ref) {
|
|
250
|
-
const responseModelId = normalizeModelId(ref.responseModelId);
|
|
251
|
-
return responseModelId.length > 0 && responseModelId !== normalizeModelId(ref.modelId);
|
|
252
|
-
}
|
|
253
|
-
function usesClaudeFable5MessagesContract(model) {
|
|
254
|
-
return normalizeApi(model.api) === "anthropic-messages" && resolveClaudeFable5ModelIdentity(model) !== void 0;
|
|
255
|
-
}
|
|
256
|
-
/** Return whether streamed output must wait for the terminal refusal decision. */
|
|
257
|
-
function usesClaudeStreamingRefusalContract(model) {
|
|
258
|
-
if (normalizeApi(model.api) !== "anthropic-messages") return false;
|
|
259
|
-
return resolveClaudeFable5ModelIdentity(model) !== void 0 || resolveClaudeMythos5ModelIdentity(model) !== void 0 || resolveClaudeSonnet5ModelIdentity(model) !== void 0;
|
|
260
|
-
}
|
|
261
|
-
function requiresClaudeAdaptiveThinking(model) {
|
|
262
|
-
if (normalizeApi(model.api) !== "anthropic-messages") return false;
|
|
263
|
-
return requiresClaudeMandatoryAdaptiveThinking(model);
|
|
264
|
-
}
|
|
265
|
-
/** Return whether omitted thinking should default to adaptive/high. */
|
|
266
|
-
function defaultsClaudeAdaptiveThinking(model) {
|
|
267
|
-
return requiresClaudeAdaptiveThinking(model) || normalizeApi(model.api) === "anthropic-messages" && resolveClaudeSonnet5ModelIdentity(model) !== void 0;
|
|
268
|
-
}
|
|
269
|
-
/** Remove Sonnet 5 assistant prefills while preserving completed tool-use turns. */
|
|
270
|
-
function prepareClaudeSonnet5RequestContext(model, context) {
|
|
271
|
-
if (!resolveClaudeSonnet5ModelIdentity(model)) return context;
|
|
272
|
-
let end = context.messages.length;
|
|
273
|
-
while (end > 0) {
|
|
274
|
-
const message = context.messages[end - 1];
|
|
275
|
-
if (message?.role !== "assistant" || Array.isArray(message.content) && message.content.some((block) => block.type === "toolCall")) break;
|
|
276
|
-
end -= 1;
|
|
277
|
-
}
|
|
278
|
-
return end === context.messages.length ? context : {
|
|
279
|
-
...context,
|
|
280
|
-
messages: context.messages.slice(0, end)
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
function applyClaudeRequestContract(params, model) {
|
|
284
|
-
if (normalizeApi(model.api) !== "anthropic-messages") return;
|
|
285
|
-
const sonnet5 = resolveClaudeSonnet5ModelIdentity(model) !== void 0;
|
|
286
|
-
if (!requiresClaudeDefaultSampling(model) && !sonnet5) return;
|
|
287
|
-
delete params.temperature;
|
|
288
|
-
delete params.top_p;
|
|
289
|
-
delete params.top_k;
|
|
290
|
-
if (sonnet5) delete params.service_tier;
|
|
291
|
-
}
|
|
292
|
-
function resolveReplayModelBoundIdentity(ref) {
|
|
293
|
-
if (normalizeApi(ref.api) !== "anthropic-messages") return;
|
|
294
|
-
const modelRef = hasConcreteResponseModel(ref) ? { id: ref.responseModelId } : {
|
|
295
|
-
id: ref.modelId,
|
|
296
|
-
params: ref.modelParams
|
|
297
|
-
};
|
|
298
|
-
const fableIdentity = resolveClaudeFable5ModelIdentity(modelRef);
|
|
299
|
-
if (fableIdentity) return `fable:${fableIdentity}`;
|
|
300
|
-
const mythosIdentity = resolveClaudeMythos5ModelIdentity(modelRef);
|
|
301
|
-
if (mythosIdentity) return `mythos:${mythosIdentity}`;
|
|
302
|
-
const sonnetIdentity = resolveClaudeSonnet5ModelIdentity(modelRef);
|
|
303
|
-
return sonnetIdentity ? `sonnet:${sonnetIdentity}` : void 0;
|
|
304
|
-
}
|
|
305
|
-
function resolveModelBoundThinkingReplayMode(params) {
|
|
306
|
-
const sourceApi = normalizeApi(params.source.api);
|
|
307
|
-
const targetApi = normalizeApi(params.target.api);
|
|
308
|
-
const sourceIdentity = resolveReplayModelBoundIdentity(params.source);
|
|
309
|
-
const targetIdentity = resolveReplayModelBoundIdentity(params.target);
|
|
310
|
-
const sameRoute = normalizeLowercaseStringOrEmpty(params.source.provider) === normalizeLowercaseStringOrEmpty(params.target.provider) && sourceApi === targetApi && normalizeModelId(params.source.modelId) === normalizeModelId(params.target.modelId);
|
|
311
|
-
if (!sourceIdentity && !targetIdentity) return "default";
|
|
312
|
-
if (!sourceIdentity && !hasConcreteResponseModel(params.source) && targetIdentity && sameRoute) return "preserve";
|
|
313
|
-
return sourceApi === targetApi && sourceIdentity === targetIdentity ? "preserve" : "drop";
|
|
314
|
-
}
|
|
315
|
-
//#endregion
|
|
316
|
-
//#region packages/ai/src/providers/transform-messages.ts
|
|
317
|
-
const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
318
|
-
const NON_VISION_TOOL_IMAGE_PLACEHOLDER = "(tool image omitted: model does not support images)";
|
|
319
|
-
function replaceImagesWithPlaceholder(content, placeholder) {
|
|
320
|
-
const result = [];
|
|
321
|
-
let previousWasPlaceholder = false;
|
|
322
|
-
for (const block of content) {
|
|
323
|
-
if (block.type === "image") {
|
|
324
|
-
if (!isImageWithMediaPayload(block)) continue;
|
|
325
|
-
if (!previousWasPlaceholder) result.push({
|
|
326
|
-
type: "text",
|
|
327
|
-
text: placeholder
|
|
328
|
-
});
|
|
329
|
-
previousWasPlaceholder = true;
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
result.push(block);
|
|
333
|
-
previousWasPlaceholder = block.text === placeholder;
|
|
334
|
-
}
|
|
335
|
-
return result;
|
|
336
|
-
}
|
|
337
|
-
function downgradeUnsupportedImages(messages, model) {
|
|
338
|
-
if (model.input.includes("image")) return messages;
|
|
339
|
-
return messages.map((msg) => {
|
|
340
|
-
if (msg.role === "user" && Array.isArray(msg.content)) return {
|
|
341
|
-
...msg,
|
|
342
|
-
content: replaceImagesWithPlaceholder(msg.content, NON_VISION_USER_IMAGE_PLACEHOLDER)
|
|
343
|
-
};
|
|
344
|
-
if (msg.role === "toolResult") return {
|
|
345
|
-
...msg,
|
|
346
|
-
content: replaceImagesWithPlaceholder(msg.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER)
|
|
347
|
-
};
|
|
348
|
-
return msg;
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Normalize tool call ID for cross-provider compatibility.
|
|
353
|
-
* OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.
|
|
354
|
-
* Anthropic APIs require IDs matching ^[a-zA-Z0-9_-]+$ (max 64 chars).
|
|
355
|
-
*/
|
|
356
|
-
function transformMessages(messages, model, normalizeToolCallId) {
|
|
357
|
-
const toolCallIdMap = /* @__PURE__ */ new Map();
|
|
358
|
-
const transformed = downgradeUnsupportedImages(messages.map((msg) => msg.content == null ? {
|
|
359
|
-
...msg,
|
|
360
|
-
content: []
|
|
361
|
-
} : msg), model).map((msg) => {
|
|
362
|
-
if (msg.role === "user") return msg;
|
|
363
|
-
if (msg.role === "toolResult") {
|
|
364
|
-
const normalizedId = toolCallIdMap.get(msg.toolCallId);
|
|
365
|
-
if (normalizedId && normalizedId !== msg.toolCallId) return Object.assign({}, msg, { toolCallId: normalizedId });
|
|
366
|
-
return msg;
|
|
367
|
-
}
|
|
368
|
-
if (msg.role === "assistant") {
|
|
369
|
-
const assistantMsg = msg;
|
|
370
|
-
const modelBoundThinkingReplayMode = resolveModelBoundThinkingReplayMode({
|
|
371
|
-
source: {
|
|
372
|
-
provider: assistantMsg.provider,
|
|
373
|
-
api: assistantMsg.api,
|
|
374
|
-
modelId: assistantMsg.model,
|
|
375
|
-
responseModelId: assistantMsg.responseModel
|
|
376
|
-
},
|
|
377
|
-
target: {
|
|
378
|
-
provider: model.provider,
|
|
379
|
-
api: model.api,
|
|
380
|
-
modelId: model.id,
|
|
381
|
-
modelParams: model.params
|
|
382
|
-
}
|
|
383
|
-
});
|
|
384
|
-
const isSameModel = modelBoundThinkingReplayMode === "preserve" || assistantMsg.provider === model.provider && assistantMsg.api === model.api && assistantMsg.model === model.id;
|
|
385
|
-
const transformedContent = (typeof assistantMsg.content === "string" ? [{
|
|
386
|
-
type: "text",
|
|
387
|
-
text: assistantMsg.content
|
|
388
|
-
}] : assistantMsg.content).flatMap((block) => {
|
|
389
|
-
if (block.type === "thinking") {
|
|
390
|
-
if (modelBoundThinkingReplayMode === "drop") return [];
|
|
391
|
-
if (block.redacted) return isSameModel ? block : [];
|
|
392
|
-
if (isSameModel && block.thinkingSignature) return block;
|
|
393
|
-
if (!block.thinking || block.thinking.trim() === "") return [];
|
|
394
|
-
if (isSameModel) return block;
|
|
395
|
-
return {
|
|
396
|
-
type: "text",
|
|
397
|
-
text: block.thinking
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
if (block.type === "text") {
|
|
401
|
-
if (isSameModel) return block;
|
|
402
|
-
return {
|
|
403
|
-
type: "text",
|
|
404
|
-
text: block.text
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
if (block.type === "toolCall") {
|
|
408
|
-
const toolCall = block;
|
|
409
|
-
let normalizedToolCall = toolCall;
|
|
410
|
-
if (!isSameModel && toolCall.thoughtSignature) {
|
|
411
|
-
normalizedToolCall = Object.assign({}, toolCall);
|
|
412
|
-
delete normalizedToolCall.thoughtSignature;
|
|
413
|
-
}
|
|
414
|
-
if (!isSameModel && normalizeToolCallId) {
|
|
415
|
-
const normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);
|
|
416
|
-
if (normalizedId !== toolCall.id) {
|
|
417
|
-
toolCallIdMap.set(toolCall.id, normalizedId);
|
|
418
|
-
normalizedToolCall = Object.assign({}, normalizedToolCall, { id: normalizedId });
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return normalizedToolCall;
|
|
422
|
-
}
|
|
423
|
-
return block;
|
|
424
|
-
});
|
|
425
|
-
return Object.assign({}, assistantMsg, { content: transformedContent });
|
|
426
|
-
}
|
|
427
|
-
return msg;
|
|
428
|
-
});
|
|
429
|
-
const result = [];
|
|
430
|
-
let pendingToolCalls = [];
|
|
431
|
-
let existingToolResultIds = /* @__PURE__ */ new Set();
|
|
432
|
-
const insertSyntheticToolResults = () => {
|
|
433
|
-
if (pendingToolCalls.length > 0) {
|
|
434
|
-
for (const tc of pendingToolCalls) if (!existingToolResultIds.has(tc.id)) result.push({
|
|
435
|
-
role: "toolResult",
|
|
436
|
-
toolCallId: tc.id,
|
|
437
|
-
toolName: tc.name,
|
|
438
|
-
content: [{
|
|
439
|
-
type: "text",
|
|
440
|
-
text: "No result provided"
|
|
441
|
-
}],
|
|
442
|
-
isError: true,
|
|
443
|
-
timestamp: Date.now()
|
|
444
|
-
});
|
|
445
|
-
pendingToolCalls = [];
|
|
446
|
-
existingToolResultIds = /* @__PURE__ */ new Set();
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
for (const msg of transformed) if (msg.role === "assistant") {
|
|
450
|
-
insertSyntheticToolResults();
|
|
451
|
-
const assistantMsg = msg;
|
|
452
|
-
if (assistantMsg.stopReason === "error" || assistantMsg.stopReason === "aborted") continue;
|
|
453
|
-
const toolCalls = assistantMsg.content.filter((b) => b.type === "toolCall");
|
|
454
|
-
if (toolCalls.length > 0) {
|
|
455
|
-
pendingToolCalls = toolCalls;
|
|
456
|
-
existingToolResultIds = /* @__PURE__ */ new Set();
|
|
457
|
-
}
|
|
458
|
-
result.push(msg);
|
|
459
|
-
} else if (msg.role === "toolResult") {
|
|
460
|
-
existingToolResultIds.add(msg.toolCallId);
|
|
461
|
-
result.push(msg);
|
|
462
|
-
} else if (msg.role === "user") {
|
|
463
|
-
insertSyntheticToolResults();
|
|
464
|
-
result.push(msg);
|
|
465
|
-
} else result.push(msg);
|
|
466
|
-
insertSyntheticToolResults();
|
|
467
|
-
return result;
|
|
468
|
-
}
|
|
469
|
-
//#endregion
|
|
470
|
-
//#region packages/ai/src/utils/prompt-cache-stability.ts
|
|
471
|
-
/**
|
|
472
|
-
* Prompt-cache normalization helpers. They keep generated prompt sections
|
|
473
|
-
* deterministic across platform newlines, trailing whitespace, and input
|
|
474
|
-
* ordering.
|
|
475
|
-
*/
|
|
476
|
-
/** Normalize structured prompt text before hashing or snapshot comparison. */
|
|
477
|
-
function normalizeStructuredPromptSection(text) {
|
|
478
|
-
return sanitizeSurrogates(text).replace(/\r\n?/g, "\n").replace(/[ \t]+$/gm, "").trim();
|
|
479
|
-
}
|
|
480
|
-
/** Normalize, de-dupe, and sort capability ids for stable prompt payloads. */
|
|
481
|
-
function normalizePromptCapabilityIds(capabilities) {
|
|
482
|
-
const seen = /* @__PURE__ */ new Set();
|
|
483
|
-
const normalized = [];
|
|
484
|
-
for (const capability of capabilities) {
|
|
485
|
-
const value = normalizeLowercaseStringOrEmpty(normalizeStructuredPromptSection(capability));
|
|
486
|
-
if (!value || seen.has(value)) continue;
|
|
487
|
-
seen.add(value);
|
|
488
|
-
normalized.push(value);
|
|
489
|
-
}
|
|
490
|
-
return normalized.toSorted((left, right) => left.localeCompare(right));
|
|
491
|
-
}
|
|
492
|
-
//#endregion
|
|
493
|
-
//#region packages/ai/src/utils/system-prompt-cache-boundary.ts
|
|
494
|
-
/**
|
|
495
|
-
* System prompt cache-boundary helpers.
|
|
496
|
-
*
|
|
497
|
-
* Keeps stable prompt prefixes separate from dynamic runtime additions for provider prompt caching.
|
|
498
|
-
*/
|
|
499
|
-
const SYSTEM_PROMPT_CACHE_BOUNDARY = "\n<!-- OPENCLAW_CACHE_BOUNDARY -->\n";
|
|
500
|
-
function stripSystemPromptCacheBoundary(text) {
|
|
501
|
-
return text.replaceAll(SYSTEM_PROMPT_CACHE_BOUNDARY, "\n");
|
|
502
|
-
}
|
|
503
|
-
function ensureSystemPromptCacheBoundary(systemPrompt) {
|
|
504
|
-
if (systemPrompt.trim().length === 0) return systemPrompt;
|
|
505
|
-
return systemPrompt.includes("\n<!-- OPENCLAW_CACHE_BOUNDARY -->\n") ? systemPrompt : `${systemPrompt}${SYSTEM_PROMPT_CACHE_BOUNDARY}`;
|
|
506
|
-
}
|
|
507
|
-
function splitSystemPromptCacheBoundary(text) {
|
|
508
|
-
const boundaryIndex = text.indexOf(SYSTEM_PROMPT_CACHE_BOUNDARY);
|
|
509
|
-
if (boundaryIndex === -1) return;
|
|
510
|
-
return {
|
|
511
|
-
stablePrefix: text.slice(0, boundaryIndex).trimEnd(),
|
|
512
|
-
dynamicSuffix: text.slice(boundaryIndex + 34).trimStart()
|
|
513
|
-
};
|
|
514
|
-
}
|
|
515
|
-
function prependSystemPromptAdditionAfterCacheBoundary(params) {
|
|
516
|
-
const systemPromptAddition = typeof params.systemPromptAddition === "string" ? normalizeStructuredPromptSection(params.systemPromptAddition) : "";
|
|
517
|
-
if (!systemPromptAddition) return params.systemPrompt;
|
|
518
|
-
if (params.systemPrompt.trim().length === 0) return systemPromptAddition;
|
|
519
|
-
const split = splitSystemPromptCacheBoundary(params.systemPrompt);
|
|
520
|
-
if (!split) return `${systemPromptAddition}\n\n${params.systemPrompt}`;
|
|
521
|
-
const dynamicSuffix = split.dynamicSuffix ? normalizeStructuredPromptSection(split.dynamicSuffix) : "";
|
|
522
|
-
if (!dynamicSuffix) return `${split.stablePrefix}${SYSTEM_PROMPT_CACHE_BOUNDARY}${systemPromptAddition}`;
|
|
523
|
-
return `${split.stablePrefix}${SYSTEM_PROMPT_CACHE_BOUNDARY}${systemPromptAddition}\n\n${dynamicSuffix}`;
|
|
524
|
-
}
|
|
525
|
-
//#endregion
|
|
526
|
-
export { isImageWithMediaPayload as C, buildBaseOptions as D, adjustMaxTokensForThinking as E, clampMaxTokensToModel as O, hasMediaPayload as S, isRecord as T, normalizeOptionalString as _, stripSystemPromptCacheBoundary as a, extractToolResultBlockText as b, transformMessages as c, prepareClaudeSonnet5RequestContext as d, requiresClaudeAdaptiveThinking as f, normalizeLowercaseStringOrEmpty as g, usesClaudeStreamingRefusalContract as h, splitSystemPromptCacheBoundary as i, clampReasoning as k, applyClaudeRequestContract as l, usesClaudeFable5MessagesContract as m, ensureSystemPromptCacheBoundary as n, normalizePromptCapabilityIds as o, resolveModelBoundThinkingReplayMode as p, prependSystemPromptAdditionAfterCacheBoundary as r, normalizeStructuredPromptSection as s, SYSTEM_PROMPT_CACHE_BOUNDARY as t, defaultsClaudeAdaptiveThinking as u, readStringValue as v, truncateUtf16Safe as w, extractToolResultText as x, describeToolResultMediaPlaceholder as y };
|