How garvbarthwal/amazon-hackon works
1. This repo is split into two independently deployed applications: website/ is the customer-facing Next.js app, and search-service/ is the SmartCart planning microservice. They communicate over HTTP; the website does not share a database with search-service.
2. The website is a Next.js App Router application with page routes for catalog, product detail, cart, checkout, orders, and order detail. Its app/api/* route handlers provide the browser-facing API surface, while components/, hooks/, and lib/ hold the UI, client state, and domain helpers that keep pages thin.
3. The main integration boundary on the website side is website/lib/services/smartcart.ts, which is the adapter to the external cart-planning service. Quick Mode and conversational flows go through this path, while other website APIs appear to serve local product/order/prediction concerns from the website’s own runtime and database.
4. The search-service is an Express/Node 20 API with a small HTTP surface: health, OpenAPI docs, and versioned cart endpoints under /v1/*. src/api/cart.controller.ts is the request boundary, and middleware adds request IDs plus optional bearer auth before any planning logic runs.
5. The core of search-service is a deterministic 8-stage pipeline in src/lib/pipeline/. It classifies the user query, plans a requirement graph, applies constraints, resolves products through layered retrieval, checks coverage, substitutes missing items, composes the cart, and then audits the result. The pipeline is orchestrated as a single end-to-end planning run.
6. Retrieval in search-service is intentionally multi-tiered and conservative: exact product match, brand match, subcategory match, category expansion, synonym lookup, then embedding similarity. This is backed by Postgres with pgvector and pg_trgm, so the data model and retrieval strategy are tightly coupled to the database layer.
7. Search-service owns its own persistence stack: Postgres 16 for catalog, mission knowledge, planner cache, brand stats, and request logs; Redis 7 for sessions and response replay. The planner cache is stored in Postgres, while request-level responses are cached in Redis and also written to a permanent CartRequestLog.
8. The generation layer in search-service is abstracted behind src/lib/infra/llm.ts, with Bedrock or Gemini used for text generation and Gemini used for embeddings. That makes the planning engine portable across LLM providers while keeping embeddings fixed to the same 768-d model used to seed the catalog.
9. Search-service is backed by Prisma migrations and a set of offline scripts under src/scripts/ for seeding products, categories, missions, embeddings, brand stats, domains, and evaluation. That indicates a clear separation between online request handling and offline catalog/knowledge preparation.
10. The website also has its own Prisma schema and migrations, plus local stores like cart-store, conv-store, prediction-store, and quick-mode-store. This suggests the browser experience is stateful on the website side, while the heavy cart-planning intelligence lives entirely in the search-service.
11. Operationally, the search-service is containerized with Docker Compose alongside Postgres and Redis, and the service can be run behind a reverse proxy. The website is a separate frontend application, so an architecture diagram should show two runtime boundaries: the Next.js app serving the UI and local APIs, and the SmartCart API serving planning and replay over HTTP.
Main entry points: infra Infra layer · app Next.js app · services Domain services · orchestrator.ts Planner run
Components 18
Website
Next.js customer-facing app and local API surface.
- Next.js app · App Router pages and browser-facing API routes for catalog, cart, checkout, and orders.
website/app - API routes · Browser-facing handlers for products, categories, predictions, conversation, quick mode, and orders.
website/app/api - UI components · Page-level and modal UI for catalog browsing, cart, checkout, quick mode, and conversation.
website/components - Client state · Hydrated cart and local stores that keep the browser experience stateful.
website/hooks - Domain services · Product, order, tier, and SmartCart adapters used by route handlers and pages.
website/lib/services - SmartCart adapter · Main integration boundary from the website to the external cart-planning service.
website/lib/services/smartcart.ts - Website DB · Website-owned Prisma schema, migrations, and local persistence for browser-facing features.
website/prisma
Search Service
Express planning API and its data/ML stack.
- Express API · SmartCart planning microservice with health, docs, and versioned cart endpoints.
search-service/src/server.ts - Cart controller · HTTP boundary for cart planning requests under /v1/*.
search-service/src/api/cart.controller.ts - Middleware · Request ID injection and optional bearer auth before planning logic runs.
search-service/src/lib/middleware - Planner run · End-to-end planning flow that executes the deterministic cart pipeline.
search-service/src/lib/pipeline/orchestrator.ts - Pipeline stages · Classification, requirement planning, constraints, retrieval, coverage, substitution, cart composition, and audit.
search-service/src/lib/pipeline - Retrieval stack · Exact, brand, subcategory, category expansion, synonym, and embedding-based product retrieval.
search-service/src/lib/pipeline/resolver.ts - Infra layer · Database, Redis, and LLM provider abstraction used by planning and replay.
search-service/src/lib/infra - Postgres · Owns catalog, mission knowledge, planner cache, brand stats, and request logs.
search-service/prisma - Redis · Stores sessions and response replay for request-level caching.
search-service/src/lib/infra/redis.ts - LLM providers · Bedrock or Gemini for text generation, Gemini for embeddings.
search-service/src/lib/infra/llm.ts - Offline scripts · Product, category, mission, embedding, brand, domain, and evaluation preparation jobs.
search-service/src/scripts
Connections 18
- Next.js app → API routes: serves · Pages consume browser-facing API handlers for local data and actions.
- Next.js app → UI components: renders · Routes are assembled from shared UI components.
- Next.js app → Client state: uses · Client-side hooks and stores keep cart and conversation state.
- API routes → Domain services: delegates · Route handlers call domain services instead of containing business logic.
- Domain services → SmartCart adapter: calls · Quick mode and conversational flows go through the SmartCart adapter.
- Domain services → Website DB: reads/writes · Local product, order, and prediction concerns are served from the website runtime and database.
- SmartCart adapter → Express API: http · Website sends planning requests to the external SmartCart service over HTTP.
- Express API → Middleware: applies · All requests pass through tracing and optional auth middleware.
- Middleware → Cart controller: guards · Middleware runs before the cart controller handles requests.
- Cart controller → Planner run: starts · HTTP requests are mapped into a single planning run.
- Planner run → Pipeline stages: executes · Orchestrator coordinates the deterministic eight-stage pipeline.
- Pipeline stages → Retrieval stack: resolves · Pipeline uses conservative layered retrieval to find candidate products.
- Retrieval stack → Postgres: queries · Retrieval relies on Postgres features such as pgvector and pg_trgm.
- Planner run → Infra layer: depends on · Planning run depends on shared infra for persistence, caching, and generation.
- Infra layer → Postgres: persists · Postgres stores catalog, planner cache, logs, and knowledge bases.
- Infra layer → Redis: caches · Redis handles sessions and replayable responses.
- Infra layer → LLM providers: abstracts · LLM access is provider-agnostic behind a small abstraction.
- Offline scripts → Postgres: seeds · Offline jobs prepare catalog, embeddings, brands, domains, and evaluation data.