@oddington/sdk 0.1.84 → 0.1.85

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
@@ -5424,6 +5424,16 @@ function selectionOptions(value, published) {
5424
5424
  ...option.description === void 0 ? {} : { description: text(option.description, "Record description") },
5425
5425
  ...numbered
5426
5426
  };
5427
+ case "assign": {
5428
+ keys(option, [...common, "label", "assignee"], "assignment option");
5429
+ let assignee = option.assignee === void 0 ? void 0 : personReference(option.assignee, "assignment target"), isUuid = assignee !== void 0 && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(assignee);
5430
+ return assignee !== void 0 && !isUuid && (published || assignee.length > 320 || !memberEmail.test(assignee)) && fail("Assignment target must be a workspace member UUID or email"), {
5431
+ type: "assign",
5432
+ label: text(option.label, "assignment label"),
5433
+ ...assignee === void 0 ? {} : { assignee: isUuid ? assignee.toLowerCase() : assignee },
5434
+ ...numbered
5435
+ };
5436
+ }
5427
5437
  case "record-picker":
5428
5438
  return keys(option, [...common, "label", "source"], "Record picker option"), {
5429
5439
  type: "record-picker",
@@ -5455,6 +5465,8 @@ function selectionResponse(saved, value) {
5455
5465
  option || fail("Option was not published");
5456
5466
  let answer;
5457
5467
  switch (option.type) {
5468
+ case "assign":
5469
+ return fail("Assignment options cannot be answered");
5458
5470
  case "choice":
5459
5471
  answer = id(response2.value, "choice value"), answer !== option.value && fail("Choice does not match published option");
5460
5472
  break;
@@ -7670,7 +7682,7 @@ function parseInboxRecords(value, maximumRecords = 100) {
7670
7682
 
7671
7683
  var InboxOperationError = class extends Error {
7672
7684
  constructor(status, reason) {
7673
- super(reason === "action_processing" ? "The previous answer is still processing. Wait for it to finish." : reason === "action_failed" ? "The previous answer could not be completed. Further answers are on hold." : reason === "handler_unavailable" ? "Deploy the routine that owns this question\u2019s response handler." : reason === "client_update_required" ? "This app must be updated to use Inbox Questions." : reason === "conflict" ? "This question already has an accepted answer" : reason === "unavailable" ? "Inbox request unavailable. Retry the same action." : reason === "invalid_input" ? "Invalid Inbox request" : "Inbox access unavailable");
7685
+ super(reason === "question_unavailable" ? "This Question is missing, changed, answered, or no longer open. Reload before assigning." : reason === "assignment_denied" ? "Only the current assignee can assign this Question." : reason === "assignee_unavailable" ? "The assignee is not a current workspace member." : reason === "action_processing" ? "The previous answer is still processing. Wait for it to finish." : reason === "action_failed" ? "The previous answer could not be completed. Further answers are on hold." : reason === "handler_unavailable" ? "Deploy the routine that owns this question\u2019s response handler." : reason === "client_update_required" ? "This app must be updated to use Inbox Questions." : reason === "conflict" ? "This question already has an accepted answer" : reason === "unavailable" ? "Inbox request unavailable. Retry the same action." : reason === "invalid_input" ? "Invalid Inbox request" : "Inbox access unavailable");
7674
7686
  this.status = status;
7675
7687
  this.reason = reason;
7676
7688
  }
@@ -7689,6 +7701,11 @@ function questionAvailability(value, available = !1) {
7689
7701
  function parseInboxUnavailableFailure(value, status) {
7690
7702
  try {
7691
7703
  let envelope = object5(value);
7704
+ for (let [reason, code] of [["question_unavailable", 409], ["assignment_denied", 403], ["assignee_unavailable", 400]]) {
7705
+ let error = new InboxOperationError(code, reason);
7706
+ if (status === code && envelope.error === error.message)
7707
+ return keys2(envelope, ["error"]), error;
7708
+ }
7692
7709
  if (status === 409) {
7693
7710
  keys2(envelope, ["error"]);
7694
7711
  for (let reason of ["action_processing", "action_failed"]) {
@@ -7711,6 +7728,9 @@ function parseInboxUnavailableFailure(value, status) {
7711
7728
  }
7712
7729
  }
7713
7730
  var INBOX_RESULT_BYTES = 8 * 1024 * 1024, INBOX_INPUT_BYTES = 8192, INBOX_LOCAL_INPUT_BYTES = 256 * 1024, INBOX_QUESTION_CONTRACT = "question-work-v2", INBOX_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
7731
+ function isAssignmentTarget(value) {
7732
+ return typeof value == "string" && value === value.trim() && value.length <= 320 && (INBOX_UUID.test(value) || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value));
7733
+ }
7714
7734
  function isAppQuestionOperation(operation2) {
7715
7735
  return operation2 === "questions.list" || operation2 === "questions.answer" || operation2 === "questions.listRecordOptions";
7716
7736
  }
@@ -7729,7 +7749,9 @@ var operations = {
7729
7749
  "items.listRecipientCandidates": ["itemId"],
7730
7750
  "items.addRecipient": ["itemId", "personId"],
7731
7751
  "questions.answer": ["itemId", "revision", "questionId", "response", "submissionKey"],
7732
- "questions.listRecordOptions": ["itemId", "revision", "questionId"]
7752
+ "questions.listRecordOptions": ["itemId", "revision", "questionId"],
7753
+ "questions.listAssignmentCandidates": ["itemId", "revision", "questionId"],
7754
+ "questions.assign": ["itemId", "revision", "questionId", "assignee"]
7733
7755
  };
7734
7756
  function assert(value) {
7735
7757
  if (!value) throw new Error("Invalid Inbox result");
@@ -7808,6 +7830,9 @@ function responseDefinition(value) {
7808
7830
  []
7809
7831
  ), option.type === "choice" ? choiceValue(option.value) : recordReference(option.value), choiceDetails(option);
7810
7832
  break;
7833
+ case "assign":
7834
+ keys2(option, ["id", "type", "label", "assignee"], []), "assignee" in option && assert(id4(option.assignee));
7835
+ break;
7811
7836
  case "record-picker": {
7812
7837
  keys2(option, ["id", "type", "label", "source"]);
7813
7838
  let source3 = object5(option.source);
@@ -7856,7 +7881,7 @@ function response(value, definition, numbered = !1) {
7856
7881
  if (typeof selected == "string" ? choiceValue(selected) : recordReference(selected), options || numbered) {
7857
7882
  if (keys2(answer, ["option", "value", "note"], []), assert(integer(answer.option) && answer.option <= (options?.length ?? 9)), options) {
7858
7883
  let option = object5(options[answer.option - 1]);
7859
- assert(option.type === "choice" == (typeof selected == "string")), assert("note" in answer == (option.type === "choice" && "note" in option));
7884
+ assert(option.type !== "assign"), assert(option.type === "choice" == (typeof selected == "string")), assert("note" in answer == (option.type === "choice" && "note" in option));
7860
7885
  }
7861
7886
  } else assert(Object.keys(answer).every((key) => ["selection", "note"].includes(key)));
7862
7887
  return Object.hasOwn(answer, "note") && assert(text2(answer.note)), answer;
@@ -7872,7 +7897,7 @@ function parseInboxRequest(value) {
7872
7897
  let input = { ...object5(envelope.input) }, required = operations[operation2];
7873
7898
  assert(required.every((key) => Object.hasOwn(input, key)) && Object.keys(input).every((key) => required.includes(key) || operation2 === "questions.answer" && key === "label" || operation2 === "questions.listRecordOptions" && envelope.contract === INBOX_QUESTION_CONTRACT && key === "option" || (operation2 === "items.list" || operation2 === "questions.list") && key === "beforeNumber" || operation2 === "questions.list" && key === "routines")), operation2 === "questions.list" && (assert(Object.hasOwn(input, "routines")), assert(array2(input.routines).every(isAppRoutineName)));
7874
7899
  for (let key of ["itemId", "personId", "fileId"]) key in input && assert(id4(input[key]));
7875
- if ("revision" in input && assert(integer(input.revision)), "beforeNumber" in input && assert(integer(input.beforeNumber)), "questionId" in input && assert(text2(input.questionId) && input.questionId === input.questionId.trim()), "submissionKey" in input && assert(text2(input.submissionKey) && input.submissionKey.length <= 128 && input.submissionKey === input.submissionKey.trim()), "title" in input && assert(text2(input.title)), "lifecycle" in input && assert(input.lifecycle === "open" || input.lifecycle === "closed"), "records" in input && (input.records = parseInboxRecordRefs(input.records)), "option" in input && assert(integer(input.option) && input.option <= 9), "response" in input && response(input.response, void 0, envelope.contract === INBOX_QUESTION_CONTRACT && "option" in object5(input.response)), "label" in input) {
7900
+ if ("revision" in input && assert(integer(input.revision)), "beforeNumber" in input && assert(integer(input.beforeNumber)), "questionId" in input && assert(text2(input.questionId) && input.questionId === input.questionId.trim()), "assignee" in input && assert(isAssignmentTarget(input.assignee)), "submissionKey" in input && assert(text2(input.submissionKey) && input.submissionKey.length <= 128 && input.submissionKey === input.submissionKey.trim()), "title" in input && assert(text2(input.title)), "lifecycle" in input && assert(input.lifecycle === "open" || input.lifecycle === "closed"), "records" in input && (input.records = parseInboxRecordRefs(input.records)), "option" in input && assert(integer(input.option) && input.option <= 9), "response" in input && response(input.response, void 0, envelope.contract === INBOX_QUESTION_CONTRACT && "option" in object5(input.response)), "label" in input) {
7876
7901
  let answer = object5(input.response);
7877
7902
  assert(text2(input.label) && typeof ("option" in answer ? answer.value : answer.selection) == "object");
7878
7903
  }
@@ -7930,7 +7955,7 @@ function parseInboxItem(value) {
7930
7955
  let event = object5(value2);
7931
7956
  assert(integer(event.id) && text2(event.type) && (event.actorId === null || id4(event.actorId)) && (event.questionId === null || text2(event.questionId)) && integer(event.revision) && date3(event.createdAt));
7932
7957
  let data3 = object5(event.data);
7933
- event.type === "recipient-added" && (keys2(data3, ["personId"]), assert(id4(event.actorId) && id4(data3.personId)));
7958
+ event.type === "question-assigned" && (keys2(data3, ["previousAssigneeId", "assigneeId", "addedRecipient"]), assert(id4(event.actorId) && text2(event.questionId) && id4(data3.previousAssigneeId) && id4(data3.assigneeId) && typeof data3.addedRecipient == "boolean")), event.type === "recipient-added" && (keys2(data3, ["personId"]), assert(id4(event.actorId) && id4(data3.personId)));
7934
7959
  }), array2(item.work).forEach((value2) => {
7935
7960
  let work = object5(value2);
7936
7961
  assert(id4(work.id) && text2(work.questionId) && text2(work.reason) && optionalDate(work.settledAt));
@@ -8006,13 +8031,21 @@ function parseInboxResult(operation2, value, contract = INBOX_QUESTION_CONTRACT)
8006
8031
  let update = object5(value), item = parseInboxItem(update.item);
8007
8032
  return assert((update.activityId === null || integer(update.activityId)) && optionalDate(update.changedAt)), { ...update, item };
8008
8033
  }
8009
- if (operation2 === "items.listRecipientCandidates") {
8034
+ if (operation2 === "items.listRecipientCandidates" || operation2 === "questions.listAssignmentCandidates") {
8010
8035
  let candidates = array2(value);
8011
8036
  return candidates.forEach((value2) => {
8012
8037
  let person = object5(value2);
8013
- assert(id4(person.id) && text2(person.name) && text2(person.email));
8038
+ operation2 === "questions.listAssignmentCandidates" && keys2(person, ["id", "name", "email"]), assert(id4(person.id) && text2(person.name) && text2(person.email));
8014
8039
  }), assert(new Set(candidates.map((value2) => object5(value2).id)).size === candidates.length), candidates;
8015
8040
  }
8041
+ if (operation2 === "questions.assign") {
8042
+ let result2 = object5(value);
8043
+ keys2(result2, ["item", "questionId", "previousAssigneeId", "assigneeId", "activityId", "assignedAt"]);
8044
+ let item = parseInboxItem(result2.item);
8045
+ assert(item.kind === "ask" && text2(result2.questionId) && id4(result2.previousAssigneeId) && id4(result2.assigneeId));
8046
+ let question2 = item.questions.find((question3) => question3.id === result2.questionId);
8047
+ return assert(question2 && question2.to === result2.assigneeId && question2.acceptedResponse === null && question2.execution === null && question2.allowedResponders.includes(result2.previousAssigneeId) && question2.allowedResponders.includes(result2.assigneeId) && item.people.some((person) => person.id === result2.assigneeId) && item.people.some((person) => person.id === result2.previousAssigneeId)), result2.previousAssigneeId === result2.assigneeId ? assert(result2.activityId === null && result2.assignedAt === null) : (assert(integer(result2.activityId) && date3(result2.assignedAt)), assert(item.activity.some((event) => event.id === result2.activityId && event.type === "question-assigned" && event.questionId === result2.questionId && event.revision === item.revision && event.createdAt === result2.assignedAt && event.actorId === result2.previousAssigneeId && event.data.previousAssigneeId === result2.previousAssigneeId && event.data.assigneeId === result2.assigneeId))), { ...result2, item };
8048
+ }
8016
8049
  if (operation2 === "items.addRecipient") {
8017
8050
  let addition = object5(value), item = parseInboxItem(addition.item);
8018
8051
  return assert(id4(addition.addedPersonId) && item.people.some((person) => person.id === addition.addedPersonId)), { ...addition, item };
@@ -8033,6 +8066,15 @@ function parseInboxResult(operation2, value, contract = INBOX_QUESTION_CONTRACT)
8033
8066
  }
8034
8067
  throw new Error("File content is not a JSON result");
8035
8068
  }
8069
+ function assertInboxAssignmentMatches(result2, input) {
8070
+ assert(result2.item.id === input.itemId && result2.item.revision === input.revision && result2.questionId === input.questionId && result2.assigneeId.toLowerCase() === input.assignee.toLowerCase());
8071
+ }
8072
+ function assignmentTargetId(assignee, candidates) {
8073
+ if (INBOX_UUID.test(assignee)) return assignee.toLowerCase();
8074
+ let matches2 = candidates.filter((person) => person.email.toLowerCase() === assignee.toLowerCase());
8075
+ if (matches2.length !== 1) throw new InboxOperationError(400, "assignee_unavailable");
8076
+ return matches2[0].id;
8077
+ }
8036
8078
  function parseInboxBootstrap(value) {
8037
8079
  let snapshot = object5(value);
8038
8080
  keys2(snapshot, ["current", "items", "hydrations"]);
@@ -11989,12 +12031,17 @@ function subscribeInbox(resolve, authorize, onDirty, onState, rt = runtime, sign
11989
12031
  return signal?.aborted ? revoked() : (signal?.addEventListener("abort", revoked, { once: !0 }), initialize()), stop;
11990
12032
  }
11991
12033
 
11992
- async function limitedBody(request3, limit, timeoutMs) {
12034
+ async function limitedBody(request3, limit, timeoutMs, signal) {
11993
12035
  if (!request3.body) return new Uint8Array();
11994
- let reader = request3.body.getReader(), expired = !1, timer = timeoutMs === void 0 ? void 0 : setTimeout(() => {
12036
+ let reader = request3.body.getReader(), expired = !1, abort = () => {
12037
+ expired = !0, reader.cancel(signal?.reason).catch(() => {
12038
+ });
12039
+ }, timer = timeoutMs === void 0 ? void 0 : setTimeout(() => {
11995
12040
  expired = !0, reader.cancel().catch(() => {
11996
12041
  });
11997
- }, timeoutMs), chunks = [], length = 0;
12042
+ }, timeoutMs);
12043
+ signal?.addEventListener("abort", abort, { once: !0 });
12044
+ let chunks = [], length = 0;
11998
12045
  try {
11999
12046
  for (; ; ) {
12000
12047
  let { done, value } = await reader.read();
@@ -12011,7 +12058,7 @@ async function limitedBody(request3, limit, timeoutMs) {
12011
12058
  contents.set(chunk, offset), offset += chunk.byteLength;
12012
12059
  return contents;
12013
12060
  } finally {
12014
- timer !== void 0 && clearTimeout(timer);
12061
+ timer !== void 0 && clearTimeout(timer), signal?.removeEventListener("abort", abort);
12015
12062
  }
12016
12063
  }
12017
12064
 
@@ -12215,8 +12262,12 @@ async function send2(operation2, input, resolved, admitted) {
12215
12262
  }
12216
12263
  if (operation2 === "current" && parsed.workspace.slug !== context.workspace) throw unavailable5();
12217
12264
  if ("itemId" in envelope.input) {
12218
- let item = operation2 === "items.read" ? parsed : operation2 === "items.rename" || operation2 === "items.setLifecycle" || operation2 === "items.addRecipient" ? parsed.item : void 0;
12265
+ let item = operation2 === "items.read" ? parsed : operation2 === "items.rename" || operation2 === "items.setLifecycle" || operation2 === "items.addRecipient" || operation2 === "questions.assign" ? parsed.item : void 0;
12219
12266
  if (item && (item.id !== envelope.input.itemId || "revision" in envelope.input && item.revision !== envelope.input.revision) || operation2 === "items.addRecipient" && parsed.addedPersonId !== envelope.input.personId) throw unavailable5();
12267
+ if (operation2 === "questions.assign") {
12268
+ let result2 = parsed;
12269
+ if (assertInboxAssignmentMatches(result2, envelope.input), result2.item.workspaceId !== host?.context.workspaceId || result2.previousAssigneeId !== context.memberId) throw unavailable5();
12270
+ }
12220
12271
  }
12221
12272
  return parsed;
12222
12273
  } catch (error) {
@@ -12315,6 +12366,25 @@ var inbox = {
12315
12366
  addRecipient: async (input) => await send2("items.addRecipient", input)
12316
12367
  },
12317
12368
  questions: {
12369
+ listAssignmentCandidates: async (input) => await send2("questions.listAssignmentCandidates", input),
12370
+ async assign(input) {
12371
+ let supplied = parseInboxRequest({ operation: "questions.assign", input }).input, initial;
12372
+ try {
12373
+ initial = await resolveAppContext(!1, !0);
12374
+ } catch (error) {
12375
+ throw contextFailure(error);
12376
+ }
12377
+ if (initial.kind === "local") throw new InboxOperationError(403, "access_denied");
12378
+ let host;
12379
+ try {
12380
+ host = await resolveHostedInboxContext(initial, "member");
12381
+ } catch (error) {
12382
+ throw hostFailure(error, initial);
12383
+ }
12384
+ if (host.context.kind !== "inbox") throw new InboxOperationError(403, "access_denied");
12385
+ let assignee = assignmentTargetId(supplied.assignee, INBOX_UUID.test(supplied.assignee) ? [] : await send2("questions.listAssignmentCandidates", { itemId: supplied.itemId, revision: supplied.revision, questionId: supplied.questionId }, initial, host));
12386
+ return await send2("questions.assign", { ...supplied, assignee }, initial, host);
12387
+ },
12318
12388
  async list(input) {
12319
12389
  let supplied = parseInboxRequest({ contract: INBOX_QUESTION_CONTRACT, operation: "questions.list", input }).input;
12320
12390
  if (Object.hasOwn(supplied, "beforeNumber")) throw new InboxOperationError(400, "invalid_input");