ai file edit

2025-12-11 0 176

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.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...\"
  }
} 

限制

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

测试

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

npm test 

执照

麻省理工学院

下载源码

通过命令行克隆项目:

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 建站资源 ai file edit https://www.zuozi.net/35085.html

workwithibmipdm
上一篇: workwithibmipdm
Super Stitch
下一篇: Super Stitch
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务