diff options
| author | theprimeagain <the.primeagen@gmail.com> | 2026-02-15 16:00:54 -0700 |
|---|---|---|
| committer | theprimeagain <the.primeagen@gmail.com> | 2026-02-15 16:00:54 -0700 |
| commit | a5130c1ec63c4b6bbceedbc61906cd9803bfabf8 (patch) | |
| tree | a5602ee1d407e40e5af0b486f1f79c04070138d5 /lua | |
| parent | 3ade8bbbbcc4fe80a09f22eb59ce8539e4fe2987 (diff) | |
| download | a4-a5130c1ec63c4b6bbceedbc61906cd9803bfabf8.tar.xz a4-a5130c1ec63c4b6bbceedbc61906cd9803bfabf8.zip | |
better entry point. success still needs work
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/99/init.lua | 121 | ||||
| -rw-r--r-- | lua/99/ops/clean-up.lua | 6 | ||||
| -rw-r--r-- | lua/99/ops/over-range.lua | 3 | ||||
| -rw-r--r-- | lua/99/ops/search.lua | 4 | ||||
| -rw-r--r-- | lua/99/ops/tutorial.lua | 3 | ||||
| -rw-r--r-- | lua/99/prompt-settings.lua | 3 | ||||
| -rw-r--r-- | lua/99/request-context.lua | 11 | ||||
| -rw-r--r-- | lua/99/request/init.lua | 16 |
8 files changed, 98 insertions, 69 deletions
diff --git a/lua/99/init.lua b/lua/99/init.lua index 43daed1..35b1444 100644 --- a/lua/99/init.lua +++ b/lua/99/init.lua @@ -58,20 +58,12 @@ end --- @alias _99.RequestEntry.Data _99.RequestEntry.Data.Search | _99.RequestEntry.Data.Tutorial | _99.RequestEntry.Data.Visual --- @class _99.RequestEntry ---- @field id number ---- @field operation string +--- @field context _99.RequestContext --- @field status _99.Request.State ---- @field filename string ---- @field lnum number ---- @field col number +--- @field point _99.Point --- @field started_at number --- @field operation_data _99.RequestEntry.Data | nil ---- @class _99.ActiveRequest ---- @field clean_up _99.Cleanup ---- @field request_id number ---- @field name string - --- @class _99.StateProps --- @field model string --- @field md_files string[] @@ -171,36 +163,38 @@ function _99_State:refresh_rules() end --- @param context _99.RequestContext ---- @param clean_up fun(): nil --- @return _99.RequestEntry -function _99_State:track_request(context, clean_up) +function _99_State:track_request(context) + assert( + context.operation, + "must have an operation defined to track the request" + ) + local point = context.range and context.range.start or Point:from_cursor() local entry = { - id = context.xid, - clean_up = clean_up, - operation = context.operation or "request", - status = "running", - filename = context.full_path, - lnum = point.row, - col = point.col, + context = context, + status = "requesting", + point = point, started_at = time.now(), operation_data = nil, } table.insert(self.__request_history, entry) - self.__request_by_id[entry.id] = entry + self.__request_by_id[context.xid] = entry return entry end --- @param context _99.RequestContext ---- @param status "success" | "failed" | "cancelled" +--- @param status _99.Request.ResponseState function _99_State:finish_request(context, status) local id = context.xid local entry = self.__request_by_id[id] - if entry then - entry.status = status + if not entry then + return end - if entry.operation == "success" and entry.operation_data then - local data = entry.operation_data + + entry.status = status + local data = entry.operation_data + if entry.status == "success" and data then if data.type == "tutorial" then table.insert(self.__tutorials, data) elseif data.type == "search" then @@ -218,7 +212,7 @@ function _99_State:add_data(id, data) end local logger = Logger:set_id(id) logger:assert( - entry.operation == data.type, + entry.context.operation == data.type, "the data type is not the same as the operation" ) entry.operation_data = data @@ -228,7 +222,7 @@ end function _99_State:previous_request_count() local count = 0 for _, entry in ipairs(self.__request_history) do - if entry.status ~= "running" then + if entry.status ~= "requesting" then count = count + 1 end end @@ -238,10 +232,10 @@ end function _99_State:clear_previous_requests() local keep = {} for _, entry in ipairs(self.__request_history) do - if entry.status == "running" then + if entry.status == "requesting" then table.insert(keep, entry) else - self.__request_by_id[entry.id] = nil + self.__request_by_id[entry.context.xid] = nil end end self.__request_history = keep @@ -257,7 +251,7 @@ end function _99_State:active_request_count() local count = 0 for _, r in pairs(self.__request_history) do - if r.status == "running" then + if r.status == "requesting" then count = count + 1 end end @@ -366,24 +360,14 @@ function _99.search(opts) end --- @param opts _99.ops.Opts -function _99.visual_prompt(opts) - vim.notify( - "use visual, visual_prompt has been deprecated", - vim.log.levels.WARN - ) - _99.visual(opts) -end - -function _99.fill_in_function() - error( - "function has been removed. Just use visual. I really hate fill in function, sorry :)" - ) -end - -function _99.fill_in_function_prompt() - error( - "function has been removed. Just use visual. I really hate fill in function, sorry :)" - ) +function _99.tutorial(opts) + opts = process_opts(opts) + local context = get_context("tutorial") + if opts.additional_prompt then + ops.tutorial(context, opts) + else + capture_prompt(ops.tutorial, "Tutorial", context, opts) + end end --- @param opts _99.ops.Opts? @@ -434,8 +418,33 @@ function _99.next_request_logs() Window.display_full_screen_message(logs[_99_state.__view_log_idx]) end +--- @class _99.QFixEntry +--- @field filename string +--- @field lnum number +--- @field col number +--- @field text string + +--- @param entry _99.RequestEntry +--- @return _99.QFixEntry +local function request_entry_to_qfix_item(entry) + local context = entry.context + local point = entry.point + local text = string.format("[%s] %s", entry.status, entry.context.operation) + + return { + filename = context and context.full_path or "", + lnum = point and point.row or 0, + col = point and point.col or 0, + text = text, + } +end + function _99.stop_all_requests() - error("implement") + for _, request in pairs(_99_state.__request_by_id) do + if request.status == "requesting" then + request.context:stop() + end + end end function _99.clear_all_marks() @@ -448,12 +457,7 @@ end function _99.previous_requests_to_qfix() local items = {} for _, entry in ipairs(_99_state.__request_history) do - table.insert(items, { - filename = entry.filename, - lnum = entry.lnum, - col = entry.col, - text = string.format("[%s] %s", entry.status, entry.operation), - }) + table.insert(items, request_entry_to_qfix_item(entry)) end vim.fn.setqflist({}, "r", { title = "99 Requests", items = items }) vim.cmd("copen") @@ -506,10 +510,17 @@ local function show_in_flight_requests() return shut_down_in_flight_requests_window() end + --- @type string[] local lines = { throb .. " requests(" .. tostring(count) .. ") " .. throb, } + for _, r in pairs(_99_state.__request_by_id) do + if r.status == "requesting" then + table.insert(lines, r.context.operation) + end + end + Window.resize(win, #lines[1], #lines) vim.api.nvim_buf_set_lines(win.buf_id, 0, 1, false, lines) end) diff --git a/lua/99/ops/clean-up.lua b/lua/99/ops/clean-up.lua index 2ac099c..cbf077a 100644 --- a/lua/99/ops/clean-up.lua +++ b/lua/99/ops/clean-up.lua @@ -7,27 +7,23 @@ local M = {} --- @field on_stderr? fun(line: string): nil --- @field on_start? fun(): nil ---- @param context _99.RequestContext --- @param clean_up fun(): nil --- @param obs_or_fn _99.Providers.PartialObserver | _99.Providers.on_complete --- @return _99.Providers.Observer -M.make_observer = function(context, clean_up, obs_or_fn) +M.make_observer = function(clean_up, obs_or_fn) --- @type _99.Providers.PartialObserver local obs = type(obs_or_fn) == "table" and obs_or_fn or { on_complete = obs_or_fn, } - return { on_start = function() - context._99:track_request(context, clean_up) if obs.on_start then obs.on_start() end end, on_complete = function(status, res) vim.schedule(clean_up) - context._99:finish_request(context, status) obs.on_complete(status, res) end, on_stderr = function(line) diff --git a/lua/99/ops/over-range.lua b/lua/99/ops/over-range.lua index a37aaf8..7070857 100644 --- a/lua/99/ops/over-range.lua +++ b/lua/99/ops/over-range.lua @@ -52,10 +52,11 @@ local function over_range(context, range, opts) request:add_prompt_content(prompt) context:add_references(refs) + context:add_clean_up(clean_up) top_status:start() bottom_status:start() - request:start(make_observer(context, clean_up, { + request:start(make_observer(clean_up, { on_complete = function(status, response) if status == "cancelled" then logger:debug("request cancelled for visual selection, removing marks") diff --git a/lua/99/ops/search.lua b/lua/99/ops/search.lua index cde8f64..44dd29c 100644 --- a/lua/99/ops/search.lua +++ b/lua/99/ops/search.lua @@ -75,10 +75,12 @@ local function search(context, opts) local prompt, refs = make_prompt(context, context._99.prompts.prompts.semantic_search(), opts) + request:add_prompt_content(prompt) context:add_references(refs) + context:add_clean_up(clean_up) - request:start(make_observer(context, clean_up, function(status, response) + request:start(make_observer(clean_up, function(status, response) if status == "cancelled" then logger:debug("request cancelled for search") elseif status == "failed" then diff --git a/lua/99/ops/tutorial.lua b/lua/99/ops/tutorial.lua index bba56e6..10d3530 100644 --- a/lua/99/ops/tutorial.lua +++ b/lua/99/ops/tutorial.lua @@ -24,8 +24,9 @@ local function tutorial(context, opts) make_prompt(context, context._99.prompts.prompts.tutorial(), opts) context:add_references(refs) request:add_prompt_content(prompt) + context:add_clean_up(clean_up) - request:start(make_observer(context, clean_up, function(status, response) + request:start(make_observer(clean_up, function(status, response) vim.schedule(clean_up) if status == "cancelled" then logger:debug("cancelled") diff --git a/lua/99/prompt-settings.lua b/lua/99/prompt-settings.lua index deb3368..9950a86 100644 --- a/lua/99/prompt-settings.lua +++ b/lua/99/prompt-settings.lua @@ -20,7 +20,8 @@ local prompts = { return [[ You are given a prompt and context and you must craft a tutorial. If a set of context has links, read through them thoroughly and decide which ones to retrieve. -Once you get the content, review it thoroughly before crafting the tutorial +Once you have fetched all the relavent content, review it thoroughly before +crafting the tutorial <Rule>The response format must be valid Markdown</Rule> ]] diff --git a/lua/99/request-context.lua b/lua/99/request-context.lua index ef10aff..ba76519 100644 --- a/lua/99/request-context.lua +++ b/lua/99/request-context.lua @@ -53,6 +53,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) diff --git a/lua/99/request/init.lua b/lua/99/request/init.lua index 3cff512..d057879 100644 --- a/lua/99/request/init.lua +++ b/lua/99/request/init.lua @@ -47,7 +47,7 @@ function Request:_set_process(proc) end function Request:cancel() - if self.state == "success" then + if self.state == "success" or self.state == "failed" then return end @@ -80,11 +80,19 @@ end --- @param r _99.Request --- @param obs _99.Providers.Observer | nil local function observer_from_request(r, obs) + + local context = r.context return { - on_start = obs and obs.on_start or function() end, + on_start = function() + r.state = "requesting" + context._99:track_request(context) + if obs then + obs.on_start() + end + end, on_complete = function(status, res) r.state = status - r.context._99:finish_request(r.context, status) + context._99:finish_request(context, status) if obs then obs.on_complete(status, res) end @@ -108,8 +116,6 @@ function Request:start(observer) self.state == "ready", "request is not in state ready when attempting to start a request" ) - self.state = "requesting" - self.context:finalize() for _, content in ipairs(self.context.ai_context) do self:add_prompt_content(content) |
