@lace-cloud/chat-core 0.19.0 → 0.20.0

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/dist/index.cjs CHANGED
@@ -278,9 +278,35 @@ function trainingRows(turn, part) {
278
278
  }));
279
279
  }
280
280
 
281
+ // src/contract/cancel.ts
282
+ var Cancelled = class extends Error {
283
+ constructor() {
284
+ super("the call was cancelled");
285
+ }
286
+ };
287
+ function stopIfCancelled(signal) {
288
+ if (signal?.aborted) throw new Cancelled();
289
+ }
290
+ function untilCancelled(work, signal) {
291
+ if (signal === void 0) return work;
292
+ let stop = () => {
293
+ };
294
+ const cancelled = new Promise((_, fail) => {
295
+ stop = () => fail(new Cancelled());
296
+ if (signal.aborted) stop();
297
+ else signal.addEventListener("abort", stop, { once: true });
298
+ });
299
+ return Promise.race([work, cancelled]).finally(() => signal.removeEventListener("abort", stop));
300
+ }
301
+ function cancellable(start, signal) {
302
+ if (signal?.aborted) return Promise.reject(new Cancelled());
303
+ return untilCancelled(start(), signal);
304
+ }
305
+
281
306
  // src/ranking/client.ts
282
307
  var RANK_PATH = "/api/v1/candidates/rank";
283
308
  var DECISIONS_PATH = "/api/v1/candidates/decisions";
309
+ var RANKING_CALL_TIMEOUT_MS = 12e3;
284
310
  var RankingUnavailable = class extends Error {
285
311
  };
