feat(session): paginated session list with infinite scroll and assistant mode improvements (#243)

* feat(session): paginated session list with infinite scroll and search

Replace per-directory useQueries session listing with a combined
cursor-based useInfiniteQuery (25 per page, loads more on scroll) and
move search into the API query instead of client-side filtering.

Add OpenCodeClient.listSessionsPage backed by the v2 /api/session
endpoint plus a toLegacySession adapter. Add invalidateSessionListCaches
to centralize session-list cache invalidation for infinite queries, and
update EventContext and useSSE to handle the new infinite-query cache
shape.

* fix(session): centralize and debounce session-list cache invalidation

Migrate ResetPermissionsDialog off the stale per-directory cache key
(which no longer matches the infinite-query key shape and hardcoded the
opencode URL) to the shared invalidateSessionListCaches helper, and drop
the now-unused repoDirectory prop and its call sites.

Debounce session-list invalidation on SSE session.updated events in
EventContext and useSSE so a burst of updates coalesces into a single
refetch instead of one per event. Fix the directory fallback in
EventContext getClient so it never returns the joined directory key.

* fix(permissions): scope instance dispose to the repo directory

Drop the session-list cache invalidation from ResetPermissionsDialog:
disposing the OpenCode instance does not change the persisted session
list, so the refetch was unnecessary, and the prior cache key no longer
matched the paginated shape.

Guard the reset-permissions route against an empty repo directory so it
never forwards /instance/dispose without a directory, which would
dispose the entire server instead of only the repo's instance.
This commit is contained in:
Chris Scott authored and GitHub committed 2026-05-31 10:32:14 -04:00
1 parent 78d934772e
commit eedbbfb94e
17 files changed
+931 -116

No files matched your search

+126
View File
@@ -42,4 +42,130 @@ describe('OpenCodeClient', () => {
'Workspace not found: wrk_stale',
)
})
describe('listSessionsPage', () => {
it('sends first-page params to /api/session with directory and returns adapted sessions', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [
{
id: 'ses_1',
projectID: 'proj_1',
workspaceID: 'ws_1',
parentID: 'parent_1',
title: 'My Session',
time: { created: 1000, updated: 2000 },
},
],
cursor: { next: 'cursor_next' },
}),
{ status: 200 },
),
)
const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({
limit: 25,
order: 'desc',
search: 'deploy',
})
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/api/session?limit=25&order=desc&search=deploy&directory=%2Frepo',
expect.any(Object),
)
expect(result.items).toHaveLength(1)
expect(result.items[0]).toMatchObject({
id: 'ses_1',
projectID: 'proj_1',
directory: '/repo',
parentID: 'parent_1',
title: 'My Session',
version: 'v2',
time: { created: 1000, updated: 2000 },
})
})
it('sends cursor-only params without directory for cursor-based requests', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [],
}),
{ status: 200 },
),
)
await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({ cursor: 'cursor_123' })
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/api/session?cursor=cursor_123',
expect.any(Object),
)
})
it('exposes response cursor as nextCursor', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [],
cursor: { next: 'next_1' },
}),
{ status: 200 },
),
)
const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({ limit: 10 })
expect(result.nextCursor).toBe('next_1')
})
it('uses Untitled Session for empty title', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [
{
id: 'ses_2',
projectID: 'proj_2',
title: '',
time: { created: 1000, updated: 2000 },
},
],
}),
{ status: 200 },
),
)
const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage()
expect(result.items[0].title).toBe('Untitled Session')
})
it('works without directory set', async () => {
fetchMock.mockResolvedValue(
new Response(
JSON.stringify({
items: [
{
id: 'ses_3',
projectID: 'proj_3',
title: 'No Dir',
time: { created: 1000, updated: 2000 },
},
],
}),
{ status: 200 },
),
)
const result = await new OpenCodeClient('/api/opencode').listSessionsPage({ limit: 5 })
expect(fetchMock).toHaveBeenCalledWith(
'http://localhost/api/opencode/api/session?limit=5',
expect.any(Object),
)
expect(result.items[0].directory).toBe('')
})
})
})