@lotics/app-sdk 0.128.6 → 0.129.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.js CHANGED
@@ -921,8 +921,12 @@ function ownText(value) {
921
921
  if (typeof value === "string") return value;
922
922
  if (value instanceof Date) return Date.prototype.toString.call(value);
923
923
  if (typeof value === "object" && value !== null) return Object.prototype.toString.call(value);
924
+ if (typeof value === "number") return numberText(value);
924
925
  return String(value);
925
926
  }
927
+ function numberText(value) {
928
+ return Number.isInteger(value) ? String(value) : String(Number(value.toPrecision(15)));
929
+ }
926
930
  function walkJoined(array, write2) {
927
931
  const joining = /* @__PURE__ */ new Set([array]);
928
932
  const stack = [{ array, at: 0 }];
@@ -2663,6 +2667,7 @@ function sortText(value) {
2663
2667
  if (typeof value === "string") return value;
2664
2668
  if (Array.isArray(value)) return textOf(value);
2665
2669
  if (typeof value === "object") return Object.prototype.toString.call(value);
2670
+ if (typeof value === "number") return numberText(value);
2666
2671
  return String(value);
2667
2672
  }
2668
2673
  function compareSortText(a, b) {
@@ -3113,7 +3118,7 @@ function join(arr, separator = ",") {
3113
3118
  if (typeof separator !== "string") {
3114
3119
  throw new ExpressionArgumentError("join: separator must be a string");
3115
3120
  }
3116
- const parts = arr.map(String);
3121
+ const parts = arr.map((element) => typeof element === "number" ? numberText(element) : String(element));
3117
3122
  const separatorsLength = Math.max(0, parts.length - 1) * separator.length;
3118
3123
  assertStringLength("join", parts.reduce((total, part) => total + part.length, separatorsLength));
3119
3124
  return parts.join(separator);
@@ -7276,7 +7281,8 @@ function figure2(name, args) {
7276
7281
  function parsedText(name, value) {
7277
7282
  if (value == null) return null;
7278
7283
  if (typeof value === "string") return value;
7279
- if (typeof value === "number" || typeof value === "boolean") return String(value);
7284
+ if (typeof value === "number") return numberText(value);
7285
+ if (typeof value === "boolean") return String(value);
7280
7286
  if (Array.isArray(value)) return textOf(value);
7281
7287
  throw new ExpressionArgumentError(`${name}: expected text or a number, got an object`);
7282
7288
  }
@@ -7313,7 +7319,7 @@ function objectFromEntries(...args) {
7313
7319
  for (const pair of pairs) {
7314
7320
  if (!Array.isArray(pair)) throw new ExpressionArgumentError("Object.fromEntries: every entry must be a [key, value] pair");
7315
7321
  const key = pair[0];
7316
- const text2 = typeof key === "string" ? key : Array.isArray(key) ? textOf(key) : String(key);
7322
+ const text2 = typeof key === "string" ? key : Array.isArray(key) ? textOf(key) : typeof key === "number" ? numberText(key) : String(key);
7317
7323
  if (isReservedObjectKey(text2)) throw new ExpressionArgumentError(`Object.fromEntries: a key cannot be "${text2}"`);
7318
7324
  defineOwn(result, text2, pair[1] ?? null);
7319
7325
  }
@@ -8835,6 +8841,7 @@ function lexicalAnchor(name, path, ctx, seen) {
8835
8841
  const write2 = step.type === "let_declare" && step.name === name ? step.initial : step.type === "assign" && step.name === name ? step.value : null;
8836
8842
  if (write2 === null) continue;
8837
8843
  found = true;
8844
+ if (write2.kind === "lit" && write2.value === null) continue;
8838
8845
  const anchor = boundValueAnchor(write2, path, ctx, new Set(seen));
8839
8846
  if (!anchor) return null;
8840
8847
  if (agreed === null) {
@@ -9696,7 +9703,7 @@ function resolveObjectKeyExpression(expr2, ctx) {
9696
9703
  }
9697
9704
  function objectKey(value) {
9698
9705
  if (typeof value === "string") return value;
9699
- if (typeof value === "number" && Number.isFinite(value)) return String(value);
9706
+ if (typeof value === "number" && Number.isFinite(value)) return numberText(value);
9700
9707
  throw new WorkflowEvalError(
9701
9708
  `Computed object key must evaluate to a string, got ${describeValue(value)}`,
9702
9709
  { kind: "read", at: "index" }
@@ -10029,6 +10036,109 @@ function stringifyForTemplate(value) {
10029
10036
  return textOf(value);
10030
10037
  }
10031
10038
 
10039
+ // ../shared/src/schemas/select_walk.ts
10040
+ function selectEnd(field, table, walk) {
10041
+ let current = { field, table };
10042
+ let several;
10043
+ for (let hops = 0; hops <= RESULT_HOPS; hops++) {
10044
+ if (walk.holds(current.field, current.table)) return { ...current, hops, several };
10045
+ const step = walk.through(current.field, current.table);
10046
+ if (step === void 0) return void 0;
10047
+ several ??= step.several ? { link: step.link, table: current.table } : void 0;
10048
+ current = { field: step.shown, table: step.target };
10049
+ }
10050
+ return void 0;
10051
+ }
10052
+
10053
+ // ../shared/src/schemas/select_options.ts
10054
+ function selectOptionsOf(field) {
10055
+ if (field.type === "select") return field.options;
10056
+ if (field.type === "formula") return field.formula.options;
10057
+ return void 0;
10058
+ }
10059
+
10060
+ // ../shared/src/select_cell_alias.ts
10061
+ var byKeyOf = /* @__PURE__ */ new WeakMap();
10062
+ function fieldsByKey(fields) {
10063
+ const held2 = byKeyOf.get(fields);
10064
+ if (held2 !== void 0) return held2;
10065
+ const built = new Map(fields.map((field) => [field.key, field]));
10066
+ byKeyOf.set(fields, built);
10067
+ return built;
10068
+ }
10069
+ var fieldOf = (fields, key) => fieldsByKey(fields).get(key);
10070
+ function selectFieldHolder(tables, fieldKey) {
10071
+ let found;
10072
+ for (const table of tables) {
10073
+ const field = fieldOf(table.fields, fieldKey);
10074
+ if (field !== void 0) found = { table, field };
10075
+ }
10076
+ return found;
10077
+ }
10078
+ var fieldsWalk = (linked) => ({
10079
+ holds: (field) => selectOptionsOf(field) !== void 0,
10080
+ through: (field, table) => {
10081
+ if (field.type !== "lookup") return void 0;
10082
+ const link2 = fieldOf(table.fields, field.source_field_key);
10083
+ const fields = link2?.type === "select_record_link" ? linked(link2.table_id) : void 0;
10084
+ const shown = fields === void 0 ? void 0 : fieldOf(fields, field.lookup_field_key);
10085
+ return link2?.type !== "select_record_link" || fields === void 0 || shown === void 0 ? void 0 : { link: link2.key, several: link2.cardinality !== "one", target: { fields }, shown };
10086
+ }
10087
+ });
10088
+ function selectBehind(field, owner, linked) {
10089
+ const end = selectEnd(field, { fields: owner }, fieldsWalk(linked));
10090
+ return end === void 0 ? void 0 : { field: end.field, fields: end.table.fields };
10091
+ }
10092
+ function optionAliasOf(field, optionKey) {
10093
+ return selectOptionsOf(field)?.find((option) => option.key === optionKey)?.alias ?? optionKey;
10094
+ }
10095
+ function optionKeyOf(field, ref) {
10096
+ const options = selectOptionsOf(field) ?? [];
10097
+ return (options.find((option) => option.key === ref) ?? options.find((option) => option.alias === ref))?.key;
10098
+ }
10099
+ function renamedDeep(value, rename) {
10100
+ if (typeof value === "string") return rename(value);
10101
+ if (Array.isArray(value)) return value.map((one) => renamedDeep(one, rename));
10102
+ return value;
10103
+ }
10104
+ function optionValue(value, rename) {
10105
+ if (typeof value === "string") return rename(value);
10106
+ if (Array.isArray(value)) return value.map((one) => typeof one === "string" ? rename(one) : one);
10107
+ return value;
10108
+ }
10109
+ function optionAliasing(fields, linked) {
10110
+ const lookedUp = (field) => field.type === "lookup" && linked !== void 0 ? selectBehind(field, fields, linked)?.field : void 0;
10111
+ const option = (fieldKey, optionKey) => {
10112
+ const field = fieldOf(fields, fieldKey);
10113
+ const looked = field === void 0 ? void 0 : lookedUp(field);
10114
+ return looked !== void 0 ? optionAliasOf(looked, optionKey) : field === void 0 ? optionKey : optionAliasOf(field, optionKey);
10115
+ };
10116
+ const cell = (fieldKey, value) => {
10117
+ const field = fieldOf(fields, fieldKey);
10118
+ if (field === void 0 || selectOptionsOf(field) === void 0 && lookedUp(field) === void 0) return value;
10119
+ return field.type === "lookup" ? renamedDeep(value, (one) => option(fieldKey, one)) : optionValue(value, (one) => option(fieldKey, one));
10120
+ };
10121
+ return { option, cell };
10122
+ }
10123
+ function renamedSelectValues(schema, payload, rename) {
10124
+ const walk = (node, value) => {
10125
+ if (node.type === "select" && node.field !== void 0) {
10126
+ const field = node.field;
10127
+ return Array.isArray(value) ? value.map((one) => rename(field, one)) : rename(field, value);
10128
+ }
10129
+ if (node.fields !== void 0 && typeof value === "object" && value !== null && !Array.isArray(value)) {
10130
+ const fields = node.fields;
10131
+ return Object.fromEntries(Object.entries(value).map(([key, inner]) => [key, fields[key] === void 0 ? inner : walk(fields[key], inner)]));
10132
+ }
10133
+ if (node.items !== void 0 && Array.isArray(value)) {
10134
+ const items = node.items;
10135
+ return value.map((item) => walk(items, item));
10136
+ }
10137
+ return value;
10138
+ };
10139
+ return Object.fromEntries(Object.entries(payload).map(([key, value]) => [key, schema[key] === void 0 ? value : walk(schema[key], value)]));
10140
+ }
10141
+
10032
10142
  // ../shared/src/workflow_effect_plan.ts
10033
10143
  var WITHHELD = "__withheld";
10034
10144
 
@@ -10443,7 +10553,28 @@ function clientRuntime(input) {
10443
10553
  }
10444
10554
  };
10445
10555
  }
10446
- async function predictWorkflowEffects(input) {
10556
+ function inBodySpelling(input) {
10557
+ if (input.spelling === "key") return input;
10558
+ const linked = (tableId) => input.tables.get(tableId)?.fields;
10559
+ const holder = (fieldKey) => selectFieldHolder(input.tables.values(), fieldKey)?.field;
10560
+ return {
10561
+ ...input,
10562
+ inputs: renamedSelectValues(input.select_inputs ?? {}, input.inputs, (fieldKey, value) => {
10563
+ const field = holder(fieldKey);
10564
+ const key = field === void 0 || typeof value !== "string" ? void 0 : optionKeyOf(field, value);
10565
+ return field === void 0 || key === void 0 ? value : optionAliasOf(field, key);
10566
+ }),
10567
+ known: (recordId) => {
10568
+ const record = input.known(recordId);
10569
+ const table = record === void 0 ? void 0 : input.tables.get(record.table_id);
10570
+ if (record === void 0 || table === void 0) return record;
10571
+ const { cell } = optionAliasing(table.fields, linked);
10572
+ return { ...record, cells: Object.fromEntries(Object.entries(record.cells).map(([key, value]) => [key, cell(key, value)])) };
10573
+ }
10574
+ };
10575
+ }
10576
+ async function predictWorkflowEffects(given) {
10577
+ const input = inBodySpelling(given);
10447
10578
  const helpers = {
10448
10579
  ...createTimezoneAwareFunctions(input.timezone),
10449
10580
  // Group membership and whom the caller stands in for are the server's to answer.
@@ -10470,7 +10601,7 @@ async function predictWorkflowEffects(input) {
10470
10601
  step_outputs: {},
10471
10602
  runtime: clientRuntime(input),
10472
10603
  helpers,
10473
- body_spelling: "key",
10604
+ body_spelling: input.spelling,
10474
10605
  record_cache: cache,
10475
10606
  fetch_record: fetchRecord,
10476
10607
  tables: knownTables(input.tables),
@@ -10604,7 +10735,7 @@ function loadModel() {
10604
10735
  });
10605
10736
  modelPromise = attempt2;
10606
10737
  }
10607
- function fieldOf(tableId, key) {
10738
+ function fieldOf2(tableId, key) {
10608
10739
  return loaded?.tables.get(tableId)?.fields.find((field) => field.key === key);
10609
10740
  }
10610
10741
  var held = /* @__PURE__ */ new Map();
@@ -10631,7 +10762,7 @@ function noteRows(shape, rows, askedAt) {
10631
10762
  const cells = new Map(held.get(id)?.cells ?? []);
10632
10763
  for (const [column, source] of Object.entries(shape.columns)) {
10633
10764
  if (source === null || "via" in source || source.table_id !== tableId || !(column in row2)) continue;
10634
- const field = fieldOf(tableId, source.field_key);
10765
+ const field = fieldOf2(tableId, source.field_key);
10635
10766
  if (field === void 0) continue;
10636
10767
  const cell = row2[column];
10637
10768
  if ((cells.get(source.field_key)?.askedAt ?? 0) <= askedAt) cells.set(source.field_key, { value: storedFromCell(field, cell), askedAt });
@@ -10671,14 +10802,20 @@ function publish() {
10671
10802
  version += 1;
10672
10803
  for (const listener of listeners) listener();
10673
10804
  }
10674
- function storedFromBody(field, value) {
10805
+ function optionKeysFromBody(field, spelling, values2) {
10806
+ if (spelling === "key" || field.type !== "select") return [...values2];
10807
+ const keys2 = values2.map((one) => typeof one === "string" ? optionKeyOf(field, one) : one);
10808
+ return keys2.some((one) => one === void 0) ? void 0 : keys2;
10809
+ }
10810
+ function storedFromBody(field, spelling, value) {
10675
10811
  if (field === void 0) return void 0;
10676
10812
  switch (field.type) {
10677
10813
  case "select":
10678
10814
  case "select_member":
10679
- if (field.multi === true) return Array.isArray(value) ? value : void 0;
10815
+ if (field.multi === true) return Array.isArray(value) ? optionKeysFromBody(field, spelling, value) : void 0;
10680
10816
  if (value === null) return [];
10681
- return typeof value === "string" ? [value] : Array.isArray(value) ? value : void 0;
10817
+ if (typeof value === "string") return optionKeysFromBody(field, spelling, [value]);
10818
+ return Array.isArray(value) ? optionKeysFromBody(field, spelling, value) : void 0;
10682
10819
  case "select_record_link": {
10683
10820
  const linked = value === null ? [] : Array.isArray(value) ? value : [value];
10684
10821
  if (!linked.every((one) => typeof one === "string")) return void 0;
@@ -10695,13 +10832,13 @@ function storedFromBody(field, value) {
10695
10832
  return value;
10696
10833
  }
10697
10834
  }
10698
- function stagedOf(effect) {
10835
+ function stagedOf(effect, spelling) {
10699
10836
  if (effect.kind === "unknown") return null;
10700
10837
  if (effect.kind === "remove") return effect;
10701
10838
  const cells = {};
10702
10839
  const unknown = [...effect.unknown];
10703
10840
  for (const [key, value] of Object.entries(effect.cells)) {
10704
- const stored = storedFromBody(fieldOf(effect.table_id, key), value);
10841
+ const stored = storedFromBody(fieldOf2(effect.table_id, key), spelling, value);
10705
10842
  if (stored === void 0) unknown.push(key);
10706
10843
  else cells[key] = stored;
10707
10844
  }
@@ -10742,14 +10879,18 @@ async function stageWrite(workflow, inputs) {
10742
10879
  if (failedAtMs === null) reportMisses([{ kind: "model_loading", workflow, count: 1 }]);
10743
10880
  return null;
10744
10881
  }
10745
- const plan = model.model.workflows[workflow]?.plan;
10746
- if (plan === void 0 || plan.length === 0) return null;
10882
+ const described = model.model.workflows[workflow];
10883
+ const plan = described?.plan;
10884
+ if (described === void 0 || plan === void 0 || plan.length === 0) return null;
10885
+ const spelling = described.option_spelling ?? "key";
10747
10886
  const minted = [];
10748
10887
  let prediction;
10749
10888
  try {
10750
10889
  prediction = await predictWorkflowEffects({
10751
10890
  plan,
10752
10891
  inputs,
10892
+ spelling,
10893
+ select_inputs: described.inputs ?? {},
10753
10894
  tables: model.tables,
10754
10895
  known: knownRecord,
10755
10896
  timezone: model.context.timezone ?? Intl.DateTimeFormat().resolvedOptions().timeZone,
@@ -10773,7 +10914,7 @@ async function stageWrite(workflow, inputs) {
10773
10914
  reportMisses([{ kind: "refused", workflow, count: 1 }]);
10774
10915
  return null;
10775
10916
  }
10776
- const staged = prediction.effects.flatMap((effect) => stagedOf(effect) ?? []);
10917
+ const staged = prediction.effects.flatMap((effect) => stagedOf(effect, spelling) ?? []);
10777
10918
  const unknown = new Set(prediction.effects.flatMap((effect) => effect.kind === "unknown" ? [effect.table_id] : []));
10778
10919
  if (staged.length === 0 && unknown.size === 0) {
10779
10920
  reportMisses([{ kind: "nothing_written", workflow, count: 1 }]);
@@ -10867,7 +11008,7 @@ function judgePredictions(shape, rows, askedAt) {
10867
11008
  const cell = predicted.get(`${target}\0${source.field_key}`);
10868
11009
  if (cell === void 0) continue;
10869
11010
  const fieldTable = via ? source.target_table_id : tableId;
10870
- const field = fieldOf(fieldTable, source.field_key);
11011
+ const field = fieldOf2(fieldTable, source.field_key);
10871
11012
  const agrees = comparable(field, cell.value) === comparable(field, row2[column]);
10872
11013
  judge(cell.entry, `${target}\0${source.field_key}`, agrees ? null : { table_id: fieldTable, field_key: source.field_key, column: via ? "via" : "own", kind: "cell" });
10873
11014
  }
@@ -10908,9 +11049,9 @@ function tablesOf(shape) {
10908
11049
  tables.add(source.target_table_id);
10909
11050
  continue;
10910
11051
  }
10911
- const field = fieldOf(source.table_id, source.field_key);
11052
+ const field = fieldOf2(source.table_id, source.field_key);
10912
11053
  if (field?.type === "rollup" || field?.type === "lookup") {
10913
- const link2 = fieldOf(source.table_id, field.source_field_key);
11054
+ const link2 = fieldOf2(source.table_id, field.source_field_key);
10914
11055
  if (link2?.type === "select_record_link") tables.add(link2.table_id);
10915
11056
  }
10916
11057
  }
@@ -10929,7 +11070,7 @@ function writesOver(tables, askedAt) {
10929
11070
  const mine = entries2.filter((entry) => (entry.settledAt === null || askedAt <= entry.settledAt) && touches(entry, tables));
10930
11071
  if (mine.length === 0) return null;
10931
11072
  return {
10932
- field: fieldOf,
11073
+ field: fieldOf2,
10933
11074
  table: (tableId) => model.tables.get(tableId),
10934
11075
  held: heldCells,
10935
11076
  member: (id) => members.get(id),
@@ -12444,14 +12585,14 @@ function comparesFigure(operator) {
12444
12585
  function nameOf(field) {
12445
12586
  return `"${field.name ?? field.key}"`;
12446
12587
  }
12447
- function conditionUnit(condition2, field, fieldOf2) {
12588
+ function conditionUnit(condition2, field, fieldOf3) {
12448
12589
  if (!comparesFigure(condition2.operator)) return { ok: true, unit: void 0 };
12449
12590
  const stated = condition2.unit_option;
12450
12591
  const read2 = figureRowSelect(field);
12451
12592
  if (read2 === void 0) {
12452
12593
  return stated === void 0 ? { ok: true, unit: void 0 } : { ok: false, reason: `${nameOf(field)} is read in one unit for every row, so a condition on it states no unit_option.` };
12453
12594
  }
12454
- const select = fieldOf2(read2.key);
12595
+ const select = fieldOf3(read2.key);
12455
12596
  const options = effectiveSelectOptions(select) ?? [];
12456
12597
  const held2 = options.length === 0 ? `${select === void 0 ? `"${read2.key}"` : nameOf(select)} holds no ${read2.vocabulary} yet` : `one of ${options.map((option2) => `${option2.name} (${option2.key})`).join(", ")}`;
12457
12598
  const reads = `${nameOf(field)} is read in each row's own ${read2.vocabulary} (${select === void 0 ? `"${read2.key}"` : nameOf(select)})`;
package/docs/mutations.md CHANGED
@@ -518,7 +518,7 @@ hands the app its **write model** (`GET /v1/apps/{app_id}/write_model`, read onc
518
518
  bound workflow's record writes and the steps they depend on, where each query's columns and rows
519
519
  come from, and the fields they name. `useWorkflow` walks the workflow's own steps against the rows
520
520
  the app has already read, with the evaluator the engine runs, and stages what it will write over
521
- every read before the request answers:
521
+ every read before the request answers — a workflow's select options compare as that workflow names them, while the app's reads keep their keys:
522
522
 
523
523
  - **Cells.** A written cell redraws in every column that reads it: an option as its label, a member
524
524
  as the face a read or `useMembers` drew, a link as its label — and a column reading the linked row beside it
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lotics/app-sdk",
3
- "version": "0.128.6",
3
+ "version": "0.129.0",
4
4
  "description": "The SDK a Lotics custom-code app reads and writes through — typed hooks over the host bridge, cell readers, mount() and AppRouter",
5
5
  "type": "module",
6
6
  "exports": {