[GH-ISSUE #6734] [Feature]: Per-chat variables you can type in your own messages #3775

Closed
opened 2026-10-21 06:07:04 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kentarofujiy on GitHub (Sep 26, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6734

Originally assigned to: @SpicyMarinara on GitHub.

What problem does this solve?

There is no way to give a name to a piece of text and reuse it in your own messages. If you want to type {{char1}} and have the model read "Mary", nothing in the app supports it:

  • {{setvar::char1::Mary}} and {{getvar::char1}} typed into a message both resolve to an empty string. The client resolves macros before the message is stored, using a context built from persona and characters only (createInputMacroResolverForChat in packages/client/src/lib/chat-macros.ts), so the stored text is already blank and the server never sees the macro. docs/prompts/macros.md says variables "persist in the current chat", which is true for prompt fields but not for anything you type.
  • Preset variables do not reach typed messages either. History is resolved in generate.routes.ts before preset values are merged into the macro context, so a preset variable named char1 leaves the literal {{char1}} in the prompt.
  • A chat already persists macroVariables in its metadata, written by {{setvar}} from prompt sections and lorebook entries, but nothing in the UI shows or edits them. They are invisible state.

The only thing that works today is a regex script with Apply To: User Input and Apply Mode: Only Prompt. That does the job, but it is an obscure route for what reads like a basic feature, it lives in a global or character-scoped script list rather than with the chat, and the two settings that make it work are easy to get wrong.

Proposed solution

A Chat Variables section in the Chat Settings drawer: rows of name and value, saved per chat.

Type {{char1}} in any message and the model reads the value. The message bubble keeps showing the tag you typed, because resolution happens when the prompt is built — which also means changing a value later changes the earlier turns that used it.

Mechanically: the {{NAME}} catch-all in the shared macro engine falls back to the chat's localVariables after the preset variables, so every path that already passes that map starts working at once — typed messages, prompt sections, lorebooks, character cards, Peek Prompt. Preset variables keep precedence on a name clash.

It reuses the existing chatMetadata.macroVariables store rather than adding one, so the section doubles as a viewer and editor for variables a prompt or lorebook set.

Naming rules: letters, numbers and underscores, starting with a letter or underscore, and not a built-in macro name. A built-in always resolves first, so a variable called char could never be read; the editor refuses it instead of failing silently. Dotted or dashed names still work with {{setvar}} and {{getvar}}, just not as a bare tag.

Alternatives considered

  • Leave it to regex scripts. Works today, but it is not discoverable and it is not per chat.
  • Defer variable macros in typed input so {{setvar}} and {{getvar}} survive the client and prompt assembly resolves them. This was the first design investigated. It does not give you the short {{char1}} form that started the request, it means touching four call sites across two input components, and the bubble would show the raw tag regardless — so the display question has to be answered either way. Not pursued.
  • Seed the preset variable map with the chat's values on the server instead of changing the engine lookup. Rejected: it creates a second source of truth that goes stale the moment a prompt or lorebook runs {{setvar}} during assembly, and it would have to be repeated at six independent context-build sites.
  • Global, app-wide variables. Per chat won, so {{char1}} can mean different people in different stories, and because the per-chat store already exists and already persists.

Additional context

The implementation is done and committed locally on feat/chat-variables; a PR against staging will follow. It ships with two new regression scripts plus additions to prompt.regression.ts, and updates docs/prompts/macros.md, docs/prompts/preset-variables.md and docs/chats/chat-settings.md.

Three consequences worth agreeing on before it lands:

  1. The "unknown tags stay literal" promise narrows. docs/prompts/macros.md currently says any unknown {{name}} survives verbatim. After this, a name that matches a chat variable resolves instead — so decorative {{mood}} text in a chat that also defines mood will expand. Documented, but it is a real behavior change.
  2. Assistant history goes through the same resolution pass. If a model emits a literal {{mood}} and a variable of that name exists, it expands on the next turn. Today it stays literal. The escape hatch, if it ever bites, is a resolve option that is on for prompt paths and off for assistant history — not built up front.
  3. A {{setvar}} of the same name wins, since both use one store per chat. The editor warns rather than blocking, and the value a generation writes is the one that persists.

The change also fixes a pre-existing leak it would otherwise widen: the catch-all did a bare property read, so {{toString}} resolved to "function toString() { [native code] }" in any macro-enabled field. The lookup now requires an own string property.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kentarofujiy on GitHub (Sep 26, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6734 Originally assigned to: @SpicyMarinara on GitHub. ## What problem does this solve? There is no way to give a name to a piece of text and reuse it in your own messages. If you want to type `{{char1}}` and have the model read "Mary", nothing in the app supports it: - `{{setvar::char1::Mary}}` and `{{getvar::char1}}` typed into a message both resolve to an empty string. The client resolves macros before the message is stored, using a context built from persona and characters only (`createInputMacroResolverForChat` in `packages/client/src/lib/chat-macros.ts`), so the stored text is already blank and the server never sees the macro. `docs/prompts/macros.md` says variables "persist in the current chat", which is true for prompt fields but not for anything you type. - Preset variables do not reach typed messages either. History is resolved in `generate.routes.ts` before preset values are merged into the macro context, so a preset variable named `char1` leaves the literal `{{char1}}` in the prompt. - A chat already persists `macroVariables` in its metadata, written by `{{setvar}}` from prompt sections and lorebook entries, but nothing in the UI shows or edits them. They are invisible state. The only thing that works today is a regex script with **Apply To: User Input** and **Apply Mode: Only Prompt**. That does the job, but it is an obscure route for what reads like a basic feature, it lives in a global or character-scoped script list rather than with the chat, and the two settings that make it work are easy to get wrong. ## Proposed solution A **Chat Variables** section in the Chat Settings drawer: rows of name and value, saved per chat. Type `{{char1}}` in any message and the model reads the value. The message bubble keeps showing the tag you typed, because resolution happens when the prompt is built — which also means changing a value later changes the earlier turns that used it. Mechanically: the `{{NAME}}` catch-all in the shared macro engine falls back to the chat's `localVariables` after the preset variables, so every path that already passes that map starts working at once — typed messages, prompt sections, lorebooks, character cards, Peek Prompt. Preset variables keep precedence on a name clash. It reuses the existing `chatMetadata.macroVariables` store rather than adding one, so the section doubles as a viewer and editor for variables a prompt or lorebook set. Naming rules: letters, numbers and underscores, starting with a letter or underscore, and not a built-in macro name. A built-in always resolves first, so a variable called `char` could never be read; the editor refuses it instead of failing silently. Dotted or dashed names still work with `{{setvar}}` and `{{getvar}}`, just not as a bare tag. ## Alternatives considered - **Leave it to regex scripts.** Works today, but it is not discoverable and it is not per chat. - **Defer variable macros in typed input** so `{{setvar}}` and `{{getvar}}` survive the client and prompt assembly resolves them. This was the first design investigated. It does not give you the short `{{char1}}` form that started the request, it means touching four call sites across two input components, and the bubble would show the raw tag regardless — so the display question has to be answered either way. Not pursued. - **Seed the preset variable map with the chat's values on the server** instead of changing the engine lookup. Rejected: it creates a second source of truth that goes stale the moment a prompt or lorebook runs `{{setvar}}` during assembly, and it would have to be repeated at six independent context-build sites. - **Global, app-wide variables.** Per chat won, so `{{char1}}` can mean different people in different stories, and because the per-chat store already exists and already persists. ## Additional context The implementation is done and committed locally on `feat/chat-variables`; a PR against `staging` will follow. It ships with two new regression scripts plus additions to `prompt.regression.ts`, and updates `docs/prompts/macros.md`, `docs/prompts/preset-variables.md` and `docs/chats/chat-settings.md`. Three consequences worth agreeing on before it lands: 1. **The "unknown tags stay literal" promise narrows.** `docs/prompts/macros.md` currently says any unknown `{{name}}` survives verbatim. After this, a name that matches a chat variable resolves instead — so decorative `{{mood}}` text in a chat that also defines `mood` will expand. Documented, but it is a real behavior change. 2. **Assistant history goes through the same resolution pass.** If a model emits a literal `{{mood}}` and a variable of that name exists, it expands on the next turn. Today it stays literal. The escape hatch, if it ever bites, is a resolve option that is on for prompt paths and off for assistant history — not built up front. 3. **A `{{setvar}}` of the same name wins**, since both use one store per chat. The editor warns rather than blocking, and the value a generation writes is the one that persists. The change also fixes a pre-existing leak it would otherwise widen: the catch-all did a bare property read, so `{{toString}}` resolved to `"function toString() { [native code] }"` in any macro-enabled field. The lookup now requires an own string property. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
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#3775
No description provided.