GitHubFileEditorTool
A Tool that allows Agents and ToolInvokers to edit files in GitHub repositories.
Mandatory init variables | "github_token": GitHub personal access token. Can be set with GITHUB_TOKEN env var. |
API reference | Tools |
GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/github |
Overview
GitHubFileEditorTool
wraps the GitHubFileEditor
component, providing a tool interface for use in agent workflows and tool-based pipelines.
The tool supports multiple file operations including editing existing files, creating new files, deleting files, and undoing recent changes. It supports four main commands:
- EDIT: Edit an existing file by replacing specific content
- CREATE: Create a new file with specified content
- DELETE: Delete an existing file
- UNDO: Revert the last commit if made by the same user
Parameters
name
is optional and defaults to "file_editor". Specifies the name of the tool.description
is optional and provides context to the LLM about what the tool does.github_token
is mandatory and must be a GitHub personal access token for API authentication. The default setting uses the environment variableGITHUB_TOKEN
.repo
is optional and sets a default repository in owner/repo format.branch
is optional and defaults to "main". Sets the default branch to work with.raise_on_failure
is optional and defaults toTrue
. If False, errors are returned instead of raising exceptions.
Usage
Install the GitHub integration to use the GitHubFileEditorTool
:
pip install github-haystack
Repository Placeholder
To run the following code snippets, you need to replace the
owner/repo
with your own GitHub repository name.
On its own
Basic usage to edit a file:
from haystack_integrations.tools.github import GitHubFileEditorTool
tool = GitHubFileEditorTool()
result = tool.invoke(
command="edit",
payload={
"path": "src/example.py",
"original": "def old_function():",
"replacement": "def new_function():",
"message": "Renamed function for clarity"
},
repo="owner/repo",
branch="main"
)
print(result)
{'result': 'Edit successful'}
With an Agent
You can use GitHubFileEditorTool
with the Agent component. The Agent will automatically invoke the tool when needed to edit files in GitHub repositories.
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.components.agents import Agent
from haystack_integrations.tools.github import GitHubFileEditorTool
editor_tool = GitHubFileEditorTool(repo="owner/repo")
agent = Agent(
chat_generator=OpenAIChatGenerator(),
tools=[editor_tool],
exit_conditions=["text"]
)
agent.warm_up()
response = agent.run(messages=[
ChatMessage.from_user("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.")
])
print(response["last_message"].text)
The file `README.md` has been successfully edited to correct the spelling of 'tpyo' to 'typo'.
Updated 1 day ago