@abelxiaoxing/cadence 2.0.29 → 2.0.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abelxiaoxing/cadence",
3
- "version": "2.0.29",
3
+ "version": "2.0.30",
4
4
  "description": "Conversation-driven Pi workspace: persistent native and WebUI workflows, explicit plan approval, checks and local delivery.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -2,5 +2,5 @@
2
2
  export const nativeBuild = {
3
3
  kind: "native",
4
4
  contractHash:
5
- "70b7d387e5677a43ff0a80f82aad39684362496916c796150a4e326cd7756b65",
5
+ "d0edc9d93f682dd7c637bd28cdb756b823ad8eafeabebc63479bee1bac5b2b55",
6
6
  } as const;
@@ -72,16 +72,24 @@ export type WorkbenchRead =
72
72
  | { kind: "workflow-detail"; id: string; stepId: string }
73
73
  | { kind: "workflow-transcript"; id: string; stepId: string }
74
74
  | { kind: "workflow"; id: string }
75
+ | { kind: "conversation-active" }
76
+ | { kind: "conversations"; after?: ChatCursor | null; scan?: string }
77
+ | { kind: "task-origin"; taskId: string }
78
+ | { kind: "conversation"; id: string }
79
+ | {
80
+ kind: "conversation-turn";
81
+ id: string;
82
+ ordinal: number;
83
+ /** Exclusive end of the steps to list; null lists the latest ones. */
84
+ before: number | null;
85
+ }
75
86
  | {
76
- kind: "conversation-transcript";
87
+ kind: "conversation-step";
77
88
  id: string;
78
89
  turnId: string;
79
- page: number;
90
+ step: number;
91
+ part: number;
80
92
  }
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 }
85
93
  | { kind: "files"; path: string }
86
94
  | { kind: "file"; path: string }
87
95
  | { kind: "directories"; path: string }
@@ -173,6 +181,93 @@ export interface ChatTurn {
173
181
  * `other` is a block of a type Cadence does not recognize. */
174
182
  omitted: { thinking: boolean; media: boolean; other: boolean };
175
183
  }
