This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Targeted Code Quality (safe while dev server runs)
npx tsc --noEmit # Type check without building
npx eslint src/path/to/file.ts # Lint specific file
npm run lint # Lint entire projectBig-AGI is a Next.js 15 application with a modular architecture built for advanced AI interactions. The codebase follows a three-layer structure with distinct separation of concerns.
/app/api/ # Next.js App Router (API routes only, mostly -> /src/server/)
/pages/ # Next.js Pages Router (file-based, mostly -> /src/apps/)
/src/
├── apps/ # Feature applications (self-contained modules)
├── modules/ # Reusable business logic and integrations
├── common/ # Shared infrastructure and utilities
└── server/ # Backend API layer with tRPC
/kb/ # Knowledge base for modules, architectures
- Frontend: Next.js 15, React 18, Material-UI Joy, Emotion (CSS-in-JS)
- State Management: Zustand with localStorge/IndexedDB (single cell) persistence
- API Layer: tRPC with React Query for type-safe communication
- Runtime: Edge Runtime for AI operations, Node.js for data processing
Each app in /src/apps/ is a self-contained feature module:
- Main component (
App*.tsx) - Local state store (
store-app-*.ts) - Feature-specific components and layouts
- Runtime configurations
Example apps: chat/, call/, beam/, draw/, personas/, settings-modal/
Modules in /src/modules/ provide reusable business logic:
aix/- AI communication framework for real-time streamingbeam/- Multi-model AI reasoning system (scatter/gather pattern)blocks/- Content rendering (markdown, code, images, etc.)llms/- Language model abstraction supporting 16 vendors
Location: /src/modules/aix/
Pattern: Client-server streaming architecture with provider abstraction
- Client → tRPC → Server → AI Providers
- Handles streaming/non-streaming responses with batching and error recovery
- Particle-based streaming:
AixWire_Particles→ContentReassembler→DMessage - Provider-agnostic through adapter pattern (OpenAI, Anthropic, Gemini protocols)
Location: /src/modules/beam/
Pattern: Scatter/Gather for parallel AI processing
- Scatter: Multiple models (rays) process input in parallel
- Gather: Fusion algorithms combine outputs
- Real-time UI updates via vanilla Zustand stores
- BeamStore per conversation via ConversationHandler
Location: /src/common/stores/chat/ and /src/common/chat-overlay/
Pattern: Overlay architecture with handler per conversation
ConversationHandlerorchestrates chat, beam, ephemerals- Per-chat stores:
PerChatOverlayStore+BeamStore - Message structure:
DMessage→DMessageFragment[] - Supports multi-pane with independent conversation states
Big-AGI uses a local-first architecture with Zustand + IndexedDB:
- Zustand stores for in-memory state management
- localStorage for persistent settings/all storage (via Zustand persist middleware)
- IndexedDB for persistent chat-only storage (via Zustand persist middleware) on a single key-val cell
- Local-first architecture with offline capability
- Migration system for upgrading data structures across versions
Key storage patterns:
- Stores use
createIDBPersistStorage()for IndexedDB persistence - Version-based migrations handle data structure changes
- Partialize/merge functions control what gets persisted
- Rehydration logic repairs and upgrades data on load
Located in /src/common/stores/ with stores like:
chat/store-chats.ts: Conversations and messagesllms/store-llms.ts: Model configurations
The Optima layout system provides:
- Responsive design adapting desktop/mobile
- Drawer/Panel/Toolbar composition
- Split-pane support for multi-conversation views
- Portal-based rendering for flexible component placement
Located in /src/common/layout/optima/
-
Global Stores (Zustand with IndexedDB persistence)
store-chats: Conversations and messagesstore-llms: Model configurationsstore-ux-labs: UI preferences and labs features
-
Per-Instance Stores (Vanilla Zustand)
store-beam_vanilla: Beam scatter/gather statestore-perchat_vanilla: Chat overlay state- High-performance, no React integration
-
Module Stores
- Feature-specific configuration and state
- Example:
store-module-beam,store-module-t2i
- User input →
Composer→DMessagecreation ConversationHandler.messageAppend()→ Store update_handleExecute()/ConversationHandler.executeChatMessages()→ AIX client request- AIX streaming →
ContentReassembler→ UI updates - Zustand auto-persistence → IndexedDB
- User triggers Beam →
BeamStore.open()state update - Scatter: Parallel
aixChatGenerateContent()to N models - Real-time ray updates → UI progress
- Gather: User selects fusion → Combined output
- Result → New message in conversation
- Each module exports its functionality through index files
- Modules register with central registries (e.g.,
vendors.registry.ts) - Configuration objects define module behavior
- Type-safe integration through strict TypeScript interfaces
- Controlled components with clear prop interfaces
- Hook-based logic extraction for reusability
- Portal rendering for overlays and modals
- Suspense boundaries for async operations
- tRPC routers for type-safe API endpoints
- Zod schemas for runtime validation
- Middleware for request/response processing
- Edge functions for performance-critical AI operations
- API keys stored client-side in localStorage (user-provided)
- Server-side API keys in environment variables only
- XSS protection through proper content escaping
- No credential transmission to third parties
Architecture and system documentation is available in the /kb/ knowledge base:
@kb/KB.md
- Run
npm run lintbefore committing - Type-check with
npx tsc --noEmit - Test critical user flows manually
- Create vendor in
/src/modules/llms/vendors/[vendor]/ - Implement
IModelVendorinterface - Register in
vendors.registry.ts - Add environment variables to
env.ts(if server-side keys needed)
- Check IndexedDB: DevTools → Application → IndexedDB →
app-chats - Monitor Zustand state: Use Zustand DevTools
- Check migration logs in console during rehydration
// Efficient streaming with decimation
aixChatGenerateContent_DMessage(
llmId,
request,
{ abortSignal, throttleParallelThreads: 1 },
async (update, isDone) => {
// Real-time UI updates
}
);// Registry pattern for extensibility
const MODEL_VENDOR_REGISTRY: Record<ModelVendorId, IModelVendor> = {
openai: ModelVendorOpenAI,
anthropic: ModelVendorAnthropic,
// ... 14 more vendors
};The server uses a split architecture with two tRPC routers:
Distributed edge runtime for low-latency AI operations:
- AIX - AI streaming and communication
- LLM Routers - Direct vendor integrations (OpenAI, Anthropic, Gemini, Ollama)
- External Services - ElevenLabs (TTS), Google Search, YouTube transcripts
Located at /src/server/trpc/trpc.router-edge.ts
Centralized server for data processing operations:
- Browse - Web scraping and content extraction
- Trade - Import/export functionality (ChatGPT, markdown, JSON)
Located at /src/server/trpc/trpc.router-cloud.ts
Key Pattern: Edge runtime for AI (fast, distributed), Cloud runtime for data ops (centralized, Node.js)