6994ba1a4e
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>
134 lines
5.8 KiB
TypeScript
134 lines
5.8 KiB
TypeScript
"use client";
|
|
import { withBase } from "@/lib/base-path";
|
|
|
|
import { useState } from "react";
|
|
import Link from "next/link";
|
|
import { Icon } from "./Icon";
|
|
import { Notice } from "./ui";
|
|
import { CAPTION_REFINEMENTS, type CaptionRefinement } from "@/lib/ai/shared";
|
|
|
|
interface Variant {
|
|
angle: string;
|
|
caption: string;
|
|
}
|
|
|
|
/**
|
|
* Drafts or rewrites the caption with Codex CLI. The chosen text only fills the
|
|
* editor — saving and approval stay exactly as for hand-written posts.
|
|
*/
|
|
export function CaptionAssistant({
|
|
pageId,
|
|
targets,
|
|
caption,
|
|
initialTopic,
|
|
onChange,
|
|
}: {
|
|
pageId: string;
|
|
targets: string[];
|
|
caption: string;
|
|
initialTopic?: string;
|
|
onChange: (text: string, fromAi: boolean) => void;
|
|
}) {
|
|
const [open, setOpen] = useState(Boolean(initialTopic));
|
|
const [topic, setTopic] = useState(initialTopic ?? "");
|
|
const [variants, setVariants] = useState<Variant[]>([]);
|
|
const [busy, setBusy] = useState<string | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [profileMissing, setProfileMissing] = useState(false);
|
|
const [previous, setPrevious] = useState<string | null>(null);
|
|
|
|
async function run(input: { topic: string } | { refine: CaptionRefinement }) {
|
|
const label = "refine" in input ? input.refine : "draft";
|
|
setBusy(label);
|
|
setError(null);
|
|
const res = await fetch(withBase("/api/ai/caption"), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ pageId, targets, caption, ...input }),
|
|
}).catch(() => null);
|
|
const body = res ? await res.json().catch(() => ({})) : {};
|
|
setBusy(null);
|
|
if (!res?.ok) return setError(body.error ?? "AI ทำงานไม่สำเร็จ ลองใหม่อีกครั้ง");
|
|
setVariants(body.variants ?? []);
|
|
setProfileMissing(Boolean(body.profileMissing));
|
|
}
|
|
|
|
function use(v: Variant) {
|
|
setPrevious(caption);
|
|
onChange(v.caption, true);
|
|
setVariants([]);
|
|
}
|
|
|
|
if (!open) {
|
|
return (
|
|
<button type="button" className="btn btn-ghost btn-sm self-start" onClick={() => setOpen(true)}>
|
|
<Icon name="sparkle" size={15} /> ให้ AI ช่วยเขียน caption
|
|
</button>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<section className="rounded-[12px] border border-line bg-board p-3.5 flex flex-col gap-3" aria-label="ผู้ช่วยเขียน caption">
|
|
<div className="flex items-center gap-2">
|
|
<Icon name="sparkle" size={16} />
|
|
<b className="text-[14px]">ให้ AI ช่วยเขียน</b>
|
|
<button type="button" className="btn btn-ghost btn-sm ml-auto" onClick={() => setOpen(false)} aria-label="ปิดผู้ช่วย AI">
|
|
<Icon name="x" size={14} />
|
|
</button>
|
|
</div>
|
|
|
|
<label className="field">
|
|
<span>อยากโพสต์เรื่องอะไร</span>
|
|
<textarea className="textarea" rows={2} maxLength={1000} value={topic} onChange={(e) => setTopic(e.target.value)} placeholder="เช่น โปรเค้กวันแม่ ลด 15% ถึง 12 ส.ค. สั่งล่วงหน้า 2 วัน" />
|
|
</label>
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<button type="button" className="btn btn-primary btn-sm" disabled={busy !== null || !topic.trim()} onClick={() => run({ topic })}>
|
|
<Icon name="sparkle" size={14} /> {busy === "draft" ? "AI กำลังเขียน…" : "เขียนให้ 3 แบบ"}
|
|
</button>
|
|
{caption.trim() && (
|
|
<>
|
|
<span className="text-[12px] text-ink-3 ml-1">ปรับข้อความที่มี:</span>
|
|
{(Object.keys(CAPTION_REFINEMENTS) as CaptionRefinement[]).map((k) => (
|
|
<button key={k} type="button" className="chip" disabled={busy !== null} onClick={() => run({ refine: k })}>
|
|
{busy === k ? "กำลังปรับ…" : CAPTION_REFINEMENTS[k].label}
|
|
</button>
|
|
))}
|
|
</>
|
|
)}
|
|
</div>
|
|
|
|
{error && <Notice tone="red">{error}</Notice>}
|
|
{profileMissing && (
|
|
<p className="text-[12px] text-ink-3">
|
|
ยังไม่มีโปรไฟล์ร้าน — <Link href="/profile" className="underline">ตั้งไว้</Link> แล้ว AI จะเขียนตรงกับร้านมากขึ้น
|
|
</p>
|
|
)}
|
|
{previous !== null && variants.length === 0 && (
|
|
<p className="text-[12px] text-ink-3">
|
|
ใส่ข้อความจาก AI แล้ว ตรวจและแก้ได้ตามต้องการ ·{" "}
|
|
<button type="button" className="underline" onClick={() => { onChange(previous, false); setPrevious(null); }}>
|
|
เอาข้อความเดิมคืน
|
|
</button>
|
|
</p>
|
|
)}
|
|
|
|
{variants.length > 0 && (
|
|
<ul className="flex flex-col gap-2">
|
|
{variants.map((v, i) => (
|
|
<li key={i} className="rounded-[10px] border border-line bg-paper p-3 flex flex-col gap-2">
|
|
<div className="flex items-center gap-2">
|
|
<span className="pill pill-blue">{v.angle || `แบบที่ ${i + 1}`}</span>
|
|
<button type="button" className="btn btn-primary btn-sm ml-auto" onClick={() => use(v)}>
|
|
{caption.trim() ? "แทนที่ด้วยแบบนี้" : "ใช้แบบนี้"}
|
|
</button>
|
|
</div>
|
|
<p className="text-[14px] whitespace-pre-wrap break-words">{v.caption}</p>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
<p className="text-[11px] text-ink-4">AI ร่างเท่านั้น — ตรวจราคาและข้อมูลในวงเล็บ [ ] ก่อนส่งอนุมัติ</p>
|
|
</section>
|
|
);
|
|
}
|