@hhoangphuoc/escape-room-cli
Version:
A CLI for playing AI-generated escape room games. Install globally with: npm install -g @hhoangphuoc/escape-room-cli
162 lines (161 loc) • 7.51 kB
JavaScript
// escape-room-cli/source/utils/commandProcessor.ts
import { handleHelpCommand, handleLookCommand, handleInspectCommand, handleGuessCommand, handlePasswordCommand, handleHintCommand, handleNewGameCommand, handleLeaderboardCommand, handleLogoutCommand, handleLoginCommand, handleNaturalLanguageCommand, handleInstructionsCommand, handleUsageCommand, handleCostCommand,
// Simplified cost monitoring - removed handlePricingCommand, handleCompareCommand, handleEstimateCommand
} from './commandHandlers.js';
import { displayResponse } from './responseDisplay.js';
// import { getApiUrl } from './apiConfig.js';
// Centralized processor for all commands
export const handleCommand = async (props) => {
const { command, auth, game, setHistory, setIsLoading, setLoadingMessage, setShowHistory, setShowModelSelector, setShowInstructions, setShowLeaderboard, setShowCostDashboard, setShowCostMonitor, selectedModel } = props;
if (command.startsWith('/')) {
const parts = command.trim().split(/\s+/);
const cmd = parts[0]?.toLowerCase();
const userContext = {
userId: auth.userId,
sessionToken: auth.sessionToken,
userName: auth.userName,
cliApiKey: auth.apiKey,
hasAICapability: auth.apiKey ? true : false,
};
const gameContext = {
currentGameId: game.gameId,
currentRoomName: game.roomName,
currentRoomBackground: game.roomBackground,
currentGameMode: game.gameMode,
totalRooms: game.totalRooms,
unlockedObjects: game.unlockedObjects,
currentRoomObjects: game.currentRoomObjects,
};
let response;
switch (cmd) {
case '/help':
response = handleHelpCommand(userContext, gameContext);
break;
case '/look':
response = await handleLookCommand(userContext);
if (response.success && response.roomData)
game.setGameFromLook(response.roomData);
break;
case '/inspect':
const objectName = parts.slice(1).join(' ');
response = await handleInspectCommand(objectName, userContext);
break;
case '/guess':
const guessObjectName = parts.slice(1, -1).join(' ');
const answer = parts[parts.length - 1];
response = await handleGuessCommand(guessObjectName, answer, userContext);
if (response.success && response.objectData?.unlocked) {
game.unlockObject(response.objectData.name);
}
break;
case '/password':
const password = parts.slice(1).join(' ');
response = await handlePasswordCommand(password, userContext);
if (response.success && response.gameResult?.gameCompleted) {
game.setGameCompleted();
}
else if (response.success && response.gameResult?.escaped) {
game.fetchGameState();
}
break;
case '/hint':
response = await handleHintCommand(userContext);
break;
case '/newgame':
const mode = parts[1]?.toLowerCase() || 'single-room';
setIsLoading(true);
setLoadingMessage(`Preparing an AI-generated [${mode}] Escape Game...`);
response = await handleNewGameCommand(mode, userContext);
if (response.success && response.gameData)
game.setGame(response.gameData);
setIsLoading(false);
break;
case '/leaderboard':
const leaderboardMode = parts[1]?.toLowerCase() || 'time';
response = await handleLeaderboardCommand(userContext, leaderboardMode);
if (response.success && response.leaderboardData) {
setShowLeaderboard(response.leaderboardData);
return;
}
break;
case '/instructions':
response = handleInstructionsCommand(userContext, gameContext);
if (response.success) {
setShowInstructions(true);
return;
}
break;
case '/logout':
response = handleLogoutCommand();
auth.logout();
game.resetGame();
break;
case '/login':
setIsLoading(true);
setLoadingMessage('Attempting to login...');
response = await handleLoginCommand();
if (response.success && response.userData) {
auth.login(response.userData);
game.fetchGameState();
}
setIsLoading(false);
break;
case '/register':
response = { success: false, message: "To register, please restart the application without a saved session, or use CLI flags." };
break;
case '/history':
// Show command history overlay and stop further processing
setShowHistory(true);
return;
case '/model':
if (userContext.hasAICapability) {
// Show model selector overlay and stop further processing
setShowModelSelector(true);
return;
}
else {
response = { success: false, message: 'AI features not available. Please /login and use your API key to enable.' };
}
break;
case '/usage':
response = await handleUsageCommand(userContext, gameContext);
if (response.success && response.showUsageDashboard && response.usageData) {
setShowCostDashboard(response.usageData);
return;
}
break;
case '/cost':
response = await handleCostCommand(userContext, gameContext);
if (response.success && response.showCostMonitor && response.costData) {
setShowCostMonitor(response.costData);
return;
}
break;
// Removed /pricing, /compare, /estimate commands - simplified cost monitoring
default:
response = { success: false, message: `Unknown command: ${cmd}` };
}
if (response) {
const displayItems = displayResponse(response);
setHistory((prev) => [...prev, ...displayItems]);
}
}
else {
// Natural language
setIsLoading(true);
setLoadingMessage(`Thinking with ${selectedModel.label}...`);
const userContext = {
userId: auth.userId,
sessionToken: auth.sessionToken,
userName: auth.userName,
cliApiKey: auth.apiKey,
hasAICapability: auth.apiKey ? true : false,
};
const response = await handleNaturalLanguageCommand(command, userContext, selectedModel);
if (response) {
const displayItems = displayResponse(response);
setHistory((prev) => [...prev, ...displayItems]);
}
setIsLoading(false);
}
};