All files / src/components/mcp/chat-display/utils mcpRichUtil.ts

8.75% Statements 28/320
100% Branches 0/0
0% Functions 0/11
8.75% Lines 28/320

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 4131x 1x                                     1x 1x                                             1x 1x       1x 1x                 1x 1x                                 1x 1x                                                                       1x 1x                                 1x 1x                                                                                                             1x         1x 1x                                                                       1x             1x 1x                                                                                                               1x               1x 1x                                                                   1x         1x 1x                                                                                                                                          
import { StringRequest } from "@shared/proto/cline/common"
import { WebServiceClient } from "@/services/grpc-client"
 
// Represents a URL found in the text with its position and metadata
export interface UrlMatch {
	url: string // The actual URL
	fullMatch: string // The full matched text
	index: number // Position in the text
	isImage: boolean // Whether this URL is an image
	isProcessed: boolean // Whether we've already processed this URL (to avoid duplicates)
}
 
// Display segment interface
export interface DisplaySegment {
	type: "text" | "url" | "image" | "link" | "error"
	content: string
	url?: string
	key: string // Pre-computed key for React
}
 
// Safely create a URL object with error handling and ensure HTTPS
export const safeCreateUrl = (url: string): URL | null => {
	try {
		// Convert HTTP to HTTPS for security
		if (url.startsWith("http://")) {
			url = url.replace("http://", "https://")
		}
 
		return new URL(url)
	} catch (_e) {
		// If the URL doesn't have a protocol, add https://
		if (!url.startsWith("https://")) {
			try {
				return new URL(`https://${url}`)
			} catch (_e) {
				console.log(`Invalid URL: ${url}`)
				return null
			}
		}
		console.log(`Invalid URL: ${url}`)
		return null
	}
}
 
// Check if a string is a valid URL
export const isUrl = (str: string): boolean => {
	return safeCreateUrl(str) !== null
}
 
// Get hostname safely
export const getSafeHostname = (url: string): string => {
	try {
		const urlObj = safeCreateUrl(url)
		return urlObj ? urlObj.hostname : "unknown-host"
	} catch (_e) {
		return "unknown-host"
	}
}
 
// Check if a URL is a localhost URL by examining the hostname
export const isLocalhostUrl = (url: string): boolean => {
	try {
		const hostname = getSafeHostname(url)
		return (
			hostname === "localhost" ||
			hostname === "127.0.0.1" ||
			hostname === "0.0.0.0" ||
			hostname.startsWith("192.168.") ||
			hostname.startsWith("10.") ||
			hostname.endsWith(".local")
		)
	} catch (_e) {
		// If we can't parse the URL, assume it's not localhost
		return false
	}
}
 
// Function to normalize relative URLs by combining with a base URL
export const normalizeRelativeUrl = (relativeUrl: string, baseUrl: string): string => {
	// If it's already an absolute URL or a data URL, return as is
	if (relativeUrl.startsWith("http://") || relativeUrl.startsWith("https://") || relativeUrl.startsWith("data:")) {
		return relativeUrl
	}
 
	try {
		// Parse the base URL
		const baseUrlObj = safeCreateUrl(baseUrl)
		if (!baseUrlObj) {
			return relativeUrl // If we can't parse the base URL, return original
		}
 
		// Handle different types of relative paths
		if (relativeUrl.startsWith("//")) {
			// Protocol-relative URL
			return `${baseUrlObj.protocol}${relativeUrl}`
		} else if (relativeUrl.startsWith("/")) {
			// Root-relative URL
			return `${baseUrlObj.protocol}//${baseUrlObj.host}${relativeUrl}`
		} else {
			// Path-relative URL
			// Get the directory part of the URL
			let basePath = baseUrlObj.pathname
			if (!basePath.endsWith("/")) {
				// If the path doesn't end with a slash, remove the file part
				basePath = basePath.substring(0, basePath.lastIndexOf("/") + 1)
			}
			return `${baseUrlObj.protocol}//${baseUrlObj.host}${basePath}${relativeUrl}`
		}
	} catch (error) {
		console.log(`Error normalizing relative URL: ${error}`)
		return relativeUrl // Return original on error
	}
}
 
// Helper to ensure URL is in a format that can be opened
export const formatUrlForOpening = (url: string): string => {
	// If it's a data URI, return as is
	if (url.startsWith("data:image/")) {
		return url
	}
 
	// Use safeCreateUrl to validate and format the URL
	const urlObj = safeCreateUrl(url)
	if (urlObj) {
		return urlObj.href
	}
 
	console.log(`Invalid URL format: ${url}`)
	// Return a safe fallback that won't crash
	return "about:blank"
}
 
// Function to check if a URL is an image using HEAD request
export const checkIfImageUrl = async (url: string): Promise<boolean> => {
	// For data URLs, we can check synchronously
	if (url.startsWith("data:image/")) {
		return true
	}
 
	// Create a secure URL for the check but don't modify the original URL
	let secureUrl = url
	// Convert HTTP to HTTPS for security in the network request only
	if (secureUrl.startsWith("http://")) {
		secureUrl = secureUrl.replace("http://", "https://")
		console.log(`Using HTTPS version for image check: ${secureUrl}`)
	}
 
	// Validate URL before proceeding
	if (!isUrl(url)) {
		console.log("Invalid URL format:", url)
		return false
	}
 
	// For https URLs, we need to use the gRPC FileService
	if (url.startsWith("https")) {
		try {
			// Use the gRPC client with timeout
			const timeoutPromise = new Promise<boolean>((resolve) => {
				setTimeout(() => {
					console.log("Hit timeout waiting for checkIsImageUrl")
					resolve(false)
				}, 3000)
			})
 
			// Create the actual service call
			const servicePromise = WebServiceClient.checkIsImageUrl(StringRequest.create({ value: url }))
				.then((result) => result.isImage)
				.catch((error) => {
					console.error("Error checking if URL is an image via gRPC:", error)
					return false
				})
 
			// Race between the service call and the timeout
			return Promise.race([servicePromise, timeoutPromise])
		} catch (_error) {
			console.log("Error checking if URL is an image:", url)
			// Return false to indicate it's not an image
			return false
		}
	}
 
	// Don't fall back to extension check for other URLs
	// Only data URLs (handled above) are guaranteed to be images
	// For all other URLs, we need proper content type verification
	console.log(`URL protocol not supported for image check: ${url}`)
	return false
}
 
/**
 * Extracts all valid URLs from the given text
 * @param text - The text to search for URLs
 * @param maxUrls - Maximum number of URLs to extract (default: 50)
 * @returns Array of URL matches sorted by position in text
 */
export const extractUrlsFromText = (text: string, maxUrls: number = 50): UrlMatch[] => {
	const matches: UrlMatch[] = []
	const urlRegex = /(?:https?:\/\/|data:image)[^\s<>"']+/g
	let urlMatch: RegExpExecArray | null
	let urlCount = 0
 
	while ((urlMatch = urlRegex.exec(text)) !== null && urlCount < maxUrls) {
		const url = urlMatch[0]
 
		// Skip invalid URLs
		if (!isUrl(url)) {
			console.log("Skipping invalid URL:", url)
			continue
		}
 
		// Skip localhost URLs to prevent security issues
		if (isLocalhostUrl(url)) {
			console.log("Skipping localhost URL:", url)
			continue
		}
 
		matches.push({
			url,
			fullMatch: url,
			index: urlMatch.index,
			isImage: false, // Will be determined later
			isProcessed: false,
		})
 
		urlCount++
	}
 
	console.log(`Found ${matches.length} URLs in text`)
	return matches.sort((a, b) => a.index - b.index)
}
 
/**
 * Processes URLs to determine their types (e.g., image vs link)
 * Processes URLs sequentially to avoid network flooding
 * @param matches - Array of URL matches to process
 * @param onProgress - Callback for progress updates with updated matches
 * @param cancellationToken - Object to check if processing should be cancelled
 * @returns Promise that resolves when processing is complete
 */
export const processUrlTypes = async (
	matches: UrlMatch[],
	onProgress: (updatedMatches: UrlMatch[]) => void,
	cancellationToken: { cancelled: boolean },
): Promise<void> => {
	console.log(`Starting sequential URL processing for ${matches.length} URLs`)
 
	for (let i = 0; i < matches.length; i++) {
		// Skip already processed URLs
		if (matches[i].isProcessed) {
			continue
		}
 
		// Check if processing has been canceled
		if (cancellationToken.cancelled) {
			console.log("URL processing canceled")
			return
		}
 
		const match = matches[i]
		console.log(`Processing URL ${i + 1} of ${matches.length}: ${match.url}`)
 
		try {
			// Check if URL is an image
			const isImage = await checkIfImageUrl(match.url)
 
			// Skip if processing has been canceled
			if (cancellationToken.cancelled) {
				return
			}
 
			// Update the match
			match.isImage = isImage
			match.isProcessed = true
 
			// Notify progress with a new array to ensure React detects changes
			onProgress([...matches])
		} catch (err) {
			console.log(`URL check error: ${match.url}`, err)
			match.isProcessed = true
 
			// Update state even on error
			if (!cancellationToken.cancelled) {
				onProgress([...matches])
			}
		}
 
		// Delay between URL processing to avoid overwhelming the network
		if (!cancellationToken.cancelled && i < matches.length - 1) {
			await new Promise((resolve) => setTimeout(resolve, 100))
		}
	}
 
	console.log(`URL processing complete. Found ${matches.filter((m) => m.isImage).length} image URLs`)
}
 
/**
 * Orchestrates the URL extraction and processing pipeline
 * @param text - The response text to process
 * @param maxUrls - Maximum number of URLs to process
 * @param onMatchesFound - Callback when initial URLs are extracted
 * @param onMatchesUpdated - Callback when URL types are determined
 * @param onError - Error handler callback
 * @returns Cleanup function to cancel processing
 */
export const processResponseUrls = (
	text: string,
	maxUrls: number,
	onMatchesFound: (matches: UrlMatch[]) => void,
	onMatchesUpdated: (matches: UrlMatch[]) => void,
	onError: (error: string) => void,
): (() => void) => {
	const cancellationToken = { cancelled: false }
 
	const process = async () => {
		try {
			// Extract URLs from text
			const matches = extractUrlsFromText(text, maxUrls)
 
			// Immediately notify about found matches
			onMatchesFound(matches)
 
			// Process URLs in the background
			await processUrlTypes(matches, onMatchesUpdated, cancellationToken)
		} catch (_error) {
			onError("Failed to process response content. Switch to plain text mode to view safely.")
		}
	}
 
	// Start processing
	process()
 
	// Return cleanup function
	return () => {
		cancellationToken.cancelled = true
		console.log("Cleaning up URL processing")
	}
}
 
/**
 * Builds an array of display segments from response text and URL matches
 * @param responseText - The full response text
 * @param urlMatches - Array of URL matches with their positions and types
 * @returns Array of display segments describing how to render the content
 */
export const buildDisplaySegments = (responseText: string, urlMatches: UrlMatch[]): DisplaySegment[] => {
	const segments: DisplaySegment[] = []
	let lastIndex = 0
	let segmentIndex = 0
 
	// Handle case with no URLs
	if (urlMatches.length === 0) {
		return [
			{
				type: "text",
				content: responseText,
				key: "segment-0",
			},
		]
	}
 
	// Process each URL match
	for (let i = 0; i < urlMatches.length; i++) {
		const match = urlMatches[i]
		const { url, fullMatch, index } = match
 
		// Add text segment before this URL
		if (index > lastIndex) {
			segments.push({
				type: "text",
				content: responseText.substring(lastIndex, index),
				key: `segment-${segmentIndex++}`,
			})
		}
 
		// Add the URL text itself
		segments.push({
			type: "url",
			content: fullMatch,
			key: `url-${segmentIndex++}`,
		})
 
		// Add embedded content after the URL
		if (match.isImage) {
			segments.push({
				type: "image",
				content: url,
				url: formatUrlForOpening(url),
				key: `embed-image-${url}-${segmentIndex++}`,
			})
		} else if (match.isProcessed && !isLocalhostUrl(url)) {
			segments.push({
				type: "link",
				content: url,
				url: formatUrlForOpening(url),
				key: `embed-${url}-${segmentIndex++}`,
			})
		}
 
		// Update lastIndex for next segment
		lastIndex = index + fullMatch.length
	}
 
	// Add any remaining text after the last URL
	if (lastIndex < responseText.length) {
		segments.push({
			type: "text",
			content: responseText.substring(lastIndex),
			key: `segment-${segmentIndex++}`,
		})
	}
 
	return segments
}