@teamkeel/functions-runtime 0.421.2 → 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
  }
@@ -3036,112 +3044,19 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
3036
3044
  };
3037
3045
  }, "complete"),
3038
3046
  step: /* @__PURE__ */ __name(async (name, optionsOrFn, fn) => {
3039
- const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
3040
- const actualFn = typeof optionsOrFn === "function" ? optionsOrFn : fn;
3041
- options.retries = options.retries ?? defaultOpts.retries;
3042
- options.timeout = options.timeout ?? defaultOpts.timeout;
3043
- const db = useDatabase();
3044
- if (usedNames.has(name)) {
3045
- await db.insertInto("keel.flow_step").values({
3046
- run_id: runId,
3047
- name,
3048
- stage: options.stage,
3049
- status: "FAILED" /* FAILED */,
3050
- type: "FUNCTION" /* FUNCTION */,
3051
- error: `Duplicate step name: ${name}`,
3052
- startTime: /* @__PURE__ */ new Date(),
3053
- endTime: /* @__PURE__ */ new Date()
3054
- }).returningAll().executeTakeFirst();
3055
- throw new Error(`Duplicate step name: ${name}`);
3056
- }
3057
- usedNames.add(name);
3058
- const past = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().execute();
3059
- const newSteps = past.filter((step) => step.status === "NEW" /* NEW */);
3060
- const completedSteps = past.filter(
3061
- (step) => step.status === "COMPLETED" /* COMPLETED */
3062
- );
3063
- const failedSteps = past.filter(
3064
- (step) => step.status === "FAILED" /* FAILED */
3065
- );
3066
- if (newSteps.length > 1) {
3067
- throw new Error("Multiple NEW steps found for the same step");
3068
- }
3069
- if (completedSteps.length > 1) {
3070
- throw new Error("Multiple completed steps found for the same step");
3071
- }
3072
- if (completedSteps.length > 1 && newSteps.length > 1) {
3073
- throw new Error(
3074
- "Multiple completed and new steps found for the same step"
3075
- );
3076
- }
3077
- if (completedSteps.length === 1) {
3078
- return completedSteps[0].value;
3079
- }
3080
- if (newSteps.length === 1) {
3081
- let result = null;
3082
- await db.updateTable("keel.flow_step").set({
3083
- startTime: /* @__PURE__ */ new Date()
3084
- }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3085
- try {
3086
- const stepArgs = {
3087
- attempt: failedSteps.length + 1,
3088
- stepOptions: options
3089
- };
3090
- result = await withTimeout(actualFn(stepArgs), options.timeout);
3091
- } catch (e) {
3092
- await db.updateTable("keel.flow_step").set({
3093
- status: "FAILED" /* FAILED */,
3094
- spanId,
3095
- endTime: /* @__PURE__ */ new Date(),
3096
- error: e instanceof Error ? e.message : "An error occurred"
3097
- }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3098
- if (failedSteps.length >= options.retries || e instanceof NonRetriableError) {
3099
- if (options.onFailure) {
3100
- await options.onFailure();
3101
- }
3102
- throw new ExhuastedRetriesDisrupt();
3103
- }
3104
- await db.insertInto("keel.flow_step").values({
3105
- run_id: runId,
3106
- name,
3107
- stage: options.stage,
3108
- status: "NEW" /* NEW */,
3109
- type: "FUNCTION" /* FUNCTION */
3110
- }).returningAll().executeTakeFirst();
3111
- throw new StepCreatedDisrupt(
3112
- options.retryPolicy ? new Date(
3113
- Date.now() + options.retryPolicy(failedSteps.length + 1)
3114
- ) : void 0
3115
- );
3116
- }
3117
- await db.updateTable("keel.flow_step").set({
3118
- status: "COMPLETED" /* COMPLETED */,
3119
- value: JSON.stringify(result),
3120
- spanId,
3121
- endTime: /* @__PURE__ */ new Date()
3122
- }).where("id", "=", newSteps[0].id).returningAll().executeTakeFirst();
3123
- return result;
3124
- }
3125
- await db.insertInto("keel.flow_step").values({
3126
- run_id: runId,
3127
- name,
3128
- stage: options.stage,
3129
- status: "NEW" /* NEW */,
3130
- type: "FUNCTION" /* FUNCTION */
3131
- }).returningAll().executeTakeFirst();
3132
- throw new StepCreatedDisrupt();
3133
- }, "step"),
3134
- ui: {
3135
- 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;
3136
3052
  const db = useDatabase();
3137
- const isCallback = element && callback;
3138
3053
  if (usedNames.has(name)) {
3139
3054
  await db.insertInto("keel.flow_step").values({
3140
3055
  run_id: runId,
3141
3056
  name,
3142
3057
  stage: options.stage,
3143
3058
  status: "FAILED" /* FAILED */,
3144
- type: "UI" /* UI */,
3059
+ type: "FUNCTION" /* FUNCTION */,
3145
3060
  error: `Duplicate step name: ${name}`,
3146
3061
  startTime: /* @__PURE__ */ new Date(),
3147
3062
  endTime: /* @__PURE__ */ new Date()
@@ -3149,77 +3064,179 @@ function createFlowContext(runId, data, action, callback, element, spanId, ctx)
3149
3064
  throw new Error(`Duplicate step name: ${name}`);
3150
3065
  }
3151
3066
  usedNames.add(name);
3152
- let step = await db.selectFrom("keel.flow_step").where("run_id", "=", runId).where("name", "=", name).selectAll().executeTakeFirst();
3153
- if (step && step.status === "COMPLETED" /* COMPLETED */) {
3154
- if (step.action) {
3155
- return { data: step.value, action: step.action };
3156
- }
3157
- 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");
3158
3077
  }
3159
- if (!step) {
3160
- step = await db.insertInto("keel.flow_step").values({
3161
- run_id: runId,
3162
- name,
3163
- stage: options.stage,
3164
- status: "PENDING" /* PENDING */,
3165
- type: "UI" /* UI */,
3166
- startTime: /* @__PURE__ */ new Date()
3167
- }).returningAll().executeTakeFirst();
3168
- throw new UIRenderDisrupt(
3169
- step?.id,
3170
- (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"
3171
3084
  );
3172
3085
  }
3173
- 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();
3174
3095
  try {
3175
- const response = await callbackFn(
3176
- options.content,
3177
- element,
3178
- callback,
3179
- data
3180
- );
3181
- 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);
3182
3101
  } catch (e) {
3183
- if (e instanceof CallbackDisrupt) {
3184
- 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();
3185
3113
  }
3186
- throw new CallbackDisrupt(
3187
- e instanceof Error ? e.message : `An error occurred`,
3188
- 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
3189
3125
  );
3190
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;
3191
3134
  }
3192
- if (!data) {
3193
- throw new UIRenderDisrupt(
3194
- step?.id,
3195
- (await page(options, null, null)).page
3196
- );
3197
- }
3198
- try {
3199
- const p = await page(options, data, action);
3200
- if (p.hasValidationErrors) {
3201
- 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}`);
3202
3163
  }
3203
- } catch (e) {
3204
- 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();
3205
3229
  throw e;
3206
3230
  }
3207
3231
  await db.updateTable("keel.flow_step").set({
3208
- status: "FAILED" /* FAILED */,
3232
+ status: "COMPLETED" /* COMPLETED */,
3233
+ value: JSON.stringify(data),
3234
+ action,
3209
3235
  spanId,
3210
- endTime: /* @__PURE__ */ new Date(),
3211
- error: e instanceof Error ? e.message : "An error occurred"
3212
- }).where("id", "=", step?.id).returningAll().executeTakeFirst();
3213
- throw e;
3214
- }
3215
- await db.updateTable("keel.flow_step").set({
3216
- status: "COMPLETED" /* COMPLETED */,
3217
- value: JSON.stringify(data),
3218
- action,
3219
- spanId,
3220
- endTime: /* @__PURE__ */ new Date()
3221
- }).where("id", "=", step.id).returningAll().executeTakeFirst();
3222
- return { data, action };
3236
+ endTime: /* @__PURE__ */ new Date()
3237
+ }).where("id", "=", step.id).returningAll().executeTakeFirst();
3238
+ return { data, action };
3239
+ });
3223
3240
  }, "page"),
3224
3241
  inputs: {
3225
3242
  text: textInput,
@@ -3298,6 +3315,7 @@ async function handleFlow(request, config) {
3298
3315
  );
3299
3316
  return opentelemetry6.context.with(activeContext, () => {
3300
3317
  return withSpan(request.method, async (span) => {
3318
+ span.setAttribute(KEEL_INTERNAL_ATTR, true);
3301
3319
  let db = null;
3302
3320
  let flowConfig = null;
3303
3321
  const runId = request.meta?.runId;
@@ -3355,6 +3373,7 @@ async function handleFlow(request, config) {
3355
3373
  });
3356
3374
  } catch (e) {
3357
3375
  if (e instanceof StepCreatedDisrupt) {
3376
+ span.setAttribute(KEEL_INTERNAL_ATTR, KEEL_INTERNAL_CHILDREN);
3358
3377
  return (0, import_json_rpc_26.createJSONRPCSuccessResponse)(request.id, {
3359
3378
  runId,
3360
3379
  runCompleted: false,
@@ -3376,11 +3395,13 @@ async function handleFlow(request, config) {
3376
3395
  ui: e.contents
3377
3396
  });
3378
3397
  }
3379
- span.recordException(e);
3380
- span.setStatus({
3381
- code: opentelemetry6.SpanStatusCode.ERROR,
3382
- message: e instanceof Error ? e.message : "unknown error"
3383
- });
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
+ }
3384
3405
  if (e instanceof ExhuastedRetriesDisrupt) {
3385
3406
  return (0, import_json_rpc_26.createJSONRPCSuccessResponse)(request.id, {
3386
3407
  runId,