Add context info in ui
This commit is contained in:
@@ -58,6 +58,8 @@ class Api:
|
|||||||
self._app.router.add_post("/api/memory/entry/{id}/update", self._update_entry)
|
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)
|
self._app.router.add_post("/api/memory/load_iteration", self._load_iteration)
|
||||||
|
|
||||||
|
self._app.router.add_get("/api/context_info", self._get_context_info)
|
||||||
|
|
||||||
async def _get_llms(self, request: web.Request) -> web.Response:
|
async def _get_llms(self, request: web.Request) -> web.Response:
|
||||||
return web.Response(
|
return web.Response(
|
||||||
text=json.dumps(
|
text=json.dumps(
|
||||||
@@ -294,3 +296,10 @@ class Api:
|
|||||||
return web.Response(status=200)
|
return web.Response(status=200)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return web.Response(status=400, text=str(e))
|
return web.Response(status=400, text=str(e))
|
||||||
|
|
||||||
|
async def _get_context_info(self, request: web.Request) -> web.Response:
|
||||||
|
"""Get current context info."""
|
||||||
|
return web.Response(
|
||||||
|
text=json.dumps(self._agent.context_info),
|
||||||
|
content_type="application/json"
|
||||||
|
)
|
||||||
48
sia/web/context_info_websocket.py
Normal file
48
sia/web/context_info_websocket.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
from aiohttp import web, WSMsgType
|
||||||
|
from typing import Dict, Set
|
||||||
|
|
||||||
|
from .util import wrap_async
|
||||||
|
from ..web_agent import WebAgent
|
||||||
|
|
||||||
|
class ContextInfoWebSocket:
|
||||||
|
"""
|
||||||
|
WebSocket handler for context info updates.
|
||||||
|
Broadcasts context info to all connected clients when it changes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
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_info: Dict):
|
||||||
|
"""Handle context changes from the agent."""
|
||||||
|
await self._broadcast_message(context_info)
|
||||||
|
|
||||||
|
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 context info
|
||||||
|
await ws.send_json(self._agent.context_info)
|
||||||
|
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.discard(ws)
|
||||||
|
|
||||||
|
return ws
|
||||||
@@ -6,6 +6,7 @@ from ..web_agent import WebAgent
|
|||||||
from ..working_memory import WorkingMemory
|
from ..working_memory import WorkingMemory
|
||||||
from .auto_approver_websocket import AutoApproverWebSocket
|
from .auto_approver_websocket import AutoApproverWebSocket
|
||||||
from .chat_websocket import ChatWebSocket
|
from .chat_websocket import ChatWebSocket
|
||||||
|
from .context_info_websocket import ContextInfoWebSocket
|
||||||
from .memory_websocket import MemoryWebSocket
|
from .memory_websocket import MemoryWebSocket
|
||||||
from .response_websocket import ResponseWebSocket
|
from .response_websocket import ResponseWebSocket
|
||||||
from .state_websocket import StateWebSocket
|
from .state_websocket import StateWebSocket
|
||||||
@@ -14,12 +15,14 @@ class Websockets:
|
|||||||
def __init__(self, app: web.Application, agent: WebAgent, io_buffer: ChatIOBuffer, 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._auto_approver_ws = AutoApproverWebSocket(auto_approver)
|
||||||
self._chat_ws = ChatWebSocket(io_buffer)
|
self._chat_ws = ChatWebSocket(io_buffer)
|
||||||
|
self._context_info_ws = ContextInfoWebSocket(agent)
|
||||||
self._memory_ws = MemoryWebSocket(working_memory)
|
self._memory_ws = MemoryWebSocket(working_memory)
|
||||||
self._response_ws = ResponseWebSocket(agent)
|
self._response_ws = ResponseWebSocket(agent)
|
||||||
self._state_ws = StateWebSocket(agent)
|
self._state_ws = StateWebSocket(agent)
|
||||||
|
|
||||||
app.router.add_get("/ws/auto_approver", self._auto_approver_ws.handle_connection)
|
app.router.add_get("/ws/auto_approver", self._auto_approver_ws.handle_connection)
|
||||||
app.router.add_get("/ws/chat", self._chat_ws.handle_connection)
|
app.router.add_get("/ws/chat", self._chat_ws.handle_connection)
|
||||||
|
app.router.add_get("/ws/context_info", self._context_info_ws.handle_connection)
|
||||||
app.router.add_get("/ws/memory", self._memory_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/response", self._response_ws.handle_connection)
|
||||||
app.router.add_get("/ws/state", self._state_ws.handle_connection)
|
app.router.add_get("/ws/state", self._state_ws.handle_connection)
|
||||||
@@ -45,6 +45,7 @@ class WebAgent(BaseAgent):
|
|||||||
|
|
||||||
self._state_change_handlers: List[Callable[[AgentState], None]] = []
|
self._state_change_handlers: List[Callable[[AgentState], None]] = []
|
||||||
self._selected_llm_change_handlers: List[Callable[[str], None]] = []
|
self._selected_llm_change_handlers: List[Callable[[str], None]] = []
|
||||||
|
self._context_change_handlers: List[Callable[[Dict], None]] = []
|
||||||
|
|
||||||
self._update_compiled_context()
|
self._update_compiled_context()
|
||||||
self._working_memory.add_change_handler(self._update_compiled_context)
|
self._working_memory.add_change_handler(self._update_compiled_context)
|
||||||
@@ -81,6 +82,13 @@ class WebAgent(BaseAgent):
|
|||||||
def add_state_change_handler(self, handler: Callable[[AgentState], None]) -> None:
|
def add_state_change_handler(self, handler: Callable[[AgentState], None]) -> None:
|
||||||
self._state_change_handlers.append(handler)
|
self._state_change_handlers.append(handler)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def context_info(self) -> Dict:
|
||||||
|
return dict(self._compiled_context.attrib)
|
||||||
|
|
||||||
|
def add_context_change_handler(self, handler: Callable[[Dict], None]) -> None:
|
||||||
|
self._context_change_handlers.append(handler)
|
||||||
|
|
||||||
def run_inference(self) -> None:
|
def run_inference(self) -> None:
|
||||||
"""Start inference on specified LLM"""
|
"""Start inference on specified LLM"""
|
||||||
if self._state != AgentState.IDLE:
|
if self._state != AgentState.IDLE:
|
||||||
@@ -133,3 +141,14 @@ class WebAgent(BaseAgent):
|
|||||||
|
|
||||||
def _update_compiled_context(self):
|
def _update_compiled_context(self):
|
||||||
self._compiled_context = self._compile_context(self._llms[self.active_llm])
|
self._compiled_context = self._compile_context(self._llms[self.active_llm])
|
||||||
|
self._notify_context_change()
|
||||||
|
|
||||||
|
def _notify_context_change(self):
|
||||||
|
"""Notify all context change handlers."""
|
||||||
|
for handler in self._context_change_handlers:
|
||||||
|
handler(self.context_info)
|
||||||
|
|
||||||
|
def _notify_selected_llm_change(self):
|
||||||
|
"""Notify all selected LLM change handlers."""
|
||||||
|
for handler in self._selected_llm_change_handlers:
|
||||||
|
handler(self._selected_llm)
|
||||||
@@ -2,6 +2,7 @@ import React, { useState } from 'react';
|
|||||||
import { useWebSocketContext } from '../../contexts/WebSocketContext';
|
import { useWebSocketContext } from '../../contexts/WebSocketContext';
|
||||||
import { api, EntryTypes, getInitialEntryData } from '../../services/api';
|
import { api, EntryTypes, getInitialEntryData } from '../../services/api';
|
||||||
import {
|
import {
|
||||||
|
ContextInfoEntry,
|
||||||
ReasoningEntry,
|
ReasoningEntry,
|
||||||
SingleEntry,
|
SingleEntry,
|
||||||
RepeatEntry,
|
RepeatEntry,
|
||||||
@@ -254,6 +255,8 @@ const MemoryEditor: React.FC<MemoryEditorProps> = ({ className = '' }) => {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
<ContextInfoEntry />
|
||||||
|
|
||||||
{memory.length === 0 ? (
|
{memory.length === 0 ? (
|
||||||
<div className="flex items-center justify-center h-full">
|
<div className="flex items-center justify-center h-full">
|
||||||
<p className="text-gray-500">No memory entries</p>
|
<p className="text-gray-500">No memory entries</p>
|
||||||
|
|||||||
30
web/src/components/Memory/entries/ContextInfoEntry.tsx
Normal file
30
web/src/components/Memory/entries/ContextInfoEntry.tsx
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { useWebSocketContext } from '../../../contexts/WebSocketContext';
|
||||||
|
|
||||||
|
const ContextInfoEntry: React.FC = () => {
|
||||||
|
const { contextInfo } = useWebSocketContext();
|
||||||
|
|
||||||
|
if (!contextInfo) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mb-4 border rounded-lg bg-gray-50">
|
||||||
|
<div className="px-3 py-2 bg-gray-100 border-b rounded-t-lg">
|
||||||
|
<span className="text-sm font-medium">Context Info</span>
|
||||||
|
</div>
|
||||||
|
<div className="p-3">
|
||||||
|
<div className="space-y-1 text-sm font-mono">
|
||||||
|
{Object.entries(contextInfo).map(([key, value]) => (
|
||||||
|
<div key={key} className="flex">
|
||||||
|
<span className="text-gray-600 mr-2">{key}:</span>
|
||||||
|
<span className="text-gray-900">{String(value)}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ContextInfoEntry;
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export { default as BaseEntry } from './BaseEntry';
|
export { default as BaseEntry } from './BaseEntry';
|
||||||
export { default as BackgroundEntry } from './BackgroundEntry';
|
export { default as BackgroundEntry } from './BackgroundEntry';
|
||||||
|
export { default as ContextInfoEntry } from './ContextInfoEntry';
|
||||||
export { default as ParseErrorEntry } from './ParseErrorEntry';
|
export { default as ParseErrorEntry } from './ParseErrorEntry';
|
||||||
export { default as ReadEntry } from './ReadEntry';
|
export { default as ReadEntry } from './ReadEntry';
|
||||||
export { default as ReasoningEntry } from './ReasoningEntry';
|
export { default as ReasoningEntry } from './ReasoningEntry';
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
|
import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
|
||||||
import { AutoApproverConfig, ChatMessage } from '../types';
|
import { AutoApproverConfig, ChatMessage } from '../types';
|
||||||
|
|
||||||
|
interface ContextInfo {
|
||||||
|
[key: string]: string | number;
|
||||||
|
token_count: number;
|
||||||
|
token_limit: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface WebSocketContextType {
|
interface WebSocketContextType {
|
||||||
state: {
|
state: {
|
||||||
agentState: 'IDLE' | 'INFERENCE' | 'PROCESSING_RESPONSE';
|
agentState: 'IDLE' | 'INFERENCE' | 'PROCESSING_RESPONSE';
|
||||||
@@ -17,6 +23,7 @@ interface WebSocketContextType {
|
|||||||
llms: {name: string}[];
|
llms: {name: string}[];
|
||||||
setActiveLLM: (llm: string) => Promise<void>;
|
setActiveLLM: (llm: string) => Promise<void>;
|
||||||
connectionStatus: Record<string, string>;
|
connectionStatus: Record<string, string>;
|
||||||
|
contextInfo: ContextInfo | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultState: WebSocketContextType = {
|
const defaultState: WebSocketContextType = {
|
||||||
@@ -40,6 +47,7 @@ const defaultState: WebSocketContextType = {
|
|||||||
llms: [],
|
llms: [],
|
||||||
setActiveLLM: async () => {},
|
setActiveLLM: async () => {},
|
||||||
connectionStatus: {},
|
connectionStatus: {},
|
||||||
|
contextInfo: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const WebSocketContext = createContext<WebSocketContextType>(defaultState);
|
const WebSocketContext = createContext<WebSocketContextType>(defaultState);
|
||||||
@@ -56,6 +64,7 @@ export const WebSocketProvider: React.FC<{children: React.ReactNode}> = ({ child
|
|||||||
const [autoApprover, setAutoApprover] = useState<AutoApproverConfig>(defaultState.autoApprover);
|
const [autoApprover, setAutoApprover] = useState<AutoApproverConfig>(defaultState.autoApprover);
|
||||||
const [llms, setLLMs] = useState<{name: string}[]>([]);
|
const [llms, setLLMs] = useState<{name: string}[]>([]);
|
||||||
const [connectionStatus, setConnectionStatus] = useState<Record<string, string>>({});
|
const [connectionStatus, setConnectionStatus] = useState<Record<string, string>>({});
|
||||||
|
const [contextInfo, setContextInfo] = useState<ContextInfo | null>(null);
|
||||||
|
|
||||||
const sockets = useRef<{[key: string]: WebSocket | null}>({});
|
const sockets = useRef<{[key: string]: WebSocket | null}>({});
|
||||||
|
|
||||||
@@ -101,7 +110,7 @@ export const WebSocketProvider: React.FC<{children: React.ReactNode}> = ({ child
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const endpoints = ['ws/state', 'ws/response', 'ws/memory', 'ws/auto_approver', 'ws/chat'];
|
const endpoints = ['ws/state', 'ws/response', 'ws/memory', 'ws/auto_approver', 'ws/chat', 'ws/context_info'];
|
||||||
const newConnectionStatus: Record<string, string> = {};
|
const newConnectionStatus: Record<string, string> = {};
|
||||||
|
|
||||||
const createWebSocket = (endpoint: string) => {
|
const createWebSocket = (endpoint: string) => {
|
||||||
@@ -171,6 +180,8 @@ export const WebSocketProvider: React.FC<{children: React.ReactNode}> = ({ child
|
|||||||
if (data.last_read_timestamp) {
|
if (data.last_read_timestamp) {
|
||||||
setLastReadTimestamp(data.last_read_timestamp);
|
setLastReadTimestamp(data.last_read_timestamp);
|
||||||
}
|
}
|
||||||
|
} else if (endpoint === 'ws/context_info') {
|
||||||
|
setContextInfo(data);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to parse message from ${endpoint}:`, error);
|
console.error(`Failed to parse message from ${endpoint}:`, error);
|
||||||
@@ -232,7 +243,8 @@ export const WebSocketProvider: React.FC<{children: React.ReactNode}> = ({ child
|
|||||||
isConnected,
|
isConnected,
|
||||||
llms,
|
llms,
|
||||||
setActiveLLM,
|
setActiveLLM,
|
||||||
connectionStatus
|
connectionStatus,
|
||||||
|
contextInfo
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user