Skip to main content

subspace_chat/agent_chat/
engine.rs

1//! Engine-specific capabilities, names, marks and user guidance for the
2//! shared panel.
3use super::{ChatEngine, ChatPanel};
4use subspace_agent::AcpAgent;
5use subspace_core::features::{FeatureSpec, FeatureStage, PI_CHAT};
6use subspace_lucide::{IconName, SvgIcon};
7
8// Product marks identify the engine that runs the chat. Lucide carries no
9// brand marks; assets/PROVENANCE.md records each file's source and licence.
10const COPILOT_MARK: SvgIcon = SvgIcon::new(include_bytes!("../../assets/copilot.svg"));
11const OPENAI_MARK: SvgIcon = SvgIcon::new(include_bytes!("../../assets/openai.svg"));
12const CLAUDE_MARK: SvgIcon = SvgIcon::new(include_bytes!("../../assets/claude.svg"));
13const OPENCODE_MARK: SvgIcon = SvgIcon::new(include_bytes!("../../assets/opencode.svg"));
14const GEMINI_MARK: SvgIcon = SvgIcon::new(include_bytes!("../../assets/gemini.svg"));
15
16const COPILOT_GUIDANCE: &str = concat!(
17    "Ask about your work. Copilot uses the GitHub account chosen in Settings > GitHub. ",
18    "Never paste saved service tokens into chat."
19);
20const COPILOT_AUTHORIZATION_GUIDANCE: &str = concat!(
21    "Subspace chat needs Copilot CLI/SDK authorization, separate from VS Code Copilot. ",
22    "If your organization blocks this access, contact your administrator. ",
23    "Reconnect after access is approved."
24);
25const PI_GUIDANCE: &str = concat!(
26    "Ask about your work. Pi uses its existing provider authentication. ",
27    "Never paste saved service tokens into chat."
28);
29const ACP_GUIDANCE: &str = concat!(
30    "Ask about your work. The agent uses its own sign-in, tools and permission rules; ",
31    "approve its requests here. Never paste saved service tokens into chat."
32);
33const PI_EXPERIMENTAL_GUIDANCE: &str = concat!(
34    "Pi is experimental and must be enabled in Settings > Experiments. ",
35    "It uses its existing provider authentication. Never paste saved service tokens into chat."
36);
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub(super) struct EngineCapabilities {
40    pub(super) copilot_controls: bool,
41}
42
43impl EngineCapabilities {
44    fn for_engine(engine: ChatEngine) -> Self {
45        Self {
46            copilot_controls: engine == ChatEngine::Copilot,
47        }
48    }
49
50    pub(super) fn cloud_replay(self) -> bool {
51        self.copilot_controls
52    }
53
54    pub(super) fn ai_units(self) -> bool {
55        self.copilot_controls
56    }
57}
58
59impl ChatPanel {
60    pub fn engine(&self) -> ChatEngine {
61        self.engine
62    }
63
64    pub fn is_disabled(&self) -> bool {
65        self.engine == ChatEngine::Off
66    }
67
68    pub fn engine_label(&self) -> &'static str {
69        engine_label(self.engine, self.acp_agent)
70    }
71
72    /// The mark shown beside the engine's name wherever the app names it.
73    pub fn engine_icon(&self) -> SvgIcon {
74        engine_icon(self.engine, self.acp_agent)
75    }
76
77    pub(super) fn capabilities(&self) -> EngineCapabilities {
78        EngineCapabilities::for_engine(self.engine)
79    }
80
81    pub(super) fn guidance(&self) -> &'static str {
82        match self.engine {
83            ChatEngine::Off => {
84                "Choose an engine in Settings > Chat, then save and restart Subspace."
85            }
86            ChatEngine::Copilot => COPILOT_GUIDANCE,
87            ChatEngine::Pi if PI_CHAT.stage == FeatureStage::Experimental => {
88                PI_EXPERIMENTAL_GUIDANCE
89            }
90            ChatEngine::Pi => PI_GUIDANCE,
91            ChatEngine::Acp => ACP_GUIDANCE,
92        }
93    }
94}
95
96/// The name the panel and its composer use for the engine.
97pub(super) fn engine_label(engine: ChatEngine, agent: AcpAgent) -> &'static str {
98    match engine {
99        ChatEngine::Acp => agent.engine_label(),
100        _ => pi_label(engine, &PI_CHAT),
101    }
102}
103
104/// The product mark for the engine, or a neutral glyph for Pi and a custom
105/// ACP agent.
106fn engine_icon(engine: ChatEngine, agent: AcpAgent) -> SvgIcon {
107    match (engine, agent) {
108        (ChatEngine::Off, _) => IconName::Bot,
109        (ChatEngine::Copilot, _) => COPILOT_MARK,
110        (ChatEngine::Pi, _) => IconName::Bot,
111        (ChatEngine::Acp, AcpAgent::OpenCode) => OPENCODE_MARK,
112        (ChatEngine::Acp, AcpAgent::Claude) => CLAUDE_MARK,
113        (ChatEngine::Acp, AcpAgent::Codex) => OPENAI_MARK,
114        (ChatEngine::Acp, AcpAgent::Gemini) => GEMINI_MARK,
115        (ChatEngine::Acp, AcpAgent::Custom) => IconName::Plug,
116    }
117}
118
119fn pi_label(engine: ChatEngine, pi: &FeatureSpec) -> &'static str {
120    match (engine, pi.stage) {
121        (ChatEngine::Off, _) => "Chat",
122        (ChatEngine::Copilot, _) => "Copilot",
123        (ChatEngine::Pi, FeatureStage::Experimental) => "Pi (Experimental)",
124        (ChatEngine::Pi, FeatureStage::Stable | FeatureStage::Removed) => "Pi",
125        (ChatEngine::Acp, _) => ChatEngine::Acp.label(),
126    }
127}
128
129/// Authorization guidance belongs to a Copilot runtime that stopped and can
130/// be reconnected, not to an error inside a running chat.
131pub(super) fn error_guidance(engine: ChatEngine, reconnect: bool) -> Option<&'static str> {
132    (engine == ChatEngine::Copilot && reconnect).then_some(COPILOT_AUTHORIZATION_GUIDANCE)
133}
134
135#[cfg(test)]
136mod tests {
137    use super::{
138        AcpAgent, CLAUDE_MARK, COPILOT_MARK, ChatEngine, EngineCapabilities, GEMINI_MARK,
139        OPENAI_MARK, OPENCODE_MARK, engine_icon, engine_label, error_guidance, pi_label,
140    };
141    use gpui_kit::{DevicePixels, SvgSize, TestAppContext, size};
142    use subspace_core::features::{FeatureSpec, FeatureStage, PI_CHAT};
143    use subspace_lucide::IconName;
144
145    const MARKS: [super::SvgIcon; 5] = [
146        COPILOT_MARK,
147        OPENAI_MARK,
148        CLAUDE_MARK,
149        OPENCODE_MARK,
150        GEMINI_MARK,
151    ];
152
153    #[test]
154    fn every_engine_and_agent_is_shown_with_its_own_mark() {
155        assert_eq!(
156            engine_icon(ChatEngine::Copilot, AcpAgent::Codex),
157            COPILOT_MARK
158        );
159        assert_eq!(engine_icon(ChatEngine::Pi, AcpAgent::Claude), IconName::Bot);
160        let acp = AcpAgent::ALL.map(|agent| engine_icon(ChatEngine::Acp, agent));
161        assert_eq!(
162            acp,
163            [
164                OPENCODE_MARK,
165                CLAUDE_MARK,
166                OPENAI_MARK,
167                GEMINI_MARK,
168                IconName::Plug
169            ]
170        );
171        for (index, mark) in MARKS.iter().enumerate() {
172            assert!(
173                MARKS[index + 1..].iter().all(|other| other != mark),
174                "mark {index} is not distinct"
175            );
176        }
177    }
178
179    #[gpui_kit::test]
180    fn every_mark_is_a_tintable_svg_that_renders_at_glyph_size(cx: &mut TestAppContext) {
181        let renderer = cx.update(|cx| cx.svg_renderer());
182        for mark in MARKS {
183            let text = std::str::from_utf8(mark.bytes()).unwrap();
184            assert!(text.contains("fill=\"currentColor\""), "{text}");
185            assert!(!text.contains('#'), "a mark must not fix its own colour");
186            let svg = renderer.parse_svg(mark.bytes()).unwrap();
187            let glyph = size(DevicePixels(16), DevicePixels(16));
188            let image = renderer
189                .render_parsed(&svg, SvgSize::ExactSize(glyph))
190                .unwrap();
191            assert_eq!(image.size(0), glyph);
192            let (pixels, _) = image.as_bytes(0).unwrap().as_chunks::<4>();
193            assert!(pixels.iter().filter(|pixel| pixel[3] > 0).count() > 16);
194        }
195    }
196
197    #[test]
198    fn local_copilot_errors_explain_authorization_without_diagnosing_a_policy_block() {
199        let guidance = error_guidance(ChatEngine::Copilot, true).unwrap();
200        assert!(guidance.contains("Copilot CLI/SDK authorization, separate from VS Code Copilot"));
201        assert!(
202            guidance
203                .contains("If your organization blocks this access, contact your administrator")
204        );
205        assert!(
206            error_guidance(ChatEngine::Copilot, false).is_none(),
207            "an error in a running chat is not an authorization problem"
208        );
209        assert!(error_guidance(ChatEngine::Pi, false).is_none());
210        assert!(error_guidance(ChatEngine::Pi, true).is_none());
211    }
212
213    #[test]
214    fn pi_keeps_shared_chat_features_but_not_copilot_controls() {
215        let pi = EngineCapabilities::for_engine(ChatEngine::Pi);
216        assert!(!pi.copilot_controls);
217        assert!(!pi.cloud_replay());
218        assert!(!pi.ai_units());
219
220        let copilot = EngineCapabilities::for_engine(ChatEngine::Copilot);
221        assert!(copilot.copilot_controls);
222        assert!(copilot.cloud_replay());
223        assert!(copilot.ai_units());
224    }
225
226    #[test]
227    fn pi_branding_follows_its_registered_lifecycle() {
228        assert_eq!(pi_label(ChatEngine::Pi, &PI_CHAT), "Pi (Experimental)");
229        let stable = FeatureSpec {
230            stage: FeatureStage::Stable,
231            ..PI_CHAT
232        };
233        assert_eq!(pi_label(ChatEngine::Pi, &stable), "Pi");
234        assert_eq!(pi_label(ChatEngine::Copilot, &PI_CHAT), "Copilot");
235    }
236
237    #[test]
238    fn acp_chat_is_named_after_its_agent() {
239        let names = AcpAgent::ALL.map(|agent| engine_label(ChatEngine::Acp, agent));
240        assert_eq!(
241            names,
242            ["OpenCode", "Claude", "Codex", "Gemini", "ACP agent"]
243        );
244        assert_eq!(
245            engine_label(ChatEngine::Copilot, AcpAgent::Claude),
246            "Copilot"
247        );
248        let acp = EngineCapabilities::for_engine(ChatEngine::Acp);
249        assert!(!acp.copilot_controls);
250        assert!(error_guidance(ChatEngine::Acp, true).is_none());
251    }
252}