Files
OSGKeyboard/OSGKeyboardHostSupport/Features/Account/AccountModels.swift
T
rockyandClaude Opus 5 0023741455 feat(app): add crash visibility, memory relief, and clipboard reply work
Squashes the working tree that had accumulated on cloud-agent. Two
strands of work, kept in one commit at the maintainer's request.

Crash and stability (this session):

- Subscribe to MetricKit so crash, hang, CPU, and disk-write diagnostics
  are recorded on device. Keyboard-extension memory kills are the most
  likely way a user loses the keyboard mid-sentence and iOS does not
  classify them as crashes, so they never reach App Store Connect.
- Upload those reports to POST /v1/analytics/diagnostics from internal
  builds only (local Debug and TestFlight), reusing the analytics
  installation identifier and skipping entirely when analytics are off.
  App Store builds keep everything on device.
- Shed the Chinese engine, English lexicon, and in-flight pipeline work
  at the 40 MiB and 48 MiB budget thresholds instead of waiting for a
  system memory warning that jetsam often preempts.
- Install audio taps with the input node's live format through one
  shared adaptive resampler, so an audio-route change mid-recording no
  longer raises an uncatchable format-mismatch exception. Covers the
  dictation preview and the Mac recorder, which still used the pattern
  the Flow capture path had already moved away from.
- Harden the librime bridge: never pass a nil string across a nonnull
  boundary when a user dictionary holds malformed UTF-8, null-check the
  engine API vector, and distrust a candidate count without its array.
- Guard the expanded candidate grid against a list that shrinks while
  the collection view is still laying out against the previous count.
- Resolve localized text without the App Group so the screen shown when
  that container is missing renders its message instead of crashing on
  launch, which is the one situation it exists to handle.
- Gate CI on the App Group and Keychain wiring that the keyboard
  extension hard-depends on, with a self-test proving the check fails.

Clipboard, reply, and semantics (pre-existing working tree):

- Unify the reply center, refresh keyboard AI features, and add native
  event and reminder exporters.
- Expand clipboard semantic routing with authored corpora and a
  product-blind holdout, plus the scripts that build and validate them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:31:29 -07:00

363 lines
11 KiB
Swift

