Files
SIA/sia/__main__.py

105 lines
3.5 KiB
Python

from concurrent.futures import ThreadPoolExecutor
from aiohttp import web
from pathlib import Path
import asyncio
import mimetypes
import time
from .llm_engine import LlmEngine
from .system_metrics import SystemMetrics
from .web_agent import WebAgent
from .web_io_buffer import WebIOBuffer
from .web_socket_manager import WebSocketManager
from .working_memory import WorkingMemory
from .xml_validator import XMLValidator
from .response_parser import ResponseParser
from .web_agent import WebAgent
from .web_io_buffer import WebIOBuffer
mimetypes.add_type("application/javascript", ".js")
mimetypes.add_type("application/javascript", ".jsx")
mimetypes.add_type("text/javascript", ".js")
mimetypes.add_type("text/javascript", ".jsx")
class TestLLM:
def infer(self, prompt: str, context: str):
yield "<reasoning>"
time.sleep(2)
yield "test reasoning"
time.sleep(2)
yield "</reasoning>"
class Main:
def __init__(self):
self._base_dir = Path(__file__).parent.parent
self._system_prompt = (self._base_dir / "system_prompt.md").read_text()
self._action_schema = (self._base_dir / "action_schema.xsd").read_text()
self._static_dir = self._base_dir / "static"
self._llm = LlmEngine("/root/model")
#self._llm = TestLLM()
self._io_buffer = WebIOBuffer()
self._agent = WebAgent(
system_prompt=self._system_prompt,
action_schema=self._action_schema,
working_memory=WorkingMemory(),
system_metrics=SystemMetrics(),
llm=self._llm,
validator=XMLValidator(self._action_schema),
parser=ResponseParser(self._io_buffer)
)
self._ws_manager = WebSocketManager(
self._agent,
self._io_buffer
)
self._app = web.Application()
self._init_routes()
async def app(self):
return self._app
def _init_routes(self):
"""Initialize application routes."""
self._app.middlewares.append(self._cors_middleware)
self._app.router.add_get("/ws", self._ws_manager.handle_websocket)
self._app.router.add_get("/", self._serve_index)
self._app.router.add_static("/static/", self._static_dir, show_index=False)
self._app.router.add_static("/assets/", self._static_dir / "assets", show_index=False)
self._app.router.add_get("/{path:.*}", self._serve_index)
async def _serve_index(self, request: web.Request) -> web.Response:
"""Serve the React application HTML for any unmatched routes."""
index_path = self._static_dir / "index.html"
if not index_path.exists():
raise web.HTTPNotFound()
with open(index_path, "r") as f:
html_content = f.read()
return web.Response(
text=html_content,
content_type="text/html"
)
@web.middleware
async def _cors_middleware(self, request: web.Request, handler):
"""Handle CORS headers."""
if request.method == "OPTIONS":
response = web.Response()
else:
response = await handler(request)
response.headers.update({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '86400',
})
return response
if __name__ == "__main__":
main = Main()
app = asyncio.run(main.app())
print("Web server started at http://localhost:8080")
web.run_app(app, port=8080)