@nexus-code/remote-protocol 0.1.0 → 0.2.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.
package/dist/rpc.js CHANGED
@@ -7,7 +7,7 @@
7
7
  * its tab's. The same folder has other ids on another device.
8
8
  */
9
9
  import { z } from "zod";
10
- import { AssistantItemSchema, ChatStatusSchema, ViewItemSchema, ViewTaskSchema, ViewToolCallSchema, } from "./view.js";
10
+ import { AssistantItemSchema, ChatPanelSchema, ChatStatusSchema, ViewItemSchema, ViewTaskSchema, ViewToolCallSchema, } from "./view.js";
11
11
  // ---------------------------------------------------------------------------
12
12
  // JSON-RPC 2.0 shapes
13
13
  // ---------------------------------------------------------------------------
@@ -62,6 +62,22 @@ export const RemoteWorkspaceSchema = z.object({
62
62
  name: z.string(),
63
63
  /** Its folder, on the serving device. */
64
64
  path: z.string(),
65
+ /** A task: a workspace on a git worktree of the workspace `parentId`, on its own branch. */
66
+ parentId: z.string().optional(),
67
+ /** The agent manager's own workspace, where the session that directs the others runs. */
68
+ manager: z.boolean().optional(),
69
+ /**
70
+ * A task's branch and how it stands against its upstream, with how many
71
+ * files it changed and did not commit. Looked at now and then, not live.
72
+ */
73
+ git: z
74
+ .object({
75
+ branch: z.string().nullable(),
76
+ ahead: z.number().int().nonnegative(),
77
+ behind: z.number().int().nonnegative(),
78
+ uncommitted: z.number().int().nonnegative(),
79
+ })
80
+ .optional(),
65
81
  });
66
82
  /** Where a session stands, as the agent lists show it. */
67
83
  export const SessionStateSchema = z.enum(["needs-you", "running", "new-result", "idle", "ended"]);
@@ -83,6 +99,8 @@ export const RemoteSessionSchema = z.object({
83
99
  .object({
84
100
  kind: z.enum(["permission", "question", "plan"]),
85
101
  text: z.string(),
102
+ /** A terminal's Claude Code asking: the request terminal.answer answers. */
103
+ requestId: z.string().optional(),
86
104
  })
87
105
  .optional(),
88
106
  });
@@ -92,6 +110,174 @@ const SessionsSchema = z.object({
92
110
  });
93
111
  /** A session of the serving device, by its ids there. */
94
112
  const TabRef = { workspaceId: z.string(), tabId: z.string() };
