Files
opencode2api/convert_response.go
T
Jason Xu ea29a62eca fix: harden gateway routing and protocol fidelity
Preserve request parameters, tool-call completion reasons and retry session
affinity. Bound upstream header waits, stop expired-context scans and add
opt-in redacted request-body diagnostics.

Isolate Playground diagnostics from production pools, report stream failures
accurately, align readiness with discoverable models and honor Docker listen
configuration.

Split Go and WebUI modules by responsibility, add regression tests and CI,
standardize formatting and rewrite the English and Chinese READMEs.

Refs: #6, #18, #19, #20, #21, #22, #23, #24, #25
2026-09-15 00:43:35 +08:00

399 lines
12 KiB
Go

package main
import (
"encoding/json"
"fmt"
"time"
)
func convertResponse(from, to Protocol, body []byte) ([]byte, error) {
if from == to {
return append([]byte(nil), body...), nil
}
var input map[string]any
if err := json.Unmarshal(body, &input); err != nil {
return nil, err
}
response, err := decodeBridgeResponse(from, input)
if err != nil {
return nil, err
}
if response.Error != "" || response.Stop == "error" {
return nil, fmt.Errorf("upstream response failed: %s", firstString(response.Error, "upstream response failed"))
}
return json.Marshal(encodeBridgeResponse(to, response))
}
func decodeBridgeResponse(protocol Protocol, input map[string]any) (bridgeResponse, error) {
response := bridgeResponse{
ID: stringAt(input, "id"),
Model: stringAt(input, "model"),
Created: int64At(input, "created"),
}
if response.Created == 0 {
response.Created = int64At(input, "created_at")
}
if response.Created == 0 {
response.Created = time.Now().Unix()
}
switch protocol {
case ProtocolChat:
if message := stringAt(input, "error", "message"); message != "" {
response.Error = message
}
if response.Error != "" {
return response, nil
}
choices := sliceAt(input, "choices")
if len(choices) == 0 {
return response, fmt.Errorf("chat response contains no choices")
}
choice, _ := choices[0].(map[string]any)
message := mapAt(choice, "message")
response.Reasoning = decodeChatReasoning(message)
blocks, err := decodeOpenAIBlocksChecked(message["content"])
if err != nil {
return response, fmt.Errorf("chat response content: %w", err)
}
response.Text, err = responseTextBlocks(blocks, "Chat")
if err != nil {
return response, err
}
for _, raw := range sliceAt(message, "tool_calls") {
call, _ := raw.(map[string]any)
function := mapAt(call, "function")
response.Tools = append(response.Tools, bridgeBlock{
Kind: "tool_call",
ID: stringAt(call, "id"),
Name: stringAt(function, "name"),
ArgumentsJSON: stringAt(function, "arguments"),
})
}
response.Stop = canonicalChatStop(stringAt(choice, "finish_reason"))
response.Usage = decodeOpenAIUsage(mapAt(input, "usage"))
case ProtocolResponses:
for _, raw := range sliceAt(input, "output") {
item, _ := raw.(map[string]any)
switch stringAt(item, "type") {
case "reasoning":
response.Reasoning = append(response.Reasoning, decodeResponsesReasoning(item)...)
case "message":
blocks, err := decodeOpenAIBlocksChecked(item["content"])
if err != nil {
return response, fmt.Errorf("Responses response content: %w", err)
}
text, err := responseTextBlocks(blocks, "Responses")
if err != nil {
return response, err
}
response.Text += text
case "function_call":
response.Tools = append(response.Tools, bridgeBlock{
Kind: "tool_call",
ID: firstString(stringAt(item, "call_id"), stringAt(item, "id")),
Name: stringAt(item, "name"),
ArgumentsJSON: stringAt(item, "arguments"),
})
default:
return response, fmt.Errorf("Responses response has unsupported output item type %q", stringAt(item, "type"))
}
}
response.Stop = "stop"
if len(response.Tools) > 0 {
response.Stop = "tool_calls"
}
if stringAt(input, "status") == "incomplete" {
response.Stop = canonicalResponsesIncomplete(stringAt(input, "incomplete_details", "reason"))
} else if stringAt(input, "status") == "failed" {
response.Stop = "error"
response.Error = firstString(stringAt(input, "error", "message"), "upstream Responses request failed")
}
response.Usage = decodeOpenAIUsage(mapAt(input, "usage"))
case ProtocolAnthropic:
if stringAt(input, "type") == "error" {
response.Error = firstString(stringAt(input, "error", "message"), "upstream Anthropic request failed")
}
blocks, err := decodeAnthropicBlocksChecked(input["content"])
if err != nil {
return response, fmt.Errorf("Anthropic response content: %w", err)
}
for _, block := range blocks {
if block.Kind == "reasoning" {
response.Reasoning = append(response.Reasoning, block)
} else if block.Kind == "text" {
response.Text += block.Text
} else if block.Kind == "tool_call" {
response.Tools = append(response.Tools, block)
} else if block.Kind != "text" {
return response, fmt.Errorf("Anthropic response contains unsupported %s content block", block.Kind)
}
}
response.Stop = canonicalAnthropicStop(stringAt(input, "stop_reason"))
response.Usage = decodeAnthropicUsage(mapAt(input, "usage"))
default:
return response, fmt.Errorf("unsupported response protocol %q", protocol)
}
return response, nil
}
func encodeBridgeResponse(protocol Protocol, response bridgeResponse) map[string]any {
if response.ID == "" {
response.ID = randomID("resp", 12)
}
switch protocol {
case ProtocolChat:
message := map[string]any{"role": "assistant", "content": response.Text}
if value, ok := bridgeReasoningText(response.Reasoning); ok {
message["reasoning_content"] = value
}
if signature := bridgeReasoningSignature(response.Reasoning); signature != "" {
message["reasoning_signature"] = signature
}
if len(response.Tools) > 0 {
calls := make([]any, 0, len(response.Tools))
for _, tool := range response.Tools {
calls = append(calls, map[string]any{
"id": tool.ID,
"type": "function",
"function": map[string]any{
"name": tool.Name,
"arguments": bridgeArgumentsJSON(tool),
},
})
}
message["tool_calls"] = calls
}
return map[string]any{
"id": asPrefix(response.ID, "chatcmpl"),
"object": "chat.completion",
"created": response.Created,
"model": response.Model,
"choices": []any{map[string]any{
"index": 0,
"message": message,
"finish_reason": chatStop(response.Stop),
}},
"usage": openAIUsage(response.Usage),
}
case ProtocolResponses:
output := make([]any, 0, len(response.Reasoning)+len(response.Tools)+1)
for _, reasoning := range response.Reasoning {
output = append(output, encodeResponsesReasoning(reasoning, true))
}
if response.Text != "" {
output = append(output, map[string]any{
"id": randomID("msg", 12),
"type": "message",
"status": "completed",
"role": "assistant",
"content": []any{map[string]any{
"type": "output_text",
"text": response.Text,
"annotations": []any{},
}},
})
}
for _, tool := range response.Tools {
output = append(output, map[string]any{
"id": randomID("fc", 12),
"type": "function_call",
"status": "completed",
"call_id": tool.ID,
"name": tool.Name,
"arguments": bridgeArgumentsJSON(tool),
})
}
status := "completed"
var incomplete any
if response.Stop == "length" || response.Stop == "content_filter" {
status = "incomplete"
incomplete = map[string]any{"reason": responsesIncompleteReason(response.Stop)}
} else if response.Stop == "error" {
status = "failed"
}
return map[string]any{
"id": asPrefix(response.ID, "resp"),
"object": "response",
"created_at": response.Created,
"status": status,
"model": response.Model,
"output": output,
"error": nil,
"incomplete_details": incomplete,
"usage": map[string]any{
"input_tokens": response.Usage.Input,
"output_tokens": response.Usage.Output,
"total_tokens": response.Usage.Total,
"input_tokens_details": map[string]any{
"cached_tokens": response.Usage.Cached,
},
"output_tokens_details": map[string]any{
"reasoning_tokens": response.Usage.Reasoning,
},
},
}
case ProtocolAnthropic:
content := make([]any, 0, len(response.Reasoning)+len(response.Tools)+1)
content = append(content, encodeAnthropicBlocks(response.Reasoning)...)
if response.Text != "" {
content = append(content, map[string]any{"type": "text", "text": response.Text})
}
for _, tool := range response.Tools {
input := tool.Arguments
if input == nil {
input = parseJSONOrString(tool.ArgumentsJSON)
}
content = append(content, map[string]any{"type": "tool_use", "id": tool.ID, "name": tool.Name, "input": input})
}
return map[string]any{
"id": asPrefix(response.ID, "msg"),
"type": "message",
"role": "assistant",
"model": response.Model,
"content": content,
"stop_reason": anthropicStop(response.Stop),
"stop_sequence": nil,
"usage": anthropicUsage(response.Usage),
}
default:
return map[string]any{}
}
}
func decodeOpenAIUsage(usage map[string]any) bridgeUsage {
input := firstNonZero(intAt(usage, "prompt_tokens"), intAt(usage, "input_tokens"))
output := firstNonZero(intAt(usage, "completion_tokens"), intAt(usage, "output_tokens"))
total := intAt(usage, "total_tokens")
if total == 0 {
total = input + output
}
cached := firstNonZero(intAt(usage, "prompt_tokens_details", "cached_tokens"), intAt(usage, "input_tokens_details", "cached_tokens"))
cacheCreation := firstNonZero(
intAt(usage, "cache_creation_input_tokens"),
intAt(usage, "prompt_tokens_details", "cache_creation_input_tokens"),
intAt(usage, "prompt_tokens_details", "cache_write_tokens"),
intAt(usage, "input_tokens_details", "cache_creation_input_tokens"),
)
reasoning := firstNonZero(intAt(usage, "completion_tokens_details", "reasoning_tokens"), intAt(usage, "output_tokens_details", "reasoning_tokens"))
return bridgeUsage{Input: input, Output: output, Total: total, Cached: cached, CacheCreation: cacheCreation, Reasoning: reasoning}
}
func decodeAnthropicUsage(usage map[string]any) bridgeUsage {
input := intAt(usage, "input_tokens")
cached := intAt(usage, "cache_read_input_tokens")
cacheCreation := intAt(usage, "cache_creation_input_tokens")
output := intAt(usage, "output_tokens")
input += cached + cacheCreation
return bridgeUsage{
Input: input,
Output: output,
Total: input + output,
Cached: cached,
CacheCreation: cacheCreation,
}
}
func openAIUsage(usage bridgeUsage) map[string]any {
return map[string]any{
"prompt_tokens": usage.Input,
"completion_tokens": usage.Output,
"total_tokens": usage.Total,
"prompt_tokens_details": map[string]any{
"cached_tokens": usage.Cached,
},
"completion_tokens_details": map[string]any{
"reasoning_tokens": usage.Reasoning,
},
}
}
func anthropicUsage(usage bridgeUsage) map[string]any {
return map[string]any{
"input_tokens": max(usage.Input-usage.Cached-usage.CacheCreation, 0),
"output_tokens": usage.Output,
"cache_creation_input_tokens": usage.CacheCreation,
"cache_read_input_tokens": usage.Cached,
}
}
func chatStop(stop string) string {
switch stop {
case "tool_use", "tool_calls":
return "tool_calls"
case "max_tokens", "length":
return "length"
case "content_filter", "error":
return stop
default:
return "stop"
}
}
func anthropicStop(stop string) string {
switch stop {
case "tool_use", "tool_calls":
return "tool_use"
case "max_tokens", "length":
return "max_tokens"
case "stop_sequence", "pause_turn", "refusal":
return stop
default:
return "end_turn"
}
}
func canonicalChatStop(stop string) string {
switch stop {
case "tool_calls", "function_call":
return "tool_calls"
case "length":
return "length"
case "content_filter":
return "content_filter"
case "error", "network_error", "server_error":
return "error"
default:
return "stop"
}
}
func canonicalAnthropicStop(stop string) string {
switch stop {
case "tool_use":
return "tool_calls"
case "max_tokens":
return "length"
case "stop_sequence", "pause_turn", "refusal":
return stop
case "error", "network_error", "server_error":
return "error"
default:
return "stop"
}
}
func canonicalResponsesIncomplete(reason string) string {
switch reason {
case "content_filter":
return "content_filter"
default:
return "length"
}
}
func responsesIncompleteReason(stop string) string {
if stop == "content_filter" {
return "content_filter"
}
return "max_output_tokens"
}
func responseTextBlocks(blocks []bridgeBlock, protocol string) (string, error) {
for _, block := range blocks {
if block.Kind != "text" {
return "", fmt.Errorf("%s response contains unsupported %s content block", protocol, block.Kind)
}
}
return bridgeBlocksText(blocks), nil
}