[PR #5089] [MERGED] feat(mari): attach chat history to Professor Mari + Context Viewer (#5073) #6622

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5089
Author: @kolacheee
Created: 8/15/2026
Status: ✅ Merged
Merged: 8/15/2026
Merged by: @kolacheee

Base: staging ← Head: feat/5073-mari-chat-context


📝 Commits (6)

  • cceee53 feat(mari): server foundation for attached chat-history context (#5073)
  • d6b1d8d feat(mari): chat-history attach menu + picker + Context Viewer (#5073)
  • ec51324 fix(mari): address adversarial review of the #5073 chat-history client
  • 334d58a Merge remote-tracking branch 'origin/staging' into feat/5073-mari-chat-context
  • abb252c fix(mari): address CodeRabbit review of the #5073 chat-history context
  • 71c368f fix(mari): picker request-token + correct focus-trigger capture (#5073)

📊 Changes

17 files changed (+1274 additions, -36 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 package.json (+1 -1)
📝 packages/client/src/components/chat/HomeProfessorMariChat.tsx (+66 -35)
➕ packages/client/src/components/chat/MariAttachButton.tsx (+123 -0)
➕ packages/client/src/components/chat/MariChatHistoryPicker.tsx (+399 -0)
➕ packages/client/src/components/chat/MariContextViewer.tsx (+109 -0)
➕ packages/client/src/hooks/use-mari-workspace-context.ts (+71 -0)
➕ packages/client/src/hooks/use-modal-focus-trap.ts (+57 -0)
📝 packages/client/src/localization/locales/en.json (+32 -0)
📝 packages/server/src/db/file-backed-store.ts (+4 -0)
📝 packages/server/src/db/schema/index.ts (+1 -0)
➕ packages/server/src/db/schema/mari-workspace-context.ts (+35 -0)
📝 packages/server/src/routes/professor-mari-workspace.routes.ts (+39 -0)
➕ packages/server/src/services/professor-mari/mari-workspace-context-prompt.ts (+85 -0)
📝 packages/server/src/services/professor-mari/workspace-agent.service.ts (+19 -0)
➕ packages/server/src/services/storage/mari-workspace-context.storage.ts (+124 -0)
➕ scripts/regressions/mari-workspace-context.regression.ts (+108 -0)

📄 Description

Linked issue

Closes #5073

Why this change

Professor Mari can give creative feedback and make changes (characters, lorebooks, presets), but she can't see the roleplay/chat you're actually playing, so the feedback isn't grounded in it. This lets you attach slices of your chat history to a Mari conversation as reference context she can read.

Design (agreed with the maintainer's issue + a chosen approach): a dedicated, persistent per-Mari-chat context store rather than riding the one-off file-attachment channel — so an attached chat stays in context regardless of message age and is managed through a Context Viewer (add / remove to free tokens), which is exactly what the issue asks for.

What changed

Server

  • New mari_workspace_context table — chat-scoped items (kind, label, sourceChatId, content JSON, tokenEstimate), registered in FILE_BACKED_TABLES and cascaded from chats so attached context can't outlive its Mari chat.
  • Storage service (listForChat / create / remove) with a per-item content size guard.
  • A prompt renderer that inlines the attached transcripts as a preserved contextKind:'injection' block (survives the history-window trimmer, unlike per-message attachments), self-bounds its total size, and frames the content as user-provided evidence, not instructions (a prompt-injection line inside an attached chat is not obeyed). Wired into buildPromptMessages.
  • CRUD routes under /professor-mari/workspace/context (privileged-gated).

Client

  • The composer paperclip becomes a menu (shared by the floating + docked composers so it can't drift): Attach files or photos (unchanged), Add chat history, and View attached context (with a count badge).
  • Chat-history picker: a searchable, most-recent-first list of your chats → pick one → All / a Range / the Last N messages (system + hidden messages filtered out) → rough token estimate → attaches a lean {role, name, content, at} JSON. Blocks before the request when a selection exceeds the server size cap.
  • Context Viewer: lists everything attached to the current Mari chat with per-item + total token estimates and a remove control. Opening the picker/viewer ensures a Mari chat exists first.
  • React Query hooks (use-mari-workspace-context); all new copy localized in en.json.

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

Automated (done): pnpm check passes (TypeScript + ESLint + localization + build); the new mari-workspace-context regression passes (storage CRUD, chat scoping, size guard, and the injected block's transcript-inlining, evidence guardrail, and size bounding). The client was adversarially reviewed and the findings fixed (a Context-Viewer focus bug, a picker stale-response race, a label-vs-clamped-selection mismatch, a long-name label cap, and a dead "View attached context" control).

Still needs a manual pass in a running app (unchecked above on purpose):

  • Open the paperclip menu in Mari's composer (both the docked home view and the floating mobile button), add chat history from a chat (try All / a Range / Last N), and confirm Mari can quote/answer about the attached messages.
  • Open the Context Viewer, confirm the token counts and that Remove frees the item; confirm the count badge updates.
  • Verify light + dark mode and a mobile viewport for the menu, picker, and viewer.

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)

CHANGELOG updated under Added. No docs/ changes. No version bump. New table registered in FILE_BACKED_TABLES + cascade graph; no on-disk layout change to an existing table, so no storage-format.json / STORAGE_VERSION bump.

Summary by CodeRabbit

  • New Features
    • Professor Mari chats can now attach chat history as context.
    • Select all, recent, or specific ranges of messages from previous chats.
    • Review attached context, view token estimates, and remove items.
    • Attached context is included in relevant Professor Mari responses.
    • Improved attachment menu with file and context counts, loading states, and accessible controls.
  • Bug Fixes
    • Added safeguards for oversized context and invalid selections.
    • Context is automatically removed when its associated chat is deleted.

🔄 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/5089 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/15/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/5073-mari-chat-context` --- ### 📝 Commits (6) - [`cceee53`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cceee53dd7ace4856b6804205e9f874b800b163a) feat(mari): server foundation for attached chat-history context (#5073) - [`d6b1d8d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d6b1d8d094ee98fcfaf2798a4640c5741c755ae7) feat(mari): chat-history attach menu + picker + Context Viewer (#5073) - [`ec51324`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ec513248be95e1e8129ec45408e4033ec99c5b5c) fix(mari): address adversarial review of the #5073 chat-history client - [`334d58a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/334d58a7962cdc0ee5acff95c8884b9036150546) Merge remote-tracking branch 'origin/staging' into feat/5073-mari-chat-context - [`abb252c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/abb252c2fc5bba82760b73cddccf76b245ad4c9b) fix(mari): address CodeRabbit review of the #5073 chat-history context - [`71c368f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/71c368ffb9e7f7836b669c1720621160e60ae0e4) fix(mari): picker request-token + correct focus-trigger capture (#5073) ### 📊 Changes **17 files changed** (+1274 additions, -36 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `package.json` (+1 -1) 📝 `packages/client/src/components/chat/HomeProfessorMariChat.tsx` (+66 -35) ➕ `packages/client/src/components/chat/MariAttachButton.tsx` (+123 -0) ➕ `packages/client/src/components/chat/MariChatHistoryPicker.tsx` (+399 -0) ➕ `packages/client/src/components/chat/MariContextViewer.tsx` (+109 -0) ➕ `packages/client/src/hooks/use-mari-workspace-context.ts` (+71 -0) ➕ `packages/client/src/hooks/use-modal-focus-trap.ts` (+57 -0) 📝 `packages/client/src/localization/locales/en.json` (+32 -0) 📝 `packages/server/src/db/file-backed-store.ts` (+4 -0) 📝 `packages/server/src/db/schema/index.ts` (+1 -0) ➕ `packages/server/src/db/schema/mari-workspace-context.ts` (+35 -0) 📝 `packages/server/src/routes/professor-mari-workspace.routes.ts` (+39 -0) ➕ `packages/server/src/services/professor-mari/mari-workspace-context-prompt.ts` (+85 -0) 📝 `packages/server/src/services/professor-mari/workspace-agent.service.ts` (+19 -0) ➕ `packages/server/src/services/storage/mari-workspace-context.storage.ts` (+124 -0) ➕ `scripts/regressions/mari-workspace-context.regression.ts` (+108 -0) </details> ### 📄 Description ## Linked issue Closes #5073 ## Why this change Professor Mari can give creative feedback and make changes (characters, lorebooks, presets), but she can't see the roleplay/chat you're actually playing, so the feedback isn't grounded in it. This lets you attach slices of your chat history to a Mari conversation as reference context she can read. Design (agreed with the maintainer's issue + a chosen approach): a **dedicated, persistent per-Mari-chat context store** rather than riding the one-off file-attachment channel — so an attached chat stays in context regardless of message age and is managed through a Context Viewer (add / remove to free tokens), which is exactly what the issue asks for. ## What changed **Server** - New `mari_workspace_context` table — chat-scoped items (`kind`, `label`, `sourceChatId`, `content` JSON, `tokenEstimate`), registered in `FILE_BACKED_TABLES` and cascaded from `chats` so attached context can't outlive its Mari chat. - Storage service (`listForChat` / `create` / `remove`) with a per-item content size guard. - A prompt renderer that inlines the attached transcripts as a preserved `contextKind:'injection'` block (survives the history-window trimmer, unlike per-message attachments), self-bounds its total size, and frames the content as user-provided **evidence, not instructions** (a prompt-injection line inside an attached chat is not obeyed). Wired into `buildPromptMessages`. - CRUD routes under `/professor-mari/workspace/context` (privileged-gated). **Client** - The composer paperclip becomes a menu (shared by the floating + docked composers so it can't drift): *Attach files or photos* (unchanged), *Add chat history*, and *View attached context* (with a count badge). - Chat-history picker: a searchable, most-recent-first list of your chats → pick one → **All / a Range / the Last N** messages (system + hidden messages filtered out) → rough token estimate → attaches a lean `{role, name, content, at}` JSON. Blocks before the request when a selection exceeds the server size cap. - Context Viewer: lists everything attached to the current Mari chat with per-item + total token estimates and a remove control. Opening the picker/viewer ensures a Mari chat exists first. - React Query hooks (`use-mari-workspace-context`); all new copy localized in `en.json`. ## Validation - [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 Automated (done): `pnpm check` passes (TypeScript + ESLint + localization + build); the new `mari-workspace-context` regression passes (storage CRUD, chat scoping, size guard, and the injected block's transcript-inlining, evidence guardrail, and size bounding). The client was adversarially reviewed and the findings fixed (a Context-Viewer focus bug, a picker stale-response race, a label-vs-clamped-selection mismatch, a long-name label cap, and a dead "View attached context" control). Still needs a manual pass in a running app (unchecked above on purpose): - Open the paperclip menu in Mari's composer (both the docked home view and the floating mobile button), add chat history from a chat (try All / a Range / Last N), and confirm Mari can quote/answer about the attached messages. - Open the Context Viewer, confirm the token counts and that Remove frees the item; confirm the count badge updates. - Verify light + dark mode and a mobile viewport for the menu, picker, and viewer. ## Docs and release impact - [ ] No docs changes needed - [x] 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) CHANGELOG updated under Added. No `docs/` changes. No version bump. New table registered in `FILE_BACKED_TABLES` + cascade graph; no on-disk layout change to an existing table, so no `storage-format.json` / `STORAGE_VERSION` bump. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Professor Mari chats can now attach chat history as context. * Select all, recent, or specific ranges of messages from previous chats. * Review attached context, view token estimates, and remove items. * Attached context is included in relevant Professor Mari responses. * Improved attachment menu with file and context counts, loading states, and accessible controls. * **Bug Fixes** * Added safeguards for oversized context and invalid selections. * Context is automatically removed when its associated chat is deleted. <!-- 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-23 04:12:09 +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#6622
No description provided.