ModalityTokenCount.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2025 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 Foundation
  15. /// Represents token counting info for a single modality.
  16. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  17. public struct ModalityTokenCount: Sendable {
  18. /// The modality associated with this token count.
  19. public let modality: ContentModality
  20. /// The number of tokens counted.
  21. public let tokenCount: Int
  22. }
  23. /// Content part modality.
  24. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  25. public struct ContentModality: DecodableProtoEnum, Hashable, Sendable {
  26. enum Kind: String {
  27. case text = "TEXT"
  28. case image = "IMAGE"
  29. case video = "VIDEO"
  30. case audio = "AUDIO"
  31. case document = "DOCUMENT"
  32. }
  33. /// Plain text.
  34. public static let text = ContentModality(kind: .text)
  35. /// Image.
  36. public static let image = ContentModality(kind: .image)
  37. /// Video.
  38. public static let video = ContentModality(kind: .video)
  39. /// Audio.
  40. public static let audio = ContentModality(kind: .audio)
  41. /// Document, e.g. PDF.
  42. public static let document = ContentModality(kind: .document)
  43. /// Returns the raw string representation of the `ContentModality` value.
  44. public let rawValue: String
  45. static let unrecognizedValueMessageCode =
  46. VertexLog.MessageCode.generateContentResponseUnrecognizedContentModality
  47. }
  48. // MARK: Codable Conformances
  49. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  50. extension ModalityTokenCount: Decodable {
  51. enum CodingKeys: CodingKey {
  52. case modality
  53. case tokenCount
  54. }
  55. public init(from decoder: any Decoder) throws {
  56. let container = try decoder.container(keyedBy: CodingKeys.self)
  57. modality = try container.decode(ContentModality.self, forKey: .modality)
  58. tokenCount = try container.decodeIfPresent(Int.self, forKey: .tokenCount) ?? 0
  59. }
  60. }