Skip to content

Commit 2ca7957

Browse files
committed
feat: add HTTP message endpoint on WS server, pass managerPort to agents
- WS server: POST /message for agents to send messages (saved to API + broadcast) - WS server: GET /health endpoint - Spawner: inject TOBAN_MANAGER_PORT env var - Types: add managerPort to AgentConfig
1 parent d083968 commit 2ca7957

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ async function runLoop(cliArgs: CliArgs, runner: AgentRunner): Promise<void> {
570570
parentAgent: cliArgs.agentName,
571571
sprintNumber: sprintData.sprint.number,
572572
...(Object.keys(secrets).length > 0 ? { secrets } : {}),
573+
...(actualWsPort ? { managerPort: actualWsPort } : {}),
573574
};
574575

575576
ui.agentSpawned({

src/spawner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export function spawnAgent(
110110
TOBAN_API_URL: config.apiUrl,
111111
TOBAN_AGENT_NAME: config.name,
112112
TOBAN_TASK_ID: config.taskId,
113+
...(config.managerPort ? { TOBAN_MANAGER_PORT: String(config.managerPort) } : {}),
113114
// Inject project secrets directly as env vars (no prefix in non-Docker mode)
114115
...(config.secrets ?? {}),
115116
},

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface AgentConfig {
2929
commandTemplate?: string;
3030
/** Project secrets to inject into the agent environment */
3131
secrets?: Record<string, string>;
32+
/** Manager WS server port (for agent HTTP messaging) */
33+
managerPort?: number;
3234
}
3335

3436
export type AgentStatus = "spawning" | "running" | "completed" | "failed" | "stopped";

src/ws-server.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ export class WsChatServer {
5858
*/
5959
async start(): Promise<number> {
6060
return new Promise((resolve, reject) => {
61-
this.httpServer = createServer();
61+
this.httpServer = createServer((req, res) => {
62+
this.handleHttpRequest(req, res);
63+
});
6264

6365
this.wss = new WebSocketServer({
6466
server: this.httpServer,
@@ -222,6 +224,53 @@ export class WsChatServer {
222224
return this.port;
223225
}
224226

227+
private handleHttpRequest(req: IncomingMessage, res: import("node:http").ServerResponse): void {
228+
// Health check
229+
if (req.method === "GET" && (req.url === "/" || req.url === "/health")) {
230+
res.writeHead(200, { "Content-Type": "application/json" });
231+
res.end(JSON.stringify({ ok: true, clients: this.clients.size }));
232+
return;
233+
}
234+
235+
// Agent message endpoint
236+
if (req.method === "POST" && req.url === "/message") {
237+
let body = "";
238+
req.on("data", (chunk: Buffer) => { body += chunk.toString(); });
239+
req.on("end", () => {
240+
try {
241+
const { from, to, content } = JSON.parse(body);
242+
if (!from || !to || !content) {
243+
res.writeHead(400, { "Content-Type": "application/json" });
244+
res.end(JSON.stringify({ error: "from, to, content required" }));
245+
return;
246+
}
247+
this.handleAgentMessage(from, to, content);
248+
res.writeHead(200, { "Content-Type": "application/json" });
249+
res.end(JSON.stringify({ ok: true }));
250+
} catch {
251+
res.writeHead(400, { "Content-Type": "application/json" });
252+
res.end(JSON.stringify({ error: "Invalid JSON" }));
253+
}
254+
});
255+
return;
256+
}
257+
258+
res.writeHead(404);
259+
res.end();
260+
}
261+
262+
private handleAgentMessage(from: string, to: string, content: string): void {
263+
ui.info(`[ws] Agent message: ${from}${to}`);
264+
this.saveMessageToApi(from, to, content).catch(() => {});
265+
this.broadcast({
266+
type: "chat",
267+
from,
268+
to,
269+
content,
270+
timestamp: new Date().toISOString(),
271+
});
272+
}
273+
225274
private async handleMessage(ws: WebSocket, msg: WsMessage): Promise<void> {
226275
switch (msg.type) {
227276
case "ping":

0 commit comments

Comments
 (0)