summaryrefslogtreecommitdiff
path: root/internal/llm/anthropic.go
blob: 6f14eeaef90bb8c069dda1fc5e6933a843e6393c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Summary: Anthropic client implementation using Messages API with optional streaming support.
package llm

import (
	"bufio"
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"strings"
	"time"

	"codeberg.org/snonux/hexai/internal/logging"
)

// anthropicClient implements Client against Anthropic's Messages API.
type anthropicClient struct {
	httpClient         *http.Client
	apiKey             string
	baseURL            string
	defaultModel       string
	chatLogger         logging.ChatLogger
	defaultTemperature *float64
}

type anthropicChatRequest struct {
	Model       string               `json:"model"`
	Messages    []anthropicMessage   `json:"messages"`
	Temperature *float64             `json:"temperature,omitempty"`
	MaxTokens   int                  `json:"max_tokens"`
	Stream      bool                 `json:"stream,omitempty"`
	System      string               `json:"system,omitempty"`
}

type anthropicMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type anthropicChatResponse struct {
	ID      string `json:"id"`
	Type    string `json:"type"`
	Content []struct {
		Type string `json:"type"`
		Text string `json:"text"`
	} `json:"content"`
	StopReason string `json:"stop_reason"`
	Error      *struct {
		Type    string `json:"type"`
		Message string `json:"message"`
	} `json:"error,omitempty"`
}

// Streaming event types
type anthropicStreamStart struct {
	Type    string `json:"type"`
	Message struct {
		ID    string `json:"id"`
		Type  string `json:"type"`
		Role  string `json:"role"`
		Model string `json:"model"`
	} `json:"message"`
}

type anthropicStreamDelta struct {
	Type  string `json:"type"`
	Delta struct {
		Type string `json:"type"`
		Text string `json:"text"`
	} `json:"delta"`
}

type anthropicStreamError struct {
	Type  string `json:"type"`
	Error struct {
		Type    string `json:"type"`
		Message string `json:"message"`
	} `json:"error"`
}

// Constructor
// newAnthropic constructs an Anthropic client using explicit configuration values.
// The apiKey may be empty; calls will fail until a valid key is supplied.
func newAnthropic(baseURL, model, apiKey string, defaultTemp *float64) Client {
	if strings.TrimSpace(baseURL) == "" {
		baseURL = "https://api.anthropic.com/v1"
	}
	if strings.TrimSpace(model) == "" {
		model = "claude-3-5-sonnet-20241022"
	}
	return anthropicClient{
		httpClient:         &http.Client{Timeout: 30 * time.Second},
		apiKey:             apiKey,
		baseURL:            baseURL,
		defaultModel:       model,
		chatLogger:         logging.NewChatLogger("anthropic"),
		defaultTemperature: defaultTemp,
	}
}

