Skip to content

Commit b65ba69

Browse files
committed
feat: production hardening — tests, repository pattern, observability, and security fixes
P0 security fixes: - Fix CSRF origin bypass (endsWith → hostname comparison) - Add 30s AbortController timeout to AI fetch requests - Return 504 on timeout instead of generic 500 P1 engineering: - Add Vitest + 46 unit tests across 4 modules (progress, gamification, achievements, adaptive) - Add CI test step to GitHub Actions workflow - Create Repository abstraction (LocalStorage/Memory/Server implementations) - Refactor progress.ts and projects.ts to use Repository pattern - Add server-side /api/storage endpoint with auth and rate limiting - Add /api/analytics endpoint with rate limiting and event type validation - Generate PWA icons (192px, 512px) with brand colors - Configure Next.js standalone output for Docker P2 observability + DevOps: - Add structured JSON logger (lib/logger.ts) - Add user behavior analytics tracking (lib/analytics.ts) - Add AI provider fallback — auto-switch to backup on failure - Add Docker multi-stage build + docker-compose.yml - Add .env.example with all new environment variables Documentation: - Update all 4 READMEs (EN, ZH-CN, ZH-Hant, JA) with new features and Docker instructions - Update SETUP_GUIDE.md with API auth config and Docker section Verification: - TypeScript: zero errors - Tests: 4 files / 46 tests passed - Build: Next.js production build successful - Security: reviewed, no hardcoded secrets
1 parent 695e273 commit b65ba69

29 files changed

Lines changed: 6912 additions & 3702 deletions

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ jobs:
2222
run: npm ci
2323
- name: Type check
2424
run: npx tsc --noEmit
25+
- name: Test
26+
run: npm test
2527
- name: Build
2628
run: npm run build

Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
FROM node:22-alpine AS deps
2+
WORKDIR /app
3+
COPY package.json package-lock.json ./
4+
RUN npm ci --omit=dev
5+
6+
FROM node:22-alpine AS builder
7+
WORKDIR /app
8+
COPY package.json package-lock.json ./
9+
RUN npm ci
10+
COPY . .
11+
ENV NEXT_TELEMETRY_DISABLED=1
12+
RUN npm run build
13+
14+
FROM node:22-alpine AS runner
15+
WORKDIR /app
16+
ENV NODE_ENV=production
17+
ENV NEXT_TELEMETRY_DISABLED=1
18+
19+
RUN addgroup --system --gid 1001 nodejs
20+
RUN adduser --system --uid 1001 nextjs
21+
USER nextjs
22+
23+
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
24+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
25+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
26+
27+
EXPOSE 3000
28+
ENV PORT=3000
29+
CMD ["node", "server.js"]

README.ja.md

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44

