feat: add omx orchestration backend selection
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireRequestSession } from "@/lib/boss-auth";
|
||||
import {
|
||||
getProject,
|
||||
getProjectOrchestrationBackendState,
|
||||
updateProjectOrchestrationBackend,
|
||||
} from "@/lib/boss-data";
|
||||
|
||||
function normalizeRequestedBackendId(value: unknown) {
|
||||
return value === "omx-team" ? "omx-team" : "boss-native-orchestrator";
|
||||
}
|
||||
|
||||
async function readGroupProjectOrNotFound(projectId: string) {
|
||||
const project = await getProject(projectId);
|
||||
if (!project) {
|
||||
return { ok: false as const, response: NextResponse.json({ ok: false, message: "PROJECT_NOT_FOUND" }, { status: 404 }) };
|
||||
}
|
||||
if (!project.isGroup) {
|
||||
return {
|
||||
ok: false as const,
|
||||
response: NextResponse.json({ ok: false, message: "PROJECT_NOT_GROUP_CHAT" }, { status: 400 }),
|
||||
};
|
||||
}
|
||||
return { ok: true as const, project };
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ projectId: string }> },
|
||||
) {
|
||||
const session = await requireRequestSession(request);
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { projectId } = await context.params;
|
||||
const projectCheck = await readGroupProjectOrNotFound(projectId);
|
||||
if (!projectCheck.ok) {
|
||||
return projectCheck.response;
|
||||
}
|
||||
|
||||
const state = await getProjectOrchestrationBackendState(projectId);
|
||||
if (!state) {
|
||||
return NextResponse.json({ ok: false, message: "PROJECT_NOT_FOUND" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
...state,
|
||||
requestedBackendId: projectCheck.project.orchestrationBackendOverride ?? null,
|
||||
requestedBackendLabel: projectCheck.project.orchestrationBackendOverride
|
||||
? state.requestedBackendLabel
|
||||
: null,
|
||||
});
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ projectId: string }> },
|
||||
) {
|
||||
const session = await requireRequestSession(request);
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { projectId } = await context.params;
|
||||
const projectCheck = await readGroupProjectOrNotFound(projectId);
|
||||
if (!projectCheck.ok) {
|
||||
return projectCheck.response;
|
||||
}
|
||||
|
||||
const rawBody = await request.text().catch(() => "");
|
||||
let body: unknown;
|
||||
try {
|
||||
body = JSON.parse(rawBody);
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false, message: "INVALID_JSON_PAYLOAD" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
||||
return NextResponse.json({ ok: false, message: "INVALID_ORCHESTRATION_BACKEND_PAYLOAD" }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = body as {
|
||||
orchestrationBackendOverride?: unknown;
|
||||
backendId?: unknown;
|
||||
requestedBackendId?: unknown;
|
||||
};
|
||||
const hasOrchestrationBackendOverride = Object.prototype.hasOwnProperty.call(
|
||||
payload,
|
||||
"orchestrationBackendOverride",
|
||||
);
|
||||
const hasBackendId = Object.prototype.hasOwnProperty.call(payload, "backendId");
|
||||
const hasRequestedBackendId = Object.prototype.hasOwnProperty.call(payload, "requestedBackendId");
|
||||
if (!hasOrchestrationBackendOverride && !hasBackendId && !hasRequestedBackendId) {
|
||||
return NextResponse.json({ ok: false, message: "INVALID_ORCHESTRATION_BACKEND_PAYLOAD" }, { status: 400 });
|
||||
}
|
||||
|
||||
const requestedBackendId = normalizeRequestedBackendId(
|
||||
hasOrchestrationBackendOverride
|
||||
? payload.orchestrationBackendOverride
|
||||
: hasRequestedBackendId
|
||||
? payload.requestedBackendId
|
||||
: payload.backendId,
|
||||
);
|
||||
|
||||
try {
|
||||
await updateProjectOrchestrationBackend(projectId, requestedBackendId);
|
||||
const state = await getProjectOrchestrationBackendState(projectId);
|
||||
if (!state) {
|
||||
return NextResponse.json({ ok: false, message: "PROJECT_NOT_FOUND" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, ...state });
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: error instanceof Error ? error.message : "UNKNOWN_ERROR" },
|
||||
{ status: error instanceof Error && error.message === "PROJECT_NOT_FOUND" ? 404 : 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user