session_bot_state.py 1.4 KB

123456789101112131415161718192021222324252627
  1. """A Bot Chat's live state, handed to the teammate whose ``message_agent`` delivery it answers.
  2. A sender that only learns "queued" cannot tell a busy teammate from one parked on its user's
  3. approval or clarify card; it waits out the bound and reports a silent timeout. The deliver answer
  4. (and every same-id replay the waiting runner makes) carries this state instead. Read on the owner
  5. loop, never persisted: a receipt holds outcomes, not a snapshot that is stale a second later.
  6. """
  7. from gateway.session_contract import SessionRef
  8. # Prompt kinds a running turn parks on, in the order a sender should hear about them.
  9. _WAITING_KINDS = ('approval', 'clarify')
  10. def bot_chat_state(authority, session_id):
  11. """``paused`` (the owner admits nothing new: a drain for restart/update or retirement),
  12. ``idle``, ``working``, or ``waiting_approval`` / ``waiting_clarify`` while the running turn
  13. waits on its user."""
  14. if authority.retiring or authority.runner._draining:
  15. return 'paused'
  16. live = authority.sessions.get(session_id)
  17. if live is None:
  18. return 'idle'
  19. handle = authority._handle(SessionRef(authority.profile_id, session_id))
  20. if handle.execution_state != 'running':
  21. return 'idle'
  22. kinds = {prompt['kind'] for prompt in live.controls.snapshot(session_id, handle.execution_generation)}
  23. return next((f'waiting_{kind}' for kind in _WAITING_KINDS if kind in kinds), 'working')