@@ -83,6 +83,10 @@ import {
8383 MAX_KNOWLEDGE_CONNECTOR_DOCUMENT_PAGE_SIZE ,
8484 MAX_KNOWLEDGE_CONNECTOR_DOCUMENT_SEARCH_LENGTH ,
8585} from '@/lib/knowledge/constants'
86+ import {
87+ isImageExtractionEnabled ,
88+ SEMANTIC_IMAGE_MIME_TYPES ,
89+ } from '@/lib/knowledge/documents/image-extraction-policy'
8690import {
8791 documentProcessingOutcomeSelection ,
8892 failedDocumentCondition ,
@@ -1270,6 +1274,47 @@ export const listKnowledgeConnectorDocuments = defineAuthorizedKnowledgeUseCase(
12701274 } ,
12711275} )
12721276
1277+ /**
1278+ * An excluded image keeps its indexed text, and turning image attachments off
1279+ * skips excluded rows so the exclusion survives turning them back on. Restoring
1280+ * one while images are off would re-enable text the source no longer lists.
1281+ */
1282+ async function assertImageRestoreAllowed (
1283+ connector : { id : string ; connectorType : string } ,
1284+ documentIds : string [ ]
1285+ ) {
1286+ const imageExtraction = getConnectorMeta ( connector . connectorType ) ?. imageExtraction
1287+ if ( ! imageExtraction ) return
1288+ const [ stored ] = await db
1289+ . select ( { sourceConfig : knowledgeConnector . sourceConfig } )
1290+ . from ( knowledgeConnector )
1291+ . where ( eq ( knowledgeConnector . id , connector . id ) )
1292+ . limit ( 1 )
1293+ if (
1294+ ! stored ||
1295+ isImageExtractionEnabled ( imageExtraction , stored . sourceConfig as Record < string , unknown > )
1296+ ) {
1297+ return
1298+ }
1299+ const [ image ] = await db
1300+ . select ( { id : document . id } )
1301+ . from ( document )
1302+ . where (
1303+ and (
1304+ eq ( document . connectorId , connector . id ) ,
1305+ inArray ( document . id , documentIds ) ,
1306+ inArray ( document . mimeType , [ ...SEMANTIC_IMAGE_MIME_TYPES ] )
1307+ )
1308+ )
1309+ . limit ( 1 )
1310+ if ( image ) {
1311+ throw new OrchestrationError (
1312+ 'validation' ,
1313+ 'Turn on image attachments for this connector before restoring an image.'
1314+ )
1315+ }
1316+ }
1317+
12731318export const updateKnowledgeConnectorDocuments = defineAuthorizedKnowledgeUseCase ( {
12741319 operation : knowledgeOperations . updateConnectorDocuments ,
12751320 resolveContext : ( {
@@ -1294,6 +1339,7 @@ export const updateKnowledgeConnectorDocuments = defineAuthorizedKnowledgeUseCas
12941339 if ( restoring && ! requiresConnectorIndexing ( context . knowledgeBase . isSearchIndex ) ) {
12951340 throw new OrchestrationError ( 'validation' , 'This search index is inactive; use Sim Search.' )
12961341 }
1342+ if ( restoring ) await assertImageRestoreAllowed ( context . connector , documentIds )
12971343 const updated = await db
12981344 . update ( document )
12991345 . set ( { userExcluded : ! restoring , enabled : restoring } )
0 commit comments