Skip to content

Commit dea468f

Browse files
committed
Refactor ONNX provider and related modules for improved readability and maintainability
- Reformatted code in `onnx_provider.rs` for consistent style and clarity. - Updated import statements and organized them for better structure. - Enhanced error handling and logging in embedding generation methods. - Improved input preparation and normalization processes. - Refined search functionality in `search.rs` by removing redundant imports. - Adjusted memory mapping and serialization in `zerocopy` crate for better performance. - Updated HTTP server configuration in `http_server.rs` for cleaner code. - Enhanced RAG tools in `rag_tools.rs` for better readability and maintainability. - Updated MCP protocol version in documentation and test files to reflect the latest standards. - Added automated tests for MCP tools in `test_mcp_tools.py` to ensure functionality.
1 parent af070c6 commit dea468f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+4679
-2025
lines changed

crates/codegraph-ai/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
pub mod ml;
22
pub mod optimization;
3+
pub mod qwen_simple;
34
pub mod rag;
45
pub mod semantic;
5-
pub mod qwen_simple;
66

7+
pub use qwen_simple::{CodeIntelligenceProvider, QwenClient, QwenConfig, QwenResult};
78
pub use semantic::search::*;
8-
pub use qwen_simple::{QwenClient, QwenConfig, QwenResult, CodeIntelligenceProvider};

crates/codegraph-ai/src/optimization/models.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ impl<I: Send + 'static, O: Send + 'static> DynamicBatcher<I, O> {
310310
.send(req)
311311
.await
312312
.map_err(|_| anyhow::anyhow!("dynamic batch queue full or closed"))?;
313-
rx.await.map_err(|_| anyhow::anyhow!("inference canceled"))?
313+
rx.await
314+
.map_err(|_| anyhow::anyhow!("inference canceled"))?
314315
}
315316
}
316317

crates/codegraph-ai/src/qwen_simple.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ impl QwenClient {
7878
}
7979

