feat: create dispatch plans from group messages
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireRequestSession } from "@/lib/boss-auth";
|
||||
import { appendProjectMessage, readState } from "@/lib/boss-data";
|
||||
import { replyToMasterAgentUserMessage } from "@/lib/boss-master-agent";
|
||||
import {
|
||||
queueGroupDispatchPlan,
|
||||
replyToMasterAgentUserMessage,
|
||||
} from "@/lib/boss-master-agent";
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
@@ -24,10 +27,28 @@ export async function POST(
|
||||
body: body.body,
|
||||
kind: body.kind ?? "text",
|
||||
});
|
||||
let dispatchPlan = null;
|
||||
let masterReply:
|
||||
| { ok: boolean; reason?: string; message?: string; accountId?: string; requestId?: string }
|
||||
| undefined;
|
||||
|
||||
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) {
|
||||
dispatchPlan = await queueGroupDispatchPlan({
|
||||
groupProjectId: projectId,
|
||||
requestMessageId: message.id,
|
||||
requestText: message.body,
|
||||
requestedBy: session.account,
|
||||
});
|
||||
}
|
||||
|
||||
if (projectId === "master-agent" && (body.kind ?? "text") === "text" && message.body.trim()) {
|
||||
masterReply = await replyToMasterAgentUserMessage({
|
||||
requestMessageId: message.id,
|
||||
@@ -38,15 +59,15 @@ export async function POST(
|
||||
});
|
||||
}
|
||||
|
||||
const state = await readState();
|
||||
const project = state.projects.find((item) => item.id === projectId);
|
||||
const collaborationGate = project
|
||||
const nextState = shouldCreateDispatchPlan ? await readState() : state;
|
||||
const nextProject = nextState.projects.find((item) => item.id === projectId);
|
||||
const collaborationGate = nextProject
|
||||
? {
|
||||
isGroup: project.isGroup,
|
||||
collaborationMode: project.collaborationMode,
|
||||
isGroup: nextProject.isGroup,
|
||||
collaborationMode: nextProject.collaborationMode,
|
||||
requiresMasterAgentApproval:
|
||||
project.isGroup && project.collaborationMode === "approval_required",
|
||||
approvalState: project.approvalState,
|
||||
nextProject.isGroup && nextProject.collaborationMode === "approval_required",
|
||||
approvalState: nextProject.approvalState,
|
||||
}
|
||||
: {
|
||||
isGroup: false,
|
||||
@@ -55,7 +76,7 @@ export async function POST(
|
||||
approvalState: "not_required" as const,
|
||||
};
|
||||
|
||||
return NextResponse.json({ ok: true, message, masterReply, collaborationGate });
|
||||
return NextResponse.json({ ok: true, message, masterReply, dispatchPlan, collaborationGate });
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: error instanceof Error ? error.message : "UNKNOWN_ERROR" },
|
||||
|
||||
@@ -130,7 +130,10 @@ export type AiProvider = "master_codex_node" | "openai_api";
|
||||
export type AiAccountRole = "primary" | "backup" | "api_fallback";
|
||||
export type AiAccountStatus = "ready" | "needs_login" | "needs_api_key" | "degraded" | "disabled";
|
||||
export type MasterAgentTaskStatus = "queued" | "running" | "completed" | "failed";
|
||||
export type MasterAgentTaskType = "conversation_reply" | "attachment_analysis";
|
||||
export type MasterAgentTaskType =
|
||||
| "conversation_reply"
|
||||
| "attachment_analysis"
|
||||
| "group_dispatch_plan";
|
||||
export type DispatchPlanStatus =
|
||||
| "pending_user_confirmation"
|
||||
| "approved"
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
AUTH_SESSION_TTL_MS,
|
||||
aiProviderLabel,
|
||||
appendProjectMessage,
|
||||
createDispatchPlan,
|
||||
getProjectAttachment,
|
||||
getAttachmentStorageConfig,
|
||||
getRuntimeAiAccountById,
|
||||
@@ -213,6 +214,70 @@ function buildMasterCodexNodePrompt(
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function summarizeDispatchRequest(requestText: string) {
|
||||
const compact = requestText.trim().replace(/\s+/g, " ");
|
||||
if (!compact) {
|
||||
return "用户发来新的群聊协作请求";
|
||||
}
|
||||
if (compact.length <= 36) {
|
||||
return compact;
|
||||
}
|
||||
return `${compact.slice(0, 33)}...`;
|
||||
}
|
||||
|
||||
export async function queueGroupDispatchPlan(params: {
|
||||
groupProjectId: string;
|
||||
requestMessageId: string;
|
||||
requestText: string;
|
||||
requestedBy: string;
|
||||
}) {
|
||||
const state = await readState();
|
||||
const project = state.projects.find((item) => item.id === params.groupProjectId);
|
||||
if (!project) {
|
||||
throw new Error("PROJECT_NOT_FOUND");
|
||||
}
|
||||
if (!project.isGroup) {
|
||||
throw new Error("PROJECT_NOT_GROUP_CHAT");
|
||||
}
|
||||
|
||||
const memberTargets = (project.groupMembers.length > 0
|
||||
? project.groupMembers
|
||||
: project.deviceIds.map((deviceId) => ({
|
||||
projectId: project.id,
|
||||
deviceId,
|
||||
threadId: project.threadMeta.threadId,
|
||||
threadDisplayName: project.threadMeta.threadDisplayName,
|
||||
folderName: project.threadMeta.folderName,
|
||||
})))
|
||||
.map((member) => ({
|
||||
deviceId: member.deviceId,
|
||||
projectId: member.projectId,
|
||||
threadId: member.threadId,
|
||||
threadDisplayName: member.threadDisplayName,
|
||||
folderName: member.folderName,
|
||||
reason: `群聊消息“${summarizeDispatchRequest(params.requestText)}”需要该线程补充状态或执行建议。`,
|
||||
}))
|
||||
.filter((target, index, array) => {
|
||||
const signature = `${target.projectId}::${target.deviceId}::${target.threadId}`;
|
||||
return array.findIndex((item) => `${item.projectId}::${item.deviceId}::${item.threadId}` === signature) === index;
|
||||
});
|
||||
|
||||
if (memberTargets.length === 0) {
|
||||
throw new Error("GROUP_DISPATCH_TARGETS_REQUIRED");
|
||||
}
|
||||
|
||||
const targetLabels = memberTargets.map((target) => target.threadDisplayName).filter(Boolean);
|
||||
const summary = `主 Agent 建议先按线程分发这条群聊消息:${summarizeDispatchRequest(params.requestText)}${targetLabels.length > 0 ? `。建议目标:${targetLabels.join("、")}` : ""}`;
|
||||
|
||||
return createDispatchPlan({
|
||||
groupProjectId: project.id,
|
||||
requestMessageId: params.requestMessageId,
|
||||
requestedBy: params.requestedBy,
|
||||
summary,
|
||||
targets: memberTargets,
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForMasterAgentTaskCompletion(taskId: string, timeoutMs = 55_000) {
|
||||
const startedAt = Date.now();
|
||||
while (Date.now() - startedAt < timeoutMs) {
|
||||
|
||||
Reference in New Issue
Block a user