forked from coder/claudecode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_diff.lua
More file actions
95 lines (87 loc) · 2.81 KB
/
open_diff.lua
File metadata and controls
95 lines (87 loc) · 2.81 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
--- Tool implementation for opening a diff view.
local schema = {
description = "Open a diff view comparing old file content with new file content",
inputSchema = {
type = "object",
properties = {
old_file_path = {
type = "string",
description = "Path to the old file to compare",
},
new_file_path = {
type = "string",
description = "Path to the new file to compare",
},
new_file_contents = {
type = "string",
description = "Contents for the new file version",
},
tab_name = {
type = "string",
description = "Name for the diff tab/view",
},
},
required = { "old_file_path", "new_file_path", "new_file_contents", "tab_name" },
additionalProperties = false,
["$schema"] = "http://json-schema.org/draft-07/schema#",
},
}
---Handles the openDiff tool invocation with MCP compliance.
---Opens a diff view and blocks until user interaction (save/close).
---Returns MCP-compliant response with content array format.
---@param params table The input parameters for the tool
---@return table response MCP-compliant response with content array
local function handler(params)
-- Validate required parameters
local required_params = { "old_file_path", "new_file_path", "new_file_contents", "tab_name" }
for _, param_name in ipairs(required_params) do
if not params[param_name] then
error({
code = -32602, -- Invalid params
message = "Invalid params",
data = "Missing required parameter: " .. param_name,
})
end
end
-- Ensure we're running in a coroutine context for blocking operation
local co, is_main = coroutine.running()
if not co or is_main then
error({
code = -32000,
message = "Internal server error",
data = "openDiff must run in coroutine context",
})
end
local diff_module_ok, diff_module = pcall(require, "opencode.diff")
if not diff_module_ok then
error({ code = -32000, message = "Internal server error", data = "Failed to load diff module" })
end
-- Use the new blocking diff operation
local success, result = pcall(
diff_module.open_diff_blocking,
params.old_file_path,
params.new_file_path,
params.new_file_contents,
params.tab_name
)
if not success then
-- Check if this is already a structured error
if type(result) == "table" and result.code then
error(result)
else
error({
code = -32000, -- Generic tool error
message = "Error opening blocking diff",
data = tostring(result),
})
end
end
-- result should already be MCP-compliant with content array format
return result
end
return {
name = "openDiff",
schema = schema,
handler = handler,
requires_coroutine = true, -- This tool needs coroutine context for blocking behavior
}