60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
import type { FlightRunState } from "./types.js";
|
|
|
|
const DEFAULT_STATE_DIR = path.join(
|
|
os.homedir(),
|
|
".openclaw",
|
|
"workspace",
|
|
"state",
|
|
"flight-finder"
|
|
);
|
|
|
|
export function getFlightFinderStatePath(
|
|
baseDir = DEFAULT_STATE_DIR
|
|
): string {
|
|
return path.join(baseDir, "flight-finder-run.json");
|
|
}
|
|
|
|
export async function saveFlightFinderRunState(
|
|
state: FlightRunState,
|
|
baseDir = DEFAULT_STATE_DIR
|
|
): Promise<string> {
|
|
await fs.mkdir(baseDir, { recursive: true });
|
|
const statePath = getFlightFinderStatePath(baseDir);
|
|
await fs.writeFile(statePath, JSON.stringify(state, null, 2));
|
|
return statePath;
|
|
}
|
|
|
|
export async function loadFlightFinderRunState(
|
|
baseDir = DEFAULT_STATE_DIR
|
|
): Promise<FlightRunState | null> {
|
|
const statePath = getFlightFinderStatePath(baseDir);
|
|
try {
|
|
const raw = await fs.readFile(statePath, "utf8");
|
|
return JSON.parse(raw) as FlightRunState;
|
|
} catch (error) {
|
|
const maybeNodeError = error as NodeJS.ErrnoException;
|
|
if (maybeNodeError.code === "ENOENT") {
|
|
return null;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function clearFlightFinderRunState(
|
|
baseDir = DEFAULT_STATE_DIR
|
|
): Promise<void> {
|
|
const statePath = getFlightFinderStatePath(baseDir);
|
|
try {
|
|
await fs.unlink(statePath);
|
|
} catch (error) {
|
|
const maybeNodeError = error as NodeJS.ErrnoException;
|
|
if (maybeNodeError.code !== "ENOENT") {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|