65db3a4fcd
* Add status bar and compact summary widgets with context window resolution - Implemented a shared status bar for CLI and TUI frontends, including helpers for managing session metrics and context windows. - Created a `CompactSummaryWidget` for displaying manual summaries in a collapsible format. - Introduced a `CompactingWidget` to indicate ongoing compacting processes. - Added a base class `TimedStatusWidget` for widgets that require a timer. - Developed context window resolution helpers to retrieve context window sizes from various model attributes. - Enhanced tests for context window resolution and status bar functionalities, ensuring accurate behavior across different scenarios. - Updated existing tests to cover new features and maintain code quality. * refactor(Channel): simplify lambda function in _send_with_retry method * feat: enhance context editing logic and improve error handling in StreamState * refactor(Channel): streamline lambda function in _send_with_retry method * feat: rename auto-approve option to auto-mode for unattended execution; update checkpoint queries to filter by agent name; improve compatibility validation logic * feat: rename auto-approve option to auto-mode; update related logic and tests for improved unattended execution * fix: correct formatting of console message for MCP server configuration status * feat: add check for None summary_message in _apply_summarization_event to prevent errors * feat: enhance _load_checkpoint_messages to validate message format and apply summarization event
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
"""ContextEditingMiddleware configuration for EvoScientist.
|
|
|
|
Wraps LangChain's built-in ``ContextEditingMiddleware`` with project-specific
|
|
defaults: dynamic trigger based on model context window, ``keep=5`` for
|
|
multi-step tool chains, and ``think_tool`` excluded from clearing.
|
|
|
|
Usage::
|
|
|
|
from EvoScientist.middleware import create_context_editing_middleware
|
|
|
|
middleware = create_context_editing_middleware(model)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from langchain_core.language_models import BaseChatModel
|
|
|
|
from ..llm.context_window import get_context_window
|
|
|
|
|
|
def compute_context_editing_trigger(
|
|
model: BaseChatModel,
|
|
fraction: float = 0.50,
|
|
fallback: int = 100_000,
|
|
) -> int:
|
|
"""Compute ClearToolUsesEdit trigger based on model context window.
|
|
|
|
Uses 50% of the best available model context window when metadata is
|
|
available, otherwise falls back to a fixed token count. This fires well
|
|
before ``SummarizationMiddleware`` (~85% / 170k).
|
|
"""
|
|
context_window = get_context_window(model)
|
|
if context_window is not None and context_window > 0:
|
|
return max(1, int(context_window * fraction))
|
|
return fallback
|
|
|
|
|
|
def create_context_editing_middleware(model: BaseChatModel | None = None):
|
|
"""Build a ContextEditingMiddleware with EvoScientist defaults.
|
|
|
|
Args:
|
|
model: Chat model used to determine context window size.
|
|
If *None*, the default model is resolved via ``_ensure_chat_model()``.
|
|
"""
|
|
from langchain.agents.middleware import ClearToolUsesEdit, ContextEditingMiddleware
|
|
|
|
if model is None:
|
|
from EvoScientist.EvoScientist import _ensure_chat_model
|
|
|
|
model = _ensure_chat_model()
|
|
|
|
return ContextEditingMiddleware(
|
|
edits=[
|
|
ClearToolUsesEdit(
|
|
trigger=compute_context_editing_trigger(model),
|
|
keep=5,
|
|
exclude_tools=["think_tool"],
|
|
),
|
|
],
|
|
)
|