feat: harden agent onboarding and device import flows

This commit is contained in:
kris
2026-03-31 05:18:58 +08:00
parent 4aed93e90c
commit f417fe1955
18 changed files with 975 additions and 132 deletions

View File

@@ -7,6 +7,26 @@ import {
replyToMasterAgentUserMessage,
} from "@/lib/boss-master-agent";
function buildCollaborationGate(project?: {
isGroup: boolean;
collaborationMode: "development" | "approval_required";
approvalState: "not_required" | "pending_agent" | "pending_user" | "approved" | "rejected";
}) {
return project
? {
isGroup: project.isGroup,
collaborationMode: project.collaborationMode,
requiresMasterAgentApproval: project.isGroup && project.collaborationMode === "approval_required",
approvalState: project.approvalState,
}
: {
isGroup: false,
collaborationMode: "development" as const,
requiresMasterAgentApproval: false,
approvalState: "not_required" as const,
};
}
function dispatchFailureNotice(error?: string) {
switch (error) {
case "GROUP_DISPATCH_TARGETS_REQUIRED":
@@ -33,6 +53,33 @@ export async function POST(
};
try {
const state = await readState();
const project = state.projects.find((item) => item.id === projectId);
const shouldCreateDispatchPlan =
project?.isGroup &&
project.id !== "master-agent" &&
(body.kind ?? "text") === "text" &&
(body.body ?? "").trim().length > 0;
if (shouldCreateDispatchPlan && project.collaborationMode === "approval_required") {
const pendingPlan = [...state.dispatchPlans]
.filter(
(plan) => plan.groupProjectId === projectId && plan.status === "pending_user_confirmation",
)
.sort((left, right) => right.createdAt.localeCompare(left.createdAt))[0];
if (pendingPlan) {
return NextResponse.json(
{
ok: false,
message: "当前还有一条主 Agent 推荐等待你确认,请先确认或拒绝后再继续发送新指令。",
pendingPlan,
collaborationGate: buildCollaborationGate(project),
},
{ status: 409 },
);
}
}
const message = await appendProjectMessage({
projectId,
senderLabel: session.displayName || "你",
@@ -66,14 +113,6 @@ export async function POST(
}
| null = null;
const state = await readState();
const project = state.projects.find((item) => item.id === projectId);
const shouldCreateDispatchPlan =
project?.isGroup &&
project.id !== "master-agent" &&
(body.kind ?? "text") === "text" &&
message.body.trim().length > 0;
if (shouldCreateDispatchPlan) {
try {
const recommendation = await queueGroupDispatchPlan({
@@ -146,20 +185,7 @@ export async function POST(
const nextState = shouldCreateDispatchPlan ? await readState() : state;
const nextProject = nextState.projects.find((item) => item.id === projectId);
const collaborationGate = nextProject
? {
isGroup: nextProject.isGroup,
collaborationMode: nextProject.collaborationMode,
requiresMasterAgentApproval:
nextProject.isGroup && nextProject.collaborationMode === "approval_required",
approvalState: nextProject.approvalState,
}
: {
isGroup: false,
collaborationMode: "development" as const,
requiresMasterAgentApproval: false,
approvalState: "not_required" as const,
};
const collaborationGate = buildCollaborationGate(nextProject);
return NextResponse.json({
ok: true,