
Security News
Opengrep Adds Apex Support and New Rule Controls in Latest Updates
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
ai-file-edit
Advanced tools
[](https://www.npmjs.com/package/ai-file-edit)
A library for editing files using AI models such as GPT, Claude, and Gemini.
Developed by the 16x Prompt team.
Related projects:
File Operations
AI Integration
Version Control & Safety
Security
import {SUPPORTED_MODELS} from 'ai-file-edit';
import {ModelEnum, AI_PROVIDERS} from 'llm-info';
// Print all supported models
console.log(SUPPORTED_MODELS);
// Example output:
[
{
model: ModelEnum['gpt-4.1'],
provider: AI_PROVIDERS.OPENAI,
recommended: true,
supportMultipleEditsPerMessage: true,
},
{
model: ModelEnum['claude-3-7-sonnet-20250219'],
provider: AI_PROVIDERS.ANTHROPIC,
recommended: false,
supportMultipleEditsPerMessage: false,
},
{
model: ModelEnum['gemini-2.5-pro-preview-05-06'],
provider: AI_PROVIDERS.GOOGLE,
recommended: false,
supportMultipleEditsPerMessage: true,
},
{
model: ModelEnum['gemini-2.5-pro-exp-03-25'],
provider: AI_PROVIDERS.GOOGLE,
recommended: false,
supportMultipleEditsPerMessage: true,
},
];
Note: The recommended model is gpt-4.1
as it provides the best performance for file editing tasks.
npm install ai-file-edit
import {FileEditTool} from 'ai-file-edit';
import {ModelEnum, AI_PROVIDERS} from 'llm-info';
// Initialize the tool with Claude
const claudeFileEditTool = new FileEditTool(
'/path/to/parent/directory', // Parent directory for relative paths
['/path/to/allowed/directory'], // Allowed directories for file operations
{
provider: AI_PROVIDERS.ANTHROPIC,
model: ModelEnum['claude-3-7-sonnet-20250219'],
apiKey: process.env.ANTHROPIC_API_KEY,
},
['/path/to/file1.js', '/path/to/file2.js'], // Optional: Files to include in context
5, // Optional: Maximum number of tool use rounds (default: 5)
);
// Initialize the tool with GPT
const gptFileEditTool = new FileEditTool(
'/path/to/parent/directory', // Parent directory for relative paths
['/path/to/allowed/directory'], // Allowed directories for file operations
{
provider: AI_PROVIDERS.OPENAI,
model: ModelEnum['gpt-4.1'],
apiKey: process.env.OPENAI_API_KEY,
},
['/path/to/file1.js', '/path/to/file2.js'], // Optional: Files to include in context
5, // Optional: Maximum number of tool use rounds (default: 5)
);
// Initialize the tool with Google AI
const googleFileEditTool = new FileEditTool(
'/path/to/parent/directory', // Parent directory for relative paths
['/path/to/allowed/directory'], // Allowed directories for file operations
{
provider: AI_PROVIDERS.GOOGLE,
model: ModelEnum['gemini-2.5-pro-preview-05-06'],
apiKey: process.env.GOOGLE_API_KEY,
},
['/path/to/file1.js', '/path/to/file2.js'], // Optional: Files to include in context
5, // Optional: Maximum number of tool use rounds (default: 5)
);
import {FileEditTool} from 'ai-file-edit';
import {ModelEnum, AI_PROVIDERS} from 'llm-info';
const fileEditTool = new FileEditTool(
'/path/to/parent/directory', // Parent directory for relative paths
['/path/to/allowed/directory'], // Allowed directories for file operations
{
provider: AI_PROVIDERS.ANTHROPIC,
model: ModelEnum['claude-3-7-sonnet-20250219'],
apiKey: 'your-api-key',
},
['/path/to/file/to/edit'], // Optional: Files to include in context
);
// Process query with debug mode enabled
const response = await fileEditTool.processQuery('Update the file to add a new function', true);
console.log(response.finalText);
console.log(response.toolResults);
console.log(response.rawDiff);
console.log(response.reverseDiff);
console.log(response.toolCallRounds);
The tool generates both forward and reverse diffs for all file changes. The forward diff shows what was changed, while the reverse diff can be used to revert the changes.
The forward diff is returned in the rawDiff
field of the response. It shows the changes made to the file in a git-style diff format:
--- file.js original
+++ file.js modified
@@ -1,2 +1,2 @@
-function add(a, b) { return a + b; }
-console.log(add(1, 2));
+function multiply(a, b) { return a * b; }
+console.log(multiply(1, 2));
The reverse diff is returned in the reverseDiff
field of the response. It shows how to revert the changes in a git-style diff format:
--- file.js modified
+++ file.js original
@@ -1,2 +1,2 @@
-function multiply(a, b) { return a * b; }
-console.log(multiply(1, 2));
+function add(a, b) { return a + b; }
+console.log(add(1, 2));
You can use the reverse diff to revert changes using the applyReversePatch
function:
import {applyReversePatch} from 'ai-file-edit';
// Apply the reverse patch to revert changes
const result = await applyReversePatch(filePath, reverseDiff);
if (result.success) {
console.log('Changes reverted successfully');
} else {
console.error('Failed to revert changes:', result.error);
}
The applyReversePatch
function returns a promise that resolves to an object with:
success
: boolean indicating whether the operation was successfulerror
: optional string containing error message if the operation failedYou can provide a list of files to include in the context of the query. This is useful when you want to reference multiple files in your query:
const fileEditTool = new FileEditTool(
'/path/to/parent/directory',
['/path/to/allowed/directory'],
ModelEnum['claude-3-7-sonnet-20250219'],
AI_PROVIDERS.ANTHROPIC,
'your-api-key',
['/path/to/file1.js', '/path/to/file2.js'],
5, // Maximum number of tool use rounds (default: 5)
);
The tool supports multiple rounds of tool use, allowing the model to make multiple changes in response to a single query. The maximum number of rounds is configurable through the maxToolUseRounds
parameter in the constructor.
The processQuery
method supports an optional debug mode that provides detailed logging of the tool's operation:
const response = await fileEditTool.processQuery('Update the file to add a new function', true);
When debug mode is enabled, the tool will log:
This is useful for debugging and understanding how the tool processes queries and makes changes.
The processQuery
method returns an object with the following structure:
{
finalText: string[]; // Array of text responses from the AI
toolResults: string[]; // Array of results from tool operations
finalStatus: 'success' | 'failure' | 'retry_limit_reached' | 'no_tool_calls';
toolCallCount: number; // Number of tool calls made
toolCallRounds: number; // Number of tool call rounds used
rawDiff?: Record<string, string>; // Forward diffs for each file, keyed by file path
reverseDiff?: Record<string, string>; // Reverse diffs for each file, keyed by file path
}
Example response with diffs:
{
finalText: ["Successfully updated files"],
toolResults: ["File updated successfully"],
finalStatus: "success",
toolCallCount: 1,
toolCallRounds: 1,
rawDiff: {
"/path/to/file1.js": "Index: /path/to/file1.js\n...",
"/path/to/file2.js": "Index: /path/to/file2.js\n..."
},
reverseDiff: {
"/path/to/file1.js": "Index: /path/to/file1.js\n...",
"/path/to/file2.js": "Index: /path/to/file2.js\n..."
}
}
The library includes test cases for both Claude and GPT models. To run the tests:
npm test
MIT
FAQs
[](https://www.npmjs.com/package/ai-file-edit)
We found that ai-file-edit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.