Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(switchdash): set GATEWAY_PUBLIC_URL so Open in SwitchDash links work (CHOO-1784)

Louis Amaudruzc30062ce4d02
diff --git a/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.test.ts b/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.test.ts
index e0e852d77..ebf21845c 100644
--- a/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.test.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.test.ts
@@ -1,3 +1,6 @@
+import { readFileSync } from 'node:fs';
+import { dirname, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import { buildEnvFile } from './env-file';
import type { LocalServerSecrets } from './secret-values';
@@ -13,6 +16,13 @@ const secrets: LocalServerSecrets = {
mattermostUserPassword: 'mm-user',
};
+/** The compose file switchdash writes beside the `.env`, and whose `${VAR}`
+ * interpolations the `.env` has to satisfy. */
+const composeYaml = readFileSync(
+ join(dirname(fileURLToPath(import.meta.url)), 'resources/standalone-docker-compose.pinned.yml'),
+ 'utf8'
+);
+
describe('buildEnvFile', () => {
const env = buildEnvFile({
version: '1.2.3',
@@ -59,37 +69,32 @@ describe('buildEnvFile', () => {
expect(vars.MATTERMOST_USER_PASSWORD).toBe('mm-user');
});
- it('defines every var the compose contract interpolates', () => {
- const required = [
- 'SWITCH_BIND_ADDR',
- 'GATEWAY_HOST_PORT',
- 'API_HOST_PORT',
- 'MATTERMOST_HOST_PORT',
- 'POSTGRES_HOST_PORT',
- 'DB_HOST',
- 'DB_PORT',
- 'DB_USER',
- 'DB_PASSWORD',
- 'DB_NAME',
- 'MATRIX_SERVER',
- 'MATRIX_SERVER_NAME',
- 'MATRIX_ADMIN_USER',
- 'MATRIX_ADMIN_PASSWORD',
- 'MATRIX_REGISTRATION_SHARED_SECRET',
- 'AGENT_REGISTRATION_TOKEN',
- 'JWT_SECRET_KEY',
- 'GATEWAY_ADMIN_EMAIL',
- 'GATEWAY_ADMIN_PASSWORD',
- 'FRONTEND_BASE_URL',
- 'MATTERMOST_ADMIN_USER',
- 'MATTERMOST_ADMIN_PASSWORD',
- 'MATTERMOST_TEAM_NAME',
- 'MATTERMOST_USER',
- 'MATTERMOST_USER_PASSWORD',
- ];
- for (const key of required) {
- expect(vars[key], `missing ${key}`).toBeDefined();
- expect(vars[key], `empty ${key}`).not.toBe('');
- }
+ it('defines every var the bundled compose file interpolates', () => {
+ // Derived from the compose file rather than a hand-kept list. The previous
+ // list had drifted both ways — it omitted GATEWAY_PUBLIC_URL (so the
+ // "Open in SwitchDash" redirect was silently disabled on every managed
+ // stack) while asserting vars compose never interpolates. A list cannot
+ // notice a variable being added to the contract; this can.
+ const interpolated = new Set(
+ [...composeYaml.matchAll(/\$\{([A-Z_][A-Z0-9_]*)/g)].map((m) => m[1])
+ );
+ // Nothing is exempt today. An entry here must say why the stack is correct
+ // without it — leaving a var unset is a decision, not a default.
+ const intentionallyUnset = new Set<string>();
+
+ const missing = [...interpolated]
+ .filter((key) => !intentionallyUnset.has(key))
+ .filter((key) => !vars[key])
+ .sort();
+
+ expect(missing, 'compose interpolates these but the .env does not set them').toEqual([]);
+ });
+
+ it('points the deeplink redirect at the API, not the operator UI', () => {
+ // switch-core only rewrites `switchdash://` links into clickable http ones
+ // when this is set, and serves the redirect on the agent-bridge app — so
+ // the gateway's own URL here would produce links that 404.
+ expect(vars.GATEWAY_PUBLIC_URL).toBe('http://localhost:51001');
+ expect(vars.FRONTEND_BASE_URL).toBe('http://localhost:51000');
});
});
diff --git a/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.ts b/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.ts
index 90acbe83d..c6d971cf2 100644
--- a/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/managed-switch-server/env-file.ts
@@ -3,7 +3,7 @@ import {
LOCAL_SERVER_BIND_ADDR,
LOCAL_SERVER_MATTERMOST_TEAM,
} from './constants';
-import type { LocalServerPorts } from './free-port';
+import { apiUrlFor, gatewayUrlFor, type LocalServerPorts } from './free-port';
import type { LocalServerSecrets } from './secret-values';
export type LocalServerEnvParams = {
@@ -54,7 +54,15 @@ export function buildEnvFile(params: LocalServerEnvParams): string {
`JWT_SECRET_KEY=${secrets.jwtSecretKey}`,
`GATEWAY_ADMIN_EMAIL=${LOCAL_SERVER_ADMIN_EMAIL}`,
`GATEWAY_ADMIN_PASSWORD=${secrets.gatewayAdminPassword}`,
- `FRONTEND_BASE_URL=http://localhost:${ports.gateway}`,
+ `FRONTEND_BASE_URL=${gatewayUrlFor(ports)}`,
+ // Turns the "Open in SwitchDash" link into an http(s) one. Discord (and any
+ // other bridge that only linkifies http) renders a raw `switchdash://`
+ // deeplink as dead text, so switch-core rewrites it to point at its own
+ // `/deeplink/session`, which 302s back to the deeplink — but only when this
+ // is set. It is the API origin, not the operator UI's: the redirect route is
+ // served on the agent-bridge app, and this must match the `server` switchdash
+ // reports in the deeplink it is rewriting.
+ `GATEWAY_PUBLIC_URL=${apiUrlFor(ports)}`,
'',
'MATTERMOST_ADMIN_USER=admin',
`MATTERMOST_ADMIN_PASSWORD=${secrets.mattermostAdminPassword}`,