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

1.9 KB
Raw Blame
import { createTrigger, TriggerStrategy, AppConnectionValueForAuthProperty } from '@activepieces/pieces-framework';
import { DedupeStrategy, Polling, pollingHelper, HttpMethod } from '@activepieces/pieces-common';
import { instabaseAuth } from '../auth';
import { makeInstabaseApiCall } from '../common';

type Conversation = {
  id: string;
  name: string;
  description?: string;
};

const polling: Polling<AppConnectionValueForAuthProperty<typeof instabaseAuth>, Record<string, never>> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth }) => {
    const response = await makeInstabaseApiCall<{ conversations: Conversation[] }>(
      auth,
      '/v2/conversations',
      HttpMethod.GET
    );

    const conversations = response.conversations || [];

    const items = conversations.map((conversation) => ({
      epochMilliSeconds: Date.now(),
      data: conversation,
    }));

    return items;
  },
};

export const newConversationTrigger = createTrigger({
  auth: instabaseAuth,
  name: 'new_conversation',
  displayName: 'New Conversation',
  description: 'Triggers when a new conversation is created',
  aiMetadata: {
    description: 'Fires when a new conversation is created in the connected Instabase AI Hub account, emitting the conversation. Polls the conversations list on an interval, so newly created conversations surface on the next poll.',
  },
  props: {},
  type: TriggerStrategy.POLLING,
  sampleData: {
    id: 'conv_123',
    name: 'Sample Conversation',
    description: 'A sample conversation for testing',
  },
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },
  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});