@devries-cameron20 / t3code-1 / commits / 5adbd28d46
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(opencode): external OpenCode 2 servers list models without the host's cwd (#30932)

Julius Marminge committed Nov 16, 2026 at 17:48 UTC 5adbd28d46532aa893e11514ea73292ca9c8a0dc
3 files changed +111 -10
packages/provider-opencode/src/server/driver.test.ts
+99 -4
@@ -148,10 +148,14 @@ const makeOpenCode2ProjectInstance = (
148 return Effect.succeed(
149 HttpClientResponse.fromWeb(
150 request,
151 - new Response(JSON.stringify({ location: { directory }, data }), {
152 - status: unavailable ? 503 : 200,
153 - headers: { "content-type": "application/json" },
154 - }),
151 + // An external server without a requested location answers for its own default.
152 + new Response(
153 + JSON.stringify({ location: { directory: directory ?? "/srv/remote" }, data }),
154 + {
155 + status: unavailable ? 503 : 200,
156 + headers: { "content-type": "application/json" },
157 + },
158 + ),
159 ),
160 );
161 });
@@ -736,6 +740,97 @@ it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
740 }).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
741 );
742
743 + it.effect("lists models from an external server that lacks the host's directory", () =>
744 + Effect.gen(function* () {
745 + const replay = replayOpenCodeServer(OPENCODE_2_RESPONSES, "secret");
746 + const catalog = (directory: string | undefined) => ({
747 + location: { directory: directory ?? "/srv/remote" },
748 + data: [
749 + {
750 + id: "big-pickle",
751 + modelID: "big-pickle",
752 + providerID: "opencode",
753 + name: "Big Pickle",
754 + package: "@opencode/ai/providers/openai-compatible",
755 + settings: {},
756 + compatibility: {},
757 + capabilities: { tools: true, input: ["text"], output: ["text"] },
758 + variants: [],
759 + time: { released: 1789516800000 },
760 + cost: [{ input: 0, output: 0, cache: { read: 0, write: 0 } }],
761 + status: "active",
762 + enabled: true,
763 + limit: { context: 1000000, output: 65536 },
764 + },
765 + ],
766 + });
767 + // Like a real remote OpenCode 2 server: a directory it does not have is a 404.
768 + const server = HttpClient.make((request) => {
769 + const url = new URL(request.url);
770 + if (url.pathname !== "/api/model" && url.pathname !== "/api/provider") {
771 + return replay.execute(request);
772 + }
773 + const directory = Option.getOrUndefined(
774 + UrlParams.getFirst(request.urlParams, "location[directory]"),
775 + );
776 + const known = directory === undefined || directory === "/srv/remote";
777 + const body = known
778 + ? url.pathname === "/api/model"
779 + ? catalog(directory)
780 + : {
781 + location: { directory: directory ?? "/srv/remote" },
782 + data: [
783 + {
784 + id: "opencode",
785 + integrationID: "opencode",
786 + name: "OpenCode Zen",
787 + activation: "enabled",
788 + package: "@opencode/ai/providers/openai-compatible",
789 + settings: {},
790 + },
791 + ],
792 + }
793 + : {
794 + _tag: "LocationNotFoundError",
795 + message: `Location not found: ${directory}`,
796 + };
797 + return Effect.succeed(
798 + HttpClientResponse.fromWeb(
799 + request,
800 + new Response(JSON.stringify(body), {
801 + status: known ? 200 : 404,
802 + headers: { "content-type": "application/json" },
803 + }),
804 + ),
805 + );
806 + });
807 + const instance = yield* create(
808 + { serverUrl: "http://127.0.0.1:4096", serverPassword: "secret" },
809 + server,
810 + );
811 + const settled = yield* instance.snapshot.streamChanges.pipe(
812 + Stream.filter(
813 + (snapshot) =>
814 + snapshot.installed && (snapshot.status === "ready" || snapshot.status === "error"),
815 + ),
816 + Stream.take(1),
817 + Stream.runCollect,
818 + Effect.forkChild,
819 + );
820 + yield* Effect.yieldNow;
821 + yield* TestClock.adjust("5 seconds");
822 + const [snapshot] = Array.from(yield* Fiber.join(settled));
823 + assert.deepStrictEqual(
824 + { status: snapshot?.status, message: snapshot?.message },
825 + { status: "ready", message: "OpenCode 2.0.18 lists 1 model." },
826 + );
827 + assert.deepStrictEqual(
828 + snapshot?.models.map((model) => [model.slug, model.subProvider]),
829 + [["opencode/big-pickle", "OpenCode Zen"]],
830 + );
831 + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
832 + );
833 +
834 it.effect("lists a project's own opencode.jsonc models in that project's snapshot", () =>
835 Effect.gen(function* () {
836 const snapshotFor = yield* makeOpenCode2ProjectInstance("listed");
packages/provider-opencode/src/server/driver.ts
+4 -1
@@ -398,7 +398,10 @@ export const OpenCodeDriver: ProviderDriver<
398 ),
399 });
400 const loadOpenCode2Models = yield* makeOpenCode2ModelLoader(
401 - openCode2Server.withConnection(({ client }) => loadOpenCode2Catalog(client, host.paths.cwd)),
401 + // An external server may not have the host's cwd; without a location it uses its own default.
402 + openCode2Server.withConnection(({ client, external }) =>
403 + loadOpenCode2Catalog(client, external ? undefined : host.paths.cwd),
404 + ),
405 processEnv,
406 );
407 const listOpenCode2Workspace = (cwd: string) =>
packages/provider-opencode/src/server/openCode2Catalog.ts
+8 -5
@@ -7,21 +7,24 @@ import type { OpenCode2Model } from "./status.ts";
7 // Cold catalogs take seconds; model reads already retry empty catalogs.
8 const PROVIDER_NAME_TIMEOUT = "3 seconds";
9
10 -const listProviders = (client: OpenCodeClient, directory: string) =>
11 - client.provider.list({ location: { directory } }).pipe(
10 +const locationOf = (directory: string | undefined) =>
11 + directory === undefined ? {} : { location: { directory } };
12 +
13 +const listProviders = (client: OpenCodeClient, directory: string | undefined) =>
14 + client.provider.list(locationOf(directory)).pipe(
15 Effect.timeout(PROVIDER_NAME_TIMEOUT),
16 Effect.map((list) => list.data),
17 );
18
19 const readCatalog = <E>(
20 client: OpenCodeClient,
18 - directory: string,
21 + directory: string | undefined,
22 providers: Effect.Effect<ReadonlyArray<{ readonly id: string; readonly name: string }>, E>,
23 ): Effect.Effect<ReadonlyArray<OpenCode2Model>, OpenCodeRuntime.OpenCodeRuntimeError> =>
24 Effect.all(
25 {
26 // Callers bound the read: a busy host can hold it past OpenCode's own activation wait.
24 - models: client.model.list({ location: { directory } }).pipe(Effect.map((list) => list.data)),
27 + models: client.model.list(locationOf(directory)).pipe(Effect.map((list) => list.data)),
28 providers,
29 },
30 { concurrency: "unbounded" },
@@ -53,7 +56,7 @@ const readCatalog = <E>(
56 );
57
58 /** Provider names are optional decoration; bound their read so models cannot stall. */
56 -export const loadOpenCode2Catalog = (client: OpenCodeClient, directory: string) =>
59 +export const loadOpenCode2Catalog = (client: OpenCodeClient, directory: string | undefined) =>
60 readCatalog(
61 client,
62 directory,