Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { initCommand } from './commands/init.js';
import { planCommand } from './commands/plan.js';
import { searchCommand } from './commands/search.js';
import { statsCommand } from './commands/stats.js';
import { storageCommand } from './commands/storage.js';
import { updateCommand } from './commands/update.js';

const program = new Command();
Expand All @@ -31,6 +32,7 @@ program.addCommand(updateCommand);
program.addCommand(statsCommand);
program.addCommand(compactCommand);
program.addCommand(cleanCommand);
program.addCommand(storageCommand);

// Show help if no command provided
if (process.argv.length === 2) {
Expand Down
54 changes: 24 additions & 30 deletions packages/cli/src/commands/clean.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import {
ensureStorageDirectory,
getStorageFilePaths,
getStoragePath,
} from '@lytics/dev-agent-core';
import chalk from 'chalk';
import { Command } from 'commander';
import ora from 'ora';
Expand All @@ -19,15 +24,23 @@ export const cleanCommand = new Command('clean')
return;
}

const dataDir = path.dirname(config.vectorStorePath);
const stateFile = path.join(config.repositoryPath, '.dev-agent', 'indexer-state.json');
// Resolve repository path
const repositoryPath = config.repository?.path || config.repositoryPath || process.cwd();
const resolvedRepoPath = path.resolve(repositoryPath);

// Get centralized storage paths
const storagePath = await getStoragePath(resolvedRepoPath);
await ensureStorageDirectory(storagePath);
const filePaths = getStorageFilePaths(storagePath);

// Show what will be deleted
logger.log('');
logger.log(chalk.bold('The following will be deleted:'));
logger.log(` ${chalk.cyan('Vector store:')} ${config.vectorStorePath}`);
logger.log(` ${chalk.cyan('State file:')} ${stateFile}`);
logger.log(` ${chalk.cyan('Data directory:')} ${dataDir}`);
logger.log(` ${chalk.cyan('Storage directory:')} ${storagePath}`);
logger.log(` ${chalk.cyan('Vector store:')} ${filePaths.vectors}`);
logger.log(` ${chalk.cyan('State file:')} ${filePaths.indexerState}`);
logger.log(` ${chalk.cyan('GitHub state:')} ${filePaths.githubState}`);
logger.log(` ${chalk.cyan('Metadata:')} ${filePaths.metadata}`);
logger.log('');

// Confirm unless --force
Expand All @@ -40,35 +53,16 @@ export const cleanCommand = new Command('clean')

const spinner = ora('Cleaning indexed data...').start();

// Delete vector store
try {
await fs.rm(config.vectorStorePath, { recursive: true, force: true });
spinner.text = 'Deleted vector store';
} catch (error) {
logger.debug(`Vector store not found or already deleted: ${error}`);
}

// Delete state file
// Delete storage directory (contains all index files)
try {
await fs.rm(stateFile, { force: true });
spinner.text = 'Deleted state file';
await fs.rm(storagePath, { recursive: true, force: true });
spinner.succeed(chalk.green('Cleaned successfully!'));
} catch (error) {
logger.debug(`State file not found or already deleted: ${error}`);
spinner.fail('Failed to clean');
logger.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}

// Delete data directory if empty
try {
const files = await fs.readdir(dataDir);
if (files.length === 0) {
await fs.rmdir(dataDir);
spinner.text = 'Deleted data directory';
}
} catch (error) {
logger.debug(`Data directory not found or not empty: ${error}`);
}

spinner.succeed(chalk.green('Cleaned successfully!'));

