winx_code_agent/tools/mod.rs
1//! Tools module for the Winx application.
2//!
3//! This module contains all the tools that are exposed to the MCP client,
4//! including shell initialization, command execution, file operations, etc.
5//!
6//! The `WinxService` struct is the main entry point for all tool calls.
7
8pub mod bash_command;
9pub mod context_save;
10pub mod file_write_or_edit;
11pub mod initialize;
12pub mod read_files;
13pub mod read_image;
14
15use std::sync::Arc;
16use tokio::sync::Mutex; // Changed from std::sync::Mutex for async safety
17use tracing::info;
18
19use crate::state::bash_state::BashState;
20
21/// Type alias for shared bash state with async-safe mutex
22pub type SharedBashState = Arc<Mutex<Option<BashState>>>;
23
24/// Version of the MCP protocol implemented by this service
25#[allow(dead_code)]
26const MCP_PROTOCOL_VERSION: &str = "2024-11-05";
27
28/// Main service implementation for Winx
29///
30/// This struct maintains the state of the shell environment and provides
31/// methods for interacting with it through the MCP protocol.
32///
33/// Uses `tokio::sync::Mutex` for thread-safe async access.
34#[derive(Debug, Clone)]
35pub struct WinxService {
36 /// Shared state for the bash shell environment (async-safe)
37 bash_state: SharedBashState,
38 /// Version information for the service
39 version: String,
40 /// Startup timestamp
41 start_time: std::time::Instant,
42}
43
44impl Default for WinxService {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50impl WinxService {
51 /// Create a new instance of the `WinxService`
52 ///
53 /// # Returns
54 ///
55 /// A new `WinxService` instance with an uninitialized bash state
56 pub fn new() -> Self {
57 info!("Creating new WinxService instance");
58 Self {
59 bash_state: Arc::new(Mutex::new(None)),
60 version: env!("CARGO_PKG_VERSION").to_string(),
61 start_time: std::time::Instant::now(),
62 }
63 }
64
65 /// Get the uptime of the service
66 ///
67 /// # Returns
68 ///
69 /// The duration since the service was started
70 pub fn uptime(&self) -> std::time::Duration {
71 self.start_time.elapsed()
72 }
73
74 /// Get the version of the service
75 ///
76 /// # Returns
77 ///
78 /// The version string of the service
79 pub fn version(&self) -> &str {
80 &self.version
81 }
82
83 /// Get a reference to the bash state, locking the mutex
84 ///
85 /// # Returns
86 ///
87 /// A `MutexGuard` for the bash state
88 #[allow(dead_code)]
89 async fn lock_bash_state(&self) -> tokio::sync::MutexGuard<'_, Option<BashState>> {
90 self.bash_state.lock().await
91 }
92}