286
312
  function isRecord(value) {
@@ -305,21 +331,60 @@ function rankedOf(body) {
305
331
  const thresholds = thresholdsOf(body.thresholds);
306
332
  return typeof body.capture_id === "string" && typeof body.head === "string" && scores.every((score) => score !== void 0) && thresholds !== void 0 ? { capture_id: body.capture_id, head: body.head, scores, thresholds } : void 0;
307
333
  }
334
+ var NotCarried = class extends Error {
335
+ };
336
+ function answerOf(answer) {
337
+ if (!isRecord(answer)) return "failed";
338
+ if (answer.carried === false) return "uncarried";
339
+ return typeof answer.status === "number" ? { status: answer.status, body: answer.body } : "failed";
340
+ }
341
+ var NO_ANSWER = { failed: "the request failed", unanswered: "no answer from Lace" };
308
342
  function rankingClient(transport) {
309
- const post = (path, body) => transport(path, { method: "POST", body }).catch(() => ({ status: 0, body: void 0 }));
343
+ const post = (path, body, cancel) => {
344
+ if (cancel?.aborted) return Promise.resolve("cancelled");
345
+ const controller = new AbortController();
346
+ const reply = new Promise(
347
+ (made) => made(transport(path, { method: "POST", body, signal: controller.signal }))
348
+ ).then(answerOf).catch(() => "failed");
349
+ let timer;
350
+ const unanswered = new Promise((settle) => {
351
+ timer = setTimeout(() => settle("unanswered"), RANKING_CALL_TIMEOUT_MS);
352
+ });
353
+ return untilCancelled(Promise.race([reply, unanswered]), cancel).catch(() => "cancelled").then((ended) => {
354
+ if (ended === "unanswered" || ended === "cancelled") controller.abort();
355
+ return ended;
356
+ }).finally(() => clearTimeout(timer));
357
+ };
310
358
  return {
311
- async rank(request) {
312
- const { status, body } = await post(RANK_PATH, request);
313
- const ranked = status === 200 ? rankedOf(body) : void 0;
314
- if (ranked === void 0) {
315
- throw new RankingUnavailable(
316
- status === 200 ? "the ranking service answered with no ranking" : `status ${status}`
317
- );
318
- }
319
- return ranked;
320
- },
321
- async decision(report) {
322
- await post(DECISIONS_PATH, report);
359
+ changeCall(cancel) {
360
+ let ended;
361
+ const send = (path, body, signal) => {
362
+ if (ended !== void 0) return { made: false, reply: Promise.resolve(ended) };
363
+ const reply = post(path, body, signal).then((answer) => {
364
+ if (answer === "failed" || answer === "unanswered" || answer === "uncarried") {
365
+ ended = answer;
366
+ }
367
+ return answer;
368
+ });
369
+ return { made: true, reply };
370
+ };
371
+ return {
372
+ async rank(request) {
373
+ const reply = await send(RANK_PATH, request, cancel).reply;
374
+ if (reply === "cancelled") throw new Cancelled();
375
+ if (reply === "uncarried") throw new NotCarried("the host carried nothing");
376
+ if (typeof reply === "string") throw new RankingUnavailable(NO_ANSWER[reply]);
377
+ const ranked = reply.status === 200 ? rankedOf(reply.body) : void 0;
378
+ if (ranked !== void 0) return ranked;
379
+ throw new RankingUnavailable(
380
+ reply.status === 200 ? "the ranking service answered with no ranking" : `status ${reply.status}`
381
+ );
382
+ },
383
+ decision(report) {
384
+ const { made, reply } = send(DECISIONS_PATH, report);
385
+ return { made, settled: reply.then(() => void 0) };
386
+ }
387
+ };
323
388
  },
324
389
  async outcome(decision, outcome) {
325
390
  await post(`${DECISIONS_PATH}/${encodeURIComponent(decision)}/outcomes`, outcome);
@@ -550,9 +615,9 @@ function errorText(error) {
550
615
  function registrationOf(schema, run, failed) {
551
616
  return {
552
617
  schema,
553
- async handler(input) {
618
+ async handler(input, signal) {
554
619
  try {
555
- return await run(input);
620
+ return await run(input, signal);
556
621
  } catch (error) {
557
622
  return failed({ content: errorText(error), isError: true });
558
623
  }
@@ -581,10 +646,10 @@ function commandEnd(path, error) {
581
646
  text: `lace ${path} did not finish cleanly (exit ${error.code}: ${error.stderrTail})`
582
647
  };
583
648
  }
584
- async function runCommand(commands, path, args) {
649
+ async function runCommand(commands, path, args, signal = new AbortController().signal) {
585
650
  const documents = [];
586
651
  try {
587
- for await (const document of commands.invoke(path, args, new AbortController().signal).events) {
652
+ for await (const document of commands.invoke(path, args, signal).events) {
588
653
  documents.push(document);
589
654
  }
590
655
  } catch (error) {
@@ -1025,7 +1090,7 @@ function read(options, reading) {
1025
1090
  (option) => statedSlots(option.slots).every((slot) => reading[slot] === option.slots[slot])
1026
1091
  );
1027
1092
  }
1028
- function answerOf(options, reading) {
1093
+ function answerOf2(options, reading) {
1029
1094
  const named2 = read(options, reading);
1030
1095
  return named2.length === 1 ? named2[0] : void 0;
1031
1096
  }
@@ -1065,14 +1130,15 @@ async function rankWithin(rank, request, statement, kind, candidates) {
1065
1130
  if (holding === void 0) throw new Error("the ranking names no candidate of the set");
1066
1131
  return holding;
1067
1132
  }
1133
+ var NOT_RANKED = { ranked: null, failed: "Lace did not rank" };
1068
1134
  async function ranking(ask, statement, kind, candidates) {
1069
- if (ask.rank === void 0) {
1070
- return { ranking: { ranked: null, failed: "Lace did not rank" }, part: candidates };
1071
- }
1135
+ if (ask.rank === void 0) return { ranking: NOT_RANKED, part: candidates };
1072
1136
  try {
1073
1137
  const { part, ranked } = await rankWithin(ask.rank, ask.request, statement, kind, candidates);
1074
1138
  return { ranking: { ranked, parts: part.length < candidates.length }, part };
1075
1139
  } catch (error) {
1140
+ if (error instanceof Cancelled) throw error;
1141
+ if (error instanceof NotCarried) return { ranking: NOT_RANKED, part: candidates };
1076
1142
  const failed = `Lace could not rank: ${error instanceof Error ? error.message : String(error)}`;
1077
1143
  return { ranking: { ranked: null, failed }, part: candidates };
1078
1144
  }
@@ -1081,7 +1147,7 @@ var asAsked = (ranking2) => ranking2 === null || ranking2.ranked === null || ran
1081
1147
  async function twoSystems(ask, kind, label, options, stated) {
1082
1148
  const candidates = options.map((option2) => option2.candidate);
1083
1149
  const readings = options.map((option2) => option2.slots);
1084
- const chat = stated === void 0 ? answerOf(options, ask.reading) : options.find((option2) => option2.value === stated);
1150
+ const chat = stated === void 0 ? answerOf2(options, ask.reading) : options.find((option2) => option2.value === stated);
1085
1151
  const read2 = await ranking(ask, ask.statement, kind, candidates);
1086
1152
  const composed = compose({
1087
1153
  kind,
@@ -1129,7 +1195,7 @@ function byName(ask, kind, label, options) {
1129
1195
  candidates: options.map((each) => each.candidate),
1130
1196
  facts: options.map((each) => each.facts),
1131
1197
  others: hits.filter((hit) => hit.option !== option).map((hit) => hit.option.candidate.id),
1132
- chat: answerOf(options, ask.reading)?.candidate.id ?? null,
1198
+ chat: answerOf2(options, ask.reading)?.candidate.id ?? null,
1133
1199
  ranking: null,
1134
1200
  composed: null,
1135
1201
  over: [],
@@ -1247,13 +1313,17 @@ var CATALOG_LIMIT = 500;
1247
1313
  var place = {
1248
1314
  id: "place",
1249
1315
  takesValue: false,
1250
- async plan({ canvas, request, ports, pick, note }) {
1316
+ async plan({ canvas, request, ports, signal, pick, note }) {
1251
1317
  const [query] = moduleReferences(request).map((reference) => reference.split("/").slice(-1)[0]);
1252
- const listing = await runCommand(ports.commands, "registry list", {
1253
- axis: "module",
1254
- limit: CATALOG_LIMIT,
1255
- query
1256
- });
1318
+ const listing = await cancellable(
1319
+ () => runCommand(
1320
+ ports.commands,
1321
+ "registry list",
1322
+ { axis: "module", limit: CATALOG_LIMIT, query },
1323
+ signal
1324
+ ),
1325
+ signal
1326
+ );
1257
1327
  if (listing.failed !== void 0) throw new Error(listing.failed.text);
1258
1328
  for (const document of listing.documents) {
1259
1329
  if (document.truncated) {
@@ -1279,18 +1349,21 @@ var place = {
1279
1349
  var connect = {
1280
1350
  id: "connect",
1281
1351
  takesValue: false,
1282
- async plan({ canvas, ports, pick }) {
1352
+ async plan({ canvas, ports, signal, pick }) {
1283
1353
  const source = await pick("instance", "source instance", sourceOptions(canvas.instances));
1284
1354
  const target = await pick(
1285
1355
  "instance",
1286
1356
  "target instance",
1287
1357
  targetOptions(canvas.instances, source)
1288
1358
  );
1289
- const scored = await ports.editor.wiringCandidates({
1290
- scope: source.scope,
1291
- source: source.name,
1292
- target: target.name
1293
- });
1359
+ const scored = await cancellable(
1360
+ () => ports.editor.wiringCandidates({
1361
+ scope: source.scope,
1362
+ source: source.name,
1363
+ target: target.name
1364
+ }),
1365
+ signal
1366
+ );
1294
1367
  const pair = await pick("connection", "connection", connectionOptions(source, target, scored));
1295
1368
  return {
1296
1369
  action: {
@@ -1416,6 +1489,7 @@ var OPERATIONS = [
1416
1489
  remove,
1417
1490
  ungroup
1418
1491
  ];
1492
+ var CANCELLED = { kind: "cancelled" };
1419
1493
  var Fell = class extends Error {
1420
1494
  constructor(label) {
1421
1495
  super(`the ${label} fell to System Two`);
@@ -1442,7 +1516,7 @@ function statedFacts(names2, reading) {
1442
1516
  );
1443
1517
  }
1444
1518
  var asItIs = (apply) => apply();
1445
- async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1519
+ async function resolveAndApply(ports, own, change, call, client, signal, trail, notApplied) {
1446
1520
  const canvas = readCanvas(await latest(ports.source));
1447
1521
  const names2 = canvasNames(canvas);
1448
1522
  const values = [...forms(change.values), ...unnamed(names2, call)];
@@ -1456,12 +1530,7 @@ async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1456
1530
  statements.reduce((longest, each) => each.length > longest.length ? each : longest)
1457
1531
  );
1458
1532
  const rankable = !holdsValue(withhold([request, ...statements].join(" "), [...names2]), values);
1459
- const rank = ports.transport === void 0 ? void 0 : rankingClient(ports.transport).rank;
1460
- if (!rankable && rank !== void 0) {
1461
- trail.notes.push(
1462
- "This change's words or reading hold a value of the call inside a word, so Lace did not rank it."
1463
- );
1464
- }
1533
+ const rank = client?.rank;
1465
1534
  const main = {
1466
1535
  request,
1467
1536
  terms: termsOf(request.split(VALUE_PLACEHOLDER).join(" ")),
@@ -1470,13 +1539,18 @@ async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1470
1539
  ...rankable ? { rank } : {}
1471
1540
  };
1472
1541
  const ask = { ...main, ...alternative === void 0 ? {} : { alternative } };
1542
+ const decided2 = (on, kind, label, options, stated2) => {
1543
+ stopIfCancelled(signal);
1544
+ return decide(on, kind, label, options, stated2);
1545
+ };
1473
1546
  const resolvingInto = (into, trial) => ({
1474
1547
  canvas,
1475
1548
  request,
1476
1549
  values: change.values,
1477
1550
  ports,
1551
+ signal,
1478
1552
  async pick(kind, label, options) {
1479
- const decision = await decide(ask, kind, label, options);
1553
+ const decision = await decided2(ask, kind, label, options);
1480
1554
  into.decisions.push(decision);
1481
1555
  if (trial && !moveStands(composedOf(into.decisions.slice(1)))) throw new Fell(label);
1482
1556
  return decision.option.value;
@@ -1487,7 +1561,7 @@ async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1487
1561
  into.decisions.push(decision);
1488
1562
  return decision.option.value.plan(resolvingInto(into, trial));
1489
1563
  };
1490
- const first = await decide(ask, "operation", "operation", operationOptions(OPERATIONS));
1564
+ const first = await decided2(ask, "operation", "operation", operationOptions(OPERATIONS));
1491
1565
  const statedOperation = operationOptions(OPERATIONS).find(
1492
1566
  (each) => each.candidate.id === first.chat
1493
1567
  );
@@ -1502,6 +1576,7 @@ async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1502
1576
  trail.notes.push(...trial.notes);
1503
1577
  return { operation: first.option.value, plan: planned };
1504
1578
  } catch (error) {
1579
+ stopIfCancelled(signal);
1505
1580
  trail.moved = withhold(movedText(first.option.value.id, error), change.values);
1506
1581
  const standing = {
1507
1582
  ...first,
@@ -1521,9 +1596,8 @@ async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1521
1596
  const { operation, plan } = await moveRule();
1522
1597
  const unread = trail.decisions.every((decision) => decision.others.length === 0);
1523
1598
  if (unread && main.rank !== void 0 && trail.decisions.some(twoSystems2)) {
1524
- await decide(main, "steps", "steps", stepsOptions(), "one").then(
1525
- (decision) => trail.decisions.push(decision)
1526
- );
1599
+ const steps = await decided2(main, "steps", "steps", stepsOptions(), "one");
1600
+ if (steps.ranking !== NOT_RANKED) trail.decisions.push(steps);
1527
1601
  }
1528
1602
  if (change.values.length > 0 && !operation.takesValue && !heldMore(trail.decisions)) {
1529
1603
  trail.notes.push(
@@ -1556,6 +1630,7 @@ async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1556
1630
  )
1557
1631
  };
1558
1632
  }
1633
+ stopIfCancelled(signal);
1559
1634
  const result = await own(() => ports.editor.apply([plan.action]));
1560
1635
  if (!result.success) {
1561
1636
  const reasons = result.errors.map((error) => error.message).join("; ");
@@ -1582,17 +1657,18 @@ async function resolveAndApply(ports, own, change, call, trail, notApplied) {
1582
1657
  withheld: unnamed(names2, call)
1583
1658
  };
1584
1659
  }
1585
- async function runChange(ports, own, change, call, trail) {
1660
+ async function runChange(ports, own, change, call, client, signal, trail) {
1586
1661
  const notApplied = (why) => ({
1587
1662
  kind: "notApplied",
1588
1663
  why: withhold(why, change.values)
1589
1664
  });
1590
1665
  try {
1591
- return await resolveAndApply(ports, own, change, call, trail, notApplied);
1666
+ return await resolveAndApply(ports, own, change, call, client, signal, trail, notApplied);
1592
1667
  } catch (error) {
1593
1668
  if (error instanceof import_engine4.ApplyUnobservedError) {
1594
1669
  return { kind: "unobserved", end: withhold(endText(error.end), change.values) };
1595
1670
  }
1671
+ if (error instanceof Cancelled) return CANCELLED;
1596
1672
  if (error instanceof Undecided) {
1597
1673
  trail.undecided = error;
1598
1674
  const candidates = error.candidates.map((candidate) => candidate.id).join(", ");
@@ -1653,6 +1729,8 @@ function outcomeLines(number, outcome, trail) {
1653
1729
  ];
1654
1730
  case "notApplied":
1655
1731
  return [`${head} not applied. Reason: ${outcome.why}`, ...lines];
1732
+ case "cancelled":
1733
+ return [`${head} not applied. The call was cancelled before it applied.`];
1656
1734
  case "unobserved":
1657
1735
  return [
1658
1736
  `${head} sent, and the engine answered it, but the session's stream ended (${outcome.end}) before the canvas showed it, so whether it applied cannot be read here. Do not send it again before the canvas is open again.`,
@@ -1690,19 +1768,19 @@ function reportOf(id, trail, applied, asked) {
1690
1768
  ];
1691
1769
  return questions.some((question) => question.took !== "named") ? { id, surface: "canvas", questions, applied, asked } : void 0;
1692
1770
  }
1693
- async function applyChanges(ports, changes, own = asItIs) {
1771
+ async function applyChanges(ports, changes, own = asItIs, signal) {
1694
1772
  const call = changes.flatMap((change) => change.values);
1695
- const client = ports.transport === void 0 ? void 0 : rankingClient(ports.transport);
1773
+ const client = ports.transport === void 0 ? void 0 : rankingClient(ports.transport).changeCall(signal);
1696
1774
  const lines = [];
1697
1775
  let applied = [];
1698
- const reports = [];
1776
+ const unapplied = [];
1699
1777
  let last;
1700
1778
  for (const [index, change] of changes.entries()) {
1701
1779
  const trail = { decisions: [], notes: [] };
1702
- const outcome = await runChange(ports, own, change, call, trail);
1780
+ const outcome = signal?.aborted ? CANCELLED : await runChange(ports, own, change, call, client, signal, trail);
1703
1781
  lines.push(
1704
1782
  ...outcomeLines(index + 1, outcome, trail),
1705
- ...trail.notes.map((line) => ` ${line}`)
1783
+ ...outcome.kind === "cancelled" ? [] : trail.notes.map((line) => ` ${line}`)
1706
1784
  );
1707
1785
  if (outcome.kind === "applied") {
1708
1786
  const { canvas } = outcome;
@@ -1712,19 +1790,32 @@ async function applyChanges(ports, changes, own = asItIs) {
1712
1790
  { record: outcome.record, withheld: outcome.withheld, trail }
1713
1791
  ];
1714
1792
  last = canvas;
1715
- } else if (outcome.kind !== "unobserved") {
1793
+ } else if (outcome.kind !== "unobserved" && outcome.kind !== "cancelled") {
1716
1794
  const report = reportOf(crypto.randomUUID(), trail, null, outcome.kind === "asked");
1717
- if (report !== void 0) reports.push(report);
1795
+ if (report !== void 0) unapplied.push(report);
1718
1796
  }
1719
1797
  }
1720
1798
  const left = last;
1721
- const records = left === void 0 ? [] : applied.map(({ record, withheld, trail }) => {
1722
- const settled = { ...record, after: targetState(left, record) };
1723
- const report = reportOf(record.id, trail, appliedText(settled, withheld), false);
1724
- if (report !== void 0) reports.push(report);
1725
- return { ...settled, reported: report !== void 0 && client !== void 0 };
1799
+ const settled = left === void 0 ? [] : applied.map(({ record, withheld, trail }) => {
1800
+ const standing = { ...record, after: targetState(left, record) };
1801
+ const report = reportOf(record.id, trail, appliedText(standing, withheld), false);
1802
+ return { record: standing, report };
1726
1803
  });
1727
- if (client !== void 0) await Promise.all(reports.map((report) => client.decision(report)));
1804
+ const reports = [
1805
+ ...signal?.aborted ? [] : unapplied,
1806
+ ...settled.flatMap(({ report }) => report === void 0 ? [] : [report])
1807
+ ];
1808
+ const sent = client === void 0 ? [] : reports.map((report) => ({ report, ...client.decision(report) }));
1809
+ const made = new Set(sent.flatMap(({ report, made: made2 }) => made2 ? [report] : []));
1810
+ try {
1811
+ await untilCancelled(Promise.all(sent.map(({ settled: settled2 }) => settled2)), signal);
1812
+ } catch (error) {
1813
+ if (!(error instanceof Cancelled)) throw error;
1814
+ }
1815
+ const records = settled.map(({ record, report }) => ({
1816
+ ...record,
1817
+ reported: report !== void 0 && made.has(report)
1818
+ }));
1728
1819
  return { text: lines.join("\n"), applied: records };
1729
1820
  }
1730
1821
  function appliedText(record, withheld) {
@@ -1856,6 +1947,12 @@ function unobserved(error) {
1856
1947
  isError: false
1857
1948
  };
1858
1949
  }
1950
+ function cancelledSaid(operation, sent) {
1951
+ return {
1952
+ text: sent ? `The call was cancelled after the ${operation} was sent: it goes on in the engine, and the canvas below may be from before it ends.` : `The call was cancelled before the ${operation} was sent: nothing was done.`,
1953
+ isError: false
1954
+ };
1955
+ }
1859
1956
  var NO_RECORDS = { applied: [] };
1860
1957
  function canvasTools(ports) {
1861
1958
  const [change, describe, save, generate] = canvasToolSchemas;
@@ -1875,9 +1972,14 @@ function canvasTools(ports) {
1875
1972
  const registrations = [
1876
1973
  registrationOf(
1877
1974
  change,
1878
- async (input) => {
1975
+ async (input, signal) => {
1879
1976
  try {
1880
- const { text: text2, applied } = await applyChanges(ports, parseChanges(input), observer?.own);
1977
+ const { text: text2, applied } = await applyChanges(
1978
+ ports,
1979
+ parseChanges(input),
1980
+ observer?.own,
1981
+ signal
1982
+ );
1881
1983
  observer?.follow(applied);
1882
1984
  return { ...await answer([{ text: text2, isError: false }]), applied };
1883
1985
  } catch (error) {
@@ -1890,13 +1992,15 @@ function canvasTools(ports) {
1890
1992
  registration(describe, () => answer([])),
1891
1993
  registration(
1892
1994
  save,
1893
- async () => answer(
1995
+ async (_input, signal) => answer(
1894
1996
  await attempt(async () => {
1997
+ if (signal?.aborted) return [cancelledSaid("save", false)];
1895
1998
  const saved = { text: "Saved the canvas.", isError: false };
1896
1999
  try {
1897
- await ports.editor.save();
2000
+ await untilCancelled(ports.editor.save(), signal);
1898
2001
  return [saved];
1899
2002
  } catch (error) {
2003
+ if (error instanceof Cancelled) return [cancelledSaid("save", true)];
1900
2004
  return [saved, unobserved(error)];
1901
2005
  }
1902
2006
  })
@@ -1904,12 +2008,17 @@ function canvasTools(ports) {
1904
2008
  ),
1905
2009
  registration(
1906
2010
  generate,
1907
- async () => answer(
2011
+ async (_input, signal) => answer(
1908
2012
  await attempt(async () => {
2013
+ if (signal?.aborted) return [cancelledSaid("Generate", false)];
1909
2014
  const said2 = [];
2015
+ const events = ports.editor.generate()[Symbol.asyncIterator]();
1910
2016
  try {
1911
- for await (const event of ports.editor.generate()) said2.push(...generateSaid(event));
2017
+ for (let next = await untilCancelled(events.next(), signal); next.done !== true; next = await untilCancelled(events.next(), signal)) {
2018
+ said2.push(...generateSaid(next.value));
2019
+ }
1912
2020
  } catch (error) {
2021
+ if (error instanceof Cancelled) return [...said2, cancelledSaid("Generate", true)];
1913
2022
  said2.push(unobserved(error));
1914
2023
  }
1915
2024
  return said2;
@@ -1979,28 +2088,35 @@ function moduleTools({ commands }) {
1979
2088
  return [
1980
2089
  moduleTool(
1981
2090
  validate,
1982
- async () => said(await runCommand(commands, "module validate", {}), trimValidate)
2091
+ async (_input, signal) => said(await runCommand(commands, "module validate", {}, signal), trimValidate)
1983
2092
  ),
1984
2093
  moduleTool(
1985
2094
  search,
1986
- async (input) => said(
1987
- await runCommand(commands, "registry list", {
1988
- axis: "module",
1989
- query: text(input.query),
1990
- system: text(input.system),
1991
- category: text(input.category)
1992
- }),
2095
+ async (input, signal) => said(
2096
+ await runCommand(
2097
+ commands,
2098
+ "registry list",
2099
+ {
2100
+ axis: "module",
2101
+ query: text(input.query),
2102
+ system: text(input.system),
2103
+ category: text(input.category)
2104
+ },
2105
+ signal
2106
+ ),
1993
2107
  trimRegistryList
1994
2108
  )
1995
2109
  ),
1996
- moduleTool(describe, async (input) => {
2110
+ moduleTool(describe, async (input, signal) => {
1997
2111
  const module2 = text(input.module);
1998
2112
  if (module2 === void 0) throw new InvalidInput("`module` is the module as `author/name`");
1999
2113
  return said(
2000
- await runCommand(commands, "registry describe", {
2001
- artifact: `module/${module2}`,
2002
- version: text(input.version)
2003
- }),
2114
+ await runCommand(
2115
+ commands,
2116
+ "registry describe",
2117
+ { artifact: `module/${module2}`, version: text(input.version) },
2118
+ signal
2119
+ ),
2004
2120
  trimRegistryDescribe
2005
2121
  );
2006
2122
  })