Skip to content

Commit

Permalink
fix: skip cr task if there is no error (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwanying authored Dec 30, 2024
1 parent 16c07d0 commit c5669c7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
17 changes: 12 additions & 5 deletions server/agent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ async def dict_to_sse(generator: AsyncGenerator[Dict, None]):
json_output = json.dumps(d, ensure_ascii=False)
yield f"data: {json_output}\n\n"
except Exception as e:
error_output = json.dumps({"status": "error", "message": str(e)}, ensure_ascii=False)
error_output = json.dumps(
{"status": "error", "message": str(e)}, ensure_ascii=False
)
yield f"data: {error_output}\n\n"


class AgentBuilder:
agent_executor: AgentExecutor

Expand Down Expand Up @@ -216,13 +219,17 @@ async def run_stream_chat(self, input_data: ChatData) -> AsyncIterator[Dict]:
async def run_chat(self, input_data: ChatData) -> str:
try:
messages = input_data.messages
return self.agent_executor.invoke(
last_message_content = messages[-1].content

result = self.agent_executor.invoke(
{
"input": messages[len(messages) - 1].content,
"input": last_message_content,
"chat_history": self.chat_history_transform(messages),
},
return_only_outputs=True,
)

return result
except Exception as e:
logger.error(e)
return f"error: {str(e)}\n"
logger.error("Error occurred in run_chat: %s", str(e))
return f"error: {str(e)}"
22 changes: 13 additions & 9 deletions server/agent/prompts/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@
For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me.
</details>
## Task 2: Code Review
If the title or description includes the flag [skip], you can skip the task.
Review the diff for significant errors in the updated files. Focus exclusively on logical, functional issues, or security vulnerabilities. Avoid comments on stylistic changes, minor refactors, or insignificant issues.
Review the code diff exclusively for critical logical, functional, or security errors. Avoid any commentary unrelated to these areas, including documentation, stylistic changes, or minor issues.
### Specific Instructions:
### Specific instructions:
- Only the code diff is available for you to review, not the entire codebase.
- Make comments only on code introducing clear and critical functional or security errors.
- Do not comment on documentation, style, accuracy of text, or minor refactoring changes.
- Provide absolutely no feedback if no critical errors are found.
- If necessary, provide code examples only for addressing critical errors.
- Adhere to language-specific coding conventions used in the PR.
- If there are critical errors to comment on, use the `create_review_comment` tool to create review comments.
- Skip the task if no errors are found.
- Upon completing the task, output strictly "All task finished", with no additional commentary.
- Take into account that you don’t have access to the full code but only the code diff.
- Only comment on code that introduces potential functional or security errors.
- If no critical issues are found in the changes, do not provide any comments.
- Provide code examples if necessary for critical fixes.
- Follow the coding conventions of the language in the PR.
- After completing the tasks, only output "All task finished".
### Input format
Expand Down
37 changes: 24 additions & 13 deletions server/agent/tools/pull_request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import traceback
from typing import Optional
from github import Github, Auth, ContentFile
Expand All @@ -7,6 +6,7 @@
from langchain.tools import tool
from agent.tools.helper import need_github_login


def factory(token: Optional[Auth.Token]):
@tool
def get_file_content(repo_name: str, path: str, ref: Optional[str] = None):
Expand All @@ -22,19 +22,28 @@ def get_file_content(repo_name: str, path: str, ref: Optional[str] = None):
g = Github(auth=token)
try:
repo = g.get_repo(repo_name)
contents: list[ContentFile.ContentFile] | ContentFile.ContentFile = repo.get_contents(path=path, ref=ref) if ref else repo.get_contents(path=path)
contents: list[ContentFile.ContentFile] | ContentFile.ContentFile = (
repo.get_contents(path=path, ref=ref)
if ref
else repo.get_contents(path=path)
)

if isinstance(contents, list):
return json.dumps(
[{
"filename": content.path,
"content": content.content,
} for content in contents]
[
{
"filename": content.path,
"content": content.content,
}
for content in contents
]
)
return json.dumps({
"filename": contents.path,
"content": contents.content,
})
return json.dumps(
{
"filename": contents.path,
"content": contents.content,
}
)
except Exception as e:
print(traceback.format_exception(e))
return json.dumps([])
Expand All @@ -53,11 +62,13 @@ def create_pr_summary(repo_name: str, pull_number: int, summary: str):
g = Github(auth=token)
repo = g.get_repo(repo_name)
pull_request = repo.get_pull(pull_number)
# print(f"create_pr_summary, pull_request={pull_request}, summary={summary}")
pull_request.create_issue_comment(summary)
return json.dumps([])

@tool
def create_review_comment(repo_name: str, pull_number: int, sha: str, path: str, line: int, comment: str):
def create_review_comment(
repo_name: str, pull_number: int, sha: str, path: str, line: int, comment: str
):
"""
Create a code review of specified pull requst file
:param repo_name: The name of the repository, e.g., "ant-design/ant-design"
Expand Down Expand Up @@ -91,4 +102,4 @@ def create_review_comment(repo_name: str, pull_number: int, sha: str, path: str,
"get_file_content": get_file_content,
"create_pr_summary": create_pr_summary,
"create_review_comment": create_review_comment,
}
}

0 comments on commit c5669c7

Please sign in to comment.