Skip to content
Python

Ollama: pulled models are selectable without manual setup

Florian van Triel424dda59cc5f
diff --git a/coworker/server/manager.py b/coworker/server/manager.py
index 2026c0c4..83434bc6 100644
--- a/coworker/server/manager.py
+++ b/coworker/server/manager.py
@@ -4175,11 +4175,16 @@ class SessionManager:
# the curated list so it shows up in the composer right after configuring the provider.
rec = d.recommended_model
added: Optional[str] = None
- if rec and rec in self._suggested_models(name):
+ suggested = self._suggested_models(name)
+ if rec and rec in suggested:
# OpenAI models stay bare (the router's default); others carry their prefix.
added = rec if name == "openai" else f"{name}:{rec}"
+ elif name == "ollama" and suggested:
+ # Ollama offers whatever the user pulled — the recommended model may not be one.
+ added = f"ollama:{suggested[0]}"
+ if added:
self.add_model(added)
- if added and not self._provider_configured(self._model_provider(self.model)):
+ if added and not self.model_selectable(self.model):
self.set_default_model(added)
return added
return None
@@ -4360,11 +4365,10 @@ class SessionManager:
def _ollama_models(self) -> list[str]:
"""Live list of models pulled into the configured Ollama server (via its native
- `/api/tags`), as `ollama:<name>` so they're directly selectable. Empty if Ollama isn't
- configured or unreachable — best-effort, never raises."""
- profile = self.secrets.get("provider:ollama")
- if not profile:
- return []
+ `/api/tags`), as `ollama:<name>` so they're directly selectable. Empty if Ollama is
+ unreachable — best-effort, never raises. No stored profile → the default localhost
+ endpoint, same as `_ollama_alive` (keyless Ollama needs no saved form)."""
+ profile = self.secrets.get("provider:ollama") or {}
base = (profile.get("base_url") or "http://localhost:11434").strip().rstrip("/")
if base.endswith("/v1"):
base = base[: -len("/v1")]
@@ -4612,6 +4616,9 @@ class SessionManager:
# Ollama is keyless, so "configured" is meaningless there — its models show only
# while a local Ollama answers (cached liveness probe).
selectable = [m for m in self._curated_models() if self.model_selectable(m)]
+ # Pulled Ollama models are offered live while Ollama answers — no need to add each one.
+ if self._ollama_alive():
+ selectable = list(dict.fromkeys([*selectable, *self._ollama_models()]))
if self.model not in selectable:
selectable.insert(0, self.model)
from ..providers.matrix import model_context_windows, model_labels
@@ -4632,9 +4639,10 @@ class SessionManager:
"model_context_windows": model_context_windows(),
"has_key": env_key or stored,
# Provider-agnostic "can this default model actually run?" — true when the default
- # model's provider is configured (any provider, not just OpenAI). Drives the GUI's
+ # model's provider is configured (any provider, not just OpenAI), or for an
+ # `ollama:*` default, when a local Ollama answers. Drives the GUI's
# "No model connected" composer chip and the onboarding Skip warning.
- "model_ready": self._provider_configured(self._model_provider(self.model)),
+ "model_ready": self.model_selectable(self.model),
"source": "env" if env_key else ("store" if stored else None),
"onboarded": bool(self._prefs.get("onboarded")),
"experimental_connectors": experimental_enabled(self.secrets),
diff --git a/tests/test_provider_router.py b/tests/test_provider_router.py
index b47bc3aa..f7b2a134 100644
--- a/tests/test_provider_router.py
+++ b/tests/test_provider_router.py
@@ -378,6 +378,8 @@ def test_manager_curated_models(tmp_path, monkeypatch):
# test_settings.py::test_ollama_models_gated_on_liveness). Unpinned, the ollama
# assertions below pass only where Ollama happens to run — green on a dev box, red in CI.
monkeypatch.setattr(SessionManager, "_ollama_alive", lambda self: True)
+ # Same for the live `/api/tags` list get_settings merges in while Ollama answers.
+ monkeypatch.setattr(SessionManager, "_ollama_models", lambda self: [])
mgr = SessionManager(data_dir=tmp_path)
# no provider keys → nothing but the always-selectable default
diff --git a/tests/test_settings.py b/tests/test_settings.py
index 873940e2..8161e7ba 100644
--- a/tests/test_settings.py
+++ b/tests/test_settings.py
@@ -172,5 +172,66 @@ def test_ollama_models_gated_on_liveness(tmp_path, monkeypatch):
monkeypatch.setattr(SessionManager, "_ollama_alive", lambda self: False)
assert "ollama:llama3.3" not in manager.get_settings()["models"]
+ monkeypatch.setattr(SessionManager, "_ollama_models", lambda self: [])
monkeypatch.setattr(SessionManager, "_ollama_alive", lambda self: True)
assert "ollama:llama3.3" in manager.get_settings()["models"]
+
+
+def _ollama_manager(tmp_path, monkeypatch, pulled=("qwen2.5:7b-instruct",)):
+ """A fresh manager (no keys, no saved Ollama profile) whose local Ollama answers
+ `/api/tags` with `pulled` — none of them the recommended model."""
+ import httpx
+
+ from coworker.server.manager import SessionManager
+
+ monkeypatch.delenv("OPENAI_API_KEY", raising=False)
+ monkeypatch.setenv("COWORKER_STATE_DIR", str(tmp_path / "state"))
+
+ class _Tags:
+ status_code = 200
+
+ def json(self):
+ return {"models": [{"name": n} for n in pulled]}
+
+ monkeypatch.setattr(httpx, "get", lambda url, timeout=None: _Tags())
+ return SessionManager(data_dir=tmp_path / "data")
+
+
+def test_ollama_models_without_saved_profile(tmp_path, monkeypatch):
+ """Keyless Ollama needs no saved form: pulled models list from the localhost default."""
+ manager = _ollama_manager(tmp_path, monkeypatch)
+ assert manager.secrets.get("provider:ollama") is None
+ assert manager._ollama_models() == ["ollama:qwen2.5:7b-instruct"]
+
+
+def test_pulled_ollama_models_reach_the_picker(tmp_path, monkeypatch):
+ manager = _ollama_manager(tmp_path, monkeypatch)
+ assert "ollama:qwen2.5:7b-instruct" in manager.get_settings()["models"]
+
+
+def test_ollama_default_ready_tracks_liveness(tmp_path, monkeypatch):
+ from coworker.server.manager import SessionManager
+
+ manager = _ollama_manager(tmp_path, monkeypatch)
+ manager.set_default_model("ollama:qwen2.5:7b-instruct")
+ assert manager.get_settings()["model_ready"] is True
+ monkeypatch.setattr(SessionManager, "_ollama_alive", lambda self: False)
+ assert manager.get_settings()["model_ready"] is False
+
+
+def test_ollama_adopts_first_pulled_model(tmp_path, monkeypatch):
+ """Saving Ollama with only a non-recommended model pulled still yields a runnable default."""
+ manager = _ollama_manager(tmp_path, monkeypatch)
+ assert manager.get_settings()["model_ready"] is False
+ assert manager.adopt_provider_default("ollama") == "ollama:qwen2.5:7b-instruct"
+ settings = manager.get_settings()
+ assert settings["model"] == "ollama:qwen2.5:7b-instruct"
+ assert settings["model_ready"] is True
+
+
+def test_ollama_adoption_never_steals_a_working_default(tmp_path, monkeypatch):
+ manager = _ollama_manager(tmp_path, monkeypatch)
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-env-123")
+ before = manager.get_settings()["model"]
+ assert manager.adopt_provider_default("ollama") is None
+ assert manager.get_settings()["model"] == before