openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.28 kB
JavaScript
import{i as resolveGlobalMap}from"./global-singleton-DolL9xGt.mjs";import{t as createDeferredCore}from"./deferred-QsZCoVH-.mjs";import{s as raceWithTimeout}from"./src-De06gkOi.mjs";
//!src/gateway/mcp-http.loopback-runtime.ts
let activeRuntime;let nextToolCallCaptureGeneration=0;const toolCallCaptures=resolveGlobalMap(Symbol.for(`openclaw.mcpLoopbackToolCallCaptures`),e=>{for(const t of e.keys())deleteMcpLoopbackToolCallCapture(t)});function observeCapture(e){try{return e()}catch{return}}function deleteMcpLoopbackToolCallCapture(e){const t=toolCallCaptures.get(e);if(!t)return;toolCallCaptures.delete(e);for(const e of t.activityWaiters)e();t.activityWaiters.clear()}function notifyMcpLoopbackToolCallCaptureActivity(e){for(const t of e.activityWaiters)t();e.activityWaiters.clear()}function beginMcpLoopbackToolCallCapture({captureKey:e,...t}){const n=e.trim();if(!n)return;nextToolCallCaptureGeneration+=1;toolCallCaptures.set(n,{...t,generation:nextToolCallCaptureGeneration,inFlight:0,activityWaiters:/* @__PURE__ */ new Set})}function resolveMcpLoopbackYieldContext(e){const t=e?.capture;if(!t?.onYield)return;return{cacheKey:String(t.generation),onYield:async(e,n)=>{await t.onYield?.(e,n)}}}function markMcpLoopbackRequestStarted(e){const t=e?.trim()??``;if(!t)return;const n=toolCallCaptures.get(t);if(!n)return;n.inFlight+=1;notifyMcpLoopbackToolCallCaptureActivity(n);observeCapture(()=>n.onRequestStart?.());return{capture:n,classified:false,finished:false}}function markMcpLoopbackRequestClassified(e){if(!e||e.classified||e.finished)return;e.classified=true;observeCapture(()=>e.capture.onRequestClassified?.())}function markMcpLoopbackRequestFinished(e){if(!e||e.finished)return;markMcpLoopbackRequestClassified(e);e.finished=true;const{capture:t}=e;t.inFlight=Math.max(0,t.inFlight-1);notifyMcpLoopbackToolCallCaptureActivity(t)}function markMcpLoopbackToolCallStarted(e){const t=e.toolName.trim();if(!t||e.requestCaptureHandle?.finished)return;const n=e.captureKey?.trim()??``;const r=e.requestCaptureHandle?.capture??toolCallCaptures.get(n);if(!r)return;const i={toolName:t,args:e.args};r.inFlight+=1;notifyMcpLoopbackToolCallCaptureActivity(r);const a=observeCapture(()=>r.onToolCallStart?.(i));return{capture:r,call:i,correlationId:typeof a===`string`?a:void 0,prepared:false,finished:false}}function updateMcpLoopbackToolCallCapture(e,t){if(!e||e.finished)return;const n=e.call;e.call=t;e.prepared=true;observeCapture(()=>{e.capture.onToolCallUpdate?.({previous:n,current:t})})}function recordMcpLoopbackToolCallResult(e){const t=e.toolName.trim();if(!t)return;observeCapture(()=>{const n=e.outcome===`blocked`?{outcome:`blocked`,deniedReason:e.deniedReason}:{outcome:e.outcome,result:e.result};e.captureHandle.capture.onToolCallResult({toolName:t,args:e.args,...n,...e.captureHandle.correlationId?{correlationId:e.captureHandle.correlationId}:{}})})}function markMcpLoopbackToolCallFinished(e){if(!e||e.finished)return;e.finished=true;const{capture:t}=e;observeCapture(()=>{t.onToolCallFinish?.(e.call,{prepared:e.prepared})});t.inFlight=Math.max(0,t.inFlight-1);notifyMcpLoopbackToolCallCaptureActivity(t)}async function waitForMcpLoopbackToolCallCaptureActivity(e,t){const n=createDeferredCore();const resolveActivity=()=>n.resolve(true);e.activityWaiters.add(resolveActivity);try{return await raceWithTimeout(n.promise,Math.max(0,t),()=>false,{ref:false})}finally{e.activityWaiters.delete(resolveActivity)}}async function waitForMcpLoopbackToolCallCaptureIdle(e,t){const n=e.trim();const r=toolCallCaptures.get(n);if(!r)return true;const i=performance.now()+Math.max(0,t.timeoutMs);while(toolCallCaptures.get(n)===r){const e=i-performance.now();if(e<=0)return false;if(r.inFlight>0){await waitForMcpLoopbackToolCallCaptureActivity(r,e);continue}return true}return true}function clearMcpLoopbackToolCallCapture(e){deleteMcpLoopbackToolCallCapture(e.trim())}function getActiveMcpLoopbackRuntime(){return activeRuntime?{...activeRuntime}:void 0}function setActiveMcpLoopbackRuntime(e){activeRuntime={...e}}function clearActiveMcpLoopbackRuntimeByOwnerToken(e){if(activeRuntime?.ownerToken===e)activeRuntime=void 0}const MCP_AUTH_HEADERS={Authorization:"Bearer ${OPENCLAW_MCP_TOKEN}"};const MCP_CAPTURE_HEADERS={"x-openclaw-cli-capture-key":"${OPENCLAW_MCP_CLI_CAPTURE_KEY}"};function createMcpServerConfig(e,t,n=true){return{mcpServers:{openclaw:{type:`http`,url:`http://127.0.0.1:${e}/mcp`,alwaysLoad:n,headers:t}}}}function createMcpLoopbackServerConfig(e,t){return createMcpServerConfig(e,{...MCP_AUTH_HEADERS,...MCP_CAPTURE_HEADERS},!t?.deferTools)}function createMcpAttachGrantServerConfig(e){return createMcpServerConfig(e,MCP_AUTH_HEADERS)}export{createMcpLoopbackServerConfig as a,markMcpLoopbackRequestFinished as c,markMcpLoopbackToolCallStarted as d,recordMcpLoopbackToolCallResult as f,waitForMcpLoopbackToolCallCaptureIdle as g,updateMcpLoopbackToolCallCapture as h,createMcpAttachGrantServerConfig as i,markMcpLoopbackRequestStarted as l,setActiveMcpLoopbackRuntime as m,clearActiveMcpLoopbackRuntimeByOwnerToken as n,getActiveMcpLoopbackRuntime as o,resolveMcpLoopbackYieldContext as p,clearMcpLoopbackToolCallCapture as r,markMcpLoopbackRequestClassified as s,beginMcpLoopbackToolCallCapture as t,markMcpLoopbackToolCallFinished as u};