-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathagent_copilot.go
More file actions
145 lines (127 loc) · 3.77 KB
/
agent_copilot.go
File metadata and controls
145 lines (127 loc) · 3.77 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
package runn
import (
"context"
"errors"
"fmt"
"github.com/Songmu/prompter"
copilot "github.com/github/copilot-sdk/go"
)
type copilotProvider struct {
client *copilot.Client
session *copilot.Session
clientOpts *copilot.ClientOptions
sessionCfg *copilot.SessionConfig
}
func newCopilotProvider(cfg *AgentRunnerConfig) (*copilotProvider, error) {
clientOpts := &copilot.ClientOptions{}
sessionCfg := &copilot.SessionConfig{
ClientName: "runn",
}
if cfg.Model != "" {
sessionCfg.Model = cfg.Model
}
if cfg.System != "" {
sessionCfg.SystemMessage = &copilot.SystemMessageConfig{
Mode: "custom",
Content: cfg.System,
}
}
if len(cfg.Tools) > 0 {
sessionCfg.AvailableTools = cfg.Tools
}
if cfg.Provider != "" {
sessionCfg.Provider = &copilot.ProviderConfig{
Type: cfg.Provider,
}
}
perms, err := parseAgentPermissions(cfg.Permissions)
if err != nil {
return nil, err
}
if perms.mode != "" {
return nil, fmt.Errorf("unsupported copilot permissions value: %s", perms.mode)
}
if denied := perms.collectDenied(); len(denied) > 0 {
sessionCfg.ExcludedTools = denied
}
interactive := cfg.Interactive
sessionCfg.OnPermissionRequest = func(req copilot.PermissionRequest, _ copilot.PermissionInvocation) (copilot.PermissionRequestResult, error) {
toolName := ""
if req.ToolName != nil {
toolName = *req.ToolName
}
switch perms.decide(toolName) {
case agentPermissionAllow:
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindApproved}, nil
case agentPermissionDeny:
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindDeniedByRules}, nil
default:
if interactive {
msg := fmt.Sprintf("Agent requests permission: %s", toolName)
if prompter.YN(msg, false) {
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindApproved}, nil
}
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindDeniedInteractivelyByUser}, nil
}
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindDeniedCouldNotRequestFromUser}, nil
}
}
// Enable user input handling when interactive
if cfg.Interactive {
sessionCfg.OnUserInputRequest = func(req copilot.UserInputRequest, _ copilot.UserInputInvocation) (copilot.UserInputResponse, error) {
answer := prompter.Prompt(req.Question, "")
return copilot.UserInputResponse{Answer: answer, WasFreeform: true}, nil
}
}
return &copilotProvider{
clientOpts: clientOpts,
sessionCfg: sessionCfg,
}, nil
}
func (p *copilotProvider) Run(ctx context.Context, req *agentRunRequest) (*AgentResponse, error) {
if req.clearContext && p.session != nil {
if err := p.session.Disconnect(); err != nil {
return nil, fmt.Errorf("copilot session disconnect: %w", err)
}
p.session = nil
}
if p.client == nil {
p.client = copilot.NewClient(p.clientOpts)
if err := p.client.Start(ctx); err != nil {
p.client = nil
return nil, fmt.Errorf("copilot client start: %w", err)
}
}
if p.session == nil {
session, err := p.client.CreateSession(ctx, p.sessionCfg)
if err != nil {
return nil, fmt.Errorf("copilot create session: %w", err)
}
p.session = session
}
event, err := p.session.SendAndWait(ctx, copilot.MessageOptions{
Prompt: req.prompt,
})
if err != nil {
return nil, fmt.Errorf("copilot send: %w", err)
}
var content string
if event != nil {
if d, ok := event.Data.(*copilot.AssistantMessageData); ok {
content = d.Content
}
}
return &AgentResponse{Content: content}, nil
}
func (p *copilotProvider) Close() error {
var errs error
if p.session != nil {
errs = errors.Join(errs, p.session.Disconnect())
p.session = nil
}
if p.client != nil {
errs = errors.Join(errs, p.client.Stop())
p.client = nil
}
return errs
}