-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuggest_command_simple.go
More file actions
347 lines (293 loc) · 9.61 KB
/
suggest_command_simple.go
File metadata and controls
347 lines (293 loc) · 9.61 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
// SuggestManager handles natural language command suggestions
type SuggestManager struct {
aiManager *AIPredictionManager
historyAnalyzer *HistoryAnalyzer
lastQuery string
lastSuggestions []CommandSuggestion
}
// NewSuggestManager creates a new suggestion manager
func NewSuggestManager() *SuggestManager {
return &SuggestManager{}
}
// Initialize sets up the suggest manager with dependencies
func (sm *SuggestManager) Initialize() error {
sm.aiManager = GetAIManager()
sm.historyAnalyzer = GetHistoryAnalyzer()
return nil
}
// GetSuggestions generates command suggestions from natural language input
func (sm *SuggestManager) GetSuggestions(query string, limit int) ([]CommandSuggestion, error) {
suggestions := []CommandSuggestion{}
// Get current context
pwd, _ := os.Getwd()
projectType := sm.detectProjectType(pwd)
// Try pattern-based suggestions first
patternSuggestions := sm.getPatternBasedSuggestions(query, projectType)
suggestions = append(suggestions, patternSuggestions...)
// If AI is available, get AI-powered suggestions
if sm.aiManager != nil && sm.aiManager.IsEnabled() {
aiSuggestions, err := sm.getAISuggestions(query, projectType)
if err == nil {
suggestions = append(suggestions, aiSuggestions...)
}
}
// Get history-based suggestions if history analyzer is available
if sm.historyAnalyzer != nil && sm.historyAnalyzer.IsEnabled() {
historySuggestions := sm.getHistoryBasedSuggestions(query, pwd)
suggestions = append(suggestions, historySuggestions...)
}
// Limit results
if len(suggestions) > limit {
suggestions = suggestions[:limit]
}
sm.lastQuery = query
sm.lastSuggestions = suggestions
return suggestions, nil
}
// detectProjectType identifies the type of project in the current directory
func (sm *SuggestManager) detectProjectType(dir string) string {
// Check for various project indicators
indicators := map[string]string{
"package.json": "nodejs",
"go.mod": "golang",
"Cargo.toml": "rust",
"pom.xml": "java",
"build.gradle": "java",
"requirements.txt": "python",
"Pipfile": "python",
"composer.json": "php",
"Gemfile": "ruby",
"Makefile": "make",
"Dockerfile": "docker",
".git": "git",
}
for file, projectType := range indicators {
if _, err := os.Stat(filepath.Join(dir, file)); err == nil {
return projectType
}
}
return "general"
}
// getPatternBasedSuggestions uses keyword patterns to suggest commands
func (sm *SuggestManager) getPatternBasedSuggestions(query string, projectType string) []CommandSuggestion {
suggestions := []CommandSuggestion{}
queryLower := strings.ToLower(query)
// Simple keyword matching
if strings.Contains(queryLower, "list") || strings.Contains(queryLower, "files") {
suggestions = append(suggestions, CommandSuggestion{
Command: "ls -la",
Confidence: 0.9,
Reason: "List all files with details",
})
}
if strings.Contains(queryLower, "find") || strings.Contains(queryLower, "search") {
suggestions = append(suggestions, CommandSuggestion{
Command: "find . -name \"*pattern*\"",
Confidence: 0.8,
Reason: "Find files by name pattern",
})
suggestions = append(suggestions, CommandSuggestion{
Command: "grep -r \"text\" .",
Confidence: 0.7,
Reason: "Search for text in files",
})
}
if strings.Contains(queryLower, "git") && strings.Contains(queryLower, "commit") {
suggestions = append(suggestions, CommandSuggestion{
Command: "git add . && git commit -m \"message\"",
Confidence: 0.9,
Reason: "Stage and commit changes",
})
}
if strings.Contains(queryLower, "install") {
switch projectType {
case "nodejs":
suggestions = append(suggestions, CommandSuggestion{
Command: "npm install",
Confidence: 0.9,
Reason: "Install Node.js dependencies",
})
case "golang":
suggestions = append(suggestions, CommandSuggestion{
Command: "go mod download",
Confidence: 0.9,
Reason: "Download Go dependencies",
})
case "python":
suggestions = append(suggestions, CommandSuggestion{
Command: "pip install -r requirements.txt",
Confidence: 0.9,
Reason: "Install Python dependencies",
})
}
}
if strings.Contains(queryLower, "build") {
switch projectType {
case "golang":
suggestions = append(suggestions, CommandSuggestion{
Command: "go build",
Confidence: 0.9,
Reason: "Build Go project",
})
case "make":
suggestions = append(suggestions, CommandSuggestion{
Command: "make build",
Confidence: 0.9,
Reason: "Run make build target",
})
}
}
if strings.Contains(queryLower, "test") {
switch projectType {
case "golang":
suggestions = append(suggestions, CommandSuggestion{
Command: "go test ./...",
Confidence: 0.9,
Reason: "Run all Go tests",
})
case "nodejs":
suggestions = append(suggestions, CommandSuggestion{
Command: "npm test",
Confidence: 0.9,
Reason: "Run Node.js tests",
})
}
}
return suggestions
}
// getAISuggestions uses AI to generate command suggestions
func (sm *SuggestManager) getAISuggestions(query string, projectType string) ([]CommandSuggestion, error) {
if sm.aiManager == nil || !sm.aiManager.IsEnabled() {
return []CommandSuggestion{}, nil
}
// Create a prompt for the AI
prompt := fmt.Sprintf(`Given the user wants to: "%s"
Current project type: %s
Current directory: %s
Suggest 3 shell commands that would accomplish this task. For each command:
1. Provide the exact command
2. Brief description of what it does
Format each suggestion as:
COMMAND: <command>
DESC: <description>
---`, query, projectType, filepath.Base(getCurrentDirectory()))
response, err := sm.aiManager.ollamaClient.Generate(prompt, "You are a command-line expert assistant. Provide practical, safe command suggestions.")
if err != nil {
return []CommandSuggestion{}, err
}
// Parse AI response
suggestions := sm.parseAIResponse(response)
return suggestions, nil
}
// parseAIResponse parses the AI response into command suggestions
func (sm *SuggestManager) parseAIResponse(response string) []CommandSuggestion {
suggestions := []CommandSuggestion{}
// Split by separator
parts := strings.Split(response, "---")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
suggestion := CommandSuggestion{}
lines := strings.Split(part, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "COMMAND:") {
suggestion.Command = strings.TrimSpace(strings.TrimPrefix(line, "COMMAND:"))
} else if strings.HasPrefix(line, "DESC:") {
suggestion.Reason = strings.TrimSpace(strings.TrimPrefix(line, "DESC:"))
}
}
if suggestion.Command != "" {
suggestion.Confidence = 0.7 // AI suggestions get slightly lower confidence
suggestions = append(suggestions, suggestion)
}
}
return suggestions
}
// getHistoryBasedSuggestions uses command history to suggest commands
func (sm *SuggestManager) getHistoryBasedSuggestions(query string, currentDir string) []CommandSuggestion {
if sm.historyAnalyzer == nil || !sm.historyAnalyzer.IsEnabled() {
return []CommandSuggestion{}
}
// Use the history analyzer's existing suggestion mechanism
suggestions := sm.historyAnalyzer.GetSuggestions(currentDir)
// Filter based on query keywords
queryLower := strings.ToLower(query)
filtered := []CommandSuggestion{}
for _, suggestion := range suggestions {
cmdLower := strings.ToLower(suggestion.Command)
// Check if command contains any query keywords
words := strings.Fields(queryLower)
matches := false
for _, word := range words {
if strings.Contains(cmdLower, word) {
matches = true
break
}
}
if matches {
filtered = append(filtered, suggestion)
}
}
return filtered
}
// ExplainCommand provides detailed explanation of a command
func (sm *SuggestManager) ExplainCommand(command string) (string, error) {
if sm.aiManager != nil && sm.aiManager.IsEnabled() {
prompt := fmt.Sprintf(`Explain this command in detail: %s
Include:
1. What the command does
2. Each flag/option explanation
3. Common use cases
4. Potential risks or warnings`, command)
return sm.aiManager.ollamaClient.Generate(prompt, "You are a command-line expert. Provide clear, detailed explanations.")
}
// Fallback to basic explanation
parts := strings.Fields(command)
if len(parts) == 0 {
return "Empty command", nil
}
explanation := fmt.Sprintf("Command: %s\n", parts[0])
// Add basic explanations for common commands
commonExplanations := map[string]string{
"ls": "Lists directory contents",
"cd": "Changes the current directory",
"cp": "Copies files or directories",
"mv": "Moves or renames files",
"rm": "Removes files or directories",
"git": "Version control system command",
"docker": "Container management command",
"npm": "Node.js package manager",
"make": "Build automation tool",
}
if desc, ok := commonExplanations[parts[0]]; ok {
explanation += "Description: " + desc
}
return explanation, nil
}
// getCurrentDirectory safely gets the current working directory
func getCurrentDirectory() string {
dir, err := os.Getwd()
if err != nil {
return ""
}
return dir
}
// GetLastSuggestions returns the last generated suggestions
func (sm *SuggestManager) GetLastSuggestions() []CommandSuggestion {
return sm.lastSuggestions
}
// ClearCache clears any cached data (simplified version has no cache)
func (sm *SuggestManager) ClearCache() {
// No cache in simplified version
}