session_messaging_picks.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """A viewer's model and reasoning picks on a messaging chat (Telegram, Discord, ...), #141152.
  2. Desktop and the TUI send ``session.mutate`` ``model`` / ``reasoning`` for whatever session they show.
  3. A LOCAL session commits the pick into its frozen policy; a messaging chat has no frozen policy, so
  4. that path refused it with ``runtime_coordination_required`` and the reasoning pill waited forever for
  5. a level. The chat's own ``/model`` and ``/reasoning`` set a per-chat override that its next turn
  6. resolves (``_record_model_switch``, ``_apply_reasoning_selection``). A viewer's pick sets that same
  7. override, so the chat and every viewer see one setting.
  8. The mutation names the session a viewer resumed, which for a compressed chat is its lineage root
  9. (``messaging_owner_ref``): the fences are checked on that row and the override is keyed by the lane's
  10. route, so it applies to the compression tip the next turn writes.
  11. """
  12. import asyncio
  13. from hermes_state_runtime import RuntimeStoreError
  14. CHAT_PICKS = frozenset({'model', 'reasoning'})
  15. def is_chat_pick(live, operation):
  16. from gateway.config import Platform
  17. return operation in CHAT_PICKS and live is not None and live.source.platform != Platform.LOCAL
  18. def _platform_key(live):
  19. from gateway.run import _platform_config_key
  20. return _platform_config_key(live.source.platform)
  21. def _apply_reasoning(runner, live, effort):
  22. """``/reasoning <level>`` (``None``: ``/reasoning reset``) as the chat would run it."""
  23. return runner._apply_reasoning_selection(live.route, _platform_key(live), 'reset' if effort is None else effort)
  24. async def _pick_model(authority, owner, live, payload, row):
  25. """The chat's ``/model <name> [--provider p]`` resolution, guarded like a LOCAL pick, then committed
  26. as its session override. Returns the receipt fields, or the guard's confirmation request."""
  27. from gateway.session_mutation_model import _Route, _selection_guard_refusal, is_model_reset
  28. from gateway.slash_commands_model import _ModelSwitchContext
  29. from hermes_constants import get_hermes_home
  30. runner = authority.runner
  31. ctx = _ModelSwitchContext(session_key=live.route, source=live.source,
  32. config_path=get_hermes_home() / 'config.yaml', persist_global=False)
  33. ctx.read_config()
  34. if is_model_reset(payload): # the chat's own `/model reset`: its override goes, its reply is the note
  35. note = await runner._handle_model_reset(None, ctx)
  36. return {'model': (await asyncio.to_thread(messaging_route_info, runner, live))['model'], 'note': note}
  37. # After a restart the chat's saved provider/base_url is only on disk: resolve against it, not
  38. # config.yaml's provider, or the record step overwrites it (as run_turn_prepare rehydrates).
  39. runner._rehydrate_session_model_override(live.route)
  40. ctx.apply_override(runner._session_model_override(live.route) or {})
  41. result, error = await runner._perform_model_switch(ctx, payload['model'], payload.get('provider', ''), live.source)
  42. if error is not None:
  43. raise RuntimeStoreError('model_resolution_failed', detail=error)
  44. snapshot = {'target': owner.session_id, 'target_revision': row['runtime_revision']}
  45. current = _Route(ctx.current_provider, ctx.current_model, ctx.current_base_url, ctx.current_api_key)
  46. refusal = await _selection_guard_refusal(authority, live, payload, snapshot, current, result)
  47. if refusal is not None:
  48. return refusal
  49. # No in-place swap of the cached agent: a turn may be running on it. The override is what the
  50. # next turn builds from, and the record step evicts the cached agent.
  51. await runner._record_model_switch(result, ctx, source=live.source, one_turn=False, picker=True)
  52. fields = {'model': result.new_model, 'provider': result.target_provider}
  53. if 'reasoning' in payload:
  54. _apply_reasoning(runner, live, payload['reasoning'])
  55. fields['effort'] = payload['reasoning']
  56. return fields
  57. async def apply_chat_pick(authority, ref, owner, live, params, *, principal_id):
  58. """Apply a ``model`` / ``reasoning`` mutation to the messaging chat *live* mounted on *owner* (the
  59. caller authorized control and holds its mutation lock). The fences are the mount's, the ones
  60. ``session.resume`` handed the viewer; comparing them with a compression segment's row refused a
  61. pick on a compressed chat with ``stale_generation``. The receipt is keyed on that mount too: an
  62. exact retry answers its stored receipt without re-applying the override, and the commit bumps the
  63. mount's revision, so a retry of an older pick refuses ``revision_conflict``."""
  64. from hermes_state_mutation_chat_pick import chat_pick_key, commit_chat_pick, prepare_chat_pick
  65. from hermes_state_mutations import validate_action
  66. operation, payload = params['operation'], params['payload']
  67. validate_action(operation, payload)
  68. key, digest = chat_pick_key(principal_id, owner.session_id, params)
  69. fenced = dict(epoch=authority.epoch, key=key, digest=digest, session_id=owner.session_id, params=params)
  70. prepared = await asyncio.to_thread(prepare_chat_pick, authority.db, **fenced)
  71. if 'receipt' in prepared:
  72. return prepared['receipt']
  73. row = prepared['row']
  74. runner = authority.runner
  75. if operation == 'reasoning':
  76. await asyncio.to_thread(_apply_reasoning, runner, live, payload['effort'])
  77. fields = {'effort': payload['effort']}
  78. else:
  79. async with runner._model_switch_lock():
  80. fields = await _pick_model(authority, owner, live, payload, row)
  81. if fields.get('status') == 'confirmation_required':
  82. return {'session_id': ref.session_id, 'operation': operation, **fields}
  83. result = {'session_id': ref.session_id, 'operation': operation, 'revision': row['runtime_revision'],
  84. 'execution_generation': row['runtime_generation'], **fields}
  85. result = await asyncio.to_thread(commit_chat_pick, authority.db, **fenced, result=result)
  86. from gateway.session_local import local_session_info
  87. # Every viewer repaints its model and reasoning pill, as after a LOCAL pick.
  88. live.event_stream.publish(owner.session_id, local_session_info(authority, owner), event_type='session.info')
  89. return result
  90. def messaging_route_info(runner, live):
  91. """``model`` and the effort fields a messaging chat's next turn resolves, without building an agent
  92. or resolving credentials: session ``/model`` > ``channel_overrides`` > config.yaml, and the session
  93. ``/reasoning`` > per-model > global level (``_resolve_session_reasoning_config``)."""
  94. from gateway.run import _load_gateway_config, _resolve_gateway_model
  95. from gateway.session_local import effort_fields
  96. config = _load_gateway_config()
  97. block = config.get('model')
  98. block = block if isinstance(block, dict) else {}
  99. model, provider, base_url = _resolve_gateway_model(config), block.get('provider'), block.get('base_url')
  100. channel = runner._channel_override_for(live.source)
  101. if channel is not None:
  102. model, provider = channel.model or model, channel.provider or provider
  103. override = runner._session_model_override(live.route) or runner.session_store.get_model_override(live.route) or {}
  104. model = override.get('model') or model
  105. provider, base_url = override.get('provider') or provider, override.get('base_url') or base_url
  106. # The wire level clamps per api_mode (a Codex app-server route keeps ``ultra``); config.yaml's
  107. # api_mode belongs to its own provider only.
  108. api_mode = override.get('api_mode') or (block.get('api_mode') if provider == block.get('provider') else None)
  109. reasoning = runner._resolve_session_reasoning_config(source=live.source, session_key=live.route, model=model)
  110. tier = runner._resolve_session_service_tier(source=live.source, session_key=live.route)
  111. return {'model': model, **effort_fields(reasoning, tier, model, provider, base_url, api_mode)}