pg-workflows 0.0.1-claimed → 0.1.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.
@@ -0,0 +1,202 @@
1
+ import { z } from "zod";
2
+ type WorkflowRun = {
3
+ id: string;
4
+ createdAt: Date;
5
+ updatedAt: Date;
6
+ resourceId: string | null;
7
+ workflowId: string;
8
+ status: "pending" | "running" | "paused" | "completed" | "failed" | "cancelled";
9
+ input: unknown;
10
+ output: unknown | null;
11
+ error: string | null;
12
+ currentStepId: string;
13
+ timeline: Record<string, unknown>;
14
+ pausedAt: Date | null;
15
+ resumedAt: Date | null;
16
+ completedAt: Date | null;
17
+ timeoutAt: Date | null;
18
+ retryCount: number;
19
+ maxRetries: number;
20
+ jobId: string | null;
21
+ };
22
+ declare enum WorkflowStatus {
23
+ PENDING = "pending",
24
+ RUNNING = "running",
25
+ PAUSED = "paused",
26
+ COMPLETED = "completed",
27
+ FAILED = "failed",
28
+ CANCELLED = "cancelled"
29
+ }
30
+ declare enum StepType {
31
+ PAUSE = "pause",
32
+ RUN = "run",
33
+ WAIT_FOR = "waitFor",
34
+ WAIT_UNTIL = "waitUntil"
35
+ }
36
+ type Parameters = z.ZodTypeAny;
37
+ type inferParameters<P extends Parameters> = P extends z.ZodTypeAny ? z.infer<P> : never;
38
+ type WorkflowOptions<I extends Parameters> = {
39
+ timeout?: number;
40
+ retries?: number;
41
+ inputSchema?: I;
42
+ };
43
+ interface WorkflowLogger {
44
+ log(message: string): void;
45
+ error(message: string, ...args: unknown[]): void;
46
+ }
47
+ type InternalWorkflowLoggerContext = {
48
+ runId?: string;
49
+ workflowId?: string;
50
+ };
51
+ interface InternalWorkflowLogger {
52
+ log(message: string, context?: InternalWorkflowLoggerContext): void;
53
+ error(message: string, error: Error, context?: InternalWorkflowLoggerContext): void;
54
+ }
55
+ type StepContext = {
56
+ run: <T>(stepId: string, handler: () => Promise<T>) => Promise<T>;
57
+ waitFor: <T extends Parameters>(stepId: string, { eventName, timeout, schema }: {
58
+ eventName: string;
59
+ timeout?: number;
60
+ schema?: T;
61
+ }) => Promise<inferParameters<T>>;
62
+ waitUntil: (stepId: string, { date }: {
63
+ date: Date;
64
+ }) => Promise<void>;
65
+ pause: (stepId: string) => Promise<void>;
66
+ };
67
+ type WorkflowContext<T extends Parameters = Parameters> = {
68
+ input: T;
69
+ step: StepContext;
70
+ workflowId: string;
71
+ runId: string;
72
+ timeline: Record<string, unknown>;
73
+ logger: WorkflowLogger;
74
+ };
75
+ type WorkflowDefinition<T extends Parameters = Parameters> = {
76
+ id: string;
77
+ handler: (context: WorkflowContext<inferParameters<T>>) => Promise<unknown>;
78
+ inputSchema?: T;
79
+ timeout?: number;
80
+ retries?: number;
81
+ };
82
+ type InternalStepDefinition = {
83
+ id: string;
84
+ type: StepType;
85
+ conditional: boolean;
86
+ loop: boolean;
87
+ isDynamic: boolean;
88
+ };
89
+ type InternalWorkflowDefinition<T extends Parameters = Parameters> = WorkflowDefinition<T> & {
90
+ steps: InternalStepDefinition[];
91
+ };
92
+ type WorkflowRunProgress = WorkflowRun & {
93
+ completionPercentage: number;
94
+ totalSteps: number;
95
+ completedSteps: number;
96
+ };
97
+ declare function workflow<I extends Parameters>(id: string, handler: (context: WorkflowContext<inferParameters<I>>) => Promise<unknown>, { inputSchema, timeout, retries }?: WorkflowOptions<I>): WorkflowDefinition<I>;
98
+ import { Db, PgBoss } from "pg-boss";
99
+ declare class WorkflowEngine {
100
+ private boss;
101
+ private db;
102
+ private unregisteredWorkflows;
103
+ private _started;
104
+ workflows: Map<string, InternalWorkflowDefinition>;
105
+ private logger;
106
+ constructor({ workflows, logger, boss }?: Partial<{
107
+ workflows: WorkflowDefinition[];
108
+ logger: WorkflowLogger;
109
+ boss: PgBoss;
110
+ }>);
111
+ start(asEngine?: boolean, { batchSize }?: {
112
+ batchSize?: number;
113
+ }): Promise<void>;
114
+ stop(): Promise<void>;
115
+ registerWorkflow(definition: WorkflowDefinition): Promise<WorkflowEngine>;
116
+ unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
117
+ unregisterAllWorkflows(): Promise<WorkflowEngine>;
118
+ startWorkflow({ resourceId, workflowId, input, options }: {
119
+ resourceId?: string;
120
+ workflowId: string;
121
+ input: unknown;
122
+ options?: {
123
+ timeout?: number;
124
+ retries?: number;
125
+ expireInSeconds?: number;
126
+ batchSize?: number;
127
+ };
128
+ }): Promise<WorkflowRun>;
129
+ pauseWorkflow({ runId, resourceId }: {
130
+ runId: string;
131
+ resourceId?: string;
132
+ }): Promise<WorkflowRun>;
133
+ resumeWorkflow({ runId, resourceId, options }: {
134
+ runId: string;
135
+ resourceId?: string;
136
+ options?: {
137
+ expireInSeconds?: number;
138
+ };
139
+ }): Promise<WorkflowRun>;
140
+ cancelWorkflow({ runId, resourceId }: {
141
+ runId: string;
142
+ resourceId?: string;
143
+ }): Promise<WorkflowRun>;
144
+ triggerEvent({ runId, resourceId, eventName, data, options }: {
145
+ runId: string;
146
+ resourceId?: string;
147
+ eventName: string;
148
+ data?: Record<string, unknown>;
149
+ options?: {
150
+ expireInSeconds?: number;
151
+ };
152
+ }): Promise<WorkflowRun>;
153
+ getRun({ runId, resourceId }: {
154
+ runId: string;
155
+ resourceId?: string;
156
+ }, { exclusiveLock, db }?: {
157
+ exclusiveLock?: boolean;
158
+ db?: Db;
159
+ }): Promise<WorkflowRun>;
160
+ updateRun({ runId, resourceId, data }: {
161
+ runId: string;
162
+ resourceId?: string;
163
+ data: Partial<WorkflowRun>;
164
+ }, { db }?: {
165
+ db?: Db;
166
+ }): Promise<WorkflowRun>;
167
+ checkProgress({ runId, resourceId }: {
168
+ runId: string;
169
+ resourceId?: string;
170
+ }): Promise<WorkflowRunProgress>;
171
+ private handleWorkflowRun;
172
+ private runStep;
173
+ private waitForEvent;
174
+ private pauseStep;
175
+ private waitUntil;
176
+ private checkIfHasStarted;
177
+ private buildLogger;
178
+ getRuns({ resourceId, startingAfter, endingBefore, limit, statuses, workflowId }: {
179
+ resourceId?: string;
180
+ startingAfter?: string | null;
181
+ endingBefore?: string | null;
182
+ limit?: number;
183
+ statuses?: WorkflowStatus[];
184
+ workflowId?: string;
185
+ }): Promise<{
186
+ items: WorkflowRun[];
187
+ nextCursor: string | null;
188
+ prevCursor: string | null;
189
+ hasMore: boolean;
190
+ hasPrev: boolean;
191
+ }>;
192
+ }
193
+ declare class WorkflowEngineError extends Error {
194
+ readonly workflowId?: string | undefined;
195
+ readonly runId?: string | undefined;
196
+ readonly cause: Error | undefined;
197
+ constructor(message: string, workflowId?: string | undefined, runId?: string | undefined, cause?: Error | undefined);
198
+ }
199
+ declare class WorkflowRunNotFoundError extends WorkflowEngineError {
200
+ constructor(runId?: string, workflowId?: string);
201
+ }
202
+ export { workflow, inferParameters, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowOptions, WorkflowLogger, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepContext, Parameters, InternalWorkflowLoggerContext, InternalWorkflowLogger, InternalWorkflowDefinition, InternalStepDefinition };
@@ -0,0 +1,202 @@
1
+ import { z } from "zod";
2
+ type WorkflowRun = {
3
+ id: string;
4
+ createdAt: Date;
5
+ updatedAt: Date;
6
+ resourceId: string | null;
7
+ workflowId: string;
8
+ status: "pending" | "running" | "paused" | "completed" | "failed" | "cancelled";
9
+ input: unknown;
10
+ output: unknown | null;
11
+ error: string | null;
12
+ currentStepId: string;
13
+ timeline: Record<string, unknown>;
14
+ pausedAt: Date | null;
15
+ resumedAt: Date | null;
16
+ completedAt: Date | null;
17
+ timeoutAt: Date | null;
18
+ retryCount: number;
19
+ maxRetries: number;
20
+ jobId: string | null;
21
+ };
22
+ declare enum WorkflowStatus {
23
+ PENDING = "pending",
24
+ RUNNING = "running",
25
+ PAUSED = "paused",
26
+ COMPLETED = "completed",
27
+ FAILED = "failed",
28
+ CANCELLED = "cancelled"
29
+ }
30
+ declare enum StepType {
31
+ PAUSE = "pause",
32
+ RUN = "run",
33
+ WAIT_FOR = "waitFor",
34
+ WAIT_UNTIL = "waitUntil"
35
+ }
36
+ type Parameters = z.ZodTypeAny;
37
+ type inferParameters<P extends Parameters> = P extends z.ZodTypeAny ? z.infer<P> : never;
38
+ type WorkflowOptions<I extends Parameters> = {
39
+ timeout?: number;
40
+ retries?: number;
41
+ inputSchema?: I;
42
+ };
43
+ interface WorkflowLogger {
44
+ log(message: string): void;
45
+ error(message: string, ...args: unknown[]): void;
46
+ }
47
+ type InternalWorkflowLoggerContext = {
48
+ runId?: string;
49
+ workflowId?: string;
50
+ };
51
+ interface InternalWorkflowLogger {
52
+ log(message: string, context?: InternalWorkflowLoggerContext): void;
53
+ error(message: string, error: Error, context?: InternalWorkflowLoggerContext): void;
54
+ }
55
+ type StepContext = {
56
+ run: <T>(stepId: string, handler: () => Promise<T>) => Promise<T>;
57
+ waitFor: <T extends Parameters>(stepId: string, { eventName, timeout, schema }: {
58
+ eventName: string;
59
+ timeout?: number;
60
+ schema?: T;
61
+ }) => Promise<inferParameters<T>>;
62
+ waitUntil: (stepId: string, { date }: {
63
+ date: Date;
64
+ }) => Promise<void>;
65
+ pause: (stepId: string) => Promise<void>;
66
+ };
67
+ type WorkflowContext<T extends Parameters = Parameters> = {
68
+ input: T;
69
+ step: StepContext;
70
+ workflowId: string;
71
+ runId: string;
72
+ timeline: Record<string, unknown>;
73
+ logger: WorkflowLogger;
74
+ };
75
+ type WorkflowDefinition<T extends Parameters = Parameters> = {
76
+ id: string;
77
+ handler: (context: WorkflowContext<inferParameters<T>>) => Promise<unknown>;
78
+ inputSchema?: T;
79
+ timeout?: number;
80
+ retries?: number;
81
+ };
82
+ type InternalStepDefinition = {
83
+ id: string;
84
+ type: StepType;
85
+ conditional: boolean;
86
+ loop: boolean;
87
+ isDynamic: boolean;
88
+ };
89
+ type InternalWorkflowDefinition<T extends Parameters = Parameters> = WorkflowDefinition<T> & {
90
+ steps: InternalStepDefinition[];
91
+ };
92
+ type WorkflowRunProgress = WorkflowRun & {
93
+ completionPercentage: number;
94
+ totalSteps: number;
95
+ completedSteps: number;
96
+ };
97
+ declare function workflow<I extends Parameters>(id: string, handler: (context: WorkflowContext<inferParameters<I>>) => Promise<unknown>, { inputSchema, timeout, retries }?: WorkflowOptions<I>): WorkflowDefinition<I>;
98
+ import { Db, PgBoss } from "pg-boss";
99
+ declare class WorkflowEngine {
100
+ private boss;
101
+ private db;
102
+ private unregisteredWorkflows;
103
+ private _started;
104
+ workflows: Map<string, InternalWorkflowDefinition>;
105
+ private logger;
106
+ constructor({ workflows, logger, boss }?: Partial<{
107
+ workflows: WorkflowDefinition[];
108
+ logger: WorkflowLogger;
109
+ boss: PgBoss;
110
+ }>);
111
+ start(asEngine?: boolean, { batchSize }?: {
112
+ batchSize?: number;
113
+ }): Promise<void>;
114
+ stop(): Promise<void>;
115
+ registerWorkflow(definition: WorkflowDefinition): Promise<WorkflowEngine>;
116
+ unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
117
+ unregisterAllWorkflows(): Promise<WorkflowEngine>;
118
+ startWorkflow({ resourceId, workflowId, input, options }: {
119
+ resourceId?: string;
120
+ workflowId: string;
121
+ input: unknown;
122
+ options?: {
123
+ timeout?: number;
124
+ retries?: number;
125
+ expireInSeconds?: number;
126
+ batchSize?: number;
127
+ };
128
+ }): Promise<WorkflowRun>;
129
+ pauseWorkflow({ runId, resourceId }: {
130
+ runId: string;
131
+ resourceId?: string;
132
+ }): Promise<WorkflowRun>;
133
+ resumeWorkflow({ runId, resourceId, options }: {
134
+ runId: string;
135
+ resourceId?: string;
136
+ options?: {
137
+ expireInSeconds?: number;
138
+ };
139
+ }): Promise<WorkflowRun>;
140
+ cancelWorkflow({ runId, resourceId }: {
141
+ runId: string;
142
+ resourceId?: string;
143
+ }): Promise<WorkflowRun>;
144
+ triggerEvent({ runId, resourceId, eventName, data, options }: {
145
+ runId: string;
146
+ resourceId?: string;
147
+ eventName: string;
148
+ data?: Record<string, unknown>;
149
+ options?: {
150
+ expireInSeconds?: number;
151
+ };
152
+ }): Promise<WorkflowRun>;
153
+ getRun({ runId, resourceId }: {
154
+ runId: string;
155
+ resourceId?: string;
156
+ }, { exclusiveLock, db }?: {
157
+ exclusiveLock?: boolean;
158
+ db?: Db;
159
+ }): Promise<WorkflowRun>;
160
+ updateRun({ runId, resourceId, data }: {
161
+ runId: string;
162
+ resourceId?: string;
163
+ data: Partial<WorkflowRun>;
164
+ }, { db }?: {
165
+ db?: Db;
166
+ }): Promise<WorkflowRun>;
167
+ checkProgress({ runId, resourceId }: {
168
+ runId: string;
169
+ resourceId?: string;
170
+ }): Promise<WorkflowRunProgress>;
171
+ private handleWorkflowRun;
172
+ private runStep;
173
+ private waitForEvent;
174
+ private pauseStep;
175
+ private waitUntil;
176
+ private checkIfHasStarted;
177
+ private buildLogger;
178
+ getRuns({ resourceId, startingAfter, endingBefore, limit, statuses, workflowId }: {
179
+ resourceId?: string;
180
+ startingAfter?: string | null;
181
+ endingBefore?: string | null;
182
+ limit?: number;
183
+ statuses?: WorkflowStatus[];
184
+ workflowId?: string;
185
+ }): Promise<{
186
+ items: WorkflowRun[];
187
+ nextCursor: string | null;
188
+ prevCursor: string | null;
189
+ hasMore: boolean;
190
+ hasPrev: boolean;
191
+ }>;
192
+ }
193
+ declare class WorkflowEngineError extends Error {
194
+ readonly workflowId?: string | undefined;
195
+ readonly runId?: string | undefined;
196
+ readonly cause: Error | undefined;
197
+ constructor(message: string, workflowId?: string | undefined, runId?: string | undefined, cause?: Error | undefined);
198
+ }
199
+ declare class WorkflowRunNotFoundError extends WorkflowEngineError {
200
+ constructor(runId?: string, workflowId?: string);
201
+ }
202
+ export { workflow, inferParameters, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowOptions, WorkflowLogger, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepContext, Parameters, InternalWorkflowLoggerContext, InternalWorkflowLogger, InternalWorkflowDefinition, InternalStepDefinition };