mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-04-28 11:40:47 +00:00
- Add WsHandler base class, WsManager (connection tracking / event routing / buffering), WsResult - Extract network.py (is_loopback_address) and context_utils.py (use_context) to eliminate duplication - Migrate three handlers to api/ following the ws_* py naming convention - Simplify run_ui.py WebSocket init from ~170 lines to ~10 - Update import paths in api.py, plugins.py, state_monitor.py
30 lines
943 B
Python
30 lines
943 B
Python
"""Shared context helper used by both ApiHandler and WsHandler."""
|
|
|
|
import threading
|
|
from typing import Union
|
|
|
|
ThreadLockType = Union[threading.Lock, threading.RLock]
|
|
|
|
|
|
def use_context(lock: ThreadLockType, ctxid: str, create_if_not_exists: bool = True):
|
|
from agent import AgentContext
|
|
from initialize import initialize_agent
|
|
|
|
with lock:
|
|
if not ctxid:
|
|
first = AgentContext.first()
|
|
if first:
|
|
AgentContext.use(first.id)
|
|
return first
|
|
context = AgentContext(config=initialize_agent(), set_current=True)
|
|
return context
|
|
got = AgentContext.use(ctxid)
|
|
if got:
|
|
return got
|
|
if create_if_not_exists:
|
|
context = AgentContext(
|
|
config=initialize_agent(), id=ctxid, set_current=True
|
|
)
|
|
return context
|
|
else:
|
|
raise Exception(f"Context {ctxid} not found")
|