diff --git a/lib/backend/adapter/zai_is.js b/lib/backend/adapter/zai_is.js index 56fcad8..42f6f73 100644 --- a/lib/backend/adapter/zai_is.js +++ b/lib/backend/adapter/zai_is.js @@ -335,14 +335,14 @@ async function generateImage(context, prompt, imgPaths, modelId, meta = {}) { return { error: '解析生成响应失败' }; } - // 8. 等待并捕获图片下载响应 (zai.is/media/ GET 链接) - logger.debug('适配器', '已获取结果,正在等待图片...', meta); + // 8. 等待 chat/completed 响应,从中提取图片链接 + logger.debug('适配器', '正在等待完成响应...', meta); - let imageResponse; + let completedResponse; try { - imageResponse = await waitApiResponse(page, { - urlMatch: 'zai.is/media/', - method: 'GET', + completedResponse = await waitApiResponse(page, { + urlMatch: 'chat/completed', + method: 'POST', timeout: 120000, meta }); @@ -350,17 +350,47 @@ async function generateImage(context, prompt, imgPaths, modelId, meta = {}) { const pageError = normalizePageError(e, meta); if (pageError) { if (e.name === 'TimeoutError') { - return { error: '已获取结果, 但图片下载时超时 (120秒)' }; + return { error: '等待完成响应超时 (120秒)' }; } return pageError; } throw e; } - // 下载图片 - const imageUrl = imageResponse.url(); - logger.info('适配器', '已获取结果,正在下载图片...', meta); + // 解析 chat/completed 响应 + let completedBody; + try { + completedBody = await completedResponse.json(); + } catch (e) { + logger.error('适配器', '解析 chat/completed 响应失败', { ...meta, error: e.message }); + return { error: '解析完成响应失败' }; + } + // 在 messages 数组中查找匹配的消息 (id 与响应体的 id 相同) + const targetMessage = (completedBody.messages || []).find(msg => msg.id === completedBody.id); + if (!targetMessage) { + logger.error('适配器', `未找到匹配的消息`, meta); + return { error: '未找到匹配的消息' }; + } + + // 检查 content + const content = targetMessage.content; + if (!content || content.trim() === '') { + logger.warn('适配器', '回复内容为空可能触发违规/限流', meta); + return { error: '回复内容为空可能触发违规/限流' }; + } + + // 从 content 中提取图片链接 (格式: ![image](https://zai.is/media/xxx.jpg)) + const imageUrlMatch = content.match(/!\[.*?\]\((https:\/\/zai\.is\/media\/[^)]+)\)/); + if (!imageUrlMatch || !imageUrlMatch[1]) { + logger.warn('适配器', '回复中未找到图片链接', meta); + return { error: '回复中未找到图片链接' }; + } + + const imageUrl = imageUrlMatch[1]; + logger.info('适配器', `已提取图片链接: ${imageUrl}`, meta); + + // 下载图片 const downloadResult = await downloadImage(imageUrl, config); if (downloadResult.error) { return downloadResult;