Skip to main content

nexus_core/app/
models.rs

1use anyhow::Result;
2use chrono::Utc;
3
4use crate::provider::BackendTag;
5use crate::provider::openrouter::OpenRouter;
6
7use super::{App, ModelPickTarget, Popup};
8
9impl App {
10    /// Rebuild every backend from the on-disk saved credentials (after boot,
11    /// or after a settings change). Keeps `saved` and `backends` in sync.
12    pub fn rebuild_all_backends(&mut self) {
13        self.backends = super::Backends::default();
14        if let Some(k) = self.saved.openrouter_key.clone() {
15            self.backends
16                .set(BackendTag::OpenRouter, OpenRouter::openrouter_flavor(k));
17        }
18        if let Some(k) = self.saved.openai_key.clone() {
19            self.backends.set(BackendTag::OpenAi, OpenRouter::openai(k));
20        }
21        if let Some(k) = self.saved.opencode_key.clone() {
22            self.backends
23                .set(BackendTag::OpencodeGo, OpenRouter::opencode_go(k));
24        }
25        if let Some(c) = self.saved.codex.clone() {
26            self.backends
27                .set(BackendTag::Codex, OpenRouter::openai_codex(c.access));
28        }
29        if self.backends.any() {
30            self.push_status("loading models…  (/model to pick, /help for commands)".to_string());
31        }
32    }
33
34    pub fn resolve_model_backend(&self, id: &str) -> Option<(OpenRouter, String)> {
35        self.backends.resolve(id)
36    }
37
38    /// Resolve a feature (non-session) model that may be a bare wire id or a
39    /// composite id. Feature models default to the session provider's
40    /// research-class default; a composite id picks its own backend.
41    pub fn resolve_utility_model_backend(
42        &self,
43        configured_id: &str,
44    ) -> Option<(OpenRouter, String)> {
45        self.resolve_feature_model_backend(configured_id, OpenRouter::default_utility_model)
46    }
47
48    /// Resolve a feature model by name for a backend that may not be
49    /// `OpenRouter` (used for image/video gen where the user may have picked a
50    /// non-OpenRouter model).
51    pub fn resolve_feature_model_backend(
52        &self,
53        configured_id: &str,
54        default: fn(&OpenRouter) -> &'static str,
55    ) -> Option<(OpenRouter, String)> {
56        let configured_id = configured_id.trim();
57        if !configured_id.is_empty()
58            && let Some((provider, raw)) = self.resolve_model_backend(configured_id)
59            && self.resolved_model_looks_valid(configured_id, provider.backend_tag(), &raw)
60        {
61            return Some((provider, raw));
62        }
63
64        let provider = self
65            .current_model
66            .as_deref()
67            .and_then(|id| self.resolve_model_backend(id).map(|(provider, _)| provider))
68            .or_else(|| {
69                self.backends
70                    .configured_tags()
71                    .first()
72                    .and_then(|tag| self.backends.get(*tag).cloned())
73            })?;
74        Some((provider.clone(), default(&provider).to_string()))
75    }
76
77    /// Whether a resolved feature model id actually exists in the catalog
78    /// (or the catalog is empty/unknown — never silently drop a feature just
79    /// because the catalog isn't fetched yet).
80    fn resolved_model_looks_valid(
81        &self,
82        original_id: &str,
83        backend: BackendTag,
84        raw: &str,
85    ) -> bool {
86        if self.models.is_empty() {
87            // The classic bad state is a legacy OpenRouter id like
88            // `google/gemini-*` being resolved against OpenAI/Codex/Go because
89            // OpenRouter is not configured. Those backends' built-in defaults
90            // do not contain `/`, so treat slashy bare ids as OpenRouter-only.
91            return backend == BackendTag::OpenRouter || !original_id.contains('/');
92        }
93        self.models
94            .iter()
95            .any(|m| m.backend == backend && m.id == raw)
96    }
97
98    /// Fetch every configured backend's catalog concurrently and merge them
99    /// into one list. A backend that fails is dropped from the merge (its
100    /// error is only surfaced if *every* backend failed) — one flaky login
101    /// shouldn't blank out the models of the others. Public so the view layer
102    /// can re-trigger a fetch from the model picker.
103    pub fn fetch_models(&mut self) {
104        let providers: Vec<OpenRouter> = [
105            self.backends.openrouter.clone(),
106            self.backends.openai.clone(),
107            self.backends.opencode.clone(),
108            self.backends.codex.clone(),
109        ]
110        .into_iter()
111        .flatten()
112        .collect();
113        if providers.is_empty() {
114            return;
115        }
116        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
117        self.models_rx = Some(rx);
118        tokio::spawn(async move {
119            let mut set = tokio::task::JoinSet::new();
120            for p in providers {
121                set.spawn(async move { p.list_models().await });
122            }
123            let mut merged = Vec::new();
124            let mut errors = Vec::new();
125            while let Some(joined) = set.join_next().await {
126                match joined {
127                    Ok(Ok(models)) => merged.extend(models),
128                    Ok(Err(e)) => errors.push(e.to_string()),
129                    Err(e) => errors.push(e.to_string()),
130                }
131            }
132            let result = if merged.is_empty() && !errors.is_empty() {
133                Err(errors.join("; "))
134            } else {
135                merged.sort_by(|a, b| a.id.cmp(&b.id));
136                Ok(merged)
137            };
138            let _ = tx.send(result);
139        });
140    }
141
142    /// Context window of the active model, if known.
143    pub fn context_limit(&self) -> Option<u64> {
144        let id = self.current_model.as_deref()?;
145        self.models
146            .iter()
147            .find(|m| super::composite_id(m) == id)
148            .and_then(|m| m.context_length)
149    }
150
151    /// Tokens used by the current session. Exact (from the provider's usage on
152    /// the last response) when idle; a ~4-chars/token estimate while streaming or
153    /// before the first response.
154    /// Estimate is what would actually be *sent* — system/memory prompt, the
155    /// compaction digest (if any), and only the tail after it, not the full
156    /// (possibly much larger) on-screen scrollback.
157    pub fn context_used(&self) -> u64 {
158        if !self.is_streaming()
159            && let Some(total) = self.context_total
160        {
161            return total;
162        }
163        let mut chars = self.system_prompt().chars().count();
164        if let Some(s) = self
165            .session
166            .as_ref()
167            .and_then(|s| s.compact_summary.as_deref())
168        {
169            chars += s.chars().count();
170        }
171        if let Some(name) = &self.forced_skill
172            && let Some(skill) = self.skills.iter().find(|s| &s.name == name)
173        {
174            chars += std::fs::read_to_string(skill.dir.join("SKILL.md"))
175                .map_or(0, |md| crate::skills::skill_body(&md).chars().count());
176        }
177        chars += self
178            .effective_messages()
179            .iter()
180            // The digest transcript row duplicates `compact_summary` (counted
181            // above) — never double-count it.
182            .filter(|m| m.role != "compaction")
183            .map(|m| m.content.chars().count())
184            .sum::<usize>();
185        if let Some(buf) = self.active_streaming_text() {
186            chars += buf.chars().count();
187        }
188        (chars / 4) as u64
189    }
190
191    /// Whether the active model accepts image input (unknown model → false).
192    pub fn current_model_supports_images(&self) -> bool {
193        self.current_model.as_deref().is_some_and(|id| {
194            self.models
195                .iter()
196                .any(|m| super::composite_id(m) == id && m.supports_images)
197        })
198    }
199
200    pub fn reasoning_of(&self, id: &str) -> Option<&str> {
201        let effort = self.reasoning.get(id)?.as_str();
202        self.effort_accepted(id, effort).then_some(effort)
203    }
204
205    /// Whether `effort` is in `model`'s accepted reasoning set, so a stored
206    /// value is only sent when the model actually accepts it. Unknown models
207    /// (not in the loaded catalog) accept anything — never silently drop a
208    /// stored value just because the catalog isn't fetched yet.
209    pub fn effort_accepted(&self, model: &str, effort: &str) -> bool {
210        self.models
211            .iter()
212            .find(|m| super::composite_id(m) == model)
213            .is_none_or(|m| m.reasoning_efforts.iter().any(|e| e.as_str() == effort))
214    }
215
216    /// Set the active model (or a feature model, per the pick target). The
217    /// view layer owns the popup routing; this is the domain half.
218    pub fn pick_model(&mut self, id: &str) -> Result<()> {
219        match self.model_pick_target {
220            ModelPickTarget::Session => {
221                self.current_model = Some(id.to_string());
222                if let Some(session) = &self.session {
223                    self.db.set_session_model(&session.id, id)?;
224                }
225                self.db.mark_model_used(id)?;
226                self.last_used
227                    .insert(id.to_string(), Utc::now().to_rfc3339());
228                self.push_status(format!("model: {id}"));
229            }
230            ModelPickTarget::Memory => {
231                self.memory_model = id.to_string();
232                self.db.set_setting("memory_model", id)?;
233                self.push_status(format!("memory model: {id}"));
234            }
235            ModelPickTarget::Transcriber => {
236                self.transcriber_model = id.to_string();
237                self.db.set_setting("transcriber_model", id)?;
238                self.push_status(format!("image model: {id}"));
239            }
240            ModelPickTarget::Ocr => {
241                self.ocr_model = id.to_string();
242                self.db.set_setting("ocr_model", id)?;
243                self.push_status(format!("OCR model: {id}"));
244            }
245            ModelPickTarget::ImageGen => {
246                self.image_gen_model = id.to_string();
247                self.db.set_setting("image_gen_model", id)?;
248                self.push_status(format!("image gen model: {id}"));
249            }
250            ModelPickTarget::VideoGen => {
251                self.video_gen_model = id.to_string();
252                self.db.set_setting("video_gen_model", id)?;
253                self.push_status(format!("video gen model: {id}"));
254            }
255            ModelPickTarget::SwarmPersona(row) => {
256                if let Some(p) = self.swarm_cache.get_mut(row) {
257                    p.model = id.to_string();
258                }
259                if let Some(session) = &self.session {
260                    let _ = self.db.save_swarm_personas(&session.id, &self.swarm_cache);
261                }
262                self.push_status(format!("persona model: {id}"));
263            }
264        }
265        Ok(())
266    }
267
268    /// The popup a confirmed pick should return to, given the pick target —
269    /// view-layer helper (the picker opens from `/config` for feature models).
270    pub fn popup_after_pick(target: ModelPickTarget) -> Popup {
271        match target {
272            ModelPickTarget::Session => Popup::None,
273            ModelPickTarget::Memory
274            | ModelPickTarget::Transcriber
275            | ModelPickTarget::Ocr
276            | ModelPickTarget::ImageGen
277            | ModelPickTarget::VideoGen => Popup::Settings,
278            ModelPickTarget::SwarmPersona(_) => Popup::Swarm,
279        }
280    }
281
282    /// Disable memory extraction entirely (Backspace on the memory-model row
283    /// in `/config`).
284    pub fn clear_memory_model(&mut self) -> Result<()> {
285        self.memory_model.clear();
286        self.db.set_setting("memory_model", "")?;
287        self.push_status("memory model cleared — extraction disabled".to_string());
288        Ok(())
289    }
290
291    /// Disable image transcription entirely (Backspace on the
292    /// transcriber-model row in `/config`).
293    pub fn clear_transcriber_model(&mut self) -> Result<()> {
294        self.transcriber_model.clear();
295        self.db.set_setting("transcriber_model", "")?;
296        self.push_status("image model cleared — image descriptions disabled".to_string());
297        Ok(())
298    }
299
300    /// Disable VLM OCR (Backspace on the OCR-model row in `/config`).
301    pub fn clear_ocr_model(&mut self) -> Result<()> {
302        self.ocr_model.clear();
303        self.db.set_setting("ocr_model", "")?;
304        self.push_status("OCR model cleared — scanned PDFs use tesseract".to_string());
305        Ok(())
306    }
307
308    /// Disable image generation (Backspace on the image gen model row in `/config`).
309    pub fn clear_image_gen_model(&mut self) -> Result<()> {
310        self.image_gen_model.clear();
311        self.db.set_setting("image_gen_model", "")?;
312        self.push_status("image gen model cleared — generation disabled".to_string());
313        Ok(())
314    }
315
316    /// Disable video generation (Backspace on the video gen model row in `/config`).
317    pub fn clear_video_gen_model(&mut self) -> Result<()> {
318        self.video_gen_model.clear();
319        self.db.set_setting("video_gen_model", "")?;
320        self.push_status("video gen model cleared — generation disabled".to_string());
321        Ok(())
322    }
323
324    /// `/login`: start the `OpenAI` Codex device-code login (the only backend
325    /// without a plain API key). Domain side: spawns the task and owns the
326    /// result channel; the view layer shows the selector.
327    pub fn start_codex_login(&mut self) {
328        // A previous login task can be left around after cancellation/timeout while
329        // the UI has no useful way to resume it. Starting again should replace the
330        // receiver instead of trapping the user behind a stale "already running" gate.
331        self.login_rx = None;
332        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
333        self.login_rx = Some(rx);
334        self.push_status("starting OpenAI Codex login…".to_string());
335        tokio::spawn(async move {
336            let (status_tx, mut status_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
337            let forward = tx.clone();
338            tokio::spawn(async move {
339                while let Some(s) = status_rx.recv().await {
340                    let _ = forward.send(super::LoginMsg::Status(s));
341                }
342            });
343            let result = crate::config::login_openai_codex_device(status_tx)
344                .await
345                .map_err(|e| e.to_string());
346            let _ = tx.send(super::LoginMsg::Done(result));
347        });
348    }
349
350    pub fn on_login_result(&mut self, msg: Option<super::LoginMsg>) {
351        match msg {
352            Some(super::LoginMsg::Status(s)) => self.push_status(s),
353            Some(super::LoginMsg::Done(Ok(creds))) => {
354                self.login_rx = None;
355                self.backends.set(
356                    BackendTag::Codex,
357                    OpenRouter::openai_codex(creds.access.clone()),
358                );
359                self.saved.codex = Some(creds);
360                self.push_status("OpenAI Codex login saved, loading models…".to_string());
361                self.fetch_models();
362                self.refresh_toolbox();
363            }
364            Some(super::LoginMsg::Done(Err(e))) => {
365                self.login_rx = None;
366                self.push_status(format!("OpenAI Codex login failed: {e}"));
367            }
368            None => self.login_rx = None,
369        }
370    }
371}