subspace_chat/agent_chat/
engine.rs1use super::{ChatEngine, ChatPanel};
4use subspace_agent::AcpAgent;
5use subspace_core::features::{FeatureSpec, FeatureStage, PI_CHAT};
6use subspace_lucide::{IconName, SvgIcon};
7
8const 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 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 if self.copilot_kept => subspace_agent::COPILOT_UNAVAILABLE,
84 ChatEngine::Off => {
85 "Choose an engine in Settings > Chat, then save and restart Subspace."
86 }
87 ChatEngine::Copilot => COPILOT_GUIDANCE,
88 ChatEngine::Pi if PI_CHAT.stage == FeatureStage::Experimental => {
89 PI_EXPERIMENTAL_GUIDANCE
90 }
91 ChatEngine::Pi => PI_GUIDANCE,
92 ChatEngine::Acp => ACP_GUIDANCE,
93 }
94 }
95}
96
97pub(super) fn runnable(configured: ChatEngine, copilot_built_in: bool) -> (ChatEngine, bool) {
102 match configured {
103 ChatEngine::Copilot if !copilot_built_in => (ChatEngine::Off, true),
104 engine => (engine, false),
105 }
106}
107
108pub(super) fn engine_label(engine: ChatEngine, agent: AcpAgent) -> &'static str {
110 match engine {
111 ChatEngine::Acp => agent.engine_label(),
112 _ => pi_label(engine, &PI_CHAT),
113 }
114}
115
116fn engine_icon(engine: ChatEngine, agent: AcpAgent) -> SvgIcon {
119 match (engine, agent) {
120 (ChatEngine::Off, _) => IconName::Bot,
121 (ChatEngine::Copilot, _) => COPILOT_MARK,
122 (ChatEngine::Pi, _) => IconName::Bot,
123 (ChatEngine::Acp, AcpAgent::OpenCode) => OPENCODE_MARK,
124 (ChatEngine::Acp, AcpAgent::Claude) => CLAUDE_MARK,
125 (ChatEngine::Acp, AcpAgent::Codex) => OPENAI_MARK,
126 (ChatEngine::Acp, AcpAgent::Gemini) => GEMINI_MARK,
127 (ChatEngine::Acp, AcpAgent::Custom) => IconName::Plug,
128 }
129}
130
131fn pi_label(engine: ChatEngine, pi: &FeatureSpec) -> &'static str {
132 match (engine, pi.stage) {
133 (ChatEngine::Off, _) => "Chat",
134 (ChatEngine::Copilot, _) => "Copilot",
135 (ChatEngine::Pi, FeatureStage::Experimental) => "Pi (Experimental)",
136 (ChatEngine::Pi, FeatureStage::Stable | FeatureStage::Removed) => "Pi",
137 (ChatEngine::Acp, _) => ChatEngine::Acp.label(),
138 }
139}
140
141pub(super) fn error_guidance(engine: ChatEngine, reconnect: bool) -> Option<&'static str> {
144 (engine == ChatEngine::Copilot && reconnect).then_some(COPILOT_AUTHORIZATION_GUIDANCE)
145}
146
147#[cfg(test)]
148mod tests {
149 use super::{
150 AcpAgent, CLAUDE_MARK, COPILOT_MARK, ChatEngine, EngineCapabilities, GEMINI_MARK,
151 OPENAI_MARK, OPENCODE_MARK, engine_icon, engine_label, error_guidance, pi_label, runnable,
152 };
153 use gpui_kit::{DevicePixels, SvgSize, TestAppContext, size};
154 use subspace_core::features::{FeatureSpec, FeatureStage, PI_CHAT};
155 use subspace_lucide::IconName;
156
157 const MARKS: [super::SvgIcon; 5] = [
158 COPILOT_MARK,
159 OPENAI_MARK,
160 CLAUDE_MARK,
161 OPENCODE_MARK,
162 GEMINI_MARK,
163 ];
164
165 #[test]
166 fn every_engine_and_agent_is_shown_with_its_own_mark() {
167 assert_eq!(
168 engine_icon(ChatEngine::Copilot, AcpAgent::Codex),
169 COPILOT_MARK
170 );
171 assert_eq!(engine_icon(ChatEngine::Pi, AcpAgent::Claude), IconName::Bot);
172 let acp = AcpAgent::ALL.map(|agent| engine_icon(ChatEngine::Acp, agent));
173 assert_eq!(
174 acp,
175 [
176 OPENCODE_MARK,
177 CLAUDE_MARK,
178 OPENAI_MARK,
179 GEMINI_MARK,
180 IconName::Plug
181 ]
182 );
183 for (index, mark) in MARKS.iter().enumerate() {
184 assert!(
185 MARKS[index + 1..].iter().all(|other| other != mark),
186 "mark {index} is not distinct"
187 );
188 }
189 }
190
191 #[gpui_kit::test]
192 fn every_mark_is_a_tintable_svg_that_renders_at_glyph_size(cx: &mut TestAppContext) {
193 let renderer = cx.update(|cx| cx.svg_renderer());
194 for mark in MARKS {
195 let text = std::str::from_utf8(mark.bytes()).unwrap();
196 assert!(text.contains("fill=\"currentColor\""), "{text}");
197 assert!(!text.contains('#'), "a mark must not fix its own colour");
198 let svg = renderer.parse_svg(mark.bytes()).unwrap();
199 let glyph = size(DevicePixels(16), DevicePixels(16));
200 let image = renderer
201 .render_parsed(&svg, SvgSize::ExactSize(glyph))
202 .unwrap();
203 assert_eq!(image.size(0), glyph);
204 let (pixels, _) = image.as_bytes(0).unwrap().as_chunks::<4>();
205 assert!(pixels.iter().filter(|pixel| pixel[3] > 0).count() > 16);
206 }
207 }
208
209 #[test]
210 fn local_copilot_errors_explain_authorization_without_diagnosing_a_policy_block() {
211 let guidance = error_guidance(ChatEngine::Copilot, true).unwrap();
212 assert!(guidance.contains("Copilot CLI/SDK authorization, separate from VS Code Copilot"));
213 assert!(
214 guidance
215 .contains("If your organization blocks this access, contact your administrator")
216 );
217 assert!(
218 error_guidance(ChatEngine::Copilot, false).is_none(),
219 "an error in a running chat is not an authorization problem"
220 );
221 assert!(error_guidance(ChatEngine::Pi, false).is_none());
222 assert!(error_guidance(ChatEngine::Pi, true).is_none());
223 }
224
225 #[test]
226 fn pi_keeps_shared_chat_features_but_not_copilot_controls() {
227 let pi = EngineCapabilities::for_engine(ChatEngine::Pi);
228 assert!(!pi.copilot_controls);
229 assert!(!pi.cloud_replay());
230 assert!(!pi.ai_units());
231
232 let copilot = EngineCapabilities::for_engine(ChatEngine::Copilot);
233 assert!(copilot.copilot_controls);
234 assert!(copilot.cloud_replay());
235 assert!(copilot.ai_units());
236 }
237
238 #[test]
239 fn a_build_without_copilot_runs_no_engine_for_a_saved_copilot_choice() {
240 assert_eq!(
241 runnable(ChatEngine::Copilot, false),
242 (ChatEngine::Off, true)
243 );
244 assert_eq!(
245 runnable(ChatEngine::Copilot, true),
246 (ChatEngine::Copilot, false)
247 );
248 for engine in [ChatEngine::Off, ChatEngine::Pi, ChatEngine::Acp] {
249 assert_eq!(runnable(engine, false), (engine, false));
250 assert_eq!(runnable(engine, true), (engine, false));
251 }
252 }
253
254 #[test]
255 fn pi_branding_follows_its_registered_lifecycle() {
256 assert_eq!(pi_label(ChatEngine::Pi, &PI_CHAT), "Pi (Experimental)");
257 let stable = FeatureSpec {
258 stage: FeatureStage::Stable,
259 ..PI_CHAT
260 };
261 assert_eq!(pi_label(ChatEngine::Pi, &stable), "Pi");
262 assert_eq!(pi_label(ChatEngine::Copilot, &PI_CHAT), "Copilot");
263 }
264
265 #[test]
266 fn acp_chat_is_named_after_its_agent() {
267 let names = AcpAgent::ALL.map(|agent| engine_label(ChatEngine::Acp, agent));
268 assert_eq!(
269 names,
270 ["OpenCode", "Claude", "Codex", "Gemini", "ACP agent"]
271 );
272 assert_eq!(
273 engine_label(ChatEngine::Copilot, AcpAgent::Claude),
274 "Copilot"
275 );
276 let acp = EngineCapabilities::for_engine(ChatEngine::Acp);
277 assert!(!acp.copilot_controls);
278 assert!(error_guidance(ChatEngine::Acp, true).is_none());
279 }
280}