@teamkeel/functions-runtime 0.453.0 → 0.454.3

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
@@ -686,9 +686,14 @@ interface UiElementInputImageCaptureApiResponse extends BaseUiInputResponse<"ui.
686
686
  type ElementDataType = {
687
687
  svg: string;
688
688
  signedAt: string;
689
- ipAddress: string;
690
689
  };
691
- type UiElementInputSignature = InputElement<ElementDataType, {}>;
690
+ declare class Signature {
691
+ readonly svg: string;
692
+ readonly signedAt: string;
693
+ constructor(data: ElementDataType);
694
+ toPngDataURL(): Promise<string>;
695
+ }
696
+ type UiElementInputSignature = InputElement<ElementDataType, {}, Signature>;
692
697
  interface UiElementInputSignatureApiResponse extends BaseUiInputResponse<"ui.input.signature", ElementDataType> {
693
698
  }
694
699
 
@@ -1684,4 +1689,4 @@ type FlowListResult = {
1684
1689
  };
1685
1690
  };
1686
1691
 
1687
- export { type BooleanArrayQueryWhereCondition, type BooleanArrayWhereCondition, type BooleanWhereCondition, type ContextAPI, type DateArrayQueryWhereCondition, type DateArrayWhereCondition, type DateQueryInput, type DateWhereCondition, Duration, type DurationString, type DurationWhereCondition, ErrorPresets, type Errors, type ExtractStageKeys, File, type FlowConfig, type FlowConfigAPI, type FlowContext, type FlowFunction, type FlowListOptions, type FlowListResult, type FlowRun, type FlowRunStatus, type FlowRunStep, FlowsAPI, type FuncWithConfig, type FunctionConfig, type Hardware, type IDWhereCondition, InlineFile, type ListResult, ModelAPI, NonRetriableError, type NullableHardware, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, type PartialPageInfo, Permissions, type Printer, type RelativeDateString, RequestHeaders, type Response, RetryBackoffExponential, RetryBackoffLinear, RetryConstant, STEP_STATUS, STEP_TYPE, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type Task, TaskAPI, type TaskCreateOptions, type TaskFlowFunction, type TaskStatus, type TimestampQueryInput, type TraceAPI, type TraceAttributeValue, type TraceAttributes, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, createTraceAPI, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
1692
+ export { type BooleanArrayQueryWhereCondition, type BooleanArrayWhereCondition, type BooleanWhereCondition, type ContextAPI, type DateArrayQueryWhereCondition, type DateArrayWhereCondition, type DateQueryInput, type DateWhereCondition, Duration, type DurationString, type DurationWhereCondition, ErrorPresets, type Errors, type ExtractStageKeys, File, type FlowConfig, type FlowConfigAPI, type FlowContext, type FlowFunction, type FlowListOptions, type FlowListResult, type FlowRun, type FlowRunStatus, type FlowRunStep, FlowsAPI, type FuncWithConfig, type FunctionConfig, type Hardware, type IDWhereCondition, InlineFile, type ListResult, ModelAPI, NonRetriableError, type NullableHardware, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, type PartialPageInfo, Permissions, type Printer, type RelativeDateString, RequestHeaders, type Response, RetryBackoffExponential, RetryBackoffLinear, RetryConstant, STEP_STATUS, STEP_TYPE, Signature, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type Task, TaskAPI, type TaskCreateOptions, type TaskFlowFunction, type TaskStatus, type TimestampQueryInput, type TraceAPI, type TraceAttributeValue, type TraceAttributes, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, createTraceAPI, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
package/dist/index.d.ts CHANGED
@@ -686,9 +686,14 @@ interface UiElementInputImageCaptureApiResponse extends BaseUiInputResponse<"ui.
686
686
  type ElementDataType = {
687
687
  svg: string;
688
688
  signedAt: string;
689
- ipAddress: string;
690
689
  };
691
- type UiElementInputSignature = InputElement<ElementDataType, {}>;
690
+ declare class Signature {
691
+ readonly svg: string;
692
+ readonly signedAt: string;
693
+ constructor(data: ElementDataType);
694
+ toPngDataURL(): Promise<string>;
695
+ }
696
+ type UiElementInputSignature = InputElement<ElementDataType, {}, Signature>;
692
697
  interface UiElementInputSignatureApiResponse extends BaseUiInputResponse<"ui.input.signature", ElementDataType> {
693
698
  }
694
699
 
@@ -1684,4 +1689,4 @@ type FlowListResult = {
1684
1689
  };
1685
1690
  };
1686
1691
 
1687
- export { type BooleanArrayQueryWhereCondition, type BooleanArrayWhereCondition, type BooleanWhereCondition, type ContextAPI, type DateArrayQueryWhereCondition, type DateArrayWhereCondition, type DateQueryInput, type DateWhereCondition, Duration, type DurationString, type DurationWhereCondition, ErrorPresets, type Errors, type ExtractStageKeys, File, type FlowConfig, type FlowConfigAPI, type FlowContext, type FlowFunction, type FlowListOptions, type FlowListResult, type FlowRun, type FlowRunStatus, type FlowRunStep, FlowsAPI, type FuncWithConfig, type FunctionConfig, type Hardware, type IDWhereCondition, InlineFile, type ListResult, ModelAPI, NonRetriableError, type NullableHardware, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, type PartialPageInfo, Permissions, type Printer, type RelativeDateString, RequestHeaders, type Response, RetryBackoffExponential, RetryBackoffLinear, RetryConstant, STEP_STATUS, STEP_TYPE, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type Task, TaskAPI, type TaskCreateOptions, type TaskFlowFunction, type TaskStatus, type TimestampQueryInput, type TraceAPI, type TraceAttributeValue, type TraceAttributes, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, createTraceAPI, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
1692
+ export { type BooleanArrayQueryWhereCondition, type BooleanArrayWhereCondition, type BooleanWhereCondition, type ContextAPI, type DateArrayQueryWhereCondition, type DateArrayWhereCondition, type DateQueryInput, type DateWhereCondition, Duration, type DurationString, type DurationWhereCondition, ErrorPresets, type Errors, type ExtractStageKeys, File, type FlowConfig, type FlowConfigAPI, type FlowContext, type FlowFunction, type FlowListOptions, type FlowListResult, type FlowRun, type FlowRunStatus, type FlowRunStep, FlowsAPI, type FuncWithConfig, type FunctionConfig, type Hardware, type IDWhereCondition, InlineFile, type ListResult, ModelAPI, NonRetriableError, type NullableHardware, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, type PartialPageInfo, Permissions, type Printer, type RelativeDateString, RequestHeaders, type Response, RetryBackoffExponential, RetryBackoffLinear, RetryConstant, STEP_STATUS, STEP_TYPE, Signature, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type Task, TaskAPI, type TaskCreateOptions, type TaskFlowFunction, type TaskStatus, type TimestampQueryInput, type TraceAPI, type TraceAttributeValue, type TraceAttributes, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, createTraceAPI, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
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,28 +3286,46 @@ 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");
3310
3297
 
3298
+ // src/flows/ui/elements/input/signature.ts
3299
+ var Signature = class {
3300
+ static {
3301
+ __name(this, "Signature");
3302
+ }
3303
+ constructor(data) {
3304
+ this.svg = data.svg;
3305
+ this.signedAt = data.signedAt;
3306
+ }
3307
+ async toPngDataURL() {
3308
+ const sharp = (await import("sharp")).default;
3309
+ const png = await sharp(Buffer.from(this.svg), { density: 300 }).png().toBuffer();
3310
+ return `data:image/png;base64,${png.toString("base64")}`;
3311
+ }
3312
+ };
3313
+ var signatureInput = /* @__PURE__ */ __name((name, options) => {
3314
+ return {
3315
+ __type: "input",
3316
+ uiConfig: {
3317
+ __type: "ui.input.signature",
3318
+ name,
3319
+ label: options?.label || name,
3320
+ optional: options?.optional || false,
3321
+ disabled: options?.disabled || false,
3322
+ helpText: options?.helpText
3323
+ },
3324
+ validate: options?.validate,
3325
+ getData: /* @__PURE__ */ __name((x) => new Signature(x), "getData")
3326
+ };
3327
+ }, "signatureInput");
3328
+
3311
3329
  // src/flows/ui/elements/input/text.ts
3312
3330
  var textInput = /* @__PURE__ */ __name((name, options) => {
3313
3331
  return {
@@ -3439,6 +3457,28 @@ var selectOne = /* @__PURE__ */ __name((name, options) => {
3439
3457
  }, "selectOne");
3440
3458
 
3441
3459
  // src/flows/ui/page.ts
3460
+ async function applyElementGetData(content, data) {
3461
+ if (!data || typeof data !== "object" || !Array.isArray(content)) return data;
3462
+ for (const c of content) {
3463
+ const resolved = await c;
3464
+ if (!resolved || !("__type" in resolved)) continue;
3465
+ const name = resolved.uiConfig?.name;
3466
+ if (!name || !(name in data)) continue;
3467
+ if (resolved.__type === "input" && typeof resolved.getData === "function") {
3468
+ data[name] = resolved.getData(data[name]);
3469
+ } else if (resolved.__type === "iterator") {
3470
+ const rows = data[name];
3471
+ const inner = resolved.uiConfig?.content;
3472
+ if (Array.isArray(rows) && Array.isArray(inner)) {
3473
+ for (let i = 0; i < rows.length; i++) {
3474
+ rows[i] = await applyElementGetData(inner, rows[i]);
3475
+ }
3476
+ }
3477
+ }
3478
+ }
3479
+ return data;
3480
+ }
3481
+ __name(applyElementGetData, "applyElementGetData");
3442
3482
  async function callbackFn(elements, elementName, callbackName, data) {
3443
3483
  const element = elements.find(
3444
3484
  (el) => el.uiConfig && el.uiConfig.name === elementName
@@ -3976,23 +4016,6 @@ var imageCapture = /* @__PURE__ */ __name((name, options) => {
3976
4016
  };
3977
4017
  }, "imageCapture");
3978
4018
 
3979
- // src/flows/ui/elements/input/signature.ts
3980
- var signatureInput = /* @__PURE__ */ __name((name, options) => {
3981
- return {
3982
- __type: "input",
3983
- uiConfig: {
3984
- __type: "ui.input.signature",
3985
- name,
3986
- label: options?.label || name,
3987
- optional: options?.optional || false,
3988
- disabled: options?.disabled || false,
3989
- helpText: options?.helpText
3990
- },
3991
- validate: options?.validate,
3992
- getData: /* @__PURE__ */ __name((x) => x, "getData")
3993
- };
3994
- }, "signatureInput");
3995
-
3996
4019
  // src/flows/ui/elements/iterator.ts
3997
4020
  var iterator = /* @__PURE__ */ __name((name, options) => {
3998
4021
  return {
@@ -4331,25 +4354,35 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
4331
4354
  throw new Error(`Duplicate step name: ${name}`);
4332
4355
  }
4333
4356
  usedNames.add(name);
4334
- let step = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
4357
+ const { step, inserted } = await db.transaction().execute(async (trx) => {
4358
+ await trx.selectFrom("keel.flow_run").select("id").where("id", "=", runId).forUpdate().executeTakeFirst();
4359
+ const existing = await trx.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
4360
+ if (existing) {
4361
+ return { step: existing, inserted: false };
4362
+ }
4363
+ const created = await trx.insertInto("keel.flow_step").values({
4364
+ run_id: runId,
4365
+ name,
4366
+ stage: options.stage,
4367
+ status: "PENDING" /* PENDING */,
4368
+ type: "UI" /* UI */,
4369
+ startTime: /* @__PURE__ */ new Date()
4370
+ }).returningAll().executeTakeFirstOrThrow();
4371
+ return { step: created, inserted: true };
4372
+ });
4335
4373
  if (step && step.status === "COMPLETED" /* COMPLETED */) {
4336
4374
  span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
4337
4375
  span.setAttribute("step.status", "COMPLETED" /* COMPLETED */);
4338
- const parsedData2 = transformRichDataTypes(step.value);
4376
+ const parsedData2 = await applyElementGetData(
4377
+ options.content,
4378
+ transformRichDataTypes(step.value)
4379
+ );
4339
4380
  if (step.action) {
4340
4381
  return { data: parsedData2, action: step.action };
4341
4382
  }
4342
4383
  return parsedData2;
4343
4384
  }
4344
- if (!step) {
4345
- step = await db.insertInto("keel.flow_step").values({
4346
- run_id: runId,
4347
- name,
4348
- stage: options.stage,
4349
- status: "PENDING" /* PENDING */,
4350
- type: "UI" /* UI */,
4351
- startTime: /* @__PURE__ */ new Date()
4352
- }).returningAll().executeTakeFirst();
4385
+ if (inserted) {
4353
4386
  span.setAttribute("rendered", true);
4354
4387
  span.setAttribute("step.status", "PENDING" /* PENDING */);
4355
4388
  throw new UIRenderDisrupt(
@@ -4412,7 +4445,10 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
4412
4445
  endTime: /* @__PURE__ */ new Date()
4413
4446
  }).where("id", "=", step.id).returningAll().executeTakeFirst();
4414
4447
  span.setAttribute("step.status", "COMPLETED" /* COMPLETED */);
4415
- const parsedData = transformRichDataTypes(data);
4448
+ const parsedData = await applyElementGetData(
4449
+ options.content,
4450
+ transformRichDataTypes(data)
4451
+ );
4416
4452
  if (action) {
4417
4453
  return { data: parsedData, action };
4418
4454
  }
@@ -4684,6 +4720,7 @@ export {
4684
4720
  RetryConstant,
4685
4721
  STEP_STATUS,
4686
4722
  STEP_TYPE,
4723
+ Signature,
4687
4724
  TaskAPI,
4688
4725
  checkBuiltInPermissions,
4689
4726
  createFlowContext,