@abelxiaoxing/cadence 2.0.33 → 2.0.35

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.
@@ -11,7 +11,12 @@ import {
11
11
  oneOf,
12
12
  text,
13
13
  } from "./core.ts";
14
- import { type ChoiceVersion, decodeChoiceVersion } from "./models.ts";
14
+ import {
15
+ type ChoiceVersion,
16
+ decodeChoiceVersion,
17
+ decodeExecutionContext,
18
+ type ExecutionContext,
19
+ } from "./models.ts";
15
20
 
16
21
  export const WORKFLOW_LIMITS = {
17
22
  tasks: 16,
@@ -59,6 +64,7 @@ export interface WorkflowProposal {
59
64
  model: ChoiceVersion;
60
65
  plan: WorkflowPlan;
61
66
  cwd: string;
67
+ checkoutRoot?: string;
62
68
  display?: { model: string; branch: string; baseCommit: string };
63
69
  }
64
70
  export interface WorkflowTiming {
@@ -103,6 +109,7 @@ export interface WorkflowStep {
103
109
  truncated: boolean;
104
110
  }
105
111
  export interface WorkflowRun {
112
+ execution?: ExecutionContext;
106
113
  diagnostics?: WorkflowDiagnostics;
107
114
  id: string;
108
115
  epoch: string;
@@ -355,6 +362,7 @@ export function decodeWorkflowProposal(v: unknown): WorkflowProposal {
355
362
  "model",
356
363
  "plan",
357
364
  "cwd",
365
+ ...(Object.hasOwn(v ?? {}, "checkoutRoot") ? ["checkoutRoot"] : []),
358
366
  ...(modern ? ["display"] : []),
359
367
  ]);
360
368
  return {
@@ -365,6 +373,9 @@ export function decodeWorkflowProposal(v: unknown): WorkflowProposal {
365
373
  model: decodeChoiceVersion(o.model),
366
374
  plan: decodeWorkflowPlan(o.plan),
367
375
  cwd: text(o.cwd, 4096, true),
376
+ ...(o.checkoutRoot === undefined
377
+ ? {}
378
+ : { checkoutRoot: text(o.checkoutRoot, 4096, true) }),
368
379
  ...(modern
369
380
  ? {
370
381
  display: (() => {
@@ -445,6 +456,7 @@ export function decodeWorkflowRun(v: unknown): WorkflowRun {
445
456
  "mergedCommit",
446
457
  "humanReviewed",
447
458
  ...(Object.hasOwn(v ?? {}, "diagnostics") ? ["diagnostics"] : []),
459
+ ...(Object.hasOwn(v ?? {}, "execution") ? ["execution"] : []),
448
460
  ]);
449
461
  const status = (x: unknown) =>
450
462
  oneOf(x, [
@@ -458,6 +470,9 @@ export function decodeWorkflowRun(v: unknown): WorkflowRun {
458
470
  if (o.humanReviewed !== false)
459
471
  throw new Error("Automatic execution is not human review");
460
472
  return {
473
+ ...(o.execution === undefined
474
+ ? {}
475
+ : { execution: decodeExecutionContext(o.execution) }),
461
476
  ...(o.diagnostics === undefined
462
477
  ? {}
463
478
  : { diagnostics: decodeWorkflowDiagnostics(o.diagnostics) }),
@@ -266,6 +266,7 @@ var ERROR_CODES = [
266
266
  "expired-grant",
267
267
  "consumed-grant",
268
268
  "unsupported-context",
269
+ "retired-conversation",
269
270
  "quarantine",
270
271
  "capacity",
271
272
  "unavailable",
@@ -1921,6 +1922,19 @@ function resolveWorkspace(cwd) {
1921
1922
  throw new Error("Git workspace lookup refused");
1922
1923
  return { root: actual, cwd: actual };
1923
1924
  }
1925
+ function withinWorkspace(root, cwd) {
1926
+ const path2 = relative(root, cwd);
1927
+ return !isAbsolute(path2) && path2 !== ".." && !path2.startsWith(`..${sep}`);
1928
+ }
1929
+ function ownerDefaultInCheckout(root, cwd) {
1930
+ try {
1931
+ return resolveWorkspace(cwd).root === root;
1932
+ } catch (error) {
1933
+ if (!absent(error))
1934
+ throw error;
1935
+ return withinWorkspace(root, canonicalAmbientRoot(cwd));
1936
+ }
1937
+ }
1924
1938
  function defaultStateHome() {
1925
1939
  const candidate = process.platform === "win32" ? process.env.LOCALAPPDATA : process.env.XDG_STATE_HOME;
1926
1940
  if (candidate && !isAbsolute(candidate))
@@ -2902,7 +2916,7 @@ function decodeProjectEntry(value) {
2902
2916
  id: decodeHash(o.id),
2903
2917
  path: text(o.path, 4096, true),
2904
2918
  cwd: text(o.cwd, 4096, true),
2905
- epoch: decodeId(o.epoch)
2919
+ epoch: o.epoch === null ? null : decodeId(o.epoch)
2906
2920
  };
2907
2921
  }
2908
2922
  var RECOVERY_SCOPES = ["registry", "workbench", "project"];
@@ -4397,7 +4411,7 @@ class GatewayStore {
4397
4411
  if (!file)
4398
4412
  throw new ContractError("corrupt-selected");
4399
4413
  const project = decodeGatewayProject(json(file));
4400
- if (entry.name !== `${project.id}.json` || hash(project.path) !== project.id)
4414
+ if (entry.name !== `${project.id}.json` || hash(project.path) !== project.id && hash(`directory\x00${project.cwd}`) !== project.id)
4401
4415
  throw new ContractError("corrupt-selected");
4402
4416
  out.push(project);
4403
4417
  }
@@ -4407,7 +4421,7 @@ class GatewayStore {
4407
4421
  return out;
4408
4422
  }
4409
4423
  associatedProjects() {
4410
- const projects = this.projects(), recovery = this.read()?.recovery;
4424
+ const projects = this.projects().filter((project) => project.epoch !== null), recovery = this.read()?.recovery;
4411
4425
  if (!recovery)
4412
4426
  return projects;
4413
4427
  const receipt = this.files.read(this.path(recovery.receipt), 65536);
@@ -5754,7 +5768,7 @@ function selectStart(options, ambient) {
5754
5768
  tokenFile: server
5755
5769
  });
5756
5770
  }
5757
- if (fromFile && encoded(fromFile) !== encoded(selected) || options.port !== undefined && options.port !== selected.http.port || options.host !== undefined && options.host !== selected.http.host || options.agentDir !== undefined && resolve5(options.agentDir) !== selected.agentDir || options.tokenFile !== undefined && resolve5(options.tokenFile) !== selected.tokenFile || env && selected.tokenSource !== "environment" || selected.tokenSource === "environment" && !env || resolveWorkspace(selected.cwd).root !== workspace.root)
5771
+ if (fromFile && encoded(fromFile) !== encoded(selected) || options.port !== undefined && options.port !== selected.http.port || options.host !== undefined && options.host !== selected.http.host || options.agentDir !== undefined && resolve5(options.agentDir) !== selected.agentDir || options.tokenFile !== undefined && resolve5(options.tokenFile) !== selected.tokenFile || env && selected.tokenSource !== "environment" || selected.tokenSource === "environment" && !env || !ownerDefaultInCheckout(workspace.root, selected.cwd))
5758
5772
  throw new ContractError("conflict");
5759
5773
  return {
5760
5774
  workspace: workspace.root,
@@ -6297,7 +6311,12 @@ async function release(spec = "latest", npmRun = npm) {
6297
6311
  if (result.status !== 0)
6298
6312
  return;
6299
6313
  try {
6300
- const value = JSON.parse(result.stdout);
6314
+ let value = JSON.parse(result.stdout);
6315
+ if (Array.isArray(value)) {
6316
+ if (value.length !== 1)
6317
+ return;
6318
+ value = value[0];
6319
+ }
6301
6320
  if (typeof value === "string")
6302
6321
  return { version: decodeVersion(value), deprecated: false };
6303
6322
  const o = closed(value, ["version", "deprecated"]);
@@ -6805,7 +6824,7 @@ async function startService(options, foreground, hostUrl) {
6805
6824
  throw new ContractError("conflict");
6806
6825
  return existing;
6807
6826
  }
6808
- return launchHost(input, c, foreground, hostUrl, input.config.cwd, env, network);
6827
+ return launchHost(input, c, foreground, hostUrl, c.store.root, env, network);
6809
6828
  }
6810
6829
  async function launchHost(input, c, foreground, hostUrl, cwd, env, network) {
6811
6830
  const serialized = `${JSON.stringify(input)}
@@ -1,3 +1,4 @@
1
+ import type { ConversationLocation } from "./service/contracts/models.ts";
1
2
  import {
2
3
  decodeWorkbenchCommand,
3
4
  decodeWorkbenchRead,
@@ -143,10 +144,18 @@ export class ServiceClient {
143
144
  const request = decodeWorkbenchRead(input);
144
145
  try {
145
146
  if (!this.transport.workbenchRead) return unavailable(false);
146
- return decodeReply(
147
+ const reply = decodeReply(
147
148
  await this.transport.workbenchRead(request, signal),
148
149
  decodeWorkbenchResult,
149
150
  );
151
+ if (
152
+ reply.kind === "ok" &&
153
+ request.kind === "conversation-context" &&
154
+ (reply.value.kind !== request.kind ||
155
+ reply.value.context.id !== request.location.id)
156
+ )
157
+ return unavailable(false);
158
+ return reply;
150
159
  } catch {
151
160
  return unavailable(false);
152
161
  }
@@ -221,8 +230,22 @@ export class ServiceClient {
221
230
  return unavailable(false);
222
231
  }
223
232
  }
224
- async models(signal?: AbortSignal): Promise<Reply<ModelCatalog>> {
233
+ async models(
234
+ signal?: AbortSignal,
235
+ location?: ConversationLocation,
236
+ ): Promise<Reply<ModelCatalog>> {
225
237
  try {
238
+ if (location) {
239
+ const reply = await this.workbenchRead(
240
+ { kind: "conversation-models", location },
241
+ signal,
242
+ );
243
+ if (reply.kind === "error") return reply;
244
+ return reply.value.kind === "conversation-models" &&
245
+ reply.value.catalog.version.conversation?.id === location.id
246
+ ? { kind: "ok", value: reply.value.catalog }
247
+ : unavailable(false);
248
+ }
226
249
  if (!this.transport.models) return unavailable(false);
227
250
  return decodeReply(
228
251
  await this.transport.models(signal),
@@ -234,19 +257,38 @@ export class ServiceClient {
234
257
  }
235
258
  async chooseModel(
236
259
  input: ChooseModelRequest,
260
+ location?: ConversationLocation,
237
261
  ): Promise<Reply<ModelChoiceState>> {
238
262
  const request = decodeChooseModelRequest(input);
239
263
  try {
240
- if (!this.transport.chooseModel) return unavailable(false);
241
- const reply = decodeReply(
242
- await this.transport.chooseModel(request),
243
- decodeModelChoiceState,
244
- );
264
+ let reply: Reply<ModelChoiceState>;
265
+ if (location) {
266
+ const result = await this.workbenchCommand({
267
+ kind: "conversation-model",
268
+ commandId: crypto.randomUUID(),
269
+ location,
270
+ ...request,
271
+ });
272
+ reply =
273
+ result.kind === "error"
274
+ ? result
275
+ : result.value.kind === "conversation-model"
276
+ ? { kind: "ok", value: result.value.choice }
277
+ : unavailable(true);
278
+ } else {
279
+ if (!this.transport.chooseModel) return unavailable(false);
280
+ reply = decodeReply(
281
+ await this.transport.chooseModel(request),
282
+ decodeModelChoiceState,
283
+ );
284
+ }
245
285
  if (reply.kind === "ok") {
246
286
  const { sourceSelectionId: _id, ...selector } =
247
287
  reply.value.selected ?? {};
248
288
  if (
249
289
  canonicalJson(selector) !== canonicalJson(request.choice) ||
290
+ canonicalJson(reply.value.version.conversation ?? null) !==
291
+ canonicalJson(request.expected.conversation ?? null) ||
250
292
  reply.value.version.epoch !== request.expected.epoch ||
251
293
  reply.value.version.accessGeneration !==
252
294
  request.expected.accessGeneration ||
@@ -254,8 +296,12 @@ export class ServiceClient {
254
296
  request.expected.configRev,
255
297
  request.expected.configRev + 1,
256
298
  ].includes(reply.value.version.configRev) ||
257
- (reply.value.version.configRev === request.expected.configRev) !==
258
- (reply.value.version.catalogId === request.expected.catalogId)
299
+ (!location &&
300
+ (reply.value.version.configRev === request.expected.configRev) !==
301
+ (reply.value.version.catalogId === request.expected.catalogId)) ||
302
+ (!!location &&
303
+ reply.value.version.configRev !== request.expected.configRev &&
304
+ reply.value.version.catalogId === request.expected.catalogId)
259
305
  )
260
306
  return unavailable(true);
261
307
  }