Skip to content

Commit f2d90de

Browse files
authored
Chat integration (Exafunction#317)
* Enable chat Starting point for chat integration: enabling the server. * Proper codeium#Chat() function This launches the browser the same way the initial authentication does. * Bugfix no-submit issue I forgot ide_telemetry_enabled=true which is needed for chat. * Automate workspace tracking * Don't search for project root until necessary * Whitespace fix * Whitespace fix * Document how to launch chat * Whitespace cleanup in README
1 parent 9286586 commit f2d90de

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

README.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ A few of the most popular options are highlighted below.
5050

5151
Codeium provides the following functions to control suggestions:
5252

53-
|Action|Function|Default Binding|
54-
|---|---|---|
55-
|Clear current suggestion| `codeium#Clear()` |`<C-]>`|
56-
|Next suggestion| `codeium#CycleCompletions(1)` |`<M-]>`|
57-
|Previous suggestion| `codeium#CycleCompletions(-1)` |`<M-[>`|
58-
|Insert suggestion| `codeium#Accept()` |`<Tab>`|
59-
|Manually trigger suggestion| `codeium#Complete()` |`<M-Bslash>`|
53+
| Action | Function | Default Binding |
54+
| --------------------------- | ------------------------------ | --------------- |
55+
| Clear current suggestion | `codeium#Clear()` | `<C-]>` |
56+
| Next suggestion | `codeium#CycleCompletions(1)` | `<M-]>` |
57+
| Previous suggestion | `codeium#CycleCompletions(-1)` | `<M-[>` |
58+
| Insert suggestion | `codeium#Accept()` | `<Tab>` |
59+
| Manually trigger suggestion | `codeium#Complete()` | `<M-Bslash>` |
6060

6161
Codeium's default keybindings can be disabled by setting
6262

@@ -75,7 +75,6 @@ use the `g:codeium_no_map_tab` option.
7575

7676
If you'd like to bind the actions above to different keys, this might look something like the following in Vim:
7777

78-
7978
```vim
8079
imap <script><silent><nowait><expr> <C-g> codeium#Accept()
8180
imap <C-;> <Cmd>call codeium#CycleCompletions(1)<CR>
@@ -101,7 +100,6 @@ use {
101100

102101
(Make sure that you ran `:Codeium Auth` after installation.)
103102

104-
105103
### ⛔ Disabling Codeium
106104

107105
Codeium can be disabled for particular filetypes by setting the
@@ -141,6 +139,7 @@ let g:codeium_manual = v:true
141139
Codeium status can be generated by calling the `codeium#GetStatusString()` function. In
142140
Neovim, you can use `vim.api.nvim_call_function("codeium#GetStatusString", {})` instead.
143141
It produces a 3 char long string with Codeium status:
142+
144143
- `'3/8'` - third suggestion out of 8
145144
- `'0'` - Codeium returned no suggestions
146145
- `'*'` - waiting for Codeium response
@@ -160,6 +159,16 @@ Please check `:help statusline` for further information about building statuslin
160159

161160
vim-airline supports Codeium out-of-the-box since commit [3854429d](https://github.com/vim-airline/vim-airline/commit/3854429d99c8a2fb555a9837b155f33c957a2202).
162161

162+
### Launching Codeium Chat
163+
164+
Calling the `codeium#Chat()` function will enable search and indexing in the current project and launch Codeium Chat in a new browser window.
165+
166+
The project root is determined by looking in Vim's current working directory for some specific files or directories to be present and goes up to parent directories until one is found. This list of hints is user-configurable and the default value is:
167+
168+
```let g:codeium_workspace_root_hints = ['.bzr','.git','.hg','.svn','_FOSSIL_','package.json']```
169+
170+
Note that launching chat enables telemetry.
171+
163172
## 💾 Installation Options
164173

165174
### 💤 Lazy

autoload/codeium.vim

+71
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,77 @@ function! codeium#CycleOrComplete() abort
390390
endif
391391
endfunction
392392

393+
function! s:LaunchChat(out, err, status) abort
394+
let l:metadata = codeium#server#RequestMetadata()
395+
let l:processes = json_decode(join(a:out, ''))
396+
let l:chat_port = l:processes['chatClientPort']
397+
let l:ws_port = l:processes['chatWebServerPort']
398+
" Hard-coded to English locale and allowed telemetry.
399+
" Not touching has_enterprise_extension
400+
let l:url = 'http://127.0.0.1:' . l:chat_port . '/?' . 'api_key=' . l:metadata.api_key . '&ide_name=' . l:metadata.ide_name . '&ide_version=' . l:metadata.ide_version . '&extension_name=' . l:metadata.extension_name . '&extension_version=' . l:metadata.extension_version . '&web_server_url=ws://127.0.0.1:' . l:ws_port . '&app_name=Vim&locale=en&ide_telemetry_enabled=true&has_index_service=true'
401+
let l:browser = codeium#command#BrowserCommand()
402+
let opened_browser = v:false
403+
if !empty(browser)
404+
echomsg 'Navigating to ' . l:url
405+
try
406+
call system(l:browser . ' ' . '"' . l:url . '"')
407+
if v:shell_error is# 0
408+
let l:opened_browser = v:true
409+
endif
410+
catch
411+
endtry
412+
413+
if !l:opened_browser
414+
echomsg 'Failed to open browser. Please go to the link above.'
415+
endif
416+
else
417+
echomsg 'No available browser found. Please go to ' . l:url
418+
endif
419+
endfunction
420+
421+
let g:codeium_workspace_root_hints = ['.bzr','.git','.hg','.svn','_FOSSIL_','package.json']
422+
function! s:GetProjectRoot() abort
423+
let l:last_dir = ''
424+
let l:dir = getcwd()
425+
while l:dir != l:last_dir
426+
for l:root_hint in g:codeium_workspace_root_hints
427+
let l:hint = l:dir . '/' . l:root_hint
428+
if isdirectory(l:hint) || filereadable(l:hint)
429+
return l:dir
430+
endif
431+
endfor
432+
let l:last_dir = l:dir
433+
let l:dir = fnamemodify(l:dir, ':h')
434+
endwhile
435+
return getcwd()
436+
endfunction
437+
438+
" This assumes a single workspace is involved per Vim session, for now.
439+
let s:codeium_workspace_indexed = v:false
440+
function! codeium#AddTrackedWorkspace() abort
441+
if (!codeium#Enabled() || s:codeium_workspace_indexed)
442+
return
443+
endif
444+
let s:codeium_workspace_indexed = v:true
445+
try
446+
call codeium#server#Request('AddTrackedWorkspace', {"workspace": s:getProjectRoot()})
447+
catch
448+
call codeium#log#Exception()
449+
endtry
450+
endfunction
451+
452+
function! codeium#Chat() abort
453+
if (!codeium#Enabled())
454+
return
455+
endif
456+
try
457+
call codeium#server#Request('GetProcesses', codeium#server#RequestMetadata(), function('s:LaunchChat', []))
458+
call codeium#AddTrackedWorkspace()
459+
catch
460+
call codeium#log#Exception()
461+
endtry
462+
endfunction
463+
393464
function! codeium#GetStatusString(...) abort
394465
let s:using_codeium_status = 1
395466
if (!codeium#Enabled())

autoload/codeium/server.vim

+3-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ function! s:ActuallyStart() abort
221221
let args = [
222222
\ s:bin,
223223
\ '--api_server_url', get(config, 'api_url', 'https://server.codeium.com'),
224-
\ '--manager_dir', manager_dir
224+
\ '--manager_dir', manager_dir,
225+
\ '--enable_local_search', '--enable_index_service', '--search_max_workspace_file_count', '5000',
226+
\ '--enable_chat_web_server', '--enable_chat_client'
225227
\ ]
226228
if has_key(config, 'api_url') && !empty(config.api_url)
227229
let args += ['--enterprise_mode']

0 commit comments

Comments
 (0)