@openclaw/ai 2026.7.2-beta.7 → 2026.7.34
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 +5 -6
- package/dist/{anthropic-CH4UUnZr.mjs → anthropic-Bcdbq-l5.mjs} +467 -100
- package/dist/{api-registry-DlMgPR39.d.mts → api-registry-BXYnCOIR.d.mts} +2 -1
- package/dist/{azure-openai-responses-CImcwB83.mjs → azure-openai-responses-2xhpNPWg.mjs} +13 -11
- package/dist/{azure-openai-responses-client-compat-C7K7QfUE.mjs → azure-openai-responses-client-compat-a_O_GVQV.mjs} +2 -23
- package/dist/{env-api-keys-DrgeBuva.mjs → env-api-keys-CtMlqaQ4.mjs} +4 -5
- package/dist/{event-stream-YjaPW20U.d.mts → event-stream-0nZeBKl2.d.mts} +1 -1
- package/dist/{event-stream-D8n2uFee.mjs → event-stream-ReMmOTzX.mjs} +6 -14
- package/dist/event-stream.d.mts +1 -1
- package/dist/event-stream.mjs +1 -1
- package/dist/{github-copilot-headers-NCJtz9i0.mjs → github-copilot-headers-BsH5cqGj.mjs} +12 -1
- package/dist/{google-CtSg0iTS.mjs → google-C6P31moq.mjs} +5 -5
- package/dist/{google-shared-DNBz5rcD.mjs → google-shared-BeyXFAcf.mjs} +26 -34
- package/dist/{google-vertex-31f1uS9L.mjs → google-vertex--9_gT74B.mjs} +19 -6
- package/dist/host-4t713IeR.mjs +37 -0
- package/dist/{anthropic-SrGtwsJu.d.mts → index-CGDEoorl.d.mts} +2 -6
- package/dist/index.d.mts +50 -7
- package/dist/index.mjs +5 -5
- package/dist/internal/anthropic.d.mts +61 -21
- package/dist/internal/anthropic.mjs +4 -5
- package/dist/internal/openai.d.mts +89 -98
- package/dist/internal/openai.mjs +6 -8
- package/dist/internal/runtime.d.mts +34 -13
- package/dist/internal/runtime.mjs +14 -26
- package/dist/internal/shared.d.mts +9 -26
- package/dist/internal/shared.mjs +2 -6
- package/dist/{json-parse-BvXNt1-7.mjs → json-parse-DzNSIQBq.mjs} +6 -4
- package/dist/{llm-request-activity-BjtkplhG.mjs → llm-request-activity-CehVkZP-.mjs} +19 -1
- package/dist/{mistral-CWmpvWYh.mjs → mistral-CBm3OQWD.mjs} +39 -141
- package/dist/model-utils-Cn9KAz3z.mjs +69 -0
- package/dist/{openai-chatgpt-responses-B84Ibtrd.mjs → openai-chatgpt-responses-M2RVpycr.mjs} +154 -101
- package/dist/{openai-completions-DsOxhOD1.mjs → openai-completions-BUlMEeBc.mjs} +295 -81
- package/dist/{openai-responses-BT7A3sLu.mjs → openai-responses-DW61C22l.mjs} +9 -14
- package/dist/openai-responses-shared-BhYsVkwW.mjs +1949 -0
- package/dist/{openai-tool-projection-OhX64DoP.mjs → openai-tool-projection-g4HuxD26.mjs} +20 -31
- package/dist/providers.d.mts +2 -3
- package/dist/providers.mjs +10 -11
- package/dist/reasoning-tag-text-partitioner-axhAdUwg.mjs +394 -0
- package/dist/{src-QkygScBs.mjs → src-CjOfhrH3.mjs} +6 -17
- package/dist/{stream-first-event-timeout-BBys9hSb.mjs → stream-first-event-timeout-RjWszj8c.mjs} +22 -2
- package/dist/transform-messages-DoWx35pj.mjs +509 -0
- package/dist/{types-bzp5k29J.d.mts → types-DRgdPqaZ.d.mts} +0 -19
- package/dist/types.d.mts +5 -5
- package/dist/types.mjs +4 -4
- package/dist/{validation-B-j7cOYp.d.mts → validation-BDMWOr8d.d.mts} +1 -1
- package/dist/{validation-DAa_yFOM.mjs → validation-FrchoOlv.mjs} +7 -2
- package/dist/validation.d.mts +1 -1
- package/dist/validation.mjs +1 -1
- package/npm-shrinkwrap.json +645 -0
- package/package.json +11 -36
- package/dist/anthropic-usage-DWU-x8MI.mjs +0 -459
- package/dist/cache-retention-0x979a5V.mjs +0 -12
- package/dist/deferred-event-buffer-DAvyP7qA.mjs +0 -19
- package/dist/error-coercion-DgxlWC0n.mjs +0 -15
- package/dist/host-B9GUmcra.d.mts +0 -173
- package/dist/host-Dog2WQiR.mjs +0 -369
- package/dist/index-BVVgDSdq.d.mts +0 -1
- package/dist/internal/retry-after.d.mts +0 -5
- package/dist/internal/retry-after.mjs +0 -101
- package/dist/number-coercion-DvG7SNMg.mjs +0 -129
- package/dist/openai-completions-compat-DBWjXoMZ.d.mts +0 -43
- package/dist/openai-prompt-cache-2uo_1OR1.d.mts +0 -7
- package/dist/openai-prompt-cache-mZTCdRPo.mjs +0 -12
- package/dist/openai-reasoning-compat-YgeLncHw.mjs +0 -396
- package/dist/openai-responses-shared-pXl6Wd8S.mjs +0 -392
- package/dist/openai-responses-stream-internal-Cw5txaGW.mjs +0 -2964
- package/dist/prompt-cache-stability-Cwcjv_fx.d.mts +0 -13
- package/dist/provider-error-CAEvRjry.mjs +0 -47
- package/dist/provider-options-D8bB3z9b.d.mts +0 -144
- package/dist/reasoning-tag-text-partitioner-CGDyLWUR.mjs +0 -12209
- package/dist/simple-options-9lhRrN73.mjs +0 -50
- package/dist/stream-first-event-timeout-DvDeSucC.d.mts +0 -29
- package/dist/tls-certificate-errors-DXSpluKI.mjs +0 -93
- package/dist/tool-result-text-CTpIRbYd.mjs +0 -225
- package/dist/transform-messages-C8mBqZxF.mjs +0 -2
- package/dist/transport-stream-shared-D81p90xq.mjs +0 -297
- package/dist/transports.d.mts +0 -573
- package/dist/transports.mjs +0 -4110
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
//#region packages/ai/src/providers/simple-options.ts
|
|
2
|
-
function buildBaseOptions(model, options, apiKey) {
|
|
3
|
-
const firstEventOptions = options;
|
|
4
|
-
return {
|
|
5
|
-
temperature: options?.temperature,
|
|
6
|
-
maxTokens: options?.maxTokens,
|
|
7
|
-
stop: options?.stop,
|
|
8
|
-
signal: options?.signal,
|
|
9
|
-
apiKey: apiKey || options?.apiKey,
|
|
10
|
-
transport: options?.transport,
|
|
11
|
-
cacheRetention: options?.cacheRetention,
|
|
12
|
-
sessionId: options?.sessionId,
|
|
13
|
-
promptCacheKey: options?.promptCacheKey,
|
|
14
|
-
headers: options?.headers,
|
|
15
|
-
onPayload: options?.onPayload,
|
|
16
|
-
onResponse: options?.onResponse,
|
|
17
|
-
timeoutMs: options?.timeoutMs,
|
|
18
|
-
firstEventTimeoutMs: firstEventOptions?.firstEventTimeoutMs,
|
|
19
|
-
onFirstEventTimeout: firstEventOptions?.onFirstEventTimeout,
|
|
20
|
-
maxRetries: options?.maxRetries,
|
|
21
|
-
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
22
|
-
metadata: options?.metadata
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
function clampMaxTokensToModel(model, requestedMaxTokens) {
|
|
26
|
-
return requestedMaxTokens === void 0 ? void 0 : Math.max(1, Math.min(requestedMaxTokens, model.maxTokens));
|
|
27
|
-
}
|
|
28
|
-
function clampReasoning(effort) {
|
|
29
|
-
return effort === "xhigh" ? "high" : effort;
|
|
30
|
-
}
|
|
31
|
-
function adjustMaxTokensForThinking(baseMaxTokens, modelMaxTokens, reasoningLevel, customBudgets) {
|
|
32
|
-
const budgets = {
|
|
33
|
-
minimal: 1024,
|
|
34
|
-
low: 2048,
|
|
35
|
-
medium: 8192,
|
|
36
|
-
high: 16384,
|
|
37
|
-
max: 32768,
|
|
38
|
-
...customBudgets
|
|
39
|
-
};
|
|
40
|
-
const minOutputTokens = 1024;
|
|
41
|
-
let thinkingBudget = budgets[clampReasoning(reasoningLevel)];
|
|
42
|
-
const maxTokens = baseMaxTokens === void 0 ? modelMaxTokens : Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);
|
|
43
|
-
if (maxTokens <= thinkingBudget) thinkingBudget = Math.max(0, maxTokens - minOutputTokens);
|
|
44
|
-
return {
|
|
45
|
-
maxTokens,
|
|
46
|
-
thinkingBudget
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
//#endregion
|
|
50
|
-
export { clampReasoning as i, buildBaseOptions as n, clampMaxTokensToModel as r, adjustMaxTokensForThinking as t };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
//#region packages/ai/src/utils/stream-first-event-timeout.d.ts
|
|
2
|
-
type StreamStage = "responses" | "completions";
|
|
3
|
-
type FirstStreamEventTimeoutContext = {
|
|
4
|
-
provider?: string;
|
|
5
|
-
api?: string;
|
|
6
|
-
model?: string;
|
|
7
|
-
timeoutMs: number;
|
|
8
|
-
stage?: StreamStage;
|
|
9
|
-
hint?: string;
|
|
10
|
-
abort?: (reason: Error) => void;
|
|
11
|
-
onTimeout?: (reason: Error) => void;
|
|
12
|
-
};
|
|
13
|
-
type FirstStreamEventInternalOptions = {
|
|
14
|
-
firstEventTimeoutMs?: number;
|
|
15
|
-
abortFirstEventStream?: (reason: Error) => void;
|
|
16
|
-
onFirstEventTimeout?: (reason: Error) => void;
|
|
17
|
-
};
|
|
18
|
-
type FirstStreamEventAbortController = {
|
|
19
|
-
signal: AbortSignal;
|
|
20
|
-
abort: (reason: Error) => void;
|
|
21
|
-
dispose: () => void;
|
|
22
|
-
};
|
|
23
|
-
declare function getFirstStreamEventTimeoutMs(options: unknown): number | undefined;
|
|
24
|
-
declare function getFirstStreamEventTimeoutHandler(options: unknown): ((reason: Error) => void) | undefined;
|
|
25
|
-
declare function createFirstStreamEventTimeoutError(context: FirstStreamEventTimeoutContext): Error;
|
|
26
|
-
declare function createFirstStreamEventAbortController(parentSignal?: AbortSignal): FirstStreamEventAbortController;
|
|
27
|
-
declare function withFirstStreamEventTimeout<T>(stream: AsyncIterable<T>, context: FirstStreamEventTimeoutContext): AsyncIterable<T>;
|
|
28
|
-
//#endregion
|
|
29
|
-
export { createFirstStreamEventTimeoutError as a, withFirstStreamEventTimeout as c, createFirstStreamEventAbortController as i, FirstStreamEventInternalOptions as n, getFirstStreamEventTimeoutHandler as o, FirstStreamEventTimeoutContext as r, getFirstStreamEventTimeoutMs as s, FirstStreamEventAbortController as t };
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
//#region packages/ai/src/utils/tls-certificate-errors.ts
|
|
2
|
-
const HOSTNAME_MISMATCH_CODES = /* @__PURE__ */ new Set(["ERR_TLS_CERT_ALTNAME_INVALID", "HOSTNAME_MISMATCH"]);
|
|
3
|
-
const CERTIFICATE_INVALID_CODES = /* @__PURE__ */ new Set([
|
|
4
|
-
"CERT_CHAIN_TOO_LONG",
|
|
5
|
-
"CERT_HAS_EXPIRED",
|
|
6
|
-
"CERT_NOT_YET_VALID",
|
|
7
|
-
"CERT_REJECTED",
|
|
8
|
-
"CERT_REVOKED",
|
|
9
|
-
"CERT_SIGNATURE_FAILURE",
|
|
10
|
-
"CERT_UNTRUSTED",
|
|
11
|
-
"CRL_HAS_EXPIRED",
|
|
12
|
-
"CRL_NOT_YET_VALID",
|
|
13
|
-
"CRL_SIGNATURE_FAILURE",
|
|
14
|
-
"DEPTH_ZERO_SELF_SIGNED_CERT",
|
|
15
|
-
"ERROR_IN_CERT_NOT_AFTER_FIELD",
|
|
16
|
-
"ERROR_IN_CERT_NOT_BEFORE_FIELD",
|
|
17
|
-
"ERROR_IN_CRL_LAST_UPDATE_FIELD",
|
|
18
|
-
"ERROR_IN_CRL_NEXT_UPDATE_FIELD",
|
|
19
|
-
"INVALID_CA",
|
|
20
|
-
"INVALID_PURPOSE",
|
|
21
|
-
"PATH_LENGTH_EXCEEDED",
|
|
22
|
-
"SELF_SIGNED_CERT_IN_CHAIN",
|
|
23
|
-
"UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY",
|
|
24
|
-
"UNABLE_TO_DECRYPT_CERT_SIGNATURE",
|
|
25
|
-
"UNABLE_TO_DECRYPT_CRL_SIGNATURE",
|
|
26
|
-
"UNABLE_TO_GET_CRL",
|
|
27
|
-
"UNABLE_TO_GET_ISSUER_CERT",
|
|
28
|
-
"UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
|
|
29
|
-
"UNABLE_TO_VERIFY_LEAF_SIGNATURE"
|
|
30
|
-
]);
|
|
31
|
-
const HOSTNAME_MISMATCH_PATTERNS = [
|
|
32
|
-
/hostname\/ip does not match certificate(?:'s)? altnames/i,
|
|
33
|
-
/hostname mismatch/i,
|
|
34
|
-
/host: .+ is not in the cert(?:ificate)?(?:'s)? altnames/i
|
|
35
|
-
];
|
|
36
|
-
const CERTIFICATE_INVALID_PATTERNS = [
|
|
37
|
-
/certificate has expired/i,
|
|
38
|
-
/certificate is not yet valid/i,
|
|
39
|
-
/self[- ]signed certificate/i,
|
|
40
|
-
/unable to get local issuer certificate/i,
|
|
41
|
-
/unable to verify the first certificate/i
|
|
42
|
-
];
|
|
43
|
-
const MAX_TLS_ERROR_DEPTH = 8;
|
|
44
|
-
function classifyCode(code) {
|
|
45
|
-
if (HOSTNAME_MISMATCH_CODES.has(code)) return "hostname_mismatch";
|
|
46
|
-
return CERTIFICATE_INVALID_CODES.has(code) ? "certificate_invalid" : null;
|
|
47
|
-
}
|
|
48
|
-
function classifyMessage(message) {
|
|
49
|
-
if (HOSTNAME_MISMATCH_PATTERNS.some((pattern) => pattern.test(message))) return "hostname_mismatch";
|
|
50
|
-
return CERTIFICATE_INVALID_PATTERNS.some((pattern) => pattern.test(message)) ? "certificate_invalid" : null;
|
|
51
|
-
}
|
|
52
|
-
function inspectTlsCertificateErrorInternal(error, seen, depth) {
|
|
53
|
-
if (depth > MAX_TLS_ERROR_DEPTH) return null;
|
|
54
|
-
if (typeof error === "string") {
|
|
55
|
-
const kind = classifyMessage(error);
|
|
56
|
-
return kind ? {
|
|
57
|
-
kind,
|
|
58
|
-
message: error
|
|
59
|
-
} : null;
|
|
60
|
-
}
|
|
61
|
-
if (!error || typeof error !== "object" || seen.has(error)) return null;
|
|
62
|
-
seen.add(error);
|
|
63
|
-
const candidate = error;
|
|
64
|
-
const code = typeof candidate.code === "string" ? candidate.code.trim().toUpperCase() : void 0;
|
|
65
|
-
const message = typeof candidate.message === "string" && candidate.message.trim() ? candidate.message : void 0;
|
|
66
|
-
const codeKind = code ? classifyCode(code) : null;
|
|
67
|
-
if (code && codeKind) return {
|
|
68
|
-
kind: codeKind,
|
|
69
|
-
code,
|
|
70
|
-
message: message ?? code
|
|
71
|
-
};
|
|
72
|
-
const nestedErrors = Array.isArray(candidate.errors) ? candidate.errors : [];
|
|
73
|
-
for (const nested of [
|
|
74
|
-
candidate.cause,
|
|
75
|
-
candidate.error,
|
|
76
|
-
...nestedErrors
|
|
77
|
-
]) {
|
|
78
|
-
if (nested === void 0 || nested === error) continue;
|
|
79
|
-
const details = inspectTlsCertificateErrorInternal(nested, seen, depth + 1);
|
|
80
|
-
if (details) return details;
|
|
81
|
-
}
|
|
82
|
-
const messageKind = message ? classifyMessage(message) : null;
|
|
83
|
-
return messageKind && message ? {
|
|
84
|
-
kind: messageKind,
|
|
85
|
-
message
|
|
86
|
-
} : null;
|
|
87
|
-
}
|
|
88
|
-
/** Classify deterministic Node/OpenSSL certificate validation failures. */
|
|
89
|
-
function inspectTlsCertificateError(error) {
|
|
90
|
-
return inspectTlsCertificateErrorInternal(error, /* @__PURE__ */ new Set(), 0);
|
|
91
|
-
}
|
|
92
|
-
//#endregion
|
|
93
|
-
export { inspectTlsCertificateError as t };
|
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
import { g as isRecord, n as getAiTransportHost, p as normalizeLowercaseStringOrEmpty } from "./host-Dog2WQiR.mjs";
|
|
2
|
-
import { t as sanitizeSurrogates } from "./sanitize-unicode-DT5o51ur.mjs";
|
|
3
|
-
//#region packages/ai/src/utils/prompt-cache-stability.ts
|
|
4
|
-
/**
|
|
5
|
-
* Prompt-cache normalization helpers. They keep generated prompt sections
|
|
6
|
-
* deterministic across platform newlines, trailing whitespace, and input
|
|
7
|
-
* ordering.
|
|
8
|
-
*/
|
|
9
|
-
/** Canonicalizes provider tool order without relying on host locale settings. */
|
|
10
|
-
function sortPromptCacheToolsByName(tools) {
|
|
11
|
-
const compareText = (left, right) => {
|
|
12
|
-
const leftText = left ?? "";
|
|
13
|
-
const rightText = right ?? "";
|
|
14
|
-
return leftText < rightText ? -1 : leftText > rightText ? 1 : 0;
|
|
15
|
-
};
|
|
16
|
-
return tools.toSorted((left, right) => compareText(left.wireName ?? left.name, right.wireName ?? right.name) || compareText(left.description, right.description));
|
|
17
|
-
}
|
|
18
|
-
/** Normalize structured prompt text before hashing or snapshot comparison. */
|
|
19
|
-
function normalizeStructuredPromptSection(text) {
|
|
20
|
-
return sanitizeSurrogates(text).replace(/\r\n?/g, "\n").replace(/[ \t]+$/gm, "").trim();
|
|
21
|
-
}
|
|
22
|
-
/** Normalize, de-dupe, and sort capability ids for stable prompt payloads. */
|
|
23
|
-
function normalizePromptCapabilityIds(capabilities) {
|
|
24
|
-
const seen = /* @__PURE__ */ new Set();
|
|
25
|
-
const normalized = [];
|
|
26
|
-
for (const capability of capabilities) {
|
|
27
|
-
const value = normalizeLowercaseStringOrEmpty(normalizeStructuredPromptSection(capability));
|
|
28
|
-
if (!value || seen.has(value)) continue;
|
|
29
|
-
seen.add(value);
|
|
30
|
-
normalized.push(value);
|
|
31
|
-
}
|
|
32
|
-
return normalized.toSorted((left, right) => left.localeCompare(right));
|
|
33
|
-
}
|
|
34
|
-
//#endregion
|
|
35
|
-
//#region packages/ai/src/utils/system-prompt-cache-boundary.ts
|
|
36
|
-
/**
|
|
37
|
-
* System prompt cache-boundary helpers.
|
|
38
|
-
*
|
|
39
|
-
* Keeps stable prompt prefixes separate from dynamic runtime additions for provider prompt caching.
|
|
40
|
-
*/
|
|
41
|
-
const SYSTEM_PROMPT_CACHE_BOUNDARY = "\n<!-- OPENCLAW_CACHE_BOUNDARY -->\n";
|
|
42
|
-
function stripSystemPromptCacheBoundary(text) {
|
|
43
|
-
return text.replaceAll(SYSTEM_PROMPT_CACHE_BOUNDARY, "\n");
|
|
44
|
-
}
|
|
45
|
-
function ensureSystemPromptCacheBoundary(systemPrompt) {
|
|
46
|
-
if (systemPrompt.trim().length === 0) return systemPrompt;
|
|
47
|
-
return systemPrompt.includes("\n<!-- OPENCLAW_CACHE_BOUNDARY -->\n") ? systemPrompt : `${systemPrompt}${SYSTEM_PROMPT_CACHE_BOUNDARY}`;
|
|
48
|
-
}
|
|
49
|
-
function splitSystemPromptCacheBoundary(text) {
|
|
50
|
-
const boundaryIndex = text.indexOf(SYSTEM_PROMPT_CACHE_BOUNDARY);
|
|
51
|
-
if (boundaryIndex === -1) return;
|
|
52
|
-
return {
|
|
53
|
-
stablePrefix: text.slice(0, boundaryIndex).trimEnd(),
|
|
54
|
-
dynamicSuffix: text.slice(boundaryIndex + 34).trimStart()
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
function prependSystemPromptAdditionAfterCacheBoundary(params) {
|
|
58
|
-
const systemPromptAddition = typeof params.systemPromptAddition === "string" ? normalizeStructuredPromptSection(params.systemPromptAddition) : "";
|
|
59
|
-
if (!systemPromptAddition) return params.systemPrompt;
|
|
60
|
-
if (params.systemPrompt.trim().length === 0) return systemPromptAddition;
|
|
61
|
-
const split = splitSystemPromptCacheBoundary(params.systemPrompt);
|
|
62
|
-
if (!split) return `${systemPromptAddition}\n\n${params.systemPrompt}`;
|
|
63
|
-
const dynamicSuffix = split.dynamicSuffix ? normalizeStructuredPromptSection(split.dynamicSuffix) : "";
|
|
64
|
-
if (!dynamicSuffix) return `${split.stablePrefix}${SYSTEM_PROMPT_CACHE_BOUNDARY}${systemPromptAddition}`;
|
|
65
|
-
return `${split.stablePrefix}${SYSTEM_PROMPT_CACHE_BOUNDARY}${systemPromptAddition}\n\n${dynamicSuffix}`;
|
|
66
|
-
}
|
|
67
|
-
//#endregion
|
|
68
|
-
//#region packages/normalization-core/src/utf16-slice.ts
|
|
69
|
-
function isHighSurrogate(codeUnit) {
|
|
70
|
-
return codeUnit >= 55296 && codeUnit <= 56319;
|
|
71
|
-
}
|
|
72
|
-
function isLowSurrogate(codeUnit) {
|
|
73
|
-
return codeUnit >= 56320 && codeUnit <= 57343;
|
|
74
|
-
}
|
|
75
|
-
/** Slices a UTF-16 string without returning dangling surrogate halves at either edge. */
|
|
76
|
-
function sliceUtf16Safe(input, start, end) {
|
|
77
|
-
const len = input.length;
|
|
78
|
-
let from = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
|
|
79
|
-
let to = end === void 0 ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
|
|
80
|
-
if (to <= from) return "";
|
|
81
|
-
if (from > 0 && from < len) {
|
|
82
|
-
if (isLowSurrogate(input.charCodeAt(from)) && isHighSurrogate(input.charCodeAt(from - 1))) from += 1;
|
|
83
|
-
}
|
|
84
|
-
if (to > 0 && to < len) {
|
|
85
|
-
if (isHighSurrogate(input.charCodeAt(to - 1)) && isLowSurrogate(input.charCodeAt(to))) to -= 1;
|
|
86
|
-
}
|
|
87
|
-
return input.slice(from, to);
|
|
88
|
-
}
|
|
89
|
-
/** Truncates a UTF-16 string without cutting a surrogate pair in half. */
|
|
90
|
-
function truncateUtf16Safe(input, maxLen) {
|
|
91
|
-
const limit = Math.max(0, Math.floor(maxLen));
|
|
92
|
-
if (input.length <= limit) return input;
|
|
93
|
-
return sliceUtf16Safe(input, 0, limit);
|
|
94
|
-
}
|
|
95
|
-
//#endregion
|
|
96
|
-
//#region packages/ai/src/providers/tool-result-text.ts
|
|
97
|
-
const PROVIDER_TOOL_RESULT_MAX_CHARS = 8e3;
|
|
98
|
-
const IMAGE_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([
|
|
99
|
-
"image",
|
|
100
|
-
"image_url",
|
|
101
|
-
"input_image"
|
|
102
|
-
]);
|
|
103
|
-
const AUDIO_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([
|
|
104
|
-
"audio",
|
|
105
|
-
"input_audio",
|
|
106
|
-
"output_audio"
|
|
107
|
-
]);
|
|
108
|
-
const MEDIA_ONLY_TOOL_RESULT_TYPES = /* @__PURE__ */ new Set([...IMAGE_TOOL_RESULT_TYPES, ...AUDIO_TOOL_RESULT_TYPES]);
|
|
109
|
-
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;
|
|
110
|
-
const MIME_KEY_CANDIDATES = [
|
|
111
|
-
"mimeType",
|
|
112
|
-
"mime_type",
|
|
113
|
-
"mediaType",
|
|
114
|
-
"media_type",
|
|
115
|
-
"contentType",
|
|
116
|
-
"content_type"
|
|
117
|
-
];
|
|
118
|
-
const TEXTUAL_MIME_PATTERN = /^(?:text\/|application\/(?:json|ld\+json|x-ndjson|xml|javascript|x-www-form-urlencoded)|[^/]+\/[^+]+\+(?:json|xml)$)/i;
|
|
119
|
-
const OPAQUE_OR_BINARY_FIELD_RE = /^(?:blob|buffer|bytes|encrypted_content|encrypted_stdout)$/i;
|
|
120
|
-
function readMimeType(value) {
|
|
121
|
-
if (!isRecord(value)) return;
|
|
122
|
-
for (const key of MIME_KEY_CANDIDATES) {
|
|
123
|
-
const mimeType = value[key];
|
|
124
|
-
if (typeof mimeType === "string" && mimeType.trim().length > 0) return mimeType;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function isBinaryMimeType(mimeType) {
|
|
128
|
-
const normalized = mimeType.split(";", 1)[0]?.trim().toLowerCase();
|
|
129
|
-
return normalized ? !TEXTUAL_MIME_PATTERN.test(normalized) : false;
|
|
130
|
-
}
|
|
131
|
-
function describeOmittedValue(value, label) {
|
|
132
|
-
const length = typeof value === "string" ? value.length : JSON.stringify(value)?.length;
|
|
133
|
-
return length ? `[${label} omitted: ${length} chars]` : `[${label} omitted]`;
|
|
134
|
-
}
|
|
135
|
-
function redactInlineDataUris(value) {
|
|
136
|
-
return value.replace(INLINE_DATA_URI_PATTERN, (_match, prefix, uri) => `${prefix}[inline data URI: ${uri.length} chars]`);
|
|
137
|
-
}
|
|
138
|
-
function redactStructuredTextValue(value) {
|
|
139
|
-
const host = getAiTransportHost();
|
|
140
|
-
const redacted = host.redactToolPayloadText(value);
|
|
141
|
-
const trimmed = redacted.trim();
|
|
142
|
-
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return redacted;
|
|
143
|
-
try {
|
|
144
|
-
const redactedWrapper = host.redactSecrets({ structuredTextValue: JSON.parse(redacted) });
|
|
145
|
-
return JSON.stringify(redactedWrapper.structuredTextValue);
|
|
146
|
-
} catch {
|
|
147
|
-
return redacted;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
function stringifyStructuredBlock(block) {
|
|
151
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
152
|
-
try {
|
|
153
|
-
const redactedBlock = getAiTransportHost().redactSecrets({ structuredToolResult: block }).structuredToolResult;
|
|
154
|
-
const serialized = JSON.stringify(redactedBlock, function structuredToolResultReplacer(key, value) {
|
|
155
|
-
if (OPAQUE_OR_BINARY_FIELD_RE.test(key)) return `[omitted ${key}]`;
|
|
156
|
-
if (key === "data") {
|
|
157
|
-
const mimeType = readMimeType(this);
|
|
158
|
-
if (mimeType && isBinaryMimeType(mimeType)) return describeOmittedValue(value, "binary data");
|
|
159
|
-
}
|
|
160
|
-
if (typeof value === "bigint") return value.toString();
|
|
161
|
-
if (typeof value === "string") return redactInlineDataUris(redactStructuredTextValue(value));
|
|
162
|
-
if (typeof value === "function" || typeof value === "symbol" || value === void 0) return;
|
|
163
|
-
if (!value || typeof value !== "object") return value;
|
|
164
|
-
if (seen.has(value)) return "[Circular]";
|
|
165
|
-
seen.add(value);
|
|
166
|
-
return value;
|
|
167
|
-
});
|
|
168
|
-
if (!serialized || serialized === "{}") return;
|
|
169
|
-
return serialized;
|
|
170
|
-
} catch {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
function truncateProviderToolText(text) {
|
|
175
|
-
if (text.length <= PROVIDER_TOOL_RESULT_MAX_CHARS) return text;
|
|
176
|
-
return `${truncateUtf16Safe(text, PROVIDER_TOOL_RESULT_MAX_CHARS)}\n…(truncated)…`;
|
|
177
|
-
}
|
|
178
|
-
/** Media metadata alone is not an attachment; provider emitters need inline bytes. */
|
|
179
|
-
function hasMediaPayload(block) {
|
|
180
|
-
return isRecord(block) && typeof block.data === "string" && block.data.trim().length > 0;
|
|
181
|
-
}
|
|
182
|
-
/** Image metadata alone is not an attachment; provider emitters need inline bytes. */
|
|
183
|
-
function isImageWithMediaPayload(block) {
|
|
184
|
-
return isRecord(block) && block.type === "image" && hasMediaPayload(block);
|
|
185
|
-
}
|
|
186
|
-
function describeToolResultMediaPlaceholder(blocks) {
|
|
187
|
-
let hasImage = false;
|
|
188
|
-
let hasAudio = false;
|
|
189
|
-
for (const block of blocks) {
|
|
190
|
-
if (!hasMediaPayload(block)) continue;
|
|
191
|
-
const record = block;
|
|
192
|
-
const type = typeof record.type === "string" ? record.type : void 0;
|
|
193
|
-
const mimeType = readMimeType(record);
|
|
194
|
-
if (type && IMAGE_TOOL_RESULT_TYPES.has(type) || mimeType?.toLowerCase().startsWith("image/")) hasImage = true;
|
|
195
|
-
if (type && AUDIO_TOOL_RESULT_TYPES.has(type) || mimeType?.toLowerCase().startsWith("audio/")) hasAudio = true;
|
|
196
|
-
}
|
|
197
|
-
if (hasImage && hasAudio) return "(see attached media)";
|
|
198
|
-
if (hasAudio) return "(see attached audio)";
|
|
199
|
-
if (hasImage) return "(see attached image)";
|
|
200
|
-
}
|
|
201
|
-
function extractToolResultBlockText(block) {
|
|
202
|
-
if (!block || typeof block !== "object") return;
|
|
203
|
-
const record = block;
|
|
204
|
-
if (typeof record.type === "string" && MEDIA_ONLY_TOOL_RESULT_TYPES.has(record.type)) return;
|
|
205
|
-
if (record.type === "text") {
|
|
206
|
-
const text = typeof record.text === "string" ? record.text : "";
|
|
207
|
-
return text ? sanitizeSurrogates(text) : void 0;
|
|
208
|
-
}
|
|
209
|
-
const structured = stringifyStructuredBlock(record);
|
|
210
|
-
return structured ? sanitizeSurrogates(truncateProviderToolText(structured)) : void 0;
|
|
211
|
-
}
|
|
212
|
-
function extractToolResultText(blocks) {
|
|
213
|
-
const explicitTexts = [];
|
|
214
|
-
const structuredTexts = [];
|
|
215
|
-
for (const block of blocks) {
|
|
216
|
-
const text = extractToolResultBlockText(block);
|
|
217
|
-
if (!text) continue;
|
|
218
|
-
if (block.type === "text") explicitTexts.push(text);
|
|
219
|
-
else structuredTexts.push(text);
|
|
220
|
-
}
|
|
221
|
-
if (explicitTexts.length > 0) return sanitizeSurrogates(explicitTexts.join("\n"));
|
|
222
|
-
return sanitizeSurrogates(truncateProviderToolText(structuredTexts.join("\n")));
|
|
223
|
-
}
|
|
224
|
-
//#endregion
|
|
225
|
-
export { isImageWithMediaPayload as a, ensureSystemPromptCacheBoundary as c, stripSystemPromptCacheBoundary as d, normalizePromptCapabilityIds as f, hasMediaPayload as i, prependSystemPromptAdditionAfterCacheBoundary as l, sortPromptCacheToolsByName as m, extractToolResultBlockText as n, truncateUtf16Safe as o, normalizeStructuredPromptSection as p, extractToolResultText as r, SYSTEM_PROMPT_CACHE_BOUNDARY as s, describeToolResultMediaPlaceholder as t, splitSystemPromptCacheBoundary as u };
|