@@ -4,16 +4,18 @@ Copyright © 2023 Chandler <chandler@chand1012.dev>
4
4
package cmd
5
5
6
6
import (
7
+ "context"
7
8
"fmt"
8
9
"os"
9
10
"strings"
10
11
11
- "github.com/chand1012/ottodocs/pkg/ai "
12
+ "github.com/chand1012/ottodocs/pkg/calc "
12
13
"github.com/chand1012/ottodocs/pkg/config"
13
14
"github.com/chand1012/ottodocs/pkg/constants"
14
15
"github.com/chand1012/ottodocs/pkg/textfile"
15
16
"github.com/chand1012/ottodocs/pkg/utils"
16
17
l "github.com/charmbracelet/log"
18
+ "github.com/sashabaranov/go-openai"
17
19
"github.com/spf13/cobra"
18
20
)
19
21
@@ -84,11 +86,14 @@ Example: otto edit main.go --start 1 --end 10 --goal "Refactor the function"`,
84
86
}
85
87
}
86
88
89
+ var messages []openai.ChatCompletionMessage
90
+ var newCode string
91
+
87
92
var prompt string
88
93
if editCode != "" {
89
- prompt = constants . EDIT_CODE_PROMPT + " \n EDIT : " + editCode + "\n \n GOAL: " + chatPrompt + "\n \n FILE: " + filePath + "\n \n " + contents
94
+ prompt = "EDIT : " + editCode + "\n \n GOAL: " + chatPrompt + "\n \n FILE: " + filePath + "\n \n " + contents + " \n \n Be sure to only output the edited code, do not print the entire file."
90
95
} else {
91
- prompt = constants . EDIT_CODE_PROMPT + " \n GOAL : " + chatPrompt + "\n \n FILE: " + filePath + "\n \n " + contents
96
+ prompt = "GOAL : " + chatPrompt + "\n \n FILE: " + filePath + "\n \n " + contents
92
97
}
93
98
94
99
if len (contextFiles ) > 0 {
@@ -103,35 +108,87 @@ Example: otto edit main.go --start 1 --end 10 --goal "Refactor the function"`,
103
108
}
104
109
}
105
110
106
- stream , err := ai .SimpleStreamRequest (prompt , c )
107
- if err != nil {
108
- log .Errorf ("Error requesting from OpenAI: %s" , err )
109
- os .Exit (1 )
111
+ client := openai .NewClient (c .APIKey )
112
+
113
+ messages = []openai.ChatCompletionMessage {
114
+ {
115
+ Role : openai .ChatMessageRoleSystem ,
116
+ Content : constants .EDIT_CODE_PROMPT ,
117
+ },
118
+ {
119
+ Role : openai .ChatMessageRoleUser ,
120
+ Content : prompt ,
121
+ },
110
122
}
111
123
112
- // print the response
113
- utils .PrintColoredTextLn ("New Code:" , c .OttoColor )
114
- newCode , err := utils .PrintChatCompletionStream (stream )
115
- if err != nil {
116
- log .Errorf ("Error printing chat completion stream: %s" , err )
117
- os .Exit (1 )
118
- }
124
+ for {
119
125
120
- confirmMsg := "Would you like to overwrite the file with the new code? (y/N): "
121
- if appendFile {
122
- confirmMsg = "Would you like to append the new code to the file? (y/N): "
123
- }
126
+ stream , err := client .CreateChatCompletionStream (context .Background (), openai.ChatCompletionRequest {
127
+ Model : c .Model ,
128
+ Messages : messages ,
129
+ })
130
+
131
+ if err != nil {
132
+ log .Errorf ("Error requesting from OpenAI: %s" , err )
133
+ os .Exit (1 )
134
+ }
124
135
125
- if ! force {
126
- confirm , err := utils .Input (confirmMsg )
136
+ // print the response
137
+ utils .PrintColoredTextLn ("New Code:" , c .OttoColor )
138
+ newCode , err = utils .PrintChatCompletionStream (stream )
127
139
if err != nil {
128
- log .Errorf ("Error getting input : %s" , err )
140
+ log .Errorf ("Error printing chat completion stream : %s" , err )
129
141
os .Exit (1 )
130
142
}
131
143
132
- confirm = strings .ToLower (confirm )
133
- if confirm != "y" && confirm != "yes" {
134
- os .Exit (0 )
144
+ confirmMsg := "Would you like to write the file with the new code? (y/N). Type your input to keep editing: "
145
+ if appendFile {
146
+ confirmMsg = "Would you like to append the new code to the file? (y/N). Type your input to keep editing: "
147
+ }
148
+
149
+ if ! force {
150
+ confirm , err := utils .Input (confirmMsg )
151
+ if err != nil {
152
+ log .Errorf ("Error getting input: %s" , err )
153
+ os .Exit (1 )
154
+ }
155
+
156
+ confirm = strings .ToLower (confirm )
157
+ if confirm == "n" || confirm == "no" {
158
+ os .Exit (0 )
159
+ } else if confirm == "y" || confirm == "yes" {
160
+ break
161
+ } else {
162
+ codeTokens , err := calc .PreciseTokens (newCode )
163
+ if err != nil {
164
+ log .Errorf ("Error calculating tokens: %s" , err )
165
+ os .Exit (1 )
166
+ }
167
+
168
+ maxTokens := calc .GetMaxTokens (c .Model ) - codeTokens
169
+
170
+ var newMessages []openai.ChatCompletionMessage
171
+ newMessages = []openai.ChatCompletionMessage {
172
+ {
173
+ Role : openai .ChatMessageRoleUser ,
174
+ Content : "Use the following input to edit the code: " + confirm ,
175
+ },
176
+ {
177
+ Role : openai .ChatMessageRoleAssistant ,
178
+ Content : newCode ,
179
+ },
180
+ }
181
+ utils .ReverseSlice (messages )
182
+ for _ , message := range messages {
183
+ if calc .PreciseTokensFromMessages (newMessages , c .Model ) < maxTokens {
184
+ newMessages = append (newMessages , message )
185
+ }
186
+ }
187
+ utils .ReverseSlice (newMessages )
188
+ messages = newMessages
189
+ utils .PrintColoredText ("Otto: " , c .OttoColor )
190
+ fmt .Println ("Ok! Here is the new code, taking your input into account." )
191
+ }
135
192
}
136
193
}
137
194
0 commit comments