yaml-flow 1.0.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.
@@ -0,0 +1,237 @@
1
+ /**
2
+ * yaml-flow - Core Types
3
+ *
4
+ * All type definitions for the workflow engine.
5
+ */
6
+ /**
7
+ * Root flow configuration - maps to YAML file structure
8
+ */
9
+ interface FlowConfig {
10
+ /** Optional flow identifier */
11
+ id?: string;
12
+ /** Flow settings */
13
+ settings: FlowSettings;
14
+ /** Step definitions */
15
+ steps: Record<string, StepConfig>;
16
+ /** Terminal state definitions */
17
+ terminal_states: Record<string, TerminalStateConfig>;
18
+ }
19
+ /**
20
+ * Flow-level settings
21
+ */
22
+ interface FlowSettings {
23
+ /** Step to start execution from */
24
+ start_step: string;
25
+ /** Maximum steps before forced termination (default: 100) */
26
+ max_total_steps?: number;
27
+ /** Flow timeout in milliseconds (optional) */
28
+ timeout_ms?: number;
29
+ }
30
+ /**
31
+ * Individual step configuration
32
+ */
33
+ interface StepConfig {
34
+ /** Human-readable description */
35
+ description?: string;
36
+ /** Data keys this step expects as input */
37
+ expects_data?: string[];
38
+ /** Data keys this step produces as output */
39
+ produces_data?: string[];
40
+ /** Transition mapping: result -> next step name */
41
+ transitions: Record<string, string>;
42
+ /** Retry configuration for failures */
43
+ retry?: RetryConfig;
44
+ /** Circuit breaker for loops */
45
+ circuit_breaker?: CircuitBreakerConfig;
46
+ }
47
+ /**
48
+ * Retry configuration for step failures
49
+ */
50
+ interface RetryConfig {
51
+ /** Maximum retry attempts */
52
+ max_attempts: number;
53
+ /** Delay between retries in ms */
54
+ delay_ms?: number;
55
+ /** Backoff multiplier (e.g., 2 for exponential) */
56
+ backoff_multiplier?: number;
57
+ }
58
+ /**
59
+ * Circuit breaker configuration
60
+ */
61
+ interface CircuitBreakerConfig {
62
+ /** Maximum iterations before circuit opens */
63
+ max_iterations: number;
64
+ /** Step to transition to when circuit opens */
65
+ on_open: string;
66
+ }
67
+ /**
68
+ * Terminal state configuration
69
+ */
70
+ interface TerminalStateConfig {
71
+ /** Human-readable description */
72
+ description?: string;
73
+ /** Intent/status to return (e.g., 'success', 'error', 'cancelled') */
74
+ return_intent: string;
75
+ /** Data key(s) to include in result, or false to exclude */
76
+ return_artifacts?: string | string[] | false;
77
+ /** Data keys this terminal state expects */
78
+ expects_data?: string[];
79
+ }
80
+ /**
81
+ * Step handler function signature
82
+ */
83
+ type StepHandler = (input: StepInput, context: StepContext) => StepResult | Promise<StepResult>;
84
+ /**
85
+ * Input passed to step handlers
86
+ */
87
+ interface StepInput {
88
+ /** Data from previous steps based on expects_data */
89
+ [key: string]: unknown;
90
+ }
91
+ /**
92
+ * Context available to step handlers
93
+ */
94
+ interface StepContext {
95
+ /** Run identifier */
96
+ runId: string;
97
+ /** Current step name */
98
+ stepName: string;
99
+ /** Injected components (DB, API clients, etc.) */
100
+ components: Record<string, unknown>;
101
+ /** Store instance for direct access if needed */
102
+ store: FlowStore;
103
+ /** Abort signal for cancellation */
104
+ signal?: AbortSignal;
105
+ /** Emit events for UI updates */
106
+ emit: (event: string, data: unknown) => void;
107
+ }
108
+ /**
109
+ * Result returned from step handlers
110
+ */
111
+ interface StepResult {
112
+ /** Result key for transition routing (e.g., 'success', 'failure', 'retry') */
113
+ result: string;
114
+ /** Data to merge into flow state (must match produces_data) */
115
+ data?: Record<string, unknown>;
116
+ }
117
+ /**
118
+ * Engine configuration options
119
+ */
120
+ interface EngineOptions {
121
+ /** Persistence store (default: MemoryStore) */
122
+ store?: FlowStore;
123
+ /** Injected components available to handlers */
124
+ components?: Record<string, unknown>;
125
+ /** Abort signal for cancellation */
126
+ signal?: AbortSignal;
127
+ /** Callback on each step execution */
128
+ onStep?: (stepName: string, result: StepResult) => void;
129
+ /** Callback on step transition */
130
+ onTransition?: (from: string, to: string) => void;
131
+ /** Callback on flow completion */
132
+ onComplete?: (result: FlowResult) => void;
133
+ /** Callback on flow error */
134
+ onError?: (error: Error) => void;
135
+ }
136
+ /**
137
+ * Final result of flow execution
138
+ */
139
+ interface FlowResult {
140
+ /** Run identifier */
141
+ runId: string;
142
+ /** Completion status */
143
+ status: 'completed' | 'failed' | 'cancelled' | 'timeout' | 'max_iterations';
144
+ /** Return intent from terminal state */
145
+ intent?: string;
146
+ /** Returned artifacts/data */
147
+ data: Record<string, unknown>;
148
+ /** Final step name */
149
+ finalStep: string;
150
+ /** Steps executed (in order) */
151
+ stepHistory: string[];
152
+ /** Total execution time in ms */
153
+ durationMs: number;
154
+ /** Error if failed */
155
+ error?: Error;
156
+ }
157
+ /**
158
+ * Pluggable store interface for persistence
159
+ */
160
+ interface FlowStore {
161
+ /**
162
+ * Save run state
163
+ */
164
+ saveRunState(runId: string, state: RunState): Promise<void>;
165
+ /**
166
+ * Load run state
167
+ */
168
+ loadRunState(runId: string): Promise<RunState | null>;
169
+ /**
170
+ * Delete run state
171
+ */
172
+ deleteRunState(runId: string): Promise<void>;
173
+ /**
174
+ * Set a data value for a run
175
+ */
176
+ setData(runId: string, key: string, value: unknown): Promise<void>;
177
+ /**
178
+ * Get a data value for a run
179
+ */
180
+ getData(runId: string, key: string): Promise<unknown>;
181
+ /**
182
+ * Get all data for a run
183
+ */
184
+ getAllData(runId: string): Promise<Record<string, unknown>>;
185
+ /**
186
+ * Clear all data for a run
187
+ */
188
+ clearData(runId: string): Promise<void>;
189
+ /**
190
+ * List all active run IDs (optional - for management)
191
+ */
192
+ listRuns?(): Promise<string[]>;
193
+ }
194
+ /**
195
+ * Persisted run state
196
+ */
197
+ interface RunState {
198
+ /** Run identifier */
199
+ runId: string;
200
+ /** Flow identifier */
201
+ flowId: string;
202
+ /** Current step name */
203
+ currentStep: string;
204
+ /** Execution status */
205
+ status: 'running' | 'paused' | 'completed' | 'failed' | 'cancelled';
206
+ /** Ordered list of executed steps */
207
+ stepHistory: string[];
208
+ /** Iteration count per step (for circuit breakers) */
209
+ iterationCounts: Record<string, number>;
210
+ /** Retry counts per step */
211
+ retryCounts: Record<string, number>;
212
+ /** Timestamp when run started (ms since epoch) */
213
+ startedAt: number;
214
+ /** Timestamp of last update (ms since epoch) */
215
+ updatedAt: number;
216
+ /** Timestamp when paused (if applicable) */
217
+ pausedAt?: number;
218
+ }
219
+ /**
220
+ * Event types emitted by the engine
221
+ */
222
+ type FlowEventType = 'step:start' | 'step:complete' | 'step:error' | 'transition' | 'flow:start' | 'flow:complete' | 'flow:error' | 'flow:paused' | 'flow:resumed';
223
+ /**
224
+ * Event payload structure
225
+ */
226
+ interface FlowEvent {
227
+ type: FlowEventType;
228
+ runId: string;
229
+ timestamp: number;
230
+ data: Record<string, unknown>;
231
+ }
232
+ /**
233
+ * Event listener function
234
+ */
235
+ type FlowEventListener = (event: FlowEvent) => void;
236
+
237
+ export type { CircuitBreakerConfig as C, EngineOptions as E, FlowConfig as F, RetryConfig as R, StepConfig as S, TerminalStateConfig as T, FlowEvent as a, FlowEventListener as b, FlowEventType as c, FlowResult as d, FlowSettings as e, FlowStore as f, RunState as g, StepContext as h, StepHandler as i, StepInput as j, StepResult as k };
package/package.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "yaml-flow",
3
+ "version": "1.0.0",
4
+ "description": "Isomorphic workflow engine with declarative YAML flows, pluggable storage, and pure function handlers",
5
+ "author": "",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
+ },
17
+ "./core": {
18
+ "types": "./dist/core/index.d.ts",
19
+ "import": "./dist/core/index.js",
20
+ "require": "./dist/core/index.cjs"
21
+ },
22
+ "./stores/memory": {
23
+ "types": "./dist/stores/memory.d.ts",
24
+ "import": "./dist/stores/memory.js",
25
+ "require": "./dist/stores/memory.cjs"
26
+ },
27
+ "./stores/localStorage": {
28
+ "types": "./dist/stores/localStorage.d.ts",
29
+ "import": "./dist/stores/localStorage.js",
30
+ "require": "./dist/stores/localStorage.cjs"
31
+ },
32
+ "./stores/file": {
33
+ "types": "./dist/stores/file.d.ts",
34
+ "import": "./dist/stores/file.js",
35
+ "require": "./dist/stores/file.cjs"
36
+ }
37
+ },
38
+ "browser": {
39
+ "./stores/file": false
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "schema"
44
+ ],
45
+ "scripts": {
46
+ "build": "tsup",
47
+ "dev": "tsup --watch",
48
+ "test": "vitest",
49
+ "test:run": "vitest run",
50
+ "lint": "eslint src/",
51
+ "typecheck": "tsc --noEmit",
52
+ "prepublishOnly": "npm run build"
53
+ },
54
+ "devDependencies": {
55
+ "@types/node": "^20.10.0",
56
+ "tsup": "^8.0.0",
57
+ "typescript": "^5.3.0",
58
+ "vitest": "^1.0.0"
59
+ },
60
+ "dependencies": {
61
+ "yaml": "^2.3.4"
62
+ },
63
+ "optionalDependencies": {
64
+ "ajv": "^8.12.0"
65
+ },
66
+ "keywords": [
67
+ "workflow",
68
+ "state-machine",
69
+ "yaml",
70
+ "flow",
71
+ "isomorphic",
72
+ "browser",
73
+ "nodejs",
74
+ "declarative"
75
+ ],
76
+ "repository": {
77
+ "type": "git",
78
+ "url": "https://github.com/nsreehari/yaml-flow.git"
79
+ },
80
+ "engines": {
81
+ "node": ">=18"
82
+ }
83
+ }
@@ -0,0 +1,159 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://github.com/yaml-flow/schema/flow.json",
4
+ "title": "YamlFlow Configuration",
5
+ "description": "Schema for yaml-flow workflow definitions",
6
+ "type": "object",
7
+ "required": ["settings", "steps", "terminal_states"],
8
+ "properties": {
9
+ "id": {
10
+ "type": "string",
11
+ "description": "Optional flow identifier"
12
+ },
13
+ "settings": {
14
+ "type": "object",
15
+ "description": "Flow-level settings",
16
+ "required": ["start_step"],
17
+ "properties": {
18
+ "start_step": {
19
+ "type": "string",
20
+ "description": "Step to start execution from"
21
+ },
22
+ "max_total_steps": {
23
+ "type": "integer",
24
+ "minimum": 1,
25
+ "default": 100,
26
+ "description": "Maximum steps before forced termination"
27
+ },
28
+ "timeout_ms": {
29
+ "type": "integer",
30
+ "minimum": 0,
31
+ "description": "Flow timeout in milliseconds"
32
+ }
33
+ },
34
+ "additionalProperties": false
35
+ },
36
+ "steps": {
37
+ "type": "object",
38
+ "description": "Step definitions",
39
+ "minProperties": 1,
40
+ "additionalProperties": {
41
+ "$ref": "#/definitions/step"
42
+ }
43
+ },
44
+ "terminal_states": {
45
+ "type": "object",
46
+ "description": "Terminal state definitions",
47
+ "minProperties": 1,
48
+ "additionalProperties": {
49
+ "$ref": "#/definitions/terminal_state"
50
+ }
51
+ }
52
+ },
53
+ "additionalProperties": false,
54
+ "definitions": {
55
+ "step": {
56
+ "type": "object",
57
+ "description": "Individual step configuration",
58
+ "required": ["transitions"],
59
+ "properties": {
60
+ "description": {
61
+ "type": "string",
62
+ "description": "Human-readable description"
63
+ },
64
+ "expects_data": {
65
+ "type": "array",
66
+ "items": { "type": "string" },
67
+ "description": "Data keys this step expects as input"
68
+ },
69
+ "produces_data": {
70
+ "type": "array",
71
+ "items": { "type": "string" },
72
+ "description": "Data keys this step produces as output"
73
+ },
74
+ "transitions": {
75
+ "type": "object",
76
+ "description": "Mapping of result -> next step name",
77
+ "additionalProperties": { "type": "string" },
78
+ "minProperties": 1
79
+ },
80
+ "retry": {
81
+ "$ref": "#/definitions/retry_config"
82
+ },
83
+ "circuit_breaker": {
84
+ "$ref": "#/definitions/circuit_breaker_config"
85
+ }
86
+ },
87
+ "additionalProperties": false
88
+ },
89
+ "terminal_state": {
90
+ "type": "object",
91
+ "description": "Terminal state configuration",
92
+ "required": ["return_intent"],
93
+ "properties": {
94
+ "description": {
95
+ "type": "string",
96
+ "description": "Human-readable description"
97
+ },
98
+ "return_intent": {
99
+ "type": "string",
100
+ "description": "Intent/status to return (e.g., 'success', 'error')"
101
+ },
102
+ "return_artifacts": {
103
+ "oneOf": [
104
+ { "type": "string" },
105
+ { "type": "array", "items": { "type": "string" } },
106
+ { "type": "boolean", "const": false }
107
+ ],
108
+ "description": "Data key(s) to include in result, or false to exclude"
109
+ },
110
+ "expects_data": {
111
+ "type": "array",
112
+ "items": { "type": "string" },
113
+ "description": "Data keys this terminal state expects"
114
+ }
115
+ },
116
+ "additionalProperties": false
117
+ },
118
+ "retry_config": {
119
+ "type": "object",
120
+ "description": "Retry configuration for step failures",
121
+ "required": ["max_attempts"],
122
+ "properties": {
123
+ "max_attempts": {
124
+ "type": "integer",
125
+ "minimum": 1,
126
+ "description": "Maximum retry attempts"
127
+ },
128
+ "delay_ms": {
129
+ "type": "integer",
130
+ "minimum": 0,
131
+ "description": "Delay between retries in ms"
132
+ },
133
+ "backoff_multiplier": {
134
+ "type": "number",
135
+ "minimum": 1,
136
+ "description": "Backoff multiplier (e.g., 2 for exponential)"
137
+ }
138
+ },
139
+ "additionalProperties": false
140
+ },
141
+ "circuit_breaker_config": {
142
+ "type": "object",
143
+ "description": "Circuit breaker configuration for loops",
144
+ "required": ["max_iterations", "on_open"],
145
+ "properties": {
146
+ "max_iterations": {
147
+ "type": "integer",
148
+ "minimum": 1,
149
+ "description": "Maximum iterations before circuit opens"
150
+ },
151
+ "on_open": {
152
+ "type": "string",
153
+ "description": "Step to transition to when circuit opens"
154
+ }
155
+ },
156
+ "additionalProperties": false
157
+ }
158
+ }
159
+ }