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/hexaicli/run_output_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'internal/hexaicli/run_output_test.go') 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) } -- cgit v1.2.3