-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy-server.ts
More file actions
659 lines (579 loc) · 22.4 KB
/
Copy pathproxy-server.ts
File metadata and controls
659 lines (579 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/**
* Relay Dashboard Server
*
* A flexible server that can operate in three modes:
* 1. Proxy mode (default): static files + Relaycast data + broker proxy
* 2. Standalone mode: static files + Relaycast data (no broker proxy)
* 3. Mock mode: fixture-backed standalone mode for demos/tests
*/
import express, { type Request, type Response, type NextFunction } from 'express';
import { createServer as createHttpServer, type Server } from 'http';
import { WebSocketServer } from 'ws';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { registerMockRoutes } from './mocks/routes.js';
import {
fetchAgents,
fetchAllMessages,
fetchChannels,
type RelaycastConfig,
} from './relaycast-provider.js';
import { createSendStrategy } from './lib/send-strategy.js';
import type { SendStrategy } from './lib/send-strategy.js';
import { DASHBOARD_DISPLAY_NAME } from './relaycast-provider-types.js';
import { clearRegistrationCache } from './relaycast-provider-helpers.js';
import { resolveIdentity } from './lib/identity.js';
import type {
DashboardMode,
DashboardSnapshot,
DashboardChannel,
DashboardServerOptions,
DashboardServer,
RouteContext,
} from './lib/types.js';
import { EMPTY_DASHBOARD_SNAPSHOT } from './lib/types.js';
import {
normalizeRelayUrl,
normalizeName,
isDirectRecipient,
sendHtmlFileOrFallback,
getBindHost,
mapChannelForDashboard,
safeUsername,
} from './lib/utils.js';
import {
filterPhantomAgents,
mergeBrokerSpawnedAgents,
createSpawnedAgentsCaches,
} from './lib/spawned-agents.js';
import { handleMockWebSocket } from './websocket/mock.js';
import { handleStandaloneWebSocket, handleHybridWebSocket } from './websocket/standalone.js';
import { handleStandaloneLogWebSocket } from './websocket/logs.js';
import { registerHealthRoutes } from './routes/health.js';
import { registerDataRoutes } from './routes/data.js';
import { registerAgentRoutes } from './routes/agents.js';
import { registerChannelRoutes } from './routes/channels.js';
import { registerBrokerProxyRoutes } from './routes/broker-proxy.js';
import { registerMetricsRoutes } from './routes/metrics.js';
import { registerReactionRoutes } from './routes/reactions.js';
import { registerThreadReplyRoutes } from './routes/thread-replies.js';
import { registerRelayConfigRoutes } from './routes/relay-config.js';
import { registerRelaycastHistoryRoutes } from './routes/history-relaycast.js';
import { registerModelsRoutes } from './routes/models.js';
export type { DashboardServerOptions, DashboardServer } from './lib/types.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function resolveMetricsPagePath(staticDir: string): string {
const candidates = [
path.join(staticDir, 'metrics.html'),
path.join(staticDir, 'metrics', 'index.html'),
path.join(staticDir, 'app.html'),
path.join(staticDir, 'index.html'),
];
for (const candidate of candidates) {
if (fs.existsSync(candidate)) {
return candidate;
}
}
return candidates[0];
}
const asString = (value: unknown): string | undefined => {
if (typeof value === 'string' && value.length > 0) return value;
if (Array.isArray(value)) {
for (const item of value) {
if (typeof item === 'string' && item.length > 0) return item;
}
}
return undefined;
};
const getWorkspaceHeader = (headers: Record<string, unknown> | undefined): string | undefined => {
if (!headers) return undefined;
const direct = headers['x-workspace-id'];
if (typeof direct === 'string' && direct.length > 0) return direct;
for (const [key, value] of Object.entries(headers)) {
if (key.toLowerCase() === 'x-workspace-id' && typeof value === 'string' && value.length > 0) {
return value;
}
}
return undefined;
};
/**
* Create the dashboard server without starting it
*/
export function createServer(options: DashboardServerOptions = {}): DashboardServer {
const {
relayUrl: relayUrlOption,
staticDir = process.env.STATIC_DIR || path.join(__dirname, '..', 'out'),
dataDir = process.env.DATA_DIR || path.join(process.cwd(), '.agent-relay'),
verbose = process.env.VERBOSE === 'true',
mock = process.env.MOCK === 'true',
corsOrigins = process.env.CORS_ORIGINS || '',
requestTimeout = parseInt(process.env.REQUEST_TIMEOUT || '60000', 10),
relayApiKey: relayApiKeyOption,
} = options;
// In-memory API key — highest priority, avoids any file I/O.
// Seeded from the option or RELAY_API_KEY env var, then updated dynamically
// via setRelayApiKey() when a workflow creates a new Relaycast workspace.
let inMemoryRelayApiKey: string | undefined =
relayApiKeyOption?.trim() || process.env.RELAY_API_KEY?.trim() || undefined;
// Cached agent token/name from the last successful registerOrRotate call.
// Prevents repeated token rotations that invalidate the frontend WS connection.
let inMemoryAgentToken: string | undefined;
let inMemoryAgentName: string | undefined;
const resolvedDataDir = path.resolve(dataDir);
if (!process.env.AGENT_RELAY_PROJECT) {
process.env.AGENT_RELAY_PROJECT = path.dirname(resolvedDataDir);
}
const relayUrl = normalizeRelayUrl(relayUrlOption ?? process.env.RELAY_URL);
const mode: DashboardMode = mock ? 'mock' : (relayUrl ? 'proxy' : 'standalone');
const brokerProxyEnabled = mode === 'proxy' && Boolean(relayUrl);
const defaultWorkspaceId = process.env.RELAY_WORKSPACE_ID ?? process.env.AGENT_RELAY_WORKSPACE_ID;
const resolveWorkspaceId = (req: {
query?: Record<string, unknown>;
body?: Record<string, unknown>;
headers?: Record<string, unknown>;
}): string | undefined => {
const fromQuery = asString(req.query?.workspaceId);
const fromBody = asString(req.body?.workspaceId);
const fromHeader = getWorkspaceHeader(req.headers);
return fromQuery || fromBody || fromHeader || defaultWorkspaceId;
};
const app = express();
const server = createHttpServer(app);
server.timeout = requestTimeout;
app.use(express.json({ limit: '10mb' }));
if (corsOrigins) {
app.use((req: Request, res: Response, next: NextFunction) => {
const origin = req.headers.origin;
if (corsOrigins === '*') {
res.header('Access-Control-Allow-Origin', '*');
} else if (origin) {
const allowedOrigins = corsOrigins.split(',').map((value) => value.trim());
if (allowedOrigins.includes(origin)) {
res.header('Access-Control-Allow-Origin', origin);
}
}
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-CSRF-Token');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Expose-Headers', 'X-CSRF-Token');
if (req.method === 'OPTIONS') {
res.sendStatus(204);
return;
}
next();
});
}
if (verbose) {
app.use((req: Request, _res: Response, next: NextFunction) => {
console.log(`[dashboard] ${req.method} ${req.url}`);
next();
});
}
// --- Build shared context ---
const applyCachedAgentIdentity = (config: RelaycastConfig | null): RelaycastConfig | null => {
if (!config) return null;
if (!inMemoryAgentToken && !inMemoryAgentName) return config;
return {
...config,
agentToken: inMemoryAgentToken ?? config.agentToken,
agentName: inMemoryAgentName ?? config.agentName,
};
};
const resolveRelaycastConfig = (): RelaycastConfig | null => {
if (!inMemoryRelayApiKey) return null;
const baseUrl = process.env.RELAYCAST_API_URL || 'https://api.relaycast.dev';
const projectIdentity = safeUsername(path.basename(path.resolve(dataDir, '..')));
return applyCachedAgentIdentity({
apiKey: inMemoryRelayApiKey,
baseUrl,
projectIdentity,
});
};
const setRelayApiKey = (apiKey: string): void => {
const trimmed = apiKey.trim();
if (trimmed !== inMemoryRelayApiKey) {
// New workspace key — clear cached agent identity so a fresh
// registerOrRotate happens on the next relay-config request.
inMemoryAgentToken = undefined;
inMemoryAgentName = undefined;
}
inMemoryRelayApiKey = trimmed;
};
const setRelayAgentIdentity = (token: string, name: string): void => {
inMemoryAgentToken = token;
inMemoryAgentName = name;
};
const clearCachedAgentToken = (): void => {
inMemoryAgentToken = undefined;
inMemoryAgentName = undefined;
clearRegistrationCache();
};
const brokerApiKey = process.env.RELAY_BROKER_API_KEY?.trim() || undefined;
const { getSpawnedAgents, getLocalAgentNames } = createSpawnedAgentsCaches({
brokerProxyEnabled,
relayUrl,
dataDir,
verbose,
brokerApiKey,
});
const getRelaycastSnapshot = async (): Promise<DashboardSnapshot> => {
const config = resolveRelaycastConfig();
if (!config) {
return { ...EMPTY_DASHBOARD_SNAPSHOT };
}
const [agents, messages, spawnedAgents, localAgentNames] = await Promise.all([
fetchAgents(config),
fetchAllMessages(config),
brokerProxyEnabled ? getSpawnedAgents() : Promise.resolve({ names: null, agents: null }),
brokerProxyEnabled ? Promise.resolve(null) : Promise.resolve(getLocalAgentNames()),
]);
const filteredAgents = filterPhantomAgents(agents, spawnedAgents.names, localAgentNames);
const mergedAgents = mergeBrokerSpawnedAgents(filteredAgents, spawnedAgents.agents);
return {
agents: mergedAgents,
users: [],
messages,
activity: [],
sessions: [],
summaries: [],
};
};
const getRelaycastChannels = async (): Promise<{ channels: DashboardChannel[]; archivedChannels: DashboardChannel[] }> => {
const config = resolveRelaycastConfig();
if (!config) {
return { channels: [], archivedChannels: [] };
}
const channels = await fetchChannels(config);
const activeChannels: DashboardChannel[] = [];
const archivedChannels: DashboardChannel[] = [];
for (const channel of channels) {
const mapped = mapChannelForDashboard({ ...channel, is_archived: channel.is_archived ?? false });
if (mapped.status === 'archived') {
archivedChannels.push(mapped);
} else {
activeChannels.push(mapped);
}
}
activeChannels.sort((a, b) => a.name.localeCompare(b.name));
archivedChannels.sort((a, b) => a.name.localeCompare(b.name));
return { channels: activeChannels, archivedChannels };
};
const sendRelaycastMessage = async (
params: { to: string; message: string; from?: string; thread?: string },
): Promise<{ success: true; messageId: string } | { success: false; status: number; error: string }> => {
const sendTimeout = Math.max(requestTimeout - 5000, 10000);
const sendStart = Date.now();
const timeoutPromise = new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error('Send timed out')), sendTimeout),
);
try {
return await Promise.race([
(async () => {
const config = resolveRelaycastConfig();
const rawTarget = params.to.trim();
const message = params.message.trim();
let resolvedTarget = rawTarget;
if (isDirectRecipient(rawTarget) && config) {
const relayAgents = await fetchAgents(config);
const relayMatch = relayAgents.find((agent) => normalizeName(agent.name) === normalizeName(rawTarget));
if (relayMatch) {
resolvedTarget = relayMatch.name;
}
}
const projectIdentity = config?.agentName?.trim()
|| safeUsername(DASHBOARD_DISPLAY_NAME);
const senderInput = params.from?.trim() ?? '';
const senderName = mode === 'proxy'
? resolveIdentity(senderInput || projectIdentity, {
projectIdentity: projectIdentity.trim(),
relayAgentName: config?.agentName?.trim(),
})
: (senderInput || projectIdentity);
const strategy: SendStrategy | null = createSendStrategy({
brokerProxyEnabled,
brokerUrl: relayUrl,
brokerApiKey,
relaycastConfig: config,
dataDir,
});
if (!strategy) {
return {
success: false as const,
status: 503,
error: 'Relaycast credentials not configured. Set RELAY_API_KEY or POST /api/relay-config.',
};
}
console.log(
`[dashboard] /api/send request: to=${resolvedTarget}, from=${senderName}, relayUrl=${relayUrl}, timeoutMs=${sendTimeout}`,
);
const outcome = await strategy.send({
to: resolvedTarget,
message,
from: senderName,
thread: params.thread,
});
console.log(`[dashboard] /api/send completed in ${Date.now() - sendStart}ms with status=${outcome.success ? 200 : outcome.status}`);
// Enrich "agent not found" errors with available agent names
if (!outcome.success && isDirectRecipient(params.to) && config) {
if (/agent\s+\".+\"\s+not\s+found/i.test(outcome.error)) {
const relayAgents = await fetchAgents(config);
const available = relayAgents.map((agent) => agent.name).sort();
const suffix = available.length > 0
? ` Available relay agents: ${available.join(', ')}.`
: ' No relay agents are currently online.';
return {
success: false as const,
status: 404,
error: `${outcome.error}.${suffix}`,
};
}
}
return outcome;
})(),
timeoutPromise,
]);
} catch (err) {
console.error(`[dashboard] /api/send failed after ${Date.now() - sendStart}ms: ${(err as Error).message}`);
return {
success: false,
status: 504,
error: (err as Error).message || 'Send request timed out',
};
}
};
const ctx: RouteContext = {
mode,
dataDir,
staticDir,
verbose,
relayUrl,
brokerProxyEnabled,
brokerApiKey,
resolveRelaycastConfig,
setRelayApiKey,
setRelayAgentIdentity,
clearCachedAgentToken,
getRelaycastSnapshot,
getRelaycastChannels,
sendRelaycastMessage,
getSpawnedAgents,
getLocalAgentNames,
filterPhantomAgents,
};
// --- Register routes ---
registerHealthRoutes(app, ctx);
if (mock) {
console.log('[dashboard] Running in MOCK mode - no relay broker required');
registerMockRoutes(app, verbose);
} else {
if (mode === 'proxy' && relayUrl) {
console.log(`[dashboard] Running in PROXY mode - relaycast + broker proxy (${relayUrl})`);
} else {
console.log('[dashboard] Running in STANDALONE mode - relaycast only (read-only broker surface)');
}
// Log Relaycast workspace key so users can view messages at relaycast.dev
const startupConfig = resolveRelaycastConfig();
if (startupConfig?.apiKey) {
console.log(`[dashboard] Relaycast workspace key: ${startupConfig.apiKey}`);
console.log('[dashboard] View messages at https://agentrelay.dev/observer');
}
registerAgentRoutes(app, ctx);
registerDataRoutes(app, ctx);
registerRelayConfigRoutes(app, ctx);
registerChannelRoutes(app, ctx);
registerReactionRoutes(app, ctx);
registerThreadReplyRoutes(app, ctx);
registerMetricsRoutes(app, {
teamDir: path.join(dataDir, 'team'),
resolveWorkspaceId,
});
registerRelaycastHistoryRoutes(app, ctx);
registerModelsRoutes(app);
registerBrokerProxyRoutes(app, ctx);
}
// --- Static files and SPA fallback ---
const fallbackHtml = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Relay Dashboard</title>
</head>
<body>
<h1>Relay Dashboard</h1>
<p>Dashboard static build not found.</p>
</body>
</html>`;
app.get('/metrics', (_req: Request, res: Response) => {
const metricsPath = resolveMetricsPagePath(staticDir);
sendHtmlFileOrFallback(res, metricsPath, fallbackHtml, 200);
});
app.get('/app', (_req: Request, res: Response) => {
const appHtmlPath = path.join(staticDir, 'app.html');
sendHtmlFileOrFallback(res, appHtmlPath, fallbackHtml, 200);
});
app.get('/app/{*path}', (_req: Request, res: Response) => {
const appHtmlPath = path.join(staticDir, 'app.html');
sendHtmlFileOrFallback(res, appHtmlPath, fallbackHtml, 200);
});
app.use(express.static(staticDir, {
extensions: ['html'],
}));
app.get('/', (_req: Request, res: Response) => {
const indexPath = path.join(staticDir, 'index.html');
sendHtmlFileOrFallback(res, indexPath, fallbackHtml, 200);
});
app.get('/{*path}', (req: Request, res: Response) => {
// WebSocket endpoints require upgrade - return 426 for regular HTTP requests
if (req.path === '/ws' || req.path.startsWith('/ws/')) {
res.status(426).json({ error: 'Upgrade Required', message: 'WebSocket upgrade required' });
return;
}
if (req.path.startsWith('/api') || req.path.startsWith('/auth') || req.path.includes('.')) {
res.status(404).json({ error: 'Not found' });
return;
}
if (req.path.startsWith('/app')) {
const appHtmlPath = path.join(staticDir, 'app.html');
sendHtmlFileOrFallback(res, appHtmlPath, fallbackHtml, 200);
return;
}
const indexPath = path.join(staticDir, 'index.html');
sendHtmlFileOrFallback(res, indexPath, fallbackHtml, 200);
});
// --- WebSocket ---
const wss = new WebSocketServer({ noServer: true });
server.on('upgrade', (request, socket, head) => {
const pathname = request.url ? new URL(request.url, `http://${request.headers.host}`).pathname : '';
if (pathname === '/ws') {
wss.handleUpgrade(request, socket, head, (ws) => {
if (mode === 'mock') {
handleMockWebSocket(ws, verbose);
} else if (mode === 'proxy' && relayUrl) {
handleHybridWebSocket(ws, getRelaycastSnapshot, relayUrl, verbose, brokerApiKey);
} else {
handleStandaloneWebSocket(ws, getRelaycastSnapshot, verbose);
}
});
return;
}
if (mode !== 'mock' && (pathname === '/ws/logs' || pathname.startsWith('/ws/logs/'))) {
wss.handleUpgrade(request, socket, head, (ws) => {
handleStandaloneLogWebSocket(ws, pathname, dataDir, getLocalAgentNames, verbose);
});
return;
}
socket.destroy();
});
const close = (): Promise<void> => {
return new Promise((resolve) => {
wss.close(() => {
server.close(() => {
resolve();
});
});
});
};
return { app, server, wss, close, mode, setRelayApiKey };
}
/**
* Try to listen on a port, returns the port if successful or null if in use.
*/
function tryListen(server: Server, port: number): Promise<number | null> {
return new Promise((resolve) => {
const onError = (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
server.removeListener('error', onError);
resolve(null);
}
};
const host = getBindHost();
server.once('error', onError);
if (host) {
server.listen(port, host, () => {
server.removeListener('error', onError);
resolve(port);
});
} else {
server.listen(port, () => {
server.removeListener('error', onError);
resolve(port);
});
}
});
}
/**
* Find an available port starting from the preferred port.
*/
async function findAvailablePort(server: Server, preferredPort: number, maxAttempts = 10): Promise<number> {
for (let i = 0; i < maxAttempts; i++) {
const port = preferredPort + i;
const result = await tryListen(server, port);
if (result !== null) {
return result;
}
server.close();
}
throw new Error(`Could not find available port after ${maxAttempts} attempts starting from ${preferredPort}`);
}
/**
* Bootstrap Relaycast credentials from the broker's authenticated /api/config
* endpoint. The workspace key is on an authenticated route (not /health) so it
* is not exposed to unauthenticated callers.
*/
async function bootstrapRelayApiKeyFromBroker(
relayUrl: string,
setRelayApiKey: (key: string) => void,
): Promise<void> {
const brokerApiKey = process.env.RELAY_BROKER_API_KEY?.trim();
const headers: Record<string, string> = {};
if (brokerApiKey) {
headers['x-api-key'] = brokerApiKey;
}
// Retry a few times to allow the broker to fully start up.
for (let attempt = 0; attempt < 5; attempt++) {
try {
const res = await fetch(`${relayUrl}/api/config`, { headers });
if (!res.ok) break;
const json = await res.json() as { workspaceKey?: string };
const key = typeof json.workspaceKey === 'string' ? json.workspaceKey.trim() : '';
if (key) {
setRelayApiKey(key);
console.log(`[dashboard] Relaycast workspace key: ${key}`);
console.log('[dashboard] View messages at https://agentrelay.dev/observer');
return;
}
} catch {
// Broker not yet ready — wait and retry.
}
await new Promise((r) => setTimeout(r, 500));
}
}
/**
* Start the dashboard server.
*/
export async function startServer(options: DashboardServerOptions = {}): Promise<DashboardServer> {
const preferredPort = options.port || parseInt(process.env.PORT || '3888', 10);
const dashboard = createServer(options);
const actualPort = await findAvailablePort(dashboard.server, preferredPort);
if (actualPort !== preferredPort) {
console.log(`[dashboard] Port ${preferredPort} in use, using port ${actualPort}`);
}
console.log(`[dashboard] Server running at http://localhost:${actualPort}`);
if (dashboard.mode === 'mock') {
console.log('[dashboard] Using mock data - ready for standalone testing');
} else if (dashboard.mode === 'proxy') {
console.log(`[dashboard] Proxy mode enabled - broker URL ${normalizeRelayUrl(options.relayUrl ?? process.env.RELAY_URL)}`);
} else {
console.log('[dashboard] Standalone mode enabled - relaycast data only');
}
// Best-effort: fetch workspace key from the broker health endpoint so the
// dashboard uses the same Relaycast workspace as the broker without any file persistence.
const resolvedRelayUrl = normalizeRelayUrl(options.relayUrl ?? process.env.RELAY_URL);
if (resolvedRelayUrl && !options.mock) {
bootstrapRelayApiKeyFromBroker(resolvedRelayUrl, dashboard.setRelayApiKey).catch(() => {});
}
return dashboard;
}