Skip to content
AI Assistant - HaloLight

AI Assistant โ€‹

HaloLight AI service is built with Hono + LangChain.js, providing RAG retrieval-augmented generation, action execution, and multi-model automatic fallback.

Live Preview: Internal API service (no standalone demo)

GitHub: https://github.com/halolight/halolight-ai

Tech Stack โ€‹

TechnologyVersionDescription
HonolatestLightweight HTTP framework
LangChain.jslatestLLM orchestration + RAG pipeline
pgvectorlatestPostgreSQL vector search extension
Drizzle ORMlatestTypeScript ORM & migrations
PostgreSQL14+Persistence and vector storage
Node.js22+Runtime
ZodlatestData validation

Directory Structure โ€‹

halolight-ai/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ index.ts                    # Application entry
โ”‚   โ”œโ”€โ”€ routes/                     # API routes
โ”‚   โ”‚   โ”œโ”€โ”€ chat.ts                 # Chat interface
โ”‚   โ”‚   โ”œโ”€โ”€ actions.ts              # Action execution
โ”‚   โ”‚   โ”œโ”€โ”€ history.ts              # History records
โ”‚   โ”‚   โ””โ”€โ”€ knowledge.ts            # Knowledge base management
โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ”œโ”€โ”€ llm/                    # LLM service layer
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ openai.ts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ anthropic.ts
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ factory.ts          # Model factory (auto fallback)
โ”‚   โ”‚   โ”œโ”€โ”€ rag/                    # RAG pipeline
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ embeddings.ts       # Document chunking & embedding
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ retriever.ts        # Vector retrieval
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ pipeline.ts         # RAG pipeline
โ”‚   โ”‚   โ”œโ”€โ”€ actions/                # Action execution system
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ executor.ts         # Action executor
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ registry.ts         # Action registry
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ permissions.ts      # Permission validation
โ”‚   โ”‚   โ””โ”€โ”€ memory/
โ”‚   โ”‚       โ””โ”€โ”€ conversation.ts     # Conversation memory management
โ”‚   โ”œโ”€โ”€ db/
โ”‚   โ”‚   โ”œโ”€โ”€ schema.ts               # Drizzle Schema (with pgvector)
โ”‚   โ”‚   โ””โ”€โ”€ client.ts               # Database client
โ”‚   โ”œโ”€โ”€ middleware/
โ”‚   โ”‚   โ”œโ”€โ”€ auth.ts                 # Authentication middleware
โ”‚   โ”‚   โ””โ”€โ”€ tenant.ts               # Tenant isolation
โ”‚   โ””โ”€โ”€ types/
โ”‚       โ””โ”€โ”€ index.ts                # TypeScript type definitions
โ”œโ”€โ”€ drizzle/                        # Migration files
โ”œโ”€โ”€ drizzle.config.ts
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
โ””โ”€โ”€ package.json

Quick Start โ€‹

Prerequisites โ€‹

  • Node.js >= 22.0.0
  • PostgreSQL >= 14 (with pgvector extension)
  • At least one LLM provider API Key configured

Installation โ€‹

bash
# Clone repository
git clone https://github.com/halolight/halolight-ai.git
cd halolight-ai

# Install dependencies
pnpm install

Environment Variables โ€‹

bash
cp .env.example .env

Key configurations:

env
# Database
DATABASE_URL=postgresql://user:password@localhost:5432/halolight_ai

# LLM Providers (configure at least one)
OPENAI_API_KEY=sk-...
# or
ANTHROPIC_API_KEY=sk-ant-...
# or
AZURE_OPENAI_API_KEY=...

# RAG Configuration
CHUNK_SIZE=1000              # Document chunk size
CHUNK_OVERLAP=200            # Chunk overlap
RETRIEVAL_TOP_K=5            # Retrieval count

# Conversation Configuration
MAX_CONVERSATION_HISTORY=20  # Conversation history length
ENABLE_STREAMING=true        # Enable streaming response
ENABLE_AUDIT_LOG=true        # Enable audit logging

# Production
JWT_SECRET=your-secret-key
CORS_ORIGINS=https://your-domain.com

Initialize Database โ€‹

bash
# Generate migration files
pnpm db:generate

# Run migrations
pnpm db:migrate

# Or push schema directly (development)
pnpm db:push

# Open Drizzle Studio
pnpm db:studio

Start Development Server โ€‹

bash
pnpm dev

Service will start at http://localhost:3000.

