-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathagent.go
More file actions
214 lines (188 loc) · 5.17 KB
/
agent.go
File metadata and controls
214 lines (188 loc) · 5.17 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
package runn
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/k1LoW/runn/internal/scope"
)
const (
agentStoreResponseKey = "res"
agentStoreContentKey = "content"
)
const (
agentPermissionsAllowPrefix = "allow:"
agentPermissionsDenyPrefix = "deny:"
agentPermissionsSandboxPrefix = "sandbox:"
)
type agentPermissionDecision int
const (
agentPermissionUndecided agentPermissionDecision = iota
agentPermissionAllow
agentPermissionDeny
)
// agentParsedPermissions holds the permissions rules and SDK-specific settings.
// Rules are evaluated in order (last match wins).
type agentParsedPermissions struct {
rules []agentPermissionRule // ordered rules from permissions array
mode string // SDK-specific mode (e.g., "plan", "acceptEdits", "full-auto")
sandbox string // sandbox mode (e.g., "workspace-write", "workspace-read")
}
type agentPermissionRule struct {
prefix string // "allow" or "deny"
toolName string // tool name or "*" for wildcard
}
func parseAgentPermissions(perms []string) (*agentParsedPermissions, error) {
p := &agentParsedPermissions{}
for _, perm := range perms {
perm = strings.TrimSpace(perm)
if perm == "" {
continue
}
switch {
case strings.HasPrefix(perm, agentPermissionsAllowPrefix):
toolName := strings.TrimPrefix(perm, agentPermissionsAllowPrefix)
if toolName == "" {
return nil, fmt.Errorf("invalid permission rule %q: tool name is required", perm)
}
p.rules = append(p.rules, agentPermissionRule{
prefix: "allow",
toolName: toolName,
})
case strings.HasPrefix(perm, agentPermissionsDenyPrefix):
toolName := strings.TrimPrefix(perm, agentPermissionsDenyPrefix)
if toolName == "" {
return nil, fmt.Errorf("invalid permission rule %q: tool name is required", perm)
}
p.rules = append(p.rules, agentPermissionRule{
prefix: "deny",
toolName: toolName,
})
case strings.HasPrefix(perm, agentPermissionsSandboxPrefix):
sandbox := strings.TrimPrefix(perm, agentPermissionsSandboxPrefix)
if sandbox == "" {
return nil, fmt.Errorf("invalid permission rule %q: sandbox mode is required", perm)
}
p.sandbox = sandbox
default:
p.mode = perm
}
}
return p, nil
}
// decide evaluates the permission rules for a tool (last match wins).
func (p *agentParsedPermissions) decide(toolName string) agentPermissionDecision {
result := agentPermissionUndecided
for _, rule := range p.rules {
if rule.toolName == "*" || rule.toolName == toolName {
switch rule.prefix {
case "allow":
result = agentPermissionAllow
case "deny":
result = agentPermissionDeny
}
}
}
return result
}
// collectAllowed returns tools explicitly allowed (for SDK AllowedTools).
func (p *agentParsedPermissions) collectAllowed() []string {
var tools []string
for _, rule := range p.rules {
if rule.prefix == "allow" {
tools = append(tools, rule.toolName)
}
}
return tools
}
// collectDenied returns tools explicitly denied (for SDK DisallowedTools/ExcludedTools).
func (p *agentParsedPermissions) collectDenied() []string {
var tools []string
for _, rule := range p.rules {
if rule.prefix == "deny" {
tools = append(tools, rule.toolName)
}
}
return tools
}
type agentRunner struct {
name string
provider agentProvider
operatorID string
mu sync.Mutex
}
func newAgentRunner(name string, cfg *AgentRunnerConfig) (*agentRunner, error) {
if cfg.Agent == "" {
return nil, fmt.Errorf("agent runner %q requires agent field", name)
}
if cfg.Model == "" {
return nil, fmt.Errorf("agent runner %q requires model field", name)
}
p, err := newAgentProvider(cfg)
if err != nil {
return nil, fmt.Errorf("agent runner %q: %w", name, err)
}
return &agentRunner{
name: name,
provider: p,
}, nil
}
func newAgentProvider(cfg *AgentRunnerConfig) (agentProvider, error) {
switch cfg.Agent {
case "claude":
return newClaudeProvider(cfg)
case "copilot":
return newCopilotProvider(cfg)
case "codex":
return newCodexProvider(cfg)
default:
return nil, fmt.Errorf("unsupported agent type: %s", cfg.Agent)
}
}
func (rnr *agentRunner) Run(ctx context.Context, s *step) error {
if !scope.IsRunAgentAllowed() {
return errors.New("scope error: agent runner is not allowed. 'run:agent' scope is required")
}
rnr.mu.Lock()
defer rnr.mu.Unlock()
o := s.parent
e, err := o.expandBeforeRecord(s.agentRequest, s)
if err != nil {
return err
}
r, ok := e.(map[string]any)
if !ok {
return fmt.Errorf("invalid agent request: %v", e)
}
parsed, err := parseAgentRequest(r)
if err != nil {
return err
}
o.capturers.captureAgentRequest(rnr.name, parsed)
resp, err := rnr.provider.Run(ctx, &agentRunRequest{
prompt: parsed.Prompt,
clearContext: parsed.ClearContext,
})
if err != nil {
return err
}
if resp == nil {
return fmt.Errorf("agent provider returned nil response")
}
o.capturers.captureAgentResponse(rnr.name, resp)
o.record(s.idx, map[string]any{
agentStoreResponseKey: map[string]any{
agentStoreContentKey: resp.Content,
},
})
return nil
}
func (rnr *agentRunner) Close() error {
rnr.mu.Lock()
defer rnr.mu.Unlock()
if rnr.provider != nil {
return rnr.provider.Close()
}
return nil
}