@teamkeel/functions-runtime 0.421.1 → 0.421.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.cjs CHANGED
@@ -247,6 +247,8 @@ var import_pg = require("pg");
247
247
  // src/tracing.js
248
248
  var tracing_exports = {};
249
249
  __export(tracing_exports, {
250
+ KEEL_INTERNAL_ATTR: () => KEEL_INTERNAL_ATTR,
251
+ KEEL_INTERNAL_CHILDREN: () => KEEL_INTERNAL_CHILDREN,
250
252
  forceFlush: () => forceFlush,
251
253
  getTracer: () => getTracer,
252
254
  init: () => init,
@@ -263,11 +265,13 @@ async function withSpan(name, fn) {
263
265
  try {
264
266
  return await fn(span);
265
267
  } catch (err) {
266
- span.recordException(err);
267
- span.setStatus({
268
- code: opentelemetry.SpanStatusCode.ERROR,
269
- message: err.message
270
- });
268
+ if (err instanceof Error) {
269
+ span.recordException(err);
270
+ span.setStatus({
271
+ code: opentelemetry.SpanStatusCode.ERROR,
272
+ message: err.message
273
+ });
274
+ }
271
275
  throw err;
272
276
  } finally {
273
277
  span.end();
@@ -405,6 +409,8 @@ function spanNameForModelAPI(modelName, action) {
405
409
  return `Database ${modelName}.${action}`;
406
410
  }
407
411
  __name(spanNameForModelAPI, "spanNameForModelAPI");
412
+ var KEEL_INTERNAL_ATTR = "keel_internal";
413
+ var KEEL_INTERNAL_CHILDREN = "includeChildrenSpans";
408
414
 
409
415
  // src/database.ts
410
416
  var import_ws = __toESM(require("ws"), 1);
@@ -473,6 +479,7 @@ var InstrumentedPool = class extends import_pg.Pool {
473
479
  const _super = super.connect.bind(this);
474
480
  return withSpan("Database Connect", function(span) {
475
481
  span.setAttribute("dialect", process.env["KEEL_DB_CONN_TYPE"]);
482
+ span.setAttribute(KEEL_INTERNAL_ATTR, true);
476
483
  return _super.apply(null, args);
477
484
  });
478
485
  }
@@ -485,6 +492,7 @@ var InstrumentedNeonServerlessPool = class extends neon.Pool {
485
492
  const _super = super.connect.bind(this);
486
493
  return withSpan("Database Connect", function(span) {
487
494
  span.setAttribute("dialect", process.env["KEEL_DB_CONN_TYPE"]);
495
+ span.setAttribute(KEEL_INTERNAL_ATTR, true);
488
496
  return _super.apply(null, args);
489
497
  });
490
498
  }
@@ -2880,7 +2888,8 @@ var print = /* @__PURE__ */ __name(async (options) => {
2880
2888
  return {
2881
2889
  type: d.type,
2882
2890
  name: d.name,
2883
- data: Array.isArray(d.data) ? d.data : [d.data]
2891
+ data: Array.isArray(d.data) ? d.data : [d.data],
2892
+ printer: d.printer
2884
2893
  };
2885
2894
  }
2886
2895
  return null;
@@ -3035,112 +3044,19 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
3035
3044
  };
3036
3045
  }, "complete"),
3037
3046
  step: /* @__PURE__ */ __name(async (name, optionsOrFn, fn) => {
3038
- const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
3039
- const actualFn = typeof optionsOrFn === "function" ? optionsOrFn : fn;
3040
- options.retries = options.retries ?? defaultOpts.retries;
3041
- options.timeout = options.timeout ?? defaultOpts.timeout;
3042
- const db = useDatabase();
3043
- if (usedNames.has(name)) {
3044
- await db.insertInto("keel.flow_step").values({
3045
- run_id: runId,
3046
- name,
3047
- stage: options.stage,
3048
- status: "FAILED" /* FAILED */,
3049
- type: "FUNCTION" /* FUNCTION */,
3050
- error: `Duplicate step name: ${name}`,
3051
- startTime: /* @__PURE__ */ new Date(),
3052
- endTime: /* @__PURE__ */ new Date()
3053
- }).returningAll().executeTakeFirst();
3054
- throw new Error(`Duplicate step name: ${name}`);
3055
- }
3056
- usedNames.add(name);
3057
- const past = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().execute();
3058
- const newSteps = past.filter((step) => step.status === "NEW" /* NEW */);
3059
- const completedSteps = past.filter(
3060
- (step) => step.status === "COMPLETED" /* COMPLETED */
3061
- );
3062
- const failedSteps = past.filter(
3063
- (step) => step.status === "FAILED" /* FAILED */
3064
- );
3065
- if (newSteps.length > 1) {
3066
- throw new Error("Multiple NEW steps found for the same step");
3067
- }
3068
- if (completedSteps.length > 1) {
3069
- throw new Error("Multiple completed steps found for the same step");
3070
- }
3071
- if (completedSteps.length > 1 && newSteps.length > 1) {
3072
- throw new Error(
3073
- "Multiple completed and new steps found for the same step"
3074
- );
3075
- }
3076
- if (completedSteps.length === 1) {
3077
- return completedSteps[0].value;
3078
- }
3079
- if (newSteps.length === 1) {
3080
- let result = null;
3081
- await db.updateTable("keel.flow_step").set({
3082
- startTime: /* @__PURE__ */ new Date()
3083
- }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3084
- try {
3085
- const stepArgs = {
3086
- attempt: failedSteps.length + 1,
3087
- stepOptions: options
3088
- };
3089
- result = await withTimeout(actualFn(stepArgs), options.timeout);
3090
- } catch (e) {
3091
- await db.updateTable("keel.flow_step").set({
3092
- status: "FAILED" /* FAILED */,
3093
- spanId,
3094
- endTime: /* @__PURE__ */ new Date(),
3095
- error: e instanceof Error ? e.message : "An error occurred"
3096
- }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3097
- if (failedSteps.length >= options.retries || e instanceof NonRetriableError) {
3098
- if (options.onFailure) {
3099
- await options.onFailure();
3100
- }
3101
- throw new ExhuastedRetriesDisrupt();
3102
- }
3103
- await db.insertInto("keel.flow_step").values({
3104
- run_id: runId,
3105
- name,
3106
- stage: options.stage,
3107
- status: "NEW" /* NEW */,
3108
- type: "FUNCTION" /* FUNCTION */
3109
- }).returningAll().executeTakeFirst();
3110
- throw new StepCreatedDisrupt(
3111
- options.retryPolicy ? new Date(
3112
- Date.now() + options.retryPolicy(failedSteps.length + 1)
3113
- ) : void 0
3114
- );
3115
- }
3116
- await db.updateTable("keel.flow_step").set({
3117
- status: "COMPLETED" /* COMPLETED */,
3118
- value: JSON.stringify(result),
3119
- spanId,
3120
- endTime: /* @__PURE__ */ new Date()
3121
- }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3122
- return result;
3123
- }
3124
- await db.insertInto("keel.flow_step").values({
3125
- run_id: runId,
3126
- name,
3127
- stage: options.stage,
3128
- status: "NEW" /* NEW */,
3129
- type: "FUNCTION" /* FUNCTION */
3130
- }).returningAll().executeTakeFirst();
3131
- throw new StepCreatedDisrupt();
3132
- }, "step"),
3133
- ui: {
3134
- page: /* @__PURE__ */ __name(async (name, options) => {
3047
+ return withSpan(`Step - ${name}`, async (span) => {
3048
+ const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
3049
+ const actualFn = typeof optionsOrFn === "function" ? optionsOrFn : fn;
3050
+ options.retries = options.retries ?? defaultOpts.retries;
3051
+ options.timeout = options.timeout ?? defaultOpts.timeout;
3135
3052
  const db = useDatabase();
3136
- const isCallback = element && callback;
3137
3053
  if (usedNames.has(name)) {
3138
3054
  await db.insertInto("keel.flow_step").values({
3139
3055
  run_id: runId,
3140
3056
  name,
3141
3057
  stage: options.stage,
3142
3058
  status: "FAILED" /* FAILED */,
3143
- type: "UI" /* UI */,
3059
+ type: "FUNCTION" /* FUNCTION */,
3144
3060
  error: `Duplicate step name: ${name}`,
3145
3061
  startTime: /* @__PURE__ */ new Date(),
3146
3062
  endTime: /* @__PURE__ */ new Date()
@@ -3148,77 +3064,179 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
3148
3064
  throw new Error(`Duplicate step name: ${name}`);
3149
3065
  }
3150
3066
  usedNames.add(name);
3151
- let step = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
3152
- if (step && step.status === "COMPLETED" /* COMPLETED */) {
3153
- if (step.action) {
3154
- return { data: step.value, action: step.action };
3155
- }
3156
- return step.value;
3067
+ const past = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().execute();
3068
+ const newSteps = past.filter((step) => step.status === "NEW" /* NEW */);
3069
+ const completedSteps = past.filter(
3070
+ (step) => step.status === "COMPLETED" /* COMPLETED */
3071
+ );
3072
+ const failedSteps = past.filter(
3073
+ (step) => step.status === "FAILED" /* FAILED */
3074
+ );
3075
+ if (newSteps.length > 1) {
3076
+ throw new Error("Multiple NEW steps found for the same step");
3157
3077
  }
3158
- if (!step) {
3159
- step = await db.insertInto("keel.flow_step").values({
3160
- run_id: runId,
3161
- name,
3162
- stage: options.stage,
3163
- status: "PENDING" /* PENDING */,
3164
- type: "UI" /* UI */,
3165
- startTime: /* @__PURE__ */ new Date()
3166
- }).returningAll().executeTakeFirst();
3167
- throw new UIRenderDisrupt(
3168
- step?.id,
3169
- (await page(options, null, null)).page
3078
+ if (completedSteps.length > 1) {
3079
+ throw new Error("Multiple completed steps found for the same step");
3080
+ }
3081
+ if (completedSteps.length > 1 && newSteps.length > 1) {
3082
+ throw new Error(
3083
+ "Multiple completed and new steps found for the same step"
3170
3084
  );
3171
3085
  }
3172
- if (isCallback) {
3086
+ if (completedSteps.length === 1) {
3087
+ span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
3088
+ return completedSteps[0].value;
3089
+ }
3090
+ if (newSteps.length === 1) {
3091
+ let result = null;
3092
+ await db.updateTable("keel.flow_step").set({
3093
+ startTime: /* @__PURE__ */ new Date()
3094
+ }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3173
3095
  try {
3174
- const response = await callbackFn(
3175
- options.content,
3176
- element,
3177
- callback,
3178
- data
3179
- );
3180
- throw new CallbackDisrupt(response, false);
3096
+ const stepArgs = {
3097
+ attempt: failedSteps.length + 1,
3098
+ stepOptions: options
3099
+ };
3100
+ result = await withTimeout(actualFn(stepArgs), options.timeout);
3181
3101
  } catch (e) {
3182
- if (e instanceof CallbackDisrupt) {
3183
- throw e;
3102
+ await db.updateTable("keel.flow_step").set({
3103
+ status: "FAILED" /* FAILED */,
3104
+ spanId,
3105
+ endTime: /* @__PURE__ */ new Date(),
3106
+ error: e instanceof Error ? e.message : "An error occurred"
3107
+ }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3108
+ if (failedSteps.length >= options.retries || e instanceof NonRetriableError) {
3109
+ if (options.onFailure) {
3110
+ await options.onFailure();
3111
+ }
3112
+ throw new ExhuastedRetriesDisrupt();
3184
3113
  }
3185
- throw new CallbackDisrupt(
3186
- e instanceof Error ? e.message : `An error occurred`,
3187
- true
3114
+ await db.insertInto("keel.flow_step").values({
3115
+ run_id: runId,
3116
+ name,
3117
+ stage: options.stage,
3118
+ status: "NEW" /* NEW */,
3119
+ type: "FUNCTION" /* FUNCTION */
3120
+ }).returningAll().executeTakeFirst();
3121
+ throw new StepCreatedDisrupt(
3122
+ options.retryPolicy ? new Date(
3123
+ Date.now() + options.retryPolicy(failedSteps.length + 1)
3124
+ ) : void 0
3188
3125
  );
3189
3126
  }
3127
+ await db.updateTable("keel.flow_step").set({
3128
+ status: "COMPLETED" /* COMPLETED */,
3129
+ value: JSON.stringify(result),
3130
+ spanId,
3131
+ endTime: /* @__PURE__ */ new Date()
3132
+ }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3133
+ return result;
3190
3134
  }
3191
- if (!data) {
3192
- throw new UIRenderDisrupt(
3193
- step?.id,
3194
- (await page(options, null, null)).page
3195
- );
3196
- }
3197
- try {
3198
- const p = await page(options, data, action);
3199
- if (p.hasValidationErrors) {
3200
- throw new UIRenderDisrupt(step?.id, p.page);
3135
+ await db.insertInto("keel.flow_step").values({
3136
+ run_id: runId,
3137
+ name,
3138
+ stage: options.stage,
3139
+ status: "NEW" /* NEW */,
3140
+ type: "FUNCTION" /* FUNCTION */
3141
+ }).returningAll().executeTakeFirst();
3142
+ span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
3143
+ throw new StepCreatedDisrupt();
3144
+ });
3145
+ }, "step"),
3146
+ ui: {
3147
+ page: /* @__PURE__ */ __name(async (name, options) => {
3148
+ return withSpan(`Page - ${name}`, async (span) => {
3149
+ const db = useDatabase();
3150
+ const isCallback = element && callback;
3151
+ if (usedNames.has(name)) {
3152
+ await db.insertInto("keel.flow_step").values({
3153
+ run_id: runId,
3154
+ name,
3155
+ stage: options.stage,
3156
+ status: "FAILED" /* FAILED */,
3157
+ type: "UI" /* UI */,
3158
+ error: `Duplicate step name: ${name}`,
3159
+ startTime: /* @__PURE__ */ new Date(),
3160
+ endTime: /* @__PURE__ */ new Date()
3161
+ }).returningAll().executeTakeFirst();
3162
+ throw new Error(`Duplicate step name: ${name}`);
3201
3163
  }
3202
- } catch (e) {
3203
- if (e instanceof UIRenderDisrupt) {
3164
+ usedNames.add(name);
3165
+ let step = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
3166
+ if (step && step.status === "COMPLETED" /* COMPLETED */) {
3167
+ span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
3168
+ if (step.action) {
3169
+ return { data: step.value, action: step.action };
3170
+ }
3171
+ return step.value;
3172
+ }
3173
+ if (!step) {
3174
+ step = await db.insertInto("keel.flow_step").values({
3175
+ run_id: runId,
3176
+ name,
3177
+ stage: options.stage,
3178
+ status: "PENDING" /* PENDING */,
3179
+ type: "UI" /* UI */,
3180
+ startTime: /* @__PURE__ */ new Date()
3181
+ }).returningAll().executeTakeFirst();
3182
+ span.setAttribute("rendered", true);
3183
+ throw new UIRenderDisrupt(
3184
+ step?.id,
3185
+ (await page(options, null, null)).page
3186
+ );
3187
+ }
3188
+ if (isCallback) {
3189
+ span.setAttribute("callback", callback);
3190
+ try {
3191
+ const response = await callbackFn(
3192
+ options.content,
3193
+ element,
3194
+ callback,
3195
+ data
3196
+ );
3197
+ throw new CallbackDisrupt(response, false);
3198
+ } catch (e) {
3199
+ if (e instanceof CallbackDisrupt) {
3200
+ throw e;
3201
+ }
3202
+ throw new CallbackDisrupt(
3203
+ e instanceof Error ? e.message : `An error occurred`,
3204
+ true
3205
+ );
3206
+ }
3207
+ }
3208
+ if (!data) {
3209
+ throw new UIRenderDisrupt(
3210
+ step?.id,
3211
+ (await page(options, null, null)).page
3212
+ );
3213
+ }
3214
+ try {
3215
+ const p = await page(options, data, action);
3216
+ if (p.hasValidationErrors) {
3217
+ throw new UIRenderDisrupt(step?.id, p.page);
3218
+ }
3219
+ } catch (e) {
3220
+ if (e instanceof UIRenderDisrupt) {
3221
+ throw e;
3222
+ }
3223
+ await db.updateTable("keel.flow_step").set({
3224
+ status: "FAILED" /* FAILED */,
3225
+ spanId,
3226
+ endTime: /* @__PURE__ */ new Date(),
3227
+ error: e instanceof Error ? e.message : "An error occurred"
3228
+ }).where("id", "=", step?.id).returningAll().executeTakeFirst();
3204
3229
  throw e;
3205
3230
  }
3206
3231
  await db.updateTable("keel.flow_step").set({
3207
- status: "FAILED" /* FAILED */,
3232
+ status: "COMPLETED" /* COMPLETED */,
3233
+ value: JSON.stringify(data),
3234
+ action,
3208
3235
  spanId,
3209
- endTime: /* @__PURE__ */ new Date(),
3210
- error: e instanceof Error ? e.message : "An error occurred"
3211
- }).where("id", "=", step?.id).returningAll().executeTakeFirst();
3212
- throw e;
3213
- }
3214
- await db.updateTable("keel.flow_step").set({
3215
- status: "COMPLETED" /* COMPLETED */,
3216
- value: JSON.stringify(data),
3217
- action,
3218
- spanId,
3219
- endTime: /* @__PURE__ */ new Date()
3220
- }).where("id", "=", step.id).returningAll().executeTakeFirst();
3221
- return { data, action };
3236
+ endTime: /* @__PURE__ */ new Date()
3237
+ }).where("id", "=", step.id).returningAll().executeTakeFirst();
3238
+ return { data, action };
3239
+ });
3222
3240
  }, "page"),
3223
3241
  inputs: {
3224
3242
  text: textInput,
@@ -3297,6 +3315,7 @@ async function handleFlow(request, config) {
3297
3315
  );
3298
3316
  return opentelemetry6.context.with(activeContext, () => {
3299
3317
  return withSpan(request.method, async (span) => {
3318
+ span.setAttribute(KEEL_INTERNAL_ATTR, true);
3300
3319
  let db = null;
3301
3320
  let flowConfig = null;
3302
3321
  const runId = request.meta?.runId;
@@ -3354,6 +3373,7 @@ async function handleFlow(request, config) {
3354
3373
  });
3355
3374
  } catch (e) {
3356
3375
  if (e instanceof StepCreatedDisrupt) {
3376
+ span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
3357
3377
  return (0, import_json_rpc_26.createJSONRPCSuccessResponse)(request.id, {
3358
3378
  runId,
3359
3379
  runCompleted: false,
@@ -3375,11 +3395,13 @@ async function handleFlow(request, config) {
3375
3395
  ui: e.contents
3376
3396
  });
3377
3397
  }
3378
- span.recordException(e);
3379
- span.setStatus({
3380
- code: opentelemetry6.SpanStatusCode.ERROR,
3381
- message: e instanceof Error ? e.message : "unknown error"
3382
- });
3398
+ if (e instanceof Error) {
3399
+ span.recordException(e);
3400
+ span.setStatus({
3401
+ code: opentelemetry6.SpanStatusCode.ERROR,
3402
+ message: e instanceof Error ? e.message : "unknown error"
3403
+ });
3404
+ }
3383
3405
  if (e instanceof ExhuastedRetriesDisrupt) {
3384
3406
  return (0, import_json_rpc_26.createJSONRPCSuccessResponse)(request.id, {
3385
3407
  runId,