ag_agent/agent/
backend.rs1use std::error::Error;
2use std::fmt;
3use std::path::Path;
4use std::process::Command;
5
6use ag_protocol::TurnPromptAttachment;
7
8use crate::channel::AgentRequestKind;
9use crate::model::agent::ReasoningLevel;
10use crate::model::permission::PermissionMode;
11use crate::model::session::SpeedMode;
12
13pub(super) const MAX_CONCURRENT_SUBAGENTS: usize = 2;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum AgentTransport {
22 AppServer,
24 Cli,
26}
27
28impl AgentTransport {
29 pub fn uses_app_server(self) -> bool {
31 matches!(self, Self::AppServer)
32 }
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub(crate) enum AgentPromptTransport {
38 Argv,
40 Stdin,
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub(crate) enum AppServerThoughtPolicy {
47 None,
49 PhaseLabel,
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub struct BuildCommandRequest<'a> {
56 pub attachments: &'a [TurnPromptAttachment],
58 pub folder: &'a Path,
60 pub main_checkout_root: Option<&'a Path>,
63 pub model: &'a str,
65 pub permission_mode: PermissionMode,
67 pub personality_prompt: Option<&'a str>,
69 pub prompt: &'a str,
71 pub reasoning_level: ReasoningLevel,
75 pub request_kind: &'a AgentRequestKind,
77 pub replay_transcript: Option<&'a str>,
79 pub speed_mode: SpeedMode,
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
85pub enum AgentBackendError {
86 Setup(String),
88 CommandBuild(String),
90}
91
92impl fmt::Display for AgentBackendError {
93 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
94 match self {
95 Self::Setup(message) | Self::CommandBuild(message) => {
96 write!(formatter, "{message}")
97 }
98 }
99 }
100}
101
102impl Error for AgentBackendError {}
103
104#[cfg_attr(any(test, feature = "test-utils"), mockall::automock)]
106pub trait AgentBackend: Send + Sync {
107 fn setup(&self, folder: &Path) -> Result<(), AgentBackendError>;
112
113 fn build_command<'request>(
123 &'request self,
124 request: BuildCommandRequest<'request>,
125 ) -> Result<Command, AgentBackendError>;
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 #[test]
133 fn test_agent_transport_app_server_uses_app_server() {
134 let transport = AgentTransport::AppServer;
136
137 let result = transport.uses_app_server();
139
140 assert!(result);
142 }
143
144 #[test]
145 fn test_agent_transport_cli_does_not_use_app_server() {
146 let transport = AgentTransport::Cli;
148
149 let result = transport.uses_app_server();
151
152 assert!(!result);
154 }
155
156 #[test]
157 fn test_agent_backend_error_setup_displays_message() {
158 let error = AgentBackendError::Setup("setup failed".to_string());
160
161 let display = format!("{error}");
163
164 assert_eq!(display, "setup failed");
166 }
167
168 #[test]
169 fn test_agent_backend_error_command_build_displays_message() {
170 let error = AgentBackendError::CommandBuild("build failed".to_string());
172
173 let display = format!("{error}");
175
176 assert_eq!(display, "build failed");
178 }
179
180 #[test]
181 fn test_agent_backend_error_implements_std_error() {
182 let error = AgentBackendError::Setup("test error".to_string());
184
185 let std_error: &dyn Error = &error;
187
188 assert_eq!(std_error.to_string(), "test error");
190 assert!(std_error.source().is_none());
191 }
192
193 #[test]
194 fn test_agent_backend_error_setup_and_command_build_are_distinct() {
195 let setup_error = AgentBackendError::Setup("failure".to_string());
197 let build_error = AgentBackendError::CommandBuild("failure".to_string());
198
199 assert_ne!(setup_error, build_error);
201 }
202}