[PR #8423] [MERGED] fix(image): decode a data URL in b64_json and type base64 results by their bytes #8591

Closed
opened 2026-11-07 16:45:50 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8423
Author: @SpicyMarinara
Created: 10/28/2026
Status: ✅ Merged
Merged: 10/28/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/xai-image-data-url


📝 Commits (1)

  • b4c17c5 fix(image): decode a data URL in b64_json and type base64 results by their bytes

📊 Changes

2 files changed (+71 additions, -3 deletions)

View changed files

📝 packages/server/src/services/image/image-generation.ts (+14 -3)
➕ scripts/regressions/image-base64-data-url.regression.ts (+57 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #8422

Why this change

  • A Grok (xAI) image with no reference picture fails or saves broken, although xAI made it and billed for it. xAI's generations endpoint returns b64_json as a whole data:image/jpeg;base64,… URL. generateXAI passed that on as the base64 of a PNG, so callers built data:image/png;base64,data:image/jpeg;base64,… and decoded garbage.
  • Found in Otome Hearth: an archetype love interest (no avatar) failed on every Grok connection, while card characters (which send their avatar and use xAI's edits endpoint, answered with a URL) painted fine. The same path serves the Engine's own avatar generation without a source image, and any package painting someone without an avatar.

What changed

  • image-generation.ts: a new imageResultFromBase64Field decodes a data URL in a b64_json-style field with the existing decodeImageDataUrl (as NanoGPT's reader already did), and types bare base64 by its bytes with detectImageMimeType, falling back to PNG only when they match no known format.
  • It replaces the hard-coded image/png in generateXAI, the shared OpenAI-compatible reader (readOpenAIImageResult) and Together AI.
  • New scripts/regressions/image-base64-data-url.regression.ts: a local fake provider answers a data URL, bare JPEG base64 and bare PNG base64 for xAI, OpenAI and Together AI. It fails on the old code (xai: a data URL keeps its type: actual image/png).

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Agent-side checks:

  • pnpm check exited 0. Its one lint warning is in an untouched client file.
  • The new regression passes, and fails with the fix stashed.
  • These neighbouring regressions pass: image-custom-parameters, image-edit-reference, generated-image-format, gpt-image-2-5, fal-image, codex-chatgpt-image and image-dimension-limit.

Please verify by hand:

  • Manually verify, on an xAI image connection (grok-imagine-image-2.0), that an avatar generated with no source image saves and shows as a JPEG.
  • Manually verify an image with a reference picture on the same connection still works (the edits path is unchanged).
  • Manually verify an OpenAI image connection still saves images (PNG bytes stay PNG).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Not a UI change.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/8423 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/28/2026 **Status:** ✅ Merged **Merged:** 10/28/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/xai-image-data-url` --- ### 📝 Commits (1) - [`b4c17c5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b4c17c5fed514af841d4a8bdc21397295e4e1c9e) fix(image): decode a data URL in b64_json and type base64 results by their bytes ### 📊 Changes **2 files changed** (+71 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/image/image-generation.ts` (+14 -3) ➕ `scripts/regressions/image-base64-data-url.regression.ts` (+57 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #8422 ## Why this change - A Grok (xAI) image with no reference picture fails or saves broken, although xAI made it and billed for it. xAI's generations endpoint returns `b64_json` as a whole `data:image/jpeg;base64,…` URL. `generateXAI` passed that on as the base64 of a PNG, so callers built `data:image/png;base64,data:image/jpeg;base64,…` and decoded garbage. - Found in Otome Hearth: an archetype love interest (no avatar) failed on every Grok connection, while card characters (which send their avatar and use xAI's edits endpoint, answered with a URL) painted fine. The same path serves the Engine's own avatar generation without a source image, and any package painting someone without an avatar. ## What changed - `image-generation.ts`: a new `imageResultFromBase64Field` decodes a data URL in a `b64_json`-style field with the existing `decodeImageDataUrl` (as NanoGPT's reader already did), and types bare base64 by its bytes with `detectImageMimeType`, falling back to PNG only when they match no known format. - It replaces the hard-coded `image/png` in `generateXAI`, the shared OpenAI-compatible reader (`readOpenAIImageResult`) and Together AI. - New `scripts/regressions/image-base64-data-url.regression.ts`: a local fake provider answers a data URL, bare JPEG base64 and bare PNG base64 for xAI, OpenAI and Together AI. It fails on the old code (`xai: a data URL keeps its type`: actual `image/png`). ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Agent-side checks: - `pnpm check` exited 0. Its one lint warning is in an untouched client file. - The new regression passes, and fails with the fix stashed. - These neighbouring regressions pass: `image-custom-parameters`, `image-edit-reference`, `generated-image-format`, `gpt-image-2-5`, `fal-image`, `codex-chatgpt-image` and `image-dimension-limit`. Please verify by hand: - [ ] Manually verify, on an xAI image connection (`grok-imagine-image-2.0`), that an avatar generated with no source image saves and shows as a JPEG. - [ ] Manually verify an image with a reference picture on the same connection still works (the edits path is unchanged). - [ ] Manually verify an OpenAI image connection still saves images (PNG bytes stay PNG). ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Not a UI change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 16:45:50 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#8591
No description provided.