@oh-my-pi/pi-coding-agent 1.337.0

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.
Files changed (224) hide show
  1. package/CHANGELOG.md +1228 -0
  2. package/README.md +1041 -0
  3. package/docs/compaction.md +403 -0
  4. package/docs/custom-tools.md +541 -0
  5. package/docs/extension-loading.md +1004 -0
  6. package/docs/hooks.md +867 -0
  7. package/docs/rpc.md +1040 -0
  8. package/docs/sdk.md +994 -0
  9. package/docs/session-tree-plan.md +441 -0
  10. package/docs/session.md +240 -0
  11. package/docs/skills.md +290 -0
  12. package/docs/theme.md +637 -0
  13. package/docs/tree.md +197 -0
  14. package/docs/tui.md +341 -0
  15. package/examples/README.md +21 -0
  16. package/examples/custom-tools/README.md +124 -0
  17. package/examples/custom-tools/hello/index.ts +20 -0
  18. package/examples/custom-tools/question/index.ts +84 -0
  19. package/examples/custom-tools/subagent/README.md +172 -0
  20. package/examples/custom-tools/subagent/agents/planner.md +37 -0
  21. package/examples/custom-tools/subagent/agents/reviewer.md +35 -0
  22. package/examples/custom-tools/subagent/agents/scout.md +50 -0
  23. package/examples/custom-tools/subagent/agents/worker.md +24 -0
  24. package/examples/custom-tools/subagent/agents.ts +156 -0
  25. package/examples/custom-tools/subagent/commands/implement-and-review.md +10 -0
  26. package/examples/custom-tools/subagent/commands/implement.md +10 -0
  27. package/examples/custom-tools/subagent/commands/scout-and-plan.md +9 -0
  28. package/examples/custom-tools/subagent/index.ts +1002 -0
  29. package/examples/custom-tools/todo/index.ts +212 -0
  30. package/examples/hooks/README.md +56 -0
  31. package/examples/hooks/auto-commit-on-exit.ts +49 -0
  32. package/examples/hooks/confirm-destructive.ts +59 -0
  33. package/examples/hooks/custom-compaction.ts +116 -0
  34. package/examples/hooks/dirty-repo-guard.ts +52 -0
  35. package/examples/hooks/file-trigger.ts +41 -0
  36. package/examples/hooks/git-checkpoint.ts +53 -0
  37. package/examples/hooks/handoff.ts +150 -0
  38. package/examples/hooks/permission-gate.ts +34 -0
  39. package/examples/hooks/protected-paths.ts +30 -0
  40. package/examples/hooks/qna.ts +119 -0
  41. package/examples/hooks/snake.ts +343 -0
  42. package/examples/hooks/status-line.ts +40 -0
  43. package/examples/sdk/01-minimal.ts +22 -0
  44. package/examples/sdk/02-custom-model.ts +49 -0
  45. package/examples/sdk/03-custom-prompt.ts +44 -0
  46. package/examples/sdk/04-skills.ts +44 -0
  47. package/examples/sdk/05-tools.ts +90 -0
  48. package/examples/sdk/06-hooks.ts +61 -0
  49. package/examples/sdk/07-context-files.ts +36 -0
  50. package/examples/sdk/08-slash-commands.ts +42 -0
  51. package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
  52. package/examples/sdk/10-settings.ts +38 -0
  53. package/examples/sdk/11-sessions.ts +48 -0
  54. package/examples/sdk/12-full-control.ts +95 -0
  55. package/examples/sdk/README.md +154 -0
  56. package/package.json +81 -0
  57. package/src/cli/args.ts +246 -0
  58. package/src/cli/file-processor.ts +72 -0
  59. package/src/cli/list-models.ts +104 -0
  60. package/src/cli/plugin-cli.ts +650 -0
  61. package/src/cli/session-picker.ts +41 -0
  62. package/src/cli.ts +10 -0
  63. package/src/commands/init.md +20 -0
  64. package/src/config.ts +159 -0
  65. package/src/core/agent-session.ts +1900 -0
  66. package/src/core/auth-storage.ts +236 -0
  67. package/src/core/bash-executor.ts +196 -0
  68. package/src/core/compaction/branch-summarization.ts +343 -0
  69. package/src/core/compaction/compaction.ts +742 -0
  70. package/src/core/compaction/index.ts +7 -0
  71. package/src/core/compaction/utils.ts +154 -0
  72. package/src/core/custom-tools/index.ts +21 -0
  73. package/src/core/custom-tools/loader.ts +248 -0
  74. package/src/core/custom-tools/types.ts +169 -0
  75. package/src/core/custom-tools/wrapper.ts +28 -0
  76. package/src/core/exec.ts +129 -0
  77. package/src/core/export-html/index.ts +211 -0
  78. package/src/core/export-html/template.css +781 -0
  79. package/src/core/export-html/template.html +54 -0
  80. package/src/core/export-html/template.js +1185 -0
  81. package/src/core/export-html/vendor/highlight.min.js +1213 -0
  82. package/src/core/export-html/vendor/marked.min.js +6 -0
  83. package/src/core/hooks/index.ts +16 -0
  84. package/src/core/hooks/loader.ts +312 -0
  85. package/src/core/hooks/runner.ts +434 -0
  86. package/src/core/hooks/tool-wrapper.ts +99 -0
  87. package/src/core/hooks/types.ts +773 -0
  88. package/src/core/index.ts +52 -0
  89. package/src/core/mcp/client.ts +158 -0
  90. package/src/core/mcp/config.ts +154 -0
  91. package/src/core/mcp/index.ts +45 -0
  92. package/src/core/mcp/loader.ts +68 -0
  93. package/src/core/mcp/manager.ts +181 -0
  94. package/src/core/mcp/tool-bridge.ts +148 -0
  95. package/src/core/mcp/transports/http.ts +316 -0
  96. package/src/core/mcp/transports/index.ts +6 -0
  97. package/src/core/mcp/transports/stdio.ts +252 -0
  98. package/src/core/mcp/types.ts +220 -0
  99. package/src/core/messages.ts +189 -0
  100. package/src/core/model-registry.ts +317 -0
  101. package/src/core/model-resolver.ts +393 -0
  102. package/src/core/plugins/doctor.ts +59 -0
  103. package/src/core/plugins/index.ts +38 -0
  104. package/src/core/plugins/installer.ts +189 -0
  105. package/src/core/plugins/loader.ts +338 -0
  106. package/src/core/plugins/manager.ts +672 -0
  107. package/src/core/plugins/parser.ts +105 -0
  108. package/src/core/plugins/paths.ts +32 -0
  109. package/src/core/plugins/types.ts +190 -0
  110. package/src/core/sdk.ts +760 -0
  111. package/src/core/session-manager.ts +1128 -0
  112. package/src/core/settings-manager.ts +443 -0
  113. package/src/core/skills.ts +437 -0
  114. package/src/core/slash-commands.ts +248 -0
  115. package/src/core/system-prompt.ts +439 -0
  116. package/src/core/timings.ts +25 -0
  117. package/src/core/tools/ask.ts +211 -0
  118. package/src/core/tools/bash-interceptor.ts +120 -0
  119. package/src/core/tools/bash.ts +250 -0
  120. package/src/core/tools/context.ts +32 -0
  121. package/src/core/tools/edit-diff.ts +475 -0
  122. package/src/core/tools/edit.ts +208 -0
  123. package/src/core/tools/exa/company.ts +59 -0
  124. package/src/core/tools/exa/index.ts +64 -0
  125. package/src/core/tools/exa/linkedin.ts +59 -0
  126. package/src/core/tools/exa/logger.ts +56 -0
  127. package/src/core/tools/exa/mcp-client.ts +368 -0
  128. package/src/core/tools/exa/render.ts +196 -0
  129. package/src/core/tools/exa/researcher.ts +90 -0
  130. package/src/core/tools/exa/search.ts +337 -0
  131. package/src/core/tools/exa/types.ts +168 -0
  132. package/src/core/tools/exa/websets.ts +248 -0
  133. package/src/core/tools/find.ts +261 -0
  134. package/src/core/tools/grep.ts +555 -0
  135. package/src/core/tools/index.ts +202 -0
  136. package/src/core/tools/ls.ts +140 -0
  137. package/src/core/tools/lsp/client.ts +605 -0
  138. package/src/core/tools/lsp/config.ts +147 -0
  139. package/src/core/tools/lsp/edits.ts +101 -0
  140. package/src/core/tools/lsp/index.ts +804 -0
  141. package/src/core/tools/lsp/render.ts +447 -0
  142. package/src/core/tools/lsp/rust-analyzer.ts +145 -0
  143. package/src/core/tools/lsp/types.ts +463 -0
  144. package/src/core/tools/lsp/utils.ts +486 -0
  145. package/src/core/tools/notebook.ts +229 -0
  146. package/src/core/tools/path-utils.ts +61 -0
  147. package/src/core/tools/read.ts +240 -0
  148. package/src/core/tools/renderers.ts +540 -0
  149. package/src/core/tools/task/agents.ts +153 -0
  150. package/src/core/tools/task/artifacts.ts +114 -0
  151. package/src/core/tools/task/bundled-agents/browser.md +71 -0
  152. package/src/core/tools/task/bundled-agents/explore.md +82 -0
  153. package/src/core/tools/task/bundled-agents/plan.md +54 -0
  154. package/src/core/tools/task/bundled-agents/reviewer.md +59 -0
  155. package/src/core/tools/task/bundled-agents/task.md +53 -0
  156. package/src/core/tools/task/bundled-commands/architect-plan.md +10 -0
  157. package/src/core/tools/task/bundled-commands/implement-with-critic.md +11 -0
  158. package/src/core/tools/task/bundled-commands/implement.md +11 -0
  159. package/src/core/tools/task/commands.ts +213 -0
  160. package/src/core/tools/task/discovery.ts +208 -0
  161. package/src/core/tools/task/executor.ts +367 -0
  162. package/src/core/tools/task/index.ts +388 -0
  163. package/src/core/tools/task/model-resolver.ts +115 -0
  164. package/src/core/tools/task/parallel.ts +38 -0
  165. package/src/core/tools/task/render.ts +232 -0
  166. package/src/core/tools/task/types.ts +99 -0
  167. package/src/core/tools/truncate.ts +265 -0
  168. package/src/core/tools/web-fetch.ts +2370 -0
  169. package/src/core/tools/web-search/auth.ts +193 -0
  170. package/src/core/tools/web-search/index.ts +537 -0
  171. package/src/core/tools/web-search/providers/anthropic.ts +198 -0
  172. package/src/core/tools/web-search/providers/exa.ts +302 -0
  173. package/src/core/tools/web-search/providers/perplexity.ts +195 -0
  174. package/src/core/tools/web-search/render.ts +182 -0
  175. package/src/core/tools/web-search/types.ts +180 -0
  176. package/src/core/tools/write.ts +99 -0
  177. package/src/index.ts +176 -0
  178. package/src/main.ts +464 -0
  179. package/src/migrations.ts +135 -0
  180. package/src/modes/index.ts +43 -0
  181. package/src/modes/interactive/components/armin.ts +382 -0
  182. package/src/modes/interactive/components/assistant-message.ts +86 -0
  183. package/src/modes/interactive/components/bash-execution.ts +196 -0
  184. package/src/modes/interactive/components/bordered-loader.ts +41 -0
  185. package/src/modes/interactive/components/branch-summary-message.ts +42 -0
  186. package/src/modes/interactive/components/compaction-summary-message.ts +45 -0
  187. package/src/modes/interactive/components/custom-editor.ts +122 -0
  188. package/src/modes/interactive/components/diff.ts +147 -0
  189. package/src/modes/interactive/components/dynamic-border.ts +25 -0
  190. package/src/modes/interactive/components/footer.ts +381 -0
  191. package/src/modes/interactive/components/hook-editor.ts +117 -0
  192. package/src/modes/interactive/components/hook-input.ts +64 -0
  193. package/src/modes/interactive/components/hook-message.ts +96 -0
  194. package/src/modes/interactive/components/hook-selector.ts +91 -0
  195. package/src/modes/interactive/components/model-selector.ts +247 -0
  196. package/src/modes/interactive/components/oauth-selector.ts +120 -0
  197. package/src/modes/interactive/components/plugin-settings.ts +479 -0
  198. package/src/modes/interactive/components/queue-mode-selector.ts +56 -0
  199. package/src/modes/interactive/components/session-selector.ts +204 -0
  200. package/src/modes/interactive/components/settings-selector.ts +453 -0
  201. package/src/modes/interactive/components/show-images-selector.ts +45 -0
  202. package/src/modes/interactive/components/theme-selector.ts +62 -0
  203. package/src/modes/interactive/components/thinking-selector.ts +64 -0
  204. package/src/modes/interactive/components/tool-execution.ts +675 -0
  205. package/src/modes/interactive/components/tree-selector.ts +866 -0
  206. package/src/modes/interactive/components/user-message-selector.ts +159 -0
  207. package/src/modes/interactive/components/user-message.ts +18 -0
  208. package/src/modes/interactive/components/visual-truncate.ts +50 -0
  209. package/src/modes/interactive/components/welcome.ts +183 -0
  210. package/src/modes/interactive/interactive-mode.ts +2516 -0
  211. package/src/modes/interactive/theme/dark.json +101 -0
  212. package/src/modes/interactive/theme/light.json +98 -0
  213. package/src/modes/interactive/theme/theme-schema.json +308 -0
  214. package/src/modes/interactive/theme/theme.ts +998 -0
  215. package/src/modes/print-mode.ts +128 -0
  216. package/src/modes/rpc/rpc-client.ts +527 -0
  217. package/src/modes/rpc/rpc-mode.ts +483 -0
  218. package/src/modes/rpc/rpc-types.ts +203 -0
  219. package/src/utils/changelog.ts +99 -0
  220. package/src/utils/clipboard.ts +265 -0
  221. package/src/utils/fuzzy.ts +108 -0
  222. package/src/utils/mime.ts +30 -0
  223. package/src/utils/shell.ts +276 -0
  224. package/src/utils/tools-manager.ts +274 -0
