Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Add a model server by its address in the first run's AI step (#680)

ghzgod447d1eb72950
diff --git a/crates/ui-egui/src/builders.rs b/crates/ui-egui/src/builders.rs
index 258e631b..ce322253 100644
--- a/crates/ui-egui/src/builders.rs
+++ b/crates/ui-egui/src/builders.rs
@@ -283,9 +283,10 @@ impl CraftApp {
let hub = self.hub.clone();
let f = &self.server_form;
let (name, address, model, key) = (f.name.trim().to_string(), f.address.clone(), f.model.trim().to_string(), Some(f.key.clone()));
+ let choices = self.backend_choices;
self.jobs.spawn(trf("Connect {name}", &[("name", &name)]), true, move |_| {
let result = hub.add_server(&name, &address, Some(model.as_str()).filter(|m| !m.is_empty()), key).map(|_| ());
- Ok(crate::Done::BackendAdded { name, result })
+ Ok(crate::Done::BackendAdded { name, result, choices })
});
}
diff --git a/crates/ui-egui/src/first_run.rs b/crates/ui-egui/src/first_run.rs
index fc680b62..e4b98a5c 100644
--- a/crates/ui-egui/src/first_run.rs
+++ b/crates/ui-egui/src/first_run.rs
@@ -141,7 +141,8 @@ fn brain(app: &mut CraftApp, ui: &mut Ui, width: f32) {
if !app.detecting && shown == 0 && !detected.is_empty() {
ui.label(RichText::new(tr("Nothing of this kind was found.")).color(t.text_faint));
}
- if !app.detecting && detected.is_empty() {
+ // Once an AI is added by its address, "we didn't find one" no longer applies.
+ if !app.detecting && detected.is_empty() && app.backends.is_empty() {
widgets::card(&t).show(ui, |ui| {
ui.set_width(ui.available_width());
ui.horizontal(|ui| {
@@ -154,6 +155,9 @@ fn brain(app: &mut CraftApp, ui: &mut Ui, width: f32) {
});
ui.add_space(8.0);
}
+ // A model server on another computer can't be found by looking here (#54, #98).
+ crate::settings_ui::server_form(app, ui);
+ ui.add_space(8.0);
// "No AI for now"
let none_on = app.state.backend.is_none();
let r = option_row(ui, "circle-stop", "No AI for now", "The built-in engines still show you everything they find.", none_on);
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index e2f416b4..0fe86254 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -229,6 +229,8 @@ pub enum Done {
BackendAdded {
name: String,
result: Result<(), ToolError>,
+ /// `CraftApp::backend_choices` when the add started: a choice made since then stands.
+ choices: u64,
},
/// The projects folder was listed.
Projects(Vec<craft_anything_engine::workspace::Workspace>),
@@ -442,6 +444,8 @@ pub struct CraftApp {
pub extractor: Option<String>,
pub detected: Option<Vec<Detected>>,
pub detecting: bool,
+ /// Counts AI choices, so an add that finishes later doesn't undo one made meanwhile.
+ pub backend_choices: u64,
pub log: Vec<LogLine>,
pub key_inputs: std::collections::BTreeMap<String, String>,
/// Settings → AI's "Add an AI by its address" form. Not kept with the UI state: its key goes
@@ -548,6 +552,7 @@ impl CraftApp {
extractor: None,
detected: None,
detecting: false,
+ backend_choices: 0,
log: Vec::new(),
key_inputs: Default::default(),
server_form: ServerForm::default(),
@@ -1171,13 +1176,20 @@ impl CraftApp {
pub fn add_backend(&mut self, config: BackendConfig, key: Option<String>) {
let hub = self.hub.clone();
let name = config.name.clone();
+ let choices = self.backend_choices;
self.jobs.spawn(i18n::trf("Connect {name}", &[("name", &name)]), true, move |_| {
let result = hub.add_backend(config, key);
- Ok(Done::BackendAdded { name, result })
+ Ok(Done::BackendAdded { name, result, choices })
});
}
pub fn choose_backend(&mut self, name: Option<String>) {
+ self.backend_choices = self.backend_choices.wrapping_add(1);
+ self.select_backend(name);
+ }
+
+ /// Select a backend without counting it as the person's choice (an add that finished).
+ fn select_backend(&mut self, name: Option<String>) {
if self.state.backend != name {
self.state.backend = name;
if let Some(mut c) = self.conversation.take() {
@@ -1193,6 +1205,11 @@ impl CraftApp {
return;
}
self.state.chat.push(ChatItem::User(text.clone()));
+ if self.state.backend.is_some() {
+ // Each message to this AI is a choice too: an AI that finishes connecting meanwhile
+ // doesn't take over (and stop) the answer.
+ self.backend_choices = self.backend_choices.wrapping_add(1);
+ }
if self.state.backend.is_some() && self.conversation.is_none() {
match self.hub.start(self.state.backend.as_deref(), self.beginner()) {
Ok(c) => self.conversation = Some(c),
@@ -1497,13 +1514,16 @@ impl CraftApp {
self.toast(error.for_people(), true);
}
}
- Done::BackendAdded { name, result } => match result {
+ Done::BackendAdded { name, result, choices } => match result {
Ok(()) => {
if self.server_form.name.trim() == name {
self.server_form = ServerForm::default();
}
self.toast(i18n::trf("{name} is ready", &[("name", &name)]), false);
- self.choose_backend(Some(name));
+ // Picking another AI (or none) while this one connected wins.
+ if choices == self.backend_choices {
+ self.select_backend(Some(name));
+ }
self.refresh_backends();
}
Err(e) => self.toast(e.for_people(), true),
@@ -1707,6 +1727,8 @@ impl CraftApp {
self.stuck_banner(ui, &ctx);
if !self.settings.first_run_done {
egui::CentralPanel::default().frame(egui::Frame::new().fill(t.canvas)).show(ui, |ui| guard_ui(ui, "first run", |ui| first_run::show(self, ui)));
+ // An AI added from the first run reports a failure here (a wrong address or key).
+ self.toasts(&ctx);
titlebar::window_edge(&ctx);
titlebar::resize_edges(&ctx);
return;
diff --git a/crates/ui-egui/src/settings_ui.rs b/crates/ui-egui/src/settings_ui.rs
index d2e5908d..856710e3 100644
--- a/crates/ui-egui/src/settings_ui.rs
+++ b/crates/ui-egui/src/settings_ui.rs
@@ -734,7 +734,8 @@ fn ai(app: &mut CraftApp, ui: &mut Ui) {
/// An AI that "Look for AI" can't find: a server on another computer or a hosted service, added by
/// its address (#54, #98). The server is asked for its models before it's added.
-fn server_form(app: &mut CraftApp, ui: &mut Ui) {
+/// The first run's AI step shows it too.
+pub(crate) fn server_form(app: &mut CraftApp, ui: &mut Ui) {
let t = Tokens::get(ui.ctx());
ui.add_space(14.0);
widgets::caption(ui, "Add an AI by its address");
@@ -1107,6 +1108,55 @@ mod tests {
draw(&mut app);
assert!(app.agent_model_picker.is_none(), "a confirmed settings change still invalidates the picker");
}
+
+ /// An AI that finishes connecting is chosen, unless another choice (here "No AI for now") was
+ /// made while it connected.
+ #[test]
+ fn a_late_add_does_not_undo_a_choice_made_meanwhile() {
+ let mut app = demo::app(demo::engine(craft_anything_engine::Mode::Beginner, false));
+ let added = |choices| Ok(Done::BackendAdded { name: "Home server".into(), result: Ok(()), choices });
+ let started = app.backend_choices;
+ app.choose_backend(None);
+ app.handle_done(Finished { job: 0, result: added(started) });
+ assert_eq!(app.state.backend, None, "the late add must not override No AI for now");
+ app.handle_done(Finished { job: 0, result: added(app.backend_choices) });
+ assert_eq!(app.state.backend.as_deref(), Some("Home server"), "with no choice meanwhile, the added AI is chosen");
+ // Two adds at once: the first one's selection isn't a choice, so the second is selected too.
+ let started = app.backend_choices;
+ let other = |choices| Ok(Done::BackendAdded { name: "Laptop".into(), result: Ok(()), choices });
+ app.handle_done(Finished { job: 0, result: added(started) });
+ app.handle_done(Finished { job: 0, result: other(started) });
+ assert_eq!(app.state.backend.as_deref(), Some("Laptop"), "the add that finished last is selected");
+ }
+
+ /// A chat started while an AI connects is a choice too: the add doesn't switch AI under it.
+ #[test]
+ fn a_late_add_does_not_stop_a_chat_started_meanwhile() {
+ let mut app = demo::app(demo::engine(craft_anything_engine::Mode::Experienced, true));
+ // A closed local port: the chat starts, and its request fails without leaving this computer.
+ let config = BackendConfig {
+ name: "Box".into(),
+ kind: BackendKind::OpenaiCompatible,
+ model: "m".into(),
+ base_url: Some("http://127.0.0.1:9/v1".into()),
+ ssh_host: None,
+ enabled: true,
+ };
+ app.hub.add_backend(config, None).unwrap();
+ app.state.backend = Some("Box".into());
+ let started = app.backend_choices;
+ app.send("Hi");
+ assert!(app.conversation.is_some(), "the chat started");
+ app.handle_done(Finished { job: 0, result: Ok(Done::BackendAdded { name: "Laptop".into(), result: Ok(()), choices: started }) });
+ assert_eq!(app.state.backend.as_deref(), Some("Box"), "the chat's AI stays chosen");
+ assert!(app.conversation.is_some(), "the chat keeps going");
+ // A follow-up in a chat that started before the add counts the same way.
+ let started = app.backend_choices;
+ app.send("And then?");
+ app.handle_done(Finished { job: 0, result: Ok(Done::BackendAdded { name: "Laptop".into(), result: Ok(()), choices: started }) });
+ assert_eq!(app.state.backend.as_deref(), Some("Box"), "a follow-up keeps its AI");
+ assert!(app.conversation.is_some(), "the follow-up's answer keeps going");
+ }
}
#[cfg(test)]
diff --git a/crates/ui-egui/tests/headless_parity.rs b/crates/ui-egui/tests/headless_parity.rs
index 7e363f47..c5707467 100644
--- a/crates/ui-egui/tests/headless_parity.rs
+++ b/crates/ui-egui/tests/headless_parity.rs
@@ -157,6 +157,7 @@ const PLUMBING: &[(&str, &str)] = &[
("refresh_view", "redraw data"), ("resolve_chat_evidence", "redraw data"), ("mark_dirty", "redraw data"),
("poll_conversation", "polling"), ("allow_for_project", "part of answer_consent"),
("handle_done", "a job's result"), ("tool_done", "a job's result"),
+ ("select_backend", "part of choose_backend; also selects an AI that finished connecting"),
("tick", "the frame loop"), ("show", "drawing"), ("toasts", "drawing"), ("shortcuts", "keys"),
("builder_backend", "part of start_make"), ("need_builder", "part of start_make"), ("start_run", "part of start_make"), ("start_run_with", "part of start_run and continue_build"),
("resume", "part of follow_up"), ("poll_run", "polling"), ("launch", "part of start_run: RunSet::start, the queue rebuild_start uses too"),
diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs
index 8ce9c97d..056f264e 100644
--- a/crates/ui-egui/tests/smoke.rs
+++ b/crates/ui-egui/tests/smoke.rs
@@ -59,6 +59,31 @@ fn first_run_walks_through_four_screens() {
h.get_by_label("Just look inside");
}
+/// A model server on another computer can be added from the first run's AI step, without
+/// leaving for Settings.
+#[test]
+fn first_run_ai_step_can_add_a_server_by_its_address() {
+ let mut h = harness(|| demo::app(demo::engine(Mode::Beginner, false)));
+ h.run_steps(3);
+ h.get_by_label("I'm new to this").click();
+ h.run_steps(3);
+ assert_eq!(h.state().state.first_run, FirstRunStep::Brain);
+ let _ = h.get_by_label("Server address");
+ let _ = h.get_by_label("AI name");
+ // The demo already has an AI, so "we didn't find one" isn't said.
+ assert!(h.query_by_label("We didn't find an AI on this computer yet").is_none());
+}
+
+/// A message (such as a server that couldn't be reached) shows on the first run too.
+#[test]
+fn first_run_shows_messages() {
+ let mut h = harness(|| demo::app(demo::engine(Mode::Beginner, false)));
+ h.run_steps(3);
+ h.state_mut().toast("Couldn't reach the server", true);
+ h.run_steps(3);
+ let _ = h.get_by_label("Couldn't reach the server");
+}
+
/// #231: at the smallest window the app allows, Purpose's Continue was below the window with
/// nothing to scroll; it scrolls into view above the footer and works.
#[test]
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index badb3014..d41d5d41 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -272,6 +272,11 @@ been run live.
- [x] Do not select an embedding, ranking or transcription model as a chat backend when a local server has no chat models — claimed: Afloat16, branch bugfix/r5.4-nonchat-model-detection-20261103
- [x] Recognize specialised model file paths and numeric task-name versions during local chat setup
— claimed: Afloat16, branch bugfix/r5.4-model-task-identifiers-20261103
+- [x] The first run's AI step can add a model server on another computer by its address (llama.cpp,
+ Ollama or LM Studio on a home server), the same form as Settings → AI
+- [ ] A chat sent while a llama.cpp router is still loading the model ended after 31 s with "The
+ answer stream ended early." and no next step (seen once live, 27B model); say the server may
+ still be loading it and to try again in a minute
### R5.5 API-key builds fail in words people can use
Owner: open · Status: todo