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:
+68
-26
@@ -58,14 +58,15 @@ model User {
|
||||
mustChangePassword Boolean @default(false)
|
||||
lastLoginAt DateTime?
|
||||
|
||||
memberships PageMember[]
|
||||
postsCreated Post[] @relation("PostCreatedBy")
|
||||
postsEdited Post[] @relation("PostLastEditedBy")
|
||||
approvals PostApproval[]
|
||||
auditLogs AuditLog[]
|
||||
metaConnections MetaConnection[]
|
||||
analyses PageAnalysis[]
|
||||
identities UserIdentity[]
|
||||
memberships PageMember[]
|
||||
postsCreated Post[] @relation("PostCreatedBy")
|
||||
postsEdited Post[] @relation("PostLastEditedBy")
|
||||
approvals PostApproval[]
|
||||
auditLogs AuditLog[]
|
||||
metaConnections MetaConnection[]
|
||||
analyses PageAnalysis[]
|
||||
identities UserIdentity[]
|
||||
postGenerationJobs PostGenerationJob[]
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
@@ -132,15 +133,16 @@ model Page {
|
||||
webhookSubscribedAt DateTime?
|
||||
tokenCheckedAt DateTime?
|
||||
|
||||
members PageMember[]
|
||||
posts Post[]
|
||||
rules ReplyRule[]
|
||||
conversations Conversation[]
|
||||
insights PageInsightSnapshot[]
|
||||
auditLogs AuditLog[]
|
||||
analyses PageAnalysis[]
|
||||
profile PageProfile?
|
||||
aiUsage AiUsage[]
|
||||
members PageMember[]
|
||||
posts Post[]
|
||||
rules ReplyRule[]
|
||||
conversations Conversation[]
|
||||
insights PageInsightSnapshot[]
|
||||
auditLogs AuditLog[]
|
||||
analyses PageAnalysis[]
|
||||
profile PageProfile?
|
||||
aiUsage AiUsage[]
|
||||
generationJobs PostGenerationJob[]
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
@@ -175,13 +177,13 @@ model Post {
|
||||
// it does not publish. Becomes the default time when the post is scheduled.
|
||||
plannedAt DateTime?
|
||||
|
||||
fbPostId String?
|
||||
fbPermalink String?
|
||||
igMediaId String?
|
||||
igPermalink String?
|
||||
lastError String?
|
||||
fbPostId String?
|
||||
fbPermalink String?
|
||||
igMediaId String?
|
||||
igPermalink String?
|
||||
lastError String?
|
||||
// Sticky: set once a caption from the AI assistant is used, so approvers know.
|
||||
aiAssisted Boolean @default(false)
|
||||
aiAssisted Boolean @default(false)
|
||||
// Claim lock for publish (manual or cron): set right before calling the Graph
|
||||
// API so a concurrent publish/cron run can't double-post. Cleared once the
|
||||
// attempt settles (success or failure). A stale value past the lock's own
|
||||
@@ -195,9 +197,10 @@ model Post {
|
||||
lastEditedBy User? @relation("PostLastEditedBy", fields: [lastEditedById], references: [id])
|
||||
lastEditedAt DateTime?
|
||||
|
||||
approvals PostApproval[]
|
||||
insights PostInsightSnapshot[]
|
||||
auditLogs AuditLog[]
|
||||
approvals PostApproval[]
|
||||
insights PostInsightSnapshot[]
|
||||
auditLogs AuditLog[]
|
||||
generationJob PostGenerationJob?
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
@@ -377,3 +380,42 @@ model AiUsage {
|
||||
|
||||
@@index([pageId, createdAt])
|
||||
}
|
||||
|
||||
enum PostGenerationStatus {
|
||||
QUEUED
|
||||
RUNNING
|
||||
COMPLETED
|
||||
FAILED
|
||||
CANCELLED
|
||||
}
|
||||
|
||||
// Durable creation queue. The output is always a draft and never a published post.
|
||||
model PostGenerationJob {
|
||||
id String @id @default(cuid())
|
||||
pageId String
|
||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
||||
requestedById String
|
||||
requestedBy User @relation(fields: [requestedById], references: [id], onDelete: Restrict)
|
||||
requestKey String
|
||||
topic String
|
||||
targets Platform[]
|
||||
plannedAt DateTime?
|
||||
profileSnapshot Json
|
||||
status PostGenerationStatus @default(QUEUED)
|
||||
stage String @default("queued")
|
||||
captionResult Json?
|
||||
imagePrompt String?
|
||||
imageBlobUrl String?
|
||||
postId String? @unique
|
||||
post Post? @relation(fields: [postId], references: [id], onDelete: SetNull)
|
||||
error String?
|
||||
startedAt DateTime?
|
||||
heartbeatAt DateTime?
|
||||
finishedAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@unique([requestedById, requestKey])
|
||||
@@index([status, createdAt])
|
||||
@@index([pageId, createdAt])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user