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:
@@ -0,0 +1,32 @@
|
||||
import { UserFacingError } from "@/lib/facebook/client";
|
||||
|
||||
/** Authenticated bridge to the machine with Codex CLI and a dedicated Chrome profile. */
|
||||
export async function runAutomation(body: Record<string, unknown>, options: { waitForWorker?: boolean } = {}): Promise<Record<string, unknown>> {
|
||||
const url = process.env.AI_AUTOMATION_URL;
|
||||
const secret = process.env.AI_AUTOMATION_SECRET;
|
||||
if (!url || !secret) throw new UserFacingError("ยังไม่ได้ตั้งค่า automation worker — ติดต่อผู้ดูแลระบบ");
|
||||
try {
|
||||
const attempts = options.waitForWorker ? 31 : 1;
|
||||
for (let attempt = 0; attempt < attempts; attempt++) {
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${secret}`, "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
signal: AbortSignal.timeout(310_000),
|
||||
cache: "no-store",
|
||||
});
|
||||
if (response.status === 409) {
|
||||
if (attempt + 1 < attempts) { await new Promise((resolve) => setTimeout(resolve, 2000)); continue; }
|
||||
throw new UserFacingError("Worker กำลังทำงานอื่น ลองสั่งสร้างใหม่เมื่อพร้อม");
|
||||
}
|
||||
if (response.status === 429) throw new UserFacingError("โควตา AI ของบัญชีที่ใช้สร้างเนื้อหาเต็มชั่วคราว ลองใหม่อีกครั้งภายหลัง");
|
||||
if (!response.ok) throw new Error(`Worker status ${response.status}`);
|
||||
return await response.json();
|
||||
}
|
||||
throw new Error("Worker did not accept the job");
|
||||
} catch (error) {
|
||||
if (error instanceof UserFacingError) throw error;
|
||||
console.error("[ai:automation] worker failed", error);
|
||||
throw new UserFacingError("Automation ทำงานไม่สำเร็จหรือกำลังติดงานอื่น — ตรวจ worker และการ login แล้วลองใหม่");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user