fix: handle providers that do not send unique tool call IDs

kujtimiihoxha created

Change summary

internal/llm/provider/openai.go | 10 ++++++++++
1 file changed, 10 insertions(+)

Detailed changes

internal/llm/provider/openai.go 🔗

@@ -340,6 +340,7 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
 			toolCalls := make([]message.ToolCall, 0)
 			msgToolCalls := make(map[int64]openai.ChatCompletionMessageToolCall)
 			toolMap := make(map[string]openai.ChatCompletionMessageToolCall)
+			toolCallIDMap := make(map[string]string)
 			for openaiStream.Next() {
 				chunk := openaiStream.Current()
 				// Kujtim: this is an issue with openrouter qwen, its sending -1 for the tool index
@@ -367,6 +368,15 @@ func (o *openaiClient) stream(ctx context.Context, messages []message.Message, t
 						currentContent += choice.Delta.Content
 					} else if len(choice.Delta.ToolCalls) > 0 {
 						toolCall := choice.Delta.ToolCalls[0]
+						if strings.HasPrefix(toolCall.ID, "functions.") {
+							exID, ok := toolCallIDMap[toolCall.ID]
+							if !ok {
+								newID := uuid.NewString()
+								toolCallIDMap[toolCall.ID] = newID
+								toolCall.ID = newID
+							}
+							toolCall.ID = exID
+						}
 						newToolCall := false
 						if existingToolCall, ok := msgToolCalls[toolCall.Index]; ok { // tool call exists
 							if toolCall.ID != "" && toolCall.ID != existingToolCall.ID {