gsd-pi 2.53.0 → 2.54.0-dev.e1efc1a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/headless-ui.d.ts +2 -2
- package/dist/headless-ui.js +18 -15
- package/dist/headless.d.ts +11 -0
- package/dist/headless.js +178 -38
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +19 -19
- package/dist/web/standalone/.next/build-manifest.json +3 -3
- package/dist/web/standalone/.next/prerender-manifest.json +3 -3
- package/dist/web/standalone/.next/required-server-files.json +4 -4
- package/dist/web/standalone/.next/server/app/_global-error/page.js +3 -3
- package/dist/web/standalone/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.html +2 -2
- package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found/page.js +2 -2
- package/dist/web/standalone/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.rsc +3 -3
- package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +3 -3
- package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +3 -3
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/api/boot/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/boot/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/input/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/input/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/resize/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/resize/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/stream/route.js +2 -2
- package/dist/web/standalone/.next/server/app/api/bridge-terminal/stream/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/browse-directories/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/browse-directories/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/captures/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/captures/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/cleanup/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/cleanup/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/dev-mode/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/dev-mode/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/doctor/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/doctor/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/experimental/route.js +2 -2
- package/dist/web/standalone/.next/server/app/api/experimental/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/export-data/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/export-data/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/files/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/files/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/forensics/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/forensics/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/git/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/git/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/history/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/history/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/hooks/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/hooks/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/inspect/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/inspect/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/knowledge/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/knowledge/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/live-state/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/live-state/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/onboarding/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/onboarding/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/preferences/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/preferences/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/projects/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/projects/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/recovery/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/recovery/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/remote-questions/route.js +2 -2
- package/dist/web/standalone/.next/server/app/api/remote-questions/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/session/browser/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/session/browser/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/session/command/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/session/command/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/session/events/route.js +2 -2
- package/dist/web/standalone/.next/server/app/api/session/events/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/session/manage/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/session/manage/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/settings-data/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/settings-data/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/shutdown/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/shutdown/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/skill-health/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/skill-health/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/steer/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/steer/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/switch-root/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/switch-root/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/input/route.js +2 -2
- package/dist/web/standalone/.next/server/app/api/terminal/input/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/resize/route.js +2 -2
- package/dist/web/standalone/.next/server/app/api/terminal/resize/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/sessions/route.js +2 -2
- package/dist/web/standalone/.next/server/app/api/terminal/sessions/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/stream/route.js +4 -4
- package/dist/web/standalone/.next/server/app/api/terminal/stream/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/upload/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/terminal/upload/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/undo/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/undo/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/update/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/update/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/api/visualizer/route.js +1 -1
- package/dist/web/standalone/.next/server/app/api/visualizer/route_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app/index.html +1 -1
- package/dist/web/standalone/.next/server/app/index.rsc +4 -4
- package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +2 -2
- package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +4 -4
- package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +3 -3
- package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/page.js +2 -2
- package/dist/web/standalone/.next/server/app/page_client-reference-manifest.js +1 -1
- package/dist/web/standalone/.next/server/app-paths-manifest.json +19 -19
- package/dist/web/standalone/.next/server/chunks/2229.js +1 -1
- package/dist/web/standalone/.next/server/chunks/7471.js +3 -3
- package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
- package/dist/web/standalone/.next/server/middleware.js +2 -2
- package/dist/web/standalone/.next/server/next-font-manifest.js +1 -1
- package/dist/web/standalone/.next/server/next-font-manifest.json +1 -1
- package/dist/web/standalone/.next/server/pages/404.html +1 -1
- package/dist/web/standalone/.next/server/pages/500.html +2 -2
- package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
- package/dist/web/standalone/.next/static/chunks/app/_not-found/{page-2f24283c162b6ab3.js → page-f2a7482d42a5614b.js} +1 -1
- package/dist/web/standalone/.next/static/chunks/app/{layout-9ecfd95f343793f0.js → layout-a16c7a7ecdf0c2cf.js} +1 -1
- package/dist/web/standalone/.next/static/chunks/app/page-b950e4e384cc62b3.js +1 -0
- package/dist/web/standalone/.next/static/chunks/main-app-fdab67f7802d7832.js +1 -0
- package/dist/web/standalone/.next/static/chunks/next/dist/client/components/builtin/global-error-459824ffb8c323dd.js +1 -0
- package/dist/web/standalone/node_modules/node-pty/build/Makefile +2 -2
- package/dist/web/standalone/node_modules/node-pty/build/Release/pty.node +0 -0
- package/dist/web/standalone/node_modules/node-pty/build/pty.target.mk +14 -14
- package/dist/web/standalone/node_modules/node-pty/node-addon-api/node_addon_api.target.mk +14 -14
- package/dist/web/standalone/node_modules/node-pty/node-addon-api/node_addon_api_except.target.mk +14 -14
- package/dist/web/standalone/node_modules/node-pty/node-addon-api/node_addon_api_maybe.target.mk +14 -14
- package/dist/web/standalone/server.js +1 -1
- package/package.json +1 -1
- package/packages/mcp-server/README.md +6 -6
- package/packages/mcp-server/package.json +14 -4
- package/packages/mcp-server/src/cli.ts +1 -1
- package/packages/mcp-server/src/index.ts +1 -1
- package/packages/mcp-server/src/mcp-server.test.ts +2 -2
- package/packages/mcp-server/src/session-manager.ts +2 -2
- package/packages/mcp-server/src/types.ts +1 -1
- package/packages/pi-coding-agent/package.json +1 -1
- package/packages/rpc-client/README.md +125 -0
- package/packages/rpc-client/examples/basic-usage.ts +13 -0
- package/packages/rpc-client/package.json +17 -3
- package/packages/rpc-client/src/index.ts +10 -0
- package/packages/rpc-client/src/jsonl.ts +64 -0
- package/packages/rpc-client/src/rpc-client.test.ts +568 -0
- package/packages/rpc-client/src/rpc-client.ts +666 -0
- package/packages/rpc-client/src/rpc-types.ts +399 -0
- package/packages/rpc-client/tsconfig.examples.json +17 -0
- package/packages/rpc-client/tsconfig.json +24 -0
- package/pkg/package.json +1 -1
- package/dist/web/standalone/.next/static/chunks/app/page-fbecd1237e2d6d1f.js +0 -1
- package/dist/web/standalone/.next/static/chunks/main-app-d3d4c336195465f9.js +0 -1
- package/dist/web/standalone/.next/static/chunks/next/dist/client/components/builtin/global-error-ab5a8926e07ec673.js +0 -1
- /package/dist/web/standalone/.next/static/{mWBOLPnUoeaTGiD-vhu5O → nISuDzAIpGYC-DVTvs4Po}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{mWBOLPnUoeaTGiD-vhu5O → nISuDzAIpGYC-DVTvs4Po}/_ssgManifest.js +0 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @gsd-build/rpc-client
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
Zero internal dependencies. Ships its own inlined types.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @gsd-build/rpc-client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { RpcClient } from '@gsd-build/rpc-client';
|
|
17
|
+
|
|
18
|
+
const client = new RpcClient({ cwd: process.cwd() });
|
|
19
|
+
await client.start();
|
|
20
|
+
const { sessionId } = await client.init({ clientId: 'my-app' });
|
|
21
|
+
console.log(`Session: ${sessionId}`);
|
|
22
|
+
|
|
23
|
+
await client.prompt('Create a hello world script');
|
|
24
|
+
for await (const event of client.events()) {
|
|
25
|
+
if (event.type === 'execution_complete') break;
|
|
26
|
+
console.log(event.type);
|
|
27
|
+
}
|
|
28
|
+
await client.shutdown();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## API
|
|
32
|
+
|
|
33
|
+
### Constructor
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
const client = new RpcClient(options?: RpcClientOptions);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
| Option | Type | Description |
|
|
40
|
+
|------------|--------------------------|------------------------------------------|
|
|
41
|
+
| `cliPath` | `string` | Path to the CLI entry point |
|
|
42
|
+
| `cwd` | `string` | Working directory for the agent |
|
|
43
|
+
| `env` | `Record<string, string>` | Environment variables |
|
|
44
|
+
| `provider` | `string` | AI provider (e.g. `"anthropic"`) |
|
|
45
|
+
| `model` | `string` | Model ID (e.g. `"claude-sonnet"`) |
|
|
46
|
+
| `args` | `string[]` | Additional CLI arguments |
|
|
47
|
+
|
|
48
|
+
### Lifecycle
|
|
49
|
+
|
|
50
|
+
| Method | Description |
|
|
51
|
+
|---------------|------------------------------------------------|
|
|
52
|
+
| `start()` | Spawn the agent process |
|
|
53
|
+
| `init(opts?)` | v2 handshake — returns `sessionId`, capabilities |
|
|
54
|
+
| `shutdown()` | Graceful shutdown |
|
|
55
|
+
| `stop()` | Force-kill the process |
|
|
56
|
+
|
|
57
|
+
### Commands
|
|
58
|
+
|
|
59
|
+
| Method | Description |
|
|
60
|
+
|--------------------------------|----------------------------------------|
|
|
61
|
+
| `prompt(message, images?)` | Send a prompt |
|
|
62
|
+
| `steer(message, images?)` | Interrupt with a steering message |
|
|
63
|
+
| `followUp(message, images?)` | Queue a follow-up message |
|
|
64
|
+
| `abort()` | Abort current operation |
|
|
65
|
+
| `subscribe(events)` | Subscribe to event types (`["*"]` for all) |
|
|
66
|
+
|
|
67
|
+
### Events
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
// Async generator — recommended
|
|
71
|
+
for await (const event of client.events()) {
|
|
72
|
+
console.log(event.type);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Callback-based
|
|
76
|
+
const unsubscribe = client.onEvent((event) => {
|
|
77
|
+
console.log(event.type);
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Helpers
|
|
82
|
+
|
|
83
|
+
| Method | Description |
|
|
84
|
+
|---------------------------------------|------------------------------------------|
|
|
85
|
+
| `waitForIdle(timeout?)` | Wait for `agent_end` event |
|
|
86
|
+
| `collectEvents(timeout?)` | Collect events until idle |
|
|
87
|
+
| `promptAndWait(message, images?, t?)` | Send prompt and collect events |
|
|
88
|
+
|
|
89
|
+
### Session & Model
|
|
90
|
+
|
|
91
|
+
| Method | Description |
|
|
92
|
+
|----------------------------------|-----------------------------------|
|
|
93
|
+
| `getState()` | Get session state |
|
|
94
|
+
| `setModel(provider, modelId)` | Set model |
|
|
95
|
+
| `cycleModel()` | Cycle to next model |
|
|
96
|
+
| `getAvailableModels()` | List available models |
|
|
97
|
+
| `setThinkingLevel(level)` | Set thinking level |
|
|
98
|
+
| `cycleThinkingLevel()` | Cycle thinking level |
|
|
99
|
+
| `compact(instructions?)` | Compact session context |
|
|
100
|
+
| `getSessionStats()` | Get session statistics |
|
|
101
|
+
| `bash(command)` | Execute a bash command |
|
|
102
|
+
| `newSession(parent?)` | Start a new session |
|
|
103
|
+
| `sendUIResponse(id, response)` | Respond to extension UI requests |
|
|
104
|
+
|
|
105
|
+
## Type Exports
|
|
106
|
+
|
|
107
|
+
All protocol types are exported from the package root:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import type {
|
|
111
|
+
RpcCommand,
|
|
112
|
+
RpcResponse,
|
|
113
|
+
RpcInitResult,
|
|
114
|
+
RpcExecutionCompleteEvent,
|
|
115
|
+
RpcCostUpdateEvent,
|
|
116
|
+
RpcV2Event,
|
|
117
|
+
SessionStats,
|
|
118
|
+
SdkAgentEvent,
|
|
119
|
+
RpcClientOptions,
|
|
120
|
+
} from '@gsd-build/rpc-client';
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RpcClient } from '@gsd-build/rpc-client';
|
|
2
|
+
|
|
3
|
+
const client = new RpcClient({ cwd: process.cwd() });
|
|
4
|
+
await client.start();
|
|
5
|
+
const { sessionId } = await client.init({ clientId: 'my-app' });
|
|
6
|
+
console.log(`Session: ${sessionId}`);
|
|
7
|
+
|
|
8
|
+
await client.prompt('Create a hello world script');
|
|
9
|
+
for await (const event of client.events()) {
|
|
10
|
+
if (event.type === 'execution_complete') break;
|
|
11
|
+
console.log(event.type);
|
|
12
|
+
}
|
|
13
|
+
await client.shutdown();
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "@gsd/rpc-client",
|
|
3
|
-
"version": "2.
|
|
2
|
+
"name": "@gsd-build/rpc-client",
|
|
3
|
+
"version": "2.52.0",
|
|
4
4
|
"description": "Standalone RPC client SDK for GSD — zero internal dependencies",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/gsd-build/gsd-2.git",
|
|
9
|
+
"directory": "packages/rpc-client"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
5
14
|
"type": "module",
|
|
6
15
|
"main": "./dist/index.js",
|
|
7
16
|
"types": "./dist/index.d.ts",
|
|
@@ -12,8 +21,13 @@
|
|
|
12
21
|
}
|
|
13
22
|
},
|
|
14
23
|
"files": [
|
|
15
|
-
"dist"
|
|
24
|
+
"dist",
|
|
25
|
+
"!dist/**/*.test.*"
|
|
16
26
|
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.json",
|
|
29
|
+
"test": "node --test dist/rpc-client.test.js"
|
|
30
|
+
},
|
|
17
31
|
"engines": {
|
|
18
32
|
"node": ">=22.0.0"
|
|
19
33
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @gsd-build/rpc-client — standalone RPC client SDK for GSD.
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all types, JSONL utilities, and the RpcClient class.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export * from "./rpc-types.js";
|
|
8
|
+
export { serializeJsonLine, attachJsonlLineReader } from "./jsonl.js";
|
|
9
|
+
export { RpcClient } from "./rpc-client.js";
|
|
10
|
+
export type { RpcClientOptions, RpcEventListener, SdkAgentEvent } from "./rpc-client.js";
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Readable } from "node:stream";
|
|
2
|
+
import { StringDecoder } from "node:string_decoder";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Serialize a single strict JSONL record.
|
|
6
|
+
*
|
|
7
|
+
* Framing is LF-only. Payload strings may contain other Unicode separators such as
|
|
8
|
+
* U+2028 and U+2029. Clients must split records on `\n` only.
|
|
9
|
+
*/
|
|
10
|
+
export function serializeJsonLine(value: unknown): string {
|
|
11
|
+
return `${JSON.stringify(value)}\n`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Attach an LF-only JSONL reader to a stream.
|
|
16
|
+
*
|
|
17
|
+
* This intentionally does not use Node readline. Readline splits on additional
|
|
18
|
+
* Unicode separators that are valid inside JSON strings and therefore does not
|
|
19
|
+
* implement strict JSONL framing.
|
|
20
|
+
*/
|
|
21
|
+
export function attachJsonlLineReader(stream: Readable, onLine: (line: string) => void): () => void {
|
|
22
|
+
const decoder = new StringDecoder("utf8");
|
|
23
|
+
let buffer = "";
|
|
24
|
+
|
|
25
|
+
const emitLine = (line: string) => {
|
|
26
|
+
onLine(line.endsWith("\r") ? line.slice(0, -1) : line);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const onData = (chunk: string | Buffer) => {
|
|
30
|
+
buffer += typeof chunk === "string" ? chunk : decoder.write(chunk);
|
|
31
|
+
|
|
32
|
+
while (true) {
|
|
33
|
+
const newlineIndex = buffer.indexOf("\n");
|
|
34
|
+
if (newlineIndex === -1) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
emitLine(buffer.slice(0, newlineIndex));
|
|
39
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const onEnd = () => {
|
|
44
|
+
buffer += decoder.end();
|
|
45
|
+
if (buffer.length > 0) {
|
|
46
|
+
emitLine(buffer);
|
|
47
|
+
buffer = "";
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const onError = (_err: Error) => {
|
|
52
|
+
// Stream errors are non-fatal for JSONL reading
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
stream.on("data", onData);
|
|
56
|
+
stream.on("end", onEnd);
|
|
57
|
+
stream.on("error", onError);
|
|
58
|
+
|
|
59
|
+
return () => {
|
|
60
|
+
stream.off("data", onData);
|
|
61
|
+
stream.off("end", onEnd);
|
|
62
|
+
stream.off("error", onError);
|
|
63
|
+
};
|
|
64
|
+
}
|