-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
63 lines (56 loc) · 1.84 KB
/
Copy pathserver.ts
File metadata and controls
63 lines (56 loc) · 1.84 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
/**
* heartbeat — server.
*
* Demonstrates the v1.1 heartbeat keepalive. The runtime advertises a
* 5-second `heartbeat_interval_sec` in `session.welcome` and emits
* `session.ping` envelopes on that cadence (a much shorter interval
* than the 30 s default, so the demo finishes quickly).
*
* Hosts a trivial `echo` agent; the demo doesn't require running jobs
* to observe heartbeats, but having an agent lets the client verify a
* normal job round-trip too.
*/
import {
ARCPServer,
StaticBearerVerifier,
startWebSocketServer,
} from "@agentruntimecontrolprotocol/sdk";
const PORT = Number(process.env["ARCP_DEMO_PORT"] ?? 7885);
const TOKEN = process.env["ARCP_DEMO_TOKEN"] ?? "demo-token";
async function main(): Promise<void> {
const server = new ARCPServer({
runtime: { name: "heartbeat-demo", version: "1.0.0" },
capabilities: {
encodings: ["json"],
agents: ["echo"],
},
bearer: new StaticBearerVerifier(new Map([[TOKEN, { principal: "demo" }]])),
// Five-second cadence so the demo is observable in a handful of
// seconds. Production deployments default to 30 s.
heartbeatIntervalSeconds: 5,
});
server.registerAgent("echo", async (input) => input);
const wss = await startWebSocketServer({
host: "127.0.0.1",
port: PORT,
onTransport: (t) => {
server.accept(t);
},
});
console.log(`ARCP runtime listening on ${wss.url}`);
console.log(`heartbeat_interval_sec=5`);
console.log(`Token: ${TOKEN}`);
console.log("Press Ctrl+C to stop.");
const shutdown = async (): Promise<void> => {
console.log("\nshutting down...");
await wss.close();
await server.close();
process.exit(0);
};
process.on("SIGINT", () => void shutdown());
process.on("SIGTERM", () => void shutdown());
}
void main().catch((err) => {
console.error(err);
process.exit(1);
});