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(ai): document rename tool (#4278)

Will Hutchinsonb33c412aceb0
diff --git a/js/app/packages/core/component/AI/component/tool/RenameDocument.tsx b/js/app/packages/core/component/AI/component/tool/RenameDocument.tsx
new file mode 100644
index 0000000000..19c4524bd6
--- /dev/null
+++ b/js/app/packages/core/component/AI/component/tool/RenameDocument.tsx
@@ -0,0 +1,26 @@
+import { ItemPreview } from '@core/component/ItemPreview';
+import PencilSimple from '@phosphor-icons/core/regular/pencil-simple.svg';
+import { Suspense } from 'solid-js';
+import { BaseTool } from './BaseTool';
+import { createToolRenderer } from './ToolRenderer';
+
+const handler = createToolRenderer({
+ name: 'RenameDocument',
+ render: (ctx) => (
+ <BaseTool icon={PencilSimple} renderContext={ctx.renderContext} type="call">
+ <div class="min-w-0 flex-1">
+ Rename{' '}
+ <Suspense>
+ <ItemPreview
+ class="inline-flex align-middle ring-0"
+ id={ctx.tool.data.documentId}
+ type="document"
+ />
+ </Suspense>{' '}
+ to <span class="text-ink">{ctx.tool.data.documentName}</span>
+ </div>
+ </BaseTool>
+ ),
+});
+
+export const renameDocumentHandler = handler;
diff --git a/js/app/packages/core/component/AI/component/tool/handler.tsx b/js/app/packages/core/component/AI/component/tool/handler.tsx
index b019ba4478..ac5226becb 100644
--- a/js/app/packages/core/component/AI/component/tool/handler.tsx
+++ b/js/app/packages/core/component/AI/component/tool/handler.tsx
@@ -31,6 +31,7 @@ import { readChatHandler } from './ReadChat';
import { readContentHandler } from './ReadContent';
import { readMetadataHandler } from './ReadMetadata';
import { readThreadHandler } from './ReadThread';
+import { renameDocumentHandler } from './RenameDocument';
import { contentSearchHandler, nameSearchHandler } from './Search';
import { sendEmailHandler } from './SendEmail';
import { subagentHandler } from './Subagent';
@@ -69,6 +70,7 @@ const toolHandlers: ToolHandlerMap<RenderContext> = {
ReadThread: readThreadHandler,
ReadContent: readContentHandler,
ReadMetadata: readMetadataHandler,
+ RenameDocument: renameDocumentHandler,
SendEmail: sendEmailHandler,
SetEntityProperty: setEntityPropertyHandler,
Subagent: subagentHandler,
diff --git a/js/app/packages/service-clients/service-cognition/generated/tools/schemas.ts b/js/app/packages/service-clients/service-cognition/generated/tools/schemas.ts
index eb4a62bd65..a54a9c958f 100644
--- a/js/app/packages/service-clients/service-cognition/generated/tools/schemas.ts
+++ b/js/app/packages/service-clients/service-cognition/generated/tools/schemas.ts
@@ -1887,6 +1887,17 @@ export const ReadMetadataResponse = z.object({
userAccessLevel: z.enum(['view', 'comment', 'edit', 'owner']),
});
+export const RenameDocument = z.object({
+ documentId: z.string().uuid(),
+ documentName: z.string(),
+});
+
+export const RenameDocumentResponse = z.object({
+ documentId: z.string().uuid(),
+ message: z.string(),
+ success: z.boolean(),
+});
+
export const SendEmail = z.object({
bcc: z
.array(
diff --git a/js/app/packages/service-clients/service-cognition/generated/tools/tool.ts b/js/app/packages/service-clients/service-cognition/generated/tools/tool.ts
index 9bb6f991af..3f3ea010b6 100644
--- a/js/app/packages/service-clients/service-cognition/generated/tools/tool.ts
+++ b/js/app/packages/service-clients/service-cognition/generated/tools/tool.ts
@@ -78,6 +78,10 @@ type ToolParserMap = {
response: types.ReadMetadataResponse;
};
ReadThread: { call: types.ReadThread; response: types.ReadResponse };
+ RenameDocument: {
+ call: types.RenameDocument;
+ response: types.RenameDocumentResponse;
+ };
SendEmail: { call: types.SendEmail; response: types.UserToolResponse };
SetEntityProperty: {
call: types.SetEntityProperty;
@@ -176,6 +180,10 @@ const toolParserMap = {
response: schemas.ReadMetadataResponse,
},
ReadThread: { call: schemas.ReadThread, response: schemas.ReadResponse },
+ RenameDocument: {
+ call: schemas.RenameDocument,
+ response: schemas.RenameDocumentResponse,
+ },
SendEmail: { call: schemas.SendEmail, response: schemas.UserToolResponse },
SetEntityProperty: {
call: schemas.SetEntityProperty,
@@ -273,6 +281,10 @@ type ToolDataMap = {
response: types.ReadMetadataResponse;
};
ReadThread: { call: types.ReadThread; response: types.ReadResponse };
+ RenameDocument: {
+ call: types.RenameDocument;
+ response: types.RenameDocumentResponse;
+ };
SendEmail: { call: types.SendEmail; response: types.UserToolResponse };
SetEntityProperty: {
call: types.SetEntityProperty;
diff --git a/js/app/packages/service-clients/service-cognition/generated/tools/types.ts b/js/app/packages/service-clients/service-cognition/generated/tools/types.ts
index ceb256a131..b004b00fa9 100644
--- a/js/app/packages/service-clients/service-cognition/generated/tools/types.ts
+++ b/js/app/packages/service-clients/service-cognition/generated/tools/types.ts
@@ -2438,6 +2438,36 @@ export interface ReadThread {
*/
messagesSince?: string | null;
}
+/**
+ * Rename a document. Requires edit access to the document.
+ */
+export interface RenameDocument {
+ /**
+ * The id of the document you want to rename.
+ */
+ documentId: string;
+ /**
+ * The new name for the document without the file extension.
+ */
+ documentName: string;
+}
+/**
+ * The rename document response.
+ */
+export interface RenameDocumentResponse {
+ /**
+ * The id of the renamed document.
+ */
+ documentId: string;
+ /**
+ * A human-readable result message.
+ */
+ message: string;
+ /**
+ * Whether the rename succeeded.
+ */
+ success: boolean;
+}
export interface SearchToolResponse {
results: UnifiedSearchResponseItem[];
}
diff --git a/rust/cloud-storage/documents/src/inbound/toolset/mod.rs b/rust/cloud-storage/documents/src/inbound/toolset/mod.rs
index 9d90e6f8d7..cb9c3ce2de 100644
--- a/rust/cloud-storage/documents/src/inbound/toolset/mod.rs
+++ b/rust/cloud-storage/documents/src/inbound/toolset/mod.rs
@@ -3,6 +3,7 @@
mod create_document;
mod read_content;
mod read_metadata;
+mod rename_document;
#[cfg(test)]
mod test;
@@ -13,6 +14,7 @@ use crate::{
domain::ports::create::DocumentCreationService,
inbound::toolset::{
create_document::CreateDocument, read_content::ReadContent, read_metadata::ReadMetadata,
+ rename_document::RenameDocument,
},
outbound::{
document_bytes_upload::ReqwestDocumentBytesUploader,
@@ -105,4 +107,5 @@ where
.add_tool::<ReadMetadata, DocumentToolContext<DSvc, ESvc>>()
.add_tool::<ReadContent, DocumentToolContext<DSvc, ESvc>>()
.add_tool::<CreateDocument, DocumentToolContext<DSvc, ESvc>>()
+ .add_tool::<RenameDocument, DocumentToolContext<DSvc, ESvc>>()
}
diff --git a/rust/cloud-storage/documents/src/inbound/toolset/rename_document.rs b/rust/cloud-storage/documents/src/inbound/toolset/rename_document.rs
new file mode 100644
index 0000000000..c307c8b108
--- /dev/null
+++ b/rust/cloud-storage/documents/src/inbound/toolset/rename_document.rs
@@ -0,0 +1,141 @@
+//! RenameDocument tool for renaming documents.
+
+use crate::domain::{
+ models::{DocumentError, EditDocumentServiceArgs},
+ ports::{DocumentService, create::DocumentCreationService},
+};
+use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};
+use async_trait::async_trait;
+use entity_access::domain::{models::EntityType, ports::EntityAccessService};
+use models_permissions::share_permission::access_level::EditAccessLevel;
+use schemars::JsonSchema;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+use super::DocumentToolContext;
+
+fn rename_document_args(document_name: String) -> EditDocumentServiceArgs {
+ EditDocumentServiceArgs {
+ document_name: Some(document_name),
+ project_id: None,
+ share_permission: None,
+ file_type: None,
+ }
+}
+
+fn deleted_document_error() -> ToolCallError {
+ let description = "cannot modify deleted document".to_string();
+
+ ToolCallError {
+ description: description.clone(),
+ internal_error: DocumentError::BadRequest(description).into(),
+ }
+}
+
+/// The rename document response.
+#[derive(Debug, Serialize, JsonSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct RenameDocumentResponse {
+ /// Whether the rename succeeded.
+ pub success: bool,
+ /// The id of the renamed document.
+ pub document_id: Uuid,
+ /// A human-readable result message.
+ pub message: String,
+}
+
+#[derive(Debug, Deserialize, JsonSchema, Clone, Default)]
+#[serde(rename_all = "camelCase")]
+#[schemars(
+ title = "RenameDocument",
+ description = "Rename a document. Requires edit access to the document."
+)]
+pub struct RenameDocument {
+ #[schemars(description = "The id of the document you want to rename.")]
+ pub document_id: Uuid,
+
+ #[schemars(description = "The new name for the document without the file extension.")]
+ pub document_name: String,
+}
+
+#[async_trait]
+impl<DSvc, ESvc> AsyncTool<DocumentToolContext<DSvc, ESvc>> for RenameDocument
+where
+ DSvc: DocumentService + DocumentCreationService,
+ ESvc: EntityAccessService,
+{
+ type Output = RenameDocumentResponse;
+
+ #[tracing::instrument(skip_all, fields(user_id=?request_context.user_id, document_id=?self.document_id), err)]
+ async fn call(
+ &self,
+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc>>,
+ request_context: RequestContext,
+ ) -> ToolResult<Self::Output> {
+ tracing::info!(params=?self, "Rename document");
+
+ let document_id = self.document_id.to_string();
+
+ let entity_access_receipt = service_context
+ .entity_access_service
+ .generate_entity_access_receipt::<EditAccessLevel>(
+ &request_context.user_id,
+ None,
+ &document_id,
+ EntityType::Document,
+ )
+ .await
+ .map_err(|e| ToolCallError {
+ description: "unable to get the entity access receipt".to_string(),
+ internal_error: e.into(),
+ })?;
+
+ // SAFETY: This is allowed because the user has edit access from the receipt above.
+ let document_context = service_context
+ .service
+ .internal_get_basic_document(&document_id)
+ .await
+ .map_err(|e| ToolCallError {
+ description: "unable to get the document context".to_string(),
+ internal_error: e.into(),
+ })?;
+
+ if document_context.deleted_at.is_some() {
+ return Err(deleted_document_error());
+ }
+
+ service_context
+ .service
+ .edit_document(
+ entity_access_receipt,
+ document_context,
+ rename_document_args(self.document_name.clone()),
+ )
+ .await
+ .map_err(|e| ToolCallError {
+ description: "unable to rename document".to_string(),
+ internal_error: e.into(),
+ })?;
+
+ Ok(RenameDocumentResponse {
+ success: true,
+ document_id: self.document_id,
+ message: "Document renamed successfully".to_string(),
+ })
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn test_rename_document_args_sets_only_document_name() {
+ let args = rename_document_args("Renamed document".to_string());
+
+ assert_eq!(args.document_name.as_deref(), Some("Renamed document"));
+ assert!(args.project_id.is_none());
+ assert!(args.share_permission.is_none());
+ assert!(args.file_type.is_none());
+ }
+}
diff --git a/rust/cloud-storage/documents/src/inbound/toolset/test.rs b/rust/cloud-storage/documents/src/inbound/toolset/test.rs
index 68fc412755..562401d63e 100644
--- a/rust/cloud-storage/documents/src/inbound/toolset/test.rs
+++ b/rust/cloud-storage/documents/src/inbound/toolset/test.rs
@@ -48,3 +48,19 @@ fn test_create_document_schema_validation() {
"Description should contain expected text"
);
}
+
+#[test]
+fn test_rename_document_schema_validation() {
+ let result = generate_validated_input_schema::<RenameDocument>();
+ assert!(result.is_ok(), "{:?}", result);
+
+ let validated = result.unwrap();
+ assert_eq!(
+ validated.name, "RenameDocument",
+ "Tool name should match the schemars title"
+ );
+ assert!(
+ validated.description.contains("Rename"),
+ "Description should contain expected text"
+ );
+}