openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
70 lines (69 loc) • 3.38 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-6kR-86XC.mjs";
import { u as asPositiveFiniteNumber } from "./number-coercion-Dq1l5muL.mjs";
import { o as asRecord } from "./record-coerce-TIsquoVR.mjs";
import { A as parseAgentSessionKey } from "./session-key-BC4m_Ly5.mjs";
import { t as ErrorCodes } from "./gateway-error-details-DIefe7Zy.mjs";
import { f as errorShape } from "./error-codes-DI20vDEB.mjs";
import { a as isAgentHarnessSessionKey, r as AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE, s as isAgentHarnessSessionStoreEntryProtected } from "./agent-harness-session-key-B7mhXqKs.mjs";
import "./session-accessor-BM3Wa1Bx.mjs";
import { n as resolveRequestedSessionAgentId } from "./session-request-agent-D8Xj-iBG.mjs";
import { i as resolveSessionStoreKey } from "./session-store-key-11PJMhso.mjs";
import { a as resolveSessionEntryAccessTarget } from "./session-accessor.entry-BoDZTg0e.mjs";
import "./session-utils-DrY1lc9W.mjs";
import { i as createMcpAttachGrantServerConfig, o as getActiveMcpLoopbackRuntime } from "./mcp-http.loopback-runtime-BkzqlAkB.mjs";
import { i as mintAttachGrant, l as revokeAttachGrant } from "./mcp-grant-store-Chv2y9y7.mjs";
import { n as ensureMcpLoopbackServer } from "./mcp-http-1Y4yAHI-.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 = resolveRequestedSessionAgentId(cfg, requestedSessionKey, normalizeOptionalString(grantParams.agentId));
if (!requestedAgent.ok) {
respond(false, void 0, requestedAgent.error);
return;
}
const storageSessionKey = resolveSessionStoreKey({
cfg,
sessionKey: requestedSessionKey,
storeAgentId: requestedAgent.agentId
});
const sessionKey = parseAgentSessionKey(storageSessionKey) ? storageSessionKey : `agent:${requestedAgent.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 };