UNPKG

autoagent-cli

Version:

Run autonomous AI agents using Claude or Gemini for task execution

303 lines (302 loc) 11.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.STMManager = exports.STMError = void 0; const simple_task_master_1 = require("simple-task-master"); class STMError extends Error { constructor(message, operation, cause) { super(message); this.operation = operation; this.cause = cause; this.name = 'STMError'; } } exports.STMError = STMError; class STMManager { constructor(config = {}) { this.taskManager = null; this.initializationPromise = null; this.config = config; } async ensureInitialized() { if (this.taskManager) { return; } if (this.initializationPromise) { return this.initializationPromise; } this.initializationPromise = this.initializeTaskManager(); return this.initializationPromise; } async initializeTaskManager() { try { this.taskManager = await simple_task_master_1.TaskManager.create(this.config); } catch (error) { const stmError = new STMError('Failed to initialize STM TaskManager', 'initialization', error instanceof Error ? error : new Error(String(error))); this.initializationPromise = null; throw stmError; } } async createTask(title, content) { await this.ensureInitialized(); try { const taskInput = { title, content: this.formatTaskContent(content), tags: this.buildTaskTags(content.tags), status: 'pending' }; if (!this.taskManager) { throw new STMError('TaskManager not initialized', 'create'); } const task = await this.taskManager.create(taskInput); return String(task.id); } catch (error) { throw new STMError(`Failed to create STM task: ${title}`, 'create', error instanceof Error ? error : new Error(String(error))); } } async getTask(id) { await this.ensureInitialized(); try { const numericId = parseInt(id, 10); if (isNaN(numericId) || numericId <= 0) { return null; } if (!this.taskManager) { throw new STMError('TaskManager not initialized', 'get'); } return await this.taskManager.get(numericId); } catch (error) { if (error instanceof simple_task_master_1.NotFoundError) { return null; } throw new STMError(`Failed to get STM task with ID: ${id}`, 'get', error instanceof Error ? error : new Error(String(error))); } } async listTasks(filters) { await this.ensureInitialized(); try { if (!this.taskManager) { throw new STMError('TaskManager not initialized', 'list'); } return await this.taskManager.list(filters); } catch (error) { const filtersDescription = filters ? ` with filters: ${JSON.stringify(filters)}` : ''; throw new STMError(`Failed to list STM tasks${filtersDescription}`, 'list', error instanceof Error ? error : new Error(String(error))); } } async updateTask(id, updates) { await this.ensureInitialized(); try { const numericId = parseInt(id, 10); if (isNaN(numericId) || numericId <= 0) { throw new STMError(`Invalid task ID format: ${id}. Expected a positive numeric ID.`, 'update'); } if (!this.taskManager) { throw new STMError('TaskManager not initialized', 'update'); } return await this.taskManager.update(numericId, updates); } catch (error) { if (error instanceof STMError) { throw error; } if (error instanceof simple_task_master_1.NotFoundError) { throw new STMError(`Task with ID ${id} not found`, 'update', error); } throw new STMError(`Failed to update STM task with ID: ${id}`, 'update', error instanceof Error ? error : new Error(String(error))); } } async markTaskComplete(id) { return this.updateTask(id, { status: 'done' }); } async markTaskInProgress(id) { return this.updateTask(id, { status: 'in-progress' }); } async updateTaskStatus(id, status) { let stmStatus; if (status === 'completed') { stmStatus = 'done'; } else if (['pending', 'in-progress', 'done'].includes(status)) { stmStatus = status; } else { throw new STMError(`Invalid status: ${status}. Valid statuses are: pending, in-progress, done, completed`, 'updateStatus'); } return this.updateTask(id, { status: stmStatus }); } async searchTasks(searchPattern, options) { await this.ensureInitialized(); try { const filters = { search: searchPattern }; if (options?.status) { filters.status = options.status; } if (options?.tags) { filters.tags = options.tags; } if (!this.taskManager) { throw new STMError('TaskManager not initialized', 'search'); } const results = await this.taskManager.list(filters); return results; } catch (error) { throw new STMError(`Failed to search STM tasks with pattern: ${searchPattern}`, 'search', error instanceof Error ? error : new Error(String(error))); } } isInitialized() { return this.taskManager !== null; } reset() { this.taskManager = null; this.initializationPromise = null; } async getTaskSections(taskId) { await this.ensureInitialized(); if (!this.taskManager) { throw new STMError('Task manager not initialized', 'getTaskSections'); } try { const numericId = this.parseTaskId(taskId); const task = await this.taskManager.get(numericId); if (task.content === undefined || task.content === null || task.content === '') { return {}; } const sections = {}; const lines = task.content.split('\n'); let currentSection = null; let sectionContent = []; for (const line of lines) { if (line.startsWith('## ')) { if (currentSection !== null && sectionContent.length > 0) { const content = sectionContent.join('\n').trim(); sections[currentSection] = content; } const sectionName = line.substring(3).toLowerCase(); if (['description', 'details', 'validation'].includes(sectionName)) { currentSection = sectionName; sectionContent = []; } else { currentSection = null; } } else if (currentSection !== null) { sectionContent.push(line); } } if (currentSection !== null && sectionContent.length > 0) { const content = sectionContent.join('\n').trim(); sections[currentSection] = content; } return sections; } catch (error) { if (error instanceof simple_task_master_1.NotFoundError) { throw new STMError(`Task not found: ${taskId}`, 'getTaskSections', error); } throw this.wrapError(error, 'getTaskSections'); } } parseTaskId(taskId) { const id = parseInt(taskId, 10); if (isNaN(id)) { throw new STMError(`Invalid task ID: ${taskId}`, 'parseTaskId'); } return id; } wrapError(error, operation) { if (error instanceof STMError) { return error; } return new STMError(`STM operation failed: ${error instanceof Error ? error.message : String(error)}`, operation, error instanceof Error ? error : undefined); } formatTaskContent(content) { const sections = []; const descriptionSection = this.formatDescription(content); if (descriptionSection) { sections.push(descriptionSection); sections.push(''); } const detailsSection = this.formatDetails(content); if (detailsSection) { sections.push(detailsSection); sections.push(''); } const validationSection = this.formatValidation(content); if (validationSection) { sections.push(validationSection); sections.push(''); } return sections.join('\n').trim(); } formatDescription(content) { const sections = []; if (content.description !== undefined && content.description !== '') { sections.push('## Why & what\n'); sections.push(content.description); if (content.acceptanceCriteria !== undefined && content.acceptanceCriteria.length > 0) { sections.push('\n### Acceptance Criteria\n'); content.acceptanceCriteria.forEach(criteria => { sections.push(`- [ ] ${criteria}`); }); } } return sections.join('\n'); } formatDetails(content) { const sections = []; if ((content.technicalDetails !== undefined && content.technicalDetails !== '') || (content.implementationPlan !== undefined && content.implementationPlan !== '')) { sections.push('## How\n'); if (content.technicalDetails !== undefined && content.technicalDetails !== '') { sections.push(content.technicalDetails); } if (content.implementationPlan !== undefined && content.implementationPlan !== '') { if (content.technicalDetails !== undefined && content.technicalDetails !== '') { sections.push('\n### Implementation Plan\n'); } sections.push(content.implementationPlan); } } return sections.join('\n'); } formatValidation(content) { const sections = []; if ((content.testingStrategy !== undefined && content.testingStrategy !== '') || (content.verificationSteps !== undefined && content.verificationSteps !== '')) { sections.push('## Validation\n'); if (content.testingStrategy !== undefined && content.testingStrategy !== '') { sections.push('### Testing Strategy\n'); sections.push(content.testingStrategy); } if (content.verificationSteps !== undefined && content.verificationSteps !== '') { if (content.testingStrategy !== undefined && content.testingStrategy !== '') { sections.push('\n### Verification Steps\n'); } else { sections.push('### Verification Steps\n'); } sections.push(content.verificationSteps); } } return sections.join('\n'); } buildTaskTags(contentTags) { const tags = ['autoagent']; if (contentTags && contentTags.length > 0) { tags.push(...contentTags); } return tags; } } exports.STMManager = STMManager;