-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathvariables.lua
More file actions
111 lines (100 loc) · 3.63 KB
/
Copy pathvariables.lua
File metadata and controls
111 lines (100 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
local M = {}
local mcphub = require("mcphub")
---@param opts MCPHub.Extensions.CodeCompanionConfig
function M.register(opts)
local hub = mcphub.get_hub_instance()
if not hub then
return
end
local resources = hub:get_resources()
local ok, config = pcall(require, "codecompanion.config")
if not ok then
return
end
local cc_editor_context = config.interactions.chat.editor_context
-- Remove existing MCP editor context entries
for key, _ in pairs(cc_editor_context) do
if type(key) == "string" and key:sub(1, 4) == "mcp:" then
cc_editor_context[key] = nil
end
end
local added_resources = {}
-- Add current resources as editor context
for _, resource in ipairs(resources) do
local server_name = resource.server_name
local uri = resource.uri
local resource_name = resource.name or uri
local description = resource.description or ""
description = description:gsub("\n", " ")
description = resource_name .. " (" .. description .. ")"
local var_id = "mcp:" .. uri
cc_editor_context[var_id] = {
description = description,
hide_in_help_window = true,
callback = function(self)
-- Sync call - blocks UI (can't use async in editor context yet)
local result = hub:access_resource(server_name, uri, {
caller = {
type = "codecompanion",
codecompanion = self,
meta = {
is_within_variable = true,
},
},
parse_response = true,
})
if not result then
return string.format("Accessing resource failed: %s", uri)
end
-- Handle images
if result.images and #result.images > 0 then
for _, image in ipairs(result.images) do
local id = string.format("mcp-%s", os.time())
self.Chat:add_image_message({
id = id,
base64 = image.data,
mimetype = image.mimeType,
})
end
end
return result.text
end,
}
table.insert(added_resources, var_id)
end
-- Update syntax highlighting for editor context
M.update_variable_syntax(added_resources)
end
-- Setup MCP resources as CodeCompanion variables
---@param opts MCPHub.Extensions.CodeCompanionConfig
function M.setup(opts)
if not opts.make_vars then
return
end
vim.schedule(function()
M.register(opts)
end)
mcphub.on(
{ "servers_updated", "resource_list_changed" },
vim.schedule_wrap(function()
M.register(opts)
end)
)
end
--- Update syntax highlighting for variables
---@param resources string[]
function M.update_variable_syntax(resources)
vim.schedule(function()
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(bufnr) and vim.bo[bufnr].filetype == "codecompanion" then
vim.api.nvim_buf_call(bufnr, function()
for _, resource in ipairs(resources) do
vim.cmd.syntax('match CodeCompanionChatVariable "#{' .. resource .. '}"')
vim.cmd.syntax('match CodeCompanionChatVariable "#{' .. resource .. '}{[^}]*}"')
end
end)
end
end
end)
end
return M