Skip to content

feat(SDK): Integrate context summarization with conversation memory BZ-43344 - #89

Merged
murdore merged 1 commit into
juspay:releasefrom
sahiltyagiii:BZ-43344-integrating-context-summarization-with-conversation-memory
Aug 29, 2025
Merged

murdore merged 1 commit into
juspay:releasefrom
sahiltyagiii:BZ-43344-integrating-context-summarization-with-conversation-memory

Conversation

@sahiltyagiii

@sahiltyagiii sahiltyagiii commented Aug 18, 2025 •

Copy link
Copy Markdown
Contributor

Pull Request

Description

This PR integrates a new Automatic Context Summarization feature into the existing Conversation Memory system. This enhancement allows the SDK to automatically summarize long-running conversations, preventing context overflow errors while preserving key information.

The summarization logic hooks into the conversation memory lifecycle, triggering before the standard turn-based truncation to ensure a more intelligent context management strategy.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🧹 Code refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧪 Test coverage improvement
  • 🔧 Build/CI configuration change

Related Issues

  • Fixes #
  • Related to #

Changes Made

  • Integrated Summarization into Conversation Memory: The summarization logic is now part of the ConversationMemoryManager, activated by a new enableSummarization flag in the conversationMemory config.

  • Corrected Order of Operations: The manager now adds a new turn, checks for summarization, and then performs truncation, fixing a race condition that caused premature context loss.

  • Configuration via Constructor: The feature is configured through the existing conversationMemory object in the NeuroLink constructor.

  • Recursion-Safe Design: The summarization call uses a new, memory-disabled NeuroLink instance to prevent infinite loops.

  • Documentation Updated: All relevant documentation has been updated to reflect the new integrated feature.

AI Provider Impact

  • OpenAI
  • Anthropic
  • Google AI/Vertex
  • AWS Bedrock
  • Azure OpenAI
  • Hugging Face
  • Ollama
  • Mistral
  • All providers
  • No provider-specific changes

Component Impact

  • CLI
  • SDK
  • MCP Integration
  • Streaming
  • Tool Calling
  • Configuration
  • Documentation
  • Tests

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • E2E tests added/updated
  • Manual testing performed
  • All existing tests pass

Test Environment

  • OS: macOS
  • Node.js version: v24.5.0
  • Package manager: pnpm

Performance Impact

  • No performance impact
  • Performance improvement
  • Minor performance impact (acceptable)
  • Significant performance impact (needs discussion)

Breaking Changes

None. The feature is opt-in through the existing conversationMemory configuration and does not change any public APIs.

Screenshots/Demo

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Additional Notes

Summary by CodeRabbit

  • New Features

    • Programmatically add MCP servers at runtime via a new API.
    • Optional automatic conversation summarization per session, configurable with thresholds, target turns, provider, and model; supports environment-based defaults.
    • Session-aware generation with optional sessionId to persist conversation memory.
  • Refactor

    • Removed the legacy enableContextSummarization method; use the conversationMemory configuration in the constructor instead.
  • Documentation

    • Updated API reference to include MCP server addition and remove the deprecated method.
    • Renamed and expanded guides to “Conversation Memory & Summarization,” with new examples and clarified workflow.

@coderabbitai

coderabbitai Bot commented Aug 18, 2025 •

Copy link
Copy Markdown

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbit review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Removes the legacy ContextManager and its configs/types/utilities. Introduces turn-based conversation summarization integrated into ConversationMemoryManager and conversationMemory config. Updates docs to reflect constructor-based activation, session scoping, and new addMCPServer API. Adjusts utils/config/types to support summarization; cleans neurolink.ts of enableContextSummarization.

Changes

