Enha: catching tool call id

This commit is contained in:
Grail Finder
2025-12-10 13:07:06 +03:00
parent 0fc70ae441
commit c80ff60668
2 changed files with 28 additions and 8 deletions

29
llm.go
View File

@@ -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)