import { HotMesh } from '../hotmesh'; import { DurableJobExport, ExportOptions, ExecutionExportOptions, WorkflowExecution } from '../../types/exporter'; import { JobInterruptOptions } from '../../types/job'; import { StreamError } from '../../types/stream'; import { ExporterService } from './exporter'; import { EscalationClientService } from '../escalations/client'; /** * Handle to a running or completed workflow execution. Returned by * `client.workflow.start()` and `client.workflow.getHandle()`. * * @example * ```typescript * const handle = await client.workflow.start({ * args: ['order-123'], * taskQueue: 'orders', * workflowName: 'orderWorkflow', * workflowId: Durable.guid(), * }); * * // Await the final result * const result = await handle.result(); * * // Or interact while running * await handle.signal('approval', { approved: true }); * await handle.cancel(); * ``` */ export declare class WorkflowHandleService { /** * @private */ exporter: ExporterService; hotMesh: HotMesh; workflowTopic: string; workflowId: string; /** @private */ escalationClient?: EscalationClientService; /** * @private */ constructor(hotMesh: HotMesh, workflowTopic: string, workflowId: string, escalationClient?: EscalationClientService); /** * Export the raw workflow state as a {@link DurableJobExport} with five sections: * * - **data** — workflow input arguments * - **state** — done flag, response, error, timestamps * - **status** — semaphore (`0` = complete, `> 0` = pending, `< 0` = error) * - **timeline** — ordered idempotent markers for activities, children, sleeps, signals * - **transitions** — per-activity execution timestamps by dimension * * Use `allow` / `block` to limit sections and `values: false` to strip payloads. * * @example * ```typescript * const raw = await handle.export(); * console.log(raw.state); // { done: true, response: '...' } * console.log(raw.timeline); // [{ key: '-proxy-1-', value: {...} }, ...] * * // Lightweight export: timeline keys only, no payloads * const slim = await handle.export({ allow: ['timeline'], values: false }); * ``` */ export(options?: ExportOptions): Promise; /** * Export the workflow as a structured event history ({@link WorkflowExecution}). * * Returns a chronologically ordered event list with Temporal-style typed events, * back-references between scheduled/completed pairs, and a summary with counts. * * **Event types:** `activity_task_scheduled/completed/failed`, * `child_workflow_execution_started/completed/failed`, `timer_started/fired`, * `workflow_execution_started/completed/failed/signaled` * * **Modes:** * - `sparse` (default) — single query, transforms timeline markers into events * - `verbose` — recursively fetches child workflows as nested `children` * * **Options:** `exclude_system`, `omit_results`, `enrich_inputs`, `allow_direct_query` * * @example * ```typescript * const exec = await handle.exportExecution({ exclude_system: true }); * for (const event of exec.events) { * console.log(event.event_type, event.attributes); * } * console.log(exec.summary); // { activities: { total: 5, ... }, timers: 1, ... } * ``` */ exportExecution(options?: ExecutionExportOptions): Promise; /** * Delivers a named signal to the workflow. If the workflow is paused * on `Durable.workflow.condition(signalId)`, it resumes with the * provided data. * * If the signal arrives before the workflow has registered its hook * (race condition under load), it is buffered as a pending signal * for up to `expire` (default 10 minutes). Use a longer duration * when signaling "early on purpose" (e.g., depositing a payload * hours before the workflow starts). * * @param signalId - Matches the `signalId` passed to `condition()`. * @param data - Payload delivered to the waiting workflow. * @param expire - Optional pending signal TTL (e.g., '1h', '30d'). Default '10m'. */ signal(signalId: string, data: Record, expire?: string): Promise; /** * Returns the current workflow state. For a completed workflow this * is the final output; for a running workflow it reflects the latest * persisted state (may change as activities complete). * * @param metadata - If `true`, returns the full job envelope including * internal metadata alongside the data. */ state(metadata?: boolean): Promise>; /** * Returns key-value pairs previously written via * `Durable.workflow.search()` or `Durable.workflow.enrich()`. * * @param fields - The field names to retrieve. */ queryState(fields: string[]): Promise>; /** * Returns the input arguments the workflow was started with, exactly as * passed to `client.workflow.start({ args })`. Reads a single field from * the job hash (no full export), so it stays cheap on large jobs and is * available for the life of the job — while running or after completion. * * @template T - The tuple type of the workflow's arguments. * * @example * ```typescript * const [orderId, region] = await handle.input<[string, string]>(); * ``` */ input(): Promise; /** * Returns the workflow's response if it has completed, or `undefined` * while it is still running. Unlike {@link result}, this never blocks — * it reads the current state and returns immediately. * * @template T - The workflow's return type. * * @example * ```typescript * const value = await handle.output<{ ok: boolean }>(); * if (value === undefined) { * // still running * } * ``` */ output(): Promise; /** * Returns the workflow's numeric status code: `0` = completed, * positive = still running, negative = interrupted/errored. */ status(): Promise; /** * Immediately terminates the workflow. The job is marked as interrupted, * subscribers are notified, and the job hash is expired. Unlike * {@link cancel}, this does **not** give the workflow a chance to * run cleanup code. * * Any pending escalations for this workflow are cancelled in the same * Postgres transaction that decrements the job semaphore — one atomic * write, no TOCTOU. A `system.escalation.*.cancelled` event is emitted * locally for each cancelled row via the configured `events.publish` * sink — instance-local only, never broadcast. */ terminate(options?: JobInterruptOptions): Promise; /** * Requests cooperative cancellation of the workflow. Unlike * `terminate()` (which terminates immediately), `cancel()` sets * a durable flag that the workflow detects at its next durable * operation (`sleep`, `proxyActivities`, `executeChild`, etc.). * The workflow receives a `CancelledFailure` error that it can * catch to perform cleanup before exiting. * * ```typescript * const handle = await client.workflow.start({ ... }); * await handle.cancel(); * // Workflow will throw CancelledFailure at its next durable operation * ``` */ cancel(): Promise; /** * Blocks until the workflow completes and returns the result. If the * workflow failed, the error is rethrown (with stack trace) unless * `throwOnError: false` is set, in which case the error object is * returned directly. * * @template T - The workflow's return type. */ result(config?: { state?: boolean; throwOnError?: boolean; }): Promise; }