Skip to content

Commit

Permalink
fix(response): Fix response splitting in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tdfacer committed Jul 5, 2023
1 parent c4afb91 commit 5988eb9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/explain-it.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Class~
Fields~
{question} `(string)`
{input} `(string)`
{response} `(string|table)`
{response} `(string)`

------------------------------------------------------------------------------
*completion_command*
Expand Down
16 changes: 13 additions & 3 deletions lua/explain-it/handlers/response.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local strings = require "explain-it.util.strings"
local notify = require "notify"

local M = {}
Expand All @@ -19,9 +20,18 @@ M.notify_response = function(ai_response)
Response:
##RESPONSE##
]]
local replaced_question = notification_template:gsub("##QUESTION##", ai_response.question)
local replaced_input = replaced_question:gsub("##INPUT##", ai_response.input)
local replaced_response = replaced_input:gsub("##RESPONSE##", ai_response.response)
local should_split = _G.ExplainIt.config.split_responses
local width = _G.ExplainIt.config.max_notification_width
local question = should_split and strings.truncate_string(ai_response.question, width)
or ai_response.question
local input = should_split and strings.truncate_string(ai_response.input, width)
or ai_response.input
local response = should_split and strings.format_string_with_line_breaks(ai_response.response)
or ai_response.response

local replaced_question = notification_template:gsub("##QUESTION##", question)
local replaced_input = replaced_question:gsub("##INPUT##", input)
local replaced_response = replaced_input:gsub("##RESPONSE##", response)

notify(replaced_response)
end
Expand Down
2 changes: 1 addition & 1 deletion lua/explain-it/services/chat-gpt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local M = {}
---@class AIResponse
---@field question string
---@field input string
---@field response string|table
---@field response string

---@alias completion_command string
local completion_command = [[
Expand Down
29 changes: 18 additions & 11 deletions lua/explain-it/util/strings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ function M.format_string_with_line_breaks(str)
local formattedStr = ""
local lineLength = 0
local words = {}
for word in str:gmatch "%S+" do
table.insert(words, word)
end
for _, word in ipairs(words) do
if lineLength + #word > _G.ExplainIt.config.max_notification_width then
formattedStr = formattedStr .. "\n" .. word
lineLength = #word
for line in str:gmatch("[^\r\n]+") do -- split string into lines
if #line <= _G.ExplainIt.config.max_notification_width then
formattedStr = formattedStr .. line .. "\n" -- preserve newline
lineLength = 0
else
formattedStr = formattedStr .. " " .. word
lineLength = lineLength + #word + 1
for word in line:gmatch("%S+") do -- split line into words
if lineLength + #word > _G.ExplainIt.config.max_notification_width then
formattedStr = formattedStr .. "\n" .. word
lineLength = #word
else
formattedStr = formattedStr .. " " .. word
lineLength = lineLength + #word + 1
end
end
formattedStr = formattedStr .. "\n" -- preserve newline
lineLength = 0
end
end
local stripped, _ = string.gsub(formattedStr, "^%s", "")
return stripped
local stripped, _ = string.gsub(formattedStr, "^%s+", "") -- remove leading whitespace
local no_trailing_newline, _ = stripped:gsub("\n$", "") -- remove trailing newline
return no_trailing_newline
end

--- Takes a string as input and returns a truncated version of the string if it is longer than 77 characters. The truncated version includes an ellipsis ("...") at the end. If the string is 77 characters or shorter, the function simply returns the original string. The code also includes comments that describe the function's input and output parameters. Finally, the code returns the module "M".
Expand Down
3 changes: 3 additions & 0 deletions lua/tests/handlers/response_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ local notify = require "notify"
describe("notify", function()
before_each(function()
stub(notify, "notify")
require("explain-it").setup {
token_limit = 2000,
}
end)

it("should notify response", function()
Expand Down

0 comments on commit 5988eb9

Please sign in to comment.