gateway_session_meta.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """Editor session metadata on the gateway path, projected from the gateway's existing reads.
  2. Main's in-process server read these off its live agent (``server.py`` ``_session_response_fields``,
  3. ``_build_usage_update``, ``_send_session_info_update``); here every figure comes from a snapshot's
  4. ``info`` (``session.info``), ``session.stats``, a ``message.complete`` ``usage`` or ``state.db``.
  5. """
  6. from __future__ import annotations
  7. from datetime import UTC, datetime
  8. import logging
  9. from pathlib import Path
  10. import sqlite3
  11. from acp.schema import (
  12. AvailableCommand, AvailableCommandsUpdate, ModelInfo, SessionConfigOptionSelect, SessionConfigSelectOption,
  13. SessionInfoUpdate, SessionMode, SessionModeState, SessionModelState, UnstructuredCommandInput, Usage, UsageUpdate,
  14. )
  15. from acp_adapter.modes import (
  16. DEFAULT_EDIT_APPROVAL_POLICY, DEFAULT_MODE, EDIT_APPROVAL_POLICY_CONFIG_ID, EDIT_APPROVAL_POLICY_TO_MODE, MODES,
  17. )
  18. logger = logging.getLogger(__name__)
  19. # Advertised name -> (description, input hint). ``prompt`` serves each through a gateway verb
  20. # (``acp_adapter/gateway_commands.py``); ``/reset`` is the registry's alias of ``/new``.
  21. COMMANDS: dict[str, tuple[str, str | None]] = {
  22. "help": ("List available commands", None),
  23. "model": ("Show current model and provider, or switch models", "model name to switch to"),
  24. "tools": ("List available tools", None),
  25. "context": ("Show conversation context usage", None),
  26. "reset": ("Clear conversation history", None),
  27. "compress": ("Compress conversation context", None),
  28. "steer": ("Inject guidance into the currently running agent turn", "guidance for the active turn"),
  29. "queue": ("Queue a prompt to run after the current turn finishes", "prompt to run next"),
  30. "usage": ("Show token usage and rate limits", None),
  31. "status": ("Show session, model, token, and context info", None),
  32. "version": ("Show Hermes version", None),
  33. "title": ("Show or set the session title", "new title"),
  34. "undo": ("Remove the last N user turns and their replies", "number of turns"),
  35. "retry": ("Resend the last user message", None),
  36. }
  37. def available_commands_update() -> AvailableCommandsUpdate:
  38. return AvailableCommandsUpdate(session_update="available_commands_update", available_commands=[
  39. AvailableCommand(name=name, description=description,
  40. input=UnstructuredCommandInput(hint=hint) if hint else None)
  41. for name, (description, hint) in COMMANDS.items()])
  42. def help_text() -> str:
  43. lines = ["Available commands:", ""]
  44. lines.extend(f" /{name:10s} {description}" for name, (description, _hint) in COMMANDS.items())
  45. lines.extend(["", "Other Hermes commands run on the gateway, which answers them or says why not.",
  46. "Unrecognized /commands are sent to the model as normal messages."])
  47. return "\n".join(lines)
  48. def current_mode(info: dict) -> str:
  49. """The mode of the session's edit approval policy (``session.info``); an owner without the field asks."""
  50. return EDIT_APPROVAL_POLICY_TO_MODE.get(info.get("edit_approval_policy") or DEFAULT_EDIT_APPROVAL_POLICY,
  51. DEFAULT_MODE)
  52. def session_modes(info: dict | None = None) -> SessionModeState:
  53. return SessionModeState(current_mode_id=current_mode(info or {}), available_modes=[
  54. SessionMode(id=mode, name=name, description=description) for mode, (_p, name, description) in MODES.items()])
  55. REASONING_CONFIG_ID = "reasoning_effort"
  56. SERVICE_TIER_CONFIG_ID = "service_tier"
  57. SERVICE_TIER_LABELS = {"normal": "Normal", "fast": "Fast", "auto": "Auto", "cold": "Cold", "ultrafast": "Ultrafast"}
  58. def reasoning_choices() -> tuple[str, ...]:
  59. """The owner's ``reasoning`` levels; ``default`` hands the level back to the profile."""
  60. from hermes_constants import VALID_REASONING_EFFORTS
  61. return ("default", "none", *VALID_REASONING_EFFORTS)
  62. def config_options(models: SessionModelState | None, info: dict, mode_id: str) -> list:
  63. """Zed renders its composer pickers only from ``configOptions``: the edit mode, the model
  64. (``set_session_model``'s picker ids) and the reasoning effort (``session.info``'s level)."""
  65. def select(config_id, name, category, current, choices):
  66. return SessionConfigOptionSelect(id=config_id, name=name, category=category, type="select", current_value=current,
  67. options=[SessionConfigSelectOption(value=v, name=n) for v, n in choices])
  68. options = [select(EDIT_APPROVAL_POLICY_CONFIG_ID, "Edits", "mode", MODES[mode_id][0],
  69. [(policy, name) for policy, name, _ in MODES.values()])]
  70. if models is not None:
  71. options.append(select("model", "Model", "model", models.current_model_id,
  72. [(m.model_id, m.name) for m in models.available_models]))
  73. # ``session.info`` reports the level in force, never ``default`` (a reset session reads the profile's), so
  74. # ``default`` is offered only while no level is set: a pick never shows as another value than the one chosen.
  75. levels = reasoning_choices()[1:]
  76. effort = info.get("reasoning_effort") if info.get("reasoning_effort") in levels else "default"
  77. options.append(select(REASONING_CONFIG_ID, "Reasoning effort", "thought_level", effort,
  78. [(v, v) for v in (levels if effort in levels else ("default", *levels))]))
  79. return options
  80. async def session_config_options(client, session_id, models, info, mode_id):
  81. """Project the owner's eligible speed choices alongside the existing ACP pickers."""
  82. from hermes_cli.gateway_client import GatewayRPCError
  83. from hermes_cli.models import model_supports_fast_mode
  84. options = config_options(models, info, mode_id)
  85. if not model_supports_fast_mode(info.get('model')):
  86. return options
  87. try:
  88. speed = await client.rpc('config.get', session_id=session_id, key='service_tier')
  89. except GatewayRPCError as exc:
  90. if str(exc) != 'invalid_params':
  91. raise
  92. return options # Older owners do not serve the speed-choice read.
  93. choices = speed.get('options') or []
  94. if choices:
  95. current = speed['value']
  96. rows = [SessionConfigSelectOption(value=value, name=SERVICE_TIER_LABELS[value]) for value in choices]
  97. if current not in choices:
  98. rows.append(SessionConfigSelectOption(value=current,
  99. name=f"{SERVICE_TIER_LABELS.get(current, current)} (unavailable on this route)"))
  100. options.append(SessionConfigOptionSelect(id=SERVICE_TIER_CONFIG_ID, name='Service tier', category='other',
  101. type='select', current_value=current, options=rows))
  102. return options
  103. def model_state(info: dict) -> SessionModelState | None:
  104. """The picker for the session's model (``session.info``), from the shared inventory; a single
  105. current-model row when nothing is listable, as main's ``_build_model_state``. Blocking."""
  106. from acp_adapter.auth import detect_provider
  107. from acp_adapter.model_catalog import build_model_state, encode_model_choice
  108. model = str(info.get("model") or "").strip()
  109. provider = info.get("provider") or detect_provider() or "openrouter"
  110. try:
  111. picker = build_model_state(model, provider, "")
  112. if picker is not None:
  113. return picker
  114. except Exception:
  115. # Boundary: a broken inventory must not fail session/new; the current model still shows.
  116. logger.debug("Could not build ACP model state", exc_info=True)
  117. if not model:
  118. return None
  119. choice = encode_model_choice(provider, model)
  120. return SessionModelState(available_models=[ModelInfo(model_id=choice, name=model)], current_model_id=choice)
  121. def provenance_meta(db_path: Path, acp_session_id: str, session_id: str) -> dict | None:
  122. """``_meta.hermes.sessionProvenance`` for the editor's id, the logical session's current
  123. compression tip and its lineage (``acp_adapter/provenance.py``). Blocking."""
  124. from acp_adapter.provenance import session_provenance_meta
  125. from hermes_state import SessionDB
  126. if not Path(db_path).exists():
  127. return None
  128. try:
  129. with SessionDB(db_path=db_path, read_only=True) as db:
  130. return session_provenance_meta(db, acp_session_id, db.get_compression_tip(session_id) or session_id)
  131. except sqlite3.Error:
  132. # Best-effort, as main's ``_provenance_meta``: an unreadable store must not fail the attach.
  133. logger.debug("Could not build ACP session provenance for %s", session_id, exc_info=True)
  134. return None
  135. # ``stats`` below is the owner's usage dict (``gateway/session_usage.py::usage_stats``): the
  136. # ``session.usage`` event published after every API call and at turn end, or ``session.stats``.
  137. def usage_update(stats: dict) -> UsageUpdate | None:
  138. """Zed's context indicator: ``size`` the context window, ``used`` its current occupancy."""
  139. size = stats.get("context_length")
  140. if type(size) is not int or size <= 0:
  141. return None
  142. return UsageUpdate(session_update="usage_update", size=size, used=max(int(stats.get("context_tokens") or 0), 0))
  143. def turn_usage(stats) -> Usage | None:
  144. """``PromptResponse.usage``: the agent's session counters, the figures main's turn result carried."""
  145. if not isinstance(stats, dict):
  146. return None
  147. return Usage(input_tokens=int(stats.get("input_tokens") or 0), output_tokens=int(stats.get("output_tokens") or 0),
  148. total_tokens=int(stats.get("total_tokens") or 0),
  149. thought_tokens=stats.get("reasoning_tokens") or None,
  150. cached_read_tokens=stats.get("cache_read_tokens") or None)
  151. def title_update(title: str) -> SessionInfoUpdate:
  152. # ``sessions`` has no ``updated_at``; "now" is right since this fires when the title changed.
  153. return SessionInfoUpdate(session_update="session_info_update", title=title,
  154. updated_at=datetime.now(UTC).isoformat())