feat(maf): add secure roundtable workflow runtime
This commit is contained in:
1 parent
e690475e6e
commit
bd4131b4e2
52 files changed
+10315
-96
No files matched your search
@@ -0,0 +1,139 @@
|
||||
"""MAF WorkflowBuilder graph construction for the roundtable experiment.
|
||||
|
||||
Creates real MAF Workflow graphs using the official SDK's WorkflowBuilder.
|
||||
The SDK is imported lazily so the Gateway starts without it when the feature
|
||||
flag is off.
|
||||
|
||||
Two topologies:
|
||||
- ``structured``: coordinator → parallel seats → approval gate → finalizer
|
||||
- ``group_chat``: coordinator loop (dynamic speaker selection) → finalizer
|
||||
|
||||
Each node is a real MAF ``Executor`` subclass instance (from
|
||||
``workflow_executors.py``). The builder uses the SDK's actual API:
|
||||
``WorkflowBuilder(start_executor=...)``, ``add_edge``, ``add_fan_out_edges``,
|
||||
``add_fan_in_edges``, ``add_switch_case_edge_group``, ``build()``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def build_structured_workflow(
|
||||
*,
|
||||
coordinator: Any,
|
||||
seat_executors: list[Any],
|
||||
approval_gate: Any,
|
||||
finalizer: Any,
|
||||
waiting_terminal: Any,
|
||||
) -> Any:
|
||||
"""Build a structured (fixed-topology) MAF Workflow.
|
||||
|
||||
Topology: coordinator → parallel seat executors → approval gate → finalizer.
|
||||
The approval gate conditionally routes to waiting_terminal (pause) or finalizer.
|
||||
"""
|
||||
from agent_framework import Case, Default, WorkflowBuilder
|
||||
|
||||
builder = WorkflowBuilder(
|
||||
start_executor=coordinator,
|
||||
name="maf-structured-roundtable",
|
||||
output_from=[finalizer, waiting_terminal],
|
||||
)
|
||||
|
||||
# coordinator → seats → approval gate. Handle 0/1/≥2 seats since the SDK's
|
||||
# FanOut/FanIn edge groups require at least two members.
|
||||
if len(seat_executors) >= 2:
|
||||
builder.add_fan_out_edges(coordinator, seat_executors)
|
||||
builder.add_fan_in_edges(seat_executors, approval_gate)
|
||||
elif len(seat_executors) == 1:
|
||||
builder.add_edge(coordinator, seat_executors[0])
|
||||
builder.add_edge(seat_executors[0], approval_gate)
|
||||
else:
|
||||
# No seats — coordinator feeds the approval gate directly.
|
||||
builder.add_edge(coordinator, approval_gate)
|
||||
|
||||
# approval gate → conditional: approved → finalizer, waiting → waiting_terminal
|
||||
builder.add_switch_case_edge_group(approval_gate, [
|
||||
Case(condition=lambda out: isinstance(out, dict) and out.get("decision") == "waiting_approval", target=waiting_terminal),
|
||||
Default(target=finalizer),
|
||||
])
|
||||
|
||||
return builder.build()
|
||||
|
||||
|
||||
def build_group_chat_workflow(
|
||||
*,
|
||||
coordinator: Any,
|
||||
seat_executors: list[Any],
|
||||
approval_gate: Any,
|
||||
finalizer: Any,
|
||||
waiting_terminal: Any,
|
||||
seat_agent_ids: list[str],
|
||||
) -> Any:
|
||||
"""Build a dynamic group-chat MAF Workflow.
|
||||
|
||||
The coordinator selects the next speaker each round via switch-case edges.
|
||||
Loop terminates on: coordinator signals completion, max_rounds reached.
|
||||
"""
|
||||
from agent_framework import Case, Default, WorkflowBuilder
|
||||
|
||||
builder = WorkflowBuilder(
|
||||
start_executor=coordinator,
|
||||
name="maf-groupchat-roundtable",
|
||||
output_from=[finalizer, waiting_terminal],
|
||||
)
|
||||
|
||||
# coordinator → switch-case: route to the selected seat, or to approval gate
|
||||
cases = []
|
||||
for i, agent_id in enumerate(seat_agent_ids):
|
||||
cases.append(Case(
|
||||
condition=lambda out, aid=agent_id: isinstance(out, dict) and out.get("agent_id") == aid,
|
||||
target=seat_executors[i],
|
||||
))
|
||||
# Default: coordinator said "done" → go to approval gate
|
||||
cases.append(Default(target=approval_gate))
|
||||
builder.add_switch_case_edge_group(coordinator, cases)
|
||||
|
||||
# each seat → back to coordinator (loop)
|
||||
for seat_exec in seat_executors:
|
||||
builder.add_edge(seat_exec, coordinator)
|
||||
|
||||
# approval gate → conditional: waiting → waiting_terminal, else → finalizer
|
||||
builder.add_switch_case_edge_group(approval_gate, [
|
||||
Case(condition=lambda out: isinstance(out, dict) and out.get("decision") == "waiting_approval", target=waiting_terminal),
|
||||
Default(target=finalizer),
|
||||
])
|
||||
|
||||
return builder.build()
|
||||
|
||||
|
||||
def create_workflow(
|
||||
*,
|
||||
mode: str,
|
||||
coordinator: Any,
|
||||
seat_executors: list[Any],
|
||||
approval_gate: Any,
|
||||
finalizer: Any,
|
||||
waiting_terminal: Any,
|
||||
seat_agent_ids: list[str],
|
||||
) -> Any:
|
||||
"""Factory: create the appropriate workflow for the given mode."""
|
||||
if mode == "group_chat":
|
||||
return build_group_chat_workflow(
|
||||
coordinator=coordinator,
|
||||
seat_executors=seat_executors,
|
||||
approval_gate=approval_gate,
|
||||
finalizer=finalizer,
|
||||
waiting_terminal=waiting_terminal,
|
||||
seat_agent_ids=seat_agent_ids,
|
||||
)
|
||||
return build_structured_workflow(
|
||||
coordinator=coordinator,
|
||||
seat_executors=seat_executors,
|
||||
approval_gate=approval_gate,
|
||||
finalizer=finalizer,
|
||||
waiting_terminal=waiting_terminal,
|
||||
)
|
||||
Reference in new issue
Block a user