Skip to content

Commit 12259fd

Browse files
committed
update live generative model
1 parent cb61d5c commit 12259fd

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.google.firebase.ai.type.Tool
3232
import com.google.firebase.annotations.concurrent.Blocking
3333
import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
3434
import com.google.firebase.auth.internal.InternalAuthProvider
35+
import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession
3536
import io.ktor.websocket.Frame
3637
import io.ktor.websocket.close
3738
import io.ktor.websocket.readBytes
@@ -114,31 +115,28 @@ internal constructor(
114115
)
115116
.toInternal()
116117
val data: String = Json.encodeToString(clientMessage)
118+
var webSession: DefaultClientWebSocketSession? = null
117119
try {
118-
val webSession = controller.getWebSocketSession(location)
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)
120+
webSession = controller.getWebSocketSession(location)
121+
webSession.send(Frame.Text(data))
122+
val receivedJsonStr = webSession.incoming.receive().readBytes().toString(Charsets.UTF_8)
123+
val receivedJson = JSON.parseToJsonElement(receivedJsonStr)
123124

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)
125+
return if (receivedJson is JsonObject && "setupComplete" in receivedJson) {
126+
LiveSession(
127+
session = webSession,
128+
blockingDispatcher = blockingDispatcher,
129+
firebaseApp = firebaseApp
130+
)
131+
} else {
132+
webSession.close()
133+
throw ServiceConnectionHandshakeFailedException("Unable to connect to the server")
139134
}
140135
} catch (e: ClosedReceiveChannelException) {
141-
throw ServiceConnectionHandshakeFailedException("Channel was closed by the server.", e)
136+
val reason = webSession?.closeReason?.await()
137+
val message =
138+
"Channel was closed by the server.${if(reason!=null) " Details: ${reason.message}" else "" }"
139+
throw ServiceConnectionHandshakeFailedException(message, e)
142140
}
143141
}
144142

0 commit comments

Comments
 (0)