|
| 1 | +from subprocess import PIPE, Popen |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import pylspclient |
| 5 | +from jupyter_ai import BaseProvider, TextField |
| 6 | +from jupyter_ai import __version__ as jupyter_ai_version |
| 7 | +from jupyter_ai_magics.models.completion import ( |
| 8 | + InlineCompletionList, |
| 9 | + InlineCompletionReply, |
| 10 | + InlineCompletionRequest, |
| 11 | +) |
| 12 | +from jupyterlab import __version__ as jupyterlab_version |
| 13 | + |
| 14 | +INIT_PARAMS = { |
| 15 | + "capabilities": {"workspace": {"workspaceFolders": False}}, |
| 16 | + "initializationOptions": { |
| 17 | + "editorInfo": {"name": "JupyterLab", "version": jupyterlab_version}, |
| 18 | + "editorPluginInfo": {"name": "jupyter-ai", "version": jupyter_ai_version}, |
| 19 | + }, |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +def calc_position_lineno_and_char(prefix, suffix): |
| 24 | + """ |
| 25 | + Calculate the line number and character position within a text based on a given prefix and suffix text. |
| 26 | + GitHub Copilot LSP requires those positions for completion requests. |
| 27 | + https://www.npmjs.com/package/@github/copilot-language-server#panel-completions |
| 28 | + """ |
| 29 | + |
| 30 | + full_text = prefix + suffix |
| 31 | + |
| 32 | + lineno = full_text.count("\n", 0, len(prefix)) |
| 33 | + prefix_text = "\n".join(full_text.split("\n")[:lineno]) |
| 34 | + char_pos = len(prefix) - len(prefix_text) - 1 |
| 35 | + |
| 36 | + return lineno, char_pos |
| 37 | + |
| 38 | + |
| 39 | +class GitHubCopilotLLM: |
| 40 | + process: Optional[Popen] = None |
| 41 | + |
| 42 | + def __init__(self, lsp_bin_path: str): |
| 43 | + self.lsp_bin_path = lsp_bin_path |
| 44 | + self.ensure_lsp_server_initialized() |
| 45 | + |
| 46 | + def _initialize(self): |
| 47 | + self.lsp_endpoint.call_method(method_name="initialize", **INIT_PARAMS) |
| 48 | + self.lsp_endpoint.send_notification(method_name="initialized") |
| 49 | + |
| 50 | + def _signin(self): |
| 51 | + self.ensure_lsp_server_initialized() |
| 52 | + res = self.lsp_endpoint.call_method( |
| 53 | + method_name="signIn", |
| 54 | + ) |
| 55 | + return res |
| 56 | + |
| 57 | + def _signout(self): |
| 58 | + self.ensure_lsp_server_initialized() |
| 59 | + res = self.lsp_endpoint.call_method( |
| 60 | + method_name="signOut", |
| 61 | + ) |
| 62 | + return res |
| 63 | + |
| 64 | + def _completion(self, code, pos_line, pos_char): |
| 65 | + self.ensure_lsp_server_initialized() |
| 66 | + self.lsp_endpoint.send_notification( |
| 67 | + method_name="textDocument/didOpen", |
| 68 | + **{ |
| 69 | + "textDocument": { |
| 70 | + "uri": "file:///dummy", |
| 71 | + "version": 0, |
| 72 | + "languageId": "python", |
| 73 | + "text": code, |
| 74 | + } |
| 75 | + }, |
| 76 | + ) |
| 77 | + |
| 78 | + res = self.lsp_endpoint.call_method( |
| 79 | + method_name="textDocument/copilotPanelCompletion", |
| 80 | + **{ |
| 81 | + "textDocument": { |
| 82 | + "uri": "file:///dummy", |
| 83 | + "version": 0, |
| 84 | + }, |
| 85 | + "position": { |
| 86 | + "line": pos_line, |
| 87 | + "character": pos_char, |
| 88 | + }, |
| 89 | + }, |
| 90 | + ) |
| 91 | + return res |
| 92 | + |
| 93 | + def _start_lsp_server(self): |
| 94 | + if not self.is_lsp_server_running: |
| 95 | + self.process = Popen( |
| 96 | + [self.lsp_bin_path, "--stdio"], stdin=PIPE, stdout=PIPE, stderr=PIPE |
| 97 | + ) |
| 98 | + self.json_rpc_endpoint = pylspclient.JsonRpcEndpoint( |
| 99 | + self.process.stdin, self.process.stdout |
| 100 | + ) |
| 101 | + self.lsp_endpoint = pylspclient.LspEndpoint( |
| 102 | + self.json_rpc_endpoint, timeout=15 |
| 103 | + ) |
| 104 | + self.lsp_endpoint.start() |
| 105 | + |
| 106 | + def _stop_lsp_server(self): |
| 107 | + self.lsp_endpoint.stop() |
| 108 | + self.process.kill() |
| 109 | + |
| 110 | + def ensure_lsp_server_initialized(self): |
| 111 | + if not self.is_lsp_server_running: |
| 112 | + self._start_lsp_server() |
| 113 | + self._initialize() |
| 114 | + |
| 115 | + @property |
| 116 | + def is_lsp_server_running(self): |
| 117 | + return self.process is not None and self.process.poll() is None |
| 118 | + |
| 119 | + @property |
| 120 | + def _llm_type(self) -> str: |
| 121 | + return "github-copilot" |
| 122 | + |
| 123 | + |
| 124 | +class GitHubCopilotProvider(BaseProvider): |
| 125 | + id = "github-copilot" |
| 126 | + name = "GitHub Copilot" |
| 127 | + models = ["*"] |
| 128 | + model_id_key = "model" |
| 129 | + pypi_package_deps = ["pylspclient"] |
| 130 | + help = ( |
| 131 | + "Make sure you've installed copilot-language-server [https://www.npmjs.com/package/@github/copilot-language-server](https://www.npmjs.com/package/@github/copilot-language-server) . " |
| 132 | + "Set this absolute path to `lsp_bin_path`." |
| 133 | + ) |
| 134 | + fields = [ |
| 135 | + TextField( |
| 136 | + key="lsp_bin_path", label="Copilot LSP binary absolute path", format="text" |
| 137 | + ), |
| 138 | + ] |
| 139 | + |
| 140 | + def __init__( |
| 141 | + self, |
| 142 | + **kwargs, |
| 143 | + ): |
| 144 | + super().__init__(**kwargs) |
| 145 | + self._llm = GitHubCopilotLLM(lsp_bin_path=self.lsp_bin_path) |
| 146 | + |
| 147 | + async def generate_inline_completions(self, request: InlineCompletionRequest): |
| 148 | + self._llm.ensure_lsp_server_initialized() |
| 149 | + |
| 150 | + full_text = request.prefix + request.suffix |
| 151 | + lineno, char = calc_position_lineno_and_char(request.prefix, request.suffix) |
| 152 | + suggestions = self._llm._completion(full_text, lineno, char) |
| 153 | + completions = [ |
| 154 | + { |
| 155 | + "insertText": item["insertText"][char:], |
| 156 | + } |
| 157 | + for item in suggestions["items"] |
| 158 | + ] |
| 159 | + return InlineCompletionReply( |
| 160 | + list=InlineCompletionList(items=completions), |
| 161 | + reply_to=request.number, |
| 162 | + ) |
0 commit comments