Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(cli): stop forcing Tokamak sign-in on a fresh jan install
Akarshan Biswas520eadc12e8e
diff --git a/src-tauri/src/core/cli/login.rs b/src-tauri/src/core/cli/login.rsindex ce78dcad5..2a6cd484e 100644--- a/src-tauri/src/core/cli/login.rs+++ b/src-tauri/src/core/cli/login.rs@@ -1,10 +1,10 @@//! Terminal sign-in UX for Tokamak: the plain-stdout counterpart to the TUI's//! `/login` overlay. Both drive [`super::tokamak`]; only presentation differs.//!-//! This runs *before* the TUI starts, which is what makes a fresh install work-//! at all: with no provider configured, session setup fails on "no model-//! specified" before a single frame is drawn, so the key has to be collected-//! here.+//! `jan login` runs this directly. The TUI no longer forces this flow on a+//! fresh install -- it launches with an empty model and shows a one-line+//! notice, letting the user run `/login` (or `jan login`) when they are ready+//! instead of being dropped into a masked key prompt immediately.use std::io::IsTerminal;@@ -16,23 +16,18 @@ const MAX_ATTEMPTS: usize = 3;const KEY_PROMPT: &str = "Paste your Tokamak API key: ";-/// Guarantee a runnable provider before the agent starts, signing the user in if-/// there is none. No-op when anything usable is already configured, so the-/// startup cost on an existing install is one config read.-///-/// Non-interactive (piped stdin) fails with instructions instead of prompting:-/// there is no one there to paste a key.-pub async fn ensure_provider_configured(- project_root: Option<&std::path::Path>,-) -> Result<(), String> {+/// Reject a non-interactive run with nothing configured: there is no terminal+/// to show the sign-in notice in and nobody to act on it. No-op otherwise --+/// an interactive run with no provider proceeds and the TUI shows its own+/// notice instead of forcing a login flow here.+pub fn reject_headless_without_provider(project_root: Option<&std::path::Path>) -> Result<(), String> {if super::providers::has_usable_provider(project_root) {return Ok(());}if !std::io::stdin().is_terminal() {return Err(headless_message());}- println!("No AI provider is configured yet.");- run_login().await+ Ok(())}/// Sign in to Tokamak from a plain terminal: point the user at the API-keysdiff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rsindex 2a0e6488e..1d2224798 100644--- a/src-tauri/src/core/cli/mod.rs+++ b/src-tauri/src/core/cli/mod.rs@@ -622,6 +622,7 @@ fn prepare_agent_session(overrides: ProviderOverrides,yolo: bool,plan: bool,+ require_model: bool,) -> Result<AgentSession, String> {let project_root = resolve_project_root(project);ensure_project(&project_root)?;@@ -642,14 +643,31 @@ fn prepare_agent_session(// model), then the desktop app's currently-selected model (settings.json// inherit). Global config outranks desktop so a standalone agent is// self-sufficient without a desktop install.+ let explicit = model_override.is_some() || overrides.api_key.is_some();let model = model_override.or_else(|| cfg.agent.model.clone()).or_else(|| crate::core::agent::global_config::default_model().ok().flatten())- .or_else(|| crate::core::cli::providers::desktop_selection().model)- .ok_or_else(|| {+ .or_else(|| crate::core::cli::providers::desktop_selection().model);+ // A project or global default can name a model with nobody around to serve+ // it (e.g. this repo's own agent.toml pins one, but a fresh `~/.jan` has no+ // credentials for anything). Trust it only when the user was explicit+ // (--model/--api-key) or some provider can actually be reached; otherwise+ // treat it as unset so the TUI's sign-in notice fires instead of failing on+ // the first message.+ let model = if !require_model+ && !explicit+ && !crate::core::cli::providers::has_usable_provider(Some(&project_root))+ {+ String::new()+ } else {+ model.unwrap_or_default()+ };+ if model.is_empty() && require_model {+ return Err("no model specified: run `jan login` to sign in to Tokamak, or pass --model, set [agent].model in agent.toml, set default_model in ~/.jan/config.toml, or select a model in the desktop app"- .to_string()- })?;+ .to_string(),+ );+ }let max_turns = max_turns_override.or(cfg.agent.max_turns).unwrap_or(DEFAULT_MAX_TURNS);@@ -772,8 +790,15 @@ fn prepare_agent_run() -> Result<PreparedRun, String> {// Non-interactive runs (`agent run`/`step`) have no plan-review handoff, so// plan mode stays a TUI-only startup option.- let session =- prepare_agent_session(project, model_override, max_turns_override, overrides, yolo, false)?;+ let session = prepare_agent_session(+ project,+ model_override,+ max_turns_override,+ overrides,+ yolo,+ false,+ true,+ )?;let project_root = resolve_project_root(project);let (clean_task, injected) = path_refs::resolve_references(task, &project_root);let final_task = if injected.is_empty() {@@ -918,13 +943,16 @@ pub async fn cli_agent_ui(resume: Option<ResumeTarget>,) -> Result<(), String> {let project_root = resolve_project_root(project);- // Fresh install: sign in before session setup, which would otherwise fail on- // "no model specified" with nothing configured. Skipped the moment any- // provider is usable, and bypassed entirely by an explicit --api-key/env key.+ // A non-interactive invocation with nothing configured has no terminal to+ // show the sign-in notice in, so it fails fast with instructions instead.+ // Bypassed by an explicit --api-key/env key.if overrides.api_key.is_none() {- login::ensure_provider_configured(Some(&project_root)).await?;+ login::reject_headless_without_provider(Some(&project_root))?;}- let session = prepare_agent_session(project, model, max_turns, overrides, yolo, plan)?;+ // Fresh install with a terminal attached: launch with no model rather than+ // forcing sign-in here. The TUI shows a one-line notice and `/login` (or+ // `jan login`) picks a model up once the user is ready.+ let session = prepare_agent_session(project, model, max_turns, overrides, yolo, plan, false)?;// TUI threads persist under the project's .jan/agent dir, separate from the// desktop store, so continuing here never mutates desktop threads.let agent_dir = agent_dir_for(&project_root);@@ -1298,4 +1326,71 @@ mod tests {let p = cli_get_data_folder();assert!(!p.as_os_str().is_empty());}++ // ── prepare_agent_session model resolution ────────────────────────────++ /// A project's `agent.toml` naming a model must not paper over "nothing can+ /// actually serve it": with no provider configured (this repo's own+ /// agent.toml pins `tokamak-1-preview`, but a fresh `~/.jan` has no+ /// credentials for it), the TUI path must still come back with an empty+ /// model so its sign-in notice fires instead of a first-message failure.+ #[test]+ fn tui_session_ignores_a_project_model_with_no_usable_provider() {+ crate::core::agent::global_config::with_temp_home(|_| {+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(+ dir.path().join("agent.toml"),+ "[agent]\nmodel = \"tokamak-1-preview\"\n",+ )+ .unwrap();++ let session = prepare_agent_session(+ dir.path().to_str().unwrap(),+ None,+ None,+ ProviderOverrides::default(),+ false,+ false,+ false,+ )+ .expect("TUI session prep must not fail with nothing configured");+ assert_eq!(session.model, "");+ });+ }++ /// The same project config, once a provider is actually usable, must be+ /// trusted again.+ #[test]+ fn tui_session_honors_a_project_model_once_a_provider_is_usable() {+ crate::core::agent::global_config::with_temp_home(|_| {+ crate::core::agent::global_config::set_provider(+ "tokamak",+ crate::core::agent::global_config::ProviderUpdate {+ api_key: Some("tk".into()),+ base_url: Some(crate::core::cli::tokamak::BASE_URL.into()),+ models: Some(vec!["tokamak-1-preview".into()]),+ api_type: None,+ },+ )+ .unwrap();+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(+ dir.path().join("agent.toml"),+ "[agent]\nmodel = \"tokamak-1-preview\"\n",+ )+ .unwrap();++ let session = prepare_agent_session(+ dir.path().to_str().unwrap(),+ None,+ None,+ ProviderOverrides::default(),+ false,+ false,+ false,+ )+ .expect("session prep");+ assert_eq!(session.model, "tokamak-1-preview");+ });+ }}diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex ffac05879..9a1aa5ef5 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -1260,6 +1260,10 @@ impl App {}fn submit_user(&mut self, text: String) {+ if self.model.is_empty() {+ self.note("not signed in — run /login to sign in to Tokamak first");+ return;+ }// If a turn is already in progress, enqueue the message insteadif self.status == Status::Running {self.message_queue.push_back(text.clone());@@ -3215,6 +3219,9 @@ pub async fn run(// Adopt the session's startup run mode (e.g. `--plan`) so the header badge// shows immediately; a resumed thread overrides this via restore_run_mode.app.run_mode = args.run_mode;+ if app.model.is_empty() {+ app.note("not signed in — run /login to sign in to Tokamak, or `jan config set` to configure a provider manually");+ }if args.yolo {app.note("--yolo: sandbox disabled, all tool calls auto-approved without prompting");}@@ -7989,6 +7996,17 @@ mod tests {assert_eq!(prompt.error.as_deref(), Some("Tokamak rejected that API key."));}+ #[test]+ fn submit_user_with_no_model_notes_instead_of_starting_a_turn() {+ let mut app = test_app();+ app.model = String::new();+ app.submit_user("hi".into());+ assert!(!app.want_start, "a fresh install must not start a turn with no model");+ assert!(app.history.is_empty());+ let text: String = app.transcript.iter().flat_map(|l| l.spans.iter()).map(|s| s.content.to_string()).collect();+ assert!(text.contains("/login"), "{text}");+ }+#[test]fn successful_login_closes_the_prompt_and_adopts_a_runnable_model() {crate::core::agent::global_config::with_temp_home(|_| {