@graphteon/juricode
Version:
We are forging the future with lines of digital steel
160 lines (159 loc) ⢠7.11 kB
JavaScript
import prompts from 'prompts';
import chalk from 'chalk';
import ora from 'ora';
import boxen from 'boxen';
import OpenHands from '../api/open-hands';
import { runTaskChat } from './chat';
export const listRepositories = async () => {
const spinner = ora('Fetching repositories...').start();
try {
const repos = await OpenHands.retrieveUserGitRepositories();
spinner.succeed('Repositories fetched successfully!');
if (repos.length === 0) {
console.log(chalk.yellow('No repositories found'));
return;
}
let filteredRepos = repos;
if (repos.length > 10) {
const { useSearch } = await prompts({
type: 'confirm',
name: 'useSearch',
message: `Found ${repos.length} repositories. Would you like to search/filter them?`,
initial: false
});
if (useSearch) {
const { searchTerm } = await prompts({
type: 'text',
name: 'searchTerm',
message: 'Enter search term (repository name):',
validate: value => value.length > 0 ? true : 'Please enter a search term'
});
if (searchTerm) {
filteredRepos = repos.filter(repo => repo.full_name.toLowerCase().includes(searchTerm.toLowerCase()));
if (filteredRepos.length === 0) {
console.log(chalk.yellow(`No repositories found matching "${searchTerm}"`));
return await listRepositories();
}
console.log(chalk.green(`Found ${filteredRepos.length} repositories matching "${searchTerm}"`));
}
}
}
const repositoryChoices = filteredRepos.map(repo => ({
title: `${chalk.blue(repo.full_name)} ${repo.stargazers_count && repo.stargazers_count > 0 ? chalk.yellow('ā
' + repo.stargazers_count) : ''}`,
value: repo,
description: `Provider: ${chalk.green(repo.git_provider)} | ${repo.is_public ? chalk.green('Public') : chalk.yellow('Private')}`
}));
const choices = [];
if (repos.length > 5) {
choices.push({
title: chalk.cyan('š Search repositories'),
value: 'search',
description: 'Filter repositories by name'
});
}
choices.push(...repositoryChoices);
const { selectedRepo } = await prompts({
type: 'select',
name: 'selectedRepo',
message: 'Select a repository:',
choices: choices
});
if (selectedRepo === 'search') {
return await listRepositories();
}
if (!selectedRepo)
return;
console.log(boxen(`Repository: ${chalk.blue(selectedRepo.full_name)}
Provider: ${chalk.green(selectedRepo.git_provider)}
Visibility: ${selectedRepo.is_public ? chalk.green('Public') : chalk.yellow('Private')}
Stars: ${chalk.yellow('ā
' + (selectedRepo.stargazers_count || 0))}`, {
padding: 1,
margin: { top: 1 },
borderColor: 'blue'
}));
const branchSpinner = ora('Fetching branches...').start();
let branches = [];
try {
branches = await OpenHands.getRepositoryBranches(selectedRepo.full_name);
branchSpinner.succeed('Branches fetched successfully!');
}
catch (error) {
branchSpinner.fail('Failed to fetch branches');
console.error(chalk.red('Using default branch selection'));
}
let selectedBranch = 'main';
if (branches.length > 0) {
const defaultBranch = branches.find(b => b.name === 'main') ||
branches.find(b => b.name === 'master') ||
branches[0];
const { branch } = await prompts({
type: 'select',
name: 'branch',
message: 'Select a branch:',
choices: branches.map(branch => ({
title: `${chalk.cyan(branch.name)} ${branch.protected ? chalk.red('š') : ''}`,
value: branch.name,
description: branch.last_push_date ? `Last push: ${new Date(branch.last_push_date).toLocaleDateString()}` : ''
})),
initial: branches.findIndex(b => b.name === defaultBranch.name)
});
if (branch) {
selectedBranch = branch;
}
}
const { action } = await prompts({
type: 'select',
name: 'action',
message: 'What would you like to do with this repository?',
choices: [
{ title: 'š¬ Start Working on Repository', value: 'start-chat' },
{ title: 'ā©ļø Back to Repository List', value: 'back' },
{ title: 'š Return to Main Menu', value: 'main' }
]
});
if (action === 'start-chat') {
const { message } = await prompts({
type: 'text',
name: 'message',
message: 'Enter initial message (optional):'
});
const createSpinner = ora('Creating conversation...').start();
try {
const conversation = await OpenHands.createConversation(selectedRepo.full_name, selectedRepo.git_provider, message || undefined, [], undefined, selectedBranch);
createSpinner.succeed('Conversation created successfully!');
console.log(boxen(`š ${chalk.green('Repository Conversation Started!')}
Repository: ${chalk.blue(selectedRepo.full_name)}
Branch: ${chalk.cyan(selectedBranch)}
Conversation ID: ${chalk.cyan(conversation.conversation_id)}
Status: ${chalk.yellow(conversation.status)}`, {
padding: 1,
margin: { top: 1 },
borderColor: 'green',
borderStyle: 'double'
}));
const { startChat } = await prompts({
type: 'confirm',
name: 'startChat',
message: 'Would you like to start chatting with the AI about this repository?',
initial: true
});
if (startChat) {
console.log(chalk.blue('\nš¤ Starting chat session...\n'));
await runTaskChat(conversation.conversation_id);
}
}
catch (error) {
createSpinner.fail('Failed to create conversation');
console.error(chalk.red(error instanceof Error ? error.message : 'Unknown error'));
}
}
else if (action === 'back') {
await listRepositories();
}
}
catch (error) {
spinner.fail('Failed to fetch repositories');
console.error(chalk.red(error instanceof Error ? error.message : 'Unknown error'));
}
};
//# sourceMappingURL=repository.js.map