@lotics/app-sdk 0.133.16 → 0.133.18

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
@@ -8979,12 +8979,25 @@ function resolveLambdaItemFrame(frames, name) {
8979
8979
  }
8980
8980
  return void 0;
8981
8981
  }
8982
- function lambdaItemAnchor(ctx, name) {
8983
- return resolveLambdaItemFrame(ctx.lambda_items, name)?.anchor ?? null;
8982
+ function lambdaItemAnchor(ctx, name, path, seen) {
8983
+ const frame2 = resolveLambdaItemFrame(ctx.lambda_items, name);
8984
+ if (frame2?.bound === void 0) return frame2?.anchor ?? null;
8985
+ return boundValueAnchor(frame2.bound.expr, path, { ...ctx, lambda_items: frame2.bound.lambda_items }, seen);
8984
8986
  }
8985
8987
  function readRecordAnchor(source, path, ctx) {
8986
- if (source.from === "lambda_param") return lambdaItemAnchor(ctx, source.name);
8987
- return anchorForRead(source, path, ctx, /* @__PURE__ */ new Set());
8988
+ const traced = source.from === "lambda_param" ? lambdaItemAnchor(ctx, source.name, path, /* @__PURE__ */ new Set()) : anchorForRead(source, path, ctx, /* @__PURE__ */ new Set());
8989
+ return traced ?? statedAnchor(path, ctx);
8990
+ }
8991
+ function statedAnchor(path, ctx) {
8992
+ for (let i = 0; i < path.length; i++) {
8993
+ const segment = path[i];
8994
+ if (segment.at !== "field" || segment.table === void 0) continue;
8995
+ const previous = path[i - 1];
8996
+ if (previous?.at === "key" && previous.key === "display") continue;
8997
+ const table = ctx.tables.get(segment.table);
8998
+ if (table !== void 0) return { table, envelopeLen: i, hops: 0 };
8999
+ }
9000
+ return null;
8988
9001
  }
8989
9002
  function anchorForRead(source, path, ctx, seen) {
8990
9003
  switch (source.from) {
@@ -8993,7 +9006,7 @@ function anchorForRead(source, path, ctx, seen) {
8993
9006
  case "foreach_item":
8994
9007
  return resolveNamedForeachFrame(ctx.foreach_items, source.name)?.anchor ?? null;
8995
9008
  case "lambda_param":
8996
- return lambdaItemAnchor(ctx, source.name);
9009
+ return lambdaItemAnchor(ctx, source.name, path, seen);
8997
9010
  case "trigger_record":
8998
9011
  return rootAnchor(ctx.trigger_record);
8999
9012
  case "trigger_record_prev":
@@ -9049,7 +9062,7 @@ function boundValueAnchor(expression, path, ctx, seen) {
9049
9062
  }
9050
9063
  const bound = linkedSequenceAnchor(expression, ctx, new Set(seen));
9051
9064
  if (bound) {
9052
- return { table: bound.table, envelopeLen: bound.isFanOut ? 1 : 0, hops: bound.hops };
9065
+ return { table: bound.table, envelopeLen: bound.isFanOut ? 1 : bound.envelopeLen, hops: bound.hops };
9053
9066
  }
9054
9067
  if (expression.kind !== "read" || expression.source.from !== "step_output") return null;
9055
9068
  const combined = [...expression.path, ...path];
@@ -9150,7 +9163,8 @@ function linkedSequenceAnchor(expr2, ctx, seen) {
9150
9163
  let currentTable = root.table;
9151
9164
  let fanOut = false;
9152
9165
  let hops = root.hops;
9153
- for (const segment of read2.path.slice(root.envelopeLen)) {
9166
+ const hopped = read2.path.slice(root.envelopeLen);
9167
+ for (const segment of hopped) {
9154
9168
  if (segment.at !== "linked") return null;
9155
9169
  const field = ctx.field_named?.(currentTable, segment.via) ?? currentTable.fields.find((f) => f.key === segment.via);
9156
9170
  if (!field || field.type !== "select_record_link") return null;
@@ -9160,7 +9174,7 @@ function linkedSequenceAnchor(expr2, ctx, seen) {
9160
9174
  fanOut = segment.index === "each";
9161
9175
  hops++;
9162
9176
  }
9163
- return { table: currentTable, isFanOut: fanOut, hops };
9177
+ return { table: currentTable, isFanOut: fanOut, hops, envelopeLen: hopped.length > 0 ? 0 : Math.max(root.envelopeLen - read2.path.length, 0) };
9164
9178
  }
9165
9179
  function unwrapOptionalGuards(expr2) {
9166
9180
  let current = expr2;
@@ -9462,6 +9476,10 @@ function evaluateExpression(expr2, ctx) {
9462
9476
  return evaluateTemplate(expr2.parts, ctx);
9463
9477
  case "lambda":
9464
9478
  return evaluateLambda(expr2.params, expr2.body, ctx, []);
9479
+ case "let": {
9480
+ const value = evaluateExpression(expr2.value, ctx);
9481
+ return value instanceof Promise ? value.then((bound) => evaluateLetBody(expr2, bound, ctx)) : evaluateLetBody(expr2, value, ctx);
9482
+ }
9465
9483
  case "array_literal":
9466
9484
  return evaluateArrayLiteral(expr2.elements, ctx);
9467
9485
  case "object_literal":
@@ -9559,6 +9577,21 @@ function evaluateLambda(params, body, ctx, items) {
9559
9577
  };
9560
9578
  return Object.assign(call, { workers: ctx.evaluation_budget.workers });
9561
9579
  }
9580
+ function evaluateLetBody(expr2, bound, ctx) {
9581
+ const frame2 = /* @__PURE__ */ Object.create(null);
9582
+ frame2[expr2.name] = bound;
9583
+ const outer = ctx.lambda_items ?? [];
9584
+ const lambdaItems = [
9585
+ ...outer,
9586
+ { name: expr2.name, anchor: null, bound: { expr: expr2.value, lambda_items: outer } }
9587
+ ];
9588
+ return evaluateExpression(expr2.body, {
9589
+ ...ctx,
9590
+ lambda_params: [...ctx.lambda_params ?? [], frame2],
9591
+ lambda_items: lambdaItems,
9592
+ anchor_context: { ...ctx.anchor_context, lambda_items: lambdaItems }
9593
+ });
9594
+ }
9562
9595
  function evalAnchorContext(ctx) {
9563
9596
  return {
9564
9597
  tables: ctx.tables.current,
package/docs/mutations.md CHANGED
@@ -163,8 +163,8 @@ Files travel **only** via `files[]` — never through `return({ data })`, and ne
163
163
 
164
164
  ### Structured results: `return({ data })`
165
165
 
166
- A workflow body ends with a `return({ status, message, field_errors?, data? })` step (the
167
- envelope's grammar — `message` is required, `field_errors` never reaches the app — is
166
+ A workflow body ends with a `return({ status, message?, field_errors?, data? })` step (the
167
+ envelope's grammar — an `error` states its `message`, `field_errors` never reaches the app — is
168
168
  [workflows](./workflows.md#return--the-envelope)). `data` is arbitrary structured data
169
169
  (computed totals, row lists, status objects) the app reads back as `result.data`:
170
170
 
package/docs/workflows.md CHANGED
@@ -139,7 +139,7 @@ The body is a sequence of statements, each of which maps 1:1 onto a stored step.
139
139
  | LLM reasoning | `const x = await agent({ instructions, input, model?, output });` |
140
140
  | Run a declared agent | `const x = await app_agent({ alias, input, wait? });` |
141
141
  | Guard | `validate({ checks: [{ fail_when, field_key?, message }, …] });` |
142
- | End the run | `return({ status, message, field_errors?, data? });` |
142
+ | End the run | `return({ status, message?, field_errors?, data? });` |
143
143
 
144
144
  `validate` and `return` are **calls**, not JS keywords in the usual sense — `return` happens to
145
145
  be spelled with the JS `return` statement, but its argument is a single object literal and the
@@ -206,8 +206,9 @@ step, so no binding may use it.
206
206
  return({ status: "success", message: "Order created.", data: { total: subtotal } });
207
207
  ```
208
208
 
209
- - `status` is a literal `"success"` or `"error"`. `message` is **required** (any string
210
- expression). Both `field_errors` and `data` are optional.
209
+ - `status` is a literal `"success"` or `"error"`. An `error` states its `message` (any string
210
+ expression); a `success` may leave it out, and a bare `return;` is that success. Both
211
+ `field_errors` and `data` are optional.
211
212
  - `data` is what the app reads back as `result.data`; the alias's `outputs` schema is derived at
212
213
  save from its inferred type. Full contract: [mutations](./mutations.md) § "Structured results".
213
214
  - `field_errors` maps a key to a message and reaches the app on `result.field_errors`, so a
@@ -399,8 +400,8 @@ written keys (`o[k] = v` with a computed `k` is refused), and a loop item is rea
399
400
  into a `let` first. Arrays and objects stay immutable underneath; each form rebuilds the value
400
401
  and reassigns the name.
401
402
 
402
- **Spread is rejected anywhere inside a tool input**, at any nesting depth — static analysis reads
403
- those keys to extract table and file ids. Bind the merged value first, then pass the binding.
403
+ **Spread works inside a tool input's values** (`records: [{ a: 1, ...extra }]`, `records: [...rows, one]`).
404
+ At the top of a tool input each input is named, so `{ ...opts }` there is refused: write `table_id: opts.table_id`.
404
405
  Computed property keys (`{ [k]: v }`) are rejected everywhere for the same reason; to write a
405
406
  field chosen at runtime, use `field_edits` (below).
406
407
 
@@ -507,8 +508,12 @@ expected, so read that message rather than guessing.
507
508
  Everything else does not — a lambda anywhere else is rejected.
508
509
 
509
510
  - Both spellings lower to the same node: `(x) => <expr>`, `(x) => { return <expr>; }`, and
510
- `function (x) { return <expr>; }`. A multi-statement body, `async`, a generator, and a named
511
- function expression are rejected. Fold branches into one expression (a ternary).
511
+ `function (x) { return <expr>; }`. A body may also be a block of `const` declarations, `if`/`else`
512
+ and `return`, every path ending in `return <expr>;`; a block's `const` behaves as a top-level `const`
513
+ does. Inside one, a loop, `let`, a reassignment, a destructuring pattern, `switch` and `try` are rejected:
514
+ use a helper (`map`, `filter`, `find`, `reduce`) or compute the value before the callback. A declared
515
+ `function` is one `return <expr>;`, and a table check takes no block. `async`, a generator, and a
516
+ named function expression are rejected.
512
517
  - Parameters are plain identifiers, positional per item — `(item, idx) => …`; `reduce` gets
513
518
  `(acc, item, idx)`. No destructuring or defaults in a lambda parameter list.
514
519
  - A body may read the reserved roots, the names in scope, enclosing `for-of` items, and an
@@ -840,7 +845,7 @@ workflow that takes inputs and no row): the draft is walked by the engine an Act
840
845
  app's authority, with the production expression evaluator. Reads are real; writes and service sends are
841
846
  said, not made, and a document a step renders is made as a paper no listing shows. It answers `preview`
842
847
  (the calls the run would make, each with its fully-resolved input, and the reads it made) and `returned`
843
- (`{ status, message }` always, plus `data` and `field_errors` when the body returns them — what the caller
848
+ (`status` always, plus `message`, `data` and `field_errors` when the body returns them — what the caller
844
849
  receives). A draft it cannot rehearse — a step that reads what a call before it answers, a wait, a bound the
845
850
  walk reaches — answers `preview.available: false` and the reason. Nothing is written.
846
851
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lotics/app-sdk",
3
- "version": "0.133.16",
3
+ "version": "0.133.18",
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": {