diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-11 00:28:27 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-11 00:28:27 +0300 |
| commit | b4f06c656d8d8733a9bf2e2e2a5eb2a7a48389bb (patch) | |
| tree | 7df0eac26ded2b4854225711dfde8f40d80a0400 /internal/lsp/handlers_completion.go | |
| parent | 2b2f4110da53a03742faff89cdec17c55e091a90 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'internal/lsp/handlers_completion.go')
| -rw-r--r-- | internal/lsp/handlers_completion.go | 10 |
1 files changed, 5 insertions, 5 deletions
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 |
