From 5c4fc0f74decede48eeda9b2aec0986901118624 Mon Sep 17 00:00:00 2001 From: Meisterlala <6453306+Meisterlala@users.noreply.github.com> Date: Fri, 5 Jun 2026 22:51:56 +0200 Subject: [PATCH] feat(ai-provider): implement status throttling for streaming updates Introduces mechanisms to track request status phases and apply time-based throttling to status updates --- local/ai-provider/lua/ai-provider/init.lua | 1 + .../lua/ai-provider/providers/ollama.lua | 14 ++++++++++++++ lua/plugins/ai-commit.lua | 1 + 3 files changed, 16 insertions(+) diff --git a/local/ai-provider/lua/ai-provider/init.lua b/local/ai-provider/lua/ai-provider/init.lua index 68c9ceb..96f0de1 100644 --- a/local/ai-provider/lua/ai-provider/init.lua +++ b/local/ai-provider/lua/ai-provider/init.lua @@ -65,6 +65,7 @@ ---@field think? boolean Per-request Ollama thinking mode override. ---@field on_chunk? fun(chunk: string, raw: table) Called for each streamed text chunk. ---@field on_status? fun(status: AiProviderStatus) Called with standardized provider progress updates. +---@field status_interval? integer Minimum milliseconds between same-phase status updates. Defaults to the provider's status throttle. ---@field callback? fun(message: string|nil, meta: AiProviderChatMeta|nil) Called once the request finishes. ---@field is_cancelled? fun(): boolean Optional cancellation predicate. ---@field register_http_job? fun(job: table) Receives the provider job/process for external cancellation. diff --git a/local/ai-provider/lua/ai-provider/providers/ollama.lua b/local/ai-provider/lua/ai-provider/providers/ollama.lua index c7bc766..1f3081f 100644 --- a/local/ai-provider/lua/ai-provider/providers/ollama.lua +++ b/local/ai-provider/lua/ai-provider/providers/ollama.lua @@ -5,6 +5,7 @@ local log = require 'ai-provider.log' local DEFAULT_ENDPOINT = 'http://127.0.0.1:11434' local HEALTH_CACHE_TTL = 30 local DEFAULT_LOAD_TIMEOUT = 120000 +local STATUS_THROTTLE_MS = 100 local state = { health = nil, @@ -143,6 +144,9 @@ function M.chat(request) local metrics = {} local thinking_chars = 0 local last_status_key = nil + local last_status_phase = nil + local last_status_sent_at = 0 + local status_throttle_ms = request.status_interval or STATUS_THROTTLE_MS local generation_started_at = nil local streamed_token_estimate = 0 @@ -172,7 +176,17 @@ function M.chat(request) if key == last_status_key then return end + + local now = vim.uv.hrtime() + local phase = status.phase or '' + local important = phase == 'loading' or phase == 'loaded' or phase == 'done' or phase == 'error' or phase ~= last_status_phase + if not important and (now - last_status_sent_at) / 1e6 < status_throttle_ms then + return + end + last_status_key = key + last_status_phase = phase + last_status_sent_at = now log.debug( string.format( 'ollama status phase=%s model=%s tokens_per_second=%s elapsed_ms=%.0f', diff --git a/lua/plugins/ai-commit.lua b/lua/plugins/ai-commit.lua index 2f8a9c3..2344a40 100644 --- a/lua/plugins/ai-commit.lua +++ b/lua/plugins/ai-commit.lua @@ -271,6 +271,7 @@ local function complete_ollama(full_prompt, callback, status_callback, request_c is_cancelled = request_context and request_context.is_cancelled, register_http_job = request_context and request_context.register_http_job, on_status = report_provider_status, + status_interval = CONFIG.spinner_interval, callback = function(message, meta) if request_context and request_context.is_cancelled and request_context.is_cancelled() then return -- 2.51.2