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>
This commit is contained in:
Loma Bot Ops
2026-11-22 21:52:09 +07:00
parent 4a673ebb28
commit 6994ba1a4e
41 changed files with 2007 additions and 113 deletions
+5 -4
View File
@@ -18,11 +18,11 @@ import { fileURLToPath } from "node:url";
import { PrismaClient } from "@prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import { budgetPlan, GOALS, validateReport } from "../src/lib/analysis/plan.mjs";
import { codexInvocation } from "./lib/codex-runtime.mjs";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const SCHEMA_PATH = path.resolve(HERE, "../src/lib/analysis/report.schema.json");
const CODEX_BIN = process.env.CODEX_BIN || "codex";
const CODEX_MODEL = process.env.CODEX_MODEL || "";
const TIMEOUT_MS = Number(process.env.ANALYSIS_TIMEOUT_MS) || 10 * 60 * 1000;
const POLL_MS = Number(process.env.ANALYSIS_POLL_MS) || 5000;
@@ -57,7 +57,8 @@ function buildPrompt(brief, input) {
].join("\n");
}
function runCodex(prompt, workDir) {
async function runCodex(prompt, workDir) {
const codex = await codexInvocation();
const outFile = path.join(workDir, "report.json");
const args = [
"exec",
@@ -68,11 +69,11 @@ function runCodex(prompt, workDir) {
"--output-schema", SCHEMA_PATH,
"-o", outFile,
"-C", workDir,
...(CODEX_MODEL ? ["-m", CODEX_MODEL] : []),
...codex.args,
"-",
];
return new Promise((resolve, reject) => {
const child = spawn(CODEX_BIN, args, { cwd: workDir, windowsHide: true, stdio: ["pipe", "ignore", "pipe"] });
const child = spawn(codex.bin, args, { cwd: workDir, windowsHide: true, stdio: ["pipe", "ignore", "pipe"] });
let stderr = "";
child.stderr.on("data", (d) => {
stderr = (stderr + d).slice(-4000);