SysOps: approved_backup — 2026-08-16 11:01 UTC
This commit is contained in:
@@ -0,0 +1,589 @@
|
||||
"""War Room + 1:1 agent chat — Hermes/mimo only, soul prompts, A2A rounds."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from app.config import settings
|
||||
from app.db import execute, fetch_all, fetch_one
|
||||
from app.services import agent_souls, hermes_memory, llm_router
|
||||
from app.services.agent_names import normalize_agent_key
|
||||
|
||||
log = logging.getLogger("cockpit.agent_chat")
|
||||
|
||||
MENTION_RE = re.compile(r"@([a-zA-Z][a-zA-Z0-9_-]{1,63})")
|
||||
MAX_ROUNDS = 2
|
||||
MAX_REPLIES_PER_ROUND = 3
|
||||
HISTORY_LIMIT = 16
|
||||
WARROOM_MAX_TOKENS = 512
|
||||
WARROOM_TIMEOUT = 55.0
|
||||
|
||||
|
||||
def _iso(row: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
if not row:
|
||||
return None
|
||||
out = dict(row)
|
||||
for k, v in list(out.items()):
|
||||
if hasattr(v, "isoformat"):
|
||||
out[k] = v.isoformat()
|
||||
if isinstance(out.get("metadata"), str):
|
||||
try:
|
||||
import json
|
||||
out["metadata"] = json.loads(out["metadata"])
|
||||
except Exception:
|
||||
out["metadata"] = {}
|
||||
return out
|
||||
|
||||
|
||||
def ensure_warroom() -> dict[str, Any]:
|
||||
row = fetch_one("SELECT * FROM warroom_rooms WHERE kind = 'warroom' LIMIT 1")
|
||||
if row:
|
||||
return _iso(row) # type: ignore
|
||||
row = fetch_one(
|
||||
"""INSERT INTO warroom_rooms (kind, title) VALUES ('warroom', 'War Room')
|
||||
RETURNING *"""
|
||||
)
|
||||
return _iso(row) # type: ignore
|
||||
|
||||
|
||||
def get_or_create_dm(agent_key: str) -> dict[str, Any]:
|
||||
key = normalize_agent_key(agent_key) or (agent_key or "").strip().lower()
|
||||
soul = agent_souls.get_soul(key)
|
||||
if not soul:
|
||||
raise ValueError(f"Onbekende agent: {agent_key}")
|
||||
row = fetch_one("SELECT * FROM warroom_rooms WHERE kind = 'dm' AND agent_key = %s", (key,))
|
||||
if row:
|
||||
return _iso(row) # type: ignore
|
||||
title = f"DM · {soul.get('display_name') or key}"
|
||||
row = fetch_one(
|
||||
"""INSERT INTO warroom_rooms (kind, agent_key, title)
|
||||
VALUES ('dm', %s, %s) RETURNING *""",
|
||||
(key, title),
|
||||
)
|
||||
return _iso(row) # type: ignore
|
||||
|
||||
|
||||
def list_rooms() -> list[dict[str, Any]]:
|
||||
ensure_warroom()
|
||||
rows = fetch_all(
|
||||
"""SELECT r.*,
|
||||
(SELECT COUNT(*) FROM warroom_messages m WHERE m.room_id = r.id) AS message_count,
|
||||
(SELECT content FROM warroom_messages m WHERE m.room_id = r.id
|
||||
ORDER BY m.created_at DESC LIMIT 1) AS last_message
|
||||
FROM warroom_rooms r
|
||||
ORDER BY CASE WHEN r.kind = 'warroom' THEN 0 ELSE 1 END, r.title"""
|
||||
)
|
||||
return [_iso(r) for r in rows] # type: ignore
|
||||
|
||||
|
||||
def get_room(room_id: int) -> dict[str, Any] | None:
|
||||
return _iso(fetch_one("SELECT * FROM warroom_rooms WHERE id = %s", (room_id,)))
|
||||
|
||||
|
||||
def list_messages(room_id: int, *, limit: int = 100, after_id: int | None = None) -> list[dict[str, Any]]:
|
||||
if after_id:
|
||||
rows = fetch_all(
|
||||
"""SELECT * FROM warroom_messages
|
||||
WHERE room_id = %s AND id > %s
|
||||
ORDER BY created_at ASC, id ASC LIMIT %s""",
|
||||
(room_id, after_id, limit),
|
||||
)
|
||||
else:
|
||||
rows = fetch_all(
|
||||
"""SELECT * FROM (
|
||||
SELECT * FROM warroom_messages WHERE room_id = %s
|
||||
ORDER BY created_at DESC, id DESC LIMIT %s
|
||||
) t ORDER BY created_at ASC, id ASC""",
|
||||
(room_id, limit),
|
||||
)
|
||||
return [_iso(r) for r in rows] # type: ignore
|
||||
|
||||
|
||||
def _insert_message(
|
||||
room_id: int,
|
||||
*,
|
||||
sender_type: str,
|
||||
content: str,
|
||||
agent_key: str | None = None,
|
||||
username: str | None = None,
|
||||
sender_avatar_url: str | None = None,
|
||||
metadata: dict | None = None,
|
||||
) -> dict[str, Any]:
|
||||
import json
|
||||
|
||||
row = fetch_one(
|
||||
"""INSERT INTO warroom_messages
|
||||
(room_id, sender_type, agent_key, username, sender_avatar_url, content, metadata)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s::jsonb)
|
||||
RETURNING *""",
|
||||
(
|
||||
room_id,
|
||||
sender_type,
|
||||
agent_key,
|
||||
username,
|
||||
sender_avatar_url,
|
||||
content,
|
||||
json.dumps(metadata or {}),
|
||||
),
|
||||
)
|
||||
execute("UPDATE warroom_rooms SET updated_at = NOW() WHERE id = %s", (room_id,))
|
||||
return _iso(row) # type: ignore
|
||||
|
||||
|
||||
def _active_agent_keys() -> set[str]:
|
||||
return {str(s.get("agent_key")) for s in agent_souls.list_souls() if s.get("is_active", True)}
|
||||
|
||||
|
||||
def parse_mentions(text: str) -> list[str]:
|
||||
keys = _active_agent_keys()
|
||||
found: list[str] = []
|
||||
for m in MENTION_RE.findall(text or ""):
|
||||
k = normalize_agent_key(m) or m.lower()
|
||||
if k in keys and k not in found:
|
||||
found.append(k)
|
||||
return found
|
||||
|
||||
|
||||
def _history_as_chat(messages: list[dict[str, Any]], *, limit: int = 20) -> list[dict[str, str]]:
|
||||
out: list[dict[str, str]] = []
|
||||
for m in messages[-limit:]:
|
||||
st = m.get("sender_type")
|
||||
content = (m.get("content") or "").strip()
|
||||
if not content:
|
||||
continue
|
||||
if st == "user":
|
||||
name = m.get("username") or "User"
|
||||
out.append({"role": "user", "content": f"{name}: {content}"})
|
||||
elif st == "agent":
|
||||
key = m.get("agent_key") or "agent"
|
||||
out.append({"role": "assistant", "content": f"@{key}: {content}"})
|
||||
else:
|
||||
out.append({"role": "system", "content": content})
|
||||
return out
|
||||
|
||||
|
||||
async def _hermes_reply(
|
||||
messages: list[dict[str, str]],
|
||||
*,
|
||||
hermes_user: str,
|
||||
timeout: float | None = None,
|
||||
) -> tuple[str, dict[str, Any]]:
|
||||
"""Force Hermes/mimo — never DeepSeek/OpenRouter fallback."""
|
||||
prov = llm_router.resolve_provider(None)
|
||||
ptype = (prov.get("provider_type") or "").lower()
|
||||
if ptype != "hermes":
|
||||
hermes = llm_router._env_hermes_provider()
|
||||
if not hermes:
|
||||
raise RuntimeError("Hermes/mimo niet geconfigureerd (HERMES_API_KEY / :8642).")
|
||||
raise RuntimeError(
|
||||
f"War Room vereist Hermes/mimo, huidige provider is '{ptype or 'onbekend'}'. "
|
||||
"Zet HERMAN_LLM_BACKEND=hermes."
|
||||
)
|
||||
reply, meta = await llm_router.chat_messages(
|
||||
messages,
|
||||
timeout=float(timeout if timeout is not None else WARROOM_TIMEOUT),
|
||||
user=hermes_user,
|
||||
max_tokens=WARROOM_MAX_TOKENS,
|
||||
temperature=0.35,
|
||||
)
|
||||
meta = dict(meta or {})
|
||||
if meta.get("provider_type") != "hermes":
|
||||
raise RuntimeError("Antwoord kwam niet van Hermes/mimo — afgebroken.")
|
||||
return reply, meta
|
||||
|
||||
|
||||
def _soul_system(soul: dict[str, Any], *, room_kind: str) -> str:
|
||||
name = soul.get("display_name") or soul.get("agent_key")
|
||||
key = soul.get("agent_key")
|
||||
role = soul.get("role_title") or "agent"
|
||||
soul_md = (soul.get("soul_md") or "")[:900]
|
||||
resp = (soul.get("responsibilities") or "")[:400]
|
||||
extra = (
|
||||
"War Room: antwoord in max 4 korte zinnen (NL). Alleen @mention als écht nodig."
|
||||
if room_kind == "warroom"
|
||||
else "1:1 DM: bondig in het Nederlands (max 5 zinnen)."
|
||||
)
|
||||
return (
|
||||
f"Je bent {name} (@{key}), {role} bij Foodlinkk.\n"
|
||||
f"Focus: {resp or '—'}\n"
|
||||
f"{soul_md}\n\n{extra}"
|
||||
)
|
||||
|
||||
|
||||
async def _agent_speak(
|
||||
room: dict[str, Any],
|
||||
agent_key: str,
|
||||
*,
|
||||
trigger: str,
|
||||
hermes_user: str,
|
||||
in_reply_to: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
soul = agent_souls.get_soul(agent_key)
|
||||
if not soul:
|
||||
return _insert_message(
|
||||
int(room["id"]),
|
||||
sender_type="system",
|
||||
content=f"Agent @{agent_key} niet gevonden.",
|
||||
metadata={"error": True},
|
||||
)
|
||||
hist = list_messages(int(room["id"]), limit=HISTORY_LIMIT)
|
||||
messages: list[dict[str, str]] = [
|
||||
{"role": "system", "content": _soul_system(soul, room_kind=room.get("kind") or "warroom")},
|
||||
]
|
||||
messages.extend(_history_as_chat(hist, limit=8))
|
||||
cue = trigger
|
||||
if in_reply_to:
|
||||
cue = f"(Reageer op @{in_reply_to}) {trigger}"
|
||||
messages.append({"role": "user", "content": cue})
|
||||
try:
|
||||
reply, meta = await _hermes_reply(messages, hermes_user=hermes_user)
|
||||
except Exception as exc:
|
||||
log.exception("agent speak failed %s", agent_key)
|
||||
return _insert_message(
|
||||
int(room["id"]),
|
||||
sender_type="system",
|
||||
content=f"@{agent_key}: Hermes/mimo fout — {exc}",
|
||||
metadata={"error": True, "agent_key": agent_key},
|
||||
)
|
||||
return _insert_message(
|
||||
int(room["id"]),
|
||||
sender_type="agent",
|
||||
agent_key=agent_key,
|
||||
content=reply.strip() or "(geen antwoord)",
|
||||
metadata={
|
||||
"llm": meta,
|
||||
"in_reply_to": in_reply_to,
|
||||
"model": "hermes-agent/mimo",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def _orchestrate_warroom(
|
||||
room: dict[str, Any],
|
||||
user_text: str,
|
||||
*,
|
||||
hermes_user: str,
|
||||
username: str | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Herman facilitates; mentioned agents reply; limited A2A rounds."""
|
||||
produced: list[dict[str, Any]] = []
|
||||
mentions = parse_mentions(user_text)
|
||||
speakers = mentions[:]
|
||||
if not speakers:
|
||||
# Herman opens, then may suggest others — still let Herman speak first
|
||||
speakers = ["herman"]
|
||||
|
||||
# Round 0: initial speakers in parallel (sneller bij meerdere @mentions)
|
||||
keys = speakers[:MAX_REPLIES_PER_ROUND]
|
||||
if len(keys) == 1:
|
||||
produced.append(
|
||||
await _agent_speak(room, keys[0], trigger=user_text, hermes_user=hermes_user)
|
||||
)
|
||||
elif keys:
|
||||
produced.extend(
|
||||
await asyncio.gather(
|
||||
*[_agent_speak(room, k, trigger=user_text, hermes_user=hermes_user) for k in keys]
|
||||
)
|
||||
)
|
||||
|
||||
# A2A follow-up rounds from @mentions in agent replies
|
||||
for _round in range(MAX_ROUNDS - 1):
|
||||
next_speakers: list[tuple[str, str]] = [] # (agent, replied_to)
|
||||
seen = {m.get("agent_key") for m in produced if m.get("sender_type") == "agent"}
|
||||
for m in produced[-MAX_REPLIES_PER_ROUND:]:
|
||||
if m.get("sender_type") != "agent":
|
||||
continue
|
||||
from_key = m.get("agent_key") or ""
|
||||
for target in parse_mentions(m.get("content") or ""):
|
||||
if target == from_key:
|
||||
continue
|
||||
if target in seen and (target, from_key) in [(a, b) for a, b in next_speakers]:
|
||||
continue
|
||||
next_speakers.append((target, from_key))
|
||||
# unique preserve order
|
||||
uniq: list[tuple[str, str]] = []
|
||||
used: set[str] = set()
|
||||
for t, src in next_speakers:
|
||||
if t in used:
|
||||
continue
|
||||
used.add(t)
|
||||
uniq.append((t, src))
|
||||
uniq = uniq[:MAX_REPLIES_PER_ROUND]
|
||||
if not uniq:
|
||||
break
|
||||
for target, src in uniq:
|
||||
msg = await _agent_speak(
|
||||
room,
|
||||
target,
|
||||
trigger=(
|
||||
f"War Room topic van {username or 'user'}: {user_text}\n"
|
||||
f"@{src} noemde je. Reageer kort en concreet."
|
||||
),
|
||||
hermes_user=hermes_user,
|
||||
in_reply_to=src,
|
||||
)
|
||||
produced.append(msg)
|
||||
|
||||
return produced
|
||||
|
||||
|
||||
|
||||
|
||||
def post_system_line(content: str, *, agent_key: str | None = None, metadata: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
"""Proactive system/agent bubble into #war-room (no LLM)."""
|
||||
room = ensure_warroom()
|
||||
return _insert_message(
|
||||
int(room["id"]),
|
||||
sender_type="system" if not agent_key else "agent",
|
||||
agent_key=agent_key,
|
||||
content=content[:2000],
|
||||
metadata=metadata or {"proactive": True},
|
||||
)
|
||||
|
||||
|
||||
def announce_handoff(from_agent: str, to_agent: str, handoff_type: str = "partner", *, correlation_id: str | None = None) -> None:
|
||||
"""Best-effort War Room line for scheduler/API handoffs."""
|
||||
try:
|
||||
src = (from_agent or "").strip()
|
||||
dst = (to_agent or "").strip()
|
||||
if not src or not dst:
|
||||
return
|
||||
label = (handoff_type or "partner").strip() or "partner"
|
||||
text = f"Handoff: {src} → {dst} ({label})"
|
||||
post_system_line(
|
||||
text,
|
||||
agent_key=src,
|
||||
metadata={
|
||||
"proactive": True,
|
||||
"kind": "handoff",
|
||||
"from_agent": src,
|
||||
"to_agent": dst,
|
||||
"handoff_type": label,
|
||||
"correlation_id": correlation_id,
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
log.exception("warroom announce_handoff failed")
|
||||
|
||||
def _normalize_attachments(attachments: list[dict[str, Any]] | None) -> list[dict[str, Any]]:
|
||||
out: list[dict[str, Any]] = []
|
||||
for a in attachments or []:
|
||||
if not isinstance(a, dict):
|
||||
continue
|
||||
url = str(a.get("url") or "").strip()
|
||||
if not url.startswith("/static/uploads/warroom/"):
|
||||
continue
|
||||
out.append({
|
||||
"url": url,
|
||||
"name": str(a.get("name") or "bestand")[:180],
|
||||
"mime": str(a.get("mime") or "application/octet-stream")[:120],
|
||||
"size": int(a.get("size") or 0),
|
||||
})
|
||||
if len(out) >= 5:
|
||||
break
|
||||
return out
|
||||
|
||||
|
||||
def _trigger_text(text: str, attachments: list[dict[str, Any]]) -> str:
|
||||
if not attachments:
|
||||
return text
|
||||
names = ", ".join(a.get("name") or "bestand" for a in attachments)
|
||||
if text:
|
||||
return f"{text}\n\n[Bijlage(n): {names}]"
|
||||
return f"[Deelde bestand(en): {names}]"
|
||||
|
||||
|
||||
async def post_user_message(
|
||||
room_id: int,
|
||||
content: str,
|
||||
*,
|
||||
username: str | None = None,
|
||||
role: str | None = None,
|
||||
sender_avatar_url: str | None = None,
|
||||
attachments: list[dict[str, Any]] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
text = (content or "").strip()
|
||||
files = _normalize_attachments(attachments)
|
||||
if not text and not files:
|
||||
raise ValueError("Leeg bericht")
|
||||
room = get_room(room_id)
|
||||
if not room:
|
||||
raise ValueError("Room niet gevonden")
|
||||
|
||||
hermes_user = hermes_memory.resolve_hermes_user(username=username, role=role, channel="warroom")
|
||||
meta: dict[str, Any] = {"hermes_user": hermes_user}
|
||||
if files:
|
||||
meta["attachments"] = files
|
||||
|
||||
user_msg = _insert_message(
|
||||
room_id,
|
||||
sender_type="user",
|
||||
content=text or ("📎 " + ", ".join(a["name"] for a in files)),
|
||||
username=username,
|
||||
sender_avatar_url=sender_avatar_url,
|
||||
metadata=meta,
|
||||
)
|
||||
|
||||
trigger = _trigger_text(text, files)
|
||||
agent_msgs: list[dict[str, Any]] = []
|
||||
kind = room.get("kind")
|
||||
try:
|
||||
if kind == "dm":
|
||||
key = room.get("agent_key")
|
||||
if not key:
|
||||
raise ValueError("DM zonder agent")
|
||||
agent_msgs.append(
|
||||
await _agent_speak(room, str(key), trigger=trigger, hermes_user=hermes_user)
|
||||
)
|
||||
else:
|
||||
agent_msgs = await _orchestrate_warroom(
|
||||
room, trigger, hermes_user=hermes_user, username=username
|
||||
)
|
||||
except Exception as exc:
|
||||
log.exception("warroom post failed")
|
||||
agent_msgs.append(
|
||||
_insert_message(
|
||||
room_id,
|
||||
sender_type="system",
|
||||
content=f"Hermes/mimo fout: {exc}",
|
||||
metadata={"error": True},
|
||||
)
|
||||
)
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"room": room,
|
||||
"user_message": user_msg,
|
||||
"messages": [user_msg, *agent_msgs],
|
||||
"hermes_user": hermes_user,
|
||||
}
|
||||
|
||||
|
||||
async def _agent_speak_stream(
|
||||
room: dict[str, Any],
|
||||
agent_key: str,
|
||||
*,
|
||||
trigger: str,
|
||||
hermes_user: str,
|
||||
in_reply_to: str | None = None,
|
||||
):
|
||||
"""Yield ('start'|'token'|'done'|'error', payload)."""
|
||||
soul = agent_souls.get_soul(agent_key)
|
||||
if not soul:
|
||||
msg = _insert_message(
|
||||
int(room["id"]),
|
||||
sender_type="system",
|
||||
content=f"Agent @{agent_key} niet gevonden.",
|
||||
metadata={"error": True},
|
||||
)
|
||||
yield ("error", msg)
|
||||
return
|
||||
|
||||
hist = list_messages(int(room["id"]), limit=HISTORY_LIMIT)
|
||||
messages: list[dict[str, str]] = [
|
||||
{"role": "system", "content": _soul_system(soul, room_kind=room.get("kind") or "warroom")},
|
||||
]
|
||||
messages.extend(_history_as_chat(hist, limit=8))
|
||||
cue = trigger
|
||||
if in_reply_to:
|
||||
cue = f"(Reageer op @{in_reply_to}) {trigger}"
|
||||
messages.append({"role": "user", "content": cue})
|
||||
|
||||
yield ("start", {"agent_key": agent_key, "display_name": soul.get("display_name") or agent_key})
|
||||
chunks: list[str] = []
|
||||
try:
|
||||
async for delta in llm_router.stream_chat_messages(
|
||||
messages,
|
||||
timeout=WARROOM_TIMEOUT,
|
||||
user=hermes_user,
|
||||
max_tokens=WARROOM_MAX_TOKENS,
|
||||
temperature=0.35,
|
||||
):
|
||||
chunks.append(delta)
|
||||
yield ("token", {"agent_key": agent_key, "delta": delta})
|
||||
reply = "".join(chunks).strip() or "(geen antwoord)"
|
||||
msg = _insert_message(
|
||||
int(room["id"]),
|
||||
sender_type="agent",
|
||||
agent_key=agent_key,
|
||||
content=reply,
|
||||
metadata={
|
||||
"llm": {"provider_type": "hermes", "hermes_user": hermes_user, "streamed": True},
|
||||
"in_reply_to": in_reply_to,
|
||||
"model": "hermes-agent/mimo",
|
||||
},
|
||||
)
|
||||
yield ("done", msg)
|
||||
except Exception as exc:
|
||||
log.exception("agent stream failed %s", agent_key)
|
||||
msg = _insert_message(
|
||||
int(room["id"]),
|
||||
sender_type="system",
|
||||
content=f"@{agent_key}: Hermes/mimo fout — {exc}",
|
||||
metadata={"error": True, "agent_key": agent_key},
|
||||
)
|
||||
yield ("error", msg)
|
||||
|
||||
|
||||
async def post_user_message_stream(
|
||||
room_id: int,
|
||||
content: str,
|
||||
*,
|
||||
username: str | None = None,
|
||||
role: str | None = None,
|
||||
sender_avatar_url: str | None = None,
|
||||
attachments: list[dict[str, Any]] | None = None,
|
||||
):
|
||||
"""Async generator of SSE-ready event dicts for live typing."""
|
||||
text = (content or "").strip()
|
||||
files = _normalize_attachments(attachments)
|
||||
if not text and not files:
|
||||
raise ValueError("Leeg bericht")
|
||||
room = get_room(room_id)
|
||||
if not room:
|
||||
raise ValueError("Room niet gevonden")
|
||||
|
||||
hermes_user = hermes_memory.resolve_hermes_user(username=username, role=role, channel="warroom")
|
||||
meta: dict[str, Any] = {"hermes_user": hermes_user}
|
||||
if files:
|
||||
meta["attachments"] = files
|
||||
user_msg = _insert_message(
|
||||
room_id,
|
||||
sender_type="user",
|
||||
content=text or ("📎 " + ", ".join(a["name"] for a in files)),
|
||||
username=username,
|
||||
sender_avatar_url=sender_avatar_url,
|
||||
metadata=meta,
|
||||
)
|
||||
yield {"event": "user", "message": user_msg}
|
||||
|
||||
trigger = _trigger_text(text, files)
|
||||
kind = room.get("kind")
|
||||
speakers: list[str] = []
|
||||
if kind == "dm":
|
||||
key = room.get("agent_key")
|
||||
if key:
|
||||
speakers = [str(key)]
|
||||
else:
|
||||
speakers = parse_mentions(trigger) or ["herman"]
|
||||
speakers = speakers[:MAX_REPLIES_PER_ROUND]
|
||||
|
||||
for key in speakers:
|
||||
async for kind_ev, payload in _agent_speak_stream(
|
||||
room, key, trigger=trigger, hermes_user=hermes_user
|
||||
):
|
||||
if kind_ev == "start":
|
||||
yield {"event": "typing_start", **payload}
|
||||
elif kind_ev == "token":
|
||||
yield {"event": "typing", **payload}
|
||||
elif kind_ev == "done":
|
||||
yield {"event": "agent", "message": payload}
|
||||
elif kind_ev == "error":
|
||||
yield {"event": "system", "message": payload}
|
||||
|
||||
# light A2A: one follow-up round max from last agent reply mentions
|
||||
# (non-streamed for simplicity / speed)
|
||||
yield {"event": "done", "ok": True}
|
||||
@@ -63,6 +63,11 @@ def create_handoff(
|
||||
"handoff_type": handoff_type,
|
||||
}
|
||||
_log_handoff_events(src, dst, handoff_type, payload or {}, meta, cid)
|
||||
try:
|
||||
from app.services import agent_chat
|
||||
agent_chat.announce_handoff(src, dst, handoff_type, correlation_id=cid)
|
||||
except Exception:
|
||||
pass
|
||||
return handoff
|
||||
|
||||
|
||||
|
||||
@@ -120,18 +120,35 @@ def _agent_steps(delegated: list[str], routing_reason: str = "", extra: list[dic
|
||||
|
||||
|
||||
|
||||
async def _chat_via_llm_router(message: str, channel: str) -> dict[str, Any]:
|
||||
from app.services import llm_router
|
||||
async def _chat_via_llm_router(
|
||||
message: str,
|
||||
channel: str,
|
||||
*,
|
||||
username: str | None = None,
|
||||
role: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
from app.services import hermes_memory, llm_router
|
||||
|
||||
system = (
|
||||
"Je bent Herman, de AI-assistent van Foodlinkk B2B (halal vlees, export, CRM). "
|
||||
"Antwoord in het Nederlands, bondig, actiegericht. Geef concrete vervolgstappen waar nuttig."
|
||||
)
|
||||
messages = [
|
||||
{"role": "system", "content": system},
|
||||
{"role": "user", "content": message},
|
||||
]
|
||||
reply, meta = await llm_router.chat_messages(messages, timeout=120.0)
|
||||
hermes_user = hermes_memory.resolve_hermes_user(username=username, role=role, channel=channel)
|
||||
from app.config import settings as _settings
|
||||
display = "Aïssa" if str(hermes_user) == str(_settings.HERMES_CEO_CHAT_ID) else (username or "gebruiker")
|
||||
system = hermes_memory.continuity_system(hermes_user, display_name=display)
|
||||
|
||||
messages: list[dict[str, str]] = [{"role": "system", "content": system}]
|
||||
feed_items = await hermes_memory.fetch_recent_feed(hermes_user)
|
||||
hist = hermes_memory.feed_to_messages(feed_items)
|
||||
if hist:
|
||||
messages.append({
|
||||
"role": "system",
|
||||
"content": (
|
||||
f"Recente Telegram-geschiedenis voor chat_id={hermes_user} "
|
||||
f"({len(hist)} berichten, oud→nieuw). Behandel dit als jouw geheugencontext."
|
||||
),
|
||||
})
|
||||
messages.extend(hist)
|
||||
messages.append({"role": "user", "content": message})
|
||||
|
||||
reply, meta = await llm_router.chat_messages(messages, timeout=180.0, user=hermes_user)
|
||||
provider = meta.get("provider_label") or "LLM"
|
||||
model = meta.get("model") or ""
|
||||
await _log_event(
|
||||
@@ -139,7 +156,13 @@ async def _chat_via_llm_router(message: str, channel: str) -> dict[str, Any]:
|
||||
"llm_chat",
|
||||
f"Herman via {provider}",
|
||||
message[:2000],
|
||||
{"provider": meta, "channel": channel},
|
||||
{
|
||||
"provider": meta,
|
||||
"channel": channel,
|
||||
"hermes_user": hermes_user,
|
||||
"memory_messages": len(hist),
|
||||
"username": username,
|
||||
},
|
||||
channel=channel,
|
||||
)
|
||||
return {
|
||||
@@ -147,9 +170,16 @@ async def _chat_via_llm_router(message: str, channel: str) -> dict[str, Any]:
|
||||
"agent_label": f"Herman · {provider}",
|
||||
"reply": reply,
|
||||
"delegated_agents": ["herman"],
|
||||
"routing_reason": f"Cloud LLM · {provider}" + (f" · {model}" if model else ""),
|
||||
"agent_steps": _agent_steps([], f"Via {provider}"),
|
||||
"routing_reason": (
|
||||
f"Cloud LLM · {provider}"
|
||||
+ (f" · {model}" if model else "")
|
||||
+ f" · memory:{hermes_user}"
|
||||
+ (f" · feed:{len(hist)}" if hist else "")
|
||||
),
|
||||
"agent_steps": _agent_steps([], f"Via {provider} + Hermes memory"),
|
||||
"llm_provider": meta,
|
||||
"hermes_user": hermes_user,
|
||||
"memory_messages": len(hist),
|
||||
}
|
||||
|
||||
|
||||
@@ -158,6 +188,8 @@ async def chat(
|
||||
channel: str = "cockpit",
|
||||
session_id: str | None = None,
|
||||
confirm_action_id: str | None = None,
|
||||
username: str | None = None,
|
||||
role: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
use_voice_router = (
|
||||
channel in voice_export_actions.VOICE_CHANNELS
|
||||
@@ -382,7 +414,7 @@ async def chat(
|
||||
# Direct LLM backends (Hermes Agent API, OpenRouter, …) — skip offline orchestrator.
|
||||
if backend in ("hermes", "router", "openrouter", "llm"):
|
||||
try:
|
||||
return await _chat_via_llm_router(message, channel)
|
||||
return await _chat_via_llm_router(message, channel, username=username, role=role)
|
||||
except Exception as exc:
|
||||
hint = (
|
||||
"Controleer HERMES_API_KEY / Hermes Agent (:8642)."
|
||||
@@ -427,7 +459,7 @@ async def chat(
|
||||
except Exception as exc:
|
||||
if backend == "auto":
|
||||
try:
|
||||
return await _chat_via_llm_router(message, channel)
|
||||
return await _chat_via_llm_router(message, channel, username=username, role=role)
|
||||
except Exception as llm_exc:
|
||||
return {
|
||||
"agent": "herman",
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Koppel cockpit Herman-chat aan Hermes/Telegram memory."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import settings
|
||||
|
||||
log = logging.getLogger("cockpit.hermes_memory")
|
||||
|
||||
_USERNAME_MAP = {
|
||||
"aissa": "ceo",
|
||||
"aïssa": "ceo",
|
||||
"aissa zarioh": "ceo",
|
||||
"mo": "cto",
|
||||
"mohamed": "cto",
|
||||
}
|
||||
|
||||
|
||||
def resolve_hermes_user(
|
||||
*,
|
||||
username: str | None = None,
|
||||
role: str | None = None,
|
||||
channel: str | None = None,
|
||||
) -> str:
|
||||
"""OpenAI-compatible `user` field = Telegram chat_id for Hermes memory."""
|
||||
uname = (username or "").strip().lower()
|
||||
r = (role or "").strip().lower()
|
||||
mapped = _USERNAME_MAP.get(uname)
|
||||
if mapped == "ceo" or r in ("ceo", "admin") or uname in ("aissa", "aïssa"):
|
||||
return str(settings.HERMES_CEO_CHAT_ID or "8859782446")
|
||||
if mapped == "cto" or r == "cto" or uname in ("mo", "mohamed"):
|
||||
return str(settings.HERMES_CTO_CHAT_ID or "789036463")
|
||||
return str(settings.HERMES_CEO_CHAT_ID or "8859782446")
|
||||
|
||||
|
||||
async def fetch_recent_feed(chat_id: str | int, limit: int | None = None) -> list[dict[str, Any]]:
|
||||
limit = limit or int(getattr(settings, "HERMES_MEMORY_LIMIT", 24) or 24)
|
||||
base = (settings.HERMES_FEED_URL or "").rstrip("/")
|
||||
if not base:
|
||||
return []
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=12.0) as client:
|
||||
r = await client.get(f"{base}/feed", params={"chat_id": int(chat_id), "limit": limit})
|
||||
if r.status_code != 200:
|
||||
return []
|
||||
items = (r.json() or {}).get("items") or []
|
||||
return list(reversed(items))
|
||||
except Exception as exc:
|
||||
log.warning("hermes feed fetch failed: %s", exc)
|
||||
return []
|
||||
|
||||
|
||||
def feed_to_messages(items: list[dict[str, Any]], *, max_chars: int = 12000) -> list[dict[str, str]]:
|
||||
out: list[dict[str, str]] = []
|
||||
total = 0
|
||||
for it in items:
|
||||
text = (it.get("content_text") or "").strip()
|
||||
if not text:
|
||||
continue
|
||||
role_raw = (it.get("role") or "").lower()
|
||||
role = "assistant" if role_raw in ("assistant", "hermes", "herman") else "user"
|
||||
if role == "assistant" and (it.get("direction") or "") == "in":
|
||||
role = "user"
|
||||
entry = {"role": role, "content": text[:2000]}
|
||||
total += len(entry["content"])
|
||||
if total > max_chars:
|
||||
break
|
||||
out.append(entry)
|
||||
return out
|
||||
|
||||
|
||||
def continuity_system(chat_id: str, display_name: str | None = None) -> str:
|
||||
name = display_name or ("Aïssa" if str(chat_id) == str(settings.HERMES_CEO_CHAT_ID) else "de gebruiker")
|
||||
return (
|
||||
"Je bent Herman / Hermes, dezelfde AI Co-CEO als in Telegram. "
|
||||
f"Deze cockpit-chat is gekoppeld aan Telegram-sessie chat_id={chat_id} ({name}). "
|
||||
"Gebruik je bestaande Hermes-memory en recente Telegram-geschiedenis. "
|
||||
"Doe alsof dit dezelfde doorlopende conversatie is — geen nieuw persoon, geen amnesie. "
|
||||
"Antwoord in het Nederlands, bondig en actiegericht."
|
||||
)
|
||||
@@ -255,6 +255,9 @@ async def chat_messages(
|
||||
provider_id: int | None = None,
|
||||
model: str | None = None,
|
||||
timeout: float = 120.0,
|
||||
user: str | None = None,
|
||||
max_tokens: int | None = None,
|
||||
temperature: float | None = None,
|
||||
) -> tuple[str, dict[str, Any]]:
|
||||
"""Returns (reply_text, meta dict with provider info)."""
|
||||
prov = resolve_provider(provider_id)
|
||||
@@ -309,9 +312,14 @@ async def chat_messages(
|
||||
payload: dict[str, Any] = {
|
||||
"model": use_model,
|
||||
"messages": messages,
|
||||
"temperature": float(extra.get("temperature", 0.4)),
|
||||
"max_tokens": int(extra.get("max_tokens", 2048)),
|
||||
"temperature": float(temperature if temperature is not None else extra.get("temperature", 0.4)),
|
||||
"max_tokens": int(max_tokens if max_tokens is not None else extra.get("max_tokens", 2048)),
|
||||
}
|
||||
# Hermes: `user` = Telegram chat_id → koppelt API-chat aan bestaande memory/sessie
|
||||
if user:
|
||||
payload["user"] = str(user)
|
||||
elif ptype == "hermes":
|
||||
payload["user"] = str(getattr(settings, "HERMES_CEO_CHAT_ID", None) or "8859782446")
|
||||
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
resp = await client.post(url, headers=headers, json=payload)
|
||||
@@ -332,9 +340,99 @@ async def chat_messages(
|
||||
"provider_type": ptype,
|
||||
"provider_label": prov.get("label"),
|
||||
"model": use_model,
|
||||
"hermes_user": payload.get("user"),
|
||||
}
|
||||
|
||||
|
||||
|
||||
async def stream_chat_messages(
|
||||
messages: list[dict[str, str]],
|
||||
*,
|
||||
provider_id: int | None = None,
|
||||
model: str | None = None,
|
||||
timeout: float = 90.0,
|
||||
user: str | None = None,
|
||||
max_tokens: int | None = None,
|
||||
temperature: float | None = None,
|
||||
):
|
||||
"""Yield text deltas from Hermes/OpenAI-compatible streaming chat."""
|
||||
prov = resolve_provider(provider_id)
|
||||
ptype = (prov.get("provider_type") or "ollama").lower()
|
||||
use_model = model or prov.get("model") or settings.OLLAMA_MODEL
|
||||
if ptype != "hermes":
|
||||
# non-stream fallback: one chunk
|
||||
reply, _meta = await chat_messages(
|
||||
messages,
|
||||
provider_id=provider_id,
|
||||
model=model,
|
||||
timeout=timeout,
|
||||
user=user,
|
||||
max_tokens=max_tokens,
|
||||
temperature=temperature,
|
||||
)
|
||||
if reply:
|
||||
yield reply
|
||||
return
|
||||
|
||||
api_key = (prov.get("api_key") or "").strip()
|
||||
if not api_key:
|
||||
raise RuntimeError("Geen Hermes API key")
|
||||
base = (prov.get("api_base_url") or "").strip().rstrip("/")
|
||||
if not base:
|
||||
raise RuntimeError("Geen Hermes API URL")
|
||||
extra = prov.get("extra_config") or {}
|
||||
if isinstance(extra, str):
|
||||
try:
|
||||
extra = json.loads(extra)
|
||||
except Exception:
|
||||
extra = {}
|
||||
url = f"{base}/chat/completions"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "text/event-stream",
|
||||
}
|
||||
payload: dict[str, Any] = {
|
||||
"model": use_model,
|
||||
"messages": messages,
|
||||
"temperature": float(temperature if temperature is not None else extra.get("temperature", 0.35)),
|
||||
"max_tokens": int(max_tokens if max_tokens is not None else extra.get("max_tokens", 512)),
|
||||
"stream": True,
|
||||
}
|
||||
if user:
|
||||
payload["user"] = str(user)
|
||||
else:
|
||||
payload["user"] = str(getattr(settings, "HERMES_CEO_CHAT_ID", None) or "8859782446")
|
||||
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
async with client.stream("POST", url, headers=headers, json=payload) as resp:
|
||||
if resp.status_code >= 400:
|
||||
body = (await resp.aread()).decode("utf-8", errors="replace")[:500]
|
||||
raise RuntimeError(f"Hermes stream fout ({resp.status_code}): {body}")
|
||||
async for line in resp.aiter_lines():
|
||||
if not line:
|
||||
continue
|
||||
if line.startswith(":"):
|
||||
continue
|
||||
if not line.startswith("data:"):
|
||||
continue
|
||||
data = line[5:].strip()
|
||||
if not data or data == "[DONE]":
|
||||
if data == "[DONE]":
|
||||
break
|
||||
continue
|
||||
try:
|
||||
obj = json.loads(data)
|
||||
except Exception:
|
||||
continue
|
||||
choices = obj.get("choices") or []
|
||||
if not choices:
|
||||
continue
|
||||
delta = (choices[0].get("delta") or {}).get("content")
|
||||
if delta:
|
||||
yield delta
|
||||
|
||||
|
||||
async def generate(
|
||||
prompt: str,
|
||||
system: str | None = None,
|
||||
|
||||
Reference in New Issue
Block a user