Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable GitAuto to extract file content partially by line number or a keyword to minimize the input so that GitAuto can not be confused #272

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions services/github/github_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,23 @@ def get_owner_name(owner_id: int, token: str) -> str | None:


@handle_exceptions(default_return_value="", raise_on_error=False)
def get_remote_file_content(file_path: str, base_args: BaseArgs) -> str:
def get_remote_file_content(
file_path: str,
base_args: BaseArgs,
line_number: Optional[int] = None,
keyword: Optional[str] = None,
) -> str:
"""
https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28
params:
- file_path: file path or directory path. Ex) 'src/main.py' or 'src'
- line_number: specific line number to focus on
- keyword: keyword to search in the file content
"""
if line_number is not None and keyword is not None:
return "Error: You can only specify either line_number or keyword, not both."

owner, repo, ref, token = (
base_args["owner"],
base_args["repo"],
Expand Down Expand Up @@ -586,11 +596,37 @@ def get_remote_file_content(file_path: str, base_args: BaseArgs) -> str:

# Otherwise, decode the content
decoded_content: str = base64.b64decode(s=encoded_content).decode(encoding=UTF8)
numbered_content: str = "\n".join(
f"{i + 1}: {line}" for i, line in enumerate(decoded_content.split("\n"))
)
lines = decoded_content.split("\n")
numbered_lines = [f"{i + 1}: {line}" for i, line in enumerate(lines)]
file_path_with_lines = file_path

# If line_number is specified, show the lines around the line_number
if line_number is not None:
start = max(line_number - 6, 0)
end = min(line_number + 5, len(lines))
numbered_lines = numbered_lines[start:end]
file_path_with_lines = f"{file_path}#L{start + 1}-L{end}"

# If keyword is specified, show the lines containing the keyword
elif keyword is not None:
segments = []
for i, line in enumerate(lines):
if keyword not in line:
continue
start = max(i - 5, 0)
end = min(i + 6, len(lines))
segment = "\n".join(numbered_lines[start:end])
file_path_with_lines = f"{file_path}#L{start + 1}-L{end}"
segments.append(f"```{file_path_with_lines}\n" + segment + "\n```")

if not segments:
return f"Keyword '{keyword}' not found in the file '{file_path}'."
msg = f"Opened file: '{file_path}' and found multiple occurrences of '{keyword}'.\n\n"
return msg + "\n\n•••\n\n".join(segments)

numbered_content: str = "\n".join(numbered_lines)
msg = f"Opened file: '{file_path}' with line numbers for your information.\n\n"
return msg + f"```{file_path}\n{numbered_content}\n```"
return msg + f"```{file_path_with_lines}\n{numbered_content}\n```"


@handle_exceptions(default_return_value="", raise_on_error=False)
Expand Down
18 changes: 15 additions & 3 deletions services/openai/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
"type": "string",
"description": "The full path to the file within the repository. For example, 'src/openai/__init__.py'. NEVER EVER be the same as the file_path in previous function calls.",
}
KEYWORD: dict[str, str] = {
"type": "string",
"description": "The keyword to search for in a file. For example, 'variable_name'. Exact matches only.",
}
LINE_NUMBER: dict[str, int] = {
"type": "integer",
"description": "If you already know the line number of interest when opening a file, use this. The 5 lines before and after this line number will be retrieved. For example, use it when checking the surrounding lines of a specific line number if the diff is incorrect.",
}

COMMIT_CHANGES_TO_REMOTE_BRANCH: shared_params.FunctionDefinition = {
"name": "commit_changes_to_remote_branch",
Expand All @@ -43,11 +51,15 @@
""",
"parameters": {
"type": "object",
"properties": {"file_path": FILE_PATH},
"properties": {
"file_path": FILE_PATH,
"line_number": LINE_NUMBER,
"keyword": KEYWORD,
},
"required": ["file_path"],
"additionalProperties": False, # For Structured Outpus
# "additionalProperties": False, # For Structured Outpus
},
"strict": True, # For Structured Outpus
# "strict": True, # For Structured Outpus
}

QUERY: dict[str, str] = {
Expand Down