@devries-cameron20 / t3code-1 / commits / 3d9aeaf757
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(shared): preview semicolon-separated CSV files as tables (#36738)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Fuksss committed Nov 14, 2026 at 14:36 UTC 3d9aeaf7570c71f19c51271dd55003cbd156d29a
3 files changed +96 -5
apps/web/src/components/files/largeText.worker.ts
+26 -3
@@ -1,4 +1,4 @@
1 -import { parseDelimitedPreview } from "@t3tools/shared/delimitedPreview";
1 +import { csvPreviewDelimiter, parseDelimitedPreview } from "@t3tools/shared/delimitedPreview";
2 import {
3 LARGE_TEXT_MAX_BYTES,
4 LARGE_TEXT_MAX_ROWS,
@@ -12,7 +12,7 @@ const chunks: { start: number; blob: Blob }[] = [];
12 const offsets: Uint32Array[] = [new Uint32Array(65536)];
13 let entries = 1;
14 let size = 0;
15 -let delimiter: "," | "\t" | null = null;
15 +let delimiter: "," | ";" | "\t" | null = null;
16 let complete = false;
17 let columns = 1;
18 function reply(value: TextPreviewResult) {
@@ -51,6 +51,7 @@ async function page(index: number) {
51 maxRows: PREVIEW_PAGE_ROWS,
52 maxColumns: LARGE_TEXT_MAX_COLUMNS,
53 maxCellLength: 1024 * 1024,
54 + detectCsv: false,
55 }).rows
56 : text
57 .replace(/\r?\n$|\r$/, "")
@@ -89,8 +90,30 @@ async function open(input: Extract<TextPreviewRequest, { type: "open" }>) {
90 let rowColumns = 1;
91 let first = false;
92 try {
93 + let prefix: Uint8Array<ArrayBuffer> | undefined;
94 + if (delimiter === ",") {
95 + const parts: Uint8Array<ArrayBuffer>[] = [];
96 + let length = 0;
97 + let quoted = false;
98 + let breaks = 0;
99 + let previous = 0;
100 + while (length <= 1024 * 1024 && breaks < 2) {
101 + const next = await reader.read();
102 + if (next.done) break;
103 + parts.push(next.value);
104 + length += next.value.length;
105 + for (const byte of next.value) {
106 + if (byte === 34) quoted = !quoted;
107 + else if (!quoted && (byte === 13 || (byte === 10 && previous !== 13))) breaks++;
108 + previous = byte;
109 + }
110 + }
111 + prefix = new Uint8Array(await new Blob(parts).arrayBuffer());
112 + delimiter = csvPreviewDelimiter(new TextDecoder().decode(prefix));
113 + }
114 while (true) {
93 - const { value, done } = await reader.read();
115 + const { value, done } = prefix ? { value: prefix, done: false } : await reader.read();
116 + prefix = undefined;
117 if (done) break;
118 if (size + value.length > LARGE_TEXT_MAX_BYTES)
119 throw new Error("Text previews support files up to 256 MB.");
packages/shared/src/delimitedPreview.test.ts
+33
@@ -21,6 +21,39 @@ describe("delimited file previews", () => {
21 ["", "three"],
22 ]);
23 });
24 + it("detects semicolon-separated CSV from its first records outside quotes", () => {
25 + expect(parseDelimitedPreview("a;b\r\n1;2,5", ",").rows).toEqual([
26 + ["a", "b"],
27 + ["1", "2,5"],
28 + ]);
29 + expect(parseDelimitedPreview('name,notes\nAlice,"a;b;c"', ",").rows).toEqual([
30 + ["name", "notes"],
31 + ["Alice", "a;b;c"],
32 + ]);
33 + expect(parseDelimitedPreview('"a, b";notes\nAlice;"say ""hi""\nagain"', ",").rows).toEqual([
34 + ["a, b", "notes"],
35 + ["Alice", 'say "hi"\nagain'],
36 + ]);
37 + expect(parseDelimitedPreview("a,b;c", ",").rows).toEqual([["a", "b;c"]]);
38 + expect(parseDelimitedPreview("id,notes;more;extra\n1,plain\n2,ordinary", ",").rows).toEqual([
39 + ["id", "notes;more;extra"],
40 + ["1", "plain"],
41 + ["2", "ordinary"],
42 + ]);
43 + expect(
44 + parseDelimitedPreview("id,notes;more;extra\n1,plain;other;last\n2,ordinary;next;end", ",")
45 + .rows,
46 + ).toEqual([
47 + ["id", "notes;more;extra"],
48 + ["1", "plain;other;last"],
49 + ["2", "ordinary;next;end"],
50 + ]);
51 + expect(parseDelimitedPreview('12",name\n24",a;b;c', ",").rows).toEqual([
52 + ['12"', "name"],
53 + ['24"', "a;b;c"],
54 + ]);
55 + expect(parseDelimitedPreview("a;b\tvalue", "\t").rows).toEqual([["a;b", "value"]]);
56 + });
57 describe.each([",", "\t"] as const)("with delimiter %j", (delimiter) => {
58 it("preserves a final quoted empty record without requiring a line ending", () => {
59 for (const ending of ["", "\n", "\r\n"]) {
packages/shared/src/delimitedPreview.ts
+37 -2
@@ -12,13 +12,16 @@ export function filePreviewDelimiter(file: { name: string; mimeType?: string }):
12
13 export function parseDelimitedPreview(
14 text: string,
15 - delimiter: "," | "\t",
16 - limits: { maxRows: number; maxColumns: number; maxCellLength: number } = {
15 + delimiter: "," | ";" | "\t",
16 + limits: { maxRows: number; maxColumns: number; maxCellLength: number; detectCsv?: boolean } = {
17 maxRows: 100,
18 maxColumns: 30,
19 maxCellLength: 2000,
20 },
21 ) {
22 + if (delimiter === "," && limits.detectCsv !== false) {
23 + delimiter = csvPreviewDelimiter(text);
24 + }
25 const rows: string[][] = [];
26 let row: string[] = [];
27 let cell = "";
@@ -71,3 +74,35 @@ export function parseDelimitedPreview(
74 }
75 return { rows, truncated: truncated || quoted };
76 }
77 +
78 +export function csvPreviewDelimiter(text: string) {
79 + // Semicolons win only when they are consistent across the first two records and commas are not.
80 + const commas = [0, 0];
81 + const semicolons = [0, 0];
82 + let record = 0;
83 + let second = false;
84 + let quoted = false;
85 + let fieldStart = true;
86 + for (let index = text.charCodeAt(0) === 0xfeff ? 1 : 0; index < text.length; index++) {
87 + const char = text[index];
88 + if (quoted) {
89 + if (char !== '"') continue;
90 + if (text[index + 1] === '"') index++;
91 + else quoted = false;
92 + } else if (char === '"' && fieldStart) quoted = true;
93 + else if (char === "\n" || char === "\r") {
94 + if (record === 1) break;
95 + if (char === "\r" && text[index + 1] === "\n") index++;
96 + record = 1;
97 + fieldStart = true;
98 + continue;
99 + } else if (char === ",") commas[record]!++;
100 + else if (char === ";") semicolons[record]!++;
101 + second ||= record === 1;
102 + fieldStart = char === "," || char === ";";
103 + }
104 + if (semicolons[0]! <= commas[0]!) return ",";
105 + if (!second) return ";";
106 + const consistentCommas = commas[0]! > 0 && commas[1] === commas[0];
107 + return semicolons[1] === semicolons[0] && !consistentCommas ? ";" : ",";
108 +}