UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

65 lines (64 loc) 2.97 kB
import { w as resolveStateDir } from "./paths-D2sRr1a_.js"; import { n as GatewayErrorDetailCodes, t as ErrorCodes } from "./gateway-error-details-w0nAGBBp.js"; import { g as retainGatewayRootWorkAdmissionContinuation } from "./gateway-work-admission-R1IpuDim.js"; import { d as errorShape } from "./error-codes-Bo8q2D1o.js"; import { r as SetupTargetLockedError, u as withSetupMigrationTargetLock } from "./setup.migration-snapshot-DEcmXjFO.js"; //#region src/gateway/server-methods/setup-admission.ts const SETUP_ADMISSION_BUSY_MESSAGE = "OpenClaw setup is already in progress; try again when it finishes."; let wizardSessionInProgress = false; const wizardSessionAdmissionSettlements = /* @__PURE__ */ new WeakMap(); var SetupAdmissionBusyError = class extends Error {}; /** Only admission failures may promise that no setup task or session began. */ function respondSetupAdmissionBusy(respond) { respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, SETUP_ADMISSION_BUSY_MESSAGE, { retryable: true, details: { code: GatewayErrorDetailCodes.SETUP_ADMISSION_BUSY } })); } async function runExclusiveSystemAgentSetupActivation(task) { let admitted = false; const admittedTask = async () => { admitted = true; return await task(); }; try { return await withSetupMigrationTargetLock(resolveStateDir(), admittedTask); } catch (error) { if (!admitted && error instanceof SetupTargetLockedError) throw new SetupAdmissionBusyError(SETUP_ADMISSION_BUSY_MESSAGE); throw error; } } /** Resolves after both the wizard runner and its setup-target admission have settled. */ function whenAdmittedWizardSessionSettled(session) { return wizardSessionAdmissionSettlements.get(session) ?? session.whenSettled(); } async function createAdmittedWizardSession(createSession, lockSetupTarget = true) { if (wizardSessionInProgress) return; wizardSessionInProgress = true; const releaseSession = () => { wizardSessionInProgress = false; }; try { let admissionSettled; const session = lockSetupTarget ? await new Promise((resolve, reject) => { admissionSettled = runExclusiveSystemAgentSetupActivation(async () => { const createdSession = createSession(); resolve(createdSession); await createdSession.whenSettled(); }); admissionSettled.catch(reject); }) : createSession(); const settled = admissionSettled ?? session.whenSettled(); wizardSessionAdmissionSettlements.set(session, settled); const releaseGatewayWork = retainGatewayRootWorkAdmissionContinuation(); if (releaseGatewayWork) settled.then(releaseGatewayWork, releaseGatewayWork); settled.then(releaseSession, releaseSession); return session; } catch (error) { releaseSession(); if (error instanceof SetupAdmissionBusyError) return; throw error; } } //#endregion export { whenAdmittedWizardSessionSettled as a, runExclusiveSystemAgentSetupActivation as i, createAdmittedWizardSession as n, respondSetupAdmissionBusy as r, SetupAdmissionBusyError as t };