A minimalist, drop-in AI chat widget for Svelte 5.
Because building chat interfaces from scratch is a pain.
It's not a complex library you install. It's a nicely crafted component you copy and paste into your project (shadcn/ui style).
I wanted something that looked premium, worked perfectly on mobile, and supported streaming out of the box. So I built this using Svelte 5 Runes and the Vercel AI SDK.
$state, $effect, and the new goodies.You just need the AI SDK and a few icons.
npm install ai @ai-sdk/svelte marked zod Lucide-svelte
Grab the src/lib/components/ai-chat folder from this repo and drop it into your codebase.
Create a standard API route at src/routes/api/chat/+server.ts:
import { streamText } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';
import { env } from '$env/dynamic/private';
const openai = createOpenAI({ apiKey: env.OPENAI_API_KEY });
export const POST = async ({ request }) => {
const { messages } = await request.json();
const result = streamText({
model: openai('gpt-4o'),
messages,
});
return result.toUIMessageStreamResponse();
};
Now just use it like any other component.
<script>
import { Chat } from "$lib/components/ai-chat";
</script>
<Chat
api="/api/chat"
title="Assistant"
/>
If you find this useful, please star the repo! It helps more people find it.
License: MIT. Go build something cool.