pg-workflows 0.13.2 → 0.14.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.
@@ -79,7 +79,7 @@ import { PgBoss } from "pg-boss";
79
79
 
80
80
  // src/db/migration.ts
81
81
  var MIGRATION_LOCK_ID = 738291645;
82
- var CURRENT_SCHEMA_VERSION = 5;
82
+ var CURRENT_SCHEMA_VERSION = 6;
83
83
  async function runMigrations(db) {
84
84
  if (await isSchemaUpToDate(db)) {
85
85
  return;
@@ -155,6 +155,9 @@ async function runMigrations(db) {
155
155
  if (currentVersion < 5) {
156
156
  commands.push("ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS scheduled_at timestamp with time zone");
157
157
  }
158
+ if (currentVersion < 6) {
159
+ commands.push("ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS priority integer DEFAULT 0 NOT NULL");
160
+ }
158
161
  if (currentVersion === 0) {
159
162
  commands.push(`INSERT INTO workflow_schema_version (version) VALUES (${CURRENT_SCHEMA_VERSION})`);
160
163
  } else {
@@ -216,6 +219,7 @@ function mapRowToWorkflowRun(row) {
216
219
  timeoutAt: row.timeout_at ? new Date(row.timeout_at) : null,
217
220
  retryCount: row.retry_count,
218
221
  maxRetries: row.max_retries,
222
+ priority: row.priority,
219
223
  jobId: row.job_id,
220
224
  idempotencyKey: row.idempotency_key,
221
225
  parentRunId: row.parent_run_id,
@@ -231,6 +235,7 @@ async function insertWorkflowRun({
231
235
  status,
232
236
  input,
233
237
  maxRetries,
238
+ priority,
234
239
  timeoutAt,
235
240
  idempotencyKey,
236
241
  parentRunId,
@@ -248,6 +253,7 @@ async function insertWorkflowRun({
248
253
  status,
249
254
  input,
250
255
  max_retries,
256
+ priority,
251
257
  timeout_at,
252
258
  created_at,
253
259
  updated_at,
@@ -259,7 +265,7 @@ async function insertWorkflowRun({
259
265
  parent_resource_id,
260
266
  scheduled_at
261
267
  )
262
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
268
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)
263
269
  ON CONFLICT (idempotency_key) WHERE idempotency_key IS NOT NULL DO NOTHING
264
270
  RETURNING *`, [
265
271
  runId,
@@ -269,6 +275,7 @@ async function insertWorkflowRun({
269
275
  status,
270
276
  JSON.stringify(input),
271
277
  maxRetries,
278
+ priority,
272
279
  timeoutAt,
273
280
  now,
274
281
  now,
@@ -511,6 +518,30 @@ async function withPostgresTransaction(db, callback, pool) {
511
518
  }
512
519
  }
513
520
 
521
+ // src/priority.ts
522
+ var PRIORITY_LEVELS = { high: 100, normal: 0, low: -100 };
523
+ function priorityToInt(value) {
524
+ if (typeof value === "number") {
525
+ if (!Number.isInteger(value)) {
526
+ throw new WorkflowEngineError(`Invalid priority ${value}: numeric priority must be an integer.`);
527
+ }
528
+ return value;
529
+ }
530
+ const mapped = PRIORITY_LEVELS[value];
531
+ if (mapped === undefined) {
532
+ throw new WorkflowEngineError(`Invalid priority "${value}": expected one of ${Object.keys(PRIORITY_LEVELS).map((k) => `"${k}"`).join(", ")} or an integer.`);
533
+ }
534
+ return mapped;
535
+ }
536
+ function resolvePriority(...candidates) {
537
+ for (const candidate of candidates) {
538
+ if (candidate !== undefined) {
539
+ return priorityToInt(candidate);
540
+ }
541
+ }
542
+ return PRIORITY_LEVELS.normal;
543
+ }
544
+
514
545
  // src/client.ts
515
546
  var LOG_PREFIX = "[WorkflowClient]";
516
547
  var defaultLogger = {
@@ -604,6 +635,7 @@ class WorkflowClient {
604
635
  status: "running" /* RUNNING */,
605
636
  input,
606
637
  maxRetries: options?.retries ?? 0,
638
+ priority: resolvePriority(options?.priority),
607
639
  timeoutAt,
608
640
  idempotencyKey
609
641
  }, _db);
@@ -617,6 +649,7 @@ class WorkflowClient {
617
649
  await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {
618
650
  startAfter: new Date,
619
651
  expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,
652
+ priority: insertedRun.priority,
620
653
  db: _db
621
654
  });
622
655
  }
@@ -645,7 +678,8 @@ class WorkflowClient {
645
678
  }
646
679
  };
647
680
  await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {
648
- expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds
681
+ expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,
682
+ priority: run.priority
649
683
  });
650
684
  this.logger.log(`${LOG_PREFIX} Event ${eventName} sent for workflow run ${runId}`);
651
685
  return run;
@@ -825,6 +859,7 @@ function createWorkflowRef(id, options) {
825
859
  inputSchema: options?.inputSchema,
826
860
  timeout: defineOptions?.timeout,
827
861
  retries: defineOptions?.retries,
862
+ priority: defineOptions?.priority,
828
863
  schedule: defineOptions?.schedule,
829
864
  timezone: defineOptions?.timezone
830
865
  });
@@ -836,12 +871,13 @@ function createWorkflowRef(id, options) {
836
871
  return ref;
837
872
  }
838
873
  function createWorkflowFactory(plugins = []) {
839
- const factory = (id, handler, { inputSchema, timeout, retries, schedule, timezone } = {}) => ({
874
+ const factory = (id, handler, { inputSchema, timeout, retries, priority, schedule, timezone } = {}) => ({
840
875
  id,
841
876
  handler,
842
877
  inputSchema,
843
878
  timeout,
844
879
  retries,
880
+ priority,
845
881
  schedule,
846
882
  timezone,
847
883
  plugins: plugins.length > 0 ? plugins : undefined
@@ -855,7 +891,7 @@ function createWorkflowFactory(plugins = []) {
855
891
  }
856
892
  var workflow = createWorkflowFactory();
857
893
 
858
- export { PAUSE_EVENT_NAME, WORKFLOW_RUN_QUEUE_NAME, WORKFLOW_RUN_DLQ_QUEUE_NAME, scheduleQueueNameFor, DEFAULT_PGBOSS_SCHEMA, invokeChildWorkflowTimelineKey, waitForTimelineKey, isInvokeChildWorkflowTimelineEntry, runMigrations, insertWorkflowRun, getWorkflowRun, getWorkflowLastRun, updateWorkflowRun, getWorkflowRuns, withPostgresTransaction, validateWorkflowId, validateResourceId, WorkflowEngineError, WorkflowRunNotFoundError, WorkflowStatus, STEP_BASE_METHOD_TYPES, WorkflowClient, createWorkflowRef, workflow };
894
+ export { PAUSE_EVENT_NAME, WORKFLOW_RUN_QUEUE_NAME, WORKFLOW_RUN_DLQ_QUEUE_NAME, scheduleQueueNameFor, DEFAULT_PGBOSS_SCHEMA, invokeChildWorkflowTimelineKey, waitForTimelineKey, isInvokeChildWorkflowTimelineEntry, runMigrations, insertWorkflowRun, getWorkflowRun, getWorkflowLastRun, updateWorkflowRun, getWorkflowRuns, withPostgresTransaction, validateWorkflowId, validateResourceId, WorkflowEngineError, WorkflowRunNotFoundError, resolvePriority, WorkflowStatus, STEP_BASE_METHOD_TYPES, WorkflowClient, createWorkflowRef, workflow };
859
895
 
860
- //# debugId=9CDBDF4EE7FE9C3F64756E2164756E21
861
- //# sourceMappingURL=chunk-hjycwrsq.js.map
896
+ //# debugId=DC21344869C0BC9E64756E2164756E21
897
+ //# sourceMappingURL=chunk-w3qcrrwx.js.map
@@ -0,0 +1,17 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["src/constants.ts", "src/error.ts", "src/types.ts", "src/client.ts", "src/db/migration.ts", "src/db/queries.ts", "src/priority.ts", "src/definition.ts"],
4
+ "sourcesContent": [
5
+ "export const PAUSE_EVENT_NAME = '__internal_pause';\nexport const WORKFLOW_RUN_QUEUE_NAME = 'workflow-run';\nexport const WORKFLOW_RUN_DLQ_QUEUE_NAME = 'workflow_run_dlq';\n// pg-boss queue names allow only alphanumeric, _, -, ., or / — keep the\n// prefix in that character set so any valid workflow id stays addressable.\nconst SCHEDULE_QUEUE_PREFIX = '__pgw_schedule_';\nexport const scheduleQueueNameFor = (workflowId: string): string =>\n `${SCHEDULE_QUEUE_PREFIX}${workflowId}`;\nexport const DEFAULT_PGBOSS_SCHEMA = 'pgboss_v12_pgworkflow';\nexport const MAX_WORKFLOW_ID_LENGTH = 256;\nexport const MAX_RESOURCE_ID_LENGTH = 256;\n\n// Per-step timeline key suffixes. The shared helpers below are the single\n// source of truth for engine + client so timeline-shape changes only need to\n// happen in one place.\nexport const INVOKE_CHILD_WORKFLOW_TIMELINE_SUFFIX = 'invoke-child-workflow';\nexport const WAIT_FOR_TIMELINE_SUFFIX = 'wait-for';\nexport const invokeChildWorkflowTimelineKey = (stepId: string) =>\n `${stepId}-${INVOKE_CHILD_WORKFLOW_TIMELINE_SUFFIX}`;\nexport const waitForTimelineKey = (stepId: string) => `${stepId}-${WAIT_FOR_TIMELINE_SUFFIX}`;\n\n/**\n * Type guard for `${stepId}-invoke-child-workflow` timeline entries. Used by the\n * engine to dispatch invoke-aware behavior and by the client's resume /\n * fast-forward no-op guards. Centralizing this keeps the timeline shape\n * contract in one place.\n */\nexport const isInvokeChildWorkflowTimelineEntry = (\n entry: unknown,\n): entry is { invokeChildWorkflow: { childRunId: string; childWorkflowId: string } } =>\n !!entry && typeof entry === 'object' && 'invokeChildWorkflow' in entry;\n",
6
+ "import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { MAX_RESOURCE_ID_LENGTH, MAX_WORKFLOW_ID_LENGTH } from './constants';\n\nexport function validateWorkflowId(workflowId: string): void {\n if (workflowId.length > MAX_WORKFLOW_ID_LENGTH) {\n throw new WorkflowEngineError(\n `workflowId exceeds maximum length of ${MAX_WORKFLOW_ID_LENGTH} characters (got ${workflowId.length})`,\n workflowId,\n );\n }\n}\n\nexport function validateResourceId(resourceId: string | undefined | null): void {\n if (resourceId != null && resourceId.length > MAX_RESOURCE_ID_LENGTH) {\n throw new WorkflowEngineError(\n `resourceId exceeds maximum length of ${MAX_RESOURCE_ID_LENGTH} characters (got ${resourceId.length})`,\n );\n }\n}\n\nexport class WorkflowEngineError extends Error {\n constructor(\n message: string,\n public readonly workflowId?: string,\n public readonly runId?: string,\n public override readonly cause: Error | undefined = undefined,\n public readonly issues?: StandardSchemaV1.FailureResult['issues'] | undefined,\n ) {\n super(message);\n this.name = 'WorkflowEngineError';\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, WorkflowEngineError);\n }\n }\n}\n\nexport class WorkflowRunNotFoundError extends WorkflowEngineError {\n constructor(runId?: string, workflowId?: string) {\n super('Workflow run not found', workflowId, runId);\n this.name = 'WorkflowRunNotFoundError';\n }\n}\n",
7
+ "import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport type { WorkflowRun } from './db/types';\nimport type { Duration } from './duration';\nimport type { WorkflowPriority } from './priority';\nimport type { Schedule } from './schedule';\n\nexport enum WorkflowStatus {\n PENDING = 'pending',\n RUNNING = 'running',\n PAUSED = 'paused',\n COMPLETED = 'completed',\n FAILED = 'failed',\n CANCELLED = 'cancelled',\n}\n\nexport enum StepType {\n PAUSE = 'pause',\n RUN = 'run',\n WAIT_FOR = 'waitFor',\n WAIT_UNTIL = 'waitUntil',\n DELAY = 'delay',\n POLL = 'poll',\n INVOKE_CHILD_WORKFLOW = 'invokeChildWorkflow',\n}\n\nexport type InputParameters = StandardSchemaV1;\nexport type InferInputParameters<P extends InputParameters> = StandardSchemaV1.InferOutput<P>;\n\nexport type StartWorkflowOptions = {\n resourceId?: string;\n timeout?: number;\n retries?: number;\n expireInSeconds?: number;\n idempotencyKey?: string;\n priority?: WorkflowPriority;\n};\n\nexport type WorkflowOptions<I extends InputParameters> = {\n timeout?: number;\n retries?: number;\n inputSchema?: I;\n priority?: WorkflowPriority;\n /**\n * Recurring schedule. Accepts a cron expression (`'0 9 * * 1-5'`),\n * a duration string (`'5m'`, `'1 hour'`), or a `DurationObject`.\n */\n schedule?: Schedule;\n /** IANA timezone for cron expressions. Defaults to UTC. Ignored for duration-based schedules. */\n timezone?: string;\n};\n\n/** Metadata about a scheduled fire, exposed on `ctx.schedule` for runs triggered by a schedule. */\nexport type ScheduleContext = {\n /** Time the schedule fired this run. */\n timestamp: Date;\n};\n\nexport type StepBaseContext = {\n run: <T>(stepId: string, handler: () => Promise<T>) => Promise<T>;\n waitFor: {\n <T extends InputParameters>(\n stepId: string,\n options: { eventName: string; schema?: T },\n ): Promise<InferInputParameters<T>>;\n <T extends InputParameters>(\n stepId: string,\n options: { eventName: string; timeout: number; schema?: T },\n ): Promise<InferInputParameters<T> | undefined>;\n };\n waitUntil: {\n (stepId: string, date: Date): Promise<void>;\n (stepId: string, dateString: string): Promise<void>;\n (stepId: string, options: { date: Date | string }): Promise<void>;\n };\n /** Delay execution for a duration (sugar over waitUntil). Alias: sleep. */\n delay: (stepId: string, duration: Duration) => Promise<void>;\n /** Alias for delay. */\n sleep: (stepId: string, duration: Duration) => Promise<void>;\n pause: (stepId: string) => Promise<void>;\n poll: <T>(\n stepId: string,\n conditionFn: () => Promise<T | false>,\n options?: { interval?: Duration; timeout?: Duration },\n ) => Promise<{ timedOut: false; data: T } | { timedOut: true }>;\n /**\n * Invoke a child workflow from inside the current workflow and pause until\n * the child run reaches a terminal state.\n */\n invokeChildWorkflow: {\n <TInput extends InputParameters, TOutput = unknown>(\n stepId: string,\n ref: WorkflowRef<TInput, TOutput>,\n input: InferInputParameters<TInput>,\n options?: StartWorkflowOptions,\n ): Promise<TOutput>;\n <TOutput = unknown>(\n stepId: string,\n params: {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n },\n ): Promise<TOutput>;\n };\n};\n\n/**\n * Plugin that extends the workflow step API with extra methods.\n * @template TStepBase - The step type this plugin receives (base + previous plugins).\n * @template TStepExt - The extra methods this plugin adds to step.\n */\nexport interface WorkflowPlugin<TStepBase = StepBaseContext, TStepExt = object> {\n name: string;\n methods: (step: TStepBase, context: WorkflowContext) => TStepExt;\n /**\n * Optional middleware around the workflow handler call. Composes in\n * registration order — the first plugin passed to `.use()` wraps everything\n * inside. Implementations MUST call `next()` exactly once.\n */\n wrap?: (context: WorkflowContext, next: () => Promise<unknown>) => Promise<unknown>;\n}\n\nexport type WorkflowContext<\n TInput extends InputParameters = InputParameters,\n TStep extends StepBaseContext = StepBaseContext,\n> = {\n input: InferInputParameters<TInput>;\n step: TStep;\n workflowId: string;\n runId: string;\n /** Tenant/scope identifier set when the run was started, if any. */\n resourceId?: string;\n /** Zero-based retry attempt number (= `run.retryCount`). */\n attempt: number;\n timeline: Record<string, unknown>;\n logger: WorkflowLogger;\n /** Set only for runs triggered by a recurring schedule. */\n schedule?: ScheduleContext;\n};\n\nexport type WorkflowDefinition<TInput extends InputParameters = InputParameters> = {\n id: string;\n /** Widest context avoids contravariance when collecting definitions; `workflow()` still types the handler narrowly. */\n handler: (context: WorkflowContext<InputParameters, StepBaseContext>) => Promise<unknown>;\n inputSchema?: TInput;\n timeout?: number; // milliseconds\n retries?: number;\n priority?: WorkflowPriority;\n schedule?: Schedule;\n timezone?: string;\n plugins?: WorkflowPlugin[];\n};\n\nexport type StepInternalDefinition = {\n id: string;\n type: StepType;\n conditional: boolean;\n loop: boolean;\n isDynamic: boolean;\n};\n\nexport type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters> =\n WorkflowDefinition<TInput> & {\n steps: StepInternalDefinition[];\n };\n\n/**\n * Chainable workflow factory: call as (id, handler, options) and/or use .use(plugin).\n * TStepExt is the accumulated step extension from all plugins (step = StepContext & TStepExt).\n */\nexport interface WorkflowFactory<TStepExt = object> {\n <I extends InputParameters = InputParameters>(\n id: string,\n handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>,\n options?: WorkflowOptions<I>,\n ): WorkflowDefinition<I>;\n use<TNewExt>(\n plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>,\n ): WorkflowFactory<TStepExt & TNewExt>;\n ref<TInput extends InputParameters = InputParameters, TOutput = unknown>(\n id: string,\n options?: { inputSchema?: TInput },\n ): WorkflowRef<TInput, TOutput>;\n}\n\n/**\n * Lightweight workflow reference - carries the workflow ID and input type\n * but no handler code. Safe to import in API services without pulling in\n * heavy worker dependencies.\n *\n * Callable: pass a handler to create a full WorkflowDefinition.\n */\nexport interface WorkflowRef<\n TInput extends InputParameters = InputParameters,\n // biome-ignore lint/correctness/noUnusedVariables: phantom type carried for typed return inference\n TOutput = unknown,\n> {\n (\n handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>,\n options?: Omit<WorkflowOptions<TInput>, 'inputSchema'>,\n ): WorkflowDefinition<TInput>;\n readonly id: string;\n readonly inputSchema?: TInput;\n}\n\nexport type WorkflowRunProgress = WorkflowRun & {\n completionPercentage: number;\n totalSteps: number;\n completedSteps: number;\n};\n\nexport interface WorkflowLogger {\n log(message: string): void;\n error(message: string, ...args: unknown[]): void;\n}\n\nexport type WorkflowInternalLoggerContext = {\n runId?: string;\n workflowId?: string;\n};\n\nexport interface WorkflowInternalLogger {\n log(message: string, context?: WorkflowInternalLoggerContext): void;\n error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;\n}\n\nconst _STEP_BASE_METHOD_TO_TYPE: Record<keyof StepBaseContext, StepType> = {\n run: StepType.RUN,\n waitFor: StepType.WAIT_FOR,\n waitUntil: StepType.WAIT_UNTIL,\n delay: StepType.DELAY,\n sleep: StepType.DELAY,\n pause: StepType.PAUSE,\n poll: StepType.POLL,\n invokeChildWorkflow: StepType.INVOKE_CHILD_WORKFLOW,\n};\n\nexport const STEP_BASE_METHOD_TYPES: ReadonlyMap<string, StepType> = new Map(\n Object.entries(_STEP_BASE_METHOD_TO_TYPE),\n);\n",
8
+ "import { merge } from 'es-toolkit';\nimport pg from 'pg';\nimport { type Db, PgBoss } from 'pg-boss';\nimport {\n DEFAULT_PGBOSS_SCHEMA,\n invokeChildWorkflowTimelineKey,\n isInvokeChildWorkflowTimelineEntry,\n PAUSE_EVENT_NAME,\n WORKFLOW_RUN_QUEUE_NAME,\n waitForTimelineKey,\n} from './constants';\nimport { runMigrations } from './db/migration';\nimport {\n getWorkflowRun,\n getWorkflowRuns,\n insertWorkflowRun,\n updateWorkflowRun,\n withPostgresTransaction,\n} from './db/queries';\nimport type { WorkflowRun } from './db/types';\nimport {\n validateResourceId,\n validateWorkflowId,\n WorkflowEngineError,\n WorkflowRunNotFoundError,\n} from './error';\nimport { resolvePriority } from './priority';\nimport {\n type InferInputParameters,\n type InputParameters,\n type StartWorkflowOptions,\n type WorkflowLogger,\n type WorkflowRef,\n type WorkflowRunProgress,\n WorkflowStatus,\n} from './types';\n\nconst LOG_PREFIX = '[WorkflowClient]';\n\ntype WorkflowRunJobParameters = {\n runId: string;\n resourceId?: string;\n workflowId: string;\n input: unknown;\n event?: {\n name: string;\n data?: Record<string, unknown>;\n };\n};\n\nexport type WorkflowClientOptions = {\n logger?: WorkflowLogger;\n /**\n * Pre-configured pg-boss instance. Pass this when the engine side uses a\n * non-default pg-boss config (schema, retention, logger, etc.) so the\n * client enqueues jobs where the engine reads them. Mirrors the same\n * option on `WorkflowEngineOptions`.\n */\n boss?: PgBoss;\n} & ({ pool: pg.Pool; connectionString?: never } | { connectionString: string; pool?: never });\n\nconst defaultLogger: WorkflowLogger = {\n log: (_message: string) => console.warn(_message),\n error: (message: string, error: Error) => console.error(message, error),\n};\n\nconst defaultExpireInSeconds = process.env.WORKFLOW_RUN_EXPIRE_IN_SECONDS\n ? Number.parseInt(process.env.WORKFLOW_RUN_EXPIRE_IN_SECONDS, 10)\n : 5 * 60;\n\nexport class WorkflowClient {\n private boss: PgBoss;\n private db: Db;\n private pool: pg.Pool;\n private _ownsPool = false;\n private _started = false;\n private logger: WorkflowLogger;\n\n constructor({ logger, boss, ...connectionOptions }: WorkflowClientOptions) {\n this.logger = logger ?? defaultLogger;\n\n if ('pool' in connectionOptions && connectionOptions.pool) {\n this.pool = connectionOptions.pool;\n } else if ('connectionString' in connectionOptions && connectionOptions.connectionString) {\n this.pool = new pg.Pool({ connectionString: connectionOptions.connectionString });\n this._ownsPool = true;\n } else {\n throw new WorkflowEngineError('Either pool or connectionString must be provided');\n }\n\n const db: Db = {\n executeSql: (text: string, values?: unknown[]) =>\n this.pool.query(text, values) as Promise<{ rows: unknown[] }>,\n };\n\n if (boss) {\n this.boss = boss;\n } else {\n this.boss = new PgBoss({ db, schema: DEFAULT_PGBOSS_SCHEMA });\n }\n this.db = db;\n }\n\n async start(): Promise<void> {\n if (this._started) {\n return;\n }\n\n await this.boss.start();\n this.db = this.boss.getDb();\n await runMigrations(this.db);\n await this.boss.createQueue(WORKFLOW_RUN_QUEUE_NAME);\n\n this._started = true;\n this.logger.log(`${LOG_PREFIX} Client started`);\n }\n\n async stop(): Promise<void> {\n await this.boss.stop();\n\n if (this._ownsPool) {\n await this.pool.end();\n }\n\n this._started = false;\n this.logger.log(`${LOG_PREFIX} Client stopped`);\n }\n\n async startWorkflow<TInput extends InputParameters>(\n ref: WorkflowRef<TInput>,\n input: InferInputParameters<TInput>,\n options?: StartWorkflowOptions,\n ): Promise<WorkflowRun>;\n\n async startWorkflow(params: {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n }): Promise<WorkflowRun>;\n\n async startWorkflow<TInput extends InputParameters>(\n refOrParams:\n | WorkflowRef<TInput>\n | {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n },\n inputArg?: InferInputParameters<TInput>,\n optionsArg?: StartWorkflowOptions,\n ): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n let workflowId: string;\n let input: unknown;\n let resourceId: string | undefined;\n let idempotencyKey: string | undefined;\n let options: StartWorkflowOptions | undefined;\n\n if (typeof refOrParams === 'function' && 'id' in refOrParams) {\n const ref = refOrParams as WorkflowRef<TInput>;\n workflowId = ref.id;\n input = inputArg;\n options = optionsArg;\n resourceId = optionsArg?.resourceId;\n idempotencyKey = optionsArg?.idempotencyKey;\n\n if (ref.inputSchema) {\n const result = await ref.inputSchema['~standard'].validate(input);\n if (result.issues) {\n throw new WorkflowEngineError(\n JSON.stringify(result.issues),\n workflowId,\n undefined,\n undefined,\n result.issues,\n );\n }\n }\n } else {\n const params = refOrParams as {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n };\n workflowId = params.workflowId;\n input = params.input;\n resourceId = params.resourceId;\n idempotencyKey = params.idempotencyKey;\n options = params.options;\n }\n\n validateWorkflowId(workflowId);\n validateResourceId(resourceId);\n\n const run = await withPostgresTransaction(\n this.db,\n async (_db) => {\n const timeoutAt = options?.timeout ? new Date(Date.now() + options.timeout) : null;\n\n const { run: insertedRun, created } = await insertWorkflowRun(\n {\n resourceId,\n workflowId,\n currentStepId: '__start__',\n status: WorkflowStatus.RUNNING,\n input,\n maxRetries: options?.retries ?? 0,\n priority: resolvePriority(options?.priority),\n timeoutAt,\n idempotencyKey,\n },\n _db,\n );\n\n if (created) {\n const job: WorkflowRunJobParameters = {\n runId: insertedRun.id,\n resourceId,\n workflowId,\n input,\n };\n\n // Same connection (`_db`) used for the workflow_runs INSERT is passed\n // to `boss.send` so the pgboss.job INSERT joins the same transaction.\n // The two writes commit or roll back together, so we never leak an\n // orphan workflow_runs row when the queue insert fails.\n await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {\n startAfter: new Date(),\n expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,\n priority: insertedRun.priority,\n db: _db,\n });\n }\n\n return insertedRun;\n },\n this.pool,\n );\n\n this.logger.log(`${LOG_PREFIX} Started workflow run ${run.id} for ${workflowId}`);\n\n return run;\n }\n\n async triggerEvent({\n runId,\n resourceId,\n eventName,\n data,\n options,\n }: {\n runId: string;\n resourceId?: string;\n eventName: string;\n data?: Record<string, unknown>;\n options?: { expireInSeconds?: number };\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await this.getRun({ runId, resourceId });\n\n const job: WorkflowRunJobParameters = {\n runId: run.id,\n resourceId: resourceId ?? run.resourceId ?? undefined,\n workflowId: run.workflowId,\n input: run.input,\n event: {\n name: eventName,\n data,\n },\n };\n\n await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {\n expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,\n priority: run.priority,\n });\n\n this.logger.log(`${LOG_PREFIX} Event ${eventName} sent for workflow run ${runId}`);\n return run;\n }\n\n async pauseWorkflow({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await updateWorkflowRun(\n {\n runId,\n resourceId,\n data: {\n status: WorkflowStatus.PAUSED,\n pausedAt: new Date(),\n },\n expectedStatuses: [WorkflowStatus.RUNNING, WorkflowStatus.PENDING],\n },\n this.db,\n );\n\n if (!run) {\n throw new WorkflowRunNotFoundError(runId);\n }\n\n this.logger.log(`${LOG_PREFIX} Paused workflow run ${runId}`);\n return run;\n }\n\n async resumeWorkflow({\n runId,\n resourceId,\n options,\n }: {\n runId: string;\n resourceId?: string;\n options?: { expireInSeconds?: number };\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const current = await this.getRun({ runId, resourceId });\n if (current.status !== WorkflowStatus.PAUSED) {\n throw new WorkflowEngineError(\n `Cannot resume workflow run in '${current.status}' status, must be 'paused'`,\n current.workflowId,\n runId,\n );\n }\n\n const currentStepId = current.currentStepId;\n const currentStepTimelineEntry =\n current.timeline[invokeChildWorkflowTimelineKey(currentStepId)];\n if (isInvokeChildWorkflowTimelineEntry(currentStepTimelineEntry)) {\n return current;\n }\n\n return this.triggerEvent({\n runId,\n resourceId,\n eventName: PAUSE_EVENT_NAME,\n data: {},\n options,\n });\n }\n\n async fastForwardWorkflow({\n runId,\n resourceId,\n data,\n }: {\n runId: string;\n resourceId?: string;\n data?: Record<string, unknown>;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await this.getRun({ runId, resourceId });\n\n if (run.status !== WorkflowStatus.PAUSED) {\n return run;\n }\n\n const currentStepId = run.currentStepId;\n const currentStepTimelineEntry = run.timeline[invokeChildWorkflowTimelineKey(currentStepId)];\n if (isInvokeChildWorkflowTimelineEntry(currentStepTimelineEntry)) {\n return run;\n }\n\n const waitForEntry = run.timeline[waitForTimelineKey(currentStepId)];\n if (!waitForEntry || typeof waitForEntry !== 'object' || !('waitFor' in waitForEntry)) {\n return run;\n }\n\n const { eventName, timeoutEvent, skipOutput } = (\n waitForEntry as { waitFor: { eventName?: string; timeoutEvent?: string; skipOutput?: true } }\n ).waitFor;\n\n // step.pause() - delegate to resumeWorkflow\n if (eventName === PAUSE_EVENT_NAME) {\n return this.resumeWorkflow({ runId, resourceId });\n }\n\n // step.poll() - write output to timeline first, then trigger resume\n if (skipOutput && timeoutEvent) {\n await withPostgresTransaction(\n this.db,\n async (db) => {\n const freshRun = await getWorkflowRun({ runId, resourceId }, { exclusiveLock: true, db });\n if (!freshRun) throw new WorkflowRunNotFoundError(runId);\n return updateWorkflowRun(\n {\n runId,\n resourceId,\n data: {\n timeline: merge(freshRun.timeline, {\n [currentStepId]: {\n output: data ?? {},\n timestamp: new Date(),\n },\n }),\n },\n },\n db,\n );\n },\n this.pool,\n );\n\n return this.triggerEvent({ runId, resourceId, eventName: timeoutEvent });\n }\n\n // waitFor steps - trigger the event with data\n if (eventName) {\n return this.triggerEvent({ runId, resourceId, eventName, data: data ?? {} });\n }\n\n // delay/waitUntil steps - trigger the timeout event\n if (timeoutEvent) {\n return this.triggerEvent({ runId, resourceId, eventName: timeoutEvent, data: data ?? {} });\n }\n\n return run;\n }\n\n async cancelWorkflow({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await updateWorkflowRun(\n {\n runId,\n resourceId,\n data: {\n status: WorkflowStatus.CANCELLED,\n },\n expectedStatuses: [WorkflowStatus.PENDING, WorkflowStatus.RUNNING, WorkflowStatus.PAUSED],\n },\n this.db,\n );\n\n if (!run) {\n throw new WorkflowRunNotFoundError(runId);\n }\n\n this.logger.log(`${LOG_PREFIX} Cancelled workflow run ${runId}`);\n return run;\n }\n\n async getRun({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await getWorkflowRun({ runId, resourceId }, { db: this.db });\n\n if (!run) {\n throw new WorkflowRunNotFoundError(runId);\n }\n\n return run;\n }\n\n async checkProgress({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRunProgress> {\n const run = await this.getRun({ runId, resourceId });\n\n const completedSteps = Object.values(run.timeline).filter(\n (entry): entry is { output: unknown; timestamp: Date } =>\n typeof entry === 'object' &&\n entry !== null &&\n 'output' in entry &&\n entry.output !== undefined,\n ).length;\n\n // Without registered workflow definitions, total steps are unknown.\n // Use completed steps as best-effort estimate for in-progress runs.\n const totalSteps = run.status === WorkflowStatus.COMPLETED ? completedSteps : 0;\n const completionPercentage =\n run.status === WorkflowStatus.COMPLETED\n ? 100\n : run.status === WorkflowStatus.FAILED || run.status === WorkflowStatus.CANCELLED\n ? 0\n : 0;\n\n return {\n ...run,\n completedSteps,\n completionPercentage,\n totalSteps,\n };\n }\n\n async getRuns({\n resourceId,\n startingAfter,\n endingBefore,\n limit = 20,\n statuses,\n workflowId,\n }: {\n resourceId?: string;\n startingAfter?: string | null;\n endingBefore?: string | null;\n limit?: number;\n statuses?: WorkflowStatus[];\n workflowId?: string;\n }): Promise<{\n items: WorkflowRun[];\n nextCursor: string | null;\n prevCursor: string | null;\n hasMore: boolean;\n hasPrev: boolean;\n }> {\n await this.ensureStarted();\n\n if (workflowId) validateWorkflowId(workflowId);\n validateResourceId(resourceId);\n\n return getWorkflowRuns(\n {\n resourceId,\n startingAfter,\n endingBefore,\n limit,\n statuses,\n workflowId,\n },\n this.db,\n );\n }\n\n private async ensureStarted(): Promise<void> {\n if (!this._started) {\n await this.start();\n }\n }\n}\n",
9
+ "import type { Db } from 'pg-boss';\n\n// Arbitrary but stable lock ID for serializing migrations across processes\nexport const MIGRATION_LOCK_ID = 738291645;\n\n// Bump this when adding new migrations. The engine stores the current version\n// in a `workflow_schema_version` table so migrations only run once per version.\nconst CURRENT_SCHEMA_VERSION = 6;\n\nexport async function runMigrations(db: Db): Promise<void> {\n // Fast path: skip the advisory lock if schema is already current.\n // This is the common case - every engine.start() after initial setup.\n if (await isSchemaUpToDate(db)) {\n return;\n }\n\n // Slow path: build migration SQL based on current version, then execute\n // everything in a single transaction with an advisory lock.\n // This mirrors pg-boss's approach: one executeSql call ensures all DDL\n // runs on a single connection inside BEGIN/COMMIT, and pg_advisory_xact_lock\n // auto-releases on commit or rollback (no manual unlock needed).\n const currentVersion = await getCurrentVersion(db);\n\n const commands: string[] = [];\n\n if (currentVersion < 1) {\n // On first run, we run a check to see if there is an existing foreign\n // `workflow_runs` table. To decide on that, we check if the `workflow_runs`\n // exists but a `workflow_schema_version` table doesn't. If that is true\n // then `workflow_runs` is foreign and we should fail lest we start\n // adding rows there, corrupting it. We also do the reverse check.\n // We are basing this off the fact that our transaction creates both or no tables\n // This is not airtight, but serves as an extra safety check for now.\n const existing = await db.executeSql(\n `SELECT\n to_regclass('workflow_runs') IS NOT NULL AS has_runs_table,\n to_regclass('workflow_schema_version') IS NOT NULL AS has_version_table`,\n [],\n );\n\n const row = existing.rows[0] as\n | { has_runs_table: boolean; has_version_table: boolean }\n | undefined;\n\n if (row?.has_runs_table && !row.has_version_table) {\n throw new Error(\n `pg-workflows: a \"workflow_runs\" table already exists in this schema but was not ` +\n `created by pg-workflows. Point the workflow engine at a dedicated schema/database.`,\n );\n }\n\n if (!row?.has_runs_table && row?.has_version_table) {\n throw new Error(\n `pg-workflows: a \"workflow_schema_version\" table already exists in this schema but was not ` +\n `created by pg-workflows. Point the workflow engine at a dedicated schema/database.`,\n );\n }\n\n commands.push(`\n CREATE TABLE IF NOT EXISTS workflow_runs (\n id varchar(32) PRIMARY KEY NOT NULL,\n created_at timestamp with time zone DEFAULT now() NOT NULL,\n updated_at timestamp with time zone DEFAULT now() NOT NULL,\n resource_id varchar(256),\n workflow_id varchar(256) NOT NULL,\n status text DEFAULT 'pending' NOT NULL,\n input jsonb NOT NULL,\n output jsonb,\n error text,\n current_step_id varchar(256) NOT NULL,\n timeline jsonb DEFAULT '{}'::jsonb NOT NULL,\n paused_at timestamp with time zone,\n resumed_at timestamp with time zone,\n completed_at timestamp with time zone,\n timeout_at timestamp with time zone,\n retry_count integer DEFAULT 0 NOT NULL,\n max_retries integer DEFAULT 0 NOT NULL,\n job_id varchar(256)\n )\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_created_at_idx ON workflow_runs USING btree (created_at)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_resource_id_created_at_idx ON workflow_runs USING btree (resource_id, created_at DESC)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_status_created_at_idx ON workflow_runs USING btree (status, created_at DESC)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_workflow_id_created_at_idx ON workflow_runs USING btree (workflow_id, created_at DESC)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_resource_id_workflow_id_created_at_idx ON workflow_runs USING btree (resource_id, workflow_id, created_at DESC)\n `);\n }\n\n if (currentVersion < 2) {\n commands.push('DROP INDEX IF EXISTS workflow_runs_workflow_id_idx');\n commands.push('DROP INDEX IF EXISTS workflow_runs_resource_id_idx');\n commands.push(\n 'ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS idempotency_key varchar(256)',\n );\n commands.push(`\n CREATE UNIQUE INDEX IF NOT EXISTS workflow_runs_idempotency_key_idx ON workflow_runs (idempotency_key) WHERE idempotency_key IS NOT NULL\n `);\n }\n\n if (currentVersion < 3) {\n commands.push('ALTER TABLE workflow_runs ALTER COLUMN resource_id TYPE varchar(256)');\n commands.push('ALTER TABLE workflow_runs ALTER COLUMN workflow_id TYPE varchar(256)');\n }\n\n if (currentVersion < 4) {\n commands.push('ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS parent_run_id varchar(32)');\n commands.push('ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS parent_step_id varchar(256)');\n commands.push(\n 'ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS parent_resource_id varchar(256)',\n );\n }\n\n if (currentVersion < 5) {\n commands.push(\n 'ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS scheduled_at timestamp with time zone',\n );\n }\n\n if (currentVersion < 6) {\n commands.push(\n 'ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS priority integer DEFAULT 0 NOT NULL',\n );\n }\n\n // Upsert the schema version\n if (currentVersion === 0) {\n commands.push(\n `INSERT INTO workflow_schema_version (version) VALUES (${CURRENT_SCHEMA_VERSION})`,\n );\n } else {\n commands.push(`UPDATE workflow_schema_version SET version = ${CURRENT_SCHEMA_VERSION}`);\n }\n\n if (commands.length === 0) {\n return;\n }\n\n const sql = [\n 'BEGIN',\n \"SET LOCAL lock_timeout = '30s'\",\n \"SET LOCAL idle_in_transaction_session_timeout = '30s'\",\n `SELECT pg_advisory_xact_lock(${MIGRATION_LOCK_ID})`,\n 'CREATE TABLE IF NOT EXISTS workflow_schema_version (version integer NOT NULL)',\n ...commands,\n 'COMMIT',\n ].join(';\\n');\n\n await db.executeSql(sql, []);\n}\n\nasync function isSchemaUpToDate(db: Db): Promise<boolean> {\n try {\n const result = await db.executeSql('SELECT version FROM workflow_schema_version LIMIT 1', []);\n return (\n ((result.rows[0] as { version: number } | undefined)?.version ?? 0) >= CURRENT_SCHEMA_VERSION\n );\n } catch {\n // Table doesn't exist yet - needs migration\n return false;\n }\n}\n\nasync function getCurrentVersion(db: Db): Promise<number> {\n try {\n const result = await db.executeSql('SELECT version FROM workflow_schema_version LIMIT 1', []);\n return (result.rows[0] as { version: number } | undefined)?.version ?? 0;\n } catch {\n return 0;\n }\n}\n",
10
+ "import ksuid from 'ksuid';\nimport type { Db } from 'pg-boss';\nimport type { WorkflowRun } from './types';\n\nexport function generateKSUID(prefix?: string): string {\n return `${prefix ? `${prefix}_` : ''}${ksuid.randomSync().string}`;\n}\n\ntype WorkflowRunRow = {\n id: string;\n created_at: string | Date;\n updated_at: string | Date;\n resource_id: string | null;\n workflow_id: string;\n status: 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'cancelled';\n input: string | unknown;\n output: string | unknown | null;\n error: string | null;\n current_step_id: string;\n timeline: string | Record<string, unknown>;\n paused_at: string | Date | null;\n resumed_at: string | Date | null;\n completed_at: string | Date | null;\n timeout_at: string | Date | null;\n retry_count: number;\n max_retries: number;\n priority: number;\n job_id: string | null;\n idempotency_key: string | null;\n parent_run_id: string | null;\n parent_step_id: string | null;\n parent_resource_id: string | null;\n scheduled_at: string | Date | null;\n};\n\nfunction mapRowToWorkflowRun(row: WorkflowRunRow): WorkflowRun {\n return {\n id: row.id,\n createdAt: new Date(row.created_at),\n updatedAt: new Date(row.updated_at),\n resourceId: row.resource_id,\n workflowId: row.workflow_id,\n status: row.status,\n input: typeof row.input === 'string' ? JSON.parse(row.input) : row.input,\n output:\n typeof row.output === 'string'\n ? row.output.trim().startsWith('{') || row.output.trim().startsWith('[')\n ? JSON.parse(row.output)\n : row.output\n : (row.output ?? null),\n error: row.error,\n currentStepId: row.current_step_id,\n timeline: typeof row.timeline === 'string' ? JSON.parse(row.timeline) : row.timeline,\n pausedAt: row.paused_at ? new Date(row.paused_at) : null,\n resumedAt: row.resumed_at ? new Date(row.resumed_at) : null,\n completedAt: row.completed_at ? new Date(row.completed_at) : null,\n timeoutAt: row.timeout_at ? new Date(row.timeout_at) : null,\n retryCount: row.retry_count,\n maxRetries: row.max_retries,\n priority: row.priority,\n jobId: row.job_id,\n idempotencyKey: row.idempotency_key,\n parentRunId: row.parent_run_id,\n parentStepId: row.parent_step_id,\n parentResourceId: row.parent_resource_id,\n scheduledAt: row.scheduled_at ? new Date(row.scheduled_at) : null,\n };\n}\n\nexport async function insertWorkflowRun(\n {\n resourceId,\n workflowId,\n currentStepId,\n status,\n input,\n maxRetries,\n priority,\n timeoutAt,\n idempotencyKey,\n parentRunId,\n parentStepId,\n parentResourceId,\n scheduledAt,\n }: {\n resourceId?: string;\n workflowId: string;\n currentStepId: string;\n status: string;\n input: unknown;\n maxRetries: number;\n priority: number;\n timeoutAt: Date | null;\n idempotencyKey?: string;\n parentRunId?: string;\n parentStepId?: string;\n parentResourceId?: string;\n scheduledAt?: Date;\n },\n db: Db,\n): Promise<{ run: WorkflowRun; created: boolean }> {\n const runId = generateKSUID('run');\n const now = new Date();\n\n const result = await db.executeSql(\n `INSERT INTO workflow_runs (\n id,\n resource_id,\n workflow_id,\n current_step_id,\n status,\n input,\n max_retries,\n priority,\n timeout_at,\n created_at,\n updated_at,\n timeline,\n retry_count,\n idempotency_key,\n parent_run_id,\n parent_step_id,\n parent_resource_id,\n scheduled_at\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)\n ON CONFLICT (idempotency_key) WHERE idempotency_key IS NOT NULL DO NOTHING\n RETURNING *`,\n [\n runId,\n resourceId ?? null,\n workflowId,\n currentStepId,\n status,\n JSON.stringify(input),\n maxRetries,\n priority,\n timeoutAt,\n now,\n now,\n '{}',\n 0,\n idempotencyKey ?? null,\n parentRunId ?? null,\n parentStepId ?? null,\n parentResourceId ?? null,\n scheduledAt ?? null,\n ],\n );\n\n if (result.rows[0]) {\n return { run: mapRowToWorkflowRun(result.rows[0]), created: true };\n }\n\n // Conflict - fetch the existing row\n const existing = await db.executeSql('SELECT * FROM workflow_runs WHERE idempotency_key = $1', [\n idempotencyKey,\n ]);\n\n if (!existing.rows[0]) {\n throw new Error(`Idempotency conflict: existing run not found for key \"${idempotencyKey}\"`);\n }\n\n return { run: mapRowToWorkflowRun(existing.rows[0]), created: false };\n}\n\nexport async function getWorkflowRun(\n {\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n },\n { exclusiveLock = false, db }: { exclusiveLock?: boolean; db: Db },\n): Promise<WorkflowRun | null> {\n const lockSuffix = exclusiveLock ? 'FOR UPDATE' : '';\n\n const result = resourceId\n ? await db.executeSql(\n `SELECT * FROM workflow_runs \n WHERE id = $1 AND resource_id = $2\n ${lockSuffix}`,\n [runId, resourceId],\n )\n : await db.executeSql(\n `SELECT * FROM workflow_runs \n WHERE id = $1\n ${lockSuffix}`,\n [runId],\n );\n\n const run = result.rows[0];\n\n if (!run) {\n return null;\n }\n\n return mapRowToWorkflowRun(run);\n}\n\nexport async function getWorkflowLastRun(\n {\n workflowId,\n resourceId,\n }: {\n workflowId: string;\n resourceId?: string;\n },\n db: Db,\n): Promise<WorkflowRun | null> {\n const result = resourceId\n ? await db.executeSql(\n `SELECT * FROM workflow_runs\n WHERE workflow_id = $1 AND resource_id = $2\n ORDER BY created_at DESC\n LIMIT 1`,\n [workflowId, resourceId],\n )\n : await db.executeSql(\n `SELECT * FROM workflow_runs\n WHERE workflow_id = $1\n ORDER BY created_at DESC\n LIMIT 1`,\n [workflowId],\n );\n\n const run = result.rows[0];\n if (!run) {\n return null;\n }\n return mapRowToWorkflowRun(run);\n}\n\nexport async function updateWorkflowRun(\n {\n runId,\n resourceId,\n data,\n expectedStatuses,\n }: {\n runId: string;\n resourceId?: string;\n data: Partial<WorkflowRun>;\n expectedStatuses?: string[];\n },\n db: Db,\n): Promise<WorkflowRun | null> {\n const now = new Date();\n\n const updates: string[] = ['updated_at = $1'];\n const values: (string | number | Date | null | string[])[] = [now];\n let paramIndex = 2;\n\n if (data.status !== undefined) {\n updates.push(`status = $${paramIndex}`);\n values.push(data.status);\n paramIndex++;\n }\n if (data.currentStepId !== undefined) {\n updates.push(`current_step_id = $${paramIndex}`);\n values.push(data.currentStepId);\n paramIndex++;\n }\n if (data.timeline !== undefined) {\n updates.push(`timeline = $${paramIndex}`);\n values.push(JSON.stringify(data.timeline));\n paramIndex++;\n }\n if (data.pausedAt !== undefined) {\n updates.push(`paused_at = $${paramIndex}`);\n values.push(data.pausedAt);\n paramIndex++;\n }\n if (data.resumedAt !== undefined) {\n updates.push(`resumed_at = $${paramIndex}`);\n values.push(data.resumedAt);\n paramIndex++;\n }\n if (data.completedAt !== undefined) {\n updates.push(`completed_at = $${paramIndex}`);\n values.push(data.completedAt);\n paramIndex++;\n }\n if (data.output !== undefined) {\n updates.push(`output = $${paramIndex}`);\n values.push(JSON.stringify(data.output));\n paramIndex++;\n }\n if (data.error !== undefined) {\n updates.push(`error = $${paramIndex}`);\n values.push(data.error);\n paramIndex++;\n }\n if (data.retryCount !== undefined) {\n updates.push(`retry_count = $${paramIndex}`);\n values.push(data.retryCount);\n paramIndex++;\n }\n if (data.jobId !== undefined) {\n updates.push(`job_id = $${paramIndex}`);\n values.push(data.jobId);\n paramIndex++;\n }\n\n values.push(runId);\n const idParam = paramIndex;\n paramIndex++;\n\n if (resourceId) {\n values.push(resourceId);\n paramIndex++;\n }\n\n if (expectedStatuses && expectedStatuses.length > 0) {\n values.push(expectedStatuses);\n paramIndex++;\n }\n\n let whereClause = resourceId\n ? `WHERE id = $${idParam} AND resource_id = $${idParam + 1}`\n : `WHERE id = $${idParam}`;\n\n if (expectedStatuses && expectedStatuses.length > 0) {\n whereClause += ` AND status = ANY($${paramIndex - 1})`;\n }\n\n const query = `\n UPDATE workflow_runs \n SET ${updates.join(', ')}\n ${whereClause}\n RETURNING *\n `;\n\n const result = await db.executeSql(query, values);\n const run = result.rows[0];\n\n if (!run) {\n return null;\n }\n\n return mapRowToWorkflowRun(run);\n}\n\nexport async function getWorkflowRuns(\n {\n resourceId,\n startingAfter,\n endingBefore,\n limit = 20,\n statuses,\n workflowId,\n }: {\n resourceId?: string;\n startingAfter?: string | null;\n endingBefore?: string | null;\n limit?: number;\n statuses?: string[];\n workflowId?: string;\n },\n db: Db,\n): Promise<{\n items: WorkflowRun[];\n nextCursor: string | null;\n prevCursor: string | null;\n hasMore: boolean;\n hasPrev: boolean;\n}> {\n const conditions: string[] = [];\n const values: (string | number | string[] | Date)[] = [];\n let paramIndex = 1;\n\n if (resourceId) {\n conditions.push(`resource_id = $${paramIndex}`);\n values.push(resourceId);\n paramIndex++;\n }\n\n if (statuses && statuses.length > 0) {\n conditions.push(`status = ANY($${paramIndex})`);\n values.push(statuses);\n paramIndex++;\n }\n\n if (workflowId) {\n conditions.push(`workflow_id = $${paramIndex}`);\n values.push(workflowId);\n paramIndex++;\n }\n\n const cursorIds = [startingAfter, endingBefore].filter(Boolean) as string[];\n if (cursorIds.length > 0) {\n const cursorResult = await db.executeSql(\n 'SELECT id, created_at FROM workflow_runs WHERE id = ANY($1)',\n [cursorIds],\n );\n const cursorMap = new Map<string, Date>();\n for (const row of cursorResult.rows) {\n cursorMap.set(\n row.id,\n typeof row.created_at === 'string' ? new Date(row.created_at) : row.created_at,\n );\n }\n\n if (startingAfter) {\n const cursor = cursorMap.get(startingAfter);\n if (cursor) {\n conditions.push(`created_at < $${paramIndex}`);\n values.push(cursor);\n paramIndex++;\n }\n }\n\n if (endingBefore) {\n const cursor = cursorMap.get(endingBefore);\n if (cursor) {\n conditions.push(`created_at > $${paramIndex}`);\n values.push(cursor);\n paramIndex++;\n }\n }\n }\n\n const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';\n const actualLimit = Math.min(Math.max(limit, 1), 100) + 1;\n\n const isBackward = !!endingBefore && !startingAfter;\n\n const query = `\n SELECT * FROM workflow_runs\n ${whereClause}\n ORDER BY created_at ${isBackward ? 'ASC' : 'DESC'}\n LIMIT $${paramIndex}\n `;\n values.push(actualLimit);\n\n const result = await db.executeSql(query, values);\n const rows = result.rows;\n\n const hasExtraRow = rows.length > (limit ?? 20);\n const rawItems = hasExtraRow ? rows.slice(0, limit) : rows;\n\n if (isBackward) {\n rawItems.reverse();\n }\n\n const items = rawItems.map((row) => mapRowToWorkflowRun(row));\n\n const hasMore = isBackward ? items.length > 0 : hasExtraRow;\n const hasPrev = isBackward ? hasExtraRow : !!startingAfter && items.length > 0;\n\n const nextCursor = hasMore && items.length > 0 ? (items[items.length - 1]?.id ?? null) : null;\n const prevCursor = hasPrev && items.length > 0 ? (items[0]?.id ?? null) : null;\n\n return { items, nextCursor, prevCursor, hasMore, hasPrev };\n}\n\n/**\n * Run a callback inside a PostgreSQL transaction using a dedicated client.\n *\n * When a `pool` is provided, a dedicated client is checked out so that\n * BEGIN / COMMIT / ROLLBACK all execute on the **same** connection.\n * This is critical for `SELECT … FOR UPDATE` locks and any work that\n * yields to the event-loop inside the transaction (e.g. async step handlers).\n *\n * Falls back to the pg-boss `Db` adapter when no pool is given (unit-test path).\n */\nexport async function withPostgresTransaction<T>(\n db: Db,\n callback: (db: Db) => Promise<T>,\n pool?: {\n connect: () => Promise<{\n query: (text: string, values?: unknown[]) => Promise<unknown>;\n release: () => void;\n }>;\n },\n): Promise<T> {\n let txDb: Db;\n let release: (() => void) | undefined;\n\n if (pool) {\n const client = await pool.connect();\n txDb = {\n executeSql: (text: string, values?: unknown[]) =>\n client.query(text, values) as Promise<{ rows: unknown[] }>,\n };\n release = () => client.release();\n } else {\n txDb = db;\n }\n\n try {\n await txDb.executeSql('BEGIN', []);\n const result = await callback(txDb);\n await txDb.executeSql('COMMIT', []);\n return result;\n } catch (error) {\n await txDb.executeSql('ROLLBACK', []);\n throw error;\n } finally {\n release?.();\n }\n}\n",
11
+ "import { WorkflowEngineError } from './error';\n\n/**\n * Named priority levels mapped to pg-boss integer priorities. Higher numbers\n * are fetched first (pg-boss orders by `priority DESC`), and `normal = 0`\n * matches pg-boss's own default. The ±100 spacing leaves room for numeric\n * tuning between tiers via the escape hatch.\n */\nexport const PRIORITY_LEVELS = { high: 100, normal: 0, low: -100 } as const;\n\nexport type WorkflowPriority = keyof typeof PRIORITY_LEVELS | number;\n\nfunction priorityToInt(value: WorkflowPriority): number {\n if (typeof value === 'number') {\n if (!Number.isInteger(value)) {\n throw new WorkflowEngineError(\n `Invalid priority ${value}: numeric priority must be an integer.`,\n );\n }\n return value;\n }\n\n const mapped = PRIORITY_LEVELS[value];\n if (mapped === undefined) {\n throw new WorkflowEngineError(\n `Invalid priority \"${value}\": expected one of ${Object.keys(PRIORITY_LEVELS)\n .map((k) => `\"${k}\"`)\n .join(', ')} or an integer.`,\n );\n }\n return mapped;\n}\n\n/**\n * Resolve the effective priority from a precedence chain. The first defined\n * candidate wins (e.g. per-run override, then definition default, then an\n * inherited parent priority); when nothing is defined it falls back to\n * normal (0).\n */\nexport function resolvePriority(...candidates: (WorkflowPriority | undefined)[]): number {\n for (const candidate of candidates) {\n if (candidate !== undefined) {\n return priorityToInt(candidate);\n }\n }\n return PRIORITY_LEVELS.normal;\n}\n",
12
+ "import type {\n InputParameters,\n StepBaseContext,\n WorkflowContext,\n WorkflowDefinition,\n WorkflowFactory,\n WorkflowOptions,\n WorkflowPlugin,\n WorkflowRef,\n} from './types';\n\n/**\n * Create a lightweight workflow reference.\n * Safe to import from `pg-workflows/client` - no engine or handler code.\n */\nexport function createWorkflowRef<\n TOutput = unknown,\n TInput extends InputParameters = InputParameters,\n>(id: string, options?: { inputSchema?: TInput }): WorkflowRef<TInput, TOutput> {\n const ref = ((\n handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>,\n defineOptions?: Omit<WorkflowOptions<TInput>, 'inputSchema'>,\n ): WorkflowDefinition<TInput> => ({\n id,\n handler: handler as (\n context: WorkflowContext<InputParameters, StepBaseContext>,\n ) => Promise<unknown>,\n inputSchema: options?.inputSchema,\n timeout: defineOptions?.timeout,\n retries: defineOptions?.retries,\n priority: defineOptions?.priority,\n schedule: defineOptions?.schedule,\n timezone: defineOptions?.timezone,\n })) as WorkflowRef<TInput, TOutput>;\n\n Object.defineProperty(ref, 'id', { value: id, enumerable: true });\n Object.defineProperty(ref, 'inputSchema', {\n value: options?.inputSchema,\n enumerable: true,\n });\n\n return ref;\n}\n\nfunction createWorkflowFactory<TStepExt extends object = object>(\n plugins: Array<WorkflowPlugin<unknown, object>> = [],\n): WorkflowFactory<TStepExt> {\n const factory = (<I extends InputParameters>(\n id: string,\n handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>,\n { inputSchema, timeout, retries, priority, schedule, timezone }: WorkflowOptions<I> = {},\n ): WorkflowDefinition<I> => ({\n id,\n handler: handler as (\n context: WorkflowContext<InputParameters, StepBaseContext>,\n ) => Promise<unknown>,\n inputSchema,\n timeout,\n retries,\n priority,\n schedule,\n timezone,\n plugins: plugins.length > 0 ? (plugins as WorkflowPlugin[]) : undefined,\n })) as WorkflowFactory<TStepExt>;\n\n factory.use = <TNewExt>(\n plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>,\n ): WorkflowFactory<TStepExt & TNewExt> =>\n createWorkflowFactory<TStepExt & TNewExt>([\n ...plugins,\n plugin as WorkflowPlugin<unknown, object>,\n ]);\n\n factory.ref = createWorkflowRef;\n\n return factory;\n}\n\nexport const workflow: WorkflowFactory = createWorkflowFactory();\n"
13
+ ],
14
+ "mappings": ";AAAO,IAAM,mBAAmB;AACzB,IAAM,0BAA0B;AAChC,IAAM,8BAA8B;AAG3C,IAAM,wBAAwB;AACvB,IAAM,uBAAuB,CAAC,eACnC,GAAG,wBAAwB;AACtB,IAAM,wBAAwB;AAC9B,IAAM,yBAAyB;AAC/B,IAAM,yBAAyB;AAK/B,IAAM,wCAAwC;AAC9C,IAAM,2BAA2B;AACjC,IAAM,iCAAiC,CAAC,WAC7C,GAAG,UAAU;AACR,IAAM,qBAAqB,CAAC,WAAmB,GAAG,UAAU;AAQ5D,IAAM,qCAAqC,CAChD,UAEA,CAAC,CAAC,SAAS,OAAO,UAAU,aAAY,yBAAyB;;;AC3B5D,SAAS,kBAAkB,CAAC,YAA0B;AAAA,EAC3D,IAAI,WAAW,SAAS,wBAAwB;AAAA,IAC9C,MAAM,IAAI,oBACR,wCAAwC,0CAA0C,WAAW,WAC7F,UACF;AAAA,EACF;AAAA;AAGK,SAAS,kBAAkB,CAAC,YAA6C;AAAA,EAC9E,IAAI,cAAc,QAAQ,WAAW,SAAS,wBAAwB;AAAA,IACpE,MAAM,IAAI,oBACR,wCAAwC,0CAA0C,WAAW,SAC/F;AAAA,EACF;AAAA;AAAA;AAGK,MAAM,4BAA4B,MAAM;AAAA,EAG3B;AAAA,EACA;AAAA,EACS;A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ANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pg-workflows",
3
3
  "description": "The simplest Postgres workflow engine for TypeScript - durable execution, event-driven orchestration, and automatic retries powered entirely by PostgreSQL",
4
- "version": "0.13.2",
4
+ "version": "0.14.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
@@ -1,16 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["src/constants.ts", "src/error.ts", "src/types.ts", "src/client.ts", "src/db/migration.ts", "src/db/queries.ts", "src/definition.ts"],
4
- "sourcesContent": [
5
- "export const PAUSE_EVENT_NAME = '__internal_pause';\nexport const WORKFLOW_RUN_QUEUE_NAME = 'workflow-run';\nexport const WORKFLOW_RUN_DLQ_QUEUE_NAME = 'workflow_run_dlq';\n// pg-boss queue names allow only alphanumeric, _, -, ., or / — keep the\n// prefix in that character set so any valid workflow id stays addressable.\nconst SCHEDULE_QUEUE_PREFIX = '__pgw_schedule_';\nexport const scheduleQueueNameFor = (workflowId: string): string =>\n `${SCHEDULE_QUEUE_PREFIX}${workflowId}`;\nexport const DEFAULT_PGBOSS_SCHEMA = 'pgboss_v12_pgworkflow';\nexport const MAX_WORKFLOW_ID_LENGTH = 256;\nexport const MAX_RESOURCE_ID_LENGTH = 256;\n\n// Per-step timeline key suffixes. The shared helpers below are the single\n// source of truth for engine + client so timeline-shape changes only need to\n// happen in one place.\nexport const INVOKE_CHILD_WORKFLOW_TIMELINE_SUFFIX = 'invoke-child-workflow';\nexport const WAIT_FOR_TIMELINE_SUFFIX = 'wait-for';\nexport const invokeChildWorkflowTimelineKey = (stepId: string) =>\n `${stepId}-${INVOKE_CHILD_WORKFLOW_TIMELINE_SUFFIX}`;\nexport const waitForTimelineKey = (stepId: string) => `${stepId}-${WAIT_FOR_TIMELINE_SUFFIX}`;\n\n/**\n * Type guard for `${stepId}-invoke-child-workflow` timeline entries. Used by the\n * engine to dispatch invoke-aware behavior and by the client's resume /\n * fast-forward no-op guards. Centralizing this keeps the timeline shape\n * contract in one place.\n */\nexport const isInvokeChildWorkflowTimelineEntry = (\n entry: unknown,\n): entry is { invokeChildWorkflow: { childRunId: string; childWorkflowId: string } } =>\n !!entry && typeof entry === 'object' && 'invokeChildWorkflow' in entry;\n",
6
- "import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { MAX_RESOURCE_ID_LENGTH, MAX_WORKFLOW_ID_LENGTH } from './constants';\n\nexport function validateWorkflowId(workflowId: string): void {\n if (workflowId.length > MAX_WORKFLOW_ID_LENGTH) {\n throw new WorkflowEngineError(\n `workflowId exceeds maximum length of ${MAX_WORKFLOW_ID_LENGTH} characters (got ${workflowId.length})`,\n workflowId,\n );\n }\n}\n\nexport function validateResourceId(resourceId: string | undefined | null): void {\n if (resourceId != null && resourceId.length > MAX_RESOURCE_ID_LENGTH) {\n throw new WorkflowEngineError(\n `resourceId exceeds maximum length of ${MAX_RESOURCE_ID_LENGTH} characters (got ${resourceId.length})`,\n );\n }\n}\n\nexport class WorkflowEngineError extends Error {\n constructor(\n message: string,\n public readonly workflowId?: string,\n public readonly runId?: string,\n public override readonly cause: Error | undefined = undefined,\n public readonly issues?: StandardSchemaV1.FailureResult['issues'] | undefined,\n ) {\n super(message);\n this.name = 'WorkflowEngineError';\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, WorkflowEngineError);\n }\n }\n}\n\nexport class WorkflowRunNotFoundError extends WorkflowEngineError {\n constructor(runId?: string, workflowId?: string) {\n super('Workflow run not found', workflowId, runId);\n this.name = 'WorkflowRunNotFoundError';\n }\n}\n",
7
- "import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport type { WorkflowRun } from './db/types';\nimport type { Duration } from './duration';\nimport type { Schedule } from './schedule';\n\nexport enum WorkflowStatus {\n PENDING = 'pending',\n RUNNING = 'running',\n PAUSED = 'paused',\n COMPLETED = 'completed',\n FAILED = 'failed',\n CANCELLED = 'cancelled',\n}\n\nexport enum StepType {\n PAUSE = 'pause',\n RUN = 'run',\n WAIT_FOR = 'waitFor',\n WAIT_UNTIL = 'waitUntil',\n DELAY = 'delay',\n POLL = 'poll',\n INVOKE_CHILD_WORKFLOW = 'invokeChildWorkflow',\n}\n\nexport type InputParameters = StandardSchemaV1;\nexport type InferInputParameters<P extends InputParameters> = StandardSchemaV1.InferOutput<P>;\n\nexport type StartWorkflowOptions = {\n resourceId?: string;\n timeout?: number;\n retries?: number;\n expireInSeconds?: number;\n idempotencyKey?: string;\n};\n\nexport type WorkflowOptions<I extends InputParameters> = {\n timeout?: number;\n retries?: number;\n inputSchema?: I;\n /**\n * Recurring schedule. Accepts a cron expression (`'0 9 * * 1-5'`),\n * a duration string (`'5m'`, `'1 hour'`), or a `DurationObject`.\n */\n schedule?: Schedule;\n /** IANA timezone for cron expressions. Defaults to UTC. Ignored for duration-based schedules. */\n timezone?: string;\n};\n\n/** Metadata about a scheduled fire, exposed on `ctx.schedule` for runs triggered by a schedule. */\nexport type ScheduleContext = {\n /** Time the schedule fired this run. */\n timestamp: Date;\n};\n\nexport type StepBaseContext = {\n run: <T>(stepId: string, handler: () => Promise<T>) => Promise<T>;\n waitFor: {\n <T extends InputParameters>(\n stepId: string,\n options: { eventName: string; schema?: T },\n ): Promise<InferInputParameters<T>>;\n <T extends InputParameters>(\n stepId: string,\n options: { eventName: string; timeout: number; schema?: T },\n ): Promise<InferInputParameters<T> | undefined>;\n };\n waitUntil: {\n (stepId: string, date: Date): Promise<void>;\n (stepId: string, dateString: string): Promise<void>;\n (stepId: string, options: { date: Date | string }): Promise<void>;\n };\n /** Delay execution for a duration (sugar over waitUntil). Alias: sleep. */\n delay: (stepId: string, duration: Duration) => Promise<void>;\n /** Alias for delay. */\n sleep: (stepId: string, duration: Duration) => Promise<void>;\n pause: (stepId: string) => Promise<void>;\n poll: <T>(\n stepId: string,\n conditionFn: () => Promise<T | false>,\n options?: { interval?: Duration; timeout?: Duration },\n ) => Promise<{ timedOut: false; data: T } | { timedOut: true }>;\n /**\n * Invoke a child workflow from inside the current workflow and pause until\n * the child run reaches a terminal state.\n */\n invokeChildWorkflow: {\n <TInput extends InputParameters, TOutput = unknown>(\n stepId: string,\n ref: WorkflowRef<TInput, TOutput>,\n input: InferInputParameters<TInput>,\n options?: StartWorkflowOptions,\n ): Promise<TOutput>;\n <TOutput = unknown>(\n stepId: string,\n params: {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n },\n ): Promise<TOutput>;\n };\n};\n\n/**\n * Plugin that extends the workflow step API with extra methods.\n * @template TStepBase - The step type this plugin receives (base + previous plugins).\n * @template TStepExt - The extra methods this plugin adds to step.\n */\nexport interface WorkflowPlugin<TStepBase = StepBaseContext, TStepExt = object> {\n name: string;\n methods: (step: TStepBase, context: WorkflowContext) => TStepExt;\n /**\n * Optional middleware around the workflow handler call. Composes in\n * registration order — the first plugin passed to `.use()` wraps everything\n * inside. Implementations MUST call `next()` exactly once.\n */\n wrap?: (context: WorkflowContext, next: () => Promise<unknown>) => Promise<unknown>;\n}\n\nexport type WorkflowContext<\n TInput extends InputParameters = InputParameters,\n TStep extends StepBaseContext = StepBaseContext,\n> = {\n input: InferInputParameters<TInput>;\n step: TStep;\n workflowId: string;\n runId: string;\n /** Tenant/scope identifier set when the run was started, if any. */\n resourceId?: string;\n /** Zero-based retry attempt number (= `run.retryCount`). */\n attempt: number;\n timeline: Record<string, unknown>;\n logger: WorkflowLogger;\n /** Set only for runs triggered by a recurring schedule. */\n schedule?: ScheduleContext;\n};\n\nexport type WorkflowDefinition<TInput extends InputParameters = InputParameters> = {\n id: string;\n /** Widest context avoids contravariance when collecting definitions; `workflow()` still types the handler narrowly. */\n handler: (context: WorkflowContext<InputParameters, StepBaseContext>) => Promise<unknown>;\n inputSchema?: TInput;\n timeout?: number; // milliseconds\n retries?: number;\n schedule?: Schedule;\n timezone?: string;\n plugins?: WorkflowPlugin[];\n};\n\nexport type StepInternalDefinition = {\n id: string;\n type: StepType;\n conditional: boolean;\n loop: boolean;\n isDynamic: boolean;\n};\n\nexport type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters> =\n WorkflowDefinition<TInput> & {\n steps: StepInternalDefinition[];\n };\n\n/**\n * Chainable workflow factory: call as (id, handler, options) and/or use .use(plugin).\n * TStepExt is the accumulated step extension from all plugins (step = StepContext & TStepExt).\n */\nexport interface WorkflowFactory<TStepExt = object> {\n <I extends InputParameters = InputParameters>(\n id: string,\n handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>,\n options?: WorkflowOptions<I>,\n ): WorkflowDefinition<I>;\n use<TNewExt>(\n plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>,\n ): WorkflowFactory<TStepExt & TNewExt>;\n ref<TInput extends InputParameters = InputParameters, TOutput = unknown>(\n id: string,\n options?: { inputSchema?: TInput },\n ): WorkflowRef<TInput, TOutput>;\n}\n\n/**\n * Lightweight workflow reference - carries the workflow ID and input type\n * but no handler code. Safe to import in API services without pulling in\n * heavy worker dependencies.\n *\n * Callable: pass a handler to create a full WorkflowDefinition.\n */\nexport interface WorkflowRef<\n TInput extends InputParameters = InputParameters,\n // biome-ignore lint/correctness/noUnusedVariables: phantom type carried for typed return inference\n TOutput = unknown,\n> {\n (\n handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>,\n options?: Omit<WorkflowOptions<TInput>, 'inputSchema'>,\n ): WorkflowDefinition<TInput>;\n readonly id: string;\n readonly inputSchema?: TInput;\n}\n\nexport type WorkflowRunProgress = WorkflowRun & {\n completionPercentage: number;\n totalSteps: number;\n completedSteps: number;\n};\n\nexport interface WorkflowLogger {\n log(message: string): void;\n error(message: string, ...args: unknown[]): void;\n}\n\nexport type WorkflowInternalLoggerContext = {\n runId?: string;\n workflowId?: string;\n};\n\nexport interface WorkflowInternalLogger {\n log(message: string, context?: WorkflowInternalLoggerContext): void;\n error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;\n}\n\nconst _STEP_BASE_METHOD_TO_TYPE: Record<keyof StepBaseContext, StepType> = {\n run: StepType.RUN,\n waitFor: StepType.WAIT_FOR,\n waitUntil: StepType.WAIT_UNTIL,\n delay: StepType.DELAY,\n sleep: StepType.DELAY,\n pause: StepType.PAUSE,\n poll: StepType.POLL,\n invokeChildWorkflow: StepType.INVOKE_CHILD_WORKFLOW,\n};\n\nexport const STEP_BASE_METHOD_TYPES: ReadonlyMap<string, StepType> = new Map(\n Object.entries(_STEP_BASE_METHOD_TO_TYPE),\n);\n",
8
- "import { merge } from 'es-toolkit';\nimport pg from 'pg';\nimport { type Db, PgBoss } from 'pg-boss';\nimport {\n DEFAULT_PGBOSS_SCHEMA,\n invokeChildWorkflowTimelineKey,\n isInvokeChildWorkflowTimelineEntry,\n PAUSE_EVENT_NAME,\n WORKFLOW_RUN_QUEUE_NAME,\n waitForTimelineKey,\n} from './constants';\nimport { runMigrations } from './db/migration';\nimport {\n getWorkflowRun,\n getWorkflowRuns,\n insertWorkflowRun,\n updateWorkflowRun,\n withPostgresTransaction,\n} from './db/queries';\nimport type { WorkflowRun } from './db/types';\nimport {\n validateResourceId,\n validateWorkflowId,\n WorkflowEngineError,\n WorkflowRunNotFoundError,\n} from './error';\nimport {\n type InferInputParameters,\n type InputParameters,\n type StartWorkflowOptions,\n type WorkflowLogger,\n type WorkflowRef,\n type WorkflowRunProgress,\n WorkflowStatus,\n} from './types';\n\nconst LOG_PREFIX = '[WorkflowClient]';\n\ntype WorkflowRunJobParameters = {\n runId: string;\n resourceId?: string;\n workflowId: string;\n input: unknown;\n event?: {\n name: string;\n data?: Record<string, unknown>;\n };\n};\n\nexport type WorkflowClientOptions = {\n logger?: WorkflowLogger;\n /**\n * Pre-configured pg-boss instance. Pass this when the engine side uses a\n * non-default pg-boss config (schema, retention, logger, etc.) so the\n * client enqueues jobs where the engine reads them. Mirrors the same\n * option on `WorkflowEngineOptions`.\n */\n boss?: PgBoss;\n} & ({ pool: pg.Pool; connectionString?: never } | { connectionString: string; pool?: never });\n\nconst defaultLogger: WorkflowLogger = {\n log: (_message: string) => console.warn(_message),\n error: (message: string, error: Error) => console.error(message, error),\n};\n\nconst defaultExpireInSeconds = process.env.WORKFLOW_RUN_EXPIRE_IN_SECONDS\n ? Number.parseInt(process.env.WORKFLOW_RUN_EXPIRE_IN_SECONDS, 10)\n : 5 * 60;\n\nexport class WorkflowClient {\n private boss: PgBoss;\n private db: Db;\n private pool: pg.Pool;\n private _ownsPool = false;\n private _started = false;\n private logger: WorkflowLogger;\n\n constructor({ logger, boss, ...connectionOptions }: WorkflowClientOptions) {\n this.logger = logger ?? defaultLogger;\n\n if ('pool' in connectionOptions && connectionOptions.pool) {\n this.pool = connectionOptions.pool;\n } else if ('connectionString' in connectionOptions && connectionOptions.connectionString) {\n this.pool = new pg.Pool({ connectionString: connectionOptions.connectionString });\n this._ownsPool = true;\n } else {\n throw new WorkflowEngineError('Either pool or connectionString must be provided');\n }\n\n const db: Db = {\n executeSql: (text: string, values?: unknown[]) =>\n this.pool.query(text, values) as Promise<{ rows: unknown[] }>,\n };\n\n if (boss) {\n this.boss = boss;\n } else {\n this.boss = new PgBoss({ db, schema: DEFAULT_PGBOSS_SCHEMA });\n }\n this.db = db;\n }\n\n async start(): Promise<void> {\n if (this._started) {\n return;\n }\n\n await this.boss.start();\n this.db = this.boss.getDb();\n await runMigrations(this.db);\n await this.boss.createQueue(WORKFLOW_RUN_QUEUE_NAME);\n\n this._started = true;\n this.logger.log(`${LOG_PREFIX} Client started`);\n }\n\n async stop(): Promise<void> {\n await this.boss.stop();\n\n if (this._ownsPool) {\n await this.pool.end();\n }\n\n this._started = false;\n this.logger.log(`${LOG_PREFIX} Client stopped`);\n }\n\n async startWorkflow<TInput extends InputParameters>(\n ref: WorkflowRef<TInput>,\n input: InferInputParameters<TInput>,\n options?: StartWorkflowOptions,\n ): Promise<WorkflowRun>;\n\n async startWorkflow(params: {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n }): Promise<WorkflowRun>;\n\n async startWorkflow<TInput extends InputParameters>(\n refOrParams:\n | WorkflowRef<TInput>\n | {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n },\n inputArg?: InferInputParameters<TInput>,\n optionsArg?: StartWorkflowOptions,\n ): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n let workflowId: string;\n let input: unknown;\n let resourceId: string | undefined;\n let idempotencyKey: string | undefined;\n let options: StartWorkflowOptions | undefined;\n\n if (typeof refOrParams === 'function' && 'id' in refOrParams) {\n const ref = refOrParams as WorkflowRef<TInput>;\n workflowId = ref.id;\n input = inputArg;\n options = optionsArg;\n resourceId = optionsArg?.resourceId;\n idempotencyKey = optionsArg?.idempotencyKey;\n\n if (ref.inputSchema) {\n const result = await ref.inputSchema['~standard'].validate(input);\n if (result.issues) {\n throw new WorkflowEngineError(\n JSON.stringify(result.issues),\n workflowId,\n undefined,\n undefined,\n result.issues,\n );\n }\n }\n } else {\n const params = refOrParams as {\n workflowId: string;\n input: unknown;\n resourceId?: string;\n idempotencyKey?: string;\n options?: StartWorkflowOptions;\n };\n workflowId = params.workflowId;\n input = params.input;\n resourceId = params.resourceId;\n idempotencyKey = params.idempotencyKey;\n options = params.options;\n }\n\n validateWorkflowId(workflowId);\n validateResourceId(resourceId);\n\n const run = await withPostgresTransaction(\n this.db,\n async (_db) => {\n const timeoutAt = options?.timeout ? new Date(Date.now() + options.timeout) : null;\n\n const { run: insertedRun, created } = await insertWorkflowRun(\n {\n resourceId,\n workflowId,\n currentStepId: '__start__',\n status: WorkflowStatus.RUNNING,\n input,\n maxRetries: options?.retries ?? 0,\n timeoutAt,\n idempotencyKey,\n },\n _db,\n );\n\n if (created) {\n const job: WorkflowRunJobParameters = {\n runId: insertedRun.id,\n resourceId,\n workflowId,\n input,\n };\n\n // Same connection (`_db`) used for the workflow_runs INSERT is passed\n // to `boss.send` so the pgboss.job INSERT joins the same transaction.\n // The two writes commit or roll back together, so we never leak an\n // orphan workflow_runs row when the queue insert fails.\n await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {\n startAfter: new Date(),\n expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,\n db: _db,\n });\n }\n\n return insertedRun;\n },\n this.pool,\n );\n\n this.logger.log(`${LOG_PREFIX} Started workflow run ${run.id} for ${workflowId}`);\n\n return run;\n }\n\n async triggerEvent({\n runId,\n resourceId,\n eventName,\n data,\n options,\n }: {\n runId: string;\n resourceId?: string;\n eventName: string;\n data?: Record<string, unknown>;\n options?: { expireInSeconds?: number };\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await this.getRun({ runId, resourceId });\n\n const job: WorkflowRunJobParameters = {\n runId: run.id,\n resourceId: resourceId ?? run.resourceId ?? undefined,\n workflowId: run.workflowId,\n input: run.input,\n event: {\n name: eventName,\n data,\n },\n };\n\n await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {\n expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,\n });\n\n this.logger.log(`${LOG_PREFIX} Event ${eventName} sent for workflow run ${runId}`);\n return run;\n }\n\n async pauseWorkflow({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await updateWorkflowRun(\n {\n runId,\n resourceId,\n data: {\n status: WorkflowStatus.PAUSED,\n pausedAt: new Date(),\n },\n expectedStatuses: [WorkflowStatus.RUNNING, WorkflowStatus.PENDING],\n },\n this.db,\n );\n\n if (!run) {\n throw new WorkflowRunNotFoundError(runId);\n }\n\n this.logger.log(`${LOG_PREFIX} Paused workflow run ${runId}`);\n return run;\n }\n\n async resumeWorkflow({\n runId,\n resourceId,\n options,\n }: {\n runId: string;\n resourceId?: string;\n options?: { expireInSeconds?: number };\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const current = await this.getRun({ runId, resourceId });\n if (current.status !== WorkflowStatus.PAUSED) {\n throw new WorkflowEngineError(\n `Cannot resume workflow run in '${current.status}' status, must be 'paused'`,\n current.workflowId,\n runId,\n );\n }\n\n const currentStepId = current.currentStepId;\n const currentStepTimelineEntry =\n current.timeline[invokeChildWorkflowTimelineKey(currentStepId)];\n if (isInvokeChildWorkflowTimelineEntry(currentStepTimelineEntry)) {\n return current;\n }\n\n return this.triggerEvent({\n runId,\n resourceId,\n eventName: PAUSE_EVENT_NAME,\n data: {},\n options,\n });\n }\n\n async fastForwardWorkflow({\n runId,\n resourceId,\n data,\n }: {\n runId: string;\n resourceId?: string;\n data?: Record<string, unknown>;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await this.getRun({ runId, resourceId });\n\n if (run.status !== WorkflowStatus.PAUSED) {\n return run;\n }\n\n const currentStepId = run.currentStepId;\n const currentStepTimelineEntry = run.timeline[invokeChildWorkflowTimelineKey(currentStepId)];\n if (isInvokeChildWorkflowTimelineEntry(currentStepTimelineEntry)) {\n return run;\n }\n\n const waitForEntry = run.timeline[waitForTimelineKey(currentStepId)];\n if (!waitForEntry || typeof waitForEntry !== 'object' || !('waitFor' in waitForEntry)) {\n return run;\n }\n\n const { eventName, timeoutEvent, skipOutput } = (\n waitForEntry as { waitFor: { eventName?: string; timeoutEvent?: string; skipOutput?: true } }\n ).waitFor;\n\n // step.pause() - delegate to resumeWorkflow\n if (eventName === PAUSE_EVENT_NAME) {\n return this.resumeWorkflow({ runId, resourceId });\n }\n\n // step.poll() - write output to timeline first, then trigger resume\n if (skipOutput && timeoutEvent) {\n await withPostgresTransaction(\n this.db,\n async (db) => {\n const freshRun = await getWorkflowRun({ runId, resourceId }, { exclusiveLock: true, db });\n if (!freshRun) throw new WorkflowRunNotFoundError(runId);\n return updateWorkflowRun(\n {\n runId,\n resourceId,\n data: {\n timeline: merge(freshRun.timeline, {\n [currentStepId]: {\n output: data ?? {},\n timestamp: new Date(),\n },\n }),\n },\n },\n db,\n );\n },\n this.pool,\n );\n\n return this.triggerEvent({ runId, resourceId, eventName: timeoutEvent });\n }\n\n // waitFor steps - trigger the event with data\n if (eventName) {\n return this.triggerEvent({ runId, resourceId, eventName, data: data ?? {} });\n }\n\n // delay/waitUntil steps - trigger the timeout event\n if (timeoutEvent) {\n return this.triggerEvent({ runId, resourceId, eventName: timeoutEvent, data: data ?? {} });\n }\n\n return run;\n }\n\n async cancelWorkflow({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await updateWorkflowRun(\n {\n runId,\n resourceId,\n data: {\n status: WorkflowStatus.CANCELLED,\n },\n expectedStatuses: [WorkflowStatus.PENDING, WorkflowStatus.RUNNING, WorkflowStatus.PAUSED],\n },\n this.db,\n );\n\n if (!run) {\n throw new WorkflowRunNotFoundError(runId);\n }\n\n this.logger.log(`${LOG_PREFIX} Cancelled workflow run ${runId}`);\n return run;\n }\n\n async getRun({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRun> {\n await this.ensureStarted();\n\n const run = await getWorkflowRun({ runId, resourceId }, { db: this.db });\n\n if (!run) {\n throw new WorkflowRunNotFoundError(runId);\n }\n\n return run;\n }\n\n async checkProgress({\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n }): Promise<WorkflowRunProgress> {\n const run = await this.getRun({ runId, resourceId });\n\n const completedSteps = Object.values(run.timeline).filter(\n (entry): entry is { output: unknown; timestamp: Date } =>\n typeof entry === 'object' &&\n entry !== null &&\n 'output' in entry &&\n entry.output !== undefined,\n ).length;\n\n // Without registered workflow definitions, total steps are unknown.\n // Use completed steps as best-effort estimate for in-progress runs.\n const totalSteps = run.status === WorkflowStatus.COMPLETED ? completedSteps : 0;\n const completionPercentage =\n run.status === WorkflowStatus.COMPLETED\n ? 100\n : run.status === WorkflowStatus.FAILED || run.status === WorkflowStatus.CANCELLED\n ? 0\n : 0;\n\n return {\n ...run,\n completedSteps,\n completionPercentage,\n totalSteps,\n };\n }\n\n async getRuns({\n resourceId,\n startingAfter,\n endingBefore,\n limit = 20,\n statuses,\n workflowId,\n }: {\n resourceId?: string;\n startingAfter?: string | null;\n endingBefore?: string | null;\n limit?: number;\n statuses?: WorkflowStatus[];\n workflowId?: string;\n }): Promise<{\n items: WorkflowRun[];\n nextCursor: string | null;\n prevCursor: string | null;\n hasMore: boolean;\n hasPrev: boolean;\n }> {\n await this.ensureStarted();\n\n if (workflowId) validateWorkflowId(workflowId);\n validateResourceId(resourceId);\n\n return getWorkflowRuns(\n {\n resourceId,\n startingAfter,\n endingBefore,\n limit,\n statuses,\n workflowId,\n },\n this.db,\n );\n }\n\n private async ensureStarted(): Promise<void> {\n if (!this._started) {\n await this.start();\n }\n }\n}\n",
9
- "import type { Db } from 'pg-boss';\n\n// Arbitrary but stable lock ID for serializing migrations across processes\nexport const MIGRATION_LOCK_ID = 738291645;\n\n// Bump this when adding new migrations. The engine stores the current version\n// in a `workflow_schema_version` table so migrations only run once per version.\nconst CURRENT_SCHEMA_VERSION = 5;\n\nexport async function runMigrations(db: Db): Promise<void> {\n // Fast path: skip the advisory lock if schema is already current.\n // This is the common case - every engine.start() after initial setup.\n if (await isSchemaUpToDate(db)) {\n return;\n }\n\n // Slow path: build migration SQL based on current version, then execute\n // everything in a single transaction with an advisory lock.\n // This mirrors pg-boss's approach: one executeSql call ensures all DDL\n // runs on a single connection inside BEGIN/COMMIT, and pg_advisory_xact_lock\n // auto-releases on commit or rollback (no manual unlock needed).\n const currentVersion = await getCurrentVersion(db);\n\n const commands: string[] = [];\n\n if (currentVersion < 1) {\n // On first run, we run a check to see if there is an existing foreign\n // `workflow_runs` table. To decide on that, we check if the `workflow_runs`\n // exists but a `workflow_schema_version` table doesn't. If that is true\n // then `workflow_runs` is foreign and we should fail lest we start\n // adding rows there, corrupting it. We also do the reverse check.\n // We are basing this off the fact that our transaction creates both or no tables\n // This is not airtight, but serves as an extra safety check for now.\n const existing = await db.executeSql(\n `SELECT\n to_regclass('workflow_runs') IS NOT NULL AS has_runs_table,\n to_regclass('workflow_schema_version') IS NOT NULL AS has_version_table`,\n [],\n );\n\n const row = existing.rows[0] as\n | { has_runs_table: boolean; has_version_table: boolean }\n | undefined;\n\n if (row?.has_runs_table && !row.has_version_table) {\n throw new Error(\n `pg-workflows: a \"workflow_runs\" table already exists in this schema but was not ` +\n `created by pg-workflows. Point the workflow engine at a dedicated schema/database.`,\n );\n }\n\n if (!row?.has_runs_table && row?.has_version_table) {\n throw new Error(\n `pg-workflows: a \"workflow_schema_version\" table already exists in this schema but was not ` +\n `created by pg-workflows. Point the workflow engine at a dedicated schema/database.`,\n );\n }\n\n commands.push(`\n CREATE TABLE IF NOT EXISTS workflow_runs (\n id varchar(32) PRIMARY KEY NOT NULL,\n created_at timestamp with time zone DEFAULT now() NOT NULL,\n updated_at timestamp with time zone DEFAULT now() NOT NULL,\n resource_id varchar(256),\n workflow_id varchar(256) NOT NULL,\n status text DEFAULT 'pending' NOT NULL,\n input jsonb NOT NULL,\n output jsonb,\n error text,\n current_step_id varchar(256) NOT NULL,\n timeline jsonb DEFAULT '{}'::jsonb NOT NULL,\n paused_at timestamp with time zone,\n resumed_at timestamp with time zone,\n completed_at timestamp with time zone,\n timeout_at timestamp with time zone,\n retry_count integer DEFAULT 0 NOT NULL,\n max_retries integer DEFAULT 0 NOT NULL,\n job_id varchar(256)\n )\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_created_at_idx ON workflow_runs USING btree (created_at)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_resource_id_created_at_idx ON workflow_runs USING btree (resource_id, created_at DESC)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_status_created_at_idx ON workflow_runs USING btree (status, created_at DESC)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_workflow_id_created_at_idx ON workflow_runs USING btree (workflow_id, created_at DESC)\n `);\n commands.push(`\n CREATE INDEX IF NOT EXISTS workflow_runs_resource_id_workflow_id_created_at_idx ON workflow_runs USING btree (resource_id, workflow_id, created_at DESC)\n `);\n }\n\n if (currentVersion < 2) {\n commands.push('DROP INDEX IF EXISTS workflow_runs_workflow_id_idx');\n commands.push('DROP INDEX IF EXISTS workflow_runs_resource_id_idx');\n commands.push(\n 'ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS idempotency_key varchar(256)',\n );\n commands.push(`\n CREATE UNIQUE INDEX IF NOT EXISTS workflow_runs_idempotency_key_idx ON workflow_runs (idempotency_key) WHERE idempotency_key IS NOT NULL\n `);\n }\n\n if (currentVersion < 3) {\n commands.push('ALTER TABLE workflow_runs ALTER COLUMN resource_id TYPE varchar(256)');\n commands.push('ALTER TABLE workflow_runs ALTER COLUMN workflow_id TYPE varchar(256)');\n }\n\n if (currentVersion < 4) {\n commands.push('ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS parent_run_id varchar(32)');\n commands.push('ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS parent_step_id varchar(256)');\n commands.push(\n 'ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS parent_resource_id varchar(256)',\n );\n }\n\n if (currentVersion < 5) {\n commands.push(\n 'ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS scheduled_at timestamp with time zone',\n );\n }\n\n // Upsert the schema version\n if (currentVersion === 0) {\n commands.push(\n `INSERT INTO workflow_schema_version (version) VALUES (${CURRENT_SCHEMA_VERSION})`,\n );\n } else {\n commands.push(`UPDATE workflow_schema_version SET version = ${CURRENT_SCHEMA_VERSION}`);\n }\n\n if (commands.length === 0) {\n return;\n }\n\n const sql = [\n 'BEGIN',\n \"SET LOCAL lock_timeout = '30s'\",\n \"SET LOCAL idle_in_transaction_session_timeout = '30s'\",\n `SELECT pg_advisory_xact_lock(${MIGRATION_LOCK_ID})`,\n 'CREATE TABLE IF NOT EXISTS workflow_schema_version (version integer NOT NULL)',\n ...commands,\n 'COMMIT',\n ].join(';\\n');\n\n await db.executeSql(sql, []);\n}\n\nasync function isSchemaUpToDate(db: Db): Promise<boolean> {\n try {\n const result = await db.executeSql('SELECT version FROM workflow_schema_version LIMIT 1', []);\n return (\n ((result.rows[0] as { version: number } | undefined)?.version ?? 0) >= CURRENT_SCHEMA_VERSION\n );\n } catch {\n // Table doesn't exist yet - needs migration\n return false;\n }\n}\n\nasync function getCurrentVersion(db: Db): Promise<number> {\n try {\n const result = await db.executeSql('SELECT version FROM workflow_schema_version LIMIT 1', []);\n return (result.rows[0] as { version: number } | undefined)?.version ?? 0;\n } catch {\n return 0;\n }\n}\n",
10
- "import ksuid from 'ksuid';\nimport type { Db } from 'pg-boss';\nimport type { WorkflowRun } from './types';\n\nexport function generateKSUID(prefix?: string): string {\n return `${prefix ? `${prefix}_` : ''}${ksuid.randomSync().string}`;\n}\n\ntype WorkflowRunRow = {\n id: string;\n created_at: string | Date;\n updated_at: string | Date;\n resource_id: string | null;\n workflow_id: string;\n status: 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'cancelled';\n input: string | unknown;\n output: string | unknown | null;\n error: string | null;\n current_step_id: string;\n timeline: string | Record<string, unknown>;\n paused_at: string | Date | null;\n resumed_at: string | Date | null;\n completed_at: string | Date | null;\n timeout_at: string | Date | null;\n retry_count: number;\n max_retries: number;\n job_id: string | null;\n idempotency_key: string | null;\n parent_run_id: string | null;\n parent_step_id: string | null;\n parent_resource_id: string | null;\n scheduled_at: string | Date | null;\n};\n\nfunction mapRowToWorkflowRun(row: WorkflowRunRow): WorkflowRun {\n return {\n id: row.id,\n createdAt: new Date(row.created_at),\n updatedAt: new Date(row.updated_at),\n resourceId: row.resource_id,\n workflowId: row.workflow_id,\n status: row.status,\n input: typeof row.input === 'string' ? JSON.parse(row.input) : row.input,\n output:\n typeof row.output === 'string'\n ? row.output.trim().startsWith('{') || row.output.trim().startsWith('[')\n ? JSON.parse(row.output)\n : row.output\n : (row.output ?? null),\n error: row.error,\n currentStepId: row.current_step_id,\n timeline: typeof row.timeline === 'string' ? JSON.parse(row.timeline) : row.timeline,\n pausedAt: row.paused_at ? new Date(row.paused_at) : null,\n resumedAt: row.resumed_at ? new Date(row.resumed_at) : null,\n completedAt: row.completed_at ? new Date(row.completed_at) : null,\n timeoutAt: row.timeout_at ? new Date(row.timeout_at) : null,\n retryCount: row.retry_count,\n maxRetries: row.max_retries,\n jobId: row.job_id,\n idempotencyKey: row.idempotency_key,\n parentRunId: row.parent_run_id,\n parentStepId: row.parent_step_id,\n parentResourceId: row.parent_resource_id,\n scheduledAt: row.scheduled_at ? new Date(row.scheduled_at) : null,\n };\n}\n\nexport async function insertWorkflowRun(\n {\n resourceId,\n workflowId,\n currentStepId,\n status,\n input,\n maxRetries,\n timeoutAt,\n idempotencyKey,\n parentRunId,\n parentStepId,\n parentResourceId,\n scheduledAt,\n }: {\n resourceId?: string;\n workflowId: string;\n currentStepId: string;\n status: string;\n input: unknown;\n maxRetries: number;\n timeoutAt: Date | null;\n idempotencyKey?: string;\n parentRunId?: string;\n parentStepId?: string;\n parentResourceId?: string;\n scheduledAt?: Date;\n },\n db: Db,\n): Promise<{ run: WorkflowRun; created: boolean }> {\n const runId = generateKSUID('run');\n const now = new Date();\n\n const result = await db.executeSql(\n `INSERT INTO workflow_runs (\n id,\n resource_id,\n workflow_id,\n current_step_id,\n status,\n input,\n max_retries,\n timeout_at,\n created_at,\n updated_at,\n timeline,\n retry_count,\n idempotency_key,\n parent_run_id,\n parent_step_id,\n parent_resource_id,\n scheduled_at\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)\n ON CONFLICT (idempotency_key) WHERE idempotency_key IS NOT NULL DO NOTHING\n RETURNING *`,\n [\n runId,\n resourceId ?? null,\n workflowId,\n currentStepId,\n status,\n JSON.stringify(input),\n maxRetries,\n timeoutAt,\n now,\n now,\n '{}',\n 0,\n idempotencyKey ?? null,\n parentRunId ?? null,\n parentStepId ?? null,\n parentResourceId ?? null,\n scheduledAt ?? null,\n ],\n );\n\n if (result.rows[0]) {\n return { run: mapRowToWorkflowRun(result.rows[0]), created: true };\n }\n\n // Conflict - fetch the existing row\n const existing = await db.executeSql('SELECT * FROM workflow_runs WHERE idempotency_key = $1', [\n idempotencyKey,\n ]);\n\n if (!existing.rows[0]) {\n throw new Error(`Idempotency conflict: existing run not found for key \"${idempotencyKey}\"`);\n }\n\n return { run: mapRowToWorkflowRun(existing.rows[0]), created: false };\n}\n\nexport async function getWorkflowRun(\n {\n runId,\n resourceId,\n }: {\n runId: string;\n resourceId?: string;\n },\n { exclusiveLock = false, db }: { exclusiveLock?: boolean; db: Db },\n): Promise<WorkflowRun | null> {\n const lockSuffix = exclusiveLock ? 'FOR UPDATE' : '';\n\n const result = resourceId\n ? await db.executeSql(\n `SELECT * FROM workflow_runs \n WHERE id = $1 AND resource_id = $2\n ${lockSuffix}`,\n [runId, resourceId],\n )\n : await db.executeSql(\n `SELECT * FROM workflow_runs \n WHERE id = $1\n ${lockSuffix}`,\n [runId],\n );\n\n const run = result.rows[0];\n\n if (!run) {\n return null;\n }\n\n return mapRowToWorkflowRun(run);\n}\n\nexport async function getWorkflowLastRun(\n {\n workflowId,\n resourceId,\n }: {\n workflowId: string;\n resourceId?: string;\n },\n db: Db,\n): Promise<WorkflowRun | null> {\n const result = resourceId\n ? await db.executeSql(\n `SELECT * FROM workflow_runs\n WHERE workflow_id = $1 AND resource_id = $2\n ORDER BY created_at DESC\n LIMIT 1`,\n [workflowId, resourceId],\n )\n : await db.executeSql(\n `SELECT * FROM workflow_runs\n WHERE workflow_id = $1\n ORDER BY created_at DESC\n LIMIT 1`,\n [workflowId],\n );\n\n const run = result.rows[0];\n if (!run) {\n return null;\n }\n return mapRowToWorkflowRun(run);\n}\n\nexport async function updateWorkflowRun(\n {\n runId,\n resourceId,\n data,\n expectedStatuses,\n }: {\n runId: string;\n resourceId?: string;\n data: Partial<WorkflowRun>;\n expectedStatuses?: string[];\n },\n db: Db,\n): Promise<WorkflowRun | null> {\n const now = new Date();\n\n const updates: string[] = ['updated_at = $1'];\n const values: (string | number | Date | null | string[])[] = [now];\n let paramIndex = 2;\n\n if (data.status !== undefined) {\n updates.push(`status = $${paramIndex}`);\n values.push(data.status);\n paramIndex++;\n }\n if (data.currentStepId !== undefined) {\n updates.push(`current_step_id = $${paramIndex}`);\n values.push(data.currentStepId);\n paramIndex++;\n }\n if (data.timeline !== undefined) {\n updates.push(`timeline = $${paramIndex}`);\n values.push(JSON.stringify(data.timeline));\n paramIndex++;\n }\n if (data.pausedAt !== undefined) {\n updates.push(`paused_at = $${paramIndex}`);\n values.push(data.pausedAt);\n paramIndex++;\n }\n if (data.resumedAt !== undefined) {\n updates.push(`resumed_at = $${paramIndex}`);\n values.push(data.resumedAt);\n paramIndex++;\n }\n if (data.completedAt !== undefined) {\n updates.push(`completed_at = $${paramIndex}`);\n values.push(data.completedAt);\n paramIndex++;\n }\n if (data.output !== undefined) {\n updates.push(`output = $${paramIndex}`);\n values.push(JSON.stringify(data.output));\n paramIndex++;\n }\n if (data.error !== undefined) {\n updates.push(`error = $${paramIndex}`);\n values.push(data.error);\n paramIndex++;\n }\n if (data.retryCount !== undefined) {\n updates.push(`retry_count = $${paramIndex}`);\n values.push(data.retryCount);\n paramIndex++;\n }\n if (data.jobId !== undefined) {\n updates.push(`job_id = $${paramIndex}`);\n values.push(data.jobId);\n paramIndex++;\n }\n\n values.push(runId);\n const idParam = paramIndex;\n paramIndex++;\n\n if (resourceId) {\n values.push(resourceId);\n paramIndex++;\n }\n\n if (expectedStatuses && expectedStatuses.length > 0) {\n values.push(expectedStatuses);\n paramIndex++;\n }\n\n let whereClause = resourceId\n ? `WHERE id = $${idParam} AND resource_id = $${idParam + 1}`\n : `WHERE id = $${idParam}`;\n\n if (expectedStatuses && expectedStatuses.length > 0) {\n whereClause += ` AND status = ANY($${paramIndex - 1})`;\n }\n\n const query = `\n UPDATE workflow_runs \n SET ${updates.join(', ')}\n ${whereClause}\n RETURNING *\n `;\n\n const result = await db.executeSql(query, values);\n const run = result.rows[0];\n\n if (!run) {\n return null;\n }\n\n return mapRowToWorkflowRun(run);\n}\n\nexport async function getWorkflowRuns(\n {\n resourceId,\n startingAfter,\n endingBefore,\n limit = 20,\n statuses,\n workflowId,\n }: {\n resourceId?: string;\n startingAfter?: string | null;\n endingBefore?: string | null;\n limit?: number;\n statuses?: string[];\n workflowId?: string;\n },\n db: Db,\n): Promise<{\n items: WorkflowRun[];\n nextCursor: string | null;\n prevCursor: string | null;\n hasMore: boolean;\n hasPrev: boolean;\n}> {\n const conditions: string[] = [];\n const values: (string | number | string[] | Date)[] = [];\n let paramIndex = 1;\n\n if (resourceId) {\n conditions.push(`resource_id = $${paramIndex}`);\n values.push(resourceId);\n paramIndex++;\n }\n\n if (statuses && statuses.length > 0) {\n conditions.push(`status = ANY($${paramIndex})`);\n values.push(statuses);\n paramIndex++;\n }\n\n if (workflowId) {\n conditions.push(`workflow_id = $${paramIndex}`);\n values.push(workflowId);\n paramIndex++;\n }\n\n const cursorIds = [startingAfter, endingBefore].filter(Boolean) as string[];\n if (cursorIds.length > 0) {\n const cursorResult = await db.executeSql(\n 'SELECT id, created_at FROM workflow_runs WHERE id = ANY($1)',\n [cursorIds],\n );\n const cursorMap = new Map<string, Date>();\n for (const row of cursorResult.rows) {\n cursorMap.set(\n row.id,\n typeof row.created_at === 'string' ? new Date(row.created_at) : row.created_at,\n );\n }\n\n if (startingAfter) {\n const cursor = cursorMap.get(startingAfter);\n if (cursor) {\n conditions.push(`created_at < $${paramIndex}`);\n values.push(cursor);\n paramIndex++;\n }\n }\n\n if (endingBefore) {\n const cursor = cursorMap.get(endingBefore);\n if (cursor) {\n conditions.push(`created_at > $${paramIndex}`);\n values.push(cursor);\n paramIndex++;\n }\n }\n }\n\n const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';\n const actualLimit = Math.min(Math.max(limit, 1), 100) + 1;\n\n const isBackward = !!endingBefore && !startingAfter;\n\n const query = `\n SELECT * FROM workflow_runs\n ${whereClause}\n ORDER BY created_at ${isBackward ? 'ASC' : 'DESC'}\n LIMIT $${paramIndex}\n `;\n values.push(actualLimit);\n\n const result = await db.executeSql(query, values);\n const rows = result.rows;\n\n const hasExtraRow = rows.length > (limit ?? 20);\n const rawItems = hasExtraRow ? rows.slice(0, limit) : rows;\n\n if (isBackward) {\n rawItems.reverse();\n }\n\n const items = rawItems.map((row) => mapRowToWorkflowRun(row));\n\n const hasMore = isBackward ? items.length > 0 : hasExtraRow;\n const hasPrev = isBackward ? hasExtraRow : !!startingAfter && items.length > 0;\n\n const nextCursor = hasMore && items.length > 0 ? (items[items.length - 1]?.id ?? null) : null;\n const prevCursor = hasPrev && items.length > 0 ? (items[0]?.id ?? null) : null;\n\n return { items, nextCursor, prevCursor, hasMore, hasPrev };\n}\n\n/**\n * Run a callback inside a PostgreSQL transaction using a dedicated client.\n *\n * When a `pool` is provided, a dedicated client is checked out so that\n * BEGIN / COMMIT / ROLLBACK all execute on the **same** connection.\n * This is critical for `SELECT … FOR UPDATE` locks and any work that\n * yields to the event-loop inside the transaction (e.g. async step handlers).\n *\n * Falls back to the pg-boss `Db` adapter when no pool is given (unit-test path).\n */\nexport async function withPostgresTransaction<T>(\n db: Db,\n callback: (db: Db) => Promise<T>,\n pool?: {\n connect: () => Promise<{\n query: (text: string, values?: unknown[]) => Promise<unknown>;\n release: () => void;\n }>;\n },\n): Promise<T> {\n let txDb: Db;\n let release: (() => void) | undefined;\n\n if (pool) {\n const client = await pool.connect();\n txDb = {\n executeSql: (text: string, values?: unknown[]) =>\n client.query(text, values) as Promise<{ rows: unknown[] }>,\n };\n release = () => client.release();\n } else {\n txDb = db;\n }\n\n try {\n await txDb.executeSql('BEGIN', []);\n const result = await callback(txDb);\n await txDb.executeSql('COMMIT', []);\n return result;\n } catch (error) {\n await txDb.executeSql('ROLLBACK', []);\n throw error;\n } finally {\n release?.();\n }\n}\n",
11
- "import type {\n InputParameters,\n StepBaseContext,\n WorkflowContext,\n WorkflowDefinition,\n WorkflowFactory,\n WorkflowOptions,\n WorkflowPlugin,\n WorkflowRef,\n} from './types';\n\n/**\n * Create a lightweight workflow reference.\n * Safe to import from `pg-workflows/client` - no engine or handler code.\n */\nexport function createWorkflowRef<\n TOutput = unknown,\n TInput extends InputParameters = InputParameters,\n>(id: string, options?: { inputSchema?: TInput }): WorkflowRef<TInput, TOutput> {\n const ref = ((\n handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>,\n defineOptions?: Omit<WorkflowOptions<TInput>, 'inputSchema'>,\n ): WorkflowDefinition<TInput> => ({\n id,\n handler: handler as (\n context: WorkflowContext<InputParameters, StepBaseContext>,\n ) => Promise<unknown>,\n inputSchema: options?.inputSchema,\n timeout: defineOptions?.timeout,\n retries: defineOptions?.retries,\n schedule: defineOptions?.schedule,\n timezone: defineOptions?.timezone,\n })) as WorkflowRef<TInput, TOutput>;\n\n Object.defineProperty(ref, 'id', { value: id, enumerable: true });\n Object.defineProperty(ref, 'inputSchema', {\n value: options?.inputSchema,\n enumerable: true,\n });\n\n return ref;\n}\n\nfunction createWorkflowFactory<TStepExt extends object = object>(\n plugins: Array<WorkflowPlugin<unknown, object>> = [],\n): WorkflowFactory<TStepExt> {\n const factory = (<I extends InputParameters>(\n id: string,\n handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>,\n { inputSchema, timeout, retries, schedule, timezone }: WorkflowOptions<I> = {},\n ): WorkflowDefinition<I> => ({\n id,\n handler: handler as (\n context: WorkflowContext<InputParameters, StepBaseContext>,\n ) => Promise<unknown>,\n inputSchema,\n timeout,\n retries,\n schedule,\n timezone,\n plugins: plugins.length > 0 ? (plugins as WorkflowPlugin[]) : undefined,\n })) as WorkflowFactory<TStepExt>;\n\n factory.use = <TNewExt>(\n plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>,\n ): WorkflowFactory<TStepExt & TNewExt> =>\n createWorkflowFactory<TStepExt & TNewExt>([\n ...plugins,\n plugin as WorkflowPlugin<unknown, object>,\n ]);\n\n factory.ref = createWorkflowRef;\n\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