Skip to content

Conversation

@ammar-agent
Copy link
Collaborator

Fixes #231

Problem

Startup time is 6-13s because all AI SDK providers load eagerly at module initialization, even though only one provider is used per session.

Solution

Lazy-load AI SDK dependencies — providers now load on-demand using dynamic imports:

  1. Tools: lazy-loads provider-specific web_search tools
  2. Models: lazy-loads Anthropic/OpenAI providers on first use
  3. Cleanup: Removed unused @ai-sdk/google dependency

Regression Prevention

Added CI checks to prevent future regressions:

  • check_eager_imports.sh — fails if AI SDK imported eagerly in startup-critical files
  • check_bundle_size.sh — fails if dist/main.js exceeds 20KB (currently 15KB)

Both integrated into make static-check for automatic enforcement.

Impact

  • Expected: 50-60% startup time reduction (6-13s → 3-6s)
  • Bundle size: Remains at 15KB (no regression)
  • Trade-off: ~200-300ms delay on first message (acceptable)
  • Tests: All pass (409 unit + 72 integration)

Generated with cmux

**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
@ammario ammario merged commit 05fba63 into main Oct 13, 2025
7 checks passed
@ammario ammario deleted the tree-shake branch October 13, 2025 21:00
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.

Optimize AI SDK imports with tree-shaking to reduce startup time

2 participants