1- import type { Config , Path , Project , ProviderAuthResponse } from "@/runtime/server/types"
1+ import type { Config , Path } from "@/runtime/server/types"
22import type { LocationGetInput , LocationGetOutput , ProjectListOutput } from "@opencode/client/promise"
3- import { showToast } from "@/shell/notifications/toast"
4- import { getFilename } from "@opencode/util/path"
53import { retry } from "@opencode/util/retry"
6- import { reconcile , type SetStoreFunction , type Store } from "solid-js/store"
7- import type { State } from "./types"
4+ import type { SetStoreFunction } from "solid-js/store"
5+ import type { GlobalStore } from "./types"
86import { normalizeProjectInfo } from "./utils"
9- import { formatServerError } from "@/runtime/server/errors"
107import { QueryClient , queryOptions } from "@tanstack/solid-query"
118import type { ServerScope } from "@/runtime/server/scope"
129import { withWorktreeInventory , worktreeInventoryKey } from "@/workspaces/inventory"
1310
14- type GlobalStore = {
15- path : Path
16- project : Project [ ]
17- provider_auth : ProviderAuthResponse
18- config : Config
19- reload : undefined | "pending" | "complete"
20- }
21-
22- function waitForPaint ( ) {
23- return new Promise < void > ( ( resolve ) => {
24- let done = false
25-
26- const finish = ( ) => {
27- if ( done ) return
28- done = true
29- resolve ( )
30- }
31-
32- const timer = setTimeout ( finish , 50 )
33-
34- if ( typeof requestAnimationFrame !== "function" ) return
35- requestAnimationFrame ( ( ) => {
36- setTimeout ( ( ) => {
37- clearTimeout ( timer )
38- finish ( )
39- } , 0 )
40- } )
41- } )
42- }
43-
44- function errors ( list : PromiseSettledResult < unknown > [ ] ) {
45- return list . filter ( ( item ) : item is PromiseRejectedResult => item . status === "rejected" ) . map ( ( item ) => item . reason )
46- }
47-
48- function runAll ( list : Array < ( ) => Promise < unknown > > ) {
49- return Promise . allSettled ( list . map ( ( item ) => item ( ) ) )
50- }
51-
5211export const loadGlobalConfigQuery = ( scope : ServerScope ) =>
5312 queryOptions ( {
5413 queryKey : [ scope , "config" ] ,
@@ -100,26 +59,18 @@ export async function bootstrapGlobal(input: {
10059 setGlobalStore : SetStoreFunction < GlobalStore >
10160 queryClient : QueryClient
10261} ) {
103- const slow = [
104- ( ) => input . queryClient . fetchQuery ( loadGlobalConfigQuery ( input . scope ) ) ,
105- ( ) =>
106- input . queryClient
107- . fetchQuery ( loadPathQuery ( input . scope , null , input . serverAPI . location ) )
108- . then ( ( data ) => input . setGlobalStore ( "path" , data ) ) ,
109- ( ) =>
110- loadProjects ( {
111- scope : input . scope ,
112- project : input . serverAPI . project ,
113- setGlobalStore : input . setGlobalStore ,
114- queryClient : input . queryClient ,
115- } ) ,
116- ]
117-
118- await runAll ( slow )
119- }
120-
121- function projectID ( directory : string , projects : Project [ ] ) {
122- return projects . find ( ( project ) => project . worktree === directory || project . sandboxes ?. includes ( directory ) ) ?. id
62+ await Promise . allSettled ( [
63+ input . queryClient . fetchQuery ( loadGlobalConfigQuery ( input . scope ) ) ,
64+ input . queryClient
65+ . fetchQuery ( loadPathQuery ( input . scope , null , input . serverAPI . location ) )
66+ . then ( ( data ) => input . setGlobalStore ( "path" , data ) ) ,
67+ loadProjects ( {
68+ scope : input . scope ,
69+ project : input . serverAPI . project ,
70+ setGlobalStore : input . setGlobalStore ,
71+ queryClient : input . queryClient ,
72+ } ) ,
73+ ] )
12374}
12475
12576export function inferHomeDirectory ( path : string ) {
@@ -143,57 +94,3 @@ export const loadPathQuery = (scope: ServerScope, directory: string | null, api:
14394 home : inferHomeDirectory ( location . directory ) || inferHomeDirectory ( location . project . directory ) ,
14495 } ) ) ,
14596 } )
146-
147- export async function bootstrapDirectory ( input : {
148- directory : string
149- scope : ServerScope
150- mcp : boolean
151- api : {
152- readonly project : ProjectApi
153- readonly location : LocationApi
154- }
155- store : Store < State >
156- setStore : SetStoreFunction < State >
157- translate : ( key : string , vars ?: Record < string , string | number > ) => string
158- global : {
159- config : Config
160- path : Path
161- project : Project [ ]
162- }
163- queryClient : QueryClient
164- } ) {
165- const loading = input . store . status !== "complete"
166- const seededProject = projectID ( input . directory , input . global . project )
167-
168- if ( seededProject ) input . setStore ( "project" , seededProject )
169-
170- if ( Object . keys ( input . store . config ) . length === 0 && Object . keys ( input . global . config ) . length > 0 ) {
171- input . setStore ( "config" , reconcile ( input . global . config , { merge : false } ) )
172- }
173-
174- if ( loading ) input . setStore ( "status" , "partial" )
175-
176- const slow = [
177- seededProject
178- ? undefined
179- : ( ) =>
180- retry ( ( ) => input . api . location . get ( { location : { directory : input . directory } } ) ) . then ( ( location ) =>
181- input . setStore ( "project" , location . project . id ) ,
182- ) ,
183- ] . filter ( ( task ) : task is ( ) => Promise < void > => ! ! task )
184-
185- await waitForPaint ( )
186- const slowErrs = errors ( await runAll ( slow ) )
187-
188- if ( slowErrs . length > 0 ) {
189- console . error ( "Failed to finish bootstrap instance" , slowErrs [ 0 ] )
190- const project = getFilename ( input . directory )
191- showToast ( {
192- variant : "error" ,
193- title : input . translate ( "toast.project.reloadFailed.title" , { project } ) ,
194- description : formatServerError ( slowErrs [ 0 ] , input . translate ) ,
195- } )
196- }
197-
198- if ( loading && slowErrs . length === 0 ) input . setStore ( "status" , "complete" )
199- }
0 commit comments