import { StringAnyType } from './serializer'; export type ExportItem = [string | null, string, any]; /** * Selectable sections of a {@link DurableJobExport}. Use with `allow` (allowlist) * or `block` (blocklist) in {@link ExportOptions} to control export size. * * - `data` — workflow input arguments * - `state` — current state (done, response, error, timestamps) * - `status` — semaphore value * - `timeline` — idempotent operation markers (can be large for complex workflows) * - `transitions` — activity execution timestamps (can be large with many cycles) */ export type ExportFields = 'data' | 'state' | 'status' | 'timeline' | 'transitions'; export interface ExportOptions { /** * limit the export byte size by specifying an allowlist */ allow?: Array; /** * limit the export byte size by specifying a block list */ block?: Array; /** * If false, do not return timeline values (like child job response, proxy activity response, etc) * @default true */ values?: boolean; /** * When true, fetches stream message history and produces a structured * `activities` array with input/output per activity, timing, dimensional * cycle info, and retry attempts. This is the dashboard-friendly format. * @default false */ enrich_inputs?: boolean; } export type JobAction = { cursor: number; items: ExportItem[]; }; export interface JobActionExport { hooks: { [key: string]: JobAction; }; main: JobAction; } export interface ActivityAction { action: string; target: string; } export interface JobTimeline { activity: string; dimension: string; duplex: 'entry' | 'exit'; timestamp: string; created?: string; updated?: string; actions?: ActivityAction[]; } export interface DependencyExport { type: string; topic: string; gid: string; jid: string; } export interface ExportTransitions { [key: string]: string[]; } export interface ExportCycles { [key: string]: string[]; } export type TimelineType = { key: string; value: Record | string | number | null; index: number; secondary?: number; dimension?: string; }; export interface TransitionType { activity: string; dimensions: string; created: string; updated: string; } /** * Raw export of a durable workflow job, returned by `handle.export()`. * * Contains five sections (filterable via {@link ExportOptions}): * * | Section | Type | Description | * |---|---|---| * | `data` | object | Workflow input arguments passed to `workflow.start()` | * | `state` | object | Current state: `done` flag, `response`, `$error`, `jc`/`ju` timestamps | * | `status` | number | Semaphore: `0` = idle/complete, `> 0` = pending activities, `< 0` = error | * | `timeline` | array | Ordered idempotent markers for each operation (proxy, child, sleep, signal) | * | `transitions` | array | Activity start/stop timestamps organized by dimension | */ export interface DurableJobExport { data?: StringAnyType; state?: StringAnyType; status?: number; timeline?: TimelineType[]; transitions?: TransitionType[]; } export interface ActivityDetail { name: string; type: string; dimension: string; input?: Record; output?: Record; started_at?: string; completed_at?: string; duration_ms?: number; retry_attempt?: number; cycle_iteration?: number; error?: string | null; } export interface JobExport { dependencies: DependencyExport[]; process: StringAnyType; status: string; activities?: ActivityDetail[]; } export type ExportMode = 'sparse' | 'verbose'; export type WorkflowEventType = 'workflow_execution_started' | 'workflow_execution_completed' | 'workflow_execution_failed' | 'activity_task_scheduled' | 'activity_task_completed' | 'activity_task_failed' | 'child_workflow_execution_started' | 'child_workflow_execution_completed' | 'child_workflow_execution_failed' | 'timer_started' | 'timer_fired' | 'signal_wait_started' | 'workflow_execution_signaled'; export type WorkflowEventCategory = 'workflow' | 'activity' | 'child_workflow' | 'timer' | 'signal'; export interface WorkflowExecutionStartedAttributes { kind: 'workflow_execution_started'; workflow_type: string; task_queue: string; input?: any; } export interface WorkflowExecutionCompletedAttributes { kind: 'workflow_execution_completed'; result?: any; } export interface WorkflowExecutionFailedAttributes { kind: 'workflow_execution_failed'; failure?: string; } export interface ActivityTaskScheduledAttributes { kind: 'activity_task_scheduled'; activity_type: string; timeline_key: string; execution_index: number; input?: any; } export interface ActivityTaskCompletedAttributes { kind: 'activity_task_completed'; activity_type: string; result?: any; scheduled_event_id?: number; timeline_key: string; execution_index: number; input?: any; } export interface ActivityTaskFailedAttributes { kind: 'activity_task_failed'; activity_type: string; failure?: any; scheduled_event_id?: number; timeline_key: string; execution_index: number; input?: any; } export interface ChildWorkflowExecutionStartedAttributes { kind: 'child_workflow_execution_started'; child_workflow_id: string; awaited: boolean; timeline_key: string; execution_index: number; input?: any; } export interface ChildWorkflowExecutionCompletedAttributes { kind: 'child_workflow_execution_completed'; child_workflow_id: string; result?: any; initiated_event_id?: number; timeline_key: string; execution_index: number; } export interface ChildWorkflowExecutionFailedAttributes { kind: 'child_workflow_execution_failed'; child_workflow_id: string; failure?: any; initiated_event_id?: number; timeline_key: string; execution_index: number; } export interface TimerStartedAttributes { kind: 'timer_started'; duration_ms?: number; timeline_key: string; execution_index: number; } export interface TimerFiredAttributes { kind: 'timer_fired'; timeline_key: string; execution_index: number; } export interface SignalWaitStartedAttributes { kind: 'signal_wait_started'; signal_name: string; timeline_key: string; execution_index: number; } export interface WorkflowExecutionSignaledAttributes { kind: 'workflow_execution_signaled'; signal_name: string; input?: any; timeline_key: string; execution_index: number; } export type WorkflowEventAttributes = WorkflowExecutionStartedAttributes | WorkflowExecutionCompletedAttributes | WorkflowExecutionFailedAttributes | ActivityTaskScheduledAttributes | ActivityTaskCompletedAttributes | ActivityTaskFailedAttributes | ChildWorkflowExecutionStartedAttributes | ChildWorkflowExecutionCompletedAttributes | ChildWorkflowExecutionFailedAttributes | TimerStartedAttributes | TimerFiredAttributes | SignalWaitStartedAttributes | WorkflowExecutionSignaledAttributes; export interface WorkflowExecutionEvent { event_id: number; event_type: WorkflowEventType; category: WorkflowEventCategory; event_time: string; duration_ms: number | null; is_system: boolean; attributes: WorkflowEventAttributes; } export interface WorkflowExecutionSummary { total_events: number; activities: { total: number; completed: number; failed: number; system: number; user: number; }; child_workflows: { total: number; completed: number; failed: number; }; timers: number; signals: number; } export type WorkflowExecutionStatus = 'running' | 'completed' | 'failed'; /** * Structured execution history for a durable workflow, returned by * `handle.exportExecution()`. * * Events are chronologically ordered with sequential `event_id` values. * Completed/failed events carry `scheduled_event_id` or `initiated_event_id` * back-references to their corresponding scheduled/started events. * * The `summary` provides aggregate counts by category (activities, child * workflows, timers, signals) for quick dashboard rendering. * * In `verbose` mode, `children` contains recursively fetched child workflow * executions, each with their own events and summaries. */ export interface WorkflowExecution { workflow_id: string; workflow_type: string; task_queue: string; status: WorkflowExecutionStatus; start_time: string | null; close_time: string | null; duration_ms: number | null; result: any; events: WorkflowExecutionEvent[]; summary: WorkflowExecutionSummary; children?: WorkflowExecution[]; stream_history?: StreamHistoryEntry[]; /** * Upward lineage pointer to the real spawning workflow — never the synthetic * collator `$C` job. `null` marks a root. Only present when the export was * requested with `include_lineage: true`. */ parent_workflow_id?: string | null; /** * Root ancestor of this execution; `null` for a root itself (identify roots * via a null `parent_workflow_id`). Every descendant carries the same * `origin_id`, so a subtree is recoverable in one query. Only present when the * export was requested with `include_lineage: true`. */ origin_id?: string | null; } /** * Options for `handle.exportExecution()`. Controls event filtering, enrichment, * and traversal depth for child workflows. */ export interface ExecutionExportOptions { /** * `sparse` (default) — single-query export from the workflow's timeline markers. * `verbose` — recursively fetches child workflow jobs and attaches their full * event histories as nested `children` arrays. */ mode?: ExportMode; /** * When true, omits internal/interceptor activities (names starting with `lt`) * from the event list. Useful for user-facing dashboards. * @default false */ exclude_system?: boolean; /** * When true, strips `result` and `input` payloads from event attributes. * Reduces response size while preserving event structure and timing. * @default false */ omit_results?: boolean; /** * Maximum recursion depth for verbose mode child workflow fetching. * @default 5 */ max_depth?: number; /** * When true, enriches activity and child workflow events with their inputs * by querying the underlying job attributes. This enables full visibility * into activity arguments without requiring separate callback queries. * * @default false (late-binding: only return timeline_key references) */ enrich_inputs?: boolean; /** * When true, allows fallback to direct database queries for expired jobs * whose in-memory handles have been pruned. Only supported with providers * that implement the extended exporter query interface (e.g., Postgres). * * @default false */ allow_direct_query?: boolean; /** * When true, populates the export's `parent_workflow_id` and `origin_id` from * the jobs table via a single indexed lookup. `parent_workflow_id` is the real * spawning workflow (never the synthetic collator `$C` job); `origin_id` is the * root ancestor (a null `parent_workflow_id` marks a root). Opt-in so the extra * query is only paid when a UI needs upward lineage. Requires a provider that * implements `getJobLineage` (e.g., Postgres); a no-op otherwise. * * @default false */ include_lineage?: boolean; /** * When true, fetches the full stream message history for this workflow * from the worker_streams table and attaches it as `stream_history`. * This provides raw activity input/output data from the original stream * messages, enabling full export fidelity. * * @default false */ include_stream_history?: boolean; } export interface StreamHistoryEntry { id: number; jid: string; aid: string; dad: string; msg_type: string; topic: string; workflow_name: string; data: Record; status?: string; code?: number; created_at: string; reserved_at?: string; expired_at?: string; } export interface JobAttributesRow { field: string; value: string; } export interface JobRow { id: string; key: string; status: number; created_at: Date; updated_at: Date; expired_at?: Date; is_live: boolean; } export interface ActivityInputMap { /** Maps activity job_id to parsed input arguments */ byJobId: Map; /** Maps "activityName:executionIndex" to parsed input arguments */ byNameIndex: Map; }