From 685738a5a4f7488a0f1b87d360c71912aa575d65 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Wed, 4 Feb 2026 13:54:54 +0300 Subject: Enha: force stop string on client side --- bot.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'bot.go') diff --git a/bot.go b/bot.go index c396d07..bbb3f65 100644 --- a/bot.go +++ b/bot.go @@ -540,6 +540,8 @@ func extractDetailedErrorFromBytes(body []byte, statusCode int) string { // sendMsgToLLM expects streaming resp func sendMsgToLLM(body io.Reader) { choseChunkParser() + // openrouter does not respect stop strings, so we have to cut the message ourselves + stopStrings := chatBody.MakeStopSliceExcluding("", listChatRoles()) req, err := http.NewRequest("POST", cfg.CurrentAPI, body) if err != nil { logger.Error("newreq error", "error", err) @@ -678,6 +680,12 @@ func sendMsgToLLM(body io.Reader) { } // bot sends way too many \n answerText = strings.ReplaceAll(chunk.Chunk, "\n\n", "\n") + // Accumulate text to check for stop strings that might span across chunks + // check if chunk is in stopstrings => stop + if slices.Contains(stopStrings, answerText) { + logger.Debug("Stop string detected and handled", "stop_string", answerText) + streamDone <- true + } chunkChan <- answerText openAIToolChan <- chunk.ToolChunk if chunk.FuncName != "" { -- cgit v1.2.3