1use glob::glob;
2use std::fmt::Write as FmtWrite;
3use std::fs::{self, File};
4use std::io::Write;
5use std::path::{Path, PathBuf};
6use std::sync::Arc;
7use tokio::sync::Mutex;
8use tracing::{debug, warn};
9
10use crate::errors::{Result, WinxError};
11use crate::state::bash_state::BashState;
12use crate::state::persistence::{
13 load_bash_state_from_path, save_bash_state_to_path, BashStateSnapshot,
14};
15use crate::types::ContextSave;
16use crate::utils::path::expand_user;
17
18pub async fn handle_tool_call(
32 bash_state: &Arc<Mutex<Option<BashState>>>,
33 args: ContextSave,
34) -> Result<String> {
35 let bash_state_guard = bash_state.lock().await;
37
38 let bash_state = bash_state_guard.as_ref().ok_or(WinxError::BashStateNotInitialized)?;
39
40 let result = save_context(bash_state, args)?;
42
43 if should_open_context_file(&result) {
44 if let Err(e) = try_open_file(&result) {
45 debug!("Failed to open the context file: {}", e);
46 }
47 }
48
49 Ok(result)
50}
51
52fn save_context(bash_state: &BashState, mut context: ContextSave) -> Result<String> {
63 normalize_context(&mut context)?;
64
65 let (relevant_files, warnings) = collect_relevant_files(&context)?;
66 let memory_dir = resolve_memory_dir()?;
67 let relevant_files_data = read_files_content(&relevant_files, 10_000)?;
68 let memory_data = format_memory(&context, &relevant_files_data);
69 let safe_id = sanitize_filename(&context.id);
70
71 let (memory_file_path, state_file_path) = context_paths_from_dir(&memory_dir, &safe_id);
72 if let Some(response) = write_memory_file(&memory_file_path, &memory_data, &context)? {
73 return Ok(response);
74 }
75
76 write_bash_state_file(&state_file_path, bash_state);
77
78 Ok(context_save_response(&relevant_files, &warnings, &context, &memory_file_path))
79}
80
81fn normalize_context(context: &mut ContextSave) -> Result<()> {
82 if !context.project_root_path.is_empty() {
83 context.project_root_path = expand_user(&context.project_root_path);
84 }
85
86 if context.id.is_empty() {
87 return Err(WinxError::ArgumentParseError("Task ID cannot be empty".to_string()));
88 }
89
90 Ok(())
91}
92
93fn collect_relevant_files(context: &ContextSave) -> Result<(Vec<PathBuf>, Vec<String>)> {
94 let mut relevant_files = Vec::new();
95 let mut warnings = Vec::new();
96
97 for glob_pattern in &context.relevant_file_globs {
98 let expanded_glob = expand_user(glob_pattern);
100
101 let final_glob =
103 if !Path::new(&expanded_glob).is_absolute() && !context.project_root_path.is_empty() {
104 PathBuf::from(&context.project_root_path)
105 .join(expanded_glob)
106 .to_string_lossy()
107 .to_string()
108 } else {
109 expanded_glob
110 };
111
112 debug!("Processing glob pattern: {}", final_glob);
113
114 let matches = glob(&final_glob).map_err(|e| {
116 WinxError::ArgumentParseError(format!("Invalid glob pattern '{final_glob}': {e}"))
117 })?;
118
119 let mut found_files = false;
120 for entry in matches {
121 match entry {
122 Ok(path) => {
123 if path.is_file() {
124 relevant_files.push(path);
125 found_files = true;
126 if relevant_files.len() >= 1000 {
128 warn!("Reached limit of 1000 files for glob '{}'", final_glob);
129 break;
130 }
131 }
132 }
133 Err(e) => {
134 warn!("Error matching glob '{}': {}", final_glob, e);
135 }
136 }
137 }
138
139 if !found_files {
140 warnings.push(format!("Warning: No files found for the glob: {glob_pattern}"));
141 }
142 }
143
144 debug!("Found {} relevant files", relevant_files.len());
145 Ok((relevant_files, warnings))
146}
147
148fn resolve_memory_dir() -> Result<PathBuf> {
149 let app_dir = match get_app_dir_xdg() {
150 Ok(dir) => dir,
151 Err(e) => {
152 debug!("Failed to get primary app directory: {:?}", e);
153 let fallback = std::env::temp_dir().join("winx-memory");
155 debug!("Using fallback directory: {}", fallback.display());
156 fs::create_dir_all(&fallback).map_err(|e2| WinxError::FileAccessError {
157 path: fallback.clone(),
158 message: format!(
159 "Failed to create fallback directory: {e2} (after previous error: {e:?})"
160 ),
161 })?;
162 fallback
163 }
164 };
165
166 let mut memory_dir = app_dir.join("memory");
167 match fs::create_dir_all(&memory_dir) {
168 Ok(()) => {}
169 Err(e) => {
170 debug!("Failed to create memory directory: {}", e);
171 debug!("Using app_dir directly as memory_dir due to failed subdirectory creation");
173 memory_dir = app_dir;
174 }
175 }
176
177 Ok(memory_dir)
178}
179
180pub(crate) fn context_paths(id: &str) -> Result<(PathBuf, PathBuf)> {
181 let safe_id = sanitize_filename(id);
182 let memory_dir = resolve_memory_dir()?;
183 Ok(context_paths_from_dir(&memory_dir, &safe_id))
184}
185
186fn context_paths_from_dir(memory_dir: &Path, safe_id: &str) -> (PathBuf, PathBuf) {
187 (
188 memory_dir.join(format!("{safe_id}.txt")),
189 memory_dir.join(format!("{safe_id}_bash_state.json")),
190 )
191}
192
193pub(crate) fn load_saved_context(id: &str) -> Result<Option<(String, Option<BashStateSnapshot>)>> {
194 if id.is_empty() {
195 return Ok(None);
196 }
197
198 let (memory_file_path, state_file_path) = context_paths(id)?;
199 if !memory_file_path.exists() {
200 return Ok(None);
201 }
202
203 let memory_data =
204 fs::read_to_string(&memory_file_path).map_err(|e| WinxError::FileAccessError {
205 path: memory_file_path.clone(),
206 message: format!("Failed to read saved context: {e}"),
207 })?;
208 let memory_data = truncate_memory_to_tokens(memory_data, MEMORY_MAX_TOKENS);
211 let state = load_bash_state_from_path(&state_file_path).map_err(|e| {
212 WinxError::SerializationError(format!("Failed to load saved bash state: {e}"))
213 })?;
214
215 Ok(Some((memory_data, state)))
216}
217
218const MEMORY_MAX_TOKENS: usize = 8_000;
221
222fn truncate_memory_to_tokens(text: String, max_tokens: usize) -> String {
226 let Some(ids) = crate::utils::encoder::encode_ids(&text) else {
227 return text;
228 };
229 if ids.len() <= max_tokens {
230 return text;
231 }
232 let keep = max_tokens.saturating_sub(8);
233 match crate::utils::encoder::decode_ids(&ids[..keep]) {
234 Some(decoded) => format!("{decoded}\n(...truncated saved context)"),
235 None => text,
236 }
237}
238
239pub(crate) fn extract_project_root(memory: &str) -> Option<PathBuf> {
242 memory
243 .lines()
244 .find_map(|line| line.strip_prefix("Project root path:").map(str::trim))
245 .filter(|root| !root.is_empty())
246 .map(PathBuf::from)
247}
248
249fn write_memory_file(
250 memory_file_path: &Path,
251 memory_data: &str,
252 context: &ContextSave,
253) -> Result<Option<String>> {
254 match File::create(memory_file_path) {
255 Ok(mut file) => {
256 if let Err(e) = file.write_all(memory_data.as_bytes()) {
257 warn!("Failed to write memory data: {}", e);
258 return Ok(Some(save_to_temp_file(memory_data, context)?));
259 }
260 }
261 Err(e) => {
262 warn!("Failed to create memory file: {}", e);
263 return Ok(Some(save_to_temp_file(memory_data, context)?));
264 }
265 }
266
267 Ok(None)
268}
269
270fn write_bash_state_file(state_file_path: &Path, bash_state: &BashState) {
271 if let Err(e) = save_bash_state_to_path(state_file_path, &bash_state.snapshot()) {
273 warn!("Failed to write bash state data: {}", e);
274 }
275}
276
277fn context_save_response(
278 relevant_files: &[PathBuf],
279 warnings: &[String],
280 context: &ContextSave,
281 memory_file_path: &Path,
282) -> String {
283 let memory_file_path_str = memory_file_path.to_string_lossy().to_string();
284 if !relevant_files.is_empty() || context.relevant_file_globs.is_empty() {
285 if warnings.is_empty() {
286 memory_file_path_str
287 } else {
288 format!(
289 "{}\n\nContext file successfully saved at {}",
290 warnings.join("\n"),
291 memory_file_path_str
292 )
293 }
294 } else {
295 format!(
296 "Error: No files found for the given globs. Context file successfully saved at \"{memory_file_path_str}\", but please fix the error."
297 )
298 }
299}
300
301fn format_memory(context: &ContextSave, relevant_files_data: &str) -> String {
312 let mut memory_data = String::new();
313
314 if !context.project_root_path.is_empty() {
316 let _ = write!(memory_data, "Project root path: {}\n\n", context.project_root_path);
317 }
318
319 memory_data.push_str(&context.description);
321 memory_data.push_str("\n\n");
322
323 let _ =
325 write!(memory_data, "Relevant file globs: {}\n\n", context.relevant_file_globs.join(", "));
326
327 memory_data.push_str("File contents:\n\n");
329 memory_data.push_str(relevant_files_data);
330
331 memory_data
332}
333
334fn get_app_dir_xdg() -> Result<PathBuf> {
346 let app_dir = get_primary_app_dir().or_else(|_| get_fallback_app_dir())?;
348
349 debug!("Using app directory: {}", app_dir.display());
350 Ok(app_dir)
351}
352
353fn get_primary_app_dir() -> Result<PathBuf> {
355 if let Ok(xdg_path) = std::env::var("XDG_DATA_HOME") {
357 let app_dir = PathBuf::from(xdg_path).join("winx");
358 if let Ok(()) = fs::create_dir_all(&app_dir) {
359 return Ok(app_dir);
360 }
361 }
362
363 if let Ok(home) = std::env::var("HOME") {
365 let app_dir = PathBuf::from(home).join(".local/share/winx");
366 if let Ok(()) = fs::create_dir_all(&app_dir) {
367 return Ok(app_dir);
368 }
369 }
370
371 Err(WinxError::FileAccessError {
373 path: PathBuf::from("<primary-paths>"),
374 message: "Could not create app directory in primary locations".to_string(),
375 })
376}
377
378fn get_fallback_app_dir() -> Result<PathBuf> {
380 let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
382
383 let app_dir = current_dir.join(".winx-data");
384 if let Ok(()) = fs::create_dir_all(&app_dir) {
385 return Ok(app_dir);
386 }
387
388 let temp_dir = std::env::temp_dir().join("winx-data");
390 fs::create_dir_all(&temp_dir).map_err(|e| WinxError::FileAccessError {
391 path: temp_dir.clone(),
392 message: format!("Failed to create app directory in any location: {e}"),
393 })?;
394
395 Ok(temp_dir)
396}
397
398fn read_files_content(file_paths: &[PathBuf], max_files: usize) -> Result<String> {
409 let mut result = String::new();
410 let mut skipped_binary = Vec::new();
411
412 for (i, path) in file_paths.iter().take(max_files).enumerate() {
413 match fs::read_to_string(path) {
415 Ok(file_content) => {
416 let _ = writeln!(result, "--- File {}: {} ---", i + 1, path.display());
417 result.push_str(&file_content);
418 result.push_str("\n\n");
419 }
420 Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {
421 skipped_binary.push(path.display().to_string());
423 }
424 Err(e) => {
425 return Err(WinxError::FileAccessError {
426 path: path.clone(),
427 message: format!("Failed to read file: {e}"),
428 });
429 }
430 }
431 }
432
433 if !skipped_binary.is_empty() {
435 let _ = write!(
436 result,
437 "Note: Skipped {} binary/non-text file(s): {}\n\n",
438 skipped_binary.len(),
439 skipped_binary.join(", ")
440 );
441 }
442
443 if file_paths.len() > max_files {
444 let _ = writeln!(
445 result,
446 "Note: Only showing the first {} files out of {}.",
447 max_files,
448 file_paths.len()
449 );
450 }
451
452 Ok(result)
453}
454
455fn try_open_file(file_path: &str) -> Result<()> {
465 if std::env::consts::OS != "macos" && std::env::consts::OS != "linux" {
466 return Ok(());
468 }
469
470 let cmd = if std::env::consts::OS == "macos" {
472 "open"
473 } else {
474 for cmd in &["xdg-open", "gnome-open", "kde-open"] {
476 let status = std::process::Command::new("which")
477 .arg(cmd)
478 .stdout(std::process::Stdio::null())
479 .stderr(std::process::Stdio::null())
480 .status();
481
482 if let Ok(status) = status {
483 if status.success() {
484 let _ = std::process::Command::new(cmd)
486 .arg(file_path)
487 .stdout(std::process::Stdio::null())
488 .stderr(std::process::Stdio::null())
489 .spawn()
490 .map_err(|e| {
491 WinxError::CommandExecutionError(format!(
492 "Failed to spawn open command: {e}"
493 ))
494 })?;
495
496 return Ok(());
498 }
499 }
500 }
501
502 return Ok(());
504 };
505
506 let _ = std::process::Command::new(cmd)
508 .arg(file_path)
509 .stdout(std::process::Stdio::null())
510 .stderr(std::process::Stdio::null())
511 .spawn()
512 .map_err(|e| {
513 WinxError::CommandExecutionError(format!("Failed to spawn open command: {e}"))
514 })?;
515
516 Ok(())
521}
522
523fn should_open_context_file(file_path: &str) -> bool {
524 std::env::var("WINX_OPEN_CONTEXT").is_ok_and(|value| value == "1" || value == "true")
525 && Path::new(file_path).is_file()
526 && !cfg!(test)
527}
528
529fn save_to_temp_file(memory_data: &str, context: &ContextSave) -> Result<String> {
531 let temp_dir = std::env::temp_dir();
532 let safe_id = sanitize_filename(&context.id);
533 let temp_file_path = temp_dir.join(format!("winx-{safe_id}.txt"));
534
535 let mut file = File::create(&temp_file_path).map_err(|e| WinxError::FileAccessError {
536 path: temp_file_path.clone(),
537 message: format!("Failed to create temporary file: {e}"),
538 })?;
539
540 file.write_all(memory_data.as_bytes()).map_err(|e| WinxError::FileAccessError {
541 path: temp_file_path.clone(),
542 message: format!("Failed to write to temporary file: {e}"),
543 })?;
544
545 let path_str = temp_file_path.to_string_lossy().to_string();
546
547 Ok(format!(
548 "Context was saved to temporary file at {path_str} due to permission issues with regular locations."
549 ))
550}
551
552fn sanitize_filename(input: &str) -> String {
554 let invalid_chars = vec!['/', '\\', ':', '*', '?', '"', '<', '>', '|'];
555 let mut result = input.to_string();
556
557 for c in invalid_chars {
558 result = result.replace(c, "_");
559 }
560
561 if result.len() > 50 {
563 use rand::RngExt;
564 result = format!("{}-{}", &result[0..45], rand::rng().random_range(1000..9999));
565 }
566
567 result
568}