feat(sidechats): headless gateway new channel auto-provisioning and dedicated heartbeat channel

This commit is contained in:
operator
2026-10-05 02:27:23 +00:00
parent 9f27dbd592
commit ec547b28ac
7 changed files with 200 additions and 14 deletions
+54
View File
@@ -598,6 +598,60 @@ def dm_send(agent, target, message, verify=True, raw=False,
except Exception as _e:
log_event({"type": "gateway_fallback", "id": msg_id, "error": str(_e)[:100]})
# Fast gateway auto-provisioning for named sidechat targets:
if not is_uuid and recipient in VALID_AGENTS and target != "main":
try:
import muse_hybrid
matched_uuid = None
gw_threads, gw_t_err = muse_hybrid.get_threads(recipient)
if not gw_t_err and gw_threads:
for t in gw_threads:
t_title = (t.get("title") or "").strip().lower()
if t_title == target.strip().lower():
matched_uuid = t.get("session_id")
break
if not matched_uuid:
# Spawn a brand new sidechat channel via fast headless gateway
res, err = muse_hybrid.start_session(recipient, title=target)
if res and not err and res.get("session_id"):
matched_uuid = res.get("session_id")
log_event({"type": "sidechat_autoprovisioned", "id": msg_id, "target": target,
"thread_uuid": matched_uuid, "agent": recipient, "transport": "gateway"})
if matched_uuid:
# Save mapping to job-sidechats.json
sc_data = load_sidechat_map()
save_key = f"{target}@{recipient}" if f"{target}@{recipient}" in sc_data else target
sc_data[save_key] = {
"thread_uuid": matched_uuid,
"agent": recipient,
"title": target,
"created_at": datetime.now(timezone.utc).isoformat()
}
tmp_sc = f"{SIDCHAT_MAP_FILE}.tmp.{os.getpid()}"
with open(tmp_sc, "w", encoding="utf-8") as f:
json.dump(sc_data, f, indent=2)
os.replace(tmp_sc, SIDCHAT_MAP_FILE)
# Send immediately via gateway
gw_res, gw_err = muse_hybrid.send_message(recipient, tagged, thread_id=matched_uuid, wait=0)
if gw_res and not gw_err:
thread_uuid = matched_uuid
tags["thread"] = thread_uuid
log_event({"type": "verified", "id": msg_id, "agent": agent, "to": recipient,
"target": target, "thread_uuid": thread_uuid, "transport": "gateway",
"placement": "confirmed"})
log_event({"type": "send_done", "id": msg_id, "agent": agent, "to": recipient, "target": target})
log_event({"type": "sent", "id": msg_id, "agent": agent, "to": recipient, "target": target,
"verified": True, "transport": "gateway", "tags": tags})
print(f"DM {msg_id} from {agent} to {recipient}/{target}: SENT and VERIFIED thread={thread_uuid} (gateway, auto-provisioned)")
if tags.get("reply:expected"):
_register_followup(msg_id, agent, recipient, target, tags)
return msg_id
except Exception as _ge:
log_event({"type": "gateway_autoprovision_fallback", "id": msg_id, "error": str(_ge)[:100]})
if target == "main":
_rc, _out, _err = run_full(f"{NETVM_EXEC} {recipient} -- python3 {API} --account {recipient} sidechat main")
else: