ai-workflows 2.1.3 → 2.3.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 (188) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +8 -1
  3. package/README.md +2 -0
  4. package/dist/barrier.d.ts +6 -0
  5. package/dist/barrier.d.ts.map +1 -1
  6. package/dist/barrier.js +45 -7
  7. package/dist/barrier.js.map +1 -1
  8. package/dist/cascade-context.d.ts.map +1 -1
  9. package/dist/cascade-context.js +25 -25
  10. package/dist/cascade-context.js.map +1 -1
  11. package/dist/cascade-executor.d.ts.map +1 -1
  12. package/dist/cascade-executor.js +1 -1
  13. package/dist/cascade-executor.js.map +1 -1
  14. package/dist/context.d.ts.map +1 -1
  15. package/dist/context.js +23 -7
  16. package/dist/context.js.map +1 -1
  17. package/dist/cron-parser.d.ts +65 -0
  18. package/dist/cron-parser.d.ts.map +1 -0
  19. package/dist/cron-parser.js +294 -0
  20. package/dist/cron-parser.js.map +1 -0
  21. package/dist/cron-scheduler.d.ts +117 -0
  22. package/dist/cron-scheduler.d.ts.map +1 -0
  23. package/dist/cron-scheduler.js +176 -0
  24. package/dist/cron-scheduler.js.map +1 -0
  25. package/dist/database-context.d.ts +184 -0
  26. package/dist/database-context.d.ts.map +1 -0
  27. package/dist/database-context.js +428 -0
  28. package/dist/database-context.js.map +1 -0
  29. package/dist/digital-objects-adapter.d.ts +159 -0
  30. package/dist/digital-objects-adapter.d.ts.map +1 -0
  31. package/dist/digital-objects-adapter.js +229 -0
  32. package/dist/digital-objects-adapter.js.map +1 -0
  33. package/dist/durable-execution-cloudflare.d.ts +427 -0
  34. package/dist/durable-execution-cloudflare.d.ts.map +1 -0
  35. package/dist/durable-execution-cloudflare.js +510 -0
  36. package/dist/durable-execution-cloudflare.js.map +1 -0
  37. package/dist/durable-execution.d.ts +482 -0
  38. package/dist/durable-execution.d.ts.map +1 -0
  39. package/dist/durable-execution.js +594 -0
  40. package/dist/durable-execution.js.map +1 -0
  41. package/dist/durable-workflow.d.ts +176 -0
  42. package/dist/durable-workflow.d.ts.map +1 -0
  43. package/dist/durable-workflow.js +552 -0
  44. package/dist/durable-workflow.js.map +1 -0
  45. package/dist/graph/topological-sort.d.ts.map +1 -1
  46. package/dist/graph/topological-sort.js +5 -5
  47. package/dist/graph/topological-sort.js.map +1 -1
  48. package/dist/index.d.ts +4 -0
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +15 -0
  51. package/dist/index.js.map +1 -1
  52. package/dist/logger.d.ts +101 -0
  53. package/dist/logger.d.ts.map +1 -0
  54. package/dist/logger.js +115 -0
  55. package/dist/logger.js.map +1 -0
  56. package/dist/on.d.ts.map +1 -1
  57. package/dist/on.js +3 -3
  58. package/dist/on.js.map +1 -1
  59. package/dist/runtime.d.ts +169 -0
  60. package/dist/runtime.d.ts.map +1 -0
  61. package/dist/runtime.js +275 -0
  62. package/dist/runtime.js.map +1 -0
  63. package/dist/send.d.ts.map +1 -1
  64. package/dist/send.js +4 -3
  65. package/dist/send.js.map +1 -1
  66. package/dist/telemetry.d.ts +150 -0
  67. package/dist/telemetry.d.ts.map +1 -0
  68. package/dist/telemetry.js +388 -0
  69. package/dist/telemetry.js.map +1 -0
  70. package/dist/timer-registry.d.ts +25 -0
  71. package/dist/timer-registry.d.ts.map +1 -1
  72. package/dist/timer-registry.js +42 -8
  73. package/dist/timer-registry.js.map +1 -1
  74. package/dist/types.d.ts +17 -6
  75. package/dist/types.d.ts.map +1 -1
  76. package/dist/types.js +1 -1
  77. package/dist/types.js.map +1 -1
  78. package/dist/worker/durable-step.d.ts +481 -0
  79. package/dist/worker/durable-step.d.ts.map +1 -0
  80. package/dist/worker/durable-step.js +606 -0
  81. package/dist/worker/durable-step.js.map +1 -0
  82. package/dist/worker/index.d.ts +106 -0
  83. package/dist/worker/index.d.ts.map +1 -0
  84. package/dist/worker/index.js +124 -0
  85. package/dist/worker/index.js.map +1 -0
  86. package/dist/worker/state-adapter.d.ts +230 -0
  87. package/dist/worker/state-adapter.d.ts.map +1 -0
  88. package/dist/worker/state-adapter.js +409 -0
  89. package/dist/worker/state-adapter.js.map +1 -0
  90. package/dist/worker/topological-executor.d.ts +282 -0
  91. package/dist/worker/topological-executor.d.ts.map +1 -0
  92. package/dist/worker/topological-executor.js +396 -0
  93. package/dist/worker/topological-executor.js.map +1 -0
  94. package/dist/worker/workflow-builder.d.ts +286 -0
  95. package/dist/worker/workflow-builder.d.ts.map +1 -0
  96. package/dist/worker/workflow-builder.js +565 -0
  97. package/dist/worker/workflow-builder.js.map +1 -0
  98. package/dist/worker.d.ts +800 -0
  99. package/dist/worker.d.ts.map +1 -0
  100. package/dist/worker.js +2428 -0
  101. package/dist/worker.js.map +1 -0
  102. package/dist/workflow-builder.d.ts +287 -0
  103. package/dist/workflow-builder.d.ts.map +1 -0
  104. package/dist/workflow-builder.js +762 -0
  105. package/dist/workflow-builder.js.map +1 -0
  106. package/dist/workflow.d.ts +14 -30
  107. package/dist/workflow.d.ts.map +1 -1
  108. package/dist/workflow.js +132 -292
  109. package/dist/workflow.js.map +1 -1
  110. package/examples/01-ecommerce-order-pipeline.ts +358 -0
  111. package/examples/02-content-moderation-cascade.ts +454 -0
  112. package/examples/03-scheduled-reporting-dependencies.ts +479 -0
  113. package/examples/04-database-persistence.ts +518 -0
  114. package/examples/README.md +173 -0
  115. package/package.json +30 -13
  116. package/src/__tests__/digital-objects-adapter.test.ts +274 -0
  117. package/src/__tests__/durable-workflow.test.ts +297 -0
  118. package/src/barrier.ts +48 -7
  119. package/src/cascade-context.ts +36 -29
  120. package/src/cascade-executor.ts +3 -2
  121. package/src/context.ts +41 -12
  122. package/src/cron-parser.ts +347 -0
  123. package/src/cron-scheduler.ts +239 -0
  124. package/src/database-context.ts +658 -0
  125. package/src/digital-objects-adapter.ts +351 -0
  126. package/src/durable-execution-cloudflare.ts +855 -0
  127. package/src/durable-execution.ts +1042 -0
  128. package/src/durable-workflow.ts +717 -0
  129. package/src/graph/topological-sort.ts +6 -8
  130. package/src/index.ts +69 -0
  131. package/src/logger.ts +148 -0
  132. package/src/on.ts +8 -9
  133. package/src/runtime.ts +436 -0
  134. package/src/send.ts +4 -5
  135. package/src/telemetry.ts +577 -0
  136. package/src/timer-registry.ts +44 -10
  137. package/src/types.ts +32 -17
  138. package/src/worker/durable-step.ts +976 -0
  139. package/src/worker/index.ts +216 -0
  140. package/src/worker/state-adapter.ts +589 -0
  141. package/src/worker/topological-executor.ts +625 -0
  142. package/src/worker/workflow-builder.ts +871 -0
  143. package/src/worker.ts +2906 -0
  144. package/src/workflow-builder.ts +1068 -0
  145. package/src/workflow.ts +188 -351
  146. package/test/barrier-join.test.ts +32 -24
  147. package/test/cascade-executor.test.ts +9 -16
  148. package/test/cron-parser.test.ts +314 -0
  149. package/test/cron-scheduler.test.ts +291 -0
  150. package/test/database-context.test.ts +770 -0
  151. package/test/db-provider-adapter.test.ts +862 -0
  152. package/test/durable-execution-cloudflare.test.ts +606 -0
  153. package/test/durable-execution-in-process.test.ts +286 -0
  154. package/test/durable-execution.test.ts +247 -0
  155. package/test/e2e/workflow-scenarios.e2e.test.ts +1039 -0
  156. package/test/integration.test.ts +442 -0
  157. package/test/rpc-surface.test.ts +946 -0
  158. package/test/runtime.test.ts +262 -0
  159. package/test/schedule-timer-cleanup.test.ts +30 -21
  160. package/test/send-race-conditions.test.ts +30 -40
  161. package/test/worker/durable-cascade.test.ts +1117 -0
  162. package/test/worker/durable-step.test.ts +723 -0
  163. package/test/worker/topological-executor.test.ts +1240 -0
  164. package/test/worker/workflow-builder.test.ts +1067 -0
  165. package/test/worker.test.ts +608 -0
  166. package/test/workflow-builder.test.ts +1670 -0
  167. package/test/workflow-cron.test.ts +256 -0
  168. package/test/workflow-state-adapter.test.ts +923 -0
  169. package/test/workflow.test.ts +25 -22
  170. package/tsconfig.json +3 -1
  171. package/vitest.config.ts +38 -1
  172. package/vitest.workers.config.ts +44 -0
  173. package/wrangler.jsonc +22 -0
  174. package/.turbo/turbo-test.log +0 -169
  175. package/LICENSE +0 -21
  176. package/src/context.js +0 -83
  177. package/src/every.js +0 -267
  178. package/src/index.js +0 -71
  179. package/src/on.js +0 -79
  180. package/src/send.js +0 -111
  181. package/src/types.js +0 -4
  182. package/src/workflow.js +0 -455
  183. package/test/context.test.js +0 -116
  184. package/test/every.test.js +0 -282
  185. package/test/on.test.js +0 -80
  186. package/test/send.test.js +0 -89
  187. package/test/workflow.test.js +0 -224
  188. package/vitest.config.js +0 -7
