-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
67 lines (55 loc) · 1.88 KB
/
agent.ts
File metadata and controls
67 lines (55 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Report API agent — TypeScript example.
*
* Generates financial reports and resumes with result.
*
* Usage:
* export AXME_API_KEY="<agent-key>"
* npx tsx agent.ts
*/
import { AxmeClient } from "@axme/axme";
const AGENT_ADDRESS = "report-api-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 reportType = payload.report_type ?? "unknown";
const fmt = payload.format ?? "pdf";
const year = payload.fiscal_year ?? "2025";
console.log(` Generating ${reportType} report (${fmt}) for FY${year}...`);
await new Promise((r) => setTimeout(r, 2000));
const result = {
action: "complete",
report_url: `https://reports.example.com/FY${year}-${reportType}.${fmt}`,
pages: 128,
generated_at: new Date().toISOString(),
};
await client.resumeIntent(intentId, result, { ownerAgent: "report-api-demo" });
console.log(` Report ready: ${result.report_url}`);
}
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);