// src/agent/tools/compress.rs — 手动上下文压缩工具 use async_trait::async_trait; use serde_json::json; use super::{AgentTool, ToolContext, ToolOutput}; /// 手动上下文压缩工具:LLM 可主动调用以压缩对话历史 pub struct CompressTool; #[async_trait] impl AgentTool for CompressTool { fn name(&self) -> &str { "compress_context" } fn display_name(&self) -> &str { "压缩上下文" } fn is_internal(&self) -> bool { true } fn description(&self) -> &str { "手动压缩对话上下文。当你发现对话历史过长、token 消耗过大时,主动调用此工具进行压缩以释放空间。\ 压缩后历史对话将被摘要替代,但关键信息不会丢失。" } fn parameters(&self) -> serde_json::Value { json!({ "type": "object", "properties": {}, "required": [] }) } /// 设置标志位是幂等操作,并发安全 fn is_concurrency_safe(&self, _args: &serde_json::Value) -> bool { true } async fn execute(&self, _args: serde_json::Value, _ctx: &ToolContext) -> ToolOutput { // 实际压缩由 AgentRuntime 通过 pending_manual_compress 标志位处理 ToolOutput::success( "上下文压缩标记已设置。当前对话历史将在下一轮 LLM 调用前被压缩。", json!({ "action": "compress" }), ) } }