feat: highlight tool errors in red text

This commit is contained in:
kavinkumarbaskar
2025-04-05 23:24:10 +05:30
parent 3032a67d4e
commit a3be8f6376
2 changed files with 12 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ interface JsonViewProps {
initialExpandDepth?: number; initialExpandDepth?: number;
className?: string; className?: string;
withCopyButton?: boolean; withCopyButton?: boolean;
isError?: boolean;
} }
function tryParseJson(str: string): { success: boolean; data: JsonValue } { function tryParseJson(str: string): { success: boolean; data: JsonValue } {
@@ -35,6 +36,7 @@ const JsonView = memo(
initialExpandDepth = 3, initialExpandDepth = 3,
className, className,
withCopyButton = true, withCopyButton = true,
isError = false
}: JsonViewProps) => { }: JsonViewProps) => {
const { toast } = useToast(); const { toast } = useToast();
const [copied, setCopied] = useState(false); const [copied, setCopied] = useState(false);
@@ -100,6 +102,7 @@ const JsonView = memo(
name={name} name={name}
depth={0} depth={0}
initialExpandDepth={initialExpandDepth} initialExpandDepth={initialExpandDepth}
isError = {isError}
/> />
</div> </div>
</div> </div>
@@ -114,10 +117,11 @@ interface JsonNodeProps {
name?: string; name?: string;
depth: number; depth: number;
initialExpandDepth: number; initialExpandDepth: number;
isError?: boolean;
} }
const JsonNode = memo( const JsonNode = memo(
({ data, name, depth = 0, initialExpandDepth }: JsonNodeProps) => { ({ data, name, depth = 0, initialExpandDepth, isError = false }: JsonNodeProps) => {
const [isExpanded, setIsExpanded] = useState(depth < initialExpandDepth); const [isExpanded, setIsExpanded] = useState(depth < initialExpandDepth);
const getDataType = (value: JsonValue): string => { const getDataType = (value: JsonValue): string => {
@@ -133,7 +137,8 @@ const JsonNode = memo(
boolean: "text-amber-600", boolean: "text-amber-600",
null: "text-purple-600", null: "text-purple-600",
undefined: "text-gray-600", undefined: "text-gray-600",
string: "text-green-600 break-all whitespace-pre-wrap", string: "text-green-600 group-hover:text-green-500",
error: "text-red-600 group-hover:text-red-500",
default: "text-gray-700", default: "text-gray-700",
}; };
@@ -236,7 +241,7 @@ const JsonNode = memo(
{name}: {name}:
</span> </span>
)} )}
<pre className={typeStyleMap.string}>"{value}"</pre> <pre className={clsx(typeStyleMap.string, "break-all whitespace-pre-wrap")}>"{value}"</pre>
</div> </div>
); );
} }
@@ -250,8 +255,8 @@ const JsonNode = memo(
)} )}
<pre <pre
className={clsx( className={clsx(
typeStyleMap.string, isError ? typeStyleMap.error : typeStyleMap.string,
"cursor-pointer group-hover:text-green-500", "cursor-pointer break-all whitespace-pre-wrap",
)} )}
onClick={() => setIsExpanded(!isExpanded)} onClick={() => setIsExpanded(!isExpanded)}
title={isExpanded ? "Click to collapse" : "Click to expand"} title={isExpanded ? "Click to collapse" : "Click to expand"}

View File

@@ -66,11 +66,11 @@ const ToolsTab = ({
return ( return (
<> <>
<h4 className="font-semibold mb-2"> <h4 className="font-semibold mb-2">
Tool Result: {isError ? "Error" : "Success"} Tool Result: {isError ? <span className="text-red-600 font-semibold">Error</span> : <span className="text-green-600 font-semibold">Success</span>}
</h4> </h4>
{structuredResult.content.map((item, index) => ( {structuredResult.content.map((item, index) => (
<div key={index} className="mb-2"> <div key={index} className="mb-2">
{item.type === "text" && <JsonView data={item.text} />} {item.type === "text" && <JsonView data={item.text} isError={isError}/>}
{item.type === "image" && ( {item.type === "image" && (
<img <img
src={`data:${item.mimeType};base64,${item.data}`} src={`data:${item.mimeType};base64,${item.data}`}