-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
53 lines (45 loc) · 1.88 KB
/
agent.py
File metadata and controls
53 lines (45 loc) · 1.88 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
"""Procurement service agent - validates purchase and resumes."""
import os, sys, time
sys.stdout.reconfigure(line_buffering=True)
from axme import AxmeClient, AxmeClientConfig
AGENT_ADDRESS = "procurement-service-demo"
def handle_intent(client, intent_id):
intent_data = client.get_intent(intent_id)
intent = intent_data.get("intent", intent_data)
payload = intent.get("payload", {})
if "parent_payload" in payload:
payload = payload["parent_payload"]
request_id = payload.get("request_id", "unknown")
amount = payload.get("amount", 0)
dept = payload.get("department", "unknown")
print(f" Processing purchase {request_id}: ${amount} for {dept}...")
time.sleep(1)
print(f" Validating budget availability...")
time.sleep(1)
result = {
"action": "complete",
"request_id": request_id,
"budget_available": True,
"validated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}
client.resume_intent(intent_id, result)
print(f" Purchase {request_id} validated. Waiting for manager approval.")
print(f" To approve: axme tasks approve <intent_id>")
def main():
api_key = os.environ.get("AXME_API_KEY", "")
if not api_key:
print("Error: AXME_API_KEY not set."); sys.exit(1)
client = AxmeClient(AxmeClientConfig(api_key=api_key))
print(f"Agent listening on {AGENT_ADDRESS}...")
print("Waiting for intents (Ctrl+C to stop)\n")
for delivery in client.listen(AGENT_ADDRESS):
intent_id = delivery.get("intent_id", "")
status = delivery.get("status", "")
if intent_id and status in ("DELIVERED", "CREATED", "IN_PROGRESS"):
print(f"[{status}] Intent received: {intent_id}")
try:
handle_intent(client, intent_id)
except Exception as e:
print(f" Error: {e}")
if __name__ == "__main__":
main()