feat(tools): stream shell_run output live into the TUI
This commit is contained in:
+40
-5
@@ -1,6 +1,7 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -8,6 +9,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"agentu/pkg/llm"
|
||||
)
|
||||
@@ -46,6 +48,10 @@ type shellRunArgs struct {
|
||||
}
|
||||
|
||||
func (t *ShellRunTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
|
||||
return t.ExecuteStream(ctx, raw, nil)
|
||||
}
|
||||
|
||||
func (t *ShellRunTool) ExecuteStream(ctx context.Context, raw json.RawMessage, emit func(string) error) (string, error) {
|
||||
var args shellRunArgs
|
||||
if err := json.Unmarshal(raw, &args); err != nil {
|
||||
return "", fmt.Errorf("parse arguments: %w", err)
|
||||
@@ -66,16 +72,45 @@ func (t *ShellRunTool) Execute(ctx context.Context, raw json.RawMessage) (string
|
||||
cmd := exec.CommandContext(ctx, shell, "-lc", args.Command)
|
||||
cmd.Dir = workdir
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
text := FormatResult(string(output))
|
||||
var mu sync.Mutex
|
||||
var combined bytes.Buffer
|
||||
writer := &shellStreamWriter{mu: &mu, buf: &combined, emit: emit}
|
||||
cmd.Stdout = writer
|
||||
cmd.Stderr = writer
|
||||
|
||||
runErr := cmd.Run()
|
||||
text := FormatResult(combined.String())
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
return text, fmt.Errorf("command timed out: %w", ctx.Err())
|
||||
}
|
||||
if err != nil {
|
||||
if runErr != nil {
|
||||
if text != "" {
|
||||
return text, fmt.Errorf("command failed: %w", err)
|
||||
return text, fmt.Errorf("command failed: %w", runErr)
|
||||
}
|
||||
return "", fmt.Errorf("command failed: %w", err)
|
||||
return "", fmt.Errorf("command failed: %w", runErr)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
// shellStreamWriter captures command output for the final result while
|
||||
// forwarding every write to the streaming callback as it happens.
|
||||
type shellStreamWriter struct {
|
||||
mu *sync.Mutex
|
||||
buf *bytes.Buffer
|
||||
emit func(string) error
|
||||
}
|
||||
|
||||
func (w *shellStreamWriter) Write(p []byte) (int, error) {
|
||||
w.mu.Lock()
|
||||
n, writeErr := w.buf.Write(p)
|
||||
w.mu.Unlock()
|
||||
if writeErr != nil {
|
||||
return n, writeErr
|
||||
}
|
||||
if w.emit != nil {
|
||||
if err := w.emit(string(p)); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShellRunStreamsOutputWhileRunning(t *testing.T) {
|
||||
tool := NewShellRunTool(t.TempDir())
|
||||
var mu sync.Mutex
|
||||
var chunks []string
|
||||
output, err := tool.ExecuteStream(context.Background(), json.RawMessage(`{"command":"printf 'a\\n'; sleep 0.05; printf 'b\\n'"}`), func(chunk string) error {
|
||||
mu.Lock()
|
||||
chunks = append(chunks, chunk)
|
||||
mu.Unlock()
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output, "a") || !strings.Contains(output, "b") {
|
||||
t.Fatalf("output = %q", output)
|
||||
}
|
||||
mu.Lock()
|
||||
joined := strings.Join(chunks, "")
|
||||
mu.Unlock()
|
||||
if len(chunks) == 0 {
|
||||
t.Fatal("expected at least one streamed chunk")
|
||||
}
|
||||
if !strings.Contains(joined, "a") || !strings.Contains(joined, "b") {
|
||||
t.Fatalf("streamed chunks = %q", joined)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellRunExecuteCollectsFullOutput(t *testing.T) {
|
||||
tool := NewShellRunTool(t.TempDir())
|
||||
output, err := tool.Execute(context.Background(), json.RawMessage(`{"command":"printf 'hello world'"}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.TrimSpace(output) != "hello world" {
|
||||
t.Fatalf("output = %q", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellRunStreamsStderrAndStdout(t *testing.T) {
|
||||
tool := NewShellRunTool(t.TempDir())
|
||||
var mu sync.Mutex
|
||||
var chunks []string
|
||||
output, err := tool.ExecuteStream(context.Background(), json.RawMessage(`{"command":"echo out; echo err >&2"}`), func(chunk string) error {
|
||||
mu.Lock()
|
||||
chunks = append(chunks, chunk)
|
||||
mu.Unlock()
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(output, "out") || !strings.Contains(output, "err") {
|
||||
t.Fatalf("output = %q", output)
|
||||
}
|
||||
mu.Lock()
|
||||
joined := strings.Join(chunks, "")
|
||||
mu.Unlock()
|
||||
if !strings.Contains(joined, "out") || !strings.Contains(joined, "err") {
|
||||
t.Fatalf("streamed chunks = %q", joined)
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,15 @@ type Tool interface {
|
||||
Execute(ctx context.Context, args json.RawMessage) (string, error)
|
||||
}
|
||||
|
||||
// StreamingTool is optionally implemented by tools that can report partial
|
||||
// output while they execute. emit receives chunks of the tool's output as
|
||||
// they are produced; the returned string is still the complete result that
|
||||
// gets sent back to the model.
|
||||
type StreamingTool interface {
|
||||
Tool
|
||||
ExecuteStream(ctx context.Context, args json.RawMessage, emit func(string) error) (string, error)
|
||||
}
|
||||
|
||||
// MutatingTool is optionally implemented by tools that modify external state.
|
||||
// It is used by the agent to determine whether tool calls can run in parallel.
|
||||
type MutatingTool interface {
|
||||
|
||||
Reference in New Issue
Block a user