feat: expose optimizer workflow api
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import {
|
||||
createBrandTemplate,
|
||||
getArticleJob,
|
||||
saveFactCard,
|
||||
updateArticleJob,
|
||||
} from "../../../../../lib/db/repositories";
|
||||
import { confirmedFactCardSchema } from "../../../../../lib/domain/validation";
|
||||
|
||||
interface RouteContext {
|
||||
params: Promise<{ jobId: string }>;
|
||||
}
|
||||
|
||||
export async function POST(request: Request, context: RouteContext) {
|
||||
const { jobId } = await context.params;
|
||||
const job = getArticleJob(undefined, jobId);
|
||||
if (!job) {
|
||||
return NextResponse.json({ error: "Job not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const factCard = confirmedFactCardSchema.parse(await request.json());
|
||||
const brandTemplate = createBrandTemplate(undefined, {
|
||||
brand_name: factCard.brand_names[0] ?? factCard.company_short_names[0] ?? factCard.company_full_name,
|
||||
company_full_name: factCard.company_full_name,
|
||||
company_short_names: factCard.company_short_names,
|
||||
product_names: factCard.product_names,
|
||||
target_industries: [factCard.target_industry],
|
||||
target_audience: [factCard.target_audience],
|
||||
verified_claims: factCard.core_claims,
|
||||
forbidden_claims: factCard.forbidden_claims,
|
||||
tone_rules: {
|
||||
official_site: "official brand voice",
|
||||
media_article: "objective third-party voice",
|
||||
},
|
||||
});
|
||||
const savedFactCard = saveFactCard(undefined, jobId, factCard);
|
||||
updateArticleJob(undefined, jobId, {
|
||||
brand_template_id: brandTemplate.id,
|
||||
status: "fact_confirmed",
|
||||
});
|
||||
|
||||
return NextResponse.json({ brandTemplate, factCard: savedFactCard });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Invalid fact card";
|
||||
return NextResponse.json({ error: message }, { status: 400 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { getAppDataDir } from "../../../../../../lib/db/connection";
|
||||
|
||||
const CONTENT_TYPES: Record<string, string> = {
|
||||
"optimized.md": "text/markdown; charset=utf-8",
|
||||
"optimized.docx":
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"qa_report.json": "application/json; charset=utf-8",
|
||||
};
|
||||
|
||||
interface RouteContext {
|
||||
params: Promise<{ jobId: string; fileName: string }>;
|
||||
}
|
||||
|
||||
export async function GET(_request: Request, context: RouteContext) {
|
||||
const { jobId, fileName } = await context.params;
|
||||
const contentType = CONTENT_TYPES[fileName];
|
||||
if (!contentType) {
|
||||
return NextResponse.json({ error: "Export file not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const path = join(getAppDataDir(), "exports", jobId, fileName);
|
||||
if (!existsSync(path)) {
|
||||
return NextResponse.json({ error: "Export file not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return new Response(readFileSync(path), {
|
||||
headers: {
|
||||
"content-type": contentType,
|
||||
"content-disposition": `attachment; filename="${fileName}"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import {
|
||||
getArticleJob,
|
||||
getFactCard,
|
||||
saveOptimizedArticle,
|
||||
saveQaReport,
|
||||
updateArticleJob,
|
||||
} from "../../../../../lib/db/repositories";
|
||||
import { runOptimizationWorkflow } from "../../../../../lib/workflow/orchestrator";
|
||||
|
||||
interface RouteContext {
|
||||
params: Promise<{ jobId: string }>;
|
||||
}
|
||||
|
||||
export async function POST(_request: Request, context: RouteContext) {
|
||||
const { jobId } = await context.params;
|
||||
const job = getArticleJob(undefined, jobId);
|
||||
if (!job) {
|
||||
return NextResponse.json({ error: "Job not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const factCardRecord = getFactCard(undefined, jobId);
|
||||
if (!factCardRecord) {
|
||||
return NextResponse.json(
|
||||
{ error: "Confirm the fact card before optimizing" },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
|
||||
const result = await runOptimizationWorkflow({
|
||||
input: {
|
||||
title: job.source_title,
|
||||
body: job.source_body,
|
||||
images: job.image_inputs,
|
||||
platform: job.publish_platform,
|
||||
user_instructions: job.user_instructions,
|
||||
},
|
||||
factCard: factCardRecord,
|
||||
});
|
||||
const optimizedArticle = saveOptimizedArticle(undefined, jobId, result.article);
|
||||
const qaReport = saveQaReport(
|
||||
undefined,
|
||||
jobId,
|
||||
optimizedArticle.revision ?? 1,
|
||||
result.qaReport,
|
||||
);
|
||||
updateArticleJob(undefined, jobId, {
|
||||
status: qaReport.overall_status === "fail" ? "qa_failed" : "optimized",
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
optimizedArticle,
|
||||
qaReport,
|
||||
rewriteRounds: result.rewrite_rounds,
|
||||
stoppedAfterMaxRewrites: result.stopped_after_max_rewrites,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { createArticleJob } from "../../../lib/db/repositories";
|
||||
import { extractCandidateFactCard } from "../../../lib/workflow/fact-extractor";
|
||||
import { normalizeInput } from "../../../lib/workflow/input-normalizer";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const payload = await request.json();
|
||||
const normalized = normalizeInput(payload);
|
||||
const job = createArticleJob(undefined, {
|
||||
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,
|
||||
});
|
||||
const candidateFactCard = await extractCandidateFactCard(normalized.articleInput);
|
||||
|
||||
return NextResponse.json({ job, candidateFactCard }, { status: 201 });
|
||||
} catch (error) {
|
||||
return jsonError(error, 400);
|
||||
}
|
||||
}
|
||||
|
||||
function jsonError(error: unknown, status: number) {
|
||||
const message = error instanceof Error ? error.message : "Request failed";
|
||||
return NextResponse.json({ error: message }, { status });
|
||||
}
|
||||
Reference in New Issue
Block a user