summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrail Finder <wohilas@gmail.com>2025-12-10 13:07:06 +0300
committerGrail Finder <wohilas@gmail.com>2025-12-10 13:07:06 +0300
commitc80ff6066872b054b32fe83e8070ba7dd16a56ef (patch)
tree6b437a095796768588a5709e65ca3763690a439e
parent0fc70ae44134b7771d37796f680df359dde72112 (diff)
Enha: catching tool call id
-rw-r--r--bot.go7
-rw-r--r--llm.go29
2 files changed, 28 insertions, 8 deletions
diff --git a/bot.go b/bot.go
index 90fbe96..a5f5602 100644
--- a/bot.go
+++ b/bot.go
@@ -629,6 +629,13 @@ func findCall(msg, toolCall string, tv *tview.TextView) {
}
lastToolCall.Args = openAIToolMap
fc = lastToolCall
+ // Ensure lastToolCallID is set if it's available in the tool call
+ if lastToolCallID == "" && len(openAIToolMap) > 0 {
+ // Attempt to extract ID from the parsed tool call if not already set
+ if id, exists := openAIToolMap["id"]; exists {
+ lastToolCallID = id
+ }
+ }
} else {
jsStr := toolCallRE.FindString(msg)
if jsStr == "" {
diff --git a/llm.go b/llm.go
index beb9273..712fc81 100644
--- a/llm.go
+++ b/llm.go
@@ -186,19 +186,32 @@ func (op LCPChat) ParseChunk(data []byte) (*models.TextChunk, error) {
logger.Error("failed to decode", "error", err, "line", string(data))
return nil, err
}
+
+ // Handle multiple choices safely
+ if len(llmchunk.Choices) == 0 {
+ logger.Warn("LCPChat ParseChunk: no choices in response", "data", string(data))
+ return &models.TextChunk{Finished: true}, nil
+ }
+
resp := &models.TextChunk{
Chunk: llmchunk.Choices[len(llmchunk.Choices)-1].Delta.Content,
}
- if len(llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls) > 0 {
- toolCall := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0]
- resp.ToolChunk = toolCall.Function.Arguments
- fname := toolCall.Function.Name
- if fname != "" {
- resp.FuncName = fname
+
+ // Check for tool calls in all choices, not just the last one
+ for _, choice := range llmchunk.Choices {
+ if len(choice.Delta.ToolCalls) > 0 {
+ toolCall := choice.Delta.ToolCalls[0]
+ resp.ToolChunk = toolCall.Function.Arguments
+ fname := toolCall.Function.Name
+ if fname != "" {
+ resp.FuncName = fname
+ }
+ // Capture the tool call ID if available
+ resp.ToolID = toolCall.ID
+ break // Process only the first tool call
}
- // Capture the tool call ID if available
- resp.ToolID = toolCall.ID
}
+
if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason == "stop" {
if resp.Chunk != "" {
logger.Error("text inside of finish llmchunk", "chunk", llmchunk)