Skip to main content

agent_commander/
tool_overrides.rs

1//! Per-run tool overrides (issue #57).
2//!
3//! A caller replaces a failing or missing part of a tool for one run — the
4//! command builder, the output parser, the session ID or usage extractor, or
5//! the launch capabilities — without waiting for an agent-commander release.
6//! With a `build_command` an override also drives a tool that is not
7//! registered. Unset fields keep the built-in behavior.
8
9use crate::command_builder::AgentCommandOptions;
10use crate::tools::launch_options::LaunchCapabilities;
11use serde_json::Value;
12use std::fmt;
13use std::panic::RefUnwindSafe;
14use std::sync::Arc;
15
16/// Builds the native tool command (without the `cd` and isolation wrappers)
17pub type BuildCommandFn = Arc<dyn Fn(&AgentCommandOptions) -> String + Send + Sync + RefUnwindSafe>;
18/// Parses the tool's stdout into messages
19pub type ParseOutputFn = Arc<dyn Fn(&str) -> Vec<Value> + Send + Sync + RefUnwindSafe>;
20/// Reads the session ID from the tool's stdout
21pub type ExtractSessionIdFn = Arc<dyn Fn(&str) -> Option<String> + Send + Sync + RefUnwindSafe>;
22/// Reads the token usage from the tool's stdout
23pub type ExtractUsageFn = Arc<dyn Fn(&str) -> Option<Value> + Send + Sync + RefUnwindSafe>;
24
25/// Replacements for parts of a tool; `None` keeps the built-in part
26#[derive(Clone, Default)]
27pub struct ToolOverrides {
28    /// Replaces the tool-specific command builder; it can call
29    /// [`crate::command_builder::build_base_command`] and extend the result
30    pub build_command: Option<BuildCommandFn>,
31    /// Replaces the output parser, including the streamed messages
32    pub parse_output: Option<ParseOutputFn>,
33    /// Replaces the session ID extractor
34    pub extract_session_id: Option<ExtractSessionIdFn>,
35    /// Replaces the usage extractor
36    pub extract_usage: Option<ExtractUsageFn>,
37    /// Replaces the launch capabilities checked before the run, so a builder
38    /// that adds a native flag can accept the matching launch option
39    pub launch_capabilities: Option<LaunchCapabilities>,
40}
41
42impl ToolOverrides {
43    /// Whether no part is replaced
44    pub fn is_empty(&self) -> bool {
45        self.build_command.is_none()
46            && self.parse_output.is_none()
47            && self.extract_session_id.is_none()
48            && self.extract_usage.is_none()
49            && self.launch_capabilities.is_none()
50    }
51}
52
53impl fmt::Debug for ToolOverrides {
54    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
55        formatter
56            .debug_struct("ToolOverrides")
57            .field("build_command", &self.build_command.is_some())
58            .field("parse_output", &self.parse_output.is_some())
59            .field("extract_session_id", &self.extract_session_id.is_some())
60            .field("extract_usage", &self.extract_usage.is_some())
61            .field("launch_capabilities", &self.launch_capabilities)
62            .finish()
63    }
64}