AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
feat(csv): read, create, filter, sort, dedupe, merge and split CSV files, and convert CSV to Excel (#15942)
Odai Ahmad36d1393ff6e7
diff --git a/bun.lock b/bun.lockindex d25adaf9530..0392709b0f3 100644--- a/bun.lock+++ b/bun.lock@@ -10368,7 +10368,7 @@},"packages/pieces/core/csv": {"name": "@activepieces/piece-csv",- "version": "0.4.22",+ "version": "0.5.0","dependencies": {"@activepieces/core-piece-types": "workspace:*","@activepieces/core-utils": "workspace:*",diff --git a/packages/pieces/core/csv/package.json b/packages/pieces/core/csv/package.jsonindex 3274c7bcf9b..49a21db4cdc 100644--- a/packages/pieces/core/csv/package.json+++ b/packages/pieces/core/csv/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/piece-csv",- "version": "0.4.22",+ "version": "0.5.0","main": "./dist/src/index.js","types": "./dist/src/index.d.ts","dependencies": {diff --git a/packages/pieces/core/csv/src/i18n/translation.json b/packages/pieces/core/csv/src/i18n/translation.jsonindex eb50ff1ea20..b1025395069 100644--- a/packages/pieces/core/csv/src/i18n/translation.json+++ b/packages/pieces/core/csv/src/i18n/translation.json@@ -2,17 +2,120 @@"Manipulate CSV text": "Manipulate CSV text","Convert CSV to JSON": "Convert CSV to JSON","Convert JSON to CSV": "Convert JSON to CSV",+ "Convert Excel to CSV": "Convert Excel to CSV",+ "Read CSV File": "Read CSV File",+ "Create CSV File": "Create CSV File",+ "Convert CSV to Excel": "Convert CSV to Excel",+ "Filter CSV Rows": "Filter CSV Rows",+ "Sort CSV Rows": "Sort CSV Rows",+ "Remove Duplicate Rows": "Remove Duplicate Rows",+ "Select Columns": "Select Columns",+ "Merge CSV Files": "Merge CSV Files",+ "Split CSV": "Split CSV","This function reads a CSV string and converts it into JSON array format.": "This function reads a CSV string and converts it into JSON array format.","This function reads a JSON array and converts it into CSV format.": "This function reads a JSON array and converts it into CSV format.",+ "Converts an Excel file (.xlsx or .xls) into CSV text.": "Converts an Excel file (.xlsx or .xls) into CSV text.",+ "Reads a CSV file (for example an email attachment) into rows you can loop over.": "Reads a CSV file (for example an email attachment) into rows you can loop over.",+ "Turns a list of rows into a .csv file you can attach to an email or upload.": "Turns a list of rows into a .csv file you can attach to an email or upload.",+ "Turns CSV text into an Excel (.xlsx) file.": "Turns CSV text into an Excel (.xlsx) file.",+ "Keeps only the rows where a column matches a condition.": "Keeps only the rows where a column matches a condition.",+ "Sorts the rows by one column, as text, numbers or dates.": "Sorts the rows by one column, as text, numbers or dates.",+ "Removes repeated rows from CSV text, comparing whole rows or chosen columns.": "Removes repeated rows from CSV text, comparing whole rows or chosen columns.",+ "Keeps, reorders or removes columns in CSV text.": "Keeps, reorders or removes columns in CSV text.",+ "Stacks several CSVs into one, matching columns by name.": "Stacks several CSVs into one, matching columns by name.",+ "Splits CSV text into smaller CSVs with a set number of rows each.": "Splits CSV text into smaller CSVs with a set number of rows each.","CSV Text": "CSV Text","Does the CSV have headers?": "Does the CSV have headers?","Delimiter Type": "Delimiter Type","Markdown": "Markdown","JSON Array": "JSON Array",+ "Excel File": "Excel File",+ "Sheet Name": "Sheet Name",+ "Delimiter": "Delimiter",+ "CSV File": "CSV File",+ "Has Header Row": "Has Header Row",+ "Trim Spaces": "Trim Spaces",+ "Rows": "Rows",+ "File Name": "File Name",+ "Include Header Row": "Include Header Row",+ "Columns": "Columns",+ "Excel-Friendly Encoding": "Excel-Friendly Encoding",+ "Protect Against Spreadsheet Formulas": "Protect Against Spreadsheet Formulas",+ "Store Numbers as Numbers": "Store Numbers as Numbers",+ "Column": "Column",+ "Condition": "Condition",+ "Value": "Value",+ "Case Sensitive": "Case Sensitive",+ "Direction": "Direction",+ "Compare As": "Compare As",+ "Compare Columns": "Compare Columns",+ "Keep": "Keep",+ "Ignore Surrounding Spaces": "Ignore Surrounding Spaces",+ "Mode": "Mode",+ "CSV Texts": "CSV Texts",+ "Rows per Chunk": "Rows per Chunk",+ "Repeat Header Row": "Repeat Header Row","Select the delimiter type for the CSV text.": "Select the delimiter type for the CSV text.","\n**Notes**:\n* The input should be a JSON array.\n* The JSON object will be flattened If nested and the keys will be used as headers.\n": "\n**Notes**:\n* The input should be a JSON array.\n* The JSON object will be flattened If nested and the keys will be used as headers.\n","Provide a JSON array to convert to CSV format.": "Provide a JSON array to convert to CSV format.","Select the delimiter type for the CSV file.": "Select the delimiter type for the CSV file.",+ "The Excel file (.xlsx or .xls) to convert to CSV.": "The Excel file (.xlsx or .xls) to convert to CSV.",+ "Name of the sheet to convert. Leave blank to use the first sheet.": "Name of the sheet to convert. Leave blank to use the first sheet.",+ "Character used to separate values in the output CSV.": "Character used to separate values in the output CSV.",+ "The .csv file to read. Map a file from an earlier step, or upload one.": "The .csv file to read. Map a file from an earlier step, or upload one.",+ "The first row holds column names. Off: column_1, column_2, …": "The first row holds column names. Off: column_1, column_2, …",+ "The character that separates columns. Auto-detect picks comma, semicolon, tab or pipe from the first rows.": "The character that separates columns. Auto-detect picks comma, semicolon, tab or pipe from the first rows.",+ "Remove spaces around each value.": "Remove spaces around each value.",+ "A list of objects, one per row, e.g. [{\"name\":\"Ann\"}].": "A list of objects, one per row, e.g. [{\"name\":\"Ann\"}].",+ "\".csv\" is added if you leave out the extension.": "\".csv\" is added if you leave out the extension.",+ "The character that separates columns in the file. Many European Excel versions expect semicolons.": "The character that separates columns in the file. Many European Excel versions expect semicolons.",+ "Write the column names as the first row.": "Write the column names as the first row.",+ "Columns to write, in order. Leave empty to write all columns.": "Columns to write, in order. Leave empty to write all columns.",+ "Adds a byte-order mark so Excel shows accented characters correctly.": "Adds a byte-order mark so Excel shows accented characters correctly.",+ "Stops text starting with = + - @ from running as a formula.": "Stops text starting with = + - @ from running as a formula.",+ "Paste CSV text or map it from an earlier step.": "Paste CSV text or map it from an earlier step.",+ "The first row holds column names and always stays text.": "The first row holds column names and always stays text.",+ "Up to 31 characters, without : \\ / ? * [ or ].": "Up to 31 characters, without : \\ / ? * [ or ].",+ "\".xlsx\" is added if you leave out the extension.": "\".xlsx\" is added if you leave out the extension.",+ "Exact numbers become number cells; values like 01234 stay text.": "Exact numbers become number cells; values like 01234 stay text.",+ "Paste the CSV, or map CSV text from an earlier step. The first row must be the column names.": "Paste the CSV, or map CSV text from an earlier step. The first row must be the column names.",+ "The column name to check, exactly as in the header row.": "The column name to check, exactly as in the header row.",+ "The value to compare with. Not used for \"Is empty\" and \"Is not empty\".": "The value to compare with. Not used for \"Is empty\" and \"Is not empty\".",+ "On: \"Paid\" and \"paid\" are different.": "On: \"Paid\" and \"paid\" are different.",+ "The column name to sort by, exactly as in the header row.": "The column name to sort by, exactly as in the header row.",+ "How to compare values. Unreadable values go last.": "How to compare values. Unreadable values go last.",+ "Columns that must match, e.g. \"email\". Empty compares whole rows.": "Columns that must match, e.g. \"email\". Empty compares whole rows.",+ "On: \"[email protected]\" and \"[email protected]\" are different.": "On: \"[email protected]\" and \"[email protected]\" are different.",+ "On: \" Ann\" and \"Ann\" count as the same.": "On: \" Ann\" and \"Ann\" count as the same.",+ "Column names as in the header. Keep mode uses this order.": "Column names as in the header. Keep mode uses this order.",+ "Two or more CSV texts, one per item. Map each from an earlier step.": "Two or more CSV texts, one per item. Map each from an earlier step.",+ "Data rows per chunk (1 to 10,000), not counting the header.": "Data rows per chunk (1 to 10,000), not counting the header.",+ "Start every chunk with the column names.": "Start every chunk with the column names.","Comma": "Comma",- "Tab": "Tab"+ "Tab": "Tab",+ "Comma (,)": "Comma (,)",+ "Semicolon (;)": "Semicolon (;)",+ "Auto-detect": "Auto-detect",+ "Pipe (|)": "Pipe (|)",+ "Equals": "Equals",+ "Does not equal": "Does not equal",+ "Contains": "Contains",+ "Does not contain": "Does not contain",+ "Starts with": "Starts with",+ "Ends with": "Ends with",+ "Is empty": "Is empty",+ "Is not empty": "Is not empty",+ "Greater than": "Greater than",+ "Greater than or equal to": "Greater than or equal to",+ "Less than": "Less than",+ "Less than or equal to": "Less than or equal to",+ "Ascending (A → Z, 1 → 9, oldest first)": "Ascending (A → Z, 1 → 9, oldest first)",+ "Descending (Z → A, 9 → 1, newest first)": "Descending (Z → A, 9 → 1, newest first)",+ "Text": "Text",+ "Number": "Number",+ "Date": "Date",+ "First occurrence": "First occurrence",+ "Last occurrence": "Last occurrence",+ "Keep only these columns": "Keep only these columns",+ "Remove these columns": "Remove these columns"}\ No newline at end of filediff --git a/packages/pieces/core/csv/src/index.ts b/packages/pieces/core/csv/src/index.tsindex ecfd229d5a1..d07366f191b 100644--- a/packages/pieces/core/csv/src/index.ts+++ b/packages/pieces/core/csv/src/index.ts@@ -3,6 +3,15 @@ import { PieceCategory } from '@activepieces/pieces-framework';import { csvToJsonAction } from './lib/actions/convert-csv-to-json';import { jsonToCsvAction } from './lib/actions/convert-json-to-csv';import { excelToCsvAction } from './lib/actions/convert-excel-to-csv';+import { readCsvFileAction } from './lib/actions/read-csv-file';+import { createCsvFileAction } from './lib/actions/create-csv-file';+import { convertCsvToExcelAction } from './lib/actions/convert-csv-to-excel';+import { filterCsvRowsAction } from './lib/actions/filter-csv-rows';+import { sortCsvRowsAction } from './lib/actions/sort-csv-rows';+import { removeDuplicateCsvRowsAction } from './lib/actions/remove-duplicate-csv-rows';+import { selectCsvColumnsAction } from './lib/actions/select-csv-columns';+import { mergeCsvAction } from './lib/actions/merge-csv';+import { splitCsvAction } from './lib/actions/split-csv';export const csv = createPiece({displayName: 'CSV',@@ -11,7 +20,20 @@ export const csv = createPiece({logoUrl: 'https://cdn.activepieces.com/pieces/new-core/csv.svg',auth: PieceAuth.None(),categories: [PieceCategory.CORE],- actions: [csvToJsonAction, jsonToCsvAction, excelToCsvAction],+ actions: [+ csvToJsonAction,+ jsonToCsvAction,+ excelToCsvAction,+ readCsvFileAction,+ createCsvFileAction,+ convertCsvToExcelAction,+ filterCsvRowsAction,+ sortCsvRowsAction,+ removeDuplicateCsvRowsAction,+ selectCsvColumnsAction,+ mergeCsvAction,+ splitCsvAction,+ ],authors: ["kishanprmr", "MoShizzle", "khaledmashaly", "abuaboud", 'sanket-a11y'],triggers: [],});diff --git a/packages/pieces/core/csv/src/lib/actions/convert-csv-to-excel.ts b/packages/pieces/core/csv/src/lib/actions/convert-csv-to-excel.tsnew file mode 100644index 00000000000..e331d1dfdb6--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/convert-csv-to-excel.ts@@ -0,0 +1,95 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import * as XLSX from 'xlsx';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { convertCsvToExcelOutputSchema } from '../output-schemas';++export const convertCsvToExcelAction = createAction({+ audience: 'both',+ name: 'convert_csv_to_excel',+ classification: 'READ',+ displayName: 'Convert CSV to Excel',+ description: 'Turns CSV text into an Excel (.xlsx) file.',+ aiMetadata: {+ description:+ 'Converts CSV text into a one-sheet .xlsx workbook file, keeping values such as zip codes with leading zeros and long ids as text and optionally storing numbers as number cells only when they fit in 15 significant digits, so the value never changes. The header row, when present, always stays text. Pick this when the recipient needs a spreadsheet file; use Convert Excel to CSV for the reverse and Create CSV File when a .csv file is enough. Up to 20 MB and 100,000 rows; the same input gives the same workbook content, so it is idempotent.',+ idempotent: true,+ },+ props: {+ csv_text: Property.LongText({+ displayName: 'CSV Text',+ description: 'Paste CSV text or map it from an earlier step.',+ placeholder: 'name,email,city',+ required: true,+ }),+ delimiter: csvProps.inputDelimiter(),+ has_header_row: Property.Checkbox({+ displayName: 'Has Header Row',+ description: 'The first row holds column names and always stays text.',+ required: false,+ defaultValue: true,+ }),+ sheet_name: Property.ShortText({+ displayName: 'Sheet Name',+ description: 'Up to 31 characters, without : \\ / ? * [ or ].',+ placeholder: 'Sheet1',+ required: false,+ defaultValue: 'Sheet1',+ }),+ file_name: Property.ShortText({+ displayName: 'File Name',+ description: '".xlsx" is added if you leave out the extension.',+ placeholder: 'data.xlsx',+ required: false,+ defaultValue: 'data.xlsx',+ }),+ detect_numbers: Property.Checkbox({+ displayName: 'Store Numbers as Numbers',+ description:+ 'Exact numbers become number cells; values like 01234 stay text.',+ required: false,+ defaultValue: true,+ }),+ },+ outputSchema: convertCsvToExcelOutputSchema,+ async run(context) {+ const { csv_text, delimiter, has_header_row, sheet_name, file_name, detect_numbers } =+ context.propsValue;+ const text = csvUtils.assertCsvText({ value: csv_text });++ const sheetName = (sheet_name ?? '').trim() || 'Sheet1';+ if (sheetName.length > 31 || INVALID_SHEET_CHARS.test(sheetName)) {+ throw new Error(+ `"${sheetName}" is not a valid sheet name. Use up to 31 characters, without : \\ / ? * [ or ].`,+ );+ }++ const hasHeader = has_header_row !== false;+ const records = csvUtils.parseCsvRecords({+ text,+ delimiter: csvUtils.resolveDelimiter({ text, delimiter }),+ hasHeader,+ });+ const toNumbers = detect_numbers !== false;+ const cells = records.map((record, index) =>+ record.map((value) =>+ toNumbers && !(hasHeader && index === 0) && csvUtils.isExactNumber(value) ? Number(value) : value,+ ),+ );++ const workbook = XLSX.utils.book_new();+ XLSX.utils.book_append_sheet(workbook, XLSX.utils.aoa_to_sheet(cells), sheetName);+ const data: Buffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer' });++ const name = csvUtils.safeFileName({ requested: file_name, fallback: 'data.xlsx', extension: 'xlsx' });+ const url = await context.files.write({ fileName: name, data });+ return {+ file_name: name,+ url,+ row_count: records.length,+ sheet_name: sheetName,+ };+ },+});++const INVALID_SHEET_CHARS = /[:\\/?*[\]]/;diff --git a/packages/pieces/core/csv/src/lib/actions/create-csv-file.ts b/packages/pieces/core/csv/src/lib/actions/create-csv-file.tsnew file mode 100644index 00000000000..c68311d96eb--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/create-csv-file.ts@@ -0,0 +1,131 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { flatten } from 'safe-flat';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { createCsvFileOutputSchema } from '../output-schemas';++export const createCsvFileAction = createAction({+ audience: 'both',+ name: 'create_csv_file',+ classification: 'READ',+ displayName: 'Create CSV File',+ description: 'Turns a list of rows into a .csv file you can attach to an email or upload.',+ aiMetadata: {+ description:+ 'Builds a .csv file from a JSON array of objects: every key seen in any row becomes a column (nested objects become dotted columns like address.city), booleans are written as true/false and missing values stay blank, with optional column order, delimiter, header row and an Excel-friendly UTF-8 byte-order mark. Text starting with = + - @ is prefixed with an apostrophe by default so spreadsheet apps do not run it as a formula (plain numbers like -5 are left alone). Pick this when the next step needs a file; use Convert JSON to CSV when you only need the CSV text. Up to 100,000 rows; nothing outside the run changes and the same input gives the same file content, so it is idempotent.',+ idempotent: true,+ },+ props: {+ json_array: Property.Json({+ displayName: 'Rows',+ description:+ 'A list of objects, one per row, e.g. [{"name":"Ann"}].',+ required: true,+ defaultValue: [+ { name: 'Ann', email: '[email protected]', paid: true },+ { name: 'Bob', email: '[email protected]', paid: false },+ ],+ }),+ file_name: Property.ShortText({+ displayName: 'File Name',+ description: '".csv" is added if you leave out the extension.',+ placeholder: 'export.csv',+ required: false,+ defaultValue: 'export.csv',+ }),+ delimiter: csvProps.outputDelimiter(),+ include_header: Property.Checkbox({+ displayName: 'Include Header Row',+ description: 'Write the column names as the first row.',+ required: false,+ defaultValue: true,+ }),+ columns: Property.Array({+ displayName: 'Columns',+ description:+ 'Columns to write, in order. Leave empty to write all columns.',+ required: false,+ }),+ add_excel_bom: Property.Checkbox({+ displayName: 'Excel-Friendly Encoding',+ description:+ 'Adds a byte-order mark so Excel shows accented characters correctly.',+ required: false,+ defaultValue: false,+ }),+ escape_formulas: Property.Checkbox({+ displayName: 'Protect Against Spreadsheet Formulas',+ description: 'Stops text starting with = + - @ from running as a formula.',+ required: false,+ defaultValue: true,+ }),+ },+ outputSchema: createCsvFileOutputSchema,+ async run(context) {+ const { json_array, file_name, delimiter, include_header, columns, add_excel_bom, escape_formulas } =+ context.propsValue;+ const input = readRows(json_array);+ csvUtils.assertRowLimit(input.length);++ const rows = input.map((item, index) => {+ if (item === null || typeof item !== 'object' || Array.isArray(item)) {+ throw new Error(+ `Row ${index + 1} is not an object. Each row must look like {"column": "value"}.`,+ );+ }+ const flat = flatten(item);+ if (!csvUtils.isRecord(flat)) {+ throw new Error(`Row ${index + 1} could not be flattened into columns.`);+ }+ return flat;+ });++ const requested = [...new Set(csvUtils.toStringList(columns))];+ const headers = requested.length > 0 ? requested : unionOfKeys(rows);+ const text = csvUtils.serializeCsv({+ headers,+ rows,+ delimiter: delimiter || ',',+ includeHeader: include_header !== false,+ escapeFormulas: escape_formulas !== false,+ });+ const content = add_excel_bom === true ? `${text}` : text;+ const name = csvUtils.safeFileName({ requested: file_name, fallback: 'export.csv', extension: 'csv' });++ const url = await context.files.write({+ fileName: name,+ data: Buffer.from(content, 'utf8'),+ });+ return {+ file_name: name,+ url,+ row_count: rows.length,+ columns: headers,+ };+ },+});++function readRows(value: unknown): unknown[] {+ let data = value;+ if (typeof data === 'string') {+ try {+ data = JSON.parse(data);+ } catch {+ throw new Error('Rows must be a JSON array, for example [{"name":"Ann"}].');+ }+ }+ if (!Array.isArray(data)) {+ throw new Error('Rows must be a JSON array of objects, not a single object.');+ }+ return data;+}++function unionOfKeys(rows: Record<string, unknown>[]): string[] {+ const keys = new Set<string>();+ for (const row of rows) {+ for (const key of Object.keys(row)) {+ keys.add(key);+ }+ }+ return [...keys];+}diff --git a/packages/pieces/core/csv/src/lib/actions/filter-csv-rows.ts b/packages/pieces/core/csv/src/lib/actions/filter-csv-rows.tsnew file mode 100644index 00000000000..eec069a4664--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/filter-csv-rows.ts@@ -0,0 +1,148 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { csvResultOutputSchema } from '../output-schemas';++export const filterCsvRowsAction = createAction({+ audience: 'both',+ name: 'filter_csv_rows',+ classification: 'READ',+ displayName: 'Filter CSV Rows',+ description: 'Keeps only the rows where a column matches a condition.',+ aiMetadata: {+ description:+ 'Filters CSV text to the rows whose value in one named column matches a condition (equals, contains, starts/ends with, is empty, greater/less than — compared as numbers when both sides are numbers, otherwise as text, case-insensitive by default). Pick this instead of a Loop plus Branch to narrow rows before sending or saving them. The CSV must have a header row and the column must exist; read-only and idempotent.',+ idempotent: true,+ },+ props: {+ csv_text: csvProps.csvText(),+ delimiter: csvProps.inputDelimiter(),+ column: Property.ShortText({+ displayName: 'Column',+ description: 'The column name to check, exactly as in the header row.',+ placeholder: 'status',+ required: true,+ }),+ condition: Property.StaticDropdown<Condition, true>({+ displayName: 'Condition',+ required: true,+ defaultValue: 'equals',+ options: {+ options: [+ { label: 'Equals', value: 'equals' },+ { label: 'Does not equal', value: 'not_equals' },+ { label: 'Contains', value: 'contains' },+ { label: 'Does not contain', value: 'not_contains' },+ { label: 'Starts with', value: 'starts_with' },+ { label: 'Ends with', value: 'ends_with' },+ { label: 'Is empty', value: 'is_empty' },+ { label: 'Is not empty', value: 'is_not_empty' },+ { label: 'Greater than', value: 'greater_than' },+ { label: 'Greater than or equal to', value: 'greater_or_equal' },+ { label: 'Less than', value: 'less_than' },+ { label: 'Less than or equal to', value: 'less_or_equal' },+ ],+ },+ }),+ value: Property.ShortText({+ displayName: 'Value',+ description: 'The value to compare with. Not used for "Is empty" and "Is not empty".',+ required: false,+ }),+ case_sensitive: Property.Checkbox({+ displayName: 'Case Sensitive',+ description: 'On: "Paid" and "paid" are different.',+ required: false,+ defaultValue: false,+ }),+ },+ outputSchema: csvResultOutputSchema,+ async run(context) {+ const { csv_text, delimiter, column, condition, value, case_sensitive } = context.propsValue;+ const parsed = csvUtils.parseCsv({ text: csvUtils.assertCsvText({ value: csv_text }), delimiter });+ const target = csvUtils.findColumn({ headers: parsed.headers, requested: column });++ const compareValue = value === undefined || value === null ? '' : String(value).trim();+ if (compareValue === '' && !VALUE_OPTIONAL_CONDITIONS.includes(condition)) {+ throw new Error(`Enter a value to compare with for the "${condition.replace(/_/g, ' ')}" condition.`);+ }+ const matches = buildMatcher({ condition, rawValue: compareValue, caseSensitive: case_sensitive === true });+ return csvUtils.toCsvResult({+ ...parsed,+ rows: parsed.rows.filter((row) => matches(row[target] ?? '')),+ });+ },+});++function buildMatcher({+ condition,+ rawValue,+ caseSensitive,+}: {+ condition: Condition;+ rawValue: string;+ caseSensitive: boolean;+}): (cell: string) => boolean {+ const norm = (s: string) => (caseSensitive ? s.trim() : s.trim().toLowerCase());+ const value = norm(rawValue);+ const valueNumber = csvUtils.parseNumber(rawValue);+ const compare = (cell: string): number | null => {+ if (cell.trim() === '') {+ return null;+ }+ const cellNumber = csvUtils.parseNumber(cell);+ if (cellNumber !== null && valueNumber !== null) {+ return cellNumber - valueNumber;+ }+ const a = norm(cell);+ return a < value ? -1 : a > value ? 1 : 0;+ };+ const ordered = (test: (diff: number) => boolean) => (cell: string) => {+ const diff = compare(cell);+ return diff !== null && test(diff);+ };+ switch (condition) {+ case 'equals':+ return (cell) => norm(cell) === value;+ case 'not_equals':+ return (cell) => norm(cell) !== value;+ case 'contains':+ return (cell) => norm(cell).includes(value);+ case 'not_contains':+ return (cell) => !norm(cell).includes(value);+ case 'starts_with':+ return (cell) => norm(cell).startsWith(value);+ case 'ends_with':+ return (cell) => norm(cell).endsWith(value);+ case 'is_empty':+ return (cell) => cell.trim() === '';+ case 'is_not_empty':+ return (cell) => cell.trim() !== '';+ case 'greater_than':+ return ordered((d) => d > 0);+ case 'greater_or_equal':+ return ordered((d) => d >= 0);+ case 'less_than':+ return ordered((d) => d < 0);+ case 'less_or_equal':+ return ordered((d) => d <= 0);+ default:+ throw new Error(`Unknown condition "${String(condition)}".`);+ }+}++const VALUE_OPTIONAL_CONDITIONS: Condition[] = ['is_empty', 'is_not_empty', 'equals', 'not_equals'];++type Condition =+ | 'equals'+ | 'not_equals'+ | 'contains'+ | 'not_contains'+ | 'starts_with'+ | 'ends_with'+ | 'is_empty'+ | 'is_not_empty'+ | 'greater_than'+ | 'greater_or_equal'+ | 'less_than'+ | 'less_or_equal';diff --git a/packages/pieces/core/csv/src/lib/actions/merge-csv.ts b/packages/pieces/core/csv/src/lib/actions/merge-csv.tsnew file mode 100644index 00000000000..92fa2730a99--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/merge-csv.ts@@ -0,0 +1,46 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { csvResultOutputSchema } from '../output-schemas';++export const mergeCsvAction = createAction({+ audience: 'both',+ name: 'merge_csv',+ classification: 'READ',+ displayName: 'Merge CSV Files',+ description: 'Stacks several CSVs into one, matching columns by name.',+ aiMetadata: {+ description:+ 'Appends the rows of two or more CSV texts into one, matching columns by header name: the result has every column seen in any input (first-seen order) and cells a source did not have stay blank. Pick this to combine exports with the same or overlapping columns; it stacks rows and does not join or look up rows by a key. Each input needs a header row; up to 100,000 rows in total; read-only and idempotent.',+ idempotent: true,+ },+ props: {+ csv_texts: Property.Array({+ displayName: 'CSV Texts',+ description: 'Two or more CSV texts, one per item. Map each from an earlier step.',+ required: true,+ }),+ delimiter: csvProps.inputDelimiter(),+ },+ outputSchema: csvResultOutputSchema,+ async run(context) {+ const { csv_texts, delimiter } = context.propsValue;+ const inputs = Array.isArray(csv_texts) ? csv_texts : [];+ if (inputs.length < 2) {+ throw new Error('Add at least two CSV texts to merge.');+ }+ const parsedInputs = inputs.map((value, i) =>+ csvUtils.parseCsv({+ text: csvUtils.assertCsvText({ value, label: `CSV Text ${i + 1}` }),+ delimiter,+ }),+ );+ const headers = [...new Set(parsedInputs.flatMap((p) => p.headers))];+ const total = parsedInputs.reduce((sum, p) => sum + p.rows.length, 0);+ csvUtils.assertRowLimit(total);+ const rows = parsedInputs.flatMap((p) =>+ p.rows.map((row) => Object.fromEntries(headers.map((h) => [h, row[h] ?? '']))),+ );+ return csvUtils.toCsvResult({ headers, rows, delimiter: parsedInputs[0].delimiter });+ },+});diff --git a/packages/pieces/core/csv/src/lib/actions/read-csv-file.ts b/packages/pieces/core/csv/src/lib/actions/read-csv-file.tsnew file mode 100644index 00000000000..5085f2f071f--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/read-csv-file.ts@@ -0,0 +1,61 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { readCsvFileOutputSchema } from '../output-schemas';++export const readCsvFileAction = createAction({+ audience: 'both',+ name: 'read_csv_file',+ classification: 'READ',+ displayName: 'Read CSV File',+ description: 'Reads a CSV file (for example an email attachment) into rows you can loop over.',+ aiMetadata: {+ description:+ 'Parses an uploaded or mapped .csv file into rows keyed by column name, auto-detecting comma, semicolon, tab or pipe delimiters and handling byte-order marks, blank lines, uneven rows and duplicate column names. Pick this when the input is a file; use Convert CSV to JSON or the other CSV actions when you already have CSV text, and Convert Excel to CSV for .xlsx/.xls files. Files up to 20 MB and 100,000 rows; read-only and idempotent.',+ idempotent: true,+ },+ props: {+ file: Property.File({+ displayName: 'CSV File',+ description: 'The .csv file to read. Map a file from an earlier step, or upload one.',+ required: true,+ }),+ has_header_row: Property.Checkbox({+ displayName: 'Has Header Row',+ description:+ 'The first row holds column names. Off: column_1, column_2, …',+ required: false,+ defaultValue: true,+ }),+ delimiter: csvProps.inputDelimiter(),+ trim_whitespace: Property.Checkbox({+ displayName: 'Trim Spaces',+ description: 'Remove spaces around each value.',+ required: false,+ defaultValue: false,+ }),+ },+ outputSchema: readCsvFileOutputSchema,+ async run(context) {+ const { file, has_header_row, delimiter, trim_whitespace } = context.propsValue;+ const buffer = csvUtils.fileToBuffer(file);+ csvUtils.assertByteLimit({ bytes: buffer.length, label: 'The file' });+ if (csvUtils.isExcelSignature(buffer)) {+ throw new Error(+ 'This looks like an Excel file, not a CSV. Use the "Convert Excel to CSV" action first.',+ );+ }+ const parsed = csvUtils.parseCsv({+ text: csvUtils.decodeText(buffer),+ delimiter,+ hasHeader: has_header_row !== false,+ trim: trim_whitespace === true,+ });+ return {+ rows: parsed.rows,+ row_count: parsed.rows.length,+ headers: parsed.headers,+ delimiter: parsed.delimiter,+ };+ },+});diff --git a/packages/pieces/core/csv/src/lib/actions/remove-duplicate-csv-rows.ts b/packages/pieces/core/csv/src/lib/actions/remove-duplicate-csv-rows.tsnew file mode 100644index 00000000000..a862780268e--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/remove-duplicate-csv-rows.ts@@ -0,0 +1,87 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { csvProps } from '../common/csv-props';+import { CsvRow, csvUtils } from '../common/csv-utils';+import { removeDuplicateCsvRowsOutputSchema } from '../output-schemas';++export const removeDuplicateCsvRowsAction = createAction({+ audience: 'both',+ name: 'remove_duplicate_csv_rows',+ classification: 'READ',+ displayName: 'Remove Duplicate Rows',+ description: 'Removes repeated rows from CSV text, comparing whole rows or chosen columns.',+ aiMetadata: {+ description:+ 'Removes duplicate rows from CSV text, treating rows as duplicates when every value matches or, if key columns are given, when just those columns match (for example the same email), keeping the first or the last occurrence and reporting how many were removed. Pick this to clean a list before importing or emailing it; comparison ignores surrounding spaces and is case-sensitive unless turned off. The CSV must have a header row and key columns must exist; read-only and idempotent.',+ idempotent: true,+ },+ props: {+ csv_text: csvProps.csvText(),+ delimiter: csvProps.inputDelimiter(),+ key_columns: Property.Array({+ displayName: 'Compare Columns',+ description:+ 'Columns that must match, e.g. "email". Empty compares whole rows.',+ required: false,+ }),+ keep: Property.StaticDropdown<KeepMode, false>({+ displayName: 'Keep',+ required: false,+ defaultValue: 'first',+ options: {+ options: [+ { label: 'First occurrence', value: 'first' },+ { label: 'Last occurrence', value: 'last' },+ ],+ },+ }),+ case_sensitive: Property.Checkbox({+ displayName: 'Case Sensitive',+ description: 'On: "[email protected]" and "[email protected]" are different.',+ required: false,+ defaultValue: true,+ }),+ trim_whitespace: Property.Checkbox({+ displayName: 'Ignore Surrounding Spaces',+ description: 'On: " Ann" and "Ann" count as the same.',+ required: false,+ defaultValue: true,+ }),+ },+ outputSchema: removeDuplicateCsvRowsOutputSchema,+ async run(context) {+ const { csv_text, delimiter, key_columns, keep, case_sensitive, trim_whitespace } =+ context.propsValue;+ const parsed = csvUtils.parseCsv({ text: csvUtils.assertCsvText({ value: csv_text }), delimiter });+ const requested = csvUtils.toStringList(key_columns);+ const keys = requested.length > 0+ ? [...new Set(requested.map((c) => csvUtils.findColumn({ headers: parsed.headers, requested: c })))]+ : parsed.headers;+ const normalize = (value: string) => {+ const trimmed = trim_whitespace === false ? value : value.trim();+ return case_sensitive === false ? trimmed.toLowerCase() : trimmed;+ };+ const signature = (row: CsvRow) => JSON.stringify(keys.map((k) => normalize(row[k] ?? '')));++ const rows = keep === 'last'+ ? keepFirst({ rows: [...parsed.rows].reverse(), signature }).reverse()+ : keepFirst({ rows: parsed.rows, signature });+ return {+ ...csvUtils.toCsvResult({ ...parsed, rows }),+ removed_count: parsed.rows.length - rows.length,+ };+ },+});++function keepFirst({ rows, signature }: { rows: CsvRow[]; signature: (row: CsvRow) => string }): CsvRow[] {+ const seen = new Set<string>();+ return rows.filter((row) => {+ const key = signature(row);+ if (seen.has(key)) {+ return false;+ }+ seen.add(key);+ return true;+ });+}++type KeepMode = 'first' | 'last';diff --git a/packages/pieces/core/csv/src/lib/actions/select-csv-columns.ts b/packages/pieces/core/csv/src/lib/actions/select-csv-columns.tsnew file mode 100644index 00000000000..ce438ec46ce--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/select-csv-columns.ts@@ -0,0 +1,55 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { csvResultOutputSchema } from '../output-schemas';++export const selectCsvColumnsAction = createAction({+ audience: 'both',+ name: 'select_csv_columns',+ classification: 'READ',+ displayName: 'Select Columns',+ description: 'Keeps, reorders or removes columns in CSV text.',+ aiMetadata: {+ description:+ 'Returns CSV text with only the listed columns in the listed order, or with the listed columns removed when the mode is Remove. Pick this to trim or reorder columns before exporting or inserting rows elsewhere. The CSV must have a header row and every listed column must exist, otherwise it fails with the real column names; read-only and idempotent.',+ idempotent: true,+ },+ props: {+ csv_text: csvProps.csvText(),+ delimiter: csvProps.inputDelimiter(),+ columns: Property.Array({+ displayName: 'Columns',+ description: 'Column names as in the header. Keep mode uses this order.',+ required: true,+ }),+ mode: Property.StaticDropdown<SelectMode, false>({+ displayName: 'Mode',+ required: false,+ defaultValue: 'keep',+ options: {+ options: [+ { label: 'Keep only these columns', value: 'keep' },+ { label: 'Remove these columns', value: 'remove' },+ ],+ },+ }),+ },+ outputSchema: csvResultOutputSchema,+ async run(context) {+ const { csv_text, delimiter, columns, mode } = context.propsValue;+ const parsed = csvUtils.parseCsv({ text: csvUtils.assertCsvText({ value: csv_text }), delimiter });+ const requested = csvUtils.toStringList(columns);+ if (requested.length === 0) {+ throw new Error('List at least one column.');+ }+ const matched = [+ ...new Set(requested.map((c) => csvUtils.findColumn({ headers: parsed.headers, requested: c }))),+ ];+ const removed = new Set(matched);+ const headers = mode === 'remove' ? parsed.headers.filter((h) => !removed.has(h)) : matched;+ const rows = parsed.rows.map((row) => Object.fromEntries(headers.map((h) => [h, row[h] ?? ''])));+ return csvUtils.toCsvResult({ headers, rows, delimiter: parsed.delimiter });+ },+});++type SelectMode = 'keep' | 'remove';diff --git a/packages/pieces/core/csv/src/lib/actions/sort-csv-rows.ts b/packages/pieces/core/csv/src/lib/actions/sort-csv-rows.tsnew file mode 100644index 00000000000..cf0ff71d6f1--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/sort-csv-rows.ts@@ -0,0 +1,93 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { csvResultOutputSchema } from '../output-schemas';++export const sortCsvRowsAction = createAction({+ audience: 'both',+ name: 'sort_csv_rows',+ classification: 'READ',+ displayName: 'Sort CSV Rows',+ description: 'Sorts the rows by one column, as text, numbers or dates.',+ aiMetadata: {+ description:+ 'Sorts the rows of CSV text by one named column, ascending or descending, comparing as text (natural order, so item2 comes before item10), numbers or dates; blank or unreadable values always go last and rows with equal values keep their order. Pick this before taking the top rows or writing an ordered report; for dates use ISO (2026-09-29) or another unambiguous format. The CSV must have a header row; read-only and idempotent.',+ idempotent: true,+ },+ props: {+ csv_text: csvProps.csvText(),+ delimiter: csvProps.inputDelimiter(),+ column: Property.ShortText({+ displayName: 'Column',+ description: 'The column name to sort by, exactly as in the header row.',+ placeholder: 'created_at',+ required: true,+ }),+ direction: Property.StaticDropdown<SortDirection, false>({+ displayName: 'Direction',+ required: false,+ defaultValue: 'asc',+ options: {+ options: [+ { label: 'Ascending (A → Z, 1 → 9, oldest first)', value: 'asc' },+ { label: 'Descending (Z → A, 9 → 1, newest first)', value: 'desc' },+ ],+ },+ }),+ compare_as: Property.StaticDropdown<CompareAs, false>({+ displayName: 'Compare As',+ description: 'How to compare values. Unreadable values go last.',+ required: false,+ defaultValue: 'text',+ options: {+ options: [+ { label: 'Text', value: 'text' },+ { label: 'Number', value: 'number' },+ { label: 'Date', value: 'date' },+ ],+ },+ }),+ },+ outputSchema: csvResultOutputSchema,+ async run(context) {+ const { csv_text, delimiter, column, direction, compare_as } = context.propsValue;+ const parsed = csvUtils.parseCsv({ text: csvUtils.assertCsvText({ value: csv_text }), delimiter });+ const target = csvUtils.findColumn({ headers: parsed.headers, requested: column });+ const sign = direction === 'desc' ? -1 : 1;+ const toKey = keyReader(compare_as ?? 'text');++ const keyed = parsed.rows.map((row, index) => ({ row, index, key: toKey(row[target] ?? '') }));+ const sorted = [...keyed].sort((a, b) => {+ if (a.key === null || b.key === null) {+ return a.key === b.key ? a.index - b.index : a.key === null ? 1 : -1;+ }+ const diff = typeof a.key === 'number' && typeof b.key === 'number'+ ? a.key - b.key+ : COLLATOR.compare(String(a.key), String(b.key));+ return diff === 0 ? a.index - b.index : sign * diff;+ });+ return csvUtils.toCsvResult({ ...parsed, rows: sorted.map((k) => k.row) });+ },+});++function keyReader(compareAs: CompareAs): (value: string) => string | number | null {+ switch (compareAs) {+ case 'number':+ return (value) => csvUtils.parseNumber(value);+ case 'date':+ return (value) => {+ if (value.trim() === '') {+ return null;+ }+ const time = Date.parse(value.trim());+ return Number.isNaN(time) ? null : time;+ };+ default:+ return (value) => (value.trim() === '' ? null : value.trim());+ }+}++const COLLATOR = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });++type SortDirection = 'asc' | 'desc';+type CompareAs = 'text' | 'number' | 'date';diff --git a/packages/pieces/core/csv/src/lib/actions/split-csv.ts b/packages/pieces/core/csv/src/lib/actions/split-csv.tsnew file mode 100644index 00000000000..d4f6aa96444--- /dev/null+++ b/packages/pieces/core/csv/src/lib/actions/split-csv.ts@@ -0,0 +1,64 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { csvProps } from '../common/csv-props';+import { csvUtils } from '../common/csv-utils';+import { splitCsvOutputSchema } from '../output-schemas';++export const splitCsvAction = createAction({+ audience: 'both',+ name: 'split_csv',+ classification: 'READ',+ displayName: 'Split CSV',+ description: 'Splits CSV text into smaller CSVs with a set number of rows each.',+ aiMetadata: {+ description:+ 'Splits CSV text into chunks of a fixed number of rows (1 to 10,000), each chunk a complete CSV that repeats the header row by default, so a Loop can send or upload them one batch at a time. Pick this for data that fits in memory (up to 20 MB); for larger files use Stream CSV to Subflows, which streams the file and runs a subflow per batch. The CSV must have a header row; read-only and idempotent.',+ idempotent: true,+ },+ props: {+ csv_text: csvProps.csvText(),+ delimiter: csvProps.inputDelimiter(),+ rows_per_chunk: Property.Number({+ displayName: 'Rows per Chunk',+ description: 'Data rows per chunk (1 to 10,000), not counting the header.',+ required: true,+ defaultValue: 100,+ }),+ repeat_header: Property.Checkbox({+ displayName: 'Repeat Header Row',+ description: 'Start every chunk with the column names.',+ required: false,+ defaultValue: true,+ }),+ },+ outputSchema: splitCsvOutputSchema,+ async run(context) {+ const { csv_text, delimiter, rows_per_chunk, repeat_header } = context.propsValue;+ const size = Number(rows_per_chunk);+ if (!Number.isInteger(size) || size < 1 || size > MAX_ROWS_PER_CHUNK) {+ throw new Error(`Rows per Chunk must be a whole number from 1 to ${MAX_ROWS_PER_CHUNK}.`);+ }+ const parsed = csvUtils.parseCsv({ text: csvUtils.assertCsvText({ value: csv_text }), delimiter });+ const chunkCount = Math.ceil(parsed.rows.length / size);+ const chunks = Array.from({ length: chunkCount }, (_, i) => {+ const rows = parsed.rows.slice(i * size, (i + 1) * size);+ return {+ index: i + 1,+ csv: csvUtils.serializeCsv({+ headers: parsed.headers,+ rows,+ delimiter: parsed.delimiter,+ includeHeader: repeat_header !== false || i === 0,+ }),+ row_count: rows.length,+ };+ });+ return {+ chunks,+ chunk_count: chunks.length,+ total_rows: parsed.rows.length,+ headers: parsed.headers,+ };+ },+});++const MAX_ROWS_PER_CHUNK = 10_000;diff --git a/packages/pieces/core/csv/src/lib/common/csv-props.ts b/packages/pieces/core/csv/src/lib/common/csv-props.tsnew file mode 100644index 00000000000..8a8ea3faccd--- /dev/null+++ b/packages/pieces/core/csv/src/lib/common/csv-props.ts@@ -0,0 +1,49 @@+import { Property } from '@activepieces/pieces-framework';+import { AUTO_DELIMITER } from './csv-utils';++function csvText() {+ return Property.LongText({+ displayName: 'CSV Text',+ description:+ 'Paste the CSV, or map CSV text from an earlier step. The first row must be the column names.',+ placeholder: 'name,email,city',+ required: true,+ });+}++function inputDelimiter() {+ return Property.StaticDropdown({+ displayName: 'Delimiter',+ description:+ 'The character that separates columns. Auto-detect picks comma, semicolon, tab or pipe from the first rows.',+ required: false,+ defaultValue: AUTO_DELIMITER,+ options: {+ options: [{ label: 'Auto-detect', value: AUTO_DELIMITER }, ...DELIMITER_OPTIONS],+ },+ });+}++function outputDelimiter() {+ return Property.StaticDropdown({+ displayName: 'Delimiter',+ description:+ 'The character that separates columns in the file. Many European Excel versions expect semicolons.',+ required: false,+ defaultValue: ',',+ options: { options: DELIMITER_OPTIONS },+ });+}++const DELIMITER_OPTIONS = [+ { label: 'Comma (,)', value: ',' },+ { label: 'Semicolon (;)', value: ';' },+ { label: 'Tab', value: '\t' },+ { label: 'Pipe (|)', value: '|' },+];++export const csvProps = {+ csvText,+ inputDelimiter,+ outputDelimiter,+};diff --git a/packages/pieces/core/csv/src/lib/common/csv-utils.ts b/packages/pieces/core/csv/src/lib/common/csv-utils.tsnew file mode 100644index 00000000000..0f3b049a3d4--- /dev/null+++ b/packages/pieces/core/csv/src/lib/common/csv-utils.ts@@ -0,0 +1,384 @@+import { parse } from 'csv-parse/sync';+import { stringify } from 'csv-stringify/sync';++function assertCsvText({ value, label = 'CSV Text' }: { value: unknown; label?: string }): string {+ if (typeof value !== 'string') {+ throw new Error(`${label} must be text. Map the CSV text from an earlier step, or paste it.`);+ }+ assertByteLimit({ bytes: Buffer.byteLength(value, 'utf8'), label });+ return value;+}++function assertByteLimit({ bytes, label }: { bytes: number; label: string }): void {+ if (bytes > CSV_LIMITS.maxInputBytes) {+ throw new Error(+ `${label} is larger than ${CSV_LIMITS.maxInputBytes / 1024 / 1024} MB. ${TOO_BIG_HINT}`,+ );+ }+}++function assertRowLimit(count: number): void {+ if (count > CSV_LIMITS.maxRows) {+ throw new Error(+ `The data has ${count} rows; this action handles up to ${CSV_LIMITS.maxRows}. ${TOO_BIG_HINT}`,+ );+ }+}++function detectDelimiter(text: string): string {+ const records = sampleDelimiterCounts(stripBom(text).slice(0, DETECTION_SAMPLE_CHARS));+ if (records.length === 0) {+ return ',';+ }+ const ranked = DELIMITER_CANDIDATES.map((candidate, order) => {+ const first = records[0][candidate];+ const consistent = records.filter((r) => r[candidate] === first).length;+ return { candidate, first, consistent, order };+ })+ .filter((r) => r.first > 0)+ .sort((a, b) => b.consistent - a.consistent || b.first - a.first || a.order - b.order);+ return ranked[0]?.candidate ?? ',';+}++function resolveDelimiter({ text, delimiter }: { text: string; delimiter: string | undefined | null }): string {+ if (!delimiter || delimiter === AUTO_DELIMITER) {+ return detectDelimiter(text);+ }+ return delimiter;+}++function parseCsvRecords({+ text,+ delimiter,+ hasHeader = true,+ trim = false,+}: {+ text: string;+ delimiter: string;+ hasHeader?: boolean;+ trim?: boolean;+}): string[][] {+ const clean = stripBom(text);+ if (clean.trim() === '') {+ return [];+ }+ const records = parseOrExplain({ text: clean, delimiter, trim });+ const nonEmpty = records.filter((r) => r.some((cell) => cell.trim() !== ''));+ assertRowLimit(Math.max(nonEmpty.length - (hasHeader ? 1 : 0), 0));+ return nonEmpty;+}++function normalizeHeaders({ raw, width }: { raw: string[]; width: number }): string[] {+ const taken = new Set<string>();+ const nextSuffix = new Map<string, number>();+ return Array.from({ length: Math.max(raw.length, width) }, (_, i) => {+ const base = (raw[i] ?? '').trim() || `column_${i + 1}`;+ let name = base;+ if (taken.has(base)) {+ let n = nextSuffix.get(base) ?? 2;+ while (taken.has(`${base}_${n}`)) {+ n++;+ }+ nextSuffix.set(base, n + 1);+ name = `${base}_${n}`;+ }+ taken.add(name);+ return name;+ });+}++function maxWidth(records: string[][]): number {+ return records.reduce((max, record) => (record.length > max ? record.length : max), 0);+}++function parseCsv({+ text,+ delimiter,+ hasHeader = true,+ trim = false,+}: {+ text: string;+ delimiter?: string | null;+ hasHeader?: boolean;+ trim?: boolean;+}): ParsedCsv {+ const resolved = resolveDelimiter({ text, delimiter });+ const records = parseCsvRecords({ text, delimiter: resolved, hasHeader, trim });+ if (records.length === 0) {+ return { headers: [], rows: [], delimiter: resolved };+ }+ const width = maxWidth(records);+ const headers = normalizeHeaders({ raw: hasHeader ? records[0] : [], width });+ const dataRecords = hasHeader ? records.slice(1) : records;+ const rows = dataRecords.map((record) =>+ Object.fromEntries(headers.map((h, i) => [h, record[i] ?? ''])),+ );+ return { headers, rows, delimiter: resolved };+}++function serializeCsv({+ headers,+ rows,+ delimiter = ',',+ includeHeader = true,+ escapeFormulas = false,+}: {+ headers: string[];+ rows: Record<string, unknown>[];+ delimiter?: string;+ includeHeader?: boolean;+ escapeFormulas?: boolean;+}): string {+ if (headers.length === 0) {+ return '';+ }+ const records = rows.map((row) => headers.map((h) => cellToString(row[h])));+ const table = includeHeader ? [headers, ...records] : records;+ const safe = escapeFormulas ? table.map((record) => record.map(escapeFormula)) : table;+ return stringify(safe, { delimiter });+}++function escapeFormula(value: string): string {+ if (!FORMULA_PREFIX.test(value) || PLAIN_NUMBER.test(value)) {+ return value;+ }+ return `'${value}`;+}++function isExactNumber(value: string): boolean {+ const match = SAFE_NUMBER_PARTS.exec(value);+ if (!match) {+ return false;+ }+ const digits = `${match[1]}${match[2] ?? ''}`.replace(/^0+/, '').replace(/0+$/, '');+ return digits.length <= MAX_EXACT_DIGITS;+}++function cellToString(value: unknown): string {+ if (value === null || value === undefined) {+ return '';+ }+ if (typeof value === 'string') {+ return value;+ }+ if (typeof value === 'boolean') {+ return value ? 'true' : 'false';+ }+ if (typeof value === 'number' || typeof value === 'bigint') {+ return String(value);+ }+ if (value instanceof Date) {+ return value.toISOString();+ }+ if (typeof value === 'object' && Object.keys(value).length === 0) {+ return '';+ }+ return JSON.stringify(value);+}++function toCsvResult(parsed: ParsedCsv): CsvResult {+ return {+ csv: serializeCsv({ headers: parsed.headers, rows: parsed.rows, delimiter: parsed.delimiter }),+ rows: parsed.rows,+ row_count: parsed.rows.length,+ headers: parsed.headers,+ };+}++function findColumn({ headers, requested }: { headers: string[]; requested: unknown }): string {+ const name = cellToString(requested).trim();+ if (name === '') {+ throw new Error('Enter a column name.');+ }+ if (headers.length === 0) {+ throw new Error(`The CSV is empty, so it has no column named "${name}".`);+ }+ if (headers.includes(name)) {+ return name;+ }+ const loose = headers.filter((h) => h.toLowerCase() === name.toLowerCase());+ if (loose.length === 1) {+ return loose[0];+ }+ throw new Error(`Column "${name}" not found. Available columns: ${headers.join(', ')}`);+}++function toStringList(value: unknown): string[] {+ if (value === undefined || value === null || value === '') {+ return [];+ }+ const list = Array.isArray(value) ? value : [value];+ return list.map((v) => cellToString(v).trim()).filter((v) => v !== '');+}++function parseNumber(value: string): number | null {+ const trimmed = value.trim();+ if (trimmed === '') {+ return null;+ }+ const n = Number(trimmed);+ return Number.isFinite(n) ? n : null;+}++function fileToBuffer(file: unknown): Buffer {+ if (isRecord(file)) {+ const { data, base64 } = file;+ if (Buffer.isBuffer(data)) {+ return data;+ }+ if (data instanceof Uint8Array) {+ return Buffer.from(data);+ }+ if (typeof base64 === 'string') {+ return Buffer.from(base64, 'base64');+ }+ }+ throw new Error('No file was provided. Upload a CSV file or map a file from an earlier step.');+}++function decodeText(buffer: Buffer): string {+ if (buffer.length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {+ return buffer.subarray(2).toString('utf16le');+ }+ if (buffer.length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {+ const evenLength = buffer.length - 2 - ((buffer.length - 2) % 2);+ const body = Buffer.from(buffer.subarray(2, 2 + evenLength));+ body.swap16();+ return body.toString('utf16le');+ }+ return stripBom(buffer.toString('utf8'));+}++function isExcelSignature(buffer: Buffer): boolean {+ const isXlsx = buffer[0] === 0x50 && buffer[1] === 0x4b;+ const isXls = buffer[0] === 0xd0 && buffer[1] === 0xcf;+ return isXlsx || isXls;+}++function safeFileName({+ requested,+ fallback,+ extension,+}: {+ requested: unknown;+ fallback: string;+ extension: string;+}): string {+ const raw = typeof requested === 'string' ? requested.trim() : '';+ const name = (raw || fallback).replace(/[\\/]/g, '_');+ return /\.[A-Za-z0-9]+$/.test(name) ? name : `${name}.${extension}`;+}++function stripBom(text: string): string {+ return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;+}++function parseOrExplain({ text, delimiter, trim }: { text: string; delimiter: string; trim: boolean }): string[][] {+ try {+ return parse(text, {+ delimiter,+ bom: true,+ skip_empty_lines: true,+ relax_column_count: true,+ relax_quotes: true,+ trim,+ });+ } catch (e) {+ const message = e instanceof Error ? e.message : String(e);+ throw new Error(+ `Could not read the CSV: ${message}. Check that the delimiter is right and that quoted values are closed.`,+ );+ }+}++function sampleDelimiterCounts(sample: string): Record<string, number>[] {+ const records: Record<string, number>[] = [];+ let counts = emptyCounts();+ let inQuotes = false;+ let hasContent = false;+ for (let i = 0; i < sample.length && records.length < DETECTION_RECORDS; i++) {+ const ch = sample[i];+ if (ch === '"') {+ inQuotes = !inQuotes;+ hasContent = true;+ } else if (!inQuotes && (ch === '\n' || ch === '\r')) {+ if (hasContent) {+ records.push(counts);+ }+ counts = emptyCounts();+ hasContent = false;+ } else {+ if (!inQuotes && ch in counts) {+ counts[ch]++;+ }+ hasContent = hasContent || ch.trim() !== '';+ }+ }+ if (hasContent && records.length < DETECTION_RECORDS) {+ records.push(counts);+ }+ return records;+}++function emptyCounts(): Record<string, number> {+ return Object.fromEntries(DELIMITER_CANDIDATES.map((d) => [d, 0]));+}++function isRecord(value: unknown): value is Record<string, unknown> {+ return value !== null && typeof value === 'object';+}++const TOO_BIG_HINT =+ 'For bigger files, use the Subflows piece\'s "Stream CSV to Subflows" action, which processes the file in batches.';+const DELIMITER_CANDIDATES = [',', ';', '\t', '|'];+const DETECTION_SAMPLE_CHARS = 64 * 1024;+const DETECTION_RECORDS = 20;+const FORMULA_PREFIX = /^[=+\-@\t\r]/;+const PLAIN_NUMBER = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;+const SAFE_NUMBER_PARTS = /^-?(0|[1-9]\d{0,14})(?:\.(\d{1,15}))?$/;+const MAX_EXACT_DIGITS = 15;++export const AUTO_DELIMITER = 'auto';++export const CSV_LIMITS = {+ maxInputBytes: 20 * 1024 * 1024,+ maxRows: 100_000,+};++export const csvUtils = {+ assertCsvText,+ assertByteLimit,+ assertRowLimit,+ detectDelimiter,+ resolveDelimiter,+ parseCsvRecords,+ normalizeHeaders,+ parseCsv,+ serializeCsv,+ cellToString,+ toCsvResult,+ findColumn,+ toStringList,+ parseNumber,+ isExactNumber,+ fileToBuffer,+ decodeText,+ isExcelSignature,+ safeFileName,+ isRecord,+};++export type CsvRow = Record<string, string>;++export type ParsedCsv = {+ headers: string[];+ rows: CsvRow[];+ delimiter: string;+};++export type CsvResult = {+ csv: string;+ rows: CsvRow[];+ row_count: number;+ headers: string[];+};diff --git a/packages/pieces/core/csv/src/lib/output-schemas.ts b/packages/pieces/core/csv/src/lib/output-schemas.tsindex dedd5dd1673..8506cf8948e 100644--- a/packages/pieces/core/csv/src/lib/output-schemas.ts+++ b/packages/pieces/core/csv/src/lib/output-schemas.ts@@ -15,3 +15,66 @@ export const excelToCsvActionOutputSchema: OutputSchema = {{ key: 'available_sheets', label: 'Available Sheets' },],};++const csvResultFields: OutputSchema['fields'] = [+ { key: 'csv', label: 'CSV Text' },+ { key: 'row_count', label: 'Row Count', format: 'number' },+ { key: 'headers', label: 'Column Names' },+ { key: 'rows', label: 'Rows' },+];++export const csvResultOutputSchema: OutputSchema = {+ fields: csvResultFields,+};++export const removeDuplicateCsvRowsOutputSchema: OutputSchema = {+ fields: [+ ...csvResultFields,+ { key: 'removed_count', label: 'Duplicates Removed', format: 'number' },+ ],+};++export const readCsvFileOutputSchema: OutputSchema = {+ fields: [+ { key: 'rows', label: 'Rows' },+ { key: 'row_count', label: 'Row Count', format: 'number' },+ { key: 'headers', label: 'Column Names' },+ { key: 'delimiter', label: 'Delimiter Used' },+ ],+};++export const createCsvFileOutputSchema: OutputSchema = {+ fields: [+ { key: 'file_name', label: 'File Name' },+ { key: 'url', label: 'File URL', format: 'url' },+ { key: 'row_count', label: 'Row Count', format: 'number' },+ { key: 'columns', label: 'Column Names' },+ ],+};++export const convertCsvToExcelOutputSchema: OutputSchema = {+ fields: [+ { key: 'file_name', label: 'File Name' },+ { key: 'url', label: 'File URL', format: 'url' },+ { key: 'row_count', label: 'Rows Written (incl. header)', format: 'number' },+ { key: 'sheet_name', label: 'Sheet Name' },+ ],+};++export const splitCsvOutputSchema: OutputSchema = {+ fields: [+ {+ key: 'chunks',+ label: 'Chunks',+ labelKey: 'index',+ listItems: [+ { key: 'index', label: 'Chunk Number', format: 'number' },+ { key: 'csv', label: 'CSV Text' },+ { key: 'row_count', label: 'Row Count', format: 'number' },+ ],+ },+ { key: 'chunk_count', label: 'Chunk Count', format: 'number' },+ { key: 'total_rows', label: 'Total Rows', format: 'number' },+ { key: 'headers', label: 'Column Names' },+ ],+};diff --git a/packages/pieces/core/csv/test/fixtures/blank-lines.csv b/packages/pieces/core/csv/test/fixtures/blank-lines.csvnew file mode 100644index 00000000000..cd7511c4081--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/blank-lines.csv@@ -0,0 +1,8 @@+a,b+1,2++3,4++,,+5,6+diff --git a/packages/pieces/core/csv/test/fixtures/bom-utf8.csv b/packages/pieces/core/csv/test/fixtures/bom-utf8.csvnew file mode 100644index 00000000000..9bc94b5f994--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/bom-utf8.csv@@ -0,0 +1,3 @@+name,city+José,Zürich+Renée,Kölndiff --git a/packages/pieces/core/csv/test/fixtures/dup-headers.csv b/packages/pieces/core/csv/test/fixtures/dup-headers.csvnew file mode 100644index 00000000000..b36fd505bc3--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/dup-headers.csv@@ -0,0 +1,2 @@+email,name,email,,name[email protected],Ann,[email protected],note,Anniediff --git a/packages/pieces/core/csv/test/fixtures/excel-unicode-text.txt b/packages/pieces/core/csv/test/fixtures/excel-unicode-text.txtnew file mode 100644index 00000000000..fb7f3f9e617Binary files /dev/null and b/packages/pieces/core/csv/test/fixtures/excel-unicode-text.txt differdiff --git a/packages/pieces/core/csv/test/fixtures/extra-columns.csv b/packages/pieces/core/csv/test/fixtures/extra-columns.csvnew file mode 100644index 00000000000..cffb993b844--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/extra-columns.csv@@ -0,0 +1,2 @@+email,name,country[email protected],Zed,DEdiff --git a/packages/pieces/core/csv/test/fixtures/leading-zeros.csv b/packages/pieces/core/csv/test/fixtures/leading-zeros.csvnew file mode 100644index 00000000000..c7ebcb0b9f2--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/leading-zeros.csv@@ -0,0 +1,3 @@+zip,id,amount,flag,small+01234,12345678901234567890,42.5,TRUE,-3+00501,9007199254740993,0.10,false,0diff --git a/packages/pieces/core/csv/test/fixtures/people.csv b/packages/pieces/core/csv/test/fixtures/people.csvnew file mode 100644index 00000000000..82219eb92d1--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/people.csv@@ -0,0 +1,6 @@+name,email,age,status,joined+Ann,[email protected],34,Paid,2026-01-15+bob,[email protected],9,paid,2025-12-01+Cy,[email protected],10,trial,+Dee,[email protected],,Trial,2026-03-02+Eve, [email protected] ,100,PAID,not a datediff --git a/packages/pieces/core/csv/test/fixtures/pipe.csv b/packages/pieces/core/csv/test/fixtures/pipe.csvnew file mode 100644index 00000000000..6576c287dab--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/pipe.csv@@ -0,0 +1,3 @@+sku|price|name+A1|9.99|Widget, large+B2|19.5|Gadgetdiff --git a/packages/pieces/core/csv/test/fixtures/quoted-multiline.csv b/packages/pieces/core/csv/test/fixtures/quoted-multiline.csvnew file mode 100644index 00000000000..0ed96cdbef4--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/quoted-multiline.csv@@ -0,0 +1,5 @@+id,comment+1,"line one+line two"+2,"he said ""hi"", then left"+3,plain "quote" insidediff --git a/packages/pieces/core/csv/test/fixtures/ragged.csv b/packages/pieces/core/csv/test/fixtures/ragged.csvnew file mode 100644index 00000000000..520c94fd362--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/ragged.csv@@ -0,0 +1,4 @@+id,name,email+1,Ann+2,Bob,[email protected],EXTRA+3,Cy,[email protected]diff --git a/packages/pieces/core/csv/test/fixtures/semicolon.csv b/packages/pieces/core/csv/test/fixtures/semicolon.csvnew file mode 100644index 00000000000..2a9d3d0e458--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/semicolon.csv@@ -0,0 +1,3 @@+name;amount;note+Ann;1,50;"a;b"+Bob;2,00;plaindiff --git a/packages/pieces/core/csv/test/fixtures/tab.tsv b/packages/pieces/core/csv/test/fixtures/tab.tsvnew file mode 100644index 00000000000..8a1fdc45171--- /dev/null+++ b/packages/pieces/core/csv/test/fixtures/tab.tsv@@ -0,0 +1,3 @@+name age city+Ann 30 Paris, FR+Bob 25 Romediff --git a/packages/pieces/core/csv/test/fixtures/utf16be.csv b/packages/pieces/core/csv/test/fixtures/utf16be.csvnew file mode 100644index 00000000000..6cf2490ea2bBinary files /dev/null and b/packages/pieces/core/csv/test/fixtures/utf16be.csv differdiff --git a/packages/pieces/core/csv/test/new-actions-output-schema.test.ts b/packages/pieces/core/csv/test/new-actions-output-schema.test.tsnew file mode 100644index 00000000000..ccbd8cb7b46--- /dev/null+++ b/packages/pieces/core/csv/test/new-actions-output-schema.test.ts@@ -0,0 +1,100 @@+/// <reference types="vitest/globals" />++import { createMockActionContext, OutputSchema } from '@activepieces/pieces-framework';+import { readCsvFileAction } from '../src/lib/actions/read-csv-file';+import { createCsvFileAction } from '../src/lib/actions/create-csv-file';+import { convertCsvToExcelAction } from '../src/lib/actions/convert-csv-to-excel';+import { filterCsvRowsAction } from '../src/lib/actions/filter-csv-rows';+import { sortCsvRowsAction } from '../src/lib/actions/sort-csv-rows';+import { removeDuplicateCsvRowsAction } from '../src/lib/actions/remove-duplicate-csv-rows';+import { selectCsvColumnsAction } from '../src/lib/actions/select-csv-columns';+import { mergeCsvAction } from '../src/lib/actions/merge-csv';+import { splitCsvAction } from '../src/lib/actions/split-csv';++const CSV = 'name,email\nAnn,[email protected]\nBob,[email protected]\n';++const cases: { name: string; schema: OutputSchema | undefined; run: () => Promise<unknown> }[] = [+ {+ name: 'read_csv_file',+ schema: readCsvFileAction.outputSchema,+ run: () =>+ readCsvFileAction.run(+ createMockActionContext({ propsValue: { file: { filename: 'a.csv', data: Buffer.from(CSV) } } }),+ ),+ },+ {+ name: 'create_csv_file',+ schema: createCsvFileAction.outputSchema,+ run: () => createCsvFileAction.run(createMockActionContext({ propsValue: { json_array: [{ a: 1 }] } })),+ },+ {+ name: 'convert_csv_to_excel',+ schema: convertCsvToExcelAction.outputSchema,+ run: () => convertCsvToExcelAction.run(createMockActionContext({ propsValue: { csv_text: CSV } })),+ },+ {+ name: 'filter_csv_rows',+ schema: filterCsvRowsAction.outputSchema,+ run: () =>+ filterCsvRowsAction.run(+ createMockActionContext({ propsValue: { csv_text: CSV, column: 'name', condition: 'equals', value: 'Ann' } }),+ ),+ },+ {+ name: 'sort_csv_rows',+ schema: sortCsvRowsAction.outputSchema,+ run: () => sortCsvRowsAction.run(createMockActionContext({ propsValue: { csv_text: CSV, column: 'name' } })),+ },+ {+ name: 'remove_duplicate_csv_rows',+ schema: removeDuplicateCsvRowsAction.outputSchema,+ run: () => removeDuplicateCsvRowsAction.run(createMockActionContext({ propsValue: { csv_text: CSV } })),+ },+ {+ name: 'select_csv_columns',+ schema: selectCsvColumnsAction.outputSchema,+ run: () =>+ selectCsvColumnsAction.run(createMockActionContext({ propsValue: { csv_text: CSV, columns: ['email'] } })),+ },+ {+ name: 'merge_csv',+ schema: mergeCsvAction.outputSchema,+ run: () => mergeCsvAction.run(createMockActionContext({ propsValue: { csv_texts: [CSV, CSV] } })),+ },+ {+ name: 'split_csv',+ schema: splitCsvAction.outputSchema,+ run: () =>+ splitCsvAction.run(createMockActionContext({ propsValue: { csv_text: CSV, rows_per_chunk: 1 } })),+ },+];++describe.each(cases)('$name output schema', ({ schema, run }) => {+ test('every described path resolves, and nothing is left undescribed', async () => {+ expect(schema).toBeDefined();+ const output = toRecord(await run());+ const fields = schema?.fields ?? [];+ const described = fields.map((f) => f.value ?? f.key);+ for (const p of described) {+ expect(output[p], `"${p}" does not resolve`).toBeDefined();+ }+ for (const key of Object.keys(output)) {+ expect(described.includes(key), `"${key}" is not described`).toBe(true);+ }+ for (const field of fields.filter((f) => f.listItems)) {+ const items = output[field.value ?? field.key];+ expect(Array.isArray(items)).toBe(true);+ const first = toRecord(Array.isArray(items) ? items[0] : undefined);+ for (const child of field.listItems ?? []) {+ expect(first[child.value ?? child.key], `"${field.key}[].${child.key}" does not resolve`).toBeDefined();+ }+ }+ });+});++function toRecord(value: unknown): Record<string, unknown> {+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {+ throw new Error('expected an object');+ }+ return Object.fromEntries(Object.entries(value));+}diff --git a/packages/pieces/core/csv/test/new-actions.test.ts b/packages/pieces/core/csv/test/new-actions.test.tsnew file mode 100644index 00000000000..da7ec3283b0--- /dev/null+++ b/packages/pieces/core/csv/test/new-actions.test.ts@@ -0,0 +1,531 @@+/// <reference types="vitest/globals" />++import fs from 'fs';+import path from 'path';+import * as XLSX from 'xlsx';+import { createMockActionContext } from '@activepieces/pieces-framework';+import { csvUtils } from '../src/lib/common/csv-utils';+import { readCsvFileAction } from '../src/lib/actions/read-csv-file';+import { createCsvFileAction } from '../src/lib/actions/create-csv-file';+import { convertCsvToExcelAction } from '../src/lib/actions/convert-csv-to-excel';+import { filterCsvRowsAction } from '../src/lib/actions/filter-csv-rows';+import { sortCsvRowsAction } from '../src/lib/actions/sort-csv-rows';+import { removeDuplicateCsvRowsAction } from '../src/lib/actions/remove-duplicate-csv-rows';+import { selectCsvColumnsAction } from '../src/lib/actions/select-csv-columns';+import { mergeCsvAction } from '../src/lib/actions/merge-csv';+import { splitCsvAction } from '../src/lib/actions/split-csv';++const fixture = (name: string) => fs.readFileSync(path.join(__dirname, 'fixtures', name));+const fixtureText = (name: string) => fixture(name).toString('utf8');+const asFile = (name: string) => ({ filename: name, extension: 'csv', data: fixture(name) });++function withFileCapture<T extends { files: { write: unknown } }>(ctx: T) {+ const written: { fileName: string; data: Buffer }[] = [];+ const write = async ({ fileName, data }: { fileName: string; data: Buffer }) => {+ written.push({ fileName, data });+ return `https://files.test/${fileName}`;+ };+ return { ctx: { ...ctx, files: { ...ctx.files, write } }, written };+}++describe('delimiter detection', () => {+ test.each([+ ['semicolon.csv', ';'],+ ['tab.tsv', '\t'],+ ['pipe.csv', '|'],+ ['people.csv', ','],+ ['quoted-multiline.csv', ','],+ ])('%s → %j', (name, expected) => {+ expect(csvUtils.detectDelimiter(fixtureText(name))).toBe(expected);+ });++ test('ignores delimiters inside quoted values', () => {+ expect(csvUtils.detectDelimiter('a;b\n"x,y,z";1\n"p,q";2\n')).toBe(';');+ });++ test('falls back to comma for a single column', () => {+ expect(csvUtils.detectDelimiter('name\nAnn\nBob\n')).toBe(',');+ });+});++describe('read_csv_file', () => {+ const run = (propsValue: Record<string, unknown>) =>+ readCsvFileAction.run(createMockActionContext({ propsValue: propsValue }));++ test('strips the BOM, handles CRLF and keeps accents', async () => {+ const out = await run({ file: asFile('bom-utf8.csv') });+ expect(out.headers).toEqual(['name', 'city']);+ expect(out.rows).toEqual([+ { name: 'José', city: 'Zürich' },+ { name: 'Renée', city: 'Köln' },+ ]);+ expect(out.row_count).toBe(2);+ });++ test('auto-detects semicolons and keeps decimal commas and quoted semicolons', async () => {+ const out = await run({ file: asFile('semicolon.csv'), delimiter: 'auto' });+ expect(out.delimiter).toBe(';');+ expect(out.rows[0]).toEqual({ name: 'Ann', amount: '1,50', note: 'a;b' });+ });++ test('explicit delimiter overrides detection', async () => {+ const out = await run({ file: asFile('semicolon.csv'), delimiter: ',' });+ expect(out.headers).toEqual(['name;amount;note', 'column_2']);+ });++ test('skips blank and delimiter-only lines anywhere', async () => {+ const out = await run({ file: asFile('blank-lines.csv') });+ expect(out.rows).toEqual([+ { a: '1', b: '2' },+ { a: '3', b: '4' },+ { a: '5', b: '6' },+ ]);+ });++ test('ragged rows: short rows padded, extra cells kept in a new column', async () => {+ const out = await run({ file: asFile('ragged.csv') });+ expect(out.headers).toEqual(['id', 'name', 'email', 'column_4']);+ expect(out.rows[0]).toEqual({ id: '1', name: 'Ann', email: '', column_4: '' });+ expect(out.rows[1].column_4).toBe('EXTRA');+ });++ test('duplicate and blank headers are renamed, never overwritten', async () => {+ const out = await run({ file: asFile('dup-headers.csv') });+ expect(out.headers).toEqual(['email', 'name', 'email_2', 'column_4', 'name_2']);+ expect(out.rows[0]).toEqual({+ email: '[email protected]',+ name: 'Ann',+ email_2: '[email protected]',+ column_4: 'note',+ name_2: 'Annie',+ });+ });++ test('quoted multi-line values, escaped quotes and stray quotes', async () => {+ const out = await run({ file: asFile('quoted-multiline.csv') });+ expect(out.rows.map((r) => r.comment)).toEqual([+ 'line one\nline two',+ 'he said "hi", then left',+ 'plain "quote" inside',+ ]);+ });++ test('no header row → column_N keys', async () => {+ const out = await run({ file: asFile('tab.tsv'), has_header_row: false });+ expect(out.headers).toEqual(['column_1', 'column_2', 'column_3']);+ expect(out.rows[0]).toEqual({ column_1: 'name', column_2: 'age', column_3: 'city' });+ expect(out.row_count).toBe(3);+ });++ test('decodes UTF-16 LE (Excel "Unicode Text") and UTF-16 BE', async () => {+ const le = await run({ file: asFile('excel-unicode-text.txt') });+ expect(le.delimiter).toBe('\t');+ expect(le.rows).toEqual([{ name: 'José', city: 'Zürich' }]);+ const be = await run({ file: asFile('utf16be.csv') });+ expect(be.rows).toEqual([{ name: 'José', city: 'Zürich' }]);+ });++ test('accepts a base64-only file object (duck-typed)', async () => {+ const out = await run({ file: { filename: 'x.csv', base64: fixture('pipe.csv').toString('base64') } });+ expect(out.rows[0]).toEqual({ sku: 'A1', price: '9.99', name: 'Widget, large' });+ });++ test('trim option', async () => {+ const out = await run({ file: asFile('people.csv'), trim_whitespace: true });+ expect(out.rows[4].email).toBe('[email protected]');+ });++ test('rejects an Excel file with a pointer to Convert Excel to CSV', async () => {+ const wb = XLSX.utils.book_new();+ XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([['a']]), 'S');+ const data: Buffer = XLSX.write(wb, { type: 'buffer', bookType: 'xlsx' });+ await expect(run({ file: { filename: 'x.xlsx', data } })).rejects.toThrow(/Convert Excel to CSV/);+ });++ test('missing file and empty file', async () => {+ await expect(run({ file: undefined })).rejects.toThrow(/No file was provided/);+ const out = await run({ file: { filename: 'e.csv', data: Buffer.from('\n\n') } });+ expect(out).toEqual({ rows: [], row_count: 0, headers: [], delimiter: ',' });+ });++ test('enforces the size limit', async () => {+ const data = Buffer.alloc(21 * 1024 * 1024, 'a');+ await expect(run({ file: { filename: 'big.csv', data } })).rejects.toThrow(/larger than 20 MB/);+ });+});++describe('create_csv_file', () => {+ const run = async (propsValue: Record<string, unknown>) => {+ const { ctx, written } = withFileCapture(createMockActionContext({ propsValue: propsValue }));+ const out = await createCsvFileAction.run(ctx);+ return { out, written };+ };++ test('union of columns, booleans true/false, null blank, nested flattened', async () => {+ const { out, written } = await run({+ json_array: [+ { name: 'Ann', paid: true, address: { city: 'Paris' } },+ { name: 'Bob', paid: false, extra: null, tags: [] },+ { name: 'Cy "Q"', note: 'a,b\nc' },+ ],+ });+ expect(out.columns).toEqual(['name', 'paid', 'address.city', 'extra', 'tags', 'note']);+ expect(written[0].data.toString('utf8')).toBe(+ 'name,paid,address.city,extra,tags,note\n' ++ 'Ann,true,Paris,,,\n' ++ 'Bob,false,,,,\n' ++ '"Cy ""Q""",,,,,"a,b\nc"\n',+ );+ expect(out).toMatchObject({ file_name: 'export.csv', url: 'https://files.test/export.csv', row_count: 3 });+ });++ test('explicit columns, semicolon, no header, Excel BOM, extension added', async () => {+ const { out, written } = await run({+ json_array: [{ a: 1, b: 2, c: 3 }],+ columns: ['c', 'a', 'missing'],+ delimiter: ';',+ include_header: false,+ add_excel_bom: true,+ file_name: 'reports/q3',+ });+ expect(out.file_name).toBe('reports_q3.csv');+ expect(written[0].data.toString('utf8')).toBe('3;1;\n');+ });++ test('accepts a JSON string and rejects non-arrays / non-object rows', async () => {+ const { out } = await run({ json_array: '[{"x":1}]' });+ expect(out.row_count).toBe(1);+ await expect(run({ json_array: { x: 1 } })).rejects.toThrow(/not a single object/);+ await expect(run({ json_array: [{ x: 1 }, 'oops'] })).rejects.toThrow(/Row 2 is not an object/);+ await expect(run({ json_array: '{bad' })).rejects.toThrow(/must be a JSON array/);+ });++ test('round-trips through read_csv_file', async () => {+ const { written } = await run({ json_array: [{ name: 'José', note: 'x;y' }], add_excel_bom: true });+ const back = await readCsvFileAction.run(+ createMockActionContext({ propsValue: { file: { filename: 'r.csv', data: written[0].data } } }),+ );+ expect(back.rows).toEqual([{ name: 'José', note: 'x;y' }]);+ });+});++describe('convert_csv_to_excel', () => {+ const run = async (propsValue: Record<string, unknown>) => {+ const { ctx, written } = withFileCapture(createMockActionContext({ propsValue: propsValue }));+ const out = await convertCsvToExcelAction.run(ctx);+ const sheet = XLSX.read(written[0].data, { type: 'buffer' });+ return { out, sheet, written };+ };++ test('keeps leading zeros and long ids as text, stores safe numbers as numbers', async () => {+ const { out, sheet } = await run({ csv_text: fixtureText('leading-zeros.csv') });+ const ws = sheet.Sheets['Sheet1'];+ expect(ws['A2']).toMatchObject({ t: 's', v: '01234' });+ expect(ws['B2']).toMatchObject({ t: 's', v: '12345678901234567890' });+ expect(ws['B3']).toMatchObject({ t: 's', v: '9007199254740993' });+ expect(ws['C2']).toMatchObject({ t: 'n', v: 42.5 });+ expect(ws['D2']).toMatchObject({ t: 's', v: 'TRUE' });+ expect(ws['E2']).toMatchObject({ t: 'n', v: -3 });+ expect(out).toEqual({+ file_name: 'data.xlsx',+ url: 'https://files.test/data.xlsx',+ row_count: 3,+ sheet_name: 'Sheet1',+ });+ });++ test('detect_numbers off keeps every cell as text', async () => {+ const { sheet } = await run({ csv_text: fixtureText('leading-zeros.csv'), detect_numbers: false });+ expect(sheet.Sheets['Sheet1']['C2']).toMatchObject({ t: 's', v: '42.5' });+ });++ test('semicolon input, custom sheet and file name, xlsx signature', async () => {+ const { out, sheet, written } = await run({+ csv_text: fixtureText('semicolon.csv'),+ sheet_name: 'Orders',+ file_name: 'orders',+ });+ expect(out.file_name).toBe('orders.xlsx');+ expect(sheet.SheetNames).toEqual(['Orders']);+ expect(sheet.Sheets['Orders']['B2']).toMatchObject({ v: '1,50' });+ expect(written[0].data[0]).toBe(0x50);+ });++ test('rejects invalid sheet names', async () => {+ await expect(run({ csv_text: 'a\n1', sheet_name: 'Q3/2026' })).rejects.toThrow(/not a valid sheet name/);+ await expect(run({ csv_text: 'a\n1', sheet_name: 'x'.repeat(32) })).rejects.toThrow(/not a valid sheet name/);+ });+});++describe('filter_csv_rows', () => {+ const people = fixtureText('people.csv');+ const names = async (propsValue: Record<string, unknown>) => {+ const out = await filterCsvRowsAction.run(+ createMockActionContext({ propsValue: { csv_text: people, ...propsValue } }),+ );+ return out.rows.map((r) => r.name);+ };++ test('equals is case-insensitive by default, sensitive on request', async () => {+ expect(await names({ column: 'status', condition: 'equals', value: 'paid' })).toEqual(['Ann', 'bob', 'Eve']);+ expect(await names({ column: 'status', condition: 'equals', value: 'paid', case_sensitive: true })).toEqual(['bob']);+ });++ test('numbers compare as numbers (10 > 9), blanks excluded', async () => {+ expect(await names({ column: 'age', condition: 'greater_than', value: '9' })).toEqual(['Ann', 'Cy', 'Eve']);+ expect(await names({ column: 'age', condition: 'less_or_equal', value: '10' })).toEqual(['bob', 'Cy']);+ });++ test('text conditions and emptiness', async () => {+ expect(await names({ column: 'email', condition: 'contains', value: 'ANN@' })).toEqual(['Ann', 'Dee']);+ expect(await names({ column: 'email', condition: 'ends_with', value: 'x.com' })).toHaveLength(5);+ expect(await names({ column: 'name', condition: 'starts_with', value: 'd' })).toEqual(['Dee']);+ expect(await names({ column: 'status', condition: 'not_contains', value: 'paid' })).toEqual(['Cy', 'Dee']);+ expect(await names({ column: 'joined', condition: 'is_empty' })).toEqual(['Cy']);+ expect(await names({ column: 'age', condition: 'is_not_empty' })).toHaveLength(4);+ expect(await names({ column: 'status', condition: 'not_equals', value: 'trial' })).toEqual(['Ann', 'bob', 'Eve']);+ });++ test('column matched case-insensitively; missing column lists the real ones', async () => {+ expect(await names({ column: 'STATUS', condition: 'equals', value: 'trial' })).toEqual(['Cy', 'Dee']);+ await expect(names({ column: 'plan', condition: 'equals', value: 'x' })).rejects.toThrow(+ 'Column "plan" not found. Available columns: name, email, age, status, joined',+ );+ });++ test('value required for comparisons', async () => {+ await expect(names({ column: 'age', condition: 'greater_than' })).rejects.toThrow(/Enter a value/);+ });++ test('csv output keeps the input delimiter', async () => {+ const out = await filterCsvRowsAction.run(+ createMockActionContext({+ propsValue: { csv_text: fixtureText('semicolon.csv'), column: 'name', condition: 'equals', value: 'Ann' },+ }),+ );+ expect(out.csv).toBe('name;amount;note\nAnn;1,50;"a;b"\n');+ expect(out.row_count).toBe(1);+ });+});++describe('sort_csv_rows', () => {+ const people = fixtureText('people.csv');+ const names = async (propsValue: Record<string, unknown>) => {+ const out = await sortCsvRowsAction.run(+ createMockActionContext({ propsValue: { csv_text: people, ...propsValue } }),+ );+ return out.rows.map((r) => r.name);+ };++ test('number sort, blanks last in both directions', async () => {+ expect(await names({ column: 'age', compare_as: 'number' })).toEqual(['bob', 'Cy', 'Ann', 'Eve', 'Dee']);+ expect(await names({ column: 'age', compare_as: 'number', direction: 'desc' })).toEqual(['Eve', 'Ann', 'Cy', 'bob', 'Dee']);+ });++ test('text sort is natural and case-insensitive', async () => {+ expect(await names({ column: 'age' })).toEqual(['bob', 'Cy', 'Ann', 'Eve', 'Dee']);+ expect(await names({ column: 'name' })).toEqual(['Ann', 'bob', 'Cy', 'Dee', 'Eve']);+ });++ test('date sort, unreadable dates last', async () => {+ expect(await names({ column: 'joined', compare_as: 'date', direction: 'desc' })).toEqual(['Dee', 'Ann', 'bob', 'Cy', 'Eve']);+ });++ test('stable for equal keys', async () => {+ expect(await names({ column: 'email' })).toEqual(['Ann', 'Dee', 'bob', 'Cy', 'Eve']);+ });+});++describe('remove_duplicate_csv_rows', () => {+ const run = (propsValue: Record<string, unknown>) =>+ removeDuplicateCsvRowsAction.run(createMockActionContext({ propsValue: propsValue }));++ test('whole-row duplicates (trim on by default)', async () => {+ const out = await run({ csv_text: 'a,b\n1,2\n1, 2\n3,4\n1,2\n' });+ expect(out.rows).toEqual([{ a: '1', b: '2' }, { a: '3', b: '4' }]);+ expect(out.removed_count).toBe(2);+ });++ test('key columns, case-insensitive, keep last', async () => {+ const out = await run({+ csv_text: fixtureText('people.csv'),+ key_columns: ['email'],+ case_sensitive: false,+ keep: 'last',+ });+ expect(out.rows.map((r) => r.name)).toEqual(['bob', 'Cy', 'Dee', 'Eve']);+ expect(out.removed_count).toBe(1);+ });++ test('case-sensitive by default', async () => {+ const out = await run({ csv_text: 'e\nA\na\n' });+ expect(out.removed_count).toBe(0);+ });++ test('unknown key column errors', async () => {+ await expect(run({ csv_text: 'a\n1', key_columns: ['nope'] })).rejects.toThrow(/Column "nope" not found/);+ });+});++describe('select_csv_columns', () => {+ const run = (propsValue: Record<string, unknown>) =>+ selectCsvColumnsAction.run(createMockActionContext({ propsValue: propsValue }));++ test('keep + reorder', async () => {+ const out = await run({ csv_text: fixtureText('people.csv'), columns: ['email', 'name'] });+ expect(out.headers).toEqual(['email', 'name']);+ expect(out.csv.split('\n')[1]).toBe('[email protected],Ann');+ });++ test('remove mode', async () => {+ const out = await run({ csv_text: fixtureText('people.csv'), columns: ['age', 'joined'], mode: 'remove' });+ expect(out.headers).toEqual(['name', 'email', 'status']);+ });++ test('errors for no columns or unknown columns', async () => {+ await expect(run({ csv_text: 'a\n1', columns: [] })).rejects.toThrow(/at least one column/);+ await expect(run({ csv_text: 'a\n1', columns: ['b'] })).rejects.toThrow(/Available columns: a/);+ });+});++describe('merge_csv', () => {+ const run = (propsValue: Record<string, unknown>) =>+ mergeCsvAction.run(createMockActionContext({ propsValue: propsValue }));++ test('union of headers in first-seen order, blanks filled, mixed delimiters auto-detected', async () => {+ const out = await run({+ csv_texts: ['name;email\nAnn;[email protected]\n', fixtureText('extra-columns.csv')],+ });+ expect(out.headers).toEqual(['name', 'email', 'country']);+ expect(out.rows).toEqual([+ { name: 'Ann', email: '[email protected]', country: '' },+ { name: 'Zed', email: '[email protected]', country: 'DE' },+ ]);+ expect(out.csv).toBe('name;email;country\nAnn;[email protected];\nZed;[email protected];DE\n');+ });++ test('needs at least two inputs; non-text input errors with its position', async () => {+ await expect(run({ csv_texts: ['a\n1'] })).rejects.toThrow(/at least two/);+ await expect(run({ csv_texts: ['a\n1', 5] })).rejects.toThrow(/CSV Text 2 must be text/);+ });+});++describe('split_csv', () => {+ const rows250 = ['id,name', ...Array.from({ length: 250 }, (_, i) => `${i + 1},n${i + 1}`)].join('\n');+ const run = (propsValue: Record<string, unknown>) =>+ splitCsvAction.run(createMockActionContext({ propsValue: propsValue }));++ test('250 rows / 100 → 100, 100, 50 with header repeated', async () => {+ const out = await run({ csv_text: rows250, rows_per_chunk: 100 });+ expect(out.chunk_count).toBe(3);+ expect(out.total_rows).toBe(250);+ expect(out.chunks.map((c) => c.row_count)).toEqual([100, 100, 50]);+ expect(out.chunks[2].csv.startsWith('id,name\n201,n201\n')).toBe(true);+ expect(out.chunks.map((c) => c.index)).toEqual([1, 2, 3]);+ });++ test('header only on the first chunk when repeat is off; string number accepted', async () => {+ const out = await run({ csv_text: rows250, rows_per_chunk: '200', repeat_header: false });+ expect(out.chunks[0].csv.startsWith('id,name\n')).toBe(true);+ expect(out.chunks[1].csv.startsWith('201,n201\n')).toBe(true);+ });++ test('validates rows_per_chunk', async () => {+ await expect(run({ csv_text: rows250, rows_per_chunk: 0 })).rejects.toThrow(/whole number/);+ await expect(run({ csv_text: rows250, rows_per_chunk: 2.5 })).rejects.toThrow(/whole number/);+ await expect(run({ csv_text: rows250, rows_per_chunk: 10001 })).rejects.toThrow(/whole number/);+ });+});++describe('limits', () => {+ test('row limit points to Stream CSV to Subflows', async () => {+ const big = ['a', ...Array.from({ length: 100_001 }, () => '1')].join('\n');+ await expect(+ filterCsvRowsAction.run(+ createMockActionContext({ propsValue: { csv_text: big, column: 'a', condition: 'is_empty' } }),+ ),+ ).rejects.toThrow(/up to 100000.*Stream CSV to Subflows/);+ });+});++describe('review fixes', () => {+ const readText = async ({ text, hasHeader }: { text: string; hasHeader: boolean }) =>+ readCsvFileAction.run(+ createMockActionContext({+ propsValue: { file: { filename: 'r.csv', data: Buffer.from(text) }, has_header_row: hasHeader },+ }),+ );+ const excel = async (propsValue: Record<string, unknown>) => {+ const { ctx, written } = withFileCapture(createMockActionContext({ propsValue }));+ const out = await convertCsvToExcelAction.run(ctx);+ return { out, ws: XLSX.read(written[0].data, { type: 'buffer' }).Sheets['Sheet1'] };+ };+ const createCsv = async (propsValue: Record<string, unknown>) => {+ const { ctx, written } = withFileCapture(createMockActionContext({ propsValue }));+ await createCsvFileAction.run(ctx);+ return written[0].data.toString('utf8');+ };++ test('create_csv_file prefixes formula-like text by default and leaves numbers alone', async () => {+ const csv = await createCsv({+ json_array: [+ { a: '=HYPERLINK("http://x","y")', b: '@SUM(1)', c: '+1+1', d: '-5', e: '-1+2', f: 'plain', g: -3 },+ ],+ });+ expect(csv.split('\n')[1]).toBe(`"'=HYPERLINK(""http://x"",""y"")",'@SUM(1),'+1+1,-5,'-1+2,plain,-3`);+ });++ test('create_csv_file escapes formula-like headers too', async () => {+ const csv = await createCsv({ json_array: [{ '=cmd': 'x' }] });+ expect(csv.split('\n')[0]).toBe("'=cmd");+ });++ test('create_csv_file writes values unchanged when protection is off', async () => {+ const csv = await createCsv({ json_array: [{ a: '=1+1' }], escape_formulas: false });+ expect(csv.split('\n')[1]).toBe('=1+1');+ });++ test('convert_csv_to_excel keeps numbers that would lose precision as text', async () => {+ const { ws } = await excel({ csv_text: 'a,b,c\n99999999999999.999999999999999,1234567890.12345,0.10\n' });+ expect(ws['A2']).toMatchObject({ t: 's', v: '99999999999999.999999999999999' });+ expect(ws['B2']).toMatchObject({ t: 'n', v: 1234567890.12345 });+ expect(ws['C2']).toMatchObject({ t: 'n', v: 0.1 });+ });++ test('convert_csv_to_excel keeps numeric headings as text unless there is no header row', async () => {+ const withHeader = await excel({ csv_text: '2026,2027\n1,2\n' });+ expect(withHeader.ws['A1']).toMatchObject({ t: 's', v: '2026' });+ expect(withHeader.ws['A2']).toMatchObject({ t: 'n', v: 1 });+ const noHeader = await excel({ csv_text: '2026,2027\n1,2\n', has_header_row: false });+ expect(noHeader.ws['A1']).toMatchObject({ t: 'n', v: 2026 });+ });++ test('headerless files count every row against the limit', async () => {+ const rows = Array.from({ length: 100_001 }, () => '1').join('\n');+ await expect(readText({ text: rows, hasHeader: false })).rejects.toThrow(/has 100001 rows/);+ const withHeader = await readText({ text: `a\n${rows}`.split('\n').slice(0, 100_001).join('\n'), hasHeader: true });+ expect(withHeader.row_count).toBe(100_000);+ await expect(excel({ csv_text: rows, has_header_row: false })).rejects.toThrow(/has 100001 rows/);+ });++ test('wide duplicate headers are renamed in linear time', () => {+ const raw = Array.from({ length: 50_000 }, () => 'a');+ const started = Date.now();+ const headers = csvUtils.normalizeHeaders({ raw, width: raw.length });+ expect(Date.now() - started).toBeLessThan(1000);+ expect(headers.slice(0, 3)).toEqual(['a', 'a_2', 'a_3']);+ expect(headers[49_999]).toBe('a_50000');+ expect(new Set(headers).size).toBe(50_000);+ });++ test('renaming still skips names that already exist', () => {+ expect(csvUtils.normalizeHeaders({ raw: ['email', 'email', 'email_2', '', 'name'], width: 6 })).toEqual([+ 'email',+ 'email_2',+ 'email_2_2',+ 'column_4',+ 'name',+ 'column_6',+ ]);+ });+});