fix: 修复 gemini-3-pro-image-preview 模型触发500的BUG (closes #1)

This commit is contained in:
foxhui
2025-11-25 19:26:30 +08:00
parent beb2872b0d
commit 4bebdd4d3c
3 changed files with 35 additions and 20 deletions
+6
View File
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.1.1] - 2024-11-24
### Fixed
- **指定模型**
- 修复因错误的UUID映射导致的 gemini-3-pro-image-preview 模型触发HTTP500的 BUG
## [1.1.0] - 2024-11-24 ## [1.1.0] - 2024-11-24
+28 -19
View File
@@ -405,6 +405,7 @@ async function initBrowser(config) {
async function generateImage(context, prompt, imgPaths, modelId) { async function generateImage(context, prompt, imgPaths, modelId) {
const { page, client, width, height } = context; const { page, client, width, height } = context;
const textareaSelector = 'textarea'; const textareaSelector = 'textarea';
let requestHandler = null;
try { try {
// 1. 强制开启新会话 (通过URL跳转) // 1. 强制开启新会话 (通过URL跳转)
@@ -427,33 +428,34 @@ async function generateImage(context, prompt, imgPaths, modelId) {
await humanType(page, textareaSelector, prompt); await humanType(page, textareaSelector, prompt);
await sleep(800, 1500); await sleep(800, 1500);
// --- 注入 Fetch 拦截器 --- // --- 注入 Fetch 拦截器 (Puppeteer Request Interception) ---
if (modelId) { if (modelId) {
await page.evaluate((targetModelId) => { await page.setRequestInterception(true);
const originalFetch = window.fetch; requestHandler = async (request) => {
window.fetch = async (...args) => { const url = request.url();
let [resource, config] = args; if (url.includes('/nextjs-api/stream/') && request.method() === 'POST') {
// 简单判断 URL try {
const url = resource instanceof Request ? resource.url : resource.toString(); const postData = request.postData();
if (postData) {
if (url.includes('/nextjs-api/stream/') && config && config.method === 'POST' && config.body) { const data = JSON.parse(postData);
try {
const data = JSON.parse(config.body);
console.log(`[Browser] 正在拦截请求。原始 modelAId: ${data.modelAId}`); console.log(`[Browser] 正在拦截请求。原始 modelAId: ${data.modelAId}`);
// 修改 modelAId // 修改 modelAId
data.modelAId = targetModelId; data.modelAId = modelId;
config.body = JSON.stringify(data); const newPostData = JSON.stringify(data);
console.log(`[Browser] 请求已修改。新 modelAId: ${data.modelAId}`); console.log(`[Browser] 请求已修改。新 modelAId: ${data.modelAId}`);
} catch (e) { await request.continue({ postData: newPostData });
console.error('[Browser] 拦截失败:', e); return;
} }
} catch (e) {
console.error('[Browser] 拦截修改失败:', e);
} }
return originalFetch.apply(window, args); }
}; await request.continue();
}, modelId); };
console.log(`>>> [Test] 已注入 Fetch 拦截器,目标模型: ${modelId}`); page.on('request', requestHandler);
console.log(`>>> [Test] 已启用请求拦截,目标模型: ${modelId}`);
} }
// 4. 发送 // 4. 发送
@@ -520,6 +522,13 @@ async function generateImage(context, prompt, imgPaths, modelId) {
} catch (err) { } catch (err) {
console.error('>>> [Error] 生成任务失败:', err.message); console.error('>>> [Error] 生成任务失败:', err.message);
return { error: err.message }; return { error: err.message };
} finally {
if (requestHandler) {
page.off('request', requestHandler);
try {
await page.setRequestInterception(false);
} catch (e) { }
}
} }
} }
+1 -1
View File
@@ -1,5 +1,5 @@
export const MODEL_MAPPING = { export const MODEL_MAPPING = {
"gemini-3-pro-image-preview": "019aa208-5c19-7162-ae3b-0a9ddbble16a", "gemini-3-pro-image-preview": "019aa208-5c19-7162-ae3b-0a9ddbb1e16a",
"seedream-4-high-res-fal": "32974d8d-333c-4d2e-abf3-f258c0ac1310", "seedream-4-high-res-fal": "32974d8d-333c-4d2e-abf3-f258c0ac1310",
"hunyuan-image-3.0": "7766a45c-1b6b-4fb8-9823-2557291e1ddd", "hunyuan-image-3.0": "7766a45c-1b6b-4fb8-9823-2557291e1ddd",
"gemini-2.5-flash-image-preview": "0199ef2a-583f-7088-b704-b75fd169401d", "gemini-2.5-flash-image-preview": "0199ef2a-583f-7088-b704-b75fd169401d",