Skip to content

Commit 1248ea4

Browse files
committed
Revert "cleanup: fix state race condition when setting image url"
This reverts commit 0b84c93.
1 parent 0b84c93 commit 1248ea4

3 files changed

Lines changed: 25 additions & 29 deletions

File tree

clients/search-component/src/TrieveModal/Chat/SuggestedQuestions.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ export const SuggestedQuestions = ({
2222
const { suggestedQuestions, isLoadingSuggestedQueries, getQuestions } =
2323
useSuggestedQuestions();
2424

25-
const { props, trieveSDK, fingerprint, abTreatment } =
25+
const { props, trieveSDK, fingerprint, abTreatment, imageUrl, setImageUrl } =
2626
useModalState();
2727
const [parent] = useAutoAnimate({ duration: 100 });
2828
const [selectedQuestion, setSelectedQuestion] = useState<
2929
AiQuestion | DefaultSearchQuery | null
3030
>(null);
3131

3232
useEffect(() => {
33-
if (selectedQuestion) {
33+
if (selectedQuestion && imageUrl) {
3434
askQuestion(
3535
isAiQuestion(selectedQuestion)
3636
? selectedQuestion.questionText
@@ -42,11 +42,9 @@ export const SuggestedQuestions = ({
4242
isAiQuestion(selectedQuestion) && selectedQuestion.promptForAI !== ""
4343
? selectedQuestion.promptForAI
4444
: undefined,
45-
undefined,
46-
isDefaultSearchQuery(selectedQuestion) ? selectedQuestion.imageUrl ?? undefined : "",
4745
);
4846
}
49-
}, [selectedQuestion]);
47+
}, [imageUrl, selectedQuestion]);
5048

5149
if (messages.length) {
5250
return null;
@@ -57,8 +55,12 @@ export const SuggestedQuestions = ({
5755
) => {
5856
console.log("q", q);
5957
setCurrentQuestion(isAiQuestion(q) ? q.questionText : (q.query ?? ""));
60-
setSelectedQuestion(q);
61-
58+
59+
if (isDefaultSearchQuery(q) && q.imageUrl) {
60+
setSelectedQuestion(q);
61+
setImageUrl(q.imageUrl);
62+
}
63+
6264
const requestId =
6365
messages[messages.length - 1]?.queryId ??
6466
"00000000-0000-0000-0000-000000000000";
@@ -81,6 +83,7 @@ export const SuggestedQuestions = ({
8183
});
8284
if (onMessageSend) {
8385
onMessageSend();
86+
setImageUrl("");
8487
}
8588
};
8689

clients/search-component/src/TrieveModal/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,11 @@ const Modal = () => {
182182
if (props.inline) return;
183183

184184
if (defaultMode === "chat") {
185+
setOpen(true);
185186
setMode("chat");
186187
cancelGroupChat();
187-
setOpen(true);
188-
askQuestion(customEvent.detail.text, undefined, undefined, "", true, customEvent.detail.imageUrl);
188+
setImageUrl(customEvent.detail.imageUrl ?? "");
189+
askQuestion(customEvent.detail.text);
189190
} else {
190191
setOpen(true);
191192
setMode("search");

clients/search-component/src/utils/hooks/chat-context.tsx

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const ChatContext = createContext<{
7575
groupIds?: string[],
7676
systemPrompt?: string,
7777
displayUserMessage?: boolean,
78-
imageUrl?: string,
7978
) => Promise<void>;
8079
isLoading: boolean;
8180
loadingText: string;
@@ -140,8 +139,6 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
140139
const [productsWithClicks, setProductsWithClicks] = useState<
141140
ChunkIdWithIndex[]
142141
>([]);
143-
let localImageUrl = imageUrl;
144-
145142

146143
const createTopic = async ({
147144
question,
@@ -523,6 +520,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
523520
let referenceImageUrls: string[] = [];
524521
let referenceChunks: Chunk[] = [];
525522

523+
526524
if (!groupIds || groupIds.length === 0) {
527525
chatMessageAbortController.current = new AbortController();
528526
const toolCallTimeout = setTimeout(
@@ -542,7 +540,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
542540
if (props.type === "ecommerce" && !curGroup) {
543541
return await trieveSDK.getToolCallFunctionParams({
544542
user_message_text: questionProp || currentQuestion,
545-
image_url: localImageUrl ? localImageUrl : null,
543+
image_url: imageUrl ? imageUrl : null,
546544
audio_input: curAudioBase64 ? curAudioBase64 : null,
547545
tool_function: {
548546
name: "get_price_filters",
@@ -602,7 +600,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
602600
}
603601
},
604602
)} \n\n${props.searchToolCallOptions?.userMessageTextPrefix ?? defaultSearchToolCallOptions.userMessageTextPrefix}: ${questionProp || currentQuestion}.`,
605-
image_url: localImageUrl ? localImageUrl : null,
603+
image_url: imageUrl ? imageUrl : null,
606604
audio_input: curAudioBase64 ? curAudioBase64 : null,
607605
tool_function: {
608606
name: "skip_search",
@@ -623,10 +621,10 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
623621
})
624622

625623
const imageFiltersPromise = retryOperation(async () => {
626-
if (localImageUrl) {
624+
if (imageUrl) {
627625
return await trieveSDK.getToolCallFunctionParams({
628626
user_message_text: questionProp || currentQuestion,
629-
image_url: localImageUrl ? localImageUrl : null,
627+
image_url: imageUrl ? imageUrl : null,
630628
tool_function: {
631629
name: "get_image_filters",
632630
description:
@@ -667,7 +665,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
667665
(message) => `\n\n${message.text}`,
668666
)} \n\n ${questionProp || currentQuestion}`
669667
: null,
670-
image_url: localImageUrl ? localImageUrl : null,
668+
image_url: imageUrl ? imageUrl : null,
671669
audio_input: curAudioBase64 ? curAudioBase64 : null,
672670
tool_function: {
673671
name: "get_filters",
@@ -717,7 +715,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
717715
text: transcribedQuery ?? "",
718716
additional: null,
719717
queryId: null,
720-
imageUrl: localImageUrl ? localImageUrl : null,
718+
imageUrl: imageUrl ? imageUrl : null,
721719
},
722720
{
723721
type: "system",
@@ -730,7 +728,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
730728
}
731729

732730
useImage = (imageFiltersResp?.parameters &&
733-
(imageFiltersResp.parameters as any)["image"] === true && localImageUrl) as boolean;
731+
(imageFiltersResp.parameters as any)["image"] === true && imageUrl) as boolean;
734732

735733
const match_any_tags = [];
736734
if (tagFiltersResp?.parameters) {
@@ -1059,6 +1057,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
10591057
}
10601058

10611059
const handleImageEdit = async () => {
1060+
console.log("REFERENCE IMAGE URLS", referenceImageUrls);
10621061
if (useImage) {
10631062
setLoadingText("Editing image...");
10641063

@@ -1067,7 +1066,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
10671066
input_images: [
10681067
{
10691068
image_src: {
1070-
url: localImageUrl,
1069+
url: imageUrl,
10711070
},
10721071
file_name: "input_image",
10731072
},
@@ -1303,13 +1302,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
13031302
groupIds?: string[],
13041303
systemPrompt?: string,
13051304
displayUserMessage?: boolean,
1306-
imageUrl?: string,
13071305
) => {
1308-
if (imageUrl) {
1309-
localImageUrl = imageUrl;
1310-
setImageUrl(imageUrl);
1311-
}
1312-
13131306
const questionProp = question;
13141307
setIsDoneReading(false);
13151308
setCurrentQuestion("");
@@ -1363,7 +1356,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
13631356
(displayUserMessage ?? true) ? questionProp || currentQuestion : "",
13641357
additional: null,
13651358
queryId: null,
1366-
imageUrl: localImageUrl ? localImageUrl : null,
1359+
imageUrl: imageUrl ? imageUrl : null,
13671360
},
13681361
{
13691362
type: "system",
@@ -1380,7 +1373,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
13801373
text: "Loading...",
13811374
additional: null,
13821375
queryId: null,
1383-
imageUrl: localImageUrl ? localImageUrl : null,
1376+
imageUrl: imageUrl ? imageUrl : null,
13841377
},
13851378
{
13861379
type: "system",
@@ -1406,7 +1399,6 @@ function ChatProvider({ children }: { children: React.ReactNode }) {
14061399
systemPrompt,
14071400
});
14081401
}
1409-
setImageUrl("");
14101402
};
14111403

14121404
const switchToChatAndAskQuestion = async (query: string) => {

0 commit comments

Comments
 (0)