function prep_prompt_file(input, options)
options = options or {}
local default_name = options.default_name or "_default_prompt"
local placeholder_suffix = options.placeholder_suffix
local initial_content = options.initial_content
local add_separator = options.add_separator ~= nil and options.add_separator or false
if input == nil then
input = default_name
end
local path = nil
if type(input) == "string" then
path = input:gsub("/+$", "")
path = utils.text.ensure(input, {prefix = "./", suffix = "/prompt.md"})
else
path = input.path
end
local first_time = utils.path.exists(path) ~= true
if placeholder_suffix ~= nil then
local placeholder_content = "placeholder - " .. placeholder_suffix
if add_separator then
placeholder_content = placeholder_content .. " \n\n====\n\n"
end
initial_content = placeholder_content
else
if initial_content == nil then
intial_content = ""
end
end
utils.file.ensure_exists(path, initial_content, {content_when_empty = true})
if first_time then
ok, err = pcall(utils.cmd.exec,"code", {path})
end
return utils.file.load(path)
end
function should_skip(inst, content)
inst = inst and utils.text.trim(inst) or ""
content = content and utils.text.trim(content) or ""
if inst == "" and content == "" then
return devai.skip("Empty content and instructions - Start writing, and do a Redo.")
end
local first_part = (inst ~= "" and inst) or content
if first_part:sub(1, 11):lower() == "placeholder" then
return devai.skip("Content is a placeholder, so skipping for now")
end
return nil
end
function prep_inst_and_content(content, separator, options)
local content_is_default = options and options.content_is_default or false
local first_part, second_part = utils.text.split_first(content, separator)
local inst, content = nil, nil
if second_part ~= nil then
inst = first_part
content = second_part
elseif content_is_default then
content = first_part
else
inst = first_part
end
return inst, content
end
function load_file_refs(base_dir, refs)
local files = nil
if refs ~= nil then
files = {}
for _, file_ref in ipairs(refs) do
local file = utils.file.load(file_ref.path, {base_dir = base_dir})
file.comment_file_path = utils.code.comment_line(file.ext, "file: " .. file.path)
table.insert(files, file)
end
end
return files
end
function shallow_copy(original, to_merge)
local copy = {}
for k, v in pairs(original) do
copy[k] = v
end
if to_merge then
for k, v in pairs(to_merge) do
copy[k] = v
end
end
return copy
end
return {
prep_prompt_file = prep_prompt_file,
should_skip = should_skip,
prep_inst_and_content = prep_inst_and_content,
load_file_refs = load_file_refs
}