Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(import/onboarding): add import / onboarding crate (#5095)
teo2feb15c23117
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 2b27763b6b..05eff0dcde 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -56,6 +56,7 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",+ "crates/import","crates/invite_email","crates/item_filters","crates/kafka_util",@@ -76,6 +77,7 @@"crates/macro_sync_service_jwt","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client","crates/mention_utils","crates/model","crates/model-entity",@@ -220,6 +222,7 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",+ "crates/import","crates/invite_email","crates/item_filters","crates/kafka_util",@@ -240,6 +243,7 @@"crates/macro_sync_service_jwt","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client","crates/mention_utils","crates/model","crates/model-entity",@@ -678,6 +682,7 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",+ "crates/import","crates/invite_email","crates/item_filters","crates/kafka_util",@@ -698,6 +703,7 @@"crates/macro_sync_service_jwt","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client","crates/mention_utils","crates/model","crates/model-entity",@@ -1392,6 +1398,7 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",+ "crates/import","crates/invite_email","crates/ip_extractor","crates/item_filters",@@ -1442,6 +1449,7 @@"crates/models_team","crates/non_empty","crates/notification",+ "crates/onboarding","crates/opensearch_client","crates/opensearch_query_builder","crates/prompt",@@ -1518,6 +1526,7 @@"crates/graphql_notification","crates/graphql_properties","crates/graphql_soup",+ "crates/import","crates/invite_email","crates/ip_extractor","crates/item_filters",@@ -1546,6 +1555,7 @@"crates/macro_tower_layers","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client","crates/mention_utils","crates/model","crates/model-entity",@@ -2876,6 +2886,31 @@"crates/workspace-hack","services/image_proxy_service"],+ "import": [+ "crates/agent",+ "crates/ai_toolset",+ "crates/ai_usage",+ "crates/attachment",+ "crates/connection_gateway_client",+ "crates/connection_gateway_models",+ "crates/cowlike",+ "crates/import",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_user_id",+ "crates/macro_uuid",+ "crates/mcp_client",+ "crates/model-entity",+ "crates/model-error-response",+ "crates/model_file_type",+ "crates/model_user",+ "crates/non_empty",+ "crates/prompt",+ "crates/remote_env_var",+ "crates/workspace-hack"+ ],"invite_email": ["crates/ai_toolset","crates/cowlike",@@ -3384,6 +3419,7 @@"crates/foreign_entity","crates/frecency","crates/fusionauth",+ "crates/import","crates/invite_email","crates/item_filters","crates/kafka_util",@@ -3406,6 +3442,7 @@"crates/macro_sync_service_jwt","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client","crates/mention_utils","crates/model","crates/model-entity",@@ -3488,6 +3525,7 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",+ "crates/import","crates/invite_email","crates/item_filters","crates/kafka_util",@@ -3508,6 +3546,7 @@"crates/macro_sync_service_jwt","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client","crates/memory","crates/mention_utils","crates/model",@@ -4054,6 +4093,32 @@"crates/workspace-hack","services/notification_service"],+ "onboarding": [+ "crates/agent",+ "crates/ai_toolset",+ "crates/ai_usage",+ "crates/attachment",+ "crates/connection_gateway_client",+ "crates/connection_gateway_models",+ "crates/cowlike",+ "crates/import",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_user_id",+ "crates/macro_uuid",+ "crates/mcp_client",+ "crates/model-entity",+ "crates/model-error-response",+ "crates/model_file_type",+ "crates/model_user",+ "crates/non_empty",+ "crates/onboarding",+ "crates/prompt",+ "crates/remote_env_var",+ "crates/workspace-hack"+ ],"opensearch_client": ["crates/models_opensearch","crates/models_search_cursor",@@ -4429,6 +4494,7 @@"crates/filter_ast","crates/foreign_entity","crates/frecency",+ "crates/import","crates/invite_email","crates/item_filters","crates/kafka_util",@@ -4452,6 +4518,7 @@"crates/macro_sync_service_jwt","crates/macro_user_id","crates/macro_uuid",+ "crates/mcp_client","crates/memory","crates/mention_utils","crates/model",diff --git a/.sqlx/query-060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa.json b/.sqlx/query-060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa.jsonnew file mode 100644index 0000000000..3ef3b8416e--- /dev/null+++ b/.sqlx/query-060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa.json@@ -0,0 +1,101 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata,\n entity_id, entity_type, team_id)\n VALUES ($1, $2, $3, 'imported', $4, $5, $6, $7, $8)\n ON CONFLICT (user_id, source, foreign_id) DO UPDATE\n SET status = 'imported', metadata = EXCLUDED.metadata,\n entity_id = EXCLUDED.entity_id, entity_type = EXCLUDED.entity_type,\n team_id = EXCLUDED.team_id, last_error = NULL, updated_at = NOW()\n WHERE import_entity.status <> 'imported'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "Text",+ "Jsonb",+ "Text",+ "Text",+ "Uuid"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "060c162e3dd450c4f72b35b19b98cbf20bc65ee4f5775ea342801a25b60e5aaa"+}diff --git a/.sqlx/query-0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac.json b/.sqlx/query-0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac.jsonnew file mode 100644index 0000000000..34ef982fa0--- /dev/null+++ b/.sqlx/query-0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac.json@@ -0,0 +1,98 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata)\n VALUES ($1, $2, $3, 'staged', $4, $5)\n ON CONFLICT (user_id, source, foreign_id) DO UPDATE\n SET metadata = EXCLUDED.metadata, updated_at = NOW()\n WHERE import_entity.status = 'staged'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "Text",+ "Jsonb"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "0fca1dabefce12acb121ed6ca71a63dfffc2df9ef70b51421f9adf07ed71f3ac"+}diff --git a/.sqlx/query-166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b.json b/.sqlx/query-166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b.jsonnew file mode 100644index 0000000000..678d913d7d--- /dev/null+++ b/.sqlx/query-166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b.json@@ -0,0 +1,96 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE user_id = $1 AND source = $2 AND foreign_id = $3\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "166dcdc916a1f95150cf1254f078ac08b9eafbfaa1f3cca4b68127ea2baeb05b"+}diff --git a/.sqlx/query-23602d33e2cfec197d23c03e500e674cc050d01d782ecc2831c1d6f28bfa2e68.json b/.sqlx/query-23602d33e2cfec197d23c03e500e674cc050d01d782ecc2831c1d6f28bfa2e68.jsonnew file mode 100644index 0000000000..2f6be87b72--- /dev/null+++ b/.sqlx/query-23602d33e2cfec197d23c03e500e674cc050d01d782ecc2831c1d6f28bfa2e68.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET updated_at = NOW()\n WHERE user_id = $1 AND id = ANY($2) AND status = 'importing'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray"+ ]+ },+ "nullable": []+ },+ "hash": "23602d33e2cfec197d23c03e500e674cc050d01d782ecc2831c1d6f28bfa2e68"+}diff --git a/.sqlx/query-27116f1f2063eee335be586f808937c5077fba102e14e3966b0c5c2c403ebf7f.json b/.sqlx/query-27116f1f2063eee335be586f808937c5077fba102e14e3966b0c5c2c403ebf7f.jsonnew file mode 100644index 0000000000..2e74b7db4e--- /dev/null+++ b/.sqlx/query-27116f1f2063eee335be586f808937c5077fba102e14e3966b0c5c2c403ebf7f.json@@ -0,0 +1,40 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO user_onboarding (user_id) VALUES ($1)\n ON CONFLICT (user_id) DO UPDATE SET user_id = EXCLUDED.user_id\n RETURNING status, skipped, started_at, completed_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "skipped",+ "type_info": "Bool"+ },+ {+ "ordinal": 2,+ "name": "started_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 3,+ "name": "completed_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ true+ ]+ },+ "hash": "27116f1f2063eee335be586f808937c5077fba102e14e3966b0c5c2c403ebf7f"+}diff --git a/.sqlx/query-48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752.json b/.sqlx/query-48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752.jsonnew file mode 100644index 0000000000..7a4344669f--- /dev/null+++ b/.sqlx/query-48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'staged', last_error = $3, updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "48167394182b4fa5b3290de7acbcc848c0f73177b0cf6d0adafbdb275d07a752"+}diff --git a/.sqlx/query-50dbd26f9ca1a622f73b5bb7a5729e107dd4134fe6e77fea27bffae137f17359.json b/.sqlx/query-50dbd26f9ca1a622f73b5bb7a5729e107dd4134fe6e77fea27bffae137f17359.jsonnew file mode 100644index 0000000000..f7e09aff03--- /dev/null+++ b/.sqlx/query-50dbd26f9ca1a622f73b5bb7a5729e107dd4134fe6e77fea27bffae137f17359.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO import_run (user_id, source, status)\n VALUES ($1, $2, 'running')\n ON CONFLICT (user_id, source) DO UPDATE\n SET status = 'running', error = NULL, updated_at = NOW()\n WHERE import_run.status = ANY($3::text[])\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "TextArray"+ ]+ },+ "nullable": []+ },+ "hash": "50dbd26f9ca1a622f73b5bb7a5729e107dd4134fe6e77fea27bffae137f17359"+}diff --git a/.sqlx/query-621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3.json b/.sqlx/query-621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3.jsonnew file mode 100644index 0000000000..72cad1ee0f--- /dev/null+++ b/.sqlx/query-621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3.json@@ -0,0 +1,98 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'imported', entity_id = $3, entity_type = $4, team_id = $5,\n last_error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'importing'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Uuid",+ "Text",+ "Text",+ "Uuid"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "621f908816dd99749329e39e1f4b895d791e5f29b573156b9cea88dcbbfcfab3"+}diff --git a/.sqlx/query-64ab0eddd585b96ab58d5b864525bba3b87cf7ef9971ec2da46d84c0868251f3.json b/.sqlx/query-64ab0eddd585b96ab58d5b864525bba3b87cf7ef9971ec2da46d84c0868251f3.jsonnew file mode 100644index 0000000000..d36b46a1ed--- /dev/null+++ b/.sqlx/query-64ab0eddd585b96ab58d5b864525bba3b87cf7ef9971ec2da46d84c0868251f3.json@@ -0,0 +1,41 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO user_onboarding (user_id, status, skipped, completed_at)\n VALUES ($1, 'completed', $2, NOW())\n ON CONFLICT (user_id) DO UPDATE\n SET status = 'completed',\n skipped = CASE\n WHEN user_onboarding.status = 'completed' THEN user_onboarding.skipped\n ELSE EXCLUDED.skipped\n END,\n completed_at = COALESCE(user_onboarding.completed_at, EXCLUDED.completed_at),\n updated_at = NOW()\n RETURNING status, skipped, started_at, completed_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "skipped",+ "type_info": "Bool"+ },+ {+ "ordinal": 2,+ "name": "started_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 3,+ "name": "completed_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Bool"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ true+ ]+ },+ "hash": "64ab0eddd585b96ab58d5b864525bba3b87cf7ef9971ec2da46d84c0868251f3"+}diff --git a/.sqlx/query-6f216a9a7f92b281059f6a8d6445c8e839dae4e1e8eaec4efec7b131a583b5ac.json b/.sqlx/query-6f216a9a7f92b281059f6a8d6445c8e839dae4e1e8eaec4efec7b131a583b5ac.jsonnew file mode 100644index 0000000000..0a518a34a7--- /dev/null+++ b/.sqlx/query-6f216a9a7f92b281059f6a8d6445c8e839dae4e1e8eaec4efec7b131a583b5ac.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET status = $4, updated_at = NOW()\n WHERE user_id = $1 AND source = $2 AND status = ANY($3::text[])\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "TextArray",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "6f216a9a7f92b281059f6a8d6445c8e839dae4e1e8eaec4efec7b131a583b5ac"+}diff --git a/.sqlx/query-80b17499f3576f22aaddce4a8f56edd5faa866172d94dbb7cb00cbcaa64f1f4a.json b/.sqlx/query-80b17499f3576f22aaddce4a8f56edd5faa866172d94dbb7cb00cbcaa64f1f4a.jsonnew file mode 100644index 0000000000..719a17222f--- /dev/null+++ b/.sqlx/query-80b17499f3576f22aaddce4a8f56edd5faa866172d94dbb7cb00cbcaa64f1f4a.json@@ -0,0 +1,17 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_run\n SET status = $3, error = $4, updated_at = NOW()\n WHERE user_id = $1 AND source = $2 AND status = 'running'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "80b17499f3576f22aaddce4a8f56edd5faa866172d94dbb7cb00cbcaa64f1f4a"+}diff --git a/.sqlx/query-90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9.json b/.sqlx/query-90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9.jsonnew file mode 100644index 0000000000..c5d2799508--- /dev/null+++ b/.sqlx/query-90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9.json@@ -0,0 +1,96 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE (\n user_id = $1\n OR (status = 'imported' AND team_id IS NOT NULL\n AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1))\n )\n AND ($2::text IS NULL OR source = $2)\n AND ($3::text IS NULL OR status = $3)\n ORDER BY (user_id = $1) DESC, created_at DESC\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "90f114af5728d942b2ad3bf11f4adc3f5f0575df8012246175675fe0842584e9"+}diff --git a/.sqlx/query-928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9.json b/.sqlx/query-928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9.jsonnew file mode 100644index 0000000000..b69e3eb5ff--- /dev/null+++ b/.sqlx/query-928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'discarded', updated_at = NOW()\n WHERE user_id = $1 AND initiator = $2 AND status = 'staged'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "928a3b85689abec8e96bfbdf6539f71966262848a6f3d3372dd12df60de0a1b9"+}diff --git a/.sqlx/query-a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc.json b/.sqlx/query-a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc.jsonnew file mode 100644index 0000000000..497b06b82b--- /dev/null+++ b/.sqlx/query-a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc.json@@ -0,0 +1,95 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'importing', last_error = NULL, updated_at = NOW()\n WHERE user_id = $1 AND id = ANY($2::uuid[]) AND status = 'staged'\n RETURNING id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "UuidArray"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "a232d6e9add612435b2aa53371d3f6bf8698340072f9a181b462a7c4669dc9dc"+}diff --git a/.sqlx/query-c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3.json b/.sqlx/query-c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3.jsonnew file mode 100644index 0000000000..00ab44a4d4--- /dev/null+++ b/.sqlx/query-c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3.json@@ -0,0 +1,95 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE user_id = $1 AND id = $2\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "c0459fd55d7b0f642eb71773dc19deca937df172ead20864de84998cb280d8a3"+}diff --git a/.sqlx/query-ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91.json b/.sqlx/query-ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91.jsonnew file mode 100644index 0000000000..841f9667ad--- /dev/null+++ b/.sqlx/query-ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91.json@@ -0,0 +1,96 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT id, user_id, team_id, source, foreign_id, status, initiator,\n metadata, entity_id, entity_type, last_error, created_at, updated_at\n FROM import_entity\n WHERE source = $2 AND foreign_id = $3\n AND status = 'imported'\n AND user_id <> $1\n AND team_id IS NOT NULL\n AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1)\n LIMIT 1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 3,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 4,+ "name": "foreign_id",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 6,+ "name": "initiator",+ "type_info": "Text"+ },+ {+ "ordinal": 7,+ "name": "metadata",+ "type_info": "Jsonb"+ },+ {+ "ordinal": 8,+ "name": "entity_id",+ "type_info": "Text"+ },+ {+ "ordinal": 9,+ "name": "entity_type",+ "type_info": "Text"+ },+ {+ "ordinal": 10,+ "name": "last_error",+ "type_info": "Text"+ },+ {+ "ordinal": 11,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 12,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text",+ "Text",+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false,+ false,+ false,+ false,+ false,+ true,+ true,+ true,+ false,+ false+ ]+ },+ "hash": "ca24a6a5985263b41ffecec6d8481191a492610fcb5c161cf75428187705bf91"+}diff --git a/.sqlx/query-d5a9632d0330f2dfc6dc0c6f6817f2ffce500a7f816bb5af32d46d74baeb793e.json b/.sqlx/query-d5a9632d0330f2dfc6dc0c6f6817f2ffce500a7f816bb5af32d46d74baeb793e.jsonnew file mode 100644index 0000000000..b7f93d687d--- /dev/null+++ b/.sqlx/query-d5a9632d0330f2dfc6dc0c6f6817f2ffce500a7f816bb5af32d46d74baeb793e.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n DELETE FROM import_entity\n WHERE user_id = $1 AND initiator = $2 AND status = 'staged'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "d5a9632d0330f2dfc6dc0c6f6817f2ffce500a7f816bb5af32d46d74baeb793e"+}diff --git a/.sqlx/query-d919f2560dd92b03ba4cde5d6f024a94ac6e340f12eee4ac14ccec55b7008485.json b/.sqlx/query-d919f2560dd92b03ba4cde5d6f024a94ac6e340f12eee4ac14ccec55b7008485.jsonnew file mode 100644index 0000000000..e5801db794--- /dev/null+++ b/.sqlx/query-d919f2560dd92b03ba4cde5d6f024a94ac6e340f12eee4ac14ccec55b7008485.json@@ -0,0 +1,40 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT source, status, error, updated_at\n FROM import_run WHERE user_id = $1\n ORDER BY source\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "source",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "error",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ true,+ false+ ]+ },+ "hash": "d919f2560dd92b03ba4cde5d6f024a94ac6e340f12eee4ac14ccec55b7008485"+}diff --git a/.sqlx/query-dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593.json b/.sqlx/query-dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593.jsonnew file mode 100644index 0000000000..66c20cb92f--- /dev/null+++ b/.sqlx/query-dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'staged',\n last_error = 'the import was interrupted — select it again to retry',\n updated_at = NOW()\n WHERE user_id = $1 AND status = 'importing'\n AND updated_at < NOW() - ($2::double precision * interval '1 second')\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Float8"+ ]+ },+ "nullable": []+ },+ "hash": "dbdf6afe127d8dd8f6e98f9b6ea98d9bbf3af9f9067524af4837f53af55bc593"+}diff --git a/.sqlx/query-e50cfcb1701912d6bc57c019babc7c4a833a10c27ff658bad95058f62ec8d9ac.json b/.sqlx/query-e50cfcb1701912d6bc57c019babc7c4a833a10c27ff658bad95058f62ec8d9ac.jsonnew file mode 100644index 0000000000..ce88bcb79c--- /dev/null+++ b/.sqlx/query-e50cfcb1701912d6bc57c019babc7c4a833a10c27ff658bad95058f62ec8d9ac.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT team_id FROM team_user WHERE user_id = $1",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "team_id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "e50cfcb1701912d6bc57c019babc7c4a833a10c27ff658bad95058f62ec8d9ac"+}diff --git a/.sqlx/query-f9308780406056ddef995284bb713c9b8b9ae41c95454c7361bd733eb2113fa0.json b/.sqlx/query-f9308780406056ddef995284bb713c9b8b9ae41c95454c7361bd733eb2113fa0.jsonnew file mode 100644index 0000000000..e79a79adb1--- /dev/null+++ b/.sqlx/query-f9308780406056ddef995284bb713c9b8b9ae41c95454c7361bd733eb2113fa0.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE import_entity\n SET status = 'discarded', updated_at = NOW()\n WHERE user_id = $1 AND id = $2 AND status = 'staged'\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "f9308780406056ddef995284bb713c9b8b9ae41c95454c7361bd733eb2113fa0"+}diff --git a/.sqlx/query-f96c6a950d757c50ec8ee0c92782a6bb01202045d5af8aca519063adeb1b2d1c.json b/.sqlx/query-f96c6a950d757c50ec8ee0c92782a6bb01202045d5af8aca519063adeb1b2d1c.jsonnew file mode 100644index 0000000000..60777c92ad--- /dev/null+++ b/.sqlx/query-f96c6a950d757c50ec8ee0c92782a6bb01202045d5af8aca519063adeb1b2d1c.json@@ -0,0 +1,40 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT status, skipped, started_at, completed_at\n FROM user_onboarding\n WHERE user_id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "status",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "skipped",+ "type_info": "Bool"+ },+ {+ "ordinal": 2,+ "name": "started_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 3,+ "name": "completed_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": [+ false,+ false,+ false,+ true+ ]+ },+ "hash": "f96c6a950d757c50ec8ee0c92782a6bb01202045d5af8aca519063adeb1b2d1c"+}diff --git a/Cargo.lock b/Cargo.lockindex 6ea3240b59..a81f7a6381 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -197,6 +197,7 @@ dependencies = ["foreign_entity","frecency","futures",+ "import","item_filters","lazy_static","lexical_client",@@ -209,6 +210,7 @@ dependencies = ["macro_queues","macro_service_urls","macro_user_id",+ "mcp_client","model","model-entity","model_notifications",@@ -4140,6 +4142,7 @@ dependencies = ["futures","http 1.4.0","http-body-util",+ "import","last_online_tracker","lazy_static","lexical_client",@@ -4171,6 +4174,7 @@ dependencies = ["models_permissions","non_empty","notification",+ "onboarding","prompt","readonly_pool","redis",@@ -7109,6 +7113,36 @@ version = "1.12.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "40fac9d56ed6437b198fddba683305e8e2d651aa42647f00f5ae542e7f5c94a2"+[[package]]+name = "import"+version = "0.1.0"+dependencies = [+ "agent",+ "ai_toolset",+ "ai_usage",+ "anyhow",+ "async-trait",+ "axum",+ "chrono",+ "connection_gateway_client",+ "futures",+ "macro_authorization",+ "macro_user_id",+ "mcp_client",+ "model-entity",+ "schemars 1.2.1",+ "serde",+ "serde_json",+ "sqlx",+ "strum",+ "thiserror 2.0.18",+ "tokio",+ "tracing",+ "utoipa",+ "uuid",+ "workspace-hack",+]+[[package]]name = "include_dir"version = "0.7.4"@@ -10029,6 +10063,29 @@ dependencies = ["objc2-foundation",]+[[package]]+name = "onboarding"+version = "0.1.0"+dependencies = [+ "anyhow",+ "axum",+ "chrono",+ "import",+ "macro_authorization",+ "macro_user_id",+ "mcp_client",+ "serde",+ "serde_json",+ "sqlx",+ "strum",+ "thiserror 2.0.18",+ "tokio",+ "tracing",+ "utoipa",+ "uuid",+ "workspace-hack",+]+[[package]]name = "once_cell"version = "1.21.4"diff --git a/Cargo.toml b/Cargo.tomlindex b423ec1478..f94b60d4bf 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -24,6 +24,7 @@ members = ["crates/graphql_notification","crates/graphql_properties","crates/graphql_soup",+ "crates/import","crates/integration_tests/local_e2e","crates/loops_client","crates/macro_authorization",@@ -34,6 +35,7 @@ members = ["crates/macro_sync_service_jwt","crates/maybe_send","crates/memory",+ "crates/onboarding","crates/prompt","crates/properties_service","crates/search_service",diff --git a/apps/web/src/components/app/Layout.tsx b/apps/web/src/components/app/Layout.tsxindex e8feddedd7..b41bf26473 100644--- a/apps/web/src/components/app/Layout.tsx+++ b/apps/web/src/components/app/Layout.tsx@@ -45,14 +45,21 @@ import {SidebarVisibilityContext,} from '@components/app/sidebarVisibility';import { useIsAuthenticated } from '@core/auth';+import { ENABLE_NEW_ONBOARDING_V3 } from '@core/constant/featureFlags';import { usePaywallState } from '@core/constant/PaywallState';import { isSoloSettings } from '@core/constant/SettingsState';import { attachGlobalDOMScope } from '@core/hotkey/hotkeys';import { isMobile } from '@core/mobile/isMobile';+import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';import { virtualKeyboardVisible } from '@core/mobile/virtualKeyboard';import { updateCookie } from '@core/util/cookies';+import { useUserInfoQuery } from '@queries/auth/user-info';import { makePersisted } from '@solid-primitives/storage';-import { type RouteSectionProps, useLocation } from '@solidjs/router';+import {+ type RouteSectionProps,+ useLocation,+ useNavigate,+} from '@solidjs/router';import { cn } from '@ui';import { ScreencastHotkeys } from '@ui/components/ScreencastHotkeys';import {@@ -80,6 +87,7 @@ const AUTH_URLS = [`${ROUTER_BASE_CONCAT}login/popup`,`${ROUTER_BASE_CONCAT}login/popup/success`,`${ROUTER_BASE_CONCAT}onboarding`,+ `${ROUTER_BASE_CONCAT}setup`,`${ROUTER_BASE_CONCAT}signup`,`${ROUTER_BASE_CONCAT}email-signup-callback`,`${ROUTER_BASE_CONCAT}offline`,@@ -275,6 +283,39 @@ function CollapsedSidebarIncomingCallWidget(props: {);}+/**+ * Sends first-time desktop users into the split-screen onboarding at+ * /setup. Fires from anywhere in the app (marketing SSO lands on /app, not+ * /login), but never off auth/full-screen routes — /setup itself included.+ */+function NewOnboardingRedirect() {+ const userInfoQuery = useUserInfoQuery();+ const navigate = useNavigate();+ const location = useLocation();++ createEffect(() => {+ if (!ENABLE_NEW_ONBOARDING_V3 || isMobile() || isNativeMobilePlatform()) {+ return;+ }+ const data = userInfoQuery.data;+ if (data?.authenticated !== true || data.tutorialComplete !== false) {+ return;+ }+ if (AUTH_URLS.includes(location.pathname)) return;+ // Preserve the deep link the user arrived on (a shared doc, an invite):+ // /setup carries it as ?next and its finish() returns there instead of+ // home. Base-relative so navigate() can resolve it against the router.+ const target =+ location.pathname.slice(ROUTER_BASE_CONCAT.length - 1) + location.search;+ navigate(+ target === '/' ? '/setup' : `/setup?next=${encodeURIComponent(target)}`,+ { replace: true }+ );+ });++ return null;+}+function LayoutInner(props: RouteSectionProps) {const isAuthenticated = useIsAuthenticated();const { paywallOpen, showPaywall } = usePaywallState();@@ -366,6 +407,7 @@ function LayoutInner(props: RouteSectionProps) {<BundleUpdateProgressBar /><Suspense><Show when={isAuthenticated()}>+ <NewOnboardingRedirect /><Show when={!AUTH_URLS.includes(location.pathname)}><GithubReauthenticationPrompt /><GmailReauthenticationPrompt />diff --git a/apps/web/src/features/setup/ConnectorsSection.tsx b/apps/web/src/features/setup/ConnectorsSection.tsxnew file mode 100644index 0000000000..d0cb3f9c46--- /dev/null+++ b/apps/web/src/features/setup/ConnectorsSection.tsx@@ -0,0 +1,178 @@+import {+ FEATURED_MCP_SERVERS,+ type FeaturedMcpServer,+} from '@core/component/AI/constant/mcpServers';+import { toast } from '@core/component/Toast/Toast';+import ArrowUpRightIcon from '@phosphor/arrow-up-right.svg';+import SpinnerIcon from '@phosphor/spinner-gap.svg';+import {+ useAddMcpServerMutation,+ useMcpServersQuery,+ useStartMcpAuthMutation,+} from '@queries/mcp-servers';+import type { StartAuthResponse } from '@service-cognition/generated/schemas';+import { cn, Layer } from '@ui';+import { createMemo, createSignal, For, Match, Switch } from 'solid-js';+import { StatusDot } from '../settings/integration-ui';++/** The connectors onboarding leads with (the ones the builder acts on). */+const SETUP_CONNECTOR_NAMES = ['Linear', 'GitHub', 'Notion', 'Slack'];++/**+ * One connector as its own small card (same chrome as the import panel's+ * source cards): brand icon, name, and an explicit trailing status — a+ * green dot + "Connected" once OAuth has completed, a quiet "Connect"+ * affordance until then (the tagline survives as the hover title).+ * Clicking an unconnected card kicks off add + OAuth in a popup; the server+ * reconciles the moment OAuth completes, and the polled servers query flips+ * the card when the user returns.+ */+function ConnectorRow(props: {+ server: FeaturedMcpServer;+ connected: boolean;+ authenticated: boolean;+}) {+ const addMutation = useAddMcpServerMutation();+ const authMutation = useStartMcpAuthMutation();+ const [busy, setBusy] = createSignal(false);++ const handleConnect = async () => {+ if (props.authenticated || busy()) return;+ setBusy(true);+ // Open the popup synchronously, while the click's transient activation+ // is still live — opening after the awaited mutations gets rejected by+ // strict popup blockers. The OAuth URL is assigned once it's known;+ // severing `opener` keeps the provider page from reaching back into+ // the app (reverse tabnabbing).+ const popup = window.open('about:blank', '_blank');+ if (popup) popup.opener = null;+ try {+ if (!props.connected) {+ await addMutation.mutateAsync({+ server_name: props.server.server_name,+ url: props.server.url,+ });+ }+ const result: StartAuthResponse = await authMutation.mutateAsync({+ server_name: props.server.server_name,+ server_url: props.server.url,+ });+ if (popup) {+ popup.location.href = result.authorization_url;+ } else {+ // Popup was blocked outright — fall back to a plain open (may+ // still be blocked, but nothing else to try).+ window.open(result.authorization_url, '_blank', 'noopener,noreferrer');+ }+ } catch {+ popup?.close();+ toast.failure(`Failed to connect ${props.server.server_name}`);+ } finally {+ setBusy(false);+ }+ };++ return (+ <Layer depth={2}>+ <button+ type="button"+ title={+ props.authenticated+ ? `${props.server.server_name} is connected`+ : props.server.tagline+ }+ onClick={() => void handleConnect()}+ class={cn(+ 'group flex h-11 w-full items-center gap-2.5 rounded-xl border border-ink/[0.05] bg-surface px-3.5 text-sm',+ 'cursor-default outline-none focus-visible:ring-1 focus-visible:ring-accent/50',+ !props.authenticated && 'hover:border-ink/10'+ )}+ >+ <span class="flex size-4 shrink-0 items-center justify-center [&_svg]:size-4">+ <props.server.icon />+ </span>+ <span class="min-w-0 truncate font-medium text-ink">+ {props.server.server_name}+ </span>+ <span class="ml-auto shrink-0">+ <Switch+ fallback={+ <span class="flex items-center gap-1 text-xs font-medium text-ink-muted group-hover:text-ink">+ Connect+ <ArrowUpRightIcon class="size-3 shrink-0" />+ </span>+ }+ >+ <Match when={props.authenticated}>+ <span class="flex items-center gap-1.5 text-xs text-ink-muted">+ <StatusDot state="connected" />+ Connected+ </span>+ </Match>+ <Match when={busy()}>+ <span class="flex items-center gap-1.5 text-xs text-ink-muted">+ <SpinnerIcon class="size-3 shrink-0 animate-spin" />+ Connecting…+ </span>+ </Match>+ </Switch>+ </span>+ </button>+ </Layer>+ );+}++/**+ * The tools half of "connect your work": the core connectors as a column of+ * one-line rows — connected ones read "Connected" with a status dot, the+ * rest are one-click OAuth. Connected tools feed the workspace builder on+ * the right.+ */+export function ConnectorsSection() {+ // Poll: OAuth completes in a popup, and if this window never blurs (or+ // regains focus) no focus-refetch would ever flip the card. neverSuspend+ // keeps the polling query from re-suspending the page's boundary.+ const serversQuery = useMcpServersQuery({+ refetchInterval: 4_000,+ neverSuspend: true,+ });++ const featured = SETUP_CONNECTOR_NAMES.flatMap(+ (name) =>+ FEATURED_MCP_SERVERS.find((server) => server.server_name === name) ?? []+ );++ const serverByUrl = createMemo(() => {+ const map = new Map<string, { authenticated: boolean }>();+ for (const server of serversQuery.data ?? []) {+ map.set(server.url, { authenticated: server.authenticated });+ }+ return map;+ });++ return (+ <section class="flex flex-col gap-3">+ <div class="flex flex-col gap-1">+ <h2 class="text-[15px] font-semibold text-ink">Bring your tools</h2>+ <p class="text-sm text-ink-muted">+ Macro AI works with everything you create in Macro and everything in+ your connected tools. Connect a few integrations, and we'll optionally+ pull in a couple of items from each to get you started.+ </p>+ </div>+ <div class="flex flex-col gap-2">+ <For each={featured}>+ {(server) => (+ <ConnectorRow+ server={server}+ connected={serverByUrl().has(server.url)}+ authenticated={+ serverByUrl().get(server.url)?.authenticated ?? false+ }+ />+ )}+ </For>+ </div>+ </section>+ );+}diff --git a/apps/web/src/features/setup/ImportEntityPill.tsx b/apps/web/src/features/setup/ImportEntityPill.tsxnew file mode 100644index 0000000000..ab18bb0dd7--- /dev/null+++ b/apps/web/src/features/setup/ImportEntityPill.tsx@@ -0,0 +1,60 @@+import { buildSimpleEntityUrl } from '@core/util/url';+import SpinnerIcon from '@phosphor/spinner-gap.svg';+import { entityLabel, type ImportEntity } from '@queries/import';+import { type JSX, Match, Switch } from 'solid-js';+import { ItemPill } from './primitives';++/** One ledger row as a pill, styled by its status. */+export function ImportEntityPill(props: {+ entity: ImportEntity;+ icon?: JSX.Element;+}) {+ const code = () => {+ const identifier = props.entity.metadata.identifier;+ return typeof identifier === 'string' ? identifier : undefined;+ };+ const hoverDetail = () => {+ const meta = props.entity.metadata;+ const detail = meta.description ?? meta.summary ?? meta.purpose;+ return typeof detail === 'string' ? detail : undefined;+ };++ return (+ <Switch>+ <Match when={props.entity.status === 'imported'}>+ <ItemPill+ icon={props.icon}+ code={code()}+ label={entityLabel(props.entity)}+ importedHref={+ props.entity.entity_id && props.entity.entity_type+ ? buildSimpleEntityUrl({+ type: props.entity.entity_type,+ id: props.entity.entity_id,+ })+ : undefined+ }+ />+ </Match>+ <Match when={props.entity.status === 'importing'}>+ <ItemPill+ icon={props.icon}+ code={code()}+ label={entityLabel(props.entity)}+ title="Importing…"+ status={+ <SpinnerIcon class="size-3 shrink-0 animate-spin text-ink-extra-muted" />+ }+ />+ </Match>+ <Match when={true}>+ <ItemPill+ icon={props.icon}+ code={code()}+ label={entityLabel(props.entity)}+ title={hoverDetail()}+ />+ </Match>+ </Switch>+ );+}diff --git a/apps/web/src/features/setup/ImportPanel.tsx b/apps/web/src/features/setup/ImportPanel.tsxnew file mode 100644index 0000000000..b8be4c935b--- /dev/null+++ b/apps/web/src/features/setup/ImportPanel.tsx@@ -0,0 +1,97 @@+import {+ type ImportEntity,+ type ImportRun,+ type ImportSource,+ useImportQuery,+ useRetryGatherMutation,+} from '@queries/import';+import { createMemo, For, Show } from 'solid-js';+import { SourceImportCard } from './SourceImportCard';+import {+ type SkippedSources,+ SOURCE_SECTIONS,+ type SourceSection,+} from './selection';++/** The `(gather run, ledger rows)` slice one source's card renders from. */+interface SourceSlice {+ run?: ImportRun;+ entities: ImportEntity[];+}++/**+ * The import side of `/setup`: one card per connected source, rendered from+ * the import aggregate. Pills stream in live (gateway pushes + polling);+ * whether a section imports is a single toggle per card. Skip state lives+ * in the page (SetupPage), so the footer's "Continue to Macro" imports+ * exactly the sections still toggled on.+ */+export function ImportPanel(props: {+ skipped: SkippedSources;+ onToggleSource: (source: ImportSource, skipped: boolean) => void;+}) {+ const importQuery = useImportQuery();+ const retryGather = useRetryGatherMutation();++ const bySource = createMemo(() => {+ const state = importQuery.data;+ const map = new Map<ImportSource, SourceSlice>();+ for (const { source } of SOURCE_SECTIONS) {+ map.set(source, {+ run: state?.runs.find((run) => run.source === source),+ entities: (state?.entities ?? []).filter(+ (entity) => entity.source === source && entity.status !== 'discarded'+ ),+ });+ }+ return map;+ });++ const sliceFor = (definition: SourceSection): SourceSlice =>+ bySource().get(definition.source) ?? { entities: [] };++ const isVisible = (slice: SourceSlice) =>+ slice.run !== undefined || slice.entities.length > 0;++ const anyVisible = createMemo(() =>+ SOURCE_SECTIONS.some((definition) => isVisible(sliceFor(definition)))+ );++ return (+ <div class="flex flex-1 flex-col gap-7 overflow-y-auto p-8">+ <header>+ <h2 class="text-lg font-semibold">Workspace setup</h2>+ </header>++ <Show+ when={anyVisible()}+ fallback={+ <p class="text-sm text-ink-extra-muted">+ Connect a tool on the left to see what you can bring over.+ </p>+ }+ >+ <div class="flex flex-col gap-4">+ {/* Iterate the static definitions (stable row identity across+ refetches); visibility is decided per row. */}+ <For each={SOURCE_SECTIONS}>+ {(definition) => (+ <Show when={isVisible(sliceFor(definition))}>+ <SourceImportCard+ definition={definition}+ run={sliceFor(definition).run}+ entities={sliceFor(definition).entities}+ skipped={props.skipped[definition.source] === true}+ onToggleSkipped={(skipped) =>+ props.onToggleSource(definition.source, skipped)+ }+ onRetryGather={() => retryGather.mutate(definition.source)}+ />+ </Show>+ )}+ </For>+ </div>+ </Show>+ </div>+ );+}diff --git a/apps/web/src/features/setup/SetupPage.tsx b/apps/web/src/features/setup/SetupPage.tsxnew file mode 100644index 0000000000..d418f64572--- /dev/null+++ b/apps/web/src/features/setup/SetupPage.tsx@@ -0,0 +1,137 @@+import LogoIcon from '@icon/macro-logo.svg';+import { useUserInfoQuery } from '@queries/auth/user-info';+import { usePrimaryEmailLinkId } from '@queries/email/link';+import { useImportQuery } from '@queries/import';+import { useOnboardingQuery } from '@queries/onboarding';+import { useNavigate } from '@solidjs/router';+import { Button, Layer } from '@ui';+import { createEffect, Suspense } from 'solid-js';+import { createStore } from 'solid-js/store';+import { ConnectorsSection } from './ConnectorsSection';+import { createSetupFinish } from './createSetupFinish';+import { ImportPanel } from './ImportPanel';+import type { SkippedSources } from './selection';++/**+ * The split-screen onboarding page (`/setup`): connect tools on the left,+ * pick what to bring over on the right. Everything is orchestrated+ * server-side — reads of an active onboarding start due gather runs,+ * connector OAuth completions hook in instantly, and `POST /import/run`+ * copies accepted items in — so this page only renders state and decides+ * when the user is done (the finish workflow lives in createSetupFinish).+ */+export function SetupPage() {+ // The page owns its Suspense boundary: /setup renders under the app's+ // root <Suspense>, so any query suspending here would otherwise blank the+ // ENTIRE app. The polling queries carry placeholderData so they can never+ // re-suspend; this boundary contains genuine first-load waits from the+ // rest (user info).+ return (+ <Suspense+ fallback={+ <div class="flex h-full w-full items-center justify-center bg-surface">+ <LogoIcon class="size-8 animate-pulse text-accent" />+ </div>+ }+ >+ <SetupPageContent />+ </Suspense>+ );+}++function SetupPageContent() {+ const navigate = useNavigate();+ const userInfoQuery = useUserInfoQuery();+ const onboardingQuery = useOnboardingQuery();+ const importQuery = useImportQuery();+ // The user's own primary inbox link — present once email is connected+ // (drives the welcome copy: inbox processing vs plain setup).+ const emailConnected = usePrimaryEmailLinkId();++ // Per-source skip set (sections import by default; each card's toggle+ // flips its whole source off). Lives here so the footer's "Continue to+ // Macro" imports exactly the sections still toggled on. Keyed by source+ // so re-gathers and refetches keep the user's picks.+ const [skippedSources, setSkippedSources] = createStore<SkippedSources>({});++ const setupFinish = createSetupFinish({+ entities: () => importQuery.data?.entities,+ skippedSources,+ });++ // Redirect out when there is nothing to set up (unauthenticated, or the+ // flow was already completed elsewhere).+ createEffect(() => {+ if (userInfoQuery.data?.authenticated === false) {+ navigate('/login', { replace: true });+ return;+ }+ if (onboardingQuery.data?.row.status === 'completed') {+ navigate(setupFinish.afterSetupTarget(), { replace: true });+ }+ });++ return (+ // Same chrome as fullscreen settings: the left rail sits directly on the+ // page surface and the right pane is the subtly-raised, bordered card in+ // a tight gutter.+ <div class="flex h-full w-full overflow-hidden bg-surface font-sans text-ink">+ {/* Left: connect your work */}+ <div class="flex w-[40%] min-w-[380px] max-w-[520px] flex-col">+ <div class="flex flex-1 flex-col overflow-y-auto">+ <header class="px-8 pt-12">+ <LogoIcon class="size-8 text-accent" />+ <h1 class="mt-6 text-2xl/tight font-semibold">Welcome to Macro</h1>+ <p class="mt-1.5 text-sm text-ink-muted">+ {emailConnected()+ ? "While we process your inbox, let's bring in your tasks, documents, contacts, and channels, and set up your team."+ : 'Set up Macro by bringing in your tasks, documents, and channels, then invite your team.'}+ </p>+ </header>++ <div class="flex flex-col gap-8 px-8 pb-8 pt-9">+ <ConnectorsSection />+ </div>+ </div>++ <footer class="flex items-center justify-between px-8 py-5">+ <Button+ variant="active"+ depth={3}+ disabled={setupFinish.finishing()}+ onClick={() => void setupFinish.finish(false)}+ >+ {setupFinish.finishing()+ ? 'Setting up your workspace…'+ : setupFinish.selectedCount() > 0+ ? `Import ${setupFinish.selectedCount()} & continue`+ : 'Continue to Macro'}+ </Button>+ <button+ type="button"+ class="text-xs text-ink-extra-muted transition-colors hover:text-ink-muted"+ disabled={setupFinish.finishing()}+ onClick={() => void setupFinish.finish(true)}+ >+ Set up later+ </button>+ </footer>+ </div>++ {/* Right: what to bring over, in the settings content card+ (Layer-raised, rounded, inner border). */}+ <div class="min-w-0 flex-1 py-2 pr-2 pl-0">+ <Layer depth={1}>+ <div class="relative flex size-full flex-col overflow-hidden rounded-xl border border-ink/[0.06] bg-surface shadow-menu">+ <ImportPanel+ skipped={skippedSources}+ onToggleSource={(source, skipped) =>+ setSkippedSources(source, skipped)+ }+ />+ </div>+ </Layer>+ </div>+ </div>+ );+}diff --git a/apps/web/src/features/setup/SourceImportCard.tsx b/apps/web/src/features/setup/SourceImportCard.tsxnew file mode 100644index 0000000000..7bc7eff25f--- /dev/null+++ b/apps/web/src/features/setup/SourceImportCard.tsx@@ -0,0 +1,157 @@+import { FEATURED_MCP_SERVERS } from '@core/component/AI/constant/mcpServers';+import SpinnerIcon from '@phosphor/spinner-gap.svg';+import type { ImportEntity, ImportRun } from '@queries/import';+import { ToggleSwitch } from '@ui';+import { createMemo, For, Match, Show, Switch } from 'solid-js';+import { ImportEntityPill } from './ImportEntityPill';+import { FailureNote, ImportCard, PillGrid, SkeletonPills } from './primitives';+import type { SourceSection } from './selection';++/** Row counts by status — derived once per entities change, read many times. */+interface StatusCounts {+ staged: number;+ importing: number;+ imported: number;+}++/**+ * One source's card on the import panel: connector-first header with the+ * section import toggle, a status blurb (gathering / importing / found /+ * failed-with-retry), and the pill cloud. Purely presentational over the+ * `(run, entities)` slice its parent derives — skip state and mutations+ * stay with the owners.+ */+export function SourceImportCard(props: {+ definition: SourceSection;+ run: ImportRun | undefined;+ entities: ImportEntity[];+ skipped: boolean;+ onToggleSkipped: (skipped: boolean) => void;+ onRetryGather: () => void;+}) {+ const server = createMemo(() =>+ FEATURED_MCP_SERVERS.find(+ (candidate) => candidate.server_name === props.definition.serverName+ )+ );+ const serverIcon = () => {+ const found = server();+ return found ? <found.icon /> : undefined;+ };+ const counts = createMemo<StatusCounts>(() => {+ const tally: StatusCounts = { staged: 0, importing: 0, imported: 0 };+ for (const entity of props.entities) {+ if (entity.status === 'staged') tally.staged += 1;+ else if (entity.status === 'importing') tally.importing += 1;+ else if (entity.status === 'imported') tally.imported += 1;+ }+ return tally;+ });+ const gathering = () => props.run?.status === 'running';+ // Skipping a section collapses its staged pills entirely;+ // importing/imported rows stay visible regardless.+ const visiblePills = createMemo(() =>+ props.skipped+ ? props.entities.filter((entity) => entity.status !== 'staged')+ : props.entities+ );++ return (+ <ImportCard+ icon={serverIcon()}+ title={props.definition.serverName}+ count={props.entities.length || undefined}+ connected+ actions={+ <Show when={counts().staged > 0}>+ <ToggleSwitch+ size="sm"+ class="flex-row-reverse"+ checked={!props.skipped}+ onChange={(checked) => props.onToggleSkipped(!checked)}+ label={props.skipped ? 'Skipped' : 'Import all'}+ labelClass="text-xs text-ink-muted select-none"+ />+ </Show>+ }+ status={+ <SectionStatus+ definition={props.definition}+ run={props.run}+ counts={counts()}+ skipped={props.skipped}+ onRetryGather={props.onRetryGather}+ />+ }+ >+ <Show when={visiblePills().length > 0 || gathering()}>+ <PillGrid>+ <For each={visiblePills()}>+ {(entity) => (+ <ImportEntityPill entity={entity} icon={serverIcon()} />+ )}+ </For>+ <Show when={gathering()}>+ <SkeletonPills count={visiblePills().length > 0 ? 3 : 6} />+ </Show>+ </PillGrid>+ </Show>+ </ImportCard>+ );+}++/**+ * The card's status blurb, continuing the well's "Connected —" lead-in+ * (lowercase starts). Failure always surfaces with a retry — even when+ * earlier/teammate imports left entities in the section, since a silent+ * failure just looks like a thin result.+ */+function SectionStatus(props: {+ definition: SourceSection;+ run: ImportRun | undefined;+ counts: StatusCounts;+ skipped: boolean;+ onRetryGather: () => void;+}) {+ return (+ <Switch>+ <Match when={props.run?.status === 'failed'}>+ <FailureNote+ message={`we couldn't look through your ${props.definition.serverName}.`}+ onRetry={() => props.onRetryGather()}+ />+ </Match>+ <Match when={props.run?.status === 'running'}>+ <span class="flex items-center gap-1.5">+ looking through your {props.definition.serverName} for{' '}+ {props.definition.noun} worth importing…+ <SpinnerIcon class="size-3 shrink-0 animate-spin" />+ </span>+ </Match>+ <Match when={props.counts.importing > 0}>+ <span class="flex items-center gap-1.5">+ importing {props.counts.importing} {props.definition.noun} into your+ workspace…+ <SpinnerIcon class="size-3 shrink-0 animate-spin" />+ </span>+ </Match>+ <Match when={props.counts.staged > 0}>+ <span>+ <Show+ when={!props.skipped}+ fallback={<>suggested imports skipped for now.</>}+ >+ here are some{' '}+ <span class="font-medium text-ink">{props.definition.noun}</span> we+ pulled in to start your workspace. You can always ask Macro AI to+ bring in more later.+ </Show>+ </span>+ </Match>+ <Match when={true}>+ {props.counts.imported} {props.definition.noun} from{' '}+ {props.definition.serverName} are in your workspace.+ </Match>+ </Switch>+ );+}diff --git a/apps/web/src/features/setup/createSetupFinish.ts b/apps/web/src/features/setup/createSetupFinish.tsnew file mode 100644index 0000000000..b8ffbf6bd5--- /dev/null+++ b/apps/web/src/features/setup/createSetupFinish.ts@@ -0,0 +1,145 @@+import { toast } from '@core/component/Toast/Toast';+import { authKeys } from '@queries/auth/keys';+import { useCompleteTutorialMutation } from '@queries/auth/tutorial';+import { useUserInfoQuery } from '@queries/auth/user-info';+import { queryClient } from '@queries/client';+import {+ fetchImportState,+ type ImportEntity,+ useRunImportMutation,+} from '@queries/import';+import { useCompleteOnboardingMutation } from '@queries/onboarding';+import { useNavigate, useSearchParams } from '@solidjs/router';+import { type Accessor, createMemo, createSignal } from 'solid-js';+import { type SkippedSources, stagedSelection } from './selection';++/**+ * How long "Continue to Macro" holds for in-flight imports before letting+ * the user through anyway (imports keep landing server-side; the ledger is+ * never lost to navigation).+ */+const FINISH_IMPORT_WAIT_MS = 90_000;+const FINISH_POLL_INTERVAL_MS = 1_500;++function sleep(ms: number): Promise<void> {+ return new Promise((resolve) => setTimeout(resolve, ms));+}++/**+ * The "leave /setup" workflow, as a composable primitive: derives the+ * current selection, and `finish()` imports it (holding while rows land),+ * completes onboarding + the legacy tutorial, and navigates into the app —+ * to the deep link the redirect preserved (`?next=`), or home.+ *+ * `skippedSources` is read reactively (pass the store proxy itself).+ */+export function createSetupFinish(options: {+ entities: Accessor<ImportEntity[] | undefined>;+ skippedSources: SkippedSources;+}) {+ const navigate = useNavigate();+ const [searchParams] = useSearchParams();+ const userInfoQuery = useUserInfoQuery();+ const completeOnboarding = useCompleteOnboardingMutation();+ const completeTutorial = useCompleteTutorialMutation();+ const runImport = useRunImportMutation();+ const [finishing, setFinishing] = createSignal(false);++ const selection = createMemo(() =>+ stagedSelection(options.entities(), options.skippedSources)+ );+ const selectedCount = createMemo(() => selection().importIds.length);++ // Where to land after setup. Same-app relative paths only.+ const afterSetupTarget = () => {+ const next = searchParams.next;+ return typeof next === 'string' &&+ next.startsWith('/') &&+ !next.startsWith('//')+ ? next+ : '/';+ };++ /**+ * Import the current selection and hold until the rows settle (the pills+ * animate importing → "in Macro" live while this waits). Returns how many+ * accepted items had NOT landed as imported when the wait ended — failed+ * rows fall back to `staged` with an error, and a capped wait counts the+ * stragglers too.+ */+ const runSelectedImports = async (): Promise<number> => {+ const { importIds, discardIds } = selection();+ if (importIds.length === 0 && discardIds.length === 0) return 0;+ await runImport.mutateAsync({ importIds, discardIds });+ if (importIds.length === 0) return 0;++ const accepted = new Set(importIds);+ const deadline = Date.now() + FINISH_IMPORT_WAIT_MS;+ while (Date.now() < deadline) {+ // Through the shared cache, so concurrent subscribers stay in sync;+ // a single failed poll is not fatal (the next tick retries).+ const entities = await fetchImportState().then(+ (state) => state.entities,+ () => undefined+ );+ if (entities) {+ const inFlight = entities.some(+ (entity) => accepted.has(entity.id) && entity.status === 'importing'+ );+ if (!inFlight) {+ return entities.filter(+ (entity) => accepted.has(entity.id) && entity.status !== 'imported'+ ).length;+ }+ }+ await sleep(FINISH_POLL_INTERVAL_MS);+ }+ return 0; // Timed out waiting — imports continue server-side.+ };++ // Import whatever is still selected (holding while the pills land), then+ // mark the flow completed server-side (which removes leftover+ // onboarding-staged candidates) and the legacy tutorial done (suppressing+ // the old modal), then land in the app.+ const finish = async (skipped: boolean) => {+ if (finishing()) return;+ setFinishing(true);+ try {+ if (!skipped) {+ // A hard failure here means the import request never took: don't+ // complete onboarding over the user's dropped selection. (A settle+ // timeout is different — runSelectedImports resolves 0 for it and+ // the imports keep landing server-side.)+ try {+ const failed = await runSelectedImports();+ if (failed > 0) {+ toast.failure(`${failed} items failed to import`);+ }+ } catch {+ toast.failure('Importing failed — please try again');+ return;+ }+ }+ await Promise.allSettled([+ completeOnboarding.mutateAsync({ skipped }),+ completeTutorial.mutateAsync(),+ ]);+ // NewOnboardingRedirect keys off userInfo.tutorialComplete: navigating+ // before the cache reflects the PATCH would bounce straight back here.+ // Refetch and verify — if the PATCH failed (allSettled hides it) or+ // the refetch missed, stay put and let the user retry.+ await queryClient+ .refetchQueries({ queryKey: authKeys.userInfo.queryKey })+ .catch(() => {});+ if (userInfoQuery.data?.tutorialComplete !== true) {+ toast.failure("Couldn't finish setup — please try again");+ return;+ }+ navigate(afterSetupTarget(), { replace: true });+ } finally {+ setFinishing(false);+ }+ };++ return { finishing, finish, selectedCount, afterSetupTarget };+}diff --git a/apps/web/src/features/setup/primitives.tsx b/apps/web/src/features/setup/primitives.tsxnew file mode 100644index 0000000000..ed76325385--- /dev/null+++ b/apps/web/src/features/setup/primitives.tsx@@ -0,0 +1,213 @@+import CheckIcon from '@phosphor-icons/core/regular/check.svg?component-solid';+import { cn, Layer } from '@ui';+import { For, type JSX, Show } from 'solid-js';+import { StatusDot } from '../settings/integration-ui';++/*+ * Building blocks for the /setup import panel: one bordered card per+ * connected source, whose body is a cloud of narrow, content-fit pills.+ * Selection happens per-card (a section-level toggle), never per-pill.+ */++/**+ * One source's card: a connector-first header (icon, name, count badge,+ * right-aligned actions), a recessed status well underneath — the same+ * inset treatment as the soup's active-filters bar — carrying the green+ * "Connected" dot and a blurb saying what Macro found, and the pill cloud+ * as the body.+ */+export function ImportCard(props: {+ /** The connector's brand icon. */+ icon?: JSX.Element;+ /** The connector's name ("Notion") — the card leads with the tool. */+ title: string;+ /** Item count shown in a rounded badge beside the title. */+ count?: number;+ /** Lead the status well with the green dot + "Connected —". */+ connected?: boolean;+ /** Right-aligned header controls (the section import toggle). */+ actions?: JSX.Element;+ /**+ * Status blurb rendered in the well after the "Connected —" lead-in, so+ * it should start lowercase ("here are 10 issues…").+ */+ status: JSX.Element;+ children?: JSX.Element;+}) {+ return (+ <Layer depth={2}>+ <section class="flex flex-col gap-3 rounded-xl border border-ink/[0.05] bg-surface p-5">+ <div class="flex items-center gap-2">+ <Show when={props.icon}>+ <span class="flex size-4 shrink-0 items-center justify-center [&_svg]:size-4">+ {props.icon}+ </span>+ </Show>+ <h3 class="min-w-0 truncate text-sm font-semibold text-ink">+ {props.title}+ </h3>+ <Show when={props.count !== undefined}>+ <span class="shrink-0 rounded-full bg-ink/10 px-1.5 py-px text-xs font-medium tabular-nums text-ink-extra-muted">+ {props.count}+ </span>+ </Show>+ <Show when={props.actions}>+ <span class="ml-auto flex shrink-0 items-center gap-2">+ {props.actions}+ </span>+ </Show>+ </div>+ {/* Recessed status well (Layer depth 0 drops bg-surface to the+ darkest level, same as the soup active-filters bar). */}+ <Layer depth={0}>+ <div class="flex flex-wrap items-center gap-x-1.5 gap-y-1 rounded-lg border border-edge-muted bg-surface px-3 py-2.5 text-[13px] text-ink-muted">+ <Show when={props.connected}>+ <StatusDot state="connected" />+ <span class="font-medium text-ink">Connected</span>+ <span>—</span>+ </Show>+ {props.status}+ </div>+ </Layer>+ <Show when={props.children}>{props.children}</Show>+ </section>+ </Layer>+ );+}++/** Wrapping row of narrow pills. */+export function PillGrid(props: { children: JSX.Element }) {+ return <div class="flex flex-wrap gap-1.5">{props.children}</div>;+}++/**+ * A narrow, content-fit pill for one importable item, whose content reads+ * like an inline @ mention: entity icon, optional muted identifier, the name+ * with the mention underline, then trailing status. Width hugs the content+ * (name truncated past a cap) so a section reads as a compact cloud instead+ * of a full-width list. Pills are display-only — whether they import is+ * decided by the card's section toggle — except imported ones, which open+ * the entity they became.+ */+export function ItemPill(props: {+ icon?: JSX.Element;+ /** Muted shorthand before the name (e.g. a Linear identifier "MAC-42"). */+ code?: string;+ label: string;+ /** Trailing status icons (e.g. an in-flight spinner). */+ status?: JSX.Element;+ /** Hover detail (subtitle/description); falls back to the label. */+ title?: string;+ /**+ * The Macro entity this item already became (imported by the user or a+ * teammate). Renders as a quiet "already in your workspace" pill that+ * opens the existing entity.+ */+ importedHref?: string;+}) {+ const content = (+ <>+ <Show when={props.icon}>+ <span+ class={cn(+ 'flex size-3.5 shrink-0 items-center justify-center [&_svg]:size-3.5',+ props.importedHref !== undefined ? 'opacity-50' : 'opacity-90'+ )}+ >+ {props.icon}+ </span>+ </Show>+ <Show when={props.code}>+ <span class="shrink-0 text-xs tabular-nums text-ink-extra-muted">+ {props.code}+ </span>+ </Show>+ <span class="min-w-0 truncate underline decoration-current/20 decoration-[max(1px,0.1em)] underline-offset-2">+ {props.label}+ </span>+ <Show when={props.status}>+ <span class="flex shrink-0 items-center gap-1">{props.status}</span>+ </Show>+ </>+ );+ const base =+ 'inline-flex h-7 max-w-72 items-center gap-1.5 rounded-lg border px-2.5 text-[13px]';++ return (+ <Show+ when={props.importedHref}+ fallback={+ <span+ title={props.title ?? props.label}+ class={cn(base, 'border-ink/10 bg-surface text-ink')}+ >+ {content}+ </span>+ }+ >+ {(href) => (+ <button+ type="button"+ title="Already in your workspace — click to open"+ onClick={() => window.open(href(), '_blank')}+ class={cn(+ base,+ 'cursor-default border-ink/5 bg-ink/[0.03] text-ink-extra-muted outline-none',+ 'transition-colors hover:text-ink-muted focus-visible:ring-1 focus-visible:ring-accent/50'+ )}+ >+ {content}+ <span class="flex shrink-0 items-center gap-1 text-xs text-ink-extra-muted">+ <CheckIcon class="size-3 shrink-0" />+ in Macro+ </span>+ </button>+ )}+ </Show>+ );+}++const SKELETON_WIDTHS = [88, 128, 72, 148, 104, 80, 136, 96];++/**+ * Shimmering placeholder pills while a gather job runs: ghost outlines of+ * the real pills (icon dot + text bar) pulsing on a slight stagger. Renders+ * bare spans so it can sit inside a `PillGrid` after the pills that already+ * landed.+ */+export function SkeletonPills(props: { count?: number }) {+ return (+ <For each={SKELETON_WIDTHS.slice(0, props.count ?? 6)}>+ {(width, index) => (+ <span+ class="flex h-7 w-(--pill-width) animate-pulse items-center gap-1.5 rounded-lg border border-ink/5 px-2.5 [animation-delay:var(--pill-delay)]"+ style={{+ '--pill-width': `${width}px`,+ '--pill-delay': `${index() * 150}ms`,+ }}+ >+ <span class="size-3.5 shrink-0 rounded-full bg-ink/10" />+ <span class="h-2 min-w-0 flex-1 rounded-full bg-ink/10" />+ </span>+ )}+ </For>+ );+}++/** Inline failure note with a retry affordance. */+export function FailureNote(props: { message?: string; onRetry: () => void }) {+ return (+ <span class="flex items-center gap-3">+ <span class="min-w-0 truncate">+ {props.message ?? "Something went wrong — this one's on us."}+ </span>+ <button+ type="button"+ class="shrink-0 font-medium text-ink-muted transition-colors hover:text-ink"+ onClick={() => props.onRetry()}+ >+ Retry+ </button>+ </span>+ );+}diff --git a/apps/web/src/features/setup/selection.ts b/apps/web/src/features/setup/selection.tsnew file mode 100644index 0000000000..2a9446615e--- /dev/null+++ b/apps/web/src/features/setup/selection.ts@@ -0,0 +1,46 @@+import type { ImportEntity, ImportSource } from '@queries/import';++/*+ * Pure selection logic for the /setup import panel: which sources exist,+ * and how the per-source skip set maps onto the accept/decline id lists+ * `POST /import/run` takes. No Solid primitives here.+ */++/** Per-source skip set: sections import by default; `true` skips one. */+export type SkippedSources = Partial<Record<ImportSource, boolean>>;++/** Display order, connector identity, and item noun for one source. */+export interface SourceSection {+ source: ImportSource;+ serverName: string;+ /** What the items are called in blurbs ("we found 16 documents…"). */+ noun: string;+}++/** All import sources, in display order. */+export const SOURCE_SECTIONS: SourceSection[] = [+ { source: 'linear', serverName: 'Linear', noun: 'issues' },+ { source: 'notion', serverName: 'Notion', noun: 'documents' },+ { source: 'slack', serverName: 'Slack', noun: 'channels' },+];++/**+ * Split the user's staged rows into accept/decline id lists from the skip+ * set. What "Continue to Macro" sends to `POST /import/run`.+ */+export function stagedSelection(+ entities: ImportEntity[] | undefined,+ skippedSources: SkippedSources+): { importIds: string[]; discardIds: string[] } {+ const staged = (entities ?? []).filter(+ (entity) => entity.status === 'staged'+ );+ return {+ importIds: staged+ .filter((entity) => !skippedSources[entity.source])+ .map((entity) => entity.id),+ discardIds: staged+ .filter((entity) => skippedSources[entity.source])+ .map((entity) => entity.id),+ };+}diff --git a/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsx b/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsxnew file mode 100644index 0000000000..a6890c8ffb--- /dev/null+++ b/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsx@@ -0,0 +1,63 @@+import TrayArrowDown from '@phosphor-icons/core/regular/tray-arrow-down.svg';+import { BaseTool } from './BaseTool';+import { createToolRenderer } from './ToolRenderer';++/*+ * Renderers for the import-ledger tools the chat agent can call+ * (CreateImportEntity / DeleteImportEntity / ListImportEntities). The tools+ * only move ledger rows — entity creation happens server-side — so the+ * renders are one-line summaries.+ */++/** Human label for the item a call is about, from its source metadata. */+function itemLabel(metadata: { [k: string]: unknown }): string | undefined {+ for (const key of ['title', 'name', 'identifier']) {+ const value = metadata[key];+ if (typeof value === 'string' && value.length > 0) return value;+ }+ return undefined;+}++export const createImportEntityHandler = createToolRenderer({+ name: 'CreateImportEntity',+ render: (ctx) => (+ <BaseTool+ icon={TrayArrowDown}+ renderContext={ctx.renderContext}+ type="call"+ >+ <div class="min-w-0 flex-1 truncate">+ {ctx.tool.data.status === 'imported' ? 'Record import of' : 'Stage'}{' '}+ <span class="text-ink">+ {itemLabel(ctx.tool.data.metadata) ?? ctx.tool.data.foreignId}+ </span>+ </div>+ </BaseTool>+ ),+});++export const deleteImportEntityHandler = createToolRenderer({+ name: 'DeleteImportEntity',+ render: (ctx) => (+ <BaseTool+ icon={TrayArrowDown}+ renderContext={ctx.renderContext}+ type="call"+ >+ Decline an import candidate+ </BaseTool>+ ),+});++export const listImportEntitiesHandler = createToolRenderer({+ name: 'ListImportEntities',+ render: (ctx) => (+ <BaseTool+ icon={TrayArrowDown}+ renderContext={ctx.renderContext}+ type="call"+ >+ List import candidates+ </BaseTool>+ ),+});diff --git a/apps/web/src/lib/core/component/AI/component/tool/handler.tsx b/apps/web/src/lib/core/component/AI/component/tool/handler.tsxindex 827b17e6d3..c4b65d86d4 100644--- a/apps/web/src/lib/core/component/AI/component/tool/handler.tsx+++ b/apps/web/src/lib/core/component/AI/component/tool/handler.tsx@@ -13,6 +13,11 @@ import { displayResultsHandler } from './DisplayResults';import { editDocumentHandler } from './EditDocument';import { editTagHandler } from './EditTag';import { getThreadHandler } from './GetThread';+import {+ createImportEntityHandler,+ deleteImportEntityHandler,+ listImportEntitiesHandler,+} from './ImportTools';import { listCallRecordsHandler } from './ListCallRecords';import { listEntitiesHandler } from './ListEntities';import { listInboxesHandler } from './ListInboxes';@@ -60,10 +65,13 @@ import { webFetchHandler } from './WebFetch';import { webSearchHandler } from './WebSearch';const toolHandlers: ToolHandlerMap<RenderContext> = {+ CreateImportEntity: createImportEntityHandler,+ DeleteImportEntity: deleteImportEntityHandler,GetCompany: getCompanyHandler,GetEntityProperties: getEntityPropertiesHandler,ListCompanies: listCompaniesHandler,ListCallRecords: listCallRecordsHandler,+ ListImportEntities: listImportEntitiesHandler,ListEntities: listEntitiesHandler,ListInboxes: listInboxesHandler,ListLabels: listLabelsHandler,diff --git a/apps/web/src/lib/core/constant/featureFlags.ts b/apps/web/src/lib/core/constant/featureFlags.tsindex c4745ba1b4..2b9d7ca500 100644--- a/apps/web/src/lib/core/constant/featureFlags.ts+++ b/apps/web/src/lib/core/constant/featureFlags.ts@@ -541,3 +541,11 @@ export const UNIFIED_CHANNEL_INPUT = resolveFeatureFlag(export const BOT_MANAGEMENT_FLAG = 'bot-management';export const BOT_MANAGEMENT_OVERRIDE =resolveFeatureFlag('BOT_MANAGEMENT', DEV_MODE_ENV) || undefined;++// New split-screen onboarding (/setup): connect tools on the left, pick+// what to import on the right. Dev-mode default; override with+// VITE_ENABLE_NEW_ONBOARDING_V3.+export const ENABLE_NEW_ONBOARDING_V3 = resolveFeatureFlag(+ 'ENABLE_NEW_ONBOARDING_V3',+ DEV_MODE_ENV+);diff --git a/apps/web/src/lib/queries/import.ts b/apps/web/src/lib/queries/import.tsnew file mode 100644index 0000000000..cfab675344--- /dev/null+++ b/apps/web/src/lib/queries/import.ts@@ -0,0 +1,135 @@+/**+ * Queries and mutations for the import pipeline.+ *+ * The server owns everything: gather jobs stage candidates, `POST+ * /import/run` accepts/declines them, and import jobs flip rows to+ * `imported`. Row flips are pushed over the connection gateway as+ * `import_updated`; polling remains as the backstop (pushes can be missed+ * while reconnecting), tightening while anything is in flight.+ */+import { throwOnErr } from '@core/util/result';+import { queryClient } from '@queries/client';+import {+ type ImportEntity,+ type ImportSource,+ type ImportState,+ importClient,+} from '@service-cognition/import';+import { createConnectionWebsocketEffect } from '@service-connection/websocket';+import { useMutation, useQuery } from '@tanstack/solid-query';++export type {+ ImportEntity,+ ImportEntityStatus,+ ImportInitiator,+ ImportRun,+ ImportRunStatus,+ ImportSource,+ ImportState,+ LinearIssueMeta,+ NotionDocMeta,+ RunImportOutcome,+ SlackChannelMeta,+ SlackParticipant,+} from '@service-cognition/import';++const KEYS = {+ state: ['import', 'state'] as const,+};++/** Gateway push sent when an import row or gather run changes server-side. */+const IMPORT_UPDATED_MESSAGE_TYPE = 'import_updated';++/**+ * Stable empty aggregate served as `placeholderData` so this polling query+ * can never suspend (a query that has never succeeded would otherwise+ * re-suspend on every scheduled refetch and rhythmically blank whatever+ * Suspense boundary is above it).+ */+const PENDING_IMPORT_STATE: ImportState = { runs: [], entities: [] };++function anythingInFlight(state: ImportState | undefined): boolean {+ if (!state) return false;+ return (+ state.runs.some((run) => run.status === 'running') ||+ state.entities.some((entity) => entity.status === 'importing')+ );+}++/** The import aggregate: gather runs plus visible ledger rows. */+export function useImportQuery(options?: { enabled?: () => boolean }) {+ createConnectionWebsocketEffect((message) => {+ if (message.type !== IMPORT_UPDATED_MESSAGE_TYPE) return;+ void invalidateImportState();+ });++ return useQuery(() => ({+ queryKey: KEYS.state,+ queryFn: async () => throwOnErr(() => importClient.getState()),+ enabled: options?.enabled ? options.enabled() : true,+ refetchInterval: (query) =>+ anythingInFlight(query.state.data) ? 3_000 : 15_000,+ placeholderData: PENDING_IMPORT_STATE,+ }));+}++function invalidateImportState() {+ return queryClient.invalidateQueries({ queryKey: KEYS.state });+}++/**+ * Imperatively fetch the import aggregate through the shared cache — for+ * non-component polling loops (the setup finish hold). Keeps every read on+ * the TanStack path so concurrent `useImportQuery` subscribers see the same+ * data.+ */+export function fetchImportState(): Promise<ImportState> {+ return queryClient.fetchQuery({+ queryKey: KEYS.state,+ queryFn: async () => throwOnErr(() => importClient.getState()),+ staleTime: 0,+ });+}++/**+ * Accept and/or decline staged rows. The server flips accepted rows to+ * `importing` and returns immediately; completion arrives via+ * `import_updated` pushes and polling.+ */+export function useRunImportMutation() {+ return useMutation(() => ({+ mutationFn: async (args: { importIds: string[]; discardIds: string[] }) =>+ throwOnErr(() =>+ importClient.runImport({+ import_ids: args.importIds,+ discard_ids: args.discardIds,+ })+ ),+ onSuccess: () => void invalidateImportState(),+ }));+}++/** Restart a failed gather run. */+export function useRetryGatherMutation() {+ return useMutation(() => ({+ mutationFn: async (source: ImportSource) =>+ throwOnErr(() => importClient.retryGather(source)),+ onSuccess: () => void invalidateImportState(),+ }));+}++/** Dismiss one source's import section. */+export function useDismissRunMutation() {+ return useMutation(() => ({+ mutationFn: async (source: ImportSource) =>+ throwOnErr(() => importClient.dismissRun(source)),+ onSuccess: () => void invalidateImportState(),+ }));+}++/** A human label for a ledger row, from its per-source metadata. */+export function entityLabel(entity: ImportEntity): string {+ const field = entity.source === 'slack' ? 'name' : 'title';+ const value = entity.metadata[field];+ return typeof value === 'string' && value.length > 0 ? value : '(unnamed)';+}diff --git a/apps/web/src/lib/queries/mcp-servers.ts b/apps/web/src/lib/queries/mcp-servers.tsindex f2bf520aa6..1902c85659 100644--- a/apps/web/src/lib/queries/mcp-servers.ts+++ b/apps/web/src/lib/queries/mcp-servers.ts@@ -14,13 +14,34 @@ const KEYS = {list: ['mcpServers', 'list'] as const,};-export function useMcpServersQuery() {+/** Stable placeholder for `neverSuspend` consumers (see below). */+const NO_SERVERS: ServerResponse[] = [];++export function useMcpServersQuery(options?: {+ /**+ * Poll for connection changes. OAuth finishes in another tab, and if this+ * one stayed visible no focus refetch fires — surfaces that must flip+ * promptly (the setup connector cards) pass a short interval.+ */+ refetchInterval?: number;+ /**+ * Serve an empty placeholder instead of suspending on first load. Only+ * for polling surfaces with their own Suspense boundary (the setup+ * connector cards), where a query that has never succeeded would+ * re-suspend on every scheduled refetch and blank the page rhythmically.+ * Everything else keeps suspending — with the placeholder they would+ * flash a fake "nothing connected" success state on first load.+ */+ neverSuspend?: boolean;+}) {return useQuery(() => ({queryKey: KEYS.list,queryFn: async () =>throwOnErr(async () => await cognitionApiServiceClient.listMcpServers()),refetchOnMount: 'always' as const,refetchOnWindowFocus: 'always' as const,+ refetchInterval: options?.refetchInterval,+ placeholderData: options?.neverSuspend ? NO_SERVERS : undefined,}));}diff --git a/apps/web/src/lib/queries/onboarding.ts b/apps/web/src/lib/queries/onboarding.tsnew file mode 100644index 0000000000..fc9fb9b29d--- /dev/null+++ b/apps/web/src/lib/queries/onboarding.ts@@ -0,0 +1,61 @@+/**+ * Queries and mutations for the onboarding flow row.+ *+ * The server observes connections itself: reading the state starts any due+ * import gather runs, and connector OAuth completions reconcile instantly+ * via a server hook. All import state lives in `@queries/import`.+ */+import { throwOnErr } from '@core/util/result';+import { queryClient } from '@queries/client';+import {+ type OnboardingState,+ onboardingClient,+} from '@service-cognition/onboarding';+import { useMutation, useQuery } from '@tanstack/solid-query';++export type {+ ConnectedServer,+ OnboardingRow,+ OnboardingState,+ OnboardingStatus,+} from '@service-cognition/onboarding';++const KEYS = {+ state: ['onboarding', 'state'] as const,+};++/**+ * Non-suspending placeholder: an active flow with nothing known yet (see+ * the import query for why polling queries here never suspend).+ */+const PENDING_ONBOARDING_STATE: OnboardingState = {+ row: {+ status: 'active',+ skipped: false,+ started_at: '1970-01-01T00:00:00.000Z',+ },+ connected_servers: [],+};++/** The onboarding row + connections. Polling keeps gather runs starting. */+export function useOnboardingQuery(options?: { enabled?: () => boolean }) {+ return useQuery(() => ({+ queryKey: KEYS.state,+ queryFn: async () => throwOnErr(() => onboardingClient.getState()),+ enabled: options?.enabled ? options.enabled() : true,+ refetchInterval: 12_000,+ placeholderData: PENDING_ONBOARDING_STATE,+ }));+}++function invalidateOnboarding() {+ return queryClient.invalidateQueries({ queryKey: KEYS.state });+}++export function useCompleteOnboardingMutation() {+ return useMutation(() => ({+ mutationFn: async (args: { skipped: boolean }) =>+ throwOnErr(() => onboardingClient.complete(args)),+ onSuccess: () => void invalidateOnboarding(),+ }));+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/client.ts b/apps/web/src/lib/service-clients/service-cognition/generated/client.tsindex d2fdbe8da2..f270127084 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/client.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/client.ts@@ -11,6 +11,7 @@ import type {ChatHistory,ChatHistoryBatchMessagesRequest,ChatMessageError,+ CompleteOnboardingRequest,CreateChatRequest,DeleteMcpServerParams,DocumentTextPart,@@ -25,9 +26,13 @@ import type {McpAuthCallbackParams,MemoryErrorBody,MemoryResponse,+ OnboardingRow,+ OnboardingState,PatchChatRequest,ProjectionStateResponse,RejectToolCallRequest,+ RunImportOutcome,+ RunImportRequest,SendChatMessageResponse,ServerResponse,SetPricingRequest,@@ -1195,6 +1200,214 @@ export const healthHandler = async (} as healthHandlerResponse;};+/**+ * @summary Accept staged imports (starts import jobs) and/or discard staged rows.+ */+export type runImportHandlerResponse200 = {+ data: RunImportOutcome;+ status: 200;+};++export type runImportHandlerResponse500 = {+ data: void;+ status: 500;+};++export type runImportHandlerResponseSuccess = runImportHandlerResponse200 & {+ headers: Headers;+};+export type runImportHandlerResponseError = runImportHandlerResponse500 & {+ headers: Headers;+};++export type runImportHandlerResponse =+ | runImportHandlerResponseSuccess+ | runImportHandlerResponseError;++export const getRunImportHandlerUrl = () => {+ return `/import/run`;+};++export const runImportHandler = async (+ runImportRequest: RunImportRequest,+ options?: RequestInit+): Promise<runImportHandlerResponse> => {+ const res = await fetch(getRunImportHandlerUrl(), {+ ...options,+ method: 'POST',+ headers: { 'Content-Type': 'application/json', ...options?.headers },+ body: JSON.stringify(runImportRequest),+ });++ const body = [204, 205, 304].includes(res.status) ? null : await res.text();++ const data: runImportHandlerResponse['data'] = body ? JSON.parse(body) : {};+ return {+ data,+ status: res.status,+ headers: res.headers,+ } as runImportHandlerResponse;+};++/**+ * @summary Dismiss one source's import section.+ */+export type dismissRunHandlerResponse204 = {+ data: void;+ status: 204;+};++export type dismissRunHandlerResponse400 = {+ data: void;+ status: 400;+};++export type dismissRunHandlerResponse500 = {+ data: void;+ status: 500;+};++export type dismissRunHandlerResponseSuccess = dismissRunHandlerResponse204 & {+ headers: Headers;+};+export type dismissRunHandlerResponseError = (+ | dismissRunHandlerResponse400+ | dismissRunHandlerResponse500+) & {+ headers: Headers;+};++export type dismissRunHandlerResponse =+ | dismissRunHandlerResponseSuccess+ | dismissRunHandlerResponseError;++export const getDismissRunHandlerUrl = (source: string) => {+ return `/import/runs/${source}/dismiss`;+};++export const dismissRunHandler = async (+ source: string,+ options?: RequestInit+): Promise<dismissRunHandlerResponse> => {+ const res = await fetch(getDismissRunHandlerUrl(source), {+ ...options,+ method: 'POST',+ });++ const body = [204, 205, 304].includes(res.status) ? null : await res.text();++ const data: dismissRunHandlerResponse['data'] = body ? JSON.parse(body) : {};+ return {+ data,+ status: res.status,+ headers: res.headers,+ } as dismissRunHandlerResponse;+};++/**+ * @summary Restart a failed (or dismissed) gather run for one source.+ */+export type retryGatherHandlerResponse204 = {+ data: void;+ status: 204;+};++export type retryGatherHandlerResponse400 = {+ data: void;+ status: 400;+};++export type retryGatherHandlerResponse500 = {+ data: void;+ status: 500;+};++export type retryGatherHandlerResponseSuccess =+ retryGatherHandlerResponse204 & {+ headers: Headers;+ };+export type retryGatherHandlerResponseError = (+ | retryGatherHandlerResponse400+ | retryGatherHandlerResponse500+) & {+ headers: Headers;+};++export type retryGatherHandlerResponse =+ | retryGatherHandlerResponseSuccess+ | retryGatherHandlerResponseError;++export const getRetryGatherHandlerUrl = (source: string) => {+ return `/import/runs/${source}/retry`;+};++export const retryGatherHandler = async (+ source: string,+ options?: RequestInit+): Promise<retryGatherHandlerResponse> => {+ const res = await fetch(getRetryGatherHandlerUrl(source), {+ ...options,+ method: 'POST',+ });++ const body = [204, 205, 304].includes(res.status) ? null : await res.text();++ const data: retryGatherHandlerResponse['data'] = body ? JSON.parse(body) : {};+ return {+ data,+ status: res.status,+ headers: res.headers,+ } as retryGatherHandlerResponse;+};++/**+ * @summary Get the authenticated user's onboarding state. While the flow is active+this also starts any gather runs that are due, so polling this endpoint+is what keeps onboarding moving.+ */+export type getStateHandlerResponse200 = {+ data: OnboardingState;+ status: 200;+};++export type getStateHandlerResponse500 = {+ data: void;+ status: 500;+};++export type getStateHandlerResponseSuccess = getStateHandlerResponse200 & {+ headers: Headers;+};+export type getStateHandlerResponseError = getStateHandlerResponse500 & {+ headers: Headers;+};++export type getStateHandlerResponse =+ | getStateHandlerResponseSuccess+ | getStateHandlerResponseError;++export const getGetStateHandlerUrl = () => {+ return `/onboarding`;+};++export const getStateHandler = async (+ options?: RequestInit+): Promise<getStateHandlerResponse> => {+ const res = await fetch(getGetStateHandlerUrl(), {+ ...options,+ method: 'GET',+ });++ const body = [204, 205, 304].includes(res.status) ? null : await res.text();++ const data: getStateHandlerResponse['data'] = body ? JSON.parse(body) : {};+ return {+ data,+ status: res.status,+ headers: res.headers,+ } as getStateHandlerResponse;+};+/*** @summary List all MCP servers configured for the authenticated user.*/@@ -1609,6 +1822,56 @@ export const getMemoryHandler = async (} as getMemoryHandlerResponse;};+/**+ * @summary Complete (or skip) onboarding. Leftover onboarding-staged import+candidates are discarded.+ */+export type completeHandlerResponse200 = {+ data: OnboardingRow;+ status: 200;+};++export type completeHandlerResponse500 = {+ data: void;+ status: 500;+};++export type completeHandlerResponseSuccess = completeHandlerResponse200 & {+ headers: Headers;+};+export type completeHandlerResponseError = completeHandlerResponse500 & {+ headers: Headers;+};++export type completeHandlerResponse =+ | completeHandlerResponseSuccess+ | completeHandlerResponseError;++export const getCompleteHandlerUrl = () => {+ return `/onboarding/complete`;+};++export const completeHandler = async (+ completeOnboardingRequest: CompleteOnboardingRequest,+ options?: RequestInit+): Promise<completeHandlerResponse> => {+ const res = await fetch(getCompleteHandlerUrl(), {+ ...options,+ method: 'POST',+ headers: { 'Content-Type': 'application/json', ...options?.headers },+ body: JSON.stringify(completeOnboardingRequest),+ });++ const body = [204, 205, 304].includes(res.status) ? null : await res.text();++ const data: completeHandlerResponse['data'] = body ? JSON.parse(body) : {};+ return {+ data,+ status: res.status,+ headers: res.headers,+ } as completeHandlerResponse;+};+export type getBatchPreviewResponse200 = {data: GetBatchPreviewResponse;status: 200;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.tsindex 3be82b6bd9..2153b3171f 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/aiFeature.ts@@ -22,4 +22,5 @@ export const AiFeature = {channel_bot: 'channel_bot',ai_projection: 'ai_projection',ai_editing: 'ai_editing',+ import: 'import',} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/completeOnboardingRequest.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/completeOnboardingRequest.tsnew file mode 100644index 0000000000..74642ef784--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/completeOnboardingRequest.ts@@ -0,0 +1,14 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Body for completing onboarding.+ */+export interface CompleteOnboardingRequest {+ /** Whether the user skipped rather than finished. */+ skipped?: boolean;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/connectedServer.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/connectedServer.tsnew file mode 100644index 0000000000..ace349eb07--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/connectedServer.ts@@ -0,0 +1,18 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * One of the user's MCP server connections, as the setup page sees it.+ */+export interface ConnectedServer {+ /** Whether the user has completed OAuth against it. */+ authenticated: boolean;+ /** The server's display name. */+ name: string;+ /** The server URL. */+ url: string;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntity.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntity.tsnew file mode 100644index 0000000000..be9e440b23--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntity.ts@@ -0,0 +1,49 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { ImportEntityEntityId } from './importEntityEntityId';+import type { ImportEntityEntityType } from './importEntityEntityType';+import type { ImportEntityLastError } from './importEntityLastError';+import type { ImportEntityTeamId } from './importEntityTeamId';+import type { ImportSource } from './importSource';+import type { ImportStatus } from './importStatus';+import type { Initiator } from './initiator';++/**+ * One row of the import ledger.+ */+export interface ImportEntity {+ /** When the row was first staged. */+ created_at: string;+ /** The Macro entity it became, once imported. */+ entity_id?: ImportEntityEntityId;+ /** The Macro entity type (`task` | `md` | `channel`), once imported. */+ entity_type?: ImportEntityEntityType;+ /** Stable id in the source system (see+[`ImportSource::normalize_foreign_id`]). */+ foreign_id: string;+ /** Ledger row id. */+ id: string;+ /** Where it was first staged from. */+ initiator: Initiator;+ /** Failure detail from the last import attempt, when one failed. */+ last_error?: ImportEntityLastError;+ /** Per-source metadata (shape depends on `source`; see+[`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`]). */+ metadata: unknown;+ /** The external system the item comes from. */+ source: ImportSource;+ /** Where the row is in its lifecycle. */+ status: ImportStatus;+ /** The team the created entity was shared with, when it was. */+ team_id?: ImportEntityTeamId;+ /** When the row last changed. */+ updated_at: string;+ /** The user who staged/imported it. Team-imported rows from teammates+are visible with their owner's id, so clients can say "created by a+teammate". */+ user_id: string;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityEntityId.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityEntityId.tsnew file mode 100644index 0000000000..0e1d0407ba--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityEntityId.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The Macro entity it became, once imported.+ */+export type ImportEntityEntityId = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityEntityType.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityEntityType.tsnew file mode 100644index 0000000000..28fd963bf5--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityEntityType.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The Macro entity type (`task` | `md` | `channel`), once imported.+ */+export type ImportEntityEntityType = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityLastError.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityLastError.tsnew file mode 100644index 0000000000..d52e775e3b--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityLastError.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Failure detail from the last import attempt, when one failed.+ */+export type ImportEntityLastError = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityTeamId.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityTeamId.tsnew file mode 100644index 0000000000..c5c383de9d--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importEntityTeamId.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The team the created entity was shared with, when it was.+ */+export type ImportEntityTeamId = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRun.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRun.tsnew file mode 100644index 0000000000..632e8a5127--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRun.ts@@ -0,0 +1,23 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { ImportRunError } from './importRunError';+import type { ImportSource } from './importSource';+import type { RunStatus } from './runStatus';++/**+ * Gather-run state for one user × source.+ */+export interface ImportRun {+ /** Failure detail, when the run failed. */+ error?: ImportRunError;+ /** The source this run gathered from. */+ source: ImportSource;+ /** Where the run is in its lifecycle. */+ status: RunStatus;+ /** When the run state last changed. */+ updated_at: string;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunError.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunError.tsnew file mode 100644index 0000000000..c58739b0ec--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importRunError.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Failure detail, when the run failed.+ */+export type ImportRunError = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importSource.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importSource.tsnew file mode 100644index 0000000000..0992a5ec84--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importSource.ts@@ -0,0 +1,18 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * External systems items can be imported from.+ */+export type ImportSource = (typeof ImportSource)[keyof typeof ImportSource];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const ImportSource = {+ linear: 'linear',+ notion: 'notion',+ slack: 'slack',+} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importState.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importState.tsnew file mode 100644index 0000000000..1eaf4c95f4--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importState.ts@@ -0,0 +1,19 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { ImportEntity } from './importEntity';+import type { ImportRun } from './importRun';++/**+ * The full import aggregate for a user: gather runs plus visible ledger+rows (their own, and teammates' team-imported rows).+ */+export interface ImportState {+ /** Visible import entities, newest first. */+ entities: ImportEntity[];+ /** Gather runs, one per source that ever gathered. */+ runs: ImportRun[];+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importStatus.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importStatus.tsnew file mode 100644index 0000000000..4c9303af5c--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/importStatus.ts@@ -0,0 +1,19 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Lifecycle of one import entity.+ */+export type ImportStatus = (typeof ImportStatus)[keyof typeof ImportStatus];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const ImportStatus = {+ staged: 'staged',+ importing: 'importing',+ imported: 'imported',+ discarded: 'discarded',+} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.tsindex ebef92894d..c5868a9924 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/index.ts@@ -83,8 +83,10 @@ export * from './chatStreamOneOfSix';export * from './chatStreamOneOfSixType';export * from './chatTokenCount';export * from './chatUpdatedAt';+export * from './completeOnboardingRequest';export * from './completionUsage';export * from './completionUsageEntity';+export * from './connectedServer';export * from './conversationRecord';export * from './createChatRequest';export * from './createChatRequestName';@@ -116,7 +118,27 @@ export * from './httpSendChatMessageRequest';export * from './httpSendChatMessageRequestAdditionalInstructions';export * from './httpSendChatMessageRequestAttachments';export * from './httpSendChatMessageRequestChatId';+export * from './importEntity';+export * from './importEntityEntityId';+export * from './importEntityEntityType';+export * from './importEntityLastError';+export * from './importEntityTeamId';+export * from './importRun';+export * from './importRunError';+export * from './importSource';+export * from './importState';+export * from './importStatus';+export * from './initiator';export * from './jwtPayload';+export * from './linearIssueMeta';+export * from './linearIssueMetaAssignee';+export * from './linearIssueMetaAssigneeEmail';+export * from './linearIssueMetaDescription';+export * from './linearIssueMetaDueDate';+export * from './linearIssueMetaIdentifier';+export * from './linearIssueMetaPriority';+export * from './linearIssueMetaStatus';+export * from './linearIssueMetaUrl';export * from './macroUserIdStr';export * from './mcpAuthCallbackParams';export * from './memoryErrorBody';@@ -127,6 +149,13 @@ export * from './newChatMessage';export * from './newChatMessageAttachments';export * from './newChatMessageId';export * from './newMessageAttachment';+export * from './notionDocMeta';+export * from './notionDocMetaSummary';+export * from './notionDocMetaUrl';+export * from './onboardingRow';+export * from './onboardingRowCompletedAt';+export * from './onboardingState';+export * from './onboardingStatus';export * from './patchChatRequest';export * from './patchChatRequestName';export * from './patchChatRequestProjectId';@@ -141,6 +170,9 @@ export * from './projectionStatus';export * from './refreshCadence';export * from './rejectToolCallRequest';export * from './role';+export * from './runImportOutcome';+export * from './runImportRequest';+export * from './runStatus';export * from './sendChatMessagePayload';export * from './sendChatMessagePayloadAllOf';export * from './sendChatMessagePayloadAllOfAdditionalInstructions';@@ -151,6 +183,11 @@ export * from './setPricingRequest';export * from './sharePermissionV2';export * from './sharePermissionV2ChannelSharePermissions';export * from './sharePermissionV2PublicAccessLevel';+export * from './slackChannelMeta';+export * from './slackChannelMetaChannelId';+export * from './slackChannelMetaPurpose';+export * from './slackParticipant';+export * from './slackParticipantEmail';export * from './startAuthRequest';export * from './startAuthResponse';export * from './stopChatStreamError';diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.tsnew file mode 100644index 0000000000..adb00ec021--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/initiator.ts@@ -0,0 +1,18 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Where an import entity was first staged from. Provenance only — never a+visibility filter.+ */+export type Initiator = (typeof Initiator)[keyof typeof Initiator];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const Initiator = {+ onboarding: 'onboarding',+ chat: 'chat',+} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMeta.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMeta.tsnew file mode 100644index 0000000000..edda5ee614--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMeta.ts@@ -0,0 +1,38 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { LinearIssueMetaAssignee } from './linearIssueMetaAssignee';+import type { LinearIssueMetaAssigneeEmail } from './linearIssueMetaAssigneeEmail';+import type { LinearIssueMetaDescription } from './linearIssueMetaDescription';+import type { LinearIssueMetaDueDate } from './linearIssueMetaDueDate';+import type { LinearIssueMetaIdentifier } from './linearIssueMetaIdentifier';+import type { LinearIssueMetaPriority } from './linearIssueMetaPriority';+import type { LinearIssueMetaStatus } from './linearIssueMetaStatus';+import type { LinearIssueMetaUrl } from './linearIssueMetaUrl';++/**+ * Metadata for one staged Linear issue.+ */+export interface LinearIssueMeta {+ /** Assignee display name as Linear reports it. */+ assignee?: LinearIssueMetaAssignee;+ /** Assignee email, when Linear exposes it. */+ assignee_email?: LinearIssueMetaAssigneeEmail;+ /** Short markdown description. */+ description?: LinearIssueMetaDescription;+ /** Due date as an ISO date (`YYYY-MM-DD`), when set. */+ due_date?: LinearIssueMetaDueDate;+ /** Linear's human identifier (e.g. `ENG-142`). */+ identifier?: LinearIssueMetaIdentifier;+ /** Priority label (e.g. `Urgent`). */+ priority?: LinearIssueMetaPriority;+ /** Workflow status name (e.g. `In Progress`). */+ status?: LinearIssueMetaStatus;+ /** Issue title. */+ title: string;+ /** Deep link back to the issue in Linear. */+ url?: LinearIssueMetaUrl;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaAssignee.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaAssignee.tsnew file mode 100644index 0000000000..60a9d23e7b--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaAssignee.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Assignee display name as Linear reports it.+ */+export type LinearIssueMetaAssignee = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaAssigneeEmail.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaAssigneeEmail.tsnew file mode 100644index 0000000000..d29653c992--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaAssigneeEmail.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Assignee email, when Linear exposes it.+ */+export type LinearIssueMetaAssigneeEmail = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaDescription.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaDescription.tsnew file mode 100644index 0000000000..110c5f8c73--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaDescription.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Short markdown description.+ */+export type LinearIssueMetaDescription = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaDueDate.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaDueDate.tsnew file mode 100644index 0000000000..4b507e527d--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaDueDate.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Due date as an ISO date (`YYYY-MM-DD`), when set.+ */+export type LinearIssueMetaDueDate = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaIdentifier.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaIdentifier.tsnew file mode 100644index 0000000000..65d30c843a--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaIdentifier.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Linear's human identifier (e.g. `ENG-142`).+ */+export type LinearIssueMetaIdentifier = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaPriority.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaPriority.tsnew file mode 100644index 0000000000..8e6d22582b--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaPriority.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Priority label (e.g. `Urgent`).+ */+export type LinearIssueMetaPriority = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaStatus.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaStatus.tsnew file mode 100644index 0000000000..8081ad5bdd--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaStatus.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Workflow status name (e.g. `In Progress`).+ */+export type LinearIssueMetaStatus = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaUrl.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaUrl.tsnew file mode 100644index 0000000000..0d73fa56aa--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/linearIssueMetaUrl.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Deep link back to the issue in Linear.+ */+export type LinearIssueMetaUrl = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMeta.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMeta.tsnew file mode 100644index 0000000000..0ae6f375af--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMeta.ts@@ -0,0 +1,21 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { NotionDocMetaSummary } from './notionDocMetaSummary';+import type { NotionDocMetaUrl } from './notionDocMetaUrl';++/**+ * Metadata for one staged Notion page. Deliberately has NO content field:+page bodies are fetched at import time, for accepted pages only.+ */+export interface NotionDocMeta {+ /** One-line summary of what the page contains. */+ summary?: NotionDocMetaSummary;+ /** Page title. */+ title: string;+ /** Deep link back to the page in Notion. */+ url?: NotionDocMetaUrl;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaSummary.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaSummary.tsnew file mode 100644index 0000000000..702804f24f--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaSummary.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * One-line summary of what the page contains.+ */+export type NotionDocMetaSummary = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaUrl.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaUrl.tsnew file mode 100644index 0000000000..126a47d6bd--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/notionDocMetaUrl.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Deep link back to the page in Notion.+ */+export type NotionDocMetaUrl = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingRow.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingRow.tsnew file mode 100644index 0000000000..d838ea2348--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingRow.ts@@ -0,0 +1,22 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { OnboardingRowCompletedAt } from './onboardingRowCompletedAt';+import type { OnboardingStatus } from './onboardingStatus';++/**+ * One user's onboarding row.+ */+export interface OnboardingRow {+ /** When the flow completed, if it has. */+ completed_at?: OnboardingRowCompletedAt;+ /** Whether the user skipped rather than finished. */+ skipped: boolean;+ /** When the flow was first touched. */+ started_at: string;+ /** Where the flow is in its lifecycle. */+ status: OnboardingStatus;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingRowCompletedAt.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingRowCompletedAt.tsnew file mode 100644index 0000000000..38679ef6cc--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingRowCompletedAt.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * When the flow completed, if it has.+ */+export type OnboardingRowCompletedAt = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingState.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingState.tsnew file mode 100644index 0000000000..b9717113ab--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingState.ts@@ -0,0 +1,19 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { ConnectedServer } from './connectedServer';+import type { OnboardingRow } from './onboardingRow';++/**+ * The onboarding aggregate served to the setup page. Import runs and+staged entities are served separately by `GET /import/state`.+ */+export interface OnboardingState {+ /** The user's MCP server connections. */+ connected_servers: ConnectedServer[];+ /** The user's onboarding row. */+ row: OnboardingRow;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingStatus.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingStatus.tsnew file mode 100644index 0000000000..318bd32e07--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/onboardingStatus.ts@@ -0,0 +1,18 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Lifecycle of the onboarding flow itself.+ */+export type OnboardingStatus =+ (typeof OnboardingStatus)[keyof typeof OnboardingStatus];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const OnboardingStatus = {+ active: 'active',+ completed: 'completed',+} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runImportOutcome.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runImportOutcome.tsnew file mode 100644index 0000000000..9cad6b7295--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runImportOutcome.ts@@ -0,0 +1,22 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Outcome of accepting/declining staged rows via `POST /import/run`.+ */+export interface RunImportOutcome {+ /**+ * How many rows were discarded.+ * @minimum 0+ */+ discarded: number;+ /**+ * How many rows flipped to `importing` (jobs are now copying them in).+ * @minimum 0+ */+ importing: number;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runImportRequest.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runImportRequest.tsnew file mode 100644index 0000000000..cf36b00145--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runImportRequest.ts@@ -0,0 +1,16 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Body for accepting/declining staged imports.+ */+export interface RunImportRequest {+ /** Staged rows to discard. */+ discard_ids?: string[];+ /** Staged rows to import. */+ import_ids?: string[];+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runStatus.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runStatus.tsnew file mode 100644index 0000000000..7e8f126e8d--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/runStatus.ts@@ -0,0 +1,19 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Lifecycle of a gather run (one per user × source).+ */+export type RunStatus = (typeof RunStatus)[keyof typeof RunStatus];++// eslint-disable-next-line @typescript-eslint/no-redeclare+export const RunStatus = {+ running: 'running',+ ready: 'ready',+ failed: 'failed',+ dismissed: 'dismissed',+} as const;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMeta.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMeta.tsnew file mode 100644index 0000000000..75d69a732e--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMeta.ts@@ -0,0 +1,23 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { SlackChannelMetaChannelId } from './slackChannelMetaChannelId';+import type { SlackChannelMetaPurpose } from './slackChannelMetaPurpose';+import type { SlackParticipant } from './slackParticipant';++/**+ * Metadata for one staged Slack channel.+ */+export interface SlackChannelMeta {+ /** Slack's channel id (e.g. `C0123456789`), stable across renames. */+ channel_id?: SlackChannelMetaChannelId;+ /** Channel name without the leading `#`. */+ name: string;+ /** The channel's most relevant members, when discoverable. */+ participants?: SlackParticipant[];+ /** The channel's purpose/topic, when set. */+ purpose?: SlackChannelMetaPurpose;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaChannelId.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaChannelId.tsnew file mode 100644index 0000000000..193a8be558--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaChannelId.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Slack's channel id (e.g. `C0123456789`), stable across renames.+ */+export type SlackChannelMetaChannelId = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaPurpose.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaPurpose.tsnew file mode 100644index 0000000000..06e6af4c40--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackChannelMetaPurpose.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * The channel's purpose/topic, when set.+ */+export type SlackChannelMetaPurpose = string | null;diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackParticipant.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackParticipant.tsnew file mode 100644index 0000000000..a4b64e46e0--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackParticipant.ts@@ -0,0 +1,17 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */+import type { SlackParticipantEmail } from './slackParticipantEmail';++/**+ * A person active in a staged Slack channel.+ */+export interface SlackParticipant {+ /** Email, when the workspace exposes it. */+ email?: SlackParticipantEmail;+ /** Display name or Slack handle. */+ name: string;+}diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackParticipantEmail.ts b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackParticipantEmail.tsnew file mode 100644index 0000000000..f941715fa4--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/generated/schemas/slackParticipantEmail.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * Document Cognition Service+ * OpenAPI spec version: 1.0.0+ */++/**+ * Email, when the workspace exposes it.+ */+export type SlackParticipantEmail = string | null;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.tsindex e288a78b60..6303711186 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@@ -653,6 +653,110 @@ export const CreateDocumentResponse = z.object({documentId: z.string().uuid(),});+export const CreateImportEntity = z.object({+ entityId: z.union([z.string(), z.null()]).default(null),+ foreignId: z.string(),+ metadata: z.any(),+ source: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('linear'),+ z.literal('notion'),+ z.literal('slack'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ status: z.any().superRefine((x, ctx) => {+ const schemas = [z.literal('staged'), z.literal('imported')];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+});++export const CreateImportEntityResponse = z.object({+ entity: z.object({+ entityId: z.union([z.string(), z.null()]).optional(),+ entityType: z.union([z.string(), z.null()]).optional(),+ foreignId: z.string(),+ id: z.string().uuid(),+ importedByTeammate: z.boolean(),+ label: z.string(),+ source: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('linear'),+ z.literal('notion'),+ z.literal('slack'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ status: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('staged'),+ z.literal('importing'),+ z.literal('imported'),+ z.literal('discarded'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ }),+ message: z.string(),+ outcome: z.string(),+});+export const CreateTag = z.object({color: z.any().superRefine((x, ctx) => {const schemas = [@@ -737,6 +841,13 @@ export const CreateTagResponse = z.object({summary: z.string(),});+export const DeleteImportEntity = z.object({ id: z.string().uuid() });++export const DeleteImportEntityResponse = z.object({+ discarded: z.boolean(),+ message: z.string(),+});+export const DeleteTag = z.object({id: z.string().uuid(),property_definition_id: z.string().uuid(),@@ -1349,6 +1460,122 @@ export const ListEntitiesResponse = z.object({summary: z.string(),});+export const ListImportEntities = z.object({+ source: z+ .union([+ z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('linear'),+ z.literal('notion'),+ z.literal('slack'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ z.null(),+ ])+ .default(null),+ status: z+ .union([+ z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('staged'),+ z.literal('importing'),+ z.literal('imported'),+ z.literal('discarded'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ z.null(),+ ])+ .default(null),+});++export const ListImportEntitiesResponse = z.object({+ entities: z.array(+ z.object({+ entityId: z.union([z.string(), z.null()]).optional(),+ entityType: z.union([z.string(), z.null()]).optional(),+ foreignId: z.string(),+ id: z.string().uuid(),+ importedByTeammate: z.boolean(),+ label: z.string(),+ source: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('linear'),+ z.literal('notion'),+ z.literal('slack'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ status: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('staged'),+ z.literal('importing'),+ z.literal('imported'),+ z.literal('discarded'),+ ];+ const errors = schemas.reduce<z.ZodError[]>(+ (errors, schema) =>+ ((result) => (result.error ? [...errors, result.error] : errors))(+ schema.safeParse(x)+ ),+ []+ );+ if (schemas.length - errors.length !== 1) {+ ctx.addIssue({+ path: ctx.path,+ code: 'invalid_union',+ unionErrors: errors,+ message: 'Invalid input: Should pass single schema',+ });+ }+ }),+ })+ ),+});+export const ListInboxes = z.record(z.any());export const ListInboxesResponse = z.object({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.tsindex 8f6a24fec5..ee871b2870 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@@ -24,7 +24,15 @@ type ToolParserMap = {call: types.CreateDocument;response: types.CreateDocumentResponse;};+ CreateImportEntity: {+ call: types.CreateImportEntity;+ response: types.CreateImportEntityResponse;+ };CreateTag: { call: types.CreateTag; response: types.CreateTagResponse };+ DeleteImportEntity: {+ call: types.DeleteImportEntity;+ response: types.DeleteImportEntityResponse;+ };DeleteTag: { call: types.DeleteTag; response: types.DeleteTagResponse };DisplayResults: {call: types.DisplayResults;@@ -53,6 +61,10 @@ type ToolParserMap = {call: types.ListEntities;response: types.ListEntitiesResponse;};+ ListImportEntities: {+ call: types.ListImportEntities;+ response: types.ListImportEntitiesResponse;+ };ListInboxes: { call: types.ListInboxes; response: types.ListInboxesResponse };ListLabels: { call: types.ListLabels; response: types.ListLabelsResponse };ListNotifications: {@@ -145,7 +157,15 @@ const toolParserMap = {call: schemas.CreateDocument,response: schemas.CreateDocumentResponse,},+ CreateImportEntity: {+ call: schemas.CreateImportEntity,+ response: schemas.CreateImportEntityResponse,+ },CreateTag: { call: schemas.CreateTag, response: schemas.CreateTagResponse },+ DeleteImportEntity: {+ call: schemas.DeleteImportEntity,+ response: schemas.DeleteImportEntityResponse,+ },DeleteTag: { call: schemas.DeleteTag, response: schemas.DeleteTagResponse },DisplayResults: {call: schemas.DisplayResults,@@ -177,6 +197,10 @@ const toolParserMap = {call: schemas.ListEntities,response: schemas.ListEntitiesResponse,},+ ListImportEntities: {+ call: schemas.ListImportEntities,+ response: schemas.ListImportEntitiesResponse,+ },ListInboxes: {call: schemas.ListInboxes,response: schemas.ListInboxesResponse,@@ -292,7 +316,15 @@ type ToolDataMap = {call: types.CreateDocument;response: types.CreateDocumentResponse;};+ CreateImportEntity: {+ call: types.CreateImportEntity;+ response: types.CreateImportEntityResponse;+ };CreateTag: { call: types.CreateTag; response: types.CreateTagResponse };+ DeleteImportEntity: {+ call: types.DeleteImportEntity;+ response: types.DeleteImportEntityResponse;+ };DeleteTag: { call: types.DeleteTag; response: types.DeleteTagResponse };DisplayResults: {call: types.DisplayResults;@@ -321,6 +353,10 @@ type ToolDataMap = {call: types.ListEntities;response: types.ListEntitiesResponse;};+ ListImportEntities: {+ call: types.ListImportEntities;+ response: types.ListImportEntitiesResponse;+ };ListInboxes: { call: types.ListInboxes; response: types.ListInboxesResponse };ListLabels: { call: types.ListLabels; response: types.ListLabelsResponse };ListNotifications: {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.tsindex c5aa188cc2..b36822a07b 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@@ -138,6 +138,18 @@ export type ContentType =| 'chat-thread'| 'chat-message'| 'project';+/**+ * External systems items can be imported from.+ */+export type ImportSource = 'linear' | 'notion' | 'slack';+/**+ * Status values accepted by [`CreateImportEntity`].+ */+export type CreateImportStatus = 'staged' | 'imported';+/**+ * Lifecycle of one import entity.+ */+export type ImportStatus = 'staged' | 'importing' | 'imported' | 'discarded';/*** A tag color from the fixed palette.*/@@ -1243,6 +1255,74 @@ export interface CreateDocumentResponse {*/documentId: string;}+/**+ * Track an external item (Linear issue, Notion page, Slack channel) in the import ledger. Use status `staged` to propose an item for import BEFORE creating anything; use status `imported` (with entityId) only to record a Macro entity you already created from the item. The response tells you when the item was already imported by the user or a teammate — in that case do NOT create a duplicate; point the user at the existing entity instead.+ */+export interface CreateImportEntity {+ /**+ * The id of the Macro entity you created, required when status is `imported`. The entity type is fixed by source: linear → task, notion → md (document), slack → channel.+ */+ entityId?: string | null;+ /**+ * Stable id of the item in the source system: the Linear issue identifier (e.g. `ENG-142`), the Notion page URL or id, or the Slack channel id (e.g. `C0123456789`, falling back to the channel name).+ */+ foreignId: string;+ /**+ * Metadata describing the item. Linear: {identifier, title, description?, status?, priority?, assignee?, assignee_email?, url?}. Notion: {title, url?, summary?} — never include page content. Slack: {name, channel_id?, purpose?, participants?: [{name, email?}]}.+ */+ metadata: {+ [k: string]: unknown;+ };+ source: ImportSource;+ status: CreateImportStatus;+}+/**+ * Response from tracking an item.+ */+export interface CreateImportEntityResponse {+ entity: ImportEntityView;+ /**+ * What to do with this outcome.+ */+ message: string;+ /**+ * What happened: `staged`, `recorded_imported`, `already_imported`,+ * `already_imported_by_teammate`, `previously_declined`, or+ * `import_in_progress`.+ */+ outcome: string;+}+/**+ * Compact row view returned by import tools.+ */+export interface ImportEntityView {+ /**+ * The Macro entity it became, when imported.+ */+ entityId?: string | null;+ /**+ * The Macro entity type, when imported.+ */+ entityType?: string | null;+ /**+ * Stable id in the source system.+ */+ foreignId: string;+ /**+ * Ledger row id (use with DeleteImportEntity).+ */+ id: string;+ /**+ * Whether the row belongs to a teammate (team-imported), not the user.+ */+ importedByTeammate: boolean;+ /**+ * A human label from the metadata (title / channel name).+ */+ label: string;+ source: ImportSource;+ status: ImportStatus;+}/*** Create a new tag — a colored label the user can apply to documents, emails, tasks, AI chats, and projects — in the user's personal set or their team's shared set. The set is provisioned automatically the first time a tag is created. Tags are matched by label, so call ListTags first and avoid creating one whose label duplicates an existing tag in the same set. Returns the new tag's id and its set's propertyDefinitionId, which you can pass straight to SetEntityProperty (add_option_ids) to apply the tag to an item. Use this only to create a brand-new tag; to apply an existing tag to an item, use ListTags then SetEntityProperty instead.*/@@ -1343,6 +1423,28 @@ export interface CrmCompanySearchResponseItem {*/updatedAt: string;}+/**+ * Decline a staged import candidate on the user's behalf. The item is remembered as declined so it won't be proposed again; only the user's own staged items can be declined.+ */+export interface DeleteImportEntity {+ /**+ * The import entity id to decline (from CreateImportEntity or ListImportEntities).+ */+ id: string;+}+/**+ * Response from declining a candidate.+ */+export interface DeleteImportEntityResponse {+ /**+ * Whether the row is now discarded.+ */+ discarded: boolean;+ /**+ * What happened.+ */+ message: string;+}/*** Permanently delete a tag from the user's personal set or their team's shared set. This removes the tag from every item it is currently applied to, so it is destructive and cannot be undone — confirm with the user first. Both ids come from a ListTags result: `id` is the tag's option id, and `property_definition_id` is the propertyDefinitionId of the set that contains it. To simply remove a tag from a single item without deleting the tag itself, use SetEntityProperty with remove_option_ids instead.*/@@ -2062,6 +2164,28 @@ export interface ListEntitiesResponse {*/summary: string;}+/**+ * List the user's import ledger: staged candidates, in-flight imports, imported items (including ones teammates imported into the team), and declined items. Use this to check what already exists before proposing or creating imports.+ */+export interface ListImportEntities {+ /**+ * Filter to one source system.+ */+ source?: ImportSource | null;+ /**+ * Filter to one status: staged, importing, imported, or discarded. Omit for all.+ */+ status?: ImportStatus | null;+}+/**+ * Response from listing the ledger.+ */+export interface ListImportEntitiesResponse {+ /**+ * The visible rows, newest first.+ */+ entities: ImportEntityView[];+}/*** List the email inboxes the user can read or act on. Returns the caller's primary inbox, any other inboxes they have connected, and any inboxes delegated to them by teammates. Each entry has an `emailAddress`, `isPrimary` (the default inbox used when no inbox is specified), and `isDelegated` (true when the inbox belongs to another user).*diff --git a/apps/web/src/lib/service-clients/service-cognition/import.ts b/apps/web/src/lib/service-clients/service-cognition/import.tsnew file mode 100644index 0000000000..e2bf0abdb9--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/import.ts@@ -0,0 +1,144 @@+/**+ * Client for the import pipeline on the document cognition service.+ *+ * Types are hand-written mirrors of the `import` crate's API models (not+ * yet in the generated schemas; replace with generated types on the next+ * codegen run).+ */+import { SERVER_HOSTS } from '@core/constant/servers';+import {+ type FetchWithTokenErrorCode,+ fetchWithToken,+} from '@core/util/fetchWithToken';+import type { ObjectLike, ResultError } from '@core/util/result';+import type { SafeFetchInit } from '@core/util/safeFetch';+import type { Result } from 'neverthrow';++const dcsHost: string = SERVER_HOSTS['cognition-service'];++function importFetch(+ url: string,+ init?: SafeFetchInit+): Promise<Result<void, ResultError<FetchWithTokenErrorCode>[]>>;+function importFetch<T extends ObjectLike>(+ url: string,+ init?: SafeFetchInit+): Promise<Result<T, ResultError<FetchWithTokenErrorCode>[]>>;+function importFetch<T extends ObjectLike = never>(+ url: string,+ init?: SafeFetchInit+):+ | Promise<Result<T, ResultError<FetchWithTokenErrorCode>[]>>+ | Promise<Result<void, ResultError<FetchWithTokenErrorCode>[]>> {+ return fetchWithToken<T>(`${dcsHost}${url}`, init);+}++export type ImportSource = 'linear' | 'notion' | 'slack';++export type ImportEntityStatus =+ | 'staged'+ | 'importing'+ | 'imported'+ | 'discarded';++export type ImportInitiator = 'onboarding' | 'chat';++export type ImportRunStatus = 'running' | 'ready' | 'failed' | 'dismissed';++/** Metadata shape for staged Linear issues (`source === 'linear'`). */+export type LinearIssueMeta = {+ identifier?: string | null;+ title: string;+ description?: string | null;+ status?: string | null;+ priority?: string | null;+ assignee?: string | null;+ assignee_email?: string | null;+ url?: string | null;+};++/** Metadata shape for staged Notion pages (`source === 'notion'`). */+export type NotionDocMeta = {+ title: string;+ url?: string | null;+ summary?: string | null;+};++export type SlackParticipant = {+ name: string;+ email?: string | null;+};++/** Metadata shape for staged Slack channels (`source === 'slack'`). */+export type SlackChannelMeta = {+ name: string;+ channel_id?: string | null;+ purpose?: string | null;+ participants?: SlackParticipant[];+};++/** One row of the import ledger. */+export type ImportEntity = {+ id: string;+ /** Owner; teammates' team-imported rows carry the teammate's id. */+ user_id: string;+ team_id?: string | null;+ source: ImportSource;+ foreign_id: string;+ status: ImportEntityStatus;+ initiator: ImportInitiator;+ /** Per-source metadata; shape keyed by `source` (see the Meta types). */+ metadata: Record<string, unknown>;+ entity_id?: string | null;+ entity_type?: string | null;+ last_error?: string | null;+ created_at: string;+ updated_at: string;+};++/** Gather-run state for one source. */+export type ImportRun = {+ source: ImportSource;+ status: ImportRunStatus;+ error?: string | null;+ updated_at: string;+};++export type ImportState = {+ runs: ImportRun[];+ entities: ImportEntity[];+};++export type RunImportOutcome = {+ discarded: number;+ importing: number;+};++export const importClient = {+ /** Gather runs plus visible ledger rows for the authenticated user. */+ async getState() {+ return await importFetch<ImportState>('/import/state', { method: 'GET' });+ },++ /** Accept staged rows (starts import jobs) and/or discard staged rows. */+ async runImport(args: { import_ids: string[]; discard_ids: string[] }) {+ return await importFetch<RunImportOutcome>('/import/run', {+ method: 'POST',+ body: JSON.stringify(args),+ });+ },++ /** Restart a failed gather run for one source. */+ async retryGather(source: ImportSource) {+ return await importFetch(`/import/runs/${source}/retry`, {+ method: 'POST',+ });+ },++ /** Dismiss one source's import section. */+ async dismissRun(source: ImportSource) {+ return await importFetch(`/import/runs/${source}/dismiss`, {+ method: 'POST',+ });+ },+};diff --git a/apps/web/src/lib/service-clients/service-cognition/onboarding.ts b/apps/web/src/lib/service-clients/service-cognition/onboarding.tsnew file mode 100644index 0000000000..064c755924--- /dev/null+++ b/apps/web/src/lib/service-clients/service-cognition/onboarding.ts@@ -0,0 +1,79 @@+/**+ * Client for the onboarding flow on the document cognition service.+ *+ * Types are hand-written mirrors of the `onboarding` crate's API models+ * (not yet in the generated schemas; replace with generated types on the+ * next codegen run). All the import machinery lives in `./import` — the+ * onboarding surface is only the flow row plus the user's connections.+ */+import { SERVER_HOSTS } from '@core/constant/servers';+import {+ type FetchWithTokenErrorCode,+ fetchWithToken,+} from '@core/util/fetchWithToken';+import type { ObjectLike, ResultError } from '@core/util/result';+import type { SafeFetchInit } from '@core/util/safeFetch';+import type { Result } from 'neverthrow';++const dcsHost: string = SERVER_HOSTS['cognition-service'];++function onboardingFetch(+ url: string,+ init?: SafeFetchInit+): Promise<Result<void, ResultError<FetchWithTokenErrorCode>[]>>;+function onboardingFetch<T extends ObjectLike>(+ url: string,+ init?: SafeFetchInit+): Promise<Result<T, ResultError<FetchWithTokenErrorCode>[]>>;+function onboardingFetch<T extends ObjectLike = never>(+ url: string,+ init?: SafeFetchInit+):+ | Promise<Result<T, ResultError<FetchWithTokenErrorCode>[]>>+ | Promise<Result<void, ResultError<FetchWithTokenErrorCode>[]>> {+ return fetchWithToken<T>(`${dcsHost}${url}`, init);+}++export type OnboardingStatus = 'active' | 'completed';++export type OnboardingRow = {+ status: OnboardingStatus;+ skipped: boolean;+ started_at: string;+ completed_at?: string | null;+};++export type ConnectedServer = {+ name: string;+ url: string;+ authenticated: boolean;+};++export type OnboardingState = {+ row: OnboardingRow;+ connected_servers: ConnectedServer[];+};++export const onboardingClient = {+ /**+ * Current onboarding aggregate for the authenticated user. While the flow+ * is active the server starts any due import gather runs on this read, so+ * polling it is what keeps onboarding moving.+ */+ async getState() {+ return await onboardingFetch<OnboardingState>('/onboarding', {+ method: 'GET',+ });+ },++ /**+ * Finish (or skip) onboarding. Leftover onboarding-staged import+ * candidates are discarded server-side.+ */+ async complete(args: { skipped: boolean }) {+ return await onboardingFetch<OnboardingRow>('/onboarding/complete', {+ method: 'POST',+ body: JSON.stringify(args),+ });+ },+};diff --git a/apps/web/src/lib/service-clients/service-cognition/openapi.json b/apps/web/src/lib/service-clients/service-cognition/openapi.jsonindex 32e6ebbcf3..13e6b1a9d1 100644--- a/apps/web/src/lib/service-clients/service-cognition/openapi.json+++ b/apps/web/src/lib/service-clients/service-cognition/openapi.json@@ -1045,6 +1045,118 @@}}},+ "/import/run": {+ "post": {+ "tags": ["import"],+ "summary": "Accept staged imports (starts import jobs) and/or discard staged rows.",+ "operationId": "run_import_handler",+ "requestBody": {+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/RunImportRequest"+ }+ }+ },+ "required": true+ },+ "responses": {+ "200": {+ "description": "Import run outcome",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/RunImportOutcome"+ }+ }+ }+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },+ "/import/runs/{source}/dismiss": {+ "post": {+ "tags": ["import"],+ "summary": "Dismiss one source's import section.",+ "operationId": "dismiss_run_handler",+ "parameters": [+ {+ "name": "source",+ "in": "path",+ "description": "Import source",+ "required": true,+ "schema": {+ "type": "string"+ }+ }+ ],+ "responses": {+ "204": {+ "description": "Dismissed"+ },+ "400": {+ "description": "Unknown import source"+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },+ "/import/runs/{source}/retry": {+ "post": {+ "tags": ["import"],+ "summary": "Restart a failed (or dismissed) gather run for one source.",+ "operationId": "retry_gather_handler",+ "parameters": [+ {+ "name": "source",+ "in": "path",+ "description": "Import source",+ "required": true,+ "schema": {+ "type": "string"+ }+ }+ ],+ "responses": {+ "204": {+ "description": "Retry accepted (idempotent)"+ },+ "400": {+ "description": "Unknown import source"+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },+ "/import/state": {+ "get": {+ "tags": ["import"],+ "summary": "Get the authenticated user's import state: gather runs plus visible\nledger rows.",+ "operationId": "get_state_handler",+ "responses": {+ "200": {+ "description": "Current import state",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ImportState"+ }+ }+ }+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },"/mcp/servers": {"get": {"tags": ["mcp"],@@ -1356,6 +1468,60 @@}}},+ "/onboarding": {+ "get": {+ "tags": ["onboarding"],+ "summary": "Get the authenticated user's onboarding state. While the flow is active\nthis also starts any gather runs that are due, so polling this endpoint\nis what keeps onboarding moving.",+ "operationId": "get_state_handler",+ "responses": {+ "200": {+ "description": "Current onboarding state",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/OnboardingState"+ }+ }+ }+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },+ "/onboarding/complete": {+ "post": {+ "tags": ["onboarding"],+ "summary": "Complete (or skip) onboarding. Leftover onboarding-staged import\ncandidates are discarded.",+ "operationId": "complete_handler",+ "requestBody": {+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/CompleteOnboardingRequest"+ }+ }+ },+ "required": true+ },+ "responses": {+ "200": {+ "description": "Completed onboarding row",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/OnboardingRow"+ }+ }+ }+ },+ "500": {+ "description": "Internal server error"+ }+ }+ }+ },"/preview": {"post": {"tags": ["get_batch_preview"],@@ -1582,7 +1748,8 @@"call_summary","channel_bot","ai_projection",- "ai_editing"+ "ai_editing",+ "import"]},"AssistantMessagePart": {@@ -2244,6 +2411,16 @@}]},+ "CompleteOnboardingRequest": {+ "type": "object",+ "description": "Body for completing onboarding.",+ "properties": {+ "skipped": {+ "type": "boolean",+ "description": "Whether the user skipped rather than finished."+ }+ }+ },"CompletionUsage": {"type": "object","description": "A recorded completion: who, what feature, optional entity, and the cost.",@@ -2268,6 +2445,25 @@}}},+ "ConnectedServer": {+ "type": "object",+ "description": "One of the user's MCP server connections, as the setup page sees it.",+ "required": ["name", "url", "authenticated"],+ "properties": {+ "authenticated": {+ "type": "boolean",+ "description": "Whether the user has completed OAuth against it."+ },+ "name": {+ "type": "string",+ "description": "The server's display name."+ },+ "url": {+ "type": "string",+ "description": "The server URL."+ }+ }+ },"ConversationRecord": {"type": "object","required": ["chat_id", "title", "messages"],@@ -2990,6 +3186,138 @@}}},+ "ImportEntity": {+ "type": "object",+ "description": "One row of the import ledger.",+ "required": [+ "id",+ "user_id",+ "source",+ "foreign_id",+ "status",+ "initiator",+ "metadata",+ "created_at",+ "updated_at"+ ],+ "properties": {+ "created_at": {+ "type": "string",+ "format": "date-time",+ "description": "When the row was first staged."+ },+ "entity_id": {+ "type": ["string", "null"],+ "description": "The Macro entity it became, once imported."+ },+ "entity_type": {+ "type": ["string", "null"],+ "description": "The Macro entity type (`task` | `md` | `channel`), once imported."+ },+ "foreign_id": {+ "type": "string",+ "description": "Stable id in the source system (see\n[`ImportSource::normalize_foreign_id`])."+ },+ "id": {+ "type": "string",+ "format": "uuid",+ "description": "Ledger row id."+ },+ "initiator": {+ "$ref": "#/components/schemas/Initiator",+ "description": "Where it was first staged from."+ },+ "last_error": {+ "type": ["string", "null"],+ "description": "Failure detail from the last import attempt, when one failed."+ },+ "metadata": {+ "description": "Per-source metadata (shape depends on `source`; see\n[`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`])."+ },+ "source": {+ "$ref": "#/components/schemas/ImportSource",+ "description": "The external system the item comes from."+ },+ "status": {+ "$ref": "#/components/schemas/ImportStatus",+ "description": "Where the row is in its lifecycle."+ },+ "team_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The team the created entity was shared with, when it was."+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "description": "When the row last changed."+ },+ "user_id": {+ "type": "string",+ "description": "The user who staged/imported it. Team-imported rows from teammates\nare visible with their owner's id, so clients can say \"created by a\nteammate\"."+ }+ }+ },+ "ImportRun": {+ "type": "object",+ "description": "Gather-run state for one user × source.",+ "required": ["source", "status", "updated_at"],+ "properties": {+ "error": {+ "type": ["string", "null"],+ "description": "Failure detail, when the run failed."+ },+ "source": {+ "$ref": "#/components/schemas/ImportSource",+ "description": "The source this run gathered from."+ },+ "status": {+ "$ref": "#/components/schemas/RunStatus",+ "description": "Where the run is in its lifecycle."+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "description": "When the run state last changed."+ }+ }+ },+ "ImportSource": {+ "type": "string",+ "description": "External systems items can be imported from.",+ "enum": ["linear", "notion", "slack"]+ },+ "ImportState": {+ "type": "object",+ "description": "The full import aggregate for a user: gather runs plus visible ledger\nrows (their own, and teammates' team-imported rows).",+ "required": ["runs", "entities"],+ "properties": {+ "entities": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/ImportEntity"+ },+ "description": "Visible import entities, newest first."+ },+ "runs": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/ImportRun"+ },+ "description": "Gather runs, one per source that ever gathered."+ }+ }+ },+ "ImportStatus": {+ "type": "string",+ "description": "Lifecycle of one import entity.",+ "enum": ["staged", "importing", "imported", "discarded"]+ },+ "Initiator": {+ "type": "string",+ "description": "Where an import entity was first staged from. Provenance only — never a\nvisibility filter.",+ "enum": ["onboarding", "chat"]+ },"JwtPayload": {"type": "object","required": ["token"],@@ -2999,6 +3327,49 @@}}},+ "LinearIssueMeta": {+ "type": "object",+ "description": "Metadata for one staged Linear issue.",+ "required": ["title"],+ "properties": {+ "assignee": {+ "type": ["string", "null"],+ "description": "Assignee display name as Linear reports it."+ },+ "assignee_email": {+ "type": ["string", "null"],+ "description": "Assignee email, when Linear exposes it."+ },+ "description": {+ "type": ["string", "null"],+ "description": "Short markdown description."+ },+ "due_date": {+ "type": ["string", "null"],+ "description": "Due date as an ISO date (`YYYY-MM-DD`), when set."+ },+ "identifier": {+ "type": ["string", "null"],+ "description": "Linear's human identifier (e.g. `ENG-142`)."+ },+ "priority": {+ "type": ["string", "null"],+ "description": "Priority label (e.g. `Urgent`)."+ },+ "status": {+ "type": ["string", "null"],+ "description": "Workflow status name (e.g. `In Progress`)."+ },+ "title": {+ "type": "string",+ "description": "Issue title."+ },+ "url": {+ "type": ["string", "null"],+ "description": "Deep link back to the issue in Linear."+ }+ }+ },"MacroUserIdStr": {"type": "string"},@@ -3106,6 +3477,73 @@}}},+ "NotionDocMeta": {+ "type": "object",+ "description": "Metadata for one staged Notion page. Deliberately has NO content field:\npage bodies are fetched at import time, for accepted pages only.",+ "required": ["title"],+ "properties": {+ "summary": {+ "type": ["string", "null"],+ "description": "One-line summary of what the page contains."+ },+ "title": {+ "type": "string",+ "description": "Page title."+ },+ "url": {+ "type": ["string", "null"],+ "description": "Deep link back to the page in Notion."+ }+ }+ },+ "OnboardingRow": {+ "type": "object",+ "description": "One user's onboarding row.",+ "required": ["status", "skipped", "started_at"],+ "properties": {+ "completed_at": {+ "type": ["string", "null"],+ "format": "date-time",+ "description": "When the flow completed, if it has."+ },+ "skipped": {+ "type": "boolean",+ "description": "Whether the user skipped rather than finished."+ },+ "started_at": {+ "type": "string",+ "format": "date-time",+ "description": "When the flow was first touched."+ },+ "status": {+ "$ref": "#/components/schemas/OnboardingStatus",+ "description": "Where the flow is in its lifecycle."+ }+ }+ },+ "OnboardingState": {+ "type": "object",+ "description": "The onboarding aggregate served to the setup page. Import runs and\nstaged entities are served separately by `GET /import/state`.",+ "required": ["row", "connected_servers"],+ "properties": {+ "connected_servers": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/ConnectedServer"+ },+ "description": "The user's MCP server connections."+ },+ "row": {+ "$ref": "#/components/schemas/OnboardingRow",+ "description": "The user's onboarding row."+ }+ }+ },+ "OnboardingStatus": {+ "type": "string",+ "description": "Lifecycle of the onboarding flow itself.",+ "enum": ["active", "completed"]+ },"PatchChatRequest": {"type": "object","description": "Request body for patching a chat.",@@ -3216,6 +3654,52 @@"description": "The role of a message participant.","enum": ["user", "assistant", "system"]},+ "RunImportOutcome": {+ "type": "object",+ "description": "Outcome of accepting/declining staged rows via `POST /import/run`.",+ "required": ["discarded", "importing"],+ "properties": {+ "discarded": {+ "type": "integer",+ "format": "int64",+ "description": "How many rows were discarded.",+ "minimum": 0+ },+ "importing": {+ "type": "integer",+ "format": "int64",+ "description": "How many rows flipped to `importing` (jobs are now copying them in).",+ "minimum": 0+ }+ }+ },+ "RunImportRequest": {+ "type": "object",+ "description": "Body for accepting/declining staged imports.",+ "properties": {+ "discard_ids": {+ "type": "array",+ "items": {+ "type": "string",+ "format": "uuid"+ },+ "description": "Staged rows to discard."+ },+ "import_ids": {+ "type": "array",+ "items": {+ "type": "string",+ "format": "uuid"+ },+ "description": "Staged rows to import."+ }+ }+ },+ "RunStatus": {+ "type": "string",+ "description": "Lifecycle of a gather run (one per user × source).",+ "enum": ["running", "ready", "failed", "dismissed"]+ },"SendChatMessagePayload": {"allOf": [{@@ -3359,6 +3843,47 @@}}},+ "SlackChannelMeta": {+ "type": "object",+ "description": "Metadata for one staged Slack channel.",+ "required": ["name"],+ "properties": {+ "channel_id": {+ "type": ["string", "null"],+ "description": "Slack's channel id (e.g. `C0123456789`), stable across renames."+ },+ "name": {+ "type": "string",+ "description": "Channel name without the leading `#`."+ },+ "participants": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/SlackParticipant"+ },+ "description": "The channel's most relevant members, when discoverable."+ },+ "purpose": {+ "type": ["string", "null"],+ "description": "The channel's purpose/topic, when set."+ }+ }+ },+ "SlackParticipant": {+ "type": "object",+ "description": "A person active in a staged Slack channel.",+ "required": ["name"],+ "properties": {+ "email": {+ "type": ["string", "null"],+ "description": "Email, when the workspace exposes it."+ },+ "name": {+ "type": "string",+ "description": "Display name or Slack handle."+ }+ }+ },"StartAuthRequest": {"type": "object","description": "Request body for starting an OAuth authorization flow.",diff --git a/apps/web/src/routes/Root.tsx b/apps/web/src/routes/Root.tsxindex 9024b1e971..5da5b1aa26 100644--- a/apps/web/src/routes/Root.tsx+++ b/apps/web/src/routes/Root.tsx@@ -13,6 +13,7 @@ import { usePendingNotificationNavigationEffect } from '@app/features/notificatiimport { InteractiveOnboardingModal } from '@app/features/onboarding/InteractiveOnboardingModal';import MobileWebSignup from '@app/features/onboarding/MobileWebSignup';import { useCheckoutCompletionListener } from '@app/features/paywall/use-checkout-completion-listener';+import { SetupPage } from '@app/features/setup/SetupPage';import { TeamInviteAcceptance } from '@app/features/team-invitations/TeamInviteAcceptance';import {AnalyticsContextProvider,@@ -34,6 +35,7 @@ import { LAYOUT_ROUTE } from '@components/app/split-layout/SplitLayoutRoute';import { clearLocalAuthSession } from '@core/auth/logout';import { ChatAttachmentsInit } from '@core/component/AI/signal/globalAttachments';import { ToastRegion } from '@core/component/Toast/ToastRegion';+import { ENABLE_NEW_ONBOARDING_V3 } from '@core/constant/featureFlags';import { ChannelsContextProvider } from '@core/context/channels';import { EmailLinksContextProvider } from '@core/context/emailLinks';import { QuickAccessProvider } from '@core/context/quickAccess';@@ -46,6 +48,7 @@ import {import { initAndStartEmailSync } from '@core/email-link';import { useHotKeyRoot } from '@core/hotkey/hotkeys';import { IosPushNotificationModal } from '@core/mobile/IosPushNotificationModal';+import { isMobile } from '@core/mobile/isMobile';import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';import { createBlockOrchestrator } from '@core/orchestrator';import { formatTabTitle, tabTitleSignal } from '@core/signal/tabTitle';@@ -426,6 +429,20 @@ const ROUTES: RouteDefinition[] = [<Navigate href="/login" />),},+ {+ path: '/setup',+ // Flag-gated at the route, not just the redirect: with the flag off a+ // direct /setup visit must not touch the onboarding backend (reading+ // it creates the flow's row and starts gathers).+ component: () =>+ !ENABLE_NEW_ONBOARDING_V3 ? (+ <Navigate href="/" />+ ) : isNativeMobilePlatform() ? (+ <Navigate href="/onboarding" />+ ) : (+ <SetupPage />+ ),+ },{path: '/team-invite',component: TeamInviteAcceptance,@@ -559,6 +576,9 @@ function InitialInteractiveOnboardingModal() {const modalOpen = () =>open() &&+ // The new split-screen onboarding replaces this modal on desktop; the+ // Layout redirect sends first-time users to /setup instead.+ (!ENABLE_NEW_ONBOARDING_V3 || isMobile()) &&!isNativeMobilePlatform() &&userInfoQuery.data?.authenticated === true &&(userInfoQuery.data.tutorialComplete === false || onboardingStarted());diff --git a/crates/ai_tools/Cargo.toml b/crates/ai_tools/Cargo.tomlindex 2897fb8c68..d1c66e30bf 100644--- a/crates/ai_tools/Cargo.toml+++ b/crates/ai_tools/Cargo.toml@@ -35,6 +35,7 @@ entity_access_management = { path = "../entity_access_management" }foreign_entity = { path = "../foreign_entity" }frecency = { path = "../frecency", features = ["outbound", "postgres"] }futures.workspace = true+import = { path = "../import" }item_filters = { path = "../item_filters" }lazy_static.workspace = truelexical_client = { path = "../lexical_client" }@@ -45,6 +46,7 @@ macro_env_var = { path = "../macro_env_var" }macro_event_broker = { path = "../macro_event_broker" }macro_queues = { path = "../macro_queues" }macro_user_id = { path = "../macro_user_id" }+mcp_client = { path = "../mcp_client" }model = { path = "../model" }model-entity = { path = "../model-entity" }model_notifications = { path = "../model_notifications" }diff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rsindex b7bd9c8daf..f45fe82285 100644--- a/crates/ai_tools/src/build_context.rs+++ b/crates/ai_tools/src/build_context.rs@@ -6,7 +6,7 @@use crate::tool_context::{NoOpCallRtcClient, NoOpConnectionService, NoOpNotificationIngress, NoOpSnsEndpointManager,- ToolNotificationQueue, ToolServiceContext,+ ToolImportToolContext, ToolNotificationQueue, ToolServiceContext,};use anthropic::toolset::AnthropicToolContext;use anyhow::Context;@@ -366,6 +366,7 @@ pub async fn build_tool_service_context_from_env(email_tool_context,call_tool_context,notification_tool_context,+ import_tool_context: ToolImportToolContext::unwired(),chat_tool_context,channel_tool_context,team_tool_context: crate::tool_context::build_team_tool_context(pool.clone()),diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rsindex 9dd28f822f..9ab4d8b670 100644--- a/crates/ai_tools/src/lib.rs+++ b/crates/ai_tools/src/lib.rs@@ -25,6 +25,7 @@ use crm::inbound::toolset::crm_toolset;use display_results::DisplayResults;use documents::inbound::toolset::document_toolset;use email::inbound::toolset::{email_toolset, mcp_toolset as email_mcp_toolset};+use import::inbound::toolset::import_toolset;use notification::inbound::ai_tool::notification_toolset;use properties::inbound::toolset::properties_toolset;use schemas::read;@@ -48,13 +49,14 @@ pub use tool_context::{ToolChannelEventDispatcher, ToolChannelMessagesService, ToolChannelToolContext,ToolChatService, ToolChatToolContext, ToolCommsService, ToolCrmService, ToolCrmToolContext,ToolDocumentService, ToolDocumentToolContext, ToolEmailService, ToolEmailToolContext,- ToolEntityAccessManagementService, ToolEntityAccessService, ToolForeignEntityService,- ToolFrecencyService, ToolNotificationQueue, ToolNotificationService,- ToolNotificationToolContext, ToolPropertiesService, ToolPropertiesToolContext,- ToolServiceContext, ToolSoupService, ToolSystemPropertiesService, ToolTeamService,- ToolTeamToolContext, ToolUserEmailService, build_channel_tool_context,- build_channel_tool_context_with_dispatcher, build_crm_tool_context, build_properties_service,- build_properties_tool_context, build_task_properties_adapter, build_team_tool_context,+ ToolEntityAccessManagementService, ToolEntityAccessService, ToolEntityCreator,+ ToolForeignEntityService, ToolFrecencyService, ToolImportService, ToolImportToolContext,+ ToolNotificationQueue, ToolNotificationService, ToolNotificationToolContext,+ ToolPropertiesService, ToolPropertiesToolContext, ToolServiceContext, ToolSoupService,+ ToolSystemPropertiesService, ToolTeamService, ToolTeamToolContext, ToolUserEmailService,+ build_channel_tool_context, build_channel_tool_context_with_dispatcher, build_crm_tool_context,+ build_properties_service, build_properties_tool_context, build_task_properties_adapter,+ build_team_repository, build_team_tool_context,};pub type AiToolSet = AsyncToolCollection<ToolServiceContext>;@@ -94,6 +96,7 @@ pub fn all_tools() -> ToolSetWithPrompt {let toolset = subagent_toolset().add_subtoolset::<ToolNotificationToolContext>(notification_toolset()).add_subtoolset::<ToolEmailToolContext>(email_toolset())+ .add_subtoolset::<ToolImportToolContext>(import_toolset()).add_tool::<Subagent, ToolServiceContext>().add_tool::<SearchTools, ToolServiceContext>().add_tool::<LoadTools, ToolServiceContext>()@@ -121,6 +124,7 @@ pub fn mcp_tools() -> ToolSetWithPrompt {let toolset = subagent_toolset().add_subtoolset::<ToolNotificationToolContext>(notification_toolset()).add_subtoolset::<ToolEmailToolContext>(email_mcp_toolset())+ .add_subtoolset::<ToolImportToolContext>(import_toolset()).add_tool::<Subagent, ToolServiceContext>();let toolset = Arc::new(toolset);ToolSetWithPrompt {diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rsindex 120f8080bb..e5a0f8bc6f 100644--- a/crates/ai_tools/src/tool_context.rs+++ b/crates/ai_tools/src/tool_context.rs@@ -33,7 +33,7 @@ use properties::inbound::toolset::PropertiesToolContext;use soup::{domain::service::SoupImpl, inbound::toolset::SoupToolContext};use std::sync::Arc;use system_properties::{- PgSystemPropertiesRepository, StatusOption, SystemPropertiesService as _,+ PgSystemPropertiesRepository, PriorityOption, StatusOption, SystemPropertiesService as _,SystemPropertiesServiceImpl,};use teams::{inbound::toolset::TeamToolContext, outbound::team_repo::TeamRepositoryImpl};@@ -172,6 +172,12 @@ pub type ToolTeamService = TeamRepositoryImpl;/// Type alias for the team AI tool context.pub type ToolTeamToolContext = TeamToolContext<ToolTeamService, ToolEntityAccessService>;+/// Build the team repository used for roster lookups (e.g. resolving+/// imported items' emails to teammates).+pub fn build_team_repository(pool: sqlx::PgPool) -> Arc<ToolTeamService> {+ Arc::new(TeamRepositoryImpl::new(pool))+}+/// Build the team AI tool context from a Postgres pool.pub fn build_team_tool_context(pool: sqlx::PgPool) -> ToolTeamToolContext {TeamToolContext::new(@@ -655,6 +661,281 @@ pub type ToolChatService = ChatServiceImpl<PgChatRepo, (), ToolEntityAccessManag/// Type alias for the chat tool contextpub type ToolChatToolContext = ChatToolContext<ToolChatService, ToolEntityAccessService>;+/// Creates the Macro entities the import pipeline's fixed mapping calls for+/// (linear → task, notion → md, slack → channel), reusing the same document+/// creator and channel service the other AI tools run on. Task system+/// properties (status, priority, due date, assignee) and channel teammate+/// invites are applied best-effort — an enrichment that fails never fails+/// the import itself.+#[derive(Clone)]+pub struct ToolEntityCreator {+ /// Backend-owned document creation use case (shared with document tools).+ pub document_creator:+ documents::inbound::toolset::DefaultDocumentToolCreator<ToolDocumentService>,+ /// Team lookup so imported tasks get the user's team task numbering.+ pub entity_access_service: Arc<ToolEntityAccessService>,+ /// Channel creation service.+ pub channel_service: Arc<ToolChannelMessagesService>,+ /// Task system-property writes (status / priority / due date / assignees).+ pub task_properties: TaskPropertiesAdapter,+ /// Team roster lookups, to resolve source-tool emails to teammates.+ pub team_repository: Arc<ToolTeamService>,+}++impl ToolEntityCreator {+ async fn create_doc(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ markdown: &str,+ is_task: bool,+ team_id: Option<uuid::Uuid>,+ ) -> anyhow::Result<String> {+ use std::str::FromStr as _;+ let document = documents::domain::create::NewPlainTextDocument::builder(+ documents::domain::create::NewDocumentMetadata::new(name.to_string()),+ )+ .file_type(model::document::FileType::from_str("md").expect("md is a valid file type"))+ .text(markdown.to_string())+ .task_flag(is_task, team_id)+ .build()?;+ let created = self+ .document_creator+ .create_plain_text(user.clone(), document)+ .await?;+ Ok(created+ .into_response()+ .document_response+ .document_metadata+ .metadata+ .document_id+ .to_string())+ }++ /// The user's team id (when they have one) and its member ids.+ async fn team_roster(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> (Option<uuid::Uuid>, Vec<MacroUserIdStr<'static>>) {+ use teams::domain::team_repo::TeamRepository as _;+ let team_id = match self.entity_access_service.get_user_team(user).await {+ Ok(team) => team.map(|team| team.team_id),+ Err(e) => {+ tracing::warn!(%user, error = ?e, "failed to get user's team");+ None+ }+ };+ let Some(team_id) = team_id else {+ return (None, Vec::new());+ };+ match self.team_repository.get_team_members(&team_id).await {+ Ok(members) => (+ Some(team_id),+ members+ .into_iter()+ .map(|member| macro_user_id::cowlike::CowLike::into_owned(member.user_id))+ .collect(),+ ),+ Err(e) => {+ tracing::warn!(%user, %team_id, error = ?e, "failed to list team members");+ (Some(team_id), Vec::new())+ }+ }+ }++ /// Apply imported task properties best-effort: each one logs and is+ /// skipped on failure so a bad label or unknown assignee never sinks+ /// the import.+ async fn apply_task_properties(+ &self,+ user: &MacroUserIdStr<'static>,+ task_id: &str,+ properties: &import::domain::ports::ImportedTaskProperties,+ roster: &[MacroUserIdStr<'static>],+ ) {+ use models_properties::api::requests::SetPropertyValue;+ use system_properties::SystemPropertyKey;++ if let Some(status) = properties.status.as_deref()+ && let Err(e) = self+ .task_properties+ .update_task_status(task_id, status)+ .await+ {+ tracing::warn!(task_id, status, error = ?e, "failed to set imported task status");+ }++ if let Some(priority) = properties.priority.as_deref() {+ let option = match priority {+ "Low" => Some(PriorityOption::Low),+ "Medium" => Some(PriorityOption::Medium),+ "High" => Some(PriorityOption::High),+ "Urgent" => Some(PriorityOption::Urgent),+ _ => None,+ };+ match option {+ Some(option) => {+ if let Err(e) = self+ .task_properties+ .set_entity_property(+ user.as_ref(),+ task_id,+ SystemPropertyKey::Priority.uuid(),+ Some(SetPropertyValue::SelectOption {+ option_id: option.uuid(),+ }),+ )+ .await+ {+ tracing::warn!(task_id, priority, error = ?e, "failed to set imported task priority");+ }+ }+ None => tracing::warn!(task_id, priority, "unknown imported task priority label"),+ }+ }++ if let Some(due_date) = properties.due_date.as_deref() {+ match chrono::NaiveDate::parse_from_str(due_date, "%Y-%m-%d") {+ Ok(date) => {+ let value = date.and_time(chrono::NaiveTime::MIN).and_utc();+ if let Err(e) = self+ .task_properties+ .set_entity_property(+ user.as_ref(),+ task_id,+ SystemPropertyKey::DueDate.uuid(),+ Some(SetPropertyValue::Date { value }),+ )+ .await+ {+ tracing::warn!(task_id, due_date, error = ?e, "failed to set imported task due date");+ }+ }+ Err(e) => {+ tracing::warn!(task_id, due_date, error = ?e, "unparseable imported task due date");+ }+ }+ }++ if let Some(email) = properties.assignee_email.as_deref() {+ // Only assign teammates — an email with no roster match (an+ // external collaborator, a bot) is silently skipped.+ let Some(assignee) = roster+ .iter()+ .find(|member| member.email_str().eq_ignore_ascii_case(email))+ else {+ return;+ };+ let reference = models_properties::EntityReference::new(+ assignee.as_ref().to_string(),+ models_properties::EntityType::User,+ );+ if let Err(e) = self+ .task_properties+ .set_entity_property(+ user.as_ref(),+ task_id,+ SystemPropertyKey::Assignees.uuid(),+ Some(SetPropertyValue::MultiEntityReference {+ references: vec![reference],+ }),+ )+ .await+ {+ tracing::warn!(task_id, email, error = ?e, "failed to set imported task assignee");+ }+ }+ }+}++impl import::domain::ports::EntityCreator for ToolEntityCreator {+ async fn create_task(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ markdown: &str,+ properties: &import::domain::ports::ImportedTaskProperties,+ ) -> anyhow::Result<String> {+ // Same team resolution as the CreateDocument tool, so imported tasks+ // number correctly within the user's team; the roster doubles as the+ // assignee-email lookup.+ let (team_id, roster) = self.team_roster(user).await;+ let task_id = self.create_doc(user, name, markdown, true, team_id).await?;+ self.apply_task_properties(user, &task_id, properties, &roster)+ .await;+ Ok(task_id)+ }++ async fn create_markdown_doc(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ markdown: &str,+ ) -> anyhow::Result<String> {+ self.create_doc(user, name, markdown, false, None).await+ }++ async fn create_channel(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ team_id: Option<uuid::Uuid>,+ participant_emails: &[String],+ ) -> anyhow::Result<String> {+ use channels::domain::ports::ChannelService as _;+ // Teammates who were in the source channel join the Macro one; emails+ // with no roster match (external collaborators, bots) are skipped.+ let (_, roster) = self.team_roster(user).await;+ let mut participants: std::collections::HashSet<MacroUserIdStr<'static>> =+ std::iter::once(user.clone()).collect();+ for email in participant_emails {+ if let Some(member) = roster+ .iter()+ .find(|member| member.email_str().eq_ignore_ascii_case(email))+ {+ participants.insert(member.clone());+ }+ }+ let request = channels::domain::models::CreateChannelRequest {+ name: Some(name.to_string()),+ channel_type: if team_id.is_some() {+ channels::domain::models::ChannelType::Team+ } else {+ channels::domain::models::ChannelType::Public+ },+ team_id,+ // The creator is always included (the service requires a+ // non-empty participant list and the repo filters out the+ // owner), plus any teammates matched by email above. Explicit+ // membership mirrors the source channel — never the whole team.+ auto_join_team: false,+ participants,+ };+ let response = self+ .channel_service+ .create_channel(+ channels::domain::models::Sender::new_from_user(user.clone()),+ None,+ request,+ )+ .await+ .map_err(|e| anyhow::anyhow!("failed to create channel: {e:?}"))?;+ Ok(response.id)+ }+}++/// Type alias for the import service implementation used by AI tools.+pub type ToolImportService = import::domain::service::ImportServiceImpl<+ import::outbound::pg_import_repo::PgImportRepo,+ mcp_client::outbound::pg_server_repo::PgServerRepo,+ ToolEntityCreator,+>;++/// Type alias for the import tool context. Built `unwired` by the shared+/// context builder; hosts that can run the import pipeline (DCS) replace it+/// with a wired one after constructing the import service.+pub type ToolImportToolContext = import::inbound::toolset::ImportToolContext<ToolImportService>;+#[derive(Clone, Default)]pub struct NoOpScheduleContext;@@ -676,6 +957,9 @@ pub struct ToolServiceContext {pub email_tool_context: ToolEmailToolContext,pub call_tool_context: ToolCallToolContext,pub notification_tool_context: ToolNotificationToolContext,+ /// Import staging/tracking tools. `unwired` in hosts that can't build+ /// the import service — calls there fail with a clear error.+ pub import_tool_context: ToolImportToolContext,/// Built per-request via a manual `FromRef` below so it can carry the/// running chat's id — the derive's field-clone would freeze it at/// startup with no chat id set.diff --git a/crates/ai_usage/src/domain/ports.rs b/crates/ai_usage/src/domain/ports.rsindex d0ad35ec5c..e4607e598b 100644--- a/crates/ai_usage/src/domain/ports.rs+++ b/crates/ai_usage/src/domain/ports.rs@@ -58,6 +58,8 @@ pub enum AiFeature {AiProjection,/// In-document AI editing (the ai-editing-worker).AiEditing,+ /// Import pipeline agent sessions (connector gathers + Notion imports).+ Import,}/// Resolved price for one completion.diff --git a/crates/import/Cargo.toml b/crates/import/Cargo.tomlnew file mode 100644index 0000000000..16856270ea--- /dev/null+++ b/crates/import/Cargo.toml@@ -0,0 +1,33 @@+[package]+edition = "2024"+name = "import"+publish = false+version = "0.1.0"++[dependencies]+agent = { path = "../agent" }+ai_toolset = { path = "../ai_toolset" }+ai_usage = { path = "../ai_usage" }+anyhow = { workspace = true }+async-trait = { workspace = true }+axum = { workspace = true }+chrono = { workspace = true }+connection_gateway_client = { path = "../connection_gateway_client" }+futures = { workspace = true }+macro_authorization = { path = "../macro_authorization", default-features = false, features = [+ "axum",+] }+macro_user_id = { path = "../macro_user_id" }+mcp_client = { path = "../mcp_client" }+model-entity = { path = "../model-entity" }+schemars = { workspace = true }+serde = { workspace = true }+serde_json = { workspace = true }+sqlx = { workspace = true }+strum = { workspace = true }+thiserror = { workspace = true }+tokio = { workspace = true }+tracing = { workspace = true }+utoipa = { workspace = true }+uuid = { workspace = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }diff --git a/crates/import/src/domain/mod.rs b/crates/import/src/domain/mod.rsnew file mode 100644index 0000000000..2a17b732fc--- /dev/null+++ b/crates/import/src/domain/mod.rs@@ -0,0 +1,5 @@+//! Domain layer: pure models, ports, and the import service.++pub mod models;+pub mod ports;+pub mod service;diff --git a/crates/import/src/domain/models.rs b/crates/import/src/domain/models.rsnew file mode 100644index 0000000000..25969cc463--- /dev/null+++ b/crates/import/src/domain/models.rs@@ -0,0 +1,430 @@+//! Pure data model for the import pipeline: sources, statuses, per-source+//! metadata shapes, and foreign-id normalization. Everything here is+//! side-effect free.++use chrono::{DateTime, Utc};+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};+use utoipa::ToSchema;+use uuid::Uuid;++#[cfg(test)]+mod test;++/// External systems items can be imported from.+#[derive(+ Debug,+ Clone,+ Copy,+ PartialEq,+ Eq,+ Hash,+ Serialize,+ Deserialize,+ JsonSchema,+ ToSchema,+ strum::EnumString,+ strum::AsRefStr,+ strum::EnumIter,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum ImportSource {+ /// Linear issues, imported as Macro tasks.+ Linear,+ /// Notion pages, imported as Macro markdown documents.+ Notion,+ /// Slack channels, recreated as Macro channels.+ Slack,+}++impl ImportSource {+ /// All import sources.+ pub fn all() -> [ImportSource; 3] {+ [+ ImportSource::Linear,+ ImportSource::Notion,+ ImportSource::Slack,+ ]+ }++ /// The MCP server URL backing this source's connector.+ pub fn mcp_server_url(self) -> &'static str {+ match self {+ ImportSource::Linear => "https://mcp.linear.app/mcp",+ ImportSource::Notion => "https://mcp.notion.com/mcp",+ ImportSource::Slack => "https://mcp.slack.com/mcp",+ }+ }++ /// The Macro entity type items from this source become. The mapping is+ /// fixed — importers have latitude on content, never on shape.+ pub fn entity_type(self) -> &'static str {+ match self {+ ImportSource::Linear => "task",+ ImportSource::Notion => "md",+ ImportSource::Slack => "channel",+ }+ }++ /// Normalize a raw foreign id for this source: trimmed, with+ /// source-specific canonicalization (Notion URLs collapse to the 32-hex+ /// page id, Slack names keep a single leading `#`). `None` when the raw+ /// id is empty.+ pub fn normalize_foreign_id(self, raw: &str) -> Option<String> {+ let raw = raw.trim();+ if raw.is_empty() {+ return None;+ }+ Some(match self {+ ImportSource::Linear => raw.to_string(),+ // Page ids survive renames; URLs don't. Accept either and store+ // the id whenever one is present.+ ImportSource::Notion => notion_page_id(raw).unwrap_or_else(|| raw.to_string()),+ ImportSource::Slack => {+ // Slack channel ids (`C0123ABCD…`) pass through; a bare or+ // `#`-prefixed name normalizes to `#name`.+ if raw.starts_with('C')+ && raw.len() >= 9+ && raw.chars().all(|c| c.is_ascii_alphanumeric())+ {+ raw.to_string()+ } else {+ format!("#{}", raw.trim_start_matches('#'))+ }+ }+ })+ }+}++/// Lifecycle of one import entity.+#[derive(+ Debug,+ Clone,+ Copy,+ PartialEq,+ Eq,+ Serialize,+ Deserialize,+ JsonSchema,+ ToSchema,+ strum::EnumString,+ strum::AsRefStr,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum ImportStatus {+ /// Discovered and offered, not yet accepted.+ Staged,+ /// Accepted; an import job is copying it in.+ Importing,+ /// A Macro entity exists for it (`entity_id`/`entity_type` are set).+ Imported,+ /// The user declined it. Remembered so re-gathers don't re-stage it.+ Discarded,+}++/// Where an import entity was first staged from. Provenance only — never a+/// visibility filter.+#[derive(+ Debug,+ Clone,+ Copy,+ PartialEq,+ Eq,+ Serialize,+ Deserialize,+ JsonSchema,+ ToSchema,+ strum::EnumString,+ strum::AsRefStr,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum Initiator {+ /// Staged by the onboarding gather job (`/setup`).+ Onboarding,+ /// Staged by an AI chat session.+ Chat,+}++/// Lifecycle of a gather run (one per user × source).+#[derive(+ Debug,+ Clone,+ Copy,+ PartialEq,+ Eq,+ Serialize,+ Deserialize,+ ToSchema,+ strum::EnumString,+ strum::AsRefStr,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum RunStatus {+ /// A gather job is discovering candidates.+ Running,+ /// The gather finished; staged rows (if any) are ready.+ Ready,+ /// The gather failed; `error` has details. Retryable.+ Failed,+ /// The user dismissed this source's import section.+ Dismissed,+}++// ---------------------------------------------------------------------------+// Per-source metadata+// ---------------------------------------------------------------------------++/// Caps applied to metadata text at the tool write boundary, so a chatty+/// agent can't bloat rows.+const MAX_TEXT: usize = 300;+/// Cap for long-form text (issue descriptions).+const MAX_LONG_TEXT: usize = 4_000;+/// Cap for summaries and purposes.+const MAX_SUMMARY: usize = 600;+/// Cap on Slack participant lists.+const MAX_PARTICIPANTS: usize = 25;++/// Truncate to a character boundary at most `max` bytes in.+fn truncated(s: String, max: usize) -> String {+ if s.len() <= max {+ return s;+ }+ let mut end = max;+ while end > 0 && !s.is_char_boundary(end) {+ end -= 1;+ }+ let mut s = s;+ s.truncate(end);+ s+}++fn truncate_opt(s: Option<String>, max: usize) -> Option<String> {+ s.map(|s| truncated(s, max))+ .filter(|s| !s.trim().is_empty())+}++/// Metadata for one staged Linear issue.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+pub struct LinearIssueMeta {+ /// Linear's human identifier (e.g. `ENG-142`).+ pub identifier: Option<String>,+ /// Issue title.+ pub title: String,+ /// Short markdown description.+ pub description: Option<String>,+ /// Workflow status name (e.g. `In Progress`).+ pub status: Option<String>,+ /// Priority label (e.g. `Urgent`).+ pub priority: Option<String>,+ /// Assignee display name as Linear reports it.+ pub assignee: Option<String>,+ /// Assignee email, when Linear exposes it.+ pub assignee_email: Option<String>,+ /// Due date as an ISO date (`YYYY-MM-DD`), when set.+ #[serde(default)]+ pub due_date: Option<String>,+ /// Deep link back to the issue in Linear.+ pub url: Option<String>,+}++impl LinearIssueMeta {+ fn capped(self) -> Self {+ Self {+ identifier: truncate_opt(self.identifier, MAX_TEXT),+ title: truncated(self.title, MAX_TEXT),+ description: truncate_opt(self.description, MAX_LONG_TEXT),+ status: truncate_opt(self.status, MAX_TEXT),+ priority: truncate_opt(self.priority, MAX_TEXT),+ assignee: truncate_opt(self.assignee, MAX_TEXT),+ assignee_email: truncate_opt(self.assignee_email, MAX_TEXT),+ due_date: truncate_opt(self.due_date, MAX_TEXT),+ url: truncate_opt(self.url, MAX_TEXT),+ }+ }+}++/// Metadata for one staged Notion page. Deliberately has NO content field:+/// page bodies are fetched at import time, for accepted pages only.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+pub struct NotionDocMeta {+ /// Page title.+ pub title: String,+ /// Deep link back to the page in Notion.+ pub url: Option<String>,+ /// One-line summary of what the page contains.+ pub summary: Option<String>,+}++impl NotionDocMeta {+ fn capped(self) -> Self {+ Self {+ title: truncated(self.title, MAX_TEXT),+ url: truncate_opt(self.url, MAX_TEXT),+ summary: truncate_opt(self.summary, MAX_SUMMARY),+ }+ }+}++/// A person active in a staged Slack channel.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+pub struct SlackParticipant {+ /// Display name or Slack handle.+ pub name: String,+ /// Email, when the workspace exposes it.+ pub email: Option<String>,+}++/// Metadata for one staged Slack channel.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]+pub struct SlackChannelMeta {+ /// Channel name without the leading `#`.+ pub name: String,+ /// Slack's channel id (e.g. `C0123456789`), stable across renames.+ pub channel_id: Option<String>,+ /// The channel's purpose/topic, when set.+ pub purpose: Option<String>,+ /// The channel's most relevant members, when discoverable.+ #[serde(default)]+ pub participants: Vec<SlackParticipant>,+}++impl SlackChannelMeta {+ fn capped(self) -> Self {+ Self {+ name: truncated(self.name, MAX_TEXT),+ channel_id: truncate_opt(self.channel_id, MAX_TEXT),+ purpose: truncate_opt(self.purpose, MAX_SUMMARY),+ participants: self+ .participants+ .into_iter()+ .take(MAX_PARTICIPANTS)+ .map(|p| SlackParticipant {+ name: truncated(p.name, MAX_TEXT),+ email: truncate_opt(p.email, MAX_TEXT),+ })+ .collect(),+ }+ }+}++/// Validate raw metadata against `source`'s shape and re-serialize it with+/// caps applied. Unknown fields are dropped; missing required fields error.+pub fn validate_metadata(+ source: ImportSource,+ metadata: serde_json::Value,+) -> Result<serde_json::Value, serde_json::Error> {+ match source {+ ImportSource::Linear => {+ serde_json::to_value(serde_json::from_value::<LinearIssueMeta>(metadata)?.capped())+ }+ ImportSource::Notion => {+ serde_json::to_value(serde_json::from_value::<NotionDocMeta>(metadata)?.capped())+ }+ ImportSource::Slack => {+ serde_json::to_value(serde_json::from_value::<SlackChannelMeta>(metadata)?.capped())+ }+ }+}++/// A human label for an entity, pulled from its metadata (for logs, agent+/// prompts, and tool responses).+pub fn metadata_label(source: ImportSource, metadata: &serde_json::Value) -> String {+ let field = match source {+ ImportSource::Linear | ImportSource::Notion => "title",+ ImportSource::Slack => "name",+ };+ metadata+ .get(field)+ .and_then(|v| v.as_str())+ .unwrap_or("(unnamed)")+ .to_string()+}++/// Extract the Notion page id from a page URL: the trailing 32-hex token+/// (with or without UUID dashes), normalized to undashed lowercase.+fn notion_page_id(url: &str) -> Option<String> {+ let path = url.split(['?', '#']).next().unwrap_or(url);+ let segment = path.trim_end_matches('/').rsplit('/').next()?;+ let is_hex = |s: &str| !s.is_empty() && s.chars().all(|c| c.is_ascii_hexdigit());++ // Undashed form: the whole segment, or the slug's last `-` token+ // (`Page-Title-<32hex>`).+ let tail = segment.rsplit('-').next().unwrap_or(segment);+ if tail.len() == 32 && is_hex(tail) {+ return Some(tail.to_lowercase());+ }++ // Dashed UUID form at the end of the segment (`…-8-4-4-4-12`).+ let candidate = segment.get(segment.len().checked_sub(36)?..)?;+ let dashes_ok = [8, 13, 18, 23]+ .iter()+ .all(|&i| candidate.as_bytes().get(i) == Some(&b'-'));+ let undashed: String = candidate.chars().filter(|c| *c != '-').collect();+ (dashes_ok && undashed.len() == 32 && is_hex(&undashed)).then(|| undashed.to_lowercase())+}++// ---------------------------------------------------------------------------+// Rows and aggregates+// ---------------------------------------------------------------------------++/// One row of the import ledger.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]+pub struct ImportEntity {+ /// Ledger row id.+ pub id: Uuid,+ /// The user who staged/imported it. Team-imported rows from teammates+ /// are visible with their owner's id, so clients can say "created by a+ /// teammate".+ pub user_id: String,+ /// The team the created entity was shared with, when it was.+ pub team_id: Option<Uuid>,+ /// The external system the item comes from.+ pub source: ImportSource,+ /// Stable id in the source system (see+ /// [`ImportSource::normalize_foreign_id`]).+ pub foreign_id: String,+ /// Where the row is in its lifecycle.+ pub status: ImportStatus,+ /// Where it was first staged from.+ pub initiator: Initiator,+ /// Per-source metadata (shape depends on `source`; see+ /// [`LinearIssueMeta`], [`NotionDocMeta`], [`SlackChannelMeta`]).+ pub metadata: serde_json::Value,+ /// The Macro entity it became, once imported.+ pub entity_id: Option<String>,+ /// The Macro entity type (`task` | `md` | `channel`), once imported.+ pub entity_type: Option<String>,+ /// Failure detail from the last import attempt, when one failed.+ pub last_error: Option<String>,+ /// When the row was first staged.+ pub created_at: DateTime<Utc>,+ /// When the row last changed.+ pub updated_at: DateTime<Utc>,+}++/// Gather-run state for one user × source.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]+pub struct ImportRun {+ /// The source this run gathered from.+ pub source: ImportSource,+ /// Where the run is in its lifecycle.+ pub status: RunStatus,+ /// Failure detail, when the run failed.+ pub error: Option<String>,+ /// When the run state last changed.+ pub updated_at: DateTime<Utc>,+}++/// The full import aggregate for a user: gather runs plus visible ledger+/// rows (their own, and teammates' team-imported rows).+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]+pub struct ImportState {+ /// Gather runs, one per source that ever gathered.+ pub runs: Vec<ImportRun>,+ /// Visible import entities, newest first.+ pub entities: Vec<ImportEntity>,+}diff --git a/crates/import/src/domain/models/test.rs b/crates/import/src/domain/models/test.rsnew file mode 100644index 0000000000..66a3f53d32--- /dev/null+++ b/crates/import/src/domain/models/test.rs@@ -0,0 +1,153 @@+use super::*;+use std::str::FromStr;++#[test]+fn enums_round_trip_through_strings() {+ for source in ImportSource::all() {+ assert_eq!(+ ImportSource::from_str(source.as_ref()).unwrap(),+ source,+ "source {}",+ source.as_ref()+ );+ }+ for status in [+ ImportStatus::Staged,+ ImportStatus::Importing,+ ImportStatus::Imported,+ ImportStatus::Discarded,+ ] {+ assert_eq!(+ ImportStatus::from_str(status.as_ref()).unwrap(),+ status,+ "status {}",+ status.as_ref()+ );+ }+ for initiator in [Initiator::Onboarding, Initiator::Chat] {+ assert_eq!(Initiator::from_str(initiator.as_ref()).unwrap(), initiator);+ }+ for run in [+ RunStatus::Running,+ RunStatus::Ready,+ RunStatus::Failed,+ RunStatus::Dismissed,+ ] {+ assert_eq!(RunStatus::from_str(run.as_ref()).unwrap(), run);+ }+}++#[test]+fn normalize_foreign_id_per_source() {+ // Linear: trimmed pass-through, blank means none.+ assert_eq!(+ ImportSource::Linear.normalize_foreign_id(" ENG-142 "),+ Some("ENG-142".into())+ );+ assert_eq!(ImportSource::Linear.normalize_foreign_id(" "), None);++ // Notion: page id out of the URL, both undashed-slug and dashed-UUID+ // forms, normalized to undashed lowercase; unparseable URL keys whole;+ // a bare page id passes through lowercased.+ let id = "0123456789abcdef0123456789ABCDEF";+ assert_eq!(+ ImportSource::Notion+ .normalize_foreign_id(&format!("https://www.notion.so/acme/Page-Title-{id}?pvs=4")),+ Some(id.to_lowercase())+ );+ assert_eq!(+ ImportSource::Notion+ .normalize_foreign_id("https://notion.so/01234567-89ab-cdef-0123-456789abcdef"),+ Some("0123456789abcdef0123456789abcdef".into())+ );+ assert_eq!(+ ImportSource::Notion.normalize_foreign_id("https://www.notion.so/acme/weird-page"),+ Some("https://www.notion.so/acme/weird-page".into())+ );+ assert_eq!(+ ImportSource::Notion.normalize_foreign_id(id),+ Some(id.to_lowercase())+ );++ // Slack: channel ids pass through; bare or #-prefixed names normalize+ // to a single leading # so they can't collide with a real Cxxxx id.+ assert_eq!(+ ImportSource::Slack.normalize_foreign_id("C0123456789"),+ Some("C0123456789".into())+ );+ assert_eq!(+ ImportSource::Slack.normalize_foreign_id("eng"),+ Some("#eng".into())+ );+ assert_eq!(+ ImportSource::Slack.normalize_foreign_id("#eng"),+ Some("#eng".into())+ );+}++#[test]+fn fixed_entity_type_mapping() {+ assert_eq!(ImportSource::Linear.entity_type(), "task");+ assert_eq!(ImportSource::Notion.entity_type(), "md");+ assert_eq!(ImportSource::Slack.entity_type(), "channel");+}++#[test]+fn validate_metadata_caps_and_drops_unknown_fields() {+ let long = "x".repeat(10_000);+ let raw = serde_json::json!({+ "title": "Roadmap",+ "description": long,+ "surprise_field": "dropped",+ });+ let validated = validate_metadata(ImportSource::Linear, raw).expect("valid linear metadata");+ assert_eq!(validated["title"], "Roadmap");+ assert_eq!(+ validated["description"].as_str().unwrap().len(),+ 4_000,+ "long description is capped"+ );+ assert!(validated.get("surprise_field").is_none());++ // Missing required fields error.+ assert!(validate_metadata(ImportSource::Slack, serde_json::json!({"purpose": "x"})).is_err());++ // Participant lists are capped.+ let many: Vec<_> = (0..100)+ .map(|i| serde_json::json!({"name": format!("p{i}")}))+ .collect();+ let validated = validate_metadata(+ ImportSource::Slack,+ serde_json::json!({"name": "eng", "participants": many}),+ )+ .unwrap();+ assert_eq!(validated["participants"].as_array().unwrap().len(), 25);+}++#[test]+fn truncation_respects_char_boundaries() {+ // A multi-byte char straddling the cap must not split.+ let s = format!("{}é", "a".repeat(299));+ let capped = truncated(s, 300);+ assert_eq!(+ capped.len(),+ 299,+ "é (2 bytes) at byte 299 is dropped whole"+ );+}++#[test]+fn metadata_label_reads_title_or_name() {+ assert_eq!(+ metadata_label(ImportSource::Linear, &serde_json::json!({"title": "T"})),+ "T"+ );+ assert_eq!(+ metadata_label(ImportSource::Slack, &serde_json::json!({"name": "eng"})),+ "eng"+ );+ assert_eq!(+ metadata_label(ImportSource::Notion, &serde_json::json!({})),+ "(unnamed)"+ );+}diff --git a/crates/import/src/domain/ports.rs b/crates/import/src/domain/ports.rsnew file mode 100644index 0000000000..0985e24fc2--- /dev/null+++ b/crates/import/src/domain/ports.rs@@ -0,0 +1,257 @@+//! Ports (traits) the import domain depends on.++use super::models::{ImportEntity, ImportRun, ImportSource, ImportStatus, Initiator, RunStatus};+use macro_user_id::user_id::MacroUserIdStr;+use thiserror::Error;+use uuid::Uuid;++/// Errors surfaced by the import service and repository.+#[derive(Debug, Error)]+pub enum ImportError {+ /// Database failure.+ #[error(transparent)]+ Db(#[from] sqlx::Error),+ /// Metadata failed to (de)serialize.+ #[error("invalid import metadata: {0}")]+ Metadata(#[from] serde_json::Error),+ /// Anything else.+ #[error(transparent)]+ Other(#[from] anyhow::Error),+}++/// Result alias for import operations.+pub type Result<T> = std::result::Result<T, ImportError>;++/// Persistence for the import ledger and gather runs.+///+/// Status writes are compare-and-swaps that report whether they happened, so+/// concurrent gather jobs, import jobs, and user actions interleave without+/// clobbering each other.+pub trait ImportRepo: Send + Sync + 'static {+ /// The user's own row for `(source, foreign_id)`, regardless of status.+ fn get_own_by_foreign_id(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ foreign_id: &str,+ ) -> impl Future<Output = Result<Option<ImportEntity>>> + Send;++ /// A teammate's `imported` row for `(source, foreign_id)` shared with the+ /// user's team, if any.+ fn find_team_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ foreign_id: &str,+ ) -> impl Future<Output = Result<Option<ImportEntity>>> + Send;++ /// Upsert a `staged` row. Inserts, or refreshes the metadata of an+ /// existing `staged` row. Returns `None` when the user's row exists in a+ /// non-staged status (imported / importing / discarded) — the caller+ /// decides what that means.+ fn upsert_staged(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ initiator: Initiator,+ foreign_id: &str,+ metadata: &serde_json::Value,+ ) -> impl Future<Output = Result<Option<ImportEntity>>> + Send;++ /// Upsert an `imported` row (used when an agent already created the+ /// entity, e.g. from chat). Overwrites any non-imported own row; an+ /// already-imported row is left untouched (first mapping wins). Returns+ /// the row as stored.+ #[allow(clippy::too_many_arguments)]+ fn upsert_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ initiator: Initiator,+ foreign_id: &str,+ metadata: &serde_json::Value,+ entity_id: &str,+ entity_type: &str,+ team_id: Option<Uuid>,+ ) -> impl Future<Output = Result<ImportEntity>> + Send;++ /// The user's own row by ledger id.+ fn get(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ ) -> impl Future<Output = Result<Option<ImportEntity>>> + Send;++ /// Visible rows for the user: their own, plus teammates' team-imported+ /// rows (deduplicated against the user's own rows by `(source,+ /// foreign_id)`, own row winning). Newest first.+ fn list(+ &self,+ user: &MacroUserIdStr<'static>,+ source: Option<ImportSource>,+ status: Option<ImportStatus>,+ ) -> impl Future<Output = Result<Vec<ImportEntity>>> + Send;++ /// CAS the user's own `staged` rows in `ids` to `importing`. Returns the+ /// rows that flipped (missing / non-staged ids are skipped, not errors).+ fn mark_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ ids: &[Uuid],+ ) -> impl Future<Output = Result<Vec<ImportEntity>>> + Send;++ /// CAS one `importing` row to `imported` with the entity it became.+ fn mark_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ entity_id: &str,+ entity_type: &str,+ team_id: Option<Uuid>,+ ) -> impl Future<Output = Result<Option<ImportEntity>>> + Send;++ /// CAS one `importing` row back to `staged`, recording the failure.+ fn mark_import_failed(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ error: &str,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Bump `updated_at` on the user's still-`importing` rows in `ids` — the+ /// running batch's heartbeat, proving a live process still owns them.+ fn touch_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ ids: &[Uuid],+ ) -> impl Future<Output = Result<u64>> + Send;++ /// CAS the user's `importing` rows whose heartbeat stopped more than+ /// `older_than_secs` ago back to `staged`. Import jobs are in-process+ /// tasks: a crash or restart mid-job orphans its rows, and nothing else+ /// can move them again. Returns how many were reaped.+ fn fail_stale_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ older_than_secs: i64,+ ) -> impl Future<Output = Result<u64>> + Send;++ /// CAS one of the user's own `staged` rows to `discarded`. Returns+ /// whether it happened.+ fn discard(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Discard all of the user's remaining `staged` rows with the given+ /// initiator (an explicit "no" — `stage()` refuses to re-stage them).+ /// Returns how many flipped.+ fn discard_staged_by_initiator(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> impl Future<Output = Result<u64>> + Send;++ /// DELETE all of the user's remaining `staged` rows with the given+ /// initiator (onboarding completion cleanup: candidates the user never+ /// acted on vanish from the ledger and stay re-stageable later, unlike+ /// discarded rows). Returns how many were removed.+ fn delete_staged_by_initiator(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> impl Future<Output = Result<u64>> + Send;++ /// The user's team, when they are on one.+ fn user_team_id(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<Option<Uuid>>> + Send;++ /// All gather runs for the user.+ fn list_runs(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<Vec<ImportRun>>> + Send;++ /// CAS a gather run to `running`. Wins when no run row exists yet, or+ /// when the existing row's status is in `from`. Returns whether this+ /// call won.+ fn start_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ ) -> impl Future<Output = Result<bool>> + Send;++ /// CAS the `running` run for `source` to `to` (ready/failed), recording+ /// `error` when there is one. Returns whether it happened.+ fn finish_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ to: RunStatus,+ error: Option<&str>,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// CAS a run from one of `from` to `to` without touching its error.+ fn transition_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ to: RunStatus,+ ) -> impl Future<Output = Result<bool>> + Send;+}++/// System properties to set on an imported task, already normalized to+/// Macro's vocabulary by the import service (the creator applies them+/// best-effort — a property that fails to apply never fails the import).+#[derive(Debug, Clone, Default, PartialEq, Eq)]+pub struct ImportedTaskProperties {+ /// Macro status display label (`Not Started` … `Canceled`).+ pub status: Option<String>,+ /// Macro priority display label (`Low` … `Urgent`).+ pub priority: Option<String>,+ /// Due date as an ISO date (`YYYY-MM-DD`).+ pub due_date: Option<String>,+ /// Assignee email, resolved against the user's team roster.+ pub assignee_email: Option<String>,+}++/// Creates the Macro entities imports become. Implemented by the host+/// service against its real document/channel services; the fixed+/// source → entity-type mapping is enforced by the import service, which+/// only ever calls the creator matching the row's source.+pub trait EntityCreator: Send + Sync + 'static {+ /// Create a task document, applying `properties` best-effort once the+ /// task exists. Returns the new entity id.+ fn create_task(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ markdown: &str,+ properties: &ImportedTaskProperties,+ ) -> impl Future<Output = anyhow::Result<String>> + Send;++ /// Create a markdown document. Returns the new entity id.+ fn create_markdown_doc(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ markdown: &str,+ ) -> impl Future<Output = anyhow::Result<String>> + Send;++ /// Create a channel, shared with `team_id` when given (public+ /// otherwise). `participant_emails` are matched against the team roster+ /// best-effort; matches join the channel alongside the creator. Returns+ /// the new entity id.+ fn create_channel(+ &self,+ user: &MacroUserIdStr<'static>,+ name: &str,+ team_id: Option<Uuid>,+ participant_emails: &[String],+ ) -> impl Future<Output = anyhow::Result<String>> + Send;+}diff --git a/crates/import/src/domain/service.rs b/crates/import/src/domain/service.rsnew file mode 100644index 0000000000..873d38c306--- /dev/null+++ b/crates/import/src/domain/service.rs@@ -0,0 +1,1230 @@+//! The import orchestrator: staging (with team-wide dedup), gather jobs,+//! and import jobs.+//!+//! Gather jobs are short agent sessions over the user's connector MCP server+//! whose toolset includes an in-process `CreateImportEntity` locked to+//! `(user, source, initiator)` — the model stages candidates by calling the+//! tool, so there is no structured output to parse. Import jobs copy+//! accepted rows in: Linear tasks and Slack channels are composed+//! deterministically from staged metadata; Notion pages are fetched+//! directly through the connector's fetch tool (no model in the content+//! path), falling back to a bounded single-page Haiku session that lands+//! content through `FinalizeImport` when the direct path can't cope.++use super::models::*;+use super::ports::{EntityCreator, ImportError, ImportRepo, ImportedTaskProperties, Result};+use crate::inbound::toolset::{+ ImportToolContext, ToolPolicy, gather_toolset, notion_import_toolset,+};+use agent::types::{ChatMessage, ChatMessageContent, Role};+use agent::{AgentLoop, PredefinedModel};+use ai_toolset::{RequestContext, ToolResult, ToolSet, ToolSetError};+use futures::StreamExt;+use macro_user_id::user_id::MacroUserIdStr;+use mcp_client::domain::models::McpServerRecord;+use mcp_client::domain::ports::McpServerStore;+use mcp_client::domain::service::McpToolSet;+use std::collections::HashSet;+use std::pin::Pin;+use std::sync::Arc;+use std::time::Duration;+use uuid::Uuid;++mod prompts;++#[cfg(test)]+mod test;++/// Model for gather sessions: fast beats maximal while the user watches the+/// section shimmer, and Cerebras serves gpt-oss-120b at interactive latency.+/// A raw provider-prefixed id (not [`PredefinedModel`]) because the registry+/// has no Cerebras tier — the model router resolves the `cerebras/` prefix.+const GATHER_MODEL: &str = "cerebras/gpt-oss-120b";+/// Turn cap for gather sessions: a couple of searches plus one staging tool+/// call per candidate (providers may batch several per turn).+const GATHER_MAX_TURNS: usize = 24;+/// Hard cap on a gather session. Staged rows land incrementally, so the+/// section fills while this runs; the cap only bounds the shimmer tail.+const GATHER_TIMEOUT: Duration = Duration::from_secs(90);++/// Model for Notion import sessions (normal ai_usage attribution).+const NOTION_IMPORT_MODEL: PredefinedModel = PredefinedModel::Fast;+/// Hard cap on importing ONE Notion page (fetch + convert + finalize).+/// Pages run as independent single-page sessions.+const NOTION_PAGE_IMPORT_TIMEOUT: Duration = Duration::from_secs(120);++/// How many Notion page sessions run at once for one accepted batch —+/// enough to collapse the batch latency toward the slowest page, low+/// enough to stay polite to Notion's MCP.+const NOTION_IMPORT_CONCURRENCY: usize = 4;++/// How often a running import batch touches its rows' `updated_at`. The+/// heartbeat covers every row the batch owns — queued AND in-flight — so a+/// fresh `updated_at` means "a live process is still responsible for this+/// row", independent of how long the row waits behind the concurrency cap.+const IMPORT_HEARTBEAT: Duration = Duration::from_secs(30);++/// How long an `importing` row may go without a heartbeat before the read+/// path declares its job dead (process crashed or restarted) and sends it+/// back to `staged`. Several missed beats, so a slow DB or a paused runtime+/// never reaps a live batch.+const STALE_IMPORT_AFTER: Duration = Duration::from_secs(IMPORT_HEARTBEAT.as_secs() * 6);++/// Turn cap for a Notion import session: fetch + finalize per page, plus+/// slack for retries.+fn notion_import_max_turns(pages: usize) -> usize {+ (2 * pages + 6).min(40)+}++/// Pushes an "import state changed" nudge to the user's connected clients.+/// A closure so this crate stays free of gateway dependencies; hosts without+/// a gateway just don't set it.+pub type ImportNotify =+ Arc<dyn Fn(MacroUserIdStr<'static>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;++/// Outcome of accepting/declining staged rows via `POST /import/run`.+#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, utoipa::ToSchema)]+pub struct RunImportOutcome {+ /// How many rows were discarded.+ pub discarded: u64,+ /// How many rows flipped to `importing` (jobs are now copying them in).+ pub importing: u64,+}++/// Outcome of staging one candidate.+#[derive(Debug, Clone, PartialEq)]+pub enum StageOutcome {+ /// A staged row now exists (fresh, or metadata-refreshed).+ Staged(ImportEntity),+ /// The item was already imported. `by_teammate` distinguishes "you did+ /// this" from "someone on your team did".+ AlreadyImported {+ /// The imported row (carries the Macro entity it became).+ entity: ImportEntity,+ /// Whether a teammate (not the user) imported it.+ by_teammate: bool,+ },+ /// The user previously declined this item; it is not re-staged.+ PreviouslyDiscarded(ImportEntity),+ /// An import job is currently copying this item in.+ ImportInProgress(ImportEntity),+}++/// Outcome of discarding one staged row.+#[derive(Debug, Clone, PartialEq)]+pub enum DiscardOutcome {+ /// The row is now discarded.+ Discarded,+ /// No such row belongs to the user.+ NotFound,+ /// The row exists but is not `staged` (only staged rows can be+ /// discarded).+ NotDiscardable(ImportStatus),+}++/// The API the import router (and host services like onboarding) talk to.+pub trait ImportService: Send + Sync + 'static {+ /// The full import aggregate for the user.+ fn state(+ &self,+ user: MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<ImportState>> + Send;++ /// Start a gather run for `source` if none has ever run (the+ /// connector-just-authenticated hook). Returns whether a run started.+ fn start_gather(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Restart a failed (or dismissed) gather run. Returns whether a run+ /// started.+ fn retry_gather(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> impl Future<Output = Result<bool>> + Send;++ /// Dismiss a source's import section.+ fn dismiss_run(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> impl Future<Output = Result<()>> + Send;++ /// Accept `import_ids` (flip to `importing` and start import jobs) and+ /// discard `discard_ids`.+ fn run_import(+ &self,+ user: MacroUserIdStr<'static>,+ import_ids: Vec<Uuid>,+ discard_ids: Vec<Uuid>,+ ) -> impl Future<Output = Result<RunImportOutcome>> + Send;++ /// Discard all remaining staged rows with `initiator` — an explicit+ /// decline; `stage()` refuses to re-stage discarded items. Returns how+ /// many were discarded.+ fn discard_staged_by_initiator(+ &self,+ user: MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> impl Future<Output = Result<u64>> + Send;++ /// Delete all remaining staged rows with `initiator` (onboarding+ /// completion cleanup). Unlike discarding, deleted candidates were+ /// never reviewed and stay re-stageable by later gathers or chat.+ /// Returns how many were removed.+ fn delete_staged_by_initiator(+ &self,+ user: MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> impl Future<Output = Result<u64>> + Send;+}++/// Staging operations the AI tools drive. Split from [`ImportService`] so+/// tool contexts depend on exactly what tools can do.+pub trait ImportStager: Send + Sync + 'static {+ /// Stage one candidate, deduplicating against the user's own rows and+ /// the team's imported rows.+ fn stage(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ source: ImportSource,+ foreign_id: &str,+ metadata: serde_json::Value,+ ) -> impl Future<Output = Result<StageOutcome>> + Send;++ /// Record an entity an agent already created (chat flow) as imported.+ fn record_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ source: ImportSource,+ foreign_id: &str,+ metadata: serde_json::Value,+ entity_id: &str,+ ) -> impl Future<Output = Result<ImportEntity>> + Send;++ /// Discard one of the user's own staged rows.+ fn discard_entity(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ ) -> impl Future<Output = Result<DiscardOutcome>> + Send;++ /// Visible rows for the user (own + team-imported).+ fn list_entities(+ &self,+ user: &MacroUserIdStr<'static>,+ source: Option<ImportSource>,+ status: Option<ImportStatus>,+ ) -> impl Future<Output = Result<Vec<ImportEntity>>> + Send;+}++/// Finalization the Notion import session's `FinalizeImport` tool drives:+/// create the Macro entity for one `importing` row and flip it to+/// `imported`.+pub trait ImportFinalizer: Send + Sync + 'static {+ /// Create the document for `import_id` (fixed mapping: linear → task,+ /// notion → md) and mark the row imported.+ fn finalize_document(+ &self,+ user: &MacroUserIdStr<'static>,+ import_id: Uuid,+ name: &str,+ content_markdown: &str,+ ) -> impl Future<Output = Result<ImportEntity>> + Send;+}++/// Concrete orchestrator wiring the repo, the user's MCP servers, the+/// entity creator, and usage recording together.+pub struct ImportServiceImpl<R, S, C> {+ repo: R,+ mcp_store: Arc<S>,+ creator: Arc<C>,+ recorder: Arc<dyn ai_usage::UsageRecorder>,+ notifier: Option<ImportNotify>,+}++impl<R: Clone, S, C> Clone for ImportServiceImpl<R, S, C> {+ fn clone(&self) -> Self {+ Self {+ repo: self.repo.clone(),+ mcp_store: self.mcp_store.clone(),+ creator: self.creator.clone(),+ recorder: self.recorder.clone(),+ notifier: self.notifier.clone(),+ }+ }+}++impl<R, S, C> ImportServiceImpl<R, S, C> {+ /// Build the orchestrator.+ pub fn new(+ repo: R,+ mcp_store: Arc<S>,+ creator: Arc<C>,+ recorder: Arc<dyn ai_usage::UsageRecorder>,+ ) -> Self {+ Self {+ repo,+ mcp_store,+ creator,+ recorder,+ notifier: None,+ }+ }++ /// Push state-change nudges through the given notifier.+ pub fn with_notifier(mut self, notifier: ImportNotify) -> Self {+ self.notifier = Some(notifier);+ self+ }++ async fn notify(&self, user: &MacroUserIdStr<'static>) {+ if let Some(notifier) = &self.notifier {+ notifier(user.clone()).await;+ }+ }+}++impl<R, S, C> ImportServiceImpl<R, S, C>+where+ R: ImportRepo + Clone,+ S: McpServerStore,+ C: EntityCreator,+{+ /// Spawn the gather session for one source; finishes the run row either+ /// way and nudges the client.+ fn spawn_gather(&self, user: MacroUserIdStr<'static>, source: ImportSource) {+ let service = self.clone();+ tokio::spawn(async move {+ let outcome =+ tokio::time::timeout(GATHER_TIMEOUT, service.run_gather_session(&user, source))+ .await+ .unwrap_or_else(|_| Err(anyhow::anyhow!("gather session timed out")));++ let finished = match outcome {+ Ok(()) => {+ service+ .repo+ .finish_run(&user, source, RunStatus::Ready, None)+ .await+ }+ Err(e) => {+ tracing::warn!(source = source.as_ref(), error = ?e, "gather session failed");+ service+ .repo+ .finish_run(&user, source, RunStatus::Failed, Some(&e.to_string()))+ .await+ }+ };+ if let Err(e) = finished {+ tracing::error!(source = source.as_ref(), error = ?e, "failed to persist gather outcome");+ }+ service.notify(&user).await;+ });+ }++ /// Run one gather session: connector MCP tools plus the locked+ /// `CreateImportEntity`. Staged rows land as the session runs; the final+ /// text is ignored.+ #[tracing::instrument(skip(self, user), err)]+ async fn run_gather_session(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> anyhow::Result<()> {+ let mcp_tools = self.connector_tools(user, source).await?;+ let native = gather_toolset::<Self>();+ let toolset = NativePlusMcp::new(native, mcp_tools);+ let context = ImportToolContext {+ service: Some(Arc::new(self.clone())),+ policy: ToolPolicy::gather(source),+ };++ self.drive_session(+ user,+ GATHER_MODEL,+ GATHER_MAX_TURNS,+ toolset,+ context,+ &prompts::gather_system(source),+ prompts::gather_prompt(source),+ )+ .await+ }++ /// Copy one accepted Notion page in WITHOUT a model: call the+ /// connector's fetch tool directly, take the markdown it returns, and+ /// finalize. The fallback Haiku session exists for pages this can't+ /// handle — routing page content through a model means re-emitting the+ /// whole page as output tokens, which is an order of magnitude slower.+ #[tracing::instrument(skip(self, user, mcp_tools, row), fields(id = %row.id), err)]+ async fn import_notion_page_direct(+ &self,+ user: &MacroUserIdStr<'static>,+ mcp_tools: &McpToolSet,+ row: &ImportEntity,+ ) -> anyhow::Result<()> {+ let fetch_tool = notion_fetch_tool_name(mcp_tools)+ .ok_or_else(|| anyhow::anyhow!("connector exposes no notion-fetch tool"))?;+ let target = row+ .metadata+ .get("url")+ .and_then(|v| v.as_str())+ .unwrap_or(&row.foreign_id)+ .to_string();++ let result = ToolSet::<()>::try_tool_call(+ mcp_tools,+ (),+ RequestContext::new(user.clone()),+ &fetch_tool,+ &serde_json::json!({ "id": target }),+ )+ .await+ .map_err(|e| anyhow::anyhow!("fetch dispatch failed: {e}"))?+ .map_err(|e| anyhow::anyhow!("fetch failed: {}", e.description))?;++ let text = match result {+ serde_json::Value::String(text) => text,+ other => other.to_string(),+ };+ let (fetched_title, body) = parse_notion_fetch_text(&text);+ anyhow::ensure!(!body.trim().is_empty(), "fetched page has no content");++ let name = row+ .metadata+ .get("title")+ .and_then(|v| v.as_str())+ .map(str::to_string)+ .or(fetched_title)+ .unwrap_or_else(|| "Untitled".to_string());+ let url = row.metadata.get("url").and_then(|v| v.as_str());+ let markdown = match url {+ Some(url) => format!("{}\n\n[Original in Notion]({url})", body.trim_end()),+ None => body,+ };++ self.finalize_document(user, row.id, &name, &markdown)+ .await+ .map_err(|e| anyhow::anyhow!("finalize failed: {e}"))?;+ Ok(())+ }++ /// Fallback: run a single-page Haiku session over the shared connector+ /// tools. Rows the agent fails to finalize are handled by the caller.+ #[tracing::instrument(skip(self, user, mcp_tools, rows), fields(pages = rows.len()), err)]+ async fn run_notion_import_session(+ &self,+ user: &MacroUserIdStr<'static>,+ rows: &[ImportEntity],+ mcp_tools: Arc<McpToolSet>,+ ) -> anyhow::Result<()> {+ let native = notion_import_toolset::<Self>();+ let toolset = NativePlusMcp::new(native, mcp_tools);+ let context = ImportToolContext {+ service: Some(Arc::new(self.clone())),+ policy: ToolPolicy::import_job(),+ };++ self.drive_session(+ user,+ NOTION_IMPORT_MODEL,+ notion_import_max_turns(rows.len()),+ toolset,+ context,+ prompts::NOTION_IMPORT_SYSTEM,+ &prompts::notion_import_prompt(rows),+ )+ .await+ }++ /// Load the user's MCP tools for `source`'s connector. Shared (behind an+ /// Arc) across the concurrent per-page work of one batch.+ async fn connector_tools(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> anyhow::Result<Arc<McpToolSet>> {+ let url = source.mcp_server_url();+ let records: Vec<McpServerRecord> = self+ .mcp_store+ .list(user)+ .await+ .map_err(|e| anyhow::anyhow!("mcp store: {e:?}"))?+ .into_iter()+ .filter(|r| r.url == url)+ .collect();+ anyhow::ensure!(!records.is_empty(), "no {} connection", source.as_ref());++ let mcp_tools = McpToolSet::new(&records).await;+ anyhow::ensure!(+ !mcp_tools.is_empty(),+ "could not load tools from {}",+ source.as_ref()+ );+ Ok(Arc::new(mcp_tools))+ }++ /// Run one bounded agent session to completion, discarding the text.+ #[allow(clippy::too_many_arguments)]+ async fn drive_session(+ &self,+ user: &MacroUserIdStr<'static>,+ model: impl ToString,+ max_turns: usize,+ toolset: NativePlusMcp<ImportToolContext<Self>>,+ context: ImportToolContext<Self>,+ system_prompt: &str,+ user_prompt: &str,+ ) -> anyhow::Result<()> {+ let usage_ctx = ai_usage::UsageContext::new(ai_usage::AiFeature::Import, user.clone());+ let agent_loop = AgentLoop::new(self.recorder.clone())+ .with_model(model)+ .with_max_turns(max_turns);+ let toolset: Arc<dyn ToolSet<ImportToolContext<Self>> + Send + Sync> = Arc::new(toolset);+ let mut session = agent_loop+ .session(toolset, Arc::new(context), system_prompt, usage_ctx)+ .await;++ let opening = ChatMessage {+ role: Role::User,+ content: ChatMessageContent::Text(user_prompt.to_string()),+ attachments: None,+ };+ let mut stream = session+ .send_message(agent::to_rig_messages(&[opening]))+ .await?;+ while let Some(part) = stream.next().await {+ // Only tool effects matter; text chunks are discarded.+ part?;+ }+ Ok(())+ }++ /// Copy one accepted Linear/Slack row in, deterministically from its+ /// staged metadata.+ async fn import_deterministic(&self, user: &MacroUserIdStr<'static>, row: &ImportEntity) {+ let created: anyhow::Result<(String, Option<Uuid>)> = match row.source {+ ImportSource::Linear => {+ match serde_json::from_value::<LinearIssueMeta>(row.metadata.clone()) {+ Ok(meta) => {+ let (name, markdown) = linear_task_content(&meta);+ let properties = linear_task_properties(&meta);+ self.creator+ .create_task(user, &name, &markdown, &properties)+ .await+ .map(|id| (id, None))+ }+ Err(e) => Err(anyhow::anyhow!("invalid linear metadata: {e}")),+ }+ }+ ImportSource::Slack => {+ match serde_json::from_value::<SlackChannelMeta>(row.metadata.clone()) {+ Ok(meta) => {+ // Channels always associate with the user's team when+ // they have one — that is what makes team dedup work.+ match self.repo.user_team_id(user).await {+ Ok(team_id) => {+ // Teammates who were in the Slack channel join+ // the Macro one (matched by email downstream).+ let emails: Vec<String> = meta+ .participants+ .iter()+ .filter_map(|p| p.email.clone())+ .collect();+ self.creator+ .create_channel(user, &meta.name, team_id, &emails)+ .await+ .map(|id| (id, team_id))+ }+ Err(e) => Err(anyhow::anyhow!("team lookup failed: {e}")),+ }+ }+ Err(e) => Err(anyhow::anyhow!("invalid slack metadata: {e}")),+ }+ }+ // Notion rows go through the agent session, never here.+ ImportSource::Notion => return,+ };++ let persisted = match created {+ Ok((entity_id, team_id)) => match self+ .repo+ .mark_imported(user, row.id, &entity_id, row.source.entity_type(), team_id)+ .await+ {+ Ok(Some(_)) => Ok(()),+ // The CAS missed: the entity exists but the row left+ // `importing` under us (reaped, or another mover). Surface+ // it loudly — re-accepting the row would duplicate the+ // entity — matching finalize_document's behavior.+ Ok(None) => Err(ImportError::Other(anyhow::anyhow!(+ "created entity {entity_id} but row was no longer importing; possible orphan"+ ))),+ Err(e) => Err(e),+ },+ Err(e) => {+ tracing::warn!(id = %row.id, source = row.source.as_ref(), error = ?e, "deterministic import failed");+ self.repo+ .mark_import_failed(user, row.id, &e.to_string())+ .await+ .map(|_| ())+ }+ };+ if let Err(e) = persisted {+ tracing::error!(id = %row.id, error = ?e, "failed to persist import outcome");+ }+ self.notify(user).await;+ }++ /// Fail every row of `ids` still `importing` (agent session ended+ /// without finalizing them).+ async fn fail_unfinished(&self, user: &MacroUserIdStr<'static>, ids: &[Uuid], reason: &str) {+ for id in ids {+ match self.repo.get(user, *id).await {+ Ok(Some(row)) if row.status == ImportStatus::Importing => {+ if let Err(e) = self.repo.mark_import_failed(user, *id, reason).await {+ tracing::error!(id = %id, error = ?e, "failed to mark unfinished import");+ }+ }+ Ok(_) => {}+ Err(e) => {+ tracing::error!(id = %id, error = ?e, "failed to check unfinished import")+ }+ }+ }+ }+}++impl<R, S, C> ImportService for ImportServiceImpl<R, S, C>+where+ R: ImportRepo + Clone,+ S: McpServerStore,+ C: EntityCreator,+{+ #[tracing::instrument(skip(self, user), err)]+ async fn state(&self, user: MacroUserIdStr<'static>) -> Result<ImportState> {+ // Self-heal on read: import jobs are in-process tasks, so a service+ // restart mid-job orphans its `importing` rows — nothing else would+ // ever move them again. Any row importing longer than the longest+ // legitimate job is dead; send it back to staged so the user can+ // retry instead of watching a spinner forever.+ match self+ .repo+ .fail_stale_importing(&user, STALE_IMPORT_AFTER.as_secs() as i64)+ .await+ {+ Ok(0) => {}+ Ok(reaped) => {+ tracing::warn!(reaped, "reaped orphaned importing rows")+ }+ // A read must never fail because the reap did.+ Err(e) => tracing::warn!(error = ?e, "failed to reap stale imports"),+ }++ let runs = self.repo.list_runs(&user).await?;+ let entities = self.repo.list(&user, None, None).await?;+ Ok(ImportState { runs, entities })+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn start_gather(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> Result<bool> {+ // Wins only when no run row exists — gathers run once per+ // connection; explicit retry is the only re-entry.+ let won = self.repo.start_run(&user, source, &[]).await?;+ if won {+ self.spawn_gather(user.clone(), source);+ self.notify(&user).await;+ }+ Ok(won)+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn retry_gather(+ &self,+ user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> Result<bool> {+ let won = self+ .repo+ .start_run(&user, source, &[RunStatus::Failed, RunStatus::Dismissed])+ .await?;+ if won {+ self.spawn_gather(user.clone(), source);+ self.notify(&user).await;+ }+ Ok(won)+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn dismiss_run(&self, user: MacroUserIdStr<'static>, source: ImportSource) -> Result<()> {+ self.repo+ .transition_run(+ &user,+ source,+ &[RunStatus::Ready, RunStatus::Failed],+ RunStatus::Dismissed,+ )+ .await?;+ Ok(())+ }++ #[tracing::instrument(skip(self, user, import_ids, discard_ids), fields(imports = import_ids.len(), discards = discard_ids.len()), err)]+ async fn run_import(+ &self,+ user: MacroUserIdStr<'static>,+ import_ids: Vec<Uuid>,+ discard_ids: Vec<Uuid>,+ ) -> Result<RunImportOutcome> {+ let mut discarded = 0u64;+ for id in discard_ids {+ if self.repo.discard(&user, id).await? {+ discarded += 1;+ }+ }++ let rows = self.repo.mark_importing(&user, &import_ids).await?;+ let importing = rows.len() as u64;+ self.notify(&user).await;++ if !rows.is_empty() {+ let (notion_rows, direct_rows): (Vec<ImportEntity>, Vec<ImportEntity>) = rows+ .into_iter()+ .partition(|row| row.source == ImportSource::Notion);++ let service = self.clone();+ let user = user.clone();+ tokio::spawn(async move {+ // Heartbeat every row this batch owns (queued and in-flight)+ // for as long as the batch runs, so the read path's stale+ // reaper only ever fires on rows whose process actually+ // died. Dropped (aborted) on every exit path of this task.+ let _heartbeat = AbortOnDrop(tokio::spawn({+ let service = service.clone();+ let user = user.clone();+ let ids: Vec<Uuid> = direct_rows+ .iter()+ .chain(notion_rows.iter())+ .map(|row| row.id)+ .collect();+ async move {+ loop {+ tokio::time::sleep(IMPORT_HEARTBEAT).await;+ if let Err(e) = service.repo.touch_importing(&user, &ids).await {+ tracing::warn!(error = ?e, "import heartbeat failed");+ }+ }+ }+ }));++ for row in &direct_rows {+ service.import_deterministic(&user, row).await;+ }++ if !notion_rows.is_empty() {+ // One connector connection per batch, pages a few at a+ // time. Each page first tries the direct path (fetch+ // tool → markdown → finalize, no model); the Haiku+ // session is the fallback for pages that path can't+ // handle.+ let mcp_tools = match service.connector_tools(&user, ImportSource::Notion).await+ {+ Ok(tools) => tools,+ Err(e) => {+ tracing::warn!(error = ?e, "notion connector unavailable");+ let ids: Vec<Uuid> = notion_rows.iter().map(|r| r.id).collect();+ service+ .fail_unfinished(&user, &ids, &format!("notion unavailable: {e}"))+ .await;+ service.notify(&user).await;+ return;+ }+ };++ futures::stream::iter(notion_rows)+ .for_each_concurrent(NOTION_IMPORT_CONCURRENCY, |row| {+ let service = service.clone();+ let user = user.clone();+ let mcp_tools = mcp_tools.clone();+ async move {+ let outcome = tokio::time::timeout(+ NOTION_PAGE_IMPORT_TIMEOUT,+ async {+ match service+ .import_notion_page_direct(&user, &mcp_tools, &row)+ .await+ {+ Ok(()) => Ok(()),+ Err(direct_error) => {+ tracing::info!(id = %row.id, error = ?direct_error, "direct notion import failed; trying the agent");+ service+ .run_notion_import_session(+ &user,+ std::slice::from_ref(&row),+ mcp_tools.clone(),+ )+ .await+ }+ }+ },+ )+ .await+ .unwrap_or_else(|_| {+ Err(anyhow::anyhow!("notion import timed out"))+ });+ if let Err(e) = outcome {+ tracing::warn!(id = %row.id, error = ?e, "notion page import failed");+ }+ service+ .fail_unfinished(+ &user,+ &[row.id],+ "the import job did not finish this item",+ )+ .await;+ }+ })+ .await;+ service.notify(&user).await;+ }+ });+ }++ Ok(RunImportOutcome {+ discarded,+ importing,+ })+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn discard_staged_by_initiator(+ &self,+ user: MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> Result<u64> {+ let discarded = self+ .repo+ .discard_staged_by_initiator(&user, initiator)+ .await?;+ if discarded > 0 {+ self.notify(&user).await;+ }+ Ok(discarded)+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn delete_staged_by_initiator(+ &self,+ user: MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> Result<u64> {+ let removed = self+ .repo+ .delete_staged_by_initiator(&user, initiator)+ .await?;+ if removed > 0 {+ self.notify(&user).await;+ }+ Ok(removed)+ }+}++impl<R, S, C> ImportStager for ImportServiceImpl<R, S, C>+where+ R: ImportRepo + Clone,+ S: McpServerStore,+ C: EntityCreator,+{+ #[tracing::instrument(skip(self, user, metadata), err)]+ async fn stage(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ source: ImportSource,+ foreign_id: &str,+ metadata: serde_json::Value,+ ) -> Result<StageOutcome> {+ let foreign_id = source+ .normalize_foreign_id(foreign_id)+ .ok_or_else(|| ImportError::Other(anyhow::anyhow!("foreign_id must not be empty")))?;+ let metadata = validate_metadata(source, metadata)?;++ // The user's own row wins over any teammate row.+ if let Some(own) = self+ .repo+ .get_own_by_foreign_id(user, source, &foreign_id)+ .await?+ {+ match own.status {+ ImportStatus::Imported => {+ return Ok(StageOutcome::AlreadyImported {+ entity: own,+ by_teammate: false,+ });+ }+ ImportStatus::Discarded => return Ok(StageOutcome::PreviouslyDiscarded(own)),+ ImportStatus::Importing => return Ok(StageOutcome::ImportInProgress(own)),+ ImportStatus::Staged => {}+ }+ } else if let Some(teammate) = self+ .repo+ .find_team_imported(user, source, &foreign_id)+ .await?+ {+ return Ok(StageOutcome::AlreadyImported {+ entity: teammate,+ by_teammate: true,+ });+ }++ match self+ .repo+ .upsert_staged(user, source, initiator, &foreign_id, &metadata)+ .await?+ {+ Some(row) => {+ self.notify(user).await;+ Ok(StageOutcome::Staged(row))+ }+ // Raced into a non-staged status between the check and the+ // upsert; re-read and classify.+ None => {+ let row = self+ .repo+ .get_own_by_foreign_id(user, source, &foreign_id)+ .await?+ .ok_or_else(|| {+ ImportError::Other(anyhow::anyhow!("staging upsert vanished"))+ })?;+ match row.status {+ ImportStatus::Discarded => Ok(StageOutcome::PreviouslyDiscarded(row)),+ ImportStatus::Imported => Ok(StageOutcome::AlreadyImported {+ entity: row,+ by_teammate: false,+ }),+ _ => Ok(StageOutcome::ImportInProgress(row)),+ }+ }+ }+ }++ #[tracing::instrument(skip(self, user, metadata), err)]+ async fn record_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ source: ImportSource,+ foreign_id: &str,+ metadata: serde_json::Value,+ entity_id: &str,+ ) -> Result<ImportEntity> {+ let foreign_id = source+ .normalize_foreign_id(foreign_id)+ .ok_or_else(|| ImportError::Other(anyhow::anyhow!("foreign_id must not be empty")))?;+ let metadata = validate_metadata(source, metadata)?;++ // Channels always associate with the user's team when they have one.+ let team_id = match source {+ ImportSource::Slack => self.repo.user_team_id(user).await?,+ _ => None,+ };++ let row = self+ .repo+ .upsert_imported(+ user,+ source,+ initiator,+ &foreign_id,+ &metadata,+ entity_id,+ source.entity_type(),+ team_id,+ )+ .await?;+ self.notify(user).await;+ Ok(row)+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn discard_entity(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ ) -> Result<DiscardOutcome> {+ if self.repo.discard(user, id).await? {+ self.notify(user).await;+ return Ok(DiscardOutcome::Discarded);+ }+ Ok(match self.repo.get(user, id).await? {+ None => DiscardOutcome::NotFound,+ Some(row) => DiscardOutcome::NotDiscardable(row.status),+ })+ }++ async fn list_entities(+ &self,+ user: &MacroUserIdStr<'static>,+ source: Option<ImportSource>,+ status: Option<ImportStatus>,+ ) -> Result<Vec<ImportEntity>> {+ self.repo.list(user, source, status).await+ }+}++impl<R, S, C> ImportFinalizer for ImportServiceImpl<R, S, C>+where+ R: ImportRepo + Clone,+ S: McpServerStore,+ C: EntityCreator,+{+ #[tracing::instrument(skip(self, user, content_markdown), err)]+ async fn finalize_document(+ &self,+ user: &MacroUserIdStr<'static>,+ import_id: Uuid,+ name: &str,+ content_markdown: &str,+ ) -> Result<ImportEntity> {+ let row = self+ .repo+ .get(user, import_id)+ .await?+ .ok_or_else(|| ImportError::Other(anyhow::anyhow!("no import row {import_id}")))?;+ if row.status != ImportStatus::Importing {+ return Err(ImportError::Other(anyhow::anyhow!(+ "import row {import_id} is {}, not importing",+ row.status.as_ref()+ )));+ }++ let entity_id = match row.source {+ ImportSource::Linear => {+ // Agent-finalized Linear rows still carry staged metadata —+ // apply the same property mapping as the deterministic path.+ let properties = serde_json::from_value::<LinearIssueMeta>(row.metadata.clone())+ .map(|meta| linear_task_properties(&meta))+ .unwrap_or_default();+ self.creator+ .create_task(user, name, content_markdown, &properties)+ .await+ }+ ImportSource::Notion => {+ self.creator+ .create_markdown_doc(user, name, content_markdown)+ .await+ }+ ImportSource::Slack => {+ return Err(ImportError::Other(anyhow::anyhow!(+ "slack channels are not finalized as documents"+ )));+ }+ }+ .map_err(ImportError::Other)?;++ let updated = self+ .repo+ .mark_imported(user, import_id, &entity_id, row.source.entity_type(), None)+ .await?+ .ok_or_else(|| {+ ImportError::Other(anyhow::anyhow!(+ "import row {import_id} changed status mid-finalize"+ ))+ })?;+ self.notify(user).await;+ Ok(updated)+ }+}++/// The mangled name of the connector's `notion-fetch` tool, whatever the+/// user named their server when connecting it (`mcp__<server>__notion-fetch`).+fn notion_fetch_tool_name(mcp_tools: &McpToolSet) -> Option<String> {+ ToolSet::<()>::request_schemas(mcp_tools)?+ .into_iter()+ .map(|schema| schema.name)+ .find(|name| name.ends_with("__notion-fetch"))+}++/// Split a `notion-fetch` result into `(title, body)`. The tool returns a+/// JSON document (`{id, title, text, url, …}`) as text; a result that isn't+/// that shape is treated as the body itself.+fn parse_notion_fetch_text(text: &str) -> (Option<String>, String) {+ if let Ok(serde_json::Value::Object(map)) = serde_json::from_str::<serde_json::Value>(text)+ && let Some(body) = map.get("text").and_then(|v| v.as_str())+ {+ let title = map+ .get("title")+ .and_then(|v| v.as_str())+ .map(str::to_string);+ return (title, body.to_string());+ }+ (None, text.to_string())+}++/// Map a Linear workflow status name onto Macro's task status label.+/// Returns `None` for anything unrecognized (the raw label then stays in+/// the task body's footer instead).+pub fn map_linear_status(status: &str) -> Option<&'static str> {+ match status.trim().to_ascii_lowercase().as_str() {+ "backlog" | "todo" | "to do" | "triage" | "unstarted" | "not started" | "planned" => {+ Some("Not Started")+ }+ "in progress" | "started" | "doing" => Some("In Progress"),+ "in review" | "review" | "code review" => Some("In Review"),+ "done" | "completed" | "closed" | "merged" => Some("Completed"),+ "canceled" | "cancelled" | "duplicate" | "won't do" | "wont do" => Some("Canceled"),+ _ => None,+ }+}++/// Map a Linear priority label onto Macro's task priority label. `None` for+/// unrecognized labels and for Linear's explicit "No priority".+pub fn map_linear_priority(priority: &str) -> Option<&'static str> {+ match priority.trim().to_ascii_lowercase().as_str() {+ "urgent" => Some("Urgent"),+ "high" => Some("High"),+ "medium" | "normal" => Some("Medium"),+ "low" => Some("Low"),+ _ => None,+ }+}++/// The system properties an imported Linear issue should carry, normalized+/// to Macro's vocabulary. Unmappable status/priority labels are dropped+/// here and surface in the body footer instead.+pub fn linear_task_properties(meta: &LinearIssueMeta) -> ImportedTaskProperties {+ ImportedTaskProperties {+ status: meta+ .status+ .as_deref()+ .and_then(map_linear_status)+ .map(String::from),+ priority: meta+ .priority+ .as_deref()+ .and_then(map_linear_priority)+ .map(String::from),+ due_date: meta.due_date.clone(),+ assignee_email: meta.assignee_email.clone(),+ }+}++/// Compose the task-document name and body for one Linear issue, from its+/// staged metadata alone. Status/priority appear in the footer only when+/// they could NOT be mapped onto real task properties — mapped values live+/// on the task itself and would be noise here.+pub fn linear_task_content(meta: &LinearIssueMeta) -> (String, String) {+ let name = match meta.identifier.as_deref() {+ Some(identifier) => format!("{identifier} {}", meta.title),+ None => meta.title.clone(),+ };+ let unmapped_status = meta+ .status+ .as_deref()+ .filter(|s| map_linear_status(s).is_none());+ let unmapped_priority = meta+ .priority+ .as_deref()+ .filter(|p| map_linear_priority(p).is_none());+ let footer = [+ unmapped_status.map(|s| format!("Status: {s}")),+ unmapped_priority.map(|p| format!("Priority: {p}")),+ Some(match meta.url.as_deref() {+ Some(url) => format!("Imported from [Linear]({url})"),+ None => "Imported from Linear".to_string(),+ }),+ ]+ .into_iter()+ .flatten()+ .collect::<Vec<_>>()+ .join(" · ");+ let markdown = [+ meta.description+ .as_deref()+ .map(str::trim)+ .filter(|s| !s.is_empty()),+ Some(&*format!("---\n{footer}")),+ ]+ .into_iter()+ .flatten()+ .collect::<Vec<_>>()+ .join("\n\n");+ (name, markdown)+}++/// Aborts the wrapped task when dropped — ties a background task (e.g. the+/// import heartbeat) to the lifetime of the scope that spawned it, whatever+/// path that scope exits through.+struct AbortOnDrop(tokio::task::JoinHandle<()>);++impl Drop for AbortOnDrop {+ fn drop(&mut self) {+ self.0.abort();+ }+}++/// A toolset combining an in-process collection with the user's connector+/// MCP tools: native tools win by name, everything else routes to MCP.+struct NativePlusMcp<Context> {+ native: ai_toolset::AsyncToolCollection<Context>,+ native_names: HashSet<String>,+ mcp: Arc<McpToolSet>,+}++impl<Context: Send + Sync + 'static> NativePlusMcp<Context> {+ fn new(native: ai_toolset::AsyncToolCollection<Context>, mcp: Arc<McpToolSet>) -> Self {+ let native_names = native+ .request_schemas()+ .unwrap_or_default()+ .into_iter()+ .map(|schema| schema.name)+ .collect();+ Self {+ native,+ native_names,+ mcp,+ }+ }+}++impl<Context> ToolSet<Context> for NativePlusMcp<Context>+where+ Context: Clone + Send + Sync + 'static,+{+ fn try_tool_call<'a>(+ &'a self,+ context: Context,+ request_context: RequestContext,+ tool_name: &'a str,+ json: &'a serde_json::Value,+ ) -> Pin<+ Box<+ dyn Future<Output = std::result::Result<ToolResult<serde_json::Value>, ToolSetError>>+ + 'a+ + Send,+ >,+ > {+ if self.native_names.contains(tool_name) {+ self.native+ .try_tool_call(context, request_context, tool_name, json)+ } else {+ self.mcp+ .try_tool_call(context, request_context, tool_name, json)+ }+ }++ fn request_schemas(&self) -> Option<Vec<ai_toolset::RequestSchema>> {+ let mut schemas = self.native.request_schemas().unwrap_or_default();+ schemas.extend(ToolSet::<Context>::request_schemas(&*self.mcp).unwrap_or_default());+ (!schemas.is_empty()).then_some(schemas)+ }+}diff --git a/crates/import/src/domain/service/prompts.rs b/crates/import/src/domain/service/prompts.rsnew file mode 100644index 0000000000..e4d7fcec7e--- /dev/null+++ b/crates/import/src/domain/service/prompts.rs@@ -0,0 +1,107 @@+//! Prompts for the gather and import agent sessions.+//!+//! Gather sessions stage candidates through the `CreateImportEntity` tool —+//! there is no structured output to parse; the tool IS the output channel.+//! Import sessions (Notion only — Linear tasks and Slack channels are+//! composed deterministically from staged metadata) fetch page content and+//! land it through `FinalizeImport`.++use crate::domain::models::{ImportEntity, ImportSource, metadata_label};++/// System prompt for a gather session over one connector.+pub fn gather_system(source: ImportSource) -> String {+ let (what, foreign_id_rule, metadata_hint) = match source {+ ImportSource::Linear => (+ "the user's most relevant recent Linear issues",+ "the Linear issue identifier (e.g. `ENG-142`); fall back to the issue URL only when \+ no identifier exists",+ "identifier, title, description (short markdown), status, priority, assignee, \+ assignee_email, due_date (ISO date, when set), url",+ ),+ ImportSource::Notion => (+ "the user's most recently updated, substantive Notion pages",+ "the page URL (it is normalized to the stable page id server-side)",+ "title, url, summary (one line). Do NOT fetch or include page content — content is \+ fetched later, only for pages the user accepts",+ ),+ ImportSource::Slack => (+ "the Slack channels the user is most active in",+ "the Slack channel id (e.g. `C0123456789`); fall back to the channel name",+ "name (without the leading #), channel_id, purpose, participants (name + email when \+ available)",+ ),+ };+ format!(+ "You are discovering {what} so they can be imported into Macro, the user's new \+ workspace.\n\+ \n\+ Use the connected tools to find 8-15 strong candidates: recently active, substantive, \+ and clearly relevant to the user's own work. Prefer one or two broad searches/list \+ calls over many narrow ones. Always pass a non-empty query and only the parameters a \+ tool's schema requires; if a call fails validation, fix the arguments and retry once.\n\+ \n\+ For EACH candidate, call `CreateImportEntity` once with:\n\+ - `foreign_id`: {foreign_id_rule}\n\+ - `metadata`: {metadata_hint}\n\+ \n\+ The tool response tells you when an item was already imported by the user or a \+ teammate, or previously declined — do not re-stage those, just move on.\n\+ \n\+ When you are done staging, reply with one short plain-text sentence summarizing what \+ you staged. Do not output JSON."+ )+}++/// User message opening a gather session.+pub fn gather_prompt(source: ImportSource) -> &'static str {+ match source {+ ImportSource::Linear => {+ "Find my most relevant recent Linear issues and stage them for import."+ }+ ImportSource::Notion => {+ "Find my most recently updated Notion pages worth bringing over and stage them for \+ import."+ }+ ImportSource::Slack => {+ "Find the Slack channels I'm most active in and stage them for import."+ }+ }+}++/// System prompt for the Notion import session.+pub const NOTION_IMPORT_SYSTEM: &str = "You are importing Notion pages the user accepted into \+ Macro, their new workspace.\n\+ \n\+ Work through the listed pages ONE AT A TIME, in order. For each page:\n\+ 1. Fetch its content with the connected Notion tools (by URL or page id).\n\+ 2. Convert it to clean markdown. Preserve headings, lists, tables, and links; drop Notion \+ artifacts that don't translate.\n\+ 3. Append a final line: `[Original in Notion](<page url>)` when a URL is known.\n\+ 4. Immediately call `FinalizeImport` with the page's `import_id`, a `name` (the page \+ title), and the markdown as `content_markdown` — BEFORE fetching the next page. The user \+ is watching items land live; never batch the finalize calls up for the end.\n\+ \n\+ If a page's content cannot be fetched, still call `FinalizeImport` using the staged summary \+ plus the backlink as the body — a thin import beats a failed one.\n\+ \n\+ You MUST call `FinalizeImport` exactly once for every listed page. When all pages are \+ finalized, reply with one short sentence.";++/// User message for the Notion import session: the accepted rows.+pub fn notion_import_prompt(rows: &[ImportEntity]) -> String {+ let items: Vec<serde_json::Value> = rows+ .iter()+ .map(|row| {+ serde_json::json!({+ "import_id": row.id,+ "title": metadata_label(row.source, &row.metadata),+ "url": row.metadata.get("url").and_then(|v| v.as_str()),+ "summary": row.metadata.get("summary").and_then(|v| v.as_str()),+ })+ })+ .collect();+ format!(+ "Import these Notion pages:\n{}",+ serde_json::to_string_pretty(&items).unwrap_or_else(|_| "[]".to_string())+ )+}diff --git a/crates/import/src/domain/service/test.rs b/crates/import/src/domain/service/test.rsnew file mode 100644index 0000000000..7f497dbab7--- /dev/null+++ b/crates/import/src/domain/service/test.rs@@ -0,0 +1,121 @@+use super::*;++fn meta(+ identifier: Option<&str>,+ description: Option<&str>,+ status: Option<&str>,+ priority: Option<&str>,+ url: Option<&str>,+) -> LinearIssueMeta {+ LinearIssueMeta {+ identifier: identifier.map(String::from),+ title: "Fix the flux capacitor".into(),+ description: description.map(String::from),+ status: status.map(String::from),+ priority: priority.map(String::from),+ assignee: None,+ assignee_email: None,+ due_date: None,+ url: url.map(String::from),+ }+}++#[test]+fn linear_task_content_keeps_mapped_labels_out_of_the_footer() {+ // "In Progress" and "Urgent" both map onto real task properties, so the+ // footer carries only the provenance link.+ let (name, markdown) = linear_task_content(&meta(+ Some("ENG-142"),+ Some("It drifts.\n"),+ Some("In Progress"),+ Some("Urgent"),+ Some("https://linear.app/acme/issue/ENG-142"),+ ));+ assert_eq!(name, "ENG-142 Fix the flux capacitor");+ assert_eq!(+ markdown,+ "It drifts.\n\n---\nImported from [Linear](https://linear.app/acme/issue/ENG-142)"+ );+}++#[test]+fn linear_task_content_keeps_unmapped_labels_in_the_footer() {+ let (_, markdown) = linear_task_content(&meta(+ None,+ None,+ Some("Blocked on vendor"),+ Some("P0"),+ None,+ ));+ assert_eq!(+ markdown,+ "---\nStatus: Blocked on vendor · Priority: P0 · Imported from Linear"+ );+}++#[test]+fn linear_task_content_degrades_without_optional_fields() {+ let (name, markdown) = linear_task_content(&meta(None, None, None, None, None));+ assert_eq!(name, "Fix the flux capacitor");+ assert_eq!(markdown, "---\nImported from Linear");+}++#[test]+fn linear_status_and_priority_map_onto_macro_labels() {+ assert_eq!(map_linear_status("Backlog"), Some("Not Started"));+ assert_eq!(map_linear_status("Todo"), Some("Not Started"));+ assert_eq!(map_linear_status("In Progress"), Some("In Progress"));+ assert_eq!(map_linear_status("In Review"), Some("In Review"));+ assert_eq!(map_linear_status("Done"), Some("Completed"));+ assert_eq!(map_linear_status("Cancelled"), Some("Canceled"));+ assert_eq!(map_linear_status("Duplicate"), Some("Canceled"));+ assert_eq!(map_linear_status("Blocked on vendor"), None);++ assert_eq!(map_linear_priority("Urgent"), Some("Urgent"));+ assert_eq!(map_linear_priority("medium"), Some("Medium"));+ assert_eq!(map_linear_priority("No priority"), None);+}++#[test]+fn linear_task_properties_normalize_and_carry_through() {+ let mut input = meta(None, None, Some("Todo"), Some("High"), None);+ input.due_date = Some("2026-08-01".into());+ input.assignee_email = Some("[email protected]".into());+ let properties = linear_task_properties(&input);+ assert_eq!(properties.status.as_deref(), Some("Not Started"));+ assert_eq!(properties.priority.as_deref(), Some("High"));+ assert_eq!(properties.due_date.as_deref(), Some("2026-08-01"));+ assert_eq!(properties.assignee_email.as_deref(), Some("[email protected]"));++ // Unmappable labels drop out of the properties (they stay in the body).+ let odd = linear_task_properties(&meta(None, None, Some("Blocked"), Some("P0"), None));+ assert_eq!(odd.status, None);+ assert_eq!(odd.priority, None);+}++#[test]+fn notion_import_turns_scale_with_pages_and_cap() {+ assert_eq!(notion_import_max_turns(1), 8);+ assert_eq!(notion_import_max_turns(10), 26);+ assert_eq!(notion_import_max_turns(100), 40);+}++#[test]+fn notion_fetch_text_parses_the_fetch_document_shape() {+ let (title, body) = parse_notion_fetch_text(+ r##"{"id":"abc","title":"Roadmap H2","text":"# Roadmap\ncontent","url":"https://notion.so/x"}"##,+ );+ assert_eq!(title.as_deref(), Some("Roadmap H2"));+ assert_eq!(body, "# Roadmap\ncontent");++ // Anything else is the body itself.+ let (title, body) = parse_notion_fetch_text("# Plain markdown\nno wrapper");+ assert_eq!(title, None);+ assert_eq!(body, "# Plain markdown\nno wrapper");++ // JSON without a text field falls through to raw.+ let raw = r#"{"unexpected":"shape"}"#;+ let (title, body) = parse_notion_fetch_text(raw);+ assert_eq!(title, None);+ assert_eq!(body, raw);+}diff --git a/crates/import/src/inbound/axum_router.rs b/crates/import/src/inbound/axum_router.rsnew file mode 100644index 0000000000..c570a4bf71--- /dev/null+++ b/crates/import/src/inbound/axum_router.rs@@ -0,0 +1,199 @@+//! HTTP surface for the import pipeline.++use crate::domain::models::{ImportSource, ImportState};+use crate::domain::ports::ImportError;+use crate::domain::service::{ImportService, RunImportOutcome};+use axum::{+ Json, Router,+ extract::{FromRef, Path, State},+ http::StatusCode,+ response::{IntoResponse, Response},+ routing::{get, post},+};+use macro_authorization::{+ MacroAuthorizationExtractor, MacroAuthorizationService, MacroAuthorizationState,+};+use serde::Deserialize;+use std::str::FromStr;+use std::sync::Arc;+use utoipa::ToSchema;+use uuid::Uuid;++/// Body for accepting/declining staged imports.+#[derive(Debug, Deserialize, ToSchema)]+pub struct RunImportRequest {+ /// Staged rows to import.+ #[serde(default)]+ pub import_ids: Vec<Uuid>,+ /// Staged rows to discard.+ #[serde(default)]+ pub discard_ids: Vec<Uuid>,+}++/// Router state: the import service plus the authorization state the+/// request extractors authenticate against (auth happens in the extractor,+/// not in host-layered middleware).+pub struct ImportRouterState<T, Auth> {+ /// The import service implementation.+ pub service: Arc<T>,+ /// The authorization state used by the request extractors.+ pub authorization_state: MacroAuthorizationState<Auth>,+}++// Manual Clone impl so T doesn't need to be Clone (it's behind Arc).+impl<T, Auth> Clone for ImportRouterState<T, Auth> {+ fn clone(&self) -> Self {+ Self {+ service: self.service.clone(),+ authorization_state: self.authorization_state.clone(),+ }+ }+}++impl<T, Auth> FromRef<ImportRouterState<T, Auth>> for Arc<T> {+ fn from_ref(state: &ImportRouterState<T, Auth>) -> Self {+ state.service.clone()+ }+}++impl<T, Auth> FromRef<ImportRouterState<T, Auth>> for MacroAuthorizationState<Auth> {+ fn from_ref(state: &ImportRouterState<T, Auth>) -> Self {+ state.authorization_state.clone()+ }+}++/// Build the import router.+pub fn import_router<T, Auth, S>(state: ImportRouterState<T, Auth>) -> Router<S>+where+ T: ImportService,+ Auth: MacroAuthorizationService,+ S: Send + Sync + Clone + 'static,+{+ Router::new()+ .route("/import/state", get(get_state_handler::<T, Auth>))+ .route("/import/run", post(run_import_handler::<T, Auth>))+ .route(+ "/import/runs/{source}/retry",+ post(retry_gather_handler::<T, Auth>),+ )+ .route(+ "/import/runs/{source}/dismiss",+ post(dismiss_run_handler::<T, Auth>),+ )+ .with_state(state)+}++fn error_response(e: ImportError) -> Response {+ tracing::error!(error = ?e, "import request failed");+ StatusCode::INTERNAL_SERVER_ERROR.into_response()+}++fn unknown_source_response(source: &str) -> Response {+ (+ StatusCode::BAD_REQUEST,+ format!("unknown import source: {source}"),+ )+ .into_response()+}++/// Get the authenticated user's import state: gather runs plus visible+/// ledger rows.+#[utoipa::path(+ get,+ path = "/import/state",+ responses(+ (status = 200, description = "Current import state", body = ImportState),+ (status = 500, description = "Internal server error"),+ ),+ tag = "import"+)]+#[tracing::instrument(skip(service, user), fields(user_id = %user.macro_user_id))]+pub async fn get_state_handler<T: ImportService, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth>,+) -> Response {+ match service.state(user.macro_user_id).await {+ Ok(state) => Json(state).into_response(),+ Err(e) => error_response(e),+ }+}++/// Accept staged imports (starts import jobs) and/or discard staged rows.+#[utoipa::path(+ post,+ path = "/import/run",+ request_body = RunImportRequest,+ responses(+ (status = 200, description = "Import run outcome", body = RunImportOutcome),+ (status = 500, description = "Internal server error"),+ ),+ tag = "import"+)]+#[tracing::instrument(skip(service, user, body), fields(user_id = %user.macro_user_id))]+pub async fn run_import_handler<T: ImportService, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth>,+ Json(body): Json<RunImportRequest>,+) -> Response {+ match service+ .run_import(user.macro_user_id, body.import_ids, body.discard_ids)+ .await+ {+ Ok(outcome) => Json(outcome).into_response(),+ Err(e) => error_response(e),+ }+}++/// Restart a failed (or dismissed) gather run for one source.+#[utoipa::path(+ post,+ path = "/import/runs/{source}/retry",+ params(("source" = String, Path, description = "Import source")),+ responses(+ (status = 204, description = "Retry accepted (idempotent)"),+ (status = 400, description = "Unknown import source"),+ (status = 500, description = "Internal server error"),+ ),+ tag = "import"+)]+#[tracing::instrument(skip(service, user), fields(user_id = %user.macro_user_id))]+pub async fn retry_gather_handler<T: ImportService, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth>,+ Path(source): Path<String>,+) -> Response {+ let Ok(source) = ImportSource::from_str(&source) else {+ return unknown_source_response(&source);+ };+ match service.retry_gather(user.macro_user_id, source).await {+ Ok(_) => StatusCode::NO_CONTENT.into_response(),+ Err(e) => error_response(e),+ }+}++/// Dismiss one source's import section.+#[utoipa::path(+ post,+ path = "/import/runs/{source}/dismiss",+ params(("source" = String, Path, description = "Import source")),+ responses(+ (status = 204, description = "Dismissed"),+ (status = 400, description = "Unknown import source"),+ (status = 500, description = "Internal server error"),+ ),+ tag = "import"+)]+#[tracing::instrument(skip(service, user), fields(user_id = %user.macro_user_id))]+pub async fn dismiss_run_handler<T: ImportService, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth>,+ Path(source): Path<String>,+) -> Response {+ let Ok(source) = ImportSource::from_str(&source) else {+ return unknown_source_response(&source);+ };+ match service.dismiss_run(user.macro_user_id, source).await {+ Ok(()) => StatusCode::NO_CONTENT.into_response(),+ Err(e) => error_response(e),+ }+}diff --git a/crates/import/src/inbound/mod.rs b/crates/import/src/inbound/mod.rsnew file mode 100644index 0000000000..4201193893--- /dev/null+++ b/crates/import/src/inbound/mod.rs@@ -0,0 +1,4 @@+//! Inbound adapters: the AI toolset and the HTTP router.++pub mod axum_router;+pub mod toolset;diff --git a/crates/import/src/inbound/toolset.rs b/crates/import/src/inbound/toolset.rsnew file mode 100644index 0000000000..66b9af731e--- /dev/null+++ b/crates/import/src/inbound/toolset.rs@@ -0,0 +1,558 @@+//! AI tools over the import ledger.+//!+//! One tool surface serves three callers, differing only in the+//! [`ToolPolicy`] baked into the context:+//! - chat sessions get all three tools, staging as `initiator = chat`;+//! - gather jobs get only `CreateImportEntity`, locked to one source and+//! `initiator = onboarding`, staging only;+//! - Notion import jobs get only `FinalizeImport`.+//!+//! The policy lives server-side in the tool context — the model can never+//! spoof another user, source, or initiator.++use crate::domain::models::{ImportEntity, ImportSource, ImportStatus, Initiator, metadata_label};+use crate::domain::ports::ImportError;+use crate::domain::service::{DiscardOutcome, ImportFinalizer, ImportStager, StageOutcome};+use ai_toolset::{+ AsyncTool, AsyncToolCollection, RequestContext, ServiceContext, ToolCallError, ToolResult,+};+use async_trait::async_trait;+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};+use std::sync::Arc;+use uuid::Uuid;++#[cfg(test)]+mod test;++/// What a given toolset instance is allowed to do; baked in server-side.+#[derive(Debug, Clone, Copy)]+pub struct ToolPolicy {+ /// The initiator recorded on rows staged through this context.+ pub initiator: Initiator,+ /// When set, staging is locked to this source (gather jobs).+ pub forced_source: Option<ImportSource>,+ /// Whether `status = imported` writes are allowed (chat agents that+ /// already created the entity).+ pub allow_imported: bool,+}++impl ToolPolicy {+ /// Policy for interactive chat sessions.+ pub fn chat() -> Self {+ Self {+ initiator: Initiator::Chat,+ forced_source: None,+ allow_imported: true,+ }+ }++ /// Policy for an onboarding gather job over one connector.+ pub fn gather(source: ImportSource) -> Self {+ Self {+ initiator: Initiator::Onboarding,+ forced_source: Some(source),+ allow_imported: false,+ }+ }++ /// Policy for a Notion import job (only `FinalizeImport` is registered,+ /// which ignores staging policy).+ pub fn import_job() -> Self {+ Self {+ initiator: Initiator::Onboarding,+ forced_source: Some(ImportSource::Notion),+ allow_imported: false,+ }+ }+}++/// Service context for import AI tools.+///+/// The service is optional because the tool schemas ship with the shared+/// chat toolset while only hosts that can build the import service (DB,+/// MCP credentials, gateway) actually wire it. Where it is absent, calls+/// fail with a clear "not available here" error instead of not existing.+pub struct ImportToolContext<T> {+ /// The import service, where the host wired one.+ pub service: Option<Arc<T>>,+ /// What this toolset instance may do.+ pub policy: ToolPolicy,+}++impl<T> Clone for ImportToolContext<T> {+ fn clone(&self) -> Self {+ Self {+ service: self.service.clone(),+ policy: self.policy,+ }+ }+}++impl<T> ImportToolContext<T> {+ /// A chat-policy context backed by a real import service.+ pub fn wired(service: Arc<T>) -> Self {+ Self {+ service: Some(service),+ policy: ToolPolicy::chat(),+ }+ }++ /// A chat-policy context with no import service: every tool call fails+ /// with a clear error. For hosts that share the toolset but cannot+ /// build the service.+ pub fn unwired() -> Self {+ Self {+ service: None,+ policy: ToolPolicy::chat(),+ }+ }++ fn require_service(&self) -> ToolResult<&Arc<T>> {+ self.service.as_ref().ok_or_else(|| ToolCallError {+ description: "Import tracking is not available in this context.".to_string(),+ internal_error: anyhow::anyhow!("import service not wired into this host"),+ })+ }+}++/// The import toolset for interactive chat.+pub fn import_toolset<T: ImportStager>() -> AsyncToolCollection<ImportToolContext<T>> {+ AsyncToolCollection::new()+ .add_tool::<CreateImportEntity, ImportToolContext<T>>()+ .add_tool::<DeleteImportEntity, ImportToolContext<T>>()+ .add_tool::<ListImportEntities, ImportToolContext<T>>()+}++/// The staging-only toolset for gather jobs.+pub fn gather_toolset<T: ImportStager>() -> AsyncToolCollection<ImportToolContext<T>> {+ AsyncToolCollection::new().add_tool::<CreateImportEntity, ImportToolContext<T>>()+}++/// The finalize-only toolset for Notion import jobs.+pub fn notion_import_toolset<T: ImportFinalizer>() -> AsyncToolCollection<ImportToolContext<T>> {+ AsyncToolCollection::new().add_tool::<FinalizeImport, ImportToolContext<T>>()+}++fn tool_error(description: String, e: impl Into<anyhow::Error>) -> ToolCallError {+ ToolCallError {+ internal_error: e.into(),+ description,+ }+}++fn import_error(context: &str, e: ImportError) -> ToolCallError {+ ToolCallError {+ description: format!("{context}: {e}"),+ internal_error: e.into(),+ }+}++/// Compact row view returned by import tools.+#[derive(Debug, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct ImportEntityView {+ /// Ledger row id (use with DeleteImportEntity).+ pub id: Uuid,+ /// The external system the item comes from.+ pub source: ImportSource,+ /// Stable id in the source system.+ pub foreign_id: String,+ /// Row status.+ pub status: ImportStatus,+ /// A human label from the metadata (title / channel name).+ pub label: String,+ /// The Macro entity it became, when imported.+ pub entity_id: Option<String>,+ /// The Macro entity type, when imported.+ pub entity_type: Option<String>,+ /// Whether the row belongs to a teammate (team-imported), not the user.+ pub imported_by_teammate: bool,+}++impl ImportEntityView {+ fn of(row: &ImportEntity, requesting_user: &str) -> Self {+ Self {+ id: row.id,+ source: row.source,+ foreign_id: row.foreign_id.clone(),+ status: row.status,+ label: metadata_label(row.source, &row.metadata),+ entity_id: row.entity_id.clone(),+ entity_type: row.entity_type.clone(),+ imported_by_teammate: row.user_id != requesting_user,+ }+ }+}++/// Status values accepted by [`CreateImportEntity`].+#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]+#[serde(rename_all = "snake_case")]+pub enum CreateImportStatus {+ /// Track an item as a candidate the user has not accepted yet.+ Staged,+ /// Record an item you have ALREADY created a Macro entity for.+ Imported,+}++/// Track an external item in the import ledger.+#[derive(Debug, Clone, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+#[schemars(+ title = "CreateImportEntity",+ description = "Track an external item (Linear issue, Notion page, Slack channel) in the import ledger. Use status `staged` to propose an item for import BEFORE creating anything; use status `imported` (with entityId) only to record a Macro entity you already created from the item. The response tells you when the item was already imported by the user or a teammate — in that case do NOT create a duplicate; point the user at the existing entity instead."+)]+pub struct CreateImportEntity {+ /// The external system the item comes from.+ #[schemars(description = "The external system the item comes from.")]+ pub source: ImportSource,++ /// Stable id of the item in the source system.+ #[schemars(+ description = "Stable id of the item in the source system: the Linear issue identifier (e.g. `ENG-142`), the Notion page URL or id, or the Slack channel id (e.g. `C0123456789`, falling back to the channel name)."+ )]+ pub foreign_id: String,++ /// Whether this is a staged candidate or an already-created import.+ #[schemars(+ description = "`staged` to propose the item for import; `imported` to record a Macro entity you already created from it (requires entityId)."+ )]+ pub status: CreateImportStatus,++ /// Per-source metadata describing the item.+ #[schemars(+ description = "Metadata describing the item. Linear: {identifier, title, description?, status?, priority?, assignee?, assignee_email?, url?}. Notion: {title, url?, summary?} — never include page content. Slack: {name, channel_id?, purpose?, participants?: [{name, email?}]}."+ )]+ pub metadata: serde_json::Value,++ /// The Macro entity id, when `status` is `imported`.+ #[schemars(+ description = "The id of the Macro entity you created, required when status is `imported`. The entity type is fixed by source: linear → task, notion → md (document), slack → channel."+ )]+ #[serde(default)]+ pub entity_id: Option<String>,+}++/// Response from tracking an item.+#[derive(Debug, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct CreateImportEntityResponse {+ /// What happened: `staged`, `recorded_imported`, `already_imported`,+ /// `already_imported_by_teammate`, `previously_declined`, or+ /// `import_in_progress`.+ pub outcome: String,+ /// The ledger row backing the outcome.+ pub entity: ImportEntityView,+ /// What to do with this outcome.+ pub message: String,+}++#[async_trait]+impl<T: ImportStager> AsyncTool<ImportToolContext<T>> for CreateImportEntity {+ type Output = CreateImportEntityResponse;++ #[tracing::instrument(skip_all, fields(user_id = %request_context.user_id, source = self.source.as_ref(), foreign_id = %self.foreign_id), err)]+ async fn call(+ &self,+ service_context: ServiceContext<ImportToolContext<T>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ let user = request_context.user_id.clone();+ let policy = service_context.policy;++ if let Some(forced) = policy.forced_source+ && self.source != forced+ {+ return Err(tool_error(+ format!(+ "This session only stages {} items; got {}.",+ forced.as_ref(),+ self.source.as_ref()+ ),+ anyhow::anyhow!("forced-source policy violation"),+ ));+ }++ match self.status {+ CreateImportStatus::Staged => {+ let outcome = service_context+ .require_service()?+ .stage(+ &user,+ policy.initiator,+ self.source,+ &self.foreign_id,+ self.metadata.clone(),+ )+ .await+ .map_err(|e| import_error("Failed to stage item", e))?;+ Ok(stage_response(outcome, user.as_ref()))+ }+ CreateImportStatus::Imported => {+ if !policy.allow_imported {+ return Err(tool_error(+ "This session can only stage candidates (status `staged`); it cannot \+ record imported entities."+ .to_string(),+ anyhow::anyhow!("imported-status policy violation"),+ ));+ }+ let Some(entity_id) = self+ .entity_id+ .as_deref()+ .map(str::trim)+ .filter(|s| !s.is_empty())+ else {+ return Err(tool_error(+ "entityId is required when status is `imported` — pass the id of the \+ Macro entity you created."+ .to_string(),+ anyhow::anyhow!("missing entity_id"),+ ));+ };+ let row = service_context+ .require_service()?+ .record_imported(+ &user,+ policy.initiator,+ self.source,+ &self.foreign_id,+ self.metadata.clone(),+ entity_id,+ )+ .await+ .map_err(|e| import_error("Failed to record import", e))?;+ Ok(CreateImportEntityResponse {+ outcome: "recorded_imported".into(),+ message: format!(+ "Recorded {} as imported.",+ metadata_label(row.source, &row.metadata)+ ),+ entity: ImportEntityView::of(&row, user.as_ref()),+ })+ }+ }+ }+}++fn stage_response(outcome: StageOutcome, user: &str) -> CreateImportEntityResponse {+ match outcome {+ StageOutcome::Staged(row) => CreateImportEntityResponse {+ outcome: "staged".into(),+ message: format!(+ "Staged {} for import. It will be imported when the user accepts it.",+ metadata_label(row.source, &row.metadata)+ ),+ entity: ImportEntityView::of(&row, user),+ },+ StageOutcome::AlreadyImported {+ entity,+ by_teammate,+ } => CreateImportEntityResponse {+ outcome: if by_teammate {+ "already_imported_by_teammate".into()+ } else {+ "already_imported".into()+ },+ message: format!(+ "{} was already imported{} as {} `{}`. Do NOT create a duplicate — tell the \+ user it already exists and reference the existing entity.",+ metadata_label(entity.source, &entity.metadata),+ if by_teammate {+ " by a teammate into the team"+ } else {+ ""+ },+ entity.entity_type.as_deref().unwrap_or("entity"),+ entity.entity_id.as_deref().unwrap_or("?"),+ ),+ entity: ImportEntityView::of(&entity, user),+ },+ StageOutcome::PreviouslyDiscarded(row) => CreateImportEntityResponse {+ outcome: "previously_declined".into(),+ message: format!(+ "The user previously declined {}; it was not re-staged. Skip it.",+ metadata_label(row.source, &row.metadata)+ ),+ entity: ImportEntityView::of(&row, user),+ },+ StageOutcome::ImportInProgress(row) => CreateImportEntityResponse {+ outcome: "import_in_progress".into(),+ message: "An import is already running for this item; leave it alone.".into(),+ entity: ImportEntityView::of(&row, user),+ },+ }+}++/// Decline a staged import candidate.+#[derive(Debug, Clone, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+#[schemars(+ title = "DeleteImportEntity",+ description = "Decline a staged import candidate on the user's behalf. The item is remembered as declined so it won't be proposed again; only the user's own staged items can be declined."+)]+pub struct DeleteImportEntity {+ /// The ledger row id to decline.+ #[schemars(+ description = "The import entity id to decline (from CreateImportEntity or ListImportEntities)."+ )]+ pub id: Uuid,+}++/// Response from declining a candidate.+#[derive(Debug, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct DeleteImportEntityResponse {+ /// Whether the row is now discarded.+ pub discarded: bool,+ /// What happened.+ pub message: String,+}++#[async_trait]+impl<T: ImportStager> AsyncTool<ImportToolContext<T>> for DeleteImportEntity {+ type Output = DeleteImportEntityResponse;++ #[tracing::instrument(skip_all, fields(user_id = %request_context.user_id, id = %self.id), err)]+ async fn call(+ &self,+ service_context: ServiceContext<ImportToolContext<T>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ let outcome = service_context+ .require_service()?+ .discard_entity(&request_context.user_id.clone(), self.id)+ .await+ .map_err(|e| import_error("Failed to decline item", e))?;+ match outcome {+ DiscardOutcome::Discarded => Ok(DeleteImportEntityResponse {+ discarded: true,+ message: "Declined. It won't be proposed again.".into(),+ }),+ DiscardOutcome::NotFound => Err(tool_error(+ format!("No import entity `{}` belongs to this user.", self.id),+ anyhow::anyhow!("not found"),+ )),+ DiscardOutcome::NotDiscardable(status) => Err(tool_error(+ format!(+ "Import entity `{}` is `{}`; only staged items can be declined.",+ self.id,+ status.as_ref()+ ),+ anyhow::anyhow!("not discardable"),+ )),+ }+ }+}++/// List import ledger rows.+#[derive(Debug, Clone, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+#[schemars(+ title = "ListImportEntities",+ description = "List the user's import ledger: staged candidates, in-flight imports, imported items (including ones teammates imported into the team), and declined items. Use this to check what already exists before proposing or creating imports."+)]+pub struct ListImportEntities {+ /// Filter to one source.+ #[schemars(description = "Filter to one source system.")]+ #[serde(default)]+ pub source: Option<ImportSource>,++ /// Filter to one status.+ #[schemars(+ description = "Filter to one status: staged, importing, imported, or discarded. Omit for all."+ )]+ #[serde(default)]+ pub status: Option<ImportStatus>,+}++/// Response from listing the ledger.+#[derive(Debug, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct ListImportEntitiesResponse {+ /// The visible rows, newest first.+ pub entities: Vec<ImportEntityView>,+}++#[async_trait]+impl<T: ImportStager> AsyncTool<ImportToolContext<T>> for ListImportEntities {+ type Output = ListImportEntitiesResponse;++ #[tracing::instrument(skip_all, fields(user_id = %request_context.user_id), err)]+ async fn call(+ &self,+ service_context: ServiceContext<ImportToolContext<T>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ let user = request_context.user_id.clone();+ let rows = service_context+ .require_service()?+ .list_entities(&user, self.source, self.status)+ .await+ .map_err(|e| import_error("Failed to list import entities", e))?;+ Ok(ListImportEntitiesResponse {+ entities: rows+ .iter()+ .map(|row| ImportEntityView::of(row, user.as_ref()))+ .collect(),+ })+ }+}++/// Finalize one importing row as a Macro document.+#[derive(Debug, Clone, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+#[schemars(+ title = "FinalizeImport",+ description = "Create the Macro document for one accepted import item and mark it imported. Call exactly once per item you were asked to import."+)]+pub struct FinalizeImport {+ /// The importing ledger row to finalize.+ #[schemars(description = "The import_id of the item, as listed in the instructions.")]+ pub import_id: Uuid,++ /// The document name.+ #[schemars(description = "The name for the created document (usually the item's title).")]+ pub name: String,++ /// The document body.+ #[schemars(description = "The full markdown body for the created document.")]+ pub content_markdown: String,+}++/// Response from finalizing an item.+#[derive(Debug, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct FinalizeImportResponse {+ /// The Macro entity id that now exists.+ pub entity_id: String,+ /// The Macro entity type.+ pub entity_type: String,+}++#[async_trait]+impl<T: ImportFinalizer> AsyncTool<ImportToolContext<T>> for FinalizeImport {+ type Output = FinalizeImportResponse;++ #[tracing::instrument(skip_all, fields(user_id = %request_context.user_id, import_id = %self.import_id), err)]+ async fn call(+ &self,+ service_context: ServiceContext<ImportToolContext<T>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ let row = service_context+ .require_service()?+ .finalize_document(+ &request_context.user_id.clone(),+ self.import_id,+ self.name.trim(),+ &self.content_markdown,+ )+ .await+ .map_err(|e| import_error("Failed to finalize import", e))?;+ Ok(FinalizeImportResponse {+ entity_id: row.entity_id.unwrap_or_default(),+ entity_type: row.entity_type.unwrap_or_default(),+ })+ }+}diff --git a/crates/import/src/inbound/toolset/test.rs b/crates/import/src/inbound/toolset/test.rsnew file mode 100644index 0000000000..b6f3db8b09--- /dev/null+++ b/crates/import/src/inbound/toolset/test.rs@@ -0,0 +1,211 @@+use super::*;+use crate::domain::models::ImportEntity;+use crate::domain::ports::Result;+use crate::domain::service::{DiscardOutcome, ImportStager, StageOutcome};+use macro_user_id::user_id::MacroUserIdStr;+use std::sync::Mutex;++fn user() -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from("macro|[email protected]".to_string()).expect("valid test user id")+}++fn row(source: ImportSource, status: ImportStatus) -> ImportEntity {+ ImportEntity {+ id: Uuid::nil(),+ user_id: user().as_ref().to_string(),+ team_id: None,+ source,+ foreign_id: "ENG-1".into(),+ status,+ initiator: Initiator::Onboarding,+ metadata: serde_json::json!({"title": "T"}),+ entity_id: matches!(status, ImportStatus::Imported).then(|| "doc-1".to_string()),+ entity_type: matches!(status, ImportStatus::Imported).then(|| "task".to_string()),+ last_error: None,+ created_at: chrono::Utc::now(),+ updated_at: chrono::Utc::now(),+ }+}++/// Stager that returns canned outcomes and records what reached it.+#[derive(Default)]+struct MockStager {+ staged_calls: Mutex<Vec<(Initiator, ImportSource, String)>>,+ stage_outcome: Option<StageOutcome>,+}++impl ImportStager for MockStager {+ async fn stage(+ &self,+ _user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ source: ImportSource,+ foreign_id: &str,+ _metadata: serde_json::Value,+ ) -> Result<StageOutcome> {+ self.staged_calls+ .lock()+ .unwrap()+ .push((initiator, source, foreign_id.to_string()));+ Ok(self+ .stage_outcome+ .clone()+ .unwrap_or(StageOutcome::Staged(row(source, ImportStatus::Staged))))+ }++ async fn record_imported(+ &self,+ _user: &MacroUserIdStr<'static>,+ _initiator: Initiator,+ source: ImportSource,+ _foreign_id: &str,+ _metadata: serde_json::Value,+ _entity_id: &str,+ ) -> Result<ImportEntity> {+ Ok(row(source, ImportStatus::Imported))+ }++ async fn discard_entity(+ &self,+ _user: &MacroUserIdStr<'static>,+ _id: Uuid,+ ) -> Result<DiscardOutcome> {+ Ok(DiscardOutcome::NotDiscardable(ImportStatus::Imported))+ }++ async fn list_entities(+ &self,+ _user: &MacroUserIdStr<'static>,+ _source: Option<ImportSource>,+ _status: Option<ImportStatus>,+ ) -> Result<Vec<ImportEntity>> {+ Ok(vec![row(ImportSource::Linear, ImportStatus::Imported)])+ }+}++fn contexts(+ policy: ToolPolicy,+) -> (+ ServiceContext<ImportToolContext<MockStager>>,+ RequestContext,+ Arc<MockStager>,+) {+ let stager = Arc::new(MockStager::default());+ let context = ImportToolContext {+ service: Some(stager.clone()),+ policy,+ };+ (ServiceContext(context), RequestContext::new(user()), stager)+}++#[tokio::test]+async fn gather_policy_locks_source_and_initiator() {+ let (service_context, request_context, stager) =+ contexts(ToolPolicy::gather(ImportSource::Linear));++ // Wrong source is refused before touching the service.+ let err = CreateImportEntity {+ source: ImportSource::Notion,+ foreign_id: "https://notion.so/x".into(),+ status: CreateImportStatus::Staged,+ metadata: serde_json::json!({"title": "T"}),+ entity_id: None,+ }+ .call(service_context.clone(), request_context.clone())+ .await+ .expect_err("forced source must reject other sources");+ assert!(err.description.contains("only stages linear"), "{err:?}");+ assert!(stager.staged_calls.lock().unwrap().is_empty());++ // status=imported is refused under a staging-only policy.+ let err = CreateImportEntity {+ source: ImportSource::Linear,+ foreign_id: "ENG-1".into(),+ status: CreateImportStatus::Imported,+ metadata: serde_json::json!({"title": "T"}),+ entity_id: Some("doc-1".into()),+ }+ .call(service_context.clone(), request_context.clone())+ .await+ .expect_err("staging-only policy must reject imported writes");+ assert!(err.description.contains("only stage"), "{err:?}");++ // The right source stages with the locked initiator.+ let response = CreateImportEntity {+ source: ImportSource::Linear,+ foreign_id: "ENG-1".into(),+ status: CreateImportStatus::Staged,+ metadata: serde_json::json!({"title": "T"}),+ entity_id: None,+ }+ .call(service_context, request_context)+ .await+ .expect("staging succeeds");+ assert_eq!(response.outcome, "staged");+ assert_eq!(+ stager.staged_calls.lock().unwrap().as_slice(),+ &[(+ Initiator::Onboarding,+ ImportSource::Linear,+ "ENG-1".to_string()+ )]+ );+}++#[tokio::test]+async fn chat_policy_requires_entity_id_for_imported_writes() {+ let (service_context, request_context, _) = contexts(ToolPolicy::chat());+ let err = CreateImportEntity {+ source: ImportSource::Linear,+ foreign_id: "ENG-1".into(),+ status: CreateImportStatus::Imported,+ metadata: serde_json::json!({"title": "T"}),+ entity_id: Some(" ".into()),+ }+ .call(service_context, request_context)+ .await+ .expect_err("imported without entity id must fail");+ assert!(err.description.contains("entityId is required"), "{err:?}");+}++#[tokio::test]+async fn teammate_dedup_response_tells_the_agent_not_to_duplicate() {+ let teammate_row = ImportEntity {+ user_id: "macro|[email protected]".into(),+ ..row(ImportSource::Linear, ImportStatus::Imported)+ };+ let stager = Arc::new(MockStager {+ stage_outcome: Some(StageOutcome::AlreadyImported {+ entity: teammate_row,+ by_teammate: true,+ }),+ ..Default::default()+ });+ let context = ImportToolContext {+ service: Some(stager),+ policy: ToolPolicy::chat(),+ };+ let response = CreateImportEntity {+ source: ImportSource::Linear,+ foreign_id: "ENG-1".into(),+ status: CreateImportStatus::Staged,+ metadata: serde_json::json!({"title": "T"}),+ entity_id: None,+ }+ .call(ServiceContext(context), RequestContext::new(user()))+ .await+ .expect("dedup is a successful outcome, not an error");+ assert_eq!(response.outcome, "already_imported_by_teammate");+ assert!(response.message.contains("Do NOT create a duplicate"));+ assert!(response.entity.imported_by_teammate);+}++#[tokio::test]+async fn delete_reports_non_discardable_status() {+ let (service_context, request_context, _) = contexts(ToolPolicy::chat());+ let err = DeleteImportEntity { id: Uuid::nil() }+ .call(service_context, request_context)+ .await+ .expect_err("non-staged rows cannot be declined");+ assert!(err.description.contains("only staged items"), "{err:?}");+}diff --git a/crates/import/src/lib.rs b/crates/import/src/lib.rsnew file mode 100644index 0000000000..b8906c8f64--- /dev/null+++ b/crates/import/src/lib.rs@@ -0,0 +1,20 @@+//! The import pipeline: tracking external items (Linear issues, Notion+//! pages, Slack channels) as they are discovered, accepted, and copied into+//! Macro entities.+//!+//! Not to be confused with the `foreign_entity` crate: foreign entities are+//! live references to items that STAY in the external system (e.g. a synced+//! GitHub PR). An [`domain::models::ImportEntity`] is a ledger row for an+//! item being COPIED into Macro — it tracks the item from `staged`+//! (discovered, not yet accepted) through `importing` to `imported` (a Macro+//! entity now exists), or `discarded` (the user declined it).+//!+//! The crate is host-agnostic: onboarding (`/setup`) and AI chat both drive+//! the same primitives, distinguished only by the `initiator` recorded on+//! each row.++#![deny(missing_docs)]++pub mod domain;+pub mod inbound;+pub mod outbound;diff --git a/crates/import/src/outbound/gateway_notifier.rs b/crates/import/src/outbound/gateway_notifier.rsnew file mode 100644index 0000000000..76e17ee164--- /dev/null+++ b/crates/import/src/outbound/gateway_notifier.rs@@ -0,0 +1,32 @@+//! Connection-gateway implementation of the import service's notify hook.+//!+//! Every ledger/run mutation calls the service's [`ImportNotify`]; this+//! adapter pushes an `import_updated` message to the user's connected+//! clients so setup sections and chat surfaces update immediately instead+//! of on the next poll. Push failures only warn — polling is the backstop.++use crate::domain::service::ImportNotify;+use connection_gateway_client::ConnectionGatewayClient;+use std::sync::Arc;++/// The gateway message type pushed on import changes. The web client+/// invalidates its import state query when it sees this.+pub const IMPORT_UPDATED_MESSAGE_TYPE: &str = "import_updated";++/// Build an [`ImportNotify`] that pushes [`IMPORT_UPDATED_MESSAGE_TYPE`]+/// to the user's connections through `gateway`.+pub fn gateway_import_notify(gateway: Arc<ConnectionGatewayClient>) -> ImportNotify {+ Arc::new(move |user| {+ let gateway = gateway.clone();+ Box::pin(async move {+ let entity = model_entity::EntityType::User.with_entity_str(user.as_ref());+ let payload = serde_json::json!({ "type": IMPORT_UPDATED_MESSAGE_TYPE });+ if let Err(e) = gateway+ .send_message(entity, IMPORT_UPDATED_MESSAGE_TYPE.to_string(), payload)+ .await+ {+ tracing::warn!(error = ?e, "failed to push import update");+ }+ })+ })+}diff --git a/crates/import/src/outbound/mod.rs b/crates/import/src/outbound/mod.rsnew file mode 100644index 0000000000..e8c430d839--- /dev/null+++ b/crates/import/src/outbound/mod.rs@@ -0,0 +1,4 @@+//! Outbound adapters: Postgres persistence and the gateway notify hook.++pub mod gateway_notifier;+pub mod pg_import_repo;diff --git a/crates/import/src/outbound/pg_import_repo.rs b/crates/import/src/outbound/pg_import_repo.rsnew file mode 100644index 0000000000..962fc39de8--- /dev/null+++ b/crates/import/src/outbound/pg_import_repo.rs@@ -0,0 +1,584 @@+//! Postgres implementation of [`ImportRepo`].+//!+//! Sources, statuses, and initiators are stored as text and parsed back+//! through the domain enums; every status write is a CAS expressed in SQL,+//! reporting whether it happened via `RETURNING` / rows-affected.++use crate::domain::models::{+ ImportEntity, ImportRun, ImportSource, ImportStatus, Initiator, RunStatus,+};+use crate::domain::ports::{ImportError, ImportRepo, Result};+use chrono::{DateTime, Utc};+use macro_user_id::user_id::MacroUserIdStr;+use sqlx::PgPool;+use std::collections::HashSet;+use std::str::FromStr;+use uuid::Uuid;++/// Postgres-backed import repository (MacroDB).+#[derive(Clone)]+pub struct PgImportRepo {+ pool: PgPool,+}++impl PgImportRepo {+ /// Build the repository on the MacroDB pool.+ pub fn new(pool: PgPool) -> Self {+ Self { pool }+ }+}++struct ImportEntityDbRow {+ id: Uuid,+ user_id: String,+ team_id: Option<Uuid>,+ source: String,+ foreign_id: String,+ status: String,+ initiator: String,+ metadata: serde_json::Value,+ entity_id: Option<String>,+ entity_type: Option<String>,+ last_error: Option<String>,+ created_at: DateTime<Utc>,+ updated_at: DateTime<Utc>,+}++impl TryFrom<ImportEntityDbRow> for ImportEntity {+ type Error = ImportError;++ fn try_from(row: ImportEntityDbRow) -> Result<ImportEntity> {+ let source = ImportSource::from_str(&row.source)+ .map_err(|_| anyhow::anyhow!("unknown import source: {}", row.source))?;+ let status = ImportStatus::from_str(&row.status)+ .map_err(|_| anyhow::anyhow!("unknown import status: {}", row.status))?;+ let initiator = Initiator::from_str(&row.initiator)+ .map_err(|_| anyhow::anyhow!("unknown import initiator: {}", row.initiator))?;+ Ok(ImportEntity {+ id: row.id,+ user_id: row.user_id,+ team_id: row.team_id,+ source,+ foreign_id: row.foreign_id,+ status,+ initiator,+ metadata: row.metadata,+ entity_id: row.entity_id,+ entity_type: row.entity_type,+ last_error: row.last_error,+ created_at: row.created_at,+ updated_at: row.updated_at,+ })+ }+}++struct ImportRunDbRow {+ source: String,+ status: String,+ error: Option<String>,+ updated_at: DateTime<Utc>,+}++impl TryFrom<ImportRunDbRow> for ImportRun {+ type Error = ImportError;++ fn try_from(row: ImportRunDbRow) -> Result<ImportRun> {+ let source = ImportSource::from_str(&row.source)+ .map_err(|_| anyhow::anyhow!("unknown import source: {}", row.source))?;+ let status = RunStatus::from_str(&row.status)+ .map_err(|_| anyhow::anyhow!("unknown run status: {}", row.status))?;+ Ok(ImportRun {+ source,+ status,+ error: row.error,+ updated_at: row.updated_at,+ })+ }+}++fn run_status_strings(statuses: &[RunStatus]) -> Vec<String> {+ statuses.iter().map(|s| s.as_ref().to_string()).collect()+}++impl ImportRepo for PgImportRepo {+ #[tracing::instrument(skip(self), err)]+ async fn get_own_by_foreign_id(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ foreign_id: &str,+ ) -> Result<Option<ImportEntity>> {+ let row = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ SELECT id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ FROM import_entity+ WHERE user_id = $1 AND source = $2 AND foreign_id = $3+ "#,+ user.as_ref(),+ source.as_ref(),+ foreign_id,+ )+ .fetch_optional(&self.pool)+ .await?;+ row.map(ImportEntity::try_from).transpose()+ }++ #[tracing::instrument(skip(self), err)]+ async fn find_team_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ foreign_id: &str,+ ) -> Result<Option<ImportEntity>> {+ let row = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ SELECT id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ FROM import_entity+ WHERE source = $2 AND foreign_id = $3+ AND status = 'imported'+ AND user_id <> $1+ AND team_id IS NOT NULL+ AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1)+ LIMIT 1+ "#,+ user.as_ref(),+ source.as_ref(),+ foreign_id,+ )+ .fetch_optional(&self.pool)+ .await?;+ row.map(ImportEntity::try_from).transpose()+ }++ #[tracing::instrument(skip(self, metadata), err)]+ async fn upsert_staged(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ initiator: Initiator,+ foreign_id: &str,+ metadata: &serde_json::Value,+ ) -> Result<Option<ImportEntity>> {+ let row = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata)+ VALUES ($1, $2, $3, 'staged', $4, $5)+ ON CONFLICT (user_id, source, foreign_id) DO UPDATE+ SET metadata = EXCLUDED.metadata, updated_at = NOW()+ WHERE import_entity.status = 'staged'+ RETURNING id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ "#,+ user.as_ref(),+ source.as_ref(),+ foreign_id,+ initiator.as_ref(),+ metadata,+ )+ .fetch_optional(&self.pool)+ .await?;+ row.map(ImportEntity::try_from).transpose()+ }++ #[tracing::instrument(skip(self, metadata), err)]+ async fn upsert_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ initiator: Initiator,+ foreign_id: &str,+ metadata: &serde_json::Value,+ entity_id: &str,+ entity_type: &str,+ team_id: Option<Uuid>,+ ) -> Result<ImportEntity> {+ let row = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ INSERT INTO import_entity (user_id, source, foreign_id, status, initiator, metadata,+ entity_id, entity_type, team_id)+ VALUES ($1, $2, $3, 'imported', $4, $5, $6, $7, $8)+ ON CONFLICT (user_id, source, foreign_id) DO UPDATE+ SET status = 'imported', metadata = EXCLUDED.metadata,+ entity_id = EXCLUDED.entity_id, entity_type = EXCLUDED.entity_type,+ team_id = EXCLUDED.team_id, last_error = NULL, updated_at = NOW()+ WHERE import_entity.status <> 'imported'+ RETURNING id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ "#,+ user.as_ref(),+ source.as_ref(),+ foreign_id,+ initiator.as_ref(),+ metadata,+ entity_id,+ entity_type,+ team_id,+ )+ .fetch_optional(&self.pool)+ .await?;+ match row {+ Some(row) => row.try_into(),+ // Already imported: first mapping wins; return the stored row.+ None => self+ .get_own_by_foreign_id(user, source, foreign_id)+ .await?+ .ok_or_else(|| ImportError::Other(anyhow::anyhow!("imported upsert vanished"))),+ }+ }++ #[tracing::instrument(skip(self), err)]+ async fn get(&self, user: &MacroUserIdStr<'static>, id: Uuid) -> Result<Option<ImportEntity>> {+ let row = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ SELECT id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ FROM import_entity+ WHERE user_id = $1 AND id = $2+ "#,+ user.as_ref(),+ id,+ )+ .fetch_optional(&self.pool)+ .await?;+ row.map(ImportEntity::try_from).transpose()+ }++ #[tracing::instrument(skip(self), err)]+ async fn list(+ &self,+ user: &MacroUserIdStr<'static>,+ source: Option<ImportSource>,+ status: Option<ImportStatus>,+ ) -> Result<Vec<ImportEntity>> {+ let rows = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ SELECT id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ FROM import_entity+ WHERE (+ user_id = $1+ OR (status = 'imported' AND team_id IS NOT NULL+ AND team_id = (SELECT team_id FROM team_user WHERE user_id = $1))+ )+ AND ($2::text IS NULL OR source = $2)+ AND ($3::text IS NULL OR status = $3)+ ORDER BY (user_id = $1) DESC, created_at DESC+ "#,+ user.as_ref(),+ source.map(|s| s.as_ref().to_string()) as Option<String>,+ status.map(|s| s.as_ref().to_string()) as Option<String>,+ )+ .fetch_all(&self.pool)+ .await?;++ // Own rows come first (see ORDER BY); drop teammate rows that+ // duplicate an own row on (source, foreign_id), then restore+ // newest-first order. Unparseable rows are skipped with a warning+ // rather than failing the whole list.+ let mut seen: HashSet<(String, String)> = HashSet::new();+ let mut entities: Vec<ImportEntity> = Vec::with_capacity(rows.len());+ for row in rows {+ let own = row.user_id == user.as_ref();+ match ImportEntity::try_from(row) {+ Ok(entity) => {+ let key = (+ entity.source.as_ref().to_string(),+ entity.foreign_id.clone(),+ );+ if own {+ seen.insert(key);+ entities.push(entity);+ } else if !seen.contains(&key) {+ entities.push(entity);+ }+ }+ Err(e) => {+ tracing::warn!(error = ?e, "skipping unparseable import_entity row");+ }+ }+ }+ entities.sort_by(|a, b| b.created_at.cmp(&a.created_at));+ Ok(entities)+ }++ #[tracing::instrument(skip(self, ids), fields(count = ids.len()), err)]+ async fn mark_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ ids: &[Uuid],+ ) -> Result<Vec<ImportEntity>> {+ if ids.is_empty() {+ return Ok(Vec::new());+ }+ let rows = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ UPDATE import_entity+ SET status = 'importing', last_error = NULL, updated_at = NOW()+ WHERE user_id = $1 AND id = ANY($2::uuid[]) AND status = 'staged'+ RETURNING id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ "#,+ user.as_ref(),+ ids,+ )+ .fetch_all(&self.pool)+ .await?;+ rows.into_iter().map(ImportEntity::try_from).collect()+ }++ #[tracing::instrument(skip(self), err)]+ async fn mark_imported(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ entity_id: &str,+ entity_type: &str,+ team_id: Option<Uuid>,+ ) -> Result<Option<ImportEntity>> {+ let row = sqlx::query_as!(+ ImportEntityDbRow,+ r#"+ UPDATE import_entity+ SET status = 'imported', entity_id = $3, entity_type = $4, team_id = $5,+ last_error = NULL, updated_at = NOW()+ WHERE user_id = $1 AND id = $2 AND status = 'importing'+ RETURNING id, user_id, team_id, source, foreign_id, status, initiator,+ metadata, entity_id, entity_type, last_error, created_at, updated_at+ "#,+ user.as_ref(),+ id,+ entity_id,+ entity_type,+ team_id,+ )+ .fetch_optional(&self.pool)+ .await?;+ row.map(ImportEntity::try_from).transpose()+ }++ #[tracing::instrument(skip(self), err)]+ async fn mark_import_failed(+ &self,+ user: &MacroUserIdStr<'static>,+ id: Uuid,+ error: &str,+ ) -> Result<bool> {+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET status = 'staged', last_error = $3, updated_at = NOW()+ WHERE user_id = $1 AND id = $2 AND status = 'importing'+ "#,+ user.as_ref(),+ id,+ error,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), err)]+ async fn fail_stale_importing(+ &self,+ user: &MacroUserIdStr<'static>,+ older_than_secs: i64,+ ) -> Result<u64> {+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET status = 'staged',+ last_error = 'the import was interrupted — select it again to retry',+ updated_at = NOW()+ WHERE user_id = $1 AND status = 'importing'+ AND updated_at < NOW() - ($2::double precision * interval '1 second')+ "#,+ user.as_ref(),+ older_than_secs as f64,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected())+ }++ #[tracing::instrument(skip(self, ids), fields(rows = ids.len()), err)]+ async fn touch_importing(&self, user: &MacroUserIdStr<'static>, ids: &[Uuid]) -> Result<u64> {+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET updated_at = NOW()+ WHERE user_id = $1 AND id = ANY($2) AND status = 'importing'+ "#,+ user.as_ref(),+ ids,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected())+ }++ #[tracing::instrument(skip(self), err)]+ async fn discard(&self, user: &MacroUserIdStr<'static>, id: Uuid) -> Result<bool> {+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET status = 'discarded', updated_at = NOW()+ WHERE user_id = $1 AND id = $2 AND status = 'staged'+ "#,+ user.as_ref(),+ id,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), err)]+ async fn discard_staged_by_initiator(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> Result<u64> {+ let result = sqlx::query!(+ r#"+ UPDATE import_entity+ SET status = 'discarded', updated_at = NOW()+ WHERE user_id = $1 AND initiator = $2 AND status = 'staged'+ "#,+ user.as_ref(),+ initiator.as_ref(),+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected())+ }++ #[tracing::instrument(skip(self), err)]+ async fn delete_staged_by_initiator(+ &self,+ user: &MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> Result<u64> {+ let result = sqlx::query!(+ r#"+ DELETE FROM import_entity+ WHERE user_id = $1 AND initiator = $2 AND status = 'staged'+ "#,+ user.as_ref(),+ initiator.as_ref(),+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected())+ }++ #[tracing::instrument(skip(self), err)]+ async fn user_team_id(&self, user: &MacroUserIdStr<'static>) -> Result<Option<Uuid>> {+ let team_id = sqlx::query_scalar!(+ r#"SELECT team_id FROM team_user WHERE user_id = $1"#,+ user.as_ref(),+ )+ .fetch_optional(&self.pool)+ .await?;+ Ok(team_id)+ }++ #[tracing::instrument(skip(self), err)]+ async fn list_runs(&self, user: &MacroUserIdStr<'static>) -> Result<Vec<ImportRun>> {+ let rows = sqlx::query_as!(+ ImportRunDbRow,+ r#"+ SELECT source, status, error, updated_at+ FROM import_run WHERE user_id = $1+ ORDER BY source+ "#,+ user.as_ref(),+ )+ .fetch_all(&self.pool)+ .await?;+ rows.into_iter().map(ImportRun::try_from).collect()+ }++ #[tracing::instrument(skip(self), err)]+ async fn start_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ ) -> Result<bool> {+ let from = run_status_strings(from);+ let result = sqlx::query!(+ r#"+ INSERT INTO import_run (user_id, source, status)+ VALUES ($1, $2, 'running')+ ON CONFLICT (user_id, source) DO UPDATE+ SET status = 'running', error = NULL, updated_at = NOW()+ WHERE import_run.status = ANY($3::text[])+ "#,+ user.as_ref(),+ source.as_ref(),+ &from,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), err)]+ async fn finish_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ to: RunStatus,+ error: Option<&str>,+ ) -> Result<bool> {+ let result = sqlx::query!(+ r#"+ UPDATE import_run+ SET status = $3, error = $4, updated_at = NOW()+ WHERE user_id = $1 AND source = $2 AND status = 'running'+ "#,+ user.as_ref(),+ source.as_ref(),+ to.as_ref(),+ error,+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }++ #[tracing::instrument(skip(self), err)]+ async fn transition_run(+ &self,+ user: &MacroUserIdStr<'static>,+ source: ImportSource,+ from: &[RunStatus],+ to: RunStatus,+ ) -> Result<bool> {+ let from = run_status_strings(from);+ let result = sqlx::query!(+ r#"+ UPDATE import_run+ SET status = $4, updated_at = NOW()+ WHERE user_id = $1 AND source = $2 AND status = ANY($3::text[])+ "#,+ user.as_ref(),+ source.as_ref(),+ &from,+ to.as_ref(),+ )+ .execute(&self.pool)+ .await?;+ Ok(result.rows_affected() > 0)+ }+}diff --git a/crates/macro_db_client/migrations/20260720221050_import_entities.sql b/crates/macro_db_client/migrations/20260720221050_import_entities.sqlnew file mode 100644index 0000000000..687d75ed59--- /dev/null+++ b/crates/macro_db_client/migrations/20260720221050_import_entities.sql@@ -0,0 +1,44 @@+-- Import pipeline ledger: rows are staged by gather jobs / chat, flipped to+-- imported by the import job. Unlike foreign_entity (live references to items+-- that stay external), import_entity tracks items being COPIED into Macro.++CREATE TABLE import_entity (+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),+ user_id TEXT NOT NULL,+ -- Set when the created entity is shared with a team (e.g. channels);+ -- imported rows with a team_id dedupe team-wide.+ team_id UUID REFERENCES team (id) ON DELETE SET NULL,+ source TEXT NOT NULL, -- 'linear' | 'notion' | 'slack'+ foreign_id TEXT NOT NULL, -- stable id in the source system+ status TEXT NOT NULL DEFAULT 'staged', -- 'staged' | 'importing' | 'imported' | 'discarded'+ initiator TEXT NOT NULL, -- 'onboarding' | 'chat' (provenance, not visibility)+ metadata JSONB NOT NULL DEFAULT '{}',+ entity_id TEXT, -- Macro entity once imported+ entity_type TEXT, -- 'task' | 'md' | 'channel'+ last_error TEXT, -- failure detail from the last import attempt+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),+ UNIQUE (user_id, source, foreign_id),+ CHECK (status <> 'imported' OR entity_id IS NOT NULL),+ CHECK (source IN ('linear', 'notion', 'slack')),+ CHECK (status IN ('staged', 'importing', 'imported', 'discarded')),+ CHECK (initiator IN ('onboarding', 'chat'))+);++-- Team-wide dedup lookup: "has anyone on my team already imported this?"+CREATE INDEX import_entity_team_imported_idx+ ON import_entity (team_id, source, foreign_id)+ WHERE team_id IS NOT NULL AND status = 'imported';++-- Gather-job state per (user, connector): drives the shimmer / failed / retry+-- states in the UI and CASes so only one gather runs per connection.+CREATE TABLE import_run (+ user_id TEXT NOT NULL,+ source TEXT NOT NULL,+ status TEXT NOT NULL, -- 'running' | 'ready' | 'failed' | 'dismissed'+ error TEXT,+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),+ PRIMARY KEY (user_id, source),+ CHECK (source IN ('linear', 'notion', 'slack')),+ CHECK (status IN ('running', 'ready', 'failed', 'dismissed'))+);diff --git a/crates/macro_db_client/migrations/20260720224652_user_onboarding.sql b/crates/macro_db_client/migrations/20260720224652_user_onboarding.sqlnew file mode 100644index 0000000000..78b082edd1--- /dev/null+++ b/crates/macro_db_client/migrations/20260720224652_user_onboarding.sql@@ -0,0 +1,13 @@+-- Onboarding flow state: one row per user. The import pipeline+-- (import_entity / import_run) carries all connector work; this row only+-- tracks whether the user is still in the /setup flow.++CREATE TABLE user_onboarding (+ user_id TEXT PRIMARY KEY,+ status TEXT NOT NULL DEFAULT 'active', -- 'active' | 'completed'+ skipped BOOLEAN NOT NULL DEFAULT FALSE,+ started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),+ completed_at TIMESTAMPTZ,+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),+ CHECK (status IN ('active', 'completed'))+);diff --git a/crates/mcp_client/src/domain/ports.rs b/crates/mcp_client/src/domain/ports.rsindex 6b9001bdd1..7ef80fed87 100644--- a/crates/mcp_client/src/domain/ports.rs+++ b/crates/mcp_client/src/domain/ports.rs@@ -1,4 +1,4 @@-use super::models::{MacroUserIdStr, McpServer, McpServerRecord, StoredCredentials};+use super::models::{MacroUserIdStr, McpServer, McpServerRecord};/// Port for persisting MCP server records, keyed by user.pub trait McpServerStore: Send + Sync + 'static {@@ -91,11 +91,11 @@ pub trait OAuthClient: Send + Sync + 'static {/// Complete the OAuth flow using the `code` and `state` returned by the/// authorization server's redirect.///- /// Exchanges the authorization code for tokens and returns the resulting- /// credentials ready to be persisted.+ /// Exchanges the authorization code for tokens, persists them, and+ /// returns the saved server record (user, url, credentials).fn exchange_authorization_code(&self,code: &str,state: &str,- ) -> impl Future<Output = anyhow::Result<StoredCredentials>> + Send;+ ) -> impl Future<Output = anyhow::Result<McpServerRecord>> + Send;}diff --git a/crates/mcp_client/src/domain/service/oauth.rs b/crates/mcp_client/src/domain/service/oauth.rsindex 20bdcd8477..8303191e74 100644--- a/crates/mcp_client/src/domain/service/oauth.rs+++ b/crates/mcp_client/src/domain/service/oauth.rs@@ -1,5 +1,5 @@use crate::domain::models::MCP_CLIENT_NAME;-use crate::domain::models::{MacroUserIdStr, McpServerRecord, StoredCredentials};+use crate::domain::models::{MacroUserIdStr, McpServerRecord};use crate::domain::ports::{McpServerStore, OAuthClient, OAuthStateStore, PendingAuth};#[cfg(feature = "providers")]use crate::domain::provider_registry::PreRegisteredProviders;@@ -153,7 +153,7 @@ where&self,code: &str,state: &str,- ) -> anyhow::Result<StoredCredentials> {+ ) -> anyhow::Result<McpServerRecord> {let pending = self.state_store.take(state)@@ -212,7 +212,7 @@ where.map_err(anyhow::Error::from)?;tracing::info!("OAuth flow completed successfully");- Ok(credentials)+ Ok(record)}}diff --git a/crates/mcp_client/src/inbound/axum_router.rs b/crates/mcp_client/src/inbound/axum_router.rsindex f6fd4d7dec..82d98d56ed 100644--- a/crates/mcp_client/src/inbound/axum_router.rs+++ b/crates/mcp_client/src/inbound/axum_router.rs@@ -17,11 +17,19 @@ use serde::{Deserialize, Serialize};use std::sync::Arc;use utoipa::{IntoParams, ToSchema};+/// Hook invoked after an OAuth flow completes and the credentials are saved.+/// Hosts use this to react to a connection the moment it exists (e.g. start+/// import gather jobs); implementations must be quick or spawn.+pub type McpAuthCompletedHook = Arc<+ dyn Fn(McpServerRecord) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync,+>;+/// Shared state for the MCP router.pub struct McpRouterState<S, O, Auth> {store: Arc<S>,oauth: Arc<O>,authorization_state: MacroAuthorizationState<Auth>,+ on_auth_completed: Option<McpAuthCompletedHook>,}impl<S, O, Auth> Clone for McpRouterState<S, O, Auth> {@@ -30,6 +38,7 @@ impl<S, O, Auth> Clone for McpRouterState<S, O, Auth> {store: self.store.clone(),oauth: self.oauth.clone(),authorization_state: self.authorization_state.clone(),+ on_auth_completed: self.on_auth_completed.clone(),}}}@@ -53,9 +62,17 @@ wherestore: Arc::new(store),oauth: Arc::new(oauth),authorization_state,+ on_auth_completed: None,}}+ /// Invoke `hook` whenever an OAuth flow completes (see+ /// [`McpAuthCompletedHook`]).+ pub fn with_auth_completed_hook(mut self, hook: McpAuthCompletedHook) -> Self {+ self.on_auth_completed = Some(hook);+ self+ }+/// Access the underlying server store.pub fn store(&self) -> Arc<S> {self.store.clone()@@ -428,10 +445,16 @@ whereO: OAuthClient,Auth: MacroAuthorizationService,{- state+ let record = state.oauth.exchange_authorization_code(¶ms.code, ¶ms.state).await?;+ // Let the host react to the brand-new connection (e.g. kick off import+ // gather jobs) before the user even returns to their original tab.+ if let Some(hook) = &state.on_auth_completed {+ hook(record).await;+ }+Ok("Authorization successful. You can close this tab.".to_string())}diff --git a/crates/memory/src/context.rs b/crates/memory/src/context.rsindex 90e6bc9661..5c1e7620a2 100644--- a/crates/memory/src/context.rs+++ b/crates/memory/src/context.rs@@ -1,5 +1,6 @@use ai_tools::{- NoOpConnectionService, NoOpSnsEndpointManager, ToolNotificationQueue, ToolServiceContext,+ NoOpConnectionService, NoOpSnsEndpointManager, ToolImportToolContext, ToolNotificationQueue,+ ToolServiceContext,};use channels::{domain::list_service::ChannelListServiceImpl, outbound::pg_channels_repo::PgChannelsRepo,@@ -235,6 +236,7 @@ pub async fn build_tool_service_context(email_tool_context,call_tool_context,notification_tool_context,+ import_tool_context: ToolImportToolContext::unwired(),chat_tool_context,channel_tool_context: ai_tools::build_channel_tool_context(pool.clone(),diff --git a/crates/onboarding/Cargo.toml b/crates/onboarding/Cargo.tomlnew file mode 100644index 0000000000..8919f15ab8--- /dev/null+++ b/crates/onboarding/Cargo.toml@@ -0,0 +1,26 @@+[package]+edition = "2024"+name = "onboarding"+publish = false+version = "0.1.0"++[dependencies]+anyhow = { workspace = true }+axum = { workspace = true }+chrono = { workspace = true }+import = { path = "../import" }+macro_authorization = { path = "../macro_authorization", default-features = false, features = [+ "axum",+] }+macro_user_id = { path = "../macro_user_id" }+mcp_client = { path = "../mcp_client" }+serde = { workspace = true }+serde_json = { workspace = true }+sqlx = { workspace = true }+strum = { workspace = true }+thiserror = { workspace = true }+tokio = { workspace = true }+tracing = { workspace = true }+utoipa = { workspace = true }+uuid = { workspace = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }diff --git a/crates/onboarding/src/domain/mod.rs b/crates/onboarding/src/domain/mod.rsnew file mode 100644index 0000000000..7c83f9fd3e--- /dev/null+++ b/crates/onboarding/src/domain/mod.rs@@ -0,0 +1,5 @@+//! Domain layer: models, ports, and the onboarding service.++pub mod models;+pub mod ports;+pub mod service;diff --git a/crates/onboarding/src/domain/models.rs b/crates/onboarding/src/domain/models.rsnew file mode 100644index 0000000000..07ca125184--- /dev/null+++ b/crates/onboarding/src/domain/models.rs@@ -0,0 +1,61 @@+//! Pure data model for the onboarding flow.++use chrono::{DateTime, Utc};+use serde::{Deserialize, Serialize};+use utoipa::ToSchema;++/// Lifecycle of the onboarding flow itself.+#[derive(+ Debug,+ Clone,+ Copy,+ PartialEq,+ Eq,+ Serialize,+ Deserialize,+ ToSchema,+ strum::EnumString,+ strum::AsRefStr,+)]+#[serde(rename_all = "snake_case")]+#[strum(serialize_all = "snake_case")]+pub enum OnboardingStatus {+ /// The user is (or can be sent) through the onboarding flow.+ Active,+ /// The user finished or skipped onboarding; never show the flow again.+ Completed,+}++/// One user's onboarding row.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]+pub struct OnboardingRow {+ /// Where the flow is in its lifecycle.+ pub status: OnboardingStatus,+ /// Whether the user skipped rather than finished.+ pub skipped: bool,+ /// When the flow was first touched.+ pub started_at: DateTime<Utc>,+ /// When the flow completed, if it has.+ pub completed_at: Option<DateTime<Utc>>,+}++/// One of the user's MCP server connections, as the setup page sees it.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]+pub struct ConnectedServer {+ /// The server's display name.+ pub name: String,+ /// The server URL.+ pub url: String,+ /// Whether the user has completed OAuth against it.+ pub authenticated: bool,+}++/// The onboarding aggregate served to the setup page. Import runs and+/// staged entities are served separately by `GET /import/state`.+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]+pub struct OnboardingState {+ /// The user's onboarding row.+ pub row: OnboardingRow,+ /// The user's MCP server connections.+ pub connected_servers: Vec<ConnectedServer>,+}diff --git a/crates/onboarding/src/domain/ports.rs b/crates/onboarding/src/domain/ports.rsnew file mode 100644index 0000000000..4db4026e07--- /dev/null+++ b/crates/onboarding/src/domain/ports.rs@@ -0,0 +1,46 @@+//! Ports (traits) the onboarding domain depends on.++use super::models::OnboardingRow;+use macro_user_id::user_id::MacroUserIdStr;+use thiserror::Error;++/// Errors surfaced by the onboarding service and repository.+#[derive(Debug, Error)]+pub enum OnboardingError {+ /// Database failure.+ #[error(transparent)]+ Db(#[from] sqlx::Error),+ /// A failure in the import pipeline this flow drives.+ #[error(transparent)]+ Import(#[from] import::domain::ports::ImportError),+ /// Anything else.+ #[error(transparent)]+ Other(#[from] anyhow::Error),+}++/// Result alias for onboarding operations.+pub type Result<T> = std::result::Result<T, OnboardingError>;++/// Persistence for the onboarding row.+pub trait OnboardingRepo: Send + Sync + 'static {+ /// Fetch the user's onboarding row, creating an `active` one on first+ /// touch.+ fn ensure_row(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<OnboardingRow>> + Send;++ /// Fetch the user's onboarding row without creating one. `None` means+ /// the user never entered the flow.+ fn get_row(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<Option<OnboardingRow>>> + Send;++ /// Mark the flow completed (idempotent; keeps the earliest completion).+ fn complete(+ &self,+ user: &MacroUserIdStr<'static>,+ skipped: bool,+ ) -> impl Future<Output = Result<OnboardingRow>> + Send;+}diff --git a/crates/onboarding/src/domain/service.rs b/crates/onboarding/src/domain/service.rsnew file mode 100644index 0000000000..5ec8edefe1--- /dev/null+++ b/crates/onboarding/src/domain/service.rs@@ -0,0 +1,178 @@+//! The onboarding orchestrator: assembles the aggregate and keeps the+//! import pipeline moving while the flow is active.+//!+//! Reconciliation is level-triggered and idempotent: every tick lists the+//! user's MCP connections and asks the import service to start a gather for+//! each authenticated connector. `start_gather` is a CAS that only wins when+//! no run has ever started, so ticking on every read (and on the MCP+//! post-auth hook) is safe.++use super::models::{ConnectedServer, OnboardingRow, OnboardingState, OnboardingStatus};+use super::ports::{OnboardingError, OnboardingRepo, Result};+use import::domain::models::{ImportSource, Initiator};+use import::domain::service::ImportService;+use macro_user_id::user_id::MacroUserIdStr;+use mcp_client::domain::ports::McpServerStore;+use std::sync::Arc;++#[cfg(test)]+mod test;++/// The API the onboarding router (and host-service hooks) talk to.+pub trait OnboardingService: Send + Sync + 'static {+ /// The full aggregate for the user. While the flow is active this also+ /// starts any gather runs that are due, so reading the state is what+ /// keeps onboarding moving.+ fn get_state(+ &self,+ user: MacroUserIdStr<'static>,+ ) -> impl Future<Output = Result<OnboardingState>> + Send;++ /// An explicit reconcile tick, for host hooks that observed a real state+ /// change (e.g. an MCP OAuth completion). A no-op for users who never+ /// entered the flow or already finished it — connecting an MCP server+ /// outside onboarding must not start gathers.+ fn reconcile(&self, user: MacroUserIdStr<'static>) -> impl Future<Output = Result<()>> + Send;++ /// Finish the flow (`skipped` records how). Leftover onboarding-staged+ /// import candidates are deleted — never reviewed is not declined, so+ /// they stay re-stageable by later gathers or chat.+ fn complete(+ &self,+ user: MacroUserIdStr<'static>,+ skipped: bool,+ ) -> impl Future<Output = Result<OnboardingRow>> + Send;+}++/// Concrete orchestrator wiring the repo, the user's MCP servers, and the+/// import service together.+pub struct OnboardingServiceImpl<R, S, I> {+ repo: R,+ mcp_store: Arc<S>,+ import: Arc<I>,+}++impl<R, S, I> OnboardingServiceImpl<R, S, I> {+ /// Build the orchestrator.+ pub fn new(repo: R, mcp_store: Arc<S>, import: Arc<I>) -> Self {+ Self {+ repo,+ mcp_store,+ import,+ }+ }+}++impl<R, S, I> OnboardingServiceImpl<R, S, I>+where+ R: OnboardingRepo,+ S: McpServerStore,+ I: ImportService,+{+ async fn connected_servers(+ &self,+ user: &MacroUserIdStr<'static>,+ ) -> Result<Vec<ConnectedServer>> {+ let servers = self+ .mcp_store+ .list(user)+ .await+ .map_err(|e| OnboardingError::Other(anyhow::anyhow!("mcp store: {e:?}")))?;+ Ok(servers+ .into_iter()+ .map(|record| ConnectedServer {+ name: record.server_name,+ url: record.url,+ authenticated: record.credentials.is_some(),+ })+ .collect())+ }++ /// Start a gather run for every authenticated connector that never had+ /// one. A failing source must not block the others (or the read).+ async fn start_due_gathers(&self, user: &MacroUserIdStr<'static>, servers: &[ConnectedServer]) {+ for source in ImportSource::all() {+ let connected = servers+ .iter()+ .any(|server| server.authenticated && server.url == source.mcp_server_url());+ if !connected {+ continue;+ }+ if let Err(e) = self.import.start_gather(user.clone(), source).await {+ tracing::warn!(source = source.as_ref(), error = ?e, "failed to start gather run");+ }+ }+ }+}++impl<R, S, I> OnboardingService for OnboardingServiceImpl<R, S, I>+where+ R: OnboardingRepo,+ S: McpServerStore,+ I: ImportService,+{+ #[tracing::instrument(skip(self, user), err)]+ async fn get_state(&self, user: MacroUserIdStr<'static>) -> Result<OnboardingState> {+ let row = self.repo.ensure_row(&user).await?;+ let connected_servers = self.connected_servers(&user).await?;+ if row.status == OnboardingStatus::Active {+ self.start_due_gathers(&user, &connected_servers).await;+ }+ Ok(OnboardingState {+ row,+ connected_servers,+ })+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn reconcile(&self, user: MacroUserIdStr<'static>) -> Result<()> {+ // get_row, NOT ensure_row: this runs from the generic MCP post-auth+ // hook, and a user connecting a server outside onboarding must not+ // be pulled into the flow (or have gathers started).+ let Some(row) = self.repo.get_row(&user).await? else {+ return Ok(());+ };+ if row.status != OnboardingStatus::Active {+ // Self-heal the completion race: a reconcile (or a gather+ // session it started) can outlive complete()'s cleanup and+ // stage rows after it ran. Any later tick for a finished flow+ // sweeps those leftovers, best-effort.+ let _ = self+ .import+ .delete_staged_by_initiator(user, Initiator::Onboarding)+ .await+ .inspect_err(|e| {+ tracing::warn!(error = ?e, "failed to sweep post-completion staged rows");+ });+ return Ok(());+ }+ let servers = self.connected_servers(&user).await?;+ self.start_due_gathers(&user, &servers).await;+ Ok(())+ }++ #[tracing::instrument(skip(self, user), err)]+ async fn complete(+ &self,+ user: MacroUserIdStr<'static>,+ skipped: bool,+ ) -> Result<OnboardingRow> {+ let row = self.repo.complete(&user, skipped).await?;+ // Candidates the user never acted on are removed from the ledger,+ // not discarded: discard means "the user said no" and blocks+ // re-staging forever, which is wrong for items they never looked at+ // ("Set up later" skips the whole review). Explicit declines (a+ // section toggled off) were already discarded by run_import.+ // Chat-staged rows are untouched. Best-effort: the completion above+ // is durable, so a cleanup failure must not fail the call — the+ // next reconcile tick sweeps leftovers anyway.+ let _ = self+ .import+ .delete_staged_by_initiator(user, Initiator::Onboarding)+ .await+ .inspect_err(|e| {+ tracing::warn!(error = ?e, "failed to delete leftover onboarding-staged candidates");+ });+ Ok(row)+ }+}diff --git a/crates/onboarding/src/domain/service/test.rs b/crates/onboarding/src/domain/service/test.rsnew file mode 100644index 0000000000..d6ce282a1e--- /dev/null+++ b/crates/onboarding/src/domain/service/test.rs@@ -0,0 +1,274 @@+use super::*;+use import::domain::models::ImportState;+use import::domain::ports::Result as ImportResult;+use import::domain::service::RunImportOutcome;+use mcp_client::domain::models::{McpServerRecord, StoredCredentials};+use std::sync::Mutex;+use uuid::Uuid;++fn user() -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from("macro|[email protected]".to_string()).expect("valid test user id")+}++fn active_row() -> OnboardingRow {+ OnboardingRow {+ status: OnboardingStatus::Active,+ skipped: false,+ started_at: chrono::Utc::now(),+ completed_at: None,+ }+}++struct MockRepo {+ status: OnboardingStatus,+ /// Whether a row exists at all (get_row returns None when false).+ exists: bool,+ completions: Mutex<Vec<bool>>,+}++impl MockRepo {+ fn new(status: OnboardingStatus) -> Self {+ Self {+ status,+ exists: true,+ completions: Mutex::new(Vec::new()),+ }+ }++ fn missing() -> Self {+ Self {+ exists: false,+ ..Self::new(OnboardingStatus::Active)+ }+ }+}++impl OnboardingRepo for MockRepo {+ async fn ensure_row(&self, _user: &MacroUserIdStr<'static>) -> Result<OnboardingRow> {+ Ok(OnboardingRow {+ status: self.status,+ ..active_row()+ })+ }++ async fn get_row(&self, _user: &MacroUserIdStr<'static>) -> Result<Option<OnboardingRow>> {+ Ok(self.exists.then(|| OnboardingRow {+ status: self.status,+ ..active_row()+ }))+ }++ async fn complete(+ &self,+ _user: &MacroUserIdStr<'static>,+ skipped: bool,+ ) -> Result<OnboardingRow> {+ self.completions.lock().unwrap().push(skipped);+ Ok(OnboardingRow {+ status: OnboardingStatus::Completed,+ skipped,+ completed_at: Some(chrono::Utc::now()),+ ..active_row()+ })+ }+}++struct MockStore {+ records: Vec<McpServerRecord>,+}++impl McpServerStore for MockStore {+ type Err = ();++ async fn save(&self, _record: &McpServerRecord) -> std::result::Result<(), ()> {+ Ok(())+ }++ async fn load(+ &self,+ _user: &MacroUserIdStr<'static>,+ _url: &str,+ ) -> std::result::Result<Option<McpServerRecord>, ()> {+ Ok(None)+ }++ async fn delete(+ &self,+ _user: &MacroUserIdStr<'static>,+ _url: &str,+ ) -> std::result::Result<(), ()> {+ Ok(())+ }++ async fn list(+ &self,+ _user: &MacroUserIdStr<'static>,+ ) -> std::result::Result<Vec<McpServerRecord>, ()> {+ Ok(self.records.clone())+ }+}++#[derive(Default)]+struct MockImport {+ gathers: Mutex<Vec<ImportSource>>,+ discards: Mutex<Vec<Initiator>>,+ deletions: Mutex<Vec<Initiator>>,+}++impl ImportService for MockImport {+ async fn state(&self, _user: MacroUserIdStr<'static>) -> ImportResult<ImportState> {+ Ok(ImportState {+ runs: Vec::new(),+ entities: Vec::new(),+ })+ }++ async fn start_gather(+ &self,+ _user: MacroUserIdStr<'static>,+ source: ImportSource,+ ) -> ImportResult<bool> {+ self.gathers.lock().unwrap().push(source);+ Ok(true)+ }++ async fn retry_gather(+ &self,+ _user: MacroUserIdStr<'static>,+ _source: ImportSource,+ ) -> ImportResult<bool> {+ Ok(false)+ }++ async fn dismiss_run(+ &self,+ _user: MacroUserIdStr<'static>,+ _source: ImportSource,+ ) -> ImportResult<()> {+ Ok(())+ }++ async fn run_import(+ &self,+ _user: MacroUserIdStr<'static>,+ _import_ids: Vec<Uuid>,+ _discard_ids: Vec<Uuid>,+ ) -> ImportResult<RunImportOutcome> {+ Ok(RunImportOutcome {+ discarded: 0,+ importing: 0,+ })+ }++ async fn discard_staged_by_initiator(+ &self,+ _user: MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> ImportResult<u64> {+ self.discards.lock().unwrap().push(initiator);+ Ok(1)+ }++ async fn delete_staged_by_initiator(+ &self,+ _user: MacroUserIdStr<'static>,+ initiator: Initiator,+ ) -> ImportResult<u64> {+ self.deletions.lock().unwrap().push(initiator);+ Ok(1)+ }+}++fn record(url: &str, authenticated: bool) -> McpServerRecord {+ McpServerRecord {+ user_id: user(),+ url: url.to_string(),+ server_name: url.to_string(),+ credentials: authenticated+ .then(|| StoredCredentials::new("client".into(), None, Vec::new(), None)),+ enabled: true,+ }+}++fn service(+ status: OnboardingStatus,+ records: Vec<McpServerRecord>,+) -> (+ OnboardingServiceImpl<MockRepo, MockStore, MockImport>,+ Arc<MockImport>,+) {+ let import = Arc::new(MockImport::default());+ (+ OnboardingServiceImpl::new(+ MockRepo::new(status),+ Arc::new(MockStore { records }),+ import.clone(),+ ),+ import,+ )+}++#[tokio::test]+async fn active_flow_starts_gathers_for_authenticated_connectors_only() {+ let (service, import) = service(+ OnboardingStatus::Active,+ vec![+ record("https://mcp.linear.app/mcp", true),+ record("https://mcp.notion.com/mcp", false), // not authed yet+ record("https://unrelated.example/mcp", true), // not an import source+ ],+ );++ let state = service.get_state(user()).await.expect("state");+ assert_eq!(state.connected_servers.len(), 3);+ assert_eq!(+ import.gathers.lock().unwrap().as_slice(),+ &[ImportSource::Linear]+ );+}++#[tokio::test]+async fn completed_flow_never_starts_gathers() {+ let (service, import) = service(+ OnboardingStatus::Completed,+ vec![record("https://mcp.linear.app/mcp", true)],+ );++ service.get_state(user()).await.expect("state");+ service.reconcile(user()).await.expect("reconcile");+ assert!(import.gathers.lock().unwrap().is_empty());+}++#[tokio::test]+async fn completing_deletes_leftover_onboarding_staged_rows() {+ let (service, import) = service(OnboardingStatus::Active, Vec::new());++ let row = service.complete(user(), true).await.expect("complete");+ assert_eq!(row.status, OnboardingStatus::Completed);+ assert!(row.skipped);+ // Deleted, not discarded — never-reviewed candidates must stay+ // re-stageable by later gathers or chat.+ assert_eq!(+ import.deletions.lock().unwrap().as_slice(),+ &[Initiator::Onboarding]+ );+ assert!(import.discards.lock().unwrap().is_empty());+}++#[tokio::test]+async fn reconcile_ignores_users_who_never_entered_the_flow() {+ // The MCP post-auth hook reconciles every OAuth completion; a user+ // connecting a server outside onboarding must not get gathers (or an+ // onboarding row) out of it.+ let import = Arc::new(MockImport::default());+ let service = OnboardingServiceImpl::new(+ MockRepo::missing(),+ Arc::new(MockStore {+ records: vec![record("https://mcp.linear.app/mcp", true)],+ }),+ import.clone(),+ );++ service.reconcile(user()).await.expect("reconcile");+ assert!(import.gathers.lock().unwrap().is_empty());+}diff --git a/crates/onboarding/src/inbound/axum_router.rs b/crates/onboarding/src/inbound/axum_router.rsnew file mode 100644index 0000000000..3e09f1a9fd--- /dev/null+++ b/crates/onboarding/src/inbound/axum_router.rs@@ -0,0 +1,123 @@+//! HTTP surface for the onboarding flow.++use crate::domain::models::{OnboardingRow, OnboardingState};+use crate::domain::ports::OnboardingError;+use crate::domain::service::OnboardingService;+use axum::{+ Json, Router,+ extract::{FromRef, State},+ http::StatusCode,+ response::{IntoResponse, Response},+ routing::{get, post},+};+use macro_authorization::{+ MacroAuthorizationExtractor, MacroAuthorizationService, MacroAuthorizationState,+};+use serde::Deserialize;+use std::sync::Arc;+use utoipa::ToSchema;++/// Body for completing onboarding.+#[derive(Debug, Deserialize, ToSchema)]+pub struct CompleteOnboardingRequest {+ /// Whether the user skipped rather than finished.+ #[serde(default)]+ pub skipped: bool,+}++/// Router state: the onboarding service plus the authorization state the+/// request extractors authenticate against (auth happens in the extractor,+/// not in host-layered middleware).+pub struct OnboardingRouterState<T, Auth> {+ /// The onboarding service implementation.+ pub service: Arc<T>,+ /// The authorization state used by the request extractors.+ pub authorization_state: MacroAuthorizationState<Auth>,+}++// Manual Clone impl so T doesn't need to be Clone (it's behind Arc).+impl<T, Auth> Clone for OnboardingRouterState<T, Auth> {+ fn clone(&self) -> Self {+ Self {+ service: self.service.clone(),+ authorization_state: self.authorization_state.clone(),+ }+ }+}++impl<T, Auth> FromRef<OnboardingRouterState<T, Auth>> for Arc<T> {+ fn from_ref(state: &OnboardingRouterState<T, Auth>) -> Self {+ state.service.clone()+ }+}++impl<T, Auth> FromRef<OnboardingRouterState<T, Auth>> for MacroAuthorizationState<Auth> {+ fn from_ref(state: &OnboardingRouterState<T, Auth>) -> Self {+ state.authorization_state.clone()+ }+}++/// Build the onboarding router.+pub fn onboarding_router<T, Auth, S>(state: OnboardingRouterState<T, Auth>) -> Router<S>+where+ T: OnboardingService,+ Auth: MacroAuthorizationService,+ S: Send + Sync + Clone + 'static,+{+ Router::new()+ .route("/onboarding", get(get_state_handler::<T, Auth>))+ .route("/onboarding/complete", post(complete_handler::<T, Auth>))+ .with_state(state)+}++fn error_response(e: OnboardingError) -> Response {+ tracing::error!(error = ?e, "onboarding request failed");+ StatusCode::INTERNAL_SERVER_ERROR.into_response()+}++/// Get the authenticated user's onboarding state. While the flow is active+/// this also starts any gather runs that are due, so polling this endpoint+/// is what keeps onboarding moving.+#[utoipa::path(+ get,+ path = "/onboarding",+ responses(+ (status = 200, description = "Current onboarding state", body = OnboardingState),+ (status = 500, description = "Internal server error"),+ ),+ tag = "onboarding"+)]+#[tracing::instrument(skip(service, user), fields(user_id = %user.macro_user_id))]+pub async fn get_state_handler<T: OnboardingService, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth>,+) -> Response {+ match service.get_state(user.macro_user_id).await {+ Ok(state) => Json(state).into_response(),+ Err(e) => error_response(e),+ }+}++/// Complete (or skip) onboarding. Leftover onboarding-staged import+/// candidates are discarded.+#[utoipa::path(+ post,+ path = "/onboarding/complete",+ request_body = CompleteOnboardingRequest,+ responses(+ (status = 200, description = "Completed onboarding row", body = OnboardingRow),+ (status = 500, description = "Internal server error"),+ ),+ tag = "onboarding"+)]+#[tracing::instrument(skip(service, user, body), fields(user_id = %user.macro_user_id))]+pub async fn complete_handler<T: OnboardingService, Auth: MacroAuthorizationService>(+ State(service): State<Arc<T>>,+ user: MacroAuthorizationExtractor<Auth>,+ Json(body): Json<CompleteOnboardingRequest>,+) -> Response {+ match service.complete(user.macro_user_id, body.skipped).await {+ Ok(row) => Json(row).into_response(),+ Err(e) => error_response(e),+ }+}diff --git a/crates/onboarding/src/inbound/mod.rs b/crates/onboarding/src/inbound/mod.rsnew file mode 100644index 0000000000..c3c71892b6--- /dev/null+++ b/crates/onboarding/src/inbound/mod.rs@@ -0,0 +1,3 @@+//! Inbound adapters: the HTTP router.++pub mod axum_router;diff --git a/crates/onboarding/src/lib.rs b/crates/onboarding/src/lib.rsnew file mode 100644index 0000000000..72daee34d3--- /dev/null+++ b/crates/onboarding/src/lib.rs@@ -0,0 +1,14 @@+//! The onboarding flow (`/setup`): one row per user tracking whether they+//! are still in the flow, plus the hook that starts import gather runs when+//! connectors authenticate.+//!+//! All the actual import machinery — staging, dedup, gather/import jobs —+//! lives in the `import` crate. Onboarding is just a feature that uses it:+//! it starts gathers for authenticated connectors while the flow is active,+//! and discards leftover onboarding-staged rows when the flow completes.++#![deny(missing_docs)]++pub mod domain;+pub mod inbound;+pub mod outbound;diff --git a/crates/onboarding/src/outbound/mod.rs b/crates/onboarding/src/outbound/mod.rsnew file mode 100644index 0000000000..68b609f79e--- /dev/null+++ b/crates/onboarding/src/outbound/mod.rs@@ -0,0 +1,3 @@+//! Outbound adapters: Postgres persistence.++pub mod pg_onboarding_repo;diff --git a/crates/onboarding/src/outbound/pg_onboarding_repo.rs b/crates/onboarding/src/outbound/pg_onboarding_repo.rsnew file mode 100644index 0000000000..7372117a94--- /dev/null+++ b/crates/onboarding/src/outbound/pg_onboarding_repo.rs@@ -0,0 +1,108 @@+//! Postgres implementation of [`OnboardingRepo`].++use crate::domain::models::{OnboardingRow, OnboardingStatus};+use crate::domain::ports::{OnboardingRepo, Result};+use chrono::{DateTime, Utc};+use macro_user_id::user_id::MacroUserIdStr;+use sqlx::PgPool;+use std::str::FromStr;++/// Postgres-backed onboarding repository (MacroDB).+#[derive(Clone)]+pub struct PgOnboardingRepo {+ pool: PgPool,+}++impl PgOnboardingRepo {+ /// Build the repository on the MacroDB pool.+ pub fn new(pool: PgPool) -> Self {+ Self { pool }+ }+}++struct OnboardingDbRow {+ status: String,+ skipped: bool,+ started_at: DateTime<Utc>,+ completed_at: Option<DateTime<Utc>>,+}++impl TryFrom<OnboardingDbRow> for OnboardingRow {+ type Error = crate::domain::ports::OnboardingError;++ fn try_from(row: OnboardingDbRow) -> Result<OnboardingRow> {+ let status = OnboardingStatus::from_str(&row.status)+ .map_err(|_| anyhow::anyhow!("unknown onboarding status: {}", row.status))?;+ Ok(OnboardingRow {+ status,+ skipped: row.skipped,+ started_at: row.started_at,+ completed_at: row.completed_at,+ })+ }+}++impl OnboardingRepo for PgOnboardingRepo {+ #[tracing::instrument(skip(self), err)]+ async fn ensure_row(&self, user: &MacroUserIdStr<'static>) -> Result<OnboardingRow> {+ // One round trip: insert-or-noop, returning the current row either+ // way (the no-op DO UPDATE makes RETURNING yield the existing row).+ let row = sqlx::query_as!(+ OnboardingDbRow,+ r#"+ INSERT INTO user_onboarding (user_id) VALUES ($1)+ ON CONFLICT (user_id) DO UPDATE SET user_id = EXCLUDED.user_id+ RETURNING status, skipped, started_at, completed_at+ "#,+ user.as_ref(),+ )+ .fetch_one(&self.pool)+ .await?;+ row.try_into()+ }++ #[tracing::instrument(skip(self), err)]+ async fn get_row(&self, user: &MacroUserIdStr<'static>) -> Result<Option<OnboardingRow>> {+ let row = sqlx::query_as!(+ OnboardingDbRow,+ r#"+ SELECT status, skipped, started_at, completed_at+ FROM user_onboarding+ WHERE user_id = $1+ "#,+ user.as_ref(),+ )+ .fetch_optional(&self.pool)+ .await?;+ row.map(TryInto::try_into).transpose()+ }++ #[tracing::instrument(skip(self), err)]+ async fn complete(+ &self,+ user: &MacroUserIdStr<'static>,+ skipped: bool,+ ) -> Result<OnboardingRow> {+ let row = sqlx::query_as!(+ OnboardingDbRow,+ r#"+ INSERT INTO user_onboarding (user_id, status, skipped, completed_at)+ VALUES ($1, 'completed', $2, NOW())+ ON CONFLICT (user_id) DO UPDATE+ SET status = 'completed',+ skipped = CASE+ WHEN user_onboarding.status = 'completed' THEN user_onboarding.skipped+ ELSE EXCLUDED.skipped+ END,+ completed_at = COALESCE(user_onboarding.completed_at, EXCLUDED.completed_at),+ updated_at = NOW()+ RETURNING status, skipped, started_at, completed_at+ "#,+ user.as_ref(),+ skipped,+ )+ .fetch_one(&self.pool)+ .await?;+ row.try_into()+ }+}diff --git a/docker/docker-compose.yml b/docker/docker-compose.ymlindex 8def1a8cc5..bd48278998 100644--- a/docker/docker-compose.yml+++ b/docker/docker-compose.yml@@ -120,6 +120,12 @@ services:environment:- OVERRIDE_DOCUMENT_STORAGE_SERVICE_URL=http://document-storage-service:8080- OVERRIDE_AI_EDITING_WORKER_URL=http://ai-editing-worker:8933+ # The import pipeline (and chat's CreateDocument) create documents from+ # inside DCS, which initializes content through lexical + sync.+ - OVERRIDE_SYNC_SERVICE_URL=http://sync-service:8787+ - OVERRIDE_LEXICAL_SERVICE_URL=http://lexical-service:8096+ # Import/ai-projection completions push gateway nudges from inside DCS.+ - OVERRIDE_CONNECTION_GATEWAY_URL=http://connection-gateway:8080networks:databases:services:diff --git a/services/document_cognition_service/Cargo.toml b/services/document_cognition_service/Cargo.tomlindex d099cad08c..65d91dcbb9 100644--- a/services/document_cognition_service/Cargo.toml+++ b/services/document_cognition_service/Cargo.toml@@ -82,9 +82,11 @@ macro_middleware = { path = "../../crates/macro_middleware", default-features =] }macro_user_id = { path = "../../crates/macro_user_id" }macro_uuid = { path = "../../crates/macro_uuid" }+import = { path = "../../crates/import" }mcp_client = { path = "../../crates/mcp_client" }model-error-response = { path = "../../crates/model-error-response" }memory = { path = "../../crates/memory" }+onboarding = { path = "../../crates/onboarding" }model = { path = "../../crates/model" }model-entity = { path = "../../crates/model-entity" }model_notifications = { path = "../../crates/model_notifications" }diff --git a/services/document_cognition_service/src/api/context/mod.rs b/services/document_cognition_service/src/api/context/mod.rsindex b5eb7708d6..824081124d 100644--- a/services/document_cognition_service/src/api/context/mod.rs+++ b/services/document_cognition_service/src/api/context/mod.rs@@ -95,6 +95,18 @@ pub type DcsMcpRouterState = mcp_client::inbound::McpRouterState<DcsAuthorizationService,>;+/// The import pipeline service, shared between the import router, the chat+/// toolset, and the onboarding flow.+pub type DcsImportService = ai_tools::ToolImportService;++/// The onboarding orchestrator wired to the Postgres repo, the MCP server+/// store, and the import service.+pub type DcsOnboardingService = onboarding::domain::service::OnboardingServiceImpl<+ onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo,+ mcp_client::outbound::pg_server_repo::PgServerRepo,+ DcsImportService,+>;+#[derive(Clone, FromRef)]pub struct ApiContext {pub db: PgPool,@@ -126,6 +138,8 @@ pub struct ApiContext {pub message_service: Arc<DcsMessageService>,pub ai_stream_registry: AiStreamRegistry,pub mcp_state: DcsMcpRouterState,+ pub import_service: Arc<DcsImportService>,+ pub onboarding_service: Arc<DcsOnboardingService>,}impl FromRef<ApiContext>diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rsindex cbc9511cec..b7ef72d8bc 100644--- a/services/document_cognition_service/src/api/context/test.rs+++ b/services/document_cognition_service/src/api/context/test.rs@@ -287,8 +287,10 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Arlet search_service_client = Arc::new(search_service_client);// Build properties tool context- let properties_tool_context =- ai_tools::build_properties_tool_context(properties_service, entity_access_service.clone());+ let properties_tool_context = ai_tools::build_properties_tool_context(+ properties_service.clone(),+ entity_access_service.clone(),+ );let email_tool_context = email::inbound::toolset::EmailToolContext::new(Arc::new(@@ -350,6 +352,7 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Aremail_tool_context: email_tool_context.clone(),call_tool_context: call_tool_context.clone(),notification_tool_context: notification_tool_context.clone(),+ import_tool_context: ai_tools::ToolImportToolContext::unwired(),chat_tool_context,channel_tool_context: ai_tools::build_channel_tool_context(pool.clone(),@@ -367,6 +370,36 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Arlet all_tools_prompt: Arc<dyn std::fmt::Display + Send + Sync> =Arc::new(all_tools.prompt.to_string());+ let (import_service, onboarding_service) = {+ let mcp_key =+ mcp_client::domain::models::AesKey::try_from(vec![0u8; 32]).expect("valid test key");+ let mcp_repo =+ mcp_client::outbound::pg_server_repo::PgServerRepo::new(pool.clone(), mcp_key);+ let creator = ai_tools::ToolEntityCreator {+ document_creator: document_tool_context.creator.clone(),+ entity_access_service: entity_access_service.clone(),+ channel_service: tool_service_context.channel_tool_context.service.clone(),+ task_properties: ai_tools::build_task_properties_adapter(+ pool.clone(),+ properties_service.clone(),+ entity_access_service.clone(),+ ),+ team_repository: ai_tools::build_team_repository(pool.clone()),+ };+ let import_service = Arc::new(import::domain::service::ImportServiceImpl::new(+ import::outbound::pg_import_repo::PgImportRepo::new(pool.clone()),+ Arc::new(mcp_repo.clone()),+ Arc::new(creator),+ ai_usage::pg_recorder(pool.clone()),+ ));+ let onboarding_service = Arc::new(onboarding::domain::service::OnboardingServiceImpl::new(+ onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo::new(pool.clone()),+ Arc::new(mcp_repo),+ import_service.clone(),+ ));+ (import_service, onboarding_service)+ };+let memory_repo = memory::outbound::pg_memory_repo::PgMemoryRepo::new(pool.clone());let memory_service = Arc::new(memory::domain::service::MemoryServiceImpl::new(pool.clone(),@@ -501,6 +534,8 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar);mcp_client::inbound::McpRouterState::new(mcp_repo, mcp_oauth, authorization_state)},+ import_service: import_service.clone(),+ onboarding_service,};Arc::new(api_context)}diff --git a/services/document_cognition_service/src/api/mod.rs b/services/document_cognition_service/src/api/mod.rsindex 97951b58a6..8269558031 100644--- a/services/document_cognition_service/src/api/mod.rs+++ b/services/document_cognition_service/src/api/mod.rs@@ -73,6 +73,8 @@ fn api_router(api_context: ApiContext) -> Router {let memory_service = api_context.memory_service.clone();let usage_service = api_context.usage_service.clone();let ai_projections_service = api_context.ai_projections_service.clone();+ let import_service = api_context.import_service.clone();+ let onboarding_service = api_context.onboarding_service.clone();let authorization_state = api_context.authorization_state.clone();let mcp_state = api_context.mcp_state.clone();@@ -95,6 +97,18 @@ fn api_router(api_context: ApiContext) -> Router {authorization_state: authorization_state.clone(),},))+ .merge(import::inbound::axum_router::import_router(+ import::inbound::axum_router::ImportRouterState {+ service: import_service,+ authorization_state: authorization_state.clone(),+ },+ ))+ .merge(onboarding::inbound::axum_router::onboarding_router(+ onboarding::inbound::axum_router::OnboardingRouterState {+ service: onboarding_service,+ authorization_state: authorization_state.clone(),+ },+ )).merge(ai_usage::inbound::ai_usage_router(ai_usage::inbound::AiUsageRouterState {service: usage_service,diff --git a/services/document_cognition_service/src/api/swagger.rs b/services/document_cognition_service/src/api/swagger.rsindex 97e5a9cd5c..806b57db05 100644--- a/services/document_cognition_service/src/api/swagger.rs+++ b/services/document_cognition_service/src/api/swagger.rs@@ -22,11 +22,13 @@ use ai_projections::inbound::axum_router::upsert_projection::{ProjectionStateResponse, UpsertProjectionRequest,};use ai_usage::inbound::axum_router::{self as ai_usage_api};+use import::inbound::axum_router::{self as import_api, RunImportRequest};use mcp_client::inbound::axum_router::{self as mcp_api, AddServerRequest, ServerResponse, StartAuthRequest, StartAuthResponse,UpdateServerRequest,};use memory::inbound::axum_router::{self as memory_api, MemoryErrorBody, MemoryResponse};+use onboarding::inbound::axum_router::{self as onboarding_api, CompleteOnboardingRequest};use crate::api::preview::get_batch_preview::{GetBatchPreviewRequest, GetBatchPreviewResponse};@@ -84,6 +86,12 @@ use utoipa::OpenApi;stream_stop::stop_chat_stream,structured_completion::structured_completion,memory_api::get_memory_handler,+ import_api::get_state_handler,+ import_api::run_import_handler,+ import_api::retry_gather_handler,+ import_api::dismiss_run_handler,+ onboarding_api::get_state_handler,+ onboarding_api::complete_handler,ai_usage_api::get_usage_handler,ai_usage_api::set_pricing_handler,ai_projections::inbound::axum_router::upsert_projection::handler::<crate::api::context::DcsAiProjectionService>,@@ -173,6 +181,28 @@ use utoipa::OpenApi;MemoryResponse,MemoryErrorBody,+ // Import pipeline+ import::domain::models::ImportState,+ import::domain::models::ImportEntity,+ import::domain::models::ImportRun,+ import::domain::models::ImportSource,+ import::domain::models::ImportStatus,+ import::domain::models::Initiator,+ import::domain::models::RunStatus,+ import::domain::models::LinearIssueMeta,+ import::domain::models::NotionDocMeta,+ import::domain::models::SlackChannelMeta,+ import::domain::models::SlackParticipant,+ import::domain::service::RunImportOutcome,+ RunImportRequest,++ // Onboarding+ onboarding::domain::models::OnboardingState,+ onboarding::domain::models::OnboardingRow,+ onboarding::domain::models::OnboardingStatus,+ onboarding::domain::models::ConnectedServer,+ CompleteOnboardingRequest,+// AI costai_usage_api::UsageRequest,ai_usage_api::SetPricingRequest,diff --git a/services/document_cognition_service/src/main.rs b/services/document_cognition_service/src/main.rsindex 63b28a4ea2..4fceed396c 100644--- a/services/document_cognition_service/src/main.rs+++ b/services/document_cognition_service/src/main.rs@@ -291,6 +291,9 @@ async fn main() -> anyhow::Result<()> {properties_service.clone(),entity_access_service.clone(),);+ // The import pipeline sets the same task system properties on imported+ // Linear issues (status, priority, due date, assignee).+ let task_properties_for_import = task_properties_service.clone();let macro_event_broker = macro_event_broker::MacroEventBrokerService::new(macro_event_broker::KafkaEventPublisher::new(config.kafka_brokers.as_ref()).context("failed to create kafka event publisher")?,@@ -417,6 +420,52 @@ async fn main() -> anyhow::Result<()> {tracing::info!("initialized chat tool context");+ let channel_tool_context =+ ai_tools::build_channel_tool_context(db.clone(), lexical_client.clone());+ let recorder = ai_usage::pg_recorder(db.clone());++ // The import pipeline: staged/imported external items, gather jobs over+ // the user's connectors, and the Haiku import job. Built before the tool+ // service context so the chat toolset gets a wired import context.+ let mcp_encryption_key = mcp_client::domain::models::AesKey::try_from(+ config.mcp_credentials_key_secret_name.as_ref(),+ )+ .context("invalid MCP credentials encryption key")?;+ let mcp_server_repo =+ mcp_client::outbound::pg_server_repo::PgServerRepo::new(db.clone(), mcp_encryption_key);++ // Nudges the user's connected clients when import rows flip, so setup+ // sections and chat surfaces update immediately instead of on the next+ // poll (see import::outbound::gateway_notifier).+ let import_notify = import::outbound::gateway_notifier::gateway_import_notify(Arc::new(+ connection_gateway_client::ConnectionGatewayClient::new(+ internal_api_key.clone(),+ ConnectionGatewayUrl::new()?.to_string(),+ ),+ ));++ let entity_creator = ai_tools::ToolEntityCreator {+ document_creator: document_tool_context.creator.clone(),+ entity_access_service: entity_access_service.clone(),+ channel_service: channel_tool_context.service.clone(),+ task_properties: task_properties_for_import,+ team_repository: ai_tools::build_team_repository(db.clone()),+ };+ let import_service = Arc::new(+ import::domain::service::ImportServiceImpl::new(+ import::outbound::pg_import_repo::PgImportRepo::new(db.clone()),+ Arc::new(mcp_server_repo.clone()),+ Arc::new(entity_creator),+ recorder.clone(),+ )+ .with_notifier(import_notify),+ );++ // No boot-time recovery: running batches heartbeat their rows, and the+ // read path reaps rows whose heartbeat stopped — safe with multiple+ // replicas, where a boot-time sweep would clobber other instances' jobs.+ tracing::info!("initialized import service");+let tool_service_context = ai_tools::ToolServiceContext {search_service_client: search_service_client.clone(),email_service_client: email_service_client_external.clone(),@@ -427,16 +476,16 @@ async fn main() -> anyhow::Result<()> {email_tool_context: email_tool_context.clone(),call_tool_context: call_tool_context.clone(),notification_tool_context: notification_tool_context.clone(),- chat_tool_context,- channel_tool_context: ai_tools::build_channel_tool_context(- db.clone(),- lexical_client.clone(),+ import_tool_context: import::inbound::toolset::ImportToolContext::wired(+ import_service.clone(),),+ chat_tool_context,+ channel_tool_context,team_tool_context: ai_tools::build_team_tool_context(db.clone()),crm_tool_context: ai_tools::build_crm_tool_context(db.clone()),schedule_tool_context: ai_tools::NoOpScheduleContext,anthropic_tool_context: ai_tools::build_anthropic_tool_context(),- recorder: ai_usage::pg_recorder(db.clone()),+ recorder,usage_context: ai_usage::UsageContext::system(ai_usage::AiFeature::Chat),};let all_tools = ai_tools::all_tools();@@ -508,12 +557,17 @@ async fn main() -> anyhow::Result<()> {tracing::info!("initialized ai projections service");- let mcp_encryption_key = mcp_client::domain::models::AesKey::try_from(- config.mcp_credentials_key_secret_name.as_ref(),- )- .context("invalid MCP credentials encryption key")?;- let mcp_server_repo =- mcp_client::outbound::pg_server_repo::PgServerRepo::new(db.clone(), mcp_encryption_key);+ // The onboarding flow drives the import pipeline: reads/hooks start+ // gather runs for authenticated connectors, completion discards leftover+ // onboarding-staged candidates.+ let onboarding_service = Arc::new(onboarding::domain::service::OnboardingServiceImpl::new(+ onboarding::outbound::pg_onboarding_repo::PgOnboardingRepo::new(db.clone()),+ Arc::new(mcp_server_repo.clone()),+ import_service.clone(),+ ));++ tracing::info!("initialized onboarding service");+let mcp_redirect_uri = format!("{}/mcp/servers/auth/callback",DocumentCognitionServiceUrl::new()?,@@ -528,11 +582,28 @@ async fn main() -> anyhow::Result<()> {mcp_redirect_uri,mcp_pre_registered,);+ // The moment a connector finishes OAuth, reconcile onboarding for that+ // user — gather jobs start before the user even returns to their+ // original tab. Spawned so the callback response never waits on them.+ let onboarding_for_auth_hook = onboarding_service.clone();+ let mcp_auth_hook: mcp_client::inbound::axum_router::McpAuthCompletedHook =+ Arc::new(move |record: mcp_client::domain::models::McpServerRecord| {+ let service = onboarding_for_auth_hook.clone();+ Box::pin(async move {+ tokio::spawn(async move {+ use onboarding::domain::service::OnboardingService;+ if let Err(e) = service.reconcile(record.user_id).await {+ tracing::warn!(error = ?e, "post-auth onboarding reconcile failed");+ }+ });+ })+ });let mcp_state = mcp_client::inbound::McpRouterState::new(mcp_server_repo,mcp_oauth,authorization_state.clone(),- );+ )+ .with_auth_completed_hook(mcp_auth_hook);let user_permissions_service = Arc::new(roles_and_permissions::domain::service::UserRolesAndPermissionsServiceImpl::new(@@ -573,6 +644,8 @@ async fn main() -> anyhow::Result<()> {redis_client.clone(),),mcp_state,+ import_service,+ onboarding_service,}).await.context("failed to setup and serve api")?;diff --git a/services/mcp_service/src/context.rs b/services/mcp_service/src/context.rsindex 199a76ad3b..f9d4996496 100644--- a/services/mcp_service/src/context.rs+++ b/services/mcp_service/src/context.rs@@ -2,7 +2,7 @@ use std::sync::Arc;use ai_tools::{NoOpCallRtcClient, NoOpConnectionService, NoOpNotificationIngress, NoOpScheduleContext,- NoOpSnsEndpointManager, ToolNotificationQueue, ToolServiceContext,+ NoOpSnsEndpointManager, ToolImportToolContext, ToolNotificationQueue, ToolServiceContext,};use anyhow::Context;use channels::{@@ -327,6 +327,7 @@ async fn build_tool_context(email_tool_context,call_tool_context,notification_tool_context,+ import_tool_context: ToolImportToolContext::unwired(),chat_tool_context,channel_tool_context: ai_tools::build_channel_tool_context(db.clone(),