|
|
import express from 'express'; |
|
|
import { CAINode } from 'cainode'; |
|
|
import bodyParser from "body-parser"; |
|
|
import os from "os"; |
|
|
|
|
|
const app = express(); |
|
|
const PORT = 7860; |
|
|
const HOST = "0.0.0.0"; |
|
|
app.use(bodyParser.json()); |
|
|
|
|
|
|
|
|
app.get('/chat', async (req, res) => { |
|
|
const token = req.query.token || process.env.token; |
|
|
const characterId = req.query.character_id || "smtV3Vyez6ODkwS8BErmBAdgGNj-1XWU73wIFVOY1hQ" |
|
|
const chatId = req.query.chat_id; |
|
|
const voiceId = req.query.voice_id || 'f4b33b1d-2e5a-40a8-8a07-958140cd104d'; |
|
|
const message = req.query.message; |
|
|
|
|
|
if (!message) { |
|
|
return res.status(400).json({ error: 'Message query parameter is required.' }); |
|
|
} |
|
|
|
|
|
console.log('Received parameters:', { characterId, chatId, voiceId, message }); |
|
|
|
|
|
let client; |
|
|
try { |
|
|
client = new CAINode(); |
|
|
|
|
|
|
|
|
await client.login(token); |
|
|
|
|
|
|
|
|
await client.character.connect(characterId); |
|
|
|
|
|
let chatSessionId = chatId; |
|
|
|
|
|
|
|
|
if (!chatSessionId) { |
|
|
const newConversation = await client.character.create_new_conversation(); |
|
|
chatSessionId = newConversation[0].chat.chat_id; |
|
|
} |
|
|
|
|
|
|
|
|
await client.chat.set_conversation_chat(chatSessionId); |
|
|
|
|
|
|
|
|
const response = await client.character.send_message(message); |
|
|
console.log('API Response:', JSON.stringify(response, null, 2)); |
|
|
|
|
|
let reply = ''; |
|
|
let voiceUrl = ''; |
|
|
|
|
|
if (response.turn && response.turn.candidates && |
|
|
response.turn.candidates.length > 0 && |
|
|
response.turn.candidates[0].raw_content) { |
|
|
const messageResponse = response.turn.candidates[0].raw_content; |
|
|
const turnId = response.turn.turn_key.turn_id; |
|
|
const candidateId = response.turn.primary_candidate_id; |
|
|
|
|
|
reply = messageResponse; |
|
|
|
|
|
|
|
|
if (voiceId) { |
|
|
const voiceResponse = await client.character.replay_tts(turnId, candidateId, voiceId); |
|
|
voiceUrl = voiceResponse.replayUrl; |
|
|
} |
|
|
|
|
|
res.json({ chatId: chatSessionId, message: reply, voiceUrl }); |
|
|
} else { |
|
|
res.status(404).json({ error: 'No response content available.' }); |
|
|
} |
|
|
|
|
|
|
|
|
await client.character.disconnect(); |
|
|
await client.logout(); |
|
|
|
|
|
} catch (error) { |
|
|
console.error('Error:', error.message); |
|
|
res.status(500).json({ error: error.message }); |
|
|
|
|
|
if (client) { |
|
|
await client.logout(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/search', async (req, res) => { |
|
|
const token = req.query.token || process.env.token; |
|
|
const query = req.query.query; |
|
|
|
|
|
if (!query) { |
|
|
return res.status(400).json({ error: 'Query parameter is required.' }); |
|
|
} |
|
|
|
|
|
let client; |
|
|
try { |
|
|
client = new CAINode(); |
|
|
|
|
|
|
|
|
await client.login(token); |
|
|
|
|
|
|
|
|
const searchResults = await client.character.search(query); |
|
|
|
|
|
|
|
|
res.json({ results: searchResults }); |
|
|
|
|
|
|
|
|
await client.logout(); |
|
|
|
|
|
} catch (error) { |
|
|
console.error('Error:', error.message); |
|
|
res.status(500).json({ error: error.message }); |
|
|
|
|
|
if (client) { |
|
|
await client.logout(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
app.listen(PORT, HOST, () => { |
|
|
console.log(`Running on http://${HOST}:${PORT}`); |
|
|
}); |