Files
OSGKeyboard/OSGKeyboardShared/Models/AIReplyVariant.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

522 lines
18 KiB
Swift

// AIReplyVariant.swift
// OSGKeyboard · Shared
//
// Typed reply choices produced by one clipboard-reply request. Model output
// never controls SF Symbols or display labels; only these local allowlists do.
import Foundation
public struct AIReplyVariant: Equatable, Identifiable, Sendable {
public enum Kind: String, CaseIterable, Sendable {
case ordinary
case formal
case playful
case invitationAccept
case invitationDecline
case invitationTentative
case taskAcknowledge
case taskClarify
case taskNegotiate
case blessingReturn
case blessingWarm
case blessingPlayful
case clarificationDirect
case clarificationQuestion
case clarificationConfirm
case answerAffirmative
case answerNegative
case answerConditional
case answerDefer
public var systemImage: String {
switch self {
case .ordinary:
return "bubble.left.fill"
case .formal:
return "briefcase.fill"
case .playful:
return "theatermasks.fill"
case .invitationAccept, .taskAcknowledge:
return "checkmark.circle.fill"
case .invitationDecline:
return "hand.raised.fill"
case .invitationTentative:
return "clock.fill"
case .taskClarify, .clarificationQuestion:
return "questionmark.bubble.fill"
case .taskNegotiate:
return "arrow.left.arrow.right"
case .blessingReturn:
return "heart.fill"
case .blessingWarm:
return "hands.sparkles.fill"
case .blessingPlayful:
return "party.popper.fill"
case .clarificationDirect:
return "bubble.left.and.text.bubble.right.fill"
case .clarificationConfirm:
return "checkmark.bubble.fill"
case .answerAffirmative:
return "checkmark.circle.fill"
case .answerNegative:
return "xmark.circle.fill"
case .answerConditional:
return "arrow.triangle.branch"
case .answerDefer:
return "clock.fill"
}
}
public var titleKey: String {
switch self {
case .ordinary:
return "keyboard.ai.replyVariant.ordinary"
case .formal:
return "keyboard.ai.replyVariant.formal"
case .playful:
return "keyboard.ai.replyVariant.playful"
case .invitationAccept:
return "keyboard.ai.replyVariant.invitationAccept"
case .invitationDecline:
return "keyboard.ai.replyVariant.invitationDecline"
case .invitationTentative:
return "keyboard.ai.replyVariant.invitationTentative"
case .taskAcknowledge:
return "keyboard.ai.replyVariant.taskAcknowledge"
case .taskClarify:
return "keyboard.ai.replyVariant.taskClarify"
case .taskNegotiate:
return "keyboard.ai.replyVariant.taskNegotiate"
case .blessingReturn:
return "keyboard.ai.replyVariant.blessingReturn"
case .blessingWarm:
return "keyboard.ai.replyVariant.blessingWarm"
case .blessingPlayful:
return "keyboard.ai.replyVariant.blessingPlayful"
case .clarificationDirect:
return "keyboard.ai.replyVariant.clarificationDirect"
case .clarificationQuestion:
return "keyboard.ai.replyVariant.clarificationQuestion"
case .clarificationConfirm:
return "keyboard.ai.replyVariant.clarificationConfirm"
case .answerAffirmative:
return "keyboard.ai.replyVariant.answerAffirmative"
case .answerNegative:
return "keyboard.ai.replyVariant.answerNegative"
case .answerConditional:
return "keyboard.ai.replyVariant.answerConditional"
case .answerDefer:
return "keyboard.ai.replyVariant.answerDefer"
}
}
/// Generic choices communicate tone through emotion icons. Intent
/// choices keep their fixed icon so the user's decision stays clear.
public var usesEmotionIcon: Bool {
switch self {
case .ordinary, .formal, .playful:
return true
case .invitationAccept,
.invitationDecline,
.invitationTentative,
.taskAcknowledge,
.taskClarify,
.taskNegotiate,
.blessingReturn,
.blessingWarm,
.blessingPlayful,
.clarificationDirect,
.clarificationQuestion,
.clarificationConfirm,
.answerAffirmative,
.answerNegative,
.answerConditional,
.answerDefer:
return false
}
}
}
/// A bounded semantic hint for future feedback and presentation work.
/// Unknown model values are intentionally normalized to `.neutral`.
public enum Emotion: String, CaseIterable, Sendable {
case neutral
case warm
case celebratory
case empathetic
case encouraging
case grateful
case apologetic
case reassuring
case playful
case enthusiastic
case calm
/// The model selects only this semantic enum. SF Symbol names stay
/// local and validated so malformed model output cannot control UI.
public func systemImage(fallback kind: Kind) -> String {
switch self {
case .neutral:
return kind.systemImage
case .warm, .grateful:
return "heart.fill"
case .celebratory:
return "party.popper.fill"
case .empathetic:
return "heart.text.square.fill"
case .encouraging:
return "hand.thumbsup.fill"
case .apologetic:
return "exclamationmark.bubble.fill"
case .reassuring:
return "checkmark.shield.fill"
case .playful:
return "theatermasks.fill"
case .enthusiastic:
return "sparkles"
case .calm:
return "leaf.fill"
}
}
}
public let id: UUID
public let kind: Kind
public let emotion: Emotion
public let text: String
public init(
id: UUID = UUID(),
kind: Kind,
emotion: Emotion,
text: String
) {
self.id = id
self.kind = kind
self.emotion = emotion
self.text = text
}
}
public enum AIReplyVariantSet: String, CaseIterable, Sendable {
case generic
case invitation
case task
case blessing
case clarification
case yesNoQuestion
public var kinds: [AIReplyVariant.Kind] {
switch self {
case .generic:
return [.ordinary, .formal, .playful]
case .invitation:
return [.invitationAccept, .invitationDecline, .invitationTentative]
case .task:
return [.taskAcknowledge, .taskClarify, .taskNegotiate]
case .blessing:
return [.blessingReturn, .blessingWarm, .blessingPlayful]
case .clarification:
return [
.clarificationDirect,
.clarificationQuestion,
.clarificationConfirm
]
case .yesNoQuestion:
// A yes/no question deserves both stances, not just tone variants —
// and this set intentionally exceeds three options.
return [
.answerAffirmative,
.answerNegative,
.answerConditional,
.answerDefer
]
}
}
public static func resolve(scene: AIClipboardReplyScene?) -> Self {
switch scene {
case .invitation:
return .invitation
case .task:
return .task
case .blessing:
return .blessing
case .clarification:
return .clarification
case .yesNoQuestion:
return .yesNoQuestion
case .complaint, .negativeQuestion, nil:
return .generic
}
}
public static func resolve(kinds: Set<AIReplyVariant.Kind>) -> Self? {
allCases.first { Set($0.kinds) == kinds }
}
public static func shouldGenerate(
multipleRepliesEnabled: Bool,
scene: AIClipboardReplyScene?
) -> Bool {
multipleRepliesEnabled || scene?.requiresIntentVariants == true
}
}
public enum AIReplyVariantParsingResult: Equatable, Sendable {
case variants([AIReplyVariant])
case single(AIReplyVariant)
}
public enum AIReplyVariantParser {
/// The only accepted multi-reply wire shape is:
/// `{"variants":[{"kind":"ordinary","emotion":"warm","text":"…"}, ...]}`
/// with exactly one item of each kind and no additional JSON fields.
public static func parse(
_ raw: String,
sourceText: String? = nil,
variantSet: AIReplyVariantSet = .generic
) -> [AIReplyVariant]? {
guard let data = raw.data(using: .utf8),
let root = try? JSONSerialization.jsonObject(with: data),
let object = root as? [String: Any],
Set(object.keys) == ["variants"],
let items = object["variants"] as? [[String: Any]],
items.count == variantSet.kinds.count else {
return nil
}
let expectedKinds = Set(variantSet.kinds)
var variantsByKind: [AIReplyVariant.Kind: AIReplyVariant] = [:]
for item in items {
guard Set(item.keys) == ["kind", "emotion", "text"],
let rawKind = item["kind"] as? String,
let kind = AIReplyVariant.Kind(rawValue: rawKind),
expectedKinds.contains(kind),
variantsByKind[kind] == nil,
let rawEmotion = item["emotion"] as? String,
let rawText = item["text"] as? String else {
return nil
}
let text = rawText.trimmingCharacters(in: .whitespacesAndNewlines)
guard !text.isEmpty,
!isSourceEcho(text, sourceText: sourceText) else {
return nil
}
let emotion = AIReplyVariant.Emotion(rawValue: rawEmotion) ?? .neutral
variantsByKind[kind] = AIReplyVariant(
kind: kind,
emotion: emotion,
text: text
)
}
let ordered = variantSet.kinds.compactMap { variantsByKind[$0] }
return ordered.count == variantSet.kinds.count ? ordered : nil
}
/// Strict multi-reply parsing with a conservative single ordinary fallback
/// only for generic tone choices. Intent scenes fail closed instead.
public static func parseOrFallback(
_ raw: String,
sourceText: String? = nil,
variantSet: AIReplyVariantSet = .generic
) -> AIReplyVariantParsingResult? {
if let variants = parse(
raw,
sourceText: sourceText,
variantSet: variantSet
) {
return .variants(variants)
}
// A plain-text fallback cannot safely preserve the user's intended
// stance for invitation, task, blessing, or clarification choices.
guard variantSet == .generic else { return nil }
guard let text = fallbackText(from: raw),
!isSourceEcho(text, sourceText: sourceText) else {
return nil
}
return .single(
AIReplyVariant(kind: .ordinary, emotion: .neutral, text: text)
)
}
public static func fallbackText(from raw: String) -> String? {
let unfenced = removingCodeFence(from: raw)
guard !unfenced.isEmpty else { return nil }
if let data = unfenced.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: data),
let extracted = replyText(in: json) {
return normalized(extracted)
}
if let extracted = textValueFromMalformedJSON(unfenced) {
return normalized(extracted)
}
guard !looksLikeJSON(unfenced) else { return nil }
return normalized(unfenced)
}
private static let preferredTextKeys = [
"text", "reply", "response", "content", "message", "answer"
]
private static let metadataValues = Set(
AIReplyVariant.Kind.allCases.map(\.rawValue)
+ AIReplyVariant.Emotion.allCases.map(\.rawValue)
+ ["variants", "kind", "emotion", "text"]
)
/// Rejects responses that merely report the clipboard message back to its
/// sender. A bounded LCS catches close paraphrases while still allowing a
/// reply to mention a necessary name or short keyword.
private static func isSourceEcho(
_ candidate: String,
sourceText: String?
) -> Bool {
guard let sourceText else { return false }
let source = normalizedComparisonCharacters(sourceText)
let reply = normalizedComparisonCharacters(candidate)
guard source.count >= 8, reply.count >= 8 else { return false }
let overlap = longestCommonSubsequenceLength(source, reply)
guard overlap >= 8 else { return false }
return Double(overlap) / Double(source.count) >= 0.68
&& Double(overlap) / Double(reply.count) >= 0.45
}
private static func normalizedComparisonCharacters(
_ text: String
) -> [Character] {
Array(
text.lowercased().filter { character in
character.unicodeScalars.contains {
CharacterSet.alphanumerics.contains($0)
}
}.prefix(1_000)
)
}
private static func longestCommonSubsequenceLength(
_ lhs: [Character],
_ rhs: [Character]
) -> Int {
let shorter: [Character]
let longer: [Character]
if lhs.count <= rhs.count {
shorter = lhs
longer = rhs
} else {
shorter = rhs
longer = lhs
}
var previous = Array(repeating: 0, count: shorter.count + 1)
var current = previous
for longCharacter in longer {
for index in shorter.indices {
if longCharacter == shorter[index] {
current[index + 1] = previous[index] + 1
} else {
current[index + 1] = max(
previous[index + 1],
current[index]
)
}
}
swap(&previous, &current)
current = Array(repeating: 0, count: shorter.count + 1)
}
return previous[shorter.count]
}
private static func replyText(in value: Any) -> String? {
if let object = value as? [String: Any] {
for key in preferredTextKeys {
if let text = object[key] as? String,
let normalized = normalized(text) {
return normalized
}
}
for nested in object.values {
if let text = replyText(in: nested) {
return text
}
}
}
if let array = value as? [Any] {
for nested in array {
if let text = replyText(in: nested) {
return text
}
}
}
if let text = value as? String,
!metadataValues.contains(text),
let normalized = normalized(text) {
return normalized
}
return nil
}
private static func removingCodeFence(from raw: String) -> String {
var lines = raw
.trimmingCharacters(in: .whitespacesAndNewlines)
.components(separatedBy: .newlines)
if lines.first?.trimmingCharacters(in: .whitespacesAndNewlines)
.hasPrefix("```") == true {
lines.removeFirst()
}
if lines.last?.trimmingCharacters(in: .whitespacesAndNewlines) == "```" {
lines.removeLast()
}
return lines
.joined(separator: "\n")
.trimmingCharacters(in: .whitespacesAndNewlines)
}
private static func textValueFromMalformedJSON(_ raw: String) -> String? {
let keyPattern = #""(?:text|reply|response|content|message|answer)"\s*:\s*("(?:\\.|[^"\\])*")"#
if let regex = try? NSRegularExpression(pattern: keyPattern),
let match = regex.firstMatch(
in: raw,
range: NSRange(raw.startIndex..., in: raw)
),
let range = Range(match.range(at: 1), in: raw) {
return decodeJSONString(String(raw[range]))
}
let stringPattern = #""(?:\\.|[^"\\])*""#
guard let regex = try? NSRegularExpression(pattern: stringPattern) else {
return nil
}
let candidates = regex.matches(
in: raw,
range: NSRange(raw.startIndex..., in: raw)
).compactMap { match -> String? in
guard let range = Range(match.range, in: raw),
let decoded = decodeJSONString(String(raw[range])),
!metadataValues.contains(decoded) else {
return nil
}
return normalized(decoded)
}
return candidates.max { $0.count < $1.count }
}
private static func decodeJSONString(_ quoted: String) -> String? {
guard let data = quoted.data(using: .utf8) else { return nil }
return try? JSONDecoder().decode(String.self, from: data)
}
private static func looksLikeJSON(_ value: String) -> Bool {
let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.hasPrefix("{") || trimmed.hasPrefix("[")
}
private static func normalized(_ value: String) -> String? {
let text = value.trimmingCharacters(in: .whitespacesAndNewlines)
return text.isEmpty ? nil : text
}
}