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.2 KB
Raw Blame
import { createAction } from '@activepieces/pieces-framework';
import { apifyAuth } from '../..';
import {
  createApifyClient,
  createBuildProperty,
  createRunOptions,
  handleRun,
  createTaskIdProperty,
  createTaskInputProperty,
  createMemoryProperty,
  createTimeoutProperty,
  createWaitForFinishProperty,
  RunType
} from '../common';
import { runTaskActionOutputSchema } from '../output-schemas';

export const runTask = createAction({
  name: 'runTask',
  classification: 'WRITE',
  auth: apifyAuth,
  displayName: 'Run Task',
  description: 'Runs a saved Actor task and, if you wait, returns its results.',
  audience: 'human',
  outputSchema: runTaskActionOutputSchema,
  aiMetadata: { description: 'Runs a saved Apify Actor task (an Actor pre-configured with stored input) by task ID, optionally overriding the input body and build/memory/timeout. Use this instead of Run Actor when the user has an existing configured task to execute; set waitForFinish to block until completion and retrieve dataset items. Not idempotent — each call launches a new task run.', idempotent: false },
  props: {
    taskid: createTaskIdProperty(),
    input: createTaskInputProperty(),
    build: createBuildProperty(),
    memory: createMemoryProperty(RunType.TASK),
    timeout: createTimeoutProperty(RunType.TASK),
    waitForFinish: createWaitForFinishProperty()
  },
  propertyGroups: [
    { key: 'task', display: 'section', label: 'What to Run', icon: 'send', props: ['taskid'] },
    { key: 'input', display: 'section', label: 'Input Changes', icon: 'code', props: ['input'] },
    { key: 'result', display: 'section', label: 'Results', icon: 'inbox', props: ['waitForFinish'] },
  ],
  async run(context) {
    const apifyToken = context.auth.props.apikey;
    const { input, taskid, timeout, build, memory, waitForFinish } = context.propsValue;
    const body = input['body'];

    const client = createApifyClient(apifyToken);

    const runOptions = createRunOptions({ timeout, memory, build });
    const resourceClient = client.task(taskid);

    return handleRun({
      resourceClient,
      body,
      runOptions,
      waitForFinish: waitForFinish || false,
      client
    });
  },
});