UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

84 lines (83 loc) • 3.24 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { t as assertionSucceeds } from "./assertion-BVvbcE3a.mjs"; import { t as getSessionMcpRequestSignal } from "./agent-bundle-mcp-request-context-B-LzHCC-.mjs"; import { randomUUID } from "node:crypto"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/agents/mcp-client-elicitation.ts const MCP_ELICITATION_TIMEOUT_MS = 6e5; const handlers = resolveGlobalSingleton(Symbol.for("openclaw.mcpElicitationHandler"), () => new AsyncLocalStorage()); function runWithMcpElicitationHandler(e, t) { return handlers.run(e, t); } /** Capture the current tool caller for harness-native, out-of-band requests. */ function captureMcpClientElicitation() { const e = handlers.getStore(); if (!e) return; const t = AsyncLocalStorage.snapshot(); return { signal: getSessionMcpRequestSignal(), handle: (n) => t(e, n) }; } /** Transport adapter only: the shared structured-input compiler owns form semantics. */ function createMcpClientElicitationHandler(e) { return async (t) => { e.assertCurrent(); const [{ compileStructuredInputForm: n, compileStructuredInputUrl: r, snapshotStructuredInput: i, isStructuredInputRecord: a }, { runStructuredInput: o }, { ProtocolError: s, ProtocolErrorCode: c }] = await Promise.all([ import("./structured-input-BHgITLcD.mjs"), import("./structured-input-execution-CIqY0HLJ.mjs"), import("@modelcontextprotocol/client") ]); e.assertCurrent(); const l = t.method === "openai/elicitation/create"; const u = i(t.params, { richForm: l }); if (!a(u)) throw new s(c.InvalidParams, "Malformed MCP elicitation request"); const d = l && e.prepareResourceContext && u.mode !== "url" ? await e.prepareResourceContext({ requestId: t.requestId ?? randomUUID(), snapshot: u, signal: t.signal }) : void 0; try { const i = u.mode === "url" ? r({ url: u.url, elicitationId: u.elicitationId, message: u.message, fallbackMessage: "MCP server needs input", protocolName: "MCP" }) : n({ schema: u.requestedSchema, message: typeof u.message === "string" ? u.message : void 0, fallbackMessage: "MCP server needs input", options: { protocolName: l ? "OpenAI" : "MCP", allowRichForms: l, ...d ? { resourceContext: d.context } : {}, allowEmptyForm: true, minimumChoiceCount: 1, allowEnumNames: true, metadata: { secretPath: ["isSecret"] } } }); if (i.kind === "unsupported") throw new s(c.InvalidParams, i.message); const a = await o({ input: i, sessionKey: e.sessionKey, agentId: e.agentId, timeoutMs: MCP_ELICITATION_TIMEOUT_MS, delivery: {}, gatewayCall: e.gatewayCall, signal: t.signal, isActive: () => assertionSucceeds(() => e.assertCurrent()) }); e.assertCurrent(); return a.status === "answered" ? { action: "accept", content: a.content } : { action: a.status === "declined" || a.status === "unsupported" ? "decline" : "cancel" }; } finally { await d?.dispose(); } }; } //#endregion export { runWithMcpElicitationHandler as i, captureMcpClientElicitation as n, createMcpClientElicitationHandler as r, MCP_ELICITATION_TIMEOUT_MS as t };