-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathagent_codex.go
More file actions
170 lines (153 loc) · 4.54 KB
/
agent_codex.go
File metadata and controls
170 lines (153 loc) · 4.54 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
package runn
import (
"context"
"fmt"
"strings"
"github.com/Songmu/prompter"
codex "github.com/k1LoW/codex-agent-sdk-go"
)
type codexProvider struct {
client *codex.Client
threadID string
opts []codex.Option
tOpts []codex.ThreadOption
}
func newCodexProvider(cfg *AgentRunnerConfig) (*codexProvider, error) {
if cfg.Provider != "" && cfg.Provider != "openai" {
return nil, fmt.Errorf("codex agent does not support provider %q (only openai)", cfg.Provider)
}
perms, err := parseAgentPermissions(cfg.Permissions)
if err != nil {
return nil, err
}
var opts []codex.Option
if cfg.Interactive {
opts = append(opts, codex.WithOnCommandApproval(func(_ context.Context, req codex.CommandApprovalRequest) (codex.ApprovalDecision, error) {
switch perms.decide(req.Command) {
case agentPermissionAllow:
return codex.DecisionAccept, nil
case agentPermissionDeny:
return codex.DecisionDecline, nil
default:
msg := fmt.Sprintf("Agent wants to run: %s", req.Command)
if prompter.YN(msg, false) {
return codex.DecisionAccept, nil
}
return codex.DecisionDecline, nil
}
}))
opts = append(opts, codex.WithOnFileChangeApproval(func(_ context.Context, _ codex.FileChangeApprovalRequest) (codex.ApprovalDecision, error) {
switch perms.decide("file_change") {
case agentPermissionAllow:
return codex.DecisionAccept, nil
case agentPermissionDeny:
return codex.DecisionDecline, nil
default:
if prompter.YN("Agent wants to modify files. Allow?", false) {
return codex.DecisionAccept, nil
}
return codex.DecisionDecline, nil
}
}))
opts = append(opts, codex.WithOnUserInput(func(_ context.Context, req codex.UserInputRequest) (map[string]string, error) {
answers := make(map[string]string)
for _, q := range req.Questions {
id, _ := q["id"].(string)
text, _ := q["text"].(string)
answers[id] = prompter.Prompt(text, "")
}
return answers, nil
}))
} else {
// Non-interactive: only explicitly allowed actions are approved
opts = append(opts, codex.WithOnCommandApproval(func(_ context.Context, req codex.CommandApprovalRequest) (codex.ApprovalDecision, error) {
if perms.decide(req.Command) == agentPermissionAllow {
return codex.DecisionAccept, nil
}
return codex.DecisionDecline, nil
}))
opts = append(opts, codex.WithOnFileChangeApproval(func(_ context.Context, _ codex.FileChangeApprovalRequest) (codex.ApprovalDecision, error) {
if perms.decide("file_change") == agentPermissionAllow {
return codex.DecisionAccept, nil
}
return codex.DecisionDecline, nil
}))
}
var tOpts []codex.ThreadOption
if cfg.Model != "" {
tOpts = append(tOpts, codex.WithModel(cfg.Model))
}
if perms.mode != "" {
tOpts = append(tOpts, codex.WithApprovalPolicy(perms.mode))
} else if perms.decide("*") == agentPermissionAllow {
tOpts = append(tOpts, codex.WithApprovalPolicy("full-auto"))
}
if perms.sandbox != "" {
tOpts = append(tOpts, codex.WithSandbox(perms.sandbox))
}
return &codexProvider{
opts: opts,
tOpts: tOpts,
}, nil
}
func (p *codexProvider) Run(ctx context.Context, req *agentRunRequest) (*AgentResponse, error) {
if req.clearContext {
if p.client != nil {
_ = p.client.Close()
p.client = nil
}
p.threadID = ""
}
if p.client == nil {
c := codex.NewClient(p.opts...)
if err := c.Connect(ctx); err != nil {
return nil, fmt.Errorf("codex agent connect: %w", err)
}
p.client = c
}
if p.threadID == "" {
thread, err := p.client.StartThread(ctx, p.tOpts...)
if err != nil {
p.closeAndReset()
return nil, fmt.Errorf("codex start thread: %w", err)
}
p.threadID = thread.ID
}
var buf strings.Builder
for evt, err := range p.client.StartTurn(ctx, p.threadID, []codex.UserInput{codex.TextInput(req.prompt)}) {
if err != nil {
p.closeAndReset()
return nil, fmt.Errorf("codex turn: %w", err)
}
switch e := evt.(type) {
case *codex.AgentMessageDeltaEvent:
buf.WriteString(e.Delta)
case *codex.ItemCompletedEvent:
if msg, ok := e.Item.(*codex.AgentMessageItem); ok {
// Use the complete text if available
buf.Reset()
buf.WriteString(msg.Text)
}
case *codex.ErrorEvent:
p.closeAndReset()
return nil, fmt.Errorf("codex error: %s", e.Message)
}
}
return &AgentResponse{Content: buf.String()}, nil
}
func (p *codexProvider) Close() error {
if p.client != nil {
err := p.client.Close()
p.client = nil
p.threadID = ""
return err
}
return nil
}
func (p *codexProvider) closeAndReset() {
if p.client != nil {
_ = p.client.Close()
p.client = nil
p.threadID = ""
}
}