-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompts.go
119 lines (100 loc) · 2.57 KB
/
prompts.go
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
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/go-yaml/yaml"
"github.com/pkg/errors"
)
type Shell struct {
Messages []Message `yaml:"messages"`
}
type Prompts struct {
Bash Shell `yaml:"bash"`
Powershell Shell `yaml:"powershell"`
Command struct {
Messages []Message `yaml:"messages"`
} `yaml:"command"`
Text struct {
Messages []Message `yaml:"messages"`
} `yaml:"text"`
}
type Mode int
const (
CommandMode Mode = iota
TextMode
)
func generateChatGPTMessages(userInput string, mode Mode) []Message {
shell := getShellCached()
shellVersion := getShellVersion(shell)
systemInfo := runtime.GOOS
workingDirectory, _ := os.Getwd()
packageManagers := []string{} // This should be implemented based on the OS
sudo := false // This should be implemented based on the OS
prompts := Prompts{}
aiHome := getAiHome()
promptsFilePath := filepath.Join(aiHome, "prompts.yaml")
promptsData, err := ioutil.ReadFile(promptsFilePath)
if err != nil {
log.Printf("Error reading prompts file: %s", promptsFilePath)
panic(err)
}
err = yaml.Unmarshal(promptsData, &prompts)
if err != nil {
panic(err)
}
shellMessages := prompts.Bash.Messages
if shell == "powershell" {
shellMessages = prompts.Powershell.Messages
}
var commonMessages []Message
if mode == CommandMode {
commonMessages = prompts.Command.Messages
} else {
commonMessages = prompts.Text.Messages
}
for i := range commonMessages {
commonMessages[i].Content = strings.NewReplacer(
"{shell}", shell,
"{shell_version}", shellVersion,
"{system_info}", systemInfo,
"{working_directory}", workingDirectory,
"{package_managers}", strings.Join(packageManagers, ", "),
"{sudo}", func() string {
if sudo {
return "sudo"
}
return "no sudo"
}(),
).Replace(commonMessages[i].Content)
}
userMessage := Message{
Role: "user",
Content: userInput,
}
var outputMessages []Message
// add common messages
outputMessages = append(outputMessages, commonMessages...)
// add shell messages if in command mode
if mode == CommandMode {
outputMessages = append(outputMessages, shellMessages...)
}
// add user message
outputMessages = append(outputMessages, userMessage)
return outputMessages
}
func getAiHome() string {
aiHome := os.Getenv("AI_HOME")
if aiHome == "" || strings.Contains(aiHome, "go-build") {
// Fallback: use the directory of the current file
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic(errors.New("Failed to get current file path"))
}
aiHome = filepath.Dir(filename)
}
return aiHome
}