// AccountModels.swift
// OSGKeyboard · HostSupport
//
// Wire models for the host-private OSG account protocol.
import Foundation
#if canImport(OSGKeyboardShared)
import OSGKeyboardShared
#endif
public struct AccountTokenSession: Codable, Equatable, Sendable {
public let accountId: UUID
public let tokenType: String
public let accessToken: String
public let accessTokenExpiresAtEpochSeconds: Int64
public let refreshToken: String
public let refreshTokenExpiresAtEpochSeconds: Int64
public init(
accountId: UUID,
tokenType: String,
accessToken: String,
accessTokenExpiresAtEpochSeconds: Int64,
refreshToken: String,
refreshTokenExpiresAtEpochSeconds: Int64
) {
self.accountId = accountId
self.tokenType = tokenType
self.accessToken = accessToken
self.accessTokenExpiresAtEpochSeconds = accessTokenExpiresAtEpochSeconds
self.refreshToken = refreshToken
self.refreshTokenExpiresAtEpochSeconds = refreshTokenExpiresAtEpochSeconds
}
}
public struct OSGAccount: Codable, Equatable, Sendable {
public let id: UUID
public let createdAtEpochSeconds: Int64
public let displayName: String?
public init(id: UUID, createdAtEpochSeconds: Int64, displayName: String? = nil) {
self.id = id
self.createdAtEpochSeconds = createdAtEpochSeconds
self.displayName = displayName
}
}
struct UpdateAccountProfileRequest: Codable, Equatable, Sendable {
let displayName: String
}
/// Exact authenticated resources exposed to the host account center. Keeping
/// this closed prevents callers from forwarding account tokens to arbitrary
/// paths or origins.
public enum AccountAuthorizedResource: Sendable, Equatable {
case creditsBalance
case referralProfile
case referralCampaigns
case referrals(limit: Int)
case redeemReferral
case storeKitProducts
case storeKitTransactions(limit: Int, cursor: String?)
case submitStoreKitTransaction
case revokeGatewayGrant(UUID)
var method: String {
switch self {
case .redeemReferral, .submitStoreKitTransaction:
return "POST"
case .revokeGatewayGrant:
return "DELETE"
default:
return "GET"
}
}
var path: String {
switch self {
case .creditsBalance:
return "/v1/credits/balance"
case .referralProfile:
return "/v1/referrals/me"
case .referralCampaigns:
return "/v1/referrals/campaigns"
case .referrals(let limit):
return "/v1/referrals?limit=\(min(max(limit, 1), 100))"
case .redeemReferral:
return "/v1/referrals/redeem"
case .storeKitProducts:
return "/v1/storekit/products"
case .storeKitTransactions(let limit, let cursor):
let boundedLimit = min(max(limit, 1), 100)
var components = URLComponents()
components.path = "/v1/storekit/transactions"
components.queryItems = [
URLQueryItem(name: "limit", value: String(boundedLimit))
]
if let cursor {
components.queryItems?.append(
URLQueryItem(name: "cursor", value: cursor)
)
}
return components.string
?? "/v1/storekit/transactions?limit=\(boundedLimit)"
case .submitStoreKitTransaction:
return "/v1/storekit/transactions"
case .revokeGatewayGrant(let grantId):
return "/v1/gateway/grants/\(grantId.uuidString.lowercased())"
}
}
}
public struct AppleSignInCredential: Equatable, Sendable {
public let identityToken: String
public let authorizationCode: String
public init(identityToken: String, authorizationCode: String) {
self.identityToken = identityToken
self.authorizationCode = authorizationCode
}
}
public struct AppleSignInNonce: Equatable, Sendable {
public let rawValue: String
public let sha256Hex: String
public init(rawValue: String, sha256Hex: String) {
self.rawValue = rawValue
self.sha256Hex = sha256Hex
}
}
public struct AppleSignInRequest: Codable, Equatable, Sendable {
public let identityToken: String
public let authorizationCode: String
public let nonce: String
public let displayName: String?
public let deviceCheckToken: String?
public let appAttest: AppAttestAssertion?
public init(
identityToken: String,
authorizationCode: String,
nonce: String,
displayName: String? = nil,
deviceCheckToken: String?,
appAttest: AppAttestAssertion?
) {
self.identityToken = identityToken
self.authorizationCode = authorizationCode
self.nonce = nonce
self.displayName = displayName
self.deviceCheckToken = deviceCheckToken
self.appAttest = appAttest
}
}
public struct AppAttestAssertion: Codable, Equatable, Sendable {
public let keyId: String
public let challengeId: UUID
public let challenge: String
public let assertion: String
public init(keyId: String, challengeId: UUID, challenge: String, assertion: String) {
self.keyId = keyId
self.challengeId = challengeId
self.challenge = challenge
self.assertion = assertion
}
}
/// Anonymous OOBE grant request. It intentionally contains no account session,
/// requested scopes, or mutable feature claim; the server fixes policy to
/// `polish` + `ai` after validating App Attest.
public struct OOBEGrantRequest: Codable, Equatable, Sendable {
public let installationId: UUID
public let keyId: String
public let challengeId: UUID
public let challenge: String
public let assertion: String
public init(
installationId: UUID,
keyId: String,
challengeId: UUID,
challenge: String,
assertion: String
) {
self.installationId = installationId
self.keyId = keyId
self.challengeId = challengeId
self.challenge = challenge
self.assertion = assertion
}
}
public enum AppAttestChallengePurpose: String, Codable, Sendable {
case attestation
case assertion
}
public struct AppAttestChallenge: Codable, Equatable, Sendable {
public let challengeId: UUID
public let challenge: String
public let expiresAtEpochSeconds: Int64
public init(challengeId: UUID, challenge: String, expiresAtEpochSeconds: Int64) {
self.challengeId = challengeId
self.challenge = challenge
self.expiresAtEpochSeconds = expiresAtEpochSeconds
}
}
public struct AppAttestKeyState: Codable, Equatable, Sendable {
public let keyId: String
public let isRegistered: Bool
public init(keyId: String, isRegistered: Bool) {
self.keyId = keyId
self.isRegistered = isRegistered
}
}
public struct AccountRefreshTransaction: Codable, Equatable, Sendable {
public let refreshTokenDigest: String
public let operationId: UUID
public init(refreshTokenDigest: String, operationId: UUID) {
self.refreshTokenDigest = refreshTokenDigest
self.operationId = operationId
}
}
public protocol AccountSessionVault: Sendable {
func loadSession() async throws -> AccountTokenSession?
func saveSession(_ session: AccountTokenSession) async throws
func clearSession() async throws
func beginRefreshTransaction(
refreshTokenDigest: String
) async throws -> AccountRefreshTransaction
func clearRefreshTransaction() async throws
}
public protocol AppAttestKeyStateStoring: Sendable {
func loadAppAttestKeyState() async throws -> AppAttestKeyState?
func saveAppAttestKeyState(_ state: AppAttestKeyState) async throws
func clearAppAttestKeyState() async throws
}
public protocol AppleUserIdentifierStoring: Sendable {
func loadAppleUserIdentifier() async throws -> String?
func saveAppleUserIdentifier(_ userIdentifier: String) async throws
func clearAppleUserIdentifier() async throws
}
public protocol OOBEInstallationIDStoring: Sendable {
func oobeInstallationID() async throws -> UUID
}
public enum AccountAPIError: Error, Equatable, Sendable {
case invalidRequest(String)
case unauthorized(String)
case refreshTokenReuse
case externalServiceUnavailable(String)
case conflict(String)
case rateLimited(String)
case server(statusCode: Int, code: String, message: String)
case transport
case invalidResponse
case decoding
case secureStorage
case sessionUnavailable
case appleAuthorization
case integrityUnavailable
}
public enum AccountSessionInvalidation: Sendable {
case expired
}
extension AccountAPIError: LocalizedError {
public var errorDescription: String? {
switch self {
case .invalidRequest(let message),
.unauthorized(let message),
.externalServiceUnavailable(let message),
.conflict(let message),
.rateLimited(let message):
return message
case .refreshTokenReuse:
return SharedL10n.string("error.account.refreshTokenReuse")
case .server(_, _, let message):
return message
case .transport:
return SharedL10n.string("error.account.transport")
case .invalidResponse:
return SharedL10n.string("error.account.invalidResponse")
case .decoding:
return SharedL10n.string("error.account.decoding")
case .secureStorage:
return SharedL10n.string("error.account.secureStorage")
case .sessionUnavailable:
return SharedL10n.string("error.account.sessionUnavailable")
case .appleAuthorization:
return SharedL10n.string("error.account.appleAuthorization")
case .integrityUnavailable:
return SharedL10n.string("error.account.integrityUnavailable")
}
}
}
struct APIDataEnvelope<Value: Codable & Sendable>: Codable, Sendable {
let data: Value
}
struct APIErrorEnvelope: Codable, Sendable {
let error: APIErrorPayload
}
struct APIErrorPayload: Codable, Sendable {
let code: String
let message: String
}
struct LegacyAPIErrorEnvelope: Codable, Sendable {
let error: String
}
struct RefreshSessionRequest: Codable, Sendable {
let refreshToken: String
let refreshOperationId: UUID
}
struct DeleteAccountRequest: Codable, Sendable {
let identityToken: String
let authorizationCode: String
let nonce: String
}
struct AppAttestChallengeRequest: Codable, Sendable {
let purpose: AppAttestChallengePurpose
let keyId: String
}
struct AppAttestationRequest: Codable, Sendable {
let challengeId: UUID
let challenge: String
let keyId: String
let attestationObject: String
}
struct AppAssertionRequest: Codable, Sendable {
let challengeId: UUID
let challenge: String
let keyId: String
let assertion: String
let clientDataHash: String
}
struct AppAssertionResponse: Codable, Sendable {
let counter: Int64
}