@teamkeel/functions-runtime 0.413.2-next.2 → 0.413.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
@@ -537,15 +537,18 @@ interface FlowContext<C extends FlowConfig> {
537
537
  step: Step<C>;
538
538
  ui: UI<C>;
539
539
  }
540
+ type JsonSerializable = string | number | boolean | null | JsonSerializable[] | {
541
+ [key: string]: JsonSerializable;
542
+ };
540
543
  type Step<C extends FlowConfig> = {
541
- <R>(name: string, options: {
544
+ <R extends JsonSerializable | void>(name: string, options: {
542
545
  stage?: ExtractStageKeys<C>;
543
546
  maxRetries?: number;
544
547
  timeoutInMs?: number;
545
548
  }, fn: () => Promise<R> & {
546
549
  catch: (errorHandler: (err: Error) => Promise<void> | void) => Promise<any>;
547
550
  }): Promise<R>;
548
- <R>(name: string, fn: () => Promise<R> & {
551
+ <R extends JsonSerializable | void>(name: string, fn: () => Promise<R> & {
549
552
  catch: (errorHandler: (err: Error) => Promise<void> | void) => Promise<any>;
550
553
  }): Promise<R>;
551
554
  };
package/dist/index.d.ts CHANGED
@@ -537,15 +537,18 @@ interface FlowContext<C extends FlowConfig> {
537
537
  step: Step<C>;
538
538
  ui: UI<C>;
539
539
  }
540
+ type JsonSerializable = string | number | boolean | null | JsonSerializable[] | {
541
+ [key: string]: JsonSerializable;
542
+ };
540
543
  type Step<C extends FlowConfig> = {
541
- <R>(name: string, options: {
544
+ <R extends JsonSerializable | void>(name: string, options: {
542
545
  stage?: ExtractStageKeys<C>;
543
546
  maxRetries?: number;
544
547
  timeoutInMs?: number;
545
548
  }, fn: () => Promise<R> & {
546
549
  catch: (errorHandler: (err: Error) => Promise<void> | void) => Promise<any>;
547
550
  }): Promise<R>;
548
- <R>(name: string, fn: () => Promise<R> & {
551
+ <R extends JsonSerializable | void>(name: string, fn: () => Promise<R> & {
549
552
  catch: (errorHandler: (err: Error) => Promise<void> | void) => Promise<any>;
550
553
  }): Promise<R>;
551
554
  };
package/dist/index.js CHANGED
@@ -2558,7 +2558,7 @@ function createFlowContext(runId, data, spanId) {
2558
2558
  const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
2559
2559
  const actualFn = typeof optionsOrFn === "function" ? optionsOrFn : fn;
2560
2560
  const db = useDatabase();
2561
- const past = await db.selectFrom("keel_flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().execute();
2561
+ const past = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().execute();
2562
2562
  const newSteps = past.filter((step) => step.status === "NEW" /* NEW */);
2563
2563
  const completedSteps = past.filter(
2564
2564
  (step) => step.status === "COMPLETED" /* COMPLETED */
@@ -2582,7 +2582,7 @@ function createFlowContext(runId, data, spanId) {
2582
2582
  }
2583
2583
  if (newSteps.length === 1) {
2584
2584
  let result = null;
2585
- await db.updateTable("keel_flow_step").set({
2585
+ await db.updateTable("keel.flow_step").set({
2586
2586
  startTime: /* @__PURE__ */ new Date()
2587
2587
  }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
2588
2588
  try {
@@ -2591,7 +2591,7 @@ function createFlowContext(runId, data, spanId) {
2591
2591
  options.timeoutInMs ?? defaultOpts.timeoutInMs
2592
2592
  );
2593
2593
  } catch (e) {
2594
- await db.updateTable("keel_flow_step").set({
2594
+ await db.updateTable("keel.flow_step").set({
2595
2595
  status: "FAILED" /* FAILED */,
2596
2596
  spanId,
2597
2597
  endTime: /* @__PURE__ */ new Date(),
@@ -2600,7 +2600,7 @@ function createFlowContext(runId, data, spanId) {
2600
2600
  if (failedSteps.length + 1 >= (options.maxRetries ?? defaultOpts.maxRetries)) {
2601
2601
  throw new ExhuastedRetriesDisrupt();
2602
2602
  }
2603
- await db.insertInto("keel_flow_step").values({
2603
+ await db.insertInto("keel.flow_step").values({
2604
2604
  run_id: runId,
2605
2605
  name,
2606
2606
  stage: options.stage,
@@ -2609,7 +2609,7 @@ function createFlowContext(runId, data, spanId) {
2609
2609
  }).returningAll().executeTakeFirst();
2610
2610
  throw new StepCreatedDisrupt();
2611
2611
  }
2612
- await db.updateTable("keel_flow_step").set({
2612
+ await db.updateTable("keel.flow_step").set({
2613
2613
  status: "COMPLETED" /* COMPLETED */,
2614
2614
  value: JSON.stringify(result),
2615
2615
  spanId,
@@ -2617,7 +2617,7 @@ function createFlowContext(runId, data, spanId) {
2617
2617
  }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
2618
2618
  return result;
2619
2619
  }
2620
- await db.insertInto("keel_flow_step").values({
2620
+ await db.insertInto("keel.flow_step").values({
2621
2621
  run_id: runId,
2622
2622
  name,
2623
2623
  stage: options.stage,
@@ -2629,12 +2629,12 @@ function createFlowContext(runId, data, spanId) {
2629
2629
  ui: {
2630
2630
  page: /* @__PURE__ */ __name(async (name, options) => {
2631
2631
  const db = useDatabase();
2632
- let step = await db.selectFrom("keel_flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
2632
+ let step = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
2633
2633
  if (step && step.status === "COMPLETED" /* COMPLETED */) {
2634
2634
  return step.value;
2635
2635
  }
2636
2636
  if (!step) {
2637
- step = await db.insertInto("keel_flow_step").values({
2637
+ step = await db.insertInto("keel.flow_step").values({
2638
2638
  run_id: runId,
2639
2639
  name,
2640
2640
  stage: options.stage,
@@ -2647,7 +2647,7 @@ function createFlowContext(runId, data, spanId) {
2647
2647
  if (!data) {
2648
2648
  throw new UIRenderDisrupt(step?.id, page(options));
2649
2649
  }
2650
- await db.updateTable("keel_flow_step").set({
2650
+ await db.updateTable("keel.flow_step").set({
2651
2651
  status: "COMPLETED" /* COMPLETED */,
2652
2652
  value: JSON.stringify(data),
2653
2653
  spanId,
@@ -2723,7 +2723,7 @@ async function handleFlow(request, config) {
2723
2723
  db = createDatabaseClient({
2724
2724
  connString: request.meta?.secrets?.KEEL_DB_CONN
2725
2725
  });
2726
- const flowRun = await db.selectFrom("keel_flow_run").where("id", "=", runId).selectAll().executeTakeFirst();
2726
+ const flowRun = await db.selectFrom("keel.flow_run").where("id", "=", runId).selectAll().executeTakeFirst();
2727
2727
  if (!flowRun) {
2728
2728
  throw new Error("no flow run found");
2729
2729
  }
@@ -2734,48 +2734,70 @@ async function handleFlow(request, config) {
2734
2734
  );
2735
2735
  const flowFunction = flows[request.method].fn;
2736
2736
  flowConfig = flows[request.method].config;
2737
- await tryExecuteFlow(db, async () => {
2738
- const inputs = parseInputs(flowRun.input);
2739
- return flowFunction(ctx, inputs);
2740
- });
2737
+ const inputs = parseInputs(flowRun.input);
2738
+ try {
2739
+ await tryExecuteFlow(db, async () => {
2740
+ return flowFunction(ctx, inputs);
2741
+ });
2742
+ } catch (e) {
2743
+ if (e instanceof StepCreatedDisrupt) {
2744
+ return createJSONRPCSuccessResponse5(request.id, {
2745
+ runId,
2746
+ runCompleted: false,
2747
+ config: flowConfig
2748
+ });
2749
+ }
2750
+ if (e instanceof UIRenderDisrupt) {
2751
+ return createJSONRPCSuccessResponse5(request.id, {
2752
+ runId,
2753
+ stepId: e.stepId,
2754
+ config: flowConfig,
2755
+ ui: e.contents
2756
+ });
2757
+ }
2758
+ span.recordException(e);
2759
+ span.setStatus({
2760
+ code: opentelemetry6.SpanStatusCode.ERROR,
2761
+ message: e.message
2762
+ });
2763
+ if (e instanceof ExhuastedRetriesDisrupt) {
2764
+ return createJSONRPCSuccessResponse5(request.id, {
2765
+ runId,
2766
+ runCompleted: true,
2767
+ error: "flow failed due to exhausted step retries",
2768
+ config: flowConfig
2769
+ });
2770
+ }
2771
+ return createJSONRPCSuccessResponse5(request.id, {
2772
+ runId,
2773
+ runCompleted: true,
2774
+ error: e.message,
2775
+ config: flowConfig
2776
+ });
2777
+ }
2741
2778
  return createJSONRPCSuccessResponse5(request.id, {
2742
2779
  runId,
2743
2780
  runCompleted: true,
2744
2781
  config: flowConfig
2745
2782
  });
2746
2783
  } catch (e) {
2747
- if (e instanceof StepCreatedDisrupt) {
2748
- return createJSONRPCSuccessResponse5(request.id, {
2749
- runId,
2750
- runCompleted: false,
2751
- config: flowConfig
2752
- });
2753
- }
2754
- if (e instanceof UIRenderDisrupt) {
2755
- return createJSONRPCSuccessResponse5(request.id, {
2756
- runId,
2757
- stepId: e.stepId,
2758
- config: flowConfig,
2759
- ui: e.contents
2784
+ if (e instanceof Error) {
2785
+ span.recordException(e);
2786
+ span.setStatus({
2787
+ code: opentelemetry6.SpanStatusCode.ERROR,
2788
+ message: e.message
2760
2789
  });
2790
+ return errorToJSONRPCResponse(request, e);
2761
2791
  }
2762
- span.recordException(e);
2792
+ const message = JSON.stringify(e);
2763
2793
  span.setStatus({
2764
2794
  code: opentelemetry6.SpanStatusCode.ERROR,
2765
- message: e.message
2795
+ message
2766
2796
  });
2767
- if (e instanceof ExhuastedRetriesDisrupt) {
2768
- return createJSONRPCSuccessResponse5(request.id, {
2769
- runId,
2770
- runCompleted: true,
2771
- error: "flow failed due to exhausted step retries",
2772
- config: flowConfig
2773
- });
2774
- }
2775
2797
  return createJSONRPCErrorResponse6(
2776
2798
  request.id,
2777
- JSONRPCErrorCode5.InternalError,
2778
- e.message
2799
+ RuntimeErrors.UnknownError,
2800
+ message
2779
2801
  );
2780
2802
  } finally {
2781
2803
  if (db) {