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): let ReadContent return unknown file types as text or a download url (#6804)
gbirmanc59bf2739685
diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.tsindex f21ca9cc88..2b19f2ae30 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@@ -4814,6 +4814,7 @@ export const ReadContentResponse = z.object({),}).strict(),+ z.object({ download: z.object({ url: z.string() }) }).strict(),];const errors = schemas.reduce<z.ZodError[]>((errors, schema) =>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 3d84b6fa58..2b4206a1f8 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@@ -913,6 +913,14 @@ export type Content =}| {markdown: MarkdownNode[];+ }+ | {+ download: {+ /**+ * Short-lived URL the raw file can be downloaded from.+ */+ url: string;+ };};/*** A single node of a markdown document as seen by the AI.diff --git a/crates/documents/src/inbound/toolset/read_content.rs b/crates/documents/src/inbound/toolset/read_content.rsindex 786cceaf18..a4af6a6fb2 100644--- a/crates/documents/src/inbound/toolset/read_content.rs+++ b/crates/documents/src/inbound/toolset/read_content.rs@@ -71,8 +71,18 @@ pub enum Content {Text(String),/// All nodes of the markdown fileMarkdown(Vec<MarkdownNode>),+ /// The file is binary or too large to return inline. Fetch the URL to download it.+ #[serde(rename_all = "camelCase")]+ Download {+ /// Short-lived URL the raw file can be downloaded from.+ url: String,+ },}+/// Files of an unsupported or unknown type are returned inline only when they+/// are UTF-8 text no larger than this; anything else is returned as a download URL.+const MAX_INLINE_TEXT_BYTES: usize = 512 * 1024;+#[derive(Debug, Serialize, JsonSchema)]#[serde(rename_all = "camelCase")]pub struct ReadContentResponse {@@ -147,20 +157,12 @@ whereinternal_error: e.into(),})?;- // Based on the file type we need to handle how we get the document differently- let file_type: FileType = if let Some(file_type) = document_context.file_type.as_ref() {- FileType::from_str(file_type).map_err(|e| ToolCallError {- description: format!("unsupported file type {file_type}"),- internal_error: e.into(),- })?- } else {- return Err(ToolCallError {- description: "cannot get read content for unknown file type".to_string(),- internal_error: anyhow::anyhow!("cannot get read content for unknown file type"),- });- };+ let file_type = document_context+ .file_type+ .as_deref()+ .and_then(|file_type| FileType::from_str(file_type).ok());- let content: Content = if file_type == FileType::Spreadsheet {+ let content: Content = if file_type == Some(FileType::Spreadsheet) {let result = service_context.spreadsheet.read(@@ -183,8 +185,8 @@ whereinternal_error: e.into(),})?)} else {- match file_type.macro_app_path() {- FileAssociation::Pdf(_) | FileAssociation::Write(_) => Content::Text(+ match file_type.map(|file_type| file_type.macro_app_path()) {+ Some(FileAssociation::Pdf(_)) | Some(FileAssociation::Write(_)) => Content::Text(service_context.service.get_document_text(entity_access_receipt.clone())@@ -194,7 +196,7 @@ whereinternal_error: e.into(),})?,),- FileAssociation::Md(_) => Content::Markdown(+ Some(FileAssociation::Md(_)) => Content::Markdown(service_context.lexical_client.parse_cognition_v2(&self.document_id.to_string())@@ -208,24 +210,31 @@ where.map(|i| i.into()).collect(),),- FileAssociation::Code(_) | FileAssociation::Document(_) => Content::Text(- get_document_content_from_location(- service_context.clone(),- &document_context,- entity_access_receipt.clone(),+ Some(FileAssociation::Code(_)) | Some(FileAssociation::Document(_)) => {+ Content::Text(+ get_document_content_from_location(+ service_context.clone(),+ &document_context,+ entity_access_receipt.clone(),+ )+ .await+ .map_err(|e| ToolCallError {+ description: "unable to get document content using location"+ .to_string(),+ internal_error: e,+ })?,)- .await- .map_err(|e| ToolCallError {- description: "unable to get document content using location".to_string(),- internal_error: e,- })?,- ),- _ => {- return Err(ToolCallError {- description: format!("unsupported file type {file_type}"),- internal_error: anyhow::anyhow!("unsupported file type"),- });}+ _ => get_document_inline_text_or_download(+ service_context.clone(),+ &document_context,+ entity_access_receipt.clone(),+ )+ .await+ .map_err(|e| ToolCallError {+ description: "unable to download document".to_string(),+ internal_error: e,+ })?,}};@@ -242,9 +251,8 @@ where}}-/// Gets the document content from location-#[tracing::instrument(skip(service_context), err)]-async fn get_document_content_from_location<+/// Gets the presigned download url for a document stored as a single file+async fn get_document_presigned_url<DSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,EDSvc: EditingWorkerService,@@ -265,15 +273,31 @@ async fn get_document_content_from_location<).await?;- let presigned_url = match location {+ match location {LocationResponseV3::PresignedUrl {presigned_url,metadata: _metadata,content: _content,- } => presigned_url,- // This should only be called with text documents which result in 1 presigned url+ } => Ok(presigned_url),+ // This should only be called with single-file documents which result in 1 presigned url_ => unreachable!(),- };+ }+}++/// Gets the document content from location+#[tracing::instrument(skip(service_context), err)]+async fn get_document_content_from_location<+ DSvc: DocumentService + DocumentCreationService,+ ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,+>(+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,+ document_context: &DocumentBasic,+ entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,+) -> anyhow::Result<String> {+ let presigned_url =+ get_document_presigned_url(service_context, document_context, entity_access_receipt)+ .await?;// Download the file and convert to UTF8let response = reqwest::get(&presigned_url).await?;@@ -288,3 +312,72 @@ async fn get_document_content_from_location<Ok(content)}++/// Reads a document of an unknown or unsupported file type: small UTF-8 files are+/// returned as text, and everything else as a url the caller can download it from.+#[tracing::instrument(skip(service_context), err)]+async fn get_document_inline_text_or_download<+ DSvc: DocumentService + DocumentCreationService,+ ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,+>(+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,+ document_context: &DocumentBasic,+ entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,+) -> anyhow::Result<Content> {+ let presigned_url =+ get_document_presigned_url(service_context, document_context, entity_access_receipt)+ .await?;++ let mut response = reqwest::get(&presigned_url).await?;++ if !response.status().is_success() {+ anyhow::bail!("Failed to download document: HTTP {}", response.status());+ }++ let mut bytes = Vec::new();+ while let Some(chunk) = response.chunk().await? {+ bytes.extend_from_slice(&chunk);+ if bytes.len() > MAX_INLINE_TEXT_BYTES {+ return Ok(Content::Download { url: presigned_url });+ }+ }++ Ok(inline_text_or_download(bytes, presigned_url))+}++fn inline_text_or_download(bytes: Vec<u8>, url: String) -> Content {+ if bytes.len() > MAX_INLINE_TEXT_BYTES {+ return Content::Download { url };+ }+ match String::from_utf8(bytes) {+ Ok(text) => Content::Text(text),+ Err(_) => Content::Download { url },+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ const URL: &str = "https://example.com/file";++ #[test]+ fn small_utf8_file_is_returned_as_text() {+ let content = inline_text_or_download(b"{\"traceEvents\":[]}".to_vec(), URL.to_string());+ assert!(matches!(content, Content::Text(text) if text == "{\"traceEvents\":[]}"));+ }++ #[test]+ fn binary_file_is_returned_as_download_url() {+ let content = inline_text_or_download(vec![0x1f, 0x8b, 0x08, 0xff], URL.to_string());+ assert!(matches!(content, Content::Download { url } if url == URL));+ }++ #[test]+ fn oversized_text_file_is_returned_as_download_url() {+ let content =+ inline_text_or_download(vec![b'a'; MAX_INLINE_TEXT_BYTES + 1], URL.to_string());+ assert!(matches!(content, Content::Download { url } if url == URL));+ }+}