Skip to content

refactor: clean AI architecture#631

Merged
datlechin merged 2 commits intomainfrom
refactor/ai-architecture-cleanup
Apr 8, 2026
Merged

refactor: clean AI architecture#631
datlechin merged 2 commits intomainfrom
refactor/ai-architecture-cleanup

Conversation

@datlechin
Copy link
Copy Markdown
Member

Summary

Architecture cleanup for the AI system — 7 improvements across 9 files, net -44 lines.

A5: Deduplicate provider code

  • Extracted identical collectErrorBody() (9 lines × 3 providers) into AIProvider protocol extension
  • Extracted mapHTTPError() base implementation into AIProviderError static method
  • Only GeminiProvider retains a thin override (adds 403 → auth failure mapping)
  • Removed ~55 lines of duplicated code

A4: Cache provider instances

  • AIProviderFactory now caches providers keyed by (config.id, apiKey)
  • Eliminates URLSession recreation on every inline suggestion request (~2x/sec)
  • Added invalidateCache() and invalidateCache(for:) methods
  • Cache invalidated from AISettingsView on provider save/delete

A3: Unified provider resolution

  • Added AIProviderFactory.ResolvedProvider struct and resolve(for:settings:) convenience
  • Replaces 3-step resolveProviderresolveModelcreateProvider pattern
  • Updated both callers: InlineSuggestionManager and AIChatViewModel

A8: Use AIPromptTemplates

  • handleFixError, handleExplainSelection, handleOptimizeSelection now use AIPromptTemplates instead of building prompts inline
  • Removed dead queryLanguage and queryTypeName computed properties

A2: Move schema fetch to ViewModel

  • Moved fetchSchemaContext() from AIChatPanelView (View) to AIChatViewModel
  • Business logic no longer lives in a SwiftUI view struct
  • View calls await viewModel.fetchSchemaContext() in .task(id:)

UI1: Use shared EmptyStateView

  • Replaced ContentUnavailableView with app's EmptyStateView component
  • Consistent styling via ThemeEngine across all panels
  • Removed SettingsLink hack with raw UserDefaults key

UI2: Fix scroll-to-bottom

  • Removed invisible Color.clear anchor element and isNearBottom state tracking
  • Scroll targets use actual message IDs directly
  • Removed DispatchQueue.main.async workarounds
  • Single scrollToBottom(proxy:) helper replaces 4 duplicate scroll blocks

Test plan

  • Build succeeds
  • AI chat: send messages, streaming works, scroll follows output
  • Inline suggestions: appear correctly, Tab accepts
  • Explain/Optimize/Fix Error from context menu work
  • Settings: add/edit/delete provider, changes take effect immediately
  • Empty states display correctly when no provider configured
  • Switch conversations, scroll resets to bottom

@datlechin datlechin changed the title refactor: clean AI architecture — providers, caching, view layer refactor: clean AI architecture Apr 8, 2026
@datlechin datlechin merged commit d827cc7 into main Apr 8, 2026
2 checks passed
@datlechin datlechin deleted the refactor/ai-architecture-cleanup branch April 8, 2026 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant