Skip to content
9 changes: 8 additions & 1 deletion libs/langchain/langchain/tools/github/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ def _run(
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
"""Use the GitHub API to run an operation."""
return self.api_wrapper.run(self.mode, instructions)
try:
return self.api_wrapper.run(self.mode, instructions)
except Exception as e:
error_msg = f'Error occurred during GitHub API operation: {e}'
if run_manager:
run_manager.error(error_msg)
print(error_msg)
raise e
55 changes: 49 additions & 6 deletions libs/langchain/langchain/utilities/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ def parse_issues(self, issues: List[Issue]) -> List[dict]:
return parsed

def get_issues(self) -> str:
try:
issues = self.github_repo_instance.get_issues(state="open")
if issues.totalCount > 0:
parsed_issues = self.parse_issues(issues)
parsed_issues_str = (
"Found " + str(len(parsed_issues)) + " issues:\n" + str(parsed_issues)
)
return parsed_issues_str
else:
return "No open issues available"
except Exception as e:
error_msg = f'Error occurred during issue retrieval: {e}'
print(error_msg)
return error_msg
"""
Fetches all open issues from the repo

Expand All @@ -118,8 +132,11 @@ def get_issue(self, issue_number: int) -> Dict[str, Any]:
Parameters:
issue_number(int): The number for the github issue
Returns:
dict: A doctionary containing the issue's title,
body, and comments as a string
dict: A dictionary containing the issue's title, body, and comments as a string
except Exception as e:
error_msg = f'Error occurred during issue retrieval: {e}'
print(error_msg)
return error_msg
"""
issue = self.github_repo_instance.get_issue(number=issue_number)
page = 0
Expand Down Expand Up @@ -164,6 +181,9 @@ def create_pull_request(self, pr_query: str) -> str:
)
return f"Successfully created PR number {pr.number}"
except Exception as e:
error_msg = f'Error occurred during pull request creation: {e}'
print(error_msg)
return error_msg
return "Unable to make pull request due to error:\n" + str(e)

def comment_on_issue(self, comment_query: str) -> str:
Expand All @@ -181,7 +201,12 @@ def comment_on_issue(self, comment_query: str) -> str:
comment = comment_query[len(str(issue_number)) + 2 :]
try:
issue = self.github_repo_instance.get_issue(number=issue_number)
if issue:
issue.create_comment(comment)
else:
error_msg = 'Failed to create comment: Issue not found'
print(error_msg)
return error_msg
return "Commented on issue " + str(issue_number)
except Exception as e:
return "Unable to make comment due to error:\n" + str(e)
Expand Down Expand Up @@ -212,7 +237,10 @@ def create_file(self, file_query: str) -> str:
else:
return f"File already exists at {file_path}. Use update_file instead"
except Exception as e:
return "Unable to make file due to error:\n" + str(e)
error_msg = f'Error occurred during file creation: {e}'
print(error_msg)
return error_msg
return error_msg

def read_file(self, file_path: str) -> str:
"""
Expand Down Expand Up @@ -244,7 +272,8 @@ def update_file(self, file_query: str) -> str:
A success or failure message
"""
try:
file_path = file_query.split("\n")[0]
try:
file_path = file_query.split("\n")[0]
old_file_contents = (
file_query.split("OLD <<<<")[1].split(">>>> OLD")[0].strip()
)
Expand All @@ -257,14 +286,26 @@ def update_file(self, file_query: str) -> str:
old_file_contents, new_file_contents
)

except Exception as e:
error_msg = f'Error occurred during file content comparison: {e}'
print(error_msg)
return error_msg

try:
if file_content == updated_file_content:
return (
"File content was not updated because old content was not found."
"It may be helpful to use the read_file action to get "
"the current file contents."
)

self.github_repo_instance.update_file(
except Exception as e:
error_msg = f'Error occurred during file update: {e}'
print(error_msg)
return error_msg

try:
self.github_repo_instance.update_file(
path=file_path,
message="Update " + file_path,
content=updated_file_content,
Expand Down Expand Up @@ -293,7 +334,9 @@ def delete_file(self, file_path: str) -> str:
)
return "Deleted file " + file_path
except Exception as e:
return "Unable to delete file due to error:\n" + str(e)
error_msg = f'Error occurred during file deletion: {e}'
print(error_msg)
return error_msg

def run(self, mode: str, query: str) -> str:
if mode == "get_issues":
Expand Down