113
+ /**
114
+ * The agent that sends a request, when an agent does (nexus chat send on
115
+ * another device): its own tab there, which the receiving chat shows as
116
+ * the reply address. The device is the frame's sender. A control method
117
+ * with `from` takes the `agents` capability, not its own, and the serving
118
+ * device may ask its user first (AGENT_CONSENT_MS) or turn it down.
119
+ */
120
+ export const AgentSenderSchema = z.object({
121
+ /** tab: a Claude or Codex tab; terminal: an agent in a terminal; manager: the agent manager. */
122
+ kind: z.enum(["tab", "terminal", "manager"]),
123
+ agent: z.string().min(1).max(40).optional(),
124
+ workspaceId: z.string(),
125
+ tabId: z.string(),
126
+ /** Its tab's title, as the receiver shows the sender. */
127
+ name: z.string().max(200).optional(),
128
+ /**
129
+ * How many messages between agents led to this one, the first being 1:
130
+ * a device refuses one past MAX_AGENT_HOPS, so agents on two devices
131
+ * cannot keep each other going without end.
132
+ */
133
+ hops: z.number().int().positive(),
134
+ });
135
+ /** The keys terminal.input presses by name, as `nexus terminal key` names them. */
136
+ export const TERMINAL_KEYS = [
137
+ "enter",
138
+ "tab",
139
+ "shift-tab",
140
+ "esc",
141
+ "space",
142
+ "backspace",
143
+ "delete",
144
+ "up",
145
+ "down",
146
+ "left",
147
+ "right",
148
+ "home",
149
+ "end",
150
+ "pageup",
151
+ "pagedown",
152
+ "ctrl-a",
153
+ "ctrl-b",
154
+ "ctrl-c",
155
+ "ctrl-d",
156
+ "ctrl-e",
157
+ "ctrl-f",
158
+ "ctrl-g",
159
+ "ctrl-h",
160
+ "ctrl-i",
161
+ "ctrl-j",
162
+ "ctrl-k",
163
+ "ctrl-l",
164
+ "ctrl-m",
165
+ "ctrl-n",
166
+ "ctrl-o",
167
+ "ctrl-p",
168
+ "ctrl-q",
169
+ "ctrl-r",
170
+ "ctrl-s",
171
+ "ctrl-t",
172
+ "ctrl-u",
173
+ "ctrl-v",
174
+ "ctrl-w",
175
+ "ctrl-x",
176
+ "ctrl-y",
177
+ "ctrl-z",
178
+ ];
179
+ export const TerminalKeySchema = z.enum(TERMINAL_KEYS);
180
+ /** Most messages between agents one chain may take; past it, `from` requests are refused. */
181
+ export const MAX_AGENT_HOPS = 8;
182
+ /**
183
+ * How long a serving device may ask its user about a request with `from`
184
+ * before it turns it down: a requester waits this long, and a little more.
185
+ */
186
+ export const AGENT_CONSENT_MS = 90_000;
187
+ /** Most requests with `from` one agent (a device's tab) may make a minute; more are Unavailable. */
188
+ export const MAX_AGENT_REQUESTS_PER_MINUTE = 20;
189
+ /** Longest a chat.wait or terminal.wait holds before it answers that the session is not done yet. */
190
+ export const MAX_WAIT_MS = 10_000;
191
+ /** Where a chat stands for one that waits on it, as `nexus chat wait` tells it. */
192
+ export const ChatWaitStateSchema = z.enum(["working", "asking", "idle", "error", "closed"]);
193
+ /** A session state change, as `nexus events` lists them. */
194
+ export const SessionEventSchema = z.object({
195
+ /** Counts the device's events from 1 since its app started. */
196
+ seq: z.number().int().positive(),
197
+ at: z.number(),
198
+ workspaceId: z.string(),
199
+ tabId: z.string(),
200
+ title: z.string(),
201
+ kind: z.enum(["terminal", "claude", "codex"]),
202
+ agent: z.string().optional(),
203
+ /** The phase it left (null: first seen) and the one it came to ("closed" when the tab went). */
204
+ from: z.string().nullable(),
205
+ to: z.string(),
206
+ message: z.string().optional(),
207
+ });
208
+ /** Most images one message takes. */
209
+ export const MAX_ATTACHMENTS = 4;
210
+ /**
211
+ * Most bytes one image chat.attach takes, before base64: its base64 and the
212
+ * request around it stay inside a frame (MAX_FRAME_BYTES, sealed).
213
+ */
214
+ export const MAX_ATTACHMENT_BYTES = 560 * 1024;
215
+ /** The image types a chat takes. */
216
+ export const ImageMediaTypeSchema = z.enum(["image/png", "image/jpeg", "image/gif", "image/webp"]);
217
+ /** What a chat runs with, and what it may be set to. */
218
+ export const ChatSettingsSchema = z.object({
219
+ agent: z.enum(["claude", "codex"]),
220
+ /** The model it runs, by the id to give back; null before the program says. */
221
+ model: z.string().nullable(),
222
+ effort: z.string().nullable(),
223
+ permissionMode: z.string(),
224
+ options: z.object({
225
+ models: z.array(z.object({ id: z.string(), label: z.string() })),
226
+ /** The effort levels its model takes; none when it takes none. */
227
+ efforts: z.array(z.string()),
228
+ /** The permission modes; `dangerous` ones ask the serving device's policy first. */
229
+ modes: z.array(z.object({ id: z.string(), label: z.string(), dangerous: z.boolean().optional() })),
230
+ }),
231
+ });
232
+ /**
233
+ * A slash command of a chat, for a "/" picker: the app's own (run with
234
+ * chat.command), the agent's own (sent as a message: chat.send "/name
235
+ * args"), or one that sets what chat.configure sets.
236
+ */
237
+ export const ChatCommandSchema = z.object({
238
+ name: z.string(),
239
+ description: z.string(),
240
+ /** command: chat.command; send: chat.send its text; settings: chat.configure. */
241
+ runs: z.enum(["command", "send", "settings"]),
242
+ /** With runs "settings": what it sets, so the picker for it opens. */
243
+ sets: z.enum(["model", "effort", "permissionMode"]).optional(),
244
+ /** What it takes after its name, as its help writes it ("<condition>"). */
245
+ args: z.string().optional(),
246
+ });
247
+ /** A chat of the past that can be resumed: Claude Code's sessions, Codex's threads. */
248
+ export const PastSessionSchema = z.object({
249
+ sessionId: z.string(),
250
+ title: z.string(),
251
+ /** When it last changed, in Unix milliseconds. */
252
+ at: z.number(),
253
+ /** Its log's size in bytes, when known. */
254
+ bytes: z.number().int().nonnegative().optional(),
255
+ /** The folder it ran in. */
256
+ cwd: z.string().optional(),
257
+ profile: z.string().optional(),
258
+ });
259
+ /** A file's state in git: a letter as `git status --porcelain` gives it (M, A, D, R, C, U, ?). */
260
+ const GitLetter = z.string().length(1);
261
+ /** A file git sees changed, staged or not. */
262
+ export const GitFileSchema = z.object({
263
+ path: z.string(),
264
+ /** Renamed or copied from. */
265
+ oldPath: z.string().optional(),
266
+ staged: GitLetter.optional(),
267
+ unstaged: GitLetter.optional(),
268
+ });
269
+ /** A piece of a diff, as jsdiff's structuredPatch gives it: lines starting " ", "-" or "+". */
270
+ export const DiffHunkSchema = z.object({
271
+ oldStart: z.number().int().nonnegative(),
272
+ oldLines: z.number().int().nonnegative(),
273
+ newStart: z.number().int().nonnegative(),
274
+ newLines: z.number().int().nonnegative(),
275
+ lines: z.array(z.string()),
276
+ });
277
+ /** Most entries one files.list gives. */
278
+ export const MAX_LIST_ENTRIES = 2000;
279
+ /** Most bytes of text one files.read gives. */
280
+ export const MAX_READ_BYTES = 512 * 1024;
95
281
  // ---------------------------------------------------------------------------
