Skip to content

Commit 4ee9038

Browse files
committed
refactor: move old signature check to deprecator
1 parent 96af1aa commit 4ee9038

File tree

2 files changed

+53
-38
lines changed

2 files changed

+53
-38
lines changed

lua/gp/deprecator.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,55 @@ M.report = function()
8888
logger.info(msg)
8989
end
9090

91+
local examplePromptHook = [[
92+
UnitTests = function(gp, params)
93+
local template = "I have the following code from {{filename}}:\n\n"
94+
.. "```{{filetype}}\n{{selection}}\n```\n\n"
95+
.. "Please respond by writing table driven unit tests for the code above."
96+
local agent = gp.get_command_agent()
97+
gp.Prompt(params, gp.Target.vnew, agent, template)
98+
end,
99+
]]
100+
101+
M.has_old_prompt_signature = function(agent)
102+
if not agent or not type(agent) == "table" or not agent.provider then
103+
logger.warning(
104+
"The `gp.Prompt` method signature has changed.\n"
105+
.. "Please update your hook functions as demonstrated in the example below:\n\n"
106+
.. examplePromptHook
107+
.. "\nFor more information, refer to the 'Extend Functionality' section in the documentation."
108+
)
109+
return true
110+
end
111+
return false
112+
end
113+
114+
local exampleChatHook = [[
115+
Translator = function(gp, params)
116+
local chat_system_prompt = "You are a Translator, please translate between English and Chinese."
117+
gp.cmd.ChatNew(params, chat_system_prompt)
118+
119+
-- -- you can also create a chat with a specific fixed agent like this:
120+
-- local agent = gp.get_chat_agent("ChatGPT4o")
121+
-- gp.cmd.ChatNew(params, chat_system_prompt, agent)
122+
end,
123+
]]
124+
125+
M.has_old_chat_signature = function(agent)
126+
if agent then
127+
if not type(agent) == "table" or not agent.provider then
128+
logger.warning(
129+
"The `gp.cmd.ChatNew` method signature has changed.\n"
130+
.. "Please update your hook functions as demonstrated in the example below:\n\n"
131+
.. exampleChatHook
132+
.. "\nFor more information, refer to the 'Extend Functionality' section in the documentation."
133+
)
134+
return true
135+
end
136+
end
137+
return false
138+
end
139+
91140
M.check_health = function()
92141
if #M._deprecated == 0 then
93142
vim.health.ok("no deprecated config options")

lua/gp/init.lua

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -765,33 +765,15 @@ M.new_chat = function(params, toggle, system_prompt, agent)
765765
return buf
766766
end
767767

768-
local exampleChatHook = [[
769-
Translator = function(gp, params)
770-
local chat_system_prompt = "You are a Translator, please translate between English and Chinese."
771-
gp.cmd.ChatNew(params, chat_system_prompt)
772-
773-
-- -- you can also create a chat with a specific fixed agent like this:
774-
-- local agent = gp.get_chat_agent("ChatGPT4o")
775-
-- gp.cmd.ChatNew(params, chat_system_prompt, agent)
776-
end,
777-
]]
778-
779768
---@param params table
780769
---@param system_prompt string | nil
781770
---@param agent table | nil # obtained from get_command_agent or get_chat_agent
782771
---@return number # buffer number
783772
M.cmd.ChatNew = function(params, system_prompt, agent)
784-
if agent then
785-
if not type(agent) == "table" or not agent.provider then
786-
M.logger.warning(
787-
"The `gp.cmd.ChatNew` method signature has changed.\n"
788-
.. "Please update your hook functions as demonstrated in the example below:\n\n"
789-
.. exampleChatHook
790-
.. "\nFor more information, refer to the 'Extend Functionality' section in the documentation."
791-
)
792-
return -1
793-
end
773+
if M.deprecator.has_old_chat_signature(agent) then
774+
return -1
794775
end
776+
795777
-- if chat toggle is open, close it and start a new one
796778
if M._toggle_close(M._toggle_kind.chat) then
797779
params.args = params.args or ""
@@ -1632,16 +1614,6 @@ M.cmd.Context = function(params)
16321614
M.helpers.feedkeys("G", "xn")
16331615
end
16341616

1635-
local examplePromptHook = [[
1636-
UnitTests = function(gp, params)
1637-
local template = "I have the following code from {{filename}}:\n\n"
1638-
.. "```{{filetype}}\n{{selection}}\n```\n\n"
1639-
.. "Please respond by writing table driven unit tests for the code above."
1640-
local agent = gp.get_command_agent()
1641-
gp.Prompt(params, gp.Target.vnew, agent, template)
1642-
end,
1643-
]]
1644-
16451617
---@param params table # vim command parameters such as range, args, etc.
16461618
---@param target number | function | table # where to put the response
16471619
---@param agent table # obtained from get_command_agent or get_chat_agent
@@ -1650,13 +1622,7 @@ end,
16501622
---@param whisper string | nil # predefined input (e.g. obtained from Whisper)
16511623
---@param callback function | nil # callback after completing the prompt
16521624
M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
1653-
if not agent or not type(agent) == "table" or not agent.provider then
1654-
M.logger.warning(
1655-
"The `gp.Prompt` method signature has changed.\n"
1656-
.. "Please update your hook functions as demonstrated in the example below:\n\n"
1657-
.. examplePromptHook
1658-
.. "\nFor more information, refer to the 'Extend Functionality' section in the documentation."
1659-
)
1625+
if M.deprecator.has_old_prompt_signature(agent) then
16601626
return
16611627
end
16621628

0 commit comments

Comments
 (0)