[PR #7615] [MERGED] fix(macros): fill in lorebook includes when a user message is sent (#7607) #7836

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

📋 Pull Request Information

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

Base: staging ← Head: fix/7607-include-macro-user-message


📝 Commits (2)

  • 8c7b5f4 fix(macros): fill in lorebook includes when a user message is sent (#7607)
  • 1ed6491 fix(macros): give include expansion a deadline and keep includes without a chat (#7607)

📊 Changes

3 files changed (+222 additions, -3 deletions)

View changed files

📝 packages/client/src/lib/chat-macros.ts (+35 -3)
📝 packages/server/src/routes/lorebooks.routes.ts (+15 -0)
➕ scripts/regressions/user-message-include-macro.regression.ts (+172 -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 #7607

Why this change

  • Since #7600, Send, Post Only and /send fill in a message's macros before saving it, so {{random}} is rolled once and the chat shows the same text the model reads. {{include::Entry}} was skipped: it reads lorebook entries, and only the server has those. The message was saved as typed, the chat showed {{include::Test}}, and every new prompt filled it in again. A {{random}} inside the entry rolled differently on every turn, and editing the entry changed old messages.
  • Now a message with an include is saved with the entry's text in it, and that text's macros are filled in once, with the rest of the message.

What changed

  • New server route POST /api/lorebooks/expand-includes ({ text, chatId } → { text }). It only puts in the included entries. It reuses loadLorebookIncludesFor and expandLorebookIncludes, so it reads the same lorebooks as an include in a prompt: a plain name is looked up in the lorebooks the chat uses, BOOK::ENTRY and entry IDs reach any lorebook, nested includes work, and an include that loops back becomes empty. A missing entry also becomes empty, as it does in a prompt. Macro comments are removed first, the same order resolveMacros uses.
  • prepareUserInputMessage (packages/client/src/lib/chat-macros.ts) calls that route through the new expandMessageIncludes just before its final macro pass, and only when the message uses {{include::. The final pass then fills in the entry's own macros ({{random}}, {{user}}, …) together with the rest of the message. Plain messages make no extra request.
  • If the request fails, for example because the server is older or offline, the include is kept as typed and the prompt fills it in, as it did before. The failure is logged with console.warn.
  • The send waits at most 10 seconds for that request. After that it goes on with the include as typed, so a server that never answers can't hold up the send.
  • With no chat ID, the client sends no request and keeps the include as typed. A plain name is looked up only in the chat's lorebooks, so without a chat it would come out empty.
  • expandMessageIncludes is exported so Game turns can use it (see Notes for reviewers).
  • Includes in prompts, cards, lorebook entries and agent prompts work as before.
  • New regression scripts/regressions/user-message-include-macro.regression.ts. It runs prepareUserInputMessage against the real route and a real database. It also covers the 10-second limit and a send without a chat.

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

Commands the agent ran locally (for reference, not ticked):

  • node ./scripts/run-regressions.mjs --filter scripts/regressions/user-message-include-macro.regression.ts: failed on unfixed staging (a8481ca6d) with 'Respond with: {{include::Test}}' instead of 'Respond with: Random Number: 42 for Pat', and passes with the fix. The two later checks also failed before their own fixes: the no-chat send came back as '[]' instead of '[{{include::Test}}]', and the hanging-server check failed with "the request has a deadline".
  • --filter macro (5/5), --filter lorebook (22/22), --filter include (2/2), --filter send-command-macros, --filter chat-persona-macros, --filter character-identity, --filter conversation-game-slash: all passed on the final head.
  • pnpm regression:prompt: passed (3/3 files) on the final head.
  • pnpm check: passed on the final head. The one ESLint warning (react-hooks/exhaustive-deps, serverRun) is already on staging.
  • Local CodeRabbit (coderabbit review --agent --committed --base-commit a8481ca6d): review completed with 0 findings, on both the first commit and the final head.
  • GitHub CodeRabbit's two threads were answered and resolved: the 10-second limit fixes the first, and the Game hand-off below answers the second.

Manual verification notes

  • Manually verify in the browser: add a lorebook to a Roleplay chat with an entry named Test whose content is Random Number: {{random:0:100}}. Send Respond with: {{include::Test}}. The chat should show Respond with: Random Number: N, not the raw macro.
  • Manually verify that regenerating, or sending more messages, keeps the same N in that message and in Peek Prompt. Then edit the entry and check that the old message stays the same.
  • Manually verify that Post Only and /send Respond with: {{include::Test}} store the same kind of filled-in text.
  • Manually verify the same in a Conversation chat.
  • Manually verify {{include::Other lorebook::Entry}} with a lorebook that is not added to the chat. It should be filled in. A plain {{include::Entry}} for that entry should come out empty, as in a prompt.
  • Manually verify with the server stopped: sending {{include::Test}} should still send, with the include as typed, and log one warning in the browser console.

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)

No docs were edited. docs/prompts/macros.md already says macros typed in the chat message box are filled in before the message is sent, and includes now do that too.

Notes for reviewers:

  • Game turns: on staging, a Game turn fills in no macros when it is sent. All of them, {{random}} too, are saved as typed and filled in at prompt time, so this PR has no Game send step to add includes to. The in-progress fix/send-followups change adds that step in GameSurface.tsx, using createInputMacroResolverForChat without prepareUserInputMessage. It should call userMessage = await expandMessageIncludes(userMessage, useChatStore.getState().activeChat?.id) before its resolver; otherwise a Game turn will save {{random}} filled in but {{include::…}} as typed. Its /remind uses prepareUserMessage, so it gets includes already. The two branches change different files.
  • Known limit, for Mari to decide: an included entry's macros are now filled in by the client, the same way #7600 fills in macros typed in the message. The client has no chat variables, model, chat ID, lorebook counts or outlets. So {{getvar::x}}, {{model}}, {{chatId}} and {{outlet::x}} inside an included entry are saved empty, {{lorebooksize::id}} is saved as 0, and a {{setvar}} in the entry doesn't change the chat's variable. Before this PR, these got real values each time a prompt was built. Typed directly into the message, they have been saved empty since #7600. A later fix could give the client the chat's macroVariables, or leave these macros as typed for the prompt. Either would change typed macros too, so it is left out of this PR.

UI evidence (if applicable)

Not applicable. The change affects what is stored, and no UI was changed.

Summary by CodeRabbit

  • New Features
    • Lorebook includes in user messages are expanded when you send a message, using the entries available to the current chat. Macros are resolved using the current lorebook entries.
  • Bug Fixes
    • If include expansion fails or times out, the original message text is preserved so sending can continue.

🔄 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/7615 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/14/2026 **Status:** ✅ Merged **Merged:** 10/14/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/7607-include-macro-user-message` --- ### 📝 Commits (2) - [`8c7b5f4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8c7b5f4295690275aa44eb11c656d2155fc183d7) fix(macros): fill in lorebook includes when a user message is sent (#7607) - [`1ed6491`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1ed6491fef193b00c28c0c5f658b50216ec7349e) fix(macros): give include expansion a deadline and keep includes without a chat (#7607) ### 📊 Changes **3 files changed** (+222 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/lib/chat-macros.ts` (+35 -3) 📝 `packages/server/src/routes/lorebooks.routes.ts` (+15 -0) ➕ `scripts/regressions/user-message-include-macro.regression.ts` (+172 -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 #7607 ## Why this change - Since #7600, Send, Post Only and /send fill in a message's macros before saving it, so `{{random}}` is rolled once and the chat shows the same text the model reads. `{{include::Entry}}` was skipped: it reads lorebook entries, and only the server has those. The message was saved as typed, the chat showed `{{include::Test}}`, and every new prompt filled it in again. A `{{random}}` inside the entry rolled differently on every turn, and editing the entry changed old messages. - Now a message with an include is saved with the entry's text in it, and that text's macros are filled in once, with the rest of the message. ## What changed - New server route `POST /api/lorebooks/expand-includes` (`{ text, chatId }` → `{ text }`). It only puts in the included entries. It reuses `loadLorebookIncludesFor` and `expandLorebookIncludes`, so it reads the same lorebooks as an include in a prompt: a plain name is looked up in the lorebooks the chat uses, `BOOK::ENTRY` and entry IDs reach any lorebook, nested includes work, and an include that loops back becomes empty. A missing entry also becomes empty, as it does in a prompt. Macro comments are removed first, the same order `resolveMacros` uses. - `prepareUserInputMessage` (`packages/client/src/lib/chat-macros.ts`) calls that route through the new `expandMessageIncludes` just before its final macro pass, and only when the message uses `{{include::`. The final pass then fills in the entry's own macros (`{{random}}`, `{{user}}`, …) together with the rest of the message. Plain messages make no extra request. - If the request fails, for example because the server is older or offline, the include is kept as typed and the prompt fills it in, as it did before. The failure is logged with `console.warn`. - The send waits at most 10 seconds for that request. After that it goes on with the include as typed, so a server that never answers can't hold up the send. - With no chat ID, the client sends no request and keeps the include as typed. A plain name is looked up only in the chat's lorebooks, so without a chat it would come out empty. - `expandMessageIncludes` is exported so Game turns can use it (see Notes for reviewers). - Includes in prompts, cards, lorebook entries and agent prompts work as before. - New regression `scripts/regressions/user-message-include-macro.regression.ts`. It runs `prepareUserInputMessage` against the real route and a real database. It also covers the 10-second limit and a send without a chat. ## 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` Commands the agent ran locally (for reference, not ticked): - `node ./scripts/run-regressions.mjs --filter scripts/regressions/user-message-include-macro.regression.ts`: failed on unfixed `staging` (`a8481ca6d`) with `'Respond with: {{include::Test}}'` instead of `'Respond with: Random Number: 42 for Pat'`, and passes with the fix. The two later checks also failed before their own fixes: the no-chat send came back as `'[]'` instead of `'[{{include::Test}}]'`, and the hanging-server check failed with "the request has a deadline". - `--filter macro` (5/5), `--filter lorebook` (22/22), `--filter include` (2/2), `--filter send-command-macros`, `--filter chat-persona-macros`, `--filter character-identity`, `--filter conversation-game-slash`: all passed on the final head. - `pnpm regression:prompt`: passed (3/3 files) on the final head. - `pnpm check`: passed on the final head. The one ESLint warning (`react-hooks/exhaustive-deps`, `serverRun`) is already on `staging`. - Local CodeRabbit (`coderabbit review --agent --committed --base-commit a8481ca6d`): review completed with 0 findings, on both the first commit and the final head. - GitHub CodeRabbit's two threads were answered and resolved: the 10-second limit fixes the first, and the Game hand-off below answers the second. ### Manual verification notes - Manually verify in the browser: add a lorebook to a Roleplay chat with an entry named `Test` whose content is `Random Number: {{random:0:100}}`. Send `Respond with: {{include::Test}}`. The chat should show `Respond with: Random Number: N`, not the raw macro. - Manually verify that regenerating, or sending more messages, keeps the same `N` in that message and in Peek Prompt. Then edit the entry and check that the old message stays the same. - Manually verify that Post Only and `/send Respond with: {{include::Test}}` store the same kind of filled-in text. - Manually verify the same in a Conversation chat. - Manually verify `{{include::Other lorebook::Entry}}` with a lorebook that is not added to the chat. It should be filled in. A plain `{{include::Entry}}` for that entry should come out empty, as in a prompt. - Manually verify with the server stopped: sending `{{include::Test}}` should still send, with the include as typed, and log one warning in the browser console. ## 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) No docs were edited. `docs/prompts/macros.md` already says macros typed in the chat message box are filled in before the message is sent, and includes now do that too. Notes for reviewers: - **Game turns:** on `staging`, a Game turn fills in no macros when it is sent. All of them, `{{random}}` too, are saved as typed and filled in at prompt time, so this PR has no Game send step to add includes to. The in-progress `fix/send-followups` change adds that step in `GameSurface.tsx`, using `createInputMacroResolverForChat` without `prepareUserInputMessage`. It should call `userMessage = await expandMessageIncludes(userMessage, useChatStore.getState().activeChat?.id)` before its resolver; otherwise a Game turn will save `{{random}}` filled in but `{{include::…}}` as typed. Its `/remind` uses `prepareUserMessage`, so it gets includes already. The two branches change different files. - **Known limit, for Mari to decide:** an included entry's macros are now filled in by the client, the same way #7600 fills in macros typed in the message. The client has no chat variables, model, chat ID, lorebook counts or outlets. So `{{getvar::x}}`, `{{model}}`, `{{chatId}}` and `{{outlet::x}}` inside an included entry are saved empty, `{{lorebooksize::id}}` is saved as `0`, and a `{{setvar}}` in the entry doesn't change the chat's variable. Before this PR, these got real values each time a prompt was built. Typed directly into the message, they have been saved empty since #7600. A later fix could give the client the chat's `macroVariables`, or leave these macros as typed for the prompt. Either would change typed macros too, so it is left out of this PR. ## UI evidence (if applicable) Not applicable. The change affects what is stored, and no UI was changed. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Lorebook includes in user messages are expanded when you send a message, using the entries available to the current chat. Macros are resolved using the current lorebook entries. * **Bug Fixes** * If include expansion fails or times out, the original message text is preserved so sending can continue. <!-- 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-24 04:08:00 +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#7836
No description provided.