Skip to content

Repository files navigation

CodeBox Server

Express.js API for CodeBox — user authentication, code project CRUD, and LangChain-powered AI code explanation.

Setup

npm install
npm run dev

Environment Variables

VariableRequiredDescription
PORTNoServer port (default 8000)
MONGODB_URIYesMongoDB connection string
JWT_SECRETYesJWT signing secret
PINECONE_API_KEYYesPinecone API key
PINECONE_INDEXNoIndex name (default codebox-code-chunks)
PINECONE_CLOUDNoPinecone cloud provider (default aws)
PINECONE_REGIONNoPinecone region (default us-east-1)
AI_API_KEYYesOpenAI-compatible API key
AI_MODELYesChat model name
AI_BASE_URLYesOpenAI-compatible base URL
EMBEDDING_MODELNoEmbedding model (default text-embedding-3-small)
EMBEDDING_DIMENSIONNoVector dimension (default 1536, use 1024 for NVIDIA embed)
CHUNK_SIZENoMax characters per chunk (default 800)
CHUNK_OVERLAPNoOverlap for line-based splits (default 120)
RETRIEVAL_TOP_KNoCandidates per retriever (default 6)
RERANK_TOP_KNoFinal chunks after rerank (default 4)

Chat Routes

All routes require Authorization: Bearer <JWT>.

POST /api/chat

Send a message and receive an AI explanation.

Body

{
"message": "Explain this code",
"codeId": "optional-saved-code-id",
"sessionId": "optional-existing-session",
"codeSnippet": "optional live editor code",
"language": "javascript"
}

POST /api/chat/index

Chunk and vector-index a saved code project into Pinecone.

Body:{ "codeId": "..." }

GET /api/chat/sessions

List chat sessions for the authenticated user.

GET /api/chat/messages?sessionId=...

Get all messages in a session.

GET /api/chat/chunks?codeId=...

View indexed chunks for a code project.

Project Structure

codebox-server/
├── controllers/chatController.js # Chat HTTP handlers
├── models/
│ ├── CodeChunk.model.js # Indexed code chunks
│ ├── ChatSession.model.js # Chat sessions
│ └── ChatMessage.model.js # Messages + retrieval metadata
├── services/
│ ├── ai/config.js # LLM + embedding clients
│ ├── chunking.js # Semantic code chunking
│ ├── indexing.js # Index pipeline
│ ├── pineconeStore.js # Pinecone vector store
│ ├── chat/chain.js # LangChain orchestration
│ └── retrieval/hybridRetriever.js # BM25 + vector + rerank
└── routers/route.js

Hybrid Retrieval Pipeline

  1. Chunk saved code into functions, classes, imports (line fallback with overlap)
  2. Embed chunks and store in Pinecone; metadata in MongoDB
  3. On chat query:
    • BM25 search over in-memory chunk documents
    • Vector similarity search in Pinecone (filtered by codeId)
    • Reciprocal Rank Fusion to merge results
    • LLM reranking to pick the best context
  4. LangChain prompt chain sends context + history to the LLM

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages