mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #295] [MERGED] feat(agents): add Knowledge Router — lower-cost lorebook routing agent #4551
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#4551
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/295
Author: @cha1latte
Created: 4/28/2026
Status: ✅ Merged
Merged: 4/28/2026
Merged by: @SpicyMarinara
Base:
main← Head:feat/knowledge-router-agent📝 Commits (10+)
33eebeefeat(agents): register knowledge-router agent type and default prompt2fa39d6feat(agents): add knowledge-router execution function and catalog injection17dd7fcfeat(generate): wire knowledge-router into the generation pipeline496f70atest(agents): add unit tests for knowledge-router helpers1d19c51chore(scripts): add cost-model script for knowledge-router PR074b9f0feat(client): expose lorebook source picker for the knowledge-router agentd4d7c83fix(knowledge-router): harden against prompt injection and duplicate IDs8c6a869fix(generate): make router optional, restore regen cache, honor chat overridesd46c6fefix(client): don't persist sourceFileIds for the knowledge-router agent4f4b990chore(scripts): replace console.log with process.stdout.write in benchmark📊 Changes
8 files changed (+1053 additions, -54 deletions)
View changed files
📝
packages/client/src/components/agents/AgentEditor.tsx(+19 -5)➕
packages/server/scripts/benchmark-knowledge-router.ts(+206 -0)📝
packages/server/src/routes/generate.routes.ts(+233 -49)📝
packages/server/src/services/agents/agent-executor.ts(+6 -0)➕
packages/server/src/services/agents/knowledge-router.ts(+247 -0)➕
packages/server/test/knowledge-router.test.ts(+305 -0)📝
packages/shared/src/constants/agent-prompts.ts(+26 -0)📝
packages/shared/src/types/agent.ts(+11 -0)📄 Description
Part 2 of #226 — the Knowledge Router agent itself.
Summary
Adds a new built-in pre-generation agent, Knowledge Router, as a lower-cost alternative to the existing Knowledge Retrieval agent for large lorebooks.
Where Knowledge Retrieval reads the full content of every enabled entry from the source lorebooks and summarizes it (chunking when needed), the router reads a short catalog (one row per entry: id, name, keys, summary) and returns the IDs of the entries it thinks are relevant. Selected entries are then injected verbatim — no per-entry summarization pass.
Why
For large lorebooks (One Piece, Wheel of Time, etc.) the existing Knowledge Retrieval agent gets expensive — it has to summarize every enabled entry on every turn, often across multiple chunked LLM calls. The router avoids that by routing between entries instead of summarizing them.
The two agents complement each other rather than replacing one another. Power users with smaller, well-curated lorebooks may still prefer Retrieval for its summarization quality; users with large lorebooks get a cheaper option.
How it works (short version)
constant: trueentries are skipped (they already inject unconditionally via the standard activation pipeline — routing them would duplicate work).<entry id="..." name="..." keys="...">summary</entry>.summaryuses the entry'sdescriptionfield (added in #227) when non-empty, otherwise falls back to the first ~60 tokens of the entry's content. This keeps the router useful out of the box on existing lorebooks while letting power users tune precision via descriptions.{ "entryIds": [...] }.<knowledge_router>(or## Knowledge Routerunder markdown wrap), parallel to how<knowledge_retrieval>is injected.The router runs in parallel with Knowledge Retrieval and the standard pre-gen agent pipeline via the same
Promise.allgate — so enabling both adds at most the slower of the two latencies, not their sum.Architecture (commit-by-commit)
Feature commits:
33eebee2fa39d6executeKnowledgeRouter+<entry_catalog>injection in agent-executor17dd7fc496f70a1d19c51074b9f0Review-feedback commits (CodeRabbit passes 1 & 2):
d4d7c838c6a869d46c6fe4f4b990372b82bcea6e1fCost model
Modeled (not benchmarked) input/output tokens for both agents at three lorebook sizes. The script (
packages/server/scripts/benchmark-knowledge-router.ts) uses the realbuildCataloghelper to measure actual catalog sizes, then estimates LLM costs analytically. All assumptions documented at the top of the file so reviewers can challenge or re-run with their own numbers.Default-assumption results (~50% description coverage, 800-char avg entry content):
The 500-entry case is the killer — KR makes 18 sequential LLM calls, Router makes 1.
Known limitations
chatMeta.entryStateOverrides. The router's loader does (added in this PR after CodeRabbit feedback) — KR's existing loader has the same gap and should be brought in line in a follow-up. Not addressed here to keep this PR's scope tight. (Note: KR's parallel-promise rejection bug was incidentally fixed incea6e1fsince the try/catch pattern was trivial to apply to both.)Test plan
Manual smoke tests performed on a fresh dev install (
pnpm dev), Roleplay chat, 5-entry test lorebook ("Bingus Bongus studios") with mixed description coverage (3/5 with description, 2/5 with blank description to exercise the content fallback):[Agent] ✓ Knowledge Router (knowledge-router) — 10.1swith verbatim entry text.[Agent] ✓ Knowledge Routerline across original + regen.constant: trueentries are filtered before reaching the router. Flipped Beans to Constant; router's text payload no longer contained### Beans the Salamander. Beans content still appeared in the response (via the standard activation pipeline).pnpm checkpasses locally.packages/server/test/knowledge-router.test.ts).Summary by CodeRabbit
New Features
Bug Fixes
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.