diff --git a/.env.example b/.env.example index 4ddd220..3797bf7 100644 --- a/.env.example +++ b/.env.example @@ -33,9 +33,9 @@ AI_AUTOMATION_HOST=127.0.0.1 AI_AUTOMATION_PORT=4318 CHATGPT_PROFILE_DIR=.data/chatgpt-profile CHATGPT_BROWSER_CHANNEL=chrome -# Windows: control an already-open normal Chrome via desktop accessibility. -# Other platforms: playwright uses the dedicated profile above. -CHATGPT_DRIVER=windows +# Playwright controls a dedicated Chrome profile without foreground keyboard access. +# Run node scripts/chatgpt-login.mjs interactively once, then close that browser. +CHATGPT_DRIVER=playwright CHATGPT_SURFACE=extension CHATGPT_IMAGE_EXPORT=download # Durable full-post queue. Same secret in the web app and queue polling worker. diff --git a/scripts/ai-automation-worker.mjs b/scripts/ai-automation-worker.mjs index cd6b4b3..f247d59 100644 --- a/scripts/ai-automation-worker.mjs +++ b/scripts/ai-automation-worker.mjs @@ -67,17 +67,24 @@ async function image(body, dir) { const page = await context.newPage(); page.setDefaultTimeout(20_000); await page.goto("https://chatgpt.com/", { waitUntil: "domcontentloaded" }); + if (await page.title() === "Just a moment..." || page.url().includes("__cf_chl_")) { + throw new Error("ChatGPT browser verification required: run node scripts/chatgpt-login.mjs and complete verification"); + } // Check login before waiting for the composer: the logged-out page has a different layout. const login = page.getByRole("button", { name: /^(Log in|เข้าสู่ระบบ)$/i }).first(); if (await page.getByTestId("login-button").isVisible() || await login.isVisible()) { throw new Error("ChatGPT login required: run node scripts/chatgpt-login.mjs"); } const editor = page.locator('#prompt-textarea'); - await editor.waitFor({ state: "visible" }); + await editor.waitFor({ state: "visible", timeout: 30_000 }).catch(() => { + throw new Error("ChatGPT composer unavailable: verify the dedicated browser profile"); + }); await editor.fill(`Create an image now. Return the generated image, not a prompt. Aspect ratio: ${body.size === "landscape" ? "3:2" : "1:1"}.\n${body.prompt}`); await editor.press("Enter"); const downloadButton = page.locator('[data-message-author-role="assistant"]').getByRole("button", { name: /download|ดาวน์โหลด/i }).last(); - await downloadButton.waitFor({ state: "visible", timeout: 200_000 }); + await downloadButton.waitFor({ state: "visible", timeout: 200_000 }).catch(() => { + throw new Error("ChatGPT image generation or download unavailable"); + }); const downloadEvent = page.waitForEvent("download", { timeout: 20_000 }); await downloadButton.click(); const download = await downloadEvent; @@ -120,7 +127,7 @@ const server = createServer(async (req, res) => { 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" + : /login required|verification 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"; diff --git a/scripts/chatgpt-login.mjs b/scripts/chatgpt-login.mjs index edd9f28..22ca00d 100644 --- a/scripts/chatgpt-login.mjs +++ b/scripts/chatgpt-login.mjs @@ -8,5 +8,5 @@ const context = await chromium.launchPersistentContext(path.resolve(process.env. }); const page = await context.newPage(); await page.goto("https://chatgpt.com/"); -console.log("Log in to ChatGPT, then close the browser. Start the worker afterward."); +console.log("Complete browser verification and log in to ChatGPT in this dedicated Chrome window. Confirm that the chat composer is visible, then close this window. Start the worker afterward."); await new Promise((resolve) => context.once("close", resolve));