| 431 | * Query chat sessions within a time range. |
| 432 | */ |
| 433 | export function getSessionsByTimeRange(db: DatabaseAdapter, startTs: number, endTs: number): ChatSessionItem[] { |
| 434 | if (!hasTable(db, 'segment')) return [] |
| 435 | try { |
| 436 | return db |
| 437 | .prepare( |
| 438 | `SELECT |
| 439 | id, start_ts as startTs, end_ts as endTs, |
| 440 | message_count as messageCount, summary, |
| 441 | (SELECT mc.message_id FROM message_context mc |
| 442 | WHERE mc.segment_id = cs.id ORDER BY mc.message_id LIMIT 1) as firstMessageId |
| 443 | FROM segment cs |
| 444 | WHERE start_ts >= ? AND start_ts <= ? |
| 445 | ORDER BY start_ts DESC` |
| 446 | ) |
| 447 | .all(startTs, endTs) as unknown as ChatSessionItem[] |
| 448 | } catch { |
| 449 | return [] |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Get the most recent N chat sessions. |