import type { ActivityCallable, ActivityCallOptions } from '../types.ts'; import { WeftError } from '../weft-error.ts'; type BareActivityFunction = ((input: TInput) => Promise | TResult) & { execute?: never; }; type BareNoInputActivityFunction = (() => Promise | TResult) & { execute?: never; }; export type DurableActivityInput = string | ActivityCallable | BareNoInputActivityFunction | ActivityCallable | BareActivityFunction; export interface DurableActivityInvocation { activity: DurableActivityInput; arguments: readonly unknown[]; callerStack: string; } export interface DurableActivityScope { dispatch(invocation: DurableActivityInvocation): Promise; } /** * Thrown when `durableActivity()` is called without an active inline `ctx.memo` * durable-activity scope, or after that scope has already closed. * * The error usually means helper code escaped the memo callback boundary, ran * under Worker execution mode, or started a helper activity without awaiting it * before the memo callback returned. * * @example * ```ts * import { durableActivity, DurableActivityScopeError } from '@lostgradient/weft'; * * async function sendOutsideWorkflow(): Promise { * try { * await durableActivity('sendEmail', { userId: 'user-1' }); * } catch (error) { * if (error instanceof DurableActivityScopeError) { * console.error(error.message); * } * } * } * * void sendOutsideWorkflow; * ``` */ export declare class DurableActivityScopeError extends WeftError<'DurableActivityScopeError'> { constructor(message: string); } /** * Thrown when helper-launched activity code reaches an activity feature that * cannot safely be represented inside a plain async memo callback. * * The first unsupported feature is `ActivityContext.completeAsync()`: async * activity completion must use `yield* ctx.run()` directly so the parked * completion token stays attached to a normal workflow operation. * * @example * ```ts * import { durableActivity, DurableActivityUnsupportedError, workflow } from '@lostgradient/weft'; * * export const completionWorkflow = workflow({ name: 'manual-completion' }).execute(async function* ( * ctx, * ) { * try { * yield* ctx.memo('manual-completion-helper', async () => * durableActivity('manualCompletionActivity', undefined, { * idempotencyKey: 'manual-completion:1', * }), * ); * } catch (error) { * if (error instanceof DurableActivityUnsupportedError) { * console.error(error.message); * } * } * }); * ``` */ export declare class DurableActivityUnsupportedError extends WeftError<'DurableActivityUnsupportedError'> { constructor(message: string); } export declare function runWithDurableActivityScope(scope: DurableActivityScope, execute: () => TResult): TResult; /** * Run a durable activity from plain async helper code while an inline workflow * is executing a `ctx.memo()` callback. * * `durableActivity()` gives shared async helpers the same activity dispatch * machinery as `yield* ctx.run()` without converting the helper stack to * generators. It is intentionally scoped: call it only from code awaited by one * inline `ctx.memo()` callback, and await each helper activity sequentially * before the callback returns. Activities with `idempotencyKey` get immediate * lease-fenced result durability for the crash window between the activity * returning and the memo checkpoint commit; unkeyed helper activities keep the * normal at-least-once crash behavior. * * @example * ```ts * import { durableActivity, workflow } from '@lostgradient/weft'; * * async function reserveInventory(input: { orderId: string }): Promise<{ reservationId: string }> { * return { reservationId: input.orderId }; * } * * export const checkout = workflow({ name: 'checkout' }).execute(async function* ( * ctx, * input: { orderId: string }, * ) { * return yield* ctx.memo('reserve-inventory', async () => * durableActivity(reserveInventory, { orderId: input.orderId }, { * idempotencyKey: `reserve:${input.orderId}`, * }), * ); * }); * ``` */ export declare function durableActivity(name: string, input?: unknown, options?: ActivityCallOptions): Promise; export declare function durableActivity(fn: ActivityCallable, options?: ActivityCallOptions): Promise; export declare function durableActivity(fn: BareNoInputActivityFunction, options?: ActivityCallOptions): Promise; export declare function durableActivity(fn: ActivityCallable, input: TInput, options?: ActivityCallOptions): Promise; export declare function durableActivity(fn: BareActivityFunction, input: TInput, options?: ActivityCallOptions): Promise; export {};