Skip to content

Commit

Permalink
fix: Add condition to check table type args (#34)
Browse files Browse the repository at this point in the history
* chore: Refactor `render_template` in base.lua
- Remove unused function parameter "templates" for the function
  `render_template`
- Change the variable name "result" to "template"

* fix: Add condition to check table type args
- If a template argument in "args" is table type, parse it to get the
  default value of the argument: `value["default"]`.
  • Loading branch information
swoh816 authored Apr 16, 2024
1 parent a84fd35 commit 955348e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lua/ogpt/flows/actions/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ function BaseAction:update_variables()
end
end

function BaseAction:render_template(variables, templates)
function BaseAction:render_template(variables)
variables = vim.tbl_extend("force", self.variables, variables or {})
-- lazily render the final string.
-- it recursively loop on the template string until it does not find anymore
-- {{{}}} patterns
local stop = false
local depth = 2
local result = self.template
local template = self.template
-- deprecating warning of {{}} (double curly)
if vim.fn.match(result, [[\v\{\{([^}]+)\}\}(})@!]]) ~= -1 then
if vim.fn.match(template, [[\v\{\{([^}]+)\}\}(})@!]]) ~= -1 then
utils.log(
"You may be using the {{<template_helper>}}, please updated to {{{<template_helpers>}}} (triple curly braces)in your custom actions.",
vim.log.levels.ERROR
Expand All @@ -165,22 +165,27 @@ function BaseAction:render_template(variables, templates)

local pattern = "%{%{%{(([%w_]+))%}%}%}"
repeat
for match in string.gmatch(result, pattern) do
for match in string.gmatch(template, pattern) do
local value = variables[match]
if value then
-- Get "default" if value is table type
if type(value) == "table" then
value = value["default"]
end
-- Run function if value is function type
if type(value) == "function" then
value = value(self.variables)
end
local escaped_value = utils.escape_pattern(value)
result = string.gsub(result, "{{{" .. match .. "}}}", escaped_value)
template = string.gsub(template, "{{{" .. match .. "}}}", escaped_value)
else
utils.log("Cannot find {{{" .. match .. "}}}", vim.log.levels.ERROR)
stop = true
end
end
depth = depth - 1
until not string.match(result, pattern) or stop or depth == 0
return result
until not string.match(template, pattern) or stop or depth == 0
return template
end

function BaseAction:get_params()
Expand Down
2 changes: 1 addition & 1 deletion lua/ogpt/flows/actions/edits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ function EditAction:build_edit_messages(input, instructions, opts)
},
{
role = "user",
content = self:render_template(variables, opts.template),
content = self:render_template(variables),
},
}

Expand Down

0 comments on commit 955348e

Please sign in to comment.