-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathsafe_output_handler.go
More file actions
129 lines (106 loc) · 4.57 KB
/
safe_output_handler.go
File metadata and controls
129 lines (106 loc) · 4.57 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
package workflow
import (
"github.com/githubnext/gh-aw/pkg/logger"
)
var safeOutputHandlerLog = logger.New("workflow:safe_output_handler")
// SafeOutputHandler defines the interface for safe output message handlers.
// Each handler is responsible for building the steps needed to process
// a specific type of safe output message (e.g., create_issue, add_comment).
type SafeOutputHandler interface {
// GetType returns the type identifier for this handler (e.g., "create_issue")
GetType() string
// IsEnabled checks if this handler should be processed based on the workflow configuration
IsEnabled(data *WorkflowData) bool
// BuildStepConfig builds the step configuration for this handler
// Returns nil if the handler is not enabled or cannot be built
BuildStepConfig(c *Compiler, data *WorkflowData, ctx *SafeOutputContext) *SafeOutputStepConfig
// GetOutputs returns the outputs that this handler produces
// These are used to build the job outputs map
GetOutputs() map[string]string
// RequiresTempIDMap returns true if this handler needs access to the temporary ID map
RequiresTempIDMap() bool
}
// SafeOutputContext holds contextual information about previously processed handlers
// This allows handlers to reference outputs from earlier handlers in the sequence
type SafeOutputContext struct {
// ThreatDetectionEnabled indicates if threat detection is enabled
ThreatDetectionEnabled bool
// MainJobName is the name of the main agent job
MainJobName string
// ProcessedHandlers tracks which handlers have been processed so far
// Map key is the handler type (e.g., "create_issue")
ProcessedHandlers map[string]bool
// HandlerOutputs tracks the outputs from each processed handler
// This allows later handlers to reference earlier outputs
HandlerOutputs map[string]map[string]string
// TempIDMapAvailable indicates if a temporary ID map is available from a previous handler
TempIDMapAvailable bool
// TempIDMapSource is the step ID that provides the temporary ID map
TempIDMapSource string
}
// NewSafeOutputContext creates a new context for processing safe output handlers
func NewSafeOutputContext(mainJobName string, threatDetectionEnabled bool) *SafeOutputContext {
return &SafeOutputContext{
ThreatDetectionEnabled: threatDetectionEnabled,
MainJobName: mainJobName,
ProcessedHandlers: make(map[string]bool),
HandlerOutputs: make(map[string]map[string]string),
TempIDMapAvailable: false,
TempIDMapSource: "",
}
}
// MarkProcessed marks a handler as processed and records its outputs
func (ctx *SafeOutputContext) MarkProcessed(handlerType string, outputs map[string]string) {
ctx.ProcessedHandlers[handlerType] = true
if len(outputs) > 0 {
ctx.HandlerOutputs[handlerType] = outputs
}
}
// IsProcessed checks if a handler has been processed
func (ctx *SafeOutputContext) IsProcessed(handlerType string) bool {
return ctx.ProcessedHandlers[handlerType]
}
// GetHandlerOutput retrieves a specific output from a processed handler
func (ctx *SafeOutputContext) GetHandlerOutput(handlerType, outputKey string) string {
if outputs, exists := ctx.HandlerOutputs[handlerType]; exists {
return outputs[outputKey]
}
return ""
}
// SetTempIDMapSource marks that a temporary ID map is available from a specific step
func (ctx *SafeOutputContext) SetTempIDMapSource(stepID string) {
ctx.TempIDMapAvailable = true
ctx.TempIDMapSource = stepID
}
// SafeOutputHandlerRegistry manages the collection of safe output handlers
type SafeOutputHandlerRegistry struct {
handlers []SafeOutputHandler
logger *logger.Logger
}
// NewSafeOutputHandlerRegistry creates a new handler registry
func NewSafeOutputHandlerRegistry() *SafeOutputHandlerRegistry {
return &SafeOutputHandlerRegistry{
handlers: make([]SafeOutputHandler, 0),
logger: safeOutputHandlerLog,
}
}
// Register adds a handler to the registry
func (r *SafeOutputHandlerRegistry) Register(handler SafeOutputHandler) {
r.handlers = append(r.handlers, handler)
r.logger.Printf("Registered safe output handler: %s", handler.GetType())
}
// GetHandlers returns all registered handlers in order
func (r *SafeOutputHandlerRegistry) GetHandlers() []SafeOutputHandler {
return r.handlers
}
// GetEnabledHandlers returns only the handlers that are enabled for the given workflow
func (r *SafeOutputHandlerRegistry) GetEnabledHandlers(data *WorkflowData) []SafeOutputHandler {
enabled := make([]SafeOutputHandler, 0)
for _, handler := range r.handlers {
if handler.IsEnabled(data) {
enabled = append(enabled, handler)
r.logger.Printf("Handler enabled: %s", handler.GetType())
}
}
return enabled
}