@hotmeshio/hotmesh 0.12.1 → 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.
Files changed (202) hide show
  1. package/README.md +18 -22
  2. package/build/modules/enums.d.ts +60 -5
  3. package/build/modules/enums.js +62 -7
  4. package/build/modules/errors.d.ts +15 -2
  5. package/build/modules/errors.js +17 -1
  6. package/build/modules/storage.d.ts +1 -0
  7. package/build/modules/storage.js +2 -1
  8. package/build/package.json +8 -2
  9. package/build/services/activities/activity/context.d.ts +22 -0
  10. package/build/services/activities/activity/context.js +76 -0
  11. package/build/services/activities/activity/index.d.ts +116 -0
  12. package/build/services/activities/activity/index.js +299 -0
  13. package/build/services/activities/activity/mapping.d.ts +12 -0
  14. package/build/services/activities/activity/mapping.js +63 -0
  15. package/build/services/activities/activity/process.d.ts +28 -0
  16. package/build/services/activities/activity/process.js +100 -0
  17. package/build/services/activities/activity/protocol.d.ts +39 -0
  18. package/build/services/activities/activity/protocol.js +151 -0
  19. package/build/services/activities/activity/state.d.ts +40 -0
  20. package/build/services/activities/activity/state.js +143 -0
  21. package/build/services/activities/activity/transition.d.ts +23 -0
  22. package/build/services/activities/activity/transition.js +71 -0
  23. package/build/services/activities/activity/verify.d.ts +22 -0
  24. package/build/services/activities/activity/verify.js +85 -0
  25. package/build/services/activities/await.d.ts +1 -4
  26. package/build/services/activities/await.js +2 -36
  27. package/build/services/activities/cycle.d.ts +1 -11
  28. package/build/services/activities/cycle.js +3 -46
  29. package/build/services/activities/hook.d.ts +2 -11
  30. package/build/services/activities/hook.js +30 -50
  31. package/build/services/activities/interrupt.d.ts +2 -4
  32. package/build/services/activities/interrupt.js +4 -38
  33. package/build/services/activities/signal.d.ts +1 -11
  34. package/build/services/activities/signal.js +3 -48
  35. package/build/services/activities/trigger.d.ts +1 -3
  36. package/build/services/activities/trigger.js +0 -3
  37. package/build/services/activities/worker.d.ts +3 -6
  38. package/build/services/activities/worker.js +4 -40
  39. package/build/services/connector/factory.d.ts +6 -0
  40. package/build/services/connector/factory.js +24 -0
  41. package/build/services/dba/index.d.ts +14 -4
  42. package/build/services/dba/index.js +57 -18
  43. package/build/services/durable/activity.d.ts +30 -0
  44. package/build/services/durable/activity.js +46 -0
  45. package/build/services/durable/client.d.ts +26 -31
  46. package/build/services/durable/client.js +26 -31
  47. package/build/services/durable/connection.d.ts +13 -7
  48. package/build/services/durable/connection.js +13 -7
  49. package/build/services/durable/exporter.d.ts +2 -2
  50. package/build/services/durable/exporter.js +27 -12
  51. package/build/services/durable/handle.d.ts +59 -41
  52. package/build/services/durable/handle.js +61 -41
  53. package/build/services/durable/index.d.ts +152 -283
  54. package/build/services/durable/index.js +161 -289
  55. package/build/services/durable/interceptor.d.ts +43 -33
  56. package/build/services/durable/interceptor.js +59 -39
  57. package/build/services/durable/schemas/factory.d.ts +2 -3
  58. package/build/services/durable/schemas/factory.js +180 -30
  59. package/build/services/durable/telemetry.d.ts +80 -0
  60. package/build/services/durable/telemetry.js +137 -0
  61. package/build/services/durable/worker.d.ts +100 -21
  62. package/build/services/durable/worker.js +314 -60
  63. package/build/services/durable/workflow/all.d.ts +1 -1
  64. package/build/services/durable/workflow/all.js +1 -1
  65. package/build/services/durable/workflow/cancellationScope.d.ts +104 -0
  66. package/build/services/durable/workflow/cancellationScope.js +139 -0
  67. package/build/services/durable/workflow/common.d.ts +5 -4
  68. package/build/services/durable/workflow/common.js +6 -1
  69. package/build/services/durable/workflow/{waitFor.d.ts → condition.d.ts} +9 -8
  70. package/build/services/durable/workflow/{waitFor.js → condition.js} +44 -11
  71. package/build/services/durable/workflow/continueAsNew.d.ts +65 -0
  72. package/build/services/durable/workflow/continueAsNew.js +92 -0
  73. package/build/services/durable/workflow/didRun.d.ts +2 -2
  74. package/build/services/durable/workflow/didRun.js +4 -4
  75. package/build/services/durable/workflow/enrich.d.ts +5 -0
  76. package/build/services/durable/workflow/enrich.js +5 -0
  77. package/build/services/durable/workflow/entityMethods.d.ts +7 -0
  78. package/build/services/durable/workflow/entityMethods.js +7 -0
  79. package/build/services/durable/workflow/execHook.js +3 -3
  80. package/build/services/durable/workflow/execHookBatch.js +2 -2
  81. package/build/services/durable/workflow/{execChild.d.ts → executeChild.d.ts} +4 -40
  82. package/build/services/durable/workflow/{execChild.js → executeChild.js} +36 -45
  83. package/build/services/durable/workflow/hook.d.ts +1 -1
  84. package/build/services/durable/workflow/hook.js +4 -3
  85. package/build/services/durable/workflow/index.d.ts +45 -50
  86. package/build/services/durable/workflow/index.js +46 -51
  87. package/build/services/durable/workflow/interruption.d.ts +7 -6
  88. package/build/services/durable/workflow/interruption.js +11 -7
  89. package/build/services/durable/workflow/patched.d.ts +72 -0
  90. package/build/services/durable/workflow/patched.js +110 -0
  91. package/build/services/durable/workflow/proxyActivities.d.ts +7 -7
  92. package/build/services/durable/workflow/proxyActivities.js +51 -15
  93. package/build/services/durable/workflow/searchMethods.d.ts +7 -0
  94. package/build/services/durable/workflow/searchMethods.js +7 -0
  95. package/build/services/durable/workflow/signal.d.ts +4 -4
  96. package/build/services/durable/workflow/signal.js +4 -4
  97. package/build/services/durable/workflow/{sleepFor.d.ts → sleep.d.ts} +7 -7
  98. package/build/services/durable/workflow/{sleepFor.js → sleep.js} +39 -10
  99. package/build/services/durable/workflow/terminate.d.ts +55 -0
  100. package/build/services/durable/workflow/{interrupt.js → terminate.js} +21 -21
  101. package/build/services/durable/workflow/trace.js +2 -2
  102. package/build/services/durable/workflow/uuid4.d.ts +14 -0
  103. package/build/services/durable/workflow/uuid4.js +39 -0
  104. package/build/services/durable/workflow/{context.d.ts → workflowInfo.d.ts} +5 -5
  105. package/build/services/durable/workflow/{context.js → workflowInfo.js} +7 -7
  106. package/build/services/engine/compiler.d.ts +19 -0
  107. package/build/services/engine/compiler.js +20 -0
  108. package/build/services/engine/completion.d.ts +46 -0
  109. package/build/services/engine/completion.js +145 -0
  110. package/build/services/engine/dispatch.d.ts +24 -0
  111. package/build/services/engine/dispatch.js +98 -0
  112. package/build/services/engine/index.d.ts +49 -81
  113. package/build/services/engine/index.js +175 -573
  114. package/build/services/engine/init.d.ts +42 -0
  115. package/build/services/engine/init.js +74 -0
  116. package/build/services/engine/pubsub.d.ts +50 -0
  117. package/build/services/engine/pubsub.js +118 -0
  118. package/build/services/engine/reporting.d.ts +20 -0
  119. package/build/services/engine/reporting.js +38 -0
  120. package/build/services/engine/schema.d.ts +23 -0
  121. package/build/services/engine/schema.js +62 -0
  122. package/build/services/engine/signal.d.ts +57 -0
  123. package/build/services/engine/signal.js +117 -0
  124. package/build/services/engine/state.d.ts +35 -0
  125. package/build/services/engine/state.js +61 -0
  126. package/build/services/engine/version.d.ts +31 -0
  127. package/build/services/engine/version.js +73 -0
  128. package/build/services/hotmesh/deployment.d.ts +21 -0
  129. package/build/services/hotmesh/deployment.js +25 -0
  130. package/build/services/hotmesh/index.d.ts +142 -533
  131. package/build/services/hotmesh/index.js +223 -674
  132. package/build/services/hotmesh/init.d.ts +42 -0
  133. package/build/services/hotmesh/init.js +93 -0
  134. package/build/services/hotmesh/jobs.d.ts +67 -0
  135. package/build/services/hotmesh/jobs.js +99 -0
  136. package/build/services/hotmesh/pubsub.d.ts +38 -0
  137. package/build/services/hotmesh/pubsub.js +54 -0
  138. package/build/services/hotmesh/quorum.d.ts +30 -0
  139. package/build/services/hotmesh/quorum.js +62 -0
  140. package/build/services/hotmesh/validation.d.ts +6 -0
  141. package/build/services/hotmesh/validation.js +28 -0
  142. package/build/services/quorum/index.js +1 -0
  143. package/build/services/router/consumption/index.d.ts +11 -5
  144. package/build/services/router/consumption/index.js +24 -17
  145. package/build/services/router/error-handling/index.d.ts +2 -2
  146. package/build/services/router/error-handling/index.js +14 -14
  147. package/build/services/router/index.d.ts +1 -1
  148. package/build/services/router/index.js +2 -2
  149. package/build/services/serializer/index.d.ts +22 -0
  150. package/build/services/serializer/index.js +39 -1
  151. package/build/services/store/index.d.ts +1 -0
  152. package/build/services/store/providers/postgres/exporter-sql.d.ts +2 -2
  153. package/build/services/store/providers/postgres/exporter-sql.js +4 -4
  154. package/build/services/store/providers/postgres/kvtables.js +7 -6
  155. package/build/services/store/providers/postgres/kvtypes/hash/basic.js +67 -52
  156. package/build/services/store/providers/postgres/kvtypes/hash/jsonb.js +87 -72
  157. package/build/services/store/providers/postgres/kvtypes/hash/udata.js +106 -79
  158. package/build/services/store/providers/postgres/kvtypes/hash/utils.d.ts +16 -0
  159. package/build/services/store/providers/postgres/kvtypes/hash/utils.js +29 -16
  160. package/build/services/store/providers/postgres/postgres.d.ts +1 -0
  161. package/build/services/store/providers/postgres/postgres.js +14 -4
  162. package/build/services/stream/factory.d.ts +3 -1
  163. package/build/services/stream/factory.js +2 -2
  164. package/build/services/stream/index.d.ts +1 -0
  165. package/build/services/stream/providers/nats/nats.d.ts +1 -0
  166. package/build/services/stream/providers/nats/nats.js +1 -0
  167. package/build/services/stream/providers/postgres/credentials.d.ts +56 -0
  168. package/build/services/stream/providers/postgres/credentials.js +129 -0
  169. package/build/services/stream/providers/postgres/kvtables.js +18 -0
  170. package/build/services/stream/providers/postgres/messages.js +7 -7
  171. package/build/services/stream/providers/postgres/notifications.js +16 -2
  172. package/build/services/stream/providers/postgres/postgres.d.ts +7 -0
  173. package/build/services/stream/providers/postgres/postgres.js +35 -4
  174. package/build/services/stream/providers/postgres/procedures.d.ts +21 -0
  175. package/build/services/stream/providers/postgres/procedures.js +213 -0
  176. package/build/services/stream/providers/postgres/secured.d.ts +34 -0
  177. package/build/services/stream/providers/postgres/secured.js +146 -0
  178. package/build/services/stream/providers/postgres/stats.d.ts +1 -0
  179. package/build/services/stream/providers/postgres/stats.js +1 -0
  180. package/build/services/stream/registry.d.ts +1 -1
  181. package/build/services/stream/registry.js +5 -2
  182. package/build/services/telemetry/index.d.ts +10 -1
  183. package/build/services/telemetry/index.js +40 -7
  184. package/build/services/worker/credentials.d.ts +51 -0
  185. package/build/services/worker/credentials.js +87 -0
  186. package/build/services/worker/index.d.ts +2 -2
  187. package/build/services/worker/index.js +7 -6
  188. package/build/types/codec.d.ts +84 -0
  189. package/build/types/codec.js +2 -0
  190. package/build/types/dba.d.ts +39 -3
  191. package/build/types/durable.d.ts +123 -25
  192. package/build/types/error.d.ts +10 -0
  193. package/build/types/exporter.d.ts +1 -1
  194. package/build/types/hotmesh.d.ts +67 -4
  195. package/build/types/index.d.ts +2 -1
  196. package/build/types/provider.d.ts +2 -2
  197. package/build/types/quorum.d.ts +35 -1
  198. package/build/types/stream.d.ts +12 -6
  199. package/package.json +8 -2
  200. package/build/services/activities/activity.d.ts +0 -192
  201. package/build/services/activities/activity.js +0 -786
  202. package/build/services/durable/workflow/interrupt.d.ts +0 -55
