Merge main into bb/gui.
Resolve merge conflicts while preserving bb/gui dashboard paths and STT provider support.
This commit is contained in:
+180
-113
@@ -288,6 +288,9 @@ def _finalize_session(session: dict | None, end_reason: str = "tui_close") -> No
|
||||
if not session or session.get("_finalized"):
|
||||
return
|
||||
session["_finalized"] = True
|
||||
stop_event = session.get("_notif_stop")
|
||||
if stop_event is not None:
|
||||
stop_event.set()
|
||||
|
||||
agent = session.get("agent")
|
||||
lock = session.get("history_lock")
|
||||
@@ -580,6 +583,7 @@ def _start_agent_build(sid: str, session: dict) -> None:
|
||||
pass
|
||||
|
||||
_wire_callbacks(sid)
|
||||
_sessions[sid]["_notif_stop"] = _start_notification_poller(sid, _sessions[sid])
|
||||
_notify_session_boundary("on_session_reset", key)
|
||||
|
||||
info = _session_info(agent, current)
|
||||
@@ -2189,6 +2193,7 @@ def _init_session(sid: str, key: str, agent, history: list, cols: int = 80):
|
||||
# session startup resilient).
|
||||
pass
|
||||
_wire_callbacks(sid)
|
||||
_sessions[sid]["_notif_stop"] = _start_notification_poller(sid, _sessions[sid])
|
||||
_notify_session_boundary("on_session_reset", key)
|
||||
_emit("session.info", sid, _session_info(agent, _sessions[sid]))
|
||||
|
||||
@@ -3407,6 +3412,105 @@ def _(rid, params: dict) -> dict:
|
||||
return _ok(rid, {"status": "streaming"})
|
||||
|
||||
|
||||
def _notification_poller_loop(
|
||||
stop_event: threading.Event, sid: str, session: dict
|
||||
) -> None:
|
||||
"""Poll completion_queue and dispatch notifications autonomously.
|
||||
|
||||
Runs in a daemon thread started by _init_session(). Emits a
|
||||
status.update (kind=process) for user visibility, then chains an
|
||||
agent turn via _run_prompt_submit if the session is idle.
|
||||
|
||||
NOTE: The completion_queue is global (one per process). If multiple
|
||||
TUI sessions coexist, whichever poller wakes first grabs the event,
|
||||
even if the process was started by a different session. This matches
|
||||
CLI/gateway behavior (single session per process).
|
||||
"""
|
||||
from tools.process_registry import process_registry, format_process_notification
|
||||
|
||||
while not stop_event.is_set() and not session.get("_finalized"):
|
||||
try:
|
||||
evt = process_registry.completion_queue.get(timeout=0.5)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
_evt_sid = evt.get("session_id", "")
|
||||
if evt.get("type") == "completion" and process_registry.is_completion_consumed(_evt_sid):
|
||||
continue
|
||||
|
||||
text = format_process_notification(evt)
|
||||
if not text:
|
||||
continue
|
||||
|
||||
_emit("status.update", sid, {"kind": "process", "text": text})
|
||||
|
||||
with session["history_lock"]:
|
||||
if session.get("running"):
|
||||
process_registry.completion_queue.put(evt)
|
||||
continue
|
||||
session["running"] = True
|
||||
|
||||
rid = f"__notif__{int(time.time() * 1000)}"
|
||||
try:
|
||||
_emit("message.start", sid)
|
||||
_run_prompt_submit(rid, sid, session, text)
|
||||
except Exception as exc:
|
||||
print(
|
||||
f"[tui_gateway] notification poller dispatch failed: "
|
||||
f"{type(exc).__name__}: {exc}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
with session["history_lock"]:
|
||||
session["running"] = False
|
||||
|
||||
# Drain any remaining events after stop signal (process all pending
|
||||
# before exiting so nothing is lost on shutdown).
|
||||
while not process_registry.completion_queue.empty():
|
||||
try:
|
||||
evt = process_registry.completion_queue.get_nowait()
|
||||
except Exception:
|
||||
break
|
||||
_evt_sid = evt.get("session_id", "")
|
||||
if evt.get("type") == "completion" and process_registry.is_completion_consumed(_evt_sid):
|
||||
continue
|
||||
text = format_process_notification(evt)
|
||||
if not text:
|
||||
continue
|
||||
|
||||
_emit("status.update", sid, {"kind": "process", "text": text})
|
||||
|
||||
with session["history_lock"]:
|
||||
if session.get("running"):
|
||||
process_registry.completion_queue.put(evt)
|
||||
break
|
||||
session["running"] = True
|
||||
|
||||
rid = f"__notif__{int(time.time() * 1000)}"
|
||||
try:
|
||||
_emit("message.start", sid)
|
||||
_run_prompt_submit(rid, sid, session, text)
|
||||
except Exception as exc:
|
||||
print(
|
||||
f"[tui_gateway] notification poller dispatch failed: "
|
||||
f"{type(exc).__name__}: {exc}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
with session["history_lock"]:
|
||||
session["running"] = False
|
||||
|
||||
|
||||
def _start_notification_poller(sid: str, session: dict) -> threading.Event:
|
||||
"""Start the background notification poller for a TUI session."""
|
||||
stop = threading.Event()
|
||||
t = threading.Thread(
|
||||
target=_notification_poller_loop,
|
||||
args=(stop, sid, session),
|
||||
daemon=True,
|
||||
)
|
||||
t.start()
|
||||
return stop
|
||||
|
||||
|
||||
def _run_prompt_submit(rid, sid: str, session: dict, text: Any) -> None:
|
||||
with session["history_lock"]:
|
||||
history = list(session["history"])
|
||||
@@ -3773,6 +3877,36 @@ def _run_prompt_submit(rid, sid: str, session: dict, text: Any) -> None:
|
||||
with session["history_lock"]:
|
||||
session["running"] = False
|
||||
|
||||
# Drain completion notifications that arrived during this turn.
|
||||
# The background poller handles between-turn delivery; this is
|
||||
# the safety net for events that arrived mid-turn.
|
||||
try:
|
||||
from tools.process_registry import process_registry
|
||||
|
||||
for _evt, synth in process_registry.drain_notifications():
|
||||
with session["history_lock"]:
|
||||
if session.get("running"):
|
||||
process_registry.completion_queue.put(_evt)
|
||||
break
|
||||
session["running"] = True
|
||||
try:
|
||||
_emit("message.start", sid)
|
||||
_run_prompt_submit(rid, sid, session, synth)
|
||||
except Exception as _n_exc:
|
||||
print(
|
||||
f"[tui_gateway] completion notification dispatch failed: "
|
||||
f"{type(_n_exc).__name__}: {_n_exc}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
with session["history_lock"]:
|
||||
session["running"] = False
|
||||
except Exception as _drain_exc:
|
||||
print(
|
||||
f"[tui_gateway] completion queue drain failed: "
|
||||
f"{type(_drain_exc).__name__}: {_drain_exc}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
threading.Thread(target=run, daemon=True).start()
|
||||
|
||||
|
||||
@@ -5719,94 +5853,37 @@ def _(rid, params: dict) -> dict:
|
||||
@method("model.options")
|
||||
def _(rid, params: dict) -> dict:
|
||||
try:
|
||||
from hermes_cli.model_switch import list_authenticated_providers
|
||||
from hermes_cli.models import CANONICAL_PROVIDERS, _PROVIDER_LABELS
|
||||
from hermes_cli.inventory import build_models_payload, load_picker_context
|
||||
|
||||
session = _sessions.get(params.get("session_id", ""))
|
||||
agent = session.get("agent") if session else None
|
||||
cfg = _load_cfg()
|
||||
current_provider = getattr(agent, "provider", "") or ""
|
||||
current_model = getattr(agent, "model", "") or _resolve_model()
|
||||
current_base_url = getattr(agent, "base_url", "") or ""
|
||||
# list_authenticated_providers already populates each provider's
|
||||
# "models" with the curated list (same source as `hermes model` and
|
||||
# classic CLI's /model picker). Do NOT overwrite with live
|
||||
# provider_model_ids() — that bypasses curation and pulls in
|
||||
# non-agentic models (e.g. Nous /models returns ~400 IDs including
|
||||
# TTS, embeddings, rerankers, image/video generators).
|
||||
user_provs = (
|
||||
cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}
|
||||
# Layer agent-session state on top of disk config — once an agent
|
||||
# is spawned, IT owns the live provider/model/base_url. Empty
|
||||
# agent attributes must NOT clobber disk config (with_overrides
|
||||
# is truthy-only).
|
||||
ctx = load_picker_context().with_overrides(
|
||||
current_provider=getattr(agent, "provider", "") if agent else "",
|
||||
current_model=(
|
||||
(getattr(agent, "model", "") if agent else "") or _resolve_model()
|
||||
),
|
||||
current_base_url=getattr(agent, "base_url", "") if agent else "",
|
||||
)
|
||||
custom_provs = (
|
||||
cfg.get("custom_providers")
|
||||
if isinstance(cfg.get("custom_providers"), list)
|
||||
else []
|
||||
)
|
||||
authenticated = list_authenticated_providers(
|
||||
current_provider=current_provider,
|
||||
current_base_url=current_base_url,
|
||||
current_model=current_model,
|
||||
user_providers=user_provs,
|
||||
custom_providers=custom_provs,
|
||||
# picker_hints + canonical_order produce the TUI's required shape:
|
||||
# `authenticated`/`auth_type`/`key_env`/`warning` per row, in
|
||||
# CANONICAL_PROVIDERS declaration order. include_unconfigured=True
|
||||
# so the picker can show the full provider universe (with the
|
||||
# setup-hint warning attached) instead of only authed rows.
|
||||
# Curated model lists are preserved — list_authenticated_providers
|
||||
# populates `models` from the curated catalog, not provider_model_ids
|
||||
# (which would pull non-agentic models like TTS/embeddings/etc.).
|
||||
payload = build_models_payload(
|
||||
ctx,
|
||||
include_unconfigured=True,
|
||||
picker_hints=True,
|
||||
canonical_order=True,
|
||||
max_models=50,
|
||||
)
|
||||
|
||||
# Mark authenticated providers and build lookup by slug
|
||||
authed_map: dict = {}
|
||||
authed_extra: list = [] # user-defined/custom not in CANONICAL_PROVIDERS
|
||||
canonical_slugs = {e.slug for e in CANONICAL_PROVIDERS}
|
||||
for p in authenticated:
|
||||
p["authenticated"] = True
|
||||
authed_map[p["slug"]] = p
|
||||
if p["slug"] not in canonical_slugs:
|
||||
authed_extra.append(p)
|
||||
|
||||
# Build final list in CANONICAL_PROVIDERS order, merging auth data
|
||||
from hermes_cli.auth import PROVIDER_REGISTRY as _auth_reg
|
||||
|
||||
ordered: list = []
|
||||
for entry in CANONICAL_PROVIDERS:
|
||||
if entry.slug in authed_map:
|
||||
ordered.append(authed_map[entry.slug])
|
||||
else:
|
||||
pconfig = _auth_reg.get(entry.slug)
|
||||
auth_type = pconfig.auth_type if pconfig else "api_key"
|
||||
key_env = (
|
||||
pconfig.api_key_env_vars[0]
|
||||
if (pconfig and pconfig.api_key_env_vars)
|
||||
else ""
|
||||
)
|
||||
if auth_type == "api_key" and key_env:
|
||||
warning = f"paste {key_env} to activate"
|
||||
else:
|
||||
warning = f"run `hermes model` to configure ({auth_type})"
|
||||
ordered.append(
|
||||
{
|
||||
"slug": entry.slug,
|
||||
"name": _PROVIDER_LABELS.get(entry.slug, entry.label),
|
||||
"is_current": entry.slug == current_provider,
|
||||
"is_user_defined": False,
|
||||
"models": [],
|
||||
"total_models": 0,
|
||||
"source": "built-in",
|
||||
"authenticated": False,
|
||||
"auth_type": auth_type,
|
||||
"key_env": key_env,
|
||||
"warning": warning,
|
||||
}
|
||||
)
|
||||
|
||||
# Append user-defined/custom providers not in canonical list
|
||||
ordered.extend(authed_extra)
|
||||
|
||||
return _ok(
|
||||
rid,
|
||||
{
|
||||
"providers": ordered,
|
||||
"model": current_model,
|
||||
"provider": current_provider,
|
||||
},
|
||||
)
|
||||
return _ok(rid, payload)
|
||||
except Exception as e:
|
||||
return _err(rid, 5033, str(e))
|
||||
|
||||
@@ -5825,7 +5902,7 @@ def _(rid, params: dict) -> dict:
|
||||
try:
|
||||
from hermes_cli.auth import PROVIDER_REGISTRY
|
||||
from hermes_cli.config import is_managed, save_env_value
|
||||
from hermes_cli.model_switch import list_authenticated_providers
|
||||
from hermes_cli.inventory import build_models_payload, load_picker_context
|
||||
|
||||
slug = (params.get("slug") or "").strip()
|
||||
api_key = (params.get("api_key") or "").strip()
|
||||
@@ -5851,43 +5928,32 @@ def _(rid, params: dict) -> dict:
|
||||
# Save the key to ~/.hermes/.env
|
||||
env_var = pconfig.api_key_env_vars[0]
|
||||
save_env_value(env_var, api_key)
|
||||
# Also set in current process so list_authenticated_providers sees it
|
||||
# Also set in current process so the refreshed inventory sees it.
|
||||
import os
|
||||
|
||||
os.environ[env_var] = api_key
|
||||
|
||||
# Refresh provider data
|
||||
cfg = _load_cfg()
|
||||
# Refresh provider data via the shared inventory builder so this
|
||||
# surface stays in lock-step with model.options + dashboard
|
||||
# /api/model/options. picker_hints=True ensures the returned row
|
||||
# carries `authenticated` for the TUI frontend.
|
||||
session = _sessions.get(params.get("session_id", ""))
|
||||
agent = session.get("agent") if session else None
|
||||
current_provider = getattr(agent, "provider", "") or ""
|
||||
current_model = getattr(agent, "model", "") or _resolve_model()
|
||||
current_base_url = getattr(agent, "base_url", "") or ""
|
||||
|
||||
providers = list_authenticated_providers(
|
||||
current_provider=current_provider,
|
||||
current_base_url=current_base_url,
|
||||
current_model=current_model,
|
||||
user_providers=(
|
||||
cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}
|
||||
ctx = load_picker_context().with_overrides(
|
||||
current_provider=getattr(agent, "provider", "") if agent else "",
|
||||
current_model=(
|
||||
(getattr(agent, "model", "") if agent else "") or _resolve_model()
|
||||
),
|
||||
custom_providers=(
|
||||
cfg.get("custom_providers")
|
||||
if isinstance(cfg.get("custom_providers"), list)
|
||||
else []
|
||||
),
|
||||
max_models=50,
|
||||
current_base_url=getattr(agent, "base_url", "") if agent else "",
|
||||
)
|
||||
|
||||
# Find the newly-authenticated provider
|
||||
provider_data = None
|
||||
for p in providers:
|
||||
if p["slug"] == slug:
|
||||
provider_data = p
|
||||
break
|
||||
|
||||
if not provider_data:
|
||||
# Key was saved but provider didn't appear — still return success
|
||||
payload = build_models_payload(
|
||||
ctx, picker_hints=True, max_models=50,
|
||||
)
|
||||
provider_data = next(
|
||||
(p for p in payload["providers"] if p["slug"] == slug), None
|
||||
)
|
||||
if provider_data is None:
|
||||
# Key was saved but provider didn't appear — still return success.
|
||||
provider_data = {
|
||||
"slug": slug,
|
||||
"name": pconfig.name,
|
||||
@@ -5896,7 +5962,8 @@ def _(rid, params: dict) -> dict:
|
||||
"total_models": 0,
|
||||
"authenticated": True,
|
||||
}
|
||||
|
||||
# picker_hints sets `authenticated` from the row state, but the
|
||||
# synthetic fallback above doesn't go through that path.
|
||||
provider_data["authenticated"] = True
|
||||
return _ok(rid, {"provider": provider_data})
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user