UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

174 lines (173 loc) 6.63 kB
import { n as isRich, r as theme } from "./theme-vjDs9tao.js"; import { r as writeRuntimeJson } from "./runtime-B4lgFmsS.js"; import { i as getRuntimeConfig } from "./io-Gi7-pyU-.js"; import "./config-C9RxTsn1.js"; import { i as GATEWAY_CLIENT_NAMES, r as GATEWAY_CLIENT_MODES } from "./client-info-CcqJJIan.js"; import { m as isGatewayTransportError, o as callGateway } from "./call-B5-GYOlf.js"; import "./message-channel-BiOeMu0l.js"; import { i as serializeSessionCleanupResult, n as resolveSessionCleanupAction, r as runSessionsCleanup } from "./sessions-D6zZvoxX.js"; import { t as resolveSessionStoreTargetsOrExit } from "./session-store-targets-BGF93pOS.js"; import { c as resolveSessionDisplayModel, i as formatSessionModelCell, n as formatSessionFlagsCell, o as toSessionDisplayRows, r as formatSessionKeyCell, t as formatSessionAgeCell } from "./sessions-table-tqTiOISJ.js"; //#region src/commands/sessions-cleanup.ts /** * Session cleanup command. * * It can delegate cleanup to a live gateway or run local store maintenance, * with dry-run tables that explain every planned pruning action. */ const ACTION_PAD = 16; function formatCleanupActionCell(action, rich) { const label = action.padEnd(ACTION_PAD); if (!rich) return label; if (action === "keep") return theme.muted(label); if (action === "prune-missing") return theme.error(label); if (action === "prune-stale") return theme.warn(label); if (action === "retire-dm-scope") return theme.warn(label); if (action === "cap-overflow") return theme.accentBright(label); return theme.error(label); } function buildActionRows(params) { return toSessionDisplayRows(params.beforeStore).map((row) => Object.assign({}, row, { action: resolveSessionCleanupAction({ key: row.key, missingKeys: params.missingKeys, staleKeys: params.staleKeys, cappedKeys: params.cappedKeys, budgetEvictedKeys: params.budgetEvictedKeys, dmScopeRetiredKeys: params.dmScopeRetiredKeys }) })); } function renderStoreDryRunPlan(params) { const rich = isRich(); if (params.showAgentHeader) params.runtime.log(`Agent: ${params.summary.agentId}`); params.runtime.log(`Session store: ${params.summary.storePath}`); params.runtime.log(`Maintenance mode: ${params.summary.mode}`); params.runtime.log(`Entries: ${params.summary.beforeCount} -> ${params.summary.afterCount} (remove ${params.summary.beforeCount - params.summary.afterCount})`); params.runtime.log(`Would prune missing transcripts: ${params.summary.missing}`); params.runtime.log(`Would retire stale direct DM sessions: ${params.summary.dmScopeRetired}`); params.runtime.log(`Would prune stale: ${params.summary.pruned}`); params.runtime.log(`Would cap overflow: ${params.summary.capped}`); if (params.summary.unreferencedArtifacts?.scannedFiles) params.runtime.log(`Would prune unreferenced artifacts: ${params.summary.unreferencedArtifacts.removedFiles}`); if (params.summary.diskBudget) params.runtime.log(`Would enforce disk budget: ${params.summary.diskBudget.totalBytesBefore} -> ${params.summary.diskBudget.totalBytesAfter} bytes (files ${params.summary.diskBudget.removedFiles}, entries ${params.summary.diskBudget.removedEntries})`); if (params.actionRows.length === 0) return; params.runtime.log(""); params.runtime.log("Planned session actions:"); const header = [ "Action".padEnd(ACTION_PAD), "Key".padEnd(26), "Age".padEnd(9), "Model".padEnd(14), "Flags" ].join(" "); params.runtime.log(rich ? theme.heading(header) : header); for (const actionRow of params.actionRows) { const model = resolveSessionDisplayModel(params.cfg, actionRow); const line = [ formatCleanupActionCell(actionRow.action, rich), formatSessionKeyCell(actionRow.key, rich), formatSessionAgeCell(actionRow.updatedAt, rich), formatSessionModelCell(model, rich), formatSessionFlagsCell(actionRow, rich) ].join(" "); params.runtime.log(line.trimEnd()); } } function renderAppliedSummaries(params) { for (let i = 0; i < params.summaries.length; i += 1) { const summary = params.summaries[i]; if (!summary) continue; if (i > 0) params.runtime.log(""); if (params.summaries.length > 1) params.runtime.log(`Agent: ${summary.agentId}`); params.runtime.log(`Session store: ${summary.storePath}`); params.runtime.log(`Applied maintenance. Current entries: ${summary.appliedCount ?? 0}`); if (summary.unreferencedArtifacts?.removedFiles) params.runtime.log(`Pruned unreferenced artifacts: ${summary.unreferencedArtifacts.removedFiles}`); } } async function maybeRunGatewayCleanup(opts) { if (opts.store || opts.dryRun) return null; try { return await callGateway({ method: "sessions.cleanup", params: { agent: opts.agent, allAgents: opts.allAgents, enforce: opts.enforce, activeKey: opts.activeKey, fixMissing: opts.fixMissing, fixDmScope: opts.fixDmScope }, mode: GATEWAY_CLIENT_MODES.CLI, clientName: GATEWAY_CLIENT_NAMES.CLI, requiredMethods: ["sessions.cleanup"] }); } catch (error) { if (isGatewayTransportError(error)) return null; throw error; } } /** Runs session cleanup, optionally using the live gateway for active stores. */ async function sessionsCleanupCommand(opts, runtime) { const gatewayResult = await maybeRunGatewayCleanup(opts); if (gatewayResult) { if (opts.json) { writeRuntimeJson(runtime, gatewayResult); return; } renderAppliedSummaries({ summaries: "stores" in gatewayResult ? gatewayResult.stores : [gatewayResult], runtime }); return; } const cfg = getRuntimeConfig(); const targets = resolveSessionStoreTargetsOrExit({ cfg, opts: { store: opts.store, agent: opts.agent, allAgents: opts.allAgents }, runtime }); if (!targets) return; const { mode, previewResults, appliedSummaries } = await runSessionsCleanup({ cfg, opts, targets }); if (opts.dryRun) { if (opts.json) { writeRuntimeJson(runtime, serializeSessionCleanupResult({ mode, dryRun: true, summaries: previewResults.map((result) => result.summary) })); return; } for (let i = 0; i < previewResults.length; i += 1) { const result = previewResults[i]; if (i > 0) runtime.log(""); renderStoreDryRunPlan({ cfg, summary: result.summary, actionRows: buildActionRows(result), runtime, showAgentHeader: previewResults.length > 1 }); } return; } if (opts.json) { writeRuntimeJson(runtime, serializeSessionCleanupResult({ mode, dryRun: false, summaries: appliedSummaries })); return; } renderAppliedSummaries({ summaries: appliedSummaries, runtime }); } //#endregion export { sessionsCleanupCommand };