Skip to content

Commit

Permalink
fix: tool UI fallback should not override makeAssistantToolUI definit…
Browse files Browse the repository at this point in the history
…ions (#753)
  • Loading branch information
Yonom authored Sep 6, 2024
1 parent b4eabee commit 0a4b8d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-walls-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat: adjust the override order of tool UIs
5 changes: 5 additions & 0 deletions .changeset/weak-singers-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

fix: tool UI fallback should not override makeAssistantToolUI definitions
31 changes: 21 additions & 10 deletions packages/react/src/primitives/message/MessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export type MessagePrimitiveContentProps = {
| undefined;
};

const ToolUIDisplay = ({
UI,
...props
}: {
UI: ToolCallContentPartComponent | undefined;
} & ToolCallContentPartProps) => {
const { useToolUIs } = useAssistantContext();
const Render = useToolUIs((s) => s.getToolUI(props.part.toolName)) ?? UI;
if (!Render) return null;
return <Render {...props} />;
};

const defaultComponents = {
Text: () => (
<p style={{ whiteSpace: "pre-line" }}>
Expand All @@ -51,14 +63,6 @@ const defaultComponents = {
),
Image: () => <ContentPartPrimitiveImage />,
UI: () => <ContentPartPrimitiveDisplay />,
tools: {
Fallback: (props) => {
const { useToolUIs } = useAssistantContext();
const Render = useToolUIs((s) => s.getToolUI(props.part.toolName));
if (!Render) return null;
return <Render {...props} />;
},
},
} satisfies MessagePrimitiveContentProps["components"];

type MessageContentPartComponentProps = {
Expand All @@ -71,7 +75,7 @@ const MessageContentPartComponent: FC<MessageContentPartComponentProps> = ({
Text = defaultComponents.Text,
Image = defaultComponents.Image,
UI = defaultComponents.UI,
tools: { by_name = {}, Fallback = defaultComponents.tools.Fallback } = {},
tools: { by_name = {}, Fallback = undefined } = {},
} = {},
}) => {
const { useThreadActions } = useThreadContext();
Expand Down Expand Up @@ -110,7 +114,14 @@ const MessageContentPartComponent: FC<MessageContentPartComponentProps> = ({
toolCallId: part.toolCallId,
result,
});
return <Tool part={part} status={status} addResult={addResult} />;
return (
<ToolUIDisplay
UI={Tool}
part={part}
status={status}
addResult={addResult}
/>
);
}
default:
const unhandledType: never = type;
Expand Down

0 comments on commit 0a4b8d7

Please sign in to comment.