Show shell tool calls as the trimmed full command
Goose's titles cut the command at ~40 characters, mostly spent on cd. Use rawInput's command, strip cd/timeout/nix develop -c/env prefixes, tag the directory and clip at 80 characters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0119iKoN3fSvRGUAYHupPW4U
This commit is contained in:
1 parent
8c83a3316a
commit
4db99eaa2a
4 files changed
+140
-2
No files matched your search
@@ -12,6 +12,7 @@ go_library(
|
||||
"listen.go",
|
||||
"manage.go",
|
||||
"stream.go",
|
||||
"toolline.go",
|
||||
"update.go",
|
||||
"version.go",
|
||||
],
|
||||
@@ -43,6 +44,7 @@ go_test(
|
||||
"bot_test.go",
|
||||
"inflight_test.go",
|
||||
"lifecycle_test.go",
|
||||
"toolline_test.go",
|
||||
"update_test.go",
|
||||
"version_test.go",
|
||||
],
|
||||
|
||||
@@ -64,11 +64,12 @@ func (s *Streamer) Update(u acp.Update) {
|
||||
if u.Title == "" {
|
||||
return
|
||||
}
|
||||
s.status = "Running " + u.Title
|
||||
line := toolLine(u)
|
||||
s.status = "Running " + line
|
||||
if t := s.full.String(); t != "" && !strings.HasSuffix(t, "\n") {
|
||||
s.full.WriteString("\n")
|
||||
}
|
||||
s.full.WriteString("> :wrench: " + u.Title + "\n")
|
||||
s.full.WriteString("> :wrench: " + line + "\n")
|
||||
case "tool_call_update":
|
||||
if u.Status == "completed" || u.Status == "failed" {
|
||||
s.status = "Thinking"
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"slackerbot/internal/acp"
|
||||
)
|
||||
|
||||
const toolLineMax = 80 // runes of a command shown in the reply
|
||||
|
||||
// Prefixes that say little about what a command does. Each match is removed
|
||||
// from the start, repeatedly, so `cd x && timeout 60 nix develop -c go test`
|
||||
// shows as `go test`.
|
||||
var (
|
||||
cdPrefix = regexp.MustCompile(`^cd\s+("[^"]*"|'[^']*'|\S+)\s*(&&|;)\s*`)
|
||||
timeoutPrefix = regexp.MustCompile(`^timeout\s+(-\S+\s+(\d\S*\s+)?)*\d+[smhd]?\s+`)
|
||||
developPrefix = regexp.MustCompile(`^nix\s+develop\b[^&|;]*?\s(-c|--command)\s+`)
|
||||
envPrefix = regexp.MustCompile(`^(env\s+)?([A-Za-z_]\w*=("[^"]*"|'[^']*'|\S*)\s+)+`)
|
||||
spaces = regexp.MustCompile(`\s+`)
|
||||
)
|
||||
|
||||
// toolLine is how a tool call shows in the reply. Goose's titles for shell
|
||||
// calls are the command cut at ~40 characters, mostly spent on `cd …`, so
|
||||
// those use the full command from rawInput with the noise stripped.
|
||||
func toolLine(u acp.Update) string {
|
||||
if cmd, ok := shellCommand(u); ok {
|
||||
cmd, dir := trimCommand(cmd)
|
||||
line := "`" + clip(strings.ReplaceAll(cmd, "`", "'"), toolLineMax) + "`"
|
||||
if dir != "" && dir != "/root" && dir != "~" {
|
||||
line += " (" + path.Base(dir) + ")"
|
||||
}
|
||||
return line
|
||||
}
|
||||
// "todo: todo write" → "todo write"
|
||||
if name, rest, ok := strings.Cut(u.Title, ": "); ok && strings.HasPrefix(rest, name+" ") {
|
||||
return rest
|
||||
}
|
||||
return u.Title
|
||||
}
|
||||
|
||||
func shellCommand(u acp.Update) (string, bool) {
|
||||
if !strings.HasSuffix(u.ToolName, "shell") && !strings.HasPrefix(u.Title, "shell") {
|
||||
return "", false
|
||||
}
|
||||
var in struct {
|
||||
Command string `json:"command"`
|
||||
}
|
||||
if json.Unmarshal(u.RawInput, &in) != nil || strings.TrimSpace(in.Command) == "" {
|
||||
return "", false
|
||||
}
|
||||
return in.Command, true
|
||||
}
|
||||
|
||||
// trimCommand strips the prefixes above and returns the last directory cd'd into.
|
||||
func trimCommand(cmd string) (string, string) {
|
||||
cmd = strings.TrimSpace(spaces.ReplaceAllString(cmd, " "))
|
||||
orig, dir := cmd, ""
|
||||
for {
|
||||
if cmd == "" {
|
||||
return orig, ""
|
||||
}
|
||||
if m := cdPrefix.FindStringSubmatch(cmd); m != nil {
|
||||
dir = strings.Trim(m[1], `"'`)
|
||||
cmd = cmd[len(m[0]):]
|
||||
continue
|
||||
}
|
||||
rest := cmd
|
||||
for _, re := range []*regexp.Regexp{timeoutPrefix, developPrefix, envPrefix} {
|
||||
if m := re.FindString(rest); m != "" {
|
||||
rest = rest[len(m):]
|
||||
break
|
||||
}
|
||||
}
|
||||
if rest == cmd || strings.TrimSpace(rest) == "" {
|
||||
return cmd, dir
|
||||
}
|
||||
cmd = rest
|
||||
}
|
||||
}
|
||||
|
||||
func clip(s string, max int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= max {
|
||||
return s
|
||||
}
|
||||
return strings.TrimSpace(string(r[:max-1])) + "…"
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"slackerbot/internal/acp"
|
||||
)
|
||||
|
||||
func shell(cmd string) acp.Update {
|
||||
in, _ := json.Marshal(map[string]string{"command": cmd})
|
||||
return acp.Update{Kind: "tool_call", ToolName: "developer__shell", Title: "shell · " + cmd[:min(len(cmd), 40)] + "...", RawInput: in}
|
||||
}
|
||||
|
||||
func TestToolLine(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
u acp.Update
|
||||
want string
|
||||
}{
|
||||
{shell("cd /root/slacker-bot && ls -la && cat README.md"), "`ls -la && cat README.md` (slacker-bot)"},
|
||||
{shell("cd /root && git clone https://gitea.skyg.ca/ari/slacker-bot"), "`git clone https://gitea.skyg.ca/ari/slacker-bot`"},
|
||||
{shell("cd /root/slacker-bot && timeout 1800 nix build .#slackerbot"), "`nix build .#slackerbot` (slacker-bot)"},
|
||||
{shell("cd /root/x; cd sub && nix develop .#go --impure -c go test ./..."), "`go test ./...` (sub)"},
|
||||
{shell("timeout -k 5 300 env CGO_ENABLED=0 GOOS=linux go build"), "`go build`"},
|
||||
{shell("ls -la /nix 2>&1; echo \"--- findmnt\"\nfindmnt"), "`ls -la /nix 2>&1; echo \"--- findmnt\" findmnt`"},
|
||||
{shell("cd /tmp && "), "`cd /tmp &&`"},
|
||||
{shell("echo `date`"), "`echo 'date'`"},
|
||||
// No usable rawInput: goose's title as before.
|
||||
{acp.Update{ToolName: "developer__shell", Title: "shell · ls"}, "shell · ls"},
|
||||
{acp.Update{ToolName: "todo__todo_write", Title: "todo: todo write"}, "todo write"},
|
||||
{acp.Update{Title: "slackerbot: send file"}, "slackerbot: send file"},
|
||||
} {
|
||||
if got := toolLine(c.u); got != c.want {
|
||||
t.Errorf("toolLine(%q) = %q, want %q", c.u.RawInput, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolLineClips(t *testing.T) {
|
||||
got := toolLine(shell("cd /root && echo " + strings.Repeat("é", 200)))
|
||||
if r := []rune(got); len(r) != toolLineMax+2 || !strings.HasSuffix(got, "…`") {
|
||||
t.Errorf("got %q (%d runes), want %d runes ending in …", got, len(r), toolLineMax+2)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user