Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes an issue where image generation requests occasionally return broken placeholder image URLs like /images/p_Lw (which decodes to /) causing 404 errors. The root cause was that intermediate NDJSON frames from Grok's streaming response contained empty or placeholder URLs that were being encoded and returned prematurely.
Changes:
- Added
normalizeGeneratedAssetUrlshelper function to filter out invalid URLs from Grok's image generation responses - Modified both streaming and non-streaming parsers to use the normalized URLs and continue scanning until valid URLs are found
- Added
.ace-tool/directory to.gitignoreto prevent local tool index files from being committed
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
.gitignore |
Added .ace-tool/ directory to ignore list for OpenCode/ace-tool index files |
src/grok/processor.ts |
Added URL normalization function and updated image generation logic to filter placeholder URLs and continue scanning NDJSON frames until valid image URLs are found |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function normalizeGeneratedAssetUrls(input: unknown): string[] { | ||
| if (!Array.isArray(input)) return []; | ||
| const out: string[] = []; | ||
| for (const u of input) { | ||
| if (typeof u !== "string") continue; | ||
| const trimmed = u.trim(); | ||
| if (!trimmed) continue; | ||
| out.push(trimmed); | ||
| } | ||
| return out; | ||
| } |
There was a problem hiding this comment.
The normalizeGeneratedAssetUrls function filters empty strings but doesn't filter the "/" placeholder value mentioned in the PR description. According to the description, both "" and "/" are possible placeholder values from Grok, but this function only filters empty/whitespace strings. A URL of "/" would pass through as valid and still get encoded as p_Lw, which is the exact issue this PR aims to fix.
Consider adding a check to filter out "/" (and possibly other path-only placeholders like "/images"):
if (!trimmed || trimmed === "/") continue;
背景 / 问题
在图像生成场景里,偶发会返回类似
/images/p_Lw的图片链接;p_Lw解码后等价于路径/,客户端拉取必然 404,表现为“黑色小占位图/坏图”。从 Worker 日志可观察到大量
GET /images/p_Lw -> 404,且这些请求并非上游限流,而是本地把空/占位的generatedImageUrls当成了最终图片 URL 提前结束导致。原因
上游(Grok imagine)是 NDJSON 流式输出,某些中间帧会先出现
modelResponse.generatedImageUrls,但其中可能包含空字符串/占位值(如""或"/")。旧逻辑只要看到
generatedImageUrls是数组就直接生成图片链接并结束,导致把占位值编码成p_Lw返回给客户端。修改
generatedImageUrls:忽略空字符串/纯空白 URL。.gitignore增加.ace-tool/,避免本地索引文件误入仓库。影响
/images/p_Lw这类坏链接,客户端不再出现黑色占位图。This pull request improves the way generated image URLs are handled and normalized in the
src/grok/processor.tsfile. The changes ensure that only valid, non-empty string URLs are processed, which prevents issues with empty or malformed image links in downstream logic.Improvements to image URL handling:
normalizeGeneratedAssetUrlsto filter and clean up thegeneratedImageUrlsarray, ensuring only valid, trimmed string URLs are returned.createOpenAiStreamFromGrokNdjsonandparseOpenAiFromGrokNdjsonto usenormalizeGeneratedAssetUrlsinstead of directly accessing or checking the type ofgeneratedImageUrls. This streamlines URL processing and avoids duplicate logic. [1] [2]parseOpenAiFromGrokNdjsonto keep scanning for usable URLs, preventing the return of broken image links when only empty or placeholder URLs are present.