Files
PAGIVA/prisma/migrations/20261122135941_add_post_generation_queue/migration.sql
T
Loma Bot Ops 6994ba1a4e Move AI to Codex CLI + ChatGPT browser workers with a durable post queue
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>
2026-11-22 21:52:09 +07:00

50 lines
1.9 KiB
SQL

-- CreateEnum
CREATE TYPE "PostGenerationStatus" AS ENUM ('QUEUED', 'RUNNING', 'COMPLETED', 'FAILED', 'CANCELLED');
-- CreateTable
CREATE TABLE "PostGenerationJob" (
"id" TEXT NOT NULL,
"pageId" TEXT NOT NULL,
"requestedById" TEXT NOT NULL,
"requestKey" TEXT NOT NULL,
"topic" TEXT NOT NULL,
"targets" "Platform"[],
"plannedAt" TIMESTAMP(3),
"profileSnapshot" JSONB NOT NULL,
"status" "PostGenerationStatus" NOT NULL DEFAULT 'QUEUED',
"stage" TEXT NOT NULL DEFAULT 'queued',
"captionResult" JSONB,
"imagePrompt" TEXT,
"imageBlobUrl" TEXT,
"postId" TEXT,
"error" TEXT,
"startedAt" TIMESTAMP(3),
"heartbeatAt" TIMESTAMP(3),
"finishedAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "PostGenerationJob_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "PostGenerationJob_postId_key" ON "PostGenerationJob"("postId");
-- CreateIndex
CREATE INDEX "PostGenerationJob_status_createdAt_idx" ON "PostGenerationJob"("status", "createdAt");
-- CreateIndex
CREATE INDEX "PostGenerationJob_pageId_createdAt_idx" ON "PostGenerationJob"("pageId", "createdAt");
-- CreateIndex
CREATE UNIQUE INDEX "PostGenerationJob_requestedById_requestKey_key" ON "PostGenerationJob"("requestedById", "requestKey");
-- AddForeignKey
ALTER TABLE "PostGenerationJob" ADD CONSTRAINT "PostGenerationJob_pageId_fkey" FOREIGN KEY ("pageId") REFERENCES "Page"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "PostGenerationJob" ADD CONSTRAINT "PostGenerationJob_requestedById_fkey" FOREIGN KEY ("requestedById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "PostGenerationJob" ADD CONSTRAINT "PostGenerationJob_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE SET NULL ON UPDATE CASCADE;