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