Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/dashboard-server/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@
*/

import fs from 'fs';
import os from 'node:os';
import path from 'path';
import type { Response } from 'express';
import { loadTeamsConfig } from '@agent-relay/config';
import type { DashboardChannel } from './types.js';

/**
* Returns the current OS username, falling back to `fallback` if os.userInfo()
* throws (e.g. in containers where the UID has no /etc/passwd entry).
*/
export function safeUsername(fallback = 'Dashboard'): string {
try {
const name = os.userInfo().username;
if (name) return name;
} catch {
// no-op — fall through to fallback
}
return fallback;
}

export const PHANTOM_OFFLINE_MAX_AGE_MS = 5 * 60 * 1000;
export const SPAWNED_CACHE_TTL_MS = 3000;
export const STANDALONE_WS_POLL_MS = 3000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Dashboard Server relay-config refresh', () => {
});

it('reuses the refreshed in-memory token for later relay-config reads without any file persistence', async () => {
const expectedProjectIdentity = path.basename(path.resolve(dataDir, '..'));
const expectedProjectIdentity = os.userInfo().username;
const getDashboardAgentToken = vi.fn()
.mockResolvedValueOnce({
token: 'agt_old',
Expand Down
8 changes: 4 additions & 4 deletions packages/dashboard-server/src/proxy-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
sendHtmlFileOrFallback,
getBindHost,
mapChannelForDashboard,
safeUsername,
} from './lib/utils.js';
import {
filterPhantomAgents,
Expand Down Expand Up @@ -209,11 +210,11 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer
if (!inMemoryRelayApiKey) return null;

const baseUrl = process.env.RELAYCAST_API_URL || 'https://api.relaycast.dev';
const projectDir = path.basename(path.resolve(dataDir, '..'));
const projectIdentity = safeUsername(path.basename(path.resolve(dataDir, '..')));
return applyCachedAgentIdentity({
apiKey: inMemoryRelayApiKey,
baseUrl,
projectIdentity: projectDir,
projectIdentity,
});
};

Expand Down Expand Up @@ -320,8 +321,7 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer
}

const projectIdentity = config?.agentName?.trim()
|| path.basename(path.resolve(dataDir, '..'))
|| DASHBOARD_DISPLAY_NAME;
|| safeUsername(DASHBOARD_DISPLAY_NAME);
const senderInput = params.from?.trim() ?? '';
const senderName = mode === 'proxy'
? resolveIdentity(senderInput || projectIdentity, {
Expand Down
2 changes: 1 addition & 1 deletion packages/dashboard-server/src/relaycast-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('relaycast-provider loadRelaycastConfig', () => {

expect(loaded).toMatchObject({
apiKey: 'rk_test',
projectIdentity: path.basename(path.resolve(dataDir, '..')),
projectIdentity: os.userInfo().username,
});
expect(loaded?.agentName).toBeUndefined();
expect(loaded?.agentToken).toBeUndefined();
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard-server/src/relaycast-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import path from 'path';
import { RelayCast } from '@relaycast/sdk';
import { extractMessageId } from './lib/message-id.js';
import { safeUsername } from './lib/utils.js';
import type {
AgentStatus,
CreateChannelInput,
Expand Down Expand Up @@ -84,10 +85,9 @@ export type {
*/
export function loadRelaycastConfig(dataDir: string): RelaycastConfig | null {
const baseUrl = process.env.RELAYCAST_API_URL || DEFAULT_RELAYCAST_BASE_URL;
const projectDir = path.basename(path.resolve(dataDir, '..'));
const envApiKey = process.env.RELAY_API_KEY?.trim();
if (envApiKey) {
const projectIdentity = projectDir.trim();
const projectIdentity = safeUsername(path.basename(path.resolve(dataDir, '..')));
return { apiKey: envApiKey, baseUrl, projectIdentity };
}

Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard-server/src/routes/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Channel route handlers: list, create, join, leave, messages, archive, etc.
*/

import path from 'path';
import type { Express, Request, Response } from 'express';
import {
fetchChannelMembers,
Expand All @@ -20,10 +19,11 @@ import {
normalizeChannelTarget,
normalizeChannelName,
parseInviteMembers,
safeUsername,
} from '../lib/utils.js';

export function registerChannelRoutes(app: Express, ctx: RouteContext): void {
const projectName = path.basename(path.resolve(ctx.dataDir, '..')) || 'Dashboard';
const projectName = safeUsername();
app.get('/api/channels', async (_req: Request, res: Response) => {
try {
const channels = await ctx.getRelaycastChannels();
Expand Down
5 changes: 2 additions & 3 deletions packages/dashboard-server/src/routes/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
* Health and keep-alive route handlers.
*/

import path from 'node:path';
import type { Express, Request, Response } from 'express';
import type { RouteContext } from '../lib/types.js';
import { countOnlineAgents } from '../lib/utils.js';
import { countOnlineAgents, safeUsername } from '../lib/utils.js';
import { mockAgents } from '../mocks/fixtures.js';

export function registerHealthRoutes(app: Express, ctx: RouteContext): void {
const projectName = path.basename(path.resolve(ctx.dataDir, '..'));
const projectName = safeUsername();

app.get('/health', (_req: Request, res: Response) => {
res.json({
Expand Down
6 changes: 3 additions & 3 deletions packages/dashboard-server/src/routes/relay-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import crypto from 'crypto';
import path from 'path';
import crypto from 'node:crypto';
import type { Express, Request, Response } from 'express';
import type { RouteContext } from '../lib/types.js';
import { getDashboardAgentToken, getWriterClient } from '../relaycast-provider-helpers.js';
import { safeUsername } from '../lib/utils.js';

export function registerRelayConfigRoutes(app: Express, ctx: RouteContext): void {
// Allow the workflow runner (or any local caller) to push a Relaycast API key
Expand Down Expand Up @@ -90,7 +90,7 @@ export function registerRelayConfigRoutes(app: Express, ctx: RouteContext): void
// token was cached earlier in this process. The new token remains cached
// in memory so future requests reuse it without any file persistence.
let agentToken = forceRefresh ? undefined : config.agentToken;
let agentName = config.agentName ?? path.basename(path.resolve(ctx.dataDir, '..'));
let agentName = config.agentName ?? safeUsername();

if (!agentToken) {
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard-server/src/routes/thread-replies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Thread reply routes for proxy/standalone modes.
*/

import path from 'path';
import type { Express, Request, Response } from 'express';
import { fetchAllMessages } from '../relaycast-provider.js';
import type { Message, RelaycastConfig } from '../relaycast-provider-types.js';
import { resolveIdentity } from '../lib/identity.js';
import type { RouteContext } from '../lib/types.js';
import { safeUsername } from '../lib/utils.js';

function parseBeforeCursor(raw: unknown): number | undefined {
if (typeof raw !== 'string' || raw.trim() === '') {
Expand Down Expand Up @@ -68,7 +68,7 @@ function resolveSenderName(
}

export function registerThreadReplyRoutes(app: Express, ctx: RouteContext): void {
const projectName = path.basename(path.resolve(ctx.dataDir, '..')) || 'Dashboard';
const projectName = safeUsername();

app.get('/api/messages/:id/replies', async (req: Request, res: Response) => {
const idParam = req.params.id;
Expand Down
Loading