-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot_hook.zsh
More file actions
50 lines (47 loc) · 1.74 KB
/
copilot_hook.zsh
File metadata and controls
50 lines (47 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Do copilot lookup
copilot_lookup() {
# Create a "thread"
thread_id="$(curl --http1.1 --no-alpn --fail --silent --show-error --data '{}' -X POST \
-H "Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview" \
-H "Authorization: Bearer $(gh auth token)" \
-H "Content-type: application/json" \
-H "User-Agent: go-gh"\
-H "X-Github-Api-Version: 2023-07-07" \
-H "Time-Zone: America/Detroit" \
https://api.githubcopilot.com/github/chat/threads | jq -r '.thread_id')"
# Make the request
data='{
"content": '"$(printf "%s" "$1" | jq -R -s '.')"',
"intent": "cli-suggest",
"references": [
{
"type": "cli-command",
"program": "shell"
}
]
}'
curl --http1.1 --no-alpn --fail --silent --show-error --data "$data" -X POST \
-H "Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview" \
-H "Authorization: Bearer $(gh auth token)" \
-H "Content-type: application/json" \
-H "User-Agent: go-gh"\
-H "X-Github-Api-Version: 2023-07-07" \
-H "Time-Zone: America/Detroit" \
https://api.githubcopilot.com/github/chat/threads/$thread_id/messages | jq -r '.message.content'
}
# Define a function to run before each command
copilot-accept-line() {
if [[ "$BUFFER" == '?:'* ]]; then
BUFFER="${BUFFER##?: }"
BUFFER="${BUFFER##?:}"
question="$BUFFER"
answer="$(copilot_lookup "$BUFFER")"
BUFFER="$answer"
zle end-of-line
BUFFER="$BUFFER
#${question//$'\n'/#\n}"
else
zle .accept-line
fi
}
zle -N accept-line copilot-accept-line