|
| 1 | +import React from "react"; |
| 2 | +import type { WebFetchToolArgs, WebFetchToolResult } from "@/common/types/tools"; |
| 3 | +import { |
| 4 | + ToolContainer, |
| 5 | + ToolHeader, |
| 6 | + ExpandIcon, |
| 7 | + StatusIndicator, |
| 8 | + ToolDetails, |
| 9 | + DetailSection, |
| 10 | + DetailLabel, |
| 11 | + LoadingDots, |
| 12 | +} from "./shared/ToolPrimitives"; |
| 13 | +import { useToolExpansion, getStatusDisplay, type ToolStatus } from "./shared/toolUtils"; |
| 14 | +import { TooltipWrapper, Tooltip } from "../Tooltip"; |
| 15 | +import { MarkdownRenderer } from "../Messages/MarkdownRenderer"; |
| 16 | + |
| 17 | +interface WebFetchToolCallProps { |
| 18 | + args: WebFetchToolArgs; |
| 19 | + result?: WebFetchToolResult; |
| 20 | + status?: ToolStatus; |
| 21 | +} |
| 22 | + |
| 23 | +function formatBytes(bytes: number): string { |
| 24 | + if (bytes < 1024) return `${bytes}B`; |
| 25 | + if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`; |
| 26 | + return `${(bytes / (1024 * 1024)).toFixed(1)}MB`; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Extract domain from URL for compact display |
| 31 | + */ |
| 32 | +function getDomain(url: string): string { |
| 33 | + try { |
| 34 | + const parsed = new URL(url); |
| 35 | + return parsed.hostname; |
| 36 | + } catch { |
| 37 | + return url; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export const WebFetchToolCall: React.FC<WebFetchToolCallProps> = ({ |
| 42 | + args, |
| 43 | + result, |
| 44 | + status = "pending", |
| 45 | +}) => { |
| 46 | + const { expanded, toggleExpanded } = useToolExpansion(); |
| 47 | + |
| 48 | + const domain = getDomain(args.url); |
| 49 | + |
| 50 | + return ( |
| 51 | + <ToolContainer expanded={expanded} className="@container"> |
| 52 | + <ToolHeader onClick={toggleExpanded}> |
| 53 | + <ExpandIcon expanded={expanded}>▶</ExpandIcon> |
| 54 | + <TooltipWrapper inline> |
| 55 | + <span>🌐</span> |
| 56 | + <Tooltip>web_fetch</Tooltip> |
| 57 | + </TooltipWrapper> |
| 58 | + <div className="text-text flex max-w-96 min-w-0 items-center gap-1.5"> |
| 59 | + <span className="font-monospace truncate">{domain}</span> |
| 60 | + </div> |
| 61 | + {result && result.success && ( |
| 62 | + <span className="text-secondary ml-2 text-[10px] whitespace-nowrap"> |
| 63 | + <span className="hidden @sm:inline">fetched </span> |
| 64 | + {formatBytes(result.length)} |
| 65 | + </span> |
| 66 | + )} |
| 67 | + <StatusIndicator status={status}>{getStatusDisplay(status)}</StatusIndicator> |
| 68 | + </ToolHeader> |
| 69 | + |
| 70 | + {expanded && ( |
| 71 | + <ToolDetails> |
| 72 | + <DetailSection> |
| 73 | + <div className="bg-code-bg flex flex-wrap gap-4 rounded px-2 py-1.5 text-[11px] leading-[1.4]"> |
| 74 | + <div className="flex min-w-0 gap-1.5"> |
| 75 | + <span className="text-secondary font-medium">URL:</span> |
| 76 | + <a |
| 77 | + href={args.url} |
| 78 | + target="_blank" |
| 79 | + rel="noopener noreferrer" |
| 80 | + className="text-link font-monospace truncate hover:underline" |
| 81 | + > |
| 82 | + {args.url} |
| 83 | + </a> |
| 84 | + </div> |
| 85 | + {result && result.success && result.title && ( |
| 86 | + <div className="flex min-w-0 gap-1.5"> |
| 87 | + <span className="text-secondary font-medium">Title:</span> |
| 88 | + <span className="text-text truncate">{result.title}</span> |
| 89 | + </div> |
| 90 | + )} |
| 91 | + </div> |
| 92 | + </DetailSection> |
| 93 | + |
| 94 | + {result && ( |
| 95 | + <> |
| 96 | + {result.success === false && result.error && ( |
| 97 | + <DetailSection> |
| 98 | + <DetailLabel>Error</DetailLabel> |
| 99 | + <div className="text-danger bg-danger-overlay border-danger rounded border-l-2 px-2 py-1.5 text-[11px]"> |
| 100 | + {result.error} |
| 101 | + </div> |
| 102 | + </DetailSection> |
| 103 | + )} |
| 104 | + |
| 105 | + {result.success && result.content && ( |
| 106 | + <DetailSection> |
| 107 | + <DetailLabel>Content</DetailLabel> |
| 108 | + <div className="bg-code-bg max-h-[300px] overflow-y-auto rounded px-3 py-2 text-[12px]"> |
| 109 | + <MarkdownRenderer content={result.content} /> |
| 110 | + </div> |
| 111 | + </DetailSection> |
| 112 | + )} |
| 113 | + </> |
| 114 | + )} |
| 115 | + |
| 116 | + {status === "executing" && !result && ( |
| 117 | + <DetailSection> |
| 118 | + <div className="text-secondary text-[11px]"> |
| 119 | + Fetching page |
| 120 | + <LoadingDots /> |
| 121 | + </div> |
| 122 | + </DetailSection> |
| 123 | + )} |
| 124 | + </ToolDetails> |
| 125 | + )} |
| 126 | + </ToolContainer> |
| 127 | + ); |
| 128 | +}; |
0 commit comments