接入文章优化案例自动保存
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { requireApiAccess } from "../../../../lib/api/auth";
|
||||
import type { ProcessSummaryStep } from "../../../../lib/cases/types";
|
||||
import { buildArticleCaseSummary, createProcessStep } from "../../../../lib/cases/summaries";
|
||||
import { getRepositoryFromRuntime } from "../../../../lib/db/repository";
|
||||
import { optimizationFactCardSchema } from "../../../../lib/domain/validation";
|
||||
import type { LlmAuditSummary } from "../../../../lib/llm/audit";
|
||||
import { LlmValidationError } from "../../../../lib/llm/client";
|
||||
import { getExportStoreFromRuntime } from "../../../../lib/workflow/export-store";
|
||||
import { extractCandidateFactCard } from "../../../../lib/workflow/fact-extractor";
|
||||
@@ -56,11 +59,26 @@ export async function POST(request: Request) {
|
||||
};
|
||||
|
||||
let jobId: string | undefined;
|
||||
let caseId: string | undefined;
|
||||
let stage: OptimizationStreamStage = "job";
|
||||
const llmAuditSummary: LlmAuditSummary[] = [];
|
||||
const processSummary: ProcessSummaryStep[] = [];
|
||||
const requestStartedAt = Date.now();
|
||||
|
||||
try {
|
||||
const repository = getRepositoryFromRuntime();
|
||||
const caseSummary = buildArticleCaseSummary({
|
||||
source_title: normalized.articleInput.title,
|
||||
source_body: normalized.articleInput.body,
|
||||
publish_platform: normalized.articleInput.platform,
|
||||
});
|
||||
const optimizationCase = await repository.createOptimizationCase({
|
||||
case_type: "article",
|
||||
...caseSummary,
|
||||
});
|
||||
caseId = optimizationCase.id;
|
||||
const job = await repository.createArticleJob({
|
||||
case_id: optimizationCase.id,
|
||||
source_title: normalized.articleInput.title,
|
||||
source_body: normalized.articleInput.body,
|
||||
image_inputs: normalized.articleInput.images,
|
||||
@@ -68,12 +86,40 @@ export async function POST(request: Request) {
|
||||
user_instructions: normalized.articleInput.user_instructions,
|
||||
});
|
||||
jobId = job.id;
|
||||
send({ type: "job_created", job: { id: job.id } });
|
||||
await repository.saveCaseInput({
|
||||
case_id: optimizationCase.id,
|
||||
case_type: "article",
|
||||
article_job_id: job.id,
|
||||
payload: {
|
||||
source_title: normalized.articleInput.title,
|
||||
source_body: normalized.articleInput.body,
|
||||
image_inputs: normalized.articleInput.images,
|
||||
publish_platform: normalized.articleInput.platform,
|
||||
user_instructions: normalized.articleInput.user_instructions,
|
||||
},
|
||||
});
|
||||
send({
|
||||
type: "job_created",
|
||||
job: { id: job.id },
|
||||
case: { id: optimizationCase.id, case_type: "article" },
|
||||
});
|
||||
|
||||
stage = "fact_card";
|
||||
const factCardStartedAt = Date.now();
|
||||
const factCard = optimizationFactCardSchema.parse(
|
||||
payload.fact_card ??
|
||||
(await extractCandidateFactCard(normalized.articleInput)),
|
||||
(await extractCandidateFactCard(normalized.articleInput, {
|
||||
onAuditSummary: (summary) => llmAuditSummary.push(summary),
|
||||
})),
|
||||
);
|
||||
processSummary.push(
|
||||
createProcessStep({
|
||||
stage: "fact_card",
|
||||
startedAt: factCardStartedAt,
|
||||
endedAt: Date.now(),
|
||||
status: "success",
|
||||
producedResultVersion: false,
|
||||
}),
|
||||
);
|
||||
const savedFactCard = await repository.saveFactCard(job.id, factCard);
|
||||
send({
|
||||
@@ -90,6 +136,7 @@ export async function POST(request: Request) {
|
||||
stage = stageForEvent(event, stage);
|
||||
send(event);
|
||||
},
|
||||
onAuditSummary: (summary) => llmAuditSummary.push(summary),
|
||||
});
|
||||
|
||||
stage = "final";
|
||||
@@ -112,19 +159,75 @@ export async function POST(request: Request) {
|
||||
status: "optimized",
|
||||
export_paths: exportPaths,
|
||||
});
|
||||
const resultVersion = await repository.createOptimizationResultVersion({
|
||||
case_id: optimizationCase.id,
|
||||
case_type: "article",
|
||||
status: "optimized",
|
||||
article_job_id: job.id,
|
||||
article_revision: optimizedArticle.revision ?? 1,
|
||||
result_summary: optimizedArticle.summary,
|
||||
payload: {
|
||||
article: optimizedArticle,
|
||||
qa_report: qaReport,
|
||||
export_paths: exportPaths,
|
||||
},
|
||||
process_summary: [...processSummary, ...result.processSummary],
|
||||
llm_audit_summary: llmAuditSummary,
|
||||
error_stage: null,
|
||||
error_summary: null,
|
||||
});
|
||||
send({
|
||||
type: "final_ready",
|
||||
job_id: job.id,
|
||||
case: { id: optimizationCase.id, case_type: "article" },
|
||||
result_version: { id: resultVersion.id, version: resultVersion.version },
|
||||
optimized_article: optimizedArticle,
|
||||
qa_report: qaReport,
|
||||
export_paths: exportPaths,
|
||||
});
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "优化失败";
|
||||
let failedVersion:
|
||||
| { id: string; version: number }
|
||||
| undefined;
|
||||
if (caseId) {
|
||||
try {
|
||||
const repository = getRepositoryFromRuntime();
|
||||
const version = await repository.createOptimizationResultVersion({
|
||||
case_id: caseId,
|
||||
case_type: "article",
|
||||
status: "failed",
|
||||
article_job_id: jobId ?? null,
|
||||
article_revision: null,
|
||||
result_summary: "",
|
||||
payload: null,
|
||||
process_summary: [
|
||||
...processSummary,
|
||||
createProcessStep({
|
||||
stage,
|
||||
startedAt: requestStartedAt,
|
||||
endedAt: Date.now(),
|
||||
status: "failed",
|
||||
errorSummary: message,
|
||||
producedResultVersion: false,
|
||||
}),
|
||||
],
|
||||
llm_audit_summary: llmAuditSummary,
|
||||
error_stage: stage,
|
||||
error_summary: message,
|
||||
});
|
||||
failedVersion = { id: version.id, version: version.version };
|
||||
} catch {
|
||||
failedVersion = undefined;
|
||||
}
|
||||
}
|
||||
send({
|
||||
type: "failed",
|
||||
job_id: jobId,
|
||||
case: caseId ? { id: caseId, case_type: "article" } : undefined,
|
||||
result_version: failedVersion,
|
||||
stage,
|
||||
error: error instanceof Error ? error.message : "优化失败",
|
||||
error: message,
|
||||
});
|
||||
} finally {
|
||||
controller.close();
|
||||
|
||||
Reference in New Issue
Block a user