Skip to content

Commit dd7a7dc

Browse files
committed
refactor(sidepanel): wire sidepanel ask-ai flow to the Typesense hook
1 parent 8b35eb6 commit dd7a7dc

1 file changed

Lines changed: 26 additions & 63 deletions

File tree

packages/docsearch-react/src/Sidepanel/Sidepanel.tsx

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import type { SidepanelShortcuts, InitialAskAiMessage } from '@docsearch/core';
22
import React, { useCallback } from 'react';
33
import type { JSX } from 'react';
44

5-
import { AlgoliaLogo, type AlgoliaLogoTranslations } from '../AlgoliaLogo';
6-
import type { DocSearchSidepanelProps, SidepanelSearchParameters } from '../Sidepanel';
5+
import type { DocSearchSidepanelProps } from '../Sidepanel';
76
import type { StoredAskAiState, SuggestedQuestionHit } from '../types';
7+
import { TypesenseLogo, type TypesenseLogoTranslations } from '../TypesenseLogo';
88
import { useAskAi } from '../useAskAi';
99
import { useIsMobile } from '../useIsMobile';
10-
import { useSearchClient } from '../useSearchClient';
11-
import { useSuggestedQuestions } from '../useSuggestedQuestions';
1210
import { buildDummyAskAiHit } from '../utils/ai';
1311

1412
import { ConversationHistoryScreen } from './ConversationHistoryScreen';
@@ -17,7 +15,6 @@ import { ConversationScreen } from './ConversationScreen';
1715
import type { NewConversationScreenTranslations } from './NewConversationScreen';
1816
import { NewConversationScreen } from './NewConversationScreen';
1917
import { PromptForm, type PromptFormTranslations } from './PromptForm';
20-
import { setSidepanelSearchClient } from './setSidepanelSearchClient';
2118
import type { HeaderTranslations } from './SidepanelHeader';
2219
import { SidepanelHeader } from './SidepanelHeader';
2320
import type { PanelSide, PanelVariant, SidepanelState } from './types';
@@ -57,9 +54,9 @@ export type SidepanelTranslations = Partial<{
5754
**/
5855
newConversationScreen: NewConversationScreenTranslations;
5956
/**
60-
* Translation text for the Algolia logo.
57+
* Translation text for the Typesense logo.
6158
**/
62-
logo: AlgoliaLogoTranslations;
59+
logo: TypesenseLogoTranslations;
6360
}>;
6461

6562
export type SidepanelProps = {
@@ -104,8 +101,7 @@ export type SidepanelProps = {
104101
portalContainer?: DocumentFragment | Element | null;
105102
/**
106103
* Enables displaying suggested questions on new conversation screen.
107-
*
108-
* @default false
104+
* Reserved for a future Typesense-native suggestions source.
109105
*/
110106
suggestedQuestions?: boolean;
111107
/**
@@ -118,13 +114,10 @@ export type SidepanelProps = {
118114
* @default `{ 'Ctrl/Cmd+I': true }`
119115
*/
120116
keyboardShortcuts?: SidepanelShortcuts;
121-
// HACK: This is a hack for testing staging, remove before releasing
122-
useStagingEnv?: boolean;
123117
};
124118

125119
type Props = Omit<DocSearchSidepanelProps, 'button' | 'panel'> &
126-
SidepanelProps &
127-
SidepanelSearchParameters & {
120+
SidepanelProps & {
128121
isOpen?: boolean;
129122
onOpen: () => void;
130123
onClose: () => void;
@@ -136,22 +129,17 @@ function SidepanelInner(
136129
isOpen = false,
137130
onOpen,
138131
onClose,
139-
assistantId,
140-
apiKey,
141-
appId,
142-
indexName,
132+
typesenseServerConfig,
133+
typesenseCollectionName,
134+
askAi,
143135
variant = 'floating',
144-
searchParameters,
145136
pushSelector,
146137
width,
147138
expandedWidth,
148-
suggestedQuestions: suggestedQuestionsEnabled = false,
149139
translations = {},
150140
keyboardShortcuts,
151141
side = 'right',
152142
initialMessage,
153-
useStagingEnv = false,
154-
agentStudio = false,
155143
}: Props,
156144
ref: React.ForwardedRef<SidepanelRef>,
157145
): JSX.Element {
@@ -174,8 +162,6 @@ function SidepanelInner(
174162
setIsExpanded(!isExpanded);
175163
}, [isExpanded]);
176164

177-
const searchClient = useSearchClient(appId, apiKey, setSidepanelSearchClient);
178-
179165
const {
180166
status,
181167
sendMessage,
@@ -185,30 +171,24 @@ function SidepanelInner(
185171
setMessages,
186172
conversations,
187173
messages,
188-
sendFeedback,
189174
askAiError,
190175
} = useAskAi({
191-
appId,
192-
indexName,
193-
assistantId,
194-
apiKey,
195-
searchParameters,
196-
useStagingEnv,
197-
agentStudio,
198-
});
199-
200-
const suggestedQuestions = useSuggestedQuestions({
201-
assistantId,
202-
suggestedQuestionsEnabled,
203-
searchClient,
176+
typesenseServerConfig,
177+
storageKey: `__DOCSEARCH_ASKAI_CONVERSATIONS__${typesenseCollectionName}`,
178+
collection: askAi.collection || typesenseCollectionName,
179+
conversationModelId: askAi.conversationModelId,
180+
queryBy: askAi.queryBy || 'embedding',
181+
excludeFields: askAi.excludeFields || 'embedding',
182+
searchParameters: askAi.searchParameters,
204183
});
184+
const suggestedQuestions: SuggestedQuestionHit[] = [];
205185

206186
const prevStatus = React.useRef(status);
207187

208188
const handleSend = (prompt: string): void => {
209189
setStoppedStreaming(false);
210190

211-
sendMessage({ text: prompt });
191+
void sendMessage(prompt);
212192
setSidepanelState('conversation');
213193
};
214194

@@ -220,14 +200,7 @@ function SidepanelInner(
220200
const handleSelectQuestion = (question: SuggestedQuestionHit): void => {
221201
setStoppedStreaming(false);
222202
setMessages([]);
223-
sendMessage(
224-
{ text: question.question },
225-
{
226-
body: {
227-
suggestedQuestionId: question.objectID,
228-
},
229-
},
230-
);
203+
void sendMessage(question.question, { suggestedQuestionId: question.objectID });
231204
setSidepanelState('conversation');
232205
};
233206

@@ -241,7 +214,7 @@ function SidepanelInner(
241214
if (conversation.messages) {
242215
setMessages(conversation.messages);
243216
} else if (conversation.query) {
244-
sendMessage({ text: conversation.query });
217+
void sendMessage(conversation.query);
245218
}
246219

247220
setSidepanelState('conversation');
@@ -284,13 +257,14 @@ function SidepanelInner(
284257
if (prevStatus.current === 'streaming' && status === 'ready') {
285258
if (stoppedStreaming && messages.at(-1)) {
286259
messages.at(-1)!.metadata = {
260+
...messages.at(-1)!.metadata,
287261
stopped: true,
288262
};
289263
}
290264

291265
for (const part of messages[0].parts) {
292266
if (part.type === 'text') {
293-
conversations.add(buildDummyAskAiHit(part.text, messages));
267+
conversations.add(buildDummyAskAiHit(part.text, messages, messages.at(-1)?.metadata?.conversationId));
294268
}
295269
}
296270
}
@@ -328,18 +302,9 @@ function SidepanelInner(
328302
handleSelectConversation(selectedConversation);
329303
} else {
330304
setMessages([]);
331-
sendMessage(
332-
{
333-
text: initialMessage.query,
334-
},
335-
initialMessage.suggestedQuestionId
336-
? {
337-
body: {
338-
suggestedQuestionId: initialMessage.suggestedQuestionId,
339-
},
340-
}
341-
: {},
342-
);
305+
void sendMessage(initialMessage.query, {
306+
suggestedQuestionId: initialMessage.suggestedQuestionId,
307+
});
343308
setSidepanelState('conversation');
344309
}
345310
}, [initialMessage, sendMessage, conversations, handleSelectConversation, setMessages]);
@@ -394,10 +359,8 @@ function SidepanelInner(
394359
exchanges={exchanges}
395360
status={status}
396361
conversations={conversations}
397-
handleFeedback={sendFeedback}
398362
translations={translations.conversationScreen}
399363
streamError={askAiError}
400-
agentStudio={agentStudio}
401364
/>
402365
)}
403366
{sidepanelState === 'conversation-history' && (
@@ -414,7 +377,7 @@ function SidepanelInner(
414377
/>
415378
<footer className="DocSearch-Sidepanel-Footer">
416379
<span className="DocSearch-Logo DocSearch-Sidepanel--powered-by">
417-
<AlgoliaLogo translations={translations.logo} />
380+
<TypesenseLogo translations={translations.logo} />
418381
</span>
419382
</footer>
420383
</aside>

0 commit comments

Comments
 (0)