How tehan-hewage/kavi works
1. Overall shape
- This is a Next.js 14 App Router application in TypeScript.
- The UI is a single-page, full-screen shopping assistant centered on chat, with side features for products, cart, checkout, and voice.
2. Primary user experience layer
- The app/page.tsx entry point likely assembles the main experience using components under components/chat, components/products, components/cart, components/checkout, and components/voice.
- The interaction model is conversational first, but the app also supports direct product browsing and cart/checkout actions.
3. Server-side API boundary
- The main backend surface is the app/api/* route set:
- api/chat for the AI shopping agent
- api/product for product lookup/catalog access
- api/speak for text-to-speech
- api/transcribe for speech-to-text
- api/voice/token for voice service auth/token exchange
- These routes form the boundary between the browser UI and external services.
4. AI orchestration subsystem
- The core agent logic lives in lib/agent-system-prompt.ts, lib/kavi-system-prompt.ts, lib/language-detect.ts, lib/mcp-tools.ts, lib/mcp-client.ts, and lib/kapruka-mcp.ts.
- The app is designed to route through Gemini or Anthropic depending on available environment keys, with the model choice abstracted away from the UI.
- The chat backend is therefore an orchestration layer: language handling, system prompting, tool use, and fallback/error translation.
5. External commerce integration
- A key boundary is the integration with the Kapruka MCP server via Model Context Protocol.
- The repo suggests tool-based access to live product browsing, availability checks, and catalog queries rather than a static local catalog.
- This is the main commerce data source feeding product cards, search results, and cartable items.
6. Cart and checkout flow
- Cart state is handled client-side via components/providers/CartProvider.tsx, with presentation components in components/cart.
- Checkout UI lives in components/checkout and includes delivery date, gift message, and a payment link card.
- The README indicates pricing logic includes a flat Sri Lankan delivery fee and a countdown-based payment prompt, so pricing and urgency are part of the UI state model.
7. Voice experience
- Voice interaction is a separate subsystem spanning hooks/useAudioCapture.ts, hooks/useVoice.ts, hooks/useTTS.ts, hooks/useGeminiLive.ts, and UI in components/voice.
- The presence of api/transcribe, api/speak, and api/voice/token shows a full voice pipeline: capture → transcription → agent response → speech output.
- components/ui/VoiceOrb.tsx and components/voice/VoiceWaveform.tsx indicate the voice mode has a distinct visual state.
8. Shared client state and personalization
- Several providers coordinate app-wide behavior:
- LanguageProvider for multilingual UI state
- CurrencyProvider for price display
- ThemeProvider for dark/light mode
- CartProvider for shopping state
- These are important architectural boundaries because they let the chat, product, cart, and checkout surfaces stay synchronized without tight coupling.
9. Component decomposition
- The UI is organized by domain rather than by page:
- chat: input, message list, bubbles, typing indicator, shell, header
- products: card, grid, carousel, detail modal
- cart: drawer, item, summary
- checkout: form, delivery picker, gift message, pay card
- shared UI: pills, chips, skeletons, status timeline, buttons
- This suggests a modular composition model where the chat experience can trigger secondary UI panels instead of navigating to separate pages.
10. Domain types and shared utilities
- types/cart.ts, types/order.ts, and types/product.ts define the commerce data model.
- lib/types.ts, lib/image-utils.ts, lib/animations.ts, lib/cultural-calendar.ts, lib/sinhala-phrases.ts, and styles/* provide shared logic for presentation, localization, and brand consistency.
- This keeps business data, cultural language assets, and visual tokens centralized.
11. Testing and quality gates
- The repo has a strong test matrix:
- unit tests for prompts, language detection, pricing, and Sinhala phrases
- integration tests for chat API, cart provider, MCP tools, and load behavior
- component tests for the major UI pieces
- end-to-end tests covering welcome, search, cart, delivery quote, gift message, checkout, tracking, language switching, dark mode, mobile, and errors
- This indicates the architecture is built around real user flows, not just isolated components.
12. Deployment and runtime shape
- vercel.json suggests deployment on Vercel, which fits the App Router + serverless route model.
- next.config.mjs, tailwind.config.ts, and vitest/playwright config files show a standard modern Next.js stack with separate build, test, and runtime concerns.
- Environment configuration is central because the app’s AI backend and voice features depend on external API keys and live service access.
13. How to draw the architecture
- The cleanest diagram would place the browser UI at the center, fed by:
- the Next.js server/API routes on one side
- Kapruka MCP and LLM providers on the other
- voice services as a parallel input/output path
- Inside the UI, show a shared state layer for language/cart/currency/theme, then domain modules for chat, products, cart, checkout, and voice.
Main entry points: page.tsx Home · ChatShell.tsx Chat Shell · useVoice.ts Use Voice · route.ts Chat API
Components 28
Browser UI
Full-screen shopping assistant experience composed from domain-specific panels and shared state.
- Home · Main App Router page assembling the assistant experience.
app/page.tsx - Chat Shell · Primary conversational container for the assistant UI.
components/chat/ChatShell.tsx - Chat Input · Composer for text prompts and user commands.
components/chat/ChatInput.tsx - Messages · Conversation transcript and message rendering.
components/chat/MessageList.tsx
Shared State
App-wide providers that synchronize language, currency, theme, and cart behavior.
- Cart Provider · Client-side cart state shared across chat, cart, and checkout.
components/providers/CartProvider.tsx - Language Provider · Global language selection and localization state.
components/providers/LanguageProvider.tsx - Currency Provider · Shared price-display and currency formatting state.
components/providers/CurrencyProvider.tsx - Theme Provider · Dark/light mode state used across the UI.
components/providers/ThemeProvider.tsx
Next.js API
Server routes that bridge the browser to AI, catalog, and voice services.
- Chat API · Server boundary for AI shopping conversations.
app/api/chat/route.ts - Product API · Server boundary for catalog and product lookup.
app/api/product/route.ts - Speak API · Text-to-speech endpoint for voice playback.
app/api/speak/route.ts - Transcribe API · Speech-to-text endpoint for audio capture uploads.
app/api/transcribe/route.ts - Voice Token · Token exchange for external voice service access.
app/api/voice/token/route.ts
AI Orchestration
Prompting, language handling, and MCP tool integration for the shopping agent.
- Agent Prompt · Core shopping-agent instructions and response shaping.
lib/agent-system-prompt.ts - Language Detect · Language and script detection used to steer responses.
lib/language-detect.ts - MCP Tools · Tool definitions and execution bridge for live commerce actions.
lib/mcp-tools.ts - MCP Client · Client wrapper for communicating with the MCP server.
lib/mcp-client.ts - Kapruka MCP · External commerce source for live catalog and availability data.
lib/kapruka-mcp.ts
Commerce UI
Product, cart, and checkout surfaces that turn chat intent into purchase flow.
- Product Grid · Browseable product results surfaced from agent or catalog lookups.
components/products/ProductGrid.tsx - Product Card · Compact product presentation used in lists and recommendations.
components/products/ProductCard.tsx - Detail Modal · Expanded product view for inspection and selection.
components/products/ProductDetailModal.tsx - Cart Drawer · Side cart surface for reviewing and editing items.
components/cart/CartDrawer.tsx - Cart Summary · Totals and purchase summary for the active cart.
components/cart/CartSummary.tsx - Checkout Form · Checkout fields for delivery, gifts, and order finalization.
components/checkout/CheckoutForm.tsx - Pay Link · Payment CTA and urgency card for completing checkout.
components/checkout/PayLinkCard.tsx
Voice Pipeline
Capture, transcribe, synthesize, and render voice interactions.
- Voice Button · Entry point for push-to-talk or voice mode interaction.
components/voice/VoiceButton.tsx - Waveform · Animated visual feedback for active voice sessions.
components/voice/VoiceWaveform.tsx - Use Voice · Client voice-state controller connecting capture and playback.
hooks/useVoice.ts
Connections 27
- Home → Chat Shell: assembles · Home page composes the conversational shell and supporting panels.
- Home → Product Grid: shows · Main experience can surface product discovery alongside chat.
- Home → Cart Drawer: includes · Cart is available as part of the single-screen shopping flow.
- Home → Checkout Form: includes · Checkout is embedded in the same UI rather than a separate route.
- Home → Voice Button: enables · Voice interaction is exposed from the main app surface.
- Chat Shell → Chat Input: contains · The shell hosts the message composer.
- Chat Shell → Messages: contains · The shell renders the conversation history.
- Cart Provider → Cart Drawer: drives · Shared cart state powers the drawer contents.
- Cart Provider → Cart Summary: drives · Totals and item counts derive from cart state.
- Currency Provider → Product Card: formats · Currency state affects displayed prices in product views.
- Language Provider → Chat Shell: localizes · Language state changes chat copy and response language.
- Theme Provider → Home: themes · Theme state controls global light/dark rendering.
- Chat API → Agent Prompt: uses · Chat route applies the agent system prompt.
- Chat API → Language Detect: detects · Incoming chat messages are normalized by language/script.
- Chat API → MCP Tools: invokes · Chat backend can call commerce tools for live actions.
- MCP Tools → MCP Client: wraps · Tool layer delegates transport to the MCP client.
- MCP Client → Kapruka MCP: connects · Client reaches the external Kapruka MCP service.
- Product API → MCP Tools: queries · Product lookups route through the same commerce tooling boundary.
- Speak API → Voice Button: supports · Speech synthesis feeds voice playback flows.
- Transcribe API → Use Voice: supports · Transcription is part of the voice capture pipeline.
- Voice Token → Use Voice: authorizes · Voice token exchange enables external voice sessions.
- Use Voice → Waveform: renders · Voice state drives the active waveform visualization.
- Use Voice → Voice Button: controls · Hook coordinates recording, playback, and mute state.
- Product Grid → Product Card: contains · Grid is composed of repeated product cards.
- Product Card → Detail Modal: opens · Card interactions can reveal the detail modal.
- Cart Drawer → Checkout Form: feeds · Selected cart items flow into checkout.
- Checkout Form → Pay Link: ends with · Checkout culminates in the payment prompt/card.