feat(ollama): enforce JSON logits & add nvim_buffer edit action

- Added format parameter to Ollama generate for JSON logit enforcement.

- Implemented safe edit action in nvim_buffer MCP tool to replace raw Lua.
This commit is contained in:
Riz Ashraf committed 2026-10-08 23:41:57 +01:00
1 parent 003b3cb2bf
commit 410b0c42ca
6 files changed
+56 -8

No files matched your search

+46 -4
View File
@@ -1088,16 +1088,17 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
"properties": {
"action": {
"type": "string",
"enum": ["get_active", "read", "open", "create_scratch", "save", "reload", "close", "list", "search"],
"enum": ["get_active", "read", "open", "create_scratch", "save", "reload", "close", "list", "search", "edit"],
"description": "Action to perform on the buffer"
},
"file": { "type": "string", "description": "Target file path (for open, read, search)" },
"file": { "type": "string", "description": "Target file path (for open, read, search, edit)" },
"buf_id": { "type": "integer", "description": "Buffer ID (for close, reload, or split)" },
"content": { "type": "string", "description": "Initial text content (for create_scratch)" },
"name": { "type": "string", "description": "Buffer display name (for create_scratch)" },
"filetype": { "type": "string", "description": "Filetype syntax (for open, create_scratch)" },
"start_line": { "type": "integer", "description": "1-indexed start line (for read)" },
"end_line": { "type": "integer", "description": "1-indexed end line (for read)" },
"start_line": { "type": "integer", "description": "1-indexed start line (for read, edit)" },
"end_line": { "type": "integer", "description": "1-indexed end line (for read, edit)" },
"replacement_content": { "type": "string", "description": "New content for the specified line range (for edit)" },
"pattern": { "type": "string", "description": "Regex or substring pattern to search for (for search)" },
"force": { "type": "boolean", "description": "Force reload or close (for reload, close)" }
},
@@ -1324,6 +1325,47 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
execute_nvim_lua_with_args(code, vec![args_rmp])
)
}
"edit" => {
let args_rmp = json_to_rmpv(args);
let code = "
local args = ...
local file = args.file
if not file or file == '' or file == vim.NIL then
error(\"Missing 'file'\")
end
local start_line = tonumber(args.start_line)
if not start_line then
error(\"Missing 'start_line'\")
end
local end_line = tonumber(args.end_line)
if not end_line then
error(\"Missing 'end_line'\")
end
local content = args.replacement_content
if not content or content == vim.NIL then
error(\"Missing 'replacement_content'\")
end
local bufnr = vim.fn.bufnr(file, true)
if vim.fn.bufloaded(bufnr) == 0 then
vim.fn.bufload(bufnr)
end
local lines = vim.split(content, '\\n', { plain = true })
vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, lines)
local win = vim.fn.bufwinid(bufnr)
if win ~= -1 then
pcall(vim.api.nvim_win_set_cursor, win, {start_line, 0})
end
return 'Successfully edited lines ' .. tostring(start_line) .. ' to ' .. tostring(end_line) .. ' in ' .. file
";
handle_lua_result!(
id,
execute_nvim_lua_with_args(code, vec![args_rmp])
)
}
"open" => {
let args_rmp = json_to_rmpv(args);
let code = "