Add automated content planning and image overlays

This commit is contained in:
Loma Bot Ops
2026-11-27 08:48:41 +07:00
parent bb840a54ff
commit 1d3bd3e7cd
10 changed files with 411 additions and 133 deletions
+13 -3
View File
@@ -10,10 +10,17 @@ import { nextContentRun } from "@/lib/ai/auto-schedule";
const Input = z.object({
enabled: z.boolean(),
scheduleMode: z.enum(["FIXED", "RANDOM"]),
weekdays: z.array(z.number().int().min(0).max(6)).min(1).max(7),
times: z.array(z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/)).min(1).max(3),
randomPostsPerDay: z.number().int().min(1).max(3),
articlePercent: z.number().int().min(0).max(100),
knowledgePercent: z.number().int().min(0).max(100),
salesPercent: z.number().int().min(0).max(100),
brief: z.string().trim().max(1000),
targets: z.array(z.enum(["FACEBOOK", "INSTAGRAM"])).min(1).max(2),
}).refine((data) => data.articlePercent + data.knowledgePercent + data.salesPercent === 100, {
message: "สัดส่วนเนื้อหาต้องรวมกันเท่ากับ 100%",
});
export async function GET(req: Request) {
@@ -33,7 +40,7 @@ export async function PUT(req: Request) {
if (!auth.ok) return auth.response;
try {
const parsed = Input.safeParse(body);
if (!parsed.success) throw new UserFacingError("เลือกวัน เวลาไม่เกิน 3 ช่วง และช่องทางให้ครบ แนวเนื้อหาไม่เกิน 1,000 ตัวอักษร");
if (!parsed.success) throw new UserFacingError(parsed.error.issues[0]?.message || "ตรวจวัน เวลา และช่องทางให้ครบ");
const input = { ...parsed.data, weekdays: [...new Set(parsed.data.weekdays)].sort(), times: [...new Set(parsed.data.times)].sort(), targets: [...new Set(parsed.data.targets)] };
const automation = await prisma.$transaction(async (tx) => {
await tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtext(${`ai-post:${pageId}`}))`;
@@ -44,7 +51,10 @@ export async function PUT(req: Request) {
if (!page.profile?.business.trim()) throw new UserFacingError("ตั้งค่าโปรไฟล์ร้านก่อนเปิด auto");
if (input.targets.includes("INSTAGRAM") && !page.igUserId) throw new UserFacingError("เพจนี้ยังไม่ได้ผูก Instagram");
}
const data = { ...input, requestedById: auth.user.id, nextRunAt: input.enabled ? nextContentRun(input.weekdays, input.times) : null, lastError: null };
const data = { ...input, requestedById: auth.user.id, nextRunAt: input.enabled ? nextContentRun({
weekdays: input.weekdays, times: input.times, mode: input.scheduleMode,
randomPostsPerDay: input.randomPostsPerDay, seed: pageId,
}) : null, lastError: null };
const saved = await tx.pageContentAutomation.upsert({ where: { pageId }, create: { pageId, ...data }, update: data, select: AUTOMATION_SELECT });
if (!input.enabled) {
const queued = await tx.postGenerationJob.findMany({ where: { pageId, status: "QUEUED", requestKey: { startsWith: "auto:" } }, select: { id: true } });
@@ -53,7 +63,7 @@ export async function PUT(req: Request) {
if (changed.count) await audit({ action: "AI_POST_CANCELLED", actorId: auth.user.id, actorLabel: actorLabel(auth.user), pageId, detail: { jobId: job.id, source: "auto-disabled" } }, tx);
}
}
await audit({ action: input.enabled ? "CONTENT_AUTO_ENABLED" : "CONTENT_AUTO_DISABLED", actorId: auth.user.id, actorLabel: actorLabel(auth.user), pageId, detail: { weekdays: input.weekdays, times: input.times, targets: input.targets } }, tx);
await audit({ action: input.enabled ? "CONTENT_AUTO_ENABLED" : "CONTENT_AUTO_DISABLED", actorId: auth.user.id, actorLabel: actorLabel(auth.user), pageId, detail: { scheduleMode: input.scheduleMode, weekdays: input.weekdays, times: input.times, randomPostsPerDay: input.randomPostsPerDay, contentMix: { article: input.articlePercent, knowledge: input.knowledgePercent, sales: input.salesPercent }, targets: input.targets } }, tx);
return saved;
});
return NextResponse.json({ automation });
+2 -1
View File
@@ -26,7 +26,8 @@ const SYSTEM = `You write prompts for an AI image generator. The images illustra
- Ask for no text, lettering, logos, watermarks or brand names inside the image (image models spell Thai badly; the caption carries the words).
- No identifiable real people, celebrities or copyrighted characters. If people appear, keep them generic.
- No before/after imagery and no exaggerated results (cures, guaranteed outcomes).
- Follow "สีและสไตล์ภาพของแบรนด์" exactly when supplied, especially its palette, medium, lighting and exclusions. Match the shop's tone and audience through the visual mood.`;
- Follow "สีและสไตล์ภาพของแบรนด์" exactly when supplied, especially its palette, medium, lighting and exclusions. Match the shop's tone and audience through the visual mood.
- When the shop operates in Thailand or targets Thai customers, ground the scene in Thailand: a Thai neighbourhood, local market, Thai-style home interior, tropical garden, wooden shophouse, or Southeast Asian textures and colour palette — so Thai viewers see something familiar and credible. Apply this only where it fits the product or service naturally; a tech or B2B brand may still prefer a neutral modern setting.`;
const IMAGE_SYSTEM = `${SYSTEM}\n${IMAGE_ART_DIRECTION}`;
/**