diff --git a/scripts/ai-automation-worker.mjs b/scripts/ai-automation-worker.mjs index dbdcdd2..cd6b4b3 100644 --- a/scripts/ai-automation-worker.mjs +++ b/scripts/ai-automation-worker.mjs @@ -117,7 +117,15 @@ const server = createServer(async (req, res) => { } catch (error) { console.error("[ai-worker] job failed", error); if (error?.usageLimit) reply(429, { error: "Codex usage limit reached" }); - else reply(500, { error: "Automation failed; inspect worker logs and login" }); + else { + const message = String(error?.message || ""); + const code = /foreground focus|Open normal Chrome/i.test(message) ? "DESKTOP_UNAVAILABLE" + : /login required/i.test(message) ? "LOGIN_REQUIRED" + : /composer unavailable|side panel did not open|extension side panel first/i.test(message) ? "CHAT_UNAVAILABLE" + : /image.*(?:download|export|visible)|download.*finish/i.test(message) ? "IMAGE_EXPORT_FAILED" + : "AUTOMATION_FAILED"; + reply(500, { error: "Automation failed; inspect worker logs", code }); + } } finally { if (dir) await rm(dir, { recursive: true, force: true }); busy = false; diff --git a/src/lib/ai/automation.ts b/src/lib/ai/automation.ts index ab7ece9..b6b4187 100644 --- a/src/lib/ai/automation.ts +++ b/src/lib/ai/automation.ts @@ -20,7 +20,19 @@ export async function runAutomation(body: Record, options: { wa throw new UserFacingError("Worker กำลังทำงานอื่น ลองสั่งสร้างใหม่เมื่อพร้อม"); } if (response.status === 429) throw new UserFacingError("โควตา AI ของบัญชีที่ใช้สร้างเนื้อหาเต็มชั่วคราว ลองใหม่อีกครั้งภายหลัง"); - if (!response.ok) throw new Error(`Worker status ${response.status}`); + if (!response.ok) { + const failure = await response.json().catch(() => ({})); + const hints: Record = { + DESKTOP_UNAVAILABLE: "เครื่องสร้างรูปไม่มีหน้าจอ Chrome ที่ใช้งานได้ — ปลดล็อก desktop session ของ worker แล้วลองใหม่", + LOGIN_REQUIRED: "บัญชี ChatGPT บนเครื่อง worker ต้องเข้าสู่ระบบใหม่", + CHAT_UNAVAILABLE: "หน้า ChatGPT บนเครื่อง worker ไม่พร้อมใช้งาน — ตรวจหน้าเว็บหรือ extension", + IMAGE_EXPORT_FAILED: "สร้างรูปแล้ว แต่ดึงไฟล์จาก ChatGPT ไม่สำเร็จ — ตรวจรูปใน Chrome ก่อนลองใหม่", + }; + const code = typeof failure.code === "string" ? failure.code : "UNKNOWN"; + console.error("[ai:automation] worker response", { status: response.status, code }); + if (hints[code]) throw new UserFacingError(hints[code]); + throw new Error(`Worker status ${response.status} (${code})`); + } return await response.json(); } throw new Error("Worker did not accept the job"); diff --git a/src/lib/facebook/posts.ts b/src/lib/facebook/posts.ts index f8de334..e544ca0 100644 --- a/src/lib/facebook/posts.ts +++ b/src/lib/facebook/posts.ts @@ -1,5 +1,6 @@ import { bareId, graphDelete, graphGet, graphGetAll, graphPostForm, graphPostJson, UserFacingError } from "./client"; import type { PageCredentials } from "./tokens"; +import sharp from "sharp"; /** * Facebook rejects a scheduled_publish_time under 10 minutes or more than ~30 @@ -73,7 +74,19 @@ async function facebookImageSource(imageUrl: string): Promise<{ blob: Blob; file if (bytes.byteLength > MAX_FACEBOOK_IMAGE_BYTES) { throw new UserFacingError("ไฟล์รูปใหญ่เกิน 8MB กรุณาอัปโหลดรูปที่เล็กลง"); } - return { blob: new Blob([bytes], { type: contentType }), filename }; + // Re-encode to a baseline JPEG so Graph receives actual image bytes even + // when storage serves a misleading MIME type or the upload has bad metadata. + try { + const metadata = await sharp(Buffer.from(bytes), { limitInputPixels: 40_000_000 }).metadata(); + if (!metadata.width || !metadata.height || !["jpeg", "png"].includes(metadata.format ?? "")) throw new Error("Unsupported image"); + const normalized = await sharp(Buffer.from(bytes), { limitInputPixels: 40_000_000 }) + .rotate().flatten({ background: "#ffffff" }).jpeg({ quality: 88, mozjpeg: true }).toBuffer(); + if (normalized.length > MAX_FACEBOOK_IMAGE_BYTES) throw new Error("Image too large after conversion"); + return { blob: new Blob([normalized], { type: "image/jpeg" }), filename: "image.jpg" }; + } catch (error) { + console.error("[publish:fb] image normalization failed", { message: error instanceof Error ? error.message : String(error), contentType, size: bytes.byteLength }); + throw new UserFacingError("ไฟล์รูปเสียหรือแปลงเป็นรูปสำหรับ Facebook ไม่ได้ กรุณาอัปโหลด JPG/PNG ใหม่"); + } } /**