[PR #330] [MERGED] feat(server): prompt-overrides registry for image prompts #4564

Closed
opened 2026-10-22 04:08:46 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/330
Author: @RaynoldVanHeyningen
Created: 4/30/2026
Status: ✅ Merged
Merged: 5/3/2026
Merged by: @SpicyMarinara

Base: main ← Head: feat/prompt-overrides


📝 Commits (2)

  • 9d55751 feat(server): prompt-overrides registry, routes, sprite conversion
  • 735cb41 feat(server): register 7 more flat-template prompt-override keys

📊 Changes

18 files changed (+878 additions, -112 deletions)

View changed files

📝 packages/server/src/db/migrate.ts (+6 -0)
📝 packages/server/src/db/schema/index.ts (+1 -0)
➕ packages/server/src/db/schema/prompt-overrides.ts (+14 -0)
📝 packages/server/src/routes/game.routes.ts (+7 -0)
📝 packages/server/src/routes/generate.routes.ts (+15 -18)
📝 packages/server/src/routes/index.ts (+2 -0)
➕ packages/server/src/routes/prompt-overrides.routes.ts (+115 -0)
📝 packages/server/src/routes/sprites.routes.ts (+36 -54)
📝 packages/server/src/services/game/game-asset-generation.ts (+50 -40)
➕ packages/server/src/services/prompt-overrides/index.ts (+31 -0)
➕ packages/server/src/services/prompt-overrides/load-prompt.ts (+31 -0)
➕ packages/server/src/services/prompt-overrides/registry.ts (+62 -0)
➕ packages/server/src/services/prompt-overrides/registry/conversation.ts (+58 -0)
➕ packages/server/src/services/prompt-overrides/registry/game-assets.ts (+148 -0)
➕ packages/server/src/services/prompt-overrides/registry/sprites.ts (+164 -0)
➕ packages/server/src/services/prompt-overrides/template.ts (+51 -0)
➕ packages/server/src/services/prompt-overrides/types.ts (+21 -0)
➕ packages/server/src/services/storage/prompt-overrides.storage.ts (+66 -0)

📄 Description

Linked issue

Closes #329

Why this change

The image-generation prompts (sprite sheets, NPC portraits, scene CGs, the
selfie meta-prompt, location backgrounds) are currently hardcoded TS strings
inside route handlers and asset-generation services. Users have no way to tune
them without forking the source.

This is observably user-facing. The multi-cell sprite-sheet prompt in
particular dedicates most of its budget to grid bookkeeping (EXACTLY N cells, strict NxM grid, no extra rows, no merged cells) and a
hardcoded "solid white studio background", which on many image models steers
the result toward a flat, low-contrast aesthetic. The prompt has no field
where users can inject style or quality keywords — the character's
appearance field is the only lever, and it is already crowded with physical
descriptors. (Sprite generation runs from the character editor outside any
game session, so it deliberately has no concept of a game-level art style —
this is a separate gap from in-game asset coherence.)

This PR introduces a generic registry that lets user-supplied templates
replace each registered prompt builder, with default behavior unchanged at
every call site.

What changed

New prompt_overrides table. Idempotent CREATE in runMigrations(); no
separate pnpm db:push needed.

New services/prompt-overrides/ module:

  • Registry: each entry declares its key, the variables it interpolates
    (${name} markers), a canonical default builder, and a realistic example
    context used for previews.
  • Template engine: simple ${name} substitution, deliberately limited to
    flat variable expansion (no conditionals — see "Not included" below).
  • loadPrompt(storage, def, ctx) helper: returns the user's rendered
    template if present and enabled, otherwise the canonical default. Falls
    back to the default on any error so prompt rendering can never break the
    surrounding feature.

New /api/prompt-overrides routes:

Method Path Description
GET / List registered keys + override status + var schemas
GET /:key Current override for a key
GET /:key/default Canonical default rendered with example context
PUT /:key Save { template, enabled } — validates ${vars}
DELETE /:key Remove override row (revert to default)
POST /:key/preview Render an arbitrary template with the example context

PUT and /preview reject templates that reference variables not declared by
the registered key (HTTP 400 with unknownVariables and declaredVariables
in the body).

Eight prompts registered and converted:

Key Replaces
sprites.expressionSheet Multi-cell expression sheet (sprites.routes.ts)
sprites.singlePortrait Single head/shoulders sprite + GPT-Image fallback per-expr
sprites.singleFullBody Single full-body sprite
sprites.fullBodySheet Multi-cell full-body pose sheet
game.npcPortrait buildNpcPortraitPrompt in game-asset-generation.ts
game.background Background prompt in generateBackground
game.sceneIllustration VN POV CG prompt in generateSceneIllustration
conversation.selfie Selfie meta-prompt in generate.routes.ts

Default behavior at every call site is byte-for-byte identical.

Conditional logic (e.g. "include this line only when X is non-empty", "use
this rule for non-human NPCs", "add the blank line before the tags block")
stays in TypeScript at the call site; the result is exposed as a pre-computed
string variable (appearanceLine, nonHumanRule, selfieTagsBlock, etc.)
that the user can rearrange in their template but cannot re-conditionalize.
This keeps the engine free of conditional constructs.

Not included by design.

  • Complex prompts with conditional sections (gm.systemPrompt,
    gm.formatReminder, scene.analyzerSystem, party agent, session recap,
    etc.) are deliberately out of scope. The flat ${var} engine cannot
    represent ~15 conditional blocks per prompt without re-implementing parts
    of Mustache. Either splitting each into sub-prompts or extending the
    engine with a minimal {{#if name}}…{{/if}} construct is a separate
    design decision worth its own PR.
  • No UI. The API is consumable from any client; a userscript-style
    extension can add an in-app editor without core changes.

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

This PR is server-only — no UI surface yet. A human reviewer should manually
verify:

  • Migration runs cleanly on an existing database. Boot the server
    against an existing marinara-engine.db; confirm the prompt_overrides
    table is created without touching other tables (e.g.
    sqlite3 packages/server/data/marinara-engine.db ".schema prompt_overrides").
  • Migration is idempotent. Restart the server multiple times; confirm
    no errors and table state stays consistent.
  • Default sprite-sheet behavior is unchanged. With no overrides set,
    generate a 2×3 sprite expression sheet for a character. The image and the
    prompt sent to the image model should be the same as before this PR. With
    LOG_LEVEL=debug you can see the prompt in the server log.
  • Default NPC portrait / scene CG / background are unchanged. Same
    check in Game Mode with image generation enabled.
  • Default selfie is unchanged. In Conversation mode, trigger a selfie.
    The meta-prompt sent to the chat LLM should look the same as before.
  • Override path actually overrides.
    curl -X PUT http://localhost:7860/api/prompt-overrides/sprites.expressionSheet \
      -H 'Content-Type: application/json' \
      -d '{"template":"NEW STYLE: ${expressionCount} cells of ${appearance}","enabled":true}'
    
    Then generate a sprite sheet — the model should receive the new prompt
    (verify with LOG_LEVEL=debug).
  • Validation rejects unknown variables. Same PUT with ${doesNotExist}
    should return HTTP 400 with unknownVariables: ["doesNotExist"].
  • DELETE reverts to default. After overriding, DELETE /:key and
    re-generate; behavior matches default again.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

A CHANGELOG.md entry could be added — leaving that to maintainer
preference. The feature is undocumented in docs/ because it has no in-app
surface in this PR; a follow-up PR (or a community extension) can add
user-facing docs once the API is stable.

UI evidence (if applicable)

N/A — server-only. Routes are exercisable via curl; sample requests are in
the Manual verification notes above.

Summary by CodeRabbit

  • New Features

    • Added a prompt override system allowing users to customize templates for asset generation (sprites, backgrounds, portraits, and scenes).
    • New API endpoints to manage, preview, and validate custom prompts.
    • Template system with variable substitution and validation to ensure correct template usage.
  • Chores

    • Updated database schema and internal asset generation services to support custom prompt overrides.

🔄 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/330 **Author:** [@RaynoldVanHeyningen](https://github.com/RaynoldVanHeyningen) **Created:** 4/30/2026 **Status:** ✅ Merged **Merged:** 5/3/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `main` ← **Head:** `feat/prompt-overrides` --- ### 📝 Commits (2) - [`9d55751`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9d55751f9297a1435e35aeb3276c3ded30b3e7b9) feat(server): prompt-overrides registry, routes, sprite conversion - [`735cb41`](https://github.com/Pasta-Devs/Marinara-Engine/commit/735cb413a8ed5a241a05c676cf060734710fbfe1) feat(server): register 7 more flat-template prompt-override keys ### 📊 Changes **18 files changed** (+878 additions, -112 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/db/migrate.ts` (+6 -0) 📝 `packages/server/src/db/schema/index.ts` (+1 -0) ➕ `packages/server/src/db/schema/prompt-overrides.ts` (+14 -0) 📝 `packages/server/src/routes/game.routes.ts` (+7 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+15 -18) 📝 `packages/server/src/routes/index.ts` (+2 -0) ➕ `packages/server/src/routes/prompt-overrides.routes.ts` (+115 -0) 📝 `packages/server/src/routes/sprites.routes.ts` (+36 -54) 📝 `packages/server/src/services/game/game-asset-generation.ts` (+50 -40) ➕ `packages/server/src/services/prompt-overrides/index.ts` (+31 -0) ➕ `packages/server/src/services/prompt-overrides/load-prompt.ts` (+31 -0) ➕ `packages/server/src/services/prompt-overrides/registry.ts` (+62 -0) ➕ `packages/server/src/services/prompt-overrides/registry/conversation.ts` (+58 -0) ➕ `packages/server/src/services/prompt-overrides/registry/game-assets.ts` (+148 -0) ➕ `packages/server/src/services/prompt-overrides/registry/sprites.ts` (+164 -0) ➕ `packages/server/src/services/prompt-overrides/template.ts` (+51 -0) ➕ `packages/server/src/services/prompt-overrides/types.ts` (+21 -0) ➕ `packages/server/src/services/storage/prompt-overrides.storage.ts` (+66 -0) </details> ### 📄 Description ## Linked issue Closes #329 ## Why this change The image-generation prompts (sprite sheets, NPC portraits, scene CGs, the selfie meta-prompt, location backgrounds) are currently hardcoded TS strings inside route handlers and asset-generation services. Users have no way to tune them without forking the source. This is observably user-facing. The multi-cell sprite-sheet prompt in particular dedicates most of its budget to grid bookkeeping (`EXACTLY N cells`, `strict NxM grid`, `no extra rows`, `no merged cells`) and a hardcoded "solid white studio background", which on many image models steers the result toward a flat, low-contrast aesthetic. The prompt has no field where users can inject style or quality keywords — the character's `appearance` field is the only lever, and it is already crowded with physical descriptors. (Sprite generation runs from the character editor outside any game session, so it deliberately has no concept of a game-level art style — this is a separate gap from in-game asset coherence.) This PR introduces a generic registry that lets user-supplied templates replace each registered prompt builder, with default behavior unchanged at every call site. ## What changed **New `prompt_overrides` table.** Idempotent CREATE in `runMigrations()`; no separate `pnpm db:push` needed. **New `services/prompt-overrides/` module:** - Registry: each entry declares its key, the variables it interpolates (`${name}` markers), a canonical default builder, and a realistic example context used for previews. - Template engine: simple `${name}` substitution, deliberately limited to flat variable expansion (no conditionals — see "Not included" below). - `loadPrompt(storage, def, ctx)` helper: returns the user's rendered template if present and enabled, otherwise the canonical default. Falls back to the default on any error so prompt rendering can never break the surrounding feature. **New `/api/prompt-overrides` routes:** | Method | Path | Description | | ------ | --------------------- | ----------------------------------------------------- | | GET | `/` | List registered keys + override status + var schemas | | GET | `/:key` | Current override for a key | | GET | `/:key/default` | Canonical default rendered with example context | | PUT | `/:key` | Save `{ template, enabled }` — validates `${vars}` | | DELETE | `/:key` | Remove override row (revert to default) | | POST | `/:key/preview` | Render an arbitrary template with the example context | PUT and `/preview` reject templates that reference variables not declared by the registered key (HTTP 400 with `unknownVariables` and `declaredVariables` in the body). **Eight prompts registered and converted:** | Key | Replaces | | ------------------------- | ---------------------------------------------------------- | | `sprites.expressionSheet` | Multi-cell expression sheet (`sprites.routes.ts`) | | `sprites.singlePortrait` | Single head/shoulders sprite + GPT-Image fallback per-expr | | `sprites.singleFullBody` | Single full-body sprite | | `sprites.fullBodySheet` | Multi-cell full-body pose sheet | | `game.npcPortrait` | `buildNpcPortraitPrompt` in `game-asset-generation.ts` | | `game.background` | Background prompt in `generateBackground` | | `game.sceneIllustration` | VN POV CG prompt in `generateSceneIllustration` | | `conversation.selfie` | Selfie meta-prompt in `generate.routes.ts` | Default behavior at every call site is byte-for-byte identical. Conditional logic (e.g. "include this line only when X is non-empty", "use this rule for non-human NPCs", "add the blank line before the tags block") stays in TypeScript at the call site; the result is exposed as a pre-computed string variable (`appearanceLine`, `nonHumanRule`, `selfieTagsBlock`, etc.) that the user can rearrange in their template but cannot re-conditionalize. This keeps the engine free of conditional constructs. **Not included by design.** - Complex prompts with conditional sections (`gm.systemPrompt`, `gm.formatReminder`, `scene.analyzerSystem`, party agent, session recap, etc.) are deliberately out of scope. The flat `${var}` engine cannot represent ~15 conditional blocks per prompt without re-implementing parts of Mustache. Either splitting each into sub-prompts or extending the engine with a minimal `{{#if name}}…{{/if}}` construct is a separate design decision worth its own PR. - No UI. The API is consumable from any client; a userscript-style extension can add an in-app editor without core changes. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes This PR is server-only — no UI surface yet. A human reviewer should manually verify: - **Migration runs cleanly on an existing database.** Boot the server against an existing `marinara-engine.db`; confirm the `prompt_overrides` table is created without touching other tables (e.g. `sqlite3 packages/server/data/marinara-engine.db ".schema prompt_overrides"`). - **Migration is idempotent.** Restart the server multiple times; confirm no errors and table state stays consistent. - **Default sprite-sheet behavior is unchanged.** With no overrides set, generate a 2×3 sprite expression sheet for a character. The image and the prompt sent to the image model should be the same as before this PR. With `LOG_LEVEL=debug` you can see the prompt in the server log. - **Default NPC portrait / scene CG / background are unchanged.** Same check in Game Mode with image generation enabled. - **Default selfie is unchanged.** In Conversation mode, trigger a selfie. The meta-prompt sent to the chat LLM should look the same as before. - **Override path actually overrides.** ```bash curl -X PUT http://localhost:7860/api/prompt-overrides/sprites.expressionSheet \ -H 'Content-Type: application/json' \ -d '{"template":"NEW STYLE: ${expressionCount} cells of ${appearance}","enabled":true}' ``` Then generate a sprite sheet — the model should receive the new prompt (verify with `LOG_LEVEL=debug`). - **Validation rejects unknown variables.** Same PUT with `${doesNotExist}` should return HTTP 400 with `unknownVariables: ["doesNotExist"]`. - **DELETE reverts to default.** After overriding, `DELETE /:key` and re-generate; behavior matches default again. ## Docs and release impact - [x] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [x] Version/release files updated (only if this PR includes a version bump) A `CHANGELOG.md` entry could be added — leaving that to maintainer preference. The feature is undocumented in `docs/` because it has no in-app surface in this PR; a follow-up PR (or a community extension) can add user-facing docs once the API is stable. ## UI evidence (if applicable) N/A — server-only. Routes are exercisable via curl; sample requests are in the **Manual verification notes** above. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a prompt override system allowing users to customize templates for asset generation (sprites, backgrounds, portraits, and scenes). * New API endpoints to manage, preview, and validate custom prompts. * Template system with variable substitution and validation to ensure correct template usage. * **Chores** * Updated database schema and internal asset generation services to support custom prompt overrides. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:08:46 +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#4564
No description provided.