Skip to content

Commit

Permalink
improve copilot
Browse files Browse the repository at this point in the history
1. add node version check
2. add status check
3. improve npm package lookup
  • Loading branch information
kongds committed Jul 28, 2023
1 parent d97f2b1 commit 931ac17
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
12 changes: 1 addition & 11 deletions acm/acm-backend-copilot.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
:group 'acm-backend-copilot)

(defcustom acm-backend-copilot-node-path "node"
"The path to store Codeium API Key."
"The path to node for copilot."
:type 'string
:group 'acm-backend-copilot)

Expand Down Expand Up @@ -53,19 +53,9 @@ in the proxy plist. For example:

(defvar-local acm-backend-copilot-items nil)

(defun acm-backend-copilot-check-node-version ()
(and (locate-file acm-backend-copilot-node-path exec-path)
;; following copilot.el to check node version only >= 16
(>= (->> (with-output-to-string
(call-process acm-backend-copilot-node-path nil standard-output nil "--version"))
(s-trim)
(s-chop-prefix "v")
(string-to-number)) 16)))

(defun acm-backend-copilot-candidates (keyword)
acm-backend-copilot-items)


(defun acm-backend-copilot-candidate-expand (candidate-info bound-start &optional preview)
;; We need replace whole area with copilot label.
(let ((end-position (line-end-position)))
Expand Down
34 changes: 30 additions & 4 deletions core/copilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
class Copilot:
def __init__(self):
self.is_run = False
self.is_initialized = False
self.is_get_info = False

(self.node_path, ) = get_emacs_vars(["acm-backend-copilot-node-path"])

npm_prefix = subprocess.check_output(['npm', 'config', 'get', 'prefix'], universal_newlines=True).strip()
self.agent_path = os.path.join(f'{npm_prefix}/lib/node_modules', "copilot-node-server", "copilot/dist/agent.js")
npm_package_path = subprocess.check_output(['npm', 'root', '-g'], universal_newlines=True).strip()
self.agent_path = os.path.join(npm_package_path, "copilot-node-server", "copilot/dist/agent.js")

self.try_completion_timer = None
self.file_versions = {}
Expand All @@ -46,14 +47,22 @@ def __init__(self):
self.is_get_info = False
self.wait_id = None

def check_node_version(self):
version = subprocess.check_output([self.node_path, '-v'], stderr=subprocess.STDOUT, universal_newlines=True).strip()
major_version = int(version.split('.')[0].lstrip('v'))
return major_version >= 16

def start_copilot(self):
self.get_info()

if self.is_run:
return

self.is_run = True

if not self.check_node_version():
message_emacs('To use copilot, Please install node version >= 16')
return

self.copilot_subprocess = subprocess.Popen([self.node_path, self.agent_path],
stdin=PIPE,
stdout=PIPE,
Expand All @@ -77,6 +86,8 @@ def start_copilot(self):
'networkProxy': epc_arg_transformer(self.proxy)}
self.sender.send_request('setEditorInfo', editor_info, generate_request_id())

self.is_initialized = True

def get_language_id(self, editor_mode):
language_id = editor_mode.replace('-mode', '')
return COPILOT_MAJOR_MODES_MAP[language_id] if language_id in COPILOT_MAJOR_MODES_MAP else language_id
Expand All @@ -90,7 +101,10 @@ def message_dispatcher(self):
message = self.receiver.get_message()
message = message['content']

if 'id' in message and message['id'] == self.wait_id:
if 'method' in message and message['method'] == 'LogMessage':
if message['params']['level'] > 1:
print('Copilot: ', message['params']['message'])
elif 'id' in message and message['id'] == self.wait_id:
self.wait_response = message
self.wait_id = None
elif 'result' in message and 'completions' in message['result']:
Expand Down Expand Up @@ -159,6 +173,9 @@ def complete(self, position, editor_mode, file_path, relative_path, tab_size, t

self.start_copilot()

if not self.is_initialized:
return

if self.try_completion_timer is not None and self.try_completion_timer.is_alive():
self.try_completion_timer.cancel()

Expand Down Expand Up @@ -213,6 +230,15 @@ def get_info(self):

self.is_get_info = True

def check_status(self):
self.start_copilot()
self.wait_id = generate_request_id()
self.sender.send_request('checkStatus', {"dummy": "checkStatus"}, self.wait_id)
while self.wait_id is not None:
time.sleep(0.1)
result = self.wait_response['result']
message_emacs(f'Copilot status: {result["status"]}' + \
f' as user {result["user"]}' if 'user' in result else '')

def login(self):
self.start_copilot()
Expand Down
3 changes: 3 additions & 0 deletions lsp-bridge-lsp-installer.el
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ Only useful on GNU/Linux. Automatically set if NixOS is detected."
(interactive)
(lsp-bridge-call-async "copilot_logout"))

(defun lsp-bridge-copilot-status ()
(interactive)
(lsp-bridge-call-async "copilot_status"))

(provide 'lsp-bridge-lsp-installer)

Expand Down
3 changes: 3 additions & 0 deletions lsp_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ def copilot_login(self):
def copilot_logout(self):
self.copilot.logout()

def copilot_status(self):
self.copilot.check_status()

def copilot_completion_accept(self, id):
self.copilot.accept(id)

Expand Down

0 comments on commit 931ac17

Please sign in to comment.