githubfileeditortool.mdx
1 --- 2 title: "GitHubFileEditorTool" 3 id: githubfileeditortool 4 slug: "/githubfileeditortool" 5 description: "A Tool that allows Agents and ToolInvokers to edit files in GitHub repositories." 6 --- 7 8 # GitHubFileEditorTool 9 10 A Tool that allows Agents and ToolInvokers to edit files in GitHub repositories. 11 12 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Mandatory init variables** | `github_token`: GitHub personal access token. Can be set with `GITHUB_TOKEN` env var. | 17 | **API reference** | [Tools](/reference/tools-api) | 18 | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/github | 19 20 </div> 21 22 ## Overview 23 24 `GitHubFileEditorTool` wraps the [`GitHubFileEditor`](../../pipeline-components/connectors/githubfileeditor.mdx) component, providing a tool interface for use in agent workflows and tool-based pipelines. 25 26 The tool supports multiple file operations including editing existing files, creating new files, deleting files, and undoing recent changes. It supports four main commands: 27 28 - **EDIT**: Edit an existing file by replacing specific content 29 - **CREATE**: Create a new file with specified content 30 - **DELETE**: Delete an existing file 31 - **UNDO**: Revert the last commit if made by the same user 32 33 ### Parameters 34 35 - `name` is _optional_ and defaults to "file_editor". Specifies the name of the tool. 36 - `description` is _optional_ and provides context to the LLM about what the tool does. 37 - `github_token` is _mandatory_ and must be a GitHub personal access token for API authentication. The default setting uses the environment variable `GITHUB_TOKEN`. 38 - `repo` is _optional_ and sets a default repository in owner/repo format. 39 - `branch` is _optional_ and defaults to "main". Sets the default branch to work with. 40 - `raise_on_failure` is _optional_ and defaults to `True`. If False, errors are returned instead of raising exceptions. 41 42 ## Usage 43 44 Install the GitHub integration to use the `GitHubFileEditorTool`: 45 46 ```shell 47 pip install github-haystack 48 ``` 49 50 :::info[Repository Placeholder] 51 52 To run the following code snippets, you need to replace the `owner/repo` with your own GitHub repository name. 53 ::: 54 55 ### On its own 56 57 Basic usage to edit a file: 58 59 ```python 60 from haystack_integrations.tools.github import GitHubFileEditorTool 61 62 tool = GitHubFileEditorTool() 63 result = tool.invoke( 64 command="edit", 65 payload={ 66 "path": "src/example.py", 67 "original": "def old_function():", 68 "replacement": "def new_function():", 69 "message": "Renamed function for clarity", 70 }, 71 repo="owner/repo", 72 branch="main", 73 ) 74 75 print(result) 76 ``` 77 78 ```bash 79 {'result': 'Edit successful'} 80 ``` 81 82 ### With an Agent 83 84 You can use `GitHubFileEditorTool` with the [Agent](../../pipeline-components/agents-1/agent.mdx) component. The Agent will automatically invoke the tool when needed to edit files in GitHub repositories. 85 86 ```python 87 from haystack.components.generators.chat import OpenAIChatGenerator 88 from haystack.dataclasses import ChatMessage 89 from haystack.components.agents import Agent 90 from haystack_integrations.tools.github import GitHubFileEditorTool 91 92 editor_tool = GitHubFileEditorTool(repo="owner/repo") 93 94 agent = Agent( 95 chat_generator=OpenAIChatGenerator(), 96 tools=[editor_tool], 97 exit_conditions=["text"], 98 ) 99 100 response = agent.run( 101 messages=[ 102 ChatMessage.from_user( 103 "Edit the file README.md in the repository \"owner/repo\" and replace the original string 'tpyo' with the replacement 'typo'. This is all context you need.", 104 ), 105 ], 106 ) 107 108 print(response["last_message"].text) 109 ``` 110 111 ```bash 112 The file `README.md` has been successfully edited to correct the spelling of 'tpyo' to 'typo'. 113 ```