-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
450 lines (383 loc) · 12.1 KB
/
client.go
File metadata and controls
450 lines (383 loc) · 12.1 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package blockrun
import (
"bytes"
"crypto/ecdsa"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/ethereum/go-ethereum/crypto"
)
const (
// DefaultAPIURL is the default BlockRun API endpoint.
DefaultAPIURL = "https://blockrun.ai/api"
// DefaultMaxTokens is the default max tokens for chat completions.
DefaultMaxTokens = 1024
// DefaultTimeout is the default HTTP timeout.
DefaultTimeout = 60 * time.Second
)
// LLMClient is the BlockRun LLM gateway client.
//
// SECURITY: Your private key is used ONLY for local EIP-712 signing.
// The key NEVER leaves your machine - only signatures are transmitted.
type LLMClient struct {
privateKey *ecdsa.PrivateKey
address string
apiURL string
httpClient *http.Client
sessionTotalUSD float64
sessionCalls int
}
// Spending represents session spending information.
type Spending struct {
TotalUSD float64
Calls int
}
// ClientOption is a function that configures an LLMClient.
type ClientOption func(*LLMClient)
// WithAPIURL sets a custom API URL.
func WithAPIURL(url string) ClientOption {
return func(c *LLMClient) {
c.apiURL = strings.TrimSuffix(url, "/")
}
}
// WithTimeout sets the HTTP timeout.
func WithTimeout(timeout time.Duration) ClientOption {
return func(c *LLMClient) {
c.httpClient.Timeout = timeout
}
}
// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(client *http.Client) ClientOption {
return func(c *LLMClient) {
c.httpClient = client
}
}
// NewLLMClient creates a new BlockRun LLM client.
//
// If privateKey is empty, it will be read from the BASE_CHAIN_WALLET_KEY
// environment variable.
//
// SECURITY: Your private key is used ONLY for local EIP-712 signing.
// The key NEVER leaves your machine - only signatures are transmitted.
func NewLLMClient(privateKey string, opts ...ClientOption) (*LLMClient, error) {
// Get private key from param or environment
key := privateKey
if key == "" {
key = os.Getenv("BASE_CHAIN_WALLET_KEY")
}
if key == "" {
return nil, &ValidationError{
Field: "privateKey",
Message: "Private key required. Pass privateKey parameter or set BASE_CHAIN_WALLET_KEY environment variable. NOTE: Your key never leaves your machine - only signatures are sent.",
}
}
// Parse private key
key = strings.TrimPrefix(key, "0x")
ecdsaKey, err := crypto.HexToECDSA(key)
if err != nil {
return nil, &ValidationError{
Field: "privateKey",
Message: fmt.Sprintf("Invalid private key format: %v", err),
}
}
// Get wallet address
address := crypto.PubkeyToAddress(ecdsaKey.PublicKey).Hex()
// Create client with defaults
client := &LLMClient{
privateKey: ecdsaKey,
address: address,
apiURL: DefaultAPIURL,
httpClient: &http.Client{Timeout: DefaultTimeout},
}
// Apply options
for _, opt := range opts {
opt(client)
}
// Check for custom API URL in environment
if envURL := os.Getenv("BLOCKRUN_API_URL"); envURL != "" && client.apiURL == DefaultAPIURL {
client.apiURL = strings.TrimSuffix(envURL, "/")
}
return client, nil
}
// Chat sends a simple 1-line chat request.
//
// This is a convenience method that wraps ChatCompletion for simple use cases.
func (c *LLMClient) Chat(model, prompt string) (string, error) {
return c.ChatWithSystem(model, prompt, "")
}
// ChatWithSystem sends a chat request with an optional system prompt.
func (c *LLMClient) ChatWithSystem(model, prompt, system string) (string, error) {
messages := []ChatMessage{}
if system != "" {
messages = append(messages, ChatMessage{Role: "system", Content: system})
}
messages = append(messages, ChatMessage{Role: "user", Content: prompt})
resp, err := c.ChatCompletion(model, messages, nil)
if err != nil {
return "", err
}
if len(resp.Choices) == 0 {
return "", &APIError{Message: "No choices in response"}
}
return resp.Choices[0].Message.Content, nil
}
// ChatCompletion sends a full chat completion request (OpenAI-compatible).
func (c *LLMClient) ChatCompletion(model string, messages []ChatMessage, opts *ChatCompletionOptions) (*ChatResponse, error) {
// Validate inputs
if model == "" {
return nil, &ValidationError{Field: "model", Message: "Model is required"}
}
if len(messages) == 0 {
return nil, &ValidationError{Field: "messages", Message: "At least one message is required"}
}
// Build request body
body := map[string]any{
"model": model,
"messages": messages,
}
// Apply options
maxTokens := DefaultMaxTokens
if opts != nil {
if opts.MaxTokens > 0 {
maxTokens = opts.MaxTokens
}
if opts.Temperature > 0 {
body["temperature"] = opts.Temperature
}
if opts.TopP > 0 {
body["top_p"] = opts.TopP
}
// Handle xAI Live Search parameters
if opts.SearchParameters != nil {
body["search_parameters"] = opts.SearchParameters
} else if opts.Search {
// Simple shortcut: Search=true enables live search with defaults
body["search_parameters"] = map[string]string{"mode": "on"}
}
}
body["max_tokens"] = maxTokens
// Make request with payment handling
return c.requestWithPayment("/v1/chat/completions", body)
}
// ListModels returns the list of available models with pricing.
func (c *LLMClient) ListModels() ([]Model, error) {
url := c.apiURL + "/v1/models"
resp, err := c.httpClient.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to list models: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, &APIError{
StatusCode: resp.StatusCode,
Message: "Failed to list models",
}
}
var result struct {
Data []Model `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode models response: %w", err)
}
return result.Data, nil
}
// GetWalletAddress returns the wallet address being used for payments.
func (c *LLMClient) GetWalletAddress() string {
return c.address
}
// GetSpending returns session spending information.
func (c *LLMClient) GetSpending() Spending {
return Spending{
TotalUSD: c.sessionTotalUSD,
Calls: c.sessionCalls,
}
}
// ListImageModels returns the list of available image models with pricing.
func (c *LLMClient) ListImageModels() ([]ImageModel, error) {
url := c.apiURL + "/v1/images/models"
resp, err := c.httpClient.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to list image models: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, &APIError{
StatusCode: resp.StatusCode,
Message: "Failed to list image models",
}
}
var result struct {
Data []ImageModel `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode image models response: %w", err)
}
return result.Data, nil
}
// ListAllModels returns a unified list of all available models (LLM and image).
func (c *LLMClient) ListAllModels() ([]AllModel, error) {
// Get LLM models
llmModels, err := c.ListModels()
if err != nil {
return nil, fmt.Errorf("failed to list LLM models: %w", err)
}
// Get image models
imageModels, err := c.ListImageModels()
if err != nil {
return nil, fmt.Errorf("failed to list image models: %w", err)
}
// Combine into unified list
allModels := make([]AllModel, 0, len(llmModels)+len(imageModels))
for _, m := range llmModels {
allModels = append(allModels, AllModel{
ID: m.ID,
Name: m.Name,
Provider: m.Provider,
Type: "llm",
InputPrice: m.InputPrice,
OutputPrice: m.OutputPrice,
ContextLimit: m.ContextLimit,
})
}
for _, m := range imageModels {
allModels = append(allModels, AllModel{
ID: m.ID,
Name: m.Name,
Provider: m.Provider,
Type: "image",
PricePerImage: m.PricePerImage,
SupportedSizes: m.SupportedSizes,
})
}
return allModels, nil
}
// requestWithPayment makes a request with automatic x402 payment handling.
func (c *LLMClient) requestWithPayment(endpoint string, body map[string]any) (*ChatResponse, error) {
url := c.apiURL + endpoint
// Encode body
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to encode request body: %w", err)
}
// First attempt (will likely return 402)
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
// Handle 402 Payment Required
if resp.StatusCode == http.StatusPaymentRequired {
return c.handlePaymentAndRetry(url, jsonBody, resp)
}
// Handle other errors
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
return nil, &APIError{
StatusCode: resp.StatusCode,
Message: fmt.Sprintf("API error: %s", string(bodyBytes)),
}
}
// Parse successful response
var chatResp ChatResponse
if err := json.NewDecoder(resp.Body).Decode(&chatResp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &chatResp, nil
}
// handlePaymentAndRetry handles a 402 response by signing a payment and retrying.
func (c *LLMClient) handlePaymentAndRetry(url string, body []byte, resp *http.Response) (*ChatResponse, error) {
// Get payment required header
paymentHeader := resp.Header.Get("payment-required")
if paymentHeader == "" {
// Try to get from response body
var respBody map[string]any
if err := json.NewDecoder(resp.Body).Decode(&respBody); err == nil {
if _, ok := respBody["x402"]; ok {
// Response body contains payment info - re-encode as header
jsonBytes, _ := json.Marshal(respBody)
paymentHeader = string(jsonBytes)
}
}
}
if paymentHeader == "" {
return nil, &PaymentError{Message: "402 response but no payment requirements found"}
}
// Parse payment requirements
paymentReq, err := ParsePaymentRequired(paymentHeader)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to parse payment requirements: %v", err)}
}
// Extract payment details
paymentOption, err := ExtractPaymentDetails(paymentReq)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to extract payment details: %v", err)}
}
// Determine resource URL
resourceURL := paymentReq.Resource.URL
if resourceURL == "" {
resourceURL = url
}
// Create signed payment payload
paymentPayload, err := CreatePaymentPayload(
c.privateKey,
paymentOption.PayTo,
paymentOption.Amount,
paymentOption.Network,
resourceURL,
paymentReq.Resource.Description,
paymentOption.MaxTimeoutSeconds,
paymentOption.Extra,
paymentReq.Extensions,
)
if err != nil {
return nil, &PaymentError{Message: fmt.Sprintf("Failed to create payment: %v", err)}
}
// Retry with payment signature
retryReq, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("failed to create retry request: %w", err)
}
retryReq.Header.Set("Content-Type", "application/json")
retryReq.Header.Set("PAYMENT-SIGNATURE", paymentPayload)
retryResp, err := c.httpClient.Do(retryReq)
if err != nil {
return nil, fmt.Errorf("retry request failed: %w", err)
}
defer retryResp.Body.Close()
// Check for payment rejection
if retryResp.StatusCode == http.StatusPaymentRequired {
return nil, &PaymentError{Message: "Payment was rejected. Check your wallet balance."}
}
// Handle other errors
if retryResp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(retryResp.Body)
return nil, &APIError{
StatusCode: retryResp.StatusCode,
Message: fmt.Sprintf("API error after payment: %s", string(bodyBytes)),
}
}
// Parse successful response
var chatResp ChatResponse
if err := json.NewDecoder(retryResp.Body).Decode(&chatResp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
// Track spending - convert amount from micro-USDC to USD
c.sessionCalls++
if amountStr := paymentOption.Amount; amountStr != "" {
// Amount is in micro-USDC (6 decimals), convert to USD
var amountMicro float64
if _, err := fmt.Sscanf(amountStr, "%f", &amountMicro); err == nil {
c.sessionTotalUSD += amountMicro / 1_000_000
}
}
return &chatResp, nil
}