import { Controller, Get, Post, Put, Delete, Param, Body, Inject, } from "@nestjs/common"; import { Hub } from "@multica/core"; @Controller("api") export class AppController { constructor(@Inject("HUB") private readonly hub: Hub) {} @Get("hub") getHub() { return { hubId: this.hub.hubId, url: this.hub.url, connectionState: this.hub.connectionState, agentCount: this.hub.listAgents().length, }; } @Put("hub/gateway") updateGateway(@Body() body: { url: string }) { this.hub.reconnect(body.url); return { url: this.hub.url, connectionState: this.hub.connectionState, }; } @Get("agents") listAgents() { return this.hub.listAgents().map((id) => { const agent = this.hub.getAgent(id); return { id, closed: agent?.closed ?? true }; }); } @Post("agents") createAgent(@Body() body?: { id?: string }) { const agent = this.hub.createAgent(body?.id); return { id: agent.sessionId }; } @Delete("agents/:id") deleteAgent(@Param("id") id: string) { const ok = this.hub.closeAgent(id); return { ok }; } }