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

2.3 KB
Raw Blame
import { HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { ticktickAuth } from '../auth';
import { tickTickApiCall } from '../common/client';
import { projectId } from '../common/props';
import { findTaskOutputSchema } from '../output-schemas';

export const findTaskAction = createAction({
	auth: ticktickAuth,
	name: 'find_task',
	outputSchema: findTaskOutputSchema,
	classification: 'SEARCH',
	displayName: 'Find Task',
	description: 'Finds tasks in a specific project by their title.',
	audience: 'human',
	aiMetadata: {
		description:
			"Searches the tasks in a TickTick list (project) for ones whose title matches a given string, with a match-type option for case-insensitive 'contains' (substring) or 'exact' matching. Use to resolve a task by name into its ID before getting, updating, completing, or deleting it. Requires the list ID and a title. Read-only and idempotent.",
		idempotent: true,
	},
	props: {
		projectId: projectId({
			displayName: 'List',
			description: 'The list to search within.',
			required: true,
		}),
		title: Property.ShortText({
			displayName: 'Task Title',
			required: true,
		}),
		matchType: Property.StaticDropdown({
			displayName: 'Match Type',
			description: 'Select how the title should be matched.',
			required: true,
			options: {
				options: [
					{ label: 'Contains (case-insensitive)', value: 'contains' },
					{ label: 'Exact Match (case-insensitive)', value: 'exact' },
				],
			},
			defaultValue: 'contains',
		}),
	},
	async run(context) {
		const { projectId, title, matchType } = context.propsValue;

		if (!projectId || !title) {
			return { found: false, result: [] };
		}

		const response = await tickTickApiCall<{
			tasks: { id: string; title: string }[];
		}>({
			accessToken: context.auth.access_token,
			method: HttpMethod.GET,
			resourceUri: `/project/${projectId}/data`,
		});

		const foundTasks = [];

		for (const task of response.tasks) {
			if (matchType === 'exact') {
				if (task.title.toLowerCase() === title.toLowerCase()) {
					foundTasks.push(task);
				}
			} else {
				// Default to 'contains'
				if (task.title.toLowerCase().includes(title.toLowerCase())) {
					foundTasks.push(task);
				}
			}
		}
		return {
			found: foundTasks.length > 0,
			result: foundTasks,
		};
	},
});