Repository navigation
Expand file tree
/
Copy pathagent_gateway.go
More file actions
155 lines (144 loc) · 6.05 KB
/
Copy pathagent_gateway.go
File metadata and controls
155 lines (144 loc) · 6.05 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
package command
import (
"context"
"errors"
"fmt"
"strings"
"zenbot/internal/agent/api"
"zenbot/internal/agent/commandgateway"
"zenbot/internal/common"
"zenbot/internal/model"
"zenbot/internal/repository"
)
// AgentCommandGateway is the narrow command boundary available to the bounded agent action.
type AgentCommandGateway = commandgateway.Gateway
type CommandExecution = commandgateway.Execution
type agentCommandGateway struct{ engine common.Engine }
func NewAgentCommandGateway(engine common.Engine) AgentCommandGateway {
return agentCommandGateway{engine: engine}
}
// NewResolvingAgentCommandGateway resolves the current master once for each
// request so a persistent agent runtime never retains a retired host.
func NewResolvingAgentCommandGateway(resolve func() common.Engine) AgentCommandGateway {
return resolvingAgentCommandGateway{resolve: resolve}
}
type resolvingAgentCommandGateway struct{ resolve func() common.Engine }
func (g resolvingAgentCommandGateway) CommandAvailable(canonical string) bool {
if g.resolve == nil {
return false
}
return agentCommandGateway{engine: g.resolve()}.CommandAvailable(canonical)
}
func (g agentCommandGateway) CommandAvailable(canonical string) bool {
definition, ok := AgentCommandDefinition(canonical)
return ok && configuredCommandAvailable(g.engine, definition.Canonical, agentInvocation)
}
func (g resolvingAgentCommandGateway) Execute(ctx context.Context, caller api.Context, command, arguments string) (CommandExecution, error) {
if g.resolve == nil {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("command gateway is unavailable")
}
engine := g.resolve()
if engine == nil {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("command gateway is unavailable")
}
return NewAgentCommandGateway(engine).Execute(ctx, caller, command, arguments)
}
func (g agentCommandGateway) Execute(ctx context.Context, caller api.Context, command, arguments string) (result CommandExecution, resultErr error) {
if ctx == nil {
ctx = context.Background()
}
if err := ctx.Err(); err != nil {
return CommandExecution{Status: commandgateway.OutcomeRejected}, err
}
if !configuredDependency(g.engine) {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("command gateway is unavailable")
}
alias := strings.ToLower(strings.TrimSpace(command))
definition, ok := commandDefinitionFor(alias)
if !ok {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("command is unavailable")
}
approved, ok := AgentCommandDefinition(definition.Canonical)
if !ok || approved.Canonical != definition.Canonical {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("command is not allowed")
}
if !AgentCommandAuthorized(caller, definition) {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("command is not authorized")
}
if !commandgateway.TargetAllowed(caller, definition.Canonical, arguments) {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("moderation action must target the reviewed author")
}
release, err := common.BeginCommandDispatch(ctx, g.engine)
if err != nil {
return CommandExecution{Status: commandgateway.OutcomeRejected}, err
}
defer release()
if err := ctx.Err(); err != nil {
return CommandExecution{Status: commandgateway.OutcomeRejected}, err
}
if !configuredCommandAvailable(g.engine, definition.Canonical, agentInvocation) {
return CommandExecution{Status: commandgateway.OutcomeRejected}, fmt.Errorf("command is unavailable")
}
trip, hash := "", ""
if v := caller.Trip(); v != nil {
trip = *v
}
if v := caller.Hash(); v != nil {
hash = *v
}
arguments = strings.TrimSpace(arguments)
text := g.engine.GetPrefix() + alias
if arguments != "" {
text += " " + arguments
}
message := &model.ChatMessage{Name: caller.Nick(), Trip: trip, Hash: hash, Channel: caller.Room(), Text: text, Whisper: caller.Whisper(), IsWhisper: caller.Whisper()}
ctx, mutations := common.WithMutationRecorder(ctx)
capturing := &agentCaptureEngine{Engine: g.engine, invocationWhisper: caller.Whisper()}
// Effects may already exist when a later command operation or audit fails.
// Preserve receipts on every return, including panics after delivery.
defer func() {
if recover() != nil {
result.Status = commandgateway.OutcomeUnknown
resultErr = nil
}
result.Messages = append([]string(nil), capturing.messages...)
if result.Status == commandgateway.OutcomeSucceeded || capturing.dataObserved {
result.Data = append(result.Data[:0:0], capturing.data...)
result.DataObserved = capturing.dataObserved
}
count := capturing.actionCount + mutations.Count()
if count > 0 {
result.EffectsCommitted = true
result.Action = &commandgateway.ActionReceipt{Count: count}
}
if capturing.deliveryCount > 0 {
result.Delivery = &commandgateway.DeliveryReceipt{Count: capturing.deliveryCount}
}
}()
status, err := definition.New(capturing, message).Execute(ctx)
(&legacyAdapter{engine: capturing, def: definition, msg: message}).audit(ctx, status)
if len(capturing.snapshotCompletions) > 0 {
_ = capturing.awaitSnapshotCompletions(ctx)
return CommandExecution{Status: capturing.snapshotStatus}, nil
}
var notAdmitted *common.LifecycleRequestRejectedError
if errors.As(err, ¬Admitted) && !capturing.dataObserved && capturing.actionCount == 0 && mutations.Count() == 0 {
return CommandExecution{Status: commandgateway.OutcomeRejected}, err
}
if ctx.Err() != nil {
return CommandExecution{Status: commandgateway.OutcomeUnknown}, nil
}
if err != nil {
if errors.Is(err, repository.ErrNotFound) {
return CommandExecution{Status: commandgateway.OutcomeNotFound}, nil
}
if errors.Is(err, repository.ErrAmbiguousHistory) {
return CommandExecution{Status: commandgateway.OutcomeAmbiguous}, nil
}
return CommandExecution{Status: commandgateway.OutcomeUnknown}, nil
}
if status != model.SUCCESSFUL {
return CommandExecution{Status: commandgateway.OutcomeRejected}, nil
}
return CommandExecution{Status: commandgateway.OutcomeSucceeded}, nil
}