| 53 | * Session Store 类 |
| 54 | */ |
| 55 | export class SessionStore { |
| 56 | private baseDir: string; |
| 57 | |
| 58 | constructor(baseDir: string = '.sessionstore') { |
| 59 | this.baseDir = path.resolve(process.cwd(), baseDir); |
| 60 | this.ensureDir(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * 确保存储目录存在 |
| 65 | */ |
| 66 | private ensureDir(): void { |
| 67 | if (!fs.existsSync(this.baseDir)) { |
| 68 | fs.mkdirSync(this.baseDir, {recursive: true}); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * 获取索引文件路径 |
| 74 | */ |
| 75 | private getIndexPath(): string { |
| 76 | return path.join(this.baseDir, 'index.json'); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * 获取 Session 文件路径 |
| 81 | */ |
| 82 | private getSessionPath(sessionId: string): string { |
| 83 | return path.join(this.baseDir, `${sessionId}.jsonl`); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * 读取索引 |
| 88 | */ |
| 89 | private readIndex(): SessionIndex { |
| 90 | const indexPath = this.getIndexPath(); |
| 91 | if (!fs.existsSync(indexPath)) { |
| 92 | return {sessions: []}; |
| 93 | } |
| 94 | try { |
| 95 | return JSON.parse(fs.readFileSync(indexPath, 'utf8')); |
| 96 | } catch { |
| 97 | return {sessions: []}; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * 写入索引 |
| 103 | */ |
| 104 | private writeIndex(index: SessionIndex): void { |
| 105 | fs.writeFileSync(this.getIndexPath(), JSON.stringify(index, null, 2)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * 生成唯一 ID |
| 110 | */ |
| 111 | private generateId(): string { |
| 112 | return `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; |
nothing calls this directly
no outgoing calls
no test coverage detected