@abelxiaoxing/cadence 2.0.29 → 2.0.31

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.
@@ -51,7 +51,7 @@ export function decodeCadenceConversation(v: unknown): CadenceConversation {
51
51
  ...(degraded ? ["notice"] : []),
52
52
  ]);
53
53
  return {
54
- id: decodeId(o.id),
54
+ id: decodeSessionId(o.id),
55
55
  revision: integer(o.revision, 0),
56
56
  enabled: bool(o.enabled),
57
57
  head: nullable(o.head, decodeId),
@@ -60,7 +60,20 @@ export function decodeCadenceConversation(v: unknown): CadenceConversation {
60
60
  ...(degraded ? { notice: text(o.notice, 1024, true) } : {}),
61
61
  };
62
62
  }
63
- /** Browser conversation that owns an active workflow; native runs have none. */
63
+ /** A conversation's identity: the Pi session id shared with the terminal. */
64
+ export function decodeSessionId(v: unknown): string {
65
+ const s = text(v, 128, true);
66
+ if (!/^[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$/.test(s))
67
+ throw new ContractError("invalid");
68
+ return s;
69
+ }
70
+ /** One entry of a Pi session (eight hex characters when Pi writes it). */
71
+ export function decodeEntryId(v: unknown): string {
72
+ const s = text(v, 64, true);
73
+ if (!/^[A-Za-z0-9_-]+$/.test(s)) throw new ContractError("invalid");
74
+ return s;
75
+ }
76
+ /** The conversation (Pi session) that started an active workflow, browser or terminal. */
64
77
  export interface WorkflowLink {
65
78
  runId: string;
66
79
  conversationId: string;
@@ -72,16 +85,35 @@ export type WorkbenchRead =
72
85
  | { kind: "workflow-detail"; id: string; stepId: string }
73
86
  | { kind: "workflow-transcript"; id: string; stepId: string }
74
87
  | { kind: "workflow"; id: string }
88
+ | { kind: "conversation-active" }
89
+ | { kind: "conversations"; after?: ChatCursor | null }
90
+ | { kind: "task-origin"; taskId: string }
91
+ | {
92
+ kind: "conversation";
93
+ id: string;
94
+ /** The branch to read, by its last entry; null reads the session's current leaf. */
95
+ leaf: string | null;
96
+ /** The leaf read last: the reply says whether the branch still continues it. */
97
+ known: string | null;
98
+ }
75
99
  | {
76
- kind: "conversation-transcript";
100
+ kind: "conversation-turn";
77
101
  id: string;
102
+ leaf: string | null;
103
+ ordinal: number;
104
+ /** Exclusive end of the steps to list; null lists the latest ones. */
105
+ before: number | null;
106
+ }
107
+ | {
108
+ kind: "conversation-step";
109
+ id: string;
110
+ leaf: string | null;
78
111
  turnId: string;
79
- page: number;
112
+ step: number;
113
+ part: number;
80
114
  }
81
- | { kind: "conversation-active" }
82
- | { kind: "conversations"; after?: ChatCursor | null; scan?: string }
83
- | { kind: "task-origin"; taskId: string }
84
- | { kind: "conversation"; id: string; before: string | null }
115
+ | { kind: "conversation-tree"; id: string; leaf: string | null }
116
+ | { kind: "conversation-legacy" }
85
117
  | { kind: "files"; path: string }
86
118
  | { kind: "file"; path: string }
87
119
  | { kind: "directories"; path: string }
@@ -144,7 +176,8 @@ export type WorkbenchCommand =
144
176
  turnId: string;
145
177
  epoch: string;
146
178
  }
147
- | { kind: "open-project"; commandId: string; path: string; epoch: string };
179
+ | { kind: "open-project"; commandId: string; path: string; epoch: string }
180
+ | { kind: "conversation-migrate"; commandId: string };
148
181
  export type ChatStatus =
149
182
  | "queued"
150
183
  | "running"
@@ -173,21 +206,141 @@ export interface ChatTurn {
173
206
  * `other` is a block of a type Cadence does not recognize. */
174
207
  omitted: { thinking: boolean; media: boolean; other: boolean };
175
208
  }
209
+ /** One recorded item of a turn as listed. A listing never carries a tool's
210
+ * arguments or output and keeps at most `STEP_TEXT_BYTES` of message text;
211
+ * the full item is read per step with `conversation-step`, one part at a time. */
212
+ export interface ChatStep {
213
+ id: string;
214
+ role: ChatItem["role"];
215
+ /** User or assistant text, a prefix of it when `clipped`; empty for tools. */
216
+ text: string;
217
+ clipped: boolean;
218
+ name: string;
219
+ /** What a tool acted on: a command's first line, else its pattern and path. */
220
+ target: string;
221
+ error: boolean;
222
+ /** Stored parts that hold the full item; 0 when nothing more is recorded. */
223
+ parts: number;
224
+ }
225
+ /** One turn of a conversation branch: a user message and what followed it. */
226
+ export interface ChatTurnHeader {
227
+ /** The user message's session entry id; a browser command id while Pi has not
228
+ * written that message yet. */
229
+ id: string;
230
+ status: ChatStatus;
231
+ notice: string;
232
+ omitted: ChatTurn["omitted"];
233
+ steps: number;
234
+ /** Leading steps that can no longer change: all of them once settled. */
235
+ stable: number;
236
+ /** The turn's last session entry: a new branch can continue from it. */
237
+ entry: string | null;
238
+ /** When the turn began (epoch ms), 0 when unrecorded. */
239
+ at: number;
240
+ }
241
+ /** A browser message that left nothing in the session, shown after the branch
242
+ * it was sent from: a refusal before any model call or a Cadence control reply. */
243
+ export interface ChatAttempt {
244
+ commandId: string;
245
+ text: string;
246
+ status: ChatStatus;
247
+ notice: string;
248
+ }
249
+ /** A user message in a session's tree, linked to the nearest earlier one. */
250
+ export interface ChatTreeNode {
251
+ id: string;
252
+ parent: string | null;
253
+ text: string;
254
+ at: number;
255
+ /** The newest entry below this message: reading it shows this branch. */
256
+ leaf: string;
257
+ /** On the branch the read named. */
258
+ current: boolean;
259
+ }
260
+ /** Nodes one `conversation-tree` read returns, newest kept when more exist. */
261
+ export const TREE_NODES = 1000;
262
+ /** Encoded bytes of message text a listed step keeps. */
263
+ export const STEP_TEXT_BYTES = 16 * 1024;
264
+ const TARGET_BYTES = 1024;
265
+ /** Steps one `conversation-turn` read lists: as many as always fit its reply
266
+ * (512 KiB) at the encoded worst case of every field. */
267
+ export const STEP_READ = 24;
268
+ /** A string's prefix whose JSON encoding fits `bytes`, ending on a whole code point. */
269
+ export function clipText(value: string, bytes: number): string {
270
+ const size = (s: string) =>
271
+ new TextEncoder().encode(JSON.stringify(s)).length;
272
+ if (size(value) <= bytes) return value;
273
+ let low = 0,
274
+ high = Math.min(value.length, bytes);
275
+ while (low < high) {
276
+ const mid = Math.ceil((low + high) / 2);
277
+ if (size(value.slice(0, mid)) <= bytes) low = mid;
278
+ else high = mid - 1;
279
+ }
280
+ return /[\uD800-\uDBFF]/u.test(value.slice(low - 1, low))
281
+ ? value.slice(0, low - 1)
282
+ : value.slice(0, low);
283
+ }
284
+ /** What a tool acted on, from its recorded arguments: a command's first line,
285
+ * else a pattern and path. Partial or unparsable arguments name nothing. */
286
+ export function toolTarget(args: string): string {
287
+ let value: unknown;
288
+ try {
289
+ value = JSON.parse(args);
290
+ } catch {
291
+ return "";
292
+ }
293
+ if (!value || typeof value !== "object" || Array.isArray(value)) return "";
294
+ const field = (name: string) => {
295
+ const v = Object.hasOwn(value, name)
296
+ ? (value as Record<string, unknown>)[name]
297
+ : undefined;
298
+ return typeof v === "string" ? v.trim() : "";
299
+ };
300
+ const command = field("command");
301
+ if (command) return command.split("\n", 1)[0];
302
+ return [field("pattern"), field("path")].filter(Boolean).join(" ");
303
+ }
304
+ /** The listed form of an item; `more` says its text continues beyond `item.text`. */
305
+ export function chatStep(
306
+ item: ChatItem,
307
+ parts: number,
308
+ more = false,
309
+ ): ChatStep {
310
+ const tool = item.role === "tool";
311
+ const text = tool ? "" : clipText(item.text, STEP_TEXT_BYTES);
312
+ return {
313
+ id: item.id,
314
+ role: item.role,
315
+ text,
316
+ clipped: !tool && (more || text !== item.text),
317
+ name: item.name,
318
+ target: tool ? clipText(toolTarget(item.args), TARGET_BYTES) : "",
319
+ error: item.error,
320
+ parts,
321
+ };
322
+ }
176
323
  export interface ChatCursor {
177
324
  id: string;
178
325
  updatedAt: number;
179
326
  }
180
327
  function decodeChatCursor(value: unknown): ChatCursor {
181
328
  const o = closed(value, ["id", "updatedAt"]);
182
- return { id: decodeId(o.id), updatedAt: integer(o.updatedAt, 0) };
329
+ return { id: decodeSessionId(o.id), updatedAt: integer(o.updatedAt, 0) };
183
330
  }
184
331
  export interface ChatSummary {
332
+ /** The Pi session id. */
185
333
  id: string;
186
334
  title: string;
187
- head: string;
335
+ /** The session's current leaf entry; null before Pi has written any entry. */
336
+ leaf: string | null;
188
337
  status: ChatStatus;
189
338
  updatedAt: number;
339
+ /** Who created the session: the Pi terminal or the browser. */
340
+ origin: "terminal" | "webui";
190
341
  cadence?: boolean;
342
+ /** The browser reply in progress, which Stop interrupts. */
343
+ live?: { commandId: string; epoch: string };
191
344
  }
192
345
  export interface FileEntry {
193
346
  name: string;
@@ -195,12 +348,6 @@ export interface FileEntry {
195
348
  directory: boolean;
196
349
  }
197
350
  export type WorkbenchResult =
198
- | {
199
- kind: "conversation-transcript";
200
- conversationId: string;
201
- page: number;
202
- turn: ChatTurn;
203
- }
204
351
  | { kind: "workflow-history-progress"; after: string | null; scan: string }
205
352
  | {
206
353
  kind: "workflow-history";
@@ -231,13 +378,52 @@ export type WorkbenchResult =
231
378
  | { kind: "workflow"; commandId: string | null; response: WorkflowResponse }
232
379
  | { kind: "task-origin"; taskId: string; conversationId: string | null }
233
380
  | { kind: "conversation-active"; entries: ChatSummary[] }
234
- | { kind: "conversations-progress"; after: ChatCursor | null; scan: string }
235
- | { kind: "conversations"; entries: ChatSummary[]; next?: ChatCursor | null }
381
+ | { kind: "conversations"; entries: ChatSummary[]; next: ChatCursor | null }
236
382
  | {
237
383
  kind: "conversation";
238
384
  summary: ChatSummary | null;
239
- turns: ChatTurn[];
240
- before: string | null;
385
+ /** The branch read, by its last entry (null: nothing written yet). */
386
+ leaf: string | null;
387
+ /** False when the branch read no longer contains the requested `known` leaf. */
388
+ continues: boolean;
389
+ turns: number;
390
+ /** Leaves in the whole session tree: more than one means branches. */
391
+ branches: number;
392
+ /** Workflows the terminal started in this session. */
393
+ runs: string[];
394
+ attempt: ChatAttempt | null;
395
+ }
396
+ | {
397
+ kind: "conversation-tree";
398
+ conversationId: string;
399
+ nodes: ChatTreeNode[];
400
+ truncated: boolean;
401
+ }
402
+ | { kind: "conversation-legacy"; pending: number }
403
+ | {
404
+ kind: "conversation-migrated";
405
+ commandId: string;
406
+ migrated: number;
407
+ skipped: number;
408
+ }
409
+ | {
410
+ kind: "conversation-turn";
411
+ conversationId: string;
412
+ leaf: string | null;
413
+ ordinal: number;
414
+ turn: ChatTurnHeader;
415
+ /** Index of the first listed step. */
416
+ start: number;
417
+ steps: ChatStep[];
418
+ }
419
+ | {
420
+ kind: "conversation-step";
421
+ conversationId: string;
422
+ leaf: string | null;
423
+ turnId: string;
424
+ step: number;
425
+ part: number;
426
+ item: ChatItem;
241
427
  }
242
428
  | {
243
429
  kind: "files" | "directories";
@@ -273,18 +459,38 @@ const statuses = [
273
459
  export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
274
460
  const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
275
461
  switch (kind) {
276
- case "conversation-transcript": {
277
- const o = closed(v, ["kind", "id", "turnId", "page"]);
462
+ case "conversation-turn": {
463
+ const o = closed(v, ["kind", "id", "leaf", "ordinal", "before"]);
278
464
  return {
279
465
  kind,
280
- id: decodeId(o.id),
281
- turnId: decodeId(o.turnId),
282
- page: integer(o.page, 0),
466
+ id: decodeSessionId(o.id),
467
+ leaf: nullable(o.leaf, decodeEntryId),
468
+ ordinal: integer(o.ordinal, 0),
469
+ before: nullable(o.before, (b) => integer(b, 0)),
470
+ };
471
+ }
472
+ case "conversation-step": {
473
+ const o = closed(v, ["kind", "id", "leaf", "turnId", "step", "part"]);
474
+ return {
475
+ kind,
476
+ id: decodeSessionId(o.id),
477
+ leaf: nullable(o.leaf, decodeEntryId),
478
+ turnId: decodeEntryId(o.turnId),
479
+ step: integer(o.step, 0),
480
+ part: integer(o.part, 0),
283
481
  };
284
482
  }
285
483
  case "cadence-conversation": {
286
484
  const o = closed(v, ["kind", "id"]);
287
- return { kind, id: decodeId(o.id) };
485
+ return { kind, id: decodeSessionId(o.id) };
486
+ }
487
+ case "conversation-tree": {
488
+ const o = closed(v, ["kind", "id", "leaf"]);
489
+ return {
490
+ kind,
491
+ id: decodeSessionId(o.id),
492
+ leaf: nullable(o.leaf, decodeEntryId),
493
+ };
288
494
  }
289
495
  case "workflow-history": {
290
496
  const continued = Object.hasOwn(v as object, "scan");
@@ -308,20 +514,15 @@ export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
308
514
  return { kind, id: decodeId(o.id) };
309
515
  }
310
516
  case "conversations": {
311
- const after = Object.hasOwn(v as object, "after"),
312
- scan = Object.hasOwn(v as object, "scan");
313
- const o = closed(v, [
314
- "kind",
315
- ...(after ? ["after"] : []),
316
- ...(scan ? ["scan"] : []),
317
- ]);
517
+ const after = Object.hasOwn(v as object, "after");
518
+ const o = closed(v, ["kind", ...(after ? ["after"] : [])]);
318
519
  return {
319
520
  kind,
320
521
  ...(after ? { after: nullable(o.after, decodeChatCursor) } : {}),
321
- ...(scan ? { scan: decodeId(o.scan) } : {}),
322
522
  };
323
523
  }
324
524
  case "conversation-active":
525
+ case "conversation-legacy":
325
526
  case "projects":
326
527
  closed(v, ["kind"]);
327
528
  return { kind };
@@ -330,8 +531,13 @@ export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
330
531
  return { kind, taskId: decodeId(o.taskId) };
331
532
  }
332
533
  case "conversation": {
333
- const o = closed(v, ["kind", "id", "before"]);
334
- return { kind, id: decodeId(o.id), before: nullable(o.before, decodeId) };
534
+ const o = closed(v, ["kind", "id", "leaf", "known"]);
535
+ return {
536
+ kind,
537
+ id: decodeSessionId(o.id),
538
+ leaf: nullable(o.leaf, decodeEntryId),
539
+ known: nullable(o.known, decodeEntryId),
540
+ };
335
541
  }
336
542
  default: {
337
543
  const o = closed(v, ["kind", "path"]);
@@ -367,8 +573,8 @@ export function decodeWorkbenchCommand(v: unknown): WorkbenchCommand {
367
573
  return {
368
574
  kind,
369
575
  commandId: decodeId(o.commandId),
370
- conversationId: decodeId(o.conversationId),
371
- head: nullable(o.head, decodeId),
576
+ conversationId: decodeSessionId(o.conversationId),
577
+ head: nullable(o.head, decodeEntryId),
372
578
  revision: integer(o.revision, 0),
373
579
  };
374
580
  }
@@ -386,8 +592,8 @@ export function decodeWorkbenchCommand(v: unknown): WorkbenchCommand {
386
592
  return {
387
593
  kind,
388
594
  commandId: decodeId(o.commandId),
389
- conversationId: decodeId(o.conversationId),
390
- head: nullable(o.head, decodeId),
595
+ conversationId: decodeSessionId(o.conversationId),
596
+ head: nullable(o.head, decodeEntryId),
391
597
  text: text(o.text, 16384, true),
392
598
  model: decodeChoiceVersion(o.model),
393
599
  revision: integer(o.revision, 0),
@@ -406,7 +612,7 @@ export function decodeWorkbenchCommand(v: unknown): WorkbenchCommand {
406
612
  const o = closed(v, kind === "cadence-approve" ? [...keys, "grant"] : keys);
407
613
  const base = {
408
614
  commandId: decodeId(o.commandId),
409
- conversationId: decodeId(o.conversationId),
615
+ conversationId: decodeSessionId(o.conversationId),
410
616
  revision: integer(o.revision, 0),
411
617
  proposalId: decodeId(o.proposalId),
412
618
  digest: decodeHash(o.digest),
@@ -437,8 +643,8 @@ export function decodeWorkbenchCommand(v: unknown): WorkbenchCommand {
437
643
  return {
438
644
  kind,
439
645
  commandId: decodeId(o.commandId),
440
- conversationId: decodeId(o.conversationId),
441
- head: nullable(o.head, decodeId),
646
+ conversationId: decodeSessionId(o.conversationId),
647
+ head: nullable(o.head, decodeEntryId),
442
648
  text: text(o.text, 16 * 1024, true),
443
649
  model: decodeChoiceVersion(o.model),
444
650
  };
@@ -454,11 +660,15 @@ export function decodeWorkbenchCommand(v: unknown): WorkbenchCommand {
454
660
  return {
455
661
  kind,
456
662
  commandId: decodeId(o.commandId),
457
- conversationId: decodeId(o.conversationId),
663
+ conversationId: decodeSessionId(o.conversationId),
458
664
  turnId: decodeId(o.turnId),
459
665
  epoch: decodeId(o.epoch),
460
666
  };
461
667
  }
668
+ if (kind === "conversation-migrate") {
669
+ const o = closed(v, ["kind", "commandId"]);
670
+ return { kind, commandId: decodeId(o.commandId) };
671
+ }
462
672
  const o = closed(v, ["kind", "commandId", "path", "epoch"]);
463
673
  return {
464
674
  kind: oneOf(o.kind, ["open-project"]),
@@ -496,50 +706,128 @@ export function decodeChatTurn(v: unknown): ChatTurn {
496
706
  media: omitted ? bool(omitted.media) : false,
497
707
  other: omitted ? bool(omitted.other) : false,
498
708
  },
499
- items: list(o.items, 256, (v) => {
500
- const i = closed(v, ["id", "role", "text", "name", "args", "error"]);
501
- return {
502
- id: text(i.id, 160, true),
503
- role: oneOf(i.role, ["user", "assistant", "tool"]),
504
- text: text(i.text, 256 * 1024),
505
- name: text(i.name, 128),
506
- args: text(i.args, 64 * 1024),
507
- error: bool(i.error),
508
- };
509
- }),
709
+ items: list(o.items, 256, decodeChatItem),
710
+ };
711
+ }
712
+ export function decodeChatItem(v: unknown): ChatItem {
713
+ const i = closed(v, ["id", "role", "text", "name", "args", "error"]);
714
+ return {
715
+ id: text(i.id, 160, true),
716
+ role: oneOf(i.role, ["user", "assistant", "tool"]),
717
+ text: text(i.text, 256 * 1024),
718
+ name: text(i.name, 128),
719
+ args: text(i.args, 64 * 1024),
720
+ error: bool(i.error),
721
+ };
722
+ }
723
+ function decodeChatStep(v: unknown): ChatStep {
724
+ const o = closed(v, [
725
+ "id",
726
+ "role",
727
+ "text",
728
+ "clipped",
729
+ "name",
730
+ "target",
731
+ "error",
732
+ "parts",
733
+ ]);
734
+ const step = {
735
+ id: text(o.id, 160, true),
736
+ role: oneOf(o.role, ["user", "assistant", "tool"]),
737
+ text: text(o.text, STEP_TEXT_BYTES),
738
+ clipped: bool(o.clipped),
739
+ name: text(o.name, 128),
740
+ target: text(o.target, TARGET_BYTES),
741
+ error: bool(o.error),
742
+ parts: integer(o.parts, 0),
743
+ };
744
+ if (step.role === "tool" && (step.text || step.clipped))
745
+ throw new ContractError("invalid");
746
+ return step;
747
+ }
748
+ function decodeChatTurnHeader(v: unknown): ChatTurnHeader {
749
+ const o = closed(v, [
750
+ "id",
751
+ "status",
752
+ "notice",
753
+ "omitted",
754
+ "steps",
755
+ "stable",
756
+ "entry",
757
+ "at",
758
+ ]);
759
+ const omitted = closed(o.omitted, ["thinking", "media", "other"]);
760
+ const steps = integer(o.steps, 0);
761
+ return {
762
+ id: decodeEntryId(o.id),
763
+ entry: nullable(o.entry, decodeEntryId),
764
+ at: integer(o.at, 0),
765
+ status: oneOf(o.status, statuses),
766
+ notice: text(o.notice, 1024),
767
+ omitted: {
768
+ thinking: bool(omitted.thinking),
769
+ media: bool(omitted.media),
770
+ other: bool(omitted.other),
771
+ },
772
+ steps,
773
+ stable: integer(o.stable, 0, steps),
510
774
  };
511
775
  }
512
776
  export function decodeChatSummary(v: unknown): ChatSummary {
513
- const modern =
514
- v !== null && typeof v === "object" && Object.hasOwn(v, "cadence");
777
+ const has = (key: string) =>
778
+ v !== null && typeof v === "object" && Object.hasOwn(v, key);
515
779
  const o = closed(v, [
516
780
  "id",
517
781
  "title",
518
- "head",
782
+ "leaf",
519
783
  "status",
520
784
  "updatedAt",
521
- ...(modern ? ["cadence"] : []),
785
+ "origin",
786
+ ...(has("cadence") ? ["cadence"] : []),
787
+ ...(has("live") ? ["live"] : []),
522
788
  ]);
789
+ const live = has("live") ? closed(o.live, ["commandId", "epoch"]) : null;
523
790
  return {
524
- id: decodeId(o.id),
791
+ id: decodeSessionId(o.id),
525
792
  title: text(o.title, 512, true),
526
- head: decodeId(o.head),
793
+ leaf: nullable(o.leaf, decodeEntryId),
527
794
  status: oneOf(o.status, statuses),
528
795
  updatedAt: integer(o.updatedAt, 0),
529
- ...(modern ? { cadence: bool(o.cadence) } : {}),
796
+ origin: oneOf(o.origin, ["terminal", "webui"]),
797
+ ...(has("cadence") ? { cadence: bool(o.cadence) } : {}),
798
+ ...(live
799
+ ? {
800
+ live: {
801
+ commandId: decodeId(live.commandId),
802
+ epoch: decodeId(live.epoch),
803
+ },
804
+ }
805
+ : {}),
806
+ };
807
+ }
808
+ function decodeChatAttempt(v: unknown): ChatAttempt {
809
+ const o = closed(v, ["commandId", "text", "status", "notice"]);
810
+ return {
811
+ commandId: decodeId(o.commandId),
812
+ text: text(o.text, 16 * 1024),
813
+ status: oneOf(o.status, statuses),
814
+ notice: text(o.notice, 1024),
815
+ };
816
+ }
817
+ function decodeChatTreeNode(v: unknown): ChatTreeNode {
818
+ const o = closed(v, ["id", "parent", "text", "at", "leaf", "current"]);
819
+ return {
820
+ id: decodeEntryId(o.id),
821
+ parent: nullable(o.parent, decodeEntryId),
822
+ text: text(o.text, 1024),
823
+ at: integer(o.at, 0),
824
+ leaf: decodeEntryId(o.leaf),
825
+ current: bool(o.current),
530
826
  };
531
827
  }
532
828
  export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
533
829
  canonicalJson(v, 512 * 1024);
534
830
  const k = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
535
- if (k === "conversation-transcript") {
536
- const o = closed(v, ["kind", "conversationId", "page", "turn"]);
537
- const turn = decodeChatTurn(o.turn),
538
- page = integer(o.page, 0);
539
- if (!turn.transcript || page >= turn.transcript.pages)
540
- throw new ContractError("invalid");
541
- return { kind: k, conversationId: decodeId(o.conversationId), page, turn };
542
- }
543
831
  if (k === "workflow-transcript") {
544
832
  const o = closed(v, ["kind", "runId", "stepId", "turn"]);
545
833
  return {
@@ -588,7 +876,7 @@ export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
588
876
  const l = closed(value, ["runId", "conversationId"]);
589
877
  return {
590
878
  runId: decodeId(l.runId),
591
- conversationId: decodeId(l.conversationId),
879
+ conversationId: decodeSessionId(l.conversationId),
592
880
  };
593
881
  })
594
882
  : [];
@@ -632,15 +920,7 @@ export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
632
920
  return {
633
921
  kind,
634
922
  taskId: decodeId(o.taskId),
635
- conversationId: nullable(o.conversationId, decodeId),
636
- };
637
- }
638
- if (kind === "conversations-progress") {
639
- const o = closed(v, ["kind", "after", "scan"]);
640
- return {
641
- kind,
642
- after: nullable(o.after, decodeChatCursor),
643
- scan: decodeId(o.scan),
923
+ conversationId: nullable(o.conversationId, decodeSessionId),
644
924
  };
645
925
  }
646
926
  if (kind === "conversation-active") {
@@ -648,21 +928,102 @@ export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
648
928
  return { kind, entries: list(o.entries, 8, decodeChatSummary) };
649
929
  }
650
930
  if (kind === "conversations") {
651
- const paged = Object.hasOwn(v as object, "next");
652
- const o = closed(v, ["kind", "entries", ...(paged ? ["next"] : [])]);
931
+ const o = closed(v, ["kind", "entries", "next"]);
653
932
  return {
654
933
  kind,
655
934
  entries: list(o.entries, 200, decodeChatSummary),
656
- ...(paged ? { next: nullable(o.next, decodeChatCursor) } : {}),
935
+ next: nullable(o.next, decodeChatCursor),
657
936
  };
658
937
  }
659
938
  if (kind === "conversation") {
660
- const o = closed(v, ["kind", "summary", "turns", "before"]);
939
+ const o = closed(v, [
940
+ "kind",
941
+ "summary",
942
+ "leaf",
943
+ "continues",
944
+ "turns",
945
+ "branches",
946
+ "runs",
947
+ "attempt",
948
+ ]);
949
+ const summary = nullable(o.summary, decodeChatSummary),
950
+ turns = integer(o.turns, 0);
951
+ if (!summary && turns) throw new ContractError("invalid");
952
+ return {
953
+ kind,
954
+ summary,
955
+ leaf: nullable(o.leaf, decodeEntryId),
956
+ continues: bool(o.continues),
957
+ turns,
958
+ branches: integer(o.branches, 0),
959
+ runs: list(o.runs, 200, decodeId),
960
+ attempt: nullable(o.attempt, decodeChatAttempt),
961
+ };
962
+ }
963
+ if (kind === "conversation-tree") {
964
+ const o = closed(v, ["kind", "conversationId", "nodes", "truncated"]);
965
+ return {
966
+ kind,
967
+ conversationId: decodeSessionId(o.conversationId),
968
+ nodes: list(o.nodes, TREE_NODES, decodeChatTreeNode),
969
+ truncated: bool(o.truncated),
970
+ };
971
+ }
972
+ if (kind === "conversation-legacy") {
973
+ const o = closed(v, ["kind", "pending"]);
974
+ return { kind, pending: integer(o.pending, 0) };
975
+ }
976
+ if (kind === "conversation-migrated") {
977
+ const o = closed(v, ["kind", "commandId", "migrated", "skipped"]);
978
+ return {
979
+ kind,
980
+ commandId: decodeId(o.commandId),
981
+ migrated: integer(o.migrated, 0),
982
+ skipped: integer(o.skipped, 0),
983
+ };
984
+ }
985
+ if (kind === "conversation-turn") {
986
+ const o = closed(v, [
987
+ "kind",
988
+ "conversationId",
989
+ "leaf",
990
+ "ordinal",
991
+ "turn",
992
+ "start",
993
+ "steps",
994
+ ]);
995
+ const turn = decodeChatTurnHeader(o.turn),
996
+ start = integer(o.start, 0),
997
+ steps = list(o.steps, STEP_READ, decodeChatStep);
998
+ if (start + steps.length > turn.steps) throw new ContractError("invalid");
999
+ return {
1000
+ kind,
1001
+ conversationId: decodeSessionId(o.conversationId),
1002
+ leaf: nullable(o.leaf, decodeEntryId),
1003
+ ordinal: integer(o.ordinal, 0),
1004
+ turn,
1005
+ start,
1006
+ steps,
1007
+ };
1008
+ }
1009
+ if (kind === "conversation-step") {
1010
+ const o = closed(v, [
1011
+ "kind",
1012
+ "conversationId",
1013
+ "leaf",
1014
+ "turnId",
1015
+ "step",
1016
+ "part",
1017
+ "item",
1018
+ ]);
661
1019
  return {
662
1020
  kind,
663
- summary: nullable(o.summary, decodeChatSummary),
664
- turns: list(o.turns, 20, decodeChatTurn),
665
- before: nullable(o.before, decodeId),
1021
+ conversationId: decodeSessionId(o.conversationId),
1022
+ leaf: nullable(o.leaf, decodeEntryId),
1023
+ turnId: decodeEntryId(o.turnId),
1024
+ step: integer(o.step, 0),
1025
+ part: integer(o.part, 0),
1026
+ item: decodeChatItem(o.item),
666
1027
  };
667
1028
  }
668
1029
  if (kind === "files" || kind === "directories") {