|
| 1 | +package anthropic |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + |
| 7 | + llm "github.com/mutablelogic/go-llm" |
| 8 | +) |
| 9 | + |
| 10 | +/////////////////////////////////////////////////////////////////////////////// |
| 11 | +// TYPES |
| 12 | + |
| 13 | +type Content struct { |
| 14 | + Type string `json:"type"` // image, document, text, tool_use |
| 15 | + ContentText |
| 16 | + ContentAttachment |
| 17 | + *ContentTool |
| 18 | + ContentToolResult |
| 19 | + CacheControl *cachecontrol `json:"cache_control,omitempty"` // ephemeral |
| 20 | +} |
| 21 | + |
| 22 | +type ContentText struct { |
| 23 | + Text string `json:"text,omitempty"` // text content |
| 24 | +} |
| 25 | + |
| 26 | +type ContentTool struct { |
| 27 | + Id string `json:"id,omitempty"` // tool id |
| 28 | + Name string `json:"name,omitempty"` // tool name |
| 29 | + Input map[string]any `json:"input"` // tool input |
| 30 | + InputJson string `json:"partial_json,omitempty"` // partial json input (for streaming) |
| 31 | +} |
| 32 | + |
| 33 | +type ContentAttachment struct { |
| 34 | + Title string `json:"title,omitempty"` // title of the document |
| 35 | + Context string `json:"context,omitempty"` // context of the document |
| 36 | + Citations *contentcitation `json:"citations,omitempty"` // citations of the document |
| 37 | + Source *contentsource `json:"source,omitempty"` // image or document content |
| 38 | +} |
| 39 | + |
| 40 | +type ContentToolResult struct { |
| 41 | + Id string `json:"tool_use_id,omitempty"` // tool id |
| 42 | + Content any `json:"content,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +type contentsource struct { |
| 46 | + Type string `json:"type"` // base64 or text |
| 47 | + MediaType string `json:"media_type"` // image/jpeg, image/png, image/gif, image/webp, application/pdf, text/plain |
| 48 | + Data any `json:"data"` // ...base64 or text encoded data |
| 49 | +} |
| 50 | + |
| 51 | +type cachecontrol struct { |
| 52 | + Type string `json:"type"` // ephemeral |
| 53 | +} |
| 54 | + |
| 55 | +type contentcitation struct { |
| 56 | + Enabled bool `json:"enabled"` // true |
| 57 | +} |
| 58 | + |
| 59 | +/////////////////////////////////////////////////////////////////////////////// |
| 60 | +// GLOBALS |
| 61 | + |
| 62 | +var ( |
| 63 | + supportedAttachments = map[string]string{ |
| 64 | + "image/jpeg": "image", |
| 65 | + "image/png": "image", |
| 66 | + "image/gif": "image", |
| 67 | + "image/webp": "image", |
| 68 | + "application/pdf": "document", |
| 69 | + "text/plain": "text", |
| 70 | + } |
| 71 | +) |
| 72 | + |
| 73 | +/////////////////////////////////////////////////////////////////////////////// |
| 74 | +// LIFECYCLE |
| 75 | + |
| 76 | +// Return a content object with text content |
| 77 | +func NewTextContent(v string) *Content { |
| 78 | + return &Content{ |
| 79 | + Type: "text", |
| 80 | + ContentText: ContentText{ |
| 81 | + Text: v, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Return a content object with tool result |
| 87 | +func NewToolResultContent(v llm.ToolResult) *Content { |
| 88 | + content := new(Content) |
| 89 | + content.Type = "tool_result" |
| 90 | + content.ContentToolResult.Id = v.Call().Id() |
| 91 | + // content.ContentToolResult.Name = v.Call().Name() |
| 92 | + |
| 93 | + // We only support JSON encoding for the moment |
| 94 | + data, err := json.Marshal(v.Value()) |
| 95 | + if err != nil { |
| 96 | + content.ContentToolResult.Content = err.Error() |
| 97 | + } else { |
| 98 | + content.ContentToolResult.Content = string(data) |
| 99 | + } |
| 100 | + |
| 101 | + return content |
| 102 | +} |
| 103 | + |
| 104 | +// Make attachment content |
| 105 | +func NewAttachment(attachment *llm.Attachment, ephemeral, citations bool) (*Content, error) { |
| 106 | + // Detect mimetype |
| 107 | + mimetype := attachment.Type() |
| 108 | + if strings.HasPrefix(mimetype, "text/") { |
| 109 | + // Switch to text/plain - TODO: charsets? |
| 110 | + mimetype = "text/plain" |
| 111 | + } |
| 112 | + |
| 113 | + // Check supported mimetype |
| 114 | + typ, exists := supportedAttachments[mimetype] |
| 115 | + if !exists { |
| 116 | + return nil, llm.ErrBadParameter.Withf("unsupported or undetected mimetype %q", mimetype) |
| 117 | + } |
| 118 | + |
| 119 | + // Create attachment |
| 120 | + content := new(Content) |
| 121 | + content.Type = typ |
| 122 | + if ephemeral { |
| 123 | + content.CacheControl = &cachecontrol{Type: "ephemeral"} |
| 124 | + } |
| 125 | + |
| 126 | + // Handle by type |
| 127 | + switch typ { |
| 128 | + case "text": |
| 129 | + content.Type = "document" |
| 130 | + content.Title = attachment.Filename() |
| 131 | + content.Source = &contentsource{ |
| 132 | + Type: "text", |
| 133 | + MediaType: mimetype, |
| 134 | + Data: string(attachment.Data()), |
| 135 | + } |
| 136 | + if citations { |
| 137 | + content.Citations = &contentcitation{Enabled: true} |
| 138 | + } |
| 139 | + case "document": |
| 140 | + content.Source = &contentsource{ |
| 141 | + Type: "base64", |
| 142 | + MediaType: mimetype, |
| 143 | + Data: attachment.Data(), |
| 144 | + } |
| 145 | + if citations { |
| 146 | + content.Citations = &contentcitation{Enabled: true} |
| 147 | + } |
| 148 | + case "image": |
| 149 | + content.Source = &contentsource{ |
| 150 | + Type: "base64", |
| 151 | + MediaType: mimetype, |
| 152 | + Data: attachment.Data(), |
| 153 | + } |
| 154 | + default: |
| 155 | + return nil, llm.ErrBadParameter.Withf("unsupported attachment type %q", typ) |
| 156 | + } |
| 157 | + |
| 158 | + // Return success |
| 159 | + return content, nil |
| 160 | +} |
0 commit comments