57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { logger } from './logger.js';
|
|
|
|
interface SessionData {
|
|
claudeSessionId: string;
|
|
createdAt: string;
|
|
lastUsedAt: string;
|
|
}
|
|
|
|
export class SessionStore {
|
|
constructor(private sessionDir: string) {
|
|
fs.mkdirSync(this.sessionDir, { recursive: true });
|
|
}
|
|
|
|
private filePath(id: string): string {
|
|
const safe = id.replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
return path.join(this.sessionDir, `${safe}.json`);
|
|
}
|
|
|
|
get(id: string): string | null {
|
|
const fp = this.filePath(id);
|
|
if (!fs.existsSync(fp)) return null;
|
|
try {
|
|
const data: SessionData = JSON.parse(fs.readFileSync(fp, 'utf-8'));
|
|
return data.claudeSessionId;
|
|
} catch (err) {
|
|
logger.warn({ err, id }, 'Failed to read session');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
save(id: string, claudeSessionId: string): void {
|
|
const fp = this.filePath(id);
|
|
const existing = fs.existsSync(fp);
|
|
const data: SessionData = {
|
|
claudeSessionId,
|
|
createdAt: existing
|
|
? JSON.parse(fs.readFileSync(fp, 'utf-8')).createdAt
|
|
: new Date().toISOString(),
|
|
lastUsedAt: new Date().toISOString(),
|
|
};
|
|
fs.writeFileSync(fp, JSON.stringify(data, null, 2));
|
|
logger.debug({ id, claudeSessionId }, 'Session saved');
|
|
}
|
|
|
|
list(): string[] {
|
|
try {
|
|
return fs.readdirSync(this.sessionDir)
|
|
.filter(f => f.endsWith('.json'))
|
|
.map(f => f.replace(/\.json$/, ''));
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
}
|