File size: 3,716 Bytes
94d94c2 415d630 6de0d93 efead7f be7d6f9 94d94c2 607a5aa e648b96 94d94c2 8aa50f6 607a5aa 94d94c2 272f420 94d94c2 272f420 94d94c2 804d780 6de0d93 be7d6f9 6de0d93 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
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());
// API endpoint for chatting
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();
// Login to the client
await client.login(token);
// Connect to the character
await client.character.connect(characterId);
let chatSessionId = chatId;
// If no chatId provided, create a new conversation
if (!chatSessionId) {
const newConversation = await client.character.create_new_conversation();
chatSessionId = newConversation[0].chat.chat_id;
}
// Set the chat session
await client.chat.set_conversation_chat(chatSessionId);
// Send message and get response
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;
// Generate voice reply if voiceId is provided
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.' });
}
// Logout and cleanup
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();
}
}
});
// API endpoint for character search
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();
// Login to the client
await client.login(token);
// Search for characters
const searchResults = await client.character.search(query);
// Return the search results
res.json({ results: searchResults });
// Logout and cleanup
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}`);
}); |