From 2fb4a7fe3ded72b6e7ea0b425a1f065ea80be0fd Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Fri, 25 Apr 2025 14:03:47 -0700 Subject: [PATCH] Renames for console logging --- .../Sources/{VertexLog.swift => AILog.swift} | 10 +++---- FirebaseAI/Sources/FirebaseAI.swift | 4 +-- .../Sources/GenerateContentResponse.swift | 12 ++++---- FirebaseAI/Sources/GenerativeAIService.swift | 28 +++++++++---------- FirebaseAI/Sources/GenerativeModel.swift | 10 +++---- FirebaseAI/Sources/ModalityTokenCount.swift | 2 +- .../Protocols/Internal/CodableProtoEnum.swift | 6 ++-- FirebaseAI/Sources/Safety.swift | 6 ++-- .../Sources/Types/Internal/ProtoDate.swift | 6 ++-- .../Imagen/ImagenGenerationResponse.swift | 2 +- .../Public/Imagen/ImagenImageFormat.swift | 2 +- FirebaseAI/Sources/Types/Public/Schema.swift | 2 +- 12 files changed, 45 insertions(+), 45 deletions(-) rename FirebaseAI/Sources/{VertexLog.swift => AILog.swift} (95%) diff --git a/FirebaseAI/Sources/VertexLog.swift b/FirebaseAI/Sources/AILog.swift similarity index 95% rename from FirebaseAI/Sources/VertexLog.swift rename to FirebaseAI/Sources/AILog.swift index b3f5b04be64..4019c2cd0ff 100644 --- a/FirebaseAI/Sources/VertexLog.swift +++ b/FirebaseAI/Sources/AILog.swift @@ -17,8 +17,8 @@ import os.log internal import FirebaseCoreExtension -enum VertexLog { - /// Log message codes for the Vertex AI SDK +enum AILog { + /// Log message codes for the Firebase AI SDK /// /// These codes should ideally not be re-used in order to facilitate matching error codes in /// support requests to lines in the SDK. These codes should range between 0 and 999999 to avoid @@ -75,10 +75,10 @@ enum VertexLog { /// Subsystem that should be used for all Loggers. static let subsystem = "com.google.firebase" - /// Log identifier for the Vertex AI SDK. + /// Log identifier for the AI SDK. /// /// > Note: This corresponds to the `category` in `OSLog`. - static let service = "[FirebaseVertexAI]" + static let service = "[FirebaseAI]" /// The raw `OSLog` log object. /// @@ -92,7 +92,7 @@ enum VertexLog { let messageCode = String(format: "I-VTX%06d", code.rawValue) FirebaseLogger.log( level: level, - service: VertexLog.service, + service: AILog.service, code: messageCode, message: message ) diff --git a/FirebaseAI/Sources/FirebaseAI.swift b/FirebaseAI/Sources/FirebaseAI.swift index 5b7eda7aee7..e3fa0fbc44a 100644 --- a/FirebaseAI/Sources/FirebaseAI.swift +++ b/FirebaseAI/Sources/FirebaseAI.swift @@ -73,7 +73,7 @@ public final class FirebaseAI: Sendable { requestOptions: RequestOptions = RequestOptions()) -> GenerativeModel { if !modelName.starts(with: GenerativeModel.geminiModelNamePrefix) { - VertexLog.warning(code: .unsupportedGeminiModel, """ + AILog.warning(code: .unsupportedGeminiModel, """ Unsupported Gemini model "\(modelName)"; see \ https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names. """) @@ -113,7 +113,7 @@ public final class FirebaseAI: Sendable { safetySettings: ImagenSafetySettings? = nil, requestOptions: RequestOptions = RequestOptions()) -> ImagenModel { if !modelName.starts(with: ImagenModel.imagenModelNamePrefix) { - VertexLog.warning(code: .unsupportedImagenModel, """ + AILog.warning(code: .unsupportedImagenModel, """ Unsupported Imagen model "\(modelName)"; see \ https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names. """) diff --git a/FirebaseAI/Sources/GenerateContentResponse.swift b/FirebaseAI/Sources/GenerateContentResponse.swift index 0ca92040091..8f8026ef376 100644 --- a/FirebaseAI/Sources/GenerateContentResponse.swift +++ b/FirebaseAI/Sources/GenerateContentResponse.swift @@ -49,7 +49,7 @@ public struct GenerateContentResponse: Sendable { /// The response's content as text, if it exists. public var text: String? { guard let candidate = candidates.first else { - VertexLog.error( + AILog.error( code: .generateContentResponseNoCandidates, "Could not get text from a response that had no candidates." ) @@ -64,7 +64,7 @@ public struct GenerateContentResponse: Sendable { } } guard textValues.count > 0 else { - VertexLog.error( + AILog.error( code: .generateContentResponseNoText, "Could not get a text part from the first candidate." ) @@ -91,7 +91,7 @@ public struct GenerateContentResponse: Sendable { /// Returns inline data parts found in any `Part`s of the first candidate of the response, if any. public var inlineDataParts: [InlineDataPart] { guard let candidate = candidates.first else { - VertexLog.error(code: .generateContentResponseNoCandidates, """ + AILog.error(code: .generateContentResponseNoCandidates, """ Could not get inline data parts because the response has no candidates. The accessor only \ checks the first candidate. """) @@ -219,7 +219,7 @@ public struct FinishReason: DecodableProtoEnum, Hashable, Sendable { public let rawValue: String static let unrecognizedValueMessageCode = - VertexLog.MessageCode.generateContentResponseUnrecognizedFinishReason + AILog.MessageCode.generateContentResponseUnrecognizedFinishReason } /// A metadata struct containing any feedback the model had on the prompt it was provided. @@ -254,7 +254,7 @@ public struct PromptFeedback: Sendable { public let rawValue: String static let unrecognizedValueMessageCode = - VertexLog.MessageCode.generateContentResponseUnrecognizedBlockReason + AILog.MessageCode.generateContentResponseUnrecognizedBlockReason } /// The reason a prompt was blocked, if it was blocked. @@ -428,7 +428,7 @@ extension Citation: Decodable { ) { publicationDate = publicationProtoDate.dateComponents if let publicationDate, !publicationDate.isValidDate { - VertexLog.warning( + AILog.warning( code: .decodedInvalidCitationPublicationDate, "Decoded an invalid citation publication date: \(publicationDate)" ) diff --git a/FirebaseAI/Sources/GenerativeAIService.swift b/FirebaseAI/Sources/GenerativeAIService.swift index daa400ce594..e1538af997f 100644 --- a/FirebaseAI/Sources/GenerativeAIService.swift +++ b/FirebaseAI/Sources/GenerativeAIService.swift @@ -50,12 +50,12 @@ struct GenerativeAIService { // Verify the status code is 200 guard response.statusCode == 200 else { - VertexLog.error( + AILog.error( code: .loadRequestResponseError, "The server responded with an error: \(response)" ) if let responseString = String(data: data, encoding: .utf8) { - VertexLog.error( + AILog.error( code: .loadRequestResponseErrorPayload, "Response payload: \(responseString)" ) @@ -104,7 +104,7 @@ struct GenerativeAIService { // Verify the status code is 200 guard response.statusCode == 200 else { - VertexLog.error( + AILog.error( code: .loadRequestStreamResponseError, "The server responded with an error: \(response)" ) @@ -113,7 +113,7 @@ struct GenerativeAIService { responseBody += line + "\n" } - VertexLog.error( + AILog.error( code: .loadRequestStreamResponseErrorPayload, "Response payload: \(responseBody)" ) @@ -128,7 +128,7 @@ struct GenerativeAIService { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase for try await line in stream.lines { - VertexLog.debug(code: .loadRequestStreamResponseLine, "Stream response: \(line)") + AILog.debug(code: .loadRequestStreamResponseLine, "Stream response: \(line)") if line.hasPrefix("data:") { // We can assume 5 characters since it's utf-8 encoded, removing `data:`. @@ -180,7 +180,7 @@ struct GenerativeAIService { let tokenResult = await appCheck.getToken(forcingRefresh: false) urlRequest.setValue(tokenResult.token, forHTTPHeaderField: "X-Firebase-AppCheck") if let error = tokenResult.error { - VertexLog.error( + AILog.error( code: .appCheckTokenFetchFailed, "Failed to fetch AppCheck token. Error: \(error)" ) @@ -212,7 +212,7 @@ struct GenerativeAIService { // response objects you get back from the URLSession, NSURLConnection, or NSURLDownload class // are instances of the HTTPURLResponse class." guard let response = urlResponse as? HTTPURLResponse else { - VertexLog.error( + AILog.error( code: .generativeAIServiceNonHTTPResponse, "Response wasn't an HTTP response, internal error \(urlResponse)" ) @@ -260,8 +260,8 @@ struct GenerativeAIService { private func logRPCError(_ error: BackendError) { let projectID = firebaseInfo.projectID if error.isVertexAIInFirebaseServiceDisabledError() { - VertexLog.error(code: .vertexAIInFirebaseAPIDisabled, """ - The Vertex AI in Firebase SDK requires the Vertex AI in Firebase API \ + AILog.error(code: .vertexAIInFirebaseAPIDisabled, """ + The Firebase AI SDK requires the Firebase AI API \ (`firebasevertexai.googleapis.com`) to be enabled in your Firebase project. Enable this API \ by visiting the Firebase Console at https://console.firebase.google.com/project/\(projectID)/genai/ and clicking "Get started". \ @@ -276,9 +276,9 @@ struct GenerativeAIService { return try JSONDecoder().decode(type, from: data) } catch { if let json = String(data: data, encoding: .utf8) { - VertexLog.error(code: .loadRequestParseResponseFailedJSON, "JSON response: \(json)") + AILog.error(code: .loadRequestParseResponseFailedJSON, "JSON response: \(json)") } - VertexLog.error( + AILog.error( code: .loadRequestParseResponseFailedJSONError, "Error decoding server JSON: \(error)" ) @@ -307,12 +307,12 @@ struct GenerativeAIService { } private func printCURLCommand(from request: URLRequest) { - guard VertexLog.additionalLoggingEnabled() else { + guard AILog.additionalLoggingEnabled() else { return } let command = cURLCommand(from: request) - os_log(.debug, log: VertexLog.logObject, """ - \(VertexLog.service) Creating request with the equivalent cURL command: + os_log(.debug, log: AILog.logObject, """ + \(AILog.service) Creating request with the equivalent cURL command: ----- cURL command ----- \(command, privacy: .private) ------------------------ diff --git a/FirebaseAI/Sources/GenerativeModel.swift b/FirebaseAI/Sources/GenerativeModel.swift index c601c2e1aee..a9ebef87b8b 100644 --- a/FirebaseAI/Sources/GenerativeModel.swift +++ b/FirebaseAI/Sources/GenerativeModel.swift @@ -104,15 +104,15 @@ public final class GenerativeModel: Sendable { } self.requestOptions = requestOptions - if VertexLog.additionalLoggingEnabled() { - VertexLog.debug(code: .verboseLoggingEnabled, "Verbose logging enabled.") + if AILog.additionalLoggingEnabled() { + AILog.debug(code: .verboseLoggingEnabled, "Verbose logging enabled.") } else { - VertexLog.info(code: .verboseLoggingDisabled, """ + AILog.info(code: .verboseLoggingDisabled, """ [FirebaseVertexAI] To enable additional logging, add \ - `\(VertexLog.enableArgumentKey)` as a launch argument in Xcode. + `\(AILog.enableArgumentKey)` as a launch argument in Xcode. """) } - VertexLog.debug(code: .generativeModelInitialized, "Model \(modelResourceName) initialized.") + AILog.debug(code: .generativeModelInitialized, "Model \(modelResourceName) initialized.") } /// Generates content from String and/or image inputs, given to the model as a prompt, that are diff --git a/FirebaseAI/Sources/ModalityTokenCount.swift b/FirebaseAI/Sources/ModalityTokenCount.swift index 8b75242d349..e7e36ece21e 100644 --- a/FirebaseAI/Sources/ModalityTokenCount.swift +++ b/FirebaseAI/Sources/ModalityTokenCount.swift @@ -54,7 +54,7 @@ public struct ContentModality: DecodableProtoEnum, Hashable, Sendable { public let rawValue: String static let unrecognizedValueMessageCode = - VertexLog.MessageCode.generateContentResponseUnrecognizedContentModality + AILog.MessageCode.generateContentResponseUnrecognizedContentModality } // MARK: Codable Conformances diff --git a/FirebaseAI/Sources/Protocols/Internal/CodableProtoEnum.swift b/FirebaseAI/Sources/Protocols/Internal/CodableProtoEnum.swift index 6ea434e37bc..3d17bfe614c 100644 --- a/FirebaseAI/Sources/Protocols/Internal/CodableProtoEnum.swift +++ b/FirebaseAI/Sources/Protocols/Internal/CodableProtoEnum.swift @@ -46,8 +46,8 @@ protocol ProtoEnum: Sendable { /// Protobuf enums are represented as strings in JSON. A default `Decodable` implementation is /// provided when conforming to this type. protocol DecodableProtoEnum: ProtoEnum, Decodable { - /// Returns the ``VertexLog/MessageCode`` associated with unrecognized (unknown) enum values. - static var unrecognizedValueMessageCode: VertexLog.MessageCode { get } + /// Returns the ``AILog/MessageCode`` associated with unrecognized (unknown) enum values. + static var unrecognizedValueMessageCode: AILog.MessageCode { get } /// Creates a new instance by decoding from the given decoder. /// @@ -90,7 +90,7 @@ extension DecodableProtoEnum { self = Self(rawValue: rawValue) if Kind(rawValue: rawValue) == nil { - VertexLog.error( + AILog.error( code: Self.unrecognizedValueMessageCode, """ Unrecognized \(Self.self) with value "\(rawValue)": diff --git a/FirebaseAI/Sources/Safety.swift b/FirebaseAI/Sources/Safety.swift index 7fb93d71b81..a4b28402cd4 100644 --- a/FirebaseAI/Sources/Safety.swift +++ b/FirebaseAI/Sources/Safety.swift @@ -107,7 +107,7 @@ public struct SafetyRating: Equatable, Hashable, Sendable { public let rawValue: String static let unrecognizedValueMessageCode = - VertexLog.MessageCode.generateContentResponseUnrecognizedHarmProbability + AILog.MessageCode.generateContentResponseUnrecognizedHarmProbability } /// The magnitude of how harmful a model response might be for the respective ``HarmCategory``. @@ -139,7 +139,7 @@ public struct SafetyRating: Equatable, Hashable, Sendable { public let rawValue: String static let unrecognizedValueMessageCode = - VertexLog.MessageCode.generateContentResponseUnrecognizedHarmSeverity + AILog.MessageCode.generateContentResponseUnrecognizedHarmSeverity } } @@ -263,7 +263,7 @@ public struct HarmCategory: CodableProtoEnum, Hashable, Sendable { public let rawValue: String static let unrecognizedValueMessageCode = - VertexLog.MessageCode.generateContentResponseUnrecognizedHarmCategory + AILog.MessageCode.generateContentResponseUnrecognizedHarmCategory } // MARK: - Codable Conformances diff --git a/FirebaseAI/Sources/Types/Internal/ProtoDate.swift b/FirebaseAI/Sources/Types/Internal/ProtoDate.swift index fed7e63e943..e6a8accd310 100644 --- a/FirebaseAI/Sources/Types/Internal/ProtoDate.swift +++ b/FirebaseAI/Sources/Types/Internal/ProtoDate.swift @@ -68,7 +68,7 @@ extension ProtoDate: Decodable { let container = try decoder.container(keyedBy: CodingKeys.self) if let year = try container.decodeIfPresent(Int.self, forKey: .year), year != 0 { if year < 0 || year > 9999 { - VertexLog.warning( + AILog.warning( code: .decodedInvalidProtoDateYear, """ Invalid year: \(year); must be from 1 to 9999, or 0 for a date without a specified year. @@ -82,7 +82,7 @@ extension ProtoDate: Decodable { if let month = try container.decodeIfPresent(Int.self, forKey: .month), month != 0 { if month < 0 || month > 12 { - VertexLog.warning( + AILog.warning( code: .decodedInvalidProtoDateMonth, """ Invalid month: \(month); must be from 1 to 12, or 0 for a year date without a specified \ @@ -97,7 +97,7 @@ extension ProtoDate: Decodable { if let day = try container.decodeIfPresent(Int.self, forKey: .day), day != 0 { if day < 0 || day > 31 { - VertexLog.warning( + AILog.warning( code: .decodedInvalidProtoDateDay, "Invalid day: \(day); must be from 1 to 31, or 0 for a date without a specified day." ) diff --git a/FirebaseAI/Sources/Types/Public/Imagen/ImagenGenerationResponse.swift b/FirebaseAI/Sources/Types/Public/Imagen/ImagenGenerationResponse.swift index b2188a053c7..48d14a79872 100644 --- a/FirebaseAI/Sources/Types/Public/Imagen/ImagenGenerationResponse.swift +++ b/FirebaseAI/Sources/Types/Public/Imagen/ImagenGenerationResponse.swift @@ -61,7 +61,7 @@ extension ImagenGenerationResponse: Decodable where T: Decodable { } else if let filteredReason = try? predictionsContainer.decode(RAIFilteredReason.self) { filteredReasons.append(filteredReason.raiFilteredReason) } else if let unsupportedPrediction = try? predictionsContainer.decode(JSONObject.self) { - VertexLog.warning( + AILog.warning( code: .decodedUnsupportedImagenPredictionType, "Ignoring unsupported Imagen prediction: \(unsupportedPrediction)" ) diff --git a/FirebaseAI/Sources/Types/Public/Imagen/ImagenImageFormat.swift b/FirebaseAI/Sources/Types/Public/Imagen/ImagenImageFormat.swift index a2217f0810b..9ae6af3c6ac 100644 --- a/FirebaseAI/Sources/Types/Public/Imagen/ImagenImageFormat.swift +++ b/FirebaseAI/Sources/Types/Public/Imagen/ImagenImageFormat.swift @@ -46,7 +46,7 @@ public struct ImagenImageFormat { /// compression (highest image quality, largest file size); defaults to `75`. public static func jpeg(compressionQuality: Int? = nil) -> ImagenImageFormat { if let compressionQuality, compressionQuality < 0 || compressionQuality > 100 { - VertexLog.warning(code: .imagenInvalidJPEGCompressionQuality, """ + AILog.warning(code: .imagenInvalidJPEGCompressionQuality, """ Invalid JPEG compression quality of \(compressionQuality) specified; the supported range is \ [0, 100]. """) diff --git a/FirebaseAI/Sources/Types/Public/Schema.swift b/FirebaseAI/Sources/Types/Public/Schema.swift index 7ad13667cfe..f5a378a4c11 100644 --- a/FirebaseAI/Sources/Types/Public/Schema.swift +++ b/FirebaseAI/Sources/Types/Public/Schema.swift @@ -435,7 +435,7 @@ public final class Schema: Sendable { /// one of these schemas. The array must not be empty. public static func anyOf(schemas: [Schema]) -> Schema { if schemas.isEmpty { - VertexLog.error(code: .invalidSchemaFormat, "The `anyOf` schemas array cannot be empty.") + AILog.error(code: .invalidSchemaFormat, "The `anyOf` schemas array cannot be empty.") } // Note: The 'type' for an 'anyOf' schema is implicitly defined by the presence of the // 'anyOf' keyword and doesn't have a specific explicit type like "OBJECT" or "STRING".