UNPKG

hataraku

Version:

An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.

223 lines 10.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerTaskCommands = registerTaskCommands; const chalk_1 = __importDefault(require("chalk")); const prompts_1 = require("@inquirer/prompts"); const TaskManager_1 = require("../../config/TaskManager"); const ProfileManager_1 = require("../../config/ProfileManager"); const agent_manager_1 = require("../../config/agent-manager"); const config_loader_1 = require("../../config/config-loader"); const execution_1 = require("../execution"); function registerTaskCommands(program) { const taskCommand = program .command('task') .description('Manage tasks'); taskCommand .command('list') .description('List all tasks') .action(async () => { try { const taskManager = new TaskManager_1.TaskManager(); const tasks = await taskManager.listTasks(); console.log(chalk_1.default.bold('\nAvailable Tasks:')); if (tasks.length === 0) { console.log(chalk_1.default.gray(' No tasks found.')); } else { for (const task of tasks) { console.log(` ${chalk_1.default.blue('•')} ${task}`); } } console.log(''); } catch (error) { console.error(chalk_1.default.red('Error listing tasks:'), error); process.exit(1); } }); taskCommand .command('show <n>') .description('Show task details') .action(async (name) => { try { const taskManager = new TaskManager_1.TaskManager(); const task = await taskManager.getTask(name); console.log(chalk_1.default.bold(`\nTask: ${task.name}`)); console.log(chalk_1.default.gray('─'.repeat(30))); console.log(`${chalk_1.default.blue('Description:')} ${task.description}`); console.log(`${chalk_1.default.blue('Agent:')} ${task.agent}`); if (task.schema) { console.log(chalk_1.default.blue('\nInput Schema:')); console.log(` ${JSON.stringify(task.schema, null, 2).replace(/\n/g, '\n ')}`); } console.log(chalk_1.default.blue('\nTask Definition:')); if (typeof task.task === 'string') { console.log(` ${task.task.substring(0, 100)}${task.task.length > 100 ? '...' : ''}`); } else { console.log(` Template with parameters: ${task.task.parameters.join(', ')}`); } console.log(''); } catch (error) { console.error(chalk_1.default.red('Error showing task:'), error); process.exit(1); } }); taskCommand .command('run <n>') .description('Run a task') .option('--agent <agent>', 'Use a specific agent for this task') .option('--provider <provider>', 'Use a specific provider for this task') .option('--model <model>', 'Use a specific model for this task') .option('-p, --param <params...>', 'Specify task parameters in key=value format (e.g., --param fileName=main.js language=JavaScript)') .option('--no-interactive', 'Skip interactive prompts for parameters') .action(async (name, options) => { try { const taskManager = new TaskManager_1.TaskManager(); const task = await taskManager.getTask(name); console.log(chalk_1.default.blue(`\nPreparing to run task: ${task.name}`)); // Get input for task const inputData = {}; // First, parse any parameters passed via command line if (options.param && options.param.length > 0) { for (const paramPair of options.param) { const [key, ...valueParts] = paramPair.split('='); const value = valueParts.join('='); // Rejoin in case value contains "=" if (key && value !== undefined) { // Convert value based on the schema type if available if (task.schema && task.schema.properties && task.schema.properties[key]) { const propType = task.schema.properties[key].type; if (propType === 'array') { inputData[key] = value.split(',').map(item => item.trim()); } else if (propType === 'boolean') { inputData[key] = value.toLowerCase() === 'true'; } else if (propType === 'number') { inputData[key] = Number(value); } else { inputData[key] = value; } } else { inputData[key] = value; } console.log(chalk_1.default.green(`Parameter: ${key}=${JSON.stringify(inputData[key])}`)); } else { console.error(chalk_1.default.yellow(`Warning: Skipping invalid parameter format: ${paramPair}. Use key=value format.`)); } } } // If interactive mode is enabled and we have a schema, prompt for missing values if (options.interactive !== false && task.schema) { const schema = task.schema; if (schema.properties) { for (const [key, prop] of Object.entries(schema.properties)) { // Skip if already provided via command line if (inputData[key] !== undefined) continue; const isRequired = schema.required && schema.required.includes(key); const promptMessage = `${prop.description || key}${isRequired ? ' (required)' : ''}:`; if (prop.type === 'array') { const items = await (0, prompts_1.input)({ message: promptMessage, validate: value => { if (isRequired && !value) return 'This field is required'; return true; } }); inputData[key] = items.split(',').map(item => item.trim()); } else if (prop.type === 'boolean') { inputData[key] = await (0, prompts_1.select)({ message: promptMessage, choices: [ { name: 'Yes', value: true }, { name: 'No', value: false } ] }); } else { inputData[key] = await (0, prompts_1.input)({ message: promptMessage, validate: value => { if (isRequired && !value) return 'This field is required'; return true; } }); } } } } // Validate if all required parameters are provided if (task.schema && task.schema.required) { const required = task.schema.required; const missingParams = required.filter((param) => inputData[param] === undefined); if (missingParams.length > 0) { console.error(chalk_1.default.red(`Error: Missing required parameters: ${missingParams.join(', ')}`)); process.exit(1); } } // Process task template const prompt = taskManager.processTaskTemplate(task, inputData); console.log(chalk_1.default.blue('\nExecuting task...')); // Get agent (from option, task config, or default) const configLoader = new config_loader_1.ConfigLoader(); const profile = await (new ProfileManager_1.ProfileManager()).getActiveProfile(); let agent; if (options.agent) { try { agent = await (new agent_manager_1.AgentManager()).getAgent(options.agent); } catch (error) { console.error(chalk_1.default.yellow(`Warning: Agent '${options.agent}' not found. Using task agent.`)); } } if (!agent) { try { agent = await (new agent_manager_1.AgentManager()).getAgent(task.agent); } catch (error) { console.error(chalk_1.default.yellow(`Warning: Task agent '${task.agent}' not found. Using default configuration.`)); } } // Execute task with agent or directly with model const cliOptions = { provider: options.provider || profile.provider, model: options.model || profile.model, agent: agent?.name, stream: profile.options?.stream, sound: profile.options?.sound }; console.log(chalk_1.default.blue('\nExecuting task with prompt:')); console.log(chalk_1.default.green(prompt)); try { // Use the executeWithConfig function to actually run the task const code = await (0, execution_1.executeWithConfig)(prompt, cliOptions, false); if (code !== 0) { console.error(chalk_1.default.red(`Task execution failed with code ${code}`)); process.exit(code); } process.exit(0); } catch (execError) { console.error(chalk_1.default.red('Error executing task:'), execError); process.exit(1); } } catch (error) { console.error(chalk_1.default.red('Error running task:'), error); process.exit(1); } }); return program; } //# sourceMappingURL=task.js.map