6994ba1a4e
Text generation now runs through a PM2 automation worker (codex exec) and images through the ChatGPT Chrome extension, replacing the Anthropic API. generateStructured keeps its signature; results are Zod-validated on the web side and the worker is reached with a bearer secret. - AI image assistant in the post editor: editable prompt step, then image (square or 3:2 only, since Instagram rejects 2:3), stored via putBlob - Full-post queue (PostGenerationJob + migration): enqueue returns 202, FIFO claim under an advisory lock, heartbeat, stale-job failure, per-user request key, cancel while QUEUED; output is always a DRAFT with audit rows - Separate 10 images/page/day quota; text quota excludes images - Worker reports Codex usage-limit as 429 with a clear Thai message - Docs: AI-AUTOMATION.md, ARCHITECTURE/README/.env.example updated Known gap: the live FIFO queue check (scripts/check-post-queue.mjs) has not passed yet; the last run hit the Codex account usage limit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
31 lines
1.9 KiB
TypeScript
31 lines
1.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { actorLabel, requirePageAction } from "@/lib/auth/user";
|
|
import { audit } from "@/lib/audit";
|
|
import { JOB_SELECT } from "@/lib/ai/post-queue";
|
|
|
|
export async function GET(_req: Request, ctx: RouteContext<"/api/ai/post-jobs/[id]">) {
|
|
const { id } = await ctx.params;
|
|
const job = await prisma.postGenerationJob.findUnique({ where: { id }, select: { pageId: true } });
|
|
if (!job) return NextResponse.json({ error: "ไม่พบงาน" }, { status: 404 });
|
|
const auth = await requirePageAction(job.pageId, "editPost");
|
|
if (!auth.ok) return auth.response;
|
|
return NextResponse.json({ job: await prisma.postGenerationJob.findUnique({ where: { id }, select: JOB_SELECT }) }, { headers: { "Cache-Control": "no-store" } });
|
|
}
|
|
|
|
export async function DELETE(_req: Request, ctx: RouteContext<"/api/ai/post-jobs/[id]">) {
|
|
const { id } = await ctx.params;
|
|
const job = await prisma.postGenerationJob.findUnique({ where: { id }, select: { pageId: true } });
|
|
if (!job) return NextResponse.json({ error: "ไม่พบงาน" }, { status: 404 });
|
|
const auth = await requirePageAction(job.pageId, "editPost");
|
|
if (!auth.ok) return auth.response;
|
|
const cancelled = await prisma.$transaction(async (tx) => {
|
|
const changed = await tx.postGenerationJob.updateMany({ where: { id, status: "QUEUED" }, data: { status: "CANCELLED", stage: "cancelled", finishedAt: new Date() } });
|
|
if (!changed.count) return false;
|
|
await audit({ action: "AI_POST_CANCELLED", actorId: auth.user.id, actorLabel: actorLabel(auth.user), pageId: job.pageId, detail: { jobId: id } }, tx);
|
|
return true;
|
|
});
|
|
if (!cancelled) return NextResponse.json({ error: "ยกเลิกได้เฉพาะงานที่ยังรอคิว งานนี้เริ่มทำหรือจบแล้ว" }, { status: 409 });
|
|
return NextResponse.json({ ok: true });
|
|
}
|