Skip to content

Memoize JSON.stringify calls in tool-fallback render path #1099

@MODSetter

Description

@MODSetter

Description

In tool-fallback.tsx, JSON.stringify(status.error) and JSON.stringify(result, null, 2) run during render. For large tool results, this creates significant CPU overhead on every re-render.

File to change

  • surfsense_web/components/assistant-ui/tool-fallback.tsx (lines 22-33, 124-126)

Current code

const cancelledReason = isCancelled && status.error
  ? typeof status.error === "string" ? status.error : JSON.stringify(status.error)
  : null;

<pre>{typeof result === "string" ? result : JSON.stringify(result, null, 2)}</pre>

What to do

Wrap the serialized strings in useMemo:

const serializedError = useMemo(
  () => (status?.error && typeof status.error !== "string" ? JSON.stringify(status.error) : null),
  [status?.error]
);

const serializedResult = useMemo(
  () => (result !== undefined && typeof result !== "string" ? JSON.stringify(result, null, 2) : null),
  [result]
);

Acceptance criteria

  • JSON.stringify is memoized and only recomputes when source data changes
  • Tool results and errors still display correctly

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions