-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
167 lines (156 loc) · 5.38 KB
/
cli.ts
File metadata and controls
167 lines (156 loc) · 5.38 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env node
/**
* `arcp` — small CLI for serving and replaying an ARCP v1.1 runtime.
*
* Implementation note (Effect migration, issue #47):
* This CLI deliberately remains a thin Commander → Promise orchestration over
* the legacy `ARCPServer` / `ARCPClient` / `EventLog` classes. Each command is
* a single-step Promise call with no concurrent resource graph, no
* cross-command shared state, and no error pipeline that benefits from
* `Effect`'s structured concurrency or Layer composition. Wrapping these in
* `ManagedRuntime.make(MainLayer)` + `runtime.runPromise(...)` would add a
* boot-time Effect dependency to a published binary without changing any
* observable behavior, so #47 was scoped out. Consumers who want an
* Effect-native entry point should compose `makeARCPServerRuntime` (from
* `@arcp/runtime`) and `makeARCPClientRuntime` (from `@arcp/client`)
* themselves; the building blocks are public.
*/
import { readFileSync } from "node:fs";
import process from "node:process";
import { Command } from "commander";
import {
ARCPClient,
ARCPServer,
EventLog,
IMPL_VERSION,
PROTOCOL_VERSION,
StaticBearerVerifier,
StdioTransport,
silentLogger,
startWebSocketServer,
WebSocketTransport,
} from "./index.js";
const program = new Command();
program
.name("arcp")
.description("ARCP reference CLI")
.version(`${IMPL_VERSION} (protocol ${PROTOCOL_VERSION})`);
program
.command("serve")
.description("Run an ARCP server (WebSocket by default)")
.option("--transport <kind>", "ws | stdio", "ws")
.option("--host <host>", "WebSocket bind host", "127.0.0.1")
.option("--port <port>", "WebSocket bind port (0 = ephemeral)", "0")
.option("--db <path>", "SQLite event log path (default :memory:)", ":memory:")
.option("--token <token>", "Static bearer token to accept", "tok")
.option(
"--principal <principal>",
"Principal bound to the token",
"anonymous",
)
.action(async (opts) => {
const eventLog = new EventLog({ path: opts.db });
const server = new ARCPServer({
runtime: { name: "arcp-cli", version: IMPL_VERSION },
capabilities: { encodings: ["json"] },
bearer: new StaticBearerVerifier(
new Map([[opts.token, { principal: opts.principal }]]),
),
eventLog,
logger: silentLogger,
});
if (opts.transport === "stdio") {
const transport = StdioTransport.fromProcess();
server.accept(transport);
process.stderr.write("arcp serve: stdio transport ready\n");
return;
}
const wss = await startWebSocketServer({
host: opts.host,
port: Number.parseInt(opts.port, 10),
onTransport: (t) => {
server.accept(t);
},
});
process.stdout.write(`arcp serve: listening on ${wss.url}\n`);
});
program
.command("submit")
.description("Submit a job to a runtime and print the terminal result")
.requiredOption("--url <url>", "WebSocket URL of the runtime")
.requiredOption("--token <token>", "Bearer token")
.requiredOption("--agent <agent>", "Agent name to invoke")
.option("--input <json>", "JSON input passed to the agent (default {})", "{}")
.option("--idempotency-key <key>", "Idempotency key")
.option("--max-runtime <sec>", "Max runtime in seconds")
.action(async (opts) => {
const client = new ARCPClient({
client: { name: "arcp-cli", version: IMPL_VERSION },
capabilities: { encodings: ["json"] },
authScheme: "bearer",
token: opts.token,
logger: silentLogger,
});
const transport = await WebSocketTransport.connect(opts.url);
await client.connect(transport);
let input: unknown;
try {
input = JSON.parse(opts.input);
} catch {
process.stderr.write("submit: --input is not valid JSON\n");
process.exit(1);
}
const handle = await client.submit({
agent: opts.agent,
input,
...(opts.idempotencyKey === undefined
? {}
: { idempotencyKey: opts.idempotencyKey }),
...(opts.maxRuntime === undefined
? {}
: { maxRuntimeSec: Number.parseInt(opts.maxRuntime, 10) }),
});
try {
const result = await handle.done;
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
} finally {
await client.close();
}
});
program
.command("replay")
.description("Dump events from a SQLite event log")
.requiredOption("--db <path>", "SQLite event log path")
.requiredOption("--session <id>", "Session id")
.option(
"--after-seq <n>",
"Replay events with event_seq strictly greater than n",
"0",
)
.action(async (opts) => {
const eventLog = new EventLog({ path: opts.db, readonly: true });
const events = await eventLog.readSinceSeq(
opts.session,
Number.parseInt(opts.afterSeq, 10),
100_000,
);
for (const env of events) {
process.stdout.write(`${JSON.stringify(env)}\n`);
}
await eventLog.close();
});
program
.command("manifest")
.description("Print the package manifest and supported message types")
.action(() => {
const manifest = {
name: "arcp",
version: IMPL_VERSION,
protocol_version: PROTOCOL_VERSION,
};
process.stdout.write(`${JSON.stringify(manifest, null, 2)}\n`);
});
// Self-check ensures the package can find its own version on disk; no-op
// otherwise, but kept here for readers of the CLI to reason about packaging.
void readFileSync;
await program.parseAsync(process.argv);