Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/pkg/cast/chain_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,11 @@ func NewBodyPair(aiMsg *llms.MessageContent, toolMsgs []*llms.MessageContent) *B
break
}
}
for _, id := range partsToDelete {
// Delete in reverse so each removal does not shift the indices of the
// entries still pending deletion (a forward loop with the pre-collected
// ascending indices would delete the wrong elements once there are 2+).
for i := len(partsToDelete) - 1; i >= 0; i-- {
id := partsToDelete[i]
aiMsg.Parts = append(aiMsg.Parts[:id], aiMsg.Parts[id+1:]...)
}
}
Expand Down
28 changes: 28 additions & 0 deletions backend/pkg/cast/newbodypair_reg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cast

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vxcontrol/langchaingo/llms"
)

func TestNewBodyPair_DropsMultipleNilFunctionCallToolCalls(t *testing.T) {
aiMsg := &llms.MessageContent{
Role: llms.ChatMessageTypeAI,
Parts: []llms.ContentPart{
llms.ToolCall{ID: "a", Type: "function", FunctionCall: nil},
llms.ToolCall{ID: "b", Type: "function", FunctionCall: nil},
llms.TextContent{Text: "keep me"},
},
}

NewBodyPair(aiMsg, nil)

assert.Len(t, aiMsg.Parts, 1, "both nil-FunctionCall tool calls should be removed, text kept")
if assert.Len(t, aiMsg.Parts, 1) {
txt, ok := aiMsg.Parts[0].(llms.TextContent)
assert.True(t, ok, "surviving part should be the TextContent, got %T", aiMsg.Parts[0])
assert.Equal(t, "keep me", txt.Text)
}
}