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.context_total = None;
29        self.push_viewport_reset();
30        self.cleanup_incognito_images();
31        self.rescan_files();
32        self.refresh_toolbox();
33        self.push_status(format!("space: {}", self.active_space.name));
34    }
35
36    /// Path to the highlighted space's instructions file, creating a stub with
37    /// a short header comment if it doesn't exist yet (so $EDITOR has something
38    /// to open). The picker cursor lives in the view layer; callers pass the
39    /// selected space's name.
40    pub fn instructions_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
41        let path = self.space.instructions_path(name);
42        if !path.exists() {
43            let _ = std::fs::write(
44                &path,
45                format!("<!-- instructions for the \"{name}\" space -->\n"),
46            );
47        }
48        Some(path)
49    }
50
51    /// Path to the highlighted space's memory file (the numbered facts a
52    /// conversation in that space has accumulated), creating an empty stub
53    /// with a header comment if nothing's been extracted yet.
54    pub fn memory_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
55        let path = self.space.memory_path(name);
56        if !path.exists() {
57            let _ = std::fs::write(
58                &path,
59                format!(
60                    "<!-- memory for the \"{name}\" space — numbered facts, one per line -->\n"
61                ),
62            );
63        }
64        Some(path)
65    }
66}