@lotics/app-sdk 0.128.7 → 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
@@ -8841,6 +8841,7 @@ function lexicalAnchor(name, path, ctx, seen) {
8841
8841
  const write2 = step.type === "let_declare" && step.name === name ? step.initial : step.type === "assign" && step.name === name ? step.value : null;
8842
8842
  if (write2 === null) continue;
8843
8843
  found = true;
8844
+ if (write2.kind === "lit" && write2.value === null) continue;
8844
8845
  const anchor = boundValueAnchor(write2, path, ctx, new Set(seen));
8845
8846
  if (!anchor) return null;
8846
8847
  if (agreed === null) {
@@ -10035,6 +10036,109 @@ function stringifyForTemplate(value) {
10035
10036
  return textOf(value);
10036
10037
  }
10037
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
+
10038
10142
  // ../shared/src/workflow_effect_plan.ts
10039
10143
  var WITHHELD = "__withheld";
10040
10144
 
@@ -10449,7 +10553,28 @@ function clientRuntime(input) {
10449
10553
  }
10450
10554
  };
10451
10555
  }
10452
- 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);
10453
10578
  const helpers = {
10454
10579
  ...createTimezoneAwareFunctions(input.timezone),
10455
10580
  // Group membership and whom the caller stands in for are the server's to answer.
@@ -10476,7 +10601,7 @@ async function predictWorkflowEffects(input) {
10476
10601
  step_outputs: {},
10477
10602
  runtime: clientRuntime(input),
10478
10603
  helpers,
10479
- body_spelling: "key",
10604
+ body_spelling: input.spelling,
10480
10605
  record_cache: cache,
10481
10606
  fetch_record: fetchRecord,
10482
10607
  tables: knownTables(input.tables),
@@ -10610,7 +10735,7 @@ function loadModel() {
10610
10735
  });
10611
10736
  modelPromise = attempt2;
10612
10737
  }
