22 * TUI data layer — pure RPC client, no direct SQLite access.
33 * All data is fetched from the server plugin via HTTP RPC.
44 */
5- import os from "node:os"
6- import path from "node:path"
7- import { MagicContextRpcClient } from "../../shared/rpc-client"
8- import type { SidebarSnapshot , StatusDetail , RpcNotificationMessage } from "../../shared/rpc-types"
5+ import os from "node:os" ;
6+ import path from "node:path" ;
7+ import { MagicContextRpcClient } from "../../shared/rpc-client" ;
8+ import type { RpcNotificationMessage , SidebarSnapshot , StatusDetail } from "../../shared/rpc-types" ;
99
10- export type { SidebarSnapshot , StatusDetail }
10+ export type { SidebarSnapshot , StatusDetail } ;
1111
12- let rpcClient : MagicContextRpcClient | null = null
12+ let rpcClient : MagicContextRpcClient | null = null ;
1313
1414function getStorageDir ( ) : string {
15- const dataDir = process . env . XDG_DATA_HOME ?? path . join ( os . homedir ( ) , ".local" , "share" )
16- return path . join ( dataDir , "opencode" , "storage" , "plugin" , "magic-context" )
15+ const dataDir = process . env . XDG_DATA_HOME ?? path . join ( os . homedir ( ) , ".local" , "share" ) ;
16+ return path . join ( dataDir , "opencode" , "storage" , "plugin" , "magic-context" ) ;
1717}
1818
1919/** Initialize the RPC client. Call once on TUI startup. */
2020export function initRpcClient ( directory : string ) : void {
21- const storageDir = getStorageDir ( )
22- rpcClient = new MagicContextRpcClient ( storageDir , directory )
21+ const storageDir = getStorageDir ( ) ;
22+ rpcClient = new MagicContextRpcClient ( storageDir , directory ) ;
2323}
2424
2525/** Clean up the RPC client. */
2626export function closeRpc ( ) : void {
27- rpcClient ?. reset ( )
28- rpcClient = null
27+ rpcClient ?. reset ( ) ;
28+ rpcClient = null ;
2929}
3030
3131const EMPTY_SNAPSHOT : SidebarSnapshot = {
@@ -48,25 +48,25 @@ const EMPTY_SNAPSHOT: SidebarSnapshot = {
4848 compartmentTokens : 0 ,
4949 factTokens : 0 ,
5050 memoryTokens : 0 ,
51- }
51+ } ;
5252
5353/** Fetch sidebar snapshot from the server via RPC. */
5454export async function loadSidebarSnapshot (
5555 sessionId : string ,
5656 directory : string ,
5757) : Promise < SidebarSnapshot > {
58- if ( ! rpcClient ) return { ...EMPTY_SNAPSHOT , sessionId }
58+ if ( ! rpcClient ) return { ...EMPTY_SNAPSHOT , sessionId } ;
5959 try {
6060 const result = await rpcClient . call < SidebarSnapshot > ( "sidebar-snapshot" , {
6161 sessionId,
6262 directory,
63- } )
63+ } ) ;
6464 if ( ( result as unknown as Record < string , unknown > ) . error ) {
65- return { ...EMPTY_SNAPSHOT , sessionId }
65+ return { ...EMPTY_SNAPSHOT , sessionId } ;
6666 }
67- return result
67+ return result ;
6868 } catch {
69- return { ...EMPTY_SNAPSHOT , sessionId }
69+ return { ...EMPTY_SNAPSHOT , sessionId } ;
7070 }
7171}
7272
@@ -102,65 +102,65 @@ export async function loadStatusDetail(
102102 historyBlockTokens : 0 ,
103103 compressionBudget : null ,
104104 compressionUsage : null ,
105- }
105+ } ;
106106
107- if ( ! rpcClient ) return emptyDetail
107+ if ( ! rpcClient ) return emptyDetail ;
108108 try {
109109 const result = await rpcClient . call < StatusDetail > ( "status-detail" , {
110110 sessionId,
111111 directory,
112112 modelKey,
113- } )
113+ } ) ;
114114 if ( ( result as unknown as Record < string , unknown > ) . error ) {
115- return emptyDetail
115+ return emptyDetail ;
116116 }
117- return result
117+ return result ;
118118 } catch {
119- return emptyDetail
119+ return emptyDetail ;
120120 }
121121}
122122
123123/** Get compartment count via RPC. */
124124export async function getCompartmentCount ( sessionId : string ) : Promise < number > {
125- if ( ! rpcClient ) return 0
125+ if ( ! rpcClient ) return 0 ;
126126 try {
127- const result = await rpcClient . call < { count : number } > ( "compartment-count" , { sessionId } )
128- return result . count ?? 0
127+ const result = await rpcClient . call < { count : number } > ( "compartment-count" , { sessionId } ) ;
128+ return result . count ?? 0 ;
129129 } catch {
130- return 0
130+ return 0 ;
131131 }
132132}
133133
134134/** Send recomp request to server via RPC. */
135135export async function requestRecomp ( sessionId : string ) : Promise < boolean > {
136- if ( ! rpcClient ) return false
136+ if ( ! rpcClient ) return false ;
137137 try {
138- const result = await rpcClient . call < { ok : boolean } > ( "recomp" , { sessionId } )
139- return result . ok ?? false
138+ const result = await rpcClient . call < { ok : boolean } > ( "recomp" , { sessionId } ) ;
139+ return result . ok ?? false ;
140140 } catch {
141- return false
141+ return false ;
142142 }
143143}
144144
145145export interface TuiMessage {
146- type : string
147- payload : Record < string , unknown >
148- sessionId ?: string
146+ type : string ;
147+ payload : Record < string , unknown > ;
148+ sessionId ?: string ;
149149}
150150
151151/** Poll for pending server→TUI notifications via RPC. */
152152export async function consumeTuiMessages ( ) : Promise < TuiMessage [ ] > {
153- if ( ! rpcClient ) return [ ]
153+ if ( ! rpcClient ) return [ ] ;
154154 try {
155155 const result = await rpcClient . call < { messages : RpcNotificationMessage [ ] } > (
156156 "pending-notifications" ,
157- )
157+ ) ;
158158 return ( result . messages ?? [ ] ) . map ( ( m ) => ( {
159159 type : m . type ,
160160 payload : m . payload ,
161161 sessionId : m . sessionId ,
162- } ) )
162+ } ) ) ;
163163 } catch {
164- return [ ]
164+ return [ ] ;
165165 }
166166}
0 commit comments