|
|
hace 5 días | |
|---|---|---|
| .. | ||
| app | hace 5 días | |
| components | hace 5 días | |
| lib | hace 5 días | |
| .env.local.example | hace 5 días | |
| .eslintrc.json | hace 5 días | |
| .gitignore | hace 5 días | |
| JSON_RENDER_USAGE.md | hace 5 días | |
| README.md | hace 5 días | |
| next.config.js | hace 5 días | |
| package-lock.json | hace 5 días | |
| package.json | hace 5 días | |
| postcss.config.js | hace 5 días | |
| tailwind.config.ts | hace 5 días | |
| tsconfig.json | hace 5 días | |
基于 Next.js 14 App Router 构建的前端应用,使用 SuperJSON 实现 JSON 序列化/反序列化。
frontend-v2/
├── app/ # Next.js App Router
│ ├── auth/ # 认证页面
│ │ └── page.tsx
│ ├── layout.tsx # 根布局
│ ├── page.tsx # 主页(聊天界面)
│ └── globals.css # 全局样式
├── components/ # React 组件
│ ├── Header.tsx # 页面头部
│ ├── ChatInput.tsx # 聊天输入
│ ├── ChatMessage.tsx # 聊天消息
│ └── ToolCallPanel.tsx # 工具调用面板
├── lib/ # 工具库
│ ├── api-client.ts # API 客户端
│ ├── superjson.ts # SuperJSON 配置
│ ├── react-query.tsx # React Query 配置
│ └── hooks.ts # 自定义 Hooks
├── public/ # 静态资源
├── package.json
├── tsconfig.json
├── tailwind.config.ts
├── next.config.js
└── README.md
cd frontend-v2
npm install
# 或
pnpm install
# 或
yarn install
复制 .env.local.example 为 .env.local:
cp .env.local.example .env.local
编辑 .env.local,配置后端 API 地址:
BACKEND_URL=http://localhost:8080
npm run dev
应用将在 http://localhost:3000 启动。
npm run build
npm start
| 端点 | 方法 | 说明 |
|---|---|---|
/api/health |
GET | 健康检查 |
/api/chat |
POST | 聊天(非流式) |
/api/chat/stream |
POST | 聊天(SSE 流式) |
/api/mcp/servers |
GET | MCP 服务器列表 |
/api/mcp/tools |
GET | MCP 工具列表 |
/api/auth/login |
POST | 用户登录 |
/api/auth/register |
POST | 用户注册 |
/api/auth/status |
GET | 认证状态 |
import { apiClient } from '@/lib/api-client';
// 发送聊天消息
const response = await apiClient.chat('你好', []);
// 登录
const result = await apiClient.login('user@example.com', 'password');
// 获取 MCP 工具列表
const tools = await apiClient.listMcpTools();
import { useChat, useAuth } from '@/lib/hooks';
function ChatComponent() {
const { sendMessage, isLoading, response } = useChat();
const { login, logout, isAuthenticated } = useAuth();
// 发送消息
const handleSend = () => {
sendMessage('你好 AI', []);
};
return (
// ...
);
}
SuperJSON 用于处理 Next.js Server Components 和 Client Components 之间的数据传输,支持:
自定义类实例
import { serialize, deserialize } from '@/lib/superjson';
// 序列化
const json = serialize(data);
// 反序列化
const data = deserialize(json);
如果需要通过外网访问后端 API,请配置 .env.local:
BACKEND_URL=https://d8d-ai-vscode-8080-{workspace_id}-{item_id}-template-6-group.dev.d8d.fun
package.json 中修改next.config.js 的 rewrites 配置代理