bash_kill.go

 1package tools
 2
 3import (
 4	"context"
 5	_ "embed"
 6	"fmt"
 7
 8	"charm.land/fantasy"
 9	"github.com/charmbracelet/crush/internal/shell"
10)
11
12const (
13	BashKillToolName = "bash_kill"
14)
15
16//go:embed bash_kill.md
17var bashKillDescription []byte
18
19type BashKillParams struct {
20	ShellID string `json:"shell_id" description:"The ID of the background shell to terminate"`
21}
22
23type BashKillResponseMetadata struct {
24	ShellID string `json:"shell_id"`
25}
26
27func NewBashKillTool() fantasy.AgentTool {
28	return fantasy.NewAgentTool(
29		BashKillToolName,
30		string(bashKillDescription),
31		func(ctx context.Context, params BashKillParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
32			if params.ShellID == "" {
33				return fantasy.NewTextErrorResponse("missing shell_id"), nil
34			}
35
36			bgManager := shell.GetBackgroundShellManager()
37			err := bgManager.Kill(params.ShellID)
38			if err != nil {
39				return fantasy.NewTextErrorResponse(err.Error()), nil
40			}
41
42			metadata := BashKillResponseMetadata(params)
43
44			result := fmt.Sprintf("Background shell %s terminated successfully", params.ShellID)
45			return fantasy.WithResponseMetadata(fantasy.NewTextResponse(result), metadata), nil
46		})
47}