diff options
Diffstat (limited to 'lua/99/request-context.lua')
| -rw-r--r-- | lua/99/request-context.lua | 85 |
1 files changed, 45 insertions, 40 deletions
diff --git a/lua/99/request-context.lua b/lua/99/request-context.lua index 6671cab..48d4489 100644 --- a/lua/99/request-context.lua +++ b/lua/99/request-context.lua @@ -15,6 +15,7 @@ local random_file = utils.random_file --- @field xid number --- @field range _99.Range? --- @field operation string? +--- @field clean_ups (fun(): nil)[] --- @field _99 _99.State local RequestContext = {} RequestContext.__index = RequestContext @@ -36,11 +37,17 @@ function RequestContext.from_current_buffer(_99, xid) table.insert(mds, md) end + local tmp_dir = _99.tmp_dir + if tmp_dir then + tmp_dir = vim.fn.expand(tmp_dir) + end + return setmetatable({ _99 = _99, + clean_ups = {}, md_file_names = mds, ai_context = {}, - tmp_file = random_file(), + tmp_file = random_file(tmp_dir), buffer = buffer, full_path = full_path, file_type = file_type, @@ -51,6 +58,17 @@ function RequestContext.from_current_buffer(_99, xid) }, RequestContext) end +function RequestContext:stop() + for _, cb in ipairs(self.clean_ups) do + cb() + end +end + +--- @param clean_up fun(): nil +function RequestContext:add_clean_up(clean_up) + table.insert(self.clean_ups, clean_up) +end + --- @param md_file_name string --- @return self function RequestContext:add_md_file_name(md_file_name) @@ -58,40 +76,6 @@ function RequestContext:add_md_file_name(md_file_name) return self end ---- TODO: Dedupe any rules that have already been added ---- @param rules (_99.Agents.Rule | string)[] -function RequestContext:add_agent_rules(rules) - for _, rule in ipairs(rules) do - -- Handle both string paths and rule objects - self.logger:debug("adding custom rule to agent", "rule", rule) - local file_path = rule.absolute_path or rule.path - local ok, file = pcall(io.open, file_path, "r") - if ok and file then - local content = file:read("*a") - file:close() - self.logger:info( - "Context#adding agent file to the context", - "agent_path", - rule.path - ) - table.insert( - self.ai_context, - string.format( - [[ -<%s> -%s -</%s>]], - rule.name, - content, - rule.name - ) - ) - else - self.logger:debug("unable to read agent rule", "rule", rule) - end - end -end - --- @param refs _99.Reference[] function RequestContext:add_references(refs) for _, ref in ipairs(refs) do @@ -133,16 +117,34 @@ function RequestContext:content() return self.ai_context end ---- @param prompt string -function RequestContext:save_prompt(prompt) +--- @return boolean +function RequestContext:_ready_request_files() + local response_file = self.tmp_file local prompt_file = self.tmp_file .. "-prompt" local dir = vim.fs.dirname(prompt_file) if dir and not vim.uv.fs_stat(dir) then - pcall(vim.uv.fs_mkdir, dir, 493) + vim.fn.mkdir(dir, "p") + end + + local files = { prompt_file, response_file } + for _, f in ipairs(files) do + local file = io.open(f, "w") + if file then + file:write("") + file:close() + else + self.logger:error("unable to create prompt file") + return false + end end + return true +end +--- @param prompt string +function RequestContext:save_prompt(prompt) + local prompt_file = self.tmp_file .. "-prompt" local file = io.open(prompt_file, "w") if file then file:write(prompt) @@ -153,8 +155,11 @@ function RequestContext:save_prompt(prompt) end end ---- @return self +--- @return boolean, self function RequestContext:finalize() + if self:_ready_request_files() == false then + return false, self + end self:_read_md_files() if self.range then table.insert(self.ai_context, self._99.prompts.get_file_location(self)) @@ -164,7 +169,7 @@ function RequestContext:finalize() self.ai_context, self._99.prompts.tmp_file_location(self.tmp_file) ) - return self + return true, self end function RequestContext:clear_marks() |
