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.1 KB
Raw Blame
import { createAction, Property } from "@activepieces/pieces-framework";
import { HttpMethod } from "@activepieces/pieces-common";
import { makeRequest } from "../common/client";
import { AgentXAuth } from "../common/auth";
import { AgentIdDropdown, ConversationIdDropdown } from "../common/dropdown";

type Message = {
  id: string;
  text?: string;
  [key: string]: unknown;
};

type ConversationDetail = {
  id: string;
  messages: Message[];
  [key: string]: unknown;
};

export const findMessage = createAction({
  auth: AgentXAuth,
  name: "find_message",
  classification: 'SEARCH',
  displayName: "Find Message",
  description:
    "Searches for a specific message by ID (or searches text within messages) inside a conversation.",
  audience: 'both',
  aiMetadata: { description: 'Retrieves messages from a specific AgentX conversation (by agent ID and conversation ID): leave the search term blank to return all messages in the conversation, or provide a search term to return only messages whose text contains it (case-insensitive). Use this to inspect or find content within a conversation. Read-only and idempotent.', idempotent: true },

  props: {
    agentId: AgentIdDropdown,
    conversationId: ConversationIdDropdown,
    searchTerm: Property.ShortText({
      displayName: "Search Term",
      description:
        "Text to search for in message content. Leave blank if searching by Message ID.",
      required: false,
    }),
  },

  async run({ auth, propsValue }) {
    const { agentId, conversationId, searchTerm } = propsValue;

    const conversation = await makeRequest(
      auth.secret_text,
      HttpMethod.GET,
      `/agents/${agentId}/conversations/${conversationId}`
    ) as ConversationDetail;

    const allMessages = conversation?.messages || [];

    if (!searchTerm) {
      return allMessages;
    }

    const lowerCaseSearchTerm = searchTerm.toLowerCase();
    return allMessages.filter(
      (m: any) =>
        (m.text && m.text.toLowerCase().includes(lowerCaseSearchTerm)) ||
        (m.content && m.content.toLowerCase().includes(lowerCaseSearchTerm))
    );
  },
});