Skip to content

Commit d7296c3

Browse files
author
David Motsonashvili
committed
rebase completed, one failing test to fix
1 parent e881958 commit d7296c3

File tree

12 files changed

+142
-205
lines changed

12 files changed

+142
-205
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/GenerativeModel.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ import com.google.firebase.auth.internal.InternalAuthProvider
2222
import com.google.firebase.vertexai.common.APIController
2323
import com.google.firebase.vertexai.common.CountTokensRequest
2424
import com.google.firebase.vertexai.common.GenerateContentRequest
25-
import com.google.firebase.vertexai.common.HeaderProvider
26-
import com.google.firebase.vertexai.internal.util.AppCheckHeaderProvider
27-
import com.google.firebase.vertexai.internal.util.toInternal
28-
import com.google.firebase.vertexai.internal.util.toPublic
25+
import com.google.firebase.vertexai.common.AppCheckHeaderProvider
2926
import com.google.firebase.vertexai.type.Content
3027
import com.google.firebase.vertexai.type.CountTokensResponse
3128
import com.google.firebase.vertexai.type.FinishReason

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/ImagenModel.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
2020
import com.google.firebase.auth.internal.InternalAuthProvider
2121
import com.google.firebase.vertexai.common.APIController
2222
import com.google.firebase.vertexai.common.ContentBlockedException
23-
import com.google.firebase.vertexai.internal.GenerateImageRequest
24-
import com.google.firebase.vertexai.internal.GenerateImageResponse
25-
import com.google.firebase.vertexai.internal.ImagenParameters
26-
import com.google.firebase.vertexai.internal.ImagenPromptInstance
27-
import com.google.firebase.vertexai.internal.util.AppCheckHeaderProvider
28-
import com.google.firebase.vertexai.internal.util.toInternal
29-
import com.google.firebase.vertexai.internal.util.toPublicGCS
30-
import com.google.firebase.vertexai.internal.util.toPublicInline
23+
import com.google.firebase.vertexai.common.AppCheckHeaderProvider
24+
import com.google.firebase.vertexai.common.GenerateImageRequest
25+
import com.google.firebase.vertexai.common.ImagenParameters
26+
import com.google.firebase.vertexai.common.ImagenPromptInstance
3127
import com.google.firebase.vertexai.type.FirebaseVertexAIException
3228
import com.google.firebase.vertexai.type.ImagenGCSImage
3329
import com.google.firebase.vertexai.type.ImagenGenerationConfig
@@ -133,7 +129,7 @@ internal constructor(
133129
}
134130
}
135131

