openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines • 6.38 kB
JavaScript
import{a as asOptionalRecord}from"./record-coerce-BTbqdJ3-.mjs";import{n as notifyListeners}from"./listeners-awzYn4LE.mjs";import{o as escapeInternalRuntimeContextDelimiters}from"./internal-runtime-context-DervRrPi.mjs";import{i as logWarn}from"./logger-BHIcoxTH.mjs";import{t as SESSION_MCP_RUNTIME_MANAGER_KEY}from"./agent-bundle-mcp-runtime-shared-CjKU-8u7.mjs";import{randomUUID}from"node:crypto";import{ContentBlockSchema}from"@modelcontextprotocol/core";
//!src/agents/agent-bundle-mcp-manager-cleanup.ts
function peekSessionMcpRuntimeManager(){const e=globalThis;if(!Object.hasOwn(e,SESSION_MCP_RUNTIME_MANAGER_KEY))return;return e[SESSION_MCP_RUNTIME_MANAGER_KEY]}function listAgentSessionMcpRuntimeIds(e){return peekSessionMcpRuntimeManager()?.listSessionIdsForAgent(e)??[]}async function retireSessionMcpRuntimeForAgentDeletion(e){e.assertCurrent();await peekSessionMcpRuntimeManager()?.retireSessionForAgentDeletion(e.sessionId,e.agentId,e.assertCurrent)}async function releaseSessionMcpRuntime(e,t){e.releaseLease?.();try{if(t)await e.retireUnusedServers?.(t)}catch(e){logWarn(`bundle-mcp: unused server cleanup failed: ${String(e)}`)}finally{await completeDeferredSessionMcpRuntimeRetirement(e.runtime).catch(e=>{logWarn(`bundle-mcp: deferred runtime cleanup failed: ${String(e)}`)})}}async function completeDeferredSessionMcpRuntimeRetirement(e){return await(peekSessionMcpRuntimeManager()?.completeDeferredRetirement(e.sessionId,e)??false)}
//!src/agents/mcp-app-model-context.ts
const MCP_APP_MODEL_CONTEXT_MAX_BYTES=6291456;const contexts=/* @__PURE__ */ new WeakMap;const listeners=/* @__PURE__ */ new WeakMap;function notify(e,t){notifyListeners(listeners.get(e)??[],t)}function subscribeMcpAppModelContext(e,t){const n=listeners.get(e)??/* @__PURE__ */ new Set;n.add(t);listeners.set(e,n);return()=>{n.delete(t);if(!n.size&&listeners.get(e)===n)listeners.delete(e)}}function getMcpAppModelContext(e,t){const n=contexts.get(e)?.get(t);if(!n||e.mcpAppModelContextRevoked)return null;const{leased:r,...i}=n;return structuredClone(i)}function revokeMcpAppModelContext(e){e.mcpAppModelContextRevoked=true;const t=contexts.get(e);contexts.delete(e);for(const e of t?.keys()??[])notify(e,null)}function allowMcpAppModelContext(e){e.mcpAppModelContextRevoked=void 0}function clearMcpAppModelContextForView(e,t){if(contexts.get(e)?.delete(t))notify(t,null)}function updateMcpAppModelContext(e,t,n){if(e.mcpAppModelContextRevoked===true)throw new Error(`MCP App model context is unavailable for this session`);if(n.content!==void 0&&!Array.isArray(n.content))throw new Error(`MCP App model context content must be an array`);const r=n.content?.map(e=>{const t=ContentBlockSchema.parse(e);if(t.type===`audio`)throw new Error(`MCP App audio context is unsupported`);return t});const i=asOptionalRecord(n.structuredContent);if(n.structuredContent!==void 0&&!i)throw new Error(`MCP App structured context must be an object`);const a={...r?.length?{content:r}:{},...i!==void 0?{structuredContent:i}:{}};const o=JSON.stringify(a);const s=contexts.get(e)??/* @__PURE__ */ new Map;const c=s.get(t);const{updateId:l,leased:u,...d}=c??{};const f=c&&JSON.stringify(d)===o?c.updateId:randomUUID();if(Buffer.byteLength(o,`utf8`)+[...s.entries()].filter(([e])=>e!==t).reduce((e,[,t])=>e+Buffer.byteLength(JSON.stringify(t),`utf8`),0)>MCP_APP_MODEL_CONTEXT_MAX_BYTES)throw new Error(`MCP App model context exceeds ${MCP_APP_MODEL_CONTEXT_MAX_BYTES} bytes`);if(!a.content&&a.structuredContent===void 0)s.delete(t);else if(c?.updateId!==f)s.set(t,{...structuredClone(a),updateId:f});contexts.set(e,s);notify(t,getMcpAppModelContext(e,t));return{_meta:{"openai/modelContext":{updateId:f}}}}function removeMcpAppModelContextItem(e,t,n,r){const i=getMcpAppModelContext(e,t);if(!i)return null;if(i.updateId!==n)throw new Error(`MCP App context changed; refresh before removing it`);if(r===void 0)updateMcpAppModelContext(e,t,{});else{if(!Number.isInteger(r)||r<0||r>=(i.content?.length??0))throw new Error(`MCP App context item does not exist`);updateMcpAppModelContext(e,t,{content:i.content?.filter((e,t)=>t!==r),structuredContent:i.structuredContent})}return getMcpAppModelContext(e,t)}function modelContent(e){const{_meta:t,...n}=e;if(n.type===`resource`){const{_meta:e,...t}=n.resource;n.resource=t}return n}function projectMcpAppModelContextInput(e,t=0){const n=[];const r=e.map(e=>({...e.content?{content:e.content.map(e=>{if(e.type===`image`){n.push({type:`image`,data:e.data,mimeType:e.mimeType});return{type:`image`,mimeType:e.mimeType,imageIndex:t+n.length-1}}if(e.type===`resource`&&`blob`in e.resource&&e.resource.mimeType?.startsWith(`image/`)){n.push({type:`image`,data:e.resource.blob,mimeType:e.resource.mimeType});return{type:`resource`,resource:{uri:e.resource.uri,mimeType:e.resource.mimeType,imageIndex:t+n.length-1}}}return e})}:{},...e.structuredContent!==void 0?{structuredContent:e.structuredContent}:{}}));const i=`MCP App context snapshots:
`+JSON.stringify(r);return{images:n,context:{kind:`conversation-data`,text:i},legacyText:escapeInternalRuntimeContextDelimiters(i)}}function leaseMcpAppModelContextForTurn(e){if(e.runtime.mcpAppModelContextRevoked===true)return;const t=contexts.get(e.runtime);const n=[...t?.entries()??[]].filter(([t,n])=>!n.leased&&(!e.views||e.views.has(t)));if(!n.length)return;for(const[,e]of n)e.leased=true;const r=n.map(([,e])=>({...e.content?{content:e.content.map(modelContent)}:{},...e.structuredContent!==void 0?{structuredContent:e.structuredContent}:{}}));let i=false;return{modelContext:r,assertCurrent:()=>{e.runtime.assertOwnerCurrent?.();if(e.runtime.mcpAppModelContextRevoked||n.some(([e,n])=>t?.get(e)!==n))throw new Error(`MCP App context authority changed before input custody`)},commit:()=>{i=true;for(const[e,r]of n)if(t?.get(e)===r){t.delete(e);notify(e,null)}},rollback:()=>{if(!i)for(const[,e]of n)e.leased=void 0}}}export{projectMcpAppModelContextInput as a,subscribeMcpAppModelContext as c,listAgentSessionMcpRuntimeIds as d,peekSessionMcpRuntimeManager as f,leaseMcpAppModelContextForTurn as i,updateMcpAppModelContext as l,retireSessionMcpRuntimeForAgentDeletion as m,clearMcpAppModelContextForView as n,removeMcpAppModelContextItem as o,releaseSessionMcpRuntime as p,getMcpAppModelContext as r,revokeMcpAppModelContext as s,allowMcpAppModelContext as t,completeDeferredSessionMcpRuntimeRetirement as u};