-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgethelp.go
More file actions
208 lines (173 loc) · 4.55 KB
/
Copy pathgethelp.go
File metadata and controls
208 lines (173 loc) · 4.55 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
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"time"
"github.com/sashabaranov/go-openai"
)
type GetHelpTool struct {
summaryPath string
modelName string
}
func NewGetHelpTool(summaryPath, modelName string) *GetHelpTool {
return &GetHelpTool{
summaryPath: summaryPath,
modelName: modelName,
}
}
func (t *GetHelpTool) Name() string {
return "get_help"
}
func (t *GetHelpTool) Description() string {
return "Escalate difficult problems to OpenAI for expert guidance"
}
func (t *GetHelpTool) Schema() map[string]interface{} {
return map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"question": map[string]interface{}{
"type": "string",
"description": "The specific question or problem you need help with",
},
"summary": map[string]interface{}{
"type": "string",
"description": "Brief summary of your project context",
},
"relevant_code": map[string]interface{}{
"type": "string",
"description": "Any relevant code snippets (optional)",
},
},
"required": []string{"question", "summary"},
}
}
func (t *GetHelpTool) Call(arguments map[string]interface{}) ([]map[string]interface{}, error) {
var question, summary, relevantCode string
if q, ok := arguments["question"].(string); ok {
question = q
}
if s, ok := arguments["summary"].(string); ok {
summary = s
}
if rc, ok := arguments["relevant_code"].(string); ok {
relevantCode = rc
}
if question == "" || summary == "" {
return []map[string]interface{}{
{
"type": "text",
"text": "Error: Missing required fields: question and summary",
},
}, fmt.Errorf("missing required fields")
}
// Load project summary
projectSummary, err := t.loadSummary()
if err != nil {
log.Printf("Couldn't load the summary file: %v", err)
return []map[string]interface{}{
{
"type": "text",
"text": "The architect is currently unavailable. Please try again later.",
},
}, err
}
// Build prompt
prompt, err := t.buildPrompt(projectSummary, question, relevantCode)
if err != nil {
log.Printf("Couldn't build the prompt: %v", err)
return []map[string]interface{}{
{
"type": "text",
"text": "The architect is currently unavailable. Please try again later.",
},
}, err
}
log.Println("Ready to call OpenAI")
// Call OpenAI
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
answer, err := t.askOpenAI(ctx, prompt)
if err != nil {
log.Printf("OpenAI call failed: %v", err)
return []map[string]interface{}{
{
"type": "text",
"text": "The architect is currently unavailable. Please try again later.",
},
}, err
}
log.Printf("[%s] OpenAI call completed successfully", time.Now().Format(time.RFC3339))
return []map[string]interface{}{
{
"type": "text",
"text": answer,
},
}, nil
}
func (t *GetHelpTool) loadSummary() (string, error) {
path := t.summaryPath
if path == "" {
path = "./README.md"
}
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
content, err := io.ReadAll(file)
if err != nil {
return "", err
}
return string(content), nil
}
func (t *GetHelpTool) buildPrompt(summary, question, relevantCode string) (string, error) {
template := `As a software architect, provide help with this issue:
<summary>
%s
</summary>
---
**Question:** %s
**Relevant Code:** %s`
prompt := fmt.Sprintf(template, summary, question, relevantCode)
// Check token limit (rough estimate: ~4 chars per token)
if len(prompt) > 80000 { // 20,000 tokens * 4 chars
return "", fmt.Errorf("prompt exceeds 20,000 token limit")
}
return prompt, nil
}
func (t *GetHelpTool) askOpenAI(ctx context.Context, prompt string) (string, error) {
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
maxRetries := 3
backoffDurations := []time.Duration{2 * time.Second, 4 * time.Second, 8 * time.Second}
model := t.modelName
if model == "" {
model = "o3" // default
}
for attempt := range maxRetries {
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
})
if err != nil {
// Check if it's a retryable error (429 or 5xx)
if attempt < maxRetries-1 {
time.Sleep(backoffDurations[attempt])
continue
}
return "", err
}
if len(resp.Choices) == 0 {
return "", fmt.Errorf("no response from OpenAI")
}
return resp.Choices[0].Message.Content, nil
}
return "", fmt.Errorf("max retries exceeded")
}