nascoder
Version:
š AI-powered conversational development assistant CLI with Azure AI integration, subscription management, and enterprise-grade features
110 lines (93 loc) ⢠3 kB
JavaScript
import inquirer from 'inquirer';
import chalk from 'chalk';
export class NascodePermissions {
constructor() {
this.sessionPermissions = new Set();
this.alwaysAsk = true;
}
async requestPermission(actionPlan) {
// If we already have session permissions for these actions, grant automatically
const hasSessionPermissions = actionPlan.permissions.every(
permission => this.sessionPermissions.has(permission)
);
if (hasSessionPermissions && !this.alwaysAsk) {
return true;
}
console.log(chalk.yellow('\nā ļø Permission Required:'));
if (actionPlan.permissions.includes('file_system')) {
console.log(chalk.gray(' ā” Create and modify files'));
}
if (actionPlan.permissions.includes('npm_install')) {
console.log(chalk.gray(' ā” Install npm packages'));
}
if (actionPlan.permissions.includes('network')) {
console.log(chalk.gray(' ā” Make network requests'));
}
const { choice } = await inquirer.prompt([
{
type: 'list',
name: 'choice',
message: 'Choose permission scope:',
choices: [
{
name: 'ā
Grant for this session (recommended)',
value: 'session'
},
{
name: 'ā” Grant for this action only',
value: 'once'
},
{
name: 'š Always ask (secure mode)',
value: 'always'
},
{
name: 'ā Cancel operation',
value: 'cancel'
}
]
}
]);
switch (choice) {
case 'session':
// Grant session permissions
actionPlan.permissions.forEach(permission => {
this.sessionPermissions.add(permission);
});
this.alwaysAsk = false;
console.log(chalk.green('ā
Session permissions granted'));
return true;
case 'once':
console.log(chalk.green('ā
One-time permission granted'));
return true;
case 'always':
this.alwaysAsk = true;
console.log(chalk.blue('š Secure mode enabled - will always ask'));
return true;
case 'cancel':
console.log(chalk.red('ā Operation cancelled'));
return false;
default:
return false;
}
}
hasPermission(permission) {
return this.sessionPermissions.has(permission);
}
clearSessionPermissions() {
this.sessionPermissions.clear();
this.alwaysAsk = true;
}
showPermissionStatus() {
console.log(chalk.cyan('\nš Permission Status:'));
if (this.sessionPermissions.size === 0) {
console.log(chalk.gray(' No active session permissions'));
} else {
console.log(chalk.white(' Active session permissions:'));
this.sessionPermissions.forEach(permission => {
console.log(chalk.green(` ā
${permission}`));
});
}
console.log(chalk.white(` Always ask: ${this.alwaysAsk ? 'Yes' : 'No'}`));
}
}