duron 0.3.0-beta.8 → 0.3.0

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.
Files changed (91) hide show
  1. package/dist/action-job.d.ts +33 -2
  2. package/dist/action-job.d.ts.map +1 -1
  3. package/dist/action-job.js +93 -26
  4. package/dist/action-manager.d.ts +44 -2
  5. package/dist/action-manager.d.ts.map +1 -1
  6. package/dist/action-manager.js +64 -3
  7. package/dist/action.d.ts +388 -7
  8. package/dist/action.d.ts.map +1 -1
  9. package/dist/action.js +44 -23
  10. package/dist/adapters/adapter.d.ts +365 -8
  11. package/dist/adapters/adapter.d.ts.map +1 -1
  12. package/dist/adapters/adapter.js +221 -15
  13. package/dist/adapters/postgres/base.d.ts +184 -6
  14. package/dist/adapters/postgres/base.d.ts.map +1 -1
  15. package/dist/adapters/postgres/base.js +436 -75
  16. package/dist/adapters/postgres/pglite.d.ts +37 -0
  17. package/dist/adapters/postgres/pglite.d.ts.map +1 -1
  18. package/dist/adapters/postgres/pglite.js +38 -0
  19. package/dist/adapters/postgres/postgres.d.ts +35 -0
  20. package/dist/adapters/postgres/postgres.d.ts.map +1 -1
  21. package/dist/adapters/postgres/postgres.js +42 -0
  22. package/dist/adapters/postgres/schema.d.ts +150 -37
  23. package/dist/adapters/postgres/schema.d.ts.map +1 -1
  24. package/dist/adapters/postgres/schema.default.d.ts +151 -38
  25. package/dist/adapters/postgres/schema.default.d.ts.map +1 -1
  26. package/dist/adapters/postgres/schema.default.js +2 -2
  27. package/dist/adapters/postgres/schema.js +60 -23
  28. package/dist/adapters/schemas.d.ts +124 -80
  29. package/dist/adapters/schemas.d.ts.map +1 -1
  30. package/dist/adapters/schemas.js +139 -26
  31. package/dist/client.d.ts +426 -22
  32. package/dist/client.d.ts.map +1 -1
  33. package/dist/client.js +370 -20
  34. package/dist/constants.js +6 -0
  35. package/dist/errors.d.ts +166 -9
  36. package/dist/errors.d.ts.map +1 -1
  37. package/dist/errors.js +189 -19
  38. package/dist/index.d.ts +2 -1
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/server.d.ts +99 -37
  41. package/dist/server.d.ts.map +1 -1
  42. package/dist/server.js +84 -25
  43. package/dist/step-manager.d.ts +111 -4
  44. package/dist/step-manager.d.ts.map +1 -1
  45. package/dist/step-manager.js +411 -75
  46. package/dist/telemetry/index.d.ts +1 -4
  47. package/dist/telemetry/index.d.ts.map +1 -1
  48. package/dist/telemetry/index.js +2 -4
  49. package/dist/telemetry/local-span-exporter.d.ts +56 -0
  50. package/dist/telemetry/local-span-exporter.d.ts.map +1 -0
  51. package/dist/telemetry/local-span-exporter.js +118 -0
  52. package/dist/utils/p-retry.d.ts +5 -0
  53. package/dist/utils/p-retry.d.ts.map +1 -1
  54. package/dist/utils/p-retry.js +8 -0
  55. package/dist/utils/wait-for-abort.d.ts +1 -0
  56. package/dist/utils/wait-for-abort.d.ts.map +1 -1
  57. package/dist/utils/wait-for-abort.js +1 -0
  58. package/migrations/postgres/{20260119153838_flimsy_thor_girl → 20260121160012_normal_bloodstrike}/migration.sql +32 -20
  59. package/migrations/postgres/{20260119153838_flimsy_thor_girl → 20260121160012_normal_bloodstrike}/snapshot.json +241 -66
  60. package/package.json +42 -26
  61. package/src/action-job.ts +43 -32
  62. package/src/action-manager.ts +5 -5
  63. package/src/action.ts +317 -149
  64. package/src/adapters/adapter.ts +54 -54
  65. package/src/adapters/postgres/base.ts +266 -86
  66. package/src/adapters/postgres/schema.default.ts +2 -2
  67. package/src/adapters/postgres/schema.ts +52 -24
  68. package/src/adapters/schemas.ts +91 -36
  69. package/src/client.ts +322 -68
  70. package/src/errors.ts +141 -30
  71. package/src/index.ts +2 -0
  72. package/src/server.ts +39 -37
  73. package/src/step-manager.ts +254 -91
  74. package/src/telemetry/index.ts +2 -20
  75. package/src/telemetry/local-span-exporter.ts +148 -0
  76. package/dist/telemetry/adapter.d.ts +0 -107
  77. package/dist/telemetry/adapter.d.ts.map +0 -1
  78. package/dist/telemetry/adapter.js +0 -134
  79. package/dist/telemetry/local.d.ts +0 -22
  80. package/dist/telemetry/local.d.ts.map +0 -1
  81. package/dist/telemetry/local.js +0 -243
  82. package/dist/telemetry/noop.d.ts +0 -17
  83. package/dist/telemetry/noop.d.ts.map +0 -1
  84. package/dist/telemetry/noop.js +0 -66
  85. package/dist/telemetry/opentelemetry.d.ts +0 -25
  86. package/dist/telemetry/opentelemetry.d.ts.map +0 -1
  87. package/dist/telemetry/opentelemetry.js +0 -312
  88. package/src/telemetry/adapter.ts +0 -642
  89. package/src/telemetry/local.ts +0 -429
  90. package/src/telemetry/noop.ts +0 -141
  91. package/src/telemetry/opentelemetry.ts +0 -453
