|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + |
| 10 | + "github.com/yanmxa/gencode/internal/message" |
| 11 | +) |
| 12 | + |
| 13 | +// promptSuggestionMsg carries the result of a background suggestion generation. |
| 14 | +type promptSuggestionMsg struct { |
| 15 | + text string |
| 16 | + err error |
| 17 | +} |
| 18 | + |
| 19 | +// promptSuggestionState holds ghost text suggestion state. |
| 20 | +type promptSuggestionState struct { |
| 21 | + text string |
| 22 | + cancel context.CancelFunc |
| 23 | +} |
| 24 | + |
| 25 | +func (s *promptSuggestionState) Clear() { |
| 26 | + s.text = "" |
| 27 | + if s.cancel != nil { |
| 28 | + s.cancel() |
| 29 | + s.cancel = nil |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const suggestionSystemPrompt = `You predict what the user will type next in a coding assistant CLI. |
| 34 | +Reply with ONLY the predicted text (2-12 words). No quotes, no explanation. |
| 35 | +If unsure, reply with nothing.` |
| 36 | + |
| 37 | +const suggestionUserPrompt = `[PREDICTION MODE] Based on this conversation, predict what the user will type next. |
| 38 | +Stay silent if the next step isn't obvious. Match the user's language and style.` |
| 39 | + |
| 40 | +const maxSuggestionMessages = 20 |
| 41 | + |
| 42 | +// startPromptSuggestion launches a background API call to generate a prompt suggestion. |
| 43 | +func (m *model) startPromptSuggestion() tea.Cmd { |
| 44 | + if m.loop.Client == nil { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + // Need at least 2 assistant messages for meaningful suggestion |
| 49 | + assistantCount := 0 |
| 50 | + for _, msg := range m.conv.Messages { |
| 51 | + if msg.Role == message.RoleAssistant { |
| 52 | + assistantCount++ |
| 53 | + } |
| 54 | + } |
| 55 | + if assistantCount < 2 { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + // Cancel any prior in-flight suggestion |
| 60 | + m.promptSuggestion.Clear() |
| 61 | + |
| 62 | + ctx, cancel := context.WithCancel(context.Background()) |
| 63 | + m.promptSuggestion.cancel = cancel |
| 64 | + |
| 65 | + // Convert only the last N messages to keep the API call lightweight |
| 66 | + startIdx := 0 |
| 67 | + if len(m.conv.Messages) > maxSuggestionMessages { |
| 68 | + startIdx = len(m.conv.Messages) - maxSuggestionMessages |
| 69 | + } |
| 70 | + msgs := m.conv.ConvertToProviderFrom(startIdx) |
| 71 | + |
| 72 | + // Append prediction request |
| 73 | + msgs = append(msgs, message.Message{ |
| 74 | + Role: message.RoleUser, |
| 75 | + Content: suggestionUserPrompt, |
| 76 | + }) |
| 77 | + |
| 78 | + client := m.loop.Client |
| 79 | + |
| 80 | + return func() tea.Msg { |
| 81 | + resp, err := client.Complete(ctx, suggestionSystemPrompt, msgs, 60) |
| 82 | + if err != nil { |
| 83 | + return promptSuggestionMsg{err: err} |
| 84 | + } |
| 85 | + return promptSuggestionMsg{text: resp.Content} |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// handlePromptSuggestion processes the suggestion result. |
| 90 | +func (m *model) handlePromptSuggestion(msg promptSuggestionMsg) { |
| 91 | + if msg.err != nil { |
| 92 | + return |
| 93 | + } |
| 94 | + // Discard if user already started typing |
| 95 | + if m.input.Textarea.Value() != "" { |
| 96 | + return |
| 97 | + } |
| 98 | + // Discard if streaming is active |
| 99 | + if m.conv.Stream.Active { |
| 100 | + return |
| 101 | + } |
| 102 | + if text := filterSuggestion(msg.text); text != "" { |
| 103 | + m.promptSuggestion.text = text |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +var ( |
| 108 | + aiVoicePrefixes = []string{ |
| 109 | + "i'll", "i will", "let me", "here's", "here is", "here are", |
| 110 | + "i can", "i would", "i think", "i notice", "i'm", |
| 111 | + "that's", "this is", "this will", |
| 112 | + "you can", "you should", "you could", |
| 113 | + "sure,", "of course", "certainly", |
| 114 | + } |
| 115 | + evaluativePhrases = []string{ |
| 116 | + "thanks", "thank you", "looks good", "sounds good", |
| 117 | + "that works", "that worked", "that's all", |
| 118 | + "nice", "great", "perfect", "makes sense", |
| 119 | + "awesome", "excellent", "good job", |
| 120 | + } |
| 121 | + prefixedLabelRe = regexp.MustCompile(`^\w+:\s`) |
| 122 | + multipleSentencesRe = regexp.MustCompile(`[.!?]\s+[A-Z]`) |
| 123 | +) |
| 124 | + |
| 125 | +// filterSuggestion validates and cleans a suggestion. Returns "" if invalid. |
| 126 | +func filterSuggestion(text string) string { |
| 127 | + text = strings.TrimSpace(text) |
| 128 | + // Remove surrounding quotes if present |
| 129 | + if len(text) >= 2 && (text[0] == '"' && text[len(text)-1] == '"' || |
| 130 | + text[0] == '\'' && text[len(text)-1] == '\'') { |
| 131 | + text = text[1 : len(text)-1] |
| 132 | + text = strings.TrimSpace(text) |
| 133 | + } |
| 134 | + |
| 135 | + if text == "" { |
| 136 | + return "" |
| 137 | + } |
| 138 | + if len(text) > 100 { |
| 139 | + return "" |
| 140 | + } |
| 141 | + words := strings.Fields(text) |
| 142 | + if len(words) > 12 { |
| 143 | + return "" |
| 144 | + } |
| 145 | + // Reject single words unless they are common short commands |
| 146 | + if len(words) < 2 && !isAllowedSingleWord(text) { |
| 147 | + return "" |
| 148 | + } |
| 149 | + // Reject markdown or multi-line |
| 150 | + if strings.ContainsAny(text, "*\n") { |
| 151 | + return "" |
| 152 | + } |
| 153 | + // Reject multiple sentences |
| 154 | + if multipleSentencesRe.MatchString(text) { |
| 155 | + return "" |
| 156 | + } |
| 157 | + // Reject prefixed labels like "Action: ..." |
| 158 | + if prefixedLabelRe.MatchString(text) { |
| 159 | + return "" |
| 160 | + } |
| 161 | + |
| 162 | + lower := strings.ToLower(text) |
| 163 | + |
| 164 | + // Reject AI voice |
| 165 | + for _, prefix := range aiVoicePrefixes { |
| 166 | + if strings.HasPrefix(lower, prefix) { |
| 167 | + return "" |
| 168 | + } |
| 169 | + } |
| 170 | + // Reject evaluative |
| 171 | + for _, phrase := range evaluativePhrases { |
| 172 | + if strings.Contains(lower, phrase) { |
| 173 | + return "" |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return text |
| 178 | +} |
| 179 | + |
| 180 | +var allowedSingleWords = map[string]bool{ |
| 181 | + "yes": true, "yeah": true, "yep": true, "yup": true, |
| 182 | + "no": true, "sure": true, "ok": true, "okay": true, |
| 183 | + "push": true, "commit": true, "deploy": true, |
| 184 | + "stop": true, "continue": true, "check": true, |
| 185 | + "exit": true, "quit": true, |
| 186 | +} |
| 187 | + |
| 188 | +func isAllowedSingleWord(word string) bool { |
| 189 | + if strings.HasPrefix(word, "/") { |
| 190 | + return true |
| 191 | + } |
| 192 | + return allowedSingleWords[strings.ToLower(word)] |
| 193 | +} |
0 commit comments