-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow-basics.lua
More file actions
236 lines (214 loc) · 7.36 KB
/
Copy pathworkflow-basics.lua
File metadata and controls
236 lines (214 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
-- Profile: minimal (recommended)
-- Run with: llmspell -p minimal run workflow-basics.lua
-- No LLM required
-- ============================================================
-- LLMSPELL FEATURES SHOWCASE
-- ============================================================
-- Phase: 13c.5.6 - Example Header Standardization
-- Category: features
-- Feature ID: 03 - Workflow Basics v0.7.0
-- Complexity: INTERMEDIATE
-- Real-World Use Case: Automating multi-step data processing pipelines
-- Feature Category: Workflows
--
-- Purpose: Introduction to workflow orchestration patterns
-- Architecture: Builder pattern for workflow construction
-- Key Capabilities:
-- • Workflow.builder() - Fluent workflow creation
-- • Sequential execution - Step-by-step processing
-- • Parallel execution - Concurrent step execution
-- • Tool integration - Orchestrating multiple tools
-- • Data flow between steps
--
-- Prerequisites: None (uses local tools)
--
-- HOW TO RUN:
-- ./target/debug/llmspell run examples/script-users/features/workflow-basics.lua
--
-- EXPECTED OUTPUT:
-- Two workflow examples: sequential and parallel execution
-- Execution time: <2 seconds
--
-- Time to Complete: 2 seconds
-- Next Steps: See advanced-patterns/complex-workflows.lua
-- ============================================================
print("=== Workflow Basics - Orchestration Patterns ===\n")
-- 1. SEQUENTIAL WORKFLOW
print("1. Sequential Workflow")
print("-" .. string.rep("-", 21))
-- Create a sequential workflow that processes data step by step
local seq_workflow = Workflow.builder()
:name("data_processing_pipeline")
:description("Process data through multiple sequential steps")
:sequential() -- Sequential execution mode
:add_step({
name = "generate_id",
type = "tool",
tool = "uuid-generator",
input = {
operation = "generate",
version = "v4"
}
})
:add_step({
name = "get_timestamp",
type = "tool",
tool = "datetime-handler",
input = {
operation = "now"
}
})
:add_step({
name = "create_message",
type = "tool",
tool = "template-creator",
input = {
input = "Workflow {{id}} completed at {{time}}",
context = {
id = "{{step.generate_id.result}}",
time = "{{step.get_timestamp.result}}"
}
}
})
:build()
print(" Executing sequential pipeline...")
local success, seq_result = pcall(function()
return seq_workflow:execute({})
end)
if success and seq_result and seq_result.text then
-- Workflow returns AgentOutput with text field containing summary
if seq_result.text:match("completed successfully") then
print(" ✓ Sequential workflow completed")
print(" Summary: " .. seq_result.text:sub(1, 60) .. "...")
else
print(" ✗ Sequential workflow failed: " .. seq_result.text)
end
else
local error_msg = success and "No result" or tostring(seq_result)
print(" ✗ Sequential workflow failed: " .. error_msg)
end
-- 2. PARALLEL WORKFLOW
print("\n2. Parallel Workflow")
print("-" .. string.rep("-", 19))
-- Create a parallel workflow that runs multiple operations concurrently
local par_workflow = Workflow.builder()
:name("parallel_data_gathering")
:description("Gather multiple data points in parallel")
:parallel() -- Parallel execution mode
:add_step({
name = "generate_uuid",
type = "tool",
tool = "uuid-generator",
input = {
operation = "generate",
version = "v4"
}
})
:add_step({
name = "calculate_hash",
type = "tool",
tool = "hash-calculator",
input = {
operation = "hash",
algorithm = "sha256",
input = "workflow_data"
}
})
:add_step({
name = "encode_data",
type = "tool",
tool = "base64-encoder",
input = {
operation = "encode",
input = "parallel_test"
}
})
:build()
print(" Executing parallel operations...")
local success, par_result = pcall(function()
return par_workflow:execute({})
end)
if success and par_result and par_result.text then
if par_result.text:match("completed successfully") then
print(" ✓ Parallel workflow completed")
print(" All 3 operations ran concurrently")
else
print(" ✗ Parallel workflow failed: " .. par_result.text)
end
else
local error_msg = success and "No result" or tostring(par_result)
print(" ✗ Parallel workflow failed: " .. error_msg)
end
-- 3. WORKFLOW WITH INPUT PARAMETERS
print("\n3. Parameterized Workflow")
print("-" .. string.rep("-", 24))
-- Create a workflow that accepts input parameters
local param_workflow = Workflow.builder()
:name("text_processor")
:description("Process text with configurable operations")
:sequential()
:add_step({
name = "uppercase",
type = "tool",
tool = "text-manipulator",
input = {
operation = "uppercase",
input = "{{input.text}}" -- Reference to workflow input
}
})
:add_step({
name = "hash_text",
type = "tool",
tool = "hash-calculator",
input = {
operation = "hash",
algorithm = "{{input.algorithm}}", -- Configurable algorithm
input = "{{step.uppercase.result}}" -- Use previous step result
}
})
:build()
print(" Executing with parameters...")
local success, param_result = pcall(function()
return param_workflow:execute({
text = "hello workflow",
algorithm = "md5"
})
end)
if success and param_result and param_result.text then
if param_result.text:match("completed successfully") then
print(" ✓ Parameterized workflow completed")
else
print(" ✗ Parameterized workflow failed: " .. param_result.text)
end
else
local error_msg = success and "No result" or tostring(param_result)
print(" ✗ Parameterized workflow failed: " .. error_msg)
end
-- 4. WORKFLOW STATE AND DATA FLOW
print("\n4. Data Flow Patterns")
print("-" .. string.rep("-", 20))
print(" Step results accessible via: {{step.STEP_NAME.result}}")
print(" Workflow input via: {{input.PARAM_NAME}}")
print(" Conditional execution: See advanced-patterns/")
print(" Error handling: Each step can define retry policies")
-- 5. WORKFLOW DISCOVERY
print("\n5. Workflow Management")
print("-" .. string.rep("-", 21))
-- List available workflows (if any registered)
local workflows = Workflow.list()
print(" Registered workflows: " .. #workflows)
-- Workflow execution modes
print(" Execution modes:")
print(" • sequential() - Steps run one after another")
print(" • parallel() - Steps run concurrently")
print(" • conditional() - See advanced patterns")
-- 6. BEST PRACTICES
print("\n6. Best Practices")
print("-" .. string.rep("-", 16))
print(" • Name steps clearly for result references")
print(" • Use parallel mode for independent operations")
print(" • Validate tool availability before workflow creation")
print(" • Handle errors at workflow and step level")
print(" • Keep workflows focused on single responsibility")
print("\n=== Workflow Basics Complete ===")
print("Next: Explore state-persistence.lua for data management")