Skip to content

Commit e05eacf

Browse files
committed
feat: 优化 summary 组件
1 parent 4ee15e3 commit e05eacf

2 files changed

Lines changed: 9 additions & 13 deletions

File tree

backend/package/yuxi/agents/middlewares/summary_middleware.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from __future__ import annotations
99

10+
from pathlib import Path
1011
import uuid
1112
from collections.abc import Callable, Iterable, Mapping
1213
from functools import partial
@@ -31,6 +32,8 @@
3132
from langgraph.graph.message import REMOVE_ALL_MESSAGES
3233
from langgraph.runtime import Runtime
3334

35+
from yuxi.utils.paths import OUTPUTS_DIR_NAME
36+
3437
TokenCounter = Callable[[Iterable[MessageLikeRepresentation]], int]
3538

3639
DEFAULT_SUMMARY_PROMPT = """<role>
@@ -74,9 +77,7 @@
7477
</messages>"""
7578

7679
_DEFAULT_MESSAGES_TO_KEEP = 20
77-
_DEFAULT_TRIM_TOKEN_LIMIT = 4000
7880
_DEFAULT_FALLBACK_MESSAGE_COUNT = 15
79-
_DEFAULT_OFFLOAD_THRESHOLD = 1000 # Token 数阈值,超过此值则卸载到文件系统
8081
_OFFLOAD_DIR = "/summary_offload" # 虚拟文件系统路径
8182

8283
ContextFraction = tuple[Literal["fraction"], float]
@@ -143,7 +144,7 @@ def _offload_tool_result(msg: ToolMessage, threshold: int, token_counter: TokenC
143144
# 生成文件路径 (工具名称-xxx)
144145
message_id = msg.id or str(uuid.uuid4())[:8]
145146
safe_name = "".join(c if c.isalnum() or c in "-_" else "_" for c in tool_name)
146-
file_path = f"{_OFFLOAD_DIR}/{safe_name}-{message_id}"
147+
file_path = (Path(OUTPUTS_DIR_NAME) / f"{_OFFLOAD_DIR}/{safe_name}-{message_id}").as_posix()
147148

148149
# 构建文件头部信息
149150
header_lines = [
@@ -226,7 +227,7 @@ def __init__(
226227
keep: ContextSize = ("messages", _DEFAULT_MESSAGES_TO_KEEP),
227228
token_counter: TokenCounter = count_tokens_approximately,
228229
summary_prompt: str = DEFAULT_SUMMARY_PROMPT,
229-
trim_tokens_to_summarize: int | None = _DEFAULT_TRIM_TOKEN_LIMIT,
230+
trim_tokens_to_summarize: int | None = 4000,
230231
# 工具结果卸载参数
231232
summary_offload_threshold: int = 1000,
232233
max_retention_ratio: float = 0.6,
@@ -240,18 +241,12 @@ def __init__(
240241
keep: 摘要后保留的消息数量/ token 策略 (作为 fallback)
241242
token_counter: token 计数函数
242243
summary_prompt: 生成摘要的提示词模板
243-
trim_tokens_to_summarize: 准备摘要消息时的最大 token 数
244-
summary_offload_threshold: Summary 时卸载阈值(token 数),默认 1000
244+
trim_tokens_to_summarize: Summary 时,无损保留的消息数
245+
summary_offload_threshold: Summary 时,工具调用结果超过此 token 数阈值则卸载到文件系统
245246
max_retention_ratio: 触发 Summary 后,如果不超过此比例(相对于 trigger),则不删除消息。默认 0.6
246247
"""
247248
super().__init__()
248249

249-
# Handle renamed argument for backward compatibility if needed,
250-
# but since we are refactoring, we map deprecated 'result_offload_threshold'
251-
# to 'summary_offload_threshold' if present in kwargs.
252-
if "result_offload_threshold" in deprecated_kwargs:
253-
summary_offload_threshold = deprecated_kwargs.pop("result_offload_threshold")
254-
255250
if isinstance(model, str):
256251
model = init_chat_model(model)
257252

backend/package/yuxi/services/subagent_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from yuxi.repositories.subagent_repository import SubAgentRepository
1010
from yuxi.storage.postgres.manager import pg_manager
1111
from yuxi.utils import logger
12+
from yuxi.utils.paths import OUTPUTS_DIR_NAME
1213

1314
# SubAgent specs cache for get_subagent_specs
1415
_subagent_specs_cache: list[dict[str, Any]] | None = None
@@ -34,7 +35,7 @@ async def _get_session(db: AsyncSession | None = None):
3435
"你是一位专注的研究员。你的工作是根据用户的问题进行研究。"
3536
"进行彻底的研究,然后用详细的答案回复用户的问题,只有你的最终答案会被传递给用户。"
3637
"除了你的最终信息,他们不会知道任何其他事情,所以你的最终报告应该就是你的最终信息!"
37-
"将调研结果保存到主题研究文件中 sub_research/xxx.md 中。"
38+
f"将调研结果保存到主题研究文件中 {OUTPUTS_DIR_NAME}/sub_research/xxx.md 中。"
3839
),
3940
"tools": ["tavily_search"],
4041
"is_builtin": True,

0 commit comments

Comments
 (0)