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

Function callOpenRouter

server-source-code/internal/ai/service.go:216–261  ·  view source on GitHub ↗

OpenRouter

(apiKey, model string, messages []Message, opts *callOptions)

Source from the content-addressed store, hash-verified

214
215// OpenRouter
216func callOpenRouter(apiKey, model string, messages []Message, opts *callOptions) (string, error) {
217 cfg := providers["openrouter"]
218 if model == "" {
219 model = cfg.defaultModel
220 }
221 body := map[string]interface{}{
222 "model": model,
223 "messages": messages,
224 "max_tokens": opts.maxTokens,
225 "temperature": opts.temperature,
226 "stream": false,
227 }
228 reqBody, _ := json.Marshal(body)
229 req, err := http.NewRequest("POST", cfg.baseURL+"/chat/completions", bytes.NewReader(reqBody))
230 if err != nil {
231 return "", err
232 }
233 req.Header.Set("Content-Type", "application/json")
234 req.Header.Set("Authorization", "Bearer "+apiKey)
235 req.Header.Set("HTTP-Referer", "https://patchmon.app")
236 req.Header.Set("X-Title", "PatchMon Terminal Assistant")
237
238 resp, err := http.DefaultClient.Do(req)
239 if err != nil {
240 return "", err
241 }
242 defer func() { _ = resp.Body.Close() }()
243 data, _ := io.ReadAll(resp.Body)
244 if resp.StatusCode != http.StatusOK {
245 return "", fmt.Errorf("OpenRouter API error: %d - %s", resp.StatusCode, string(data))
246 }
247 var out struct {
248 Choices []struct {
249 Message struct {
250 Content string `json:"content"`
251 } `json:"message"`
252 } `json:"choices"`
253 }
254 if err := json.Unmarshal(data, &out); err != nil {
255 return "", err
256 }
257 if len(out.Choices) > 0 {
258 return out.Choices[0].Message.Content, nil
259 }
260 return "", nil
261}
262
263// Anthropic
264func callAnthropic(apiKey, model string, messages []Message, opts *callOptions) (string, error) {

Callers 1

callAIMethod · 0.85

Calls 3

UnmarshalMethod · 0.80
SetMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected