Update src/index.js
Browse files- src/index.js +21 -5
src/index.js
CHANGED
|
@@ -15,8 +15,19 @@ const app = express()
|
|
| 15 |
app.set('trust proxy', 1)
|
| 16 |
app.set('json spaces', 4)
|
| 17 |
app.use(express.urlencoded({ extended: true }))
|
| 18 |
-
app.use((req, res, next) => {
|
| 19 |
console.log(req.method, req.path, res.statusCode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
next()
|
| 21 |
})
|
| 22 |
|
|
@@ -53,6 +64,8 @@ app.get('/ev', async (req, res) => {
|
|
| 53 |
}
|
| 54 |
})
|
| 55 |
|
|
|
|
|
|
|
| 56 |
app.all('/api', rateLimit(), async (req, res) => {
|
| 57 |
if (!['GET', 'POST'].includes(req.method)) return res.json({ message: 'Method not allowed' })
|
| 58 |
try {
|
|
@@ -63,8 +76,10 @@ app.all('/api', rateLimit(), async (req, res) => {
|
|
| 63 |
if (sessionId) {
|
| 64 |
if (!sessions.has(sessionId)) return res.json({ message: 'sessionId not found' })
|
| 65 |
const characterAI = sessions.get(sessionId)
|
|
|
|
|
|
|
| 66 |
const chat = await characterAI.createOrContinueChat(characterId)
|
| 67 |
-
|
| 68 |
|
| 69 |
delete response.chat
|
| 70 |
res.json({ ...response, sessionId })
|
|
@@ -75,7 +90,7 @@ app.all('/api', rateLimit(), async (req, res) => {
|
|
| 75 |
const characterAI = new CharacterAI()
|
| 76 |
characterAI.requester.puppeteerPath = puppeteer.executablePath()
|
| 77 |
await characterAI.authenticateAsGuest()
|
| 78 |
-
sessions.set(id, characterAI)
|
| 79 |
|
| 80 |
const chat = await characterAI.createOrContinueChat(characterId)
|
| 81 |
const response = await chat.sendAndAwaitResponse(text, true)
|
|
@@ -84,7 +99,8 @@ app.all('/api', rateLimit(), async (req, res) => {
|
|
| 84 |
res.json({ ...response, sessionId: id })
|
| 85 |
} catch (e) {
|
| 86 |
console.log(e)
|
| 87 |
-
|
|
|
|
| 88 |
}
|
| 89 |
})
|
| 90 |
|
|
@@ -94,4 +110,4 @@ app.listen(PORT, () => console.log('App running on port', PORT))
|
|
| 94 |
function findChromium() {
|
| 95 |
let cmd = execSync('nix eval nixpkgs.chromium.outPath --raw').toString()
|
| 96 |
return `${cmd}/bin/chromium`
|
| 97 |
-
}
|
|
|
|
| 15 |
app.set('trust proxy', 1)
|
| 16 |
app.set('json spaces', 4)
|
| 17 |
app.use(express.urlencoded({ extended: true }))
|
| 18 |
+
app.use(async (req, res, next) => {
|
| 19 |
console.log(req.method, req.path, res.statusCode)
|
| 20 |
+
|
| 21 |
+
const sessionId = req.query.sessionId || req.body.sessionId
|
| 22 |
+
if (sessions.has(sessionId)) {
|
| 23 |
+
const characterAI = sessions.get(sessionId)
|
| 24 |
+
|
| 25 |
+
if (Date.now() - characterAI.lastfetch > 10 * 60 * 1000) {
|
| 26 |
+
await characterAI.unauthenticate()
|
| 27 |
+
sessions.delete(sessionId)
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
next()
|
| 32 |
})
|
| 33 |
|
|
|
|
| 64 |
}
|
| 65 |
})
|
| 66 |
|
| 67 |
+
app.get('/sessions', (_, res) => res.json({ sessions: eval(sessions.values()) }))
|
| 68 |
+
|
| 69 |
app.all('/api', rateLimit(), async (req, res) => {
|
| 70 |
if (!['GET', 'POST'].includes(req.method)) return res.json({ message: 'Method not allowed' })
|
| 71 |
try {
|
|
|
|
| 76 |
if (sessionId) {
|
| 77 |
if (!sessions.has(sessionId)) return res.json({ message: 'sessionId not found' })
|
| 78 |
const characterAI = sessions.get(sessionId)
|
| 79 |
+
sessions.set(sessionId, Object.assign(characterAI, { lastFetch: Date.now() }))
|
| 80 |
+
|
| 81 |
const chat = await characterAI.createOrContinueChat(characterId)
|
| 82 |
+
const response = await chat.sendAndAwaitResponse(text, true)
|
| 83 |
|
| 84 |
delete response.chat
|
| 85 |
res.json({ ...response, sessionId })
|
|
|
|
| 90 |
const characterAI = new CharacterAI()
|
| 91 |
characterAI.requester.puppeteerPath = puppeteer.executablePath()
|
| 92 |
await characterAI.authenticateAsGuest()
|
| 93 |
+
sessions.set(id, Object.assign(characterAI, { lastFetch: Date.now() }))
|
| 94 |
|
| 95 |
const chat = await characterAI.createOrContinueChat(characterId)
|
| 96 |
const response = await chat.sendAndAwaitResponse(text, true)
|
|
|
|
| 99 |
res.json({ ...response, sessionId: id })
|
| 100 |
} catch (e) {
|
| 101 |
console.log(e)
|
| 102 |
+
e = e.toString()
|
| 103 |
+
res.json({ message: e === '[object Object]' ? util.inspect(e) : e })
|
| 104 |
}
|
| 105 |
})
|
| 106 |
|
|
|
|
| 110 |
function findChromium() {
|
| 111 |
let cmd = execSync('nix eval nixpkgs.chromium.outPath --raw').toString()
|
| 112 |
return `${cmd}/bin/chromium`
|
| 113 |
+
}
|