ai file edit

其他资源 2025-08-01

ai file edit

使用AI模型(例如GPT,Claude和Gemini)编辑文件的库。

由16倍提示团队开发。

相关项目:

  • LLM-INFO:有关LLM型号的信息,上下文窗口令牌限制,输出令牌限制,定价等。
  • Send-Prompt:与标准化接口和功能调用的AI模型交互的统一打字稿库。

特征

文件操作

  • 使用自然语言编辑文件
  • 创建新文件
  • 覆盖现有文件
  • 对文件进行选择性编辑
  • 支持单个操作中多个文件编辑

AI集成

  • 支持OpenAI,Anthropic和Google AI模型
  • 支持多个工具使用弹
  • 调试模式用于详细记录

版本控制与安全

  • 为所有更改生成差异
  • 生成反向差异以恢复更改
  • 使用反向差异恢复变化的能力

安全

  • 使用允许目录的安全文件访问
  • API密钥安全性
  • 文件路径验证
  • 安全的链接处理

支持的模型

 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 ,
  } ,
] ;

注意:推荐模型是GPT-4.1,因为它为文件编辑任务提供了最佳性能。

安装

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 ) ;

差异和恢复变化

该工具为所有文件更改生成向前和反向差异。正向差异显示了变化的内容,而反向差异可用于恢复更改。

向前差异

向前差异在响应的RAWDIFF字段中返回。它以git式差异格式显示了对文件所做的更改:

 --- 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)); 

反向差异

反向差异在响应的反向段字段中返回。它显示了如何以git风格的差异格式恢复变化:

 --- 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)); 

恢复变化

您可以使用反向差异来使用ApplyReversePatch函数恢复更改:

 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 ) ;
}

applyReversePatch函数返回的承诺可以通过以下方式解决对象。

  • 成功:布尔值指示操作是否成功
  • 错误:如果操作失败,则包含错误消息的可选字符串

档案上下文和工具使用回合

您可以提供在查询上下文中包含的文件列表。当您想在查询中引用多个文件时,这很有用:

 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)
) ;

该工具支持多轮工具使用,从而允许模型对单个查询进行多次更改。最大弹奏数可通过构造函数中的Maxtooluserounds参数进行配置。

调试模式

ProcessQuery方法支持可选的调试模式,该模式提供了该工具操作的详细日志记录:

 const response = await fileEditTool . processQuery ( 'Update the file to add a new function' , true ) ;

当启用调试模式时,该工具将记录:

  • 初始查询
  • 每个工具呼叫回合
  • 消息历史记录
  • 工具电话处理详细信息
  • 响应细节

这对于调试和了解工具如何处理查询和更改很有用。

响应结构

该过程方法返回具有以下结构的对象:

 {
  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
}

示例响应差异:

 {
  finalText : [ "Successfully updated files" ] ,
  toolResults : [ "File updated successfully" ] ,
  finalStatus : "success" ,
  toolCallCount : 1 ,
  toolCallRounds : 1 ,
  rawDiff : {
    "/path/to/file1.js" : "Index: /path/to/file1.jsn..." ,
    "/path/to/file2.js" : "Index: /path/to/file2.jsn..."
  } ,
  reverseDiff : {
    "/path/to/file1.js" : "Index: /path/to/file1.jsn..." ,
    "/path/to/file2.js" : "Index: /path/to/file2.jsn..."
  }
} 

限制

  • 无法删除文件
  • 不能一次编辑太多文件(> 3个文件)

测试

该库包括Claude和GPT模型的测试用例。进行测试:

npm test 

执照

麻省理工学院

下载源码

通过命令行克隆项目:

git clone https://github.com/paradite/ai-file-edit.git