package llm import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "strings" "time" ) const ( openRouterAPIURL = "https://openrouter.ai/api/v1/chat/completions" openRouterAPIModel = "thinkingmachines/inkling-small:free" ) type OpenRouterProvider struct { apiKey string client *http.Client } func NewOpenRouterProvider(apiKey string) *OpenRouterProvider { return &OpenRouterProvider{apiKey: apiKey, client: &http.Client{Timeout: 50 * time.Second}} } type openRouterMessage struct { Role string `json:"role"` Content string `json:"content"` } type openRouterAPIRequest struct { Model string `json:"model"` Messages []openRouterMessage `json:"messages"` Stream bool `json:"stream"` } type openRouterAPIResponse struct { ID string `json:"id"` Model string `json:"model"` Choices []openRouterChoice `json:"choices"` Usage *openRouterUsage `json:"usage,omitempty"` Error *openRouterAPIError `json:"error,omitempty"` } type openRouterChoice struct { FinishReason string `json:"finish_reason"` Message *openRouterMessage `json:"message,omitempty"` Text string `json:"text,omitempty"` } type openRouterUsage struct { PromptTokens int64 `json:"prompt_tokens"` CompletionTokens int64 `json:"completion_tokens"` TotalTokens int64 `json:"total_tokens"` } type openRouterAPIError struct { Message string `json:"message"` Code int `json:"code"` } func (p *OpenRouterProvider) Complete(ctx context.Context, systemPrompt, userPrompt string) (*Completion, error) { reqBody := openRouterAPIRequest{ Model: openRouterAPIModel, Messages: []openRouterMessage{ {Role: "system", Content: systemPrompt}, {Role: "user", Content: userPrompt}, }, Stream: false, } bodyBytes, err := json.Marshal(reqBody) if err != nil { return nil, fmt.Errorf("marshal request: %w", err) } req, err := http.NewRequestWithContext(ctx, http.MethodPost, openRouterAPIURL, bytes.NewReader(bodyBytes)) if err != nil { return nil, fmt.Errorf("build request: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+p.apiKey) resp, err := p.client.Do(req) if err != nil { return nil, fmt.Errorf("openrouter api request failed: %w", err) } defer resp.Body.Close() switch resp.StatusCode { case http.StatusOK: case http.StatusUnauthorized, http.StatusForbidden: return nil, ErrAuthFailed case http.StatusPaymentRequired, http.StatusTooManyRequests: return nil, ErrQuotaExceeded default: body, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, body) } var apiResp openRouterAPIResponse if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil { return nil, fmt.Errorf("decode response: %w", err) } if apiResp.Error != nil { return nil, fmt.Errorf("openrouter api error (code %d): %s", apiResp.Error.Code, apiResp.Error.Message) } text := extractOpenRouterText(apiResp) if strings.TrimSpace(text) == "" { return nil, fmt.Errorf("openrouter returned empty completion (model %q)", apiResp.Model) } completion := &Completion{Text: text, Model: apiResp.Model} if apiResp.Usage != nil { completion.Tokens = apiResp.Usage.TotalTokens } if completion.Model == "" { completion.Model = openRouterAPIModel } return completion, nil } func extractOpenRouterText(resp openRouterAPIResponse) string { var sb strings.Builder for _, choice := range resp.Choices { if choice.Message != nil && choice.Message.Content != "" { sb.WriteString(choice.Message.Content) continue } if choice.Text != "" { sb.WriteString(choice.Text) } } return sb.String() }