Skip to main content

deepseek_rust_cli/agent/
ops.rs

1use crate::agent::{agent::DeepSeekAgent, history::save_history};
2
3impl DeepSeekAgent {
4    pub fn save(&self) {
5        save_history(&self.session_id, &self.messages);
6    }
7
8    pub fn manage_context(&mut self) {
9        let max_chars = self.config.max_context_chars;
10        let mut current_chars: usize = self
11            .messages
12            .iter()
13            .map(|m| {
14                m.content.as_deref().unwrap_or("").len()
15                    + m.reasoning_content.as_deref().unwrap_or("").len()
16            })
17            .sum();
18
19        while current_chars > max_chars && self.messages.len() > 1 {
20            let mut remove_count = 1;
21            let idx = 1;
22
23            if idx >= self.messages.len() {
24                break;
25            }
26
27            match self.messages[idx].role.as_str() {
28                "assistant" if self.messages[idx].tool_calls.is_some() => {
29                    let mut j = idx + 1;
30                    while j < self.messages.len() && self.messages[j].role == "tool" {
31                        j += 1;
32                    }
33                    remove_count = j - idx;
34                }
35                "assistant" => {}
36                "tool" => {
37                    remove_count = 1;
38                }
39                _ => {}
40            }
41
42            let mut chars_removed = 0;
43            for _ in 0..remove_count {
44                if idx < self.messages.len() {
45                    let removed = self.messages.remove(idx);
46                    chars_removed += removed.content.as_deref().unwrap_or("").len()
47                        + removed.reasoning_content.as_deref().unwrap_or("").len();
48                }
49            }
50            current_chars -= chars_removed;
51        }
52    }
53
54    pub fn undo(&mut self) -> String {
55        if let Some(action) = self.undo_stack.pop() {
56            while self.messages.len() > 1
57                && (self
58                    .messages
59                    .last()
60                    .map(|m| m.role == "tool" || m.role == "assistant")
61                    .unwrap_or(false))
62            {
63                self.messages.pop();
64            }
65
66            match action.r#type.as_str() {
67                "write" | "replace" | "delete" => {
68                    if let Some(backup) = action.backup {
69                        let _ = std::fs::write(&action.path, backup);
70                        format!("✅ Undone {}: Restored {}", action.r#type, action.path)
71                    } else if action.r#type == "write" || action.r#type == "replace" {
72                        let _ = std::fs::remove_file(&action.path);
73                        format!(
74                            "✅ Undone {}: Deleted new file {}",
75                            action.r#type, action.path
76                        )
77                    } else {
78                        "❌ Undo failed: No backup available.".to_string()
79                    }
80                }
81                "rename" => {
82                    if let Some(backup) = action.backup {
83                        let original_path = String::from_utf8_lossy(&backup).to_string();
84                        let _ = std::fs::rename(&action.path, &original_path);
85                        format!(
86                            "✅ Undone rename: Moved {} back to {}",
87                            action.path, original_path
88                        )
89                    } else {
90                        "❌ Undo failed: No backup path available.".to_string()
91                    }
92                }
93                _ => "❌ Undo failed: Unknown action type".to_string(),
94            }
95        } else {
96            "ℹ️ Undo stack is empty.".to_string()
97        }
98    }
99
100    pub fn cleanup_aborted_messages(&mut self) {
101        while self.messages.last().is_some_and(|m| m.role == "tool") {
102            self.messages.pop();
103        }
104        if self
105            .messages
106            .last()
107            .is_some_and(|m| m.role == "assistant" && m.tool_calls.is_some())
108        {
109            self.messages.pop();
110        }
111    }
112}