singularity-forge/packages/rpc-client/README.md
TÂCHES a91b8bec34 feat: Headless Integration Hardening & Release (M002) (#2811)
* feat: Migrated headless orchestrator to use execution_complete events,…

- "src/headless.ts"
- "src/headless-ui.ts"
- "src/tests/headless-v2-migration.test.ts"

GSD-Task: S06/T02

* test: Wired pi-coding-agent to re-export JSONL utils from @gsd/rpc-clie…

- "packages/pi-coding-agent/src/modes/rpc/jsonl.ts"
- "packages/pi-coding-agent/package.json"
- "packages/rpc-client/src/index.ts"
- "packages/rpc-client/src/jsonl.ts"
- "packages/rpc-client/src/rpc-client.ts"
- "packages/rpc-client/src/rpc-types.ts"
- "packages/rpc-client/src/rpc-client.test.ts"
- "packages/rpc-client/package.json"

GSD-Task: S06/T03

* feat: Wire --resume flag to resolve session IDs via prefix matching and…

- "src/headless.ts"
- "dist/headless.js"

GSD-Task: S01/T01

* test: Added 5 e2e integration tests proving headless JSON batch, SIGINT…

- "src/tests/integration/e2e-headless.test.ts"

GSD-Task: S01/T02

* test: Updated @gsd/rpc-client and @gsd/mcp-server to 2.52.0 with publis…

- "packages/rpc-client/package.json"
- "packages/mcp-server/package.json"
- "packages/rpc-client/.npmignore"
- "packages/mcp-server/.npmignore"

GSD-Task: S02/T01

* chore: auto-commit after complete-milestone

GSD-Unit: M002-gzq23a

* fix: revert jsonl.ts to inline implementation — @gsd-build/rpc-client not available at source-level test time in CI

The re-export from @gsd-build/rpc-client fails in CI because tests run against
TypeScript source (--experimental-strip-types) before any build step. The npm
dependency resolves to node_modules/ which requires dist/ to exist. Reverting
to the original inline implementation eliminates the cross-package dependency
for source-level imports.
2026-03-26 23:33:22 -06:00

125 lines
4.4 KiB
Markdown

# @gsd-build/rpc-client
Standalone RPC client SDK for GSD. Spawn the agent process, perform a v2 protocol handshake, send commands, and consume typed events via an async generator — all in a few lines of TypeScript.
Zero internal dependencies. Ships its own inlined types.
## Installation
```bash
npm install @gsd-build/rpc-client
```
## Quick Start
```typescript
import { RpcClient } from '@gsd-build/rpc-client';
const client = new RpcClient({ cwd: process.cwd() });
await client.start();
const { sessionId } = await client.init({ clientId: 'my-app' });
console.log(`Session: ${sessionId}`);
await client.prompt('Create a hello world script');
for await (const event of client.events()) {
if (event.type === 'execution_complete') break;
console.log(event.type);
}
await client.shutdown();
```
## API
### Constructor
```typescript
const client = new RpcClient(options?: RpcClientOptions);
```
| Option | Type | Description |
|------------|--------------------------|------------------------------------------|
| `cliPath` | `string` | Path to the CLI entry point |
| `cwd` | `string` | Working directory for the agent |
| `env` | `Record<string, string>` | Environment variables |
| `provider` | `string` | AI provider (e.g. `"anthropic"`) |
| `model` | `string` | Model ID (e.g. `"claude-sonnet"`) |
| `args` | `string[]` | Additional CLI arguments |
### Lifecycle
| Method | Description |
|---------------|------------------------------------------------|
| `start()` | Spawn the agent process |
| `init(opts?)` | v2 handshake — returns `sessionId`, capabilities |
| `shutdown()` | Graceful shutdown |
| `stop()` | Force-kill the process |
### Commands
| Method | Description |
|--------------------------------|----------------------------------------|
| `prompt(message, images?)` | Send a prompt |
| `steer(message, images?)` | Interrupt with a steering message |
| `followUp(message, images?)` | Queue a follow-up message |
| `abort()` | Abort current operation |
| `subscribe(events)` | Subscribe to event types (`["*"]` for all) |
### Events
```typescript
// Async generator — recommended
for await (const event of client.events()) {
console.log(event.type);
}
// Callback-based
const unsubscribe = client.onEvent((event) => {
console.log(event.type);
});
```
### Helpers
| Method | Description |
|---------------------------------------|------------------------------------------|
| `waitForIdle(timeout?)` | Wait for `agent_end` event |
| `collectEvents(timeout?)` | Collect events until idle |
| `promptAndWait(message, images?, t?)` | Send prompt and collect events |
### Session & Model
| Method | Description |
|----------------------------------|-----------------------------------|
| `getState()` | Get session state |
| `setModel(provider, modelId)` | Set model |
| `cycleModel()` | Cycle to next model |
| `getAvailableModels()` | List available models |
| `setThinkingLevel(level)` | Set thinking level |
| `cycleThinkingLevel()` | Cycle thinking level |
| `compact(instructions?)` | Compact session context |
| `getSessionStats()` | Get session statistics |
| `bash(command)` | Execute a bash command |
| `newSession(parent?)` | Start a new session |
| `sendUIResponse(id, response)` | Respond to extension UI requests |
## Type Exports
All protocol types are exported from the package root:
```typescript
import type {
RpcCommand,
RpcResponse,
RpcInitResult,
RpcExecutionCompleteEvent,
RpcCostUpdateEvent,
RpcV2Event,
SessionStats,
SdkAgentEvent,
RpcClientOptions,
} from '@gsd-build/rpc-client';
```
## License
MIT