2024-12-06 17:41:32 +00:00
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import queue
|
|
|
|
|
from pulsar.schema import JsonSchema
|
|
|
|
|
import uuid
|
|
|
|
|
from aiohttp import web, WSMsgType
|
|
|
|
|
|
|
|
|
|
from . socket import SocketEndpoint
|
2024-12-06 20:58:31 +00:00
|
|
|
from . text_completion import TextCompletionRequestor
|
2024-12-06 17:41:32 +00:00
|
|
|
|
|
|
|
|
class CommandEndpoint(SocketEndpoint):
|
|
|
|
|
|
|
|
|
|
def __init__(
|
2024-12-06 20:58:31 +00:00
|
|
|
self, pulsar_host, auth,
|
|
|
|
|
services,
|
|
|
|
|
path="/api/v1/command",
|
2024-12-06 17:41:32 +00:00
|
|
|
):
|
|
|
|
|
|
|
|
|
|
super(CommandEndpoint, self).__init__(
|
|
|
|
|
endpoint_path=path, auth=auth,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.pulsar_host=pulsar_host
|
|
|
|
|
|
2024-12-06 20:58:31 +00:00
|
|
|
# self.text_completion = TextCompletionRequestor(
|
|
|
|
|
# )
|
|
|
|
|
|
2024-12-06 17:41:32 +00:00
|
|
|
async def start(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def async_thread(self, ws, running):
|
|
|
|
|
|
|
|
|
|
id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
while running.get():
|
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
|
running.stop()
|
|
|
|
|
|
|
|
|
|
async def listener(self, ws, running):
|
|
|
|
|
|
|
|
|
|
async for msg in ws:
|
|
|
|
|
|
|
|
|
|
# On error, finish
|
|
|
|
|
if msg.type == WSMsgType.ERROR:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
data = msg.json()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
await ws.send_json({"error": str(e)})
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if "service" not in data:
|
|
|
|
|
await ws.send_json({"error": "Malformed message"})
|
|
|
|
|
|
|
|
|
|
running.stop()
|
|
|
|
|
|