184
+ /** One recorded item of a turn as listed. A listing never carries a tool's
185
+ * arguments or output and keeps at most `STEP_TEXT_BYTES` of message text;
186
+ * the full item is read per step with `conversation-step`, one part at a time. */
187
+ export interface ChatStep {
188
+ id: string;
189
+ role: ChatItem["role"];
190
+ /** User or assistant text, a prefix of it when `clipped`; empty for tools. */
191
+ text: string;
192
+ clipped: boolean;
193
+ name: string;
194
+ /** What a tool acted on: a command's first line, else its pattern and path. */
195
+ target: string;
196
+ error: boolean;
197
+ /** Stored parts that hold the full item; 0 when nothing more is recorded. */
198
+ parts: number;
199
+ }
200
+ export interface ChatTurnHeader {
201
+ id: string;
202
+ epoch: string;
203
+ status: ChatStatus;
204
+ notice: string;
205
+ omitted: ChatTurn["omitted"];
206
+ steps: number;
207
+ /** Leading steps that can no longer change: all of them once settled. */
208
+ stable: number;
209
+ }
210
+ /** Encoded bytes of message text a listed step keeps. */
211
+ export const STEP_TEXT_BYTES = 16 * 1024;
212
+ const TARGET_BYTES = 1024;
213
+ /** Steps one `conversation-turn` read lists: as many as always fit its reply
214
+ * (512 KiB) at the encoded worst case of every field. */
215
+ export const STEP_READ = 24;
216
+ /** A string's prefix whose JSON encoding fits `bytes`, ending on a whole code point. */
217
+ export function clipText(value: string, bytes: number): string {
218
+ const size = (s: string) =>
219
+ new TextEncoder().encode(JSON.stringify(s)).length;
220
+ if (size(value) <= bytes) return value;
221
+ let low = 0,
222
+ high = Math.min(value.length, bytes);
223
+ while (low < high) {
224
+ const mid = Math.ceil((low + high) / 2);
225
+ if (size(value.slice(0, mid)) <= bytes) low = mid;
226
+ else high = mid - 1;
227
+ }
228
+ return /[\uD800-\uDBFF]/u.test(value.slice(low - 1, low))
229
+ ? value.slice(0, low - 1)
230
+ : value.slice(0, low);
231
+ }
232
+ /** What a tool acted on, from its recorded arguments: a command's first line,
233
+ * else a pattern and path. Partial or unparsable arguments name nothing. */
234
+ export function toolTarget(args: string): string {
235
+ let value: unknown;
236
+ try {
237
+ value = JSON.parse(args);
238
+ } catch {
239
+ return "";
240
+ }
241
+ if (!value || typeof value !== "object" || Array.isArray(value)) return "";
242
+ const field = (name: string) => {
243
+ const v = Object.hasOwn(value, name)
244
+ ? (value as Record<string, unknown>)[name]
245
+ : undefined;
246
+ return typeof v === "string" ? v.trim() : "";
247
+ };
248
+ const command = field("command");
249
+ if (command) return command.split("\n", 1)[0];
250
+ return [field("pattern"), field("path")].filter(Boolean).join(" ");
251
+ }
252
+ /** The listed form of an item; `more` says its text continues beyond `item.text`. */
253
+ export function chatStep(
254
+ item: ChatItem,
255
+ parts: number,
256
+ more = false,
257
+ ): ChatStep {
258
+ const tool = item.role === "tool";
259
+ const text = tool ? "" : clipText(item.text, STEP_TEXT_BYTES);
260
+ return {
261
+ id: item.id,
262
+ role: item.role,
263
+ text,
264
+ clipped: !tool && (more || text !== item.text),
265
+ name: item.name,
266
+ target: tool ? clipText(toolTarget(item.args), TARGET_BYTES) : "",
267
+ error: item.error,
268
+ parts,
269
+ };
270
+ }
176
271
  export interface ChatCursor {
177
272
  id: string;
178
273
  updatedAt: number;
@@ -195,12 +290,6 @@ export interface FileEntry {
195
290
  directory: boolean;
196
291
  }
197
292
  export type WorkbenchResult =
198
- | {
199
- kind: "conversation-transcript";
200
- conversationId: string;
201
- page: number;
202
- turn: ChatTurn;
203
- }
204
293
  | { kind: "workflow-history-progress"; after: string | null; scan: string }
205
294
  | {
206
295
  kind: "workflow-history";
@@ -233,11 +322,23 @@ export type WorkbenchResult =
233
322
  | { kind: "conversation-active"; entries: ChatSummary[] }
234
323
  | { kind: "conversations-progress"; after: ChatCursor | null; scan: string }
235
324
  | { kind: "conversations"; entries: ChatSummary[]; next?: ChatCursor | null }
325
+ | { kind: "conversation"; summary: ChatSummary | null; turns: number }
236
326
  | {
237
- kind: "conversation";
238
- summary: ChatSummary | null;
239
- turns: ChatTurn[];
240
- before: string | null;
327
+ kind: "conversation-turn";
328
+ conversationId: string;
329
+ ordinal: number;
330
+ turn: ChatTurnHeader;
331
+ /** Index of the first listed step. */
332
+ start: number;
333
+ steps: ChatStep[];
334
+ }
335
+ | {
336
+ kind: "conversation-step";
337
+ conversationId: string;
338
+ turnId: string;
339
+ step: number;
340
+ part: number;
341
+ item: ChatItem;
241
342
  }
242
343
  | {
243
344
  kind: "files" | "directories";
@@ -273,13 +374,23 @@ const statuses = [
273
374
  export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
274
375
  const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
275
376
  switch (kind) {
276
- case "conversation-transcript": {
277
- const o = closed(v, ["kind", "id", "turnId", "page"]);
377
+ case "conversation-turn": {
378
+ const o = closed(v, ["kind", "id", "ordinal", "before"]);
379
+ return {
380
+ kind,
381
+ id: decodeId(o.id),
382
+ ordinal: integer(o.ordinal, 0),
383
+ before: nullable(o.before, (b) => integer(b, 0)),
384
+ };
385
+ }
386
+ case "conversation-step": {
387
+ const o = closed(v, ["kind", "id", "turnId", "step", "part"]);
278
388
  return {
279
389
  kind,
280
390
  id: decodeId(o.id),
281
391
  turnId: decodeId(o.turnId),
282
- page: integer(o.page, 0),
392
+ step: integer(o.step, 0),
393
+ part: integer(o.part, 0),
283
394
  };
284
395
  }
285
396
  case "cadence-conversation": {
@@ -330,8 +441,8 @@ export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
330
441
  return { kind, taskId: decodeId(o.taskId) };
331
442
  }
332
443
  case "conversation": {
333
- const o = closed(v, ["kind", "id", "before"]);
334
- return { kind, id: decodeId(o.id), before: nullable(o.before, decodeId) };
444
+ const o = closed(v, ["kind", "id"]);
445
+ return { kind, id: decodeId(o.id) };
335
446
  }
336
447
  default: {
337
448
  const o = closed(v, ["kind", "path"]);
@@ -496,17 +607,69 @@ export function decodeChatTurn(v: unknown): ChatTurn {
496
607
  media: omitted ? bool(omitted.media) : false,
497
608
  other: omitted ? bool(omitted.other) : false,
498
609
  },
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
- }),
610
+ items: list(o.items, 256, decodeChatItem),
611
+ };
612
+ }
613
+ export function decodeChatItem(v: unknown): ChatItem {
614
+ const i = closed(v, ["id", "role", "text", "name", "args", "error"]);
615
+ return {
616
+ id: text(i.id, 160, true),
617
+ role: oneOf(i.role, ["user", "assistant", "tool"]),
618
+ text: text(i.text, 256 * 1024),
619
+ name: text(i.name, 128),
620
+ args: text(i.args, 64 * 1024),
621
+ error: bool(i.error),
622
+ };
623
+ }
624
+ function decodeChatStep(v: unknown): ChatStep {
625
+ const o = closed(v, [
626
+ "id",
627
+ "role",
628
+ "text",
629
+ "clipped",
630
+ "name",
631
+ "target",
632
+ "error",
633
+ "parts",
634
+ ]);
635
+ const step = {
636
+ id: text(o.id, 160, true),
637
+ role: oneOf(o.role, ["user", "assistant", "tool"]),
638
+ text: text(o.text, STEP_TEXT_BYTES),
639
+ clipped: bool(o.clipped),
640
+ name: text(o.name, 128),
641
+ target: text(o.target, TARGET_BYTES),
642
+ error: bool(o.error),
643
+ parts: integer(o.parts, 0),
644
+ };
645
+ if (step.role === "tool" && (step.text || step.clipped))
646
+ throw new ContractError("invalid");
647
+ return step;
648
+ }
649
+ function decodeChatTurnHeader(v: unknown): ChatTurnHeader {
650
+ const o = closed(v, [
651
+ "id",
652
+ "epoch",
653
+ "status",
654
+ "notice",
655
+ "omitted",
656
+ "steps",
657
+ "stable",
658
+ ]);
659
+ const omitted = closed(o.omitted, ["thinking", "media", "other"]);
660
+ const steps = integer(o.steps, 0);
661
+ return {
662
+ id: decodeId(o.id),
663
+ epoch: decodeId(o.epoch),
664
+ status: oneOf(o.status, statuses),
665
+ notice: text(o.notice, 1024),
666
+ omitted: {
667
+ thinking: bool(omitted.thinking),
668
+ media: bool(omitted.media),
669
+ other: bool(omitted.other),
670
+ },
671
+ steps,
672
+ stable: integer(o.stable, 0, steps),
510
673
  };
511
674
  }
512
675
  export function decodeChatSummary(v: unknown): ChatSummary {
@@ -532,14 +695,6 @@ export function decodeChatSummary(v: unknown): ChatSummary {
532
695
  export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
533
696
  canonicalJson(v, 512 * 1024);
534
697
  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
698
  if (k === "workflow-transcript") {
544
699
  const o = closed(v, ["kind", "runId", "stepId", "turn"]);
545
700
  return {
@@ -657,12 +812,50 @@ export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
657
812
  };
658
813
  }
659
814
  if (kind === "conversation") {
660
- const o = closed(v, ["kind", "summary", "turns", "before"]);
815
+ const o = closed(v, ["kind", "summary", "turns"]);
816
+ const summary = nullable(o.summary, decodeChatSummary),
817
+ turns = integer(o.turns, 0);
818
+ if (!summary && turns) throw new ContractError("invalid");
819
+ return { kind, summary, turns };
820
+ }
821
+ if (kind === "conversation-turn") {
822
+ const o = closed(v, [
823
+ "kind",
824
+ "conversationId",
825
+ "ordinal",
826
+ "turn",
827
+ "start",
828
+ "steps",
829
+ ]);
830
+ const turn = decodeChatTurnHeader(o.turn),
831
+ start = integer(o.start, 0),
832
+ steps = list(o.steps, STEP_READ, decodeChatStep);
833
+ if (start + steps.length > turn.steps) throw new ContractError("invalid");
661
834
  return {
662
835
  kind,
663
- summary: nullable(o.summary, decodeChatSummary),
664
- turns: list(o.turns, 20, decodeChatTurn),
665
- before: nullable(o.before, decodeId),
836
+ conversationId: decodeId(o.conversationId),
837
+ ordinal: integer(o.ordinal, 0),
838
+ turn,
839
+ start,
840
+ steps,
841
+ };
842
+ }
843
+ if (kind === "conversation-step") {
844
+ const o = closed(v, [
845
+ "kind",
846
+ "conversationId",
847
+ "turnId",
848
+ "step",
849
+ "part",
850
+ "item",
851
+ ]);
852
+ return {
853
+ kind,
854
+ conversationId: decodeId(o.conversationId),
855
+ turnId: decodeId(o.turnId),
856
+ step: integer(o.step, 0),
857
+ part: integer(o.part, 0),
858
+ item: decodeChatItem(o.item),
666
859
  };
667
860
  }
668
861
  if (kind === "files" || kind === "directories") {