Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Stable state in tool components #385

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions frontend/app/components/GeneratingQuestionsToolUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Sheet, SheetContent, SheetTrigger } from "./ui/sheet";
import { TooltipIconButton } from "./ui/assistant-ui/tooltip-icon-button";
import { DocumentCard, Document } from "./DocumentCard";
import { useCallback } from "react";

type Question = {
question: string;
Expand Down Expand Up @@ -103,7 +104,9 @@ const QuestionCard = ({ question }: { question: Question }) => {
export const useGeneratingQuestionsUI = () =>
useAssistantToolUI({
toolName: "generating_questions",
render: (input) => {
// Wrap the component in a useCallback to keep the identity stable.
// Allows the component to be interactable and not be re-rendered on every state change.
render: useCallback((input) => {
if (!input.args?.questions || input.args.questions.length === 0) {
return null;
}
Expand All @@ -128,5 +131,5 @@ export const useGeneratingQuestionsUI = () =>
</div>
</div>
);
},
}, []),
});
7 changes: 5 additions & 2 deletions frontend/app/components/ProgressToolUI.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAssistantToolUI } from "@assistant-ui/react";
import { Progress } from "./ui/progress";
import { cn } from "../utils/cn";
import { useCallback } from "react";

export const stepToProgressFields = (step: number) => {
switch (step) {
Expand Down Expand Up @@ -40,7 +41,9 @@ export const stepToProgressFields = (step: number) => {
export const useProgressToolUI = () =>
useAssistantToolUI({
toolName: "progress",
render: (input) => {
// Wrap the component in a useCallback to keep the identity stable.
// Allows the component to be interactable and not be re-rendered on every state change.
render: useCallback((input) => {
const { text, progress } = stepToProgressFields(input.args.step);

return (
Expand All @@ -60,5 +63,5 @@ export const useProgressToolUI = () =>
</p>
</div>
);
},
}, []),
});
7 changes: 5 additions & 2 deletions frontend/app/components/SelectedDocumentsToolUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { BookOpenText, Plus } from "lucide-react";
import { Sheet, SheetContent, SheetTrigger } from "./ui/sheet";
import { DocumentDialog } from "./DocumentDialog";
import { DocumentCard, Document } from "./DocumentCard";
import { useCallback } from "react";

export const useSelectedDocumentsUI = () =>
useAssistantToolUI({
toolName: "selected_documents",
render: (input) => {
// Wrap the component in a useCallback to keep the identity stable.
// Allows the component to be interactable and not be re-rendered on every state change.
render: useCallback((input) => {
if (!input.args?.documents || input.args.documents.length === 0) {
return null;
}
Expand Down Expand Up @@ -75,5 +78,5 @@ export const useSelectedDocumentsUI = () =>
</div>
</div>
);
},
}, []),
});
Loading