96
282
  // Chats
97
283
  // ---------------------------------------------------------------------------
@@ -125,6 +311,21 @@ export const ChatUpdateSchema = z.object({
125
311
  tools: z.record(ViewToolCallSchema),
126
312
  tasks: z.record(ViewTaskSchema),
127
313
  agentItems: z.record(z.array(AssistantItemSchema)),
314
+ /** The side panel, when it changed. */
315
+ panel: ChatPanelSchema.optional(),
316
+ });
317
+ /**
318
+ * A watched terminal's text changed: the text from `start` (a UTF-16
319
+ * offset into the text as it was) is now `text`. A redraw at the bottom
320
+ * sends only the bottom; a scroll, all of it from 0. A watch with `ansi`
321
+ * counts in the text with its escapes.
322
+ */
323
+ export const TerminalUpdateSchema = z.object({
324
+ watchId: z.string(),
325
+ /** Counts the watch's updates from 1, so one lost shows. */
326
+ rev: z.number().int().positive(),
327
+ start: z.number().int().nonnegative(),
328
+ text: z.string(),
128
329
  });
129
330
  /** The sessions' list as it is now, sent whenever it changes during a sessions.watch. */
130
331
  export const SessionsUpdateSchema = SessionsSchema.extend({ watchId: z.string() });
@@ -149,6 +350,49 @@ export const RPC_METHODS = {
149
350
  params: z.object({ watchId: z.string() }),
150
351
  result: Ok,
151
352
  },
