mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Merge pull request #8429 from lkon2112/fix/responses-message-content
fix(responses): preserve message content fields in completed responses
This commit is contained in:
3 files changed
+70
-19
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package apicompat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func marshalMessageWire(t *testing.T, value any) gjson.Result {
|
||||
t.Helper()
|
||||
raw, err := json.Marshal(value)
|
||||
require.NoError(t, err)
|
||||
return gjson.ParseBytes(raw)
|
||||
}
|
||||
|
||||
func TestMessageContentWireConsistent(t *testing.T) {
|
||||
for _, text := range []string{"", "search result"} {
|
||||
t.Run(text, func(t *testing.T) {
|
||||
part := ResponsesContentPart{Type: "output_text", Text: text}
|
||||
item := ResponsesOutput{Type: "message", ID: "msg_1", Role: "assistant", Status: "completed", Content: []ResponsesContentPart{part}}
|
||||
response := ResponsesResponse{ID: "resp_1", Object: "response", Status: "completed", Output: []ResponsesOutput{item}}
|
||||
parts := []gjson.Result{
|
||||
marshalMessageWire(t, ResponsesStreamEvent{Type: "response.content_part.done", Part: &part}).Get("part"),
|
||||
marshalMessageWire(t, ResponsesStreamEvent{Type: "response.output_item.done", Item: &item}).Get("item.content.0"),
|
||||
marshalMessageWire(t, ResponsesStreamEvent{Type: "response.completed", Response: &response}).Get("response.output.0.content.0"),
|
||||
marshalMessageWire(t, response).Get("output.0.content.0"),
|
||||
}
|
||||
for _, encoded := range parts {
|
||||
require.Equal(t, "output_text", encoded.Get("type").String())
|
||||
require.True(t, encoded.Get("text").Exists())
|
||||
require.Equal(t, text, encoded.Get("text").String())
|
||||
require.Equal(t, "[]", encoded.Get("annotations").Raw)
|
||||
require.Equal(t, "[]", encoded.Get("logprobs").Raw)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageRefusalPreserved(t *testing.T) {
|
||||
item := ResponsesOutput{Type: "message", ID: "msg_1", Role: "assistant", Content: []ResponsesContentPart{{Type: "refusal", Refusal: "Cannot provide this."}}}
|
||||
for _, encoded := range []gjson.Result{
|
||||
marshalMessageWire(t, item),
|
||||
marshalMessageWire(t, ResponsesStreamEvent{Type: "response.output_item.done", Item: &item}).Get("item"),
|
||||
} {
|
||||
require.Equal(t, "refusal", encoded.Get("content.0.type").String())
|
||||
require.Equal(t, "Cannot provide this.", encoded.Get("content.0.refusal").String())
|
||||
require.False(t, encoded.Get("content.0.text").Exists())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyMessageContentArray(t *testing.T) {
|
||||
item := marshalMessageWire(t, ResponsesOutput{Type: "message", ID: "msg_1", Role: "assistant"})
|
||||
require.Equal(t, "[]", item.Get("content").Raw)
|
||||
}
|
||||
@@ -214,7 +214,14 @@ func messageContentWire(parts []ResponsesContentPart) []map[string]any {
|
||||
if typ == "" {
|
||||
typ = "output_text"
|
||||
}
|
||||
out = append(out, map[string]any{"type": typ, "text": p.Text})
|
||||
switch typ {
|
||||
case "output_text":
|
||||
out = append(out, outputTextPartWire(&p))
|
||||
case "refusal":
|
||||
out = append(out, map[string]any{"type": typ, "refusal": p.Refusal})
|
||||
default:
|
||||
out = append(out, map[string]any{"type": typ, "text": p.Text})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -425,26 +425,14 @@ type ResponsesOutput struct {
|
||||
Action *WebSearchAction `json:"action,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalJSON 处理 tool_search_call 项的线上形态(复用 CallID/Arguments 字段):
|
||||
// execution 固定为 "client"(codex 的必填字段,非 client 的调用会被静默忽略),
|
||||
// arguments 是 JSON 对象而非 function_call 语义下的字符串。其余类型走默认结构体
|
||||
// 序列化,输出逐字节不变。
|
||||
// MarshalJSON keeps message and tool_search_call items consistent with their
|
||||
// streamed representation. Other output types retain their default encoding.
|
||||
func (o ResponsesOutput) MarshalJSON() ([]byte, error) {
|
||||
if o.Type == "message" || o.Type == "tool_search_call" {
|
||||
return json.Marshal(responsesItemWire(&o))
|
||||
}
|
||||
type responsesOutputAlias ResponsesOutput
|
||||
if o.Type != "tool_search_call" {
|
||||
return json.Marshal(responsesOutputAlias(o))
|
||||
}
|
||||
m := map[string]any{
|
||||
"type": o.Type,
|
||||
"id": o.ID,
|
||||
"call_id": o.CallID,
|
||||
"execution": "client",
|
||||
"arguments": toolSearchCallArgumentsJSON(o.Arguments),
|
||||
}
|
||||
if o.Status != "" {
|
||||
m["status"] = o.Status
|
||||
}
|
||||
return json.Marshal(m)
|
||||
return json.Marshal(responsesOutputAlias(o))
|
||||
}
|
||||
|
||||
// UnmarshalJSON accepts both the Responses function-call string form and the
|
||||
|
||||
Reference in new issue
Block a user