59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
"""Which agents a request is for (ADR-0003). It never guesses.
|
|
|
|
In order: the work the caller named, an agent's or a sequence's; the only agent,
|
|
when there is one and no sequence; otherwise none, with how to name the work.
|
|
Reading a request by Jev is step F3, added to these rules rather than in place of
|
|
them.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from collections.abc import Sequence as Seq
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
from orchestration.agents import Agent, Sequence
|
|
|
|
WORK = "work" # the metadata field a caller names the work in
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Decision:
|
|
agents: tuple[Agent, ...] # in the order they work; empty when none
|
|
reason: str
|
|
|
|
@property
|
|
def agent(self) -> Agent | None:
|
|
"""The first agent, or None when nothing is to start."""
|
|
return self.agents[0] if self.agents else None
|
|
|
|
|
|
def route(
|
|
options: Mapping[str, Any],
|
|
agents: Seq[Agent],
|
|
sequences: Seq[Sequence] = (),
|
|
) -> Decision:
|
|
named = options.get(WORK)
|
|
offered = ", ".join([*(a.work for a in agents), *(s.work for s in sequences)])
|
|
if named:
|
|
for agent in agents:
|
|
if agent.work == named:
|
|
return Decision((agent,), f"The caller named the work {named!r}.")
|
|
for sequence in sequences:
|
|
if sequence.work == named:
|
|
return Decision(
|
|
sequence.steps, f"The caller named the sequence {named!r}."
|
|
)
|
|
return Decision(
|
|
(),
|
|
f"No agent here does the work {named!r}. The work it can name: {offered}.",
|
|
)
|
|
if len(agents) == 1 and not sequences:
|
|
return Decision((agents[0],), "It is the only agent.")
|
|
return Decision(
|
|
(),
|
|
"Nothing was started: name the work this is, as `work` in the request's "
|
|
f"metadata. The work it can name: {offered}.",
|
|
)
|