|
|
@@ -18,8 +18,10 @@ import {
|
|
|
bootstrapDirectory,
|
|
|
bootstrapGlobal,
|
|
|
clearProviderRev,
|
|
|
+ loadAgentsQuery,
|
|
|
loadGlobalConfigQuery,
|
|
|
loadPathQuery,
|
|
|
+ loadProjectsQuery,
|
|
|
loadProvidersQuery,
|
|
|
} from "./global-sync/bootstrap"
|
|
|
import { createChildStoreManager } from "./global-sync/child-store"
|
|
|
@@ -33,6 +35,7 @@ import { formatServerError } from "@/utils/server-errors"
|
|
|
import { queryOptions, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/solid-query"
|
|
|
import { createRefreshQueue } from "./global-sync/queue"
|
|
|
import { directoryKey } from "./global-sync/utils"
|
|
|
+import { PathKey } from "@/utils/path-key"
|
|
|
|
|
|
type GlobalStore = {
|
|
|
ready: boolean
|
|
|
@@ -48,24 +51,33 @@ type GlobalStore = {
|
|
|
reload: undefined | "pending" | "complete"
|
|
|
}
|
|
|
|
|
|
-export const loadSessionsQueryKey = (directory: string) => [directory, "loadSessions"] as const
|
|
|
-
|
|
|
-export const mcpQueryKey = (directory: string) => [directory, "mcp"] as const
|
|
|
-
|
|
|
export const loadMcpQuery = (directory: string, sdk: OpencodeClient) =>
|
|
|
queryOptions({
|
|
|
- queryKey: mcpQueryKey(directory),
|
|
|
+ queryKey: [directory, "mcp"] as const,
|
|
|
queryFn: () => sdk.mcp.status().then((r) => r.data ?? {}),
|
|
|
})
|
|
|
|
|
|
-export const lspQueryKey = (directory: string) => [directory, "lsp"] as const
|
|
|
-
|
|
|
export const loadLspQuery = (directory: string, sdk: OpencodeClient) =>
|
|
|
queryOptions({
|
|
|
- queryKey: lspQueryKey(directory),
|
|
|
+ queryKey: [directory, "lsp"] as const,
|
|
|
queryFn: () => sdk.lsp.status().then((r) => r.data ?? []),
|
|
|
})
|
|
|
|
|
|
+function makeQueryOptionsApi(globalSDK: () => OpencodeClient, sdkFor: (dir: PathKey) => OpencodeClient) {
|
|
|
+ return {
|
|
|
+ globalConfig: () => loadGlobalConfigQuery(globalSDK()),
|
|
|
+ projects: () => loadProjectsQuery(globalSDK()),
|
|
|
+ providers: (directory: PathKey | null) =>
|
|
|
+ loadProvidersQuery(directory, directory === null ? globalSDK() : sdkFor(directory)),
|
|
|
+ path: (directory: PathKey | null) => loadPathQuery(directory, directory === null ? globalSDK() : sdkFor(directory)),
|
|
|
+ agents: (directory: PathKey) => loadAgentsQuery(directory, sdkFor(directory)),
|
|
|
+ mcp: (directory: PathKey) => loadMcpQuery(directory, sdkFor(directory)),
|
|
|
+ lsp: (directory: PathKey) => loadLspQuery(directory, sdkFor(directory)),
|
|
|
+ sessions: (directory: PathKey) => ({ queryKey: [directory, "loadSessions"] as const }),
|
|
|
+ }
|
|
|
+}
|
|
|
+export type QueryOptionsApi = ReturnType<typeof makeQueryOptionsApi>
|
|
|
+
|
|
|
function createGlobalSync() {
|
|
|
const globalSDK = useGlobalSDK()
|
|
|
const language = useLanguage()
|
|
|
@@ -77,12 +89,22 @@ function createGlobalSync() {
|
|
|
const sessionLoads = new Map<string, Promise<void>>()
|
|
|
const sessionMeta = new Map<string, { limit: number }>()
|
|
|
|
|
|
+ const sdkFor = (directory: string) => {
|
|
|
+ const key = directoryKey(directory)
|
|
|
+ const cached = sdkCache.get(key)
|
|
|
+ if (cached) return cached
|
|
|
+ const sdk = globalSDK.createClient({
|
|
|
+ directory,
|
|
|
+ throwOnError: true,
|
|
|
+ })
|
|
|
+ sdkCache.set(key, sdk)
|
|
|
+ return sdk
|
|
|
+ }
|
|
|
+
|
|
|
+ const queryOptionsApi = makeQueryOptionsApi(() => globalSDK.client, sdkFor)
|
|
|
+
|
|
|
const [configQuery, providerQuery, pathQuery] = useQueries(() => ({
|
|
|
- queries: [
|
|
|
- loadGlobalConfigQuery(globalSDK.client),
|
|
|
- loadProvidersQuery(null, globalSDK.client),
|
|
|
- loadPathQuery(null, globalSDK.client),
|
|
|
- ],
|
|
|
+ queries: [queryOptionsApi.globalConfig(), queryOptionsApi.providers(null), queryOptionsApi.path(null)],
|
|
|
}))
|
|
|
|
|
|
const [globalStore, setGlobalStore] = createStore<GlobalStore>({
|
|
|
@@ -181,18 +203,6 @@ function createGlobalSync() {
|
|
|
bootstrapInstance,
|
|
|
})
|
|
|
|
|
|
- const sdkFor = (directory: string) => {
|
|
|
- const key = directoryKey(directory)
|
|
|
- const cached = sdkCache.get(key)
|
|
|
- if (cached) return cached
|
|
|
- const sdk = globalSDK.createClient({
|
|
|
- directory,
|
|
|
- throwOnError: true,
|
|
|
- })
|
|
|
- sdkCache.set(key, sdk)
|
|
|
- return sdk
|
|
|
- }
|
|
|
-
|
|
|
const children = createChildStoreManager({
|
|
|
owner,
|
|
|
isBooting: (directory) => booting.has(directory),
|
|
|
@@ -209,7 +219,7 @@ function createGlobalSync() {
|
|
|
clearSessionPrefetchDirectory(key)
|
|
|
},
|
|
|
translate: language.t,
|
|
|
- getSdk: sdkFor,
|
|
|
+ queryOptions: queryOptionsApi,
|
|
|
global: {
|
|
|
provider: globalStore.provider,
|
|
|
},
|
|
|
@@ -239,7 +249,7 @@ function createGlobalSync() {
|
|
|
const limit = Math.max(store.limit + SESSION_RECENT_LIMIT, SESSION_RECENT_LIMIT)
|
|
|
const promise = queryClient
|
|
|
.fetchQuery({
|
|
|
- queryKey: loadSessionsQueryKey(key),
|
|
|
+ ...queryOptionsApi.sessions(key),
|
|
|
queryFn: () =>
|
|
|
loadRootSessionsWithFallback({
|
|
|
directory,
|
|
|
@@ -368,7 +378,7 @@ function createGlobalSync() {
|
|
|
setSessionTodo,
|
|
|
vcsCache: children.vcsCache.get(key),
|
|
|
loadLsp: () => {
|
|
|
- void queryClient.fetchQuery(loadLspQuery(key, sdkFor(directory)))
|
|
|
+ void queryClient.fetchQuery(queryOptionsApi.lsp(key))
|
|
|
},
|
|
|
})
|
|
|
})
|
|
|
@@ -426,6 +436,7 @@ function createGlobalSync() {
|
|
|
},
|
|
|
child: children.child,
|
|
|
peek: children.peek,
|
|
|
+ queryOptions: queryOptionsApi,
|
|
|
// bootstrap,
|
|
|
updateConfig: updateConfigMutation.mutateAsync,
|
|
|
project: projectApi,
|
|
|
@@ -447,3 +458,7 @@ export function useGlobalSync() {
|
|
|
if (!context) throw new Error("useGlobalSync must be used within GlobalSyncProvider")
|
|
|
return context
|
|
|
}
|
|
|
+
|
|
|
+export function useQueryOptions() {
|
|
|
+ return useGlobalSync().queryOptions
|
|
|
+}
|