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
//! Port of `lib/ruby_llm/progress.rb`, `support/progress_reporter.rb`, and
//! `support/cancellation.rb`.
//!
//! RubyLLM keeps the chat's progress listener and cancellation checkpoint in fiber/thread storage
//! while a tool runs, so code deep inside the tool (an MCP transport waiting on a server) can
//! report progress and notice a cancelled chat. Here the same state lives in tokio task-locals
//! scoped around the tool's future.
use std::future::Future;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
/// `RubyLLM::Progress`: what a tool reports while it works.
#[derive(Debug, Clone, PartialEq)]
pub struct Progress {
pub value: Option<f64>,
pub total: Option<f64>,
pub message: Option<String>,
}
impl Progress {
/// `Progress#fraction`: `value / total` when both are known and `total` is positive.
pub fn fraction(&self) -> Option<f64> {
match (self.value, self.total) {
(Some(value), Some(total)) if total > 0.0 => Some(value / total),
_ => None,
}
}
}
/// Receives progress reports (`Chat#after_tool_progress`, `MCP.after_progress`).
pub type Listener = Arc<dyn Fn(&Progress) + Send + Sync>;
tokio::task_local! {
static LISTENER: Option<Listener>;
static CANCELLATION: Arc<AtomicBool>;
}
/// `ProgressReporter.listen`: runs `future` with `listener` receiving its progress reports.
pub async fn listen<F: Future>(listener: Option<Listener>, future: F) -> F::Output {
LISTENER.scope(listener, future).await
}
/// `ProgressReporter.listener`: the listener of the running tool call, if any.
pub fn listener() -> Option<Listener> {
LISTENER.try_with(Clone::clone).ok().flatten()
}
/// `ProgressReporter.report` (`Tool#progress`): sends a report to the running tool call's listener.
pub fn report(progress: Progress) {
if let Some(listener) = listener() {
listener(&progress);
}
}
/// `Cancellation.watch`: runs `future` with `flag` as its cancellation checkpoint.
pub async fn watch<F: Future>(flag: Arc<AtomicBool>, future: F) -> F::Output {
CANCELLATION.scope(flag, future).await
}
/// `Cancellation.check`: whether the surrounding chat has been cancelled. Long waits (an MCP
/// server that has not answered yet) poll this and stop with `Error::Cancelled`.
pub fn is_cancelled() -> bool {
CANCELLATION
.try_with(|flag| flag.load(Ordering::SeqCst))
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fraction_needs_a_positive_total() {
let progress = |value, total| Progress {
value,
total,
message: None,
};
assert_eq!(progress(Some(1.0), Some(2.0)).fraction(), Some(0.5));
assert_eq!(progress(Some(1.0), Some(0.0)).fraction(), None);
assert_eq!(progress(None, Some(2.0)).fraction(), None);
}
}