@@ -54,7 +54,7 @@ declare class ExporterService {
54
54
  */
55
55
  export(jobId: string, options?: ExportOptions): Promise<DurableJobExport>;
56
56
  /**
57
- * Export a workflow execution as a Temporal-compatible event history.
57
+ * Export a workflow execution as a structured event history.
58
58
  *
59
59
  * **Sparse mode** (default): transforms the main workflow's timeline
60
60
  * into a flat event list. No additional I/O beyond the initial export.
@@ -80,7 +80,7 @@ declare class ExporterService {
80
80
  private resolveSymbolField;
81
81
  /**
82
82
  * Pure transformation: convert a raw DurableJobExport into a
83
- * Temporal-compatible WorkflowExecution event history.
83
+ * WorkflowExecution event history.
84
84
  */
85
85
  transformToExecution(raw: DurableJobExport, workflowId: string, workflowTopic: string, options: ExecutionExportOptions): WorkflowExecution;
86
86
  /**
@@ -173,7 +173,7 @@ class ExporterService {
173
173
  return jobExport;
174
174
  }
175
175
  /**
176
- * Export a workflow execution as a Temporal-compatible event history.
176
+ * Export a workflow execution as a structured event history.
177
177
  *
178
178
  * **Sparse mode** (default): transforms the main workflow's timeline
179
179
  * into a flat event list. No additional I/O beyond the initial export.
@@ -510,28 +510,43 @@ class ExporterService {
510
510
  }
511
511
  }
512
512
  }
513
- // ── 3. Stream-based fallback for unenriched activity events ──
514
- // When job attributes have been pruned, recover inputs from worker_streams
513
+ // ── 3. Stream-based enrichment from worker_streams ──
514
+ // Fetches worker invocation messages to recover:
515
+ // - Workflow input arguments (for workflow_execution_started)
516
+ // - Activity inputs when job attributes have been pruned
515
517
  if (this.store.getStreamHistory) {
516
- const unenrichedEvents = execution.events.filter((e) => (e.event_type === 'activity_task_scheduled' ||
518
+ const unenrichedActivityEvents = execution.events.filter((e) => (e.event_type === 'activity_task_scheduled' ||
517
519
  e.event_type === 'activity_task_completed' ||
518
520
  e.event_type === 'activity_task_failed') &&
519
521
  e.attributes.input === undefined);
520
- if (unenrichedEvents.length > 0) {
521
- const streamHistory = await this.store.getStreamHistory(workflowId, {
522
- types: ['worker'],
523
- });
522
+ const startedEvent = execution.events.find((e) => e.event_type === 'workflow_execution_started');
523
+ const startedNeedsInput = startedEvent &&
524
+ (!(startedEvent.attributes.input) ||
525
+ Object.keys(startedEvent.attributes.input).length === 0);
526
+ if (unenrichedActivityEvents.length > 0 || startedNeedsInput) {
527
+ const streamHistory = await this.store.getStreamHistory(workflowId);
524
528
  // Build a map of aid -> stream message data (the worker invocation inputs)
525
529
  const streamInputsByAid = new Map();
526
530
  for (const entry of streamHistory) {
527
- if (entry.msg_type === 'worker' && entry.data) {
531
+ if (entry.data) {
528
532
  const key = `${entry.aid}:${entry.dad || ''}`;
529
533
  if (!streamInputsByAid.has(key)) {
530
534
  streamInputsByAid.set(key, entry.data);
531
535
  }
532
536
  }
533
537
  }
534
- for (const evt of unenrichedEvents) {
538
+ // Enrich workflow_execution_started with input arguments from the
539
+ // first worker invocation message (aid="worker", data.arguments=[...])
540
+ if (startedNeedsInput) {
541
+ const workerEntry = streamHistory.find((entry) => entry.aid === 'worker' &&
542
+ entry.jid === workflowId &&
543
+ entry.data?.arguments);
544
+ if (workerEntry) {
545
+ startedEvent.attributes.input = workerEntry.data.arguments;
546
+ }
547
+ }
548
+ // Enrich unenriched activity events
549
+ for (const evt of unenrichedActivityEvents) {
535
550
  const attrs = evt.attributes;
536
551
  // Try matching by activity_type + dimensional address
537
552
  const key = `${attrs.activity_type}:${attrs.timeline_key || ''}`;
@@ -571,7 +586,7 @@ class ExporterService {
571
586
  }
572
587
  /**
573
588
  * Pure transformation: convert a raw DurableJobExport into a
574
- * Temporal-compatible WorkflowExecution event history.
589
+ * WorkflowExecution event history.
575
590
  */
576
591
  transformToExecution(raw, workflowId, workflowTopic, options) {
577
592
  const events = [];
@@ -761,7 +776,7 @@ class ExporterService {
761
776
  for (let i = 0; i < events.length; i++) {
762
777
  events[i].event_id = i + 1;
763
778
  }
764
- // ── Back-references (Temporal-compatible) ────────────────────
779
+ // ── Back-references ────────────────────────────────────────
765
780
  const scheduledMap = new Map();
766
781
  const initiatedMap = new Map();
767
782
  for (const e of events) {
@@ -4,28 +4,24 @@ import { JobInterruptOptions } from '../../types/job';
4
4
  import { StreamError } from '../../types/stream';
5
5
  import { ExporterService } from './exporter';
6
6
  /**
7
- * The WorkflowHandleService provides methods to interact with a running
8
- * workflow. This includes exporting the workflow, sending signals, and
9
- * querying the state of the workflow. It is instanced/accessed via the
10
- * Durable.Client class.
7
+ * Handle to a running or completed workflow execution. Returned by
8
+ * `client.workflow.start()` and `client.workflow.getHandle()`.
11
9
  *
12
10
  * @example
13
11
  * ```typescript
14
- * import { Client } from '@hotmeshio/hotmesh';
15
- * import { Client as Postgres } from 'pg';
16
- *
17
- * const client = new Client({ connection: {
18
- * class: Postgres,
19
- * options: { connectionString: 'postgres://user:pass@localhost:5432/db' }
20
- * }});
21
- *
22
12
  * const handle = await client.workflow.start({
23
- * args: ['HotMesh'],
24
- * taskQueue: 'hello-world',
13
+ * args: ['order-123'],
14
+ * taskQueue: 'orders',
15
+ * workflowName: 'orderWorkflow',
16
+ * workflowId: Durable.guid(),
25
17
  * });
26
18
  *
27
- * //perform actions like send a signal
28
- * await handle.signal('my-signal', { data: 'Hello' });
19
+ * // Await the final result
20
+ * const result = await handle.result();
21
+ *
22
+ * // Or interact while running
23
+ * await handle.signal('approval', { approved: true });
24
+ * await handle.cancel();
29
25
  * ```
30
26
  */
31
27
  export declare class WorkflowHandleService {
@@ -41,11 +37,12 @@ export declare class WorkflowHandleService {
41
37
  */
42
38
  constructor(hotMesh: HotMesh, workflowTopic: string, workflowId: string);
43
39
  /**
44
- * Exports the workflow state to a JSON object.
40
+ * Exports the full workflow state (job hash, metadata, activity
41
+ * results) as a JSON object.
45
42
  */
46
43
  export(options?: ExportOptions): Promise<DurableJobExport>;
47
44
  /**
48
- * Exports the workflow as a Temporal-like execution event history.
45
+ * Exports the workflow as an execution event history.
49
46
  *
50
47
  * **Sparse mode** (default): transforms the main workflow's timeline
51
48
  * into a flat event list with workflow lifecycle, activity, child workflow,
@@ -56,43 +53,64 @@ export declare class WorkflowHandleService {
56
53
  */
57
54
  exportExecution(options?: ExecutionExportOptions): Promise<WorkflowExecution>;
58
55
  /**
59
- * Sends a signal to the workflow. This is a way to send
60
- * a message to a workflow that is paused due to having
61
- * executed `Durable.workflow.waitFor`. The workflow
62
- * will awaken if no other signals are pending.
56
+ * Delivers a named signal to the workflow. If the workflow is paused
57
+ * on `Durable.workflow.condition(signalId)`, it resumes with the
58
+ * provided data.
59
+ *
60
+ * @param signalId - Matches the `signalId` passed to `condition()`.
61
+ * @param data - Payload delivered to the waiting workflow.
63
62
  */
64
63
  signal(signalId: string, data: Record<any, any>): Promise<void>;
65
64
  /**
66
- * Returns the job state of the workflow. If the workflow has completed
67
- * this is also the job output. If the workflow is still running, this
68
- * is the current state of the job, but it may change depending upon
69
- * the activities that remain.
65
+ * Returns the current workflow state. For a completed workflow this
66
+ * is the final output; for a running workflow it reflects the latest
67
+ * persisted state (may change as activities complete).
68
+ *
69
+ * @param metadata - If `true`, returns the full job envelope including
70
+ * internal metadata alongside the data.
70
71
  */
71
72
  state(metadata?: boolean): Promise<Record<string, any>>;
72
73
  /**
73
- * Returns the current search state of the workflow. This is
74
- * different than the job state or individual activity state.
75
- * Search state represents name/value pairs that were added
76
- * to the workflow.
74
+ * Returns key-value pairs previously written via
75
+ * `Durable.workflow.search()` or `Durable.workflow.enrich()`.
76
+ *
77
+ * @param fields - The field names to retrieve.
77
78
  */
78
79
  queryState(fields: string[]): Promise<Record<string, any>>;
79
80
  /**
80
- * Returns the current status of the workflow. This is a semaphore
81
- * value that represents the current state of the workflow, where
82
- * 0 is complete and a negative value represents that the flow was
83
- * interrupted.
81
+ * Returns the workflow's numeric status code: `0` = completed,
82
+ * positive = still running, negative = interrupted/errored.
84
83
  */
85
84
  status(): Promise<number>;
86
85
  /**
87
- * Interrupts a running workflow. Standard Job Completion tasks will
88
- * run. Subscribers will be notified and the job hash will be expired.
86
+ * Immediately terminates the workflow. The job is marked as interrupted,
87
+ * subscribers are notified, and the job hash is expired. Unlike
88
+ * {@link cancel}, this does **not** give the workflow a chance to
89
+ * run cleanup code.
89
90
  */
90
- interrupt(options?: JobInterruptOptions): Promise<string>;
91
+ terminate(options?: JobInterruptOptions): Promise<string>;
91
92
  /**
92
- * Waits for the workflow to complete and returns the result. If
93
- * the workflow response includes an error, this method will rethrow
94
- * the error, including the stack trace if available.
95
- * Wrap calls in a try/catch as necessary to avoid unhandled exceptions.
93
+ * Requests cooperative cancellation of the workflow. Unlike
94
+ * `terminate()` (which terminates immediately), `cancel()` sets
95
+ * a durable flag that the workflow detects at its next durable
96
+ * operation (`sleep`, `proxyActivities`, `executeChild`, etc.).
97
+ * The workflow receives a `CancelledFailure` error that it can
98
+ * catch to perform cleanup before exiting.
99
+ *
100
+ * ```typescript
101
+ * const handle = await client.workflow.start({ ... });
102
+ * await handle.cancel();
103
+ * // Workflow will throw CancelledFailure at its next durable operation
104
+ * ```
105
+ */
106
+ cancel(): Promise<void>;
107
+ /**
108
+ * Blocks until the workflow completes and returns the result. If the
109
+ * workflow failed, the error is rethrown (with stack trace) unless
110
+ * `throwOnError: false` is set, in which case the error object is
111
+ * returned directly.
112
+ *
113
+ * @template T - The workflow's return type.
96
114
  */
97
115
  result<T>(config?: {
98
116
  state?: boolean;
@@ -3,28 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WorkflowHandleService = void 0;
4
4
  const exporter_1 = require("./exporter");
5
5
  /**
6
- * The WorkflowHandleService provides methods to interact with a running
7
- * workflow. This includes exporting the workflow, sending signals, and
8
- * querying the state of the workflow. It is instanced/accessed via the
9
- * Durable.Client class.
6
+ * Handle to a running or completed workflow execution. Returned by
7
+ * `client.workflow.start()` and `client.workflow.getHandle()`.
10
8
  *
11
9
  * @example
12
10
  * ```typescript
13
- * import { Client } from '@hotmeshio/hotmesh';
14
- * import { Client as Postgres } from 'pg';
15
- *
16
- * const client = new Client({ connection: {
17
- * class: Postgres,
18
- * options: { connectionString: 'postgres://user:pass@localhost:5432/db' }
19
- * }});
20
- *
21
11
  * const handle = await client.workflow.start({
22
- * args: ['HotMesh'],
23
- * taskQueue: 'hello-world',
12
+ * args: ['order-123'],
13
+ * taskQueue: 'orders',
14
+ * workflowName: 'orderWorkflow',
15
+ * workflowId: Durable.guid(),
24
16
  * });
25
17
  *
26
- * //perform actions like send a signal
27
- * await handle.signal('my-signal', { data: 'Hello' });
18
+ * // Await the final result
19
+ * const result = await handle.result();
20
+ *
21
+ * // Or interact while running
22
+ * await handle.signal('approval', { approved: true });
23
+ * await handle.cancel();
28
24
  * ```
29
25
  */
30
26
  class WorkflowHandleService {
@@ -38,13 +34,14 @@ class WorkflowHandleService {
38
34
  this.exporter = new exporter_1.ExporterService(this.hotMesh.appId, this.hotMesh.engine.store, this.hotMesh.engine.logger);
39
35
  }
40
36
  /**
41
- * Exports the workflow state to a JSON object.
37
+ * Exports the full workflow state (job hash, metadata, activity
38
+ * results) as a JSON object.
42
39
  */
43
40
  async export(options) {
44
41
  return this.exporter.export(this.workflowId, options);
45
42
  }
46
43
  /**
47
- * Exports the workflow as a Temporal-like execution event history.
44
+ * Exports the workflow as an execution event history.
48
45
  *
49
46
  * **Sparse mode** (default): transforms the main workflow's timeline
50
47
  * into a flat event list with workflow lifecycle, activity, child workflow,
@@ -57,10 +54,12 @@ class WorkflowHandleService {
57
54
  return this.exporter.exportExecution(this.workflowId, this.workflowTopic, options);
58
55
  }
59
56
  /**
60
- * Sends a signal to the workflow. This is a way to send
61
- * a message to a workflow that is paused due to having
62
- * executed `Durable.workflow.waitFor`. The workflow
63
- * will awaken if no other signals are pending.
57
+ * Delivers a named signal to the workflow. If the workflow is paused
58
+ * on `Durable.workflow.condition(signalId)`, it resumes with the
59
+ * provided data.
60
+ *
61
+ * @param signalId - Matches the `signalId` passed to `condition()`.
62
+ * @param data - Payload delivered to the waiting workflow.
64
63
  */
65
64
  async signal(signalId, data) {
66
65
  await this.hotMesh.signal(`${this.hotMesh.appId}.wfs.signal`, {
@@ -69,10 +68,12 @@ class WorkflowHandleService {
69
68
  });
70
69
  }
71
70
  /**
72
- * Returns the job state of the workflow. If the workflow has completed
73
- * this is also the job output. If the workflow is still running, this
74
- * is the current state of the job, but it may change depending upon
75
- * the activities that remain.
71
+ * Returns the current workflow state. For a completed workflow this
72
+ * is the final output; for a running workflow it reflects the latest
73
+ * persisted state (may change as activities complete).
74
+ *
75
+ * @param metadata - If `true`, returns the full job envelope including
76
+ * internal metadata alongside the data.
76
77
  */
77
78
  async state(metadata = false) {
78
79
  const state = await this.hotMesh.getState(`${this.hotMesh.appId}.execute`, this.workflowId);
@@ -82,35 +83,54 @@ class WorkflowHandleService {
82
83
  return metadata ? state : state.data;
83
84
  }
84
85
  /**
85
- * Returns the current search state of the workflow. This is
86
- * different than the job state or individual activity state.
87
- * Search state represents name/value pairs that were added
88
- * to the workflow.
86
+ * Returns key-value pairs previously written via
87
+ * `Durable.workflow.search()` or `Durable.workflow.enrich()`.
88
+ *
89
+ * @param fields - The field names to retrieve.
89
90
  */
90
91
  async queryState(fields) {
91
92
  return await this.hotMesh.getQueryState(this.workflowId, fields);
92
93
  }
93
94
  /**
94
- * Returns the current status of the workflow. This is a semaphore
95
- * value that represents the current state of the workflow, where
96
- * 0 is complete and a negative value represents that the flow was
97
- * interrupted.
95
+ * Returns the workflow's numeric status code: `0` = completed,
96
+ * positive = still running, negative = interrupted/errored.
98
97
  */
99
98
  async status() {
100
99
  return await this.hotMesh.getStatus(this.workflowId);
101
100
  }
102
101
  /**
103
- * Interrupts a running workflow. Standard Job Completion tasks will
104
- * run. Subscribers will be notified and the job hash will be expired.
102
+ * Immediately terminates the workflow. The job is marked as interrupted,
103
+ * subscribers are notified, and the job hash is expired. Unlike
104
+ * {@link cancel}, this does **not** give the workflow a chance to
105
+ * run cleanup code.
105
106
  */
106
- async interrupt(options) {
107
+ async terminate(options) {
107
108
  return await this.hotMesh.interrupt(`${this.hotMesh.appId}.execute`, this.workflowId, options);
108
109
  }
109
110
  /**
110
- * Waits for the workflow to complete and returns the result. If
111
- * the workflow response includes an error, this method will rethrow
112
- * the error, including the stack trace if available.
113
- * Wrap calls in a try/catch as necessary to avoid unhandled exceptions.
111
+ * Requests cooperative cancellation of the workflow. Unlike
112
+ * `terminate()` (which terminates immediately), `cancel()` sets
113
+ * a durable flag that the workflow detects at its next durable
114
+ * operation (`sleep`, `proxyActivities`, `executeChild`, etc.).
115
+ * The workflow receives a `CancelledFailure` error that it can
116
+ * catch to perform cleanup before exiting.
117
+ *
118
+ * ```typescript
119
+ * const handle = await client.workflow.start({ ... });
120
+ * await handle.cancel();
121
+ * // Workflow will throw CancelledFailure at its next durable operation
122
+ * ```
123
+ */
124
+ async cancel() {
125
+ await this.hotMesh.cancel(this.workflowId);
126
+ }
127
+ /**
128
+ * Blocks until the workflow completes and returns the result. If the
129
+ * workflow failed, the error is rethrown (with stack trace) unless
130
+ * `throwOnError: false` is set, in which case the error object is
131
+ * returned directly.
132
+ *
133
+ * @template T - The workflow's return type.
114
134
  */
115
135
  async result(config) {
116
136
  const topic = `${this.hotMesh.appId}.executed.${this.workflowId}`;