Skip to main content

subspace_core/config/
acp.rs

1//! `chat.acp`: the Agent Client Protocol agent that the ACP chat engine runs.
2//! The runtime owns each preset's command; this file only names the choice.
3
4use std::collections::BTreeMap;
5
6use serde::{Deserialize, Serialize};
7
8/// An ACP agent preset. `Custom` runs `chat.acp.command`.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)]
10#[serde(rename_all = "lowercase")]
11pub enum AcpAgent {
12    #[default]
13    OpenCode,
14    Claude,
15    Codex,
16    Gemini,
17    Custom,
18}
19
20impl AcpAgent {
21    /// Every preset, in the order Settings lists them.
22    pub const ALL: [Self; 5] = [
23        Self::OpenCode,
24        Self::Claude,
25        Self::Codex,
26        Self::Gemini,
27        Self::Custom,
28    ];
29
30    /// The `chat.acp.agent` value.
31    pub fn id(self) -> &'static str {
32        self.names().0
33    }
34
35    pub fn from_id(id: &str) -> Option<Self> {
36        Self::ALL.into_iter().find(|agent| agent.id() == id)
37    }
38
39    /// The agent's own product name.
40    pub fn label(self) -> &'static str {
41        self.names().1
42    }
43
44    /// The short name chat shows for the engine, such as "Codex ready".
45    pub fn engine_label(self) -> &'static str {
46        self.names().2
47    }
48
49    /// The config id, the product name and the engine name.
50    fn names(self) -> (&'static str, &'static str, &'static str) {
51        match self {
52            AcpAgent::OpenCode => ("opencode", "OpenCode", "OpenCode"),
53            AcpAgent::Claude => ("claude", "Claude Code", "Claude"),
54            AcpAgent::Codex => ("codex", "Codex", "Codex"),
55            AcpAgent::Gemini => ("gemini", "Gemini CLI", "Gemini"),
56            AcpAgent::Custom => ("custom", "Custom agent", "ACP agent"),
57        }
58    }
59}
60
61/// The selected agent, and an optional command that replaces its preset.
62#[derive(Debug, Clone, PartialEq, Eq, Default)]
63pub struct AcpConfig {
64    pub agent: AcpAgent,
65    /// The executable to run instead of the preset's. `custom` requires it.
66    pub command: Option<String>,
67    /// The arguments to pass instead of the preset's.
68    pub args: Option<Vec<String>>,
69    /// Extra environment variables for the agent process.
70    pub env: BTreeMap<String, String>,
71}
72
73#[cfg(test)]
74mod tests {
75    use super::AcpAgent;
76
77    #[test]
78    fn every_agent_round_trips_through_its_config_id() {
79        for agent in AcpAgent::ALL {
80            assert_eq!(AcpAgent::from_id(agent.id()), Some(agent));
81            assert_eq!(
82                serde_json::to_value(agent).unwrap(),
83                serde_json::Value::String(agent.id().into())
84            );
85        }
86        assert_eq!(AcpAgent::from_id("cursor"), None);
87        assert_eq!(AcpAgent::default(), AcpAgent::OpenCode);
88        assert_eq!(AcpAgent::OpenCode.engine_label(), "OpenCode");
89        assert_eq!(AcpAgent::Claude.engine_label(), "Claude");
90        assert_eq!(AcpAgent::Gemini.label(), "Gemini CLI");
91        assert_eq!(AcpAgent::Gemini.engine_label(), "Gemini");
92    }
93}