Skip to content

Commit 1a34943

Browse files
committed
fix: address PR review comments
- Use safeJsonParse instead of JSON.parse in ChatRow.tsx - Add type definition for parsed image info - Add more specific error types in ClineProvider.ts - Add comprehensive JSDoc comments to ImageBlock.tsx - Improve error handling and type safety
1 parent 253deb8 commit 1a34943

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2481,6 +2481,8 @@ export class ClineProvider
24812481
*
24822482
* @param filePath - The absolute file path to convert
24832483
* @returns The webview URI string, or the original file URI if conversion fails
2484+
* @throws {Error} When webview is not available
2485+
* @throws {TypeError} When file path is invalid
24842486
*/
24852487
public convertToWebviewUri(filePath: string): string {
24862488
try {
@@ -2492,10 +2494,18 @@ export class ClineProvider
24922494
return webviewUri.toString()
24932495
}
24942496

2497+
// Specific error for no webview available
2498+
const error = new Error("No webview available for URI conversion")
2499+
console.error(error.message)
24952500
// Fallback to file URI if no webview available
24962501
return fileUri.toString()
24972502
} catch (error) {
2498-
console.error("Failed to convert to webview URI:", error)
2503+
// More specific error handling
2504+
if (error instanceof TypeError) {
2505+
console.error("Invalid file path provided for URI conversion:", error)
2506+
} else {
2507+
console.error("Failed to convert to webview URI:", error)
2508+
}
24992509
// Return file URI as fallback
25002510
return vscode.Uri.file(filePath).toString()
25012511
}

webview-ui/src/components/chat/ChatRow.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,10 @@ export const ChatRowContent = ({
11611161
return <UpdateTodoListToolBlock userEdited onChange={() => {}} />
11621162
case "image":
11631163
// Parse the JSON to get imageUri and imagePath
1164-
const imageInfo = JSON.parse(message.text || "{}")
1164+
const imageInfo = safeJsonParse<{ imageUri: string; imagePath: string }>(message.text || "{}")
1165+
if (!imageInfo) {
1166+
return null
1167+
}
11651168
return (
11661169
<div style={{ marginTop: "10px" }}>
11671170
<ImageBlock imageUri={imageInfo.imageUri} imagePath={imageInfo.imagePath} />

webview-ui/src/components/common/ImageBlock.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
import React from "react"
22
import { ImageViewer } from "./ImageViewer"
33

4+
/**
5+
* Props for the ImageBlock component
6+
*/
47
interface ImageBlockProps {
5-
// For new image generation tool format (preferred)
6-
imageUri?: string // The webview-accessible URI for rendering
7-
imagePath?: string // The actual file path for display and opening
8+
/**
9+
* The webview-accessible URI for rendering the image.
10+
* This is the preferred format for new image generation tools.
11+
* Should be a URI that can be directly loaded in the webview context.
12+
*/
13+
imageUri?: string
814

9-
// For backward compatibility with Mermaid diagrams and old format
10-
imageData?: string // Base64 data or regular URL (legacy)
11-
path?: string // Optional path for Mermaid diagrams (legacy)
15+
/**
16+
* The actual file path for display purposes and file operations.
17+
* Used to show the path to the user and for opening the file in the editor.
18+
* This is typically an absolute or relative path to the image file.
19+
*/
20+
imagePath?: string
21+
22+
/**
23+
* Base64 data or regular URL for backward compatibility.
24+
* @deprecated Use imageUri instead for new implementations.
25+
* This is maintained for compatibility with Mermaid diagrams and legacy code.
26+
*/
27+
imageData?: string
28+
29+
/**
30+
* Optional path for Mermaid diagrams.
31+
* @deprecated Use imagePath instead for new implementations.
32+
* This is maintained for backward compatibility with existing Mermaid diagram rendering.
33+
*/
34+
path?: string
1235
}
1336

1437
export default function ImageBlock({ imageUri, imagePath, imageData, path }: ImageBlockProps) {

0 commit comments

Comments
 (0)