Skip to content

armgit / macro

Public Imported from GitHub
0

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

feat(crm): round-trip CRM CSVs in the app and through agent tools (#8159)

Jacob Beckerman187c07b497f5
diff --git a/Cargo.lock b/Cargo.lock
index ff74e6dc20..29dd751d44 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5192,10 +5192,12 @@ dependencies = [
"bot_id",
"call",
"chrono",
+ "csv",
"databases",
"entity_access",
"entity_access_db_utils",
"filter_ast",
+ "futures",
"item_filters",
"kafka_util",
"macro_authorization",
@@ -5427,6 +5429,27 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "csv"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938"
+dependencies = [
+ "csv-core",
+ "itoa",
+ "ryu",
+ "serde_core",
+]
+
+[[package]]
+name = "csv-core"
+version = "0.1.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782"
+dependencies = [
+ "memchr",
+]
+
[[package]]
name = "ctr"
version = "0.9.2"
diff --git a/apps/docs/AI/mcp/tools/export-pipeline-csv.mdx b/apps/docs/AI/mcp/tools/export-pipeline-csv.mdx
new file mode 100644
index 0000000000..8b26439a95
--- /dev/null
+++ b/apps/docs/AI/mcp/tools/export-pipeline-csv.mdx
@@ -0,0 +1,17 @@
+---
+title: ExportPipelineCsv
+description: >-
+ Export a CRM pipeline's deals (entries) to a CSV file saved in the user's
+ Macro files: Company and Domain (or Contact and Email), then one column per
+ field, with people as emails.
+---
+
+# ExportPipelineCsv
+
+Export a CRM pipeline's deals (entries) to a CSV file saved in the user's Macro files: Company and Domain (or Contact and Email), then one column per field, with people as emails. Use it to back up a pipeline, move it to another team (ImportPipelineCsv reads the same file), or share it as a spreadsheet; to answer questions about deals, use FindPipelineEntries instead. Returns the file's documentId and how many entries it holds.
+
+## Parameters
+
+| Parameter | Type | Required | Description |
+| --- | --- | --- | --- |
+| `pipelineId` | string | Yes | The pipeline's id from ListPipelines. |
diff --git a/apps/docs/AI/mcp/tools/import-companies-csv.mdx b/apps/docs/AI/mcp/tools/import-companies-csv.mdx
new file mode 100644
index 0000000000..fdd5f223e8
--- /dev/null
+++ b/apps/docs/AI/mcp/tools/import-companies-csv.mdx
@@ -0,0 +1,20 @@
+---
+title: ImportCompaniesCsv
+description: >-
+ Import companies into the team CRM from a CSV, such as a Macro companies
+ export or a list from another CRM.
+---
+
+# ImportCompaniesCsv
+
+Import companies into the team CRM from a CSV, such as a Macro companies export or a list from another CRM. Each row needs a domain (a Domain, Domains or Website column) and may name the company (Company or Name); a row takes its first valid domain, and companies the team already has are left as they are. Stage, Owner and Revenue live on pipeline entries rather than companies: when the CSV has them, they go on entries of pipelineId, or of a new pipeline (private unless shareNewPipeline) named after the file. Give the CSV as text in csv, or as documentId for a CSV file in Macro. Returns counts and the rows that could not be imported. To add rows to a pipeline without this, use ImportPipelineCsv.
+
+## Parameters
+
+| Parameter | Type | Required | Description |
+| --- | --- | --- | --- |
+| `csv` | string | No | The CSV text with a header row. Give this or documentId, not both. |
+| `documentId` | string | No | The id of a CSV file in Macro the user can open, e.g. one they uploaded. Give this or csv, not both. |
+| `pipelineId` | string | No | A company pipeline (id from ListPipelines) to put the CSV's Stage, Owner and Revenue on. Omit to create a new pipeline when the CSV has them. |
+| `newPipelineName` | string | No | The name of the new pipeline, when one is created. Defaults to the file's name. |
+| `shareNewPipeline` | boolean | No | Share the new pipeline with the team instead of keeping it private to the user. Defaults to false. |
diff --git a/apps/docs/AI/mcp/tools/import-pipeline-csv.mdx b/apps/docs/AI/mcp/tools/import-pipeline-csv.mdx
new file mode 100644
index 0000000000..e0d775df8f
--- /dev/null
+++ b/apps/docs/AI/mcp/tools/import-pipeline-csv.mdx
@@ -0,0 +1,18 @@
+---
+title: ImportPipelineCsv
+description: >-
+ Import a CSV into a CRM pipeline as new deals (entries), such as a pipeline
+ export or a sheet of deals.
+---
+
+# ImportPipelineCsv
+
+Import a CSV into a CRM pipeline as new deals (entries), such as a pipeline export or a sheet of deals. In a company pipeline each row needs a domain (Domain, Domains or Website column) and may name the company (Company); in a contact pipeline each row needs an Email and may name the contact (Contact). Missing companies and contacts are created. Other columns fill the pipeline fields with the same name (Stage, Owner as an email, Revenue, custom fields); stage labels the pipeline lacks are added. Records already in the pipeline are not added again, so retrying an interrupted import adds only what is missing; several rows for one new record become separate deals. Give the CSV as text in csv, or as documentId for a CSV file in Macro. Returns counts and the rows that could not be added.
+
+## Parameters
+
+| Parameter | Type | Required | Description |
+| --- | --- | --- | --- |
+| `pipelineId` | string | Yes | The pipeline's id from ListPipelines. |
+| `csv` | string | No | The CSV text with a header row. Give this or documentId, not both. |
+| `documentId` | string | No | The id of a CSV file in Macro the user can open, e.g. one they uploaded or an ExportPipelineCsv result. Give this or csv, not both. |
diff --git a/apps/docs/AI/mcp/tools/index.mdx b/apps/docs/AI/mcp/tools/index.mdx
index 042081ef4a..88909089ee 100644
--- a/apps/docs/AI/mcp/tools/index.mdx
+++ b/apps/docs/AI/mcp/tools/index.mdx
@@ -60,6 +60,7 @@ These pages are generated from Macro's Rust MCP tool registry.
- [EditSpreadsheet](/AI/mcp/tools/edit-spreadsheet)
- [EditTag](/AI/mcp/tools/edit-tag)
- [EditWordDocument](/AI/mcp/tools/edit-word-document)
+- [ExportPipelineCsv](/AI/mcp/tools/export-pipeline-csv)
- [FindPipelineEntries](/AI/mcp/tools/find-pipeline-entries)
- [GenerateImage](/AI/mcp/tools/generate-image)
- [GenerateObject](/AI/mcp/tools/generate-object)
@@ -70,7 +71,9 @@ These pages are generated from Macro's Rust MCP tool registry.
- [GetEntityProperties](/AI/mcp/tools/get-entity-properties)
- [GetTeamAvailability](/AI/mcp/tools/get-team-availability)
- [GetThread](/AI/mcp/tools/get-thread)
+- [ImportCompaniesCsv](/AI/mcp/tools/import-companies-csv)
- [ImportNotionPage](/AI/mcp/tools/import-notion-page)
+- [ImportPipelineCsv](/AI/mcp/tools/import-pipeline-csv)
- [IssueBotCredential](/AI/mcp/tools/issue-bot-credential)
- [ListAgents](/AI/mcp/tools/list-agents)
- [ListBookingLinks](/AI/mcp/tools/list-booking-links)
diff --git a/apps/docs/config/tool-pages.json b/apps/docs/config/tool-pages.json
index 408fb1d217..553e48063a 100644
--- a/apps/docs/config/tool-pages.json
+++ b/apps/docs/config/tool-pages.json
@@ -51,6 +51,7 @@
"AI/mcp/tools/edit-spreadsheet",
"AI/mcp/tools/edit-tag",
"AI/mcp/tools/edit-word-document",
+ "AI/mcp/tools/export-pipeline-csv",
"AI/mcp/tools/find-pipeline-entries",
"AI/mcp/tools/generate-image",
"AI/mcp/tools/generate-object",
@@ -61,7 +62,9 @@
"AI/mcp/tools/get-entity-properties",
"AI/mcp/tools/get-team-availability",
"AI/mcp/tools/get-thread",
+ "AI/mcp/tools/import-companies-csv",
"AI/mcp/tools/import-notion-page",
+ "AI/mcp/tools/import-pipeline-csv",
"AI/mcp/tools/issue-bot-credential",
"AI/mcp/tools/list-agents",
"AI/mcp/tools/list-booking-links",
diff --git a/apps/docs/docs.json b/apps/docs/docs.json
index 9460abe8bb..5b63b89bfc 100644
--- a/apps/docs/docs.json
+++ b/apps/docs/docs.json
@@ -122,6 +122,7 @@
"AI/mcp/tools/edit-spreadsheet",
"AI/mcp/tools/edit-tag",
"AI/mcp/tools/edit-word-document",
+ "AI/mcp/tools/export-pipeline-csv",
"AI/mcp/tools/find-pipeline-entries",
"AI/mcp/tools/generate-image",
"AI/mcp/tools/generate-object",
@@ -132,7 +133,9 @@
"AI/mcp/tools/get-entity-properties",
"AI/mcp/tools/get-team-availability",
"AI/mcp/tools/get-thread",
+ "AI/mcp/tools/import-companies-csv",
"AI/mcp/tools/import-notion-page",
+ "AI/mcp/tools/import-pipeline-csv",
"AI/mcp/tools/issue-bot-credential",
"AI/mcp/tools/list-agents",
"AI/mcp/tools/list-booking-links",
diff --git a/apps/web/src/features/block-agent/state/tool-approval-wording.ts b/apps/web/src/features/block-agent/state/tool-approval-wording.ts
index 59e0a5c0b5..b62ef45a9a 100644
--- a/apps/web/src/features/block-agent/state/tool-approval-wording.ts
+++ b/apps/web/src/features/block-agent/state/tool-approval-wording.ts
@@ -65,6 +65,7 @@ const MACRO_TOOLS: Record<MacroToolName, Wording | null> = {
EditSpreadsheet: (whose) => `edit ${whose} spreadsheets`,
EditTag: (whose) => `edit tags in ${whose} workspace`,
EditWordDocument: (whose) => `edit ${whose} documents`,
+ ExportPipelineCsv: (whose) => `export ${whose} pipelines to CSV`,
FindPipelineEntries: (whose) => `read ${whose} pipelines`,
ExecuteCode: (whose) =>
`run potentially sensitive tools with ${whose} access`,
@@ -79,7 +80,9 @@ const MACRO_TOOLS: Record<MacroToolName, Wording | null> = {
GetEntityProperties: (whose) => `read properties in ${whose} workspace`,
GetTeamAvailability: (whose) => `check availability for ${whose} team`,
GetThread: (whose) => `read ${whose} email`,
+ ImportCompaniesCsv: (whose) => `import companies into ${whose} CRM`,
ImportNotionPage: (whose) => `import a Notion page into ${whose} workspace`,
+ ImportPipelineCsv: (whose) => `import deals into ${whose} pipelines`,
IssueBotCredential: (whose) => `issue credentials for ${whose} bots`,
ListAgents: (whose) => `list ${whose} agents`,
ListBookings: (whose) => `read ${whose} bookings`,
diff --git a/apps/web/src/features/block-database/queries/transfer.test.ts b/apps/web/src/features/block-database/queries/transfer.test.ts
index 0fac039067..8fc805ea54 100644
--- a/apps/web/src/features/block-database/queries/transfer.test.ts
+++ b/apps/web/src/features/block-database/queries/transfer.test.ts
@@ -4,11 +4,8 @@ import type { DatabaseDetail } from '@service-storage/generated/schemas/database
import type { TableDetail } from '@service-storage/generated/schemas/tableDetail';
import { errAsync, ok, okAsync } from 'neverthrow';
import { describe, expect, it, vi } from 'vitest';
-import {
- exportDatabaseTableCsv,
- exportedValue,
- importDatabaseTable,
-} from './transfer';
+import { exportedValue } from '../../database/core/csv';
+import { exportDatabaseTableCsv, importDatabaseTable } from './transfer';
const mocks = vi.hoisted(() => ({
read: vi.fn(),
diff --git a/apps/web/src/features/block-database/queries/transfer.ts b/apps/web/src/features/block-database/queries/transfer.ts
index d435de2111..f3e4d78b78 100644
--- a/apps/web/src/features/block-database/queries/transfer.ts
+++ b/apps/web/src/features/block-database/queries/transfer.ts
@@ -10,14 +10,8 @@ import type { ImportTable } from '@service-storage/generated/schemas/importTable
import type { Table } from '@service-storage/generated/schemas/table';
import type { TableDetail } from '@service-storage/generated/schemas/tableDetail';
import { err, ok, type ResultAsync } from 'neverthrow';
-import { encodeDatabaseCsv } from '../../database/core/csv';
-import { relatedRowIds } from '../../database/core/database-relations';
-import type {
- DatabaseCellValue,
- DatabaseViewColumn,
-} from '../../database/core/database-view';
+import { encodeDatabaseCsv, exportedValue } from '../../database/core/csv';
import { gridRows } from '../../database/core/grid-cells';
-import { formatCellValue } from '../../database/core/table';
import { tableRowsStatement } from '../sql';
import { toViewColumn } from './table-rows';
@@ -37,24 +31,6 @@ export function importDatabaseTable(
/** The table's rows could not be read, or not all of them. */
type DatabaseExportFailure = DatabaseSqlFailure | { kind: 'too-large' };
-/**
- * A cell as the CSV carries it: what the grid shows, except where its display
- * drops data. Dates keep their stored moment (the grid shows the day) and
- * numbers every digit (the grid rounds). Relation cells have no row names
- * loaded here, so they keep the related rows' ids rather than claiming those
- * rows are unavailable.
- */
-export function exportedValue(
- column: DatabaseViewColumn,
- value: DatabaseCellValue
-): string {
- if (column.relation) return relatedRowIds(value).join(', ');
- if (column.dataType === 'DATE' && typeof value === 'string') return value;
- if (column.dataType === 'NUMBER' && typeof value === 'number')
- return String(value);
- return formatCellValue(column, value);
-}
-
/** Never silently export a partial read. */
export function exportDatabaseTableCsv(
database: DatabaseDetail,
diff --git a/apps/web/src/features/crm/browser-test/pipelines.browser.e2e.ts b/apps/web/src/features/crm/browser-test/pipelines.browser.e2e.ts
index 2b58990ae3..23f4caa751 100644
--- a/apps/web/src/features/crm/browser-test/pipelines.browser.e2e.ts
+++ b/apps/web/src/features/crm/browser-test/pipelines.browser.e2e.ts
@@ -81,3 +81,51 @@ test('contact pipeline preserves choices after a failed create', async ({
page.getByRole('navigation', { name: 'CRM pipelines' })
).toContainText('Team');
});
+
+test('export a pipeline and import a CSV into it', async ({ page }) => {
+ await page.routeWebSocket('**', (socket) => socket.close());
+ await page.route('https://**', (route) => route.abort());
+ await page.route('**/__macro_dev/**', (route) => route.abort());
+ await page.goto(fixture, { waitUntil: 'domcontentloaded' });
+ await page.getByRole('button', { name: 'New pipeline', exact: true }).click();
+ const dialog = page.getByRole('dialog');
+ await dialog.getByRole('textbox', { name: 'Pipeline name' }).fill('Q4 Deals');
+ await dialog.getByRole('button', { name: 'Create pipeline' }).click();
+ await expect(dialog).not.toBeVisible();
+
+ await page.getByRole('button', { name: 'Pipeline actions' }).click();
+ const downloaded = page.waitForEvent('download');
+ await page.getByRole('menuitem', { name: 'Export CSV' }).click();
+ const download = await downloaded;
+ expect(download.suggestedFilename()).toMatch(
+ /^macro-q4-deals-\d{4}-\d{2}-\d{2}\.csv$/
+ );
+ const path = await download.path();
+ const { readFile } = await import('node:fs/promises');
+ expect((await readFile(path, 'utf8')).replace(/^\uFEFF/, '')).toBe(
+ 'Company,Domain,Stage\nacme.com,acme.com,Lead'
+ );
+
+ await page.getByRole('button', { name: 'Pipeline actions' }).click();
+ await page.getByRole('menuitem', { name: 'Import CSV' }).click();
+ const importDialog = page.getByRole('dialog');
+ await importDialog.getByLabel('Pipeline CSV').setInputFiles({
+ name: 'deals.csv',
+ mimeType: 'text/csv',
+ buffer: Buffer.from(
+ 'Company,Domain,Stage,Notes\nAcme,acme.com,Lead,\nBeta,https://beta.io,Won,hi\n'
+ ),
+ });
+ await expect(importDialog).toContainText('2 entries ready to import.');
+ await expect(importDialog).toContainText('Fills Stage.');
+ await expect(importDialog).toContainText(
+ "Skips columns this pipeline can't fill: Notes."
+ );
+ await importDialog
+ .getByRole('button', { name: 'Import', exact: true })
+ .click();
+ await expect(importDialog.getByRole('status')).toHaveText(
+ '1 entry added, 1 already in this pipeline.'
+ );
+ await expect(page.getByLabel('Pipeline entries')).toHaveText('2');
+});
diff --git a/apps/web/src/features/crm/browser-test/pipelines.tsx b/apps/web/src/features/crm/browser-test/pipelines.tsx
index e88e8d1fb1..54a2a6014d 100644
--- a/apps/web/src/features/crm/browser-test/pipelines.tsx
+++ b/apps/web/src/features/crm/browser-test/pipelines.tsx
@@ -7,8 +7,9 @@ import { DatabaseColumnHeader } from '../../database/components/database-column-
import type { DatabaseViewColumn } from '../../database/core/database-view';
import { PipelineDialog } from '../components/pipeline-dialog';
import { PipelineSidebar } from '../components/pipeline-sidebar';
-import type { PipelinesSource } from '../context/pipelines';
+import type { PipelinesSource, PipelineTransfer } from '../context/pipelines';
import type { Pipeline } from '../core/pipeline';
+import type { PipelineCsvRow } from '../core/pipeline-csv';
import { PipelineView } from '../views/pipeline';
// Real creation, navigation and column-header controls. Persistence is
@@ -68,12 +69,69 @@ function Editor(props: { pipeline: Pipeline }) {
</div>
);
}
+const transferColumns: DatabaseViewColumn[] = [
+ {
+ id: 'primary',
+ name: 'Company',
+ dataType: 'ENTITY',
+ specificEntityType: 'COMPANY',
+ writable: true,
+ isMultiSelect: false,
+ options: [],
+ },
+ {
+ id: 'stage',
+ name: 'Stage',
+ dataType: 'SELECT_STRING',
+ writable: true,
+ isMultiSelect: false,
+ options: [{ id: 'lead', label: 'Lead', color: null }],
+ },
+];
+
+/** In-memory entries: each pipeline starts with Acme, and records are named by domain. */
+function createTransfer(onInserted: (count: number) => void): PipelineTransfer {
+ const rows: PipelineCsvRow[] = [
+ { cells: { primary: 'acme.com-id', stage: 'Lead' } },
+ ];
+ return {
+ readTable: async () => ({ columns: transferColumns, rows: [...rows] }),
+ describeRecords: async (ids) =>
+ new Map(
+ ids.map((id) => [
+ id,
+ { name: id.replace(/-id$/, ''), key: id.replace(/-id$/, '') },
+ ])
+ ),
+ resolveRecords: async (records) =>
+ new Map(records.map((record) => [record.key, `${record.key}-id`])),
+ insertRows: async (added) => {
+ for (const cells of added) {
+ const [record] = cells;
+ if (record?.value.type === 'entities')
+ rows.push({ cells: { primary: record.value.value[0].entityId } });
+ }
+ onInserted(rows.length);
+ },
+ refresh: async () => {},
+ };
+}
+
+async function downloadCsv(content: string, filename: string) {
+ const link = document.createElement('a');
+ link.href = URL.createObjectURL(new Blob([content], { type: 'text/csv' }));
+ link.download = filename;
+ link.click();
+ return { saved: true };
+}
+
function Fixture() {
const [pipelines, setPipelines] = createSignal<Pipeline[]>([]);
const [selected, setSelected] = createSignal<string>();
const [creating, setCreating] = createSignal(false);
const [failNext, setFailNext] = createSignal(false);
const [calls, setCalls] = createSignal(0);
+ const [entries, setEntries] = createSignal(1);
const active = () =>
pipelines().find((pipeline) => pipeline.id === selected());
const source: PipelinesSource = {
@@ -117,6 +175,7 @@ function Fixture() {
<div class="flex gap-3 p-2">
<Button onClick={() => setFailNext(true)}>Fail next create</Button>
<output aria-label="Create requests">{calls()}</output>
+ <output aria-label="Pipeline entries">{entries()}</output>
</div>
<div class="flex min-h-0 flex-1">
<aside class="w-60 shrink-0 border-r border-edge-muted">
@@ -143,6 +202,10 @@ function Fixture() {
Sharing={() => null}
onCopyLink={() => {}}
onTrashed={() => setSelected(undefined)}
+ transfer={{
+ source: createTransfer(setEntries),
+ downloadCsv,
+ }}
/>
)}
</Show>
diff --git a/apps/web/src/features/crm/context/crm-context.tsx b/apps/web/src/features/crm/context/crm-context.tsx
index 590039e36d..fc93b6d102 100644
--- a/apps/web/src/features/crm/context/crm-context.tsx
+++ b/apps/web/src/features/crm/context/crm-context.tsx
@@ -9,8 +9,10 @@ import type {
CrmDisplayOptions,
CrmListColumnId,
} from '../core/display-options';
+import type { Pipeline } from '../core/pipeline';
import type { CrmRecordScope } from '../core/record';
import type {
+ CompanyImporter,
CompanySource,
ContactSource,
CrmCapabilities,
@@ -34,6 +36,7 @@ import type {
PipelineEntriesView,
PipelineSharing,
PipelinesSource,
+ PipelineTransfer,
} from './pipelines';
export type CrmContext = {
@@ -41,6 +44,7 @@ export type CrmContext = {
PipelineEditor: PipelineEditor;
PipelineEntries: PipelineEntriesView;
PipelineSharing: PipelineSharing;
+ createPipelineTransfer(pipeline: Pipeline): PipelineTransfer;
downloadCsv(content: string, filename: string): Promise<{ saved: boolean }>;
/** Profile picture lookup for a contact; presentation owns the fallback. */
createContactPhoto?(email: string): Accessor<string | undefined>;
@@ -134,6 +138,7 @@ export type CrmContext = {
{ name: string; domain: string },
{ id: string }
>;
+ createCompanyImporter(): CompanyImporter;
createContact(): CrmMutation<
{ companyId: string; name: string; email: string },
{ id: string }
diff --git a/apps/web/src/features/crm/context/crm-sources.ts b/apps/web/src/features/crm/context/crm-sources.ts
index 193474d0b5..75070a9f58 100644
--- a/apps/web/src/features/crm/context/crm-sources.ts
+++ b/apps/web/src/features/crm/context/crm-sources.ts
@@ -21,6 +21,16 @@ export type CrmQuery<T> = {
readonly isError: boolean;
refetch(): Promise<unknown>;
};
+/** Creates many companies, refreshing CRM listings once at the end. */
+export type CompanyImporter = {
+ /** `exists` when the team already tracks the domain. */
+ create(input: {
+ name: string;
+ domain: string;
+ }): Promise<'created' | 'exists'>;
+ refresh(): Promise<void>;
+};
+
export type CrmMutation<Input, Output = unknown> = {
readonly isPending: boolean;
mutate(
diff --git a/apps/web/src/features/crm/context/pipelines.ts b/apps/web/src/features/crm/context/pipelines.ts
index 58148a4781..11d3c20d1d 100644
--- a/apps/web/src/features/crm/context/pipelines.ts
+++ b/apps/web/src/features/crm/context/pipelines.ts
@@ -1,5 +1,8 @@
+import type { CellWrite } from '@core/database-sql/generated/types';
import type { Accessor, Component, JSX } from 'solid-js';
+import type { DatabaseViewColumn } from '../../database/core/database-view';
import type { NewPipeline, Pipeline } from '../core/pipeline';
+import type { PipelineCsvRecord, PipelineCsvRow } from '../core/pipeline-csv';
import type { PipelineEntryRecord } from '../core/pipeline-entry';
export type PipelinesSource = {
@@ -12,6 +15,44 @@ export type PipelinesSource = {
trash(id: string): Promise<void>;
};
+/** Reads and writes one pipeline's entries in bulk, for CSV import and export. */
+export type PipelineTransfer = {
+ /** The pipeline's columns and every one of its rows. */
+ readTable(signal: AbortSignal): Promise<{
+ columns: DatabaseViewColumn[];
+ rows: PipelineCsvRow[];
+ }>;
+ /** The name and domain or email of each record the ids name. */
+ describeRecords(
+ ids: readonly string[],
+ signal: AbortSignal
+ ): Promise<Map<string, PipelineCsvRecord>>;
+ /**
+ * Record ids by domain or email, creating the companies and contacts the
+ * team lacks. A record that cannot be found or created is left out.
+ */
+ resolveRecords(
+ records: readonly PipelineCsvRecord[],
+ signal: AbortSignal,
+ progress: (count: number) => void
+ ): Promise<Map<string, string>>;
+ /** Appends rows, first adding the select options they name that columns lack. */
+ insertRows(
+ rows: readonly CellWrite[][],
+ columns: readonly DatabaseViewColumn[]
+ ): Promise<void>;
+ /** Shows the new rows and records everywhere they appear. */
+ refresh(): Promise<void>;
+};
+
+/** Where a company import puts Stage, Owner, and Revenue: a new company pipeline. */
+export type CompanyImportPipelines = {
+ /** Company pipelines the viewer can edit, which the import can fill instead. */
+ existing: Accessor<Pipeline[]>;
+ create(name: string, sharing: Pipeline['sharing']): Promise<Pipeline>;
+ transfer(pipeline: Pipeline): PipelineTransfer;
+};
+
export type PipelineEditor = Component<{
pipeline: Pipeline;
/** Compact host actions placed alongside the editor's record controls. */
diff --git a/apps/web/src/features/crm/core/import-companies.ts b/apps/web/src/features/crm/core/import-companies.ts
index 16921d5143..5ae79abcd6 100644
--- a/apps/web/src/features/crm/core/import-companies.ts
+++ b/apps/web/src/features/crm/core/import-companies.ts
@@ -1,37 +1,89 @@
+import { err, ok, type Result } from 'neverthrow';
+import { match } from 'ts-pattern';
+
export type CompanyImportRow = { name: string; domain: string };
-export function normalizeCompanyImport(
- records: readonly Record<string, string>[]
-):
- | { rows: CompanyImportRow[]; error?: never }
- | { error: string; rows?: never } {
- const normalized = records.map((row) =>
- Object.fromEntries(
- Object.entries(row).map(([key, value]) => [
- key.toLowerCase(),
- value.trim(),
- ])
+
+/** Rows to create, and how many the file had without a new, usable domain. */
+export type CompanyImport = { rows: CompanyImportRow[]; skipped: number };
+
+export type CompanyImportFailure =
+ | { kind: 'no-domain-column' }
+ | { kind: 'no-companies' }
+ | { kind: 'too-many-rows' };
+
+const MAX_COMPANY_IMPORT_ROWS = 10_000;
+
+// Our own export writes `Company` and `Domains`.
+const NAME_HEADERS = ['name', 'company', 'company name'];
+const DOMAIN_HEADERS = ['domain', 'domains', 'website'];
+const DOMAIN = /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/;
+
+/** A CSV cell as typed, without the quote CSV exports put before formula characters. */
+export function importedText(value: string | undefined): string {
+ const text = (value ?? '').trim();
+ // Exports quote values that start with one after spaces too.
+ return /^'\s*[=+\-@]/.test(text) ? text.slice(1).trim() : text;
+}
+
+/** The bare domain of a domain, URL, or email address. */
+export function importedDomain(value: string): string | undefined {
+ const host = value
+ .trim()
+ .toLowerCase()
+ .replace(/^[a-z][a-z0-9+.-]*:\/\//, '')
+ .replace(/^[^@/]*@/, '')
+ .replace(/^www\./, '')
+ .split(/[/?#:]/)[0];
+ return host && DOMAIN.test(host) ? host : undefined;
+}
+
+export function companyImportMessage(failure: CompanyImportFailure): string {
+ return match(failure)
+ .with(
+ { kind: 'no-domain-column' },
+ () => 'Include a Domain column, with domains like acme.com.'
)
- );
- if (
- !normalized.length ||
- normalized.length > 100 ||
- normalized.some(
- (row) =>
- !row.name ||
- !/^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i.test(
- row.domain ?? ''
- )
+ .with(
+ { kind: 'no-companies' },
+ () => 'No rows have a domain like acme.com.'
)
- ) {
- return {
- error:
- 'Use name and domain columns, with 1–100 companies and domains like acme.com.',
- };
+ .with(
+ { kind: 'too-many-rows' },
+ () =>
+ `Import up to ${MAX_COMPANY_IMPORT_ROWS.toLocaleString('en-US')} companies at a time.`
+ )
+ .exhaustive();
+}
+
+/**
+ * Companies from CSV records keyed by header, including a CRM export. A row
+ * takes its first valid domain, and its name defaults to that domain.
+ */
+export function normalizeCompanyImport(
+ records: readonly Record<string, string>[]
+): Result<CompanyImport, CompanyImportFailure> {
+ if (records.length > MAX_COMPANY_IMPORT_ROWS)
+ return err({ kind: 'too-many-rows' });
+ const headers = Object.keys(records[0] ?? {});
+ const header = (names: string[]) =>
+ headers.find((key) => names.includes(key.trim().toLowerCase()));
+ const nameHeader = header(NAME_HEADERS);
+ const domainHeader = header(DOMAIN_HEADERS);
+ if (!domainHeader) return err({ kind: 'no-domain-column' });
+ const seen = new Set<string>();
+ const rows: CompanyImportRow[] = [];
+ for (const record of records) {
+ const domain = importedText(record[domainHeader])
+ .split(/[;,\s]+/)
+ .map(importedDomain)
+ .find((domain) => domain !== undefined);
+ if (!domain || seen.has(domain)) continue;
+ seen.add(domain);
+ rows.push({
+ name: (nameHeader && importedText(record[nameHeader])) || domain,
+ domain,
+ });
}
- return {
- rows: normalized.map((row) => ({
- name: row.name,
- domain: row.domain.toLowerCase(),
- })),
- };
+ if (!rows.length) return err({ kind: 'no-companies' });
+ return ok({ rows, skipped: records.length - rows.length });
}
diff --git a/apps/web/src/features/crm/core/pipeline-csv.test.ts b/apps/web/src/features/crm/core/pipeline-csv.test.ts
new file mode 100644
index 0000000000..4444f6114f
--- /dev/null
+++ b/apps/web/src/features/crm/core/pipeline-csv.test.ts
@@ -0,0 +1,177 @@
+import { describe, expect, it } from 'vitest';
+import { parseDatabaseCsv } from '../../database/core/csv';
+import type { DatabaseViewColumn } from '../../database/core/database-view';
+import { encodePipelineCsv, planPipelineImport } from './pipeline-csv';
+
+const columns: DatabaseViewColumn[] = [
+ {
+ id: 'company',
+ name: 'Company',
+ dataType: 'ENTITY',
+ specificEntityType: 'COMPANY',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'stage',
+ name: 'Stage',
+ dataType: 'SELECT_STRING',
+ isMultiSelect: false,
+ options: [{ id: 'lead', label: 'Lead', color: null }],
+ writable: true,
+ },
+ {
+ id: 'owner',
+ name: 'Owner',
+ dataType: 'ENTITY',
+ specificEntityType: 'USER',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'revenue',
+ name: 'Revenue',
+ dataType: 'NUMBER',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'close',
+ name: 'Close date',
+ dataType: 'DATE',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'tags',
+ name: 'Tags',
+ dataType: 'TAG',
+ isMultiSelect: true,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'doc',
+ name: 'Proposal',
+ dataType: 'ENTITY',
+ specificEntityType: 'DOCUMENT',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+];
+const pipeline = {
+ recordType: 'company' as const,
+ columns,
+ primaryColumnId: 'company',
+};
+
+describe('pipeline CSV', () => {
+ it('imports what it exports', () => {
+ const csv = encodePipelineCsv({
+ ...pipeline,
+ rows: [
+ {
+ cells: {
+ company: 'acme-id',
+ stage: 'Lead',
+ owner: 'macro|[email protected]',
+ revenue: 1250.5,
+ close: '2026-11-01T00:00:00Z',
+ tags: '["Enterprise","Inbound"]',
+ doc: 'doc-id',
+ },
+ },
+ { cells: { company: 'missing-id', stage: null } },
+ ],
+ record: (id) =>
+ id === 'acme-id' ? { name: ' =Acme', key: 'acme.com' } : undefined,
+ });
+ expect(csv.split('\n')[0]).toBe(
+ 'Company,Domain,Stage,Owner,Revenue,Close date,Tags,Proposal'
+ );
+ const plan = planPipelineImport(
+ parseDatabaseCsv(csv)._unsafeUnwrap(),
+ pipeline
+ )._unsafeUnwrap();
+ expect(plan.rows).toEqual([
+ {
+ record: { name: '=Acme', key: 'acme.com' },
+ cells: [
+ {
+ column: 'stage',
+ value: { type: 'options', value: [{ label: 'Lead' }] },
+ },
+ {
+ column: 'owner',
+ value: {
+ type: 'entities',
+ value: [{ entityType: 'USER', entityId: 'macro|[email protected]' }],
+ },
+ },
+ { column: 'revenue', value: { type: 'number', value: 1250.5 } },
+ {
+ column: 'close',
+ value: { type: 'date', value: '2026-11-01T00:00:00.000Z' },
+ },
+ {
+ column: 'tags',
+ value: {
+ type: 'options',
+ value: [{ label: 'Enterprise' }, { label: 'Inbound' }],
+ },
+ },
+ ],
+ },
+ ]);
+ expect(plan.skipped).toBe(1);
+ expect(plan.columns).toEqual([
+ 'Stage',
+ 'Owner',
+ 'Revenue',
+ 'Close date',
+ 'Tags',
+ ]);
+ expect(plan.ignored).toEqual(['Proposal']);
+ });
+
+ it('leaves out values that do not fit their column instead of the row', () => {
+ const plan = planPipelineImport(
+ parseDatabaseCsv(
+ 'name,website,revenue,owner,close date,notes\n' +
+ 'Acme,https://www.acme.com/about,lots,not-an-email,someday,hi\n' +
+ ',beta.io,10,,,\n'
+ )._unsafeUnwrap(),
+ pipeline
+ )._unsafeUnwrap();
+ expect(plan.rows).toEqual([
+ { record: { name: 'Acme', key: 'acme.com' }, cells: [] },
+ {
+ record: { name: 'beta.io', key: 'beta.io' },
+ cells: [{ column: 'revenue', value: { type: 'number', value: 10 } }],
+ },
+ ]);
+ expect(plan.invalidCells).toBe(3);
+ expect(plan.ignored).toEqual(['notes']);
+ });
+
+ it('needs the column that names each record', () => {
+ const csv = parseDatabaseCsv('Company,Stage\nAcme,Lead')._unsafeUnwrap();
+ expect(planPipelineImport(csv, pipeline)._unsafeUnwrapErr()).toEqual({
+ kind: 'no-record-column',
+ recordType: 'company',
+ });
+ const contacts = planPipelineImport(
+ parseDatabaseCsv('Contact,Email\nJane,[email protected]')._unsafeUnwrap(),
+ { ...pipeline, recordType: 'contact', primaryColumnId: 'contact' }
+ )._unsafeUnwrap();
+ expect(contacts.rows[0].record).toEqual({
+ name: 'Jane',
+ });
+ });
+});
diff --git a/apps/web/src/features/crm/core/pipeline-csv.ts b/apps/web/src/features/crm/core/pipeline-csv.ts
new file mode 100644
index 0000000000..c18f328145
--- /dev/null
+++ b/apps/web/src/features/crm/core/pipeline-csv.ts
@@ -0,0 +1,310 @@
+import type { CellWrite } from '@core/database-sql/generated/types';
+import { emailToMacroId, isMacroId, macroIdToEmail } from '@core/user/macroId';
+import { err, ok, type Result } from 'neverthrow';
+import { match } from 'ts-pattern';
+import { cellValue } from '../../database/core/cell-ops';
+import {
+ type DatabaseCsv,
+ encodeDatabaseCsv,
+ exportedValue,
+} from '../../database/core/csv';
+import type {
+ DatabaseCellValue,
+ DatabaseViewColumn,
+} from '../../database/core/database-view';
+import { importedDomain, importedText } from './import-companies';
+import type { Pipeline } from './pipeline';
+
+/** The company or contact a pipeline row is for: its name, and its domain or email. */
+export type PipelineCsvRecord = { name: string; key: string };
+
+export type PipelineCsvRow = {
+ cells: Record<string, DatabaseCellValue>;
+};
+
+/** A CSV row to add: the record it is for and the cells it starts with. */
+type PipelineImportRow = {
+ record: PipelineCsvRecord;
+ cells: CellWrite[];
+};
+
+export type PipelineImportPlan = {
+ rows: PipelineImportRow[];
+ /** Pipeline columns the CSV fills. */
+ columns: string[];
+ /** CSV headers that match no column the import can write. */
+ ignored: string[];
+ /** Rows without a usable domain or email. */
+ skipped: number;
+ /** Values left empty because they do not fit their column. */
+ invalidCells: number;
+};
+
+export type PipelineImportFailure =
+ | { kind: 'no-record-column'; recordType: Pipeline['recordType'] }
+ | { kind: 'no-rows' };
+
+/** The record columns of a pipeline CSV, before the pipeline's own columns. */
+export function pipelineRecordHeaders(
+ recordType: Pipeline['recordType']
+): [name: string, key: string] {
+ return recordType === 'company'
+ ? ['Company', 'Domain']
+ : ['Contact', 'Email'];
+}
+
+const NAME_HEADERS = {
+ company: ['company', 'name', 'company name'],
+ contact: ['contact', 'name', 'contact name'],
+};
+const KEY_HEADERS = {
+ company: ['domain', 'domains', 'website'],
+ contact: ['email', 'email address'],
+};
+const EMAIL = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;
+
+const isPersonColumn = (column: DatabaseViewColumn) =>
+ column.dataType === 'ENTITY' && column.specificEntityType === 'USER';
+
+/** A grid value's entries: one for a single value, the list for a multi-valued cell. */
+function entries(
+ column: DatabaseViewColumn,
+ value: DatabaseCellValue
+): string[] {
+ if (value === null || value === '') return [];
+ if (!column.isMultiSelect) return [String(value)];
+ try {
+ const list: unknown = JSON.parse(String(value));
+ return Array.isArray(list) ? list.map(String) : [String(value)];
+ } catch {
+ return [String(value)];
+ }
+}
+
+/** People leave as emails, which other teams can match; other cells as a table export writes them. */
+function exportedCell(column: DatabaseViewColumn, value: DatabaseCellValue) {
+ return isPersonColumn(column)
+ ? entries(column, value)
+ .map((id) => (isMacroId(id) ? macroIdToEmail(id) : id))
+ .join(', ')
+ : exportedValue(column, value);
+}
+
+/** A pipeline as CSV: the record's name and key, then every other column. */
+export function encodePipelineCsv(input: {
+ recordType: Pipeline['recordType'];
+ columns: readonly DatabaseViewColumn[];
+ primaryColumnId: string;
+ rows: readonly PipelineCsvRow[];
+ record(id: string): PipelineCsvRecord | undefined;
+}): string {
+ const columns = input.columns.filter(
+ (column) => column.id !== input.primaryColumnId
+ );
+ return encodeDatabaseCsv(
+ [
+ ...pipelineRecordHeaders(input.recordType),
+ ...columns.map((column) => column.name),
+ ],
+ input.rows.map((row) => {
+ const id = row.cells[input.primaryColumnId];
+ const record = typeof id === 'string' ? input.record(id) : undefined;
+ return [
+ record?.name ?? '',
+ record?.key ?? '',
+ ...columns.map((column) =>
+ exportedCell(column, row.cells[column.id] ?? null)
+ ),
+ ];
+ })
+ );
+}
+
+/**
+ * Columns an import fills: writable, stored, and holding values that carry
+ * across teams. References to records and other entities hold ids that a
+ * different workspace would not have.
+ */
+function importable(column: DatabaseViewColumn, primaryColumnId: string) {
+ return (
+ column.id !== primaryColumnId &&
+ column.writable &&
+ !column.formula &&
+ !column.relation &&
+ (column.dataType !== 'ENTITY' || isPersonColumn(column))
+ );
+}
+
+/** A date cell's stored instant, from a day or any date the browser reads. */
+function importedDate(text: string): string | undefined {
+ if (/^\d{4}-\d{2}-\d{2}$/.test(text)) return text;
+ const time = Date.parse(text);
+ return Number.isNaN(time) ? undefined : new Date(time).toISOString();
+}
+
+/**
+ * CSV text as the grid value `cellValue` writes, or undefined when it does
+ * not fit the column. The service refuses a whole batch over one bad value,
+ * so dates, links, and people are checked here.
+ */
+function gridValue(
+ column: DatabaseViewColumn,
+ text: string
+): DatabaseCellValue | undefined {
+ if (column.dataType === 'DATE') return importedDate(text);
+ if (column.dataType === 'LINK')
+ return /^https?:\/\/\S+$/i.test(text) ? text : undefined;
+ if (!column.isMultiSelect && !isPersonColumn(column)) return text;
+ const values = text
+ .split(',')
+ .map((value) => value.trim())
+ .filter(Boolean)
+ .map((value) =>
+ isPersonColumn(column) && !isMacroId(value)
+ ? emailToMacroId(value.toLowerCase())
+ : value
+ );
+ if (values.some((value) => value === undefined)) return undefined;
+ return column.isMultiSelect ? JSON.stringify(values) : (values[0] ?? '');
+}
+
+function recordKey(recordType: Pipeline['recordType'], text: string) {
+ if (recordType === 'company')
+ return text
+ .split(/[;,\s]+/)
+ .map(importedDomain)
+ .find((domain) => domain !== undefined);
+ const email = text.toLowerCase();
+ return EMAIL.test(email) ? email : undefined;
+}
+
+/**
+ * The rows a CSV adds to a pipeline: each names its company by domain or its
+ * contact by email, and fills the pipeline columns its other headers name.
+ * A value that does not fit its column is left out rather than failing the row.
+ */
+export function planPipelineImport(
+ csv: DatabaseCsv,
+ pipeline: {
+ recordType: Pipeline['recordType'];
+ columns: readonly DatabaseViewColumn[];
+ primaryColumnId: string;
+ }
+): Result<PipelineImportPlan, PipelineImportFailure> {
+ const { recordType, primaryColumnId } = pipeline;
+ const header = (names: string[]) =>
+ csv.columns.findIndex((name) => names.includes(name.trim().toLowerCase()));
+ const keyIndex = header(KEY_HEADERS[recordType]);
+ if (keyIndex < 0) return err({ kind: 'no-record-column', recordType });
+ const nameIndex = header(NAME_HEADERS[recordType]);
+ const targets = csv.columns.map((name, index) =>
+ index === keyIndex || index === nameIndex
+ ? undefined
+ : pipeline.columns.find(
+ (column) =>
+ importable(column, primaryColumnId) &&
+ column.name.trim().toLowerCase() === name.trim().toLowerCase()
+ )
+ );
+ let skipped = 0;
+ let invalidCells = 0;
+ const rows: PipelineImportRow[] = [];
+ for (const values of csv.rows) {
+ const key = recordKey(recordType, importedText(values[keyIndex]));
+ if (!key) {
+ skipped++;
+ continue;
+ }
+ const cells: CellWrite[] = [];
+ targets.forEach((column, index) => {
+ const text = importedText(values[index]);
+ if (!column || !text) return;
+ const value = gridValue(column, text);
+ if (value === undefined) {
+ invalidCells++;
+ return;
+ }
+ cellValue(column, value).match(
+ (written) => cells.push({ column: column.id, value: written }),
+ () => invalidCells++
+ );
+ });
+ rows.push({
+ record: {
+ name: (nameIndex >= 0 && importedText(values[nameIndex])) || key,
+ key,
+ },
+ cells,
+ });
+ }
+ if (!rows.length) return err({ kind: 'no-rows' });
+ return ok({
+ rows,
+ columns: targets.flatMap((column) => (column ? [column.name] : [])),
+ ignored: csv.columns.filter(
+ (_, index) => index !== keyIndex && index !== nameIndex && !targets[index]
+ ),
+ skipped,
+ invalidCells,
+ });
+}
+
+export function pipelineImportMessage(failure: PipelineImportFailure): string {
+ return match(failure)
+ .with(
+ { kind: 'no-record-column', recordType: 'company' },
+ () => 'Include a Domain column, with domains like acme.com.'
+ )
+ .with(
+ { kind: 'no-record-column', recordType: 'contact' },
+ () => 'Include an Email column, with addresses like [email protected].'
+ )
+ .with({ kind: 'no-rows' }, () => 'No rows name a company or contact.')
+ .exhaustive();
+}
+
+/** The cell naming the company or contact a new pipeline row is for. */
+export function pipelineRecordCell(
+ pipeline: Pick<Pipeline, 'recordType' | 'primaryColumnId'>,
+ id: string
+): CellWrite {
+ return {
+ column: pipeline.primaryColumnId,
+ value: {
+ type: 'entities',
+ value: [
+ {
+ entityType: pipeline.recordType === 'company' ? 'COMPANY' : 'CONTACT',
+ entityId: id,
+ },
+ ],
+ },
+ };
+}
+
+/** Company fields that pipeline rows hold now, rather than companies. */
+const COMPANY_PIPELINE_FIELDS = ['stage', 'owner', 'revenue'];
+
+/**
+ * The part of a companies CSV that belongs on pipeline rows: the rows with a
+ * Stage, Owner, or Revenue value, and which of those headers have one.
+ * Undefined when no row has any.
+ */
+export function companyPipelineCsv(
+ csv: DatabaseCsv
+): { csv: DatabaseCsv; fields: string[] } | undefined {
+ const fields = csv.columns.flatMap((name, index) =>
+ COMPANY_PIPELINE_FIELDS.includes(name.trim().toLowerCase()) &&
+ csv.rows.some((row) => importedText(row[index]))
+ ? [index]
+ : []
+ );
+ const rows = csv.rows.filter((row) =>
+ fields.some((index) => importedText(row[index]))
+ );
+ if (!rows.length) return undefined;
+ return {
+ csv: { columns: csv.columns, rows },
+ fields: fields.map((index) => csv.columns[index].trim()),
+ };
+}
diff --git a/apps/web/src/features/crm/core/run-concurrently.ts b/apps/web/src/features/crm/core/run-concurrently.ts
new file mode 100644
index 0000000000..d958a73797
--- /dev/null
+++ b/apps/web/src/features/crm/core/run-concurrently.ts
@@ -0,0 +1,14 @@
+/** Runs `task` on every item, `limit` at a time, starting them in order. */
+export async function runConcurrently<T>(
+ items: readonly T[],
+ limit: number,
+ task: (item: T) => Promise<void>
+): Promise<void> {
+ let next = 0;
+ const worker = async () => {
+ while (next < items.length) await task(items[next++]);
+ };
+ await Promise.all(
+ Array.from({ length: Math.min(limit, items.length) }, worker)
+ );
+}
diff --git a/apps/web/src/features/crm/crm-adapter.ts b/apps/web/src/features/crm/crm-adapter.ts
index a5e7d08791..6256a392cc 100644
--- a/apps/web/src/features/crm/crm-adapter.ts
+++ b/apps/web/src/features/crm/crm-adapter.ts
@@ -49,6 +49,7 @@ import {
createCrmPermissions,
} from './primitives/team-config';
import {
+ createCompanyImporter,
useCompanyQuery,
useCreateCompanyMutation,
useCreateContactMutation,
@@ -65,6 +66,7 @@ import {
} from './queries/contacts';
import { fetchCrmExportCompanies } from './queries/export';
import { useCrmLists } from './queries/lists';
+import { createPipelineTransfer } from './queries/pipeline-transfer';
import { createPipelinesSource } from './queries/pipelines';
import {
recordListFailure,
@@ -174,6 +176,8 @@ export function createAppCrmContext(): CrmContext {
storage: deps.storage,
}),
PipelineSharing: PipelineShare,
+ createPipelineTransfer: (pipeline) =>
+ createPipelineTransfer(deps, pipeline),
feedback: toast,
downloadCsv: downloadCrmCsv,
createContactPhoto: (email) =>
@@ -274,6 +278,7 @@ export function createAppCrmContext(): CrmContext {
createTeamViews: () => useTeamCrmViews(deps, userId, createSettings()),
createLists: (teamId) => useCrmLists(deps, teamId),
createCompany: (...args) => useCreateCompanyMutation(deps, ...args),
+ createCompanyImporter: () => createCompanyImporter(deps),
createContact: (...args) => useCreateContactMutation(deps, ...args),
renameCompany: (...args) => useSetCompanyNameMutation(deps, ...args),
renameContact: (...args) => useSetContactNameMutation(deps, ...args),
diff --git a/apps/web/src/features/crm/pipeline-tool-renderers.tsx b/apps/web/src/features/crm/pipeline-tool-renderers.tsx
index 51e608ac3f..cec1e75024 100644
--- a/apps/web/src/features/crm/pipeline-tool-renderers.tsx
+++ b/apps/web/src/features/crm/pipeline-tool-renderers.tsx
@@ -1,7 +1,9 @@
import { BaseTool } from '@core/component/AI/component/tool/BaseTool';
import { createToolRenderer } from '@core/component/AI/component/tool/ToolRenderer';
+import FileCsv from '@phosphor-icons/core/regular/file-csv.svg';
import Kanban from '@phosphor-icons/core/regular/kanban.svg';
import { queryClient } from '@queries/client';
+import { soupKeys } from '@queries/soup/keys';
import { Show } from 'solid-js';
import { pluralize } from './components/tool-companies';
import {
@@ -17,6 +19,18 @@ async function refreshPipelines() {
await queryClient.invalidateQueries({ queryKey: crmKeys.pipelines._def });
}
+/** Imported companies and entries show in listings and open pipelines now. */
+async function refreshImported() {
+ await Promise.all([
+ queryClient.invalidateQueries({ queryKey: crmKeys._def }),
+ queryClient.invalidateQueries({ queryKey: soupKeys._def }),
+ ]);
+}
+
+function Summary(props: { text: string }) {
+ return <p class="px-2 py-1 text-xs text-ink-muted">{props.text}</p>;
+}
+
function Status(props: { text?: string }) {
return (
<Show when={props.text}>
@@ -241,6 +255,86 @@ const editPipelineHandler = createToolRenderer({
),
});
+const importCompaniesCsvHandler = createToolRenderer({
+ name: 'ImportCompaniesCsv',
+ handleResponse: refreshImported,
+ render: (ctx) => (
+ <BaseTool
+ icon={FileCsv}
+ renderContext={ctx.renderContext}
+ type="call"
+ response={
+ ctx.response ? <Summary text={ctx.response.data.summary} /> : undefined
+ }
+ >
+ <Label
+ status={
+ ctx.response
+ ? `${pluralize(ctx.response.data.created, 'company', 'companies')} created`
+ : undefined
+ }
+ >
+ {ctx.response
+ ? 'Imported companies from CSV'
+ : 'Import companies from CSV'}
+ </Label>
+ </BaseTool>
+ ),
+});
+
+const importPipelineCsvHandler = createToolRenderer({
+ name: 'ImportPipelineCsv',
+ handleResponse: refreshImported,
+ render: (ctx) => (
+ <BaseTool
+ icon={FileCsv}
+ renderContext={ctx.renderContext}
+ type="call"
+ response={
+ ctx.response ? <Summary text={ctx.response.data.summary} /> : undefined
+ }
+ >
+ <Label
+ status={
+ ctx.response
+ ? `${pluralize(ctx.response.data.added, 'entry', 'entries')} added`
+ : undefined
+ }
+ >
+ {ctx.response
+ ? `Imported CSV into ${ctx.response.data.pipelineName}`
+ : 'Import CSV into pipeline'}
+ </Label>
+ </BaseTool>
+ ),
+});
+
+const exportPipelineCsvHandler = createToolRenderer({
+ name: 'ExportPipelineCsv',
+ render: (ctx) => (
+ <BaseTool
+ icon={FileCsv}
+ renderContext={ctx.renderContext}
+ type="call"
+ response={
+ ctx.response ? <Summary text={ctx.response.data.summary} /> : undefined
+ }
+ >
+ <Label
+ status={
+ ctx.response
+ ? pluralize(ctx.response.data.rows, 'entry', 'entries')
+ : undefined
+ }
+ >
+ {ctx.response
+ ? `Exported ${ctx.response.data.fileName} to CSV`
+ : 'Export pipeline to CSV'}
+ </Label>
+ </BaseTool>
+ ),
+});
+
export const pipelineToolHandlers = {
ListPipelines: listPipelinesHandler,
FindPipelineEntries: findPipelineEntriesHandler,
@@ -249,4 +343,7 @@ export const pipelineToolHandlers = {
RemoveFromPipeline: removeFromPipelineHandler,
CreatePipeline: createPipelineHandler,
EditPipeline: editPipelineHandler,
+ ImportCompaniesCsv: importCompaniesCsvHandler,
+ ImportPipelineCsv: importPipelineCsvHandler,
+ ExportPipelineCsv: exportPipelineCsvHandler,
};
diff --git a/apps/web/src/features/crm/primitives/import-companies.test.ts b/apps/web/src/features/crm/primitives/import-companies.test.ts
index a134ca27c1..79eab341eb 100644
--- a/apps/web/src/features/crm/primitives/import-companies.test.ts
+++ b/apps/web/src/features/crm/primitives/import-companies.test.ts
@@ -1,19 +1,98 @@
+import type { CellWrite } from '@core/database-sql/generated/types';
import { createRoot } from 'solid-js';
import { describe, expect, it, vi } from 'vitest';
+import type { DatabaseViewColumn } from '../../database/core/database-view';
+import type { PipelineTransfer } from '../context/pipelines';
+import { createCrmCsv } from '../core/export';
+import type { Pipeline } from '../core/pipeline';
import { createCompanyImport } from './import-companies';
-const file = (text: string) => ({ size: text.length, text: async () => text });
+const file = (text: string, name = 'companies.csv') => ({
+ name,
+ size: text.length,
+ text: async () => text,
+});
+
+function setup(
+ create: (row: {
+ name: string;
+ domain: string;
+ }) => Promise<'created' | 'exists'>
+) {
+ const refresh = vi.fn(async () => {});
+ const { state, dispose } = createRoot((dispose) => ({
+ state: createCompanyImport({ create, refresh }),
+ dispose,
+ }));
+ return { state, dispose, refresh };
+}
describe('company import', () => {
+ it('imports a CRM companies export', async () => {
+ const create = vi.fn(async () => 'created' as const);
+ const { state, dispose, refresh } = setup(create);
+ const companies = Array.from({ length: 150 }, (_, index) => ({
+ name: `Company ${index}`,
+ domains: `company${index}.com; alias${index}.com`,
+ stage: 'Churned',
+ }));
+ await state.read(
+ file(
+ createCrmCsv(
+ [
+ { id: 'name', label: 'Company' },
+ { id: 'domains', label: 'Domains' },
+ { id: 'stage', label: 'Stage' },
+ ],
+ [
+ ...companies,
+ { name: '=Formula Co', domains: 'formula.co', stage: '' },
+ { name: 'No domain', domains: '', stage: '' },
+ { name: 'Repeat', domains: 'company0.com', stage: '' },
+ ]
+ )
+ )
+ );
+ expect(state.error()).toBe('');
+ expect(state.rows()).toHaveLength(151);
+ expect(state.rows()[0]).toEqual({
+ name: 'Company 0',
+ domain: 'company0.com',
+ });
+ expect(state.rows()[150]).toEqual({
+ name: '=Formula Co',
+ domain: 'formula.co',
+ });
+ expect(state.skipped()).toBe(2);
+ await state.run();
+ expect(create).toHaveBeenCalledTimes(151);
+ expect(refresh).toHaveBeenCalledTimes(1);
+ expect(state.completed()).toBe(151);
+ expect(state.rows()).toEqual([]);
+ dispose();
+ });
+
+ it('counts companies the team already tracks without failing them', async () => {
+ const { state, dispose } = setup(async (row) =>
+ row.domain === 'known.example' ? 'exists' : 'created'
+ );
+ await state.read(
+ file('name,domain\nKnown,known.example\nNew,https://www.new.example/')
+ );
+ await state.run();
+ expect(state.completed()).toBe(1);
+ expect(state.existing()).toBe(1);
+ expect(state.error()).toBe('');
+ dispose();
+ });
+
it('retries only failed rows and preserves the successful count', async () => {
let fail = true;
const create = vi.fn(async (row: { name: string; domain: string }) => {
if (row.name === 'Retry' && fail) throw new Error('temporary failure');
+ return 'created' as const;
});
- const { state, dispose } = createRoot((dispose) => ({
- state: createCompanyImport(create),
- dispose,
- }));
+ const { state, dispose } = setup(create);
await state.read(
file('Name,Domain\nDone,DONE.EXAMPLE\nRetry,retry.example')
);
@@ -33,18 +112,261 @@ describe('company import', () => {
]);
dispose();
});
- it('rejects oversized and invalid files before any mutation', async () => {
- const create = vi.fn();
+
+ it('rejects oversized files and files without domains before any mutation', async () => {
+ const create = vi.fn(async () => 'created' as const);
+ const { state, dispose } = setup(create);
+ await state.read(file('x'.repeat(5 * 1024 * 1024 + 1)));
+ expect(state.error()).toContain('smaller than 5 MB');
+ await state.read(file('name,website\nInvalid,not a domain'));
+ expect(state.error()).toBe('No rows have a domain like acme.com.');
+ await state.read(file('name,stage\nAcme,Lead'));
+ expect(state.error()).toContain('Include a Domain column');
+ await state.run();
+ expect(create).not.toHaveBeenCalled();
+ expect(state.rows()).toEqual([]);
+ dispose();
+ });
+
+ it('puts Stage and Owner on rows of a new pipeline, once', async () => {
+ const created: Pipeline[] = [];
+ const inserted: CellWrite[][] = [];
+ let failInsert = true;
+ const columns: DatabaseViewColumn[] = [
+ {
+ id: 'company',
+ name: 'Company',
+ dataType: 'ENTITY',
+ specificEntityType: 'COMPANY',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'stage',
+ name: 'Stage',
+ dataType: 'SELECT_STRING',
+ isMultiSelect: false,
+ options: [{ id: 'lead', label: 'Lead', color: null }],
+ writable: true,
+ },
+ {
+ id: 'owner',
+ name: 'Owner',
+ dataType: 'ENTITY',
+ specificEntityType: 'USER',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ ];
+ const transfer: PipelineTransfer = {
+ readTable: async () => ({
+ columns,
+ rows: inserted.map((cells) => ({
+ cells: {
+ company:
+ cells[0]?.value.type === 'entities'
+ ? cells[0].value.value[0].entityId
+ : null,
+ },
+ })),
+ }),
+ describeRecords: async () => new Map(),
+ resolveRecords: async (records) =>
+ new Map(records.map((record) => [record.key, `${record.key}-id`])),
+ insertRows: async (rows) => {
+ if (failInsert) throw new Error('offline');
+ inserted.push(...rows);
+ },
+ refresh: async () => {},
+ };
+ const create = vi.fn(async () => 'created' as const);
const { state, dispose } = createRoot((dispose) => ({
- state: createCompanyImport(create),
+ state: createCompanyImport(
+ { create, refresh: async () => {} },
+ {
+ existing: () => [],
+ create: async (name, sharing) => {
+ const pipeline: Pipeline = {
+ id: 'pipeline',
+ teamId: 'team',
+ name,
+ userId: 'macro|[email protected]',
+ recordType: 'company',
+ databaseId: 'database',
+ tableId: 'table',
+ primaryColumnId: 'company',
+ sharing,
+ grant: 'owner',
+ createdAt: '2026-10-16T00:00:00Z',
+ trashedAt: null,
+ };
+ created.push(pipeline);
+ return pipeline;
+ },
+ transfer: () => transfer,
+ }
+ ),
dispose,
}));
- await state.read(file('x'.repeat(1024 * 1024 + 1)));
- expect(state.error()).toContain('smaller than 1 MB');
- await state.read(file('name,domain\nInvalid,https://example.com'));
+ await state.read(
+ file(
+ createCrmCsv(
+ [
+ { id: 'name', label: 'Company' },
+ { id: 'domains', label: 'Domains' },
+ { id: 'stage', label: 'Stage' },
+ { id: 'owner', label: 'Owner' },
+ ],
+ [
+ { name: 'Acme', domains: 'acme.com', stage: 'Churned', owner: '' },
+ { name: 'Beta', domains: 'beta.io', stage: '', owner: '' },
+ {
+ name: 'Gamma',
+ domains: 'gamma.dev',
+ stage: '',
+ owner: '[email protected]',
+ },
+ ]
+ ),
+ 'Macro Customers Oct 16 2026.csv'
+ )
+ );
+ expect(state.pipelineData()?.fields).toEqual(['Stage', 'Owner']);
+ expect(state.pipelineData()?.csv.rows).toHaveLength(2);
await state.run();
- expect(create).not.toHaveBeenCalled();
- expect(state.rows()).toEqual([]);
+ expect(state.completed()).toBe(3);
+ expect(state.error()).toContain('Could not add Stage, Owner');
+ expect(state.runnable()).toBe(true);
+ failInsert = false;
+ await state.run();
+ expect(state.error()).toBe('');
+ expect(create).toHaveBeenCalledTimes(3);
+ expect(
+ created.map((pipeline) => [pipeline.name, pipeline.sharing])
+ ).toEqual([['Macro Customers Oct 16 2026', 'private']]);
+ expect(state.pipeline()?.added).toBe(2);
+ expect(inserted).toEqual([
+ [
+ {
+ column: 'company',
+ value: {
+ type: 'entities',
+ value: [{ entityType: 'COMPANY', entityId: 'acme.com-id' }],
+ },
+ },
+ {
+ column: 'stage',
+ value: { type: 'options', value: [{ label: 'Churned' }] },
+ },
+ ],
+ [
+ {
+ column: 'company',
+ value: {
+ type: 'entities',
+ value: [{ entityType: 'COMPANY', entityId: 'gamma.dev-id' }],
+ },
+ },
+ {
+ column: 'owner',
+ value: {
+ type: 'entities',
+ value: [{ entityType: 'USER', entityId: 'macro|[email protected]' }],
+ },
+ },
+ ],
+ ]);
+ expect(state.runnable()).toBe(false);
+ dispose();
+ });
+
+ it('imports companies without a pipeline when the file has no pipeline fields', async () => {
+ const createPipeline = vi.fn();
+ const { state, dispose } = createRoot((dispose) => ({
+ state: createCompanyImport(
+ { create: async () => 'created', refresh: async () => {} },
+ { existing: () => [], create: createPipeline, transfer: vi.fn() }
+ ),
+ dispose,
+ }));
+ await state.read(file('Company,Domains,Stage\nAcme,acme.com,\n'));
+ expect(state.pipelineData()).toBeUndefined();
+ await state.run();
+ expect(createPipeline).not.toHaveBeenCalled();
+ expect(state.completed()).toBe(1);
+ dispose();
+ });
+
+ it('fills an existing pipeline the viewer picks instead of making one', async () => {
+ const deals: Pipeline = {
+ id: 'deals',
+ teamId: 'team',
+ name: 'Deals',
+ userId: 'macro|[email protected]',
+ recordType: 'company',
+ databaseId: 'database',
+ tableId: 'table',
+ primaryColumnId: 'company',
+ sharing: 'team',
+ grant: 'edit',
+ createdAt: '2026-10-01T00:00:00Z',
+ trashedAt: null,
+ };
+ const inserted: CellWrite[][] = [];
+ const transferFor = vi.fn(
+ (): PipelineTransfer => ({
+ readTable: async () => ({
+ columns: [
+ {
+ id: 'company',
+ name: 'Company',
+ dataType: 'ENTITY',
+ specificEntityType: 'COMPANY',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'stage',
+ name: 'Stage',
+ dataType: 'SELECT_STRING',
+ isMultiSelect: false,
+ options: [{ id: 'lead', label: 'Lead', color: null }],
+ writable: true,
+ },
+ ],
+ rows: [],
+ }),
+ describeRecords: async () => new Map(),
+ resolveRecords: async (records) =>
+ new Map(records.map((record) => [record.key, `${record.key}-id`])),
+ insertRows: async (rows) => {
+ inserted.push(...rows);
+ },
+ refresh: async () => {},
+ })
+ );
+ const createPipeline = vi.fn();
+ const { state, dispose } = createRoot((dispose) => ({
+ state: createCompanyImport(
+ { create: async () => 'created', refresh: async () => {} },
+ {
+ existing: () => [deals],
+ create: createPipeline,
+ transfer: transferFor,
+ }
+ ),
+ dispose,
+ }));
+ await state.read(file('Company,Domains,Stage\nAcme,acme.com,Lead\n'));
+ state.setDestination({ kind: 'existing', id: 'deals' });
+ await state.run();
+ expect(createPipeline).not.toHaveBeenCalled();
+ expect(transferFor).toHaveBeenCalledWith(deals);
+ expect(state.pipeline()?.pipeline.id).toBe('deals');
+ expect(inserted).toHaveLength(1);
dispose();
});
});
diff --git a/apps/web/src/features/crm/primitives/import-companies.ts b/apps/web/src/features/crm/primitives/import-companies.ts
index cceba8aa7f..7f667c8f32 100644
--- a/apps/web/src/features/crm/primitives/import-companies.ts
+++ b/apps/web/src/features/crm/primitives/import-companies.ts
@@ -1,20 +1,110 @@
import { parseCsv } from '@core/util/csv';
-import { createSignal } from 'solid-js';
-import { normalizeCompanyImport } from '../core/import-companies';
+import { type Span, Telemetry } from '@macro-inc/observability';
+import { createSignal, onCleanup } from 'solid-js';
+import type { DatabaseCsv } from '../../database/core/csv';
+import type { CompanyImporter } from '../context/crm-sources';
+import type { CompanyImportPipelines } from '../context/pipelines';
+import {
+ type CompanyImportRow,
+ companyImportMessage,
+ normalizeCompanyImport,
+} from '../core/import-companies';
+import type { Pipeline } from '../core/pipeline';
+import { companyPipelineCsv, planPipelineImport } from '../core/pipeline-csv';
+import { runConcurrently } from '../core/run-concurrently';
+import {
+ addPipelineRows,
+ type PipelineImportResult,
+} from './pipeline-transfer';
+
+const MAX_IMPORT_BYTES = 5 * 1024 * 1024;
+/** Concurrent creates: fast enough for thousands of rows, gentle on the service. */
+const IMPORT_CONCURRENCY = 4;
+const MAX_PIPELINE_NAME = 200;
+
+/** Where the import puts Stage, Owner, and Revenue. */
+export type ImportDestination =
+ | { kind: 'new'; sharing: Pipeline['sharing'] }
+ | { kind: 'existing'; id: string };
+
+/** A pipeline named after the imported file. */
+function importedPipelineName(fileName: string) {
+ return (
+ fileName
+ .replace(/\.csv$/i, '')
+ .trim()
+ .slice(0, MAX_PIPELINE_NAME) || 'Imported companies'
+ );
+}
+
+/**
+ * Companies from a CSV. Stage, Owner, and Revenue now live on pipeline rows,
+ * so when the file has them they go into a pipeline the viewer picks: a new
+ * one, private or shared with the team, or an existing company pipeline, one
+ * row per
+ * company that has a value. Running again reuses that pipeline and adds only
+ * the rows it lacks.
+ */
export function createCompanyImport(
- createCompany: (input: { name: string; domain: string }) => Promise<unknown>
+ importer: CompanyImporter,
+ pipelines?: CompanyImportPipelines
) {
- const [rows, setRows] = createSignal<{ name: string; domain: string }[]>([]);
+ const [rows, setRows] = createSignal<CompanyImportRow[]>([]);
+ const [skipped, setSkipped] = createSignal(0);
const [error, setError] = createSignal('');
const [pending, setPending] = createSignal(false);
const [completed, setCompleted] = createSignal(0);
- async function read(file?: Pick<File, 'size' | 'text'>) {
+ const [existing, setExisting] = createSignal(0);
+ const [pipelineData, setPipelineData] = createSignal<{
+ csv: DatabaseCsv;
+ fields: string[];
+ name: string;
+ }>();
+ const [pipeline, setPipeline] = createSignal<
+ { pipeline: Pipeline } & PipelineImportResult
+ >();
+ const [destination, setDestinationSignal] = createSignal<ImportDestination>({
+ kind: 'new',
+ sharing: 'private',
+ });
+ let target: Pipeline | undefined;
+ /** A new choice applies to the next run, so a pipeline made earlier is forgotten. */
+ const setDestination = (next: ImportDestination) => {
+ target = undefined;
+ setPipeline(undefined);
+ setDestinationSignal(next);
+ };
+ const targetPipeline = async (span: Span, name: string) => {
+ const chosen = destination();
+ if (chosen.kind === 'existing') {
+ const existing = pipelines
+ ?.existing()
+ .find((pipeline) => pipeline.id === chosen.id);
+ if (!existing) throw new Error('The chosen pipeline is unavailable.');
+ return existing;
+ }
+ return span.span('crm.companies.import.create_pipeline', async () => {
+ if (!pipelines) throw new Error('Pipelines are unavailable.');
+ return pipelines.create(name, chosen.sharing);
+ });
+ };
+ let controller: AbortController | undefined;
+ onCleanup(() => controller?.abort());
+ /** Whether running again has anything left to do. */
+ const runnable = () =>
+ rows().length > 0 || (!!pipelines && !!pipelineData() && !pipeline());
+ async function read(file?: Pick<File, 'name' | 'size' | 'text'>) {
setError('');
setRows([]);
+ setSkipped(0);
setCompleted(0);
+ setExisting(0);
+ setPipelineData(undefined);
+ setPipeline(undefined);
+ target = undefined;
if (!file) return;
- if (file.size > 1024 * 1024) {
- setError('Choose a CSV smaller than 1 MB.');
+ if (file.size > MAX_IMPORT_BYTES) {
+ setError('Choose a CSV smaller than 5 MB.');
return;
}
try {
@@ -24,34 +114,131 @@ export function createCompanyImport(
return;
}
const normalized = normalizeCompanyImport(parsed.records);
- if (normalized.error !== undefined) {
- setError(normalized.error);
+ if (normalized.isErr()) {
+ setError(companyImportMessage(normalized.error));
return;
}
- setRows(normalized.rows);
+ setRows(normalized.value.rows);
+ setSkipped(normalized.value.skipped);
+ const columns = [...parsed.headers];
+ const fields = companyPipelineCsv({
+ columns,
+ rows: parsed.records.map((record) =>
+ columns.map((column) => record[column] ?? '')
+ ),
+ });
+ if (fields)
+ setPipelineData({ ...fields, name: importedPipelineName(file.name) });
} catch {
setError('Could not read that file. Please try again.');
}
}
- async function run() {
- if (pending() || !rows().length) return;
- setPending(true);
- setError('');
- const failed: { name: string; domain: string }[] = [];
- for (const row of rows()) {
+ async function importRow(row: CompanyImportRow) {
+ try {
+ const outcome = await importer.create(row);
+ if (outcome === 'exists') setExisting((n) => n + 1);
+ else setCompleted((n) => n + 1);
+ return true;
+ } catch {
+ return false;
+ }
+ }
+ /** Stage, Owner, and Revenue as rows of the import's pipeline. */
+ async function importPipeline(span: Span, signal: AbortSignal) {
+ const data = pipelineData();
+ if (!pipelines || !data) return true;
+ try {
+ target ??= await targetPipeline(span, data.name);
+ span.setAttr('crm.pipeline_id', target.id);
+ const transfer = pipelines.transfer(target);
+ const table = await span.span('crm.pipeline.read_table', () =>
+ transfer.readTable(signal)
+ );
+ const plan = planPipelineImport(data.csv, {
+ recordType: target.recordType,
+ columns: table.columns,
+ primaryColumnId: target.primaryColumnId,
+ });
+ // The companies import already found a domain column and rows.
+ if (plan.isErr()) return false;
+ const result = await addPipelineRows({
+ span,
+ pipeline: target,
+ transfer,
+ plan: plan.value,
+ table,
+ signal,
+ progress: () => {},
+ });
+ span.setAttr('crm.import.pipeline_rows', result.added);
+ setPipeline({ pipeline: target, ...result });
try {
- await createCompany(row);
- setCompleted((n) => n + 1);
+ await transfer.refresh();
} catch {
- failed.push(row);
+ // The rows are saved; the pipeline's own polling shows them.
}
+ return true;
+ } catch (cause) {
+ span.error(cause);
+ return false;
}
- setRows(failed);
+ }
+ async function run() {
+ if (pending() || !runnable()) return;
+ setPending(true);
+ setError('');
+ controller = new AbortController();
+ const signal = controller.signal;
+ const failed = new Set<CompanyImportRow>();
+ const pipelineSaved = await Telemetry.span(
+ 'crm.companies.import',
+ async (span) => {
+ span.setAttr('crm.import.rows', rows().length);
+ await span.span('crm.companies.import.create', () =>
+ runConcurrently(rows(), IMPORT_CONCURRENCY, async (row) => {
+ if (!(await importRow(row))) failed.add(row);
+ })
+ );
+ span.setAttr('crm.import.created', completed());
+ span.setAttr('crm.import.existing', existing());
+ span.setAttr('crm.import.failed', failed.size);
+ const saved = await importPipeline(span, signal);
+ try {
+ await importer.refresh();
+ } catch (cause) {
+ // The companies exist; listings catch up on their next read.
+ span.event('refresh_failed', {
+ error: cause instanceof Error ? cause.message : String(cause),
+ });
+ }
+ return saved;
+ }
+ );
+ setRows((all) => all.filter((row) => failed.has(row)));
setPending(false);
- if (failed.length)
- setError(
- `${failed.length} could not be imported. They may already exist or use an unsupported domain. Successful rows will not be retried.`
- );
+ const messages = [
+ failed.size
+ ? `${failed.size} could not be imported. They may use an unsupported domain, such as gmail.com. Successful rows will not be retried.`
+ : '',
+ pipelineSaved
+ ? ''
+ : `Could not add ${pipelineData()?.fields.join(', ')} to the pipeline. Import again to retry.`,
+ ];
+ setError(messages.filter(Boolean).join(' '));
}
- return { rows, error, pending, completed, read, run };
+ return {
+ rows,
+ skipped,
+ error,
+ pending,
+ completed,
+ existing,
+ pipelineData,
+ pipeline,
+ destination,
+ setDestination,
+ runnable,
+ read,
+ run,
+ };
}
diff --git a/apps/web/src/features/crm/primitives/pipeline-transfer.test.ts b/apps/web/src/features/crm/primitives/pipeline-transfer.test.ts
new file mode 100644
index 0000000000..9d77386c80
--- /dev/null
+++ b/apps/web/src/features/crm/primitives/pipeline-transfer.test.ts
@@ -0,0 +1,151 @@
+import type { CellWrite } from '@core/database-sql/generated/types';
+import { createRoot } from 'solid-js';
+import { describe, expect, it, vi } from 'vitest';
+import type { DatabaseViewColumn } from '../../database/core/database-view';
+import type { PipelineTransfer } from '../context/pipelines';
+import type { Pipeline } from '../core/pipeline';
+import type { PipelineCsvRow } from '../core/pipeline-csv';
+import {
+ createPipelineExport,
+ createPipelineImport,
+} from './pipeline-transfer';
+
+const pipeline: Pipeline = {
+ id: 'pipeline',
+ teamId: 'team',
+ name: 'Q4 Deals',
+ userId: 'macro|[email protected]',
+ recordType: 'company',
+ databaseId: 'database',
+ tableId: 'table',
+ primaryColumnId: 'company',
+ sharing: 'team',
+ grant: 'owner',
+ createdAt: '2026-10-01T00:00:00Z',
+ trashedAt: null,
+};
+const columns: DatabaseViewColumn[] = [
+ {
+ id: 'company',
+ name: 'Company',
+ dataType: 'ENTITY',
+ specificEntityType: 'COMPANY',
+ isMultiSelect: false,
+ options: [],
+ writable: true,
+ },
+ {
+ id: 'stage',
+ name: 'Stage',
+ dataType: 'SELECT_STRING',
+ isMultiSelect: false,
+ options: [{ id: 'lead', label: 'Lead', color: null }],
+ writable: true,
+ },
+];
+const file = (text: string) => ({ size: text.length, text: async () => text });
+
+function fakeTransfer(rows: PipelineCsvRow[]) {
+ const inserted: CellWrite[][] = [];
+ let failInsert = false;
+ const transfer = {
+ readTable: vi.fn(async () => ({ columns, rows: [...rows] })),
+ describeRecords: vi.fn(
+ async () => new Map([['acme-id', { name: 'Acme', key: 'acme.com' }]])
+ ),
+ resolveRecords: vi.fn(
+ async () =>
+ new Map([
+ ['acme.com', 'acme-id'],
+ ['beta.io', 'beta-id'],
+ ])
+ ),
+ insertRows: vi.fn(async (added: readonly CellWrite[][]) => {
+ if (failInsert) throw new Error('offline');
+ inserted.push(...added);
+ }),
+ refresh: vi.fn(async () => {}),
+ } satisfies PipelineTransfer;
+ return {
+ transfer,
+ inserted,
+ failNextInsert: (fail: boolean) => {
+ failInsert = fail;
+ },
+ };
+}
+
+describe('pipeline import', () => {
+ it('adds records the pipeline lacks and retries only what is missing', async () => {
+ const { transfer, inserted, failNextInsert } = fakeTransfer([
+ { cells: { company: 'acme-id', stage: 'Lead' } },
+ ]);
+ const { state, dispose } = createRoot((dispose) => ({
+ state: createPipelineImport(pipeline, transfer),
+ dispose,
+ }));
+ await state.read(
+ file(
+ '\uFEFFCompany,Domain,Stage\nAcme,acme.com,Lead\nBeta,beta.io,Won\nGmail,gmail.com,Lead\n'
+ )
+ );
+ expect(state.error()).toBe('');
+ expect(state.plan()?.rows).toHaveLength(3);
+ failNextInsert(true);
+ await state.run();
+ expect(state.error()).toContain('Retry');
+ expect(state.plan()?.rows).toHaveLength(3);
+ failNextInsert(false);
+ await state.run();
+ expect(state.result()).toEqual({ added: 1, present: 1, unresolved: 1 });
+ expect(inserted).toEqual([
+ [
+ {
+ column: 'company',
+ value: {
+ type: 'entities',
+ value: [{ entityType: 'COMPANY', entityId: 'beta-id' }],
+ },
+ },
+ {
+ column: 'stage',
+ value: { type: 'options', value: [{ label: 'Won' }] },
+ },
+ ],
+ ]);
+ expect(transfer.refresh).toHaveBeenCalledTimes(2);
+ dispose();
+ });
+
+ it('exports each entry with its company name and domain', async () => {
+ const { transfer } = fakeTransfer([
+ { cells: { company: 'acme-id', stage: 'Lead' } },
+ ]);
+ const download = vi.fn(async () => ({ saved: true }));
+ const { state, dispose } = createRoot((dispose) => ({
+ state: createPipelineExport(() => pipeline, transfer, download),
+ dispose,
+ }));
+ expect(await state.run()).toBe('saved');
+ expect(download).toHaveBeenCalledWith(
+ '\uFEFFCompany,Domain,Stage\nAcme,acme.com,Lead',
+ expect.stringMatching(/^macro-q4-deals-\d{4}-\d{2}-\d{2}\.csv$/)
+ );
+ dispose();
+ });
+
+ it('keeps two deals for one new company as two entries', async () => {
+ const { transfer, inserted } = fakeTransfer([]);
+ const { state, dispose } = createRoot((dispose) => ({
+ state: createPipelineImport(pipeline, transfer),
+ dispose,
+ }));
+ await state.read(
+ file('Company,Domain,Stage\nBeta,beta.io,Lead\nBeta,beta.io,Won\n')
+ );
+ await state.run();
+ expect(state.result()).toEqual({ added: 2, present: 0, unresolved: 0 });
+ expect(inserted).toHaveLength(2);
+ dispose();
+ });
+});
diff --git a/apps/web/src/features/crm/primitives/pipeline-transfer.ts b/apps/web/src/features/crm/primitives/pipeline-transfer.ts
new file mode 100644
index 0000000000..9e4f2c91be
--- /dev/null
+++ b/apps/web/src/features/crm/primitives/pipeline-transfer.ts
@@ -0,0 +1,232 @@
+import type { CellWrite } from '@core/database-sql/generated/types';
+import { type Span, Telemetry } from '@macro-inc/observability';
+import { type Accessor, createSignal, onCleanup } from 'solid-js';
+import {
+ databaseCsvMessage,
+ MAX_CSV_BYTES,
+ parseDatabaseCsv,
+} from '../../database/core/csv';
+import type { DatabaseViewColumn } from '../../database/core/database-view';
+import type { PipelineTransfer } from '../context/pipelines';
+import type { Pipeline } from '../core/pipeline';
+import {
+ encodePipelineCsv,
+ type PipelineCsvRow,
+ type PipelineImportPlan,
+ pipelineImportMessage,
+ pipelineRecordCell,
+ planPipelineImport,
+} from '../core/pipeline-csv';
+
+type PipelineTable = { columns: DatabaseViewColumn[]; rows: PipelineCsvRow[] };
+
+/** Ids of the records a pipeline's rows are for. */
+function recordIds(pipeline: Pipeline, rows: readonly PipelineCsvRow[]) {
+ return new Set(
+ rows.flatMap((row) => {
+ const id = row.cells[pipeline.primaryColumnId];
+ return typeof id === 'string' && id ? [id] : [];
+ })
+ );
+}
+
+function pipelineCsvFilename(name: string, now: Date) {
+ const slug =
+ name
+ .toLowerCase()
+ .replace(/[^a-z0-9]+/g, '-')
+ .replace(/^-|-$/g, '') || 'pipeline';
+ return `macro-${slug}-${now.toISOString().slice(0, 10)}.csv`;
+}
+
+/** Every entry of the pipeline, with its company's or contact's name and key. */
+export function createPipelineExport(
+ pipeline: Accessor<Pipeline>,
+ transfer: PipelineTransfer,
+ download: (content: string, filename: string) => Promise<{ saved: boolean }>
+) {
+ const [pending, setPending] = createSignal(false);
+ let controller: AbortController | undefined;
+ onCleanup(() => controller?.abort());
+ async function run(): Promise<'saved' | 'cancelled' | 'failed'> {
+ if (pending()) return 'cancelled';
+ setPending(true);
+ controller = new AbortController();
+ const signal = controller.signal;
+ const span = Telemetry.span('crm.pipeline.export');
+ span.setAttr('crm.pipeline_id', pipeline().id);
+ try {
+ const table = await span.span('crm.pipeline.read_table', () =>
+ transfer.readTable(signal)
+ );
+ span.setAttr('crm.pipeline.rows', table.rows.length);
+ const ids = [...recordIds(pipeline(), table.rows)];
+ span.setAttr('crm.pipeline.records', ids.length);
+ const records = await span.span('crm.pipeline.describe_records', () =>
+ transfer.describeRecords(ids, signal)
+ );
+ const csv = encodePipelineCsv({
+ recordType: pipeline().recordType,
+ columns: table.columns,
+ primaryColumnId: pipeline().primaryColumnId,
+ rows: table.rows,
+ record: (id) => records.get(id),
+ });
+ const { saved } = await download(
+ `\uFEFF${csv}`,
+ pipelineCsvFilename(pipeline().name, new Date())
+ );
+ span.setAttr('crm.export.saved', saved);
+ return saved ? 'saved' : 'cancelled';
+ } catch (cause) {
+ span.error(cause);
+ return 'failed';
+ } finally {
+ span.end();
+ setPending(false);
+ }
+ }
+ return { pending, run };
+}
+
+export type PipelineImportResult = {
+ added: number;
+ /** Records the pipeline already had a row for. */
+ present: number;
+ /** Records that could not be found or created, such as gmail.com companies. */
+ unresolved: number;
+};
+
+/**
+ * Adds a planned CSV's rows to a pipeline, creating the records the team
+ * lacks. Rows for records the pipeline already has are skipped, so running
+ * it again after a failure adds only what is still missing.
+ */
+export async function addPipelineRows(input: {
+ span: Span;
+ pipeline: Pipeline;
+ transfer: PipelineTransfer;
+ plan: PipelineImportPlan;
+ table: PipelineTable;
+ signal: AbortSignal;
+ progress: (count: number) => void;
+}): Promise<PipelineImportResult> {
+ const { span, pipeline, transfer, plan, table } = input;
+ const ids = await span.span('crm.pipeline.resolve_records', () =>
+ transfer.resolveRecords(
+ plan.rows.map((row) => row.record),
+ input.signal,
+ input.progress
+ )
+ );
+ const present = recordIds(pipeline, table.rows);
+ const rows: CellWrite[][] = [];
+ const outcome: PipelineImportResult = { added: 0, present: 0, unresolved: 0 };
+ for (const row of plan.rows) {
+ const id = ids.get(row.record.key);
+ if (!id) outcome.unresolved++;
+ else if (present.has(id)) outcome.present++;
+ else rows.push([pipelineRecordCell(pipeline, id), ...row.cells]);
+ }
+ await span.span('crm.pipeline.insert_rows', () =>
+ transfer.insertRows(rows, table.columns)
+ );
+ outcome.added = rows.length;
+ return outcome;
+}
+
+/**
+ * A CSV's rows added to a pipeline. Records missing from the CRM are created;
+ * a record already in the pipeline is not added again, so a retry after a
+ * failure adds only what is still missing.
+ */
+export function createPipelineImport(
+ pipeline: Pipeline,
+ transfer: PipelineTransfer
+) {
+ const [plan, setPlan] = createSignal<PipelineImportPlan>();
+ const [error, setError] = createSignal('');
+ const [reading, setReading] = createSignal(false);
+ const [pending, setPending] = createSignal(false);
+ const [resolved, setResolved] = createSignal(0);
+ const [result, setResult] = createSignal<PipelineImportResult>();
+ let controller: AbortController | undefined;
+ onCleanup(() => controller?.abort());
+ async function read(file?: Pick<File, 'size' | 'text'>) {
+ setError('');
+ setPlan(undefined);
+ setResult(undefined);
+ if (!file || reading() || pending()) return;
+ if (file.size > MAX_CSV_BYTES) {
+ setError(databaseCsvMessage({ kind: 'too-large' }));
+ return;
+ }
+ setReading(true);
+ controller = new AbortController();
+ try {
+ const csv = parseDatabaseCsv((await file.text()).replace(/^\uFEFF/, ''));
+ if (csv.isErr()) {
+ setError(databaseCsvMessage(csv.error));
+ return;
+ }
+ const table = await transfer.readTable(controller.signal);
+ planPipelineImport(csv.value, {
+ recordType: pipeline.recordType,
+ columns: table.columns,
+ primaryColumnId: pipeline.primaryColumnId,
+ }).match(setPlan, (failure) => setError(pipelineImportMessage(failure)));
+ } catch {
+ setError('Could not read this pipeline. Please try again.');
+ } finally {
+ setReading(false);
+ }
+ }
+ async function run() {
+ const current = plan();
+ if (!current || pending()) return;
+ setPending(true);
+ setError('');
+ setResolved(0);
+ controller = new AbortController();
+ const signal = controller.signal;
+ const span = Telemetry.span('crm.pipeline.import');
+ span.setAttr('crm.pipeline_id', pipeline.id);
+ span.setAttr('crm.import.rows', current.rows.length);
+ try {
+ // Read again so a retry skips rows an interrupted import already added.
+ const table = await span.span('crm.pipeline.read_table', () =>
+ transfer.readTable(signal)
+ );
+ const outcome = await addPipelineRows({
+ span,
+ pipeline,
+ transfer,
+ plan: current,
+ table,
+ signal,
+ progress: setResolved,
+ });
+ span.setAttr('crm.import.added', outcome.added);
+ span.setAttr('crm.import.present', outcome.present);
+ span.setAttr('crm.import.unresolved', outcome.unresolved);
+ setResult(outcome);
+ setPlan(undefined);
+ } catch (cause) {
+ span.error(cause);
+ setError(
+ 'Could not finish the import. Retry to add the rows that are still missing.'
+ );
+ }
+ try {
+ await span.run(() => transfer.refresh());
+ } catch (cause) {
+ // The rows are saved; the pipeline's own polling shows them.
+ span.event('refresh_failed', {
+ error: cause instanceof Error ? cause.message : String(cause),
+ });
+ }
+ span.end();
+ setPending(false);
+ }
+ return { plan, error, reading, pending, resolved, result, read, run };
+}
diff --git a/apps/web/src/features/crm/queries/companies.ts b/apps/web/src/features/crm/queries/companies.ts
index ff24bebbeb..f0e6202368 100644
--- a/apps/web/src/features/crm/queries/companies.ts
+++ b/apps/web/src/features/crm/queries/companies.ts
@@ -1,5 +1,5 @@
import { NIL_UUID } from '@app/features/next-soup/filters/filter-store';
-import { throwOnErr } from '@core/util/result';
+import { ThrownResultError, throwOnErr } from '@core/util/result';
import type { CrmCompanyEntity } from '@entity';
import { soupKeys } from '@queries/soup/keys';
import { invalidateCachedCrmContacts } from '@service-storage/crm-contacts';
@@ -12,6 +12,7 @@ import {
useQuery,
} from '@tanstack/solid-query';
import { type Accessor, createMemo } from 'solid-js';
+import type { CompanyImporter } from '../context/crm-sources';
import type { CrmRecordDependencies } from './dependencies';
import { crmKeys } from './keys';
@@ -159,6 +160,32 @@ export function useCreateCompanyMutation(deps: CrmRecordDependencies) {
);
}
+/**
+ * Creates companies for an import without `useCreateCompanyMutation`'s
+ * per-company soup refresh, which would refetch every listing once per row.
+ * A domain the team already tracks (409) is reported as existing.
+ */
+export function createCompanyImporter(
+ deps: CrmRecordDependencies
+): CompanyImporter {
+ return {
+ async create(input) {
+ const created = await deps.storage.createCompany(input);
+ if (created.isOk()) {
+ deps.client.setQueryData(
+ crmKeys.company(created.value.id).queryKey,
+ created.value
+ );
+ return 'created';
+ }
+ if (created.error.some((error) => error.code === 'CONFLICT'))
+ return 'exists';
+ throw new ThrownResultError(created.error);
+ },
+ refresh: () => deps.client.invalidateQueries({ queryKey: soupKeys._def }),
+ };
+}
+
/**
* Manually creates a contact under a company via
* `POST /crm/companies/{id}/contacts` (a display name + email). Invalidates
diff --git a/apps/web/src/features/crm/queries/pipeline-transfer.test.ts b/apps/web/src/features/crm/queries/pipeline-transfer.test.ts
new file mode 100644
index 0000000000..98693751e6
--- /dev/null
+++ b/apps/web/src/features/crm/queries/pipeline-transfer.test.ts
@@ -0,0 +1,150 @@
+import type { DatabaseOp } from '@core/database-sql/generated/types';
+import { QueryClient } from '@tanstack/solid-query';
+import { err, ok } from 'neverthrow';
+import { describe, expect, it, vi } from 'vitest';
+import type { DatabaseViewColumn } from '../../database/core/database-view';
+import type { Pipeline } from '../core/pipeline';
+import type { CrmRecordDependencies } from './dependencies';
+import { createPipelineTransfer } from './pipeline-transfer';
+
+vi.mock('./export', () => ({
+ fetchCrmExportCompanies: async () => [
+ { id: 'acme-id', name: 'Acme', domains: [{ domain: 'acme.com' }] },
+ ],
+}));
+
+const pipeline: Pipeline = {
+ id: 'pipeline',
+ teamId: 'team',
+ name: 'Deals',
+ userId: 'macro|[email protected]',
+ recordType: 'company',
+ databaseId: 'database',
+ tableId: 'table',
+ primaryColumnId: 'company',
+ sharing: 'team',
+ grant: 'owner',
+ createdAt: '2026-10-01T00:00:00Z',
+ trashedAt: null,
+};
+const stage: DatabaseViewColumn = {
+ id: 'stage',
+ name: 'Stage',
+ dataType: 'SELECT_STRING',
+ isMultiSelect: false,
+ options: [{ id: 'lead', label: 'Lead', color: null }],
+ writable: true,
+};
+
+function setup(recordType: Pipeline['recordType'] = 'company') {
+ const applied: DatabaseOp[][] = [];
+ const storage = {
+ createCompany: vi.fn(async ({ domain }: { domain: string }) => {
+ // A slow create, so concurrent contacts at one domain overlap it.
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ return domain === 'gmail.com'
+ ? err([{ code: 'BAD_REQUEST', message: 'generic domain' }])
+ : ok({ id: `${domain}-id` });
+ }),
+ getContactByEmail: vi.fn(async ({ email }: { email: string }) =>
+ email === '[email protected]'
+ ? err([{ code: 'SERVER_ERROR', message: 'unavailable' }])
+ : ok({ contact: null })
+ ),
+ createContact: vi.fn(async ({ email }: { email: string }) =>
+ ok({ id: `${email}-id` })
+ ),
+ applyCrmPipelineOps: vi.fn(
+ async (_id: string, request: { ops: DatabaseOp[] }) => {
+ applied.push(request.ops);
+ return ok({ results: [] });
+ }
+ ),
+ };
+ const deps = {
+ storage,
+ client: new QueryClient(),
+ } as unknown as CrmRecordDependencies;
+ return {
+ transfer: createPipelineTransfer(deps, { ...pipeline, recordType }),
+ storage,
+ applied,
+ };
+}
+
+describe('pipeline transfer', () => {
+ it('matches companies by domain and creates the missing ones once', async () => {
+ const { transfer, storage } = setup();
+ const ids = await transfer.resolveRecords(
+ [
+ { name: 'Acme', key: 'acme.com' },
+ { name: 'Beta', key: 'beta.io' },
+ { name: 'Beta again', key: 'beta.io' },
+ { name: 'Gmail', key: 'gmail.com' },
+ ],
+ new AbortController().signal,
+ () => {}
+ );
+ expect([...ids]).toEqual([
+ ['acme.com', 'acme-id'],
+ ['beta.io', 'beta.io-id'],
+ ]);
+ expect(storage.createCompany.mock.calls.map(([input]) => input)).toEqual([
+ { name: 'Beta', domain: 'beta.io' },
+ { name: 'Gmail', domain: 'gmail.com' },
+ ]);
+ });
+
+ it('creates one company for new contacts that share its domain', async () => {
+ const { transfer, storage } = setup('contact');
+ const ids = await transfer.resolveRecords(
+ [
+ { name: 'Jane', key: '[email protected]' },
+ { name: 'Raj', key: '[email protected]' },
+ { name: 'Ana', key: '[email protected]' },
+ { name: 'Down', key: '[email protected]' },
+ ],
+ new AbortController().signal,
+ () => {}
+ );
+ expect(ids.size).toBe(3);
+ expect(ids.has('[email protected]')).toBe(false);
+ expect(storage.createContact).toHaveBeenCalledTimes(3);
+ expect(storage.createCompany.mock.calls.map(([input]) => input)).toEqual([
+ { name: 'beta.io', domain: 'beta.io' },
+ ]);
+ expect(storage.createContact.mock.calls.map(([input]) => input)).toEqual(
+ expect.arrayContaining([
+ { companyId: 'beta.io-id', name: 'Jane', email: '[email protected]' },
+ { companyId: 'beta.io-id', name: 'Raj', email: '[email protected]' },
+ { companyId: 'acme-id', name: 'Ana', email: '[email protected]' },
+ ])
+ );
+ });
+
+ it('adds a missing stage once, ahead of the first batch that names it', async () => {
+ const { transfer, applied } = setup();
+ const row = (label: string) => [
+ {
+ column: 'stage',
+ value: { type: 'options' as const, value: [{ label }] },
+ },
+ ];
+ await transfer.insertRows(
+ [
+ ...Array.from({ length: 500 }, () => row('Lead')),
+ row('Won'),
+ row('won'),
+ ],
+ [stage]
+ );
+ expect(applied).toHaveLength(2);
+ expect(applied[0]).toHaveLength(1);
+ expect(applied[1]).toHaveLength(2);
+ expect(applied[1][0]).toMatchObject({
+ kind: 'column',
+ column: 'stage',
+ change: { kind: 'add_options', options: [{ label: 'Won' }] },
+ });
+ });
+});
diff --git a/apps/web/src/features/crm/queries/pipeline-transfer.ts b/apps/web/src/features/crm/queries/pipeline-transfer.ts
new file mode 100644
index 0000000000..b3be037815
--- /dev/null
+++ b/apps/web/src/features/crm/queries/pipeline-transfer.ts
@@ -0,0 +1,203 @@
+import type { DatabaseOp } from '@core/database-sql/generated/types';
+import { ThrownResultError, throwOnErr } from '@core/util/result';
+import { soupKeys } from '@queries/soup/keys';
+import { v7 as uuidv7 } from 'uuid';
+import { missingOptionLabels } from '../../database/core/cell-ops';
+import { toViewColumn } from '../../database/queries/column-detail';
+import type { PipelineTransfer } from '../context/pipelines';
+import { importedDomain } from '../core/import-companies';
+import type { Pipeline } from '../core/pipeline';
+import type { PipelineCsvRecord } from '../core/pipeline-csv';
+import { runConcurrently } from '../core/run-concurrently';
+import type { CrmRecordDependencies } from './dependencies';
+import { fetchCrmExportCompanies } from './export';
+import { crmKeys } from './keys';
+import { pipelineCell } from './pipeline-data';
+
+const CONCURRENCY = 4;
+/** Rows per write; the service accepts up to 10,000. */
+const INSERT_BATCH = 500;
+
+/** Bulk reads and writes of one pipeline, over its own routes. */
+export function createPipelineTransfer(
+ deps: CrmRecordDependencies,
+ pipeline: Pipeline
+): PipelineTransfer {
+ const { storage } = deps;
+ const companies = (signal: AbortSignal) =>
+ fetchCrmExportCompanies(storage, signal, () => {});
+ /**
+ * The company for a domain, created when the team lacks it. Contacts at one
+ * new domain share a single creation rather than racing to a conflict.
+ */
+ function companyId(
+ domains: Map<string, Promise<string | undefined>>,
+ record: PipelineCsvRecord,
+ domain: string
+ ) {
+ const known = domains.get(domain);
+ if (known) return known;
+ const created = (async () => {
+ const company = await storage.createCompany({
+ name: record.name,
+ domain,
+ });
+ return company.isOk() ? company.value.id : undefined;
+ })();
+ domains.set(domain, created);
+ return created;
+ }
+ async function contactId(
+ domains: Map<string, Promise<string | undefined>>,
+ record: PipelineCsvRecord
+ ) {
+ const found = await storage.getContactByEmail({ email: record.key });
+ // A failed lookup is not a missing contact; creating one could duplicate it.
+ if (found.isErr()) return undefined;
+ if (found.value.contact) return found.value.contact.id;
+ const domain = importedDomain(record.key);
+ if (!domain) return undefined;
+ const company = await companyId(
+ domains,
+ { name: domain, key: domain },
+ domain
+ );
+ if (!company) return undefined;
+ const created = await storage.createContact({
+ companyId: company,
+ name: record.name,
+ email: record.key,
+ });
+ return created.isOk() ? created.value.id : undefined;
+ }
+ return {
+ async readTable(signal) {
+ const table = await throwOnErr(() =>
+ storage.getCrmPipelineTable(pipeline.id)
+ );
+ const rows = [];
+ let after: string | undefined;
+ do {
+ signal.throwIfAborted();
+ const page = await throwOnErr(() =>
+ storage.queryCrmPipelineRows(pipeline.id, {
+ after,
+ query: { filter: null },
+ })
+ );
+ rows.push(
+ ...page.rows.map((row) => ({
+ cells: Object.fromEntries(
+ table.columns.map((column) => [
+ column.column.id,
+ pipelineCell(row.cells[column.column.id], column),
+ ])
+ ),
+ }))
+ );
+ after = page.next ?? undefined;
+ } while (after);
+ return { columns: table.columns.map(toViewColumn), rows };
+ },
+ async describeRecords(ids, signal) {
+ const records = new Map<string, PipelineCsvRecord>();
+ if (pipeline.recordType === 'company') {
+ for (const company of await companies(signal))
+ records.set(company.id, {
+ name: company.name,
+ key: company.domains[0]?.domain ?? '',
+ });
+ }
+ const missing = ids.filter((id) => !records.has(id));
+ await runConcurrently(missing, CONCURRENCY, async (id) => {
+ signal.throwIfAborted();
+ if (pipeline.recordType === 'company') {
+ const company = await storage.getCompany({ companyId: id });
+ if (company.isOk())
+ records.set(id, {
+ name: company.value.name ?? '',
+ key: company.value.domains[0]?.domain ?? '',
+ });
+ return;
+ }
+ const contact = await storage.getContact({ contactId: id });
+ if (contact.isOk())
+ records.set(id, {
+ name: contact.value.name ?? '',
+ key: contact.value.email,
+ });
+ });
+ return records;
+ },
+ async resolveRecords(records, signal, progress) {
+ const domains = new Map<string, Promise<string | undefined>>();
+ for (const company of await companies(signal))
+ for (const { domain } of company.domains)
+ domains.set(domain.toLowerCase(), Promise.resolve(company.id));
+ const ids = new Map<string, string>();
+ // A record named on several rows takes its first row's name.
+ const seen = new Set<string>();
+ const unique = records.filter(
+ (record) => !seen.has(record.key) && seen.add(record.key)
+ );
+ await runConcurrently(unique, CONCURRENCY, async (record) => {
+ signal.throwIfAborted();
+ const id =
+ pipeline.recordType === 'company'
+ ? await companyId(domains, record, record.key)
+ : await contactId(domains, record);
+ if (id) ids.set(record.key, id);
+ progress(ids.size);
+ });
+ return ids;
+ },
+ async insertRows(rows, columns) {
+ const labels = new Map(
+ columns.map((column) => [
+ column.id,
+ column.options.map((option) => option.label),
+ ])
+ );
+ for (let start = 0; start < rows.length; start += INSERT_BATCH) {
+ const insert: DatabaseOp = {
+ kind: 'rows',
+ table: pipeline.tableId,
+ change: {
+ kind: 'insert',
+ rows: rows.slice(start, start + INSERT_BATCH),
+ },
+ };
+ const options: DatabaseOp[] = missingOptionLabels(
+ insert,
+ (id) => labels.get(id) ?? []
+ ).map(({ column, labels: added }) => {
+ labels.set(column, [...(labels.get(column) ?? []), ...added]);
+ return {
+ kind: 'column',
+ table: pipeline.tableId,
+ column,
+ change: {
+ kind: 'add_options',
+ options: added.map((label) => ({ id: uuidv7(), label })),
+ },
+ };
+ });
+ const applied = await storage.applyCrmPipelineOps(pipeline.id, {
+ ops: [...options, insert],
+ });
+ if (applied.isErr()) throw new ThrownResultError(applied.error);
+ }
+ },
+ async refresh() {
+ await Promise.all([
+ deps.client.invalidateQueries({ queryKey: soupKeys._def }),
+ deps.client.invalidateQueries({
+ queryKey: crmKeys.pipelineTable(pipeline.id).queryKey,
+ }),
+ deps.client.invalidateQueries({
+ queryKey: ['crm', 'pipeline', pipeline.id],
+ }),
+ ]);
+ },
+ };
+}
diff --git a/apps/web/src/features/crm/views/crm-workspace.tsx b/apps/web/src/features/crm/views/crm-workspace.tsx
index 7ee1a04c33..cc351ad3e8 100644
--- a/apps/web/src/features/crm/views/crm-workspace.tsx
+++ b/apps/web/src/features/crm/views/crm-workspace.tsx
@@ -462,6 +462,10 @@ export function CrmWorkspaceView(props: {
<PipelineView
pipeline={pipeline()}
source={pipelines}
+ transfer={{
+ source: useCrmContext().createPipelineTransfer(pipeline()),
+ downloadCsv: useCrmContext().downloadCsv,
+ }}
Sharing={useCrmContext().PipelineSharing}
Editor={PipelineEditor}
onCopyLink={() =>
@@ -632,7 +636,35 @@ export function CrmWorkspaceView(props: {
/>
</Show>
<Show when={importing()}>
- <CrmImport onClose={() => setImporting(false)} />
+ <CrmImport
+ onClose={() => setImporting(false)}
+ pipelines={
+ canCreatePipeline()
+ ? {
+ existing: () =>
+ pipelines
+ .pipelines()
+ .filter(
+ (pipeline) =>
+ pipeline.recordType === 'company' &&
+ (pipeline.grant === 'owner' ||
+ pipeline.grant === 'edit')
+ ),
+ create: (name, sharing) =>
+ pipelines.create({
+ name,
+ recordType: 'company',
+ sharing,
+ }),
+ transfer: useCrmContext().createPipelineTransfer,
+ }
+ : undefined
+ }
+ onOpenPipeline={(id) => {
+ setImporting(false);
+ selectPipeline(id);
+ }}
+ />
</Show>
</ViewShell.Root>
);
diff --git a/apps/web/src/features/crm/views/import-companies.tsx b/apps/web/src/features/crm/views/import-companies.tsx
index 11d7accddf..fd3c777a7a 100644
--- a/apps/web/src/features/crm/views/import-companies.tsx
+++ b/apps/web/src/features/crm/views/import-companies.tsx
@@ -1,13 +1,69 @@
-import { Button, Dialog, Panel } from '@ui';
+import { Button, Dialog, Panel, Select } from '@ui';
import { For, Show } from 'solid-js';
-import { createCompanyImport } from '../primitives/import-companies';
-import { useCreateCompanyMutation } from './use-crm';
+import type { CompanyImportPipelines } from '../context/pipelines';
+import {
+ createCompanyImport,
+ type ImportDestination,
+} from '../primitives/import-companies';
+import { useCompanyImporter } from './use-crm';
-export function CrmImport(props: { onClose: () => void }) {
- const create = useCreateCompanyMutation();
- const { rows, error, pending, completed, read, run } = createCompanyImport(
- (input) => create.mutateAsync(input)
- );
+/** The preview lists this many rows; the import takes them all. */
+const PREVIEW_ROWS = 100;
+
+export function CrmImport(props: {
+ onClose: () => void;
+ /** Where Stage, Owner, and Revenue go; absent while pipelines are unavailable. */
+ pipelines?: CompanyImportPipelines;
+ onOpenPipeline: (id: string) => void;
+}) {
+ const {
+ rows,
+ skipped,
+ error,
+ pending,
+ completed,
+ existing,
+ pipelineData,
+ pipeline,
+ destination,
+ setDestination,
+ runnable,
+ read,
+ run,
+ } = createCompanyImport(useCompanyImporter(), props.pipelines);
+ type Choice = {
+ value: string;
+ label: string;
+ destination: ImportDestination;
+ };
+ const choices = (): Choice[] => [
+ {
+ value: 'new:private',
+ label: 'New private pipeline',
+ destination: { kind: 'new', sharing: 'private' },
+ },
+ {
+ value: 'new:team',
+ label: 'New team pipeline',
+ destination: { kind: 'new', sharing: 'team' },
+ },
+ ...(props.pipelines?.existing() ?? []).map(
+ (existing): Choice => ({
+ value: `existing:${existing.id}`,
+ label: existing.name,
+ destination: { kind: 'existing', id: existing.id },
+ })
+ ),
+ ];
+ const chosen = () => {
+ const current = destination();
+ return choices().find((choice) =>
+ current.kind === 'existing'
+ ? choice.value === `existing:${current.id}`
+ : choice.value === `new:${current.sharing}`
+ );
+ };
+ const done = () => completed() + existing();
return (
<Dialog
open
@@ -21,7 +77,8 @@ export function CrmImport(props: { onClose: () => void }) {
Import companies
</Dialog.Title>
<Dialog.Description class="text-sm text-ink-muted">
- Upload a CSV with name and domain columns. Review the companies
+ Upload a CSV with a domain column and, optionally, a name or
+ company column, such as a Macro CRM export. Review the companies
before importing them into your team CRM.
</Dialog.Description>
<input
@@ -32,9 +89,9 @@ export function CrmImport(props: { onClose: () => void }) {
onChange={(e) => void read(e.currentTarget.files?.[0])}
class="text-sm"
/>
- <Show when={rows().length}>
+ <Show when={rows().length && !pending()}>
<div class="max-h-64 overflow-auto rounded-lg border border-edge-muted">
- <For each={rows()}>
+ <For each={rows().slice(0, PREVIEW_ROWS)}>
{(row) => (
<div class="flex justify-between gap-3 border-b border-edge-muted px-3 py-2 text-sm last:border-0">
<span class="truncate">{row.name}</span>
@@ -42,13 +99,92 @@ export function CrmImport(props: { onClose: () => void }) {
</div>
)}
</For>
+ <Show when={rows().length > PREVIEW_ROWS}>
+ <div class="px-3 py-2 text-sm text-ink-muted">
+ and {(rows().length - PREVIEW_ROWS).toLocaleString()} more
+ </div>
+ </Show>
</div>
</Show>
- <Show when={completed()}>
+ <Show when={skipped() && !done()}>
+ <p class="text-sm text-ink-muted">
+ {skipped().toLocaleString()} rows skipped: no domain like
+ acme.com, or a domain listed earlier in the file.
+ </p>
+ </Show>
+ <Show when={!pipeline() && pipelineData()}>
+ {(data) => (
+ <p class="text-sm text-ink-muted">
+ <Show
+ when={props.pipelines}
+ fallback={`${data().fields.join(', ')} won't be imported: pipelines aren't available for your team.`}
+ >
+ {data().fields.join(', ')} for the{' '}
+ {data().csv.rows.length.toLocaleString()} companies that
+ have them go into
+ <Show when={destination().kind === 'new'}>
+ {' '}
+ a pipeline named “{data().name}”
+ </Show>
+ :
+ </Show>
+ </p>
+ )}
+ </Show>
+ <Show when={props.pipelines && !pipeline() && pipelineData()}>
+ <Select<Choice>
+ options={choices()}
+ optionValue="value"
+ optionTextValue="label"
+ value={chosen()}
+ onChange={(choice) =>
+ choice && setDestination(choice.destination)
+ }
+ disabled={pending()}
+ modal={false}
+ >
+ <Select.Trigger
+ aria-label="Pipeline for Stage and Owner"
+ class="h-10 w-full min-w-0 rounded-lg border border-edge bg-surface-2 px-3 text-sm text-ink outline-none focus-visible:ring-2 focus-visible:ring-ink/20 disabled:opacity-50"
+ >
+ <Select.Value<Choice>>
+ {(state) => state.selectedOption()?.label}
+ </Select.Value>
+ <Select.Icon />
+ </Select.Trigger>
+ <Select.Content>
+ <Select.Listbox />
+ </Select.Content>
+ </Select>
+ </Show>
+ <Show when={done()}>
<p role="status" class="text-sm">
- {completed()} companies imported.
+ {completed().toLocaleString()}{' '}
+ {completed() === 1 ? 'company' : 'companies'} imported
+ <Show when={existing()}>
+ , {existing().toLocaleString()} already in your CRM
+ </Show>
+ .
</p>
</Show>
+ <Show when={pipeline()}>
+ {(added) => (
+ <p
+ role="status"
+ class="flex flex-wrap items-center gap-1 text-sm"
+ >
+ {added().added.toLocaleString()} rows added to{' '}
+ {added().pipeline.name}.
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={() => props.onOpenPipeline(added().pipeline.id)}
+ >
+ Open pipeline
+ </Button>
+ </p>
+ )}
+ </Show>
<Show when={error()}>
<p role="alert" class="text-sm text-failure">
{error()}
@@ -64,12 +200,14 @@ export function CrmImport(props: { onClose: () => void }) {
</Button>
<Button
variant="strong"
- disabled={pending() || !rows().length}
+ disabled={pending() || !runnable()}
onClick={() => void run()}
>
{pending()
- ? 'Importing…'
- : `Import ${rows().length || ''} companies`}
+ ? `Importing… ${done().toLocaleString()} of ${rows().length.toLocaleString()}`
+ : rows().length || !runnable()
+ ? `Import ${rows().length ? rows().length.toLocaleString() : ''} companies`
+ : 'Add to pipeline'}
</Button>
</div>
</div>
diff --git a/apps/web/src/features/crm/views/pipeline-import.tsx b/apps/web/src/features/crm/views/pipeline-import.tsx
new file mode 100644
index 0000000000..39e380f8d0
--- /dev/null
+++ b/apps/web/src/features/crm/views/pipeline-import.tsx
@@ -0,0 +1,122 @@
+import { Button, Dialog, Panel } from '@ui';
+import { Show } from 'solid-js';
+import type { PipelineTransfer } from '../context/pipelines';
+import type { Pipeline } from '../core/pipeline';
+import { pipelineRecordHeaders } from '../core/pipeline-csv';
+import { createPipelineImport } from '../primitives/pipeline-transfer';
+
+const entries = (count: number) =>
+ `${count.toLocaleString()} ${count === 1 ? 'entry' : 'entries'}`;
+
+export function PipelineImport(props: {
+ pipeline: Pipeline;
+ transfer: PipelineTransfer;
+ onClose: () => void;
+}) {
+ const { plan, error, reading, pending, resolved, result, read, run } =
+ createPipelineImport(props.pipeline, props.transfer);
+ const [name, key] = pipelineRecordHeaders(props.pipeline.recordType);
+ const records = () =>
+ props.pipeline.recordType === 'company' ? 'companies' : 'contacts';
+ return (
+ <Dialog
+ open
+ onOpenChange={(open) => !open && !pending() && props.onClose()}
+ class="w-112 max-w-[calc(100vw-2rem)]"
+ >
+ <Panel>
+ <Panel.Body>
+ <div class="flex flex-col gap-4 p-5">
+ <Dialog.Title class="text-base font-semibold">
+ Import into {props.pipeline.name}
+ </Dialog.Title>
+ <Dialog.Description class="text-sm text-ink-muted">
+ Upload a CSV with a {key} column and, optionally, a {name} column,
+ such as a pipeline export. Other columns fill the pipeline columns
+ with the same name. Missing {records()} are added to your CRM.
+ </Dialog.Description>
+ <input
+ aria-label="Pipeline CSV"
+ type="file"
+ accept=".csv,text/csv"
+ disabled={reading() || pending()}
+ onChange={(e) => void read(e.currentTarget.files?.[0])}
+ class="text-sm"
+ />
+ <Show when={plan()}>
+ {(current) => (
+ <div class="flex flex-col gap-1 text-sm">
+ <p>{entries(current().rows.length)} ready to import.</p>
+ <Show when={current().columns.length}>
+ <p class="text-ink-muted">
+ Fills {current().columns.join(', ')}.
+ </p>
+ </Show>
+ <Show when={current().ignored.length}>
+ <p class="text-ink-muted">
+ Skips columns this pipeline can't fill:{' '}
+ {current().ignored.join(', ')}.
+ </p>
+ </Show>
+ <Show when={current().skipped}>
+ <p class="text-ink-muted">
+ Skips {current().skipped.toLocaleString()} rows without a{' '}
+ {key.toLowerCase()}.
+ </p>
+ </Show>
+ <Show when={current().invalidCells}>
+ <p class="text-ink-muted">
+ Leaves {current().invalidCells.toLocaleString()} values
+ empty that don't fit their column.
+ </p>
+ </Show>
+ </div>
+ )}
+ </Show>
+ <Show when={result()}>
+ {(done) => (
+ <p role="status" class="text-sm">
+ {entries(done().added)} added
+ <Show when={done().present}>
+ , {done().present.toLocaleString()} already in this pipeline
+ </Show>
+ <Show when={done().unresolved}>
+ , {done().unresolved.toLocaleString()} {records()} could not
+ be found or added to your CRM, such as personal domains like
+ gmail.com. Import again to retry them
+ </Show>
+ .
+ </p>
+ )}
+ </Show>
+ <Show when={error()}>
+ <p role="alert" class="text-sm text-failure">
+ {error()}
+ </p>
+ </Show>
+ <div class="flex justify-end gap-2">
+ <Button
+ variant="ghost"
+ disabled={pending()}
+ onClick={props.onClose}
+ >
+ Close
+ </Button>
+ <Button
+ variant="strong"
+ disabled={reading() || pending() || !plan()}
+ onClick={() => void run()}
+ >
+ {pending()
+ ? `Importing… ${resolved().toLocaleString()} ${records()} matched`
+ : reading()
+ ? 'Reading…'
+ : 'Import'}
+ </Button>
+ </div>
+ </div>
+ </Panel.Body>
+ </Panel>
+ </Dialog>
+ );
+}
diff --git a/apps/web/src/features/crm/views/pipeline.tsx b/apps/web/src/features/crm/views/pipeline.tsx
index 50f3eba554..3608815b00 100644
--- a/apps/web/src/features/crm/views/pipeline.tsx
+++ b/apps/web/src/features/crm/views/pipeline.tsx
@@ -1,8 +1,10 @@
import { InlineTitleEditor } from '@core/component/InlineTitleEditor';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import DotsThreeIcon from '@phosphor/dots-three.svg';
+import DownloadIcon from '@phosphor/download-simple.svg';
import PencilIcon from '@phosphor/pencil-line.svg';
import TrashIcon from '@phosphor/trash-simple.svg';
+import UploadIcon from '@phosphor/upload-simple.svg';
import { ActionDialogShell } from '@ui/components/ActionDialogShell';
import { Button } from '@ui/components/Button';
import { DeleteDialog } from '@ui/components/DeleteDialog';
@@ -14,8 +16,11 @@ import type {
PipelineEditor,
PipelineSharing,
PipelinesSource,
+ PipelineTransfer,
} from '../context/pipelines';
import type { Pipeline } from '../core/pipeline';
+import { createPipelineExport } from '../primitives/pipeline-transfer';
+import { PipelineImport } from './pipeline-import';
/** A pipeline's page: its name, sharing and actions above its one table. */
export function PipelineView(props: {
@@ -28,8 +33,14 @@ export function PipelineView(props: {
/** After the pipeline moves to trash, leave its page. */
onTrashed(): void;
onOpenRecord?(record: { type: 'company' | 'contact'; id: string }): void;
+ /** CSV export and import of the pipeline's entries, when the host offers them. */
+ transfer?: {
+ source: PipelineTransfer;
+ downloadCsv(content: string, filename: string): Promise<{ saved: boolean }>;
+ };
}) {
const [pending, setPending] = createSignal(false);
+ const [importing, setImporting] = createSignal(false);
const [error, setError] = createSignal('');
const [deleting, setDeleting] = createSignal(false);
const [actionsOpen, setActionsOpen] = createSignal(false);
@@ -39,6 +50,20 @@ export function PipelineView(props: {
let mobileNameInput: HTMLInputElement | undefined;
const canEdit = () => ['owner', 'edit'].includes(props.pipeline.grant);
const isOwner = () => props.pipeline.grant === 'owner';
+ // A host supplies one transfer for the page's lifetime.
+ const transfer = props.transfer;
+ const exporter =
+ transfer &&
+ createPipelineExport(
+ () => props.pipeline,
+ transfer.source,
+ transfer.downloadCsv
+ );
+ const exportCsv = async () => {
+ setError('');
+ if ((await exporter?.run()) === 'failed')
+ setError('Could not export this pipeline. Please try again.');
+ };
async function save(action: () => Promise<void>) {
if (pending()) return false;
setPending(true);
@@ -104,7 +129,7 @@ export function PipelineView(props: {
/>
</span>
</Show>
- <Show when={canEdit() || isOwner()}>
+ <Show when={canEdit() || isOwner() || transfer}>
<Dropdown placement="bottom-start">
<Dropdown.Trigger
variant="ghost"
@@ -122,6 +147,21 @@ export function PipelineView(props: {
Rename
</Dropdown.Item>
</Show>
+ <Show when={transfer}>
+ <Dropdown.Item
+ disabled={exporter?.pending()}
+ onSelect={() => void exportCsv()}
+ >
+ <DownloadIcon class="size-4" />
+ Export CSV
+ </Dropdown.Item>
+ <Show when={canEdit()}>
+ <Dropdown.Item onSelect={() => setImporting(true)}>
+ <UploadIcon class="size-4" />
+ Import CSV
+ </Dropdown.Item>
+ </Show>
+ </Show>
<Show when={isOwner()}>
<Dropdown.Item onSelect={() => setDeleting(true)}>
<TrashIcon class="size-4" />
@@ -215,6 +255,30 @@ export function PipelineView(props: {
</form>
</Show>
</Show>
+ <Show when={transfer}>
+ <Button
+ variant="ghost"
+ class="w-full justify-start"
+ disabled={exporter?.pending()}
+ onClick={() => void exportCsv()}
+ >
+ <DownloadIcon class="size-4" />
+ Export CSV
+ </Button>
+ <Show when={canEdit()}>
+ <Button
+ variant="ghost"
+ class="w-full justify-start"
+ onClick={() => {
+ setActionsOpen(false);
+ setImporting(true);
+ }}
+ >
+ <UploadIcon class="size-4" />
+ Import CSV
+ </Button>
+ </Show>
+ </Show>
<div class="flex items-center justify-between gap-3">
<span class="text-sm">Sharing</span>
<props.Sharing
@@ -252,6 +316,15 @@ export function PipelineView(props: {
</ActionDialogShell.Footer>
</ActionDialogShell>
</Dialog>
+ <Show when={importing() && transfer}>
+ {(current) => (
+ <PipelineImport
+ pipeline={props.pipeline}
+ transfer={current().source}
+ onClose={() => setImporting(false)}
+ />
+ )}
+ </Show>
<DeleteDialog
open={deleting()}
onOpenChange={setDeleting}
diff --git a/apps/web/src/features/crm/views/use-crm.ts b/apps/web/src/features/crm/views/use-crm.ts
index 65db239a89..70b1149ebb 100644
--- a/apps/web/src/features/crm/views/use-crm.ts
+++ b/apps/web/src/features/crm/views/use-crm.ts
@@ -34,6 +34,7 @@ export const useCrmLists = (...args: Parameters<CrmContext['createLists']>) =>
export const useCreateCompanyMutation = (
...args: Parameters<CrmContext['createCompany']>
) => useCrmContext().createCompany(...args);
+export const useCompanyImporter = () => useCrmContext().createCompanyImporter();
export const useCreateContactMutation = (
...args: Parameters<CrmContext['createContact']>
) => useCrmContext().createContact(...args);
diff --git a/apps/web/src/features/database/core/csv.ts b/apps/web/src/features/database/core/csv.ts
index 3683d6f9f1..fc353350ad 100644
--- a/apps/web/src/features/database/core/csv.ts
+++ b/apps/web/src/features/database/core/csv.ts
@@ -3,6 +3,9 @@ import { err, ok, type Result } from 'neverthrow';
import Papa from 'papaparse';
import { match } from 'ts-pattern';
import type { ResultError } from '../../../lib/core/util/result';
+import { relatedRowIds } from './database-relations';
+import type { DatabaseCellValue, DatabaseViewColumn } from './database-view';
+import { formatCellValue } from './table';
export type DatabaseCsv = {
columns: string[];
@@ -133,3 +136,21 @@ export function encodeDatabaseCsv(
}
);
}
+
+/**
+ * A cell as the CSV carries it: what the grid shows, except where its display
+ * drops data. Dates keep their stored moment (the grid shows the day) and
+ * numbers every digit (the grid rounds). Relation cells have no row names
+ * loaded here, so they keep the related rows' ids rather than claiming those
+ * rows are unavailable.
+ */
+export function exportedValue(
+ column: DatabaseViewColumn,
+ value: DatabaseCellValue
+): string {
+ if (column.relation) return relatedRowIds(value).join(', ');
+ if (column.dataType === 'DATE' && typeof value === 'string') return value;
+ if (column.dataType === 'NUMBER' && typeof value === 'number')
+ return String(value);
+ return formatCellValue(column, value);
+}
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts
index d333277fde..e61046fa88 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts
@@ -8317,6 +8317,16 @@ export const EditWordDocument = z.object({
export const WordDocumentResponse = z.object({ content: z.string() });
+export const ExportPipelineCsv = z.object({ pipelineId: z.string().uuid() });
+
+export const ExportPipelineCsvResponse = z.object({
+ documentId: z.string().uuid(),
+ fileName: z.string(),
+ rows: z.number().int().gte(0),
+ complete: z.boolean(),
+ summary: z.string(),
+});
+
export const FindPipelineEntries = z.object({
pipelineId: z.union([z.string().uuid(), z.null()]).optional(),
filters: z
@@ -8769,6 +8779,45 @@ export const GetThreadResponse = z.object({
summary: z.string(),
});
+export const ImportCompaniesCsv = z.object({
+ csv: z.union([z.string(), z.null()]).optional(),
+ documentId: z.union([z.string().uuid(), z.null()]).optional(),
+ pipelineId: z.union([z.string().uuid(), z.null()]).optional(),
+ newPipelineName: z.union([z.string(), z.null()]).optional(),
+ shareNewPipeline: z.union([z.boolean(), z.null()]).optional(),
+});
+
+export const ImportCompaniesCsvResponse = z.object({
+ created: z.number().int().gte(0),
+ existing: z.number().int().gte(0),
+ skippedRows: z.number().int().gte(0),
+ failedCount: z.number().int().gte(0),
+ failed: z.array(z.object({ key: z.string(), reason: z.string() })),
+ pipeline: z
+ .union([
+ z.object({
+ pipelineId: z.string().uuid(),
+ pipelineName: z.string(),
+ added: z.number().int().gte(0),
+ alreadyPresent: z.number().int().gte(0),
+ createdRecords: z.number().int().gte(0),
+ unresolvedCount: z.number().int().gte(0),
+ unresolved: z.array(z.object({ key: z.string(), reason: z.string() })),
+ invalidCount: z.number().int().gte(0),
+ invalid: z.array(z.object({ key: z.string(), reason: z.string() })),
+ skippedRows: z.number().int().gte(0),
+ filledFields: z.array(z.string()),
+ ignoredColumns: z.array(z.string()),
+ addedOptions: z.array(
+ z.object({ field: z.string(), labels: z.array(z.string()) })
+ ),
+ }),
+ z.null(),
+ ])
+ .optional(),
+ summary: z.string(),
+});
+
export const ImportNotionPage = z.object({ pageUrl: z.string() });
export const ImportNotionPageResponse = z.object({
@@ -8829,6 +8878,31 @@ export const ImportNotionPageResponse = z.object({
message: z.string(),
});
+export const ImportPipelineCsv = z.object({
+ pipelineId: z.string().uuid(),
+ csv: z.union([z.string(), z.null()]).optional(),
+ documentId: z.union([z.string().uuid(), z.null()]).optional(),
+});
+
+export const ImportPipelineCsvResponse = z.object({
+ pipelineId: z.string().uuid(),
+ pipelineName: z.string(),
+ added: z.number().int().gte(0),
+ alreadyPresent: z.number().int().gte(0),
+ createdRecords: z.number().int().gte(0),
+ unresolvedCount: z.number().int().gte(0),
+ unresolved: z.array(z.object({ key: z.string(), reason: z.string() })),
+ invalidCount: z.number().int().gte(0),
+ invalid: z.array(z.object({ key: z.string(), reason: z.string() })),
+ skippedRows: z.number().int().gte(0),
+ filledFields: z.array(z.string()),
+ ignoredColumns: z.array(z.string()),
+ addedOptions: z.array(
+ z.object({ field: z.string(), labels: z.array(z.string()) })
+ ),
+ summary: z.string(),
+});
+
export const IssueBotCredential = z.object({
botId: z.string().uuid(),
label: z.union([z.string(), z.null()]).optional(),
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts
index 0a254a5431..f3cd6fbfb2 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts
@@ -182,6 +182,10 @@ type ToolParserMap = {
response: types.WordDocumentResponse;
};
ExecuteCode: { call: types.ExecuteCode; response: types.ExecutionReceipt };
+ ExportPipelineCsv: {
+ call: types.ExportPipelineCsv;
+ response: types.ExportPipelineCsvResponse;
+ };
FindPipelineEntries: {
call: types.FindPipelineEntries;
response: types.FindPipelineEntriesResponse;
@@ -216,10 +220,18 @@ type ToolParserMap = {
response: types.TeamAvailability;
};
GetThread: { call: types.GetThread; response: types.GetThreadResponse };
+ ImportCompaniesCsv: {
+ call: types.ImportCompaniesCsv;
+ response: types.ImportCompaniesCsvResponse;
+ };
ImportNotionPage: {
call: types.ImportNotionPage;
response: types.ImportNotionPageResponse;
};
+ ImportPipelineCsv: {
+ call: types.ImportPipelineCsv;
+ response: types.ImportPipelineCsvResponse;
+ };
IssueBotCredential: {
call: types.IssueBotCredential;
response: types.IssueBotCredentialResponse;
@@ -671,6 +683,10 @@ const toolParserMap = {
call: schemas.ExecuteCode,
response: schemas.ExecutionReceipt,
},
+ ExportPipelineCsv: {
+ call: schemas.ExportPipelineCsv,
+ response: schemas.ExportPipelineCsvResponse,
+ },
FindPipelineEntries: {
call: schemas.FindPipelineEntries,
response: schemas.FindPipelineEntriesResponse,
@@ -708,10 +724,18 @@ const toolParserMap = {
response: schemas.TeamAvailability,
},
GetThread: { call: schemas.GetThread, response: schemas.GetThreadResponse },
+ ImportCompaniesCsv: {
+ call: schemas.ImportCompaniesCsv,
+ response: schemas.ImportCompaniesCsvResponse,
+ },
ImportNotionPage: {
call: schemas.ImportNotionPage,
response: schemas.ImportNotionPageResponse,
},
+ ImportPipelineCsv: {
+ call: schemas.ImportPipelineCsv,
+ response: schemas.ImportPipelineCsvResponse,
+ },
IssueBotCredential: {
call: schemas.IssueBotCredential,
response: schemas.IssueBotCredentialResponse,
@@ -1207,6 +1231,10 @@ type ToolDataMap = {
response: types.WordDocumentResponse;
};
ExecuteCode: { call: types.ExecuteCode; response: types.ExecutionReceipt };
+ ExportPipelineCsv: {
+ call: types.ExportPipelineCsv;
+ response: types.ExportPipelineCsvResponse;
+ };
FindPipelineEntries: {
call: types.FindPipelineEntries;
response: types.FindPipelineEntriesResponse;
@@ -1241,10 +1269,18 @@ type ToolDataMap = {
response: types.TeamAvailability;
};
GetThread: { call: types.GetThread; response: types.GetThreadResponse };
+ ImportCompaniesCsv: {
+ call: types.ImportCompaniesCsv;
+ response: types.ImportCompaniesCsvResponse;
+ };
ImportNotionPage: {
call: types.ImportNotionPage;
response: types.ImportNotionPageResponse;
};
+ ImportPipelineCsv: {
+ call: types.ImportPipelineCsv;
+ response: types.ImportPipelineCsvResponse;
+ };
IssueBotCredential: {
call: types.IssueBotCredential;
response: types.IssueBotCredentialResponse;
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts
index a65532e54e..9c612ec220 100644
--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts
+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts
@@ -9201,6 +9201,40 @@ export interface WordDocumentResponse {
*/
content: string;
}
+/**
+ * Export a CRM pipeline's deals (entries) to a CSV file saved in the user's Macro files: Company and Domain (or Contact and Email), then one column per field, with people as emails. Use it to back up a pipeline, move it to another team (ImportPipelineCsv reads the same file), or share it as a spreadsheet; to answer questions about deals, use FindPipelineEntries instead. Returns the file's documentId and how many entries it holds.
+ */
+export interface ExportPipelineCsv {
+ /**
+ * The pipeline's id from ListPipelines.
+ */
+ pipelineId: string;
+}
+/**
+ * The saved file.
+ */
+export interface ExportPipelineCsvResponse {
+ /**
+ * The CSV file in Macro.
+ */
+ documentId: string;
+ /**
+ * The file's name.
+ */
+ fileName: string;
+ /**
+ * Entries written.
+ */
+ rows: number;
+ /**
+ * Whether every entry was written.
+ */
+ complete: boolean;
+ /**
+ * What happened.
+ */
+ summary: string;
+}
/**
* Find deals (entries) in a CRM pipeline: which companies or contacts are in which stage, who owns them, their revenue and other fields. Filter by field values (e.g. Stage is Demo or Trial, Owner is me), by company or contact name/domain/email with `search`, or by record id; sort by any field (Stage sorts in pipeline order). Returns each entry's entryId, the record it tracks (id and name) and its filled-in fields, plus, over every match, the count per stage (and per option of each single-select field) and the total of each number field such as Revenue: use these for questions like "how many deals are in Negotiation" or "what is the pipeline worth". To find where a company or contact is across all pipelines ("what stage is Acme in?"), leave out pipelineId and pass its name as `search` (or its recordIds). Get pipeline ids and field names from ListPipelines.
*/
@@ -10031,6 +10065,147 @@ export interface ToolContact {
*/
name?: string | null;
}
+/**
+ * Import companies into the team CRM from a CSV, such as a Macro companies export or a list from another CRM. Each row needs a domain (a Domain, Domains or Website column) and may name the company (Company or Name); a row takes its first valid domain, and companies the team already has are left as they are. Stage, Owner and Revenue live on pipeline entries rather than companies: when the CSV has them, they go on entries of pipelineId, or of a new pipeline (private unless shareNewPipeline) named after the file. Give the CSV as text in csv, or as documentId for a CSV file in Macro. Returns counts and the rows that could not be imported. To add rows to a pipeline without this, use ImportPipelineCsv.
+ */
+export interface ImportCompaniesCsv {
+ /**
+ * The CSV text with a header row. Give this or documentId, not both.
+ */
+ csv?: string | null;
+ /**
+ * The id of a CSV file in Macro the user can open, e.g. one they uploaded. Give this or csv, not both.
+ */
+ documentId?: string | null;
+ /**
+ * A company pipeline (id from ListPipelines) to put the CSV's Stage, Owner and Revenue on. Omit to create a new pipeline when the CSV has them.
+ */
+ pipelineId?: string | null;
+ /**
+ * The name of the new pipeline, when one is created. Defaults to the file's name.
+ */
+ newPipelineName?: string | null;
+ /**
+ * Share the new pipeline with the team instead of keeping it private to the user. Defaults to false.
+ */
+ shareNewPipeline?: boolean | null;
+}
+/**
+ * What the import did.
+ */
+export interface ImportCompaniesCsvResponse {
+ /**
+ * Companies created.
+ */
+ created: number;
+ /**
+ * Companies the team already had.
+ */
+ existing: number;
+ /**
+ * Rows without a domain like acme.com.
+ */
+ skippedRows: number;
+ /**
+ * How many companies could not be created.
+ */
+ failedCount: number;
+ /**
+ * The first of them, with why.
+ */
+ failed: ToolSkippedRecord[];
+ /**
+ * The pipeline entries made from Stage, Owner and Revenue.
+ */
+ pipeline?: ToolPipelineImport | null;
+ /**
+ * What happened.
+ */
+ summary: string;
+}
+/**
+ * A record an import could not add.
+ */
+export interface ToolSkippedRecord {
+ /**
+ * The company's domain or the contact's email.
+ */
+ key: string;
+ /**
+ * Why it was not added.
+ */
+ reason: string;
+}
+/**
+ * What a pipeline import did.
+ */
+export interface ToolPipelineImport {
+ /**
+ * The pipeline filled.
+ */
+ pipelineId: string;
+ /**
+ * Its name.
+ */
+ pipelineName: string;
+ /**
+ * Entries added.
+ */
+ added: number;
+ /**
+ * Rows whose record the pipeline already had, so were not added again.
+ */
+ alreadyPresent: number;
+ /**
+ * Companies or contacts created to add their entries.
+ */
+ createdRecords: number;
+ /**
+ * How many records could not be found or created.
+ */
+ unresolvedCount: number;
+ /**
+ * The first of them, with why.
+ */
+ unresolved: ToolSkippedRecord[];
+ /**
+ * How many rows were refused for a value their field cannot hold.
+ */
+ invalidCount: number;
+ /**
+ * The first of them, with why.
+ */
+ invalid: ToolSkippedRecord[];
+ /**
+ * Rows without a domain or email.
+ */
+ skippedRows: number;
+ /**
+ * Pipeline fields the CSV filled.
+ */
+ filledFields: string[];
+ /**
+ * CSV columns that match no field the import can fill.
+ */
+ ignoredColumns: string[];
+ /**
+ * Options added to select fields so the CSV's values fit.
+ */
+ addedOptions: ToolAddedOptions[];
+}
+/**
+ * A select field and the options an import added to it.
+ */
+export interface ToolAddedOptions {
+ /**
+ * The field.
+ */
+ field: string;
+ /**
+ * The labels added.
+ */
+ labels: string[];
+}
/**
* Import one specific Notion page through Macro's canonical Notion importer. Use this when the user explicitly asks to import a page URL or id. The tool performs deduplication, fetches through the user's connected Notion MCP, normalizes the page, creates the Macro markdown document, and returns its entity id. Do not fetch and recreate the page manually with generic document tools. Notion databases and database-first pages are intentionally not imported.
*/
@@ -10056,6 +10231,84 @@ export interface ImportNotionPageResponse {
*/
message: string;
}
+/**
+ * Import a CSV into a CRM pipeline as new deals (entries), such as a pipeline export or a sheet of deals. In a company pipeline each row needs a domain (Domain, Domains or Website column) and may name the company (Company); in a contact pipeline each row needs an Email and may name the contact (Contact). Missing companies and contacts are created. Other columns fill the pipeline fields with the same name (Stage, Owner as an email, Revenue, custom fields); stage labels the pipeline lacks are added. Records already in the pipeline are not added again, so retrying an interrupted import adds only what is missing; several rows for one new record become separate deals. Give the CSV as text in csv, or as documentId for a CSV file in Macro. Returns counts and the rows that could not be added.
+ */
+export interface ImportPipelineCsv {
+ /**
+ * The pipeline's id from ListPipelines.
+ */
+ pipelineId: string;
+ /**
+ * The CSV text with a header row. Give this or documentId, not both.
+ */
+ csv?: string | null;
+ /**
+ * The id of a CSV file in Macro the user can open, e.g. one they uploaded or an ExportPipelineCsv result. Give this or csv, not both.
+ */
+ documentId?: string | null;
+}
+/**
+ * What the import did.
+ */
+export interface ImportPipelineCsvResponse {
+ /**
+ * The pipeline filled.
+ */
+ pipelineId: string;
+ /**
+ * Its name.
+ */
+ pipelineName: string;
+ /**
+ * Entries added.
+ */
+ added: number;
+ /**
+ * Rows whose record the pipeline already had, so were not added again.
+ */
+ alreadyPresent: number;
+ /**
+ * Companies or contacts created to add their entries.
+ */
+ createdRecords: number;
+ /**
+ * How many records could not be found or created.
+ */
+ unresolvedCount: number;
+ /**
+ * The first of them, with why.
+ */
+ unresolved: ToolSkippedRecord[];
+ /**
+ * How many rows were refused for a value their field cannot hold.
+ */
+ invalidCount: number;
+ /**
+ * The first of them, with why.
+ */
+ invalid: ToolSkippedRecord[];
+ /**
+ * Rows without a domain or email.
+ */
+ skippedRows: number;
+ /**
+ * Pipeline fields the CSV filled.
+ */
+ filledFields: string[];
+ /**
+ * CSV columns that match no field the import can fill.
+ */
+ ignoredColumns: string[];
+ /**
+ * Options added to select fields so the CSV's values fit.
+ */
+ addedOptions: ToolAddedOptions[];
+ /**
+ * What happened.
+ */
+ summary: string;
+}
/**
* Prepare a new bot credential for a bot the current user can manage. This does not mint the secret. Tell the user to click the chat card or open bot settings to create the token. The secret is shown only while that card is open. Use this when the user asks for bot credentials or a webhook token; existing raw secrets cannot be recovered safely.
*/
diff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rs
index 7eac30bbed..13049ff4e8 100644
--- a/crates/ai_tools/src/build_context.rs
+++ b/crates/ai_tools/src/build_context.rs
@@ -531,6 +531,11 @@ pub async fn build_tool_service_context_from_env(
&databases_tool_context,
&crm_tool_context,
);
+ let crm_transfer_tool_context = crate::tool_context::build_crm_transfer_tool_context(
+ &pipeline_tool_context,
+ &crm_tool_context,
+ &document_tool_context,
+ );
let forms_tool_context = crate::tool_context::build_forms_tool_context(
pool.clone(),
@@ -673,6 +678,7 @@ pub async fn build_tool_service_context_from_env(
team_tool_context: crate::tool_context::build_team_tool_context(pool.clone()),
crm_tool_context,
pipeline_tool_context,
+ crm_transfer_tool_context,
skill_tool_context,
schedule_tool_context: crate::build_routine_tool_context()?,
anthropic_tool_context,
diff --git a/crates/ai_tools/src/crm_csv_files.rs b/crates/ai_tools/src/crm_csv_files.rs
new file mode 100644
index 0000000000..726043b698
--- /dev/null
+++ b/crates/ai_tools/src/crm_csv_files.rs
@@ -0,0 +1,172 @@
+//! The documents domain as the CRM's CSV files: what the CSV import tools
+//! read and the pipeline export saves.
+//!
+//! The crm crate defines the [`CsvFiles`] port and knows nothing about
+//! documents; this bridge lives in the composition root, like
+//! [`crate::email_attachments`].
+
+use std::sync::Arc;
+use std::time::Duration;
+
+use crm::domain::crm_csv::MAX_CSV_BYTES;
+use crm::domain::crm_transfer::{CsvFile, CsvFiles, TransferError};
+use databases::domain::models::Viewer;
+use documents::domain::{
+ create::{NewDocumentMetadata, NewPlainTextDocument},
+ models::LocationQueryParams,
+ ports::{DocumentService, create::DocumentCreationService},
+ response::LocationResponseV3,
+};
+use documents::inbound::toolset::DefaultDocumentToolCreator;
+use entity_access::domain::{
+ models::{EntityType, ViewAccessLevel},
+ ports::EntityAccessService,
+};
+use model::document::FileType;
+use model_owner::CreationPrincipal;
+use uuid::Uuid;
+
+/// How long one CSV download may take, body included.
+const DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(60);
+
+/// [`CsvFiles`] over Macro documents: CSV uploads in, CSV documents out.
+pub struct DocumentCsvFiles<D: DocumentService + DocumentCreationService, A> {
+ documents: Arc<D>,
+ creator: DefaultDocumentToolCreator<D>,
+ access: Arc<A>,
+ http: reqwest::Client,
+}
+
+impl<D: DocumentService + DocumentCreationService, A> DocumentCsvFiles<D, A> {
+ /// CSV files through `documents`, created with `creator`, authorized by `access`.
+ pub fn new(documents: Arc<D>, creator: DefaultDocumentToolCreator<D>, access: Arc<A>) -> Self {
+ Self {
+ documents,
+ creator,
+ access,
+ http: reqwest::Client::new(),
+ }
+ }
+}
+
+fn unreadable(reason: impl std::fmt::Display) -> TransferError {
+ TransferError::File(format!("The CSV file could not be read: {reason}."))
+}
+
+impl<D, A> CsvFiles for DocumentCsvFiles<D, A>
+where
+ D: DocumentService + DocumentCreationService,
+ A: EntityAccessService,
+{
+ #[tracing::instrument(skip_all, fields(document_id = %document), err)]
+ async fn read(&self, viewer: &Viewer, document: Uuid) -> Result<CsvFile, TransferError> {
+ let document_id = document.to_string();
+ let receipt = self
+ .access
+ .generate_entity_access_receipt::<ViewAccessLevel>(
+ &viewer.user_id,
+ None,
+ &document_id,
+ EntityType::Document,
+ )
+ .await
+ .map_err(|_| {
+ TransferError::File(
+ "No CSV file with that documentId exists for this user.".to_owned(),
+ )
+ })?;
+ let basic = self
+ .documents
+ .internal_get_basic_document(&document_id)
+ .await
+ .map_err(unreadable)?;
+ if !matches!(basic.try_file_type(), Some(FileType::Csv | FileType::Txt)) {
+ return Err(TransferError::File(format!(
+ "\"{}\" is not a CSV file; pass a .csv upload, or the CSV text in csv.",
+ basic.document_name
+ )));
+ }
+ let location = self
+ .documents
+ .get_document_location(
+ &basic,
+ receipt,
+ LocationQueryParams {
+ document_version_id: None,
+ get_converted_docx_url: None,
+ },
+ )
+ .await
+ .map_err(unreadable)?;
+ let LocationResponseV3::PresignedUrl { presigned_url, .. } = location else {
+ return Err(unreadable("it is not a single uploaded file"));
+ };
+ // The location is browser-facing; a server-side fetch on the local
+ // stack must reach LocalStack directly (a no-op elsewhere).
+ let url = macro_aws_config::transform_aws_url_for_internal_fetch(&presigned_url);
+ let response = self
+ .http
+ .get(&url)
+ .timeout(DOWNLOAD_TIMEOUT)
+ .send()
+ .await
+ .map_err(unreadable)?;
+ if !response.status().is_success() {
+ return Err(unreadable(format!(
+ "download failed with {}",
+ response.status()
+ )));
+ }
+ if response
+ .content_length()
+ .is_some_and(|length| length > MAX_CSV_BYTES as u64)
+ {
+ return Err(TransferError::File(format!(
+ "The CSV file is larger than {} MB.",
+ MAX_CSV_BYTES / (1024 * 1024)
+ )));
+ }
+ let bytes = response.bytes().await.map_err(unreadable)?;
+ let text =
+ String::from_utf8(bytes.to_vec()).map_err(|_| unreadable("it is not UTF-8 text"))?;
+ Ok(CsvFile {
+ name: basic.document_name,
+ text,
+ })
+ }
+
+ #[tracing::instrument(skip_all, err)]
+ async fn save(&self, viewer: &Viewer, name: &str, text: String) -> Result<Uuid, TransferError> {
+ let principal = match viewer.acting_bot {
+ Some(bot) => CreationPrincipal::BotForUser {
+ bot,
+ user: viewer.user_id.clone(),
+ },
+ None => CreationPrincipal::User(viewer.user_id.clone()),
+ };
+ let document =
+ NewPlainTextDocument::builder(NewDocumentMetadata::builder(name.to_owned()).build())
+ .file_type(FileType::Csv)
+ .text(text)
+ .build()
+ .map_err(|error| {
+ TransferError::File(format!("The CSV file could not be saved: {error}."))
+ })?;
+ let created = self
+ .creator
+ .create_plain_text(&principal, document)
+ .await
+ .map_err(|error| {
+ TransferError::File(format!("The CSV file could not be saved: {error}."))
+ })?;
+ let id = created
+ .into_response()
+ .document_response
+ .document_metadata
+ .metadata
+ .document_id;
+ id.to_string()
+ .parse()
+ .map_err(|_| TransferError::File("The saved CSV file has no valid id.".to_owned()))
+ }
+}
diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rs
index f0436f127c..b42bf4fb51 100644
--- a/crates/ai_tools/src/lib.rs
+++ b/crates/ai_tools/src/lib.rs
@@ -10,6 +10,7 @@ mod test;
pub mod ai_operations;
mod build_context;
+pub mod crm_csv_files;
mod deferred;
mod display_results;
pub mod email_attachments;
@@ -77,8 +78,9 @@ pub use tool_context::{
ToolChannelEventDispatcher, ToolChannelMediaUploadToolContext, ToolChannelMessagesService,
ToolChannelToolContext, ToolChatService, ToolChatToolContext, ToolCodingAgentToolContext,
ToolCognitionToolContext, ToolCommsService, ToolCrmService, ToolCrmToolContext,
- ToolDatabasesService, ToolDatabasesSqlToolContext, ToolDatabasesToolContext,
- ToolDocumentService, ToolDocumentToolContext, ToolEmailService, ToolEmailToolContext,
+ ToolCrmTransferService, ToolCrmTransferToolContext, ToolDatabasesService,
+ ToolDatabasesSqlToolContext, ToolDatabasesToolContext, ToolDocumentService,
+ ToolDocumentToolContext, ToolEmailService, ToolEmailToolContext,
ToolEntityAccessManagementService, ToolEntityAccessService, ToolEntityCreator,
ToolFavoritesService, ToolFavoritesToolContext, ToolForeignEntityService, ToolFormsToolContext,
ToolFrecencyService, ToolGithubPullRequestService, ToolImageGenerationToolContext,
@@ -94,15 +96,15 @@ pub use tool_context::{
build_booking_management_tool_context, build_bot_tool_context, build_calendar_tool_context,
build_channel_media_upload_tool_context, build_channel_tool_context_with_dispatcher,
build_channel_tool_context_with_side_effects, build_channel_tool_context_without_side_effects,
- build_coding_agent_tool_context, build_crm_tool_context, build_databases_sql_tool_context,
- build_databases_tool_context, build_favorites_service, build_favorites_tool_context,
- build_forms_tool_context, build_image_generation_tool_context, build_initiative_tool_context,
- build_item_notification_preference_tool_context, build_message_service_with_side_effects,
- build_message_service_without_side_effects, build_pipeline_tool_context,
- build_project_tool_context, build_properties_service, build_properties_service_with_broker,
- build_properties_tool_context, build_routine_tool_context, build_skill_tool_context,
- build_task_properties_adapter, build_team_calendar_tool_context, build_team_repository,
- build_team_tool_context,
+ build_coding_agent_tool_context, build_crm_tool_context, build_crm_transfer_tool_context,
+ build_databases_sql_tool_context, build_databases_tool_context, build_favorites_service,
+ build_favorites_tool_context, build_forms_tool_context, build_image_generation_tool_context,
+ build_initiative_tool_context, build_item_notification_preference_tool_context,
+ build_message_service_with_side_effects, build_message_service_without_side_effects,
+ build_pipeline_tool_context, build_project_tool_context, build_properties_service,
+ build_properties_service_with_broker, build_properties_tool_context,
+ build_routine_tool_context, build_skill_tool_context, build_task_properties_adapter,
+ build_team_calendar_tool_context, build_team_repository, build_team_tool_context,
};
pub use tool_context::{ToolConnectorToolContext, build_connector_tool_context};
#[cfg(any(test, feature = "test-support"))]
@@ -170,6 +172,9 @@ pub(crate) fn subagent_toolset() -> AiToolSet {
.add_subtoolset::<ToolTeamToolContext>(team_toolset())
.add_subtoolset::<ToolCrmToolContext>(crm_toolset())
.add_subtoolset::<ToolPipelineToolContext>(pipeline_toolset())
+ .add_subtoolset::<ToolCrmTransferToolContext>(
+ crm::inbound::toolset::transfer::crm_transfer_toolset(),
+ )
.add_subtoolset::<ToolDatabasesToolContext>(databases_toolset())
.add_subtoolset::<ToolFormsToolContext>(forms::inbound::toolset::authoring_toolset())
.add_subtoolset::<ToolDatabasesSqlToolContext>(databases_sql_toolset())
diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rs
index ac949a83ef..25c2d5abaf 100644
--- a/crates/ai_tools/src/tool_context.rs
+++ b/crates/ai_tools/src/tool_context.rs
@@ -651,6 +651,42 @@ pub fn build_pipeline_tool_context(
}
}
+/// CRM CSV imports and exports over the pipeline workflows, the CRM's
+/// records and Macro documents.
+pub type ToolCrmTransferService = crm::domain::crm_transfer::CrmTransferServiceImpl<
+ ToolPipelineWorkflows,
+ crm::domain::crm_transfer::CrmTransferRecords<ToolCrmService, ToolEntityAccessService>,
+ crate::crm_csv_files::DocumentCsvFiles<ToolDocumentService, ToolEntityAccessService>,
+>;
+
+/// Type alias for the CRM CSV AI tool context.
+pub type ToolCrmTransferToolContext =
+ crm::inbound::toolset::transfer::CrmTransferToolContext<ToolCrmTransferService>;
+
+/// Build the CRM CSV tools over the pipeline tools' workflows, the CRM
+/// tools' records, and the document tools' files.
+pub fn build_crm_transfer_tool_context(
+ pipelines: &ToolPipelineToolContext,
+ crm: &ToolCrmToolContext,
+ documents: &ToolDocumentToolContext,
+) -> ToolCrmTransferToolContext {
+ crm::inbound::toolset::transfer::CrmTransferToolContext {
+ service: Arc::new(crm::domain::crm_transfer::CrmTransferServiceImpl::new(
+ pipelines.service.clone(),
+ crm::domain::crm_transfer::CrmTransferRecords::new(
+ crm.service.clone(),
+ crm.entity_access_service.clone(),
+ ),
+ crate::crm_csv_files::DocumentCsvFiles::new(
+ documents.service.clone(),
+ documents.creator.clone(),
+ documents.entity_access_service.clone(),
+ ),
+ )),
+ actor: pipelines.actor,
+ }
+}
+
/// Type alias for the skill service implementation used by AI tools.
pub type ToolSkillService = skills::domain::service::SkillServiceImpl<
skills::outbound::search_service_searcher::SearchServiceSkillSearcher,
@@ -1802,6 +1838,8 @@ pub struct ToolServiceContext {
pub crm_tool_context: ToolCrmToolContext,
/// CRM pipelines: deals by record, fields by name.
pub pipeline_tool_context: ToolPipelineToolContext,
+ /// CRM CSV imports and exports.
+ pub crm_transfer_tool_context: ToolCrmTransferToolContext,
pub skill_tool_context: ToolSkillToolContext,
pub schedule_tool_context: RoutineToolContext,
#[from_ref(skip)]
@@ -1872,6 +1910,7 @@ impl ToolServiceContext {
self.databases_tool_context = self.databases_tool_context.with_actor(actor);
self.forms_tool_context.actor = actor;
self.pipeline_tool_context.actor = actor;
+ self.crm_transfer_tool_context.actor = actor;
self.databases_sql_tool_context = self.databases_sql_tool_context.with_actor(actor);
self
}
diff --git a/crates/crm/AGENTS.md b/crates/crm/AGENTS.md
index 8b4dc78b80..3a5cee903f 100644
--- a/crates/crm/AGENTS.md
+++ b/crates/crm/AGENTS.md
@@ -11,6 +11,10 @@ tools/HTTP/GraphQL surfaces over them.
- `src/domain/service.rs`, `src/domain/pipelines.rs`: core use cases.
- `src/domain/pipeline_workflows/`: pipeline entry/field/record ops built on
the shared `databases` engine (Sales pipelines are a `databases` consumer).
+- `src/domain/crm_csv.rs`, `src/domain/crm_transfer.rs`: CSV import/export for
+ agents (companies, pipeline entries, pipeline exports). Writes go through the
+ pipeline workflows and `CrmService`; CSV files reach documents through the
+ `CsvFiles` port, implemented in `ai_tools::crm_csv_files`.
- `src/inbound/axum_router.rs` (+ `axum_router/*.rs`): HTTP handlers.
- `src/inbound/toolset.rs` (+ `toolset/*.rs`): AI tool definitions.
- `src/outbound/companies_repo.rs`: the Postgres adapter for companies/contacts.
diff --git a/crates/crm/Cargo.toml b/crates/crm/Cargo.toml
index 8bdd5a6754..4bcfe50e63 100644
--- a/crates/crm/Cargo.toml
+++ b/crates/crm/Cargo.toml
@@ -54,7 +54,7 @@ outbound = [
"dep:unfurl",
"ports",
]
-ports = ["dep:bot_id", "dep:databases", "dep:models_databases", "dep:models_permissions", "dep:models_properties", "dep:macro_user_id", "dep:macro_uuid", "dep:rootcause", "dep:utoipa", "entity_access/ports"]
+ports = ["dep:bot_id", "dep:csv", "dep:databases", "dep:futures", "dep:models_databases", "dep:models_permissions", "dep:models_properties", "dep:macro_user_id", "dep:macro_uuid", "dep:rootcause", "dep:utoipa", "entity_access/ports"]
# Read-only CRM search (name/domain). Pulls only sqlx + ports — no
# reqwest/unfurl/axum — so search consumers stay lightweight.
search = ["dep:sqlx", "ports"]
@@ -66,6 +66,8 @@ models_databases = { path = "../models_databases", optional = true }
models_permissions = { path = "../models_permissions", optional = true }
entity_access_db_utils = { path = "../entity_access_db_utils", optional = true }
anyhow = { workspace = true }
+csv = { workspace = true, optional = true }
+futures = { workspace = true, optional = true }
chrono = { workspace = true }
filter_ast = { path = "../filter_ast" }
item_filters = { path = "../item_filters", default-features = false }
diff --git a/crates/crm/src/domain.rs b/crates/crm/src/domain.rs
index 239f25f638..19b0020a38 100644
--- a/crates/crm/src/domain.rs
+++ b/crates/crm/src/domain.rs
@@ -40,6 +40,12 @@ pub mod association;
/// Contact listing models shared by Soup and the CRM service.
#[cfg(feature = "ports")]
pub mod contact_listing;
+/// CRM CSV files: parsing companies and pipeline entries, and writing exports.
+#[cfg(feature = "ports")]
+pub mod crm_csv;
+/// CSV imports and exports of companies and pipelines, for agents.
+#[cfg(feature = "ports")]
+pub mod crm_transfer;
/// Pipeline use cases addressed by record, field name and stage label.
#[cfg(feature = "ports")]
pub mod pipeline_workflows;
diff --git a/crates/crm/src/domain/crm_csv.rs b/crates/crm/src/domain/crm_csv.rs
new file mode 100644
index 0000000000..d6e7d7b4e3
--- /dev/null
+++ b/crates/crm/src/domain/crm_csv.rs
@@ -0,0 +1,284 @@
+//! CRM CSV files as the import and export tools read and write them: a
+//! companies list (a domain column, a name column, and any pipeline fields)
+//! or a pipeline's entries (the record's name and domain or email, then one
+//! column per field). Parsing and shaping only; no I/O.
+
+#[cfg(test)]
+mod test;
+
+use std::collections::HashSet;
+
+use super::pipelines::PipelineRecordType;
+
+/// Most data rows one import reads.
+pub const MAX_CSV_ROWS: usize = 10_000;
+/// Most bytes one import reads.
+pub const MAX_CSV_BYTES: usize = 16 * 1024 * 1024;
+
+/// Headers naming a company in a companies or company-pipeline CSV.
+const COMPANY_NAME_HEADERS: &[&str] = &["company", "name", "company name"];
+/// Headers holding a company's domain; our companies export writes `Domains`.
+const COMPANY_KEY_HEADERS: &[&str] = &["domain", "domains", "website"];
+/// Headers naming a contact in a contact-pipeline CSV.
+const CONTACT_NAME_HEADERS: &[&str] = &["contact", "name", "contact name"];
+/// Headers holding a contact's email.
+const CONTACT_KEY_HEADERS: &[&str] = &["email", "email address"];
+/// Company columns that pipeline entries hold now, rather than companies.
+pub const COMPANY_PIPELINE_FIELDS: &[&str] = &["stage", "owner", "revenue"];
+
+/// A parsed CSV: its header row and data rows, every row as wide as the header.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct CsvTable {
+ /// Column names as written.
+ pub headers: Vec<String>,
+ /// Data rows, padded or cut to the header's width.
+ pub rows: Vec<Vec<String>>,
+}
+
+/// Why a CSV cannot be read; the message says what to change.
+#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
+pub enum CsvError {
+ /// The text is not a CSV the importer can read.
+ #[error("{0}")]
+ Invalid(String),
+}
+
+/// Parse CSV text with a header row. A UTF-8 byte order mark is ignored.
+pub fn parse_csv(text: &str) -> Result<CsvTable, CsvError> {
+ if text.len() > MAX_CSV_BYTES {
+ return Err(CsvError::Invalid(format!(
+ "The CSV is larger than {} MB.",
+ MAX_CSV_BYTES / (1024 * 1024)
+ )));
+ }
+ let text = text.strip_prefix('\u{feff}').unwrap_or(text);
+ let mut reader = csv::ReaderBuilder::new()
+ .has_headers(true)
+ .flexible(true)
+ .from_reader(text.as_bytes());
+ let headers: Vec<String> = reader
+ .headers()
+ .map_err(|error| CsvError::Invalid(format!("The CSV header could not be read: {error}")))?
+ .iter()
+ .map(|header| header.trim().to_owned())
+ .collect();
+ if headers.iter().all(String::is_empty) {
+ return Err(CsvError::Invalid("The CSV needs a header row.".into()));
+ }
+ let mut rows = Vec::new();
+ for (index, record) in reader.records().enumerate() {
+ let record = record.map_err(|error| {
+ CsvError::Invalid(format!("CSV row {} could not be read: {error}", index + 2))
+ })?;
+ if record.iter().all(|value| value.trim().is_empty()) {
+ continue;
+ }
+ if rows.len() == MAX_CSV_ROWS {
+ return Err(CsvError::Invalid(format!(
+ "The CSV has more than {MAX_CSV_ROWS} rows; split it and import each part."
+ )));
+ }
+ rows.push(
+ (0..headers.len())
+ .map(|column| record.get(column).unwrap_or_default().to_owned())
+ .collect(),
+ );
+ }
+ Ok(CsvTable { headers, rows })
+}
+
+/// A cell as typed: trimmed, without the quote exports put before a value
+/// that starts with a formula character (after spaces too).
+pub fn imported_text(value: &str) -> String {
+ let text = value.trim();
+ match text.strip_prefix('\'') {
+ Some(rest) if rest.trim_start().starts_with(['=', '+', '-', '@']) => rest.trim().to_owned(),
+ _ => text.to_owned(),
+ }
+}
+
+/// Whether `host` is a bare domain like `acme.com` or `mail.acme.co.uk`.
+fn is_domain(host: &str) -> bool {
+ let labels: Vec<&str> = host.split('.').collect();
+ let Some((top, rest)) = labels.split_last() else {
+ return false;
+ };
+ !rest.is_empty()
+ && top.len() >= 2
+ && top.chars().all(|c| c.is_ascii_alphabetic())
+ && rest.iter().all(|label| {
+ !label.is_empty()
+ && !label.starts_with('-')
+ && !label.ends_with('-')
+ && label.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
+ })
+}
+
+/// The bare domain of a domain, URL, or email address.
+pub fn imported_domain(value: &str) -> Option<String> {
+ let value = value.trim().to_ascii_lowercase();
+ let without_scheme = value
+ .split_once("://")
+ .map_or(value.as_str(), |(_, rest)| rest);
+ let without_user = without_scheme
+ .split_once('@')
+ .map_or(without_scheme, |(_, host)| host);
+ let host = without_user
+ .split(['/', '?', '#', ':'])
+ .next()
+ .unwrap_or_default();
+ let host = host.strip_prefix("www.").unwrap_or(host);
+ is_domain(host).then(|| host.to_owned())
+}
+
+/// A row's first valid domain among values separated by `;`, `,` or spaces.
+fn first_domain(value: &str) -> Option<String> {
+ imported_text(value)
+ .split([';', ',', ' ', '\t'])
+ .find_map(imported_domain)
+}
+
+fn imported_email(value: &str) -> Option<String> {
+ let email = imported_text(value).to_ascii_lowercase();
+ let (local, host) = email.split_once('@')?;
+ (!local.is_empty() && !local.contains(char::is_whitespace) && is_domain(host)).then_some(email)
+}
+
+fn header_index(headers: &[String], names: &[&str]) -> Option<usize> {
+ headers
+ .iter()
+ .position(|header| names.contains(&header.trim().to_ascii_lowercase().as_str()))
+}
+
+/// One CSV row as an entry or record to import.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct RecordRow {
+ /// The record's name; its domain or email when the row names none.
+ pub name: String,
+ /// The company's domain or the contact's email, lowercased.
+ pub key: String,
+ /// The row's other non-empty cells: header and value.
+ pub values: Vec<(String, String)>,
+}
+
+/// The rows of a CSV that name a record.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct RecordRows {
+ /// Rows with a usable domain or email, in file order.
+ pub rows: Vec<RecordRow>,
+ /// Rows without one.
+ pub skipped: usize,
+ /// Headers other than the record's name and key, in file order.
+ pub other_headers: Vec<String>,
+}
+
+/// The records a CSV names: companies by domain, contacts by email. Rows are
+/// kept in file order, repeats included, since a pipeline export lists one
+/// row per deal.
+pub fn record_rows(
+ table: &CsvTable,
+ record_type: PipelineRecordType,
+) -> Result<RecordRows, CsvError> {
+ let (name_headers, key_headers) = match record_type {
+ PipelineRecordType::Company => (COMPANY_NAME_HEADERS, COMPANY_KEY_HEADERS),
+ PipelineRecordType::Contact => (CONTACT_NAME_HEADERS, CONTACT_KEY_HEADERS),
+ };
+ let key_index = header_index(&table.headers, key_headers).ok_or_else(|| {
+ CsvError::Invalid(match record_type {
+ PipelineRecordType::Company => {
+ "The CSV needs a Domain column, with domains like acme.com.".into()
+ }
+ PipelineRecordType::Contact => {
+ "The CSV needs an Email column, with addresses like [email protected].".into()
+ }
+ })
+ })?;
+ let name_index = header_index(&table.headers, name_headers).filter(|&i| i != key_index);
+ let other: Vec<usize> = (0..table.headers.len())
+ .filter(|&i| i != key_index && Some(i) != name_index && !table.headers[i].is_empty())
+ .collect();
+ let mut rows = Vec::new();
+ let mut skipped = 0;
+ for row in &table.rows {
+ let key = match record_type {
+ PipelineRecordType::Company => first_domain(&row[key_index]),
+ PipelineRecordType::Contact => imported_email(&row[key_index]),
+ };
+ let Some(key) = key else {
+ skipped += 1;
+ continue;
+ };
+ let name = name_index
+ .map(|i| imported_text(&row[i]))
+ .filter(|name| !name.is_empty())
+ .unwrap_or_else(|| key.clone());
+ let values = other
+ .iter()
+ .map(|&i| (table.headers[i].clone(), imported_text(&row[i])))
+ .filter(|(_, value)| !value.is_empty())
+ .collect();
+ rows.push(RecordRow { name, key, values });
+ }
+ Ok(RecordRows {
+ rows,
+ skipped,
+ other_headers: other.iter().map(|&i| table.headers[i].clone()).collect(),
+ })
+}
+
+/// The companies a companies CSV adds, one per domain, first row first.
+pub fn unique_companies(rows: &[RecordRow]) -> Vec<&RecordRow> {
+ let mut seen = HashSet::new();
+ rows.iter()
+ .filter(|row| seen.insert(row.key.as_str()))
+ .collect()
+}
+
+/// The pipeline part of a companies CSV: its Stage, Owner and Revenue
+/// values on each row that has one. Empty when no row has any.
+pub fn company_pipeline_rows(rows: &[RecordRow]) -> Vec<RecordRow> {
+ rows.iter()
+ .filter_map(|row| {
+ let values: Vec<(String, String)> = row
+ .values
+ .iter()
+ .filter(|(header, _)| {
+ COMPANY_PIPELINE_FIELDS.contains(&header.trim().to_ascii_lowercase().as_str())
+ })
+ .cloned()
+ .collect();
+ (!values.is_empty()).then(|| RecordRow {
+ values,
+ ..row.clone()
+ })
+ })
+ .collect()
+}
+
+/// A cell written so a spreadsheet shows it as text, never as a formula.
+fn safe_cell(value: &str) -> String {
+ if value.trim_start().starts_with(['=', '+', '-', '@']) {
+ format!("'{value}")
+ } else {
+ value.to_owned()
+ }
+}
+
+/// CSV text for `headers` and `rows`, quoted where needed.
+pub fn encode_csv(headers: &[String], rows: &[Vec<String>]) -> String {
+ let mut writer = csv::WriterBuilder::new().from_writer(Vec::new());
+ let mut write = |record: Vec<String>| {
+ // Writing to memory fails only on a bug in the writer itself.
+ writer
+ .write_record(record)
+ .expect("a CSV record writes to memory");
+ };
+ write(headers.iter().map(|header| safe_cell(header)).collect());
+ for row in rows {
+ write(row.iter().map(|value| safe_cell(value)).collect());
+ }
+ let bytes = writer
+ .into_inner()
+ .expect("an in-memory CSV writer flushes");
+ String::from_utf8(bytes).expect("CSV written from strings is UTF-8")
+}
diff --git a/crates/crm/src/domain/crm_csv/test.rs b/crates/crm/src/domain/crm_csv/test.rs
new file mode 100644
index 0000000000..1441198b2b
--- /dev/null
+++ b/crates/crm/src/domain/crm_csv/test.rs
@@ -0,0 +1,124 @@
+use super::*;
+
+#[test]
+fn reads_a_companies_export() {
+ let table = parse_csv(
+ "\u{feff}\"Company\",\"Domains\",\"Stage\",\"Owner\",\"Revenue\"\r\n\
+ \"Acme\",\"acme.com; acme.io\",\"Lead\",\"[email protected]\",\"\"\r\n\
+ \"'=Formula\",\"https://www.formula.co/about\",\"\",\"\",\"\"\r\n\
+ \"No domain\",\"\",\"Lead\",\"\",\"\"\r\n\
+ \"Acme again\",\"ACME.com\",\"\",\"\",\"\"\r\n",
+ )
+ .unwrap();
+ let rows = record_rows(&table, PipelineRecordType::Company).unwrap();
+ assert_eq!(rows.skipped, 1);
+ assert_eq!(rows.other_headers, ["Stage", "Owner", "Revenue"]);
+ assert_eq!(
+ rows.rows,
+ [
+ RecordRow {
+ name: "Acme".into(),
+ key: "acme.com".into(),
+ values: vec![
+ ("Stage".into(), "Lead".into()),
+ ("Owner".into(), "[email protected]".into()),
+ ],
+ },
+ RecordRow {
+ name: "=Formula".into(),
+ key: "formula.co".into(),
+ values: vec![],
+ },
+ RecordRow {
+ name: "Acme again".into(),
+ key: "acme.com".into(),
+ values: vec![],
+ },
+ ]
+ );
+ let companies: Vec<&str> = unique_companies(&rows.rows)
+ .iter()
+ .map(|row| row.name.as_str())
+ .collect();
+ assert_eq!(companies, ["Acme", "=Formula"]);
+ let pipeline = company_pipeline_rows(&rows.rows);
+ assert_eq!(pipeline.len(), 1);
+ assert_eq!(pipeline[0].key, "acme.com");
+}
+
+#[test]
+fn names_contacts_by_email() {
+ let table = parse_csv(
+ "Contact,Email,Stage\nJane,[email protected],Demo\n,[email protected],\nBad,not-an-email,Lead\n",
+ )
+ .unwrap();
+ let rows = record_rows(&table, PipelineRecordType::Contact).unwrap();
+ assert_eq!(rows.skipped, 1);
+ assert_eq!(
+ rows.rows
+ .iter()
+ .map(|row| (row.name.as_str(), row.key.as_str()))
+ .collect::<Vec<_>>(),
+ );
+}
+
+#[test]
+fn needs_the_column_that_names_each_record() {
+ let table = parse_csv("Company,Stage\nAcme,Lead\n").unwrap();
+ assert!(matches!(
+ record_rows(&table, PipelineRecordType::Company),
+ Err(CsvError::Invalid(message)) if message.contains("Domain column")
+ ));
+}
+
+#[test]
+fn round_trips_an_export() {
+ let headers = vec![
+ "Company".to_owned(),
+ "Domain".to_owned(),
+ "Notes".to_owned(),
+ ];
+ let rows = vec![vec![
+ " =SUM(A1)".to_owned(),
+ "acme.com".to_owned(),
+ "said \"hi\", twice".to_owned(),
+ ]];
+ let text = encode_csv(&headers, &rows);
+ assert_eq!(
+ text,
+ "Company,Domain,Notes\n' =SUM(A1),acme.com,\"said \"\"hi\"\", twice\"\n"
+ );
+ let table = parse_csv(&text).unwrap();
+ let rows = record_rows(&table, PipelineRecordType::Company).unwrap();
+ assert_eq!(rows.rows[0].name, "=SUM(A1)");
+ assert_eq!(
+ rows.rows[0].values,
+ [("Notes".to_owned(), "said \"hi\", twice".to_owned())]
+ );
+}
+
+#[test]
+fn refuses_files_beyond_the_row_limit() {
+ let text = format!(
+ "Domain\n{}",
+ (0..=MAX_CSV_ROWS)
+ .map(|i| format!("c{i}.com\n"))
+ .collect::<String>()
+ );
+ assert!(
+ matches!(parse_csv(&text), Err(CsvError::Invalid(message)) if message.contains("10000"))
+ );
+}
+
+#[test]
+fn accepts_only_bare_domains() {
+ assert_eq!(
+ imported_domain("Mail.Acme.co.uk"),
+ Some("mail.acme.co.uk".into())
+ );
+ assert_eq!(imported_domain("[email protected]"), Some("acme.com".into()));
+ assert_eq!(imported_domain("localhost"), None);
+ assert_eq!(imported_domain("-acme.com"), None);
+ assert_eq!(imported_domain("acme.c0m"), None);
+}
diff --git a/crates/crm/src/domain/crm_transfer.rs b/crates/crm/src/domain/crm_transfer.rs
new file mode 100644
index 0000000000..845ab2dd16
--- /dev/null
+++ b/crates/crm/src/domain/crm_transfer.rs
@@ -0,0 +1,769 @@
+//! CSV import and export for agents: a companies list into the team CRM
+//! (with its Stage, Owner and Revenue on pipeline entries), a CSV into a
+//! pipeline, and a pipeline out to a CSV file.
+//!
+//! Every write goes through [`PipelineWorkflowService`] and the CRM's own
+//! record creation, so imports keep the same access, validation and field
+//! rules as the single-record tools; this module only reads the CSV, matches
+//! its columns, and batches the writes.
+
+mod models;
+mod records;
+
+#[cfg(test)]
+mod test;
+
+use std::collections::{HashMap, HashSet};
+use std::sync::{Arc, Mutex, PoisonError};
+
+use databases::domain::models::Viewer;
+use futures::{StreamExt, stream};
+use uuid::Uuid;
+
+use super::{
+ crm_csv::{
+ CsvTable, RecordRow, company_pipeline_rows, encode_csv, parse_csv, record_rows,
+ unique_companies,
+ },
+ pipeline_workflows::{
+ EntryAddition, EntryQuery, FieldInput, FieldKind, FieldValue, MAX_SCANNED_ROWS,
+ NewPipeline, PipelineEdit, PipelineField, PipelineOverview, PipelineWorkflowService,
+ RecordTarget, WorkflowError,
+ },
+ pipelines::{PipelineRecordType, PipelineSharing},
+};
+
+pub use models::{
+ CompanyImportOutcome, Created, CsvFile, CsvFiles, CsvInput, PipelineDestination,
+ PipelineExport, PipelineImportOutcome, SkippedRecord, TransferError, TransferRecords,
+};
+pub use records::CrmTransferRecords;
+
+/// Records created at once; enough for thousands of rows, gentle on the CRM.
+const CONCURRENCY: usize = 4;
+/// Entries written per pipeline write.
+const BATCH: usize = 500;
+/// A record's id and whether the import created it, or why it has none.
+type Found = Result<(Uuid, bool), String>;
+
+/// The name of a pipeline made for a companies CSV given as text.
+const DEFAULT_PIPELINE_NAME: &str = "Imported companies";
+
+/// CSV imports and exports for a viewer, who may be an agent acting for a user.
+pub trait CrmTransferService: Send + Sync + 'static {
+ /// Create the companies a CSV lists, then put its Stage, Owner and
+ /// Revenue values on entries of `destination`: by default a new private
+ /// pipeline, made only when the CSV has such values.
+ fn import_companies(
+ &self,
+ viewer: &Viewer,
+ csv: CsvInput,
+ destination: Option<PipelineDestination>,
+ ) -> impl Future<Output = Result<CompanyImportOutcome, TransferError>> + Send;
+
+ /// Add a CSV's rows to a pipeline as entries, creating the companies or
+ /// contacts the team lacks. Records the pipeline already had are not
+ /// added again; repeated rows for a new record add separate entries.
+ fn import_to_pipeline(
+ &self,
+ viewer: &Viewer,
+ pipeline: Uuid,
+ csv: CsvInput,
+ ) -> impl Future<Output = Result<PipelineImportOutcome, TransferError>> + Send;
+
+ /// Save every entry of a pipeline as a CSV file in the viewer's files.
+ fn export_pipeline(
+ &self,
+ viewer: &Viewer,
+ pipeline: Uuid,
+ ) -> impl Future<Output = Result<PipelineExport, TransferError>> + Send;
+}
+
+/// [`CrmTransferService`] over the pipeline workflows, the CRM's records and
+/// Macro's files.
+pub struct CrmTransferServiceImpl<Workflows, Records, Files> {
+ workflows: Arc<Workflows>,
+ records: Records,
+ files: Files,
+}
+
+impl<Workflows, Records, Files> CrmTransferServiceImpl<Workflows, Records, Files> {
+ /// Imports and exports through these services.
+ pub fn new(workflows: Arc<Workflows>, records: Records, files: Files) -> Self {
+ Self {
+ workflows,
+ records,
+ files,
+ }
+ }
+}
+
+/// Run `futures` at most [`CONCURRENCY`] at a time. Built eagerly by the
+/// caller (they do nothing until polled), so no closure here borrows across
+/// the await.
+async fn bounded<F: Future>(futures: Vec<F>) -> Vec<F::Output> {
+ stream::iter(futures)
+ .buffer_unordered(CONCURRENCY)
+ .collect()
+ .await
+}
+
+/// The domain of an email address.
+fn email_domain(email: &str) -> Option<&str> {
+ email.split_once('@').map(|(_, domain)| domain)
+}
+
+/// The headers that name a record, so are never matched to a field.
+fn record_headers(record_type: PipelineRecordType) -> [&'static str; 2] {
+ match record_type {
+ PipelineRecordType::Company => ["Company", "Domain"],
+ PipelineRecordType::Contact => ["Contact", "Email"],
+ }
+}
+
+/// Fields an import can fill from text: stored values that carry across
+/// teams. References to records and other items hold ids another workspace
+/// would not have.
+fn importable(field: &PipelineField) -> bool {
+ field.writable
+ && matches!(
+ field.kind,
+ FieldKind::Text
+ | FieldKind::Number
+ | FieldKind::Checkbox
+ | FieldKind::Date
+ | FieldKind::Link
+ | FieldKind::Select
+ | FieldKind::Tags
+ | FieldKind::Person
+ )
+}
+
+/// A cell's value as the CSV writes it, so an import reads it back.
+fn exported_value(value: &FieldValue) -> String {
+ match value {
+ FieldValue::Text(text) => text.clone(),
+ FieldValue::Number(number) if number.fract() == 0.0 && number.abs() < 1e15 => {
+ format!("{number:.0}")
+ }
+ FieldValue::Number(number) => number.to_string(),
+ FieldValue::Checkbox(checked) => checked.to_string(),
+ FieldValue::Date(date) => date.to_rfc3339(),
+ FieldValue::Labels(values) | FieldValue::Links(values) => values.join(", "),
+ // People leave as emails, which another team can match.
+ FieldValue::References(ids) => ids
+ .iter()
+ .map(|id| id.strip_prefix("macro|").unwrap_or(id))
+ .collect::<Vec<_>>()
+ .join(", "),
+ }
+}
+
+/// What a row writes into `field`; several values only for a field that holds several.
+fn field_input(field: &PipelineField, value: &str) -> FieldInput {
+ if field.multiple {
+ FieldInput::Many(
+ value
+ .split(',')
+ .map(str::trim)
+ .filter(|part| !part.is_empty())
+ .map(str::to_owned)
+ .collect(),
+ )
+ } else {
+ FieldInput::One(value.to_owned())
+ }
+}
+
+/// Labels the rows name that a select field lacks, per field, first spelling kept.
+fn missing_options(
+ fields: &[(String, &PipelineField)],
+ rows: &[RecordRow],
+) -> Vec<(String, Vec<String>)> {
+ fields
+ .iter()
+ .filter(|(_, field)| matches!(field.kind, FieldKind::Select | FieldKind::Tags))
+ .filter_map(|(header, field)| {
+ let mut known: HashSet<String> = field
+ .options
+ .iter()
+ .map(|(_, label)| label.to_lowercase())
+ .collect();
+ let mut added = Vec::new();
+ for row in rows {
+ let Some((_, value)) = row.values.iter().find(|(h, _)| h == header) else {
+ continue;
+ };
+ let labels = match field_input(field, value) {
+ FieldInput::Many(labels) => labels,
+ FieldInput::One(label) => vec![label],
+ FieldInput::Clear => Vec::new(),
+ };
+ for label in labels {
+ if known.insert(label.to_lowercase()) {
+ added.push(label);
+ }
+ }
+ }
+ (!added.is_empty()).then(|| (field.name.clone(), added))
+ })
+ .collect()
+}
+
+impl<Workflows, Records, Files> CrmTransferServiceImpl<Workflows, Records, Files>
+where
+ Workflows: PipelineWorkflowService,
+ Records: TransferRecords,
+ Files: CsvFiles,
+{
+ async fn read(
+ &self,
+ viewer: &Viewer,
+ csv: CsvInput,
+ ) -> Result<(CsvTable, Option<String>), TransferError> {
+ match csv {
+ CsvInput::Text(text) => Ok((parse_csv(&text)?, None)),
+ CsvInput::File(document) => {
+ let file = self.files.read(viewer, document).await?;
+ Ok((parse_csv(&file.text)?, Some(file.name)))
+ }
+ }
+ }
+
+ async fn pipeline(&self, viewer: &Viewer, id: Uuid) -> Result<PipelineOverview, TransferError> {
+ self.workflows
+ .pipelines(viewer)
+ .await?
+ .into_iter()
+ .find(|overview| overview.pipeline.pipeline.id == id)
+ .ok_or_else(|| {
+ TransferError::Invalid(format!(
+ "{id} is not a pipeline the user can open. Find pipeline ids with ListPipelines."
+ ))
+ })
+ }
+
+ /// The company for `domain`, created as `name` when the team lacks it.
+ async fn company(
+ &self,
+ viewer: &Viewer,
+ domains: &SharedDomains,
+ name: &str,
+ domain: &str,
+ ) -> Result<Found, TransferError> {
+ if let Some(id) = domains.get(domain) {
+ return Ok(Ok((id, false)));
+ }
+ Ok(
+ match self.records.create_company(viewer, name, domain).await? {
+ Created::New(id) => {
+ domains.insert(domain, id);
+ Ok((id, true))
+ }
+ Created::Exists => {
+ Err("the team already tracks this domain on a hidden company".into())
+ }
+ Created::Refused(reason) => Err(reason),
+ },
+ )
+ }
+
+ /// The record each row names, by key, creating the ones the team lacks.
+ async fn resolve(
+ &self,
+ viewer: &Viewer,
+ record_type: PipelineRecordType,
+ rows: &[RecordRow],
+ domains: &SharedDomains,
+ ) -> Result<(HashMap<String, Uuid>, usize, Vec<SkippedRecord>), TransferError> {
+ let mut seen = HashSet::new();
+ let unique: Vec<&RecordRow> = rows
+ .iter()
+ .filter(|row| seen.insert(row.key.as_str()))
+ .collect();
+ let found = match record_type {
+ PipelineRecordType::Company => self.companies(viewer, &unique, domains).await?,
+ PipelineRecordType::Contact => self.contacts(viewer, &unique, domains).await?,
+ };
+ let mut ids = HashMap::new();
+ let mut created = 0;
+ let mut unresolved = Vec::new();
+ for (key, result) in found {
+ match result {
+ Ok((id, new)) => {
+ created += usize::from(new);
+ ids.insert(key, id);
+ }
+ Err(reason) => unresolved.push(SkippedRecord { key, reason }),
+ }
+ }
+ unresolved.sort_by(|a, b| a.key.cmp(&b.key));
+ Ok((ids, created, unresolved))
+ }
+
+ /// Each row's company, created when the team lacks it. Rows name
+ /// distinct domains, so concurrent creations never race on one.
+ async fn companies(
+ &self,
+ viewer: &Viewer,
+ rows: &[&RecordRow],
+ domains: &SharedDomains,
+ ) -> Result<Vec<(String, Found)>, TransferError> {
+ let found = bounded(
+ rows.iter()
+ .map(|row| async move {
+ let found = self.company(viewer, domains, &row.name, &row.key).await?;
+ Ok::<_, TransferError>((row.key.clone(), found))
+ })
+ .collect(),
+ )
+ .await;
+ found.into_iter().collect()
+ }
+
+ /// Each row's contact: found by email, or created under its domain's
+ /// company. Missing companies are created first, once per domain, so
+ /// contacts at one new domain share it.
+ async fn contacts(
+ &self,
+ viewer: &Viewer,
+ rows: &[&RecordRow],
+ domains: &SharedDomains,
+ ) -> Result<Vec<(String, Found)>, TransferError> {
+ let lookups = bounded(
+ rows.iter()
+ .map(|row| async move {
+ let id = self.records.contact(viewer, &row.key).await?;
+ Ok::<_, TransferError>((*row, id))
+ })
+ .collect(),
+ )
+ .await
+ .into_iter()
+ .collect::<Result<Vec<(&RecordRow, Option<Uuid>)>, _>>()?;
+
+ let mut new_domains: Vec<String> = lookups
+ .iter()
+ .filter(|(_, id)| id.is_none())
+ .filter_map(|(row, _)| email_domain(&row.key).map(str::to_owned))
+ .filter(|domain| domains.get(domain).is_none())
+ .collect::<HashSet<_>>()
+ .into_iter()
+ .collect();
+ new_domains.sort();
+ let mut refused = HashMap::new();
+ for result in bounded(
+ new_domains
+ .iter()
+ .map(|domain| async move {
+ let found = self.company(viewer, domains, domain, domain).await?;
+ Ok::<_, TransferError>((domain.clone(), found.err()))
+ })
+ .collect(),
+ )
+ .await
+ {
+ if let (domain, Some(reason)) = result? {
+ refused.insert(domain, reason);
+ }
+ }
+
+ let refused = &refused;
+ let found = bounded(
+ lookups
+ .iter()
+ .map(|&(row, existing)| async move {
+ if let Some(id) = existing {
+ return Ok((row.key.clone(), Ok((id, false))));
+ }
+ let domain = email_domain(&row.key).unwrap_or_default();
+ let Some(company) = domains.get(domain) else {
+ let reason = refused
+ .get(domain)
+ .map_or("it could not be created", String::as_str);
+ return Ok((
+ row.key.clone(),
+ Err(format!("its company could not be added: {reason}")),
+ ));
+ };
+ let found = match self
+ .records
+ .create_contact(viewer, company, &row.name, &row.key)
+ .await?
+ {
+ Created::New(id) => Ok((id, true)),
+ Created::Exists => {
+ Err("the contact exists where the user cannot see it".into())
+ }
+ Created::Refused(reason) => Err(reason),
+ };
+ Ok::<_, TransferError>((row.key.clone(), found))
+ })
+ .collect(),
+ )
+ .await;
+ found.into_iter().collect()
+ }
+
+ /// Add `rows` to `overview`'s pipeline: the shared half of both imports.
+ async fn fill(
+ &self,
+ viewer: &Viewer,
+ overview: &PipelineOverview,
+ rows: &[RecordRow],
+ other_headers: &[String],
+ skipped_rows: usize,
+ domains: &SharedDomains,
+ ) -> Result<PipelineImportOutcome, TransferError> {
+ let pipeline = &overview.pipeline.pipeline;
+ let record_type = pipeline.record_type;
+ let reserved = record_headers(record_type);
+ let mut fields: Vec<(String, &PipelineField)> = Vec::new();
+ let mut ignored_columns = Vec::new();
+ for header in other_headers {
+ if reserved
+ .iter()
+ .any(|name| name.eq_ignore_ascii_case(header))
+ {
+ continue;
+ }
+ match overview.fields.iter().find(|field| {
+ !field.is_record() && importable(field) && field.name.eq_ignore_ascii_case(header)
+ }) {
+ Some(field) => fields.push((header.clone(), field)),
+ None => ignored_columns.push(header.clone()),
+ }
+ }
+
+ let (ids, created_records, unresolved) =
+ self.resolve(viewer, record_type, rows, domains).await?;
+ let record_ids: Vec<Uuid> = ids
+ .values()
+ .copied()
+ .collect::<HashSet<_>>()
+ .into_iter()
+ .collect();
+ let present: HashSet<Uuid> = if record_ids.is_empty() {
+ HashSet::new()
+ } else {
+ self.workflows
+ .record_entries(viewer, &record_ids)
+ .await?
+ .into_iter()
+ .filter(|found| found.pipeline.pipeline.id == pipeline.id)
+ .flat_map(|found| found.entries.into_iter().map(|entry| entry.record.id))
+ .collect()
+ };
+
+ let mut already_present = 0;
+ let mut additions = Vec::new();
+ let mut keys = Vec::new();
+ let mut new_rows = Vec::new();
+ for row in rows {
+ let Some(&id) = ids.get(&row.key) else {
+ continue;
+ };
+ if present.contains(&id) {
+ already_present += 1;
+ continue;
+ }
+ let set = fields
+ .iter()
+ .filter_map(|(header, field)| {
+ row.values
+ .iter()
+ .find(|(h, _)| h == header)
+ .map(|(_, value)| (field.name.clone(), field_input(field, value)))
+ })
+ .collect();
+ additions.push(EntryAddition {
+ record: RecordTarget::Id(id),
+ set,
+ });
+ keys.push(row.key.clone());
+ new_rows.push(row.clone());
+ }
+
+ let added_options = missing_options(&fields, &new_rows);
+ if !added_options.is_empty() {
+ self.workflows
+ .edit(
+ viewer,
+ pipeline.id,
+ PipelineEdit {
+ add_options: added_options.clone(),
+ ..PipelineEdit::default()
+ },
+ )
+ .await?;
+ }
+
+ let (added, invalid) = self.add(viewer, pipeline.id, additions, keys).await?;
+ Ok(PipelineImportOutcome {
+ pipeline_id: pipeline.id,
+ pipeline_name: pipeline.name.clone(),
+ added,
+ already_present,
+ created_records,
+ unresolved,
+ invalid,
+ skipped_rows,
+ filled_fields: fields.iter().map(|(_, field)| field.name.clone()).collect(),
+ ignored_columns,
+ added_options,
+ })
+ }
+
+ /// Write `additions` in batches. A batch refused for a bad value is
+ /// retried row by row, so one bad row never costs the others.
+ async fn add(
+ &self,
+ viewer: &Viewer,
+ pipeline: Uuid,
+ additions: Vec<EntryAddition>,
+ keys: Vec<String>,
+ ) -> Result<(usize, Vec<SkippedRecord>), TransferError> {
+ let mut added = 0;
+ let mut invalid = Vec::new();
+ for (batch, batch_keys) in additions.chunks(BATCH).zip(keys.chunks(BATCH)) {
+ match self
+ .workflows
+ .add(viewer, pipeline, batch.to_vec(), true)
+ .await
+ {
+ Ok(result) => added += result.added.len(),
+ Err(WorkflowError::Invalid(_)) => {
+ for (addition, key) in batch.iter().zip(batch_keys) {
+ match self
+ .workflows
+ .add(viewer, pipeline, vec![addition.clone()], true)
+ .await
+ {
+ Ok(result) => added += result.added.len(),
+ Err(WorkflowError::Invalid(reason)) => invalid.push(SkippedRecord {
+ key: key.clone(),
+ reason,
+ }),
+ Err(error) => return Err(error.into()),
+ }
+ }
+ }
+ Err(error) => return Err(error.into()),
+ }
+ }
+ Ok((added, invalid))
+ }
+}
+
+impl<Workflows, Records, Files> CrmTransferService
+ for CrmTransferServiceImpl<Workflows, Records, Files>
+where
+ Workflows: PipelineWorkflowService,
+ Records: TransferRecords,
+ Files: CsvFiles,
+{
+ #[tracing::instrument(skip_all, err)]
+ async fn import_companies(
+ &self,
+ viewer: &Viewer,
+ csv: CsvInput,
+ destination: Option<PipelineDestination>,
+ ) -> Result<CompanyImportOutcome, TransferError> {
+ let (table, file_name) = self.read(viewer, csv).await?;
+ let parsed = record_rows(&table, PipelineRecordType::Company)?;
+ if parsed.rows.is_empty() {
+ return Err(TransferError::Invalid(
+ "No rows have a domain like acme.com.".into(),
+ ));
+ }
+ tracing::info!(rows = parsed.rows.len(), "importing companies");
+ let domains = SharedDomains::new(self.records.company_domains(viewer).await?);
+ let companies = unique_companies(&parsed.rows);
+ let results = self.companies(viewer, &companies, &domains).await?;
+ let mut created = 0;
+ let mut existing = 0;
+ let mut failed = Vec::new();
+ for result in results {
+ match result {
+ (_, Ok((_, true))) => created += 1,
+ (_, Ok((_, false))) => existing += 1,
+ (key, Err(reason)) => failed.push(SkippedRecord { key, reason }),
+ }
+ }
+ failed.sort_by(|a, b| a.key.cmp(&b.key));
+
+ let pipeline_rows = company_pipeline_rows(&parsed.rows);
+ let pipeline = if pipeline_rows.is_empty() {
+ None
+ } else {
+ let overview = match destination.unwrap_or(PipelineDestination::New {
+ name: None,
+ sharing: PipelineSharing::Private,
+ }) {
+ PipelineDestination::Existing(id) => {
+ let overview = self.pipeline(viewer, id).await?;
+ if overview.pipeline.pipeline.record_type != PipelineRecordType::Company {
+ return Err(TransferError::Invalid(format!(
+ "\"{}\" tracks contacts; choose a company pipeline for Stage, Owner and Revenue.",
+ overview.pipeline.pipeline.name
+ )));
+ }
+ overview
+ }
+ PipelineDestination::New { name, sharing } => {
+ self.workflows
+ .create(
+ viewer,
+ NewPipeline {
+ name: name
+ .or(file_name)
+ .filter(|name| !name.trim().is_empty())
+ .unwrap_or_else(|| DEFAULT_PIPELINE_NAME.to_owned()),
+ record_type: PipelineRecordType::Company,
+ sharing,
+ stages: Vec::new(),
+ fields: Vec::new(),
+ },
+ )
+ .await?
+ }
+ };
+ // Only the pipeline columns some row fills.
+ let headers: Vec<String> = parsed
+ .other_headers
+ .iter()
+ .filter(|header| {
+ pipeline_rows
+ .iter()
+ .any(|row| row.values.iter().any(|(h, _)| h == *header))
+ })
+ .cloned()
+ .collect();
+ Some(
+ self.fill(viewer, &overview, &pipeline_rows, &headers, 0, &domains)
+ .await?,
+ )
+ };
+ Ok(CompanyImportOutcome {
+ created,
+ existing,
+ skipped_rows: parsed.skipped,
+ failed,
+ pipeline,
+ })
+ }
+
+ #[tracing::instrument(skip_all, fields(pipeline = %pipeline), err)]
+ async fn import_to_pipeline(
+ &self,
+ viewer: &Viewer,
+ pipeline: Uuid,
+ csv: CsvInput,
+ ) -> Result<PipelineImportOutcome, TransferError> {
+ let overview = self.pipeline(viewer, pipeline).await?;
+ let (table, _) = self.read(viewer, csv).await?;
+ let parsed = record_rows(&table, overview.pipeline.pipeline.record_type)?;
+ if parsed.rows.is_empty() {
+ return Err(TransferError::Invalid(
+ "No rows name a company by domain or a contact by email.".into(),
+ ));
+ }
+ tracing::info!(rows = parsed.rows.len(), "importing pipeline entries");
+ let domains = SharedDomains::new(self.records.company_domains(viewer).await?);
+ self.fill(
+ viewer,
+ &overview,
+ &parsed.rows,
+ &parsed.other_headers,
+ parsed.skipped,
+ &domains,
+ )
+ .await
+ }
+
+ #[tracing::instrument(skip_all, fields(pipeline = %pipeline), err)]
+ async fn export_pipeline(
+ &self,
+ viewer: &Viewer,
+ pipeline: Uuid,
+ ) -> Result<PipelineExport, TransferError> {
+ let found = self
+ .workflows
+ .find(
+ viewer,
+ pipeline,
+ EntryQuery {
+ limit: MAX_SCANNED_ROWS,
+ ..EntryQuery::default()
+ },
+ )
+ .await?;
+ let overview = &found.pipeline;
+ let fields: Vec<&PipelineField> = overview
+ .fields
+ .iter()
+ .filter(|field| !field.is_record())
+ .collect();
+ let headers: Vec<String> = record_headers(overview.pipeline.pipeline.record_type)
+ .iter()
+ .map(|header| (*header).to_owned())
+ .chain(fields.iter().map(|field| field.name.clone()))
+ .collect();
+ let rows: Vec<Vec<String>> = found
+ .entries
+ .iter()
+ .map(|entry| {
+ [
+ entry.record.name.clone().unwrap_or_default(),
+ entry.record.detail.clone().unwrap_or_default(),
+ ]
+ .into_iter()
+ .chain(fields.iter().map(|field| {
+ entry
+ .fields
+ .iter()
+ .find(|(name, _)| name == &field.name)
+ .map(|(_, value)| exported_value(value))
+ .unwrap_or_default()
+ }))
+ .collect()
+ })
+ .collect();
+ let file_name = overview.pipeline.pipeline.name.clone();
+ let document_id = self
+ .files
+ .save(viewer, &file_name, encode_csv(&headers, &rows))
+ .await?;
+ Ok(PipelineExport {
+ document_id,
+ file_name,
+ rows: rows.len(),
+ complete: found.complete,
+ })
+ }
+}
+
+/// Company ids by domain, shared by the concurrent creations of one import
+/// so a company created for one row is found by the rows after it.
+struct SharedDomains(Mutex<HashMap<String, Uuid>>);
+
+impl SharedDomains {
+ fn new(domains: HashMap<String, Uuid>) -> Self {
+ Self(Mutex::new(domains))
+ }
+
+ fn get(&self, domain: &str) -> Option<Uuid> {
+ self.0
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .get(domain)
+ .copied()
+ }
+
+ fn insert(&self, domain: &str, id: Uuid) {
+ self.0
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .insert(domain.to_owned(), id);
+ }
+}
diff --git a/crates/crm/src/domain/crm_transfer/models.rs b/crates/crm/src/domain/crm_transfer/models.rs
new file mode 100644
index 0000000000..ba55903937
--- /dev/null
+++ b/crates/crm/src/domain/crm_transfer/models.rs
@@ -0,0 +1,199 @@
+//! What CSV imports and exports take and return, and the ports they use.
+
+use databases::domain::models::Viewer;
+use entity_access::domain::models::AccessError;
+use uuid::Uuid;
+
+use super::super::{
+ crm_csv::CsvError, model::CrmError, pipeline_workflows::WorkflowError,
+ pipelines::PipelineSharing,
+};
+
+/// Where an import reads its CSV from.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum CsvInput {
+ /// The CSV text itself.
+ Text(String),
+ /// A CSV file in Macro the viewer can open.
+ File(Uuid),
+}
+
+/// The pipeline a companies import puts Stage, Owner and Revenue into.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum PipelineDestination {
+ /// An existing company pipeline the viewer can edit.
+ Existing(Uuid),
+ /// A new company pipeline; unnamed, it is named after the file.
+ New {
+ /// Its name.
+ name: Option<String>,
+ /// Whether the team may edit it.
+ sharing: PipelineSharing,
+ },
+}
+
+/// A record an import could not add, and why.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SkippedRecord {
+ /// The company's domain or the contact's email.
+ pub key: String,
+ /// What went wrong, in words the user can act on.
+ pub reason: String,
+}
+
+/// What a companies import did.
+#[derive(Debug, Clone, PartialEq)]
+pub struct CompanyImportOutcome {
+ /// Companies created.
+ pub created: usize,
+ /// Companies the team already had.
+ pub existing: usize,
+ /// Rows without a domain like acme.com.
+ pub skipped_rows: usize,
+ /// Companies that could not be created.
+ pub failed: Vec<SkippedRecord>,
+ /// The pipeline entries made from Stage, Owner and Revenue, when the
+ /// CSV had any.
+ pub pipeline: Option<PipelineImportOutcome>,
+}
+
+/// What a pipeline import did.
+#[derive(Debug, Clone, PartialEq)]
+pub struct PipelineImportOutcome {
+ /// The pipeline filled.
+ pub pipeline_id: Uuid,
+ /// Its name.
+ pub pipeline_name: String,
+ /// Entries added.
+ pub added: usize,
+ /// Rows whose record the pipeline already had before the import.
+ pub already_present: usize,
+ /// Companies or contacts the import created to add their entries.
+ pub created_records: usize,
+ /// Records that could not be found or created.
+ pub unresolved: Vec<SkippedRecord>,
+ /// Rows refused for a value their field cannot hold.
+ pub invalid: Vec<SkippedRecord>,
+ /// Rows without a domain or email.
+ pub skipped_rows: usize,
+ /// Pipeline fields the CSV filled.
+ pub filled_fields: Vec<String>,
+ /// CSV columns that match no field the import can fill.
+ pub ignored_columns: Vec<String>,
+ /// Options added to select fields so the CSV's values fit: field, labels.
+ pub added_options: Vec<(String, Vec<String>)>,
+}
+
+/// A pipeline saved as a CSV file.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct PipelineExport {
+ /// The CSV file in Macro.
+ pub document_id: Uuid,
+ /// The file's name.
+ pub file_name: String,
+ /// Entries written.
+ pub rows: usize,
+ /// Whether every entry was written; a pipeline beyond the read limit
+ /// is cut there.
+ pub complete: bool,
+}
+
+/// A CSV file read from Macro.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct CsvFile {
+ /// The file's name, without its extension.
+ pub name: String,
+ /// Its text.
+ pub text: String,
+}
+
+/// What creating a company or contact did.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum Created {
+ /// The new record's id.
+ New(Uuid),
+ /// The team already has it, where the viewer cannot see it.
+ Exists,
+ /// The CRM refused it; the reason says why.
+ Refused(String),
+}
+
+/// Why an import or export did not run.
+#[derive(Debug, thiserror::Error)]
+pub enum TransferError {
+ /// The request or its CSV is unusable; the message says what to change.
+ #[error("{0}")]
+ Invalid(String),
+ /// A CSV file could not be read or saved; the message says why.
+ #[error("{0}")]
+ File(String),
+ /// A pipeline workflow refused or failed.
+ #[error(transparent)]
+ Workflow(#[from] WorkflowError),
+ /// A CRM read or write failed.
+ #[error(transparent)]
+ Crm(#[from] CrmError),
+ /// Access could not be established.
+ #[error(transparent)]
+ Access(#[from] AccessError),
+}
+
+impl From<CsvError> for TransferError {
+ fn from(error: CsvError) -> Self {
+ match error {
+ CsvError::Invalid(message) => Self::Invalid(message),
+ }
+ }
+}
+
+/// Finds and creates the companies and contacts an import names, as the
+/// viewer.
+pub trait TransferRecords: Send + Sync + 'static {
+ /// Every company id the viewer's team CRM tracks, by each of its domains.
+ fn company_domains(
+ &self,
+ viewer: &Viewer,
+ ) -> impl Future<Output = Result<std::collections::HashMap<String, Uuid>, TransferError>> + Send;
+
+ /// Create a company named `name` for `domain`.
+ fn create_company(
+ &self,
+ viewer: &Viewer,
+ name: &str,
+ domain: &str,
+ ) -> impl Future<Output = Result<Created, TransferError>> + Send;
+
+ /// The id of the team's contact with `email`.
+ fn contact(
+ &self,
+ viewer: &Viewer,
+ email: &str,
+ ) -> impl Future<Output = Result<Option<Uuid>, TransferError>> + Send;
+
+ /// Create a contact named `name` with `email` under `company`.
+ fn create_contact(
+ &self,
+ viewer: &Viewer,
+ company: Uuid,
+ name: &str,
+ email: &str,
+ ) -> impl Future<Output = Result<Created, TransferError>> + Send;
+}
+
+/// Reads and saves CSV files in Macro, as the viewer.
+pub trait CsvFiles: Send + Sync + 'static {
+ /// The CSV file `document` the viewer can open.
+ fn read(
+ &self,
+ viewer: &Viewer,
+ document: Uuid,
+ ) -> impl Future<Output = Result<CsvFile, TransferError>> + Send;
+
+ /// Save `text` as a CSV file named `name` in the viewer's files.
+ fn save(
+ &self,
+ viewer: &Viewer,
+ name: &str,
+ text: String,
+ ) -> impl Future<Output = Result<Uuid, TransferError>> + Send;
+}
diff --git a/crates/crm/src/domain/crm_transfer/records.rs b/crates/crm/src/domain/crm_transfer/records.rs
new file mode 100644
index 0000000000..d2793ca8d4
--- /dev/null
+++ b/crates/crm/src/domain/crm_transfer/records.rs
@@ -0,0 +1,184 @@
+//! [`TransferRecords`] over the CRM service: the viewer's team CRM, with the
+//! same receipts and refusals as the single-record tools.
+
+use std::collections::HashMap;
+use std::sync::Arc;
+
+use databases::domain::models::Viewer;
+use entity_access::domain::{
+ models::{AccessError, Entity, EntityAccessReceipt, EntityPermission, EntityType},
+ ports::EntityAccessService,
+};
+use uuid::Uuid;
+
+use super::super::{
+ auth::{CrmCompanyReceipt, CrmTeamReceipt},
+ companies_repo::{CrmCompanyListSort, CrmCompanySoupCursor},
+ model::CrmError,
+ service::CrmService,
+};
+use super::models::{Created, TransferError, TransferRecords};
+use entity_access::domain::models::{MemberTeamRole, ViewAccessLevel};
+
+/// Companies read per page while matching domains.
+const PAGE: i64 = 500;
+
+/// [`TransferRecords`] through [`CrmService`].
+pub struct CrmTransferRecords<Crm, Access> {
+ crm: Arc<Crm>,
+ access: Arc<Access>,
+}
+
+impl<Crm, Access> CrmTransferRecords<Crm, Access> {
+ /// Records through `crm`, authorized by `access`.
+ pub fn new(crm: Arc<Crm>, access: Arc<Access>) -> Self {
+ Self { crm, access }
+ }
+}
+
+impl<Crm: CrmService, Access: EntityAccessService> CrmTransferRecords<Crm, Access> {
+ async fn team(&self, viewer: &Viewer) -> Result<CrmTeamReceipt<MemberTeamRole>, TransferError> {
+ let team = self
+ .access
+ .get_user_team(&viewer.user_id)
+ .await?
+ .ok_or_else(|| {
+ TransferError::Invalid("The user is not in a team, so they have no CRM.".into())
+ })?;
+ let receipt = EntityAccessReceipt::try_new_authenticated_user(
+ viewer.user_id.clone(),
+ Entity {
+ entity_id: team.team_id.to_string(),
+ entity_type: EntityType::Team,
+ },
+ EntityPermission::TeamRole { role: team.role },
+ )?;
+ Ok(CrmTeamReceipt::from_team_receipt(receipt)?)
+ }
+}
+
+/// A refusal the user can fix by changing the row, as opposed to a failure.
+fn refusal(error: CrmError) -> Result<Created, TransferError> {
+ match error {
+ CrmError::CompanyAlreadyExistsForTeam | CrmError::ContactAlreadyExistsForCompany => {
+ Ok(Created::Exists)
+ }
+ CrmError::InvalidRequest(reason) => Ok(Created::Refused(reason)),
+ CrmError::ContactEmailDomainMismatch => Ok(Created::Refused(
+ "the email's domain is not one of its company's domains".into(),
+ )),
+ CrmError::CompanyHidden => Ok(Created::Refused("its company is hidden".into())),
+ other => Err(other.into()),
+ }
+}
+
+impl<Crm: CrmService, Access: EntityAccessService> TransferRecords
+ for CrmTransferRecords<Crm, Access>
+{
+ async fn company_domains(
+ &self,
+ viewer: &Viewer,
+ ) -> Result<HashMap<String, Uuid>, TransferError> {
+ let team = self.team(viewer).await?;
+ let mut domains = HashMap::new();
+ let mut cursor = None;
+ loop {
+ let page = self
+ .crm
+ .list_companies_for_soup(
+ &team,
+ viewer.user_id.as_ref(),
+ &[],
+ Some(false),
+ CrmCompanyListSort::UpdatedAt,
+ cursor,
+ PAGE,
+ )
+ .await?;
+ for company in &page {
+ for domain in &company.company.domains {
+ domains.insert(domain.domain.to_ascii_lowercase(), company.company.id);
+ }
+ }
+ match page.last() {
+ Some(last) if page.len() as i64 == PAGE => {
+ cursor = Some(CrmCompanySoupCursor {
+ last_sort_ts: last.company.updated_at,
+ last_id: last.company.id,
+ });
+ }
+ _ => return Ok(domains),
+ }
+ }
+ }
+
+ async fn create_company(
+ &self,
+ viewer: &Viewer,
+ name: &str,
+ domain: &str,
+ ) -> Result<Created, TransferError> {
+ let team = self.team(viewer).await?;
+ match self.crm.create_company(&team, name, domain).await {
+ Ok(company) => Ok(Created::New(company.company.id)),
+ Err(error) => refusal(error),
+ }
+ }
+
+ async fn contact(&self, viewer: &Viewer, email: &str) -> Result<Option<Uuid>, TransferError> {
+ let team = self.team(viewer).await?;
+ Ok(self
+ .crm
+ .get_contact_by_email(&team, email)
+ .await?
+ .map(|contact| contact.id))
+ }
+
+ async fn create_contact(
+ &self,
+ viewer: &Viewer,
+ company: Uuid,
+ name: &str,
+ email: &str,
+ ) -> Result<Created, TransferError> {
+ let (permission, team_id, team_role) = match self
+ .access
+ .get_crm_entity_permission_with_team(
+ Some(&viewer.user_id),
+ &company.to_string(),
+ EntityType::CrmCompany,
+ )
+ .await
+ {
+ Ok(found) => found,
+ Err(
+ AccessError::NotFound(_)
+ | AccessError::Unauthorized
+ | AccessError::UnauthorizedWithMessage(_),
+ ) => {
+ return Ok(Created::Refused(
+ "its company is not visible to the user".into(),
+ ));
+ }
+ Err(error) => return Err(error.into()),
+ };
+ if !permission.satisfies::<ViewAccessLevel>() {
+ return Ok(Created::Refused(
+ "its company is not visible to the user".into(),
+ ));
+ }
+ let receipt = EntityAccessReceipt::try_new_authenticated_user(
+ viewer.user_id.clone(),
+ Entity {
+ entity_id: company.to_string(),
+ entity_type: EntityType::CrmCompany,
+ },
+ permission,
+ )?;
+ let receipt = CrmCompanyReceipt::new(receipt, team_id, team_role);
+ match self.crm.create_contact(&receipt, name, email).await {
+ Ok(contact) => Ok(Created::New(contact.id)),
+ Err(error) => refusal(error),
+ }
+ }
+}
diff --git a/crates/crm/src/domain/crm_transfer/test.rs b/crates/crm/src/domain/crm_transfer/test.rs
new file mode 100644
index 0000000000..8762282de2
--- /dev/null
+++ b/crates/crm/src/domain/crm_transfer/test.rs
@@ -0,0 +1,508 @@
+use std::collections::HashMap;
+use std::sync::Mutex;
+
+use chrono::{TimeZone, Utc};
+use databases::domain::models::{ColumnId, DatabaseId, OptionId, RowId, TableId, TableVersion};
+use entity_access::domain::models::AccessLevel;
+use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
+
+use super::*;
+use crate::domain::{
+ pipeline_workflows::{
+ AddedEntries, EntryTarget, EntryUpdate, FoundEntries, PipelineEntryView, PipelineRecord,
+ RecordEntries, RemovedEntries, UpdatedEntries,
+ },
+ pipelines::{AccessiblePipeline, Pipeline},
+};
+
+fn viewer() -> Viewer {
+ Viewer {
+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")
+ .unwrap()
+ .into_owned(),
+ acting_bot: None,
+ }
+}
+
+fn field(name: &str, kind: FieldKind) -> PipelineField {
+ PipelineField {
+ id: ColumnId::new(),
+ name: name.into(),
+ kind,
+ multiple: false,
+ options: Vec::new(),
+ required: false,
+ writable: true,
+ }
+}
+
+fn overview(record_type: PipelineRecordType) -> PipelineOverview {
+ PipelineOverview {
+ pipeline: AccessiblePipeline {
+ pipeline: Pipeline {
+ id: Uuid::now_v7(),
+ team_id: Uuid::now_v7(),
+ user_id: "macro|[email protected]".into(),
+ name: "Deals".into(),
+ record_type,
+ database_id: DatabaseId::new(),
+ table_id: TableId::new(),
+ primary_column_id: ColumnId::new(),
+ sharing: PipelineSharing::Team,
+ created_at: Utc.with_ymd_and_hms(2026, 10, 1, 0, 0, 0).unwrap(),
+ trashed_at: None,
+ },
+ grant: AccessLevel::Owner,
+ },
+ fields: vec![
+ field("Company", FieldKind::Record(record_type)),
+ PipelineField {
+ options: vec![(OptionId::new(), "Lead".into())],
+ ..field("Stage", FieldKind::Select)
+ },
+ field("Owner", FieldKind::Person),
+ field("Revenue", FieldKind::Number),
+ field(
+ "Proposal",
+ FieldKind::Entity(models_databases::EntityKind::Document),
+ ),
+ ],
+ version: TableVersion(1),
+ }
+}
+
+fn entry(id: Uuid) -> PipelineEntryView {
+ PipelineEntryView {
+ entry_id: RowId::new(),
+ record: PipelineRecord {
+ id,
+ name: None,
+ detail: None,
+ },
+ fields: Vec::new(),
+ }
+}
+
+/// Pipelines in memory: one pipeline, entries for `present`, and a value
+/// `refused` that makes any batch holding it invalid.
+struct Workflows {
+ overview: PipelineOverview,
+ present: Vec<Uuid>,
+ refused: Option<&'static str>,
+ exported: Vec<PipelineEntryView>,
+ created: Mutex<Vec<NewPipeline>>,
+ edits: Mutex<Vec<PipelineEdit>>,
+ added: Mutex<Vec<EntryAddition>>,
+}
+
+impl Workflows {
+ fn new(overview: PipelineOverview) -> Self {
+ Self {
+ overview,
+ present: Vec::new(),
+ refused: None,
+ exported: Vec::new(),
+ created: Mutex::default(),
+ edits: Mutex::default(),
+ added: Mutex::default(),
+ }
+ }
+}
+
+impl PipelineWorkflowService for Workflows {
+ async fn pipelines(&self, _: &Viewer) -> Result<Vec<PipelineOverview>, WorkflowError> {
+ Ok(vec![self.overview.clone()])
+ }
+
+ async fn find(
+ &self,
+ _: &Viewer,
+ _: Uuid,
+ _: EntryQuery,
+ ) -> Result<FoundEntries, WorkflowError> {
+ Ok(FoundEntries {
+ pipeline: self.overview.clone(),
+ entries: self.exported.clone(),
+ matched: self.exported.len(),
+ complete: true,
+ counts: Vec::new(),
+ totals: Vec::new(),
+ })
+ }
+
+ async fn search_entries(
+ &self,
+ _: &Viewer,
+ _: &str,
+ _: usize,
+ ) -> Result<Vec<RecordEntries>, WorkflowError> {
+ unimplemented!("imports never search")
+ }
+
+ async fn record_entries(
+ &self,
+ _: &Viewer,
+ records: &[Uuid],
+ ) -> Result<Vec<RecordEntries>, WorkflowError> {
+ Ok(vec![RecordEntries {
+ pipeline: self.overview.pipeline.clone(),
+ entries: self
+ .present
+ .iter()
+ .filter(|id| records.contains(id))
+ .map(|id| entry(*id))
+ .collect(),
+ }])
+ }
+
+ async fn add(
+ &self,
+ _: &Viewer,
+ _: Uuid,
+ additions: Vec<EntryAddition>,
+ allow_duplicates: bool,
+ ) -> Result<AddedEntries, WorkflowError> {
+ assert!(allow_duplicates, "imports keep every row of a new record");
+ let refused = self.refused.is_some_and(|value| {
+ additions.iter().any(|addition| {
+ addition
+ .set
+ .iter()
+ .any(|(_, input)| *input == FieldInput::One(value.into()))
+ })
+ });
+ if refused {
+ return Err(WorkflowError::Invalid("\"Revenue\" takes a number.".into()));
+ }
+ let added = additions
+ .iter()
+ .map(|addition| match addition.record {
+ RecordTarget::Id(id) => entry(id),
+ RecordTarget::Email(_) => unreachable!("imports add records by id"),
+ })
+ .collect();
+ self.added.lock().unwrap().extend(additions);
+ Ok(AddedEntries {
+ added,
+ already_present: Vec::new(),
+ version: None,
+ })
+ }
+
+ async fn update(
+ &self,
+ _: &Viewer,
+ _: Uuid,
+ _: Vec<EntryUpdate>,
+ _: Option<TableVersion>,
+ ) -> Result<UpdatedEntries, WorkflowError> {
+ unimplemented!("imports never update")
+ }
+
+ async fn remove(
+ &self,
+ _: &Viewer,
+ _: Uuid,
+ _: Vec<EntryTarget>,
+ ) -> Result<RemovedEntries, WorkflowError> {
+ unimplemented!("imports never remove")
+ }
+
+ async fn create(
+ &self,
+ _: &Viewer,
+ input: NewPipeline,
+ ) -> Result<PipelineOverview, WorkflowError> {
+ let mut overview = self.overview.clone();
+ overview.pipeline.pipeline.name = input.name.clone();
+ self.created.lock().unwrap().push(input);
+ Ok(overview)
+ }
+
+ async fn edit(
+ &self,
+ _: &Viewer,
+ _: Uuid,
+ edit: PipelineEdit,
+ ) -> Result<PipelineOverview, WorkflowError> {
+ self.edits.lock().unwrap().push(edit);
+ Ok(self.overview.clone())
+ }
+}
+
+/// A team CRM in memory: companies by domain, contacts by email.
+#[derive(Default)]
+struct Records {
+ domains: HashMap<String, Uuid>,
+ contacts: HashMap<String, Uuid>,
+ companies_created: Mutex<Vec<String>>,
+ contacts_created: Mutex<Vec<(Uuid, String)>>,
+}
+
+impl TransferRecords for Records {
+ async fn company_domains(&self, _: &Viewer) -> Result<HashMap<String, Uuid>, TransferError> {
+ Ok(self.domains.clone())
+ }
+
+ async fn create_company(
+ &self,
+ _: &Viewer,
+ _: &str,
+ domain: &str,
+ ) -> Result<Created, TransferError> {
+ if domain == "gmail.com" {
+ return Ok(Created::Refused(
+ "gmail.com is a personal email domain".into(),
+ ));
+ }
+ let mut created = self.companies_created.lock().unwrap();
+ if created.iter().any(|known| known == domain) {
+ return Ok(Created::Exists);
+ }
+ created.push(domain.to_owned());
+ Ok(Created::New(Uuid::new_v5(
+ &Uuid::NAMESPACE_DNS,
+ domain.as_bytes(),
+ )))
+ }
+
+ async fn contact(&self, _: &Viewer, email: &str) -> Result<Option<Uuid>, TransferError> {
+ Ok(self.contacts.get(email).copied())
+ }
+
+ async fn create_contact(
+ &self,
+ _: &Viewer,
+ company: Uuid,
+ _: &str,
+ email: &str,
+ ) -> Result<Created, TransferError> {
+ self.contacts_created
+ .lock()
+ .unwrap()
+ .push((company, email.to_owned()));
+ Ok(Created::New(Uuid::new_v5(
+ &Uuid::NAMESPACE_OID,
+ email.as_bytes(),
+ )))
+ }
+}
+
+/// Files in memory: `file` to read, and what was saved.
+#[derive(Default)]
+struct Files {
+ file: Option<CsvFile>,
+ saved: Mutex<Vec<(String, String)>>,
+}
+
+impl CsvFiles for Files {
+ async fn read(&self, _: &Viewer, _: Uuid) -> Result<CsvFile, TransferError> {
+ self.file
+ .clone()
+ .ok_or_else(|| TransferError::File("no such file".into()))
+ }
+
+ async fn save(&self, _: &Viewer, name: &str, text: String) -> Result<Uuid, TransferError> {
+ self.saved.lock().unwrap().push((name.to_owned(), text));
+ Ok(Uuid::nil())
+ }
+}
+
+fn id_of(domain: &str) -> Uuid {
+ Uuid::new_v5(&Uuid::NAMESPACE_DNS, domain.as_bytes())
+}
+
+#[tokio::test]
+async fn a_companies_export_restores_stage_and_owner_on_a_new_private_pipeline() {
+ let acme = Uuid::now_v7();
+ let service = CrmTransferServiceImpl::new(
+ Arc::new(Workflows::new(overview(PipelineRecordType::Company))),
+ Records {
+ domains: HashMap::from([("acme.com".to_owned(), acme)]),
+ ..Records::default()
+ },
+ Files {
+ file: Some(CsvFile {
+ name: "Macro Customers Oct 16 2026".into(),
+ text: "Company,Domains,Stage,Owner,Revenue,Last interaction\n\
+ Acme,acme.com,Lead,,,2026-10-16\n\
+ Beta,beta.io,Churned,[email protected],,2026-10-15\n\
+ Gamma,gamma.dev,,,,2026-10-14\n\
+ Gmail,gmail.com,Lead,,,2026-10-13\n"
+ .into(),
+ }),
+ ..Files::default()
+ },
+ );
+ let outcome = service
+ .import_companies(&viewer(), CsvInput::File(Uuid::nil()), None)
+ .await
+ .unwrap();
+ assert_eq!((outcome.created, outcome.existing), (2, 1));
+ assert_eq!(outcome.failed.len(), 1);
+ assert_eq!(outcome.failed[0].key, "gmail.com");
+
+ let created = service.workflows.created.lock().unwrap();
+ assert_eq!(created.len(), 1);
+ assert_eq!(created[0].name, "Macro Customers Oct 16 2026");
+ assert_eq!(created[0].sharing, PipelineSharing::Private);
+
+ let pipeline = outcome.pipeline.unwrap();
+ assert_eq!(pipeline.added, 2);
+ assert_eq!(pipeline.filled_fields, ["Stage", "Owner"]);
+ assert_eq!(pipeline.unresolved.len(), 1);
+ assert_eq!(
+ pipeline.added_options,
+ [("Stage".to_owned(), vec!["Churned".to_owned()])]
+ );
+ let added = service.workflows.added.lock().unwrap();
+ assert_eq!(added[0].record, RecordTarget::Id(acme));
+ assert_eq!(
+ added[0].set,
+ [("Stage".to_owned(), FieldInput::One("Lead".into()))]
+ );
+ assert_eq!(added[1].record, RecordTarget::Id(id_of("beta.io")));
+ assert_eq!(
+ added[1].set,
+ [
+ ("Stage".to_owned(), FieldInput::One("Churned".into())),
+ ("Owner".to_owned(), FieldInput::One("[email protected]".into())),
+ ]
+ );
+}
+
+#[tokio::test]
+async fn a_companies_csv_without_pipeline_values_makes_no_pipeline() {
+ let service = CrmTransferServiceImpl::new(
+ Arc::new(Workflows::new(overview(PipelineRecordType::Company))),
+ Records::default(),
+ Files::default(),
+ );
+ let outcome = service
+ .import_companies(
+ &viewer(),
+ CsvInput::Text("name,website\nAcme,https://www.acme.com/\nNo domain,\n".into()),
+ None,
+ )
+ .await
+ .unwrap();
+ assert_eq!((outcome.created, outcome.skipped_rows), (1, 1));
+ assert!(outcome.pipeline.is_none());
+ assert!(service.workflows.created.lock().unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn a_pipeline_import_skips_present_records_keeps_new_deals_and_isolates_bad_rows() {
+ let acme = Uuid::now_v7();
+ let mut workflows = Workflows::new(overview(PipelineRecordType::Company));
+ workflows.present = vec![acme];
+ workflows.refused = Some("lots");
+ let pipeline = workflows.overview.pipeline.pipeline.id;
+ let service = CrmTransferServiceImpl::new(
+ Arc::new(workflows),
+ Records {
+ domains: HashMap::from([("acme.com".to_owned(), acme)]),
+ ..Records::default()
+ },
+ Files::default(),
+ );
+ let outcome = service
+ .import_to_pipeline(
+ &viewer(),
+ pipeline,
+ CsvInput::Text(
+ "Company,Domain,Stage,Revenue,Proposal,Notes\n\
+ Acme,acme.com,Lead,10,doc-1,\n\
+ Beta,beta.io,Lead,20,,first deal\n\
+ Beta,beta.io,Lead,30,,second deal\n\
+ Delta,delta.dev,Lead,lots,,\n"
+ .into(),
+ ),
+ )
+ .await
+ .unwrap();
+ assert_eq!(outcome.already_present, 1);
+ assert_eq!(outcome.added, 2);
+ assert_eq!(outcome.created_records, 2);
+ assert_eq!(outcome.invalid.len(), 1);
+ assert_eq!(outcome.invalid[0].key, "delta.dev");
+ assert_eq!(outcome.filled_fields, ["Stage", "Revenue"]);
+ assert_eq!(outcome.ignored_columns, ["Proposal", "Notes"]);
+ assert!(outcome.added_options.is_empty());
+}
+
+#[tokio::test]
+async fn new_contacts_at_one_new_domain_share_one_new_company() {
+ let mut contacts = overview(PipelineRecordType::Contact);
+ contacts.fields[0].name = "Contact".into();
+ let pipeline = contacts.pipeline.pipeline.id;
+ let known = Uuid::now_v7();
+ let service = CrmTransferServiceImpl::new(
+ Arc::new(Workflows::new(contacts)),
+ Records {
+ contacts: HashMap::from([("[email protected]".to_owned(), known)]),
+ ..Records::default()
+ },
+ Files::default(),
+ );
+ let outcome = service
+ .import_to_pipeline(
+ &viewer(),
+ pipeline,
+ CsvInput::Text(
+ "Contact,Email\nJane,[email protected]\nRaj,[email protected]\nAna,[email protected]\n".into(),
+ ),
+ )
+ .await
+ .unwrap();
+ assert_eq!(outcome.added, 3);
+ assert!(outcome.unresolved.is_empty(), "{:?}", outcome.unresolved);
+ assert_eq!(
+ *service.records.companies_created.lock().unwrap(),
+ ["beta.io"]
+ );
+ let mut created = service.records.contacts_created.lock().unwrap().clone();
+ created.sort();
+ assert_eq!(
+ created,
+ [
+ (id_of("beta.io"), "[email protected]".to_owned()),
+ (id_of("beta.io"), "[email protected]".to_owned()),
+ ]
+ );
+}
+
+#[tokio::test]
+async fn an_export_writes_people_as_emails_and_reimports() {
+ let mut workflows = Workflows::new(overview(PipelineRecordType::Company));
+ workflows.exported = vec![PipelineEntryView {
+ entry_id: RowId::new(),
+ record: PipelineRecord {
+ id: Uuid::now_v7(),
+ name: Some("=Acme".into()),
+ detail: Some("acme.com".into()),
+ },
+ fields: vec![
+ ("Stage".into(), FieldValue::Labels(vec!["Lead".into()])),
+ (
+ "Owner".into(),
+ FieldValue::References(vec!["macro|[email protected]".into()]),
+ ),
+ ("Revenue".into(), FieldValue::Number(48000.0)),
+ ],
+ }];
+ let pipeline = workflows.overview.pipeline.pipeline.id;
+ let service =
+ CrmTransferServiceImpl::new(Arc::new(workflows), Records::default(), Files::default());
+ let export = service.export_pipeline(&viewer(), pipeline).await.unwrap();
+ assert_eq!((export.rows, export.complete), (1, true));
+ let saved = service.files.saved.lock().unwrap();
+ assert_eq!(saved[0].0, "Deals");
+ assert_eq!(
+ saved[0].1,
+ "Company,Domain,Stage,Owner,Revenue,Proposal\n'=Acme,acme.com,Lead,[email protected],48000,\n"
+ );
+ let table = parse_csv(&saved[0].1).unwrap();
+ let rows = record_rows(&table, PipelineRecordType::Company).unwrap();
+ assert_eq!(rows.rows[0].name, "=Acme");
+}
diff --git a/crates/crm/src/inbound/toolset.rs b/crates/crm/src/inbound/toolset.rs
index 75c3d6a8bb..09286efc4a 100644
--- a/crates/crm/src/inbound/toolset.rs
+++ b/crates/crm/src/inbound/toolset.rs
@@ -12,6 +12,7 @@ mod create_contact;
mod get_company;
mod list_companies;
pub mod pipelines;
+pub mod transfer;
#[cfg(test)]
mod test;
diff --git a/crates/crm/src/inbound/toolset/transfer.rs b/crates/crm/src/inbound/toolset/transfer.rs
new file mode 100644
index 0000000000..2f61fab005
--- /dev/null
+++ b/crates/crm/src/inbound/toolset/transfer.rs
@@ -0,0 +1,231 @@
+//! Agent tools that move CRM data through CSV: a companies list into the
+//! team CRM, a CSV into a pipeline, and a pipeline out to a CSV file. Each
+//! is a thin adapter over [`CrmTransferService`].
+
+mod export_pipeline_csv;
+mod import_companies_csv;
+mod import_pipeline_csv;
+
+#[cfg(test)]
+mod test;
+
+use std::sync::Arc;
+
+use ai_toolset::{AsyncToolCollection, ToolCallError};
+use bot_id::BotId;
+use databases::domain::models::Viewer;
+use macro_user_id::user_id::MacroUserIdStr;
+use schemars::JsonSchema;
+use serde::Serialize;
+use uuid::Uuid;
+
+use crate::domain::crm_transfer::{
+ CrmTransferService, CsvInput, PipelineImportOutcome, SkippedRecord, TransferError,
+};
+
+pub use export_pipeline_csv::{ExportPipelineCsv, ExportPipelineCsvResponse};
+pub use import_companies_csv::{ImportCompaniesCsv, ImportCompaniesCsvResponse};
+pub use import_pipeline_csv::{ImportPipelineCsv, ImportPipelineCsvResponse};
+
+use super::{crm_error_description, pipelines::workflow_error};
+
+/// Most skipped records a response lists; the counts cover the rest.
+const LISTED: usize = 50;
+
+/// The transfer service and the agent the tools act as.
+pub struct CrmTransferToolContext<Transfer> {
+ /// Reads the CSV, matches it to the CRM, and writes through the pipelines.
+ pub service: Arc<Transfer>,
+ /// The agent acting for the requesting user; attribution only.
+ pub actor: BotId,
+}
+
+impl<Transfer> Clone for CrmTransferToolContext<Transfer> {
+ fn clone(&self) -> Self {
+ Self {
+ service: self.service.clone(),
+ actor: self.actor,
+ }
+ }
+}
+
+impl<Transfer> CrmTransferToolContext<Transfer> {
+ /// The requesting user, with this context's agent acting for them.
+ fn viewer(&self, user_id: MacroUserIdStr<'static>) -> Viewer {
+ Viewer {
+ user_id,
+ acting_bot: Some(self.actor),
+ }
+ }
+}
+
+/// Every CRM CSV tool.
+pub fn crm_transfer_toolset<Transfer: CrmTransferService>()
+-> AsyncToolCollection<CrmTransferToolContext<Transfer>> {
+ AsyncToolCollection::new()
+ .add_tool::<ImportCompaniesCsv, CrmTransferToolContext<Transfer>>()
+ .add_tool::<ImportPipelineCsv, CrmTransferToolContext<Transfer>>()
+ .add_tool::<ExportPipelineCsv, CrmTransferToolContext<Transfer>>()
+}
+
+/// The CSV a tool reads: exactly one of its text or a Macro file.
+fn csv_input(csv: &Option<String>, document_id: Option<Uuid>) -> Result<CsvInput, ToolCallError> {
+ match (csv, document_id) {
+ (Some(text), None) => Ok(CsvInput::Text(text.clone())),
+ (None, Some(id)) => Ok(CsvInput::File(id)),
+ _ => Err(ToolCallError {
+ description: "Give exactly one of csv (the CSV text) or documentId (a CSV file in \
+ Macro)."
+ .to_owned(),
+ internal_error: anyhow::anyhow!("csv input without exactly one source"),
+ }),
+ }
+}
+
+/// A transfer failure in words the model can act on.
+fn transfer_error(error: TransferError) -> ToolCallError {
+ let description = match error {
+ TransferError::Workflow(error) => return workflow_error(error),
+ TransferError::Invalid(ref message) | TransferError::File(ref message) => message.clone(),
+ TransferError::Crm(ref error) => crm_error_description(error),
+ TransferError::Access(_) => {
+ "Access to the CRM could not be checked right now. Retry the call.".to_owned()
+ }
+ };
+ ToolCallError {
+ description,
+ internal_error: error.into(),
+ }
+}
+
+/// A record an import could not add.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ToolSkippedRecord {
+ /// The company's domain or the contact's email.
+ pub key: String,
+ /// Why it was not added.
+ pub reason: String,
+}
+
+fn listed(records: &[SkippedRecord]) -> Vec<ToolSkippedRecord> {
+ records
+ .iter()
+ .take(LISTED)
+ .map(|record| ToolSkippedRecord {
+ key: record.key.clone(),
+ reason: record.reason.clone(),
+ })
+ .collect()
+}
+
+/// A select field and the options an import added to it.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ToolAddedOptions {
+ /// The field.
+ pub field: String,
+ /// The labels added.
+ pub labels: Vec<String>,
+}
+
+/// What a pipeline import did.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ToolPipelineImport {
+ /// The pipeline filled.
+ pub pipeline_id: Uuid,
+ /// Its name.
+ pub pipeline_name: String,
+ /// Entries added.
+ pub added: usize,
+ /// Rows whose record the pipeline already had, so were not added again.
+ pub already_present: usize,
+ /// Companies or contacts created to add their entries.
+ pub created_records: usize,
+ /// How many records could not be found or created.
+ pub unresolved_count: usize,
+ /// The first of them, with why.
+ pub unresolved: Vec<ToolSkippedRecord>,
+ /// How many rows were refused for a value their field cannot hold.
+ pub invalid_count: usize,
+ /// The first of them, with why.
+ pub invalid: Vec<ToolSkippedRecord>,
+ /// Rows without a domain or email.
+ pub skipped_rows: usize,
+ /// Pipeline fields the CSV filled.
+ pub filled_fields: Vec<String>,
+ /// CSV columns that match no field the import can fill.
+ pub ignored_columns: Vec<String>,
+ /// Options added to select fields so the CSV's values fit.
+ pub added_options: Vec<ToolAddedOptions>,
+}
+
+impl From<&PipelineImportOutcome> for ToolPipelineImport {
+ fn from(outcome: &PipelineImportOutcome) -> Self {
+ Self {
+ pipeline_id: outcome.pipeline_id,
+ pipeline_name: outcome.pipeline_name.clone(),
+ added: outcome.added,
+ already_present: outcome.already_present,
+ created_records: outcome.created_records,
+ unresolved_count: outcome.unresolved.len(),
+ unresolved: listed(&outcome.unresolved),
+ invalid_count: outcome.invalid.len(),
+ invalid: listed(&outcome.invalid),
+ skipped_rows: outcome.skipped_rows,
+ filled_fields: outcome.filled_fields.clone(),
+ ignored_columns: outcome.ignored_columns.clone(),
+ added_options: outcome
+ .added_options
+ .iter()
+ .map(|(field, labels)| ToolAddedOptions {
+ field: field.clone(),
+ labels: labels.clone(),
+ })
+ .collect(),
+ }
+ }
+}
+
+/// One sentence per part of a pipeline import worth reporting.
+fn pipeline_summary(outcome: &PipelineImportOutcome) -> String {
+ let mut parts = vec![format!(
+ "Added {} entries to \"{}\".",
+ outcome.added, outcome.pipeline_name
+ )];
+ if outcome.already_present > 0 {
+ parts.push(format!(
+ "{} rows were already in the pipeline, so not added again.",
+ outcome.already_present
+ ));
+ }
+ if outcome.created_records > 0 {
+ parts.push(format!(
+ "Created {} companies or contacts for them.",
+ outcome.created_records
+ ));
+ }
+ if !outcome.unresolved.is_empty() {
+ parts.push(format!(
+ "{} records could not be found or created.",
+ outcome.unresolved.len()
+ ));
+ }
+ if !outcome.invalid.is_empty() {
+ parts.push(format!(
+ "{} rows were refused for values their fields cannot hold.",
+ outcome.invalid.len()
+ ));
+ }
+ if !outcome.ignored_columns.is_empty() {
+ parts.push(format!(
+ "Columns with no matching field were skipped: {}.",
+ outcome.ignored_columns.join(", ")
+ ));
+ }
+ for (field, labels) in &outcome.added_options {
+ parts.push(format!("Added {field} options: {}.", labels.join(", ")));
+ }
+ parts.join(" ")
+}
diff --git a/crates/crm/src/inbound/toolset/transfer/export_pipeline_csv.rs b/crates/crm/src/inbound/toolset/transfer/export_pipeline_csv.rs
new file mode 100644
index 0000000000..04285aa5b9
--- /dev/null
+++ b/crates/crm/src/inbound/toolset/transfer/export_pipeline_csv.rs
@@ -0,0 +1,83 @@
+//! ExportPipelineCsv: a pipeline's entries saved as a CSV file.
+
+use ai_toolset::{
+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolResult,
+};
+use async_trait::async_trait;
+use schemars::JsonSchema;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use super::{CrmTransferToolContext, transfer_error};
+use crate::domain::{crm_transfer::CrmTransferService, pipeline_workflows::MAX_SCANNED_ROWS};
+
+/// Export a pipeline to a CSV file.
+#[derive(Debug, Clone, Deserialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+#[schemars(
+ title = "ExportPipelineCsv",
+ description = "Export a CRM pipeline's deals (entries) to a CSV file saved in the user's Macro files: Company and Domain (or Contact and Email), then one column per field, with people as emails. Use it to back up a pipeline, move it to another team (ImportPipelineCsv reads the same file), or share it as a spreadsheet; to answer questions about deals, use FindPipelineEntries instead. Returns the file's documentId and how many entries it holds."
+)]
+pub struct ExportPipelineCsv {
+ /// The pipeline.
+ #[schemars(description = "The pipeline's id from ListPipelines.")]
+ pub pipeline_id: Uuid,
+}
+
+/// The saved file.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ExportPipelineCsvResponse {
+ /// The CSV file in Macro.
+ pub document_id: Uuid,
+ /// The file's name.
+ pub file_name: String,
+ /// Entries written.
+ pub rows: usize,
+ /// Whether every entry was written.
+ pub complete: bool,
+ /// What happened.
+ pub summary: String,
+}
+
+impl ToolAnnotated for ExportPipelineCsv {
+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::additive("Export pipeline");
+}
+
+#[async_trait]
+impl<Transfer: CrmTransferService> AsyncTool<CrmTransferToolContext<Transfer>>
+ for ExportPipelineCsv
+{
+ type Output = ExportPipelineCsvResponse;
+
+ #[tracing::instrument(skip_all, fields(user_id = ?request_context.user_id, pipeline = %self.pipeline_id), err)]
+ async fn call(
+ &self,
+ context: ServiceContext<CrmTransferToolContext<Transfer>>,
+ request_context: RequestContext,
+ ) -> ToolResult<Self::Output> {
+ let viewer = context.viewer(request_context.user_id);
+ let export = context
+ .service
+ .export_pipeline(&viewer, self.pipeline_id)
+ .await
+ .map_err(transfer_error)?;
+ let mut summary = format!(
+ "Saved {} entries to the CSV file \"{}\".",
+ export.rows, export.file_name
+ );
+ if !export.complete {
+ summary.push_str(&format!(
+ " The pipeline has more than {MAX_SCANNED_ROWS} entries; only the first \
+ {MAX_SCANNED_ROWS} were written."
+ ));
+ }
+ Ok(ExportPipelineCsvResponse {
+ document_id: export.document_id,
+ file_name: export.file_name,
+ rows: export.rows,
+ complete: export.complete,
+ summary,
+ })
+ }
+}
diff --git a/crates/crm/src/inbound/toolset/transfer/import_companies_csv.rs b/crates/crm/src/inbound/toolset/transfer/import_companies_csv.rs
new file mode 100644
index 0000000000..ad561fcd68
--- /dev/null
+++ b/crates/crm/src/inbound/toolset/transfer/import_companies_csv.rs
@@ -0,0 +1,141 @@
+//! ImportCompaniesCsv: a companies list into the team CRM, with its Stage,
+//! Owner and Revenue on pipeline entries.
+
+use ai_toolset::{
+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolResult,
+};
+use async_trait::async_trait;
+use schemars::JsonSchema;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use super::{
+ CrmTransferToolContext, ToolPipelineImport, ToolSkippedRecord, csv_input, listed,
+ pipeline_summary, transfer_error,
+};
+use crate::domain::{
+ crm_transfer::{CrmTransferService, PipelineDestination},
+ pipelines::PipelineSharing,
+};
+
+/// Import companies from a CSV.
+#[derive(Debug, Clone, Deserialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+#[schemars(
+ title = "ImportCompaniesCsv",
+ description = "Import companies into the team CRM from a CSV, such as a Macro companies export or a list from another CRM. Each row needs a domain (a Domain, Domains or Website column) and may name the company (Company or Name); a row takes its first valid domain, and companies the team already has are left as they are. Stage, Owner and Revenue live on pipeline entries rather than companies: when the CSV has them, they go on entries of pipelineId, or of a new pipeline (private unless shareNewPipeline) named after the file. Give the CSV as text in csv, or as documentId for a CSV file in Macro. Returns counts and the rows that could not be imported. To add rows to a pipeline without this, use ImportPipelineCsv."
+)]
+pub struct ImportCompaniesCsv {
+ /// The CSV text.
+ #[schemars(description = "The CSV text with a header row. Give this or documentId, not both.")]
+ #[serde(default)]
+ pub csv: Option<String>,
+ /// A CSV file in Macro.
+ #[schemars(
+ description = "The id of a CSV file in Macro the user can open, e.g. one they uploaded. Give this or csv, not both."
+ )]
+ #[serde(default)]
+ pub document_id: Option<Uuid>,
+ /// An existing company pipeline for Stage, Owner and Revenue.
+ #[schemars(
+ description = "A company pipeline (id from ListPipelines) to put the CSV's Stage, Owner and Revenue on. Omit to create a new pipeline when the CSV has them."
+ )]
+ #[serde(default)]
+ pub pipeline_id: Option<Uuid>,
+ /// The new pipeline's name.
+ #[schemars(
+ description = "The name of the new pipeline, when one is created. Defaults to the file's name."
+ )]
+ #[serde(default)]
+ pub new_pipeline_name: Option<String>,
+ /// Whether the new pipeline is the team's.
+ #[schemars(
+ description = "Share the new pipeline with the team instead of keeping it private to the user. Defaults to false."
+ )]
+ #[serde(default)]
+ pub share_new_pipeline: Option<bool>,
+}
+
+/// What the import did.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ImportCompaniesCsvResponse {
+ /// Companies created.
+ pub created: usize,
+ /// Companies the team already had.
+ pub existing: usize,
+ /// Rows without a domain like acme.com.
+ pub skipped_rows: usize,
+ /// How many companies could not be created.
+ pub failed_count: usize,
+ /// The first of them, with why.
+ pub failed: Vec<ToolSkippedRecord>,
+ /// The pipeline entries made from Stage, Owner and Revenue.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub pipeline: Option<ToolPipelineImport>,
+ /// What happened.
+ pub summary: String,
+}
+
+impl ToolAnnotated for ImportCompaniesCsv {
+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::additive("Import companies");
+}
+
+#[async_trait]
+impl<Transfer: CrmTransferService> AsyncTool<CrmTransferToolContext<Transfer>>
+ for ImportCompaniesCsv
+{
+ type Output = ImportCompaniesCsvResponse;
+
+ #[tracing::instrument(skip_all, fields(user_id = ?request_context.user_id), err)]
+ async fn call(
+ &self,
+ context: ServiceContext<CrmTransferToolContext<Transfer>>,
+ request_context: RequestContext,
+ ) -> ToolResult<Self::Output> {
+ let csv = csv_input(&self.csv, self.document_id)?;
+ let destination = match self.pipeline_id {
+ Some(id) => PipelineDestination::Existing(id),
+ None => PipelineDestination::New {
+ name: self.new_pipeline_name.clone(),
+ sharing: if self.share_new_pipeline.unwrap_or(false) {
+ PipelineSharing::Team
+ } else {
+ PipelineSharing::Private
+ },
+ },
+ };
+ let viewer = context.viewer(request_context.user_id);
+ let outcome = context
+ .service
+ .import_companies(&viewer, csv, Some(destination))
+ .await
+ .map_err(transfer_error)?;
+ let mut summary = format!(
+ "Created {} companies; {} were already in the CRM.",
+ outcome.created, outcome.existing
+ );
+ if !outcome.failed.is_empty() {
+ summary.push_str(&format!(" {} could not be created.", outcome.failed.len()));
+ }
+ if outcome.skipped_rows > 0 {
+ summary.push_str(&format!(
+ " {} rows had no domain like acme.com.",
+ outcome.skipped_rows
+ ));
+ }
+ if let Some(pipeline) = &outcome.pipeline {
+ summary.push(' ');
+ summary.push_str(&pipeline_summary(pipeline));
+ }
+ Ok(ImportCompaniesCsvResponse {
+ created: outcome.created,
+ existing: outcome.existing,
+ skipped_rows: outcome.skipped_rows,
+ failed_count: outcome.failed.len(),
+ failed: listed(&outcome.failed),
+ pipeline: outcome.pipeline.as_ref().map(ToolPipelineImport::from),
+ summary,
+ })
+ }
+}
diff --git a/crates/crm/src/inbound/toolset/transfer/import_pipeline_csv.rs b/crates/crm/src/inbound/toolset/transfer/import_pipeline_csv.rs
new file mode 100644
index 0000000000..c4d82e3ae4
--- /dev/null
+++ b/crates/crm/src/inbound/toolset/transfer/import_pipeline_csv.rs
@@ -0,0 +1,85 @@
+//! ImportPipelineCsv: a CSV's rows into a pipeline as entries.
+
+use ai_toolset::{
+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolResult,
+};
+use async_trait::async_trait;
+use schemars::JsonSchema;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use super::{
+ CrmTransferToolContext, ToolPipelineImport, csv_input, pipeline_summary, transfer_error,
+};
+use crate::domain::crm_transfer::CrmTransferService;
+
+/// Import a CSV into a pipeline.
+#[derive(Debug, Clone, Deserialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+#[schemars(
+ title = "ImportPipelineCsv",
+ description = "Import a CSV into a CRM pipeline as new deals (entries), such as a pipeline export or a sheet of deals. In a company pipeline each row needs a domain (Domain, Domains or Website column) and may name the company (Company); in a contact pipeline each row needs an Email and may name the contact (Contact). Missing companies and contacts are created. Other columns fill the pipeline fields with the same name (Stage, Owner as an email, Revenue, custom fields); stage labels the pipeline lacks are added. Records already in the pipeline are not added again, so retrying an interrupted import adds only what is missing; several rows for one new record become separate deals. Give the CSV as text in csv, or as documentId for a CSV file in Macro. Returns counts and the rows that could not be added."
+)]
+pub struct ImportPipelineCsv {
+ /// The pipeline.
+ #[schemars(description = "The pipeline's id from ListPipelines.")]
+ pub pipeline_id: Uuid,
+ /// The CSV text.
+ #[schemars(description = "The CSV text with a header row. Give this or documentId, not both.")]
+ #[serde(default)]
+ pub csv: Option<String>,
+ /// A CSV file in Macro.
+ #[schemars(
+ description = "The id of a CSV file in Macro the user can open, e.g. one they uploaded or an ExportPipelineCsv result. Give this or csv, not both."
+ )]
+ #[serde(default)]
+ pub document_id: Option<Uuid>,
+}
+
+/// What the import did.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ImportPipelineCsvResponse {
+ /// The entries added and the rows left out.
+ #[serde(flatten)]
+ pub import: ToolPipelineImport,
+ /// What happened.
+ pub summary: String,
+}
+
+impl ToolAnnotated for ImportPipelineCsv {
+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::additive("Import into pipeline");
+}
+
+#[async_trait]
+impl<Transfer: CrmTransferService> AsyncTool<CrmTransferToolContext<Transfer>>
+ for ImportPipelineCsv
+{
+ type Output = ImportPipelineCsvResponse;
+
+ #[tracing::instrument(skip_all, fields(user_id = ?request_context.user_id, pipeline = %self.pipeline_id), err)]
+ async fn call(
+ &self,
+ context: ServiceContext<CrmTransferToolContext<Transfer>>,
+ request_context: RequestContext,
+ ) -> ToolResult<Self::Output> {
+ let csv = csv_input(&self.csv, self.document_id)?;
+ let viewer = context.viewer(request_context.user_id);
+ let outcome = context
+ .service
+ .import_to_pipeline(&viewer, self.pipeline_id, csv)
+ .await
+ .map_err(transfer_error)?;
+ let mut summary = pipeline_summary(&outcome);
+ if outcome.skipped_rows > 0 {
+ summary.push_str(&format!(
+ " {} rows named no company by domain or contact by email.",
+ outcome.skipped_rows
+ ));
+ }
+ Ok(ImportPipelineCsvResponse {
+ import: ToolPipelineImport::from(&outcome),
+ summary,
+ })
+ }
+}
diff --git a/crates/crm/src/inbound/toolset/transfer/test.rs b/crates/crm/src/inbound/toolset/transfer/test.rs
new file mode 100644
index 0000000000..e61a795b61
--- /dev/null
+++ b/crates/crm/src/inbound/toolset/transfer/test.rs
@@ -0,0 +1,39 @@
+use ai_toolset::schema::generate_validated_input_schema;
+
+use super::*;
+
+#[test]
+fn every_csv_tool_schema_is_valid_for_strict_tool_use() {
+ for (name, validated) in [
+ (
+ "ImportCompaniesCsv",
+ generate_validated_input_schema::<ImportCompaniesCsv>(),
+ ),
+ (
+ "ImportPipelineCsv",
+ generate_validated_input_schema::<ImportPipelineCsv>(),
+ ),
+ (
+ "ExportPipelineCsv",
+ generate_validated_input_schema::<ExportPipelineCsv>(),
+ ),
+ ] {
+ let validated = validated.unwrap_or_else(|error| panic!("{name}: {error}"));
+ assert_eq!(validated.name, name);
+ // The deferred-tool catalog shows only the first sentence.
+ let first = validated.description.split(". ").next().unwrap_or_default();
+ assert!(first.contains("CSV"), "{name}: {first}");
+ }
+}
+
+#[test]
+fn a_csv_comes_as_text_or_a_file_but_not_both() {
+ let file = Uuid::now_v7();
+ assert_eq!(
+ csv_input(&Some("Domain\nacme.com".into()), None).unwrap(),
+ CsvInput::Text("Domain\nacme.com".into())
+ );
+ assert_eq!(csv_input(&None, Some(file)).unwrap(), CsvInput::File(file));
+ assert!(csv_input(&None, None).is_err());
+ assert!(csv_input(&Some(String::new()), Some(file)).is_err());
+}
diff --git a/crates/prompt/src/crm_pipelines.md b/crates/prompt/src/crm_pipelines.md
index a91b4c4fc0..19a1a7eebc 100644
--- a/crates/prompt/src/crm_pipelines.md
+++ b/crates/prompt/src/crm_pipelines.md
@@ -6,6 +6,7 @@ A pipeline is a board of deals. Each entry tracks one CRM company or contact and
2. Read: `FindPipelineEntries` filters by field values and by company or contact name, and returns the count per stage and the total of each number field over every match. Answer counts and sums from those. To find which pipelines and stages a company or contact is in, leave out `pipelineId` and pass its name as `search` (or its `recordIds`).
3. Change: `UpdatePipelineEntries` moves entries between stages and sets fields by name, naming each entry by its `entryId` or by the company or contact it tracks. `AddToPipeline` adds companies (ids from `ListCompanies`) or contacts (by email). Remove entries with `RemoveFromPipeline` only when asked; the company or contact stays in the CRM. `EditPipeline` adds, renames or removes stages (the Stage field's options) and other fields. `CreatePipeline` makes a new pipeline only when the user asks for one.
4. Use the pipeline's exact field names and stage labels. If a requested stage does not exist, say so and offer to add it; never silently pick a similar one. Name people as "me", an email address, or a macro| user id; never guess a person.
-5. Check the tool result, such as each field's value before and after, before saying a change is saved.
+5. Bulk: for a CSV or spreadsheet of companies or deals, use `ImportCompaniesCsv` (companies, with their Stage, Owner and Revenue on pipeline entries) or `ImportPipelineCsv` (rows into one pipeline) instead of adding records one at a time; pass an uploaded file's `documentId` rather than its text. `ExportPipelineCsv` saves a pipeline as a CSV file the user can download or import elsewhere.
+6. Check the tool result, such as each field's value before and after, before saying a change is saved.
A company also has its own Stage, Owner and Revenue properties on its CRM page. They are separate from every pipeline and not kept in sync with it. When the user has pipelines, deal and stage requests mean pipeline entries; change the company's own properties with `SetEntityProperty` only when the user asks for those properties specifically.
diff --git a/crates/prompt/src/crm_pipelines/test.rs b/crates/prompt/src/crm_pipelines/test.rs
index 959cf4cfdf..912747950b 100644
--- a/crates/prompt/src/crm_pipelines/test.rs
+++ b/crates/prompt/src/crm_pipelines/test.rs
@@ -19,6 +19,9 @@ fn every_agent_host_maps_deal_requests_to_the_pipeline_tools() {
"RemoveFromPipeline",
"EditPipeline",
"CreatePipeline",
+ "ImportCompaniesCsv",
+ "ImportPipelineCsv",
+ "ExportPipelineCsv",
"not a Macro database",
"SetEntityProperty",
] {
diff --git a/docs/agent-guide/crm.md b/docs/agent-guide/crm.md
index 6aaf94e752..f94ee35c83 100644
--- a/docs/agent-guide/crm.md
+++ b/docs/agent-guide/crm.md
@@ -46,8 +46,8 @@ The tabs remain available inside pipelines, with no hamburger button. Company
filters lead the strip when available. On mobile, pipelines omit their title
and sharing row and use an edge-to-edge table, below the top safe area and above
the bottom dock. The compact **Pipeline actions** button in the table toolbar
-opens rename and sharing controls; only owners see **Trash pipeline**, which
-requires confirmation. Sharing opens the existing share dialog. Companies uses
+opens rename, **Export CSV**, **Import CSV** (editors) and sharing controls;
+only owners see **Trash pipeline**, which requires confirmation. Sharing opens the existing share dialog. Companies uses
the full-frame list with the global **+ Company** action above the dock. People keeps its search and **New contact** below the tab
strip. The embedded detail stack stays out of the mobile flow, so selecting a
row navigates in place.
@@ -201,7 +201,8 @@ pipelines refresh other editors' changes periodically; a local edit refreshes
immediately after saving.
A pipeline page uses the database page's styling with one table and no table
tabs. Its header shows the name, edited in place by editors, a **Pipeline
-actions** menu (**Rename**; **Trash pipeline** for the owner) and **Share**. The
+actions** menu (**Rename** and **Import CSV** for editors, **Export CSV** for
+anyone who can open it, **Trash pipeline** for the owner) and **Share**. The
toolbar below has **All records**, the pipeline's stored views and **New view**
(Table or Board), plus Group by (boards), Filter and Sort, then **Add company**
or **Add contact** (by the pipeline's record type) for editors, which starts a
@@ -214,6 +215,17 @@ views cannot be reordered yet. The selected view is remembered per pipeline.
**Trash pipeline** removes its table from navigation without deleting the linked
companies or contacts, then returns to Companies.
+**Export CSV** downloads every row: `Company` and `Domain` (or `Contact` and
+`Email`), then each pipeline column by name, with people as emails. **Import
+CSV** reads the same shape, so an export imports into another team's pipeline.
+Rows match companies by domain and contacts by email, creating missing ones
+(personal domains such as gmail.com cannot be created); other headers fill the
+pipeline columns of the same name, adding missing select options. A record
+the pipeline already had is not added again, so retrying an interrupted import
+adds only what is missing; several rows for one new record add separate entries
+(deals), as an export of such a pipeline lists them. Relation and non-person entity columns are skipped,
+and a value that does not fit its column is left empty.
+
CRM lists are currently disabled by `enableCrmLists` (default `false`). The sidebar
Lists section, list editor, and company membership controls only mount when enabled.
Existing list data is preserved; a restored list view returns to All companies while
@@ -231,9 +243,21 @@ picker. Checking or unchecking saves immediately and refreshes the CRM sidebar's
membership counts. Changes are disabled while saving; failures show an inline retry
message and keep the last saved membership. With no lists, create one in the CRM sidebar.
-`New company` uses the existing creation dialog. `Import` previews a CSV with `name`
-and `domain` columns (1–100 rows, at most 1 MB). The explicit Import button writes the
-previewed companies. Partial failures retain only failed rows for retry. Use preview
+`New company` uses the existing creation dialog. `Import` previews a CSV with a
+`Domain` (or `Domains`, `Website`) column and an optional `Name` or `Company`
+column, such as a Companies export (up to 10,000 rows, at most 5 MB). Each row
+takes its first valid domain; rows without one or repeating an earlier domain are
+skipped. The explicit Import button writes the previewed companies four at a
+time; domains the team already tracks count as already in the CRM. Partial
+failures retain only failed rows for retry. Stage, Owner, and Revenue live on
+pipeline rows, not companies: when the file has values in those columns and
+pipelines are available, a picker chooses where they go: **New private
+pipeline** (the default) or **New team pipeline**, either named after the file,
+or any company pipeline you can edit. The import adds one row per company with a
+value (missing Stage options are added; owners match by email) and offers **Open
+pipeline** after. If that step fails, importing again reuses the same pipeline
+and adds only missing rows. A pipeline's own **Import CSV** also takes a
+Companies export. Use preview
and cancel for browser checks against hosted dev data. `CRM settings` opens the existing
settings panel. Requires a team with CRM enabled.
diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rs
index 1f099c2108..883df54f11 100644
--- a/services/document_cognition_service/src/api/context/test.rs
+++ b/services/document_cognition_service/src/api/context/test.rs
@@ -472,6 +472,11 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
&databases_tool_context,
&crm_tool_context,
);
+ let crm_transfer_tool_context = ai_tools::build_crm_transfer_tool_context(
+ &pipeline_tool_context,
+ &crm_tool_context,
+ &document_tool_context,
+ );
let calendar_tool_context = ai_tools::build_calendar_tool_context(
pool.clone(),
@@ -563,6 +568,7 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
team_tool_context: ai_tools::build_team_tool_context(pool.clone()),
crm_tool_context,
pipeline_tool_context,
+ crm_transfer_tool_context,
skill_tool_context: ai_tools::build_skill_tool_context(
search_service_client.clone(),
soup_service.clone(),
diff --git a/services/document_cognition_service/src/server.rs b/services/document_cognition_service/src/server.rs
index 925a5f6c72..3c9366cc06 100644
--- a/services/document_cognition_service/src/server.rs
+++ b/services/document_cognition_service/src/server.rs
@@ -727,6 +727,11 @@ pub async fn run() -> anyhow::Result<()> {
&databases_tool_context,
&crm_tool_context,
);
+ let crm_transfer_tool_context = ai_tools::build_crm_transfer_tool_context(
+ &pipeline_tool_context,
+ &crm_tool_context,
+ &document_tool_context,
+ );
let calendar_tool_context = ai_tools::build_calendar_tool_context(
db.clone(),
@@ -817,6 +822,7 @@ pub async fn run() -> anyhow::Result<()> {
team_tool_context: ai_tools::build_team_tool_context(db.clone()),
crm_tool_context,
pipeline_tool_context,
+ crm_transfer_tool_context,
skill_tool_context: ai_tools::build_skill_tool_context(
search_service_client.clone(),
soup_service.clone(),
diff --git a/services/mcp_service/src/context.rs b/services/mcp_service/src/context.rs
index b0dd2fc41c..c5abf86f9a 100644
--- a/services/mcp_service/src/context.rs
+++ b/services/mcp_service/src/context.rs
@@ -447,6 +447,11 @@ async fn build_tool_context(args: ToolContextBuildArgs<'_>) -> anyhow::Result<To
&databases_tool_context,
&crm_tool_context,
);
+ let crm_transfer_tool_context = ai_tools::build_crm_transfer_tool_context(
+ &pipeline_tool_context,
+ &crm_tool_context,
+ &document_tool_context,
+ );
let calendar_tool_context = ai_tools::build_calendar_tool_context(
db.clone(),
@@ -599,6 +604,7 @@ async fn build_tool_context(args: ToolContextBuildArgs<'_>) -> anyhow::Result<To
team_tool_context: ai_tools::build_team_tool_context(db.clone()),
crm_tool_context,
pipeline_tool_context,
+ crm_transfer_tool_context,
skill_tool_context,
schedule_tool_context: ai_tools::build_routine_tool_context()?,
anthropic_tool_context: ai_tools::build_anthropic_tool_context(),