Load a workflow definition from a TOML file.
Expected format:
[workflow]
name = "my_workflow"
[[workflow.nodes]]
id = "researcher"
type = "agent"
agent = "orchestrator"
tools = ["web_search"]
[[workflow.nodes]]
id = "summarizer"
type = "agent"
agent = "simple"
[[workflow.edges]]
source = "researcher"
target = "summarizer"
Source code in src/openjarvis/workflow/loader.py
| def load_workflow(path: str | Path) -> WorkflowGraph:
"""Load a workflow definition from a TOML file.
Expected format:
```toml
[workflow]
name = "my_workflow"
[[workflow.nodes]]
id = "researcher"
type = "agent"
agent = "orchestrator"
tools = ["web_search"]
[[workflow.nodes]]
id = "summarizer"
type = "agent"
agent = "simple"
[[workflow.edges]]
source = "researcher"
target = "summarizer"
```
"""
path = Path(path)
with open(path, "rb") as fh:
data = tomllib.load(fh)
wf_data = data.get("workflow", {})
name = wf_data.get("name", path.stem)
graph = WorkflowGraph(name=name)
for node_data in wf_data.get("nodes", []):
node = _parse_node(node_data)
graph.add_node(node)
for edge_data in wf_data.get("edges", []):
edge = WorkflowEdge(
source=edge_data["source"],
target=edge_data["target"],
condition=edge_data.get("condition", ""),
)
graph.add_edge(edge)
valid, msg = graph.validate()
if not valid:
raise ValueError(f"Invalid workflow in {path}: {msg}")
return graph
|