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,30 @@
|
||||
import { runAutomation } from "./automation";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { UserFacingError } from "@/lib/facebook/client";
|
||||
import { type ImageSizeKey } from "./image-shared";
|
||||
|
||||
/** Successful image generations allowed per page in any rolling 24 hours (images cost far more than text). */
|
||||
export const IMAGE_DAILY_LIMIT_PER_PAGE = 10;
|
||||
|
||||
export interface GeneratedImage {
|
||||
bytes: Buffer;
|
||||
contentType: "image/jpeg";
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
}
|
||||
|
||||
/** Counts this page's successful image generations in the last 24 hours. */
|
||||
export async function assertImageQuota(pageId: string): Promise<void> {
|
||||
const since = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
const used = await prisma.aiUsage.count({ where: { pageId, kind: "image", createdAt: { gte: since } } });
|
||||
if (used >= IMAGE_DAILY_LIMIT_PER_PAGE) {
|
||||
throw new UserFacingError(`เพจนี้สร้างรูปด้วย AI ครบ ${IMAGE_DAILY_LIMIT_PER_PAGE} รูปใน 24 ชั่วโมงแล้ว ลองใหม่ภายหลัง`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Image bytes come from the dedicated ChatGPT browser worker. */
|
||||
export async function generateImage(prompt: string, size: ImageSizeKey, waitForWorker = false): Promise<GeneratedImage> {
|
||||
const result = await runAutomation({ kind: "image", prompt, size }, { waitForWorker });
|
||||
if (typeof result.b64 !== "string" || !result.b64) throw new UserFacingError("Worker ไม่ส่งรูปกลับมา ตรวจการ login ChatGPT แล้วลองใหม่");
|
||||
return { bytes: Buffer.from(result.b64, "base64"), contentType: "image/jpeg", inputTokens: 0, outputTokens: 0 };
|
||||
}
|
||||
Reference in New Issue
Block a user