Skip to content

Commit

Permalink
Merge pull request #1981 from stakwork/hotfix/ai-answers
Browse files Browse the repository at this point in the history
fix: fix loading of ai question
  • Loading branch information
Rassl authored Aug 1, 2024
2 parents 925b79f + c8c0b92 commit 7ec8a60
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/components/App/SideBar/AiSummary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { AiSummarySkeleton } from './AiSummarySkeleton'
type Props = {
question: string
response: AIEntity
refId: string
}

const Title = styled(Text)`
Expand All @@ -37,7 +38,7 @@ const TitleWrapper = styled(Flex).attrs({
overflow: hidden;
`

export const AiSummary = ({ question, response }: Props) => {
export const AiSummary = ({ question, response, refId }: Props) => {
const ref = useRef<HTMLDivElement>(null)
const [collapsed, setCollapsed] = useState(false)
const { setAiSummaryAnswer } = useAiSummaryStore((s) => s)
Expand All @@ -53,8 +54,8 @@ export const AiSummary = ({ question, response }: Props) => {
}

const handleLoaded = () => {
if (question) {
setAiSummaryAnswer(question, { hasBeenRendered: true })
if (refId) {
setAiSummaryAnswer(refId, { hasBeenRendered: true })
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/components/App/SideBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ const Content = forwardRef<HTMLDivElement, ContentProp>(({ subViewOpen }, ref) =
)}
<Flex>
{Object.keys(aiSummaryAnswers).map((i: string) => (
<AiSummary key={i} question={i} response={aiSummaryAnswers[i]} />
<AiSummary
key={i}
question={aiSummaryAnswers[i]?.question || ''}
refId={i}
response={aiSummaryAnswers[i]}
/>
))}

{isLoading ? <EpisodeSkeleton /> : !hasAiChats && <LatestView isSearchResult={!!searchTerm || hasAiChats} />}
Expand Down
14 changes: 6 additions & 8 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Flex } from '~/components/common/Flex'
import { DataRetriever } from '~/components/DataRetriever'
import { GlobalStyle } from '~/components/GlobalStyle'
import { Overlay } from '~/components/Universe/Overlay' // Import Overlay directly
import { Preloader } from '~/components/Universe/Preloader' // Import Preloader directly
import { isDevelopment } from '~/constants'
import { useSocket } from '~/hooks/useSockets'
import { useAiSummaryStore } from '~/stores/useAiSummaryStore'
Expand Down Expand Up @@ -128,23 +127,23 @@ export const App = () => {

const handleAiSummaryAnswer = useCallback(
(data: AiSummaryAnswerResponse) => {
if (data.question && getKeyExist(aiRefId)) {
setAiSummaryAnswer(aiRefId, { answer: data.answer, answerLoading: false })
if (data.ref_id) {
setAiSummaryAnswer(data.ref_id, { answer: data.answer, answerLoading: false })
}
},
[setAiSummaryAnswer, getKeyExist, aiRefId],
[setAiSummaryAnswer],
)

const handleAiRelevantQuestions = useCallback(
(data: AiSummaryQuestionsResponse) => {
if (data.question && getKeyExist(aiRefId)) {
setAiSummaryAnswer(aiRefId, {
if (data.ref_id) {
setAiSummaryAnswer(data.ref_id, {
questions: data.relevant_questions.map((i) => i.question),
questionsLoading: false,
})
}
},
[setAiSummaryAnswer, getKeyExist, aiRefId],
[setAiSummaryAnswer],
)

const handleAiSources = useCallback(
Expand Down Expand Up @@ -239,7 +238,6 @@ export const App = () => {
<LazyMainToolbar />
<LazySideBar />
<LazyUniverse />
{false && <Preloader fullSize={false} />}
<Overlay />
<SecondarySideBar />
<AppBar />
Expand Down
8 changes: 7 additions & 1 deletion src/stores/useDataStore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export const useDataStore = create<DataStore>()(
}

if (AISearchQuery) {
setAiSummaryAnswer(AISearchQuery, { answer: '', answerLoading: true, sourcesLoading: true })
ai = { ...ai, ai_summary: String(true) }
}

Expand Down Expand Up @@ -189,6 +188,13 @@ export const useDataStore = create<DataStore>()(

if (data?.query_data?.ref_id) {
useAiSummaryStore.setState({ aiRefId: data?.query_data?.ref_id })

setAiSummaryAnswer(data?.query_data?.ref_id, {
question: AISearchQuery,
answer: '',
answerLoading: true,
sourcesLoading: true,
})
}

const currentNodes = currentPage === 0 && !aiRefId ? [] : [...(existingData?.nodes || [])]
Expand Down
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,23 @@ export type RelayUser = {
export type AiSummaryAnswerResponse = {
question: string
answer: string
ref_id: string
}

export type AiSummarySourcesResponse = {
question: string
sources: { ref_id: string }[]
ref_id: string
}

export type AiSummaryQuestionsResponse = {
question: string
relevant_questions: { question: string }[]
ref_id: string
}

export type AIEntity = {
question?: string
answer?: string
sources?: string[]
questions?: string[]
Expand Down

0 comments on commit 7ec8a60

Please sign in to comment.