|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState, useRef } from "react" |
| 4 | +import { |
| 5 | + Globe, |
| 6 | + Loader2, |
| 7 | + CheckCircle2, |
| 8 | + ChevronRight, |
| 9 | + ChevronDown, |
| 10 | + Monitor, |
| 11 | +} from "lucide-react" |
| 12 | +import type { ToolRenderProps } from "@/hooks/useToolRenderer" |
| 13 | +import { BrowserLiveView } from "./BrowserLiveView" |
| 14 | + |
| 15 | +/** |
| 16 | + * Browser tool display component. |
| 17 | + * |
| 18 | + * Uses streamEvents length as a React key on the actions container |
| 19 | + * to force re-render when new events arrive. |
| 20 | + */ |
| 21 | +export function BrowserToolDisplay({ |
| 22 | + name, |
| 23 | + args, |
| 24 | + status, |
| 25 | + result, |
| 26 | + liveViewUrl, |
| 27 | + streamEvents, |
| 28 | +}: ToolRenderProps) { |
| 29 | + const [expanded, setExpanded] = useState(false) |
| 30 | + const [showViewer, setShowViewer] = useState(false) |
| 31 | + const stableUrlRef = useRef<string | null>(null) |
| 32 | + const wasAutoExpanded = useRef(false) |
| 33 | + |
| 34 | + // Parse the input args to extract task and URL |
| 35 | + let taskDescription = "" |
| 36 | + let startingUrl = "" |
| 37 | + try { |
| 38 | + const parsedArgs = JSON.parse(args) |
| 39 | + taskDescription = parsedArgs.task || "" |
| 40 | + startingUrl = parsedArgs.starting_url || "" |
| 41 | + } catch { |
| 42 | + taskDescription = args |
| 43 | + } |
| 44 | + |
| 45 | + // Prefer streamed liveViewUrl; fall back to extracting from final result. |
| 46 | + let url: string | null = liveViewUrl ?? null |
| 47 | + if (!url && result) { |
| 48 | + try { |
| 49 | + const parsed = JSON.parse(result) |
| 50 | + if (parsed?.live_view_url && String(parsed.live_view_url).includes("bedrock-agentcore")) { |
| 51 | + url = String(parsed.live_view_url) |
| 52 | + } |
| 53 | + } catch { |
| 54 | + const match = result.match(/https:\/\/bedrock-agentcore\.[^\s"]+live-view[^\s"]+/) |
| 55 | + if (match) url = match[0] |
| 56 | + } |
| 57 | + } |
| 58 | + if (url && !stableUrlRef.current) { |
| 59 | + stableUrlRef.current = url |
| 60 | + } |
| 61 | + const stableUrl = stableUrlRef.current |
| 62 | + |
| 63 | + // Parse browser tool result |
| 64 | + const parsedResult = result ? parseBrowserResult(result) : null |
| 65 | + |
| 66 | + // Extract actions from stream events |
| 67 | + const evtCount = streamEvents?.length ?? 0 |
| 68 | + const liveActions: string[] = [] |
| 69 | + if (streamEvents) { |
| 70 | + for (const ev of streamEvents) { |
| 71 | + if (typeof ev === "object" && ev !== null) { |
| 72 | + const data = ev as Record<string, unknown> |
| 73 | + if (data.type !== "browser_live_view" && data.extracted_content) { |
| 74 | + liveActions.push(String(data.extracted_content)) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const isInProgress = status === "streaming" || status === "executing" |
| 81 | + |
| 82 | + // Auto-expand when first stream event arrives so user sees live actions |
| 83 | + if (isInProgress && liveActions.length > 0 && !wasAutoExpanded.current) { |
| 84 | + wasAutoExpanded.current = true |
| 85 | + if (!expanded) setExpanded(true) |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <div className="my-1 text-sm"> |
| 90 | + {/* Header row */} |
| 91 | + <div className="flex items-center gap-1.5 px-2 py-1 rounded hover:bg-gray-200/50 transition-colors"> |
| 92 | + <button |
| 93 | + onClick={() => setExpanded(!expanded)} |
| 94 | + className="flex items-center gap-1.5 flex-1 text-left" |
| 95 | + > |
| 96 | + {expanded ? ( |
| 97 | + <ChevronDown size={12} className="text-gray-400" /> |
| 98 | + ) : ( |
| 99 | + <ChevronRight size={12} className="text-gray-400" /> |
| 100 | + )} |
| 101 | + <Globe size={12} className="text-blue-500" /> |
| 102 | + <span className="text-blue-700 font-medium">{name}</span> |
| 103 | + {!expanded && ( |
| 104 | + <span className="text-xs text-gray-500 ml-1"> |
| 105 | + {isInProgress |
| 106 | + ? liveActions.length > 0 |
| 107 | + ? `(${liveActions.length} actions)` |
| 108 | + : "(executing...)" |
| 109 | + : status === "complete" |
| 110 | + ? `✓ done${liveActions.length > 0 ? ` (${liveActions.length} actions)` : ""}` |
| 111 | + : ""} |
| 112 | + </span> |
| 113 | + )} |
| 114 | + </button> |
| 115 | + {stableUrl && ( |
| 116 | + <button |
| 117 | + onClick={() => setShowViewer(!showViewer)} |
| 118 | + className="flex items-center gap-1 text-xs text-blue-600 hover:text-blue-800 px-1.5 py-0.5 rounded hover:bg-blue-50 transition-colors" |
| 119 | + > |
| 120 | + <Monitor size={10} /> |
| 121 | + {showViewer ? "Hide viewer" : status === "complete" ? "View session" : "Watch live"} |
| 122 | + </button> |
| 123 | + )} |
| 124 | + {status === "streaming" && <Loader2 size={12} className="animate-spin text-blue-500" />} |
| 125 | + {status === "executing" && <Loader2 size={12} className="animate-spin text-amber-500" />} |
| 126 | + {status === "complete" && <CheckCircle2 size={12} className="text-green-500" />} |
| 127 | + </div> |
| 128 | + |
| 129 | + {/* Expanded content — ONLY visible when expanded */} |
| 130 | + {expanded && ( |
| 131 | + <div key={`actions-${evtCount}-${status}`} className="mx-2 mt-1 space-y-2"> |
| 132 | + {/* 1. Input — task and URL */} |
| 133 | + {args && ( |
| 134 | + <div className="text-xs text-gray-600"> |
| 135 | + {taskDescription && <div><span className="font-medium">Task:</span> {taskDescription}</div>} |
| 136 | + {startingUrl && ( |
| 137 | + <div className="mt-0.5"> |
| 138 | + <span className="font-medium">URL:</span>{" "} |
| 139 | + <a href={startingUrl} target="_blank" rel="noopener noreferrer" className="text-blue-600 hover:underline">{startingUrl}</a> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + )} |
| 144 | + |
| 145 | + {/* 2. Browser actions */} |
| 146 | + {isInProgress && liveActions.length === 0 && ( |
| 147 | + <div className="text-xs text-gray-400 italic">Waiting for browser actions...</div> |
| 148 | + )} |
| 149 | + {liveActions.length > 0 && ( |
| 150 | + <div className="space-y-0.5"> |
| 151 | + <div className="text-xs text-gray-500">Browser actions ({liveActions.length}):</div> |
| 152 | + {liveActions.slice(-5).map((action, i) => ( |
| 153 | + <div |
| 154 | + key={`${evtCount}-${i}`} |
| 155 | + className={`text-xs rounded px-2 py-1 border-l-2 ${ |
| 156 | + isInProgress |
| 157 | + ? "text-gray-600 bg-blue-50 border-blue-300" |
| 158 | + : "text-gray-500 bg-gray-50 border-gray-300" |
| 159 | + }`} |
| 160 | + > |
| 161 | + {action} |
| 162 | + </div> |
| 163 | + ))} |
| 164 | + </div> |
| 165 | + )} |
| 166 | + |
| 167 | + {/* 3. Result */} |
| 168 | + {status === "complete" && parsedResult?.finalContent && ( |
| 169 | + <div className="text-xs text-gray-700 bg-green-50 rounded px-2 py-1.5 border border-green-200"> |
| 170 | + <span className="font-medium text-green-800">✓ </span> |
| 171 | + <span className="line-clamp-3">{parsedResult.finalContent}</span> |
| 172 | + </div> |
| 173 | + )} |
| 174 | + </div> |
| 175 | + )} |
| 176 | + |
| 177 | + {/* 4. DCV live viewer — with spacing */} |
| 178 | + {showViewer && stableUrl && ( |
| 179 | + <div className="mx-2 mt-3 mb-1 rounded overflow-hidden border border-gray-200"> |
| 180 | + <BrowserLiveView presignedUrl={stableUrl} isActive={showViewer} onConnectionError={() => {}} /> |
| 181 | + </div> |
| 182 | + )} |
| 183 | + |
| 184 | + </div> |
| 185 | + ) |
| 186 | +} |
| 187 | + |
| 188 | +/** Parse browser tool result to extract meaningful content. */ |
| 189 | +function parseBrowserResult(result: string): { |
| 190 | + finalContent: string | null |
| 191 | + actions: string[] |
| 192 | + raw: string |
| 193 | +} | null { |
| 194 | + try { |
| 195 | + const parsed = JSON.parse(result) |
| 196 | + let finalContent: string | null = null |
| 197 | + const actions: string[] = [] |
| 198 | + if (parsed.response && typeof parsed.response === "string") { |
| 199 | + const matches = Array.from( |
| 200 | + parsed.response.matchAll(/extracted_content='([^']+(?:''[^']+)*)'/g) |
| 201 | + ) as RegExpMatchArray[] |
| 202 | + const contents = matches.map((m: RegExpMatchArray) => m[1].replace(/''/g, "'")) |
| 203 | + if (contents.length > 0) { |
| 204 | + finalContent = contents[contents.length - 1] |
| 205 | + for (let i = 0; i < contents.length - 1; i++) { |
| 206 | + if (contents[i] && !contents[i].includes("AgentHistoryList")) actions.push(contents[i]) |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + return { finalContent, actions, raw: result } |
| 211 | + } catch { |
| 212 | + return { finalContent: null, actions: [], raw: result } |
| 213 | + } |
| 214 | +} |
0 commit comments