Fix Responses WebSocket tool output context repair
Unified · Split
| @@ -662,6 +662,34 @@ func TestRepairResponsesWebsocketToolCallsInsertsCachedCallForOrphanOutput(t *te · 661 unmodified lines | |||
| 662 | } | 662 | } |
| 663 | } | 663 | } |
| 664 | 664 | ||
| 665 | func TestRepairResponsesWebsocketToolCallsInsertsCachedCallForPreviousResponseOutput(t *testing.T) { | ||
| 666 | outputCache := newWebsocketToolOutputCache(time.Minute, 10) | ||
| 667 | callCache := newWebsocketToolOutputCache(time.Minute, 10) | ||
| 668 | sessionKey := "session-1" | ||
| 669 | |||
| 670 | callCache.record(sessionKey, "call-1", []byte(`{"type":"function_call","id":"fc-1","call_id":"call-1","name":"tool"}`)) | ||
| 671 | |||
| 672 | raw := []byte(`{"previous_response_id":"resp-latest","input":[{"type":"function_call_output","call_id":"call-1","id":"tool-out-1","output":"ok"},{"type":"message","id":"msg-1"}]}`) | ||
| 673 | repaired := repairResponsesWebsocketToolCallsWithCaches(outputCache, callCache, sessionKey, raw) | ||
| 674 | |||
| 675 | if got := gjson.GetBytes(repaired, "previous_response_id").String(); got != "resp-latest" { | ||
| 676 | t.Fatalf("previous_response_id = %q, want resp-latest", got) | ||
| 677 | } | ||
| 678 | input := gjson.GetBytes(repaired, "input").Array() | ||
| 679 | if len(input) != 3 { | ||
| 680 | t.Fatalf("repaired input len = %d, want 3: %s", len(input), repaired) | ||
| 681 | } | ||
| 682 | if input[0].Get("type").String() != "function_call" || input[0].Get("call_id").String() != "call-1" { | ||
| 683 | t.Fatalf("missing inserted call: %s", input[0].Raw) | ||
| 684 | } | ||
| 685 | if input[1].Get("type").String() != "function_call_output" || input[1].Get("call_id").String() != "call-1" { | ||
| 686 | t.Fatalf("unexpected output item: %s", input[1].Raw) | ||
| 687 | } | ||
| 688 | if input[2].Get("type").String() != "message" || input[2].Get("id").String() != "msg-1" { | ||
| 689 | t.Fatalf("unexpected trailing item: %s", input[2].Raw) | ||
| 690 | } | ||
| 691 | } | ||
| 692 | |||
| 665 | func TestRepairResponsesWebsocketToolCallsDropsOrphanOutputWhenCallMissing(t *testing.T) { | 693 | func TestRepairResponsesWebsocketToolCallsDropsOrphanOutputWhenCallMissing(t *testing.T) { |
| 666 | outputCache := newWebsocketToolOutputCache(time.Minute, 10) | 694 | outputCache := newWebsocketToolOutputCache(time.Minute, 10) |
| 667 | callCache := newWebsocketToolOutputCache(time.Minute, 10) | 695 | callCache := newWebsocketToolOutputCache(time.Minute, 10) |
| @@ -300,11 +300,6 @@ func repairResponsesToolCallsArray(outputCache, callCache *websocketToolOutputCa · 299 unmodified lines | |||
| 300 | continue | 300 | continue |
| 301 | } | 301 | } |
| 302 | 302 | ||
| 303 | if allowOrphanOutputs { | ||
| 304 | filtered = append(filtered, item) | ||
| 305 | continue | ||
| 306 | } | ||
| 307 | |||
| 308 | if _, ok := callPresent[callID]; ok { | 303 | if _, ok := callPresent[callID]; ok { |
| 309 | filtered = append(filtered, item) | 304 | filtered = append(filtered, item) |
| 310 | continue | 305 | continue |
| @@ -322,6 +317,11 @@ func repairResponsesToolCallsArray(outputCache, callCache *websocketToolOutputCa · 11 unmodified lines | |||
| 322 | } | 317 | } |
| 323 | } | 318 | } |
| 324 | 319 | ||
| 320 | if allowOrphanOutputs { | ||
| 321 | filtered = append(filtered, item) | ||
| 322 | continue | ||
| 323 | } | ||
| 324 | |||
| 325 | // Drop orphaned function_call_output items; upstream rejects transcripts with missing calls. | 325 | // Drop orphaned function_call_output items; upstream rejects transcripts with missing calls. |
| 326 | continue | 326 | continue |
| 327 | } | 327 | } |