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(messages): give agents the text a PDF highlight comment covers (#6895)

gbirman0d72669f1b5f
diff --git a/.sqlx/query-3838b67d205c890938a25586fbad12a81934453fb9d03e14df03c06001040144.json b/.sqlx/query-3838b67d205c890938a25586fbad12a81934453fb9d03e14df03c06001040144.json
new file mode 100644
index 0000000000..1090a16503
--- /dev/null
+++ b/.sqlx/query-3838b67d205c890938a25586fbad12a81934453fb9d03e14df03c06001040144.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT uuid, text FROM \"PdfHighlightAnchor\" WHERE uuid = ANY($1)",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "uuid",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "text",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "3838b67d205c890938a25586fbad12a81934453fb9d03e14df03c06001040144"
+}
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 c1f277881c..c479a93f27 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
@@ -4866,6 +4866,7 @@ export const ReadContentResponse = z.object({
}),
z.object({
anchorId: z.string().uuid(),
+ markedText: z.union([z.string(), z.null()]).optional(),
type: z.literal('pdfHighlight'),
}),
z.object({ anchorId: z.string().uuid(), type: z.literal('pdfPin') }),
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 128496f4d0..ac9207bf07 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
@@ -991,6 +991,10 @@ export type CommentAnchor =
* The highlight annotation.
*/
anchorId: string;
+ /**
+ * The text the highlight covers; absent when the highlight carries none.
+ */
+ markedText?: string | null;
type: 'pdfHighlight';
}
| {
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
index 36eb5c34d3..01cedf9a32 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/index.ts
@@ -1417,10 +1417,11 @@ export * from './thread';
export * from './threadAnchor';
export * from './threadAnchorOneOf';
export * from './threadAnchorOneOfFour';
+export * from './threadAnchorOneOfFourMarkedText';
export * from './threadAnchorOneOfFourType';
export * from './threadAnchorOneOfMarkedText';
-export * from './threadAnchorOneOfSix';
-export * from './threadAnchorOneOfSixType';
+export * from './threadAnchorOneOfSeven';
+export * from './threadAnchorOneOfSevenType';
export * from './threadAnchorOneOfType';
export * from './threadCreatedAt';
export * from './threadDeletedAt';
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchor.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchor.ts
index b6b0d9f938..9295730729 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchor.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchor.ts
@@ -6,7 +6,7 @@
*/
import type { ThreadAnchorOneOf } from './threadAnchorOneOf';
import type { ThreadAnchorOneOfFour } from './threadAnchorOneOfFour';
-import type { ThreadAnchorOneOfSix } from './threadAnchorOneOfSix';
+import type { ThreadAnchorOneOfSeven } from './threadAnchorOneOfSeven';
/**
* A thread's location within its document. Geometry remains annotation-owned.
@@ -14,4 +14,4 @@ import type { ThreadAnchorOneOfSix } from './threadAnchorOneOfSix';
export type ThreadAnchor =
| ThreadAnchorOneOf
| ThreadAnchorOneOfFour
- | ThreadAnchorOneOfSix;
+ | ThreadAnchorOneOfSeven;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfFour.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfFour.ts
index 9a0eb2a9a4..07dfe93b0b 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfFour.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfFour.ts
@@ -4,6 +4,7 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/
+import type { ThreadAnchorOneOfFourMarkedText } from './threadAnchorOneOfFourMarkedText';
import type { ThreadAnchorOneOfFourType } from './threadAnchorOneOfFourType';
/**
@@ -12,5 +13,10 @@ import type { ThreadAnchorOneOfFourType } from './threadAnchorOneOfFourType';
export type ThreadAnchorOneOfFour = {
/** Highlight annotation UUID. */
anchor_id: string;
+ /** The text the highlight covers, trimmed and bounded like a markdown
+snapshot. The highlight owns it and it can be edited there, so it is
+read from the highlight whenever the thread is, never stored on the
+thread. Absent when the highlight carries no text. */
+ marked_text?: ThreadAnchorOneOfFourMarkedText;
type: ThreadAnchorOneOfFourType;
};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfFourMarkedText.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfFourMarkedText.ts
new file mode 100644
index 0000000000..fd2fa017a7
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfFourMarkedText.ts
@@ -0,0 +1,14 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * The text the highlight covers, trimmed and bounded like a markdown
+snapshot. The highlight owns it and it can be edited there, so it is
+read from the highlight whenever the thread is, never stored on the
+thread. Absent when the highlight carries no text.
+ */
+export type ThreadAnchorOneOfFourMarkedText = string | null;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSeven.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSeven.ts
new file mode 100644
index 0000000000..c5c5324a5b
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSeven.ts
@@ -0,0 +1,17 @@
+/**
+ * Generated by orval v7.21.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+import type { ThreadAnchorOneOfSevenType } from './threadAnchorOneOfSevenType';
+
+/**
+ * A comment-only placeable PDF annotation. It marks a point on a page,
+not a span of text, so it has no marked text.
+ */
+export type ThreadAnchorOneOfSeven = {
+ /** Placeable annotation UUID. */
+ anchor_id: string;
+ type: ThreadAnchorOneOfSevenType;
+};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSixType.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSevenType.ts
similarity index 58%
rename from apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSixType.ts
rename to apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSevenType.ts
index 503f622e60..1c52dcef31 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSixType.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSevenType.ts
@@ -5,10 +5,10 @@
* OpenAPI spec version: 0.1.0
*/
-export type ThreadAnchorOneOfSixType =
- (typeof ThreadAnchorOneOfSixType)[keyof typeof ThreadAnchorOneOfSixType];
+export type ThreadAnchorOneOfSevenType =
+ (typeof ThreadAnchorOneOfSevenType)[keyof typeof ThreadAnchorOneOfSevenType];
// eslint-disable-next-line @typescript-eslint/no-redeclare
-export const ThreadAnchorOneOfSixType = {
+export const ThreadAnchorOneOfSevenType = {
pdf_placeable: 'pdf_placeable',
} as const;
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSix.ts b/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSix.ts
deleted file mode 100644
index 7a7b021585..0000000000
--- a/apps/web/src/lib/service-clients/service-storage/generated/schemas/threadAnchorOneOfSix.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * Generated by orval v7.21.0 🍺
- * Do not edit manually.
- * document_storage_service
- * OpenAPI spec version: 0.1.0
- */
-import type { ThreadAnchorOneOfSixType } from './threadAnchorOneOfSixType';
-
-/**
- * A comment-only placeable PDF annotation.
- */
-export type ThreadAnchorOneOfSix = {
- /** Placeable annotation UUID. */
- anchor_id: string;
- type: ThreadAnchorOneOfSixType;
-};
diff --git a/apps/web/src/lib/service-clients/service-storage/generated/zod.ts b/apps/web/src/lib/service-clients/service-storage/generated/zod.ts
index 64cb68b7bf..6f3db0a4bd 100644
--- a/apps/web/src/lib/service-clients/service-storage/generated/zod.ts
+++ b/apps/web/src/lib/service-clients/service-storage/generated/zod.ts
@@ -28648,6 +28648,12 @@ export const messageTimelineResponse = zod
anchor_id: zod
.uuid()
.describe('Highlight annotation UUID.'),
+ marked_text: zod
+ .string()
+ .nullish()
+ .describe(
+ 'The text the highlight covers, trimmed and bounded like a markdown\nsnapshot. The highlight owns it and it can be edited there, so it is\nread from the highlight whenever the thread is, never stored on the\nthread. Absent when the highlight carries no text.'
+ ),
type: zod.enum(['pdf_highlight']),
})
.describe(
@@ -28661,7 +28667,7 @@ export const messageTimelineResponse = zod
type: zod.enum(['pdf_placeable']),
})
.describe(
- 'A comment-only placeable PDF annotation.'
+ 'A comment-only placeable PDF annotation. It marks a point on a page,\nnot a span of text, so it has no marked text.'
),
])
.describe(
@@ -30193,6 +30199,12 @@ export const entityMessageGetThreadResponse = zod
anchor_id: zod
.uuid()
.describe('Highlight annotation UUID.'),
+ marked_text: zod
+ .string()
+ .nullish()
+ .describe(
+ 'The text the highlight covers, trimmed and bounded like a markdown\nsnapshot. The highlight owns it and it can be edited there, so it is\nread from the highlight whenever the thread is, never stored on the\nthread. Absent when the highlight carries no text.'
+ ),
type: zod.enum(['pdf_highlight']),
})
.describe('An independently existing PDF highlight.'),
@@ -30203,7 +30215,9 @@ export const entityMessageGetThreadResponse = zod
.describe('Placeable annotation UUID.'),
type: zod.enum(['pdf_placeable']),
})
- .describe('A comment-only placeable PDF annotation.'),
+ .describe(
+ 'A comment-only placeable PDF annotation. It marks a point on a page,\nnot a span of text, so it has no marked text.'
+ ),
])
.describe(
"A thread's location within its document. Geometry remains annotation-owned."
@@ -30279,6 +30293,12 @@ export const entityMessageDeleteThreadResponse = zod
zod
.object({
anchor_id: zod.uuid().describe('Highlight annotation UUID.'),
+ marked_text: zod
+ .string()
+ .nullish()
+ .describe(
+ 'The text the highlight covers, trimmed and bounded like a markdown\nsnapshot. The highlight owns it and it can be edited there, so it is\nread from the highlight whenever the thread is, never stored on the\nthread. Absent when the highlight carries no text.'
+ ),
type: zod.enum(['pdf_highlight']),
})
.describe('An independently existing PDF highlight.'),
@@ -30287,7 +30307,9 @@ export const entityMessageDeleteThreadResponse = zod
anchor_id: zod.uuid().describe('Placeable annotation UUID.'),
type: zod.enum(['pdf_placeable']),
})
- .describe('A comment-only placeable PDF annotation.'),
+ .describe(
+ 'A comment-only placeable PDF annotation. It marks a point on a page,\nnot a span of text, so it has no marked text.'
+ ),
])
.describe(
"A thread's location within its document. Geometry remains annotation-owned."
@@ -30376,6 +30398,12 @@ export const entityMessagePatchThreadResponse = zod
zod
.object({
anchor_id: zod.uuid().describe('Highlight annotation UUID.'),
+ marked_text: zod
+ .string()
+ .nullish()
+ .describe(
+ 'The text the highlight covers, trimmed and bounded like a markdown\nsnapshot. The highlight owns it and it can be edited there, so it is\nread from the highlight whenever the thread is, never stored on the\nthread. Absent when the highlight carries no text.'
+ ),
type: zod.enum(['pdf_highlight']),
})
.describe('An independently existing PDF highlight.'),
@@ -30384,7 +30412,9 @@ export const entityMessagePatchThreadResponse = zod
anchor_id: zod.uuid().describe('Placeable annotation UUID.'),
type: zod.enum(['pdf_placeable']),
})
- .describe('A comment-only placeable PDF annotation.'),
+ .describe(
+ 'A comment-only placeable PDF annotation. It marks a point on a page,\nnot a span of text, so it has no marked text.'
+ ),
])
.describe(
"A thread's location within its document. Geometry remains annotation-owned."
diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.json
index 01fb32759d..3f5978a42c 100644
--- a/apps/web/src/lib/service-clients/service-storage/openapi.json
+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json
@@ -30337,6 +30337,10 @@
"format": "uuid",
"description": "Highlight annotation UUID."
},
+ "marked_text": {
+ "type": ["string", "null"],
+ "description": "The text the highlight covers, trimmed and bounded like a markdown\nsnapshot. The highlight owns it and it can be edited there, so it is\nread from the highlight whenever the thread is, never stored on the\nthread. Absent when the highlight carries no text."
+ },
"type": {
"type": "string",
"enum": ["pdf_highlight"]
@@ -30345,7 +30349,7 @@
},
{
"type": "object",
- "description": "A comment-only placeable PDF annotation.",
+ "description": "A comment-only placeable PDF annotation. It marks a point on a page,\nnot a span of text, so it has no marked text.",
"required": ["anchor_id", "type"],
"properties": {
"anchor_id": {
diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rs
index f0009ef47e..9e5181d3ab 100644
--- a/crates/agent_harness/src/domain/model.rs
+++ b/crates/agent_harness/src/domain/model.rs
@@ -321,19 +321,36 @@ pub struct PriorMessage {
pub content: String,
}
-/// Where in a document a comment thread sits. The mark id alone names a
+/// Where in a document a comment thread sits. An annotation id alone names a
/// location the agent has no way to resolve: the document body it can read
-/// carries no marks, so the text the comment covers travels with the id.
+/// carries no marks or highlights, so the text the comment covers travels
+/// with the id.
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct CommentAnchor {
- /// Lexical mark the thread is attached to.
- pub mark_id: String,
- /// The marked text as it read when the comment was posted. Absent on
- /// threads anchored before snapshots were captured.
- pub marked_text: Option<String>,
- /// The mark as the document reads now. Absent when the document no longer
- /// carries it or the lookup failed, leaving the snapshot as the fallback.
- pub current: Option<MarkedPassage>,
+pub enum CommentAnchor {
+ /// A comment mark in a markdown document.
+ Mark {
+ /// Lexical mark the thread is attached to.
+ mark_id: String,
+ /// The marked text as it read when the comment was posted. Absent on
+ /// threads anchored before snapshots were captured.
+ marked_text: Option<String>,
+ /// The mark as the document reads now. Absent when the document no longer
+ /// carries it or the lookup failed, leaving the snapshot as the fallback.
+ current: Option<MarkedPassage>,
+ },
+ /// A highlight on a PDF.
+ PdfHighlight {
+ /// Highlight annotation the thread is attached to.
+ anchor_id: String,
+ /// The text the highlight covers, read from the highlight. Absent when
+ /// the highlight carries none.
+ marked_text: Option<String>,
+ },
+ /// A point pinned on a PDF page, which covers no text.
+ PdfPin {
+ /// Pin annotation the thread is attached to.
+ anchor_id: String,
+ },
}
/// A comment mark resolved against the live document, both fields bounded.
diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rs
index 10f1a11300..dc2259f5cf 100644
--- a/crates/agent_harness/src/domain/service/test.rs
+++ b/crates/agent_harness/src/domain/service/test.rs
@@ -906,7 +906,7 @@ async fn open_sends_context_but_not_agent_instructions_to_the_agent_prompt() {
#[tokio::test]
async fn open_sends_the_comment_anchor_the_prompt_was_posted_on() {
let context = ConversationContext {
- anchor: Some(CommentAnchor {
+ anchor: Some(CommentAnchor::Mark {
mark_id: "0199f3d4-0000-7000-8000-00000000000a".to_owned(),
marked_text: Some("the marked phrase".to_owned()),
current: None,
diff --git a/crates/agent_harness/src/outbound/agent_prompt_composer.rs b/crates/agent_harness/src/outbound/agent_prompt_composer.rs
index cb892824ed..e215dbcacf 100644
--- a/crates/agent_harness/src/outbound/agent_prompt_composer.rs
+++ b/crates/agent_harness/src/outbound/agent_prompt_composer.rs
@@ -4,7 +4,7 @@ use lexical_client::LexicalClient;
use lexical_client::parse_markdown::{AgentContextAnchor, AgentContextMessage};
use crate::domain::error::{HarnessError, Result};
-use crate::domain::model::ConversationContext;
+use crate::domain::model::{CommentAnchor, ConversationContext};
use crate::domain::ports::AgentPromptComposer;
/// Lexical-service-backed agent prompt composer.
@@ -36,14 +36,29 @@ impl AgentPromptComposer for LexicalAgentPromptComposer {
})
.collect::<Vec<_>>()
});
- let anchor = context
- .and_then(|context| context.anchor.as_ref())
- .map(|anchor| AgentContextAnchor {
- mark_id: &anchor.mark_id,
- marked_text: anchor.marked_text.as_deref(),
- current_marked_text: anchor.current.as_ref().map(|c| c.marked_text.as_str()),
- surrounding_text: anchor.current.as_ref().map(|c| c.surrounding_text.as_str()),
- });
+ let anchor =
+ context
+ .and_then(|context| context.anchor.as_ref())
+ .map(|anchor| match anchor {
+ CommentAnchor::Mark {
+ mark_id,
+ marked_text,
+ current,
+ } => AgentContextAnchor::Markdown {
+ mark_id,
+ marked_text: marked_text.as_deref(),
+ current_marked_text: current.as_ref().map(|c| c.marked_text.as_str()),
+ surrounding_text: current.as_ref().map(|c| c.surrounding_text.as_str()),
+ },
+ CommentAnchor::PdfHighlight {
+ anchor_id,
+ marked_text,
+ } => AgentContextAnchor::PdfHighlight {
+ anchor_id,
+ marked_text: marked_text.as_deref(),
+ },
+ CommentAnchor::PdfPin { anchor_id } => AgentContextAnchor::PdfPin { anchor_id },
+ });
self.lexical
.compose_agent_context(
diff --git a/crates/agent_harness/src/outbound/agent_prompt_composer/test.rs b/crates/agent_harness/src/outbound/agent_prompt_composer/test.rs
index d40a07b4bc..43bd176598 100644
--- a/crates/agent_harness/src/outbound/agent_prompt_composer/test.rs
+++ b/crates/agent_harness/src/outbound/agent_prompt_composer/test.rs
@@ -54,7 +54,7 @@ async fn the_comment_anchor_reaches_the_lexical_service_beside_the_history() {
"Raw prompt",
None,
Some(&ConversationContext {
- anchor: Some(CommentAnchor {
+ anchor: Some(CommentAnchor::Mark {
mark_id: "mark-1".to_owned(),
marked_text: Some("the marked phrase".to_owned()),
current: Some(MarkedPassage {
@@ -71,6 +71,7 @@ async fn the_comment_anchor_reaches_the_lexical_service_beside_the_history() {
.await
.unwrap();
let body = received.lock().unwrap().clone().unwrap();
+ assert_eq!(body["anchor"]["type"], "markdown");
assert_eq!(body["anchor"]["markId"], "mark-1");
assert_eq!(body["anchor"]["markedText"], "the marked phrase");
assert_eq!(body["anchor"]["currentMarkedText"], "the edited phrase");
@@ -86,7 +87,7 @@ async fn the_comment_anchor_reaches_the_lexical_service_beside_the_history() {
"Raw prompt",
None,
Some(&ConversationContext {
- anchor: Some(CommentAnchor {
+ anchor: Some(CommentAnchor::Mark {
mark_id: "mark-2".to_owned(),
marked_text: None,
current: None,
@@ -100,5 +101,51 @@ async fn the_comment_anchor_reaches_the_lexical_service_beside_the_history() {
assert_eq!(body["anchor"]["markId"], "mark-2");
assert!(body["anchor"].get("markedText").is_none());
assert!(body["anchor"].get("currentMarkedText").is_none());
+
+ let sent = |anchor: CommentAnchor| {
+ let composer = &composer;
+ let received = received.clone();
+ async move {
+ composer
+ .compose(
+ "Raw prompt",
+ None,
+ Some(&ConversationContext {
+ anchor: Some(anchor),
+ messages: vec![],
+ }),
+ )
+ .await
+ .unwrap();
+ received.lock().unwrap().clone().unwrap()["anchor"].clone()
+ }
+ };
+ assert_eq!(
+ sent(CommentAnchor::PdfHighlight {
+ anchor_id: "highlight-1".to_owned(),
+ marked_text: Some("the highlighted words".to_owned()),
+ })
+ .await,
+ serde_json::json!({
+ "type": "pdfHighlight",
+ "anchorId": "highlight-1",
+ "markedText": "the highlighted words"
+ })
+ );
+ assert_eq!(
+ sent(CommentAnchor::PdfHighlight {
+ anchor_id: "highlight-2".to_owned(),
+ marked_text: None,
+ })
+ .await,
+ serde_json::json!({ "type": "pdfHighlight", "anchorId": "highlight-2" })
+ );
+ assert_eq!(
+ sent(CommentAnchor::PdfPin {
+ anchor_id: "pin-1".to_owned(),
+ })
+ .await,
+ serde_json::json!({ "type": "pdfPin", "anchorId": "pin-1" })
+ );
server.abort();
}
diff --git a/crates/agent_harness/src/outbound/channel_prompt_context.rs b/crates/agent_harness/src/outbound/channel_prompt_context.rs
index ba46befbb3..ad392471bb 100644
--- a/crates/agent_harness/src/outbound/channel_prompt_context.rs
+++ b/crates/agent_harness/src/outbound/channel_prompt_context.rs
@@ -158,9 +158,11 @@ impl<Access: ContextAuthorizer, Marks: MarkReader> MessagePromptContext
}
/// Where a document discussion sits, read from the thread the prompt was posted
-/// in, and what that mark covers in the document now. Only documents anchor
-/// discussions, so a channel prompt never pays for the lookup. A failed live
-/// lookup leaves the stored snapshot to stand in rather than failing the prompt.
+/// in, and what that anchor covers now. Only documents anchor discussions, so a
+/// channel prompt never pays for the lookup. A PDF highlight's text arrives with
+/// the thread; a markdown mark is resolved against the live document, and a
+/// failed lookup leaves the stored snapshot to stand in rather than failing the
+/// prompt.
async fn anchor(
messages: &dyn MessageReader,
marks: &impl MarkReader,
@@ -174,16 +176,27 @@ async fn anchor(
.get_thread(access, origin.thread_id)
.await
.map_err(|error| HarnessError::PromptContext(rootcause::report!(error).into()))?;
- // PDF anchors name an annotation the agent cannot read either, but their
- // text is owned by the annotation rather than the thread.
- let Some(ThreadAnchor::Markdown {
- mark_id,
- marked_text,
- }) = thread.state.anchor
- else {
- return Ok(None);
+ let (mark_id, marked_text) = match thread.state.anchor {
+ None => return Ok(None),
+ Some(ThreadAnchor::PdfHighlight {
+ anchor_id,
+ marked_text,
+ }) => {
+ return Ok(Some(CommentAnchor::PdfHighlight {
+ anchor_id: anchor_id.to_string(),
+ marked_text,
+ }));
+ }
+ Some(ThreadAnchor::PdfPlaceable { anchor_id }) => {
+ return Ok(Some(CommentAnchor::PdfPin {
+ anchor_id: anchor_id.to_string(),
+ }));
+ }
+ Some(ThreadAnchor::Markdown {
+ mark_id,
+ marked_text,
+ }) => (mark_id.to_string(), marked_text),
};
- let mark_id = mark_id.to_string();
let current = marks
.resolve(&origin.parent.entity_id(), &mark_id)
.await
@@ -197,7 +210,7 @@ async fn anchor(
})
.ok()
.flatten();
- Ok(Some(CommentAnchor {
+ Ok(Some(CommentAnchor::Mark {
mark_id,
marked_text,
current,
diff --git a/crates/agent_harness/src/outbound/channel_prompt_context/test.rs b/crates/agent_harness/src/outbound/channel_prompt_context/test.rs
index f3b0c0d7dd..a3db76f6ff 100644
--- a/crates/agent_harness/src/outbound/channel_prompt_context/test.rs
+++ b/crates/agent_harness/src/outbound/channel_prompt_context/test.rs
@@ -214,7 +214,7 @@ async fn a_marked_discussion_names_its_mark_and_the_text_it_covers() {
.unwrap();
assert_eq!(
context.anchor,
- Some(CommentAnchor {
+ Some(CommentAnchor::Mark {
mark_id: mark_id.to_string(),
marked_text: Some("the marked phrase".to_owned()),
current: None,
@@ -239,7 +239,7 @@ async fn a_discussion_anchored_before_snapshots_still_names_its_mark() {
.unwrap();
assert_eq!(
context.anchor,
- Some(CommentAnchor {
+ Some(CommentAnchor::Mark {
mark_id: mark_id.to_string(),
marked_text: None,
current: None,
@@ -292,7 +292,7 @@ async fn a_marked_discussion_reads_the_mark_as_the_document_has_it_now() {
.unwrap();
assert_eq!(
context.anchor,
- Some(CommentAnchor {
+ Some(CommentAnchor::Mark {
mark_id: mark_id.to_string(),
marked_text: Some("the original phrase".to_owned()),
current: Some(current),
@@ -317,10 +317,55 @@ async fn a_failed_live_lookup_falls_back_to_the_snapshot() {
.unwrap();
assert_eq!(
context.anchor,
- Some(CommentAnchor {
+ Some(CommentAnchor::Mark {
mark_id: mark_id.to_string(),
marked_text: Some("the original phrase".to_owned()),
current: None,
})
);
}
+
+#[tokio::test]
+async fn a_pdf_highlight_discussion_names_the_text_the_highlight_covers() {
+ let anchor_id = Uuid::from_u128(11);
+ let adapter = MessagePromptContextAdapter::new(
+ Arc::new(reader(Some(ThreadAnchor::PdfHighlight {
+ anchor_id,
+ marked_text: Some("indemnifies the lessor".to_owned()),
+ }))),
+ Arc::new(Authorizer { allowed: true }),
+ // The highlight's text arrives with the thread; no document lookup.
+ Arc::new(Marks(Err("never asked"))),
+ );
+ let context = adapter
+ .conversation_context(&actor(), &origin())
+ .await
+ .unwrap();
+ assert_eq!(
+ context.anchor,
+ Some(CommentAnchor::PdfHighlight {
+ anchor_id: anchor_id.to_string(),
+ marked_text: Some("indemnifies the lessor".to_owned()),
+ })
+ );
+}
+
+#[tokio::test]
+async fn a_pdf_pin_discussion_names_its_pin() {
+ let anchor_id = Uuid::from_u128(12);
+ let adapter = MessagePromptContextAdapter::new(
+ Arc::new(reader(Some(ThreadAnchor::PdfPlaceable { anchor_id }))),
+ Arc::new(Authorizer { allowed: true }),
+ Arc::new(Marks::none()),
+ );
+ let context = adapter
+ .conversation_context(&actor(), &origin())
+ .await
+ .unwrap();
+ assert_eq!(
+ context.anchor,
+ Some(CommentAnchor::PdfPin {
+ anchor_id: anchor_id.to_string(),
+ })
+ );
+}
diff --git a/crates/channel_bots/src/domain/service.rs b/crates/channel_bots/src/domain/service.rs
index c6a500a9ae..9363f4edc1 100644
--- a/crates/channel_bots/src/domain/service.rs
+++ b/crates/channel_bots/src/domain/service.rs
@@ -53,12 +53,35 @@ const SNAPSHOT_ANCHOR_INSTRUCTION: &str = "The document text this discussion is
is what the mark covered when the discussion was started, so the document may have changed since \
— read the document itself if you need its current wording.";
+const HIGHLIGHT_ANCHOR_INSTRUCTION: &str = "The PDF text this discussion is attached to: the \
+words the highlight covers.";
+
+const BLANK_HIGHLIGHT_INSTRUCTION: &str = "This discussion is attached to a PDF highlight that \
+carries no text, so which words it covers is not known.";
+
+const PIN_ANCHOR_INSTRUCTION: &str = "This discussion is pinned to a point on a PDF page rather \
+than to a span of text, so it covers no words.";
+
/// The thread a mention sits in, read once for everything the prompt needs.
struct ThreadContext {
lines: Vec<PromptLine>,
ids: HashSet<Uuid>,
- /// Present only for a document discussion the author anchored to text.
- anchor: Option<MarkAnchor>,
+ /// Present only for an inline document discussion.
+ anchor: Option<DiscussionAnchor>,
+}
+
+/// What an inline document discussion is attached to.
+enum DiscussionAnchor {
+ Mark(MarkAnchor),
+ /// A PDF highlight and the text it covers, read from the highlight.
+ PdfHighlight {
+ anchor_id: Uuid,
+ marked_text: Option<String>,
+ },
+ /// A point on a PDF page, which covers no text.
+ PdfPin {
+ anchor_id: Uuid,
+ },
}
/// The mark a markdown discussion is attached to — the same id the comment
@@ -128,6 +151,33 @@ fn append_anchor(prompt: &mut String, anchor: &MarkAnchor, current: Option<&Mark
}
}
+/// Write the block naming what a PDF discussion is attached to. A highlight
+/// carries its own text; a pin or a highlight without text says so, so the
+/// agent is not left to infer the words from the page.
+fn append_pdf_anchor(prompt: &mut String, anchor: &DiscussionAnchor) {
+ let _ = match anchor {
+ DiscussionAnchor::Mark(_) => return,
+ DiscussionAnchor::PdfHighlight {
+ anchor_id,
+ marked_text: Some(text),
+ } => write!(
+ prompt,
+ "\n<anchor highlight=\"{anchor_id}\">\n{HIGHLIGHT_ANCHOR_INSTRUCTION}\n\n{text}\n</anchor>\n"
+ ),
+ DiscussionAnchor::PdfHighlight {
+ anchor_id,
+ marked_text: None,
+ } => write!(
+ prompt,
+ "\n<anchor highlight=\"{anchor_id}\">\n{BLANK_HIGHLIGHT_INSTRUCTION}\n</anchor>\n"
+ ),
+ DiscussionAnchor::PdfPin { anchor_id } => write!(
+ prompt,
+ "\n<anchor pin=\"{anchor_id}\">\n{PIN_ANCHOR_INSTRUCTION}\n</anchor>\n"
+ ),
+ };
+}
+
/// Write a tagged context block: an instruction line followed by one message
/// per line, labeled by sender. Skipped entirely when there are no messages.
fn append_block(
@@ -251,16 +301,23 @@ where
root_id: Uuid,
) -> anyhow::Result<ThreadContext> {
let thread = self.messages.get_thread(access, root_id).await?;
- let anchor = match thread.state.anchor {
- Some(ThreadAnchor::Markdown {
+ let anchor = thread.state.anchor.map(|anchor| match anchor {
+ ThreadAnchor::Markdown {
mark_id,
marked_text,
- }) => Some(MarkAnchor {
+ } => DiscussionAnchor::Mark(MarkAnchor {
mark_id,
snapshot: marked_text,
}),
- _ => None,
- };
+ ThreadAnchor::PdfHighlight {
+ anchor_id,
+ marked_text,
+ } => DiscussionAnchor::PdfHighlight {
+ anchor_id,
+ marked_text,
+ },
+ ThreadAnchor::PdfPlaceable { anchor_id } => DiscussionAnchor::PdfPin { anchor_id },
+ });
let mut thread_ids = HashSet::new();
let mut lines = Vec::new();
for message in std::iter::once(thread.root).chain(thread.replies) {
@@ -366,9 +423,13 @@ where
ids: thread_ids,
anchor,
} = self.thread_lines(event, view, root_id).await?;
- if let Some(anchor) = &anchor {
- let current = self.current_mark(parent, anchor.mark_id).await;
- append_anchor(&mut prompt, anchor, current.as_ref());
+ match &anchor {
+ Some(DiscussionAnchor::Mark(mark)) => {
+ let current = self.current_mark(parent, mark.mark_id).await;
+ append_anchor(&mut prompt, mark, current.as_ref());
+ }
+ Some(pdf) => append_pdf_anchor(&mut prompt, pdf),
+ None => {}
}
append_block(&mut prompt, "thread", thread_instruction, marker, &thread);
diff --git a/crates/channel_bots/src/domain/service/tests.rs b/crates/channel_bots/src/domain/service/tests.rs
index 67422cd53d..e71a886711 100644
--- a/crates/channel_bots/src/domain/service/tests.rs
+++ b/crates/channel_bots/src/domain/service/tests.rs
@@ -242,6 +242,54 @@ async fn a_failed_live_lookup_falls_back_to_the_snapshot() {
assert!(prompt.contains("the document may have changed since"));
}
+async fn prompt_on(anchor: ThreadAnchor) -> String {
+ let trigger = message(1, None, "@macro what is this anchored to?");
+ let mut history = thread(trigger.clone(), vec![]);
+ history.state.anchor = Some(anchor);
+ let mut api = MockMessageServiceApi::new();
+ configure_reads(&mut api, &trigger, history);
+ handler(api, Arc::new(Access::default()), responder("reply"))
+ .build_prompt(&invocation(&trigger))
+ .await
+ .unwrap()
+}
+
+#[tokio::test]
+async fn a_pdf_highlight_discussion_names_the_words_it_covers() {
+ let prompt = prompt_on(ThreadAnchor::PdfHighlight {
+ anchor_id: Uuid::from_u128(0xbb),
+ marked_text: Some("indemnifies the lessor".to_owned()),
+ })
+ .await;
+ assert!(prompt.contains(&format!(
+ "<anchor highlight=\"{}\">\n{HIGHLIGHT_ANCHOR_INSTRUCTION}\n\nindemnifies the lessor\n</anchor>",
+ Uuid::from_u128(0xbb)
+ )));
+}
+
+#[tokio::test]
+async fn a_highlight_without_text_says_its_words_are_unknown() {
+ let prompt = prompt_on(ThreadAnchor::PdfHighlight {
+ anchor_id: Uuid::from_u128(0xbb),
+ marked_text: None,
+ })
+ .await;
+ assert!(prompt.contains(BLANK_HIGHLIGHT_INSTRUCTION));
+ assert!(!prompt.contains(HIGHLIGHT_ANCHOR_INSTRUCTION));
+}
+
+#[tokio::test]
+async fn a_pdf_pin_discussion_says_it_covers_no_words() {
+ let prompt = prompt_on(ThreadAnchor::PdfPlaceable {
+ anchor_id: Uuid::from_u128(0xcc),
+ })
+ .await;
+ assert!(prompt.contains(&format!(
+ "<anchor pin=\"{}\">\n{PIN_ANCHOR_INSTRUCTION}\n</anchor>",
+ Uuid::from_u128(0xcc)
+ )));
+}
+
#[tokio::test]
async fn revoked_document_access_prevents_context_reads_and_agent_work() {
let trigger = message(1, None, "@macro help");
diff --git a/crates/documents/src/domain/comments.rs b/crates/documents/src/domain/comments.rs
index d2e417b95d..e010a16286 100644
--- a/crates/documents/src/domain/comments.rs
+++ b/crates/documents/src/domain/comments.rs
@@ -67,8 +67,12 @@ pub enum CommentAnchor {
PdfHighlight {
/// The highlight annotation.
anchor_id: Uuid,
+ /// The text the highlight covers; absent when the highlight carries none.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ marked_text: Option<String>,
},
- /// A comment pinned to a point on a PDF page.
+ /// A comment pinned to a point on a PDF page. A pin marks a place, not a
+ /// span of text, so it has no marked text.
#[serde(rename_all = "camelCase")]
PdfPin {
/// The pin annotation.
@@ -336,7 +340,13 @@ fn comment_anchor(
removed: false,
},
},
- Some(ThreadAnchor::PdfHighlight { anchor_id }) => CommentAnchor::PdfHighlight { anchor_id },
+ Some(ThreadAnchor::PdfHighlight {
+ anchor_id,
+ marked_text,
+ }) => CommentAnchor::PdfHighlight {
+ anchor_id,
+ marked_text,
+ },
Some(ThreadAnchor::PdfPlaceable { anchor_id }) => CommentAnchor::PdfPin { anchor_id },
}
}
diff --git a/crates/documents/src/domain/comments/test.rs b/crates/documents/src/domain/comments/test.rs
index aada966234..5c7ec1c672 100644
--- a/crates/documents/src/domain/comments/test.rs
+++ b/crates/documents/src/domain/comments/test.rs
@@ -263,6 +263,14 @@ async fn pdf_anchors_are_inline_threads() {
messages.expect_timeline().returning(|_, _| {
Ok(page(
vec![
+ item(
+ message(3, None, "blank highlight"),
+ Some(ThreadAnchor::PdfHighlight {
+ anchor_id: id(11),
+ marked_text: None,
+ }),
+ vec![],
+ ),
item(
message(2, None, "pin"),
Some(ThreadAnchor::PdfPlaceable { anchor_id: id(20) }),
@@ -270,7 +278,10 @@ async fn pdf_anchors_are_inline_threads() {
),
item(
message(1, None, "highlight"),
- Some(ThreadAnchor::PdfHighlight { anchor_id: id(10) }),
+ Some(ThreadAnchor::PdfHighlight {
+ anchor_id: id(10),
+ marked_text: Some("highlighted words".to_owned()),
+ }),
vec![],
),
],
@@ -291,12 +302,22 @@ async fn pdf_anchors_are_inline_threads() {
vec![
(
CommentThreadKind::Inline,
- CommentAnchor::PdfHighlight { anchor_id: id(10) }
+ CommentAnchor::PdfHighlight {
+ anchor_id: id(10),
+ marked_text: Some("highlighted words".to_owned()),
+ }
),
(
CommentThreadKind::Inline,
CommentAnchor::PdfPin { anchor_id: id(20) }
),
+ (
+ CommentThreadKind::Inline,
+ CommentAnchor::PdfHighlight {
+ anchor_id: id(11),
+ marked_text: None,
+ }
+ ),
]
);
}
@@ -473,6 +494,30 @@ fn serializes_ids_kind_and_anchor_for_agents() {
);
}
+#[test]
+fn pdf_highlights_serialize_like_text_anchors() {
+ let anchor = |marked_text: Option<&str>| {
+ serde_json::to_value(CommentAnchor::PdfHighlight {
+ anchor_id: id(10),
+ marked_text: marked_text.map(str::to_owned),
+ })
+ .unwrap()
+ };
+
+ assert_eq!(
+ anchor(Some("highlighted words")),
+ serde_json::json!({
+ "type": "pdfHighlight",
+ "anchorId": id(10),
+ "markedText": "highlighted words"
+ })
+ );
+ assert_eq!(
+ anchor(None),
+ serde_json::json!({ "type": "pdfHighlight", "anchorId": id(10) })
+ );
+}
+
#[tokio::test]
async fn deleted_threads_do_not_count_toward_the_cap() {
let mut messages = MockMessageReader::new();
diff --git a/crates/lexical_client/src/parse_markdown.rs b/crates/lexical_client/src/parse_markdown.rs
index 43cb755a64..935fceedb2 100644
--- a/crates/lexical_client/src/parse_markdown.rs
+++ b/crates/lexical_client/src/parse_markdown.rs
@@ -221,19 +221,39 @@ pub struct AgentContextMessage<'a> {
/// The document location of the comment thread an agent prompt was posted in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
-#[serde(rename_all = "camelCase")]
-pub struct AgentContextAnchor<'a> {
- /// Lexical mark the comment is attached to.
- pub mark_id: &'a str,
- /// The marked text when the comment was posted, when it was captured.
- #[serde(skip_serializing_if = "Option::is_none")]
- pub marked_text: Option<&'a str>,
- /// The text the mark covers in the document now, when it was resolved.
- #[serde(skip_serializing_if = "Option::is_none")]
- pub current_marked_text: Option<&'a str>,
- /// The passage around the mark now, when it was resolved.
- #[serde(skip_serializing_if = "Option::is_none")]
- pub surrounding_text: Option<&'a str>,
+#[serde(
+ tag = "type",
+ rename_all = "camelCase",
+ rename_all_fields = "camelCase"
+)]
+pub enum AgentContextAnchor<'a> {
+ /// A comment mark in a markdown document.
+ Markdown {
+ /// Lexical mark the comment is attached to.
+ mark_id: &'a str,
+ /// The marked text when the comment was posted, when it was captured.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ marked_text: Option<&'a str>,
+ /// The text the mark covers in the document now, when it was resolved.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ current_marked_text: Option<&'a str>,
+ /// The passage around the mark now, when it was resolved.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ surrounding_text: Option<&'a str>,
+ },
+ /// A highlight on a PDF.
+ PdfHighlight {
+ /// Highlight annotation the comment is attached to.
+ anchor_id: &'a str,
+ /// The text the highlight covers, when it carries any.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ marked_text: Option<&'a str>,
+ },
+ /// A point pinned on a PDF page, which covers no text.
+ PdfPin {
+ /// Pin annotation the comment is attached to.
+ anchor_id: &'a str,
+ },
}
#[derive(Debug, serde::Serialize)]
diff --git a/crates/messages/src/domain/models.rs b/crates/messages/src/domain/models.rs
index bf01193792..140416132e 100644
--- a/crates/messages/src/domain/models.rs
+++ b/crates/messages/src/domain/models.rs
@@ -109,8 +109,15 @@ pub enum ThreadAnchor {
PdfHighlight {
/// Highlight annotation UUID.
anchor_id: Uuid,
+ /// The text the highlight covers, trimmed and bounded like a markdown
+ /// snapshot. The highlight owns it and it can be edited there, so it is
+ /// read from the highlight whenever the thread is, never stored on the
+ /// thread. Absent when the highlight carries no text.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ marked_text: Option<String>,
},
- /// A comment-only placeable PDF annotation.
+ /// A comment-only placeable PDF annotation. It marks a point on a page,
+ /// not a span of text, so it has no marked text.
PdfPlaceable {
/// Placeable annotation UUID.
anchor_id: Uuid,
@@ -189,6 +196,7 @@ impl NewThreadAnchor {
},
Self::PdfHighlight { anchor_id } => ThreadAnchor::PdfHighlight {
anchor_id: *anchor_id,
+ marked_text: None,
},
Self::PdfPlaceable { anchor_id, .. } => ThreadAnchor::PdfPlaceable {
anchor_id: *anchor_id,
diff --git a/crates/messages/src/domain/service/test.rs b/crates/messages/src/domain/service/test.rs
index 2cc4579374..f2cc6a29fe 100644
--- a/crates/messages/src/domain/service/test.rs
+++ b/crates/messages/src/domain/service/test.rs
@@ -325,6 +325,7 @@ async fn thread_patch_cannot_detach_pdf_annotations() {
let mut repo = fixture();
repo.state.anchor = Some(ThreadAnchor::PdfHighlight {
anchor_id: Uuid::from_u128(3),
+ marked_text: None,
});
let events = Events::default();
let service = MessageService::new(repo.clone(), events.clone());
diff --git a/crates/messages/src/outbound/pg_message_repo.rs b/crates/messages/src/outbound/pg_message_repo.rs
index fc23b6ff4f..b7c8979b77 100644
--- a/crates/messages/src/outbound/pg_message_repo.rs
+++ b/crates/messages/src/outbound/pg_message_repo.rs
@@ -73,6 +73,44 @@ impl PgMessageRepository {
Self { pool }
}
+ /// Fill in what each PDF highlight anchor covers. The highlight owns its
+ /// text and can be edited or re-derived, so the thread only names it and
+ /// every read resolves it here; channel threads never carry one and cost
+ /// nothing.
+ async fn resolve_highlight_text<'s>(
+ executor: impl sqlx::PgExecutor<'_>,
+ states: impl IntoIterator<Item = &'s mut ThreadState>,
+ ) -> Result<(), MessageError> {
+ let mut anchors: Vec<(Uuid, &mut Option<String>)> = states
+ .into_iter()
+ .filter_map(|state| match &mut state.anchor {
+ Some(ThreadAnchor::PdfHighlight {
+ anchor_id,
+ marked_text,
+ }) => Some((*anchor_id, marked_text)),
+ _ => None,
+ })
+ .collect();
+ if anchors.is_empty() {
+ return Ok(());
+ }
+ let ids: Vec<Uuid> = anchors.iter().map(|(id, _)| *id).collect();
+ let texts: HashMap<Uuid, String> = sqlx::query!(
+ r#"SELECT uuid, text FROM "PdfHighlightAnchor" WHERE uuid = ANY($1)"#,
+ &ids,
+ )
+ .fetch_all(executor)
+ .await
+ .map_err(database_error)?
+ .into_iter()
+ .map(|row| (row.uuid, row.text))
+ .collect();
+ for (id, marked_text) in &mut anchors {
+ **marked_text = texts.get(id).and_then(|text| marked_text_snapshot(text));
+ }
+ Ok(())
+ }
+
async fn hydrate(&self, rows: Vec<Json<StoredMessage>>) -> Result<Vec<Message>, MessageError> {
let mut connection = self.pool.acquire().await.map_err(database_error)?;
Self::hydrate_in(&mut connection, rows).await
@@ -445,13 +483,14 @@ impl PgMessageRepository {
.map_err(database_error)?;
}
}
- let result = sqlx::query_scalar!(
+ let mut result = sqlx::query_scalar!(
r#"UPDATE comms_message_threads t SET resolved = COALESCE($2, resolved), updated_at = now(),
deleted_at = CASE WHEN $3 THEN now() ELSE deleted_at END,
anchor = CASE WHEN $4 OR ($3 AND anchor->>'type' <> 'markdown') THEN NULL ELSE anchor END
WHERE root_id = $1 RETURNING to_jsonb(t) AS "state!: Json<ThreadState>""#,
root_id, patch.resolved, delete, patch.detach_anchor,
).fetch_one(&mut **tx).await.map_err(database_error)?;
+ Self::resolve_highlight_text(&mut **tx, [&mut result.0]).await?;
Ok(result.0)
}
}
@@ -505,7 +544,7 @@ impl MessageRepository for PgMessageRepository {
parent: &MessageParent,
root: Uuid,
) -> Result<Option<ThreadState>, MessageError> {
- Ok(sqlx::query_scalar!(
+ let mut state = sqlx::query_scalar!(
r#"SELECT to_jsonb(t) AS "state!: Json<ThreadState>" FROM comms_message_threads t
WHERE t.root_id = $1 AND t.parent_entity_type = $2 AND t.parent_entity_id = $3"#,
root,
@@ -515,7 +554,9 @@ impl MessageRepository for PgMessageRepository {
.fetch_optional(&self.pool)
.await
.map_err(database_error)?
- .map(|state| state.0))
+ .map(|state| state.0);
+ Self::resolve_highlight_text(&self.pool, state.as_mut()).await?;
+ Ok(state)
}
async fn preceding(
diff --git a/crates/messages/src/outbound/pg_message_repo/test.rs b/crates/messages/src/outbound/pg_message_repo/test.rs
index 9bbe405eea..dc5f71618a 100644
--- a/crates/messages/src/outbound/pg_message_repo/test.rs
+++ b/crates/messages/src/outbound/pg_message_repo/test.rs
@@ -1149,3 +1149,103 @@ async fn client_supplied_ids_are_kept_and_never_reused(pool: PgPool) {
"first"
);
}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn highlight_threads_read_the_text_the_highlight_covers(pool: PgPool) {
+ setup(&pool).await;
+ let repo = PgMessageRepository::new(pool.clone());
+ let parent = MessageParent::parse("document", "message-doc-a").unwrap();
+ let highlight = |text: &'static str| {
+ let pool = pool.clone();
+ async move {
+ let anchor_id = macro_uuid::generate_uuid_v7();
+ sqlx::query!(r#"INSERT INTO "PdfHighlightAnchor"
+ (uuid, "documentId", owner, page, red, green, blue, alpha, type, text, "pageViewportWidth", "pageViewportHeight")
+ VALUES ($1, 'message-doc-a', $2, 1, 255, 255, 0, 0.5, 1, $3, 600, 800)"#, anchor_id, USER, text)
+ .execute(&pool).await.unwrap();
+ anchor_id
+ }
+ };
+ let thread_on = |anchor_id: Uuid, content: &str| {
+ let mut create = command("message-doc-a", None, content);
+ create.input.anchor = Some(NewThreadAnchor::PdfHighlight { anchor_id });
+ create
+ };
+ let covered = highlight(" the highlighted words \n").await;
+ let blank = highlight(" ").await;
+ let root = repo.create(thread_on(covered, "on words")).await.unwrap();
+ let blank_root = repo.create(thread_on(blank, "on nothing")).await.unwrap();
+ let anchor = |anchor_id, text: Option<&str>| {
+ Some(ThreadAnchor::PdfHighlight {
+ anchor_id,
+ marked_text: text.map(str::to_owned),
+ })
+ };
+
+ let stored = sqlx::query_scalar!(
+ r#"SELECT anchor AS "anchor!" FROM comms_message_threads WHERE root_id = $1"#,
+ root.id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ stored,
+ serde_json::json!({ "type": "pdf_highlight", "anchor_id": covered })
+ );
+ assert_eq!(
+ repo.thread(&parent, root.id).await.unwrap().unwrap().anchor,
+ anchor(covered, Some("the highlighted words"))
+ );
+ assert_eq!(
+ repo.thread(&parent, blank_root.id)
+ .await
+ .unwrap()
+ .unwrap()
+ .anchor,
+ anchor(blank, None)
+ );
+
+ sqlx::query!(
+ r#"UPDATE "PdfHighlightAnchor" SET text = 'edited words' WHERE uuid = $1"#,
+ covered
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let timeline = repo
+ .timeline(
+ &parent,
+ MessageTimelineQuery {
+ limit: Some(10),
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+ let mut anchors: Vec<_> = timeline
+ .items
+ .into_iter()
+ .map(|item| (item.message.id, item.state.anchor))
+ .collect();
+ anchors.sort_by_key(|(id, _)| *id);
+ assert_eq!(
+ anchors,
+ vec![
+ (root.id, anchor(covered, Some("edited words"))),
+ (blank_root.id, anchor(blank, None)),
+ ]
+ );
+ let patched = repo
+ .patch_thread(
+ &parent,
+ root.id,
+ ThreadPatch {
+ resolved: Some(true),
+ ..Default::default()
+ },
+ )
+ .await
+ .unwrap();
+ assert_eq!(patched.anchor, anchor(covered, Some("edited words")));
+}
diff --git a/crates/messages/src/outbound/pg_message_repo/timeline.rs b/crates/messages/src/outbound/pg_message_repo/timeline.rs
index 305d48d956..20754d1257 100644
--- a/crates/messages/src/outbound/pg_message_repo/timeline.rs
+++ b/crates/messages/src/outbound/pg_message_repo/timeline.rs
@@ -133,6 +133,11 @@ impl PgMessageRepository {
}
}
let mut states: HashMap<_, _> = states.into_iter().map(|s| (s.root_id, s)).collect();
+ Self::resolve_highlight_text(
+ &self.pool,
+ states.values_mut().map(|state| &mut state.state.0),
+ )
+ .await?;
let items = roots
.into_iter()
.filter_map(|id| {
diff --git a/packages/lexical-core/tests/agent-context.test.ts b/packages/lexical-core/tests/agent-context.test.ts
index 4a3c4da84f..89729aa237 100644
--- a/packages/lexical-core/tests/agent-context.test.ts
+++ b/packages/lexical-core/tests/agent-context.test.ts
@@ -242,6 +242,52 @@ describe('composeAgentContextPrompt', () => {
});
});
+ it('names the words a PDF highlight covers', () => {
+ const composed = composeAgentContextPrompt({
+ promptMarkdown: 'what does this mean?',
+ anchor: {
+ type: 'pdfHighlight',
+ anchorId: 'highlight-1',
+ markedText: 'indemnifies the lessor',
+ },
+ });
+ const state = markdownToSerializedEditorStateWithIds(composed);
+
+ expect(state.root.children[0]).toMatchObject({
+ text:
+ 'Comment anchor: {"type":"pdfHighlight","anchorId":"highlight-1","markedText":"indemnifies the lessor"}\n' +
+ 'markedText is the text the PDF highlight covers.',
+ });
+ });
+
+ it('says a PDF highlight without text covers unknown words', () => {
+ const composed = composeAgentContextPrompt({
+ promptMarkdown: 'what does this mean?',
+ anchor: { type: 'pdfHighlight', anchorId: 'highlight-1' },
+ });
+ const state = markdownToSerializedEditorStateWithIds(composed);
+
+ expect(state.root.children[0]).toMatchObject({
+ text:
+ 'Comment anchor: {"type":"pdfHighlight","anchorId":"highlight-1"}\n' +
+ 'The PDF highlight carries no text, so which words it covers is not known.',
+ });
+ });
+
+ it('says a PDF pin covers no words', () => {
+ const composed = composeAgentContextPrompt({
+ promptMarkdown: 'what does this mean?',
+ anchor: { type: 'pdfPin', anchorId: 'pin-1' },
+ });
+ const state = markdownToSerializedEditorStateWithIds(composed);
+
+ expect(state.root.children[0]).toMatchObject({
+ text:
+ 'Comment anchor: {"type":"pdfPin","anchorId":"pin-1"}\n' +
+ 'The comment is pinned to a point on a PDF page rather than to text, so it covers no words.',
+ });
+ });
+
it('cannot close the context envelope from marked text', () => {
const composed = composeAgentContextPrompt({
promptMarkdown: 'original',
diff --git a/packages/lexical-core/utils/agent-context.ts b/packages/lexical-core/utils/agent-context.ts
index 6d8d6c2eb0..49f5fccd72 100644
--- a/packages/lexical-core/utils/agent-context.ts
+++ b/packages/lexical-core/utils/agent-context.ts
@@ -20,8 +20,9 @@ export type AgentContextParent = {
id: string;
};
-/** Where in a document the comment thread a prompt was posted in sits. */
-export type AgentContextAnchor = {
+/** A comment mark in a markdown document. Older callers omit `type`. */
+export type MarkdownCommentAnchor = {
+ type?: 'markdown';
markId: string;
/** What the mark covered when the comment was posted; absent on threads anchored before it was captured. */
markedText?: string;
@@ -31,6 +32,26 @@ export type AgentContextAnchor = {
surroundingText?: string;
};
+/** A highlight on a PDF. */
+export type PdfHighlightCommentAnchor = {
+ type: 'pdfHighlight';
+ anchorId: string;
+ /** The text the highlight covers; absent when the highlight carries none. */
+ markedText?: string;
+};
+
+/** A point pinned on a PDF page, which covers no text. */
+export type PdfPinCommentAnchor = {
+ type: 'pdfPin';
+ anchorId: string;
+};
+
+/** Where in a document the comment thread a prompt was posted in sits. */
+export type AgentContextAnchor =
+ | MarkdownCommentAnchor
+ | PdfHighlightCommentAnchor
+ | PdfPinCommentAnchor;
+
/** Input used to compose an agent prompt with private conversation context. */
export type AgentContextPrompt = {
promptMarkdown: string;
@@ -45,10 +66,19 @@ export type AgentContextPrompt = {
* nothing the agent can read maps that id back onto text, so the text travels
* with it: as the document reads now when it could be resolved, and as it read
* when the comment was posted, which is all there is when the live lookup
- * failed or the text has since been removed.
+ * failed or the text has since been removed. A PDF highlight carries its own
+ * text; a pin covers none, and says so rather than leaving the agent to guess.
*/
function describeAnchor(anchor: AgentContextAnchor): string {
const location = `Comment anchor: ${JSON.stringify(anchor)}`;
+ if (anchor.type === 'pdfPin') {
+ return `${location}\nThe comment is pinned to a point on a PDF page rather than to text, so it covers no words.`;
+ }
+ if (anchor.type === 'pdfHighlight') {
+ return anchor.markedText === undefined
+ ? `${location}\nThe PDF highlight carries no text, so which words it covers is not known.`
+ : `${location}\nmarkedText is the text the PDF highlight covers.`;
+ }
if (anchor.currentMarkedText !== undefined) {
const snapshot =
anchor.markedText === undefined
diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.ts
index 26e6ea9176..f2447ad591 100644
--- a/packages/sdk/generated/storage/types.gen.ts
+++ b/packages/sdk/generated/storage/types.gen.ts
@@ -9996,6 +9996,13 @@ export type ThreadAnchor = {
* Highlight annotation UUID.
*/
anchor_id: string;
+ /**
+ * The text the highlight covers, trimmed and bounded like a markdown
+ * snapshot. The highlight owns it and it can be edited there, so it is
+ * read from the highlight whenever the thread is, never stored on the
+ * thread. Absent when the highlight carries no text.
+ */
+ marked_text?: string | null;
type: 'pdf_highlight';
} | {
/**
diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.json
index 01fb32759d..3f5978a42c 100644
--- a/packages/sdk/specs/storage.json
+++ b/packages/sdk/specs/storage.json
@@ -30337,6 +30337,10 @@
"format": "uuid",
"description": "Highlight annotation UUID."
},
+ "marked_text": {
+ "type": ["string", "null"],
+ "description": "The text the highlight covers, trimmed and bounded like a markdown\nsnapshot. The highlight owns it and it can be edited there, so it is\nread from the highlight whenever the thread is, never stored on the\nthread. Absent when the highlight carries no text."
+ },
"type": {
"type": "string",
"enum": ["pdf_highlight"]
@@ -30345,7 +30349,7 @@
},
{
"type": "object",
- "description": "A comment-only placeable PDF annotation.",
+ "description": "A comment-only placeable PDF annotation. It marks a point on a page,\nnot a span of text, so it has no marked text.",
"required": ["anchor_id", "type"],
"properties": {
"anchor_id": {
diff --git a/services/lexical-service/src/endpoints/agent-context.ts b/services/lexical-service/src/endpoints/agent-context.ts
index 25a4ccfef8..9da51d812c 100644
--- a/services/lexical-service/src/endpoints/agent-context.ts
+++ b/services/lexical-service/src/endpoints/agent-context.ts
@@ -10,12 +10,24 @@ const messageParent = z.object({
id: z.string().min(1),
});
-const commentAnchor = z.object({
- markId: z.string().min(1),
- markedText: z.string().optional(),
- currentMarkedText: z.string().optional(),
- surroundingText: z.string().optional(),
-});
+const commentAnchor = z.union([
+ z.object({
+ type: z.literal('markdown').optional(),
+ markId: z.string().min(1),
+ markedText: z.string().optional(),
+ currentMarkedText: z.string().optional(),
+ surroundingText: z.string().optional(),
+ }),
+ z.object({
+ type: z.literal('pdfHighlight'),
+ anchorId: z.string().min(1),
+ markedText: z.string().optional(),
+ }),
+ z.object({
+ type: z.literal('pdfPin'),
+ anchorId: z.string().min(1),
+ }),
+]);
const agentContextRequest = z.object({
promptMarkdown: z.string(),