Skip to content

Commit 8122575

Browse files
committed
refactor(mappings): move callback functions to mapping config
Restructures the codebase by moving mapping-related callback functions from init.lua to config/mappings.lua. This improves code organization and makes mapping configuration more self-contained. - Adds callback field to mapping config class - Moves all mapping callback functions to their respective mapping configs - Extracts shared functions to utils module - Simplifies mapping registration by using callback field Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent b634f04 commit 8122575

5 files changed

Lines changed: 610 additions & 640 deletions

File tree

README.md

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -360,63 +360,49 @@ You can define custom providers by adding them to `providers` config. Provider h
360360
- `get_models?(headers: table): table` - Optional function that returns list of available models
361361
- `get_agents?(headers: table): table` - Optional function that returns list of available agents
362362

363-
Example custom provider:
363+
Here is how you implement [ollama](https://ollama.com/) provider for example:
364364

365365
```lua
366366
{
367367
providers = {
368-
my_provider = {
369-
-- Required fields
370-
get_token = function()
371-
return "my-token", os.time() + 3600 -- Token valid for 1 hour
372-
end,
373-
get_headers = function(token, sessionid, machineid)
368+
ollama = {
369+
get_headers = function()
374370
return {
375-
["authorization"] = "Bearer " .. token,
376-
["content-type"] = "application/json",
371+
['Content-Type'] = 'application/json',
377372
}
378373
end,
379-
get_url = function(opts)
380-
if opts.agent then
381-
return "https://api.custom.com/agents/" .. opts.agent
374+
375+
get_models = function()
376+
local response = cutils.curl_get('http://localhost:11434/api/tags')
377+
378+
if not response or response.status ~= 200 then
379+
error('Failed to fetch models: ' .. tostring(response and response.status))
380+
end
381+
382+
local models = {}
383+
for _, model in ipairs(vim.json.decode(response.body)['models']) do
384+
table.insert(models, {
385+
id = model.name,
386+
name = model.name,
387+
version = "latest",
388+
tokenizer = "o200k_base",
389+
})
382390
end
383-
return "https://api.custom.com/chat"
391+
392+
return models
384393
end,
385-
prepare_input = function(inputs, opts, model)
394+
395+
prepare_input = function(inputs, opts)
386396
return {
387-
messages = inputs,
388-
temperature = opts.temperature,
389397
model = opts.model,
390-
stream = true
398+
messages = inputs,
399+
stream = true,
391400
}
392401
end,
393402

394-
-- Optional fields
395-
disabled = false,
396-
embeddings = "copilot_embeddings", -- Use copilot for embeddings
397-
get_models = function(headers)
398-
-- Return list of available models
399-
return {
400-
{
401-
id = "gpt-4",
402-
name = "GPT-4",
403-
version = "1.0",
404-
tokenizer = "gpt2",
405-
max_prompt_tokens = 8000,
406-
max_output_tokens = 2000,
407-
}
408-
}
403+
get_url = function()
404+
return 'http://localhost:11434/api/chat'
409405
end,
410-
get_agents = function(headers)
411-
-- Return list of available agents
412-
return {
413-
{
414-
id = "agent1",
415-
name = "My Agent",
416-
description = "Custom agent"
417-
}
418-
}
419-
end
420406
}
421407
}
422408
}
@@ -564,47 +550,39 @@ Also see [here](/lua/CopilotChat/config.lua):
564550
separator = '───', -- Separator to use in chat
565551

566552
-- default providers
553+
-- see config/providers.lua for implementation
567554
providers = {
568555
copilot = {
569-
-- see config.lua for implementation
570556
},
571557
github_models = {
572-
-- see config.lua for implementation
573558
},
574559
copilot_embeddings = {
575-
-- see config.lua for implementation
576560
},
577561
}
578562

579563
-- default contexts
564+
-- see config/contexts.lua for implementation
580565
contexts = {
581566
buffer = {
582-
-- see config.lua for implementation
583567
},
584568
buffers = {
585-
-- see config.lua for implementation
586569
},
587570
file = {
588-
-- see config.lua for implementation
589571
},
590572
files = {
591-
-- see config.lua for implementation
592573
},
593574
git = {
594-
-- see config.lua for implementation
595575
},
596576
url = {
597-
-- see config.lua for implementation
598577
},
599578
register = {
600-
-- see config.lua for implementation
601579
},
602580
quickfix = {
603-
-- see config.lua for implementation
604581
},
605582
},
606583

607584
-- default prompts
585+
-- see config/prompts.lua for implementation
608586
prompts = {
609587
Explain = {
610588
prompt = '> /COPILOT_EXPLAIN\n\nWrite an explanation for the selected code as paragraphs of text.',
@@ -630,6 +608,7 @@ Also see [here](/lua/CopilotChat/config.lua):
630608
},
631609

632610
-- default mappings
611+
-- see config/mappings.lua for implementation
633612
mappings = {
634613
complete = {
635614
insert = '<Tab>',

0 commit comments

Comments
 (0)