feat(sidechats): headless gateway new channel auto-provisioning and dedicated heartbeat channel
This commit is contained in:
@@ -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:
|
||||
|
||||
+47
-4
@@ -455,14 +455,57 @@ def main():
|
||||
target = p_entry.get("target") or f"pipe-{pipeline_run_id.split('-')[-1]}"
|
||||
else:
|
||||
target = f"pipe-{pipeline_run_id.split('-')[-1]}"
|
||||
elif job.get("sidechat", {}).get("create"):
|
||||
sc_cfg = job.get("sidechat", {})
|
||||
sc_name = render_prompt(sc_cfg.get("name_template", "job-{job_name}-{date}"), variables)
|
||||
reuse_key = sc_cfg.get("reuse_key")
|
||||
|
||||
# Check if reuse_key exists in job-sidechats.json and thread is still alive
|
||||
sc_state = load_sidechat_state()
|
||||
reused_uuid = None
|
||||
if reuse_key and reuse_key in sc_state:
|
||||
val = sc_state[reuse_key]
|
||||
cand_uuid = val.get("thread_uuid") if isinstance(val, dict) else val
|
||||
if cand_uuid:
|
||||
try:
|
||||
import muse_hybrid
|
||||
threads, err = muse_hybrid.get_threads(agent)
|
||||
if not err and threads:
|
||||
thread_ids = [t.get("session_id") for t in threads]
|
||||
if cand_uuid in thread_ids:
|
||||
reused_uuid = cand_uuid
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if reused_uuid:
|
||||
target = reused_uuid
|
||||
else:
|
||||
# Spawn a brand new sidechat/channel via fast headless gateway!
|
||||
channel_title = sc_name
|
||||
try:
|
||||
import muse_hybrid
|
||||
res, err = muse_hybrid.start_session(agent, title=channel_title)
|
||||
if res and not err and res.get("session_id"):
|
||||
new_uuid = res.get("session_id")
|
||||
key_to_save = reuse_key or sc_name
|
||||
sc_state[key_to_save] = {
|
||||
"thread_uuid": new_uuid,
|
||||
"agent": agent,
|
||||
"title": channel_title,
|
||||
"created_at": datetime.now(timezone.utc).isoformat()
|
||||
}
|
||||
save_sidechat_state(sc_state)
|
||||
target = new_uuid
|
||||
print(f"Spawned new sidechat channel '{channel_title}' ({new_uuid}) for {agent}")
|
||||
else:
|
||||
target = sc_name
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"warning: fast gateway session-start exception ({e}), falling back to name {sc_name}\n")
|
||||
target = sc_name
|
||||
elif job.get("dm_target"):
|
||||
target = job.get("dm_target").strip()
|
||||
elif job.get("target"):
|
||||
target = job.get("target").strip()
|
||||
elif job.get("sidechat", {}).get("create"):
|
||||
sc_cfg = job.get("sidechat", {})
|
||||
sc_name = render_prompt(sc_cfg.get("name_template", "job-{job_name}-{date}"), variables)
|
||||
target = sc_name
|
||||
|
||||
# Sidechat-first policy (2026-10-04): refuse to dispatch to main chat
|
||||
# unless the job explicitly opts in. Never fall back to main silently.
|
||||
|
||||
Reference in New Issue
Block a user