You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🤖 Lazy-load AI SDK providers to optimize startup time
**Problem:**
App startup time was 6-13s due to eagerly loading all AI SDK providers
(@ai-sdk/anthropic, @ai-sdk/openai, @ai-sdk/google) at module load time.
**Solution:**
Implemented lazy loading strategy for AI SDK dependencies:
Phase 1: Lazy-load provider-specific web search tools
- Made getToolsForModel() async with dynamic imports
- Providers only loaded when creating tools for that specific model
Phase 2: Lazy-load provider model creation in aiService
- Converted createModel() to async with dynamic imports
- Anthropic and OpenAI providers loaded on-demand
Phase 3: Removed unused Google provider
- Google was only used for web_search tool (not chat)
- Removed @ai-sdk/google dependency entirely
**Regression Prevention:**
Added two CI checks to prevent future regressions:
1. check_eager_imports.sh - Detects eager AI SDK imports in critical
startup files (main.ts, config.ts, preload.ts)
2. check_bundle_size.sh - Fails if dist/main.js exceeds 20KB
(currently 15KB, indicates eager imports if it grows)
Both integrated into `make static-check` for CI enforcement.
**Impact:**
- Expected 50-60% reduction in startup time (6-13s → 3-6s target)
- Bundle size remains at 15KB (within 20KB limit)
- First message has ~200-300ms delay for provider load (acceptable)
- All unit and integration tests pass
**Files changed:**
- src/utils/tools/tools.ts - Async tools with lazy provider imports
- src/services/aiService.ts - Lazy provider creation
- src/utils/ai/providerFactory.ts - NEW: Centralized provider factory
- scripts/check_eager_imports.sh - NEW: Static import analysis
- scripts/check_bundle_size.sh - NEW: Bundle size monitoring
- Makefile - Added startup performance checks to static-check
- eslint.config.mjs - Allow dynamic imports for AI SDK lazy loading
- src/main.ts - Added documentation about lazy loading requirements
Related: #231
0 commit comments