This document provides detailed information on using CodeWhisper programmatically in your Node.js projects.
- Installation
- Basic Usage
- Advanced Usage
- API Reference
- Examples
- Error Handling
- Best Practices
- Troubleshooting
To use CodeWhisper as a library in your project, install it using npm:
npm install codewhisper
Here's a basic example of using CodeWhisper in your Node.js project:
import { processFiles, generateMarkdown } from 'codewhisper';
async function generateCodeSummary() {
try {
const files = await processFiles({
path: '/path/to/project',
filter: ['**/*.js', '**/*.ts'],
exclude: ['**/node_modules/**'],
});
const markdown = await generateMarkdown(files, 'default', {
noCodeblock: false,
customData: {
projectName: 'My Project',
},
});
console.log(markdown);
} catch (error) {
console.error('Error generating code summary:', error);
}
}
generateCodeSummary();
import { processFiles, generateMarkdown } from 'codewhisper';
import fs from 'fs/promises';
async function generateCustomOutput() {
try {
const files = await processFiles({
path: '/path/to/project',
filter: ['src/**/*.js'],
});
const customTemplate = await fs.readFile(
'path/to/custom-template.hbs',
'utf-8'
);
const output = await generateMarkdown(files, customTemplate, {
customData: {
projectName: 'My Project',
version: '1.0.0',
},
});
await fs.writeFile('output.md', output);
console.log('Custom output generated successfully!');
} catch (error) {
console.error('Error generating custom output:', error);
}
}
generateCustomOutput();
import { runAIAssistedTask } from 'codewhisper';
async function performAITask() {
try {
await runAIAssistedTask({
path: '/path/to/project',
task: 'Implement user authentication',
description: 'Add user login and registration functionality using JWT',
instructions: 'Use bcrypt for password hashing...',
context: ['src/api/auth.js', 'src/utils/auth.js'],
model: 'claude-3-5-sonnet-20240620',
dryRun: true,
});
console.log('AI-assisted task completed successfully!');
} catch (error) {
console.error('Error in AI-assisted task:', error);
}
}
performAITask();
Processes files in the specified directory based on the given options.
function processFiles(options: ProcessOptions): Promise<FileInfo[]>;
Option | Type | Description | Default |
---|---|---|---|
path |
string |
Path to the codebase | Current directory |
gitignore |
string |
Path to .gitignore file | - |
filter |
string[] |
File patterns to include (glob patterns) | - |
exclude |
string[] |
File patterns to exclude (glob patterns) | - |
suppressComments |
boolean |
Strip comments from the code | false |
caseSensitive |
boolean |
Use case-sensitive pattern matching | false |
customIgnores |
string[] |
Additional patterns to ignore | - |
cachePath |
string |
Custom path for the cache file | - |
respectGitignore |
boolean |
Respect entries in .gitignore | true |
Promise<FileInfo[]>
- An array of processed file information
const files = await processFiles({
path: '/path/to/project',
filter: ['**/*.js', '**/*.ts'],
exclude: ['**/node_modules/**'],
});
Generates markdown output based on the processed files and template.
function generateMarkdown(
files: FileInfo[],
templateContent: string,
options: MarkdownOptions
): Promise<string>;
Parameter | Type | Description |
---|---|---|
files |
FileInfo[] |
Array of processed file information |
templateContent |
string |
Handlebars template content |
options |
MarkdownOptions |
Configuration options |
Option | Type | Description | Default |
---|---|---|---|
noCodeblock |
boolean |
Disable wrapping code inside markdown code blocks | false |
customData |
Record<string, string> |
Custom data to pass to the template | - |
basePath |
string |
Base path for relative file paths | - |
lineNumbers |
boolean |
Add line numbers to code blocks | false |
Promise<string>
- Generated markdown content
const markdown = await generateMarkdown(files, customTemplate, {
customData: {
projectName: 'My Project',
version: '1.0.0',
},
lineNumbers: true,
});
Runs an AI-assisted coding task.
function runAIAssistedTask(options: AiAssistedTaskOptions): Promise<void>;
Option | Type | Description | Default |
---|---|---|---|
path |
string |
Path to the codebase | Current directory |
task |
string |
Short task title | - |
description |
string |
Detailed task description | - |
instructions |
string |
Additional instructions for the task | - |
model |
string |
AI model to use | - |
dryRun |
boolean |
Perform a dry run without making actual changes | false |
maxCostThreshold |
number |
Maximum cost threshold for AI operations | - |
autoCommit |
boolean |
Automatically commit changes | false |
Promise<void>
await runAIAssistedTask({
path: '/path/to/project',
task: 'Refactor authentication module',
description: 'Improve the current authentication module...',
instructions: 'Use bcrypt for password hashing...',
model: 'claude-3-5-sonnet-20240620',
dryRun: false,
autoCommit: true,
});
import { processFiles, generateMarkdown } from 'codewhisper';
import fs from 'fs/promises';
async function generateSecurityReview() {
try {
const files = await processFiles({
path: '/path/to/project',
filter: ['**/*.js', '**/*.ts', '**/*.php'],
exclude: ['**/vendor/**', '**/node_modules/**'],
});
const templateContent = await fs.readFile(
'security-review-template.hbs',
'utf-8'
);
const markdown = await generateMarkdown(files, templateContent, {
customData: {
projectName: 'My Secure Project',
reviewDate: new Date().toISOString(),
},
lineNumbers: true,
});
await fs.writeFile('security-review.md', markdown);
console.log('Security review generated successfully!');
} catch (error) {
console.error('Error generating security review:', error);
}
}
generateSecurityReview();
import { runAIAssistedTask } from 'codewhisper';
async function refactorCode() {
try {
await runAIAssistedTask({
path: '/path/to/project',
task: 'Refactor authentication module',
description:
'Improve the current authentication module by implementing proper password hashing, adding multi-factor authentication support, and reorganizing the code for better maintainability.',
instructions:
'Use bcrypt for password hashing. Implement TOTP for multi-factor authentication. Follow SOLID principles in the refactored code.',
model: 'claude-3-5-sonnet-20240620',
dryRun: false,
autoCommit: true,
});
console.log('Refactoring task completed successfully!');
} catch (error) {
console.error('Error during refactoring task:', error);
}
}
refactorCode();
CodeWhisper functions throw errors when they encounter issues. It's recommended to wrap your code in try-catch blocks to handle these errors gracefully. For example:
try {
const files = await processFiles(options);
// ... rest of your code
} catch (error) {
console.error('Error processing files:', error.message);
// Handle the error appropriately
}
- Always use error handling when working with CodeWhisper functions.
- Use the
dryRun
option when testing AI-assisted tasks to preview changes before applying them. - Regularly update CodeWhisper to benefit from the latest features and bug fixes.
- Use custom templates to tailor the output to your specific needs.
- Leverage the
customData
option to pass project-specific information to your templates.
If you encounter issues while using CodeWhisper:
- Check that you're using the latest version of CodeWhisper.
- Verify that your project structure and file patterns are correct.
- Review the error messages for specific information about what went wrong.
- Consult the FAQ section in the README for common issues and solutions.
- If the problem persists, please open an issue on GitHub with a detailed description of the problem and steps to reproduce it.