langsmith_rust/tracing/
graph.rs1use crate::error::Result;
2use crate::models::run::RunType;
3use crate::tracing::scope::RunScope;
4use crate::tracing::tracer::Tracer;
5use serde_json::Value;
6
7pub struct GraphTrace {
15 root: RunScope,
16}
17
18impl GraphTrace {
19 pub async fn start_root(inputs: Value, thread_id: Option<String>) -> Result<Self> {
21 let mut root = RunScope::root_value("Graph", RunType::Chain, inputs);
22 if let Some(tid) = thread_id {
23 root = root.with_thread_id(tid);
24 }
25 root.post_start().await?;
26 Ok(Self { root })
27 }
28
29 pub fn root_scope(&self) -> &RunScope {
30 &self.root
31 }
32
33 pub fn root_tracer(&self) -> &Tracer {
34 self.root.tracer()
35 }
36
37 pub async fn start_node_iteration(&self, node_name: &str, inputs: Value) -> Result<RunScope> {
40 let mut step = self.root.child_value(node_name, RunType::Chain, inputs);
41 step.post_start().await?;
42 Ok(step)
43 }
44
45 pub async fn trace_llm_call(
49 &self,
50 parent_node: &RunScope,
51 llm_name: &str,
52 inputs: Value,
53 outputs: Value,
54 model_name: Option<&str>,
55 ) -> Result<()> {
56 let mut llm_inputs = inputs;
57 if let Some(model) = model_name {
58 if let Some(obj) = llm_inputs.as_object_mut() {
59 obj.insert("model".to_string(), serde_json::json!(model));
60 }
61 }
62 let mut llm = parent_node.child_value(llm_name, RunType::Llm, llm_inputs);
63 llm.post_start().await?;
64 llm.end_ok(outputs).await
65 }
66
67 pub async fn trace_decision(
69 &self,
70 parent_node: &RunScope,
71 decision_name: &str,
72 inputs: Value,
73 outputs: Value,
74 ) -> Result<()> {
75 let mut decision = parent_node.child_value(decision_name, RunType::Chain, inputs);
76 decision.post_start().await?;
77 decision.end_ok(outputs).await
78 }
79
80 pub async fn trace_tool_call(
83 &self,
84 parent_node: &RunScope,
85 tool_name: &str,
86 inputs: Value,
87 outputs: Value,
88 ) -> Result<()> {
89 let formatted_name = format!("tool/{}", tool_name);
91 let mut tool = parent_node.child_value(&formatted_name, RunType::Tool, inputs);
92 tool.post_start().await?;
93 tool.end_ok(outputs).await
94 }
95
96 pub async fn end_root(self, outputs: Value) -> Result<()> {
98 self.root.end_ok(outputs).await
99 }
100}
101
102