package/dist/index.d.ts CHANGED
@@ -60,6 +60,7 @@
60
60
  * ```
61
61
  */
62
62
  export { Workflow, createTestContext, parseEvent, type WorkflowInstance } from './workflow.js';
63
+ export { createWorkflowRuntime, type WorkflowRuntime, type WorkflowRuntimeOptions, } from './runtime.js';
63
64
  export { on, registerEventHandler, getEventHandlers, clearEventHandlers } from './on.js';
64
65
  export { every, registerScheduleHandler, getScheduleHandlers, clearScheduleHandlers, setCronConverter, toCron, intervalToMs, formatInterval, } from './every.js';
65
66
  export { send, getEventBus } from './send.js';
@@ -69,5 +70,8 @@ export { DependencyGraph, CircularDependencyError, MissingDependencyError, type
69
70
  export { topologicalSort, topologicalSortKahn, topologicalSortDFS, getExecutionLevels, CycleDetectedError, MissingNodeError, type SortableNode, type ExecutionLevel, type TopologicalSortOptions, type TopologicalSortResult, } from './graph/topological-sort.js';
70
71
  export { Barrier, BarrierTimeoutError, createBarrier, waitForAll, waitForAny, withConcurrencyLimit, type BarrierOptions, type BarrierProgress, type BarrierResult, type WaitForAllOptions, type WaitForAnyOptions, type WaitForAnyResult, type ConcurrencyOptions, } from './barrier.js';
71
72
  export { CascadeExecutor, CascadeTimeoutError, TierSkippedError, AllTiersFailedError, TIER_ORDER, DEFAULT_TIER_TIMEOUTS, type CapabilityTier, type TierHandler, type TierContext, type TierResult, type TierRetryConfig, type CascadeConfig, type CascadeResult, type CascadeMetrics, type SkipCondition, } from './cascade-executor.js';
73
+ export { DurableStep, StepContext as DurableStepContext, type StepConfig, type StepMetadata, type RetryConfig as DurableRetryConfig, type WorkflowStep, type StepFunction as DurableStepFunction, DurableCascadeStep, AllTiersFailed, CascadeTimeout, DEFAULT_CASCADE_TIMEOUTS, CASCADE_TIER_ORDER, type CascadeTier, type CascadeConfig as DurableCascadeConfig, type CascadeTierConfig, type CascadeTierResult, type CascadeResult as DurableCascadeResult, type CascadeContext as DurableCascadeContext, type CascadeTierContext, type AiBinding, type HumanReviewRequest, type CodeTierHandler, type AiTierHandler, type HumanTierHandler, } from './worker/durable-step.js';
74
+ export { workflow, WorkflowBuilder, type StepDefinition, type StepChain, type ConditionalChain, type LoopChain, type BuiltWorkflow, type StepContext, type RetryConfig, type StepFunction, type ConditionFunction, type ErrorHandler, type LoopOptions, type ForEachOptions, } from './workflow-builder.js';
75
+ export { WorkflowStateAdapter, type PersistedWorkflowState, type StepCheckpoint, type WorkflowHistoryEntry as StateHistoryEntry, type SnapshotInfo, type DatabaseConnection, } from './worker/state-adapter.js';
72
76
  export type { EventHandler, ScheduleHandler, WorkflowContext, WorkflowState, WorkflowHistoryEntry, EventRegistration, ScheduleRegistration, ScheduleInterval, WorkflowDefinition, WorkflowOptions, ParsedEvent, OnProxy, EveryProxy, HandlerFunction, DatabaseContext, ActionData, ArtifactData, DependencyConfig, DependencyType, } from './types.js';
73
77
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAGH,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAG9F,OAAO,EAAE,EAAE,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAGxF,OAAO,EACL,KAAK,EACL,uBA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B,EAC1C,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACtB,KAAK,aAAa,IAAI,oBAAoB,EAC1C,KAAK,cAAc,IAAI,qBAAqB,EAC5C,KAAK,kBAAkB,EACvB,KAAK,SAAS,EACd,KAAK,kBAAkB,EACvB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,gBAAgB,GACtB,MAAM,0BAA0B,CAAA;AAGjC,OAAO,EACL,QAAQ,EACR,eAAe,EACf,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,uBAAuB,CAAA;AAG9B,OAAO,EACL,oBAAoB,EACpB,KAAK,sBAAsB,EAC3B,KAAK,cAAc,EACnB,KAAK,oBAAoB,IAAI,iBAAiB,EAC9C,KAAK,YAAY,EACjB,KAAK,kBAAkB,GACxB,MAAM,2BAA2B,CAAA;AAGlC,YAAY,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,aAAa,EACb,oBAAoB,EACpB,iBAAiB,EACjB,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,WAAW,EACX,OAAO,EACP,UAAU,EACV,eAAe,EACf,eAAe,EACf,UAAU,EACV,YAAY,EACZ,gBAAgB,EAChB,cAAc,GACf,MAAM,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -61,6 +61,13 @@
61
61
  */
62
62
  // Main Workflow API
63
63
  export { Workflow, createTestContext, parseEvent } from './workflow.js';
64
+ // WorkflowRuntime - single owner of the `$` runtime contract
65
+ // (composes cascade-context, database-context, on/send/every internally).
66
+ // New callers should reach for createWorkflowRuntime() to understand exactly
67
+ // what `$` contains end-to-end. The Workflow() function wraps a runtime with
68
+ // schedule/timer lifecycle; for tests, runtime.dispatch() is the canonical
69
+ // surface.
70
+ export { createWorkflowRuntime, } from './runtime.js';
64
71
  // Standalone event handling (for global registration)
65
72
  export { on, registerEventHandler, getEventHandlers, clearEventHandlers } from './on.js';
66
73
  // Standalone scheduling (for global registration)
@@ -79,4 +86,12 @@ export { topologicalSort, topologicalSortKahn, topologicalSortDFS, getExecutionL
79
86
  export { Barrier, BarrierTimeoutError, createBarrier, waitForAll, waitForAny, withConcurrencyLimit, } from './barrier.js';
80
87
  // Cascade Executor - code -> generative -> agentic -> human pattern
81
88
  export { CascadeExecutor, CascadeTimeoutError, TierSkippedError, AllTiersFailedError, TIER_ORDER, DEFAULT_TIER_TIMEOUTS, } from './cascade-executor.js';
89
+ // DurableStep - Cloudflare Workflows step wrapper
90
+ export { DurableStep, StepContext as DurableStepContext,
91
+ // Cascade types
92
+ DurableCascadeStep, AllTiersFailed, CascadeTimeout, DEFAULT_CASCADE_TIMEOUTS, CASCADE_TIER_ORDER, } from './worker/durable-step.js';
93
+ // WorkflowBuilder DSL - Fluent API for building durable workflows
94
+ export { workflow, WorkflowBuilder, } from './workflow-builder.js';
95
+ // WorkflowStateAdapter - Persistent state storage
96
+ export { WorkflowStateAdapter, } from './worker/state-adapter.js';
82
97
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAEH,oBAAoB;AACpB,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,UAAU,EAAyB,MAAM,eAAe,CAAA;AAE9F,sDAAsD;AACtD,OAAO,EAAE,EAAE,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAExF,kDAAkD;AAClD,OAAO,EACL,KAAK,EACL,uBAAuB,EACvB,mBA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OAAO,EACL,oBAAoB,GAMrB,MAAM,2BAA2B,CAAA"}
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Configurable Logger Interface
3
+ *
4
+ * Provides a pluggable logging abstraction for ai-workflows.
5
+ * By default uses console.log/warn/error, but can be replaced
6
+ * with any logging implementation.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { setLogger, getLogger } from 'ai-workflows'
11
+ *
12
+ * // Use default console logger
13
+ * const logger = getLogger()
14
+ * logger.log('[workflow] Starting', { workflowId: '123' })
15
+ *
16
+ * // Set a custom logger
17
+ * setLogger({
18
+ * log: (msg, data) => winston.info(msg, data),
19
+ * warn: (msg, data) => winston.warn(msg, data),
20
+ * error: (msg, error) => winston.error(msg, { error }),
21
+ * })
22
+ *
23
+ * // Disable logging
24
+ * setLogger({
25
+ * log: () => {},
26
+ * warn: () => {},
27
+ * error: () => {},
28
+ * })
29
+ * ```
30
+ */
31
+ /**
32
+ * Logger interface for ai-workflows
33
+ *
34
+ * Implementations must provide log, warn, and error methods.
35
+ * Data parameter is optional and can be any value for context.
36
+ */
37
+ export interface Logger {
38
+ /**
39
+ * Log informational messages
40
+ * @param msg - The log message
41
+ * @param data - Optional data for context
42
+ */
43
+ log(msg: string, data?: unknown): void;
44
+ /**
45
+ * Log warning messages
46
+ * @param msg - The warning message
47
+ * @param data - Optional data for context
48
+ */
49
+ warn(msg: string, data?: unknown): void;
50
+ /**
51
+ * Log error messages
52
+ * @param msg - The error message
53
+ * @param error - Optional error object or data
54
+ */
55
+ error(msg: string, error?: unknown): void;
56
+ }
57
+ /**
58
+ * Default console logger implementation
59
+ *
60
+ * Uses console.log, console.warn, and console.error.
61
+ */
62
+ export declare const consoleLogger: Logger;
63
+ /**
64
+ * No-op logger that discards all messages
65
+ *
66
+ * Useful for silencing logs in tests or production.
67
+ */
68
+ export declare const noopLogger: Logger;
69
+ /**
70
+ * Get the current logger instance
71
+ *
72
+ * @returns The current Logger implementation
73
+ */
74
+ export declare function getLogger(): Logger;
75
+ /**
76
+ * Set the logger implementation
77
+ *
78
+ * @param logger - The Logger implementation to use
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * // Use a custom logger
83
+ * setLogger({
84
+ * log: (msg, data) => myLogger.info(msg, data),
85
+ * warn: (msg, data) => myLogger.warn(msg, data),
86
+ * error: (msg, error) => myLogger.error(msg, { error }),
87
+ * })
88
+ *
89
+ * // Disable logging
90
+ * setLogger(noopLogger)
91
+ *
92
+ * // Reset to console
93
+ * setLogger(consoleLogger)
94
+ * ```
95
+ */
96
+ export declare function setLogger(logger: Logger): void;
97
+ /**
98
+ * Reset the logger to the default console logger
99
+ */
100
+ export declare function resetLogger(): void;
101
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH;;;;;GAKG;AACH,MAAM,WAAW,MAAM;IACrB;;;;OAIG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IAEtC;;;;OAIG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IAEvC;;;;OAIG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;CAC1C;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,MAwB3B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,UAAU,EAAE,MAIxB,CAAA;AAQD;;;;GAIG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9C;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,IAAI,CAElC"}
package/dist/logger.js ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Configurable Logger Interface
3
+ *
4
+ * Provides a pluggable logging abstraction for ai-workflows.
5
+ * By default uses console.log/warn/error, but can be replaced
6
+ * with any logging implementation.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { setLogger, getLogger } from 'ai-workflows'
11
+ *
12
+ * // Use default console logger
13
+ * const logger = getLogger()
14
+ * logger.log('[workflow] Starting', { workflowId: '123' })
15
+ *
16
+ * // Set a custom logger
17
+ * setLogger({
18
+ * log: (msg, data) => winston.info(msg, data),
19
+ * warn: (msg, data) => winston.warn(msg, data),
20
+ * error: (msg, error) => winston.error(msg, { error }),
21
+ * })
22
+ *
23
+ * // Disable logging
24
+ * setLogger({
25
+ * log: () => {},
26
+ * warn: () => {},
27
+ * error: () => {},
28
+ * })
29
+ * ```
30
+ */
31
+ /**
32
+ * Default console logger implementation
33
+ *
34
+ * Uses console.log, console.warn, and console.error.
35
+ */
36
+ export const consoleLogger = {
37
+ log(msg, data) {
38
+ if (data !== undefined) {
39
+ console.log(msg, data);
40
+ }
41
+ else {
42
+ console.log(msg);
43
+ }
44
+ },
45
+ warn(msg, data) {
46
+ if (data !== undefined) {
47
+ console.warn(msg, data);
48
+ }
49
+ else {
50
+ console.warn(msg);
51
+ }
52
+ },
53
+ error(msg, error) {
54
+ if (error !== undefined) {
55
+ console.error(msg, error);
56
+ }
57
+ else {
58
+ console.error(msg);
59
+ }
60
+ },
61
+ };
62
+ /**
63
+ * No-op logger that discards all messages
64
+ *
65
+ * Useful for silencing logs in tests or production.
66
+ */
67
+ export const noopLogger = {
68
+ log() { },
69
+ warn() { },
70
+ error() { },
71
+ };
72
+ /**
73
+ * Current logger instance
74
+ * Defaults to consoleLogger
75
+ */
76
+ let currentLogger = consoleLogger;
77
+ /**
78
+ * Get the current logger instance
79
+ *
80
+ * @returns The current Logger implementation
81
+ */
82
+ export function getLogger() {
83
+ return currentLogger;
84
+ }
85
+ /**
86
+ * Set the logger implementation
87
+ *
88
+ * @param logger - The Logger implementation to use
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * // Use a custom logger
93
+ * setLogger({
94
+ * log: (msg, data) => myLogger.info(msg, data),
95
+ * warn: (msg, data) => myLogger.warn(msg, data),
96
+ * error: (msg, error) => myLogger.error(msg, { error }),
97
+ * })
98
+ *
99
+ * // Disable logging
100
+ * setLogger(noopLogger)
101
+ *
102
+ * // Reset to console
103
+ * setLogger(consoleLogger)
104
+ * ```
105
+ */
106
+ export function setLogger(logger) {
107
+ currentLogger = logger;
108
+ }
109
+ /**
110
+ * Reset the logger to the default console logger
111
+ */
112
+ export function resetLogger() {
113
+ currentLogger = consoleLogger;
114
+ }
115
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AA+BH;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAW;IACnC,GAAG,CAAC,GAAW,EAAE,IAAc;QAC7B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;QACxB,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAClB,CAAC;IACH,CAAC;IAED,IAAI,CAAC,GAAW,EAAE,IAAc;QAC9B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;QACzB,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QACnB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,GAAW,EAAE,KAAe;QAChC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,CAAC,KAAK,CAAC,GAAG,EAA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;AAET;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAAC,gBAAgB,EAAE,2BAA2B,GAAG,OAAO,CA+BzF;AAYD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,EAAE,SAAkB,CAAA"}
1
+ {"version":3,"file":"on.d.ts","sourceRoot":"","sources":["../src/on.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,gBAAgB,EAChB,OAAO,EAIR,MAAM,YAAY,CAAA;AAOnB;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,iBAAiB,EAAE,CAEtD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,YAAY,EACrB,YAAY,CAAC,EAAE,gBAAgB,GAC9B,IAAI,CAQN;AAED;;;GAGG;AACH,MAAM,MAAM,2BAA2B,GAAG,CACxC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,YAAY,EACrB,YAAY,CAAC,EAAE,gBAAgB,KAC5B,IAAI,CAAA;AAET;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAAC,gBAAgB,EAAE,2BAA2B,GAAG,OAAO,CA8BzF;AAYD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,EAAE,SAAkB,CAAA"}
package/dist/on.js CHANGED
@@ -35,7 +35,7 @@ export function registerEventHandler(noun, event, handler, dependencies) {
35
35
  event,
36
36
  handler,
37
37
  source: handler.toString(),
38
- dependencies,
38
+ ...(dependencies !== undefined && { dependencies }),
39
39
  });
