Skip to content

Commit 73b32a0

Browse files
fix(app): route OpenCode via server proxy (anomalyco#520)
Remote web UI should never target 127.0.0.1:4096 for OpenCode APIs. Prefer the OpenWork server /opencode proxy, gate SSE subscriptions behind health, and surface disconnected status.
1 parent 51b1da1 commit 73b32a0

4 files changed

Lines changed: 84 additions & 11 deletions

File tree

‎packages/app/src/app/components/status-bar.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export default function StatusBar(props: StatusBarProps) {
196196
>
197197
<span class={`w-2 h-2 rounded-full ${opencodeStatusMeta().dot}`} />
198198
<Cpu class="w-4 h-4 text-gray-11" />
199-
<Show when={props.developerMode}>
199+
<Show when={props.developerMode || !props.clientConnected}>
200200
<span class="text-gray-11 font-medium">OpenCode</span>
201201
<span class={opencodeStatusMeta().text}>{opencodeStatusMeta().label}</span>
202202
</Show>
@@ -208,7 +208,7 @@ export default function StatusBar(props: StatusBarProps) {
208208
>
209209
<span class={`w-2 h-2 rounded-full ${openworkStatusMeta().dot}`} />
210210
<Server class="w-4 h-4 text-gray-11" />
211-
<Show when={props.developerMode}>
211+
<Show when={props.developerMode || props.openworkServerStatus !== "connected"}>
212212
<span class="text-gray-11 font-medium">OpenWork</span>
213213
<span class={openworkStatusMeta().text}>{openworkStatusMeta().label}</span>
214214
</Show>

‎packages/app/src/app/context/global-sdk.tsx‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,42 @@ export function GlobalSDKProvider(props: ParentProps) {
3636

3737
createEffect(() => {
3838
const baseUrl = server.url;
39+
const isHealthy = server.healthy() === true;
40+
41+
const token = (() => {
42+
if (typeof window === "undefined") return "";
43+
try {
44+
return (window.localStorage.getItem("openwork.server.token") ?? "").trim();
45+
} catch {
46+
return "";
47+
}
48+
})();
49+
const headers = token && baseUrl.includes("/opencode") ? { Authorization: `Bearer ${token}` } : undefined;
3950
setUrl(baseUrl);
4051

41-
const abort = new AbortController();
42-
const eventClient = createOpencodeClient({
43-
baseUrl,
44-
signal: abort.signal,
45-
fetch: platform.fetch,
46-
});
47-
52+
// Always keep the request client in sync with the active URL.
4853
setClient(
4954
createOpencodeClient({
5055
baseUrl,
56+
headers,
5157
fetch: platform.fetch,
5258
throwOnError: true,
5359
}),
5460
);
5561

62+
// Avoid silent retry loops (SSE reconnects) when the dependency is unavailable.
63+
if (!baseUrl || !isHealthy) {
64+
return;
65+
}
66+
67+
const abort = new AbortController();
68+
const eventClient = createOpencodeClient({
69+
baseUrl,
70+
headers,
71+
signal: abort.signal,
72+
fetch: platform.fetch,
73+
});
74+
5675
type Queued = { directory: string; payload: Event };
5776

5877
let queue: Array<Queued | undefined> = [];

‎packages/app/src/app/context/server.tsx‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,24 @@ export function ServerProvider(props: ParentProps & { defaultUrl: string }) {
5757
if (typeof window === "undefined") return;
5858
if (ready()) return;
5959

60-
const storedList = readStoredList();
6160
const fallback = normalizeServerUrl(props.defaultUrl) ?? "";
61+
62+
// In production web builds served by OpenWork (Docker "remote" mode), OpenCode
63+
// traffic should go through the server proxy (usually same-origin `/opencode`).
64+
// Do not reuse any persisted localhost targets.
65+
const forceProxy =
66+
!isTauriRuntime() &&
67+
(import.meta.env.PROD ||
68+
(typeof import.meta.env?.VITE_OPENWORK_URL === "string" &&
69+
import.meta.env.VITE_OPENWORK_URL.trim().length > 0));
70+
if (forceProxy && fallback) {
71+
setList([fallback]);
72+
setActiveRaw(fallback);
73+
setReady(true);
74+
return;
75+
}
76+
77+
const storedList = readStoredList();
6278
const storedActive = normalizeServerUrl(readStoredActive());
6379

6480
const initialList = storedList.length ? storedList : fallback ? [fallback] : [];
@@ -83,10 +99,21 @@ export function ServerProvider(props: ParentProps & { defaultUrl: string }) {
8399

84100
const activeUrl = createMemo(() => active());
85101

102+
const readOpenworkToken = () => {
103+
try {
104+
return (window.localStorage.getItem("openwork.server.token") ?? "").trim();
105+
} catch {
106+
return "";
107+
}
108+
};
109+
86110
const checkHealth = async (url: string) => {
87111
if (!url) return false;
112+
const token = readOpenworkToken();
113+
const headers = token && url.includes("/opencode") ? { Authorization: `Bearer ${token}` } : undefined;
88114
const client = createOpencodeClient({
89115
baseUrl: url,
116+
headers,
90117
signal: AbortSignal.timeout(3000),
91118
fetch: isTauriRuntime() ? tauriFetch : undefined,
92119
});

‎packages/app/src/app/entry.tsx‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,36 @@ import { GlobalSDKProvider } from "./context/global-sdk";
33
import { GlobalSyncProvider } from "./context/global-sync";
44
import { LocalProvider } from "./context/local";
55
import { ServerProvider } from "./context/server";
6+
import { isTauriRuntime } from "./utils";
67

78
export default function AppEntry() {
8-
const defaultUrl = "http://127.0.0.1:4096";
9+
const defaultUrl = (() => {
10+
// Desktop app connects to the local OpenCode engine.
11+
if (isTauriRuntime()) return "http://127.0.0.1:4096";
12+
13+
// When running the web UI against an OpenWork server (e.g. Docker dev stack),
14+
// use the server's `/opencode` proxy instead of loopback.
15+
const openworkUrl =
16+
typeof import.meta.env?.VITE_OPENWORK_URL === "string"
17+
? import.meta.env.VITE_OPENWORK_URL.trim()
18+
: "";
19+
if (openworkUrl) {
20+
return `${openworkUrl.replace(/\/+$/, "")}/opencode`;
21+
}
22+
23+
// When the UI is served by the OpenWork server (Docker "remote" mode),
24+
// OpenCode is proxied at same-origin `/opencode`.
25+
if (import.meta.env.PROD && typeof window !== "undefined") {
26+
return `${window.location.origin}/opencode`;
27+
}
28+
29+
// Dev fallback (Vite) - allow overriding for remote debugging.
30+
const envUrl =
31+
typeof import.meta.env?.VITE_OPENCODE_URL === "string"
32+
? import.meta.env.VITE_OPENCODE_URL.trim()
33+
: "";
34+
return envUrl || "http://127.0.0.1:4096";
35+
})();
936

1037
return (
1138
<ServerProvider defaultUrl={defaultUrl}>

0 commit comments

Comments
 (0)