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(ai-tools): accept email targets and surface tool argument errors (#6363)
gbirmand6e7a85c12d5
diff --git a/apps/docs/AI/mcp/tools/get-entity-properties.mdx b/apps/docs/AI/mcp/tools/get-entity-properties.mdxindex e69ca2ec80..058314511f 100644--- a/apps/docs/AI/mcp/tools/get-entity-properties.mdx+++ b/apps/docs/AI/mcp/tools/get-entity-properties.mdx@@ -12,4 +12,4 @@ Get all properties attached to an entity (document, project, CRM company, etc.).| Parameter | Type | Required | Description || --- | --- | --- | --- || `entity_id` | string | Yes | The ID of the entity to get properties for. |-| `entity_type` | `"document"` \| `"project"` \| `"chat"` \| `"thread"` \| `"channel"` \| `"call"` \| `"user"` \| `"company"` | Yes | Canonical entity type accepted when an AI tool targets an entity's properties. |+| `entity_type` | `"document"` \| `"project"` \| `"chat"` \| `"thread"` \| `"channel"` \| `"call"` \| `"user"` \| `"company"` | Yes | Canonical entity type accepted when an AI tool targets an entity's properties. Tasks are targeted as `document`; email threads (type `email` in ListEntities and search results) are targeted as `thread`. |diff --git a/apps/docs/AI/mcp/tools/set-entity-property.mdx b/apps/docs/AI/mcp/tools/set-entity-property.mdxindex f4bff6a69a..43110c9496 100644--- a/apps/docs/AI/mcp/tools/set-entity-property.mdx+++ b/apps/docs/AI/mcp/tools/set-entity-property.mdx@@ -31,7 +31,7 @@ For non-system or custom properties, call GetEntityProperties first to discover| Parameter | Type | Required | Description || --- | --- | --- | --- || `entity_id` | string | Yes | The ID of the entity to update. |-| `entity_type` | `"document"` \| `"project"` \| `"chat"` \| `"thread"` \| `"channel"` \| `"call"` \| `"user"` \| `"company"` | Yes | Canonical entity type accepted when an AI tool targets an entity's properties. |+| `entity_type` | `"document"` \| `"project"` \| `"chat"` \| `"thread"` \| `"channel"` \| `"call"` \| `"user"` \| `"company"` | Yes | Canonical entity type accepted when an AI tool targets an entity's properties. Tasks are targeted as `document`; email threads (type `email` in ListEntities and search results) are targeted as `thread`. || `property_definition_id` | string | Yes | The property definition ID. Get this from GetEntityProperties results. || `boolean_value` | boolean | No | For boolean properties. || `date_value` | string | No | For date properties (ISO 8601 date-time). |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 0fa4dfd4d0..9588a6edaf 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@@ -31,6 +31,8 @@ export type CodeExecutionErrorCode =| 'string_not_found';/*** Canonical entity type accepted when an AI tool targets an entity's properties.+ * Tasks are targeted as `document`; email threads (type `email` in ListEntities+ * and search results) are targeted as `thread`.*/export type ToolPropertyTargetEntityType =| 'document'diff --git a/crates/agent/src/test/agent_loop/test_tool.rs b/crates/agent/src/test/agent_loop/test_tool.rsindex 328830f6a9..779ce010da 100644--- a/crates/agent/src/test/agent_loop/test_tool.rs+++ b/crates/agent/src/test/agent_loop/test_tool.rs@@ -111,6 +111,42 @@ async fn tool_call_error_emits_call_and_error_response() {assert_eq!(result.content(), "done", "the loop should continue");}+/// Invalid tool arguments+/// A call whose arguments don't match the tool's input schema must come back to+/// the model as a typed error naming what was wrong, so it can correct the call+/// instead of retrying the same input.+#[tokio::test]+async fn invalid_tool_arguments_emit_error_naming_the_problem() {+ let model = tool_then_done([MockStreamEvent::tool_call(+ "call-1",+ "echo_tool",+ serde_json::json!({ "value": 123 }),+ )]);+ let toolset = util::single_tool_set::<EchoTool, ()>();+ let mut session = util::session(toolset, Arc::new(()), model).await;++ let result = util::drive(&mut session, "call echo with a bad argument").await;++ let call = result+ .tool_calls()+ .into_iter()+ .find(|call| call.name == "echo_tool")+ .expect("the mistyped call should be emitted");+ let response = result+ .tool_response(&call.id)+ .expect("the mistyped call's response should be emitted");+ assert!(+ matches!(response, ToolResponse::Err { description, .. } if description.contains("expected a string")),+ "the error should carry the serde detail, got {response:?}"+ );+ assert!(+ result.error.is_none(),+ "invalid arguments must not stop the loop, got {:?}",+ result.error+ );+ assert_eq!(result.content(), "done", "the loop should continue");+}+/// Parallel tool call success/// Multiple tool calls are executed in one turn. They all succeed. The call/response eventsa re all emitted#[tokio::test]diff --git a/crates/ai_toolset/src/toolset/types.rs b/crates/ai_toolset/src/toolset/types.rsindex cecddecb8c..f1ec7fbc51 100644--- a/crates/ai_toolset/src/toolset/types.rs+++ b/crates/ai_toolset/src/toolset/types.rs@@ -11,6 +11,9 @@ use serde::de::Deserialize;use std::collections::BTreeMap;use thiserror::Error;+#[cfg(test)]+mod test;+/// some tools need additional information#[derive(Debug)]pub enum ToolInfo {@@ -40,7 +43,9 @@ pub enum ToolSetCreationError {#[derive(Debug, Error)]pub enum ToolSetError {/// Failed to deserialize the tool input (possibly an AI hallucination).- #[error("error deserializing tool call (possible hallucination)")]+ /// The message carries the serde detail so a model reading it as a tool+ /// result can correct the offending argument instead of retrying blindly.+ #[error("error deserializing tool call (possible hallucination): {0}")]Deserialization(serde_json::Error),/// The requested tool was not found in the toolset.#[error("tool not in toolset")]diff --git a/crates/ai_toolset/src/toolset/types/test.rs b/crates/ai_toolset/src/toolset/types/test.rsnew file mode 100644index 0000000000..3baedf0714--- /dev/null+++ b/crates/ai_toolset/src/toolset/types/test.rs@@ -0,0 +1,20 @@+use super::*;++/// The deserialization error is what a model reads back as the tool result, so+/// it has to say which argument was wrong rather than only that something was.+#[test]+fn deserialization_error_carries_the_serde_detail() {+ let error = serde_json::from_value::<u8>(serde_json::json!("email")).unwrap_err();+ let detail = error.to_string();++ let message = ToolSetError::Deserialization(error).to_string();++ assert!(+ message.starts_with("error deserializing tool call"),+ "message should keep its prefix, got {message}"+ );+ assert!(+ message.ends_with(&detail),+ "message should end with the serde detail {detail:?}, got {message}"+ );+}diff --git a/crates/properties/src/inbound/toolset/get_entity_properties.rs b/crates/properties/src/inbound/toolset/get_entity_properties.rsindex 6c52fe8686..daeb5a0f59 100644--- a/crates/properties/src/inbound/toolset/get_entity_properties.rs+++ b/crates/properties/src/inbound/toolset/get_entity_properties.rs@@ -25,6 +25,10 @@ pub enum ToolEntityType {Task,Project,Chat,+ // Listing and search tools report email threads as `email`. A doc comment+ // here would turn the schema enum into a named type, so this stays a plain+ // comment.+ #[serde(alias = "email", alias = "email_thread")]Thread,Channel,Call,@@ -49,12 +53,18 @@ impl From<ToolEntityType> for EntityType {}/// Canonical entity type accepted when an AI tool targets an entity's properties.+/// Tasks are targeted as `document`; email threads (type `email` in ListEntities+/// and search results) are targeted as `thread`.#[derive(Debug, Clone, Copy, Deserialize, JsonSchema)]#[serde(rename_all = "snake_case")]pub enum ToolPropertyTargetEntityType {Document,Project,Chat,+ // Listing and search tools report email threads as `email`. A doc comment+ // here would turn the schema enum into a named type, so this stays a plain+ // comment.+ #[serde(alias = "email", alias = "email_thread")]Thread,Channel,Call,diff --git a/crates/properties/src/inbound/toolset/test.rs b/crates/properties/src/inbound/toolset/test.rsindex b0a78866f3..6fd5e64eb9 100644--- a/crates/properties/src/inbound/toolset/test.rs+++ b/crates/properties/src/inbound/toolset/test.rs@@ -184,3 +184,83 @@ fn print_set_output_schema() {let schema = schemars::schema_for!(SetEntityPropertyResponse);println!("{}", serde_json::to_string_pretty(&schema).unwrap());}++mod entity_type_aliases {+ use super::super::get_entity_properties::{ToolEntityType, ToolPropertyTargetEntityType};+ use super::*;++ /// ListEntities and search report email threads as `email`, so the+ /// property tools accept that spelling alongside the canonical `thread`.+ #[test]+ fn set_entity_property_accepts_email_as_thread() {+ let tool: SetEntityProperty = serde_json::from_value(serde_json::json!({+ "entity_id": "01a09073-8a65-71f0-84b6-5ae47da19367",+ "entity_type": "email",+ "property_definition_id": "01a08770-8c51-7299-a31b-8569d2959337",+ "add_option_ids": ["01a09083-9c93-7732-9ffc-8f2b45f508bb"],+ }))+ .expect("email should deserialize as a thread target");++ assert!(matches!(+ tool.entity_type,+ ToolPropertyTargetEntityType::Thread+ ));+ assert_eq!(+ model_entity::EntityType::from(tool.entity_type),+ model_entity::EntityType::EmailThread+ );+ }++ #[test]+ fn bulk_set_entity_property_options_accepts_email_and_email_thread() {+ let tool: BulkSetEntityPropertyOptions = serde_json::from_value(serde_json::json!({+ "entities": [+ { "entity_id": "01a09073-8a65-71f0-84b6-5ae47da19367", "entity_type": "email" },+ { "entity_id": "01a0905a-3ab3-7c60-83fa-6987af8935fd", "entity_type": "email_thread" },+ { "entity_id": "01a08e26-fd59-7120-8a4b-594e492eb054", "entity_type": "thread" },+ ],+ "property_definition_id": "01a08770-8c51-7299-a31b-8569d2959337",+ "add_option_ids": ["01a09083-9c93-7732-9ffc-8f2b45f508bb"],+ }))+ .expect("email spellings should deserialize as thread targets");++ assert_eq!(tool.entities.len(), 3);+ assert!(+ tool.entities+ .iter()+ .all(|entity| matches!(entity.entity_type, ToolPropertyTargetEntityType::Thread))+ );+ }++ #[test]+ fn get_entity_properties_accepts_email_as_thread() {+ let tool: GetEntityProperties = serde_json::from_value(serde_json::json!({+ "entity_id": "01a09073-8a65-71f0-84b6-5ae47da19367",+ "entity_type": "email",+ }))+ .expect("email should deserialize as a thread target");++ assert!(matches!(+ tool.entity_type,+ ToolPropertyTargetEntityType::Thread+ ));+ }++ #[test]+ fn entity_ref_accepts_email_as_thread() {+ let tool: SetEntityProperty = serde_json::from_value(serde_json::json!({+ "entity_id": "01a09073-8a65-71f0-84b6-5ae47da19367",+ "entity_type": "document",+ "property_definition_id": "01a08770-8c51-7299-a31b-8569d2959337",+ "entity_ref": { "entityType": "email", "entityId": "01a0905a-3ab3-7c60-83fa-6987af8935fd" },+ }))+ .expect("email should deserialize as a thread reference");++ let entity_ref = tool.entity_ref.expect("entity_ref should be set");+ assert!(matches!(entity_ref.entity_type, ToolEntityType::Thread));+ assert_eq!(+ models_properties::EntityType::from(entity_ref.entity_type),+ models_properties::EntityType::Thread+ );+ }+}