Skip to content

Commit 2ce2724

Browse files
committed
Migrate location to apiConfig
1 parent a935ee3 commit 2ce2724

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

FirebaseAI/Sources/FirebaseAI.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public final class FirebaseAI: Sendable {
4747
useLimitedUseAppCheckTokens: Bool = false) -> FirebaseAI {
4848
let instance = createInstance(
4949
app: app,
50-
location: backend.location,
5150
apiConfig: backend.apiConfig,
5251
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
5352
)
@@ -188,21 +187,19 @@ public final class FirebaseAI: Sendable {
188187

189188
let apiConfig: APIConfig
190189

191-
/// A map of active `FirebaseAI` instances keyed by the `FirebaseApp` name and the `location`,
192-
/// in the format `appName:location`.
190+
/// A map of active `FirebaseAI` instances keyed by the `FirebaseApp`, the `APIConfig`, and `useLimitedUseAppCheckTokens`.
193191
private nonisolated(unsafe) static var instances: [InstanceKey: FirebaseAI] = [:]
194192

195193
/// Lock to manage access to the `instances` array to avoid race conditions.
196194
private nonisolated(unsafe) static var instancesLock: os_unfair_lock = .init()
197195

198-
let location: String?
199-
200196
static let defaultVertexAIAPIConfig = APIConfig(
201197
service: .vertexAI(endpoint: .firebaseProxyProd),
202-
version: .v1beta
198+
version: .v1beta,
199+
location: "us-central1"
203200
)
204201

205-
static func createInstance(app: FirebaseApp?, location: String?,
202+
static func createInstance(app: FirebaseApp?,
206203
apiConfig: APIConfig,
207204
useLimitedUseAppCheckTokens: Bool) -> FirebaseAI {
208205
guard let app = app ?? FirebaseApp.app() else {
@@ -216,7 +213,6 @@ public final class FirebaseAI: Sendable {
216213

217214
let instanceKey = InstanceKey(
218215
appName: app.name,
219-
location: location,
220216
apiConfig: apiConfig,
221217
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
222218
)
@@ -225,15 +221,14 @@ public final class FirebaseAI: Sendable {
225221
}
226222
let newInstance = FirebaseAI(
227223
app: app,
228-
location: location,
229224
apiConfig: apiConfig,
230225
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
231226
)
232227
instances[instanceKey] = newInstance
233228
return newInstance
234229
}
235230

236-
init(app: FirebaseApp, location: String?, apiConfig: APIConfig,
231+
init(app: FirebaseApp, apiConfig: APIConfig,
237232
useLimitedUseAppCheckTokens: Bool) {
238233
guard let projectID = app.options.projectID else {
239234
fatalError("The Firebase app named \"\(app.name)\" has no project ID in its configuration.")
@@ -254,7 +249,6 @@ public final class FirebaseAI: Sendable {
254249
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
255250
)
256251
self.apiConfig = apiConfig
257-
self.location = location
258252
}
259253

260254
func modelResourceName(modelName: String) -> String {
@@ -276,7 +270,7 @@ public final class FirebaseAI: Sendable {
276270
}
277271

278272
private func vertexAIModelResourceName(modelName: String) -> String {
279-
guard let location else {
273+
guard let location = apiConfig.location else {
280274
fatalError("Location must be specified for the Firebase AI service.")
281275
}
282276
guard !location.isEmpty && location
@@ -307,7 +301,6 @@ public final class FirebaseAI: Sendable {
307301
/// This type is `Hashable` so that it can be used as a key in the `instances` dictionary.
308302
private struct InstanceKey: Sendable, Hashable {
309303
let appName: String
310-
let location: String?
311304
let apiConfig: APIConfig
312305
let useLimitedUseAppCheckTokens: Bool
313306
}

FirebaseAI/Sources/Types/Internal/APIConfig.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ struct APIConfig: Sendable, Hashable, Encodable {
2222
/// The version of the selected API to use, e.g., "v1".
2323
let version: Version
2424

25+
/// The server location to use, e.g., "us-central1"
26+
let location: String?
27+
2528
/// Initializes an API configuration.
2629
///
2730
/// - Parameters:
2831
/// - service: The API service to use for generative AI.
2932
/// - version: The version of the API to use.
30-
init(service: Service, version: Version) {
33+
/// - location: The server location to use.
34+
init(service: Service, version: Version, location: String?) {
3135
self.service = service
3236
self.version = version
37+
self.location = location
3338
}
3439
}
3540

FirebaseAI/Sources/Types/Public/Backend.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,30 @@ public struct Backend {
2525
/// for a list of supported locations.
2626
public static func vertexAI(location: String = "us-central1") -> Backend {
2727
return Backend(
28-
apiConfig: APIConfig(service: .vertexAI(endpoint: .firebaseProxyProd), version: .v1beta),
29-
location: location
28+
apiConfig: APIConfig(
29+
service: .vertexAI(endpoint: .firebaseProxyProd),
30+
version: .v1beta,
31+
location: location
32+
)
3033
)
3134
}
3235

3336
/// Initializes a `Backend` configured for the Google Developer API.
3437
public static func googleAI() -> Backend {
3538
return Backend(
36-
apiConfig: APIConfig(service: .googleAI(endpoint: .firebaseProxyProd), version: .v1beta),
37-
location: nil
39+
apiConfig: APIConfig(
40+
service: .googleAI(endpoint: .firebaseProxyProd),
41+
version: .v1beta,
42+
location: nil
43+
)
3844
)
3945
}
4046

4147
// MARK: - Internal
4248

4349
let apiConfig: APIConfig
44-
let location: String?
4550

46-
init(apiConfig: APIConfig, location: String?) {
51+
init(apiConfig: APIConfig) {
4752
self.apiConfig = apiConfig
48-
self.location = location
4953
}
5054
}

0 commit comments

Comments
 (0)