Files
orchestration_engine/orchestration/agents.py

69 lines
2.3 KiB
Python

"""The agents the Orchestration engine routes to, and the sequences of them, from
configuration."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import yaml
@dataclass(frozen=True)
class Agent:
"""One kind of work, and the agent that does it."""
work: str # what a caller names: "operating_model", "solution_architecture"
url: str # the agent's base URL; its card says where its A2A interface is
description: str = ""
@dataclass(frozen=True)
class Sequence:
"""Work done by agents in turn: each one's files are the next one's sources."""
work: str # what a caller names: "architecture_and_operating_model"
steps: tuple[Agent, ...]
description: str = ""
def load_routes(path: str | Path) -> tuple[list[Agent], list[Sequence]]:
"""The agents and the sequences in `path`, in the order written. One agent or
sequence per kind of work; a sequence names only agents configured here."""
raw = yaml.safe_load(Path(path).read_text(encoding="utf-8")) or {}
agents = [
Agent(
work=str(entry["work"]),
url=str(entry["url"]).rstrip("/"),
description=str(entry.get("description") or ""),
)
for entry in raw.get("agents") or []
]
by_work = {agent.work: agent for agent in agents}
sequences: list[Sequence] = []
for entry in raw.get("sequences") or []:
unknown = [str(step) for step in entry["steps"] if str(step) not in by_work]
if unknown:
raise ValueError(
f"The sequence {entry['work']!r} names no configured agent for "
+ ", ".join(repr(step) for step in unknown)
)
sequences.append(
Sequence(
work=str(entry["work"]),
steps=tuple(by_work[str(step)] for step in entry["steps"]),
description=str(entry.get("description") or ""),
)
)
seen: set[str] = set()
for work in [*(a.work for a in agents), *(s.work for s in sequences)]:
if work in seen:
raise ValueError(f"Two agents are configured for the work {work!r}")
seen.add(work)
return agents, sequences
def load_agents(path: str | Path) -> list[Agent]:
"""The agents in `path`, without its sequences."""
return load_routes(path)[0]