Orchestration : skill DELEGATE + registre agents + system prompt chef d'orchestre
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
Skill : DELEGATE
|
||||
Délègue une tâche à un agent spécialisé via MQTT et attend sa réponse.
|
||||
|
||||
Commande :
|
||||
DELEGATE: <agent_name> | <tâche>
|
||||
|
||||
Exemple :
|
||||
DELEGATE: agent2_debian13 | Comment installer Docker sur Debian 13 ?
|
||||
"""
|
||||
import json
|
||||
import time
|
||||
import threading
|
||||
from pathlib import Path
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
SKILL_NAME = "delegate"
|
||||
TRIGGER = "DELEGATE:"
|
||||
|
||||
CONFIG_FILE = Path("/opt/agent/config/config.json")
|
||||
REGISTRY_FILE = Path("/opt/agent/config/agents_registry.json")
|
||||
TIMEOUT = 120 # secondes max d'attente
|
||||
|
||||
def _load():
|
||||
cfg = json.loads(CONFIG_FILE.read_text())
|
||||
registry = json.loads(REGISTRY_FILE.read_text())
|
||||
return cfg, registry
|
||||
|
||||
def execute(args: str) -> str:
|
||||
if "|" not in args:
|
||||
return "Erreur : format attendu → DELEGATE: <agent> | <tâche>"
|
||||
|
||||
agent_name, _, task = args.partition("|")
|
||||
agent_name, task = agent_name.strip(), task.strip()
|
||||
|
||||
cfg, registry = _load()
|
||||
if agent_name not in registry:
|
||||
available = ", ".join(registry.keys())
|
||||
return "Agent inconnu : «{}». Agents disponibles : {}".format(agent_name, available)
|
||||
|
||||
agent = registry[agent_name]
|
||||
inbox = agent["mqtt_inbox"]
|
||||
outbox = agent["mqtt_outbox"]
|
||||
host = cfg.get("mqtt_host", "localhost")
|
||||
port = int(cfg.get("mqtt_port", 1883))
|
||||
|
||||
response_received = threading.Event()
|
||||
response_container = []
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
response_container.append(msg.payload.decode(errors="replace"))
|
||||
response_received.set()
|
||||
|
||||
# Souscription à la réponse
|
||||
sub = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="agent1_delegate_sub")
|
||||
sub.on_message = on_message
|
||||
sub.connect(host, port)
|
||||
sub.subscribe(outbox)
|
||||
sub.loop_start()
|
||||
|
||||
# Envoi de la tâche
|
||||
pub = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="agent1_delegate_pub")
|
||||
pub.connect(host, port)
|
||||
pub.publish(inbox, task)
|
||||
pub.disconnect()
|
||||
|
||||
print("[DELEGATE] Tâche envoyée à {} : {}".format(agent_name, task[:80]))
|
||||
|
||||
# Attente de la réponse
|
||||
received = response_received.wait(timeout=TIMEOUT)
|
||||
sub.loop_stop()
|
||||
sub.disconnect()
|
||||
|
||||
if received and response_container:
|
||||
return "[{}] {}".format(agent_name, response_container[0])
|
||||
return "Timeout : {} n'a pas répondu dans les {}s.".format(agent_name, TIMEOUT)
|
||||
Reference in New Issue
Block a user