-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
56 lines (47 loc) · 1.63 KB
/
client.ts
File metadata and controls
56 lines (47 loc) · 1.63 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
/**
* Express example — client.
*
* Hits the HTTP `/health` route and then submits an ARCP job to the same
* server, to demonstrate the two co-exist on one port.
*/
import { ARCPClient, WebSocketTransport } from "@arcp/sdk";
const PORT = Number(process.env.ARCP_DEMO_PORT ?? 7896);
const URL = process.env.ARCP_DEMO_URL ?? `ws://127.0.0.1:${PORT}/arcp`;
const TOKEN = process.env.ARCP_DEMO_TOKEN ?? "demo-token";
async function main(): Promise<void> {
// HTTP first — same port.
const httpRes = await fetch(`http://127.0.0.1:${PORT}/health`);
const healthBody = (await httpRes.json()) as { status: string; arcp: string };
process.stdout.write(
`GET /health → ${httpRes.status} ${JSON.stringify(healthBody)}\n`,
);
// ARCP next — same port.
const client = new ARCPClient({
client: { name: "express-demo-client", version: "1.0.0" },
capabilities: { encodings: ["json"] },
authScheme: "bearer",
token: TOKEN,
});
client.on("job.event", (env) => {
if (env.type !== "job.event") return;
process.stdout.write(
`event[seq=${env.event_seq}] ${env.payload.kind} ` +
`${JSON.stringify(env.payload.body)}\n`,
);
});
const transport = await WebSocketTransport.connect(URL);
await client.connect(transport);
const handle = await client.submit({
agent: "echo",
input: { msg: "hello over express" },
});
const result = await handle.done;
process.stdout.write(`result: ${JSON.stringify(result)}\n`);
await client.close();
}
void main().catch((err) => {
process.stderr.write(
`client failed: ${err instanceof Error ? err.stack : String(err)}\n`,
);
process.exit(1);
});