@geenius/ai-workflow 0.1.0 → 0.4.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 (159) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +69 -1
  3. package/package.json +60 -27
  4. package/packages/convex/dist/index.d.ts +363 -0
  5. package/packages/convex/dist/index.js +200 -0
  6. package/packages/convex/dist/index.js.map +1 -0
  7. package/packages/react/dist/index.d.ts +466 -0
  8. package/packages/react/dist/index.js +13914 -0
  9. package/packages/react/dist/index.js.map +1 -0
  10. package/packages/react-css/{src/styles.css → dist/index.css} +107 -253
  11. package/packages/react-css/dist/index.css.map +1 -0
  12. package/packages/react-css/dist/index.d.ts +495 -0
  13. package/packages/react-css/dist/index.js +13901 -0
  14. package/packages/react-css/dist/index.js.map +1 -0
  15. package/packages/shared/dist/index.d.ts +1368 -0
  16. package/packages/shared/dist/index.js +1681 -0
  17. package/packages/shared/dist/index.js.map +1 -0
  18. package/packages/solidjs/dist/index.d.ts +452 -0
  19. package/packages/solidjs/dist/index.js +13830 -0
  20. package/packages/solidjs/dist/index.js.map +1 -0
  21. package/packages/solidjs-css/{src/styles.css → dist/index.css} +107 -253
  22. package/packages/solidjs-css/dist/index.css.map +1 -0
  23. package/packages/solidjs-css/dist/index.d.ts +471 -0
  24. package/packages/solidjs-css/dist/index.js +13774 -0
  25. package/packages/solidjs-css/dist/index.js.map +1 -0
  26. package/.changeset/config.json +0 -11
  27. package/.github/CODEOWNERS +0 -1
  28. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -16
  29. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -11
  30. package/.github/PULL_REQUEST_TEMPLATE.md +0 -10
  31. package/.github/dependabot.yml +0 -11
  32. package/.github/workflows/ci.yml +0 -23
  33. package/.github/workflows/release.yml +0 -29
  34. package/.nvmrc +0 -1
  35. package/.project/ACCOUNT.yaml +0 -4
  36. package/.project/IDEAS.yaml +0 -7
  37. package/.project/PROJECT.yaml +0 -11
  38. package/.project/ROADMAP.yaml +0 -15
  39. package/CODE_OF_CONDUCT.md +0 -16
  40. package/CONTRIBUTING.md +0 -26
  41. package/SECURITY.md +0 -15
  42. package/SUPPORT.md +0 -8
  43. package/packages/convex/README.md +0 -1
  44. package/packages/convex/package.json +0 -12
  45. package/packages/convex/src/convex.config.ts +0 -3
  46. package/packages/convex/src/index.ts +0 -3
  47. package/packages/convex/src/mutations.ts +0 -36
  48. package/packages/convex/src/queries.ts +0 -19
  49. package/packages/convex/src/schema.ts +0 -24
  50. package/packages/convex/tsconfig.json +0 -25
  51. package/packages/react/README.md +0 -1
  52. package/packages/react/package.json +0 -46
  53. package/packages/react/src/components/ApprovalModal.tsx +0 -47
  54. package/packages/react/src/components/StepConfigPanel.tsx +0 -67
  55. package/packages/react/src/components/StepConnector.tsx +0 -47
  56. package/packages/react/src/components/StepNode.tsx +0 -38
  57. package/packages/react/src/components/StepPalette.tsx +0 -48
  58. package/packages/react/src/components/WorkflowCanvas.tsx +0 -42
  59. package/packages/react/src/components/WorkflowRunPanel.tsx +0 -64
  60. package/packages/react/src/components/WorkflowToolbar.tsx +0 -43
  61. package/packages/react/src/components/index.ts +0 -9
  62. package/packages/react/src/hooks/index.ts +0 -10
  63. package/packages/react/src/hooks/useApprovalGate.ts +0 -59
  64. package/packages/react/src/hooks/useWorkflow.ts +0 -39
  65. package/packages/react/src/hooks/useWorkflowBuilder.ts +0 -121
  66. package/packages/react/src/hooks/useWorkflowRun.ts +0 -75
  67. package/packages/react/src/hooks/useWorkflowStep.ts +0 -52
  68. package/packages/react/src/hooks/useWorkflowTemplates.ts +0 -54
  69. package/packages/react/src/index.ts +0 -16
  70. package/packages/react/src/pages/WorkflowBuilderPage.tsx +0 -81
  71. package/packages/react/src/pages/WorkflowRunsPage.tsx +0 -59
  72. package/packages/react/src/pages/index.ts +0 -3
  73. package/packages/react/tsconfig.json +0 -1
  74. package/packages/react/tsup.config.ts +0 -7
  75. package/packages/react-css/README.md +0 -1
  76. package/packages/react-css/package.json +0 -44
  77. package/packages/react-css/src/components/ApprovalModal.tsx +0 -6
  78. package/packages/react-css/src/components/StepConfigPanel.tsx +0 -7
  79. package/packages/react-css/src/components/StepConnector.tsx +0 -6
  80. package/packages/react-css/src/components/StepNode.tsx +0 -7
  81. package/packages/react-css/src/components/StepPalette.tsx +0 -6
  82. package/packages/react-css/src/components/WorkflowCanvas.tsx +0 -6
  83. package/packages/react-css/src/components/WorkflowRunPanel.tsx +0 -9
  84. package/packages/react-css/src/components/WorkflowToolbar.tsx +0 -4
  85. package/packages/react-css/src/components/index.ts +0 -9
  86. package/packages/react-css/src/hooks/index.ts +0 -3
  87. package/packages/react-css/src/hooks/useWorkflow.ts +0 -39
  88. package/packages/react-css/src/hooks/useWorkflowBuilder.ts +0 -121
  89. package/packages/react-css/src/index.ts +0 -7
  90. package/packages/react-css/src/pages/WorkflowBuilderPage.tsx +0 -16
  91. package/packages/react-css/src/pages/WorkflowRunsPage.tsx +0 -6
  92. package/packages/react-css/src/pages/index.ts +0 -3
  93. package/packages/react-css/tsconfig.json +0 -26
  94. package/packages/react-css/tsup.config.ts +0 -2
  95. package/packages/shared/README.md +0 -1
  96. package/packages/shared/package.json +0 -56
  97. package/packages/shared/src/__tests__/ai-workflow.test.ts +0 -217
  98. package/packages/shared/src/config.ts +0 -49
  99. package/packages/shared/src/convex/index.ts +0 -2
  100. package/packages/shared/src/convex/schemas.ts +0 -42
  101. package/packages/shared/src/engine.test.ts +0 -1
  102. package/packages/shared/src/engine.ts +0 -295
  103. package/packages/shared/src/index.ts +0 -43
  104. package/packages/shared/src/steps.ts +0 -68
  105. package/packages/shared/src/templates.ts +0 -172
  106. package/packages/shared/src/types.ts +0 -237
  107. package/packages/shared/src/utils/cost.ts +0 -79
  108. package/packages/shared/src/utils/dag.ts +0 -133
  109. package/packages/shared/src/utils/index.ts +0 -5
  110. package/packages/shared/src/utils/interpolation.ts +0 -53
  111. package/packages/shared/src/validators.ts +0 -215
  112. package/packages/shared/tsconfig.json +0 -1
  113. package/packages/shared/tsup.config.ts +0 -5
  114. package/packages/shared/vitest.config.ts +0 -4
  115. package/packages/solidjs/README.md +0 -1
  116. package/packages/solidjs/package.json +0 -45
  117. package/packages/solidjs/src/components/ApprovalModal.tsx +0 -18
  118. package/packages/solidjs/src/components/StepConfigPanel.tsx +0 -14
  119. package/packages/solidjs/src/components/StepConnector.tsx +0 -11
  120. package/packages/solidjs/src/components/StepNode.tsx +0 -12
  121. package/packages/solidjs/src/components/StepPalette.tsx +0 -22
  122. package/packages/solidjs/src/components/WorkflowCanvas.tsx +0 -23
  123. package/packages/solidjs/src/components/WorkflowRunPanel.tsx +0 -18
  124. package/packages/solidjs/src/components/WorkflowToolbar.tsx +0 -13
  125. package/packages/solidjs/src/components/index.ts +0 -9
  126. package/packages/solidjs/src/index.ts +0 -7
  127. package/packages/solidjs/src/pages/WorkflowBuilderPage.tsx +0 -37
  128. package/packages/solidjs/src/pages/WorkflowRunsPage.tsx +0 -20
  129. package/packages/solidjs/src/pages/index.ts +0 -3
  130. package/packages/solidjs/src/primitives/createApprovalGate.ts +0 -29
  131. package/packages/solidjs/src/primitives/createWorkflow.ts +0 -28
  132. package/packages/solidjs/src/primitives/createWorkflowBuilder.ts +0 -56
  133. package/packages/solidjs/src/primitives/createWorkflowRun.ts +0 -32
  134. package/packages/solidjs/src/primitives/createWorkflowStep.ts +0 -23
  135. package/packages/solidjs/src/primitives/createWorkflowTemplates.ts +0 -28
  136. package/packages/solidjs/src/primitives/index.ts +0 -8
  137. package/packages/solidjs/tsconfig.json +0 -1
  138. package/packages/solidjs/tsup.config.ts +0 -7
  139. package/packages/solidjs-css/README.md +0 -1
  140. package/packages/solidjs-css/package.json +0 -43
  141. package/packages/solidjs-css/src/components/ApprovalModal.tsx +0 -6
  142. package/packages/solidjs-css/src/components/StepConfigPanel.tsx +0 -7
  143. package/packages/solidjs-css/src/components/StepConnector.tsx +0 -6
  144. package/packages/solidjs-css/src/components/StepNode.tsx +0 -7
  145. package/packages/solidjs-css/src/components/StepPalette.tsx +0 -7
  146. package/packages/solidjs-css/src/components/WorkflowCanvas.tsx +0 -7
  147. package/packages/solidjs-css/src/components/WorkflowRunPanel.tsx +0 -8
  148. package/packages/solidjs-css/src/components/WorkflowToolbar.tsx +0 -5
  149. package/packages/solidjs-css/src/components/index.ts +0 -9
  150. package/packages/solidjs-css/src/index.ts +0 -7
  151. package/packages/solidjs-css/src/pages/WorkflowBuilderPage.tsx +0 -2
  152. package/packages/solidjs-css/src/pages/WorkflowRunsPage.tsx +0 -7
  153. package/packages/solidjs-css/src/pages/index.ts +0 -3
  154. package/packages/solidjs-css/src/primitives/createWorkflow.ts +0 -28
  155. package/packages/solidjs-css/src/primitives/createWorkflowBuilder.ts +0 -56
  156. package/packages/solidjs-css/src/primitives/index.ts +0 -1
  157. package/packages/solidjs-css/tsconfig.json +0 -27
  158. package/packages/solidjs-css/tsup.config.ts +0 -2
  159. package/pnpm-workspace.yaml +0 -2
