Skip to content

Commit 36b0e0f

Browse files
committed
Use token files in plugin directory to only require single login + fix multi-line update to buffer on chat response + adds move.sh + run-chat.sh
1 parent e93fbcc commit 36b0e0f

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

.devcontainer/Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
FROM alpine:latest
2-
RUN apk update && apk add git vim curl
3-
RUN mkdir ~/.vim
4-
RUN mkdir ~/.vim/bundle
2+
RUN apk update && apk add git vim curl wslview
3+
RUN mkdir -p ~/.vim/bundle
54
RUN git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

move.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
cp -r * ~/.vim/bundle/copilot.vim/

plugin/copilot_chat.vim

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
let s:plugin_dir = expand('<sfile>:p:h')
2-
let s:token_file = s:plugin_dir . '/copilot_token'
1+
scriptencoding utf-8
2+
3+
let s:plugin_dir = expand('<sfile>:p:h:h')
4+
let s:device_token_file = s:plugin_dir . "/.device_token"
5+
let s:chat_token_file = s:plugin_dir . "/.chat_token"
6+
37

48
function! CopilotChat()
59
" Open a new split window for the chat
@@ -30,7 +34,7 @@ function! SubmitChatMessage()
3034
let l:response = CopilotAPIRequest(l:message)
3135

3236
" Append the parsed response to the buffer
33-
call append(line('$'), 'Copilot: ' . l:response)
37+
call append(line('$'), split(l:response, "\n"))
3438

3539
" Move cursor to the end of the buffer
3640
normal! G
@@ -39,14 +43,6 @@ endfunction
3943
function! GetBearerToken()
4044
" Replace with actual token setup and device registry logic
4145
let l:token_url = 'https://github.com/login/device/code'
42-
" headers = {
43-
" 'accept': 'application/json',
44-
" 'editor-version': 'Neovim/0.6.1',
45-
" 'editor-plugin-version': 'copilot.vim/1.16.0',
46-
" 'content-type': 'application/json',
47-
" 'user-agent': 'GithubCopilot/1.155.0',
48-
" 'accept-encoding': 'gzip,deflate,br'
49-
" }
5046

5147
let l:token_headers = [
5248
\ 'Accept: application/json',
@@ -70,8 +66,6 @@ function! GetBearerToken()
7066

7167
" Execute the curl command
7268
let l:response = system(l:curl_cmd)
73-
echom 'Response: ' . l:response
74-
exit(1)
7569

7670
" Check for errors in the response
7771
if v:shell_error != 0
@@ -86,10 +80,8 @@ function! GetBearerToken()
8680
let l:verification_uri = l:json_response.verification_uri
8781

8882
" Display the user code and verification URI to the user
89-
echom 'Please visit ' . l:verification_uri . ' and enter the code: ' . l:user_code
90-
91-
" Wait for the user to complete the device verification
92-
sleep 30
83+
echo 'Please visit ' . l:verification_uri . ' and enter the code: ' . l:user_code
84+
call input("Press Enter to continue...\n")
9385

9486
" Poll the token endpoint to get the Bearer token
9587
let l:token_poll_url = 'https://github.com/login/oauth/access_token'
@@ -118,18 +110,18 @@ function! GetBearerToken()
118110
" Parse the response to get the Bearer token
119111
let l:json_response = json_decode(l:response)
120112
let l:bearer_token = l:json_response.access_token
121-
let $COPILOT_BEARER_TOKEN = l:bearer_token
113+
call writefile([l:bearer_token], s:device_token_file)
122114

123115
" Return the Bearer token
124116
return l:bearer_token
125117
endfunction
126118

127-
function! GetChatToken()
119+
function! GetChatToken(bearer_token)
128120
let l:token_url = 'https://api.github.com/copilot_internal/v2/token'
129121
let l:token_headers = [
130122
\ 'Content-Type: application/json',
131123
\ 'Editor-Version: vscode/1.80.1',
132-
\ 'Authorization: token ' . $COPILOT_BEARER_TOKEN,
124+
\ 'Authorization: token ' . a:bearer_token,
133125
\ ]
134126
let l:token_data = json_encode({
135127
\ 'client_id': 'Iv1.b507a08c87ecfe98',
@@ -164,13 +156,16 @@ endfunction
164156
function! CopilotAPIRequest(message)
165157
"CheckDeviceToken()
166158
let l:url = 'https://api.githubcopilot.com/chat/completions'
167-
if exists('$COPILOT_BEARER_TOKEN')
168-
let l:bearer_token = $COPILOT_BEARER_TOKEN
159+
160+
if filereadable(s:device_token_file)
161+
echo "READING DEVICE TOKEN"
162+
let l:bearer_token = join(readfile(s:device_token_file), "\n")
163+
echo l:bearer_token
169164
else
170165
let l:bearer_token = GetBearerToken()
171166
endif
172167

173-
let l:chat_token = GetChatToken()
168+
let l:chat_token = GetChatToken(l:bearer_token)
174169
let l:headers = [
175170
\ 'Content-Type: application/json',
176171
\ 'Authorization: Bearer ' . l:chat_token,
@@ -218,7 +213,7 @@ function! CopilotAPIRequest(message)
218213
try
219214
let l:result .= l:json_completion.choices[0].delta.content
220215
catch
221-
let l:result .= '\\n'
216+
let l:result .= "\n"
222217
endtry
223218
endif
224219
endfor

0 commit comments

Comments
 (0)