Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import Contacts
import CryptoKit
import Foundation
/// Reads names for phone numbers and emails from the Mac's Contacts, so the
/// chat list and the notes say "Ana" instead of "Phone ending 0001". Only
/// name, phone and email fields are read, and only after Contacts access is
/// allowed. A refusal leaves the "Phone ending …" labels in place.
public enum ContactNames {
/// What to do with the current Contacts permission. Denied and restricted
/// are `skip`, so a later launch does not ask again.
public enum Step: Equatable, Sendable {
/// The person already allowed Contacts. Read the address book.
case read
/// Show the system prompt once.
case ask
/// Access is off. Do not ask again.
case skip
}
/// Privacy & Security → Contacts.
public static let settingsURL = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Contacts")!
public static func authorization() -> CNAuthorizationStatus {
CNContactStore.authorizationStatus(for: .contacts)
}
public static func step(_ status: CNAuthorizationStatus) -> Step {
switch status {
case .authorized:
return .read
case .notDetermined:
return .ask
case .denied, .restricted:
return .skip
default:
// A newer limited grant still has names to read. Ask, then take
// whatever Contacts shares. requestAccess does not prompt again
// once the person has already answered.
return .ask
}
}
public static func requestAccess() async -> Bool {
let store = CNContactStore()
do {
return try await store.requestAccess(for: .contacts)
} catch {
return false
}
}
/// Address → name for every contact with a phone number or email.
public static func addressBook() throws -> [String: String] {
let store = CNContactStore()
let keys: [CNKeyDescriptor] = [
CNContactGivenNameKey as CNKeyDescriptor,
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactNicknameKey as CNKeyDescriptor,
CNContactOrganizationNameKey as CNKeyDescriptor,
CNContactPhoneNumbersKey as CNKeyDescriptor,
CNContactEmailAddressesKey as CNKeyDescriptor,
]
var names: [String: String] = [:]
try store.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keys)) { contact, _ in
guard let name = displayName(given: contact.givenName, family: contact.familyName,
nickname: contact.nickname, organization: contact.organizationName) else { return }
for phone in contact.phoneNumbers { names[phone.value.stringValue] = name }
for email in contact.emailAddresses { names[(email.value as String).lowercased()] = name }
}
return names
}
/// Stable id for an address book, so an unchanged book is not written again.
public static func digest(_ book: [String: String]) -> String {
let text = book.keys.sorted().map { "\($0)\t\(book[$0] ?? "")" }.joined(separator: "\n")
let hash = SHA256.hash(data: Data(text.utf8))
return hash.map { byte in
let hex = String(byte, radix: 16)
return byte < 16 ? "0" + hex : hex
}.joined()
}
/// "Ana Ruiz", else the nickname, else the company.
public static func displayName(given: String, family: String, nickname: String, organization: String) -> String? {
let full = [given, family].filter { !$0.isEmpty }.joined(separator: " ")
for candidate in [full, nickname, organization] where !candidate.trimmingCharacters(in: .whitespaces).isEmpty {
return candidate.trimmingCharacters(in: .whitespaces)
}
return nil
}
}