[PR #189] [MERGED] fix(summarization): resolve fraction trigger bug by using model profile for maxInputTokens #192

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

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/189
Author: @christian-bromann
Created: 2/4/2026
Status: Merged
Merged: 2/4/2026
Merged by: @christian-bromann

Base: mainHead: cb/summarization-fix


📝 Commits (2)

  • befb2c1 fix(summarization): resolve fraction trigger bug by using model profile for maxInputTokens
  • ce8059d fix type

📊 Changes

2 files changed (+206 additions, -28 deletions)

View changed files

📝 libs/deepagents/src/middleware/summarization.test.ts (+128 -9)
📝 libs/deepagents/src/middleware/summarization.ts (+78 -19)

📄 Description

Fixes a bug where the fraction trigger type in createSummarizationMiddleware was always ineffective because maxInputTokens was never passed to the internal functions.

Before: Fraction-based triggers (e.g., { type: "fraction", value: 0.85 }) never fired because maxInputTokens was always undefined.

After: The middleware automatically retrieves maxInputTokens from the model's profile, aligning with the Python implementation.

Changes

  • Use initChatModel: Replaced ChatOpenAI with initChatModel from langchain/chat_models/universal to resolve string model names. This supports any provider (OpenAI, Anthropic, Google, etc.)
  • Automatic maxInputTokens resolution: Get maxInputTokens from model.profile.maxInputTokens (similar to Python's _get_profile_limits)
  • Removed explicit maxInputTokens option: No longer needed since it's derived from the model profile
  • Model caching: Cache the resolved model to avoid repeated initChatModel calls on each beforeModel invocation
  • Updated tests: Added tests for fraction triggers and updated mocks to use initChatModel

Usage

// Works with any model provider - maxInputTokens comes from profile automatically
const middleware = createSummarizationMiddleware({
  model: "anthropic:claude-opus-4-0", // or "openai:gpt-4", etc.
  backend,
  trigger: { type: "fraction", value: 0.85 },
  keep: { type: "fraction", value: 0.10 },
});

// Or with model instance
const model = new ChatAnthropic({ model: "claude-opus-4-0" });
const middleware = createSummarizationMiddleware({
  model,
  backend,
  trigger: { type: "fraction", value: 0.85 },
});

fixes #185


🔄 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/189 **Author:** [@christian-bromann](https://github.com/christian-bromann) **Created:** 2/4/2026 **Status:** ✅ Merged **Merged:** 2/4/2026 **Merged by:** [@christian-bromann](https://github.com/christian-bromann) **Base:** `main` ← **Head:** `cb/summarization-fix` --- ### 📝 Commits (2) - [`befb2c1`](https://github.com/langchain-ai/deepagentsjs/commit/befb2c11224e61e6d86c2a3d79b452470e0ee3a6) fix(summarization): resolve fraction trigger bug by using model profile for maxInputTokens - [`ce8059d`](https://github.com/langchain-ai/deepagentsjs/commit/ce8059dcbb8294bf483703ed3eb9f023423a3033) fix type ### 📊 Changes **2 files changed** (+206 additions, -28 deletions) <details> <summary>View changed files</summary> 📝 `libs/deepagents/src/middleware/summarization.test.ts` (+128 -9) 📝 `libs/deepagents/src/middleware/summarization.ts` (+78 -19) </details> ### 📄 Description Fixes a bug where the `fraction` trigger type in `createSummarizationMiddleware` was always ineffective because `maxInputTokens` was never passed to the internal functions. **Before:** Fraction-based triggers (e.g., `{ type: "fraction", value: 0.85 }`) never fired because `maxInputTokens` was always `undefined`. **After:** The middleware automatically retrieves `maxInputTokens` from the model's profile, aligning with the Python implementation. ## Changes - **Use `initChatModel`**: Replaced `ChatOpenAI` with `initChatModel` from `langchain/chat_models/universal` to resolve string model names. This supports any provider (OpenAI, Anthropic, Google, etc.) - **Automatic `maxInputTokens` resolution**: Get `maxInputTokens` from `model.profile.maxInputTokens` (similar to Python's `_get_profile_limits`) - **Removed explicit `maxInputTokens` option**: No longer needed since it's derived from the model profile - **Model caching**: Cache the resolved model to avoid repeated `initChatModel` calls on each `beforeModel` invocation - **Updated tests**: Added tests for fraction triggers and updated mocks to use `initChatModel` ## Usage ```typescript // Works with any model provider - maxInputTokens comes from profile automatically const middleware = createSummarizationMiddleware({ model: "anthropic:claude-opus-4-0", // or "openai:gpt-4", etc. backend, trigger: { type: "fraction", value: 0.85 }, keep: { type: "fraction", value: 0.10 }, }); // Or with model instance const model = new ChatAnthropic({ model: "claude-opus-4-0" }); const middleware = createSummarizationMiddleware({ model, backend, trigger: { type: "fraction", value: 0.85 }, }); ``` fixes #185 --- <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:24 -05:00
yindo closed this issue 2026-02-16 06:17:24 -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#192