|
| 1 | +package extproc |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// mapResponsesRequestToChatCompletions converts a minimal OpenAI Responses API request |
| 10 | +// into a legacy Chat Completions request JSON. Supports only text input for PR1. |
| 11 | +func mapResponsesRequestToChatCompletions(original []byte) ([]byte, error) { |
| 12 | + var req map[string]interface{} |
| 13 | + if err := json.Unmarshal(original, &req); err != nil { |
| 14 | + return nil, err |
| 15 | + } |
| 16 | + |
| 17 | + // Extract model |
| 18 | + model, _ := req["model"].(string) |
| 19 | + if model == "" { |
| 20 | + return nil, fmt.Errorf("missing model") |
| 21 | + } |
| 22 | + |
| 23 | + // Derive user content |
| 24 | + var userContent string |
| 25 | + if input, ok := req["input"]; ok { |
| 26 | + switch v := input.(type) { |
| 27 | + case string: |
| 28 | + userContent = v |
| 29 | + case []interface{}: |
| 30 | + // Join any string elements; ignore non-string for now |
| 31 | + var parts []string |
| 32 | + for _, it := range v { |
| 33 | + if s, ok := it.(string); ok { |
| 34 | + parts = append(parts, s) |
| 35 | + } else if m, ok := it.(map[string]interface{}); ok { |
| 36 | + // Try common shapes: {type:"input_text"|"text", text:"..."} |
| 37 | + if t, _ := m["type"].(string); t == "input_text" || t == "text" { |
| 38 | + if txt, _ := m["text"].(string); txt != "" { |
| 39 | + parts = append(parts, txt) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + userContent = strings.TrimSpace(strings.Join(parts, " ")) |
| 45 | + default: |
| 46 | + // unsupported multimodal |
| 47 | + return nil, fmt.Errorf("unsupported input type") |
| 48 | + } |
| 49 | + } else if msgs, ok := req["messages"].([]interface{}); ok { |
| 50 | + // Fallback: if caller already provided messages, pass them through |
| 51 | + // This enables easy migration from chat/completions |
| 52 | + mapped := map[string]interface{}{ |
| 53 | + "model": model, |
| 54 | + "messages": msgs, |
| 55 | + } |
| 56 | + // Map basic params |
| 57 | + if v, ok := req["temperature"]; ok { |
| 58 | + mapped["temperature"] = v |
| 59 | + } |
| 60 | + if v, ok := req["top_p"]; ok { |
| 61 | + mapped["top_p"] = v |
| 62 | + } |
| 63 | + if v, ok := req["max_output_tokens"]; ok { |
| 64 | + mapped["max_tokens"] = v |
| 65 | + } |
| 66 | + return json.Marshal(mapped) |
| 67 | + } |
| 68 | + |
| 69 | + if userContent == "" { |
| 70 | + return nil, fmt.Errorf("empty input") |
| 71 | + } |
| 72 | + |
| 73 | + // Build minimal Chat Completions request |
| 74 | + mapped := map[string]interface{}{ |
| 75 | + "model": model, |
| 76 | + "messages": []map[string]interface{}{ |
| 77 | + {"role": "user", "content": userContent}, |
| 78 | + }, |
| 79 | + } |
| 80 | + // Map basic params |
| 81 | + if v, ok := req["temperature"]; ok { |
| 82 | + mapped["temperature"] = v |
| 83 | + } |
| 84 | + if v, ok := req["top_p"]; ok { |
| 85 | + mapped["top_p"] = v |
| 86 | + } |
| 87 | + if v, ok := req["max_output_tokens"]; ok { |
| 88 | + mapped["max_tokens"] = v |
| 89 | + } |
| 90 | + |
| 91 | + return json.Marshal(mapped) |
| 92 | +} |
| 93 | + |
| 94 | +// mapChatCompletionToResponses converts an OpenAI ChatCompletion JSON |
| 95 | +// into a minimal Responses API JSON (non-streaming only) for PR1. |
| 96 | +func mapChatCompletionToResponses(chatCompletionJSON []byte) ([]byte, error) { |
| 97 | + var parsed struct { |
| 98 | + ID string `json:"id"` |
| 99 | + Object string `json:"object"` |
| 100 | + Created int64 `json:"created"` |
| 101 | + Model string `json:"model"` |
| 102 | + Choices []struct { |
| 103 | + Index int `json:"index"` |
| 104 | + FinishReason string `json:"finish_reason"` |
| 105 | + Message struct { |
| 106 | + Role string `json:"role"` |
| 107 | + Content string `json:"content"` |
| 108 | + } `json:"message"` |
| 109 | + } `json:"choices"` |
| 110 | + Usage struct { |
| 111 | + PromptTokens int `json:"prompt_tokens"` |
| 112 | + CompletionTokens int `json:"completion_tokens"` |
| 113 | + TotalTokens int `json:"total_tokens"` |
| 114 | + } `json:"usage"` |
| 115 | + } |
| 116 | + if err := json.Unmarshal(chatCompletionJSON, &parsed); err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + content := "" |
| 121 | + stopReason := "stop" |
| 122 | + if len(parsed.Choices) > 0 { |
| 123 | + content = parsed.Choices[0].Message.Content |
| 124 | + if parsed.Choices[0].FinishReason != "" { |
| 125 | + stopReason = parsed.Choices[0].FinishReason |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + out := map[string]interface{}{ |
| 130 | + "id": parsed.ID, |
| 131 | + "object": "response", |
| 132 | + "created": parsed.Created, |
| 133 | + "model": parsed.Model, |
| 134 | + "output": []map[string]interface{}{ |
| 135 | + {"type": "message", "role": "assistant", "content": content}, |
| 136 | + }, |
| 137 | + "stop_reason": stopReason, |
| 138 | + "usage": map[string]int{ |
| 139 | + "input_tokens": parsed.Usage.PromptTokens, |
| 140 | + "output_tokens": parsed.Usage.CompletionTokens, |
| 141 | + "total_tokens": parsed.Usage.TotalTokens, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + return json.Marshal(out) |
| 146 | +} |
0 commit comments