@abelxiaoxing/cadence 2.0.20 → 2.0.22

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.
@@ -1,8 +1,10 @@
1
1
  /** Finite, authenticated workbench API. Ordinary chat is not task admission. */
2
2
  import {
3
3
  bool,
4
+ ContractError,
4
5
  canonicalJson,
5
6
  closed,
7
+ decodeHash,
6
8
  decodeId,
7
9
  integer,
8
10
  list,
@@ -12,7 +14,62 @@ import {
12
14
  } from "./core.ts";
13
15
  import { type ChoiceVersion, decodeChoiceVersion } from "./models.ts";
14
16
 
17
+ import {
18
+ decodeWorkflowProposal,
19
+ decodeWorkflowRequest,
20
+ decodeWorkflowResponse,
21
+ decodeWorkflowRun,
22
+ type WorkflowProposal,
23
+ type WorkflowRequest,
24
+ type WorkflowResponse,
25
+ type WorkflowRun,
26
+ } from "./workflow.ts";
27
+
28
+ export interface CadenceConversation {
29
+ id: string;
30
+ revision: number;
31
+ enabled: boolean;
32
+ head: string | null;
33
+ proposal: WorkflowProposal | null;
34
+ runs: string[];
35
+ /** Present only in a read projection of an unusable retained record: every
36
+ * command for that conversation refuses; nothing is inferred or repaired.
37
+ * Never persisted. */
38
+ notice?: string;
39
+ }
40
+ export function decodeCadenceConversation(v: unknown): CadenceConversation {
41
+ const degraded =
42
+ v !== null && typeof v === "object" && Object.hasOwn(v, "notice");
43
+ const o = closed(v, [
44
+ "id",
45
+ "revision",
46
+ "enabled",
47
+ "head",
48
+ "proposal",
49
+ "runs",
50
+ ...(degraded ? ["notice"] : []),
51
+ ]);
52
+ return {
53
+ id: decodeId(o.id),
54
+ revision: integer(o.revision, 0),
55
+ enabled: bool(o.enabled),
56
+ head: nullable(o.head, decodeId),
57
+ proposal: nullable(o.proposal, decodeWorkflowProposal),
58
+ runs: list(o.runs, 200, decodeId),
59
+ ...(degraded ? { notice: text(o.notice, 1024, true) } : {}),
60
+ };
61
+ }
62
+ /** Browser conversation that owns an active workflow; native runs have none. */
63
+ export interface WorkflowLink {
64
+ runId: string;
65
+ conversationId: string;
66
+ }
15
67
  export type WorkbenchRead =
68
+ | { kind: "cadence-conversation"; id: string }
69
+ | { kind: "workflow-active" }
70
+ | { kind: "workflow-detail"; id: string; stepId: string }
71
+ | { kind: "workflow-transcript"; id: string; stepId: string }
72
+ | { kind: "workflow"; id: string }
16
73
  | { kind: "conversations" }
17
74
  | { kind: "task-origin"; taskId: string }
18
75
  | { kind: "conversation"; id: string; before: string | null }
@@ -21,6 +78,48 @@ export type WorkbenchRead =
21
78
  | { kind: "directories"; path: string }
22
79
  | { kind: "projects" };
23
80
  export type WorkbenchCommand =
81
+ | {
82
+ kind: "cadence-exit";
83
+ commandId: string;
84
+ conversationId: string;
85
+ head: string | null;
86
+ revision: number;
87
+ }
88
+ | { kind: "restore-model"; commandId: string; epoch: string }
89
+ | {
90
+ kind: "cadence-send";
91
+ commandId: string;
92
+ conversationId: string;
93
+ head: string | null;
94
+ text: string;
95
+ model: ChoiceVersion;
96
+ revision: number;
97
+ intent: "message" | "enter" | "exit";
98
+ }
99
+ | {
100
+ kind: "cadence-prepare";
101
+ commandId: string;
102
+ conversationId: string;
103
+ revision: number;
104
+ proposalId: string;
105
+ digest: string;
106
+ }
107
+ | {
108
+ kind: "cadence-approve";
109
+ commandId: string;
110
+ conversationId: string;
111
+ revision: number;
112
+ proposalId: string;
113
+ digest: string;
114
+ grant: string;
115
+ }
116
+ | { kind: "workflow-cancel"; commandId: string; epoch: string; runId: string }
117
+ | {
118
+ kind: "workflow";
119
+ commandId: string;
120
+ epoch: string;
121
+ request: WorkflowRequest;
122
+ }
24
123
  | {
25
124
  kind: "link-task";
26
125
  commandId: string;
@@ -76,6 +175,7 @@ export interface ChatSummary {
76
175
  head: string;
77
176
  status: ChatStatus;
78
177
  updatedAt: number;
178
+ cadence?: boolean;
79
179
  }
80
180
  export interface FileEntry {
81
181
  name: string;
@@ -83,6 +183,28 @@ export interface FileEntry {
83
183
  directory: boolean;
84
184
  }
85
185
  export type WorkbenchResult =
186
+ | {
187
+ kind: "workflow-transcript";
188
+ runId: string;
189
+ stepId: string;
190
+ turn: ChatTurn | null;
191
+ }
192
+ | { kind: "cadence-conversation"; state: CadenceConversation }
193
+ | {
194
+ kind: "workflow-active";
195
+ runs: WorkflowRun[];
196
+ /** At most one link per listed run; set by the workbench composition. */
197
+ links?: WorkflowLink[];
198
+ }
199
+ | {
200
+ kind: "cadence-grant";
201
+ commandId: string;
202
+ grant: string;
203
+ expiresAt: number;
204
+ revision: number;
205
+ proposal: WorkflowProposal;
206
+ }
207
+ | { kind: "workflow"; commandId: string | null; response: WorkflowResponse }
86
208
  | { kind: "task-origin"; taskId: string; conversationId: string | null }
87
209
  | { kind: "conversations"; entries: ChatSummary[] }
88
210
  | {
@@ -106,6 +228,11 @@ export interface WorkbenchPort {
106
228
  read(request: WorkbenchRead): WorkbenchResult;
107
229
  command(
108
230
  request: WorkbenchCommand,
231
+ caller?: {
232
+ attachmentId: string;
233
+ authority: "browser" | "local";
234
+ accessGeneration: number;
235
+ },
109
236
  ): WorkbenchResult | Promise<WorkbenchResult>;
110
237
  }
111
238
  const statuses = [
@@ -120,6 +247,22 @@ const statuses = [
120
247
  export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
121
248
  const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
122
249
  switch (kind) {
250
+ case "cadence-conversation": {
251
+ const o = closed(v, ["kind", "id"]);
252
+ return { kind, id: decodeId(o.id) };
253
+ }
254
+ case "workflow-active":
255
+ closed(v, ["kind"]);
256
+ return { kind };
257
+ case "workflow-transcript":
258
+ case "workflow-detail": {
259
+ const o = closed(v, ["kind", "id", "stepId"]);
260
+ return { kind, id: decodeId(o.id), stepId: text(o.stepId, 160, true) };
261
+ }
262
+ case "workflow": {
263
+ const o = closed(v, ["kind", "id"]);
264
+ return { kind, id: decodeId(o.id) };
265
+ }
123
266
  case "conversations":
124
267
  case "projects":
125
268
  closed(v, ["kind"]);
@@ -143,6 +286,86 @@ export function decodeWorkbenchRead(v: unknown): WorkbenchRead {
143
286
  }
144
287
  export function decodeWorkbenchCommand(v: unknown): WorkbenchCommand {
145
288
  const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
289
+ if (kind === "restore-model" || kind === "workflow-cancel") {
290
+ const o = closed(
291
+ v,
292
+ kind === "restore-model"
293
+ ? ["kind", "commandId", "epoch"]
294
+ : ["kind", "commandId", "epoch", "runId"],
295
+ );
296
+ const base = { commandId: decodeId(o.commandId), epoch: decodeId(o.epoch) };
297
+ return kind === "restore-model"
298
+ ? { kind, ...base }
299
+ : { kind, ...base, runId: decodeId(o.runId) };
300
+ }
301
+ if (kind === "cadence-exit") {
302
+ const o = closed(v, [
303
+ "kind",
304
+ "commandId",
305
+ "conversationId",
306
+ "head",
307
+ "revision",
308
+ ]);
309
+ return {
310
+ kind,
311
+ commandId: decodeId(o.commandId),
312
+ conversationId: decodeId(o.conversationId),
313
+ head: nullable(o.head, decodeId),
314
+ revision: integer(o.revision, 0),
315
+ };
316
+ }
317
+ if (kind === "cadence-send") {
318
+ const o = closed(v, [
319
+ "kind",
320
+ "commandId",
321
+ "conversationId",
322
+ "head",
323
+ "text",
324
+ "model",
325
+ "revision",
326
+ "intent",
327
+ ]);
328
+ return {
329
+ kind,
330
+ commandId: decodeId(o.commandId),
331
+ conversationId: decodeId(o.conversationId),
332
+ head: nullable(o.head, decodeId),
333
+ text: text(o.text, 16384, true),
334
+ model: decodeChoiceVersion(o.model),
335
+ revision: integer(o.revision, 0),
336
+ intent: oneOf(o.intent, ["message", "enter", "exit"]),
337
+ };
338
+ }
339
+ if (kind === "cadence-prepare" || kind === "cadence-approve") {
340
+ const keys = [
341
+ "kind",
342
+ "commandId",
343
+ "conversationId",
344
+ "revision",
345
+ "proposalId",
346
+ "digest",
347
+ ];
348
+ const o = closed(v, kind === "cadence-approve" ? [...keys, "grant"] : keys);
349
+ const base = {
350
+ commandId: decodeId(o.commandId),
351
+ conversationId: decodeId(o.conversationId),
352
+ revision: integer(o.revision, 0),
353
+ proposalId: decodeId(o.proposalId),
354
+ digest: decodeHash(o.digest),
355
+ };
356
+ return kind === "cadence-prepare"
357
+ ? { kind, ...base }
358
+ : { kind, ...base, grant: decodeId(o.grant) };
359
+ }
360
+ if (kind === "workflow") {
361
+ const o = closed(v, ["kind", "commandId", "epoch", "request"]);
362
+ return {
363
+ kind,
364
+ commandId: decodeId(o.commandId),
365
+ epoch: decodeId(o.epoch),
366
+ request: decodeWorkflowRequest(o.request),
367
+ };
368
+ }
146
369
  if (kind === "link-task") {
147
370
  const o = closed(v, [
148
371
  "kind",
@@ -234,19 +457,90 @@ export function decodeChatTurn(v: unknown): ChatTurn {
234
457
  };
235
458
  }
236
459
  export function decodeChatSummary(v: unknown): ChatSummary {
237
- const o = closed(v, ["id", "title", "head", "status", "updatedAt"]);
460
+ const modern =
461
+ v !== null && typeof v === "object" && Object.hasOwn(v, "cadence");
462
+ const o = closed(v, [
463
+ "id",
464
+ "title",
465
+ "head",
466
+ "status",
467
+ "updatedAt",
468
+ ...(modern ? ["cadence"] : []),
469
+ ]);
238
470
  return {
239
471
  id: decodeId(o.id),
240
472
  title: text(o.title, 512, true),
241
473
  head: decodeId(o.head),
242
474
  status: oneOf(o.status, statuses),
243
475
  updatedAt: integer(o.updatedAt, 0),
476
+ ...(modern ? { cadence: bool(o.cadence) } : {}),
244
477
  };
245
478
  }
246
479
  export function decodeWorkbenchResult(v: unknown): WorkbenchResult {
247
- // A complete finite reply fits the existing page ceiling. No arbitrary file body.
248
480
  canonicalJson(v, 512 * 1024);
481
+ const k = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
482
+ if (k === "workflow-transcript") {
483
+ const o = closed(v, ["kind", "runId", "stepId", "turn"]);
484
+ return {
485
+ kind: k,
486
+ runId: decodeId(o.runId),
487
+ stepId: text(o.stepId, 160, true),
488
+ turn: nullable(o.turn, decodeChatTurn),
489
+ };
490
+ }
491
+ if (k === "cadence-conversation") {
492
+ const o = closed(v, ["kind", "state"]);
493
+ return { kind: k, state: decodeCadenceConversation(o.state) };
494
+ }
495
+ if (k === "workflow-active") {
496
+ const linked =
497
+ v !== null && typeof v === "object" && Object.hasOwn(v, "links");
498
+ const o = closed(v, ["kind", "runs", ...(linked ? ["links"] : [])]);
499
+ const runs = list(o.runs, 4, decodeWorkflowRun);
500
+ const links = linked
501
+ ? list(o.links, 4, (value) => {
502
+ const l = closed(value, ["runId", "conversationId"]);
503
+ return {
504
+ runId: decodeId(l.runId),
505
+ conversationId: decodeId(l.conversationId),
506
+ };
507
+ })
508
+ : [];
509
+ if (
510
+ new Set(links.map((l) => l.runId)).size !== links.length ||
511
+ links.some((l) => !runs.some((r) => r.id === l.runId))
512
+ )
513
+ throw new ContractError("invalid");
514
+ return { kind: k, runs, links };
515
+ }
516
+ if (k === "cadence-grant") {
517
+ const o = closed(v, [
518
+ "kind",
519
+ "commandId",
520
+ "grant",
521
+ "expiresAt",
522
+ "revision",
523
+ "proposal",
524
+ ]);
525
+ return {
526
+ kind: k,
527
+ commandId: decodeId(o.commandId),
528
+ grant: decodeId(o.grant),
529
+ expiresAt: integer(o.expiresAt, 0),
530
+ revision: integer(o.revision, 0),
531
+ proposal: decodeWorkflowProposal(o.proposal),
532
+ };
533
+ }
534
+ // A complete finite reply fits the existing page ceiling. No arbitrary file body.
249
535
  const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
536
+ if (kind === "workflow") {
537
+ const o = closed(v, ["kind", "commandId", "response"]);
538
+ return {
539
+ kind,
540
+ commandId: nullable(o.commandId, decodeId),
541
+ response: decodeWorkflowResponse(o.response),
542
+ };
543
+ }
250
544
  if (kind === "task-origin") {
251
545
  const o = closed(v, ["kind", "taskId", "conversationId"]);
252
546
  return {