|
| 1 | +""" |
| 2 | +Minimal Manual Agent Example |
| 3 | +---------------------------- |
| 4 | +This example demonstrates how to build and run an agent programmatically |
| 5 | +without using the Claude Code CLI or external LLM APIs. |
| 6 | +
|
| 7 | +It uses 'function' nodes to define logic in pure Python, making it perfect |
| 8 | +for understanding the core runtime loop: |
| 9 | +Setup -> Graph definition -> Execution -> Result |
| 10 | +
|
| 11 | +Run with: |
| 12 | + PYTHONPATH=core python core/examples/manual_agent.py |
| 13 | +""" |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import logging |
| 17 | +from framework.graph import Goal, NodeSpec, EdgeSpec, GraphSpec, EdgeCondition |
| 18 | +from framework.graph.executor import GraphExecutor |
| 19 | +from framework.runtime.core import Runtime |
| 20 | + |
| 21 | +# 1. Define Node Logic (Pure Python Functions) |
| 22 | +def greet(name: str) -> str: |
| 23 | + """Generate a simple greeting.""" |
| 24 | + return f"Hello, {name}!" |
| 25 | + |
| 26 | +def uppercase(greeting: str) -> str: |
| 27 | + """Convert text to uppercase.""" |
| 28 | + return greeting.upper() |
| 29 | + |
| 30 | +async def main(): |
| 31 | + print("🚀 Setting up Manual Agent...") |
| 32 | + |
| 33 | + # 2. Define the Goal |
| 34 | + # Every agent needs a goal with success criteria |
| 35 | + goal = Goal( |
| 36 | + id="greet-user", |
| 37 | + name="Greet User", |
| 38 | + description="Generate a friendly uppercase greeting", |
| 39 | + success_criteria=[ |
| 40 | + { |
| 41 | + "id": "greeting_generated", |
| 42 | + "description": "Greeting produced", |
| 43 | + "metric": "custom", |
| 44 | + "target": "any" |
| 45 | + } |
| 46 | + ] |
| 47 | + ) |
| 48 | + |
| 49 | + # 3. Define Nodes |
| 50 | + # Nodes describe steps in the process |
| 51 | + node1 = NodeSpec( |
| 52 | + id="greeter", |
| 53 | + name="Greeter", |
| 54 | + description="Generates a simple greeting", |
| 55 | + node_type="function", |
| 56 | + function="greet", # Matches the registered function name |
| 57 | + input_keys=["name"], |
| 58 | + output_keys=["greeting"] |
| 59 | + ) |
| 60 | + |
| 61 | + node2 = NodeSpec( |
| 62 | + id="uppercaser", |
| 63 | + name="Uppercaser", |
| 64 | + description="Converts greeting to uppercase", |
| 65 | + node_type="function", |
| 66 | + function="uppercase", |
| 67 | + input_keys=["greeting"], |
| 68 | + output_keys=["final_greeting"] |
| 69 | + ) |
| 70 | + |
| 71 | + # 4. Define Edges |
| 72 | + # Edges define the flow between nodes |
| 73 | + edge1 = EdgeSpec( |
| 74 | + id="greet-to-upper", |
| 75 | + source="greeter", |
| 76 | + target="uppercaser", |
| 77 | + condition=EdgeCondition.ON_SUCCESS |
| 78 | + ) |
| 79 | + |
| 80 | + # 5. Create Graph |
| 81 | + # The graph works like a blueprint connecting nodes and edges |
| 82 | + graph = GraphSpec( |
| 83 | + id="greeting-agent", |
| 84 | + goal_id="greet-user", |
| 85 | + entry_node="greeter", |
| 86 | + terminal_nodes=["uppercaser"], |
| 87 | + nodes=[node1, node2], |
| 88 | + edges=[edge1], |
| 89 | + ) |
| 90 | + |
| 91 | + # 6. Initialize Runtime & Executor |
| 92 | + # Runtime handles state/memory; Executor runs the graph |
| 93 | + from pathlib import Path |
| 94 | + runtime = Runtime(storage_path=Path("./agent_logs")) |
| 95 | + executor = GraphExecutor(runtime=runtime) |
| 96 | + |
| 97 | + # 7. Register Function Implementations |
| 98 | + # Connect string names in NodeSpecs to actual Python functions |
| 99 | + executor.register_function("greeter", greet) |
| 100 | + executor.register_function("uppercaser", uppercase) |
| 101 | + |
| 102 | + # 8. Execute Agent |
| 103 | + print(f"▶ Executing agent with input: name='Alice'...") |
| 104 | + |
| 105 | + result = await executor.execute( |
| 106 | + graph=graph, |
| 107 | + goal=goal, |
| 108 | + input_data={"name": "Alice"} |
| 109 | + ) |
| 110 | + |
| 111 | + # 9. Verify Results |
| 112 | + if result.success: |
| 113 | + print("\n✅ Success!") |
| 114 | + print(f"Path taken: {' -> '.join(result.path)}") |
| 115 | + print(f"Final output: {result.output.get('final_greeting')}") |
| 116 | + else: |
| 117 | + print(f"\n❌ Failed: {result.error}") |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + # Optional: Enable logging to see internal decision flow |
| 121 | + # logging.basicConfig(level=logging.INFO) |
| 122 | + asyncio.run(main()) |
0 commit comments