fix(web): route Mission Control API calls through orchestrator proxy #747
96
apps/web/src/app/api/orchestrator/[...path]/route.ts
Normal file
96
apps/web/src/app/api/orchestrator/[...path]/route.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const DEFAULT_ORCHESTRATOR_URL = "http://localhost:3001";
|
||||
|
||||
function getOrchestratorUrl(): string {
|
||||
return (
|
||||
process.env.ORCHESTRATOR_URL ??
|
||||
process.env.NEXT_PUBLIC_ORCHESTRATOR_URL ??
|
||||
process.env.NEXT_PUBLIC_API_URL ??
|
||||
DEFAULT_ORCHESTRATOR_URL
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic catch-all proxy for orchestrator API routes.
|
||||
*
|
||||
* Forwards any request to /api/orchestrator/<path> → ORCHESTRATOR_URL/<path>
|
||||
* with the ORCHESTRATOR_API_KEY injected server-side so it never reaches the browser.
|
||||
*
|
||||
* Supports GET, POST, PATCH, DELETE, PUT.
|
||||
*
|
||||
* Example:
|
||||
* GET /api/orchestrator/mission-control/sessions
|
||||
* → GET ORCHESTRATOR_URL/api/mission-control/sessions
|
||||
* POST /api/orchestrator/mission-control/sessions/abc/kill
|
||||
* → POST ORCHESTRATOR_URL/api/mission-control/sessions/abc/kill
|
||||
*/
|
||||
async function proxyToOrchestrator(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ path: string[] }> }
|
||||
): Promise<NextResponse> {
|
||||
const orchestratorApiKey = process.env.ORCHESTRATOR_API_KEY;
|
||||
if (!orchestratorApiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: "ORCHESTRATOR_API_KEY is not configured on the web server." },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
|
||||
const { path } = await context.params;
|
||||
const upstreamPath = `/${path.join("/")}`;
|
||||
const search = request.nextUrl.search ?? "";
|
||||
const upstreamUrl = `${getOrchestratorUrl()}${upstreamPath}${search}`;
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 30_000);
|
||||
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
"X-API-Key": orchestratorApiKey,
|
||||
};
|
||||
|
||||
const contentType = request.headers.get("Content-Type");
|
||||
if (contentType) {
|
||||
headers["Content-Type"] = contentType;
|
||||
}
|
||||
|
||||
const body =
|
||||
request.method !== "GET" && request.method !== "HEAD"
|
||||
? await request.text()
|
||||
: undefined;
|
||||
|
||||
const upstream = await fetch(upstreamUrl, {
|
||||
method: request.method,
|
||||
headers,
|
||||
body,
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
const responseText = await upstream.text();
|
||||
return new NextResponse(responseText, {
|
||||
status: upstream.status,
|
||||
headers: {
|
||||
"Content-Type":
|
||||
upstream.headers.get("Content-Type") ?? "application/json",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.name === "AbortError"
|
||||
? "Orchestrator request timed out."
|
||||
: "Unable to reach orchestrator.";
|
||||
return NextResponse.json({ error: message }, { status: 502 });
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = proxyToOrchestrator;
|
||||
export const POST = proxyToOrchestrator;
|
||||
export const PATCH = proxyToOrchestrator;
|
||||
export const PUT = proxyToOrchestrator;
|
||||
export const DELETE = proxyToOrchestrator;
|
||||
@@ -158,7 +158,7 @@ async function fetchAuditLog(
|
||||
}
|
||||
|
||||
try {
|
||||
return await apiGet<AuditLogResponse>(`/api/mission-control/audit-log?${params.toString()}`);
|
||||
return await apiGet<AuditLogResponse>(`/api/orchestrator/api/mission-control/audit-log?${params.toString()}`);
|
||||
} catch (error) {
|
||||
if (isRateLimitError(error)) {
|
||||
return createEmptyAuditLogResponse(page, "Rate limited - retrying...");
|
||||
|
||||
@@ -59,7 +59,7 @@ describe("BargeInInput", (): void => {
|
||||
await user.click(screen.getByRole("button", { name: "Send" }));
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/session-1/inject", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/session-1/inject", {
|
||||
content: "execute plan",
|
||||
});
|
||||
});
|
||||
@@ -83,12 +83,12 @@ describe("BargeInInput", (): void => {
|
||||
|
||||
const calls = mockApiPost.mock.calls as [string, unknown?][];
|
||||
|
||||
expect(calls[0]).toEqual(["/api/mission-control/sessions/session-2/pause", undefined]);
|
||||
expect(calls[0]).toEqual(["/api/orchestrator/api/mission-control/sessions/session-2/pause", undefined]);
|
||||
expect(calls[1]).toEqual([
|
||||
"/api/mission-control/sessions/session-2/inject",
|
||||
"/api/orchestrator/api/mission-control/sessions/session-2/inject",
|
||||
{ content: "hello world" },
|
||||
]);
|
||||
expect(calls[2]).toEqual(["/api/mission-control/sessions/session-2/resume", undefined]);
|
||||
expect(calls[2]).toEqual(["/api/orchestrator/api/mission-control/sessions/session-2/resume", undefined]);
|
||||
});
|
||||
|
||||
it("submits with Enter and does not submit on Shift+Enter", async (): Promise<void> => {
|
||||
@@ -105,7 +105,7 @@ describe("BargeInInput", (): void => {
|
||||
fireEvent.keyDown(textarea, { key: "Enter", code: "Enter", shiftKey: false });
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/session-3/inject", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/session-3/inject", {
|
||||
content: "first",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -39,7 +39,7 @@ export function BargeInInput({ sessionId, onSent }: BargeInInputProps): React.JS
|
||||
}
|
||||
|
||||
const encodedSessionId = encodeURIComponent(sessionId);
|
||||
const baseEndpoint = `/api/mission-control/sessions/${encodedSessionId}`;
|
||||
const baseEndpoint = `/api/orchestrator/api/mission-control/sessions/${encodedSessionId}`;
|
||||
let didPause = false;
|
||||
let didInject = false;
|
||||
|
||||
|
||||
@@ -177,7 +177,7 @@ describe("GlobalAgentRoster", (): void => {
|
||||
fireEvent.click(screen.getByRole("button", { name: "Kill session killme12" }));
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/killme123456/kill", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/killme123456/kill", {
|
||||
force: false,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -83,7 +83,7 @@ function groupByProvider(sessions: MissionControlSession[]): ProviderSessionGrou
|
||||
|
||||
async function fetchSessions(): Promise<MissionControlSession[]> {
|
||||
const payload = await apiGet<MissionControlSession[] | SessionsPayload>(
|
||||
"/api/mission-control/sessions"
|
||||
"/api/orchestrator/api/mission-control/sessions"
|
||||
);
|
||||
return Array.isArray(payload) ? payload : payload.sessions;
|
||||
}
|
||||
@@ -118,7 +118,7 @@ export function GlobalAgentRoster({
|
||||
|
||||
const killMutation = useMutation({
|
||||
mutationFn: async (sessionId: string): Promise<string> => {
|
||||
await apiPost<{ message: string }>(`/api/mission-control/sessions/${sessionId}/kill`, {
|
||||
await apiPost<{ message: string }>(`/api/orchestrator/api/mission-control/sessions/${sessionId}/kill`, {
|
||||
force: false,
|
||||
});
|
||||
return sessionId;
|
||||
|
||||
@@ -112,12 +112,12 @@ describe("KillAllDialog", (): void => {
|
||||
await user.click(screen.getByRole("button", { name: "Kill All Agents" }));
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/internal-1/kill", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/internal-1/kill", {
|
||||
force: true,
|
||||
});
|
||||
});
|
||||
|
||||
expect(mockApiPost).not.toHaveBeenCalledWith("/api/mission-control/sessions/external-1/kill", {
|
||||
expect(mockApiPost).not.toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/external-1/kill", {
|
||||
force: true,
|
||||
});
|
||||
expect(onComplete).toHaveBeenCalledTimes(1);
|
||||
@@ -141,10 +141,10 @@ describe("KillAllDialog", (): void => {
|
||||
await user.click(screen.getByRole("button", { name: "Kill All Agents" }));
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/internal-2/kill", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/internal-2/kill", {
|
||||
force: true,
|
||||
});
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/external-2/kill", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/external-2/kill", {
|
||||
force: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -96,7 +96,7 @@ export function KillAllDialog({ sessions, onComplete }: KillAllDialogProps): Rea
|
||||
|
||||
const killRequests = targetSessions.map(async (session) => {
|
||||
try {
|
||||
await apiPost<{ message: string }>(`/api/mission-control/sessions/${session.id}/kill`, {
|
||||
await apiPost<{ message: string }>(`/api/orchestrator/api/mission-control/sessions/${session.id}/kill`, {
|
||||
force: true,
|
||||
});
|
||||
return true;
|
||||
|
||||
@@ -89,7 +89,7 @@ describe("PanelControls", (): void => {
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith(
|
||||
"/api/mission-control/sessions/session%20with%20space/pause",
|
||||
"/api/orchestrator/api/mission-control/sessions/session%20with%20space/pause",
|
||||
undefined
|
||||
);
|
||||
});
|
||||
@@ -114,7 +114,7 @@ describe("PanelControls", (): void => {
|
||||
await user.click(screen.getByRole("button", { name: "Confirm" }));
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/session-4/kill", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/session-4/kill", {
|
||||
force: false,
|
||||
});
|
||||
});
|
||||
@@ -137,7 +137,7 @@ describe("PanelControls", (): void => {
|
||||
await user.click(screen.getByRole("button", { name: "Confirm" }));
|
||||
|
||||
await waitFor((): void => {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/mission-control/sessions/session-5/kill", {
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/api/orchestrator/api/mission-control/sessions/session-5/kill", {
|
||||
force: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -50,23 +50,23 @@ export function PanelControls({
|
||||
switch (action) {
|
||||
case "pause":
|
||||
await apiPost<{ message: string }>(
|
||||
`/api/mission-control/sessions/${encodeURIComponent(sessionId)}/pause`
|
||||
`/api/orchestrator/api/mission-control/sessions/${encodeURIComponent(sessionId)}/pause`
|
||||
);
|
||||
return { nextStatus: "paused" };
|
||||
case "resume":
|
||||
await apiPost<{ message: string }>(
|
||||
`/api/mission-control/sessions/${encodeURIComponent(sessionId)}/resume`
|
||||
`/api/orchestrator/api/mission-control/sessions/${encodeURIComponent(sessionId)}/resume`
|
||||
);
|
||||
return { nextStatus: "active" };
|
||||
case "graceful-kill":
|
||||
await apiPost<{ message: string }>(
|
||||
`/api/mission-control/sessions/${encodeURIComponent(sessionId)}/kill`,
|
||||
`/api/orchestrator/api/mission-control/sessions/${encodeURIComponent(sessionId)}/kill`,
|
||||
{ force: false }
|
||||
);
|
||||
return { nextStatus: "killed" };
|
||||
case "force-kill":
|
||||
await apiPost<{ message: string }>(
|
||||
`/api/mission-control/sessions/${encodeURIComponent(sessionId)}/kill`,
|
||||
`/api/orchestrator/api/mission-control/sessions/${encodeURIComponent(sessionId)}/kill`,
|
||||
{ force: true }
|
||||
);
|
||||
return { nextStatus: "killed" };
|
||||
|
||||
Reference in New Issue
Block a user