定制 adachsoft/ai-agent-context-window 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

adachsoft/ai-agent-context-window

最新稳定版本:v0.3.1

Composer 安装命令:

composer require adachsoft/ai-agent-context-window

包简介

Context window strategy for adachsoft/ai-agent: sliding window with tool-call aware trimming for AI conversation history.

README 文档

README

Library extending adachsoft/ai-agent with a context window conversation trimming strategy.

Installation

composer require adachsoft/ai-agent-context-window

context_window Strategy

The library provides an SPI implementation:

  • AdachSoft\\AiAgent\\Spi\\Conversation\\ConversationContextStrategyInterface

Strategy ID: context_window.

The strategy:

  • trims history to a window of N last messages (max_messages),
  • guarantees tool-calling consistency in the returned context:
    • if tool results (toolResults) are present in the window, the strategy automatically extends the window backwards to include their corresponding calls (toolCalls),
    • when needed, the limit is treated as a soft limit (more messages than max_messages are returned).

For inconsistent history (tool result without its corresponding call) an exception is thrown:

  • AdachSoft\\AiAgentContextWindow\\Exception\\InconsistentToolCallingHistoryException

Usage with AiAgentBuilder

use AdachSoft\\AiAgent\\PublicApi\\Builder\\AiAgentBuilder;
use AdachSoft\\AiAgent\\PublicApi\\Dto\\AgentConfigDto;
use AdachSoft\\AiAgentContextWindow\\Conversation\\Strategy\\ContextWindowConversationContextStrategy;

$strategy = new ContextWindowConversationContextStrategy();

$builder = new AiAgentBuilder();

$facade = $builder
    ->withPorts($ports)               // PortsConfigDto
    ->withAgentConfig(new AgentConfigDto(
        name: 'MyAgent',
        description: 'Demo agent with context window',
        providerId: 'deepseek',
        modelId: 'deepseek-chat',
        conversationContextStrategyId: 'context_window',
        conversationContextStrategyParams: [
            'max_messages' => 30,
        ],
    ))
    ->withPolicies($policies)         // PoliciesConfigDto
    ->withSpiConversationContextStrategy($strategy)
    ->build();

Configuration

Configuration is provided via the conversationContextStrategyParams bag on AgentConfigDto and read by ContextWindowConversationContextStrategy.

Supported parameters:

  • max_messages (int, required for trimming)
    • soft limit for the number of recent messages kept in the context window,
    • must be an integer greater than or equal to 1.

Example:

use AdachSoft\\AiAgent\\PublicApi\\Dto\\AgentConfigDto;

$agentConfig = new AgentConfigDto(
    name: 'MyAgent',
    description: 'Demo agent with context window',
    providerId: 'deepseek',
    modelId: 'deepseek-chat',
    conversationContextStrategyId: 'context_window',
    conversationContextStrategyParams: [
        'max_messages' => 50,
    ],
);

If max_messages is not provided, the strategy returns the full conversation without trimming (but still validates tool-calling consistency).

Exceptions

  • InvalidContextWindowConfigException when the max_messages parameter is missing, has an invalid type or is less than 1.
  • InconsistentToolCallingHistoryException for inconsistent tool-calling history (tool result without matching tool call).

Static Analysis

Running PHPStan:

composer install
composer run phpstan

License

MIT

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固