o
    ȳg<                     @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z& G d	d
 d
e	Z'G dd de	Z(G dd de	Z)G dd de	Z*G dd de	Z+G dd de	Z,G dd de	Z-G dd de	Z.G dd de	Z/G dd de	Z0G dd de	Z1G dd  d e	Z2G d!d" d"e	Z3G d#d$ d$e	Z4G d%d& d&e	Z5G d'd( d(eZ6d)S )*zGitHub Toolkit.    )DictList)BaseTool)BaseToolkit)	BaseModelField)COMMENT_ON_ISSUE_PROMPTCREATE_BRANCH_PROMPTCREATE_FILE_PROMPTCREATE_PULL_REQUEST_PROMPTCREATE_REVIEW_REQUEST_PROMPTDELETE_FILE_PROMPTGET_FILES_FROM_DIRECTORY_PROMPTGET_ISSUE_PROMPTGET_ISSUES_PROMPTGET_LATEST_RELEASE_PROMPTGET_PR_PROMPTGET_RELEASE_PROMPTGET_RELEASES_PROMPTLIST_BRANCHES_IN_REPO_PROMPTLIST_PRS_PROMPTLIST_PULL_REQUEST_FILES"OVERVIEW_EXISTING_FILES_BOT_BRANCHOVERVIEW_EXISTING_FILES_IN_MAINREAD_FILE_PROMPTSEARCH_CODE_PROMPTSEARCH_ISSUES_AND_PRS_PROMPTSET_ACTIVE_BRANCH_PROMPTUPDATE_FILE_PROMPT)GitHubAction)GitHubAPIWrapperc                   @   &   e Zd ZU dZedddZeed< dS )NoInputz4Schema for operations that do not require any input. z*No input required, e.g. `` (empty string).descriptionno_inputN)__name__
__module____qualname____doc__r   r&   str__annotations__ r-   r-   m/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/agent_toolkits/github/toolkit.pyr"   &      
 r"   c                   @   r!   )GetIssuez<Schema for operations that require an issue number as input.r   z%Issue number as an integer, e.g. `42`r$   issue_numberN)r'   r(   r)   r*   r   r1   intr,   r-   r-   r-   r.   r0   ,   r/   r0   c                   @   r!   )CommentOnIssuez6Schema for operations that require a comment as input..Follow the required formatting.r$   inputNr'   r(   r)   r*   r   r5   r+   r,   r-   r-   r-   r.   r3   2   r/   r3   c                   @   r!   )GetPRz8Schema for operations that require a PR number as input.r   z&The PR number as an integer, e.g. `12`r$   	pr_numberN)r'   r(   r)   r*   r   r8   r2   r,   r-   r-   r-   r.   r7   8   r/   r7   c                   @   r!   )CreatePRz@Schema for operations that require a PR title and body as input..r4   r$   formatted_prN)r'   r(   r)   r*   r   r:   r+   r,   r-   r-   r-   r.   r9   >   r/   r9   c                   @   r!   )
CreateFileDSchema for operations that require a file path and content as input..r4   r$   formatted_fileN)r'   r(   r)   r*   r   r=   r+   r,   r-   r-   r-   r.   r;   D   r/   r;   c                   @   r!   )ReadFile8Schema for operations that require a file path as input..z}The full file path of the file you would like to read where the path must NOT start with a slash, e.g. `some_dir/my_file.py`.r$   formatted_filepathNr'   r(   r)   r*   r   r@   r+   r,   r-   r-   r-   r.   r>   J      
 r>   c                   @   r!   )
UpdateFiler<   .z#Strictly follow the provided rules.r$   formatted_file_updateN)r'   r(   r)   r*   r   rD   r+   r,   r-   r-   r-   r.   rC   V   
   
 rC   c                   @   r!   )
DeleteFiler?   .zThe full file path of the file you would like to delete where the path must NOT start with a slash, e.g. `some_dir/my_file.py`. Only input a string, not the param name.r$   r@   NrA   r-   r-   r-   r.   rF   ^   rB   rF   c                   @   r!   )DirectoryPathz=Schema for operations that require a directory path as input.r#   zmThe path of the directory, e.g. `some_dir/inner_dir`. Only input a string, do not include the parameter name.r$   r5   Nr6   r-   r-   r-   r.   rG   l   rB   rG   c                   @   r!   )
BranchNamez:Schema for operations that require a branch name as input..z)The name of the branch, e.g. `my_branch`.r$   branch_nameN)r'   r(   r)   r*   r   rI   r+   r,   r-   r-   r-   r.   rH   x   rE   rH   c                   @   r!   )
SearchCode;Schema for operations that require a search query as input..zQA keyword-focused natural language searchquery for code, e.g. `MyFunctionName()`.r$   search_queryNr'   r(   r)   r*   r   rL   r+   r,   r-   r-   r-   r.   rJ      rB   rJ   c                   @   r!   )CreateReviewRequestz7Schema for operations that require a username as input..z@GitHub username of the user being requested, e.g. `my_username`.r$   usernameN)r'   r(   r)   r*   r   rO   r+   r,   r-   r-   r-   r.   rN         
 rN   c                   @   r!   )SearchIssuesAndPRsrK   .z>Natural language search query, e.g. `My issue title or topic`.r$   rL   NrM   r-   r-   r-   r.   rQ      rP   rQ   c                   @   r!   )TagNamez7Schema for operations that require a tag name as input..z+The tag name of the release, e.g. `v1.0.0`.r$   tag_nameN)r'   r(   r)   r*   r   rS   r+   r,   r-   r-   r-   r.   rR      rP   rR   c                   @   sR   e Zd ZU dZg Zee ed< e	dde	de
