From 97af1ada5d83d874b8386178ff42e5aaa1bad394 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 15 Nov 2026 21:26:04 -0800 Subject: [PATCH 1/3] fix(search): read the full text of Slack canvases and files instead of the preview --- apps/sim/lib/knowledge/search/diagnostics.ts | 2 + apps/sim/lib/sim-search/live/README.md | 4 +- apps/sim/lib/sim-search/live/drive-content.ts | 94 +------------ apps/sim/lib/sim-search/live/file-text.ts | 105 ++++++++++++++ apps/sim/lib/sim-search/live/google.ts | 4 +- apps/sim/lib/sim-search/live/http.test.ts | 25 ++-- apps/sim/lib/sim-search/live/http.ts | 48 ++++--- apps/sim/lib/sim-search/live/providers.ts | 11 +- apps/sim/lib/sim-search/live/slack.test.ts | 132 ++++++++++++++++++ apps/sim/lib/sim-search/live/slack.ts | 89 +++++++++++- apps/sim/lib/sim-search/live/types.ts | 18 ++- 11 files changed, 402 insertions(+), 130 deletions(-) create mode 100644 apps/sim/lib/sim-search/live/file-text.ts create mode 100644 apps/sim/lib/sim-search/live/slack.test.ts diff --git a/apps/sim/lib/knowledge/search/diagnostics.ts b/apps/sim/lib/knowledge/search/diagnostics.ts index 8bc855b8778..3eaaf3aba91 100644 --- a/apps/sim/lib/knowledge/search/diagnostics.ts +++ b/apps/sim/lib/knowledge/search/diagnostics.ts @@ -239,6 +239,8 @@ type SearchProviderSignal = | 'slack.channel_filter_unresolved' /** The channel lookup failed; the search kept its results without a note. */ | 'slack.channel_lookup_failed' + /** Slack would not serve a file's contents, so its read returned the preview instead. */ + | 'slack.file_preview_fallback' const FAILURE_REASONS = [ 'provider_validation', diff --git a/apps/sim/lib/sim-search/live/README.md b/apps/sim/lib/sim-search/live/README.md index 08a3eb1bcc4..00f6ace7d53 100644 --- a/apps/sim/lib/sim-search/live/README.md +++ b/apps/sim/lib/sim-search/live/README.md @@ -58,7 +58,7 @@ Each event must exist under the source's delegated token, be in an allowed calen ### Slack -Member mode only. Search uses the connected user's Slack real-time search grant and provider search syntax. Date bounds are sent as numeric provider timestamps; date ordering selects keyword retrieval and timestamp sort, because Slack semantic retrieval uses relevance ordering. Channels, messages, direct messages, and files are limited by that grant and provider scopes; there is no admin channel filter in member mode. Missing required search scopes produce a reconnect status. Thread/file reads use the same connected identity. Real-time Search returns at most 20 results a call and counts every page against the member's rate limit, so a search across every source reads one page; a Slack-scoped or native Slack search follows the cursor for up to three pages to fill its limit, and a later page that fails keeps the earlier pages and returns the unread cursor. +Member mode only. Search uses the connected user's Slack real-time search grant and provider search syntax. Date bounds are sent as numeric provider timestamps; date ordering selects keyword retrieval and timestamp sort, because Slack semantic retrieval uses relevance ordering. Channels, messages, direct messages, and files are limited by that grant and provider scopes; there is no admin channel filter in member mode. Missing required search scopes produce a reconnect status. Thread/file reads use the same connected identity. A file read downloads the full text of a canvas (HTML), text snippet, PDF or DOCX from `files.slack.com` only, never a remote file's own URL, within the shared response-size limit; when Slack will not serve it, the read returns the `files.info` preview. Real-time Search returns at most 20 results a call and counts every page against the member's rate limit, so a search across every source reads one page; a Slack-scoped or native Slack search follows the cursor for up to three pages to fill its limit, and a later page that fails keeps the earlier pages and returns the unread cursor. ### GitHub @@ -131,7 +131,7 @@ Self-managed GitLab is resolved from the saved source's validated host/project i | Drive | `GET /drive/v3/files` with Drive `q` | File metadata, Docs/Slides exports, Sheets values, or supported text media | Delegated Drive file visibility and configured folders/types | | Gmail | `GET /gmail/v1/users/me/messages` with Gmail operators | Anchor plus up to seven independently authorized conversation messages | Same-mailbox delegation, labels, date range, category and custom-query checks | | Calendar | CalendarList then `/calendars/{id}/events` | `/calendars/{id}/events/{eventId}` | Same-user delegation, selected calendars, event window/query | -| Slack | `POST /api/assistant.search.context` | `conversations.replies` or `files.info` preview | Member only; Slack enforces the connected user's grant | +| Slack | `POST /api/assistant.search.context` | `conversations.replies`, or the full text of a Slack-hosted canvas, text, PDF or DOCX file (`files.info` preview otherwise) | Member only; Slack enforces the connected user's grant | | Jira | `POST /ex/jira/{cloudId}/rest/api/3/search/jql` | `/rest/api/3/issue/{key}` under that cloud site | Member only | | Confluence | `/ex/confluence/{cloudId}/wiki/rest/api/search` with CQL | v2 `/wiki/api/v2/pages/{id}` or `/blogposts/{id}` (`body-format=view`); a space reads as its homepage | Same site, spaces, current type/status/labels, source readability | | GitHub | `/search/issues`, `/search/code`, `/search/repositories`, `/search/commits` | Issue, repository, commit, or contents endpoint for returned kind | Added repositories; installation coverage/stable IDs and code filters | diff --git a/apps/sim/lib/sim-search/live/drive-content.ts b/apps/sim/lib/sim-search/live/drive-content.ts index 9c284b97512..05ebcbec2c3 100644 --- a/apps/sim/lib/sim-search/live/drive-content.ts +++ b/apps/sim/lib/sim-search/live/drive-content.ts @@ -1,24 +1,10 @@ import { toStringOrNull } from '@sim/utils/coerce' import { toRecord } from '@sim/utils/object' import { isPayloadSizeLimitError } from '@/lib/core/utils/stream-limits' -import { FileParserError, getFileParserErrorCode } from '@/lib/file-parsers/errors' -import { sniffFileKind } from '@/lib/file-parsers/sniff' -import type { FileParseResult } from '@/lib/file-parsers/types' -import { - assertOoxmlArchiveWithinLimits, - DEFAULT_OOXML_SIZE_LIMITS, -} from '@/lib/file-parsers/zip-guard' +import { documentExtension, extractDocumentText } from '@/lib/sim-search/live/file-text' import { NATIVE_RESPONSE_MAX_BYTES, NativeSearchError, segment } from '@/lib/sim-search/live/http' import type { NativeClient } from '@/lib/sim-search/live/types' -const MAX_DRIVE_TEXT_BYTES = 1024 * 1024 -const MAX_DRIVE_PDF_PAGES = 100 -const DRIVE_DOCX_LIMITS = { - ...DEFAULT_OOXML_SIZE_LIMITS, - maxTotalUncompressedBytes: 10 * 1024 * 1024, - maxEntryUncompressedBytes: 4 * 1024 * 1024, -} - /** Extracts bounded text from the exact downloadable PDF or DOCX identified by Drive metadata. */ export async function readDriveFileContent( client: NativeClient, @@ -37,13 +23,7 @@ export async function readDriveFileContent( 'unavailable', 'Drive does not allow downloading this file. Ask the owner to allow downloads or open the source.' ) - const mimeType = toStringOrNull(row.mimeType) - const extension = - mimeType === 'application/pdf' - ? 'pdf' - : mimeType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' - ? 'docx' - : undefined + const extension = documentExtension(toStringOrNull(row.mimeType)) if (!extension) throw new NativeSearchError( 'unavailable', @@ -78,73 +58,5 @@ export async function readDriveFileContent( ) throw error } - signal?.throwIfAborted() - try { - if (bytes.byteLength === 0) throw new FileParserError('empty_input', 'Empty file') - if (bytes.byteLength > NATIVE_RESPONSE_MAX_BYTES) - throw new FileParserError('complexity_limit', 'File exceeds the binary read limit') - const kind = sniffFileKind(bytes, extension) - if (kind === 'encrypted-ooxml') - throw new FileParserError('encrypted_file', 'Encrypted document') - if (kind !== extension) - throw new FileParserError('invalid_format', 'File content does not match its Drive MIME type') - - let parsed: FileParseResult - if (extension === 'pdf') { - const { PdfParser } = await import('@/lib/file-parsers/pdf-parser') - parsed = await new PdfParser().parseBuffer(bytes, { - signal, - pdfTextMode: 'complete', - pdfMaxPages: MAX_DRIVE_PDF_PAGES, - maxTextBytes: MAX_DRIVE_TEXT_BYTES, - }) - } else { - assertOoxmlArchiveWithinLimits(bytes, DRIVE_DOCX_LIMITS) - const { DocxParser } = await import('@/lib/file-parsers/docx-parser') - parsed = await new DocxParser().parseBuffer(bytes, { - signal, - docxTextMode: 'complete', - maxTextBytes: MAX_DRIVE_TEXT_BYTES, - }) - } - signal?.throwIfAborted() - if (parsed.metadata?.degraded || parsed.metadata?.truncated) - throw new FileParserError('complexity_limit', 'Complete document text was not extracted') - if (!parsed.content.trim()) - throw new FileParserError('no_extractable_text', 'No document text was extracted') - if (Buffer.byteLength(parsed.content, 'utf8') > MAX_DRIVE_TEXT_BYTES) - throw new FileParserError('complexity_limit', 'Extracted text exceeds the live-read limit') - return parsed.content - } catch (error) { - signal?.throwIfAborted() - switch (getFileParserErrorCode(error)) { - case 'encrypted_file': - throw new NativeSearchError( - 'unavailable', - 'This file is password-protected. Remove the password from a copy or open the source.' - ) - case 'empty_input': - case 'no_extractable_text': - throw new NativeSearchError( - 'unavailable', - 'No text could be extracted from this file. Image-only scans require OCR; open the source or provide a searchable copy.' - ) - case 'complexity_limit': - throw new NativeSearchError( - 'unavailable', - 'This file exceeds live text extraction limits. Split it into smaller files or open the source.' - ) - case 'invalid_format': - case 'unsupported_type': - throw new NativeSearchError( - 'unavailable', - 'The file content is malformed or does not match its PDF or DOCX type. Re-save it in a supported format or open the source.' - ) - default: - throw new NativeSearchError( - 'unavailable', - 'The file could not be read as text. Retry or open the source.' - ) - } - } + return extractDocumentText(bytes, extension, signal) } diff --git a/apps/sim/lib/sim-search/live/file-text.ts b/apps/sim/lib/sim-search/live/file-text.ts new file mode 100644 index 00000000000..70324912919 --- /dev/null +++ b/apps/sim/lib/sim-search/live/file-text.ts @@ -0,0 +1,105 @@ +import { FileParserError, getFileParserErrorCode } from '@/lib/file-parsers/errors' +import { sniffFileKind } from '@/lib/file-parsers/sniff' +import type { FileParseResult } from '@/lib/file-parsers/types' +import { + assertOoxmlArchiveWithinLimits, + DEFAULT_OOXML_SIZE_LIMITS, +} from '@/lib/file-parsers/zip-guard' +import { NATIVE_RESPONSE_MAX_BYTES, NativeSearchError } from '@/lib/sim-search/live/http' + +/** Extracted text one live file read may return. */ +export const MAX_FILE_TEXT_BYTES = 1024 * 1024 +const MAX_FILE_PDF_PAGES = 100 +const FILE_DOCX_LIMITS = { + ...DEFAULT_OOXML_SIZE_LIMITS, + maxTotalUncompressedBytes: 10 * 1024 * 1024, + maxEntryUncompressedBytes: 4 * 1024 * 1024, +} + +/** The document format `extractDocumentText` reads for a provider-declared MIME type. */ +export function documentExtension(mimeType: string | null | undefined): 'pdf' | 'docx' | undefined { + if (mimeType === 'application/pdf') return 'pdf' + if (mimeType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') + return 'docx' + return undefined +} + +/** + * Extracts the complete, bounded text of a downloaded PDF or DOCX whose bytes must match the type + * its provider declared. Every failure is a NativeSearchError naming what the reader can do. + */ +export async function extractDocumentText( + bytes: Buffer, + extension: 'pdf' | 'docx', + signal?: AbortSignal +): Promise { + signal?.throwIfAborted() + try { + if (bytes.byteLength === 0) throw new FileParserError('empty_input', 'Empty file') + if (bytes.byteLength > NATIVE_RESPONSE_MAX_BYTES) + throw new FileParserError('complexity_limit', 'File exceeds the binary read limit') + const kind = sniffFileKind(bytes, extension) + if (kind === 'encrypted-ooxml') + throw new FileParserError('encrypted_file', 'Encrypted document') + if (kind !== extension) + throw new FileParserError('invalid_format', 'File content does not match its declared type') + + let parsed: FileParseResult + if (extension === 'pdf') { + const { PdfParser } = await import('@/lib/file-parsers/pdf-parser') + parsed = await new PdfParser().parseBuffer(bytes, { + signal, + pdfTextMode: 'complete', + pdfMaxPages: MAX_FILE_PDF_PAGES, + maxTextBytes: MAX_FILE_TEXT_BYTES, + }) + } else { + assertOoxmlArchiveWithinLimits(bytes, FILE_DOCX_LIMITS) + const { DocxParser } = await import('@/lib/file-parsers/docx-parser') + parsed = await new DocxParser().parseBuffer(bytes, { + signal, + docxTextMode: 'complete', + maxTextBytes: MAX_FILE_TEXT_BYTES, + }) + } + signal?.throwIfAborted() + if (parsed.metadata?.degraded || parsed.metadata?.truncated) + throw new FileParserError('complexity_limit', 'Complete document text was not extracted') + if (!parsed.content.trim()) + throw new FileParserError('no_extractable_text', 'No document text was extracted') + if (Buffer.byteLength(parsed.content, 'utf8') > MAX_FILE_TEXT_BYTES) + throw new FileParserError('complexity_limit', 'Extracted text exceeds the live-read limit') + return parsed.content + } catch (error) { + signal?.throwIfAborted() + switch (getFileParserErrorCode(error)) { + case 'encrypted_file': + throw new NativeSearchError( + 'unavailable', + 'This file is password-protected. Remove the password from a copy or open the source.' + ) + case 'empty_input': + case 'no_extractable_text': + throw new NativeSearchError( + 'unavailable', + 'No text could be extracted from this file. Image-only scans require OCR; open the source or provide a searchable copy.' + ) + case 'complexity_limit': + throw new NativeSearchError( + 'unavailable', + 'This file exceeds live text extraction limits. Split it into smaller files or open the source.' + ) + case 'invalid_format': + case 'unsupported_type': + throw new NativeSearchError( + 'unavailable', + 'The file content is malformed or does not match its PDF or DOCX type. Re-save it in a supported format or open the source.' + ) + default: + throw new NativeSearchError( + 'unavailable', + 'The file could not be read as text. Retry or open the source.' + ) + } + } +} diff --git a/apps/sim/lib/sim-search/live/google.ts b/apps/sim/lib/sim-search/live/google.ts index 067cb658f2b..e42ccfba89c 100644 --- a/apps/sim/lib/sim-search/live/google.ts +++ b/apps/sim/lib/sim-search/live/google.ts @@ -124,14 +124,14 @@ export async function readDrive( } else if (document.kind === 'application/vnd.google-apps.spreadsheet') { const metadata = object( await client.json(`/v4/spreadsheets/${segment(id)}`, { - googleService: 'sheets', + service: 'sheets', query: { fields: 'sheets.properties.title' }, }) ) const titles = array(metadata.sheets).map((sheet) => string(object(sheet.properties).title)) const values = object( await client.json(`/v4/spreadsheets/${segment(id)}/values:batchGet`, { - googleService: 'sheets', + service: 'sheets', query: { ranges: titles.slice(0, 20).map((title) => `'${title.replace(/'/g, "''")}'!A1:AZ1000`), valueRenderOption: 'FORMATTED_VALUE', diff --git a/apps/sim/lib/sim-search/live/http.test.ts b/apps/sim/lib/sim-search/live/http.test.ts index 4d4efa0cd49..7a3ecd57472 100644 --- a/apps/sim/lib/sim-search/live/http.test.ts +++ b/apps/sim/lib/sim-search/live/http.test.ts @@ -63,16 +63,25 @@ describe('native search network boundary', () => { await expect(client.json('https://evil.test')).rejects.toThrow('Invalid provider path') expect(inputValidationMockFns.mockSecureFetchWithValidation).not.toHaveBeenCalled() }) - it('cannot move a non-Google token to the Sheets API', async () => { - const client = createNativeClient({ - origin: 'https://api.github.com', - accessToken: 'private', - signal: new AbortController().signal, + it('only moves a token to the sibling host of its own provider', async () => { + const clientFor = (origin: string) => + createNativeClient({ origin, accessToken: 'private', signal: new AbortController().signal }) + await expect( + clientFor('https://api.github.com').json('/v4/spreadsheets/id', { service: 'sheets' }) + ).rejects.toThrow('Invalid provider service') + await expect( + clientFor('https://www.googleapis.com').text('/files-pri/T1-F1/plan', undefined, { + service: 'slackFiles', + }) + ).rejects.toThrow('Invalid provider service') + expect(inputValidationMockFns.mockSecureFetchWithValidation).not.toHaveBeenCalled() + await clientFor('https://slack.com').text('/files-pri/T1-F1/plan', undefined, { + service: 'slackFiles', }) - await expect(client.json('/v4/spreadsheets/id', { googleService: 'sheets' })).rejects.toThrow( - 'Invalid Google service' + expect(inputValidationMockFns.mockSecureFetchWithValidation).toHaveBeenCalledWith( + 'https://files.slack.com/files-pri/T1-F1/plan', + expect.objectContaining({ maxRedirects: 0 }) ) - expect(inputValidationMockFns.mockSecureFetchWithValidation).not.toHaveBeenCalled() }) it('keeps the HTTP status of a provider failure so callers can tell a missing item apart', async () => { const client = createNativeClient({ diff --git a/apps/sim/lib/sim-search/live/http.ts b/apps/sim/lib/sim-search/live/http.ts index 0d878690a1e..be6331f4065 100644 --- a/apps/sim/lib/sim-search/live/http.ts +++ b/apps/sim/lib/sim-search/live/http.ts @@ -6,7 +6,7 @@ import { type SearchFailureReason, withSearchRequestDiagnostics, } from '@/lib/knowledge/search/diagnostics' -import type { NativeClient } from '@/lib/sim-search/live/types' +import type { NativeClient, NativeRequestOptions } from '@/lib/sim-search/live/types' import { isGoogleQuotaReason, safeGoogleErrorReasons, @@ -30,6 +30,18 @@ export const NATIVE_SEARCH_REQUEST_BUDGET = 30 /** Decoded bytes allowed in any native provider response, including file downloads. */ export const NATIVE_RESPONSE_MAX_BYTES = 4 * 1024 * 1024 +/** + * Sibling hosts a provider's token may reach, each only from its own provider origin: Google + * serves Sheets apart from Drive, and Slack serves file contents apart from its Web API. + */ +const NATIVE_SERVICES = { + sheets: { origin: 'https://www.googleapis.com', hostname: 'sheets.googleapis.com' }, + slackFiles: { origin: 'https://slack.com', hostname: 'files.slack.com' }, +} as const + +/** A sibling host a request may target instead of its client's provider origin. */ +export type NativeService = keyof typeof NATIVE_SERVICES + /** Tokens only go to a code-selected provider origin; redirects never carry credentials. */ export function createNativeClient(input: { origin: string @@ -46,12 +58,7 @@ export function createNativeClient(input: { async function request( path: string, options: - | { - query?: Record - body?: unknown - googleService?: 'sheets' - signal?: AbortSignal - } + | (NativeRequestOptions & { query?: Record; body?: unknown }) | undefined, read: (response: Awaited>) => Promise ) { @@ -69,9 +76,10 @@ export function createNativeClient(input: { 'request_budget_exceeded' ) const url = new URL(input.origin) - if (options?.googleService === 'sheets') { - if (url.origin !== 'https://www.googleapis.com') throw new Error('Invalid Google service') - url.hostname = 'sheets.googleapis.com' + if (options?.service) { + const service = NATIVE_SERVICES[options.service] + if (url.origin !== service.origin) throw new Error('Invalid provider service') + url.hostname = service.hostname } if (!path.startsWith('/') || path.startsWith('//') || path.includes('\\')) throw new Error('Invalid provider path') @@ -164,8 +172,10 @@ export function createNativeClient(input: { }, async bytes(path, query, options) { try { - return await request(path, { query, signal: options?.signal }, async (response) => - Buffer.from(await response.arrayBuffer()) + return await request( + path, + { query, service: options?.service, signal: options?.signal }, + async (response) => Buffer.from(await response.arrayBuffer()) ) } catch (error) { input.signal.throwIfAborted() @@ -173,7 +183,11 @@ export function createNativeClient(input: { } }, async text(path, query, options) { - return request(path, { query, signal: options?.signal }, (response) => response.text()) + return request( + path, + { query, service: options?.service, signal: options?.signal }, + (response) => response.text() + ) }, }) } @@ -191,8 +205,10 @@ export function withRequestSignal(client: NativeClient, signal: AbortSignal): Na path, options?.memo && !options.body ? options : { ...options, signal: scoped(options?.signal) } ), - text: (path, query, options) => client.text(path, query, { signal: scoped(options?.signal) }), - bytes: (path, query, options) => client.bytes(path, query, { signal: scoped(options?.signal) }), + text: (path, query, options) => + client.text(path, query, { ...options, signal: scoped(options?.signal) }), + bytes: (path, query, options) => + client.bytes(path, query, { ...options, signal: scoped(options?.signal) }), } } @@ -208,7 +224,7 @@ export function withJsonMemo(client: NativeClient): NativeClient { json(...request) { const [path, options] = request if (!options?.memo || options.body) return client.json(...request) - const key = JSON.stringify([path, options.query, options.googleService]) + const key = JSON.stringify([path, options.query, options.service]) let pending = memo.get(key) if (!pending) { pending = client.json(...request) diff --git a/apps/sim/lib/sim-search/live/providers.ts b/apps/sim/lib/sim-search/live/providers.ts index 8f56a83689a..09f75ab7d48 100644 --- a/apps/sim/lib/sim-search/live/providers.ts +++ b/apps/sim/lib/sim-search/live/providers.ts @@ -177,8 +177,15 @@ export const LIVE_SEARCH_PROVIDERS = { 'joining alternatives with OR or spaces in one query, which keyword retrieval treats as all required; send them as separate native queries.', }, search: searchSlack, - read: (client, reference) => - readSlack(client, reference.id, reference.container, reference.kind, reference.threadId), + read: (client, reference, options) => + readSlack( + client, + reference.id, + reference.container, + reference.kind, + reference.threadId, + options.signal + ), }, jira: { guide: { diff --git a/apps/sim/lib/sim-search/live/slack.test.ts b/apps/sim/lib/sim-search/live/slack.test.ts new file mode 100644 index 00000000000..592ed718ce3 --- /dev/null +++ b/apps/sim/lib/sim-search/live/slack.test.ts @@ -0,0 +1,132 @@ +/** + * @vitest-environment node + */ +import { PDFDocument, StandardFonts } from 'pdf-lib' +import { describe, expect, it, vi } from 'vitest' +import { NativeSearchError } from '@/lib/sim-search/live/http' +import { readSlack } from '@/lib/sim-search/live/slack' + +const FILE_URL = 'https://files.slack.com/files-pri/T1-F123/plan' + +function fileClient(file: Record, download?: () => Promise) { + return { + json: vi.fn(async () => ({ + ok: true, + file: { + title: 'Rollout plan', + permalink: 'https://sim.slack.com/files/U1/F123/plan', + preview_plain_text: 'Short preview', + url_private: FILE_URL, + size: 2048, + ...file, + }, + })), + text: vi.fn(download ?? (async () => 'Full text')), + bytes: vi.fn(), + } +} + +describe('Slack file reads', () => { + it('reads the full text of a canvas from the Slack file host', async () => { + const api = fileClient( + { filetype: 'quip', mimetype: 'application/vnd.slack-docs' }, + async () => '

Rollout

Phase two starts after the audit.

' + ) + const read = await readSlack(api, 'F123', undefined, 'file') + expect(read.content).toContain('Phase two starts after the audit.') + expect(read.content).not.toContain('

') + expect(read.content).not.toContain('Short preview') + expect(api.text).toHaveBeenCalledWith( + '/files-pri/T1-F123/plan', + {}, + expect.objectContaining({ service: 'slackFiles' }) + ) + }) + it('reads the full text of a text snippet', async () => { + const api = fileClient( + { filetype: 'text', mimetype: 'text/plain' }, + async () => 'line one\nline two' + ) + expect((await readSlack(api, 'F123', undefined, 'file')).content).toBe('line one\nline two') + }) + it('extracts the full text of a PDF downloaded from the Slack file host', async () => { + const pdf = await PDFDocument.create() + const font = await pdf.embedFont(StandardFonts.Helvetica) + pdf.addPage().drawText('Phase two starts after the audit.', { x: 50, y: 700, font, size: 12 }) + const api = fileClient({ mimetype: 'application/pdf', filetype: 'pdf' }) + api.bytes.mockResolvedValue(Buffer.from(await pdf.save())) + const read = await readSlack(api, 'F123', undefined, 'file') + expect(read.content).toContain('Phase two starts after the audit.') + expect(api.bytes).toHaveBeenCalledWith( + '/files-pri/T1-F123/plan', + {}, + expect.objectContaining({ service: 'slackFiles' }) + ) + }) + it('never sends the token to a file host other than Slack', async () => { + for (const file of [ + { mimetype: 'text/plain', url_private: 'https://files.example.com/plan.txt' }, + { mimetype: 'text/plain', url_private: 'http://files.slack.com/files-pri/T1-F123/plan' }, + { mimetype: 'text/plain', is_external: true }, + ]) { + const api = fileClient(file) + expect((await readSlack(api, 'F123', undefined, 'file')).content).toContain('Short preview') + expect(api.text).not.toHaveBeenCalled() + } + }) + it('keeps the preview, with the reason Slack gave, when Slack will not serve the file', async () => { + const denied = fileClient({ mimetype: 'text/plain' }, async () => { + throw new NativeSearchError('reconnect', 'The provider denied access.', undefined, 403) + }) + const read = await readSlack(denied, 'F123', undefined, 'file') + expect(read.content).toContain('Full text unavailable: The provider denied access.') + expect(read.content).toContain('Short preview') + for (const failure of [ + new Error('Too many redirects (max: 0)'), + new DOMException('The operation timed out.', 'TimeoutError'), + ]) { + const api = fileClient({ mimetype: 'text/plain' }, async () => { + throw failure + }) + const caller = new AbortController().signal + const read = await readSlack(api, 'F123', undefined, 'file', undefined, caller) + expect(read.content).toContain('Short preview') + } + }) + it('does not let an abandoned download pass as a preview', async () => { + const controller = new AbortController() + const aborted = new DOMException('aborted', 'AbortError') + const api = fileClient({ mimetype: 'text/plain' }, async () => { + controller.abort(aborted) + throw aborted + }) + await expect( + readSlack(api, 'F123', undefined, 'file', undefined, controller.signal) + ).rejects.toBe(aborted) + }) + it('keeps the preview when the file holds more text than one read returns', async () => { + const api = fileClient({ mimetype: 'text/plain', size: undefined }, async () => + 'x'.repeat(1024 * 1024 + 1) + ) + expect((await readSlack(api, 'F123', undefined, 'file')).content).toContain('Short preview') + }) + it('reads markup declared with parameters as HTML', async () => { + const api = fileClient( + { mimetype: 'Text/HTML; charset=utf-8' }, + async () => '

Rendered text

' + ) + expect((await readSlack(api, 'F123', undefined, 'file')).content).toBe('Rendered text') + }) + it('skips the download for files it cannot read as text', async () => { + for (const file of [ + { mimetype: 'image/png', filetype: 'png' }, + { mimetype: 'text/plain', size: 2 * 1024 * 1024 }, + { filetype: 'quip', mimetype: 'application/vnd.slack-docs', size: 3 * 1024 * 1024 }, + ]) { + const api = fileClient(file) + expect((await readSlack(api, 'F123', undefined, 'file')).content).toContain('Short preview') + expect(api.text).not.toHaveBeenCalled() + expect(api.bytes).not.toHaveBeenCalled() + } + }) +}) diff --git a/apps/sim/lib/sim-search/live/slack.ts b/apps/sim/lib/sim-search/live/slack.ts index 8db571be21c..d2595564fdf 100644 --- a/apps/sim/lib/sim-search/live/slack.ts +++ b/apps/sim/lib/sim-search/live/slack.ts @@ -1,6 +1,17 @@ import { recordSearchProviderSignal } from '@/lib/knowledge/search/diagnostics' import { dateSortDirection, nativeDateBounds, nativeText } from '@/lib/sim-search/live/dates' -import { array, NativeSearchError, object, string } from '@/lib/sim-search/live/http' +import { + documentExtension, + extractDocumentText, + MAX_FILE_TEXT_BYTES, +} from '@/lib/sim-search/live/file-text' +import { + array, + NATIVE_RESPONSE_MAX_BYTES, + NativeSearchError, + object, + string, +} from '@/lib/sim-search/live/http' import { joinMessages } from '@/lib/sim-search/live/pages' import { permitsResources } from '@/lib/sim-search/live/policy' import { @@ -8,6 +19,7 @@ import { slackConversationUrl, slackPlainText, } from '@/lib/sim-search/live/slack-format' +import { providerText } from '@/lib/sim-search/live/text' import type { NativeClient, NativeDocument, @@ -17,6 +29,15 @@ import type { /** Slack's own label for an account whose name is unavailable. */ const SLACK_MEMBER = 'Slack member' +/** Time a file download may take before the read settles for the preview it already has. */ +const FILE_DOWNLOAD_TIMEOUT_MS = 5_000 +/** Bytes downloaded per format: markup roughly doubles a canvas's text. */ +const MAX_FILE_DOWNLOAD_BYTES = { + html: 2 * MAX_FILE_TEXT_BYTES, + plain: MAX_FILE_TEXT_BYTES, + pdf: NATIVE_RESPONSE_MAX_BYTES, + docx: NATIVE_RESPONSE_MAX_BYTES, +} as const /** Directory lookups one read may make; a DM or typical thread has only a few authors. */ const MAX_AUTHOR_LOOKUPS = 10 @@ -326,24 +347,86 @@ async function searchSlackPage( } } +type SlackFileFormat = 'html' | 'plain' | 'pdf' | 'docx' + +/** How a file's full text can be read, or undefined when Slack offers only its preview. */ +function slackFileFormat(file: Record): SlackFileFormat | undefined { + const mimetype = string(file.mimetype).split(';')[0].trim().toLowerCase() + if ( + string(file.filetype) === 'quip' || + mimetype === 'application/vnd.slack-docs' || + mimetype === 'text/html' + ) + return 'html' + if (mimetype.startsWith('text/') || mimetype === 'application/json') return 'plain' + return documentExtension(mimetype) +} + +/** + * The full text of a file Slack hosts: canvases as HTML, snippets and text files as text, PDF and + * DOCX extracted. The token only goes to Slack's file host, never a remote file's own URL. Short of + * the caller aborting, any failure (denial, redirect, size limit, unreadable bytes, slow download) + * leaves the read with its preview, plus the provider's reason when it gave one. + */ +async function slackFileText( + client: NativeClient, + file: Record, + signal?: AbortSignal +): Promise<{ text: string } | { unavailable?: string }> { + const format = slackFileFormat(file) + if (!format || file.is_external === true || Number(file.size) > MAX_FILE_DOWNLOAD_BYTES[format]) + return {} + const source = URL.parse(string(file.url_private)) + if (source?.origin !== 'https://files.slack.com') return {} + const timeout = AbortSignal.timeout(FILE_DOWNLOAD_TIMEOUT_MS) + const download = signal ? AbortSignal.any([signal, timeout]) : timeout + const request = { service: 'slackFiles', signal: download } as const + const query = Object.fromEntries(source.searchParams) + try { + const text = + format === 'pdf' || format === 'docx' + ? await extractDocumentText( + await client.bytes(source.pathname, query, request), + format, + download + ) + : providerText(await client.text(source.pathname, query, request), format) + return text && Buffer.byteLength(text, 'utf8') <= MAX_FILE_TEXT_BYTES ? { text } : {} + } catch (error) { + signal?.throwIfAborted() + recordSearchProviderSignal('slack.file_preview_fallback') + return error instanceof NativeSearchError ? { unavailable: error.message } : {} + } +} + export async function readSlack( client: NativeClient, id: string, channel?: string, kind?: string, - threadId?: string + threadId?: string, + signal?: AbortSignal ): Promise { if (kind === 'file') { if (!/^F[A-Z0-9]+$/.test(id)) throw new NativeSearchError('unavailable', 'Invalid Slack file reference.') const data = slackResult(await client.json('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/api/files.info', { query: { file: id } })) const file = object(data.file) + const full = await slackFileText(client, file, signal) return { id, kind, title: string(file.title), url: string(file.permalink), - content: `File preview (open the source for full contents):\n${string(file.preview_plain_text) || string(file.preview) || string(file.title)}`, + content: + 'text' in full + ? full.text + : [ + full.unavailable && `Full text unavailable: ${full.unavailable}`, + `File preview (open the source for full contents):\n${string(file.preview_plain_text) || string(file.preview) || string(file.title)}`, + ] + .filter(Boolean) + .join('\n\n'), } } if ( diff --git a/apps/sim/lib/sim-search/live/types.ts b/apps/sim/lib/sim-search/live/types.ts index 171d6502354..3e3fafb8d1d 100644 --- a/apps/sim/lib/sim-search/live/types.ts +++ b/apps/sim/lib/sim-search/live/types.ts @@ -3,6 +3,7 @@ import type { LiveSearchProvider, NativeSearchQuery, } from '@/lib/api/contracts/mothership-assistant-tools' +import type { NativeService } from '@/lib/sim-search/live/http' import type { LiveSearchPolicy } from '@/lib/sim-search/live/policy-schema' import type { TextMatch } from '@/lib/sim-search/live/text-match' @@ -77,28 +78,33 @@ export interface NativePage { message?: string } +/** Per-request options shared by every native client method. */ +export interface NativeRequestOptions { + /** Sends the request to a sibling host of the client's provider origin. */ + service?: NativeService + /** Cancels this request alongside the client's own signal. */ + signal?: AbortSignal +} + export interface NativeClient { json( path: string, - options?: { + options?: NativeRequestOptions & { query?: Record body?: unknown - googleService?: 'sheets' /** Reuse this client's earlier response to the same GET instead of requesting it again. */ memo?: boolean - /** Cancels this request alongside the client's own signal. */ - signal?: AbortSignal } ): Promise text( path: string, query?: Record, - options?: { signal?: AbortSignal } + options?: NativeRequestOptions ): Promise bytes( path: string, query?: Record, - options?: { signal?: AbortSignal } + options?: NativeRequestOptions ): Promise } From fa4fbbea34141fdb81414baf4e167526b94b122b Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 15 Nov 2026 21:37:01 -0800 Subject: [PATCH 2/3] fix(search): decode Slack text files in their charset and count every preview read --- apps/sim/lib/knowledge/search/diagnostics.ts | 4 +- apps/sim/lib/sim-search/live/slack.test.ts | 57 ++++++++++++++++++-- apps/sim/lib/sim-search/live/slack.ts | 36 +++++++++---- 3 files changed, 81 insertions(+), 16 deletions(-) diff --git a/apps/sim/lib/knowledge/search/diagnostics.ts b/apps/sim/lib/knowledge/search/diagnostics.ts index 3eaaf3aba91..4cb0e6bb2c1 100644 --- a/apps/sim/lib/knowledge/search/diagnostics.ts +++ b/apps/sim/lib/knowledge/search/diagnostics.ts @@ -239,8 +239,8 @@ type SearchProviderSignal = | 'slack.channel_filter_unresolved' /** The channel lookup failed; the search kept its results without a note. */ | 'slack.channel_lookup_failed' - /** Slack would not serve a file's contents, so its read returned the preview instead. */ - | 'slack.file_preview_fallback' + /** A Slack file read returned the preview instead of the file's text. */ + | 'slack.file_preview' const FAILURE_REASONS = [ 'provider_validation', diff --git a/apps/sim/lib/sim-search/live/slack.test.ts b/apps/sim/lib/sim-search/live/slack.test.ts index 592ed718ce3..8db88835d60 100644 --- a/apps/sim/lib/sim-search/live/slack.test.ts +++ b/apps/sim/lib/sim-search/live/slack.test.ts @@ -1,14 +1,20 @@ /** * @vitest-environment node */ + +import { getMockLogger } from '@sim/testing/mocks/logger.mock' import { PDFDocument, StandardFonts } from 'pdf-lib' import { describe, expect, it, vi } from 'vitest' +import { + withSearchDiagnostics, + withSearchProviderDiagnostics, +} from '@/lib/knowledge/search/diagnostics' import { NativeSearchError } from '@/lib/sim-search/live/http' import { readSlack } from '@/lib/sim-search/live/slack' const FILE_URL = 'https://files.slack.com/files-pri/T1-F123/plan' -function fileClient(file: Record, download?: () => Promise) { +function fileClient(file: Record, download?: () => Promise) { return { json: vi.fn(async () => ({ ok: true, @@ -21,8 +27,11 @@ function fileClient(file: Record, download?: () => Promise 'Full text')), - bytes: vi.fn(), + text: vi.fn(), + bytes: vi.fn(async () => { + const body = await (download ?? (async () => 'Full text'))() + return typeof body === 'string' ? Buffer.from(body) : body + }), } } @@ -36,7 +45,7 @@ describe('Slack file reads', () => { expect(read.content).toContain('Phase two starts after the audit.') expect(read.content).not.toContain('

') expect(read.content).not.toContain('Short preview') - expect(api.text).toHaveBeenCalledWith( + expect(api.bytes).toHaveBeenCalledWith( '/files-pri/T1-F123/plan', {}, expect.objectContaining({ service: 'slackFiles' }) @@ -71,7 +80,7 @@ describe('Slack file reads', () => { ]) { const api = fileClient(file) expect((await readSlack(api, 'F123', undefined, 'file')).content).toContain('Short preview') - expect(api.text).not.toHaveBeenCalled() + expect(api.bytes).not.toHaveBeenCalled() } }) it('keeps the preview, with the reason Slack gave, when Slack will not serve the file', async () => { @@ -117,6 +126,44 @@ describe('Slack file reads', () => { ) expect((await readSlack(api, 'F123', undefined, 'file')).content).toBe('Rendered text') }) + it('decodes text in the charset its bytes declare', async () => { + const utf16 = Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from('Größe: 3', 'utf16le')]) + const bom = fileClient({ mimetype: 'text/plain' }, async () => utf16) + expect((await readSlack(bom, 'F123', undefined, 'file')).content).toBe('Größe: 3') + const latin1 = fileClient({ mimetype: 'text/plain; charset=ISO-8859-1' }, async () => + Buffer.from('Größe: 3', 'latin1') + ) + expect((await readSlack(latin1, 'F123', undefined, 'file')).content).toBe('Größe: 3') + }) + it('keeps the preview when text bytes cannot be read in their charset', async () => { + for (const bytes of [ + Buffer.from('Plan: 3', 'utf16le'), + Buffer.from([0x47, 0x72, 0xff, 0xfe, 0x21]), + ]) { + const api = fileClient({ mimetype: 'text/plain' }, async () => bytes) + expect((await readSlack(api, 'F123', undefined, 'file')).content).toContain('Short preview') + } + }) + it('counts every read that returns the preview instead of the text', async () => { + await withSearchDiagnostics({ surface: 'mcp' }, () => + withSearchProviderDiagnostics({ provider: 'slack', accountIndex: 0 }, async () => { + await readSlack(fileClient({ mimetype: 'image/png' }), 'F123', undefined, 'file') + await readSlack( + fileClient({ mimetype: 'text/plain' }, async () => { + throw new Error('Too many redirects (max: 0)') + }), + 'F123', + undefined, + 'file' + ) + await readSlack(fileClient({ mimetype: 'text/plain' }), 'F123', undefined, 'file') + }) + ) + const summary = getMockLogger('KnowledgeSearchDiagnostics').info.mock.calls.find( + ([message]) => message === 'Live search provider completed' + )?.[1] + expect(summary?.signals).toEqual({ 'slack.file_preview': 2 }) + }) it('skips the download for files it cannot read as text', async () => { for (const file of [ { mimetype: 'image/png', filetype: 'png' }, diff --git a/apps/sim/lib/sim-search/live/slack.ts b/apps/sim/lib/sim-search/live/slack.ts index d2595564fdf..295fbd853d0 100644 --- a/apps/sim/lib/sim-search/live/slack.ts +++ b/apps/sim/lib/sim-search/live/slack.ts @@ -362,6 +362,27 @@ function slackFileFormat(file: Record): SlackFileFormat | undef return documentExtension(mimetype) } +/** + * Decodes downloaded text strictly: a byte-order mark wins, then the declared charset, then UTF-8. + * Bytes invalid in that charset, or text holding NULs (UTF-16 without a mark), are unreadable. + */ +function decodeFileText(bytes: Buffer, mimetype: string): string | undefined { + const charset = /;\s*charset="?([\w.:-]+)"?/i.exec(mimetype)?.[1] + const label = + bytes[0] === 0xff && bytes[1] === 0xfe + ? 'utf-16le' + : bytes[0] === 0xfe && bytes[1] === 0xff + ? 'utf-16be' + : (charset ?? 'utf-8') + let text: string + try { + text = new TextDecoder(label, { fatal: true }).decode(bytes) + } catch { + return undefined + } + return text.includes('\u0000') ? undefined : text +} + /** * The full text of a file Slack hosts: canvases as HTML, snippets and text files as text, PDF and * DOCX extracted. The token only goes to Slack's file host, never a remote file's own URL. Short of @@ -383,18 +404,14 @@ async function slackFileText( const request = { service: 'slackFiles', signal: download } as const const query = Object.fromEntries(source.searchParams) try { - const text = - format === 'pdf' || format === 'docx' - ? await extractDocumentText( - await client.bytes(source.pathname, query, request), - format, - download - ) - : providerText(await client.text(source.pathname, query, request), format) + const bytes = await client.bytes(source.pathname, query, request) + if (format === 'pdf' || format === 'docx') + return { text: await extractDocumentText(bytes, format, download) } + const decoded = decodeFileText(bytes, string(file.mimetype)) + const text = decoded && providerText(decoded, format) return text && Buffer.byteLength(text, 'utf8') <= MAX_FILE_TEXT_BYTES ? { text } : {} } catch (error) { signal?.throwIfAborted() - recordSearchProviderSignal('slack.file_preview_fallback') return error instanceof NativeSearchError ? { unavailable: error.message } : {} } } @@ -413,6 +430,7 @@ export async function readSlack( const data = slackResult(await client.json('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/api/files.info', { query: { file: id } })) const file = object(data.file) const full = await slackFileText(client, file, signal) + if (!('text' in full)) recordSearchProviderSignal('slack.file_preview') return { id, kind, From 95a9eadb4ba1d95444e894fe2c3fb996127457e8 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 15 Nov 2026 21:46:06 -0800 Subject: [PATCH 3/3] fix(search): let a UTF-8 byte-order mark override a declared charset --- apps/sim/lib/sim-search/live/slack.test.ts | 3 +++ apps/sim/lib/sim-search/live/slack.ts | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/sim/lib/sim-search/live/slack.test.ts b/apps/sim/lib/sim-search/live/slack.test.ts index 8db88835d60..cb68346ce73 100644 --- a/apps/sim/lib/sim-search/live/slack.test.ts +++ b/apps/sim/lib/sim-search/live/slack.test.ts @@ -134,6 +134,9 @@ describe('Slack file reads', () => { Buffer.from('Größe: 3', 'latin1') ) expect((await readSlack(latin1, 'F123', undefined, 'file')).content).toBe('Größe: 3') + const utf8 = Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from('Größe: 3')]) + const marked = fileClient({ mimetype: 'text/plain; charset=ISO-8859-1' }, async () => utf8) + expect((await readSlack(marked, 'F123', undefined, 'file')).content).toBe('Größe: 3') }) it('keeps the preview when text bytes cannot be read in their charset', async () => { for (const bytes of [ diff --git a/apps/sim/lib/sim-search/live/slack.ts b/apps/sim/lib/sim-search/live/slack.ts index 295fbd853d0..70255d60beb 100644 --- a/apps/sim/lib/sim-search/live/slack.ts +++ b/apps/sim/lib/sim-search/live/slack.ts @@ -362,6 +362,12 @@ function slackFileFormat(file: Record): SlackFileFormat | undef return documentExtension(mimetype) } +const BYTE_ORDER_MARKS = [ + [[0xef, 0xbb, 0xbf], 'utf-8'], + [[0xff, 0xfe], 'utf-16le'], + [[0xfe, 0xff], 'utf-16be'], +] as const + /** * Decodes downloaded text strictly: a byte-order mark wins, then the declared charset, then UTF-8. * Bytes invalid in that charset, or text holding NULs (UTF-16 without a mark), are unreadable. @@ -369,11 +375,9 @@ function slackFileFormat(file: Record): SlackFileFormat | undef function decodeFileText(bytes: Buffer, mimetype: string): string | undefined { const charset = /;\s*charset="?([\w.:-]+)"?/i.exec(mimetype)?.[1] const label = - bytes[0] === 0xff && bytes[1] === 0xfe - ? 'utf-16le' - : bytes[0] === 0xfe && bytes[1] === 0xff - ? 'utf-16be' - : (charset ?? 'utf-8') + BYTE_ORDER_MARKS.find(([mark]) => mark.every((byte, index) => bytes[index] === byte))?.[1] ?? + charset ?? + 'utf-8' let text: string try { text = new TextDecoder(label, { fatal: true }).decode(bytes)