Skip to content

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

3.2 KB
Raw Blame
import { Property, createAction, OAuth2PropertyValue } from '@activepieces/pieces-framework';
import { getGraphBaseUrl } from '../common/microsoft-cloud';
import { microsoftToDoAuth } from '../auth';
import { getTaskListsDropdown } from '../common';
import { Client, PageCollection } from '@microsoft/microsoft-graph-client';
import { TodoTask } from '@microsoft/microsoft-graph-types';
import { listTasksOutputSchema } from '../output-schemas';

export const listTasksAction = createAction({
	auth: microsoftToDoAuth,
	name: 'list_tasks',
	outputSchema: listTasksOutputSchema,
	classification: 'SEARCH',
	displayName: 'List Tasks',
	description: 'Returns a list of all tasks in a specific list.',
	audience: 'human',
	aiMetadata: { description: 'List the tasks in a given Microsoft To Do task list, paging through all results. Optionally narrow by a title substring and/or a status filter (not started, in progress, completed, waiting on others, deferred); with no filters it returns every task in the list. Use to enumerate or browse a list\'s contents. Read-only and idempotent. Requires a task list id.', idempotent: true },
	props: {
		task_list_id: Property.Dropdown({
			auth: microsoftToDoAuth,
			displayName: 'Task List',
			description: 'The task list to get tasks from.',
			required: true,
			refreshers: [],
			options: async ({ auth }) => {
				if (!(auth as OAuth2PropertyValue)?.access_token) {
					return {
						disabled: true,
						placeholder: 'Connect your account first',
						options: [],
					};
				}
				return await getTaskListsDropdown(auth as OAuth2PropertyValue);
			},
		}),
		search: Property.ShortText({
			displayName: 'Search',
			description: 'Search for tasks by title.',
			required: false,
		}),
		status: Property.StaticDropdown({
			displayName: 'Status',
			description: 'Filter tasks by status.',
			required: false,
			options: {
				options: [
					{ label: 'Not Started', value: 'notStarted' },
					{ label: 'In Progress', value: 'inProgress' },
					{ label: 'Completed', value: 'completed' },
					{ label: 'Waiting On Others', value: 'waitingOnOthers' },
					{ label: 'Deferred', value: 'deferred' },
				],
			},
		}),
	},
	async run(context) {
		const { auth, propsValue } = context;
		const { task_list_id, status, search } = propsValue;

		const cloud = (auth as OAuth2PropertyValue).props?.['cloud'] as string | undefined;
		const client = Client.initWithMiddleware({
			authProvider: {
				getAccessToken: () => Promise.resolve(auth.access_token),
			},
			baseUrl: getGraphBaseUrl(cloud),
		});

		const result: TodoTask[] = [];
		let apiCall = client.api(`/me/todo/lists/${task_list_id}/tasks`);
		
		const filters: string[] = [];
		if (status) {
			filters.push(`status eq '${status}'`);
		}
		if (search) {
			const escapedSearch = search.replace(/'/g, "''");
			filters.push(`contains(title, '${escapedSearch}')`);
		}

		if (filters.length > 0) {
			apiCall = apiCall.filter(filters.join(' and '));
		}

		let response: PageCollection = await apiCall.get();

		while (response.value.length > 0) {
			for (const task of response.value as TodoTask[]) {
				result.push(task);
			}
			if (response['@odata.nextLink']) {
				response = await client.api(response['@odata.nextLink']).get();
			} else {
				break;
			}
		}

		return result;
	},
});