VertexAIComponent.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseAppCheckInterop
  15. import FirebaseAuthInterop
  16. import FirebaseCore
  17. import Foundation
  18. // Avoids exposing internal FirebaseCore APIs to Swift users.
  19. @_implementationOnly import FirebaseCoreExtension
  20. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  21. @objc(FIRVertexAIProvider)
  22. protocol VertexAIProvider {
  23. @objc func vertexAI(_ location: String) -> VertexAI
  24. }
  25. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  26. @objc(FIRVertexAIComponent)
  27. class VertexAIComponent: NSObject, Library, VertexAIProvider {
  28. // MARK: - Private Variables
  29. /// The app associated with all `VertexAI` instances in this container.
  30. /// This is `unowned` instead of `weak` so it can be used without unwrapping in `vertexAI(...)`
  31. unowned let app: FirebaseApp
  32. /// A map of active `VertexAI` instances for `app`, keyed by model resource names
  33. /// (e.g., "projects/my-project-id/locations/us-central1/publishers/google/models/gemini-pro").
  34. private var instances: [String: VertexAI] = [:]
  35. /// Lock to manage access to the `instances` array to avoid race conditions.
  36. private var instancesLock: os_unfair_lock = .init()
  37. // MARK: - Initializers
  38. required init(app: FirebaseApp) {
  39. self.app = app
  40. }
  41. // MARK: - Library conformance
  42. static func componentsToRegister() -> [Component] {
  43. let appCheckInterop = Dependency(with: AppCheckInterop.self, isRequired: false)
  44. let authInterop = Dependency(with: AuthInterop.self, isRequired: false)
  45. return [Component(VertexAIProvider.self,
  46. instantiationTiming: .lazy,
  47. dependencies: [
  48. appCheckInterop,
  49. authInterop,
  50. ]) { container, isCacheable in
  51. guard let app = container.app else { return nil }
  52. isCacheable.pointee = true
  53. return self.init(app: app)
  54. }]
  55. }
  56. // MARK: - VertexAIProvider conformance
  57. func vertexAI(_ location: String) -> VertexAI {
  58. os_unfair_lock_lock(&instancesLock)
  59. // Unlock before the function returns.
  60. defer { os_unfair_lock_unlock(&instancesLock) }
  61. if let instance = instances[location] {
  62. return instance
  63. }
  64. let newInstance = VertexAI(app: app, location: location)
  65. instances[location] = newInstance
  66. return newInstance
  67. }
  68. }