AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { googleDocsAuth, createGoogleClient } from '../auth';
import {
createAction,
DynamicPropsValue,
Property,
} from '@activepieces/pieces-framework';
import { drive as googleDrive } from '@googleapis/drive';
import { docs as googleDocs } from '@googleapis/docs';
import { folderIdProp } from '../common/props';
import { driveQuery } from '../common/drive-query';
import { findDocumentActionOutputSchema } from '../output-schemas';
export const findDocumentAction = createAction({
auth: googleDocsAuth,
name: 'google-docs-find-document',
classification: 'SEARCH',
displayName: 'Find Document',
description: 'Find a document by name, optionally creating it.',
audience: 'human',
aiMetadata: {
description:
'Searches Google Drive for a Google Docs document whose name contains the given text, optionally scoped to a folder, and returns the first match. Use when an agent has a document name but not its ID and needs to resolve it. Idempotent only when used purely for lookup; enabling "Create a new document if not found" makes it create (and optionally move) a new document on each miss, which is a mutating side effect.',
idempotent: false,
},
propertyGroups: [
{
key: 'search',
display: 'section',
label: 'Search',
icon: 'filter',
props: ['name', 'folderId'],
},
{
key: 'notFound',
display: 'section',
label: 'If Not Found',
icon: 'file',
props: ['createIfNotFound', 'newDocumentProps'],
},
],
props: {
name: Property.ShortText({
displayName: 'Document Name',
description: 'Matches any document whose name contains this text.',
placeholder: 'Q3 report',
required: true,
}),
folderId: folderIdProp,
createIfNotFound: Property.Checkbox({
displayName: 'Create If Not Found',
defaultValue: false,
required: false,
}),
newDocumentProps: Property.DynamicProperties({
auth: googleDocsAuth,
displayName: 'New Document Properties',
required: false,
refreshers: ['createIfNotFound'],
props: async ({ auth, createIfNotFound }): Promise<DynamicPropsValue> => {
if (!auth) return {};
if (!createIfNotFound) return {};
const props: DynamicPropsValue = {};
if (createIfNotFound) {
props['content'] = Property.LongText({
displayName: 'Document Content',
placeholder: 'Agenda, decisions, action items',
required: true,
});
}
return props;
},
}),
},
outputSchema: findDocumentActionOutputSchema,
async run(context) {
const { name: documentName, folderId, createIfNotFound, newDocumentProps } = context.propsValue;
const newDocumentContent = newDocumentProps?.['content'] as string;
const authClient = await createGoogleClient(context.auth);
const drive = googleDrive({ version: 'v3', auth: authClient });
const docs = googleDocs({ version: 'v1', auth: authClient });
// Search for the document in Google Drive
const query: string[] = [
`name contains '${driveQuery.escapeLiteral(documentName)}'`,
`mimeType='application/vnd.google-apps.document'`,
'trashed=false',
];
if (folderId) query.push(`'${driveQuery.escapeLiteral(folderId)}' in parents`);
const response = await drive.files.list({
q: query.join(' and '),
supportsAllDrives: true,
fields: '*',
pageSize: 1,
includeItemsFromAllDrives: true,
corpora: 'allDrives',
});
const existingFile = response.data.files?.[0];
if (existingFile) {
return { found: true, file: existingFile };
}
// Create a new document if not found
if (!createIfNotFound) return { found: false, file: {} };
//creating new Document
const createdDoc = await docs.documents.create({ requestBody: { title: documentName } });
const documentId = createdDoc.data.documentId;
if (!documentId) throw new Error('Failed to create document');
// Insert content into the new document
if (newDocumentContent) {
// appending text
await docs.documents.batchUpdate({
documentId,
requestBody: {
requests: [{ insertText: { text: newDocumentContent, endOfSegmentLocation: {} } }],
},
});
}
// Move the document to the specified folder
if (folderId) {
const fileData = await drive.files.get({
fileId: documentId,
supportsAllDrives: true,
fields: 'id, parents',
});
await drive.files.update({
fileId: documentId,
fields: 'id, name, parents',
removeParents: fileData.data.parents?.join(','),
addParents: folderId,
supportsAllDrives: true,
});
}
// Fetch document details
const finalFile = await drive.files.get({
fileId: documentId,
supportsAllDrives: true,
fields: '*',
});
return { found: false, file: finalFile.data };
},
});