Cohort / File(s) Summary of Changes
API Reference Updates
docs/API-REFERENCE.md
Removed enableContextSummarization(...) docs; added async addMCPServer(serverId, config) docs with signature and example.
Conversation Summarization Docs
docs/CONTEXT-SUMMARIZATION.md
Renamed feature to Automatic Conversation Summarization; moved enablement to NeuroLink constructor conversationMemory; switched to turn-based thresholds; added order of operations and updated examples; removed legacy word-based options.
Memory Bank Docs Refresh
memory-bank/activeContext.md, memory-bank/progress.md, memory-bank/systemPatterns.md
Updated status, architecture, and workflow to integrated ConversationMemoryManager; documented sequencing (add → summarize → truncate); moved/expanded issues; added constructor-based activation and session handling.
Remove Legacy ContextManager
src/lib/context/ContextManager.ts, src/lib/context/config.ts, src/lib/context/types.ts, src/lib/context/utils.ts
Deleted ContextManager implementation, default config/prompt utilities, related types (ChatMessage, ContextManagerConfig), and formatHistoryToString helper.
Conversation Memory Integration
src/lib/core/conversationMemoryManager.ts
Added per-session summarization after threshold; summary replaces older history with a system message + recent turns; added getSession, publicized getStats/clear*; added internal summarization helpers; consistent session limit enforcement.
Summarization Config Defaults
src/lib/config/conversationMemoryConfig.ts
Added env-driven defaults: enableSummarization, summarizationThresholdTurns, summarizationTargetTurns, summarizationProvider, summarizationModel.
Public Types Expansion
src/lib/types/conversationTypes.ts
Extended ConversationMemoryConfig with optional summarization fields (toggle, thresholds, provider, model).
Conversation Memory Utilities
src/lib/utils/conversationMemoryUtils.ts
Added optional summarization during message retrieval; helper to summarize session and create prompts; simplified default merging.
NeuroLink Core Cleanup
src/lib/neurolink.ts
Removed enableContextSummarization API and all ContextManager integration from generation flow.
Type Import Cleanup
src/lib/types/generateTypes.ts
Removed unused ContextManagerConfig import; no public API changes.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant NeuroLink
  participant ConversationMemoryManager as ConvMemory
  participant Summarizer as NeuroLink(summarize)

  Client->>NeuroLink: generate({ input, context.sessionId })
  NeuroLink->>ConvMemory: storeConversationTurn(sessionId, user/assistant)
  alt Summarization enabled & threshold exceeded
    ConvMemory->>Summarizer: generate(summary prompt, provider/model)
    Summarizer-->>ConvMemory: summary text
    ConvMemory->>ConvMemory: replace older history with system(summary) + recent turns
  else Below threshold
    ConvMemory->>ConvMemory: keep history, enforce limits
  end
  NeuroLink->>ConvMemory: buildContextMessages(sessionId)
  ConvMemory-->>NeuroLink: messages (possibly with system summary)
  NeuroLink-->>Client: generate result
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

I tidied my warren of turns and tales,
Snipped old threads, stitched summaries’ trails.
Sessions hop lightly, memories trim,
Context once bulky now sleek and slim.
New MCP burrows open on cue—
Thump-thump! A rabbit shipping something new. 🐇✨

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sahiltyagiii
sahiltyagiii force-pushed the BZ-43344-integrating-context-summarization-with-conversation-memory branch 2 times, most recently from 466cc61 to 3b222c8 Compare August 18, 2025 13:38
@murdore
murdore requested a review from Copilot August 18, 2025 16:08

This comment was marked as outdated.

@sahiltyagiii
sahiltyagiii force-pushed the BZ-43344-integrating-context-summarization-with-conversation-memory branch from 3b222c8 to 995f62f Compare August 19, 2025 07:43
@sahiltyagiii
sahiltyagiii requested a review from Copilot August 19, 2025 07:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR integrates automatic context summarization functionality into the existing conversation memory system. The change replaces a standalone context management approach with a unified system that manages conversation history and summarization within the ConversationMemoryManager.

  • Removes the legacy enableContextSummarization method and related ContextManager class
  • Integrates summarization logic directly into ConversationMemoryManager with proper turn-based ordering
  • Configures summarization through the existing conversationMemory constructor option

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/lib/utils/conversationMemoryUtils.ts Adds summarization logic and helper functions to conversation memory utilities
src/lib/types/generateTypes.ts Removes unused import of deprecated ContextManagerConfig
src/lib/types/conversationTypes.ts Extends ConversationMemoryConfig with summarization options
src/lib/neurolink.ts Removes deprecated enableContextSummarization method and related context manager code
src/lib/core/types.ts Adds conversationMemoryConfig to TextGenerationOptions
src/lib/core/conversationMemoryManager.ts Integrates summarization logic with proper turn-based ordering and refactors methods
src/lib/context/* Removes deprecated context management files
src/lib/config/conversationMemoryConfig.ts Adds environment variable support for summarization configuration
memory-bank/* Updates documentation to reflect new integrated approach
docs/* Updates API documentation and guides for the new unified system

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/lib/utils/conversationMemoryUtils.ts Outdated
Comment thread src/lib/core/conversationMemoryManager.ts Outdated
@sahiltyagiii
sahiltyagiii force-pushed the BZ-43344-integrating-context-summarization-with-conversation-memory branch 3 times, most recently from cb443df to d3701cb Compare August 22, 2025 11:05
@sahiltyagiii
sahiltyagiii force-pushed the BZ-43344-integrating-context-summarization-with-conversation-memory branch 4 times, most recently from ca4727a to ee2b428 Compare August 29, 2025 10:49
@sahiltyagiii
sahiltyagiii force-pushed the BZ-43344-integrating-context-summarization-with-conversation-memory branch from ee2b428 to 904a4e7 Compare August 29, 2025 11:16
Comment thread src/lib/utils/conversationMemoryUtils.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants