@@ -22,6 +22,7 @@ import com.google.firebase.auth.internal.InternalAuthProvider
2222import com.google.firebase.vertexai.common.APIController
2323import com.google.firebase.vertexai.common.AppCheckHeaderProvider
2424import com.google.firebase.vertexai.type.BidiGenerateContentClientMessage
25+ import com.google.firebase.vertexai.type.BidiServerHandshakeFailed
2526import com.google.firebase.vertexai.type.Content
2627import com.google.firebase.vertexai.type.LiveGenerationConfig
2728import com.google.firebase.vertexai.type.LiveSession
@@ -33,8 +34,8 @@ import io.ktor.client.engine.okhttp.OkHttp
3334import io.ktor.client.plugins.websocket.WebSockets
3435import io.ktor.client.plugins.websocket.webSocketSession
3536import io.ktor.websocket.Frame
37+ import io.ktor.websocket.close
3638import io.ktor.websocket.readBytes
37- import java.nio.channels.ClosedChannelException
3839import kotlinx.serialization.encodeToString
3940import kotlinx.serialization.json.Json
4041
@@ -84,9 +85,9 @@ internal constructor(
8485 /* *
8586 * Returns a LiveSession object using which you could send/receive messages from the server
8687 * @return LiveSession object created. Returns null if the object cannot be created.
87- * @throws [ClosedChannelException ] if channel was closed before creating a websocket connection .
88+ * @throws [BidiServerHandshakeFailed ] if the handshake with the server failed .
8889 */
89- public suspend fun connect (): LiveSession ? {
90+ public suspend fun connect (): LiveSession {
9091 val client = HttpClient (OkHttp ) { install(WebSockets ) }
9192
9293 val bidiEndPoint = this .controller.getBidiEndpoint(location)
@@ -115,7 +116,8 @@ internal constructor(
115116 return if (shouldReturn) {
116117 LiveSession (session = webSession, isRecording = false )
117118 } else {
118- null
119+ webSession.close()
120+ throw BidiServerHandshakeFailed ()
119121 }
120122 }
121123
0 commit comments