Skip to content

Repository files navigation

ReScript + Next.js Chat Template

An OpenAI-compatible chat app written in ReScript on the Next.js 16 App Router, using Bun and Tailwind CSS v4.

The UI streams tokens from POST /api/chat. That route calls ${OPENAI_BASE_URL}/chat/completions, so it works with OpenAI, OpenRouter, Groq, Ollama, Azure-compatible proxies, vLLM, and other Chat Completions servers.

Without OPENAI_API_KEY or OPENAI_BASE_URL, the API streams a local demo response so you can try the UI immediately.

Getting Started

Install dependencies:

bun install

Copy the env file. Leave the key and base URL empty to keep demo mode; fill them in for a real model:

cp .env.example .env.local

Compile ReScript and start the development server:

bun run res:build
bun dev

For ReScript watch mode (auto-recompile on save), run in a separate terminal:

bun run res:dev

Open http://localhost:3000.

Environment

Variable Default Notes
OPENAI_API_KEY unset Bearer token. Optional for local servers that skip auth.
OPENAI_BASE_URL unset (code default https://api.openai.com/v1) Must include /v1 when the provider uses it.
OPENAI_MODEL unset (code default gpt-4o-mini) Sent as model in the Chat Completions body.
OPENAI_SYSTEM_PROMPT unset Prepended as a system message on the server.

If both OPENAI_API_KEY and OPENAI_BASE_URL are unset, the route runs in demo mode.

Provider examples

OpenAI:

OPENAI_API_KEY=sk-...
OPENAI_BASE_URL=https://api.openai.com/v1
OPENAI_MODEL=gpt-4o-mini

OpenRouter:

OPENAI_API_KEY=sk-or-...
OPENAI_BASE_URL=https://openrouter.ai/api/v1
OPENAI_MODEL=openai/gpt-4o-mini

Ollama:

OPENAI_BASE_URL=http://localhost:11434/v1
OPENAI_MODEL=llama3.2
OPENAI_API_KEY=ollama

Project Structure

src/
├── app/
│   ├── layout.res          # Root layout (fonts, metadata, CSS)
│   ├── page.res            # Home page
│   ├── Chat.res            # Client chat UI
│   ├── globals.css         # Tailwind CSS + theme variables
│   └── api/chat/route.res  # GET config + POST streaming completions
├── chat/
│   ├── Message.res         # Message types and JSON decoding
│   ├── OpenAI.res          # OpenAI-compatible client + demo stream
│   └── Sse.res             # SSE parser for Chat Completions chunks
└── bindings/
    ├── Next.res            # Metadata (add Next APIs as needed)
    └── WebApi.res          # fetch, streams, abort

Scripts

Command Description
bun dev Start development server
bun run build Compile ReScript + build for production
bun run res:build Compile ReScript files
bun run res:dev ReScript watch mode
bun run res:clean Clean ReScript build artifacts
bun run lint Run Biome linter and formatter checks
bun run format Auto-format code with Biome

Learn More

About

OpenAI-compatible chat template for Next.js 16 App Router, ReScript, and Bun

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages