Skip to content

Commit

Permalink
Chat integration (#317)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vphantom committed Feb 27, 2024
1 parent 9286586 commit f2d90de
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ A few of the most popular options are highlighted below.

Codeium provides the following functions to control suggestions:

|Action|Function|Default Binding|
|---|---|---|
|Clear current suggestion| `codeium#Clear()` |`<C-]>`|
|Next suggestion| `codeium#CycleCompletions(1)` |`<M-]>`|
|Previous suggestion| `codeium#CycleCompletions(-1)` |`<M-[>`|
|Insert suggestion| `codeium#Accept()` |`<Tab>`|
|Manually trigger suggestion| `codeium#Complete()` |`<M-Bslash>`|
| Action | Function | Default Binding |
| --------------------------- | ------------------------------ | --------------- |
| Clear current suggestion | `codeium#Clear()` | `<C-]>` |
| Next suggestion | `codeium#CycleCompletions(1)` | `<M-]>` |
| Previous suggestion | `codeium#CycleCompletions(-1)` | `<M-[>` |
| Insert suggestion | `codeium#Accept()` | `<Tab>` |
| Manually trigger suggestion | `codeium#Complete()` | `<M-Bslash>` |

Codeium's default keybindings can be disabled by setting

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

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


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

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


### ⛔ Disabling Codeium

Codeium can be disabled for particular filetypes by setting the
Expand Down Expand Up @@ -141,6 +139,7 @@ let g:codeium_manual = v:true
Codeium status can be generated by calling the `codeium#GetStatusString()` function. In
Neovim, you can use `vim.api.nvim_call_function("codeium#GetStatusString", {})` instead.
It produces a 3 char long string with Codeium status:

- `'3/8'` - third suggestion out of 8
- `'0'` - Codeium returned no suggestions
- `'*'` - waiting for Codeium response
Expand All @@ -160,6 +159,16 @@ Please check `:help statusline` for further information about building statuslin

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

### Launching Codeium Chat

Calling the `codeium#Chat()` function will enable search and indexing in the current project and launch Codeium Chat in a new browser window.

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:

```let g:codeium_workspace_root_hints = ['.bzr','.git','.hg','.svn','_FOSSIL_','package.json']```

Note that launching chat enables telemetry.

## 💾 Installation Options

### 💤 Lazy
Expand Down
71 changes: 71 additions & 0 deletions autoload/codeium.vim
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,77 @@ function! codeium#CycleOrComplete() abort
endif
endfunction

function! s:LaunchChat(out, err, status) abort
let l:metadata = codeium#server#RequestMetadata()
let l:processes = json_decode(join(a:out, ''))
let l:chat_port = l:processes['chatClientPort']
let l:ws_port = l:processes['chatWebServerPort']
" Hard-coded to English locale and allowed telemetry.
" Not touching has_enterprise_extension
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'
let l:browser = codeium#command#BrowserCommand()
let opened_browser = v:false
if !empty(browser)
echomsg 'Navigating to ' . l:url
try
call system(l:browser . ' ' . '"' . l:url . '"')
if v:shell_error is# 0
let l:opened_browser = v:true
endif
catch
endtry

if !l:opened_browser
echomsg 'Failed to open browser. Please go to the link above.'
endif
else
echomsg 'No available browser found. Please go to ' . l:url
endif
endfunction

let g:codeium_workspace_root_hints = ['.bzr','.git','.hg','.svn','_FOSSIL_','package.json']
function! s:GetProjectRoot() abort
let l:last_dir = ''
let l:dir = getcwd()
while l:dir != l:last_dir
for l:root_hint in g:codeium_workspace_root_hints
let l:hint = l:dir . '/' . l:root_hint
if isdirectory(l:hint) || filereadable(l:hint)
return l:dir
endif
endfor
let l:last_dir = l:dir
let l:dir = fnamemodify(l:dir, ':h')
endwhile
return getcwd()
endfunction

" This assumes a single workspace is involved per Vim session, for now.
let s:codeium_workspace_indexed = v:false
function! codeium#AddTrackedWorkspace() abort
if (!codeium#Enabled() || s:codeium_workspace_indexed)
return
endif
let s:codeium_workspace_indexed = v:true
try
call codeium#server#Request('AddTrackedWorkspace', {"workspace": s:getProjectRoot()})

This comment has been minimized.

Copy link
@Blowa

Blowa Mar 7, 2024

@vphantom
This is a typo right?
I have to replace getProjectRoot by GetProjectRoot for it to get my project path correctly.

This comment has been minimized.

Copy link
@vphantom

vphantom Mar 7, 2024

Author Contributor

Huh, there is a case difference indeed! I wonder why that still worked on my version of Vim. Since the PR has already been merged I'm not too sure if I should be fixing it here or in a new PR though. 🤔

catch
call codeium#log#Exception()
endtry
endfunction

function! codeium#Chat() abort
if (!codeium#Enabled())
return
endif
try
call codeium#server#Request('GetProcesses', codeium#server#RequestMetadata(), function('s:LaunchChat', []))
call codeium#AddTrackedWorkspace()
catch
call codeium#log#Exception()
endtry
endfunction

function! codeium#GetStatusString(...) abort
let s:using_codeium_status = 1
if (!codeium#Enabled())
Expand Down
4 changes: 3 additions & 1 deletion autoload/codeium/server.vim
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ function! s:ActuallyStart() abort
let args = [
\ s:bin,
\ '--api_server_url', get(config, 'api_url', 'https://server.codeium.com'),
\ '--manager_dir', manager_dir
\ '--manager_dir', manager_dir,
\ '--enable_local_search', '--enable_index_service', '--search_max_workspace_file_count', '5000',
\ '--enable_chat_web_server', '--enable_chat_client'
\ ]
if has_key(config, 'api_url') && !empty(config.api_url)
let args += ['--enterprise_mode']
Expand Down

0 comments on commit f2d90de

Please sign in to comment.