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

feat(boxai-sidebar): Cache custom suggested questions #4019

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/elements/content-sidebar/BoxAISidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import * as React from 'react';
import { useIntl } from 'react-intl';
import { type ItemType } from '@box/box-ai-content-answers';
import {type ItemType, SuggestedQuestionType} from '@box/box-ai-content-answers';
import { AgentsProvider, RecordActionType } from '@box/box-ai-agent-selector';
import BoxAISidebarContent from './BoxAISidebarContent';
import { BoxAISidebarContext } from './context/BoxAISidebarContext';
Expand Down Expand Up @@ -121,17 +121,23 @@ const BoxAISidebar = (props: BoxAISidebarProps) => {
spreadsheetNotice = formatMessage(messages.welcomeMessageSpreadsheetNotice);
}

const handleSuggestedQuestionsFetched = (fetchedSuggestedQuestions: SuggestedQuestionType[]) => {
setCacheValue('suggestions', fetchedSuggestedQuestions);
};

return (
// BoxAISidebarContent is using withApiWrapper that is not passing all provided props,
// that's why we need to use provider to pass other props
<AgentsProvider value={cache.agents}>
<BoxAISidebarContext.Provider value={contextValue}>
<BoxAISidebarContent
cachedSuggestions={cache.suggestions}
getSuggestedQuestions={getSuggestedQuestions}
isOpen
isStopResponseEnabled={isStopResponseEnabled}
itemID={fileID}
itemIDs={[fileID]}
onSuggestedQuestionsFetched={handleSuggestedQuestionsFetched}
restoredQuestions={questionsWithoutInProgress}
restoredSession={cache.encodedSession}
shouldPreinitSession={shouldPreinitSession}
Expand Down
4 changes: 3 additions & 1 deletion src/elements/content-sidebar/BoxAISidebarContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ function BoxAISidebarContent(props: ApiWrapperWithInjectedProps) {
items={items}
questions={questions}
onUserIntentToUseAI={handleUserIntentToUseAI}
showLoadingIndicator={isLoading && shouldPreinitSession}
stopQuestion={stopQuestion}
submitQuestion={sendQuestion}
showLoadingIndicator={isLoading && shouldPreinitSession}
suggestedQuestions={cache.suggestions}
variant="sidebar"
recordAction={recordAction}
{...rest}
Expand Down Expand Up @@ -232,6 +233,7 @@ function BoxAISidebarContent(props: ApiWrapperWithInjectedProps) {
stopPropagationOnEsc
stopQuestion={stopQuestion}
submitQuestion={sendQuestion}
suggestedQuestions={cache.suggestions}
variant="collapsible"
{...rest}
shouldRenderProviders={false}
Expand Down
3 changes: 2 additions & 1 deletion src/elements/content-sidebar/SidebarPanels.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class SidebarPanels extends React.Component<Props, State> {
requestState: 'not_started',
},
encodedSession: null,
suggestions: [],
questions: [],
};

Expand Down Expand Up @@ -171,7 +172,7 @@ class SidebarPanels extends React.Component<Props, State> {
}
};

setBoxAiSidebarCacheValue = (key: 'agents' | 'encodedSession' | 'questions', value: any) => {
setBoxAiSidebarCacheValue = (key: 'agents' | 'encodedSession' | 'suggestions' | 'questions', value: any) => {
this.boxAiSidebarCache[key] = value;
};

Expand Down
24 changes: 24 additions & 0 deletions src/elements/content-sidebar/__tests__/BoxAISidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('elements/content-sidebar/BoxAISidebar', () => {
encodedSession: '',
questions: [],
agents: mockAgents,
suggestions: [],
},
createSessionRequest: jest.fn(() => ({ encodedSession: '1234' })),
elementId: '123',
Expand Down Expand Up @@ -212,6 +213,28 @@ describe('elements/content-sidebar/BoxAISidebar', () => {
expect(screen.getByText('Welcome to Box AI', { exact: false })).toBeInTheDocument();
});

test('should render cached custom suggested questions', async () => {
await renderComponent( {
cache: {
encodedSession: '1234',
questions: [],
agents: mockAgents,
suggestions: [
{
id: 'suggested-question-1',
prompt: 'Summarize this document',
label: 'Please summarize this document',
},
],
},
hasCustomSuggestedQuestions: true,
shouldShowLandingPage: true,
});

expect(screen.queryByText('Summarize this document', { exact: false })).toBeInTheDocument();
expect(screen.queryByText('Loading suggestions', { exact: false })).not.toBeInTheDocument();
});

test('should not set questions that are in progress', async () => {
await renderComponent({
cache: {
Expand All @@ -229,6 +252,7 @@ describe('elements/content-sidebar/BoxAISidebar', () => {
},
],
agents: mockAgents,
suggestions: [],
},
});

Expand Down
5 changes: 3 additions & 2 deletions src/elements/content-sidebar/types/BoxAISidebarTypes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type QuestionType } from '@box/box-ai-content-answers';
import { type QuestionType , type SuggestedQuestionType } from '@box/box-ai-content-answers';
import { type AgentState } from '@box/box-ai-agent-selector';

export type BoxAISidebarCache = {
agents: AgentState,
encodedSession: string | null,
suggestions: SuggestedQuestionType[],
questions: QuestionType[],
};

export type BoxAISidebarCacheSetter = (key: 'agents' | 'encodedSession' | 'questions', value: AgentState | QuestionType[] | string | null) => void;
export type BoxAISidebarCacheSetter = (key: 'agents' | 'encodedSession' | 'suggestions' | 'questions', value: AgentState | SuggestedQuestionType[] | QuestionType[] | string | null) => void;