diff --git a/src/state/store.ts b/src/state/store.ts new file mode 100644 index 0000000..11f0456 --- /dev/null +++ b/src/state/store.ts @@ -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; + sessions: Record; + 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; + } +}