fable
This commit is contained in:
+183
-205
@@ -2,224 +2,202 @@
|
||||
|
||||
const express = require('express');
|
||||
const { CJbot } = require('../../model/minecraft');
|
||||
const { getInstance } = require('./manager');
|
||||
|
||||
function createRouter() {
|
||||
const router = express.Router();
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/api/ai/status', (req, res) => {
|
||||
try {
|
||||
const result = {};
|
||||
for (const [name, bot] of Object.entries(CJbot.bots)) {
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai) continue;
|
||||
const config = ai.__getConfig();
|
||||
result[name] = {
|
||||
connected: bot.isReady,
|
||||
provider: config.provider || 'unknown',
|
||||
model: config.model || 'unknown',
|
||||
interval: ai.intervalLength,
|
||||
promptName: ai.promptName || 'unknown',
|
||||
active: !!ai._active,
|
||||
};
|
||||
}
|
||||
res.json({ bots: result });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/status:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
// ---- AI Status ----
|
||||
|
||||
// Get list of all bots (for UI)
|
||||
router.get('/api/ai/bots', (req, res) => {
|
||||
try {
|
||||
const bots = [];
|
||||
for (const [name, bot] of Object.entries(CJbot.bots)) {
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
bots.push({
|
||||
name: name,
|
||||
hasAI: !!ai,
|
||||
hasMemory: !!(ai && ai.memoryDB)
|
||||
});
|
||||
}
|
||||
res.json({ bots });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/bots:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
router.get('/api/ai/status', (req, res) => {
|
||||
try {
|
||||
const manager = getInstance();
|
||||
const result = {};
|
||||
const faceName = manager.faceBotName;
|
||||
|
||||
// Get all players with memories (shared across all bots)
|
||||
router.get('/api/ai/memories/players', async (req, res) => {
|
||||
try {
|
||||
// Get players from any bot's memoryDB (they're shared now)
|
||||
let players = [];
|
||||
for (const [name, bot] of Object.entries(CJbot.bots)) {
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (ai && ai.memoryDB) {
|
||||
players = await ai.memoryDB.getAllKnownPlayers();
|
||||
break;
|
||||
}
|
||||
}
|
||||
res.json({ players });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories/players:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
for (const [name, bot] of Object.entries(CJbot.bots)) {
|
||||
const isFace = name === faceName && manager.isActive;
|
||||
if (!isFace && !bot.plunginsLoaded['Ai']) continue;
|
||||
|
||||
// Get memories for a specific player
|
||||
router.get('/api/ai/memories/:botName/:playerName', async (req, res) => {
|
||||
try {
|
||||
const { botName, playerName } = req.params;
|
||||
const bot = CJbot.bots[botName];
|
||||
if (!bot) {
|
||||
return res.status(404).json({ error: 'Bot not found' });
|
||||
}
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai || !ai.memoryDB) {
|
||||
return res.status(404).json({ error: 'AI or memory DB not loaded' });
|
||||
}
|
||||
const memories = await ai.memoryDB.getAllPlayerMemories(playerName);
|
||||
res.json({ bot: botName, player: playerName, memories });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories/:bot/:player:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
result[name] = {
|
||||
connected: bot.isReady,
|
||||
provider: manager.isActive ? (manager._config?.provider || 'unknown') : 'unknown',
|
||||
model: manager.isActive ? (manager._config?.model || 'unknown') : 'unknown',
|
||||
interval: manager.isActive ? (manager._config?.interval || 10) : 10,
|
||||
promptName: isFace && manager._config ? (manager._config.promptName || 'unknown') : 'unknown',
|
||||
active: isFace && manager.isActive,
|
||||
isFace,
|
||||
};
|
||||
}
|
||||
res.json({ bots: result });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/status:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Set or update a player memory
|
||||
router.post('/api/ai/memories/:botName/:playerName', async (req, res) => {
|
||||
try {
|
||||
const { botName, playerName } = req.params;
|
||||
const { key, value } = req.body;
|
||||
if (!key || !value) {
|
||||
return res.status(400).json({ error: 'key and value are required' });
|
||||
}
|
||||
const bot = CJbot.bots[botName];
|
||||
if (!bot) {
|
||||
return res.status(404).json({ error: 'Bot not found' });
|
||||
}
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai || !ai.memoryDB) {
|
||||
return res.status(404).json({ error: 'AI or memory DB not loaded' });
|
||||
}
|
||||
await ai.memoryDB.setPlayerMemory(playerName, key, value);
|
||||
res.json({ success: true, bot: botName, player: playerName, key, value });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories/:bot/:player POST:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
// ---- Bot list (for UI) ----
|
||||
|
||||
// Delete a player memory
|
||||
router.delete('/api/ai/memories/:botName/:playerName/:key', async (req, res) => {
|
||||
try {
|
||||
const { botName, playerName, key } = req.params;
|
||||
const bot = CJbot.bots[botName];
|
||||
if (!bot) {
|
||||
return res.status(404).json({ error: 'Bot not found' });
|
||||
}
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai || !ai.memoryDB) {
|
||||
return res.status(404).json({ error: 'AI or memory DB not loaded' });
|
||||
}
|
||||
await ai.memoryDB.deletePlayerMemory(playerName, key);
|
||||
res.json({ success: true, bot: botName, player: playerName, key });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories/:bot/:player/:key DELETE:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
router.get('/api/ai/bots', (req, res) => {
|
||||
try {
|
||||
const manager = getInstance();
|
||||
const faceName = manager.faceBotName;
|
||||
const bots = [];
|
||||
|
||||
// Get bot directives
|
||||
router.get('/api/ai/directives/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const bot = CJbot.bots[botName];
|
||||
if (!bot) {
|
||||
return res.status(404).json({ error: 'Bot not found' });
|
||||
}
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai || !ai.memoryDB) {
|
||||
return res.status(404).json({ error: 'AI or memory DB not loaded' });
|
||||
}
|
||||
const directives = await ai.memoryDB.getAllDirectives();
|
||||
res.json({ bot: botName, directives });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/directives/:bot:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
for (const [name, bot] of Object.entries(CJbot.bots)) {
|
||||
const isFace = name === faceName && manager.isActive;
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
bots.push({
|
||||
name,
|
||||
hasAI: isFace || !!ai,
|
||||
hasMemory: isFace || !!(ai && ai.memoryDB),
|
||||
isFace,
|
||||
});
|
||||
}
|
||||
res.json({ bots });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/bots:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Set or update a directive
|
||||
router.post('/api/ai/directives/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const { key, value } = req.body;
|
||||
if (!key || !value) {
|
||||
return res.status(400).json({ error: 'key and value are required' });
|
||||
}
|
||||
const bot = CJbot.bots[botName];
|
||||
if (!bot) {
|
||||
return res.status(404).json({ error: 'Bot not found' });
|
||||
}
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai || !ai.memoryDB) {
|
||||
return res.status(404).json({ error: 'AI or memory DB not loaded' });
|
||||
}
|
||||
await ai.memoryDB.setDirective(key, value);
|
||||
res.json({ success: true, bot: botName, key, value });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/directives/:bot POST:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
// ---- Player Memories (shared across all bots) ----
|
||||
|
||||
// Get general memories
|
||||
router.get('/api/ai/general-memories/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const bot = CJbot.bots[botName];
|
||||
if (!bot) {
|
||||
return res.status(404).json({ error: 'Bot not found' });
|
||||
}
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai || !ai.memoryDB) {
|
||||
return res.status(404).json({ error: 'AI or memory DB not loaded' });
|
||||
}
|
||||
const memories = await ai.memoryDB.getAllGeneralMemories();
|
||||
res.json({ bot: botName, memories });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/general-memories/:bot:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
router.get('/api/ai/memories/players', async (req, res) => {
|
||||
try {
|
||||
const manager = getInstance();
|
||||
const players = manager.isActive
|
||||
? await manager._memoryDB.getAllKnownPlayers()
|
||||
: [];
|
||||
res.json({ players });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories/players:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Set or update a general memory
|
||||
router.post('/api/ai/general-memories/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const { key, value } = req.body;
|
||||
if (!key || !value) {
|
||||
return res.status(400).json({ error: 'key and value are required' });
|
||||
}
|
||||
const bot = CJbot.bots[botName];
|
||||
if (!bot) {
|
||||
return res.status(404).json({ error: 'Bot not found' });
|
||||
}
|
||||
const ai = bot.plunginsLoaded['Ai'];
|
||||
if (!ai || !ai.memoryDB) {
|
||||
return res.status(404).json({ error: 'AI or memory DB not loaded' });
|
||||
}
|
||||
await ai.memoryDB.setGeneralMemory(key, value);
|
||||
res.json({ success: true, bot: botName, key, value });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/general-memories/:bot POST:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
router.get('/api/ai/memories/:botName/:playerName', async (req, res) => {
|
||||
try {
|
||||
const { playerName } = req.params;
|
||||
const manager = getInstance();
|
||||
if (!manager.isActive) {
|
||||
return res.status(404).json({ error: 'AI manager not active' });
|
||||
}
|
||||
const memories = await manager._memoryDB.getAllPlayerMemories(playerName);
|
||||
res.json({ player: playerName, memories });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories/:bot/:player:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
router.post('/api/ai/memories/:botName/:playerName', async (req, res) => {
|
||||
try {
|
||||
const { playerName } = req.params;
|
||||
const { key, value } = req.body;
|
||||
if (!key || !value) {
|
||||
return res.status(400).json({ error: 'key and value are required' });
|
||||
}
|
||||
const manager = getInstance();
|
||||
if (!manager.isActive) {
|
||||
return res.status(404).json({ error: 'AI manager not active' });
|
||||
}
|
||||
await manager._memoryDB.setPlayerMemory(playerName, key, value);
|
||||
res.json({ success: true, player: playerName, key, value });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories POST:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/api/ai/memories/:botName/:playerName/:key', async (req, res) => {
|
||||
try {
|
||||
const { playerName, key } = req.params;
|
||||
const manager = getInstance();
|
||||
if (!manager.isActive) {
|
||||
return res.status(404).json({ error: 'AI manager not active' });
|
||||
}
|
||||
await manager._memoryDB.deletePlayerMemory(playerName, key);
|
||||
res.json({ success: true, player: playerName, key });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/memories DELETE:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// ---- Bot Directives ----
|
||||
|
||||
router.get('/api/ai/directives/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const manager = getInstance();
|
||||
if (!manager.isActive) {
|
||||
return res.status(404).json({ error: 'AI manager not active' });
|
||||
}
|
||||
const directives = await manager._memoryDB.getAllDirectives(botName);
|
||||
res.json({ bot: botName, directives });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/directives:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/api/ai/directives/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const { key, value } = req.body;
|
||||
if (!key || !value) {
|
||||
return res.status(400).json({ error: 'key and value are required' });
|
||||
}
|
||||
const manager = getInstance();
|
||||
if (!manager.isActive) {
|
||||
return res.status(404).json({ error: 'AI manager not active' });
|
||||
}
|
||||
await manager._memoryDB.setDirective(botName, key, value);
|
||||
res.json({ success: true, bot: botName, key, value });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/directives POST:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// ---- General Memories ----
|
||||
|
||||
router.get('/api/ai/general-memories/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const manager = getInstance();
|
||||
if (!manager.isActive) {
|
||||
return res.status(404).json({ error: 'AI manager not active' });
|
||||
}
|
||||
const memories = await manager._memoryDB.getAllGeneralMemories(botName);
|
||||
res.json({ bot: botName, memories });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/general-memories:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/api/ai/general-memories/:botName', async (req, res) => {
|
||||
try {
|
||||
const { botName } = req.params;
|
||||
const { key, value } = req.body;
|
||||
if (!key || !value) {
|
||||
return res.status(400).json({ error: 'key and value are required' });
|
||||
}
|
||||
const manager = getInstance();
|
||||
if (!manager.isActive) {
|
||||
return res.status(404).json({ error: 'AI manager not active' });
|
||||
}
|
||||
await manager._memoryDB.setGeneralMemory(botName, key, value);
|
||||
res.json({ success: true, bot: botName, key, value });
|
||||
} catch (error) {
|
||||
console.error('API Error /api/ai/general-memories POST:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
const webUI = {
|
||||
|
||||
Reference in New Issue
Block a user