15
15
import Foundation
16
16
17
17
/// Response message for BidiGenerateContent RPC call.
18
- struct BidiGenerateContentServerMessage : Decodable {
18
+ struct BidiGenerateContentServerMessage {
19
19
/// The type of the message.
20
- enum MessageType : Decodable {
20
+ enum MessageType {
21
21
/// Sent in response to a `BidiGenerateContentSetup` message from the client.
22
22
case setupComplete( BidiGenerateContentSetupComplete )
23
23
@@ -43,3 +43,55 @@ struct BidiGenerateContentServerMessage: Decodable {
43
43
/// Usage metadata about the response(s).
44
44
let usageMetadata : GenerateContentResponse . UsageMetadata ?
45
45
}
46
+
47
+ // MARK: - Decodable
48
+
49
+ extension BidiGenerateContentServerMessage : Decodable {
50
+ enum CodingKeys : String , CodingKey {
51
+ case setupComplete
52
+ case serverContent
53
+ case toolCall
54
+ case toolCallCancellation
55
+ case goAway
56
+ case usageMetadata
57
+ }
58
+
59
+ init ( from decoder: any Decoder ) throws {
60
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
61
+
62
+ if let setupComplete = try container. decodeIfPresent (
63
+ BidiGenerateContentSetupComplete . self,
64
+ forKey: . setupComplete
65
+ ) {
66
+ messageType = . setupComplete( setupComplete)
67
+ } else if let serverContent = try container. decodeIfPresent (
68
+ BidiGenerateContentServerContent . self,
69
+ forKey: . serverContent
70
+ ) {
71
+ messageType = . serverContent( serverContent)
72
+ } else if let toolCall = try container. decodeIfPresent (
73
+ BidiGenerateContentToolCall . self,
74
+ forKey: . toolCall
75
+ ) {
76
+ messageType = . toolCall( toolCall)
77
+ } else if let toolCallCancellation = try container. decodeIfPresent (
78
+ BidiGenerateContentToolCallCancellation . self,
79
+ forKey: . toolCallCancellation
80
+ ) {
81
+ messageType = . toolCallCancellation( toolCallCancellation)
82
+ } else if let goAway = try container. decodeIfPresent ( GoAway . self, forKey: . goAway) {
83
+ messageType = . goAway( goAway)
84
+ } else {
85
+ let context = DecodingError . Context (
86
+ codingPath: decoder. codingPath,
87
+ debugDescription: " Could not decode server message. "
88
+ )
89
+ throw DecodingError . dataCorrupted ( context)
90
+ }
91
+
92
+ usageMetadata = try container. decodeIfPresent (
93
+ GenerateContentResponse . UsageMetadata. self,
94
+ forKey: . usageMetadata
95
+ )
96
+ }
97
+ }
0 commit comments