Skip to content

Commit 5b5f944

Browse files
committed
feat: chat templates with {{tag}} syntax
1 parent e1905ae commit 5b5f944

File tree

3 files changed

+56
-45
lines changed

3 files changed

+56
-45
lines changed

lua/gp/config.lua

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,9 @@ local config = {
282282
-- if you really want just a static string, make it a table with one element { "🤖:" }
283283
chat_assistant_prefix = { "🤖:", "[{{agent}}]" },
284284
-- The banner shown at the top of each chat file.
285-
chat_template = [[
286-
# topic: ?
287-
288-
- file: %s
289-
%s
290-
Write your queries after %s. Use `%s` or :%sChatRespond to generate a response.
291-
Response generation can be terminated by using `%s` or :%sChatStop command.
292-
Chats are saved automatically. To delete this chat, use `%s` or :%sChatDelete.
293-
Be cautious of very long chats. Start a fresh chat by using `%s` or :%sChatNew.
294-
295-
---
296-
297-
%s]],
285+
chat_template = require("gp.defaults").chat_template,
286+
-- if you want more real estate in your chat files and don't need the helper text
287+
-- chat_template = require("gp.defaults").short_chat_template,
298288
-- chat topic generation prompt
299289
chat_topic_gen_prompt = "Summarize the topic of our conversation above"
300290
.. " in two or three words. Respond only with those words.",

lua/gp/defaults.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,27 @@ M.code_system_prompt = "You are an AI working as a code editor.\n\n"
1313
.. "Please AVOID COMMENTARY OUTSIDE OF THE SNIPPET RESPONSE.\n"
1414
.. "START AND END YOUR ANSWER WITH:\n\n```"
1515

16+
M.chat_template = [[
17+
# topic: ?
18+
19+
- file: {{filename}}
20+
{{optional_headers}}
21+
Write your queries after {{user_prefix}}. Use `{{respond_shortcut}}` or :{{cmd_prefix}}ChatRespond to generate a response.
22+
Response generation can be terminated by using `{{stop_shortcut}}` or :{{cmd_prefix}}ChatStop command.
23+
Chats are saved automatically. To delete this chat, use `{{delete_shortcut}}` or :{{cmd_prefix}}ChatDelete.
24+
Be cautious of very long chats. Start a fresh chat by using `{{new_shortcut}}` or :{{cmd_prefix}}ChatNew.
25+
26+
---
27+
28+
{{user_prefix}}
29+
]]
30+
31+
M.short_chat_template = [[
32+
# topic: ?
33+
- file: {{filename}}
34+
---
35+
36+
{{user_prefix}}
37+
]]
38+
1639
return M

lua/gp/init.lua

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ local M = {
5656
config = {}, -- config variables
5757
hooks = {}, -- user defined command functions
5858
spinner = require("gp.spinner"), -- spinner module
59+
defaults = require("gp.defaults"), -- some useful defaults
5960
}
6061

6162
--------------------------------------------------------------------------------
@@ -1612,20 +1613,6 @@ end
16121613
-- Chat logic
16131614
--------------------
16141615

1615-
M.chat_template = [[
1616-
# topic: ?
1617-
1618-
- file: %s
1619-
%s
1620-
Write your queries after %s. Use `%s` or :%sChatRespond to generate a response.
1621-
Response generation can be terminated by using `%s` or :%sChatStop command.
1622-
Chats are saved automatically. To delete this chat, use `%s` or :%sChatDelete.
1623-
Be cautious of very long chats. Start a fresh chat by using `%s` or :%sChatNew.
1624-
1625-
---
1626-
1627-
%s]]
1628-
16291616
M._toggle = {}
16301617

16311618
M._toggle_kind = {
@@ -1708,7 +1695,7 @@ M.not_chat = function(buf, file_name)
17081695
end
17091696

17101697
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
1711-
if #lines < 7 then
1698+
if #lines < 5 then
17121699
return "file too short"
17131700
end
17141701

@@ -1717,8 +1704,8 @@ M.not_chat = function(buf, file_name)
17171704
end
17181705

17191706
local header_found = nil
1720-
for i = 1, 6 do
1721-
if lines[i]:match("^- file: ") then
1707+
for i = 1, 10 do
1708+
if i < #lines and lines[i]:match("^- file: ") then
17221709
header_found = true
17231710
break
17241711
end
@@ -2051,21 +2038,32 @@ M.new_chat = function(params, toggle, system_prompt, agent)
20512038
system_prompt = ""
20522039
end
20532040

2054-
local template = string.format(
2055-
M.config.chat_template or M.chat_template,
2056-
string.match(filename, "([^/]+)$"),
2057-
model .. provider .. system_prompt,
2058-
M.config.chat_user_prefix,
2059-
M.config.chat_shortcut_respond.shortcut,
2060-
M.config.cmd_prefix,
2061-
M.config.chat_shortcut_stop.shortcut,
2062-
M.config.cmd_prefix,
2063-
M.config.chat_shortcut_delete.shortcut,
2064-
M.config.cmd_prefix,
2065-
M.config.chat_shortcut_new.shortcut,
2066-
M.config.cmd_prefix,
2067-
M.config.chat_user_prefix
2068-
)
2041+
local template = M._H.template_render(M.config.chat_template or require("gp.defaults").chat_template, {
2042+
["{{filename}}"] = string.match(filename, "([^/]+)$"),
2043+
["{{optional_headers}}"] = model .. provider .. system_prompt,
2044+
["{{user_prefix}}"] = M.config.chat_user_prefix,
2045+
["{{respond_shortcut}}"] = M.config.chat_shortcut_respond.shortcut,
2046+
["{{cmd_prefix}}"] = M.config.cmd_prefix,
2047+
["{{stop_shortcut}}"] = M.config.chat_shortcut_stop.shortcut,
2048+
["{{delete_shortcut}}"] = M.config.chat_shortcut_delete.shortcut,
2049+
["{{new_shortcut}}"] = M.config.chat_shortcut_new.shortcut,
2050+
})
2051+
2052+
-- local template = string.format(
2053+
-- M.config.chat_template or require("gp.defaults").chat_template,
2054+
-- string.match(filename, "([^/]+)$"),
2055+
-- model .. provider .. system_prompt,
2056+
-- M.config.chat_user_prefix,
2057+
-- M.config.chat_shortcut_respond.shortcut,
2058+
-- M.config.cmd_prefix,
2059+
-- M.config.chat_shortcut_stop.shortcut,
2060+
-- M.config.cmd_prefix,
2061+
-- M.config.chat_shortcut_delete.shortcut,
2062+
-- M.config.cmd_prefix,
2063+
-- M.config.chat_shortcut_new.shortcut,
2064+
-- M.config.cmd_prefix,
2065+
-- M.config.chat_user_prefix
2066+
-- )
20692067

20702068
-- escape underscores (for markdown)
20712069
template = template:gsub("_", "\\_")

0 commit comments

Comments
 (0)