UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

65 lines (64 loc) • 3.24 kB
import { l as normalizeOptionalString } from "./string-coerce-1VuiLaaR.mjs"; import { u as asPositiveFiniteNumber } from "./number-coercion-CkhMiRw8.mjs"; import { o as asRecord } from "./record-coerce-TIsquoVR.mjs"; import "./session-key-DY3MOX4N.mjs"; import { h as parseAgentSessionKey } from "./session-key-utils-CDyBC9Gn.mjs"; import { t as ErrorCodes } from "./gateway-error-details-gTekQWj2.mjs"; import { d as errorShape } from "./error-codes-D0WRV4Bq.mjs"; import { a as isAgentHarnessSessionKey, r as AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE, s as isAgentHarnessSessionStoreEntryProtected } from "./agent-harness-session-key-Dx0vv6oG.mjs"; import "./session-accessor-C3ljJ974.mjs"; import { n as resolveRequestedSessionStoreTarget } from "./session-store-key-Dvf3CKvN.mjs"; import { s as resolveSessionEntryAccessTarget } from "./session-accessor.entry-xtqpYxWo.mjs"; import { i as createMcpAttachGrantServerConfig, o as getActiveMcpLoopbackRuntime } from "./mcp-http.loopback-runtime-DAwH1ifm.mjs"; import { a as mintAttachGrant, u as revokeAttachGrant } from "./server-connection-work-DogQttEZ.mjs"; import { n as ensureMcpLoopbackServer } from "./mcp-http-CkjqDJdJ.mjs"; //#region src/gateway/server-methods/attach.ts const attachHandlers = { "attach.grant": async ({ params, respond, context }) => { const grantParams = asRecord(params); const cfg = context.getRuntimeConfig(); const requestedSessionKey = normalizeOptionalString(grantParams.sessionKey) ?? "main"; const requestedAgent = resolveRequestedSessionStoreTarget(cfg, requestedSessionKey, normalizeOptionalString(grantParams.agentId)); if (!requestedAgent.ok) { respond(false, void 0, requestedAgent.error); return; } const { sessionKey: storageSessionKey, agentId } = requestedAgent.value; const sessionKey = parseAgentSessionKey(storageSessionKey) ? storageSessionKey : `agent:${agentId}:${storageSessionKey}`; const harnessEntry = isAgentHarnessSessionKey(storageSessionKey) ? resolveSessionEntryAccessTarget({ cfg, sessionKey: storageSessionKey }).entry : void 0; if (isAgentHarnessSessionKey(storageSessionKey) && (!harnessEntry || isAgentHarnessSessionStoreEntryProtected(storageSessionKey, harnessEntry))) { respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE)); return; } await ensureMcpLoopbackServer(); const runtime = getActiveMcpLoopbackRuntime(); if (!runtime) { respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "mcp loopback server unavailable")); return; } const grant = mintAttachGrant({ sessionKey, ttlMs: asPositiveFiniteNumber(grantParams.ttlMs) }); respond(true, { sessionKey: grant.sessionKey, token: grant.token, expiresAtMs: grant.expiresAtMs, mcpConfig: createMcpAttachGrantServerConfig(runtime.port), env: { OPENCLAW_MCP_TOKEN: grant.token } }); }, "attach.revoke": async ({ params, respond }) => { const token = normalizeOptionalString(asRecord(params).token); if (!token) { respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "token is required")); return; } respond(true, { revoked: revokeAttachGrant(token) }); } }; //#endregion export { attachHandlers };