UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

116 lines (115 loc) 5.46 kB
import { l as normalizeOptionalString } from "./string-coerce-CIXf7egm.js"; import { r as createLazyRuntimeModule } from "./lazy-runtime-CgCh8H_K.js"; import { O as parseAgentSessionKey } from "./session-key-BnWWjqNc.js"; import { t as createSubsystemLogger } from "./subsystem-Dy2tqXOS.js"; import { t as ErrorCodes } from "./gateway-error-details-w0nAGBBp.js"; import { d as errorShape } from "./error-codes-Bo8q2D1o.js"; import { A as isInternalSessionEffectsKey } from "./session-accessor.sqlite-entry-CWk3jL7s.js"; import { r as resolveAgentMainSessionKey } from "./main-session-Br0F9dzh.js"; import { n as resolveWorkerPlacementExecutionMode, r as resolveWorkerPlacementSessionRuntime } from "./placement-session-runtime-Cts2P0Rr.js"; import { d as resolveGatewaySessionStoreTarget, f as resolveGatewaySessionStoreTargetWithStore, s as resolveCanonicalSessionEntryFromStoreKeys } from "./session-utils-store-CInT2loy.js"; import "./session-utils-Cai0_C6U.js"; import { o as resolveWorkerPlacementArchiveRestoreError } from "./session-placement-lifecycle-7DQHpaT9.js"; //#region src/gateway/server-methods/sessions-shared.ts const sessionLog = createSubsystemLogger("gateway/sessions"); function respondSessionWorkerPlacementMutationError(error, respond) { respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, error.message)); } function resolveSessionWorkerPlacementPatchError(params) { const placement = params.entry?.sessionId ? params.context.workerSessionPlacementService?.getMany([params.entry.sessionId]).get(params.entry.sessionId) : void 0; if (!placement || placement.state === "local") return; if ("permissionMode" in params.patch && placement.executionMode === "worker-turn" && placement.turnClaim) return "This remote worker cannot apply permissions while active. Stop the worker run, then change permissions."; if (params.patch.archived === false) { const restoreError = resolveWorkerPlacementArchiveRestoreError({ context: params.context, key: params.key, placement }); if (restoreError) return restoreError; } if (!params.validateModelRuntime || params.patch.model === void 0 || !params.entry?.sessionId) return; const runtime = resolveWorkerPlacementSessionRuntime({ cfg: params.cfg, entry: params.entry, agentId: params.agentId, sessionKey: params.sessionKey }); const executionMode = resolveWorkerPlacementExecutionMode(runtime); if (executionMode === placement.executionMode) return; return executionMode ? `Session ${params.key} cannot change cloud placement execution mode while placement is ${placement.state}.` : `Session ${params.key} cannot select the ${runtime} runtime while cloud worker placement is ${placement.state}.`; } const loadSessionsRuntimeModule = createLazyRuntimeModule(() => import("./sessions.runtime.js")); function requireSessionKey(key, respond) { const normalized = normalizeOptionalString(typeof key === "string" ? key : typeof key === "number" ? String(key) : typeof key === "bigint" ? String(key) : "") ?? ""; if (!normalized) { respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "key required")); return null; } return normalized; } function resolveGatewaySessionTargetFromKey(key, cfg, opts) { const target = resolveGatewaySessionStoreTarget({ cfg, key, ...opts?.agentId ? { agentId: opts.agentId } : {} }); return { cfg, target, storePath: target.storePath }; } function loadAccessorSessionEntryForGatewayTarget(params) { const target = resolveGatewaySessionStoreTargetWithStore({ cfg: params.cfg, key: params.key, exactRead: true, ...params.agentId ? { agentId: params.agentId } : {} }); return { target, storePath: target.storePath, entry: isInternalSessionEffectsKey(target.canonicalKey) ? void 0 : resolveCanonicalSessionEntryFromStoreKeys(target.store, target.storeKeys), canonicalKey: target.canonicalKey, sessionStoreKey: target.canonicalKey }; } function loadSessionEntriesForTarget(params) { const target = resolveGatewaySessionStoreTargetWithStore({ cfg: params.cfg, key: params.key, clone: false, ...params.agentId ? { agentId: params.agentId } : {} }); const store = target.store; const entry = resolveCanonicalSessionEntryFromStoreKeys(store, target.storeKeys); return { target, storePath: target.storePath, store, entry }; } function emitSessionOperation(context, payload) { const connIds = context.getSessionEventSubscriberConnIds(); if (connIds.size === 0) return; context.broadcastToConnIds("session.operation", { ...payload, ts: Date.now() }, connIds, { dropIfSlow: true }); } function isWorkerDispatchInputError(error) { if (typeof error !== "object" || error === null || !("code" in error)) return false; const code = error.code; return code === "invalid_profile" || code === "profile_not_found" || code === "invalid_state"; } function isAgentMainSessionKey(cfg, sessionKey) { const parsed = parseAgentSessionKey(sessionKey); if (!parsed) return false; return sessionKey === resolveAgentMainSessionKey({ cfg, agentId: parsed.agentId }); } //#endregion export { loadSessionEntriesForTarget as a, resolveGatewaySessionTargetFromKey as c, sessionLog as d, loadAccessorSessionEntryForGatewayTarget as i, resolveSessionWorkerPlacementPatchError as l, isAgentMainSessionKey as n, loadSessionsRuntimeModule as o, isWorkerDispatchInputError as r, requireSessionKey as s, emitSessionOperation as t, respondSessionWorkerPlacementMutationError as u };