Source Code Reference
All source lives in src/ and is compiled by tsc (per tsconfig.json) to dist/.
The package is ESM ("type": "module" in package.json), so internal imports use
explicit .js extensions (e.g. import { loadConfig } from "./config.js").
src/index.ts
The CLI entrypoint (#!/usr/bin/env node). Responsibilities:
- Calls
loadConfig()to obtain{ apiKey }, exiting the process if it's missing. - Inspects
process.argvfor--bridge/-b(→runBridgeMode(apiKey)),--help/-h(→ prints usage and returns), or neither (→runInteractiveCLI(apiKey)).
src/config.ts
loadConfig()— loads.envfromprocess.cwd()viadotenv, withoverride: false(real environment variables always win). Readsprocess.env.PATBA_API_KEY; if unset, prints an error tostderrand callsprocess.exit(1). Returns{ apiKey }.maskKey(key: string): string— returns"undefined"for an empty key,"****"for keys of 8 characters or fewer, otherwise"<first 4>...<last 4>".
src/cli.ts
runInteractiveCLI(apiKey: string)— sets up areadlineinterface overstdin/stdout, prints the startup banner (with the masked key), and callscreateAWSThreadto open a thread. Then loops onaskQuestion():- Empty input re-prompts.
exit/quit(case-insensitive) closes the interface and exits with code0.- Any other input calls
triggerAWSRunthenconnectAWSStream, printing progress tostderrand the final response (or error) tostdout/stderr, then re-prompting.
src/bridge.ts
runBridgeMode(apiKey: string)— sets up a non-TTYreadlineinterface that parses each incoming line as a JSON-RPC request and dispatches onrequest.method:initialize— sets anisInitializedflag; responds withserverInfoandcapabilities: { agents: true }.session/new— requiresinitializefirst; callscreateAWSThreadand stores the resulting thread id in an in-memoryMap<localSessionId, threadId>.session/prompt— looks up the session's thread id, extracts prompt text (string or an array of{type: "text", text}blocks), then callstriggerAWSRunandconnectAWSStream, forwarding progress/results assession/updatenotifications and finishing with astopReason: "end_turn"result.agents/list— returns the single static agent descriptor forthe-brain.- Any other method — responds with a JSON-RPC
-32601 Method not founderror. - Malformed JSON on a line produces a
-32700 Parse errorresponse (withid: null).
sendError(id, code, message)— helper that writes a JSON-RPC error response tostdout.
See ACP Protocol for the full list of methods and error codes.
src/remote.ts
All functions target the fixed remote host d33ib4uu7f4xpi.cloudfront.net over
Node's built-in https module (no HTTP client dependency):
createAWSThread(apiKey): Promise<string>—POST /threadswith theX-API-Keyheader; resolves the parsedthread_id, or rejects if the response body has nothread_id.triggerAWSRun(apiKey, threadId, prompt): Promise<string>—POST /threads/:threadId/runswith body{ agentName: "the-brain", prompt, wait: false }; resolves the parsedrun_id.connectAWSStream(apiKey, threadId, runId, callbacks)—GET /threads/:threadId/runs/:runId/streamwithAccept: text/event-stream; buffers chunks, splits on newlines, and parses anydata: {...}line as JSON. Dispatches:event: "progress"→callbacks.onProgress(node, status).event: "complete"→ resolves the response text using the priority order described in CLI Usage, then callscallbacks.onComplete(responseText).- Any request-level error →
callbacks.onError(err). - Non-JSON stream fragments are silently ignored.
Extending the CLI
- Add a new flag: handle it in
src/index.tsalongside--bridge/--help, and document it in Command Reference. - Add a new ACP method: add a
casein theswitch (request.method)block ofsrc/bridge.ts, and document it in ACP Protocol. - Change remote response parsing: update the
event: "complete"handling inconnectAWSStream(src/remote.ts); bothcli.tsandbridge.tswill pick up the change automatically since they share this function.