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