-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
71 lines (59 loc) · 2.09 KB
/
agent.ts
File metadata and controls
71 lines (59 loc) · 2.09 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
/**
* Procurement service agent — TypeScript example.
*
* Validates purchase requests and resumes. Workflow then pauses
* for manager approval.
*
* Usage:
* export AXME_API_KEY="<agent-key>"
* npx tsx agent.ts
*/
import { AxmeClient } from "@axme/axme";
const AGENT_ADDRESS = "procurement-service-demo";
async function handleIntent(client: AxmeClient, intentId: string) {
const intentData = await client.getIntent(intentId);
const intent = intentData.intent ?? intentData;
let payload = intent.payload ?? {};
if (payload.parent_payload) {
payload = payload.parent_payload;
}
const requestId = payload.request_id ?? "unknown";
const amount = payload.amount ?? 0;
const dept = payload.department ?? "unknown";
console.log(` Processing purchase ${requestId}: $${amount} for ${dept}...`);
await new Promise((r) => setTimeout(r, 1000));
console.log(` Validating budget availability...`);
await new Promise((r) => setTimeout(r, 1000));
const result = {
action: "complete",
request_id: requestId,
budget_available: true,
validated_at: new Date().toISOString(),
};
await client.resumeIntent(intentId, result, { ownerAgent: "procurement-service-demo" });
console.log(` Purchase ${requestId} validated. Waiting for manager approval.`);
console.log(` To approve: axme tasks approve <intent_id>`);
}
async function main() {
const apiKey = process.env.AXME_API_KEY;
if (!apiKey) {
console.error("Error: AXME_API_KEY not set.");
process.exit(1);
}
const client = new AxmeClient({ apiKey });
console.log(`Agent listening on ${AGENT_ADDRESS}...`);
console.log("Waiting for intents (Ctrl+C to stop)\n");
for await (const delivery of client.listen(AGENT_ADDRESS)) {
const intentId = delivery.intent_id;
const status = delivery.status;
if (intentId && ["DELIVERED", "CREATED", "IN_PROGRESS"].includes(status)) {
console.log(`[${status}] Intent received: ${intentId}`);
try {
await handleIntent(client, intentId);
} catch (e) {
console.error(` Error: ${e}`);
}
}
}
}
main().catch(console.error);