dd fddZdee fd	d
ZdS )GitHubToolkita)  GitHub Toolkit.

    *Security Note*: This toolkit contains tools that can read and modify
        the state of a service; e.g., by creating, deleting, or updating,
        reading underlying data.

        For example, this toolkit can be used to create issues, pull requests,
        and comments on GitHub.

        See [Security](https://python.langchain.com/docs/security) for more information.

    Setup:
        See detailed installation instructions here:
        https://python.langchain.com/docs/integrations/tools/github/#installation

        You will need to install ``pygithub`` and set the following environment
        variables:

        .. code-block:: bash

            pip install -U pygithub
            export GITHUB_APP_ID="your-app-id"
            export GITHUB_APP_PRIVATE_KEY="path-to-private-key"
            export GITHUB_REPOSITORY="your-github-repository"

    Instantiate:
        .. code-block:: python

            from langchain_community.agent_toolkits.github.toolkit import GitHubToolkit
            from langchain_community.utilities.github import GitHubAPIWrapper

            github = GitHubAPIWrapper()
            toolkit = GitHubToolkit.from_github_api_wrapper(github)

    Tools:
        .. code-block:: python

            tools = toolkit.get_tools()
            for tool in tools:
                print(tool.name)

        .. code-block:: none

            Get Issues
            Get Issue
            Comment on Issue
            List open pull requests (PRs)
            Get Pull Request
            Overview of files included in PR
            Create Pull Request
            List Pull Requests' Files
            Create File
            Read File
            Update File
            Delete File
            Overview of existing files in Main branch
            Overview of files in current working branch
            List branches in this repository
            Set active branch
            Create a new branch
            Get files from a directory
            Search issues and pull requests
            Search code
            Create review request

    Include release tools:
        By default, the toolkit does not include release-related tools.
        You can include them by setting ``include_release_tools=True`` when
        initializing the toolkit:

        .. code-block:: python

            toolkit = GitHubToolkit.from_github_api_wrapper(
                github, include_release_tools=True
            )

        Setting ``include_release_tools=True`` will include the following tools:

        .. code-block:: none

            Get latest release
            Get releases
            Get release

    Use within an agent:
        .. code-block:: python

            from langchain_openai import ChatOpenAI
            from langgraph.prebuilt import create_react_agent

            # Select example tool
            tools = [tool for tool in toolkit.get_tools() if tool.name == "Get Issue"]
            assert len(tools) == 1
            tools[0].name = "get_issue"

            llm = ChatOpenAI(model="gpt-4o-mini")
            agent_executor = create_react_agent(llm, tools)

            example_query = "What is the title of issue 24888?"

            events = agent_executor.stream(
                {"messages": [("user", example_query)]},
                stream_mode="values",
            )
            for event in events:
                event["messages"][-1].pretty_print()

        .. code-block:: none

             ================================[1m Human Message [0m=================================

            What is the title of issue 24888?
            ==================================[1m Ai Message [0m==================================
            Tool Calls:
            get_issue (call_iSYJVaM7uchfNHOMJoVPQsOi)
            Call ID: call_iSYJVaM7uchfNHOMJoVPQsOi
            Args:
                issue_number: 24888
            =================================[1m Tool Message [0m=================================
            Name: get_issue

            {"number": 24888, "title": "Standardize KV-Store Docs", "body": "..."
            ==================================[1m Ai Message [0m==================================

            The title of issue 24888 is "Standardize KV-Store Docs".

    Parameters:
        tools: List[BaseTool]. The tools in the toolkit. Default is an empty list.
    toolsFgithub_api_wrapperinclude_release_toolsreturnc                    sT  ddt tdddttdddttddd	ttdd
dttdddt	tdddt
tdddt	tdddttdddttdddttdddttdddttdddttdddttddd ttdd!d"ttdd#d$ttdd%d&ttdd'd(ttdd)d*t t!dg}d+d,t"tdd-d.t#tdd/d0t$t%dg}||r|ng  } fd1d2|D }| |d3S )4aH  Create a GitHubToolkit from a GitHubAPIWrapper.

        Args:
            github_api_wrapper: GitHubAPIWrapper. The GitHub API wrapper.
            include_release_tools: bool. Whether to include release-related tools.
                Defaults to False.

        Returns:
            GitHubToolkit. The GitHub toolkit.
        
get_issuesz
Get Issues)modenamer%   args_schema	get_issuez	Get Issuecomment_on_issuezComment on Issuelist_open_pull_requestszList open pull requests (PRs)get_pull_requestzGet Pull Requestlist_pull_request_filesz Overview of files included in PRcreate_pull_requestzCreate Pull RequestzList Pull Requests' Filescreate_filezCreate File	read_filez	Read Fileupdate_filezUpdate Filedelete_filezDelete Filelist_files_in_main_branchz)Overview of existing files in Main branchlist_files_in_bot_branchz+Overview of files in current working branchlist_branches_in_repoz List branches in this repositoryset_active_branchzSet active branchcreate_branchzCreate a new branchget_files_from_directoryzGet files from a directorysearch_issues_and_prszSearch issues and pull requestssearch_codezSearch codecreate_review_requestzCreate review requestget_latest_releasezGet latest releaseget_releaseszGet releasesget_releasezGet releasec                    s2   g | ]}t |d  |d |d  |dddqS )r[   r%   rZ   r\   N)r[   r%   rZ   api_wrapperr\   )r   get).0actionrV   r-   r.   
<listcomp>  s    
z9GitHubToolkit.from_github_api_wrapper.<locals>.<listcomp>rU   )&r   r"   r   r0   r   r3   r   r   r7   r   r   r9   r
   r;   r   r>   r   rC   r   rF   r   r   r   r   rH   r	   r   rG   r   rQ   r   rJ   r   rN   r   r   r   rR   )clsrV   rW   
operationsrelease_operationsrU   r-   rw   r.   from_github_api_wrapper,  s    


z%GitHubToolkit.from_github_api_wrapperc                 C   s   | j S )zGet the tools in the toolkit.ry   )selfr-   r-   r.   	get_tools  s   zGitHubToolkit.get_toolsN)F)r'   r(   r)   r*   rU   r   r   r,   classmethodr    boolr}   r   r-   r-   r-   r.   rT      s   
   1rT   N)7r*   typingr   r   langchain_core.toolsr   langchain_core.tools.baser   pydanticr   r   'langchain_community.tools.github.promptr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   %langchain_community.tools.github.toolr   $langchain_community.utilities.githubr    r"   r0   r3   r7   r9   r;   r>   rC   rF   rG   rH   rJ   rN   rQ   rR   rT   r-   r-   r-   r.   <module>   s0    d			