The open source coding agent.
TypeScript
fix(desktop): don't hang on to dead server
Adam55c601d13a3d
diff --git a/packages/app/src/app.tsx b/packages/app/src/app.tsxindex dd2491961f0..ff9b8b4db11 100644--- a/packages/app/src/app.tsx+++ b/packages/app/src/app.tsx@@ -31,19 +31,16 @@ declare global {}}-const serverDefaults = iife(() => {+const defaultServerUrl = iife(() => {const param = new URLSearchParams(document.location.search).get("url")- if (param) return { url: param, forced: true }+ if (param) return param- if (location.hostname.includes("opencode.ai")) return { url: "http://localhost:4096", forced: false }- if (window.__OPENCODE__) return { url: `http://127.0.0.1:${window.__OPENCODE__.port}`, forced: false }+ if (location.hostname.includes("opencode.ai")) return "http://localhost:4096"+ if (window.__OPENCODE__) return `http://127.0.0.1:${window.__OPENCODE__.port}`if (import.meta.env.DEV)- return {- url: `http://${import.meta.env.VITE_OPENCODE_SERVER_HOST ?? "localhost"}:${import.meta.env.VITE_OPENCODE_SERVER_PORT ?? "4096"}`,- forced: false,- }+ return `http://${import.meta.env.VITE_OPENCODE_SERVER_HOST ?? "localhost"}:${import.meta.env.VITE_OPENCODE_SERVER_PORT ?? "4096"}`- return { url: window.location.origin, forced: false }+ return window.location.origin})function ServerKey(props: ParentProps) {@@ -65,7 +62,7 @@ export function App() {<MarkedProvider><DiffComponentProvider component={Diff}><CodeComponentProvider component={Code}>- <ServerProvider defaultUrl={serverDefaults.url} forceUrl={serverDefaults.forced}>+ <ServerProvider defaultUrl={defaultServerUrl}><ServerKey><GlobalSDKProvider><GlobalSyncProvider>diff --git a/packages/app/src/context/server.tsx b/packages/app/src/context/server.tsxindex 07700ba09dc..991fa7aaf9a 100644--- a/packages/app/src/context/server.tsx+++ b/packages/app/src/context/server.tsx@@ -25,28 +25,27 @@ export function serverDisplayName(url: string) {export const { use: useServer, provider: ServerProvider } = createSimpleContext({name: "Server",- init: (props: { defaultUrl: string; forceUrl?: boolean }) => {+ init: (props: { defaultUrl: string }) => {const platform = usePlatform()- const fallback = () => normalizeServerUrl(props.defaultUrl)- const [forced, setForced] = createSignal(props.forceUrl ?? false)const [store, setStore, _, ready] = persisted(- "server.v2",+ "server.v3",createStore({list: [] as string[],- active: "",projects: {} as Record<string, StoredProject[]>,}),)+ const [active, setActiveRaw] = createSignal("")+function setActive(input: string) {const url = normalizeServerUrl(input)if (!url) returnbatch(() => {if (!store.list.includes(url)) {- setStore("list", (list) => [url, ...list])+ setStore("list", store.list.length, url)}- setStore("active", url)+ setActiveRaw(url)})}@@ -55,49 +54,31 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(if (!url) returnconst list = store.list.filter((x) => x !== url)- const next = store.active === url ? (list[0] ?? fallback() ?? "") : store.active+ const next = active() === url ? (list[0] ?? normalizeServerUrl(props.defaultUrl) ?? "") : active()batch(() => {setStore("list", list)- setStore("active", next)+ setActiveRaw(next)})}createEffect(() => {if (!ready()) return-- const url = fallback()+ const url = normalizeServerUrl(props.defaultUrl)if (!url) return- if (forced()) {- batch(() => {- if (!store.list.includes(url)) {- setStore("list", (list) => [url, ...list])- }- if (store.active !== url) {- setStore("active", url)- }- })- setForced(false)- return- }-- if (store.list.length === 0) {- batch(() => {- setStore("list", [url])- setStore("active", url)- })- return- }-- if (store.active && store.list.includes(store.active)) return- setStore("active", store.list[0])+ batch(() => {+ if (!store.list.includes(url)) {+ setStore("list", store.list.length, url)+ }+ setActiveRaw(url)+ })})- const isReady = createMemo(() => ready() && !!store.active)+ const isReady = createMemo(() => ready() && !!active())const [healthy, { refetch }] = createResource(- () => store.active || undefined,+ () => active() || undefined,async (url) => {if (!url) return@@ -114,21 +95,21 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext()createEffect(() => {- if (!store.active) return+ if (!active()) returnconst interval = setInterval(() => refetch(), 10_000)onCleanup(() => clearInterval(interval))})- const projectsList = createMemo(() => store.projects[store.active] ?? [])+ const projectsList = createMemo(() => store.projects[active()] ?? [])return {ready: isReady,healthy,get url() {- return store.active+ return active()},get name() {- return serverDisplayName(store.active)+ return serverDisplayName(active())},get list() {return store.list@@ -139,14 +120,14 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(projects: {list: projectsList,open(directory: string) {- const url = store.active+ const url = active()if (!url) returnconst current = store.projects[url] ?? []if (current.find((x) => x.worktree === directory)) returnsetStore("projects", url, [{ worktree: directory, expanded: true }, ...current])},close(directory: string) {- const url = store.active+ const url = active()if (!url) returnconst current = store.projects[url] ?? []setStore(@@ -156,21 +137,21 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext()},expand(directory: string) {- const url = store.active+ const url = active()if (!url) returnconst current = store.projects[url] ?? []const index = current.findIndex((x) => x.worktree === directory)if (index !== -1) setStore("projects", url, index, "expanded", true)},collapse(directory: string) {- const url = store.active+ const url = active()if (!url) returnconst current = store.projects[url] ?? []const index = current.findIndex((x) => x.worktree === directory)if (index !== -1) setStore("projects", url, index, "expanded", false)},move(directory: string, toIndex: number) {- const url = store.active+ const url = active()if (!url) returnconst current = store.projects[url] ?? []const fromIndex = current.findIndex((x) => x.worktree === directory)