Diagnose image worker failures and normalize Facebook uploads

This commit is contained in:
Loma Bot Ops
2026-11-23 22:27:53 +07:00
parent 4429e6541b
commit df20406cf5
3 changed files with 36 additions and 3 deletions
+9 -1
View File
@@ -117,7 +117,15 @@ const server = createServer(async (req, res) => {
} catch (error) { } catch (error) {
console.error("[ai-worker] job failed", error); console.error("[ai-worker] job failed", error);
if (error?.usageLimit) reply(429, { error: "Codex usage limit reached" }); 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 { } finally {
if (dir) await rm(dir, { recursive: true, force: true }); if (dir) await rm(dir, { recursive: true, force: true });
busy = false; busy = false;
+13 -1
View File
@@ -20,7 +20,19 @@ export async function runAutomation(body: Record<string, unknown>, options: { wa
throw new UserFacingError("Worker กำลังทำงานอื่น ลองสั่งสร้างใหม่เมื่อพร้อม"); throw new UserFacingError("Worker กำลังทำงานอื่น ลองสั่งสร้างใหม่เมื่อพร้อม");
} }
if (response.status === 429) throw new UserFacingError("โควตา AI ของบัญชีที่ใช้สร้างเนื้อหาเต็มชั่วคราว ลองใหม่อีกครั้งภายหลัง"); 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<string, string> = {
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(); return await response.json();
} }
throw new Error("Worker did not accept the job"); throw new Error("Worker did not accept the job");
+14 -1
View File
@@ -1,5 +1,6 @@
import { bareId, graphDelete, graphGet, graphGetAll, graphPostForm, graphPostJson, UserFacingError } from "./client"; import { bareId, graphDelete, graphGet, graphGetAll, graphPostForm, graphPostJson, UserFacingError } from "./client";
import type { PageCredentials } from "./tokens"; import type { PageCredentials } from "./tokens";
import sharp from "sharp";
/** /**
* Facebook rejects a scheduled_publish_time under 10 minutes or more than ~30 * 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) { if (bytes.byteLength > MAX_FACEBOOK_IMAGE_BYTES) {
throw new UserFacingError("ไฟล์รูปใหญ่เกิน 8MB กรุณาอัปโหลดรูปที่เล็กลง"); 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 ใหม่");
}
} }
/** /**