UNPKG

@graphteon/juricode

Version:

We are forging the future with lines of digital steel

219 lines 9.25 kB
import * as p from '@clack/prompts'; import chalk from 'chalk'; import boxen from 'boxen'; import { ConversationService } from '../api/conversation'; import { GitService } from '../api/git'; const getStatusColor = (status) => { switch (status.toLowerCase()) { case 'completed': return chalk.green(status); case 'active': return chalk.yellow(status); case 'failed': return chalk.red(status); default: return chalk.white(status); } }; export const createConversation = async () => { const gitService = new GitService(); p.intro('Create a new conversation'); const s = p.spinner(); s.start('Loading repositories'); try { const repos = await gitService.getUserRepositories(); s.stop('Repositories loaded'); const repository = await p.select({ message: 'Select a repository', options: [ { label: 'No repository (start empty conversation)', value: '' }, ...repos.map(repo => ({ label: repo.full_name, value: repo.full_name })) ] }); if (p.isCancel(repository)) { p.cancel('Operation cancelled'); return; } let selectedBranch; if (repository) { s.start('Loading branches'); try { const branches = await gitService.getRepositoryBranches(repository); s.stop('Branches loaded'); const branch = await p.select({ message: 'Select a branch', options: branches.map(branch => ({ label: branch.name, value: branch.name })) }); if (p.isCancel(branch)) { p.cancel('Operation cancelled'); return; } selectedBranch = branch; } catch (error) { p.log.error('Failed to load branches'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); return; } } const message = await p.text({ message: 'Enter initial message (optional)', }); if (p.isCancel(message)) { p.cancel('Operation cancelled'); return; } s.start('Creating conversation'); try { const conversationService = new ConversationService(); const conversation = await conversationService.createConversation({ repository: repository || undefined, git_provider: 'github', selected_branch: selectedBranch, initial_user_msg: message || undefined }); s.stop('Conversation created successfully'); console.log(boxen(`ID: ${chalk.cyan(conversation.conversation_id)} Title: ${chalk.white(conversation.title)} Status: ${getStatusColor(conversation.status)} Repository: ${chalk.yellow(conversation.repository || 'N/A')} Branch: ${chalk.yellow(conversation.selected_branch || 'N/A')} Created At: ${chalk.gray(new Date(conversation.created_at).toLocaleString())}`, { padding: 1, borderColor: 'green' })); p.outro('Conversation created successfully!'); } catch (error) { p.log.error('Failed to create conversation'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } } catch (error) { p.log.error('Failed to load repositories'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } }; export const listConversations = async () => { p.intro('List conversations'); const s = p.spinner(); s.start('Fetching conversations'); try { const conversationService = new ConversationService(); const conversations = await conversationService.getConversations(); s.stop('Conversations fetched'); if (conversations.length === 0) { p.log.warn('No conversations found'); p.outro('No conversations available'); return; } console.log(boxen(chalk.white(`Found ${conversations.length} conversation(s)`), { padding: 1, margin: { top: 1 }, borderColor: 'blue' })); conversations.forEach(conversation => { console.log(boxen(`ID: ${chalk.cyan(conversation.conversation_id)} Title: ${chalk.white(conversation.title)} Status: ${getStatusColor(conversation.status)} Repository: ${chalk.yellow(conversation.repository || 'N/A')} Branch: ${chalk.yellow(conversation.selected_branch || 'N/A')} Created: ${chalk.gray(new Date(conversation.created_at).toLocaleString())}`, { padding: 1, margin: { top: 0, bottom: 1 }, borderColor: conversation.status === 'completed' ? 'green' : 'blue', borderStyle: conversation.status === 'completed' ? 'double' : 'single' })); }); p.outro('All conversations listed successfully!'); } catch (error) { s.stop('Failed to fetch conversations'); p.log.error('Failed to fetch conversations'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } }; export const getConversationDetails = async () => { p.intro('Get conversation details'); const conversationId = await p.text({ message: 'Enter conversation ID', validate: (input) => input.length === 0 ? 'Conversation ID is required' : undefined }); if (p.isCancel(conversationId)) { p.cancel('Operation cancelled'); return; } const s = p.spinner(); s.start('Fetching conversation details'); try { const conversationService = new ConversationService(); const conversation = await conversationService.getConversation(conversationId); if (!conversation) { s.stop('Conversation not found'); p.log.error('Conversation not found'); return; } s.stop('Conversation details fetched'); console.log(boxen(`ID: ${chalk.cyan(conversation.conversation_id)} Title: ${chalk.white(conversation.title)} Status: ${getStatusColor(conversation.status)} Repository: ${chalk.yellow(conversation.repository || 'N/A')} Branch: ${chalk.yellow(conversation.selected_branch || 'N/A')} Created At: ${chalk.gray(new Date(conversation.created_at).toLocaleString())} Updated At: ${chalk.gray(new Date(conversation.updated_at).toLocaleString())}`, { padding: 1, borderColor: 'cyan' })); if (conversation.repository) { s.start('Fetching git changes'); try { const gitService = new GitService(); const changes = await gitService.getGitChanges(conversation.conversation_id); s.stop('Git changes fetched'); if (changes.length > 0) { console.log(boxen(chalk.white('Git Changes:') + '\n' + changes.map(change => `${change.status === 'modified' ? chalk.yellow('M') : change.status === 'added' ? chalk.green('A') : change.status === 'deleted' ? chalk.red('D') : chalk.white('?')} ${change.path}`).join('\n'), { padding: 1, borderColor: 'yellow', margin: { top: 1 } })); } else { p.log.info('No git changes found'); } } catch (error) { s.stop('Failed to fetch git changes'); p.log.error('Failed to fetch git changes'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } } p.outro('Details fetched successfully!'); } catch (error) { s.stop('Failed to fetch conversation details'); p.log.error('Failed to fetch conversation details'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } }; export const deleteConversation = async () => { p.intro('Delete conversation'); const conversationId = await p.text({ message: 'Enter conversation ID to delete', validate: (input) => input.length === 0 ? 'Conversation ID is required' : undefined }); if (p.isCancel(conversationId)) { p.cancel('Operation cancelled'); return; } const shouldProceed = await p.confirm({ message: chalk.red('Are you sure you want to delete this conversation? This action cannot be undone.'), initialValue: false, }); if (p.isCancel(shouldProceed) || !shouldProceed) { p.cancel('Operation cancelled'); return; } const s = p.spinner(); s.start('Deleting conversation'); try { const conversationService = new ConversationService(); await conversationService.deleteConversation(conversationId); s.stop('Conversation deleted'); p.outro('Conversation deleted successfully!'); } catch (error) { s.stop('Failed to delete conversation'); p.log.error('Failed to delete conversation'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } }; //# sourceMappingURL=conversation.js.map