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

5.4 KB
Raw Blame
import {
  Property,
  createAction,
} from '@activepieces/pieces-framework';
import {
  httpClient,
  HttpMethod,
  HttpRequest,
} from '@activepieces/pieces-common';
import { drupalAuth } from '../auth';

export const drupalCallServiceAction = createAction({
  auth: drupalAuth,
  name: 'drupal-call-service',
  classification: 'WRITE',
  displayName: 'Call Service',
  description: 'Call a service on the Drupal site',
  audience: 'both',
  aiMetadata: { description: 'Executes a named server-side service (e.g. an ECA/orchestration action) exposed by the Drupal site, passing a dynamic configuration object whose fields are defined by the chosen service. Use to invoke custom Drupal-side logic that has no dedicated action here. The service must be discoverable via the site\'s orchestration endpoint; because it runs arbitrary server logic, treat each call as potentially side-effecting and not idempotent.', idempotent: false },
  props: {
    service: Property.Dropdown({
      displayName: 'Service',
      description: 'The service to call.',
      required: true,
      refreshers: [],
      auth: drupalAuth,
      options: async ({ auth }) => {
        if (!auth) {
          return {
            disabled: true,
            options: [],
            placeholder: 'Please authenticate first.',
          };
        }
        const { website_url, username, password } = auth.props;


        try {
          const response = await httpClient.sendRequest<DrupalService[]>({
            method: HttpMethod.GET,
            url: website_url + `/orchestration/services`,
            headers: {
              'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
              'Accept': 'application/vnd.api+json',
            },
          });
          if (response.status === 200) {
            return {
              disabled: false,
              options: response.body.map((service) => {
                return {
                  label: service.label,
                  description: service.description,
                  value: service,
                };
              }),
            };
          }
        } catch (e: any) {
          console.debug('Service error', e);
        }
        return {
          disabled: true,
          options: [],
          placeholder: 'Error processing services',
        };
      },
    }),
    config: Property.DynamicProperties({
      displayName: 'Service configuration',
      refreshers: ['service'],
      required: true,
      auth: drupalAuth,
      props: async ({ service }) => {
        const fields: Record<string, any> = {};
        const items = (service as { config: DrupalServiceConfig[] }).config;
        items.forEach((config: any) => {
          if (config.type === 'boolean') {
            fields[config.key] = Property.Checkbox({
              displayName: config.label,
              description: config.description,
              required: config.required,
              defaultValue: config.default_value,
            });
          } else if (config.type === 'integer' || config.type === 'float') {
            fields[config.key] = Property.Number({
              displayName: config.label,
              description: config.description,
              required: config.required,
              defaultValue: config.default_value,
            });
          } else if (config.type === 'timestamp' || config.type === 'datetime_iso8601') {
            fields[config.key] = Property.DateTime({
              displayName: config.label,
              description: config.description,
              required: config.required,
              defaultValue: config.default_value,
            });
          } else if (config.options.length > 0) {
            fields[config.key] = Property.StaticDropdown({
              displayName: config.label,
              description: config.description,
              required: config.required,
              defaultValue: config.default_value,
              options: {
                options: config.options.map((option: any) => ({
                  label: option.name,
                  value: option.key,
                }))
              },
            });
          } else {

            fields[config.key] = Property.ShortText({
              displayName: config.label,
              description: config.description,
              required: config.required,
              defaultValue: config.default_value,
            });
          }
        });
        return fields;
      },
    }),
  },
  async run({ auth, propsValue }) {
    const { website_url, username, password } = auth.props;
    const request: HttpRequest = {
      method: HttpMethod.POST,
      url: website_url + `/orchestration/service/execute`,
      body: {
        id: propsValue.service.id,
        config: propsValue.config,
      },
      headers: {
        'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
        'Accept': 'application/vnd.api+json',
      },
    };

    const result = await httpClient.sendRequest<DrupalService>(request);

    if (result.status === 200 || result.status === 202) {
      return result.body;
    } else {
      return result;
    }
  },
});

interface DrupalService {
  id: string;
  label: string;
  description: string;
  config: DrupalServiceConfig[];
}

interface DrupalServiceConfig {
  key: string;
  label: string;
  description: string;
  required: boolean;
  type: string;
  default_value: string;
  options: string[];
  editable: boolean;
}