|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/docker/docker-agent/pkg/chat" |
| 10 | + "github.com/docker/docker-agent/pkg/tools" |
| 11 | +) |
| 12 | + |
| 13 | +func TestConvertMessagesToResponseInput_OrphanedFunctionCall(t *testing.T) { |
| 14 | + // Simulate a conversation where an assistant made 2 tool calls but only |
| 15 | + // one has a result (the other was cancelled/interrupted). |
| 16 | + messages := []chat.Message{ |
| 17 | + {Role: chat.MessageRoleUser, Content: "hello"}, |
| 18 | + { |
| 19 | + Role: chat.MessageRoleAssistant, |
| 20 | + ToolCalls: []tools.ToolCall{ |
| 21 | + {ID: "call_1", Type: "function", Function: tools.FunctionCall{Name: "tool_a", Arguments: "{}"}}, |
| 22 | + {ID: "call_2", Type: "function", Function: tools.FunctionCall{Name: "tool_b", Arguments: "{}"}}, |
| 23 | + }, |
| 24 | + }, |
| 25 | + {Role: chat.MessageRoleTool, Content: "result a", ToolCallID: "call_1"}, |
| 26 | + // call_2 has no result — orphaned |
| 27 | + } |
| 28 | + |
| 29 | + input := convertMessagesToResponseInput(messages) |
| 30 | + |
| 31 | + // Count function calls and outputs |
| 32 | + var callIDs, outputIDs []string |
| 33 | + for _, item := range input { |
| 34 | + if item.OfFunctionCall != nil { |
| 35 | + callIDs = append(callIDs, item.OfFunctionCall.CallID) |
| 36 | + } |
| 37 | + if item.OfFunctionCallOutput != nil { |
| 38 | + outputIDs = append(outputIDs, item.OfFunctionCallOutput.CallID) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + require.Len(t, callIDs, 2) |
| 43 | + require.Len(t, outputIDs, 2, "orphaned function call should get a placeholder output") |
| 44 | + |
| 45 | + assert.Contains(t, outputIDs, "call_1") |
| 46 | + assert.Contains(t, outputIDs, "call_2") |
| 47 | +} |
| 48 | + |
| 49 | +func TestConvertMessagesToResponseInput_NoOrphans(t *testing.T) { |
| 50 | + // All tool calls have matching results — no placeholder needed. |
| 51 | + messages := []chat.Message{ |
| 52 | + {Role: chat.MessageRoleUser, Content: "hello"}, |
| 53 | + { |
| 54 | + Role: chat.MessageRoleAssistant, |
| 55 | + ToolCalls: []tools.ToolCall{ |
| 56 | + {ID: "call_1", Type: "function", Function: tools.FunctionCall{Name: "tool_a", Arguments: "{}"}}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + {Role: chat.MessageRoleTool, Content: "result a", ToolCallID: "call_1"}, |
| 60 | + } |
| 61 | + |
| 62 | + input := convertMessagesToResponseInput(messages) |
| 63 | + |
| 64 | + var outputCount int |
| 65 | + for _, item := range input { |
| 66 | + if item.OfFunctionCallOutput != nil { |
| 67 | + outputCount++ |
| 68 | + } |
| 69 | + } |
| 70 | + assert.Equal(t, 1, outputCount, "should not inject extra outputs when all calls have results") |
| 71 | +} |
0 commit comments