1package tools
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "net/http"
8 "os"
9 "path/filepath"
10 "strings"
11 "time"
12
13 "github.com/charmbracelet/crush/internal/ai"
14 "github.com/charmbracelet/crush/internal/permission"
15)
16
17type DownloadParams struct {
18 URL string `json:"url" description:"The URL to download from"`
19 FilePath string `json:"file_path" description:"The local file path where the downloaded content should be saved"`
20 Timeout int `json:"timeout,omitempty" description:"Optional timeout in seconds (max 600)"`
21}
22
23type DownloadPermissionsParams struct {
24 URL string `json:"url"`
25 FilePath string `json:"file_path"`
26 Timeout int `json:"timeout,omitempty"`
27}
28
29const (
30 DownloadToolName = "download"
31)
32
33func NewDownloadTool(permissions permission.Service, workingDir string) ai.AgentTool {
34 client := &http.Client{
35 Timeout: 5 * time.Minute, // Default 5 minute timeout for downloads
36 Transport: &http.Transport{
37 MaxIdleConns: 100,
38 MaxIdleConnsPerHost: 10,
39 IdleConnTimeout: 90 * time.Second,
40 },
41 }
42 return ai.NewTypedToolFunc(
43 DownloadToolName,
44 `Downloads binary data from a URL and saves it to a local file.
45
46WHEN TO USE THIS TOOL:
47- Use when you need to download files, images, or other binary data from URLs
48- Helpful for downloading assets, documents, or any file type
49- Useful for saving remote content locally for processing or storage
50
51HOW TO USE:
52- Provide the URL to download from
53- Specify the local file path where the content should be saved
54- Optionally set a timeout for the request
55
56FEATURES:
57- Downloads any file type (binary or text)
58- Automatically creates parent directories if they don't exist
59- Handles large files efficiently with streaming
60- Sets reasonable timeouts to prevent hanging
61- Validates input parameters before making requests
62
63LIMITATIONS:
64- Maximum file size is 100MB
65- Only supports HTTP and HTTPS protocols
66- Cannot handle authentication or cookies
67- Some websites may block automated requests
68- Will overwrite existing files without warning
69
70TIPS:
71- Use absolute paths or paths relative to the working directory
72- Set appropriate timeouts for large files or slow connections`,
73 func(ctx context.Context, params DownloadParams, call ai.ToolCall) (ai.ToolResponse, error) {
74 if params.URL == "" {
75 return ai.NewTextErrorResponse("URL parameter is required"), nil
76 }
77
78 if params.FilePath == "" {
79 return ai.NewTextErrorResponse("file_path parameter is required"), nil
80 }
81
82 if !strings.HasPrefix(params.URL, "http://") && !strings.HasPrefix(params.URL, "https://") {
83 return ai.NewTextErrorResponse("URL must start with http:// or https://"), nil
84 }
85
86 // Convert relative path to absolute path
87 var filePath string
88 if filepath.IsAbs(params.FilePath) {
89 filePath = params.FilePath
90 } else {
91 filePath = filepath.Join(workingDir, params.FilePath)
92 }
93
94 sessionID, messageID := GetContextValues(ctx)
95 if sessionID == "" || messageID == "" {
96 return ai.ToolResponse{}, fmt.Errorf("session ID and message ID are required for the download tool")
97 }
98 granted := permissions.Request(
99 permission.CreatePermissionRequest{
100 SessionID: sessionID,
101 Path: filePath,
102 ToolName: DownloadToolName,
103 Action: "download",
104 Description: fmt.Sprintf("Download file from URL: %s to %s", params.URL, filePath),
105 Params: DownloadPermissionsParams(params),
106 },
107 )
108
109 if !granted {
110 return ai.ToolResponse{}, permission.ErrorPermissionDenied
111 }
112
113 // Handle timeout with context
114 requestCtx := ctx
115 if params.Timeout > 0 {
116 maxTimeout := 600 // 10 minutes
117 if params.Timeout > maxTimeout {
118 params.Timeout = maxTimeout
119 }
120 var cancel context.CancelFunc
121 requestCtx, cancel = context.WithTimeout(ctx, time.Duration(params.Timeout)*time.Second)
122 defer cancel()
123 }
124
125 req, err := http.NewRequestWithContext(requestCtx, "GET", params.URL, nil)
126 if err != nil {
127 return ai.ToolResponse{}, fmt.Errorf("failed to create request: %w", err)
128 }
129
130 req.Header.Set("User-Agent", "crush/1.0")
131
132 resp, err := client.Do(req)
133 if err != nil {
134 return ai.ToolResponse{}, fmt.Errorf("failed to download from URL: %w", err)
135 }
136 defer resp.Body.Close()
137
138 if resp.StatusCode != http.StatusOK {
139 return ai.NewTextErrorResponse(fmt.Sprintf("Request failed with status code: %d", resp.StatusCode)), nil
140 }
141
142 // Check content length if available
143 maxSize := int64(100 * 1024 * 1024) // 100MB
144 if resp.ContentLength > maxSize {
145 return ai.NewTextErrorResponse(fmt.Sprintf("File too large: %d bytes (max %d bytes)", resp.ContentLength, maxSize)), nil
146 }
147
148 // Create parent directories if they don't exist
149 if err := os.MkdirAll(filepath.Dir(filePath), 0o755); err != nil {
150 return ai.ToolResponse{}, fmt.Errorf("failed to create parent directories: %w", err)
151 }
152
153 // Create the output file
154 outFile, err := os.Create(filePath)
155 if err != nil {
156 return ai.ToolResponse{}, fmt.Errorf("failed to create output file: %w", err)
157 }
158 defer outFile.Close()
159
160 // Copy data with size limit
161 limitedReader := io.LimitReader(resp.Body, maxSize)
162 bytesWritten, err := io.Copy(outFile, limitedReader)
163 if err != nil {
164 return ai.ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
165 }
166
167 // Check if we hit the size limit
168 if bytesWritten == maxSize {
169 // Clean up the file since it might be incomplete
170 os.Remove(filePath)
171 return ai.NewTextErrorResponse(fmt.Sprintf("File too large: exceeded %d bytes limit", maxSize)), nil
172 }
173
174 contentType := resp.Header.Get("Content-Type")
175 responseMsg := fmt.Sprintf("Successfully downloaded %d bytes to %s", bytesWritten, filePath)
176 if contentType != "" {
177 responseMsg += fmt.Sprintf(" (Content-Type: %s)", contentType)
178 }
179
180 return ai.NewTextResponse(responseMsg), nil
181 })
182}