A Next.js-based conversational AI agent that helps users discover, research, and purchase Thorlabs photonics products. The system uses a filesystem-first retrieval approach with PageIndex Cloud and the Vercel AI SDK to provide accurate product information and enable e-commerce functionality.
Odin is an intelligent support agent that combines:
- Documentation Search: Query product manuals and datasheets using structured document trees
- Inventory Management: Real-time stock checking and product availability
- Shopping Cart: Full e-commerce functionality with account-based discounts
- Delivery Visualization: Animated map showing delivery routes from Berlin warehouse to customer
The system demonstrates a retrieval-first approach where the AI agent uses structured document nodes (from PageIndex Cloud) to answer questions, reducing hallucination and improving accuracy compared to traditional RAG systems.
- Product Discovery: Search across product documentation using natural language queries
- Technical Specifications: Get detailed specs, parameters, and technical data from product manuals
- Inventory Checking: Real-time stock levels and pricing for all products
- Documentation Navigation: Browse product documentation in a hierarchical sidebar (toggle with
Cmd+B/Ctrl+B) - Source Citations: Clickable links to specific document sections with popup previews
- Shopping Cart: Add products to cart with quantity management
- Account-Based Discounts: Automatic discount calculation (Academic 15%, Government 12%, Commercial 10%)
- Checkout Flow: Order processing with inventory validation
- Delivery Animation: Visual map showing delivery route from Berlin warehouse to destination
- Order History: Track orders in
order-sessions.json
- PDF Upload: Upload product PDFs via web interface
- Automatic Processing: Python scripts automatically extract document trees and generate summaries
- Tree Structure: Documents stored as hierarchical JSON trees with node IDs, titles, and summaries
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Frontend (Next.js) β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Chat UI β β Cart Sidebarβ β Docs Sidebar β β
β ββββββββ¬ββββββββ ββββββββ¬ββββββββ ββββββββ¬ββββββββ β
βββββββββββΌβββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API Gateway (/api/chat) β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Vercel AI SDK Agent (ToolLoopAgent) β β
β β ββββββββββββββ ββββββββββββββ ββββββββββββββ β β
β β β queryDoc β β checkInv β β addToCart β β β
β β β querySum β β purchase β β estimate β β β
β β ββββββββββββββ ββββββββββββββ ββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Data Layer β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Documentationβ β Inventory β β User Accountsβ β
β β (JSON trees)β β (JSON) β β (JSON) β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Frontend: Next.js App Router with React components
- Agent Runtime: Vercel AI SDK
ToolLoopAgentwith OpenAI GPT-4o - Retrieval: PageIndex Cloud for document processing and tree extraction
- Tools: TypeScript functions for document querying, inventory, cart management
- State Management: Zustand for cart state
- Maps: Mapbox GL for delivery visualization
- Node.js 18+ and pnpm (or npm/yarn)
- Python 3.8+ (for document processing scripts)
- OpenAI API key
- PageIndex API key (for PDF processing)
- Mapbox access token (for delivery map - optional)
-
Clone the repository
git clone https://github.com/OwnerOfJK/odin.git cd odin -
Install dependencies
pnpm install
-
Set up Python environment (for document processing)
cd app/python python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt
-
Configure environment variables
Create a
.env.localfile in the root directory:# Required OPENAI_API_KEY=sk-your-openai-key PAGEINDEX_API_KEY=your-pageindex-key # Optional (for delivery map) NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN=pk.your-mapbox-token
-
Start the development server
pnpm dev
-
Open your browser Navigate to http://localhost:3000
odin/
βββ app/
β βββ actions/ # Server actions
β β βββ getAllDocuments.ts # Fetch all documentation files
β β βββ getNode.ts # Get specific document node
β βββ api/ # API routes
β β βββ chat/ # Main chat endpoint
β β βββ checkout/ # Order processing
β β βββ accounts/ # User account management
β β βββ upload-product/ # PDF upload & processing
β βββ components/ # React components
β β βββ cart/ # Shopping cart components
β β βββ map/ # Map visualization
β β βββ modals/ # Modal dialogs
β β βββ Message.tsx # Chat message display
β β βββ ChatInput.tsx # Message input
β β βββ NodesSidebar.tsx # Documentation browser
β β βββ ShoppingCart.tsx # Cart container
β βββ data/ # Data files
β β βββ documentation/ # Product PDFs and JSON trees
β β βββ inventory.json # Product inventory
β β βββ user-accounts.json # Customer accounts
β β βββ order-sessions.json # Order history
β β βββ summary.json # Product summaries
β βββ hooks/ # React hooks
β β βββ useCart.ts # Cart operations
β β βββ useCheckout.ts # Checkout logic
β β βββ useMapAnimation.ts # Map animation
β βββ python/ # Python scripts
β β βββ scripts/
β β βββ build-tree-from-pdf.py # Extract document tree
β β βββ append-summary.py # Generate summaries
β β βββ summarize_tree.py # Summary generation
β βββ stores/ # State management
β β βββ cartStore.ts # Zustand cart store
β βββ tools/ # AI agent tools
β β βββ search-product-documentation.ts # Product documentation search
β β βββ search-product-summary.ts # Product summary search
β β βββ check-inventory.ts # Stock checking
β β βββ add-to-cart.ts # Add items to cart
β β βββ purchase-product.ts # Process purchases
β β βββ estimate-delivery.ts # Delivery estimates
β βββ utils/ # Utility functions
β βββ cartValidation.ts # Cart validation
β βββ discountCalculator.ts # Discount logic
β βββ stateCoordinates.ts # US state coordinates
βββ PRODUCT.md # Product specification
βββ SHOPPING_CART_README.md # Shopping cart documentation
βββ README.md # This file
The AI agent has access to the following tools:
| Tool | Description | Use Case |
|---|---|---|
searchProductDocumentationTool |
Search within specific product documentation | "What are the specifications for the DFB15TK laser?" |
searchProductSummariesTool |
Search product summaries to find relevant products | "What lasers do you have?" |
checkInventoryTool |
Check stock levels and pricing | "Is APD130A in stock?" |
addToCartTool |
Add products to shopping cart | "Add 2 APD130A to my cart" |
purchaseProductTool |
Process a purchase (usually via checkout UI) | Checkout flow |
estimateDeliveryTool |
Get delivery time estimates | "How long will delivery take?" |
manageAccountTool |
Manage user account information | Account operations |
Documentation is stored as hierarchical JSON trees in app/data/documentation/:
[
{
"title": "Chapter 1: Introduction",
"node_id": "0001",
"page_index": 1,
"summary": "Brief summary of the chapter",
"text": "Full chapter text...",
"nodes": [
{
"title": "1.1 Overview",
"node_id": "0002",
"page_index": 2,
"summary": "...",
"text": "..."
}
]
}
]Products are stored in app/data/inventory.json:
{
"products": [
{
"productCode": "APD130A",
"name": "Avalanche Photodetector, Si, 400-1000 nm",
"category": "APD130x-Avalanche-Photodetectors",
"stock": 45,
"price": 2499.00,
"currency": "USD",
"location": "Berlin Warehouse",
"restockDate": null
}
]
}User: "What lasers do you have available?"
Agent: [Searches summaries, checks inventory, presents table]
User: "What is the wavelength range of the DFB15TK laser?"
Agent: [Queries documentation, returns specs with source links]
User: "Add 2 APD130A detectors to my cart"
Agent: [Adds to cart, shows confirmation]
- Press
Cmd+B(Mac) orCtrl+B(Windows/Linux) to toggle documentation sidebar - Click on any node to view its content in a popup
- Navigate hierarchical structure with dropdowns
-
Create a new tool file in
app/tools/:import { tool } from 'ai'; import { z } from 'zod'; export const myTool = tool({ description: 'Tool description', inputSchema: z.object({ param: z.string(), }), execute: async ({ param }) => { // Tool logic return { result: '...' }; }, });
-
Export it from
app/tools/index.ts -
Add it to the agent in
app/api/chat/route.ts
- Upload PDF via the web interface (Admin β Upload Product)
- Or manually:
- Place PDF in
app/data/documentation/ - Run:
python app/python/scripts/build-tree-from-pdf.py <pdf_path> <product_name> <output_dir> - Run:
python app/python/scripts/append-summary.py <product_name> <tree_json_path> <summary_json_path>
- Place PDF in
Edit app/data/inventory.json and add product entries following the existing structure.
- Push code to GitHub
- Import project in Vercel
- Add environment variables in Vercel dashboard
- Deploy
pnpm build
pnpm start| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY |
Yes | OpenAI API key for the AI agent |
PAGEINDEX_API_KEY |
Yes | PageIndex Cloud API key for PDF processing |
NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN |
No | Mapbox token for delivery map visualization |
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is private and proprietary.
- Built with Next.js
- AI powered by Vercel AI SDK
- Document processing by PageIndex Cloud
- Maps by Mapbox
For detailed shopping cart documentation, see SHOPPING_CART_README.md
For product specifications, see PRODUCT.md