|
| 1 | +import cx from 'classnames' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import React, { useCallback, useState, useEffect } from 'react' |
| 4 | +import { useLocation, useNavigate } from 'react-router-dom' |
| 5 | + |
| 6 | +import { useClient } from 'cozy-client' |
| 7 | +import { extractText, chatCompletion } from 'cozy-client/dist/models/ai' |
| 8 | +import { fetchBlobFileById } from 'cozy-client/dist/models/file' |
| 9 | +import Button from 'cozy-ui/transpiled/react/Buttons' |
| 10 | +import Icon from 'cozy-ui/transpiled/react/Icon' |
| 11 | +import IconButton from 'cozy-ui/transpiled/react/IconButton' |
| 12 | +import AssistantIcon from 'cozy-ui/transpiled/react/Icons/Assistant' |
| 13 | +import CopyIcon from 'cozy-ui/transpiled/react/Icons/Copy' |
| 14 | +import CrossMediumIcon from 'cozy-ui/transpiled/react/Icons/CrossMedium' |
| 15 | +import RefreshIcon from 'cozy-ui/transpiled/react/Icons/Refresh' |
| 16 | +import Paper from 'cozy-ui/transpiled/react/Paper' |
| 17 | +import Stack from 'cozy-ui/transpiled/react/Stack' |
| 18 | +import Typography from 'cozy-ui/transpiled/react/Typography' |
| 19 | +import { useAlert } from 'cozy-ui/transpiled/react/providers/Alert' |
| 20 | +import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n' |
| 21 | + |
| 22 | +import styles from './styles.styl' |
| 23 | +import { useViewer } from '../../providers/ViewerProvider' |
| 24 | + |
| 25 | +const AIAssistantPanel = () => { |
| 26 | + const { t } = useI18n() |
| 27 | + const client = useClient() |
| 28 | + const { file, setIsOpenAiAssistant } = useViewer() |
| 29 | + const { showAlert } = useAlert() |
| 30 | + |
| 31 | + const [isLoading, setIsLoading] = useState(true) |
| 32 | + const [summary, setSummary] = useState('') |
| 33 | + const [error, setError] = useState(null) |
| 34 | + |
| 35 | + const location = useLocation() |
| 36 | + const navigate = useNavigate() |
| 37 | + |
| 38 | + const handleClose = () => { |
| 39 | + setIsOpenAiAssistant(false) |
| 40 | + if (location?.state?.showAIAssistant) { |
| 41 | + navigate('..') |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const summarizeFile = async ({ |
| 46 | + client, |
| 47 | + file, |
| 48 | + stream = false, |
| 49 | + model |
| 50 | + }) => { |
| 51 | + try { |
| 52 | + const fileBlob = await fetchBlobFileById(client, file?._id) |
| 53 | + |
| 54 | + const textContent = await extractText(client, fileBlob, { |
| 55 | + name: file.name, |
| 56 | + mime: file.mime || file.class |
| 57 | + }) |
| 58 | + |
| 59 | + const messages = [ |
| 60 | + { role: 'system', content: 'You are an expert in summarizing documents.' }, |
| 61 | + { |
| 62 | + role: 'user', |
| 63 | + content: `Please provide a concise summary of the following document:\n\n${textContent}` |
| 64 | + } |
| 65 | + ] |
| 66 | + |
| 67 | + const summaryResponse = await chatCompletion(client, messages, { |
| 68 | + stream, |
| 69 | + model |
| 70 | + }) |
| 71 | + |
| 72 | + return summaryResponse |
| 73 | + } catch (error) { |
| 74 | + console.error('Error in summarizeFile:', error) |
| 75 | + throw error |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const fetchSummary = useCallback(async () => { |
| 80 | + if (!file) return |
| 81 | + |
| 82 | + setIsLoading(true) |
| 83 | + setError(null) |
| 84 | + try { |
| 85 | + const response = await summarizeFile({ client, file, stream: false }) |
| 86 | + if (response && response.content) { |
| 87 | + setSummary(response.content) |
| 88 | + } else if (response && response.choices && response.choices[0]) { |
| 89 | + setSummary(response.choices[0].message.content) |
| 90 | + } |
| 91 | + } catch (err) { |
| 92 | + setError(err.message || 'Failed to generate summary') |
| 93 | + } finally { |
| 94 | + setIsLoading(false) |
| 95 | + } |
| 96 | + }, [client, file, t]) |
| 97 | + |
| 98 | + const handleRefresh = () => { |
| 99 | + fetchSummary() |
| 100 | + } |
| 101 | + |
| 102 | + const handleCopy = () => { |
| 103 | + if (summary) { |
| 104 | + navigator.clipboard.writeText(summary) |
| 105 | + showAlert({ message: t('Viewer.ai.copied'), severity: 'success' }) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + useEffect(() => { |
| 110 | + fetchSummary() |
| 111 | + }, [fetchSummary]) |
| 112 | + |
| 113 | + return ( |
| 114 | + <> |
| 115 | + <Stack spacing="s" className={cx('u-flex u-flex-column u-h-100')}> |
| 116 | + <Paper |
| 117 | + className={cx({ |
| 118 | + 'u-flex-grow-1': !isLoading |
| 119 | + })} |
| 120 | + elevation={2} |
| 121 | + square |
| 122 | + > |
| 123 | + <div className="u-flex u-flex-items-center u-flex-justify-between u-h-3 u-ph-1 u-flex-shrink-0"> |
| 124 | + <Typography variant="h4"> |
| 125 | + <Icon icon={AssistantIcon} /> {t('Viewer.ai.panelTitle')} |
| 126 | + </Typography> |
| 127 | + <IconButton aria-label="Close AI Assistant" onClick={handleClose}> |
| 128 | + <Icon icon={CrossMediumIcon} /> |
| 129 | + </IconButton> |
| 130 | + </div> |
| 131 | + {!isLoading && ( |
| 132 | + <Stack spacing="s" className="u-ph-1"> |
| 133 | + <div> |
| 134 | + <div className="u-flex u-flex-items-center u-flex-justify-between u-mb-1"> |
| 135 | + <Typography variant="subtitle1">{t('Viewer.ai.bodyText')}</Typography> |
| 136 | + <div className="u-flex"> |
| 137 | + <IconButton size="small" onClick={handleRefresh}> |
| 138 | + <Icon icon={RefreshIcon} /> |
| 139 | + </IconButton> |
| 140 | + {summary && ( |
| 141 | + <IconButton size="small" onClick={handleCopy}> |
| 142 | + <Icon icon={CopyIcon} /> |
| 143 | + </IconButton> |
| 144 | + )} |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + <Typography className="u-mb-1"> |
| 148 | + {error ? ( |
| 149 | + <span style={{ color: 'var(--errorColor)' }}>{error}</span> |
| 150 | + ) : ( |
| 151 | + summary |
| 152 | + )} |
| 153 | + </Typography> |
| 154 | + {!isLoading && summary && ( |
| 155 | + <Typography variant="caption" color="textSecondary"> |
| 156 | + {t('Viewer.ai.footerText')} |
| 157 | + </Typography> |
| 158 | + )} |
| 159 | + </div> |
| 160 | + </Stack> |
| 161 | + )} |
| 162 | + </Paper> |
| 163 | + {isLoading ? ( |
| 164 | + <> |
| 165 | + <div className={styles.loaderContainer}> |
| 166 | + <div className={styles.loaderBar} /> |
| 167 | + </div> |
| 168 | + <div className="u-flex u-flex-items-center u-flex-justify-between u-ph-1"> |
| 169 | + <Typography |
| 170 | + variant="body1" |
| 171 | + className="u-flex u-flex-items-center" |
| 172 | + > |
| 173 | + <Icon |
| 174 | + icon={AssistantIcon} |
| 175 | + color="var(--primaryColor)" |
| 176 | + className="u-mr-1" |
| 177 | + /> |
| 178 | + {t('Viewer.ai.loadingText')} |
| 179 | + </Typography> |
| 180 | + <Button |
| 181 | + size="small" |
| 182 | + variant="text" |
| 183 | + color="default" |
| 184 | + label={t('Viewer.ai.stop')} |
| 185 | + onClick={handleClose} |
| 186 | + /> |
| 187 | + </div> |
| 188 | + </> |
| 189 | + ) : null} |
| 190 | + </Stack> |
| 191 | + </> |
| 192 | + ) |
| 193 | +} |
| 194 | + |
| 195 | +AIAssistantPanel.propTypes = { |
| 196 | + isLoading: PropTypes.bool, |
| 197 | + summary: PropTypes.string, |
| 198 | + onStop: PropTypes.func, |
| 199 | + onSend: PropTypes.func |
| 200 | +} |
| 201 | + |
| 202 | +AIAssistantPanel.defaultProps = { |
| 203 | + isLoading: false, |
| 204 | + summary: '' |
| 205 | +} |
| 206 | + |
| 207 | +export default AIAssistantPanel |
0 commit comments