Skip to content

The Open Standard for Generative UI

2.2 KB
Raw Blame

OpenUI Svelte Chat

A chat application built with SvelteKit, Vercel AI SDK, and @openuidev/svelte-lang — demonstrating how to render structured LLM output as live Svelte components.

How it works

  1. User sends a message via the chat input
  2. Server streams a response using the Vercel AI SDK with OpenAI, guided by a system prompt written in openui-lang syntax
  3. @openuidev/svelte-lang Renderer parses the streaming openui-lang text and renders it as Svelte components in real time
  4. Tool calls (weather, stocks, math, web search) are displayed inline with status indicators

Setup

Prerequisites

Install dependencies

From this example directory:

pnpm install --ignore-workspace

Configure environment

cp .env.example .env

Edit .env and add your OpenUI Cloud API key:

THESYS_API_KEY=sk-th-...

Run

pnpm dev

Open http://localhost:5173.

Project structure

src/
├── routes/
│   ├── +page.svelte           # Chat UI with AI SDK Chat class + OpenUI Renderer
│   ├── +layout.svelte         # Root layout (imports Tailwind)
│   ├── +layout.ts             # Disables SSR (client-side rendering)
│   └── api/chat/+server.ts    # AI SDK streaming endpoint
├── lib/
│   ├── library.ts             # OpenUI component definitions (Stack, Card, TextContent, Button)
│   ├── tools.ts               # AI tool definitions (weather, stocks, math, search)
│   └── components/            # Svelte component renderers
│       ├── Stack.svelte
│       ├── Card.svelte
│       ├── TextContent.svelte
│       └── Button.svelte

Adding components

  1. Create a Svelte component in src/lib/components/
  2. Define it with defineComponent() in src/lib/library.ts
  3. Export it from src/lib/library.ts; the API builds the system prompt from the library at runtime

See the @openuidev/svelte-lang README for the full API.

Verify

pnpm verify