-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(examples): add minimal Next.js App Router chat example #10652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elunno
wants to merge
2
commits into
vercel:main
Choose a base branch
from
elunno:feat/next-basic-chat-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Next.js Basic Chat Example | ||
|
|
||
| A minimal Next.js App Router chat example using the Vercel AI SDK. | ||
|
|
||
| ## Features | ||
|
|
||
| - `@ai-sdk/react` → `useChat` hook for client-side chat | ||
| - `@ai-sdk/openai` + `streamText` for server streaming | ||
| - Edge runtime | ||
| - Clean and minimal UI | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Add your key (`AI_GATEWAY_API_KEY`) to `.env.local` in the **repo root**: | ||
|
|
||
| AI_GATEWAY_API_KEY=your_key_here | ||
|
|
||
|
|
||
| 2. Install & run: | ||
|
|
||
|
|
||
| pnpm install | ||
| pnpm dev --filter ai-examples-next-basic-chat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { openai } from "@ai-sdk/openai"; | ||
| import { streamText } from "ai"; | ||
|
|
||
| export const runtime = "edge"; | ||
|
|
||
| export async function POST(req: Request) { | ||
| const { messages } = await req.json(); | ||
|
|
||
| const result = streamText({ | ||
| model: openai("gpt-4o-mini"), | ||
| messages | ||
| }); | ||
|
|
||
| return result.toDataStreamResponse(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import type { ReactNode } from "react"; | ||
|
|
||
| export const metadata = { | ||
| title: "AI SDK – Next.js Basic Chat", | ||
| description: "Minimal chat example using Vercel AI SDK." | ||
| }; | ||
|
|
||
| export default function RootLayout({ children }: { children: ReactNode }) { | ||
| return ( | ||
| <html lang="en"> | ||
| <body | ||
| style={{ | ||
| margin: 0, | ||
| fontFamily: "system-ui, sans-serif", | ||
| backgroundColor: "#0b1120", | ||
| color: "#e2e8f0" | ||
| }} | ||
| > | ||
| {children} | ||
| </body> | ||
| </html> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| "use client"; | ||
|
|
||
| import { useChat } from "@ai-sdk/react"; | ||
| import { FormEvent } from "react"; | ||
|
|
||
| export default function Page() { | ||
| const { messages, input, handleInputChange, handleSubmit, isLoading, error } = | ||
| useChat({ | ||
| api: "/api/chat" | ||
| }); | ||
|
|
||
| const onSubmit = (e: FormEvent<HTMLFormElement>) => { | ||
| e.preventDefault(); | ||
| handleSubmit(e); | ||
| }; | ||
|
|
||
| return ( | ||
| <main | ||
| style={{ | ||
| maxWidth: 720, | ||
| margin: "0 auto", | ||
| padding: "2rem 1rem", | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| gap: "1.5rem", | ||
| minHeight: "100vh" | ||
| }} | ||
| > | ||
| <h1 style={{ fontSize: "1.6rem", margin: 0 }}>Next.js Basic Chat</h1> | ||
|
|
||
| <div | ||
| style={{ | ||
| flex: 1, | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| gap: "0.75rem", | ||
| border: "1px solid rgba(148,163,184,0.35)", | ||
| padding: "1rem", | ||
| borderRadius: 12, | ||
| backgroundColor: "#0f172a90", | ||
| maxHeight: "60vh", | ||
| overflowY: "auto" | ||
| }} | ||
| > | ||
| {messages.map(m => ( | ||
| <div | ||
| key={m.id} | ||
| style={{ | ||
| alignSelf: m.role === "user" ? "flex-end" : "flex-start", | ||
| backgroundColor: | ||
| m.role === "user" | ||
| ? "rgba(59,130,246,0.2)" | ||
| : "rgba(15,23,42,0.85)", | ||
| borderRadius: 8, | ||
| padding: "0.6rem 0.8rem", | ||
| maxWidth: "75%" | ||
| }} | ||
| > | ||
| <div | ||
| style={{ | ||
| opacity: 0.7, | ||
| fontSize: "0.75rem", | ||
| marginBottom: 4 | ||
| }} | ||
| > | ||
| {m.role === "user" ? "You" : "AI"} | ||
| </div> | ||
| {m.content} | ||
| </div> | ||
| ))} | ||
|
|
||
| {error && ( | ||
| <div style={{ color: "#f87171", fontSize: "0.9rem" }}> | ||
| {String(error.message ?? error)} | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| <form | ||
| onSubmit={onSubmit} | ||
| style={{ | ||
| display: "flex", | ||
| gap: "0.75rem", | ||
| alignItems: "center" | ||
| }} | ||
| > | ||
| <input | ||
| value={input} | ||
| onChange={handleInputChange} | ||
| placeholder="Ask something..." | ||
| style={{ | ||
| flex: 1, | ||
| padding: "0.7rem 1rem", | ||
| borderRadius: 999, | ||
| border: "1px solid rgba(148,163,184,0.5)", | ||
| backgroundColor: "#0f172a", | ||
| color: "#e2e8f0" | ||
| }} | ||
| /> | ||
|
|
||
| <button | ||
| type="submit" | ||
| disabled={isLoading || !input.trim()} | ||
| style={{ | ||
| padding: "0.7rem 1.1rem", | ||
| borderRadius: 999, | ||
| background: | ||
| "linear-gradient(to right, rgb(59,130,246), rgb(56,189,248))", | ||
| border: "none", | ||
| fontWeight: 600, | ||
| cursor: isLoading ? "default" : "pointer", | ||
| opacity: isLoading || !input.trim() ? 0.5 : 1 | ||
| }} | ||
| > | ||
| {isLoading ? "..." : "Send"} | ||
| </button> | ||
| </form> | ||
| </main> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** @type {import('next').NextConfig} */ | ||
| const nextConfig = { | ||
| experimental: { | ||
| reactCompiler: false | ||
| } | ||
| }; | ||
|
|
||
| export default nextConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "ai-examples-next-basic-chat", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "scripts": { | ||
| "dev": "next dev", | ||
| "build": "next build", | ||
| "start": "next start" | ||
| }, | ||
| "dependencies": { | ||
| "ai": "workspace:*", | ||
| "@ai-sdk/react": "workspace:*", | ||
| "@ai-sdk/openai": "workspace:*", | ||
| "next": "15.1.0", | ||
| "react": "18.3.1", | ||
| "react-dom": "18.3.1" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.with-examples.json", | ||
| "compilerOptions": { | ||
| "strict": true | ||
| }, | ||
| "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], | ||
| "exclude": ["node_modules"] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.