[PR #210] [MERGED] refactor(summarization): state rework, move to wrap pattern #209

Closed
opened 2026-02-16 06:17:27 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/210
Author: @hntrl
Created: 2/9/2026
Status: Merged
Merged: 2/10/2026
Merged by: @hntrl

Base: mainHead: hunter/summarization-refactor


📝 Commits (2)

  • 0245d13 refactor(summarization): state rework, move to wrap pattern
  • e3330b5 cr

📊 Changes

6 files changed (+828 additions, -423 deletions)

View changed files

.changeset/summarization-wrap-model-call.md (+7 -0)
📝 libs/deepagents/package.json (+3 -3)
📝 libs/deepagents/src/middleware/index.ts (+1 -0)
📝 libs/deepagents/src/middleware/summarization.test.ts (+261 -68)
📝 libs/deepagents/src/middleware/summarization.ts (+102 -21)
📝 pnpm-lock.yaml (+454 -331)

📄 Description

Summary

Ports deepagents PR #1138 to deepagentsjs: migrates createSummarizationMiddleware from the beforeModel hook to the wrapModelCall hook. Instead of mutating LangGraph state with RemoveMessage(REMOVE_ALL_MESSAGES) on each summarization, the middleware now tracks a SummarizationEvent in private state and reconstructs the effective message list on each call, avoiding full state rewrites.

Do not merge until https://github.com/langchain-ai/langchainjs/pull/9981 is merged and released.

Changes

libs/deepagents/src/middleware/summarization.ts

  • Added SummarizationEventSchema (Zod) and derived SummarizationEvent type tracking cutoffIndex, summaryMessage, and filePath
  • Added _summarizationEvent to SummarizationStateSchema using the proper Zod schema
  • Added getEffectiveMessages() to reconstruct the message list from prior summarization events (supports chained summarizations)
  • Replaced beforeModel hook with wrapModelCall hook:
    • No-op/truncation-only cases pass through to handler({...request, messages})
    • Summarization cases return Command({ update: { _summarizationEvent, _summarizationSessionId } })
  • Added chained cutoff index formula: previousEvent.cutoffIndex + cutoffIndex - 1

libs/deepagents/src/middleware/summarization.test.ts

  • Added callWrapModelCall() test helper that creates a ModelRequest, captures the handler's received request, and returns both the result and captured request
  • Converted all 17 existing tests from beforeModel to wrapModelCall pattern
  • Added "summarization event tracking" test suite (2 tests)
  • Added "chained summarization" test verifying cutoff index progression across 3 rounds (8 → 14 → 20 messages)

libs/deepagents/src/middleware/index.ts

  • Exported SummarizationEvent type

🔄 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/langchain-ai/deepagentsjs/pull/210 **Author:** [@hntrl](https://github.com/hntrl) **Created:** 2/9/2026 **Status:** ✅ Merged **Merged:** 2/10/2026 **Merged by:** [@hntrl](https://github.com/hntrl) **Base:** `main` ← **Head:** `hunter/summarization-refactor` --- ### 📝 Commits (2) - [`0245d13`](https://github.com/langchain-ai/deepagentsjs/commit/0245d139893e238fdfcee7348a873fc74349674f) refactor(summarization): state rework, move to wrap pattern - [`e3330b5`](https://github.com/langchain-ai/deepagentsjs/commit/e3330b57b291090ae23ad362549f0debf943e170) cr ### 📊 Changes **6 files changed** (+828 additions, -423 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/summarization-wrap-model-call.md` (+7 -0) 📝 `libs/deepagents/package.json` (+3 -3) 📝 `libs/deepagents/src/middleware/index.ts` (+1 -0) 📝 `libs/deepagents/src/middleware/summarization.test.ts` (+261 -68) 📝 `libs/deepagents/src/middleware/summarization.ts` (+102 -21) 📝 `pnpm-lock.yaml` (+454 -331) </details> ### 📄 Description ## Summary Ports [deepagents PR #1138](https://github.com/langchain-ai/deepagents/pull/1138) to deepagentsjs: migrates `createSummarizationMiddleware` from the `beforeModel` hook to the `wrapModelCall` hook. Instead of mutating LangGraph state with `RemoveMessage(REMOVE_ALL_MESSAGES)` on each summarization, the middleware now tracks a `SummarizationEvent` in private state and reconstructs the effective message list on each call, avoiding full state rewrites. Do not merge until https://github.com/langchain-ai/langchainjs/pull/9981 is merged and released. ## Changes ### `libs/deepagents/src/middleware/summarization.ts` - Added `SummarizationEventSchema` (Zod) and derived `SummarizationEvent` type tracking `cutoffIndex`, `summaryMessage`, and `filePath` - Added `_summarizationEvent` to `SummarizationStateSchema` using the proper Zod schema - Added `getEffectiveMessages()` to reconstruct the message list from prior summarization events (supports chained summarizations) - Replaced `beforeModel` hook with `wrapModelCall` hook: - No-op/truncation-only cases pass through to `handler({...request, messages})` - Summarization cases return `Command({ update: { _summarizationEvent, _summarizationSessionId } })` - Added chained cutoff index formula: `previousEvent.cutoffIndex + cutoffIndex - 1` ### `libs/deepagents/src/middleware/summarization.test.ts` - Added `callWrapModelCall()` test helper that creates a `ModelRequest`, captures the handler's received request, and returns both the result and captured request - Converted all 17 existing tests from `beforeModel` to `wrapModelCall` pattern - Added "summarization event tracking" test suite (2 tests) - Added "chained summarization" test verifying cutoff index progression across 3 rounds (8 → 14 → 20 messages) ### `libs/deepagents/src/middleware/index.ts` - Exported `SummarizationEvent` type --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 06:17:27 -05:00
yindo closed this issue 2026-02-16 06:17:27 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#209