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 {
  createTrigger,
  TriggerStrategy,
  PiecePropValueSchema,
  StaticPropsValue,
  AppConnectionValueForAuthProperty,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import dayjs from 'dayjs';
import { structureIdDropdown } from '../common/props';
import { griptapeAuth } from '../common/auth';
import { makeRequest } from '../common/client';

const props = {
  structure_id: structureIdDropdown,
};

const polling: Polling<AppConnectionValueForAuthProperty<typeof griptapeAuth>, StaticPropsValue<typeof props>> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, propsValue, lastFetchEpochMS }) => {
    const { structure_id } = propsValue;

    try {
      const response = await makeRequest(
        auth.secret_text,
        HttpMethod.GET,
        `/structures/${structure_id}/runs?status=SUCCEEDED&page_size=100`
      );

      const items = response.structure_runs || [];

      const succeededRuns = items.filter(
        (run: any) =>
          run.status === 'SUCCEEDED' &&
          dayjs(run.completed_at || run.updated_at).valueOf() > lastFetchEpochMS
      );

      return succeededRuns.map((run: any) => ({
        epochMilliSeconds: dayjs(run.completed_at || run.updated_at).valueOf(),
        data: run,
      }));
    } catch (error) {
      console.error('Error fetching assistant runs:', error);
      return [];
    }
  },
};

export const structureRunSucceeds = createTrigger({
  auth: griptapeAuth,
  name: 'structureRunSucceeds',
  displayName: 'Structure Run Succeeds',
  description: '',
  aiMetadata: {
    description:
      'Fires when a run for the selected Griptape Cloud structure completes with status SUCCEEDED. Polls for newly succeeded runs; use when you only want to react to successful structure runs and ignore failures or cancellations.',
  },
  props,
  sampleData: {},
  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);
  },
});