@@ -18,8 +18,10 @@ import {
1818 bootstrapDirectory ,
1919 bootstrapGlobal ,
2020 clearProviderRev ,
21+ loadAgentsQuery ,
2122 loadGlobalConfigQuery ,
2223 loadPathQuery ,
24+ loadProjectsQuery ,
2325 loadProvidersQuery ,
2426} from "./global-sync/bootstrap"
2527import { createChildStoreManager } from "./global-sync/child-store"
@@ -33,6 +35,7 @@ import { formatServerError } from "@/utils/server-errors"
3335import { queryOptions , useMutation , useQueries , useQuery , useQueryClient } from "@tanstack/solid-query"
3436import { createRefreshQueue } from "./global-sync/queue"
3537import { directoryKey } from "./global-sync/utils"
38+ import { PathKey } from "@/utils/path-key"
3639
3740type GlobalStore = {
3841 ready : boolean
@@ -48,24 +51,33 @@ type GlobalStore = {
4851 reload : undefined | "pending" | "complete"
4952}
5053
51- export const loadSessionsQueryKey = ( directory : string ) => [ directory , "loadSessions" ] as const
52-
53- export const mcpQueryKey = ( directory : string ) => [ directory , "mcp" ] as const
54-
5554export const loadMcpQuery = ( directory : string , sdk : OpencodeClient ) =>
5655 queryOptions ( {
57- queryKey : mcpQueryKey ( directory ) ,
56+ queryKey : [ directory , "mcp" ] as const ,
5857 queryFn : ( ) => sdk . mcp . status ( ) . then ( ( r ) => r . data ?? { } ) ,
5958 } )
6059
61- export const lspQueryKey = ( directory : string ) => [ directory , "lsp" ] as const
62-
6360export const loadLspQuery = ( directory : string , sdk : OpencodeClient ) =>
6461 queryOptions ( {
65- queryKey : lspQueryKey ( directory ) ,
62+ queryKey : [ directory , "lsp" ] as const ,
6663 queryFn : ( ) => sdk . lsp . status ( ) . then ( ( r ) => r . data ?? [ ] ) ,
6764 } )
6865
66+ function makeQueryOptionsApi ( globalSDK : ( ) => OpencodeClient , sdkFor : ( dir : PathKey ) => OpencodeClient ) {
67+ return {
68+ globalConfig : ( ) => loadGlobalConfigQuery ( globalSDK ( ) ) ,
69+ projects : ( ) => loadProjectsQuery ( globalSDK ( ) ) ,
70+ providers : ( directory : PathKey | null ) =>
71+ loadProvidersQuery ( directory , directory === null ? globalSDK ( ) : sdkFor ( directory ) ) ,
72+ path : ( directory : PathKey | null ) => loadPathQuery ( directory , directory === null ? globalSDK ( ) : sdkFor ( directory ) ) ,
73+ agents : ( directory : PathKey ) => loadAgentsQuery ( directory , sdkFor ( directory ) ) ,
74+ mcp : ( directory : PathKey ) => loadMcpQuery ( directory , sdkFor ( directory ) ) ,
75+ lsp : ( directory : PathKey ) => loadLspQuery ( directory , sdkFor ( directory ) ) ,
76+ sessions : ( directory : PathKey ) => ( { queryKey : [ directory , "loadSessions" ] as const } ) ,
77+ }
78+ }
79+ export type QueryOptionsApi = ReturnType < typeof makeQueryOptionsApi >
80+
6981function createGlobalSync ( ) {
7082 const globalSDK = useGlobalSDK ( )
7183 const language = useLanguage ( )
@@ -77,12 +89,22 @@ function createGlobalSync() {
7789 const sessionLoads = new Map < string , Promise < void > > ( )
7890 const sessionMeta = new Map < string , { limit : number } > ( )
7991
92+ const sdkFor = ( directory : string ) => {
93+ const key = directoryKey ( directory )
94+ const cached = sdkCache . get ( key )
95+ if ( cached ) return cached
96+ const sdk = globalSDK . createClient ( {
97+ directory,
98+ throwOnError : true ,
99+ } )
100+ sdkCache . set ( key , sdk )
101+ return sdk
102+ }
103+
104+ const queryOptionsApi = makeQueryOptionsApi ( ( ) => globalSDK . client , sdkFor )
105+
80106 const [ configQuery , providerQuery , pathQuery ] = useQueries ( ( ) => ( {
81- queries : [
82- loadGlobalConfigQuery ( globalSDK . client ) ,
83- loadProvidersQuery ( null , globalSDK . client ) ,
84- loadPathQuery ( null , globalSDK . client ) ,
85- ] ,
107+ queries : [ queryOptionsApi . globalConfig ( ) , queryOptionsApi . providers ( null ) , queryOptionsApi . path ( null ) ] ,
86108 } ) )
87109
88110 const [ globalStore , setGlobalStore ] = createStore < GlobalStore > ( {
@@ -181,18 +203,6 @@ function createGlobalSync() {
181203 bootstrapInstance,
182204 } )
183205
184- const sdkFor = ( directory : string ) => {
185- const key = directoryKey ( directory )
186- const cached = sdkCache . get ( key )
187- if ( cached ) return cached
188- const sdk = globalSDK . createClient ( {
189- directory,
190- throwOnError : true ,
191- } )
192- sdkCache . set ( key , sdk )
193- return sdk
194- }
195-
196206 const children = createChildStoreManager ( {
197207 owner,
198208 isBooting : ( directory ) => booting . has ( directory ) ,
@@ -209,7 +219,7 @@ function createGlobalSync() {
209219 clearSessionPrefetchDirectory ( key )
210220 } ,
211221 translate : language . t ,
212- getSdk : sdkFor ,
222+ queryOptions : queryOptionsApi ,
213223 global : {
214224 provider : globalStore . provider ,
215225 } ,
@@ -239,7 +249,7 @@ function createGlobalSync() {
239249 const limit = Math . max ( store . limit + SESSION_RECENT_LIMIT , SESSION_RECENT_LIMIT )
240250 const promise = queryClient
241251 . fetchQuery ( {
242- queryKey : loadSessionsQueryKey ( key ) ,
252+ ... queryOptionsApi . sessions ( key ) ,
243253 queryFn : ( ) =>
244254 loadRootSessionsWithFallback ( {
245255 directory,
@@ -368,7 +378,7 @@ function createGlobalSync() {
368378 setSessionTodo,
369379 vcsCache : children . vcsCache . get ( key ) ,
370380 loadLsp : ( ) => {
371- void queryClient . fetchQuery ( loadLspQuery ( key , sdkFor ( directory ) ) )
381+ void queryClient . fetchQuery ( queryOptionsApi . lsp ( key ) )
372382 } ,
373383 } )
374384 } )
@@ -422,6 +432,7 @@ function createGlobalSync() {
422432 } ,
423433 child : children . child ,
424434 peek : children . peek ,
435+ queryOptions : queryOptionsApi ,
425436 // bootstrap,
426437 updateConfig : updateConfigMutation . mutateAsync ,
427438 project : projectApi ,
@@ -443,3 +454,7 @@ export function useGlobalSync() {
443454 if ( ! context ) throw new Error ( "useGlobalSync must be used within GlobalSyncProvider" )
444455 return context
445456}
457+
458+ export function useQueryOptions ( ) {
459+ return useGlobalSync ( ) . queryOptions
460+ }
0 commit comments