2026-05-02 03:18:47 +02:00
|
|
|
import api from "./client";
|
2026-06-29 05:38:21 +02:00
|
|
|
import type {
|
|
|
|
|
OrchestratorStatus,
|
|
|
|
|
AgentStatusResponse,
|
|
|
|
|
WaitingAgent,
|
|
|
|
|
} from "@/types";
|
|
|
|
|
import {
|
|
|
|
|
isMockMode,
|
|
|
|
|
mockOrchestratorStatus,
|
|
|
|
|
mockWaitingAgents,
|
|
|
|
|
} from "@/lib/mock-data";
|
2026-05-02 03:18:47 +02:00
|
|
|
|
|
|
|
|
export interface SpawnAgentRequest {
|
|
|
|
|
task_id?: string;
|
|
|
|
|
initial_prompt?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const orchestratorApi = {
|
|
|
|
|
// Get orchestrator status
|
|
|
|
|
getStatus: async (): Promise<OrchestratorStatus> => {
|
|
|
|
|
if (isMockMode()) {
|
|
|
|
|
return mockOrchestratorStatus as OrchestratorStatus;
|
|
|
|
|
}
|
|
|
|
|
const { data } = await api.get<OrchestratorStatus>("/orchestrator/status");
|
|
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Get specific agent status
|
|
|
|
|
getAgentStatus: async (agentId: string): Promise<AgentStatusResponse> => {
|
|
|
|
|
if (isMockMode()) {
|
2026-06-29 05:38:21 +02:00
|
|
|
const found = mockOrchestratorStatus.agents.find(
|
|
|
|
|
(a) => a.agent_id === agentId,
|
|
|
|
|
);
|
2026-05-02 03:18:47 +02:00
|
|
|
if (found) return found as AgentStatusResponse;
|
|
|
|
|
throw new Error("Agent not found");
|
|
|
|
|
}
|
2026-06-29 05:38:21 +02:00
|
|
|
const { data } = await api.get<AgentStatusResponse>(
|
|
|
|
|
"/orchestrator/agents/" + agentId,
|
|
|
|
|
);
|
2026-05-02 03:18:47 +02:00
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Get waiting agents
|
|
|
|
|
getWaitingAgents: async (): Promise<WaitingAgent[]> => {
|
|
|
|
|
if (isMockMode()) {
|
|
|
|
|
return mockWaitingAgents as WaitingAgent[];
|
|
|
|
|
}
|
|
|
|
|
const { data } = await api.get<WaitingAgent[]>("/orchestrator/waiting");
|
|
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Spawn an agent
|
2026-06-29 05:38:21 +02:00
|
|
|
spawn: async (
|
|
|
|
|
agentId: string,
|
|
|
|
|
request?: SpawnAgentRequest,
|
|
|
|
|
): Promise<AgentStatusResponse> => {
|
2026-05-02 03:18:47 +02:00
|
|
|
if (isMockMode()) {
|
|
|
|
|
// Find or create agent in mock status
|
2026-06-29 05:38:21 +02:00
|
|
|
const existing = mockOrchestratorStatus.agents.find(
|
|
|
|
|
(a) => a.agent_id === agentId,
|
|
|
|
|
);
|
2026-05-02 03:18:47 +02:00
|
|
|
if (existing) {
|
|
|
|
|
existing.state = "running";
|
|
|
|
|
// Mock data uses string, so use empty string for no task
|
|
|
|
|
(existing as { task_id: string }).task_id = request?.task_id ?? "";
|
|
|
|
|
return existing as AgentStatusResponse;
|
|
|
|
|
}
|
|
|
|
|
const newAgent = {
|
|
|
|
|
agent_id: agentId,
|
|
|
|
|
state: "running",
|
|
|
|
|
task_id: request?.task_id ?? "",
|
|
|
|
|
error_count: 0,
|
|
|
|
|
started_at: new Date().toISOString(),
|
|
|
|
|
waiting_for: null as string | null,
|
|
|
|
|
};
|
|
|
|
|
mockOrchestratorStatus.agents.push(newAgent);
|
|
|
|
|
return newAgent as AgentStatusResponse;
|
|
|
|
|
}
|
|
|
|
|
const { data } = await api.post<AgentStatusResponse>(
|
|
|
|
|
"/orchestrator/agents/" + agentId + "/spawn",
|
2026-06-29 05:38:21 +02:00
|
|
|
{ agent_id: agentId, ...request },
|
2026-05-02 03:18:47 +02:00
|
|
|
);
|
|
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Stop an agent
|
|
|
|
|
stop: async (agentId: string, graceful: boolean = true): Promise<void> => {
|
|
|
|
|
if (isMockMode()) {
|
2026-06-29 05:38:21 +02:00
|
|
|
const agent = mockOrchestratorStatus.agents.find(
|
|
|
|
|
(a) => a.agent_id === agentId,
|
|
|
|
|
);
|
2026-05-02 03:18:47 +02:00
|
|
|
if (agent) {
|
|
|
|
|
agent.state = "idle";
|
|
|
|
|
(agent as { task_id: string }).task_id = "";
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await api.post("/orchestrator/agents/" + agentId + "/stop", null, {
|
|
|
|
|
params: { graceful },
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Resolve a waiting agent
|
2026-06-29 05:38:21 +02:00
|
|
|
resolveWait: async (
|
|
|
|
|
agentId: string,
|
|
|
|
|
resolution: string,
|
|
|
|
|
): Promise<AgentStatusResponse> => {
|
2026-05-02 03:18:47 +02:00
|
|
|
if (isMockMode()) {
|
|
|
|
|
// Remove from waiting agents
|
2026-06-29 05:38:21 +02:00
|
|
|
const idx = mockWaitingAgents.findIndex((a) => a.agent_id === agentId);
|
2026-05-02 03:18:47 +02:00
|
|
|
if (idx !== -1) mockWaitingAgents.splice(idx, 1);
|
|
|
|
|
// Update agent status
|
2026-06-29 05:38:21 +02:00
|
|
|
const agent = mockOrchestratorStatus.agents.find(
|
|
|
|
|
(a) => a.agent_id === agentId,
|
|
|
|
|
);
|
2026-05-02 03:18:47 +02:00
|
|
|
if (agent) {
|
|
|
|
|
agent.state = "running";
|
|
|
|
|
return agent as AgentStatusResponse;
|
|
|
|
|
}
|
|
|
|
|
throw new Error("Agent not found");
|
|
|
|
|
}
|
|
|
|
|
const { data } = await api.post<AgentStatusResponse>(
|
|
|
|
|
"/orchestrator/agents/" + agentId + "/resolve-wait",
|
2026-06-29 05:38:21 +02:00
|
|
|
{ resolution },
|
2026-05-02 03:18:47 +02:00
|
|
|
);
|
|
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
};
|