Optimize AI generation speed and add richer insight data
Speed optimizations:
- Add session.prewarm() in InsightsViewModel and ReportsViewModel init
for 40% faster first-token latency
- Cap maximumResponseTokens on all 8 AI respond() calls (100-600 per use case)
- Add prompt brevity constraints ("1-2 sentences", "2 sentences")
- Reduce report batch concurrency from 4 to 2 to prevent device contention
- Pre-fetch health data once and share across all 3 insight periods
Richer insight data in MoodDataSummarizer:
- Tag-mood correlations: overall frequency + good day vs bad day tag breakdown
- Weather-mood correlations: avg mood by condition and temperature range
- Absence pattern detection: logging gap count with pre/post-gap mood averages
- Entry source breakdown: % of entries from App, Widget, Watch, Siri, etc.
- Update insight prompt to leverage tags, weather, and gap data when available
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -57,6 +57,7 @@ class InsightsViewModel: ObservableObject {
|
||||
let service = FoundationModelsInsightService()
|
||||
insightService = service
|
||||
isAIAvailable = service.isAvailable
|
||||
service.prewarm()
|
||||
} else {
|
||||
insightService = nil
|
||||
isAIAvailable = false
|
||||
@@ -118,12 +119,23 @@ class InsightsViewModel: ObservableObject {
|
||||
let yearEntries = DataController.shared.getData(startDate: yearStart, endDate: now, includedDays: [1, 2, 3, 4, 5, 6, 7])
|
||||
let allTimeEntries = DataController.shared.getData(startDate: allTimeStart, endDate: now, includedDays: [1, 2, 3, 4, 5, 6, 7])
|
||||
|
||||
// Pre-fetch health data once (instead of 3x per period)
|
||||
var sharedHealthAverages: HealthService.HealthAverages?
|
||||
if healthService.isEnabled && healthService.isAuthorized {
|
||||
let allValidEntries = allTimeEntries.filter { ![.missing, .placeholder].contains($0.mood) }
|
||||
if !allValidEntries.isEmpty {
|
||||
let healthData = await healthService.fetchHealthData(for: allValidEntries)
|
||||
sharedHealthAverages = healthService.computeHealthAverages(entries: allValidEntries, healthData: healthData)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate insights concurrently for all three periods
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
group.addTask { @MainActor in
|
||||
await self.generatePeriodInsights(
|
||||
entries: monthEntries,
|
||||
periodName: "this month",
|
||||
healthAverages: sharedHealthAverages,
|
||||
updateState: { self.monthLoadingState = $0 },
|
||||
updateInsights: { self.monthInsights = $0 }
|
||||
)
|
||||
@@ -133,6 +145,7 @@ class InsightsViewModel: ObservableObject {
|
||||
await self.generatePeriodInsights(
|
||||
entries: yearEntries,
|
||||
periodName: "this year",
|
||||
healthAverages: sharedHealthAverages,
|
||||
updateState: { self.yearLoadingState = $0 },
|
||||
updateInsights: { self.yearInsights = $0 }
|
||||
)
|
||||
@@ -142,6 +155,7 @@ class InsightsViewModel: ObservableObject {
|
||||
await self.generatePeriodInsights(
|
||||
entries: allTimeEntries,
|
||||
periodName: "all time",
|
||||
healthAverages: sharedHealthAverages,
|
||||
updateState: { self.allTimeLoadingState = $0 },
|
||||
updateInsights: { self.allTimeInsights = $0 }
|
||||
)
|
||||
@@ -152,6 +166,7 @@ class InsightsViewModel: ObservableObject {
|
||||
private func generatePeriodInsights(
|
||||
entries: [MoodEntryModel],
|
||||
periodName: String,
|
||||
healthAverages: HealthService.HealthAverages?,
|
||||
updateState: @escaping (InsightLoadingState) -> Void,
|
||||
updateInsights: @escaping ([Insight]) -> Void
|
||||
) async {
|
||||
@@ -184,13 +199,6 @@ class InsightsViewModel: ObservableObject {
|
||||
|
||||
updateState(.loading)
|
||||
|
||||
// Fetch health data if enabled - pass raw averages to AI for correlation analysis
|
||||
var healthAverages: HealthService.HealthAverages?
|
||||
if healthService.isEnabled && healthService.isAuthorized {
|
||||
let healthData = await healthService.fetchHealthData(for: validEntries)
|
||||
healthAverages = healthService.computeHealthAverages(entries: validEntries, healthData: healthData)
|
||||
}
|
||||
|
||||
if #available(iOS 26, *), let service = insightService as? FoundationModelsInsightService {
|
||||
do {
|
||||
let insights = try await service.generateInsights(
|
||||
|
||||
Reference in New Issue
Block a user