-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdk.go
More file actions
843 lines (732 loc) · 21.5 KB
/
sdk.go
File metadata and controls
843 lines (732 loc) · 21.5 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
package sdk
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"math"
"net"
"net/http"
"strings"
"syscall"
"time"
"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/require"
)
// Client represents the SDK client interface
type Client interface {
WithAuthToken(token string) *clientImpl
WithTools(tools *[]ChatCompletionTool) *clientImpl
WithOptions(options *CreateChatCompletionRequest) *clientImpl
WithHeaders(headers map[string]string) *clientImpl
WithHeader(name, value string) *clientImpl
WithMiddlewareOptions(options *MiddlewareOptions) *clientImpl
ListModels(ctx context.Context) (*ListModelsResponse, error)
ListProviderModels(ctx context.Context, provider Provider) (*ListModelsResponse, error)
ListTools(ctx context.Context) (*ListToolsResponse, error)
GenerateContent(ctx context.Context, provider Provider, model string, messages []Message) (*CreateChatCompletionResponse, error)
GenerateContentStream(ctx context.Context, provider Provider, model string, messages []Message) (<-chan SSEvent, error)
HealthCheck(ctx context.Context) error
}
// isRetryableError determines if an error should trigger a retry
func isRetryableError(err error) bool {
if err == nil {
return false
}
if netErr, ok := err.(net.Error); ok {
if netErr.Timeout() {
return true
}
}
if opErr, ok := err.(*net.OpError); ok {
if opErr.Op == "dial" || opErr.Op == "read" {
return true
}
if sysErr, ok := opErr.Err.(*syscall.Errno); ok {
return *sysErr == syscall.ECONNREFUSED || *sysErr == syscall.ECONNRESET
}
}
if _, ok := err.(*net.DNSError); ok {
return true
}
if err == context.DeadlineExceeded {
return true
}
errStr := err.Error()
return strings.Contains(errStr, "connection refused") ||
strings.Contains(errStr, "connection reset") ||
strings.Contains(errStr, "timeout") ||
strings.Contains(errStr, "EOF") ||
strings.Contains(errStr, "timeout awaiting response headers")
}
// isRetryableStatusCode determines if an HTTP status code should trigger a retry
func isRetryableStatusCode(statusCode int, config *RetryConfig) bool {
if len(config.RetryableStatusCodes) > 0 {
for _, code := range config.RetryableStatusCodes {
if statusCode == code {
return true
}
}
return false
}
switch statusCode {
case
http.StatusRequestTimeout, // 408
http.StatusTooManyRequests, // 429
http.StatusInternalServerError, // 500
http.StatusBadGateway, // 502
http.StatusServiceUnavailable, // 503
http.StatusGatewayTimeout: // 504
return true
default:
return false
}
}
// calculateBackoff calculates the backoff delay for exponential backoff
func calculateBackoff(attempt int, config *RetryConfig) time.Duration {
if attempt == 0 {
return 0
}
backoff := float64(config.InitialBackoffSec) * math.Pow(float64(config.BackoffMultiplier), float64(attempt-1))
if backoff > float64(config.MaxBackoffSec) {
backoff = float64(config.MaxBackoffSec)
}
return time.Duration(backoff) * time.Second
}
// getDefaultRetryConfig returns the default retry configuration
func getDefaultRetryConfig() *RetryConfig {
return &RetryConfig{
Enabled: true,
MaxAttempts: 3,
InitialBackoffSec: 2,
MaxBackoffSec: 30,
BackoffMultiplier: 2,
}
}
// clientImpl represents the concrete implementation of the SDK client
type clientImpl struct {
baseURL string // Base URL of the Inference Gateway API
http *resty.Client // HTTP client for making requests
token string // Authentication token
tools *[]ChatCompletionTool
options *CreateChatCompletionRequest // Custom request options
retryConfig *RetryConfig // Retry configuration
}
// NewClient creates a new SDK client with the specified options.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// APIKey: "your-api-key",
// Timeout: 30 * time.Second,
// Tools: nil,
// Headers: map[string]string{
// "X-Custom-Header": "custom-value",
// "User-Agent": "my-app/1.0",
// },
// })
func NewClient(options *ClientOptions) Client {
client := resty.New()
if options.Timeout > 0 {
client.SetTimeout(options.Timeout)
}
if options.APIKey != "" {
client.SetAuthToken(options.APIKey)
}
if len(options.Headers) > 0 {
client.SetHeaders(options.Headers)
}
retryConfig := options.RetryConfig
if retryConfig == nil {
retryConfig = getDefaultRetryConfig()
}
return &clientImpl{
baseURL: options.BaseURL,
http: client,
token: options.APIKey,
tools: options.Tools,
options: nil,
retryConfig: retryConfig,
}
}
// parseRetryAfter parses the Retry-After header and returns the delay duration
// The header can be either a delay in seconds or an HTTP-date
func parseRetryAfter(retryAfter string) (time.Duration, bool) {
if retryAfter == "" {
return 0, false
}
if seconds, err := time.ParseDuration(retryAfter + "s"); err == nil {
return seconds, true
}
if retryTime, err := http.ParseTime(retryAfter); err == nil {
delay := time.Until(retryTime)
if delay > 0 {
return delay, true
}
}
return 0, false
}
// executeWithRetry executes an HTTP request with retry logic
func (c *clientImpl) executeWithRetry(ctx context.Context, request func() (*resty.Response, error)) (*resty.Response, error) {
if !c.retryConfig.Enabled {
return request()
}
var lastErr error
var resp *resty.Response
for attempt := 0; attempt < c.retryConfig.MaxAttempts; attempt++ {
if attempt > 0 {
var delay time.Duration
if resp != nil && resp.StatusCode() == 429 {
if retryAfterDelay, ok := parseRetryAfter(resp.Header().Get("Retry-After")); ok {
delay = retryAfterDelay
} else {
delay = calculateBackoff(attempt, c.retryConfig)
}
} else {
delay = calculateBackoff(attempt, c.retryConfig)
}
if c.retryConfig.OnRetry != nil {
c.retryConfig.OnRetry(attempt, lastErr, delay)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(delay):
}
}
resp, lastErr = request()
if lastErr == nil {
if !resp.IsError() || !isRetryableStatusCode(resp.StatusCode(), c.retryConfig) {
return resp, nil
}
lastErr = fmt.Errorf("HTTP %d", resp.StatusCode())
}
if !isRetryableError(lastErr) && (resp == nil || !isRetryableStatusCode(resp.StatusCode(), c.retryConfig)) {
break
}
if ctx.Err() != nil {
return resp, lastErr
}
}
return resp, lastErr
}
// WithAuthToken sets the authentication token for the client.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// client = client.WithAuthToken("your-auth-token")
// resp, err := client.ListModels(ctx)
func (c *clientImpl) WithAuthToken(token string) *clientImpl {
c.token = token
c.http.SetAuthToken(token)
return c
}
// WithTools sets the tools for the client.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// tools := []sdk.ChatCompletionTool{
// {
// Name: "Weather",
// Functions: []sdk.FunctionObject{
// {
// Name: "get_current_weather",
// Description: stringPtr("Get the current weather in a given location"),
// Parameters: &sdk.FunctionParameters{
// Type: stringPtr("object"),
// Properties: &map[string]interface{}{
// "location": map[string]interface{}{
// "type": "string",
// "description": "The city and state, e.g. San Francisco, CA",
// },
// "unit": map[string]interface{}{
// "type": "string",
// "enum": []string{"celsius", "fahrenheit"},
// "description": "The temperature unit to use",
// },
// },
// Required: &[]string{"location"},
// },
// },
// },
// },
// }
// resp, err = client.WithTools(tools).GenerateContent(ctx, sdk.Openai, "gpt-4o", messages)
func (c *clientImpl) WithTools(tools *[]ChatCompletionTool) *clientImpl {
c.tools = tools
return c
}
// WithOptions sets custom request options for subsequent API calls.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
//
// // Set reasoning format for subsequent requests
// reasoningFormat := "parsed"
// options := &sdk.CreateChatCompletionRequest{
// ReasoningFormat: &reasoningFormat,
// }
//
// // Use the options in a request
// response, err := client.WithOptions(options).GenerateContent(
// ctx,
// sdk.Anthropic,
// "anthropic/claude-3-opus-20240229",
// messages,
// )
//
// Notes:
// - For GenerateContent calls, Stream will always be set to false regardless of options
// - For GenerateContentStream calls, Stream will always be set to true regardless of options
// - Model and Messages provided in the actual method calls will override options
// - Options will persist for all future calls until cleared with WithOptions(nil)
func (c *clientImpl) WithOptions(options *CreateChatCompletionRequest) *clientImpl {
c.options = options
return c
}
// WithHeaders sets custom headers for the client.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// headers := map[string]string{
// "X-Custom-Header": "value",
// }
// client = client.WithHeaders(headers)
// resp, err := client.ListModels(ctx)
func (c *clientImpl) WithHeaders(headers map[string]string) *clientImpl {
for name, value := range headers {
c.http.Header.Set(name, value)
}
return c
}
// WithHeader sets a single custom header for the client.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// client = client.WithHeader("X-Custom-Header", "value")
// resp, err := client.ListModels(ctx)
func (c *clientImpl) WithHeader(name, value string) *clientImpl {
c.http.Header.Set(name, value)
return c
}
// WithMiddlewareOptions sets middleware control options for subsequent API calls.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// middlewareOpts := &sdk.MiddlewareOptions{
// SkipMCP: true,
// }
// resp, err := client.WithMiddlewareOptions(middlewareOpts).GenerateContent(ctx, provider, model, messages)
//
// Note: This functionality requires the Inference Gateway to support the corresponding headers:
// - X-MCP-Bypass: Skip MCP middleware processing
// - X-Direct-Provider: Route directly to provider
func (c *clientImpl) WithMiddlewareOptions(options *MiddlewareOptions) *clientImpl {
if options == nil {
return c
}
if options.SkipMCP {
c.http.Header.Set("X-MCP-Bypass", "true")
} else {
c.http.Header.Del("X-MCP-Bypass")
}
if options.DirectProvider {
c.http.Header.Set("X-Direct-Provider", "true")
} else {
c.http.Header.Del("X-Direct-Provider")
}
return c
}
// ListModels returns all available language models from all providers.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// ctx := context.Background()
// models, err := client.ListModels(ctx)
// if err != nil {
// log.Fatalf("Error listing models: %v", err)
// }
// fmt.Printf("Available models: %+v\n", models)
func (c *clientImpl) ListModels(ctx context.Context) (*ListModelsResponse, error) {
resp, err := c.executeWithRetry(ctx, func() (*resty.Response, error) {
return c.http.R().
SetContext(ctx).
SetResult(&ListModelsResponse{}).
Get(fmt.Sprintf("%s/models", c.baseURL))
})
if err != nil {
return &ListModelsResponse{}, err
}
if resp.IsError() {
return &ListModelsResponse{}, fmt.Errorf("failed to list models, status code: %d", resp.StatusCode())
}
result, ok := resp.Result().(*ListModelsResponse)
if !ok || result == nil {
return &ListModelsResponse{}, fmt.Errorf("failed to parse response")
}
return result, nil
}
// ListProviderModels returns all available language models for a specific provider.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// ctx := context.Background()
// resp, err := client.ListProviderModels(ctx, sdk.Ollama)
// if err != nil {
// log.Fatalf("Error listing models: %v", resp)
// }
// fmt.Printf("Provider: %s", resp.Provider)
// fmt.Printf("Available models: %+v\n", resp.Data)
func (c *clientImpl) ListProviderModels(ctx context.Context, provider Provider) (*ListModelsResponse, error) {
resp, err := c.executeWithRetry(ctx, func() (*resty.Response, error) {
return c.http.R().
SetContext(ctx).
SetResult(&ListModelsResponse{}).
Get(fmt.Sprintf("%s/models?provider=%s", c.baseURL, provider))
})
if err != nil {
return nil, err
}
if resp.IsError() {
var errorResp Error
if err := json.Unmarshal(resp.Body(), &errorResp); err == nil && errorResp.Error != nil {
return nil, fmt.Errorf("API error: %s (status code: %d)", *errorResp.Error, resp.StatusCode())
}
errMsg := fmt.Sprintf("failed to list provider models, status code: %d", resp.StatusCode())
if len(resp.Body()) > 0 {
errMsg = fmt.Sprintf("%s, response body: %s", errMsg, string(resp.Body()))
}
return nil, fmt.Errorf("%s", errMsg)
}
result, ok := resp.Result().(*ListModelsResponse)
if !ok || result == nil {
return nil, fmt.Errorf("failed to parse response")
}
return result, nil
}
// ListTools returns all available MCP tools.
// Only accessible when EXPOSE_MCP is enabled on the server.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// APIKey: "your-api-key",
// })
// ctx := context.Background()
// tools, err := client.ListTools(ctx)
// if err != nil {
// log.Fatalf("Error listing tools: %v", err)
// }
// fmt.Printf("Available tools: %+v\n", tools.Data)
func (c *clientImpl) ListTools(ctx context.Context) (*ListToolsResponse, error) {
resp, err := c.executeWithRetry(ctx, func() (*resty.Response, error) {
return c.http.R().
SetContext(ctx).
SetResult(&ListToolsResponse{}).
Get(fmt.Sprintf("%s/mcp/tools", c.baseURL))
})
if err != nil {
return nil, err
}
if resp.IsError() {
var errorResp Error
if err := json.Unmarshal(resp.Body(), &errorResp); err == nil && errorResp.Error != nil {
return nil, fmt.Errorf("API error: %s (status code: %d)", *errorResp.Error, resp.StatusCode())
}
errMsg := fmt.Sprintf("failed to list MCP tools, status code: %d", resp.StatusCode())
if len(resp.Body()) > 0 {
errMsg = fmt.Sprintf("%s, response body: %s", errMsg, string(resp.Body()))
}
return nil, fmt.Errorf("%s", errMsg)
}
result, ok := resp.Result().(*ListToolsResponse)
if !ok || result == nil {
return nil, fmt.Errorf("failed to parse response")
}
return result, nil
}
// GenerateContent generates content using the specified provider and model.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// ctx := context.Background()
// ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
// defer cancel()
//
// response, err := client.GenerateContent(
// ctx,
// sdk.Ollama,
// "llama2",
// []sdk.Message{
// {
// Role: sdk.MessageRoleSystem,
// Content: "You are a helpful assistant.",
// },
// {
// Role: sdk.MessageRoleUser,
// Content: "What is Go?",
// },
// },
// )
// if err != nil {
// log.Fatalf("Error generating content: %v", err)
// }
// fmt.Printf("Generated content: %s\n", response.Response.Content)
func (c *clientImpl) GenerateContent(ctx context.Context, provider Provider, model string, messages []Message) (*CreateChatCompletionResponse, error) {
request := CreateChatCompletionRequest{
Model: model,
Messages: messages,
Tools: c.tools,
Stream: boolPtr(false),
}
if c.options != nil {
options := *c.options
if options.Model == "" {
options.Model = request.Model
}
if len(options.Messages) == 0 {
options.Messages = request.Messages
}
if options.Tools == nil && c.tools != nil {
options.Tools = c.tools
}
options.Stream = boolPtr(false)
request = options
}
queryParams := make(map[string]string)
if provider != "" {
queryParams["provider"] = string(provider)
}
resp, err := c.executeWithRetry(ctx, func() (*resty.Response, error) {
return c.http.R().
SetContext(ctx).
SetQueryParams(queryParams).
SetBody(request).
SetResult(&CreateChatCompletionResponse{}).
Post(fmt.Sprintf("%s/chat/completions", c.baseURL))
})
if err != nil {
return nil, err
}
if resp.IsError() {
var errorResp Error
if err := json.Unmarshal(resp.Body(), &errorResp); err == nil && errorResp.Error != nil {
return nil, fmt.Errorf("API error: %s (status code: %d)", *errorResp.Error, resp.StatusCode())
}
errMsg := fmt.Sprintf("failed to generate content, status code: %d", resp.StatusCode())
if len(resp.Body()) > 0 {
errMsg = fmt.Sprintf("%s, response body: %s", errMsg, string(resp.Body()))
}
return nil, fmt.Errorf("%s", errMsg)
}
result, ok := resp.Result().(*CreateChatCompletionResponse)
if !ok || result == nil {
return nil, fmt.Errorf("failed to parse response")
}
return result, nil
}
// GenerateContentStream generates content using streaming mode and returns a channel of events.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// ctx := context.Background()
// events, err := client.GenerateContentStream(
// ctx,
// sdk.Ollama,
// "llama2",
// []sdk.Message{
// {Role: sdk.MessageRoleSystem, Content: "You are a helpful assistant."},
// {Role: sdk.MessageRoleUser, Content: "What is Go?"},
// },
// )
// if err != nil {
// log.Fatalf("Error: %v", err)
// }
//
// for event := range events {
// switch event.Event {
// case sdk.StreamEventContentDelta:
// var streamResponse CreateChatCompletionStreamResponse
// if err := json.Unmarshal(*event.Data, &streamResponse); err != nil {
// log.Printf("Error parsing stream response: %v", err)
// continue
// }
//
// for _, choice := range streamResponse.Choices {
// if choice.Delta.Content != "" {
// log.Printf("Content: %s", choice.Delta.Content)
// }
// }
// case sdk.StreamEventMessageError:
// var errResp struct {
// Error string `json:"error"`
// }
// if err := json.Unmarshal(event.Data, &errResp); err != nil {
// log.Printf("Error parsing error: %v", err)
// continue
// }
// log.Printf("Error: %s", errResp.Error)
// }
// }
func (c *clientImpl) GenerateContentStream(ctx context.Context, provider Provider, model string, messages []Message) (<-chan SSEvent, error) {
eventChan := make(chan SSEvent, 100)
request := CreateChatCompletionRequest{
Model: model,
Messages: messages,
Stream: boolPtr(true),
Tools: c.tools,
}
if c.options != nil {
options := *c.options
if options.Model == "" {
options.Model = request.Model
}
if len(options.Messages) == 0 {
options.Messages = request.Messages
}
if options.Tools == nil && c.tools != nil {
options.Tools = c.tools
}
options.Stream = boolPtr(true)
request = options
}
queryParams := make(map[string]string)
if provider != "" {
queryParams["provider"] = string(provider)
}
resp, err := c.executeWithRetry(ctx, func() (*resty.Response, error) {
return c.http.R().
SetContext(ctx).
SetQueryParams(queryParams).
SetBody(request).
SetDoNotParseResponse(true).
Post(fmt.Sprintf("%s/chat/completions", c.baseURL))
})
if err != nil {
close(eventChan)
return eventChan, err
}
if resp.IsError() {
close(eventChan)
body, _ := io.ReadAll(resp.RawBody())
var errorResp Error
if err := json.Unmarshal(body, &errorResp); err == nil && errorResp.Error != nil {
return eventChan, fmt.Errorf("API stream error: %s (status code: %d)", *errorResp.Error, resp.StatusCode())
}
errMsg := fmt.Sprintf("stream request failed with status: %d", resp.StatusCode())
if len(body) > 0 {
errMsg = fmt.Sprintf("%s, response body: %s", errMsg, string(body))
}
return eventChan, fmt.Errorf("%s", errMsg)
}
rawBody := resp.RawBody()
if rawBody == nil {
close(eventChan)
return eventChan, fmt.Errorf("empty response body")
}
go func() {
defer close(eventChan)
defer func() {
err := rawBody.Close()
require.NoError(nil, err, "failed to close response body")
}()
reader := bufio.NewReader(rawBody)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
errorData := []byte(fmt.Sprintf(`{"error": "%s"}`, err.Error()))
eventChan <- SSEvent{
Event: nil,
Data: &errorData,
}
}
return
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimPrefix(line, "data: ")
if data == "[DONE]" {
streamEnd := StreamEnd
eventChan <- SSEvent{
Event: &streamEnd,
}
return
}
contentDelta := ContentDelta
dataBytes := []byte(data)
eventChan <- SSEvent{
Event: &contentDelta,
Data: &dataBytes,
}
}
}()
return eventChan, nil
}
func boolPtr(b bool) *bool {
return &b
}
// HealthCheck performs a health check request to verify API availability.
//
// Example:
//
// client := sdk.NewClient(&sdk.ClientOptions{
// BaseURL: "http://localhost:8080/v1",
// })
// ctx := context.Background()
// err := client.HealthCheck(ctx)
// if err != nil {
// log.Fatalf("Health check failed: %v", err)
// }
func (c *clientImpl) HealthCheck(ctx context.Context) error {
resp, err := c.executeWithRetry(ctx, func() (*resty.Response, error) {
return c.http.R().
SetContext(ctx).
Get(fmt.Sprintf("%s/health", c.baseURL))
})
if err != nil {
return err
}
if resp.IsError() {
return fmt.Errorf("health check failed with status: %d", resp.StatusCode())
}
return nil
}