feat: add workflow timing summaries

This commit is contained in:
Codex
2026-06-21 23:43:03 +08:00
parent 48f266ce32
commit 933ac94edf
4 changed files with 130 additions and 22 deletions
+12 -1
View File
@@ -31,6 +31,7 @@ export async function POST(request: Request, context: RouteContext) {
);
}
const requestStartedAt = Date.now();
try {
const result = await runOptimizationWorkflow({
input: {
@@ -71,10 +72,20 @@ export async function POST(request: Request, context: RouteContext) {
exportPaths,
rewriteRounds: result.rewrite_rounds,
stoppedAfterMaxRewrites: result.stopped_after_max_rewrites,
timing: result.timing,
});
} catch (error) {
const message = error instanceof Error ? error.message : "LLM optimization failed";
return NextResponse.json({ error: message }, { status: getErrorStatus(error) });
return NextResponse.json(
{
error: message,
timing: {
total_ms: Date.now() - requestStartedAt,
steps: [],
},
},
{ status: getErrorStatus(error) },
);
}
}
+26 -4
View File
@@ -23,17 +23,39 @@ export async function POST(request: Request) {
publish_platform: normalized.articleInput.platform,
user_instructions: normalized.articleInput.user_instructions,
});
const candidateFactCard = await extractCandidateFactCard(normalized.articleInput);
const factStartedAt = Date.now();
const timing = {
total_ms: 0,
steps: [] as Array<{ label: string; duration_ms: number }>,
};
try {
const candidateFactCard = await extractCandidateFactCard(normalized.articleInput);
const duration = Date.now() - factStartedAt;
timing.total_ms = duration;
timing.steps.push({ label: "事实卡提取", duration_ms: duration });
return NextResponse.json({ job, candidateFactCard }, { status: 201 });
return NextResponse.json({ job, candidateFactCard, timing }, { status: 201 });
} catch (error) {
const duration = Date.now() - factStartedAt;
timing.total_ms = duration;
timing.steps.push({ label: "事实卡提取", duration_ms: duration });
return jsonError(error, getErrorStatus(error), timing);
}
} catch (error) {
return jsonError(error, getErrorStatus(error));
}
}
function jsonError(error: unknown, status: number) {
function jsonError(
error: unknown,
status: number,
timing?: { total_ms: number; steps: Array<{ label: string; duration_ms: number }> },
) {
const message = error instanceof Error ? error.message : "Request failed";
return NextResponse.json({ error: message }, { status });
return NextResponse.json(
timing ? { error: message, timing } : { error: message },
{ status },
);
}
function getErrorStatus(error: unknown) {