intent-cli
Version:
A fully functional CLI built with TypeScript and modern tools
188 lines • 8.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntentCategory = exports.IntentJSParser = void 0;
// Using custom implementation based on IntentJS patterns
// @intentjs/core has different module structure than expected
const chalk_1 = __importDefault(require("chalk"));
/**
* IntentJS-style Intent Parser Service
* Implements IntentJS-inspired patterns for command processing and intent handling
*/
class IntentJSParser {
/**
* Parse user input using IntentJS command patterns
*/
parseIntent(input) {
const cleanedInput = input.toLowerCase().trim();
// Use pattern matching and keyword analysis
const intent = this.analyzeIntent(cleanedInput);
const entities = this.extractEntities(cleanedInput);
return {
category: intent.category,
confidence: intent.confidence,
entities,
originalText: input,
suggestedAction: intent.suggestedAction
};
}
/**
* Analyze intent using IntentJS-style command patterns
*/
analyzeIntent(input) {
const patterns = {
[IntentCategory.SYSTEM_INFO]: {
keywords: ['system', 'info', 'cpu', 'memory', 'disk', 'stats', 'monitor', 'usage'],
patterns: [/show (me )?system/, /check (my )?(cpu|memory|disk)/, /system (information|stats)/],
action: 'Show system information'
},
[IntentCategory.FILE_OPERATIONS]: {
keywords: ['file', 'create', 'delete', 'read', 'write', 'list', 'search', 'copy', 'move'],
patterns: [/create (a )?file/, /delete (the )?file/, /list (the )?files/, /search for files/, /(read|open) file/],
action: 'Perform file operation'
},
[IntentCategory.PRODUCTIVITY]: {
keywords: ['timer', 'pomodoro', 'calculator', 'calculate', 'note', 'todo', 'stopwatch'],
patterns: [/start (a )?timer/, /run calculator/, /take (a )?note/, /pomodoro/],
action: 'Open productivity tool'
},
[IntentCategory.NETWORK]: {
keywords: ['network', 'internet', 'connection', 'ping', 'test'],
patterns: [/check (my )?(internet|network)/, /test connection/, /network status/],
action: 'Check network status'
},
[IntentCategory.CALCULATOR]: {
keywords: ['calculate', 'compute', 'math', '+', '-', '*', '/', '='],
patterns: [/\d+ [+*/-] \d+/, /calculate/, /compute/, /what is/],
action: 'Open calculator'
},
[IntentCategory.TIMER]: {
keywords: ['timer', 'countdown', 'minutes', 'seconds', 'hours'],
patterns: [/set (a )?timer/, /\d+ (minutes?|seconds?|hours?)/, /start timer/],
action: 'Start timer'
},
[IntentCategory.NOTES]: {
keywords: ['note', 'notes', 'remember', 'write down'],
patterns: [/take (a )?note/, /write (a )?note/, /remember to/, /add (a )?note/],
action: 'Take a note'
},
[IntentCategory.HELP]: {
keywords: ['help', 'assist', 'how to', 'what can', 'commands'],
patterns: [/help/, /what can you do/, /show (me )?commands/, /how do i/],
action: 'Show help menu'
},
[IntentCategory.EXIT]: {
keywords: ['exit', 'quit', 'bye', 'close', 'goodbye'],
patterns: [/(exit|quit|bye|close|goodbye)/],
action: 'Exit application'
}
};
let bestMatch = {
category: IntentCategory.UNKNOWN,
confidence: 0,
suggestedAction: 'Show main menu'
};
// Check each intent category
for (const [category, config] of Object.entries(patterns)) {
let confidence = 0;
// Keyword matching
const keywordMatches = config.keywords.filter(keyword => input.includes(keyword.toLowerCase())).length;
// Pattern matching
const patternMatches = config.patterns.filter(pattern => pattern.test(input)).length;
// Calculate confidence score
confidence = (keywordMatches * 0.3) + (patternMatches * 0.7);
// Boost confidence for exact keyword matches
if (keywordMatches > 0) {
confidence += 0.1;
}
if (confidence > bestMatch.confidence) {
bestMatch = {
category: category,
confidence: Math.min(confidence, 1.0),
suggestedAction: config.action
};
}
}
return bestMatch;
}
/**
* Extract entities from user input
*/
extractEntities(input) {
const entities = {};
// Extract numbers
const numbers = input.match(/\d+/g);
if (numbers) {
entities.numbers = numbers.map(n => parseInt(n));
}
// Extract time expressions
const timeMatch = input.match(/(\d+)\s*(minute|minutes|second|seconds|hour|hours?)\b/i);
if (timeMatch) {
entities.timeValue = parseInt(timeMatch[1]);
entities.timeUnit = timeMatch[2].toLowerCase();
}
// Extract file extensions
const extensionMatch = input.match(/\.\w{2,5}\b/g);
if (extensionMatch) {
entities.fileExtensions = extensionMatch;
}
// Extract operations
const operations = ['create', 'make', 'delete', 'remove', 'read', 'open', 'list', 'search', 'find', 'calculate', 'show', 'check'];
const foundOps = operations.filter(op => input.includes(op));
if (foundOps.length > 0) {
entities.operations = foundOps;
}
// Extract file operations specifically
const fileOps = ['create', 'delete', 'copy', 'move', 'read', 'list', 'search'];
const foundFileOps = fileOps.filter(op => input.includes(op));
if (foundFileOps.length > 0) {
entities.fileOperations = foundFileOps;
}
return entities;
}
/**
* Check if intent confidence is high enough
*/
isHighConfidence(intent) {
return intent.confidence >= 0.6;
}
/**
* Format intent result for display
*/
formatIntentResult(intent) {
const confidenceColor = intent.confidence >= 0.8 ? 'green' :
intent.confidence >= 0.6 ? 'yellow' : 'red';
return [
`${chalk_1.default.bold('Intent:')} ${chalk_1.default.cyan(intent.category.replace('_', ' ').toUpperCase())}`,
`${chalk_1.default.bold('Confidence:')} ${chalk_1.default[confidenceColor](`${(intent.confidence * 100).toFixed(1)}%`)}`,
`${chalk_1.default.bold('Suggested Action:')} ${chalk_1.default.white(intent.suggestedAction || 'None')}`,
intent.entities && Object.keys(intent.entities).length > 0
? `${chalk_1.default.bold('Entities:')} ${JSON.stringify(intent.entities, null, 2)}`
: ''
].filter(Boolean).join('\n');
}
/**
* Get available intent categories
*/
getAvailableIntents() {
return Object.values(IntentCategory).filter(category => category !== IntentCategory.UNKNOWN);
}
}
exports.IntentJSParser = IntentJSParser;
// Re-export interfaces for compatibility
var IntentCategory;
(function (IntentCategory) {
IntentCategory["SYSTEM_INFO"] = "system_info";
IntentCategory["FILE_OPERATIONS"] = "file_operations";
IntentCategory["PRODUCTIVITY"] = "productivity";
IntentCategory["NETWORK"] = "network";
IntentCategory["CALCULATOR"] = "calculator";
IntentCategory["TIMER"] = "timer";
IntentCategory["NOTES"] = "notes";
IntentCategory["HELP"] = "help";
IntentCategory["EXIT"] = "exit";
IntentCategory["UNKNOWN"] = "unknown";
})(IntentCategory || (exports.IntentCategory = IntentCategory = {}));
//# sourceMappingURL=intentjs-parser.js.map