feat: add state store with JSON persistence
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
||||
import { homedir } from "os";
|
||||
import { join } from "path";
|
||||
|
||||
export interface SessionInfo {
|
||||
sessionId: string;
|
||||
cwd: string;
|
||||
status: "idle" | "busy";
|
||||
}
|
||||
|
||||
export interface AppState {
|
||||
projects: Record<string, string>;
|
||||
sessions: Record<string, SessionInfo>;
|
||||
activeSession: string | null;
|
||||
}
|
||||
|
||||
const STATE_DIR = join(homedir(), ".claude-telegram-bot");
|
||||
const STATE_FILE = join(STATE_DIR, "state.json");
|
||||
|
||||
const DEFAULT_STATE: AppState = {
|
||||
projects: {},
|
||||
sessions: {},
|
||||
activeSession: null,
|
||||
};
|
||||
|
||||
export class Store {
|
||||
private state: AppState;
|
||||
|
||||
constructor() {
|
||||
this.state = this.load();
|
||||
}
|
||||
|
||||
private load(): AppState {
|
||||
if (!existsSync(STATE_FILE)) {
|
||||
return { ...DEFAULT_STATE, projects: {}, sessions: {} };
|
||||
}
|
||||
const raw = readFileSync(STATE_FILE, "utf-8");
|
||||
return JSON.parse(raw) as AppState;
|
||||
}
|
||||
|
||||
save(): void {
|
||||
if (!existsSync(STATE_DIR)) {
|
||||
mkdirSync(STATE_DIR, { recursive: true });
|
||||
}
|
||||
writeFileSync(STATE_FILE, JSON.stringify(this.state, null, 2));
|
||||
}
|
||||
|
||||
get data(): AppState {
|
||||
return this.state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user