Implement attachment analysis task flow

This commit is contained in:
kris
2026-03-29 16:21:05 +08:00
parent 8273340f7f
commit 9e4b64ba9e
5 changed files with 453 additions and 5 deletions

View File

@@ -0,0 +1,56 @@
import { NextRequest, NextResponse } from "next/server";
import { requireRequestSession } from "@/lib/boss-auth";
import { canSessionAccessAttachmentProject } from "@/lib/boss-attachment-access";
import { getProjectAttachment, readState } from "@/lib/boss-data";
import { queueAttachmentAnalysisTask } from "@/lib/boss-master-agent";
export const runtime = "nodejs";
export async function POST(
request: NextRequest,
context: { params: Promise<{ projectId: string; attachmentId: string }> },
) {
const session = await requireRequestSession(request);
if (!session) {
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
}
const { projectId, attachmentId } = await context.params;
const record = await getProjectAttachment(projectId, attachmentId);
if (!record) {
return NextResponse.json({ ok: false, message: "ATTACHMENT_NOT_FOUND" }, { status: 404 });
}
const state = await readState();
if (!canSessionAccessAttachmentProject(state, session, record.project)) {
return NextResponse.json({ ok: false, message: "FORBIDDEN" }, { status: 403 });
}
if (record.attachment.analysisState !== "ready_manual" && record.attachment.analysisState !== "failed") {
return NextResponse.json(
{
ok: false,
message: "ATTACHMENT_NOT_READY_FOR_MANUAL_ANALYSIS",
analysisState: record.attachment.analysisState,
},
{ status: 409 },
);
}
try {
const task = await queueAttachmentAnalysisTask({
projectId,
attachmentId,
requestMessageId: record.message.id,
requestedBy: session.displayName || "你",
requestedByAccount: session.account,
markProcessing: true,
});
return NextResponse.json({ ok: true, taskId: task.taskId, task });
} catch (error) {
const message = error instanceof Error ? error.message : "UNKNOWN_ERROR";
const status = message === "ATTACHMENT_NOT_FOUND" ? 404 : 500;
return NextResponse.json({ ok: false, message }, { status });
}
}

View File

@@ -8,6 +8,7 @@ import {
readState,
type MessageAttachment,
} from "@/lib/boss-data";
import { queueAttachmentAnalysisTask } from "@/lib/boss-master-agent";
import { detectAttachmentKind, resolveAttachmentAnalysisState } from "@/lib/boss-attachments";
import { getAttachmentStorageProvider } from "@/lib/boss-storage";
@@ -81,10 +82,22 @@ export async function POST(
attachment,
});
let analysisTask = null;
if (attachment.analysisState === "queued_auto") {
analysisTask = await queueAttachmentAnalysisTask({
projectId,
attachmentId,
requestMessageId: message.id,
requestedBy: session.displayName || "你",
requestedByAccount: session.account,
});
}
return NextResponse.json({
ok: true,
attachment,
message,
analysisTask,
downloadUrl: `/api/v1/attachments/${attachmentId}/download`,
});
}