Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(channels): add CreateChannelMediaUpload tool for direct media uploads (#7975)
Jacob Beckermanf4f24b7d20f0
diff --git a/apps/docs/AI/mcp/tools/send-channel-message.mdx b/apps/docs/AI/mcp/tools/send-channel-message.mdxindex 4234258f4d..fdea41473d 100644--- a/apps/docs/AI/mcp/tools/send-channel-message.mdx+++ b/apps/docs/AI/mcp/tools/send-channel-message.mdx@@ -6,12 +6,13 @@ description: >-# SendChannelMessage-Send or reply to a channel on behalf of the user. Only use this when explicitly asked to send a message+Send or reply to a channel on behalf of the user. Only use this when explicitly asked to send a message. To send an image or video, first upload it with CreateChannelMediaUpload (where available) and pass the returned attachment here; to attach a Macro document, pass its document ID.## Parameters| Parameter | Type | Required | Description || --- | --- | --- | --- |-| `content` | string | Yes | Message content in macro markdown format. This uses the same syntax as markdown documents |+| `content` | string | Yes | Message content in macro markdown format. This uses the same syntax as markdown documents. May be empty when attachments are sent. || `channel_id` | string | Yes | The channel id to send the message to || `thread_id` | string | No | An optional thread id to reply too |+| `attachments` | ChannelMessageAttachmentInput[] | No | Optional images, videos, or documents to attach, at most 10. Upload media before sending; a media attachment whose upload has not finished shows as broken. |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 518f82c2ed..0ef951c162 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@@ -14035,6 +14035,37 @@ export const SendChannelMessage = z.object({content: z.string(),channel_id: z.string().uuid(),thread_id: z.union([z.string().uuid(), z.null()]).optional(),+ attachments: z+ .array(+ z.object({+ id: z.string(),+ kind: z.any().superRefine((x, ctx) => {+ const schemas = [+ z.literal('image'),+ z.literal('video'),+ z.literal('document'),+ ];+ 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',+ });+ }+ }),+ width: z.union([z.number().int(), z.null()]).optional(),+ height: z.union([z.number().int(), z.null()]).optional(),+ })+ )+ .optional(),});export const SendChannelMessageResponse = z.object({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 9e29576dee..17ca4296b2 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@@ -4363,6 +4363,10 @@ export type LaneKey =* How search terms are matched against skill names.*/export type SearchSkillsMatchType = 'partial' | 'exact';+/**+ * How an attachment on a sent message renders.+ */+export type ChannelAttachmentKind = 'image' | 'video' | 'document';/*** Response from the SendEmail tool.*/@@ -13359,11 +13363,11 @@ export interface SelfKnowledgeResponse {about: string;}/**- * Send or reply to a channel on behalf of the user. Only use this when explicitly asked to send a message+ * Send or reply to a channel on behalf of the user. Only use this when explicitly asked to send a message. To send an image or video, first upload it with CreateChannelMediaUpload (where available) and pass the returned attachment here; to attach a Macro document, pass its document ID.*/export interface SendChannelMessage {/**- * Message content in macro markdown format. This uses the same syntax as markdown documents+ * Message content in macro markdown format. This uses the same syntax as markdown documents. May be empty when attachments are sent.*/content: string;/**@@ -13374,6 +13378,28 @@ export interface SendChannelMessage {* An optional thread id to reply too*/thread_id?: string | null;+ /**+ * Optional images, videos, or documents to attach, at most 10. Upload media before sending; a media attachment whose upload has not finished shows as broken.+ */+ attachments?: ChannelMessageAttachmentInput[];+}+/**+ * An attachment to send with a channel message.+ */+export interface ChannelMessageAttachmentInput {+ /**+ * The attachmentId returned by CreateChannelMediaUpload (after its upload succeeded), or the ID of a Macro document.+ */+ id: string;+ kind: ChannelAttachmentKind;+ /**+ * Optional pixel width of an image or video, used to lay it out before it loads.+ */+ width?: number | null;+ /**+ * Optional pixel height of an image or video.+ */+ height?: number | null;}export interface SendChannelMessageResponse {channel_id: string;diff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rsindex 05d8654631..ef791755f2 100644--- a/crates/ai_tools/src/build_context.rs+++ b/crates/ai_tools/src/build_context.rs@@ -636,6 +636,7 @@ pub async fn build_tool_service_context_from_env(import_tool_context,chat_tool_context,channel_tool_context,+ channel_media_upload_tool_context: crate::build_channel_media_upload_tool_context()?,bot_tool_context: crate::tool_context::build_bot_tool_context(pool.clone(),crate::tool_context::MaybeToolEventBroker::Real(macro_event_broker.clone()),diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rsindex 1e39e1f27b..ce70477e49 100644--- a/crates/ai_tools/src/lib.rs+++ b/crates/ai_tools/src/lib.rs@@ -73,24 +73,25 @@ pub use tool_context::{ToolBookingLinkToolContext, ToolBookingManagementToolContext, ToolBotService,ToolBotToolContext, ToolCalendarMutationService, ToolCalendarReadService,ToolCalendarToolContext, ToolCallRecordQueryService, ToolCallService, ToolCallToolContext,- ToolChannelEventDispatcher, ToolChannelMessagesService, ToolChannelToolContext,- ToolChatService, ToolChatToolContext, ToolCodingAgentToolContext, ToolCommsService,- ToolCrmService, ToolCrmToolContext, ToolDatabasesService, ToolDatabasesSqlToolContext,- ToolDatabasesToolContext, ToolDocumentService, ToolDocumentToolContext, ToolEmailService,- ToolEmailToolContext, ToolEntityAccessManagementService, ToolEntityAccessService,- ToolEntityCreator, ToolFavoritesService, ToolFavoritesToolContext, ToolForeignEntityService,- ToolFormsToolContext, ToolFrecencyService, ToolGithubPullRequestService,- ToolImageGenerationToolContext, ToolImportService, ToolImportToolContext,- ToolInitiativeToolContext, ToolItemNotificationPreferenceToolContext, ToolMcpSelector,- ToolNotificationQueue, ToolNotificationService, ToolNotificationToolContext,- ToolPipedreamConnection, ToolPipelineService, ToolPipelineToolContext, ToolPipelineWorkflows,- ToolProjectService, ToolProjectToolContext, ToolPropertiesService, ToolPropertiesToolContext,- ToolServiceContext, ToolSkillService, ToolSkillToolContext, ToolSoupService,- ToolSystemPropertiesService, ToolTableEventPublisher, ToolTeamCalendarService,- ToolTeamCalendarToolContext, ToolTeamService, ToolTeamToolContext, ToolUserEmailService,- ToolViewOnlyDatabasesSqlToolContext, build_activity_tool_context,- build_booking_link_tool_context, build_booking_management_tool_context, build_bot_tool_context,- build_calendar_tool_context, build_channel_tool_context_with_dispatcher,+ ToolChannelEventDispatcher, ToolChannelMediaUploadToolContext, ToolChannelMessagesService,+ ToolChannelToolContext, ToolChatService, ToolChatToolContext, ToolCodingAgentToolContext,+ ToolCommsService, ToolCrmService, ToolCrmToolContext, ToolDatabasesService,+ ToolDatabasesSqlToolContext, ToolDatabasesToolContext, ToolDocumentService,+ ToolDocumentToolContext, ToolEmailService, ToolEmailToolContext,+ ToolEntityAccessManagementService, ToolEntityAccessService, ToolEntityCreator,+ ToolFavoritesService, ToolFavoritesToolContext, ToolForeignEntityService, ToolFormsToolContext,+ ToolFrecencyService, ToolGithubPullRequestService, ToolImageGenerationToolContext,+ ToolImportService, ToolImportToolContext, ToolInitiativeToolContext,+ ToolItemNotificationPreferenceToolContext, ToolMcpSelector, ToolNotificationQueue,+ ToolNotificationService, ToolNotificationToolContext, ToolPipedreamConnection,+ ToolPipelineService, ToolPipelineToolContext, ToolPipelineWorkflows, ToolProjectService,+ ToolProjectToolContext, ToolPropertiesService, ToolPropertiesToolContext, ToolServiceContext,+ ToolSkillService, ToolSkillToolContext, ToolSoupService, ToolSystemPropertiesService,+ ToolTableEventPublisher, ToolTeamCalendarService, ToolTeamCalendarToolContext, ToolTeamService,+ ToolTeamToolContext, ToolUserEmailService, ToolViewOnlyDatabasesSqlToolContext,+ build_activity_tool_context, build_booking_link_tool_context,+ build_booking_management_tool_context, build_bot_tool_context, build_calendar_tool_context,+ build_channel_media_upload_tool_context, build_channel_tool_context_with_dispatcher,build_channel_tool_context_with_side_effects, build_channel_tool_context_without_side_effects,build_coding_agent_tool_context, build_crm_tool_context, build_databases_sql_tool_context,build_databases_tool_context, build_favorites_service, build_favorites_tool_context,@@ -104,7 +105,10 @@ pub use tool_context::{};pub use tool_context::{ToolConnectorToolContext, build_connector_tool_context};#[cfg(any(test, feature = "test-support"))]-pub use tool_context::{build_image_generation_tool_context_test, no_op_schedule_context};+pub use tool_context::{+ build_channel_media_upload_tool_context_test, build_image_generation_tool_context_test,+ no_op_schedule_context,+};pub type AiToolSet = AsyncToolCollection<ToolServiceContext>;/// Database-only capabilities for the in-app database assistant. This excludes@@ -276,6 +280,15 @@ pub fn tools_for(host: AiHost) -> ToolSetWithPrompt {}AiHost::ChannelBot | AiHost::Mcp => toolset,};+ // A direct upload needs a client that can PUT the bytes itself; in-app+ // hosts have no network-capable shell, so only MCP clients get it.+ let toolset = match host {+ AiHost::Mcp => toolset.add_tool::<+ channels::inbound::toolset::CreateChannelMediaUpload,+ ToolChannelMediaUploadToolContext,+ >(),+ AiHost::Chat | AiHost::AgentSession | AiHost::ChannelBot => toolset,+ };// Connector discovery belongs to agent sessions. Their runtime refreshes// the permitted connector list before the next turn after authorization.let toolset = match host {diff --git a/crates/ai_tools/src/test.rs b/crates/ai_tools/src/test.rsindex 31d584a9cb..f24fa5153b 100644--- a/crates/ai_tools/src/test.rs+++ b/crates/ai_tools/src/test.rs@@ -483,6 +483,27 @@ fn the_mcp_host_defers_nothing() {assert!(!tools.prompt.to_string().contains("LoadTools"));}+/// Only an MCP client can PUT bytes to the upload URL, so the tool is not+/// offered to in-app hosts, which would call it and then have no way to use it.+#[test]+fn channel_media_uploads_are_mcp_only() {+ assert!(+ tools_for(AiHost::Mcp)+ .toolset+ .tools+ .contains_key("CreateChannelMediaUpload")+ );+ for host in [AiHost::Chat, AiHost::AgentSession, AiHost::ChannelBot] {+ assert!(+ !tools_for(host)+ .toolset+ .tools+ .contains_key("CreateChannelMediaUpload"),+ "{host:?}"+ );+ }+}+#[test]fn every_eager_tool_exists() {let tools = tools_for(AiHost::AgentSession);diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rsindex 6ad924b076..caf473c7b2 100644--- a/crates/ai_tools/src/tool_context.rs+++ b/crates/ai_tools/src/tool_context.rs@@ -76,6 +76,12 @@ use teams::{inbound::toolset::TeamToolContext, outbound::team_repo::TeamRepositouse tokio_util::task::TaskTracker;mod activity_metadata;+mod channel_media;+#[cfg(any(test, feature = "test-support"))]+pub use channel_media::build_channel_media_upload_tool_context_test;+pub use channel_media::{+ ToolChannelMediaUploadToolContext, build_channel_media_upload_tool_context,+};mod coding_agents;pub use coding_agents::{ToolCodingAgentToolContext, build_coding_agent_tool_context};mod images;@@ -1780,6 +1786,8 @@ pub struct ToolServiceContext {#[from_ref(skip)]pub chat_tool_context: ToolChatToolContext,pub channel_tool_context: ToolChannelToolContext,+ /// Direct uploads of channel images and videos; exposed to MCP clients only.+ pub channel_media_upload_tool_context: ToolChannelMediaUploadToolContext,pub bot_tool_context: ToolBotToolContext,pub coding_agent_tool_context: ToolCodingAgentToolContext,pub project_tool_context: ToolProjectToolContext,diff --git a/crates/ai_tools/src/tool_context/channel_media.rs b/crates/ai_tools/src/tool_context/channel_media.rsnew file mode 100644index 0000000000..b0622631c9--- /dev/null+++ b/crates/ai_tools/src/tool_context/channel_media.rs@@ -0,0 +1,35 @@+//! Composition of channel media uploads with the static file service.++/// Reserves direct uploads of channel images and videos in static-file storage.+pub type ToolChannelMediaUploadToolContext =+ channels::inbound::toolset::ChannelMediaUploadToolContext<+ channels::outbound::static_file_media_uploads::StaticFileMediaUploads,+ >;++/// Reserve channel media through the static file service's internal API.+pub fn build_channel_media_upload_tool_context() -> anyhow::Result<ToolChannelMediaUploadToolContext>+{+ Ok(with_client(+ static_file_service_client::StaticFileServiceClient::new(+ macro_auth::InternalApiKey::new()?.to_string(),+ macro_service_urls::StaticFileServiceUrl::new()?.to_string(),+ ),+ ))+}++fn with_client(+ client: static_file_service_client::StaticFileServiceClient,+) -> ToolChannelMediaUploadToolContext {+ channels::inbound::toolset::ChannelMediaUploadToolContext::new(+ channels::outbound::static_file_media_uploads::StaticFileMediaUploads::new(client),+ )+}++/// Test context pointing at an unreachable static file service.+#[cfg(any(test, feature = "test-support"))]+pub fn build_channel_media_upload_tool_context_test() -> ToolChannelMediaUploadToolContext {+ with_client(static_file_service_client::StaticFileServiceClient::new(+ "test-key".to_string(),+ "https://static.example.test".to_string(),+ ))+}diff --git a/crates/channels/src/domain/media_upload.rs b/crates/channels/src/domain/media_upload.rsnew file mode 100644index 0000000000..65e43acaac--- /dev/null+++ b/crates/channels/src/domain/media_upload.rs@@ -0,0 +1,164 @@+//! Reserve upload slots for images and videos that will be attached to channel+//! messages. The caller uploads the bytes straight to object storage, so the+//! file never travels through the service that issued the slot.++#[cfg(test)]+mod test;++use std::{future::Future, str::FromStr};++use macro_user_id::user_id::MacroUserIdStr;+use model_file_type::FileType;+use uuid::Uuid;++/// How long a reserved upload URL accepts a new upload.+pub const MEDIA_UPLOAD_URL_TTL_SECONDS: u64 = 120;++/// Media that renders inline in a channel message.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum ChannelMediaKind {+ /// An inline image.+ Image,+ /// An inline, playable video.+ Video,+}++impl ChannelMediaKind {+ /// The message attachment entity type for this kind of media.+ pub fn attachment_entity_type(self) -> &'static str {+ match self {+ Self::Image => "static/image",+ Self::Video => "static/video",+ }+ }++ fn of_content_type(content_type: &str) -> Option<Self> {+ let essence = content_type.split(';').next()?.trim();+ if essence.starts_with("image/") {+ Some(Self::Image)+ } else if essence.starts_with("video/") {+ Some(Self::Video)+ } else {+ None+ }+ }+}++/// A request to upload one image or video for a channel message.+#[derive(Debug, Clone)]+pub struct NewChannelMediaUpload {+ /// Filename, including its extension.+ pub file_name: String,+ /// MIME type, when the extension does not identify it.+ pub content_type: Option<String>,+}++/// A media file whose storage slot the static-file store has created.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct StaticMediaReservation {+ /// Static file ID, used as the message attachment ID.+ pub file_id: Uuid,+ /// Presigned URL that accepts one HTTP PUT of the file bytes.+ pub upload_url: String,+}++/// A reserved upload, ready for the caller to PUT the bytes.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct ChannelMediaUpload {+ /// Attachment ID to send with the message once the bytes are uploaded.+ pub attachment_id: Uuid,+ /// How the attachment renders.+ pub kind: ChannelMediaKind,+ /// Presigned URL that accepts one HTTP PUT of the file bytes.+ pub upload_url: String,+ /// Content type the PUT must declare; the URL is signed for it.+ pub content_type: String,+}++/// Why a media upload could not be reserved.+#[derive(Debug, thiserror::Error)]+pub enum ChannelMediaUploadError {+ /// The request was malformed or named a file that is not media.+ #[error("{0}")]+ Invalid(String),+ /// The static-file store failed.+ #[error("failed to reserve the upload")]+ Storage(#[source] anyhow::Error),+}++/// Creates static-file storage slots owned by a user.+pub trait StaticMediaStore: Send + Sync + 'static {+ /// Reserve a slot for `file_name`, owned by `owner`, that accepts an upload+ /// declaring `content_type`.+ fn reserve(+ &self,+ owner: &MacroUserIdStr<'_>,+ file_name: &str,+ content_type: &str,+ ) -> impl Future<Output = anyhow::Result<StaticMediaReservation>> + Send;+}++/// Reserves media uploads for channel message attachments.+pub struct ChannelMediaUploads<S> {+ store: S,+}++impl<S: StaticMediaStore> ChannelMediaUploads<S> {+ /// Use `store` for the reserved files.+ pub fn new(store: S) -> Self {+ Self { store }+ }++ /// Reserve an upload for one image or video owned by `owner`. Other files+ /// are rejected: they belong in documents, not inline media.+ #[tracing::instrument(skip_all, err)]+ pub async fn reserve(+ &self,+ owner: &MacroUserIdStr<'_>,+ upload: NewChannelMediaUpload,+ ) -> Result<ChannelMediaUpload, ChannelMediaUploadError> {+ let file_name = upload.file_name.trim();+ if file_name.is_empty()+ || file_name.contains(['/', '\\'])+ || file_name.chars().any(char::is_control)+ {+ return Err(ChannelMediaUploadError::Invalid(+ "fileName must be a filename without directory components".to_string(),+ ));+ }+ let content_type = match upload.content_type.as_deref().map(str::trim) {+ Some(content_type) if !content_type.is_empty() => content_type.to_ascii_lowercase(),+ _ => content_type_from_extension(file_name).ok_or_else(|| {+ ChannelMediaUploadError::Invalid(+ "cannot infer the media type from fileName; pass contentType, for example video/mp4"+ .to_string(),+ )+ })?,+ };+ let kind = ChannelMediaKind::of_content_type(&content_type).ok_or_else(|| {+ ChannelMediaUploadError::Invalid(format!(+ "{content_type} is not an image or video; upload other files as documents and attach the document ID"+ ))+ })?;++ let reservation = self+ .store+ .reserve(owner, file_name, &content_type)+ .await+ .map_err(ChannelMediaUploadError::Storage)?;++ Ok(ChannelMediaUpload {+ attachment_id: reservation.file_id,+ kind,+ upload_url: reservation.upload_url,+ content_type,+ })+ }+}++fn content_type_from_extension(file_name: &str) -> Option<String> {+ let (_, extension) = file_name.rsplit_once('.')?;+ FileType::from_str(extension)+ .ok()+ .map(|file_type| file_type.mime_type().to_string())+}diff --git a/crates/channels/src/domain/media_upload/test.rs b/crates/channels/src/domain/media_upload/test.rsnew file mode 100644index 0000000000..dbac7edd3f--- /dev/null+++ b/crates/channels/src/domain/media_upload/test.rs@@ -0,0 +1,135 @@+use std::sync::Mutex;++use super::*;++#[derive(Default)]+struct FakeStore {+ reserved: Mutex<Vec<(String, String, String)>>,+ fail: bool,+}++impl StaticMediaStore for FakeStore {+ async fn reserve(+ &self,+ owner: &MacroUserIdStr<'_>,+ file_name: &str,+ content_type: &str,+ ) -> anyhow::Result<StaticMediaReservation> {+ if self.fail {+ anyhow::bail!("store unavailable");+ }+ self.reserved.lock().unwrap().push((+ owner.to_string(),+ file_name.to_string(),+ content_type.to_string(),+ ));+ Ok(StaticMediaReservation {+ file_id: Uuid::nil(),+ upload_url: "https://upload.example.test/put".to_string(),+ })+ }+}++fn owner() -> MacroUserIdStr<'static> {+ MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap()+}++fn upload(file_name: &str, content_type: Option<&str>) -> NewChannelMediaUpload {+ NewChannelMediaUpload {+ file_name: file_name.to_string(),+ content_type: content_type.map(str::to_string),+ }+}++#[tokio::test]+async fn infers_video_from_extension() {+ let uploads = ChannelMediaUploads::new(FakeStore::default());++ let reserved = uploads+ .reserve(&owner(), upload("Demo.MP4", None))+ .await+ .unwrap();++ assert_eq!(reserved.kind, ChannelMediaKind::Video);+ assert_eq!(reserved.content_type, "video/mp4");+ assert_eq!(reserved.attachment_id, Uuid::nil());+ assert_eq!(reserved.upload_url, "https://upload.example.test/put");+ assert_eq!(+ *uploads.store.reserved.lock().unwrap(),+ vec![(+ "macro|[email protected]".to_string(),+ "Demo.MP4".to_string(),+ "video/mp4".to_string()+ )]+ );+}++#[tokio::test]+async fn explicit_content_type_wins() {+ let uploads = ChannelMediaUploads::new(FakeStore::default());++ let reserved = uploads+ .reserve(&owner(), upload("clip", Some("Image/PNG")))+ .await+ .unwrap();++ assert_eq!(reserved.kind, ChannelMediaKind::Image);+ assert_eq!(reserved.content_type, "image/png");+}++#[tokio::test]+async fn rejects_files_that_are_not_media() {+ let uploads = ChannelMediaUploads::new(FakeStore::default());++ for request in [+ upload("report.pdf", None),+ upload("clip.mp4", Some("application/octet-stream")),+ ] {+ let error = uploads.reserve(&owner(), request).await.unwrap_err();+ assert!(matches!(error, ChannelMediaUploadError::Invalid(_)));+ }+ assert!(uploads.store.reserved.lock().unwrap().is_empty());+}++#[tokio::test]+async fn rejects_unknown_extension_without_content_type() {+ let uploads = ChannelMediaUploads::new(FakeStore::default());++ let error = uploads+ .reserve(&owner(), upload("clip.unknownext", None))+ .await+ .unwrap_err();++ assert!(matches!(error, ChannelMediaUploadError::Invalid(_)));+}++#[tokio::test]+async fn rejects_paths_and_empty_names() {+ let uploads = ChannelMediaUploads::new(FakeStore::default());++ for name in ["", " ", "videos/clip.mp4", "..\\clip.mp4", "clip\n.mp4"] {+ let error = uploads+ .reserve(&owner(), upload(name, None))+ .await+ .unwrap_err();+ assert!(+ matches!(error, ChannelMediaUploadError::Invalid(_)),+ "{name:?}"+ );+ }+}++#[tokio::test]+async fn storage_failure_is_reported() {+ let uploads = ChannelMediaUploads::new(FakeStore {+ fail: true,+ ..FakeStore::default()+ });++ let error = uploads+ .reserve(&owner(), upload("clip.mp4", None))+ .await+ .unwrap_err();++ assert!(matches!(error, ChannelMediaUploadError::Storage(_)));+}diff --git a/crates/channels/src/domain/mod.rs b/crates/channels/src/domain/mod.rsindex 78e691115c..9a9c1fb910 100644--- a/crates/channels/src/domain/mod.rs+++ b/crates/channels/src/domain/mod.rs@@ -22,6 +22,8 @@ pub mod historical;/// Legacy channel list service implementation.#[cfg(feature = "list")]pub mod list_service;+/// Reserved uploads for images and videos attached to channel messages.+pub mod media_upload;/// Kafka event models for the `macro.mentions` topic.pub mod mention_events;/// Channel side effects for messages committed by the shared message service.diff --git a/crates/channels/src/inbound/toolset.rs b/crates/channels/src/inbound/toolset.rsindex 9c7f199958..d041fb890c 100644--- a/crates/channels/src/inbound/toolset.rs+++ b/crates/channels/src/inbound/toolset.rs@@ -1,6 +1,7 @@//! AI tools for reading channel messages and mutating channel membership.mod create_channel;+mod create_channel_media_upload;mod delete_channel_message;mod edit_channel_message;mod manage_channel_participants;@@ -46,11 +47,15 @@ use messages::domain::{use std::sync::Arc;use uuid::Uuid;+pub use create_channel_media_upload::{+ ChannelMediaUploadToolContext, CreateChannelMediaUpload, CreateChannelMediaUploadResponse,+};pub use read_channel_message_context::{ReadChannelMessageContext, ReadChannelMessageContextResponse,};pub use read_channel_messages::{ReadChannelMessages, ReadChannelMessagesResponse};pub use read_channel_thread::{ReadChannelThread, ReadChannelThreadResponse};+pub use types::{ChannelAttachmentKind, ChannelMessageAttachmentInput};/// Service context for channel AI tools.pub struct ChannelToolContext<Svc, AccessSvc>diff --git a/crates/channels/src/inbound/toolset/create_channel_media_upload.rs b/crates/channels/src/inbound/toolset/create_channel_media_upload.rsnew file mode 100644index 0000000000..90f4247286--- /dev/null+++ b/crates/channels/src/inbound/toolset/create_channel_media_upload.rs@@ -0,0 +1,131 @@+//! Reserve a direct upload for an image or video to attach to a channel message.++use std::sync::Arc;++use ai_toolset::{+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolCallError,+ ToolResult,+};+use async_trait::async_trait;+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};+use uuid::Uuid;++use super::types::ChannelAttachmentKind;+use crate::domain::media_upload::{+ ChannelMediaKind, ChannelMediaUploadError, ChannelMediaUploads, MEDIA_UPLOAD_URL_TTL_SECONDS,+ NewChannelMediaUpload, StaticMediaStore,+};++/// Service context for reserving channel media uploads.+pub struct ChannelMediaUploadToolContext<S> {+ /// Reserves uploads in static-file storage.+ pub uploads: Arc<ChannelMediaUploads<S>>,+}++impl<S> Clone for ChannelMediaUploadToolContext<S> {+ fn clone(&self) -> Self {+ Self {+ uploads: self.uploads.clone(),+ }+ }+}++impl<S: StaticMediaStore> ChannelMediaUploadToolContext<S> {+ /// Reserve uploads in `store`.+ pub fn new(store: S) -> Self {+ Self {+ uploads: Arc::new(ChannelMediaUploads::new(store)),+ }+ }+}++/// Reserve a direct upload for one channel image or video.+#[derive(Debug, Clone, Deserialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+#[schemars(+ title = "CreateChannelMediaUpload",+ description = "Reserve a direct upload for an image or video to attach to a channel message; use this for media too large to pass inline, such as screen recordings. Returns an uploadUrl: immediately PUT the raw file bytes to it from your shell with the returned contentType as the Content-Type header (see uploadCommand), then call SendChannelMessage with an attachment whose id is attachmentId and whose kind is the returned kind. The URL must be used within 2 minutes; call again for a fresh one if it expires. Only images and videos are accepted; attach other files by uploading them as documents and passing the document ID."+)]+pub struct CreateChannelMediaUpload {+ #[schemars(+ description = "Filename including its extension, for example demo.mp4. Do not include a directory path."+ )]+ /// Filename, including its extension.+ pub file_name: String,+ #[serde(default)]+ #[schemars(+ description = "MIME type such as video/mp4 or image/png. Omit to infer it from the fileName extension."+ )]+ /// MIME type, when the extension does not identify it.+ pub content_type: Option<String>,+}++/// A reserved upload slot for one media file.+#[derive(Debug, Clone, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct CreateChannelMediaUploadResponse {+ /// Attachment ID to pass to SendChannelMessage after the upload succeeds.+ pub attachment_id: Uuid,+ /// Attachment kind to pass to SendChannelMessage.+ pub kind: ChannelAttachmentKind,+ /// Presigned URL that accepts one HTTP PUT of the raw file bytes.+ pub upload_url: String,+ /// Content-Type header value the PUT must send; the URL is signed for it.+ pub content_type: String,+ /// Seconds before the upload URL stops accepting new uploads.+ pub expires_in_seconds: u64,+ /// A curl command that performs the upload; replace <path-to-file>.+ pub upload_command: String,+}++impl ToolAnnotated for CreateChannelMediaUpload {+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::additive("Create channel media upload");+}++#[async_trait]+impl<S: StaticMediaStore> AsyncTool<ChannelMediaUploadToolContext<S>> for CreateChannelMediaUpload {+ type Output = CreateChannelMediaUploadResponse;++ #[tracing::instrument(skip_all, fields(user_id=?request_context.user_id), err)]+ async fn call(+ &self,+ service_context: ServiceContext<ChannelMediaUploadToolContext<S>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ let upload = service_context+ .uploads+ .reserve(+ &request_context.user_id,+ NewChannelMediaUpload {+ file_name: self.file_name.clone(),+ content_type: self.content_type.clone(),+ },+ )+ .await+ .map_err(|error| ToolCallError {+ description: match &error {+ ChannelMediaUploadError::Invalid(message) => message.clone(),+ ChannelMediaUploadError::Storage(_) => {+ "failed to reserve the upload; try again".to_string()+ }+ },+ internal_error: error.into(),+ })?;++ Ok(CreateChannelMediaUploadResponse {+ attachment_id: upload.attachment_id,+ kind: match upload.kind {+ ChannelMediaKind::Image => ChannelAttachmentKind::Image,+ ChannelMediaKind::Video => ChannelAttachmentKind::Video,+ },+ upload_command: format!(+ "curl --fail -X PUT -H 'Content-Type: {}' --upload-file <path-to-file> '{}'",+ upload.content_type, upload.upload_url+ ),+ upload_url: upload.upload_url,+ content_type: upload.content_type,+ expires_in_seconds: MEDIA_UPLOAD_URL_TTL_SECONDS,+ })+ }+}diff --git a/crates/channels/src/inbound/toolset/send_channel_message.rs b/crates/channels/src/inbound/toolset/send_channel_message.rsindex b24132935a..d2ed3d9237 100644--- a/crates/channels/src/inbound/toolset/send_channel_message.rs+++ b/crates/channels/src/inbound/toolset/send_channel_message.rs@@ -6,22 +6,22 @@ use serde::{Deserialize, Serialize};use uuid::Uuid;use crate::domain::ports::ChannelService;-use crate::inbound::toolset::ChannelToolContext;+use crate::inbound::toolset::{ChannelToolContext, types::ChannelMessageAttachmentInput};use entity_access::domain::ports::EntityAccessService;use messages::domain::{- models::{MessageAttribution, PostMessage, PostMessageNotificationPolicy},+ models::{MessageAttribution, NewAttachment, PostMessage, PostMessageNotificationPolicy},ports::MessageError,};#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]#[schemars(title = "SendChannelMessage",- description = "Send or reply to a channel on behalf of the user. Only use this when explicitly asked to send a message"+ description = "Send or reply to a channel on behalf of the user. Only use this when explicitly asked to send a message. To send an image or video, first upload it with CreateChannelMediaUpload (where available) and pass the returned attachment here; to attach a Macro document, pass its document ID.")]pub struct SendChannelMessage {/// macro markdown#[schemars(- description = "Message content in macro markdown format. This uses the same syntax as markdown documents"+ description = "Message content in macro markdown format. This uses the same syntax as markdown documents. May be empty when attachments are sent.")]pub content: String,/// the channel to send to@@ -30,6 +30,12 @@ pub struct SendChannelMessage {/// an optional thread being replied too#[schemars(description = "An optional thread id to reply too")]pub thread_id: Option<Uuid>,+ /// files shown with the message+ #[serde(default)]+ #[schemars(+ description = "Optional images, videos, or documents to attach, at most 10. Upload media before sending; a media attachment whose upload has not finished shows as broken."+ )]+ pub attachments: Vec<ChannelMessageAttachmentInput>,}#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]@@ -79,12 +85,12 @@ wherethread_id: self.thread_id,anchor: None,mentions: vec![],- attachments: vec![],+ attachments: self.attachments.iter().map(NewAttachment::from).collect(),nonce: None,},).await- .map_err(tool_err("failed to send message"))+ .map_err(send_error).map(|message| SendChannelMessageResponse {channel_id: self.channel_id,message_id: message.id.to_string(),@@ -92,9 +98,20 @@ where}}-fn tool_err(description: &'static str) -> impl FnOnce(MessageError) -> ToolCallError {- move |err| ToolCallError {- description: description.to_string(),+fn send_error(err: MessageError) -> ToolCallError {+ let description = match &err {+ MessageError::Invalid(message) => (*message).to_string(),+ MessageError::Forbidden => {+ "not allowed to send this message: it attaches or mentions an item the user cannot view"+ .to_string()+ }+ MessageError::NotFound => "thread not found in this channel".to_string(),+ MessageError::Conflict | MessageError::Repository(_) => {+ "failed to send message".to_string()+ }+ };+ ToolCallError {+ description,internal_error: anyhow::Error::new(err),}}diff --git a/crates/channels/src/inbound/toolset/test.rs b/crates/channels/src/inbound/toolset/test.rsindex 0cff518c52..1cb90f1144 100644--- a/crates/channels/src/inbound/toolset/test.rs+++ b/crates/channels/src/inbound/toolset/test.rs@@ -1202,6 +1202,7 @@ async fn send_channel_message_posts_as_the_context_actor_for_the_user() {content: "hello".to_string(),channel_id,thread_id: None,+ attachments: vec![],};tool.call(@@ -1252,6 +1253,76 @@ async fn send_channel_message_posts_as_the_context_actor_for_the_user() {assert_eq!(bot_of(custom_access), Some(BotId::TEST_A));}+#[tokio::test]+async fn send_channel_message_maps_attachments_to_message_references() {+ let video_id = Uuid::new_v4().to_string();+ let document_id = Uuid::new_v4().to_string();+ let messages = RecordingMessages::default();+ let posts = messages.posts.clone();+ let tool: SendChannelMessage = serde_json::from_value(serde_json::json!({+ "content": "",+ "channel_id": Uuid::new_v4(),+ "attachments": [+ { "id": video_id, "kind": "video", "width": 1920, "height": 1080 },+ { "id": format!(" {document_id} "), "kind": "document", "width": 10, "height": 10 },+ ],+ }))+ .expect("attachments deserialize");++ tool.call(+ ServiceContext(ChannelToolContext::new(+ Arc::new(messages),+ ToolTestChannelService::default(),+ ToolTestAccessService::default(),+ )),+ RequestContext::new(user_id()),+ )+ .await+ .expect("member can post attachments");++ let posts = posts.lock().expect("post lock");+ let [(_, input)] = posts.as_slice() else {+ panic!("expected one post, got {}", posts.len());+ };+ assert_eq!(+ input.attachments,+ vec![+ messages::domain::models::NewAttachment {+ entity_type: "static/video".to_string(),+ entity_id: video_id,+ width: Some(1920),+ height: Some(1080),+ },+ messages::domain::models::NewAttachment {+ entity_type: "document".to_string(),+ entity_id: document_id,+ width: None,+ height: None,+ },+ ]+ );+}++#[test]+fn send_channel_message_without_attachments_still_deserializes() {+ let tool: SendChannelMessage = serde_json::from_value(serde_json::json!({+ "content": "hello",+ "channel_id": Uuid::new_v4(),+ }))+ .expect("attachments are optional");+ assert!(tool.attachments.is_empty());+}++#[test]+fn send_channel_message_schema_is_valid() {+ generate_validated_input_schema::<SendChannelMessage>().expect("valid schema");+}++#[test]+fn create_channel_media_upload_schema_is_valid() {+ generate_validated_input_schema::<super::CreateChannelMediaUpload>().expect("valid schema");+}+#[tokio::test]async fn manage_participants_adds_canonical_ids() {let channel_id = Uuid::new_v4();diff --git a/crates/channels/src/inbound/toolset/types.rs b/crates/channels/src/inbound/toolset/types.rsindex 8060f7cce6..d025a4dc60 100644--- a/crates/channels/src/inbound/toolset/types.rs+++ b/crates/channels/src/inbound/toolset/types.rs@@ -1,9 +1,11 @@//! Shared channel AI tool input/output types.use messages::domain::{models::MessageListItem,- models::{CountedReaction, Message, MessageAttachment},+ models::{CountedReaction, Message, MessageAttachment, NewAttachment},};+use crate::domain::media_upload::ChannelMediaKind;+use chrono::{DateTime, Utc};use schemars::JsonSchema;use serde::{Deserialize, Serialize};@@ -37,6 +39,66 @@ pub enum ToolMessageKind {ThreadReply,}+/// How an attachment on a sent message renders.+#[derive(Debug, Clone, Copy, Deserialize, Serialize, JsonSchema, PartialEq, Eq)]+#[serde(rename_all = "camelCase")]+pub enum ChannelAttachmentKind {+ /// An inline image uploaded with CreateChannelMediaUpload.+ Image,+ /// An inline, playable video uploaded with CreateChannelMediaUpload.+ Video,+ /// A Macro document, shown as a file chip.+ Document,+}++impl ChannelAttachmentKind {+ fn entity_type(self) -> &'static str {+ match self {+ Self::Image => ChannelMediaKind::Image.attachment_entity_type(),+ Self::Video => ChannelMediaKind::Video.attachment_entity_type(),+ Self::Document => "document",+ }+ }+}++/// An attachment to send with a channel message.+#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]+#[serde(rename_all = "camelCase")]+pub struct ChannelMessageAttachmentInput {+ /// The attachmentId from CreateChannelMediaUpload, or a document ID.+ #[schemars(+ description = "The attachmentId returned by CreateChannelMediaUpload (after its upload succeeded), or the ID of a Macro document."+ )]+ pub id: String,+ /// How the attachment renders.+ #[schemars(+ description = "image or video for media from CreateChannelMediaUpload (use the kind it returned); document for a Macro document ID."+ )]+ pub kind: ChannelAttachmentKind,+ /// Optional pixel width of an image or video, used to lay it out before it loads.+ #[serde(default)]+ #[schemars(+ description = "Optional pixel width of an image or video, used to lay it out before it loads."+ )]+ pub width: Option<i32>,+ /// Optional pixel height of an image or video.+ #[serde(default)]+ #[schemars(description = "Optional pixel height of an image or video.")]+ pub height: Option<i32>,+}++impl From<&ChannelMessageAttachmentInput> for NewAttachment {+ fn from(input: &ChannelMessageAttachmentInput) -> Self {+ let media = input.kind != ChannelAttachmentKind::Document;+ Self {+ entity_type: input.kind.entity_type().to_string(),+ entity_id: input.id.trim().to_string(),+ width: input.width.filter(|_| media),+ height: input.height.filter(|_| media),+ }+ }+}+/// A reaction summary on a message.#[derive(Debug, Clone, Serialize, JsonSchema)]#[serde(rename_all = "camelCase")]diff --git a/crates/channels/src/outbound/mod.rs b/crates/channels/src/outbound/mod.rsindex 6e334ecf47..61041d9f01 100644--- a/crates/channels/src/outbound/mod.rs+++ b/crates/channels/src/outbound/mod.rs@@ -12,5 +12,7 @@ pub mod pg_channel_reference_share_permissions;pub mod pg_channels_repo;/// Postgres context adapter for channel side-effect policy.pub mod pg_side_effect_context;+/// Static-file adapter that reserves channel media uploads.+pub mod static_file_media_uploads;/// Static-file metadata adapter for channel pictures.pub mod static_file_pictures;diff --git a/crates/channels/src/outbound/static_file_media_uploads.rs b/crates/channels/src/outbound/static_file_media_uploads.rsnew file mode 100644index 0000000000..38bae94659--- /dev/null+++ b/crates/channels/src/outbound/static_file_media_uploads.rs@@ -0,0 +1,40 @@+//! Static-file adapter that reserves channel media uploads.++use crate::domain::media_upload::{StaticMediaReservation, StaticMediaStore};+use anyhow::Context;+use macro_user_id::user_id::MacroUserIdStr;+use static_file_service_client::StaticFileServiceClient;++/// Reserves channel media in the static-file service.+#[derive(Clone)]+pub struct StaticFileMediaUploads {+ client: StaticFileServiceClient,+}++impl StaticFileMediaUploads {+ /// Create a media upload adapter with an authenticated service client.+ pub fn new(client: StaticFileServiceClient) -> Self {+ Self { client }+ }+}++impl StaticMediaStore for StaticFileMediaUploads {+ async fn reserve(+ &self,+ owner: &MacroUserIdStr<'_>,+ file_name: &str,+ content_type: &str,+ ) -> anyhow::Result<StaticMediaReservation> {+ let reserved = self+ .client+ .reserve_for_user(&owner.to_string(), file_name, content_type)+ .await?;+ Ok(StaticMediaReservation {+ file_id: reserved+ .id+ .parse()+ .context("static file service returned an invalid file id")?,+ upload_url: reserved.upload_url,+ })+ }+}diff --git a/crates/static_file_service_client/src/lib.rs b/crates/static_file_service_client/src/lib.rsindex 049a44210e..d6a21896e3 100644--- a/crates/static_file_service_client/src/lib.rs+++ b/crates/static_file_service_client/src/lib.rs@@ -3,6 +3,9 @@ pub mod get_file;pub mod put_file;pub static INTERNAL_ACCESS_HEADER: &str = "x-internal-auth-key";+/// Names the user an internal request acts for; the service records them as+/// the owner of files it reserves.+pub static INTERNAL_MACRO_USER_ID_HEADER: &str = "x-internal-macro-user-id";#[derive(Clone)]pub struct StaticFileServiceClient {diff --git a/crates/static_file_service_client/src/put_file.rs b/crates/static_file_service_client/src/put_file.rsindex b24b355c04..ef7515a2cd 100644--- a/crates/static_file_service_client/src/put_file.rs+++ b/crates/static_file_service_client/src/put_file.rs@@ -88,6 +88,43 @@ impl StaticFileServiceClient {.await}+ /// Reserve a file owned by `owner_id` without uploading it. The caller+ /// PUTs the bytes to the returned `upload_url`, declaring `content_type`,+ /// before the URL expires.+ #[tracing::instrument(skip(self), err)]+ pub async fn reserve_for_user(+ &self,+ owner_id: &str,+ file_name: &str,+ content_type: &str,+ ) -> Result<PutFileResponse> {+ let full_url = format!("{}/internal/file", self.url);+ let res = self+ .client+ .put(&full_url)+ .header(crate::INTERNAL_MACRO_USER_ID_HEADER, owner_id)+ .json(&PutFileRequest {+ file_name: file_name.to_string(),+ content_type: Some(content_type.to_string()),+ extension_data: None,+ })+ .send()+ .await?;++ let status_code = res.status();+ if status_code != reqwest::StatusCode::OK {+ let body: String = res.text().await?;+ tracing::error!(+ body=%body,+ status=%status_code,+ "unexpected response from static file service"+ );+ anyhow::bail!("HTTP {}: {}", status_code, body);+ }++ Ok(res.json().await?)+ }+/// Reserve a file and PUT its bytes to the upload URL that comes back.async fn upload(&self,diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rsindex 3b7361c8e6..0c7e4fd6d5 100644--- a/services/document_cognition_service/src/api/context/test.rs+++ b/services/document_cognition_service/src/api/context/test.rs@@ -540,6 +540,7 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Arpool.clone(),std::sync::Arc::new(test_lexical_client),),+ channel_media_upload_tool_context: ai_tools::build_channel_media_upload_tool_context_test(),bot_tool_context: ai_tools::build_bot_tool_context(pool.clone(),ai_tools::MaybeToolEventBroker::Real(macro_event_broker.clone()),diff --git a/services/document_cognition_service/src/server.rs b/services/document_cognition_service/src/server.rsindex 31b4dcf94d..7785351f3c 100644--- a/services/document_cognition_service/src/server.rs+++ b/services/document_cognition_service/src/server.rs@@ -780,6 +780,7 @@ pub async fn run() -> anyhow::Result<()> {),chat_tool_context,channel_tool_context,+ channel_media_upload_tool_context: ai_tools::build_channel_media_upload_tool_context()?,bot_tool_context: ai_tools::build_bot_tool_context(db.clone(),ai_tools::MaybeToolEventBroker::Real(macro_event_broker.clone()),diff --git a/services/mcp_service/src/context.rs b/services/mcp_service/src/context.rsindex 4d70471c02..8220faf10d 100644--- a/services/mcp_service/src/context.rs+++ b/services/mcp_service/src/context.rs@@ -577,6 +577,7 @@ async fn build_tool_context(args: ToolContextBuildArgs<'_>) -> anyhow::Result<Toimport_tool_context: ToolImportToolContext::wired(import_service),chat_tool_context,channel_tool_context,+ channel_media_upload_tool_context: ai_tools::build_channel_media_upload_tool_context()?,bot_tool_context: ai_tools::build_bot_tool_context(db.clone(),ai_tools::MaybeToolEventBroker::Real(macro_event_broker.clone()),