Skip to content

Commit 345daeb

Browse files
committed
first commit
0 parents  commit 345daeb

File tree

187 files changed

+39656
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+39656
-0
lines changed

.cursor/rules/ultracite.mdc

Lines changed: 333 additions & 0 deletions
Large diffs are not rendered by default.

.env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
AUTH_SECRET=****
2+
3+
# AI Gateway API Key - Required for non-Vercel deployments to access AI models
4+
# Generate using: openssl rand -base64 32
5+
AI_GATEWAY_API_KEY=****
6+
7+
# Blob storage token for file uploads - Uses Vercel Blob storage API
8+
BLOB_READ_WRITE_TOKEN=****
9+
10+
# MongoDB connection string
11+
# For local development with Docker: mongodb://admin:admin_password@localhost:27018/gitnation?authSource=admin
12+
# For production: use MongoDB Atlas or your hosted MongoDB instance
13+
MONGODB_URI=mongodb://admin:admin_password@localhost:27018/gitnation?authSource=admin
14+
MONGODB_DB=gitnation

.github/workflows/lint.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Lint
2+
on:
3+
push:
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-22.04
8+
strategy:
9+
matrix:
10+
node-version: [20]
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Use Node.js ${{ matrix.node-version }}
14+
uses: actions/setup-node@v4
15+
with:
16+
node-version: ${{ matrix.node-version }}
17+
cache: "npm"
18+
- name: Install dependencies
19+
run: npm ci
20+
- name: Run lint
21+
run: npm run lint

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
node_modules
5+
.pnp
6+
.pnp.js
7+
8+
# testing
9+
coverage
10+
11+
# next.js
12+
.next/
13+
out/
14+
build
15+
16+
# misc
17+
.DS_Store
18+
*.pem
19+
logs
20+
21+
# debug
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*
25+
.pnpm-debug.log*
26+
27+
28+
# local env files
29+
.env.local
30+
.env.development.local
31+
.env.test.local
32+
.env.production.local
33+
34+
# turbo
35+
.turbo
36+
37+
.env
38+
.vercel
39+
.env*.local
40+
41+
# Playwright
42+
/test-results/
43+
/playwright-report/
44+
/blob-report/
45+
/playwright/*
46+
47+
# Snyk Security Extension - AI Rules (auto-generated)
48+
.github/instructions/snyk_rules.instructions.md
49+
50+
# Personal notes
51+
TODO.md
52+
WORKING_TODO.md

.mcp.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"mcpServers": {
3+
"context7": {
4+
"command": "npx",
5+
"args": ["-y", "@upstash/context7-mcp"],
6+
"env": {
7+
"CONTEXT7_API_KEY": "ctx7sk-1d829fe1-62b2-4697-b7f4-673ae5047efd"
8+
}
9+
},
10+
"puppeteer": {
11+
"command": "npx",
12+
"args": ["-y", "puppeteer-mcp-server"],
13+
"env": {}
14+
},
15+
"sequential-thinking": {
16+
"command": "npx",
17+
"args": ["-y", "@modelcontextprotocol/server-sequential-thinking"]
18+
},
19+
"mongodb": {
20+
"command": "npx",
21+
"args": ["-y", "mongodb-mcp-server@latest", "--readOnly"],
22+
"env": {
23+
"MDB_MCP_CONNECTION_STRING": "mongodb://admin:admin_password@localhost:27018/gitnation?authSource=admin"
24+
}
25+
}
26+
}
27+
}

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["biomejs.biome"]
3+
}

.vscode/settings.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
3+
"[javascript]": {
4+
"editor.defaultFormatter": "biomejs.biome"
5+
},
6+
"[typescript]": {
7+
"editor.defaultFormatter": "biomejs.biome"
8+
},
9+
"[javascriptreact]": {
10+
"editor.defaultFormatter": "biomejs.biome"
11+
},
12+
"[typescriptreact]": {
13+
"editor.defaultFormatter": "biomejs.biome"
14+
},
15+
"[json]": {
16+
"editor.defaultFormatter": "biomejs.biome"
17+
},
18+
"[jsonc]": {
19+
"editor.defaultFormatter": "biomejs.biome"
20+
},
21+
"[css]": {
22+
"editor.defaultFormatter": "biomejs.biome"
23+
},
24+
"[graphql]": {
25+
"editor.defaultFormatter": "biomejs.biome"
26+
},
27+
"typescript.tsdk": "node_modules/typescript/lib",
28+
"editor.formatOnSave": true,
29+
"editor.formatOnPaste": true,
30+
"emmet.showExpandedAbbreviation": "never",
31+
"editor.codeActionsOnSave": {
32+
"source.fixAll.biome": "explicit",
33+
"source.organizeImports.biome": "explicit"
34+
}
35+
}

CHAPTER-0.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# Chapter 0: The Starting Point
2+
3+
Welcome to the AI Chatbot Workshop! In this chapter, we'll explore the foundation of our application - a basic chat interface powered by Next.js and the AI SDK.
4+
5+
## Learning Objectives
6+
7+
By the end of this chapter, you'll understand:
8+
- The basic project structure
9+
- How the chat API route works
10+
- How streaming responses work with the AI SDK
11+
- The message format and data flow
12+
13+
## Project Structure Overview
14+
15+
```
16+
ai-chatbot/
17+
├── app/
18+
│ ├── (auth)/ # Authentication routes
19+
│ │ ├── auth.ts # NextAuth configuration
20+
│ │ ├── login/ # Login page
21+
│ │ └── register/ # Registration page
22+
│ └── (chat)/ # Main chat application
23+
│ ├── api/
24+
│ │ └── chat/ # Chat streaming endpoint
25+
│ │ └── route.ts
26+
│ ├── page.tsx # Main chat page
27+
│ └── layout.tsx # Chat layout
28+
├── components/ # React components
29+
├── lib/
30+
│ ├── ai/ # AI configuration
31+
│ │ ├── providers.ts # Model setup
32+
│ │ └── prompts.ts # System prompts
33+
│ └── db/ # Database layer
34+
├── hooks/ # React hooks
35+
└── docker/ # Docker configuration
36+
```
37+
38+
## The Chat API Route
39+
40+
The heart of the application is `/app/(chat)/api/chat/route.ts`. This is where messages are sent to the AI and responses are streamed back.
41+
42+
### Basic Chat Flow
43+
44+
```typescript
45+
// app/(chat)/api/chat/route.ts (simplified)
46+
import { streamText } from "ai";
47+
import { myProvider } from "@/lib/ai/providers";
48+
import { systemPrompt } from "@/lib/ai/prompts";
49+
50+
export async function POST(request: Request) {
51+
const { messages } = await request.json();
52+
53+
// Stream the AI response
54+
const result = streamText({
55+
model: myProvider.languageModel("chat-model"),
56+
system: systemPrompt(),
57+
messages,
58+
});
59+
60+
return result.toDataStreamResponse();
61+
}
62+
```
63+
64+
### Key Concepts
65+
66+
1. **`streamText`**: The AI SDK function that sends messages to the model and streams the response token by token.
67+
2. **`myProvider`**: Our configured AI provider (Claude Haiku via AI Gateway).
68+
3. **`systemPrompt`**: Instructions that tell the AI how to behave.
69+
4. **`toDataStreamResponse`**: Converts the stream into a format the frontend can consume.
70+
71+
## How Streaming Works
72+
73+
When you send a message:
74+
75+
```
76+
┌─────────────────────────────────────────────────────────┐
77+
│ 1. User types message in chat input │
78+
│ ↓ │
79+
│ 2. Frontend sends POST to /api/chat │
80+
│ ↓ │
81+
│ 3. streamText sends messages to AI model │
82+
│ ↓ │
83+
│ 4. AI generates response token by token │
84+
│ ↓ │
85+
│ 5. Each token streams back to frontend │
86+
│ ↓ │
87+
│ 6. UI updates in real-time as tokens arrive │
88+
└─────────────────────────────────────────────────────────┘
89+
```
90+
91+
## The Frontend Chat Hook
92+
93+
The frontend uses `useChat` from the AI SDK React package:
94+
95+
```typescript
96+
// Simplified usage in a chat component
97+
import { useChat } from "@ai-sdk/react";
98+
99+
export function Chat() {
100+
const { messages, input, handleSubmit, handleInputChange } = useChat();
101+
102+
return (
103+
<div>
104+
{messages.map((message) => (
105+
<div key={message.id}>
106+
<strong>{message.role}:</strong> {message.content}
107+
</div>
108+
))}
109+
<form onSubmit={handleSubmit}>
110+
<input value={input} onChange={handleInputChange} />
111+
<button type="submit">Send</button>
112+
</form>
113+
</div>
114+
);
115+
}
116+
```
117+
118+
The `useChat` hook handles:
119+
- Managing message history
120+
- Sending messages to the API
121+
- Streaming response updates
122+
- Input state management
123+
124+
## Message Format
125+
126+
Messages follow this structure:
127+
128+
```typescript
129+
type Message = {
130+
id: string;
131+
role: "user" | "assistant" | "system";
132+
content: string;
133+
// Can also contain parts for multimodal content
134+
parts?: MessagePart[];
135+
};
136+
```
137+
138+
## The System Prompt
139+
140+
The system prompt shapes the AI's personality and behavior:
141+
142+
```typescript
143+
// lib/ai/prompts.ts
144+
export const systemPrompt = () => `
145+
You are a helpful AI assistant. Be concise and helpful.
146+
Today's date is ${new Date().toLocaleDateString()}.
147+
`;
148+
```
149+
150+
## Exercise: Trace a Message
151+
152+
1. Start the development server: `npm run dev`
153+
2. Open the browser console (F12)
154+
3. Send a message like "Hello!"
155+
4. Watch the Network tab to see the streaming response
156+
5. Notice how the text appears token by token
157+
158+
## What's Next
159+
160+
In Chapter 1, we'll add our first **tool** - giving the AI the ability to do more than just respond with text. We'll start with a simple weather tool that demonstrates how AI can call functions to retrieve information.
161+
162+
## Key Files to Explore
163+
164+
Before moving on, familiarize yourself with these files:
165+
166+
| File | Purpose |
167+
|------|---------|
168+
| `app/(chat)/api/chat/route.ts` | Main streaming endpoint |
169+
| `lib/ai/providers.ts` | AI model configuration |
170+
| `lib/ai/prompts.ts` | System prompts |
171+
| `components/chat.tsx` | Chat UI component |
172+
| `components/message.tsx` | Message rendering |
173+
174+
## Running the Application
175+
176+
```bash
177+
# Start MongoDB
178+
npm run docker:up
179+
180+
# Start the dev server
181+
npm run dev
182+
183+
# Visit http://localhost:3000
184+
```
185+
186+
You should see a chat interface. Send a message and watch the AI respond!

0 commit comments

Comments
 (0)