Skip to content

Commit ff4446a

Browse files
authored
feat(server): expose opencode server proxy url env (#351)
Fixes #321 ## Summary - expose OPENCODE_SERVER_BASE_URL in the spawned OpenCode workspace environment - point it at the current workspace's stable CodeNomad proxy URL - propagate the new variable through WSL launches alongside the existing auth env vars ## Why Tools and plugins already receive the auth credentials needed to call the current workspace instance, but they still need an extra lookup to discover the correct URL. The proxy URL is already known before runtime startup, so exposing it directly removes that lookup without changing OpenCode's port selection flow. ## What Changed - packages/server/src/workspaces/opencode-auth.ts: added the OPENCODE_SERVER_BASE_URL env var constant - packages/server/src/workspaces/manager.ts: injects OPENCODE_SERVER_BASE_URL into the workspace runtime environment using CODENOMAD_BASE_URL + proxyPath - packages/server/src/workspaces/__tests__/spawn.test.ts: updates WSL env propagation coverage for the new variable ## Validation - npx tsx --test "packages/server/src/workspaces/__tests__/spawn.test.ts"
1 parent 0ba1371 commit ff4446a

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

packages/server/src/workspaces/__tests__/spawn.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ describe("buildWindowsSpawnSpec", () => {
5656
env: {
5757
OPENCODE_CONFIG_DIR: String.raw`C:\Users\dev\AppData\Roaming\CodeNomad\opencode-config`,
5858
CODENOMAD_INSTANCE_ID: "workspace-123",
59+
OPENCODE_SERVER_BASE_URL: "https://127.0.0.1:4321/workspaces/workspace-123/worktrees/root/instance",
5960
OPENCODE_SERVER_PASSWORD: "secret",
6061
},
61-
propagateEnvKeys: ["OPENCODE_CONFIG_DIR", "CODENOMAD_INSTANCE_ID", "OPENCODE_SERVER_PASSWORD"],
62+
propagateEnvKeys: ["OPENCODE_CONFIG_DIR", "CODENOMAD_INSTANCE_ID", "OPENCODE_SERVER_BASE_URL", "OPENCODE_SERVER_PASSWORD"],
6263
},
6364
)
6465

@@ -75,7 +76,7 @@ describe("buildWindowsSpawnSpec", () => {
7576
"0",
7677
])
7778
assert.equal(spec.cwd, undefined)
78-
assert.equal(spec.env?.WSLENV, "OPENCODE_CONFIG_DIR/p:CODENOMAD_INSTANCE_ID:OPENCODE_SERVER_PASSWORD")
79+
assert.equal(spec.env?.WSLENV, "OPENCODE_CONFIG_DIR/p:CODENOMAD_INSTANCE_ID:OPENCODE_SERVER_BASE_URL:OPENCODE_SERVER_PASSWORD")
7980
})
8081

8182
it("upgrades existing WSLENV path entries to include /p", () => {

packages/server/src/workspaces/manager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { WorkspaceRuntime, ProcessExitInfo } from "./runtime"
1212
import { Logger } from "../logger"
1313
import { getOpencodeConfigDir } from "../opencode-config.js"
1414
import {
15+
OPENCODE_SERVER_BASE_URL_ENV,
1516
buildOpencodeBasicAuthHeader,
1617
OPENCODE_SERVER_PASSWORD_ENV,
1718
OPENCODE_SERVER_USERNAME_ENV,
@@ -122,6 +123,8 @@ export class WorkspaceManager {
122123
const serverConfig = this.options.settings.getOwner("config", "server")
123124
const envVars = (serverConfig as any)?.environmentVariables
124125
const userEnvironment = envVars && typeof envVars === "object" && !Array.isArray(envVars) ? (envVars as any) : {}
126+
const serverBaseUrl = this.options.getServerBaseUrl()
127+
const normalizedServerBaseUrl = serverBaseUrl.replace(/\/+$/, "")
125128

126129
const { username: opencodeUsername, password: opencodePassword } = resolveOpencodeServerAuth({
127130
userEnvironment,
@@ -137,8 +140,9 @@ export class WorkspaceManager {
137140
...userEnvironment,
138141
OPENCODE_CONFIG_DIR: this.opencodeConfigDir,
139142
CODENOMAD_INSTANCE_ID: id,
140-
CODENOMAD_BASE_URL: this.options.getServerBaseUrl(),
143+
CODENOMAD_BASE_URL: serverBaseUrl,
141144
...(this.options.nodeExtraCaCertsPath ? { NODE_EXTRA_CA_CERTS: this.options.nodeExtraCaCertsPath } : {}),
145+
[OPENCODE_SERVER_BASE_URL_ENV]: `${normalizedServerBaseUrl}${proxyPath}`,
142146
[OPENCODE_SERVER_USERNAME_ENV]: opencodeUsername,
143147
[OPENCODE_SERVER_PASSWORD_ENV]: opencodePassword,
144148
}

packages/server/src/workspaces/opencode-auth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import crypto from "node:crypto"
22

33
export const OPENCODE_SERVER_USERNAME_ENV = "OPENCODE_SERVER_USERNAME" as const
44
export const OPENCODE_SERVER_PASSWORD_ENV = "OPENCODE_SERVER_PASSWORD" as const
5+
export const OPENCODE_SERVER_BASE_URL_ENV = "OPENCODE_SERVER_BASE_URL" as const
56

67
export const DEFAULT_OPENCODE_USERNAME = "codenomad" as const
78

0 commit comments

Comments
 (0)