Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / ProviderKeys.swift
| 1 | import Foundation |
| 2 | import Security |
| 3 | |
| 4 | enum ProviderKey: String, CaseIterable, Identifiable { |
| 5 | case anthropic, openai, gemini, apify, x |
| 6 | |
| 7 | var id: Self { self } |
| 8 | |
| 9 | var name: String { |
| 10 | switch self { |
| 11 | case .anthropic: "Anthropic" |
| 12 | case .openai: "OpenAI" |
| 13 | case .gemini: "Gemini" |
| 14 | case .apify: "Apify" |
| 15 | case .x: "X Bearer Token" |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func currentModelID(_ id: String) -> String { |
| 20 | self == .anthropic && id == "claude-fable-5-1" ? "claude-fable-5-2" : id |
| 21 | } |
| 22 | |
| 23 | var managementURL: URL { |
| 24 | let address = switch self { |
| 25 | case .anthropic: "https://console.anthropic.com/settings/keys" |
| 26 | case .openai: "https://platform.openai.com/api-keys" |
| 27 | case .gemini: "https://aistudio.google.com/api-keys" |
| 28 | case .apify: "https://console.apify.com/settings/integrations" |
| 29 | case .x: "https://console.x.com/" |
| 30 | } |
| 31 | return URL(string: address)! |
| 32 | } |
| 33 | |
| 34 | private var query: [String: Any] { |
| 35 | [ |
| 36 | kSecClass as String: kSecClassGenericPassword, |
| 37 | kSecAttrService as String: "com.protomota.inkspring.native.providers", |
| 38 | kSecAttrAccount as String: rawValue |
| 39 | ] |
| 40 | } |
| 41 | |
| 42 | var isSaved: Bool { |
| 43 | SecItemCopyMatching(query as CFDictionary, nil) == errSecSuccess |
| 44 | } |
| 45 | |
| 46 | func read() throws -> String { |
| 47 | var query = query |
| 48 | query[kSecReturnData as String] = true |
| 49 | var result: CFTypeRef? |
| 50 | let status = SecItemCopyMatching(query as CFDictionary, &result) |
| 51 | if status == errSecItemNotFound { throw WorkspaceError("Add your \(name) key in Settings.") } |
| 52 | try check(status) |
| 53 | guard let data = result as? Data, let value = String(data: data, encoding: .utf8), !value.isEmpty else { |
| 54 | throw WorkspaceError("Could not read the \(name) key.") |
| 55 | } |
| 56 | return value |
| 57 | } |
| 58 | |
| 59 | func save(_ value: String, replaceExisting: Bool = true) throws { |
| 60 | let key = value.trimmingCharacters(in: .whitespacesAndNewlines) |
| 61 | guard !key.isEmpty, key.utf8.count < 8192, |
| 62 | !key.unicodeScalars.contains(where: CharacterSet.controlCharacters.contains) else { |
| 63 | throw WorkspaceError("Enter a valid \(name) key.") |
| 64 | } |
| 65 | let data = Data(key.utf8) |
| 66 | let status = replaceExisting |
| 67 | ? SecItemUpdate(query as CFDictionary, [kSecValueData as String: data] as CFDictionary) |
| 68 | : errSecItemNotFound |
| 69 | if status == errSecItemNotFound { |
| 70 | var attributes = query |
| 71 | attributes[kSecValueData as String] = data |
| 72 | let added = SecItemAdd(attributes as CFDictionary, nil) |
| 73 | if !replaceExisting, added == errSecDuplicateItem { |
| 74 | throw WorkspaceError("A \(name) key is already saved. Request a replacement to change it.") |
| 75 | } |
| 76 | try check(added) |
| 77 | } else { |
| 78 | try check(status) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func remove() throws { |
| 83 | let status = SecItemDelete(query as CFDictionary) |
| 84 | if status != errSecItemNotFound { try check(status) } |
| 85 | } |
| 86 | |
| 87 | private func check(_ status: OSStatus) throws { |
| 88 | guard status == errSecSuccess else { |
| 89 | throw WorkspaceError("Keychain couldn’t update \(name) (\(status)).") |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | struct ProviderCatalog: Decodable { |
| 95 | struct Model: Decodable, Identifiable { |
| 96 | let id: String |
| 97 | let name: String |
| 98 | } |
| 99 | |
| 100 | let defaults: [String: String] |
| 101 | let anthropic: [Model] |
| 102 | let openai: [Model] |
| 103 | let gemini: [Model] |
| 104 | let openaiImage: [Model] |
| 105 | let geminiImage: [Model] |
| 106 | |
| 107 | func writingModels(for provider: ProviderKey) -> [Model] { |
| 108 | switch provider { |
| 109 | case .anthropic: anthropic |
| 110 | case .openai: openai |
| 111 | case .gemini: gemini |
| 112 | default: [] |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static var bundled: Self? { |
| 117 | guard let url = Bundle.main.url(forResource: "models", withExtension: "json"), |
| 118 | let data = try? Data(contentsOf: url) else { return nil } |
| 119 | return try? JSONDecoder().decode(Self.self, from: data) |
| 120 | } |
| 121 | } |