package/dist/constants.js CHANGED
@@ -1,3 +1,6 @@
1
+ // ============================================================================
2
+ // Job Status Constants
3
+ // ============================================================================
1
4
  export const JOB_STATUS_CREATED = 'created';
2
5
  export const JOB_STATUS_ACTIVE = 'active';
3
6
  export const JOB_STATUS_COMPLETED = 'completed';
@@ -10,6 +13,9 @@ export const JOB_STATUSES = [
10
13
  JOB_STATUS_FAILED,
11
14
  JOB_STATUS_CANCELLED,
12
15
  ];
16
+ // ============================================================================
17
+ // Step Status Constants
18
+ // ============================================================================
13
19
  export const STEP_STATUS_ACTIVE = 'active';
14
20
  export const STEP_STATUS_COMPLETED = 'completed';
15
21
  export const STEP_STATUS_FAILED = 'failed';
package/dist/errors.d.ts CHANGED
@@ -1,49 +1,206 @@
1
+ export declare const ERROR_CODES: {
2
+ readonly DURON_ERROR: "DURON_ERROR";
3
+ readonly STEP_ALREADY_EXECUTED: "STEP_ALREADY_EXECUTED";
4
+ readonly NON_RETRIABLE: "NON_RETRIABLE";
5
+ readonly ACTION_TIMEOUT: "ACTION_TIMEOUT";
6
+ readonly STEP_TIMEOUT: "STEP_TIMEOUT";
7
+ readonly ACTION_CANCEL: "ACTION_CANCEL";
8
+ readonly UNHANDLED_CHILD_STEPS: "UNHANDLED_CHILD_STEPS";
9
+ };
10
+ export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
11
+ /**
12
+ * Metadata about the context where an error occurred.
13
+ */
14
+ export interface ErrorMetadata {
15
+ jobId?: string;
16
+ stepId?: string;
17
+ parentStepId?: string | null;
18
+ actionName?: string;
19
+ stepName?: string;
20
+ }
21
+ /**
22
+ * Base class for all built-in errors in Duron.
23
+ * All errors include a cause property that can be serialized.
24
+ */
1
25
  export declare abstract class DuronError extends Error {
26
+ /**
27
+ * Error code for type checking without instanceof.
28
+ */
29
+ readonly code: ErrorCode;
30
+ /**
31
+ * Whether this error should prevent retries.
32
+ */
33
+ readonly nonRetriable: boolean;
34
+ /**
35
+ * The underlying cause of the error, if any.
36
+ *
37
+ * This will be serialized and stored in the database.
38
+ */
2
39
  readonly cause?: unknown;
40
+ /**
41
+ * Metadata about the context where the error occurred.
42
+ */
43
+ metadata: ErrorMetadata;
3
44
  constructor(message: string, options?: {
45
+ /**
46
+ * The underlying cause of the error, if any.
47
+ *
48
+ * This will be serialized and stored in the database.
49
+ */
4
50
  cause?: unknown;
51
+ /**
52
+ * Metadata about the context where the error occurred.
53
+ */
54
+ metadata?: ErrorMetadata;
5
55
  });
56
+ /**
57
+ * Set or update metadata about the context where the error occurred.
58
+ * Merges with existing metadata (new values override existing ones).
59
+ *
60
+ * @param metadata - The metadata to set or merge
61
+ * @returns this - Returns the error instance for chaining
62
+ */
63
+ setMetadata(metadata: ErrorMetadata): this;
6
64
  }
