Fix: interrupt on the word 'error' in response

This commit is contained in:
Grail Finder
2025-11-27 20:35:01 +03:00
parent 7bca1e3f28
commit 77774f2356

31
bot.go
View File

@@ -44,7 +44,7 @@ var (
ragger *rag.RAG ragger *rag.RAG
chunkParser ChunkParser chunkParser ChunkParser
lastToolCall *models.FuncCall lastToolCall *models.FuncCall
lastToolCallID string // Store the ID of the most recent tool call lastToolCallID string // Store the ID of the most recent tool call
//nolint:unused // TTS_ENABLED conditionally uses this //nolint:unused // TTS_ENABLED conditionally uses this
orator extra.Orator orator extra.Orator
asr extra.STT asr extra.STT
@@ -190,8 +190,7 @@ func sendMsgToLLM(body io.Reader) {
} }
} else { } else {
// Log the request body for debugging // Log the request body for debugging
logger.Info("sending request to API", "api", cfg.CurrentAPI, "body", string(bodyBytes)) logger.Debug("sending request to API", "api", cfg.CurrentAPI, "body", string(bodyBytes))
// Create request with the captured body // Create request with the captured body
req, err = http.NewRequest("POST", cfg.CurrentAPI, bytes.NewReader(bodyBytes)) req, err = http.NewRequest("POST", cfg.CurrentAPI, bytes.NewReader(bodyBytes))
if err != nil { if err != nil {
@@ -239,6 +238,9 @@ func sendMsgToLLM(body io.Reader) {
logger.Error("error reading response body", "error", err, "line", string(line), logger.Error("error reading response body", "error", err, "line", string(line),
"user_role", cfg.UserRole, "parser", chunkParser, "link", cfg.CurrentAPI) "user_role", cfg.UserRole, "parser", chunkParser, "link", cfg.CurrentAPI)
// if err.Error() != "EOF" { // if err.Error() != "EOF" {
if err := notifyUser("API error", err.Error()); err != nil {
logger.Error("failed to notify", "error", err)
}
streamDone <- true streamDone <- true
break break
// } // }
@@ -268,11 +270,12 @@ func sendMsgToLLM(body io.Reader) {
break break
} }
// Handle error messages in response content // Handle error messages in response content
if string(line) != "" && strings.Contains(strings.ToLower(string(line)), "error") { // example needed, since llm could use the word error in the normal msg
logger.Error("API error response detected", "line", line, "url", cfg.CurrentAPI) // if string(line) != "" && strings.Contains(strings.ToLower(string(line)), "error") {
streamDone <- true // logger.Error("API error response detected", "line", line, "url", cfg.CurrentAPI)
break // streamDone <- true
} // break
// }
if chunk.Finished { if chunk.Finished {
if chunk.Chunk != "" { if chunk.Chunk != "" {
logger.Warn("text inside of finish llmchunk", "chunk", chunk, "counter", counter) logger.Warn("text inside of finish llmchunk", "chunk", chunk, "counter", counter)
@@ -497,8 +500,8 @@ func findCall(msg, toolCall string, tv *tview.TextView) {
m := fc.Name + " is not implemented" m := fc.Name + " is not implemented"
// Create tool response message with the proper tool_call_id // Create tool response message with the proper tool_call_id
toolResponseMsg := models.RoleMsg{ toolResponseMsg := models.RoleMsg{
Role: cfg.ToolRole, Role: cfg.ToolRole,
Content: m, Content: m,
ToolCallID: lastToolCallID, // Use the stored tool call ID ToolCallID: lastToolCallID, // Use the stored tool call ID
} }
chatBody.Messages = append(chatBody.Messages, toolResponseMsg) chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
@@ -512,20 +515,18 @@ func findCall(msg, toolCall string, tv *tview.TextView) {
} }
resp := f(fc.Args) resp := f(fc.Args)
toolMsg := string(resp) // Remove the "tool response: " prefix and %+v formatting toolMsg := string(resp) // Remove the "tool response: " prefix and %+v formatting
logger.Info("llm used tool call", "tool_resp", toolMsg, "tool_attrs", fc)
fmt.Fprintf(tv, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n", fmt.Fprintf(tv, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
"\n", len(chatBody.Messages), cfg.ToolRole, toolMsg) "\n", len(chatBody.Messages), cfg.ToolRole, toolMsg)
// Create tool response message with the proper tool_call_id // Create tool response message with the proper tool_call_id
toolResponseMsg := models.RoleMsg{ toolResponseMsg := models.RoleMsg{
Role: cfg.ToolRole, Role: cfg.ToolRole,
Content: toolMsg, Content: toolMsg,
ToolCallID: lastToolCallID, // Use the stored tool call ID ToolCallID: lastToolCallID, // Use the stored tool call ID
} }
chatBody.Messages = append(chatBody.Messages, toolResponseMsg) chatBody.Messages = append(chatBody.Messages, toolResponseMsg)
// Clear the stored tool call ID after using it // Clear the stored tool call ID after using it
lastToolCallID = "" lastToolCallID = ""
// Trigger the assistant to continue processing with the new tool response // Trigger the assistant to continue processing with the new tool response
// by calling chatRound with empty content to continue the assistant's response // by calling chatRound with empty content to continue the assistant's response
chatRound("", cfg.AssistantRole, tv, false, false) chatRound("", cfg.AssistantRole, tv, false, false)