Skip to content

The agent that grows with you

refactor(tui): keep server.py from growing by moving the scoped provider lookup to agent_factory

yoniebans06c879e6b110
diff --git a/tui_gateway/agent_factory.py b/tui_gateway/agent_factory.py
index 829089aa7e6..73647e40f66 100644
--- a/tui_gateway/agent_factory.py
+++ b/tui_gateway/agent_factory.py
@@ -179,6 +179,13 @@ def _stored_session_runtime_overrides(row: dict | None) -> dict:
return overrides
+def _scoped_session_provider(agent, session: dict) -> str:
+ """The entry an agent on the ``custom`` adapter runs, resolved in the session's own profile: clients
+ reuse this identity for new chats without the endpoint or key, and callers need not be bound to it."""
+ with _profile_build_scope(session.get("profile_home") or _hermes_home):
+ return _runtime_model_config(agent).get("provider") or "custom"
+
+
def _runtime_model_config(agent, existing: dict | None = None) -> dict:
"""Merge the agent's CURRENT runtime identity onto the row's persisted ``model_config``. Falsy agent
attributes DELETE the key rather than skip the write: resume reads provider/endpoint from this JSON
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index e7f03ed0d8e..e5a7a6193e7 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -1508,13 +1508,6 @@ def _turn_started_at(session: dict | None) -> float | None:
return float(inflight["started_at"]) if isinstance(inflight, dict) and inflight.get("started_at") else None
-def _scoped_session_provider(agent, session: dict) -> str:
- """The entry an agent on the ``custom`` adapter runs, resolved in the session's own profile: clients
- reuse this identity for new chats without the endpoint or key, and callers need not be bound to it."""
- with _profile_build_scope(session.get("profile_home") or _hermes_home):
- return _runtime_model_config(agent).get("provider") or "custom"
-
-
def _live_session_identity(session: dict) -> tuple[str, str]:
"""``(model, provider)`` the live session actually runs — the same precedence ``_session_info`` reports:
a switch queued mid-turn, the metadata mirror, the built agent, the composer override a deferred record
@@ -1526,8 +1519,7 @@ def _live_session_identity(session: dict) -> tuple[str, str]:
model = (str(pending.get("display_model") or "").strip() or mirror.get("model")
or getattr(agent, "model", "") or override.get("model"))
agent_provider = getattr(agent, "provider", "")
- if agent_provider == "custom":
- agent_provider = _scoped_session_provider(agent, session)
+ agent_provider = _scoped_session_provider(agent, session) if agent_provider == "custom" else agent_provider
provider = (str(pending.get("display_provider") or "").strip() or mirror.get("provider")
or agent_provider or override.get("provider"))
default = ("", "") if model else _session_default_route(session)