65
+ /**
66
+ * Error thrown when attempting to execute a step that has already been executed.
67
+ */
7
68
  export declare class StepAlreadyExecutedError extends DuronError {
69
+ readonly code: "STEP_ALREADY_EXECUTED";
70
+ readonly nonRetriable = true;
71
+ /**
72
+ * Create a new StepAlreadyExecutedError.
73
+ *
74
+ * @param stepName - The name of the step that was already executed
75
+ * @param jobId - The ID of the job containing the step
76
+ * @param actionName - The name of the action containing the step
77
+ */
8
78
  constructor(stepName: string, jobId: string, actionName: string);
9
79
  }
80
+ /**
81
+ * NonRetriableError indicates that a step should not be retried.
82
+ *
83
+ * If a step handler throws this error, the step will fail immediately
84
+ * without retrying, even if retry options are configured.
85
+ */
10
86
  export declare class NonRetriableError extends DuronError {
87
+ readonly code: ErrorCode;
88
+ readonly nonRetriable = true;
11
89
  }
90
+ /**
91
+ * Error thrown when an action exceeds its timeout.
92
+ */
12
93
  export declare class ActionTimeoutError extends DuronError {
13
- constructor(actionName: string, timeoutMs: number, options?: {
94
+ readonly code: "ACTION_TIMEOUT";
95
+ readonly nonRetriable = true;
96
+ /**
97
+ * Create a new ActionTimeoutError.
98
+ *
99
+ * @param actionName - The name of the action that timed out
100
+ * @param jobId - The ID of the job containing the action
101
+ * @param timeoutMs - The timeout value in milliseconds
102
+ * @param options - Optional error options including cause
103
+ */
104
+ constructor(actionName: string, jobId: string, timeoutMs: number, options?: {
14
105
  cause?: unknown;
15
106
  });
16
107
  }
108
+ /**
109
+ * Error thrown when a step exceeds its timeout.
110
+ */
17
111
  export declare class StepTimeoutError extends DuronError {
112
+ readonly code: "STEP_TIMEOUT";
113
+ readonly nonRetriable = false;
114
+ /**
115
+ * Create a new StepTimeoutError.
116
+ *
117
+ * @param stepName - The name of the step that timed out
118
+ * @param jobId - The ID of the job containing the step
119
+ * @param timeoutMs - The timeout value in milliseconds
120
+ * @param options - Optional error options including cause and additional metadata
121
+ */
18
122
  constructor(stepName: string, jobId: string, timeoutMs: number, options?: {
19
123
  cause?: unknown;
124
+ stepId?: string;
125
+ parentStepId?: string | null;
126
+ actionName?: string;
20
127
  });
21
128
  }
129
+ /**
130
+ * Error thrown when an action is cancelled.
131
+ */
22
132
  export declare class ActionCancelError extends DuronError {
133
+ readonly code: "ACTION_CANCEL";
134
+ readonly nonRetriable = true;
135
+ /**
136
+ * Create a new ActionCancelError.
137
+ *
138
+ * @param actionName - The name of the action that was cancelled
139
+ * @param jobId - The ID of the job containing the action
140
+ * @param options - Optional error options including cause
141
+ */
23
142
  constructor(actionName: string, jobId: string, options?: {
24
143
  cause?: unknown;
25
144
  });
26
145
  }
146
+ /**
147
+ * Error thrown when a parent step completes with unhandled (non-awaited) child steps.
148
+ *
149
+ * This error indicates a bug in the action handler where child steps were started
150
+ * but not properly awaited. All child steps must be awaited before the parent returns.
151
+ */
27
152
  export declare class UnhandledChildStepsError extends NonRetriableError {
28
- readonly stepName: string;
153
+ readonly code: "UNHANDLED_CHILD_STEPS";
154
+ /**
155
+ * The number of unhandled child steps.
156
+ */
29
157
  readonly pendingCount: number;
30
- constructor(stepName: string, pendingCount: number);
158
+ /**
159
+ * Create a new UnhandledChildStepsError.
160
+ *
161
+ * @param stepName - The name of the parent step
162
+ * @param pendingCount - The number of unhandled child steps
163
+ * @param options - Optional metadata
164
+ */
165
+ constructor(stepName: string, pendingCount: number, options?: {
166
+ stepId?: string;
167
+ parentStepId?: string | null;
168
+ jobId?: string;
169
+ actionName?: string;
170
+ });
31
171
  }
172
+ /**
173
+ * Checks if an error is a DuronError instance.
174
+ */
32
175
  export declare function isDuronError(error: unknown): error is DuronError;
176
+ /**
177
+ * Checks if an error is a NonRetriableError instance.
178
+ */
33
179
  export declare function isNonRetriableError(error: unknown): error is NonRetriableError;
180
+ /**
181
+ * Checks if an error is an UnhandledChildStepsError instance.
182
+ */
34
183
  export declare function isUnhandledChildStepsError(error: unknown): error is UnhandledChildStepsError;
184
+ /**
185
+ * Checks if an error is a timeout error (ActionTimeoutError or StepTimeoutError).
186
+ */
35
187
  export declare function isTimeoutError(error: unknown): error is ActionTimeoutError | StepTimeoutError;
188
+ /**
189
+ * Checks if an error is a cancel error (ActionCancelError or StepCancelError).
190
+ */
36
191
  export declare function isCancelError(error: unknown): error is ActionCancelError;
37
192
  export type SerializableError = {
38
193
  name: string;
39
194
  message: string;
195
+ code?: ErrorCode;
196
+ nonRetriable?: boolean;
197
+ metadata?: ErrorMetadata;
40
198
  cause?: unknown;
41
199
  stack?: string;
42
200
  };
43
- export declare function serializeError(error: unknown): {
44
- name: string;
45
- message: string;
46
- cause?: unknown;
47
- stack?: string;
48
- };
201
+ /**
202
+ * Serializes an error for storage in the database.
203
+ * Handles DuronError instances specially to preserve their type information.
204
+ */
205
+ export declare function serializeError(error: unknown): SerializableError;
49
206
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAIA,8BAAsB,UAAW,SAAQ,KAAK;IAM5C,SAAyB,KAAK,CAAC,EAAE,OAAO,CAAA;gBAGtC,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QAMR,KAAK,CAAC,EAAE,OAAO,CAAA;KAChB;CAWJ;AAKD,qBAAa,wBAAyB,SAAQ,UAAU;gBAQ1C,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;CAGhE;AAQD,qBAAa,iBAAkB,SAAQ,UAAU;CAEhD;AAKD,qBAAa,kBAAmB,SAAQ,UAAU;gBAS9C,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,OAAO,CAAA;KAChB;CAIJ;AAKD,qBAAa,gBAAiB,SAAQ,UAAU;gBAU5C,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,OAAO,CAAA;KAChB;CAIJ;AAKD,qBAAa,iBAAkB,SAAQ,UAAU;gBAS7C,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,OAAO,CAAA;KAChB;CAIJ;AAQD,qBAAa,wBAAyB,SAAQ,iBAAiB;IAI7D,SAAgB,QAAQ,EAAE,MAAM,CAAA;IAKhC,SAAgB,YAAY,EAAE,MAAM,CAAA;gBAQxB,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;CAOnD;AAKD,wBAAgB,YAAY,CA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GAAG,KAAK,IAAI,iBAAiB,CAExE;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,SAAS,CAAA;IAChB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,QAAQ,CAAC,EAAE,aAAa,CAAA;IACxB,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,CAAA;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,iBAAiB,CA0ChE"}
package/dist/errors.js CHANGED
@@ -1,76 +1,246 @@
1
+ // Error codes for type checking without instanceof
2
+ export const ERROR_CODES = {
3
+ DURON_ERROR: 'DURON_ERROR',
4
+ STEP_ALREADY_EXECUTED: 'STEP_ALREADY_EXECUTED',
5
+ NON_RETRIABLE: 'NON_RETRIABLE',
6
+ ACTION_TIMEOUT: 'ACTION_TIMEOUT',
7
+ STEP_TIMEOUT: 'STEP_TIMEOUT',
8
+ ACTION_CANCEL: 'ACTION_CANCEL',
9
+ UNHANDLED_CHILD_STEPS: 'UNHANDLED_CHILD_STEPS',
10
+ };
11
+ /**
12
+ * Base class for all built-in errors in Duron.
13
+ * All errors include a cause property that can be serialized.
14
+ */
1
15
  export class DuronError extends Error {
16
+ /**
17
+ * Error code for type checking without instanceof.
18
+ */
19
+ code = ERROR_CODES.DURON_ERROR;
20
+ /**
21
+ * Whether this error should prevent retries.
22
+ */
23
+ nonRetriable = false;
24
+ /**
25
+ * The underlying cause of the error, if any.
26
+ *
27
+ * This will be serialized and stored in the database.
28
+ */
2
29
  cause;
30
+ /**
31
+ * Metadata about the context where the error occurred.
32
+ */
33
+ metadata;
3
34
  constructor(message, options) {
4
35
  super(message);
5
36
  this.cause = options?.cause;
37
+ this.metadata = options?.metadata ?? {};
38
+ // Set the name to the class name
6
39
  this.name = this.constructor.name;
40
+ // Ensure stack trace points to the error location
7
41
  if (Error.captureStackTrace) {
8
42
  Error.captureStackTrace(this, this.constructor);
9
43
  }
10
44
  }
45
+ /**
46
+ * Set or update metadata about the context where the error occurred.
47
+ * Merges with existing metadata (new values override existing ones).
48
+ *
49
+ * @param metadata - The metadata to set or merge
50
+ * @returns this - Returns the error instance for chaining
51
+ */
52
+ setMetadata(metadata) {
53
+ this.metadata = { ...this.metadata, ...metadata };
54
+ return this;
55
+ }
11
56
  }
57
+ /**
58
+ * Error thrown when attempting to execute a step that has already been executed.
59
+ */
12
60
  export class StepAlreadyExecutedError extends DuronError {
61
+ code = ERROR_CODES.STEP_ALREADY_EXECUTED;
62
+ nonRetriable = true;
63
+ /**
64
+ * Create a new StepAlreadyExecutedError.
65
+ *
66
+ * @param stepName - The name of the step that was already executed
67
+ * @param jobId - The ID of the job containing the step
68
+ * @param actionName - The name of the action containing the step
69
+ */
13
70
  constructor(stepName, jobId, actionName) {
14
- super(`Step "${stepName}" has already been executed for job "${jobId}" and action "${actionName}"`);
71
+ super(`Step "${stepName}" has already been executed for job "${jobId}" and action "${actionName}"`, {
72
+ metadata: { stepName, jobId, actionName },
73
+ });
15
74
  }
16
75
  }
76
+ /**
77
+ * NonRetriableError indicates that a step should not be retried.
78
+ *
79
+ * If a step handler throws this error, the step will fail immediately
80
+ * without retrying, even if retry options are configured.
81
+ */
17
82
  export class NonRetriableError extends DuronError {
83
+ code = ERROR_CODES.NON_RETRIABLE;
84
+ nonRetriable = true;
18
85
  }
86
+ /**
87
+ * Error thrown when an action exceeds its timeout.
88
+ */
19
89
  export class ActionTimeoutError extends DuronError {
20
- constructor(actionName, timeoutMs, options) {
21
- super(`Action "${actionName}" timed out after ${timeoutMs}ms`, options);
90
+ code = ERROR_CODES.ACTION_TIMEOUT;
91
+ nonRetriable = true;
92
+ /**
93
+ * Create a new ActionTimeoutError.
94
+ *
95
+ * @param actionName - The name of the action that timed out
96
+ * @param jobId - The ID of the job containing the action
97
+ * @param timeoutMs - The timeout value in milliseconds
98
+ * @param options - Optional error options including cause
99
+ */
100
+ constructor(actionName, jobId, timeoutMs, options) {
101
+ super(`Action "${actionName}" in job "${jobId}" timed out after ${timeoutMs}ms`, {
102
+ cause: options?.cause,
103
+ metadata: { actionName, jobId },
104
+ });
22
105
  }
23
106
  }
107
+ /**
108
+ * Error thrown when a step exceeds its timeout.
109
+ */
24
110
  export class StepTimeoutError extends DuronError {
111
+ code = ERROR_CODES.STEP_TIMEOUT;
112
+ nonRetriable = false;
113
+ /**
114
+ * Create a new StepTimeoutError.
115
+ *
116
+ * @param stepName - The name of the step that timed out
117
+ * @param jobId - The ID of the job containing the step
118
+ * @param timeoutMs - The timeout value in milliseconds
119
+ * @param options - Optional error options including cause and additional metadata
120
+ */
25
121
  constructor(stepName, jobId, timeoutMs, options) {
26
- super(`Step "${stepName}" in job "${jobId}" timed out after ${timeoutMs}ms`, options);
122
+ super(`Step "${stepName}" in job "${jobId}" timed out after ${timeoutMs}ms`, {
123
+ cause: options?.cause,
124
+ metadata: {
125
+ stepName,
126
+ jobId,
127
+ stepId: options?.stepId,
128
+ parentStepId: options?.parentStepId,
129
+ actionName: options?.actionName,
130
+ },
131
+ });
27
132
  }
28
133
  }
134
+ /**
135
+ * Error thrown when an action is cancelled.
136
+ */
29
137
  export class ActionCancelError extends DuronError {
138
+ code = ERROR_CODES.ACTION_CANCEL;
139
+ nonRetriable = true;
140
+ /**
141
+ * Create a new ActionCancelError.
142
+ *
143
+ * @param actionName - The name of the action that was cancelled
144
+ * @param jobId - The ID of the job containing the action
145
+ * @param options - Optional error options including cause
146
+ */
30
147
  constructor(actionName, jobId, options) {
31
- super(`Action "${actionName}" in job "${jobId}" was cancelled`, options);
148
+ super(`Action "${actionName}" in job "${jobId}" was cancelled`, {
149
+ cause: options?.cause,
150
+ metadata: { actionName, jobId },
151
+ });
32
152
  }
33
153
  }
154
+ /**
155
+ * Error thrown when a parent step completes with unhandled (non-awaited) child steps.
156
+ *
157
+ * This error indicates a bug in the action handler where child steps were started
158
+ * but not properly awaited. All child steps must be awaited before the parent returns.
159
+ */
34
160
  export class UnhandledChildStepsError extends NonRetriableError {
35
- stepName;
161
+ code = ERROR_CODES.UNHANDLED_CHILD_STEPS;
162
+ /**
163
+ * The number of unhandled child steps.
164
+ */
36
165
  pendingCount;
37
- constructor(stepName, pendingCount) {
38
- super(`Parent step "${stepName}" completed with ${pendingCount} unhandled child step(s). All child steps must be awaited before the parent returns.`);
39
- this.stepName = stepName;
166
+ /**
167
+ * Create a new UnhandledChildStepsError.
168
+ *
169
+ * @param stepName - The name of the parent step
170
+ * @param pendingCount - The number of unhandled child steps
171
+ * @param options - Optional metadata
172
+ */
173
+ constructor(stepName, pendingCount, options) {
174
+ super(`Parent step "${stepName}" completed with ${pendingCount} unhandled child step(s). All child steps must be awaited before the parent returns.`, {
175
+ metadata: {
176
+ stepName,
177
+ stepId: options?.stepId,
178
+ parentStepId: options?.parentStepId,
179
+ jobId: options?.jobId,
180
+ actionName: options?.actionName,
181
+ },
182
+ });
40
183
  this.pendingCount = pendingCount;
41
184
  }
42
185
  }
186
+ /**
187
+ * Checks if an error is a DuronError instance.
188
+ */
43
189
  export function isDuronError(error) {
44
- return error instanceof DuronError;
190
+ const code = error?.code;
191
+ return code !== undefined && Object.values(ERROR_CODES).includes(code);
45
192
  }
193
+ /**
194
+ * Checks if an error is a NonRetriableError instance.
195
+ */
46
196
  export function isNonRetriableError(error) {
47
- return (error instanceof NonRetriableError ||
48
- error instanceof ActionCancelError ||
49
- error instanceof ActionTimeoutError ||
50
- error instanceof UnhandledChildStepsError);
197
+ return error?.nonRetriable === true;
51
198
  }
199
+ /**
200
+ * Checks if an error is an UnhandledChildStepsError instance.
201
+ */
52
202
  export function isUnhandledChildStepsError(error) {
53
- return error instanceof UnhandledChildStepsError;
203
+ return error?.code === ERROR_CODES.UNHANDLED_CHILD_STEPS;
54
204
  }
205
+ /**
206
+ * Checks if an error is a timeout error (ActionTimeoutError or StepTimeoutError).
207
+ */
55
208
  export function isTimeoutError(error) {
56
- return error instanceof ActionTimeoutError || error instanceof StepTimeoutError;
209
+ const code = error?.code;
210
+ return code === ERROR_CODES.ACTION_TIMEOUT || code === ERROR_CODES.STEP_TIMEOUT;
57
211
  }
212
+ /**
213
+ * Checks if an error is a cancel error (ActionCancelError or StepCancelError).
214
+ */
58
215
  export function isCancelError(error) {
59
- return error instanceof ActionCancelError;
216
+ return error?.code === ERROR_CODES.ACTION_CANCEL;
60
217
  }
218
+ /**
219
+ * Serializes an error for storage in the database.
220
+ * Handles DuronError instances specially to preserve their type information.
221
+ */
61
222
  export function serializeError(error) {
62
- if (error instanceof StepTimeoutError || error instanceof ActionTimeoutError) {
223
+ const code = error?.code;
224
+ const nonRetriable = error?.nonRetriable;
225
+ const metadata = error?.metadata;
226
+ if (isTimeoutError(error)) {
63
227
  return {
64
228
  name: error.name,
65
229
  message: error.message,
230
+ code,
231
+ nonRetriable,
232
+ metadata,
66
233
  cause: error.cause,
67
234
  stack: undefined,
68
235
  };
69
236
  }
70
- if (error instanceof DuronError) {
237
+ if (isDuronError(error)) {
71
238
  return {
72
239
  name: error.name,
73
240
  message: error.message,
241
+ code,
242
+ nonRetriable,
243
+ metadata,
74
244
  cause: error.cause,
75
245
  stack: error.stack,
76
246
  };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import type { Action } from './action.js';
2
2
  import { Client, type ClientOptions } from './client.js';
3
- export { createStep, defineAction, type StepDefinition, type StepDefinitionHandlerContext, type StepDefinitionInput, } from './action.js';
3
+ export { createStep, defineAction, type StepDefinition, type StepDefinitionHandlerContext, type StepDefinitionInput, type StepNameContext, } from './action.js';
4
4
  export * from './client.js';
5
5
  export * from './constants.js';
6
6
  export { NonRetriableError, UnhandledChildStepsError } from './errors.js';
7
7
  export * from './server.js';
8
+ export type { TelemetryContext } from './step-manager.js';
8
9
  export * from './telemetry/index.js';
9
10
  export declare const duron: <TActions extends Record<string, Action<any, any, TVariables>>, TVariables = Record<string, unknown>>(options: ClientOptions<TActions, TVariables>) => Client<TActions, TVariables>;
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAA;AAExD,OAAO,EACL,UAAU,EACV,YAAY,EACZ,KAAK,cAAc,EACnB,KAAK,4BAA4B,EACjC,KAAK,mBAAmB,GACzB,MAAM,aAAa,CAAA;AACpB,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AACzE,cAAc,aAAa,CAAA;AAC3B,cAAc,sBAAsB,CAAA;AAEpC,eAAO,MAAM,KAAK,GAChB,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC,EAC7D,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAEpC,SAAS,aAAa,CAAC,QAAQ,EAAE,UAAU,CAAC,iCACA,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAA;AAExD,OAAO,EACL,UAAU,EACV,YAAY,EACZ,KAAK,cAAc,EACnB,KAAK,4BAA4B,EACjC,KAAK,mBAAmB,EACxB,KAAK,eAAe,GACrB,MAAM,aAAa,CAAA;AACpB,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAA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