openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
80 lines (79 loc) • 3.83 kB
JavaScript
import { i as resolveGlobalSingleton } from "./global-singleton-Dc_stLtU.js";
import { n as INTERNAL_RUNTIME_CONTEXT_END, o as escapeInternalRuntimeContextDelimiters, t as INTERNAL_RUNTIME_CONTEXT_BEGIN } from "./internal-runtime-context-UgZVJlox.js";
import { r as getAsyncWorkSignal } from "./async-work-scope-CMQS2uTf.js";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/agents/agent-bundle-mcp-request-context.ts
const requestSignals = resolveGlobalSingleton(Symbol.for("openclaw.sessionMcpRequestSignal"), () => new AsyncLocalStorage());
function getSessionMcpRequestSignal() {
const caller = requestSignals.getStore();
const owner = getAsyncWorkSignal();
return caller && owner ? AbortSignal.any([caller, owner]) : caller ?? owner;
}
function runWithSessionMcpRequestSignal(signal, run) {
return signal ? requestSignals.run(signal, run) : run();
}
//#endregion
//#region src/agents/mcp-app-model-context.ts
const MCP_APP_MODEL_CONTEXT_MAX_BYTES = 16384;
function clearMcpAppModelContext(runtime) {
runtime.pendingMcpAppModelContext = void 0;
}
function revokeMcpAppModelContext(runtime) {
clearMcpAppModelContext(runtime);
runtime.mcpAppModelContextRevoked = true;
}
function allowMcpAppModelContext(runtime) {
runtime.mcpAppModelContextRevoked = void 0;
}
function clearMcpAppModelContextForView(runtime, view) {
if (runtime.pendingMcpAppModelContext?.owner === view) clearMcpAppModelContext(runtime);
}
function updateMcpAppModelContext(runtime, view, params) {
if (runtime.mcpAppModelContextRevoked === true) throw new Error("MCP App model context is unavailable for this session");
if (Object.hasOwn(params, "structuredContent")) throw new Error("MCP App structured model context is unsupported");
if (params.content === void 0 || Array.isArray(params.content) && params.content.length === 0) {
clearMcpAppModelContext(runtime);
return;
}
if (!Array.isArray(params.content) || params.content.length !== 1) throw new Error("MCP App model context must contain exactly one text block");
const block = params.content[0];
if (!block || typeof block !== "object" || Array.isArray(block)) throw new Error("MCP App model context must contain exactly one text block");
const { type, text } = block;
if (type !== "text" || typeof text !== "string") throw new Error("MCP App model context must contain exactly one text block");
if (text.length === 0) {
clearMcpAppModelContext(runtime);
return;
}
if (Buffer.byteLength(text, "utf8") > MCP_APP_MODEL_CONTEXT_MAX_BYTES) throw new Error(`MCP App model context exceeds ${MCP_APP_MODEL_CONTEXT_MAX_BYTES} bytes`);
runtime.pendingMcpAppModelContext = {
owner: view,
text
};
}
function leaseMcpAppModelContextForTurn(params) {
const snapshot = params.runtime.pendingMcpAppModelContext;
if (!snapshot || snapshot.leased === true || params.runtime.mcpAppModelContextRevoked === true) return;
snapshot.leased = true;
const encodedSnapshot = escapeInternalRuntimeContextDelimiters(JSON.stringify({ text: snapshot.text }));
let committed = false;
return {
prompt: [
INTERNAL_RUNTIME_CONTEXT_BEGIN,
"MCP App context snapshot:",
encodedSnapshot,
INTERNAL_RUNTIME_CONTEXT_END,
"",
params.prompt
].join("\n"),
transcriptPrompt: params.transcriptPrompt ?? params.prompt,
commit: () => {
committed = true;
if (params.runtime.pendingMcpAppModelContext === snapshot) clearMcpAppModelContext(params.runtime);
},
rollback: () => {
if (!committed && params.runtime.pendingMcpAppModelContext === snapshot) snapshot.leased = void 0;
}
};
}
//#endregion
export { updateMcpAppModelContext as a, revokeMcpAppModelContext as i, clearMcpAppModelContextForView as n, getSessionMcpRequestSignal as o, leaseMcpAppModelContextForTurn as r, runWithSessionMcpRequestSignal as s, allowMcpAppModelContext as t };