editFileAndApplyDiff
codebolt.utils.editFileAndApplyDiff(filePath: string, diff: string, diffIdentifier: string, prompt: string, applyModel: string): Promise<FsEditFileAndApplyDiffResponse>
Edits a file and applies a diff with AI assistance.
Parameters
filePath(string): The path to the file to edit.diff(string): The diff to apply to the file.diffIdentifier(string): A unique identifier for the diff operation.prompt(string): The prompt for the AI model to guide the diff application.applyModel(string): Optional model to use for applying the diff.
Returns
Promise<FsEditFileAndApplyDiffResponse>: A promise that resolves with the file edit response.
Example 1: Basic File Edit with Diff
// Edit a file with a simple diff
const result = await codebolt.utils.editFileAndApplyDiff(
'/src/utils/helpers.ts',
'Add new helper function',
'diff-001',
'Add a utility function to format dates'
);
console.log('File edit result:', result);
// Response structure:
// {
// success: true,
// filePath: '/src/utils/helpers.ts',
// changes: [ /* list of changes applied */ ]
// }
Example 2: Fix Bug with AI Assistance
// Use AI to fix a bug in a file
async function fixBug(filePath, bugDescription) {
const result = await codebolt.utils.editFileAndApplyDiff(
filePath,
`Fix: ${bugDescription}`,
`bugfix-${Date.now()}`,
`Fix this bug: ${bugDescription}. Ensure the fix doesn't break existing functionality.`
);
if (result.success) {
console.log('Bug fixed successfully');
} else {
console.error('Failed to fix bug:', result.error);
}
return result;
}
// Usage
await fixBug('/src/auth/login.ts', 'Null reference error when user is undefined');
Example 3: Refactor Code with AI
// Refactor code section with AI guidance
async function refactorCode(filePath, refactorDescription, specificPrompt) {
const result = await codebolt.utils.editFileAndApplyDiff(
filePath,
`Refactor: ${refactorDescription}`,
`refactor-${Date.now()}`,
specificPrompt
);
console.log('Refactoring completed:', result.success);
return result;
}
// Usage
await refactorCode(
'/src/api/user.ts',
'Improve error handling',
'Refactor the error handling to use async/await and add proper try-catch blocks. Ensure all error cases are handled gracefully.'
);
Example 4: Add New Feature
// Add a new feature to a file
async function addFeature(filePath, featureDescription, implementationDetails) {
const result = await codebolt.utils.editFileAndApplyDiff(
filePath,
`Feature: ${featureDescription}`,
`feature-${Date.now()}`,
implementationDetails
);
if (result.success) {
console.log('Feature added successfully');
console.log('Changes applied:', result.changes);
}
return result;
}
// Usage
await addFeature(
'/src/services/user.ts',
'Add user authentication',
'Add a new authenticate() method that accepts credentials and returns a JWT token. Include proper error handling and validation.'
);
Example 5: Update Dependencies
// Update imports and dependencies in a file
async function updateDependencies(filePath, oldImport, newImport) {
const result = await codebolt.utils.editFileAndApplyDiff(
filePath,
`Update import: ${oldImport} -> ${newImport}`,
`import-update-${Date.now()}`,
`Replace all imports of '${oldImport}' with '${newImport}' and update any related code to match the new API.`
);
console.log('Dependencies updated:', result.success);
return result;
}
// Usage
await updateDependencies(
'/src/components/Button.tsx',
'lodash',
'lodash-es'
);
Example 6: Batch File Updates
// Apply similar changes to multiple files
async function batchUpdateFiles(files, changeDescription, prompt) {
const results = [];
for (const file of files) {
console.log(`Processing ${file}...`);
const result = await codebolt.utils.editFileAndApplyDiff(
file,
changeDescription,
`batch-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
prompt
);
results.push({
file,
success: result.success,
error: result.error
});
// Small delay between files
await new Promise(resolve => setTimeout(resolve, 500));
}
const successful = results.filter(r => r.success).length;
console.log(`Batch update complete: ${successful}/${files.length} successful`);
return results;
}
// Usage
const results = await batchUpdateFiles(
[
'/src/components/Header.tsx',
'/src/components/Footer.tsx',
'/src/components/Sidebar.tsx'
],
'Update to use new theme API',
'Update component to use the new theme API. Replace old theme references with ThemeContext and useTheme hook.'
);
Example 7: Custom Model Selection
// Use a specific AI model for diff application
async function editWithModel(filePath, diff, prompt, model) {
const result = await codebolt.utils.editFileAndApplyDiff(
filePath,
diff,
`edit-${Date.now()}`,
prompt,
model // Use specified model (e.g., 'gpt-4', 'claude-3', etc.)
);
console.log(`Edit with ${model} completed:`, result.success);
return result;
}
// Usage
await editWithModel(
'/src/core/processor.ts',
'Optimize performance',
'Optimize this function for better performance. Use more efficient algorithms and data structures.',
'gpt-4' // Use GPT-4 for this edit
);
Example 8: Code Style Updates
async function updateCodeStyle(filePath, styleChanges) {
const result = await codebolt.utils.editFileAndApplyDiff(
filePath,
'Code style update',
`style-${Date.now()}`,
`Update the code to follow these style guidelines:\n${styleChanges}\n\nEnsure all code in the file conforms to these standards.`
);
return result;
}
// Usage
const styleGuide = `
- Use const instead of let when possible
- Prefer arrow functions
- Use template literals instead of string concatenation
- Add JSDoc comments to all functions
- Use meaningful variable names
`;
await updateCodeStyle('/src/utils/formatters.ts', styleGuide);
Explanation
The codebolt.utils.editFileAndApplyDiff(filePath, diff, diffIdentifier, prompt, applyModel) function edits a file and applies a diff with AI assistance. This is a powerful function for making intelligent code changes.
Key Points:
- AI-Powered: Uses AI to intelligently apply diffs
- Flexible: Works with any file type
- Customizable: Accepts custom prompts for guidance
- Model Selection: Optional parameter to specify AI model
- Safe: AI helps ensure correct diff application
Parameters:
- filePath (string): Path to the file to edit
- diff (string): Description of the diff/changes to apply
- diffIdentifier (string): Unique identifier for this diff operation
- prompt (string): Detailed prompt for the AI model
- applyModel (string, optional): Specific AI model to use
Return Value Structure:
{
success: boolean, // Whether the edit was successful
filePath: string, // Path to the edited file
changes: array, // List of changes applied
error?: string, // Error message if failed
originalContent?: string, // Optional original file content
newContent?: string, // Optional new file content
timestamp?: string // Optional timestamp of operation
}
Common Use Cases:
- Bug fixes with AI assistance
- Code refactoring
- Adding new features
- Updating dependencies
- Code style improvements
- Performance optimizations
- Documentation updates
Best Practices:
- Use specific, detailed prompts
- Provide clear diff descriptions
- Use unique diff identifiers
- Handle errors gracefully
- Review changes after application
- Test changes after editing
- Use version control for safety
Prompt Writing Tips:
- Be specific about what you want
- Provide context and constraints
- Mention any edge cases to handle
- Specify coding standards to follow
- Include error handling requirements
- Describe expected behavior clearly
Typical Workflow:
// 1. Define the change
const diff = 'Add error handling';
// 2. Create detailed prompt
const prompt = 'Add try-catch blocks around all async operations. Handle errors appropriately with logging and user feedback.';
// 3. Apply the diff
const result = await codebolt.utils.editFileAndApplyDiff(
'/src/api/client.ts',
diff,
`diff-${Date.now()}`,
prompt
);
// 4. Check result
if (result.success) {
console.log('Changes applied successfully');
} else {
console.error('Failed to apply changes:', result.error);
}
Diff Identifier Patterns:
// Timestamp-based
`diff-${Date.now()}`
// Category-based
`bugfix-${issueId}`,
`feature-${featureName}`,
`refactor-${componentName}`
// Unique ID
`edit-${uuidv4()}`,
`change-${Math.random().toString(36)}`
Advanced Patterns:
- Batch file updates
- Consistent refactoring across projects
- Migration assistance
- Code style enforcement
- Dependency updates
- Performance optimizations
Model Selection:
- Different models for different tasks
- Use advanced models for complex changes
- Use faster models for simple changes
- Consider model capabilities and cost
Related Functions:
- File system operations (if available)
- Code analysis tools
- Linting and formatting tools
Error Handling:
try {
const result = await codebolt.utils.editFileAndApplyDiff(
filePath,
diff,
diffId,
prompt
);
if (!result.success) {
console.error('Edit failed:', result.error);
// Handle failure
}
} catch (error) {
console.error('Exception during edit:', error);
// Handle exception
}
Notes:
- AI model availability may vary
- Response time depends on model and complexity
- Always review AI-suggested changes
- Use version control to track changes
- Test thoroughly after edits
- May require multiple attempts for complex changes
- Prompt quality affects result quality
- Consider file size and complexity
- Some changes may require manual review
- Diff identifier should be unique per operation