MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / callAI

Method callAI

server-source-code/internal/ai/service.go:170–213  ·  view source on GitHub ↗
(settings *models.Settings, prompt string, opts *callOptions, systemPrompt, context string, history []Message)

Source from the content-addressed store, hash-verified

168}
169
170func (s *Service) callAI(settings *models.Settings, prompt string, opts *callOptions, systemPrompt, context string, history []Message) (string, error) {
171 if s.enc == nil {
172 return "", fmt.Errorf("encryption not configured")
173 }
174 if settings.AiAPIKey == nil || *settings.AiAPIKey == "" {
175 return "", fmt.Errorf("AI API key not configured")
176 }
177 apiKey, err := s.enc.Decrypt(*settings.AiAPIKey)
178 if err != nil || apiKey == "" {
179 return "", fmt.Errorf("failed to decrypt AI API key")
180 }
181
182 messages := []Message{{Role: "system", Content: systemPrompt}}
183 if context != "" {
184 messages = append(messages,
185 Message{Role: "user", Content: "Recent terminal output:\n```\n" + context + "\n```"},
186 Message{Role: "assistant", Content: "I've noted the terminal context. How can I help?"},
187 )
188 }
189 messages = append(messages, history...)
190 messages = append(messages, Message{Role: "user", Content: prompt})
191
192 provider := settings.AiProvider
193 if provider == "" {
194 provider = "openrouter"
195 }
196 model := ""
197 if settings.AiModel != nil {
198 model = *settings.AiModel
199 }
200
201 switch provider {
202 case "openrouter":
203 return callOpenRouter(apiKey, model, messages, opts)
204 case "anthropic":
205 return callAnthropic(apiKey, model, messages, opts)
206 case "openai":
207 return callOpenAI(apiKey, model, messages, opts)
208 case "gemini":
209 return callGemini(apiKey, model, messages, opts)
210 default:
211 return "", fmt.Errorf("unknown AI provider: %s", provider)
212 }
213}
214
215// OpenRouter
216func callOpenRouter(apiKey, model string, messages []Message, opts *callOptions) (string, error) {

Callers 2

GetAssistanceMethod · 0.95
GetCompletionMethod · 0.95

Calls 5

callOpenRouterFunction · 0.85
callAnthropicFunction · 0.85
callOpenAIFunction · 0.85
callGeminiFunction · 0.85
DecryptMethod · 0.80

Tested by

no test coverage detected