-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameters.go
More file actions
464 lines (407 loc) · 15.7 KB
/
Copy pathparameters.go
File metadata and controls
464 lines (407 loc) · 15.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package task_engine
import (
"context"
"fmt"
"reflect"
"strings"
)
// ActionParameter interface for all parameter types that can be resolved at runtime
// to provide values for action execution. Parameters support references to outputs
// from other actions, tasks, or static values.
type ActionParameter interface {
// Resolve returns the actual value for this parameter by looking up
// references in the global context or returning static values.
Resolve(ctx context.Context, globalContext *GlobalContext) (interface{}, error)
}
// StaticParameter represents a fixed value that doesn't need resolution.
// Use this for values known at task creation time.
type StaticParameter struct {
Value interface{} // The static value to use
}
func (p StaticParameter) Resolve(ctx context.Context, globalContext *GlobalContext) (interface{}, error) {
return p.Value, nil
}
// ActionOutputParameter references output from a specific action.
// Use this to pass data between actions within the same task.
type ActionOutputParameter struct {
ActionID string // Required: ID of the action to reference
OutputKey string // Optional: specific output field to extract (omit for entire output)
}
func (p ActionOutputParameter) Resolve(ctx context.Context, globalContext *GlobalContext) (interface{}, error) {
if globalContext == nil {
return nil, fmt.Errorf("ActionOutputParameter: globalContext is nil")
}
if p.ActionID == "" {
return nil, fmt.Errorf("ActionOutputParameter: ActionID cannot be empty")
}
globalContext.mu.RLock()
output, exists := globalContext.ActionOutputs[p.ActionID]
globalContext.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("ActionOutputParameter: action '%s' not found in context", p.ActionID)
}
if p.OutputKey != "" {
// Validate OutputKey exists in output
if outputMap, ok := output.(map[string]interface{}); ok {
if value, exists := outputMap[p.OutputKey]; exists {
return value, nil
}
return nil, fmt.Errorf("ActionOutputParameter: output key '%s' not found in action '%s'", p.OutputKey, p.ActionID)
}
return nil, fmt.Errorf("ActionOutputParameter: action '%s' output is not a map, cannot extract key '%s'", p.ActionID, p.OutputKey)
}
return output, nil
}
// ActionResultParameter references results from actions implementing ResultProvider
type ActionResultParameter struct {
ActionID string // Required: ID of the action to reference
ResultKey string // Optional: specific result field to extract
}
func (p ActionResultParameter) Resolve(ctx context.Context, globalContext *GlobalContext) (interface{}, error) {
if globalContext == nil {
return nil, fmt.Errorf("ActionResultParameter: globalContext is nil")
}
if p.ActionID == "" {
return nil, fmt.Errorf("ActionResultParameter: ActionID cannot be empty")
}
globalContext.mu.RLock()
resultProvider, exists := globalContext.ActionResults[p.ActionID]
globalContext.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("ActionResultParameter: action '%s' not found in context", p.ActionID)
}
result := resultProvider.GetResult()
if p.ResultKey != "" {
// Extract specific field from result
if resultMap, ok := result.(map[string]interface{}); ok {
if value, exists := resultMap[p.ResultKey]; exists {
return value, nil
}
return nil, fmt.Errorf("ActionResultParameter: result key '%s' not found in action '%s'", p.ResultKey, p.ActionID)
}
return nil, fmt.Errorf("ActionResultParameter: action '%s' result is not a map, cannot extract key '%s'", p.ActionID, p.ResultKey)
}
return result, nil
}
// TaskResultParameter references results from tasks implementing ResultProvider
type TaskResultParameter struct {
TaskID string // Required: ID of the task to reference
ResultKey string // Optional: specific result field to extract
}
func (p TaskResultParameter) Resolve(ctx context.Context, globalContext *GlobalContext) (interface{}, error) {
if globalContext == nil {
return nil, fmt.Errorf("TaskResultParameter: globalContext is nil")
}
if p.TaskID == "" {
return nil, fmt.Errorf("TaskResultParameter: TaskID cannot be empty")
}
globalContext.mu.RLock()
resultProvider, exists := globalContext.TaskResults[p.TaskID]
globalContext.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("TaskResultParameter: task '%s' not found in context", p.TaskID)
}
result := resultProvider.GetResult()
if p.ResultKey != "" {
if resultMap, ok := result.(map[string]interface{}); ok {
if value, exists := resultMap[p.ResultKey]; exists {
return value, nil
}
return nil, fmt.Errorf("TaskResultParameter: result key '%s' not found in task '%s'", p.ResultKey, p.TaskID)
}
return nil, fmt.Errorf("TaskResultParameter: task '%s' result is not a map, cannot extract key '%s'", p.TaskID, p.ResultKey)
}
return result, nil
}
// TaskOutputParameter references output from a specific task
type TaskOutputParameter struct {
TaskID string // Required: ID of the task to reference
OutputKey string // Optional: specific output field to extract
}
func (p TaskOutputParameter) Resolve(ctx context.Context, globalContext *GlobalContext) (interface{}, error) {
if globalContext == nil {
return nil, fmt.Errorf("TaskOutputParameter: globalContext is nil")
}
if p.TaskID == "" {
return nil, fmt.Errorf("TaskOutputParameter: TaskID cannot be empty")
}
globalContext.mu.RLock()
output, exists := globalContext.TaskOutputs[p.TaskID]
globalContext.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("TaskOutputParameter: task '%s' not found in context", p.TaskID)
}
if p.OutputKey != "" {
// Extract specific field from output
if outputMap, ok := output.(map[string]interface{}); ok {
if value, exists := outputMap[p.OutputKey]; exists {
return value, nil
}
return nil, fmt.Errorf("TaskOutputParameter: output key '%s' not found in task '%s'", p.OutputKey, p.TaskID)
}
return nil, fmt.Errorf("TaskOutputParameter: task '%s' output is not a map, cannot extract key '%s'", p.TaskID, p.OutputKey)
}
return output, nil
}
// EntityOutputParameter references output from any entity (action or task)
type EntityOutputParameter struct {
EntityType string // Required: "action" or "task"
EntityID string // Required: ID of the entity to reference
OutputKey string // Optional: specific output field to extract
}
func (p EntityOutputParameter) Resolve(ctx context.Context, globalContext *GlobalContext) (interface{}, error) {
if globalContext == nil {
return nil, fmt.Errorf("EntityOutputParameter: globalContext is nil")
}
if p.EntityType == "" || p.EntityID == "" {
return nil, fmt.Errorf("EntityOutputParameter: EntityType and EntityID cannot be empty")
}
const (
entityTypeAction = "action"
entityTypeTask = "task"
)
switch p.EntityType {
case entityTypeAction:
// Try ActionOutputs first
globalContext.mu.RLock()
output, existsOutput := globalContext.ActionOutputs[p.EntityID]
globalContext.mu.RUnlock()
if existsOutput {
if p.OutputKey != "" {
if outputMap, ok := output.(map[string]interface{}); ok {
if value, exists := outputMap[p.OutputKey]; exists {
return value, nil
}
return nil, fmt.Errorf("EntityOutputParameter: output key '%s' not found in action '%s'", p.OutputKey, p.EntityID)
}
return nil, fmt.Errorf("EntityOutputParameter: action '%s' output is not a map, cannot extract key '%s'", p.EntityID, p.OutputKey)
}
return output, nil
}
// Try ActionResults if ActionOutputs doesn't have it
globalContext.mu.RLock()
resultProvider, existsResult := globalContext.ActionResults[p.EntityID]
globalContext.mu.RUnlock()
if existsResult {
result := resultProvider.GetResult()
if p.OutputKey != "" {
if resultMap, ok := result.(map[string]interface{}); ok {
if value, exists := resultMap[p.OutputKey]; exists {
return value, nil
}
return nil, fmt.Errorf("EntityOutputParameter: result key '%s' not found in action '%s'", p.OutputKey, p.EntityID)
}
return nil, fmt.Errorf("EntityOutputParameter: action '%s' result is not a map, cannot extract key '%s'", p.EntityID, p.OutputKey)
}
return result, nil
}
return nil, fmt.Errorf("EntityOutputParameter: action '%s' not found in context", p.EntityID)
case entityTypeTask:
// Try TaskOutputs first
globalContext.mu.RLock()
output, existsOutput := globalContext.TaskOutputs[p.EntityID]
globalContext.mu.RUnlock()
if existsOutput {
if p.OutputKey != "" {
if outputMap, ok := output.(map[string]interface{}); ok {
if value, exists := outputMap[p.OutputKey]; exists {
return value, nil
}
return nil, fmt.Errorf("EntityOutputParameter: output key '%s' not found in task '%s'", p.OutputKey, p.EntityID)
}
return nil, fmt.Errorf("EntityOutputParameter: task '%s' output is not a map, cannot extract key '%s'", p.EntityID, p.OutputKey)
}
return output, nil
}
// Try TaskResults if TaskOutputs doesn't have it
globalContext.mu.RLock()
resultProvider, existsResult := globalContext.TaskResults[p.EntityID]
globalContext.mu.RUnlock()
if existsResult {
result := resultProvider.GetResult()
if p.OutputKey != "" {
if resultMap, ok := result.(map[string]interface{}); ok {
if value, exists := resultMap[p.OutputKey]; exists {
return value, nil
}
return nil, fmt.Errorf("EntityOutputParameter: result key '%s' not found in task '%s'", p.OutputKey, p.EntityID)
}
return nil, fmt.Errorf("EntityOutputParameter: task '%s' result is not a map, cannot extract key '%s'", p.EntityID, p.OutputKey)
}
return result, nil
}
return nil, fmt.Errorf("EntityOutputParameter: task '%s' not found in context", p.EntityID)
default:
return nil, fmt.Errorf("EntityOutputParameter: invalid entity type '%s', must be 'action' or 'task'", p.EntityType)
}
}
// --- Typed parameter resolution helpers ---
// ResolveString resolves an ActionParameter to a string with helpful
// conversions and clear error messages. When the parameter is nil,
// it returns an empty string without error.
func ResolveString(ctx context.Context, p ActionParameter, globalContext *GlobalContext) (string, error) {
if p == nil {
return "", nil
}
v, err := p.Resolve(ctx, globalContext)
if err != nil {
return "", err
}
switch t := v.(type) {
case string:
return t, nil
case []byte:
return string(t), nil
case fmt.Stringer:
return t.String(), nil
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
return fmt.Sprint(v), nil
default:
return "", fmt.Errorf("parameter is not a string, got %T", v)
}
}
// ResolveBool resolves an ActionParameter to a bool with common coercions.
// If parameter is nil, returns false.
func ResolveBool(ctx context.Context, p ActionParameter, globalContext *GlobalContext) (bool, error) {
if p == nil {
return false, nil
}
v, err := p.Resolve(ctx, globalContext)
if err != nil {
return false, err
}
switch t := v.(type) {
case bool:
return t, nil
case string:
s := strings.TrimSpace(strings.ToLower(t))
if s == "true" || s == "1" || s == "yes" || s == "y" { // common truthy strings
return true, nil
}
if s == "false" || s == "0" || s == "no" || s == "n" {
return false, nil
}
return false, fmt.Errorf("cannot convert string '%s' to bool", t)
case int:
return t != 0, nil
case int64:
return t != 0, nil
case uint:
return t != 0, nil
default:
return false, fmt.Errorf("parameter is not a bool, got %T", v)
}
}
// ResolveStringSlice resolves an ActionParameter into a []string.
// Accepts []string directly, or splits a string by comma or spaces.
func ResolveStringSlice(ctx context.Context, p ActionParameter, globalContext *GlobalContext) ([]string, error) {
if p == nil {
return nil, nil
}
v, err := p.Resolve(ctx, globalContext)
if err != nil {
return nil, err
}
switch t := v.(type) {
case []string:
return t, nil
case string:
s := strings.TrimSpace(t)
if s == "" {
return []string{}, nil
}
if strings.Contains(s, ",") {
parts := strings.Split(s, ",")
out := make([]string, 0, len(parts))
for _, p := range parts {
p = strings.TrimSpace(p)
if p != "" {
out = append(out, p)
}
}
return out, nil
}
return strings.Fields(s), nil
default:
return nil, fmt.Errorf("parameter is not a string slice or string, got %T", v)
}
}
// ResolveAs provides a generic typed resolver using existing parameter resolution.
func ResolveAs[T any](ctx context.Context, p ActionParameter, globalContext *GlobalContext) (T, error) {
var zero T
if p == nil {
return zero, nil
}
v, err := p.Resolve(ctx, globalContext)
if err != nil {
return zero, err
}
out, ok := v.(T)
if !ok {
return zero, fmt.Errorf("expected %T, got %T", zero, v)
}
return out, nil
}
// Helper functions for common parameter patterns
// ActionOutput creates a parameter reference to an entire action output
func ActionOutput(actionID string) ActionOutputParameter {
return ActionOutputParameter{ActionID: actionID}
}
// ActionOutputField creates a parameter reference to a specific field in an action output
func ActionOutputField(actionID, field string) ActionOutputParameter {
return ActionOutputParameter{ActionID: actionID, OutputKey: field}
}
// ActionResult creates a parameter reference to an action result (for ResultProvider actions)
func ActionResult(actionID string) ActionResultParameter {
return ActionResultParameter{ActionID: actionID}
}
// ActionResultField creates a parameter reference to a specific field in an action result
func ActionResultField(actionID, field string) ActionResultParameter {
return ActionResultParameter{ActionID: actionID, ResultKey: field}
}
// TaskOutput creates a parameter reference to an entire task output
func TaskOutput(taskID string) TaskOutputParameter {
return TaskOutputParameter{TaskID: taskID}
}
// TaskOutputField creates a parameter reference to a specific field in a task output
func TaskOutputField(taskID, field string) TaskOutputParameter {
return TaskOutputParameter{TaskID: taskID, OutputKey: field}
}
// TaskResult creates a parameter reference to an entire task result (for ResultProvider tasks)
func TaskResult(taskID string) TaskResultParameter {
return TaskResultParameter{TaskID: taskID}
}
// TaskResultField creates a parameter reference to a specific field in a task result
func TaskResultField(taskID, field string) TaskResultParameter {
return TaskResultParameter{TaskID: taskID, ResultKey: field}
}
// EntityOutput creates a parameter reference to any entity type (action or task)
func EntityOutput(entityType, entityID string) EntityOutputParameter {
return EntityOutputParameter{EntityType: entityType, EntityID: entityID}
}
// EntityOutputField creates a parameter reference to a specific field in any entity output
func EntityOutputField(entityType, entityID, field string) EntityOutputParameter {
return EntityOutputParameter{EntityType: entityType, EntityID: entityID, OutputKey: field}
}
// TypedOutputKey provides a way to associate an output field name with an expected
// struct type T. Validate can be used to check that the field exists on T at runtime.
// Note: This is a runtime validation helper; compile-time validation would require codegen.
// TypedOutputKey provides compile-time validation of output keys for type-safe
// parameter references. Use this when you want to ensure output keys exist
// in your output types at compile time.
type TypedOutputKey[T any] struct {
ActionID string // ID of the action to reference
Key string // Field name to extract from the output
}
// Validate checks whether Key is a valid exported field on T when T is a struct.
// If T is not a struct, Validate returns nil (no validation performed).
func (k TypedOutputKey[T]) Validate() error {
t := reflect.TypeOf((*T)(nil)).Elem()
if t.Kind() != reflect.Struct {
return nil
}
if _, exists := t.FieldByName(k.Key); !exists {
return fmt.Errorf("field '%s' does not exist on output type %s", k.Key, t.Name())
}
return nil
}