mirror of
https://github.com/Myzel394/zsh-copilot.git
synced 2025-06-18 12:45:27 +02:00
fix: Overall improvements
This commit is contained in:
parent
411e9329e9
commit
6c1ada57ab
@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
# Default key binding
|
# Default key binding
|
||||||
(( ! ${+ZSH_COPILOT_KEY} )) &&
|
(( ! ${+ZSH_COPILOT_KEY} )) &&
|
||||||
typeset -g ZSH_COPILOT_KEY='^z'
|
typeset -g ZSH_COPILOT_KEY='^z'
|
||||||
@ -47,24 +49,27 @@ read -r -d '' ZSH_COPILOT_SYSTEM_PROMPT <<- EOM
|
|||||||
EOM
|
EOM
|
||||||
fi
|
fi
|
||||||
|
|
||||||
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() {
|
function _suggest_ai() {
|
||||||
local OPENAI_API_URL=${OPENAI_API_URL:-"api.openai.com"}
|
#### Prepare environment
|
||||||
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=""
|
local context_info=""
|
||||||
if [[ "$ZSH_COPILOT_SEND_CONTEXT" == 'true' ]]; then
|
if [[ "$ZSH_COPILOT_SEND_CONTEXT" == 'true' ]]; then
|
||||||
context_info="Context: You are user $(whoami) with id $(id) in directory $(pwd).
|
local system
|
||||||
Your shell is $(echo $SHELL) and your terminal is $(echo $TERM) running on $(uname -a).
|
|
||||||
$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
|
fi
|
||||||
|
|
||||||
# Get input
|
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"
|
||||||
|
fi
|
||||||
|
|
||||||
|
##### Get input
|
||||||
local input=$(echo "${BUFFER:0:$CURSOR}" | tr '\n' ';')
|
local input=$(echo "${BUFFER:0:$CURSOR}" | tr '\n' ';')
|
||||||
input=$(echo "$input" | sed 's/"/\\"/g')
|
input=$(echo "$input" | sed 's/"/\\"/g')
|
||||||
|
|
||||||
@ -73,8 +78,10 @@ function _suggest_ai() {
|
|||||||
|
|
||||||
local full_prompt=$(echo "$ZSH_COPILOT_SYSTEM_PROMPT $context_info" | tr -d '\n')
|
local full_prompt=$(echo "$ZSH_COPILOT_SYSTEM_PROMPT $context_info" | tr -d '\n')
|
||||||
|
|
||||||
|
##### Fetch message
|
||||||
local data
|
local data
|
||||||
local response
|
local response
|
||||||
|
local message
|
||||||
|
|
||||||
if [[ "$ZSH_COPILOT_AI_PROVIDER" == "openai" ]]; then
|
if [[ "$ZSH_COPILOT_AI_PROVIDER" == "openai" ]]; then
|
||||||
data="{
|
data="{
|
||||||
@ -90,12 +97,13 @@ function _suggest_ai() {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}"
|
}"
|
||||||
response=$(curl "https://${OPENAI_API_URL}/v1/chat/completions" \
|
response=$(curl "https://${openai_api_url}/v1/chat/completions" \
|
||||||
--silent \
|
--silent \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "Authorization: Bearer $OPENAI_API_KEY" \
|
-H "Authorization: Bearer $OPENAI_API_KEY" \
|
||||||
-d "$data")
|
-d "$data")
|
||||||
local message=$(echo "$response" | jq -r '.choices[0].message.content')
|
|
||||||
|
message=$(echo "$response" | jq -r '.choices[0].message.content')
|
||||||
elif [[ "$ZSH_COPILOT_AI_PROVIDER" == "anthropic" ]]; then
|
elif [[ "$ZSH_COPILOT_AI_PROVIDER" == "anthropic" ]]; then
|
||||||
data="{
|
data="{
|
||||||
\"model\": \"claude-3-5-sonnet-20240620\",
|
\"model\": \"claude-3-5-sonnet-20240620\",
|
||||||
@ -108,18 +116,21 @@ function _suggest_ai() {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}"
|
}"
|
||||||
response=$(curl "https://${ANTHROPIC_API_URL}/v1/messages" \
|
response=$(curl "https://${anthropic_api_url}/v1/messages" \
|
||||||
--silent \
|
--silent \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
||||||
-H "anthropic-version: 2023-06-01" \
|
-H "anthropic-version: 2023-06-01" \
|
||||||
-d "$data")
|
-d "$data")
|
||||||
local message=$(echo "$response" | jq -r '.content[0].text')
|
|
||||||
|
message=$(echo "$response" | jq -r '.content[0].text')
|
||||||
else
|
else
|
||||||
echo "Invalid AI provider selected. Please choose 'openai' or 'anthropic'."
|
echo "Invalid AI provider selected. Please choose 'openai' or 'anthropic'."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
##### Process response
|
||||||
|
|
||||||
local first_char=${message:0:1}
|
local first_char=${message:0:1}
|
||||||
local suggestion=${message:1:${#message}}
|
local suggestion=${message:1:${#message}}
|
||||||
|
|
||||||
@ -128,6 +139,8 @@ function _suggest_ai() {
|
|||||||
echo "$(date);INPUT:$input;RESPONSE:$response;FIRST_CHAR:$first_char;SUGGESTION:$suggestion:DATA:$data" >> /tmp/zsh-copilot.log
|
echo "$(date);INPUT:$input;RESPONSE:$response;FIRST_CHAR:$first_char;SUGGESTION:$suggestion:DATA:$data" >> /tmp/zsh-copilot.log
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
##### And now, let's actually show the suggestion to the user!
|
||||||
|
|
||||||
if [[ "$first_char" == '=' ]]; then
|
if [[ "$first_char" == '=' ]]; then
|
||||||
# Reset user input
|
# Reset user input
|
||||||
BUFFER=""
|
BUFFER=""
|
||||||
@ -150,4 +163,5 @@ function zsh-copilot() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
zle -N _suggest_ai
|
zle -N _suggest_ai
|
||||||
bindkey $ZSH_COPILOT_KEY _suggest_ai
|
bindkey "$ZSH_COPILOT_KEY" _suggest_ai
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user