-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.rs
More file actions
130 lines (112 loc) · 3.72 KB
/
progress.rs
File metadata and controls
130 lines (112 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Streaming progress tracker for real-time status updates
//!
//! Provides detailed progress information during long-running operations.
use tokio::sync::mpsc;
/// Progress stage during operation execution
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProgressStage {
/// Classifying user query
Classifying,
/// Searching RAPTOR index
SearchingContext { chunks: usize },
/// Executing a tool
ExecutingTool { tool_name: String },
/// Generating response
Generating,
/// Completed successfully
Complete,
/// Failed with error
Failed { error: String },
}
/// Progress update message
#[derive(Debug, Clone)]
pub struct ProgressUpdate {
pub stage: ProgressStage,
pub message: String,
pub elapsed_ms: u64,
}
impl ProgressUpdate {
/// Create a new progress update
pub fn new(stage: ProgressStage, message: impl Into<String>, elapsed_ms: u64) -> Self {
Self {
stage,
message: message.into(),
elapsed_ms,
}
}
}
/// Progress tracker for streaming updates
pub struct ProgressTracker {
tx: mpsc::Sender<ProgressUpdate>,
start_time: std::time::Instant,
}
impl ProgressTracker {
/// Create a new progress tracker
pub fn new(tx: mpsc::Sender<ProgressUpdate>) -> Self {
Self {
tx,
start_time: std::time::Instant::now(),
}
}
/// Send a progress update (non-blocking)
pub async fn update(&self, stage: ProgressStage, message: impl Into<String>) {
let elapsed = self.start_time.elapsed().as_millis() as u64;
let update = ProgressUpdate::new(stage, message, elapsed);
let _ = self.tx.try_send(update);
}
/// Send classifying stage
pub async fn classifying(&self) {
self.update(ProgressStage::Classifying, "🔍 Clasificando consulta...").await;
}
/// Send searching context stage
pub async fn searching_context(&self, chunks: usize) {
self.update(
ProgressStage::SearchingContext { chunks },
format!("📊 Buscando contexto ({} chunks)...", chunks),
).await;
}
/// Send executing tool stage
pub async fn executing_tool(&self, tool_name: impl Into<String>) {
let tool = tool_name.into();
self.update(
ProgressStage::ExecutingTool { tool_name: tool.clone() },
format!("🔧 Ejecutando {}...", tool),
).await;
}
/// Send generating response stage
pub async fn generating(&self) {
self.update(ProgressStage::Generating, "💭 Generando respuesta...").await;
}
/// Send complete stage
pub async fn complete(&self) {
self.update(ProgressStage::Complete, "✓ Completado").await;
}
/// Send failed stage
pub async fn failed(&self, error: impl Into<String>) {
let err = error.into();
self.update(
ProgressStage::Failed { error: err.clone() },
format!("❌ Error: {}", err),
).await;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_progress_tracker() {
let (tx, mut rx) = mpsc::channel(10);
let tracker = ProgressTracker::new(tx);
tracker.classifying().await;
let update = rx.recv().await.unwrap();
assert_eq!(update.stage, ProgressStage::Classifying);
assert!(update.message.contains("Clasificando"));
tracker.searching_context(100).await;
let update = rx.recv().await.unwrap();
assert_eq!(update.stage, ProgressStage::SearchingContext { chunks: 100 });
assert!(update.message.contains("100 chunks"));
tracker.complete().await;
let update = rx.recv().await.unwrap();
assert_eq!(update.stage, ProgressStage::Complete);
}
}