Files
PAGIVA/src/lib/audit.ts
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

62 lines
1.4 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { prisma } from "@/lib/db";
export const SYSTEM_ACTOR = "ระบบ";
export type AuditAction =
| "META_LOGIN_CONNECTED"
| "META_LOGIN_REMOVED"
| "PAGE_CONNECTED"
| "PAGE_DISCONNECTED"
| "PAGE_WEBHOOK_SUBSCRIBED"
| "PAGE_TOKEN_INVALID"
| "PAGE_MEMBER_SET"
| "PAGE_MEMBER_REMOVED"
| "PAGE_ANALYSIS_REQUESTED"
| "PAGE_PROFILE_SAVED"
| "POST_CREATED"
| "POST_EDITED"
| "POST_PLANNED"
| "POST_SUBMITTED"
| "POST_APPROVED"
| "POST_REJECTED"
| "POST_SCHEDULED"
| "POST_PUBLISHED"
| "POST_FAILED"
| "POST_CANCELLED"
| "POST_DELETED"
| "AI_POST_QUEUED"
| "AI_POST_STARTED"
| "AI_POST_COMPLETED"
| "AI_POST_FAILED"
| "AI_POST_CANCELLED"
| "RULE_SAVED"
| "RULE_DELETED"
| "USER_CREATED"
| "USER_UPDATED"
| "USER_IDENTITY_LINKED";
/** Append-only — AuditLog rows are never updated or deleted. */
export async function audit(
entry: {
action: AuditAction;
actorId?: string | null;
actorLabel: string;
pageId?: string | null;
postId?: string | null;
detail?: Prisma.InputJsonValue;
},
tx: Prisma.TransactionClient = prisma
) {
await tx.auditLog.create({
data: {
action: entry.action,
actorId: entry.actorId ?? null,
actorLabel: entry.actorLabel,
pageId: entry.pageId ?? null,
postId: entry.postId ?? null,
detail: entry.detail,
},
});
}