| package types
|
|
|
| import "encoding/json"
|
|
|
|
|
| type ClaudeRequest struct {
|
| Model string `json:"model"`
|
| MaxTokens int `json:"max_tokens,omitempty"`
|
| Messages []ClaudeMessage `json:"messages"`
|
| System interface{} `json:"system,omitempty"`
|
| Tools []ClaudeTool `json:"tools,omitempty"`
|
| ToolChoice interface{} `json:"tool_choice,omitempty"`
|
| Stream bool `json:"stream,omitempty"`
|
| Temperature float64 `json:"temperature,omitempty"`
|
| TopP float64 `json:"top_p,omitempty"`
|
| TopK int `json:"top_k,omitempty"`
|
| Metadata map[string]interface{} `json:"metadata,omitempty"`
|
| }
|
|
|
|
|
| type ClaudeMessage struct {
|
| Role string `json:"role"`
|
| Content interface{} `json:"content"`
|
| }
|
|
|
|
|
| type ClaudeSystemMessage struct {
|
| Type string `json:"type"`
|
| Text string `json:"text"`
|
| CacheControl map[string]interface{} `json:"cache_control,omitempty"`
|
| }
|
|
|
|
|
| type ClaudeContentBlock interface {
|
| GetType() string
|
| }
|
|
|
|
|
| type ClaudeContentBlockText struct {
|
| Type string `json:"type"`
|
| Text string `json:"text"`
|
| }
|
|
|
| func (c ClaudeContentBlockText) GetType() string { return c.Type }
|
|
|
|
|
| type ClaudeContentBlockImage struct {
|
| Type string `json:"type"`
|
| Source struct {
|
| Type string `json:"type"`
|
| MediaType string `json:"media_type"`
|
| Data string `json:"data"`
|
| } `json:"source"`
|
| }
|
|
|
| func (c ClaudeContentBlockImage) GetType() string { return c.Type }
|
|
|
|
|
| type ClaudeContentBlockToolUse struct {
|
| Type string `json:"type"`
|
| ID string `json:"id"`
|
| Name string `json:"name"`
|
| Input map[string]interface{} `json:"input"`
|
| }
|
|
|
| func (c ClaudeContentBlockToolUse) GetType() string { return c.Type }
|
|
|
|
|
| type ClaudeContentBlockToolResult struct {
|
| Type string `json:"type"`
|
| ToolUseID string `json:"tool_use_id"`
|
| Content interface{} `json:"content"`
|
| IsError bool `json:"is_error,omitempty"`
|
| }
|
|
|
| func (c ClaudeContentBlockToolResult) GetType() string { return c.Type }
|
|
|
|
|
| type ClaudeTool struct {
|
| Name string `json:"name"`
|
| Description string `json:"description"`
|
| InputSchema map[string]interface{} `json:"input_schema"`
|
| }
|
|
|
|
|
| func (m *ClaudeMessage) UnmarshalJSON(data []byte) error {
|
| type Alias ClaudeMessage
|
| aux := &struct {
|
| Content json.RawMessage `json:"content"`
|
| *Alias
|
| }{
|
| Alias: (*Alias)(m),
|
| }
|
|
|
| if err := json.Unmarshal(data, &aux); err != nil {
|
| return err
|
| }
|
|
|
|
|
| var str string
|
| if err := json.Unmarshal(aux.Content, &str); err == nil {
|
| m.Content = str
|
| return nil
|
| }
|
|
|
|
|
| var blocks []json.RawMessage
|
| if err := json.Unmarshal(aux.Content, &blocks); err != nil {
|
| return err
|
| }
|
|
|
| var contentBlocks []ClaudeContentBlock
|
| for _, block := range blocks {
|
| var typeCheck struct {
|
| Type string `json:"type"`
|
| }
|
| if err := json.Unmarshal(block, &typeCheck); err != nil {
|
| continue
|
| }
|
|
|
| switch typeCheck.Type {
|
| case "text":
|
| var textBlock ClaudeContentBlockText
|
| if err := json.Unmarshal(block, &textBlock); err == nil {
|
| contentBlocks = append(contentBlocks, textBlock)
|
| }
|
| case "image":
|
| var imageBlock ClaudeContentBlockImage
|
| if err := json.Unmarshal(block, &imageBlock); err == nil {
|
| contentBlocks = append(contentBlocks, imageBlock)
|
| }
|
| case "tool_use":
|
| var toolUseBlock ClaudeContentBlockToolUse
|
| if err := json.Unmarshal(block, &toolUseBlock); err == nil {
|
| contentBlocks = append(contentBlocks, toolUseBlock)
|
| }
|
| case "tool_result":
|
| var toolResultBlock ClaudeContentBlockToolResult
|
| if err := json.Unmarshal(block, &toolResultBlock); err == nil {
|
| contentBlocks = append(contentBlocks, toolResultBlock)
|
| }
|
| }
|
| }
|
|
|
| m.Content = contentBlocks
|
| return nil
|
| } |