8080
/// Generate analysis using Qwen2.5-Coder with comprehensive context
81-
pub async fn generate_analysis(&self, prompt: &str, system_prompt: Option<&str>) -> Result<QwenResult> {
81+
pub async fn generate_analysis(
82+
&self,
83+
prompt: &str,
84+
system_prompt: Option<&str>,
85+
) -> Result<QwenResult> {
8286
let start_time = Instant::now();
8387

8488
// Build full prompt with system context if provided
@@ -99,22 +103,36 @@ impl QwenClient {
99103
},
100104
};
101105

102-
debug!("Sending request to Qwen2.5-Coder: {} context window", self.config.context_window);
106+
debug!(
107+
"Sending request to Qwen2.5-Coder: {} context window",
108+
self.config.context_window
109+
);
103110

104111
let response = timeout(
105112
self.config.timeout,
106113
self.client
107114
.post(&format!("{}/api/generate", self.config.base_url))
108115
.json(&request)
109-
.send()
116+
.send(),
110117
)
111118
.await
112-
.map_err(|_| CodeGraphError::Timeout(format!("Qwen request timeout after {:?}", self.config.timeout)))?
119+
.map_err(|_| {
120+
CodeGraphError::Timeout(format!(
121+
"Qwen request timeout after {:?}",
122+
self.config.timeout
123+
))
124+
})?
113125
.map_err(|e| CodeGraphError::Network(format!("Qwen request failed: {}", e)))?;
114126

115127
if !response.status().is_success() {
116-
let error_text = response.text().await.unwrap_or_else(|_| "Unknown error".to_string());
117-
return Err(CodeGraphError::External(format!("Qwen API error: {}", error_text)));
128+
let error_text = response
129+
.text()
130+
.await
131+
.unwrap_or_else(|_| "Unknown error".to_string());
132+
return Err(CodeGraphError::External(format!(
133+
"Qwen API error: {}",
134+
error_text
135+
)));
118136
}
119137

120138
let response_data: SimpleResponse = response
@@ -147,11 +165,16 @@ impl QwenClient {
147165

148166
/// Check if Qwen2.5-Coder model is available
149167
pub async fn check_availability(&self) -> Result<bool> {
150-
debug!("Checking Qwen2.5-Coder availability at {}", self.config.base_url);
168+
debug!(
169+
"Checking Qwen2.5-Coder availability at {}",
170+
self.config.base_url
171+
);
151172

152173
let response = timeout(
153174
Duration::from_secs(5),
154-
self.client.get(&format!("{}/api/tags", self.config.base_url)).send()
175+
self.client
176+
.get(&format!("{}/api/tags", self.config.base_url))
177+
.send(),
155178
)
156179
.await
157180
.map_err(|_| CodeGraphError::Timeout("Qwen availability check timeout".to_string()))?
@@ -201,7 +224,10 @@ impl QwenClient {
201224
}
202225

203226
// Technical terminology indicates code understanding
204-
if response.contains("function") || response.contains("class") || response.contains("module") {
227+
if response.contains("function")
228+
|| response.contains("class")
229+
|| response.contains("module")
230+
{
205231
confidence += 0.1;
206232
}
207233

@@ -280,4 +306,4 @@ impl CodeIntelligenceProvider for QwenClient {
280306

281307
Ok(result.text)
282308
}
283-
}
309+
}

crates/codegraph-ai/src/rag/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::Arc;
22
use std::time::Instant;
33

4-
use tokio::sync::{mpsc, RwLock, Mutex};
4+
use tokio::sync::{mpsc, Mutex, RwLock};
55
use tokio::task::JoinHandle;
66
use tracing::{debug, error, info, instrument, warn};
77
use uuid::Uuid;

crates/codegraph-ai/src/semantic/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
44

55
use codegraph_core::{CodeGraphError, CodeNode, GraphStore, Language, NodeId, NodeType, Result};
66
use codegraph_graph::CodeGraph;
7-
use codegraph_vector::{EmbeddingGenerator, search::SemanticSearch};
7+
use codegraph_vector::{search::SemanticSearch, EmbeddingGenerator};
88
use futures::future::try_join_all;
99
use tokio::sync::RwLock;
1010

crates/codegraph-core/src/integration/parser_graph.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ where
178178
&self,
179179
dir: &str,
180180
max_concurrent: usize,
181-
exclude_generated: bool
181+
exclude_generated: bool,
182182
) -> Result<DirSummary> {
183183
let start = std::time::Instant::now();
184184

@@ -259,7 +259,10 @@ where
259259
let mut processed = 0usize;
260260
let mut skipped = 0usize;
261261

262-
info!("Processing {} files for edge derivation (all languages supported)", total);
262+
info!(
263+
"Processing {} files for edge derivation (all languages supported)",
264+
total
265+
);
263266

264267
// Two-phase approach for better linking: first ingest nodes, then edges.
265268
// Phase 1: parse + add nodes for all files (incremental aware) with progress tracking
@@ -273,9 +276,12 @@ where
273276
processed += 1;
274277
// Simple progress logging every 10 files
275278
if processed % 10 == 0 {
276-
info!("Edge analysis progress: {}/{} files processed", processed, total);
279+
info!(
280+
"Edge analysis progress: {}/{} files processed",
281+
processed, total
282+
);
277283
}
278-
},
284+
}
279285
ProcessStatus::Skipped => skipped += 1,
280286
},
281287
Err(e) => warn!("process_file error: {}", e),

crates/codegraph-core/src/types.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use uuid::Uuid;
77
pub type NodeId = Uuid;
88
pub type EdgeId = Uuid;
99