353
+ /**
354
+ * The session state changes after `since` (the first `limit`, `more` when
355
+ * some were left out), or the last `limit`; `cursor` is the latest seq. A
356
+ * `since` past it (from before the app restarted) reads from the start.
357
+ */
358
+ "events.read": {
359
+ capability: "sessions",
360
+ params: z.object({
361
+ since: z.number().int().nonnegative().optional(),
362
+ limit: z.number().int().positive().max(500).optional(),
363
+ }),
364
+ result: z.object({
365
+ events: z.array(SessionEventSchema),
366
+ cursor: z.number().int().nonnegative(),
367
+ more: z.boolean(),
368
+ }),
369
+ },
370
+ /** The agent profiles a chat can run with (accounts, config folders). */
371
+ "profiles.list": {
372
+ capability: "sessions",
373
+ params: z.object({ agent: z.enum(["claude", "codex"]).optional() }),
374
+ result: z.object({
375
+ profiles: z.array(z.object({
376
+ id: z.string(),
377
+ name: z.string(),
378
+ agent: z.enum(["claude", "codex"]),
379
+ /** The one a workspace runs with unless told otherwise. */
380
+ isDefault: z.boolean(),
381
+ })),
382
+ }),
383
+ },
384
+ /** The past chats of a workspace's folder that can be resumed, latest first. */
385
+ "history.list": {
386
+ capability: "chat.read",
387
+ params: z.object({
388
+ workspaceId: z.string(),
389
+ agent: z.enum(["claude", "codex"]),
390
+ /** Only those whose title has it. */
391
+ query: z.string().max(200).optional(),
392
+ limit: z.number().int().positive().max(200).optional(),
393
+ }),
394
+ result: z.object({ sessions: z.array(PastSessionSchema) }),
395
+ },
152
396
  /** The chat's last `limit` items, or those before `before`. */
153
397
  "chat.read": {
154
398
  capability: "chat.read",
@@ -166,18 +410,119 @@ export const RPC_METHODS = {
166
410
  ...TabRef,
167
411
  limit: z.number().int().positive().max(MAX_READ_ITEMS).optional(),
168
412
  }),
169
- result: ChatPageSchema.extend({ watchId: z.string() }),
413
+ result: ChatPageSchema.extend({ watchId: z.string(), panel: ChatPanelSchema.optional() }),
170
414
  },
171
415
  "chat.unwatch": {
172
416
  capability: "chat.read",
173
417
  params: z.object({ watchId: z.string() }),
174
418
  result: Ok,
175
419
  },
176
- /** Sends a message, as its composer does: during a turn, the turn reads it next. */
420
+ /** What the chat runs with and may be set to (model, effort, permission mode). */
421
+ "chat.settings": {
422
+ capability: "chat.read",
423
+ params: z.object(TabRef),
424
+ result: ChatSettingsSchema,
425
+ },
426
+ /** Its slash commands, the app's and the agent's, for a "/" picker. */
427
+ "chat.commands": {
428
+ capability: "chat.read",
429
+ params: z.object(TabRef),
430
+ result: z.object({ commands: z.array(ChatCommandSchema) }),
431
+ },
432
+ /** Its side panel now; a watch sends it again in chat.update as it changes. */
433
+ "chat.panel": {
434
+ capability: "chat.read",
435
+ params: z.object(TabRef),
436
+ result: ChatPanelSchema,
437
+ },
438
+ /** One tool call with as much of its result as a frame takes. */
439
+ "chat.tool": {
440
+ capability: "chat.read",
441
+ params: z.object({ ...TabRef, toolUseId: z.string() }),
442
+ result: ViewToolCallSchema,
443
+ },
444
+ /**
445
+ * Waits up to `timeoutMs` (at most MAX_WAIT_MS) for the chat to be done
446
+ * with its turn: no longer working (asking, idle, error, or closed). Ask
447
+ * again while `done` is false. `after`: a chat.send's `at`, so a message
448
+ * the chat has not shown yet counts as working.
449
+ */
450
+ "chat.wait": {
451
+ capability: "chat.read",
452
+ params: z.object({
453
+ ...TabRef,
454
+ timeoutMs: z.number().int().positive().max(MAX_WAIT_MS).optional(),
455
+ after: z.number().optional(),
456
+ }),
457
+ result: z.object({
458
+ done: z.boolean(),
459
+ state: ChatWaitStateSchema,
460
+ /** Its last answer; when asking, what it asks; on an error, what went wrong. */
461
+ message: z.string().optional(),
462
+ }),
463
+ },
464
+ /**
465
+ * An image of the chat, made small enough for a frame: a user message's
466
+ * (by its item's key and the image's index) or a tool result's.
467
+ */
468
+ "chat.image": {
469
+ capability: "chat.read",
470
+ params: z.object({
471
+ ...TabRef,
472
+ ref: z.discriminatedUnion("kind", [
473
+ z.object({
474
+ kind: z.literal("user"),
475
+ itemKey: z.string(),
476
+ index: z.number().int().nonnegative(),
477
+ }),
478
+ z.object({
479
+ kind: z.literal("tool"),
480
+ toolUseId: z.string(),
481
+ index: z.number().int().nonnegative(),
482
+ }),
483
+ ]),
484
+ }),
485
+ result: z.object({
486
+ mediaType: ImageMediaTypeSchema,
487
+ /** Base64. */
488
+ data: z.string(),
489
+ width: z.number().int().positive().optional(),
490
+ height: z.number().int().positive().optional(),
491
+ }),
492
+ },
493
+ /**
494
+ * Sends a message, as its composer does: during a turn, the turn reads it
495
+ * next. `text` may be empty only with attachments. Sent by an agent
496
+ * (`from`), the chat shows it as that agent's, not the user's.
497
+ */
177
498
  "chat.send": {
178
499
  capability: "chat.control",
179
- params: z.object({ ...TabRef, text: z.string().min(1) }),
180
- result: Ok,
500
+ params: z.object({
501
+ ...TabRef,
502
+ text: z.string(),
503
+ /** Images chat.attach took, at most MAX_ATTACHMENTS. */
504
+ attachments: z.array(z.string()).max(MAX_ATTACHMENTS).optional(),
505
+ from: AgentSenderSchema.optional(),
506
+ }),
507
+ /** `at`: when the serving device took it, by its clock, for chat.wait's `after`. */
508
+ result: z.object({ at: z.number().optional() }),
509
+ },
510
+ /**
511
+ * Takes an image for the next chat.send to this tab: one image a request,
512
+ * small enough for a frame. Kept 10 minutes, for this device only.
513
+ */
514
+ "chat.attach": {
515
+ capability: "chat.control",
516
+ params: z.object({
517
+ ...TabRef,
518
+ mediaType: ImageMediaTypeSchema,
519
+ /** Base64, of at most MAX_ATTACHMENT_BYTES bytes. */
520
+ data: z
521
+ .string()
522
+ .min(1)
523
+ .max(Math.ceil(MAX_ATTACHMENT_BYTES / 3) * 4),
524
+ }),
525
+ result: z.object({ attachmentId: z.string() }),
181
526
  },
