diff options
| author | Yuren Hao <97327730+YurenHao0426@users.noreply.github.com> | 2026-09-13 11:07:18 +0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-13 11:07:18 +0700 |
| commit | c78a5ef589cacb5d7bdc7586b0cc270aded4b5fc (patch) | |
| tree | 69877995c0ecad3eb7266b1bb282c11a6f16dcdf /src/cli/localState.ts | |
| parent | 34f22876ab9e15e9f14300a8c21cd7109800d1ab (diff) | |
| parent | 1e81ba361eed444442fa98b0770e7ed78dae864b (diff) | |
Merge pull request #1 from YurenHao0426/agent-cli
Headless CLI mode for agents
Diffstat (limited to 'src/cli/localState.ts')
| -rw-r--r-- | src/cli/localState.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/cli/localState.ts b/src/cli/localState.ts new file mode 100644 index 0000000..73a1060 --- /dev/null +++ b/src/cli/localState.ts @@ -0,0 +1,76 @@ +// Manages per-clone state stored in <dir>/.lattex-cli.json +// and the global auth credential at ~/.config/lattex/auth.json + +import { join } from 'path' +import { readFile, writeFile, mkdir, chmod } from 'fs/promises' +import { existsSync } from 'fs' +import { homedir } from 'os' + +const CONFIG_DIR = join(homedir(), '.config', 'lattex') +const AUTH_PATH = join(CONFIG_DIR, 'auth.json') +const STATE_FILE = '.lattex-cli.json' + +// ── Global auth ── + +export async function saveCookie(cookie: string): Promise<void> { + await mkdir(CONFIG_DIR, { recursive: true }) + await writeFile(AUTH_PATH, JSON.stringify({ cookie }), { mode: 0o600 }) + // Ensure directory and file are user-only + await chmod(CONFIG_DIR, 0o700).catch(() => {}) + await chmod(AUTH_PATH, 0o600).catch(() => {}) +} + +export async function loadCookie(): Promise<string | null> { + try { + const data = JSON.parse(await readFile(AUTH_PATH, 'utf-8')) + return data.cookie || null + } catch { + return null + } +} + +// ── Per-clone state ── + +export interface CloneState { + projectId: string + projectName: string + /** docId → relPath */ + docPathMap: Record<string, string> + /** relPath → docId */ + pathDocMap: Record<string, string> + /** fileRefId → relPath */ + fileRefPathMap: Record<string, string> + /** relPath → fileRefId */ + pathFileRefMap: Record<string, string> + /** folderId → relDirPath */ + folderMap: Record<string, string> + /** relDirPath → folderId */ + pathFolderMap: Record<string, string> + rootFolderId: string + rootDocId: string + /** ISO timestamp of last pull */ + lastPull: string + /** sha256 hash of each file at last pull (relPath → hash) */ + fileHashes: Record<string, string> +} + +export function statePath(dir: string): string { + return join(dir, STATE_FILE) +} + +export async function loadState(dir: string): Promise<CloneState | null> { + const p = statePath(dir) + try { + return JSON.parse(await readFile(p, 'utf-8')) + } catch { + return null + } +} + +export async function saveState(dir: string, state: CloneState): Promise<void> { + await writeFile(statePath(dir), JSON.stringify(state, null, 2)) +} + +export function hasState(dir: string): boolean { + return existsSync(statePath(dir)) +} |