10-
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, bincode::Encode, bincode::Decode)]
10+
#[derive(
11+
Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, bincode::Encode, bincode::Decode,
12+
)]
1113
pub enum Language {
1214
Rust,
1315
TypeScript,
@@ -26,7 +28,9 @@ pub enum Language {
2628
Other(String),
2729
}
2830

29-
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, bincode::Encode, bincode::Decode)]
31+
#[derive(
32+
Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, bincode::Encode, bincode::Decode,
33+
)]
3034
pub enum NodeType {
3135
Function,
3236
Struct,

crates/codegraph-graph/src/file_watcher.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use codegraph_core::{traits::FileWatcher, ChangeEvent, Result};
22
use crossbeam_channel::Sender;
3-
use notify::{recommended_watcher, Event, RecommendedWatcher, RecursiveMode, Watcher};
3+
use notify::{
4+
recommended_watcher, Config, Event, PollWatcher, RecursiveMode, Watcher,
5+
};
6+
use std::panic::{catch_unwind, AssertUnwindSafe};
47
use std::path::Path;
8+
use std::time::Duration;
59

610
pub struct FileWatcherImpl {
711
path: String,
@@ -19,10 +23,30 @@ impl FileWatcher for FileWatcherImpl {
1923
fn watch(&self, tx: Sender<ChangeEvent>) -> Result<()> {
2024
let (notify_tx, notify_rx) = std::sync::mpsc::channel::<notify::Result<Event>>();
2125

22-
let mut watcher: RecommendedWatcher = recommended_watcher(move |res: notify::Result<Event>| {
23-
let _ = notify_tx.send(res);
24-
})
25-
.map_err(|e| codegraph_core::CodeGraphError::Notify(e))?;
26+
let watcher_result = catch_unwind(AssertUnwindSafe(|| {
27+
let tx_clone = notify_tx.clone();
28+
recommended_watcher(move |res: notify::Result<Event>| {
29+
let _ = tx_clone.send(res);
30+
})
31+
}));
32+
33+
let mut watcher: Box<dyn Watcher + Send> = match watcher_result {
34+
Ok(Ok(watcher)) => Box::new(watcher),
35+
Ok(Err(e)) => return Err(codegraph_core::CodeGraphError::Notify(e)),
36+
Err(_) => {
37+
eprintln!("⚠️ macOS FSEvents watcher unavailable; falling back to polling file watcher");
38+
let tx_clone = notify_tx.clone();
39+
let poll_config = Config::default().with_poll_interval(Duration::from_secs(2));
40+
let poll_watcher = PollWatcher::new(
41+
move |res: notify::Result<Event>| {
42+
let _ = tx_clone.send(res);
43+
},
44+
poll_config,
45+
)
46+
.map_err(|e| codegraph_core::CodeGraphError::Notify(e))?;
47+
Box::new(poll_watcher)
48+
}
49+
};
2650

2751
watcher
2852
.watch(Path::new(&self.path), RecursiveMode::Recursive)

crates/codegraph-graph/src/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl CodeGraph {
393393
from: NodeId,
394394
to: NodeId,
395395
edge_type: EdgeType,
396-
metadata: HashMap<String, String>
396+
metadata: HashMap<String, String>,
397397
) -> Result<()> {
398398
// Create CodeEdge and use existing add_edge method
399399
let edge = CodeEdge {

crates/codegraph-graph/src/io_batcher.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ impl ReadCoalescer {
6969
.map_err(|e| CodeGraphError::Database(e.to_string()))?;
7070
if let Some(bytes) = data {
7171
// SerializableCodeNode is defined in storage.rs module; mimic fields via serde
72-
let s: crate::storage::SerializableCodeNode = serde_json::from_slice::<crate::storage::SerializableCodeNode>(&bytes)
73-
.map_err(|e| CodeGraphError::Database(e.to_string()))?;
72+
let s: crate::storage::SerializableCodeNode =
73+
serde_json::from_slice::<crate::storage::SerializableCodeNode>(&bytes)
74+
.map_err(|e| CodeGraphError::Database(e.to_string()))?;
7475
let node: CodeNode = s.into();
7576
self.cache.insert(id, Arc::new(node.clone()));
7677
Ok(Some(node))

0 commit comments

Comments
 (0)