From 4e8ada9ac4318f986f9ed81855ceb0e9105dba03 Mon Sep 17 00:00:00 2001 From: neken Date: Fri, 10 Apr 2026 15:05:18 +0500 Subject: [PATCH] feat: add state store with JSON persistence --- src/state/store.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/state/store.ts 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; + } +}