@teamkeel/functions-runtime 0.414.1 → 0.414.2

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
@@ -546,12 +546,36 @@ type UIApiResponses = {
546
546
  };
547
547
  };
548
548
 
549
- interface FlowContext<C extends FlowConfig, E = any, S = any> {
549
+ type CompleteOptions<C extends FlowConfig> = {
550
+ stage?: ExtractStageKeys<C>;
551
+ title?: string;
552
+ description?: string;
553
+ content: DisplayElementResponse[];
554
+ data?: any;
555
+ };
556
+ type Complete<C extends FlowConfig> = (options: CompleteOptions<C>) => CompleteOptions<C>;
557
+
558
+ declare const enum STEP_STATUS {
559
+ NEW = "NEW",
560
+ RUNNING = "RUNNING",
561
+ PENDING = "PENDING",
562
+ COMPLETED = "COMPLETED",
563
+ FAILED = "FAILED"
564
+ }
565
+ declare const enum STEP_TYPE {
566
+ FUNCTION = "FUNCTION",
567
+ UI = "UI",
568
+ DELAY = "DELAY",
569
+ COMPLETE = "COMPLETE"
570
+ }
571
+ interface FlowContext<C extends FlowConfig, E = any, S = any, Id = any> {
550
572
  step: Step<C>;
551
573
  ui: UI<C>;
574
+ complete: Complete<C>;
552
575
  env: E;
553
576
  now: Date;
554
577
  secrets: S;
578
+ identity: Id;
555
579
  }
556
580
  type JsonSerializable = string | number | boolean | null | JsonSerializable[] | {
557
581
  [key: string]: JsonSerializable;
@@ -590,7 +614,7 @@ interface FlowConfigAPI {
590
614
  title: string;
591
615
  description?: string;
592
616
  }
593
- type FlowFunction<C extends FlowConfig, E extends any = {}, S extends any = {}, I extends any = {}> = (ctx: FlowContext<C, E, S>, inputs: I) => Promise<void>;
617
+ type FlowFunction<C extends FlowConfig, E extends any = {}, S extends any = {}, Id extends any = {}, I extends any = {}> = (ctx: FlowContext<C, E, S, Id>, inputs: I) => Promise<CompleteOptions<C> | any | void>;
594
618
  type ExtractStageKeys<T extends FlowConfig> = T extends {
595
619
  stages: infer S;
596
620
  } ? S extends ReadonlyArray<infer U> ? U extends string ? U : U extends {
@@ -607,11 +631,12 @@ type StageConfigObject = {
607
631
  initiallyHidden?: boolean;
608
632
  };
609
633
  type StageConfig = string | StageConfigObject;
610
- declare function createFlowContext<C extends FlowConfig, E = any, S = any>(runId: string, data: any, action: string | null, spanId: string, ctx: {
634
+ declare function createFlowContext<C extends FlowConfig, E = any, S = any, I = any>(runId: string, data: any, action: string | null, spanId: string, ctx: {
611
635
  env: E;
612
636
  now: Date;
613
637
  secrets: S;
614
- }): FlowContext<C, E, S>;
638
+ identity: I;
639
+ }): FlowContext<C, E, S, I>;
615
640
 
616
641
  declare function ksuid(): string;
617
642
 
@@ -781,4 +806,4 @@ type dateDuration = `${number}Y${number}M${number}D` | `${number}Y${number}M` |
781
806
  type timeDuration = `${number}H${number}M${number}S` | `${number}H${number}M` | `${number}M${number}S` | `${number}H${number}S` | `${number}H` | `${number}M` | `${number}S`;
782
807
  type DurationString = `P${dateDuration}T${timeDuration}` | `P${dateDuration}` | `PT${timeDuration}`;
783
808
 
784
- 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 FuncWithConfig, type FunctionConfig, type IDWhereCondition, InlineFile, ModelAPI, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, Permissions, type RelativeDateString, RequestHeaders, type Response, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type TimestampQueryInput, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
809
+ 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 FuncWithConfig, type FunctionConfig, type IDWhereCondition, InlineFile, ModelAPI, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, Permissions, type RelativeDateString, RequestHeaders, type Response, STEP_STATUS, STEP_TYPE, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type TimestampQueryInput, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
package/dist/index.d.ts CHANGED
@@ -546,12 +546,36 @@ type UIApiResponses = {
546
546
  };
547
547
  };
548
548
 
549
- interface FlowContext<C extends FlowConfig, E = any, S = any> {
549
+ type CompleteOptions<C extends FlowConfig> = {
550
+ stage?: ExtractStageKeys<C>;
551
+ title?: string;
552
+ description?: string;
553
+ content: DisplayElementResponse[];
554
+ data?: any;
555
+ };
556
+ type Complete<C extends FlowConfig> = (options: CompleteOptions<C>) => CompleteOptions<C>;
557
+
558
+ declare const enum STEP_STATUS {
559
+ NEW = "NEW",
560
+ RUNNING = "RUNNING",
561
+ PENDING = "PENDING",
562
+ COMPLETED = "COMPLETED",
563
+ FAILED = "FAILED"
564
+ }
565
+ declare const enum STEP_TYPE {
566
+ FUNCTION = "FUNCTION",
567
+ UI = "UI",
568
+ DELAY = "DELAY",
569
+ COMPLETE = "COMPLETE"
570
+ }
571
+ interface FlowContext<C extends FlowConfig, E = any, S = any, Id = any> {
550
572
  step: Step<C>;
551
573
  ui: UI<C>;
574
+ complete: Complete<C>;
552
575
  env: E;
553
576
  now: Date;
554
577
  secrets: S;
578
+ identity: Id;
555
579
  }
556
580
  type JsonSerializable = string | number | boolean | null | JsonSerializable[] | {
557
581
  [key: string]: JsonSerializable;
@@ -590,7 +614,7 @@ interface FlowConfigAPI {
590
614
  title: string;
591
615
  description?: string;
592
616
  }
593
- type FlowFunction<C extends FlowConfig, E extends any = {}, S extends any = {}, I extends any = {}> = (ctx: FlowContext<C, E, S>, inputs: I) => Promise<void>;
617
+ type FlowFunction<C extends FlowConfig, E extends any = {}, S extends any = {}, Id extends any = {}, I extends any = {}> = (ctx: FlowContext<C, E, S, Id>, inputs: I) => Promise<CompleteOptions<C> | any | void>;
594
618
  type ExtractStageKeys<T extends FlowConfig> = T extends {
595
619
  stages: infer S;
596
620
  } ? S extends ReadonlyArray<infer U> ? U extends string ? U : U extends {
@@ -607,11 +631,12 @@ type StageConfigObject = {
607
631
  initiallyHidden?: boolean;
608
632
  };
609
633
  type StageConfig = string | StageConfigObject;
610
- declare function createFlowContext<C extends FlowConfig, E = any, S = any>(runId: string, data: any, action: string | null, spanId: string, ctx: {
634
+ declare function createFlowContext<C extends FlowConfig, E = any, S = any, I = any>(runId: string, data: any, action: string | null, spanId: string, ctx: {
611
635
  env: E;
612
636
  now: Date;
613
637
  secrets: S;
614
- }): FlowContext<C, E, S>;
638
+ identity: I;
639
+ }): FlowContext<C, E, S, I>;
615
640
 
616
641
  declare function ksuid(): string;
617
642
 
@@ -781,4 +806,4 @@ type dateDuration = `${number}Y${number}M${number}D` | `${number}Y${number}M` |
781
806
  type timeDuration = `${number}H${number}M${number}S` | `${number}H${number}M` | `${number}M${number}S` | `${number}H${number}S` | `${number}H` | `${number}M` | `${number}S`;
782
807
  type DurationString = `P${dateDuration}T${timeDuration}` | `P${dateDuration}` | `PT${timeDuration}`;
783
808
 
784
- 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 FuncWithConfig, type FunctionConfig, type IDWhereCondition, InlineFile, ModelAPI, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, Permissions, type RelativeDateString, RequestHeaders, type Response, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type TimestampQueryInput, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
809
+ 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 FuncWithConfig, type FunctionConfig, type IDWhereCondition, InlineFile, ModelAPI, type NumberArrayQueryWhereCondition, type NumberArrayWhereCondition, type NumberWhereCondition, PERMISSION_STATE, type PageInfo, Permissions, type RelativeDateString, RequestHeaders, type Response, STEP_STATUS, STEP_TYPE, type SortDirection, type Step, type StringArrayQueryWhereCondition, type StringArrayWhereCondition, type StringWhereCondition, type TimestampQueryInput, type UI, type UIApiResponses, checkBuiltInPermissions, createFlowContext, handleFlow, handleJob, handleRequest, handleRoute, handleSubscriber, ksuid, tracing, useDatabase };
package/dist/index.js CHANGED
@@ -1075,11 +1075,11 @@ var TimePeriod = class _TimePeriod {
1075
1075
  static {
1076
1076
  __name(this, "TimePeriod");
1077
1077
  }
1078
- constructor(period = "", value = 0, offset = 0, complete = false) {
1078
+ constructor(period = "", value = 0, offset = 0, complete2 = false) {
1079
1079
  this.period = period;
1080
1080
  this.value = value;
1081
1081
  this.offset = offset;
1082
- this.complete = complete;
1082
+ this.complete = complete2;
1083
1083
  }
1084
1084
  static fromExpression(expression) {
1085
1085
  const pattern = /^(this|next|last)?\s*(\d+)?\s*(complete)?\s*(second|minute|hour|day|week|month|year|seconds|minutes|hours|days|weeks|months|years)?$/i;
@@ -1105,15 +1105,15 @@ var TimePeriod = class _TimePeriod {
1105
1105
  const [, direction, rawValue, isComplete, rawPeriod] = match;
1106
1106
  let period = rawPeriod ? rawPeriod.toLowerCase().replace(/s$/, "") : "";
1107
1107
  let value = rawValue ? parseInt(rawValue, 10) : 1;
1108
- let complete = Boolean(isComplete);
1108
+ let complete2 = Boolean(isComplete);
1109
1109
  let offset = 0;
1110
1110
  switch (direction?.toLowerCase()) {
1111
1111
  case "this":
1112
1112
  offset = 0;
1113
- complete = true;
1113
+ complete2 = true;
1114
1114
  break;
1115
1115
  case "next":
1116
- offset = complete ? 1 : 0;
1116
+ offset = complete2 ? 1 : 0;
1117
1117
  break;
1118
1118
  case "last":
1119
1119
  offset = -value;
@@ -1123,7 +1123,7 @@ var TimePeriod = class _TimePeriod {
1123
1123
  "Time period expression must start with this, next, or last"
1124
1124
  );
1125
1125
  }
1126
- return new _TimePeriod(period, value, offset, complete);
1126
+ return new _TimePeriod(period, value, offset, complete2);
1127
1127
  }
1128
1128
  periodStartSQL() {
1129
1129
  let sql4 = "NOW()";
@@ -2596,6 +2596,21 @@ var header = /* @__PURE__ */ __name((options) => {
2596
2596
  }, "header");
2597
2597
 
2598
2598
  // src/flows/index.ts
2599
+ var STEP_STATUS = /* @__PURE__ */ ((STEP_STATUS2) => {
2600
+ STEP_STATUS2["NEW"] = "NEW";
2601
+ STEP_STATUS2["RUNNING"] = "RUNNING";
2602
+ STEP_STATUS2["PENDING"] = "PENDING";
2603
+ STEP_STATUS2["COMPLETED"] = "COMPLETED";
2604
+ STEP_STATUS2["FAILED"] = "FAILED";
2605
+ return STEP_STATUS2;
2606
+ })(STEP_STATUS || {});
2607
+ var STEP_TYPE = /* @__PURE__ */ ((STEP_TYPE2) => {
2608
+ STEP_TYPE2["FUNCTION"] = "FUNCTION";
2609
+ STEP_TYPE2["UI"] = "UI";
2610
+ STEP_TYPE2["DELAY"] = "DELAY";
2611
+ STEP_TYPE2["COMPLETE"] = "COMPLETE";
2612
+ return STEP_TYPE2;
2613
+ })(STEP_TYPE || {});
2599
2614
  var defaultOpts = {
2600
2615
  retries: 5,
2601
2616
  timeout: 6e4
@@ -2603,9 +2618,11 @@ var defaultOpts = {
2603
2618
  function createFlowContext(runId, data, action, spanId, ctx) {
2604
2619
  const usedNames = /* @__PURE__ */ new Set();
2605
2620
  return {
2621
+ identity: ctx.identity,
2606
2622
  env: ctx.env,
2607
2623
  now: ctx.now,
2608
2624
  secrets: ctx.secrets,
2625
+ complete: /* @__PURE__ */ __name((options) => options, "complete"),
2609
2626
  step: /* @__PURE__ */ __name(async (name, optionsOrFn, fn) => {
2610
2627
  const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
2611
2628
  const actualFn = typeof optionsOrFn === "function" ? optionsOrFn : fn;
@@ -2786,6 +2803,26 @@ function withTimeout(promiseFn, timeout) {
2786
2803
  }
2787
2804
  __name(withTimeout, "withTimeout");
2788
2805
 
2806
+ // src/flows/ui/complete.ts
2807
+ async function complete(options) {
2808
+ const content = options.content;
2809
+ const contentUiConfig = await Promise.all(
2810
+ content.map(async (c) => {
2811
+ return c.uiConfig;
2812
+ })
2813
+ );
2814
+ return {
2815
+ complete: {
2816
+ __type: "ui.complete",
2817
+ stage: options.stage,
2818
+ title: options.title,
2819
+ description: options.description,
2820
+ content: contentUiConfig
2821
+ }
2822
+ };
2823
+ }
2824
+ __name(complete, "complete");
2825
+
2789
2826
  // src/handleFlow.ts
2790
2827
  import { sentenceCase } from "change-case";
2791
2828
  async function handleFlow(request, config) {
@@ -2843,8 +2880,9 @@ async function handleFlow(request, config) {
2843
2880
  })
2844
2881
  };
2845
2882
  const inputs = parseInputs(request.meta?.inputs);
2883
+ let response = void 0;
2846
2884
  try {
2847
- await tryExecuteFlow(db, async () => {
2885
+ response = await tryExecuteFlow(db, async () => {
2848
2886
  return flowFunction(ctx, inputs);
2849
2887
  });
2850
2888
  } catch (e) {
@@ -2883,10 +2921,32 @@ async function handleFlow(request, config) {
2883
2921
  config: flowConfig
2884
2922
  });
2885
2923
  }
2924
+ let ui = null;
2925
+ let data = null;
2926
+ if (response && typeof response == "object" && "content" in response) {
2927
+ const completeStep = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("type", "=", "COMPLETE" /* COMPLETE */).selectAll().executeTakeFirst();
2928
+ if (!completeStep) {
2929
+ await db.insertInto("keel.flow_step").values({
2930
+ run_id: runId,
2931
+ name: "",
2932
+ stage: response.stage,
2933
+ status: "COMPLETED" /* COMPLETED */,
2934
+ type: "COMPLETE" /* COMPLETE */,
2935
+ startTime: /* @__PURE__ */ new Date(),
2936
+ endTime: /* @__PURE__ */ new Date()
2937
+ }).returningAll().executeTakeFirst();
2938
+ }
2939
+ ui = (await complete(response)).complete;
2940
+ data = response.data;
2941
+ } else if (response) {
2942
+ data = response;
2943
+ }
2886
2944
  return createJSONRPCSuccessResponse5(request.id, {
2887
2945
  runId,
2888
2946
  runCompleted: true,
2889
- config: flowConfig
2947
+ data,
2948
+ config: flowConfig,
2949
+ ui
2890
2950
  });
2891
2951
  } catch (e) {
2892
2952
  if (e instanceof Error) {
@@ -2933,6 +2993,8 @@ export {
2933
2993
  PERMISSION_STATE,
2934
2994
  Permissions,
2935
2995
  RequestHeaders,
2996
+ STEP_STATUS,
2997
+ STEP_TYPE,
2936
2998
  checkBuiltInPermissions,
2937
2999
  createFlowContext,
2938
3000
  handleFlow,