Skip to content

Commit cb61d5c

Browse files
committed
add close reason
1 parent 9b9b8f8 commit cb61d5c

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

firebase-ai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
- [changed] **Breaking Change**: Removed the `candidateCount` option from `LiveGenerationConfig`
4+
- [changed] Added better error messages to `ServiceConnectionHandshakeFailedException`
45

56
# 17.3.0
67

firebase-ai/src/main/kotlin/com/google/firebase/ai/LiveGenerativeModel.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,29 @@ internal constructor(
116116
val data: String = Json.encodeToString(clientMessage)
117117
try {
118118
val webSession = controller.getWebSocketSession(location)
119-
webSession.send(Frame.Text(data))
120-
val receivedJsonStr = webSession.incoming.receive().readBytes().toString(Charsets.UTF_8)
121-
val receivedJson = JSON.parseToJsonElement(receivedJsonStr)
119+
try {
120+
webSession.send(Frame.Text(data))
121+
val receivedJsonStr = webSession.incoming.receive().readBytes().toString(Charsets.UTF_8)
122+
val receivedJson = JSON.parseToJsonElement(receivedJsonStr)
122123

123-
return if (receivedJson is JsonObject && "setupComplete" in receivedJson) {
124-
LiveSession(
125-
session = webSession,
126-
blockingDispatcher = blockingDispatcher,
127-
firebaseApp = firebaseApp
128-
)
129-
} else {
130-
webSession.close()
131-
throw ServiceConnectionHandshakeFailedException("Unable to connect to the server")
124+
return if (receivedJson is JsonObject && "setupComplete" in receivedJson) {
125+
LiveSession(
126+
session = webSession,
127+
blockingDispatcher = blockingDispatcher,
128+
firebaseApp = firebaseApp
129+
)
130+
} else {
131+
webSession.close()
132+
throw ServiceConnectionHandshakeFailedException("Unable to connect to the server")
133+
}
134+
} catch (e: ClosedReceiveChannelException) {
135+
val reason = webSession.closeReason.await()
136+
val message =
137+
"Channel was closed by the server.${if(reason!=null) " Details: ${reason.message}" else "" }"
138+
throw ServiceConnectionHandshakeFailedException(message, e)
132139
}
133140
} catch (e: ClosedReceiveChannelException) {
134-
throw ServiceConnectionHandshakeFailedException("Channel was closed by the server", e)
141+
throw ServiceConnectionHandshakeFailedException("Channel was closed by the server.", e)
135142
}
136143
}
137144

firebase-ai/src/main/kotlin/com/google/firebase/ai/common/APIController.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import io.ktor.client.engine.HttpClientEngine
3838
import io.ktor.client.engine.okhttp.OkHttp
3939
import io.ktor.client.plugins.HttpTimeout
4040
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
41-
import io.ktor.client.plugins.websocket.ClientWebSocketSession
41+
import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession
4242
import io.ktor.client.plugins.websocket.WebSockets
4343
import io.ktor.client.plugins.websocket.webSocketSession
4444
import io.ktor.client.request.HttpRequestBuilder
@@ -174,7 +174,7 @@ internal constructor(
174174
"wss://firebasevertexai.googleapis.com/ws/google.firebase.vertexai.v1beta.GenerativeService/BidiGenerateContent?key=$key"
175175
}
176176

177-
suspend fun getWebSocketSession(location: String): ClientWebSocketSession =
177+
suspend fun getWebSocketSession(location: String): DefaultClientWebSocketSession =
178178
client.webSocketSession(getBidiEndpoint(location)) { applyCommonHeaders() }
179179

180180
fun generateContentStream(

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/LiveSession.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import com.google.firebase.ai.common.util.CancelledCoroutineScope
2929
import com.google.firebase.ai.common.util.accumulateUntil
3030
import com.google.firebase.ai.common.util.childJob
3131
import com.google.firebase.annotations.concurrent.Blocking
32-
import io.ktor.client.plugins.websocket.ClientWebSocketSession
32+
import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession
3333
import io.ktor.websocket.Frame
3434
import io.ktor.websocket.close
3535
import io.ktor.websocket.readBytes
@@ -59,7 +59,7 @@ import kotlinx.serialization.json.Json
5959
@OptIn(ExperimentalSerializationApi::class)
6060
public class LiveSession
6161
internal constructor(
62-
private val session: ClientWebSocketSession,
62+
private val session: DefaultClientWebSocketSession,
6363
@Blocking private val blockingDispatcher: CoroutineContext,
6464
private var audioHelper: AudioHelper? = null,
6565
private val firebaseApp: FirebaseApp,

0 commit comments

Comments
 (0)