UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.09 kB
import"./src-0ci69Prg.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as stableStringify}from"./stable-stringify-Cy0VZ0ci.mjs";import{t as isToolResultError}from"./tool-result-error-DAdEhHzf.mjs";import{t as createMcpStructuredContentMirrorMatcher}from"./mcp-structured-mirror-DItchU0L.mjs"; //!src/agents/tool-search-json.ts 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)}} //!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)}}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)}}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}:{}})}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))}export{setMcpCodeModeGuestResultFromAgentResult as a,setMcpCodeModeGuestResult as i,projectMcpCallToolResult as n,transferMcpCodeModeGuestResult as o,projectMcpGetPromptResult as r,toToolSearchJsonSafe as s,consumeMcpCodeModeGuestResult as t};