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