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.6 KB
Raw Blame
import { ActionContext, createAction, Property } from '@activepieces/pieces-framework';
import { HttpRequest, HttpMethod, AuthenticationType, httpClient } from '@activepieces/pieces-common';
import { googleCalendarCommon, googleCalendarAuth, getAccessToken, GoogleCalendarAuthValue } from '../common';
import { getCalendars } from '../common/helper';
import dayjs from 'dayjs';


import { freeBusyActionOutputSchema } from '../output-schemas';
interface FreeBusyResponse {
  kind: 'calendar#freeBusy';
  timeMin: string;
  timeMax: string;
  calendars: {
    [calendarId: string]: {
      busy: {
        start: string;
        end: string;
      }[];
      errors?: {
        domain: string;
        reason: string;
      }[];
    };
  };
}

export const findFreeBusyProps = {
  calendar_ids: Property.MultiSelectDropdown({
    auth: googleCalendarAuth,
    displayName: 'Calendars',
    description: 'Calendars to check.',
    required: true,
    refreshers: [],
    options: async ({ auth }) => {
      if (!auth) {
        return {
          disabled: true,
          placeholder: 'Connect your Google account first',
          options: [],
        };
      }
      const authProp = auth as GoogleCalendarAuthValue;
      const calendars = await getCalendars(authProp);
      return {
        disabled: false,
        options: calendars.map((calendar) => {
          return {
            label: calendar.summary,
            value: calendar.id,
          };
        }),
      };
    },
  }),
  start_date: Property.DateTime({
    displayName: 'Start Time',
    description: 'The start of the time range to check.',
    required: true,
    width: 'half',
  }),
  end_date: Property.DateTime({
    displayName: 'End Time',
    description: 'The end of the time range to check.',
    required: true,
    width: 'half',
  }),
};

export async function runFindFreeBusy(
  context: ActionContext<typeof googleCalendarAuth, typeof findFreeBusyProps>
) {
  const { calendar_ids, start_date, end_date } = context.propsValue;
  const access_token = await getAccessToken(context.auth);

  const requestBody = {
    timeMin: dayjs(start_date).toISOString(),
    timeMax: dayjs(end_date).toISOString(),
    items: calendar_ids.map((id) => ({ id })),
  };

  const request: HttpRequest = {
    method: HttpMethod.POST,
    url: `${googleCalendarCommon.baseUrl}/freeBusy`,
    body: requestBody,
    authentication: {
      type: AuthenticationType.BEARER_TOKEN,
      token: access_token,
    },
  };

  const response = await httpClient.sendRequest<FreeBusyResponse>(request);

  return response.body;
}

export const findFreeBusy = createAction({
  auth: googleCalendarAuth,
  name: 'google_calendar_find_busy_free_periods',
  classification: 'SEARCH',
  displayName: 'Find Busy/Free Periods in Calendar',
  description: 'Finds busy periods across calendars in a time range.',
  audience: 'human',
  aiMetadata: { description: 'Queries one or more calendars for their busy time blocks within a given start/end window, without exposing event details. Use to check availability or find open slots before scheduling a meeting. Requires the calendars to check and a time range. Read-only and idempotent.', idempotent: true },
  propertyGroups: [
    {
      key: 'calendars',
      display: 'section',
      label: 'Calendars',
      icon: 'calendar',
      props: ['calendar_ids'],
    },
    {
      key: 'time_range',
      display: 'section',
      label: 'Time range',
      icon: 'calendar',
      props: ['start_date', 'end_date'],
    },
  ],
  props: findFreeBusyProps,
  outputSchema: freeBusyActionOutputSchema,
  run: runFindFreeBusy,
});