|
| 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 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +/// Context of a single URL retrieval. |
| 18 | +@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
| 19 | +public struct URLMetadata: Sendable, Hashable { |
| 20 | + /// Status of the URL retrieval. |
| 21 | + public struct URLRetrievalStatus: DecodableProtoEnum, Hashable { |
| 22 | + enum Kind: String { |
| 23 | + case unspecified = "URL_RETRIEVAL_STATUS_UNSPECIFIED" |
| 24 | + case success = "URL_RETRIEVAL_STATUS_SUCCESS" |
| 25 | + case error = "URL_RETRIEVAL_STATUS_ERROR" |
| 26 | + case paywall = "URL_RETRIEVAL_STATUS_PAYWALL" |
| 27 | + case unsafe = "URL_RETRIEVAL_STATUS_UNSAFE" |
| 28 | + } |
| 29 | + |
| 30 | + /// Internal only - default value. |
| 31 | + static let unspecified = URLRetrievalStatus(kind: .unspecified) |
| 32 | + |
| 33 | + /// URL retrieval succeeded. |
| 34 | + public static let success = URLRetrievalStatus(kind: .success) |
| 35 | + |
| 36 | + /// URL retrieval failed due to an error. |
| 37 | + public static let error = URLRetrievalStatus(kind: .error) |
| 38 | + |
| 39 | + // URL retrieval failed failed because the content is behind paywall. |
| 40 | + public static let paywall = URLRetrievalStatus(kind: .paywall) |
| 41 | + |
| 42 | + // URL retrieval failed because the content is unsafe. |
| 43 | + public static let unsafe = URLRetrievalStatus(kind: .unsafe) |
| 44 | + |
| 45 | + /// Returns the raw string representation of the `URLRetrievalStatus` value. |
| 46 | + public let rawValue: String |
| 47 | + |
| 48 | + static let unrecognizedValueMessageCode = |
| 49 | + AILog.MessageCode.urlMetadataUnrecognizedURLRetrievalStatus |
| 50 | + } |
| 51 | + |
| 52 | + /// The URL retrieved by the ``URLContext`` tool. |
| 53 | + public let retrievedURL: URL? |
| 54 | + |
| 55 | + /// The status of the URL retrieval. |
| 56 | + public let retrievalStatus: URLRetrievalStatus |
| 57 | +} |
| 58 | + |
| 59 | +// MARK: - Codable Conformances |
| 60 | + |
| 61 | +extension URLMetadata: Decodable { |
| 62 | + enum CodingKeys: String, CodingKey { |
| 63 | + case retrievedURL = "retrievedUrl" |
| 64 | + case retrievalStatus = "urlRetrievalStatus" |
| 65 | + } |
| 66 | + |
| 67 | + public init(from decoder: any Decoder) throws { |
| 68 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 69 | + |
| 70 | + if let retrievedURLString = try container.decodeIfPresent(String.self, forKey: .retrievedURL), |
| 71 | + let retrievedURL = URL(string: retrievedURLString) { |
| 72 | + self.retrievedURL = retrievedURL |
| 73 | + } else { |
| 74 | + retrievedURL = nil |
| 75 | + } |
| 76 | + let retrievalStatus = try container.decodeIfPresent( |
| 77 | + URLMetadata.URLRetrievalStatus.self, forKey: .retrievalStatus |
| 78 | + ) |
| 79 | + |
| 80 | + self.retrievalStatus = AILog.safeUnwrap( |
| 81 | + retrievalStatus, fallback: URLMetadata.URLRetrievalStatus(kind: .unspecified) |
| 82 | + ) |
| 83 | + } |
| 84 | +} |
0 commit comments