| 454 | * Get the most recent N chat sessions. |
| 455 | */ |
| 456 | export function getRecentChatSessions(db: DatabaseAdapter, limit: number): ChatSessionItem[] { |
| 457 | if (!hasTable(db, 'segment')) return [] |
| 458 | try { |
| 459 | return db |
| 460 | .prepare( |
| 461 | `SELECT |
| 462 | id, start_ts as startTs, end_ts as endTs, |
| 463 | message_count as messageCount, summary, |
| 464 | (SELECT mc.message_id FROM message_context mc |
| 465 | WHERE mc.segment_id = cs.id ORDER BY mc.message_id LIMIT 1) as firstMessageId |
| 466 | FROM segment cs |
| 467 | ORDER BY start_ts DESC |
| 468 | LIMIT ?` |
| 469 | ) |
| 470 | .all(limit) as unknown as ChatSessionItem[] |
| 471 | } catch { |
| 472 | return [] |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Timeline list of chat sessions with first message id and summary. |