Skip to main content

winx_code_agent/utils/
mod.rs

1//! Utility modules for the Winx application.
2//!
3//! This module contains various utility functions and types used throughout
4//! the application, such as file and path handling, repository analysis, etc.
5
6pub mod bash_parser;
7pub mod display_tree;
8pub mod encoder;
9pub mod file_cache;
10pub mod mmap;
11pub mod mode_prompts;
12pub mod output_compress;
13pub mod path;
14pub mod path_prob;
15pub mod repo;
16pub mod syntax;
17pub mod workspace_stats;
18
19use crate::types::Initialize;
20use serde_json::Value;
21use tracing::debug;
22
23/// Largest index `<= idx` that is a char boundary of `s`.
24///
25/// Offsets derived from byte arithmetic over possibly-multibyte text (UTF-8
26/// CJK/emoji/box-drawing, the prompt glyphs, NBSP indentation) can land in the
27/// middle of a code point. Slicing there panics. Snap down to the nearest
28/// boundary so `&s[floor_char_boundary(s, idx)..]` is always valid. For ASCII
29/// input this is the identity, so hot paths keep their existing behavior.
30pub fn floor_char_boundary(s: &str, idx: usize) -> usize {
31    if idx >= s.len() {
32        return s.len();
33    }
34    let mut i = idx;
35    while i > 0 && !s.is_char_boundary(i) {
36        i -= 1;
37    }
38    i
39}
40
41/// Debug helper to test JSON parsing of an Initialize request
42pub fn test_json_parsing(json_str: &str) -> Result<(), String> {
43    // First, try to parse as raw JSON to see if the format is valid
44    let raw_json_result = serde_json::from_str::<Value>(json_str);
45    if let Err(e) = raw_json_result {
46        return Err(format!("Invalid JSON format: {e}"));
47    }
48
49    // Now try to parse into our Initialize struct
50    let init_result = serde_json::from_str::<Initialize>(json_str);
51    match init_result {
52        Ok(init) => {
53            debug!(
54                init_type = ?init.init_type,
55                mode_name = ?init.mode_name,
56                code_writer_config = ?init.code_writer_config,
57                task_id_to_resume = init.task_id_to_resume,
58                "Successfully parsed JSON into Initialize struct"
59            );
60            Ok(())
61        }
62        Err(e) => Err(format!("Failed to parse JSON into Initialize struct: {e}")),
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn floor_char_boundary_snaps_into_multibyte() {
72        let s = "a\u{3000}b"; // 'a' (1B) + ideographic space (3B) + 'b' (1B)
73        assert_eq!(floor_char_boundary(s, 0), 0);
74        assert_eq!(floor_char_boundary(s, 1), 1); // boundary before the wide char
75        assert_eq!(floor_char_boundary(s, 2), 1); // mid code point -> snap back to 1
76        assert_eq!(floor_char_boundary(s, 3), 1); // still mid -> 1
77        assert_eq!(floor_char_boundary(s, 4), 4); // boundary after the wide char
78        assert_eq!(floor_char_boundary(s, 999), s.len()); // past end -> len
79                                                          // every returned index must be sliceable without panicking
80        for i in 0..=s.len() + 5 {
81            let cut = floor_char_boundary(s, i);
82            let _ = &s[cut..];
83        }
84    }
85}