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

6.8 KB
Raw Blame
import {
  createTrigger,
  TriggerStrategy,
  Property,
  PiecePropValueSchema,
  StaticPropsValue,
  AppConnectionValueForAuthProperty,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';

import { frontAuth } from '../common/auth';
import { makeRequest } from '../common/client';

import { conversationIdDropdown } from '../common/dropdown';

const props = {
  conversation_id: conversationIdDropdown,
  desired_state: Property.StaticDropdown({
    displayName: 'Desired State',
    description:
      'The state to trigger on (e.g., open, archived, deleted, assigned, etc.).',
    required: true,
    options: {
      options: [
        { label: 'Open', value: 'open' },
        { label: 'Archived', value: 'archived' },
        { label: 'Deleted', value: 'deleted' },
        { label: 'Assigned', value: 'assigned' },
        { label: 'Unassigned', value: 'unassigned' },
      ],
    },
  }),
};
const polling: Polling<AppConnectionValueForAuthProperty<typeof frontAuth>, StaticPropsValue<typeof props>> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, propsValue, lastFetchEpochMS }) => {
    const { conversation_id, desired_state } = propsValue;
    const conv = await makeRequest(
      auth,
      HttpMethod.GET,
      `/conversations/${conversation_id}`
    );
    const stateChangedAt = conv.updated_at
      ? Math.floor(Number(conv.updated_at) * 1000)
      : null;

    // Only emit if the state matches and it's new since last poll
    if (
      conv.status === desired_state &&
      stateChangedAt !== null &&
      (!lastFetchEpochMS || stateChangedAt > lastFetchEpochMS)
    ) {
      return [
        {
          epochMilliSeconds: stateChangedAt,
          data: conv,
        },
      ];
    }
    return [];
  },
};

export const newConversationStateChange = createTrigger({
  auth: frontAuth,
  name: 'newConversationStateChange',
  displayName: 'New Conversation State Change',
  description: 'Triggers when a conversation changes to a specified state.',
  aiMetadata: {
    description:
      'Fires when the selected Front conversation transitions into a chosen status (open, archived, deleted, assigned, or unassigned). Represents a single conversation reaching that state and emits the full conversation object, letting an agent react to lifecycle changes such as a ticket being resolved, reopened, or routed to a teammate.',
  },
  props,
  sampleData: {
    _links: {
      self: 'https://example.api.frontapp.com/conversations/cnv_1jm05qco',
      related: {
        events:
          'https://example.api.frontapp.com/conversations/cnv_1jm05qco/events',
        followers:
          'https://example.api.frontapp.com/conversations/cnv_1jm05qco/followers',
        messages:
          'https://example.api.frontapp.com/conversations/cnv_1jm05qco/messages',
        comments:
          'https://example.api.frontapp.com/conversations/cnv_1jm05qco/comments',
        inboxes:
          'https://example.api.frontapp.com/conversations/cnv_1jm05qco/inboxes',
        last_message:
          'https://example.api.frontapp.com/messages/msg_326rt3vs?referer=conversation',
      },
    },
    id: 'cnv_1jm05qco',
    subject: 'Re: test',
    status: 'assigned',
    status_id: 'sts_695a3s',
    status_category: 'open',
    ticket_ids: ['SU-2'],
    assignee: {
      _links: {
        self: 'https://example.api.frontapp.com/teammates/tea_mfoko',
        related: {
          inboxes:
            'https://example.api.frontapp.com/teammates/tea_mfoko/inboxes',
          conversations:
            'https://example.api.frontapp.com/teammates/tea_mfoko/conversations',
        },
      },
      id: 'tea_mfoko',
      email: '[email protected]',
      username: 'jon',
      first_name: 'jon',
      last_name: 'deo',
      is_admin: true,
      is_available: true,
      is_blocked: false,
      type: 'user',
      custom_fields: {},
    },
    recipient: {
      _links: {
        related: {
          contact: 'https://example.api.frontapp.com/contacts/crd_4x1iwyw',
        },
      },
      name: 'jon deo',
      handle: '[email protected]',
      role: 'to',
    },
    tags: [
      {
        _links: {
          self: 'https://example.api.frontapp.com/tags/tag_6958vc',
          related: {
            conversations:
              'https://example.api.frontapp.com/tags/tag_6958vc/conversations',
            owner: 'https://example.api.frontapp.com/teammates/tea_mfoko',
            parent_tag: null,
            children: null,
          },
        },
        id: 'tag_6958vc',
        name: 'Inbox',
        highlight: null,
        description: null,
        is_private: true,
        is_visible_in_conversation_lists: false,
        updated_at: 1758518633.403,
        created_at: 1758518633.403,
      },
      {
        _links: {
          self: 'https://example.api.frontapp.com/tags/tag_695n8o',
          related: {
            conversations:
              'https://example.api.frontapp.com/tags/tag_695n8o/conversations',
            owner: 'https://example.api.frontapp.com/teams/tim_8t9ew',
            parent_tag: null,
            children: null,
          },
        },
        id: 'tag_695n8o',
        name: 'YELLOW_STAR',
        highlight: null,
        description: null,
        is_private: false,
        is_visible_in_conversation_lists: false,
        updated_at: 1758532572.491,
        created_at: 1758531672.777,
      },
      {
        _links: {
          self: 'https://example.api.frontapp.com/tags/tag_695n6w',
          related: {
            conversations:
              'https://example.api.frontapp.com/tags/tag_695n6w/conversations',
            owner: 'https://example.api.frontapp.com/teams/tim_8t9ew',
            parent_tag: null,
            children: null,
          },
        },
        id: 'tag_695n6w',
        name: 'CHAT',
        highlight: null,
        description: null,
        is_private: false,
        is_visible_in_conversation_lists: false,
        updated_at: 1758532572.5,
        created_at: 1758531672.726,
      },
    ],
    links: [
      {
        _links: {
          self: 'https://example.api.frontapp.com/links/top_isetpk',
        },
        id: 'top_isetpk',
        name: 'test update link',
        type: 'web',
        external_url: 'https://example.com/',
        custom_fields: {},
      },
    ],
    custom_fields: {},
    created_at: 1758531757.285,
    waiting_since: 1758534767,
    is_private: false,
    scheduled_reminders: [],
    metadata: {},
  },
  type: TriggerStrategy.POLLING,

  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },

  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },

  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});