@@ -0,0 +1,1368 @@
1
+ import { z } from 'zod/v4';
2
+ import { GeeniusError, ErrorCode } from '@geenius/tools/errors';
3
+
4
+ /**
5
+ * @module WorkflowConfig
6
+ * @package @geenius/ai-workflow-shared
7
+ * @description Shared configuration helpers for the workflow runtime. This file
8
+ * defines the normalized execution options that the engine and UI bindings use
9
+ * to stay aligned on limits, timeouts, and telemetry defaults.
10
+ */
11
+ /**
12
+ * Runtime configuration options accepted by the workflow package.
13
+ *
14
+ * @property maxSteps Maximum number of steps allowed in a definition.
15
+ * @property maxExecutionTimeMs Maximum allowed runtime for one execution.
16
+ * @property maxRetries Default retry count for retryable steps.
17
+ * @property defaultTimeout Default timeout for external operations.
18
+ * @property enableLogging Whether runtime logging is enabled.
19
+ * @property enableMetrics Whether metric capture is enabled.
20
+ */
21
+ interface ConfigureWorkflowOptions {
22
+ maxSteps?: number;
23
+ maxExecutionTimeMs?: number;
24
+ maxRetries?: number;
25
+ defaultTimeout?: number;
26
+ enableLogging?: boolean;
27
+ enableMetrics?: boolean;
28
+ }
29
+ /**
30
+ * Normalized workflow runtime configuration.
31
+ *
32
+ * @property maxSteps Maximum number of steps allowed in a definition.
33
+ * @property maxExecutionTimeMs Maximum allowed runtime for one execution.
34
+ * @property maxRetries Default retry count for retryable steps.
35
+ * @property defaultTimeout Default timeout for external operations.
36
+ * @property enableLogging Whether runtime logging is enabled.
37
+ * @property enableMetrics Whether metric capture is enabled.
38
+ */
39
+ interface WorkflowRuntimeConfig {
40
+ maxSteps: number;
41
+ maxExecutionTimeMs: number;
42
+ maxRetries: number;
43
+ defaultTimeout: number;
44
+ enableLogging: boolean;
45
+ enableMetrics: boolean;
46
+ }
47
+ /**
48
+ * Build a normalized workflow runtime configuration object.
49
+ *
50
+ * @param options Partial execution options supplied by the consumer.
51
+ * @returns A fully populated workflow runtime configuration.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const config = configureWorkflow({
56
+ * maxSteps: 50,
57
+ * maxExecutionTimeMs: 3600000,
58
+ * maxRetries: 3,
59
+ * enableMetrics: true,
60
+ * })
61
+ * ```
62
+ */
63
+ declare function configureWorkflow(options?: ConfigureWorkflowOptions): WorkflowRuntimeConfig;
64
+ /**
65
+ * Default runtime configuration used when consumers do not override options.
66
+ */
67
+ declare const defaultWorkflowConfig: WorkflowRuntimeConfig;
68
+
69
+ /**
70
+ * @module WorkflowTypes
71
+ * @package @geenius/ai-workflow-shared
72
+ * @description Shared type contract for workflow definitions, step configs,
73
+ * execution runs, and builder state. Every framework variant and adapter layer
74
+ * imports these types to stay surface-compatible.
75
+ */
76
+ /**
77
+ * Lifecycle state for a stored workflow definition.
78
+ */
79
+ type WorkflowStatus = 'draft' | 'active' | 'paused' | 'archived';
80
+ /**
81
+ * Lifecycle state for a workflow execution run.
82
+ */
83
+ type RunStatus = 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'cancelled';
84
+ /**
85
+ * Lifecycle state for a single step execution result.
86
+ */
87
+ type StepStatus = 'pending' | 'running' | 'completed' | 'failed' | 'skipped' | 'waiting-approval';
88
+ /**
89
+ * Discriminant for all built-in workflow step kinds.
90
+ */
91
+ type StepType = 'llm-call' | 'transform' | 'condition' | 'human-approval' | 'webhook' | 'delay' | 'parallel' | 'loop' | 'sub-workflow' | 'custom';
92
+ /**
93
+ * Serializable workflow definition consumed by the engine and UI packages.
94
+ *
95
+ * @property id Stable workflow identifier.
96
+ * @property name Human-readable workflow name.
97
+ * @property description Optional description shown in builders or catalogs.
98
+ * @property version Monotonic workflow definition version.
99
+ * @property status Lifecycle state for the definition.
100
+ * @property steps Ordered step definitions included in the workflow.
101
+ * @property connections Directed edges that form the workflow DAG.
102
+ * @property inputSchema Declared user inputs accepted by the workflow.
103
+ * @property variables Global variables available to all workflow steps.
104
+ * @property tags Optional classification tags for discovery and filtering.
105
+ * @property createdBy Identifier for the workflow author.
106
+ * @property createdAt Unix timestamp for initial creation.
107
+ * @property updatedAt Unix timestamp for the last definition update.
108
+ */
109
+ interface WorkflowDefinition {
110
+ id: string;
111
+ name: string;
112
+ description?: string;
113
+ version: number;
114
+ status: WorkflowStatus;
115
+ /** Steps in execution order */
116
+ steps: WorkflowStepDef[];
117
+ /** Connections between steps (DAG edges) */
118
+ connections: StepConnection[];
119
+ /** Input variables the workflow accepts */
120
+ inputSchema?: WorkflowVariable[];
121
+ /** Global variables available to all steps */
122
+ variables?: Record<string, unknown>;
123
+ /** Tags for organization */
124
+ tags?: string[];
125
+ createdBy: string;
126
+ createdAt: number;
127
+ updatedAt: number;
128
+ }
129
+ /**
130
+ * Definition for one step inside a workflow DAG.
131
+ *
132
+ * @property id Stable step identifier unique within the workflow.
133
+ * @property name Human-readable step label.
134
+ * @property type Step kind discriminant used by builders and executors.
135
+ * @property config Step-specific configuration payload.
136
+ * @property position Optional builder-canvas coordinates for the node.
137
+ * @property optional Whether the workflow may continue after this step fails.
138
+ * @property retries Optional retry policy for the step.
139
+ */
140
+ interface WorkflowStepDef {
141
+ id: string;
142
+ name: string;
143
+ type: StepType;
144
+ /** Step-specific configuration */
145
+ config: StepConfig;
146
+ /** Position for visual builder */
147
+ position?: {
148
+ x: number;
149
+ y: number;
150
+ };
151
+ /** Whether this step can be skipped on failure */
152
+ optional?: boolean;
153
+ /** Retry config */
154
+ retries?: {
155
+ maxAttempts: number;
156
+ backoffMs: number;
157
+ };
158
+ }
159
+ /**
160
+ * Union of all supported step configuration payloads.
161
+ */
162
+ type StepConfig = LLMCallConfig | TransformConfig | ConditionConfig | HumanApprovalConfig | WebhookConfig | DelayConfig | ParallelConfig | LoopConfig | SubWorkflowConfig | CustomStepConfig;
163
+ /**
164
+ * Configuration for an LLM invocation step.
165
+ *
166
+ * @property type Discriminant for the step config union.
167
+ * @property model Optional model identifier to execute against.
168
+ * @property systemPrompt System prompt applied before the user prompt.
169
+ * @property userPromptTemplate Interpolated user prompt template.
170
+ * @property temperature Optional creativity control for the model.
171
+ * @property maxTokens Optional output token limit.
172
+ * @property parseJson Whether the response should be parsed as JSON.
173
+ * @property outputVar Variable name that receives the step output.
174
+ */
175
+ interface LLMCallConfig {
176
+ type: 'llm-call';
177
+ model?: string;
178
+ systemPrompt: string;
179
+ userPromptTemplate: string;
180
+ temperature?: number;
181
+ maxTokens?: number;
182
+ /** Parse response as JSON */
183
+ parseJson?: boolean;
184
+ /** Output variable name */
185
+ outputVar: string;
186
+ }
187
+ /**
188
+ * Configuration for a transform step that evaluates a data expression.
189
+ *
190
+ * @property type Discriminant for the step config union.
191
+ * @property expression Transform expression evaluated against workflow variables.
192
+ * @property inputVars Variable names required by the expression.
193
+ * @property outputVar Variable name that receives the transform result.
194
+ */
195
+ interface TransformConfig {
196
+ type: 'transform';
197
+ /** JavaScript expression to evaluate (sandboxed) */
198
+ expression: string;
199
+ /** Input variable names */
200
+ inputVars: string[];
201
+ /** Output variable name */
202
+ outputVar: string;
203
+ }
204
+ /**
205
+ * Configuration for a conditional branch step.
206
+ *
207
+ * @property type Discriminant for the step config union.
208
+ * @property expression Boolean expression evaluated against workflow variables.
209
+ * @property trueStepId Step identifier selected when the expression is truthy.
210
+ * @property falseStepId Step identifier selected when the expression is falsy.
211
+ */
212
+ interface ConditionConfig {
213
+ type: 'condition';
214
+ /** JavaScript expression that evaluates to boolean */
215
+ expression: string;
216
+ /** Step ID to go to if true */
217
+ trueStepId: string;
218
+ /** Step ID to go to if false */
219
+ falseStepId: string;
220
+ }
221
+ /**
222
+ * Configuration for a human approval gate.
223
+ *
224
+ * @property type Discriminant for the step config union.
225
+ * @property message Approval request message shown to operators.
226
+ * @property approvers Optional list of users or roles allowed to approve.
227
+ * @property autoApproveAfterMs Optional timeout before auto-approval.
228
+ */
229
+ interface HumanApprovalConfig {
230
+ type: 'human-approval';
231
+ message: string;
232
+ /** Who can approve */
233
+ approvers?: string[];
234
+ /** Auto-approve after timeout (ms) */
235
+ autoApproveAfterMs?: number;
236
+ }
237
+ /**
238
+ * Configuration for an outbound webhook step.
239
+ *
240
+ * @property type Discriminant for the step config union.
241
+ * @property url Target endpoint URL.
242
+ * @property method HTTP method used for the request.
243
+ * @property headers Optional request headers.
244
+ * @property bodyTemplate Optional interpolated request body template.
245
+ * @property outputVar Variable name that receives the webhook response.
246
+ */
247
+ interface WebhookConfig {
248
+ type: 'webhook';
249
+ url: string;
250
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
251
+ headers?: Record<string, string>;
252
+ bodyTemplate?: string;
253
+ outputVar: string;
254
+ }
255
+ /**
256
+ * Configuration for a wait or backoff step.
257
+ *
258
+ * @property type Discriminant for the step config union.
259
+ * @property durationMs Delay duration in milliseconds.
260
+ */
261
+ interface DelayConfig {
262
+ type: 'delay';
263
+ durationMs: number;
264
+ }
265
+ /**
266
+ * Configuration for a fan-out step that runs multiple children concurrently.
267
+ *
268
+ * @property type Discriminant for the step config union.
269
+ * @property stepIds Step identifiers executed in parallel.
270
+ * @property failurePolicy Failure handling policy for the parallel branch.
271
+ */
272
+ interface ParallelConfig {
273
+ type: 'parallel';
274
+ /** Step IDs to execute in parallel */
275
+ stepIds: string[];
276
+ /** How to handle failures: 'fail-fast' or 'continue' */
277
+ failurePolicy: 'fail-fast' | 'continue';
278
+ }
279
+ /**
280
+ * Configuration for iterating over a collection of values.
281
+ *
282
+ * @property type Discriminant for the step config union.
283
+ * @property stepIds Step identifiers executed for each iteration.
284
+ * @property iterateVar Variable name containing the input collection.
285
+ * @property itemVar Variable name exposed for the current item.
286
+ * @property maxIterations Optional safety cap for loop iterations.
287
+ */
288
+ interface LoopConfig {
289
+ type: 'loop';
290
+ /** Step IDs to loop over */
291
+ stepIds: string[];
292
+ /** Variable containing the array to iterate */
293
+ iterateVar: string;
294
+ /** Variable name for current item */
295
+ itemVar: string;
296
+ /** Max iterations */
297
+ maxIterations?: number;
298
+ }
299
+ /**
300
+ * Configuration for invoking another stored workflow definition.
301
+ *
302
+ * @property type Discriminant for the step config union.
303
+ * @property workflowId Referenced workflow identifier.
304
+ * @property inputMapping Mapping from local variables to child workflow inputs.
305
+ * @property outputVar Variable name that receives the sub-workflow result.
306
+ */
307
+ interface SubWorkflowConfig {
308
+ type: 'sub-workflow';
309
+ workflowId: string;
310
+ inputMapping: Record<string, string>;
311
+ outputVar: string;
312
+ }
313
+ /**
314
+ * Configuration for a user-provided custom handler step.
315
+ *
316
+ * @property type Discriminant for the step config union.
317
+ * @property handler Registered custom handler name.
318
+ * @property params Arbitrary parameters passed to the handler.
319
+ * @property outputVar Optional variable name that receives the handler result.
320
+ */
321
+ interface CustomStepConfig {
322
+ type: 'custom';
323
+ handler: string;
324
+ params: Record<string, unknown>;
325
+ outputVar?: string;
326
+ }
327
+ /**
328
+ * Directed edge between two workflow steps.
329
+ *
330
+ * @property fromStepId Source step identifier.
331
+ * @property toStepId Destination step identifier.
332
+ * @property label Optional edge label for builder displays.
333
+ * @property condition Optional condition label for branching edges.
334
+ */
335
+ interface StepConnection {
336
+ fromStepId: string;
337
+ toStepId: string;
338
+ /** Optional label for the connection */
339
+ label?: string;
340
+ /** Condition for conditional edges */
341
+ condition?: string;
342
+ }
343
+ /**
344
+ * Definition for one declared workflow input or variable.
345
+ *
346
+ * @property name Variable identifier.
347
+ * @property type Primitive or structured variable kind.
348
+ * @property description Optional explanatory text for consumers.
349
+ * @property required Whether the variable must be supplied.
350
+ * @property default Optional default value used when none is supplied.
351
+ */
352
+ interface WorkflowVariable {
353
+ name: string;
354
+ type: 'string' | 'number' | 'boolean' | 'object' | 'array';
355
+ description?: string;
356
+ required?: boolean;
357
+ default?: unknown;
358
+ }
359
+ /**
360
+ * Runtime snapshot for one workflow execution.
361
+ *
362
+ * @property id Stable run identifier.
363
+ * @property workflowId Identifier of the executed workflow.
364
+ * @property workflowVersion Definition version used for this run.
365
+ * @property status Current lifecycle state for the run.
366
+ * @property input Input payload supplied to the execution.
367
+ * @property variables Current workflow variable bag.
368
+ * @property stepResults Per-step execution results accumulated so far.
369
+ * @property currentStepIndex Index of the step currently being executed.
370
+ * @property error Optional terminal error message for failed runs.
371
+ * @property triggeredBy Actor identifier that initiated the run.
372
+ * @property startedAt Unix timestamp when execution began.
373
+ * @property completedAt Optional Unix timestamp when execution finished.
374
+ */
375
+ interface WorkflowRun {
376
+ id: string;
377
+ workflowId: string;
378
+ workflowVersion: number;
379
+ status: RunStatus;
380
+ /** Input variables provided */
381
+ input: Record<string, unknown>;
382
+ /** Current workflow variables state */
383
+ variables: Record<string, unknown>;
384
+ /** Step execution results */
385
+ stepResults: StepResult[];
386
+ /** Current step index */
387
+ currentStepIndex: number;
388
+ /** Error if failed */
389
+ error?: string;
390
+ /** Who triggered the run */
391
+ triggeredBy: string;
392
+ startedAt: number;
393
+ completedAt?: number;
394
+ }
395
+ /**
396
+ * Runtime result captured for one executed workflow step.
397
+ *
398
+ * @property stepId Identifier of the executed step.
399
+ * @property stepName Human-readable step name.
400
+ * @property type Step kind discriminant.
401
+ * @property status Execution status for the step.
402
+ * @property input Optional input variables visible to the step.
403
+ * @property output Optional output produced by the step.
404
+ * @property error Optional error message for failed steps.
405
+ * @property durationMs Step duration in milliseconds.
406
+ * @property tokens Optional token usage for LLM-backed steps.
407
+ * @property costUsd Optional cost metadata for LLM-backed steps.
408
+ * @property startedAt Unix timestamp when the step started.
409
+ * @property completedAt Optional Unix timestamp when the step finished.
410
+ */
411
+ interface StepResult {
412
+ stepId: string;
413
+ stepName: string;
414
+ type: StepType;
415
+ status: StepStatus;
416
+ /** Input variables for this step */
417
+ input?: Record<string, unknown>;
418
+ /** Output from this step */
419
+ output?: unknown;
420
+ /** Error message */
421
+ error?: string;
422
+ /** Duration */
423
+ durationMs: number;
424
+ /** Tokens used (for LLM steps) */
425
+ tokens?: number;
426
+ /** Cost (for LLM steps) */
427
+ costUsd?: number;
428
+ startedAt: number;
429
+ completedAt?: number;
430
+ }
431
+ /**
432
+ * Mutable UI state tracked by builder primitives and hooks.
433
+ *
434
+ * @property definition Current workflow definition being edited.
435
+ * @property selectedStepId Currently selected step identifier.
436
+ * @property isDirty Whether unsaved local edits exist.
437
+ * @property undoStack Historical definitions for undo support.
438
+ * @property redoStack Historical definitions for redo support.
439
+ */
440
+ interface WorkflowBuilderState {
441
+ definition: WorkflowDefinition;
442
+ selectedStepId: string | null;
443
+ isDirty: boolean;
444
+ undoStack: WorkflowDefinition[];
445
+ redoStack: WorkflowDefinition[];
446
+ }
447
+
448
+ /**
449
+ * @module WorkflowEngine
450
+ * @package @geenius/ai-workflow-shared
451
+ * @description Runtime execution engine for workflow definitions. This file
452
+ * walks the declared DAG in execution order, normalizes retry/error handling,
453
+ * and captures run state that every UI variant can render consistently.
454
+ */
455
+
456
+ /**
457
+ * Runtime dependencies injected into the workflow engine.
458
+ *
459
+ * The engine stays framework-agnostic by requiring callers to provide LLM,
460
+ * webhook, approval, and custom-step integrations explicitly.
461
+ */
462
+ interface WorkflowEngineOptions {
463
+ /** LLM call function for llm-call steps */
464
+ callLLM?: (systemPrompt: string, userPrompt: string, model?: string) => Promise<{
465
+ content: string;
466
+ tokens?: number;
467
+ costUsd?: number;
468
+ }>;
469
+ /** Webhook call function */
470
+ callWebhook?: (url: string, method: string, body?: string, headers?: Record<string, string>) => Promise<string>;
471
+ /** Human approval callback */
472
+ onApprovalRequired?: (message: string, stepId: string) => Promise<boolean>;
473
+ /** Step event callback */
474
+ onStepComplete?: (result: StepResult) => void;
475
+ /** Custom step handlers */
476
+ customHandlers?: Record<string, (params: Record<string, unknown>, vars: Record<string, unknown>) => Promise<unknown>>;
477
+ }
478
+ /**
479
+ * Executes workflow definitions against provided runtime integrations.
480
+ */
481
+ declare class WorkflowEngine {
482
+ private options;
483
+ private cancelled;
484
+ constructor(options: WorkflowEngineOptions);
485
+ /**
486
+ * Cancel the in-flight execution loop before the next step begins.
487
+ */
488
+ cancel(): void;
489
+ /**
490
+ * Execute a workflow definition from start to finish and capture the full
491
+ * run state for UI or persistence layers.
492
+ */
493
+ execute(definition: WorkflowDefinition, input?: Record<string, unknown>): Promise<WorkflowRun>;
494
+ /**
495
+ * Execute one step with retry handling and normalized result capture.
496
+ */
497
+ private executeStep;
498
+ /**
499
+ * Run the concrete logic for an individual step type.
500
+ */
501
+ private runStepLogic;
502
+ /** Interpolate `{{variable}}` references in a string. */
503
+ private interpolate;
504
+ private buildExpressionScope;
505
+ /** Topological sort of steps based on declared workflow connections. */
506
+ private topoSort;
507
+ }
508
+
509
+ /**
510
+ * @module WorkflowStepFactories
511
+ * @package @geenius/ai-workflow-shared
512
+ * @description Built-in step factory helpers for constructing shared workflow
513
+ * definitions in a concise and type-safe way.
514
+ */
515
+
516
+ /**
517
+ * Create an LLM call step definition with a generated identifier.
518
+ *
519
+ * @param name Human-readable step name.
520
+ * @param config Step configuration without the discriminant.
521
+ * @returns A workflow step definition ready for insertion into a workflow.
522
+ */
523
+ declare function llmStep(name: string, config: Omit<LLMCallConfig, 'type'>): WorkflowStepDef;
524
+ /**
525
+ * Create a data transform step definition with a generated identifier.
526
+ *
527
+ * @param name Human-readable step name.
528
+ * @param config Step configuration without the discriminant.
529
+ * @returns A workflow step definition ready for insertion into a workflow.
530
+ */
531
+ declare function transformStep(name: string, config: Omit<TransformConfig, 'type'>): WorkflowStepDef;
532
+ /**
533
+ * Create a conditional branch step definition with a generated identifier.
534
+ *
535
+ * @param name Human-readable step name.
536
+ * @param config Step configuration without the discriminant.
537
+ * @returns A workflow step definition ready for insertion into a workflow.
538
+ */
539
+ declare function conditionStep(name: string, config: Omit<ConditionConfig, 'type'>): WorkflowStepDef;
540
+ /**
541
+ * Create a human approval step definition with a generated identifier.
542
+ *
543
+ * @param name Human-readable step name.
544
+ * @param config Step configuration without the discriminant.
545
+ * @returns A workflow step definition ready for insertion into a workflow.
546
+ */
547
+ declare function approvalStep(name: string, config: Omit<HumanApprovalConfig, 'type'>): WorkflowStepDef;
548
+ /**
549
+ * Create a webhook step definition with a generated identifier.
550
+ *
551
+ * @param name Human-readable step name.
552
+ * @param config Step configuration without the discriminant.
553
+ * @returns A workflow step definition ready for insertion into a workflow.
554
+ */
555
+ declare function webhookStep(name: string, config: Omit<WebhookConfig, 'type'>): WorkflowStepDef;
556
+ /**
557
+ * Create a delay step definition with a generated identifier.
558
+ *
559
+ * @param name Human-readable step name.
560
+ * @param durationMs Delay duration in milliseconds.
561
+ * @returns A workflow step definition ready for insertion into a workflow.
562
+ */
563
+ declare function delayStep(name: string, durationMs: number): WorkflowStepDef;
564
+ /**
565
+ * Connect two steps (create a DAG edge).
566
+ *
567
+ * @param fromStepId Source step identifier.
568
+ * @param toStepId Destination step identifier.
569
+ * @param label Optional edge label for builder displays.
570
+ * @returns A directed connection between the two steps.
571
+ */
572
+ declare function connect(fromStepId: string, toStepId: string, label?: string): {
573
+ fromStepId: string;
574
+ toStepId: string;
575
+ label?: string;
576
+ };
577
+ /**
578
+ * Build a linear workflow from an ordered list of steps.
579
+ *
580
+ * @param name Unused descriptive name retained for factory API parity.
581
+ * @param steps Ordered list of workflow steps.
582
+ * @returns The original steps paired with sequential connections.
583
+ */
584
+ declare function linearWorkflow(name: string, steps: WorkflowStepDef[]): {
585
+ steps: WorkflowStepDef[];
586
+ connections: Array<{
587
+ fromStepId: string;
588
+ toStepId: string;
589
+ }>;
590
+ };
591
+
592
+ /**
593
+ * @module WorkflowValidators
594
+ * @package @geenius/ai-workflow-shared
595
+ * @description Zod schemas and validation helpers for workflow definitions,
596
+ * step configs, runtime results, and graph integrity checks.
597
+ */
598
+
599
+ /**
600
+ * Discriminated union covering every supported step configuration schema.
601
+ */
602
+ declare const stepConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
603
+ type: z.ZodLiteral<"llm-call">;
604
+ model: z.ZodOptional<z.ZodString>;
605
+ systemPrompt: z.ZodString;
606
+ userPromptTemplate: z.ZodString;
607
+ temperature: z.ZodOptional<z.ZodNumber>;
608
+ maxTokens: z.ZodOptional<z.ZodNumber>;
609
+ parseJson: z.ZodOptional<z.ZodBoolean>;
610
+ outputVar: z.ZodString;
611
+ }, z.core.$strip>, z.ZodObject<{
612
+ type: z.ZodLiteral<"transform">;
613
+ expression: z.ZodString;
614
+ inputVars: z.ZodArray<z.ZodString>;
615
+ outputVar: z.ZodString;
616
+ }, z.core.$strip>, z.ZodObject<{
617
+ type: z.ZodLiteral<"condition">;
618
+ expression: z.ZodString;
619
+ trueStepId: z.ZodString;
620
+ falseStepId: z.ZodString;
621
+ }, z.core.$strip>, z.ZodObject<{
622
+ type: z.ZodLiteral<"human-approval">;
623
+ message: z.ZodString;
624
+ approvers: z.ZodOptional<z.ZodArray<z.ZodString>>;
625
+ autoApproveAfterMs: z.ZodOptional<z.ZodNumber>;
626
+ }, z.core.$strip>, z.ZodObject<{
627
+ type: z.ZodLiteral<"webhook">;
628
+ url: z.ZodString;
629
+ method: z.ZodEnum<{
630
+ GET: "GET";
631
+ POST: "POST";
632
+ PUT: "PUT";
633
+ DELETE: "DELETE";
634
+ }>;
635
+ headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
636
+ bodyTemplate: z.ZodOptional<z.ZodString>;
637
+ outputVar: z.ZodString;
638
+ }, z.core.$strip>, z.ZodObject<{
639
+ type: z.ZodLiteral<"delay">;
640
+ durationMs: z.ZodNumber;
641
+ }, z.core.$strip>, z.ZodObject<{
642
+ type: z.ZodLiteral<"parallel">;
643
+ stepIds: z.ZodArray<z.ZodString>;
644
+ failurePolicy: z.ZodEnum<{
645
+ "fail-fast": "fail-fast";
646
+ continue: "continue";
647
+ }>;
648
+ }, z.core.$strip>, z.ZodObject<{
649
+ type: z.ZodLiteral<"loop">;
650
+ stepIds: z.ZodArray<z.ZodString>;
651
+ iterateVar: z.ZodString;
652
+ itemVar: z.ZodString;
653
+ maxIterations: z.ZodOptional<z.ZodNumber>;
654
+ }, z.core.$strip>, z.ZodObject<{
655
+ type: z.ZodLiteral<"sub-workflow">;
656
+ workflowId: z.ZodString;
657
+ inputMapping: z.ZodRecord<z.ZodString, z.ZodString>;
658
+ outputVar: z.ZodString;
659
+ }, z.core.$strip>, z.ZodObject<{
660
+ type: z.ZodLiteral<"custom">;
661
+ handler: z.ZodString;
662
+ params: z.ZodRecord<z.ZodString, z.ZodUnknown>;
663
+ outputVar: z.ZodOptional<z.ZodString>;
664
+ }, z.core.$strip>]>;
665
+ /**
666
+ * Zod schema for one workflow step definition.
667
+ */
668
+ declare const workflowStepDefSchema: z.ZodObject<{
669
+ id: z.ZodString;
670
+ name: z.ZodString;
671
+ type: z.ZodEnum<{
672
+ "llm-call": "llm-call";
673
+ transform: "transform";
674
+ condition: "condition";
675
+ "human-approval": "human-approval";
676
+ webhook: "webhook";
677
+ delay: "delay";
678
+ parallel: "parallel";
679
+ loop: "loop";
680
+ "sub-workflow": "sub-workflow";
681
+ custom: "custom";
682
+ }>;
683
+ config: z.ZodDiscriminatedUnion<[z.ZodObject<{
684
+ type: z.ZodLiteral<"llm-call">;
685
+ model: z.ZodOptional<z.ZodString>;
686
+ systemPrompt: z.ZodString;
687
+ userPromptTemplate: z.ZodString;
688
+ temperature: z.ZodOptional<z.ZodNumber>;
689
+ maxTokens: z.ZodOptional<z.ZodNumber>;
690
+ parseJson: z.ZodOptional<z.ZodBoolean>;
691
+ outputVar: z.ZodString;
692
+ }, z.core.$strip>, z.ZodObject<{
693
+ type: z.ZodLiteral<"transform">;
694
+ expression: z.ZodString;
695
+ inputVars: z.ZodArray<z.ZodString>;
696
+ outputVar: z.ZodString;
697
+ }, z.core.$strip>, z.ZodObject<{
698
+ type: z.ZodLiteral<"condition">;
699
+ expression: z.ZodString;
700
+ trueStepId: z.ZodString;
701
+ falseStepId: z.ZodString;
702
+ }, z.core.$strip>, z.ZodObject<{
703
+ type: z.ZodLiteral<"human-approval">;
704
+ message: z.ZodString;
705
+ approvers: z.ZodOptional<z.ZodArray<z.ZodString>>;
706
+ autoApproveAfterMs: z.ZodOptional<z.ZodNumber>;
707
+ }, z.core.$strip>, z.ZodObject<{
708
+ type: z.ZodLiteral<"webhook">;
709
+ url: z.ZodString;
710
+ method: z.ZodEnum<{
711
+ GET: "GET";
712
+ POST: "POST";
713
+ PUT: "PUT";
714
+ DELETE: "DELETE";
715
+ }>;
716
+ headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
717
+ bodyTemplate: z.ZodOptional<z.ZodString>;
718
+ outputVar: z.ZodString;
719
+ }, z.core.$strip>, z.ZodObject<{
720
+ type: z.ZodLiteral<"delay">;
721
+ durationMs: z.ZodNumber;
722
+ }, z.core.$strip>, z.ZodObject<{
723
+ type: z.ZodLiteral<"parallel">;
724
+ stepIds: z.ZodArray<z.ZodString>;
725
+ failurePolicy: z.ZodEnum<{
726
+ "fail-fast": "fail-fast";
727
+ continue: "continue";
728
+ }>;
729
+ }, z.core.$strip>, z.ZodObject<{
730
+ type: z.ZodLiteral<"loop">;
731
+ stepIds: z.ZodArray<z.ZodString>;
732
+ iterateVar: z.ZodString;
733
+ itemVar: z.ZodString;
734
+ maxIterations: z.ZodOptional<z.ZodNumber>;
735
+ }, z.core.$strip>, z.ZodObject<{
736
+ type: z.ZodLiteral<"sub-workflow">;
737
+ workflowId: z.ZodString;
738
+ inputMapping: z.ZodRecord<z.ZodString, z.ZodString>;
739
+ outputVar: z.ZodString;
740
+ }, z.core.$strip>, z.ZodObject<{
741
+ type: z.ZodLiteral<"custom">;
742
+ handler: z.ZodString;
743
+ params: z.ZodRecord<z.ZodString, z.ZodUnknown>;
744
+ outputVar: z.ZodOptional<z.ZodString>;
745
+ }, z.core.$strip>]>;
746
+ position: z.ZodOptional<z.ZodObject<{
747
+ x: z.ZodNumber;
748
+ y: z.ZodNumber;
749
+ }, z.core.$strip>>;
750
+ optional: z.ZodOptional<z.ZodBoolean>;
751
+ retries: z.ZodOptional<z.ZodObject<{
752
+ maxAttempts: z.ZodNumber;
753
+ backoffMs: z.ZodNumber;
754
+ }, z.core.$strip>>;
755
+ }, z.core.$strip>;
756
+ /**
757
+ * Zod schema for directed graph edges between workflow steps.
758
+ */
759
+ declare const stepConnectionSchema: z.ZodObject<{
760
+ fromStepId: z.ZodString;
761
+ toStepId: z.ZodString;
762
+ label: z.ZodOptional<z.ZodString>;
763
+ condition: z.ZodOptional<z.ZodString>;
764
+ }, z.core.$strip>;
765
+ /**
766
+ * Zod schema for declared workflow inputs and variables.
767
+ */
768
+ declare const workflowVariableSchema: z.ZodObject<{
769
+ name: z.ZodString;
770
+ type: z.ZodEnum<{
771
+ string: "string";
772
+ number: "number";
773
+ boolean: "boolean";
774
+ object: "object";
775
+ array: "array";
776
+ }>;
777
+ description: z.ZodOptional<z.ZodString>;
778
+ required: z.ZodOptional<z.ZodBoolean>;
779
+ default: z.ZodOptional<z.ZodUnknown>;
780
+ }, z.core.$strip>;
781
+ /**
782
+ * Zod schema for a persisted workflow definition.
783
+ */
784
+ declare const workflowDefinitionSchema: z.ZodObject<{
785
+ id: z.ZodString;
786
+ name: z.ZodString;
787
+ description: z.ZodOptional<z.ZodString>;
788
+ version: z.ZodNumber;
789
+ status: z.ZodEnum<{
790
+ draft: "draft";
791
+ active: "active";
792
+ paused: "paused";
793
+ archived: "archived";
794
+ }>;
795
+ steps: z.ZodArray<z.ZodObject<{
796
+ id: z.ZodString;
797
+ name: z.ZodString;
798
+ type: z.ZodEnum<{
799
+ "llm-call": "llm-call";
800
+ transform: "transform";
801
+ condition: "condition";
802
+ "human-approval": "human-approval";
803
+ webhook: "webhook";
804
+ delay: "delay";
805
+ parallel: "parallel";
806
+ loop: "loop";
807
+ "sub-workflow": "sub-workflow";
808
+ custom: "custom";
809
+ }>;
810
+ config: z.ZodDiscriminatedUnion<[z.ZodObject<{
811
+ type: z.ZodLiteral<"llm-call">;
812
+ model: z.ZodOptional<z.ZodString>;
813
+ systemPrompt: z.ZodString;
814
+ userPromptTemplate: z.ZodString;
815
+ temperature: z.ZodOptional<z.ZodNumber>;
816
+ maxTokens: z.ZodOptional<z.ZodNumber>;
817
+ parseJson: z.ZodOptional<z.ZodBoolean>;
818
+ outputVar: z.ZodString;
819
+ }, z.core.$strip>, z.ZodObject<{
820
+ type: z.ZodLiteral<"transform">;
821
+ expression: z.ZodString;
822
+ inputVars: z.ZodArray<z.ZodString>;
823
+ outputVar: z.ZodString;
824
+ }, z.core.$strip>, z.ZodObject<{
825
+ type: z.ZodLiteral<"condition">;
826
+ expression: z.ZodString;
827
+ trueStepId: z.ZodString;
828
+ falseStepId: z.ZodString;
829
+ }, z.core.$strip>, z.ZodObject<{
830
+ type: z.ZodLiteral<"human-approval">;
831
+ message: z.ZodString;
832
+ approvers: z.ZodOptional<z.ZodArray<z.ZodString>>;
833
+ autoApproveAfterMs: z.ZodOptional<z.ZodNumber>;
834
+ }, z.core.$strip>, z.ZodObject<{
835
+ type: z.ZodLiteral<"webhook">;
836
+ url: z.ZodString;
837
+ method: z.ZodEnum<{
838
+ GET: "GET";
839
+ POST: "POST";
840
+ PUT: "PUT";
841
+ DELETE: "DELETE";
842
+ }>;
843
+ headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
844
+ bodyTemplate: z.ZodOptional<z.ZodString>;
845
+ outputVar: z.ZodString;
846
+ }, z.core.$strip>, z.ZodObject<{
847
+ type: z.ZodLiteral<"delay">;
848
+ durationMs: z.ZodNumber;
849
+ }, z.core.$strip>, z.ZodObject<{
850
+ type: z.ZodLiteral<"parallel">;
851
+ stepIds: z.ZodArray<z.ZodString>;
852
+ failurePolicy: z.ZodEnum<{
853
+ "fail-fast": "fail-fast";
854
+ continue: "continue";
855
+ }>;
856
+ }, z.core.$strip>, z.ZodObject<{
857
+ type: z.ZodLiteral<"loop">;
858
+ stepIds: z.ZodArray<z.ZodString>;
859
+ iterateVar: z.ZodString;
860
+ itemVar: z.ZodString;
861
+ maxIterations: z.ZodOptional<z.ZodNumber>;
862
+ }, z.core.$strip>, z.ZodObject<{
863
+ type: z.ZodLiteral<"sub-workflow">;
864
+ workflowId: z.ZodString;
865
+ inputMapping: z.ZodRecord<z.ZodString, z.ZodString>;
866
+ outputVar: z.ZodString;
867
+ }, z.core.$strip>, z.ZodObject<{
868
+ type: z.ZodLiteral<"custom">;
869
+ handler: z.ZodString;
870
+ params: z.ZodRecord<z.ZodString, z.ZodUnknown>;
871
+ outputVar: z.ZodOptional<z.ZodString>;
872
+ }, z.core.$strip>]>;
873
+ position: z.ZodOptional<z.ZodObject<{
874
+ x: z.ZodNumber;
875
+ y: z.ZodNumber;
876
+ }, z.core.$strip>>;
877
+ optional: z.ZodOptional<z.ZodBoolean>;
878
+ retries: z.ZodOptional<z.ZodObject<{
879
+ maxAttempts: z.ZodNumber;
880
+ backoffMs: z.ZodNumber;
881
+ }, z.core.$strip>>;
882
+ }, z.core.$strip>>;
883
+ connections: z.ZodArray<z.ZodObject<{
884
+ fromStepId: z.ZodString;
885
+ toStepId: z.ZodString;
886
+ label: z.ZodOptional<z.ZodString>;
887
+ condition: z.ZodOptional<z.ZodString>;
888
+ }, z.core.$strip>>;
889
+ inputSchema: z.ZodOptional<z.ZodArray<z.ZodObject<{
890
+ name: z.ZodString;
891
+ type: z.ZodEnum<{
892
+ string: "string";
893
+ number: "number";
894
+ boolean: "boolean";
895
+ object: "object";
896
+ array: "array";
897
+ }>;
898
+ description: z.ZodOptional<z.ZodString>;
899
+ required: z.ZodOptional<z.ZodBoolean>;
900
+ default: z.ZodOptional<z.ZodUnknown>;
901
+ }, z.core.$strip>>>;
902
+ variables: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
903
+ tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
904
+ createdBy: z.ZodString;
905
+ createdAt: z.ZodNumber;
906
+ updatedAt: z.ZodNumber;
907
+ }, z.core.$strip>;
908
+ /**
909
+ * Zod schema for one captured step result inside a workflow run.
910
+ */
911
+ declare const stepResultSchema: z.ZodObject<{
912
+ stepId: z.ZodString;
913
+ stepName: z.ZodString;
914
+ type: z.ZodEnum<{
915
+ "llm-call": "llm-call";
916
+ transform: "transform";
917
+ condition: "condition";
918
+ "human-approval": "human-approval";
919
+ webhook: "webhook";
920
+ delay: "delay";
921
+ parallel: "parallel";
922
+ loop: "loop";
923
+ "sub-workflow": "sub-workflow";
924
+ custom: "custom";
925
+ }>;
926
+ status: z.ZodEnum<{
927
+ pending: "pending";
928
+ running: "running";
929
+ completed: "completed";
930
+ failed: "failed";
931
+ skipped: "skipped";
932
+ "waiting-approval": "waiting-approval";
933
+ }>;
934
+ input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
935
+ output: z.ZodOptional<z.ZodUnknown>;
936
+ error: z.ZodOptional<z.ZodString>;
937
+ durationMs: z.ZodNumber;
938
+ tokens: z.ZodOptional<z.ZodNumber>;
939
+ costUsd: z.ZodOptional<z.ZodNumber>;
940
+ startedAt: z.ZodNumber;
941
+ completedAt: z.ZodOptional<z.ZodNumber>;
942
+ }, z.core.$strip>;
943
+ /**
944
+ * Zod schema for a full workflow execution run snapshot.
945
+ */
946
+ declare const workflowRunSchema: z.ZodObject<{
947
+ id: z.ZodString;
948
+ workflowId: z.ZodString;
949
+ workflowVersion: z.ZodNumber;
950
+ status: z.ZodEnum<{
951
+ paused: "paused";
952
+ pending: "pending";
953
+ running: "running";
954
+ completed: "completed";
955
+ failed: "failed";
956
+ cancelled: "cancelled";
957
+ }>;
958
+ input: z.ZodRecord<z.ZodString, z.ZodUnknown>;
959
+ variables: z.ZodRecord<z.ZodString, z.ZodUnknown>;
960
+ stepResults: z.ZodArray<z.ZodObject<{
961
+ stepId: z.ZodString;
962
+ stepName: z.ZodString;
963
+ type: z.ZodEnum<{
964
+ "llm-call": "llm-call";
965
+ transform: "transform";
966
+ condition: "condition";
967
+ "human-approval": "human-approval";
968
+ webhook: "webhook";
969
+ delay: "delay";
970
+ parallel: "parallel";
971
+ loop: "loop";
972
+ "sub-workflow": "sub-workflow";
973
+ custom: "custom";
974
+ }>;
975
+ status: z.ZodEnum<{
976
+ pending: "pending";
977
+ running: "running";
978
+ completed: "completed";
979
+ failed: "failed";
980
+ skipped: "skipped";
981
+ "waiting-approval": "waiting-approval";
982
+ }>;
983
+ input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
984
+ output: z.ZodOptional<z.ZodUnknown>;
985
+ error: z.ZodOptional<z.ZodString>;
986
+ durationMs: z.ZodNumber;
987
+ tokens: z.ZodOptional<z.ZodNumber>;
988
+ costUsd: z.ZodOptional<z.ZodNumber>;
989
+ startedAt: z.ZodNumber;
990
+ completedAt: z.ZodOptional<z.ZodNumber>;
991
+ }, z.core.$strip>>;
992
+ currentStepIndex: z.ZodNumber;
993
+ error: z.ZodOptional<z.ZodString>;
994
+ triggeredBy: z.ZodString;
995
+ startedAt: z.ZodNumber;
996
+ completedAt: z.ZodOptional<z.ZodNumber>;
997
+ }, z.core.$strip>;
998
+ /**
999
+ * Validate an unknown payload as a workflow definition.
1000
+ *
1001
+ * @param data Unknown payload to validate.
1002
+ * @returns A safe-parse result containing the validated definition or errors.
1003
+ */
1004
+ declare function validateWorkflow(data: unknown): z.ZodSafeParseResult<z.infer<typeof workflowDefinitionSchema>>;
1005
+ /**
1006
+ * Validate an unknown payload as a workflow run snapshot.
1007
+ *
1008
+ * @param data Unknown payload to validate.
1009
+ * @returns A safe-parse result containing the validated run or errors.
1010
+ */
1011
+ declare function validateRun(data: unknown): z.ZodSafeParseResult<z.infer<typeof workflowRunSchema>>;
1012
+ /**
1013
+ * Validate that all connections reference known step identifiers.
1014
+ *
1015
+ * @param steps Declared workflow steps.
1016
+ * @param connections Directed edges between those steps.
1017
+ * @returns A list of integrity errors, empty when every reference is valid.
1018
+ */
1019
+ declare function validateConnectionIntegrity(steps: Array<{
1020
+ id: string;
1021
+ }>, connections: Array<{
1022
+ fromStepId: string;
1023
+ toStepId: string;
1024
+ }>): string[];
1025
+
1026
+ /**
1027
+ * @module WorkflowErrors
1028
+ * @package @geenius/ai-workflow-shared
1029
+ * @description Package-specific typed error classes for workflow configuration,
1030
+ * execution, graph validation, and expression evaluation. These errors extend
1031
+ * the shared Geenius error base so consumers can discriminate failures safely.
1032
+ */
1033
+
1034
+ /**
1035
+ * Shared context payload for workflow failures.
1036
+ *
1037
+ * @property stepId Optional workflow step identifier related to the failure.
1038
+ * @property workflowId Optional workflow identifier related to the failure.
1039
+ * @property expression Optional expression string that failed to parse or evaluate.
1040
+ * @property details Arbitrary structured metadata that helps consumers debug failures.
1041
+ */
1042
+ interface WorkflowErrorContext {
1043
+ [key: string]: unknown;
1044
+ stepId?: string;
1045
+ workflowId?: string;
1046
+ expression?: string;
1047
+ details?: Record<string, unknown>;
1048
+ }
1049
+ /**
1050
+ * Base typed error for the AI workflow package.
1051
+ */
1052
+ declare class WorkflowError extends GeeniusError {
1053
+ /**
1054
+ * Create a structured workflow error.
1055
+ *
1056
+ * @param options Error metadata, message, and optional structured context.
1057
+ * @returns A typed workflow error instance.
1058
+ */
1059
+ constructor(options: {
1060
+ code?: ErrorCode;
1061
+ message: string;
1062
+ cause?: unknown;
1063
+ context?: WorkflowErrorContext;
1064
+ });
1065
+ }
1066
+ /**
1067
+ * Error raised when workflow configuration is invalid or incomplete.
1068
+ */
1069
+ declare class WorkflowConfigurationError extends WorkflowError {
1070
+ /**
1071
+ * Create a configuration error for invalid workflow setup.
1072
+ *
1073
+ * @param message Human-readable explanation of the invalid configuration.
1074
+ * @param context Optional structured metadata describing the failing config area.
1075
+ * @returns A typed workflow configuration error.
1076
+ */
1077
+ constructor(message: string, context?: WorkflowErrorContext);
1078
+ }
1079
+ /**
1080
+ * Error raised when workflow execution fails at runtime.
1081
+ */
1082
+ declare class WorkflowExecutionError extends WorkflowError {
1083
+ /**
1084
+ * Create an execution error for a workflow runtime failure.
1085
+ *
1086
+ * @param message Human-readable explanation of the runtime failure.
1087
+ * @param context Optional structured metadata describing the failing execution path.
1088
+ * @param cause Optional underlying exception that triggered the runtime failure.
1089
+ * @returns A typed workflow execution error.
1090
+ */
1091
+ constructor(message: string, context?: WorkflowErrorContext, cause?: unknown);
1092
+ }
1093
+ /**
1094
+ * Error raised when a workflow DAG is invalid.
1095
+ */
1096
+ declare class WorkflowGraphError extends WorkflowError {
1097
+ /**
1098
+ * Create a graph-validation error for an invalid workflow topology.
1099
+ *
1100
+ * @param message Human-readable explanation of the graph validation failure.
1101
+ * @param context Optional structured metadata describing the invalid topology.
1102
+ * @returns A typed workflow graph error.
1103
+ */
1104
+ constructor(message: string, context?: WorkflowErrorContext);
1105
+ }
1106
+ /**
1107
+ * Error raised when workflow expressions fail to parse or evaluate safely.
1108
+ */
1109
+ declare class WorkflowExpressionError extends WorkflowError {
1110
+ /**
1111
+ * Create an expression error for a failed transform or condition expression.
1112
+ *
1113
+ * @param message Human-readable explanation of the expression failure.
1114
+ * @param context Optional structured metadata including the source expression.
1115
+ * @param cause Optional underlying parse or runtime error.
1116
+ * @returns A typed workflow expression error.
1117
+ */
1118
+ constructor(message: string, context?: WorkflowErrorContext, cause?: unknown);
1119
+ }
1120
+ /**
1121
+ * Normalize unknown thrown values into an exported workflow error type.
1122
+ *
1123
+ * @param error Unknown thrown value from workflow code.
1124
+ * @param fallbackMessage Message used when the original value is not informative.
1125
+ * @param context Optional structured context describing the failing workflow area.
1126
+ * @returns A typed workflow error instance suitable for rethrowing or storing.
1127
+ */
1128
+ declare function normalizeWorkflowError(error: unknown, fallbackMessage: string, context?: WorkflowErrorContext): WorkflowError;
1129
+
1130
+ /**
1131
+ * @module WorkflowTemplates
1132
+ * @package @geenius/ai-workflow-shared
1133
+ * @description Curated workflow templates for common AI and business patterns.
1134
+ * These templates seed the builder UI and provide canonical examples of the
1135
+ * shared workflow definition contract.
1136
+ */
1137
+
1138
+ /**
1139
+ * Template metadata and factory for one reusable workflow starter.
1140
+ *
1141
+ * @property id Stable template identifier.
1142
+ * @property name Human-readable template name.
1143
+ * @property description Summary of the workflow pattern.
1144
+ * @property category Top-level template grouping used in browsers.
1145
+ * @property tags Searchable tags for discovery.
1146
+ * @property create Factory that returns a fresh workflow definition instance.
1147
+ */
1148
+ interface WorkflowTemplate {
1149
+ id: string;
1150
+ name: string;
1151
+ description: string;
1152
+ category: 'content' | 'data' | 'approval' | 'research' | 'devops';
1153
+ tags: string[];
1154
+ /** Factory that creates a fresh WorkflowDefinition from this template */
1155
+ create: (createdBy: string) => WorkflowDefinition;
1156
+ }
1157
+ /**
1158
+ * Canonical template catalog shipped with the package.
1159
+ */
1160
+ declare const WORKFLOW_TEMPLATES: WorkflowTemplate[];
1161
+ /**
1162
+ * Look up one workflow template by its stable identifier.
1163
+ *
1164
+ * @param id Template identifier to resolve.
1165
+ * @returns The matching workflow template when found.
1166
+ */
1167
+ declare function getTemplate(id: string): WorkflowTemplate | undefined;
1168
+ /**
1169
+ * Return every workflow template that belongs to one category.
1170
+ *
1171
+ * @param category Template category to filter by.
1172
+ * @returns Templates whose category matches the requested value.
1173
+ */
1174
+ declare function getTemplatesByCategory(category: WorkflowTemplate['category']): WorkflowTemplate[];
1175
+
1176
+ /**
1177
+ * @module WorkflowInterpolationUtils
1178
+ * @package @geenius/ai-workflow-shared
1179
+ * @description Template-variable interpolation helpers for workflow prompts,
1180
+ * webhook bodies, and approval copy. These functions resolve dot paths and
1181
+ * replace `{{variable}}` markers using the workflow runtime context.
1182
+ */
1183
+ /**
1184
+ * Interpolate {{variable}} or {{nested.path}} references in a string.
1185
+ * Supports optional default values: {{varName|default}}.
1186
+ */
1187
+ declare function interpolate(template: string, vars: Record<string, unknown>): string;
1188
+ /**
1189
+ * Extract all variable references from a template string.
1190
+ * Returns unique variable names (without defaults).
1191
+ */
1192
+ declare function extractVariables(template: string): string[];
1193
+ /**
1194
+ * Check if all required variables in a template are present in the vars object.
1195
+ * Returns missing variable names.
1196
+ */
1197
+ declare function findMissingVariables(template: string, vars: Record<string, unknown>): string[];
1198
+
1199
+ /**
1200
+ * @module WorkflowExpressionUtils
1201
+ * @package @geenius/ai-workflow-shared
1202
+ * @description Safe expression helpers for transform and condition steps. This
1203
+ * file implements the parser and evaluator used by the workflow engine to
1204
+ * support a constrained JavaScript-like expression language without `eval`.
1205
+ */
1206
+ /**
1207
+ * Evaluate a workflow expression against the provided variables.
1208
+ */
1209
+ declare function evaluateExpression(expression: string, context: Record<string, unknown>): unknown;
1210
+
1211
+ /**
1212
+ * @module WorkflowDagUtils
1213
+ * @package @geenius/ai-workflow-shared
1214
+ * @description Directed-acyclic-graph helpers used by the workflow engine and
1215
+ * validators. These utilities normalize graph traversal, cycle detection, and
1216
+ * entry/exit analysis across every framework variant.
1217
+ */
1218
+ /**
1219
+ * Minimal graph node shape accepted by DAG helpers.
1220
+ */
1221
+ interface DAGNode {
1222
+ id: string;
1223
+ }
1224
+ /**
1225
+ * Minimal graph edge shape accepted by DAG helpers.
1226
+ */
1227
+ interface DAGEdge {
1228
+ from: string;
1229
+ to: string;
1230
+ }
1231
+ /**
1232
+ * Topological sort using Kahn's algorithm.
1233
+ * Returns sorted node IDs or throws if a cycle is detected.
1234
+ */
1235
+ declare function topoSort(nodeIds: string[], edges: DAGEdge[]): string[];
1236
+ /**
1237
+ * Detect cycles using DFS. Returns the cycle path if one exists, or null.
1238
+ */
1239
+ declare function detectCycle(nodeIds: string[], edges: DAGEdge[]): string[] | null;
1240
+ /**
1241
+ * Find all paths from source to target in the DAG.
1242
+ */
1243
+ declare function findPaths(nodeIds: string[], edges: DAGEdge[], source: string, target: string): string[][];
1244
+ /**
1245
+ * Find entry nodes (no incoming edges) in the DAG.
1246
+ */
1247
+ declare function findEntryNodes(nodeIds: string[], edges: DAGEdge[]): string[];
1248
+ /**
1249
+ * Find exit nodes (no outgoing edges) in the DAG.
1250
+ */
1251
+ declare function findExitNodes(nodeIds: string[], edges: DAGEdge[]): string[];
1252
+
1253
+ /**
1254
+ * @module WorkflowCostUtils
1255
+ * @package @geenius/ai-workflow-shared
1256
+ * @description Cost and token estimation helpers for workflow definitions and
1257
+ * completed runs. These utilities keep LLM spend reporting consistent across
1258
+ * the builder, run panel, and Storybook surfaces.
1259
+ */
1260
+
1261
+ /** Cost-per-token rates by model (USD per 1K tokens) */
1262
+ declare const MODEL_RATES: Record<string, {
1263
+ input: number;
1264
+ output: number;
1265
+ }>;
1266
+ /** Rough token estimation (1 token ≈ 4 chars for English text) */
1267
+ declare function estimateTokens(text: string): number;
1268
+ /** Estimate the token count for an LLM step */
1269
+ declare function estimateStepTokens(config: LLMCallConfig, vars?: Record<string, unknown>): {
1270
+ inputTokens: number;
1271
+ outputTokens: number;
1272
+ };
1273
+ /** Estimate cost for a single step */
1274
+ declare function estimateStepCost(config: LLMCallConfig, vars?: Record<string, unknown>): number;
1275
+ /** Estimate total cost for an entire workflow definition */
1276
+ declare function estimateWorkflowCost(definition: WorkflowDefinition, vars?: Record<string, unknown>): {
1277
+ totalCostUsd: number;
1278
+ totalTokens: number;
1279
+ stepEstimates: Array<{
1280
+ stepId: string;
1281
+ stepName: string;
1282
+ costUsd: number;
1283
+ tokens: number;
1284
+ }>;
1285
+ };
1286
+ /** Calculate actual cost from completed step results */
1287
+ declare function calculateRunCost(results: StepResult[]): {
1288
+ totalCostUsd: number;
1289
+ totalTokens: number;
1290
+ };
1291
+ /** Format cost as a human-readable string */
1292
+ declare function formatCost(usd: number): string;
1293
+
1294
+ /**
1295
+ * @module WorkflowConvexSchemaFields
1296
+ * @package @geenius/ai-workflow-shared
1297
+ * @description Typed Convex-style schema manifests for the workflow package.
1298
+ * These structured objects preserve field shape so consumers can persist and
1299
+ * query workflow payloads without losing typed storage metadata.
1300
+ */
1301
+ type LiteralField<T extends string | number | boolean> = {
1302
+ type: 'literal';
1303
+ value: T;
1304
+ optional?: true;
1305
+ };
1306
+ type StringField = {
1307
+ type: 'string';
1308
+ optional?: true;
1309
+ };
1310
+ type NumberField = {
1311
+ type: 'number';
1312
+ optional?: true;
1313
+ };
1314
+ type BooleanField = {
1315
+ type: 'boolean';
1316
+ optional?: true;
1317
+ };
1318
+ type UnknownField = {
1319
+ type: 'any';
1320
+ optional?: true;
1321
+ };
1322
+ type ArrayField = {
1323
+ type: 'array';
1324
+ items: SchemaField;
1325
+ optional?: true;
1326
+ };
1327
+ type RecordField = {
1328
+ type: 'record';
1329
+ key: SchemaField;
1330
+ value: SchemaField;
1331
+ optional?: true;
1332
+ };
1333
+ type ObjectField = {
1334
+ type: 'object';
1335
+ fields: Record<string, SchemaField>;
1336
+ optional?: true;
1337
+ };
1338
+ type UnionField = {
1339
+ type: 'union';
1340
+ values: SchemaField[];
1341
+ optional?: true;
1342
+ };
1343
+ /**
1344
+ * Typed schema field descriptor used to describe Convex document shapes.
1345
+ */
1346
+ type SchemaField = LiteralField<string | number | boolean> | StringField | NumberField | BooleanField | UnknownField | ArrayField | RecordField | ObjectField | UnionField;
1347
+ /**
1348
+ * Manifest describing persisted workflow definition documents.
1349
+ */
1350
+ declare const workflowsTable: ObjectField;
1351
+ /**
1352
+ * Manifest describing persisted workflow execution run documents.
1353
+ */
1354
+ declare const workflowRunsTable: ObjectField;
1355
+ /**
1356
+ * Manifest describing persisted per-step execution result documents.
1357
+ */
1358
+ declare const workflowStepResultsTable: ObjectField;
1359
+ /**
1360
+ * Named collection of all workflow table manifests for consumer schema merges.
1361
+ */
1362
+ declare const workflowTables: {
1363
+ workflows: ObjectField;
1364
+ workflowRuns: ObjectField;
1365
+ workflowStepResults: ObjectField;
1366
+ };
1367
+
1368
+ export { type ConditionConfig, type ConfigureWorkflowOptions, type CustomStepConfig, type DAGEdge, type DAGNode, type DelayConfig, type HumanApprovalConfig, type LLMCallConfig, type LoopConfig, MODEL_RATES, type ParallelConfig, type RunStatus, type StepConfig, type StepConnection, type StepResult, type StepStatus, type StepType, type SubWorkflowConfig, type TransformConfig, WORKFLOW_TEMPLATES, type WebhookConfig, type WorkflowBuilderState, WorkflowConfigurationError, type WorkflowDefinition, WorkflowEngine, type WorkflowEngineOptions, WorkflowError, type WorkflowErrorContext, WorkflowExecutionError, WorkflowExpressionError, WorkflowGraphError, type WorkflowRun, type WorkflowStatus, type WorkflowStepDef, type WorkflowTemplate, type WorkflowVariable, approvalStep, calculateRunCost, conditionStep, configureWorkflow, connect, defaultWorkflowConfig, delayStep, detectCycle, estimateStepCost, estimateStepTokens, estimateTokens, estimateWorkflowCost, evaluateExpression, extractVariables, findEntryNodes, findExitNodes, findMissingVariables, findPaths, formatCost, getTemplate, getTemplatesByCategory, interpolate, linearWorkflow, llmStep, normalizeWorkflowError, stepConfigSchema, stepConnectionSchema, stepResultSchema, topoSort, transformStep, validateConnectionIntegrity, validateRun, validateWorkflow, webhookStep, workflowDefinitionSchema, workflowRunSchema, workflowRunsTable, workflowStepDefSchema, workflowStepResultsTable, workflowTables, workflowVariableSchema, workflowsTable };