How rasaljayasinghe/nayana-kapruka works
1. Product shape
- This is a Next.js 16 App Router application for a conversational shopping assistant called Nayana for Kapruka Sri Lanka.
- The app is centered on a full-screen chat experience, but it also includes product browsing, cart review, and checkout handoff.
2. Top-level runtime boundary
- The architecture splits cleanly into client UI and server route handlers.
- Client-side components handle the conversational shell, product cards, cart state, voice interaction, and page transitions.
- Server-side API routes handle LLM chat, product lookup, speech synthesis, tracking, and checkout/order creation.
3. Primary user flow
- Users start in chat, ask for products or help, and Nayana searches the Kapruka catalog through MCP tools.
- Selected items are added to a session cart stored in localStorage, not a database.
- Users then move to a dedicated order review screen, confirm the purchase, and only then does the app create the actual Kapruka order.
4. Chat orchestration layer
- The chat subsystem is the core orchestration layer: src/components/chat and src/lib/chat-*, nayana-*, mcp-actions, and kapruka-mcp.
- It coordinates assistant messaging, tool results, suggestions, delegation indicators, and conversation state.
- The UI exposes “agent” roles for clarity, but these map to real tool execution rather than separate backend services.
5. LLM and tool integration
- /api/chat is the main AI entry point and uses AI SDK v6 with MiniMax as the model provider.
- Tooling is mediated through Kapruka MCP at https://mcp.kapruka.com/mcp, which provides product search, delivery checks, and tracking.
- The assistant is intentionally prevented from directly placing orders; kapruka_create_order is reserved for the explicit checkout step.
6. Checkout boundary
- Checkout is isolated in /api/checkout.
- The review page gathers cart contents and delivery details, then a confirmation modal gates the final request.
- That route calls Kapruka’s order creation tool and returns a live payment link, keeping purchase authorization outside the chat agent.
7. Voice and speech subsystem
- Voice is a separate capability with three modes: hands-free conversation, per-message playback, and one-shot dictation.
- useVoiceConversation, useSpeechInput, useNayanaSpeech, and the ElevenLabs-backed /api/speech route support this loop.
- The voice flow is designed so speech input and speech output remain additive to chat, not a replacement for it.
8. Product discovery and merchandising UI
- Product browsing is implemented as a dedicated UI layer under src/components/product and src/app/product/[id].
- This includes cards, compare rows, carousel, detail modal/shell, and discovery grid components.
- These components connect product exploration to the cart, and product detail pages can participate in the same shopping session as chat.
9. Order review and cart state
- The cart is managed locally through provider state plus persistent browser storage.
- CartProvider, MobileBasketProvider, and order-review components share a single session-based shopping context.
- This is the boundary where UI validation and budget caps are enforced before any server-side order creation happens.
10. App shell and shared UI composition
- AppProviders wires the global providers for cart, toasts, voice session state, and mobile basket behavior.
- The main layout uses shared chat shell components, brand/header components, and reusable UI primitives in src/components/ui.
- This keeps the app visually consistent while allowing the chat, product, and order surfaces to reuse the same design system.
11. Language and regional behavior
- The app supports English, Sinhala, and Tamil, with code-switch-aware Tanglish mirroring.
- Language selection is passed with chat requests and is part of the prompt/system behavior, not a separate translation service.
- Sri Lankan context helpers and parsing utilities shape catalog and language outputs to feel local to Kapruka’s audience.
12. External dependencies and deployment constraints
- The meaningful external systems are Kapruka MCP, MiniMax for chat generation, and ElevenLabs for voice.
- Netlify is the deployment target, so the chat route is tuned for function time limits and multi-step tool loops.
- There is no backend database; persistence is deliberately lightweight and browser/session-oriented.
13. Repo structure to diagram
- A clear diagram would show: browser UI and providers → chat/product/order pages → API routes → external services.
- The important arrows are chat to MCP, checkout to Kapruka order creation, voice to ElevenLabs, and cart/order review staying client-side until confirmation.
Main entry points: ChatShell.tsx Chat shell · route.ts Chat API · AppProviders.tsx AppProviders · useVoiceConversation.ts Voice hooks
Components 24
Client UI
Browser-side chat, product browsing, cart, voice, and review surfaces.
- App shell · Root app layout and global frame.
src/app/layout.tsx - AppProviders · Global provider composition for cart, toast, voice, and mobile basket state.
src/components/providers/AppProviders.tsx - Cart state · Client-side shopping cart stored in provider state and browser storage.
src/components/providers/CartProvider.tsx - Voice session · Shared voice and playback session context.
src/components/providers/VoiceSessionContext.tsx - Chat home · Main conversational landing page for Nayana.
src/app/page.tsx - Chat shell · Full-screen chat experience and assistant orchestration surface.
src/components/chat/ChatShell.tsx - Messages · Renders assistant and user conversation turns.
src/components/chat/MessageList.tsx - Composer · Text entry and chat submission controls.
src/components/chat/Composer.tsx - Voice hooks · Browser voice conversation and dictation hooks.
src/hooks/useVoiceConversation.ts - Browse grid · Product discovery grid and cards for browsing catalog items.
src/components/product/ProductDiscoverGrid.tsx - Product page · Dedicated product route that joins the shopping session.
src/app/product/[id]/page.tsx - Detail client · Interactive product detail shell and cart-aware actions.
src/app/product/[id]/ProductDetailPageClient.tsx - Review page · Session cart and delivery review before purchase confirmation.
src/app/order/review/page.tsx - Review content · Cart validation, delivery details, and confirmation gate.
src/components/order/OrderReviewContent.tsx
Server Routes
Next.js API handlers for chat, checkout, speech, product lookup, and tracking.
- Chat API · Main LLM entry point for assistant turns and tool loops.
src/app/api/chat/route.ts - Checkout API · Final order creation and payment-link handoff.
src/app/api/checkout/route.ts - Speech API · Server-side speech synthesis endpoint.
src/app/api/speech/route.ts - Product API · Product lookup endpoint used by product detail flows.
src/app/api/product/[id]/route.ts - Track API · Tracking and event capture endpoint.
src/app/api/track/route.ts - MCP actions · Chat-side tool mediation and action wiring.
src/lib/mcp-actions.ts - Kapruka MCP · Integration client for catalog, delivery, tracking, and order tools.
src/lib/kapruka-mcp.ts
External Services
Third-party services used for model access, tool execution, speech, and order creation.
- ElevenLabs · External speech generation service.
- MiniMax · LLM provider used by the chat route.
- Kapruka tools · Kapruka MCP-backed product search, checks, tracking, and order creation.
Connections 23
- App shell → AppProviders: wraps · Root layout installs global providers.
- AppProviders → Cart state: cart state · Cart context is made available app-wide.
- AppProviders → Voice session: voice state · Voice session context is made available app-wide.
- Chat home → Chat shell: mounts · Home page renders the chat experience.
- Chat shell → Messages: shows · Conversation transcript is rendered in the shell.
- Chat shell → Composer: inputs · User text entry flows through the composer.
- Chat shell → Voice hooks: voice · Hands-free and dictation behavior is layered into chat.
- Chat shell → Chat API: sends · Messages are posted to the chat route.
- Chat API → MiniMax: generates · The chat route calls the model provider.
- Chat API → MCP actions: tools · Assistant tool calls are mediated through the action layer.
- MCP actions → Kapruka MCP: executes · Actions delegate to the Kapruka MCP client.
- Kapruka MCP → Kapruka tools: calls · MCP client reaches Kapruka product and order tools.
- Browse grid → Product page: navigates · Discovery browsing opens product detail routes.
- Product page → Detail client: hydrates · Route renders the interactive product detail client.
- Detail client → Product API: loads · Product details are fetched from the product API.
- Review page → Review content: renders · Review route uses shared checkout UI.
- Review content → Cart state: reads cart · Checkout review is driven by session cart state.
- Review content → Checkout API: confirms · Final confirmation submits the order request.
- Checkout API → Kapruka tools: creates order · Checkout route invokes the order creation tool.
- Speech API → ElevenLabs: synthesizes · Speech endpoint delegates to ElevenLabs.
- Track API → Kapruka tools: tracks · Tracking events are forwarded through the Kapruka integration.
- Voice hooks → Speech API: plays · Voice UI can trigger server-side speech output.
- Voice hooks → Chat API: dictates · Speech input can feed chat requests.