|
1 | 1 | import { Box, Paper, Typography } from '@mui/material'
|
2 |
| -import { FileCitation } from '../../../shared/types' |
| 2 | +import { CourseAssistant, FileCitation } from '../../../shared/types' |
3 | 3 | import { Message } from '../../types'
|
| 4 | +import { useQuery } from '@tanstack/react-query' |
| 5 | +import { useCourseAssistant } from '../../hooks/useCourseAssistant' |
| 6 | +import { useParams } from 'react-router-dom' |
| 7 | + |
| 8 | +const useFileCitationText = (citation: FileCitation, vectorStoreId: string) => { |
| 9 | + const { data, ...rest } = useQuery({ |
| 10 | + queryKey: ['file-citation-content', vectorStoreId, citation.file_id], |
| 11 | + queryFn: async () => { |
| 12 | + const response = await fetch(`/api/files/${vectorStoreId}/${citation.file_id}`) |
| 13 | + if (!response.ok) { |
| 14 | + throw new Error('Failed to fetch file text') |
| 15 | + } |
| 16 | + return response.text() |
| 17 | + }, |
| 18 | + retry: false, |
| 19 | + }) |
| 20 | + |
| 21 | + return { |
| 22 | + text: data, |
| 23 | + ...rest, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const Citation = ({ citation, courseAssistant }: { citation: FileCitation, courseAssistant: CourseAssistant }) => { |
| 28 | + const { text, isLoading, error } = useFileCitationText(citation, courseAssistant.vector_store_id) |
4 | 29 |
|
5 |
| -const Citation = ({ citation }: { citation: FileCitation }) => { |
6 | 30 | return (
|
7 | 31 | <Box>
|
8 | 32 | <Typography variant="body2" color="textSecondary">
|
9 | 33 | {citation.filename} (Index: {citation.index})
|
10 | 34 | </Typography>
|
| 35 | + {isLoading ? ( |
| 36 | + <Typography variant="body2" color="textSecondary"> |
| 37 | + Loading citation text... |
| 38 | + </Typography> |
| 39 | + ) : error ? ( |
| 40 | + <Typography variant="body2" color="error"> |
| 41 | + Error loading citation text: {error.message} |
| 42 | + </Typography> |
| 43 | + ) : ( |
| 44 | + <Typography variant="body2">{text}</Typography> |
| 45 | + )} |
11 | 46 | </Box>
|
12 | 47 | )
|
13 | 48 | }
|
14 | 49 |
|
15 |
| -const MessageCitations = ({ citations }: { citations: FileCitation[] }) => { |
| 50 | +const MessageCitations = ({ citations, courseAssistant }: { citations: FileCitation[]; courseAssistant: CourseAssistant }) => { |
16 | 51 | return (
|
17 | 52 | <Box sx={{ mt: 1, fontSize: '0.875rem', color: 'gray' }}>
|
18 | 53 | Citations:
|
19 | 54 | {citations.map((citation, index) => (
|
20 |
| - <Citation key={index} citation={citation} /> |
| 55 | + <Citation key={index} citation={citation} courseAssistant={courseAssistant} /> |
21 | 56 | ))}
|
22 | 57 | </Box>
|
23 | 58 | )
|
24 | 59 | }
|
25 | 60 |
|
26 | 61 | export const CitationsBox = ({ messages, citations }: { messages: Message[]; citations: FileCitation[] }) => {
|
| 62 | + const { courseId } = useParams() |
| 63 | + const { courseAssistant, isLoading } = useCourseAssistant(courseId) |
| 64 | + |
27 | 65 | const messageCitations = [...messages.map((message) => (Array.isArray(message.citations) ? message.citations : [])), citations]
|
28 | 66 |
|
29 | 67 | console.log('CitationsBox messageCitations', messageCitations)
|
30 | 68 |
|
31 | 69 | return (
|
32 | 70 | <Paper>
|
33 | 71 | <Box sx={{ width: 300, padding: 2 }}>
|
34 |
| - {messageCitations.map((c, index) => ( |
35 |
| - <MessageCitations key={index} citations={c} /> |
36 |
| - ))} |
| 72 | + {courseAssistant ? ( |
| 73 | + messageCitations.map((c, index) => <MessageCitations key={index} citations={c} courseAssistant={courseAssistant} />) |
| 74 | + ) : isLoading ? ( |
| 75 | + <Typography variant="body2" color="textSecondary"> |
| 76 | + Loading course assistant... |
| 77 | + </Typography> |
| 78 | + ) : ( |
| 79 | + <Typography variant="body2" color="error"> |
| 80 | + No course assistant found for this course. |
| 81 | + </Typography> |
| 82 | + )} |
37 | 83 | </Box>
|
38 | 84 | </Paper>
|
39 | 85 | )
|
|
0 commit comments