Files
OSGKeyboard/OSGKeyboardShared/Core/Configuration/LiveConfigurationStore.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

186 lines
7.7 KiB
Swift

// LiveConfigurationStore.swift
// OSGKeyboard · Shared
//
// In-memory settings snapshot for connection checks and model probes.
// Reads the values the user is editing — not a delayed Keychain re-fetch.
import Foundation
public struct LiveConfigurationSnapshot: @unchecked Sendable {
public let providerId: String
public let baseURL: String
public let apiKey: String
public let model: String
public let asrProviderId: String
public let asrBaseURL: String
public let asrApiKey: String
public let asrModel: String
public let engineMode: String
public let credentialSource: CredentialSource
public let polishIntensity: PolishIntensity
public let aiResponseLength: AIResponseLength
public let llmThinkingEnabled: Bool
public let personalDictionary: PersonalDictionary
public let polishStyleCatalog: PolishStyleCatalog
public let activePolishStyleId: String
public let personalReplyStyleId: String
public let detectedAppContext: (context: AppContext, observedAt: Date)?
public let cloudASRPersistence: UserDefaults
public init(
providerId: String,
baseURL: String,
apiKey: String,
model: String,
asrProviderId: String,
asrBaseURL: String,
asrApiKey: String,
asrModel: String,
engineMode: String,
credentialSource: CredentialSource = .byok,
polishIntensity: PolishIntensity,
aiResponseLength: AIResponseLength = .default,
llmThinkingEnabled: Bool,
personalDictionary: PersonalDictionary,
polishStyleCatalog: PolishStyleCatalog,
activePolishStyleId: String,
personalReplyStyleId: String,
detectedAppContext: (context: AppContext, observedAt: Date)?,
cloudASRPersistence: UserDefaults
) {
self.providerId = providerId
self.baseURL = baseURL
self.apiKey = apiKey
self.model = model
self.asrProviderId = asrProviderId
self.asrBaseURL = asrBaseURL
self.asrApiKey = asrApiKey
self.asrModel = asrModel
self.engineMode = engineMode
self.credentialSource = credentialSource
self.polishIntensity = polishIntensity
self.aiResponseLength = aiResponseLength
self.llmThinkingEnabled = llmThinkingEnabled
self.personalDictionary = personalDictionary
self.polishStyleCatalog = polishStyleCatalog
self.activePolishStyleId = activePolishStyleId
self.personalReplyStyleId = personalReplyStyleId
self.detectedAppContext = detectedAppContext
self.cloudASRPersistence = cloudASRPersistence
}
/// Capture every value exposed by an arbitrary configuration store.
/// Later source changes cannot alter provider, credential, or style
/// selection for an operation already using this snapshot.
public init(store: any ConfigurationStore) {
self.init(
providerId: store.providerId,
baseURL: store.baseURL,
apiKey: store.apiKey,
model: store.model,
asrProviderId: store.asrProviderId,
asrBaseURL: store.asrBaseURL,
asrApiKey: store.asrApiKey,
asrModel: store.asrModel,
engineMode: store.engineMode,
credentialSource: store.credentialSource,
polishIntensity: store.polishIntensity,
aiResponseLength: store.aiResponseLength,
llmThinkingEnabled: store.llmThinkingEnabled,
personalDictionary: store.personalDictionary,
polishStyleCatalog: store.polishStyleCatalog,
activePolishStyleId: store.activePolishStyleId,
personalReplyStyleId: store.personalReplyStyleId,
detectedAppContext: store.detectedAppContext,
cloudASRPersistence: store.cloudASRPersistence
)
}
/// Build from live `ProviderConfig` plus persisted App Group extras.
public init(config: ProviderConfig, fallback: AppGroupStore) {
self.init(
providerId: config.providerId,
baseURL: config.baseURL,
apiKey: config.apiKey,
model: config.model,
asrProviderId: config.asrProviderId,
asrBaseURL: config.asrBaseURL,
asrApiKey: config.asrApiKey,
asrModel: config.asrModel,
engineMode: config.engineMode,
credentialSource: config.credentialSource,
polishIntensity: config.polishIntensity,
aiResponseLength: config.aiResponseLength,
llmThinkingEnabled: config.llmThinkingEnabled,
personalDictionary: fallback.personalDictionary,
polishStyleCatalog: fallback.polishStyleCatalog,
activePolishStyleId: fallback.activePolishStyleId,
personalReplyStyleId: fallback.personalReplyStyleId,
detectedAppContext: fallback.detectedAppContext,
cloudASRPersistence: fallback.defaults
)
}
}
/// Ephemeral `ConfigurationStore` backed by an immutable capture of the edited
/// values. API keys remain in this in-memory snapshot and are never persisted
/// by the store; `@unchecked Sendable` covers the referenced UserDefaults handle.
public struct LiveConfigurationStore: ConfigurationStore, @unchecked Sendable {
private let snapshot: LiveConfigurationSnapshot
public init(snapshot: LiveConfigurationSnapshot) {
self.snapshot = snapshot
}
public init(store: any ConfigurationStore) {
self.init(snapshot: LiveConfigurationSnapshot(store: store))
}
public init(config: ProviderConfig, fallback: AppGroupStore) {
self.init(snapshot: LiveConfigurationSnapshot(config: config, fallback: fallback))
}
public var providerId: String { snapshot.providerId }
public var baseURL: String { snapshot.baseURL }
public var apiKey: String { snapshot.apiKey }
public var model: String { snapshot.model }
public var asrProviderId: String { snapshot.asrProviderId }
public var asrBaseURL: String { snapshot.asrBaseURL }
public var asrApiKey: String { snapshot.asrApiKey }
public var asrModel: String { snapshot.asrModel }
public var engineMode: String { snapshot.engineMode }
public var credentialSource: CredentialSource { snapshot.credentialSource }
public var polishIntensity: PolishIntensity { snapshot.polishIntensity }
public var aiResponseLength: AIResponseLength { snapshot.aiResponseLength }
public var llmThinkingEnabled: Bool { snapshot.llmThinkingEnabled }
public var personalDictionary: PersonalDictionary { snapshot.personalDictionary }
public var polishStyleCatalog: PolishStyleCatalog { snapshot.polishStyleCatalog }
public var activePolishStyleId: String { snapshot.activePolishStyleId }
public var personalReplyStyleId: String { snapshot.personalReplyStyleId }
public var detectedAppContext: (context: AppContext, observedAt: Date)? { snapshot.detectedAppContext }
public var cloudASRPersistence: UserDefaults { snapshot.cloudASRPersistence }
public func makeClient(
taskKind: ManagedGatewayTaskKind?,
requestPurpose: ManagedGatewayRequestPurpose?,
oobeFeature: ManagedGatewayOOBEFeature?
) -> LLMClient {
if credentialSource == .managed || requestPurpose == .oobe {
return ManagedLLMClient(
capability: .resolve(taskKind: taskKind),
taskKind: taskKind,
requestPurpose: requestPurpose,
oobeFeature: oobeFeature,
grants: GatewayGrantCoordinator()
)
}
return LLMClientFactory.make(
providerId: providerId,
baseURL: baseURL,
apiKey: apiKey,
model: model,
thinkingEnabled: llmThinkingEnabled
)
}
}