@ideascol/cli-maker
Version:
A simple library to help create CLIs
112 lines (111 loc) • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBuiltInSlashCommands = getBuiltInSlashCommands;
const colors_1 = require("../colors");
/**
* Built-in slash commands available in every interactive session.
*/
function getBuiltInSlashCommands() {
return [
{
name: 'help',
description: 'Show available slash commands',
action: (_args, ctx) => {
const allCommands = getAllSlashCommands(ctx);
console.log(`\n${colors_1.Colors.Bright}${colors_1.Colors.FgCyan}Available Commands${colors_1.Colors.Reset}`);
console.log(`${colors_1.Colors.FgGray}${'─'.repeat(40)}${colors_1.Colors.Reset}`);
for (const cmd of allCommands) {
console.log(` ${colors_1.Colors.FgGreen}/${cmd.name}${colors_1.Colors.Reset} ${colors_1.Colors.FgGray}${cmd.description}${colors_1.Colors.Reset}`);
}
console.log('');
},
},
{
name: 'clear',
description: 'Clear the screen',
action: (_args, _ctx) => {
process.stdout.write('\x1B[2J\x1B[0;0H');
},
},
{
name: 'history',
description: 'Show conversation history',
action: (_args, ctx) => {
if (ctx.history.length === 0) {
console.log(`\n${colors_1.Colors.FgGray}No messages in history yet.${colors_1.Colors.Reset}\n`);
return;
}
console.log(`\n${colors_1.Colors.Bright}${colors_1.Colors.FgCyan}Conversation History${colors_1.Colors.Reset} ${colors_1.Colors.FgGray}(${ctx.history.length} messages)${colors_1.Colors.Reset}`);
console.log(`${colors_1.Colors.FgGray}${'─'.repeat(40)}${colors_1.Colors.Reset}`);
for (const msg of ctx.history) {
const roleColor = msg.role === 'user'
? colors_1.Colors.FgGreen
: msg.role === 'assistant'
? colors_1.Colors.FgCyan
: colors_1.Colors.FgYellow;
const roleLabel = msg.role === 'tool' && msg.toolName
? `tool:${msg.toolName}`
: msg.role;
const preview = msg.content.length > 80
? msg.content.slice(0, 80) + '...'
: msg.content;
console.log(` ${roleColor}[${roleLabel}]${colors_1.Colors.Reset} ${preview}`);
}
console.log('');
},
},
{
name: 'tools',
description: 'List registered tools',
action: (_args, ctx) => {
if (ctx.tools.length === 0) {
console.log(`\n${colors_1.Colors.FgGray}No tools registered.${colors_1.Colors.Reset}\n`);
return;
}
console.log(`\n${colors_1.Colors.Bright}${colors_1.Colors.FgCyan}Registered Tools${colors_1.Colors.Reset} ${colors_1.Colors.FgGray}(${ctx.tools.length})${colors_1.Colors.Reset}`);
console.log(`${colors_1.Colors.FgGray}${'─'.repeat(40)}${colors_1.Colors.Reset}`);
for (const tool of ctx.tools) {
console.log(` ${colors_1.Colors.FgYellow}⚡${colors_1.Colors.Reset} ${colors_1.Colors.Bright}${tool.name}${colors_1.Colors.Reset} ${colors_1.Colors.FgGray}${tool.description}${colors_1.Colors.Reset}`);
if (tool.parameters && tool.parameters.length > 0) {
for (const p of tool.parameters) {
const req = p.required ? `${colors_1.Colors.FgRed}*${colors_1.Colors.Reset}` : '';
console.log(` ${colors_1.Colors.FgGray}${p.name}${req} (${p.type}) — ${p.description}${colors_1.Colors.Reset}`);
}
}
}
console.log('');
},
},
{
name: 'compact',
description: 'Trim conversation history to the last N messages (default: 10)',
action: (args, ctx) => {
const keep = parseInt(args.trim(), 10) || 10;
const removed = Math.max(0, ctx.history.length - keep);
if (removed > 0) {
ctx.history.splice(0, removed);
}
console.log(`\n${colors_1.Colors.FgGreen}✓${colors_1.Colors.Reset} History compacted: kept ${ctx.history.length} messages, removed ${removed}.\n`);
},
},
{
name: 'exit',
description: 'End the interactive session',
action: (_args, _ctx) => {
// Handled specially by the session loop — this is just for /help listing
},
},
];
}
/**
* Collect built-in + custom slash commands from the session context.
* Custom commands override built-in ones if they share the same name.
*/
function getAllSlashCommands(ctx) {
// The session stores the merged list on the context; just return from tools listing
// This helper is used internally for /help rendering
const builtIn = getBuiltInSlashCommands();
// We don't have access to custom commands here, but the session merges them.
// For /help, we rely on the session passing the full list.
return builtIn;
}