|
const express = require('express'); |
|
const CharacterAI = require('node_characterai'); |
|
const app = express(); |
|
const PORT = 7860; |
|
|
|
const DEFAULT_TOKEN = process.env['TOKEN_CAI']; |
|
|
|
const SESSION_DURATION = 14400000; |
|
|
|
let sessions = []; |
|
|
|
const createSession = async (token) => { |
|
const existingSession = sessions.find(v => v.token === token); |
|
if (existingSession) { |
|
return existingSession; |
|
} |
|
|
|
const characterAI = new CharacterAI(); |
|
await characterAI.authenticateWithToken(token); |
|
const session = { |
|
token, |
|
characterAI, |
|
lastUsed: Date.now(), |
|
browser: characterAI.unauthenticate, |
|
isDefault: token === DEFAULT_TOKEN |
|
}; |
|
sessions.push(session); |
|
return session; |
|
}; |
|
|
|
const getSession = async (token) => { |
|
let session = sessions.find(s => s.token === token); |
|
if (session) { |
|
session.lastUsed = Date.now(); |
|
return session; |
|
} else { |
|
return await createSession(token); |
|
} |
|
}; |
|
|
|
const cleanExpiredSessions = async () => { |
|
const now = Date.now(); |
|
const expiredSessions = sessions.filter(session => !session.isDefault && session.lastUsed <= now - SESSION_DURATION); |
|
|
|
|
|
for (const session of expiredSessions) { |
|
if (session.browser) { |
|
await session.browser(); |
|
} |
|
} |
|
|
|
|
|
sessions = sessions.filter(session => session.isDefault || session.lastUsed > now - SESSION_DURATION); |
|
}; |
|
|
|
|
|
setInterval(cleanExpiredSessions, 10 * 60 * 1000); |
|
|
|
app.use(express.json()); |
|
app.set('json spaces', 4); |
|
app.get('/', async (req, res) => { |
|
res.send('halo dunia') |
|
console.log(sessions) |
|
console.log(new Date().toLocaleString('id-ID', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' })); |
|
}) |
|
app.get('/api', async (req, res) => { |
|
const { token, text, characterId, sessionId } = req.query; |
|
console.log("token access: " + (token || DEFAULT_TOKEN)) |
|
try { |
|
const session = token ? await getSession(token) : await createSession(DEFAULT_TOKEN); |
|
const chat = await session.characterAI.createOrContinueChat(characterId); |
|
sessionId ? await chat.changeToConversationId(sessionId, true) : await chat.saveAndStartNewChat() |
|
const response = await chat.sendAndAwaitResponse(text, true); |
|
console.log(response) |
|
response.sessionId = response?.chat?.externalId |
|
response.characterId = response?.chat?.characterId |
|
delete response.chat |
|
res.send({ success: true, result: response }); |
|
console.log('api done') |
|
} catch (error) { |
|
res.status(500).json({ error: error.message }); |
|
} |
|
}); |
|
|
|
app.get('/api/chara/search', async (req, res) => { |
|
const { name, token } = req.query; |
|
|
|
try { |
|
const session = token ? await getSession(token) : await createSession(DEFAULT_TOKEN); |
|
const characters = await session.characterAI.searchCharacters(name); |
|
res.send({ success: true, result: characters.characters }); |
|
console.log('search done') |
|
} catch (error) { |
|
res.status(500).json({ error: error.message }); |
|
} |
|
}); |
|
|
|
app.get('/api/chara/info', async (req, res) => { |
|
const { characterId, token } = req.query; |
|
|
|
try { |
|
const session = token ? await getSession(token) : await createSession(DEFAULT_TOKEN); |
|
const characterInfo = await session.characterAI.fetchCharacterInfo(characterId); |
|
res.json({ success: true, result: characterInfo }); |
|
console.log('info done') |
|
} catch (error) { |
|
res.status(500).json({ error: error.message }); |
|
} |
|
}); |
|
|
|
app.listen(PORT, () => { |
|
console.log(`Server running on port ${PORT}`); |
|
}); |
|
|