@@ -5,7 +5,7 @@ import { useLocation, useNavigate } from 'react-router-dom'
55import { useI18n } from 'twake-i18n'
66
77import { useClient } from 'cozy-client'
8- import { extractText , chatCompletion } from 'cozy-client/dist/models/ai'
8+ import { chatCompletion } from 'cozy-client/dist/models/ai'
99import { fetchBlobFileById } from 'cozy-client/dist/models/file'
1010import flag from 'cozy-flags'
1111import logger from 'cozy-logger'
@@ -21,9 +21,9 @@ import Stack from 'cozy-ui/transpiled/react/Stack'
2121import Typography from 'cozy-ui/transpiled/react/Typography'
2222import { useAlert } from 'cozy-ui/transpiled/react/providers/Alert'
2323
24+ import { extractFileContent , validateContentSize } from './helpers'
2425import { SUMMARY_SYSTEM_PROMPT , getSummaryUserPrompt } from './prompts'
2526import styles from './styles.styl'
26- import { roughTokensEstimation } from '../../helpers'
2727import { useViewer } from '../../providers/ViewerProvider'
2828
2929const AIAssistantPanel = ( { className } ) => {
@@ -52,22 +52,10 @@ const AIAssistantPanel = ({ className }) => {
5252 const summarizeFile = async ( { client, file, stream = false , model } ) => {
5353 try {
5454 const fileBlob = await fetchBlobFileById ( client , file ?. _id )
55+ const textContent = await extractFileContent ( client , fileBlob , file )
5556
56- const rawTextContent = await extractText ( client , fileBlob , {
57- name : file . name ,
58- mime : file . mime
59- } )
60- const textContent = rawTextContent ? JSON . stringify ( rawTextContent ) : ''
61-
62- const summaryConfig = flag ( 'drive.summary' )
63- if (
64- summaryConfig ?. maxTokens &&
65- roughTokensEstimation ( textContent ) > summaryConfig . maxTokens
66- ) {
67- const error = new Error ( 'DOCUMENT_TOO_LARGE' )
68- error . code = 'DOCUMENT_TOO_LARGE'
69- throw error
70- }
57+ const { maxTokens } = flag ( 'drive.summary' ) ?? { }
58+ validateContentSize ( textContent , maxTokens )
7159
7260 const messages = [
7361 { role : 'system' , content : SUMMARY_SYSTEM_PROMPT } ,
@@ -89,23 +77,22 @@ const AIAssistantPanel = ({ className }) => {
8977 }
9078 }
9179
92- const persistedSummary = async (
93- fileMetadata ,
94- targetFileId ,
95- summaryContent
96- ) => {
97- try {
98- await client
99- . collection ( 'io.cozy.files' )
100- . updateMetadataAttribute ( targetFileId , {
101- ...fileMetadata ,
102- description : summaryContent
103- } )
104- fetchedFileIdRef . current = targetFileId
105- } catch ( error ) {
106- logger . error ( 'Error when persisting summary to file metadata:' , error )
107- }
108- }
80+ const persistedSummary = useCallback (
81+ async ( fileMetadata , targetFileId , summaryContent ) => {
82+ try {
83+ await client
84+ . collection ( 'io.cozy.files' )
85+ . updateMetadataAttribute ( targetFileId , {
86+ ...fileMetadata ,
87+ description : summaryContent
88+ } )
89+ fetchedFileIdRef . current = targetFileId
90+ } catch ( error ) {
91+ logger . error ( 'Error when persisting summary to file metadata:' , error )
92+ }
93+ } ,
94+ [ client ]
95+ )
10996
11097 useEffect ( ( ) => {
11198 activeFileIdRef . current = file ?. _id || null
@@ -142,10 +129,14 @@ const AIAssistantPanel = ({ className }) => {
142129 await persistedSummary ( fileMetadata , targetFileId , summaryContent )
143130 } catch ( err ) {
144131 if ( activeFileIdRef . current === targetFileId ) {
145- const errorMessage =
146- err . code === 'DOCUMENT_TOO_LARGE'
147- ? t ( 'Viewer.ai.error.documentTooLarge' )
148- : t ( 'Viewer.ai.error.summary' )
132+ let errorMessage = t ( 'Viewer.ai.error.summary' )
133+
134+ if ( err . code === 'DOCUMENT_TOO_LARGE' ) {
135+ errorMessage = t ( 'Viewer.ai.error.documentTooLarge' )
136+ } else if ( err . code === 'CONTENT_EXTRACTION_FAILED' ) {
137+ errorMessage = t ( 'Viewer.ai.error.extractContent' )
138+ }
139+
149140 setError ( errorMessage )
150141 }
151142 } finally {
@@ -157,7 +148,7 @@ const AIAssistantPanel = ({ className }) => {
157148 }
158149 }
159150 } ,
160- [ client , file , t ]
151+ [ client , file , persistedSummary , t ]
161152 )
162153
163154 const handleRefresh = ( ) => {
0 commit comments