55
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
66
[![Next.js](https://img.shields.io/badge/Next.js-16-black)](https://nextjs.org/)
7-
[![AI SDK](https://img.shields.io/badge/AI%20SDK-multi--model-purple)](https://sdk.vercel.ai/)
7+
[![CI](https://github.com/sixtdreanight/vibe-coding-agent/actions/workflows/ci.yml/badge.svg)](https://github.com/sixtdreanight/vibe-coding-agent/actions/workflows/ci.yml)
88

9-
ゼロから学べる AI アシストプログラミング(Vibe Coding)のインタラクティブな学習サイトです。12 のレッスンで段階的に学習でき、AI チューターが組み込まれており、Prompt Playground で直接実行可能なコードを生成できます
9+
ゼロから学べる AI アシストプログラミング(Vibe Coding)のインタラクティブな学習サイトです。7 章 22 レッスンで段階的に学習でき、AI チューター、ゲーミフィケーションシステム、コードを直接生成できる Prompt Playground を備えています
1010

1111
---
1212

1313
## 機能
1414

15-
- **体系的なカリキュラム** — 12 のレッスンで概念理解から実践まで
16-
- **AI 学習アシスタント** — 組み込みの AI チューターにいつでも質問可能
17-
- **Prompt Playground** — 説明を入力するだけで実行可能なコードを生成
15+
- **22 の体系的なレッスン** — 概念から公開までの 7 章構成
16+
- **AI 学習アシスタント** — ソクラテス式誘導モード + 直接回答モード、ストリーミング応答
17+
- **Prompt Playground** — 説明を入力するだけで実行可能な HTML/CSS/JS を生成
18+
- **ゲーミフィケーション** — XP、レベル、連続学習日数、10 種類の達成バッジ
19+
- **コード演習** — ヒントと自動チェック付きのインタラクティブ演習
20+
- **学習ダッシュボード** — 進捗追跡、ヒートマップ、スキルレーダー
21+
- **PWA 対応** — インストール可能、オフライン動作対応
1822
- **マルチモデル対応** — Claude、OpenAI、DeepSeek 及び任意の OpenAI 互換インターフェース
23+
- **AI フォールバック** — メインプロバイダー障害時に自動でバックアップに切替
1924

2025
## クイックスタート
2126

@@ -36,66 +41,112 @@ npm run dev
3641

3742
http://localhost:3000 にアクセスして学習を開始してください。
3843

39-
> 初心者向けの詳細なセットアップガイド[SETUP_GUIDE.md](./SETUP_GUIDE.md)
44+
> 初心者向け詳細セットアップガイド[SETUP_GUIDE.md](./SETUP_GUIDE.md)
4045
41-
## マルチモデルの設定
46+
### Docker
4247

43-
3 つの AI プロバイダーを `.env` で設定できます:
48+
```bash
49+
docker compose up -d
50+
```
51+
52+
## 設定
4453

45-
### DeepSeek(推奨、国内から直接アクセス可能)
54+
### 必須
4655

4756
```env
57+
# API 認証キー(ランダムな文字列を生成)
58+
AI_API_AUTH_TOKEN=あなたの秘密キー
59+
60+
# メイン AI プロバイダー
4861
AI_PROVIDER=openai-compatible
4962
AI_MODEL=deepseek-chat
5063
AI_API_KEY=sk-...
5164
AI_BASE_URL=https://api.deepseek.com/v1
5265
```
5366

54-
### Anthropic Claude
67+
### オプション:バックアップ AI プロバイダー
68+
69+
メインプロバイダー障害時に自動切替:
5570

5671
```env
57-
AI_PROVIDER=anthropic
58-
AI_MODEL=claude-sonnet-4-20250514
59-
ANTHROPIC_API_KEY=sk-ant-...
72+
AI_FALLBACK_PROVIDER=openai
73+
AI_FALLBACK_MODEL=gpt-4o-mini
74+
AI_FALLBACK_API_KEY=sk-...
75+
AI_FALLBACK_BASE_URL=https://api.openai.com/v1
6076
```
6177

62-
### OpenAI または互換インターフェース
78+
全設定は `.env.example` を参照してください。
6379

64-
```env
65-
AI_PROVIDER=openai
66-
AI_MODEL=gpt-4o
67-
OPENAI_API_KEY=sk-...
80+
## 開発
81+
82+
```bash
83+
npm install
84+
npm run dev # 開発サーバー起動
85+
npm test # テスト実行(46 テスト、4 スイート)
86+
npm run build # プロダクションビルド
6887
```
6988

7089
## 技術スタック
7190

72-
- [Next.js](https://nextjs.org/) 16 + React 19 + TypeScript
91+
- [Next.js](https://nextjs.org/) 16 + React 19 + TypeScript(strict モード)
7392
- [Tailwind CSS](https://tailwindcss.com/) v4
7493
- [AI SDK](https://sdk.vercel.ai/) — 統一マルチモデルインターフェース
94+
- [Vitest](https://vitest.dev/) — ユニットテスト(目標 80%+ カバレッジ)
7595
- [Lucide React](https://lucide.dev/) — アイコン
76-
- [react-markdown](https://github.com/remarkjs/react-markdown)レッスンコンテンツのレンダリング
96+
- [react-markdown](https://github.com/remarkjs/react-markdown)コンテンツレンダリング
7797

7898
## プロジェクト構造
7999

80100
```
81101
app/
82-
api/agent/route.ts # AI 会話ストリーミング API
83-
lesson/[id]/page.tsx # レッスン詳細ページ
84-
page.tsx # トップページ
85-
layout.tsx # ルートレイアウト
102+
api/agent/route.ts # AI 会話ストリーミング API(認証 + レート制限 + フォールバック)
103+
api/review/route.ts # AI コードレビュー API
104+
api/storage/route.ts # サーバーサイド key-value ストレージ
105+
api/analytics/route.ts # 分析イベントエンドポイント
106+
lesson/[id]/page.tsx # レッスン詳細ページ
107+
dashboard/page.tsx # 学習ダッシュボード
108+
page.tsx # トップページ
86109
components/
87-
ChatInterface.tsx # AI チャットコンポーネント
88-
PromptPlayground.tsx # コード生成練習エリア
89-
LessonNavigator.tsx # レッスンナビゲーションサイドバー
110+
ChatInterface.tsx # AI チャットコンポーネント
111+
PromptPlayground.tsx # コード生成サンドボックス
112+
exercise/ # インタラクティブコード演習
113+
dashboard/ # 統計、ヒートマップ、スキルレーダー
114+
gamification/ # XP バー、バッジ解除、紙吹雪エフェクト
115+
visualizations/ # アニメーションフローチャート、DOM ツリー
90116
lib/
91-
lessons.ts # レッスンデータ
117+
lessons.ts # 22 レッスン定義
118+
progress.ts # 学習進捗(Repository パターン)
119+
gamification.ts # XP、レベル、連続学習
120+
achievements.ts # 10 種類の達成バッジ
121+
adaptive.ts # 学習レコメンデーションエンジン
122+
repository.ts # ストレージ抽象化(localStorage / サーバー)
123+
logger.ts # 構造化ログ
124+
analytics.ts # ユーザー行動トラッキング
92125
scripts/
93-
setup.sh # 自動セットアップスクリプト
126+
setup.sh # 自動セットアップスクリプト
127+
generate-icons.mjs # PWA アイコンジェネレーター
94128
```
95129

96130
## カスタムレッスン
97131

98-
`lib/lessons.ts` を編集すると、レッスンコンテンツの修正や追加が可能です。Markdown 形式に対応しています。
132+
`lib/lessons.ts` を編集するとレッスン内容の修正や追加が可能です。Markdown 形式に対応しています。
133+
134+
## デプロイ
135+
136+
### Vercel(最も簡単)
137+
138+
1. GitHub にプッシュ
139+
2. [vercel.com](https://vercel.com/) でリポジトリをインポート
140+
3. プロジェクト設定で環境変数を追加
141+
4. デプロイ
142+
143+
### Docker
144+
145+
```bash
146+
docker compose up -d
147+
```
148+
149+
マルチステージビルドにより、イメージは約 150MB です。
99150

100151
## 関連プロジェクト
101152

README.md

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44

55
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
66
[![Next.js](https://img.shields.io/badge/Next.js-16-black)](https://nextjs.org/)
7-
[![AI SDK](https://img.shields.io/badge/AI%20SDK-multi--model-purple)](https://sdk.vercel.ai/)
7+
[![CI](https://github.com/sixtdreanight/vibe-coding-agent/actions/workflows/ci.yml/badge.svg)](https://github.com/sixtdreanight/vibe-coding-agent/actions/workflows/ci.yml)
88

9-
An interactive tutorial site that teaches absolute beginners how to code with AI assistance (Vibe Coding). 12 lessons, built-in AI teaching assistant, and a prompt playground that generates real HTML/CSS/JS.
9+
An interactive tutorial site that teaches absolute beginners how to code with AI assistance (Vibe Coding). 22 lessons across 7 chapters, built-in AI teaching assistant, gamification system, and a prompt playground that generates real HTML/CSS/JS.
1010

1111
---
1212

1313
## Features
1414

15-
- **Structured curriculum**12 lessons from concepts to hands-on practice
16-
- **AI teaching assistant**Ask questions anytime, get instant answers
15+
- **22 structured lessons**7 chapters from concepts to publishing
16+
- **AI teaching assistant**Socratic and direct modes, streaming responses
1717
- **Prompt Playground** — Describe what you want, get runnable HTML/CSS/JS code
18+
- **Gamification** — XP, levels, streaks, 10 achievement badges
19+
- **Code exercises** — Interactive exercises with hints and auto-check
20+
- **Learning dashboard** — Progress tracking, heatmap, skill radar
21+
- **PWA support** — Installable, works offline
1822
- **Multi-model support** — Claude, OpenAI, DeepSeek, and any OpenAI-compatible API
23+
- **AI fallback** — Automatic switch to backup provider if primary fails
1924

2025
## Quick Start
2126

@@ -38,68 +43,110 @@ Open http://localhost:3000 to start learning.
3843

3944
> Detailed setup guide for beginners: [SETUP_GUIDE.md](./SETUP_GUIDE.md)
4045
41-
## Model Configuration
46+
### Docker
4247

43-
Supports three AI providers, configured via `.env`:
48+
```bash
49+
docker compose up -d
50+
```
4451

45-
### DeepSeek (recommended for users in China)
52+
## Configuration
53+
54+
### Required
4655

4756
```env
57+
# Shared secret for API authentication (generate a random string)
58+
AI_API_AUTH_TOKEN=your-secret-token
59+
60+
# Primary AI provider
4861
AI_PROVIDER=openai-compatible
4962
AI_MODEL=deepseek-chat
5063
AI_API_KEY=sk-...
5164
AI_BASE_URL=https://api.deepseek.com/v1
5265
```
5366

54-
### Anthropic Claude
67+
### Optional: Fallback AI Provider
68+
69+
If the primary AI provider fails, the fallback is tried automatically:
5570

5671
```env
57-
AI_PROVIDER=anthropic
58-
AI_MODEL=claude-sonnet-4-20250514
59-
ANTHROPIC_API_KEY=sk-ant-...
72+
AI_FALLBACK_PROVIDER=openai
73+
AI_FALLBACK_MODEL=gpt-4o-mini
74+
AI_FALLBACK_API_KEY=sk-...
75+
AI_FALLBACK_BASE_URL=https://api.openai.com/v1
6076
```
6177

62-
### OpenAI or compatible
78+
See `.env.example` for all options.
6379

64-
```env
65-
AI_PROVIDER=openai
66-
AI_MODEL=gpt-4o
67-
OPENAI_API_KEY=sk-...
80+
## Development
81+
82+
```bash
83+
npm install
84+
npm run dev # Start dev server
85+
npm test # Run tests (46 tests, 4 suites)
86+
npm run build # Production build
6887
```
6988

7089
## Tech Stack
7190

72-
- [Next.js](https://nextjs.org/) 16 + React 19 + TypeScript
91+
- [Next.js](https://nextjs.org/) 16 + React 19 + TypeScript (strict mode)
7392
- [Tailwind CSS](https://tailwindcss.com/) v4
7493
- [AI SDK](https://sdk.vercel.ai/) — unified multi-model interface
94+
- [Vitest](https://vitest.dev/) — unit testing (80%+ coverage target)
7595
- [Lucide React](https://lucide.dev/) — icons
7696
- [react-markdown](https://github.com/remarkjs/react-markdown) — content rendering
7797

7898
## Project Structure
7999

80100
```
81101
app/
82-
api/agent/route.ts # AI streaming chat API
83-
lesson/[id]/page.tsx # Lesson detail page
84-
page.tsx # Home page
85-
layout.tsx # Root layout
102+
api/agent/route.ts # AI streaming chat API (auth + rate limit + fallback)
103+
api/review/route.ts # AI code review API
104+
api/storage/route.ts # Server-side key-value storage
105+
api/analytics/route.ts # Analytics event endpoint
106+
lesson/[id]/page.tsx # Lesson detail page
107+
dashboard/page.tsx # Learning dashboard
108+
page.tsx # Home page
86109
components/
87-
ChatInterface.tsx # AI chat component
88-
PromptPlayground.tsx # Code generation sandbox
89-
LessonNavigator.tsx # Course navigation sidebar
110+
ChatInterface.tsx # AI chat with Socratic mode
111+
PromptPlayground.tsx # Code generation sandbox
112+
exercise/ # Interactive code exercises
113+
dashboard/ # Stats, heatmap, skill radar
114+
gamification/ # XP bar, badge unlock, confetti
115+
visualizations/ # Animated flow charts, DOM tree
90116
lib/
91-
lessons.ts # Course data
117+
lessons.ts # 22 lesson definitions
118+
progress.ts # Learning progress (Repository pattern)
119+
gamification.ts # XP, levels, streaks
120+
achievements.ts # 10 badges with conditions
121+
adaptive.ts # Learning recommendations
122+
repository.ts # Storage abstraction (localStorage / server)
123+
logger.ts # Structured JSON logging
124+
analytics.ts # User behavior tracking
92125
scripts/
93-
setup.sh # Auto setup script
126+
setup.sh # Auto setup script
127+
generate-icons.mjs # PWA icon generator
94128
```
95129

96130
## Customizing Lessons
97131

98132
Edit `lib/lessons.ts` to modify or add lessons. Content supports Markdown.
99133

100-
## Related
134+
## Deployment
135+
136+
### Vercel (easiest)
137+
138+
1. Push to GitHub
139+
2. Import repo at [vercel.com](https://vercel.com/)
140+
3. Add environment variables in project settings
141+
4. Deploy
142+
143+
### Docker
144+
145+
```bash
146+
docker compose up -d
147+
```
101148

102-
- [myBlog](https://github.com/sixtdreanight/myBlog) — Author's personal blog, more articles on vibe coding
149+
The image uses multi-stage builds for a small footprint (~150MB).
103150

104151
## License
105152

0 commit comments

Comments
 (0)