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 { Property, createAction } from '@activepieces/pieces-framework';
import { retuneAuth } from '../auth';
import { HttpMethod, httpClient } from '@activepieces/pieces-common';

export const askChatbot = createAction({
  auth: retuneAuth,
  name: 'ask_chatbot',
  classification: 'WRITE',
  displayName: 'Ask Chatbot',
  description: 'Sends a message to an existing thread with a chatbot.',
  audience: 'both',
  aiMetadata: { description: 'Sends a user message to an existing re:tune chatbot conversation thread and returns the bot\'s generated reply. Use this to interact with a configured re:tune chatbot; requires both a thread ID (a conversation context that must already exist) and the message text. Not idempotent — each call appends a turn to the thread and produces a fresh AI response.', idempotent: false },
  props: {
    thread: Property.Dropdown({
      auth: retuneAuth,
      displayName: 'Thread',
      description: 'The thread you want to send the message to.',
      required: true,
      refreshers: [],
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please connect your account first',
          };
        }
        const options = await httpClient.sendRequest({
          url: `https://retune.so/api/chat/${(auth).props.chatId}/threads`,
          method: HttpMethod.POST,
          headers: {
            'X-Workspace-API-Key': (auth).props.apiKey,
          },
          body: {},
        });

        return {
          options: options.body['threads'].map((item: any) => {
            return {
              label: item.name ?? item.id,
              value: item.id,
            };
          }),
        };
      },
    }),
    message: Property.ShortText({
      displayName: 'Message',
      description: 'The message you want to send.',
      required: true,
    }),
  },
  async run({ auth, propsValue }) {
    const { thread, message } = propsValue;

    const response = await httpClient.sendRequest({
      url: `https://retune.so/api/chat/${auth.props.chatId}/response`,
      method: HttpMethod.POST,
      headers: {
        'X-Workspace-API-Key': auth.props.apiKey,
      },
      body: {
        threadId: thread,
        input: message,
      },
    });

    return (response.body as any).response.value;
  },
});