Skip to content

armgit / macro

Public Imported from GitHub
0

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

feat(import): improve Notion imports and MCP result handling (#5192)

teo095a50d7e642
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index b2a998543c..2fc2a8a24a 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -3007,24 +3007,43 @@
"crates/ai_usage",
"crates/attachment",
"crates/bot_id",
+ "crates/channel_sender",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
"crates/cowlike",
+ "crates/document_sub_type",
+ "crates/entity_access",
+ "crates/entity_access_db_utils",
"crates/import",
+ "crates/invite_email",
+ "crates/last_online_tracker",
"crates/macro_auth",
"crates/macro_authorization",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_service_urls",
"crates/macro_user_id",
"crates/macro_uuid",
"crates/mcp_client",
+ "crates/mention_utils",
+ "crates/model",
"crates/model-entity",
"crates/model-error-response",
"crates/model_file_type",
+ "crates/model_notifications",
"crates/model_user",
+ "crates/models_bulk_upload",
+ "crates/models_entity_access_management",
+ "crates/models_pagination",
+ "crates/models_permissions",
+ "crates/models_properties",
"crates/non_empty",
+ "crates/notification",
"crates/prompt",
+ "crates/properties",
+ "crates/rate_limit",
"crates/remote_env_var",
+ "crates/system_properties",
"crates/workspace-hack"
],
"invite_email": [
@@ -4232,26 +4251,45 @@
"crates/ai_usage",
"crates/attachment",
"crates/bot_id",
+ "crates/channel_sender",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
"crates/cowlike",
+ "crates/document_sub_type",
+ "crates/entity_access",
+ "crates/entity_access_db_utils",
"crates/generic_email_domains",
"crates/import",
+ "crates/invite_email",
+ "crates/last_online_tracker",
"crates/macro_auth",
"crates/macro_authorization",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_service_urls",
"crates/macro_user_id",
"crates/macro_uuid",
"crates/mcp_client",
+ "crates/mention_utils",
+ "crates/model",
"crates/model-entity",
"crates/model-error-response",
"crates/model_file_type",
+ "crates/model_notifications",
"crates/model_user",
+ "crates/models_bulk_upload",
+ "crates/models_entity_access_management",
+ "crates/models_pagination",
+ "crates/models_permissions",
+ "crates/models_properties",
"crates/non_empty",
+ "crates/notification",
"crates/onboarding",
"crates/prompt",
+ "crates/properties",
+ "crates/rate_limit",
"crates/remote_env_var",
+ "crates/system_properties",
"crates/workspace-hack"
],
"opensearch_client": [
diff --git a/Cargo.lock b/Cargo.lock
index 1e848f1dd1..bd0c4f8389 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7164,12 +7164,16 @@ dependencies = [
"axum",
"chrono",
"connection_gateway_client",
+ "dateparser",
"futures",
"macro_authorization",
"macro_db_migrator",
"macro_user_id",
"mcp_client",
"model-entity",
+ "models_properties",
+ "properties",
+ "regex",
"schemars 1.2.1",
"serde",
"serde_json",
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.tsx
index a6890c8ffb..d814c9718a 100644
--- a/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsx
+++ b/apps/web/src/lib/core/component/AI/component/tool/ImportTools.tsx
@@ -1,12 +1,14 @@
import TrayArrowDown from '@phosphor-icons/core/regular/tray-arrow-down.svg';
+import { createSignal } from 'solid-js';
+import { match } from 'ts-pattern';
import { BaseTool } from './BaseTool';
+import { Tool } from './Tool';
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.
+ * (CreateImportEntity / ImportNotionPage / DeleteImportEntity /
+ * ListImportEntities).
*/
/** Human label for the item a call is about, from its source metadata. */
@@ -36,6 +38,53 @@ export const createImportEntityHandler = createToolRenderer({
),
});
+export const importNotionPageHandler = createToolRenderer({
+ name: 'ImportNotionPage',
+ render: (ctx) => {
+ const [isExpanded, setIsExpanded] = createSignal(false);
+ const hasResponse = () => ctx.response !== undefined;
+ const status = () =>
+ match(ctx.response?.data.outcome)
+ .with('imported', () => 'Imported')
+ .with(
+ 'already_imported',
+ 'already_imported_by_teammate',
+ () => 'Already imported'
+ )
+ .with('previously_declined', () => 'Previously declined')
+ .with('import_in_progress', () => 'Importing')
+ .otherwise(() => undefined);
+
+ return (
+ <BaseTool
+ icon={TrayArrowDown}
+ renderContext={ctx.renderContext}
+ type="call"
+ response={
+ hasResponse() && isExpanded() ? (
+ <div class="rounded-md bg-surface-secondary px-3 py-2 text-xs text-ink-muted">
+ {ctx.response?.data.message}
+ </div>
+ ) : undefined
+ }
+ >
+ <div class="flex min-w-0 flex-1 items-center justify-between gap-3 overflow-hidden">
+ <div class="min-w-0 flex-1 truncate">
+ Import Notion page{' '}
+ <span class="text-ink">{ctx.tool.data.pageUrl}</span>
+ </div>
+ <Tool.ResultToggle
+ expanded={isExpanded()}
+ onToggle={() => setIsExpanded((expanded) => !expanded)}
+ showToggle={hasResponse()}
+ status={status()}
+ />
+ </div>
+ </BaseTool>
+ );
+ },
+});
+
export const deleteImportEntityHandler = createToolRenderer({
name: 'DeleteImportEntity',
render: (ctx) => (
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.tsx
index a05cd228bb..3f252d0f38 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
@@ -16,6 +16,7 @@ import { getThreadHandler } from './GetThread';
import {
createImportEntityHandler,
deleteImportEntityHandler,
+ importNotionPageHandler,
listImportEntitiesHandler,
} from './ImportTools';
import { listEntitiesHandler } from './ListEntities';
@@ -66,6 +67,7 @@ import { webSearchHandler } from './WebSearch';
const toolHandlers: ToolHandlerMap<RenderContext> = {
CreateImportEntity: createImportEntityHandler,
DeleteImportEntity: deleteImportEntityHandler,
+ ImportNotionPage: importNotionPageHandler,
GetCompany: getCompanyHandler,
GetEntityProperties: getEntityPropertiesHandler,
ListCompanies: listCompaniesHandler,
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts
index 8eddc421b9..de510c7b7e 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
@@ -1069,6 +1069,66 @@ export const GetThreadResponse = z.object({
threadId: z.string().uuid(),
});
+export const ImportNotionPage = z.object({ pageUrl: z.string() });
+
+export const ImportNotionPageResponse = 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 ListCompanies = z.object({
include_hidden: z.union([z.boolean(), z.null()]).optional(),
limit: z.union([z.number().int().gte(0).lte(65535), z.null()]).optional(),
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/tool.ts
index 073ca70483..03fd452409 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
@@ -49,6 +49,10 @@ type ToolParserMap = {
response: types.GetEntityPropertiesResponse;
};
GetThread: { call: types.GetThread; response: types.GetThreadResponse };
+ ImportNotionPage: {
+ call: types.ImportNotionPage;
+ response: types.ImportNotionPageResponse;
+ };
ListCompanies: {
call: types.ListCompanies;
response: types.ListCompaniesResponse;
@@ -181,6 +185,10 @@ const toolParserMap = {
response: schemas.GetEntityPropertiesResponse,
},
GetThread: { call: schemas.GetThread, response: schemas.GetThreadResponse },
+ ImportNotionPage: {
+ call: schemas.ImportNotionPage,
+ response: schemas.ImportNotionPageResponse,
+ },
ListCompanies: {
call: schemas.ListCompanies,
response: schemas.ListCompaniesResponse,
@@ -333,6 +341,10 @@ type ToolDataMap = {
response: types.GetEntityPropertiesResponse;
};
GetThread: { call: types.GetThread; response: types.GetThreadResponse };
+ ImportNotionPage: {
+ call: types.ImportNotionPage;
+ response: types.ImportNotionPageResponse;
+ };
ListCompanies: {
call: types.ListCompanies;
response: types.ListCompaniesResponse;
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts
index 2025237b93..1bbe940b44 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
@@ -1949,6 +1949,31 @@ export interface ToolContact {
*/
name?: string | null;
}
+/**
+ * Import one specific Notion page through Macro's canonical Notion importer. Use this when the user explicitly asks to import a page URL or id. The tool performs deduplication, fetches through the user's connected Notion MCP, normalizes the page, creates the Macro markdown document, and returns its entity id. Do not fetch and recreate the page manually with generic document tools. Notion databases and database-first pages are intentionally not imported.
+ */
+export interface ImportNotionPage {
+ /**
+ * The exact Notion page URL or stable 32-character Notion page id to import.
+ */
+ pageUrl: string;
+}
+/**
+ * Response from importing one Notion page.
+ */
+export interface ImportNotionPageResponse {
+ entity: ImportEntityView;
+ /**
+ * Human-readable result and next action.
+ */
+ message: string;
+ /**
+ * What happened: `imported`, `already_imported`,
+ * `already_imported_by_teammate`, `previously_declined`, or
+ * `import_in_progress`.
+ */
+ outcome: string;
+}
/**
* List the CRM companies tracked by the authenticated user's team, sorted by most recent interaction. Each row includes the company id, name, domains, last interaction time, and its pipeline Stage / Owner / Revenue properties when set. Use the filters to narrow results: `search` for name/domain text, `stage` for pipeline stage, `owner_user_id` for companies owned by a user. Use GetCompany for one company's full details (contacts + all properties), and SetEntityProperty with entity_type=company to move stages or update owner/revenue/custom properties.
*/
diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rs
index 3968bca628..2538fe14d4 100644
--- a/crates/ai_tools/src/tool_context.rs
+++ b/crates/ai_tools/src/tool_context.rs
@@ -645,6 +645,10 @@ pub type ToolPropertiesService = properties::PropertiesServiceImpl<
NoOpNotificationService,
>;
+/// Imported-document property enrichment backed by the AI tool host's Properties service.
+pub type ToolDocumentPropertiesApplicator =
+ import::outbound::document_properties::DocumentPropertiesApplicator<ToolPropertiesService>;
+
/// Type alias for the properties tool context
pub type ToolPropertiesToolContext =
PropertiesToolContext<ToolPropertiesService, ToolEntityAccessService>;
@@ -746,6 +750,8 @@ pub struct ToolEntityCreator {
pub channel_service: Arc<ToolChannelMessagesService>,
/// Task system-property writes (status / priority / due date / assignees).
pub task_properties: TaskPropertiesAdapter,
+ /// Notion document property and tag enrichment.
+ pub document_properties: ToolDocumentPropertiesApplicator,
/// Team roster lookups, to resolve source-tool emails to teammates.
pub team_repository: Arc<ToolTeamService>,
}
@@ -939,8 +945,33 @@ impl import::domain::ports::EntityCreator for ToolEntityCreator {
user: &MacroUserIdStr<'static>,
name: &str,
markdown: &str,
+ properties: &import::domain::ports::ImportedDocumentProperties,
) -> anyhow::Result<String> {
- self.create_doc(user, name, markdown, false, None).await
+ let document_id = self.create_doc(user, name, markdown, false, None).await?;
+ let access = match self
+ .entity_access_service
+ .generate_entity_access_receipt::<EditAccessLevel>(
+ user,
+ None,
+ &document_id,
+ model_entity::EntityType::Document,
+ )
+ .await
+ {
+ Ok(access) => access,
+ Err(error) => {
+ tracing::warn!(
+ document_id,
+ error = ?error,
+ "failed to authorize imported document properties"
+ );
+ return Ok(document_id);
+ }
+ };
+ self.document_properties
+ .apply(user, &access, &document_id, properties)
+ .await;
+ Ok(document_id)
}
async fn create_channel(
diff --git a/crates/import/Cargo.toml b/crates/import/Cargo.toml
index 74ed10e112..6ef5c29a46 100644
--- a/crates/import/Cargo.toml
+++ b/crates/import/Cargo.toml
@@ -13,6 +13,7 @@ async-trait = { workspace = true }
axum = { workspace = true }
chrono = { workspace = true }
connection_gateway_client = { path = "../connection_gateway_client" }
+dateparser = { workspace = true }
futures = { workspace = true }
macro_authorization = { path = "../macro_authorization", default-features = false, features = [
"axum",
@@ -20,6 +21,11 @@ macro_authorization = { path = "../macro_authorization", default-features = fals
macro_user_id = { path = "../macro_user_id" }
mcp_client = { path = "../mcp_client" }
model-entity = { path = "../model-entity" }
+models_properties = { path = "../models_properties" }
+properties = { path = "../properties", default-features = false, features = [
+ "ports",
+] }
+regex = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
diff --git a/crates/import/src/domain/models.rs b/crates/import/src/domain/models.rs
index de7211a5f1..57786d3c06 100644
--- a/crates/import/src/domain/models.rs
+++ b/crates/import/src/domain/models.rs
@@ -345,6 +345,8 @@ pub fn metadata_label(source: ImportSource, metadata: &serde_json::Value) -> Str
metadata
.get(field)
.and_then(|v| v.as_str())
+ .map(str::trim)
+ .filter(|label| !label.is_empty())
.unwrap_or("(unnamed)")
.to_string()
}
diff --git a/crates/import/src/domain/models/test.rs b/crates/import/src/domain/models/test.rs
index a66f4dfcd7..1d9a0ee114 100644
--- a/crates/import/src/domain/models/test.rs
+++ b/crates/import/src/domain/models/test.rs
@@ -152,4 +152,8 @@ fn metadata_label_reads_title_or_name() {
metadata_label(ImportSource::Notion, &serde_json::json!({})),
"(unnamed)"
);
+ assert_eq!(
+ metadata_label(ImportSource::Notion, &serde_json::json!({"title": " "})),
+ "(unnamed)"
+ );
}
diff --git a/crates/import/src/domain/ports.rs b/crates/import/src/domain/ports.rs
index 198475c51d..289345f3ec 100644
--- a/crates/import/src/domain/ports.rs
+++ b/crates/import/src/domain/ports.rs
@@ -250,6 +250,72 @@ pub struct ImportedTaskProperties {
pub assignee_email: Option<String>,
}
+/// Properties recovered from a Notion page and attached to the imported
+/// Macro document. The creator applies these best-effort after the document
+/// exists, so unsupported or invalid values never prevent the body import.
+#[derive(
+ Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
+)]
+#[serde(rename_all = "camelCase")]
+pub struct ImportedDocumentProperties {
+ /// Ordinary Notion database properties.
+ #[serde(default)]
+ pub values: Vec<ImportedDocumentProperty>,
+ /// Notion tag/label values, mapped onto Macro's personal tag set.
+ #[serde(default)]
+ pub tags: Vec<String>,
+}
+
+/// One named property recovered from a Notion database page.
+#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ImportedDocumentProperty {
+ /// Property display name.
+ pub name: String,
+ /// Typed property value.
+ pub value: ImportedDocumentPropertyValue,
+}
+
+/// Portable property values that have direct Macro property equivalents.
+#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
+#[serde(rename_all = "snake_case", tag = "type")]
+pub enum ImportedDocumentPropertyValue {
+ /// Boolean value.
+ Boolean {
+ /// Imported value.
+ value: bool,
+ },
+ /// Date or date-time value, encoded as ISO-8601.
+ Date {
+ /// Imported value.
+ value: String,
+ },
+ /// Numeric value.
+ Number {
+ /// Imported value.
+ value: f64,
+ },
+ /// Plain text value.
+ String {
+ /// Imported value.
+ value: String,
+ },
+ /// One or more select labels.
+ Select {
+ /// Imported option labels.
+ values: Vec<String>,
+ /// Whether the source property accepts multiple options.
+ multi: bool,
+ },
+ /// One or more URLs.
+ Link {
+ /// Imported URLs.
+ urls: Vec<String>,
+ /// Whether the source property accepts multiple links.
+ multi: bool,
+ },
+}
+
/// 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
@@ -271,6 +337,7 @@ pub trait EntityCreator: Send + Sync + 'static {
user: &MacroUserIdStr<'static>,
name: &str,
markdown: &str,
+ properties: &ImportedDocumentProperties,
) -> impl Future<Output = anyhow::Result<String>> + Send;
/// Create a channel, shared with `team_id` when given (public
diff --git a/crates/import/src/domain/service.rs b/crates/import/src/domain/service.rs
index 0f918c06c7..67d2588378 100644
--- a/crates/import/src/domain/service.rs
+++ b/crates/import/src/domain/service.rs
@@ -12,7 +12,10 @@
//! content through `FinalizeImport` when the direct path can't cope.
use super::models::*;
-use super::ports::{EntityCreator, ImportError, ImportRepo, ImportedTaskProperties, Result};
+use super::ports::{
+ EntityCreator, ImportError, ImportRepo, ImportedDocumentProperties, ImportedDocumentProperty,
+ ImportedDocumentPropertyValue, ImportedTaskProperties, Result,
+};
use crate::inbound::toolset::{
ImportToolContext, ToolPolicy, gather_toolset, notion_import_toolset,
};
@@ -27,6 +30,7 @@ use mcp_client::domain::service::McpToolSet;
use std::collections::HashSet;
use std::pin::Pin;
use std::sync::Arc;
+use std::sync::LazyLock;
use std::time::Duration;
use uuid::Uuid;
@@ -76,6 +80,14 @@ fn notion_import_max_turns(pages: usize) -> usize {
(2 * pages + 6).min(40)
}
+fn notion_import_failure_reason(outcome: &anyhow::Result<()>) -> String {
+ outcome
+ .as_ref()
+ .err()
+ .map(ToString::to_string)
+ .unwrap_or_else(|| "the import job did not finish this item".to_string())
+}
+
/// 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.
@@ -239,9 +251,43 @@ pub trait ImportFinalizer: Send + Sync + 'static {
import_id: Uuid,
name: &str,
content_markdown: &str,
+ properties: &ImportedDocumentProperties,
) -> impl Future<Output = Result<ImportEntity>> + Send;
}
+/// Outcome of explicitly importing one Notion page from an interactive chat.
+#[derive(Debug, Clone, PartialEq)]
+pub enum ImportNotionPageOutcome {
+ /// The page now exists as a Macro document.
+ Imported {
+ /// The ledger row carrying the Macro document id.
+ entity: ImportEntity,
+ /// Whether the document existed before this request.
+ already_existed: bool,
+ /// Whether a teammate imported the existing document.
+ by_teammate: bool,
+ },
+ /// The user previously declined this page, so the explicit import did not
+ /// override that remembered decision.
+ PreviouslyDiscarded(ImportEntity),
+ /// Another request is already importing this page.
+ ImportInProgress(ImportEntity),
+}
+
+/// Workflow used by the interactive agent to import one specific Notion page.
+///
+/// Unlike [`ImportFinalizer`], this owns the complete operation: deduplication,
+/// ledger transitions, connector fetch, content normalization, document
+/// creation, and final ledger state.
+pub trait NotionPageImporter: Send + Sync + 'static {
+ /// Import a Notion page URL or page id for `user`.
+ fn import_notion_page(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ page_url_or_id: &str,
+ ) -> impl Future<Output = Result<ImportNotionPageOutcome>> + Send;
+}
+
/// Concrete orchestrator wiring the repo, the user's MCP servers, the
/// entity creator, and usage recording together.
pub struct ImportServiceImpl<R, S, C> {
@@ -448,27 +494,30 @@ where
.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 fetched = parse_notion_fetch_result(result);
+ anyhow::ensure!(
+ !fetched.is_database,
+ "fetched object is a Notion database rather than a page"
+ );
+ anyhow::ensure!(
+ !fetched.truncated,
+ "fetched page was truncated and requires additional subtree fetches"
+ );
- let name = row
- .metadata
- .get("title")
- .and_then(|v| v.as_str())
- .map(str::to_string)
- .or(fetched_title)
+ let name = fetched
+ .title
+ .or_else(|| {
+ row.metadata
+ .get("title")
+ .and_then(|v| v.as_str())
+ .map(str::trim)
+ .filter(|title| !title.is_empty())
+ .map(str::to_string)
+ })
.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,
- };
+ let markdown = prepare_notion_markdown(&fetched.body)?;
- self.finalize_document(user, row.id, &name, &markdown)
+ self.finalize_document(user, row.id, &name, &markdown, &fetched.properties)
.await
.map_err(|e| anyhow::anyhow!("finalize failed: {e}"))?;
Ok(())
@@ -659,6 +708,42 @@ where
}
}
+ /// Run the canonical single-page Notion pipeline for a row that is
+ /// already `importing`.
+ async fn process_notion_page(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ mcp_tools: Arc<McpToolSet>,
+ row: &ImportEntity,
+ ) -> anyhow::Result<()> {
+ let outcome = tokio::time::timeout(NOTION_PAGE_IMPORT_TIMEOUT, async {
+ match self
+ .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");
+ self.run_notion_import_session(
+ user,
+ std::slice::from_ref(row),
+ mcp_tools,
+ )
+ .await
+ }
+ }
+ })
+ .await
+ .unwrap_or_else(|_| Err(anyhow::anyhow!("notion import timed out")));
+
+ let _ = outcome.as_ref().inspect_err(|e| {
+ tracing::warn!(id = %row.id, error = ?e, "notion page import failed");
+ });
+ let failure_reason = notion_import_failure_reason(&outcome);
+ self.fail_unfinished(user, &[row.id], &failure_reason).await;
+ outcome
+ }
+
/// Run a claimed import batch in the background. Manual batches only
/// update their entity rows; automatic batches also settle their owning
/// run after every row reaches a terminal state.
@@ -728,39 +813,7 @@ where
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"))
- });
- let _ = outcome.inspect_err(|e| {
- 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;
+ let _ = service.process_notion_page(&user, mcp_tools, &row).await;
}
})
.await;
@@ -1097,6 +1150,136 @@ where
}
}
+impl<R, S, C> NotionPageImporter for ImportServiceImpl<R, S, C>
+where
+ R: ImportRepo + Clone,
+ S: McpServerStore,
+ C: EntityCreator,
+{
+ #[tracing::instrument(skip(self, user), fields(page = page_url_or_id), err)]
+ async fn import_notion_page(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ page_url_or_id: &str,
+ ) -> Result<ImportNotionPageOutcome> {
+ let page_url_or_id = page_url_or_id.trim();
+ if page_url_or_id.is_empty() {
+ return Err(ImportError::Other(anyhow::anyhow!(
+ "Notion page URL or id must not be empty"
+ )));
+ }
+
+ let staged = self
+ .stage(
+ user,
+ Initiator::Chat,
+ ImportSource::Notion,
+ page_url_or_id,
+ serde_json::json!({
+ "title": "",
+ "url": page_url_or_id,
+ }),
+ )
+ .await?;
+
+ let staged_row = match staged {
+ StageOutcome::Staged(row) => row,
+ StageOutcome::AlreadyImported {
+ entity,
+ by_teammate,
+ } => {
+ return Ok(ImportNotionPageOutcome::Imported {
+ entity,
+ already_existed: true,
+ by_teammate,
+ });
+ }
+ StageOutcome::PreviouslyDiscarded(row) => {
+ return Ok(ImportNotionPageOutcome::PreviouslyDiscarded(row));
+ }
+ StageOutcome::ImportInProgress(row) => {
+ return Ok(ImportNotionPageOutcome::ImportInProgress(row));
+ }
+ };
+
+ let Some(importing_row) = self
+ .repo
+ .mark_importing(user, &[staged_row.id])
+ .await?
+ .into_iter()
+ .next()
+ else {
+ let row = self.repo.get(user, staged_row.id).await?.ok_or_else(|| {
+ ImportError::Other(anyhow::anyhow!(
+ "Notion import row {} disappeared before it could start",
+ staged_row.id
+ ))
+ })?;
+ return match row.status {
+ ImportStatus::Imported => Ok(ImportNotionPageOutcome::Imported {
+ entity: row,
+ already_existed: true,
+ by_teammate: false,
+ }),
+ ImportStatus::Importing => Ok(ImportNotionPageOutcome::ImportInProgress(row)),
+ ImportStatus::Discarded => Ok(ImportNotionPageOutcome::PreviouslyDiscarded(row)),
+ ImportStatus::Staged => Err(ImportError::Other(anyhow::anyhow!(
+ "Notion page could not be claimed for import"
+ ))),
+ };
+ };
+ self.notify(user).await;
+
+ let mcp_tools = match self.connector_tools(user, ImportSource::Notion).await {
+ Ok(tools) => tools,
+ Err(error) => {
+ self.fail_unfinished(
+ user,
+ &[importing_row.id],
+ &format!("notion unavailable: {error}"),
+ )
+ .await;
+ self.notify(user).await;
+ return Err(ImportError::Other(error));
+ }
+ };
+ let pipeline_result = self
+ .process_notion_page(user, mcp_tools, &importing_row)
+ .await;
+ self.notify(user).await;
+
+ let row = self
+ .repo
+ .get(user, importing_row.id)
+ .await?
+ .ok_or_else(|| {
+ ImportError::Other(anyhow::anyhow!(
+ "Notion import row {} disappeared after import",
+ importing_row.id
+ ))
+ })?;
+ match row.status {
+ ImportStatus::Imported => Ok(ImportNotionPageOutcome::Imported {
+ entity: row,
+ already_existed: false,
+ by_teammate: false,
+ }),
+ ImportStatus::Discarded => Ok(ImportNotionPageOutcome::PreviouslyDiscarded(row)),
+ ImportStatus::Importing => Ok(ImportNotionPageOutcome::ImportInProgress(row)),
+ ImportStatus::Staged => {
+ let detail = pipeline_result
+ .err()
+ .map(|error| error.to_string())
+ .or(row.last_error)
+ .unwrap_or_else(|| "the import did not create a document".to_string());
+ Err(ImportError::Other(anyhow::anyhow!(
+ "Notion page import failed: {detail}"
+ )))
+ }
+ }
+ }
+}
+
impl<R, S, C> ImportFinalizer for ImportServiceImpl<R, S, C>
where
R: ImportRepo + Clone,
@@ -1110,6 +1293,7 @@ where
import_id: Uuid,
name: &str,
content_markdown: &str,
+ properties: &ImportedDocumentProperties,
) -> Result<ImportEntity> {
let row = self
.repo
@@ -1135,8 +1319,10 @@ where
.await
}
ImportSource::Notion => {
+ let content_markdown =
+ prepare_notion_markdown(content_markdown).map_err(ImportError::Other)?;
self.creator
- .create_markdown_doc(user, name, content_markdown)
+ .create_markdown_doc(user, name, &content_markdown, properties)
.await
}
ImportSource::Slack => {
@@ -1161,29 +1347,923 @@ where
}
}
-/// The mangled name of the connector's `notion-fetch` tool, whatever the
-/// user named their server when connecting it (`mcp__<server>__notion-fetch`).
+/// The mangled name of the connector's Notion fetch tool. Notion exposes this
+/// as `notion-fetch` generally and as `fetch` on OpenAI-compatible surfaces.
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"))
+ .find(|name| is_notion_fetch_tool_name(name))
}
-/// 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())
+fn is_notion_fetch_tool_name(name: &str) -> bool {
+ matches!(
+ name.rsplit_once("__").map(|(_, tool)| tool),
+ Some("notion-fetch" | "fetch")
+ )
+}
+
+/// Split a Notion fetch result into its title, Markdown body, and properties.
+/// Structured MCP output and JSON text may use either Notion's `markdown`
+/// field or the generic MCP fetch `text` field.
+#[derive(Debug, Default, PartialEq)]
+struct ParsedNotionPage {
+ title: Option<String>,
+ body: String,
+ properties: ImportedDocumentProperties,
+ is_database: bool,
+ truncated: bool,
+}
+
+fn parse_notion_fetch_result(result: serde_json::Value) -> ParsedNotionPage {
+ match result {
+ serde_json::Value::String(text) => {
+ if let Ok(structured @ (serde_json::Value::Object(_) | serde_json::Value::Array(_))) =
+ serde_json::from_str::<serde_json::Value>(&text)
+ {
+ return parse_notion_fetch_result(structured);
+ }
+ parse_notion_tool_text(&text)
+ }
+ serde_json::Value::Object(map) => parse_notion_fetch_object(map),
+ serde_json::Value::Array(items) => {
+ let mut combined = ParsedNotionPage::default();
+ let mut bodies = Vec::new();
+ for item in items {
+ let parsed = parse_notion_fetch_result(item);
+ if combined.title.is_none() {
+ combined.title = parsed.title;
+ }
+ if combined.properties == ImportedDocumentProperties::default()
+ && parsed.properties != ImportedDocumentProperties::default()
+ {
+ combined.properties = parsed.properties;
+ }
+ combined.is_database |= parsed.is_database;
+ combined.truncated |= parsed.truncated;
+ if !parsed.body.trim().is_empty() {
+ bodies.push(parsed.body);
+ }
+ }
+ combined.body = bodies.join("\n\n");
+ combined
+ }
+ _ => ParsedNotionPage::default(),
+ }
+}
+
+fn parse_notion_fetch_object(map: serde_json::Map<String, serde_json::Value>) -> ParsedNotionPage {
+ let title = notion_title_from_map(&map).or_else(|| {
+ map.get("metadata")
+ .and_then(|value| value.as_object())
+ .and_then(notion_title_from_map)
+ });
+ let properties = map
+ .get("properties")
+ .and_then(|value| value.as_object())
+ .map(imported_notion_properties)
+ .unwrap_or_default();
+ let is_database = notion_object_is_database(&map);
+ let truncated = map
+ .get("truncated")
+ .and_then(serde_json::Value::as_bool)
+ .unwrap_or(false);
+
+ if let Some(body) = map
+ .get("markdown")
+ .or_else(|| map.get("text"))
+ .and_then(|value| value.as_str())
{
- let title = map
- .get("title")
- .and_then(|v| v.as_str())
- .map(str::to_string);
- return (title, body.to_string());
+ let mut parsed = parse_notion_tool_text(body);
+ parsed.title = title.or(parsed.title);
+ if parsed.properties == ImportedDocumentProperties::default() {
+ parsed.properties = properties;
+ }
+ parsed.is_database |= is_database;
+ parsed.truncated |= truncated;
+ return parsed;
+ }
+
+ for nested_key in ["data", "result", "resource"] {
+ if let Some(nested) = map.get(nested_key) {
+ let mut parsed = parse_notion_fetch_result(nested.clone());
+ if parsed.title.is_none() {
+ parsed.title = title.clone();
+ }
+ if parsed.properties == ImportedDocumentProperties::default() {
+ parsed.properties = properties.clone();
+ }
+ parsed.is_database |= is_database;
+ parsed.truncated |= truncated;
+ if !parsed.body.trim().is_empty() {
+ return parsed;
+ }
+ }
+ }
+
+ ParsedNotionPage {
+ title,
+ properties,
+ is_database,
+ truncated,
+ ..Default::default()
+ }
+}
+
+/// The hosted Notion MCP wraps fetched pages in narration plus
+/// `<page><properties>…</properties><content>…</content></page>`. Isolate the
+/// two source-backed sections here so neither the narration nor the metadata
+/// can ever reach the document body.
+fn parse_notion_tool_text(text: &str) -> ParsedNotionPage {
+ if !NOTION_TOOL_RESULT_PREAMBLE.is_match(text) || !text.contains("<page") {
+ return ParsedNotionPage {
+ body: text.to_string(),
+ ..Default::default()
+ };
+ }
+ let Some(content) = notion_xml_section(text, "content") else {
+ return ParsedNotionPage {
+ body: text.to_string(),
+ ..Default::default()
+ };
+ };
+
+ let mut parsed = ParsedNotionPage {
+ body: content.trim_matches('\n').to_string(),
+ ..Default::default()
+ };
+ if let Some(raw_properties) = notion_xml_section(text, "properties")
+ && let Some(map) = parse_notion_property_map(raw_properties.trim())
+ {
+ parsed.title = notion_title_from_map(&map);
+ parsed.properties = imported_notion_properties(&map);
+ }
+ parsed
+}
+
+fn notion_xml_section<'a>(text: &'a str, tag: &str) -> Option<&'a str> {
+ let opening = format!("<{tag}>");
+ let closing = format!("</{tag}>");
+ let start = text.find(&opening)? + opening.len();
+ let end = text[start..].find(&closing)? + start;
+ Some(&text[start..end])
+}
+
+fn parse_notion_property_map(text: &str) -> Option<serde_json::Map<String, serde_json::Value>> {
+ match serde_json::from_str::<serde_json::Value>(text).ok()? {
+ serde_json::Value::Object(map) => Some(map),
+ serde_json::Value::Array(values) => values
+ .into_iter()
+ .find_map(|value| value.as_object().cloned()),
+ _ => None,
+ }
+}
+
+fn notion_title_from_map(map: &serde_json::Map<String, serde_json::Value>) -> Option<String> {
+ ["title", "Name", "name"].into_iter().find_map(|key| {
+ map.get(key)
+ .and_then(serde_json::Value::as_str)
+ .map(str::trim)
+ .filter(|title| !title.is_empty())
+ .map(str::to_string)
+ })
+}
+
+fn notion_object_is_database(map: &serde_json::Map<String, serde_json::Value>) -> bool {
+ let object_type = map
+ .get("object")
+ .and_then(serde_json::Value::as_str)
+ .or_else(|| {
+ map.get("metadata")
+ .and_then(serde_json::Value::as_object)
+ .and_then(|metadata| metadata.get("type"))
+ .and_then(serde_json::Value::as_str)
+ });
+ matches!(object_type, Some("database" | "data_source"))
+}
+
+fn imported_notion_properties(
+ raw: &serde_json::Map<String, serde_json::Value>,
+) -> ImportedDocumentProperties {
+ let mut imported = ImportedDocumentProperties::default();
+
+ for (raw_name, value) in raw {
+ let name = raw_name
+ .strip_prefix("userDefined:")
+ .unwrap_or(raw_name)
+ .trim();
+ if name.is_empty()
+ || name.len() > 100
+ || name.eq_ignore_ascii_case("title")
+ || name.eq_ignore_ascii_case("name")
+ {
+ continue;
+ }
+
+ if let Some(date_name) = name
+ .strip_prefix("date:")
+ .and_then(|rest| rest.strip_suffix(":start"))
+ {
+ if let Some(value) = value.as_str().filter(|value| !value.trim().is_empty()) {
+ imported.values.push(ImportedDocumentProperty {
+ name: date_name.to_string(),
+ value: ImportedDocumentPropertyValue::Date {
+ value: value.to_string(),
+ },
+ });
+ }
+ continue;
+ }
+ if name.starts_with("date:") {
+ continue;
+ }
+
+ if is_tag_property_name(name) {
+ append_string_values(value, &mut imported.tags);
+ continue;
+ }
+
+ let value = match value {
+ serde_json::Value::Bool(value) => {
+ Some(ImportedDocumentPropertyValue::Boolean { value: *value })
+ }
+ serde_json::Value::Number(value) => value
+ .as_f64()
+ .map(|value| ImportedDocumentPropertyValue::Number { value }),
+ serde_json::Value::String(value) => {
+ let value = value.trim();
+ match value {
+ "" => None,
+ "__YES__" => Some(ImportedDocumentPropertyValue::Boolean { value: true }),
+ "__NO__" => Some(ImportedDocumentPropertyValue::Boolean { value: false }),
+ _ if is_web_url(value) => Some(ImportedDocumentPropertyValue::Link {
+ urls: vec![value.to_string()],
+ multi: false,
+ }),
+ _ => Some(ImportedDocumentPropertyValue::String {
+ value: value.to_string(),
+ }),
+ }
+ }
+ serde_json::Value::Array(values) => {
+ let strings: Vec<String> = values
+ .iter()
+ .filter_map(|value| value.as_str())
+ .map(str::trim)
+ .filter(|value| !value.is_empty())
+ .map(str::to_string)
+ .collect();
+ if strings.is_empty() {
+ None
+ } else if strings.iter().all(|value| is_web_url(value)) {
+ Some(ImportedDocumentPropertyValue::Link {
+ urls: strings,
+ multi: true,
+ })
+ } else {
+ Some(ImportedDocumentPropertyValue::Select {
+ values: strings,
+ multi: true,
+ })
+ }
+ }
+ serde_json::Value::Null | serde_json::Value::Object(_) => None,
+ };
+ if let Some(value) = value {
+ imported.values.push(ImportedDocumentProperty {
+ name: name.to_string(),
+ value,
+ });
+ }
+ }
+
+ dedupe_strings(&mut imported.tags);
+ imported
+}
+
+fn is_tag_property_name(name: &str) -> bool {
+ matches!(
+ name.trim().to_ascii_lowercase().as_str(),
+ "tag" | "tags" | "label" | "labels"
+ )
+}
+
+fn is_web_url(value: &str) -> bool {
+ value.starts_with("https://") || value.starts_with("http://")
+}
+
+fn append_string_values(value: &serde_json::Value, output: &mut Vec<String>) {
+ match value {
+ serde_json::Value::String(value) => {
+ let value = value.trim();
+ if !value.is_empty() {
+ output.push(value.to_string());
+ }
+ }
+ serde_json::Value::Array(values) => {
+ output.extend(
+ values
+ .iter()
+ .filter_map(|value| value.as_str())
+ .map(str::trim)
+ .filter(|value| !value.is_empty())
+ .map(str::to_string),
+ );
+ }
+ _ => {}
+ }
+}
+
+fn dedupe_strings(values: &mut Vec<String>) {
+ let mut seen = HashSet::new();
+ values.retain(|value| seen.insert(value.to_ascii_lowercase()));
+}
+
+static PAIRED_NOTION_PAGE_REFS: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {
+ ["page", "mention-page"]
+ .into_iter()
+ .map(|tag| {
+ regex::Regex::new(&format!(
+ r#"(?s)<{tag}\b(?P<attrs>[^>]*)>(?P<label>.*?)</{tag}>"#
+ ))
+ .expect("valid notion page reference regex")
+ })
+ .collect()
+});
+static SELF_CLOSING_NOTION_PAGE_REF: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"<(?:ancestor-\d+-)?page\b(?P<attrs>[^>]*)/?>"#)
+ .expect("valid self-closing notion page reference regex")
+});
+static PAIRED_NOTION_TEXT_MENTIONS: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {
+ ["mention-user", "mention-agent"]
+ .into_iter()
+ .map(|tag| {
+ regex::Regex::new(&format!(r#"(?s)<{tag}\b[^>]*>(?P<label>.*?)</{tag}>"#))
+ .expect("valid notion text mention regex")
+ })
+ .collect()
+});
+static NOTION_DATE_MENTION: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"<mention-date\b(?P<attrs>[^>]*)/?>"#)
+ .expect("valid notion date mention regex")
+});
+static PAIRED_NOTION_MEDIA: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {
+ ["audio", "video", "file", "pdf"]
+ .into_iter()
+ .map(|tag| {
+ regex::Regex::new(&format!(
+ r#"(?s)<{tag}\b(?P<attrs>[^>]*)>(?P<label>.*?)</{tag}>"#
+ ))
+ .expect("valid notion media regex")
+ })
+ .collect()
+});
+static NOTION_UNKNOWN_BLOCK: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"<unknown\b(?P<attrs>[^>]*)/?>"#).expect("valid notion unknown-block regex")
+});
+static PAIRED_NOTION_DATABASES: LazyLock<Vec<regex::Regex>> = LazyLock::new(|| {
+ ["database", "mention-database", "mention-data-source"]
+ .into_iter()
+ .map(|tag| {
+ regex::Regex::new(&format!(r#"(?s)<{tag}\b[^>]*>.*?</{tag}>"#))
+ .expect("valid notion database regex")
+ })
+ .collect()
+});
+static SELF_CLOSING_NOTION_DATABASE: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"(?s)<(?:database|mention-database|mention-data-source)\b[^>]*/?>"#)
+ .expect("valid self-closing notion database regex")
+});
+static NOTION_TOGGLE_MARKER: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"[ \t]*\{toggle\s*=\s*"true"\}"#).expect("valid notion toggle marker regex")
+});
+static NOTION_BLOCK_ATTRIBUTES: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"[ \t]*\{(?:(?:toggle|color)\s*=\s*"[^"]*"[ \t]*)+\}[ \t]*$"#)
+ .expect("valid notion block-attribute regex")
+});
+static PAIRED_NOTION_SPAN: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"(?s)<span\b[^>]*>(?P<label>.*?)</span>"#).expect("valid notion span regex")
+});
+static NOTION_SUMMARY: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"(?s)<summary>(?P<label>.*?)</summary>"#)
+ .expect("valid notion summary regex")
+});
+static NOTION_EMPTY_BLOCK: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"(?m)^[\t ]*<(?:empty-block|table_of_contents)\b[^>]*/?>[\t ]*$"#)
+ .expect("valid empty Notion block regex")
+});
+static ESCAPED_NOTION_TODO: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"^(?P<indent>[\t ]*)(?:-\s+)?\\\[(?P<state>[ xX])\\\](?P<rest>.*)$"#)
+ .expect("valid escaped Notion todo regex")
+});
+static NOTION_TOOL_RESULT_PREAMBLE: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"(?im)^\s*Here is the result of "(?:view|fetch)"(?:\s|:)"#)
+ .expect("valid notion tool-result preamble regex")
+});
+static NOTION_SERIALIZED_TITLE_WRAPPER: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"(?m)^\s*(?:\[\s*)?\{\s*"[^"]+"\s*:"#)
+ .expect("valid notion serialized title wrapper regex")
+});
+static UNHANDLED_NOTION_MARKUP: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(
+ r#"(?i)</?(?:page|database|mention-[\w-]+|ancestor-\d+-page|details|summary|callout|columns?|synced_block(?:_reference)?|table|tr|td|th|colgroup|col|properties|content)\b"#,
+ )
+ .expect("valid unsupported Notion markup regex")
+});
+static NOTION_ATTR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r#"(?P<name>[\w-]+)="(?P<value>[^"]*)""#)
+ .expect("valid notion attribute regex")
+});
+static INLINE_TAG_RE: LazyLock<regex::Regex> =
+ LazyLock::new(|| regex::Regex::new(r"(?s)<[^>]+>").expect("valid inline tag regex"));
+static BREAK_TAG_RE: LazyLock<regex::Regex> =
+ LazyLock::new(|| regex::Regex::new(r"(?i)<br\s*/?>").expect("valid HTML break regex"));
+static TABLE_ROW_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r"(?s)<tr\b[^>]*>(?P<body>.*?)</tr>").expect("valid notion table row regex")
+});
+static TABLE_CELL_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
+ regex::Regex::new(r"(?s)<t[dh]\b[^>]*>(?P<body>.*?)</t[dh]>")
+ .expect("valid notion table cell regex")
+});
+
+/// Convert Notion's enhanced-markdown extensions into the markdown dialect
+/// consumed by Macro's Lexical service. In particular, pipe tables become
+/// native Lexical tables and Notion page references remain clickable external
+/// links even when the referenced page was not part of the import.
+fn normalize_notion_markdown(input: &str) -> String {
+ transform_outside_fenced_code(input, normalize_notion_markdown_fragment)
+}
+
+fn normalize_notion_markdown_fragment(input: &str) -> String {
+ let mut markdown = remove_notion_databases_fragment(input);
+ for regex in PAIRED_NOTION_PAGE_REFS.iter() {
+ markdown = regex
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ notion_page_link(
+ &captures["attrs"],
+ Some(INLINE_TAG_RE.replace_all(&captures["label"], "")),
+ )
+ })
+ .into_owned();
}
- (None, text.to_string())
+ markdown = SELF_CLOSING_NOTION_PAGE_REF
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ notion_page_link(&captures["attrs"], None)
+ })
+ .into_owned();
+ for regex in PAIRED_NOTION_TEXT_MENTIONS.iter() {
+ markdown = regex
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ let label = INLINE_TAG_RE.replace_all(&captures["label"], "");
+ let label = label.trim();
+ if label.is_empty() {
+ String::new()
+ } else {
+ format!("@{label}")
+ }
+ })
+ .into_owned();
+ }
+ markdown = NOTION_DATE_MENTION
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ notion_date_mention(&captures["attrs"])
+ })
+ .into_owned();
+ for regex in PAIRED_NOTION_MEDIA.iter() {
+ markdown = regex
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ notion_media_link(&captures["attrs"], &captures["label"])
+ })
+ .into_owned();
+ }
+ markdown = NOTION_UNKNOWN_BLOCK
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ notion_unknown_block(&captures["attrs"])
+ })
+ .into_owned();
+ markdown = PAIRED_NOTION_SPAN
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ captures["label"].to_string()
+ })
+ .into_owned();
+ markdown = NOTION_SUMMARY
+ .replace_all(&markdown, |captures: &regex::Captures<'_>| {
+ captures["label"].to_string()
+ })
+ .into_owned();
+ markdown = markdown
+ .replace("<ancestor-path>\n", "")
+ .replace("\n</ancestor-path>", "")
+ .replace("<ancestor-path>", "")
+ .replace("</ancestor-path>", "");
+ markdown = NOTION_TOGGLE_MARKER.replace_all(&markdown, "").into_owned();
+ markdown = NOTION_BLOCK_ATTRIBUTES
+ .replace_all(&markdown, "")
+ .into_owned();
+ markdown = NOTION_EMPTY_BLOCK.replace_all(&markdown, "").into_owned();
+ markdown = convert_notion_tables(&markdown);
+ markdown = flatten_notion_containers(&markdown);
+ markdown = normalize_escaped_notion_todos(&markdown);
+ let input_trailing_newlines = input
+ .chars()
+ .rev()
+ .take_while(|value| *value == '\n')
+ .count();
+ let output_trailing_newlines = markdown
+ .chars()
+ .rev()
+ .take_while(|value| *value == '\n')
+ .count();
+ for _ in output_trailing_newlines..input_trailing_newlines {
+ markdown.push('\n');
+ }
+ markdown
+}
+
+fn remove_notion_databases(input: &str) -> String {
+ transform_outside_fenced_code(input, remove_notion_databases_fragment)
+}
+
+fn remove_notion_databases_fragment(input: &str) -> String {
+ let mut markdown = input.to_string();
+ for regex in PAIRED_NOTION_DATABASES.iter() {
+ markdown = regex.replace_all(&markdown, "").into_owned();
+ }
+ SELF_CLOSING_NOTION_DATABASE
+ .replace_all(&markdown, "")
+ .into_owned()
+}
+
+fn prepare_notion_markdown(input: &str) -> anyhow::Result<String> {
+ anyhow::ensure!(!input.trim().is_empty(), "fetched page has no content");
+ anyhow::ensure!(
+ !NOTION_TOOL_RESULT_PREAMBLE.is_match(input)
+ && !NOTION_SERIALIZED_TITLE_WRAPPER.is_match(input),
+ "fetched page contained tool-result metadata instead of body content"
+ );
+ anyhow::ensure!(
+ !notion_page_is_mostly_database(input),
+ "fetched page is primarily a Notion database"
+ );
+
+ let markdown = normalize_notion_markdown(input);
+ anyhow::ensure!(
+ !markdown.trim().is_empty(),
+ "fetched page has no supported body content"
+ );
+ anyhow::ensure!(
+ !contains_outside_fenced_code(&markdown, &UNHANDLED_NOTION_MARKUP),
+ "fetched page contained unsupported Notion markup after normalization"
+ );
+ Ok(markdown)
+}
+
+fn notion_page_is_mostly_database(input: &str) -> bool {
+ let without_databases = remove_notion_databases(input);
+ if without_databases == input {
+ return false;
+ }
+
+ // Database blocks often contain only a title/reference rather than their
+ // full row data. A page with fewer than roughly two paragraphs of
+ // non-database text is therefore treated as database-first and left out.
+ let remaining_text = INLINE_TAG_RE.replace_all(&without_databases, "");
+ let substantive_chars = remaining_text
+ .chars()
+ .filter(|character| character.is_alphanumeric())
+ .count();
+ substantive_chars < 200
+}
+
+fn notion_page_link(attrs: &str, body_label: Option<std::borrow::Cow<'_, str>>) -> String {
+ let mut url = None;
+ let mut title = None;
+ for captures in NOTION_ATTR_RE.captures_iter(attrs) {
+ match &captures["name"] {
+ "url" => url = Some(captures["value"].to_string()),
+ "title" => title = Some(captures["value"].to_string()),
+ _ => {}
+ }
+ }
+ let Some(url) = url else {
+ return body_label.unwrap_or_default().into_owned();
+ };
+ let label = body_label
+ .as_deref()
+ .map(str::trim)
+ .filter(|label| !label.is_empty())
+ .or_else(|| {
+ title
+ .as_deref()
+ .map(str::trim)
+ .filter(|label| !label.is_empty())
+ })
+ .unwrap_or("Notion page");
+ format!("[{}]({url})", escape_markdown_link_label(label))
+}
+
+fn notion_media_link(attrs: &str, body_label: &str) -> String {
+ let attributes = notion_attributes(attrs);
+ let Some(url) = attributes
+ .get("src")
+ .or_else(|| attributes.get("url"))
+ .filter(|url| is_web_url(url))
+ else {
+ return INLINE_TAG_RE.replace_all(body_label, "").trim().to_string();
+ };
+ let label = INLINE_TAG_RE.replace_all(body_label, "");
+ let label = label.trim();
+ let label = if label.is_empty() {
+ "Notion file"
+ } else {
+ label
+ };
+ format!("[{}]({url})", escape_markdown_link_label(label))
+}
+
+fn notion_unknown_block(attrs: &str) -> String {
+ let attributes = notion_attributes(attrs);
+ let Some(url) = attributes.get("url").filter(|url| is_web_url(url)) else {
+ return String::new();
+ };
+ let label = attributes
+ .get("alt")
+ .map(String::as_str)
+ .map(str::trim)
+ .filter(|label| !label.is_empty())
+ .unwrap_or("Unsupported Notion content");
+ format!("[{}]({url})", escape_markdown_link_label(label))
+}
+
+fn notion_date_mention(attrs: &str) -> String {
+ let attributes = notion_attributes(attrs);
+ let Some(start) = attributes.get("start") else {
+ return String::new();
+ };
+ let mut label = humanize_notion_date(start);
+ if let Some(time) = attributes.get("startTime").filter(|time| !time.is_empty()) {
+ label.push(' ');
+ label.push_str(time);
+ }
+ if let Some(end) = attributes
+ .get("end")
+ .filter(|end| !end.is_empty() && *end != start)
+ {
+ label.push_str(" – ");
+ label.push_str(&humanize_notion_date(end));
+ }
+ label
+}
+
+fn humanize_notion_date(value: &str) -> String {
+ chrono::NaiveDate::parse_from_str(value, "%Y-%m-%d")
+ .map(|date| date.format("%B %-d, %Y").to_string())
+ .unwrap_or_else(|_| value.to_string())
+}
+
+fn notion_attributes(attrs: &str) -> std::collections::HashMap<String, String> {
+ NOTION_ATTR_RE
+ .captures_iter(attrs)
+ .map(|captures| (captures["name"].to_string(), captures["value"].to_string()))
+ .collect()
+}
+
+fn escape_markdown_link_label(label: &str) -> String {
+ label
+ .replace('\\', "\\\\")
+ .replace('[', "\\[")
+ .replace(']', "\\]")
+}
+
+#[derive(Debug)]
+enum DroppedNotionContainer {
+ Plain,
+ Callout {
+ icon: Option<String>,
+ emitted_icon: bool,
+ },
+}
+
+fn flatten_notion_containers(input: &str) -> String {
+ let mut output = Vec::new();
+ let mut containers: Vec<DroppedNotionContainer> = Vec::new();
+
+ for line in input.lines() {
+ let trimmed = line.trim();
+ if is_notion_container_close(trimmed) {
+ containers.pop();
+ continue;
+ }
+ if let Some(container) = notion_container_open(trimmed) {
+ containers.push(container);
+ continue;
+ }
+
+ let mut flattened = drop_notion_container_indentation(line, containers.len()).to_string();
+ if containers
+ .iter()
+ .any(|container| matches!(container, DroppedNotionContainer::Callout { .. }))
+ {
+ let icon = containers
+ .iter_mut()
+ .rev()
+ .find_map(|container| match container {
+ DroppedNotionContainer::Callout { icon, emitted_icon } if !*emitted_icon => {
+ *emitted_icon = true;
+ icon.take()
+ }
+ _ => None,
+ });
+ flattened = match (icon, flattened.trim().is_empty()) {
+ (Some(icon), false) => format!("> {icon} {flattened}"),
+ (Some(icon), true) => format!("> {icon}"),
+ (None, false) => format!("> {flattened}"),
+ (None, true) => ">".to_string(),
+ };
+ }
+ output.push(flattened);
+ }
+
+ output.join("\n")
+}
+
+fn notion_container_open(line: &str) -> Option<DroppedNotionContainer> {
+ for tag in [
+ "details",
+ "columns",
+ "column",
+ "synced_block",
+ "synced_block_reference",
+ ] {
+ if line.starts_with(&format!("<{tag}")) && line.ends_with('>') {
+ return Some(DroppedNotionContainer::Plain);
+ }
+ }
+ if line.starts_with("<callout") && line.ends_with('>') {
+ let attrs = line
+ .strip_prefix("<callout")
+ .and_then(|line| line.strip_suffix('>'))
+ .unwrap_or_default();
+ return Some(DroppedNotionContainer::Callout {
+ icon: notion_attributes(attrs).remove("icon"),
+ emitted_icon: false,
+ });
+ }
+ None
+}
+
+fn is_notion_container_close(line: &str) -> bool {
+ [
+ "</details>",
+ "</callout>",
+ "</columns>",
+ "</column>",
+ "</synced_block>",
+ "</synced_block_reference>",
+ ]
+ .contains(&line)
+}
+
+fn drop_notion_container_indentation(mut line: &str, levels: usize) -> &str {
+ for _ in 0..levels {
+ if let Some(rest) = line.strip_prefix('\t') {
+ line = rest;
+ } else if let Some(rest) = line.strip_prefix(" ") {
+ line = rest;
+ } else {
+ break;
+ }
+ }
+ line
+}
+
+fn normalize_escaped_notion_todos(input: &str) -> String {
+ input
+ .lines()
+ .map(|line| {
+ ESCAPED_NOTION_TODO
+ .captures(line)
+ .map(|captures| {
+ format!(
+ "{}- [{}]{}",
+ &captures["indent"],
+ captures["state"].to_ascii_lowercase(),
+ &captures["rest"]
+ )
+ })
+ .unwrap_or_else(|| line.to_string())
+ })
+ .collect::<Vec<_>>()
+ .join("\n")
+}
+
+fn transform_outside_fenced_code(input: &str, mut transform: impl FnMut(&str) -> String) -> String {
+ let mut output = String::with_capacity(input.len());
+ let mut fragment = String::new();
+ let mut fence: Option<char> = None;
+
+ for line in input.split_inclusive('\n') {
+ let trimmed = line.trim_start();
+ let marker = trimmed
+ .chars()
+ .next()
+ .filter(|marker| matches!(marker, '`' | '~'))
+ .filter(|marker| trimmed.chars().take_while(|value| value == marker).count() >= 3);
+
+ match (fence, marker) {
+ (None, Some(marker)) => {
+ output.push_str(&transform(&fragment));
+ fragment.clear();
+ output.push_str(line);
+ fence = Some(marker);
+ }
+ (Some(open), Some(close)) if open == close => {
+ output.push_str(line);
+ fence = None;
+ }
+ (Some(_), _) => output.push_str(line),
+ (None, None) => fragment.push_str(line),
+ }
+ }
+ output.push_str(&transform(&fragment));
+ output
+}
+
+fn contains_outside_fenced_code(input: &str, pattern: &regex::Regex) -> bool {
+ let mut found = false;
+ let _ = transform_outside_fenced_code(input, |fragment| {
+ found |= pattern.is_match(fragment);
+ fragment.to_string()
+ });
+ found
+}
+
+fn convert_notion_tables(input: &str) -> String {
+ let mut output = String::with_capacity(input.len());
+ let mut remaining = input;
+ while let Some(start) = remaining.find("<table") {
+ output.push_str(&remaining[..start]);
+ let table = &remaining[start..];
+ let Some(close_start) = table.find("</table>") else {
+ output.push_str(table);
+ return output;
+ };
+ let close_end = close_start + "</table>".len();
+ let fragment = &table[..close_end];
+ match notion_table_to_markdown(fragment) {
+ Some(converted) => output.push_str(&converted),
+ None => output.push_str(fragment),
+ }
+ remaining = &table[close_end..];
+ }
+ output.push_str(remaining);
+ output
+}
+
+fn notion_table_to_markdown(table: &str) -> Option<String> {
+ let mut rows: Vec<Vec<String>> = TABLE_ROW_RE
+ .captures_iter(table)
+ .filter_map(|row| {
+ let cells: Vec<String> = TABLE_CELL_RE
+ .captures_iter(&row["body"])
+ .map(|cell| normalize_notion_table_cell(&cell["body"]))
+ .collect();
+ (!cells.is_empty()).then_some(cells)
+ })
+ .collect();
+ let column_count = rows.iter().map(Vec::len).max()?;
+ if column_count == 0 {
+ return None;
+ }
+ for row in &mut rows {
+ row.resize(column_count, String::new());
+ }
+
+ let mut lines = Vec::with_capacity(rows.len() + 1);
+ for (index, row) in rows.into_iter().enumerate() {
+ lines.push(format!("| {} |", row.join(" | ")));
+ if index == 0 {
+ lines.push(format!(
+ "| {} |",
+ std::iter::repeat_n("---", column_count)
+ .collect::<Vec<_>>()
+ .join(" | ")
+ ));
+ }
+ }
+ Some(lines.join("\n"))
+}
+
+fn normalize_notion_table_cell(cell: &str) -> String {
+ let with_breaks = BREAK_TAG_RE.replace_all(cell, "\n");
+ let without_tags = INLINE_TAG_RE.replace_all(&with_breaks, "");
+ without_tags
+ .lines()
+ .map(str::trim)
+ .filter(|line| !line.is_empty())
+ .collect::<Vec<_>>()
+ .join("\\n")
+ .replace('|', "&#124;")
}
/// Map a Linear workflow status name onto Macro's task status label.
diff --git a/crates/import/src/domain/service/prompts.rs b/crates/import/src/domain/service/prompts.rs
index 5ac34588fa..8d0ba3e50d 100644
--- a/crates/import/src/domain/service/prompts.rs
+++ b/crates/import/src/domain/service/prompts.rs
@@ -88,19 +88,56 @@ pub const NOTION_IMPORT_SYSTEM: &str = "You are importing Notion pages the user
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\
+ 1. Fetch its content by URL or page id with the canonical Notion `notion-fetch` tool (named \
+ `fetch` on OpenAI-compatible MCP connections). Never use a search snippet or a generic \
+ `view` result as the page body. If neither `notion-fetch` nor `fetch` is available, skip the \
+ page.\n\
+ 2. Treat the tool result as input data, not as page content. `content_markdown` must contain \
+ ONLY body content that actually appears in the fetched page. Never include tool narration \
+ such as `Here is the result of \"view\"...`, timestamps, serialized result wrappers such as \
+ `[{\"title\":\"Content team\"}]`, property/title metadata, the staged summary, or text you \
+ inferred or invented. The page title belongs in `name`; do not repeat it in \
+ `content_markdown` merely because it appeared as title metadata. A hosted Notion MCP result \
+ may wrap the source as `<page><properties>...</properties><content>...</content></page>`; use \
+ only the contents of `<content>` for the body and map `<properties>` separately. If no body \
+ content was fetched, skip the page rather than fabricating content.\n\
+ 3. Convert Notion's enhanced markdown to clean Macro markdown. Preserve headings, lists, \
+ checkboxes, formatting, and image URLs. Never copy raw Notion XML-like tags into the result. \
+ Macro has no toggle block: remove markers such as `{toggle=\"true\"}` and `<details>` / \
+ `<summary>` wrappers while retaining and de-indenting the toggle title and body as ordinary \
+ markdown. Convert Notion user/date mentions to readable text, file blocks to markdown links, \
+ and callouts to blockquotes.\n\
+ 4. Macro does not support Notion databases. Remove every `<database>` and \
+ `<mention-database>` block or reference completely; do not turn it into a link, table, or \
+ prose. If the fetched object is itself a database, or the page is mostly database with \
+ little substantive non-database body content, skip the whole page and do NOT call \
+ `FinalizeImport` for it.\n\
+ 5. Convert every `<page>`, `<mention-page>`, and `<ancestor-N-page>` reference \
+ into a normal markdown link: `[visible title](notion URL)`. The destination may not be \
+ imported into Macro, so keep it as an external Notion URL; never invent a Macro entity id. \
+ If a reference has no title, use `Notion page` as its visible text.\n\
+ 6. Convert every Notion `<table>` to a rectangular pipe table. Use one `| ... |` line per \
+ row and put `| --- | ... |` immediately after the first row. Every row must have the same \
+ cell count. Represent line breaks or lists inside a cell with the two literal characters \
+ `\\n` so the Macro Lexical transformer can reconstruct rich cell content; encode a literal \
+ pipe inside a cell as `&#124;`. Do not emit `<table>`, `<tr>`, `<td>`, `<colgroup>`, or \
+ `<col>` tags.\n\
+ 7. Read the fetched page's `properties` map. Put properties named Tags, Tag, Labels, or \
+ Label into `tags`. Put other useful, non-title values into `properties` with the closest \
+ supported type: boolean, date (ISO-8601), number, string, select, or link. For select and \
+ link values, set `multi` from the fetched source shape: arrays are multi-valued even when \
+ they contain only one item; scalar values are not. Omit empty, computed, rollup, relation, \
+ and unsupported values rather than flattening them into the document body. If the fetch \
+ says it was truncated, fetch every available \
+ `unknown_block_id` and replace its `<unknown>` placeholder with that fetched subtree. If a \
+ subtree is inaccessible, remove its placeholder; never guess its contents.\n\
+ 8. Immediately call `FinalizeImport` with the page's `import_id`, `name`, \
+ `content_markdown`, `properties`, and `tags` — 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.";
+ Call `FinalizeImport` exactly once for every eligible page. Never call it for a page skipped \
+ because its body was unavailable or it was primarily a database. When all pages have been \
+ handled, reply with one short sentence.";
/// User message for the Notion import session: the accepted rows.
pub fn notion_import_prompt(rows: &[ImportEntity]) -> String {
diff --git a/crates/import/src/domain/service/test.rs b/crates/import/src/domain/service/test.rs
index 24a7d3dd29..ed8f625f61 100644
--- a/crates/import/src/domain/service/test.rs
+++ b/crates/import/src/domain/service/test.rs
@@ -100,6 +100,19 @@ fn notion_import_turns_scale_with_pages_and_cap() {
assert_eq!(notion_import_max_turns(100), 40);
}
+#[test]
+fn notion_import_failure_reason_preserves_the_actual_error() {
+ let failure = Err(anyhow::anyhow!("notion fetch was truncated"));
+ assert_eq!(
+ notion_import_failure_reason(&failure),
+ "notion fetch was truncated"
+ );
+ assert_eq!(
+ notion_import_failure_reason(&Ok(())),
+ "the import job did not finish this item"
+ );
+}
+
#[test]
fn slack_gather_lists_channels_with_an_explicit_empty_query() {
let prompt = prompts::gather_system(ImportSource::Slack);
@@ -113,20 +126,327 @@ fn slack_gather_lists_channels_with_an_explicit_empty_query() {
#[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"}"##,
+ let parsed = parse_notion_fetch_result(serde_json::Value::String(
+ r##"{"id":"abc","title":"Roadmap H2","text":"# Roadmap\ncontent","url":"https://notion.so/x","properties":{"title":"Roadmap H2","Tags":["Planning","H2"],"Done":"__YES__","Score":4.5,"date:Due:start":"2026-08-01","date:Due:is_datetime":0}}"##
+ .to_string(),
+ ));
+ assert_eq!(parsed.title.as_deref(), Some("Roadmap H2"));
+ assert_eq!(parsed.body, "# Roadmap\ncontent");
+ assert_eq!(parsed.properties.tags, vec!["Planning", "H2"]);
+ assert_eq!(
+ parsed.properties.values,
+ vec![
+ ImportedDocumentProperty {
+ name: "Done".into(),
+ value: ImportedDocumentPropertyValue::Boolean { value: true },
+ },
+ ImportedDocumentProperty {
+ name: "Score".into(),
+ value: ImportedDocumentPropertyValue::Number { value: 4.5 },
+ },
+ ImportedDocumentProperty {
+ name: "Due".into(),
+ value: ImportedDocumentPropertyValue::Date {
+ value: "2026-08-01".into(),
+ },
+ },
+ ]
);
- 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");
+ let parsed = parse_notion_fetch_result(serde_json::Value::String(
+ "# Plain markdown\nno wrapper".to_string(),
+ ));
+ assert_eq!(parsed.title, None);
+ assert_eq!(parsed.body, "# Plain markdown\nno wrapper");
- // JSON without a text field falls through to raw.
+ // Metadata-only JSON is not page content.
let raw = r#"{"unexpected":"shape"}"#;
- let (title, body) = parse_notion_fetch_text(raw);
- assert_eq!(title, None);
- assert_eq!(body, raw);
+ let parsed = parse_notion_fetch_result(serde_json::Value::String(raw.to_string()));
+ assert_eq!(parsed.title, None);
+ assert_eq!(parsed.body, "");
+}
+
+#[test]
+fn notion_fetch_accepts_structured_markdown_and_resource_arrays() {
+ let parsed = parse_notion_fetch_result(serde_json::json!({
+ "object": "page_markdown",
+ "id": "abc",
+ "title": "Roadmap",
+ "markdown": "# Actual body",
+ "truncated": false,
+ }));
+ assert_eq!(parsed.title.as_deref(), Some("Roadmap"));
+ assert_eq!(parsed.body, "# Actual body");
+
+ let parsed = parse_notion_fetch_result(serde_json::json!([
+ {"title": "Content team"},
+ {"text": "First paragraph."},
+ {"text": "Second paragraph."}
+ ]));
+ assert_eq!(parsed.title.as_deref(), Some("Content team"));
+ assert_eq!(parsed.body, "First paragraph.\n\nSecond paragraph.");
+}
+
+#[test]
+fn notion_property_cardinality_comes_from_the_source_shape() {
+ let parsed = parse_notion_fetch_result(serde_json::json!({
+ "title": "Roadmap",
+ "text": "# Roadmap",
+ "properties": {
+ "Topics": ["Planning"],
+ "Homepage": "https://notion.so/home",
+ "References": ["https://notion.so/spec"]
+ }
+ }));
+
+ assert_eq!(
+ parsed.properties.values,
+ vec![
+ ImportedDocumentProperty {
+ name: "Homepage".into(),
+ value: ImportedDocumentPropertyValue::Link {
+ urls: vec!["https://notion.so/home".into()],
+ multi: false,
+ },
+ },
+ ImportedDocumentProperty {
+ name: "References".into(),
+ value: ImportedDocumentPropertyValue::Link {
+ urls: vec!["https://notion.so/spec".into()],
+ multi: true,
+ },
+ },
+ ImportedDocumentProperty {
+ name: "Topics".into(),
+ value: ImportedDocumentPropertyValue::Select {
+ values: vec!["Planning".into()],
+ multi: true,
+ },
+ },
+ ]
+ );
+}
+
+#[test]
+fn notion_fetch_unwraps_hosted_mcp_page_content_and_properties() {
+ let response = serde_json::json!({
+ "metadata": {"type": "page"},
+ "title": "Q1 Product + Engineering Planning",
+ "url": "https://app.notion.com/p/4d005e63b2fd4d079df4e376e35b7519",
+ "text": "Here is the result of \"view\" for the Page with URL https://app.notion.com/p/4d005e63b2fd4d079df4e376e35b7519 as of 2026-07-25T19:00:00Z:\n<page url=\"https://app.notion.com/p/4d005e63b2fd4d079df4e376e35b7519\">\n<properties>\n{\"Name\":\"Q1 Product + Engineering Planning\",\"Status\":\"Not started\",\"Tags\":[\"Planning\",\"Q1\"]}\n</properties>\n<content>\n## Pre-requisite reading\n\n- <page url=\"https://notion.so/preflight\">Macro 4.0 pre-flight items 🚀</page>\n- [x] Ship it\n</content>\n</page>"
+ });
+
+ let parsed = parse_notion_fetch_result(response);
+ assert_eq!(
+ parsed.title.as_deref(),
+ Some("Q1 Product + Engineering Planning")
+ );
+ assert_eq!(
+ parsed.body,
+ "## Pre-requisite reading\n\n- <page url=\"https://notion.so/preflight\">Macro 4.0 pre-flight items 🚀</page>\n- [x] Ship it"
+ );
+ assert_eq!(parsed.properties.tags, vec!["Planning", "Q1"]);
+ assert_eq!(
+ parsed.properties.values,
+ vec![ImportedDocumentProperty {
+ name: "Status".into(),
+ value: ImportedDocumentPropertyValue::String {
+ value: "Not started".into(),
+ },
+ }]
+ );
+ assert!(!parsed.is_database);
+ assert!(!parsed.truncated);
+}
+
+#[test]
+fn notion_fetch_marks_databases_and_truncated_markdown() {
+ let database = parse_notion_fetch_result(serde_json::json!({
+ "metadata": {"type": "database"},
+ "text": "A database"
+ }));
+ assert!(database.is_database);
+
+ let truncated = parse_notion_fetch_result(serde_json::json!({
+ "object": "page_markdown",
+ "markdown": "# Partial",
+ "truncated": true,
+ "unknown_block_ids": ["abc"]
+ }));
+ assert!(truncated.truncated);
+}
+
+#[test]
+fn notion_fetch_recognizes_documented_tool_aliases() {
+ assert!(is_notion_fetch_tool_name("mcp__Notion__notion-fetch"));
+ assert!(is_notion_fetch_tool_name("mcp__Notion__fetch"));
+ assert!(!is_notion_fetch_tool_name("mcp__Notion__search"));
+}
+
+#[test]
+fn notion_page_references_become_external_markdown_links() {
+ let input = r#"<ancestor-path>
+<ancestor-3-page url="https://app.notion.com/p/93fa70f914eb477f89049c38912f9bb1" title=""/>
+<ancestor-4-page url="https://app.notion.com/p/dcff3c11ce9847f9b17b4a10eafa4410" title="Parent"/>
+</ancestor-path>
+See <mention-page url="https://notion.so/roadmap">Roadmap</mention-page>.
+<page url="https://notion.so/spec">Product spec</page>
+<page url="https://notion.so/draft">Plan \ [draft]</page>"#;
+
+ assert_eq!(
+ normalize_notion_markdown(input),
+ "[Notion page](https://app.notion.com/p/93fa70f914eb477f89049c38912f9bb1)\n\
+[Parent](https://app.notion.com/p/dcff3c11ce9847f9b17b4a10eafa4410)\n\
+See [Roadmap](https://notion.so/roadmap).\n\
+[Product spec](https://notion.so/spec)\n\
+[Plan \\\\ \\[draft\\]](https://notion.so/draft)"
+ );
+}
+
+#[test]
+fn notion_prompt_imports_only_source_backed_body_content() {
+ let prompt = prompts::NOTION_IMPORT_SYSTEM;
+
+ assert!(prompt.contains("canonical Notion `notion-fetch` tool"));
+ assert!(prompt.contains("Never use a search snippet or a generic `view` result"));
+ assert!(prompt.contains("ONLY body content that actually appears in the fetched page"));
+ assert!(prompt.contains(r#"Here is the result of "view"..."#));
+ assert!(prompt.contains(r#"[{"title":"Content team"}]"#));
+ assert!(prompt.contains("use only the contents of `<content>`"));
+ assert!(prompt.contains("do not repeat it in `content_markdown`"));
+ assert!(prompt.contains("inferred or invented"));
+ assert!(prompt.contains("Remove every `<database>`"));
+ assert!(prompt.contains("skip the whole page"));
+ assert!(prompt.contains("arrays are multi-valued even when"));
+ assert!(!prompt.contains("staged summary plus the backlink"));
+}
+
+#[test]
+fn notion_tool_result_wrappers_are_rejected_as_content() {
+ let view_wrapper = r#"Here is the result of "view" for the Page with URL https://app.notion.com/p/39dd7bb70863806ab8afe6d96ecf9701 as of 2026-07-14T21:50:42.234Z:
+[{"title":""}]"#;
+ assert!(prepare_notion_markdown(view_wrapper).is_err());
+ assert!(prepare_notion_markdown(r#"[{"title":"Content team"}]"#).is_err());
+ assert!(prepare_notion_markdown("# Actual page content").is_ok());
+}
+
+#[test]
+fn notion_toggles_and_databases_are_not_imported_as_markup() {
+ let input = r#"# Useful notes {toggle="true"}
+Keep this text.
+<database url="https://notion.so/database">Content calendar</database>
+<mention-database url="https://notion.so/roadmap">Roadmap</mention-database>
+End."#;
+
+ assert_eq!(
+ normalize_notion_markdown(input),
+ "# Useful notes\nKeep this text.\n\n\nEnd."
+ );
+}
+
+#[test]
+fn notion_enhanced_markdown_becomes_macro_markdown() {
+ let input = r#"<details color="Gray">
+<summary>Coming in Q2 (starts <mention-date start="2024-04-01"/>)</summary>
+ ### Revamp Java PDF Parser
+ - [ ] Open task
+ - [x] Assigned to <mention-user url="https://notion.so/user">Teo Nys</mention-user>
+ <callout icon="🔢" color="Gray">
+ Numbers are estimates in **weeks**.
+ </callout>
+</details>
+
+<file src="https://notion.so/organizing-polish.docx">Organizing-polish.docx</file>
+<span underline="true">Readable text</span>
+<unknown url="https://notion.so/unsupported" alt="Linked preview"/>
+<empty-block/>"#;
+
+ assert_eq!(
+ normalize_notion_markdown(input),
+ "Coming in Q2 (starts April 1, 2024)\n\
+### Revamp Java PDF Parser\n\
+- [ ] Open task\n\
+- [x] Assigned to @Teo Nys\n\
+> 🔢 Numbers are estimates in **weeks**.\n\
+\n\
+[Organizing-polish.docx](https://notion.so/organizing-polish.docx)\n\
+Readable text\n\
+[Linked preview](https://notion.so/unsupported)"
+ );
+}
+
+#[test]
+fn notion_escaped_todos_are_repaired_but_code_examples_are_untouched() {
+ let input = r#"\[x\] Completed
+ \[ \] Nested
+
+```text
+\[x\] This is an example
+<page url="https://notion.so/example">also an example</page>
+```"#;
+
+ assert_eq!(
+ normalize_notion_markdown(input),
+ "- [x] Completed\n\
+\t- [ ] Nested\n\
+\n\
+```text\n\
+\\[x\\] This is an example\n\
+<page url=\"https://notion.so/example\">also an example</page>\n\
+```"
+ );
+}
+
+#[test]
+fn notion_database_heavy_pages_are_detected() {
+ assert!(notion_page_is_mostly_database(
+ r#"<database url="https://notion.so/database">Content calendar</database>"#
+ ));
+ assert!(notion_page_is_mostly_database(
+ r#"A short introduction.
+<database url="https://notion.so/database">Content calendar</database>"#
+ ));
+ assert!(!notion_page_is_mostly_database(
+ "A substantive standalone page without any database content."
+ ));
+
+ let substantive = format!(
+ "{}\n<database url=\"https://notion.so/database\">Content calendar</database>",
+ "Useful non-database documentation. ".repeat(20)
+ );
+ assert!(!notion_page_is_mostly_database(&substantive));
+}
+
+#[test]
+fn notion_tables_become_rectangular_macro_pipe_tables() {
+ let input = r#"<table fit-page-width="true" header-row="true">
+<colgroup>
+<col width="379">
+<col width="230">
+</colgroup>
+<tr>
+<td>AI PDF Editors</td>
+<td>Features</td>
+<td>Pricing</td>
+</tr>
+<tr>
+lc
+</tr>
+<tr>
+<td>PDFelement</td>
+<td>• Automatically bookmarks important pages
+• Use Lumi AI to chat with PDFs</td>
+<td>Paid plans start at $79.99 per year.</td>
+</tr>
+<tr><td>Forma | Pro</td><td>Draft with AI</td></tr>
+</table>"#;
+
+ assert_eq!(
+ normalize_notion_markdown(input),
+ "| AI PDF Editors | Features | Pricing |\n\
+| --- | --- | --- |\n\
+| PDFelement | • Automatically bookmarks important pages\\n• Use Lumi AI to chat with PDFs | Paid plans start at $79.99 per year. |\n\
+| Forma &#124; Pro | Draft with AI | |"
+ );
}
diff --git a/crates/import/src/inbound/toolset.rs b/crates/import/src/inbound/toolset.rs
index 66b9af731e..9268b29925 100644
--- a/crates/import/src/inbound/toolset.rs
+++ b/crates/import/src/inbound/toolset.rs
@@ -2,7 +2,8 @@
//!
//! 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`;
+//! - chat sessions get the ledger tools plus the single-page Notion import
+//! workflow, staging as `initiator = chat`;
//! - gather jobs get only `CreateImportEntity`, locked to one source and
//! `initiator = onboarding`, staging only;
//! - Notion import jobs get only `FinalizeImport`.
@@ -11,8 +12,11 @@
//! 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 crate::domain::ports::{ImportError, ImportedDocumentProperties, ImportedDocumentProperty};
+use crate::domain::service::{
+ DiscardOutcome, ImportFinalizer, ImportNotionPageOutcome, ImportStager, NotionPageImporter,
+ StageOutcome,
+};
use ai_toolset::{
AsyncTool, AsyncToolCollection, RequestContext, ServiceContext, ToolCallError, ToolResult,
};
@@ -117,9 +121,11 @@ impl<T> ImportToolContext<T> {
}
/// The import toolset for interactive chat.
-pub fn import_toolset<T: ImportStager>() -> AsyncToolCollection<ImportToolContext<T>> {
+pub fn import_toolset<T: ImportStager + NotionPageImporter>()
+-> AsyncToolCollection<ImportToolContext<T>> {
AsyncToolCollection::new()
.add_tool::<CreateImportEntity, ImportToolContext<T>>()
+ .add_tool::<ImportNotionPage, ImportToolContext<T>>()
.add_tool::<DeleteImportEntity, ImportToolContext<T>>()
.add_tool::<ListImportEntities, ImportToolContext<T>>()
}
@@ -384,6 +390,107 @@ fn stage_response(outcome: StageOutcome, user: &str) -> CreateImportEntityRespon
}
}
+/// Import one specific Notion page as a Macro markdown document.
+#[derive(Debug, Clone, Deserialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+#[schemars(
+ title = "ImportNotionPage",
+ description = "Import one specific Notion page through Macro's canonical Notion importer. Use this when the user explicitly asks to import a page URL or id. The tool performs deduplication, fetches through the user's connected Notion MCP, normalizes the page, creates the Macro markdown document, and returns its entity id. Do not fetch and recreate the page manually with generic document tools. Notion databases and database-first pages are intentionally not imported."
+)]
+pub struct ImportNotionPage {
+ /// The exact Notion page URL or stable 32-character page id.
+ #[schemars(
+ description = "The exact Notion page URL or stable 32-character Notion page id to import."
+ )]
+ pub page_url: String,
+}
+
+/// Response from importing one Notion page.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct ImportNotionPageResponse {
+ /// What happened: `imported`, `already_imported`,
+ /// `already_imported_by_teammate`, `previously_declined`, or
+ /// `import_in_progress`.
+ pub outcome: String,
+ /// The ledger row and Macro entity id, when one exists.
+ pub entity: ImportEntityView,
+ /// Human-readable result and next action.
+ pub message: String,
+}
+
+#[async_trait]
+impl<T: NotionPageImporter> AsyncTool<ImportToolContext<T>> for ImportNotionPage {
+ type Output = ImportNotionPageResponse;
+
+ #[tracing::instrument(skip_all, fields(user_id = %request_context.user_id, page_url = %self.page_url), err)]
+ async fn call(
+ &self,
+ service_context: ServiceContext<ImportToolContext<T>>,
+ request_context: RequestContext,
+ ) -> ToolResult<Self::Output> {
+ let page_url = self.page_url.trim();
+ if page_url.is_empty() {
+ return Err(tool_error(
+ "A Notion page URL or id is required.".to_string(),
+ anyhow::anyhow!("empty Notion page URL"),
+ ));
+ }
+
+ let user = request_context.user_id.clone();
+ let outcome = service_context
+ .require_service()?
+ .import_notion_page(&user, page_url)
+ .await
+ .map_err(|e| import_error("Failed to import Notion page", e))?;
+
+ Ok(match outcome {
+ ImportNotionPageOutcome::Imported {
+ entity,
+ already_existed,
+ by_teammate,
+ } => {
+ let outcome = if by_teammate {
+ "already_imported_by_teammate"
+ } else if already_existed {
+ "already_imported"
+ } else {
+ "imported"
+ };
+ let message = if already_existed {
+ format!(
+ "This Notion page was already imported{} as Macro document `{}`. Do not create a duplicate.",
+ if by_teammate { " by a teammate" } else { "" },
+ entity.entity_id.as_deref().unwrap_or("?"),
+ )
+ } else {
+ format!(
+ "Imported the Notion page as Macro document `{}`.",
+ entity.entity_id.as_deref().unwrap_or("?"),
+ )
+ };
+ ImportNotionPageResponse {
+ outcome: outcome.to_string(),
+ entity: ImportEntityView::of(&entity, user.as_ref()),
+ message,
+ }
+ }
+ ImportNotionPageOutcome::PreviouslyDiscarded(entity) => ImportNotionPageResponse {
+ outcome: "previously_declined".to_string(),
+ message: "The user previously declined this page, so it was not imported."
+ .to_string(),
+ entity: ImportEntityView::of(&entity, user.as_ref()),
+ },
+ ImportNotionPageOutcome::ImportInProgress(entity) => ImportNotionPageResponse {
+ outcome: "import_in_progress".to_string(),
+ message: "This Notion page is already being imported. Do not start a duplicate."
+ .to_string(),
+ entity: ImportEntityView::of(&entity, user.as_ref()),
+ },
+ })
+ }
+}
+
/// Decline a staged import candidate.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
@@ -518,6 +625,18 @@ pub struct FinalizeImport {
/// The document body.
#[schemars(description = "The full markdown body for the created document.")]
pub content_markdown: String,
+
+ /// Notion database properties to attach to the document.
+ #[serde(default)]
+ #[schemars(
+ description = "Useful non-title Notion page properties, typed for Macro. Omit unsupported or empty values."
+ )]
+ pub properties: Vec<ImportedDocumentProperty>,
+
+ /// Notion labels/tags to attach as Macro tags.
+ #[serde(default)]
+ #[schemars(description = "Labels from Notion properties named Tags, Tag, Labels, or Label.")]
+ pub tags: Vec<String>,
}
/// Response from finalizing an item.
@@ -547,6 +666,10 @@ impl<T: ImportFinalizer> AsyncTool<ImportToolContext<T>> for FinalizeImport {
self.import_id,
self.name.trim(),
&self.content_markdown,
+ &ImportedDocumentProperties {
+ values: self.properties.clone(),
+ tags: self.tags.clone(),
+ },
)
.await
.map_err(|e| import_error("Failed to finalize import", e))?;
diff --git a/crates/import/src/inbound/toolset/test.rs b/crates/import/src/inbound/toolset/test.rs
index b6f3db8b09..7bbfaac802 100644
--- a/crates/import/src/inbound/toolset/test.rs
+++ b/crates/import/src/inbound/toolset/test.rs
@@ -20,7 +20,8 @@ fn row(source: ImportSource, status: ImportStatus) -> ImportEntity {
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()),
+ entity_type: matches!(status, ImportStatus::Imported)
+ .then(|| source.entity_type().to_string()),
last_error: None,
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
@@ -31,6 +32,7 @@ fn row(source: ImportSource, status: ImportStatus) -> ImportEntity {
#[derive(Default)]
struct MockStager {
staged_calls: Mutex<Vec<(Initiator, ImportSource, String)>>,
+ notion_import_calls: Mutex<Vec<String>>,
stage_outcome: Option<StageOutcome>,
}
@@ -83,6 +85,24 @@ impl ImportStager for MockStager {
}
}
+impl NotionPageImporter for MockStager {
+ async fn import_notion_page(
+ &self,
+ _user: &MacroUserIdStr<'static>,
+ page_url_or_id: &str,
+ ) -> Result<ImportNotionPageOutcome> {
+ self.notion_import_calls
+ .lock()
+ .unwrap()
+ .push(page_url_or_id.to_string());
+ Ok(ImportNotionPageOutcome::Imported {
+ entity: row(ImportSource::Notion, ImportStatus::Imported),
+ already_existed: false,
+ by_teammate: false,
+ })
+ }
+}
+
fn contexts(
policy: ToolPolicy,
) -> (
@@ -200,6 +220,42 @@ async fn teammate_dedup_response_tells_the_agent_not_to_duplicate() {
assert!(response.entity.imported_by_teammate);
}
+#[tokio::test]
+async fn import_notion_page_runs_the_workflow_and_returns_the_document() {
+ let (service_context, request_context, stager) = contexts(ToolPolicy::chat());
+ let page_url = "https://app.notion.com/p/4d005e63b2fd4d079df4e376e35b7519";
+
+ let response = ImportNotionPage {
+ page_url: format!(" {page_url} "),
+ }
+ .call(service_context, request_context)
+ .await
+ .expect("Notion import succeeds");
+
+ assert_eq!(response.outcome, "imported");
+ assert_eq!(response.entity.entity_id.as_deref(), Some("doc-1"));
+ assert_eq!(response.entity.entity_type.as_deref(), Some("md"));
+ assert_eq!(
+ stager.notion_import_calls.lock().unwrap().as_slice(),
+ &[page_url.to_string()]
+ );
+}
+
+#[tokio::test]
+async fn import_notion_page_requires_a_page_url() {
+ let (service_context, request_context, stager) = contexts(ToolPolicy::chat());
+
+ let error = ImportNotionPage {
+ page_url: " ".to_string(),
+ }
+ .call(service_context, request_context)
+ .await
+ .expect_err("empty page URL must fail");
+
+ assert!(error.description.contains("required"), "{error:?}");
+ assert!(stager.notion_import_calls.lock().unwrap().is_empty());
+}
+
#[tokio::test]
async fn delete_reports_non_discardable_status() {
let (service_context, request_context, _) = contexts(ToolPolicy::chat());
diff --git a/crates/import/src/outbound/document_properties.rs b/crates/import/src/outbound/document_properties.rs
new file mode 100644
index 0000000000..8e924ecbdd
--- /dev/null
+++ b/crates/import/src/outbound/document_properties.rs
@@ -0,0 +1,468 @@
+//! Applies properties recovered from an external document to a Macro document.
+//!
+//! This adapter translates import-domain values into the Properties service's
+//! definitions, options, and stored values. Imported enrichment is best-effort:
+//! an invalid or conflicting property is logged and skipped without invalidating
+//! the document that was already created.
+
+use std::sync::Arc;
+
+use macro_user_id::user_id::MacroUserIdStr;
+use models_properties::api::{
+ AddPropertyOptionRequest, AddStringOptionRequest, CreatePropertyDefinitionRequest,
+ CreatePropertyScope, PropertyDataType, SetPropertyValue,
+};
+use models_properties::service::property_definition::PropertyDefinition;
+use models_properties::service::property_option::{PropertyOption, PropertyOptionValue};
+use models_properties::{DataType, EntityType};
+use properties::domain::model::TagScope;
+use properties::{EditReceipt, PropertiesService};
+use uuid::Uuid;
+
+use crate::domain::ports::{
+ ImportedDocumentProperties, ImportedDocumentProperty, ImportedDocumentPropertyValue,
+};
+
+#[cfg(test)]
+mod test;
+
+/// Applies imported document properties through Macro's Properties service.
+pub struct DocumentPropertiesApplicator<P> {
+ properties: Arc<P>,
+}
+
+impl<P> Clone for DocumentPropertiesApplicator<P> {
+ fn clone(&self) -> Self {
+ Self {
+ properties: self.properties.clone(),
+ }
+ }
+}
+
+impl<P: PropertiesService> DocumentPropertiesApplicator<P> {
+ /// Create an applicator backed by the shared Properties service.
+ pub fn new(properties: Arc<P>) -> Self {
+ Self { properties }
+ }
+
+ /// Apply imported tags and ordinary properties to an editable document.
+ pub async fn apply(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ access: &EditReceipt,
+ document_id: &str,
+ imported: &ImportedDocumentProperties,
+ ) {
+ self.apply_tags(user, access, document_id, &imported.tags)
+ .await;
+ self.apply_values(user, access, document_id, &imported.values)
+ .await;
+ }
+
+ async fn apply_tags(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ access: &EditReceipt,
+ document_id: &str,
+ labels: &[String],
+ ) {
+ if labels.is_empty() {
+ return;
+ }
+
+ let tag_set = match self
+ .properties
+ .ensure_tag_set(user, None, TagScope::User)
+ .await
+ {
+ Ok(tag_set) => tag_set,
+ Err(error) => {
+ tracing::warn!(document_id, error = ?error, "failed to prepare imported document tags");
+ return;
+ }
+ };
+ let Some(definition) = tag_set.definition else {
+ tracing::warn!(document_id, "imported document tag set has no definition");
+ return;
+ };
+ let mut options = tag_set.options;
+
+ for (index, label) in labels.iter().enumerate() {
+ let option_id = self
+ .find_or_create_string_option(
+ user,
+ document_id,
+ definition.id,
+ &mut options,
+ label,
+ Some(imported_tag_color(index)),
+ )
+ .await;
+ let Some(option_id) = option_id else {
+ continue;
+ };
+
+ if let Err(error) = self
+ .properties
+ .add_entity_property_option(access, definition.id, option_id)
+ .await
+ {
+ tracing::warn!(
+ document_id,
+ label,
+ error = ?error,
+ "failed to attach imported document tag"
+ );
+ }
+ }
+ }
+
+ async fn apply_values(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ access: &EditReceipt,
+ document_id: &str,
+ imported: &[ImportedDocumentProperty],
+ ) {
+ if imported.is_empty() {
+ return;
+ }
+
+ let mut definitions = match self
+ .properties
+ .list_property_definitions(None, Some(user), false, Some(EntityType::Document))
+ .await
+ {
+ Ok(definitions) => definitions,
+ Err(error) => {
+ tracing::warn!(document_id, error = ?error, "failed to list imported document properties");
+ return;
+ }
+ };
+
+ for property in imported {
+ let descriptor = ImportedPropertyDescriptor::of(&property.value);
+ let definition = self
+ .find_or_create_definition(
+ user,
+ document_id,
+ property,
+ &descriptor,
+ &mut definitions,
+ )
+ .await;
+ let Some(definition) = definition else {
+ continue;
+ };
+
+ let value = self
+ .property_value(
+ user,
+ document_id,
+ property,
+ &definition,
+ descriptor.is_multi_select,
+ )
+ .await;
+ let Some(value) = value else {
+ continue;
+ };
+
+ if let Err(error) = self
+ .properties
+ .set_entity_property(access, definition.id, Some(value))
+ .await
+ {
+ tracing::warn!(
+ document_id,
+ property = %property.name,
+ error = ?error,
+ "failed to set imported document property"
+ );
+ }
+ }
+ }
+
+ async fn find_or_create_definition(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ document_id: &str,
+ property: &ImportedDocumentProperty,
+ descriptor: &ImportedPropertyDescriptor,
+ definitions: &mut Vec<PropertyDefinition>,
+ ) -> Option<PropertyDefinition> {
+ if let Some(definition) = definitions
+ .iter()
+ .find(|definition| definition.display_name.eq_ignore_ascii_case(&property.name))
+ .cloned()
+ {
+ if descriptor.matches(&definition) {
+ return Some(definition);
+ }
+ tracing::warn!(
+ document_id,
+ property = %property.name,
+ "skipping imported property whose existing Macro definition has a different type"
+ );
+ return None;
+ }
+
+ let request = CreatePropertyDefinitionRequest {
+ scope: CreatePropertyScope::User,
+ display_name: property.name.clone(),
+ data_type: descriptor.definition_type.clone(),
+ };
+ let definition = match self
+ .properties
+ .create_property_definition(user, None, &request)
+ .await
+ {
+ Ok(definition) => definition,
+ Err(error) => {
+ // Concurrent imports may race to create the same definition.
+ let recovered = self
+ .properties
+ .list_property_definitions(None, Some(user), false, Some(EntityType::Document))
+ .await
+ .ok()
+ .and_then(|definitions| {
+ definitions.into_iter().find(|definition| {
+ definition.display_name.eq_ignore_ascii_case(&property.name)
+ && descriptor.matches(definition)
+ })
+ });
+ let Some(definition) = recovered else {
+ tracing::warn!(
+ document_id,
+ property = %property.name,
+ error = ?error,
+ "failed to create imported document property"
+ );
+ return None;
+ };
+ definition
+ }
+ };
+ definitions.push(definition.clone());
+ Some(definition)
+ }
+
+ async fn property_value(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ document_id: &str,
+ property: &ImportedDocumentProperty,
+ definition: &PropertyDefinition,
+ is_multi_select: bool,
+ ) -> Option<SetPropertyValue> {
+ match &property.value {
+ ImportedDocumentPropertyValue::Boolean { value } => {
+ Some(SetPropertyValue::Boolean { value: *value })
+ }
+ ImportedDocumentPropertyValue::Date { value } => match dateparser::parse(value) {
+ Ok(value) => Some(SetPropertyValue::Date { value }),
+ Err(error) => {
+ tracing::warn!(
+ document_id,
+ property = %property.name,
+ error = ?error,
+ "failed to parse imported date property"
+ );
+ None
+ }
+ },
+ ImportedDocumentPropertyValue::Number { value } => {
+ Some(SetPropertyValue::Number { value: *value })
+ }
+ ImportedDocumentPropertyValue::String { value } => Some(SetPropertyValue::String {
+ value: value.clone(),
+ }),
+ ImportedDocumentPropertyValue::Link { urls, .. } if is_multi_select => {
+ Some(SetPropertyValue::MultiLink { urls: urls.clone() })
+ }
+ ImportedDocumentPropertyValue::Link { urls, .. } => urls
+ .first()
+ .cloned()
+ .map(|url| SetPropertyValue::Link { url }),
+ ImportedDocumentPropertyValue::Select { values, .. } => {
+ self.select_value(
+ user,
+ document_id,
+ property,
+ definition,
+ values,
+ is_multi_select,
+ )
+ .await
+ }
+ }
+ }
+
+ async fn select_value(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ document_id: &str,
+ property: &ImportedDocumentProperty,
+ definition: &PropertyDefinition,
+ values: &[String],
+ is_multi_select: bool,
+ ) -> Option<SetPropertyValue> {
+ let mut options = match self
+ .properties
+ .get_property_options(definition.id, user, None)
+ .await
+ {
+ Ok(options) => options,
+ Err(error) => {
+ tracing::warn!(
+ document_id,
+ property = %property.name,
+ error = ?error,
+ "failed to list imported property options"
+ );
+ return None;
+ }
+ };
+
+ let mut option_ids = Vec::new();
+ for value in values {
+ if let Some(option_id) = self
+ .find_or_create_string_option(
+ user,
+ document_id,
+ definition.id,
+ &mut options,
+ value,
+ None,
+ )
+ .await
+ {
+ option_ids.push(option_id);
+ }
+ }
+ option_ids.sort_unstable();
+ option_ids.dedup();
+
+ if is_multi_select {
+ Some(SetPropertyValue::MultiSelectOption { option_ids })
+ } else {
+ option_ids
+ .first()
+ .copied()
+ .map(|option_id| SetPropertyValue::SelectOption { option_id })
+ }
+ }
+
+ async fn find_or_create_string_option(
+ &self,
+ user: &MacroUserIdStr<'static>,
+ document_id: &str,
+ definition_id: Uuid,
+ options: &mut Vec<PropertyOption>,
+ label: &str,
+ color: Option<&str>,
+ ) -> Option<Uuid> {
+ if let Some(option) = find_string_option(options, label) {
+ return Some(option.id);
+ }
+
+ let request = AddPropertyOptionRequest::SelectString {
+ option: AddStringOptionRequest {
+ display_order: options.len() as i32,
+ value: label.to_string(),
+ color: color.map(str::to_string),
+ },
+ };
+ match self
+ .properties
+ .add_property_option(user, None, definition_id, &request)
+ .await
+ {
+ Ok(option) => {
+ let id = option.id;
+ options.push(option);
+ Some(id)
+ }
+ Err(error) => {
+ // A concurrent import may have created the option first.
+ let recovered = self
+ .properties
+ .get_property_options(definition_id, user, None)
+ .await
+ .ok()
+ .and_then(|fresh| find_string_option(&fresh, label).cloned());
+ if let Some(option) = recovered {
+ let id = option.id;
+ options.push(option);
+ Some(id)
+ } else {
+ tracing::warn!(
+ document_id,
+ label,
+ error = ?error,
+ "failed to create imported property option"
+ );
+ None
+ }
+ }
+ }
+ }
+}
+
+#[derive(Debug, Clone, PartialEq)]
+struct ImportedPropertyDescriptor {
+ /// API type used when a Macro definition must be created.
+ definition_type: PropertyDataType,
+ /// Stored type used to check compatibility with an existing definition.
+ stored_type: DataType,
+ /// Whether the existing definition and stored value accept multiple values.
+ is_multi_select: bool,
+}
+
+impl ImportedPropertyDescriptor {
+ fn of(value: &ImportedDocumentPropertyValue) -> Self {
+ let definition_type = match value {
+ ImportedDocumentPropertyValue::Boolean { .. } => PropertyDataType::Boolean,
+ ImportedDocumentPropertyValue::Date { .. } => PropertyDataType::Date,
+ ImportedDocumentPropertyValue::Number { .. } => PropertyDataType::Number,
+ ImportedDocumentPropertyValue::String { .. } => PropertyDataType::String,
+ ImportedDocumentPropertyValue::Select { multi, .. } => PropertyDataType::SelectString {
+ options: Vec::new(),
+ multi: *multi,
+ },
+ ImportedDocumentPropertyValue::Link { multi, .. } => {
+ PropertyDataType::Link { multi: *multi }
+ }
+ };
+ Self {
+ stored_type: definition_type.to_data_type(),
+ is_multi_select: definition_type.is_multi_select(),
+ definition_type,
+ }
+ }
+
+ fn matches(&self, definition: &PropertyDefinition) -> bool {
+ definition.data_type == self.stored_type
+ && definition.is_multi_select == self.is_multi_select
+ }
+}
+
+fn find_string_option<'a>(
+ options: &'a [PropertyOption],
+ label: &str,
+) -> Option<&'a PropertyOption> {
+ options.iter().find(|option| {
+ matches!(
+ &option.value,
+ PropertyOptionValue::String(value) if value.eq_ignore_ascii_case(label)
+ )
+ })
+}
+
+fn imported_tag_color(index: usize) -> &'static str {
+ const COLORS: [&str; 12] = [
+ "#0091FF", "#46A758", "#8E4EC6", "#F76B15", "#E93D82", "#12A594", "#FFB224", "#3E63DD",
+ "#E5484D", "#F5D90A", "#889096", "#E54D2E",
+ ];
+ COLORS[index % COLORS.len()]
+}
diff --git a/crates/import/src/outbound/document_properties/test.rs b/crates/import/src/outbound/document_properties/test.rs
new file mode 100644
index 0000000000..741b13d68e
--- /dev/null
+++ b/crates/import/src/outbound/document_properties/test.rs
@@ -0,0 +1,32 @@
+use super::*;
+
+#[test]
+fn descriptor_uses_declared_source_cardinality() {
+ let multi_select = ImportedPropertyDescriptor::of(&ImportedDocumentPropertyValue::Select {
+ values: vec!["Planning".to_string()],
+ multi: true,
+ });
+ assert_eq!(multi_select.stored_type, DataType::SelectString);
+ assert!(multi_select.is_multi_select);
+ assert_eq!(
+ multi_select.definition_type,
+ PropertyDataType::SelectString {
+ options: Vec::new(),
+ multi: true,
+ }
+ );
+
+ let single_link = ImportedPropertyDescriptor::of(&ImportedDocumentPropertyValue::Link {
+ urls: vec![
+ "https://notion.so/roadmap".to_string(),
+ "https://notion.so/spec".to_string(),
+ ],
+ multi: false,
+ });
+ assert_eq!(single_link.stored_type, DataType::Link);
+ assert!(!single_link.is_multi_select);
+ assert_eq!(
+ single_link.definition_type,
+ PropertyDataType::Link { multi: false }
+ );
+}
diff --git a/crates/import/src/outbound/mod.rs b/crates/import/src/outbound/mod.rs
index e8c430d839..56ed58c990 100644
--- a/crates/import/src/outbound/mod.rs
+++ b/crates/import/src/outbound/mod.rs
@@ -1,4 +1,5 @@
-//! Outbound adapters: Postgres persistence and the gateway notify hook.
+//! Outbound adapters for import persistence, notifications, and entity enrichment.
+pub mod document_properties;
pub mod gateway_notifier;
pub mod pg_import_repo;
diff --git a/crates/mcp_client/src/domain/models/call_tool_result.rs b/crates/mcp_client/src/domain/models/call_tool_result.rs
new file mode 100644
index 0000000000..488ed5bb67
--- /dev/null
+++ b/crates/mcp_client/src/domain/models/call_tool_result.rs
@@ -0,0 +1,66 @@
+//! Convenience behavior for MCP tool-call results.
+
+use rmcp::model::{CallToolResult, ResourceContents};
+
+#[cfg(test)]
+mod test;
+
+/// Conversion helpers implemented directly on [`CallToolResult`] through an
+/// extension trait.
+pub trait CallToolResultExt {
+ /// Concatenate text-bearing content blocks while preserving block boundaries.
+ fn text_content(&self) -> String;
+
+ /// Convert a successful result into the JSON value exposed to Macro tools.
+ ///
+ /// Structured content is preserved as JSON. Results without structured
+ /// content fall back to their text-bearing content blocks.
+ fn into_value(self) -> serde_json::Value;
+
+ /// Produce a useful error description from text or structured content.
+ fn error_description(&self) -> String;
+}
+
+impl CallToolResultExt for CallToolResult {
+ fn text_content(&self) -> String {
+ self.content
+ .iter()
+ .filter_map(|content| {
+ content
+ .raw
+ .as_text()
+ .map(|text| text.text.clone())
+ .or_else(|| {
+ let resource = content.raw.as_resource()?;
+ match &resource.resource {
+ ResourceContents::TextResourceContents { text, .. }
+ if !text.is_empty() =>
+ {
+ Some(text.clone())
+ }
+ _ => None,
+ }
+ })
+ })
+ .collect::<Vec<_>>()
+ .join("\n")
+ }
+
+ fn into_value(self) -> serde_json::Value {
+ let text = self.text_content();
+ self.structured_content
+ .unwrap_or(serde_json::Value::String(text))
+ }
+
+ fn error_description(&self) -> String {
+ let text = self.text_content();
+ if text.is_empty() {
+ self.structured_content
+ .as_ref()
+ .map(ToString::to_string)
+ .unwrap_or_else(|| "MCP tool returned an error".to_string())
+ } else {
+ text
+ }
+ }
+}
diff --git a/crates/mcp_client/src/domain/models/call_tool_result/test.rs b/crates/mcp_client/src/domain/models/call_tool_result/test.rs
new file mode 100644
index 0000000000..e26e068fab
--- /dev/null
+++ b/crates/mcp_client/src/domain/models/call_tool_result/test.rs
@@ -0,0 +1,46 @@
+use super::*;
+use rmcp::model::Content;
+use serde_json::json;
+
+#[test]
+fn structured_tool_results_are_preserved() {
+ let result = CallToolResult::structured(json!({
+ "object": "page_markdown",
+ "markdown": "# Roadmap",
+ "truncated": false,
+ }));
+
+ assert_eq!(
+ result.into_value(),
+ json!({
+ "object": "page_markdown",
+ "markdown": "# Roadmap",
+ "truncated": false,
+ })
+ );
+}
+
+#[test]
+fn unstructured_text_results_preserve_block_boundaries() {
+ let result = CallToolResult::success(vec![Content::text("first"), Content::text("second")]);
+
+ assert_eq!(result.into_value(), json!("first\nsecond"));
+}
+
+#[test]
+fn embedded_text_resources_are_not_discarded() {
+ let result = CallToolResult::success(vec![
+ Content::text("# Page"),
+ Content::embedded_text("notion://page/abc", "Page content"),
+ ]);
+
+ assert_eq!(result.into_value(), json!("# Page\nPage content"));
+}
+
+#[test]
+fn structured_errors_have_a_description_without_text_content() {
+ let mut result = CallToolResult::structured(json!({"message": "not found"}));
+ result.is_error = Some(true);
+
+ assert_eq!(result.error_description(), r#"{"message":"not found"}"#);
+}
diff --git a/crates/mcp_client/src/domain/models/mod.rs b/crates/mcp_client/src/domain/models/mod.rs
index 792735de27..3ce889aa44 100644
--- a/crates/mcp_client/src/domain/models/mod.rs
+++ b/crates/mcp_client/src/domain/models/mod.rs
@@ -1,9 +1,11 @@
mod aes_key;
+mod call_tool_result;
mod consts;
mod result;
mod server;
pub use aes_key::{AesKey, AesKeyError};
+pub use call_tool_result::CallToolResultExt;
pub use consts::*;
pub use macro_user_id::user_id::MacroUserIdStr;
pub use result::{Error, Result};
diff --git a/crates/mcp_client/src/domain/service/toolset.rs b/crates/mcp_client/src/domain/service/toolset.rs
index e2d80b57a6..0f8faf7fef 100644
--- a/crates/mcp_client/src/domain/service/toolset.rs
+++ b/crates/mcp_client/src/domain/service/toolset.rs
@@ -1,4 +1,4 @@
-use crate::domain::models::{Error, McpServer, McpServerRecord};
+use crate::domain::models::{CallToolResultExt, Error, McpServer, McpServerRecord};
use crate::domain::ports::{McpConnector, McpServerStore};
use ai_toolset::{
AsyncToolCollection, RequestContext, RequestSchema, SearchableTool, ToolCallError, ToolInfo,
@@ -208,20 +208,14 @@ impl<Context: Send + Sync + 'static> ToolSet<Context> for McpToolSet {
}
};
- let text = result
- .content
- .into_iter()
- .filter_map(|c| c.raw.as_text().map(|t| t.text.clone()))
- .collect::<Vec<_>>()
- .join("");
-
if result.is_error.unwrap_or(false) {
+ let description = result.error_description();
Ok(Err(ToolCallError {
- internal_error: anyhow::anyhow!("{}", &text),
- description: text,
+ internal_error: anyhow::anyhow!("{}", &description),
+ description,
}))
} else {
- Ok(Ok(serde_json::Value::String(text)))
+ Ok(Ok(result.into_value()))
}
})
}
diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rs
index 8f50846166..848e2d0ad8 100644
--- a/services/document_cognition_service/src/api/context/test.rs
+++ b/services/document_cognition_service/src/api/context/test.rs
@@ -382,6 +382,10 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
properties_service.clone(),
entity_access_service.clone(),
),
+ document_properties:
+ import::outbound::document_properties::DocumentPropertiesApplicator::new(
+ properties_service.clone(),
+ ),
team_repository: ai_tools::build_team_repository(pool.clone()),
};
let import_service = Arc::new(import::domain::service::ImportServiceImpl::new(
diff --git a/services/document_cognition_service/src/main.rs b/services/document_cognition_service/src/main.rs
index de1347bc3e..042dde5277 100644
--- a/services/document_cognition_service/src/main.rs
+++ b/services/document_cognition_service/src/main.rs
@@ -295,6 +295,10 @@ async fn main() -> anyhow::Result<()> {
// 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 document_properties_for_import =
+ import::outbound::document_properties::DocumentPropertiesApplicator::new(
+ 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")?,
@@ -459,6 +463,7 @@ async fn main() -> anyhow::Result<()> {
entity_access_service: entity_access_service.clone(),
channel_service: channel_tool_context.service.clone(),
task_properties: task_properties_for_import,
+ document_properties: document_properties_for_import,
team_repository: ai_tools::build_team_repository(db.clone()),
};
let import_service = Arc::new(