listen.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import uvicorn
  2. from fastapi import FastAPI, WebSocket
  3. from openhands.core.logger import openhands_logger as logger
  4. from openhands.core.schema import ActionType
  5. from openhands.utils.shutdown_listener import should_continue
  6. app = FastAPI()
  7. @app.websocket('/ws')
  8. async def websocket_endpoint(websocket: WebSocket):
  9. await websocket.accept()
  10. # send message to mock connection
  11. await websocket.send_json(
  12. {'action': ActionType.INIT, 'message': 'Control loop started.'}
  13. )
  14. try:
  15. while should_continue():
  16. # receive message
  17. data = await websocket.receive_json()
  18. logger.debug(f'Received message: {data}')
  19. # send mock response to client
  20. response = {'message': f'receive {data}'}
  21. await websocket.send_json(response)
  22. logger.debug(f'Sent message: {response}')
  23. except Exception as e:
  24. logger.debug(f'WebSocket Error: {e}')
  25. @app.get('/')
  26. def read_root():
  27. return {'message': 'This is a mock server'}
  28. @app.get('/api/options/models')
  29. def read_llm_models():
  30. return [
  31. 'gpt-4',
  32. 'gpt-4-turbo-preview',
  33. 'gpt-4-0314',
  34. 'gpt-4-0613',
  35. ]
  36. @app.get('/api/options/agents')
  37. def read_llm_agents():
  38. return [
  39. 'CodeActAgent',
  40. 'PlannerAgent',
  41. ]
  42. @app.get('/api/list-files')
  43. def refresh_files():
  44. return ['hello_world.py']
  45. if __name__ == '__main__':
  46. uvicorn.run(app, host='127.0.0.1', port=3000)