openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
154 lines (153 loc) • 5.17 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import "./src-H3RMXyFL.mjs";
import { t as stableStringify } from "./stable-stringify-vqde_3tT.mjs";
import { n as isToolResultError } from "./tool-result-error-B2GHe83l.mjs";
import { t as createMcpStructuredContentMirrorMatcher } from "./mcp-structured-mirror-CtppLJbX.mjs";
//#region src/agents/tool-search-json.ts
/** Convert bridge and transcript values into detached JSON-compatible data. */
function toToolSearchJsonSafe(e) {
try {
const t = JSON.stringify(e);
return t === void 0 ? null : JSON.parse(t);
} catch {
if (e instanceof Error) return e.message;
return typeof e === "bigint" || typeof e === "function" ? String(e) : Object.prototype.toString.call(e);
}
}
//#endregion
//#region src/agents/mcp-content.ts
const mcpCodeModeGuestResults = /* @__PURE__ */ new WeakMap();
function setMcpCodeModeGuestResult(e, t) {
mcpCodeModeGuestResults.set(e, t);
return e;
}
function setMcpCodeModeGuestResultFromAgentResult(e) {
return setMcpCodeModeGuestResult(e, {
content: e.content,
isError: isToolResultError(e)
});
}
function transferMcpCodeModeGuestResult(e, t) {
if (mcpCodeModeGuestResults.has(e)) {
mcpCodeModeGuestResults.set(t, mcpCodeModeGuestResults.get(e));
mcpCodeModeGuestResults.delete(e);
}
return t;
}
function consumeMcpCodeModeGuestResult(e) {
const t = mcpCodeModeGuestResults.get(e);
if (!mcpCodeModeGuestResults.delete(e)) return;
const n = toToolSearchJsonSafe(t);
if (isRecord(n)) delete n._meta;
return n;
}
function stringifyMcpContent(e) {
try {
return JSON.stringify(e) ?? String(e);
} catch {
return String(e);
}
}
/** Converts untrusted MCP content into the agent text/image contract. */
function mcpContentBlockToAgentContent(e) {
if (!isRecord(e)) return {
type: "text",
text: stringifyMcpContent(e)
};
switch (e.type) {
case "text":
if (typeof e.text === "string") return {
type: "text",
text: e.text
};
break;
case "image":
if (typeof e.data === "string" && typeof e.mimeType === "string") return {
type: "image",
data: e.data,
mimeType: e.mimeType
};
break;
case "audio":
if (typeof e.mimeType === "string") return {
type: "text",
text: `[audio ${e.mimeType}]`
};
break;
case "resource_link": {
if (typeof e.uri !== "string") break;
const t = typeof e.title === "string" ? e.title : typeof e.name === "string" ? e.name : void 0;
return {
type: "text",
text: t ? `[${t}] ${e.uri}` : e.uri
};
}
case "resource":
if (!isRecord(e.resource) || typeof e.resource.uri !== "string") break;
return {
type: "text",
text: (typeof e.resource.text === "string" ? e.resource.text : void 0) ?? e.resource.uri
};
}
return {
type: "text",
text: stringifyMcpContent(e)
};
}
function projectMcpCallToolResultContent(e, t) {
if (isRecord(t)) try {
const n = createMcpStructuredContentMirrorMatcher(t);
return { content: [{
type: "text",
text: `structuredContent:\n${JSON.stringify(JSON.parse(stableStringify(t)))}`
}, ...e.filter((e) => !isRecord(e) || e.type !== "text" || typeof e.text !== "string" || !n(e.text)).map(mcpContentBlockToAgentContent)] };
} catch (t) {
if (!(t instanceof RangeError)) throw t;
return {
content: [{
type: "text",
text: "structuredContent was too deeply nested to project. Ask the MCP server for a flatter result or query a specific field."
}, ...e.map(mcpContentBlockToAgentContent)],
unprojectable: true
};
}
return { content: e.map(mcpContentBlockToAgentContent) };
}
/** Projects a raw MCP CallToolResult exactly once at the model boundary. */
function projectMcpCallToolResult(e, t = {}) {
const n = Array.isArray(e.content) ? e.content : [];
const r = projectMcpCallToolResultContent(n, e.structuredContent);
const i = r.unprojectable === true;
const a = e.isError === true || i;
const o = r.content;
const s = {
content: o.length > 0 ? o : [{
type: "text",
text: a ? "MCP tool failed without returning content." : "MCP tool completed without returning content."
}],
details: {
...t,
...e.structuredContent !== void 0 && !i ? { structuredContent: e.structuredContent } : {},
...a ? { status: "error" } : {}
}
};
return setMcpCodeModeGuestResult(s, {
content: i ? s.content : n,
...e.structuredContent !== void 0 && !i ? { structuredContent: e.structuredContent } : {},
...typeof e.isError === "boolean" || i ? { isError: a } : {}
});
}
/** Keep template roles descriptive while projecting its content for the model. */
function projectMcpGetPromptResult(e, t) {
const n = e.messages.flatMap(({ role: e, content: t }) => [{
type: "text",
text: `${e}:`
}, t]);
if (e.description !== void 0) n.unshift({
type: "text",
text: e.description
});
return setMcpCodeModeGuestResult(projectMcpCallToolResult({ content: n }, t), toToolSearchJsonSafe(e));
}
//#endregion
export { setMcpCodeModeGuestResultFromAgentResult as a, setMcpCodeModeGuestResult as i, projectMcpCallToolResult as n, transferMcpCodeModeGuestResult as o, projectMcpGetPromptResult as r, toToolSearchJsonSafe as s, consumeMcpCodeModeGuestResult as t };