-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.py
308 lines (254 loc) · 9.89 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import argparse
import asyncio
from enum import Enum
from os import path
import sys
import traceback
from typing import Any, Literal, get_args, get_origin
import uvicorn
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.concurrency import asynccontextmanager
from fastapi.routing import APIRoute
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from api.commands import WebSocketCommandModel
from api.enums import ENUM_TYPES, LogType, WingmanInitializationErrorType
import keyboard.keyboard as keyboard
import mouse.mouse as mouse
from services.command_handler import CommandHandler
from services.config_manager import ConfigManager
from services.connection_manager import ConnectionManager
from services.esp32_handler import Esp32Handler
from services.secret_keeper import SecretKeeper
from services.printr import Printr
from services.system_manager import SystemManager
from wingman_core import WingmanCore
port = None
host = None
connection_manager = ConnectionManager()
printr = Printr()
Printr.set_connection_manager(connection_manager)
app_is_bundled = getattr(sys, "frozen", False)
app_root_path = sys._MEIPASS if app_is_bundled else path.dirname(path.abspath(__file__))
# creates all the configs from templates - do this first!
config_manager = ConfigManager(app_root_path)
printr.print(
f"Config directory: {config_manager.config_dir}",
server_only=True,
color=LogType.HIGHLIGHT,
)
secret_keeper = SecretKeeper()
SecretKeeper.set_connection_manager(connection_manager)
system_manager = SystemManager()
printr.print(
f"Wingman AI Core v{system_manager.local_version}",
server_only=True,
color=LogType.HIGHLIGHT,
)
is_latest_version = system_manager.check_version()
if not is_latest_version:
printr.print(
"A new Wingman AI version is available! Download at https://www.wingman-ai.com",
server_only=True,
color=LogType.WARNING,
)
# uses the Singletons above, so don't move this up!
core = WingmanCore(
config_manager=config_manager,
app_root_path=app_root_path,
app_is_bundled=app_is_bundled,
)
core.set_connection_manager(connection_manager)
keyboard.hook(core.on_key)
# TODO: Just hook the mouse event if one config has mouse configured. Because this could have performance implications.
mouse.hook(core.on_mouse)
def custom_generate_unique_id(route: APIRoute):
return f"{route.tags[0]}-{route.name}"
def modify_openapi():
"""Strip the tagname of the functions (for the client) in the OpenAPI spec"""
openapi_schema = app.openapi()
for path_data in openapi_schema["paths"].values():
for operation in path_data.values():
tags = operation.get("tags")
if tags:
tag = tags[0]
operation_id = operation.get("operationId")
if operation_id:
to_remove = f"{tag}-"
new_operation_id = operation_id[len(to_remove) :]
operation["operationId"] = new_operation_id
app.openapi_schema = openapi_schema
@asynccontextmanager
async def lifespan(_app: FastAPI):
# executed before the application starts
modify_openapi()
yield
# executed after the application has finished
await connection_manager.shutdown()
keyboard.unhook_all()
app = FastAPI(lifespan=lifespan, generate_unique_id_function=custom_generate_unique_id)
def custom_openapi():
global host
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Wingman AI Core REST API",
version=str(system_manager.local_version),
description="Communicate with Wingman AI Core",
routes=app.routes,
)
# Add custom server configuration
if not host.startswith("http://") and not host.startswith("https://"):
host = f"http://{host}"
openapi_schema["servers"] = [{"url": f"{host}:{port}"}]
# Ensure the components.schemas key exists
openapi_schema.setdefault("components", {}).setdefault("schemas", {})
# Add enums to schema
for enum_name, enum_model in ENUM_TYPES.items():
enum_field_name, enum_type = next(iter(enum_model.__annotations__.items()))
if issubclass(enum_type, Enum):
enum_values = [e.value for e in enum_type]
enum_schema = {
"type": "string",
"enum": enum_values,
"description": f"Possible values for {enum_name}",
}
openapi_schema["components"]["schemas"][enum_name] = enum_schema
openapi_schema["components"]["schemas"]["CommandActionConfig"] = {
"type": "object",
"properties": {
"keyboard": {"$ref": "#/components/schemas/CommandKeyboardConfig"},
"wait": {"type": "number"},
"mouse": {"$ref": "#/components/schemas/CommandMouseConfig"},
"write": {"type": "string"},
"audio": {"$ref": "#/components/schemas/AudioFileConfig"},
},
}
# Add WebSocket command models to schema
for cls in WebSocketCommandModel.__subclasses__():
cls_schema_dict = cls.model_json_schema(
ref_template="#/components/schemas/{model}"
)
for field_name, field_type in cls.__annotations__.items():
origin = get_origin(field_type)
if origin is Literal:
literal_args = get_args(field_type)
if len(literal_args) == 1:
literal_value = literal_args[0]
cls_schema_dict["properties"][field_name] = {
"type": "string",
"enum": [literal_value],
}
else:
cls_schema_dict["properties"][field_name] = {
"type": "string",
"enum": list(literal_args),
}
cls_schema_dict.setdefault("required", []).append(field_name)
openapi_schema["components"]["schemas"][cls.__name__] = cls_schema_dict
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# if a class adds GET/POST endpoints, add them here:
app.include_router(core.router)
app.include_router(core.config_service.router)
app.include_router(core.settings_service.router)
app.include_router(core.voice_service.router)
app.include_router(system_manager.router)
app.include_router(secret_keeper.router)
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await connection_manager.connect(websocket)
command_handler = CommandHandler(connection_manager, core)
try:
while True:
message = await websocket.receive_text()
await command_handler.dispatch(message, websocket)
except WebSocketDisconnect:
await connection_manager.disconnect(websocket)
await printr.print_async("Client disconnected", server_only=True)
# Websocket for other clients to stream audio to and from (like M5Atom Echo ESP32 devices)
@app.websocket("/")
async def oi_websocket_endpoint(websocket: WebSocket):
await websocket.accept()
esp32_handler = Esp32Handler(core)
receive_task = asyncio.create_task(esp32_handler.receive_messages(websocket))
send_task = asyncio.create_task(esp32_handler.send_messages(websocket))
try:
await asyncio.gather(receive_task, send_task)
except Exception as e:
print(traceback.format_exc())
print(f"Connection lost. Error: {e}")
@app.post("/start-secrets", tags=["main"])
async def start_secrets(secrets: dict[str, Any]):
await secret_keeper.post_secrets(secrets)
core.startup_errors = []
await core.config_service.load_config()
@app.get("/ping", tags=["main"])
async def ping():
return "Ok"
async def async_main(host: str, port: int, sidecar: bool):
await core.config_service.migrate_configs(system_manager)
await core.config_service.load_config()
saved_secrets: list[str] = []
for error in core.tower_errors:
if (
not sidecar # running standalone
and error.error_type == WingmanInitializationErrorType.MISSING_SECRET
and not error.secret_name in saved_secrets
):
secret = input(f"Please enter your '{error.secret_name}' API key/secret: ")
if secret:
secret_keeper.secrets[error.secret_name] = secret
await secret_keeper.save()
saved_secrets.append(error.secret_name)
else:
return
else:
core.startup_errors.append(error)
await core.startup()
event_loop = asyncio.get_running_loop()
core.audio_player.set_event_loop(event_loop)
asyncio.create_task(core.process_events())
core.is_started = True
config = uvicorn.Config(app=app, host=host, port=port, lifespan="on")
server = uvicorn.Server(config)
await server.serve()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run the FastAPI server.")
parser.add_argument(
"-H",
"--host",
type=str,
default="127.0.0.1",
help="Host for the FastAPI server to listen on.",
)
parser.add_argument(
"-p",
"--port",
type=str,
default="8000",
help="Port for the FastAPI server to listen on.",
)
parser.add_argument(
"--sidecar",
action="store_true",
help="Whether or not Wingman AI Core was launched from a client (as sidecar).",
)
args = parser.parse_args()
host = args.host
port = int(args.port)
try:
loop = asyncio.get_running_loop()
except RuntimeError: # No running event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
finally:
loop.run_until_complete(async_main(host=host, port=port, sidecar=args.sidecar))