UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

302 lines (301 loc) • 12.1 kB
import { r as settleWithCleanup } from "./error-graph-internal-DdjdM90N.mjs"; import { l as trackAsyncWork } from "./async-work-scope-BXagjUap.mjs"; import { l as withPluginRuntimeGatewayRequestScope, o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs"; import { i as getLegacyPluginSdkResourceHost, n as bindLegacyPluginSdkResourceHost, r as getBoundLegacyPluginSdkResourceHost, t as LegacyPluginSdkResourceHost } from "./legacy-sdk-resource-host-BiedXayN.mjs"; import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs"; import "./agent-scope-config-DmXgFGKP.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { t as GatewayScheduler } from "./gateway-scheduler-BApPBa3S.mjs"; import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import { C as withAgentRunLifecycleGeneration, r as captureAgentRunLifecycleGeneration } from "./agent-events-BdfWYmot.mjs"; import { n as withPluginRetentionOwner } from "./plugin-retention-diagnostics-gj7JVNsM.mjs"; import { n as withPluginRuntimeGenerationScope } from "./generation-scope-Cq7RLFex.mjs"; import "./io-4QsrsWTJ.mjs"; import { t as createAgentRuntimeApprovalAuthorityValidator } from "./agent-runtime-approval-authority-C0rhCYlf.mjs"; import { t as bindSessionRowProjection } from "./session-row-projection-access-CjLImwLs.mjs"; import { i as scopePreparedModelRuntimeLease } from "./prepared-model-runtime-generation-scope-Dpvf6WHJ.mjs"; import { t as ensureGlobalHookRunner } from "./hook-runner-global-DmYyIVDp.mjs"; import { t as acquireAgentRunPreparedModelRuntime } from "./prepared-model-runtime-Bbo9uBdt.mjs"; import { n as readPreparedServerMethodModelCatalogs } from "./optional-model-catalog-D8GNpUVH.mjs"; import { r as registerGatewayModelCatalogPrivateAccess } from "./server-model-catalog-auth-DPSO0-N0.mjs"; import { r as createChatRunState } from "./server-chat-state-hXRKEuCM.mjs"; import { d as createCronCreatorAuthorityCapability, p as runWithCronCreatorAuthorityCapability } from "./cron-creator-authority-context-D4fHKVAj.mjs"; import { a as readPreparedGatewayModelCatalogBatch, i as readPreparedGatewayModelCatalog, n as loadGatewayModelCatalogSnapshot, o as readPreparedGatewayModelCatalogOwnerSnapshot, r as loadPreparedGatewayModelCatalogSnapshot, t as loadGatewayModelCatalog } from "./server-model-catalog-DpggHr2T.mjs"; import { p as resolveAgentCommandDeps } from "./runtime-loaders-AND97j4j.mjs"; import { t as measureAgentStartup } from "./startup-timing-KnC51YT7.mjs"; import { t as prepareAgentCommandExecution } from "./prepare-sHsW64ql.mjs"; import { t as withLocalAgentCronJobsRemoved } from "./local-service-a878KfUi.mjs"; import { t as loadGatewayConfigRevisionProjector } from "./config-revision-token-Yyf06n3S.mjs"; import { t as NodeRegistry } from "./node-registry-CE_3ElcX.mjs"; import { t as resolveSessionRequestTargets } from "./session-request-targets-BhETnJOv.mjs"; //#region src/gateway/local-request-context.ts async function cronUnavailable() { throw new Error("Cron is unavailable in local embedded agent gateway context."); } const unavailableCron = { start: cronUnavailable, stop: () => {}, pauseScheduling: () => {}, resumeScheduling: () => {}, status: cronUnavailable, list: cronUnavailable, listPage: cronUnavailable, add: cronUnavailable, update: cronUnavailable, updateWithPrecondition: cronUnavailable, remove: cronUnavailable, removeStaleJobFamily: cronUnavailable, removeAgentJobsTransactional: cronUnavailable, quiesceJobs: cronUnavailable, run: cronUnavailable, enqueueRun: cronUnavailable, readEventSources: cronUnavailable, runEvent: cronUnavailable, waitForManualRun: cronUnavailable, getJob: () => void 0, getLoadedJobs: () => void 0, readJob: async () => void 0, readScratch: cronUnavailable, writeScratch: cronUnavailable, getDefaultAgentId: () => void 0, wake: async () => ({ ok: false, reason: "unwakeable-session-key" }) }; /** Creates the minimal gateway context used by embedded local agent execution. */ async function createLocalGatewayRequestContext(e) { const t = createSubsystemLogger("gateway/local"); const n = getLegacyPluginSdkResourceHost(); const r = { ...unavailableCron, removeAgentJobsTransactional: async (t, n) => await withLocalAgentCronJobsRemoved(t, e.getRuntimeConfig, n) }; const i = /* @__PURE__ */ new Set(); const a = createChatRunState(); const loadCatalogSnapshot = (t) => loadGatewayModelCatalogSnapshot({ ...t, getConfig: e.getRuntimeConfig }); registerGatewayModelCatalogPrivateAccess(loadCatalogSnapshot, { loadDeferred: (t) => loadPreparedGatewayModelCatalogSnapshot({ ...t, getConfig: e.getRuntimeConfig }), readPrepared: (t) => readPreparedGatewayModelCatalogOwnerSnapshot({ ...t, getConfig: e.getRuntimeConfig }) }); const o = { localEmbedded: true, trackExecution: trackAsyncWork, deps: e.deps, configRevisionProjector: await loadGatewayConfigRevisionProjector({ env: process.env }), cron: r, cronStorePath: "", getRuntimeConfig: e.getRuntimeConfig, validateAgentRuntimeApprovalAuthority: createAgentRuntimeApprovalAuthorityValidator(), resolveSessionRequestTargets: (e) => resolveSessionRequestTargets({ ...e, context: o }), isConfigReloadSettled: () => false, resolveTerminalLaunchPolicy: () => ({ ok: false, block: { kind: "disabled" } }), isTerminalEnabled: () => false, loadGatewayModelCatalog: (t) => loadGatewayModelCatalog({ ...t, getConfig: e.getRuntimeConfig }), loadGatewayModelCatalogSnapshot: loadCatalogSnapshot, readPreparedGatewayModelCatalog: (t) => readPreparedGatewayModelCatalog({ ...t, getConfig: e.getRuntimeConfig }), readPreparedGatewayModelCatalogBatch: (t, n) => readPreparedGatewayModelCatalogBatch(t, { ...n, getConfig: e.getRuntimeConfig }), readChatMetadata: async () => { throw new Error("Chat metadata is unavailable in local embedded agent gateway context."); }, getHealthCache: () => null, refreshHealthSnapshot: async () => ({}), logHealth: { error: (e) => t.error(e) }, logGateway: t, publishPresence: () => {}, getPresenceSnapshot: () => { throw new Error("Presence requires a running Gateway connection."); }, broadcast: () => {}, broadcastToConnIds: () => {}, nodeSendToSession: () => {}, nodeSendToAllSubscribed: () => {}, nodeSubscribe: () => {}, nodeUnsubscribe: () => {}, nodeUnsubscribeAll: () => {}, hasConnectedTalkNode: async () => false, nodeRegistry: new NodeRegistry(), agentRunSeq: /* @__PURE__ */ new Map(), chatRunState: a, addChatRun: a.registry.add, removeChatRun: a.registry.remove, subscribeSessionEvents: (e) => { i.add(e); }, unsubscribeSessionEvents: (e) => { i.delete(e); }, forgetConnectionAncestors: () => {}, subscribeSessionMessageEvents: () => void 0, unsubscribeSessionMessageEvents: () => {}, unsubscribeAllSessionEvents: (e) => { i.delete(e); }, getSessionEventSubscriberConnIds: () => i, registerToolEventRecipient: () => {}, dedupe: /* @__PURE__ */ new Map(), wizardSessions: /* @__PURE__ */ new Map(), systemAgentSessions: /* @__PURE__ */ new Map(), findRunningWizard: () => null, purgeWizardSession: () => {}, getRuntimeSnapshot: () => ({}), startChannel: async () => { throw new Error("Channel start is unavailable in local embedded agent gateway context."); }, stopChannel: async () => { throw new Error("Channel stop is unavailable in local embedded agent gateway context."); }, markChannelLoggedOut: () => {}, wizardRunner: async () => { throw new Error("Onboarding wizard is unavailable in local embedded agent gateway context."); }, channelWizardRunner: async () => { throw new Error("Channel setup wizard is unavailable in local embedded agent gateway context."); }, broadcastVoiceWakeChanged: () => {}, broadcastVoiceWakeRoutingChanged: () => {}, unavailableGatewayMethods: /* @__PURE__ */ new Set() }; let s; let c; bindSessionRowProjection(o, () => s); o.ensureSessionRowProjection = () => { if (!c) { n.adopt(o, { release: async () => { await c?.catch(() => {}); s?.dispose(); await s?.ensureMaterialized(); } }); c = import("./session-row-projection-Dny-dCXp.mjs").then(async ({ createSessionRowProjection: t }) => { s = await t({ cfg: e.getRuntimeConfig(), getConfig: e.getRuntimeConfig, getModelCatalog: () => readPreparedServerMethodModelCatalogs(o, listAgentIds(e.getRuntimeConfig())), context: o }); }); } return c; }; o.createAgentTurnFacade = async (e) => { const { createInternalAgentTurnFacade: t } = await import("./internal-facade.runtime.js"); return t({ ...e, getContext: () => o }); }; return o; } /** Runs code inside a local gateway request scope unless an outer scope already exists. */ async function withLocalGatewayRequestScope(e, t) { const n = getPluginRuntimeGatewayRequestScope(); if (n?.context || n?.resolveGatewayContext) return await t(); const r = await createLocalGatewayRequestContext(e); const resolveGatewayContext = () => r; r.resolveGatewayContext = resolveGatewayContext; bindLegacyPluginSdkResourceHost(resolveGatewayContext, getLegacyPluginSdkResourceHost()); return await withPluginRuntimeGatewayRequestScope({ ...n, context: r, resolveGatewayContext, isWebchatConnect: n?.isWebchatConnect ?? (() => false) }, t); } //#endregion //#region src/agents/agent-command-local.ts /** Runs a local command under one request and plugin-registry generation. */ async function runLocalAgentCommand(e) { const t = getBoundLegacyPluginSdkResourceHost(); const n = t ? t.scheduler : new GatewayScheduler(); const r = t ?? new LegacyPluginSdkResourceHost(); if (!t) r.bindScheduler(n); return settleWithCleanup(r.run(async () => { const n = await measureAgentStartup("command-dependencies", () => resolveAgentCommandDeps(e.deps)); const r = e.opts.lifecycleGeneration ?? captureAgentRunLifecycleGeneration(e.opts.runId ?? ""); return await withAgentRunLifecycleGeneration(r, () => withLocalGatewayRequestScope({ deps: n, getRuntimeConfig: readRuntimeConfig }, async () => { const i = { ...e.opts, lifecycleGeneration: r, senderIsOwner: e.opts.senderIsOwner ?? true, allowModelOverride: e.opts.allowModelOverride ?? true, cleanupBundleMcpOnRunEnd: t ? e.opts.cleanupBundleMcpOnRunEnd : true }; const a = await measureAgentStartup("command-prepare", () => prepareAgentCommandExecution(i, e.runtime)); try { const t = e.operatorAuthority && a.opts.senderIsOwner === true ? createCronCreatorAuthorityCapability(a.runId, { kind: "local" }) : void 0; const r = t ? { ...a, opts: { ...a.opts, cronCreatorAuthorityCapability: t } } : a; const run = () => withPluginRetentionOwner({ agentId: a.sessionAgentId, sessionKey: a.sessionKey, runId: a.runId }, async () => { try { var t = _usingCtx(); const i = t.a(scopePreparedModelRuntimeLease(await acquireAgentRunPreparedModelRuntime({ config: r.cfg, agentId: r.sessionAgentId, agentDir: r.agentDir, workspaceDir: r.workspaceDir }, { abortSignal: r.opts.abortSignal }))); ensureGlobalHookRunner(); return await withPluginRuntimeGenerationScope(i.snapshot, () => i.run(() => e.run({ ...r, commandRuntimeContext: { config: i.snapshot.config, pluginGeneration: i.pluginGeneration } }, n))); } catch (e) { t.e = e; } finally { await t.d(); } }); return t ? await runWithCronCreatorAuthorityCapability(t, run, r.opts.abortSignal) : await run(); } finally { await a.runLease?.release(); } })); }), async () => { if (!t) { await n.stop(); await r.close(); } }, "Local agent command and resource cleanup failed."); } //#endregion export { runLocalAgentCommand };