-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
58 lines (50 loc) · 1.72 KB
/
server.ts
File metadata and controls
58 lines (50 loc) · 1.72 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
/**
* stdio — server (child process).
*
* This file is run as a child subprocess by the client. It instantiates
* an ARCPServer and binds a StdioTransport to this process's
* stdin/stdout, so the parent client can speak ARCP newline-delimited
* JSON over our pipes.
*
* IMPORTANT: anything we write to stdout MUST be valid ARCP framing —
* one JSON envelope per line. Diagnostic logs go to stderr.
*
* Run by the client via: `pnpm tsx examples/stdio/client.ts`
*/
import {
ARCPServer,
StaticBearerVerifier,
StdioTransport,
silentLogger,
} from "@arcp/sdk";
const TOKEN = process.env.ARCP_DEMO_TOKEN ?? "demo-token";
async function main(): Promise<void> {
const server = new ARCPServer({
runtime: { name: "stdio-demo", version: "1.0.0" },
capabilities: { encodings: ["json"], agents: ["echo"] },
bearer: new StaticBearerVerifier(new Map([[TOKEN, { principal: "demo" }]])),
logger: silentLogger,
});
server.registerAgent("echo", async (input, ctx) => {
const opts = (input ?? {}) as { message?: string };
const message = opts.message ?? "hello";
await ctx.status("echoing");
await ctx.log("info", `received: ${message}`);
return { echoed: message };
});
const transport = StdioTransport.fromProcess();
server.accept(transport);
process.stderr.write("[child] stdio server ready\n");
const shutdown = async (): Promise<void> => {
process.stderr.write("[child] shutting down\n");
await transport.close("shutdown");
await server.close();
process.exit(0);
};
process.on("SIGINT", () => void shutdown());
process.on("SIGTERM", () => void shutdown());
}
void main().catch((err) => {
process.stderr.write(`[child] failed: ${String(err)}\n`);
process.exit(1);
});