package/docs/rpc.md ADDED
@@ -0,0 +1,1040 @@
1
+ # RPC Mode
2
+
3
+ RPC mode enables headless operation of the coding agent via a JSON protocol over stdin/stdout. This is useful for embedding the agent in other applications, IDEs, or custom UIs.
4
+
5
+ **Note for Node.js/TypeScript users**: If you're building a Node.js application, consider using `AgentSession` directly from `@oh-my-pi/pi-coding-agent` instead of spawning a subprocess. See [`src/core/agent-session.ts`](../src/core/agent-session.ts) for the API. For a subprocess-based TypeScript client, see [`src/modes/rpc/rpc-client.ts`](../src/modes/rpc/rpc-client.ts).
6
+
7
+ ## Starting RPC Mode
8
+
9
+ ```bash
10
+ pi --mode rpc [options]
11
+ ```
12
+
13
+ Common options:
14
+
15
+ - `--provider <name>`: Set the LLM provider (anthropic, openai, google, etc.)
16
+ - `--model <id>`: Set the model ID
17
+ - `--no-session`: Disable session persistence
18
+ - `--session-dir <path>`: Custom session storage directory
19
+
20
+ ## Protocol Overview
21
+
22
+ - **Commands**: JSON objects sent to stdin, one per line
23
+ - **Responses**: JSON objects with `type: "response"` indicating command success/failure
24
+ - **Events**: Agent events streamed to stdout as JSON lines
25
+
26
+ All commands support an optional `id` field for request/response correlation. If provided, the corresponding response will include the same `id`.
27
+
28
+ ## Commands
29
+
30
+ ### Prompting
31
+
32
+ #### prompt
33
+
34
+ Send a user prompt to the agent. Returns immediately; events stream asynchronously.
35
+
36
+ ```json
37
+ { "id": "req-1", "type": "prompt", "message": "Hello, world!" }
38
+ ```
39
+
40
+ With images:
41
+
42
+ ```json
43
+ {
44
+ "type": "prompt",
45
+ "message": "What's in this image?",
46
+ "images": [{ "type": "image", "source": { "type": "base64", "mediaType": "image/png", "data": "..." } }]
47
+ }
48
+ ```
49
+
50
+ Response:
51
+
52
+ ```json
53
+ { "id": "req-1", "type": "response", "command": "prompt", "success": true }
54
+ ```
55
+
56
+ The `images` field is optional. Each image uses `ImageContent` format with base64 or URL source.
57
+
58
+ #### queue_message
59
+
60
+ Queue a message to be injected at the next agent turn. Queued messages are added to the conversation without triggering a new prompt. Useful for injecting context mid-conversation.
61
+
62
+ ```json
63
+ { "type": "queue_message", "message": "Additional context" }
64
+ ```
65
+
66
+ Response:
67
+
68
+ ```json
69
+ { "type": "response", "command": "queue_message", "success": true }
70
+ ```
71
+
72
+ See [set_queue_mode](#set_queue_mode) for controlling how queued messages are processed.
73
+
74
+ #### abort
75
+
76
+ Abort the current agent operation.
77
+
78
+ ```json
79
+ { "type": "abort" }
80
+ ```
81
+
82
+ Response:
83
+
84
+ ```json
85
+ { "type": "response", "command": "abort", "success": true }
86
+ ```
87
+
88
+ #### new_session
89
+
90
+ Start a fresh session. Can be cancelled by a `session_before_switch` hook.
91
+
92
+ ```json
93
+ { "type": "new_session" }
94
+ ```
95
+
96
+ With optional parent session tracking:
97
+
98
+ ```json
99
+ { "type": "new_session", "parentSession": "/path/to/parent-session.jsonl" }
100
+ ```
101
+
102
+ Response:
103
+
104
+ ```json
105
+ { "type": "response", "command": "new_session", "success": true, "data": { "cancelled": false } }
106
+ ```
107
+
108
+ If a hook cancelled:
109
+
110
+ ```json
111
+ { "type": "response", "command": "new_session", "success": true, "data": { "cancelled": true } }
112
+ ```
113
+
114
+ ### State
115
+
116
+ #### get_state
117
+
118
+ Get current session state.
119
+
120
+ ```json
121
+ { "type": "get_state" }
122
+ ```
123
+
124
+ Response:
125
+
126
+ ```json
127
+ {
128
+ "type": "response",
129
+ "command": "get_state",
130
+ "success": true,
131
+ "data": {
132
+ "model": {...},
133
+ "thinkingLevel": "medium",
134
+ "isStreaming": false,
135
+ "isCompacting": false,
136
+ "queueMode": "all",
137
+ "sessionFile": "/path/to/session.jsonl",
138
+ "sessionId": "abc123",
139
+ "autoCompactionEnabled": true,
140
+ "messageCount": 5,
141
+ "queuedMessageCount": 0
142
+ }
143
+ }
144
+ ```
145
+
146
+ The `model` field is a full [Model](#model) object or `null`.
147
+
148
+ #### get_messages
149
+
150
+ Get all messages in the conversation.
151
+
152
+ ```json
153
+ { "type": "get_messages" }
154
+ ```
155
+
156
+ Response:
157
+
158
+ ```json
159
+ {
160
+ "type": "response",
161
+ "command": "get_messages",
162
+ "success": true,
163
+ "data": {"messages": [...]}
164
+ }
165
+ ```
166
+
167
+ Messages are `AgentMessage` objects (see [Message Types](#message-types)).
168
+
169
+ ### Model
170
+
171
+ #### set_model
172
+
173
+ Switch to a specific model.
174
+
175
+ ```json
176
+ { "type": "set_model", "provider": "anthropic", "modelId": "claude-sonnet-4-20250514" }
177
+ ```
178
+
179
+ Response contains the full [Model](#model) object:
180
+
181
+ ```json
182
+ {
183
+ "type": "response",
184
+ "command": "set_model",
185
+ "success": true,
186
+ "data": {...}
187
+ }
188
+ ```
189
+
190
+ #### cycle_model
191
+
192
+ Cycle to the next available model. Returns `null` data if only one model available.
193
+
194
+ ```json
195
+ { "type": "cycle_model" }
196
+ ```
197
+
198
+ Response:
199
+
200
+ ```json
201
+ {
202
+ "type": "response",
203
+ "command": "cycle_model",
204
+ "success": true,
205
+ "data": {
206
+ "model": {...},
207
+ "thinkingLevel": "medium",
208
+ "isScoped": false
209
+ }
210
+ }
211
+ ```
212
+
213
+ The `model` field is a full [Model](#model) object.
214
+
215
+ #### get_available_models
216
+
217
+ List all configured models.
218
+
219
+ ```json
220
+ { "type": "get_available_models" }
221
+ ```
222
+
223
+ Response contains an array of full [Model](#model) objects:
224
+
225
+ ```json
226
+ {
227
+ "type": "response",
228
+ "command": "get_available_models",
229
+ "success": true,
230
+ "data": {
231
+ "models": [...]
232
+ }
233
+ }
234
+ ```
235
+
236
+ ### Thinking
237
+
238
+ #### set_thinking_level
239
+
240
+ Set the reasoning/thinking level for models that support it.
241
+
242
+ ```json
243
+ { "type": "set_thinking_level", "level": "high" }
244
+ ```
245
+
246
+ Levels: `"off"`, `"minimal"`, `"low"`, `"medium"`, `"high"`, `"xhigh"`
247
+
248
+ Note: `"xhigh"` is only supported by OpenAI codex-max models.
249
+
250
+ Response:
251
+
252
+ ```json
253
+ { "type": "response", "command": "set_thinking_level", "success": true }
254
+ ```
255
+
256
+ #### cycle_thinking_level
257
+
258
+ Cycle through available thinking levels. Returns `null` data if model doesn't support thinking.
259
+
260
+ ```json
261
+ { "type": "cycle_thinking_level" }
262
+ ```
263
+
264
+ Response:
265
+
266
+ ```json
267
+ {
268
+ "type": "response",
269
+ "command": "cycle_thinking_level",
270
+ "success": true,
271
+ "data": { "level": "high" }
272
+ }
273
+ ```
274
+
275
+ ### Queue Mode
276
+
277
+ #### set_queue_mode
278
+
279
+ Control how queued messages (from `queue_message`) are injected into the conversation.
280
+
281
+ ```json
282
+ { "type": "set_queue_mode", "mode": "one-at-a-time" }
283
+ ```
284
+
285
+ Modes:
286
+
287
+ - `"all"`: Inject all queued messages at the next turn
288
+ - `"one-at-a-time"`: Inject one queued message per turn (default)
289
+
290
+ Response:
291
+
292
+ ```json
293
+ { "type": "response", "command": "set_queue_mode", "success": true }
294
+ ```
295
+
296
+ ### Compaction
297
+
298
+ #### compact
299
+
300
+ Manually compact conversation context to reduce token usage.
301
+
302
+ ```json
303
+ { "type": "compact" }
304
+ ```
305
+
306
+ With custom instructions:
307
+
308
+ ```json
309
+ { "type": "compact", "customInstructions": "Focus on code changes" }
310
+ ```
311
+
312
+ Response:
313
+
314
+ ```json
315
+ {
316
+ "type": "response",
317
+ "command": "compact",
318
+ "success": true,
319
+ "data": {
320
+ "summary": "Summary of conversation...",
321
+ "firstKeptEntryId": "abc123",
322
+ "tokensBefore": 150000,
323
+ "details": {}
324
+ }
325
+ }
326
+ ```
327
+
328
+ #### set_auto_compaction
329
+
330
+ Enable or disable automatic compaction when context is nearly full.
331
+
332
+ ```json
333
+ { "type": "set_auto_compaction", "enabled": true }
334
+ ```
335
+
336
+ Response:
337
+
338
+ ```json
339
+ { "type": "response", "command": "set_auto_compaction", "success": true }
340
+ ```
341
+
342
+ ### Retry
343
+
344
+ #### set_auto_retry
345
+
346
+ Enable or disable automatic retry on transient errors (overloaded, rate limit, 5xx).
347
+
348
+ ```json
349
+ { "type": "set_auto_retry", "enabled": true }
350
+ ```
351
+
352
+ Response:
353
+
354
+ ```json
355
+ { "type": "response", "command": "set_auto_retry", "success": true }
356
+ ```
357
+
358
+ #### abort_retry
359
+
360
+ Abort an in-progress retry (cancel the delay and stop retrying).
361
+
362
+ ```json
363
+ { "type": "abort_retry" }
364
+ ```
365
+
366
+ Response:
367
+
368
+ ```json
369
+ { "type": "response", "command": "abort_retry", "success": true }
370
+ ```
371
+
372
+ ### Bash
373
+
374
+ #### bash
375
+
376
+ Execute a shell command and add output to conversation context.
377
+
378
+ ```json
379
+ { "type": "bash", "command": "ls -la" }
380
+ ```
381
+
382
+ Response:
383
+
384
+ ```json
385
+ {
386
+ "type": "response",
387
+ "command": "bash",
388
+ "success": true,
389
+ "data": {
390
+ "output": "total 48\ndrwxr-xr-x ...",
391
+ "exitCode": 0,
392
+ "cancelled": false,
393
+ "truncated": false
394
+ }
395
+ }
396
+ ```
397
+
398
+ If output was truncated, includes `fullOutputPath`:
399
+
400
+ ```json
401
+ {
402
+ "type": "response",
403
+ "command": "bash",
404
+ "success": true,
405
+ "data": {
406
+ "output": "truncated output...",
407
+ "exitCode": 0,
408
+ "cancelled": false,
409
+ "truncated": true,
410
+ "fullOutputPath": "/tmp/pi-bash-abc123.log"
411
+ }
412
+ }
413
+ ```
414
+
415
+ **How bash results reach the LLM:**
416
+
417
+ The `bash` command executes immediately and returns a `BashResult`. Internally, a `BashExecutionMessage` is created and stored in the agent's message state. This message does NOT emit an event.
418
+
419
+ When the next `prompt` command is sent, all messages (including `BashExecutionMessage`) are transformed before being sent to the LLM. The `BashExecutionMessage` is converted to a `UserMessage` with this format:
420
+
421
+ ```
422
+ Ran `ls -la`
423
+ \`\`\`
424
+ total 48
425
+ drwxr-xr-x ...
426
+ \`\`\`
427
+ ```
428
+
429
+ This means:
430
+
431
+ 1. Bash output is included in the LLM context on the **next prompt**, not immediately
432
+ 2. Multiple bash commands can be executed before a prompt; all outputs will be included
433
+ 3. No event is emitted for the `BashExecutionMessage` itself
434
+
435
+ #### abort_bash
436
+
437
+ Abort a running bash command.
438
+
439
+ ```json
440
+ { "type": "abort_bash" }
441
+ ```
442
+
443
+ Response:
444
+
445
+ ```json
446
+ { "type": "response", "command": "abort_bash", "success": true }
447
+ ```
448
+
449
+ ### Session
450
+
451
+ #### get_session_stats
452
+
453
+ Get token usage and cost statistics.
454
+
455
+ ```json
456
+ { "type": "get_session_stats" }
457
+ ```
458
+
459
+ Response:
460
+
461
+ ```json
462
+ {
463
+ "type": "response",
464
+ "command": "get_session_stats",
465
+ "success": true,
466
+ "data": {
467
+ "sessionFile": "/path/to/session.jsonl",
468
+ "sessionId": "abc123",
469
+ "userMessages": 5,
470
+ "assistantMessages": 5,
471
+ "toolCalls": 12,
472
+ "toolResults": 12,
473
+ "totalMessages": 22,
474
+ "tokens": {
475
+ "input": 50000,
476
+ "output": 10000,
477
+ "cacheRead": 40000,
478
+ "cacheWrite": 5000,
479
+ "total": 105000
480
+ },
481
+ "cost": 0.45
482
+ }
483
+ }
484
+ ```
485
+
486
+ #### export_html
487
+
488
+ Export session to an HTML file.
489
+
490
+ ```json
491
+ { "type": "export_html" }
492
+ ```
493
+
494
+ With custom path:
495
+
496
+ ```json
497
+ { "type": "export_html", "outputPath": "/tmp/session.html" }
498
+ ```
499
+
500
+ Response:
501
+
502
+ ```json
503
+ {
504
+ "type": "response",
505
+ "command": "export_html",
506
+ "success": true,
507
+ "data": { "path": "/tmp/session.html" }
508
+ }
509
+ ```
510
+
511
+ #### switch_session
512
+
513
+ Load a different session file. Can be cancelled by a `before_switch` hook.
514
+
515
+ ```json
516
+ { "type": "switch_session", "sessionPath": "/path/to/session.jsonl" }
517
+ ```
518
+
519
+ Response:
520
+
521
+ ```json
522
+ { "type": "response", "command": "switch_session", "success": true, "data": { "cancelled": false } }
523
+ ```
524
+
525
+ If a hook cancelled the switch:
526
+
527
+ ```json
528
+ { "type": "response", "command": "switch_session", "success": true, "data": { "cancelled": true } }
529
+ ```
530
+
531
+ #### branch
532
+
533
+ Create a new branch from a previous user message. Can be cancelled by a `before_branch` hook. Returns the text of the message being branched from.
534
+
535
+ ```json
536
+ { "type": "branch", "entryId": "abc123" }
537
+ ```
538
+
539
+ Response:
540
+
541
+ ```json
542
+ {
543
+ "type": "response",
544
+ "command": "branch",
545
+ "success": true,
546
+ "data": { "text": "The original prompt text...", "cancelled": false }
547
+ }
548
+ ```
549
+
550
+ If a hook cancelled the branch:
551
+
552
+ ```json
553
+ {
554
+ "type": "response",
555
+ "command": "branch",
556
+ "success": true,
557
+ "data": { "text": "The original prompt text...", "cancelled": true }
558
+ }
559
+ ```
560
+
561
+ #### get_branch_messages
562
+
563
+ Get user messages available for branching.
564
+
565
+ ```json
566
+ { "type": "get_branch_messages" }
567
+ ```
568
+
569
+ Response:
570
+
571
+ ```json
572
+ {
573
+ "type": "response",
574
+ "command": "get_branch_messages",
575
+ "success": true,
576
+ "data": {
577
+ "messages": [
578
+ { "entryId": "abc123", "text": "First prompt..." },
579
+ { "entryId": "def456", "text": "Second prompt..." }
580
+ ]
581
+ }
582
+ }
583
+ ```
584
+
585
+ #### get_last_assistant_text
586
+
587
+ Get the text content of the last assistant message.
588
+
589
+ ```json
590
+ { "type": "get_last_assistant_text" }
591
+ ```
592
+
593
+ Response:
594
+
595
+ ```json
596
+ {
597
+ "type": "response",
598
+ "command": "get_last_assistant_text",
599
+ "success": true,
600
+ "data": { "text": "The assistant's response..." }
601
+ }
602
+ ```
603
+
604
+ Returns `{"text": null}` if no assistant messages exist.
605
+
606
+ ## Events
607
+
608
+ Events are streamed to stdout as JSON lines during agent operation. Events do NOT include an `id` field (only responses do).
609
+
610
+ ### Event Types
611
+
612
+ | Event | Description |
613
+ | ----------------------- | ------------------------------------------------------------ |
614
+ | `agent_start` | Agent begins processing |
615
+ | `agent_end` | Agent completes (includes all generated messages) |
616
+ | `turn_start` | New turn begins |
617
+ | `turn_end` | Turn completes (includes assistant message and tool results) |
618
+ | `message_start` | Message begins |
619
+ | `message_update` | Streaming update (text/thinking/toolcall deltas) |
620
+ | `message_end` | Message completes |
621
+ | `tool_execution_start` | Tool begins execution |
622
+ | `tool_execution_update` | Tool execution progress (streaming output) |
623
+ | `tool_execution_end` | Tool completes |
624
+ | `auto_compaction_start` | Auto-compaction begins |
625
+ | `auto_compaction_end` | Auto-compaction completes |
626
+ | `auto_retry_start` | Auto-retry begins (after transient error) |
627
+ | `auto_retry_end` | Auto-retry completes (success or final failure) |
628
+ | `hook_error` | Hook threw an error |
629
+
630
+ ### agent_start
631
+
632
+ Emitted when the agent begins processing a prompt.
633
+
634
+ ```json
635
+ { "type": "agent_start" }
636
+ ```
637
+
638
+ ### agent_end
639
+
640
+ Emitted when the agent completes. Contains all messages generated during this run.
641
+
642
+ ```json
643
+ {
644
+ "type": "agent_end",
645
+ "messages": [...]
646
+ }
647
+ ```
648
+
649
+ ### turn_start / turn_end
650
+
651
+ A turn consists of one assistant response plus any resulting tool calls and results.
652
+
653
+ ```json
654
+ { "type": "turn_start" }
655
+ ```
656
+
657
+ ```json
658
+ {
659
+ "type": "turn_end",
660
+ "message": {...},
661
+ "toolResults": [...]
662
+ }
663
+ ```
664
+
665
+ ### message_start / message_end
666
+
667
+ Emitted when a message begins and completes. The `message` field contains an `AgentMessage`.
668
+
669
+ ```json
670
+ {"type": "message_start", "message": {...}}
671
+ {"type": "message_end", "message": {...}}
672
+ ```
673
+
674
+ ### message_update (Streaming)
675
+
676
+ Emitted during streaming of assistant messages. Contains both the partial message and a streaming delta event.
677
+
678
+ ```json
679
+ {
680
+ "type": "message_update",
681
+ "message": {...},
682
+ "assistantMessageEvent": {
683
+ "type": "text_delta",
684
+ "contentIndex": 0,
685
+ "delta": "Hello ",
686
+ "partial": {...}
687
+ }
688
+ }
689
+ ```
690
+
691
+ The `assistantMessageEvent` field contains one of these delta types:
692
+
693
+ | Type | Description |
694
+ | ---------------- | ------------------------------------------------------------ |
695
+ | `start` | Message generation started |
696
+ | `text_start` | Text content block started |
697
+ | `text_delta` | Text content chunk |
698
+ | `text_end` | Text content block ended |
699
+ | `thinking_start` | Thinking block started |
700
+ | `thinking_delta` | Thinking content chunk |
701
+ | `thinking_end` | Thinking block ended |
702
+ | `toolcall_start` | Tool call started |
703
+ | `toolcall_delta` | Tool call arguments chunk |
704
+ | `toolcall_end` | Tool call ended (includes full `toolCall` object) |
705
+ | `done` | Message complete (reason: `"stop"`, `"length"`, `"toolUse"`) |
706
+ | `error` | Error occurred (reason: `"aborted"`, `"error"`) |
707
+
708
+ Example streaming a text response:
709
+
710
+ ```json
711
+ {"type":"message_update","message":{...},"assistantMessageEvent":{"type":"text_start","contentIndex":0,"partial":{...}}}
712
+ {"type":"message_update","message":{...},"assistantMessageEvent":{"type":"text_delta","contentIndex":0,"delta":"Hello","partial":{...}}}
713
+ {"type":"message_update","message":{...},"assistantMessageEvent":{"type":"text_delta","contentIndex":0,"delta":" world","partial":{...}}}
714
+ {"type":"message_update","message":{...},"assistantMessageEvent":{"type":"text_end","contentIndex":0,"content":"Hello world","partial":{...}}}
715
+ ```
716
+
717
+ ### tool_execution_start / tool_execution_update / tool_execution_end
718
+
719
+ Emitted when a tool begins, streams progress, and completes execution.
720
+
721
+ ```json
722
+ {
723
+ "type": "tool_execution_start",
724
+ "toolCallId": "call_abc123",
725
+ "toolName": "bash",
726
+ "args": { "command": "ls -la" }
727
+ }
728
+ ```
729
+
730
+ During execution, `tool_execution_update` events stream partial results (e.g., bash output as it arrives):
731
+
732
+ ```json
733
+ {
734
+ "type": "tool_execution_update",
735
+ "toolCallId": "call_abc123",
736
+ "toolName": "bash",
737
+ "args": { "command": "ls -la" },
738
+ "partialResult": {
739
+ "content": [{ "type": "text", "text": "partial output so far..." }],
740
+ "details": { "truncation": null, "fullOutputPath": null }
741
+ }
742
+ }
743
+ ```
744
+
745
+ When complete:
746
+
747
+ ```json
748
+ {
749
+ "type": "tool_execution_end",
750
+ "toolCallId": "call_abc123",
751
+ "toolName": "bash",
752
+ "result": {
753
+ "content": [{"type": "text", "text": "total 48\n..."}],
754
+ "details": {...}
755
+ },
756
+ "isError": false
757
+ }
758
+ ```
759
+
760
+ Use `toolCallId` to correlate events. The `partialResult` in `tool_execution_update` contains the accumulated output so far (not just the delta), allowing clients to simply replace their display on each update.
761
+
762
+ ### auto_compaction_start / auto_compaction_end
763
+
764
+ Emitted when automatic compaction runs (when context is nearly full).
765
+
766
+ ```json
767
+ { "type": "auto_compaction_start", "reason": "threshold" }
768
+ ```
769
+
770
+ The `reason` field is `"threshold"` (context getting large) or `"overflow"` (context exceeded limit).
771
+
772
+ ```json
773
+ {
774
+ "type": "auto_compaction_end",
775
+ "result": {
776
+ "summary": "Summary of conversation...",
777
+ "firstKeptEntryId": "abc123",
778
+ "tokensBefore": 150000,
779
+ "details": {}
780
+ },
781
+ "aborted": false,
782
+ "willRetry": false
783
+ }
784
+ ```
785
+
786
+ If `reason` was `"overflow"` and compaction succeeds, `willRetry` is `true` and the agent will automatically retry the prompt.
787
+
788
+ If compaction was aborted, `result` is `null` and `aborted` is `true`.
789
+
790
+ ### auto_retry_start / auto_retry_end
791
+
792
+ Emitted when automatic retry is triggered after a transient error (overloaded, rate limit, 5xx).
793
+
794
+ ```json
795
+ {
796
+ "type": "auto_retry_start",
797
+ "attempt": 1,
798
+ "maxAttempts": 3,
799
+ "delayMs": 2000,
800
+ "errorMessage": "529 {\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"Overloaded\"}}"
801
+ }
802
+ ```
803
+
804
+ ```json
805
+ {
806
+ "type": "auto_retry_end",
807
+ "success": true,
808
+ "attempt": 2
809
+ }
810
+ ```
811
+
812
+ On final failure (max retries exceeded):
813
+
814
+ ```json
815
+ {
816
+ "type": "auto_retry_end",
817
+ "success": false,
818
+ "attempt": 3,
819
+ "finalError": "529 overloaded_error: Overloaded"
820
+ }
821
+ ```
822
+
823
+ ### hook_error
824
+
825
+ Emitted when a hook throws an error.
826
+
827
+ ```json
828
+ {
829
+ "type": "hook_error",
830
+ "hookPath": "/path/to/hook.ts",
831
+ "event": "tool_call",
832
+ "error": "Error message..."
833
+ }
834
+ ```
835
+
836
+ ## Error Handling
837
+
838
+ Failed commands return a response with `success: false`:
839
+
840
+ ```json
841
+ {
842
+ "type": "response",
843
+ "command": "set_model",
844
+ "success": false,
845
+ "error": "Model not found: invalid/model"
846
+ }
847
+ ```
848
+
849
+ Parse errors:
850
+
851
+ ```json
852
+ {
853
+ "type": "response",
854
+ "command": "parse",
855
+ "success": false,
856
+ "error": "Failed to parse command: Unexpected token..."
857
+ }
858
+ ```
859
+
860
+ ## Types
861
+
862
+ Source files:
863
+
864
+ - [`packages/ai/src/types.ts`](../../ai/src/types.ts) - `Model`, `UserMessage`, `AssistantMessage`, `ToolResultMessage`
865
+ - [`packages/agent/src/types.ts`](../../agent/src/types.ts) - `AgentMessage`, `AgentEvent`
866
+ - [`src/core/messages.ts`](../src/core/messages.ts) - `BashExecutionMessage`
867
+ - [`src/modes/rpc/rpc-types.ts`](../src/modes/rpc/rpc-types.ts) - RPC command/response types
868
+
869
+ ### Model
870
+
871
+ ```json
872
+ {
873
+ "id": "claude-sonnet-4-20250514",
874
+ "name": "Claude Sonnet 4",
875
+ "api": "anthropic-messages",
876
+ "provider": "anthropic",
877
+ "baseUrl": "https://api.anthropic.com",
878
+ "reasoning": true,
879
+ "input": ["text", "image"],
880
+ "contextWindow": 200000,
881
+ "maxTokens": 16384,
882
+ "cost": {
883
+ "input": 3.0,
884
+ "output": 15.0,
885
+ "cacheRead": 0.3,
886
+ "cacheWrite": 3.75
887
+ }
888
+ }
889
+ ```
890
+
891
+ ### UserMessage
892
+
893
+ ```json
894
+ {
895
+ "role": "user",
896
+ "content": "Hello!",
897
+ "timestamp": 1733234567890,
898
+ "attachments": []
899
+ }
900
+ ```
901
+
902
+ The `content` field can be a string or an array of `TextContent`/`ImageContent` blocks.
903
+
904
+ ### AssistantMessage
905
+
906
+ ```json
907
+ {
908
+ "role": "assistant",
909
+ "content": [
910
+ { "type": "text", "text": "Hello! How can I help?" },
911
+ { "type": "thinking", "thinking": "User is greeting me..." },
912
+ { "type": "toolCall", "id": "call_123", "name": "bash", "arguments": { "command": "ls" } }
913
+ ],
914
+ "api": "anthropic-messages",
915
+ "provider": "anthropic",
916
+ "model": "claude-sonnet-4-20250514",
917
+ "usage": {
918
+ "input": 100,
919
+ "output": 50,
920
+ "cacheRead": 0,
921
+ "cacheWrite": 0,
922
+ "cost": { "input": 0.0003, "output": 0.00075, "cacheRead": 0, "cacheWrite": 0, "total": 0.00105 }
923
+ },
924
+ "stopReason": "stop",
925
+ "timestamp": 1733234567890
926
+ }
927
+ ```
928
+
929
+ Stop reasons: `"stop"`, `"length"`, `"toolUse"`, `"error"`, `"aborted"`
930
+
931
+ ### ToolResultMessage
932
+
933
+ ```json
934
+ {
935
+ "role": "toolResult",
936
+ "toolCallId": "call_123",
937
+ "toolName": "bash",
938
+ "content": [{ "type": "text", "text": "total 48\ndrwxr-xr-x ..." }],
939
+ "isError": false,
940
+ "timestamp": 1733234567890
941
+ }
942
+ ```
943
+
944
+ ### BashExecutionMessage
945
+
946
+ Created by the `bash` RPC command (not by LLM tool calls):
947
+
948
+ ```json
949
+ {
950
+ "role": "bashExecution",
951
+ "command": "ls -la",
952
+ "output": "total 48\ndrwxr-xr-x ...",
953
+ "exitCode": 0,
954
+ "cancelled": false,
955
+ "truncated": false,
956
+ "fullOutputPath": null,
957
+ "timestamp": 1733234567890
958
+ }
959
+ ```
960
+
961
+ ### Attachment
962
+
963
+ ```json
964
+ {
965
+ "id": "img1",
966
+ "type": "image",
967
+ "fileName": "photo.jpg",
968
+ "mimeType": "image/jpeg",
969
+ "size": 102400,
970
+ "content": "base64-encoded-data...",
971
+ "extractedText": null,
972
+ "preview": null
973
+ }
974
+ ```
975
+
976
+ ## Example: Basic Client (Python)
977
+
978
+ ```python
979
+ import subprocess
980
+ import json
981
+
982
+ proc = subprocess.Popen(
983
+ ["pi", "--mode", "rpc", "--no-session"],
984
+ stdin=subprocess.PIPE,
985
+ stdout=subprocess.PIPE,
986
+ text=True
987
+ )
988
+
989
+ def send(cmd):
990
+ proc.stdin.write(json.dumps(cmd) + "\n")
991
+ proc.stdin.flush()
992
+
993
+ def read_events():
994
+ for line in proc.stdout:
995
+ yield json.loads(line)
996
+
997
+ # Send prompt
998
+ send({"type": "prompt", "message": "Hello!"})
999
+
1000
+ # Process events
1001
+ for event in read_events():
1002
+ if event.get("type") == "message_update":
1003
+ delta = event.get("assistantMessageEvent", {})
1004
+ if delta.get("type") == "text_delta":
1005
+ print(delta["delta"], end="", flush=True)
1006
+
1007
+ if event.get("type") == "agent_end":
1008
+ print()
1009
+ break
1010
+ ```
1011
+
1012
+ ## Example: Interactive Client (Node.js)
1013
+
1014
+ See [`test/rpc-example.ts`](../test/rpc-example.ts) for a complete interactive example, or [`src/modes/rpc/rpc-client.ts`](../src/modes/rpc/rpc-client.ts) for a typed client implementation.
1015
+
1016
+ ```javascript
1017
+ const { spawn } = require("child_process");
1018
+ const readline = require("readline");
1019
+
1020
+ const agent = spawn("pi", ["--mode", "rpc", "--no-session"]);
1021
+
1022
+ readline.createInterface({ input: agent.stdout }).on("line", (line) => {
1023
+ const event = JSON.parse(line);
1024
+
1025
+ if (event.type === "message_update") {
1026
+ const { assistantMessageEvent } = event;
1027
+ if (assistantMessageEvent.type === "text_delta") {
1028
+ process.stdout.write(assistantMessageEvent.delta);
1029
+ }
1030
+ }
1031
+ });
1032
+
1033
+ // Send prompt
1034
+ agent.stdin.write(JSON.stringify({ type: "prompt", message: "Hello" }) + "\n");
1035
+
1036
+ // Abort on Ctrl+C
1037
+ process.on("SIGINT", () => {
1038
+ agent.stdin.write(JSON.stringify({ type: "abort" }) + "\n");
1039
+ });
1040
+ ```