Skip to content

Commit 2ea027d

Browse files
committed
Log json payload if we don't have a mapping
1 parent a8c24ba commit 2ea027d

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

FirebaseAI/Sources/AILog.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ enum AILog {
6868
case fallbackValueUsed = 3017
6969
case urlMetadataUnrecognizedURLRetrievalStatus = 3018
7070
case liveSessionUnsupportedMessage = 3019
71-
case liveSessionFailedToEncodeClientMessage = 3020
72-
case liveSessionFailedToEncodeClientMessagePayload = 3021
73-
case liveSessionFailedToSendClientMessage = 3022
74-
case liveSessionUnexpectedResponse = 3023
75-
case liveSessionGoingAwaySoon = 3024
76-
case decodedMissingProtoDurationSuffix = 3025
77-
case decodedInvalidProtoDurationString = 3026
78-
case decodedInvalidProtoDurationSeconds = 3027
79-
case decodedInvalidProtoDurationNanoseconds = 3028
71+
case liveSessionUnsupportedMessagePayload = 3020
72+
case liveSessionFailedToEncodeClientMessage = 3021
73+
case liveSessionFailedToEncodeClientMessagePayload = 3022
74+
case liveSessionFailedToSendClientMessage = 3023
75+
case liveSessionUnexpectedResponse = 3024
76+
case liveSessionGoingAwaySoon = 3025
77+
case decodedMissingProtoDurationSuffix = 3026
78+
case decodedInvalidProtoDurationString = 3027
79+
case decodedInvalidProtoDurationSeconds = 3028
80+
case decodedInvalidProtoDurationNanoseconds = 3029
8081

8182
// SDK State Errors
8283
case generateContentResponseNoCandidates = 4000

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ extension BidiGenerateContentServerMessage: Decodable {
8686
} else if let goAway = try container.decodeIfPresent(GoAway.self, forKey: .goAway) {
8787
messageType = .goAway(goAway)
8888
} else {
89-
let context = DecodingError.Context(
90-
codingPath: decoder.codingPath,
91-
debugDescription: "Could not decode server message."
92-
)
93-
throw DecodingError.dataCorrupted(context)
89+
throw InvalidMessageTypeError()
9490
}
9591

9692
usageMetadata = try container.decodeIfPresent(
@@ -99,3 +95,11 @@ extension BidiGenerateContentServerMessage: Decodable {
9995
)
10096
}
10197
}
98+
99+
struct InvalidMessageTypeError: Error, Sendable, CustomNSError {
100+
public var errorUserInfo: [String: Any] {
101+
[
102+
NSLocalizedDescriptionKey: "Missing server message type.",
103+
]
104+
}
105+
}

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,26 @@ actor LiveSessionService {
206206
from: message
207207
)
208208
} catch {
209-
throw LiveSessionUnsupportedMessageError(underlyingError: error)
209+
// only log the json if it wasn't a decoding error, but an unsupported message type
210+
if error is InvalidMessageTypeError {
211+
AILog.error(
212+
code: .liveSessionUnsupportedMessage,
213+
"The server sent a message that we don't currently have a mapping for."
214+
)
215+
216+
AILog.debug(
217+
code: .liveSessionUnsupportedMessagePayload,
218+
message.encodeToJsonString() ?? "\(message)"
219+
)
220+
}
221+
222+
let error = LiveSessionUnsupportedMessageError(underlyingError: error)
223+
// if we've already finished setting up, then only surface the error through responses
224+
// otherwise, make the setup task error as well
225+
if !resumed {
226+
setupComplete.resume(throwing: error)
227+
}
228+
throw error
210229
}
211230

212231
if case .setupComplete = response.messageType {
@@ -231,13 +250,6 @@ actor LiveSessionService {
231250
}
232251

233252
responseContinuation.yield(liveMessage)
234-
} else {
235-
// we don't raise an error, since this allows us to add support internally but not
236-
// publicly. We still log it in debug though, in case it's not expected.
237-
AILog.debug(
238-
code: .liveSessionUnsupportedMessage,
239-
"The server sent a message that we don't currently have a mapping for: \(response)"
240-
)
241253
}
242254
}
243255
} catch {
@@ -348,3 +360,19 @@ actor LiveSessionService {
348360
return AsyncWebSocket(urlSession: urlSession, urlRequest: urlRequest)
349361
}
350362
}
363+
364+
private extension Data {
365+
/// Encodes this into a raw json string, with no regard to specific keys.
366+
///
367+
/// Will return `nil` if this data doesn't represent a valid json object.
368+
func encodeToJsonString() -> String? {
369+
do {
370+
let object = try JSONSerialization.jsonObject(with: self)
371+
let data = try JSONSerialization.data(withJSONObject: object)
372+
373+
return String(data: data, encoding: .utf8)
374+
} catch {
375+
return nil
376+
}
377+
}
378+
}

0 commit comments

Comments
 (0)