|
| 1 | +import json |
| 2 | + |
| 3 | +import quart |
| 4 | +import quart_cors |
| 5 | +from quart import request |
| 6 | + |
| 7 | +app = quart_cors.cors(quart.Quart(__name__), allow_origin="https://chat.openai.com") |
| 8 | + |
| 9 | +# Keep track of todo's. Does not persist if Python session is restarted. |
| 10 | +_TODOS = {} |
| 11 | + |
| 12 | +@app.post("/todos/<string:username>") |
| 13 | +async def add_todo(username): |
| 14 | + request = await quart.request.get_json(force=True) |
| 15 | + if username not in _TODOS: |
| 16 | + _TODOS[username] = [] |
| 17 | + _TODOS[username].append(request["todo"]) |
| 18 | + return quart.Response(response='OK', status=200) |
| 19 | + |
| 20 | +@app.get("/todos/<string:username>") |
| 21 | +async def get_todos(username): |
| 22 | + return quart.Response(response=json.dumps(_TODOS.get(username, [])), status=200) |
| 23 | + |
| 24 | +@app.delete("/todos/<string:username>") |
| 25 | +async def delete_todo(username): |
| 26 | + request = await quart.request.get_json(force=True) |
| 27 | + todo_idx = request["todo_idx"] |
| 28 | + # fail silently, it's a simple plugin |
| 29 | + if 0 <= todo_idx < len(_TODOS[username]): |
| 30 | + _TODOS[username].pop(todo_idx) |
| 31 | + return quart.Response(response='OK', status=200) |
| 32 | + |
| 33 | +@app.get("/logo.png") |
| 34 | +async def plugin_logo(): |
| 35 | + filename = 'logo.png' |
| 36 | + return await quart.send_file(filename, mimetype='image/png') |
| 37 | + |
| 38 | +@app.get("/.well-known/ai-plugin.json") |
| 39 | +async def plugin_manifest(): |
| 40 | + host = request.headers['Host'] |
| 41 | + with open("./.well-known/ai-plugin.json") as f: |
| 42 | + text = f.read() |
| 43 | + return quart.Response(text, mimetype="text/json") |
| 44 | + |
| 45 | +@app.get("/openapi.yaml") |
| 46 | +async def openapi_spec(): |
| 47 | + host = request.headers['Host'] |
| 48 | + with open("openapi.yaml") as f: |
| 49 | + text = f.read() |
| 50 | + return quart.Response(text, mimetype="text/yaml") |
| 51 | + |
| 52 | +def main(): |
| 53 | + app.run(debug=True, host="0.0.0.0", port=5003) |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + main() |
0 commit comments