func (c anthropicClient) Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) {
	if c.apiKey == "" {
		return nilStringErr("missing Anthropic API key")
	}
	o := Options{Model: c.defaultModel}
	for _, opt := range opts {
		opt(&o)
	}
	if o.Model == "" {
		o.Model = c.defaultModel
	}
	start := time.Now()
	c.logStart(false, o, messages)
	req := buildAnthropicChatRequest(o, messages, c.defaultModel, c.defaultTemperature, false)
	body, err := json.Marshal(req)
	if err != nil {
		c.logf("marshal error: %v", err)
		return "", err
	}
	endpoint := c.baseURL + "/messages"
	logging.Logf("llm/anthropic ", "POST %s", endpoint)
	resp, err := c.doJSON(ctx, endpoint, body, map[string]string{
		"x-api-key":       c.apiKey,
		"anthropic-version": "2023-06-01",
	})
	if err != nil {
		logging.Logf("llm/anthropic ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase)
		return "", err
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {
			logging.Logf("llm/anthropic", "failed to close response body: %v", err)
		}
	}()
	if err := handleAnthropicNon2xx(resp, start); err != nil {
		return "", err
	}
	out, err := decodeAnthropicChat(resp, start)
	if err != nil {
		return "", err
	}
	if len(out.Content) == 0 {
		logging.Logf("llm/anthropic ", "%sno content returned duration=%s%s", logging.AnsiRed, time.Since(start), logging.AnsiBase)
		return "", errors.New("anthropic: no content returned")
	}
	content := out.Content[0].Text
	logging.Logf("llm/anthropic ", "success stop_reason=%s size=%d preview=%s%s%s duration=%s", out.StopReason, len(content), logging.AnsiGreen, logging.PreviewForLog(content), logging.AnsiBase, time.Since(start))
	return content, nil
}

// Provider metadata
func (c anthropicClient) Name() string         { return "anthropic" }
func (c anthropicClient) DefaultModel() string { return c.defaultModel }

// Streaming support (optional)
func (c anthropicClient) ChatStream(ctx context.Context, messages []Message, onDelta func(string), opts ...RequestOption) error {
	if c.apiKey == "" {
		return errors.New("missing Anthropic API key")
	}
	o := Options{Model: c.defaultModel}
	for _, opt := range opts {
		opt(&o)
	}
	if o.Model == "" {
		o.Model = c.defaultModel
	}
	start := time.Now()
	c.logStart(true, o, messages)
	req := buildAnthropicChatRequest(o, messages, c.defaultModel, c.defaultTemperature, true)
	body, err := json.Marshal(req)
	if err != nil {
		c.logf("marshal error: %v", err)
		return err
	}
	endpoint := c.baseURL + "/messages"
	logging.Logf("llm/anthropic ", "POST %s (stream)", endpoint)
	resp, err := c.doJSON(ctx, endpoint, body, map[string]string{
		"x-api-key":       c.apiKey,
		"anthropic-version": "2023-06-01",
	})
	if err != nil {
		logging.Logf("llm/anthropic ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase)
		return err
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {
			logging.Logf("llm/anthropic", "failed to close response body: %v", err)
		}
	}()
	if err := handleAnthropicNon2xx(resp, start); err != nil {
		return err
	}

	if err := parseAnthropicStream(resp, start, onDelta); err != nil {
		return err
	}
	logging.Logf("llm/anthropic ", "stream end duration=%s", time.Since(start))
	return nil
}

// Private helpers
func (c anthropicClient) logf(format string, args ...any) {
	logging.Logf("llm/anthropic ", format, args...)
}

func (c anthropicClient) logStart(stream bool, o Options, messages []Message) {
	logMessages := make([]struct{ Role, Content string }, len(messages))
	for i, m := range messages {
		logMessages[i] = struct{ Role, Content string }{m.Role, m.Content}
	}
	c.chatLogger.LogStart(stream, o.Model, o.Temperature, o.MaxTokens, o.Stop, logMessages)
}

func buildAnthropicChatRequest(o Options, messages []Message, defaultModel string, defaultTemp *float64, stream bool) anthropicChatRequest {
	req := anthropicChatRequest{
		Model:     o.Model,
		Stream:    stream,
		MaxTokens: 4096, // Anthropic requires max_tokens
	}
	req.Messages = make([]anthropicMessage, len(messages))
	for i, m := range messages {
		req.Messages[i] = anthropicMessage{
			Role:    m.Role,
			Content: m.Content,
		}
	}
	if o.Temperature != 0 {
		req.Temperature = &o.Temperature
	} else if defaultTemp != nil {
		t := *defaultTemp
		req.Temperature = &t
	}
	if o.MaxTokens > 0 {
		req.MaxTokens = o.MaxTokens
	}
	// Note: Anthropic's API doesn't support stop sequences in the same way as OpenAI,
	// but we keep them in the request for future compatibility.
	return req
}

func (c anthropicClient) doJSON(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")
	for k, v := range headers {
		req.Header.Set(k, v)
	}
	return c.httpClient.Do(req)
}

func handleAnthropicNon2xx(resp *http.Response, start time.Time) error {
	if resp.StatusCode >= 200 && resp.StatusCode < 300 {
		return nil
	}
	var apiErr anthropicChatResponse
	_ = json.NewDecoder(resp.Body).Decode(&apiErr)
	if apiErr.Error != nil && apiErr.Error.Message != "" {
		logging.Logf("llm/anthropic ", "%sapi error status=%d type=%s msg=%s duration=%s%s", logging.AnsiRed, resp.StatusCode, apiErr.Error.Type, apiErr.Error.Message, time.Since(start), logging.AnsiBase)
		return fmt.Errorf("anthropic error: %s (status %d)", apiErr.Error.Message, resp.StatusCode)
	}
	logging.Logf("llm/anthropic ", "%shttp non-2xx status=%d duration=%s%s", logging.AnsiRed, resp.StatusCode, time.Since(start), logging.AnsiBase)
	return fmt.Errorf("anthropic http error: status %d", resp.StatusCode)
}

func decodeAnthropicChat(resp *http.Response, start time.Time) (anthropicChatResponse, error) {
	var out anthropicChatResponse
	if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
		logging.Logf("llm/anthropic ", "%sdecode error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase)
		return anthropicChatResponse{}, err
	}
	return out, nil
}

func parseAnthropicStream(resp *http.Response, start time.Time, onDelta func(string)) error {
	// Parse server-sent events: lines starting with "data: " containing JSON
	scanner := bufio.NewScanner(resp.Body)
	const maxBuf = 1024 * 1024
	buf := make([]byte, 0, 64*1024)
	scanner.Buffer(buf, maxBuf)
	for scanner.Scan() {
		line := scanner.Text()
		if !strings.HasPrefix(line, "data: ") {
			continue
		}
		payload := strings.TrimPrefix(line, "data: ")
		// Check for stream end event
		if strings.Contains(payload, "\"type\":\"message_stop\"") {
			break
		}
		// Try to parse as delta event
		var delta anthropicStreamDelta
		if err := json.Unmarshal([]byte(payload), &delta); err != nil {
			continue
		}
		if delta.Type == "content_block_delta" && delta.Delta.Type == "text_delta" && delta.Delta.Text != "" {
			onDelta(delta.Delta.Text)
		}
		// Check for errors in stream
		var errEvent anthropicStreamError
		if err := json.Unmarshal([]byte(payload), &errEvent); err == nil {
			if errEvent.Type == "error" && errEvent.Error.Message != "" {
				logging.Logf("llm/anthropic ", "%sstream error: %s%s", logging.AnsiRed, errEvent.Error.Message, logging.AnsiBase)
				return fmt.Errorf("anthropic stream error: %s", errEvent.Error.Message)
			}
		}
	}
	if err := scanner.Err(); err != nil {
		logging.Logf("llm/anthropic ", "%sstream read error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase)
		return err
	}
	return nil
}