download.go

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