Files
PAGIVA/scripts/lib/codex-runtime.mjs
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

39 lines
1.7 KiB
JavaScript

import { execFile } from "node:child_process";
import { access } from "node:fs/promises";
import { homedir } from "node:os";
import path from "node:path";
import { promisify } from "node:util";
const execute = promisify(execFile);
export const DEFAULT_CODEX_MODEL = "gpt-5.6-sol";
let invocation;
/** Isolate background content jobs from the desktop model default and unrelated MCP servers. */
export function codexInvocation() {
invocation ??= resolveInvocation();
return invocation;
}
async function resolveInvocation() {
let bin = process.env.CODEX_BIN;
if (!bin && process.platform === "win32") {
const installed = path.join(process.env.LOCALAPPDATA || path.join(homedir(), "AppData", "Local"), "Programs", "OpenAI", "Codex", "bin", "codex.exe");
try { await access(installed); bin = installed; } catch { bin = "codex.exe"; }
}
bin ||= "codex";
const configArgs = [];
try {
const { stdout } = await execute(bin, ["mcp", "list", "--json"], { windowsHide: true, timeout: 15_000, maxBuffer: 1024 * 1024 });
const servers = JSON.parse(stdout);
if (!Array.isArray(servers)) throw new Error("Invalid MCP list");
for (const server of servers) {
if (typeof server.name !== "string" || !/^[A-Za-z0-9_-]+$/.test(server.name)) throw new Error("Unsupported MCP configuration key");
configArgs.push("-c", `mcp_servers.${server.name}.enabled=false`);
}
} catch {
// Fail closed rather than loading external integrations while processing untrusted page content.
throw new Error("Cannot inspect Codex MCP configuration; check CODEX_BIN and run codex mcp list --json.");
}
return { bin, args: ["-m", process.env.CODEX_MODEL || DEFAULT_CODEX_MODEL, ...configArgs] };
}