Skip to content

Commit c612d06

Browse files
committed
rewrite and append with full file context
1 parent 52938ff commit c612d06

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

lua/gp/config.lua

+19-33
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ local config = {
3636
-- agents = { { name = "ChatGPT4" }, ... },
3737
agents = {
3838
{
39-
name = "ChatGPT4",
39+
name = "ChatGPT4o",
4040
chat = true,
4141
command = false,
4242
-- string with model name or table with model name and parameters
43-
model = { model = "gpt-4-1106-preview", temperature = 1.1, top_p = 1 },
43+
model = { model = "gpt-4o", temperature = 1.1, top_p = 1 },
4444
-- system prompt (use this to specify the persona/role of the AI)
4545
system_prompt = "You are a general AI assistant.\n\n"
4646
.. "The user provided the additional info about how they would like you to respond:\n\n"
@@ -53,39 +53,11 @@ local config = {
5353
.. "- Take a deep breath; You've got this!\n",
5454
},
5555
{
56-
name = "ChatGPT3-5",
57-
chat = true,
58-
command = false,
59-
-- string with model name or table with model name and parameters
60-
model = { model = "gpt-3.5-turbo-1106", temperature = 1.1, top_p = 1 },
61-
-- system prompt (use this to specify the persona/role of the AI)
62-
system_prompt = "You are a general AI assistant.\n\n"
63-
.. "The user provided the additional info about how they would like you to respond:\n\n"
64-
.. "- If you're unsure don't guess and say you don't know instead.\n"
65-
.. "- Ask question if you need clarification to provide better answer.\n"
66-
.. "- Think deeply and carefully from first principles step by step.\n"
67-
.. "- Zoom out first to see the big picture and then zoom in to details.\n"
68-
.. "- Use Socratic method to improve your thinking and coding skills.\n"
69-
.. "- Don't elide any code from your output if the answer requires coding.\n"
70-
.. "- Take a deep breath; You've got this!\n",
71-
},
72-
{
73-
name = "CodeGPT4",
74-
chat = false,
75-
command = true,
76-
-- string with model name or table with model name and parameters
77-
model = { model = "gpt-4-1106-preview", temperature = 0.8, top_p = 1 },
78-
-- system prompt (use this to specify the persona/role of the AI)
79-
system_prompt = "You are an AI working as a code editor.\n\n"
80-
.. "Please AVOID COMMENTARY OUTSIDE OF THE SNIPPET RESPONSE.\n"
81-
.. "START AND END YOUR ANSWER WITH:\n\n```",
82-
},
83-
{
84-
name = "CodeGPT3-5",
56+
name = "CodeGPT4o",
8557
chat = false,
8658
command = true,
8759
-- string with model name or table with model name and parameters
88-
model = { model = "gpt-3.5-turbo-1106", temperature = 0.8, top_p = 1 },
60+
model = { model = "gpt-4o", temperature = 0.8, top_p = 1 },
8961
-- system prompt (use this to specify the persona/role of the AI)
9062
system_prompt = "You are an AI working as a code editor.\n\n"
9163
.. "Please AVOID COMMENTARY OUTSIDE OF THE SNIPPET RESPONSE.\n"
@@ -158,12 +130,26 @@ local config = {
158130
-- templates
159131
template_selection = "I have the following from {{filename}}:"
160132
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}",
161-
template_rewrite = "I have the following from {{filename}}:"
133+
template_rewrite = "I have the following awesome stuff from {{filename}}:"
162134
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
163135
.. "\n\nRespond exclusively with the snippet that should replace the selection above.",
136+
template_rewrite_with_file = "I have the following file {{filename}}:"
137+
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
138+
.. "\n\n I want to update the following code block"
139+
.. "\n\n```{{filetype}}\n{{selection}}\n```"
140+
.. "\n\nInstructions: "
141+
.. "\n- {{command}}"
142+
.. "\n- Respond exclusively with the snippet that should replace the selection above.",
164143
template_append = "I have the following from {{filename}}:"
165144
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
166145
.. "\n\nRespond exclusively with the snippet that should be appended after the selection above.",
146+
template_append_with_file = "I have the following file {{filename}}:"
147+
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
148+
.. "\n\n I want to append after the following code block"
149+
.. "\n\n```{{filetype}}\n{{selection}}\n```"
150+
.. "\n\nInstructions: "
151+
.. "\n- {{command}}"
152+
.. "\n- Respond exclusively with the snippet that should be appended after the selection above.",
167153
template_prepend = "I have the following from {{filename}}:"
168154
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
169155
.. "\n\nRespond exclusively with the snippet that should be prepended before the selection above.",

lua/gp/init.lua

+31-5
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,15 @@ _H.find_git_root = function()
603603
return ""
604604
end
605605

606+
_H.repo_instruct_file = function()
607+
local git_root = _H.find_git_root()
608+
609+
if git_root == "" then
610+
return ""
611+
end
612+
613+
return git_root .. "/.gp.md"
614+
end
606615
-- tries to find an .gp.md file in the root of current git repo
607616
---@return string # returns instructions from the .gp.md file
608617
M.repo_instructions = function()
@@ -628,6 +637,7 @@ M.template_render = function(template, command, selection, filetype, filename)
628637
["{{selection}}"] = selection,
629638
["{{filetype}}"] = filetype,
630639
["{{filename}}"] = filename,
640+
["{{file_content}}"] = vim.api.nvim_buf_get_lines(0, 0, -1, false)
631641
}
632642
return _H.template_render(template, key_value_pairs)
633643
end
@@ -916,6 +926,8 @@ M.Target = {
916926
append = 1, -- for appending after the selection, range or the current line
917927
prepend = 2, -- for prepending before the selection, range or the current line
918928
popup = 3, -- for writing into the popup window
929+
rewriteWithFile = 4, -- for replacing the selection, range or the current line with the full file as context
930+
appendWithFile = 5, -- for appending after the selection, range or the current line with the full file as context
919931

920932
-- for writing into a new buffer
921933
---@param filetype nil | string # nil = same as the original buffer
@@ -966,11 +978,13 @@ M.prepare_commands = function()
966978
-- rewrite needs custom template
967979
if target == M.Target.rewrite then
968980
template = M.config.template_rewrite
969-
end
970-
if target == M.Target.append then
981+
elseif target == M.Target.rewriteWithFile then
982+
template = M.config.template_rewrite_with_file
983+
elseif target == M.Target.append then
971984
template = M.config.template_append
972-
end
973-
if target == M.Target.prepend then
985+
elseif target == M.Target.appendWithFile then
986+
template = M.config.template_append_with_file
987+
elseif target == M.Target.prepend then
974988
template = M.config.template_prepend
975989
end
976990
end
@@ -2494,6 +2508,7 @@ end
24942508

24952509
M.Prompt = function(params, target, prompt, model, template, system_template, whisper)
24962510
-- enew, new, vnew, tabnew should be resolved into table
2511+
print(template)
24972512
if type(target) == "function" then
24982513
target = target()
24992514
end
@@ -2665,7 +2680,6 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
26652680

26662681
local user_prompt = M.template_render(template, command, selection, filetype, filename)
26672682
table.insert(messages, { role = "user", content = user_prompt })
2668-
26692683
-- cancel possible visual mode before calling the model
26702684
M._H.feedkeys("<esc>", "xn")
26712685

@@ -2680,13 +2694,25 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
26802694
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
26812695
-- prepare handler
26822696
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
2697+
elseif target == M.Target.rewriteWithFile then
2698+
-- delete selection
2699+
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
2700+
-- prepare handler
2701+
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
26832702
elseif target == M.Target.append then
26842703
-- move cursor to the end of the selection
26852704
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
26862705
-- put newline after selection
26872706
vim.api.nvim_put({ "" }, "l", true, true)
26882707
-- prepare handler
26892708
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
2709+
elseif target == M.Target.appendWithFile then
2710+
-- move cursor to the end of the selection
2711+
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
2712+
-- put newline after selection
2713+
vim.api.nvim_put({ "" }, "l", true, true)
2714+
-- prepare handler
2715+
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
26902716
elseif target == M.Target.prepend then
26912717
-- move cursor to the start of the selection
26922718
vim.api.nvim_win_set_cursor(0, { start_line, 0 })

0 commit comments

Comments
 (0)