10613
- function fieldOf(tableId, key) {
10738
+ function fieldOf2(tableId, key) {
10614
10739
  return loaded?.tables.get(tableId)?.fields.find((field) => field.key === key);
10615
10740
  }
10616
10741
  var held = /* @__PURE__ */ new Map();
@@ -10637,7 +10762,7 @@ function noteRows(shape, rows, askedAt) {
10637
10762
  const cells = new Map(held.get(id)?.cells ?? []);
10638
10763
  for (const [column, source] of Object.entries(shape.columns)) {
10639
10764
  if (source === null || "via" in source || source.table_id !== tableId || !(column in row2)) continue;
10640
- const field = fieldOf(tableId, source.field_key);
10765
+ const field = fieldOf2(tableId, source.field_key);
10641
10766
  if (field === void 0) continue;
10642
10767
  const cell = row2[column];
10643
10768
  if ((cells.get(source.field_key)?.askedAt ?? 0) <= askedAt) cells.set(source.field_key, { value: storedFromCell(field, cell), askedAt });
@@ -10677,14 +10802,20 @@ function publish() {
10677
10802
  version += 1;
10678
10803
  for (const listener of listeners) listener();
10679
10804
  }
10680
- 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) {
10681
10811
  if (field === void 0) return void 0;
10682
10812
  switch (field.type) {
10683
10813
  case "select":
10684
10814
  case "select_member":
10685
- 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;
10686
10816
  if (value === null) return [];
10687
- 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;
10688
10819
  case "select_record_link": {
10689
10820
  const linked = value === null ? [] : Array.isArray(value) ? value : [value];
10690
10821
  if (!linked.every((one) => typeof one === "string")) return void 0;
@@ -10701,13 +10832,13 @@ function storedFromBody(field, value) {
10701
10832
  return value;
10702
10833
  }
10703
10834
  }
10704
- function stagedOf(effect) {
10835
+ function stagedOf(effect, spelling) {
10705
10836
  if (effect.kind === "unknown") return null;
10706
10837
  if (effect.kind === "remove") return effect;
10707
10838
  const cells = {};
10708
10839
  const unknown = [...effect.unknown];
10709
10840
  for (const [key, value] of Object.entries(effect.cells)) {
10710
- const stored = storedFromBody(fieldOf(effect.table_id, key), value);
10841
+ const stored = storedFromBody(fieldOf2(effect.table_id, key), spelling, value);
10711
10842
  if (stored === void 0) unknown.push(key);
10712
10843
  else cells[key] = stored;
10713
10844
  }
@@ -10748,14 +10879,18 @@ async function stageWrite(workflow, inputs) {
10748
10879
  if (failedAtMs === null) reportMisses([{ kind: "model_loading", workflow, count: 1 }]);
10749
10880
  return null;
10750
10881
  }
10751
- const plan = model.model.workflows[workflow]?.plan;
10752
- 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";
10753
10886
  const minted = [];
10754
10887
  let prediction;
10755
10888
  try {
10756
10889
  prediction = await predictWorkflowEffects({
10757
10890
  plan,
10758
10891
  inputs,
10892
+ spelling,
10893
+ select_inputs: described.inputs ?? {},
10759
10894
  tables: model.tables,
10760
10895
  known: knownRecord,
10761
10896
  timezone: model.context.timezone ?? Intl.DateTimeFormat().resolvedOptions().timeZone,
@@ -10779,7 +10914,7 @@ async function stageWrite(workflow, inputs) {
10779
10914
  reportMisses([{ kind: "refused", workflow, count: 1 }]);
10780
10915
  return null;
10781
10916
  }
10782
- const staged = prediction.effects.flatMap((effect) => stagedOf(effect) ?? []);
10917
+ const staged = prediction.effects.flatMap((effect) => stagedOf(effect, spelling) ?? []);
10783
10918
  const unknown = new Set(prediction.effects.flatMap((effect) => effect.kind === "unknown" ? [effect.table_id] : []));
10784
10919
  if (staged.length === 0 && unknown.size === 0) {
10785
10920
  reportMisses([{ kind: "nothing_written", workflow, count: 1 }]);
@@ -10873,7 +11008,7 @@ function judgePredictions(shape, rows, askedAt) {
10873
11008
  const cell = predicted.get(`${target}\0${source.field_key}`);
10874
11009
  if (cell === void 0) continue;
10875
11010
  const fieldTable = via ? source.target_table_id : tableId;
10876
- const field = fieldOf(fieldTable, source.field_key);
11011
+ const field = fieldOf2(fieldTable, source.field_key);
10877
11012
  const agrees = comparable(field, cell.value) === comparable(field, row2[column]);
10878
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" });
10879
11014
  }
@@ -10914,9 +11049,9 @@ function tablesOf(shape) {
10914
11049
  tables.add(source.target_table_id);
10915
11050
  continue;
10916
11051
  }
10917
- const field = fieldOf(source.table_id, source.field_key);
11052
+ const field = fieldOf2(source.table_id, source.field_key);
10918
11053
  if (field?.type === "rollup" || field?.type === "lookup") {
10919
- const link2 = fieldOf(source.table_id, field.source_field_key);
11054
+ const link2 = fieldOf2(source.table_id, field.source_field_key);
10920
11055
  if (link2?.type === "select_record_link") tables.add(link2.table_id);
10921
11056
  }
10922
11057
  }
@@ -10935,7 +11070,7 @@ function writesOver(tables, askedAt) {
10935
11070
  const mine = entries2.filter((entry) => (entry.settledAt === null || askedAt <= entry.settledAt) && touches(entry, tables));
10936
11071
  if (mine.length === 0) return null;
10937
11072
  return {
10938
- field: fieldOf,
11073
+ field: fieldOf2,
10939
11074
  table: (tableId) => model.tables.get(tableId),
10940
11075
  held: heldCells,
10941
11076
  member: (id) => members.get(id),
@@ -12450,14 +12585,14 @@ function comparesFigure(operator) {
12450
12585
  function nameOf(field) {
12451
12586
  return `"${field.name ?? field.key}"`;
12452
12587
  }
12453
- function conditionUnit(condition2, field, fieldOf2) {
12588
+ function conditionUnit(condition2, field, fieldOf3) {
12454
12589
  if (!comparesFigure(condition2.operator)) return { ok: true, unit: void 0 };
12455
12590
  const stated = condition2.unit_option;
12456
12591
  const read2 = figureRowSelect(field);
12457
12592
  if (read2 === void 0) {
12458
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.` };
12459
12594
  }
12460
- const select = fieldOf2(read2.key);
12595
+ const select = fieldOf3(read2.key);
12461
12596
  const options = effectiveSelectOptions(select) ?? [];
12462
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(", ")}`;
12463
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.7",
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": {