many changes; using dev laniakea

This commit is contained in:
2026-03-06 14:36:06 +03:00
parent 3ceffa23ae
commit f3f15dad78
13 changed files with 134 additions and 86 deletions

View File

@@ -39,7 +39,7 @@ func NewOpenAIAPI(baseURL, token, model string) *API {
proxy, err := url.Parse(os.Getenv("HTTPS_PROXY"))
if err != nil {
logger.Error(err)
logger.Close()
_ = logger.Close()
return nil
}
t := &http.Transport{}
@@ -99,15 +99,11 @@ func (r *Request[P]) doWithContext(ctx context.Context, api *API) (io.ReadCloser
}
if res.StatusCode == 504 || res.StatusCode == 400 || res.StatusCode == 502 {
api.logger.Warn(fmt.Sprintf("[%d] %s", res.StatusCode, res.Status))
res.Body.Close()
_ = res.Body.Close()
return nil, fmt.Errorf("[%d] %s", res.StatusCode, res.Status)
}
return res.Body, nil
}
func (r *Request[P]) do(api *API) (io.ReadCloser, error) {
ctx := context.Background()
return r.doWithContext(ctx, api)
}
func (r *Request[P]) DoWithContext(ctx context.Context, api *API) (AIResponse, error) {
var zero AIResponse
@@ -115,7 +111,9 @@ func (r *Request[P]) DoWithContext(ctx context.Context, api *API) (AIResponse, e
if err != nil {
return zero, err
}
defer body.Close()
defer func(body io.ReadCloser) {
_ = body.Close()
}(body)
data, err := io.ReadAll(body)
if err != nil {
return zero, err
@@ -140,7 +138,9 @@ func (r *Request[P]) DoStreamWithContext(ctx context.Context, api *API) (iter.Se
reader := bufio.NewReader(body)
return func(yield func(AIResponse, error) bool) {
defer body.Close()
defer func(body io.ReadCloser) {
_ = body.Close()
}(body)
var zero AIResponse
for {
line, err := reader.ReadString('\n')
@@ -152,9 +152,7 @@ func (r *Request[P]) DoStreamWithContext(ctx context.Context, api *API) (iter.Se
continue
}
if strings.HasPrefix(line, "data: ") {
line = line[len("data: "):]
}
line = strings.TrimPrefix(line, "data: ")
line = strings.Trim(strings.Trim(strings.TrimSpace(line), "\r"), "\n")
if strings.HasPrefix(line, "[DONE]") {
return
@@ -192,7 +190,7 @@ func (api *API) handleAIError(body []byte) error {
if !ok {
return errors.New("unknown error code")
}
return errors.New(fmt.Sprintf("%v", code))
return fmt.Errorf("%v", code)
}
return errors.New(string(body))
}