1use async_trait::async_trait;
5use std::sync::Arc;
6
7use super::run_tree::RunTree;
8use lc_schema::Message;
9
10#[async_trait]
15pub trait CallbackHandler: Send + Sync {
16 async fn on_run_start(&self, run: &RunTree);
20
21 async fn on_run_end(&self, run: &RunTree);
23
24 async fn on_run_error(&self, run: &RunTree, error: &str);
26
27 async fn on_llm_start(&self, run: &RunTree, _messages: &[Message]) {
31 self.on_run_start(run).await;
32 }
33
34 async fn on_llm_end(&self, run: &RunTree, _response: &str) {
36 self.on_run_end(run).await;
37 }
38
39 async fn on_llm_new_token(&self, _run: &RunTree, _token: &str) {
41 }
43
44 async fn on_llm_thinking(&self, _run: &RunTree, _thinking: &str) {
51 }
53
54 async fn on_llm_error(&self, run: &RunTree, error: &str) {
56 self.on_run_error(run, error).await;
57 }
58
59 async fn on_chain_start(&self, run: &RunTree, _inputs: &serde_json::Value) {
63 self.on_run_start(run).await;
64 }
65
66 async fn on_chain_end(&self, run: &RunTree, _outputs: &serde_json::Value) {
68 self.on_run_end(run).await;
69 }
70
71 async fn on_chain_error(&self, run: &RunTree, error: &str) {
73 self.on_run_error(run, error).await;
74 }
75
76 async fn on_tool_start(&self, run: &RunTree, _tool_name: &str, _input: &str) {
80 self.on_run_start(run).await;
81 }
82
83 async fn on_tool_end(&self, run: &RunTree, _output: &str) {
85 self.on_run_end(run).await;
86 }
87
88 async fn on_tool_error(&self, run: &RunTree, error: &str) {
90 self.on_run_error(run, error).await;
91 }
92
93 async fn on_retriever_start(&self, run: &RunTree, _query: &str) {
97 self.on_run_start(run).await;
98 }
99
100 async fn on_retriever_end(&self, run: &RunTree, _documents: &[serde_json::Value]) {
102 self.on_run_end(run).await;
103 }
104
105 async fn on_retriever_error(&self, run: &RunTree, error: &str) {
107 self.on_run_error(run, error).await;
108 }
109}
110
111pub struct CallbackManager {
113 inner: Arc<CallbackManagerInner>,
114}
115
116struct CallbackManagerInner {
117 handlers: Vec<Arc<dyn CallbackHandler>>,
118}
119
120impl std::fmt::Debug for CallbackManager {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 f.debug_struct("CallbackManager")
123 .field("handlers_count", &self.inner.handlers.len())
124 .finish()
125 }
126}
127
128impl CallbackManager {
129 pub fn new() -> Self {
131 Self {
132 inner: Arc::new(CallbackManagerInner {
133 handlers: Vec::new(),
134 }),
135 }
136 }
137
138 pub fn add_handler(self, handler: Arc<dyn CallbackHandler>) -> Self {
140 let mut handlers = self.inner.handlers.clone();
141 handlers.push(handler);
142 Self {
143 inner: Arc::new(CallbackManagerInner { handlers }),
144 }
145 }
146
147 pub fn handlers(&self) -> &[Arc<dyn CallbackHandler>] {
149 &self.inner.handlers
150 }
151
152 pub fn is_empty(&self) -> bool {
154 self.inner.handlers.is_empty()
155 }
156}
157
158impl Default for CallbackManager {
159 fn default() -> Self {
160 Self::new()
161 }
162}
163
164impl Clone for CallbackManager {
165 fn clone(&self) -> Self {
166 Self {
167 inner: Arc::clone(&self.inner),
168 }
169 }
170}