openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
220 lines (219 loc) • 9.23 kB
JavaScript
import { c as normalizeOptionalLowercaseString, g as readStringValue, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord, i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { m as tryParseJson } from "./utils-C8YECn2Q.mjs";
import { n as estimateBase64DecodedBytes } from "./base64-CnKpkbff.mjs";
import { t as collectTextContentBlocks } from "./content-blocks-DgfxPwgj.mjs";
import { a as readToolResultDetails, n as isToolResultError, o as readToolResultStatus } from "./tool-result-error-B2GHe83l.mjs";
import { t as readNodePermissionError } from "./node-permission-error-FsjZ46I7.mjs";
//#region src/agents/embedded-agent-tool-results.ts
const TOOL_RESULT_MAX_CHARS = 8e3;
const TOOL_ERROR_MAX_CHARS = 400;
const LIVE_EXEC_OUTPUT_MAX_CHARS = 8e3;
const TOOL_DENIAL_ERROR_CODES = ["SYSTEM_RUN_DENIED", "INVALID_REQUEST"];
const OPAQUE_STRUCTURED_RESULT_FIELDS = /* @__PURE__ */ new Set(["encrypted_content", "encrypted_stdout"]);
/** Recognize work accepted by a tool whose background task owns completion. */
function isAsyncStartedToolResult(e) {
const t = readToolResultDetails(e);
return t?.async === true && t.status === "started";
}
/** Preserve the accepted task's identity independently of result presentation. */
function readAsyncStartedTaskIds(e) {
const t = readToolResultDetails(e);
if (!t) return {};
const n = asOptionalRecord(t.task);
const r = readStringValue(t.runId) ?? readStringValue(n?.runId);
const i = readStringValue(t.taskId) ?? readStringValue(n?.taskId);
return {
...r ? { asyncTaskRunId: r } : {},
...i ? { asyncTaskId: i } : {}
};
}
function truncateToolText(e, t = TOOL_RESULT_MAX_CHARS, n = "\n…(truncated)…") {
if (e.length <= t) return e;
return `${truncateUtf16Safe(e, t)}${n}`;
}
function truncateLiveExecOutput(e) {
return truncateToolText(e, LIVE_EXEC_OUTPUT_MAX_CHARS, "\n...(live output truncated)...");
}
function capLiveExecResult(e) {
const t = asOptionalRecord(e);
const n = readToolResultDetails(t);
if (!n || typeof n.status !== "string" || typeof n.aggregated !== "string") return e;
const { aggregated: r, ...i } = n;
const a = t?.content;
const o = Array.isArray(a) && a.some((e) => {
const t = asOptionalObjectRecord(e);
return t?.type === "text" && typeof t.text === "string" && t.text.length > 0;
});
return {
...t,
details: o ? i : {
...i,
aggregated: truncateLiveExecOutput(r)
}
};
}
function normalizeToolErrorText(e) {
const t = e.trimStart().split(/\r?\n/, 1)[0]?.trim();
if (!t) return;
return truncateToolText(t, TOOL_ERROR_MAX_CHARS, "…");
}
function readErrorCandidate(e) {
if (typeof e === "string") return normalizeToolErrorText(e);
const t = asOptionalObjectRecord(e);
if (typeof t?.message === "string") return normalizeToolErrorText(t.message);
if (typeof t?.error === "string") return normalizeToolErrorText(t.error);
}
function extractErrorField(e) {
const t = asOptionalObjectRecord(e);
if (!t) return;
const n = extractDirectErrorField(t);
if (n) return n;
const r = normalizeOptionalString(t.status);
return r && /error|fail|timeout|timed[_\s-]?out|denied|cancel|invalid|forbidden/.test(r.toLowerCase()) ? normalizeToolErrorText(r) : void 0;
}
function extractDirectErrorField(e) {
const t = asOptionalObjectRecord(e);
return readErrorCandidate(t?.error) ?? readErrorCandidate(t?.message) ?? readErrorCandidate(t?.reason);
}
function readDenialErrorCodeFromMessage(e) {
const t = normalizeOptionalString(e);
if (!t) return;
return TOOL_DENIAL_ERROR_CODES.find((e) => t === e || t.startsWith(`${e}:`));
}
function extractErrorCodeField(e, t = false) {
const n = asOptionalObjectRecord(e);
return (t ? readDenialErrorCodeFromMessage(n?.message) ?? readDenialErrorCodeFromMessage(n?.error) : extractErrorCodeField(n?.error, true) ?? extractErrorCodeField(n?.nodeError, true)) ?? normalizeOptionalString(n?.code) ?? normalizeOptionalString(n?.gatewayCode);
}
function buildToolLifecycleErrorResult(e) {
const t = asOptionalRecord(e);
const n = asOptionalRecord(t?.details);
const r = asOptionalRecord(n?.nodeError);
const i = readNodePermissionError(e);
const a = normalizeOptionalString(t?.gatewayCode) ?? normalizeOptionalString(t?.code);
const o = e instanceof Error ? e.message : String(e);
return {
content: [{
type: "text",
text: o
}],
details: {
status: "error",
error: o,
...a ? { gatewayCode: a } : {},
...r ? { nodeError: r } : {},
...i ? { permissionMissing: i } : {}
}
};
}
function sanitizeToolResult(e) {
if (!e || typeof e !== "object") return e;
return sanitizeStructuredToolResult(e);
}
function prepareToolResult(e) {
return () => sanitizeToolResult(e);
}
function sanitizeStructuredToolResult(e) {
if (Array.isArray(e)) return e;
const t = e;
const n = { ...t };
if (Array.isArray(t.content)) n.content = t.content.map((e) => {
if (!e || typeof e !== "object") return e;
const t = e;
if (readStringValue(t.type) === "image") {
const e = readStringValue(t.data);
const n = typeof t.bytes === "number" ? t.bytes : void 0;
const r = e === void 0 ? n : estimateBase64DecodedBytes(e);
const i = { ...t };
delete i.data;
return Object.assign(i, {
bytes: r,
omitted: true
});
}
if (readStringValue(t.type) === "text" && typeof t.text === "string") return Object.assign({ ...t }, { text: truncateToolText(t.text) });
return t;
});
return n;
}
const INLINE_DATA_URI_VALUE_PATTERN = /^data:(?:[a-z][a-z0-9.+-]*\/[a-z0-9.+-]+)?(?:;[a-z0-9.+-]+(?:=[^,;"'\s]+)?)*,/i;
function omitInlineDataUriValue(e) {
const t = e.trimStart();
if (!INLINE_DATA_URI_VALUE_PATTERN.test(t)) return e;
return `[inline data URI: ${e.length} chars]`;
}
function carriesBinaryData(e) {
const t = normalizeOptionalLowercaseString(e.type);
if (t === "audio" || t === "image" || t === "base64") return true;
const n = normalizeOptionalLowercaseString(e.media_type ?? e.mimeType);
return n?.startsWith("image/") === true || n?.startsWith("audio/") === true || n?.startsWith("video/") === true || n === "application/pdf";
}
function prepareStructuredToolResultValue(e, t, n = false, r = /* @__PURE__ */ new WeakSet()) {
if (typeof e === "string") {
if (t === "blob" || t === "data" && n) return `[binary omitted: ${e.length} chars]`;
if (t && OPAQUE_STRUCTURED_RESULT_FIELDS.has(t)) return `[opaque data omitted: ${e.length} chars]`;
return truncateToolText(omitInlineDataUriValue(e));
}
if (typeof e === "bigint") return e.toString();
if (!e || typeof e !== "object") return e;
if (r.has(e)) return "[Circular]";
r.add(e);
if (Array.isArray(e)) return e.map((e) => prepareStructuredToolResultValue(e, t, n, r));
const i = e;
const a = carriesBinaryData(i);
return Object.fromEntries(Object.entries(i).map(([e, t]) => [e, prepareStructuredToolResultValue(t, e, a, r)]));
}
function stringifyStructuredToolResultContent(e) {
if (!e || typeof e !== "object") return;
const t = e;
const n = readStringValue(t.type);
if (n === "text" || n === "image" || n === "image_url" || n === "audio") return;
try {
const e = JSON.stringify(prepareStructuredToolResultValue(t));
return e && e !== "{}" ? e : void 0;
} catch {
return;
}
}
function resolveToolResultContentBlocks(e) {
if (Array.isArray(e)) return e;
const t = e;
if (readStringValue(t.type)) return [t];
if (Array.isArray(t.content)) return t.content;
if (t.content && typeof t.content === "object") return [t.content];
return [t];
}
function extractToolResultText(e) {
if (typeof e === "string") {
const t = omitInlineDataUriValue(e).trim();
return t ? truncateToolText(t) : void 0;
}
if (!e || typeof e !== "object") return;
const t = resolveToolResultContentBlocks(e);
const n = collectTextContentBlocks(t).map((e) => e.trim()).filter(Boolean);
const r = n.length > 0 ? n : t.map(stringifyStructuredToolResultContent).filter(Boolean);
return r.length > 0 ? truncateToolText(r.join("\n")) : void 0;
}
function extractToolErrorCode(e) {
const t = asOptionalObjectRecord(e);
return extractErrorCodeField(t?.details) ?? extractErrorCodeField(t);
}
function isToolResultTimedOut(e) {
return readToolResultStatus(e) === "timeout" || readToolResultDetails(e)?.timedOut === true;
}
function extractToolErrorMessage(e) {
if (!e || typeof e !== "object") return;
const t = e;
const n = extractDirectErrorField(t.details) ?? readErrorCandidate(asOptionalObjectRecord(t.details)?.aggregated) ?? extractDirectErrorField(t);
if (n) return n;
const r = extractToolResultText(e);
const i = r ? extractErrorField(tryParseJson(r)) : void 0;
if (i) return i;
const a = extractErrorField(t.details) ?? extractErrorField(t);
if (a) return a;
if (readToolResultStatus(e) && !isToolResultError(e)) return;
return r ? normalizeToolErrorText(r) : void 0;
}
//#endregion
export { extractToolResultText as a, prepareToolResult as c, truncateLiveExecOutput as d, extractToolErrorMessage as i, readAsyncStartedTaskIds as l, capLiveExecResult as n, isAsyncStartedToolResult as o, extractToolErrorCode as r, isToolResultTimedOut as s, buildToolLifecycleErrorResult as t, sanitizeToolResult as u };