2023-04-22 16:23:56 -04:00
|
|
|
package llm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File represents a file in a git repository.
|
|
|
|
type File struct {
|
|
|
|
Path string
|
|
|
|
Contents string
|
|
|
|
}
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
type ResponseType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ResponseAnswer ResponseType = iota
|
|
|
|
ResponseCodeChange
|
|
|
|
)
|
|
|
|
|
2023-04-22 16:23:56 -04:00
|
|
|
// CodeChangeRequest contains all necessary information for generating a prompt for a LLM.
|
|
|
|
type CodeChangeRequest struct {
|
2023-05-12 00:59:21 -04:00
|
|
|
Files []File
|
|
|
|
Subject string
|
|
|
|
Body string
|
|
|
|
IssueNumber int
|
|
|
|
BaseBranch string
|
2023-04-22 16:23:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CodeChangeResponse contains data derived from an LLM response to a prompt generated via a CodeChangeRequest.
|
|
|
|
type CodeChangeResponse struct {
|
|
|
|
Files []File
|
|
|
|
Notes string
|
|
|
|
}
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
// TODO support threads
|
|
|
|
type DiffCommentRequest struct {
|
|
|
|
File File
|
|
|
|
Contents string
|
|
|
|
Diff string
|
2023-04-22 16:23:56 -04:00
|
|
|
}
|
|
|
|
|
2023-05-08 20:14:19 -04:00
|
|
|
type DiffCommentResponse struct {
|
|
|
|
Type ResponseType
|
|
|
|
Answer string
|
|
|
|
File File
|
2023-04-22 16:23:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseFiles process the "files" subsection of the LLM's response. It is a helper for GetCodeChangeResponse.
|
|
|
|
func parseFiles(filesSection string) []File {
|
2023-05-04 20:01:46 -04:00
|
|
|
fileStringList := strings.Split(filesSection, "ppname:")
|
|
|
|
if len(fileStringList) < 2 {
|
|
|
|
return []File{}
|
|
|
|
}
|
2023-04-22 16:23:56 -04:00
|
|
|
// first item in the list is just gonna be "Files:"
|
|
|
|
fileStringList = fileStringList[1:]
|
|
|
|
|
2023-05-02 22:07:10 -04:00
|
|
|
replacer := strings.NewReplacer(
|
|
|
|
"\\n", "\n",
|
|
|
|
"\\\"", "\"",
|
|
|
|
"```", "",
|
|
|
|
)
|
2023-04-22 16:23:56 -04:00
|
|
|
fileList := make([]File, len(fileStringList))
|
|
|
|
for i, f := range fileStringList {
|
2023-05-04 20:01:46 -04:00
|
|
|
fileParts := strings.Split(f, "ppcontents:")
|
|
|
|
if len(fileParts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-02 22:07:10 -04:00
|
|
|
path := replacer.Replace(fileParts[0])
|
|
|
|
path = strings.TrimSpace(path)
|
|
|
|
|
|
|
|
contents := replacer.Replace(fileParts[1])
|
|
|
|
contents = strings.TrimSpace(contents)
|
2023-04-22 16:23:56 -04:00
|
|
|
|
|
|
|
fileList[i] = File{
|
|
|
|
Path: path,
|
|
|
|
Contents: contents,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileList
|
|
|
|
}
|