[PR #929] [MERGED] Add a server lorebook embedding service #4841

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/929
Author: @Promansis
Created: 5/17/2026
Status: ✅ Merged
Merged: 5/17/2026
Merged by: @SpicyMarinara

Base: staging ← Head: staging


📝 Commits (4)

  • 22bf470 Add a server lorebook embedding service
  • 7e941c9 Merge branch 'staging' into staging
  • b3e6a3a fix: address knowledge router review feedback
  • 42da248 Merge remote-tracking branch 'origin/staging' into review-pr-929

📊 Changes

9 files changed (+931 additions, -59 deletions)

View changed files

📝 packages/client/src/components/chat/ChatSettingsDrawer.tsx (+14 -3)
📝 packages/server/src/routes/generate.routes.ts (+102 -45)
📝 packages/server/src/services/agents/knowledge-router.ts (+120 -3)
➕ packages/server/src/services/lorebook/embeddings.ts (+141 -0)
📝 packages/server/src/services/lorebook/index.ts (+11 -4)
📝 packages/server/src/services/prompt/assembler.ts (+11 -0)
📝 packages/server/test/knowledge-router.test.ts (+234 -4)
➕ packages/server/test/lorebook-embeddings.test.ts (+238 -0)
📝 packages/server/test/lorebook-processing.test.ts (+60 -0)

📄 Description

Add cosine semantic search helpers for lorebook entries Update Knowledge Router candidate loading

Linked issue

Closes #927

Why this change

  • Knowledge Router can become noisy or expensive when it receives a broad source lorebook catalog.
  • Exact keyword matching is not enough for lorebooks where relevant entries are implied by the scene rather than directly named.
  • The router needs a candidate-loading step that narrows large lorebooks semantically while preserving the existing deterministic lorebook activation rules.

What changed

  • Added packages/server/src/services/lorebook/embeddings.ts for lorebook embedding text generation, embedding warmup, cosine similarity, and semantic top-K entry search.
  • Reused the existing memory recall embedding source so lorebook semantic search follows the app's configured embedding path.
  • Updated generation to warm missing embeddings for active lorebook entries in bounded batches.
  • Added Knowledge Router candidate preparation that builds a query from recent messages, chat summary, and game state.
  • Merged semantic matches with keyword-activated lorebook entries, deduping entries while keeping semantic matches first.
  • Passed scan context into the router path so character filters, tags, generation triggers, and game state remain respected.
  • Stopped excluding constant entries from the router candidate list before candidate preparation, allowing already-activated constant entries to be represented in router candidates when appropriate.
  • Updated lorebook scan results to expose matched keys for activated entries so the router can distinguish keyword activations from semantic-style matches.
  • Added server tests for lorebook embedding helpers and Knowledge Router candidate preparation/fallback behavior.
  • Adjusted the chat settings drawer section header from a native button to a keyboard-accessible role="button" wrapper so nested help controls can be clicked without toggling the whole section.

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

  • Run pnpm check.
  • Verify the new lorebook embedding test file compiles.
  • Create or use a chat with a Knowledge Router agent and a source lorebook containing many entries.
  • Verify exact keyword-triggered entries are still available to the router.
  • Verify semantically related entries can be shortlisted even when the exact key is not present in recent chat text.
  • Verify generation continues when embeddings are unavailable or stored vectors have mismatched dimensions.
  • Verify disabled entries and exhausted ephemeral entries are not routed.
  • Verify character/tag/generation-trigger filters still affect router candidates.
  • In chat settings, verify collapsible drawer sections toggle with mouse, Enter, and Space, and that help tooltips remain clickable.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

N/A

Summary by CodeRabbit

  • New Features

    • Improved keyboard accessibility for chat settings drawer—now supports Enter and Space keys to toggle sections.
  • Bug Fixes

    • Enhanced lorebook system to prevent redundant content activation and improved semantic matching for more relevant contextual information retrieval.

Review Change Stack


🔄 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/929 **Author:** [@Promansis](https://github.com/Promansis) **Created:** 5/17/2026 **Status:** ✅ Merged **Merged:** 5/17/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `staging` --- ### 📝 Commits (4) - [`22bf470`](https://github.com/Pasta-Devs/Marinara-Engine/commit/22bf4706fd7ce0ea6b18c24673361d9216cc4f6c) Add a server lorebook embedding service - [`7e941c9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7e941c94326ffde69219f2614daf95af6434d34c) Merge branch 'staging' into staging - [`b3e6a3a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b3e6a3aefcf916c1f8996cb0f3dbe303cc8899c9) fix: address knowledge router review feedback - [`42da248`](https://github.com/Pasta-Devs/Marinara-Engine/commit/42da248f691dbce09d13b74983bd07a1af2f1205) Merge remote-tracking branch 'origin/staging' into review-pr-929 ### 📊 Changes **9 files changed** (+931 additions, -59 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/chat/ChatSettingsDrawer.tsx` (+14 -3) 📝 `packages/server/src/routes/generate.routes.ts` (+102 -45) 📝 `packages/server/src/services/agents/knowledge-router.ts` (+120 -3) ➕ `packages/server/src/services/lorebook/embeddings.ts` (+141 -0) 📝 `packages/server/src/services/lorebook/index.ts` (+11 -4) 📝 `packages/server/src/services/prompt/assembler.ts` (+11 -0) 📝 `packages/server/test/knowledge-router.test.ts` (+234 -4) ➕ `packages/server/test/lorebook-embeddings.test.ts` (+238 -0) 📝 `packages/server/test/lorebook-processing.test.ts` (+60 -0) </details> ### 📄 Description Add cosine semantic search helpers for lorebook entries Update Knowledge Router candidate loading ## Linked issue <!-- Every PR should reference a feature request or issue report. If there isn't one yet, open one first to gather opinions: --> <!-- https://github.com/Pasta-Devs/Marinara-Engine/issues/new/choose --> Closes #927 ## Why this change <!-- What user problem, bug, or goal does this solve? --> - Knowledge Router can become noisy or expensive when it receives a broad source lorebook catalog. - Exact keyword matching is not enough for lorebooks where relevant entries are implied by the scene rather than directly named. - The router needs a candidate-loading step that narrows large lorebooks semantically while preserving the existing deterministic lorebook activation rules. ## What changed <!-- List the key changes in this PR --> - Added `packages/server/src/services/lorebook/embeddings.ts` for lorebook embedding text generation, embedding warmup, cosine similarity, and semantic top-K entry search. - Reused the existing memory recall embedding source so lorebook semantic search follows the app's configured embedding path. - Updated generation to warm missing embeddings for active lorebook entries in bounded batches. - Added Knowledge Router candidate preparation that builds a query from recent messages, chat summary, and game state. - Merged semantic matches with keyword-activated lorebook entries, deduping entries while keeping semantic matches first. - Passed scan context into the router path so character filters, tags, generation triggers, and game state remain respected. - Stopped excluding constant entries from the router candidate list before candidate preparation, allowing already-activated constant entries to be represented in router candidates when appropriate. - Updated lorebook scan results to expose matched keys for activated entries so the router can distinguish keyword activations from semantic-style matches. - Added server tests for lorebook embedding helpers and Knowledge Router candidate preparation/fallback behavior. - Adjusted the chat settings drawer section header from a native button to a keyboard-accessible `role="button"` wrapper so nested help controls can be clicked without toggling the whole section. ## Validation <!-- Required: include commands run and/or manual checks performed --> - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] 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. --> - [x] Run `pnpm check`. - [x] Verify the new lorebook embedding test file compiles. - [x] Create or use a chat with a Knowledge Router agent and a source lorebook containing many entries. - [x] Verify exact keyword-triggered entries are still available to the router. - [x] Verify semantically related entries can be shortlisted even when the exact key is not present in recent chat text. - [x] Verify generation continues when embeddings are unavailable or stored vectors have mismatched dimensions. - [x] Verify disabled entries and exhausted ephemeral entries are not routed. - [x] Verify character/tag/generation-trigger filters still affect router candidates. - [x] In chat settings, verify collapsible drawer sections toggle with mouse, Enter, and Space, and that help tooltips remain clickable. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) <!-- Add before/after screenshots or recordings for UI changes --> N/A <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Improved keyboard accessibility for chat settings drawer—now supports Enter and Space keys to toggle sections. * **Bug Fixes** * Enhanced lorebook system to prevent redundant content activation and improved semantic matching for more relevant contextual information retrieval. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/Pasta-Devs/Marinara-Engine/pull/929?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- 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:11:06 +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#4841
No description provided.