session_reactions.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Desktop's message reactions (``message.react``) on the owner's transcript.
  2. The sidecar handler looked the session up in its own table, which never holds an authority
  3. session, and the ``message.*`` namespace never falls back to it, so every reaction was rolled back
  4. as "Could not react". The write is the same ``SessionDB.set_message_reaction`` (Tapback semantics,
  5. rows of the session's visible resume lineage only), on the conversation's current physical session.
  6. """
  7. from functools import partial
  8. from gateway.session_busy_controls import authorize
  9. from hermes_state_runtime import RuntimeStoreError
  10. _FIELDS = frozenset({'session_id', 'profile', 'row_id', 'newest_role', 'emoji', 'author'})
  11. def _parse(params):
  12. row_id, newest_role, emoji = params.get('row_id'), params.get('newest_role'), params.get('emoji')
  13. author = params.get('author') or 'user'
  14. if (set(params) - _FIELDS or (row_id is not None and type(row_id) is not int)
  15. or (row_id is None and newest_role not in {'user', 'assistant'})
  16. or (emoji is not None and (not isinstance(emoji, str) or not emoji.strip()))
  17. or author not in {'user', 'agent'}):
  18. raise RuntimeStoreError('invalid_params')
  19. return row_id, newest_role, emoji.strip() if emoji else None, author
  20. def _write(db, target, row_id, newest_role, emoji, author):
  21. if row_id is None:
  22. row_id = db.latest_message_row_id(target, role=newest_role)
  23. if row_id is None:
  24. return None, None
  25. return row_id, db.set_message_reaction(target, row_id, emoji, author=author)
  26. async def react(connection, ref, params):
  27. row_id, newest_role, emoji, author = _parse(params)
  28. authority = connection.authority
  29. if not ref.session_id or ref.session_id not in authority.sessions:
  30. raise RuntimeStoreError('not_found')
  31. authorize(connection, ref, params, 'session:submit')
  32. from gateway.session_ancillary import physical_target
  33. from gateway.session_runtime_workers import tracked_write
  34. # A reset or compression moves the conversation's rows onto a new physical session; the
  35. # reaction lineage check runs from that tip, as the sidecar's submit-row target did.
  36. target = physical_target(authority, ref)
  37. row_id, reactions = await tracked_write(authority, partial(
  38. _write, authority.db, target, row_id, newest_role, emoji, author))
  39. if reactions is None:
  40. raise RuntimeStoreError('not_found')
  41. return {'row_id': row_id, 'reactions': reactions}