Skip to main content

nexus_core/app/
spaces.rs

1use anyhow::Result;
2
3use super::App;
4use crate::db::Space as SpaceRow;
5
6impl App {
7    /// Switch back to the default space (used when the active space is
8    /// deleted from the picker — view layer calls this, so it's pub).
9    pub fn switch_to_default_space(&mut self) -> Result<()> {
10        let default_id = self.db.default_space_id()?;
11        let row = self
12            .db
13            .list_spaces()?
14            .into_iter()
15            .find(|s| s.id == default_id)
16            .ok_or_else(|| anyhow::anyhow!("default space {default_id:?} no longer exists"))?;
17        self.set_active_space(row);
18        Ok(())
19    }
20
21    /// Switch the active space, clearing the open conversation (a session
22    /// belongs to exactly one space). The space picker's confirm (view layer)
23    /// calls this after closing its popup.
24    pub fn set_active_space(&mut self, row: SpaceRow) {
25        self.active_space = row;
26        self.session = None;
27        self.messages.clear();
28        self.refresh_memory_snapshot();
29        self.context_total = None;
30        self.push_viewport_reset();
31        self.cleanup_incognito_images();
32        self.rescan_files();
33        self.refresh_toolbox();
34        self.push_status(format!("space: {}", self.active_space.name));
35    }
36
37    /// Path to the highlighted space's instructions file, creating a stub with
38    /// a short header comment if it doesn't exist yet (so $EDITOR has something
39    /// to open). The picker cursor lives in the view layer; callers pass the
40    /// selected space's name.
41    pub fn instructions_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
42        let path = self.space.instructions_path(name);
43        if !path.exists() {
44            let _ = std::fs::write(
45                &path,
46                format!("<!-- instructions for the \"{name}\" space -->\n"),
47            );
48        }
49        Some(path)
50    }
51
52    /// Path to the highlighted space's memory file (the numbered facts a
53    /// conversation in that space has accumulated), creating an empty stub
54    /// with a header comment if nothing's been extracted yet.
55    pub fn memory_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
56        let path = self.space.memory_path(name);
57        if !path.exists() {
58            let _ = std::fs::write(
59                &path,
60                format!(
61                    "<!-- memory for the \"{name}\" space — numbered facts, one per line -->\n"
62                ),
63            );
64        }
65        Some(path)
66    }
67}