feat: add dispatch plan confirmation flow
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireRequestSession } from "@/lib/boss-auth";
|
||||
import { confirmDispatchPlanAndCreateExecutions } from "@/lib/boss-data";
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ projectId: string; planId: string }> },
|
||||
) {
|
||||
const session = await requireRequestSession(request);
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = (await request.json().catch(() => ({}))) as {
|
||||
approvedTargetProjectIds?: string[];
|
||||
};
|
||||
const { projectId, planId } = await context.params;
|
||||
|
||||
try {
|
||||
const result = await confirmDispatchPlanAndCreateExecutions({
|
||||
groupProjectId: projectId,
|
||||
planId,
|
||||
confirmedBy: session.account,
|
||||
approvedTargetProjectIds: Array.isArray(body.approvedTargetProjectIds)
|
||||
? body.approvedTargetProjectIds.filter(
|
||||
(item): item is string => typeof item === "string" && item.trim().length > 0,
|
||||
)
|
||||
: [],
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
plan: result.plan,
|
||||
executions: result.executions,
|
||||
notice: result.notice,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: error instanceof Error ? error.message : "UNKNOWN_ERROR" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
26
src/app/api/v1/projects/[projectId]/dispatch-plans/route.ts
Normal file
26
src/app/api/v1/projects/[projectId]/dispatch-plans/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireRequestSession } from "@/lib/boss-auth";
|
||||
import { listDispatchPlansByProject, readState } from "@/lib/boss-data";
|
||||
|
||||
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 state = await readState();
|
||||
const project = state.projects.find((item) => item.id === projectId);
|
||||
if (!project) {
|
||||
return NextResponse.json({ ok: false, message: "PROJECT_NOT_FOUND" }, { status: 404 });
|
||||
}
|
||||
if (!project.isGroup) {
|
||||
return NextResponse.json({ ok: false, message: "PROJECT_NOT_GROUP_CHAT" }, { status: 400 });
|
||||
}
|
||||
|
||||
const plans = await listDispatchPlansByProject(projectId);
|
||||
return NextResponse.json({ ok: true, plans });
|
||||
}
|
||||
@@ -28,6 +28,14 @@ export async function POST(
|
||||
kind: body.kind ?? "text",
|
||||
});
|
||||
let dispatchPlan = null;
|
||||
let dispatchRecommendation:
|
||||
| {
|
||||
ok: boolean;
|
||||
taskId?: string;
|
||||
status: "completed" | "failed" | "skipped";
|
||||
error?: string;
|
||||
}
|
||||
| null = null;
|
||||
let masterReply:
|
||||
| { ok: boolean; reason?: string; message?: string; accountId?: string; requestId?: string }
|
||||
| undefined;
|
||||
@@ -41,12 +49,27 @@ export async function POST(
|
||||
message.body.trim().length > 0;
|
||||
|
||||
if (shouldCreateDispatchPlan) {
|
||||
dispatchPlan = await queueGroupDispatchPlan({
|
||||
groupProjectId: projectId,
|
||||
requestMessageId: message.id,
|
||||
requestText: message.body,
|
||||
requestedBy: session.account,
|
||||
});
|
||||
try {
|
||||
const recommendation = await queueGroupDispatchPlan({
|
||||
groupProjectId: projectId,
|
||||
requestMessageId: message.id,
|
||||
requestText: message.body,
|
||||
requestedBy: session.account,
|
||||
});
|
||||
dispatchRecommendation = recommendation;
|
||||
dispatchPlan = recommendation.dispatchPlan;
|
||||
} catch (error) {
|
||||
dispatchRecommendation = {
|
||||
ok: false,
|
||||
status: "failed",
|
||||
error: error instanceof Error ? error.message : "GROUP_DISPATCH_PLAN_FAILED",
|
||||
};
|
||||
}
|
||||
} else {
|
||||
dispatchRecommendation = {
|
||||
ok: false,
|
||||
status: "skipped",
|
||||
};
|
||||
}
|
||||
|
||||
if (projectId === "master-agent" && (body.kind ?? "text") === "text" && message.body.trim()) {
|
||||
@@ -76,7 +99,14 @@ export async function POST(
|
||||
approvalState: "not_required" as const,
|
||||
};
|
||||
|
||||
return NextResponse.json({ ok: true, message, masterReply, dispatchPlan, collaborationGate });
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
message,
|
||||
masterReply,
|
||||
dispatchPlan,
|
||||
dispatchRecommendation,
|
||||
collaborationGate,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: error instanceof Error ? error.message : "UNKNOWN_ERROR" },
|
||||
|
||||
Reference in New Issue
Block a user