( limit: number = 100, )
| 449 | * List all available sessions with metadata (both local and remote) |
| 450 | */ |
| 451 | export async function listSessions( |
| 452 | limit: number = 100, |
| 453 | ): Promise<ExtendedSessionMetadata[]> { |
| 454 | try { |
| 455 | // Get local sessions |
| 456 | const localSessions = historyManager.list({ limit }); |
| 457 | |
| 458 | // Add first user message preview to each local session |
| 459 | const localSessionsWithPreview: ExtendedSessionMetadata[] = []; |
| 460 | |
| 461 | for (const sessionMeta of localSessions) { |
| 462 | const sessionFilePath = path.join( |
| 463 | getSessionDir(), |
| 464 | `${sessionMeta.sessionId}.json`, |
| 465 | ); |
| 466 | |
| 467 | if (fs.existsSync(sessionFilePath)) { |
| 468 | const metadata = getSessionMetadataWithPreview(sessionFilePath); |
| 469 | if (metadata) { |
| 470 | localSessionsWithPreview.push({ |
| 471 | ...metadata, |
| 472 | isRemote: false, |
| 473 | }); |
| 474 | } |
| 475 | } else { |
| 476 | // Fall back to basic metadata if file doesn't exist |
| 477 | localSessionsWithPreview.push({ |
| 478 | ...sessionMeta, |
| 479 | isRemote: false, |
| 480 | }); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Get remote sessions |
| 485 | const remoteSessions = await getRemoteSessions(); |
| 486 | |
| 487 | // Combine and sort by date (most recent first) |
| 488 | const allSessions = [...localSessionsWithPreview, ...remoteSessions] |
| 489 | .sort( |
| 490 | (a, b) => |
| 491 | new Date(b.dateCreated).getTime() - new Date(a.dateCreated).getTime(), |
| 492 | ) |
| 493 | .slice(0, limit); |
| 494 | |
| 495 | return allSessions; |
| 496 | } catch (error) { |
| 497 | logger.error("Error listing sessions:", error); |
| 498 | return []; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Load session by ID |
no test coverage detected