|
| 1 | +package protocol |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +var claudeKnownRequestKeys = map[string]struct{}{ |
| 9 | + "model": {}, |
| 10 | + "messages": {}, |
| 11 | + "stream": {}, |
| 12 | + "max_tokens": {}, |
| 13 | + "temperature": {}, |
| 14 | + "system": {}, |
| 15 | + "tools": {}, |
| 16 | +} |
| 17 | + |
| 18 | +func appendClaudeRequestFieldExtensions(req *Request, raw map[string]any) error { |
| 19 | + if req == nil || raw == nil { |
| 20 | + return nil |
| 21 | + } |
| 22 | + for key, value := range raw { |
| 23 | + if !claudeRequestFieldNeedsPreservation(key, value) { |
| 24 | + continue |
| 25 | + } |
| 26 | + ext, err := requestFieldExtensionForKind(ExtAnthropicRequestField, key, value) |
| 27 | + if err != nil { |
| 28 | + return fmt.Errorf("preserve request field %q: %w", key, err) |
| 29 | + } |
| 30 | + req.Extensions = append(req.Extensions, ext) |
| 31 | + } |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +func claudeRequestFieldNeedsPreservation(key string, value any) bool { |
| 36 | + if _, known := claudeKnownRequestKeys[key]; !known { |
| 37 | + return true |
| 38 | + } |
| 39 | + switch key { |
| 40 | + case "system": |
| 41 | + return !isSimpleClaudeSystemWire(value) |
| 42 | + case "messages": |
| 43 | + return !isSimpleClaudeMessagesWire(value) |
| 44 | + case "tools": |
| 45 | + return !isSimpleClaudeToolsWire(value) |
| 46 | + default: |
| 47 | + return false |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func isSimpleClaudeSystemWire(value any) bool { |
| 52 | + if value == nil { |
| 53 | + return true |
| 54 | + } |
| 55 | + switch v := value.(type) { |
| 56 | + case string: |
| 57 | + return true |
| 58 | + case []any: |
| 59 | + for _, item := range v { |
| 60 | + block, ok := item.(map[string]any) |
| 61 | + if !ok || !isSimpleClaudeTextBlock(block) { |
| 62 | + return false |
| 63 | + } |
| 64 | + } |
| 65 | + return len(v) > 0 |
| 66 | + default: |
| 67 | + return false |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func isSimpleClaudeMessagesWire(value any) bool { |
| 72 | + arr, ok := value.([]any) |
| 73 | + if !ok { |
| 74 | + return value == nil |
| 75 | + } |
| 76 | + for _, item := range arr { |
| 77 | + msg, ok := item.(map[string]any) |
| 78 | + if !ok { |
| 79 | + return false |
| 80 | + } |
| 81 | + role := strings.ToLower(strings.TrimSpace(fmt.Sprint(msg["role"]))) |
| 82 | + if role != string(RoleUser) && role != string(RoleAssistant) { |
| 83 | + return false |
| 84 | + } |
| 85 | + if !isSimpleClaudeMessageContent(msg["content"]) { |
| 86 | + return false |
| 87 | + } |
| 88 | + for field := range msg { |
| 89 | + if field != "role" && field != "content" { |
| 90 | + return false |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return true |
| 95 | +} |
| 96 | + |
| 97 | +func isSimpleClaudeMessageContent(content any) bool { |
| 98 | + if content == nil { |
| 99 | + return true |
| 100 | + } |
| 101 | + switch v := content.(type) { |
| 102 | + case string: |
| 103 | + return true |
| 104 | + case []any: |
| 105 | + for _, item := range v { |
| 106 | + block, ok := item.(map[string]any) |
| 107 | + if !ok || !isSimpleClaudeTextBlock(block) { |
| 108 | + return false |
| 109 | + } |
| 110 | + } |
| 111 | + return true |
| 112 | + default: |
| 113 | + return false |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func isSimpleClaudeTextBlock(block map[string]any) bool { |
| 118 | + if block == nil { |
| 119 | + return false |
| 120 | + } |
| 121 | + if strings.TrimSpace(fmt.Sprint(block["type"])) != "text" { |
| 122 | + return false |
| 123 | + } |
| 124 | + if _, ok := block["text"]; !ok { |
| 125 | + return false |
| 126 | + } |
| 127 | + for field := range block { |
| 128 | + if field != "type" && field != "text" { |
| 129 | + return false |
| 130 | + } |
| 131 | + } |
| 132 | + return true |
| 133 | +} |
| 134 | + |
| 135 | +func isSimpleClaudeToolsWire(value any) bool { |
| 136 | + arr, ok := value.([]any) |
| 137 | + if !ok { |
| 138 | + return value == nil |
| 139 | + } |
| 140 | + allowedToolKeys := map[string]struct{}{ |
| 141 | + "name": {}, "description": {}, "input_schema": {}, "parameters": {}, "type": {}, "function": {}, |
| 142 | + } |
| 143 | + for _, item := range arr { |
| 144 | + entry, ok := item.(map[string]any) |
| 145 | + if !ok { |
| 146 | + return false |
| 147 | + } |
| 148 | + if fn, ok := entry["function"].(map[string]any); ok && fn != nil { |
| 149 | + entry = fn |
| 150 | + } |
| 151 | + for field := range entry { |
| 152 | + if _, ok := allowedToolKeys[field]; !ok { |
| 153 | + return false |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + return true |
| 158 | +} |
| 159 | + |
| 160 | +func decodeClaudeInputSlots(messages []any) []InputSlot { |
| 161 | + slots := make([]InputSlot, 0) |
| 162 | + for _, raw := range messages { |
| 163 | + msg, ok := raw.(map[string]any) |
| 164 | + if !ok { |
| 165 | + continue |
| 166 | + } |
| 167 | + role := strings.ToLower(strings.TrimSpace(fmt.Sprint(msg["role"]))) |
| 168 | + if role == "" { |
| 169 | + role = string(RoleUser) |
| 170 | + } |
| 171 | + content := msg["content"] |
| 172 | + blocks, ok := content.([]any) |
| 173 | + if !ok { |
| 174 | + text := strings.TrimSpace(TextContent(content)) |
| 175 | + if text != "" { |
| 176 | + m := Message{Role: Role(role), Content: text} |
| 177 | + slots = append(slots, InputSlot{Message: &m}) |
| 178 | + } |
| 179 | + continue |
| 180 | + } |
| 181 | + var textParts []string |
| 182 | + flushText := func() { |
| 183 | + text := strings.TrimSpace(strings.Join(filterNonEmpty(textParts), "\n")) |
| 184 | + textParts = nil |
| 185 | + if text == "" { |
| 186 | + return |
| 187 | + } |
| 188 | + m := Message{Role: Role(role), Content: text} |
| 189 | + slots = append(slots, InputSlot{Message: &m}) |
| 190 | + } |
| 191 | + for _, rawBlock := range blocks { |
| 192 | + block, ok := rawBlock.(map[string]any) |
| 193 | + if !ok { |
| 194 | + continue |
| 195 | + } |
| 196 | + switch strings.TrimSpace(fmt.Sprint(block["type"])) { |
| 197 | + case "text": |
| 198 | + if text := strings.TrimSpace(TextContent(block["text"])); text != "" { |
| 199 | + textParts = append(textParts, text) |
| 200 | + } |
| 201 | + case "tool_use": |
| 202 | + flushText() |
| 203 | + if tc := toolCallFromClaudeBlock(block); tc != nil { |
| 204 | + slots = append(slots, InputSlot{ToolCall: tc}) |
| 205 | + } |
| 206 | + case "tool_result": |
| 207 | + flushText() |
| 208 | + if tr := toolResultFromClaudeBlock(block); tr != nil { |
| 209 | + slots = append(slots, InputSlot{ToolResult: tr}) |
| 210 | + } |
| 211 | + default: |
| 212 | + if text := strings.TrimSpace(TextContent(block)); text != "" { |
| 213 | + textParts = append(textParts, text) |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + flushText() |
| 218 | + } |
| 219 | + return slots |
| 220 | +} |
0 commit comments