-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
280 lines (223 loc) · 6.76 KB
/
context.go
File metadata and controls
280 lines (223 loc) · 6.76 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package gorkflow
import (
"context"
"encoding/json"
"fmt"
"github.com/rs/zerolog"
)
// StepContext provides rich context to step handlers
type StepContext struct {
context.Context
// Execution metadata
RunID string
StepID string
Attempt int
// Logger (enriched with step context)
Logger zerolog.Logger
// Access to other step data (inputs and outputs)
Data StepDataAccessor
// Access to workflow-level state
State StateAccessor
// Custom context (user-defined)
CustomContext any
}
// GetContext retrieves the custom context from the step context
func GetContext[T any](ctx *StepContext) (T, error) {
var zero T
if ctx.CustomContext == nil {
return zero, fmt.Errorf("custom context is nil")
}
val, ok := ctx.CustomContext.(T)
if !ok {
return zero, fmt.Errorf("custom context is not of type %T", zero)
}
return val, nil
}
// StepDataAccessor provides type-safe access to other step outputs and inputs
type StepDataAccessor interface {
// GetOutput retrieves output from a specific step
GetOutput(stepID string, target interface{}) error
// GetInput retrieves input from a specific step
GetInput(stepID string, target interface{}) error
// HasOutput checks if a step has produced output
HasOutput(stepID string) bool
}
// GetOutput is a generic function for type-safe output retrieval from StepContext
func GetOutput[T any](ctx *StepContext, stepID string) (T, error) {
var result T
err := ctx.Data.GetOutput(stepID, &result)
return result, err
}
// GetInput is a generic function for type-safe input retrieval from StepContext
func GetInput[T any](ctx *StepContext, stepID string) (T, error) {
var result T
err := ctx.Data.GetInput(stepID, &result)
return result, err
}
// StateAccessor provides type-safe access to workflow state
type StateAccessor interface {
// Set stores a value in the workflow state
Set(key string, value interface{}) error
// Get retrieves a value from the workflow state
Get(key string, target interface{}) error
// Delete removes a key from the state
Delete(key string) error
// Has checks if a key exists
Has(key string) bool
// GetAll retrieves all state data
GetAll() (map[string][]byte, error)
}
// SetTyped is a generic function for type-safe state setting
func SetTyped[T any](accessor StateAccessor, key string, value T) error {
return accessor.Set(key, value)
}
// GetTyped is a generic function for type-safe state retrieval
func GetTyped[T any](accessor StateAccessor, key string) (T, error) {
var result T
err := accessor.Get(key, &result)
return result, err
}
// stepAccessor implements StepDataAccessor
type stepAccessor struct {
runID string
store WorkflowStore
outputCache map[string][]byte
inputCache map[string][]byte
}
// newStepAccessor creates a new step accessor
func newStepAccessor(runID string, wfStore WorkflowStore) StepDataAccessor {
return &stepAccessor{
runID: runID,
store: wfStore,
outputCache: make(map[string][]byte),
inputCache: make(map[string][]byte),
}
}
// NewStepAccessor creates a new step accessor (exported)
func NewStepAccessor(runID string, wfStore WorkflowStore) StepDataAccessor {
return newStepAccessor(runID, wfStore)
}
func (a *stepAccessor) GetOutput(stepID string, target interface{}) error {
// Check cache first
if data, ok := a.outputCache[stepID]; ok {
return json.Unmarshal(data, target)
}
// Load from store
data, err := a.store.LoadStepOutput(context.Background(), a.runID, stepID)
if err != nil {
return fmt.Errorf("failed to load output for step %s: %w", stepID, err)
}
// Cache it
a.outputCache[stepID] = data
// Unmarshal
if err := json.Unmarshal(data, target); err != nil {
return fmt.Errorf("failed to unmarshal output for step %s: %w", stepID, err)
}
return nil
}
func (a *stepAccessor) HasOutput(stepID string) bool {
// Check cache
if _, ok := a.outputCache[stepID]; ok {
return true
}
// Check store
_, err := a.store.LoadStepOutput(context.Background(), a.runID, stepID)
return err == nil
}
func (a *stepAccessor) GetInput(stepID string, target interface{}) error {
// Check cache first
if data, ok := a.inputCache[stepID]; ok {
return json.Unmarshal(data, target)
}
// Load step execution to get the input
exec, err := a.store.GetStepExecution(context.Background(), a.runID, stepID)
if err != nil {
return fmt.Errorf("failed to load step execution for step %s: %w", stepID, err)
}
if exec.Input == nil {
return fmt.Errorf("no input found for step %s", stepID)
}
// Cache it
a.inputCache[stepID] = exec.Input
// Unmarshal
if err := json.Unmarshal(exec.Input, target); err != nil {
return fmt.Errorf("failed to unmarshal input for step %s: %w", stepID, err)
}
return nil
}
// stateAccessor implements StateAccessor
type stateAccessor struct {
runID string
store WorkflowStore
cache map[string][]byte
}
// NewStateAccessor creates a new state accessor
func NewStateAccessor(runID string, wfStore WorkflowStore) StateAccessor {
return &stateAccessor{
runID: runID,
store: wfStore,
cache: make(map[string][]byte),
}
}
func (a *stateAccessor) Set(key string, value interface{}) error {
// Marshal value
data, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("failed to marshal state value for key %s: %w", key, err)
}
// Update cache
a.cache[key] = data
// Persist to store
if err := a.store.SaveState(context.Background(), a.runID, key, data); err != nil {
return fmt.Errorf("failed to save state for key %s: %w", key, err)
}
return nil
}
func (a *stateAccessor) Get(key string, target interface{}) error {
// Check cache first
if data, ok := a.cache[key]; ok {
return json.Unmarshal(data, target)
}
// Load from store
data, err := a.store.LoadState(context.Background(), a.runID, key)
if err != nil {
return fmt.Errorf("failed to load state for key %s: %w", key, err)
}
// Cache it
a.cache[key] = data
// Unmarshal
if err := json.Unmarshal(data, target); err != nil {
return fmt.Errorf("failed to unmarshal state for key %s: %w", key, err)
}
return nil
}
func (a *stateAccessor) Delete(key string) error {
// Remove from cache
delete(a.cache, key)
// Delete from store
if err := a.store.DeleteState(context.Background(), a.runID, key); err != nil {
return fmt.Errorf("failed to delete state for key %s: %w", key, err)
}
return nil
}
func (a *stateAccessor) Has(key string) bool {
// Check cache
if _, ok := a.cache[key]; ok {
return true
}
// Check store
_, err := a.store.LoadState(context.Background(), a.runID, key)
return err == nil
}
func (a *stateAccessor) GetAll() (map[string][]byte, error) {
// Get all from store
data, err := a.store.GetAllState(context.Background(), a.runID)
if err != nil {
return nil, fmt.Errorf("failed to get all state: %w", err)
}
// Update cache
for k, v := range data {
a.cache[k] = v
}
return data, nil
}