feat(core): persist assistant message metadata in memory#1183
feat(core): persist assistant message metadata in memory#1183
Conversation
🦋 Changeset detectedLatest commit: c8378ed The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This comment has been minimized.
This comment has been minimized.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds an option to persist assistant message metadata (usage and finishReason) into memory-backed UIMessage.metadata; includes type/schema support, runtime option resolution, agent persistence logic for generate/stream flows, tests, changeset, and documentation updates. Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client
participant Agent as Agent
participant Provider as ModelProvider
participant Memory as MemoryStore
Client->>Agent: generateText / streamText (with memory.options.messageMetadataPersistence)
Agent->>Provider: invoke model generation / stream
Provider-->>Agent: response (+ provider usage, finishReason on finish)
Agent->>Agent: build persistedMetadata { operationId, usage, finishReason }
Agent->>Memory: persist assistant UIMessage with metadata
Memory-->>Agent: ack saved message
Agent-->>Client: return response (stream or final)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/server-core/src/schemas/agent.schemas.spec.ts (1)
97-112: Add legacy top-level object-form test for parity.The backward-compatibility test currently covers only boolean. Since the accepted type is
boolean | object, add a case for the deprecated top-level object form too.Suggested test addition
it("keeps legacy top-level memory fields for backward compatibility", () => { const payload = { userId: "legacy-user", conversationId: "legacy-conv", contextLimit: 10, semanticMemory: { enabled: true, }, conversationPersistence: { mode: "finish", }, messageMetadataPersistence: true, }; expect(() => GenerateOptionsSchema.parse(payload)).not.toThrow(); }); + + it("accepts legacy top-level messageMetadataPersistence object form", () => { + const payload = { + userId: "legacy-user", + conversationId: "legacy-conv", + messageMetadataPersistence: { + usage: true, + finishReason: true, + }, + }; + + expect(() => GenerateOptionsSchema.parse(payload)).not.toThrow(); + });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/server-core/src/schemas/agent.schemas.spec.ts` around lines 97 - 112, Add a new test case to cover the deprecated top-level object form of messageMetadataPersistence: create a payload similar to the existing legacy test but set messageMetadataPersistence to an object (e.g., { enabled: true }) and assert GenerateOptionsSchema.parse(payload) does not throw; locate the test suite around the existing "keeps legacy top-level memory fields for backward compatibility" spec and add this new it(...) using GenerateOptionsSchema.parse to validate parity with the boolean case.website/docs/ui/ai-sdk-integration.md (1)
364-366: Consider documenting boolean semantics explicitly (true/falseand default).The table and paragraph are good, but adding one sentence clarifying what
truepersists, whatfalsedoes, and the default behavior would reduce ambiguity.Also applies to: 403-404
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@website/docs/ui/ai-sdk-integration.md` around lines 364 - 366, Update the docs for memory.options.messageMetadataPersistence and its sub-properties (memory.options.messageMetadataPersistence.usage and memory.options.messageMetadataPersistence.finishReason) to include a single sentence that states the boolean semantics: what happens when set to true (persist the specified metadata), what happens when set to false (do not persist it), and the default value when the option is omitted; ensure the sentence appears alongside the existing table entries so readers see for each symbol whether the default is true or false and the exact effect of true vs false.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/server-core/src/schemas/agent.schemas.spec.ts`:
- Around line 97-112: Add a new test case to cover the deprecated top-level
object form of messageMetadataPersistence: create a payload similar to the
existing legacy test but set messageMetadataPersistence to an object (e.g., {
enabled: true }) and assert GenerateOptionsSchema.parse(payload) does not throw;
locate the test suite around the existing "keeps legacy top-level memory fields
for backward compatibility" spec and add this new it(...) using
GenerateOptionsSchema.parse to validate parity with the boolean case.
In `@website/docs/ui/ai-sdk-integration.md`:
- Around line 364-366: Update the docs for
memory.options.messageMetadataPersistence and its sub-properties
(memory.options.messageMetadataPersistence.usage and
memory.options.messageMetadataPersistence.finishReason) to include a single
sentence that states the boolean semantics: what happens when set to true
(persist the specified metadata), what happens when set to false (do not persist
it), and the default value when the option is omitted; ensure the sentence
appears alongside the existing table entries so readers see for each symbol
whether the default is true or false and the exact effect of true vs false.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 097b7f21-8470-4675-ae75-852936105616
📒 Files selected for processing (7)
.changeset/funny-ravens-wave.mdpackages/core/src/agent/agent.spec.tspackages/core/src/agent/agent.tspackages/core/src/agent/types.tspackages/server-core/src/schemas/agent.schemas.spec.tspackages/server-core/src/schemas/agent.schemas.tswebsite/docs/ui/ai-sdk-integration.md
PR Checklist
Please check if your PR fulfills the following requirements:
Bugs / Features
What is the current behavior?
messageMetadataonly affects the live UI stream. When messages are later fetched from memory,UIMessage.metadataonly contains the default persisted metadata such asoperationId.What is the new behavior?
messageMetadataPersistencecan now be configured at the agent level or per call viamemory.options.messageMetadataPersistence(or the legacy top-levelmessageMetadataPersistence). When enabled, VoltAgent persists assistantusageand/orfinishReasoninto memory-backedUIMessage.metadataforgenerateText,streamText,generateObject, andstreamObject.fixes (issue)
N/A
Notes for reviewers
UIMessages in object flows.messageMetadataPersistencethrough resolved memory overrides.pnpm vitest run src/agent/agent.spec.ts --typecheckinpackages/core.pnpm vitest run src/schemas/agent.schemas.spec.tsinpackages/server-core.pnpm buildinpackages/core.pnpm buildinpackages/server-core.Summary by CodeRabbit
New Features
Tests
Documentation