Skip to main content

codetether_agent/telemetry/tools/execution/
execution_methods.rs

1//! Builder and finalization methods for [`super::ToolExecution`].
2
3use chrono::Utc;
4
5use super::super::FileChange;
6use super::ToolExecution;
7
8impl ToolExecution {
9    /// Start a new pending execution. `success` defaults to `false` until
10    /// explicitly completed.
11    pub fn start(tool_name: &str, input: serde_json::Value) -> Self {
12        Self {
13            id: uuid::Uuid::new_v4().to_string(),
14            tool_name: tool_name.to_string(),
15            timestamp: Utc::now(),
16            duration_ms: 0,
17            success: false,
18            error: None,
19            tokens_used: None,
20            session_id: None,
21            input: Some(input),
22            file_changes: Vec::new(),
23        }
24    }
25
26    /// Attach a [`FileChange`] the tool performed.
27    pub fn add_file_change(&mut self, change: FileChange) {
28        self.file_changes.push(change);
29    }
30
31    /// Builder-style: associate this execution with a session.
32    pub fn with_session(mut self, session_id: String) -> Self {
33        self.session_id = Some(session_id);
34        self
35    }
36
37    /// In-place finalizer — mutates an existing record.
38    pub fn complete(&mut self, success: bool, duration_ms: u64) {
39        self.success = success;
40        self.duration_ms = duration_ms;
41    }
42
43    /// In-place failure finalizer.
44    pub fn fail(&mut self, error: String, duration_ms: u64) {
45        self.success = false;
46        self.error = Some(error);
47        self.duration_ms = duration_ms;
48    }
49
50    /// Consuming finalizer for success. `_output` is accepted for API
51    /// compatibility and currently discarded.
52    pub fn complete_success(mut self, _output: String, duration: std::time::Duration) -> Self {
53        self.success = true;
54        self.duration_ms = duration.as_millis() as u64;
55        self
56    }
57
58    /// Consuming finalizer for failure.
59    pub fn complete_error(mut self, error: String, duration: std::time::Duration) -> Self {
60        self.success = false;
61        self.error = Some(error);
62        self.duration_ms = duration.as_millis() as u64;
63        self
64    }
65}