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

3.8 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { peekshotAuth } from '../auth';
import { projectId } from '../common/props';
import {
  CreateScreenshotResponse,
  GetScreenshotResponse,
} from '../common/types';

export const captureScreenshot = createAction({
  auth: peekshotAuth,
  name: 'captureScreenshot',
  classification: 'WRITE',
  displayName: 'Capture Screenshot',
  description: 'Captures Screenshot of a URL.',
  audience: 'both',
  aiMetadata: {
    description:
      'Captures a screenshot of a given web page URL via PeekShot and waits (polling up to ~5 minutes) for the rendered image to be ready. Use when an agent needs to render a live web page to an image; supports optional viewport width/height, PNG/JPEG output, full-page capture, and injecting custom CSS/JS before the shot. Requires a PeekShot project id and a target URL. Not idempotent: each call submits a new capture request.',
    idempotent: false,
  },
  props: {
    projectId: projectId,
    url: Property.ShortText({ displayName: 'Target URL', required: true }),
    width: Property.ShortText({
      displayName: 'Custom Screenshot Width',
      required: false,
    }),
    height: Property.ShortText({
      displayName: 'Custom Screenshot Height',
      required: false,
    }),
    file_type: Property.StaticDropdown({
      displayName: 'Image Format',
      description: 'Output format for the screenshot.',
      required: false,
      options: {
        disabled: false,
        options: [
          { label: 'PNG', value: 'png' },
          { label: 'JPEG', value: 'jpeg' },
        ],
      },
    }),
    inject_css: Property.LongText({
      displayName: 'Custom CSS',
      description: 'Custom CSS to apply.',
      required: false,
    }),
    inject_js: Property.LongText({
      displayName: 'Custom JavaScript',
      description: 'Custom JavaScript to apply.',
      required: false,
    }),
    full_page: Property.Checkbox({
      displayName: 'Full Page?',
      description: 'To capture the entire page.',
      required: false,
    }),
  },
  async run({ propsValue, auth }) {
    const {
      projectId,
      url,
      width,
      height,
      file_type,
      full_page,
      inject_css,
      inject_js,
    } = propsValue;

    const res = await httpClient.sendRequest<CreateScreenshotResponse>({
      method: HttpMethod.POST,
      url: 'https://api.peekshot.com/api/v1/screenshots',
      headers: {
        'x-api-key': auth.secret_text as string,
        'Content-Type': 'application/json',
      },
      body: {
        project_id: projectId.toString(),
        url,
        width,
        height,
        inject_css,
        inject_js,
        file_type,
        full_page: full_page ? 'true' : 'false',
      },
    });

    // Handle error in initial response
    if (!res.body || !res.body.data || !res.body.data.requestId) {
      throw new Error('Failed to initiate screenshot request');
    }

    const requestId = res.body.data.requestId;
    let status = res.body.status;
    const timeoutAt = Date.now() + 5 * 60 * 1000;

    while (status !== 'COMPLETE' && Date.now() < timeoutAt) {
      await new Promise((resolve) => setTimeout(resolve, 5000)); // wait 5 seconds

      const pollRes = await httpClient.sendRequest<GetScreenshotResponse>({
        method: HttpMethod.GET,
        url: `https://api.peekshot.com/api/v1/screenshots/${requestId}`,
        headers: {
          'x-api-key': auth.secret_text,
          'Content-Type': 'application/json',
        },
      });

      status = pollRes.body?.data?.status;

      if (status === 'COMPLETE') {
        return pollRes.body; // Screenshot is ready
      }
    }

    throw new Error('Screenshot generation timed out or failed.');
  },
});