src / state.ts
import { readFile, writeFile, mkdir, appendFile } from "fs/promises";
import { join } from "path";
import * as os from "os";
const STATE_DIR = join(os.homedir(), ".arkantu-core-tools");
const STATE_FILE = join(STATE_DIR, "state.json");
export interface PluginState {
cwd: string;
}
export async function loadState(defaultCwd: string): Promise<PluginState> {
try {
const raw = await readFile(STATE_FILE, "utf-8");
const parsed = JSON.parse(raw);
return { cwd: parsed.cwd ?? defaultCwd };
} catch {
return { cwd: defaultCwd };
}
}
export async function saveState(state: PluginState): Promise<void> {
try {
await mkdir(STATE_DIR, { recursive: true });
await writeFile(STATE_FILE, JSON.stringify(state, null, 2), "utf-8");
} catch (e) {
console.error("[state] Failed to save state:", e);
}
}
export async function ensureDir(path: string): Promise<void> {
await mkdir(path, { recursive: true });
}
export async function appendMemory(cwd: string, fact: string): Promise<void> {
const memFile = join(cwd, "memory.md");
const entry = `\n- [${new Date().toISOString()}] ${fact}`;
try {
await appendFile(memFile, entry, "utf-8");
} catch {
await writeFile(memFile, `# Memoria\n${entry}`, "utf-8");
}
}