51 lines
958 B
Go
51 lines
958 B
Go
package llm
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func resolvedHTTPClient(base *http.Client, timeout time.Duration) *http.Client {
|
|
if base == nil {
|
|
if timeout <= 0 {
|
|
return http.DefaultClient
|
|
}
|
|
return &http.Client{Timeout: timeout}
|
|
}
|
|
|
|
if timeout <= 0 {
|
|
return base
|
|
}
|
|
|
|
cloned := *base
|
|
cloned.Timeout = timeout
|
|
return &cloned
|
|
}
|
|
|
|
func validateOutputTarget(out any) error {
|
|
if out == nil {
|
|
return fmt.Errorf("output target must not be nil")
|
|
}
|
|
value := reflect.ValueOf(out)
|
|
if value.Kind() != reflect.Ptr || value.IsNil() {
|
|
return fmt.Errorf("output target must be a non-nil pointer")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func sanitizeError(err error, apiKey string) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
msg := err.Error()
|
|
key := strings.TrimSpace(apiKey)
|
|
if key != "" {
|
|
msg = strings.ReplaceAll(msg, key, "[REDACTED]")
|
|
msg = strings.ReplaceAll(msg, "Bearer "+key, "Bearer [REDACTED]")
|
|
}
|
|
return fmt.Errorf("%s", msg)
|
|
}
|