Compare commits

..

No commits in common. "bcea2d2033a3109f4d173ccb0acd508076e32169" and "0bc635926b02166731c132fe71bcbfa097e83cd9" have entirely different histories.

2 changed files with 26 additions and 76 deletions

View File

@ -1,7 +1,6 @@
**NOTICE**: I'm slowly migrating my repositories to my own Git server. Please visit this repository at [https://git.myzel394.app/Myzel394/zsh-copilot](https://git.myzel394.app/Myzel394/zsh-copilot) for the latest updates.
# zsh-copilot
Get suggestions **truly** in your shell. No `suggest` bullshit. Just press `CTRL + Z` and get your suggestion.
https://github.com/Myzel394/zsh-copilot/assets/50424412/ed2bc8ac-ce49-4012-ab73-53cf6f3151a2
@ -16,31 +15,12 @@ Please make sure you have the following dependencies installed:
* [jq](https://github.com/jqlang/jq)
* [curl](https://github.com/curl/curl)
### Oh My Zsh
1. Clone `zsh-copilot` into `$ZSH_CUSTOM/plugins` (by default ~/.config/oh-my-zsh/custom/plugins)
```sh
git clone https://git.myzel394.app/Myzel394/zsh-copilot ${ZSH_CUSTOM:-~/.config/oh-my-zsh/custom}/plugins/zsh-copilot
git clone https://github.com/Myzel394/zsh-copilot.git ~/.zsh-copilot
echo "source ~/.zsh-copilot/zsh-copilot.plugin.zsh" >> ~/.zshrc
```
2. Add `zsh-copilot` to the plugins array in your `.zshrc` file:
```bash
plugins=(
# your other plugins...
zsh-autosuggestions
)
```
### Manual Installation
```sh
git clone https://git.myzel394.app/Myzel394/zsh-copilot ~/.config/zsh-copilot
echo "source ~/.config/zsh-copilot/zsh-copilot.plugin.zsh" >> ~/.zshrc
```
## Configuration
### Configuration
You need to have an API key for either OpenAI or Anthropic to use this plugin. Expose this via the appropriate environment variable:

View File

@ -1,5 +1,3 @@
#!/usr/bin/env zsh
# Default key binding
(( ! ${+ZSH_COPILOT_KEY} )) &&
typeset -g ZSH_COPILOT_KEY='^z'
@ -12,82 +10,63 @@
typeset -g ZSH_COPILOT_DEBUG=false
# New option to select AI provider
if [[ -z "$ZSH_COPILOT_AI_PROVIDER" ]]; then
if [[ -n "$OPENAI_API_KEY" ]]; then
typeset -g ZSH_COPILOT_AI_PROVIDER="openai"
elif [[ -n "$ANTHROPIC_API_KEY" ]]; then
typeset -g ZSH_COPILOT_AI_PROVIDER="anthropic"
else
echo "No AI provider selected. Please set either OPENAI_API_KEY or ANTHROPIC_API_KEY."
return 1
fi
fi
(( ! ${+ZSH_COPILOT_AI_PROVIDER} )) &&
typeset -g ZSH_COPILOT_AI_PROVIDER="openai"
# System prompt
if [[ -z "$ZSH_COPILOT_SYSTEM_PROMPT" ]]; then
read -r -d '' ZSH_COPILOT_SYSTEM_PROMPT <<- EOM
read -r -d '' SYSTEM_PROMPT <<- EOM
You will be given the raw input of a shell command.
Your task is to either complete the command or provide a new command that you think the user is trying to type.
If you return a completely new command for the user, prefix is with an equal sign (=).
If you return a completion for the user's command, prefix it with a plus sign (+).
MAKE SURE TO ONLY INCLUDE THE REST OF THE COMPLETION!!!
Do not write any leading or trailing characters except if required for the completion to work.
Only respond with either a completion or a new command, not both.
Your response may only start with either a plus sign or an equal sign.
Your response MAY NOT start with both! This means that your response IS NOT ALLOWED to start with '+=' or '=+'.
Your response MAY NOT contain any newlines!
Do NOT add any additional text, comments, or explanations to your response.
You MAY explain the command by writing a short line after the comment symbol (#).
Do not ask for more information, you won't receive it.
Your response will be run in the user's shell.
Make sure input is escaped correctly if needed so.
Your input should be able to run without any modifications to it.
Don't you dare to return anything else other than a shell command!!!
DO NOT INTERACT WITH THE USER IN NATURAL LANGUAGE! If you do, you will be banned from the system.
Note that the double quote sign is escaped. Keep this in mind when you create quotes.
Here are two examples:
* User input: 'list files in current directory'; Your response: '=ls' (ls is the builtin command for listing files)
* User input: 'cd /tm'; Your response: '+p' (/tmp is the standard temp folder on linux and mac).
EOM
if [[ "$OSTYPE" == "darwin"* ]]; then
SYSTEM="Your system is ${$(sw_vers | xargs | sed 's/ /./g')}."
else
SYSTEM="Your system is ${$(cat /etc/*-release | xargs | sed 's/ /,/g')}."
fi
function _suggest_ai() {
#### Prepare environment
local openai_api_url=${OPENAI_API_URL:-"api.openai.com"}
local anthropic_api_url=${ANTHROPIC_API_URL:-"api.anthropic.com"}
local OPENAI_API_URL=${OPENAI_API_URL:-"api.openai.com"}
local ANTHROPIC_API_URL=${ANTHROPIC_API_URL:-"api.anthropic.com"}
local context_info=""
if [[ "$ZSH_COPILOT_SEND_CONTEXT" == 'true' ]]; then
local system
if [[ "$OSTYPE" == "darwin"* ]]; then
system="Your system is ${$(sw_vers | xargs | sed 's/ /./g')}."
else
system="Your system is ${$(cat /etc/*-release | xargs | sed 's/ /,/g')}."
fi
context_info="Context: You are user $(whoami) with id $(id) in directory $(pwd).
Your shell is $(echo $SHELL) and your terminal is $(echo $TERM) running on $(uname -a).
$system"
$SYSTEM"
fi
##### Get input
# Get input
local input=$(echo "${BUFFER:0:$CURSOR}" | tr '\n' ';')
input=$(echo "$input" | sed 's/"/\\"/g')
_zsh_autosuggest_clear
zle -R "Thinking..."
local full_prompt=$(echo "$ZSH_COPILOT_SYSTEM_PROMPT $context_info" | tr -d '\n')
local full_prompt=$(echo "$SYSTEM_PROMPT $context_info" | tr -d '\n')
##### Fetch message
local data
local response
local message
if [[ "$ZSH_COPILOT_AI_PROVIDER" == "openai" ]]; then
# OpenAI's API payload
data="{
\"model\": \"gpt-4o-mini\",
\"messages\": [
@ -101,17 +80,15 @@ function _suggest_ai() {
}
]
}"
response=$(curl "https://${openai_api_url}/v1/chat/completions" \
response=$(curl "https://${OPENAI_API_URL}/v1/chat/completions" \
--silent \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$data")
message=$(echo "$response" | tr -d '\n' | jq -r '.choices[0].message.content')
local message=$(echo "$response" | jq -r '.choices[0].message.content')
elif [[ "$ZSH_COPILOT_AI_PROVIDER" == "anthropic" ]]; then
# Anthropic's API payload
data="{
\"model\": \"claude-3-5-sonnet-latest\",
\"model\": \"claude-3-5-sonnet-20240620\",
\"max_tokens\": 1000,
\"system\": \"$full_prompt\",
\"messages\": [
@ -121,21 +98,18 @@ function _suggest_ai() {
}
]
}"
response=$(curl "https://${anthropic_api_url}/v1/messages" \
response=$(curl "https://${ANTHROPIC_API_URL}/v1/messages" \
--silent \
-H "Content-Type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d "$data")
message=$(echo "$response" | tr -d '\n' | jq -r '.content[0].text')
local message=$(echo "$response" | jq -r '.content[0].text')
else
echo "Invalid AI provider selected. Please choose 'openai' or 'anthropic'."
return 1
fi
##### Process response
local first_char=${message:0:1}
local suggestion=${message:1:${#message}}
@ -144,8 +118,6 @@ function _suggest_ai() {
echo "$(date);INPUT:$input;RESPONSE:$response;FIRST_CHAR:$first_char;SUGGESTION:$suggestion:DATA:$data" >> /tmp/zsh-copilot.log
fi
##### And now, let's actually show the suggestion to the user!
if [[ "$first_char" == '=' ]]; then
# Reset user input
BUFFER=""
@ -163,10 +135,8 @@ function zsh-copilot() {
echo "Configurations:"
echo " - ZSH_COPILOT_KEY: Key to press to get suggestions (default: ^z, value: $ZSH_COPILOT_KEY)."
echo " - ZSH_COPILOT_SEND_CONTEXT: If \`true\`, zsh-copilot will send context information (whoami, shell, pwd, etc.) to the AI model (default: true, value: $ZSH_COPILOT_SEND_CONTEXT)."
echo " - ZSH_COPILOT_AI_PROVIDER: AI provider to use ('openai' or 'anthropic', value: $ZSH_COPILOT_AI_PROVIDER)."
echo " - ZSH_COPILOT_SYSTEM_PROMPT: System prompt to use for the AI model (uses a built-in prompt by default)."
echo " - ZSH_COPILOT_AI_PROVIDER: AI provider to use ('openai' or 'anthropic', default: openai, value: $ZSH_COPILOT_AI_PROVIDER)."
}
zle -N _suggest_ai
bindkey "$ZSH_COPILOT_KEY" _suggest_ai
bindkey $ZSH_COPILOT_KEY _suggest_ai