@teable/agent-gateway 1.0.0 → 1.0.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.
@@ -4,17 +4,31 @@
4
4
  * and the builder's agent panel, over two WebSockets.
5
5
  *
6
6
  * gateway ⇄ hub GET GATEWAY_PATH, with `Authorization: Bearer <gateway
7
- * token>`. ACP's JSON-RPC messages, unchanged, in
8
- * `{ session, acp }` frames; and control frames, which
9
- * have a `type`: the hub asks the gateway to open and
10
- * close sessions, the gateway says what it runs.
11
- * tab ⇄ hub GET `${APPS_PATH}/:appId/agent?since=<seq>` (APPS_PATH
12
- * from @teable/app/protocol), with the identity cookie.
13
- * The hub sends a snapshot, then each stored event after
14
- * `since` as `{ type: "event", seq, session, at, event }`,
15
- * then live ones; the tab sends commands `{ id, session,
16
- * op }`, each acknowledged once by its `id`. Everything the
17
- * hub sends a tab has a `type`.
7
+ * token>`. The gateway's hello first, then ACP's JSON-RPC
8
+ * messages, unchanged, in `{ session, acp }` frames, with
9
+ * what agents ask the person beyond ACP in one
10
+ * agent-neutral form (ASK); and control frames, which have
11
+ * a `type`: the hub asks the gateway to open and close
12
+ * sessions, the gateway says what became of them. Each
13
+ * side numbers these frames (`n`) and keeps each until the
14
+ * other says it has it (`received`), and sends again what
15
+ * the other lacks when the gateway dials again: while the
16
+ * gateway's process lives, nothing is lost and nothing is
17
+ * taken twice.
18
+ * tab ⇄ hub GET `${APPS_PATH}/:appId/agent?session=<id>&since=<seq>`
19
+ * (APPS_PATH from @teable/app/protocol), with the identity
20
+ * cookie. The hub sends a snapshot of the app's sessions;
21
+ * then, as `{ type: "event", seq, session, at, event }`,
22
+ * the app's events after `since` when they are few, else
23
+ * the latest events of the session the tab shows; then
24
+ * `replayed`, then live events of every session of the
25
+ * app. Older events come when the tab asks (`history`).
26
+ * The tab sends commands `{ id, session, op }`, each
27
+ * acknowledged once by its `id`. Everything the hub sends
28
+ * a tab has a `type`. A tab the runtime refuses, or whose
29
+ * identity runs out, gets its socket closed with a
30
+ * TAB_CLOSE code, which says whether to dial again:
31
+ * browsers are not told why an upgrade failed.
18
32
  *
19
33
  * Every message is one JSON text frame, except the keep-alive: either side
20
34
  * of a hub socket may send PING, which the hub answers with PONG.
@@ -26,9 +40,12 @@
26
40
  export declare const GATEWAY_PATH = "/api/v4/agent/gateway";
27
41
  /**
28
42
  * This protocol's version, which the gateway says in its hello. The hub
29
- * closes the socket of a gateway that speaks another (GATEWAY_CLOSE.unsupported).
43
+ * closes the socket of a gateway that speaks another (GATEWAY_CLOSE.unsupported),
44
+ * which then exits: a gateway already running in a sandbox may be older
45
+ * than the hub. 2: the hello comes first, and frames are numbered and
46
+ * acknowledged.
30
47
  */
31
- export declare const AGENT_PROTOCOL = 1;
48
+ export declare const AGENT_PROTOCOL = 2;
32
49
  /** The keep-alive: sent as a text frame, answered by the hub without waking it. */
33
50
  export declare const PING = "ping";
34
51
  export declare const PONG = "pong";
@@ -44,6 +61,44 @@ export declare const GATEWAY_CLOSE: {
44
61
  readonly expired: 4001;
45
62
  readonly unsupported: 4002;
46
63
  };
64
+ /**
65
+ * The hub closed the gateway's socket because it sent frames faster than
66
+ * the hub takes them. Not for good: the gateway dials again after its
67
+ * usual wait and sends again what the hub did not take.
68
+ */
69
+ export declare const GATEWAY_TOO_FAST = 4429;
70
+ /**
71
+ * The most characters of JSON a gateway's frame may hold. The gateway
72
+ * shrinks what an agent says to fit (`shrink`); the hub shrinks a bigger
73
+ * one itself. Either says how much was cut (`shrunk`).
74
+ */
75
+ export declare const MAX_FRAME: number;
76
+ /**
77
+ * A message as it fits `max` characters of JSON, and how many were cut:
78
+ * its longest strings are cut short (to a length all of them fit under,
79
+ * with "…" where they end); a message that still does not fit keeps only
80
+ * what names it (`jsonrpc`, `id`, `method`), its params or result null.
81
+ */
82
+ export declare function shrink(message: AcpMessage, max: number): {
83
+ message: AcpMessage;
84
+ shrunk: number;
85
+ };
86
+ /**
87
+ * Why the runtime closed a tab's socket. A tab it refuses gets a socket
88
+ * closed at once: signed out, no access, no such app (dial again only once
89
+ * that changes: the person signs in, is given access), a malformed request,
90
+ * or too many requests (dial again later). `expired`: the identity the tab
91
+ * connected with ran out; it dials again at once, with the fresh one the
92
+ * builder keeps, and has the rights that one gives.
93
+ */
94
+ export declare const TAB_CLOSE: {
95
+ readonly expired: 4000;
96
+ readonly badInput: 4400;
97
+ readonly unauthenticated: 4401;
98
+ readonly forbidden: 4403;
99
+ readonly notFound: 4404;
100
+ readonly rateLimited: 4429;
101
+ };
47
102
  /** Session ids, made by the hub: `ses` and 16 letters or digits. */
48
103
  export declare const SESSION_ID: RegExp;
49
104
  /** A JSON-RPC request id. */
@@ -261,6 +316,99 @@ export interface AcpSessionSetup {
261
316
  models?: unknown;
262
317
  configOptions?: unknown;
263
318
  }
319
+ export declare const ASK: {
320
+ /** Questions for the person (AgentQuestionRequest), answered with an AgentQuestionOutcome. */
321
+ readonly question: "_teable/question";
322
+ /** A plan for the person to approve before the agent acts on it (AgentPlanRequest), answered with an AgentPlanOutcome. */
323
+ readonly plan: "_teable/plan";
324
+ };
325
+ /** A question: the person picks an option, or several, or answers in their own words. */
326
+ export interface AgentQuestion {
327
+ /** The question, as a full sentence. */
328
+ question: string;
329
+ options: AgentQuestionOption[];
330
+ /** Whether more than one option may be picked. */
331
+ multiSelect: boolean;
332
+ /** Whether the person may answer in their own words instead of an option. */
333
+ freeText: boolean;
334
+ }
335
+ export interface AgentQuestionOption {
336
+ label: string;
337
+ /** What picking it means. */
338
+ description: string | null;
339
+ /** What to show while it is considered: a sketch, a snippet. */
340
+ preview: string | null;
341
+ }
342
+ export interface AgentQuestionRequest {
343
+ sessionId: string;
344
+ /** The agent's tool call that asks, when it says. */
345
+ toolCallId: string | null;
346
+ questions: AgentQuestion[];
347
+ }
348
+ /** The answer to questions: for each, in order, the labels picked and the person's own words; or none. */
349
+ export type AgentQuestionOutcome = {
350
+ outcome: "answered";
351
+ answers: {
352
+ picked: string[];
353
+ text: string | null;
354
+ }[];
355
+ } | {
356
+ outcome: "cancelled";
357
+ };
358
+ export interface AgentPlanRequest {
359
+ sessionId: string;
360
+ /** The agent's tool call that asks, when it says. */
361
+ toolCallId: string | null;
362
+ /** The plan, in Markdown. */
363
+ plan: string;
364
+ }
365
+ /** The answer to a plan: approved, or rejected with what to change; or none. */
366
+ export type AgentPlanOutcome = {
367
+ outcome: "approved";
368
+ } | {
369
+ outcome: "rejected";
370
+ feedback: string | null;
371
+ } | {
372
+ outcome: "cancelled";
373
+ };
374
+ /** An answer to anything the agent asks the person: a permission, questions, a plan. */
375
+ export type AgentAnswer = AcpPermissionOutcome | AgentQuestionOutcome | AgentPlanOutcome;
376
+ /**
377
+ * What the gateway's shims tell the hub an agent did, when the agent says
378
+ * it in words of its own beyond ACP: notifications in the session's ACP
379
+ * stream, where they happened.
380
+ */
381
+ export declare const NOTICE: {
382
+ /** The agent took a message sent while its turn ran (a `steer`) into that turn (SteeredNotice). */
383
+ readonly steered: "_teable/steered";
384
+ /** A background task of the agent's started, or ended (TaskNotice). */
385
+ readonly task: "_teable/task";
386
+ };
387
+ export interface SteeredNotice {
388
+ sessionId: string;
389
+ /** The id the hub gave the message, as for a `session/prompt`. */
390
+ request: JsonRpcId;
391
+ }
392
+ /**
393
+ * A background task an agent runs beyond its turn (a server, a long build),
394
+ * as it reports it: running, then finished (exit code 0) or failed (another
395
+ * exit code, or killed). While it runs, what it prints comes on the tool
396
+ * call that started it, when the agent says which (`toolCallId`).
397
+ */
398
+ export interface TaskNotice {
399
+ sessionId: string;
400
+ /** The agent's id for it. */
401
+ task: string;
402
+ /** What it runs, for people. */
403
+ title: string;
404
+ status: "running" | "finished" | "failed";
405
+ toolCallId: string | null;
406
+ /** What it printed, its end when long (TASK_OUTPUT); null while it runs. */
407
+ output: string | null;
408
+ exitCode: number | null;
409
+ }
410
+ /** The most of a finished task's output a TaskNotice holds: its last characters. */
411
+ export declare const TASK_OUTPUT = 8000;
264
412
  export declare function isAcpRequest(message: AcpMessage): message is AcpRequest;
265
413
  export declare function isAcpNotification(message: AcpMessage): message is AcpNotification;
266
414
  export declare function isAcpResponse(message: AcpMessage): message is AcpResponse;
@@ -277,15 +425,21 @@ export interface AgentCapabilities {
277
425
  /** Its name, for people. */
278
426
  title: string;
279
427
  /**
280
- * What happens to a message sent with `steer` while a turn runs: "steer"
281
- * puts it into the running turn; "interrupt" stops the turn and sends it next.
428
+ * What happens to a message sent with `steer` while a turn runs:
429
+ * "native", the agent takes it into the running turn (the gateway's shim
430
+ * sends it as the agent takes one); "interrupt", the hub stops the turn
431
+ * and the message runs next.
282
432
  */
283
- steering: "steer" | "interrupt";
433
+ steering: "native" | "interrupt";
284
434
  /** Whether it asks before acting (`session/request_permission`). */
285
435
  approvals: boolean;
436
+ /** Whether it asks the person questions (ASK.question). */
437
+ questions: boolean;
438
+ /** Whether it asks the person to approve its plan before it acts on it (ASK.plan). */
439
+ planApproval: boolean;
286
440
  /** Whether it reports its plan (`plan` updates). */
287
441
  plans: boolean;
288
- /** Whether it runs background tasks that outlive a turn. */
442
+ /** Whether it runs background tasks that outlive a turn, and reports them (NOTICE.task). */
289
443
  backgroundTasks: boolean;
290
444
  /** Whether a session survives a restart of the agent or the sandbox (`session/load`). */
291
445
  resume: boolean;
@@ -294,11 +448,32 @@ export interface AgentCapabilities {
294
448
  * ACP for one session, both ways: relayed unchanged between the hub and the
295
449
  * session's agent. The gateway answers a request for a session whose agent
296
450
  * does not run with an error, and keeps vendor extensions (methods starting
297
- * with `_`) to its per-agent shims: the hub sees standard ACP.
451
+ * with `_`) to its per-agent shims: the hub sees standard ACP, and the
452
+ * requests the shims make of agents' own (ASK).
298
453
  */
299
454
  export interface AcpFrame {
300
455
  session: string;
301
456
  acp: AcpMessage;
457
+ /** How many characters the gateway cut from the message to fit MAX_FRAME (`shrink`). */
458
+ shrunk?: number;
459
+ }
460
+ /**
461
+ * A frame the sender keeps until the other side says it has it, numbered
462
+ * in the order sent: by the gateway from 1 in each of its processes, by the
463
+ * hub on from where it got to, whichever gateway it talks to. A side takes
464
+ * a frame whose number it has seen already only to say again that it has it.
465
+ */
466
+ export type Numbered<T> = T & {
467
+ n: number;
468
+ };
469
+ /**
470
+ * Either side has every numbered frame up to `n` from the other, and keeps
471
+ * none of them any more. Not numbered: it is sent again with the next frame
472
+ * taken, or the next hello.
473
+ */
474
+ export interface Received {
475
+ type: "received";
476
+ n: number;
302
477
  }
303
478
  /** What the hub asks of the gateway. */
304
479
  export type HubControl =
@@ -320,38 +495,50 @@ export type HubControl =
320
495
  | {
321
496
  type: "close";
322
497
  session: string;
323
- };
324
- /** The hub sends nothing before the gateway's hello. */
325
- export type HubToGateway = AcpFrame | HubControl;
326
- /** A session the gateway runs an agent for. */
327
- export interface GatewaySession {
328
- session: string;
329
- /** The agent's own session id; null while the agent starts. */
330
- acpSessionId: string | null;
331
- /** The ids of the `session/prompt` requests its agent has not answered yet. */
332
- prompts: JsonRpcId[];
333
498
  }
334
- /** What the gateway tells the hub. */
335
- export type GatewayControl =
336
499
  /**
337
- * Sent on every connection, after what the gateway kept while it was
338
- * disconnected: the protocol it speaks, which gateway process this is
339
- * (`instance`) and its package version, its agents, and the sessions it
340
- * runs now. A session it does not list has no agent.
500
+ * Send a message into the session's running turn, to an agent that takes
501
+ * one natively (`steering: "native"`): the gateway's shim sends it as the
502
+ * agent does, under `request`. The agent takes it into the turn (and the
503
+ * shim says so: NOTICE.steered), or runs it as a turn of its own once the
504
+ * running one ends, answering `request` as a `session/prompt`.
341
505
  */
342
- {
506
+ | {
507
+ type: "steer";
508
+ session: string;
509
+ request: string;
510
+ prompt: AcpContentBlock[];
511
+ };
512
+ /**
513
+ * The hub sends nothing before the gateway's hello; then what the gateway
514
+ * has not taken yet of what it sent before, again, and on.
515
+ */
516
+ export type HubToGateway = Numbered<AcpFrame | HubControl> | Received;
517
+ /**
518
+ * The first frame on every connection: the protocol the gateway speaks
519
+ * (anything else first is an older gateway's, which the hub closes as
520
+ * unsupported), which gateway process this is (`instance`) and its package
521
+ * version, its agents, the last of the hub's frames it took (`received`)
522
+ * and the last it numbered itself (`sent`). Its own frames that the hub may
523
+ * lack follow, again. A process the hub has not met runs no agents: the
524
+ * turns of the one before end, and what the hub kept for it is void.
525
+ */
526
+ export interface GatewayHello {
343
527
  type: "hello";
344
528
  protocol: number;
345
529
  instance: string;
346
530
  version: string;
347
531
  agents: AgentCapabilities[];
348
- sessions: GatewaySession[];
532
+ received: number;
533
+ sent: number;
349
534
  }
535
+ /** What the gateway tells the hub of its sessions. */
536
+ export type GatewayControl =
350
537
  /**
351
538
  * The session's agent runs: it resumed the agent's session (`resumed`), or
352
539
  * started a new one, which knows nothing of the conversation before.
353
540
  */
354
- | {
541
+ {
355
542
  type: "opened";
356
543
  session: string;
357
544
  acpSessionId: string;
@@ -371,7 +558,7 @@ export type GatewayControl =
371
558
  session: string;
372
559
  message: string;
373
560
  };
374
- export type GatewayToHub = AcpFrame | GatewayControl;
561
+ export type GatewayToHub = GatewayHello | Numbered<AcpFrame | GatewayControl> | Received;
375
562
  /**
376
563
  * A command from a tab. `id` names it: any string the tab makes up, unique
377
564
  * among the person's commands, so that sending it again (after a reconnect)
@@ -384,9 +571,9 @@ export interface TabCommand {
384
571
  op: TabOp;
385
572
  }
386
573
  /**
387
- * What a command does. Starting a session, prompting and answering
388
- * permission requests need a creator or owner of the app's space, as
389
- * publishing does; any member may connect, read their own sessions and cancel.
574
+ * What a command does. Starting a session, prompting and answering what the
575
+ * agent asks need a creator or owner of the app's space, as publishing does;
576
+ * any member may connect, read their own sessions and cancel.
390
577
  */
391
578
  export type TabOp =
392
579
  /** Start a session about the tab's app, with `prompt` as its first message, if given. */
@@ -397,28 +584,71 @@ export type TabOp =
397
584
  }
398
585
  /**
399
586
  * Send a message. While a turn runs it waits in the queue; with `steer`
400
- * it goes to the running turn instead, as the agent's `steering` says.
587
+ * it goes to the running turn now: an agent that steers natively takes it
588
+ * in (`steered`) unless the turn ends first, when it runs next; for any
589
+ * other, the turn stops and the message runs next.
401
590
  */
402
591
  | {
403
592
  type: "prompt";
404
593
  prompt: AcpContentBlock[];
405
594
  steer?: boolean;
406
595
  }
407
- /** Stop a turn: the running one, or a queued one by its number. */
596
+ /** Stop the running turn; `turn` names it, so that a stop meant for one that ended stops no other. */
408
597
  | {
409
598
  type: "cancel";
410
599
  turn?: number;
411
600
  }
412
- /** Answer a permission request, named by the `seq` of the event that asked. The first answer wins. */
601
+ /** Change a message that waits in the queue. */
602
+ | {
603
+ type: "edit";
604
+ turn: number;
605
+ prompt: AcpContentBlock[];
606
+ }
607
+ /** Take back a message that waits in the queue: it never runs. */
608
+ | {
609
+ type: "remove";
610
+ turn: number;
611
+ }
612
+ /** Run a message that waits in the queue next. */
613
+ | {
614
+ type: "front";
615
+ turn: number;
616
+ }
617
+ /**
618
+ * Close the session: what waits is taken back, a running turn is stopped
619
+ * and its agent too; it takes no more messages, and stays readable.
620
+ */
621
+ | {
622
+ type: "close";
623
+ }
624
+ /** Archive the session, closing it, or bring it back to the list (closed still). */
625
+ | {
626
+ type: "archive";
627
+ archived: boolean;
628
+ }
629
+ /**
630
+ * Answer what the agent asks the person (a permission, questions, a plan),
631
+ * named by the `seq` of the event that asked, with the outcome its kind
632
+ * takes. The first answer wins. While the gateway is away the answer waits
633
+ * for it, since the agent waits too.
634
+ */
413
635
  | {
414
- type: "permission";
636
+ type: "answer";
415
637
  request: number;
416
- outcome: AcpPermissionOutcome;
638
+ outcome: AgentAnswer;
417
639
  };
418
- /** Why a command was refused: malformed, not allowed, nothing to act on, or too late. */
419
- export type AgentErrorCode = "BAD_INPUT" | "FORBIDDEN" | "NOT_FOUND" | "CONFLICT";
640
+ /**
641
+ * Why a command was refused: malformed (a tab's own mistake), not this
642
+ * person's to do here, nothing to act on, what the agent asked was answered
643
+ * already or can no longer be, the turn has ended, the message went to the
644
+ * agent already (it no longer waits), the session is closed, the message is
645
+ * larger than MAX_FRAME, or the person's hub holds as much as it may. Tabs
646
+ * tell the person in their own words, by the code.
647
+ */
648
+ export type AgentErrorCode = "BAD_INPUT" | "FORBIDDEN" | "NOT_FOUND" | "ANSWERED" | "EXPIRED" | "ENDED" | "STARTED" | "CLOSED" | "TOO_LARGE" | "FULL";
420
649
  export interface AgentError {
421
650
  code: AgentErrorCode;
651
+ /** For people reading logs, in English. */
422
652
  message: string;
423
653
  }
424
654
  /** The answer to a command, sent once per command id to the tab that sent it. */
@@ -456,8 +686,12 @@ export interface SessionInfo {
456
686
  turn: number | null;
457
687
  /** The turns waiting, in the order they will run. */
458
688
  queued: number[];
459
- /** The permission requests waiting for an answer, by the `seq` of the event that asked. */
460
- permissions: number[];
689
+ /** What the agent asks the person and waits for an answer to, by the `seq` of the event that asked. */
690
+ requests: number[];
691
+ /** It takes no more messages. */
692
+ closed: boolean;
693
+ /** It is left out of the list a person picks from. */
694
+ archived: boolean;
461
695
  lastSeq: number;
462
696
  }
463
697
  /** Something that happened in a session: stored, numbered and sent to every tab of its app. */
@@ -482,20 +716,93 @@ export type SessionEvent = {
482
716
  turn: number;
483
717
  }
484
718
  /**
485
- * A message from the agent, unchanged: `session/update` notifications and
486
- * `session/request_permission` requests, during `turn` (null between turns).
719
+ * A message from the agent, unchanged: `session/update` notifications, and
720
+ * what it asks the person (`session/request_permission` requests, and ASK's),
721
+ * during `turn` (null between turns).
487
722
  */
488
723
  | {
489
724
  type: "acp";
490
725
  turn: number | null;
491
726
  acp: AcpMessage;
727
+ /** How many characters were cut from it to keep it under MAX_FRAME. */
728
+ shrunk?: number;
729
+ }
730
+ /**
731
+ * A background task of the agent's (TaskNotice, less its session):
732
+ * started, or ended, as the agent reports it. Each report of the same
733
+ * `task` stands for the one before.
734
+ */
735
+ | ({
736
+ type: "task";
737
+ } & Omit<TaskNotice, "sessionId">)
738
+ /**
739
+ * The hub would not take more of what the agent sends: the session holds
740
+ * as much as one may (`session`: it is closed), or the person's hub does
741
+ * (`hub`: the turn stops, and no new message is taken); or the gateway
742
+ * sent too fast (`rate`), and was made to send again more slowly.
743
+ */
744
+ | {
745
+ type: "limited";
746
+ limit: "session" | "hub" | "rate";
747
+ }
748
+ /**
749
+ * What the agent asked (the `seq` of its event) was answered, by `by`. The
750
+ * answer reaches the agent at once, or when its gateway is back, unless
751
+ * the request expires first.
752
+ */
753
+ | {
754
+ type: "answered";
755
+ request: number;
756
+ outcome: AgentAnswer;
757
+ by: string;
492
758
  }
493
- /** A permission request (the `seq` of its event) was answered, by `by`; null when it was cancelled. */
759
+ /**
760
+ * What the agent asked can no longer be answered: its turn was stopped or
761
+ * ended, or its agent stopped, before an answer reached it. An answer given
762
+ * meanwhile went nowhere.
763
+ */
494
764
  | {
495
- type: "permission_answered";
765
+ type: "expired";
496
766
  request: number;
497
- outcome: AcpPermissionOutcome;
498
- by: string | null;
767
+ }
768
+ /** A message that waits was changed. */
769
+ | {
770
+ type: "edited";
771
+ turn: number;
772
+ by: string;
773
+ prompt: AcpContentBlock[];
774
+ }
775
+ /** A message that waited was taken back: it never runs. */
776
+ | {
777
+ type: "removed";
778
+ turn: number;
779
+ by: string;
780
+ }
781
+ /** A message that waits runs next. */
782
+ | {
783
+ type: "moved";
784
+ turn: number;
785
+ by: string;
786
+ }
787
+ /**
788
+ * The message of `turn`, sent to steer, went into the running turn
789
+ * `into`: it is part of that turn from here, and `turn` never runs.
790
+ */
791
+ | {
792
+ type: "steered";
793
+ turn: number;
794
+ into: number;
795
+ }
796
+ /** The session was closed: it takes no more messages. */
797
+ | {
798
+ type: "closed";
799
+ by: string;
800
+ }
801
+ /** The session was archived, or brought back to the list. */
802
+ | {
803
+ type: "archived";
804
+ archived: boolean;
805
+ by: string;
499
806
  }
500
807
  /** Someone asked to stop the turn; it ends with `turn_ended`. */
501
808
  | {
@@ -530,15 +837,38 @@ export interface TabEvent {
530
837
  /** When the hub stored it, in milliseconds since the epoch. */
531
838
  at: number;
532
839
  event: SessionEvent;
840
+ /**
841
+ * It stands for the session's events from this `seq` to its own, which
842
+ * the hub folded into it when their turn ended: a run of the agent's
843
+ * message or thought chunks, joined, or of one tool call's updates,
844
+ * merged. A tab that has some of them puts this in their place.
845
+ */
846
+ folds?: number;
847
+ }
848
+ /**
849
+ * What a tab asks of the hub besides commands: a session's events before
850
+ * `before` (the first it has; null for the latest), the newest page of them.
851
+ */
852
+ export interface HistoryRequest {
853
+ type: "history";
854
+ session: string;
855
+ before: number | null;
533
856
  }
857
+ export type TabToHub = TabCommand | HistoryRequest;
534
858
  export type HubToTab = TabEvent | Ack
535
859
  /**
536
860
  * First on every connection: this app's sessions as they are, whether a
537
861
  * gateway is connected, whether this person may start sessions, prompt
538
- * and answer permission requests here (`canPrompt`), and the latest `seq`
539
- * (of any app's). Events after the tab's `since` follow, up to that `seq`,
540
- * then live ones. Reconnect with `since` the larger of that `seq` and the
541
- * last event's.
862
+ * and answer what the agent asks here (`canPrompt`), the latest `seq` (of
863
+ * any app's), and what the replay that follows holds, up to that `seq`:
864
+ * - `since`: every event of the app after the tab's `since`, which joins
865
+ * on to what it has;
866
+ * - `latest`: the latest events of `session` (the one the tab shows, else
867
+ * the app's newest; null when it has none), as `history` would give
868
+ * them, when the tab has nothing (`since` 0) or would get too many.
869
+ * The tab drops the events it had; older ones, and other sessions'
870
+ * events before the snapshot, come when it asks.
871
+ * Then `replayed`, then live events.
542
872
  */
543
873
  | {
544
874
  type: "snapshot";
@@ -546,6 +876,27 @@ export type HubToTab = TabEvent | Ack
546
876
  canPrompt: boolean;
547
877
  gateway: GatewayStatus;
548
878
  sessions: SessionInfo[];
879
+ replay: "since" | "latest";
880
+ session: string | null;
881
+ }
882
+ /**
883
+ * The replay after the snapshot is whole. Dial again with `since` the
884
+ * larger of the snapshot's `seq` and the last event's; before this, with
885
+ * the last event's (or 0, after a `latest` replay).
886
+ */
887
+ | {
888
+ type: "replayed";
889
+ }
890
+ /**
891
+ * The answer to a HistoryRequest: the session's newest events before
892
+ * `before`, at most a page, oldest first; none when there are none. A
893
+ * session's first event is `created`.
894
+ */
895
+ | {
896
+ type: "history";
897
+ session: string;
898
+ before: number | null;
899
+ events: TabEvent[];
549
900
  }
550
901
  /** The gateway connected or went away. */
551
902
  | {
package/dist/protocol.js CHANGED
@@ -2,25 +2,39 @@ import {
2
2
  ACP,
3
3
  AGENT_NAMES,
4
4
  AGENT_PROTOCOL,
5
+ ASK,
5
6
  GATEWAY_CLOSE,
6
7
  GATEWAY_PATH,
8
+ GATEWAY_TOO_FAST,
9
+ MAX_FRAME,
10
+ NOTICE,
7
11
  PING,
8
12
  PONG,
9
13
  SESSION_ID,
14
+ TAB_CLOSE,
15
+ TASK_OUTPUT,
10
16
  isAcpNotification,
11
17
  isAcpRequest,
12
- isAcpResponse
13
- } from "./chunks/5YARUI7L.js";
18
+ isAcpResponse,
19
+ shrink
20
+ } from "./chunks/XGVR5ONA.js";
14
21
  export {
15
22
  ACP,
16
23
  AGENT_NAMES,
17
24
  AGENT_PROTOCOL,
25
+ ASK,
18
26
  GATEWAY_CLOSE,
19
27
  GATEWAY_PATH,
28
+ GATEWAY_TOO_FAST,
29
+ MAX_FRAME,
30
+ NOTICE,
20
31
  PING,
21
32
  PONG,
22
33
  SESSION_ID,
34
+ TAB_CLOSE,
35
+ TASK_OUTPUT,
23
36
  isAcpNotification,
24
37
  isAcpRequest,
25
- isAcpResponse
38
+ isAcpResponse,
39
+ shrink
26
40
  };