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/hexaicli | |
| 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/hexaicli')
| -rw-r--r-- | internal/hexaicli/run.go | 46 | ||||
| -rw-r--r-- | internal/hexaicli/run_output_test.go | 8 |
2 files changed, 10 insertions, 44 deletions
diff --git a/internal/hexaicli/run.go b/internal/hexaicli/run.go index 94dcd12..6614bc5 100644 --- a/internal/hexaicli/run.go +++ b/internal/hexaicli/run.go @@ -13,6 +13,7 @@ import ( "time" "codeberg.org/snonux/hexai/internal/appconfig" + "codeberg.org/snonux/hexai/internal/chatrun" "codeberg.org/snonux/hexai/internal/llm" "codeberg.org/snonux/hexai/internal/llmutils" "codeberg.org/snonux/hexai/internal/logging" @@ -503,51 +504,16 @@ func effectiveModel(req requestArgs, client llm.Client) string { return model } +// runChatRequest invokes the LLM via the shared chatrun.Invoke helper, which +// prefers streaming when the provider supports it and mirrors chunks to out. func runChatRequest(ctx context.Context, client llm.Client, req requestArgs, msgs []llm.Message, out io.Writer) (string, error) { - if streamer, ok := client.(llm.Streamer); ok { - return runStreamingChat(ctx, streamer, msgs, req.options, out) - } - return runSimpleChat(ctx, client, msgs, req.options, out) -} - -func runStreamingChat(ctx context.Context, client llm.Streamer, msgs []llm.Message, options []llm.RequestOption, out io.Writer) (string, error) { - var output strings.Builder - var writeErr error - if err := client.ChatStream(ctx, msgs, func(chunk string) { - if writeErr != nil { - return - } - output.WriteString(chunk) - if _, err := fmt.Fprint(out, chunk); err != nil { - writeErr = err - } - }, options...); err != nil { - return "", err - } - if writeErr != nil { - return "", writeErr - } - return output.String(), nil -} - -func runSimpleChat(ctx context.Context, client llm.Client, msgs []llm.Message, options []llm.RequestOption, out io.Writer) (string, error) { - output, err := client.Chat(ctx, msgs, options...) - if err != nil { - return "", err - } - if _, err := fmt.Fprint(out, output); err != nil { - return "", err - } - return output, nil + return chatrun.Invoke(ctx, client, msgs, req.options, out) } func summarizeChatRun(ctx context.Context, client llm.Client, model string, msgs []llm.Message, output string) chatRunSummary { summary := chatRunSummary{snapshot: stats.Snapshot{Window: time.Hour}} - for _, m := range msgs { - summary.sent += len(m.Content) - } - summary.recv = len(output) - _ = stats.Update(ctx, client.Name(), model, summary.sent, summary.recv) + // Byte accounting + stats.Update are shared via chatrun.Account. + summary.sent, summary.recv = chatrun.Account(ctx, client.Name(), model, msgs, output) snap, err := stats.TakeSnapshot() if err == nil { summary.snapshot = snap diff --git a/internal/hexaicli/run_output_test.go b/internal/hexaicli/run_output_test.go index e61e4b6..07a2cab 100644 --- a/internal/hexaicli/run_output_test.go +++ b/internal/hexaicli/run_output_test.go @@ -227,7 +227,7 @@ func TestRunStreamingChat_StreamError(t *testing.T) { streamErr: fmt.Errorf("stream broken"), } var out bytes.Buffer - _, err := runStreamingChat(context.Background(), client, nil, nil, &out) + _, err := runChatRequest(context.Background(), client, requestArgs{}, nil, &out) if err == nil || !strings.Contains(err.Error(), "stream broken") { t.Fatalf("expected stream error, got %v", err) } @@ -247,7 +247,7 @@ func (s *streamWriteErrClient) ChatStream(_ context.Context, _ []llm.Message, on func TestRunStreamingChat_WriteError(t *testing.T) { client := &streamWriteErrClient{fakeClient: fakeClient{name: "p", model: "m"}} w := errWriter{err: errors.New("write fail")} - _, err := runStreamingChat(context.Background(), client, nil, nil, w) + _, err := runChatRequest(context.Background(), client, requestArgs{}, nil, w) if err == nil || !strings.Contains(err.Error(), "write fail") { t.Fatalf("expected write error, got %v", err) } @@ -298,7 +298,7 @@ func TestEffectiveModel_Whitespace(t *testing.T) { func TestRunSimpleChat_WriteError(t *testing.T) { client := &fakeClient{name: "p", model: "m", resp: "ok"} w := errWriter{err: errors.New("write fail")} - _, err := runSimpleChat(context.Background(), client, nil, nil, w) + _, err := runChatRequest(context.Background(), client, requestArgs{}, nil, w) if err == nil || !strings.Contains(err.Error(), "write fail") { t.Fatalf("expected write error, got %v", err) } @@ -448,7 +448,7 @@ func TestRunSimpleChat_ChatError(t *testing.T) { chatErr: fmt.Errorf("chat broken"), } var out bytes.Buffer - _, err := runSimpleChat(context.Background(), client, nil, nil, &out) + _, err := runChatRequest(context.Background(), client, requestArgs{}, nil, &out) if err == nil || !strings.Contains(err.Error(), "chat broken") { t.Fatalf("expected chat error, got %v", err) } |
