singularity-forge/packages/rpc-client/README.md

126 lines
4.5 KiB
Markdown
Raw Permalink Normal View History

fix: update remaining stale repo and scope refs to singularity-forge After fixing forensics.md and error-classifier.ts last fire, swept the rest of the tree for the same class of stale reference: - scripts/validate-pack.js: criticalPackages list used \`@sf\` and \`@sf-build\` scopes — neither exists in node_modules; this is in CI (.github/workflows/ci.yml) + prepublishOnly, so the validation step was failing to find anything. Now \`@singularity-forge/pi-coding-agent\` and \`@singularity-forge/rpc-client\` (the actual scope). - src/resources/skills/github-workflows/references/gh/SKILL.md: same GraphQL bug as forensics.md — owner:"sf-build" name:"sf-2" — and three \`gh project\` commands using owner sf-build. The gh issue create command above already used singularity-forge/sf-run, so the follow-up calls always failed. Also retitled "sf-2 Backlog" to "sf-run Backlog". - src/resources/extensions/sf/bootstrap/system-context.ts: deprecation warning linked to https://github.com/sf-build/SF/issues/1492. - packages/mcp-server/README.md, packages/rpc-client/README.md: 9 refs to \`@sf-build/...\` for installable package names — would mislead anyone copy-pasting into npm install. - docs/user-docs/troubleshooting.md (+ zh-CN): GitHub Issues link pointed at github.com/sf-build/SF/issues. - docs/user-docs/getting-started.md (+ zh-CN): clone URL was correct but the next \`cd\` was \`cd sf-2/docker\` — won't exist after a fresh clone of sf-run. - docs/dev/ci-cd-pipeline.md: GHCR org was \`sf-build\`. Code comments containing "sf-2" / "sf-build" in non-active places (parsers.ts banner, error message URLs in tests, dev-doc absolute paths from a contributor's Mac) left alone — they're informational and not addressed by users or runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 18:01:55 +02:00
# @singularity-forge/rpc-client
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
Standalone RPC client SDK for SF. 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.
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
Zero internal dependencies. Ships its own inlined types.
## Installation
```bash
fix: update remaining stale repo and scope refs to singularity-forge After fixing forensics.md and error-classifier.ts last fire, swept the rest of the tree for the same class of stale reference: - scripts/validate-pack.js: criticalPackages list used \`@sf\` and \`@sf-build\` scopes — neither exists in node_modules; this is in CI (.github/workflows/ci.yml) + prepublishOnly, so the validation step was failing to find anything. Now \`@singularity-forge/pi-coding-agent\` and \`@singularity-forge/rpc-client\` (the actual scope). - src/resources/skills/github-workflows/references/gh/SKILL.md: same GraphQL bug as forensics.md — owner:"sf-build" name:"sf-2" — and three \`gh project\` commands using owner sf-build. The gh issue create command above already used singularity-forge/sf-run, so the follow-up calls always failed. Also retitled "sf-2 Backlog" to "sf-run Backlog". - src/resources/extensions/sf/bootstrap/system-context.ts: deprecation warning linked to https://github.com/sf-build/SF/issues/1492. - packages/mcp-server/README.md, packages/rpc-client/README.md: 9 refs to \`@sf-build/...\` for installable package names — would mislead anyone copy-pasting into npm install. - docs/user-docs/troubleshooting.md (+ zh-CN): GitHub Issues link pointed at github.com/sf-build/SF/issues. - docs/user-docs/getting-started.md (+ zh-CN): clone URL was correct but the next \`cd\` was \`cd sf-2/docker\` — won't exist after a fresh clone of sf-run. - docs/dev/ci-cd-pipeline.md: GHCR org was \`sf-build\`. Code comments containing "sf-2" / "sf-build" in non-active places (parsers.ts banner, error message URLs in tests, dev-doc absolute paths from a contributor's Mac) left alone — they're informational and not addressed by users or runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 18:01:55 +02:00
npm install @singularity-forge/rpc-client
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
```
## Quick Start
```typescript
fix: update remaining stale repo and scope refs to singularity-forge After fixing forensics.md and error-classifier.ts last fire, swept the rest of the tree for the same class of stale reference: - scripts/validate-pack.js: criticalPackages list used \`@sf\` and \`@sf-build\` scopes — neither exists in node_modules; this is in CI (.github/workflows/ci.yml) + prepublishOnly, so the validation step was failing to find anything. Now \`@singularity-forge/pi-coding-agent\` and \`@singularity-forge/rpc-client\` (the actual scope). - src/resources/skills/github-workflows/references/gh/SKILL.md: same GraphQL bug as forensics.md — owner:"sf-build" name:"sf-2" — and three \`gh project\` commands using owner sf-build. The gh issue create command above already used singularity-forge/sf-run, so the follow-up calls always failed. Also retitled "sf-2 Backlog" to "sf-run Backlog". - src/resources/extensions/sf/bootstrap/system-context.ts: deprecation warning linked to https://github.com/sf-build/SF/issues/1492. - packages/mcp-server/README.md, packages/rpc-client/README.md: 9 refs to \`@sf-build/...\` for installable package names — would mislead anyone copy-pasting into npm install. - docs/user-docs/troubleshooting.md (+ zh-CN): GitHub Issues link pointed at github.com/sf-build/SF/issues. - docs/user-docs/getting-started.md (+ zh-CN): clone URL was correct but the next \`cd\` was \`cd sf-2/docker\` — won't exist after a fresh clone of sf-run. - docs/dev/ci-cd-pipeline.md: GHCR org was \`sf-build\`. Code comments containing "sf-2" / "sf-build" in non-active places (parsers.ts banner, error message URLs in tests, dev-doc absolute paths from a contributor's Mac) left alone — they're informational and not addressed by users or runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 18:01:55 +02:00
import { RpcClient } from '@singularity-forge/rpc-client';
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
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,
fix: update remaining stale repo and scope refs to singularity-forge After fixing forensics.md and error-classifier.ts last fire, swept the rest of the tree for the same class of stale reference: - scripts/validate-pack.js: criticalPackages list used \`@sf\` and \`@sf-build\` scopes — neither exists in node_modules; this is in CI (.github/workflows/ci.yml) + prepublishOnly, so the validation step was failing to find anything. Now \`@singularity-forge/pi-coding-agent\` and \`@singularity-forge/rpc-client\` (the actual scope). - src/resources/skills/github-workflows/references/gh/SKILL.md: same GraphQL bug as forensics.md — owner:"sf-build" name:"sf-2" — and three \`gh project\` commands using owner sf-build. The gh issue create command above already used singularity-forge/sf-run, so the follow-up calls always failed. Also retitled "sf-2 Backlog" to "sf-run Backlog". - src/resources/extensions/sf/bootstrap/system-context.ts: deprecation warning linked to https://github.com/sf-build/SF/issues/1492. - packages/mcp-server/README.md, packages/rpc-client/README.md: 9 refs to \`@sf-build/...\` for installable package names — would mislead anyone copy-pasting into npm install. - docs/user-docs/troubleshooting.md (+ zh-CN): GitHub Issues link pointed at github.com/sf-build/SF/issues. - docs/user-docs/getting-started.md (+ zh-CN): clone URL was correct but the next \`cd\` was \`cd sf-2/docker\` — won't exist after a fresh clone of sf-run. - docs/dev/ci-cd-pipeline.md: GHCR org was \`sf-build\`. Code comments containing "sf-2" / "sf-build" in non-active places (parsers.ts banner, error message URLs in tests, dev-doc absolute paths from a contributor's Mac) left alone — they're informational and not addressed by users or runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 18:01:55 +02:00
} from '@singularity-forge/rpc-client';
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
```
## License
MIT