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