-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
89 lines (72 loc) · 2.57 KB
/
Copy pathagent.ts
File metadata and controls
89 lines (72 loc) · 2.57 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
/**
* Job processor agent - TypeScript example.
*
* Listens for intents via SSE, processes jobs (invoice generation),
* resumes with generation results.
*
* Usage:
* export AXME_API_KEY="<agent-key>"
* npx tsx agent.ts
*/
import { AxmeClient } from "@axme/axme";
const AGENT_ADDRESS = "inngest-alt-processor-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 jobId = payload.job_id ?? "unknown";
const jobType = payload.job_type ?? "unknown";
const customer = payload.customer ?? "unknown";
const params = payload.params ?? {};
console.log(` Job ID: ${jobId}`);
console.log(` Type: ${jobType}`);
console.log(` Customer: ${customer}`);
// Step 1: Validate job parameters
console.log(` [1/3] Validating job parameters...`);
await new Promise((r) => setTimeout(r, 1000));
// Step 2: Generate invoice
const period = params.period ?? "unknown";
const fmt = params.format ?? "pdf";
console.log(` [2/3] Generating ${fmt} invoice for ${period}...`);
await new Promise((r) => setTimeout(r, 2000));
// Step 3: Upload artifact
console.log(` [3/3] Uploading invoice artifact...`);
await new Promise((r) => setTimeout(r, 1000));
const result = {
action: "complete",
job_id: jobId,
invoice_url: `https://storage.example.com/invoices/${customer}/${period}.${fmt}`,
pages: 12,
generated_at: new Date().toISOString(),
};
await client.resumeIntent(intentId, result, { ownerAgent: AGENT_ADDRESS });
console.log(` Invoice generated: ${result.invoice_url}`);
console.log(` Pages: ${result.pages}`);
}
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) continue;
if (["DELIVERED", "CREATED", "IN_PROGRESS"].includes(status)) {
console.log(`[${status}] Intent received: ${intentId}`);
try {
await handleIntent(client, intentId);
} catch (e) {
console.error(` Error processing intent: ${e}`);
}
}
}
}
main().catch(console.error);