From 9ab15b3287a7bc165c7a03362d3a5e9302cfb51d Mon Sep 17 00:00:00 2001 From: Yufan Song <33971064+yufansong@users.noreply.github.com> Date: Tue, 26 Mar 2024 13:15:20 -0700 Subject: [PATCH] add mock server (#214) --- opendevin/mock/README.md | 10 ++++++++++ opendevin/mock/listen.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 opendevin/mock/README.md create mode 100644 opendevin/mock/listen.py diff --git a/opendevin/mock/README.md b/opendevin/mock/README.md new file mode 100644 index 0000000000..26bb0d7e64 --- /dev/null +++ b/opendevin/mock/README.md @@ -0,0 +1,10 @@ +# OpenDevin mock server +This is a simple mock server to facilitate development in the frontend. + +## Start the Server +``` +python -m pip install -r requirements.txt +python listen.py +``` + +Then open the frontend to connect to the mock server. It will simply reply to every received message. \ No newline at end of file diff --git a/opendevin/mock/listen.py b/opendevin/mock/listen.py new file mode 100644 index 0000000000..05c157ca0e --- /dev/null +++ b/opendevin/mock/listen.py @@ -0,0 +1,29 @@ +import uvicorn +from fastapi import FastAPI, WebSocket + +app = FastAPI() +@app.websocket("/ws") +async def websocket_endpoint(websocket: WebSocket): + await websocket.accept() + # send message to mock connection + await websocket.send_json({"action": "initialize", "message": "Control loop started."}) + + try: + while True: + # receive message + data = await websocket.receive_json() + print(f"Received message: {data}") + + # send mock response to client + response = {"message": f"receive {data}"} + await websocket.send_json(response) + print(f"Sent message: {response}") + except Exception as e: + print(f"WebSocket Error: {e}") + +@app.get("/") +def read_root(): + return {"message": "This is a mock server"} + +if __name__ == "__main__": + uvicorn.run(app, host="127.0.0.1", port=3000)