[PR #5454] [MERGED] Feat/lorebook size macro #6773

Closed
opened 2026-10-23 04:13:35 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5454
Author: @ylli-selmani
Created: 8/23/2026
Status: ✅ Merged
Merged: 8/24/2026
Merged by: @SpicyMarinara

Base: staging ← Head: feat/lorebookSizeMacro


📝 Commits (8)

  • d609408 added {{lorebooksize::lorebookID}} macro that resolves to number of total entries of said lorebook
  • 64bee23 Update CHANGELOG.md
  • 443a569 added a try and catch for the loading of entry counts.
  • 237adc7 Update packages/server/src/services/prompt/macro-context.ts
  • e152a92 reverted the logger.warn
  • 774c0bb re-did the logger, now correctly. oops.
  • 29bcd4c Merge branch 'staging' into feat/lorebookSizeMacro
  • b32609f test: cover lorebook size macro resolution

📊 Changes

10 files changed (+68 additions, -0 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 docs/prompts/macros.md (+8 -0)
📝 packages/server/src/routes/generate.routes.ts (+1 -0)
📝 packages/server/src/routes/generate/retry-agents-route.ts (+1 -0)
📝 packages/server/src/services/agents/agent-executor.ts (+1 -0)
📝 packages/server/src/services/prompt/macro-context.ts (+12 -0)
📝 packages/server/src/services/storage/lorebooks.storage.ts (+11 -0)
📝 packages/shared/src/types/agent.ts (+2 -0)
📝 packages/shared/src/utils/macro-engine.ts (+16 -0)
📝 scripts/regressions/prompt.regression.ts (+15 -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 #5464

Why this change

  • The idea was to implement a Lorebook Macro called {{lorebookSize::lorebookId}} macro that simply resolves to the total number of entries inside the specified lorebook.
  • Specifically intended to be used inside the Lorebook Keeper / a custom agent with lorebook editing capabilities prompt, to hand it the number of entries and always set a new entries' order at {{lorebookSize::lorebookId}} + 1 (e.g. new memories will have a higher order, be injected lower in the prompt, and closer to the 'now' for chronological flow)

What changed

  • Implemented a macro called {{lorebookSize::lorebookId}} that resolves to the total number of entries inside the Lorebook, specified by its ID.
    • For example, if a lorebook has the ID V1StGXR8_Z5jdHi6B-myT and contains 151 entries, {{lorebooksize::V1StGXR8_Z5jdHi6B-myT}} resolves to 151.
  • This is accessible from both the main chat and agent prompts.
  • If a lorebookID is corrupted/just pure gibberish, the macro resolves to 0.
  • If the pre-load fails inside macro-context.ts, all lorebooksize macros also resolve to 0.

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

  • Tried the macro with a wrong id => resolved to 0
  • Inserted {{lorebooksize ::JparyXKfJPOF24kT_F0QC} } inside a main chat prompt section => resolved to 151, the correct number.
  • Inserted {{lorebooksize ::JparyXKfJPOF24kT_F0QC}} + 1 inside an agent's prompt => Agent correctly output 152.
  • Tried {{setvar::size::{{lorebooksize ::JparyXKfJPOF24kT_F0QC}}}} => setvar resolves before 1{{lorebooksize}}` in macro pipeline, => turns into 0?

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) 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)

UI evidence (if applicable)

Inside Main Prompt:

pre resolving After: after resolving

Inside Agent Prompt:

agent pre resolve After: inside agent order

Gibberish:

fallback pre-resolve After: fallback resolve

Summary by CodeRabbit

  • New Features

    • Added the {{lorebooksize::ID}} macro to return a lorebook’s total entry count.
    • Unknown lorebook IDs return 0.
    • Counts include all entries, regardless of status or folder.
    • The macro is available wherever macros are supported.
  • Documentation

    • Added usage details for the new macro.
    • Documented the feature in the unreleased changelog.

Maintainer verification

  • Added a direct macro regression and own-property fallback proof on the current head.
  • pnpm check passes on the current head.
  • Translated documentation follow-up: #5472.

🔄 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/5454 **Author:** [@ylli-selmani](https://github.com/ylli-selmani) **Created:** 8/23/2026 **Status:** ✅ Merged **Merged:** 8/24/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `feat/lorebookSizeMacro` --- ### 📝 Commits (8) - [`d609408`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d6094082e72d34a7a86deb935208ef3f01f9e3b5) added {{lorebooksize::lorebookID}} macro that resolves to number of total entries of said lorebook - [`64bee23`](https://github.com/Pasta-Devs/Marinara-Engine/commit/64bee236b97a3cb541776bc3dc94aa03fa971088) Update CHANGELOG.md - [`443a569`](https://github.com/Pasta-Devs/Marinara-Engine/commit/443a569e12a5ad6fb7a3013442cf0bb9bfecb1ed) added a try and catch for the loading of entry counts. - [`237adc7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/237adc7be927b31f5ee484e862d16a53251a7310) Update packages/server/src/services/prompt/macro-context.ts - [`e152a92`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e152a927f8094a29eb0403c983a8bf81959b67e1) reverted the logger.warn - [`774c0bb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/774c0bb0cb720de10564e8765326d197ee98d067) re-did the logger, now correctly. oops. - [`29bcd4c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/29bcd4cbbf38f6a9dbb17d394bfae55dcebcd583) Merge branch 'staging' into feat/lorebookSizeMacro - [`b32609f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b32609fdb92d2c4ba34a432540446aeaab27c88a) test: cover lorebook size macro resolution ### 📊 Changes **10 files changed** (+68 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `docs/prompts/macros.md` (+8 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+1 -0) 📝 `packages/server/src/routes/generate/retry-agents-route.ts` (+1 -0) 📝 `packages/server/src/services/agents/agent-executor.ts` (+1 -0) 📝 `packages/server/src/services/prompt/macro-context.ts` (+12 -0) 📝 `packages/server/src/services/storage/lorebooks.storage.ts` (+11 -0) 📝 `packages/shared/src/types/agent.ts` (+2 -0) 📝 `packages/shared/src/utils/macro-engine.ts` (+16 -0) 📝 `scripts/regressions/prompt.regression.ts` (+15 -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 #5464 ## Why this change - The idea was to implement a Lorebook Macro called `{{lorebookSize::lorebookId}}` macro that simply resolves to the total number of entries inside the specified lorebook. - Specifically intended to be used inside the Lorebook Keeper / a custom agent with lorebook editing capabilities prompt, to hand it the number of entries and always set a new entries' order at `{{lorebookSize::lorebookId}} + 1` (e.g. new memories will have a higher order, be injected lower in the prompt, and closer to the 'now' for chronological flow) ## What changed - Implemented a macro called `{{lorebookSize::lorebookId}}` that resolves to the total number of entries inside the Lorebook, specified by its ID. - For example, if a lorebook has the ID `V1StGXR8_Z5jdHi6B-myT` and contains 151 entries, `{{lorebooksize::V1StGXR8_Z5jdHi6B-myT}}` resolves to `151`. - This is accessible from both the main chat and agent prompts. - If a lorebookID is corrupted/just pure gibberish, the macro resolves to 0. - If the pre-load fails inside macro-context.ts, all lorebooksize macros also resolve to 0. ## Validation <!-- Required: include commands run and/or manual checks performed --> - [x] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [ ] 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 <!-- Describe exactly what you tested in a real browser/app, step by step. --> <!-- ⚠️ If an AI agent filled out or ticked these boxes for you, treat them --> <!-- as a TO-DO LIST, not as proof the work is done. Verify each item yourself --> <!-- before submitting. If you haven't verified it, untick the box. --> - Tried the macro with a wrong id => resolved to 0 - Inserted `{{lorebooksize ::JparyXKfJPOF24kT_F0QC} }` inside a main chat prompt section => resolved to `151`, the correct number. - Inserted `{{lorebooksize ::JparyXKfJPOF24kT_F0QC}} + 1` inside an agent's prompt => Agent correctly output `152`. - Tried `{{setvar::size::{{lorebooksize ::JparyXKfJPOF24kT_F0QC}}}}` => setvar resolves before 1{{lorebooksize}}` in macro pipeline, => turns into 0? ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) 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) ## UI evidence (if applicable) ### Inside Main Prompt: <!-- Add before/after screenshots or recordings for UI changes --> <img width="400" height="250" alt="pre resolving" src="https://github.com/user-attachments/assets/7b5f1fa1-a23d-47f1-b2f8-acee4101112c" /> After: <img width="1195" height="222" alt="after resolving" src="https://github.com/user-attachments/assets/32568ebf-8c4f-4744-b5b6-900bc3462d00" /> ### Inside Agent Prompt: <img width="800" height="300" alt="agent pre resolve" src="https://github.com/user-attachments/assets/0cb5859b-6d61-470d-a915-bf18e2f94405" /> After: <img width="942" height="381" alt="inside agent order" src="https://github.com/user-attachments/assets/1692e10f-a958-4f2a-a4d2-dd4da9363c5e" /> ### Gibberish: <img width="727" height="578" alt="fallback pre-resolve" src="https://github.com/user-attachments/assets/d4344f92-c51a-4403-805d-dc415ce2708f" /> After: <img width="1082" height="190" alt="fallback resolve" src="https://github.com/user-attachments/assets/64a6225d-e086-4359-b65a-3333921dcabb" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added the `{{lorebooksize::ID}}` macro to return a lorebook’s total entry count. * Unknown lorebook IDs return `0`. * Counts include all entries, regardless of status or folder. * The macro is available wherever macros are supported. * **Documentation** * Added usage details for the new macro. * Documented the feature in the unreleased changelog. <!-- end of auto-generated comment: release notes by coderabbit.ai --> ### Maintainer verification - Added a direct macro regression and own-property fallback proof on the current head. - `pnpm check` passes on the current head. - Translated documentation follow-up: #5472. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:13:35 +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#6773
No description provided.