Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (47 loc) · 1.66 KB
/
Copy pathserver.js
File metadata and controls
56 lines (47 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require('dotenv').config()
const express = require('express')
const cors = require('cors')
const path = require('path')
const db = require('./db')
const context = require('./context')
const ai = require('./ai')
const fs = require('fs')
const app = express()
const PORT = process.env.PORT || 3000
const DB_PATH = process.env.DB_PATH || './nature_republic.db'
const CS_CONTACT = 'cs@naturerepublic.id or www.naturerepublic.id'
if (!process.env.DEEPSEEK_API_KEY || process.env.DEEPSEEK_API_KEY === 'your_deepseek_api_key_here') {
console.error('ERROR: DEEPSEEK_API_KEY is not set in .env')
process.exit(1)
}
if (!fs.existsSync(path.resolve(DB_PATH))) {
console.error(`ERROR: Database file not found at ${DB_PATH}`)
console.error('Please place your nature_republic.db file in the project root.')
process.exit(1)
}
app.use(cors())
app.use(express.json())
app.use(express.static(path.join(__dirname, 'public')))
db.init(DB_PATH)
ai.init({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: process.env.DEEPSEEK_BASE_URL,
model: process.env.DEEPSEEK_MODEL,
})
app.post('/api/chat', async (req, res) => {
try {
const { message } = req.body
if (!message || !message.trim()) {
return res.status(400).json({ error: 'Message is required' })
}
const ctx = context.buildContext(message)
const reply = await ai.getReply(message, ctx, CS_CONTACT)
res.json({ reply, hasData: !!ctx })
} catch (err) {
console.error('Chat error:', err)
res.status(500).json({ error: 'Internal server error' })
}
})
app.listen(PORT, () => {
console.log(`CS Chatbot running at http://localhost:${PORT}`)
})