From b5ade054896a3fd73cb9de64fa76145f4ae94829 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Mon, 24 Nov 2025 13:06:42 +0300 Subject: Enha: or img attachment --- bot.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 13 deletions(-) (limited to 'bot.go') diff --git a/bot.go b/bot.go index d5b4a97..e8a13e3 100644 --- a/bot.go +++ b/bot.go @@ -151,21 +151,63 @@ func fetchORModels(free bool) ([]string, error) { func sendMsgToLLM(body io.Reader) { choseChunkParser() - // nolint - req, err := http.NewRequest("POST", cfg.CurrentAPI, body) - if err != nil { - logger.Error("newreq error", "error", err) - if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil { - logger.Error("failed to notify", "error", err) + + var req *http.Request + var err error + + // Capture and log the request body for debugging + if _, ok := body.(*io.LimitedReader); ok { + // If it's a LimitedReader, we need to handle it differently + logger.Debug("request body type is LimitedReader", "parser", chunkParser, "link", cfg.CurrentAPI) + req, err = http.NewRequest("POST", cfg.CurrentAPI, body) + if err != nil { + logger.Error("newreq error", "error", err) + if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil { + logger.Error("failed to notify", "error", err) + } + streamDone <- true + return } - streamDone <- true - return + req.Header.Add("Accept", "application/json") + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+chunkParser.GetToken()) + req.Header.Set("Accept-Encoding", "gzip") + } else { + // For other reader types, capture and log the body content + bodyBytes, err := io.ReadAll(body) + if err != nil { + logger.Error("failed to read request body for logging", "error", err) + // Create request with original body if reading fails + req, err = http.NewRequest("POST", cfg.CurrentAPI, bytes.NewReader(bodyBytes)) + if err != nil { + logger.Error("newreq error", "error", err) + if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil { + logger.Error("failed to notify", "error", err) + } + streamDone <- true + return + } + } else { + // Log the request body for debugging + logger.Info("sending request to API", "api", cfg.CurrentAPI, "body", string(bodyBytes)) + + // Create request with the captured body + req, err = http.NewRequest("POST", cfg.CurrentAPI, bytes.NewReader(bodyBytes)) + if err != nil { + logger.Error("newreq error", "error", err) + if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil { + logger.Error("failed to notify", "error", err) + } + streamDone <- true + return + } + } + + req.Header.Add("Accept", "application/json") + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+chunkParser.GetToken()) + req.Header.Set("Accept-Encoding", "gzip") } - req.Header.Add("Accept", "application/json") - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", "Bearer "+chunkParser.GetToken()) - // req.Header.Set("Content-Length", strconv.Itoa(len(bodyBytes))) - req.Header.Set("Accept-Encoding", "gzip") // nolint resp, err := httpClient.Do(req) if err != nil { -- cgit v1.2.3