[PR #1749] [MERGED] feat: character-scoped regex scripts with tri-state mode #5259

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1749
Author: @kolacheee
Created: 5/31/2026
Status: ✅ Merged
Merged: 6/1/2026
Merged by: @cha1latte

Base: refactor ← Head: feat/character-scoped-regex


📝 Commits (7)

  • 7ad3f50 feat: character-scoped regex scripts with tri-state mode
  • 79b9a01 fix: match scoped regex mode button order to card theming
  • 57743b7 security: harden CSS sanitizer and regex import against malicious cards
  • e04cb7a fix: resolve architecture violations and unused exports
  • 40e5a95 Fix scoped regex PR regressions
  • d4e63dd Merge upstream refactor into scoped regex PR
  • 329d036 Address review feedback: scoped regex imports

📊 Changes

16 files changed (+784 additions, -35 deletions)

View changed files

📝 src/engine/contracts/schemas/regex.schema.ts (+1 -0)
📝 src/engine/contracts/types/chat.ts (+3 -0)
📝 src/engine/contracts/types/regex.ts (+1 -0)
📝 src/features/catalog/agents/hooks/use-regex-scripts.ts (+12 -3)
📝 src/features/catalog/agents/index.ts (+1 -0)
📝 src/features/catalog/agents/regex-application.test.tsx (+75 -4)
📝 src/features/catalog/agents/regex-application.ts (+40 -6)
📝 src/features/catalog/characters/hooks/use-character-import-flow.ts (+37 -1)
📝 src/features/modes/conversation/components/ConversationInput.tsx (+12 -8)
📝 src/features/modes/game/components/GameNarration.tsx (+24 -4)
📝 src/features/modes/shared/chat-ui/components/ChatInput.tsx (+7 -6)
📝 src/features/modes/shared/chat-ui/components/ChatMessage.tsx (+18 -2)
📝 src/features/modes/shared/chat-ui/components/ChatSettingsDrawer.tsx (+164 -1)
📝 src/features/shell/bot-browser/components/BotBrowserView.tsx (+17 -0)
➕ src/shared/lib/regex-script-import.test.tsx (+220 -0)
➕ src/shared/lib/regex-script-import.ts (+152 -0)

📄 Description

Linked issue

Closes #1758

Why this change

SillyTavern character cards can embed regex find-and-replace scripts in extensions.regex_scripts that transform AI output (e.g., replacing <coras_comm> tags with styled HTML). In the old engine these were imported as character-scoped scripts (PR #1059) but the feature was never completed. This is a clean rewrite for the refactored Tauri architecture, adding character scoping to the existing global-only regex system, wiring extraction into the import pipeline, and providing per-chat mode controls.

What changed

  • Types: added characterId: string | null to RegexScript interface and createRegexScriptSchema
  • ChatMetadata: added scopedRegexMode field (disabled/exclusive/chat)
  • use-regex-scripts hook: accepts optional characterIds parameter for client-side filtering (falsy checks for null/undefined compatibility with JSON storage), added useBatchCreateRegexScripts and useDeleteRegexScriptsByCharacter mutations
  • regex-application.ts: exported ScopedRegexMode type, added filterForMode() function, updated useApplyRegex(characterIds?) to filter scripts by scoped mode before application
  • ChatMessage: passes chatCharacterIds to useApplyRegex, reads scopedRegexMode from chat store, passes scopedMode + characterId to applyToAIOutput
  • ChatInput + ConversationInput: compute chatCharacterIds from props, pass to useApplyRegex, add scopedMode to all applyToUserInput calls
  • GameNarration: passes activeCharacterIds to useApplyRegex, reads scopedRegexMode from store, passes context to applyToAIOutput
  • ChatSettingsDrawer: added Scoped Regex Scripts section with tri-state mode selector (Disabled/Exclusive/Chat) and collapsible per-character script groups with on/off toggles
  • New utility: src/shared/lib/regex-script-import.ts — extracts embedded regex scripts from ST card extensions.regex_scripts, converts ST format (numeric placements, /pattern/flags, disabled → enabled), validates with isPatternSafe() ReDoS check, batch-creates via storageApi
  • Import integration: use-character-import-flow.ts calls importRegexScriptsForCharacter after successful ST batch import; BotBrowserView.tsx calls it after both importStCharacter paths

Refactor impact

Primary owner: catalog (agents/regex), shared mode UI, imports

Impact areas reviewed:

  • src/engine/contracts/ — type + schema additions (one nullable field)
  • src/features/catalog/agents/ — hooks + application logic updated
  • src/features/modes/shared/chat-ui/ — ChatMessage, ChatInput, ChatSettingsDrawer modified
  • src/features/modes/conversation/ — ConversationInput modified
  • src/features/modes/game/ — GameNarration modified
  • src/features/catalog/characters/hooks/ — import flow hook modified
  • src/features/shell/bot-browser/ — BotBrowserView modified

Boundary notes:

  • No Rust/Tauri changes — regex scripts are stored as JSON documents in the existing regex-scripts collection, character filtering is client-side
  • No engine generation-core changes — the regex-application.ts in features/catalog/agents/ is the display-time hook, not the prompt-assembly pipeline
  • Import extraction runs in TypeScript after the Rust import returns the character record

Pressure points touched:

  • ChatMessage (useApplyRegex call signature, applyToAIOutput options)
  • ChatInput (new chatCharacterIds memo, applyToUserInput options)
  • ConversationInput (same pattern as ChatInput, 3 call sites)
  • GameNarration (useApplyRegex + useChatStore imports, applyToAIOutput options)
  • ChatSettingsDrawer (new section + 3 internal components)
  • use-character-import-flow (post-import regex extraction call)
  • BotBrowserView (post-import regex extraction at 2 call sites)

Validation

  • Matching validation command passes locally (for example pnpm typecheck, pnpm build, pnpm check:architecture, pnpm check:docs, or full pnpm check when warranted)
  • pnpm check passes locally before PR push/handoff, including the unused-code check
  • pnpm typecheck passes locally
  • pnpm build passes locally
  • pnpm check:architecture passes locally
  • pnpm check:docs passes locally
  • Browser or Tauri app manual verification completed

Manual verification notes

  • Imported KZK character card with 30 embedded regex scripts — all 30 extracted and created as character-scoped scripts
  • Verified Scoped Regex Scripts section appears in Chat Settings with count badge, mode selector, and collapsible KZK group showing all scripts with on/off toggles
  • Verified Exclusive mode applies character-scoped scripts only to that character's messages (AI output transformed, user input unaffected by character scripts)
  • Verified Disabled mode turns off all scoped scripts while global scripts still run
  • Verified Chat mode applies all scoped scripts to every message including user input
  • Verified mode selector order matches Card Theming (Disabled → Exclusive → Chat)
  • Verified toggling individual scripts on/off works and persists

Docs and release impact

  • No docs changes needed

UI evidence

image image

Summary by CodeRabbit

  • New Features
    • Introduced character-scoped regex scripts with three application modes: disabled (global scripts only), exclusive (character-specific scripts only), or chat (all scripts)
    • Regex scripts can now be automatically imported from character files
    • New chat settings for managing per-character regex script configuration and application behavior

🔄 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/1749 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 5/31/2026 **Status:** ✅ Merged **Merged:** 6/1/2026 **Merged by:** [@cha1latte](https://github.com/cha1latte) **Base:** `refactor` ← **Head:** `feat/character-scoped-regex` --- ### 📝 Commits (7) - [`7ad3f50`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7ad3f50e40b9fc2b13a96e13c582dc85fb5509ab) feat: character-scoped regex scripts with tri-state mode - [`79b9a01`](https://github.com/Pasta-Devs/Marinara-Engine/commit/79b9a013759133fc7afc981b4bd04b48a3566b85) fix: match scoped regex mode button order to card theming - [`57743b7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/57743b7ca14515361374310029fc0aecf70bc4c0) security: harden CSS sanitizer and regex import against malicious cards - [`e04cb7a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e04cb7a39721d7961f024b40d64f952d2ada0aa3) fix: resolve architecture violations and unused exports - [`40e5a95`](https://github.com/Pasta-Devs/Marinara-Engine/commit/40e5a9561f0728cb61a1a9168027d6ff206f6287) Fix scoped regex PR regressions - [`d4e63dd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d4e63ddc36ec08ae5a6de8ddc09822743e885b7a) Merge upstream refactor into scoped regex PR - [`329d036`](https://github.com/Pasta-Devs/Marinara-Engine/commit/329d0368450e2b266fe1d504f4e511dea5703dca) Address review feedback: scoped regex imports ### 📊 Changes **16 files changed** (+784 additions, -35 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/contracts/schemas/regex.schema.ts` (+1 -0) 📝 `src/engine/contracts/types/chat.ts` (+3 -0) 📝 `src/engine/contracts/types/regex.ts` (+1 -0) 📝 `src/features/catalog/agents/hooks/use-regex-scripts.ts` (+12 -3) 📝 `src/features/catalog/agents/index.ts` (+1 -0) 📝 `src/features/catalog/agents/regex-application.test.tsx` (+75 -4) 📝 `src/features/catalog/agents/regex-application.ts` (+40 -6) 📝 `src/features/catalog/characters/hooks/use-character-import-flow.ts` (+37 -1) 📝 `src/features/modes/conversation/components/ConversationInput.tsx` (+12 -8) 📝 `src/features/modes/game/components/GameNarration.tsx` (+24 -4) 📝 `src/features/modes/shared/chat-ui/components/ChatInput.tsx` (+7 -6) 📝 `src/features/modes/shared/chat-ui/components/ChatMessage.tsx` (+18 -2) 📝 `src/features/modes/shared/chat-ui/components/ChatSettingsDrawer.tsx` (+164 -1) 📝 `src/features/shell/bot-browser/components/BotBrowserView.tsx` (+17 -0) ➕ `src/shared/lib/regex-script-import.test.tsx` (+220 -0) ➕ `src/shared/lib/regex-script-import.ts` (+152 -0) </details> ### 📄 Description ## Linked issue Closes #1758 ## Why this change SillyTavern character cards can embed regex find-and-replace scripts in `extensions.regex_scripts` that transform AI output (e.g., replacing `<coras_comm>` tags with styled HTML). In the old engine these were imported as character-scoped scripts ([PR #1059](https://github.com/Pasta-Devs/Marinara-Engine/pull/1059)) but the feature was never completed. This is a clean rewrite for the refactored Tauri architecture, adding character scoping to the existing global-only regex system, wiring extraction into the import pipeline, and providing per-chat mode controls. ## What changed - **Types:** added `characterId: string | null` to `RegexScript` interface and `createRegexScriptSchema` - **ChatMetadata:** added `scopedRegexMode` field (disabled/exclusive/chat) - **`use-regex-scripts` hook:** accepts optional `characterIds` parameter for client-side filtering (falsy checks for null/undefined compatibility with JSON storage), added `useBatchCreateRegexScripts` and `useDeleteRegexScriptsByCharacter` mutations - **`regex-application.ts`:** exported `ScopedRegexMode` type, added `filterForMode()` function, updated `useApplyRegex(characterIds?)` to filter scripts by scoped mode before application - **ChatMessage:** passes `chatCharacterIds` to `useApplyRegex`, reads `scopedRegexMode` from chat store, passes `scopedMode` + `characterId` to `applyToAIOutput` - **ChatInput + ConversationInput:** compute `chatCharacterIds` from props, pass to `useApplyRegex`, add `scopedMode` to all `applyToUserInput` calls - **GameNarration:** passes `activeCharacterIds` to `useApplyRegex`, reads `scopedRegexMode` from store, passes context to `applyToAIOutput` - **ChatSettingsDrawer:** added Scoped Regex Scripts section with tri-state mode selector (Disabled/Exclusive/Chat) and collapsible per-character script groups with on/off toggles - **New utility:** `src/shared/lib/regex-script-import.ts` — extracts embedded regex scripts from ST card `extensions.regex_scripts`, converts ST format (numeric placements, `/pattern/flags`, `disabled` → `enabled`), validates with `isPatternSafe()` ReDoS check, batch-creates via `storageApi` - **Import integration:** `use-character-import-flow.ts` calls `importRegexScriptsForCharacter` after successful ST batch import; `BotBrowserView.tsx` calls it after both `importStCharacter` paths ## Refactor impact Primary owner: catalog (agents/regex), shared mode UI, imports Impact areas reviewed: - `src/engine/contracts/` — type + schema additions (one nullable field) - `src/features/catalog/agents/` — hooks + application logic updated - `src/features/modes/shared/chat-ui/` — ChatMessage, ChatInput, ChatSettingsDrawer modified - `src/features/modes/conversation/` — ConversationInput modified - `src/features/modes/game/` — GameNarration modified - `src/features/catalog/characters/hooks/` — import flow hook modified - `src/features/shell/bot-browser/` — BotBrowserView modified Boundary notes: - No Rust/Tauri changes — regex scripts are stored as JSON documents in the existing `regex-scripts` collection, character filtering is client-side - No engine generation-core changes — the `regex-application.ts` in `features/catalog/agents/` is the display-time hook, not the prompt-assembly pipeline - Import extraction runs in TypeScript after the Rust import returns the character record Pressure points touched: - ChatMessage (useApplyRegex call signature, applyToAIOutput options) - ChatInput (new chatCharacterIds memo, applyToUserInput options) - ConversationInput (same pattern as ChatInput, 3 call sites) - GameNarration (useApplyRegex + useChatStore imports, applyToAIOutput options) - ChatSettingsDrawer (new section + 3 internal components) - use-character-import-flow (post-import regex extraction call) - BotBrowserView (post-import regex extraction at 2 call sites) ## Validation - [x] Matching validation command passes locally (for example `pnpm typecheck`, `pnpm build`, `pnpm check:architecture`, `pnpm check:docs`, or full `pnpm check` when warranted) - [x] `pnpm check` passes locally before PR push/handoff, including the unused-code check - [x] `pnpm typecheck` passes locally - [x] `pnpm build` passes locally - [x] `pnpm check:architecture` passes locally - [x] `pnpm check:docs` passes locally - [x] Browser or Tauri app manual verification completed ### Manual verification notes - Imported KZK character card with 30 embedded regex scripts — all 30 extracted and created as character-scoped scripts - Verified Scoped Regex Scripts section appears in Chat Settings with count badge, mode selector, and collapsible KZK group showing all scripts with on/off toggles - Verified Exclusive mode applies character-scoped scripts only to that character's messages (AI output transformed, user input unaffected by character scripts) - Verified Disabled mode turns off all scoped scripts while global scripts still run - Verified Chat mode applies all scoped scripts to every message including user input - Verified mode selector order matches Card Theming (Disabled → Exclusive → Chat) - Verified toggling individual scripts on/off works and persists ## Docs and release impact - [x] No docs changes needed ## UI evidence <img width="2169" height="1605" alt="image" src="https://github.com/user-attachments/assets/3f8d0c73-011d-4125-b0c7-1f46db55101c" /> <img width="1456" height="825" alt="image" src="https://github.com/user-attachments/assets/3a5fb6eb-9ac1-4795-a500-0e63a82b0f50" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Introduced character-scoped regex scripts with three application modes: disabled (global scripts only), exclusive (character-specific scripts only), or chat (all scripts) * Regex scripts can now be automatically imported from character files * New chat settings for managing per-character regex script configuration and application behavior <!-- 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:14:40 +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#5259
No description provided.