// Copyright (c) 2022-2023 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 /***************************** * * NOTE: this is copy & pasted from the types/ folder, as when bazel * runs it doesn't have access to that directly and thusly is sad. * TODO: come up with a better system for this. * ****************************** / /** * NonRetryableError allows for a user to throw a fatal error * that makes a Workflow instance fail immediately without triggering a retry */ declare abstract class NonRetryableError extends Error { /** * `__brand` is used to differentiate between `NonRetryableError` and `Error` * and is omitted from the constructor because users should not set it */ constructor(message: string, name?: string); } declare abstract class Workflow { /** * Get a handle to an existing instance of the Workflow. * @param id Id for the instance of this Workflow * @returns A promise that resolves with a handle for the Instance */ get(id: string): Promise; /** * Create a new instance and return a handle to it. If a provided id exists, an error will be thrown. * @param options Options when creating an instance including name and params * @returns A promise that resolves with a handle for the Instance */ create( options?: WorkflowInstanceCreateOptions ): Promise; /** * Create a batch of instances and return handle for all of them. If a provided id exists, an error will be thrown. * `createBatch` is limited at 100 instances at a time or when the RPC limit (1MiB) is reached. * @param batch List of Options when creating an instance including name and params * @returns A promise that resolves with a list of handles for the created instances. */ createBatch( batch: WorkflowInstanceCreateOptions[] ): Promise; } type WorkflowDurationLabel = | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'; type WorkflowSleepDuration = | `${number} ${WorkflowDurationLabel}${'s' | ''}` | number; type WorkflowRetentionDuration = WorkflowSleepDuration; interface WorkflowInstanceCreateOptions { /** * An id for your Workflow instance. Must be unique within the Workflow. * This is automatically generated if not passed in. */ id?: string; /** * The event payload the Workflow instance is triggered with */ params?: PARAMS; /** * The retention policy for the Workflow instance. * Defaults to the maximum retention period available for the owner's account. */ retention?: { successRetention?: WorkflowRetentionDuration; errorRetention?: WorkflowRetentionDuration; }; } type InstanceStatus = { status: | 'queued' // means that instance is waiting to be started (see concurrency limits) | 'running' | 'paused' | 'errored' | 'terminated' // user terminated the instance while it was running | 'complete' | 'waiting' // instance is hibernating and waiting for sleep or event to finish | 'waitingForPause' // instance is finishing the current work to pause | 'unknown'; error?: { name: string; message: string; }; output?: unknown; }; interface WorkflowError { code?: number; message: string; } interface WorkflowInstanceRestartOptions { /** * Restart from a specific step. If omitted, the instance restarts from the beginning. * The step must exist in the instance's execution history. */ from?: { /** * The step name as defined in your workflow code. */ name: string; /** * 1-indexed occurrence of this step name. Use when the same step name appears multiple times (e.g. in a loop). * @default 1 */ count?: number; /** * Step type filter. Use when different step types share the same name. */ type?: 'do' | 'sleep' | 'waitForEvent'; }; } declare abstract class WorkflowInstance { id: string; /** * Pause the instance. */ pause(): Promise; /** * Resume the instance. If it is already running, an error will be thrown. */ resume(): Promise; /** * Terminate the instance. If it is errored, terminated or complete, an error will be thrown. */ terminate(): Promise; /** * Restart the instance. Optionally restart from a specific step, preserving * cached results for all steps before it. * @param options Options for the restart, including an optional step to restart from. */ restart(options?: WorkflowInstanceRestartOptions): Promise; /** * Returns the current status of the instance. */ status(): Promise; /** * Send an event to this instance. */ sendEvent({ type, payload, }: { type: string; payload: unknown; }): Promise; }