|
| 1 | +package review |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +// Finding is one structured review comment from the LLM. |
| 13 | +type Finding struct { |
| 14 | + File string `json:"file"` |
| 15 | + Line int `json:"line,omitempty"` |
| 16 | + Severity string `json:"severity"` // info | low | medium | high | critical |
| 17 | + Comment string `json:"comment"` |
| 18 | +} |
| 19 | + |
| 20 | +// Report is the structured LLM output. Both CLI (markdown) and MCP (JSON) |
| 21 | +// paths consume this shape. |
| 22 | +type Report struct { |
| 23 | + Summary string `json:"summary"` |
| 24 | + Findings []Finding `json:"findings"` |
| 25 | + Model string `json:"model"` |
| 26 | + RequestID string `json:"request_id,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +// SystemPrompt is the single system message we use for every review. |
| 30 | +// Plan §3.1 — "use the structured graph evidence to find correctness, |
| 31 | +// security, and architectural issues". |
| 32 | +const SystemPrompt = `You are reviewing a pull request. Use the structured graph evidence to find correctness, security, and architectural issues. ` + |
| 33 | + `Return strictly JSON in this shape: ` + |
| 34 | + `{"summary": "<one-paragraph overview>", "findings": [{"file": "<path>", "line": <int>, "severity": "info|low|medium|high|critical", "comment": "<message>"}]}. ` + |
| 35 | + `No prose before or after the JSON. ` + |
| 36 | + `If the diff is trivial, return an empty findings array — do NOT invent issues.` |
| 37 | + |
| 38 | +// Client wraps the OpenAI-compatible /chat/completions endpoint exposed |
| 39 | +// by Ollama, Ollama Cloud, and proxies. The HTTPClient field is exported |
| 40 | +// so tests can inject a stub. |
| 41 | +type Client struct { |
| 42 | + Config Config |
| 43 | + HTTPClient *http.Client |
| 44 | +} |
| 45 | + |
| 46 | +// NewClient returns a Client with cfg and a default *http.Client. |
| 47 | +func NewClient(cfg Config) *Client { |
| 48 | + return &Client{ |
| 49 | + Config: cfg, |
| 50 | + HTTPClient: &http.Client{Timeout: cfg.Timeout}, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +type chatRequest struct { |
| 55 | + Model string `json:"model"` |
| 56 | + Messages []chatMessage `json:"messages"` |
| 57 | + Stream bool `json:"stream"` |
| 58 | +} |
| 59 | + |
| 60 | +type chatMessage struct { |
| 61 | + Role string `json:"role"` |
| 62 | + Content string `json:"content"` |
| 63 | +} |
| 64 | + |
| 65 | +type chatResponse struct { |
| 66 | + ID string `json:"id"` |
| 67 | + Model string `json:"model"` |
| 68 | + Choices []struct { |
| 69 | + Message chatMessage `json:"message"` |
| 70 | + } `json:"choices"` |
| 71 | +} |
| 72 | + |
| 73 | +// Review sends the assembled prompt to the LLM and parses the structured |
| 74 | +// reply into a Report. The user prompt should already include the diff |
| 75 | +// + evidence pack rendering. |
| 76 | +func (c *Client) Review(ctx context.Context, userPrompt string) (*Report, error) { |
| 77 | + body, err := json.Marshal(chatRequest{ |
| 78 | + Model: c.Config.Model, |
| 79 | + Stream: false, |
| 80 | + Messages: []chatMessage{ |
| 81 | + {Role: "system", Content: SystemPrompt}, |
| 82 | + {Role: "user", Content: userPrompt}, |
| 83 | + }, |
| 84 | + }) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("marshal request: %w", err) |
| 87 | + } |
| 88 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Config.Endpoint+"/chat/completions", bytes.NewReader(body)) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + req.Header.Set("Content-Type", "application/json") |
| 93 | + if c.Config.APIKey != "" { |
| 94 | + req.Header.Set("Authorization", "Bearer "+c.Config.APIKey) |
| 95 | + } |
| 96 | + resp, err := c.HTTPClient.Do(req) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("LLM call: %w", err) |
| 99 | + } |
| 100 | + defer resp.Body.Close() |
| 101 | + raw, err := io.ReadAll(resp.Body) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + if resp.StatusCode >= 400 { |
| 106 | + return nil, fmt.Errorf("LLM HTTP %d: %s", resp.StatusCode, snippet(string(raw))) |
| 107 | + } |
| 108 | + var cr chatResponse |
| 109 | + if err := json.Unmarshal(raw, &cr); err != nil { |
| 110 | + return nil, fmt.Errorf("decode chat response: %w (body: %s)", err, snippet(string(raw))) |
| 111 | + } |
| 112 | + if len(cr.Choices) == 0 { |
| 113 | + return nil, fmt.Errorf("LLM returned no choices: %s", snippet(string(raw))) |
| 114 | + } |
| 115 | + var rep Report |
| 116 | + content := cr.Choices[0].Message.Content |
| 117 | + if err := json.Unmarshal([]byte(content), &rep); err != nil { |
| 118 | + return nil, fmt.Errorf("LLM did not return strict JSON: %w (content: %s)", err, snippet(content)) |
| 119 | + } |
| 120 | + if rep.Model == "" { |
| 121 | + rep.Model = cr.Model |
| 122 | + } |
| 123 | + return &rep, nil |
| 124 | +} |
| 125 | + |
| 126 | +func snippet(s string) string { |
| 127 | + const max = 500 |
| 128 | + if len(s) > max { |
| 129 | + return s[:max] + "..." |
| 130 | + } |
| 131 | + return s |
| 132 | +} |
0 commit comments