Skip to content

DeviceChatStorage

DeviceChatStorage is the bundled on-device store for iOS 17+ and macOS 14+. It backs history with a SwiftData SQLite store under Library/Caches/AgentSquad — never backed up, reclaimable by the OS under disk pressure. Use FileChatStorage on iOS 16.

@available(iOS 17, macOS 14, *)
public actor DeviceChatStorage: ChatStorage, ModelActor {
public init(userId: String, baseURL: URL? = nil, inMemory: Bool = false) throws
}
ParameterDefaultNotes
userIdRequired. Bound at init. The per-call userId on fetch / save / saveMessages / fetchAllChats is asserted to match.
baseURLLibrary/Caches/AgentSquadOverride the SQLite store directory.
inMemoryfalsetrue uses an ephemeral SwiftData store — for tests.

Production:

let store = try DeviceChatStorage(userId: currentUser.id)
let orchestrator = Orchestrator(agents: [agent], store: store)

Tests — ephemeral, no disk I/O:

let store = try DeviceChatStorage(userId: "test-user", inMemory: true)
public func clear() throws

clear() deletes all rows for the bound userId. Call it on logout or account switch so the next user cannot read the previous session’s history.

try await store.clear()
  • Bound userId. The store is created for one user. Passing a different userId at call time triggers an assert failure in debug builds and silently uses the bound id in release. Create a new instance when the user changes.
  • Self-healing. A partial WAL/SHM purge (common when Library/Caches is evicted mid-write) would make ModelContainer throw forever. DeviceChatStorage detects this and deletes the corrupt store once, re-creating it from scratch. History is lost in that scenario, but the app recovers automatically.
  • Row-level writes. Unlike FileChatStorage, each save inserts individual rows. This scales better with large message budgets.
  • [agentId] attribution. fetchAllChats prefixes assistant messages with [agentId], giving the Classifier correct routing attribution in multi-agent sessions.

See also: Storage overview · InMemoryChatStorage · FileChatStorage · Custom store