diff --git a/frontend/src/api/opencode.test.ts b/frontend/src/api/opencode.test.ts index a69dc017..ad8cde86 100644 --- a/frontend/src/api/opencode.test.ts +++ b/frontend/src/api/opencode.test.ts @@ -44,6 +44,45 @@ describe('OpenCodeClient', () => { }) describe('listSessionsPage', () => { + it('handles v1.16.0+ API response format with data envelope and nested location', async () => { + fetchMock.mockResolvedValue( + new Response( + JSON.stringify({ + data: [ + { + id: 'ses_v2_1', + projectID: 'proj_v2', + parentID: 'parent_v2', + title: 'V2 Session', + time: { created: 3000, updated: 4000 }, + location: { directory: '/my-repo', workspaceID: 'ws_v2' }, + agent: 'default', + model: { id: 'gpt-4', providerID: 'openai' }, + cost: 0.05, + tokens: { input: 100, output: 50, reasoning: 10, cache: { read: 0, write: 0 } }, + }, + ], + cursor: { next: 'v2_cursor' }, + }), + { status: 200 }, + ), + ) + + const result = await new OpenCodeClient('/api/opencode', '/repo').listSessionsPage({ limit: 10 }) + + expect(result.items).toHaveLength(1) + expect(result.items[0]).toMatchObject({ + id: 'ses_v2_1', + projectID: 'proj_v2', + workspaceID: 'ws_v2', + directory: '/repo', + parentID: 'parent_v2', + title: 'V2 Session', + version: 'v2', + }) + expect(result.nextCursor).toBe('v2_cursor') + }) + it('sends first-page params to /api/session with directory and returns adapted sessions', async () => { fetchMock.mockResolvedValue( new Response( diff --git a/frontend/src/api/opencode.ts b/frontend/src/api/opencode.ts index 83dc1e37..8e7bbe1e 100644 --- a/frontend/src/api/opencode.ts +++ b/frontend/src/api/opencode.ts @@ -23,21 +23,62 @@ type LspStatusResponse = paths['/lsp']['get']['responses']['200']['content']['ap type LspStatus = LspStatusResponse[number] type LegacySession = SessionListResponse[number] -type SessionV2Info = { + +/** Pre-v1.16.0 session shape returned by /api/session */ +type SessionV2InfoV1 = { id: string parentID?: string projectID: string workspaceID?: string title: string - time: LegacySession['time'] + time: { created: number; updated: number; compacting?: number; archived?: number } path?: unknown } + +/** v1.16.0+ session shape returned by /api/session */ +type SessionV2InfoV2 = { + id: string + parentID?: string + projectID: string + title: string + time: { created: number; updated: number; archived?: number } + location: { directory: string; workspaceID?: string } + agent?: string + model?: { id: string; providerID: string; variant?: string } + cost: number + tokens: { input: number; output: number; reasoning: number; cache: { read: number; write: number } } + subpath?: string +} + +type SessionV2Info = SessionV2InfoV1 | SessionV2InfoV2 type SessionPageCursor = { previous?: string; next?: string } -type SessionPageResponse = { items: SessionV2Info[]; cursor?: SessionPageCursor } + +/** Response from /api/session — may be old (items) or new (data) format */ +type SessionPageResponse = { + data?: SessionV2InfoV2[] + items?: SessionV2InfoV1[] + cursor?: SessionPageCursor +} type SessionPageParams = { limit?: number; order?: 'asc' | 'desc'; search?: string; cursor?: string } type SessionPage = { items: LegacySession[]; nextCursor?: string } +function isNewSession(session: SessionV2Info): session is SessionV2InfoV2 { + return 'location' in session && session.location !== undefined +} + function toLegacySession(session: SessionV2Info, directory?: string): LegacySession { + if (isNewSession(session)) { + return { + id: session.id, + projectID: session.projectID, + workspaceID: session.location.workspaceID, + directory: directory ?? session.location.directory ?? '', + parentID: session.parentID, + title: session.title || 'Untitled Session', + version: 'v2', + time: session.time, + } as LegacySession + } return { id: session.id, projectID: session.projectID, @@ -88,8 +129,9 @@ export class OpenCodeClient { const response = await fetchWrapper(`${this.baseURL}/api/session`, { params: queryParams, }) + const rawItems = response.data ?? response.items ?? [] return { - items: response.items.map((item) => toLegacySession(item, this.directory)), + items: rawItems.map((item) => toLegacySession(item, this.directory)), nextCursor: response.cursor?.next, } }