UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

178 lines (177 loc) 7.44 kB
import { O as sessionEntryForkedFromParent } from "./session-accessor.sqlite-entry-CWk3jL7s.js"; import { i as isStaleLifecycleEventForSession, t as deriveGatewaySessionLifecycleProjectionPatch } from "./session-lifecycle-state-DCd4j8Bd.js"; //#region src/gateway/session-event-payload.ts /** * Project a catalog-less session row for websocket merge events. * Picker metadata comes from catalog-backed list/patch responses; emitting a * locally reconstructed subset here would replace richer client state. */ function buildGatewaySessionEventFields(params) { const { sessionRow } = params; const omitUnscopedGlobalGoal = sessionRow.key === "global" && !params.agentId; return { updatedAt: sessionRow.updatedAt ?? void 0, sessionId: sessionRow.sessionId, createdActor: sessionRow.createdActor ?? null, owner: sessionRow.owner ?? null, participants: sessionRow.participants ?? [], participantCount: sessionRow.participantCount ?? 0, kind: sessionRow.kind, visibility: sessionRow.visibility, channel: sessionRow.channel, subject: sessionRow.subject, groupChannel: sessionRow.groupChannel, space: sessionRow.space, chatType: sessionRow.chatType, origin: sessionRow.origin, archived: sessionRow.archived ?? false, archivedAt: sessionRow.archivedAt ?? null, archivedBy: sessionRow.archivedBy ?? null, archiveReason: sessionRow.archiveReason ?? null, pinned: sessionRow.pinned ?? false, pinnedAt: sessionRow.pinnedAt ?? null, unread: sessionRow.unread ?? false, lastReadAt: sessionRow.lastReadAt, markedUnreadAt: sessionRow.markedUnreadAt ?? null, agentStatus: sessionRow.agentStatus ?? null, observerDigest: sessionRow.observerDigest ?? null, lastActivityAt: sessionRow.lastActivityAt, spawnedBy: sessionRow.spawnedBy, controlOwnerSessionKey: sessionRow.controlOwnerSessionKey ?? null, swarmGroupId: sessionRow.swarmGroupId, spawnedWorkspaceDir: sessionRow.spawnedWorkspaceDir, spawnedCwd: sessionRow.spawnedCwd, permissionMode: sessionRow.permissionMode ?? null, permissionModePending: sessionRow.permissionModePending ?? false, ...sessionRow.permissionMode !== void 0 && sessionRow.sessionRoot !== void 0 ? { sessionRoot: sessionRow.sessionRoot } : {}, forkedFromParent: sessionEntryForkedFromParent(sessionRow) ? true : void 0, spawnDepth: sessionRow.spawnDepth, subagentRole: sessionRow.subagentRole, subagentControlScope: sessionRow.subagentControlScope, createdVia: sessionRow.createdVia, createdAt: sessionRow.createdAt, forkSource: sessionRow.forkSource, previousSessionId: sessionRow.previousSessionId, label: params.label ?? sessionRow.label ?? null, icon: sessionRow.icon ?? null, color: sessionRow.color ?? null, channelAvatarUrl: sessionRow.channelAvatarUrl ?? null, category: sessionRow.category ?? null, displayName: params.displayName ?? sessionRow.displayName ?? null, deliveryContext: sessionRow.deliveryContext, parentSessionKey: params.parentSessionKey ?? sessionRow.parentSessionKey, childSessions: sessionRow.childSessions, thinkingLevel: sessionRow.thinkingLevel ?? null, fastMode: sessionRow.fastMode, effectiveFastMode: sessionRow.effectiveFastMode, effectiveFastModeSource: sessionRow.effectiveFastModeSource, fastAutoOnSeconds: sessionRow.fastAutoOnSeconds, toolOverrides: sessionRow.toolOverrides ?? null, verboseLevel: sessionRow.verboseLevel, traceLevel: sessionRow.traceLevel, reasoningLevel: sessionRow.reasoningLevel, elevatedLevel: sessionRow.elevatedLevel, sendPolicy: sessionRow.sendPolicy, systemSent: sessionRow.systemSent, abortedLastRun: sessionRow.abortedLastRun, restartRecoveryStatus: sessionRow.restartRecoveryStatus ?? null, inputTokens: sessionRow.inputTokens, outputTokens: sessionRow.outputTokens, lastChannel: sessionRow.lastChannel, lastTo: sessionRow.lastTo, lastAccountId: sessionRow.lastAccountId, lastThreadId: sessionRow.lastThreadId, totalTokens: sessionRow.totalTokens, totalTokensFresh: sessionRow.totalTokensFresh, ...omitUnscopedGlobalGoal ? {} : { goal: sessionRow.goal ?? null }, contextTokens: sessionRow.contextTokens, estimatedCostUsd: sessionRow.estimatedCostUsd, responseUsage: sessionRow.responseUsage, effectiveResponseUsage: sessionRow.effectiveResponseUsage, modelProvider: sessionRow.modelProvider, model: sessionRow.model, activeModelProvider: sessionRow.activeModelProvider ?? null, activeModel: sessionRow.activeModel ?? null, modelOverrideSource: sessionRow.modelOverrideSource, agentRuntime: sessionRow.agentRuntime, status: params.status ?? sessionRow.status, lastRunError: sessionRow.lastRunError ?? null, lastRunId: sessionRow.lastRunId ?? null, hasAutomation: sessionRow.hasAutomation ?? false, ...params.hasActiveRun === void 0 ? {} : { hasActiveRun: params.hasActiveRun }, ...params.activeRunIds === void 0 ? {} : { activeRunIds: params.activeRunIds }, startedAt: sessionRow.startedAt, endedAt: sessionRow.endedAt, runtimeMs: sessionRow.runtimeMs, compactionCheckpointCount: sessionRow.compactionCheckpointCount, latestCompactionCheckpoint: sessionRow.latestCompactionCheckpoint, pluginExtensions: sessionRow.pluginExtensions }; } function buildGatewaySessionSnapshot(params) { const { event, sessionRow: storedRow } = params; if (!storedRow) return {}; const lifecycleRow = event ? { ...storedRow, updatedAt: storedRow.updatedAt ?? void 0 } : void 0; const patch = event && !isStaleLifecycleEventForSession({ owningSessionId: event.sessionId, currentSessionId: storedRow.sessionId, eventRunId: event.runId, currentRunId: params.lifecycleRunId, eventStartedAt: event.data?.startedAt, currentStartedAt: storedRow.startedAt }) ? deriveGatewaySessionLifecycleProjectionPatch({ entry: lifecycleRow, event }) : {}; const sessionRow = { ...storedRow, ...patch }; for (const key of [ "thinkingLevels", "thinkingOptions", "thinkingDefault" ]) delete sessionRow[key]; if (params.lifecycle && sessionRow.totalTokensFresh !== true) { delete sessionRow.totalTokens; delete sessionRow.totalTokensFresh; delete sessionRow.contextTokens; delete sessionRow.estimatedCostUsd; } const activeStatus = params.activeRunState?.active ? params.activeRunState.status ?? "running" : void 0; const status = params.status ?? patch.status ?? activeStatus; const eventFields = buildGatewaySessionEventFields({ sessionRow, agentId: params.agentId, label: params.label, displayName: params.displayName, parentSessionKey: params.parentSessionKey, status, hasActiveRun: params.activeRunState?.active, activeRunIds: params.activeRunState ? params.activeRunState.runIds ?? null : void 0 }); if (params.lifecycle) for (const field of [ "modelProvider", "model", "activeModelProvider", "activeModel", "modelOverrideSource", "agentRuntime" ]) { delete sessionRow[field]; delete eventFields[field]; } const session = params.includeSession ? Object.assign(sessionRow, Object.fromEntries(Object.entries(eventFields).filter(([, value]) => value !== void 0))) : void 0; if (session && sessionRow.key === "global" && !params.agentId) delete session.goal; return { ...session ? { session } : {}, ...eventFields, subagentRunState: sessionRow.subagentRunState, hasActiveSubagentRun: sessionRow.hasActiveSubagentRun }; } //#endregion export { buildGatewaySessionSnapshot as n, buildGatewaySessionEventFields as t };