diff --git a/server/agent/prompts/issue_helper.py b/server/agent/prompts/issue_helper.py index c7ef95c5..6f855d30 100644 --- a/server/agent/prompts/issue_helper.py +++ b/server/agent/prompts/issue_helper.py @@ -6,6 +6,8 @@ - Inform users if their request is a new feature and ask them to wait. - Respect the language of the issue's title and content. Ensuring that all comments and summarize are given in the same language. e.g., English or Chinese. - Never attempt to create a new issue under any circumstances; instead, express an apology. +- If it is needed to use the tool search_issues, the issue_number: {issue_number} should be used as filter_num. +- If you don’t have any useful conclusions, use your own knowledge to assist the user as much as possible, but do not fabricate facts. - At the end of the conversation, be sure to include the following wording and adhere to the language used in previous conversations: For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me. @@ -19,11 +21,16 @@ """ ISSUE_COMMENT_PROMPT = """ +- If it is needed to use the tool search_issues, the issue_number: {issue_number} should be used as filter_num. +- If you don’t have any useful conclusions, use your own knowledge to assist the user as much as possible, but do not fabricate facts. - Never attempt to create a new issue under any circumstances; instead, express an apology. +- If the found issue_number is the same as this issue_number: {issue_number}, it means no similar issues were found, You don’t need to mention the issue again. +- If you don’t have any useful conclusions, use your own knowledge to assist the user as much as possible, but do not fabricate facts. - At the end of the conversation, be sure to include the following wording and adhere to the language used in previous conversations: For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me. issue_content: {issue_content} +issue_number: {issue_number} ``` """ @@ -39,7 +46,7 @@ def generate_issue_prompt( ) -def generate_issue_comment_prompt(repo_name: str, issue_url: str, issue_content: str): +def generate_issue_comment_prompt(issue_number: str, issue_content: str): return ISSUE_COMMENT_PROMPT.format( - repo_name=repo_name, issue_url=issue_url, issue_content=issue_content + issue_number=issue_number, issue_content=issue_content ) diff --git a/server/agent/tools/issue.py b/server/agent/tools/issue.py index ad0d4e59..46473e52 100644 --- a/server/agent/tools/issue.py +++ b/server/agent/tools/issue.py @@ -8,6 +8,7 @@ DEFAULT_REPO_NAME = "ant-design/ant-design" + def factory(token: Optional[Auth.Token]): @tool def create_issue(repo_name: str, title: str, body: str): @@ -29,93 +30,100 @@ def create_issue(repo_name: str, title: str, body: str): # Create an issue issue = repo.create_issue(title=title, body=body) - return json.dumps({ - "url": issue.html_url, - "title": issue.title, - }) + return json.dumps( + { + "url": issue.html_url, + "title": issue.title, + } + ) except Exception as e: print(f"An error occurred: {e}") return json.dumps([]) @tool def get_issues( - repo_name: Optional[str] = DEFAULT_REPO_NAME, - max_num: Optional[int] = 5, - state: Optional[str] = "all", - sort: Optional[str] = "created", - order: Optional[str] = "desc" - ): - """ - Fetches issues from the configured repository + repo_name: Optional[str] = DEFAULT_REPO_NAME, + max_num: Optional[int] = 5, + state: Optional[str] = "all", + sort: Optional[str] = "created", + order: Optional[str] = "desc", + filter_num: Optional[str] = "", + ): + """ + Fetches issues from the configured repository - :param repo_name: The name of the repository, e.g., "ant-design/ant-design" - :param max_num: The maximum number of issues to fetch - :param state: The state of the issue, e.g: open, closed, all - :param sort: The sorting method, e.g: created, updated, comments - :param order: The order of the sorting, e.g: asc, desc - """ - g = Github() - try: - # Obtain the repository object - repo = g.get_repo(repo_name) + :param repo_name: The name of the repository, e.g., "ant-design/ant-design" + :param max_num: The maximum number of issues to fetch + :param state: The state of the issue, e.g: open, closed, all + :param sort: The sorting method, e.g: created, updated, comments + :param order: The order of the sorting, e.g: asc, desc + :filter_num: The number of the issue to filtered out. If it's empty, no filtering will be performed + """ + g = Github() + try: + # Obtain the repository object + repo = g.get_repo(repo_name) - # Retrieve a list of issues from the repository - issues = repo.get_issues(state=state, sort=sort, direction=order)[:max_num] + # Retrieve a list of issues from the repository + issues = repo.get_issues(state=state, sort=sort, direction=order)[:max_num] - issues_list = [ - { - "issue_name": f"Issue #{issue.number} - {issue.title}", - "issue_url": issue.html_url - } - for issue in issues - ] - return json.dumps(issues_list) - except Exception as e: - print(f"An error occurred: {e}") - return json.dumps([]) + issues_list = [ + { + "issue_name": f"Issue #{issue.number} - {issue.title}", + "issue_url": issue.html_url, + } + for issue in issues + if issue.number != filter_num + ] + return json.dumps(issues_list) + except Exception as e: + print(f"An error occurred: {e}") + return json.dumps([]) @tool def search_issues( - keyword: str = None, - repo_name: Optional[str] = DEFAULT_REPO_NAME, - max_num: Optional[int] = 5, - sort: Optional[str] = "created", - order: Optional[str] ="asc" - ): - """ - Search Issues Or PR from repository by keyword + keyword: str = None, + repo_name: Optional[str] = DEFAULT_REPO_NAME, + max_num: Optional[int] = 5, + sort: Optional[str] = "created", + order: Optional[str] = "asc", + ): + """ + Search Issues Or PR from repository by keyword - :param repo_name: The name of the repository, e.g., "ant-design/ant-design" - :param keyword: The keyword to search for in the issues / pr - :param max_num: The maximum number of issues / pr to fetch - :param sort: The sorting method, e.g: created, updated, comments - :param order: The order of the sorting, e.g: asc, desc - :param state: The state of the issue, e.g: open, closed, all - """ - if token is None: - g = Github() - else: - g = Github(auth=token) + :param repo_name: The name of the repository, e.g., "ant-design/ant-design" + :param keyword: The keyword to search for in the issues / pr + :param max_num: The maximum number of issues / pr to fetch + :param sort: The sorting method, e.g: created, updated, comments + :param order: The order of the sorting, e.g: asc, desc + :param state: The state of the issue, e.g: open, closed, all + """ + if token is None: + g = Github() + else: + g = Github(auth=token) - try: - search_query = f"{keyword} in:title,body,comments repo:{repo_name}" - # Retrieve a list of open issues from the repository - issues: PaginatedList[Issue.Issue] = g.search_issues(query=search_query, sort=sort, order=order)[:max_num] + try: + search_query = f"{keyword} in:title,body,comments repo:{repo_name}" + # Retrieve a list of open issues from the repository + issues: PaginatedList[Issue.Issue] = g.search_issues( + query=search_query, sort=sort, order=order + )[:max_num] - issues_list = [ - { - "issue_name": f"{'PR' if issue.pull_request else 'Issue'} #{issue.number} - {issue.title}", - "issue_url": issue.html_url - } - for issue in issues - ] - return json.dumps(issues_list) - except Exception as e: - print(f"An error occurred: {e}") - return json.dumps([]) + issues_list = [ + { + "issue_name": f"{'PR' if issue.pull_request else 'Issue'} #{issue.number} - {issue.title}", + "issue_url": issue.html_url, + } + for issue in issues + ] + return json.dumps(issues_list) + except Exception as e: + print(f"An error occurred: {e}") + return json.dumps([]) return { "create_issue": create_issue, "get_issues": get_issues, "search_issues": search_issues, - } \ No newline at end of file + } diff --git a/server/event_handler/discussion.py b/server/event_handler/discussion.py index a5375cbb..037c9e9c 100644 --- a/server/event_handler/discussion.py +++ b/server/event_handler/discussion.py @@ -243,8 +243,7 @@ async def execute(self): bot = get_bot_by_id(repo_config.robot_id) prompt = generate_issue_comment_prompt( - repo_name=repo_name, - issue_url=discussion["html_url"], + issue_number=discussion["number"], issue_content=discussion_content, ) diff --git a/server/event_handler/issue.py b/server/event_handler/issue.py index 5fa72ab8..19242d8e 100644 --- a/server/event_handler/issue.py +++ b/server/event_handler/issue.py @@ -104,8 +104,7 @@ async def execute(self): issue_content = f"{issue.title}: {issue.body}" prompt = generate_issue_comment_prompt( - repo_name=repo.full_name, - issue_url=issue.url, + issue_number=issue.number, issue_content=issue_content, )