Backend: - Add unified URL fetcher with fallback chain: curl → wget → native Go → headless Chrome - Implement JS-rendered page detection for sites like docs.rs - Add chromedp dependency for headless browser support - Log fetch method on server startup Frontend: - Store tool results in structured ToolCall.result field instead of message content - Show tool results collapsed by default in ToolCallDisplay - Add expandable results section with truncation for large outputs - Add Message.hidden flag for internal messages (tool context) - Separate visibleMessages (UI) from allMessages (API) to fix infinite loop - Fix tool result messages not being sent to model 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// URLFetchRequest represents a request to fetch a URL
|
|
type URLFetchRequest struct {
|
|
URL string `json:"url" binding:"required"`
|
|
MaxLength int `json:"maxLength"`
|
|
}
|
|
|
|
// URLFetchProxyHandler returns a handler that fetches URLs for the frontend
|
|
// This bypasses CORS restrictions for the fetch_url tool
|
|
// Uses curl/wget when available for better compatibility, falls back to native Go
|
|
func URLFetchProxyHandler() gin.HandlerFunc {
|
|
fetcher := GetFetcher()
|
|
|
|
return func(c *gin.Context) {
|
|
var req URLFetchRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
// Validate URL
|
|
parsedURL, err := url.Parse(req.URL)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid URL: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
// Only allow HTTP/HTTPS
|
|
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "only HTTP and HTTPS URLs are supported"})
|
|
return
|
|
}
|
|
|
|
// Set up fetch options
|
|
opts := DefaultFetchOptions()
|
|
opts.Timeout = 30 * time.Second
|
|
|
|
// Set max length (default 500KB)
|
|
if req.MaxLength > 0 && req.MaxLength <= 500000 {
|
|
opts.MaxLength = req.MaxLength
|
|
}
|
|
|
|
// Fetch the URL
|
|
result, err := fetcher.Fetch(c.Request.Context(), req.URL, opts)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to fetch URL: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
// Check status
|
|
if result.StatusCode >= 400 {
|
|
c.JSON(http.StatusBadGateway, gin.H{
|
|
"error": "HTTP " + http.StatusText(result.StatusCode),
|
|
"status": result.StatusCode,
|
|
})
|
|
return
|
|
}
|
|
|
|
// Return the content
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"content": result.Content,
|
|
"contentType": result.ContentType,
|
|
"url": result.FinalURL,
|
|
"status": result.StatusCode,
|
|
"fetchMethod": string(result.Method),
|
|
})
|
|
}
|
|
}
|
|
|
|
// GetFetchMethodHandler returns a handler that reports the current fetch method
|
|
func GetFetchMethodHandler() gin.HandlerFunc {
|
|
fetcher := GetFetcher()
|
|
|
|
return func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"method": string(fetcher.Method()),
|
|
"hasChrome": fetcher.HasChrome(),
|
|
})
|
|
}
|
|
}
|