responses_to_chatcompletions_codex_events_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package apicompat
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. )
  7. // custom_tool_call(custom/freeform 工具,如新版 apply_patch)应像 function_call 一样
  8. // 注册为工具调用,其 *_input.delta 增量映射到正确的工具索引。
  9. func TestResponsesEventToChatChunks_CustomToolCallInputDelta(t *testing.T) {
  10. state := NewResponsesEventToChatState()
  11. state.Model = "gpt-5-codex"
  12. state.SentRole = true
  13. chunks := ResponsesEventToChatChunks(&ResponsesStreamEvent{
  14. Type: "response.output_item.added",
  15. OutputIndex: 1,
  16. Item: &ResponsesOutput{
  17. Type: "custom_tool_call",
  18. CallID: "call_patch",
  19. Name: "apply_patch",
  20. },
  21. }, state)
  22. require.Len(t, chunks, 1)
  23. require.Len(t, chunks[0].Choices[0].Delta.ToolCalls, 1)
  24. tc := chunks[0].Choices[0].Delta.ToolCalls[0]
  25. assert.Equal(t, "call_patch", tc.ID)
  26. assert.Equal(t, "apply_patch", tc.Function.Name)
  27. chunks = ResponsesEventToChatChunks(&ResponsesStreamEvent{
  28. Type: "response.custom_tool_call_input.delta",
  29. OutputIndex: 1,
  30. Delta: "*** Begin Patch",
  31. }, state)
  32. require.Len(t, chunks, 1)
  33. tc = chunks[0].Choices[0].Delta.ToolCalls[0]
  34. require.NotNil(t, tc.Index)
  35. assert.Equal(t, 0, *tc.Index)
  36. assert.Equal(t, "*** Begin Patch", tc.Function.Arguments)
  37. }
  38. // 原始推理文本增量 reasoning_text.delta 应像 reasoning_summary_text.delta 一样
  39. // 映射为 reasoning_content。
  40. func TestResponsesEventToChatChunks_ReasoningTextDelta(t *testing.T) {
  41. state := NewResponsesEventToChatState()
  42. state.Model = "gpt-5-codex"
  43. state.SentRole = true
  44. chunks := ResponsesEventToChatChunks(&ResponsesStreamEvent{
  45. Type: "response.reasoning_text.delta",
  46. Delta: "thinking step",
  47. }, state)
  48. require.Len(t, chunks, 1)
  49. require.NotNil(t, chunks[0].Choices[0].Delta.ReasoningContent)
  50. assert.Equal(t, "thinking step", *chunks[0].Choices[0].Delta.ReasoningContent)
  51. }
  52. // 缓冲(非流式)累加器同样需识别两类新事件。
  53. func TestBufferedResponseAccumulator_CodexEvents(t *testing.T) {
  54. acc := NewBufferedResponseAccumulator()
  55. acc.ProcessEvent(&ResponsesStreamEvent{
  56. Type: "response.output_item.added",
  57. OutputIndex: 0,
  58. Item: &ResponsesOutput{Type: "custom_tool_call", CallID: "c1", Name: "apply_patch"},
  59. })
  60. acc.ProcessEvent(&ResponsesStreamEvent{
  61. Type: "response.custom_tool_call_input.delta",
  62. OutputIndex: 0,
  63. Delta: "patch-body",
  64. })
  65. acc.ProcessEvent(&ResponsesStreamEvent{
  66. Type: "response.reasoning_text.delta",
  67. Delta: "raw-reasoning",
  68. })
  69. require.True(t, acc.HasContent())
  70. }