New web interface, move llm engine to separate process
This commit is contained in:
182
sia/web/api.py
182
sia/web/api.py
@@ -1,14 +1,14 @@
|
||||
from pathlib import Path
|
||||
from aiohttp import web
|
||||
import json
|
||||
from pathlib import Path
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
|
||||
from ..auto_approver import AutoApprover
|
||||
from ..auto_approver import AutoApprover, AutoApproverConfig
|
||||
from ..chat_io_buffer import ChatIOBuffer
|
||||
from ..entry.entry_factory import EntryFactory
|
||||
from ..iteration_parser import IterationParser
|
||||
from ..web_agent import WebAgent
|
||||
from ..web_io_buffer import WebIOBuffer
|
||||
from ..working_memory import WorkingMemory
|
||||
|
||||
class Api:
|
||||
@@ -17,7 +17,7 @@ class Api:
|
||||
work_dir: Path,
|
||||
app: web.Application,
|
||||
agent: WebAgent,
|
||||
io_buffer: WebIOBuffer,
|
||||
io_buffer: ChatIOBuffer,
|
||||
working_memory: WorkingMemory,
|
||||
auto_approver: AutoApprover
|
||||
):
|
||||
@@ -32,23 +32,24 @@ class Api:
|
||||
|
||||
def _init_routes(self):
|
||||
"""Initialize REST API and WebSocket routes."""
|
||||
self._app.router.add_post("/api/inference/{llm}", self._run_inference)
|
||||
self._app.router.add_post("/api/inference/{llm}/stop", self._stop_inference)
|
||||
self._app.router.add_get("/api/llms", self._get_llms)
|
||||
self._app.router.add_get("/api/llms/active", self._get_active_llm)
|
||||
self._app.router.add_post("/api/llms/active", self._set_active_llm)
|
||||
|
||||
self._app.router.add_post("/api/inference", self._run_inference)
|
||||
self._app.router.add_post("/api/inference/stop", self._stop_inference)
|
||||
|
||||
self._app.router.add_get("/api/response", self._get_response)
|
||||
self._app.router.add_post("/api/response", self._set_response)
|
||||
self._app.router.add_post("/api/response/approve", self._approve_response)
|
||||
self._app.router.add_get("/api/response", self._get_response)
|
||||
self._app.router.add_post("/api/context", self._modify_context)
|
||||
self._app.router.add_post("/api/input", self._send_input)
|
||||
self._app.router.add_post("/api/clear", self._clear_output)
|
||||
self._app.router.add_get("/api/output/{llm}", self._get_output)
|
||||
self._app.router.add_get("/api/llms", self._get_llms)
|
||||
self._app.router.add_get("/api/auto_approver/config", self._get_auto_approver_config)
|
||||
self._app.router.add_post("/api/auto_approver/config", self._set_auto_approver_config)
|
||||
self._app.router.add_post("/api/auto_approver/context_enabled", self._set_context_enabled)
|
||||
self._app.router.add_post("/api/auto_approver/response_enabled", self._set_response_enabled)
|
||||
self._app.router.add_post("/api/auto_approver/context_timeout", self._set_context_timeout)
|
||||
self._app.router.add_post("/api/auto_approver/response_timeout", self._set_response_timeout)
|
||||
self._app.router.add_post("/api/auto_approver/llm", self._set_llm_name)
|
||||
|
||||
self._app.router.add_post("/api/chat/message", self._add_chat_message)
|
||||
self._app.router.add_delete("/api/chat/message/{id}", self._delete_chat_message)
|
||||
self._app.router.add_delete("/api/chat", self._clear_chat)
|
||||
|
||||
self._app.router.add_get("/api/auto_approver", self._get_auto_approver_config)
|
||||
self._app.router.add_post("/api/auto_approver", self._set_auto_approver_config)
|
||||
|
||||
self._app.router.add_get("/api/memory", self._get_memory)
|
||||
self._app.router.add_post("/api/memory/entry", self._create_entry)
|
||||
self._app.router.add_put("/api/memory/entry/{id}", self._save_entry)
|
||||
@@ -57,16 +58,33 @@ class Api:
|
||||
self._app.router.add_post("/api/memory/entry/{id}/update", self._update_entry)
|
||||
self._app.router.add_post("/api/memory/load_iteration", self._load_iteration)
|
||||
|
||||
async def _run_inference(self, request: web.Request) -> web.Response:
|
||||
"""Start inference on specified LLM."""
|
||||
async def _get_llms(self, request: web.Request) -> web.Response:
|
||||
return web.Response(
|
||||
text=json.dumps(
|
||||
[{"name": name} for name in self._agent.llms]
|
||||
),
|
||||
content_type="application/json"
|
||||
)
|
||||
|
||||
async def _get_active_llm(self, request: web.Request) -> web.Response:
|
||||
return web.Response(
|
||||
text=json.dumps(
|
||||
{"active_llm": self._agent.active_llm}
|
||||
),
|
||||
content_type="application/json"
|
||||
)
|
||||
|
||||
async def _set_active_llm(self, request: web.Request) -> web.Response:
|
||||
try:
|
||||
llm_name = request.match_info["llm"]
|
||||
data = await request.json()
|
||||
text = data.get("response")
|
||||
context = data.get("context")
|
||||
self._agent._response_buffer.set_text(text)
|
||||
self._agent.modify_context(context)
|
||||
await asyncio.get_event_loop().run_in_executor(None, self._agent.run_inference, llm_name)
|
||||
self._agent.active_llm = data["active_llm"]
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _run_inference(self, request: web.Request) -> web.Response:
|
||||
try:
|
||||
await asyncio.get_event_loop().run_in_executor(None, self._agent.run_inference)
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
@@ -74,29 +92,7 @@ class Api:
|
||||
async def _stop_inference(self, request: web.Request) -> web.Response:
|
||||
"""Stop inference on specified LLM."""
|
||||
try:
|
||||
llm_name = request.match_info["llm"]
|
||||
self._agent.stop_inference(llm_name)
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _set_response(self, request: web.Request) -> web.Response:
|
||||
"""Edit the shared response buffer"""
|
||||
try:
|
||||
data = await request.json()
|
||||
text = data.get("response")
|
||||
self._agent._response_buffer.set_text(text)
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _approve_response(self, request: web.Request) -> web.Response:
|
||||
"""Approve current buffer content"""
|
||||
data = await request.json()
|
||||
try:
|
||||
response = data.get("response")
|
||||
self._agent.response_buffer.set_text(response)
|
||||
self._agent.approve_response()
|
||||
self._agent.stop_inference()
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
@@ -110,53 +106,50 @@ class Api:
|
||||
content_type="application/json"
|
||||
)
|
||||
|
||||
async def _modify_context(self, request: web.Request) -> web.Response:
|
||||
"""Modify the current context."""
|
||||
async def _set_response(self, request: web.Request) -> web.Response:
|
||||
"""Edit the shared response buffer"""
|
||||
try:
|
||||
data = await request.json()
|
||||
context = data.get("context")
|
||||
self._agent.modify_context(context)
|
||||
text = data.get("response")
|
||||
self._agent._response_buffer.set_text(text)
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _send_input(self, request: web.Request) -> web.Response:
|
||||
"""Send input to the IO buffer."""
|
||||
async def _approve_response(self, request: web.Request) -> web.Response:
|
||||
"""Approve current buffer content"""
|
||||
try:
|
||||
data = await request.json()
|
||||
input_text = data.get("input")
|
||||
self._io_buffer.append_stdin(input_text)
|
||||
self._agent.approve_response()
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _clear_output(self, request: web.Request) -> web.Response:
|
||||
"""Clear the stdout buffer."""
|
||||
self._io_buffer.clear_stdout()
|
||||
return web.Response(status=200)
|
||||
|
||||
async def _get_output(self, request: web.Request) -> web.Response:
|
||||
"""Get complete output for specified LLM."""
|
||||
|
||||
async def _add_chat_message(self, request: web.Request) -> web.Response:
|
||||
"""Add a chat message"""
|
||||
try:
|
||||
llm_name = request.match_info["llm"]
|
||||
output = self._agent.get_output(llm_name)
|
||||
return web.Response(
|
||||
text=json.dumps({"output": output}),
|
||||
content_type="application/json"
|
||||
)
|
||||
data = await request.json()
|
||||
message = data.get("message")
|
||||
self._io_buffer.add_user_message(message)
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _delete_chat_message(self, request: web.Request) -> web.Response:
|
||||
"""Delete a chat message"""
|
||||
try:
|
||||
message_id = request.match_info["id"]
|
||||
self._io_buffer.delete_message(message_id)
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _clear_chat(self, request: web.Request) -> web.Response:
|
||||
"""Clear chat messages"""
|
||||
try:
|
||||
self._io_buffer.clear()
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _get_llms(self, request: web.Request) -> web.Response:
|
||||
"""Get all LLMs and their current states."""
|
||||
states = self._agent.llms
|
||||
return web.Response(
|
||||
text=json.dumps(
|
||||
[{"name": name, "state": state.name}
|
||||
for name, state in states.items()]
|
||||
),
|
||||
content_type="application/json"
|
||||
)
|
||||
|
||||
async def _get_auto_approver_config(self, request: web.Request) -> web.Response:
|
||||
"""Get current auto approver configuration."""
|
||||
@@ -169,7 +162,7 @@ class Api:
|
||||
"""Update auto approver configuration."""
|
||||
try:
|
||||
data = await request.json()
|
||||
self._auto_approver.set_config(data)
|
||||
self._auto_approver.config = AutoApproverConfig(**data)
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
@@ -219,18 +212,6 @@ class Api:
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _set_llm_name(self, request: web.Request) -> web.Response:
|
||||
"""Set LLM name for auto-approval."""
|
||||
try:
|
||||
data = await request.json()
|
||||
name = data.get("name")
|
||||
if name is None:
|
||||
return web.Response(status=400, text="Missing name parameter")
|
||||
self._auto_approver.llm_name = name
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
|
||||
async def _get_memory(self, request: web.Request) -> web.Response:
|
||||
"""Get complete working memory state."""
|
||||
entries = self._working_memory.get_entries()
|
||||
@@ -304,13 +285,12 @@ class Api:
|
||||
if not content:
|
||||
return web.Response(status=400, text="Missing content in request body")
|
||||
|
||||
(context, response, entries) = IterationParser.parse_iteration(content, self._work_dir, self._io_buffer)
|
||||
(_context, response, entries) = IterationParser.parse_iteration(content, self._work_dir, self._io_buffer)
|
||||
|
||||
for entry in entries:
|
||||
self._working_memory.add_entry(entry)
|
||||
self._agent.modify_context(context, True)
|
||||
self._agent.response_buffer.set_text(response)
|
||||
|
||||
return web.Response(status=200)
|
||||
except Exception as e:
|
||||
return web.Response(status=400, text=str(e))
|
||||
return web.Response(status=400, text=str(e))
|
||||
@@ -1,4 +1,5 @@
|
||||
from aiohttp import web, WSMsgType
|
||||
from dataclasses import asdict
|
||||
from typing import Dict, Set
|
||||
|
||||
from ..auto_approver import AutoApprover, AutoApproverConfig
|
||||
@@ -28,7 +29,7 @@ class AutoApproverWebSocket:
|
||||
async def _handle_config_change(self, config: AutoApproverConfig):
|
||||
"""Handle config changes from the AutoApprover."""
|
||||
await self._broadcast_message({
|
||||
"config": config
|
||||
"config": asdict(config)
|
||||
})
|
||||
|
||||
async def handle_connection(self, request: web.Request) -> web.WebSocketResponse:
|
||||
@@ -41,7 +42,7 @@ class AutoApproverWebSocket:
|
||||
try:
|
||||
# Send initial config
|
||||
await ws.send_json({
|
||||
"config": self._auto_approver.config
|
||||
"config": asdict(self._auto_approver.config)
|
||||
})
|
||||
|
||||
async for msg in ws:
|
||||
|
||||
64
sia/web/chat_websocket.py
Normal file
64
sia/web/chat_websocket.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from aiohttp import web, WSMsgType
|
||||
from typing import Dict, Set
|
||||
|
||||
from .util import wrap_async
|
||||
from ..chat_io_buffer import ChatIOBuffer
|
||||
|
||||
class ChatWebSocket:
|
||||
|
||||
def __init__(self, io_buffer: ChatIOBuffer):
|
||||
self._io_buffer = io_buffer
|
||||
self._clients: Set[web.WebSocketResponse] = set()
|
||||
self._io_buffer.add_change_handler(wrap_async(self._handle_io_buffer_change))
|
||||
|
||||
async def _broadcast_message(self, message: Dict):
|
||||
"""Broadcast message to all connected clients."""
|
||||
disconnected = set()
|
||||
for ws in self._clients:
|
||||
try:
|
||||
await ws.send_json(message)
|
||||
except ConnectionResetError:
|
||||
disconnected.add(ws)
|
||||
self._clients -= disconnected
|
||||
|
||||
async def _handle_io_buffer_change(self, new_messages, deleted_message_ids, last_read_timestamp):
|
||||
"""Handle changes to the ChatIOBuffer."""
|
||||
await self._broadcast_message({
|
||||
"messages": [
|
||||
{
|
||||
"id": message.id,
|
||||
"content": message.content,
|
||||
"message_type": message.message_type.value
|
||||
}
|
||||
for message in new_messages
|
||||
],
|
||||
"deleted_message_ids": deleted_message_ids,
|
||||
"last_read_timestamp": last_read_timestamp
|
||||
})
|
||||
|
||||
async def handle_connection(self, request: web.Request) -> web.WebSocketResponse:
|
||||
"""Handle new WebSocket connections."""
|
||||
ws = web.WebSocketResponse(heartbeat=30)
|
||||
await ws.prepare(request)
|
||||
|
||||
try:
|
||||
# Send initial state
|
||||
await ws.send_json({
|
||||
"messages": [
|
||||
{
|
||||
"id": message.id,
|
||||
"content": message.content,
|
||||
"message_type": message.message_type.value
|
||||
}
|
||||
for message in self._io_buffer.messages
|
||||
],
|
||||
"last_read_timestamp": self._io_buffer.last_read_timestamp
|
||||
})
|
||||
self._clients.add(ws)
|
||||
async for msg in ws:
|
||||
if msg.type == WSMsgType.ERROR:
|
||||
print(f"WebSocket connection closed with error: {ws.exception()}")
|
||||
finally:
|
||||
self._clients.remove(ws)
|
||||
|
||||
return ws
|
||||
@@ -1,55 +0,0 @@
|
||||
from aiohttp import web, WSMsgType
|
||||
from typing import Dict, Set
|
||||
|
||||
from .util import wrap_async
|
||||
from ..web_agent import WebAgent
|
||||
|
||||
class ContextWebSocket:
|
||||
"""
|
||||
WebSocket handler for context changes.
|
||||
Broadcasts context updates to all connected clients.
|
||||
"""
|
||||
|
||||
def __init__(self, agent: WebAgent):
|
||||
self._agent = agent
|
||||
self._clients: Set[web.WebSocketResponse] = set()
|
||||
self._agent.add_context_change_handler(wrap_async(self._handle_context_change))
|
||||
|
||||
async def _broadcast_message(self, message: Dict):
|
||||
"""Broadcast message to all connected clients."""
|
||||
disconnected = set()
|
||||
for ws in self._clients:
|
||||
try:
|
||||
await ws.send_json(message)
|
||||
except ConnectionResetError:
|
||||
disconnected.add(ws)
|
||||
self._clients -= disconnected
|
||||
|
||||
async def _handle_context_change(self, context: str, generated: bool):
|
||||
"""Handle context changes from the WebAgent."""
|
||||
await self._broadcast_message({
|
||||
"context": context,
|
||||
"generated": generated
|
||||
})
|
||||
|
||||
async def handle_connection(self, request: web.Request) -> web.WebSocketResponse:
|
||||
"""Handle new WebSocket connections."""
|
||||
ws = web.WebSocketResponse(heartbeat=30)
|
||||
await ws.prepare(request)
|
||||
|
||||
self._clients.add(ws)
|
||||
|
||||
try:
|
||||
# Send initial context
|
||||
await ws.send_json({
|
||||
"context": self._agent.context,
|
||||
"generated": True
|
||||
})
|
||||
|
||||
async for msg in ws:
|
||||
if msg.type == WSMsgType.ERROR:
|
||||
print(f"WebSocket connection closed with error: {ws.exception()}")
|
||||
finally:
|
||||
self._clients.remove(ws)
|
||||
|
||||
return ws
|
||||
@@ -1,8 +1,6 @@
|
||||
from aiohttp import web, WSMsgType
|
||||
from typing import Dict, Set
|
||||
|
||||
from ..entry import Entry
|
||||
from ..web_agent import WebAgent
|
||||
from ..working_memory import WorkingMemory
|
||||
from .util import wrap_async
|
||||
|
||||
|
||||
@@ -2,18 +2,19 @@ from aiohttp import web, WSMsgType
|
||||
from typing import Dict, Set
|
||||
|
||||
from .util import wrap_async
|
||||
from ..web_agent import WebAgent, LlmState
|
||||
from ..web_agent import WebAgent, AgentState
|
||||
|
||||
class LlmWebSocket:
|
||||
class StateWebSocket:
|
||||
"""
|
||||
WebSocket handler for LLM state changes.
|
||||
WebSocket handler for agent state changes.
|
||||
Broadcasts state updates to all connected clients.
|
||||
"""
|
||||
|
||||
def __init__(self, agent: WebAgent):
|
||||
self._agent = agent
|
||||
self._clients: Set[web.WebSocketResponse] = set()
|
||||
self._agent.add_llm_change_handler(wrap_async(self._handle_state_change))
|
||||
self._agent.add_state_change_handler(wrap_async(self._handle_change))
|
||||
self._agent.add_selected_llm_change_handler(wrap_async(self._handle_change))
|
||||
|
||||
async def _broadcast_message(self, message: Dict):
|
||||
"""Broadcast message to all connected clients."""
|
||||
@@ -24,12 +25,12 @@ class LlmWebSocket:
|
||||
except ConnectionResetError:
|
||||
disconnected.add(ws)
|
||||
self._clients -= disconnected
|
||||
|
||||
async def _handle_state_change(self, llm_name: str, new_state: LlmState):
|
||||
"""Handle state changes from the WebAgent."""
|
||||
|
||||
async def _handle_change(self, arg):
|
||||
"""Handle changes to the active LLM."""
|
||||
await self._broadcast_message({
|
||||
"llm": llm_name,
|
||||
"state": new_state.name
|
||||
"state": self._agent.state.name,
|
||||
"active_llm": self._agent.active_llm
|
||||
})
|
||||
|
||||
async def handle_connection(self, request: web.Request) -> web.WebSocketResponse:
|
||||
@@ -38,16 +39,12 @@ class LlmWebSocket:
|
||||
await ws.prepare(request)
|
||||
|
||||
try:
|
||||
# Send initial states for all LLMs
|
||||
states = self._agent.llms
|
||||
for llm_name, state in states.items():
|
||||
await ws.send_json({
|
||||
"llm": llm_name,
|
||||
"state": state.name
|
||||
})
|
||||
|
||||
# Send initial state
|
||||
await ws.send_json({
|
||||
"state": self._agent.state.name,
|
||||
"active_llm": self._agent.active_llm
|
||||
})
|
||||
self._clients.add(ws)
|
||||
|
||||
async for msg in ws:
|
||||
if msg.type == WSMsgType.ERROR:
|
||||
print(f"WebSocket connection closed with error: {ws.exception()}")
|
||||
@@ -1,28 +1,25 @@
|
||||
from aiohttp import web
|
||||
|
||||
from ..auto_approver import AutoApprover
|
||||
from ..chat_io_buffer import ChatIOBuffer
|
||||
from ..web_agent import WebAgent
|
||||
from ..web_io_buffer import WebIOBuffer
|
||||
from ..working_memory import WorkingMemory
|
||||
from .auto_approver_websocket import AutoApproverWebSocket
|
||||
from .context_websocket import ContextWebSocket
|
||||
from .llm_websocket import LlmWebSocket
|
||||
from .chat_websocket import ChatWebSocket
|
||||
from .memory_websocket import MemoryWebSocket
|
||||
from .response_websocket import ResponseWebSocket
|
||||
from .stdout_websocket import StdoutWebSocket
|
||||
from .state_websocket import StateWebSocket
|
||||
|
||||
class Websockets:
|
||||
def __init__(self, app: web.Application, agent: WebAgent, io_buffer: WebIOBuffer, auto_approver: AutoApprover, working_memory: WorkingMemory):
|
||||
def __init__(self, app: web.Application, agent: WebAgent, io_buffer: ChatIOBuffer, auto_approver: AutoApprover, working_memory: WorkingMemory):
|
||||
self._auto_approver_ws = AutoApproverWebSocket(auto_approver)
|
||||
self._context_ws = ContextWebSocket(agent)
|
||||
self._llm_ws = LlmWebSocket(agent)
|
||||
self._chat_ws = ChatWebSocket(io_buffer)
|
||||
self._memory_ws = MemoryWebSocket(working_memory)
|
||||
self._response_ws = ResponseWebSocket(agent)
|
||||
self._stdout_ws = StdoutWebSocket(io_buffer)
|
||||
self._state_ws = StateWebSocket(agent)
|
||||
|
||||
app.router.add_get("/ws/auto_approver", self._auto_approver_ws.handle_connection)
|
||||
app.router.add_get("/ws/context", self._context_ws.handle_connection)
|
||||
app.router.add_get("/ws/llms", self._llm_ws.handle_connection)
|
||||
app.router.add_get("/ws/chat", self._chat_ws.handle_connection)
|
||||
app.router.add_get("/ws/memory", self._memory_ws.handle_connection)
|
||||
app.router.add_get("/ws/response", self._response_ws.handle_connection)
|
||||
app.router.add_get("/ws/stdout", self._stdout_ws.handle_connection)
|
||||
app.router.add_get("/ws/state", self._state_ws.handle_connection)
|
||||
Reference in New Issue
Block a user