@@ -28,12 +28,12 @@ public enum ContentType: String {
28
28
/// Starting with Claude 3 models, you can also send `image` content blocks.
29
29
public enum Content {
30
30
/// a single string
31
- case text( String )
31
+ case text( String , cacheControl : CacheControl ? = nil )
32
32
/// currently supported the `base64` source type for images, and the `image/jpeg`, `image/png`, `image/gif`, and `image/webp` media types.
33
- case image( ImageContent )
33
+ case image( ImageContent , cacheControl : CacheControl ? = nil )
34
34
case toolUse( ToolUseContent )
35
35
case toolResult( ToolResultContent )
36
- case document( DocumentContent )
36
+ case document( DocumentContent , cacheControl : CacheControl ? = nil )
37
37
38
38
/// The type of content block.
39
39
public var contentType : ContentType {
@@ -54,9 +54,19 @@ public enum Content {
54
54
55
55
extension Content : Encodable {
56
56
enum CodingKeys : String , CodingKey {
57
+ case type
58
+ }
59
+
60
+ enum TextCodingKeys : String , CodingKey {
61
+ case type
57
62
case text
63
+ case cacheControl = " cache_control "
64
+ }
65
+
66
+ enum BinaryCodingKeys : String , CodingKey {
58
67
case type
59
68
case source
69
+ case cacheControl = " cache_control "
60
70
}
61
71
62
72
enum ToolUseCodingKeys : String , CodingKey {
@@ -75,14 +85,20 @@ extension Content: Encodable {
75
85
76
86
public func encode( to encoder: Encoder ) throws {
77
87
switch self {
78
- case let . text( text) :
79
- var container = encoder. container ( keyedBy: CodingKeys . self)
88
+ case let . text( text, cacheControl ) :
89
+ var container = encoder. container ( keyedBy: TextCodingKeys . self)
80
90
try container. encode ( self . contentType. rawValue, forKey: . type)
81
91
try container. encode ( text, forKey: . text)
82
- case let . image( image) :
83
- var container = encoder. container ( keyedBy: CodingKeys . self)
92
+ if let cacheControl {
93
+ try container. encode ( cacheControl, forKey: . cacheControl)
94
+ }
95
+ case let . image( image, cacheControl) :
96
+ var container = encoder. container ( keyedBy: BinaryCodingKeys . self)
84
97
try container. encode ( self . contentType. rawValue, forKey: . type)
85
98
try container. encode ( image, forKey: . source)
99
+ if let cacheControl {
100
+ try container. encode ( cacheControl, forKey: . cacheControl)
101
+ }
86
102
case let . toolUse( toolUse) :
87
103
var container = encoder. container ( keyedBy: ToolUseCodingKeys . self)
88
104
try container. encode ( self . contentType. rawValue, forKey: . type)
@@ -97,10 +113,13 @@ extension Content: Encodable {
97
113
if toolResult. isError != nil {
98
114
try container. encode ( toolResult. isError, forKey: . isError)
99
115
}
100
- case let . document( document) :
101
- var container = encoder. container ( keyedBy: CodingKeys . self)
116
+ case let . document( document, cacheControl ) :
117
+ var container = encoder. container ( keyedBy: BinaryCodingKeys . self)
102
118
try container. encode ( self . contentType. rawValue, forKey: . type)
103
119
try container. encode ( document, forKey: . source)
120
+ if let cacheControl {
121
+ try container. encode ( cacheControl, forKey: . cacheControl)
122
+ }
104
123
}
105
124
}
106
125
}
@@ -113,18 +132,21 @@ extension Content: Decodable {
113
132
114
133
switch type {
115
134
case . text:
116
- let text = try container. decode ( String . self, forKey: . text)
135
+ let textContainer = try decoder. container ( keyedBy: TextCodingKeys . self)
136
+ let text = try textContainer. decode ( String . self, forKey: . text)
117
137
self = . text( text)
118
138
case . image:
119
- let image = try container. decode ( ImageContent . self, forKey: . source)
139
+ let imageContainer = try decoder. container ( keyedBy: BinaryCodingKeys . self)
140
+ let image = try imageContainer. decode ( ImageContent . self, forKey: . source)
120
141
self = . image( image)
121
142
case . toolUse:
122
143
let content = try ToolUseContent ( from: decoder)
123
144
self = . toolUse( content)
124
145
case . toolResult:
125
146
fatalError ( " ContentType: `tool_result` is only used by user, not by assistant " )
126
147
case . document:
127
- let document = try container. decode ( DocumentContent . self, forKey: . source)
148
+ let documentContainer = try decoder. container ( keyedBy: BinaryCodingKeys . self)
149
+ let document = try documentContainer. decode ( DocumentContent . self, forKey: . source)
128
150
self = . document( document)
129
151
case . none:
130
152
throw ClientError . failedToParseContentType ( contentTypeString)
0 commit comments