mcp-repl
Version:
MCP REPL with code execution and semantic code search
119 lines (99 loc) ⢠3.97 kB
JavaScript
// Test script for enhanced code search with structural metadata
import { initialize, syncIndex, queryIndex } from '../src/js-vector-indexer.js';
import * as path from 'node:path';
import fs from 'fs/promises';
// Working directory is the root of the project
const workingDir = path.resolve('../');
// Function to run a search query and display results
async function searchCode(query, topK = 5) {
console.log(`\nš Searching for: "${query}"\n`);
try {
// Make sure the indexer is initialized
await initialize();
// Sync the index with the current codebase
const syncResult = await syncIndex([workingDir], ['js'], ['node_modules']);
console.log(`ā
Index synced: ${syncResult.total} chunks in total (${syncResult.new} new, ${syncResult.deleted} deleted)`);
// Run the query
const results = await queryIndex(query, topK);
if (results.length === 0) {
console.log('No results found.');
return;
}
// Display results with structural metadata
console.log(`\nš Found ${results.length} results:\n`);
results.forEach((result, i) => {
console.log(`\n--- Result ${i + 1} (score: ${result.score}) ---`);
console.log(`Type: ${result.type}`);
console.log(`Name: ${result.qualifiedName || result.name}`);
console.log(`File: ${result.file}`);
console.log(`Lines: ${result.startLine}-${result.endLine} (${result.lines} lines)`);
// Display documentation if available
if (result.doc) {
console.log(`\nDocumentation: ${result.doc}`);
}
// Display structural metadata if available
if (result.structure) {
console.log('\nStructural metadata:');
for (const [key, value] of Object.entries(result.structure)) {
if (Array.isArray(value)) {
if (value.length > 0) {
console.log(` - ${key}: ${value.length} items`);
// For parameters, show more details
if (key === 'parameters' && value.length > 0) {
value.forEach(param => {
console.log(` ⢠${param.name}${param.type ? `: ${param.type}` : ''}`);
});
}
}
} else if (typeof value === 'object' && value !== null) {
console.log(` - ${key}: ${JSON.stringify(value)}`);
} else {
console.log(` - ${key}: ${value}`);
}
}
}
// Display relationships if available
if (result.relationships && Object.keys(result.relationships).length > 0) {
console.log('\nRelationships:');
for (const [key, value] of Object.entries(result.relationships)) {
if (Array.isArray(value)) {
console.log(` - ${key}: ${value.join(', ')}`);
} else {
console.log(` - ${key}: ${value}`);
}
}
}
// Display code preview
if (result.code) {
console.log('\nCode Preview:');
console.log(`${result.code.length > 150 ? result.code.substring(0, 150) + '...' : result.code}`);
}
console.log('\n' + '-'.repeat(50));
});
} catch (error) {
console.error('Error during search:', error);
}
}
// Main function to run the test
async function main() {
console.log('š Testing Enhanced Code Search with Structural Metadata\n');
// Sample search queries to test
const queries = [
'extract chunks',
'code structure',
'function for searching',
'class definition',
'relationships between code'
];
// Run searches
for (const query of queries) {
await searchCode(query);
}
console.log('\n⨠Search tests completed!');
}
// Run the main function
main().catch(error => {
console.error('Error:', error);
process.exit(1);
});