subspace_chat/agent_chat/
tabbing.rs1use super::copilot_only::{self, CopilotOnly};
4use super::interaction::chat_preferences;
5use super::tabs::{
6 SessionChoice, Tab, can_open, choose_session, index_after_close, stepped_index, tab_index,
7};
8use super::{ChatCommand, ChatId, ChatPanel, Command, Context, Window};
9use crate::CloudSession;
10
11impl ChatPanel {
12 pub fn show_session(&mut self, id: &str, window: &mut Window, cx: &mut Context<Self>) {
15 self.open_session(id, window, cx);
16 }
17
18 pub(super) fn focus_chat(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) {
21 self.sessions_open = false;
22 self.select_tab(index, window, cx);
23 }
24
25 pub(super) fn select_tab(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) {
26 if index >= self.tabs.len() || (index == self.active && !self.sessions_open) {
27 return;
28 }
29 self.remember_draft(cx);
30 self.renaming = None;
31 self.sessions_open = false;
32 self.active = index;
33 self.show_draft(window, cx);
34 let chat = self.tab().id;
36 self.send(Command::Activate(chat), cx);
37 self.sync_toolbar(cx);
38 cx.notify();
39 }
40
41 pub(super) fn step_tab(&mut self, step: isize, window: &mut Window, cx: &mut Context<Self>) {
42 let index = stepped_index(self.tabs.len(), self.active, step);
43 self.select_tab(index, window, cx);
44 }
45
46 pub(super) fn new_tab(&mut self, window: &mut Window, cx: &mut Context<Self>) {
48 self.open_tab(None, window, cx);
49 }
50
51 fn open_tab(&mut self, session: Option<String>, window: &mut Window, cx: &mut Context<Self>) {
52 let Some(chat) = self.claim_chat(cx) else {
53 return;
54 };
55 let opened = if session.is_none() {
56 self.send(new_chat_command(chat, cx), cx)
57 } else {
58 self.send(
59 Command::OpenChat {
60 chat,
61 session,
62 preferences: None,
63 },
64 cx,
65 )
66 };
67 let _ = opened.then(|| self.place_tab(Tab::opened(chat), window, cx));
68 }
69
70 pub fn cloud_replay_refusal(&self) -> Option<&'static str> {
75 copilot_only::refuses_replay(
76 self.capabilities().cloud_replay(),
77 self.is_disabled(),
78 subspace_agent::COPILOT_BUILT_IN,
79 )
80 .then(|| copilot_only::unavailable(CopilotOnly::Replay))
81 }
82
83 pub fn show_cloud_session(
88 &mut self,
89 task: &CloudSession,
90 window: &mut Window,
91 cx: &mut Context<Self>,
92 ) {
93 if self.is_disabled() {
94 return;
95 }
96 if !self.capabilities().cloud_replay() {
97 self.tab_mut()
98 .set_error(copilot_only::unavailable(CopilotOnly::Replay).into());
99 self.log_accepted_errors(cx);
100 cx.notify();
101 return;
102 }
103 let Some(chat) = self.claim_chat(cx) else {
104 return;
105 };
106 let command = Command::OpenRemote {
107 chat,
108 task_id: task.task_id.to_string(),
109 };
110 let opened = self.send(command, cx);
111 let _ = opened.then(|| self.place_tab(Tab::replay(chat, task), window, cx));
112 }
113
114 fn claim_chat(&mut self, cx: &mut Context<Self>) -> Option<ChatId> {
117 if let Err(refused) = can_open(self.tabs.len()) {
118 self.tab_mut().set_error(refused);
119 self.log_accepted_errors(cx);
120 cx.notify();
121 return None;
122 }
123 self.remember_draft(cx);
124 let chat = self.next_chat;
125 self.next_chat += 1;
126 Some(chat)
127 }
128
129 fn place_tab(&mut self, tab: Tab, window: &mut Window, cx: &mut Context<Self>) {
131 self.tabs.push(tab);
132 self.active = self.tabs.len() - 1;
133 self.renaming = None;
134 self.sessions_open = false;
135 self.show_draft(window, cx);
136 self.sync_toolbar(cx);
137 cx.notify();
138 }
139
140 pub(super) fn close_tab(&mut self, id: ChatId, window: &mut Window, cx: &mut Context<Self>) {
142 let Some(index) = tab_index(&self.tabs, id) else {
143 return;
144 };
145 if self.tabs.len() == 1 {
148 self.new_tab(window, cx);
149 }
150 if self.tabs.len() == 1 {
151 return;
152 }
153 self.send(
154 Command::Chat {
155 chat: id,
156 command: ChatCommand::Close,
157 },
158 cx,
159 );
160 self.remember_draft(cx);
161 self.active = index_after_close(self.tabs.len(), index, self.active);
162 self.tabs.remove(index);
163 self.renaming = None;
164 self.show_draft(window, cx);
165 self.sync_toolbar(cx);
166 cx.notify();
167 }
168
169 pub(super) fn open_session(
171 &mut self,
172 session: &str,
173 window: &mut Window,
174 cx: &mut Context<Self>,
175 ) {
176 match choose_session(&self.tabs, session) {
177 SessionChoice::Focus(index) => self.focus_chat(index, window, cx),
178 SessionChoice::Open(session) => self.open_tab(Some(session), window, cx),
179 }
180 }
181}
182
183fn new_chat_command(chat: ChatId, cx: &super::App) -> Command {
184 Command::OpenChat {
185 chat,
186 session: None,
187 preferences: Some(chat_preferences(cx)),
188 }
189}
190
191#[cfg(test)]
192mod tests {
193 use super::new_chat_command;
194 use crate::prefs;
195 use subspace_agent::Command;
196
197 #[gpui_kit::test]
198 fn same_app_context_refreshes_new_chat_snapshot_without_mutating_old_one(
199 cx: &mut gpui_kit::TestAppContext,
200 ) {
201 cx.update(|cx| {
202 prefs::init_file(None, cx);
203 prefs::set(cx, "chat.default-model.copilot", "gpt-5");
204 let first = new_chat_command(4, cx);
205 prefs::set(cx, "chat.approve-all-default", "1");
206 let second = new_chat_command(5, cx);
207 prefs::set(cx, "unrelated.preference", "kept");
208 prefs::save(cx).unwrap();
209 assert!(matches!(
210 first,
211 Command::OpenChat {
212 preferences: Some(found), ..
213 } if found.copilot_model.as_deref() == Some("gpt-5") && !found.approve_all
214 ));
215 assert!(matches!(
216 second,
217 Command::OpenChat {
218 preferences: Some(found), ..
219 } if found.copilot_model.as_deref() == Some("gpt-5") && found.approve_all
220 ));
221 assert_eq!(
222 prefs::get(cx, "chat.default-model.copilot").as_deref(),
223 Some("gpt-5")
224 );
225 assert_eq!(
226 prefs::get(cx, "unrelated.preference").as_deref(),
227 Some("kept")
228 );
229 let restored = Command::OpenChat {
230 chat: 6,
231 session: Some("saved".into()),
232 preferences: None,
233 };
234 assert!(matches!(
235 restored,
236 Command::OpenChat {
237 preferences: None,
238 ..
239 }
240 ));
241 });
242 }
243}