fix provider

This commit is contained in:
lpf
2026-02-18 23:44:52 +08:00
parent 8e9d5d71ca
commit 9ebc951441
2 changed files with 31 additions and 7 deletions

View File

@@ -141,11 +141,13 @@ func (p *HTTPProvider) callResponses(ctx context.Context, messages []Message, to
func toResponsesInputItems(msg Message) []map[string]interface{} {
role := strings.ToLower(strings.TrimSpace(msg.Role))
switch role {
case "system", "developer", "assistant", "user":
return []map[string]interface{}{responsesMessageItem(role, msg.Content)}
case "system", "developer", "user":
return []map[string]interface{}{responsesMessageItem(role, msg.Content, "input_text")}
case "assistant":
return []map[string]interface{}{responsesMessageItem(role, msg.Content, "output_text")}
case "tool":
if strings.TrimSpace(msg.ToolCallID) == "" {
return []map[string]interface{}{responsesMessageItem("user", msg.Content)}
return []map[string]interface{}{responsesMessageItem("user", msg.Content, "input_text")}
}
return []map[string]interface{}{map[string]interface{}{
"type": "function_call_output",
@@ -153,17 +155,21 @@ func toResponsesInputItems(msg Message) []map[string]interface{} {
"output": msg.Content,
}}
default:
return []map[string]interface{}{responsesMessageItem("user", msg.Content)}
return []map[string]interface{}{responsesMessageItem("user", msg.Content, "input_text")}
}
}
func responsesMessageItem(role, text string) map[string]interface{} {
func responsesMessageItem(role, text, contentType string) map[string]interface{} {
ct := strings.TrimSpace(contentType)
if ct == "" {
ct = "input_text"
}
return map[string]interface{}{
"type": "message",
"role": role,
"content": []map[string]interface{}{
{
"type": "input_text",
"type": ct,
"text": text,
},
},
@@ -523,7 +529,7 @@ func (p *HTTPProvider) BuildSummaryViaResponsesCompact(ctx context.Context, mode
}
input := make([]map[string]interface{}, 0, len(messages)+1)
if strings.TrimSpace(existingSummary) != "" {
input = append(input, responsesMessageItem("system", "Existing summary:\n"+strings.TrimSpace(existingSummary)))
input = append(input, responsesMessageItem("system", "Existing summary:\n"+strings.TrimSpace(existingSummary), "input_text"))
}
for _, msg := range messages {
input = append(input, toResponsesInputItems(msg)...)

View File

@@ -117,6 +117,24 @@ func TestEndpointForResponsesCompact(t *testing.T) {
}
}
func TestToResponsesInputItems_AssistantUsesOutputText(t *testing.T) {
items := toResponsesInputItems(Message{
Role: "assistant",
Content: "hello",
})
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
content, ok := items[0]["content"].([]map[string]interface{})
if !ok || len(content) == 0 {
t.Fatalf("unexpected content shape: %#v", items[0]["content"])
}
gotType, _ := content[0]["type"].(string)
if gotType != "output_text" {
t.Fatalf("assistant content type = %q, want output_text", gotType)
}
}
func containsFunctionCallMarkup(s string) bool {
return len(s) > 0 && (strings.Contains(s, "<function_call>") || strings.Contains(s, "</function_call>"))
}