@@ -23,21 +23,62 @@ type LspStatusResponse = paths['/lsp']['get']['responses']['200']['content']['ap
2323type LspStatus = LspStatusResponse [ number ]
2424
2525type LegacySession = SessionListResponse [ number ]
26- type SessionV2Info = {
26+
27+ /** Pre-v1.16.0 session shape returned by /api/session */
28+ type SessionV2InfoV1 = {
2729 id : string
2830 parentID ?: string
2931 projectID : string
3032 workspaceID ?: string
3133 title : string
32- time : LegacySession [ 'time' ]
34+ time : { created : number ; updated : number ; compacting ?: number ; archived ?: number }
3335 path ?: unknown
3436}
37+
38+ /** v1.16.0+ session shape returned by /api/session */
39+ type SessionV2InfoV2 = {
40+ id : string
41+ parentID ?: string
42+ projectID : string
43+ title : string
44+ time : { created : number ; updated : number ; archived ?: number }
45+ location : { directory : string ; workspaceID ?: string }
46+ agent ?: string
47+ model ?: { id : string ; providerID : string ; variant ?: string }
48+ cost : number
49+ tokens : { input : number ; output : number ; reasoning : number ; cache : { read : number ; write : number } }
50+ subpath ?: string
51+ }
52+
53+ type SessionV2Info = SessionV2InfoV1 | SessionV2InfoV2
3554type SessionPageCursor = { previous ?: string ; next ?: string }
36- type SessionPageResponse = { items : SessionV2Info [ ] ; cursor ?: SessionPageCursor }
55+
56+ /** Response from /api/session — may be old (items) or new (data) format */
57+ type SessionPageResponse = {
58+ data ?: SessionV2InfoV2 [ ]
59+ items ?: SessionV2InfoV1 [ ]
60+ cursor ?: SessionPageCursor
61+ }
3762type SessionPageParams = { limit ?: number ; order ?: 'asc' | 'desc' ; search ?: string ; cursor ?: string }
3863type SessionPage = { items : LegacySession [ ] ; nextCursor ?: string }
3964
65+ function isNewSession ( session : SessionV2Info ) : session is SessionV2InfoV2 {
66+ return 'location' in session && session . location !== undefined
67+ }
68+
4069function toLegacySession ( session : SessionV2Info , directory ?: string ) : LegacySession {
70+ if ( isNewSession ( session ) ) {
71+ return {
72+ id : session . id ,
73+ projectID : session . projectID ,
74+ workspaceID : session . location . workspaceID ,
75+ directory : directory ?? session . location . directory ?? '' ,
76+ parentID : session . parentID ,
77+ title : session . title || 'Untitled Session' ,
78+ version : 'v2' ,
79+ time : session . time ,
80+ } as LegacySession
81+ }
4182 return {
4283 id : session . id ,
4384 projectID : session . projectID ,
@@ -88,8 +129,9 @@ export class OpenCodeClient {
88129 const response = await fetchWrapper < SessionPageResponse > ( `${ this . baseURL } /api/session` , {
89130 params : queryParams ,
90131 } )
132+ const rawItems = response . data ?? response . items ?? [ ]
91133 return {
92- items : response . items . map ( ( item ) => toLegacySession ( item , this . directory ) ) ,
134+ items : rawItems . map ( ( item ) => toLegacySession ( item , this . directory ) ) ,
93135 nextCursor : response . cursor ?. next ,
94136 }
95137 }
0 commit comments