136-
private fun GenerateImageResponse.validate(): GenerateImageResponse {
132+
private fun ImagenGenerationResponse.Internal.validate(): ImagenGenerationResponse.Internal {
137133
if (predictions.none { it.mimeType != null }) {
138134
throw ContentBlockedException(
139135
message = predictions.first { it.raiFilteredReason != null }.raiFilteredReason

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import com.google.firebase.vertexai.type.CountTokensResponse
2525
import com.google.firebase.vertexai.type.FinishReason
2626
import com.google.firebase.vertexai.type.GRpcErrorResponse
2727
import com.google.firebase.vertexai.type.GenerateContentResponse
28-
import com.google.firebase.vertexai.internal.GenerateImageRequest
29-
import com.google.firebase.vertexai.internal.GenerateImageResponse
28+
import com.google.firebase.vertexai.type.ImagenGenerationResponse
3029
import com.google.firebase.vertexai.type.RequestOptions
3130
import com.google.firebase.vertexai.type.Response
3231
import io.ktor.client.HttpClient
@@ -59,6 +58,7 @@ import kotlinx.coroutines.flow.catch
5958
import kotlinx.coroutines.flow.channelFlow
6059
import kotlinx.coroutines.flow.map
6160
import kotlinx.coroutines.launch
61+
import kotlinx.coroutines.tasks.await
6262
import kotlinx.coroutines.withTimeout
6363
import kotlinx.serialization.json.Json
6464

@@ -124,15 +124,15 @@ internal constructor(
124124
throw FirebaseCommonAIException.from(e)
125125
}
126126

127-
suspend fun generateImage(request: GenerateImageRequest): GenerateImageResponse =
127+
suspend fun generateImage(request: GenerateImageRequest): ImagenGenerationResponse.Internal =
128128
try {
129129
client
130130
.post("${requestOptions.endpoint}/${requestOptions.apiVersion}/$model:predict") {
131131
applyCommonConfiguration(request)
132132
applyHeaderProvider()
133133
}
134134
.also { validateResponse(it) }
135-
.body<GenerateImageResponse>()
135+
.body<ImagenGenerationResponse.Internal>()
136136
} catch (e: Throwable) {
137137
throw FirebaseCommonAIException.from(e)
138138
}
@@ -315,4 +315,4 @@ private fun GenerateContentResponse.Internal.validate() = apply {
315315
?.mapNotNull { it.finishReason }
316316
?.firstOrNull { it != FinishReason.Internal.STOP }
317317
?.let { throw ResponseStoppedException(this) }
318-
}
318+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.common
18+
19+
import android.util.Log
20+
import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
21+
import com.google.firebase.auth.internal.InternalAuthProvider
22+
import kotlinx.coroutines.tasks.await
23+
import kotlin.time.Duration
24+
import kotlin.time.Duration.Companion.seconds
25+
26+
internal class AppCheckHeaderProvider(
27+
private val logTag: String,
28+
private val appCheckTokenProvider: InteropAppCheckTokenProvider? = null,
29+
private val internalAuthProvider: InternalAuthProvider? = null,
30+
) : HeaderProvider {
31+
override val timeout: Duration
32+
get() = 10.seconds
33+
34+
override suspend fun generateHeaders(): Map<String, String> {
35+
val headers = mutableMapOf<String, String>()
36+
if (appCheckTokenProvider == null) {
37+
Log.w(logTag, "AppCheck not registered, skipping")
38+
} else {
39+
val token = appCheckTokenProvider.getToken(false).await()
40+
41+
if (token.error != null) {
42+
Log.w(logTag, "Error obtaining AppCheck token", token.error)
43+
}
44+
// The Firebase App Check backend can differentiate between apps without App Check, and
45+
// wrongly configured apps by verifying the value of the token, so it always needs to be
46+
// included.
47+
headers["X-Firebase-AppCheck"] = token.token
48+
}
49+
50+
if (internalAuthProvider == null) {
51+
Log.w(logTag, "Auth not registered, skipping")
52+
} else {
53+
try {
54+
val token = internalAuthProvider.getAccessToken(false).await()
55+
56+
headers["Authorization"] = "Firebase ${token.token!!}"
57+
} catch (e: Exception) {
58+
Log.w(logTag, "Error getting Auth token ", e)
59+
}
60+
}
61+
62+
return headers
63+
}
64+
}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/Request.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.google.firebase.vertexai.common
1919
import com.google.firebase.vertexai.common.util.fullModelName
2020
import com.google.firebase.vertexai.type.Content
2121
import com.google.firebase.vertexai.type.GenerationConfig
22+
import com.google.firebase.vertexai.type.ImagenImageFormat
2223
import com.google.firebase.vertexai.type.SafetySetting
2324
import com.google.firebase.vertexai.type.Tool
2425
import com.google.firebase.vertexai.type.ToolConfig
@@ -65,3 +66,24 @@ internal data class CountTokensRequest(
6566
)
6667
}
6768
}
69+
70+
@Serializable
71+
internal data class GenerateImageRequest(
72+
val instances: List<ImagenPromptInstance>,
73+
val parameters: ImagenParameters,
74+
) : Request {}
75+
76+
@Serializable internal data class ImagenPromptInstance(val prompt: String)
77+
78+
@Serializable
79+
internal data class ImagenParameters(
80+
val sampleCount: Int = 1,
81+
val includeRaiReason: Boolean = true,
82+
val storageUri: String?,
83+
val negativePrompt: String?,
84+
val aspectRatio: String?,
85+
val safetySetting: String?,
86+
val personGeneration: String?,
87+
val addWatermark: Boolean?,
88+
val imageOutputOptions: ImagenImageFormat.Internal?,
89+
)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/GenerateImageRequest.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/GenerateImageResponse.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/AppCheckHeaderProvider.kt

Lines changed: 0 additions & 65 deletions
This file was deleted.

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/kotlin.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenGenerationConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public class ImagenGenerationConfig(
3636
/**
3737
* Builder for creating a [ImagenGenerationConfig].
3838
*
39-
* This is mainly intended for Java interop. For Kotlin, use [imagenGenerationConfig] for a
40-
* more idiomatic experience.
39+
* This is mainly intended for Java interop. For Kotlin, use [imagenGenerationConfig] for a more
40+
* idiomatic experience.
4141
*
4242
* @property negativePrompt See [ImagenGenerationConfig.negativePrompt].
4343
* @property numberOfImages See [ImagenGenerationConfig.numberOfImages].

0 commit comments

Comments
 (0)