182
527
  /** Answers the permission request `requestId`, as its card's buttons do. */
183
528
  "chat.answer": {
@@ -192,32 +537,333 @@ export const RPC_METHODS = {
192
537
  updatedInput: z.record(z.unknown()).optional(),
193
538
  /** With deny: the turn stops too, as the card's "No, and stop" does. */
194
539
  stop: z.boolean().optional(),
540
+ /** With allow: the request's suggestions to keep from now on, by index ("always allow"). */
541
+ always: z.array(z.number().int().nonnegative()).optional(),
542
+ from: AgentSenderSchema.optional(),
195
543
  }),
196
544
  result: Ok,
197
545
  },
198
- "chat.interrupt": { capability: "chat.control", params: z.object(TabRef), result: Ok },
199
- /** Opens a chat in one of the serving device's workspaces, sending it `prompt` if given. */
546
+ "chat.interrupt": {
547
+ capability: "chat.control",
548
+ params: z.object({ ...TabRef, from: AgentSenderSchema.optional() }),
549
+ result: Ok,
550
+ },
551
+ /**
552
+ * Sets any of model, effort and permission mode, as the composer's chips
553
+ * do; what it runs with after. A mode the serving device's policy does
554
+ * not allow is refused (Forbidden) with why.
555
+ */
556
+ "chat.configure": {
557
+ capability: "chat.control",
558
+ params: z.object({
559
+ ...TabRef,
560
+ model: z.string().min(1).max(100).optional(),
561
+ effort: z.string().min(1).max(100).optional(),
562
+ permissionMode: z.string().min(1).max(100).optional(),
563
+ from: AgentSenderSchema.optional(),
564
+ }),
565
+ result: ChatSettingsSchema,
566
+ },
567
+ /** Runs one of the app's slash commands (chat.commands' `command` ones), `args` after its name. */
568
+ "chat.command": {
569
+ capability: "chat.control",
570
+ params: z.object({
571
+ ...TabRef,
572
+ name: z.string().min(1).max(100),
573
+ args: z.string().max(10_000).optional(),
574
+ }),
575
+ result: z.object({
576
+ /** What it said, when it says something rather than changing the chat. */
577
+ message: z.string().optional(),
578
+ /** The tab it opened, when it opened one. */
579
+ tabId: z.string().optional(),
580
+ }),
581
+ },
582
+ /** Stops a task of the chat's (a background run, a subagent), as the side panel's button does. */
583
+ "chat.stopTask": {
584
+ capability: "chat.control",
585
+ params: z.object({ ...TabRef, taskId: z.string() }),
586
+ result: Ok,
587
+ },
588
+ "chat.rename": {
589
+ capability: "chat.control",
590
+ params: z.object({ ...TabRef, title: z.string().min(1).max(200) }),
591
+ result: z.object({ title: z.string() }),
592
+ },
593
+ /** Opens a past chat (history.list) in a new tab. */
594
+ "chat.resume": {
595
+ capability: "chat.control",
596
+ params: z.object({
597
+ workspaceId: z.string(),
598
+ agent: z.enum(["claude", "codex"]),
599
+ sessionId: z.string(),
600
+ }),
601
+ result: z.object({ tabId: z.string() }),
602
+ },
603
+ /**
604
+ * Opens a chat in one of the serving device's workspaces, sending it
605
+ * `prompt` if given. `model` and `effort` are set before the prompt goes,
606
+ * named as the agent's own menus name them ("haiku", "low"); the tab's
607
+ * profile's own otherwise.
608
+ */
200
609
  "chat.new": {
201
610
  capability: "chat.control",
202
611
  params: z.object({
203
612
  workspaceId: z.string(),
204
613
  agent: z.enum(["claude", "codex"]),
205
614
  prompt: z.string().optional(),
615
+ model: z.string().min(1).max(100).optional(),
616
+ effort: z.string().min(1).max(100).optional(),
617
+ permissionMode: z.string().min(1).max(100).optional(),
618
+ /** The agent profile, by id or name (profiles.list); the workspace's otherwise. */
619
+ profile: z.string().min(1).max(100).optional(),
620
+ /** A folder inside the workspace to run in; its own folder otherwise. */
621
+ folder: z.string().min(1).max(4096).optional(),
622
+ /** Sent by an agent: the chat shows the prompt as that agent's. */
623
+ from: AgentSenderSchema.optional(),
624
+ }),
625
+ result: z.object({
626
+ tabId: z.string(),
627
+ /** The tab opened, but the model or effort could not be set: the prompt was not sent. */
628
+ error: z.string().optional(),
206
629
  }),
207
- result: z.object({ tabId: z.string() }),
208
630
  },
