@knightcodeai/cli-linux-arm64 0.9.1 → 0.9.2
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/bin/CHANGELOG.md +50 -0
- package/bin/README.md +52 -19
- package/bin/docs/cli-integration.md +106 -0
- package/bin/docs/cli.md +270 -0
- package/bin/docs/compaction.md +56 -37
- package/bin/docs/configuration.md +46 -0
- package/bin/docs/containerization.md +86 -54
- package/bin/docs/custom-provider.md +132 -785
- package/bin/docs/docs.json +143 -103
- package/bin/docs/environment-variables.md +5 -4
- package/bin/docs/extensions.md +134 -2956
- package/bin/docs/how-knightcode-works.md +49 -0
- package/bin/docs/index.md +24 -69
- package/bin/docs/json.md +193 -65
- package/bin/docs/keybindings.md +56 -101
- package/bin/docs/llama-cpp.md +3 -3
- package/bin/docs/message-types.md +261 -0
- package/bin/docs/models.md +64 -547
- package/bin/docs/packages.md +66 -167
- package/bin/docs/prompt-templates.md +31 -68
- package/bin/docs/providers.md +103 -241
- package/bin/docs/quickstart.md +61 -106
- package/bin/docs/rpc-commands.md +854 -0
- package/bin/docs/rpc-extension-ui.md +200 -0
- package/bin/docs/rpc.md +129 -1556
- package/bin/docs/sdk.md +76 -1160
- package/bin/docs/security.md +70 -32
- package/bin/docs/session-format.md +25 -216
- package/bin/docs/sessions.md +38 -143
- package/bin/docs/settings.md +111 -389
- package/bin/docs/shell-aliases.md +85 -5
- package/bin/docs/skills.md +51 -189
- package/bin/docs/slash-commands.md +63 -0
- package/bin/docs/terminal-setup.md +107 -79
- package/bin/docs/termux.md +74 -83
- package/bin/docs/themes.md +68 -280
- package/bin/docs/tmux.md +31 -39
- package/bin/docs/tui.md +69 -923
- package/bin/docs/usage.md +79 -286
- package/bin/docs/windows.md +43 -17
- package/bin/export-html/template.js +6 -1
- package/bin/knightcode +2 -2
- package/bin/package.json +6 -6
- package/package.json +1 -1
- package/bin/docs/development.md +0 -71
package/bin/docs/rpc.md
CHANGED
|
@@ -1,1618 +1,191 @@
|
|
|
1
1
|
# RPC Mode
|
|
2
2
|
|
|
3
|
-
RPC mode
|
|
3
|
+
RPC mode runs KnightCode as a long-lived subprocess controlled through JSON records on stdin and stdout. Use it for language-independent integrations, process isolation, IDEs, and custom user interfaces.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
For an in-process Node.js or Bun integration, prefer the [SDK](sdk.md). For a subprocess-based TypeScript integration, prefer the exported `RpcClient`, which starts KnightCode, correlates responses, exposes typed command methods, and delivers events to listeners.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
| Interface | Process boundary | Control model | Best fit |
|
|
8
|
+
|---|---|---|---|
|
|
9
|
+
| [SDK](sdk.md) | In process | Direct TypeScript methods and events | Node.js or Bun hosts that want complete API access |
|
|
10
|
+
| RPC | Child process | JSONL commands, responses, and events | Other languages, isolated processes, IDEs, or custom clients |
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
knightcode --mode rpc [options]
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Common options:
|
|
14
|
-
- `--provider <name>`: Set the LLM provider (anthropic, openai, google, etc.)
|
|
15
|
-
- `--model <pattern>`: Model pattern or ID (supports `provider/id` and optional `:<thinking>`)
|
|
16
|
-
- `--name <name>` / `-n <name>`: Set the session display name at startup
|
|
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`. `bash_execution_update` events also include the `id` of their originating `bash` command.
|
|
27
|
-
|
|
28
|
-
### Framing
|
|
29
|
-
|
|
30
|
-
RPC mode uses strict JSONL semantics with LF (`\n`) as the only record delimiter.
|
|
31
|
-
|
|
32
|
-
This matters for clients:
|
|
33
|
-
- Split records on `\n` only
|
|
34
|
-
- Accept optional `\r\n` input by stripping a trailing `\r`
|
|
35
|
-
- Do not use generic line readers that treat Unicode separators as newlines
|
|
36
|
-
|
|
37
|
-
In particular, Node `readline` is not protocol-compliant for RPC mode because it also splits on `U+2028` and `U+2029`, which are valid inside JSON strings.
|
|
38
|
-
|
|
39
|
-
## Commands
|
|
40
|
-
|
|
41
|
-
### Prompting
|
|
42
|
-
|
|
43
|
-
#### prompt
|
|
44
|
-
|
|
45
|
-
Send a user prompt to the agent. The command response is emitted after the prompt is accepted, queued, or handled. Events continue streaming asynchronously after acceptance.
|
|
46
|
-
|
|
47
|
-
```json
|
|
48
|
-
{"id": "req-1", "type": "prompt", "message": "Hello, world!"}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
With images:
|
|
52
|
-
```json
|
|
53
|
-
{"type": "prompt", "message": "What's in this image?", "images": [{"type": "image", "data": "base64-encoded-data", "mimeType": "image/png"}]}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
**During streaming**: If the agent is already streaming, you must specify `streamingBehavior` to queue the message:
|
|
57
|
-
|
|
58
|
-
```json
|
|
59
|
-
{"type": "prompt", "message": "New instruction", "streamingBehavior": "steer"}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
- `"steer"`: Queue the message while the agent is running. It is delivered after the current assistant turn finishes executing its tool calls, before the next LLM call.
|
|
63
|
-
- `"followUp"`: Wait until the agent finishes. Message is delivered only when agent stops.
|
|
64
|
-
|
|
65
|
-
If the agent is streaming and no `streamingBehavior` is specified, the command returns an error.
|
|
66
|
-
|
|
67
|
-
**Extension commands**: If the message is an extension command (e.g., `/mycommand`), it executes immediately even during streaming. Extension commands manage their own LLM interaction via `knightcode.sendMessage()`.
|
|
68
|
-
|
|
69
|
-
**Input expansion**: Skill commands (`/skill:name`) and prompt templates (`/template`) are expanded before sending/queueing.
|
|
70
|
-
|
|
71
|
-
Response:
|
|
72
|
-
```json
|
|
73
|
-
{"id": "req-1", "type": "response", "command": "prompt", "success": true}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
`success: true` means the prompt was accepted, queued, or handled immediately. `success: false` means the prompt was rejected before acceptance. Failures after acceptance are reported through the normal event and message stream, not as a second `response` for the same request id.
|
|
77
|
-
|
|
78
|
-
The `images` field is optional. Each image uses `ImageContent` format: `{"type": "image", "data": "base64-encoded-data", "mimeType": "image/png"}`.
|
|
79
|
-
|
|
80
|
-
#### steer
|
|
81
|
-
|
|
82
|
-
Queue a steering message while the agent is running. It is delivered after the current assistant turn finishes executing its tool calls, before the next LLM call. Skill commands and prompt templates are expanded. Extension commands are not allowed (use `prompt` instead).
|
|
83
|
-
|
|
84
|
-
```json
|
|
85
|
-
{"type": "steer", "message": "Stop and do this instead"}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
With images:
|
|
89
|
-
```json
|
|
90
|
-
{"type": "steer", "message": "Look at this instead", "images": [{"type": "image", "data": "base64-encoded-data", "mimeType": "image/png"}]}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
The `images` field is optional. Each image uses `ImageContent` format (same as `prompt`).
|
|
94
|
-
|
|
95
|
-
Response:
|
|
96
|
-
```json
|
|
97
|
-
{"type": "response", "command": "steer", "success": true}
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
See [set_steering_mode](#set_steering_mode) for controlling how steering messages are processed.
|
|
101
|
-
|
|
102
|
-
#### follow_up
|
|
103
|
-
|
|
104
|
-
Queue a follow-up message to be processed after the agent finishes. Delivered only when agent has no more tool calls or steering messages. Skill commands and prompt templates are expanded. Extension commands are not allowed (use `prompt` instead).
|
|
105
|
-
|
|
106
|
-
```json
|
|
107
|
-
{"type": "follow_up", "message": "After you're done, also do this"}
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
With images:
|
|
111
|
-
```json
|
|
112
|
-
{"type": "follow_up", "message": "Also check this image", "images": [{"type": "image", "data": "base64-encoded-data", "mimeType": "image/png"}]}
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
The `images` field is optional. Each image uses `ImageContent` format (same as `prompt`).
|
|
116
|
-
|
|
117
|
-
Response:
|
|
118
|
-
```json
|
|
119
|
-
{"type": "response", "command": "follow_up", "success": true}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
See [set_follow_up_mode](#set_follow_up_mode) for controlling how follow-up messages are processed.
|
|
123
|
-
|
|
124
|
-
#### abort
|
|
125
|
-
|
|
126
|
-
Abort the current operation and wait for the session to become idle before responding.
|
|
127
|
-
|
|
128
|
-
```json
|
|
129
|
-
{"type": "abort"}
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
Response:
|
|
133
|
-
```json
|
|
134
|
-
{"type": "response", "command": "abort", "success": true}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
#### clear_queue
|
|
138
|
-
|
|
139
|
-
Remove queued steering and follow-up messages and return their text.
|
|
140
|
-
|
|
141
|
-
```json
|
|
142
|
-
{"type": "clear_queue"}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Response:
|
|
146
|
-
```json
|
|
147
|
-
{
|
|
148
|
-
"type": "response",
|
|
149
|
-
"command": "clear_queue",
|
|
150
|
-
"success": true,
|
|
151
|
-
"data": {
|
|
152
|
-
"steering": ["Change direction"],
|
|
153
|
-
"followUp": ["Summarize when finished"]
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
To implement interactive Esc behavior, send `clear_queue` before `abort`, then restore the returned text in the client editor. `abort` continues queued messages when they remain in the session.
|
|
159
|
-
|
|
160
|
-
#### new_session
|
|
161
|
-
|
|
162
|
-
Start a fresh session. Can be cancelled by a `session_before_switch` extension event handler.
|
|
163
|
-
|
|
164
|
-
```json
|
|
165
|
-
{"type": "new_session"}
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
With optional parent session tracking:
|
|
169
|
-
```json
|
|
170
|
-
{"type": "new_session", "parentSession": "/path/to/parent-session.jsonl"}
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
Response:
|
|
174
|
-
```json
|
|
175
|
-
{"type": "response", "command": "new_session", "success": true, "data": {"cancelled": false}}
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
If an extension cancelled:
|
|
179
|
-
```json
|
|
180
|
-
{"type": "response", "command": "new_session", "success": true, "data": {"cancelled": true}}
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### State
|
|
184
|
-
|
|
185
|
-
#### get_state
|
|
186
|
-
|
|
187
|
-
Get current session state.
|
|
188
|
-
|
|
189
|
-
```json
|
|
190
|
-
{"type": "get_state"}
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
Response:
|
|
194
|
-
```json
|
|
195
|
-
{
|
|
196
|
-
"type": "response",
|
|
197
|
-
"command": "get_state",
|
|
198
|
-
"success": true,
|
|
199
|
-
"data": {
|
|
200
|
-
"model": {...},
|
|
201
|
-
"thinkingLevel": "medium",
|
|
202
|
-
"isStreaming": false,
|
|
203
|
-
"isCompacting": false,
|
|
204
|
-
"steeringMode": "all",
|
|
205
|
-
"followUpMode": "one-at-a-time",
|
|
206
|
-
"sessionFile": "/path/to/session.jsonl",
|
|
207
|
-
"sessionId": "abc123",
|
|
208
|
-
"sessionName": "my-feature-work",
|
|
209
|
-
"autoCompactionEnabled": true,
|
|
210
|
-
"messageCount": 5,
|
|
211
|
-
"pendingMessageCount": 0
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
The `model` field is a full [Model](#model) object or `null`. The `sessionName` field is the display name set via `set_session_name`, or omitted if not set.
|
|
217
|
-
|
|
218
|
-
#### get_messages
|
|
219
|
-
|
|
220
|
-
Get all messages in the conversation.
|
|
221
|
-
|
|
222
|
-
```json
|
|
223
|
-
{"type": "get_messages"}
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
Response:
|
|
227
|
-
```json
|
|
228
|
-
{
|
|
229
|
-
"type": "response",
|
|
230
|
-
"command": "get_messages",
|
|
231
|
-
"success": true,
|
|
232
|
-
"data": {"messages": [...]}
|
|
233
|
-
}
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
Messages are `AgentMessage` objects (see [Message Types](#message-types)).
|
|
237
|
-
|
|
238
|
-
### Model
|
|
239
|
-
|
|
240
|
-
#### set_model
|
|
241
|
-
|
|
242
|
-
Switch to a specific model.
|
|
243
|
-
|
|
244
|
-
```json
|
|
245
|
-
{"type": "set_model", "provider": "anthropic", "modelId": "claude-sonnet-4-20250514"}
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
Response contains the full [Model](#model) object:
|
|
249
|
-
```json
|
|
250
|
-
{
|
|
251
|
-
"type": "response",
|
|
252
|
-
"command": "set_model",
|
|
253
|
-
"success": true,
|
|
254
|
-
"data": {...}
|
|
255
|
-
}
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
#### cycle_model
|
|
259
|
-
|
|
260
|
-
Cycle to the next available model. Returns `null` data if only one model available.
|
|
261
|
-
|
|
262
|
-
```json
|
|
263
|
-
{"type": "cycle_model"}
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
Response:
|
|
267
|
-
```json
|
|
268
|
-
{
|
|
269
|
-
"type": "response",
|
|
270
|
-
"command": "cycle_model",
|
|
271
|
-
"success": true,
|
|
272
|
-
"data": {
|
|
273
|
-
"model": {...},
|
|
274
|
-
"thinkingLevel": "medium",
|
|
275
|
-
"isScoped": false
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
The `model` field is a full [Model](#model) object.
|
|
281
|
-
|
|
282
|
-
#### get_available_models
|
|
283
|
-
|
|
284
|
-
List all configured models.
|
|
285
|
-
|
|
286
|
-
```json
|
|
287
|
-
{"type": "get_available_models"}
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
Response contains an array of full [Model](#model) objects:
|
|
291
|
-
```json
|
|
292
|
-
{
|
|
293
|
-
"type": "response",
|
|
294
|
-
"command": "get_available_models",
|
|
295
|
-
"success": true,
|
|
296
|
-
"data": {
|
|
297
|
-
"models": [...]
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
### Thinking
|
|
303
|
-
|
|
304
|
-
#### set_thinking_level
|
|
305
|
-
|
|
306
|
-
Set the reasoning/thinking level for models that support it.
|
|
307
|
-
|
|
308
|
-
```json
|
|
309
|
-
{"type": "set_thinking_level", "level": "high"}
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
Levels: `"off"`, `"minimal"`, `"low"`, `"medium"`, `"high"`, `"xhigh"`, `"max"`
|
|
313
|
-
|
|
314
|
-
`"xhigh"` and `"max"` are exposed only when supported by the selected model. Some models, including GPT-5.6, expose both.
|
|
315
|
-
|
|
316
|
-
Response:
|
|
317
|
-
```json
|
|
318
|
-
{"type": "response", "command": "set_thinking_level", "success": true}
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
#### cycle_thinking_level
|
|
322
|
-
|
|
323
|
-
Cycle through available thinking levels. Returns `null` data if model doesn't support thinking.
|
|
324
|
-
|
|
325
|
-
```json
|
|
326
|
-
{"type": "cycle_thinking_level"}
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
Response:
|
|
330
|
-
```json
|
|
331
|
-
{
|
|
332
|
-
"type": "response",
|
|
333
|
-
"command": "cycle_thinking_level",
|
|
334
|
-
"success": true,
|
|
335
|
-
"data": {"level": "high"}
|
|
336
|
-
}
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
#### get_available_thinking_levels
|
|
340
|
-
|
|
341
|
-
List the thinking levels supported by the current model. Returns `["off"]` for a model without reasoning support.
|
|
342
|
-
|
|
343
|
-
```json
|
|
344
|
-
{"type": "get_available_thinking_levels"}
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
Response:
|
|
348
|
-
```json
|
|
349
|
-
{
|
|
350
|
-
"type": "response",
|
|
351
|
-
"command": "get_available_thinking_levels",
|
|
352
|
-
"success": true,
|
|
353
|
-
"data": {
|
|
354
|
-
"levels": ["off", "minimal", "low", "medium", "high"]
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
```
|
|
358
|
-
|
|
359
|
-
### Queue Modes
|
|
360
|
-
|
|
361
|
-
#### set_steering_mode
|
|
362
|
-
|
|
363
|
-
Control how steering messages (from `steer`) are delivered.
|
|
364
|
-
|
|
365
|
-
```json
|
|
366
|
-
{"type": "set_steering_mode", "mode": "one-at-a-time"}
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
Modes:
|
|
370
|
-
- `"all"`: Deliver all steering messages after the current assistant turn finishes executing its tool calls
|
|
371
|
-
- `"one-at-a-time"`: Deliver one steering message per completed assistant turn (default)
|
|
372
|
-
|
|
373
|
-
Response:
|
|
374
|
-
```json
|
|
375
|
-
{"type": "response", "command": "set_steering_mode", "success": true}
|
|
376
|
-
```
|
|
377
|
-
|
|
378
|
-
#### set_follow_up_mode
|
|
379
|
-
|
|
380
|
-
Control how follow-up messages (from `follow_up`) are delivered.
|
|
381
|
-
|
|
382
|
-
```json
|
|
383
|
-
{"type": "set_follow_up_mode", "mode": "one-at-a-time"}
|
|
384
|
-
```
|
|
385
|
-
|
|
386
|
-
Modes:
|
|
387
|
-
- `"all"`: Deliver all follow-up messages when agent finishes
|
|
388
|
-
- `"one-at-a-time"`: Deliver one follow-up message per agent completion (default)
|
|
389
|
-
|
|
390
|
-
Response:
|
|
391
|
-
```json
|
|
392
|
-
{"type": "response", "command": "set_follow_up_mode", "success": true}
|
|
393
|
-
```
|
|
394
|
-
|
|
395
|
-
### Compaction
|
|
396
|
-
|
|
397
|
-
#### compact
|
|
398
|
-
|
|
399
|
-
Manually compact conversation context to reduce token usage.
|
|
400
|
-
|
|
401
|
-
```json
|
|
402
|
-
{"type": "compact"}
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
With custom instructions:
|
|
406
|
-
```json
|
|
407
|
-
{"type": "compact", "customInstructions": "Focus on code changes"}
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
Response:
|
|
411
|
-
```json
|
|
412
|
-
{
|
|
413
|
-
"type": "response",
|
|
414
|
-
"command": "compact",
|
|
415
|
-
"success": true,
|
|
416
|
-
"data": {
|
|
417
|
-
"summary": "Summary of conversation...",
|
|
418
|
-
"firstKeptEntryId": "abc123",
|
|
419
|
-
"tokensBefore": 150000,
|
|
420
|
-
"estimatedTokensAfter": 32000,
|
|
421
|
-
"usage": {
|
|
422
|
-
"input": 32000,
|
|
423
|
-
"output": 1200,
|
|
424
|
-
"cacheRead": 0,
|
|
425
|
-
"cacheWrite": 0,
|
|
426
|
-
"totalTokens": 33200,
|
|
427
|
-
"cost": {"input": 0.01, "output": 0.02, "cacheRead": 0, "cacheWrite": 0, "total": 0.03}
|
|
428
|
-
},
|
|
429
|
-
"details": {}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
```
|
|
433
|
-
|
|
434
|
-
`estimatedTokensAfter` is a heuristic estimate over the rebuilt message context immediately after compaction, not a provider-exact token count. `usage` reports the LLM call or calls that generated the summary and may be omitted by custom compaction handlers.
|
|
435
|
-
|
|
436
|
-
#### set_auto_compaction
|
|
437
|
-
|
|
438
|
-
Enable or disable automatic compaction when context is nearly full.
|
|
439
|
-
|
|
440
|
-
```json
|
|
441
|
-
{"type": "set_auto_compaction", "enabled": true}
|
|
442
|
-
```
|
|
443
|
-
|
|
444
|
-
Response:
|
|
445
|
-
```json
|
|
446
|
-
{"type": "response", "command": "set_auto_compaction", "success": true}
|
|
447
|
-
```
|
|
448
|
-
|
|
449
|
-
### Retry
|
|
450
|
-
|
|
451
|
-
#### set_auto_retry
|
|
452
|
-
|
|
453
|
-
Enable or disable automatic retry on transient errors (overloaded, rate limit, 5xx).
|
|
454
|
-
|
|
455
|
-
```json
|
|
456
|
-
{"type": "set_auto_retry", "enabled": true}
|
|
457
|
-
```
|
|
458
|
-
|
|
459
|
-
Response:
|
|
460
|
-
```json
|
|
461
|
-
{"type": "response", "command": "set_auto_retry", "success": true}
|
|
462
|
-
```
|
|
463
|
-
|
|
464
|
-
#### abort_retry
|
|
465
|
-
|
|
466
|
-
Abort an in-progress retry (cancel the delay and stop retrying).
|
|
467
|
-
|
|
468
|
-
```json
|
|
469
|
-
{"type": "abort_retry"}
|
|
470
|
-
```
|
|
471
|
-
|
|
472
|
-
Response:
|
|
473
|
-
```json
|
|
474
|
-
{"type": "response", "command": "abort_retry", "success": true}
|
|
475
|
-
```
|
|
476
|
-
|
|
477
|
-
### Bash
|
|
478
|
-
|
|
479
|
-
#### bash
|
|
480
|
-
|
|
481
|
-
Execute a shell command and add output to conversation context. Output streams as `bash_execution_update` events while the command runs; the response contains the final result.
|
|
482
|
-
|
|
483
|
-
```json
|
|
484
|
-
{"id": "req-1", "type": "bash", "command": "ls -la"}
|
|
485
|
-
```
|
|
486
|
-
|
|
487
|
-
Include an `id` to associate streamed `bash_execution_update` events with this command.
|
|
488
|
-
|
|
489
|
-
Response:
|
|
490
|
-
```json
|
|
491
|
-
{
|
|
492
|
-
"id": "req-1",
|
|
493
|
-
"type": "response",
|
|
494
|
-
"command": "bash",
|
|
495
|
-
"success": true,
|
|
496
|
-
"data": {
|
|
497
|
-
"output": "total 48\ndrwxr-xr-x ...",
|
|
498
|
-
"exitCode": 0,
|
|
499
|
-
"cancelled": false,
|
|
500
|
-
"truncated": false
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
```
|
|
504
|
-
|
|
505
|
-
If output was truncated, includes `fullOutputPath`:
|
|
506
|
-
```json
|
|
507
|
-
{
|
|
508
|
-
"type": "response",
|
|
509
|
-
"command": "bash",
|
|
510
|
-
"success": true,
|
|
511
|
-
"data": {
|
|
512
|
-
"output": "truncated output...",
|
|
513
|
-
"exitCode": 0,
|
|
514
|
-
"cancelled": false,
|
|
515
|
-
"truncated": true,
|
|
516
|
-
"fullOutputPath": "/tmp/knightcode-bash-abc123.log"
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
```
|
|
520
|
-
|
|
521
|
-
**How bash results reach the LLM:**
|
|
522
|
-
|
|
523
|
-
The `bash` command executes immediately and returns a `BashResult`. Internally, a `BashExecutionMessage` is created and stored in the agent's message state.
|
|
524
|
-
|
|
525
|
-
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:
|
|
526
|
-
|
|
527
|
-
````
|
|
528
|
-
Ran `ls -la`
|
|
529
|
-
```
|
|
530
|
-
total 48
|
|
531
|
-
drwxr-xr-x ...
|
|
532
|
-
```
|
|
533
|
-
````
|
|
534
|
-
|
|
535
|
-
This means:
|
|
536
|
-
1. Bash output is included in the LLM context on the **next prompt**, not immediately
|
|
537
|
-
2. Multiple bash commands can be executed before a prompt; all outputs will be included
|
|
538
|
-
|
|
539
|
-
#### abort_bash
|
|
540
|
-
|
|
541
|
-
Abort a running bash command.
|
|
542
|
-
|
|
543
|
-
```json
|
|
544
|
-
{"type": "abort_bash"}
|
|
545
|
-
```
|
|
546
|
-
|
|
547
|
-
Response:
|
|
548
|
-
```json
|
|
549
|
-
{"type": "response", "command": "abort_bash", "success": true}
|
|
550
|
-
```
|
|
551
|
-
|
|
552
|
-
### Session
|
|
553
|
-
|
|
554
|
-
#### get_session_stats
|
|
555
|
-
|
|
556
|
-
Get token usage, cost statistics, and current context window usage.
|
|
557
|
-
|
|
558
|
-
```json
|
|
559
|
-
{"type": "get_session_stats"}
|
|
560
|
-
```
|
|
561
|
-
|
|
562
|
-
Response:
|
|
563
|
-
```json
|
|
564
|
-
{
|
|
565
|
-
"type": "response",
|
|
566
|
-
"command": "get_session_stats",
|
|
567
|
-
"success": true,
|
|
568
|
-
"data": {
|
|
569
|
-
"sessionFile": "/path/to/session.jsonl",
|
|
570
|
-
"sessionId": "abc123",
|
|
571
|
-
"userMessages": 5,
|
|
572
|
-
"assistantMessages": 5,
|
|
573
|
-
"toolCalls": 12,
|
|
574
|
-
"toolResults": 12,
|
|
575
|
-
"totalMessages": 22,
|
|
576
|
-
"tokens": {
|
|
577
|
-
"input": 50000,
|
|
578
|
-
"output": 10000,
|
|
579
|
-
"cacheRead": 40000,
|
|
580
|
-
"cacheWrite": 5000,
|
|
581
|
-
"total": 105000
|
|
582
|
-
},
|
|
583
|
-
"cost": 0.45,
|
|
584
|
-
"contextUsage": {
|
|
585
|
-
"tokens": 60000,
|
|
586
|
-
"contextWindow": 200000,
|
|
587
|
-
"percent": 30
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
```
|
|
592
|
-
|
|
593
|
-
`tokens` and `cost` include assistant messages, usage reported by tools, and compaction/branch-summary generation across the full session. `contextUsage` contains the actual current context-window estimate used for compaction and footer display.
|
|
594
|
-
|
|
595
|
-
`contextUsage` is omitted when no model or context window is available. `contextUsage.tokens` and `contextUsage.percent` are `null` immediately after compaction until a fresh post-compaction assistant response provides valid usage data.
|
|
596
|
-
|
|
597
|
-
#### export_html
|
|
598
|
-
|
|
599
|
-
Export session to an HTML file.
|
|
600
|
-
|
|
601
|
-
```json
|
|
602
|
-
{"type": "export_html"}
|
|
603
|
-
```
|
|
604
|
-
|
|
605
|
-
With custom path:
|
|
606
|
-
```json
|
|
607
|
-
{"type": "export_html", "outputPath": "/tmp/session.html"}
|
|
608
|
-
```
|
|
609
|
-
|
|
610
|
-
Response:
|
|
611
|
-
```json
|
|
612
|
-
{
|
|
613
|
-
"type": "response",
|
|
614
|
-
"command": "export_html",
|
|
615
|
-
"success": true,
|
|
616
|
-
"data": {"path": "/tmp/session.html"}
|
|
617
|
-
}
|
|
618
|
-
```
|
|
619
|
-
|
|
620
|
-
#### switch_session
|
|
621
|
-
|
|
622
|
-
Load a different session file. Can be cancelled by a `session_before_switch` extension event handler.
|
|
623
|
-
|
|
624
|
-
```json
|
|
625
|
-
{"type": "switch_session", "sessionPath": "/path/to/session.jsonl"}
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
Response:
|
|
629
|
-
```json
|
|
630
|
-
{"type": "response", "command": "switch_session", "success": true, "data": {"cancelled": false}}
|
|
631
|
-
```
|
|
632
|
-
|
|
633
|
-
If an extension cancelled the switch:
|
|
634
|
-
```json
|
|
635
|
-
{"type": "response", "command": "switch_session", "success": true, "data": {"cancelled": true}}
|
|
636
|
-
```
|
|
637
|
-
|
|
638
|
-
#### fork
|
|
639
|
-
|
|
640
|
-
Create a new fork from a previous user message on the active branch. Can be cancelled by a `session_before_fork` extension event handler. Returns the text of the message being forked from.
|
|
641
|
-
|
|
642
|
-
```json
|
|
643
|
-
{"type": "fork", "entryId": "abc123"}
|
|
644
|
-
```
|
|
645
|
-
|
|
646
|
-
Response:
|
|
647
|
-
```json
|
|
648
|
-
{
|
|
649
|
-
"type": "response",
|
|
650
|
-
"command": "fork",
|
|
651
|
-
"success": true,
|
|
652
|
-
"data": {"text": "The original prompt text...", "cancelled": false}
|
|
653
|
-
}
|
|
654
|
-
```
|
|
655
|
-
|
|
656
|
-
If an extension cancelled the fork:
|
|
657
|
-
```json
|
|
658
|
-
{
|
|
659
|
-
"type": "response",
|
|
660
|
-
"command": "fork",
|
|
661
|
-
"success": true,
|
|
662
|
-
"data": {"text": "The original prompt text...", "cancelled": true}
|
|
663
|
-
}
|
|
664
|
-
```
|
|
665
|
-
|
|
666
|
-
#### clone
|
|
667
|
-
|
|
668
|
-
Duplicate the current active branch into a new session at the current position. Can be cancelled by a `session_before_fork` extension event handler.
|
|
669
|
-
|
|
670
|
-
```json
|
|
671
|
-
{"type": "clone"}
|
|
672
|
-
```
|
|
673
|
-
|
|
674
|
-
Response:
|
|
675
|
-
```json
|
|
676
|
-
{
|
|
677
|
-
"type": "response",
|
|
678
|
-
"command": "clone",
|
|
679
|
-
"success": true,
|
|
680
|
-
"data": {"cancelled": false}
|
|
681
|
-
}
|
|
682
|
-
```
|
|
683
|
-
|
|
684
|
-
If an extension cancelled the clone:
|
|
685
|
-
```json
|
|
686
|
-
{
|
|
687
|
-
"type": "response",
|
|
688
|
-
"command": "clone",
|
|
689
|
-
"success": true,
|
|
690
|
-
"data": {"cancelled": true}
|
|
691
|
-
}
|
|
692
|
-
```
|
|
693
|
-
|
|
694
|
-
#### get_fork_messages
|
|
695
|
-
|
|
696
|
-
Get user messages available for forking.
|
|
697
|
-
|
|
698
|
-
```json
|
|
699
|
-
{"type": "get_fork_messages"}
|
|
700
|
-
```
|
|
12
|
+
## Start RPC mode
|
|
701
13
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
{
|
|
705
|
-
"type": "response",
|
|
706
|
-
"command": "get_fork_messages",
|
|
707
|
-
"success": true,
|
|
708
|
-
"data": {
|
|
709
|
-
"messages": [
|
|
710
|
-
{"entryId": "abc123", "text": "First prompt..."},
|
|
711
|
-
{"entryId": "def456", "text": "Second prompt..."}
|
|
712
|
-
]
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
```
|
|
716
|
-
|
|
717
|
-
#### get_entries
|
|
718
|
-
|
|
719
|
-
Get all session entries in append order (excluding the session header). The session is an append-only tree of entries with stable ids, so an entry id works as a durable cursor: pass the last entry id you have seen as `since` to get only entries strictly after it, even across client restarts. Unlike `get_messages`, this includes pre-compaction history and abandoned branches.
|
|
720
|
-
|
|
721
|
-
```json
|
|
722
|
-
{"type": "get_entries"}
|
|
723
|
-
```
|
|
724
|
-
|
|
725
|
-
With a cursor:
|
|
726
|
-
```json
|
|
727
|
-
{"type": "get_entries", "since": "abc123"}
|
|
728
|
-
```
|
|
729
|
-
|
|
730
|
-
Response:
|
|
731
|
-
```json
|
|
732
|
-
{
|
|
733
|
-
"type": "response",
|
|
734
|
-
"command": "get_entries",
|
|
735
|
-
"success": true,
|
|
736
|
-
"data": {
|
|
737
|
-
"entries": [
|
|
738
|
-
{"type": "message", "id": "def456", "parentId": "abc123", "timestamp": "...", "message": {"role": "user", "...": "..."}}
|
|
739
|
-
],
|
|
740
|
-
"leafId": "def456"
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
```
|
|
744
|
-
|
|
745
|
-
`leafId` is the id of the current leaf entry (`null` for an empty session), so a client can tell in one round trip whether the active branch moved. If `since` does not match any entry id, the response is `success: false`.
|
|
746
|
-
|
|
747
|
-
#### get_tree
|
|
748
|
-
|
|
749
|
-
Get the session as a tree of entries. Each node is `{entry, children, label?, labelTimestamp?}`. A well-formed session has a single root; orphaned entries (broken parent chain) also appear as roots.
|
|
750
|
-
|
|
751
|
-
```json
|
|
752
|
-
{"type": "get_tree"}
|
|
753
|
-
```
|
|
754
|
-
|
|
755
|
-
Response:
|
|
756
|
-
```json
|
|
757
|
-
{
|
|
758
|
-
"type": "response",
|
|
759
|
-
"command": "get_tree",
|
|
760
|
-
"success": true,
|
|
761
|
-
"data": {
|
|
762
|
-
"tree": [
|
|
763
|
-
{
|
|
764
|
-
"entry": {"type": "message", "id": "abc123", "parentId": null, "...": "..."},
|
|
765
|
-
"children": [
|
|
766
|
-
{"entry": {"type": "message", "id": "def456", "parentId": "abc123", "...": "..."}, "children": []}
|
|
767
|
-
]
|
|
768
|
-
}
|
|
769
|
-
],
|
|
770
|
-
"leafId": "def456"
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
```
|
|
774
|
-
|
|
775
|
-
#### get_last_assistant_text
|
|
776
|
-
|
|
777
|
-
Get the text content of the last assistant message.
|
|
778
|
-
|
|
779
|
-
```json
|
|
780
|
-
{"type": "get_last_assistant_text"}
|
|
781
|
-
```
|
|
782
|
-
|
|
783
|
-
Response:
|
|
784
|
-
```json
|
|
785
|
-
{
|
|
786
|
-
"type": "response",
|
|
787
|
-
"command": "get_last_assistant_text",
|
|
788
|
-
"success": true,
|
|
789
|
-
"data": {"text": "The assistant's response..."}
|
|
790
|
-
}
|
|
791
|
-
```
|
|
792
|
-
|
|
793
|
-
Returns `{"text": null}` if no assistant messages exist.
|
|
794
|
-
|
|
795
|
-
#### set_session_name
|
|
796
|
-
|
|
797
|
-
Set a display name for the current session. The name appears in session listings and helps identify sessions.
|
|
798
|
-
|
|
799
|
-
```json
|
|
800
|
-
{"type": "set_session_name", "name": "my-feature-work"}
|
|
801
|
-
```
|
|
802
|
-
|
|
803
|
-
Response:
|
|
804
|
-
```json
|
|
805
|
-
{
|
|
806
|
-
"type": "response",
|
|
807
|
-
"command": "set_session_name",
|
|
808
|
-
"success": true
|
|
809
|
-
}
|
|
810
|
-
```
|
|
811
|
-
|
|
812
|
-
The current session name is available via `get_state` in the `sessionName` field. To set the initial name when starting RPC mode, pass `--name <name>` or `-n <name>` to the `knightcode --mode rpc` process.
|
|
813
|
-
|
|
814
|
-
### Commands
|
|
815
|
-
|
|
816
|
-
#### get_commands
|
|
817
|
-
|
|
818
|
-
Get available commands (extension commands, prompt templates, and skills). These can be invoked via the `prompt` command by prefixing with `/`.
|
|
819
|
-
|
|
820
|
-
```json
|
|
821
|
-
{"type": "get_commands"}
|
|
822
|
-
```
|
|
823
|
-
|
|
824
|
-
Response:
|
|
825
|
-
```json
|
|
826
|
-
{
|
|
827
|
-
"type": "response",
|
|
828
|
-
"command": "get_commands",
|
|
829
|
-
"success": true,
|
|
830
|
-
"data": {
|
|
831
|
-
"commands": [
|
|
832
|
-
{"name": "session-name", "description": "Set or clear session name", "source": "extension", "path": "/home/user/.knightcode/agent/extensions/session.ts"},
|
|
833
|
-
{"name": "fix-tests", "description": "Fix failing tests", "source": "prompt", "location": "project", "path": "/home/user/myproject/.knightcode/agent/prompts/fix-tests.md"},
|
|
834
|
-
{"name": "skill:brave-search", "description": "Web search via Brave API", "source": "skill", "location": "user", "path": "/home/user/.knightcode/agent/skills/brave-search/SKILL.md"}
|
|
835
|
-
]
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
```
|
|
839
|
-
|
|
840
|
-
Each command has:
|
|
841
|
-
- `name`: Command name (invoke with `/name`)
|
|
842
|
-
- `description`: Human-readable description (optional for extension commands)
|
|
843
|
-
- `source`: What kind of command:
|
|
844
|
-
- `"extension"`: Registered via `knightcode.registerCommand()` in an extension
|
|
845
|
-
- `"prompt"`: Loaded from a prompt template `.md` file
|
|
846
|
-
- `"skill"`: Loaded from a skill directory (name is prefixed with `skill:`)
|
|
847
|
-
- `location`: Where it was loaded from (optional, not present for extensions):
|
|
848
|
-
- `"user"`: User-level (`~/.knightcode/agent/`)
|
|
849
|
-
- `"project"`: Project-level (`./.knightcode/agent/`)
|
|
850
|
-
- `"path"`: Explicit path via CLI or settings
|
|
851
|
-
- `path`: Absolute file path to the command source (optional)
|
|
852
|
-
|
|
853
|
-
**Note**: Built-in TUI commands (`/settings`, `/hotkeys`, etc.) are not included. They are handled only in interactive mode and would not execute if sent via `prompt`.
|
|
854
|
-
|
|
855
|
-
## Events
|
|
856
|
-
|
|
857
|
-
Events are streamed to stdout as JSON lines during agent operation. Events do not generally include an `id` field; `bash_execution_update` includes the `id` of its originating `bash` command when one was provided.
|
|
858
|
-
|
|
859
|
-
### Event Types
|
|
860
|
-
|
|
861
|
-
| Event | Description |
|
|
862
|
-
|-------|-------------|
|
|
863
|
-
| `agent_start` | Agent begins processing |
|
|
864
|
-
| `agent_end` | One low-level agent run completes (may still be followed by retry, compaction, or queued continuations) |
|
|
865
|
-
| `agent_settled` | Agent run is fully settled; no automatic retry, compaction retry, or queued continuation remains |
|
|
866
|
-
| `turn_start` | New turn begins |
|
|
867
|
-
| `turn_end` | Turn completes (includes assistant message and tool results) |
|
|
868
|
-
| `message_start` | Message begins |
|
|
869
|
-
| `message_update` | Streaming update (text/thinking/toolcall deltas) |
|
|
870
|
-
| `message_end` | Message completes |
|
|
871
|
-
| `bash_execution_update` | Direct RPC bash command output chunk |
|
|
872
|
-
| `tool_execution_start` | Tool begins execution |
|
|
873
|
-
| `tool_execution_update` | Tool execution progress (streaming output) |
|
|
874
|
-
| `tool_execution_end` | Tool completes |
|
|
875
|
-
| `queue_update` | Pending steering/follow-up queue changed |
|
|
876
|
-
| `compaction_start` | Compaction begins |
|
|
877
|
-
| `compaction_end` | Compaction completes |
|
|
878
|
-
| `auto_retry_start` | Auto-retry begins (after transient error) |
|
|
879
|
-
| `auto_retry_end` | Auto-retry completes (success or final failure) |
|
|
880
|
-
| `summarization_retry_scheduled` | Retry scheduled for a transient compaction or branch-summary summarization error |
|
|
881
|
-
| `summarization_retry_attempt_start` | Retried summarization request starts |
|
|
882
|
-
| `summarization_retry_finished` | Summarization retry loop completes |
|
|
883
|
-
| `extension_error` | Extension threw an error |
|
|
884
|
-
|
|
885
|
-
### agent_start
|
|
886
|
-
|
|
887
|
-
Emitted when the agent begins processing a prompt.
|
|
888
|
-
|
|
889
|
-
```json
|
|
890
|
-
{"type": "agent_start"}
|
|
891
|
-
```
|
|
892
|
-
|
|
893
|
-
### agent_end
|
|
894
|
-
|
|
895
|
-
Emitted when one low-level agent run completes. Contains all messages generated during this run. If `willRetry` is true, an automatic retry will follow.
|
|
896
|
-
|
|
897
|
-
```json
|
|
898
|
-
{
|
|
899
|
-
"type": "agent_end",
|
|
900
|
-
"messages": [...],
|
|
901
|
-
"willRetry": false
|
|
902
|
-
}
|
|
903
|
-
```
|
|
904
|
-
|
|
905
|
-
### agent_settled
|
|
906
|
-
|
|
907
|
-
Emitted after the full session-level run settles. At this point KnightCode will not continue automatically through retry, compaction retry, or queued follow-up messages.
|
|
908
|
-
|
|
909
|
-
```json
|
|
910
|
-
{"type": "agent_settled"}
|
|
911
|
-
```
|
|
912
|
-
|
|
913
|
-
### turn_start / turn_end
|
|
914
|
-
|
|
915
|
-
A turn consists of one assistant response plus any resulting tool calls and results.
|
|
916
|
-
|
|
917
|
-
```json
|
|
918
|
-
{"type": "turn_start"}
|
|
919
|
-
```
|
|
920
|
-
|
|
921
|
-
```json
|
|
922
|
-
{
|
|
923
|
-
"type": "turn_end",
|
|
924
|
-
"message": {...},
|
|
925
|
-
"toolResults": [...]
|
|
926
|
-
}
|
|
927
|
-
```
|
|
928
|
-
|
|
929
|
-
### message_start / message_end
|
|
930
|
-
|
|
931
|
-
Emitted when a message begins and completes. The `message` field contains an `AgentMessage`.
|
|
932
|
-
|
|
933
|
-
```json
|
|
934
|
-
{"type": "message_start", "message": {...}}
|
|
935
|
-
{"type": "message_end", "message": {...}}
|
|
936
|
-
```
|
|
937
|
-
|
|
938
|
-
### message_update (Streaming)
|
|
939
|
-
|
|
940
|
-
Emitted during streaming of assistant messages. Contains a delta event without a cumulative message snapshot.
|
|
941
|
-
|
|
942
|
-
```json
|
|
943
|
-
{
|
|
944
|
-
"type": "message_update",
|
|
945
|
-
"usage": {
|
|
946
|
-
"input": 100,
|
|
947
|
-
"output": 1,
|
|
948
|
-
"cacheRead": 0,
|
|
949
|
-
"cacheWrite": 0,
|
|
950
|
-
"totalTokens": 101,
|
|
951
|
-
"cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0, "total": 0}
|
|
952
|
-
},
|
|
953
|
-
"assistantMessageEvent": {
|
|
954
|
-
"type": "text_delta",
|
|
955
|
-
"contentIndex": 0,
|
|
956
|
-
"delta": "Hello "
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
```
|
|
960
|
-
|
|
961
|
-
The `assistantMessageEvent` field contains one of these delta types:
|
|
962
|
-
|
|
963
|
-
| Type | Description |
|
|
964
|
-
|------|-------------|
|
|
965
|
-
| `text_start` | Text content block started |
|
|
966
|
-
| `text_delta` | Text content chunk |
|
|
967
|
-
| `text_end` | Text content block ended |
|
|
968
|
-
| `thinking_start` | Thinking block started |
|
|
969
|
-
| `thinking_delta` | Thinking content chunk |
|
|
970
|
-
| `thinking_end` | Thinking block ended |
|
|
971
|
-
| `toolcall_start` | Tool call started (includes `id` and `toolName`) |
|
|
972
|
-
| `toolcall_delta` | Tool call arguments chunk |
|
|
973
|
-
| `toolcall_end` | Tool call ended (includes full `toolCall` object) |
|
|
974
|
-
|
|
975
|
-
Example streaming a text response:
|
|
976
|
-
```json
|
|
977
|
-
{"type":"message_update","usage":{...},"assistantMessageEvent":{"type":"text_start","contentIndex":0}}
|
|
978
|
-
{"type":"message_update","usage":{...},"assistantMessageEvent":{"type":"text_delta","contentIndex":0,"delta":"Hello"}}
|
|
979
|
-
{"type":"message_update","usage":{...},"assistantMessageEvent":{"type":"text_delta","contentIndex":0,"delta":" world"}}
|
|
980
|
-
{"type":"message_update","usage":{...},"assistantMessageEvent":{"type":"text_end","contentIndex":0,"content":"Hello world"}}
|
|
981
|
-
```
|
|
982
|
-
|
|
983
|
-
The top-level `usage` field contains the latest cumulative provider-reported usage. It may remain
|
|
984
|
-
zero until completion when a provider does not report usage during streaming.
|
|
985
|
-
|
|
986
|
-
Example starting a tool call:
|
|
987
|
-
```json
|
|
988
|
-
{"type":"message_update","usage":{...},"assistantMessageEvent":{"type":"toolcall_start","contentIndex":1,"id":"call_abc123","toolName":"write"}}
|
|
989
|
-
```
|
|
990
|
-
|
|
991
|
-
`message_update` intentionally omits the former cumulative `message` field and
|
|
992
|
-
`assistantMessageEvent.partial`. Clients that need a live partial message must assemble it
|
|
993
|
-
from `message_start` and subsequent events using `contentIndex`. Treat `message_end.message`
|
|
994
|
-
as authoritative. For tool calls, `toolcall_start` provides the call `id` and `toolName`;
|
|
995
|
-
buffer `toolcall_delta.delta` for arguments. `toolcall_end.toolCall` contains the completed
|
|
996
|
-
call.
|
|
997
|
-
|
|
998
|
-
### bash_execution_update
|
|
999
|
-
|
|
1000
|
-
Emitted once for each output chunk from a direct `bash` command. `id` matches the command's `id`, allowing clients to associate output with the correct command.
|
|
1001
|
-
|
|
1002
|
-
Events stream all output while the command runs, even if the final `bash` response's `output` is truncated.
|
|
1003
|
-
|
|
1004
|
-
```json
|
|
1005
|
-
{
|
|
1006
|
-
"type": "bash_execution_update",
|
|
1007
|
-
"id": "req-1",
|
|
1008
|
-
"delta": "total 48\n"
|
|
1009
|
-
}
|
|
1010
|
-
```
|
|
1011
|
-
|
|
1012
|
-
### tool_execution_start / tool_execution_update / tool_execution_end
|
|
1013
|
-
|
|
1014
|
-
Emitted when a tool begins, streams progress, and completes execution.
|
|
1015
|
-
|
|
1016
|
-
```json
|
|
1017
|
-
{
|
|
1018
|
-
"type": "tool_execution_start",
|
|
1019
|
-
"toolCallId": "call_abc123",
|
|
1020
|
-
"toolName": "bash",
|
|
1021
|
-
"args": {"command": "ls -la"}
|
|
1022
|
-
}
|
|
1023
|
-
```
|
|
1024
|
-
|
|
1025
|
-
During execution, `tool_execution_update` events stream partial results (e.g., bash output as it arrives):
|
|
1026
|
-
|
|
1027
|
-
```json
|
|
1028
|
-
{
|
|
1029
|
-
"type": "tool_execution_update",
|
|
1030
|
-
"toolCallId": "call_abc123",
|
|
1031
|
-
"toolName": "bash",
|
|
1032
|
-
"args": {"command": "ls -la"},
|
|
1033
|
-
"partialResult": {
|
|
1034
|
-
"content": [{"type": "text", "text": "partial output so far..."}],
|
|
1035
|
-
"details": {"truncation": null, "fullOutputPath": null}
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
```
|
|
1039
|
-
|
|
1040
|
-
When complete:
|
|
1041
|
-
|
|
1042
|
-
```json
|
|
1043
|
-
{
|
|
1044
|
-
"type": "tool_execution_end",
|
|
1045
|
-
"toolCallId": "call_abc123",
|
|
1046
|
-
"toolName": "bash",
|
|
1047
|
-
"result": {
|
|
1048
|
-
"content": [{"type": "text", "text": "total 48\n..."}],
|
|
1049
|
-
"details": {...}
|
|
1050
|
-
},
|
|
1051
|
-
"isError": false
|
|
1052
|
-
}
|
|
1053
|
-
```
|
|
1054
|
-
|
|
1055
|
-
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.
|
|
1056
|
-
|
|
1057
|
-
### queue_update
|
|
1058
|
-
|
|
1059
|
-
Emitted whenever the pending steering or follow-up queue changes.
|
|
1060
|
-
|
|
1061
|
-
```json
|
|
1062
|
-
{
|
|
1063
|
-
"type": "queue_update",
|
|
1064
|
-
"steering": ["Focus on error handling"],
|
|
1065
|
-
"followUp": ["After that, summarize the result"]
|
|
1066
|
-
}
|
|
1067
|
-
```
|
|
1068
|
-
|
|
1069
|
-
### compaction_start / compaction_end
|
|
1070
|
-
|
|
1071
|
-
Emitted when compaction runs, whether manual or automatic.
|
|
1072
|
-
|
|
1073
|
-
```json
|
|
1074
|
-
{"type": "compaction_start", "reason": "threshold"}
|
|
1075
|
-
```
|
|
1076
|
-
|
|
1077
|
-
The `reason` field is `"manual"`, `"threshold"`, or `"overflow"`.
|
|
1078
|
-
|
|
1079
|
-
```json
|
|
1080
|
-
{
|
|
1081
|
-
"type": "compaction_end",
|
|
1082
|
-
"reason": "threshold",
|
|
1083
|
-
"result": {
|
|
1084
|
-
"summary": "Summary of conversation...",
|
|
1085
|
-
"firstKeptEntryId": "abc123",
|
|
1086
|
-
"tokensBefore": 150000,
|
|
1087
|
-
"estimatedTokensAfter": 32000,
|
|
1088
|
-
"usage": {
|
|
1089
|
-
"input": 32000,
|
|
1090
|
-
"output": 1200,
|
|
1091
|
-
"cacheRead": 0,
|
|
1092
|
-
"cacheWrite": 0,
|
|
1093
|
-
"totalTokens": 33200,
|
|
1094
|
-
"cost": {"input": 0.01, "output": 0.02, "cacheRead": 0, "cacheWrite": 0, "total": 0.03}
|
|
1095
|
-
},
|
|
1096
|
-
"details": {}
|
|
1097
|
-
},
|
|
1098
|
-
"aborted": false,
|
|
1099
|
-
"willRetry": false
|
|
1100
|
-
}
|
|
1101
|
-
```
|
|
1102
|
-
|
|
1103
|
-
If `reason` was `"overflow"` and compaction succeeds, `willRetry` is `true` and the agent will automatically retry the prompt.
|
|
1104
|
-
|
|
1105
|
-
If compaction was aborted, `result` is `null` and `aborted` is `true`.
|
|
1106
|
-
|
|
1107
|
-
If compaction failed (e.g., API quota exceeded), `result` is `null`, `aborted` is `false`, and `errorMessage` contains the error description.
|
|
1108
|
-
|
|
1109
|
-
### auto_retry_start / auto_retry_end
|
|
1110
|
-
|
|
1111
|
-
Emitted when automatic retry is triggered after a transient error (overloaded, rate limit, 5xx).
|
|
1112
|
-
|
|
1113
|
-
```json
|
|
1114
|
-
{
|
|
1115
|
-
"type": "auto_retry_start",
|
|
1116
|
-
"attempt": 1,
|
|
1117
|
-
"maxAttempts": 3,
|
|
1118
|
-
"delayMs": 2000,
|
|
1119
|
-
"errorMessage": "529 {\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"Overloaded\"}}"
|
|
1120
|
-
}
|
|
1121
|
-
```
|
|
1122
|
-
|
|
1123
|
-
```json
|
|
1124
|
-
{
|
|
1125
|
-
"type": "auto_retry_end",
|
|
1126
|
-
"success": true,
|
|
1127
|
-
"attempt": 2
|
|
1128
|
-
}
|
|
1129
|
-
```
|
|
1130
|
-
|
|
1131
|
-
On final failure (max retries exceeded):
|
|
1132
|
-
```json
|
|
1133
|
-
{
|
|
1134
|
-
"type": "auto_retry_end",
|
|
1135
|
-
"success": false,
|
|
1136
|
-
"attempt": 3,
|
|
1137
|
-
"finalError": "529 overloaded_error: Overloaded"
|
|
1138
|
-
}
|
|
1139
|
-
```
|
|
1140
|
-
|
|
1141
|
-
### summarization_retry_scheduled / summarization_retry_attempt_start / summarization_retry_finished
|
|
1142
|
-
|
|
1143
|
-
Emitted when compaction or branch-summary summarization retries after a transient provider error. These events use the same retry settings as automatic assistant-turn retries.
|
|
1144
|
-
|
|
1145
|
-
```json
|
|
1146
|
-
{
|
|
1147
|
-
"type": "summarization_retry_scheduled",
|
|
1148
|
-
"attempt": 1,
|
|
1149
|
-
"maxAttempts": 3,
|
|
1150
|
-
"delayMs": 2000,
|
|
1151
|
-
"errorMessage": "terminated"
|
|
1152
|
-
}
|
|
1153
|
-
```
|
|
1154
|
-
|
|
1155
|
-
```json
|
|
1156
|
-
{
|
|
1157
|
-
"type": "summarization_retry_attempt_start",
|
|
1158
|
-
"source": "compaction",
|
|
1159
|
-
"reason": "threshold"
|
|
1160
|
-
}
|
|
1161
|
-
```
|
|
1162
|
-
|
|
1163
|
-
For branch summaries, `source` is `"branchSummary"` and no `reason` is present.
|
|
1164
|
-
|
|
1165
|
-
```json
|
|
1166
|
-
{
|
|
1167
|
-
"type": "summarization_retry_finished"
|
|
1168
|
-
}
|
|
1169
|
-
```
|
|
1170
|
-
|
|
1171
|
-
### extension_error
|
|
1172
|
-
|
|
1173
|
-
Emitted when an extension throws an error.
|
|
1174
|
-
|
|
1175
|
-
```json
|
|
1176
|
-
{
|
|
1177
|
-
"type": "extension_error",
|
|
1178
|
-
"extensionPath": "/path/to/extension.ts",
|
|
1179
|
-
"event": "tool_call",
|
|
1180
|
-
"error": "Error message..."
|
|
1181
|
-
}
|
|
1182
|
-
```
|
|
1183
|
-
|
|
1184
|
-
## Extension UI Protocol
|
|
1185
|
-
|
|
1186
|
-
Extensions can request user interaction via `ctx.ui.select()`, `ctx.ui.confirm()`, etc. In RPC mode, these are translated into a request/response sub-protocol on top of the base command/event flow.
|
|
1187
|
-
|
|
1188
|
-
There are two categories of extension UI methods:
|
|
1189
|
-
|
|
1190
|
-
- **Dialog methods** (`select`, `confirm`, `input`, `editor`): emit an `extension_ui_request` on stdout and block until the client sends back an `extension_ui_response` on stdin with the matching `id`.
|
|
1191
|
-
- **Fire-and-forget methods** (`notify`, `setStatus`, `setWidget`, `setTitle`, `set_editor_text`): emit an `extension_ui_request` on stdout but do not expect a response. The client can display the information or ignore it.
|
|
1192
|
-
|
|
1193
|
-
If a dialog method includes a `timeout` field, the agent-side will auto-resolve with a default value when the timeout expires. The client does not need to track timeouts.
|
|
1194
|
-
|
|
1195
|
-
Some `ExtensionUIContext` methods are not supported or degraded in RPC mode because they require direct TUI access:
|
|
1196
|
-
- `custom()` returns `undefined`
|
|
1197
|
-
- `setWorkingMessage()`, `setWorkingIndicator()`, `setFooter()`, `setHeader()`, `setEditorComponent()`, `setToolsExpanded()` are no-ops
|
|
1198
|
-
- `getEditorText()` returns `""`
|
|
1199
|
-
- `getToolsExpanded()` returns `false`
|
|
1200
|
-
- `pasteToEditor()` delegates to `setEditorText()` (no paste/collapse handling)
|
|
1201
|
-
- `getAllThemes()` returns `[]`
|
|
1202
|
-
- `getTheme()` returns `undefined`
|
|
1203
|
-
- `setTheme()` returns `{ success: false, error: "..." }`
|
|
1204
|
-
|
|
1205
|
-
Note: `ctx.mode` is `"rpc"` and `ctx.hasUI` is `true` in RPC mode because the dialog and fire-and-forget methods are functional via the extension UI sub-protocol. Use `ctx.mode === "tui"` to guard TUI-specific features like `custom()` that require a real terminal.
|
|
1206
|
-
|
|
1207
|
-
### Extension UI Requests (stdout)
|
|
1208
|
-
|
|
1209
|
-
All requests have `type: "extension_ui_request"`, a unique `id`, and a `method` field.
|
|
1210
|
-
|
|
1211
|
-
#### select
|
|
1212
|
-
|
|
1213
|
-
Prompt the user to choose from a list. Dialog methods with a `timeout` field include the timeout in milliseconds; the agent auto-resolves with `undefined` if the client doesn't respond in time.
|
|
1214
|
-
|
|
1215
|
-
```json
|
|
1216
|
-
{
|
|
1217
|
-
"type": "extension_ui_request",
|
|
1218
|
-
"id": "uuid-1",
|
|
1219
|
-
"method": "select",
|
|
1220
|
-
"title": "Allow dangerous command?",
|
|
1221
|
-
"options": ["Allow", "Block"],
|
|
1222
|
-
"timeout": 10000
|
|
1223
|
-
}
|
|
1224
|
-
```
|
|
1225
|
-
|
|
1226
|
-
Expected response: `extension_ui_response` with `value` (the selected option string) or `cancelled: true`.
|
|
1227
|
-
|
|
1228
|
-
#### confirm
|
|
1229
|
-
|
|
1230
|
-
Prompt the user for yes/no confirmation.
|
|
1231
|
-
|
|
1232
|
-
```json
|
|
1233
|
-
{
|
|
1234
|
-
"type": "extension_ui_request",
|
|
1235
|
-
"id": "uuid-2",
|
|
1236
|
-
"method": "confirm",
|
|
1237
|
-
"title": "Clear session?",
|
|
1238
|
-
"message": "All messages will be lost.",
|
|
1239
|
-
"timeout": 5000
|
|
1240
|
-
}
|
|
1241
|
-
```
|
|
1242
|
-
|
|
1243
|
-
Expected response: `extension_ui_response` with `confirmed: true/false` or `cancelled: true`.
|
|
1244
|
-
|
|
1245
|
-
#### input
|
|
1246
|
-
|
|
1247
|
-
Prompt the user for free-form text.
|
|
1248
|
-
|
|
1249
|
-
```json
|
|
1250
|
-
{
|
|
1251
|
-
"type": "extension_ui_request",
|
|
1252
|
-
"id": "uuid-3",
|
|
1253
|
-
"method": "input",
|
|
1254
|
-
"title": "Enter a value",
|
|
1255
|
-
"placeholder": "type something..."
|
|
1256
|
-
}
|
|
1257
|
-
```
|
|
1258
|
-
|
|
1259
|
-
Expected response: `extension_ui_response` with `value` (the entered text) or `cancelled: true`.
|
|
1260
|
-
|
|
1261
|
-
#### editor
|
|
1262
|
-
|
|
1263
|
-
Open a multi-line text editor with optional prefilled content.
|
|
1264
|
-
|
|
1265
|
-
```json
|
|
1266
|
-
{
|
|
1267
|
-
"type": "extension_ui_request",
|
|
1268
|
-
"id": "uuid-4",
|
|
1269
|
-
"method": "editor",
|
|
1270
|
-
"title": "Edit some text",
|
|
1271
|
-
"prefill": "Line 1\nLine 2\nLine 3"
|
|
1272
|
-
}
|
|
1273
|
-
```
|
|
1274
|
-
|
|
1275
|
-
Expected response: `extension_ui_response` with `value` (the edited text) or `cancelled: true`.
|
|
1276
|
-
|
|
1277
|
-
#### notify
|
|
1278
|
-
|
|
1279
|
-
Display a notification. Fire-and-forget, no response expected.
|
|
1280
|
-
|
|
1281
|
-
```json
|
|
1282
|
-
{
|
|
1283
|
-
"type": "extension_ui_request",
|
|
1284
|
-
"id": "uuid-5",
|
|
1285
|
-
"method": "notify",
|
|
1286
|
-
"message": "Command blocked by user",
|
|
1287
|
-
"notifyType": "warning"
|
|
1288
|
-
}
|
|
1289
|
-
```
|
|
1290
|
-
|
|
1291
|
-
The `notifyType` field is `"info"`, `"warning"`, or `"error"`. Defaults to `"info"` if omitted.
|
|
1292
|
-
|
|
1293
|
-
#### setStatus
|
|
1294
|
-
|
|
1295
|
-
Set or clear a status entry in the footer/status bar. Fire-and-forget.
|
|
1296
|
-
|
|
1297
|
-
```json
|
|
1298
|
-
{
|
|
1299
|
-
"type": "extension_ui_request",
|
|
1300
|
-
"id": "uuid-6",
|
|
1301
|
-
"method": "setStatus",
|
|
1302
|
-
"statusKey": "my-ext",
|
|
1303
|
-
"statusText": "Turn 3 running..."
|
|
1304
|
-
}
|
|
1305
|
-
```
|
|
1306
|
-
|
|
1307
|
-
Send `statusText: undefined` (or omit it) to clear the status entry for that key.
|
|
1308
|
-
|
|
1309
|
-
#### setWidget
|
|
1310
|
-
|
|
1311
|
-
Set or clear a widget (block of text lines) displayed above or below the editor. Fire-and-forget.
|
|
1312
|
-
|
|
1313
|
-
```json
|
|
1314
|
-
{
|
|
1315
|
-
"type": "extension_ui_request",
|
|
1316
|
-
"id": "uuid-7",
|
|
1317
|
-
"method": "setWidget",
|
|
1318
|
-
"widgetKey": "my-ext",
|
|
1319
|
-
"widgetLines": ["--- My Widget ---", "Line 1", "Line 2"],
|
|
1320
|
-
"widgetPlacement": "aboveEditor"
|
|
1321
|
-
}
|
|
1322
|
-
```
|
|
1323
|
-
|
|
1324
|
-
Send `widgetLines: undefined` (or omit it) to clear the widget. The `widgetPlacement` field is `"aboveEditor"` (default) or `"belowEditor"`. Only string arrays are supported in RPC mode; component factories are ignored.
|
|
1325
|
-
|
|
1326
|
-
#### setTitle
|
|
1327
|
-
|
|
1328
|
-
Set the terminal window/tab title. Fire-and-forget.
|
|
1329
|
-
|
|
1330
|
-
```json
|
|
1331
|
-
{
|
|
1332
|
-
"type": "extension_ui_request",
|
|
1333
|
-
"id": "uuid-8",
|
|
1334
|
-
"method": "setTitle",
|
|
1335
|
-
"title": "knightcode - my project"
|
|
1336
|
-
}
|
|
14
|
+
```bash
|
|
15
|
+
knightcode --mode rpc --no-session
|
|
1337
16
|
```
|
|
1338
17
|
|
|
1339
|
-
|
|
18
|
+
Normal CLI options still select the working folder, model, tools, resources, and session behavior. Common choices include `--provider`, `--model`, `--name`, `--no-session`, and `--session-dir`. See [Command Line](cli.md) for the complete, version-specific interface; `knightcode --help` is authoritative for the installed version.
|
|
1340
19
|
|
|
1341
|
-
|
|
20
|
+
RPC mode rejects `@file` prompt arguments. Send prompts through the [`prompt`](rpc-commands.md#prompt) command instead.
|
|
1342
21
|
|
|
1343
|
-
|
|
1344
|
-
{
|
|
1345
|
-
"type": "extension_ui_request",
|
|
1346
|
-
"id": "uuid-9",
|
|
1347
|
-
"method": "set_editor_text",
|
|
1348
|
-
"text": "prefilled text for the user"
|
|
1349
|
-
}
|
|
1350
|
-
```
|
|
22
|
+
## Protocol records
|
|
1351
23
|
|
|
1352
|
-
|
|
24
|
+
The protocol has four record families:
|
|
1353
25
|
|
|
1354
|
-
|
|
26
|
+
| Direction | Record | Purpose |
|
|
27
|
+
|---|---|---|
|
|
28
|
+
| stdin | Command | Ask KnightCode to prompt, inspect state, change configuration, or manage the session |
|
|
29
|
+
| stdout | `response` | Report whether one command succeeded and return any command data |
|
|
30
|
+
| stdout | Session event | Stream run, message, tool, queue, compaction, and retry activity |
|
|
31
|
+
| Both | Extension UI record | Forward supported extension interactions between KnightCode and the client |
|
|
1355
32
|
|
|
1356
|
-
|
|
33
|
+
See [RPC Commands](rpc-commands.md), [JSON Event Stream](json.md), and [RPC Extension UI](rpc-extension-ui.md) for the canonical record definitions.
|
|
1357
34
|
|
|
1358
|
-
|
|
1359
|
-
{"type": "extension_ui_response", "id": "uuid-1", "value": "Allow"}
|
|
1360
|
-
```
|
|
35
|
+
### Correlate commands and responses
|
|
1361
36
|
|
|
1362
|
-
|
|
37
|
+
Every command accepts an optional string `id`. A matching response repeats it:
|
|
1363
38
|
|
|
1364
39
|
```json
|
|
1365
|
-
{"
|
|
40
|
+
{"id":"req-1","type":"get_state"}
|
|
41
|
+
{"id":"req-1","type":"response","command":"get_state","success":true,"data":{"...":"..."}}
|
|
1366
42
|
```
|
|
1367
43
|
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
Dismiss any dialog method. The extension receives `undefined` (for select/input/editor) or `false` (for confirm).
|
|
44
|
+
Use unique IDs whenever more than one command can be outstanding. Command handling is asynchronous, so clients should correlate by ID rather than response order.
|
|
1371
45
|
|
|
1372
|
-
|
|
1373
|
-
{"type": "extension_ui_response", "id": "uuid-3", "cancelled": true}
|
|
1374
|
-
```
|
|
46
|
+
Session events generally have no command ID because they describe session activity. `bash_execution_update` is the exception: when the originating [`bash`](rpc-commands.md#bash) command has an ID, its output events repeat that ID.
|
|
1375
47
|
|
|
1376
|
-
|
|
48
|
+
An `extension_ui_response` uses the ID supplied by its `extension_ui_request`. It does not produce a normal command response.
|
|
1377
49
|
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
```json
|
|
1381
|
-
{
|
|
1382
|
-
"type": "response",
|
|
1383
|
-
"command": "set_model",
|
|
1384
|
-
"success": false,
|
|
1385
|
-
"error": "Model not found: invalid/model"
|
|
1386
|
-
}
|
|
1387
|
-
```
|
|
50
|
+
## Framing
|
|
1388
51
|
|
|
1389
|
-
|
|
52
|
+
RPC uses strict JSONL framing. Write one complete JSON object per record and terminate it with LF (`\n`). Read stdout as a byte or UTF-8 stream and split records only on LF. Strip an optional preceding carriage return to accept CRLF input.
|
|
1390
53
|
|
|
1391
|
-
|
|
1392
|
-
{
|
|
1393
|
-
"type": "response",
|
|
1394
|
-
"command": "parse",
|
|
1395
|
-
"success": false,
|
|
1396
|
-
"error": "Failed to parse command: Unexpected token..."
|
|
1397
|
-
}
|
|
1398
|
-
```
|
|
54
|
+
Do not use a generic line reader that treats Unicode line or paragraph separators as record boundaries. In particular, Node.js `readline` also splits on `U+2028` and `U+2029`, which are valid inside JSON strings.
|
|
1399
55
|
|
|
1400
|
-
|
|
56
|
+
Read stdout continuously. KnightCode honors stdout backpressure, but a client that stops reading can stall the process. Honor stdin backpressure when writing commands. Stdout is reserved for protocol records; diagnostics and application logging go to stderr.
|
|
1401
57
|
|
|
1402
|
-
|
|
1403
|
-
- [`packages/ai/src/types.ts`](../../ai/src/types.ts) - `Model`, `UserMessage`, `AssistantMessage`, `ToolResultMessage`
|
|
1404
|
-
- [`packages/agent/src/types.ts`](../../agent/src/types.ts) - `AgentMessage`, `AgentEvent`
|
|
1405
|
-
- [`src/core/messages.ts`](../src/core/messages.ts) - `BashExecutionMessage`
|
|
1406
|
-
- [`src/modes/json-event.ts`](../src/modes/json-event.ts) - `JsonAgentSessionEvent`
|
|
1407
|
-
- [`src/modes/rpc/rpc-types.ts`](../src/modes/rpc/rpc-types.ts) - RPC command/response types, extension UI request/response types
|
|
58
|
+
## Run lifecycle
|
|
1408
59
|
|
|
1409
|
-
|
|
60
|
+
A successful `prompt` response means the prompt was accepted, queued, or handled. It does not mean model work completed:
|
|
1410
61
|
|
|
1411
62
|
```json
|
|
1412
|
-
{
|
|
1413
|
-
|
|
1414
|
-
"name": "Claude Sonnet 4",
|
|
1415
|
-
"api": "anthropic-messages",
|
|
1416
|
-
"provider": "anthropic",
|
|
1417
|
-
"baseUrl": "https://api.anthropic.com",
|
|
1418
|
-
"reasoning": true,
|
|
1419
|
-
"input": ["text", "image"],
|
|
1420
|
-
"contextWindow": 200000,
|
|
1421
|
-
"maxTokens": 16384,
|
|
1422
|
-
"cost": {
|
|
1423
|
-
"input": 3.0,
|
|
1424
|
-
"output": 15.0,
|
|
1425
|
-
"cacheRead": 0.3,
|
|
1426
|
-
"cacheWrite": 3.75
|
|
1427
|
-
}
|
|
1428
|
-
}
|
|
63
|
+
{"id":"req-2","type":"prompt","message":"Review this repository"}
|
|
64
|
+
{"id":"req-2","type":"response","command":"prompt","success":true}
|
|
1429
65
|
```
|
|
1430
66
|
|
|
1431
|
-
|
|
67
|
+
Continue consuming [events](json.md) after that response. `agent_end` marks the end of one low-level agent run, but retries, overflow recovery, compaction, steering, or follow-up work can still follow. Wait for `agent_settled` when the client needs to know KnightCode will not continue automatically.
|
|
1432
68
|
|
|
1433
|
-
|
|
1434
|
-
{
|
|
1435
|
-
"role": "user",
|
|
1436
|
-
"content": "Hello!",
|
|
1437
|
-
"timestamp": 1733234567890,
|
|
1438
|
-
"attachments": []
|
|
1439
|
-
}
|
|
1440
|
-
```
|
|
69
|
+
Subscribe before sending a prompt to avoid missing a fast completion. `RpcClient.promptAndWait()` does this internally. If using separate `RpcClient` calls, install the event listener before `prompt()` and call `waitForIdle()` only while a run is active.
|
|
1441
70
|
|
|
1442
|
-
|
|
71
|
+
## Errors
|
|
1443
72
|
|
|
1444
|
-
|
|
73
|
+
A failed command returns one response with `success: false`:
|
|
1445
74
|
|
|
1446
75
|
```json
|
|
1447
|
-
{
|
|
1448
|
-
"role": "assistant",
|
|
1449
|
-
"content": [
|
|
1450
|
-
{"type": "text", "text": "Hello! How can I help?"},
|
|
1451
|
-
{"type": "thinking", "thinking": "User is greeting me..."},
|
|
1452
|
-
{"type": "toolCall", "id": "call_123", "name": "bash", "arguments": {"command": "ls"}}
|
|
1453
|
-
],
|
|
1454
|
-
"api": "anthropic-messages",
|
|
1455
|
-
"provider": "anthropic",
|
|
1456
|
-
"model": "claude-sonnet-4-20250514",
|
|
1457
|
-
"usage": {
|
|
1458
|
-
"input": 100,
|
|
1459
|
-
"output": 50,
|
|
1460
|
-
"cacheRead": 0,
|
|
1461
|
-
"cacheWrite": 0,
|
|
1462
|
-
"cost": {"input": 0.0003, "output": 0.00075, "cacheRead": 0, "cacheWrite": 0, "total": 0.00105}
|
|
1463
|
-
},
|
|
1464
|
-
"stopReason": "stop",
|
|
1465
|
-
"timestamp": 1733234567890
|
|
1466
|
-
}
|
|
76
|
+
{"id":"req-3","type":"response","command":"set_model","success":false,"error":"Model not found: invalid/model"}
|
|
1467
77
|
```
|
|
1468
78
|
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
### ToolResultMessage
|
|
79
|
+
Malformed JSON produces a parse response without a request ID:
|
|
1472
80
|
|
|
1473
81
|
```json
|
|
1474
|
-
{
|
|
1475
|
-
"role": "toolResult",
|
|
1476
|
-
"toolCallId": "call_123",
|
|
1477
|
-
"toolName": "bash",
|
|
1478
|
-
"content": [{"type": "text", "text": "total 48\ndrwxr-xr-x ..."}],
|
|
1479
|
-
"usage": {
|
|
1480
|
-
"input": 100,
|
|
1481
|
-
"output": 50,
|
|
1482
|
-
"cacheRead": 0,
|
|
1483
|
-
"cacheWrite": 0,
|
|
1484
|
-
"totalTokens": 150,
|
|
1485
|
-
"cost": {"input": 0.0003, "output": 0.00075, "cacheRead": 0, "cacheWrite": 0, "total": 0.00105}
|
|
1486
|
-
},
|
|
1487
|
-
"isError": false,
|
|
1488
|
-
"timestamp": 1733234567890
|
|
1489
|
-
}
|
|
82
|
+
{"type":"response","command":"parse","success":false,"error":"Failed to parse command: Unexpected token..."}
|
|
1490
83
|
```
|
|
1491
84
|
|
|
1492
|
-
|
|
85
|
+
A success response only covers command handling. Provider failures and aborts after a prompt is accepted appear in the message and event stream.
|
|
1493
86
|
|
|
1494
|
-
|
|
87
|
+
Clients must also handle child-process startup failures, unexpected exits, stderr diagnostics, cancellation, and their own deadlines. Do not parse stderr as protocol data.
|
|
1495
88
|
|
|
1496
|
-
|
|
89
|
+
## Shutdown
|
|
1497
90
|
|
|
1498
|
-
|
|
1499
|
-
{
|
|
1500
|
-
"role": "bashExecution",
|
|
1501
|
-
"command": "ls -la",
|
|
1502
|
-
"output": "total 48\ndrwxr-xr-x ...",
|
|
1503
|
-
"exitCode": 0,
|
|
1504
|
-
"cancelled": false,
|
|
1505
|
-
"truncated": false,
|
|
1506
|
-
"fullOutputPath": null,
|
|
1507
|
-
"timestamp": 1733234567890
|
|
1508
|
-
}
|
|
1509
|
-
```
|
|
91
|
+
Close the child's stdin to request an orderly shutdown. KnightCode disposes the active runtime before exiting. Clients should still handle process signals and unexpected exits.
|
|
1510
92
|
|
|
1511
|
-
|
|
93
|
+
An extension can also request shutdown through its extension context. KnightCode completes shutdown after the current command or after the active run emits `agent_settled`.
|
|
1512
94
|
|
|
1513
|
-
|
|
1514
|
-
{
|
|
1515
|
-
"id": "img1",
|
|
1516
|
-
"type": "image",
|
|
1517
|
-
"fileName": "photo.jpg",
|
|
1518
|
-
"mimeType": "image/jpeg",
|
|
1519
|
-
"size": 102400,
|
|
1520
|
-
"content": "base64-encoded-data...",
|
|
1521
|
-
"extractedText": null,
|
|
1522
|
-
"preview": null
|
|
1523
|
-
}
|
|
1524
|
-
```
|
|
95
|
+
## Minimal client
|
|
1525
96
|
|
|
1526
|
-
|
|
97
|
+
This Python example uses a binary pipe reader, which splits on LF without treating Unicode separators as protocol boundaries:
|
|
1527
98
|
|
|
1528
99
|
```python
|
|
1529
|
-
import subprocess
|
|
1530
100
|
import json
|
|
101
|
+
import subprocess
|
|
1531
102
|
|
|
1532
|
-
|
|
103
|
+
process = subprocess.Popen(
|
|
1533
104
|
["knightcode", "--mode", "rpc", "--no-session"],
|
|
1534
105
|
stdin=subprocess.PIPE,
|
|
1535
106
|
stdout=subprocess.PIPE,
|
|
1536
|
-
text=True
|
|
1537
107
|
)
|
|
1538
108
|
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
proc.stdin.flush()
|
|
109
|
+
assert process.stdin is not None
|
|
110
|
+
assert process.stdout is not None
|
|
1542
111
|
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
112
|
+
command = {"id": "prompt-1", "type": "prompt", "message": "Hello"}
|
|
113
|
+
process.stdin.write(json.dumps(command).encode("utf-8") + b"\n")
|
|
114
|
+
process.stdin.flush()
|
|
1546
115
|
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
if delta.get("type") == "text_delta":
|
|
1555
|
-
print(delta["delta"], end="", flush=True)
|
|
1556
|
-
|
|
1557
|
-
if event.get("type") == "agent_end":
|
|
116
|
+
while line := process.stdout.readline():
|
|
117
|
+
record = json.loads(line)
|
|
118
|
+
if record.get("type") == "message_update":
|
|
119
|
+
update = record["assistantMessageEvent"]
|
|
120
|
+
if update["type"] == "text_delta":
|
|
121
|
+
print(update["delta"], end="", flush=True)
|
|
122
|
+
elif record.get("type") == "agent_settled":
|
|
1558
123
|
print()
|
|
1559
124
|
break
|
|
1560
|
-
```
|
|
1561
|
-
|
|
1562
|
-
## Example: Interactive Client (Node.js)
|
|
1563
|
-
|
|
1564
|
-
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.
|
|
1565
|
-
|
|
1566
|
-
For a complete example of handling the extension UI protocol, see [`examples/rpc-extension-ui.ts`](../examples/rpc-extension-ui.ts) which pairs with the [`examples/extensions/rpc-demo.ts`](../examples/extensions/rpc-demo.ts) extension.
|
|
1567
|
-
|
|
1568
|
-
```javascript
|
|
1569
|
-
const { spawn } = require("child_process");
|
|
1570
|
-
const { StringDecoder } = require("string_decoder");
|
|
1571
|
-
|
|
1572
|
-
const agent = spawn("knightcode", ["--mode", "rpc", "--no-session"]);
|
|
1573
|
-
|
|
1574
|
-
function attachJsonlReader(stream, onLine) {
|
|
1575
|
-
const decoder = new StringDecoder("utf8");
|
|
1576
|
-
let buffer = "";
|
|
1577
125
|
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
126
|
+
process.stdin.close()
|
|
127
|
+
process.wait()
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
For maintained TypeScript clients, use the checked [RPC client example](../examples/rpc-client.ts). It requires a built KnightCode CLI because the repository example points to `dist/cli.js`.
|
|
131
|
+
|
|
132
|
+
## Reference
|
|
133
|
+
|
|
134
|
+
- [RPC Commands](rpc-commands.md): every stdin command and response
|
|
135
|
+
- [JSON Event Stream](json.md): shared stdout session events and streaming reconstruction
|
|
136
|
+
- [RPC Extension UI](rpc-extension-ui.md): dialogs, notifications, responses, and limitations
|
|
137
|
+
- [Message Types](message-types.md): messages and content blocks used by responses and events
|
|
138
|
+
- [Session File Format](session-format.md): entries returned by session commands
|
|
139
|
+
- [`rpc-types.ts`](https://github.com/KnightCodeAI/knightcode/blob/main/packages/cli/src/modes/rpc/rpc-types.ts): exported TypeScript protocol definitions
|
|
140
|
+
- [`RpcClient`](https://github.com/KnightCodeAI/knightcode/blob/main/packages/cli/src/modes/rpc/rpc-client.ts): subprocess client implementation
|
|
141
|
+
|
|
142
|
+
## Moved reference anchors
|
|
143
|
+
|
|
144
|
+
The detailed references formerly on this page now have dedicated pages. These anchors preserve existing links.
|
|
145
|
+
|
|
146
|
+
<a id="prompt"></a>
|
|
147
|
+
<a id="steer"></a>
|
|
148
|
+
<a id="follow_up"></a>
|
|
149
|
+
<a id="abort"></a>
|
|
150
|
+
<a id="clear_queue"></a>
|
|
151
|
+
<a id="new_session"></a>
|
|
152
|
+
<a id="get_state"></a>
|
|
153
|
+
<a id="get_messages"></a>
|
|
154
|
+
<a id="set_model"></a>
|
|
155
|
+
<a id="cycle_model"></a>
|
|
156
|
+
<a id="get_available_models"></a>
|
|
157
|
+
<a id="set_thinking_level"></a>
|
|
158
|
+
<a id="cycle_thinking_level"></a>
|
|
159
|
+
<a id="get_available_thinking_levels"></a>
|
|
160
|
+
<a id="set_steering_mode"></a>
|
|
161
|
+
<a id="set_follow_up_mode"></a>
|
|
162
|
+
<a id="compact"></a>
|
|
163
|
+
<a id="set_auto_compaction"></a>
|
|
164
|
+
<a id="set_auto_retry"></a>
|
|
165
|
+
<a id="abort_retry"></a>
|
|
166
|
+
<a id="bash"></a>
|
|
167
|
+
<a id="abort_bash"></a>
|
|
168
|
+
<a id="get_session_stats"></a>
|
|
169
|
+
<a id="export_html"></a>
|
|
170
|
+
<a id="switch_session"></a>
|
|
171
|
+
<a id="fork"></a>
|
|
172
|
+
<a id="clone"></a>
|
|
173
|
+
<a id="get_fork_messages"></a>
|
|
174
|
+
<a id="get_entries"></a>
|
|
175
|
+
<a id="get_tree"></a>
|
|
176
|
+
<a id="get_last_assistant_text"></a>
|
|
177
|
+
<a id="set_session_name"></a>
|
|
178
|
+
<a id="get_commands"></a>
|
|
179
|
+
|
|
180
|
+
Command details moved to [RPC Commands](rpc-commands.md).
|
|
181
|
+
|
|
182
|
+
<a id="message_update-streaming"></a>
|
|
183
|
+
<a id="bash_execution_update"></a>
|
|
184
|
+
<a id="compaction_start--compaction_end"></a>
|
|
185
|
+
<a id="summarization_retry_scheduled--summarization_retry_attempt_start--summarization_retry_finished"></a>
|
|
186
|
+
|
|
187
|
+
Event details moved to [JSON Event Stream](json.md).
|
|
188
|
+
|
|
189
|
+
<a id="extension-ui-protocol"></a>
|
|
190
|
+
|
|
191
|
+
Extension interaction details moved to [RPC Extension UI](rpc-extension-ui.md).
|