@oddington/sdk 0.1.82 → 0.1.83

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/browser.js CHANGED
@@ -5440,6 +5440,46 @@ function validateResponseDefinition(value) {
5440
5440
  let definition = object(value, "response definition");
5441
5441
  return Object.hasOwn(definition, "options") ? (keys(definition, ["type", "options"], "response definition"), definition.type !== "selection" && fail("Expected a selection definition"), { type: "selection", options: selectionOptions(definition.options, !1) }) : validateLegacyResponseDefinition(definition);
5442
5442
  }
5443
+ function validatePublishedSelectionDefinition(value) {
5444
+ let definition = object(value, "response definition");
5445
+ return keys(definition, ["type", "options"], "response definition"), definition.type !== "selection" && fail("Expected a selection definition"), { type: "selection", options: selectionOptions(definition.options, !0) };
5446
+ }
5447
+ function validatePublishedResponseDefinition(value) {
5448
+ let definition = object(value, "response definition");
5449
+ return Object.hasOwn(definition, "options") ? validatePublishedSelectionDefinition(definition) : validateLegacyResponseDefinition(definition);
5450
+ }
5451
+ function selectionResponse(saved, value) {
5452
+ let response2 = object(value, "response");
5453
+ keys(response2, ["option", "value", "note"], "response");
5454
+ let number = natural(response2.option, "option number", 1), option = saved.options[number - 1];
5455
+ option || fail("Option was not published");
5456
+ let answer;
5457
+ switch (option.type) {
5458
+ case "choice":
5459
+ answer = id(response2.value, "choice value"), answer !== option.value && fail("Choice does not match published option");
5460
+ break;
5461
+ case "record":
5462
+ answer = validateRecordReference(response2.value), (answer.repository !== option.value.repository || answer.ref !== option.value.ref) && fail("Record does not match published option");
5463
+ break;
5464
+ case "record-picker":
5465
+ answer = validateRecordReference(response2.value), (answer.repository !== option.source.collection.repository || option.source.refPrefix && !answer.ref.startsWith(option.source.refPrefix)) && fail("Record does not match picker source");
5466
+ break;
5467
+ }
5468
+ return option.type === "choice" && option.note ? { option: number, value: answer, note: text(response2.note, "required note").trim() } : (Object.hasOwn(response2, "note") && fail("This option does not accept a note"), { option: number, value: answer });
5469
+ }
5470
+ function validateResponse(definition, value) {
5471
+ let saved = validatePublishedResponseDefinition(definition);
5472
+ if ("options" in saved) return selectionResponse(saved, value);
5473
+ let response2 = object(value, "response");
5474
+ if (saved.source.type === "records") {
5475
+ keys(response2, ["selection"], "response");
5476
+ let record4 = validateRecordReference(response2.selection);
5477
+ return record4.repository !== saved.source.collection.repository && fail("Record belongs to another repository"), { selection: record4 };
5478
+ }
5479
+ keys(response2, ["selection", "note"], "response");
5480
+ let choice = id(response2.selection, "response selection"), option = saved.source.options.find((option2) => option2.value === choice);
5481
+ return option || fail("Choice was not published"), option.note ? { selection: choice, note: text(response2.note, "required note").trim() } : (Object.hasOwn(response2, "note") && fail("This choice does not accept a note"), { selection: choice });
5482
+ }
5443
5483
  function handlerReference(value) {
5444
5484
  let source2 = object(value, "handler reference");
5445
5485
  return keys(source2, ["id", "input"], "handler reference"), { id: id(source2.id, "handler ID"), input: serializable(source2.input) };
@@ -5548,6 +5588,60 @@ function publicationRequestKey(value) {
5548
5588
  let options = object(value, "publication options");
5549
5589
  return keys(options, ["requestKey"], "publication options"), id(options.requestKey, "publication request key");
5550
5590
  }
5591
+ function askPublicationOptions(value) {
5592
+ let options = object(value, "publication options");
5593
+ return keys(options, ["requestKey", "wait"], "publication options"), options.wait !== void 0 && typeof options.wait != "boolean" && fail("publication wait must be a boolean"), { requestKey: id(options.requestKey, "publication request key"), wait: options.wait === !0 };
5594
+ }
5595
+ function resolvedAskAnswer(question2, value) {
5596
+ let answer = object(value, `answer ${question2.id}`);
5597
+ if (answer.answered === !1)
5598
+ return keys(answer, ["answered"], `answer ${question2.id}`), { answered: !1 };
5599
+ if (answer.answered !== !0 && fail(`answer ${question2.id} requires answered`), "options" in question2.response) {
5600
+ keys(answer, ["answered", "option", "value", "note"], `answer ${question2.id}`);
5601
+ let definition = { type: "selection", options: question2.response.options.map((option, index) => ({
5602
+ ...option,
5603
+ id: index + 1
5604
+ })) }, response3 = validateResponse(definition, {
5605
+ option: answer.option,
5606
+ value: answer.value,
5607
+ ...Object.hasOwn(answer, "note") ? { note: answer.note } : {}
5608
+ });
5609
+ return "option" in response3 || fail(`answer ${question2.id} must use a numbered selection`), {
5610
+ answered: !0,
5611
+ option: response3.option,
5612
+ value: response3.value,
5613
+ ...response3.note === void 0 ? {} : { note: response3.note }
5614
+ };
5615
+ }
5616
+ keys(answer, ["answered", "value", "note"], `answer ${question2.id}`);
5617
+ let response2 = validateResponse(question2.response, {
5618
+ selection: answer.value,
5619
+ ...Object.hasOwn(answer, "note") ? { note: answer.note } : {}
5620
+ });
5621
+ return "selection" in response2 || fail(`answer ${question2.id} must use a source selection`), {
5622
+ answered: !0,
5623
+ value: response2.selection,
5624
+ ...response2.note === void 0 ? {} : { note: response2.note }
5625
+ };
5626
+ }
5627
+ function validateResolvedAsk(value, publication) {
5628
+ let source2 = object(value, "resolved Ask");
5629
+ keys(source2, ["id", "number", "revision", "title", "outcome", "answers"], "resolved Ask");
5630
+ let answers = object(source2.answers, "resolved Ask answers");
5631
+ keys(answers, publication.questions.map((question2) => question2.id), "resolved Ask answers");
5632
+ let retained = Object.fromEntries(publication.questions.map((question2) => [
5633
+ question2.id,
5634
+ resolvedAskAnswer(question2, answers[question2.id])
5635
+ ]));
5636
+ return {
5637
+ id: id(source2.id, "resolved Ask ID"),
5638
+ number: natural(source2.number, "resolved Ask number", 1),
5639
+ revision: natural(source2.revision, "resolved Ask revision", 1),
5640
+ title: text(source2.title, "resolved Ask title"),
5641
+ outcome: text(source2.outcome, "resolved Ask outcome"),
5642
+ answers: retained
5643
+ };
5644
+ }
5551
5645
  function digestPublicationRequest(value, options) {
5552
5646
  let source2 = object(value, "digest");
5553
5647
  return keys(source2, ["title", "description", "recipients", "views", "body", "notifyImmediately"], "digest"), {
@@ -5556,16 +5650,23 @@ function digestPublicationRequest(value, options) {
5556
5650
  };
5557
5651
  }
5558
5652
  function createInboxPublisher(transport) {
5653
+ async function publishAsk(value, options) {
5654
+ let source2 = object(value, "ask");
5655
+ keys(source2, ["title", "description", "recipients", "views", "body", "questions", "notifyImmediately", "tags", "recordRefs"], "ask");
5656
+ let publication = enforcePublicationSize(validatePublication({ ...source2, kind: "ask" }));
5657
+ publication.kind !== "ask" && fail("Expected an Ask publication");
5658
+ let boundedOptions = askPublicationOptions(options);
5659
+ if (boundedOptions.wait)
5660
+ return publication.questions.some((question2) => question2.handler) || fail("A waiting Ask requires a handler-backed Question"), transport.wait || fail("Waiting Ask publication requires a hosted queue-origin processing Run"), validateResolvedAsk(await transport.wait({
5661
+ requestKey: boundedOptions.requestKey,
5662
+ wait: !0,
5663
+ item: publication
5664
+ }), publication);
5665
+ let result2 = await transport.publish({ requestKey: boundedOptions.requestKey, item: publication });
5666
+ return id(result2.id, "published ask ID"), (!("revision" in result2) || !Number.isSafeInteger(result2.revision) || result2.revision !== 1) && fail("Initial ask publication must return revision 1"), { id: result2.id, revision: result2.revision };
5667
+ }
5559
5668
  return {
5560
- async publishAsk(value, options) {
5561
- let source2 = object(value, "ask");
5562
- keys(source2, ["title", "description", "recipients", "views", "body", "questions", "notifyImmediately", "tags", "recordRefs"], "ask");
5563
- let result2 = await transport.publish({
5564
- requestKey: publicationRequestKey(options),
5565
- item: enforcePublicationSize(validatePublication({ ...source2, kind: "ask" }))
5566
- });
5567
- return id(result2.id, "published ask ID"), (!("revision" in result2) || !Number.isSafeInteger(result2.revision) || result2.revision !== 1) && fail("Initial ask publication must return revision 1"), { id: result2.id, revision: result2.revision };
5568
- },
5669
+ publishAsk,
5569
5670
  async publishDigest(value, options) {
5570
5671
  let result2 = await transport.publish(digestPublicationRequest(value, options));
5571
5672
  return { id: id(result2.id, "published digest ID") };
@@ -6627,7 +6728,7 @@ async function executeRoutineDefinition(definition, input, runtime2) {
6627
6728
  if (result2 !== void 0) return invalid3();
6628
6729
  }
6629
6730
 
6630
- var queueStates = ["pending", "running", "failed", "succeeded", "removed"], uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
6731
+ var queueStates = ["pending", "running", "waiting", "failed", "succeeded", "removed"], uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
6631
6732
  function object3(value) {
6632
6733
  if (!value || typeof value != "object" || Array.isArray(value)) throw new Error("Invalid queue result");
6633
6734
  return value;
@@ -6640,6 +6741,12 @@ function date(value) {
6640
6741
  if (typeof value != "string" || !Number.isFinite(Date.parse(value))) throw new Error("Invalid queue timestamp");
6641
6742
  return value;
6642
6743
  }
6744
+ function parseQueueAskWait(value) {
6745
+ let v = object3(value), ask = object3(v.ask);
6746
+ if (!Number.isSafeInteger(ask.number) || Number(ask.number) < 1 || typeof ask.title != "string" || !ask.title.trim())
6747
+ throw new Error("Invalid queue Ask wait");
6748
+ return { runId: id2(v.runId), ask: { id: id2(ask.id), number: ask.number, title: ask.title } };
6749
+ }
6643
6750
  function parseQueueSummary(value) {
6644
6751
  let v = object3(value), counts = object3(v.counts);
6645
6752
  if (typeof v.routineId != "string" || !/^[a-z0-9][a-z0-9-]{0,62}$/.test(v.routineId) || typeof v.paused != "boolean" || queueStates.some((state) => !Number.isSafeInteger(counts[state]) || Number(counts[state]) < 0)) throw new Error("Invalid queue summary");
@@ -6655,15 +6762,18 @@ function parseQueueItem(value) {
6655
6762
  if (!Object.hasOwn(v, "input") || !queueStates.includes(v.state) || !Array.isArray(v.associatedRunIds) || v.idempotencyKey !== void 0 && typeof v.idempotencyKey != "string") throw new Error("Invalid queue item");
6656
6763
  let failure = v.failure === void 0 ? void 0 : object3(v.failure);
6657
6764
  if (failure && typeof failure.message != "string") throw new Error("Invalid queue failure");
6765
+ let associatedRunIds = v.associatedRunIds.map(id2), askWait = v.askWait === void 0 ? void 0 : parseQueueAskWait(v.askWait);
6766
+ if (askWait && !associatedRunIds.includes(askWait.runId)) throw new Error("Invalid queue Ask wait");
6658
6767
  return {
6659
6768
  id: id2(v.id),
6660
6769
  input: v.input,
6661
6770
  state: v.state,
6662
- associatedRunIds: v.associatedRunIds.map(id2),
6771
+ associatedRunIds,
6663
6772
  createdAt: date(v.createdAt),
6664
6773
  updatedAt: date(v.updatedAt),
6665
6774
  ...v.idempotencyKey === void 0 ? {} : { idempotencyKey: v.idempotencyKey },
6666
6775
  ...v.currentRunId === void 0 ? {} : { currentRunId: id2(v.currentRunId) },
6776
+ ...askWait === void 0 ? {} : { askWait },
6667
6777
  ...v.failureId === void 0 ? {} : { failureId: id2(v.failureId) },
6668
6778
  ...failure ? { failure: { message: failure.message } } : {}
6669
6779
  };
@@ -7084,7 +7194,7 @@ function progress(value) {
7084
7194
  }
7085
7195
  function parseRunStep(value, expected) {
7086
7196
  let v = record2(value);
7087
- if (typeof v.id != "string" || !name2.test(v.id) || expected !== void 0 && v.id !== expected || typeof v.title != "string" || !v.title.trim() || v.title.length > 256 || !["pending", "running", "completed", "failed", "skipped"].includes(v.status)) return invalid5();
7197
+ if (typeof v.id != "string" || !name2.test(v.id) || expected !== void 0 && v.id !== expected || typeof v.title != "string" || !v.title.trim() || v.title.length > 256 || !["pending", "running", "waiting", "completed", "failed", "skipped"].includes(v.status)) return invalid5();
7088
7198
  let output = captureRetainedStepOutput(v.id, {
7089
7199
  ...Object.hasOwn(v, "summary") ? { summary: v.summary } : {},
7090
7200
  ...Object.hasOwn(v, "data") ? { data: v.data } : {}
@@ -7101,7 +7211,7 @@ function parseRunStep(value, expected) {
7101
7211
  }
7102
7212
  function parseRemoteRun(value, expected = {}) {
7103
7213
  let v = record2(value);
7104
- if (typeof v.id != "string" || !uuid3.test(v.id) || expected.id !== void 0 && v.id !== expected.id || typeof v.routine != "string" || !name2.test(v.routine) || expected.routine !== void 0 && v.routine !== expected.routine || v.placement !== "remote" || !["queued", "running", "succeeded", "failed"].includes(v.status) || !Array.isArray(v.steps) || v.steps.length > 64) return invalid5();
7214
+ if (typeof v.id != "string" || !uuid3.test(v.id) || expected.id !== void 0 && v.id !== expected.id || typeof v.routine != "string" || !name2.test(v.routine) || expected.routine !== void 0 && v.routine !== expected.routine || v.placement !== "remote" || !["queued", "running", "waiting", "succeeded", "failed"].includes(v.status) || !Array.isArray(v.steps) || v.steps.length > 64) return invalid5();
7105
7215
  let steps = v.steps.map((value2) => {
7106
7216
  let step = record2(value2);
7107
7217
  return typeof step.detailAvailable != "boolean" || Object.hasOwn(step, "data") ? invalid5() : { ...parseRunStep(step), detailAvailable: step.detailAvailable };
@@ -7123,7 +7233,9 @@ function parseRemoteRun(value, expected = {}) {
7123
7233
  }
7124
7234
  else failure = { message: f.message };
7125
7235
  }
7126
- return Object.hasOwn(v, "result") && v.status !== "succeeded" ? invalid5() : {
7236
+ if (Object.hasOwn(v, "result") && v.status !== "succeeded") return invalid5();
7237
+ let askWait = v.askWait === void 0 ? void 0 : parseQueueAskWait(v.askWait);
7238
+ return askWait && askWait.runId !== v.id ? invalid5() : {
7127
7239
  id: v.id,
7128
7240
  routine: v.routine,
7129
7241
  placement: "remote",
@@ -7131,6 +7243,8 @@ function parseRemoteRun(value, expected = {}) {
7131
7243
  createdAt: time(v.createdAt),
7132
7244
  ...times(v),
7133
7245
  steps,
7246
+ ...v.queueItemId === void 0 ? {} : { queueItemId: typeof v.queueItemId == "string" && uuid3.test(v.queueItemId) ? v.queueItemId : invalid5() },
7247
+ ...askWait === void 0 ? {} : { askWait },
7134
7248
  ...Object.hasOwn(v, "result") ? { result: bounded(v.result) } : {},
7135
7249
  ...failure ? { failure } : {}
7136
7250
  };
@@ -7144,7 +7258,7 @@ function routineRunHandle(operations2) {
7144
7258
  let previous;
7145
7259
  for (; ; ) {
7146
7260
  let state = await operations2.current(), encoded = JSON.stringify(state);
7147
- if (encoded !== previous && (previous = encoded, yield state), state.status === "succeeded" || state.status === "failed" || state.status === "legacy") return;
7261
+ if (encoded !== previous && (previous = encoded, yield state), state.status === "waiting" || state.status === "succeeded" || state.status === "failed" || state.status === "legacy") return;
7148
7262
  await (operations2.wait?.() ?? new Promise((resolve) => setTimeout(resolve, 100)));
7149
7263
  }
7150
7264
  }
@@ -7764,7 +7878,11 @@ function parseInboxCurrent(value) {
7764
7878
  }
7765
7879
  function parseInboxItem(value) {
7766
7880
  let item = object5(json(value));
7767
- return assert(id4(item.id) && id4(item.workspaceId) && integer(item.number) && integer(item.revision) && integer(item.activityVersion, 0) && ["ask", "digest"].includes(String(item.kind)) && ["open", "closed"].includes(String(item.lifecycle)) && text2(item.title) && text2(item.description, !0) && text2(item.author) && date3(item.publishedAt) && optionalDate(item.seenAt) && optionalDate(item.digestReadAt)), item.outcome !== null && assert(text2(object5(item.outcome).outcome)), "canonicalRoutineId" in item && assert(item.canonicalRoutineId === null || isAppRoutineName(item.canonicalRoutineId)), "apiClientId" in item && assert(item.apiClientId === null || id4(item.apiClientId) && item.kind === "digest"), item.kind === "ask" ? askMetadata(item) : assert(!("tags" in item) && !("recordRefs" in item)), array2(item.body).forEach(validateBlock), array2(item.views).forEach((value2) => {
7881
+ if (assert(id4(item.id) && id4(item.workspaceId) && integer(item.number) && integer(item.revision) && integer(item.activityVersion, 0) && ["ask", "digest"].includes(String(item.kind)) && ["open", "closed"].includes(String(item.lifecycle)) && text2(item.title) && text2(item.description, !0) && text2(item.author) && date3(item.publishedAt) && optionalDate(item.seenAt) && optionalDate(item.digestReadAt)), item.outcome !== null && assert(text2(object5(item.outcome).outcome)), "canonicalRoutineId" in item && assert(item.canonicalRoutineId === null || isAppRoutineName(item.canonicalRoutineId)), "apiClientId" in item && assert(item.apiClientId === null || id4(item.apiClientId) && item.kind === "digest"), "queueContinuation" in item) {
7882
+ let continuation2 = object5(item.queueContinuation);
7883
+ keys2(continuation2, ["routineId", "itemId", "runId"]), assert(item.kind === "ask" && isAppRoutineName(continuation2.routineId) && id4(continuation2.itemId) && id4(continuation2.runId));
7884
+ }
7885
+ return item.kind === "ask" ? askMetadata(item) : assert(!("tags" in item) && !("recordRefs" in item)), array2(item.body).forEach(validateBlock), array2(item.views).forEach((value2) => {
7768
7886
  let view = object5(value2);
7769
7887
  assert(text2(view.id) && text2(view.view) && text2(view.implementation)), json(view.input);
7770
7888
  }), array2(item.people).forEach((value2) => {
@@ -13106,15 +13224,19 @@ function routinePublicationClient2(options = {}) {
13106
13224
  if (!response2.ok) throw new InboxPublicationError(result2.error);
13107
13225
  return result2;
13108
13226
  }
13227
+ let transport = {
13228
+ async publish(body) {
13229
+ return await send3("publish", body);
13230
+ },
13231
+ ...installed2 ? { async wait(body) {
13232
+ return await send3("publish", body);
13233
+ } } : {},
13234
+ async resolve(body) {
13235
+ await send3("resolve", body);
13236
+ }
13237
+ };
13109
13238
  return {
13110
- ...createInboxPublisher({
13111
- async publish(body) {
13112
- return await send3("publish", body);
13113
- },
13114
- async resolve(body) {
13115
- await send3("resolve", body);
13116
- }
13117
- }),
13239
+ ...createInboxPublisher(transport),
13118
13240
  async check(body) {
13119
13241
  await send3("check", { requestKey: body.requestKey, item: enforcePublicationSize(validatePublication(body.item)) });
13120
13242
  }