-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
86 lines (68 loc) · 2.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import asyncio
import sys
import json
import logging
import requests
import quart
import quart_cors
from quart import request
# Set up logging
logging.basicConfig(level=logging.INFO)
# Load settings
with open("settings.json") as f:
settings = json.load(f)
cwd = settings.get("working_directory", ".")
app = quart_cors.cors(quart.Quart(__name__), allow_origin="https://chat.openai.com")
@app.get("/logo.png")
async def plugin_logo():
"""
Serve the plugin logo.
This function returns the logo.png file to the client.
"""
filename = "logo.png"
return await quart.send_file(filename, mimetype="image/png")
@app.post("/command")
async def command():
"""
Execute a shell command and return the output.
This function receives a JSON request with a "command" field, executes the command,
and returns the output. If the command fails, the function returns the error message.
"""
data = await request.get_json()
command = data.get("command")
if not command:
return quart.Response(response="No command provided", status=400)
logging.info(f"Received command: {command}")
# Use asyncio to execute the command
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=cwd)
stdout, stderr = await process.communicate()
# Check for errors
if process.returncode != 0:
return quart.Response(response=stderr.decode("utf-8"), status=500)
else:
return quart.Response(response=stdout.decode("utf-8"), status=200)
@app.get("/.well-known/ai-plugin.json")
async def plugin_manifest():
"""
Serve the plugin manifest.
This function reads the ai-plugin.json file and returns it to the client.
"""
with open("./.well-known/ai-plugin.json") as f:
text = f.read()
return quart.Response(text, mimetype="text/json")
@app.get("/openapi.yaml")
async def openapi_spec():
"""
Serve the OpenAPI specification.
This function reads the openapi.yaml file and returns it to the client.
"""
with open("openapi.yaml") as f:
text = f.read()
return quart.Response(text, mimetype="text/yaml")
if __name__ == "__main__":
# Run the Quart application
app.run(debug=True, host="0.0.0.0", port=5004)