Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(files): read text files without a recognized extension in the app (#9328)

gbirmanb0659b0b9bb4
diff --git a/apps/web/src/features/block-unknown/component/Block.tsx b/apps/web/src/features/block-unknown/component/Block.tsx
index 55b3367f54..86a40ab91f 100644
--- a/apps/web/src/features/block-unknown/component/Block.tsx
+++ b/apps/web/src/features/block-unknown/component/Block.tsx
@@ -35,7 +35,7 @@ import {
} from '../legacy-office';
import { useGetFileBlob } from '../signal/blockData';
import { TopBar } from './TopBar';
-import { UnknownContent } from './UnknownContent';
+import { UnknownFilePreview } from './UnknownFilePreview';
const UploadedWorkbook = lazy(
() => import('../../block-spreadsheet/views/UploadedWorkbook')
@@ -91,7 +91,8 @@ function BlockUnknownContent() {
<Show
when={spreadsheet()}
fallback={
- <UnknownContent
+ <UnknownFilePreview
+ file={blockMetadataSignal.get()}
fileName={fileName()}
convertingTo={convertingTo()}
onShare={openShare}
diff --git a/apps/web/src/features/block-unknown/component/UnknownFilePreview.test.tsx b/apps/web/src/features/block-unknown/component/UnknownFilePreview.test.tsx
new file mode 100644
index 0000000000..647b19c64f
--- /dev/null
+++ b/apps/web/src/features/block-unknown/component/UnknownFilePreview.test.tsx
@@ -0,0 +1,134 @@
+import { cleanup, render, screen, waitFor } from '@solidjs/testing-library';
+import { QueryClient, QueryClientProvider } from '@tanstack/solid-query';
+import { err, ok } from 'neverthrow';
+import { afterEach, beforeAll, beforeEach, expect, it, vi } from 'vitest';
+
+const mocks = vi.hoisted(() => ({
+ getFileDocumentUrl: vi.fn(),
+ fetchPresignedBlobWithProgress: vi.fn(),
+}));
+
+vi.mock('@queries/storage/file-document', () => ({
+ getFileDocumentUrl: mocks.getFileDocumentUrl,
+}));
+
+vi.mock('@service-storage/util/fetchPresigned', () => ({
+ fetchPresignedBlobWithProgress: mocks.fetchPresignedBlobWithProgress,
+}));
+
+import { TEXT_PREVIEW_MAX_BYTES } from '../core/text-preview';
+import { UnknownFilePreview } from './UnknownFilePreview';
+
+/** jsdom's Blob has no arrayBuffer(). */
+function fileBlob(bytes: Uint8Array) {
+ return { size: bytes.length, arrayBuffer: async () => bytes.buffer };
+}
+
+function renderPreview(fileType: string | null, client = new QueryClient()) {
+ return render(() => (
+ <QueryClientProvider client={client}>
+ <UnknownFilePreview
+ file={{ documentId: 'doc', documentVersionId: 7, fileType }}
+ fileName="qa-test-attach"
+ onShare={vi.fn()}
+ onDownload={vi.fn()}
+ />
+ </QueryClientProvider>
+ ));
+}
+
+// The static renderer is a slow chunk to load, so the lazy view resolves at once.
+beforeAll(() => import('./UnknownTextContent'), 60_000);
+
+beforeEach(() => {
+ mocks.getFileDocumentUrl.mockResolvedValue('https://files.test/doc');
+});
+
+afterEach(() => {
+ cleanup();
+ vi.clearAllMocks();
+});
+
+it('shows a text file literally, never as Markdown', async () => {
+ mocks.fetchPresignedBlobWithProgress.mockResolvedValue(
+ ok(
+ fileBlob(new TextEncoder().encode('# Not a heading\n\n **indented**\n'))
+ )
+ );
+
+ const { container } = renderPreview(null);
+
+ await waitFor(() => screen.getByText('# Not a heading'));
+ expect(container.textContent).toContain(' **indented**');
+ expect(container.querySelectorAll('p')).toHaveLength(1);
+ expect(container.querySelector('h1')).toBeNull();
+ expect(container.querySelector('strong')).toBeNull();
+ expect(screen.queryByText(/No preview available/)).toBeNull();
+ expect(mocks.fetchPresignedBlobWithProgress).toHaveBeenCalledWith(
+ 'https://files.test/doc',
+ expect.any(Function),
+ expect.anything(),
+ TEXT_PREVIEW_MAX_BYTES
+ );
+});
+
+it('offers the file when its bytes are not text', async () => {
+ mocks.fetchPresignedBlobWithProgress.mockResolvedValue(
+ ok(fileBlob(new Uint8Array([0x00, 0x01, 0x02])))
+ );
+
+ renderPreview(null);
+
+ await waitFor(() => screen.getByText(/No preview available/));
+ expect(screen.getByText('Download')).toBeTruthy();
+});
+
+it('offers the file when it is empty', async () => {
+ mocks.fetchPresignedBlobWithProgress.mockResolvedValue(
+ ok(fileBlob(new Uint8Array()))
+ );
+
+ renderPreview(null);
+
+ await waitFor(() => screen.getByText(/No preview available/));
+});
+
+it('offers the file when it is too large or the download fails', async () => {
+ mocks.fetchPresignedBlobWithProgress.mockResolvedValue(
+ err([{ code: 'UNKNOWN_ERROR', message: 'File exceeds the limit.' }])
+ );
+
+ renderPreview(null);
+
+ await waitFor(() => screen.getByText(/No preview available/));
+});
+
+it('offers the file when its download link fails', async () => {
+ mocks.getFileDocumentUrl.mockRejectedValue(new Error('no location'));
+
+ renderPreview(null);
+
+ await waitFor(() => screen.getByText(/No preview available/));
+});
+
+it('tries again when a file that failed to load is opened again', async () => {
+ const client = new QueryClient();
+ mocks.getFileDocumentUrl.mockRejectedValueOnce(new Error('offline'));
+ mocks.fetchPresignedBlobWithProgress.mockResolvedValue(
+ ok(fileBlob(new TextEncoder().encode('back online\n')))
+ );
+
+ const firstOpen = renderPreview(null, client);
+ await waitFor(() => screen.getByText(/No preview available/));
+ firstOpen.unmount();
+
+ renderPreview(null, client);
+ await waitFor(() => screen.getByText('back online'));
+});
+
+it('never downloads a known binary type', async () => {
+ renderPreview('zip');
+
+ await waitFor(() => screen.getByText(/No preview available/));
+ expect(mocks.fetchPresignedBlobWithProgress).not.toHaveBeenCalled();
+});
diff --git a/apps/web/src/features/block-unknown/component/UnknownFilePreview.tsx b/apps/web/src/features/block-unknown/component/UnknownFilePreview.tsx
new file mode 100644
index 0000000000..7d3fcacacc
--- /dev/null
+++ b/apps/web/src/features/block-unknown/component/UnknownFilePreview.tsx
@@ -0,0 +1,53 @@
+import { ContentLoading } from '@components/app/ContentLoading';
+import { lazyNamed } from '@core/util/lazyNamed';
+import { useQuery } from '@tanstack/solid-query';
+import { Match, Suspense, Switch } from 'solid-js';
+import {
+ type TextPreviewFile,
+ textPreviewQueryOptions,
+} from '../queries/text-preview';
+import { UnknownContent } from './UnknownContent';
+
+const UnknownTextContent = lazyNamed(
+ () => import('./UnknownTextContent'),
+ 'UnknownTextContent'
+);
+
+/**
+ * Prop-driven unknown file body shared by the legacy block and Drive detail
+ * hosts: text reads in place, anything else offers the file itself.
+ */
+export function UnknownFilePreview(props: {
+ file: TextPreviewFile | undefined;
+ fileName: string;
+ convertingTo?: string;
+ onShare: () => void;
+ onDownload: () => void;
+}) {
+ const preview = useQuery(() => textPreviewQueryOptions(props.file));
+ const text = () => (preview.isSuccess ? preview.data : null);
+
+ return (
+ <Switch
+ fallback={
+ <UnknownContent
+ fileName={props.fileName}
+ convertingTo={props.convertingTo}
+ onShare={props.onShare}
+ onDownload={props.onDownload}
+ />
+ }
+ >
+ <Match when={preview.isLoading}>
+ <ContentLoading />
+ </Match>
+ <Match when={text()}>
+ {(value) => (
+ <Suspense fallback={<ContentLoading />}>
+ <UnknownTextContent text={value()} />
+ </Suspense>
+ )}
+ </Match>
+ </Switch>
+ );
+}
diff --git a/apps/web/src/features/block-unknown/component/UnknownTextContent.tsx b/apps/web/src/features/block-unknown/component/UnknownTextContent.tsx
new file mode 100644
index 0000000000..c980fb8aea
--- /dev/null
+++ b/apps/web/src/features/block-unknown/component/UnknownTextContent.tsx
@@ -0,0 +1,18 @@
+import {
+ StaticLexical,
+ StaticMarkdownContext,
+} from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';
+import { textPreviewState } from '../core/text-preview';
+
+/** Shows a file's text as written: each line stays literal, never Markdown. */
+export function UnknownTextContent(props: { text: string }) {
+ return (
+ <div class="size-full select-text overflow-y-auto overscroll-contain touch:pt-(--mobile-content-inset-top) touch:pb-(--mobile-content-inset-bottom)">
+ <div class="wrap-anywhere whitespace-pre-wrap px-6 py-4 font-mono text-sm">
+ <StaticMarkdownContext>
+ <StaticLexical serializedState={textPreviewState(props.text)} />
+ </StaticMarkdownContext>
+ </div>
+ </div>
+ );
+}
diff --git a/apps/web/src/features/block-unknown/core/text-preview.test.ts b/apps/web/src/features/block-unknown/core/text-preview.test.ts
new file mode 100644
index 0000000000..2b2092eefe
--- /dev/null
+++ b/apps/web/src/features/block-unknown/core/text-preview.test.ts
@@ -0,0 +1,85 @@
+import { describe, expect, it } from 'vitest';
+import {
+ decodeTextPreview,
+ mayContainText,
+ TEXT_PREVIEW_MAX_LINES,
+ textPreviewState,
+} from './text-preview';
+
+const bytes = (text: string) => new TextEncoder().encode(text);
+
+describe('mayContainText', () => {
+ it('reads a file whose name had no recognized extension', () => {
+ expect(mayContainText(null)).toBe(true);
+ expect(mayContainText(undefined)).toBe(true);
+ });
+
+ it('reads text types that have no block of their own', () => {
+ expect(mayContainText('env')).toBe(true);
+ expect(mayContainText('ics')).toBe(true);
+ expect(mayContainText('vcf')).toBe(true);
+ });
+
+ it('never downloads a file of a known binary type', () => {
+ expect(mayContainText('zip')).toBe(false);
+ expect(mayContainText('exe')).toBe(false);
+ expect(mayContainText('doc')).toBe(false);
+ expect(mayContainText('ppt')).toBe(false);
+ expect(mayContainText('xls')).toBe(false);
+ });
+
+ it('leaves a type this build does not know as a download', () => {
+ expect(mayContainText('from-a-newer-server')).toBe(false);
+ });
+});
+
+describe('decodeTextPreview', () => {
+ it('keeps text as written, without the final newline', () => {
+ expect(decodeTextPreview(bytes('QA test attachment\n'))).toBe(
+ 'QA test attachment'
+ );
+ expect(decodeTextPreview(bytes('# title\n\n indented\n'))).toBe(
+ '# title\n\n indented'
+ );
+ });
+
+ it('keeps blank lines the file ends with beyond the final newline', () => {
+ expect(decodeTextPreview(bytes('a\n\n\n'))).toBe('a\n\n');
+ });
+
+ it('normalizes Windows and classic Mac line endings', () => {
+ expect(decodeTextPreview(bytes('a\r\nb\rc\n'))).toBe('a\nb\nc');
+ });
+
+ it('reads non-ASCII text and drops a byte order mark', () => {
+ expect(decodeTextPreview(bytes('héllo 你好'))).toBe('héllo 你好');
+ expect(
+ decodeTextPreview(new Uint8Array([0xef, 0xbb, 0xbf, 0x68, 0x69]))
+ ).toBe('hi');
+ });
+
+ it('leaves a file with too many lines to render as a download', () => {
+ const atLimit = 'a\n'.repeat(TEXT_PREVIEW_MAX_LINES);
+
+ expect(decodeTextPreview(bytes(atLimit))).not.toBeNull();
+ expect(decodeTextPreview(bytes(`${atLimit}a\n`))).toBeNull();
+ });
+
+ it('rejects binary content', () => {
+ expect(decodeTextPreview(new Uint8Array([0x68, 0x00, 0x69]))).toBeNull();
+ expect(decodeTextPreview(new Uint8Array([0xff, 0xd8, 0xff]))).toBeNull();
+ });
+});
+
+describe('textPreviewState', () => {
+ it('keeps every line in one paragraph, so copied text has no extra blank lines', () => {
+ const { root } = JSON.parse(textPreviewState('one\n\n three'));
+
+ expect(root.children).toHaveLength(1);
+ expect(
+ root.children[0].children.map(
+ (node: { type: string; text?: string }) => node.text ?? node.type
+ )
+ ).toEqual(['one', 'linebreak', 'linebreak', ' three']);
+ });
+});
diff --git a/apps/web/src/features/block-unknown/core/text-preview.ts b/apps/web/src/features/block-unknown/core/text-preview.ts
new file mode 100644
index 0000000000..59cd7f738c
--- /dev/null
+++ b/apps/web/src/features/block-unknown/core/text-preview.ts
@@ -0,0 +1,77 @@
+import { FileTypeMap } from '@service-storage/fileTypeMap';
+import { Result } from 'neverthrow';
+
+// The static renderer mounts every line, so bigger files stay a download. Lines
+// need a limit of their own: a megabyte of short lines freezes the tab.
+export const TEXT_PREVIEW_MAX_BYTES = 1_000_000;
+export const TEXT_PREVIEW_MAX_LINES = 20_000;
+
+const FILE_TYPES: Record<string, { mime: string } | undefined> = FileTypeMap;
+
+/**
+ * Text types with a block of their own never reach the unknown block. A
+ * missing file type means the name had no recognized extension, so only the
+ * bytes can tell.
+ */
+export function mayContainText(fileType: string | null | undefined): boolean {
+ if (!fileType) return true;
+ return FILE_TYPES[fileType]?.mime.startsWith('text/') ?? false;
+}
+
+const decodeUtf8 = Result.fromThrowable((bytes: Uint8Array) =>
+ new TextDecoder('utf-8', { fatal: true }).decode(bytes)
+);
+
+/**
+ * The file's text with one newline style, or null when it has no preview:
+ * it is not UTF-8 text, or has too many lines to render.
+ */
+export function decodeTextPreview(bytes: Uint8Array): string | null {
+ if (bytes.includes(0)) return null;
+ const decoded = decodeUtf8(bytes);
+ if (decoded.isErr()) return null;
+ const text = decoded.value.replace(/\r\n?/g, '\n').replace(/\n$/, '');
+ return text.split('\n').length > TEXT_PREVIEW_MAX_LINES ? null : text;
+}
+
+/**
+ * A Lexical state holding the text as one paragraph of line breaks. A paragraph
+ * per line would put a blank line between every line of copied text.
+ */
+export function textPreviewState(text: string): string {
+ const children = text.split('\n').flatMap((line, index) => [
+ ...(index > 0 ? [{ type: 'linebreak', version: 1 }] : []),
+ ...(line
+ ? [
+ {
+ type: 'text',
+ version: 1,
+ text: line,
+ format: 0,
+ detail: 0,
+ mode: 'normal',
+ style: '',
+ },
+ ]
+ : []),
+ ]);
+ return JSON.stringify({
+ root: {
+ type: 'root',
+ version: 1,
+ direction: null,
+ format: '',
+ indent: 0,
+ children: [
+ {
+ type: 'paragraph',
+ version: 1,
+ direction: null,
+ format: '',
+ indent: 0,
+ children,
+ },
+ ],
+ },
+ });
+}
diff --git a/apps/web/src/features/block-unknown/queries/keys.ts b/apps/web/src/features/block-unknown/queries/keys.ts
new file mode 100644
index 0000000000..d4866f9ab3
--- /dev/null
+++ b/apps/web/src/features/block-unknown/queries/keys.ts
@@ -0,0 +1,7 @@
+import { createQueryKeys } from '@lukemorales/query-key-factory';
+
+export const unknownFileKeys = createQueryKeys('unknownFile', {
+ textPreview: (id: string | undefined, version: number | undefined) => ({
+ queryKey: [id, version],
+ }),
+});
diff --git a/apps/web/src/features/block-unknown/queries/text-preview.ts b/apps/web/src/features/block-unknown/queries/text-preview.ts
new file mode 100644
index 0000000000..7051682137
--- /dev/null
+++ b/apps/web/src/features/block-unknown/queries/text-preview.ts
@@ -0,0 +1,66 @@
+import { throwOnErr } from '@core/util/result';
+import { type Span, Telemetry } from '@macro-inc/observability';
+import { getFileDocumentUrl } from '@queries/storage/file-document';
+import { fetchPresignedBlobWithProgress } from '@service-storage/util/fetchPresigned';
+import { queryOptions, skipToken } from '@tanstack/solid-query';
+import {
+ decodeTextPreview,
+ mayContainText,
+ TEXT_PREVIEW_MAX_BYTES,
+} from '../core/text-preview';
+import { unknownFileKeys } from './keys';
+
+export type TextPreviewFile = {
+ documentId: string;
+ documentVersionId: number;
+ fileType?: string | null;
+};
+
+function fetchTextFile(url: string, signal: AbortSignal, parent: Span) {
+ return parent.span('file.transfer', async (span) => {
+ const blob = await fetchPresignedBlobWithProgress(
+ url,
+ () => {},
+ { signal },
+ TEXT_PREVIEW_MAX_BYTES
+ );
+ if (blob.isOk()) span.setAttr('file.size', blob.value.size);
+ return blob;
+ });
+}
+
+/**
+ * Resolves to null when the file holds no previewable text, which is final for
+ * this version. A failed read rejects instead, so it is not cached and opening
+ * the file again retries.
+ */
+function loadTextPreview(file: TextPreviewFile, signal: AbortSignal) {
+ return Telemetry.span('file.text_preview', async (span) => {
+ span.setAttr('document.id', file.documentId);
+ span.setAttr('document.version_id', file.documentVersionId);
+ span.setAttr('file.text_preview.outcome', 'unavailable');
+ const url = await getFileDocumentUrl(file, span);
+ signal.throwIfAborted();
+ const blob = await throwOnErr(() => fetchTextFile(url, signal, span));
+ const text = decodeTextPreview(new Uint8Array(await blob.arrayBuffer()));
+ span.setAttr('file.text_preview.outcome', text ? 'text' : 'no_preview');
+ return text;
+ });
+}
+
+/** Cached by immutable file version. */
+export function textPreviewQueryOptions(file: TextPreviewFile | undefined) {
+ return queryOptions({
+ queryKey: unknownFileKeys.textPreview(
+ file?.documentId,
+ file?.documentVersionId
+ ).queryKey,
+ staleTime: Infinity,
+ gcTime: 60_000,
+ retry: false,
+ queryFn:
+ file && mayContainText(file.fileType)
+ ? ({ signal }) => loadTextPreview(file, signal)
+ : skipToken,
+ });
+}
diff --git a/apps/web/src/features/drive-view/views/UnknownDetail.tsx b/apps/web/src/features/drive-view/views/UnknownDetail.tsx
index 028cce8c93..58cc524d61 100644
--- a/apps/web/src/features/drive-view/views/UnknownDetail.tsx
+++ b/apps/web/src/features/drive-view/views/UnknownDetail.tsx
@@ -1,4 +1,4 @@
-import { UnknownContent } from '@block-unknown/component/UnknownContent';
+import { UnknownFilePreview } from '@block-unknown/component/UnknownFilePreview';
import { getPermissions } from '@core/component/SharePermissions';
import { useShareModal } from '@core/component/TopBar/shareModal';
import { createFileDownload } from '@core/util/createFileDownload';
@@ -65,7 +65,8 @@ export function UnknownDetailDocument(props: {
operations,
download,
})}
- <UnknownContent
+ <UnknownFilePreview
+ file={props.data.documentMetadata}
fileName={props.data.documentMetadata.documentName}
onShare={openShare}
onDownload={() =>
diff --git a/docs/agent-guide/files.md b/docs/agent-guide/files.md
index ab79f7ea0f..6180074ebf 100644
--- a/docs/agent-guide/files.md
+++ b/docs/agent-guide/files.md
@@ -199,6 +199,24 @@ title and navigation goes through the hamburger overlay. Location, search,
filters, expanded folders, list focus, and scroll position are restored when
returning from an opened file.
+## Reading unrecognized files
+
+A file Macro has no viewer for reads in place when its content is text: one whose
+name has no recognized extension, or a text type such as `.env`, `.ics`, `.vcf`,
+or `.dot`. Both `/app/unknown/<id>` (channel attachments, mentions) and the Drive
+detail show it read-only through the static Lexical renderer, one literal line
+per file line, never as Markdown.
+
+- Upload a file named `notes` containing `# heading` and an indented line. Open
+ it from a channel and from Drive: the heading stays literal, the indentation
+ stays, and the header keeps Download and Share. Select several lines and copy:
+ the copy has no extra blank lines.
+- Binary content, empty files, text that is not UTF-8, and files over 1 MB or
+ 20,000 lines keep **No preview available** with Share and Download. Known
+ binary types such as `.zip` never download for this check; legacy Office files
+ keep their conversion message.
+- `.txt` and other code types open in the code editor instead.
+
## Downloading files
Open a video, image, code file, canvas, or other uploaded file in Drive. Its