forked from bcurts/agentchattr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.py
More file actions
63 lines (51 loc) · 2.09 KB
/
Copy pathagents.py
File metadata and controls
63 lines (51 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Agent trigger — writes to queue files picked up by visible worker terminals."""
import json
import logging
from pathlib import Path
log = logging.getLogger(__name__)
class AgentTrigger:
def __init__(self, config: dict, data_dir: str = "./data"):
self._config = config
self._data_dir = Path(data_dir)
self._sessions_path = self._data_dir / "sessions.json"
self._sessions: dict[str, str] = {}
self._load_sessions()
def _load_sessions(self):
if self._sessions_path.exists():
try:
self._sessions = json.loads(
self._sessions_path.read_text("utf-8"))
except Exception:
self._sessions = {}
def is_available(self, name: str) -> bool:
return name in self._config
def is_busy(self, name: str) -> bool:
return False # Worker handles busy state
def get_status(self) -> dict:
# Reload sessions in case worker updated them
self._load_sessions()
# Check MCP presence to determine if agent is actually online
from mcp_bridge import is_online
return {
name: {
"available": is_online(name),
"busy": False,
"label": cfg.get("label", name),
"color": cfg.get("color", "#888"),
"session_id": self._sessions.get(name),
}
for name, cfg in self._config.items()
}
async def trigger(self, agent_name: str, message: str = "", **kwargs):
"""Write to the agent's queue file. The worker terminal picks it up."""
queue_file = self._data_dir / f"{agent_name}_queue.jsonl"
self._data_dir.mkdir(parents=True, exist_ok=True)
import time
entry = {
"sender": message.split(":")[0].strip() if ":" in message else "?",
"text": message,
"time": time.strftime("%H:%M:%S"),
}
with open(queue_file, "a", encoding="utf-8") as f:
f.write(json.dumps(entry) + "\n")
log.info("Queued @%s trigger: %s", agent_name, message[:80])