| 1 | package proxy |
| 2 | |
| 3 | import "encoding/json" |
| 4 | |
| 5 | // ---------- Anthropic API types ---------- |
| 6 | |
| 7 | type anthropicRequest struct { |
| 8 | Model string `json:"model"` |
| 9 | MaxTokens int `json:"max_tokens"` |
| 10 | Messages []anthropicMessage `json:"messages"` |
| 11 | System json.RawMessage `json:"system,omitempty"` |
| 12 | Stream bool `json:"stream,omitempty"` |
| 13 | Temperature *float64 `json:"temperature,omitempty"` |
| 14 | TopP *float64 `json:"top_p,omitempty"` |
| 15 | StopSequences []string `json:"stop_sequences,omitempty"` |
| 16 | Tools []anthropicTool `json:"tools,omitempty"` |
| 17 | ToolChoice json.RawMessage `json:"tool_choice,omitempty"` |
| 18 | Thinking json.RawMessage `json:"thinking,omitempty"` |
| 19 | } |
| 20 | |
| 21 | type anthropicMessage struct { |
| 22 | Role string `json:"role"` |
| 23 | Content json.RawMessage `json:"content"` // string | []contentBlock |
| 24 | } |
| 25 | |
| 26 | type contentBlock struct { |
| 27 | Type string `json:"type"` |
| 28 | Text string `json:"text,omitempty"` |
| 29 | ID string `json:"id,omitempty"` |
| 30 | Name string `json:"name,omitempty"` |
| 31 | Input json.RawMessage `json:"input,omitempty"` |
| 32 | ToolUseID string `json:"tool_use_id,omitempty"` |
| 33 | Content json.RawMessage `json:"content,omitempty"` // tool_result content |
| 34 | IsError bool `json:"is_error,omitempty"` |
| 35 | } |
| 36 | |
| 37 | type anthropicTool struct { |
| 38 | Name string `json:"name"` |
| 39 | Description string `json:"description,omitempty"` |
| 40 | InputSchema json.RawMessage `json:"input_schema"` |
| 41 | } |
| 42 | |
| 43 | type anthropicToolChoice struct { |
| 44 | Type string `json:"type"` |
| 45 | Name string `json:"name,omitempty"` |
| 46 | } |
| 47 | |
| 48 | type anthropicResponse struct { |
| 49 | ID string `json:"id"` |
| 50 | Type string `json:"type"` |
| 51 | Role string `json:"role"` |
| 52 | Content []contentBlock `json:"content"` |
| 53 | Model string `json:"model"` |
| 54 | StopReason *string `json:"stop_reason"` |
| 55 | StopSequence *string `json:"stop_sequence"` |
| 56 | Usage anthropicUsage `json:"usage"` |
| 57 | } |
| 58 | |
| 59 | type anthropicUsage struct { |
| 60 | InputTokens int `json:"input_tokens"` |
| 61 | OutputTokens int `json:"output_tokens"` |
| 62 | CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"` |
| 63 | CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"` |
| 64 | } |
| 65 | |
| 66 | // ---------- OpenAI API types ---------- |
| 67 | |
| 68 | type openaiRequest struct { |
| 69 | Model string `json:"model"` |
| 70 | Messages []openaiMessage `json:"messages"` |
| 71 | MaxTokens *int `json:"max_tokens,omitempty"` |
| 72 | Temperature *float64 `json:"temperature,omitempty"` |
| 73 | TopP *float64 `json:"top_p,omitempty"` |
| 74 | Stream bool `json:"stream,omitempty"` |
| 75 | StreamOptions *streamOptions `json:"stream_options,omitempty"` |
| 76 | Stop []string `json:"stop,omitempty"` |
| 77 | Tools []openaiTool `json:"tools,omitempty"` |
| 78 | ToolChoice any `json:"tool_choice,omitempty"` |
| 79 | } |
| 80 | |
| 81 | type streamOptions struct { |
| 82 | IncludeUsage bool `json:"include_usage"` |
| 83 | } |
| 84 | |
| 85 | type openaiMessage struct { |
| 86 | Role string `json:"role"` |
| 87 | Content any `json:"content"` |
| 88 | ToolCalls []openaiToolCall `json:"tool_calls,omitempty"` |
| 89 | ToolCallID string `json:"tool_call_id,omitempty"` |
| 90 | } |
| 91 | |
| 92 | type openaiTool struct { |
| 93 | Type string `json:"type"` |
| 94 | Function openaiFunction `json:"function"` |
| 95 | } |
| 96 | |
| 97 | type openaiFunction struct { |
| 98 | Name string `json:"name"` |
| 99 | Description string `json:"description,omitempty"` |
| 100 | Parameters json.RawMessage `json:"parameters,omitempty"` |
| 101 | } |
| 102 | |
| 103 | type openaiToolCall struct { |
| 104 | ID string `json:"id"` |
| 105 | Type string `json:"type"` |
| 106 | Function openaiCallFunc `json:"function"` |
| 107 | } |
| 108 | |
| 109 | type openaiCallFunc struct { |
| 110 | Name string `json:"name"` |
| 111 | Arguments string `json:"arguments"` |
| 112 | } |
| 113 | |
| 114 | type openaiResponse struct { |
| 115 | ID string `json:"id"` |
| 116 | Object string `json:"object"` |
| 117 | Created int64 `json:"created"` |
| 118 | Model string `json:"model"` |
| 119 | Choices []openaiChoice `json:"choices"` |
| 120 | Usage *openaiUsage `json:"usage,omitempty"` |
| 121 | } |
| 122 | |
| 123 | type openaiChoice struct { |
| 124 | Index int `json:"index"` |
| 125 | Message openaiMessage `json:"message"` |
| 126 | FinishReason *string `json:"finish_reason"` |
| 127 | } |
| 128 | |
| 129 | type openaiUsage struct { |
| 130 | PromptTokens int `json:"prompt_tokens"` |
| 131 | CompletionTokens int `json:"completion_tokens"` |
| 132 | TotalTokens int `json:"total_tokens"` |
| 133 | PromptTokensDetails *openaiPromptTokensDetails `json:"prompt_tokens_details,omitempty"` |
| 134 | } |
| 135 | |
| 136 | type openaiPromptTokensDetails struct { |
| 137 | CachedTokens int `json:"cached_tokens"` |
| 138 | } |
| 139 | |
| 140 | // ---------- OpenAI streaming types ---------- |
| 141 | |
| 142 | type openaiStreamChunk struct { |
| 143 | ID string `json:"id"` |
| 144 | Object string `json:"object"` |
| 145 | Created int64 `json:"created"` |
| 146 | Model string `json:"model"` |
| 147 | Choices []openaiStreamChoice `json:"choices"` |
| 148 | Usage *openaiUsage `json:"usage,omitempty"` |
| 149 | } |
| 150 | |
| 151 | type openaiStreamChoice struct { |
| 152 | Index int `json:"index"` |
| 153 | Delta openaiStreamDelta `json:"delta"` |
| 154 | FinishReason *string `json:"finish_reason"` |
| 155 | } |
| 156 | |
| 157 | type openaiStreamDelta struct { |
| 158 | Role string `json:"role,omitempty"` |
| 159 | Content *string `json:"content,omitempty"` |
| 160 | ToolCalls []openaiStreamToolCall `json:"tool_calls,omitempty"` |
| 161 | } |
| 162 | |
| 163 | type openaiStreamToolCall struct { |
| 164 | Index int `json:"index"` |
| 165 | ID string `json:"id,omitempty"` |
| 166 | Type string `json:"type,omitempty"` |
| 167 | Function *openaiCallFunc `json:"function,omitempty"` |
| 168 | } |
| 169 | |