refactor: v2 重构 — 模块化拆分 + AI Prompt模板化 + 前端状态管理
P0 - AI交互层:
- ai/prompt.js: 模块化 Prompt 模板
- ai/validator.js: JSON Schema 校验 + normalize
- ai/client.js: DeepSeek API 封装
- 金额计算唯一化,时间格式归一化
P1 - 后端架构:
- routes/: auth/rooms/messages/purchases 独立路由
- middleware/auth.js: 认证授权中间件
- ws/index.js: WebSocket 连接管理
- db.js: 启用 foreign_keys
- server.js: 从588行精简到40行入口
P2 - 前端架构:
- public/css/style.css: CSS 独立
- public/js/store.js: 全局状态 Store
- public/js/{chat,ws,purchases,auth}.js: 功能模块拆分
- index.html: 纯 HTML 结构, v2.6
This commit is contained in:
19
server/ai/client.js
Normal file
19
server/ai/client.js
Normal file
@@ -0,0 +1,19 @@
|
||||
// DeepSeek API 客户端
|
||||
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY || '';
|
||||
|
||||
async function callDeepSeek(messages, temperature = 0.1) {
|
||||
console.log('🤖 调用 DeepSeek...');
|
||||
const res = await fetch('https://api.deepseek.com/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${DEEPSEEK_API_KEY}` },
|
||||
body: JSON.stringify({ model: 'deepseek-v4-flash', messages, temperature, stream: false })
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!data.choices || !data.choices[0]) {
|
||||
console.error('🤖 DeepSeek 返回异常:', JSON.stringify(data));
|
||||
throw new Error('DeepSeek API 返回异常: ' + (data.error?.message || JSON.stringify(data)));
|
||||
}
|
||||
return data.choices[0].message.content;
|
||||
}
|
||||
|
||||
module.exports = { callDeepSeek };
|
||||
Reference in New Issue
Block a user