@teamkeel/functions-runtime 0.454.2 → 0.455.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.d.cts CHANGED
@@ -1124,7 +1124,7 @@ interface UiElementPickListApiResponse extends BaseUiMinimalInputResponse<"ui.in
1124
1124
  autoContinue: boolean;
1125
1125
  }
1126
1126
 
1127
- type UiElementScan = <N extends string, const M extends scanMode = "single", const D extends ScanDuplicateMode = "none">(name: N, options?: ScanOptions<M, D>) => InputElementResponse<N, M extends "single" ? string : ScanResponseItem<D>[]>;
1127
+ type UiElementScan = <N extends string, const M extends scanMode = "single", const D extends ScanDuplicateMode = "none", R = string | null>(name: N, options?: ScanOptions<M, D, R>) => InputElementResponse<N, M extends "single" ? string : ScanResponseItem<D>[]>;
1128
1128
  type ScanResponseItem<D> = D extends "trackQuantity" ? {
1129
1129
  value: string;
1130
1130
  quantity: number;
@@ -1141,7 +1141,7 @@ type ScanDuplicateMode =
1141
1141
  /** Reject duplicate scans with an error message */
1142
1142
  | "rejectDuplicates";
1143
1143
  type scanMode = "single" | "multi";
1144
- type ScanOptions<M, D> = {
1144
+ type ScanOptions<M, D, R = string | null> = {
1145
1145
  /** The title of the input block
1146
1146
  * @default "Scan {unit plural | 'items'}"
1147
1147
  */
@@ -1154,6 +1154,13 @@ type ScanOptions<M, D> = {
1154
1154
  */
1155
1155
  mode: M | scanMode;
1156
1156
  validate?: ValidateFn<ScanResponseItem<D>>;
1157
+ /** Converts a scanned value into the value this element should return.
1158
+ * Use it to turn a raw barcode into the data your flow actually wants —
1159
+ * e.g. a product name, an internal ID, or a full record. Defaults to
1160
+ * `string | null`; return a richer JSON-serializable shape to let
1161
+ * TypeScript infer it.
1162
+ */
1163
+ lookup?: (value: string) => R | Promise<R>;
1157
1164
  } & (M extends "multi" ? {
1158
1165
  max?: number;
1159
1166
  min?: number;
package/dist/index.d.ts CHANGED
@@ -1124,7 +1124,7 @@ interface UiElementPickListApiResponse extends BaseUiMinimalInputResponse<"ui.in
1124
1124
  autoContinue: boolean;
1125
1125
  }
1126
1126
 
1127
- type UiElementScan = <N extends string, const M extends scanMode = "single", const D extends ScanDuplicateMode = "none">(name: N, options?: ScanOptions<M, D>) => InputElementResponse<N, M extends "single" ? string : ScanResponseItem<D>[]>;
1127
+ type UiElementScan = <N extends string, const M extends scanMode = "single", const D extends ScanDuplicateMode = "none", R = string | null>(name: N, options?: ScanOptions<M, D, R>) => InputElementResponse<N, M extends "single" ? string : ScanResponseItem<D>[]>;
1128
1128
  type ScanResponseItem<D> = D extends "trackQuantity" ? {
1129
1129
  value: string;
1130
1130
  quantity: number;
@@ -1141,7 +1141,7 @@ type ScanDuplicateMode =
1141
1141
  /** Reject duplicate scans with an error message */
1142
1142
  | "rejectDuplicates";
1143
1143
  type scanMode = "single" | "multi";
1144
- type ScanOptions<M, D> = {
1144
+ type ScanOptions<M, D, R = string | null> = {
1145
1145
  /** The title of the input block
1146
1146
  * @default "Scan {unit plural | 'items'}"
1147
1147
  */
@@ -1154,6 +1154,13 @@ type ScanOptions<M, D> = {
1154
1154
  */
1155
1155
  mode: M | scanMode;
1156
1156
  validate?: ValidateFn<ScanResponseItem<D>>;
1157
+ /** Converts a scanned value into the value this element should return.
1158
+ * Use it to turn a raw barcode into the data your flow actually wants —
1159
+ * e.g. a product name, an internal ID, or a full record. Defaults to
1160
+ * `string | null`; return a richer JSON-serializable shape to let
1161
+ * TypeScript infer it.
1162
+ */
1163
+ lookup?: (value: string) => R | Promise<R>;
1157
1164
  } & (M extends "multi" ? {
1158
1165
  max?: number;
1159
1166
  min?: number;
package/dist/index.js CHANGED
@@ -530,9 +530,9 @@ var InstrumentedClient = class extends Client {
530
530
  }
531
531
  async query(...args) {
532
532
  const _super = super.query.bind(this);
533
- const sql5 = args[0];
533
+ const sql4 = args[0];
534
534
  let sqlAttribute = false;
535
- let spanName = txStatements[sql5.toLowerCase()];
535
+ let spanName = txStatements[sql4.toLowerCase()];
536
536
  if (!spanName) {
537
537
  spanName = "Database Query";
538
538
  sqlAttribute = true;
@@ -600,9 +600,9 @@ function getDialect(connString) {
600
600
  pool.on("connect", (client) => {
601
601
  const originalQuery = client.query;
602
602
  client.query = function(...args) {
603
- const sql5 = args[0];
603
+ const sql4 = args[0];
604
604
  let sqlAttribute = false;
605
- let spanName = txStatements[sql5.toLowerCase()];
605
+ let spanName = txStatements[sql4.toLowerCase()];
606
606
  if (!spanName) {
607
607
  spanName = "Database Query";
608
608
  sqlAttribute = true;
@@ -1097,23 +1097,23 @@ var TimePeriod = class _TimePeriod {
1097
1097
  return new _TimePeriod(period, value, offset, complete2);
1098
1098
  }
1099
1099
  periodStartSQL() {
1100
- let sql5 = "NOW()";
1100
+ let sql4 = "NOW()";
1101
1101
  if (this.offset !== 0) {
1102
- sql5 = `${sql5} + INTERVAL '${this.offset} ${this.period}'`;
1102
+ sql4 = `${sql4} + INTERVAL '${this.offset} ${this.period}'`;
1103
1103
  }
1104
1104
  if (this.complete) {
1105
- sql5 = `DATE_TRUNC('${this.period}', ${sql5})`;
1105
+ sql4 = `DATE_TRUNC('${this.period}', ${sql4})`;
1106
1106
  } else {
1107
- sql5 = `(${sql5})`;
1107
+ sql4 = `(${sql4})`;
1108
1108
  }
1109
- return sql5;
1109
+ return sql4;
1110
1110
  }
1111
1111
  periodEndSQL() {
1112
- let sql5 = this.periodStartSQL();
1112
+ let sql4 = this.periodStartSQL();
1113
1113
  if (this.value != 0) {
1114
- sql5 = `(${sql5} + INTERVAL '${this.value} ${this.period}')`;
1114
+ sql4 = `(${sql4} + INTERVAL '${this.value} ${this.period}')`;
1115
1115
  }
1116
- return sql5;
1116
+ return sql4;
1117
1117
  }
1118
1118
  };
1119
1119
 
@@ -3286,24 +3286,11 @@ import {
3286
3286
  import * as opentelemetry6 from "@opentelemetry/api";
3287
3287
 
3288
3288
  // src/tryExecuteFlow.js
3289
- import { sql as sql4 } from "kysely";
3290
3289
  function tryExecuteFlow(db, request, cb) {
3291
- return withDatabase(db, false, async ({ sDb }) => {
3292
- const runId = request?.meta?.runId;
3293
- if (runId && sDb) {
3294
- await sql4`SELECT pg_advisory_lock(hashtextextended(${runId}, 0))`.execute(
3295
- sDb
3296
- );
3297
- }
3298
- try {
3299
- return await withAuditContext(request, async () => {
3300
- return cb();
3301
- });
3302
- } finally {
3303
- if (runId && sDb) {
3304
- await sql4`SELECT pg_advisory_unlock(hashtextextended(${runId}, 0))`.execute(sDb).catch(() => void 0);
3305
- }
3306
- }
3290
+ return withDatabase(db, false, async () => {
3291
+ return withAuditContext(request, async () => {
3292
+ return cb();
3293
+ });
3307
3294
  });
3308
3295
  }
3309
3296
  __name(tryExecuteFlow, "tryExecuteFlow");
@@ -4158,7 +4145,8 @@ var scan = /* @__PURE__ */ __name((name, options) => {
4158
4145
  validate: /* @__PURE__ */ __name(async (data, action) => {
4159
4146
  return options?.validate?.(data, action) ?? true;
4160
4147
  }, "validate"),
4161
- getData: /* @__PURE__ */ __name((x) => x, "getData")
4148
+ getData: /* @__PURE__ */ __name((x) => x, "getData"),
4149
+ lookup: options?.lookup
4162
4150
  };
4163
4151
  }, "scan");
4164
4152
 
@@ -4367,7 +4355,22 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
4367
4355
  throw new Error(`Duplicate step name: ${name}`);
4368
4356
  }
4369
4357
  usedNames.add(name);
4370
- let step = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
4358
+ const { step, inserted } = await db.transaction().execute(async (trx) => {
4359
+ await trx.selectFrom("keel.flow_run").select("id").where("id", "=", runId).forUpdate().executeTakeFirst();
4360
+ const existing = await trx.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
4361
+ if (existing) {
4362
+ return { step: existing, inserted: false };
4363
+ }
4364
+ const created = await trx.insertInto("keel.flow_step").values({
4365
+ run_id: runId,
4366
+ name,
4367
+ stage: options.stage,
4368
+ status: "PENDING" /* PENDING */,
4369
+ type: "UI" /* UI */,
4370
+ startTime: /* @__PURE__ */ new Date()
4371
+ }).returningAll().executeTakeFirstOrThrow();
4372
+ return { step: created, inserted: true };
4373
+ });
4371
4374
  if (step && step.status === "COMPLETED" /* COMPLETED */) {
4372
4375
  span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
4373
4376
  span.setAttribute("step.status", "COMPLETED" /* COMPLETED */);
@@ -4380,15 +4383,7 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
4380
4383
  }
4381
4384
  return parsedData2;
4382
4385
  }
4383
- if (!step) {
4384
- step = await db.insertInto("keel.flow_step").values({
4385
- run_id: runId,
4386
- name,
4387
- stage: options.stage,
4388
- status: "PENDING" /* PENDING */,
4389
- type: "UI" /* UI */,
4390
- startTime: /* @__PURE__ */ new Date()
4391
- }).returningAll().executeTakeFirst();
4386
+ if (inserted) {
4392
4387
  span.setAttribute("rendered", true);
4393
4388
  span.setAttribute("step.status", "PENDING" /* PENDING */);
4394
4389
  throw new UIRenderDisrupt(