Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions apps/sim/connectors/onedrive/onedrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import type { ConnectorConfig, ExternalDocument, ExternalDocumentList } from '@/
import {
CONNECTOR_MAX_FILE_BYTES,
ConnectorFileTooLargeError,
ConnectorTextExtractionError,
connectorFileExtension,
extractConnectorText,
extractionFailedSkipReason,
isIndexableConnectorFile,
isSkippedDocument,
markSkipped,
parseTagDate,
pipelineParsedMimeType,
readBodyWithLimit,
sizeLimitSkipReason,
stubOrSkipBySize,
Expand Down Expand Up @@ -103,13 +102,19 @@ async function downloadFileContent(accessToken: string, fileId: string): Promise
* Fetches a file and extracts its indexable text — a UTF-8 decode for text
* formats, and the shared knowledge-base parsers for Office documents and PDFs.
*/
async function fetchFileContent(
async function fetchFilePayload(
accessToken: string,
fileId: string,
fileName: string
): Promise<string> {
): Promise<Pick<ExternalDocument, 'content' | 'sourceFile' | 'mimeType'>> {
const buffer = await downloadFileContent(accessToken, fileId)
return extractConnectorText(buffer, fileName)

const mimeType = pipelineParsedMimeType(fileName)
if (mimeType) {
return { content: '', mimeType, sourceFile: { bytes: buffer, fileName, mimeType } }
}

return { content: extractConnectorText(buffer, fileName), mimeType: 'text/plain' }
}

/**
Expand Down Expand Up @@ -377,23 +382,16 @@ export const onedriveConnector: ConnectorConfig = {
if (!item.file || !isIndexableConnectorFile(item.name)) return null

try {
const content = await fetchFileContent(accessToken, item.id, item.name)
if (!content.trim()) return null
const payload = await fetchFilePayload(accessToken, item.id, item.name)
if (!payload.sourceFile && !payload.content.trim()) return null

const stub = fileToStub(item)
return { ...stub, content, contentDeferred: false }
return { ...stub, ...payload, contentDeferred: false }
} catch (error) {
if (error instanceof ConnectorFileTooLargeError) {
logger.info('Skipping oversized OneDrive file', { fileId: item.id, name: item.name })
return markSkipped(fileToStub(item), sizeLimitSkipReason(error.limitBytes))
}
if (error instanceof ConnectorTextExtractionError) {
logger.info('Skipping OneDrive file with no extractable text', {
fileId: item.id,
name: item.name,
})
return markSkipped(fileToStub(item), extractionFailedSkipReason(error.extension))
}
/**
* A transport or Graph failure that survived `fetchWithRetry`. Returning
* `null` would drop the file from the run with no `failed` row and no error
Expand Down
54 changes: 25 additions & 29 deletions apps/sim/connectors/sharepoint/sharepoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'

const { mockFetchWithRetry, mockParseBuffer } = vi.hoisted(() => ({
mockFetchWithRetry: vi.fn(),
mockParseBuffer: vi.fn(),
}))
const { mockFetchWithRetry } = vi.hoisted(() => ({ mockFetchWithRetry: vi.fn() }))

vi.mock('@/lib/knowledge/documents/utils', () => ({
fetchWithRetry: mockFetchWithRetry,
VALIDATE_RETRY_OPTIONS: {},
}))
vi.mock('@/lib/file-parsers', () => ({ parseBuffer: mockParseBuffer }))
vi.mock('@/components/icons', () => ({ MicrosoftSharepointIcon: () => null }))

import {
Expand Down Expand Up @@ -496,45 +492,45 @@ describe('getDocument content extraction', () => {
)
}

it('indexes the parsed text of an Office document', async () => {
mockGraph({ ...itemRoute('f1', 'SOP.docx'), ...contentRoute('f1', 'ignored') })
mockParseBuffer.mockResolvedValue({
content: 'Approved vendor list',
metadata: { extractionMethod: 'mammoth' },
})
/**
* The connector hands an Office document over untouched so the shared pipeline
* parses it — the same path an upload of the same file takes, which is what
* routes PDFs through OCR.
*/
it('delivers an Office document as its source file rather than extracting it', async () => {
mockGraph({ ...itemRoute('f1', 'SOP.docx'), ...contentRoute('f1', 'PK-docx-bytes') })

const doc = await get('f1')

expect(doc?.content).toBe('Approved vendor list')
expect(doc?.skippedReason).toBeUndefined()
expect(doc?.content).toBe('')
expect(doc?.sourceFile?.fileName).toBe('SOP.docx')
expect(doc?.sourceFile?.mimeType).toBe(
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
expect(doc?.sourceFile?.bytes.toString()).toBe('PK-docx-bytes')
expect(doc?.mimeType).toBe(
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
expect(doc?.contentDeferred).toBe(false)
})

/**
* A degraded extraction must become a visible `failed` row, not a silent drop
* and not indexed placeholder text — the same treatment oversized files get.
*/
it('surfaces a degraded extraction as a skipped document with an actionable reason', async () => {
mockGraph({ ...itemRoute('f2', 'Deck.ppt'), ...contentRoute('f2', 'ole2') })
mockParseBuffer.mockResolvedValue({
content: 'Unable to extract text from PowerPoint file.',
metadata: { extractionMethod: 'fallback', degraded: true },
})
it('declares a PDF as application/pdf so the pipeline can route it to OCR', async () => {
mockGraph({ ...itemRoute('f4', 'Contract.pdf'), ...contentRoute('f4', '%PDF-1.7 bytes') })

const doc = await get('f2')
const doc = await get('f4')

expect(doc?.content).toBe('')
expect(doc?.skippedReason).toContain('PPTX')
expect(doc?.externalId).toBe('f2')
expect(doc?.mimeType).toBe('application/pdf')
expect(doc?.sourceFile?.mimeType).toBe('application/pdf')
})

it('reads a text file without invoking a parser', async () => {
it('still extracts a text file itself, since there is nothing for a parser to do', async () => {
mockGraph({ ...itemRoute('f3', 'notes.txt'), ...contentRoute('f3', 'plain notes') })

const doc = await get('f3')

expect(doc?.content).toBe('plain notes')
expect(mockParseBuffer).not.toHaveBeenCalled()
expect(doc?.sourceFile).toBeUndefined()
expect(doc?.mimeType).toBe('text/plain')
})
})

Expand Down
31 changes: 13 additions & 18 deletions apps/sim/connectors/sharepoint/sharepoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import type { ConnectorConfig, ExternalDocument, ExternalDocumentList } from '@/
import {
CONNECTOR_MAX_FILE_BYTES,
ConnectorFileTooLargeError,
ConnectorTextExtractionError,
connectorFileExtension,
extractConnectorText,
extractionFailedSkipReason,
isIndexableConnectorFile,
isSkippedDocument,
markSkipped,
parseTagDate,
pipelineParsedMimeType,
readBodyWithLimit,
sizeLimitSkipReason,
stubOrSkipBySize,
Expand Down Expand Up @@ -214,14 +213,20 @@ async function downloadFileContent(
* Fetches a file and extracts its indexable text — a UTF-8 decode for text
* formats, and the shared knowledge-base parsers for Office documents and PDFs.
*/
async function fetchFileContent(
async function fetchFilePayload(
accessToken: string,
driveId: string,
itemId: string,
fileName: string
): Promise<string> {
): Promise<Pick<ExternalDocument, 'content' | 'sourceFile' | 'mimeType'>> {
const buffer = await downloadFileContent(accessToken, driveId, itemId, fileName)
return extractConnectorText(buffer, fileName)

const mimeType = pipelineParsedMimeType(fileName)
if (mimeType) {
return { content: '', mimeType, sourceFile: { bytes: buffer, fileName, mimeType } }
}

return { content: extractConnectorText(buffer, fileName), mimeType: 'text/plain' }
}

/**
Expand Down Expand Up @@ -925,11 +930,11 @@ export const sharepointConnector: ConnectorConfig = {
}

try {
const content = await fetchFileContent(accessToken, driveId, item.id, item.name)
if (!content.trim()) return null
const payload = await fetchFilePayload(accessToken, driveId, item.id, item.name)
if (!payload.sourceFile && !payload.content.trim()) return null

const stub = itemToStub(item, siteName ?? siteUrl)
return { ...stub, content, contentDeferred: false }
return { ...stub, ...payload, contentDeferred: false }
} catch (error) {
if (error instanceof ConnectorFileTooLargeError) {
logger.info('Skipping oversized SharePoint file', { fileId: item.id, name: item.name })
Expand All @@ -938,16 +943,6 @@ export const sharepointConnector: ConnectorConfig = {
sizeLimitSkipReason(error.limitBytes)
)
}
if (error instanceof ConnectorTextExtractionError) {
logger.info('Skipping SharePoint file with no extractable text', {
fileId: item.id,
name: item.name,
})
return markSkipped(
itemToStub(item, siteName ?? siteUrl),
extractionFailedSkipReason(error.extension)
)
}
/**
* A transport or Graph failure that survived `fetchWithRetry`. Returning
* `null` would drop the file from the run with no `failed` row and no error
Expand Down
21 changes: 20 additions & 1 deletion apps/sim/connectors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,29 @@ export interface ExternalDocument {
externalId: string
/** Document title / filename */
title: string
/** Extracted text content */
/** Extracted text content. Empty when {@link ExternalDocument.sourceFile} carries the document instead. */
content: string
/** MIME type of the content */
mimeType: string
/**
* The source file itself, for connectors that hand over the original document
* rather than text they extracted from it.
*
* Preferred for any format the knowledge base can parse. Extracting inside a
* connector strands the document on a second, weaker parser: the shared
* pipeline routes PDFs to OCR (so scanned pages are readable at all) and owns
* every other format's parser, while a connector doing its own extraction
* stores plain text that no longer declares what it came from.
*
* Carried as one object so the bytes can never disagree with the name and type
* that describe them.
*/
sourceFile?: {
bytes: Buffer
/** Name whose extension names the format, e.g. `Report.pdf`. */
fileName: string
mimeType: string
}
/** Link back to the original document */
sourceUrl?: string
/** Hash of content for change detection (format varies by connector) */
Expand Down
Loading
Loading