40
40
  }
41
41
  /**
@@ -70,7 +70,7 @@ export function createTypedOnProxy(registerCallback) {
70
70
  return (handler, dependencies) => {
71
71
  registerCallback(noun, event, handler, dependencies);
72
72
  };
73
- }
73
+ },
74
74
  });
75
75
  // Create typed handler for the top-level proxy (noun accessors)
76
76
  const onHandler = {
@@ -78,7 +78,7 @@ export function createTypedOnProxy(registerCallback) {
78
78
  // Return a proxy for the event level with typed handler
79
79
  const eventTarget = {};
80
80
  return new Proxy(eventTarget, createNounHandler(noun));
81
- }
81
+ },
82
82
  };
83
83
  // Create and return the typed OnProxy
84
84
  const target = {};
package/dist/on.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"on.js","sourceRoot":"","sources":["../src/on.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAYH;;GAEG;AACH,MAAM,aAAa,GAAwB,EAAE,CAAA;AAE7C;;GAEG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,CAAC,GAAG,aAAa,CAAC,CAAA;AAC3B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB;IAChC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAA;AAC1B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAClC,IAAY,EACZ,KAAa,EACb,OAAqB,EACrB,YAA+B;IAE/B,aAAa,CAAC,IAAI,CAAC;QACjB,IAAI;QACJ,KAAK;QACL,OAAO;QACP,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE;QAC1B,YAAY;KACb,CAAC,CAAA;AACJ,CAAC;AAaD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,kBAAkB,CAAC,gBAA6C;IAC9E,4DAA4D;IAC5D,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAyB,EAAE,CAAC,CAAC;QAClE,GAAG,CACD,OAAyF,EACzF,KAAa,EACb,SAAkB;YAElB,0EAA0E;YAC1E,OAAO,CAAC,OAAqB,EAAE,YAA+B,EAAE,EAAE;gBAChE,gBAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,CAAC,CAAA;YACtD,CAAC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uBG;AACH,MAAM,UAAU,kBAAkB,CAAC,gBAA6C;IAC9E,4DAA4D;IAC5D,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAyB,EAAE,CAAC,CAAC;QAClE,GAAG,CACD,OAAyF,EACzF,KAAa,EACb,SAAkB;YAElB,0EAA0E;YAC1E,OAAO,CAAC,OAAqB,EAAE,YAA+B,EAAE,EAAE;gBAChE,gBAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,CAAC,CAAA;YACtD,CAAC,CAAA;QACH,CAAC;KACF,CAAC,CAAA;IAEF,gEAAgE;IAChE,MAAM,SAAS,GAAmB;QAChC,GAAG,CAAC,OAAuC,EAAE,IAAY,EAAE,SAAkB;YAC3E,wDAAwD;YACxD,MAAM,WAAW,GAGb,EAAE,CAAA;YACN,OAAO,IAAI,KAAK,CAAC,WAAW,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAmB,CAAA;QAC1E,CAAC;KACF,CAAA;IAED,sCAAsC;IACtC,MAAM,MAAM,GAAmC,EAAE,CAAA;IACjD,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE,SAAS,CAAY,CAAA;AAChD,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa;IACpB,OAAO,kBAAkB,CAAC,oBAAoB,CAAC,CAAA;AACjD,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,aAAa,EAAE,CAAA"}
@@ -0,0 +1,169 @@
1
+ /**
2
+ * WorkflowRuntime - Owns the `$` runtime contract
3
+ *
4
+ * Purpose: Single module that owns end-to-end construction of the `$` context
5
+ * a workflow handler receives. Before this module existed the answer to
6
+ * "what does the handler see when it runs?" was stitched together from three
7
+ * separate modules:
8
+ *
9
+ * - cascade-context.ts (5W+H tracing)
10
+ * - database-context.ts (DB injection)
11
+ * - on.ts / send.ts / every.ts (event dispatch)
12
+ *
13
+ * Callers reading `$.on.Order.placed(...)` had no single place to discover
14
+ * what `$` contains. Tests had to assemble three contexts independently to
15
+ * exercise a handler.
16
+ *
17
+ * `WorkflowRuntime` collapses these into one port:
18
+ *
19
+ * - {@link createWorkflowRuntime} builds the runtime, owning the event/
20
+ * schedule registries, the optional `DatabaseContext` injection, and the
21
+ * optional cascade context.
22
+ * - The runtime exposes `$` (a {@link WorkflowContext}) and `dispatch()` —
23
+ * the latter is the test surface for exercising a handler against an
24
+ * event without spinning up a full {@link Workflow}.
25
+ *
26
+ * The cascade-context, database-context, and on/send/every modules continue
27
+ * to exist as **internal seams** — private adapters the runtime composes.
28
+ * `DatabaseContext` stays as an injected port so callers can wire either
29
+ * `ai-database`'s adapter or an in-memory adapter; this keeps `ai-workflows`
30
+ * Layer 0.
31
+ *
32
+ * @example Basic usage
33
+ * ```ts
34
+ * import { createWorkflowRuntime } from 'ai-workflows'
35
+ *
36
+ * const runtime = createWorkflowRuntime()
37
+ * runtime.register('Customer', 'created', async (customer, $) => {
38
+ * $.log('New customer:', customer)
39
+ * })
40
+ * await runtime.dispatch('Customer.created', { id: '123' })
41
+ * ```
42
+ *
43
+ * @example With injected DatabaseContext
44
+ * ```ts
45
+ * import { createWorkflowRuntime, createMemoryDatabaseContext } from 'ai-workflows'
46
+ *
47
+ * const runtime = createWorkflowRuntime({ db: createMemoryDatabaseContext() })
48
+ * ```
49
+ *
50
+ * @example Direct handler dispatch (test surface)
51
+ * ```ts
52
+ * const runtime = createWorkflowRuntime()
53
+ * const handler = vi.fn()
54
+ * runtime.register('Order', 'placed', handler)
55
+ * await runtime.dispatch('Order.placed', { id: 'o-1' })
56
+ * expect(handler).toHaveBeenCalled()
57
+ * ```
58
+ *
59
+ * @packageDocumentation
60
+ */
61
+ import type { DatabaseContext, EventHandler, EventRegistration, ParsedEvent, ScheduleHandler, ScheduleInterval, ScheduleRegistration, WorkflowContext, WorkflowState, DependencyConfig } from './types.js';
62
+ import { type CascadeContext } from './cascade-context.js';
63
+ /**
64
+ * Parse an event string in `Noun.event` form. Returns `null` for invalid
65
+ * input. Lives on the runtime because dispatch is the only thing that needs
66
+ * to crack event names; `workflow.ts` re-exports it for back-compat.
67
+ */
68
+ export declare function parseEvent(event: string): ParsedEvent | null;
69
+ /**
70
+ * Options for constructing a {@link WorkflowRuntime}.
71
+ *
72
+ * All options are optional — a runtime constructed with no arguments runs
73
+ * fully in-memory with no persistence and no parent trace.
74
+ */
75
+ export interface WorkflowRuntimeOptions {
76
+ /**
77
+ * Initial state context (key/value bag exposed as `$.state`).
78
+ * Cloned defensively at construction so the caller's object is not mutated.
79
+ */
80
+ context?: Record<string, unknown>;
81
+ /**
82
+ * Optional persistence port. When provided, `$.send` records events and
83
+ * `$.do` records actions through this adapter. When omitted, events and
84
+ * actions are still delivered in-memory but not persisted.
85
+ */
86
+ db?: DatabaseContext;
87
+ /**
88
+ * Optional cascade context for distributed tracing / 5W+H step recording.
89
+ * If omitted a fresh root cascade context is created.
90
+ */
91
+ cascade?: CascadeContext;
92
+ /**
93
+ * Optional name for the cascade context (used when cascade is auto-created).
94
+ */
95
+ name?: string;
96
+ }
97
+ /**
98
+ * Public surface of the workflow runtime.
99
+ *
100
+ * The runtime owns construction of `$` and provides a single dispatch port
101
+ * for delivering events. Internal modules (cascade-context, database-context,
102
+ * on/send/every) are composed here, not exposed.
103
+ */
104
+ export interface WorkflowRuntime {
105
+ /**
106
+ * The `$` context handed to event and schedule handlers. This is the
107
+ * single source of truth for "what does a handler see when it runs."
108
+ */
109
+ readonly $: WorkflowContext;
110
+ /**
111
+ * The cascade context owned by this runtime. Exposed for distributed
112
+ * tracing integration; not part of the handler-facing surface.
113
+ */
114
+ readonly cascade: CascadeContext;
115
+ /**
116
+ * Register an event handler under a noun/event pair.
117
+ *
118
+ * Equivalent to `$.on.<Noun>.<event>(handler)`; provided as a direct method
119
+ * for tests and callers that hold the runtime reference rather than `$`.
120
+ */
121
+ register(noun: string, event: string, handler: EventHandler, dependencies?: DependencyConfig): void;
122
+ /**
123
+ * Register a schedule handler under an interval.
124
+ * Schedule timers are not started by the runtime — the {@link Workflow}
125
+ * instance handles that. The runtime only tracks registrations.
126
+ */
127
+ registerSchedule(interval: ScheduleInterval, handler: ScheduleHandler): void;
128
+ /**
129
+ * Dispatch an event to all matching registered handlers.
130
+ *
131
+ * This is the canonical test surface — exercise a handler by registering
132
+ * it on the runtime and calling `dispatch`. Awaits all handlers; rethrows
133
+ * the first error if any handler throws.
134
+ */
135
+ dispatch(event: string, data: unknown): Promise<void>;
136
+ /**
137
+ * Dispatch an event and return the result of the first matching handler.
138
+ * Used by `$.do` and `$.try` semantics — `do` is the durable variant that
139
+ * also persists through the database adapter when one is configured.
140
+ */
141
+ execute<TResult = unknown>(event: string, data: unknown, durable: boolean): Promise<TResult>;
142
+ /**
143
+ * All event handlers registered on this runtime.
144
+ * Returns the live array (mutated as new handlers register) so callers
145
+ * such as the {@link Workflow} lifecycle wrapper can read it as a definition.
146
+ */
147
+ getEventRegistry(): EventRegistration[];
148
+ /**
149
+ * All schedule handlers registered on this runtime. See {@link getEventRegistry}
150
+ * for sharing semantics.
151
+ */
152
+ getScheduleRegistry(): ScheduleRegistration[];
153
+ /** Mutable workflow state (context bag + history). */
154
+ readonly state: WorkflowState;
155
+ }
156
+ /**
157
+ * Construct a {@link WorkflowRuntime}.
158
+ *
159
+ * The runtime is the single owner of the `$` contract — it composes:
160
+ *
161
+ * 1. Event/schedule registries (the dispatch half of on/send/every).
162
+ * 2. The optional injected `DatabaseContext` for durable record-keeping.
163
+ * 3. A cascade context for 5W+H tracing.
164
+ *
165
+ * The returned `runtime.$` is what handlers receive; `runtime.dispatch` is
166
+ * the canonical test surface.
167
+ */
168
+ export declare function createWorkflowRuntime(options?: WorkflowRuntimeOptions): WorkflowRuntime;
169
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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