Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,64 @@ func TestResponsesStreamEventToChatChunksUsesTerminalDoneOutput(t *testing.T) {
assert.Equal(t, "tool_calls", *chunks[3].Choices[0].FinishReason)
}

func TestResponsesStreamEventToChatChunksDoesNotResendToolOnTerminalOutput(t *testing.T) {
state := newTestResponsesStreamState()
outputIndex := 0

var chunks []dto.ChatCompletionsStreamResponse
chunks = append(chunks, mustStreamChunks(t, state, &dto.ResponsesStreamResponse{Type: responsesEventCreated})...)
chunks = append(chunks, mustStreamChunks(t, state, &dto.ResponsesStreamResponse{
Type: responsesEventOutputItemAdded,
OutputIndex: &outputIndex,
Item: &dto.ResponsesOutput{
Type: responsesOutputTypeFunctionCall,
ID: "fc_1",
CallId: "call_1",
Name: "lookup",
},
})...)
chunks = append(chunks, mustStreamChunks(t, state, &dto.ResponsesStreamResponse{
Type: responsesEventFunctionArgsDelta,
OutputIndex: &outputIndex,
Delta: `{"q":"x"}`,
})...)
chunks = append(chunks, mustStreamChunks(t, state, &dto.ResponsesStreamResponse{
Type: responsesEventCompleted,
Response: &dto.OpenAIResponsesResponse{
Status: []byte(`"completed"`),
Output: []dto.ResponsesOutput{
{
Type: responsesOutputTypeFunctionCall,
ID: "fc_1",
CallId: "call_1",
Name: "lookup",
Arguments: []byte(`{"q":"x"}`),
},
},
},
})...)

totalArgs := ""
toolIndexes := map[int]bool{}
var finishReason string
for _, chunk := range chunks {
for _, choice := range chunk.Choices {
for _, tc := range choice.Delta.ToolCalls {
require.NotNil(t, tc.Index)
toolIndexes[*tc.Index] = true
totalArgs += tc.Function.Arguments
}
if choice.FinishReason != nil {
finishReason = *choice.FinishReason
}
}
}

assert.Equal(t, map[int]bool{0: true}, toolIndexes)
assert.Equal(t, `{"q":"x"}`, totalArgs)
assert.Equal(t, "tool_calls", finishReason)
}

func TestFinalizeResponsesToChatStreamFlushesPendingDeltaOnlyArguments(t *testing.T) {
state := newTestResponsesStreamState()
outputIndex := 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ func (s *ResponsesToChatStreamState) ensureToolForEvent(event *dto.ResponsesStre
}

tool := s.toolByKey[key]
if tool == nil {
if itemID := responseStreamEventItemID(event); itemID != "" {
if existingKey := s.itemIDToKey[itemID]; existingKey != "" {
tool = s.toolByKey[existingKey]
}
}
if tool == nil {
if callID := strings.TrimSpace(event.Item.CallId); callID != "" {
if existingKey := s.callIDToKey[callID]; existingKey != "" {
tool = s.toolByKey[existingKey]
}
}
}
if tool != nil {
s.toolByKey[key] = tool
}
}
if tool == nil {
tool = &responsesStreamTool{Key: key, Index: s.nextToolIndex}
s.nextToolIndex++
Expand Down