Skip to content

Instantly share code, notes, and snippets.

@obeone
Last active June 26, 2024 00:19
Show Gist options
  • Save obeone/dc66f2ca40b8254edab61ac50cdec0f3 to your computer and use it in GitHub Desktop.
Save obeone/dc66f2ca40b8254edab61ac50cdec0f3 to your computer and use it in GitHub Desktop.

Revisions

  1. obeone revised this gist Jun 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ sgpt --model [tab] # Will propose available models
    ### 2024-06-26

    - The completion of the role was bugged; it indicated .json at the end of role and it was not compatible with the expected arguments. The extension is now correctly filtered; however, if you have been impacted by the problem, you have to manually delete the existing cache of roles : `rm ~/.cache/sgpt_roles`
    -
    - Removing caches on chats, it was impossible to detect their creation of a new chat and therefore invalidate this cache. It is still retained on roles, even if the same defect is present. Because roles, to me, do not change every hours.

    ## How It Was Created

  2. obeone revised this gist Jun 26, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,7 @@ sgpt --model [tab] # Will propose available models
    ### 2024-06-26

    - The completion of the role was bugged; it indicated .json at the end of role and it was not compatible with the expected arguments. The extension is now correctly filtered; however, if you have been impacted by the problem, you have to manually delete the existing cache of roles : `rm ~/.cache/sgpt_roles`
    -

    ## How It Was Created

  3. obeone revised this gist Jun 26, 2024. 1 changed file with 22 additions and 44 deletions.
    66 changes: 22 additions & 44 deletions _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@
    # - a little bit [obeone](https://github.com/obeone)

    local model_cache_file="${HOME}/.cache/sgpt_models"
    local role_cache_file="${HOME}/.cache/sgpt_roles"
    local sgptrc="${HOME}/.config/shell_gpt/.sgptrc"

    # Source configuration if it exists
    @@ -47,56 +48,35 @@ _arguments -s \
    '--show-role[show role]:role name:_sgpt_roles' \
    '--list-roles[list roles]'

    function check_and_invalidate_cache {
    local chat_id="$1"
    local cache_file="${HOME}/.cache/sgpt_chats"
    local found_in_cache=0

    # Check if the chat/repl ID is in the cache
    if [[ -f "$cache_file" ]]; then
    while read -r line; do
    if [[ "$line" == *"$chat_id"* ]]; then
    found_in_cache=1
    break
    fi
    done < "$cache_file"
    fi

    # Invalidate the cache if not found
    if [[ $found_in_cache -eq 0 ]]; then
    rm -f "$cache_file"
    fi
    function _sgpt_chats {
    local -a chats
    # Fetch chats dynamically every time
    chats=("${(@f)$(sgpt --list-chats | xargs -n 1 basename)}")
    _describe -t chats 'available chats' chats
    }

    # Only invalidate cache when adding a new chat or repl, not on every completion
    function add_chat_or_repl {
    local new_id="$1"
    check_and_invalidate_cache "$new_id"
    function _invalidate_cache {
    local cache_file="$1"
    rm -f "$cache_file"
    }

    function _sgpt_chats {
    local -a chats
    local cache_file="${HOME}/.cache/sgpt_chats"

    # Call the check_and_invalidate_cache function with a sample chat ID
    check_and_invalidate_cache "sample_chat_id"
    function _validate_cache {
    local cache_file="$1"
    local cache_duration="$2"

    # Check cache
    if [[ -f "$cache_file" ]] && (( $(date +%s) - $(stat -c "%Y" "$cache_file" 2>/dev/null || stat -f "%m" "$cache_file" 2>/dev/null) < 43200 )); then
    chats=("${(@f)$(<"$cache_file")}")
    # Check if the cache file exists and is recent enough
    if [[ -f "$cache_file" ]] && (( $(date +%s) - $(stat -c "%Y" "$cache_file" 2>/dev/null || stat -f "%m" "$cache_file" 2>/dev/null) < cache_duration )); then
    return 0
    else
    # Fetch chats dynamically and cache results
    chats=("${(@f)$(sgpt --list-chats | xargs -n 1 basename)}")
    print -l $chats > "$cache_file"
    return 1
    fi
    _describe -t chats 'available chats' chats
    }

    function _sgpt_models {
    local -a models

    # Fetch OpenAI models if API key is available, exclude blacklisted models
    if [[ -n "$OPENAI_API_KEY" ]]; then
    if [[ -n "$OPENAI_API_KEY" ]] && ! [[ -z "$OPENAI_BASE_URL" ]]; then
    models+=("${(@f)$(curl -s "$OPENAI_BASE_URL/models" -H "Authorization: Bearer $OPENAI_API_KEY" | jq -r '.data[].id | select(test("dall-e.*|text-embedding.*|tts.*|whisper.*") | not)')}")
    fi

    @@ -110,21 +90,19 @@ function _sgpt_models {

    function _sgpt_roles {
    local -a roles
    local cache_file="${HOME}/.cache/sgpt_roles"

    # Check cache
    if [[ -f "$cache_file" ]] && (( $(date +%s) - $(stat -c "%Y" "$cache_file" 2>/dev/null || stat -f "%m" "$cache_file" 2>/dev/null) < 86400 )); then
    roles=("${(@f)$(<"$cache_file")}")
    # Validate cache (24 hours)
    if _validate_cache "$role_cache_file" 86400; then
    roles=("${(@f)$(<"$role_cache_file")}")
    else
    roles=("${(@f)$(sgpt --list-roles | awk -F/ '{print $NF}' | sed 's/\.json$//')}")
    print -l $roles > "$cache_file"
    print -l $roles > "$role_cache_file"
    fi
    _describe -t roles 'available roles' roles
    }

    function _invalidate_roles_cache {
    local cache_file="${HOME}/.cache/sgpt_roles"
    rm -f "$cache_file"
    _invalidate_cache "$role_cache_file"
    _sgpt_roles # Refresh the roles list
    }

  4. obeone revised this gist Jun 25, 2024. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,12 @@ sgpt -[tab] # Lists all subcommands
    sgpt --model [tab] # Will propose available models
    ```

    ## Changelog

    ### 2024-06-26

    - The completion of the role was bugged; it indicated .json at the end of role and it was not compatible with the expected arguments. The extension is now correctly filtered; however, if you have been impacted by the problem, you have to manually delete the existing cache of roles : `rm ~/.cache/sgpt_roles`

    ## How It Was Created

    The completion script for `sgpt` was primarily generated using a custom GPT model I created named [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) (you need ChatGPT Plus to use it).
  5. obeone revised this gist Jun 25, 2024. No changes.
  6. obeone revised this gist Jun 25, 2024. 1 changed file with 39 additions and 21 deletions.
    60 changes: 39 additions & 21 deletions _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,15 @@
    #compdef sgpt

    # Purpose:
    # This script file `_sgpt` should be placed in your fpath to provide zsh completions functionality for SGPT commands.
    # It utilizes zsh's native completion system by defining specific completion behaviors tailored to SGPT commands.

    # Installation:
    # 1. Check your current fpath by executing: `echo $fpath` in your zsh shell.
    # 2. To introduce a new directory to fpath, edit your .zshrc file:
    # Example: `fpath=($HOME/.zsh-completions $fpath)`
    # 3. Store this script file in the directory you have added to your fpath.
    # 4. For a system-wide installation on Linux:
    # Download and deploy this script with the following command:
    # sudo wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh

    # Contributions:
    # Principal contributions by:
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) as the primary creator.
    # - Guidance and revisions by [obeone](https://github.com/obeone).

    # Note:
    # - This configuration file presupposes the utilization of Zsh as your primary shell environment.
    # - It is crucial to restart your zsh session subsequent to alterations made to your fpath to ensure the updates are effectively recognized.
    # Be sure to name this file `_sgpt` and place it in your fpath.
    # This file is sourced by zsh when you start a new shell session.
    # To list your fpath, run `echo $fpath` in your shell.
    # To add this directory to fpath, add `fpath=($HOME/.config/shell_gpt $fpath)` to your .zshrc file.
    # For example, to install it system-wide:
    # wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh

    # Authors :
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)
    # - a little bit [obeone](https://github.com/obeone)

    local model_cache_file="${HOME}/.cache/sgpt_models"
    local sgptrc="${HOME}/.config/shell_gpt/.sgptrc"
    @@ -59,10 +47,40 @@ _arguments -s \
    '--show-role[show role]:role name:_sgpt_roles' \
    '--list-roles[list roles]'

    function check_and_invalidate_cache {
    local chat_id="$1"
    local cache_file="${HOME}/.cache/sgpt_chats"
    local found_in_cache=0

    # Check if the chat/repl ID is in the cache
    if [[ -f "$cache_file" ]]; then
    while read -r line; do
    if [[ "$line" == *"$chat_id"* ]]; then
    found_in_cache=1
    break
    fi
    done < "$cache_file"
    fi

    # Invalidate the cache if not found
    if [[ $found_in_cache -eq 0 ]]; then
    rm -f "$cache_file"
    fi
    }

    # Only invalidate cache when adding a new chat or repl, not on every completion
    function add_chat_or_repl {
    local new_id="$1"
    check_and_invalidate_cache "$new_id"
    }

    function _sgpt_chats {
    local -a chats
    local cache_file="${HOME}/.cache/sgpt_chats"

    # Call the check_and_invalidate_cache function with a sample chat ID
    check_and_invalidate_cache "sample_chat_id"

    # Check cache
    if [[ -f "$cache_file" ]] && (( $(date +%s) - $(stat -c "%Y" "$cache_file" 2>/dev/null || stat -f "%m" "$cache_file" 2>/dev/null) < 43200 )); then
    chats=("${(@f)$(<"$cache_file")}")
  7. obeone revised this gist May 14, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    # Installation:
    # 1. Check your current fpath by executing: `echo $fpath` in your zsh shell.
    # 2. To introduce a new directory to fpath, edit your .zshrc file:
    # Example: `fpath=($HOME/.config/shell_gpt $fpath)`
    # Example: `fpath=($HOME/.zsh-completions $fpath)`
    # 3. Store this script file in the directory you have added to your fpath.
    # 4. For a system-wide installation on Linux:
    # Download and deploy this script with the following command:
  8. obeone revised this gist May 14, 2024. No changes.
  9. obeone revised this gist May 14, 2024. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -12,17 +12,16 @@
    # 4. For a system-wide installation on Linux:
    # Download and deploy this script with the following command:
    # sudo wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh

    # Contributions:
    # Principal contributions by:
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) as the primary creator.
    # - Guidance and revisions by [obeone](https://github.com/obeone).

    # Note:
    # - This configuration file presupposes the utilization of Zsh as your primary shell environment.
    # - It is crucial to restart your zsh session subsequent to alterations made to your fpath to ensure the updates are effectively recognized.

    # Authors :
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)
    # - a little bit [obeone](https://github.com/obeone)

    local model_cache_file="${HOME}/.cache/sgpt_models"
    local sgptrc="${HOME}/.config/shell_gpt/.sgptrc"
  10. obeone revised this gist May 14, 2024. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,11 @@
    # Installation:
    # 1. Check your current fpath by executing: `echo $fpath` in your zsh shell.
    # 2. To introduce a new directory to fpath, edit your .zshrc file:
    # Example: `fpath=($HOME/.config/shell_gpt $fpath)`
    # Example: `fpath=($HOME/.config/shell_gpt $fpath)`
    # 3. Store this script file in the directory you have added to your fpath.
    # 4. For a system-wide installation on Linux:
    # Download and deploy this script with the following command:
    # sudo wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh
    # Download and deploy this script with the following command:
    # sudo wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh
    # Contributions:
    # Principal contributions by:
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) as the primary creator.
  11. obeone revised this gist May 14, 2024. 1 changed file with 19 additions and 6 deletions.
    25 changes: 19 additions & 6 deletions _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,24 @@
    #compdef sgpt

    # Be sure to name this file `_sgpt` and place it in your fpath.
    # This file is sourced by zsh when you start a new shell session.
    # To list your fpath, run `echo $fpath` in your shell.
    # To add this directory to fpath, add `fpath=($HOME/.config/shell_gpt $fpath)` to your .zshrc file.
    # For example, to install it system-wide :
    # wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh
    # Purpose:
    # This script file `_sgpt` should be placed in your fpath to provide zsh completions functionality for SGPT commands.
    # It utilizes zsh's native completion system by defining specific completion behaviors tailored to SGPT commands.

    # Installation:
    # 1. Check your current fpath by executing: `echo $fpath` in your zsh shell.
    # 2. To introduce a new directory to fpath, edit your .zshrc file:
    # Example: `fpath=($HOME/.config/shell_gpt $fpath)`
    # 3. Store this script file in the directory you have added to your fpath.
    # 4. For a system-wide installation on Linux:
    # Download and deploy this script with the following command:
    # sudo wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh
    # Contributions:
    # Principal contributions by:
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) as the primary creator.
    # - Guidance and revisions by [obeone](https://github.com/obeone).
    # Note:
    # - This configuration file presupposes the utilization of Zsh as your primary shell environment.
    # - It is crucial to restart your zsh session subsequent to alterations made to your fpath to ensure the updates are effectively recognized.

    # Authors :
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)
  12. obeone revised this gist May 14, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ You can view the entire creation process and the discussions that led to the fin
    ## Contributors

    - **[ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)** (ChatGPT Custom GPT, did almost all the job)
    - **[obeone](https://github.com/obeone)** (Testing)
    - **[obeone](https://github.com/obeone)** (Guiding and testing)

    ## License

  13. obeone revised this gist May 14, 2024. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -27,17 +27,17 @@ This ZSH completion script enhances your command line interface by providing aut

    3. **Source the Completion Script**
    > To enable the completion script, restart your ZSH session or source your `.zshrc` file if you haven't done so since updating it `source ~/.zshrc`
    > If you use Oh My Zsh, you can reload it with `omz reload`.
    ZSH should automatically source all completion scripts in your `fpath` when starting a new shell session. Ensure the script is named correctly as `_sgpt`.

    ## Usage

    Simply type `sgpt` followed by a space, then press `tab` to see available subcommands and options. For example:
    Simply type `sgpt` followed by a space and a `-`, then press `tab` to see available subcommands and options. For example:

    ```bash
    sgpt [tab] # Lists all subcommands
    sgpt -[tab] # Lists all subcommands
    sgpt --model [tab] # Will propose available models
    ```

  14. obeone revised this gist May 14, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ This ZSH completion script enhances your command line interface by providing aut

    3. **Source the Completion Script**
    > To enable the completion script, restart your ZSH session or source your `.zshrc` file if you haven't done so since updating it `source ~/.zshrc`
    > If you use Oh My Zsh, you can reload it with `omz reload`.
    ZSH should automatically source all completion scripts in your `fpath` when starting a new shell session. Ensure the script is named correctly as `_sgpt`.
  15. obeone revised this gist May 14, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ This ZSH completion script enhances your command line interface by providing aut
    ## Installation

    1. **Download the Completion Script**
    You can download the `_lms.zsh` script directly using the following command:
    You can download the `_sgpt` script directly using the following command:

    ```bash
    mkdir -p $HOME/.zsh_completions
  16. obeone revised this gist May 14, 2024. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,6 @@ This ZSH completion script enhances your command line interface by providing aut
    fpath=($HOME/.zsh_completions $fpath)
    ```


    3. **Source the Completion Script**
    > To enable the completion script, restart your ZSH session or source your `.zshrc` file if you haven't done so since updating it `source ~/.zshrc`
    > If you use Oh My Zsh, you can reload it with `omz reload`.
    @@ -43,7 +42,9 @@ sgpt --model [tab] # Will propose available models

    ## How It Was Created

    The completion script for `sgpt` was primarily generated using a custom GPT model named [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) (you need ChatGPT Plus to use it). This model specializes in creating ZSH completion scripts by analyzing the `--help` outputs of various commands. Here's the process:
    The completion script for `sgpt` was primarily generated using a custom GPT model I created named [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) (you need ChatGPT Plus to use it).

    **This model specializes in creating ZSH completion scripts** by analyzing the `--help` outputs of various commands. Here's the process:

    1. **Generating Completion Logic:**
    I provided the `--help` outputs of the `sgpt` command to ZSH Expert, which then generated the necessary ZSH completion logic, including handling of subcommands and options.
  17. obeone revised this gist May 14, 2024. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ This ZSH completion script enhances your command line interface by providing aut

    ```bash
    mkdir -p $HOME/.zsh_completions
    wget -O $HOME/.zsh_completions/_lms https://gist.github.com/obeone/e090762d683f1e666caf12902aa74c75/raw/_lms.zsh
    wget -O $HOME/.zsh_completions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh
    ```

    2. **Add to ZSH Fpath**
    @@ -48,18 +48,15 @@ The completion script for `sgpt` was primarily generated using a custom GPT mode
    1. **Generating Completion Logic:**
    I provided the `--help` outputs of the `sgpt` command to ZSH Expert, which then generated the necessary ZSH completion logic, including handling of subcommands and options.

    2. **Manual Tweaks and Testing:**
    While ZSH Expert handled the bulk of the script creation, manual adjustments were made for optimal performance and to ensure compatibility with ZSH's completion system.

    3. **Validation and Iteration:**
    2. **Validation and Iteration:**
    The script was tested iteratively to catch any issues with command completion, especially around new or complex command options. Feel free to report any issues to help improve the script further.

    You can view the entire creation process and the discussions that led to the final script in this [chat history](https://chatgpt.com/share/2e375594-bc69-4403-9e48-f0fe8faeb542?oai-dm=1).

    ## Contributors

    - **[ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)** (ChatGPT Custom GPT, did almost all the job)
    - **[obeone](https://github.com/obeone)** (Manual Tweaks and Testing)
    - **[obeone](https://github.com/obeone)** (Testing)

    ## License

  18. obeone revised this gist May 14, 2024. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,11 @@ This ZSH completion script enhances your command line interface by providing aut
    ## Installation

    1. **Download the Completion Script**
    You can download the `_sgpt.zsh` script directly using the following command:
    You can download the `_lms.zsh` script directly using the following command:

    ```bash
    mkdir -p $HOME/.config/shell_gpt
    wget -O $HOME/.config/shell_gpt/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh
    mkdir -p $HOME/.zsh_completions
    wget -O $HOME/.zsh_completions/_lms https://gist.github.com/obeone/e090762d683f1e666caf12902aa74c75/raw/_lms.zsh
    ```

    2. **Add to ZSH Fpath**
    @@ -22,9 +22,10 @@ This ZSH completion script enhances your command line interface by providing aut
    To add a new directory to your `fpath`, include the following line in your `.zshrc`:

    ```bash
    fpath=($HOME/.config/shell_gpt $fpath)
    fpath=($HOME/.zsh_completions $fpath)
    ```


    3. **Source the Completion Script**
    > To enable the completion script, restart your ZSH session or source your `.zshrc` file if you haven't done so since updating it `source ~/.zshrc`
    > If you use Oh My Zsh, you can reload it with `omz reload`.
  19. obeone revised this gist May 14, 2024. No changes.
  20. obeone revised this gist May 14, 2024. 1 changed file with 65 additions and 1 deletion.
    66 changes: 65 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,65 @@
    ‎‎​
    # ZSH Completion for Shell GPT CLI (`sgpt` command)

    This ZSH completion script enhances your command line interface by providing auto-completion for the [`sgpt`](https://github.com/TheR1D/shell_gpt) command, part of the Shell GPT suite. It supports all subcommands and their options, significantly improving efficiency and reducing the need for memorizing syntax.

    ## Installation

    1. **Download the Completion Script**
    You can download the `_sgpt.zsh` script directly using the following command:

    ```bash
    mkdir -p $HOME/.config/shell_gpt
    wget -O $HOME/.config/shell_gpt/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh
    ```

    2. **Add to ZSH Fpath**
    Ensure that the script is in one of the directories listed in your `fpath`. You can view your current `fpath` with:

    ```bash
    echo $fpath
    ```

    To add a new directory to your `fpath`, include the following line in your `.zshrc`:

    ```bash
    fpath=($HOME/.config/shell_gpt $fpath)
    ```

    3. **Source the Completion Script**
    > To enable the completion script, restart your ZSH session or source your `.zshrc` file if you haven't done so since updating it `source ~/.zshrc`
    > If you use Oh My Zsh, you can reload it with `omz reload`.
    ZSH should automatically source all completion scripts in your `fpath` when starting a new shell session. Ensure the script is named correctly as `_sgpt`.

    ## Usage

    Simply type `sgpt` followed by a space, then press `tab` to see available subcommands and options. For example:

    ```bash
    sgpt [tab] # Lists all subcommands
    sgpt --model [tab] # Will propose available models
    ```

    ## How It Was Created

    The completion script for `sgpt` was primarily generated using a custom GPT model named [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) (you need ChatGPT Plus to use it). This model specializes in creating ZSH completion scripts by analyzing the `--help` outputs of various commands. Here's the process:

    1. **Generating Completion Logic:**
    I provided the `--help` outputs of the `sgpt` command to ZSH Expert, which then generated the necessary ZSH completion logic, including handling of subcommands and options.

    2. **Manual Tweaks and Testing:**
    While ZSH Expert handled the bulk of the script creation, manual adjustments were made for optimal performance and to ensure compatibility with ZSH's completion system.

    3. **Validation and Iteration:**
    The script was tested iteratively to catch any issues with command completion, especially around new or complex command options. Feel free to report any issues to help improve the script further.

    You can view the entire creation process and the discussions that led to the final script in this [chat history](https://chatgpt.com/share/2e375594-bc69-4403-9e48-f0fe8faeb542?oai-dm=1).

    ## Contributors

    - **[ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)** (ChatGPT Custom GPT, did almost all the job)
    - **[obeone](https://github.com/obeone)** (Manual Tweaks and Testing)

    ## License

    Distributed under the MIT License.
  21. obeone revised this gist May 14, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​
  22. obeone revised this gist May 14, 2024. No changes.
  23. obeone revised this gist May 12, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -86,7 +86,7 @@ function _sgpt_roles {
    if [[ -f "$cache_file" ]] && (( $(date +%s) - $(stat -c "%Y" "$cache_file" 2>/dev/null || stat -f "%m" "$cache_file" 2>/dev/null) < 86400 )); then
    roles=("${(@f)$(<"$cache_file")}")
    else
    roles=("${(@f)$(sgpt --list-roles | awk -F/ '{print $NF}' | sed 's/\\.json$//')}")
    roles=("${(@f)$(sgpt --list-roles | awk -F/ '{print $NF}' | sed 's/\.json$//')}")
    print -l $roles > "$cache_file"
    fi
    _describe -t roles 'available roles' roles
  24. obeone revised this gist May 6, 2024. No changes.
  25. obeone revised this gist May 6, 2024. No changes.
  26. obeone revised this gist May 6, 2024. No changes.
  27. obeone revised this gist May 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # To list your fpath, run `echo $fpath` in your shell.
    # To add this directory to fpath, add `fpath=($HOME/.config/shell_gpt $fpath)` to your .zshrc file.
    # For example, to install it system-wide :
    # wget -O /usr/share/zsh/site-functions/_sgpt
    # wget -O /usr/share/zsh/site-functions/_sgpt https://gist.github.com/obeone/dc66f2ca40b8254edab61ac50cdec0f3/raw/_sgpt.zsh

    # Authors :
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)
  28. obeone revised this gist May 6, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@
    # This file is sourced by zsh when you start a new shell session.
    # To list your fpath, run `echo $fpath` in your shell.
    # To add this directory to fpath, add `fpath=($HOME/.config/shell_gpt $fpath)` to your .zshrc file.
    # For example, to install it system-wide :
    # wget -O /usr/share/zsh/site-functions/_sgpt

    # Authors :
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)
  29. obeone revised this gist May 6, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion _sgpt.zsh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,9 @@
    # To list your fpath, run `echo $fpath` in your shell.
    # To add this directory to fpath, add `fpath=($HOME/.config/shell_gpt $fpath)` to your .zshrc file.

    # Author : ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert) and a little bit [obeone](https://github.com/obeone)
    # Authors :
    # - ChatGPT [ZSH Expert](https://chatgpt.com/g/g-XczdbjXSW-zsh-expert)
    # - a little bit [obeone](https://github.com/obeone)

    local model_cache_file="${HOME}/.cache/sgpt_models"
    local sgptrc="${HOME}/.config/shell_gpt/.sgptrc"
  30. obeone renamed this gist May 6, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.