mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
fix(openai): follow the streaming path for rebuilt buffered items
A rebuilt output skips a web search that is not completed, gives an empty custom tool input as {} and keeps a reasoning signature that only the added event carries.
Deltas without an output_index join their item by item_id or as the newest open item of their kind, and deltas that came before the item was added belong to it.
Items that share an output_index or have none keep their own events: an item of another kind, a done item that is already closed and a different call_id each get their own slot.
This commit is contained in:
1 parent
5279823757
commit
7f1ecfaa2d
4 files changed
+260
-31
No files matched your search
@@ -154,12 +154,13 @@ func containsAnthropicToolUseBlock(blocks []AnthropicContentBlock) bool {
|
||||
// arguments are not an object (truncated by max_output_tokens, or empty) gets {};
|
||||
// a custom_tool_call's free-form input is wrapped in the same {"input": ...}
|
||||
// shape that downgrades a custom tool to a function tool, so that
|
||||
// extractCustomToolCallInput gives the text back.
|
||||
// extractCustomToolCallInput gives the text back; an empty custom input is {} as
|
||||
// it is in the streamed tool_use.
|
||||
func anthropicToolUseInput(itemType, name, raw string) json.RawMessage {
|
||||
if trimmed := strings.TrimSpace(raw); strings.HasPrefix(trimmed, "{") && json.Valid([]byte(trimmed)) {
|
||||
return sanitizeAnthropicToolUseInput(name, raw)
|
||||
}
|
||||
if itemType == "custom_tool_call" {
|
||||
if itemType == "custom_tool_call" && raw != "" {
|
||||
wrapped, err := json.Marshal(map[string]string{"input": raw})
|
||||
if err == nil {
|
||||
return wrapped
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestAnthropicToolUseInput(t *testing.T) {
|
||||
{"empty function arguments", "function_call", "", "{}"},
|
||||
{"non-object function arguments", "function_call", "[1]", "{}"},
|
||||
{"custom input wrapped", "custom_tool_call", "dir /b", "{\"input\":\"dir /b\"}"},
|
||||
{"empty custom input wrapped", "custom_tool_call", "", "{\"input\":\"\"}"},
|
||||
{"empty custom input is an empty object", "custom_tool_call", "", "{}"},
|
||||
{"custom input that is an object kept", "custom_tool_call", "{\"a\":1}", "{\"a\":1}"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
|
||||
@@ -64,6 +64,7 @@ type openAICompatOutputSlot struct {
|
||||
index int64
|
||||
indexed bool
|
||||
itemType string
|
||||
id string
|
||||
added *apicompat.ResponsesOutput
|
||||
done *apicompat.ResponsesOutput
|
||||
reasoning strings.Builder
|
||||
@@ -92,48 +93,48 @@ func (o *openAICompatOutputSlots) Observe(payload []byte) {
|
||||
switch strings.TrimSpace(gjson.GetBytes(payload, "type").String()) {
|
||||
case "response.output_item.added":
|
||||
if item, ok := openAICompatEventItem(payload); ok {
|
||||
slot := o.slot(payload, item.Type, item.ID, false, true)
|
||||
slot := o.slot(payload, item.Type, item.ID, item.CallID, false, true)
|
||||
slot.added = &item
|
||||
}
|
||||
case "response.output_item.done":
|
||||
if item, ok := openAICompatEventItem(payload); ok {
|
||||
slot := o.slot(payload, item.Type, item.ID, true, true)
|
||||
slot := o.slot(payload, item.Type, item.ID, item.CallID, true, true)
|
||||
slot.done = &item
|
||||
}
|
||||
case "response.output_text.delta":
|
||||
if delta := gjson.GetBytes(payload, "delta").String(); delta != "" {
|
||||
o.slot(payload, "message", "", false, true).text.write(gjson.GetBytes(payload, "content_index").Int(), delta)
|
||||
o.slot(payload, "message", gjson.GetBytes(payload, "item_id").String(), "", false, true).text.write(gjson.GetBytes(payload, "content_index").Int(), delta)
|
||||
}
|
||||
case "response.refusal.delta":
|
||||
if delta := gjson.GetBytes(payload, "delta").String(); delta != "" {
|
||||
o.slot(payload, "message", "", false, true).refusal.write(gjson.GetBytes(payload, "content_index").Int(), delta)
|
||||
o.slot(payload, "message", gjson.GetBytes(payload, "item_id").String(), "", false, true).refusal.write(gjson.GetBytes(payload, "content_index").Int(), delta)
|
||||
}
|
||||
case "response.reasoning_summary_text.delta":
|
||||
if delta := gjson.GetBytes(payload, "delta").String(); delta != "" {
|
||||
slot := o.slot(payload, "reasoning", "", false, true)
|
||||
slot := o.slot(payload, "reasoning", gjson.GetBytes(payload, "item_id").String(), "", false, true)
|
||||
_, _ = slot.reasoning.WriteString(delta)
|
||||
slot.summaryText.write(gjson.GetBytes(payload, "summary_index").Int(), delta)
|
||||
}
|
||||
case "response.reasoning_text.delta":
|
||||
if delta := gjson.GetBytes(payload, "delta").String(); delta != "" {
|
||||
slot := o.slot(payload, "reasoning", "", false, true)
|
||||
slot := o.slot(payload, "reasoning", gjson.GetBytes(payload, "item_id").String(), "", false, true)
|
||||
_, _ = slot.reasoning.WriteString(delta)
|
||||
slot.reasoningText.write(gjson.GetBytes(payload, "content_index").Int(), delta)
|
||||
}
|
||||
case "response.function_call_arguments.delta", "response.custom_tool_call_input.delta":
|
||||
if delta := gjson.GetBytes(payload, "delta").String(); delta != "" {
|
||||
if slot := o.slot(payload, "function_call", "", false, false); slot != nil {
|
||||
if slot := o.slot(payload, "function_call", gjson.GetBytes(payload, "item_id").String(), "", false, false); slot != nil {
|
||||
_, _ = slot.args.WriteString(delta)
|
||||
}
|
||||
}
|
||||
case "response.function_call_arguments.done":
|
||||
if slot := o.slot(payload, "function_call", "", false, false); slot != nil {
|
||||
if slot := o.slot(payload, "function_call", gjson.GetBytes(payload, "item_id").String(), "", false, false); slot != nil {
|
||||
if completed := gjson.GetBytes(payload, "arguments").String(); completed != "" {
|
||||
slot.argsDone = completed
|
||||
}
|
||||
}
|
||||
case "response.custom_tool_call_input.done":
|
||||
if slot := o.slot(payload, "function_call", "", false, false); slot != nil {
|
||||
if slot := o.slot(payload, "function_call", gjson.GetBytes(payload, "item_id").String(), "", false, false); slot != nil {
|
||||
if completed := gjson.GetBytes(payload, "input").String(); completed != "" {
|
||||
slot.argsDone = completed
|
||||
}
|
||||
@@ -151,17 +152,24 @@ func openAICompatEventItem(payload []byte) (apicompat.ResponsesOutput, bool) {
|
||||
return item, item.Type != ""
|
||||
}
|
||||
|
||||
// slot finds the slot an event belongs to. A done event whose item has an id
|
||||
// slot finds the slot an event belongs to. id is the id of the item the event
|
||||
// is about (the item of an added or done event, the item_id of a delta) and
|
||||
// callID the call_id of a tool call item. A done event whose item has an id
|
||||
// belongs to the slot that already holds a done item with that id, so a repeated
|
||||
// item replaces its earlier copy and items that merely share an output_index stay
|
||||
// apart. Otherwise an event with an output_index belongs to the newest slot of
|
||||
// that index (an added event: the newest one that has none yet; a done event
|
||||
// without an id replaces the done item of that slot). An event without an
|
||||
// output_index belongs to the newest unindexed slot of its kind (a done event:
|
||||
// the oldest one still without a done item), and an added event always starts a
|
||||
// new slot, so unindexed items keep arrival order. With create unset, a missing
|
||||
// slot returns nil.
|
||||
func (o *openAICompatOutputSlots) slot(payload []byte, kind, id string, done, create bool) *openAICompatOutputSlot {
|
||||
// item replaces its earlier copy. Otherwise an event whose id a slot already
|
||||
// has, because the deltas or the added event of that item opened it, belongs to
|
||||
// that slot, also when the event has no output_index. Otherwise an event with an
|
||||
// output_index belongs to the newest slot of that index that can hold its kind
|
||||
// of item and has no other id (an added event: the newest one without an added or
|
||||
// done item; a done event with an id: the newest one without a done item; a done
|
||||
// event without an id replaces the done item of that slot), so items that merely
|
||||
// share an output_index stay apart. An event without an output_index belongs to
|
||||
// the newest slot of its kind that is not done, else to the newest unindexed
|
||||
// one; a done event with a call_id belongs to the slot of that call, else to the
|
||||
// oldest unindexed slot of its kind without a done item, and an added event
|
||||
// always starts a new slot, so unindexed items keep arrival order. With create
|
||||
// unset, a missing slot returns nil.
|
||||
func (o *openAICompatOutputSlots) slot(payload []byte, kind, id, callID string, done, create bool) *openAICompatOutputSlot {
|
||||
index := gjson.GetBytes(payload, "output_index")
|
||||
added := strings.TrimSpace(gjson.GetBytes(payload, "type").String()) == "response.output_item.added"
|
||||
matches := func(slot *openAICompatOutputSlot) bool {
|
||||
@@ -170,6 +178,9 @@ func (o *openAICompatOutputSlots) slot(payload []byte, kind, id string, done, cr
|
||||
}
|
||||
return openAICompatIsToolCallType(kind) && openAICompatIsToolCallType(slot.itemType)
|
||||
}
|
||||
fits := func(slot *openAICompatOutputSlot) bool {
|
||||
return matches(slot) && (slot.id == "" || id == "" || slot.id == id)
|
||||
}
|
||||
newest := func(accept func(*openAICompatOutputSlot) bool) *openAICompatOutputSlot {
|
||||
for i := len(o.slots) - 1; i >= 0; i-- {
|
||||
if accept(o.slots[i]) {
|
||||
@@ -182,40 +193,64 @@ func (o *openAICompatOutputSlots) slot(payload []byte, kind, id string, done, cr
|
||||
if done && id != "" {
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool { return slot.done != nil && slot.done.ID == id })
|
||||
}
|
||||
if found == nil && id != "" && (done || added || !index.Exists()) {
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool {
|
||||
return slot.id == id && matches(slot) && slot.done == nil && (!added || slot.added == nil)
|
||||
})
|
||||
}
|
||||
if found == nil {
|
||||
switch {
|
||||
case index.Exists():
|
||||
atIndex := func(slot *openAICompatOutputSlot) bool { return slot.indexed && slot.index == index.Int() }
|
||||
atIndex := func(slot *openAICompatOutputSlot) bool {
|
||||
return slot.indexed && slot.index == index.Int() && matches(slot)
|
||||
}
|
||||
switch {
|
||||
case done && id != "":
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool { return atIndex(slot) && slot.done == nil })
|
||||
case added:
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool { return atIndex(slot) && slot.added == nil })
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool { return atIndex(slot) && slot.added == nil && slot.done == nil })
|
||||
default:
|
||||
found = newest(atIndex)
|
||||
}
|
||||
case added:
|
||||
case done:
|
||||
if callID != "" {
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool {
|
||||
return slot.done == nil && slot.added != nil && slot.added.CallID == callID && matches(slot)
|
||||
})
|
||||
}
|
||||
for _, slot := range o.slots {
|
||||
if !slot.indexed && slot.done == nil && slot.itemType == kind {
|
||||
if found == nil && !slot.indexed && slot.done == nil && slot.itemType == kind {
|
||||
found = slot
|
||||
break
|
||||
}
|
||||
}
|
||||
default:
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool { return !slot.indexed && matches(slot) })
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool { return slot.done == nil && fits(slot) })
|
||||
if found == nil {
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool { return !slot.indexed && fits(slot) })
|
||||
}
|
||||
}
|
||||
}
|
||||
if found == nil && index.Exists() && (added || done) {
|
||||
// Deltas that came with no output_index and before their item was announced
|
||||
// belong to the newest such item of this kind.
|
||||
found = newest(func(slot *openAICompatOutputSlot) bool {
|
||||
return !slot.indexed && slot.added == nil && slot.done == nil && fits(slot)
|
||||
})
|
||||
}
|
||||
if found == nil {
|
||||
if !create {
|
||||
return nil
|
||||
}
|
||||
found = &openAICompatOutputSlot{}
|
||||
if index.Exists() {
|
||||
found.index, found.indexed = index.Int(), true
|
||||
}
|
||||
o.slots = append(o.slots, found)
|
||||
}
|
||||
if index.Exists() && !found.indexed {
|
||||
found.index, found.indexed = index.Int(), true
|
||||
}
|
||||
if found.id == "" {
|
||||
found.id = id
|
||||
}
|
||||
if found.itemType == "" || done || added {
|
||||
found.itemType = kind
|
||||
}
|
||||
@@ -249,6 +284,10 @@ func (o *openAICompatOutputSlots) fillTerminalOutput(eventType string, response
|
||||
})
|
||||
for _, slot := range slots {
|
||||
if item, ok := slot.build(); ok {
|
||||
// The streaming converters render a web search only once it is completed.
|
||||
if item.Type == "web_search_call" && item.Status != "completed" {
|
||||
continue
|
||||
}
|
||||
response.Output = append(response.Output, item)
|
||||
if item.Type == "custom_tool_call" {
|
||||
response.RenderCustomToolCalls = true
|
||||
@@ -277,6 +316,10 @@ func (s *openAICompatOutputSlot) buildReasoning() (apicompat.ResponsesOutput, bo
|
||||
if s.done != nil {
|
||||
item = *s.done
|
||||
}
|
||||
// The signature may be on the added event only.
|
||||
if s.added != nil && strings.TrimSpace(item.EncryptedContent) == "" {
|
||||
item.EncryptedContent = s.added.EncryptedContent
|
||||
}
|
||||
// The converters render the summary text and fall back to the reasoning_text
|
||||
// content only when the summary has none, so an item carrying both would lose
|
||||
// one. The deltas carried every text the client is shown, so they stand in
|
||||
@@ -325,7 +368,7 @@ func (s *openAICompatOutputSlot) buildReasoning() (apicompat.ResponsesOutput, bo
|
||||
}
|
||||
}
|
||||
}
|
||||
return item, s.done != nil || s.reasoning.Len() > 0
|
||||
return item, s.done != nil || s.reasoning.Len() > 0 || strings.TrimSpace(item.EncryptedContent) != ""
|
||||
}
|
||||
|
||||
func (s *openAICompatOutputSlot) buildMessage() (apicompat.ResponsesOutput, bool) {
|
||||
|
||||
@@ -590,3 +590,188 @@ func TestBufferedCompat_PartialReasoningParts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The streaming converters render a web search once it is completed, so a
|
||||
// rebuilt output does the same.
|
||||
func TestBufferedCompat_WebSearchNotCompletedIsNotRebuilt(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
status string
|
||||
want []string
|
||||
}{
|
||||
{"completed", []string{"server_tool_use", "web_search_tool_result"}},
|
||||
{"in_progress", []string{"text"}},
|
||||
} {
|
||||
t.Run(tc.status, func(t *testing.T) {
|
||||
sse := bufferedItemsSSE(
|
||||
bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"web_search_call","id":"ws_1","status":"`+tc.status+`","action":{"type":"search","query":"q"}}}`,
|
||||
bufferedHostedItemsCompleted,
|
||||
)
|
||||
out := forwardBufferedHostedItems(t, "/v1/messages", bufferedHostedItemsMessagesBody, sse)
|
||||
var types []string
|
||||
for _, block := range gjson.Get(out, "content").Array() {
|
||||
types = append(types, block.Get("type").String())
|
||||
}
|
||||
require.Equal(t, tc.want, types, out)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A custom tool call without input is {} as in the streamed tool_use.
|
||||
func TestBufferedCompat_EmptyCustomToolInputIsAnEmptyObject(t *testing.T) {
|
||||
sse := bufferedItemsSSE(
|
||||
bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"custom_tool_call","id":"ctc_1","call_id":"call_1","name":"exec","input":""}}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"custom_tool_call","id":"ctc_1","call_id":"call_1","name":"exec","input":""}}`,
|
||||
bufferedHostedItemsCompleted,
|
||||
)
|
||||
toolBody := `{"model":"gpt-5.5","max_tokens":64,"stream":false,"tools":[{"name":"exec","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`
|
||||
out := forwardBufferedHostedItems(t, "/v1/messages", toolBody, sse)
|
||||
require.Equal(t, "tool_use", gjson.Get(out, "content.0.type").String(), out)
|
||||
require.Equal(t, "{}", gjson.Get(out, "content.0.input").Raw, out)
|
||||
}
|
||||
|
||||
// Deltas without an output_index belong to their item, found by item_id or as the
|
||||
// newest open item of their kind, not to a second copy of it.
|
||||
func TestBufferedCompat_DeltasWithoutOutputIndexJoinTheirItem(t *testing.T) {
|
||||
customBody := `{"model":"gpt-5.5","stream":false,"tools":[{"type":"custom","custom":{"name":"exec","description":"run"}}],"messages":[{"role":"user","content":"hi"}]}`
|
||||
cases := []struct {
|
||||
name, shown string
|
||||
sse string
|
||||
}{
|
||||
{"item_id, no added event", "text", bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_text.delta","item_id":"msg_1","content_index":0,"delta":"hello"}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","content":[{"type":"output_text","text":"hello"}]}}`,
|
||||
bufferedHostedItemsCompleted)},
|
||||
{"item_id, after added", "thinking", bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1","summary":[]}}`,
|
||||
`{"type":"response.reasoning_text.delta","item_id":"rs_1","content_index":0,"delta":"hello"}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_1","summary":[],"content":[{"type":"reasoning_text","text":"hello"}]}}`,
|
||||
bufferedHostedItemsCompleted)},
|
||||
{"no item_id, after added", "text", bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.added","output_index":1,"item":{"type":"message","role":"assistant","content":[]}}`,
|
||||
`{"type":"response.output_text.delta","content_index":0,"delta":"hello"}`,
|
||||
`{"type":"response.output_item.done","output_index":1,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hello"}]}}`,
|
||||
bufferedHostedItemsCompleted)},
|
||||
}
|
||||
for _, entry := range bufferedPartsEntries {
|
||||
for _, tc := range cases {
|
||||
t.Run(entry.name+" "+tc.name, func(t *testing.T) {
|
||||
out := forwardBufferedHostedItems(t, entry.path, entry.body, tc.sse)
|
||||
require.Equal(t, "hello", bufferedPartsVisible(out, entry.path, tc.shown), out)
|
||||
})
|
||||
}
|
||||
}
|
||||
t.Run("custom tool call input", func(t *testing.T) {
|
||||
sse := bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.added","output_index":1,"item":{"type":"reasoning","summary":[]}}`,
|
||||
`{"type":"response.reasoning_summary_text.delta","summary_index":0,"delta":"plan"}`,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"custom_tool_call","call_id":"call_1","name":"exec","input":""}}`,
|
||||
`{"type":"response.custom_tool_call_input.delta","delta":"dir"}`,
|
||||
`{"type":"response.custom_tool_call_input.done"}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"custom_tool_call","call_id":"call_1","name":"exec","input":""}}`,
|
||||
bufferedHostedItemsCompleted)
|
||||
out := forwardBufferedHostedItems(t, "/v1/chat/completions", customBody, sse)
|
||||
require.Equal(t, "dir", gjson.Get(out, "choices.0.message.tool_calls.0.function.arguments").String(), out)
|
||||
require.Equal(t, "plan", gjson.Get(out, "choices.0.message.reasoning_content").String(), out)
|
||||
})
|
||||
}
|
||||
|
||||
// Items that share an output_index, or have none, and lack an added or a done
|
||||
// event keep their own deltas.
|
||||
func TestBufferedCompat_ItemsOfOneOutputIndexKeepTheirOwnEvents(t *testing.T) {
|
||||
fnBody := `{"model":"gpt-5.5","stream":false,"tools":[{"type":"function","function":{"name":"get","parameters":{"type":"object"}}}],"messages":[{"role":"user","content":"hi"}]}`
|
||||
t.Run("reasoning deltas, then a message added at the same index", func(t *testing.T) {
|
||||
sse := bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.reasoning_summary_text.delta","output_index":0,"summary_index":0,"delta":"plan"}`,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","content":[]}}`,
|
||||
`{"type":"response.output_text.delta","output_index":0,"content_index":0,"item_id":"msg_1","delta":"answer"}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","content":[{"type":"output_text","text":"answer"}]}}`,
|
||||
bufferedHostedItemsCompleted)
|
||||
for _, entry := range bufferedPartsEntries {
|
||||
out := forwardBufferedHostedItems(t, entry.path, entry.body, sse)
|
||||
require.Equal(t, "plan", bufferedPartsVisible(out, entry.path, "thinking"), out)
|
||||
require.Equal(t, "answer", bufferedPartsVisible(out, entry.path, "text"), out)
|
||||
}
|
||||
})
|
||||
t.Run("a message done, then another message added at the same index", func(t *testing.T) {
|
||||
sse := bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"first "}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":""}]}}`,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"message","role":"assistant","content":[]}}`,
|
||||
`{"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"second"}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"second"}]}}`,
|
||||
bufferedHostedItemsCompleted)
|
||||
for _, entry := range bufferedPartsEntries {
|
||||
out := forwardBufferedHostedItems(t, entry.path, entry.body, sse)
|
||||
require.Equal(t, "first second", bufferedPartsVisible(out, entry.path, "text"), out)
|
||||
}
|
||||
})
|
||||
t.Run("a hosted call added at the index of delta-only text", func(t *testing.T) {
|
||||
sse := bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.refusal.delta","output_index":0,"content_index":0,"delta":"no"}`,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"web_search_call","status":"in_progress"}}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"web_search_call","status":"completed"}}`,
|
||||
bufferedHostedItemsCompleted)
|
||||
out := forwardBufferedHostedItems(t, "/v1/chat/completions", bufferedHostedItemsChatBody, sse)
|
||||
require.Equal(t, "no", bufferedPartsVisible(out, "/v1/chat/completions", "refusal"), out)
|
||||
})
|
||||
t.Run("two function calls without index and id", func(t *testing.T) {
|
||||
sse := bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.added","item":{"type":"function_call","call_id":"call_a","name":"get","arguments":""}}`,
|
||||
`{"type":"response.function_call_arguments.delta","delta":"{\"a\":1}"}`,
|
||||
`{"type":"response.output_item.added","item":{"type":"function_call","call_id":"call_b","name":"get","arguments":""}}`,
|
||||
`{"type":"response.function_call_arguments.delta","delta":"{\"b\":2}"}`,
|
||||
`{"type":"response.output_item.done","item":{"type":"function_call","call_id":"call_b","name":"get","arguments":""}}`,
|
||||
bufferedHostedItemsCompleted)
|
||||
out := forwardBufferedHostedItems(t, "/v1/chat/completions", fnBody, sse)
|
||||
calls := gjson.Get(out, "choices.0.message.tool_calls").Array()
|
||||
require.Len(t, calls, 2, out)
|
||||
require.Equal(t, "call_a", calls[0].Get("id").String())
|
||||
require.Equal(t, `{"a":1}`, calls[0].Get("function.arguments").String())
|
||||
require.Equal(t, "call_b", calls[1].Get("id").String())
|
||||
require.Equal(t, `{"b":2}`, calls[1].Get("function.arguments").String())
|
||||
})
|
||||
}
|
||||
|
||||
// The signature of a reasoning item may be on its added event only; the item is
|
||||
// kept for it even without text, as the streaming path keeps it.
|
||||
func TestBufferedCompat_ReasoningSignatureOnAddedEventIsKept(t *testing.T) {
|
||||
cases := []struct {
|
||||
name, sse string
|
||||
want []string
|
||||
}{
|
||||
{"with text deltas and a done item without it", bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1","summary":[],"encrypted_content":"ENC"}}`,
|
||||
`{"type":"response.reasoning_summary_text.delta","output_index":0,"summary_index":0,"delta":"plan"}`,
|
||||
`{"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"plan"}]}}`,
|
||||
bufferedHostedItemsCompleted), []string{`thinking "plan" ENC`}},
|
||||
{"added event only", bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1","summary":[],"encrypted_content":"ENC"}}`,
|
||||
bufferedHostedItemsCompleted), []string{`thinking "" ENC`}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
out := forwardBufferedHostedItems(t, "/v1/messages", bufferedHostedItemsThinkingBody, tc.sse)
|
||||
var got []string
|
||||
for _, block := range gjson.Get(out, "content").Array() {
|
||||
got = append(got, block.Get("type").String()+" "+strconv.Quote(block.Get("thinking").String())+" "+block.Get("signature").String())
|
||||
}
|
||||
require.Equal(t, tc.want, got, out)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Reasoning deltas that came with no output_index before the item was added belong
|
||||
// to that item, so the text keeps its arrival order.
|
||||
func TestBufferedCompat_UnindexedReasoningDeltasJoinTheItemAddedLater(t *testing.T) {
|
||||
sse := bufferedItemsSSE(bufferedHostedItemsCreated,
|
||||
`{"type":"response.reasoning_text.delta","content_index":0,"delta":"first "}`,
|
||||
`{"type":"response.output_item.added","output_index":1,"item":{"type":"reasoning","summary":[]}}`,
|
||||
`{"type":"response.reasoning_summary_text.delta","summary_index":0,"delta":"second"}`,
|
||||
bufferedHostedItemsCompleted)
|
||||
for _, entry := range bufferedPartsEntries {
|
||||
out := forwardBufferedHostedItems(t, entry.path, entry.body, sse)
|
||||
require.Equal(t, "first second", bufferedPartsVisible(out, entry.path, "thinking"), out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user