77
88from __future__ import annotations
99
10+ from pathlib import Path
1011import uuid
1112from collections .abc import Callable , Iterable , Mapping
1213from functools import partial
3132from langgraph .graph .message import REMOVE_ALL_MESSAGES
3233from langgraph .runtime import Runtime
3334
35+ from yuxi .utils .paths import OUTPUTS_DIR_NAME
36+
3437TokenCounter = Callable [[Iterable [MessageLikeRepresentation ]], int ]
3538
3639DEFAULT_SUMMARY_PROMPT = """<role>
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
8283ContextFraction = 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
0 commit comments