socket.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from fastapi import status
  2. from openhands.core.logger import openhands_logger as logger
  3. from openhands.core.schema.action import ActionType
  4. from openhands.events.action import (
  5. NullAction,
  6. )
  7. from openhands.events.observation import (
  8. NullObservation,
  9. )
  10. from openhands.events.observation.agent import AgentStateChangedObservation
  11. from openhands.events.serialization import event_to_dict
  12. from openhands.events.stream import AsyncEventStreamWrapper
  13. from openhands.server.auth import get_sid_from_token, sign_token
  14. from openhands.server.github_utils import authenticate_github_user
  15. from openhands.server.shared import config, session_manager, sio
  16. @sio.event
  17. async def connect(connection_id: str, environ):
  18. logger.info(f'sio:connect: {connection_id}')
  19. @sio.event
  20. async def oh_action(connection_id: str, data: dict):
  21. # If it's an init, we do it here.
  22. action = data.get('action', '')
  23. if action == ActionType.INIT:
  24. await init_connection(connection_id, data)
  25. return
  26. logger.info(f'sio:oh_action:{connection_id}')
  27. await session_manager.send_to_event_stream(connection_id, data)
  28. async def init_connection(connection_id: str, data: dict):
  29. gh_token = data.pop('github_token', None)
  30. if not await authenticate_github_user(gh_token):
  31. raise RuntimeError(status.WS_1008_POLICY_VIOLATION)
  32. token = data.pop('token', None)
  33. if token:
  34. sid = get_sid_from_token(token, config.jwt_secret)
  35. if sid == '':
  36. await sio.emit('oh_event', {'error': 'Invalid token', 'error_code': 401})
  37. return
  38. logger.info(f'Existing session: {sid}')
  39. else:
  40. sid = connection_id
  41. logger.info(f'New session: {sid}')
  42. token = sign_token({'sid': sid}, config.jwt_secret)
  43. await sio.emit('oh_event', {'token': token, 'status': 'ok'}, to=connection_id)
  44. latest_event_id = int(data.pop('latest_event_id', -1))
  45. # The session in question should exist, but may not actually be running locally...
  46. event_stream = await session_manager.init_or_join_session(sid, connection_id, data)
  47. # Send events
  48. agent_state_changed = None
  49. async_stream = AsyncEventStreamWrapper(event_stream, latest_event_id + 1)
  50. async for event in async_stream:
  51. if isinstance(
  52. event,
  53. (
  54. NullAction,
  55. NullObservation,
  56. ),
  57. ):
  58. continue
  59. elif isinstance(event, AgentStateChangedObservation):
  60. agent_state_changed = event
  61. continue
  62. await sio.emit('oh_event', event_to_dict(event), to=connection_id)
  63. if agent_state_changed:
  64. await sio.emit('oh_event', event_to_dict(agent_state_changed), to=connection_id)
  65. @sio.event
  66. async def disconnect(connection_id: str):
  67. logger.info(f'sio:disconnect:{connection_id}')
  68. await session_manager.disconnect_from_session(connection_id)