Skip to content

Self-improving AI Employees for everyone

fix(employees): a hire whose only model is a saved endpoint runs on it

trohitgab80545bfe79
diff --git a/server/services/employees/connections.py b/server/services/employees/connections.py
index 3c83934b..789a2ac1 100644
--- a/server/services/employees/connections.py
+++ b/server/services/employees/connections.py
@@ -62,14 +62,25 @@ class Connections:
}
async def ai_providers(self) -> List[str]:
- """LLM providers an agent could run on right now (a key stored, a
- local server saved, or a named OpenAI-compatible endpoint)."""
- from services.llm.config import PROVIDER_CONFIGS
+ """LLM providers an agent could run on right now: one with a key
+ stored or a local server saved, and each saved OpenAI-compatible
+ endpoint by its reference (``openai_compatible:<slug>``). The bare
+ ``openai_compatible`` id is never one: an agent set to it has no
+ server to call."""
+ from services.llm.config import ENDPOINT_PROVIDER, PROVIDER_CONFIGS
usable = []
for provider_id in PROVIDER_CONFIGS:
+ if provider_id == ENDPOINT_PROVIDER:
+ continue
if self.provider(provider_id) is not None and await self.is_connected(provider_id):
usable.append(provider_id)
+ try:
+ from services.llm.endpoints import list_endpoints
+
+ usable += [endpoint.ref for endpoint in await list_endpoints(self._auth)]
+ except Exception:
+ logger.warning("Could not list OpenAI-compatible endpoints", exc_info=True)
return usable
async def has_ai(self) -> bool:
diff --git a/server/services/employees/start.py b/server/services/employees/start.py
index 5e9c7297..225f3e33 100644
--- a/server/services/employees/start.py
+++ b/server/services/employees/start.py
@@ -50,12 +50,6 @@ async def heal_agent_models(database: Any, auth_service: Any, connections: Conne
"""Point each agent whose provider is not usable at the owner's current
model. Returns the agents changed."""
usable = set(await connections.ai_providers())
- try:
- from services.llm.endpoints import list_endpoints
-
- usable.update(endpoint.ref for endpoint in await list_endpoints(auth_service))
- except Exception:
- pass
changed: List[str] = []
choice = None
for agent_id in await _agent_ids(database, workflow_id):
diff --git a/server/tests/services/employees/test_connections.py b/server/tests/services/employees/test_connections.py
new file mode 100644
index 00000000..3a505df7
--- /dev/null
+++ b/server/tests/services/employees/test_connections.py
@@ -0,0 +1,79 @@
+"""Which AI models an employee can run on (services/employees/connections.py):
+a saved OpenAI-compatible endpoint counts by its reference, never as the bare
+provider id, so a hire whose only model is an endpoint runs on it, and an
+employee left on the bare id is moved onto the endpoint when it starts."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+import pytest
+
+from services.employees import connections, llm, start
+
+
+def setup(monkeypatch):
+ state = SimpleNamespace(connected=set(), endpoints=[], saved={})
+
+ async def is_connected(self, provider_id):
+ return provider_id in state.connected
+
+ async def list_endpoints(_auth):
+ return state.endpoints
+
+ import services.llm.endpoints as endpoints_module
+
+ monkeypatch.setattr(connections.Connections, "is_connected", is_connected)
+ monkeypatch.setattr(endpoints_module, "list_endpoints", list_endpoints)
+ return state
+
+
+class Database:
+ def __init__(self, state):
+ self.state = state
+
+ async def get_user_settings(self, user_id):
+ return {}
+
+ async def get_provider_defaults(self, provider):
+ return None
+
+ async def get_node_parameters(self, node_id):
+ return {"provider": "openai_compatible", "model": "", "prompt": "p"}
+
+ async def save_node_parameters(self, node_id, params):
+ self.state.saved[node_id] = params
+
+
+class Auth:
+ async def get_stored_models(self, provider, session_id="default"):
+ return []
+
+
+async def test_a_saved_endpoint_counts_by_its_reference(setup):
+ # The credential store reports the base id as connected once any
+ # endpoint is saved.
+ setup.connected = {"anthropic", "openai_compatible"}
+ setup.endpoints = [SimpleNamespace(ref="openai_compatible:home", models=["m1"])]
+ assert await connections.Connections(Auth()).ai_providers() == ["anthropic", "openai_compatible:home"]
+
+
+async def test_a_hire_whose_only_model_is_an_endpoint_runs_on_it(setup):
+ setup.connected = {"openai_compatible"}
+ setup.endpoints = [SimpleNamespace(ref="openai_compatible:home", models=["m1"])]
+ choice = await llm.resolve_llm_choice(Database(setup), Auth(), connections.Connections(Auth()))
+ assert choice == llm.LLMChoice(provider="openai_compatible:home", model="m1", local=False)
+
+
+async def test_an_employee_left_on_the_bare_id_is_moved_onto_the_endpoint(setup, monkeypatch):
+ setup.connected = {"openai_compatible"}
+ setup.endpoints = [SimpleNamespace(ref="openai_compatible:home", models=["m1"])]
+
+ async def agents(_database, _workflow_id):
+ return ["7:aiAgent:1"]
+
+ monkeypatch.setattr(start, "_agent_ids", agents)
+ auth = Auth()
+ assert await start.heal_agent_models(Database(setup), auth, connections.Connections(auth), "7") == ["7:aiAgent:1"]
+ assert setup.saved["7:aiAgent:1"] == {"provider": "openai_compatible:home", "model": "m1", "prompt": "p"}