Skip to content

Commit da8c21b

Browse files
andrewhearddaymxn
authored andcommitted
Fix BidiGenerateContentServerMessage decoding
1 parent 1b5d791 commit da8c21b

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

FirebaseAI/Sources/Types/Internal/Live/BidiGenerateContentServerMessage.swift

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import Foundation
1616

1717
/// Response message for BidiGenerateContent RPC call.
18-
struct BidiGenerateContentServerMessage: Decodable {
18+
struct BidiGenerateContentServerMessage {
1919
/// The type of the message.
20-
enum MessageType: Decodable {
20+
enum MessageType {
2121
/// Sent in response to a `BidiGenerateContentSetup` message from the client.
2222
case setupComplete(BidiGenerateContentSetupComplete)
2323

@@ -43,3 +43,55 @@ struct BidiGenerateContentServerMessage: Decodable {
4343
/// Usage metadata about the response(s).
4444
let usageMetadata: GenerateContentResponse.UsageMetadata?
4545
}
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

Comments
 (0)