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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// Anthropic client implementation using Messages API with optional streaming support.
package llm
import (
"bufio"
"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"`
}
// Ensure anthropicClient implements Client and Streamer.
var (
_ Client = (*anthropicClient)(nil)
_ Streamer = (*anthropicClient)(nil)
)
func anthropicProviderFactory(cfg Config, keys ProviderKeys) (Client, error) {
if strings.TrimSpace(keys.AnthropicAPIKey) == "" {
return nil, missingAPIKeyError("anthropic", "ANTHROPIC_API_KEY", "HEXAI_ANTHROPIC_API_KEY")
}
return newAnthropicWithTimeout(
cfg.AnthropicBaseURL,
cfg.AnthropicModel,
keys.AnthropicAPIKey,
withDefaultTemperature(cfg.AnthropicTemperature, 0.2),
cfg.RequestTimeout,
), nil
}
// 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 {
return newAnthropicWithTimeout(baseURL, model, apiKey, defaultTemp, 0)
}
func newAnthropicWithTimeout(baseURL, model, apiKey string, defaultTemp *float64, timeoutSec int) Client {
if strings.TrimSpace(baseURL) == "" {
baseURL = "https://api.anthropic.com/v1"
}
if strings.TrimSpace(model) == "" {
model = "claude-3-5-sonnet-20240620"
}
if timeoutSec <= 0 {
timeoutSec = 30
}
return anthropicClient{
httpClient: &http.Client{Timeout: time.Duration(timeoutSec) * time.Second},
apiKey: apiKey,
baseURL: baseURL,
defaultModel: model,
chatLogger: logging.NewChatLogger("anthropic"),
defaultTemperature: defaultTemp,
}
}
// Chat sends a request to Anthropic and returns the response.
func (c anthropicClient) Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) {
if c.apiKey == "" {
return "", missingAPIKeyError("anthropic", "ANTHROPIC_API_KEY", "HEXAI_ANTHROPIC_API_KEY")
}
o := c.resolveOptions(opts)
start := time.Now()
c.logStart(false, o, messages)
resp, err := c.sendRequest(ctx, o, messages, false, start)
if err != nil {
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
}
return c.extractContent(out, start)
}
// Name returns the provider's short name.
func (c anthropicClient) Name() string { return "anthropic" }
// DefaultModel returns the configured default model name.
func (c anthropicClient) DefaultModel() string { return c.defaultModel }
// ChatStream sends a streaming request and invokes onDelta for each text chunk.
func (c anthropicClient) ChatStream(ctx context.Context, messages []Message, onDelta func(string), opts ...RequestOption) error {
if c.apiKey == "" {
return missingAPIKeyError("anthropic", "ANTHROPIC_API_KEY", "HEXAI_ANTHROPIC_API_KEY")
}
o := c.resolveOptions(opts)
start := time.Now()
c.logStart(true, o, messages)
resp, err := c.sendRequest(ctx, o, messages, true, start)
if err != nil {
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) resolveOptions(opts []RequestOption) Options {
o := Options{Model: c.defaultModel}
for _, opt := range opts {
opt(&o)
}
if o.Model == "" {
o.Model = c.defaultModel
}
return o
}
func (c anthropicClient) sendRequest(ctx context.Context, o Options, messages []Message, stream bool, start time.Time) (*http.Response, error) {
req := buildAnthropicChatRequest(o, messages, c.defaultModel, c.defaultTemperature, stream)
body, err := json.Marshal(req)
if err != nil {
c.logf("marshal error: %v", err)
return nil, err
}
endpoint := c.baseURL + "/messages"
mode := "POST"
if stream {
mode = "POST (stream)"
}
logging.Logf("llm/anthropic ", "%s %s", mode, endpoint)
return c.doJSON(ctx, endpoint, body, map[string]string{
"x-api-key": c.apiKey,
"anthropic-version": "2023-06-01",
})
}
func (c anthropicClient) extractContent(out anthropicChatResponse, start time.Time) (string, error) {
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
}
func (c anthropicClient) logf(format string, args ...any) {
logging.Logf("llm/anthropic ", format, args...)
}
func (c anthropicClient) logStart(stream bool, o Options, messages []Message) {
logStartMessages(c.chatLogger, stream, o, messages)
}
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
}
// Anthropic requires system messages in a top-level "system" field, not in messages array
var systemParts []string
var nonSystemMessages []Message
for _, m := range messages {
if m.Role == "system" {
systemParts = append(systemParts, m.Content)
} else {
nonSystemMessages = append(nonSystemMessages, m)
}
}
if len(systemParts) > 0 {
req.System = strings.Join(systemParts, "\n\n")
}
req.Messages = make([]anthropicMessage, len(nonSystemMessages))
for i, m := range nonSystemMessages {
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
}
return req
}
func (c anthropicClient) doJSON(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error) {
return doJSONRequest(ctx, c.httpClient, url, body, headers, "")
}
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
}
|