Merge pull request #1115 from SohamBhattacharjee2003/fix/memoize-json-stringify

fix: memoize json stringify
This commit is contained in:
Rohan Verma 2026-04-03 08:14:24 -07:00 committed by GitHub
commit 90228d5e0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,6 @@
import type { ToolCallMessagePartComponent } from "@assistant-ui/react"; import type { ToolCallMessagePartComponent } from "@assistant-ui/react";
import { CheckIcon, ChevronDownIcon, ChevronUpIcon, XCircleIcon } from "lucide-react"; import { CheckIcon, ChevronDownIcon, ChevronUpIcon, XCircleIcon } from "lucide-react";
import { useState } from "react"; import { useMemo, useState } from "react";
import { getToolIcon } from "@/contracts/enums/toolIcons"; import { getToolIcon } from "@/contracts/enums/toolIcons";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
@ -19,17 +19,28 @@ export const ToolFallback: ToolCallMessagePartComponent = ({
const isCancelled = status?.type === "incomplete" && status.reason === "cancelled"; const isCancelled = status?.type === "incomplete" && status.reason === "cancelled";
const isError = status?.type === "incomplete" && status.reason === "error"; const isError = status?.type === "incomplete" && status.reason === "error";
const isRunning = status?.type === "running" || status?.type === "requires-action"; const isRunning = status?.type === "running" || status?.type === "requires-action";
const errorData = status?.type === "incomplete" ? status.error : undefined;
const serializedError = useMemo(
() => (errorData && typeof errorData !== "string" ? JSON.stringify(errorData) : null),
[errorData]
);
const serializedResult = useMemo(
() => (result !== undefined && typeof result !== "string" ? JSON.stringify(result, null, 2) : null),
[result]
);
const cancelledReason = const cancelledReason =
isCancelled && status.error isCancelled && status.error
? typeof status.error === "string" ? typeof status.error === "string"
? status.error ? status.error
: JSON.stringify(status.error) : serializedError
: null; : null;
const errorReason = const errorReason =
isError && status.error isError && status.error
? typeof status.error === "string" ? typeof status.error === "string"
? status.error ? status.error
: JSON.stringify(status.error) : serializedError
: null; : null;
const Icon = getToolIcon(toolName); const Icon = getToolIcon(toolName);
@ -122,7 +133,7 @@ export const ToolFallback: ToolCallMessagePartComponent = ({
<div> <div>
<p className="text-xs font-medium text-muted-foreground mb-1">Result</p> <p className="text-xs font-medium text-muted-foreground mb-1">Result</p>
<pre className="text-xs text-foreground/80 whitespace-pre-wrap break-all"> <pre className="text-xs text-foreground/80 whitespace-pre-wrap break-all">
{typeof result === "string" ? result : JSON.stringify(result, null, 2)} {typeof result === "string" ? result : serializedResult}
</pre> </pre>
</div> </div>
</> </>