-
硬编码重复:
plan.pyline 63:"steps_summary_count": 20discover.pyline 59:"steps_summary_count": 20formatter.pyline 285:options.get("steps_summary_count", 20)formatter.pyline 500:steps_count: int = 20
-
配置分散:
- 同样的值在 4 个地方定义
- 修改需要同步更新多个文件
- 容易产生不一致
-
未使用现有配置系统:
- 已有
MemoryConfig在config/models.py - 但
steps_summary_count等格式化选项未纳入配置系统 - 其他类似配置项也有同样问题:
tool_results_count(默认 5)max_file_content_length(默认 20000)
- 已有
-
违反设计原则:
- DRY (Don't Repeat Yourself): 值重复定义
- Single Source of Truth: 没有单一配置源
- Separation of Concerns: 格式化配置应该集中在配置系统
优点:
- 利用现有配置系统
- 单一数据源
- 可通过配置文件修改
- 类型安全
实施步骤:
- 在
MemoryConfig中添加格式化配置:
@dataclass(frozen=True)
class MemoryConfig:
# ... existing fields ...
# Formatting options
steps_summary_count: int = field(
default=20,
metadata={"description": "Number of recent steps to show in Recent Activity"}
)
tool_results_count: int = field(
default=5,
metadata={"description": "Number of tool results to show in Tool Execution Results"}
)
max_file_content_length: int = field(
default=20000,
metadata={"description": "Maximum file content length in memory summary"}
)
include_file_content: bool = field(
default=True,
metadata={"description": "Whether to include file content in memory summary"}
)- 在
MemoryFormatter中从配置读取默认值:
class MemoryFormatter:
def __init__(self, tool_registry: Optional[Any] = None):
self.tool_registry = tool_registry
self.tool_result_formatter = ToolResultFormatter()
# Load default format options from config
self._load_default_format_options()
def _load_default_format_options(self):
"""Load default format options from config."""
config = ConfigLoader.get()
self.default_format_options = {
"steps_summary_count": config.memory.steps_summary_count,
"tool_results_count": config.memory.tool_results_count,
"max_file_content_length": config.memory.max_file_content_length,
"include_file_content": config.memory.include_file_content,
}
def format(self, state, task_goal=None, format_options=None):
options = {**self.default_format_options, **(format_options or {})}
# ... rest of the code- 在
plan.py和discover.py中使用配置:
# In plan.py and discover.py
config = ConfigLoader.get()
memory_context = state.memory.get_formatted_context(
state=state,
task_goal=self.coordinator.task_spec.goal,
max_length=memory_summary_max_length,
format_options={
"tool_results_count": config.memory.tool_results_count,
"steps_summary_count": config.memory.steps_summary_count,
"include_file_content": config.memory.include_file_content,
"max_file_content_length": config.memory.max_file_content_length,
},
tool_registry=self.coordinator.tool_runtime.registry,
)优点:
- 更清晰的配置结构
- 可以独立配置不同场景
- 更好的类型提示
缺点:
- 需要额外的配置类
- 可能过度设计
优点:
- 实施简单
- 单一数据源(在 formatter 中)
缺点:
- 无法通过配置文件修改
- 不够灵活
- 利用现有架构:
MemoryConfig已经存在,应该充分利用 - 单一数据源:所有配置在
config/models.py中定义 - 可配置性:用户可以通过配置文件修改
- 一致性:与其他 memory 配置保持一致
- 类型安全:dataclass 提供类型检查
- 配置定义:在
MemoryConfig中添加格式化选项 - 默认值加载:
MemoryFormatter从配置加载默认值 - 调用方使用配置:
plan.py和discover.py从配置读取 - 向后兼容:
format_options参数仍然可以覆盖配置值
不仅 steps_summary_count,其他格式化选项也应该统一:
tool_results_countmax_file_content_lengthinclude_file_content
在 MemoryConfig 中添加验证:
def __post_init__(self):
if self.steps_summary_count < 1:
raise ValueError("steps_summary_count must be >= 1")
if self.tool_results_count < 1:
raise ValueError("tool_results_count must be >= 1")更新相关文档,说明如何通过配置文件修改这些选项。
当前设计确实存在冗余和结构问题:
- ✅ 你的观察是正确的
- ✅ 有明确的改进空间
- ✅ 应该遵循单一数据源原则
- ✅ 应该利用现有配置系统
推荐采用方案 1,将格式化选项纳入 MemoryConfig,实现配置的统一管理。