From b4f06c656d8d8733a9bf2e2e2a5eb2a7a48389bb Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 11 Jun 2026 00:28:27 +0300 Subject: Extract shared chatrun package to eliminate chat-runner DRY violations The CLI, LSP server and tmux code-action tool each carried near-identical chat-running logic: invoking the LLM (streaming-aware), collecting the response, and accounting sent/received bytes into the stats package. Introduce internal/chatrun with: - Invoke: streaming-aware LLM call that collects the full response and optionally mirrors chunks to a writer (nil writer = collect only). - SentBytes / Account: shared byte counting and stats.Update. Wire all three surfaces to it: - hexaicli: runChatRequest delegates to chatrun.Invoke; summarizeChatRun uses chatrun.Account. Removed the duplicated streaming/simple helpers. - hexaiaction: runOnce uses chatrun.Invoke + chatrun.Account, keeping the tmux status update local. - lsp: chatWithStats and the completion path use chatrun.SentBytes/Invoke; extracted unavailableClientError to keep chatWithStats small. chatrun has 100% test coverage; full suite passes with -race. Co-Authored-By: Claude Opus 4.8 --- internal/lsp/handlers_completion.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'internal/lsp/handlers_completion.go') diff --git a/internal/lsp/handlers_completion.go b/internal/lsp/handlers_completion.go index fced629..5ae6cd4 100644 --- a/internal/lsp/handlers_completion.go +++ b/internal/lsp/handlers_completion.go @@ -12,6 +12,7 @@ import ( "sync" "time" + "codeberg.org/snonux/hexai/internal/chatrun" "codeberg.org/snonux/hexai/internal/llm" "codeberg.org/snonux/hexai/internal/llmutils" "codeberg.org/snonux/hexai/internal/logging" @@ -326,12 +327,11 @@ func (cs *completionService) runCompletionForSpec(ctx context.Context, plan comp func (cs *completionService) executeChatCompletion(ctx context.Context, plan completionPlan, spec requestSpec, client llm.Client, sortPrefix string) ([]CompletionItem, bool) { s := cs.srv messages := cs.buildCompletionMessages(plan.inlinePrompt, plan.hasExtra, plan.extraText, plan.inParams, plan.params, plan.above, plan.current, plan.below, plan.funcCtx) - sentSize := 0 - for _, m := range messages { - sentSize += len(m.Content) - } + sentSize := chatrun.SentBytes(messages) s.incSentCounters(sentSize) - text, err := client.Chat(ctx, messages, spec.options...) + // Completion never streams to a writer; Invoke collects the full text and + // keeps this path aligned with chatWithStats and the other surfaces. + text, err := chatrun.Invoke(ctx, client, messages, spec.options, nil) if err != nil { logging.Logf("lsp ", "llm completion error: %v", err) return nil, false -- cgit v1.2.3