import { search, query, extract, tools } from '@buger/probe';
async function basicSearchExample() {
console.log('=== Basic Search Example ===');
try {
const results = await search({
path: '/path/to/your/project',
query: 'function',
maxResults: 5
});
console.log('Search results:');
console.log(results);
} catch (error) {
console.error('Search error:', error);
}
}
async function advancedSearchExample() {
console.log('\n=== Advanced Search Example ===');
try {
const results = await search({
path: '/path/to/your/project',
query: 'config AND (parse OR tokenize)',
ignore: ['node_modules', 'dist'],
reranker: 'hybrid',
frequencySearch: true,
maxResults: 10,
maxTokens: 20000,
allowTests: false
});
console.log('Advanced search results:');
console.log(results);
} catch (error) {
console.error('Advanced search error:', error);
}
}
async function queryExample() {
console.log('\n=== Query Example ===');
try {
const jsResults = await query({
path: '/path/to/your/project',
pattern: 'function $NAME($$$PARAMS) $$$BODY',
language: 'javascript',
maxResults: 5
});
console.log('JavaScript functions:');
console.log(jsResults);
const rustResults = await query({
path: '/path/to/your/project',
pattern: 'struct $NAME $$$BODY',
language: 'rust',
maxResults: 5
});
console.log('Rust structs:');
console.log(rustResults);
} catch (error) {
console.error('Query error:', error);
}
}
async function extractExample() {
console.log('\n=== Extract Example ===');
try {
const results = await extract({
files: [
'/path/to/your/project/src/main.js',
'/path/to/your/project/src/utils.js:42' ],
contextLines: 2,
format: 'markdown'
});
console.log('Extracted code:');
console.log(results);
} catch (error) {
console.error('Extract error:', error);
}
}
async function findApiEndpoints() {
console.log('\n=== Find API Endpoints Example ===');
try {
const expressRoutes = await search({
path: '/path/to/your/project',
query: 'app.get OR app.post OR app.put OR app.delete',
maxResults: 20
});
console.log('Express.js routes:');
console.log(expressRoutes);
const springControllers = await query({
path: '/path/to/your/project',
pattern: '@RequestMapping($$$PARAMS) $$$BODY',
language: 'java',
maxResults: 20
});
console.log('Spring Boot controllers:');
console.log(springControllers);
} catch (error) {
console.error('API endpoint search error:', error);
}
}
async function generateDocumentation() {
console.log('\n=== Generate Documentation Example ===');
try {
const jsdocComments = await search({
path: '/path/to/your/project',
query: '/**',
maxResults: 50
});
console.log('JSDoc comments:');
console.log(jsdocComments);
const docs = processJsDocComments(jsdocComments);
console.log('Generated documentation:', docs);
} catch (error) {
console.error('Documentation generation error:', error);
}
}
function processJsDocComments(comments) {
return "Documentation would be generated here";
}
async function findUnusedCode() {
console.log('\n=== Find Unused Code Example ===');
try {
const functionDefs = await query({
path: '/path/to/your/project',
pattern: 'function $NAME($$$PARAMS) $$$BODY',
language: 'javascript',
maxResults: 100
});
const functionNames = extractFunctionNames(functionDefs);
const unusedFunctions = [];
for (const name of functionNames) {
const usages = await search({
path: '/path/to/your/project',
query: name,
maxResults: 2 });
if (countOccurrences(usages) <= 1) {
unusedFunctions.push(name);
}
}
console.log('Potentially unused functions:');
console.log(unusedFunctions);
} catch (error) {
console.error('Unused code search error:', error);
}
}
function extractFunctionNames(functionDefs) {
return ["someFunction", "anotherFunction"];
}
function countOccurrences(searchResults) {
return 1;
}
async function vercelAiToolsExample() {
console.log('\n=== Vercel AI SDK Tools Example ===');
try {
console.log('Using Vercel AI SDK tools:');
console.log('- searchTool:', typeof tools.searchTool);
console.log('- queryTool:', typeof tools.queryTool);
console.log('- extractTool:', typeof tools.extractTool);
console.log('\nExample usage with Vercel AI SDK:');
console.log(`
async function chatWithAI(userMessage) {
const result = await generateText({
model: provider(modelName),
messages: [{ role: 'user', content: userMessage }],
system: "You are a code intelligence assistant. Use the provided tools to search and analyze code.",
tools: {
search: tools.searchTool,
query: tools.queryTool,
extract: tools.extractTool
},
maxSteps: 15,
temperature: 0.7
});
return result.text;
}`);
} catch (error) {
console.error('Vercel AI SDK tools error:', error);
}
}
async function langchainToolsExample() {
console.log('\n=== LangChain Tools Example ===');
try {
const searchTool = tools.createSearchTool();
const queryTool = tools.createQueryTool();
const extractTool = tools.createExtractTool();
console.log('Created LangChain tools:');
console.log('- searchTool:', typeof searchTool);
console.log('- queryTool:', typeof queryTool);
console.log('- extractTool:', typeof extractTool);
console.log('\nExample usage with LangChain:');
console.log(`
async function chatWithAI(userMessage) {
const model = new ChatOpenAI({
modelName: "gpt-4o",
temperature: 0.7
}).withTools([searchTool, queryTool, extractTool]);
const result = await model.invoke([
{ role: "system", content: "You are a code intelligence assistant. Use the provided tools to search and analyze code." },
{ role: "user", content: userMessage }
]);
return result.content;
}`);
} catch (error) {
console.error('LangChain tools error:', error);
}
}
async function systemMessageExample() {
console.log('\n=== System Message Example ===');
try {
console.log('DEFAULT_SYSTEM_MESSAGE:', typeof tools.DEFAULT_SYSTEM_MESSAGE);
console.log('\nExample usage of DEFAULT_SYSTEM_MESSAGE:');
console.log(`
// Use the default system message in your AI application
const systemMessage = tools.DEFAULT_SYSTEM_MESSAGE;
// Example with Vercel AI SDK
const result = await generateText({
model: provider(modelName),
messages: [{ role: 'user', content: userMessage }],
system: tools.DEFAULT_SYSTEM_MESSAGE,
tools: {
search: tools.searchTool,
query: tools.queryTool,
extract: tools.extractTool
}
});
// Example with LangChain
const model = new ChatOpenAI({
modelName: "gpt-4o",
temperature: 0.7
}).withTools([searchTool, queryTool, extractTool]);
const result = await model.invoke([
{ role: "system", content: tools.DEFAULT_SYSTEM_MESSAGE },
{ role: "user", content: userMessage }
]);`);
} catch (error) {
console.error('System message error:', error);
}
}
async function runAllExamples() {
await basicSearchExample();
await advancedSearchExample();
await queryExample();
await extractExample();
await findApiEndpoints();
await generateDocumentation();
await findUnusedCode();
await vercelAiToolsExample();
await langchainToolsExample();
await systemMessageExample();
}
runAllExamples().catch(console.error);