-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
75 lines (67 loc) · 2.15 KB
/
server.ts
File metadata and controls
75 lines (67 loc) · 2.15 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
/**
* subscribe — server.
*
* Hosts a `timer` agent that emits a status, a few logs, then an
* artifact_ref before returning. The demo uses two clients (one as
* submitter, one as observer) sharing the same principal; the
* observer subscribes to the live job and replays history.
*/
import {
ARCPServer,
StaticBearerVerifier,
startWebSocketServer,
} from "@arcp/sdk";
const PORT = Number(process.env.ARCP_DEMO_PORT ?? 7888);
const TOKEN = process.env.ARCP_DEMO_TOKEN ?? "demo-token";
async function main(): Promise<void> {
const server = new ARCPServer({
runtime: { name: "subscribe-demo", version: "1.0.0" },
capabilities: {
encodings: ["json"],
agents: ["timer"],
},
// Both clients authenticate as the same principal so the default
// (same-principal-only) authorization policy admits the subscriber.
bearer: new StaticBearerVerifier(new Map([[TOKEN, { principal: "demo" }]])),
});
server.registerAgent("timer", async (input, ctx) => {
const opts = (input ?? {}) as { ticks?: number; tickMs?: number };
const ticks = opts.ticks ?? 4;
const tickMs = opts.tickMs ?? 250;
await ctx.status("running");
for (let i = 1; i <= ticks; i++) {
if (ctx.signal.aborted) throw ctx.signal.reason;
await ctx.log("info", `tick ${i}/${ticks}`);
await sleep(tickMs);
}
await ctx.artifactRef({
uri: "memory://timer/result",
content_type: "application/json",
});
return { ticks };
});
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(`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());
}
const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
void main().catch((err) => {
console.error(err);
process.exit(1);
});