Skip to content

Conversation

@sdsdsdff
Copy link

@sdsdsdff sdsdsdff commented Feb 2, 2026

背景 / 问题

在图像生成场景里,偶发会返回类似 /images/p_Lw 的图片链接;p_Lw 解码后等价于路径 /,客户端拉取必然 404,表现为“黑色小占位图/坏图”。

从 Worker 日志可观察到大量 GET /images/p_Lw -> 404,且这些请求并非上游限流,而是本地把空/占位的 generatedImageUrls 当成了最终图片 URL 提前结束导致。

原因

上游(Grok imagine)是 NDJSON 流式输出,某些中间帧会先出现 modelResponse.generatedImageUrls,但其中可能包含空字符串/占位值(如 """/")。
旧逻辑只要看到 generatedImageUrls 是数组就直接生成图片链接并结束,导致把占位值编码成 p_Lw 返回给客户端。

修改

  • 过滤/规范化 generatedImageUrls:忽略空字符串/纯空白 URL。
  • 非流式解析:继续扫描 NDJSON,直到拿到至少 1 个有效 URL 才结束,避免提前返回占位链接。
  • 流式解析:同样使用过滤后的 URL 列表生成图片链接。
  • 另外:.gitignore 增加 .ace-tool/,避免本地索引文件误入仓库。

影响

  • 消除 /images/p_Lw 这类坏链接,客户端不再出现黑色占位图。
  • 不改变正常情况下的图片返回方式;上游 403/429 等风控/限流问题不在本 PR 解决范围内。
    This pull request improves the way generated image URLs are handled and normalized in the src/grok/processor.ts file. 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:

  • Added a new helper function normalizeGeneratedAssetUrls to filter and clean up the generatedImageUrls array, ensuring only valid, trimmed string URLs are returned.
  • Updated both createOpenAiStreamFromGrokNdjson and parseOpenAiFromGrokNdjson to use normalizeGeneratedAssetUrls instead of directly accessing or checking the type of generatedImageUrls. This streamlines URL processing and avoids duplicate logic. [1] [2]
  • Enhanced the image generation response handling in parseOpenAiFromGrokNdjson to keep scanning for usable URLs, preventing the return of broken image links when only empty or placeholder URLs are present.

Copilot AI review requested due to automatic review settings February 2, 2026 13:37
@sdsdsdff sdsdsdff changed the title Fix image placeholder URLs and ignore ace-tool index Fix image placeholder URLs Feb 2, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 normalizeGeneratedAssetUrls helper 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 .gitignore to 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.

Comment on lines +74 to +84
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;
}
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant