openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.42 kB
JavaScript
import{i as resolveGlobalMap}from"./global-singleton-DolL9xGt.mjs";
//!src/agents/mcp-form-resource-context.ts
const owners=resolveGlobalMap(Symbol.for(`openclaw.mcpFormResourceOwners`),e=>{for(const t of Array.from(e.values()))t.dispose();e.clear()});function registerMcpFormResourceOwner(e,t){if(owners.has(e)||owners.size>=128)throw new Error(`MCP form resource capacity exceeded`);owners.set(e,t);return()=>{if(owners.get(e)===t)owners.delete(e)}}function resourceProjection(e){return JSON.stringify({resource:e.resource,options:e.options})}function reserveMcpFormQuestion(e){const t=[];const release=()=>{for(const{owner:n,reservation:r}of t)if(n.pending.get(e.questionId)===r)n.pending.delete(e.questionId)};try{const n=new Set(e.questions.flatMap(e=>e.resource?.viewId?[e.resource.viewId]:[]));for(const r of n){const n=owners.get(r);if(!n||n.sessionKey!==e.sessionKey||n.agentId!==e.agentId)throw new Error(`MCP form origin is unavailable for this question`);n.assertCurrent();if(n.pending.has(e.questionId))throw new Error(`MCP form question id is already reserved`);const i=/* @__PURE__ */ new Map;for(const t of e.questions){if(t.resource?.viewId!==r)continue;const e=n.questions.get(t.id);if(!e||resourceProjection(e)!==resourceProjection(t))throw new Error(`MCP form question does not match its originating schema`);i.set(t.id,e)}const a={fields:i};n.pending.set(e.questionId,a);t.push({owner:n,reservation:a})}}catch(e){release();throw e}return release}function requireMcpFormQuestion(e){const t=owners.get(e.viewId);const n=t?.pending.get(e.requestId);const r=n?.fields.get(e.question.questionId);if(!t||t.sessionKey!==e.sessionKey||t.agentId!==e.agentId||!r||n?.record!==e.record||n.isCurrent?.()!==true||resourceProjection(r)!==resourceProjection(e.question))throw new Error(`MCP resource question is not bound to this origin`);t.assertCurrent();return t}function bindMcpFormQuestionRecord(e,t){for(const n of e.questions){const r=n.resource?.viewId;const i=r?owners.get(r):void 0;const a=i?.pending.get(e.id);if(!i||!a||a.record||i.agentId!==e.agentId||i.sessionKey!==e.sessionKey)continue;if(e.questions.filter(e=>e.resource?.viewId===r).some(e=>{const t=a.fields.get(e.questionId);return!t||resourceProjection(t)!==resourceProjection(e)}))continue;a.record=e;a.isCurrent=t}}export{reserveMcpFormQuestion as i,registerMcpFormResourceOwner as n,requireMcpFormQuestion as r,bindMcpFormQuestionRecord as t};