Production Build โ€‹

bash
pnpm build
pnpm start

Core Features โ€‹

1. Multi-Model Support โ€‹

The system automatically detects available LLM providers and falls back by priority:

Azure OpenAI (1) โ†’ OpenAI (2) โ†’ Anthropic (3) โ†’ Ollama (4)

2. RAG Knowledge Base โ€‹

StepDescriptionConfiguration
Document ChunkingRecursiveCharacterTextSplitter1000 chars, 200 overlap
Vector EmbeddingOpenAI Embeddingstext-embedding-3-small
Vector Storagepgvector1536 dimensions
RetrievalCosine similarityTop-K (default 5)
Context InjectionInject retrieval results into LLM prompt-

3. Streaming Response โ€‹

Enable SSE streaming output to reduce first-token latency:

bash
POST /api/ai/chat/stream
Content-Type: application/json

{
  "message": "Hello",
  "streaming": true
}

4. Permission Control โ€‹

Role-based access control (RBAC):

RolePermission Level
super_adminHighest permission
adminAdmin permission
userRegular user
guestGuest

Sensitive operations require confirmation (_confirmed: true).

5. Conversation Memory โ€‹

  • Stored in conversations and messages tables
  • Retains last 20 messages by default
  • Supports multi-turn conversation context

6. Tenant Isolation โ€‹

All data operations are based on TenantContext:

typescript
interface TenantContext {
  tenantId: string;
  userId: string;
  role: UserRole;
}

Extracted from request headers:

  • X-Tenant-ID
  • X-User-ID
  • X-User-Role

API Endpoints โ€‹

Health Check โ€‹

bash
GET /health
GET /health/ready
GET /api/ai/info

Chat โ€‹

bash
# Send message
POST /api/ai/chat
Content-Type: application/json

{
  "message": "Hello, introduce HaloLight",
  "conversationId": "uuid",    // optional
  "includeContext": true,
  "maxContextDocs": 5
}

# Streaming response
POST /api/ai/chat/stream

Action Execution โ€‹

bash
# Execute action
POST /api/ai/actions/execute
Content-Type: application/json

{
  "action": "query_users",
  "params": {
    "role": "admin",
    "limit": 10
  }
}

# Get available actions
GET /api/ai/actions/available

# Get action details
GET /api/ai/actions/:name

History โ€‹

bash
GET /api/ai/history?limit=10
GET /api/ai/history/:id
DELETE /api/ai/history/:id
PATCH /api/ai/history/:id

Knowledge Base โ€‹

bash
# Import document
POST /api/ai/knowledge/ingest
Content-Type: application/json

{
  "content": "Document content...",
  "metadata": {
    "title": "Document Title",
    "category": "Technical Documentation"
  },
  "source": "manual",
  "sourceId": "doc-001"
}

# Batch import
POST /api/ai/knowledge/batch-ingest

# List documents
GET /api/ai/knowledge?limit=50&offset=0

# Delete document
DELETE /api/ai/knowledge/:id

Authentication โ€‹

All /api/* endpoints require the following headers (optional in development):

X-Tenant-ID: your-tenant-id
X-User-ID: your-user-id
X-User-Role: admin | user | guest

Deployment โ€‹

Docker โ€‹

bash
# Build image
docker build -t halolight-ai .

# Run container
docker run -p 3000:3000 \
  -e DATABASE_URL=postgresql://... \
  -e OPENAI_API_KEY=sk-... \
  halolight-ai

Docker Compose โ€‹

bash
docker-compose up -d

Production Requirements โ€‹

  • DATABASE_URL: PostgreSQL connection string
  • NODE_ENV=production
  • At least one LLM provider API Key
  • JWT_SECRET: Secret key for authentication
  • CORS_ORIGINS: Allowed cross-origin sources

Troubleshooting โ€‹

Database Connection Failed โ€‹

bash
# Check if PostgreSQL is running
psql $DATABASE_URL -c "SELECT 1"

# Check pgvector extension
psql $DATABASE_URL -c "CREATE EXTENSION IF NOT EXISTS vector"

LLM Provider Errors โ€‹

bash
# Check available providers
curl http://localhost:3000/api/ai/info

Inaccurate Vector Retrieval โ€‹

  • Increase RETRIEVAL_TOP_K value
  • Adjust CHUNK_SIZE and CHUNK_OVERLAP
  • Use hybrid retrieval (vector + keyword)