Skip to main content

j_agent/tools/
compact_tool.rs

1use crate::tools::{PlanDecision, Tool, ToolResult, schema_to_tool_params};
2use schemars::JsonSchema;
3use serde::Deserialize;
4use serde_json::Value;
5use std::borrow::Cow;
6use std::sync::{Arc, atomic::AtomicBool};
7
8/// CompactTool 参数
9#[derive(Deserialize, JsonSchema)]
10#[allow(dead_code)]
11struct CompactParams {
12    /// What to preserve in the summary (optional)
13    #[serde(default)]
14    focus: Option<String>,
15}
16
17/// CompactTool: 让模型可以主动触发对话压缩(Layer 3)
18///
19#[derive(Debug)]
20pub struct CompactTool;
21
22impl CompactTool {
23    pub const NAME: &'static str = "Compact";
24}
25
26impl Tool for CompactTool {
27    fn name(&self) -> &str {
28        Self::NAME
29    }
30
31    fn description(&self) -> Cow<'_, str> {
32        r#"
33        Trigger conversation compression to free up context window.
34        Use when 
35        - the conversation is getting long and you want to summarize and compress the history to continue working efficiently.
36        - when you try to solve a issue but fail too many time, this tool help you free down your mind and help you solve clearly.
37        "#.into()
38    }
39
40    fn parameters_schema(&self) -> Value {
41        schema_to_tool_params::<CompactParams>()
42    }
43
44    fn execute(&self, _arguments: &str, _cancelled: &Arc<AtomicBool>) -> ToolResult {
45        ToolResult {
46            output: "Compression requested.".to_string(),
47            is_error: false,
48            images: vec![],
49            plan_decision: PlanDecision::None,
50        }
51    }
52
53    fn requires_confirmation(&self) -> bool {
54        false
55    }
56}