Repository navigation
Expand file tree
/
Copy path07_refund_workflow.py
More file actions
108 lines (85 loc) · 3.11 KB
/
Copy path07_refund_workflow.py
File metadata and controls
108 lines (85 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
================================================================
EXAMPLE 07 — "Customer support, real-world rules" (ADVANCED)
================================================================
SCENARIO:
Three customers, three policy outcomes:
- C-789 (fraud watchlist): DENY
- C-456 (medium refund): APPROVE_REQUIRED (auto-approved here)
- C-123 (small refund): ALLOW
Each is a separate run_agent() call — no shared state between runs.
RUN WITH:
python examples/07_refund_workflow.py
"""
from __future__ import annotations
import asyncio
from pathlib import Path
from lynx import (
FinalAnswer,
Message,
ToolCall,
ToolSet,
auto_approve,
load_policy_file,
run_agent,
stdout_sink,
tool,
)
CUSTOMERS = {
"C-123": {"name": "Alice"},
"C-456": {"name": "Bob"},
"C-789": {"name": "Carol"}, # on fraud watchlist
}
@tool(reversible=True, scope=("customer:read",))
async def get_customer(customer_id: str) -> dict:
return CUSTOMERS.get(customer_id, {"error": "not found"})
@tool(reversible=False, scope=("customer:write", "money:transfer"))
async def refund_customer(customer_id: str, amount_usd: float, reason: str) -> dict:
return {"refunded": amount_usd, "to": customer_id, "reason": reason, "txn": "TXN-XYZ"}
@refund_customer.shadow
async def _refund_shadow(customer_id: str, amount_usd: float, reason: str) -> dict:
return {"would_refund": amount_usd, "to": customer_id}
class RefundAgent:
SCENARIOS = {
"C-789": (5000.0, "customer demanded compensation"),
"C-456": (200.0, "month-long outage credit"),
"C-123": (1.63, "1-day outage refund"),
}
def __init__(self, customer_id: str):
amount, reason = self.SCENARIOS[customer_id]
self._i = 0
self._plan = [
ToolCall("get_customer", {"customer_id": customer_id}, call_id="c1"),
ToolCall(
"refund_customer",
{"customer_id": customer_id, "amount_usd": amount, "reason": reason},
call_id="c2",
),
FinalAnswer(text=f"Processed ticket for {customer_id}."),
]
async def step(self, conv: tuple[Message, ...]):
a = self._plan[self._i]
self._i += 1
return a
async def run_one(customer_id: str, tools, policy) -> None:
name = CUSTOMERS[customer_id]["name"]
print(f"\n=== Ticket for {customer_id} ({name}) ===")
result = await run_agent(
RefundAgent(customer_id),
task=f"Process refund for {customer_id}",
tools=tools,
policy=policy,
sinks=(stdout_sink(),),
on_approval=auto_approve(approver="supervisor-auto"),
)
print(f" Final: {result.final_answer}")
if result.error:
print(f" Error: {result.error}")
async def main() -> None:
policy_path = Path(__file__).resolve().parent / "policies" / "refund.yaml"
policy = load_policy_file(policy_path)
tools = ToolSet.from_functions(get_customer, refund_customer)
for cid in ("C-789", "C-456", "C-123"):
await run_one(cid, tools, policy)
if __name__ == "__main__":
asyncio.run(main())