Open History to see the last commit
1import { parseArgs } from 'node:util'
2
3export interface CliArgs {
4 help: boolean
5 print?: string
6 output: 'text' | 'ndjson'
7 acp: boolean
8 mcp: boolean
9 yes: boolean
10 serve: boolean
11 port: number
12 hostname: string
13 token?: string
14 mediaKey?: string
15 thread: string
16 mediaDir?: string
17}
18
19export const USAGE = `Usage: <your-cli> [options]
20
21With no options in a terminal, starts the UI of this CLI, if it has one.
22Otherwise, and for piped input, reads one message or command per line.
23In a message, @path sends a file with it (@"my cat.png" for a path with spaces).
24
25Options:
26 -p, --print <prompt> Run one prompt, print the answer, and exit
27 --output <format> Print format for --print: text (default) or ndjson
28 --acp Serve the harness as an ACP v2 agent over stdio
29 --mcp Serve the harness as an MCP server over stdio
30 --yes Approve every tool call in MCP mode (--mcp, and /mcp of --serve)
31 --serve Serve the session protocol over HTTP (and MCP at /mcp, with @tanstack/ai-mcp)
32 --port <port> Port for --serve (default 8787)
33 --host <name> Host name for --serve (default 127.0.0.1)
34 --token <token> Bearer token for --serve (default: HARNESS_TOKEN, or a new random token)
35 --media-key <key> Key that signs media URLs of --serve, 32+ bytes (default: HARNESS_MEDIA_KEY, or a new random key at each start)
36 --thread <id> Conversation id (default: main)
37 --media-dir <dir> Save the media a turn makes here (default: ./<harness name>-media)
38 -h, --help Show this help
39
40Exit codes for --print: 0 done, 1 failed, 2 waiting for approval, 130 cancelled.`
41
42/** Parse CLI flags. Throws with a short message on an unknown flag. */
43export function parseCliArgs(argv: ReadonlyArray<string>): CliArgs {
44 const { values } = parseArgs({
45 args: [...argv],
46 options: {
47 help: { type: 'boolean', short: 'h' },
48 print: { type: 'string', short: 'p' },
49 output: { type: 'string' },
50 acp: { type: 'boolean' },
51 mcp: { type: 'boolean' },
52 yes: { type: 'boolean' },
53 serve: { type: 'boolean' },
54 port: { type: 'string' },
55 host: { type: 'string' },
56 token: { type: 'string' },
57 'media-key': { type: 'string' },
58 thread: { type: 'string' },
59 'media-dir': { type: 'string' },
60 },
61 strict: true,
62 })
63 const output = values.output ?? 'text'
64 if (output !== 'text' && output !== 'ndjson') {
65 throw new Error(`--output must be text or ndjson, got "${output}".`)
66 }
67 const port = Number(values.port ?? '8787')
68 if (!Number.isInteger(port) || port < 0 || port > 65535) {
69 throw new Error(`--port must be a port number, got "${values.port}".`)
70 }
71 return {
72 help: values.help ?? false,
73 ...(values.print !== undefined ? { print: values.print } : {}),
74 output,
75 acp: values.acp ?? false,
76 mcp: values.mcp ?? false,
77 yes: values.yes ?? false,
78 serve: values.serve ?? false,
79 port,
80 hostname: values.host ?? '127.0.0.1',
81 ...(values.token !== undefined ? { token: values.token } : {}),
82 ...(values['media-key'] !== undefined
83 ? { mediaKey: values['media-key'] }
84 : {}),
85 thread: values.thread ?? 'main',
86 ...(values['media-dir'] !== undefined
87 ? { mediaDir: values['media-dir'] }
88 : {}),
89 }
90}