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,} from "@activepieces/pieces-common";
import dayjs from "dayjs";
import { makeRequest } from "../common/client";
import { HttpMethod } from "@activepieces/pieces-common";
import { AgentXAuth } from "../common/auth";

type Agent = {
  id: string;
  name?: string;
  createdAt: string;
};

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof AgentXAuth>,
  Record<string, never>
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth }) => {
    const agents = (await makeRequest(auth.secret_text, HttpMethod.GET, "/agents")) as Agent[];
    
    const sortedAgents = agents.sort((a, b) => 
      dayjs(b.createdAt).valueOf() - dayjs(a.createdAt).valueOf()
    );

    return sortedAgents.map((agent) => ({
      epochMilliSeconds: dayjs(agent.createdAt).valueOf(),
      data: agent,
    }));
  },
};

export const newAgent = createTrigger({
  auth: AgentXAuth,
  name: "new_agent",
  classification: 'READ',
  displayName: "New Agent",
  description: "Triggers when a new AgentX agent is created.",
  aiMetadata: {
    description: 'Fires when a new agent is created on the AgentX account, emitting the newly created agent. Polls periodically and only surfaces agents created after the trigger was enabled.',
  },
  props: {},
  sampleData: {
    _id: "agt_1234567890abcdef",
    name: "Customer Support Bot",
    created_at: "2025-09-08T10:00:00Z",
  },
  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);
  },
});