logger.log('');
logger.log('All indexed data has been removed.');
logger.log(`Run ${chalk.yellow('dev index')} to re-index your repository.`);
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/commands/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('CLI Commands', () => {
exitSpy.mockRestore();

// Check config file was created
const configPath = path.join(initDir, '.dev-agent.json');
const configPath = path.join(initDir, '.dev-agent', 'config.json');
const exists = await fs
.access(configPath)
.then(() => true)
Expand All @@ -46,6 +46,9 @@ describe('CLI Commands', () => {
// Verify config content
const content = await fs.readFile(configPath, 'utf-8');
const config = JSON.parse(content);
expect(config.version).toBe('1.0');
expect(config.repository).toBeDefined();
// Legacy fields for backward compatibility
expect(config.repositoryPath).toBe(path.resolve(initDir));
expect(config.embeddingModel).toBe('Xenova/all-MiniLM-L6-v2');
});
Expand Down
26 changes: 24 additions & 2 deletions packages/cli/src/commands/compact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { RepositoryIndexer } from '@lytics/dev-agent-core';
import * as path from 'node:path';
import {
ensureStorageDirectory,
getStorageFilePaths,
getStoragePath,
RepositoryIndexer,
} from '@lytics/dev-agent-core';
import chalk from 'chalk';
import { Command } from 'commander';
import ora from 'ora';
Expand All @@ -21,8 +27,24 @@ export const compactCommand = new Command('compact')
return;
}

// Resolve repository path
const repositoryPath = config.repository?.path || config.repositoryPath || process.cwd();
const resolvedRepoPath = path.resolve(repositoryPath);

// Get centralized storage paths
const storagePath = await getStoragePath(resolvedRepoPath);
await ensureStorageDirectory(storagePath);
const filePaths = getStorageFilePaths(storagePath);

spinner.text = 'Initializing indexer...';
const indexer = new RepositoryIndexer(config);
const indexer = new RepositoryIndexer({
repositoryPath: resolvedRepoPath,
vectorStorePath: filePaths.vectors,
statePath: filePaths.indexerState,
excludePatterns: config.repository?.excludePatterns || config.excludePatterns,
languages: config.repository?.languages || config.languages,
});

await indexer.initialize();

// Get stats before optimization
Expand Down
53 changes: 47 additions & 6 deletions packages/cli/src/commands/explore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { RepositoryIndexer } from '@lytics/dev-agent-core';
import * as path from 'node:path';
import {
ensureStorageDirectory,
getStorageFilePaths,
getStoragePath,
RepositoryIndexer,
} from '@lytics/dev-agent-core';
import chalk from 'chalk';
import { Command } from 'commander';
import ora from 'ora';
Expand Down Expand Up @@ -26,7 +32,23 @@ explore
return;
}

const indexer = new RepositoryIndexer(config);
// Resolve repository path
const repositoryPath = config.repository?.path || config.repositoryPath || process.cwd();
const resolvedRepoPath = path.resolve(repositoryPath);

// Get centralized storage paths
const storagePath = await getStoragePath(resolvedRepoPath);
await ensureStorageDirectory(storagePath);
const filePaths = getStorageFilePaths(storagePath);

const indexer = new RepositoryIndexer({
repositoryPath: resolvedRepoPath,
vectorStorePath: filePaths.vectors,
statePath: filePaths.indexerState,
excludePatterns: config.repository?.excludePatterns || config.excludePatterns,
languages: config.repository?.languages || config.languages,
});

await indexer.initialize();

spinner.text = `Searching: "${query}"`;
Expand Down Expand Up @@ -85,20 +107,36 @@ explore
return;
}

// Resolve repository path
const repositoryPath = config.repository?.path || config.repositoryPath || process.cwd();
const resolvedRepoPath = path.resolve(repositoryPath);

// Get centralized storage paths
const storagePath = await getStoragePath(resolvedRepoPath);
await ensureStorageDirectory(storagePath);
const filePaths = getStorageFilePaths(storagePath);

// Prepare file for search (read content, resolve paths)
spinner.text = 'Reading file content...';
const { prepareFileForSearch } = await import('../utils/file.js');

let fileInfo: Awaited<ReturnType<typeof prepareFileForSearch>>;
try {
fileInfo = await prepareFileForSearch(config.repositoryPath, file);
fileInfo = await prepareFileForSearch(resolvedRepoPath, file);
} catch (error) {
spinner.fail((error as Error).message);
process.exit(1);
return;
}

const indexer = new RepositoryIndexer(config);
const indexer = new RepositoryIndexer({
repositoryPath: resolvedRepoPath,
vectorStorePath: filePaths.vectors,
statePath: filePaths.indexerState,
excludePatterns: config.repository?.excludePatterns || config.excludePatterns,
languages: config.repository?.languages || config.languages,
});

await indexer.initialize();

// Search using file content, not filename
Expand All @@ -124,15 +162,18 @@ explore
return;
}

console.log(chalk.cyan(`\n🔗 Similar to: ${file}\n`));
console.log(chalk.cyan(`\n🔍 Similar Code to: ${file}\n`));

