|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { vi } from "vitest"; |
| 4 | +import type { OpenClawConfig } from "../config/config.js"; |
| 5 | +import { |
| 6 | + resolveSessionStorePathCore, |
| 7 | + resolveSessionTranscriptsDirForAgent, |
| 8 | +} from "../config/sessions/paths.js"; |
| 9 | +import { noteStateIntegrity as noteStateIntegrityRaw } from "./doctor-state-integrity.js"; |
| 10 | + |
| 11 | +export const noteMock = vi.fn(); |
| 12 | + |
| 13 | +export function withMainAgentRoster(cfg: OpenClawConfig): OpenClawConfig { |
| 14 | + if (cfg.agents?.entries || cfg.agents?.list) { |
| 15 | + return cfg; |
| 16 | + } |
| 17 | + return { |
| 18 | + ...cfg, |
| 19 | + agents: { ...cfg.agents, entries: { main: { default: true } } }, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +export async function noteStateIntegrity( |
| 24 | + cfg: OpenClawConfig, |
| 25 | + prompter: Parameters<typeof noteStateIntegrityRaw>[1], |
| 26 | + configPath?: string, |
| 27 | +) { |
| 28 | + return noteStateIntegrityRaw(withMainAgentRoster(cfg), prompter, configPath); |
| 29 | +} |
| 30 | + |
| 31 | +export function setupSessionState(cfg: OpenClawConfig, env: NodeJS.ProcessEnv, homeDir: string) { |
| 32 | + const agentId = "main"; |
| 33 | + const sessionsDir = resolveSessionTranscriptsDirForAgent(agentId, env, () => homeDir); |
| 34 | + const storePath = resolveSessionStorePathCore(cfg.session?.store, { agentId }); |
| 35 | + fs.mkdirSync(sessionsDir, { recursive: true }); |
| 36 | + fs.mkdirSync(path.dirname(storePath), { recursive: true }); |
| 37 | +} |
| 38 | + |
| 39 | +export function stateIntegrityText(): string { |
| 40 | + return noteMock.mock.calls |
| 41 | + .filter((call) => call[1] === "State integrity") |
| 42 | + .map((call) => String(call[0])) |
| 43 | + .join("\n"); |
| 44 | +} |
| 45 | + |
| 46 | +export function doctorChangesText(): string { |
| 47 | + return noteMock.mock.calls |
| 48 | + .filter((call) => call[1] === "Doctor changes") |
| 49 | + .map((call) => String(call[0])) |
| 50 | + .join("\n"); |
| 51 | +} |
| 52 | + |
| 53 | +export type RuntimeRepairPrompt = { |
| 54 | + initialValue?: boolean; |
| 55 | + message?: string; |
| 56 | + requiresInteractiveConfirmation?: boolean; |
| 57 | +}; |
| 58 | + |
| 59 | +export function repairPromptCalls(confirmRuntimeRepair: { |
| 60 | + mock: { calls: unknown[][] }; |
| 61 | +}): RuntimeRepairPrompt[] { |
| 62 | + return confirmRuntimeRepair.mock.calls.map((call) => call[0] as RuntimeRepairPrompt); |
| 63 | +} |
| 64 | + |
| 65 | +export function hasRepairPromptMessage( |
| 66 | + confirmRuntimeRepair: { mock: { calls: unknown[][] } }, |
| 67 | + text: string, |
| 68 | +): boolean { |
| 69 | + return repairPromptCalls(confirmRuntimeRepair).some((prompt) => prompt.message?.includes(text)); |
| 70 | +} |
| 71 | + |
| 72 | +export function writeSessionStore( |
| 73 | + cfg: OpenClawConfig, |
| 74 | + sessions: Record<string, { sessionId: string; updatedAt: number } & Record<string, unknown>>, |
| 75 | +) { |
| 76 | + setupSessionState(cfg, process.env, process.env.HOME ?? ""); |
| 77 | + const storePath = resolveSessionStorePathCore(cfg.session?.store, { agentId: "main" }); |
| 78 | + fs.writeFileSync(storePath, JSON.stringify(sessions, null, 2)); |
| 79 | +} |
| 80 | + |
| 81 | +export async function runStateIntegrityText(cfg: OpenClawConfig): Promise<string> { |
| 82 | + await noteStateIntegrity(withMainAgentRoster(cfg), { |
| 83 | + confirmRuntimeRepair: vi.fn(async () => false), |
| 84 | + note: noteMock, |
| 85 | + }); |
| 86 | + return stateIntegrityText(); |
| 87 | +} |
0 commit comments