feat: add the possibility to add initial files for the prompt

This commit is contained in:
2024-12-14 15:28:42 +01:00
parent 63832ae0c3
commit 44a8458b97
3 changed files with 64 additions and 34 deletions

View File

@@ -30,7 +30,6 @@ local function parse_response(raw)
return data
end
-- A simple token estimation function (approx. 4 chars per token)
local function estimate_tokens(text)
local approx_chars_per_token = 4
local length = #text
@@ -43,8 +42,25 @@ local function is_subpath(root, path)
return target_abs:sub(1, #root_abs) == root_abs
end
-- We'll store requested files in a global variable for now, though a more robust solution might be needed.
local requested_files = {}
local function read_file(path)
local fd = vim.loop.fs_open(path, "r", 438)
if not fd then
return nil
end
local stat = vim.loop.fs_fstat(fd)
if not stat then
vim.loop.fs_close(fd)
return nil
end
local data = vim.loop.fs_read(fd, stat.size, 0)
vim.loop.fs_close(fd)
return data
end
local function is_directory(path)
local stat = vim.loop.fs_stat(path)
return stat and stat.type == "directory"
end
function M.run_chatgpt_command()
local conf = config.load()
@@ -60,11 +76,38 @@ function M.run_chatgpt_command()
local dirs = conf.directories or {"."}
local project_structure = context.get_project_structure(dirs)
local initial_files = conf.initial_files or {}
local included_sections = {}
if #initial_files > 0 then
table.insert(included_sections, "\n\nIncluded files and directories (pre-selected):\n")
local root = vim.fn.getcwd()
for _, item in ipairs(initial_files) do
local full_path = root .. "/" .. item
if is_directory(full_path) then
local dir_files = context.get_project_files({item})
for _, f in ipairs(dir_files) do
local path = root .. "/" .. f
local data = read_file(path)
if data then
table.insert(included_sections, "\nFile: `" .. f .. "`\n```\n" .. data .. "\n```\n")
end
end
else
local data = read_file(full_path)
if data then
table.insert(included_sections, "\nFile: `" .. item .. "`\n```\n" .. data .. "\n```\n")
end
end
end
end
local initial_sections = {
conf.initial_prompt .. "\n" .. user_input,
"\n\nProject name: " .. (conf.project_name or "") .. "\n",
"\n\nProject Structure:\n",
project_structure,
table.concat(included_sections, "\n"),
"\n\nPlease respond with a YAML listing which files you need from the project. For example:\n",
"project_name: " .. (conf.project_name or "") .. "\nfiles:\n - path: \"relative/path/to/file\"\n\n"
}
@@ -157,9 +200,7 @@ function M.run_chatgpt_paste_command()
return
else
-- Not final: the model is requesting these files. We must gather and send them.
local dirs = conf.directories or {"."}
local all_files = context.get_project_files(dirs)
local requested_paths = {}
for _, fileinfo in ipairs(data.files) do
if fileinfo.path then
@@ -171,19 +212,10 @@ function M.run_chatgpt_paste_command()
local root = vim.fn.getcwd()
for _, f in ipairs(requested_paths) do
local full_path = root .. "/" .. f
local fd = vim.loop.fs_open(full_path, "r", 438)
if fd then
local stat = vim.loop.fs_fstat(fd)
if stat then
local content = vim.loop.fs_read(fd, stat.size, 0)
vim.loop.fs_close(fd)
if content then
table.insert(file_sections, "\nFile: `" .. f .. "`\n```\n" .. content .. "\n```\n")
end
else
vim.loop.fs_close(fd)
end
local path = root .. "/" .. f
local content = read_file(path)
if content then
table.insert(file_sections, "\nFile: `" .. f .. "`\n```\n" .. content .. "\n```\n")
else
table.insert(file_sections, "\nFile: `" .. f .. "`\n```\n(Could not read file)\n```\n")
end