for (const [i, result] of similar.entries()) {
const meta = result.metadata as {
path: string;
name?: string;
type: string;
startLine?: number;
};

console.log(chalk.white(`${i + 1}. ${meta.path}`));
console.log(chalk.white(`${i + 1}. ${meta.name || meta.type}`));
console.log(chalk.gray(` ${meta.path}${meta.startLine ? `:${meta.startLine}` : ''}`));
console.log(chalk.green(` ${(result.score * 100).toFixed(1)}% similar\n`));
}

Expand Down
37 changes: 33 additions & 4 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { RepositoryIndexer } from '@lytics/dev-agent-core';
import {
ensureStorageDirectory,
getStorageFilePaths,
getStoragePath,
RepositoryIndexer,
updateIndexedStats,
} from '@lytics/dev-agent-core';
import chalk from 'chalk';
import { Command } from 'commander';
import ora from 'ora';
Expand All @@ -21,11 +27,26 @@ export const indexCommand = new Command('index')
config = getDefaultConfig(repositoryPath);
}

// Override with command line args
config.repositoryPath = repositoryPath;
// Override repository path with command line arg
const resolvedRepoPath = repositoryPath;

// Get centralized storage path
spinner.text = 'Resolving storage path...';
const storagePath = await getStoragePath(resolvedRepoPath);
await ensureStorageDirectory(storagePath);
const filePaths = getStorageFilePaths(storagePath);

spinner.text = 'Initializing indexer...';
const indexer = new RepositoryIndexer(config);
const indexer = new RepositoryIndexer({
repositoryPath: resolvedRepoPath,
vectorStorePath: filePaths.vectors,
statePath: filePaths.indexerState,
excludePatterns: config.repository?.excludePatterns || config.excludePatterns,
languages: config.repository?.languages || config.languages,
embeddingModel: config.embeddingModel,
embeddingDimension: config.dimension,
});

await indexer.initialize();

spinner.text = 'Scanning repository...';
Expand All @@ -47,6 +68,13 @@ export const indexCommand = new Command('index')
},
});

// Update metadata with indexing stats
await updateIndexedStats(storagePath, {
files: stats.filesScanned,
components: stats.documentsIndexed,
size: 0, // TODO: Calculate actual size
});

await indexer.close();

const duration = ((Date.now() - startTime) / 1000).toFixed(2);
Expand All @@ -61,6 +89,7 @@ export const indexCommand = new Command('index')
logger.log(` ${chalk.cyan('Documents indexed:')} ${stats.documentsIndexed}`);
logger.log(` ${chalk.cyan('Vectors stored:')} ${stats.vectorsStored}`);
logger.log(` ${chalk.cyan('Duration:')} ${duration}s`);
logger.log(` ${chalk.cyan('Storage:')} ${storagePath}`);

if (stats.errors.length > 0) {
logger.log('');
Expand Down
26 changes: 24 additions & 2 deletions packages/cli/src/commands/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* Generate development plan from GitHub issue
*/

import { RepositoryIndexer } from '@lytics/dev-agent-core';
import * as path from 'node:path';
import {
ensureStorageDirectory,
getStorageFilePaths,
getStoragePath,
RepositoryIndexer,
} from '@lytics/dev-agent-core';
import chalk from 'chalk';
import { Command } from 'commander';
import ora from 'ora';
Expand Down Expand Up @@ -93,7 +99,23 @@ export const planCommand = new Command('plan')
if (options.explorer !== false) {
spinner.text = 'Finding relevant code...';

const indexer = new RepositoryIndexer(config);
// Resolve repository path
const repositoryPath = config.repository?.path || config.repositoryPath || process.cwd();
const resolvedRepoPath = path.resolve(repositoryPath);

// Get centralized storage paths
const storagePath = await getStoragePath(resolvedRepoPath);
await ensureStorageDirectory(storagePath);
const filePaths = getStorageFilePaths(storagePath);

const indexer = new RepositoryIndexer({
repositoryPath: resolvedRepoPath,
vectorStorePath: filePaths.vectors,
statePath: filePaths.indexerState,
excludePatterns: config.repository?.excludePatterns || config.excludePatterns,
languages: config.repository?.languages || config.languages,
});

await indexer.initialize();

for (const task of tasks) {
Expand Down
Loading