on main help sign in
chore: improve shell output and summary to include cwd
commitc8e2938de64b146ca8d7c029e304a279cd5f9307 authorKujtim Hoxha <kujtimii.h@gmail.com> dateTue 22 Jul 2025, 14:07 (1 year ago) parentbb3b380
3 files changed+30−19
internal/llm/agent/agent.go+3 internal/llm/tools/bash.go+16−17 internal/tui/components/chat/messages/renderer.go+11−2
internal/llm/agent/agent.go+3
@@ -22,6 +22,7 @@ import (
2222 "github.com/charmbracelet/crush/internal/permission"
2323 "github.com/charmbracelet/crush/internal/pubsub"
2424 "github.com/charmbracelet/crush/internal/session"
25+ "github.com/charmbracelet/crush/internal/shell"
2526)
2627
2728// Common errors
@@ -762,6 +763,8 @@ func (a *agent) Summarize(ctx context.Context, sessionID string) error {
762763 a.Publish(pubsub.CreatedEvent, event)
763764 return
764765 }
766+ shell := shell.GetPersistentShell(config.Get().WorkingDir())
767+ summary += "\n\n**Current working directory of the persistent shell**\n\n" + shell.GetWorkingDir()
765768 event = AgentEvent{
766769 Type: AgentEventTypeSummarize,
767770 Progress: "Creating new session...",
internal/llm/tools/bash.go+16−17
@@ -4,7 +4,6 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7− "log/slog"
87 "strings"
98 "time"
109
@@ -23,8 +22,10 @@ type BashPermissionsParams struct {
2322}
2423
2524type BashResponseMetadata struct {
26− StartTime int64 `json:"start_time"`
27− EndTime int64 `json:"end_time"`
25+ StartTime int64 `json:"start_time"`
26+ EndTime int64 `json:"end_time"`
27+ Output string `json:"output"`
28+ WorkingDirectory string `json:"working_directory"`
2829}
2930type bashTool struct {
3031 permissions permission.Service
@@ -146,6 +147,7 @@ Before executing the command, please follow these steps:
1461475. Return Result:
147148 - Provide the processed output of the command.
148149 - If any errors occurred during execution, include those in the output.
150+ - The result will also have metadata like the cwd (current working directory) at the end, included with <cwd></cwd> tags.
149151
150152Usage notes:
151153- The command argument is required.
@@ -389,9 +391,12 @@ func (b *bashTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
389391 ctx, cancel = context.WithTimeout(ctx, time.Duration(params.Timeout)*time.Millisecond)
390392 defer cancel()
391393 }
392− stdout, stderr, err := shell.
393− GetPersistentShell(b.workingDir).
394− Exec(ctx, params.Command)
394+
395+ persistentShell := shell.GetPersistentShell(b.workingDir)
396+ stdout, stderr, err := persistentShell.Exec(ctx, params.Command)
397+
398+ // Get the current working directory after command execution
399+ currentWorkingDir := persistentShell.GetWorkingDir()
395400 interrupted := shell.IsInterrupt(err)
396401 exitCode := shell.ExitCode(err)
397402 if exitCode == 0 && !interrupted && err != nil {
@@ -401,15 +406,6 @@ func (b *bashTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
401406 stdout = truncateOutput(stdout)
402407 stderr = truncateOutput(stderr)
403408
404− slog.Info("Bash command executed",
405− "command", params.Command,
406− "stdout", stdout,
407− "stderr", stderr,
408− "exit_code", exitCode,
409− "interrupted", interrupted,
410− "err", err,
411− )
412−
413409 errorMessage := stderr
414410 if errorMessage == "" && err != nil {
415411 errorMessage = err.Error()
@@ -438,9 +434,12 @@ func (b *bashTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
438434 }
439435
440436 metadata := BashResponseMetadata{
441− StartTime: startTime.UnixMilli(),
442− EndTime: time.Now().UnixMilli(),
437+ StartTime: startTime.UnixMilli(),
438+ EndTime: time.Now().UnixMilli(),
439+ Output: stdout,
440+ WorkingDirectory: currentWorkingDir,
443441 }
442+ stdout += fmt.Sprintf("\n\n<cwd>%s</cwd>", currentWorkingDir)
444443 if stdout == "" {
445444 return WithResponseMetadata(NewTextResponse(BashNoOutput), metadata), nil
446445 }
internal/tui/components/chat/messages/renderer.go+11−2
@@ -212,10 +212,19 @@ func (br bashRenderer) Render(v *toolCallCmp) string {
212212 args := newParamBuilder().addMain(cmd).build()
213213
214214 return br.renderWithParams(v, "Bash", args, func() string {
215− if v.result.Content == tools.BashNoOutput {
215+ var meta tools.BashResponseMetadata
216+ if err := br.unmarshalParams(v.result.Metadata, &meta); err != nil {
217+ return renderPlainContent(v, v.result.Content)
218+ }
219+ // for backwards compatibility with older tool calls.
220+ if meta.Output == "" && v.result.Content != tools.BashNoOutput {
221+ meta.Output = v.result.Content
222+ }
223+
224+ if meta.Output == "" {
216225 return ""
217226 }
218− return renderPlainContent(v, v.result.Content)
227+ return renderPlainContent(v, meta.Output)
219228 })
220229}
221230