-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
213 lines (183 loc) · 5.16 KB
/
main.go
File metadata and controls
213 lines (183 loc) · 5.16 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
209
210
211
212
213
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
"github.com/eiannone/keyboard"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
// Function to get the operating system
func getOS() string {
osName := runtime.GOOS
switch osName {
case "windows":
return "Windows (WSL/Ubuntu)" // Assume WSL if on Windows
case "darwin":
return "macOS"
case "linux":
return "Linux"
default:
return "Unknown"
}
}
// Function to read Zsh history (updated to handle both Linux and macOS)
func readZshHistory(n int) ([]string, error) {
historyFile := os.Getenv("HISTFILE")
if historyFile == "" {
// Default Zsh history file locations
if runtime.GOOS == "darwin" {
historyFile = os.Getenv("HOME") + "/.zsh_history"
} else { // Assume Linux for other OSes
historyFile = os.Getenv("HOME") + "/.zsh_history"
}
}
file, err := os.Open(historyFile)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
// Extract only the command portion from the history lines
var commands []string
for _, line := range lines {
parts := strings.SplitN(line, ";", 2)
if len(parts) == 2 {
commands = append(commands, parts[1])
}
}
// Get the last n commands
startIndex := len(commands) - n
if startIndex < 0 {
startIndex = 0
}
return commands[startIndex:], nil
}
// build the prompt and send it to Gemini
func getGeminiResponse(ctx context.Context, client *genai.Client, osName string, history []string, prompt string) ([]string, error) {
// Build the prompt with OS, history, and user request
var fullPrompt strings.Builder
fullPrompt.WriteString(fmt.Sprintf("You are an expert at bash command line for %s. ", osName))
fullPrompt.WriteString("Here is my zsh command history for context:\n")
for _, h := range history {
fullPrompt.WriteString(h)
fullPrompt.WriteString("\n")
}
fullPrompt.WriteString("Based on this history and the following request, generate ONLY three bash commands in your response, each on a new line, and nothing else, no other text, that accomplish what is described: ")
fullPrompt.WriteString(prompt)
model := client.GenerativeModel("gemini-pro")
resp, err := model.GenerateContent(ctx, genai.Text(fullPrompt.String()))
if err != nil {
return nil, err
}
// Parse the response to extract the three commands
var commands []string
if len(resp.Candidates) > 0 && len(resp.Candidates[0].Content.Parts) > 0 {
responseContent := fmt.Sprintf("%s", resp.Candidates[0].Content.Parts[0])
lines := strings.Split(responseContent, "\n")
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
if trimmedLine != "" {
commands = append(commands, trimmedLine)
}
}
} else {
return nil, fmt.Errorf("empty response from Gemini API")
}
return commands, nil
}
// execute the command
func executeCommand(command string) error {
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// show some suggestions
func printSuggestions(suggestions []string, selectedIndex int) {
for i, suggestion := range suggestions {
if i == selectedIndex {
fmt.Printf("-> %s\n", suggestion)
} else {
fmt.Printf(" %s\n", suggestion)
}
}
}
//
func handleKeyPress(suggestions []string, selectedIndex int) (int, bool) {
_, key, err := keyboard.GetKey()
if err != nil {
log.Fatal(err)
}
switch key {
case keyboard.KeyTab:
return (selectedIndex + 1) % len(suggestions), false
case keyboard.KeyEnter:
executeCommand(suggestions[selectedIndex])
return selectedIndex, true
case keyboard.KeyEsc:
fmt.Println("\nSuggestions rejected.")
return selectedIndex, true
default:
fmt.Println("\nExiting.")
return selectedIndex, true
}
}
func main() {
apiKey := os.Getenv("GOOGLE_API_KEY")
if apiKey == "" {
log.Fatal("Error: GOOGLE_API_KEY environment variable not set")
}
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
log.Fatal("Error creating Gemini client:", err)
}
defer client.Close()
if len(os.Args) < 2 {
fmt.Println("Usage: please <your request>")
return
}
userRequest := strings.Join(os.Args[1:], " ")
history, err := readZshHistory(10)
if err != nil {
log.Fatal("Error reading Zsh history:", err)
}
osName := getOS()
suggestions, err := getGeminiResponse(ctx, client, osName, history, userRequest)
if err != nil {
log.Fatal("Error getting response from Gemini:", err)
}
if len(suggestions) == 0 {
fmt.Println("No suggestions available.")
return
}
if err := keyboard.Open(); err != nil {
log.Fatal(err)
}
defer keyboard.Close()
fmt.Println("Suggestions (use Tab to cycle, Enter to accept, Esc to reject):")
printSuggestions(suggestions, 0)
selectedIndex := 0
for {
selectedIndex, shouldExit := handleKeyPress(suggestions, selectedIndex)
if shouldExit {
break
}
fmt.Print("\033[H\033[2J") // Clear the screen
fmt.Println("Suggestions (use Tab to cycle, Enter to accept, Esc to reject):")
printSuggestions(suggestions, selectedIndex)
}
}