Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/254558_trans.json
T

1 line
17 KiB
JSON

{"repo": "254558/trans", "files": {"trans/Services/AppConfig.swift": {"62f2c355e3b5eaf39c9dfd4c5db7e520dc2b50ee": {"t": 1785745884.0395963, "n": 3093, "h": "cb7cb4ef902dc02a", "c": "import Foundation\nimport Security\n\n// MARK: - Keychain Manager\n\nenum KeychainManager {\n static func save(key: String, value: String) throws {\n let data = Data(value.utf8)\n let query: [String: Any] = [\n kSecClass as String: kSecClassGenericPassword,\n kSecAttrAccount as String: key,\n kSecAttrService as String: \"com.zh.hi.trans\",\n kSecValueData as String: data,\n kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock\n ]\n SecItemDelete(query as CFDictionary)\n let status = SecItemAdd(query as CFDictionary, nil)\n guard status == errSecSuccess else {\n throw KeychainError.writeFailed(status)\n }\n }\n\n static func read(key: String) throws -> String? {\n let query: [String: Any] = [\n kSecClass as String: kSecClassGenericPassword,\n kSecAttrAccount as String: key,\n kSecAttrService as String: \"com.zh.hi.trans\",\n kSecReturnData as String: true,\n kSecMatchLimit as String: kSecMatchLimitOne\n ]\n var result: AnyObject?\n let status = SecItemCopyMatching(query as CFDictionary, &result)\n if status == errSecItemNotFound { return nil }\n guard status == errSecSuccess, let data = result as? Data else {\n throw KeychainError.readFailed(status)\n }\n return String(data: data, encoding: .utf8)\n }\n\n static func delete(key: String) throws {\n let query: [String: Any] = [\n kSecClass as String: kSecClassGenericPassword,\n kSecAttrAccount as String: key,\n kSecAttrService as String: \"com.zh.hi.trans\"\n ]\n let status = SecItemDelete(query as CFDictionary)\n guard status == errSecSuccess || status == errSecItemNotFound else {\n throw KeychainError.deleteFailed(status)\n }\n }\n\n enum KeychainError: LocalizedError {\n case writeFailed(OSStatus)\n case readFailed(OSStatus)\n case deleteFailed(OSStatus)\n\n var errorDescription: String? {\n switch self {\n case .writeFailed(let s): return \"Keychain 写入失败 (\\(s))\"\n case .readFailed(let s): return \"Keychain 读取失败 (\\(s))\"\n case .deleteFailed(let s): return \"Keychain 删除失败 (\\(s))\"\n }\n }\n }\n}\n\n// MARK: - App Config\n\nenum AppConfig {\n static var isAutoCopyEnabled: Bool {\n get { UserDefaults.standard.bool(forKey: \"autoCopy\") }\n set { UserDefaults.standard.set(newValue, forKey: \"autoCopy\") }\n }\n\n static var apiKey: String {\n get throws {\n if let key = try KeychainManager.read(key: \"deepseek_api_key\"), !key.isEmpty {\n return key\n }\n throw TranslationError.missingAPIKey\n }\n }\n\n static func saveAPIKey(_ key: String) throws {\n try KeychainManager.save(key: \"deepseek_api_key\", value: key)\n }\n\n static var hasAPIKey: Bool {\n (try? KeychainManager.read(key: \"deepseek_api_key\")).flatMap { $0.isEmpty == false } ?? false\n }\n}\n"}, "c7a0eccda704cdfc3731c5db176dbcc623728796": {"t": 1785757609.3532608, "n": 3093, "h": "cb7cb4ef902dc02a", "c": "import Foundation\nimport Security\n\n// MARK: - Keychain Manager\n\nenum KeychainManager {\n static func save(key: String, value: String) throws {\n let data = Data(value.utf8)\n let query: [String: Any] = [\n kSecClass as String: kSecClassGenericPassword,\n kSecAttrAccount as String: key,\n kSecAttrService as String: \"com.zh.hi.trans\",\n kSecValueData as String: data,\n kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock\n ]\n SecItemDelete(query as CFDictionary)\n let status = SecItemAdd(query as CFDictionary, nil)\n guard status == errSecSuccess else {\n throw KeychainError.writeFailed(status)\n }\n }\n\n static func read(key: String) throws -> String? {\n let query: [String: Any] = [\n kSecClass as String: kSecClassGenericPassword,\n kSecAttrAccount as String: key,\n kSecAttrService as String: \"com.zh.hi.trans\",\n kSecReturnData as String: true,\n kSecMatchLimit as String: kSecMatchLimitOne\n ]\n var result: AnyObject?\n let status = SecItemCopyMatching(query as CFDictionary, &result)\n if status == errSecItemNotFound { return nil }\n guard status == errSecSuccess, let data = result as? Data else {\n throw KeychainError.readFailed(status)\n }\n return String(data: data, encoding: .utf8)\n }\n\n static func delete(key: String) throws {\n let query: [String: Any] = [\n kSecClass as String: kSecClassGenericPassword,\n kSecAttrAccount as String: key,\n kSecAttrService as String: \"com.zh.hi.trans\"\n ]\n let status = SecItemDelete(query as CFDictionary)\n guard status == errSecSuccess || status == errSecItemNotFound else {\n throw KeychainError.deleteFailed(status)\n }\n }\n\n enum KeychainError: LocalizedError {\n case writeFailed(OSStatus)\n case readFailed(OSStatus)\n case deleteFailed(OSStatus)\n\n var errorDescription: String? {\n switch self {\n case .writeFailed(let s): return \"Keychain 写入失败 (\\(s))\"\n case .readFailed(let s): return \"Keychain 读取失败 (\\(s))\"\n case .deleteFailed(let s): return \"Keychain 删除失败 (\\(s))\"\n }\n }\n }\n}\n\n// MARK: - App Config\n\nenum AppConfig {\n static var isAutoCopyEnabled: Bool {\n get { UserDefaults.standard.bool(forKey: \"autoCopy\") }\n set { UserDefaults.standard.set(newValue, forKey: \"autoCopy\") }\n }\n\n static var apiKey: String {\n get throws {\n if let key = try KeychainManager.read(key: \"deepseek_api_key\"), !key.isEmpty {\n return key\n }\n throw TranslationError.missingAPIKey\n }\n }\n\n static func saveAPIKey(_ key: String) throws {\n try KeychainManager.save(key: \"deepseek_api_key\", value: key)\n }\n\n static var hasAPIKey: Bool {\n (try? KeychainManager.read(key: \"deepseek_api_key\")).flatMap { $0.isEmpty == false } ?? false\n }\n}\n"}}, "trans/UI/SettingsView.swift": {"62f2c355e3b5eaf39c9dfd4c5db7e520dc2b50ee": {"t": 1785745888.5511508, "n": 4669, "h": "2779112cb776f9d4", "c": "import SwiftUI\n\nstruct SettingsView: View {\n @EnvironmentObject var appState: AppStateManager\n @State private var apiKey: String = \"\"\n @State private var autoCopy: Bool = true\n @State private var saveStatus: String?\n @State private var isSaveError = false\n\n var body: some View {\n Form {\n Section(\"DeepSeek API 配置\") {\n SecureField(\"API Key\", text: $apiKey)\n .textFieldStyle(.roundedBorder)\n\n HStack {\n Button(\"保存\") { save() }\n .buttonStyle(.borderedProminent)\n .disabled(apiKey.trimmingCharacters(in: .whitespaces).isEmpty)\n\n if let status = saveStatus {\n Text(status)\n .font(.caption)\n .foregroundColor(isSaveError ? .red : .green)\n }\n }\n }\n\n Section(\"偏好设置\") {\n Toggle(\"自动复制翻译结果\", isOn: $autoCopy)\n }\n\n Section(\"快捷键\") {\n HStack {\n Text(\"翻译选中文本\")\n Spacer()\n Text(\"⌥ + Z\")\n .font(.body.monospaced())\n .padding(.horizontal, 8)\n .padding(.vertical, 4)\n .background(Color.secondary.opacity(0.1))\n .clipShape(RoundedRectangle(cornerRadius: 4))\n }\n }\n\n Section(\"所需权限\") {\n Group {\n PermissionRow(\n title: \"输入监控\",\n description: \"检测 ⌥ + Z 快捷键\",\n granted: appState.isInputMonitoringGranted,\n settingsURL: \"x-apple.systempreferences:com.apple.preference.security?Privacy_InputMonitoring\"\n )\n PermissionRow(\n title: \"辅助功能\",\n description: \"读取选中文本\",\n granted: appState.isAccessibilityTrusted,\n settingsURL: \"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility\"\n )\n }\n if !appState.isInputMonitoringGranted {\n Text(\"授权后在设置页面点击 ⌥ + Z 测试\")\n .font(.caption)\n .foregroundColor(.secondary)\n }\n Button(\"检查权限状态\") {\n appState.refreshPermissions()\n }\n .buttonStyle(.link)\n .font(.caption)\n }\n\n Section(\"关于\") {\n HStack {\n Text(\"版本\")\n Spacer()\n Text(\"1.0.0\")\n .foregroundColor(.secondary)\n }\n }\n }\n .formStyle(.grouped)\n .frame(width: 460, height: 440)\n .onAppear {\n if let key = try? KeychainManager.read(key: \"deepseek_api_key\") {\n apiKey = key\n }\n autoCopy = AppConfig.isAutoCopyEnabled\n }\n .onChange(of: autoCopy) { newValue in\n AppConfig.isAutoCopyEnabled = newValue\n }\n }\n\n private func save() {\n let key = apiKey.trimmingCharacters(in: .whitespaces)\n guard !key.isEmpty else { return }\n do {\n try AppConfig.saveAPIKey(key)\n saveStatus = \"已保存\"\n isSaveError = false\n } catch {\n saveStatus = \"保存失败: \\(error.localizedDescription)\"\n isSaveError = true\n }\n DispatchQueue.main.asyncAfter(deadline: .now() + 2) {\n saveStatus = nil\n }\n }\n}\n\nstruct PermissionRow: View {\n let title: String\n let description: String\n let granted: Bool\n let settingsURL: String\n\n var body: some View {\n HStack {\n VStack(alignment: .leading, spacing: 2) {\n Text(title)\n .font(.subheadline)\n Text(description)\n .font(.caption)\n .foregroundColor(.secondary)\n }\n Spacer()\n if granted {\n Label(\"已授权\", systemImage: \"checkmark.circle.fill\")\n .foregroundColor(.green)\n .font(.caption)\n } else {\n Button(\"去设置\") {\n if let url = URL(string: settingsURL) {\n NSWorkspace.shared.open(url)\n }\n }\n .buttonStyle(.link)\n .font(.caption)\n }\n }\n }\n}\n"}, "c7a0eccda704cdfc3731c5db176dbcc623728796": {"t": 1785757610.1272116, "n": 4669, "h": "2779112cb776f9d4", "c": "import SwiftUI\n\nstruct SettingsView: View {\n @EnvironmentObject var appState: AppStateManager\n @State private var apiKey: String = \"\"\n @State private var autoCopy: Bool = true\n @State private var saveStatus: String?\n @State private var isSaveError = false\n\n var body: some View {\n Form {\n Section(\"DeepSeek API 配置\") {\n SecureField(\"API Key\", text: $apiKey)\n .textFieldStyle(.roundedBorder)\n\n HStack {\n Button(\"保存\") { save() }\n .buttonStyle(.borderedProminent)\n .disabled(apiKey.trimmingCharacters(in: .whitespaces).isEmpty)\n\n if let status = saveStatus {\n Text(status)\n .font(.caption)\n .foregroundColor(isSaveError ? .red : .green)\n }\n }\n }\n\n Section(\"偏好设置\") {\n Toggle(\"自动复制翻译结果\", isOn: $autoCopy)\n }\n\n Section(\"快捷键\") {\n HStack {\n Text(\"翻译选中文本\")\n Spacer()\n Text(\"⌥ + Z\")\n .font(.body.monospaced())\n .padding(.horizontal, 8)\n .padding(.vertical, 4)\n .background(Color.secondary.opacity(0.1))\n .clipShape(RoundedRectangle(cornerRadius: 4))\n }\n }\n\n Section(\"所需权限\") {\n Group {\n PermissionRow(\n title: \"输入监控\",\n description: \"检测 ⌥ + Z 快捷键\",\n granted: appState.isInputMonitoringGranted,\n settingsURL: \"x-apple.systempreferences:com.apple.preference.security?Privacy_InputMonitoring\"\n )\n PermissionRow(\n title: \"辅助功能\",\n description: \"读取选中文本\",\n granted: appState.isAccessibilityTrusted,\n settingsURL: \"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility\"\n )\n }\n if !appState.isInputMonitoringGranted {\n Text(\"授权后在设置页面点击 ⌥ + Z 测试\")\n .font(.caption)\n .foregroundColor(.secondary)\n }\n Button(\"检查权限状态\") {\n appState.refreshPermissions()\n }\n .buttonStyle(.link)\n .font(.caption)\n }\n\n Section(\"关于\") {\n HStack {\n Text(\"版本\")\n Spacer()\n Text(\"1.0.0\")\n .foregroundColor(.secondary)\n }\n }\n }\n .formStyle(.grouped)\n .frame(width: 460, height: 440)\n .onAppear {\n if let key = try? KeychainManager.read(key: \"deepseek_api_key\") {\n apiKey = key\n }\n autoCopy = AppConfig.isAutoCopyEnabled\n }\n .onChange(of: autoCopy) { newValue in\n AppConfig.isAutoCopyEnabled = newValue\n }\n }\n\n private func save() {\n let key = apiKey.trimmingCharacters(in: .whitespaces)\n guard !key.isEmpty else { return }\n do {\n try AppConfig.saveAPIKey(key)\n saveStatus = \"已保存\"\n isSaveError = false\n } catch {\n saveStatus = \"保存失败: \\(error.localizedDescription)\"\n isSaveError = true\n }\n DispatchQueue.main.asyncAfter(deadline: .now() + 2) {\n saveStatus = nil\n }\n }\n}\n\nstruct PermissionRow: View {\n let title: String\n let description: String\n let granted: Bool\n let settingsURL: String\n\n var body: some View {\n HStack {\n VStack(alignment: .leading, spacing: 2) {\n Text(title)\n .font(.subheadline)\n Text(description)\n .font(.caption)\n .foregroundColor(.secondary)\n }\n Spacer()\n if granted {\n Label(\"已授权\", systemImage: \"checkmark.circle.fill\")\n .foregroundColor(.green)\n .font(.caption)\n } else {\n Button(\"去设置\") {\n if let url = URL(string: settingsURL) {\n NSWorkspace.shared.open(url)\n }\n }\n .buttonStyle(.link)\n .font(.caption)\n }\n }\n }\n}\n"}}}}