209
- "chat.close": { capability: "chat.control", params: z.object(TabRef), result: Ok },
210
- /** A terminal's screen and the lines above it, as text. Nothing can be typed into it. */
631
+ "chat.close": {
632
+ capability: "chat.control",
633
+ params: z.object({ ...TabRef, from: AgentSenderSchema.optional() }),
634
+ result: Ok,
635
+ },
636
+ /**
637
+ * A terminal's screen and the lines above it, as text; with `ansi`, with
638
+ * its colors and styles as SGR escapes (no other escapes).
639
+ */
211
640
  "terminal.read": {
212
641
  capability: "terminal.read",
213
- params: z.object({ ...TabRef, lines: z.number().int().positive().max(5000).optional() }),
642
+ params: z.object({
643
+ ...TabRef,
644
+ lines: z.number().int().positive().max(5000).optional(),
645
+ ansi: z.boolean().optional(),
646
+ }),
214
647
  result: z.object({ text: z.string() }),
215
648
  },
649
+ /** The terminal's text now, as terminal.read; then a terminal.update each time it changes, until terminal.unwatch. */
650
+ "terminal.watch": {
651
+ capability: "terminal.read",
652
+ params: z.object({
653
+ ...TabRef,
654
+ lines: z.number().int().positive().max(5000).optional(),
655
+ ansi: z.boolean().optional(),
656
+ }),
657
+ result: z.object({ text: z.string(), watchId: z.string() }),
658
+ },
659
+ "terminal.unwatch": {
660
+ capability: "terminal.read",
661
+ params: z.object({ watchId: z.string() }),
662
+ result: Ok,
663
+ },
664
+ /**
665
+ * Types into a terminal: `text` as pasted, then `keys` by name
666
+ * (TERMINAL_KEYS), then Enter when `enter`. Sent by an agent (`from`), the receiving device counts it
667
+ * as that agent's.
668
+ */
669
+ "terminal.input": {
670
+ capability: "terminal.control",
671
+ params: z.object({
672
+ ...TabRef,
673
+ text: z
674
+ .string()
675
+ .max(64 * 1024)
676
+ .optional(),
677
+ keys: z.array(TerminalKeySchema).max(64).optional(),
678
+ enter: z.boolean().optional(),
679
+ from: AgentSenderSchema.optional(),
680
+ }),
681
+ /** `at`: when it was typed, by the serving device's clock, for terminal.wait's `after`. */
682
+ result: z.object({ at: z.number().optional() }),
683
+ },
684
+ /**
685
+ * Waits up to `timeoutMs` (at most MAX_WAIT_MS) as `nexus terminal wait`
686
+ * does: for the terminal's agent to finish its turn, its shell to be back
687
+ * at the prompt, or quiet output; with `exit`, for its program to end;
688
+ * with `match`, for a line to match. Ask again while `done` is false.
689
+ */
690
+ "terminal.wait": {
691
+ capability: "terminal.read",
692
+ params: z.object({
693
+ ...TabRef,
694
+ timeoutMs: z.number().int().positive().max(MAX_WAIT_MS).optional(),
695
+ /** A terminal.input's `at`: a turn must have started after it. */
696
+ after: z.number().optional(),
697
+ exit: z.boolean().optional(),
698
+ match: z.string().min(1).max(1000).optional(),
699
+ /** Quiet for this long counts as idle, for a program that is no agent or shell. */
700
+ quietMs: z.number().int().positive().max(600_000).optional(),
701
+ }),
702
+ result: z.object({
703
+ done: z.boolean(),
704
+ /** idle, completed, needsInput, permissionPending, error, notStarted, notified, exited, matched, or waiting. */
705
+ state: z.string(),
706
+ message: z.string().optional(),
707
+ }),
708
+ },
709
+ /**
710
+ * Answers the permission request a terminal's Claude Code holds for the
711
+ * app (its session's `waiting.requestId`), as the agent list's buttons do.
712
+ * An answer for agents, so chat.control's: no typing goes in.
713
+ */
714
+ "terminal.answer": {
715
+ capability: "chat.control",
716
+ params: z.object({
717
+ ...TabRef,
718
+ requestId: z.string(),
719
+ decision: z.enum(["allow", "allowAlways", "deny", "denyStop"]),
720
+ from: AgentSenderSchema.optional(),
721
+ }),
722
+ result: Ok,
723
+ },
724
+ /** The shells a new terminal can run, as "New terminal with" lists them. */
725
+ "terminal.shells": {
726
+ capability: "terminal.control",
727
+ params: z.object({ workspaceId: z.string() }),
728
+ result: z.object({
729
+ shells: z.array(z.object({ id: z.string(), name: z.string(), isDefault: z.boolean() })),
730
+ }),
731
+ },
732
+ /** Opens a terminal: a shell (by its id), in a folder inside the workspace, running `command`. */
733
+ "terminal.open": {
734
+ capability: "terminal.control",
735
+ params: z.object({
736
+ workspaceId: z.string(),
737
+ shell: z.string().min(1).max(200).optional(),
738
+ folder: z.string().min(1).max(4096).optional(),
739
+ command: z.string().min(1).max(10_000).optional(),
740
+ title: z.string().min(1).max(200).optional(),
741
+ from: AgentSenderSchema.optional(),
742
+ }),
743
+ result: z.object({ tabId: z.string() }),
744
+ },
745
+ "terminal.rename": {
746
+ capability: "terminal.control",
747
+ params: z.object({ ...TabRef, title: z.string().min(1).max(200) }),
748
+ result: z.object({ title: z.string() }),
749
+ },
750
+ /** Closes a terminal; one running a program only with `force`. */
751
+ "terminal.close": {
752
+ capability: "terminal.control",
753
+ params: z.object({
754
+ ...TabRef,
755
+ force: z.boolean().optional(),
756
+ from: AgentSenderSchema.optional(),
757
+ }),
758
+ result: Ok,
759
+ },
760
+ /**
761
+ * A folder's entries, folders first. `path` is absolute or inside the
762
+ * workspace's folder ("" for it); it must be in the workspace's folders
763
+ * (its own, and those its chats were given).
764
+ */
765
+ "files.list": {
766
+ capability: "files",
767
+ params: z.object({ workspaceId: z.string(), path: z.string().max(4096) }),
768
+ result: z.object({
769
+ /** The folder, absolute. */
770
+ path: z.string(),
771
+ entries: z.array(z.object({
772
+ name: z.string(),
773
+ kind: z.enum(["file", "dir", "link"]),
774
+ size: z.number().int().nonnegative().optional(),
775
+ })),
776
+ /** More than MAX_LIST_ENTRIES: the rest left out. */
777
+ truncated: z.boolean().optional(),
778
+ }),
779
+ },
780
+ /** A file: text (its start, up to `maxBytes`), an image made small enough for a frame, or that it is binary. */
781
+ "files.read": {
782
+ capability: "files",
783
+ params: z.object({
784
+ workspaceId: z.string(),
785
+ path: z.string().min(1).max(4096),
786
+ maxBytes: z.number().int().positive().max(MAX_READ_BYTES).optional(),
787
+ }),
788
+ result: z.discriminatedUnion("kind", [
789
+ z.object({
790
+ kind: z.literal("text"),
791
+ path: z.string(),
792
+ text: z.string(),
793
+ size: z.number().int().nonnegative(),
794
+ truncated: z.boolean(),
795
+ }),
796
+ z.object({
797
+ kind: z.literal("image"),
798
+ path: z.string(),
799
+ mediaType: ImageMediaTypeSchema,
800
+ data: z.string(),
801
+ size: z.number().int().nonnegative(),
802
+ }),
803
+ z.object({
804
+ kind: z.literal("binary"),
805
+ path: z.string(),
806
+ size: z.number().int().nonnegative(),
807
+ }),
808
+ ]),
809
+ },
810
+ /** The workspace's branch, how it stands against its upstream, and its changed files. */
811
+ "git.status": {
812
+ capability: "git",
813
+ params: z.object({ workspaceId: z.string() }),
814
+ result: z.object({
815
+ /** null when no branch is checked out. */
816
+ branch: z.string().nullable(),
817
+ upstream: z.string().optional(),
818
+ ahead: z.number().int().nonnegative(),
819
+ behind: z.number().int().nonnegative(),
820
+ files: z.array(GitFileSchema),
821
+ /** More files than a frame takes: the rest left out. */
822
+ truncated: z.boolean().optional(),
823
+ }),
824
+ },
825
+ /** A file's changes, staged or not, in hunks as an edit's patch has them. */
826
+ "git.diff": {
827
+ capability: "git",
828
+ params: z.object({
829
+ workspaceId: z.string(),
830
+ path: z.string().min(1).max(4096),
831
+ staged: z.boolean().optional(),
832
+ }),
833
+ result: z.object({
834
+ path: z.string(),
835
+ oldPath: z.string().optional(),
836
+ binary: z.boolean().optional(),
837
+ hunks: z.array(DiffHunkSchema),
838
+ /** More than a frame takes: the hunks after left out. */
839
+ truncated: z.boolean().optional(),
840
+ }),
841
+ },
842
+ /** The branch's commits, latest first. */
843
+ "git.log": {
844
+ capability: "git",
845
+ params: z.object({
846
+ workspaceId: z.string(),
847
+ limit: z.number().int().positive().max(200).optional(),
848
+ skip: z.number().int().nonnegative().optional(),
849
+ }),
850
+ result: z.object({
851
+ commits: z.array(z.object({
852
+ hash: z.string(),
853
+ subject: z.string(),
854
+ author: z.string(),
855
+ /** Unix milliseconds. */
856
+ at: z.number(),
857
+ refs: z.array(z.string()).optional(),
858
+ })),
859
+ }),
860
+ },
216
861
  };
217
862
  /** The notifications a serving device sends, by method. */
218
863
  export const RPC_NOTIFICATIONS = {
219
864
  "sessions.update": SessionsUpdateSchema,
220
865
  "chat.update": ChatUpdateSchema,
866
+ "terminal.update": TerminalUpdateSchema,
221
867
  };
222
868
  export function isRpcMethod(method) {
223
869
  return Object.hasOwn(RPC_METHODS, method);