Skip to content

Commit

Permalink
perf: memoize tool UI components (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Sep 7, 2024
1 parent 78ed910 commit 2dfdeb2
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/react/src/ui/assistant-message.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { ComponentPropsWithoutRef, forwardRef, type FC } from "react";
import { ComponentPropsWithoutRef, forwardRef, useMemo, type FC } from "react";
import { MessagePrimitive, MessagePrimitiveContentProps } from "../primitives";
import BranchPicker from "./branch-picker";
import { Avatar } from "./base/avatar";
Expand Down Expand Up @@ -47,23 +47,29 @@ const AssistantMessageContent = forwardRef<
const { tools, assistantMessage: { components = {} } = {} } =
useThreadConfig();

const toolsComponents = useMemo(
() => ({
by_name: !tools
? undefined
: Object.fromEntries(
tools.map((t) => [
t.unstable_tool.toolName,
t.unstable_tool.render,
]),
),
Fallback: components.ToolFallback,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[...(tools ?? []), components.ToolFallback],
);

return (
<AssistantMessageContentWrapper {...rest} ref={ref}>
<MessagePrimitive.Content
components={{
...componentsProp,
Text: componentsProp?.Text ?? components.Text ?? ContentPart.Text,
tools: {
by_name: !tools
? undefined
: Object.fromEntries(
tools.map((t) => [
t.unstable_tool.toolName,
t.unstable_tool.render,
]),
),
Fallback: components.ToolFallback,
},
tools: toolsComponents,
}}
/>
</AssistantMessageContentWrapper>
Expand Down

0 comments on commit 2dfdeb2

Please sign in to comment.