feat(orchestrator): add SSE events, queue controls, and mosaic rails sync
This commit is contained in:
@@ -11,8 +11,11 @@ import {
|
||||
HttpCode,
|
||||
UseGuards,
|
||||
ParseUUIDPipe,
|
||||
Sse,
|
||||
MessageEvent,
|
||||
} from "@nestjs/common";
|
||||
import { Throttle } from "@nestjs/throttler";
|
||||
import { Observable } from "rxjs";
|
||||
import { QueueService } from "../../queue/queue.service";
|
||||
import { AgentSpawnerService } from "../../spawner/agent-spawner.service";
|
||||
import { AgentLifecycleService } from "../../spawner/agent-lifecycle.service";
|
||||
@@ -20,6 +23,7 @@ import { KillswitchService } from "../../killswitch/killswitch.service";
|
||||
import { SpawnAgentDto, SpawnAgentResponseDto } from "./dto/spawn-agent.dto";
|
||||
import { OrchestratorApiKeyGuard } from "../../common/guards/api-key.guard";
|
||||
import { OrchestratorThrottlerGuard } from "../../common/guards/throttler.guard";
|
||||
import { AgentEventsService } from "./agent-events.service";
|
||||
|
||||
/**
|
||||
* Controller for agent management endpoints
|
||||
@@ -41,7 +45,8 @@ export class AgentsController {
|
||||
private readonly queueService: QueueService,
|
||||
private readonly spawnerService: AgentSpawnerService,
|
||||
private readonly lifecycleService: AgentLifecycleService,
|
||||
private readonly killswitchService: KillswitchService
|
||||
private readonly killswitchService: KillswitchService,
|
||||
private readonly eventsService: AgentEventsService
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -67,6 +72,9 @@ export class AgentsController {
|
||||
context: dto.context,
|
||||
});
|
||||
|
||||
// Persist initial lifecycle state in Valkey.
|
||||
await this.lifecycleService.registerSpawnedAgent(spawnResponse.agentId, dto.taskId);
|
||||
|
||||
// Queue task in Valkey
|
||||
await this.queueService.addTask(dto.taskId, dto.context, {
|
||||
priority: 5, // Default priority
|
||||
@@ -85,6 +93,41 @@ export class AgentsController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream orchestrator events as server-sent events (SSE)
|
||||
*/
|
||||
@Sse("events")
|
||||
@Throttle({ status: { limit: 200, ttl: 60000 } })
|
||||
streamEvents(): Observable<MessageEvent> {
|
||||
return new Observable<MessageEvent>((subscriber) => {
|
||||
let isClosed = false;
|
||||
|
||||
const unsubscribe = this.eventsService.subscribe((event) => {
|
||||
if (!isClosed) {
|
||||
subscriber.next({ data: event });
|
||||
}
|
||||
});
|
||||
|
||||
void this.eventsService.getInitialSnapshot().then((snapshot) => {
|
||||
if (!isClosed) {
|
||||
subscriber.next({ data: snapshot });
|
||||
}
|
||||
});
|
||||
|
||||
const heartbeat = setInterval(() => {
|
||||
if (!isClosed) {
|
||||
subscriber.next({ data: this.eventsService.createHeartbeat() });
|
||||
}
|
||||
}, 15000);
|
||||
|
||||
return () => {
|
||||
isClosed = true;
|
||||
clearInterval(heartbeat);
|
||||
unsubscribe();
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* List all agents
|
||||
* @returns Array of all agent sessions with their status
|
||||
|
||||
Reference in New Issue
Block a user