llm_handlers.js•2.76 kB
// llm_handlers.js
// Handler functions for LLM-driven bot event responses
// Example: Low health handler
function handleLowHealth(event, mcpServer) {
// Issue MCP command to move bot to safety or heal
// Example: mcpServer.executeBotCommand('chat', ['I need healing!']);
// Example: mcpServer.pathfindToPosition({ x: event.safeX, y: event.safeY, z: event.safeZ, range: 2 });
console.log('Handling low health event:', event);
}
// Example: Hostile mob nearby handler
function handleHostileMobNearby(event, mcpServer) {
// Directly alert the LLM (this module) to decide and act
console.log('ALERT: Hostile mob detected near bot:', event);
llmDecideHostileMobAction(event, mcpServer);
}
// LLM decision function for hostile mob events
function llmDecideHostileMobAction(event, mcpServer) {
// Notify the LLM of the hostile mob event and provide all context
// The LLM should decide dynamically what action to take
if (typeof global.llmDynamicDecision === 'function') {
global.llmDynamicDecision('hostileMobNearby', event, mcpServer);
} else {
console.log('LLM needs to decide dynamically for hostile mob event:', event);
}
}
// Example: Special chat handler
function handleSpecialChat(event, mcpServer) {
console.log('Handling special chat event:', event);
// Use MCP sampling to send event to LLM and get decision
const samplingRequest = {
messages: [
{ role: 'user', content: { type: 'text', text: `Special chat event: ${JSON.stringify(event)}. What should the bot do or say in response?` } }
]
};
mcpServer.sampling.createMessage(samplingRequest).then(response => {
const llmDecision = response.content.text;
console.log('LLM sampling decision:', llmDecision);
// Parse and execute the LLM's decision
if (llmDecision.includes('chat') || llmDecision.includes('say')) {
// Extract message from response
const messageMatch = llmDecision.match(/"([^"]*)"/);
const message = messageMatch ? messageMatch[1] : 'Hello!';
mcpServer.executeBotCommand('chat', [message]);
}
}).catch(err => {
console.error('Sampling failed:', err);
// Fallback
mcpServer.executeBotCommand('chat', ['I received your message!']);
});
}
// LLM dynamic decision function
function llmDynamicDecision(eventType, event, mcpServer) {
// This is where the LLM decides what to do based on eventType and event context
// Example: Log the event and prompt for decision (to be replaced by actual LLM logic)
console.log(`LLM dynamic decision required for event type: ${eventType}`, event);
// TODO: Implement actual LLM-driven decision logic here
}
module.exports = {
handleLowHealth,
handleHostileMobNearby,
handleSpecialChat,
llmDynamicDecision
};