|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.vertexai.type |
| 18 | + |
| 19 | +import kotlinx.serialization.DeserializationStrategy |
| 20 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 21 | +import kotlinx.serialization.Serializable |
| 22 | +import kotlinx.serialization.json.JsonContentPolymorphicSerializer |
| 23 | +import kotlinx.serialization.json.JsonElement |
| 24 | +import kotlinx.serialization.json.JsonNull |
| 25 | +import kotlinx.serialization.json.JsonObject |
| 26 | +import kotlinx.serialization.json.jsonObject |
| 27 | + |
| 28 | +/** |
| 29 | + * Parent interface for responses from the model during live interactions. |
| 30 | + * |
| 31 | + * @see LiveServerContent |
| 32 | + * @see LiveServerToolCall |
| 33 | + * @see LiveServerToolCallCancellation |
| 34 | + * @see LiveServerSetupComplete |
| 35 | + */ |
| 36 | +@PublicPreviewAPI |
| 37 | +@Deprecated( |
| 38 | + """The Firebase VertexAI SDK (firebase-vertexai) has been replaced with the FirebaseAI SDK (firebase-ai) to accommodate the evolving set of supported features and services. |
| 39 | +For migration details, see the migration guide: https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk""" |
| 40 | +) |
| 41 | +public interface LiveServerMessage |
| 42 | + |
| 43 | +/** |
| 44 | + * Incremental server update generated by the model in response to client messages. |
| 45 | + * |
| 46 | + * Content is generated as quickly as possible, and not in realtime. You may choose to buffer and |
| 47 | + * play it out in realtime. |
| 48 | + */ |
| 49 | +@PublicPreviewAPI |
| 50 | +@Deprecated( |
| 51 | + """The Firebase VertexAI SDK (firebase-vertexai) has been replaced with the FirebaseAI SDK (firebase-ai) to accommodate the evolving set of supported features and services. |
| 52 | +For migration details, see the migration guide: https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk""" |
| 53 | +) |
| 54 | +public class LiveServerContent( |
| 55 | + /** |
| 56 | + * The content that the model has generated as part of the current conversation with the user. |
| 57 | + * |
| 58 | + * This can be `null` if there is no content. |
| 59 | + */ |
| 60 | + public val content: Content?, |
| 61 | + |
| 62 | + /** |
| 63 | + * The model was interrupted by the client while generating data. |
| 64 | + * |
| 65 | + * An interruption occurs when the client sends a message while the model is actively sending |
| 66 | + * data. |
| 67 | + */ |
| 68 | + public val interrupted: Boolean, |
| 69 | + |
| 70 | + /** |
| 71 | + * The model has finished sending data in the current turn. |
| 72 | + * |
| 73 | + * Generation will only start in response to additional client messages. |
| 74 | + * |
| 75 | + * Can be set alongside [content], indicating that the [content] is the last in the turn. |
| 76 | + * |
| 77 | + * @see generationComplete |
| 78 | + */ |
| 79 | + public val turnComplete: Boolean, |
| 80 | + |
| 81 | + /** |
| 82 | + * The model has finished _generating_ data for the current turn. |
| 83 | + * |
| 84 | + * For realtime playback, there will be a delay between when the model finishes generating content |
| 85 | + * and the client has finished playing back the generated content. [generationComplete] indicates |
| 86 | + * that the model is done generating data, while [turnComplete] indicates the model is waiting for |
| 87 | + * additional client messages. Sending a message during this delay may cause an [interrupted] |
| 88 | + * message to be sent. |
| 89 | + * |
| 90 | + * Note that if the model was [interrupted], this will not be set. The model will go from |
| 91 | + * [interrupted] -> [turnComplete]. |
| 92 | + */ |
| 93 | + public val generationComplete: Boolean, |
| 94 | +) : LiveServerMessage { |
| 95 | + @OptIn(ExperimentalSerializationApi::class) |
| 96 | + @Serializable |
| 97 | + internal data class Internal( |
| 98 | + val modelTurn: Content.Internal? = null, |
| 99 | + val interrupted: Boolean = false, |
| 100 | + val turnComplete: Boolean = false, |
| 101 | + val generationComplete: Boolean = false |
| 102 | + ) |
| 103 | + @Serializable |
| 104 | + internal data class InternalWrapper(val serverContent: Internal) : InternalLiveServerMessage { |
| 105 | + @OptIn(ExperimentalSerializationApi::class) |
| 106 | + override fun toPublic() = |
| 107 | + LiveServerContent( |
| 108 | + serverContent.modelTurn?.toPublic(), |
| 109 | + serverContent.interrupted, |
| 110 | + serverContent.turnComplete, |
| 111 | + serverContent.generationComplete |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** The model is ready to receive client messages. */ |
| 117 | +@PublicPreviewAPI |
| 118 | +@Deprecated( |
| 119 | + """The Firebase VertexAI SDK (firebase-vertexai) has been replaced with the FirebaseAI SDK (firebase-ai) to accommodate the evolving set of supported features and services. |
| 120 | +For migration details, see the migration guide: https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk""" |
| 121 | +) |
| 122 | +public class LiveServerSetupComplete : LiveServerMessage { |
| 123 | + @Serializable |
| 124 | + internal data class Internal(val setupComplete: JsonObject) : InternalLiveServerMessage { |
| 125 | + override fun toPublic() = LiveServerSetupComplete() |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Request for the client to execute the provided [functionCalls]. |
| 131 | + * |
| 132 | + * The client should return matching [FunctionResponsePart], where the `id` fields correspond to |
| 133 | + * individual [FunctionCallPart]s. |
| 134 | + * |
| 135 | + * @property functionCalls A list of [FunctionCallPart] to run and return responses for. |
| 136 | + */ |
| 137 | +@PublicPreviewAPI |
| 138 | +@Deprecated( |
| 139 | + """The Firebase VertexAI SDK (firebase-vertexai) has been replaced with the FirebaseAI SDK (firebase-ai) to accommodate the evolving set of supported features and services. |
| 140 | +For migration details, see the migration guide: https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk""" |
| 141 | +) |
| 142 | +public class LiveServerToolCall(public val functionCalls: List<FunctionCallPart>) : |
| 143 | + LiveServerMessage { |
| 144 | + @Serializable |
| 145 | + internal data class Internal( |
| 146 | + val functionCalls: List<FunctionCallPart.Internal.FunctionCall> = emptyList() |
| 147 | + ) |
| 148 | + @Serializable |
| 149 | + internal data class InternalWrapper(val toolCall: Internal) : InternalLiveServerMessage { |
| 150 | + override fun toPublic() = |
| 151 | + LiveServerToolCall( |
| 152 | + toolCall.functionCalls.map { functionCall -> |
| 153 | + FunctionCallPart( |
| 154 | + name = functionCall.name, |
| 155 | + args = functionCall.args.orEmpty().mapValues { it.value ?: JsonNull } |
| 156 | + ) |
| 157 | + } |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * Notification for the client to cancel a previous function call from [LiveServerToolCall]. |
| 164 | + * |
| 165 | + * You do not need to send [FunctionResponsePart]s for the cancelled [FunctionCallPart]s. |
| 166 | + * |
| 167 | + * @property functionIds A list of `id`s matching the `id` provided in a previous |
| 168 | + * [LiveServerToolCall], where only the provided `id`s should be cancelled. |
| 169 | + */ |
| 170 | +@PublicPreviewAPI |
| 171 | +@Deprecated( |
| 172 | + """The Firebase VertexAI SDK (firebase-vertexai) has been replaced with the FirebaseAI SDK (firebase-ai) to accommodate the evolving set of supported features and services. |
| 173 | +For migration details, see the migration guide: https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk""" |
| 174 | +) |
| 175 | +public class LiveServerToolCallCancellation(public val functionIds: List<String>) : |
| 176 | + LiveServerMessage { |
| 177 | + @Serializable internal data class Internal(val functionIds: List<String> = emptyList()) |
| 178 | + @Serializable |
| 179 | + internal data class InternalWrapper(val toolCallCancellation: Internal) : |
| 180 | + InternalLiveServerMessage { |
| 181 | + override fun toPublic() = LiveServerToolCallCancellation(toolCallCancellation.functionIds) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +@PublicPreviewAPI |
| 186 | +@Serializable(LiveServerMessageSerializer::class) |
| 187 | +internal sealed interface InternalLiveServerMessage { |
| 188 | + fun toPublic(): LiveServerMessage |
| 189 | +} |
| 190 | + |
| 191 | +@OptIn(PublicPreviewAPI::class) |
| 192 | +internal object LiveServerMessageSerializer : |
| 193 | + JsonContentPolymorphicSerializer<InternalLiveServerMessage>(InternalLiveServerMessage::class) { |
| 194 | + @OptIn(PublicPreviewAPI::class) |
| 195 | + override fun selectDeserializer( |
| 196 | + element: JsonElement |
| 197 | + ): DeserializationStrategy<InternalLiveServerMessage> { |
| 198 | + val jsonObject = element.jsonObject |
| 199 | + return when { |
| 200 | + "serverContent" in jsonObject -> LiveServerContent.InternalWrapper.serializer() |
| 201 | + "setupComplete" in jsonObject -> LiveServerSetupComplete.Internal.serializer() |
| 202 | + "toolCall" in jsonObject -> LiveServerToolCall.InternalWrapper.serializer() |
| 203 | + "toolCallCancellation" in jsonObject -> |
| 204 | + LiveServerToolCallCancellation.InternalWrapper.serializer() |
| 205 | + else -> |
| 206 | + throw SerializationException( |
| 207 | + "The given subclass of LiveServerMessage (${javaClass.simpleName}) is not supported in the serialization yet." |
| 208 | + ) |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments