Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions webview-ui/src/components/chat/ChatRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { memo, useEffect, useMemo, useRef, useState } from "react"
import { useSize } from "react-use"
import { useCopyToClipboard } from "../../utils/clipboard"
import { useTranslation, Trans } from "react-i18next"
import { safeJsonParse } from "../../utils/json"
import {
ClineApiReqInfo,
ClineAskUseMcpServer,
Expand Down Expand Up @@ -92,8 +93,8 @@ export const ChatRowContent = ({

const [cost, apiReqCancelReason, apiReqStreamingFailedMessage] = useMemo(() => {
if (message.text !== null && message.text !== undefined && message.say === "api_req_started") {
const info: ClineApiReqInfo = JSON.parse(message.text)
return [info.cost, info.cancelReason, info.streamingFailedMessage]
const info = safeJsonParse<ClineApiReqInfo>(message.text)
return [info?.cost, info?.cancelReason, info?.streamingFailedMessage]
}

return [undefined, undefined, undefined]
Expand Down Expand Up @@ -147,7 +148,10 @@ export const ChatRowContent = ({
<span style={{ color: normalColor, fontWeight: "bold" }}>{t("chat:runCommand.title")}:</span>,
]
case "use_mcp_server":
const mcpServerUse = JSON.parse(message.text || "{}") as ClineAskUseMcpServer
const mcpServerUse = safeJsonParse<ClineAskUseMcpServer>(message.text)
if (mcpServerUse === undefined) {
return [null, null]
}
return [
isMcpServerResponding ? (
<ProgressIndicator />
Expand Down Expand Up @@ -250,14 +254,14 @@ export const ChatRowContent = ({

const tool = useMemo(() => {
if (message.ask === "tool" || message.say === "tool") {
return JSON.parse(message.text || "{}") as ClineSayTool
return safeJsonParse<ClineSayTool>(message.text)
}
return null
}, [message.ask, message.say, message.text])

const followUpData = useMemo(() => {
if (message.type === "ask" && message.ask === "followup" && !message.partial) {
return JSON.parse(message.text || "{}")
return safeJsonParse<any>(message.text)
}
return null
}, [message.type, message.ask, message.partial, message.text])
Expand Down Expand Up @@ -830,7 +834,7 @@ export const ChatRowContent = ({
{isExpanded && (
<div style={{ marginTop: "10px" }}>
<CodeAccordian
code={JSON.parse(message.text || "{}").request}
code={safeJsonParse<any>(message.text)?.request}
language="markdown"
isExpanded={true}
onToggleExpand={onToggleExpand}
Expand Down Expand Up @@ -897,15 +901,15 @@ export const ChatRowContent = ({
</div>
)
case "user_feedback_diff":
const tool = JSON.parse(message.text || "{}") as ClineSayTool
const tool = safeJsonParse<ClineSayTool>(message.text)
return (
<div
style={{
marginTop: -10,
width: "100%",
}}>
<CodeAccordian
diff={tool.diff!}
diff={tool?.diff!}
isFeedback={true}
isExpanded={isExpanded}
onToggleExpand={onToggleExpand}
Expand Down Expand Up @@ -1113,7 +1117,10 @@ export const ChatRowContent = ({
</>
)
case "use_mcp_server":
const useMcpServer = JSON.parse(message.text || "{}") as ClineAskUseMcpServer
const useMcpServer = safeJsonParse<ClineAskUseMcpServer>(message.text)
if (!useMcpServer) {
return null
}
const server = mcpServers.find((server) => server.name === useMcpServer.serverName)
return (
<>
Expand Down
17 changes: 17 additions & 0 deletions webview-ui/src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Safely parses JSON without crashing on invalid input
* @param jsonString The string to parse
* @param defaultValue Value to return if parsing fails
* @returns Parsed JSON object or defaultValue if parsing fails
*/
export function safeJsonParse<T>(jsonString: string | null | undefined, defaultValue?: T): T | undefined {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be neat if this optionally took in a zod schema and validated the object, but that might present some TS headaches.

if (!jsonString) return defaultValue

try {
return JSON.parse(jsonString) as T
} catch (error) {
// Log the error to the console for debugging
console.error("Error parsing JSON:", error)
return defaultValue
}
}