| 257 | } |
| 258 | |
| 259 | function validateFile(value: unknown): SessionRecord[] { |
| 260 | if (typeof value !== 'object' || value === null) { |
| 261 | throw new Error('sessions.json: root must be an object'); |
| 262 | } |
| 263 | const v = value as Record<string, unknown>; |
| 264 | if (v['version'] !== 1) { |
| 265 | throw new Error(`sessions.json: unsupported version ${String(v['version'])}`); |
| 266 | } |
| 267 | if (!Array.isArray(v['records'])) { |
| 268 | throw new Error('sessions.json: records must be an array'); |
| 269 | } |
| 270 | const out: SessionRecord[] = []; |
| 271 | for (let i = 0; i < v['records'].length; i++) { |
| 272 | const e = v['records'][i]; |
| 273 | if (typeof e !== 'object' || e === null) { |
| 274 | throw new Error(`sessions.json: record ${i} not an object`); |
| 275 | } |
| 276 | const r = e as Record<string, unknown>; |
| 277 | if ( |
| 278 | typeof r['id'] !== 'string' || |
| 279 | typeof r['wsId'] !== 'string' || |
| 280 | typeof r['agent'] !== 'string' || |
| 281 | typeof r['name'] !== 'string' || |
| 282 | typeof r['createdAt'] !== 'string' || |
| 283 | typeof r['lastActiveAt'] !== 'string' || |
| 284 | (r['state'] !== 'running' && r['state'] !== 'paused') |
| 285 | ) { |
| 286 | throw new Error(`sessions.json: record ${i} has wrong shape`); |
| 287 | } |
| 288 | const base: SessionRecord = { |
| 289 | id: r['id'], |
| 290 | wsId: r['wsId'], |
| 291 | agent: r['agent'], |
| 292 | name: r['name'], |
| 293 | createdAt: r['createdAt'], |
| 294 | lastActiveAt: r['lastActiveAt'], |
| 295 | state: r['state'], |
| 296 | // Carry the session title (the captured first message) across reloads — |
| 297 | // it's written to disk by `flush`, so it must be read back here too, or |
| 298 | // every server restart / registry reload reverts the row to the `c1` name. |
| 299 | ...(typeof r['title'] === 'string' ? { title: r['title'] } : {}), |
| 300 | }; |
| 301 | const hint = r['resumeHint']; |
| 302 | if ( |
| 303 | typeof hint === 'object' && hint !== null && |
| 304 | (hint as Record<string, unknown>)['kind'] === 'agent-session-id' && |
| 305 | typeof (hint as Record<string, unknown>)['value'] === 'string' |
| 306 | ) { |
| 307 | base.resumeHint = { kind: 'agent-session-id', value: (hint as { value: string }).value }; |
| 308 | } |
| 309 | if (typeof r['scrollbackFile'] === 'string') { |
| 310 | base.scrollbackFile = r['scrollbackFile']; |
| 311 | } |
| 312 | out.push(base); |
| 313 | } |
| 314 | return out; |
| 315 | } |