From 01da37b3971a6aa1c3c051c34666672b339ae2b6 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Tue, 25 Nov 2025 11:40:37 +0300 Subject: Fix: openrouter func ctx resp --- bot.go | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'bot.go') diff --git a/bot.go b/bot.go index e8a13e3..2962eb7 100644 --- a/bot.go +++ b/bot.go @@ -44,6 +44,7 @@ var ( ragger *rag.RAG chunkParser ChunkParser lastToolCall *models.FuncCall + lastToolCallID string // Store the ID of the most recent tool call //nolint:unused // TTS_ENABLED conditionally uses this orator extra.Orator asr extra.STT @@ -290,6 +291,8 @@ func sendMsgToLLM(body io.Reader) { openAIToolChan <- chunk.ToolChunk if chunk.FuncName != "" { lastToolCall.Name = chunk.FuncName + // Store the tool call ID for the response + lastToolCallID = chunk.ToolID } interrupt: if interruptResp { // read bytes, so it would not get into beginning of the next req @@ -492,14 +495,40 @@ func findCall(msg, toolCall string, tv *tview.TextView) { f, ok := fnMap[fc.Name] if !ok { m := fc.Name + " is not implemented" - chatRound(m, cfg.ToolRole, tv, false, false) + // Create tool response message with the proper tool_call_id + toolResponseMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: m, + ToolCallID: lastToolCallID, // Use the stored tool call ID + } + chatBody.Messages = append(chatBody.Messages, toolResponseMsg) + // Clear the stored tool call ID after using it + lastToolCallID = "" + + // Trigger the assistant to continue processing with the new tool response + // by calling chatRound with empty content to continue the assistant's response + chatRound("", cfg.AssistantRole, tv, false, false) return } resp := f(fc.Args) - toolMsg := fmt.Sprintf("tool response: %+v", string(resp)) + toolMsg := string(resp) // Remove the "tool response: " prefix and %+v formatting fmt.Fprintf(tv, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n", "\n", len(chatBody.Messages), cfg.ToolRole, toolMsg) - chatRound(toolMsg, cfg.ToolRole, tv, false, false) + + // Create tool response message with the proper tool_call_id + toolResponseMsg := models.RoleMsg{ + Role: cfg.ToolRole, + Content: toolMsg, + ToolCallID: lastToolCallID, // Use the stored tool call ID + } + chatBody.Messages = append(chatBody.Messages, toolResponseMsg) + + // Clear the stored tool call ID after using it + lastToolCallID = "" + + // Trigger the assistant to continue processing with the new tool response + // by calling chatRound with empty content to continue the assistant's response + chatRound("", cfg.AssistantRole, tv, false, false) } func chatToTextSlice(showSys bool) []string { -- cgit v1.2.3