Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(documents): read ReadContent comments from the message tables (#6858)
gbirman5300660bd035
diff --git a/.sqlx/query-6ff2b4ae4df25f6fe125338e274f4149fe8829da208e8689ccd7e2154e8d7136.json b/.sqlx/query-6ff2b4ae4df25f6fe125338e274f4149fe8829da208e8689ccd7e2154e8d7136.jsondeleted file mode 100644index 8d0fb607f9..0000000000--- a/.sqlx/query-6ff2b4ae4df25f6fe125338e274f4149fe8829da208e8689ccd7e2154e8d7136.json+++ /dev/null@@ -1,76 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT\n c.id as \"comment_id!\",\n c.\"threadId\" as \"thread_id!\",\n c.owner as \"owner!\",\n c.sender,\n c.text as \"text!\",\n c.metadata,\n c.\"createdAt\"::timestamptz as \"created_at\",\n c.\"updatedAt\"::timestamptz as \"updated_at\",\n c.\"deletedAt\"::timestamptz as \"deleted_at\",\n c.order\n FROM \"Comment\" c\n JOIN \"Thread\" t ON c.\"threadId\" = t.id\n WHERE t.\"documentId\" = $1\n AND t.\"deletedAt\" IS NULL\n AND c.\"deletedAt\" IS NULL\n ORDER BY c.\"createdAt\" ASC\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "comment_id!",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "thread_id!",- "type_info": "Int8"- },- {- "ordinal": 2,- "name": "owner!",- "type_info": "Text"- },- {- "ordinal": 3,- "name": "sender",- "type_info": "Text"- },- {- "ordinal": 4,- "name": "text!",- "type_info": "Text"- },- {- "ordinal": 5,- "name": "metadata",- "type_info": "Jsonb"- },- {- "ordinal": 6,- "name": "created_at",- "type_info": "Timestamptz"- },- {- "ordinal": 7,- "name": "updated_at",- "type_info": "Timestamptz"- },- {- "ordinal": 8,- "name": "deleted_at",- "type_info": "Timestamptz"- },- {- "ordinal": 9,- "name": "order",- "type_info": "Int4"- }- ],- "parameters": {- "Left": [- "Text"- ]- },- "nullable": [- false,- false,- false,- true,- false,- true,- null,- null,- null,- true- ]- },- "hash": "6ff2b4ae4df25f6fe125338e274f4149fe8829da208e8689ccd7e2154e8d7136"-}diff --git a/.sqlx/query-d0a3d7eadc0e7d5d469f5ae755561b859efa9b55a2b085a1f564b42024be3e92.json b/.sqlx/query-d0a3d7eadc0e7d5d469f5ae755561b859efa9b55a2b085a1f564b42024be3e92.jsondeleted file mode 100644index 909828efa5..0000000000--- a/.sqlx/query-d0a3d7eadc0e7d5d469f5ae755561b859efa9b55a2b085a1f564b42024be3e92.json+++ /dev/null@@ -1,64 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT\n t.id as \"thread_id!\",\n t.resolved as \"resolved!\",\n t.\"documentId\" as \"document_id!\",\n t.\"createdAt\"::timestamptz as \"created_at\",\n t.\"updatedAt\"::timestamptz as \"updated_at\",\n t.\"deletedAt\"::timestamptz as \"deleted_at\",\n t.metadata as \"metadata\",\n t.owner as \"owner!\"\n FROM \"Thread\" t\n WHERE t.\"documentId\" = $1 AND t.\"deletedAt\" IS NULL\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "thread_id!",- "type_info": "Int8"- },- {- "ordinal": 1,- "name": "resolved!",- "type_info": "Bool"- },- {- "ordinal": 2,- "name": "document_id!",- "type_info": "Text"- },- {- "ordinal": 3,- "name": "created_at",- "type_info": "Timestamptz"- },- {- "ordinal": 4,- "name": "updated_at",- "type_info": "Timestamptz"- },- {- "ordinal": 5,- "name": "deleted_at",- "type_info": "Timestamptz"- },- {- "ordinal": 6,- "name": "metadata",- "type_info": "Jsonb"- },- {- "ordinal": 7,- "name": "owner!",- "type_info": "Text"- }- ],- "parameters": {- "Left": [- "Text"- ]- },- "nullable": [- false,- false,- false,- null,- null,- null,- true,- false- ]- },- "hash": "d0a3d7eadc0e7d5d469f5ae755561b859efa9b55a2b085a1f564b42024be3e92"-}diff --git a/Cargo.lock b/Cargo.lockindex 5fa4e20a39..51fc95672f 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5796,6 +5796,7 @@ dependencies = ["axum-extra","base64 0.22.1","bot_id",+ "channel_sender","chrono","clap","cloudfront_sign",@@ -5828,6 +5829,7 @@ dependencies = ["macro_tower_layers","macro_user_id","macro_uuid",+ "messages","mockall","model","model-entity",diff --git a/apps/docs/AI/mcp/tools/read-content.mdx b/apps/docs/AI/mcp/tools/read-content.mdxindex bbbc00cb88..24f0e87ea4 100644--- a/apps/docs/AI/mcp/tools/read-content.mdx+++ b/apps/docs/AI/mcp/tools/read-content.mdx@@ -1,11 +1,11 @@---title: ReadContent-description: "Retrieve a documents content"+description: "Retrieve a document's content and its comment threads, including inline comments with the text they are on, Discussion comments, replies and resolved state."---# ReadContent-Retrieve a documents content+Retrieve a document's content and its comment threads, including inline comments with the text they are on, Discussion comments, replies and resolved state.## Parametersdiff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.tsindex 2b19f2ae30..24227bc462 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@@ -4834,38 +4834,66 @@ export const ReadContentResponse = z.object({}),comments: z.array(z.object({- thread: z.object({- threadId: z.number().int(),- owner: z.string(),- resolved: z.boolean(),- documentId: z.string(),- createdAt: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- updatedAt: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- deletedAt: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- metadata: z.any().optional(),+ id: z.string().uuid(),+ kind: z.any().superRefine((x, ctx) => {+ const schemas = [z.literal('inline'), z.literal('discussion')];+ 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',+ });+ }+ }),+ resolved: z.boolean(),+ anchor: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.object({ type: z.literal('document') }),+ z.object({+ markId: z.string().uuid(),+ markedText: z.union([z.string(), z.null()]).optional(),+ originalMarkedText: z.union([z.string(), z.null()]).optional(),+ removed: z.boolean().optional(),+ type: z.literal('text'),+ }),+ z.object({+ anchorId: z.string().uuid(),+ type: z.literal('pdfHighlight'),+ }),+ z.object({ anchorId: z.string().uuid(), type: z.literal('pdfPin') }),+ ];+ 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',+ });+ }}),comments: z.array(z.object({- commentId: z.number().int(),- threadId: z.number().int(),- order: z.union([z.number().int(), z.null()]).optional(),- owner: z.string(),- sender: z.union([z.string(), z.null()]).optional(),- text: z.string(),- metadata: z.any().optional(),- createdAt: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- updatedAt: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- deletedAt: z+ id: z.string().uuid(),+ author: z.string(),+ authorName: z.union([z.string(), z.null()]).optional(),+ content: z.union([z.string(), z.null()]).optional(),+ createdAt: z.string().datetime({ offset: true }),+ editedAt: z.union([z.string().datetime({ offset: true }), z.null()]).optional(),})diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.tsindex 2b4206a1f8..5f293b32c9 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@@ -955,6 +955,51 @@ export type MarkdownNode =id: string;type: 'dssImage';};+/**+ * Whether a thread is on part of a document or on the document as a whole.+ */+export type CommentThreadKind = 'inline' | 'discussion';+/**+ * Where a discussion sits in its document.+ */+export type CommentAnchor =+ | {+ type: 'document';+ }+ | {+ /**+ * The comment mark in the document.+ */+ markId: string;+ /**+ * The text the comment is on, as the document reads now; the text+ * when the comment was written if the document could not be read.+ */+ markedText?: string | null;+ /**+ * The text when the comment was written, when it differs from now.+ */+ originalMarkedText?: string | null;+ /**+ * The commented text has since been removed from the document.+ */+ removed?: boolean;+ type: 'text';+ }+ | {+ /**+ * The highlight annotation.+ */+ anchorId: string;+ type: 'pdfHighlight';+ }+ | {+ /**+ * The pin annotation.+ */+ anchorId: string;+ type: 'pdfPin';+ };/*** API-visible content lifecycle state derived from current document metadata.*/@@ -5214,7 +5259,7 @@ export interface ChatMessagePreview {attachmentIds: string[];}/**- * Retrieve a documents content+ * Retrieve a document's content and its comment threads, including inline comments with the text they are on, Discussion comments, replies and resolved state.*/export interface ReadContent {/**@@ -5225,105 +5270,60 @@ export interface ReadContent {export interface ReadContentResponse {content: Content;/**- * Any comments on the document+ * The comment threads on the document, oldest first: inline comments+ * with the text they are on, and Discussion comments on the whole+ * document. Each thread lists its first comment followed by the replies.*/- comments: CommentThread[];+ comments: DocumentDiscussion[];}/**- * A thread bundled together with its ordered comments.+ * A comment thread on a document: its first comment followed by the replies.*/-export interface CommentThread {- thread: Thread;+export interface DocumentDiscussion {/**- * The comments in the thread, ordered by `createdAt` ASC.+ * The thread id, which is the id of its first comment. Replies and+ * resolution address the thread by this id.*/- comments: Comment[];-}-/**- * A comment thread attached to a document.- */-export interface Thread {- /**- * The unique id of the thread.- */- threadId: number;- /**- * The user id of the thread owner.- */- owner: string;+ id: string;+ kind: CommentThreadKind;/*** Whether the thread has been resolved.*/resolved: boolean;+ anchor: CommentAnchor;/**- * The document the thread is attached to.+ * The comments in order, first comment first.*/- documentId: string;- /**- * When the thread was created.- */- createdAt?: string | null;- /**- * When the thread was last updated.- */- updatedAt?: string | null;- /**- * When the thread was deleted, if ever.- */- deletedAt?: string | null;- /**- * Arbitrary thread metadata.- */- metadata?: {- [k: string]: unknown;- };+ comments: DocumentComment[];}/**- * A single comment in a thread.+ * A single comment in a discussion.*/-export interface Comment {- /**- * The unique id of the comment.- */- commentId: number;+export interface DocumentComment {/**- * The thread this comment belongs to.+ * The comment id.*/- threadId: number;- /**- * Ordering position within the thread.- */- order?: number | null;- /**- * The user id of the comment owner.- */- owner: string;+ id: string;/**- * Sender display string.+ * The user or bot id of the author.*/- sender?: string | null;+ author: string;/**- * Comment body.+ * The author's display name, for bots and comments imported from other documents.*/- text: string;+ authorName?: string | null;/**- * Arbitrary comment metadata.+ * The comment body in markdown; absent when the comment was deleted.*/- metadata?: {- [k: string]: unknown;- };- /**- * When the comment was created.- */- createdAt?: string | null;+ content?: string | null;/**- * When the comment was last updated.+ * When the comment was written.*/- updatedAt?: string | null;+ createdAt: string;/**- * When the comment was deleted, if ever.+ * When the comment was last edited.*/- deletedAt?: string | null;+ editedAt?: string | null;}/*** Retrieve a documents metadatadiff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rsindex 8a30f55d18..c8447a0b03 100644--- a/crates/ai_tools/src/build_context.rs+++ b/crates/ai_tools/src/build_context.rs@@ -305,6 +305,7 @@ pub async fn build_tool_service_context_from_env(sync_client.as_ref().clone(),ReqwestEditingWorkerClient::new(ai_editing_worker_url, Arc::new(reqwest::Client::new())),env.document_permission_jwt.to_string(),+ crate::tool_context::message_reader(pool.clone()),);let properties_tool_context = crate::tool_context::build_properties_tool_context(diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rsindex 5659080575..85bd7f658f 100644--- a/crates/ai_tools/src/lib.rs+++ b/crates/ai_tools/src/lib.rs@@ -70,7 +70,7 @@ pub use tool_context::{build_channel_tool_context_without_side_effects, build_crm_tool_context,build_project_tool_context, build_properties_service, build_properties_tool_context,build_reminders_tool_context, build_skill_tool_context, build_task_properties_adapter,- build_team_repository, build_team_tool_context,+ build_team_repository, build_team_tool_context, message_reader,};pub type AiToolSet = AsyncToolCollection<ToolServiceContext>;diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rsindex 059929c2ed..a9fd7b0b1c 100644--- a/crates/ai_tools/src/tool_context.rs+++ b/crates/ai_tools/src/tool_context.rs@@ -209,6 +209,16 @@ pub fn build_channel_tool_context_without_side_effects()}+/// Read-only view of the shared message tables for tools that only read+/// conversations, such as a document's comments. It publishes no events, so it+/// is exposed only through [`messages::domain::api::MessageReader`].+pub fn message_reader(pool: sqlx::PgPool) -> Arc<dyn messages::domain::api::MessageReader> {+ Arc::new(messages::domain::service::MessageService::new(+ messages::outbound::pg_message_repo::PgMessageRepository::new(pool),+ messages::domain::ports::NoMessageEventPublisher,+ ))+}+/// Shared message service used by agent tools: the same persistence, reference/// authorization, and group-mention policy as the channel HTTP API, over the/// delivery `effects` a host composed.diff --git a/crates/documents/Cargo.toml b/crates/documents/Cargo.tomlindex fc4831dde0..491f0d5d4b 100644--- a/crates/documents/Cargo.toml+++ b/crates/documents/Cargo.toml@@ -15,6 +15,7 @@ ai_tools = ["dep:ai_usage","dep:async-trait","dep:macro_tower_layers",+ "dep:messages","dep:model_file_type","dep:schemars","dep:sha2",@@ -158,6 +159,7 @@ ai_usage = { path = "../ai_usage", optional = true }async-trait = { workspace = true, optional = true }lexical_client = { path = "../lexical_client", optional = true }macro_tower_layers = { path = "../macro_tower_layers", optional = true }+messages = { path = "../messages", optional = true, features = ["ports"] }model_file_type = { path = "../model_file_type", optional = true }reqwest = { workspace = true, optional = true }schemars = { workspace = true, optional = true }@@ -170,6 +172,8 @@ non_empty = { path = "../non_empty" }workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]+channel_sender = { path = "../channel_sender" }+messages = { path = "../messages", features = ["ports", "test-utils"] }bot_id = { path = "../bot_id", features = ["test-utils"] }dotenvy = { workspace = true }embedding = { path = "../embedding" }diff --git a/crates/documents/fixtures/document_pdf_comments_and_highlights.sql b/crates/documents/fixtures/document_pdf_comments_and_highlights.sqldeleted file mode 100644index 0641b77df5..0000000000--- a/crates/documents/fixtures/document_pdf_comments_and_highlights.sql+++ /dev/null@@ -1,1007 +0,0 @@--- Setup organizations and users-INSERT INTO- public."Organization" ("id", "name")-VALUES- (1, 'organization-one');--INSERT INTO public."macro_user" ("id", "username", "email", "stripe_customer_id")-VALUES- ('a1111111-1111-1111-1111-111111111111', 'user', '[email protected]', 'stripe_id'),- ('a2222222-2222-2222-2222-222222222222', 'user2', '[email protected]', 'stripe_id2');--INSERT INTO- public."User" (- "id",- "email",- "stripeCustomerId",- "organizationId",- "macro_user_id"- )-VALUES- (- 'macro|[email protected]',- '[email protected]',- 'stripe_id',- 1,- 'a1111111-1111-1111-1111-111111111111'- );--INSERT INTO- public."User" ("id", "email", "stripeCustomerId", "macro_user_id")-VALUES- (- 'macro|[email protected]',- '[email protected]',- 'stripe_id2',- 'a2222222-2222-2222-2222-222222222222'- );------------------------------------------------------- Document with comments------------------------------------------------------ Create a PDF document that will have comments-INSERT INTO- public."Document" (- "id",- "name",- "fileType",- "owner",- "createdAt",- "updatedAt"- )-VALUES- (- 'document-with-comments',- 'Document With Comments',- 'pdf',- 'macro|[email protected]',- '2022-01-01 00:00:00',- '2022-01-01 00:00:00'- );---- Create document instance for the PDF-INSERT INTO- public."DocumentInstance" (- "id",- "documentId",- "sha",- "createdAt",- "updatedAt"- )-VALUES- (- 100,- 'document-with-comments',- 'sha-pdf-comments',- '2022-01-01 00:00:00',- '2022-01-01 00:00:00'- );---- Add share permissions-INSERT INTO- public."SharePermission" (- "id",- "linkShare",- "linkShareAccessLevel",- "createdAt",- "updatedAt"- )-VALUES- (- 'sp-comments',- NULL,- NULL,- '2022-01-01 00:00:00',- '2022-01-01 00:00:00'- );--INSERT INTO- public."DocumentPermission" ("documentId", "sharePermissionId")-VALUES- ('document-with-comments', 'sp-comments');---- Create threads for the document-INSERT INTO- public."Thread" (- "id",- "owner",- "documentId",- "createdAt",- "updatedAt",- "deletedAt",- "resolved"- )-VALUES- (- 1001,- 'macro|[email protected]',- 'document-with-comments',- '2022-01-10 10:00:00',- '2022-01-10 10:00:00',- NULL,- false- ),- (- 1002,- 'macro|[email protected]',- 'document-with-comments',- '2022-01-10 11:30:00',- '2022-01-10 11:30:00',- NULL,- TRUE- ),- (- 1003,- 'macro|[email protected]',- 'document-with-comments',- '2022-01-10 14:45:00',- '2022-01-10 14:45:00',- NULL,- false- ),- (- 1004,- 'macro|[email protected]',- 'document-with-comments',- '2022-01-10 14:45:00',- '2022-01-10 14:45:00',- '2022-01-10 14:45:00',- false- ),- (- 1005,- 'macro|[email protected]',- 'document-with-comments',- '2022-01-10 14:45:00',- '2022-01-10 14:45:00',- NULL,- false- );---- Create PDF anchors for each thread-INSERT INTO- public."PdfPlaceableCommentAnchor" (- "documentId",- "uuid",- "owner",- "page",- "originalPage",- "originalIndex",- "shouldLockOnSave",- "xPct",- "yPct",- "widthPct",- "heightPct",- "rotation",- "threadId",- "wasEdited",- "wasDeleted",- "allowableEdits"- )-VALUES- (- 'document-with-comments',- '91111111-1111-1111-1111-111111111111',- 'macro|[email protected]',- 1,- 1,- 0,- TRUE,- 0.2,- 0.3,- 0.1,- 0.05,- 0.0,- 1001,- false,- false,- '{"allowResize": true, "allowTranslate": true, "allowRotate": true, "allowDelete": true, "lockAspectRatio": false}'- ),- (- 'document-with-comments',- '81111111-1111-1111-1111-111111111111',- 'macro|[email protected]',- 2,- 2,- 1,- false,- 0.5,- 0.6,- 0.15,- 0.07,- 0.0,- 1002,- false,- false,- '{"allowResize": true, "allowTranslate": true, "allowRotate": true, "allowDelete": true, "lockAspectRatio": false}'- ),- (- 'document-with-comments',- '71111111-1111-1111-1111-111111111111',- 'macro|[email protected]',- 3,- 3,- 2,- TRUE,- 0.7,- 0.4,- 0.12,- 0.06,- 0.0,- 1003,- TRUE,- false,- '{"allowResize": true, "allowTranslate": true, "allowRotate": true, "allowDelete": true, "lockAspectRatio": false}'- );---- Create comments for each thread-INSERT INTO- public."Comment" (- "id",- "threadId",- "owner",- "sender",- "text",- "createdAt",- "updatedAt"- )-VALUES- -- Comments for thread 1- (- 10001,- 1001,- 'macro|[email protected]',- '[email protected]',- 'Initial question on page 1',- '2022-01-10 10:00:00',- '2022-01-10 10:00:00'- ),- (- 10002,- 1001,- 'macro|[email protected]',- '[email protected]',- 'Response to question on page 1',- '2022-01-10 10:15:00',- '2022-01-10 10:15:00'- ),- (- 10003,- 1001,- 'macro|[email protected]',- '[email protected]',- 'Follow up question',- '2022-01-10 10:30:00',- '2022-01-10 10:30:00'- ), -- Comments for thread 2 (resolved)- (- 10004,- 1002,- 'macro|[email protected]',- '[email protected]',- 'Question about diagram on page 2',- '2022-01-10 11:30:00',- '2022-01-10 11:30:00'- ),- (- 10005,- 1002,- 'macro|[email protected]',- '[email protected]',- 'Answer about the diagram',- '2022-01-10 11:45:00',- '2022-01-10 11:45:00'- ),- (- 10006,- 1002,- 'macro|[email protected]',- '[email protected]',- 'Thanks, marking as resolved',- '2022-01-10 12:00:00',- '2022-01-10 12:00:00'- ), -- Comments for thread 3- (- 10007,- 1003,- 'macro|[email protected]',- '[email protected]',- 'Feedback on page 3',- '2022-01-10 14:45:00',- '2022-01-10 14:45:00'- ),- (- 10008,- 1005,- 'macro|[email protected]',- '[email protected]',- 'Comment on page 3',- '2022-01-10 14:45:00',- '2022-01-10 14:45:00'- );---- Create modification data JSON that matches the thread/comment structure with camelCase-INSERT INTO- public."DocumentInstanceModificationData" ("documentInstanceId", "modificationData")-VALUES- (- 100,- '{- "placeables": [- {- "allowableEdits": {- "allowResize": true,- "allowTranslate": true,- "allowRotate": true,- "allowDelete": true,- "lockAspectRatio": false- },- "wasEdited": false,- "wasDeleted": false,- "pageRange": [1],- "position": {- "xPct": 0.2,- "yPct": 0.3,- "widthPct": 0.1,- "heightPct": 0.05,- "rotation": 0.0- },- "shouldLockOnSave": true,- "originalPage": 1,- "originalIndex": 0,- "payloadType": "thread",- "payload": {- "headID": "thread1",- "page": 1,- "comments": [- {- "id": "comment1",- "sender": "[email protected]",- "content": "Initial question on page 1",- "editDate": "2022-01-10T10:00:00Z"- },- {- "id": "comment2",- "sender": "[email protected]",- "content": "Response to question on page 1",- "editDate": "2022-01-10T10:15:00Z"- },- {- "id": "comment3",- "sender": "[email protected]",- "content": "Follow up question",- "editDate": "2022-01-10T10:30:00Z"- }- ],- "isResolved": false- }- },- {- "allowableEdits": {- "allowResize": true,- "allowTranslate": true,- "allowRotate": true,- "allowDelete": true,- "lockAspectRatio": false- },- "wasEdited": false,- "wasDeleted": false,- "pageRange": [2],- "position": {- "xPct": 0.5,- "yPct": 0.6,- "widthPct": 0.15,- "heightPct": 0.07,- "rotation": 0.0- },- "shouldLockOnSave": false,- "originalPage": 2,- "originalIndex": 1,- "payloadType": "thread",- "payload": {- "headID": "thread2",- "page": 2,- "comments": [- {- "id": "comment4",- "sender": "[email protected]",- "content": "Question about diagram on page 2",- "editDate": "2022-01-10T11:30:00Z"- },- {- "id": "comment5",- "sender": "[email protected]",- "content": "Answer about the diagram",- "editDate": "2022-01-10T11:45:00Z"- },- {- "id": "comment6",- "sender": "[email protected]",- "content": "Thanks, marking as resolved",- "editDate": "2022-01-10T12:00:00Z"- }- ],- "isResolved": true- }- },- {- "allowableEdits": {- "allowResize": true,- "allowTranslate": true,- "allowRotate": true,- "allowDelete": true,- "lockAspectRatio": false- },- "wasEdited": true,- "wasDeleted": false,- "pageRange": [3],- "position": {- "xPct": 0.7,- "yPct": 0.4,- "widthPct": 0.12,- "heightPct": 0.06,- "rotation": 0.0- },- "shouldLockOnSave": true,- "originalPage": 3,- "originalIndex": 2,- "payloadType": "thread",- "payload": {- "headID": "thread3",- "page": 3,- "comments": [- {- "id": "comment7",- "sender": "[email protected]",- "content": "Feedback on page 3",- "editDate": "2022-01-10T14:45:00Z"- }- ],- "isResolved": false- }- }- ]-}'- );------------------------------------------------------- Document for updates------------------------------------------------------ Create another document to test the updates-INSERT INTO- public."Document" (- "id",- "name",- "fileType",- "owner",- "createdAt",- "updatedAt"- )-VALUES- (- 'document-for-updates',- 'Document For Updates',- 'pdf',- 'macro|[email protected]',- '2022-01-02 00:00:00',- '2022-01-02 00:00:00'- );---- Create document instance for the PDF that will be updated-INSERT INTO- public."DocumentInstance" (- "id",- "documentId",- "sha",- "createdAt",- "updatedAt"- )-VALUES- (- 200,- 'document-for-updates',- 'sha-updates',- '2022-01-02 00:00:00',- '2022-01-02 00:00:00'- );---- Add an initial thread to document-for-updates-INSERT INTO- public."Thread" (- "id",- "owner",- "documentId",- "createdAt",- "updatedAt",- "resolved"- )-VALUES- (- 2001,- 'macro|[email protected]',- 'document-for-updates',- '2022-01-15 09:00:00',- '2022-01-15 09:00:00',- false- );---- Create PDF anchor for the thread-INSERT INTO- public."PdfPlaceableCommentAnchor" (- "documentId",- "uuid",- "owner",- "page",- "originalPage",- "originalIndex",- "shouldLockOnSave",- "xPct",- "yPct",- "widthPct",- "heightPct",- "rotation",- "threadId",- "wasEdited",- "wasDeleted",- "allowableEdits"- )-VALUES- (- 'document-for-updates',- '61111111-1111-1111-1111-111111111111',- 'macro|[email protected]',- 1,- 1,- 0,- TRUE,- 0.3,- 0.4,- 0.1,- 0.05,- 0.0,- 2001,- false,- false,- '{"allowResize": true, "allowTranslate": true, "allowRotate": true, "allowDelete": true, "lockAspectRatio": false}'- );---- Add comments to thread-INSERT INTO- public."Comment" (- "id",- "threadId",- "owner",- "sender",- "text",- "createdAt",- "updatedAt"- )-VALUES- (- 20001,- 2001,- 'macro|[email protected]',- '[email protected]',- 'Initial comment for thread',- '2022-01-15 09:00:00',- '2022-01-15 09:00:00'- );---- Add consistent modification data with camelCase-INSERT INTO- public."DocumentInstanceModificationData" ("documentInstanceId", "modificationData")-VALUES- (- 200,- '{- "placeables": [- {- "allowableEdits": {- "allowResize": true,- "allowTranslate": true,- "allowRotate": true,- "allowDelete": true,- "lockAspectRatio": false- },- "wasEdited": false,- "wasDeleted": false,- "pageRange": [1],- "position": {- "xPct": 0.3,- "yPct": 0.4,- "widthPct": 0.1,- "heightPct": 0.05,- "rotation": 0.0- },- "shouldLockOnSave": true,- "originalPage": 1,- "originalIndex": 0,- "payloadType": "thread",- "payload": {- "headID": "update-thread1",- "page": 1,- "comments": [- {- "id": "update-comment1",- "sender": "[email protected]",- "content": "Initial comment for thread",- "editDate": "2022-01-15T09:00:00Z"- }- ],- "isResolved": false- }- }- ]-}'- );------------------------------------------------------- Document for deletion tests------------------------------------------------------ Create document that will be used for testing thread deletion-INSERT INTO- public."Document" (- "id",- "name",- "fileType",- "owner",- "createdAt",- "updatedAt"- )-VALUES- (- 'document-delete-test',- 'Document Delete Test',- 'pdf',- 'macro|[email protected]',- '2022-01-03 00:00:00',- '2022-01-03 00:00:00'- );---- Create document instance for the PDF-INSERT INTO- public."DocumentInstance" (- "id",- "documentId",- "sha",- "createdAt",- "updatedAt"- )-VALUES- (- 300,- 'document-delete-test',- 'sha-delete',- '2022-01-03 00:00:00',- '2022-01-03 00:00:00'- );---- Add threads to document-delete-test-INSERT INTO- public."Thread" (- "id",- "owner",- "documentId",- "createdAt",- "updatedAt",- "resolved"- )-VALUES- (- 3001,- 'macro|[email protected]',- 'document-delete-test',- '2022-01-20 10:00:00',- '2022-01-20 10:00:00',- false- ),- (- 3002,- 'macro|[email protected]',- 'document-delete-test',- '2022-01-20 11:00:00',- '2022-01-20 11:00:00',- false- );---- Create PDF anchors for the threads-INSERT INTO- public."PdfPlaceableCommentAnchor" (- "documentId",- "uuid",- "owner",- "page",- "originalPage",- "originalIndex",- "shouldLockOnSave",- "xPct",- "yPct",- "widthPct",- "heightPct",- "rotation",- "threadId",- "wasEdited",- "wasDeleted",- "allowableEdits"- )-VALUES- (- 'document-delete-test',- '51111111-1111-1111-1111-111111111111',- 'macro|[email protected]',- 1,- 1,- 0,- TRUE,- 0.2,- 0.3,- 0.1,- 0.05,- 0.0,- 3001,- false,- false,- '{"allowResize": true, "allowTranslate": true, "allowRotate": true, "allowDelete": true, "lockAspectRatio": false}'- ),- (- 'document-delete-test',- '41111111-1111-1111-1111-111111111111',- 'macro|[email protected]',- 2,- 2,- 1,- TRUE,- 0.5,- 0.6,- 0.15,- 0.07,- 0.0,- 3002,- false,- false,- '{"allowResize": true, "allowTranslate": true, "allowRotate": true, "allowDelete": true, "lockAspectRatio": false}'- );---- Add comments to threads-INSERT INTO- public."Comment" (- "id",- "threadId",- "owner",- "sender",- "text",- "createdAt",- "updatedAt"- )-VALUES- (- 30001,- 3001,- 'macro|[email protected]',- '[email protected]',- 'Comment in thread 1',- '2022-01-20 10:00:00',- '2022-01-20 10:00:00'- ),- (- 30002,- 3002,- 'macro|[email protected]',- '[email protected]',- 'Comment in thread 2',- '2022-01-20 11:00:00',- '2022-01-20 11:00:00'- );---- Add modification data with only the first thread (to test deletion) with camelCase-INSERT INTO- public."DocumentInstanceModificationData" ("documentInstanceId", "modificationData")-VALUES- (- 300,- '{- "placeables": [- {- "allowableEdits": {- "allowResize": true,- "allowTranslate": true,- "allowRotate": true,- "allowDelete": true,- "lockAspectRatio": false- },- "wasEdited": false,- "wasDeleted": false,- "pageRange": [1],- "position": {- "xPct": 0.2,- "yPct": 0.3,- "widthPct": 0.1,- "heightPct": 0.05,- "rotation": 0.0- },- "shouldLockOnSave": true,- "originalPage": 1,- "originalIndex": 0,- "payloadType": "thread",- "payload": {- "headID": "delete-thread1",- "page": 1,- "comments": [- {- "id": "delete-comment1",- "sender": "[email protected]",- "content": "Comment in thread 1",- "editDate": "2022-01-20T10:00:00Z"- }- ],- "isResolved": false- }- }- ]-}'- );---- Add highlight anchors (each associated with a thread)-INSERT INTO- public."PdfHighlightAnchor" (- "uuid",- "documentId",- "owner",- "threadId",- "page",- "red",- "green",- "blue",- "alpha",- "type",- "text",- "pageViewportWidth",- "pageViewportHeight",- "createdAt",- "updatedAt",- "deletedAt"- )-VALUES- -- Highlight 1 (red highlight on page 1, linked to thread 1001)- (- '11111111-1111-1111-1111-111111111111',- 'document-with-comments',- 'macro|[email protected]',- 1001,- 1,- 255,- 0,- 0,- 0.8,- 1,- 'Highlighted text on page 1',- 800,- 1000,- '2022-01-10 10:00:00',- '2022-01-10 10:00:00',- NULL- ), -- Highlight 2 (green highlight on page 2, linked to thread 1002)- (- '22222222-2222-2222-2222-222222222222',- 'document-with-comments',- 'macro|[email protected]',- 1002,- 2,- 0,- 255,- 0,- 0.7,- 1,- 'Another highlight on page 2',- 800,- 1000,- '2022-01-10 11:30:00',- '2022-01-10 11:30:00',- NULL- ), -- Highlight 3 (blue highlight on page 3, linked to no thread)- (- '33333333-3333-3333-3333-333333333333',- 'document-with-comments',- 'macro|[email protected]',- NULL,- 3,- 0,- 0,- 255,- 0.6,- 1,- 'Last highlight on page 3',- 800,- 1000,- '2022-01-10 14:45:00',- '2022-01-10 14:45:00',- NULL- );---- Add highlight rectangles (each linked to its respective highlight anchor)-INSERT INTO- public."PdfHighlightRect" (- "pdfHighlightAnchorId",- "top",- "left",- "width",- "height"- )-VALUES- -- Rectangles for highlight 1 (on page 1)- (- '11111111-1111-1111-1111-111111111111',- 100,- 50,- 200,- 50- ),- (- '11111111-1111-1111-1111-111111111111',- 160,- 50,- 200,- 50- ), -- Rectangle for highlight 2 (on page 2)- (- '22222222-2222-2222-2222-222222222222',- 220,- 80,- 250,- 55- ), -- Rectangles for highlight 3 (on page 3)- (- '33333333-3333-3333-3333-333333333333',- 300,- 100,- 150,- 40- ),- (- '33333333-3333-3333-3333-333333333333',- 350,- 100,- 150,- 40- ),- (- '33333333-3333-3333-3333-333333333333',- 400,- 100,- 150,- 40- );--INSERT INTO- "ThreadAnchor" ("threadId", "anchorId", "anchorTableName")-VALUES- (- 1001,- '91111111-1111-1111-1111-111111111111',- 'PdfPlaceableCommentAnchor'- ),- (- 1002,- '81111111-1111-1111-1111-111111111111',- 'PdfPlaceableCommentAnchor'- ),- (- 1003,- '71111111-1111-1111-1111-111111111111',- 'PdfPlaceableCommentAnchor'- ),- (- 2001,- '61111111-1111-1111-1111-111111111111',- 'PdfPlaceableCommentAnchor'- ),- (- 3001,- '51111111-1111-1111-1111-111111111111',- 'PdfPlaceableCommentAnchor'- ),- (- 3002,- '41111111-1111-1111-1111-111111111111',- 'PdfPlaceableCommentAnchor'- );\ No newline at end of filediff --git a/crates/documents/src/domain.rs b/crates/documents/src/domain.rsindex f24c2974ea..5bbdde939a 100644--- a/crates/documents/src/domain.rs+++ b/crates/documents/src/domain.rs@@ -3,6 +3,9 @@/// Event-to-activity mappings for this domain.pub mod activity;pub mod branch_name;+/// A document's comment threads, read through the shared message service.+#[cfg(feature = "ai_tools")]+pub mod comments;pub mod content;/// Unified entity-mutation capability impls.#[cfg(feature = "service")]diff --git a/crates/documents/src/domain/comments.rs b/crates/documents/src/domain/comments.rsnew file mode 100644index 0000000000..d2e417b95d--- /dev/null+++ b/crates/documents/src/domain/comments.rs@@ -0,0 +1,364 @@+//! A document's comments as an agent reads them: inline threads with what they+//! are anchored to, and Discussion threads on the document as a whole.++#[cfg(test)]+mod test;++use std::{collections::HashMap, sync::Arc};++use chrono::{DateTime, Utc};+use entity_access::domain::models::{EntityAccessReceipt, ViewAccessLevel};+use futures::{StreamExt, TryStreamExt, stream};+use messages::domain::{+ api::MessageReader,+ models::{Message, MessageListItem, ThreadAnchor},+ ports::{MessageError, MessageTimelineQuery},+ service::MessageView,+};+use uuid::Uuid;++use super::models::DocumentError;++/// Discussions read per timeline page, the most the message service returns.+const PAGE_SIZE: u16 = 100;++/// Most discussions returned for one document, newest kept, so a heavily+/// discussed document cannot crowd its own content out of an agent's context.+pub const MAX_DISCUSSIONS: usize = 500;++/// Concurrent thread and mark lookups for one read.+const LOOKUP_CONCURRENCY: usize = 8;++/// Reads what a comment mark covers in the live document.+#[async_trait::async_trait]+pub trait CommentMarks: Send + Sync + 'static {+ /// The text the mark covers now, `None` when the document no longer+ /// carries it. Checks no access of its own: it is only asked after the+ /// discussions were read under the caller's capability on the document.+ async fn marked_text(&self, document_id: &str, mark_id: Uuid)+ -> anyhow::Result<Option<String>>;+}++/// Where a discussion sits in its document.+#[derive(serde::Serialize, Debug, Clone, PartialEq, Eq)]+#[cfg_attr(feature = "ai_tools", derive(schemars::JsonSchema))]+#[serde(tag = "type", rename_all = "camelCase")]+pub enum CommentAnchor {+ /// A Discussion comment on the document as a whole.+ Document,+ /// An inline comment on a passage of a markdown document.+ #[serde(rename_all = "camelCase")]+ Text {+ /// The comment mark in the document.+ mark_id: Uuid,+ /// The text the comment is on, as the document reads now; the text+ /// when the comment was written if the document could not be read.+ #[serde(skip_serializing_if = "Option::is_none")]+ marked_text: Option<String>,+ /// The text when the comment was written, when it differs from now.+ #[serde(skip_serializing_if = "Option::is_none")]+ original_marked_text: Option<String>,+ /// The commented text has since been removed from the document.+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]+ removed: bool,+ },+ /// A comment on a PDF highlight.+ #[serde(rename_all = "camelCase")]+ PdfHighlight {+ /// The highlight annotation.+ anchor_id: Uuid,+ },+ /// A comment pinned to a point on a PDF page.+ #[serde(rename_all = "camelCase")]+ PdfPin {+ /// The pin annotation.+ anchor_id: Uuid,+ },+}++/// A single comment in a discussion.+#[derive(serde::Serialize, Debug, Clone, PartialEq, Eq)]+#[cfg_attr(feature = "ai_tools", derive(schemars::JsonSchema))]+#[serde(rename_all = "camelCase")]+pub struct DocumentComment {+ /// The comment id.+ pub id: Uuid,+ /// The user or bot id of the author.+ pub author: String,+ /// The author's display name, for bots and comments imported from other documents.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub author_name: Option<String>,+ /// The comment body in markdown; absent when the comment was deleted.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub content: Option<String>,+ /// When the comment was written.+ pub created_at: DateTime<Utc>,+ /// When the comment was last edited.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub edited_at: Option<DateTime<Utc>>,+}++/// Whether a thread is on part of a document or on the document as a whole.+#[derive(serde::Serialize, Debug, Clone, Copy, PartialEq, Eq)]+#[cfg_attr(feature = "ai_tools", derive(schemars::JsonSchema))]+#[serde(rename_all = "camelCase")]+pub enum CommentThreadKind {+ /// An inline comment on a passage, PDF highlight or PDF pin.+ Inline,+ /// A comment in the document's Discussion panel.+ Discussion,+}++/// A comment thread on a document: its first comment followed by the replies.+#[derive(serde::Serialize, Debug, Clone, PartialEq, Eq)]+#[cfg_attr(feature = "ai_tools", derive(schemars::JsonSchema))]+#[serde(rename_all = "camelCase")]+pub struct DocumentDiscussion {+ /// The thread id, which is the id of its first comment. Replies and+ /// resolution address the thread by this id.+ pub id: Uuid,+ /// Whether the thread is inline or a Discussion comment.+ pub kind: CommentThreadKind,+ /// Whether the thread has been resolved.+ pub resolved: bool,+ /// What the thread is attached to.+ pub anchor: CommentAnchor,+ /// The comments in order, first comment first.+ pub comments: Vec<DocumentComment>,+}++/// Reads a document's comments.+#[async_trait::async_trait]+pub trait DocumentComments: Send + Sync + 'static {+ /// Every live discussion on the document, oldest first, capped at+ /// [`MAX_DISCUSSIONS`].+ async fn discussions(+ &self,+ receipt: EntityAccessReceipt<ViewAccessLevel>,+ ) -> Result<Vec<DocumentDiscussion>, DocumentError>;+}++/// [`DocumentComments`] over the shared message service.+pub struct DocumentCommentReader<M> {+ messages: Arc<dyn MessageReader>,+ marks: M,+}++impl<M: CommentMarks> DocumentCommentReader<M> {+ /// Read discussions from `messages`, resolving inline anchors with `marks`.+ pub fn new(messages: Arc<dyn MessageReader>, marks: M) -> Self {+ Self { messages, marks }+ }++ async fn roots(+ &self,+ access: &EntityAccessReceipt<MessageView>,+ ) -> Result<Vec<MessageListItem>, MessageError> {+ let mut roots = Vec::new();+ let mut cursor = None;+ loop {+ let page = self+ .messages+ .timeline(+ access.clone(),+ MessageTimelineQuery {+ cursor,+ limit: Some(PAGE_SIZE),+ ..Default::default()+ },+ )+ .await?;+ // A deleted first comment with no live replies is not returned,+ // so it must not count toward the cap.+ roots.extend(+ page.items.into_iter().filter(|item| {+ item.message.deleted_at.is_none() || item.thread.reply_count > 0+ }),+ );+ if roots.len() >= MAX_DISCUSSIONS {+ tracing::warn!(count = roots.len(), "document discussions truncated");+ roots.truncate(MAX_DISCUSSIONS);+ return Ok(roots);+ }+ match page.next_cursor {+ Some(next) => cursor = Some(next),+ None => return Ok(roots),+ }+ }+ }++ /// Replies of the threads whose timeline preview does not hold them all.+ async fn full_replies(+ &self,+ access: &EntityAccessReceipt<MessageView>,+ roots: &[MessageListItem],+ ) -> Result<HashMap<Uuid, Vec<Message>>, MessageError> {+ let partial: Vec<Uuid> = roots+ .iter()+ .filter(|item| item.thread.reply_count > item.thread.preview.len() as i64)+ .map(|item| item.message.id)+ .collect();+ stream::iter(partial)+ .map(|root| async move {+ let thread = self.messages.get_thread(access.clone(), root).await?;+ Ok::<_, MessageError>((root, thread.replies))+ })+ .buffer_unordered(LOOKUP_CONCURRENCY)+ .try_collect()+ .await+ }++ /// What each markdown mark covers now. A failed lookup leaves the mark+ /// out, so the snapshot stands in rather than failing the read.+ async fn live_marks(+ &self,+ document_id: &str,+ roots: &[MessageListItem],+ ) -> HashMap<Uuid, Option<String>> {+ let mark_ids: Vec<Uuid> = roots+ .iter()+ .filter_map(|item| match &item.state.anchor {+ Some(ThreadAnchor::Markdown { mark_id, .. }) => Some(*mark_id),+ _ => None,+ })+ .collect();+ stream::iter(mark_ids)+ .map(|mark_id| async move {+ self.marks+ .marked_text(document_id, mark_id)+ .await+ .inspect_err(|error| {+ tracing::warn!(error = ?error, %mark_id, "reading comment without the live marked text");+ })+ .ok()+ .map(|text| (mark_id, text))+ })+ .buffer_unordered(LOOKUP_CONCURRENCY)+ .filter_map(std::future::ready)+ .collect()+ .await+ }+}++#[async_trait::async_trait]+impl<M: CommentMarks> DocumentComments for DocumentCommentReader<M> {+ #[tracing::instrument(err, skip_all, fields(document_id = %receipt.entity().entity_id))]+ async fn discussions(+ &self,+ receipt: EntityAccessReceipt<ViewAccessLevel>,+ ) -> Result<Vec<DocumentDiscussion>, DocumentError> {+ let document_id = receipt.entity().entity_id.clone();+ let access = receipt+ .try_into_requirement::<MessageView>()+ .map_err(|_| DocumentError::Unauthorized)?;+ let roots = self.roots(&access).await.map_err(message_error)?;+ let (replies, marks) = futures::join!(+ self.full_replies(&access, &roots),+ self.live_marks(&document_id, &roots)+ );+ let mut replies = replies.map_err(message_error)?;+ Ok(roots+ .into_iter()+ .rev()+ .filter_map(|item| {+ let replies = replies+ .remove(&item.message.id)+ .unwrap_or(item.thread.preview);+ discussion(+ item.message,+ item.state.resolved,+ item.state.anchor,+ replies,+ &marks,+ )+ })+ .collect())+ }+}++/// A thread whose first comment was deleted is kept only while it has replies.+fn discussion(+ root: Message,+ resolved: bool,+ anchor: Option<ThreadAnchor>,+ replies: Vec<Message>,+ marks: &HashMap<Uuid, Option<String>>,+) -> Option<DocumentDiscussion> {+ if root.deleted_at.is_some() && replies.is_empty() {+ return None;+ }+ let kind = match anchor {+ None => CommentThreadKind::Discussion,+ Some(_) => CommentThreadKind::Inline,+ };+ Some(DocumentDiscussion {+ id: root.id,+ kind,+ resolved,+ anchor: comment_anchor(anchor, marks),+ comments: std::iter::once(root)+ .chain(+ replies+ .into_iter()+ .filter(|reply| reply.deleted_at.is_none()),+ )+ .map(comment)+ .collect(),+ })+}++fn comment_anchor(+ anchor: Option<ThreadAnchor>,+ marks: &HashMap<Uuid, Option<String>>,+) -> CommentAnchor {+ match anchor {+ None => CommentAnchor::Document,+ Some(ThreadAnchor::Markdown {+ mark_id,+ marked_text: snapshot,+ }) => match marks.get(&mark_id) {+ Some(Some(current)) => CommentAnchor::Text {+ mark_id,+ original_marked_text: snapshot.filter(|snapshot| snapshot != current),+ marked_text: Some(current.clone()),+ removed: false,+ },+ Some(None) => CommentAnchor::Text {+ mark_id,+ marked_text: snapshot,+ original_marked_text: None,+ removed: true,+ },+ None => CommentAnchor::Text {+ mark_id,+ marked_text: snapshot,+ original_marked_text: None,+ removed: false,+ },+ },+ Some(ThreadAnchor::PdfHighlight { anchor_id }) => CommentAnchor::PdfHighlight { anchor_id },+ Some(ThreadAnchor::PdfPlaceable { anchor_id }) => CommentAnchor::PdfPin { anchor_id },+ }+}++fn comment(message: Message) -> DocumentComment {+ let deleted = message.deleted_at.is_some();+ DocumentComment {+ id: message.id,+ author: message.sender_id.as_ref().to_owned(),+ author_name: message+ .imported_author+ .map(|author| author.name)+ .or(message.bot_profile.map(|bot| bot.name)),+ content: (!deleted).then_some(message.content),+ created_at: message.created_at,+ edited_at: message.edited_at,+ }+}++fn message_error(error: MessageError) -> DocumentError {+ match error {+ MessageError::Forbidden => DocumentError::Unauthorized,+ error => DocumentError::Internal(anyhow::anyhow!(error.to_string())),+ }+}diff --git a/crates/documents/src/domain/comments/test.rs b/crates/documents/src/domain/comments/test.rsnew file mode 100644index 0000000000..aada966234--- /dev/null+++ b/crates/documents/src/domain/comments/test.rs@@ -0,0 +1,521 @@+use super::*;+use channel_sender::ChannelSender;+use chrono::TimeZone;+use entity_access::domain::models::{Entity, EntityPermission, EntityType};+use macro_user_id::user_id::MacroUserIdStr;+use messages::domain::{+ api::MockMessageReader,+ models::{BotSenderProfile, MessageParent, MessageThread, MessageThreadPreview, ThreadState},+ ports::{MessageCursor, MessagePage},+};+use models_permissions::share_permission::access_level::AccessLevel;++const DOCUMENT: &str = "019fd3b9-3c6c-7c05-89c2-a27f0121813b";++fn user() -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap()+}++fn receipt() -> EntityAccessReceipt<ViewAccessLevel> {+ EntityAccessReceipt::try_new_authenticated_user(+ user(),+ Entity {+ entity_id: DOCUMENT.to_owned(),+ entity_type: EntityType::Document,+ },+ EntityPermission::AccessLevel {+ access_level: AccessLevel::View,+ },+ )+ .unwrap()+}++fn at(minute: u32) -> DateTime<Utc> {+ Utc.with_ymd_and_hms(2026, 9, 23, 12, minute, 0).unwrap()+}++fn id(n: u128) -> Uuid {+ Uuid::from_u128(n)+}++fn message(n: u128, root: Option<u128>, content: &str) -> Message {+ Message {+ id: id(n),+ parent: MessageParent::parse("document", DOCUMENT).unwrap(),+ thread_id: root.map(id),+ sender_id: ChannelSender::new_from_user(user()),+ imported_author: None,+ bot_profile: None,+ mentions: vec![],+ triggered_by: None,+ content: content.to_owned(),+ created_at: at(n as u32),+ updated_at: at(n as u32),+ edited_at: None,+ deleted_at: None,+ attachments: vec![],+ reactions: vec![],+ }+}++fn item(root: Message, anchor: Option<ThreadAnchor>, replies: Vec<Message>) -> MessageListItem {+ MessageListItem {+ state: ThreadState {+ root_id: root.id,+ user_id: user().as_ref().to_owned(),+ resolved: false,+ anchor,+ created_at: root.created_at,+ updated_at: root.updated_at,+ deleted_at: None,+ },+ thread: MessageThreadPreview {+ reply_count: replies.len() as i64,+ latest_reply_at: replies.last().map(|reply| reply.created_at),+ preview: replies,+ },+ message: root,+ }+}++fn markdown(mark: u128, snapshot: Option<&str>) -> Option<ThreadAnchor> {+ Some(ThreadAnchor::Markdown {+ mark_id: id(mark),+ marked_text: snapshot.map(str::to_owned),+ })+}++fn page(items: Vec<MessageListItem>, next_cursor: Option<MessageCursor>) -> MessagePage {+ MessagePage {+ items,+ next_cursor,+ previous_cursor: None,+ }+}++/// Mark lookups answered from a fixed table; a mark missing from it fails.+struct Marks(HashMap<Uuid, Option<String>>);++#[async_trait::async_trait]+impl CommentMarks for Marks {+ async fn marked_text(+ &self,+ document_id: &str,+ mark_id: Uuid,+ ) -> anyhow::Result<Option<String>> {+ assert_eq!(document_id, DOCUMENT);+ self.0+ .get(&mark_id)+ .cloned()+ .ok_or_else(|| anyhow::anyhow!("lexical service unavailable"))+ }+}++fn no_marks() -> Marks {+ Marks(HashMap::new())+}++fn reader(messages: MockMessageReader, marks: Marks) -> DocumentCommentReader<Marks> {+ DocumentCommentReader::new(Arc::new(messages), marks)+}++#[tokio::test]+async fn reads_inline_and_discussion_threads_oldest_first() {+ let mut messages = MockMessageReader::new();+ messages.expect_timeline().times(1).returning(|access, _| {+ assert_eq!(access.entity().entity_id, DOCUMENT);+ // The timeline is newest first.+ Ok(page(+ vec![+ item(message(3, None, "Discussion comment"), None, vec![]),+ item(+ message(1, None, "Inline comment"),+ markdown(100, Some("resolve linked accounts")),+ vec![message(2, Some(1), "A reply")],+ ),+ ],+ None,+ ))+ });+ let marks = Marks(HashMap::from([(+ id(100),+ Some("resolve linked accounts at read time instead".to_owned()),+ )]));++ let discussions = reader(messages, marks)+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(+ discussions,+ vec![+ DocumentDiscussion {+ id: id(1),+ kind: CommentThreadKind::Inline,+ resolved: false,+ anchor: CommentAnchor::Text {+ mark_id: id(100),+ marked_text: Some("resolve linked accounts at read time instead".to_owned()),+ original_marked_text: Some("resolve linked accounts".to_owned()),+ removed: false,+ },+ comments: vec![+ comment(message(1, None, "Inline comment")),+ comment(message(2, Some(1), "A reply")),+ ],+ },+ DocumentDiscussion {+ id: id(3),+ kind: CommentThreadKind::Discussion,+ resolved: false,+ anchor: CommentAnchor::Document,+ comments: vec![comment(message(3, None, "Discussion comment"))],+ },+ ]+ );+}++#[tokio::test]+async fn unchanged_mark_text_is_not_repeated() {+ let mut messages = MockMessageReader::new();+ messages.expect_timeline().returning(|_, _| {+ Ok(page(+ vec![item(+ message(1, None, "c"),+ markdown(100, Some("same")),+ vec![],+ )],+ None,+ ))+ });+ let marks = Marks(HashMap::from([(id(100), Some("same".to_owned()))]));++ let discussions = reader(messages, marks)+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(+ discussions[0].anchor,+ CommentAnchor::Text {+ mark_id: id(100),+ marked_text: Some("same".to_owned()),+ original_marked_text: None,+ removed: false,+ }+ );+}++#[tokio::test]+async fn removed_or_unreadable_marks_fall_back_to_the_snapshot() {+ let mut messages = MockMessageReader::new();+ messages.expect_timeline().returning(|_, _| {+ Ok(page(+ vec![+ item(+ message(2, None, "b"),+ markdown(200, Some("unreadable")),+ vec![],+ ),+ item(+ message(1, None, "a"),+ markdown(100, Some("deleted text")),+ vec![],+ ),+ ],+ None,+ ))+ });+ // Mark 100 is gone from the document; mark 200's lookup fails.+ let marks = Marks(HashMap::from([(id(100), None)]));++ let discussions = reader(messages, marks)+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(+ discussions+ .into_iter()+ .map(|discussion| discussion.anchor)+ .collect::<Vec<_>>(),+ vec![+ CommentAnchor::Text {+ mark_id: id(100),+ marked_text: Some("deleted text".to_owned()),+ original_marked_text: None,+ removed: true,+ },+ CommentAnchor::Text {+ mark_id: id(200),+ marked_text: Some("unreadable".to_owned()),+ original_marked_text: None,+ removed: false,+ },+ ]+ );+}++#[tokio::test]+async fn pdf_anchors_are_inline_threads() {+ let mut messages = MockMessageReader::new();+ messages.expect_timeline().returning(|_, _| {+ Ok(page(+ vec![+ item(+ message(2, None, "pin"),+ Some(ThreadAnchor::PdfPlaceable { anchor_id: id(20) }),+ vec![],+ ),+ item(+ message(1, None, "highlight"),+ Some(ThreadAnchor::PdfHighlight { anchor_id: id(10) }),+ vec![],+ ),+ ],+ None,+ ))+ });++ let discussions = reader(messages, no_marks())+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(+ discussions+ .iter()+ .map(|discussion| (discussion.kind, discussion.anchor.clone()))+ .collect::<Vec<_>>(),+ vec![+ (+ CommentThreadKind::Inline,+ CommentAnchor::PdfHighlight { anchor_id: id(10) }+ ),+ (+ CommentThreadKind::Inline,+ CommentAnchor::PdfPin { anchor_id: id(20) }+ ),+ ]+ );+}++#[tokio::test]+async fn threads_beyond_the_preview_load_every_reply() {+ let mut messages = MockMessageReader::new();+ messages.expect_timeline().returning(|_, _| {+ let replies = (2..5).map(|n| message(n, Some(1), "preview")).collect();+ let mut root = item(message(1, None, "root"), None, replies);+ root.thread.reply_count = 4;+ Ok(page(vec![root], None))+ });+ messages.expect_get_thread().times(1).returning(|_, root| {+ assert_eq!(root, id(1));+ let mut deleted = message(6, Some(1), "gone");+ deleted.deleted_at = Some(at(7));+ Ok(MessageThread {+ state: item(message(1, None, "root"), None, vec![]).state,+ root: message(1, None, "root"),+ replies: vec![+ message(2, Some(1), "r2"),+ message(3, Some(1), "r3"),+ message(4, Some(1), "r4"),+ message(5, Some(1), "r5"),+ deleted,+ ],+ })+ });++ let discussions = reader(messages, no_marks())+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(+ discussions[0]+ .comments+ .iter()+ .map(|comment| comment.id)+ .collect::<Vec<_>>(),+ vec![id(1), id(2), id(3), id(4), id(5)]+ );+}++#[tokio::test]+async fn deleted_first_comments_are_kept_only_with_replies() {+ let mut messages = MockMessageReader::new();+ messages.expect_timeline().returning(|_, _| {+ let mut lone = message(3, None, "lone");+ lone.deleted_at = Some(at(4));+ let mut answered = message(1, None, "answered");+ answered.deleted_at = Some(at(4));+ Ok(page(+ vec![+ item(lone, None, vec![]),+ item(answered, None, vec![message(2, Some(1), "reply")]),+ ],+ None,+ ))+ });++ let discussions = reader(messages, no_marks())+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(discussions.len(), 1);+ assert_eq!(discussions[0].id, id(1));+ assert_eq!(discussions[0].comments[0].content, None);+ assert_eq!(discussions[0].comments[1].content.as_deref(), Some("reply"));+}++#[tokio::test]+async fn follows_the_timeline_to_its_oldest_page() {+ let mut messages = MockMessageReader::new();+ let mut sequence = mockall::Sequence::new();+ messages+ .expect_timeline()+ .times(1)+ .in_sequence(&mut sequence)+ .returning(|_, query| {+ assert!(query.cursor.is_none());+ assert_eq!(query.limit, Some(PAGE_SIZE));+ Ok(page(+ vec![item(message(2, None, "newer"), None, vec![])],+ Some(MessageCursor {+ created_at: at(2),+ id: id(2),+ }),+ ))+ });+ messages+ .expect_timeline()+ .times(1)+ .in_sequence(&mut sequence)+ .returning(|_, query| {+ assert_eq!(query.cursor.map(|cursor| cursor.id), Some(id(2)));+ Ok(page(+ vec![item(message(1, None, "older"), None, vec![])],+ None,+ ))+ });++ let discussions = reader(messages, no_marks())+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(+ discussions.iter().map(|d| d.id).collect::<Vec<_>>(),+ vec![id(1), id(2)]+ );+}++#[tokio::test]+async fn message_failures_fail_the_read() {+ let mut messages = MockMessageReader::new();+ messages+ .expect_timeline()+ .returning(|_, _| Err(MessageError::Forbidden));++ let error = reader(messages, no_marks())+ .discussions(receipt())+ .await+ .unwrap_err();++ assert!(matches!(error, DocumentError::Unauthorized));+}++#[test]+fn serializes_ids_kind_and_anchor_for_agents() {+ let mut bot = message(2, Some(1), "On it");+ bot.bot_profile = Some(BotSenderProfile {+ name: "Macro AI".to_owned(),+ avatar_url: None,+ });+ let discussion = DocumentDiscussion {+ id: id(1),+ kind: CommentThreadKind::Inline,+ resolved: true,+ anchor: CommentAnchor::Text {+ mark_id: id(100),+ marked_text: Some("marked".to_owned()),+ original_marked_text: None,+ removed: false,+ },+ comments: vec![comment(message(1, None, "Why?")), comment(bot)],+ };++ assert_eq!(+ serde_json::to_value(discussion).unwrap(),+ serde_json::json!({+ "id": id(1),+ "kind": "inline",+ "resolved": true,+ "anchor": { "type": "text", "markId": id(100), "markedText": "marked" },+ "comments": [+ {+ "id": id(1),+ "author": "macro|[email protected]",+ "content": "Why?",+ "createdAt": "2026-09-23T12:01:00Z"+ },+ {+ "id": id(2),+ "author": "macro|[email protected]",+ "authorName": "Macro AI",+ "content": "On it",+ "createdAt": "2026-09-23T12:02:00Z"+ }+ ]+ })+ );+}++#[tokio::test]+async fn deleted_threads_do_not_count_toward_the_cap() {+ let mut messages = MockMessageReader::new();+ let mut sequence = mockall::Sequence::new();+ messages+ .expect_timeline()+ .times(1)+ .in_sequence(&mut sequence)+ .returning(|_, _| {+ let items = (0..MAX_DISCUSSIONS as u128)+ .map(|n| {+ let mut root = message(2, None, "deleted");+ root.id = id(1_000 + n);+ root.deleted_at = Some(at(0));+ item(root, None, vec![])+ })+ .collect();+ Ok(page(+ items,+ Some(MessageCursor {+ created_at: at(0),+ id: id(1_000),+ }),+ ))+ });+ messages+ .expect_timeline()+ .times(1)+ .in_sequence(&mut sequence)+ .returning(|_, _| {+ Ok(page(+ vec![item(message(1, None, "live"), None, vec![])],+ None,+ ))+ });++ let discussions = reader(messages, no_marks())+ .discussions(receipt())+ .await+ .unwrap();++ assert_eq!(+ discussions.iter().map(|d| d.id).collect::<Vec<_>>(),+ vec![id(1)]+ );+}diff --git a/crates/documents/src/domain/models.rs b/crates/documents/src/domain/models.rsindex 09c021b58e..070e01e574 100644--- a/crates/documents/src/domain/models.rs+++ b/crates/documents/src/domain/models.rs@@ -10,7 +10,6 @@ use models_permissions::share_permission::LinkShareState;use super::response::DocumentResponse;use model::sync_service::SyncServiceVersionID;use models_properties::api::requests::SetPropertyValue;-use serde_json::Value;/// SHA256 hash of an empty string — used for empty markdown documents (tasks).pub const EMPTY_SHA256: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";@@ -667,67 +666,3 @@ impl From<DocumentTeamShare> for DocumentTeamShareResponse {}}}--/// A comment thread attached to a document.-#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone)]-#[cfg_attr(feature = "ai_tools", derive(schemars::JsonSchema))]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]-#[serde(rename_all = "camelCase")]-pub struct Thread {- /// The unique id of the thread.- pub thread_id: i64,- /// The user id of the thread owner.- pub owner: String,- /// Whether the thread has been resolved.- pub resolved: bool,- /// The document the thread is attached to.- pub document_id: String,- /// When the thread was created.- pub created_at: Option<DateTime<Utc>>,- /// When the thread was last updated.- pub updated_at: Option<DateTime<Utc>>,- /// When the thread was deleted, if ever.- pub deleted_at: Option<DateTime<Utc>>,- /// Arbitrary thread metadata.- pub metadata: Option<Value>,-}--/// A single comment in a thread.-#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone)]-#[cfg_attr(feature = "ai_tools", derive(schemars::JsonSchema))]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]-#[serde(rename_all = "camelCase")]-pub struct Comment {- /// The unique id of the comment.- pub comment_id: i64,- /// The thread this comment belongs to.- pub thread_id: i64,- /// Ordering position within the thread.- pub order: Option<i32>,- /// The user id of the comment owner.- pub owner: String,- /// Sender display string.- pub sender: Option<String>,- /// Comment body.- pub text: String,- /// Arbitrary comment metadata.- pub metadata: Option<Value>,- /// When the comment was created.- pub created_at: Option<DateTime<Utc>>,- /// When the comment was last updated.- pub updated_at: Option<DateTime<Utc>>,- /// When the comment was deleted, if ever.- pub deleted_at: Option<DateTime<Utc>>,-}--/// A thread bundled together with its ordered comments.-#[derive(serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug, Clone)]-#[cfg_attr(feature = "ai_tools", derive(schemars::JsonSchema))]-#[cfg_attr(feature = "axum", derive(utoipa::ToSchema))]-#[serde(rename_all = "camelCase")]-pub struct CommentThread {- /// The thread metadata.- pub thread: Thread,- /// The comments in the thread, ordered by `createdAt` ASC.- pub comments: Vec<Comment>,-}diff --git a/crates/documents/src/domain/ports.rs b/crates/documents/src/domain/ports.rsindex b59d55ba92..b99df5a15d 100644--- a/crates/documents/src/domain/ports.rs+++ b/crates/documents/src/domain/ports.rs@@ -36,11 +36,10 @@ use model_entity::Entity;use activity::Attribution;use super::models::{- BranchNameContext, CommentThread, CopyDocumentRepoArgs, CreateDocumentRepoArgs,- CreateTaskRequest, DocumentError, DocumentTeamShare, DocumentTeamShareResponse,- EditDocumentRepoArgs, EditDocumentServiceArgs, EmailImportRepoOutcome,- GithubPullRequestsResponse, ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,- TeamTaskMetadata,+ BranchNameContext, CopyDocumentRepoArgs, CreateDocumentRepoArgs, CreateTaskRequest,+ DocumentError, DocumentTeamShare, DocumentTeamShareResponse, EditDocumentRepoArgs,+ EditDocumentServiceArgs, EmailImportRepoOutcome, GithubPullRequestsResponse,+ ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName, TeamTaskMetadata,};/// Repository for accessing document data from the database.@@ -131,12 +130,6 @@ pub trait DocumentRepo: Send + Sync + 'static {document_id: &str,) -> impl Future<Output = Result<String, Self::Err>> + Send;- /// Get all comment threads (with their comments) attached to a document.- fn get_document_comments(- &self,- document_id: &str,- ) -> impl Future<Output = Result<Vec<CommentThread>, Self::Err>> + Send;-/// Create a new document with all associated records in a single transaction.////// Always inserts a `Document` row. Email-attachment linking and SHA reuse@@ -468,12 +461,6 @@ pub trait DocumentService: Send + Sync + 'static {entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,) -> impl Future<Output = Result<String, DocumentError>> + Send;- /// Get all comment threads (with their comments) for a document.- fn get_document_comments(- &self,- entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,- ) -> impl Future<Output = Result<Vec<CommentThread>, DocumentError>> + Send;-/// Create a new document, generate an S3 presigned upload URL, and/// optionally attach task properties and update project modified.fn create_document(diff --git a/crates/documents/src/domain/service.rs b/crates/documents/src/domain/service.rsindex 9b8c48e255..03efed36fa 100644--- a/crates/documents/src/domain/service.rs+++ b/crates/documents/src/domain/service.rs@@ -60,11 +60,10 @@ use super::events::{DocumentInteractionMetadata, DocumentMacroEvent, DocumentUpdatedMetadata, InteractionReason,};use super::models::{- CloudFrontConfig, CommentThread, CopyDocumentRepoArgs, CreateDocumentRepoArgs,- CreateTaskRequest, DocumentError, DocumentTeamShareResponse, EditDocumentRepoArgs,- EditDocumentServiceArgs, EmailImportRepoOutcome, FileTypeUpdate, GithubPullRequest,- GithubPullRequestsResponse, ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,- TeamTaskMetadata,+ CloudFrontConfig, CopyDocumentRepoArgs, CreateDocumentRepoArgs, CreateTaskRequest,+ DocumentError, DocumentTeamShareResponse, EditDocumentRepoArgs, EditDocumentServiceArgs,+ EmailImportRepoOutcome, FileTypeUpdate, GithubPullRequest, GithubPullRequestsResponse,+ ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName, TeamTaskMetadata,};#[cfg(feature = "document_create")]use super::ports::create::DocumentCreationService;@@ -1143,16 +1142,6 @@ impl<.map_err(|e| DocumentError::Internal(e.into()))}- async fn get_document_comments(- &self,- entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,- ) -> Result<Vec<CommentThread>, DocumentError> {- self.repo- .get_document_comments(&entity_access_receipt.entity().entity_id)- .await- .map_err(|e| DocumentError::Internal(e.into()))- }-#[tracing::instrument(err, skip(self))]async fn get_project_name(&self, project_id: &str) -> Result<String, DocumentError> {self.repodiff --git a/crates/documents/src/inbound/axum_router/tests.rs b/crates/documents/src/inbound/axum_router/tests.rsindex 9a10045c10..e5fdaebd7e 100644--- a/crates/documents/src/inbound/axum_router/tests.rs+++ b/crates/documents/src/inbound/axum_router/tests.rs@@ -52,9 +52,9 @@ use crate::{create::DocumentCreator,events::InteractionReason,models::{- CommentThread, CreateDocumentRepoArgs, CreateTaskRequest, DocumentError,- DocumentTeamShareResponse, EditDocumentServiceArgs, GithubPullRequestsResponse,- ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,+ CreateDocumentRepoArgs, CreateTaskRequest, DocumentError, DocumentTeamShareResponse,+ EditDocumentServiceArgs, GithubPullRequestsResponse, ImportEmailAttachmentRepoArgs,+ LocationQueryParams, TaskBranchName,},ports::{DocumentContentEventService, DocumentService, create::DocumentCreationService},response::{@@ -290,13 +290,6 @@ impl DocumentService for FakeDocumentService {panic!("unexpected get_document_text call")}- async fn get_document_comments(- &self,- _entity_access_receipt: EntityAccessReceipt<entity_access::domain::models::ViewAccessLevel>,- ) -> Result<Vec<CommentThread>, DocumentError> {- panic!("unexpected get_document_comments call")- }-async fn create_document(&self,user_id: MacroUserIdStr<'static>,diff --git a/crates/documents/src/inbound/toolset.rs b/crates/documents/src/inbound/toolset.rsindex 2e00beec1e..b80e850d9b 100644--- a/crates/documents/src/inbound/toolset.rs+++ b/crates/documents/src/inbound/toolset.rs@@ -12,6 +12,7 @@ mod upload_file;mod test;use crate::{+ domain::comments::{DocumentCommentReader, DocumentComments},domain::create::DocumentCreator,domain::ports::DocumentService,domain::ports::create::DocumentCreationService,@@ -28,7 +29,7 @@ use crate::{},outbound::{document_bytes_upload::ReqwestDocumentBytesUploader,- markdown_init::LexicalSyncMarkdownInitializer,+ lexical_comment_marks::LexicalCommentMarks, markdown_init::LexicalSyncMarkdownInitializer,},};use activity::{Actor, Attribution};@@ -37,6 +38,7 @@ use bot_id::BotId;use entity_access::domain::ports::EntityAccessService;use lexical_client::LexicalClient;use macro_user_id::user_id::MacroUserIdStr;+use messages::domain::api::MessageReader;use std::sync::Arc;use sync_service_client::SyncServiceClient;@@ -71,6 +73,9 @@ pub struct DocumentToolContext</// Editing worker service for the EditDocument tool.pub editing: Arc<EDSvc>,+ /// A document's comment threads, read by the ReadContent tool.+ pub comments: Arc<dyn DocumentComments>,+/// Permission-scoped deterministic spreadsheet workflows.pub spreadsheet: Arc<crate::domain::spreadsheet::SpreadsheetService<DSvc, EDSvc>>,@@ -100,6 +105,7 @@ impl<sync_service_client: self.sync_service_client.clone(),creator: self.creator.clone(),editing: self.editing.clone(),+ comments: self.comments.clone(),spreadsheet: self.spreadsheet.clone(),document_permission_jwt_secret: self.document_permission_jwt_secret.clone(),recorder: self.recorder.clone(),@@ -122,6 +128,7 @@ impl<sync_service_client: SyncServiceClient,editing: EDSvc,document_permission_jwt_secret: String,+ messages: Arc<dyn MessageReader>,) -> Self {let service = Arc::new(service);let lexical_client = Arc::new(lexical_client);@@ -135,6 +142,10 @@ impl<ReqwestDocumentBytesUploader::default(),NoOpDocumentMentionTracker,);+ let comments = Arc::new(DocumentCommentReader::new(+ messages,+ LexicalCommentMarks::new(lexical_client.clone()),+ ));let editing = Arc::new(editing);let spreadsheet = Arc::new(crate::domain::spreadsheet::SpreadsheetService::new(service.clone(),@@ -149,6 +160,7 @@ impl<sync_service_client,creator,editing,+ comments,spreadsheet,document_permission_jwt_secret,recorder: Arc::new(ai_usage::NoOpUsageRecorder),diff --git a/crates/documents/src/inbound/toolset/edit_document/test.rs b/crates/documents/src/inbound/toolset/edit_document/test.rsindex e32e3c8ef3..71aeb16931 100644--- a/crates/documents/src/inbound/toolset/edit_document/test.rs+++ b/crates/documents/src/inbound/toolset/edit_document/test.rs@@ -5,9 +5,9 @@ use std::sync::{Arc, Mutex};use crate::domain::content::DocumentContent;use crate::domain::events::InteractionReason;use crate::domain::models::{- CommentThread, CreateDocumentRepoArgs, CreateTaskRequest, DocumentError,- DocumentTeamShareResponse, EditDocumentServiceArgs, GithubPullRequestsResponse,- ImportEmailAttachmentRepoArgs, LocationQueryParams, TaskBranchName,+ CreateDocumentRepoArgs, CreateTaskRequest, DocumentError, DocumentTeamShareResponse,+ EditDocumentServiceArgs, GithubPullRequestsResponse, ImportEmailAttachmentRepoArgs,+ LocationQueryParams, TaskBranchName,};use crate::domain::permission_token::decode_permission_token;use crate::domain::ports::editing::{EditMode, EditResult, EditingWorkerService};@@ -114,13 +114,6 @@ impl DocumentService for FakeDocumentService {panic!("unexpected get_document_text call")}- async fn get_document_comments(- &self,- _entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,- ) -> Result<Vec<CommentThread>, DocumentError> {- panic!("unexpected get_document_comments call")- }-async fn create_document(&self,_user_id: MacroUserIdStr<'static>,@@ -460,6 +453,7 @@ fn tool_context(),editing,"unused-jwt-secret".to_string(),+ std::sync::Arc::new(messages::domain::api::MockMessageReader::new()),))}diff --git a/crates/documents/src/inbound/toolset/read_content.rs b/crates/documents/src/inbound/toolset/read_content.rsindex a4af6a6fb2..dbd303053c 100644--- a/crates/documents/src/inbound/toolset/read_content.rs+++ b/crates/documents/src/inbound/toolset/read_content.rs@@ -4,7 +4,8 @@ use ai_toolset::{ToolAnnotated, ToolAnnotations};use std::str::FromStr;use crate::domain::{- models::{CommentThread, LocationQueryParams},+ comments::DocumentDiscussion,+ models::LocationQueryParams,ports::{DocumentService, create::DocumentCreationService, editing::EditingWorkerService},response::LocationResponseV3,};@@ -88,13 +89,18 @@ const MAX_INLINE_TEXT_BYTES: usize = 512 * 1024;pub struct ReadContentResponse {/// The content of the documentpub content: Content,- /// Any comments on the document- pub comments: Vec<CommentThread>,+ /// The comment threads on the document, oldest first: inline comments+ /// with the text they are on, and Discussion comments on the whole+ /// document. Each thread lists its first comment followed by the replies.+ pub comments: Vec<DocumentDiscussion>,}#[derive(Debug, Deserialize, JsonSchema, Clone, Default)]#[serde(rename_all = "camelCase")]-#[schemars(title = "ReadContent", description = "Retrieve a documents content")]+#[schemars(+ title = "ReadContent",+ description = "Retrieve a document's content and its comment threads, including inline comments with the text they are on, Discussion comments, replies and resolved state."+)]pub struct ReadContent {#[schemars(description = "The id of the document you want to retrieve content for.")]pub document_id: Uuid,@@ -239,8 +245,8 @@ where};let comments = service_context- .service- .get_document_comments(entity_access_receipt)+ .comments+ .discussions(entity_access_receipt).await.map_err(|e| ToolCallError {description: "unable to get document comments".to_string(),diff --git a/crates/documents/src/outbound.rs b/crates/documents/src/outbound.rsindex 5cecbe5412..889b3e3240 100644--- a/crates/documents/src/outbound.rs+++ b/crates/documents/src/outbound.rs@@ -7,6 +7,8 @@ pub mod document_sync;pub mod document_bytes_upload;#[cfg(feature = "ai_tools")]pub mod editing_worker_client;+#[cfg(feature = "ai_tools")]+pub mod lexical_comment_marks;#[cfg(feature = "markdown_init")]pub mod markdown_init;#[cfg(feature = "document_create_adapters")]diff --git a/crates/documents/src/outbound/lexical_comment_marks.rs b/crates/documents/src/outbound/lexical_comment_marks.rsnew file mode 100644index 0000000000..03e296737f--- /dev/null+++ b/crates/documents/src/outbound/lexical_comment_marks.rs@@ -0,0 +1,35 @@+//! Lexical-service lookup of the live text a comment mark covers.++use std::sync::Arc;++use lexical_client::LexicalClient;+use uuid::Uuid;++use crate::domain::comments::CommentMarks;++/// [`CommentMarks`] resolved by the lexical service from the synced document.+pub struct LexicalCommentMarks {+ lexical: Arc<LexicalClient>,+}++impl LexicalCommentMarks {+ /// Resolve marks through `lexical`.+ pub fn new(lexical: Arc<LexicalClient>) -> Self {+ Self { lexical }+ }+}++#[async_trait::async_trait]+impl CommentMarks for LexicalCommentMarks {+ async fn marked_text(+ &self,+ document_id: &str,+ mark_id: Uuid,+ ) -> anyhow::Result<Option<String>> {+ Ok(self+ .lexical+ .resolve_comment_mark(document_id, &mark_id.to_string())+ .await?+ .map(|mark| mark.marked_text))+ }+}diff --git a/crates/documents/src/outbound/pg_document_repo.rs b/crates/documents/src/outbound/pg_document_repo.rsindex c69445c54f..3724ec03bb 100644--- a/crates/documents/src/outbound/pg_document_repo.rs+++ b/crates/documents/src/outbound/pg_document_repo.rs@@ -23,9 +23,9 @@ use sqlx::Row;use crate::domain::content::{DocumentContent, DocumentContentState};use crate::domain::models::{- BranchNameContext, Comment, CommentThread, CopyDocumentRepoArgs, CreateDocumentRepoArgs,- DocumentError, DocumentTeamShare, EditDocumentRepoArgs, EmailImportRepoOutcome,- ImportEmailAttachmentRepoArgs, TeamTaskMetadata, Thread,+ BranchNameContext, CopyDocumentRepoArgs, CreateDocumentRepoArgs, DocumentError,+ DocumentTeamShare, EditDocumentRepoArgs, EmailImportRepoOutcome, ImportEmailAttachmentRepoArgs,+ TeamTaskMetadata,};use crate::domain::ports::DocumentRepo;@@ -1166,100 +1166,4 @@ impl DocumentRepo for PgDocumentRepo {transaction.commit().await?;Ok(())}-- #[tracing::instrument(err, skip(self))]- async fn get_document_comments(- &self,- document_id: &str,- ) -> Result<Vec<CommentThread>, Self::Err> {- let threads = sqlx::query!(- r#"- SELECT- t.id as "thread_id!",- t.resolved as "resolved!",- t."documentId" as "document_id!",- t."createdAt"::timestamptz as "created_at",- t."updatedAt"::timestamptz as "updated_at",- t."deletedAt"::timestamptz as "deleted_at",- t.metadata as "metadata",- t.owner as "owner!"- FROM "Thread" t- WHERE t."documentId" = $1 AND t."deletedAt" IS NULL- "#,- document_id,- )- .map(|row| Thread {- thread_id: row.thread_id,- resolved: row.resolved,- document_id: row.document_id,- created_at: row.created_at,- updated_at: row.updated_at,- deleted_at: row.deleted_at,- metadata: row.metadata,- owner: row.owner,- })- .fetch_all(&self.pool)- .await?;-- let comments = sqlx::query!(- r#"- SELECT- c.id as "comment_id!",- c."threadId" as "thread_id!",- c.owner as "owner!",- c.sender,- c.text as "text!",- c.metadata,- c."createdAt"::timestamptz as "created_at",- c."updatedAt"::timestamptz as "updated_at",- c."deletedAt"::timestamptz as "deleted_at",- c.order- FROM "Comment" c- JOIN "Thread" t ON c."threadId" = t.id- WHERE t."documentId" = $1- AND t."deletedAt" IS NULL- AND c."deletedAt" IS NULL- ORDER BY c."createdAt" ASC- "#,- document_id,- )- .map(|row| Comment {- comment_id: row.comment_id,- thread_id: row.thread_id,- owner: row.owner,- sender: row.sender,- text: row.text,- metadata: row.metadata,- created_at: row.created_at,- updated_at: row.updated_at,- deleted_at: row.deleted_at,- order: row.order,- })- .fetch_all(&self.pool)- .await?;-- let mut comments_by_thread: std::collections::HashMap<i64, Vec<Comment>> =- std::collections::HashMap::new();- for comment in comments {- comments_by_thread- .entry(comment.thread_id)- .or_default()- .push(comment);- }-- let comment_threads = threads- .into_iter()- .map(|thread| {- let thread_comments = comments_by_thread- .remove(&thread.thread_id)- .unwrap_or_default();- CommentThread {- thread,- comments: thread_comments,- }- })- .collect();-- Ok(comment_threads)- }}diff --git a/crates/documents/src/outbound/pg_document_repo/tests.rs b/crates/documents/src/outbound/pg_document_repo/tests.rsindex 69c8af2ac4..542f4937c3 100644--- a/crates/documents/src/outbound/pg_document_repo/tests.rs+++ b/crates/documents/src/outbound/pg_document_repo/tests.rs@@ -2263,159 +2263,6 @@ async fn test_edit_document_channel_share_idempotent(pool: Pool<Postgres>) {"Should still have exactly 1 channel row after idempotent upsert");}--#[sqlx::test(- migrator = "MACRO_DB_MIGRATIONS",- fixtures(- path = "../../../fixtures",- scripts("document_pdf_comments_and_highlights")- )-)]-async fn test_fetch_document_comments(pool: Pool<Postgres>) -> anyhow::Result<()> {- let repo = PgDocumentRepo::new(pool);-- // Test fetching comments for document-with-comments (which has 3 threads and 7 comments)- let comment_threads = repo.get_document_comments("document-with-comments").await?;-- // Verify we got all threads- assert_eq!(comment_threads.len(), 4);-- // Map threads by ID for easier testing- let thread_map: std::collections::HashMap<i64, &crate::domain::models::CommentThread> =- comment_threads- .iter()- .map(|t| (t.thread.thread_id, t))- .collect();-- // Check thread 1001 (unresolved with 3 comments)- let thread_1001 = thread_map.get(&1001).expect("Thread 1001 should exist");- assert_eq!(thread_1001.comments.len(), 3);- assert!(!thread_1001.thread.resolved);-- // Check thread 1002 (resolved with 3 comments)- let thread_1002 = thread_map.get(&1002).expect("Thread 1002 should exist");- assert_eq!(thread_1002.comments.len(), 3);- assert!(thread_1002.thread.resolved);-- // Check thread 1003 (unresolved with 1 comment)- let thread_1003 = thread_map.get(&1003).expect("Thread 1003 should exist");- assert_eq!(thread_1003.comments.len(), 1);- assert!(!thread_1003.thread.resolved);-- // Check specific comment content for thread 1001- let first_comment = &thread_1001.comments[0];- assert_eq!(first_comment.text, "Initial question on page 1");- assert_eq!(first_comment.sender, Some("[email protected]".to_string()));-- Ok(())-}--#[sqlx::test(- migrator = "MACRO_DB_MIGRATIONS",- fixtures(- path = "../../../fixtures",- scripts("document_pdf_comments_and_highlights")- )-)]-async fn test_fetch_document_comments_empty(pool: Pool<Postgres>) -> anyhow::Result<()> {- let repo = PgDocumentRepo::new(pool);-- // This document ID doesn't exist in our fixture, so should return empty results- let comment_threads = repo.get_document_comments("non-existent-document").await?;-- // Verify we got an empty list- assert_eq!(comment_threads.len(), 0);-- Ok(())-}--#[sqlx::test(- migrator = "MACRO_DB_MIGRATIONS",- fixtures(- path = "../../../fixtures",- scripts("document_pdf_comments_and_highlights")- )-)]-async fn test_thread_deletion(pool: Pool<Postgres>) -> anyhow::Result<()> {- let repo = PgDocumentRepo::new(pool.clone());-- // First verify we have 2 threads in document-delete-test- let threads_before = repo.get_document_comments("document-delete-test").await?;- assert_eq!(threads_before.len(), 2);-- // Now mark one thread as deleted- sqlx::query!(- r#"- UPDATE "Thread"- SET "deletedAt" = NOW()- WHERE "id" = 3001- "#- )- .execute(&pool)- .await?;-- // Fetch comments again - deleted threads should be filtered out- let threads_after = repo.get_document_comments("document-delete-test").await?;-- // Verify only one thread remains and it's the correct one- assert_eq!(threads_after.len(), 1);- assert_eq!(threads_after[0].thread.thread_id, 3002);-- Ok(())-}--#[sqlx::test(- migrator = "MACRO_DB_MIGRATIONS",- fixtures(- path = "../../../fixtures",- scripts("document_pdf_comments_and_highlights")- )-)]-async fn test_comment_deletion(pool: Pool<Postgres>) -> anyhow::Result<()> {- let repo = PgDocumentRepo::new(pool.clone());-- // First verify thread 1001 has 3 comments- let threads_before = repo.get_document_comments("document-with-comments").await?;- let thread_1001_before = threads_before- .iter()- .find(|t| t.thread.thread_id == 1001)- .expect("Thread 1001 should exist");- assert_eq!(thread_1001_before.comments.len(), 3);-- // Now mark one comment as deleted- sqlx::query!(- r#"- UPDATE "Comment"- SET "deletedAt" = NOW()- WHERE "id" = 10001- "#- )- .execute(&pool)- .await?;-- // Fetch comments again - deleted comments should be filtered out- let threads_after = repo.get_document_comments("document-with-comments").await?;- let thread_1001_after = threads_after- .iter()- .find(|t| t.thread.thread_id == 1001)- .expect("Thread 1001 should exist");-- // Verify one comment was filtered out- assert_eq!(thread_1001_after.comments.len(), 2);-- // Verify the remaining comments are the ones we expect- let comment_ids: Vec<i64> = thread_1001_after- .comments- .iter()- .map(|c| c.comment_id)- .collect();- assert!(comment_ids.contains(&10002));- assert!(comment_ids.contains(&10003));- assert!(!comment_ids.contains(&10001));-- Ok(())-}-#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS",fixtures(path = "../../../fixtures", scripts("documents_test_data"))diff --git a/crates/github/src/domain/service/sync/test.rs b/crates/github/src/domain/service/sync/test.rsindex 1e6228f36b..c082bb0726 100644--- a/crates/github/src/domain/service/sync/test.rs+++ b/crates/github/src/domain/service/sync/test.rs@@ -280,13 +280,6 @@ impl DocumentService for StubDocumentService {unimplemented!()}- async fn get_document_comments(- &self,- _entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,- ) -> Result<Vec<documents::domain::models::CommentThread>, DocumentError> {- unimplemented!()- }-async fn handle_task_properties(&self,_user_id: MacroUserIdStr<'static>,diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rsindex f7baa9e1c9..721b9e70b1 100644--- a/services/document_cognition_service/src/api/context/test.rs+++ b/services/document_cognition_service/src/api/context/test.rs@@ -287,6 +287,7 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Arsync_service_client.as_ref().clone(),test_editing_client,"test-jwt-secret".to_string(),+ ai_tools::message_reader(pool.clone()),);let search_service_client = Arc::new(search_service_client);diff --git a/services/document_cognition_service/src/main.rs b/services/document_cognition_service/src/main.rsindex aa662f42b1..c6a6009b80 100644--- a/services/document_cognition_service/src/main.rs+++ b/services/document_cognition_service/src/main.rs@@ -330,6 +330,7 @@ async fn main() -> anyhow::Result<()> {std::sync::Arc::new(reqwest::Client::new()),),config.document_permission_jwt.as_ref().to_string(),+ ai_tools::message_reader(db.clone()),);tracing::info!("initialized document tool context");diff --git a/services/mcp_service/src/context.rs b/services/mcp_service/src/context.rsindex 4015ae38bd..e38e970553 100644--- a/services/mcp_service/src/context.rs+++ b/services/mcp_service/src/context.rs@@ -271,6 +271,7 @@ async fn build_tool_context(args: ToolContextBuildArgs<'_>) -> anyhow::Result<Tosync_service_client.clone(),ReqwestEditingWorkerClient::from_url(ai_editing_worker_url),config.document_permission_jwt.to_string(),+ ai_tools::message_reader(db.clone()),);let properties_tool_context = ai_tools::build_properties_tool_context(