6312
Ga
go-opencode-ai-zen-sdk / files
go-opencode-ai-zen-sdk / config.go
2.1 KB
1package zen 2 3import ( 4 "errors" 5 "net/http" 6 "strings" 7 "time" 8) 9 10const defaultBaseURL = "https://opencode.ai/zen/v1" 11 12type RetryConfig struct { 13 MaxRetries int 14 RetryOnNonIdempotent bool 15 Backoff func(attempt int) time.Duration 16} 17 18type AuthHeader string 19 20const ( 21 AuthHeaderAuto AuthHeader = "auto" 22 AuthHeaderBearer AuthHeader = "authorization" 23 AuthHeaderAPIKey AuthHeader = "x-api-key" 24 AuthHeaderGoogAPIKey AuthHeader = "x-goog-api-key" 25) 26 27type Config struct { 28 APIKey string 29 BaseURL string 30 // Timeout sets http.Client.Timeout on the internal HTTP client. 31 // 32 // WARNING: http.Client.Timeout is a total round-trip deadline — it starts 33 // when the request is sent and fires while the response body is still being 34 // read. Setting a non-zero value WILL kill SSE/streaming responses that 35 // run longer than the deadline with "context deadline exceeded". 36 // 37 // Leave at 0 (the default) for streaming calls and enforce deadlines via 38 // the request context instead (e.g. context.WithTimeout / WithDeadline). 39 // 40 // ResponseHeaderTimeout sets http.Transport.ResponseHeaderTimeout on the 41 // internal HTTP client. Leave at 0 (the default) to match fetch behavior 42 // and avoid aborting slow-starting streaming responses. 43 // 44 // This field has no effect when HTTPClient is supplied by the caller. 45 ResponseHeaderTimeout time.Duration 46 Timeout time.Duration 47 UserAgent string 48 HTTPClient *http.Client 49 Retry RetryConfig 50 AuthHeader AuthHeader 51} 52 53func (c *Config) applyDefaults() error { 54 if strings.TrimSpace(c.APIKey) == "" { 55 return errors.New("zen: API key is required") 56 } 57 58 if strings.TrimSpace(c.BaseURL) == "" { 59 c.BaseURL = defaultBaseURL 60 } 61 62 c.BaseURL = strings.TrimRight(c.BaseURL, "/") 63 64 if strings.TrimSpace(c.UserAgent) == "" { 65 c.UserAgent = "go-opencode-zen-sdk/0.1" 66 } 67 68 if c.AuthHeader == "" { 69 c.AuthHeader = AuthHeaderAuto 70 } 71 72 if c.Retry.Backoff == nil { 73 c.Retry.Backoff = func(attempt int) time.Duration { 74 base := 200 * time.Millisecond 75 if attempt <= 0 { 76 return base 77 } 78 return base * time.Duration(1<<attempt) 79 } 80 } 81 82 return nil 83}