[PR #1184] [CLOSED] feat: native AI-assisted Magic Rewrite for character, persona, and lorebook fields #4965

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1184
Author: @kevin-ho
Created: 5/25/2026
Status: ❌ Closed

Base: main ← Head: magic-rewrite


📝 Commits (2)

  • ecd2816 feat: native AI-assisted Magic Rewrite for character, persona, and lorebook fields (#1183)
  • b665962 fix: address CodeRabbit review feedback

📊 Changes

11 files changed (+587 additions, -41 deletions)

View changed files

📝 packages/client/src/components/connections/ConnectionEditor.tsx (+31 -0)
📝 packages/client/src/components/lorebooks/LorebookFormFields.tsx (+78 -24)
📝 packages/client/src/components/ui/ExpandedTextarea.tsx (+84 -17)
➕ packages/client/src/components/ui/MagicRewritePanel.tsx (+245 -0)
📝 packages/server/src/db/migrate.ts (+5 -0)
📝 packages/server/src/db/schema/connections.ts (+2 -0)
📝 packages/server/src/routes/index.ts (+2 -0)
➕ packages/server/src/routes/magic-rewrite.routes.ts (+101 -0)
📝 packages/server/src/services/storage/connections.storage.ts (+36 -0)
📝 packages/shared/src/schemas/connection.schema.ts (+1 -0)
📝 packages/shared/src/types/connection.ts (+2 -0)

📄 Description

Linked issue

Closes #1183

Why this change

Writing and maintaining long-form text entries — character descriptions, persona definitions, lorebook entries, system prompts — is one of the more tedious parts of setting up a roleplay session. Other RP platforms offer AI-assisted rewriting that lets users iteratively refine these fields with natural language instructions: "add more detail about their childhood," "convert to XML format," "make this more token-efficient."

Currently Marinara has no built-in mechanism for this. A Marinara app extension (magic-rewrite-extension) exists as a proof of concept, but it requires users to manually enter API keys because it cannot access the LLM connections already configured in the app. This PR ships the feature natively, reusing Marinara's existing connection infrastructure so users can leverage their configured endpoints with no additional credential management.

What changed

  • packages/shared/src/types/connection.ts + packages/shared/src/schemas/connection.schema.ts: add defaultForRewrite boolean field to the shared connection type and Zod create schema.
  • packages/server/src/db/schema/connections.ts + packages/server/src/db/migrate.ts: add default_for_rewrite text column to the api_connections table with a migration entry.
  • packages/server/src/services/storage/connections.storage.ts: add getDefaultForRewrite() method and mutual-exclusion enforcement in create/update — only one non-image-generation connection can hold the rewrite default at a time.
  • packages/server/src/routes/magic-rewrite.routes.ts: new POST /api/magic-rewrite/generate route. Accepts text, instruction, and optional context (character, lorebook, chat history). Resolves connection via rewrite-specific → default chat → agent default chain. Uses createLLMProvider() and chatComplete() with a focused system prompt that returns only rewritten text.
  • packages/client/src/components/ui/MagicRewritePanel.tsx: new panel component with instruction textarea, context selectors (character card, lorebook entries, chat history with configurable message count up to 250), generate button with loading spinner, and a word-level diff preview showing before/after with changed spans highlighted. Supports persistent instruction recall via localStorage.
  • packages/client/src/components/lorebooks/LorebookFormFields.tsx: adds ✨ Rewrite button in the lorebook description editor modal (bottom-right, left of Done). Clicking it enters rewrite mode with Back/Apply controls.
  • packages/client/src/components/ui/ExpandedTextarea.tsx: adds ✨ Rewrite button in the shared full-screen editor header. This automatically makes the feature available in the character editor, persona editor, and chat settings drawer — all of which use this shared component.
  • packages/client/src/components/connections/ConnectionEditor.tsx: adds "Default for Rewrite" toggle beneath the existing "Default for Agents" toggle, with tooltip explaining the fallback behavior. Hidden for image-generation provider connections.

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

  • Lorebook description editor: ✨ Rewrite button appears, generates rewrite using the default chat connection, diff preview renders correctly with line breaks in both before/after panes, Apply saves rewritten text back to the editor.
  • Character editor: ✨ Rewrite button appears in the expand-textarea header for all text fields.
  • Persona editor: ✨ Rewrite button appears in the expand-textarea header for all text fields.
  • Connection settings: "Default for Rewrite" toggle works, mutual exclusion enforced (only one non-image connection can hold the flag).
  • Fallback chain: with no rewrite-specific connection set, correctly falls through to the default chat connection, then agent default.
  • Chat history context: messages slider configurable up to 250, context included in the generation request.
  • Instruction persistence: rewrite instructions saved to and recalled from localStorage.
  • Escape key does not close the modal while Magic Rewrite mode is active.

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)

UI evidence (if applicable)

Screenshot 2026-05-24 222848
Screenshot 2026-05-24 223102 ---- Screenshot 2026-05-24 223043

🔄 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/1184 **Author:** [@kevin-ho](https://github.com/kevin-ho) **Created:** 5/25/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `magic-rewrite` --- ### 📝 Commits (2) - [`ecd2816`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ecd281611e1f50ecb3a88bbf2158bd4dbd3500b6) feat: native AI-assisted Magic Rewrite for character, persona, and lorebook fields (#1183) - [`b665962`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b665962401f22d775e5797528c09799016a516f6) fix: address CodeRabbit review feedback ### 📊 Changes **11 files changed** (+587 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/connections/ConnectionEditor.tsx` (+31 -0) 📝 `packages/client/src/components/lorebooks/LorebookFormFields.tsx` (+78 -24) 📝 `packages/client/src/components/ui/ExpandedTextarea.tsx` (+84 -17) ➕ `packages/client/src/components/ui/MagicRewritePanel.tsx` (+245 -0) 📝 `packages/server/src/db/migrate.ts` (+5 -0) 📝 `packages/server/src/db/schema/connections.ts` (+2 -0) 📝 `packages/server/src/routes/index.ts` (+2 -0) ➕ `packages/server/src/routes/magic-rewrite.routes.ts` (+101 -0) 📝 `packages/server/src/services/storage/connections.storage.ts` (+36 -0) 📝 `packages/shared/src/schemas/connection.schema.ts` (+1 -0) 📝 `packages/shared/src/types/connection.ts` (+2 -0) </details> ### 📄 Description <!-- Target branch: `staging`. The default base in this UI is `main` (release branch). --> <!-- Change the base to `staging` before submitting unless this is a maintainer-approved mainline change. --> <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #1183 ## Why this change Writing and maintaining long-form text entries — character descriptions, persona definitions, lorebook entries, system prompts — is one of the more tedious parts of setting up a roleplay session. Other RP platforms offer AI-assisted rewriting that lets users iteratively refine these fields with natural language instructions: "add more detail about their childhood," "convert to XML format," "make this more token-efficient." Currently Marinara has no built-in mechanism for this. A Marinara app extension (`magic-rewrite-extension`) exists as a proof of concept, but it requires users to manually enter API keys because it cannot access the LLM connections already configured in the app. This PR ships the feature natively, reusing Marinara's existing connection infrastructure so users can leverage their configured endpoints with no additional credential management. ## What changed - **`packages/shared/src/types/connection.ts`** + **`packages/shared/src/schemas/connection.schema.ts`**: add `defaultForRewrite` boolean field to the shared connection type and Zod create schema. - **`packages/server/src/db/schema/connections.ts`** + **`packages/server/src/db/migrate.ts`**: add `default_for_rewrite` text column to the `api_connections` table with a migration entry. - **`packages/server/src/services/storage/connections.storage.ts`**: add `getDefaultForRewrite()` method and mutual-exclusion enforcement in create/update — only one non-image-generation connection can hold the rewrite default at a time. - **`packages/server/src/routes/magic-rewrite.routes.ts`**: new `POST /api/magic-rewrite/generate` route. Accepts `text`, `instruction`, and optional `context` (character, lorebook, chat history). Resolves connection via rewrite-specific → default chat → agent default chain. Uses `createLLMProvider()` and `chatComplete()` with a focused system prompt that returns only rewritten text. - **`packages/client/src/components/ui/MagicRewritePanel.tsx`**: new panel component with instruction textarea, context selectors (character card, lorebook entries, chat history with configurable message count up to 250), generate button with loading spinner, and a word-level diff preview showing before/after with changed spans highlighted. Supports persistent instruction recall via `localStorage`. - **`packages/client/src/components/lorebooks/LorebookFormFields.tsx`**: adds ✨ Rewrite button in the lorebook description editor modal (bottom-right, left of Done). Clicking it enters rewrite mode with Back/Apply controls. - **`packages/client/src/components/ui/ExpandedTextarea.tsx`**: adds ✨ Rewrite button in the shared full-screen editor header. This automatically makes the feature available in the character editor, persona editor, and chat settings drawer — all of which use this shared component. - **`packages/client/src/components/connections/ConnectionEditor.tsx`**: adds "Default for Rewrite" toggle beneath the existing "Default for Agents" toggle, with tooltip explaining the fallback behavior. Hidden for image-generation provider connections. ## Validation - [x] `pnpm check` passes locally - [ ] 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 - Lorebook description editor: ✨ Rewrite button appears, generates rewrite using the default chat connection, diff preview renders correctly with line breaks in both before/after panes, Apply saves rewritten text back to the editor. - Character editor: ✨ Rewrite button appears in the expand-textarea header for all text fields. - Persona editor: ✨ Rewrite button appears in the expand-textarea header for all text fields. - Connection settings: "Default for Rewrite" toggle works, mutual exclusion enforced (only one non-image connection can hold the flag). - Fallback chain: with no rewrite-specific connection set, correctly falls through to the default chat connection, then agent default. - Chat history context: messages slider configurable up to 250, context included in the generation request. - Instruction persistence: rewrite instructions saved to and recalled from `localStorage`. - Escape key does not close the modal while Magic Rewrite mode is active. ## Docs and release impact - [x] 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) ## UI evidence (if applicable) <img width="743" height="325" alt="Screenshot 2026-05-24 222848" src="https://github.com/user-attachments/assets/9e052222-bb93-47a0-8f63-fd417c152cff" /> --- <img width="788" height="387" alt="Screenshot 2026-05-24 223102" src="https://github.com/user-attachments/assets/26436275-6f82-4bf5-b7a3-9de920dc6da2" /> ---- <img width="2944" height="1697" alt="Screenshot 2026-05-24 223043" src="https://github.com/user-attachments/assets/d01c92e0-825a-427e-ae63-9d2905128010" /> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:12:05 +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#4965
No description provided.