Skip to content

Commit 504b70e

Browse files
authored
Merge pull request #72 from AgentWorkforce/fix-updated-broker
fix updated broker key needed
2 parents 0160ce3 + 701e472 commit 504b70e

5 files changed

Lines changed: 30 additions & 7 deletions

File tree

packages/dashboard-server/src/lib/send-strategy.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,19 @@ export interface SendStrategy {
6161
* Used when running in proxy mode with a broker URL configured.
6262
*/
6363
export class BrokerSendStrategy implements SendStrategy {
64-
constructor(private brokerUrl: string) {}
64+
constructor(private brokerUrl: string, private brokerApiKey?: string) {}
6565

6666
async send(request: SendRequest): Promise<SendOutcome> {
6767
try {
68+
const headers: Record<string, string> = {
69+
'content-type': 'application/json',
70+
};
71+
if (this.brokerApiKey) {
72+
headers['x-api-key'] = this.brokerApiKey;
73+
}
6874
const upstream = await fetch(`${this.brokerUrl}/api/send`, {
6975
method: 'POST',
70-
headers: {
71-
'content-type': 'application/json',
72-
},
76+
headers,
7377
body: JSON.stringify({
7478
to: request.to,
7579
message: request.message,
@@ -159,6 +163,7 @@ export class DirectSendStrategy implements SendStrategy {
159163
export interface CreateSendStrategyOptions {
160164
brokerProxyEnabled: boolean;
161165
brokerUrl?: string;
166+
brokerApiKey?: string;
162167
relaycastConfig?: RelaycastConfig | null;
163168
dataDir: string;
164169
}
@@ -171,7 +176,7 @@ export interface CreateSendStrategyOptions {
171176
*/
172177
export function createSendStrategy(opts: CreateSendStrategyOptions): SendStrategy | null {
173178
if (opts.brokerProxyEnabled && opts.brokerUrl) {
174-
return new BrokerSendStrategy(opts.brokerUrl);
179+
return new BrokerSendStrategy(opts.brokerUrl, opts.brokerApiKey);
175180
}
176181

177182
if (opts.relaycastConfig) {

packages/dashboard-server/src/lib/spawned-agents.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,12 @@ export function createSpawnedAgentsCaches(opts: {
275275
relayUrl: string | undefined;
276276
dataDir: string;
277277
verbose: boolean;
278+
brokerApiKey?: string;
278279
}): {
279280
getSpawnedAgents: () => Promise<{ names: Set<string> | null; agents: SpawnedAgentSummary[] | null }>;
280281
getLocalAgentNames: () => Set<string> | null;
281282
} {
282-
const { brokerProxyEnabled, relayUrl, dataDir, verbose } = opts;
283+
const { brokerProxyEnabled, relayUrl, dataDir, verbose, brokerApiKey } = opts;
283284

284285
let spawnedAgentsCache: { expiresAt: number; names: Set<string> | null; agents: SpawnedAgentSummary[] | null } = {
285286
expiresAt: 0,
@@ -305,9 +306,13 @@ export function createSpawnedAgentsCaches(opts: {
305306
}
306307

307308
try {
309+
const headers: Record<string, string> = { Accept: 'application/json' };
310+
if (brokerApiKey) {
311+
headers['x-api-key'] = brokerApiKey;
312+
}
308313
const response = await fetch(`${relayUrl}/api/spawned`, {
309314
method: 'GET',
310-
headers: { Accept: 'application/json' },
315+
headers,
311316
});
312317

313318
if (!response.ok) {

packages/dashboard-server/src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export interface RouteContext {
112112
verbose: boolean;
113113
relayUrl: string | undefined;
114114
brokerProxyEnabled: boolean;
115+
brokerApiKey: string | undefined;
115116
resolveRelaycastConfig: () => RelaycastConfig | null;
116117
setRelayApiKey: (apiKey: string) => void;
117118
setRelayAgentIdentity: (token: string, name: string) => void;

packages/dashboard-server/src/proxy-server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,13 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer
237237
inMemoryAgentName = undefined;
238238
clearRegistrationCache();
239239
};
240+
const brokerApiKey = process.env.RELAY_BROKER_API_KEY?.trim() || undefined;
240241
const { getSpawnedAgents, getLocalAgentNames } = createSpawnedAgentsCaches({
241242
brokerProxyEnabled,
242243
relayUrl,
243244
dataDir,
244245
verbose,
246+
brokerApiKey,
245247
});
246248

247249
const getRelaycastSnapshot = async (): Promise<DashboardSnapshot> => {
@@ -333,6 +335,7 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer
333335
const strategy: SendStrategy | null = createSendStrategy({
334336
brokerProxyEnabled,
335337
brokerUrl: relayUrl,
338+
brokerApiKey,
336339
relaycastConfig: config,
337340
dataDir,
338341
});
@@ -394,6 +397,7 @@ export function createServer(options: DashboardServerOptions = {}): DashboardSer
394397
verbose,
395398
relayUrl,
396399
brokerProxyEnabled,
400+
brokerApiKey,
397401
resolveRelaycastConfig,
398402
setRelayApiKey,
399403
setRelayAgentIdentity,

packages/dashboard-server/src/routes/broker-proxy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export function registerBrokerProxyRoutes(app: Express, ctx: RouteContext): void
2626
const headers: Record<string, string> = {
2727
'content-type': 'application/json',
2828
};
29+
if (ctx.brokerApiKey) {
30+
headers['x-api-key'] = ctx.brokerApiKey;
31+
}
2932
const workspaceId = req.header('x-workspace-id');
3033
if (workspaceId) {
3134
headers['x-workspace-id'] = workspaceId;
@@ -74,6 +77,11 @@ export function registerBrokerProxyRoutes(app: Express, ctx: RouteContext): void
7477
ws: false,
7578
logger: ctx.verbose ? console : undefined,
7679
on: {
80+
proxyReq: (proxyReq) => {
81+
if (ctx.brokerApiKey) {
82+
proxyReq.setHeader('x-api-key', ctx.brokerApiKey);
83+
}
84+
},
7785
error: (err, _req, res) => {
7886
console.error('[dashboard] Broker proxy error:', (err as Error).message);
7987
if (res && 'writeHead' in res && typeof res.writeHead === 'function') {

0 commit comments

Comments
 (0)