@@ -35,7 +35,7 @@ public class Chat {
3535 /// - Parameter parts: The new content to send as a single chat message.
3636 /// - Returns: The model's response if no error occurred.
3737 /// - Throws: A ``GenerateContentError`` if an error occurred.
38- public func sendMessage( _ parts: any ThrowingPartsRepresentable ... ) async throws
38+ public func sendMessage( _ parts: any PartsRepresentable ... ) async throws
3939 -> GenerateContentResponse {
4040 return try await sendMessage ( [ ModelContent ( parts: parts) ] )
4141 }
@@ -45,19 +45,10 @@ public class Chat {
4545 /// - Parameter content: The new content to send as a single chat message.
4646 /// - Returns: The model's response if no error occurred.
4747 /// - Throws: A ``GenerateContentError`` if an error occurred.
48- public func sendMessage( _ content: @autoclosure ( ) throws -> [ ModelContent ] ) async throws
48+ public func sendMessage( _ content: [ ModelContent ] ) async throws
4949 -> GenerateContentResponse {
5050 // Ensure that the new content has the role set.
51- let newContent : [ ModelContent ]
52- do {
53- newContent = try content ( ) . map ( populateContentRole ( _: ) )
54- } catch let underlying {
55- if let contentError = underlying as? ImageConversionError {
56- throw GenerateContentError . promptImageContentError ( underlying: contentError)
57- } else {
58- throw GenerateContentError . internalError ( underlying: underlying)
59- }
60- }
51+ let newContent = content. map ( populateContentRole ( _: ) )
6152
6253 // Send the history alongside the new message as context.
6354 let request = history + newContent
@@ -85,7 +76,7 @@ public class Chat {
8576 /// - Parameter parts: The new content to send as a single chat message.
8677 /// - Returns: A stream containing the model's response or an error if an error occurred.
8778 @available ( macOS 12 . 0 , * )
88- public func sendMessageStream( _ parts: any ThrowingPartsRepresentable ... ) throws
79+ public func sendMessageStream( _ parts: any PartsRepresentable ... ) throws
8980 -> AsyncThrowingStream < GenerateContentResponse , Error > {
9081 return try sendMessageStream ( [ ModelContent ( parts: parts) ] )
9182 }
@@ -95,24 +86,14 @@ public class Chat {
9586 /// - Parameter content: The new content to send as a single chat message.
9687 /// - Returns: A stream containing the model's response or an error if an error occurred.
9788 @available ( macOS 12 . 0 , * )
98- public func sendMessageStream( _ content: @autoclosure ( ) throws -> [ ModelContent ] ) throws
89+ public func sendMessageStream( _ content: [ ModelContent ] ) throws
9990 -> AsyncThrowingStream < GenerateContentResponse , Error > {
100- let resolvedContent : [ ModelContent ]
101- do {
102- resolvedContent = try content ( )
103- } catch let underlying {
104- if let contentError = underlying as? ImageConversionError {
105- throw GenerateContentError . promptImageContentError ( underlying: contentError)
106- }
107- throw GenerateContentError . internalError ( underlying: underlying)
108- }
109-
11091 return AsyncThrowingStream { continuation in
11192 Task {
11293 var aggregatedContent : [ ModelContent ] = [ ]
11394
11495 // Ensure that the new content has the role set.
115- let newContent : [ ModelContent ] = resolvedContent . map ( populateContentRole ( _: ) )
96+ let newContent : [ ModelContent ] = content . map ( populateContentRole ( _: ) )
11697
11798 // Send the history alongside the new message as context.
11899 let request = history + newContent
@@ -146,20 +127,20 @@ public class Chat {
146127 }
147128
148129 private func aggregatedChunks( _ chunks: [ ModelContent ] ) -> ModelContent {
149- var parts : [ ModelContent . Part ] = [ ]
130+ var parts : [ any Part ] = [ ]
150131 var combinedText = " "
151132 for aggregate in chunks {
152133 // Loop through all the parts, aggregating the text and adding the images.
153134 for part in aggregate. parts {
154135 switch part {
155- case let . text ( str ) :
156- combinedText += str
136+ case let textPart as TextPart :
137+ combinedText += textPart . text
157138
158- case . inlineData , . fileData , . functionCall , . functionResponse :
139+ default :
159140 // Don't combine it, just add to the content. If there's any text pending, add that as
160141 // a part.
161142 if !combinedText. isEmpty {
162- parts. append ( . text ( combinedText) )
143+ parts. append ( TextPart ( combinedText) )
163144 combinedText = " "
164145 }
165146
@@ -169,7 +150,7 @@ public class Chat {
169150 }
170151
171152 if !combinedText. isEmpty {
172- parts. append ( . text ( combinedText) )
153+ parts. append ( TextPart ( combinedText) )
173154 }
174155
175156 return ModelContent ( role: " model " , parts: parts)
0 commit comments