Skip to content

Commit 15b28ef

Browse files
authored
Feature: add Tabby support (#24)
* feat: add tabby backend * docs: add Tabby docs
1 parent f1fd188 commit 15b28ef

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,36 @@ cmp_ai:setup({
169169
})
170170
```
171171

172+
To use Tabby:
173+
174+
```lua
175+
local cmp_ai = require('cmp_ai.config')
176+
177+
cmp_ai:setup({
178+
max_lines = 1000,
179+
provider = 'Tabby',
180+
notify = true,
181+
provider_options = {
182+
-- These are optional
183+
-- user = 'yourusername',
184+
-- temperature = 0.2,
185+
-- seed = 'randomstring',
186+
},
187+
notify_callback = function(msg)
188+
vim.notify(msg)
189+
end,
190+
run_on_every_keystroke = true,
191+
ignored_file_types = {
192+
-- default is not to ignore
193+
-- uncomment to ignore in lua:
194+
-- lua = true
195+
},
196+
})
197+
```
198+
199+
You will also need to make sure you have the Tabby api key in your environment, `TABBY_API_KEY`.
200+
201+
172202
### `notify`
173203

174204
As some completion sources can be quit slow, setting this to `true` will trigger

doc/cmp-ai.txt

+31
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,37 @@ To use Ollama <https://ollama.ai>:
189189
})
190190
<
191191

192+
To use Tabby:
193+
194+
>lua
195+
local cmp_ai = require('cmp_ai.config')
196+
197+
cmp_ai:setup({
198+
max_lines = 1000,
199+
provider = 'Tabby',
200+
provider_options = {
201+
-- These are optional
202+
-- user = 'yourusername',
203+
-- temperature = 0.2,
204+
-- seed = 'randomstring',
205+
},
206+
notify = true,
207+
notify_callback = function(msg)
208+
vim.notify(msg)
209+
end,
210+
run_on_every_keystroke = true,
211+
ignored_file_types = {
212+
-- default is not to ignore
213+
-- uncomment to ignore in lua:
214+
-- lua = true
215+
},
216+
})
217+
<
218+
219+
220+
You will also need to make sure you have the Tabby api key in
221+
your environment, `TABBY_API_KEY`.
222+
192223

193224
NOTIFY ~
194225

lua/cmp_ai/backends/tabby.lua

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
local requests = require('cmp_ai.requests')
2+
3+
Tabby = requests:new(nil)
4+
5+
function Tabby:new(o)
6+
o = o or {}
7+
setmetatable(o, self)
8+
self.__index = self
9+
self.params = vim.tbl_deep_extend('keep', o or {}, {
10+
base_url = 'http://127.0.0.1:8080/v1/completions',
11+
options = {},
12+
})
13+
14+
self.api_key = os.getenv('TABBY_API_KEY')
15+
16+
return o
17+
end
18+
19+
function Tabby:complete(lines_before, lines_after, cb)
20+
if not self.api_key then
21+
vim.schedule(function()
22+
vim.notify('TABBY_API_KEY environment variable not set', vim.log.levels.ERROR)
23+
end)
24+
return
25+
end
26+
27+
local headers = {
28+
'Authorization: Bearer ' .. self.api_key,
29+
}
30+
31+
local data = {
32+
language = vim.bo.filetype,
33+
segments = {
34+
prefix = lines_before,
35+
suffix = lines_after,
36+
filepath = vim.api.nvim_buf_get_name(0),
37+
},
38+
user = self.params.options.user,
39+
temperature = self.params.options.temperature,
40+
seed = self.params.options.seed,
41+
}
42+
43+
self:Get(self.params.base_url, headers, data, function(answer)
44+
local new_data = {}
45+
if answer.error ~= nil then
46+
vim.notify('Tabby error: ' .. answer.error)
47+
return
48+
end
49+
if answer.choices ~= nil and answer.choices[1] ~= nil then
50+
table.insert(new_data, answer.choices[1].text)
51+
end
52+
cb(new_data)
53+
end)
54+
end
55+
56+
function Tabby:test()
57+
self:complete('def factorial(n)\n if', ' return ans\n', function(data)
58+
dump(data)
59+
end)
60+
end
61+
62+
return Tabby

0 commit comments

Comments
 (0)