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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
feat(google-document-ai): add Google Document AI piece (#16251)
Fabio3f1b3e14aa76
diff --git a/bun.lock b/bun.lockindex 425ab03c42b..773dd4b4c8b 100644--- a/bun.lock+++ b/bun.lock@@ -3883,6 +3883,18 @@"vitest": "3.2.6",},},+ "packages/pieces/community/google-document-ai": {+ "name": "@activepieces/piece-google-document-ai",+ "version": "0.0.1",+ "dependencies": {+ "@activepieces/pieces-common": "workspace:*",+ "@activepieces/pieces-framework": "workspace:*",+ },+ "devDependencies": {+ "tslib": "2.6.2",+ "vitest": "3.2.6",+ },+ },"packages/pieces/community/google-drive": {"name": "@activepieces/piece-google-drive","version": "0.12.0",@@ -11876,6 +11888,7 @@"@activepieces/piece-google-docs": ["@activepieces/piece-google-docs@workspace:packages/pieces/community/google-docs"],+ "@activepieces/piece-google-document-ai": ["@activepieces/piece-google-document-ai@workspace:packages/pieces/community/google-document-ai"],"@activepieces/piece-google-drive": ["@activepieces/piece-google-drive@workspace:packages/pieces/community/google-drive"],"@activepieces/piece-google-forms": ["@activepieces/piece-google-forms@workspace:packages/pieces/community/google-forms"],diff --git a/packages/pieces/community/google-document-ai/.eslintrc.json b/packages/pieces/community/google-document-ai/.eslintrc.jsonnew file mode 100644index 00000000000..9ee1a0770e8--- /dev/null+++ b/packages/pieces/community/google-document-ai/.eslintrc.json@@ -0,0 +1,26 @@+{+ "extends": ["../../../../.eslintrc.json"],+ "ignorePatterns": ["!**/*"],+ "overrides": [+ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], "rules": {} },+ {+ "files": ["*.ts", "*.tsx"],+ "rules": {+ "no-restricted-imports": [+ "error",+ {+ "patterns": [+ "lodash",+ "lodash/*",+ "@activepieces/core-*",+ "@activepieces/server*",+ "@activepieces/engine",+ "@activepieces/shared"+ ]+ }+ ]+ }+ },+ { "files": ["*.js", "*.jsx"], "rules": {} }+ ]+}diff --git a/packages/pieces/community/google-document-ai/README.md b/packages/pieces/community/google-document-ai/README.mdnew file mode 100644index 00000000000..af55b1fcd6f--- /dev/null+++ b/packages/pieces/community/google-document-ai/README.md@@ -0,0 +1,31 @@+# Google Document AI++Extract text, entities, form fields and tables from documents (PDF, images, Office files) with+[Google Document AI](https://cloud.google.com/document-ai) processors: OCR, Form Parser,+Invoice and Expense parsers, Layout Parser and Custom Extractors.++## Connection++Both connection types use your own Google Cloud project. Every action accepts either one.++- **Service Account (Recommended)**: paste the JSON key file of a service account with the+ **Document AI API User** role (`roles/documentai.apiUser`) and the processors' location. The+ connection is checked on creation by listing the processors of the project and location.+- **Google Account (OAuth2)**: bring your own OAuth client (scope+ `https://www.googleapis.com/auth/cloud-platform`), then fill in the project ID and the location.++Prerequisites: the **Cloud Document AI API** and **billing** enabled on the project (Document AI+charges per page), and at least one processor created in the console. Document AI is regional:+requests go to `https://{location}-documentai.googleapis.com`, so the location of the connection+(`us`, `eu`, ...) must match where the processors live.++## Actions++- **Process Document**: online processing of one file from the flow or a `gs://` Cloud Storage+ URI. Online processing accepts up to 15 pages (30 with Imageless Mode, on by default); use+ **Pages** (for example `1,3-5`) to stay under the limit. The output has the same shape for every+ processor type: `text`, `mimeType`, `pageCount`, `languages`, `entities`, `formFields` and+ `tables`. Turn on **Include Full Document** to also get the raw Document AI `document`.+- **Custom API Call**: any Document AI REST endpoint on the connection's regional host (paths+ include `/v1`), for example `batchProcess` for longer documents, operations and processor+ management.diff --git a/packages/pieces/community/google-document-ai/package.json b/packages/pieces/community/google-document-ai/package.jsonnew file mode 100644index 00000000000..656f300afc0--- /dev/null+++ b/packages/pieces/community/google-document-ai/package.json@@ -0,0 +1,20 @@+{+ "name": "@activepieces/piece-google-document-ai",+ "version": "0.0.1",+ "main": "./dist/src/index.js",+ "types": "./dist/src/index.d.ts",+ "scripts": {+ "build": "tsc -p tsconfig.lib.json && cp package.json dist/",+ "bundle": "node ../../../../dist/packages/cli/src/index.js pieces bundle",+ "lint": "eslint 'src/**/*.ts'",+ "test": "vitest run"+ },+ "dependencies": {+ "@activepieces/pieces-common": "workspace:*",+ "@activepieces/pieces-framework": "workspace:*"+ },+ "devDependencies": {+ "tslib": "2.6.2",+ "vitest": "3.2.6"+ }+}diff --git a/packages/pieces/community/google-document-ai/src/i18n/translation.json b/packages/pieces/community/google-document-ai/src/i18n/translation.jsonnew file mode 100644index 00000000000..a1f8d1b97ef--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/i18n/translation.json@@ -0,0 +1,49 @@+{+ "Extract text, entities, form fields and tables from documents with Google Document AI processors.": "Extract text, entities, form fields and tables from documents with Google Document AI processors.",+ "Process Document": "Process Document",+ "Custom API Call": "Custom API Call",+ "Run a document through a Document AI processor and get its text, entities, form fields and tables": "Run a document through a Document AI processor and get its text, entities, form fields and tables",+ "Call any Document AI endpoint. Paths start with the version, for example `/v1/projects/{project}/locations/{location}/processors`.": "Call any Document AI endpoint. Paths start with the version, for example `/v1/projects/{project}/locations/{location}/processors`.",+ "Processor": "Processor",+ "Processor Version": "Processor Version",+ "Document Source": "Document Source",+ "File": "File",+ "Cloud Storage URI": "Cloud Storage URI",+ "MIME Type": "MIME Type",+ "Pages": "Pages",+ "Imageless Mode": "Imageless Mode",+ "Include Full Document": "Include Full Document",+ "Field Mask (Advanced)": "Field Mask (Advanced)",+ "Method": "Method",+ "Headers": "Headers",+ "Query Parameters": "Query Parameters",+ "Body Type": "Body Type",+ "Body": "Body",+ "Response is Binary": "Response is Binary",+ "Return Error as Output": "Return Error as Output",+ "Timeout": "Timeout",+ "Follow redirects": "Follow redirects",+ "The processor to run the document through.": "The processor to run the document through.",+ "For example `pretrained-ocr-v2.1-2024-08-07`. Leave empty to use the processor's default version.": "For example `pretrained-ocr-v2.1-2024-08-07`. Leave empty to use the processor's default version.",+ "The document to process. Up to 15 pages, or 30 with Imageless Mode.": "The document to process. Up to 15 pages, or 30 with Imageless Mode.",+ "For example `gs://bucket/file.pdf`.": "For example `gs://bucket/file.pdf`.",+ "Leave empty to detect it from the file extension.": "Leave empty to detect it from the file extension.",+ "Only process these pages, for example `1,3-5`.": "Only process these pages, for example `1,3-5`.",+ "Leave page images out of the result and allow up to 30 pages.": "Leave page images out of the result and allow up to 30 pages.",+ "Also return Google's raw `document` output. Can be several MB.": "Also return Google's raw `document` output. Can be several MB.",+ "Comma-separated fields to return, for example `text,entities`. Ignored when Include Full Document is on.": "Comma-separated fields to return, for example `text,entities`. Ignored when Include Full Document is on.",+ "Authorization headers are injected automatically from your connection.": "Authorization headers are injected automatically from your connection.",+ "Appended to the URL as ?key=value.": "Appended to the URL as ?key=value.",+ "Enable for files like PDFs, images, etc.": "Enable for files like PDFs, images, etc.",+ "On a failed request, output the error instead of failing the step.": "On a failed request, output the error instead of failing the step.",+ "Seconds to wait for a response. Empty: up to the flow limit (10 min).": "Seconds to wait for a response. Empty: up to the flow limit (10 min).",+ "Follow 3xx redirects instead of returning them as the response.": "Follow 3xx redirects instead of returning them as the response.",+ "File (upload or a file from a previous step)": "File (upload or a file from a previous step)",+ "Cloud Storage URI (gs://bucket/object)": "Cloud Storage URI (gs://bucket/object)",+ "GET": "GET",+ "POST": "POST",+ "PATCH": "PATCH",+ "PUT": "PUT",+ "DELETE": "DELETE",+ "HEAD": "HEAD"+}\ No newline at end of filediff --git a/packages/pieces/community/google-document-ai/src/index.ts b/packages/pieces/community/google-document-ai/src/index.tsnew file mode 100644index 00000000000..062bd3a54bd--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/index.ts@@ -0,0 +1,36 @@+import { createCustomApiCallAction } from '@activepieces/pieces-common';+import { createPiece, PieceCategory } from '@activepieces/pieces-framework';++import { processDocumentAction } from './lib/actions/process-document';+import { googleDocumentAiAuth } from './lib/auth';+import { googleDocumentAiClient } from './lib/common/client';++export const googleDocumentAi = createPiece({+ displayName: 'Google Document AI',+ description:+ 'Extract text, entities, form fields and tables from documents with Google Document AI processors.',+ auth: googleDocumentAiAuth,+ minimumSupportedRelease: '0.88.0',+ logoUrl: 'https://cdn.activepieces.com/pieces/google-document-ai.png',+ categories: [PieceCategory.ARTIFICIAL_INTELLIGENCE, PieceCategory.CONTENT_AND_FILES],+ authors: ['fabio-kozlowski'],+ actions: [+ processDocumentAction,+ createCustomApiCallAction({+ auth: googleDocumentAiAuth,+ baseUrl: (auth) => googleDocumentAiClient.baseUrl({ auth }),+ description:+ "Call any Document AI endpoint. Paths start with the version, for example `/v1/projects/{project}/locations/{location}/processors`.",+ authMapping: async (auth, propsValue) =>+ await googleDocumentAiClient.authHeaders({ auth, url: requestedUrl(propsValue) }),+ }),+ ],+ triggers: [],+});++function requestedUrl(propsValue: Record<string, unknown>): string {+ const urlProp = propsValue['url'];+ const url =+ typeof urlProp === 'object' && urlProp !== null && 'url' in urlProp ? urlProp.url : undefined;+ return typeof url === 'string' ? url : '';+}diff --git a/packages/pieces/community/google-document-ai/src/lib/actions/process-document.ts b/packages/pieces/community/google-document-ai/src/lib/actions/process-document.tsnew file mode 100644index 00000000000..cb6caf548d9--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/actions/process-document.ts@@ -0,0 +1,164 @@+import { createAction, Property } from '@activepieces/pieces-framework';++import { googleDocumentAiAuth } from '../auth';+import { googleDocumentAiApi } from '../common/api';+import { googleDocumentAiProps } from '../common/props';+import { googleDocumentAiUtils } from '../common/utils';+import { processDocumentOutputSchema } from '../output-schemas';++import type { GoogleDocumentAiProcessRequest } from '../common/types';++const sourceOptions: { label: string; value: 'file' | 'gcs' }[] = [+ { label: 'File (upload or a file from a previous step)', value: 'file' },+ { label: 'Cloud Storage URI (gs://bucket/object)', value: 'gcs' },+];++export const processDocumentAction = createAction({+ name: 'processDocument',+ classification: 'READ',+ displayName: 'Process Document',+ description:+ 'Run a document through a Document AI processor and get its text, entities, form fields and tables',+ audience: 'both',+ aiMetadata: {+ description:+ 'Extract text, entities, form fields and tables from one document (a flow file or a gs:// Cloud Storage URI) with a Google Document AI processor of the connected project. Use for OCR, form parsing or invoice/ID extraction; online processing takes up to 15 pages (30 in imageless mode), so pick pages or use Custom API Call batchProcess for longer files. Stores nothing, but each call is billed per page; safe to retry.',+ idempotent: true,+ },+ outputSchema: processDocumentOutputSchema,+ auth: googleDocumentAiAuth,+ props: {+ processor: googleDocumentAiProps.processor({ required: true }),+ processorVersion: Property.ShortText({+ displayName: 'Processor Version',+ description:+ "For example `pretrained-ocr-v2.1-2024-08-07`. Leave empty to use the processor's default version.",+ required: false,+ }),+ source: Property.StaticDropdown<'file' | 'gcs', true>({+ displayName: 'Document Source',+ required: true,+ defaultValue: 'file',+ options: { disabled: false, options: sourceOptions },+ }),+ file: Property.File({+ displayName: 'File',+ description:+ 'The document to process. Up to 15 pages, or 30 with Imageless Mode.',+ required: false,+ }),+ gcsUri: Property.ShortText({+ displayName: 'Cloud Storage URI',+ description:+ 'For example `gs://bucket/file.pdf`.',+ required: false,+ }),+ mimeType: Property.ShortText({+ displayName: 'MIME Type',+ description: 'Leave empty to detect it from the file extension.',+ required: false,+ }),+ pages: Property.ShortText({+ displayName: 'Pages',+ description:+ 'Only process these pages, for example `1,3-5`.',+ required: false,+ }),+ imagelessMode: Property.Checkbox({+ displayName: 'Imageless Mode',+ description:+ 'Leave page images out of the result and allow up to 30 pages.',+ required: false,+ defaultValue: true,+ }),+ includeFullDocument: Property.Checkbox({+ displayName: 'Include Full Document',+ description:+ 'Also return Google\'s raw `document` output. Can be several MB.',+ required: false,+ defaultValue: false,+ }),+ fieldMask: Property.ShortText({+ displayName: 'Field Mask (Advanced)',+ description:+ 'Comma-separated fields to return, for example `text,entities`. Ignored when Include Full Document is on.',+ required: false,+ }),+ },+ async run(context) {+ const {+ processor,+ processorVersion,+ source,+ file,+ gcsUri,+ mimeType,+ pages,+ imagelessMode,+ includeFullDocument,+ fieldMask,+ } = context.propsValue;+ const auth = await googleDocumentAiApi.resolveAuth({ auth: context.auth });+ const name = googleDocumentAiUtils.processorResourceName({+ auth,+ processor,+ version: processorVersion ?? undefined,+ });++ const request: GoogleDocumentAiProcessRequest = {};+ if (source === 'gcs') {+ const uri = String(gcsUri ?? '').trim();+ if (!uri.startsWith('gs://')) {+ throw new Error('Cloud Storage URI must look like gs://bucket/path/file.pdf.');+ }+ request.gcsDocument = {+ gcsUri: uri,+ mimeType: googleDocumentAiUtils.mimeTypeFor({+ explicit: mimeType ?? undefined,+ names: [uri],+ }),+ };+ } else {+ if (!file) {+ throw new Error('Pick a file to process (or switch Document Source to Cloud Storage URI).');+ }+ request.rawDocument = {+ content: file.base64,+ mimeType: googleDocumentAiUtils.mimeTypeFor({+ explicit: mimeType ?? undefined,+ names: [file.filename, file.extension ? `x.${file.extension}` : undefined],+ }),+ ...(file.filename ? { displayName: file.filename } : {}),+ };+ }++ const selectedPages = googleDocumentAiUtils.parsePageSelection(pages ?? undefined);+ if (selectedPages)+ request.processOptions = { individualPageSelector: { pages: selectedPages } };+ if (imagelessMode !== false) request.imagelessMode = true;+ if (!includeFullDocument)+ request.fieldMask = googleDocumentAiUtils.effectiveFieldMask({+ fieldMask: fieldMask ?? undefined,+ });++ const response = await googleDocumentAiApi.processDocument({+ auth,+ processorName: name,+ request,+ });+ if (response.document?.error?.message) {+ throw new Error(+ `Document AI could not process the document: ${response.document.error.message}`,+ );+ }+ return {+ processor: name,+ ...googleDocumentAiUtils.summarizeDocument({+ document: response.document,+ includeFull: Boolean(includeFullDocument),+ selectedPages,+ }),+ ...(response.humanReviewStatus ? { humanReviewStatus: response.humanReviewStatus } : {}),+ };+ },+});diff --git a/packages/pieces/community/google-document-ai/src/lib/auth.ts b/packages/pieces/community/google-document-ai/src/lib/auth.tsnew file mode 100644index 00000000000..6b6a176d70f--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/auth.ts@@ -0,0 +1,95 @@+import { AppConnectionType, PieceAuth, Property } from '@activepieces/pieces-framework';++import { googleDocumentAiApi } from './common/api';+import { googleDocumentAiClient } from './common/client';++const locationDescription =+ 'Where your processors live, usually `us` or `eu`.';++const serviceAccountInstructions = `+Connect with a **service account** from **your own** Google Cloud project (recommended: flows run without anyone signing in):++1. In [Google Cloud Console](https://console.cloud.google.com/), select or create a project with **billing enabled** (Document AI charges per page). **APIs & Services → Library**: enable **Cloud Document AI API**.+2. **Document AI → Processors**: create the processors you need (e.g. Enterprise Document OCR, Form Parser, Invoice Parser) and note their **location** (\`us\` or \`eu\`).+3. **IAM & Admin → Service Accounts → Create**. Grant it the role **Document AI API User** (\`roles/documentai.apiUser\`) on the project. Open the account, **Keys → Add key → JSON** and download the key file.+4. Paste the **whole JSON key file** below and fill in the processors' location. The project is read from the key file unless you override it.+`.trim();++const oauthInstructions = `+Sign in with a Google account that can use Document AI in **your own** Google Cloud project:++1. In [Google Cloud Console](https://console.cloud.google.com/), select or create a project with **billing enabled** (Document AI charges per page). **APIs & Services → Library**: enable **Cloud Document AI API**.+2. **APIs & Services → OAuth consent screen**: add the scope \`https://www.googleapis.com/auth/cloud-platform\`. While the app is in *Testing*, add the Google accounts that will connect as test users. The account needs the **Document AI API User** role on the project.+3. **APIs & Services → Credentials → Create credentials → OAuth client ID**, type *Web application*. Add the **Redirect URL shown below** to *Authorized redirect URIs*.+4. Paste the **Client ID** and **Client Secret**, fill in the **Project ID** and the processors' **location**, then sign in.+`.trim();++export const googleDocumentAiScopes = [googleDocumentAiClient.DOCUMENT_AI_SCOPE, 'email'];++export const googleDocumentAiServiceAccount = PieceAuth.CustomAuth({+ displayName: 'Service Account (Recommended)',+ description: serviceAccountInstructions,+ required: true,+ props: {+ keyFile: PieceAuth.SecretText({+ displayName: 'Service Account Key (JSON)',+ description:+ 'The full content of the key file downloaded from Google Cloud (starts with `{ "type": "service_account", …`).',+ required: true,+ }),+ location: Property.ShortText({+ displayName: 'Location',+ description: locationDescription,+ required: true,+ defaultValue: googleDocumentAiClient.DEFAULT_LOCATION,+ }),+ projectId: Property.ShortText({+ displayName: 'Project ID (Override)',+ description:+ "Only if your processors are in a different project than the key file's.",+ required: false,+ }),+ },+ validate: async ({ auth }) => {+ try {+ const resolved = await googleDocumentAiApi.resolveAuth({+ auth: { type: AppConnectionType.CUSTOM_AUTH, props: auth },+ });+ const processors = await googleDocumentAiApi.listProcessors({ auth: resolved });+ if (processors.length === 0) {+ return {+ valid: false,+ error: `No processors found in project ${resolved.projectId}, location ${resolved.location}. Create one in Document AI → Processors, or check the location (us / eu).`,+ };+ }+ return { valid: true };+ } catch (error) {+ return { valid: false, error: error instanceof Error ? error.message : String(error) };+ }+ },+});++export const googleDocumentAiOAuth2 = PieceAuth.OAuth2({+ displayName: 'Google Account (OAuth2)',+ description: oauthInstructions,+ authUrl: 'https://accounts.google.com/o/oauth2/auth',+ tokenUrl: 'https://oauth2.googleapis.com/token',+ required: true,+ scope: googleDocumentAiScopes,+ props: {+ projectId: Property.ShortText({+ displayName: 'Project ID',+ description:+ 'Your Google Cloud project ID, for example `my-company-prod`.',+ required: true,+ }),+ location: Property.ShortText({+ displayName: 'Location',+ description: locationDescription,+ required: true,+ defaultValue: googleDocumentAiClient.DEFAULT_LOCATION,+ }),+ },+});++export const googleDocumentAiAuth = [googleDocumentAiServiceAccount, googleDocumentAiOAuth2];diff --git a/packages/pieces/community/google-document-ai/src/lib/common/api.ts b/packages/pieces/community/google-document-ai/src/lib/common/api.tsnew file mode 100644index 00000000000..d13c298a780--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/common/api.ts@@ -0,0 +1,97 @@+import { googleDocumentAiClient } from './client';+import { googleDocumentAiUtils } from './utils';++import type {+ GoogleDocumentAiAuthValue,+ GoogleDocumentAiListProcessorsResponse,+ GoogleDocumentAiProcessor,+ GoogleDocumentAiProcessRequest,+ GoogleDocumentAiProcessResponse,+ GoogleDocumentAiResolvedAuth,+} from './types';++async function resolveAuth({+ auth,+}: {+ auth: GoogleDocumentAiAuthValue | undefined;+}): Promise<GoogleDocumentAiResolvedAuth> {+ return await googleDocumentAiClient.resolveAuth({ auth });+}++async function listProcessors({+ auth,+}: {+ auth: GoogleDocumentAiResolvedAuth;+}): Promise<GoogleDocumentAiProcessor[]> {+ const processors: GoogleDocumentAiProcessor[] = [];+ let pageToken: string | undefined;+ do {+ const page = await googleDocumentAiClient.request<GoogleDocumentAiListProcessorsResponse>({+ auth,+ method: 'GET',+ path: `/${googleDocumentAiUtils.parentOf({ auth })}/processors`,+ query: { pageSize: 100, pageToken },+ timeoutMs: METADATA_TIMEOUT_MS,+ isExpected: isListProcessorsResponse,+ });+ processors.push(...(page.processors ?? []));+ pageToken = page.nextPageToken;+ } while (pageToken);+ return processors;+}++async function processDocument({+ auth,+ processorName,+ request,+}: {+ auth: GoogleDocumentAiResolvedAuth;+ processorName: string;+ request: GoogleDocumentAiProcessRequest;+}): Promise<GoogleDocumentAiProcessResponse> {+ if (!request.rawDocument && !request.gcsDocument) {+ throw new Error('process needs a rawDocument or a gcsDocument.');+ }+ return await googleDocumentAiClient.request<GoogleDocumentAiProcessResponse>({+ auth,+ method: 'POST',+ path: `/${processorName}:process`,+ body: request,+ timeoutMs: PROCESS_TIMEOUT_MS,+ timeoutHint: 'For large files, select fewer pages or turn on Imageless Mode.',+ isExpected: isProcessResponse,+ });+}++function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === 'object' && value !== null;+}++function isProcessor(value: unknown): value is GoogleDocumentAiProcessor {+ return isRecord(value) && typeof value['name'] === 'string';+}++function isListProcessorsResponse(value: unknown): value is GoogleDocumentAiListProcessorsResponse {+ if (!isRecord(value)) return false;+ const processors = value['processors'];+ const nextPageToken = value['nextPageToken'];+ return (+ (processors === undefined || (Array.isArray(processors) && processors.every(isProcessor))) &&+ (nextPageToken === undefined || typeof nextPageToken === 'string')+ );+}++function isProcessResponse(value: unknown): value is GoogleDocumentAiProcessResponse {+ if (!isRecord(value)) return false;+ const document = value['document'];+ const humanReviewStatus = value['humanReviewStatus'];+ return (+ (document === undefined || isRecord(document)) &&+ (humanReviewStatus === undefined || isRecord(humanReviewStatus))+ );+}++export const googleDocumentAiApi = { resolveAuth, listProcessors, processDocument };++const METADATA_TIMEOUT_MS = 30_000;+const PROCESS_TIMEOUT_MS = 120_000;diff --git a/packages/pieces/community/google-document-ai/src/lib/common/client.ts b/packages/pieces/community/google-document-ai/src/lib/common/client.tsnew file mode 100644index 00000000000..d0e7001cabb--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/common/client.ts@@ -0,0 +1,653 @@+import { createHash, createSign } from 'node:crypto';++import { AppConnectionType } from '@activepieces/pieces-framework';++import type {+ GoogleDocumentAiAuthValue,+ GoogleDocumentAiFieldViolation,+ GoogleDocumentAiResolvedAuth,+ GoogleDocumentAiServiceAccountKey,+ GoogleDocumentAiServiceAccountKeyFile,+ GoogleDocumentAiServiceAccountProps,+} from './types';++function baseUrl({ auth }: { auth: GoogleDocumentAiAuthValue | undefined }): string {+ try {+ return apiRoot(normalizeLocation(connectionLocation(auth)));+ } catch {+ return apiRoot('us');+ }+}++async function request<T>({+ auth,+ method,+ path,+ query,+ body,+ timeoutMs,+ timeoutHint,+ isExpected,+}: RequestParams & {+ auth: GoogleDocumentAiResolvedAuth;+ isExpected: (value: unknown) => value is T;+}): Promise<T> {+ const signal = AbortSignal.timeout(timeoutMs);+ const headers: Record<string, string> = {+ Authorization: `Bearer ${auth.accessToken}`,+ Accept: 'application/json',+ };+ if (body !== undefined) {+ headers['Content-Type'] = 'application/json';+ }+ let response: Response;+ try {+ response = await fetch(+ buildUrl({ url: `${apiRoot(auth.location)}/${API_VERSION}${path}`, query }),+ {+ method,+ headers,+ ...(body !== undefined ? { body: JSON.stringify(body) } : {}),+ signal,+ },+ );+ } catch (error) {+ if (isDeadlineExceeded({ error, signal })) {+ throw timeoutError({ timeoutMs, hint: timeoutHint });+ }+ throw new Error(+ `Could not reach Google Document AI: ${+ error instanceof Error ? error.message : 'network error'+ }`,+ );+ }+ let text: string;+ try {+ text = await response.text();+ } catch (error) {+ if (isDeadlineExceeded({ error, signal })) {+ throw timeoutError({ timeoutMs, hint: timeoutHint });+ }+ throw new Error(`Google Document AI returned ${response.status} with an unreadable body.`);+ }+ const parsed = text === '' ? undefined : safeJson(text);+ if (!response.ok) {+ throw GoogleDocumentAiApiError.fromResponse({ status: response.status, body: parsed });+ }+ if (!isExpected(parsed)) {+ throw new Error(+ `Google Document AI returned ${response.status} with an unexpected response body.`,+ );+ }+ return parsed;+}++async function resolveAuth({+ auth,+}: {+ auth: GoogleDocumentAiAuthValue | undefined;+}): Promise<GoogleDocumentAiResolvedAuth> {+ if (!auth) {+ throw new Error('A Google Document AI connection is required.');+ }+ if (auth.type === AppConnectionType.CUSTOM_AUTH) {+ return await resolveServiceAccount({ props: auth.props });+ }+ if (!auth.access_token) {+ throw new Error('The Google Document AI connection has no access token; reconnect it.');+ }+ return {+ accessToken: auth.access_token,+ projectId: normalizeProjectId(stringProp({ props: auth.props, key: 'projectId' })),+ location: normalizeLocation(stringProp({ props: auth.props, key: 'location' })),+ };+}++async function authHeaders({+ auth,+ url,+}: {+ auth: GoogleDocumentAiAuthValue;+ url: string;+}): Promise<Record<string, string>> {+ assertConnectionHost({ url, root: apiRoot(normalizeLocation(connectionLocation(auth))) });+ const { accessToken } = await resolveAuth({ auth });+ return { Authorization: `Bearer ${accessToken}` };+}++function apiRoot(location: string): string {+ return `https://${location}-documentai.googleapis.com`;+}++function normalizeLocation(value: string | undefined): string {+ const location = String(value ?? '')+ .trim()+ .toLowerCase();+ if (location === '') return DEFAULT_LOCATION;+ if (!/^[a-z][a-z0-9-]*$/.test(location)) {+ throw new Error(`Invalid location "${value}": use a Document AI location id such as us or eu.`);+ }+ return location;+}++function normalizeProjectId(value: string | undefined): string {+ const projectId = String(value ?? '').trim();+ if (projectId === '') {+ throw new Error('The connection has no Google Cloud project ID.');+ }+ if (!/^[a-z0-9][a-z0-9:.-]*$/i.test(projectId)) {+ throw new Error(+ `Invalid project ID "${value}": use the project ID shown in Google Cloud Console (e.g. my-company-prod).`,+ );+ }+ return projectId;+}++function connectionLocation(auth: GoogleDocumentAiAuthValue | undefined): string | undefined {+ if (!auth) return undefined;+ if (auth.type === AppConnectionType.CUSTOM_AUTH) return auth.props.location;+ return stringProp({ props: auth.props, key: 'location' });+}++function stringProp({+ props,+ key,+}: {+ props: Record<string, unknown> | undefined;+ key: string;+}): string | undefined {+ const value = props?.[key];+ return typeof value === 'string' ? value : undefined;+}++async function resolveServiceAccount({+ props,+}: {+ props: GoogleDocumentAiServiceAccountProps;+}): Promise<GoogleDocumentAiResolvedAuth> {+ const key = parseServiceAccountKey(props.keyFile);+ const projectId = normalizeProjectId(props.projectId?.trim() ? props.projectId : key.projectId);+ return {+ accessToken: await mintServiceAccountToken({ key, scopes: [DOCUMENT_AI_SCOPE] }),+ projectId,+ location: normalizeLocation(props.location),+ };+}++function assertConnectionHost({ url, root }: { url: string; root: string }): void {+ if (!url.startsWith('http://') && !url.startsWith('https://')) return;+ const origin = originOf(url);+ if (origin === new URL(root).origin) return;+ throw new Error(+ `Custom API Call only sends the connection's Google credentials to ${root}, but the URL points to ${+ origin ?? 'an invalid address'+ }. Use a path such as /${API_VERSION}/projects/{project}/locations/{location}/processors, or a full URL on ${root}.`,+ );+}++function originOf(url: string): string | undefined {+ try {+ return new URL(url).origin;+ } catch {+ return undefined;+ }+}++function hintFor({+ status,+ reason,+ message,+}: {+ status: number;+ reason: string | undefined;+ message: string | undefined;+}): string {+ const text = `${reason ?? ''} ${message ?? ''}`;+ if (reason === 'SERVICE_DISABLED' || /has not been used in project|is disabled/i.test(text)) {+ return ' Enable the Cloud Document AI API in the Google Cloud project of the connection.';+ }+ if (reason === 'BILLING_DISABLED' || /billing/i.test(text)) {+ return ' Document AI needs billing enabled on the Google Cloud project.';+ }+ if (status === 403) {+ return " Grant the connection's account the role Document AI API User (roles/documentai.apiUser) on the project.";+ }+ if (status === 404 && /processor/i.test(text)) {+ return " Check the processor ID and that the connection's project and location match where the processor was created.";+ }+ if (/page|pages/i.test(text) && /limit|exceed|maximum/i.test(text)) {+ return ' Online processing accepts up to 15 pages (30 with Imageless Mode); select pages or split the file.';+ }+ return '';+}++function safeJson(text: string): unknown {+ try {+ return JSON.parse(text);+ } catch {+ return text;+ }+}++function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === 'object' && value !== null;+}++function optionalString(value: unknown): string | undefined {+ return typeof value === 'string' ? value : undefined;+}++function parseGoogleError(value: unknown): ParsedGoogleError {+ const error = isRecord(value) ? value['error'] : undefined;+ if (!isRecord(error)) {+ return { status: undefined, message: undefined, details: [] };+ }+ const details = Array.isArray(error['details']) ? error['details'] : [];+ return {+ status: optionalString(error['status']),+ message: optionalString(error['message']),+ details: details.filter(isRecord).map((detail) => ({+ reason: optionalString(detail['reason']),+ fieldViolations: Array.isArray(detail['fieldViolations'])+ ? detail['fieldViolations'].filter(isRecord).map((violation) => {+ const field = optionalString(violation['field']);+ const description = optionalString(violation['description']);+ return {+ ...(field !== undefined ? { field } : {}),+ ...(description !== undefined ? { description } : {}),+ };+ })+ : [],+ })),+ };+}++function truncate(text: string): string {+ return text.length > MAX_RAW_ERROR_LENGTH ? `${text.slice(0, MAX_RAW_ERROR_LENGTH)}...` : text;+}++function buildUrl({+ url,+ query,+}: {+ url: string;+ query: Record<string, string | number | undefined> | undefined;+}): string {+ const search = new URLSearchParams();+ for (const [key, value] of Object.entries(query ?? {})) {+ if (value === undefined || value === '') continue;+ search.set(key, String(value));+ }+ const text = search.toString();+ return text === '' ? url : `${url}?${text}`;+}++function isDeadlineExceeded({ error, signal }: { error: unknown; signal: AbortSignal }): boolean {+ return (+ signal.aborted ||+ (error instanceof Error && (error.name === 'TimeoutError' || error.name === 'AbortError'))+ );+}++function timeoutError({ timeoutMs, hint }: { timeoutMs: number; hint: string | undefined }): Error {+ return new Error(+ `Google Document AI did not answer within ${Math.round(+ timeoutMs / 1000,+ )} seconds. Try again in a moment.${hint ? ` ${hint}` : ''}`,+ );+}++function normalizePrivateKey(value: string): string {+ const key = String(value ?? '')+ .trim()+ .replace(/^"|"$/g, '')+ .replace(/\\n/g, '\n');+ if (!key.includes('-----BEGIN') || !key.includes('PRIVATE KEY-----')) {+ throw new Error(+ 'The private key must be the PEM block of the service account key file (-----BEGIN PRIVATE KEY----- … -----END PRIVATE KEY-----).',+ );+ }+ return key;+}++function parseServiceAccountKey(raw: string): GoogleDocumentAiServiceAccountKey {+ const text = String(raw ?? '')+ .trim()+ .replace(/^```(?:json)?\s*/i, '')+ .replace(/\s*```$/, '');+ if (text === '') {+ throw new Error('Paste the service account key file (JSON) into the connection.');+ }+ let parsed: GoogleDocumentAiServiceAccountKeyFile | string | number | boolean | null;+ try {+ parsed = JSON.parse(text);+ } catch {+ throw new Error(+ 'The service account key is not valid JSON: paste the whole file downloaded from Google Cloud (it starts with { "type": "service_account", …).',+ );+ }+ if (typeof parsed !== 'object' || parsed === null) {+ throw new Error('The service account key must be a JSON object.');+ }+ if (parsed.type !== undefined && parsed.type !== 'service_account') {+ throw new Error(+ `The key file has type "${String(+ parsed.type,+ )}"; a service account key has type "service_account".`,+ );+ }+ const clientEmail = typeof parsed.client_email === 'string' ? parsed.client_email.trim() : '';+ const privateKey = typeof parsed.private_key === 'string' ? parsed.private_key : '';+ if (!clientEmail || !privateKey) {+ throw new Error('The service account key must contain client_email and private_key.');+ }+ const projectId =+ typeof parsed.project_id === 'string' && parsed.project_id.trim() !== ''+ ? parsed.project_id.trim()+ : undefined;+ return {+ clientEmail,+ privateKey: normalizePrivateKey(privateKey),+ ...(projectId ? { projectId } : {}),+ };+}++function buildAssertion({+ key,+ scopes,+ now,+}: {+ key: GoogleDocumentAiServiceAccountKey;+ scopes: string[];+ now: number;+}): string {+ const header = base64Url(JSON.stringify({ alg: 'RS256', typ: 'JWT' }));+ const claims = base64Url(+ JSON.stringify({+ iss: key.clientEmail.trim(),+ scope: scopes.join(' '),+ aud: GOOGLE_TOKEN_URL,+ iat: now,+ exp: now + JWT_LIFETIME_SECONDS,+ }),+ );+ const input = `${header}.${claims}`;+ const signature = createSign('RSA-SHA256')+ .update(input)+ .end()+ .sign(normalizePrivateKey(key.privateKey), 'base64url');+ return `${input}.${signature}`;+}++async function mintServiceAccountToken({+ key,+ scopes,+}: {+ key: GoogleDocumentAiServiceAccountKey;+ scopes: string[];+}): Promise<string> {+ const cacheKey = `${key.clientEmail.trim()}|${privateKeyFingerprint(+ key.privateKey,+ )}|${scopes.join(' ')}`;+ const cached = readCachedToken({ cacheKey, now: Math.floor(Date.now() / 1000) });+ if (cached) {+ return cached;+ }+ const inFlight = pendingExchanges.get(cacheKey);+ if (inFlight) {+ return await inFlight;+ }+ const exchange = mintAndCache({ key, scopes, cacheKey }).finally(() => {+ if (pendingExchanges.get(cacheKey) === exchange) {+ pendingExchanges.delete(cacheKey);+ }+ });+ pendingExchanges.set(cacheKey, exchange);+ return await exchange;+}++async function mintAndCache({+ key,+ scopes,+ cacheKey,+}: {+ key: GoogleDocumentAiServiceAccountKey;+ scopes: string[];+ cacheKey: string;+}): Promise<string> {+ const now = Math.floor(Date.now() / 1000);+ const { accessToken, expiresIn } = await exchangeAssertion({+ assertion: buildAssertion({ key, scopes, now }),+ });+ storeCachedToken({ cacheKey, token: { accessToken, expiresAt: now + expiresIn }, now });+ return accessToken;+}++function privateKeyFingerprint(privateKey: string): string {+ return createHash('sha256').update(normalizePrivateKey(privateKey)).digest('hex');+}++function base64Url(input: string | Buffer): string {+ return Buffer.from(input).toString('base64url');+}++async function exchangeAssertion({+ assertion,+}: {+ assertion: string;+}): Promise<{ accessToken: string; expiresIn: number }> {+ const signal = AbortSignal.timeout(TOKEN_REQUEST_TIMEOUT_MS);+ let response: Response;+ try {+ response = await fetch(GOOGLE_TOKEN_URL, {+ method: 'POST',+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },+ body: new URLSearchParams({+ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',+ assertion,+ }).toString(),+ signal,+ });+ } catch (error) {+ if (isDeadlineExceeded({ error, signal })) {+ throw new Error(TOKEN_TIMEOUT_MESSAGE);+ }+ throw new Error(+ `Could not reach the Google token endpoint: ${+ error instanceof Error ? error.message : String(error)+ }`,+ );+ }+ const body = await readTokenBody({ response, signal });+ if (!response.ok) {+ throw new Error(describeTokenError({ status: response.status, body }));+ }+ const accessToken = isRecord(body) ? optionalString(body['access_token']) : undefined;+ if (!accessToken) {+ const code = isRecord(body) ? optionalString(body['error']) : undefined;+ const description = isRecord(body) ? optionalString(body['error_description']) : undefined;+ throw new Error(+ `Google token endpoint returned no access token${+ code ? ` (${code}: ${description ?? ''})` : ''+ }.`,+ );+ }+ const expiresIn = isRecord(body) ? body['expires_in'] : undefined;+ return {+ accessToken,+ expiresIn: typeof expiresIn === 'number' && expiresIn > 0 ? expiresIn : JWT_LIFETIME_SECONDS,+ };+}++async function readTokenBody({+ response,+ signal,+}: {+ response: Response;+ signal: AbortSignal;+}): Promise<unknown> {+ try {+ const parsed: unknown = await response.json();+ return parsed;+ } catch (error) {+ if (isDeadlineExceeded({ error, signal })) {+ throw new Error(TOKEN_TIMEOUT_MESSAGE);+ }+ return undefined;+ }+}++function describeTokenError({ status, body }: { status: number; body: unknown }): string {+ if (isRecord(body)) {+ const code = optionalString(body['error']);+ const description = optionalString(body['error_description']);+ const hint =+ code === 'invalid_grant'+ ? ' Check that the key was not deleted or disabled in Google Cloud and that the server clock is right.'+ : code === 'invalid_client'+ ? ' The client_email of the key file was not recognised; paste the whole key file again.'+ : '';+ return `Google rejected the service account (${code ?? status}): ${+ description ?? ''+ }${hint}`.trim();+ }+ return `Google token endpoint returned ${status}.`;+}++function readCachedToken({ cacheKey, now }: { cacheKey: string; now: number }): string | undefined {+ sweepExpiredTokens({ now });+ return tokenCache.get(cacheKey)?.accessToken;+}++function storeCachedToken({+ cacheKey,+ token,+ now,+}: {+ cacheKey: string;+ token: CachedToken;+ now: number;+}): void {+ sweepExpiredTokens({ now });+ tokenCache.delete(cacheKey);+ tokenCache.set(cacheKey, token);+ for (const oldest of tokenCache.keys()) {+ if (tokenCache.size <= MAX_CACHED_TOKENS) break;+ tokenCache.delete(oldest);+ }+}++function sweepExpiredTokens({ now }: { now: number }): void {+ for (const [cacheKey, token] of tokenCache) {+ if (token.expiresAt - REFRESH_MARGIN_SECONDS <= now) {+ tokenCache.delete(cacheKey);+ }+ }+}++class GoogleDocumentAiApiError extends Error {+ readonly status: number;+ readonly googleStatus: string | undefined;+ readonly reason: string | undefined;+ readonly googleMessage: string | undefined;+ readonly fieldViolations: GoogleDocumentAiFieldViolation[];++ constructor({+ status,+ googleStatus,+ reason,+ googleMessage,+ fieldViolations,+ summary,+ }: GoogleDocumentAiApiErrorParams) {+ super(summary);+ this.name = 'GoogleDocumentAiApiError';+ this.status = status;+ this.googleStatus = googleStatus;+ this.reason = reason;+ this.googleMessage = googleMessage;+ this.fieldViolations = fieldViolations;+ }++ static fromResponse({+ status,+ body,+ }: {+ status: number;+ body: unknown;+ }): GoogleDocumentAiApiError {+ const parsed = parseGoogleError(body);+ const googleStatus = parsed.status;+ const googleMessage = parsed.message;+ const reason = parsed.details.find((d) => d.reason !== undefined)?.reason;+ const fieldViolations = parsed.details.flatMap((d) => d.fieldViolations);++ const head = `Google Document AI returned ${status}${googleStatus ? ` (${googleStatus})` : ''}`;+ const detail =+ googleMessage ?? truncate(typeof body === 'string' ? body : JSON.stringify(body ?? null));+ const violations =+ fieldViolations.length > 0+ ? ` [${fieldViolations.map((v) => `${v.field ?? '?'}: ${v.description ?? ''}`).join('; ')}]`+ : '';+ const hint = hintFor({ status, reason, message: googleMessage });+ return new GoogleDocumentAiApiError({+ status,+ googleStatus,+ reason,+ googleMessage,+ fieldViolations,+ summary: `${head}: ${detail}${violations}${reason ? ` [reason: ${reason}]` : ''}${hint}`,+ });+ }+}++const DOCUMENT_AI_SCOPE = 'https://www.googleapis.com/auth/cloud-platform';+const DEFAULT_LOCATION = 'us';++export const googleDocumentAiClient = {+ baseUrl,+ request,+ resolveAuth,+ authHeaders,+ DOCUMENT_AI_SCOPE,+ DEFAULT_LOCATION,+};++const API_VERSION = 'v1';+const MAX_RAW_ERROR_LENGTH = 500;+const GOOGLE_TOKEN_URL = 'https://oauth2.googleapis.com/token';+const JWT_LIFETIME_SECONDS = 3600;+const REFRESH_MARGIN_SECONDS = 300;+const MAX_CACHED_TOKENS = 100;+const TOKEN_REQUEST_TIMEOUT_MS = 30_000;+const TOKEN_TIMEOUT_MESSAGE =+ 'Google token endpoint did not answer within 30 seconds. Try again in a moment.';++const tokenCache = new Map<string, CachedToken>();+const pendingExchanges = new Map<string, Promise<string>>();++type RequestParams = {+ method: 'GET' | 'POST';+ path: string;+ query?: Record<string, string | number | undefined>;+ body?: unknown;+ timeoutMs: number;+ timeoutHint?: string;+};++type CachedToken = { accessToken: string; expiresAt: number };++type ParsedGoogleError = {+ status: string | undefined;+ message: string | undefined;+ details: { reason: string | undefined; fieldViolations: GoogleDocumentAiFieldViolation[] }[];+};++type GoogleDocumentAiApiErrorParams = {+ status: number;+ googleStatus: string | undefined;+ reason: string | undefined;+ googleMessage: string | undefined;+ fieldViolations: GoogleDocumentAiFieldViolation[];+ summary: string;+};diff --git a/packages/pieces/community/google-document-ai/src/lib/common/props.ts b/packages/pieces/community/google-document-ai/src/lib/common/props.tsnew file mode 100644index 00000000000..73d8f6f2d4b--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/common/props.ts@@ -0,0 +1,80 @@+import { Property } from '@activepieces/pieces-framework';+import type { DropdownState } from '@activepieces/pieces-framework';++import { googleDocumentAiAuth } from '../auth';+import { googleDocumentAiApi } from './api';++import type { GoogleDocumentAiAuthValue, GoogleDocumentAiProcessor } from './types';++function processor<R extends boolean>({+ required,+ displayName = 'Processor',+ description = 'The processor to run the document through.',+}: PropParams<R>) {+ return Property.Dropdown({+ auth: googleDocumentAiAuth,+ displayName,+ description,+ required,+ refreshers: [],+ options: async ({ auth }) => await processorOptions({ auth }),+ });+}++async function processorOptions({+ auth,+}: {+ auth: GoogleDocumentAiAuthValue | undefined;+}): Promise<DropdownState<string>> {+ if (!auth) {+ return disabledOptions({+ placeholder: 'Please select an existing or create a new connection.',+ });+ }+ try {+ const resolved = await googleDocumentAiApi.resolveAuth({ auth });+ const processors = await googleDocumentAiApi.listProcessors({ auth: resolved });+ if (processors.length === 0) {+ return disabledOptions({+ placeholder: `No processors in project ${resolved.projectId}, location ${resolved.location}. Create one in Document AI → Processors.`,+ });+ }+ const options = processors+ .map((p) => ({ label: processorLabel(p), value: p.name }))+ .sort((a, b) => a.label.localeCompare(b.label));+ return { disabled: false, options };+ } catch (error) {+ const detail = error instanceof Error ? error.message : String(error);+ return disabledOptions({ placeholder: `Could not list processors: ${detail.slice(0, 300)}` });+ }+}++function processorTypeLabel(type: string | undefined): string {+ if (!type) return 'unknown type';+ const words = type+ .replace(/_PROCESSOR$/, '')+ .toLowerCase()+ .split('_');+ return words+ .map((w, i) => (w === 'ocr' ? 'OCR' : i === 0 ? w.charAt(0).toUpperCase() + w.slice(1) : w))+ .join(' ');+}++function processorLabel(processor: GoogleDocumentAiProcessor): string {+ const id = processor.name.split('/').pop() ?? processor.name;+ const state =+ processor.state && processor.state !== 'ENABLED' ? ` · ${processor.state.toLowerCase()}` : '';+ return `${processor.displayName ?? id} (${processorTypeLabel(processor.type)})${state} · ${id}`;+}++function disabledOptions({ placeholder }: { placeholder: string }): DropdownState<never> {+ return { disabled: true, options: [], placeholder };+}++export const googleDocumentAiProps = { processor };++export type PropParams<R extends boolean> = {+ required: R;+ displayName?: string;+ description?: string;+};diff --git a/packages/pieces/community/google-document-ai/src/lib/common/types.ts b/packages/pieces/community/google-document-ai/src/lib/common/types.tsnew file mode 100644index 00000000000..e1e29de6c61--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/common/types.ts@@ -0,0 +1,147 @@+import type { AppConnectionType } from '@activepieces/pieces-framework';++export type GoogleDocumentAiServiceAccountProps = {+ keyFile: string;+ location: string;+ projectId?: string | undefined;+};++export type GoogleDocumentAiAuthValue =+ | { type: AppConnectionType.CUSTOM_AUTH; props: GoogleDocumentAiServiceAccountProps }+ | {+ type: AppConnectionType.OAUTH2;+ access_token: string;+ props?: Record<string, unknown>;+ };++export type GoogleDocumentAiResolvedAuth = {+ accessToken: string;+ projectId: string;+ location: string;+};++export type GoogleDocumentAiServiceAccountKey = {+ clientEmail: string;+ privateKey: string;+ projectId?: string;+};++export type GoogleDocumentAiServiceAccountKeyFile = {+ type?: unknown;+ client_email?: unknown;+ private_key?: unknown;+ project_id?: unknown;+};++export type GoogleDocumentAiFieldViolation = { field?: string; description?: string };++export type GoogleDocumentAiProcessor = {+ name: string;+ type?: string;+ displayName?: string;+ state?: string;+ defaultProcessorVersion?: string;+ processEndpoint?: string;+ createTime?: string;+};++export type GoogleDocumentAiListProcessorsResponse = {+ processors?: GoogleDocumentAiProcessor[];+ nextPageToken?: string;+};++export type GoogleDocumentAiDocument = {+ text?: string;+ mimeType?: string;+ uri?: string;+ entities?: GoogleDocumentAiEntity[];+ pages?: GoogleDocumentAiPage[];+ error?: { code?: number; message?: string };+ [key: string]: unknown;+};++export type GoogleDocumentAiProcessRequest = {+ rawDocument?: { content: string; mimeType: string; displayName?: string };+ gcsDocument?: { gcsUri: string; mimeType: string };+ fieldMask?: string;+ imagelessMode?: boolean;+ processOptions?: Record<string, unknown>;+ labels?: Record<string, string>;+};++export type GoogleDocumentAiProcessResponse = {+ document?: GoogleDocumentAiDocument;+ humanReviewStatus?: Record<string, unknown>;+};++export type GoogleDocumentAiTextAnchor = {+ textSegments?: { startIndex?: string | number; endIndex?: string | number }[];+ content?: string;+};++export type GoogleDocumentAiLayout = {+ textAnchor?: GoogleDocumentAiTextAnchor;+ confidence?: number;+};++export type GoogleDocumentAiEntity = {+ type?: string;+ mentionText?: string;+ textAnchor?: GoogleDocumentAiTextAnchor;+ confidence?: number;+ normalizedValue?: Record<string, unknown> & { text?: string };+ id?: string;+ pageAnchor?: { pageRefs?: { page?: string | number }[] };+ properties?: GoogleDocumentAiEntity[];+};++export type GoogleDocumentAiPage = {+ pageNumber?: number;+ formFields?: {+ fieldName?: GoogleDocumentAiLayout;+ fieldValue?: GoogleDocumentAiLayout;+ valueType?: string;+ }[];+ tables?: {+ headerRows?: GoogleDocumentAiTableRow[];+ bodyRows?: GoogleDocumentAiTableRow[];+ }[];+ detectedLanguages?: { languageCode?: string; confidence?: number }[];+};++export type GoogleDocumentAiTableRow = { cells?: { layout?: GoogleDocumentAiLayout }[] };++export type GoogleDocumentAiFlatEntity = {+ type: string;+ mentionText: string;+ confidence: number | null;+ normalizedText: string | null;+ normalizedValue: Record<string, unknown> | null;+ id: string | null;+ page: number | null;+ properties: GoogleDocumentAiFlatEntity[];+};++export type GoogleDocumentAiFormField = {+ page: number;+ name: string;+ value: string;+ nameConfidence: number | null;+ valueConfidence: number | null;+ valueType: string | null;+};++export type GoogleDocumentAiTable = { page: number; headerRows: string[][]; bodyRows: string[][] };++export type GoogleDocumentAiDetectedLanguage = { languageCode: string; confidence: number | null };++export type GoogleDocumentAiProcessedDocument = {+ text: string;+ mimeType: string | null;+ pageCount: number;+ languages: GoogleDocumentAiDetectedLanguage[];+ entities: GoogleDocumentAiFlatEntity[];+ formFields: GoogleDocumentAiFormField[];+ tables: GoogleDocumentAiTable[];+ document?: GoogleDocumentAiDocument;+};diff --git a/packages/pieces/community/google-document-ai/src/lib/common/utils.ts b/packages/pieces/community/google-document-ai/src/lib/common/utils.tsnew file mode 100644index 00000000000..73a0c9cc4fe--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/common/utils.ts@@ -0,0 +1,389 @@+import type {+ GoogleDocumentAiDetectedLanguage,+ GoogleDocumentAiDocument,+ GoogleDocumentAiEntity,+ GoogleDocumentAiFlatEntity,+ GoogleDocumentAiFormField,+ GoogleDocumentAiPage,+ GoogleDocumentAiProcessedDocument,+ GoogleDocumentAiResolvedAuth,+ GoogleDocumentAiTable,+ GoogleDocumentAiTableRow,+ GoogleDocumentAiTextAnchor,+} from './types';++function parentOf({+ auth,+}: {+ auth: Pick<GoogleDocumentAiResolvedAuth, 'projectId' | 'location'>;+}): string {+ return `projects/${auth.projectId}/locations/${auth.location}`;+}++function processorResourceName({+ auth,+ processor,+ version,+}: {+ auth: Pick<GoogleDocumentAiResolvedAuth, 'projectId' | 'location'>;+ processor: string;+ version?: string;+}): string {+ const trimmed = String(processor ?? '').trim();+ if (trimmed === '') {+ throw new Error('Pick a processor.');+ }+ let name: string;+ if (trimmed.startsWith('projects/')) {+ if (+ !/^projects\/[^/]+\/locations\/[^/]+\/processors\/[^/]+(\/processorVersions\/[^/]+)?$/.test(+ trimmed,+ )+ ) {+ throw new Error(+ `"${trimmed}" is not a processor resource name (expected projects/<project>/locations/<location>/processors/<id>).`,+ );+ }+ name = trimmed;+ } else if (/^[a-z0-9]+$/i.test(trimmed)) {+ name = `${parentOf({ auth })}/processors/${trimmed}`;+ } else {+ throw new Error(`"${trimmed}" is not a processor id or resource name.`);+ }+ const v = String(version ?? '').trim();+ if (v === '') return name;+ if (name.includes('/processorVersions/')) {+ throw new Error(+ 'The processor name already carries a version; leave Processor Version empty or pass the bare processor.',+ );+ }+ return v.startsWith('projects/')+ ? v+ : `${name}/processorVersions/${v.replace(/^processorVersions\//, '')}`;+}++function mimeTypeFor({+ explicit,+ names,+}: {+ explicit: string | undefined;+ names: (string | undefined)[];+}): string {+ const given = String(explicit ?? '').trim();+ if (given !== '') return given;+ for (const name of names) {+ const ext = extensionOf(String(name ?? '').trim());+ if (ext && MIME_BY_EXTENSION[ext]) return MIME_BY_EXTENSION[ext];+ }+ throw new Error(+ `Could not tell the file type from the name; fill in MIME Type (one of ${supportedMimeTypes()}).`,+ );+}++function supportedMimeTypes(): string {+ return [...new Set(Object.values(MIME_BY_EXTENSION))].join(', ');+}++function parsePageSelection(value: string | undefined): number[] | undefined {+ const text = String(value ?? '').trim();+ if (text === '') return undefined;+ const pages = new Set<number>();+ for (const part of text.split(',')) {+ const piece = part.trim();+ if (piece === '') continue;+ const range = /^(\d+)\s*-\s*(\d+)$/.exec(piece);+ const single = /^\d+$/.test(piece);+ if (!range && !single) {+ throw new Error(`Pages must be numbers or ranges like "1,3-5"; got "${piece}".`);+ }+ const [from, to] = range+ ? [Number(range[1]), Number(range[2])]+ : [Number(piece), Number(piece)];+ if (!Number.isSafeInteger(from) || !Number.isSafeInteger(to)) {+ throw new Error(`Invalid page "${piece}": the page number is too large.`);+ }+ if (from < 1 || to < from) {+ throw new Error(`Invalid page range "${piece}": pages start at 1 and ranges go upwards.`);+ }+ for (let p = from; p <= to; p++) {+ pages.add(p);+ if (pages.size > MAX_SELECTED_PAGES) {+ throw new Error(+ `Too many pages selected: pick at most ${MAX_SELECTED_PAGES} (online processing takes up to 15 pages, 30 with Imageless Mode). For longer documents use Custom API Call with batchProcess.`,+ );+ }+ }+ }+ return [...pages].sort((a, b) => a - b);+}++function textOf({+ chars,+ anchor,+}: {+ chars: string[];+ anchor: GoogleDocumentAiTextAnchor | undefined;+}): string {+ if (!anchor) return '';+ if (anchor.content) return anchor.content.trim();+ const segments = anchor.textSegments ?? [];+ if (segments.length === 0 || chars.length === 0) return '';+ return segments+ .map((s) => chars.slice(Number(s.startIndex ?? 0), Number(s.endIndex ?? chars.length)).join(''))+ .join('')+ .trim();+}++function splitDocumentText(text: string | undefined): string[] {+ return Array.from(text ?? '');+}++function flattenEntities({+ entities,+ chars,+}: {+ entities: GoogleDocumentAiEntity[] | undefined;+ chars: string[];+}): GoogleDocumentAiFlatEntity[] {+ if (!Array.isArray(entities)) return [];+ return entities.map((e) => {+ const { text: normalizedText, ...rest } = e.normalizedValue ?? {};+ const pageRef = e.pageAnchor?.pageRefs?.[0];+ return {+ type: e.type ?? '',+ mentionText: e.mentionText ?? textOf({ chars, anchor: e.textAnchor }),+ confidence: typeof e.confidence === 'number' ? e.confidence : null,+ normalizedText: typeof normalizedText === 'string' ? normalizedText : null,+ normalizedValue: e.normalizedValue+ ? { ...rest, ...(normalizedText !== undefined ? { text: normalizedText } : {}) }+ : null,+ id: e.id ?? null,+ page: pageRef === undefined ? null : Number(pageRef.page ?? 0) + 1,+ properties: flattenEntities({ entities: e.properties, chars }),+ };+ });+}++function formFieldsOf({+ pages,+ chars,+ selectedPages,+}: {+ pages: GoogleDocumentAiPage[] | undefined;+ chars: string[];+ selectedPages?: number[];+}): GoogleDocumentAiFormField[] {+ if (!Array.isArray(pages)) return [];+ return pages.flatMap((page, index) =>+ (page.formFields ?? []).map((f) => ({+ page: pageLabel({ page, index, selectedPages }),+ name: textOf({ chars, anchor: f.fieldName?.textAnchor }),+ value: textOf({ chars, anchor: f.fieldValue?.textAnchor }),+ nameConfidence: typeof f.fieldName?.confidence === 'number' ? f.fieldName.confidence : null,+ valueConfidence:+ typeof f.fieldValue?.confidence === 'number' ? f.fieldValue.confidence : null,+ valueType: f.valueType ?? null,+ })),+ );+}++function tablesOf({+ pages,+ chars,+ selectedPages,+}: {+ pages: GoogleDocumentAiPage[] | undefined;+ chars: string[];+ selectedPages?: number[];+}): GoogleDocumentAiTable[] {+ if (!Array.isArray(pages)) return [];+ return pages.flatMap((page, index) =>+ (page.tables ?? []).map((t) => ({+ page: pageLabel({ page, index, selectedPages }),+ headerRows: rowsToText({ rows: t.headerRows, chars }),+ bodyRows: rowsToText({ rows: t.bodyRows, chars }),+ })),+ );+}++function languagesOf(+ pages: GoogleDocumentAiPage[] | undefined,+): GoogleDocumentAiDetectedLanguage[] {+ if (!Array.isArray(pages)) return [];+ const best = new Map<string, number | null>();+ for (const page of pages) {+ for (const lang of page.detectedLanguages ?? []) {+ if (!lang.languageCode) continue;+ const current = best.get(lang.languageCode);+ const confidence = typeof lang.confidence === 'number' ? lang.confidence : null;+ if (+ current === undefined ||+ (confidence !== null && (current === null || confidence > current))+ )+ best.set(lang.languageCode, confidence);+ }+ }+ return [...best.entries()]+ .map(([languageCode, confidence]) => ({ languageCode, confidence }))+ .sort((a, b) => (b.confidence ?? -1) - (a.confidence ?? -1));+}++function summarizeDocument({+ document,+ includeFull,+ selectedPages,+}: {+ document: GoogleDocumentAiDocument | undefined;+ includeFull: boolean;+ selectedPages?: number[];+}): GoogleDocumentAiProcessedDocument {+ const doc: GoogleDocumentAiDocument = document ?? {};+ const text = typeof doc.text === 'string' ? doc.text : '';+ const pages = Array.isArray(doc.pages) ? doc.pages : [];+ const chars = splitDocumentText(text);+ return {+ text,+ mimeType: typeof doc.mimeType === 'string' ? doc.mimeType : null,+ pageCount: pages.length,+ languages: languagesOf(pages),+ entities: flattenEntities({ entities: doc.entities, chars }),+ formFields: formFieldsOf({ pages, chars, selectedPages }),+ tables: tablesOf({ pages, chars, selectedPages }),+ ...(includeFull ? { document: doc } : {}),+ };+}++function effectiveFieldMask({ fieldMask }: { fieldMask: string | undefined }): string {+ const paths = [+ ...new Set(+ String(fieldMask ?? '')+ .split(',')+ .map((p) => p.trim())+ .filter((p) => p !== ''),+ ),+ ];+ if (paths.length === 0) return DEFAULT_FIELD_MASK;+ if (+ !paths.includes('pages') &&+ !paths.includes('pages.pageNumber') &&+ paths.some(needsPageNumber)+ )+ paths.push('pages.pageNumber');+ if (!paths.includes('text') && paths.some(needsDocumentText)) paths.push('text');+ return paths.join(',');+}++function needsPageNumber(path: string): boolean {+ const [root, field] = path.split('.');+ return root === 'pages' && field !== undefined && PAGE_LABELLED_SEGMENTS.includes(field);+}++function pageLabel({+ page,+ index,+ selectedPages,+}: {+ page: GoogleDocumentAiPage;+ index: number;+ selectedPages: number[] | undefined;+}): number {+ if (typeof page.pageNumber === 'number') return page.pageNumber;+ return selectedPages?.[index] ?? index + 1;+}++function needsDocumentText(path: string): boolean {+ if (path === 'entities' || path.startsWith('entities.') || path === 'pages') return true;+ if (!path.startsWith('pages.')) return false;+ return path+ .split('.')+ .slice(1)+ .some((segment) => TEXT_ANCHORED_PAGE_SEGMENTS.includes(segment));+}++function extensionOf(name: string): string | undefined {+ const base = pathOf(name).split('/').pop() ?? '';+ if (!base.includes('.')) return undefined;+ return base.split('.').pop()?.toLowerCase();+}++function pathOf(name: string): string {+ if (/^gs:\/\//i.test(name)) return name.slice('gs://'.length).split('/').slice(1).join('/');+ if (!/^https?:\/\//i.test(name)) return name;+ try {+ return new URL(name).pathname;+ } catch {+ return name;+ }+}++function rowsToText({+ rows,+ chars,+}: {+ rows: GoogleDocumentAiTableRow[] | undefined;+ chars: string[];+}): string[][] {+ return (rows ?? []).map((row) =>+ (row.cells ?? []).map((cell) => textOf({ chars, anchor: cell.layout?.textAnchor })),+ );+}++const MIME_BY_EXTENSION: Record<string, string> = {+ pdf: 'application/pdf',+ gif: 'image/gif',+ tif: 'image/tiff',+ tiff: 'image/tiff',+ jpg: 'image/jpeg',+ jpeg: 'image/jpeg',+ png: 'image/png',+ bmp: 'image/bmp',+ webp: 'image/webp',+ html: 'text/html',+ htm: 'text/html',+ docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',+ pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',+ xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',+ xlsm: 'application/vnd.ms-excel.sheet.macroenabled.12',+};++const DEFAULT_FIELD_MASK =+ 'text,mimeType,entities,pages.pageNumber,pages.formFields,pages.tables,pages.detectedLanguages';++export const googleDocumentAiUtils = {+ parentOf,+ processorResourceName,+ mimeTypeFor,+ parsePageSelection,+ textOf,+ splitDocumentText,+ flattenEntities,+ formFieldsOf,+ tablesOf,+ languagesOf,+ summarizeDocument,+ effectiveFieldMask,+ DEFAULT_FIELD_MASK,+};++const MAX_SELECTED_PAGES = 30;++const PAGE_LABELLED_SEGMENTS = [+ 'formFields',+ 'tables',+ 'paragraphs',+ 'lines',+ 'tokens',+ 'blocks',+ 'layout',+];++const TEXT_ANCHORED_PAGE_SEGMENTS = [+ 'formFields',+ 'tables',+ 'paragraphs',+ 'lines',+ 'tokens',+ 'blocks',+ 'layout',+ 'textAnchor',+];diff --git a/packages/pieces/community/google-document-ai/src/lib/output-schemas.ts b/packages/pieces/community/google-document-ai/src/lib/output-schemas.tsnew file mode 100644index 00000000000..e392902c780--- /dev/null+++ b/packages/pieces/community/google-document-ai/src/lib/output-schemas.ts@@ -0,0 +1,92 @@+import { OutputSchema } from '@activepieces/pieces-framework';++const entityFields: OutputSchema['fields'] = [+ { key: 'type', label: 'Type' },+ { key: 'mentionText', label: 'Text' },+ { key: 'confidence', label: 'Confidence', format: 'number' },+ { key: 'normalizedText', label: 'Normalized Text' },+ {+ key: 'normalizedValue',+ label: 'Normalized Value',+ description:+ 'Typed value Google derived (money, date, address…); its keys depend on the entity type',+ },+ { key: 'id', label: 'Entity ID' },+ { key: 'page', label: 'Page', format: 'number' },+];++export const processDocumentOutputSchema: OutputSchema = {+ fields: [+ { key: 'processor', label: 'Processor' },+ {+ key: 'text',+ label: 'Text',+ description: 'Full text of the document as read by the processor',+ },+ { key: 'mimeType', label: 'MIME Type' },+ { key: 'pageCount', label: 'Page Count', format: 'number' },+ {+ key: 'languages',+ label: 'Languages',+ labelKey: 'languageCode',+ listItems: [+ { key: 'languageCode', label: 'Language Code' },+ { key: 'confidence', label: 'Confidence', format: 'number' },+ ],+ },+ {+ key: 'entities',+ label: 'Entities',+ description: 'Filled by specialized processors (invoice, expense, ID) and Custom Extractors',+ labelKey: 'type',+ listItems: [+ ...entityFields,+ {+ key: 'properties',+ label: 'Properties',+ labelKey: 'type',+ listItems: entityFields,+ },+ ],+ },+ {+ key: 'formFields',+ label: 'Form Fields',+ description: 'Key and value pairs found by the Form Parser',+ labelKey: 'name',+ listItems: [+ { key: 'page', label: 'Page', format: 'number' },+ { key: 'name', label: 'Name' },+ { key: 'value', label: 'Value' },+ { key: 'nameConfidence', label: 'Name Confidence', format: 'number' },+ { key: 'valueConfidence', label: 'Value Confidence', format: 'number' },+ { key: 'valueType', label: 'Value Type' },+ ],+ },+ {+ key: 'tables',+ label: 'Tables',+ description: 'Tables found by the Form Parser, each cell as plain text',+ listItems: [+ { key: 'page', label: 'Page', format: 'number' },+ { key: 'headerRows', label: 'Header Rows' },+ { key: 'bodyRows', label: 'Body Rows' },+ ],+ },+ {+ key: 'humanReviewStatus',+ label: 'Human Review Status',+ description: 'Only present when the processor has human review configured',+ children: [+ { key: 'state', label: 'State' },+ { key: 'stateMessage', label: 'State Message' },+ { key: 'humanReviewOperation', label: 'Human Review Operation' },+ ],+ },+ {+ key: 'document',+ label: 'Full Document',+ description: 'Raw Document AI document, only present when Include Full Document is on',+ },+ ],+};diff --git a/packages/pieces/community/google-document-ai/test/lib/actions/custom-api-call.test.ts b/packages/pieces/community/google-document-ai/test/lib/actions/custom-api-call.test.tsnew file mode 100644index 00000000000..9a3c0bbb5b0--- /dev/null+++ b/packages/pieces/community/google-document-ai/test/lib/actions/custom-api-call.test.ts@@ -0,0 +1,35 @@+import { httpClient } from '@activepieces/pieces-common';+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { googleDocumentAi } from '../../../src/index';++describe('custom API call', () => {+ const sendRequest = vi.spyOn(httpClient, 'sendRequest');+ const customApiCall = googleDocumentAi.actions()['custom_api_call'];+ const ctx = (url: string) => ({ auth: { type: 'OAUTH2', access_token: 'ya29.secret', props: { projectId: 'p', location: 'eu' } }, propsValue: { url: { url }, method: 'GET', headers: {}, queryParams: {} } }) as never;++ beforeEach(() => {+ sendRequest.mockReset();+ sendRequest.mockResolvedValue({ status: 200, headers: {}, body: {} });+ });++ it('should refuse an absolute URL on another host without sending a token', async () => {+ await expect(customApiCall.run(ctx('https://attacker.example.com/v1/collect'))).rejects.toThrow(+ "only sends the connection's Google credentials to https://eu-documentai.googleapis.com, but the URL points to https://attacker.example.com"+ );+ await expect(customApiCall.run(ctx('https://us-documentai.googleapis.com/v1/projects/p/locations/us/processors'))).rejects.toThrow('points to https://us-documentai.googleapis.com');++ expect(sendRequest).not.toHaveBeenCalled();+ });++ it.each(['https://eu-documentai.googleapis.com/v1/projects/p/locations/eu/processors', '/v1/projects/p/locations/eu/processors'])('should send the token to %s on the connection host', async (url) => {+ await customApiCall.run(ctx(url));++ expect(sendRequest).toHaveBeenCalledWith(+ expect.objectContaining({+ url: 'https://eu-documentai.googleapis.com/v1/projects/p/locations/eu/processors',+ headers: expect.objectContaining({ Authorization: 'Bearer ya29.secret' }),+ })+ );+ });+});diff --git a/packages/pieces/community/google-document-ai/test/lib/actions/process-document.test.ts b/packages/pieces/community/google-document-ai/test/lib/actions/process-document.test.tsnew file mode 100644index 00000000000..28c9c5075ec--- /dev/null+++ b/packages/pieces/community/google-document-ai/test/lib/actions/process-document.test.ts@@ -0,0 +1,94 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++const { googleDocumentAiApi } = await import('../../../src/lib/common/api');+const { googleDocumentAiUtils } = await import('../../../src/lib/common/utils');+const { processDocumentAction: processDocument } = await import('../../../src/lib/actions/process-document');++const process = vi.spyOn(googleDocumentAiApi, 'processDocument');+const resolveAuth = vi.spyOn(googleDocumentAiApi, 'resolveAuth');+const { DEFAULT_FIELD_MASK } = googleDocumentAiUtils;++const AUTH = { accessToken: 't', projectId: 'my-project', location: 'us' };+const PROCESSOR = 'projects/my-project/locations/us/processors/abc';+const ctx = (propsValue: Record<string, unknown>) => ({ auth: { type: 'CUSTOM_AUTH', props: {} }, propsValue }) as never;+const file = { filename: 'invoice.pdf', extension: 'pdf', base64: 'JVBERi0=' };++beforeEach(() => {+ process.mockReset();+ resolveAuth.mockReset();+ resolveAuth.mockResolvedValue(AUTH);+ process.mockResolvedValue({ document: { text: 'Hello', mimeType: 'application/pdf', pages: [{ pageNumber: 1 }] } });+});++describe('processDocument', () => {+ it('should send a raw document with inferred MIME type, default field mask and imageless mode, and flatten the result', async () => {+ const output = await processDocument.run(ctx({ processor: PROCESSOR, source: 'file', file }));++ expect(process).toHaveBeenCalledWith({ auth: AUTH, processorName: PROCESSOR, request: {+ rawDocument: { content: 'JVBERi0=', mimeType: 'application/pdf', displayName: 'invoice.pdf' },+ imagelessMode: true,+ fieldMask: DEFAULT_FIELD_MASK,+ } });+ expect(output).toEqual({ processor: PROCESSOR, text: 'Hello', mimeType: 'application/pdf', pageCount: 1, languages: [], entities: [], formFields: [], tables: [] });+ });++ it('should accept a bare processor id with a version, page selection, explicit MIME type and the full document', async () => {+ process.mockResolvedValue({ document: { text: 'x', pages: [] }, humanReviewStatus: { state: 'SKIPPED' } });++ const output = await processDocument.run(+ ctx({ processor: 'abc', processorVersion: 'pretrained-ocr-v2.0-2023-06-02', source: 'file', file: { ...file, filename: 'scan' }, mimeType: 'image/png', pages: '1,3-4', imagelessMode: false, includeFullDocument: true, fieldMask: 'pages.formFields' })+ );++ expect(process).toHaveBeenCalledWith({ auth: AUTH, processorName: `${PROCESSOR}/processorVersions/pretrained-ocr-v2.0-2023-06-02`, request: {+ rawDocument: { content: 'JVBERi0=', mimeType: 'image/png', displayName: 'scan' },+ processOptions: { individualPageSelector: { pages: [1, 3, 4] } },+ } });+ expect(output).toMatchObject({ document: { text: 'x', pages: [] }, humanReviewStatus: { state: 'SKIPPED' } });+ });++ it('should add text to a custom field mask that needs it and normalize the mask', async () => {+ await processDocument.run(ctx({ processor: PROCESSOR, source: 'file', file, fieldMask: ' pages.formFields, pages.formFields ,' }));++ expect(process).toHaveBeenCalledWith({ auth: AUTH, processorName: PROCESSOR, request: {+ rawDocument: { content: 'JVBERi0=', mimeType: 'application/pdf', displayName: 'invoice.pdf' },+ imagelessMode: true,+ fieldMask: 'pages.formFields,pages.pageNumber,text',+ } });+ });++ it('should label form fields with the selected page number', async () => {+ process.mockResolvedValue({ document: { text: 'Nome: João', pages: [{ formFields: [{ fieldName: { textAnchor: { content: 'Nome:' } }, fieldValue: { textAnchor: { content: 'João' } } }] }] } });++ const output = await processDocument.run(ctx({ processor: PROCESSOR, source: 'file', file, pages: '5', fieldMask: 'pages.formFields' }));++ expect(process).toHaveBeenCalledWith(expect.objectContaining({ request: expect.objectContaining({ processOptions: { individualPageSelector: { pages: [5] } }, fieldMask: 'pages.formFields,pages.pageNumber,text' }) }));+ expect(output).toMatchObject({ formFields: [{ page: 5, name: 'Nome:', value: 'João' }] });+ });++ it('should send a custom field mask that already has text or does not need it unchanged', async () => {+ await processDocument.run(ctx({ processor: PROCESSOR, source: 'file', file, fieldMask: 'mimeType' }));+ await processDocument.run(ctx({ processor: PROCESSOR, source: 'file', file, fieldMask: 'text,entities' }));++ expect(process).toHaveBeenNthCalledWith(1, expect.objectContaining({ request: expect.objectContaining({ fieldMask: 'mimeType' }) }));+ expect(process).toHaveBeenNthCalledWith(2, expect.objectContaining({ request: expect.objectContaining({ fieldMask: 'text,entities' }) }));+ });++ it('should send a Cloud Storage document with the MIME type taken from the URI', async () => {+ await processDocument.run(ctx({ processor: PROCESSOR, source: 'gcs', gcsUri: ' gs://bucket/in/contract.tiff ' }));++ expect(process).toHaveBeenCalledWith({ auth: AUTH, processorName: PROCESSOR, request: { gcsDocument: { gcsUri: 'gs://bucket/in/contract.tiff', mimeType: 'image/tiff' }, imagelessMode: true, fieldMask: DEFAULT_FIELD_MASK } });+ });++ it('should refuse a missing file, a bad URI and an unknown file type before calling Google', async () => {+ await expect(processDocument.run(ctx({ processor: PROCESSOR, source: 'file' }))).rejects.toThrow('Pick a file to process');+ await expect(processDocument.run(ctx({ processor: PROCESSOR, source: 'gcs', gcsUri: 'http://x' }))).rejects.toThrow('gs://bucket/path/file.pdf');+ await expect(processDocument.run(ctx({ processor: PROCESSOR, source: 'file', file: { filename: 'blob', base64: 'AA==' } }))).rejects.toThrow('fill in MIME Type');+ expect(process).not.toHaveBeenCalled();+ });++ it('should surface a document-level error from Google', async () => {+ process.mockResolvedValue({ document: { error: { code: 3, message: 'Unsupported input file format.' } } });++ await expect(processDocument.run(ctx({ processor: PROCESSOR, source: 'file', file }))).rejects.toThrow('Document AI could not process the document: Unsupported input file format.');+ });+});diff --git a/packages/pieces/community/google-document-ai/test/setup.ts b/packages/pieces/community/google-document-ai/test/setup.tsnew file mode 100644index 00000000000..0e24d4494e9--- /dev/null+++ b/packages/pieces/community/google-document-ai/test/setup.ts@@ -0,0 +1,9 @@+import { beforeEach, vi } from 'vitest';++const blockNetwork = vi.fn(async (input: unknown): Promise<Response> => {+ throw new Error(`Unexpected network call in tests: ${String(input)}`);+});++beforeEach(() => {+ vi.stubGlobal('fetch', blockNetwork);+});diff --git a/packages/pieces/community/google-document-ai/tsconfig.json b/packages/pieces/community/google-document-ai/tsconfig.jsonnew file mode 100644index 00000000000..71bc5814f5d--- /dev/null+++ b/packages/pieces/community/google-document-ai/tsconfig.json@@ -0,0 +1,15 @@+{+ "extends": "../../../../tsconfig.base.json",+ "compilerOptions": {+ "module": "commonjs",+ "forceConsistentCasingInFileNames": true,+ "strict": true,+ "noImplicitOverride": true,+ "noPropertyAccessFromIndexSignature": true,+ "noImplicitReturns": true,+ "noFallthroughCasesInSwitch": true+ },+ "files": [],+ "include": [],+ "references": [{ "path": "./tsconfig.lib.json" }]+}diff --git a/packages/pieces/community/google-document-ai/tsconfig.lib.json b/packages/pieces/community/google-document-ai/tsconfig.lib.jsonnew file mode 100644index 00000000000..a648eb45d7f--- /dev/null+++ b/packages/pieces/community/google-document-ai/tsconfig.lib.json@@ -0,0 +1,13 @@+{+ "extends": "./tsconfig.json",+ "compilerOptions": {+ "rootDir": ".",+ "baseUrl": ".",+ "paths": {},+ "outDir": "./dist",+ "declaration": true,+ "types": ["node"]+ },+ "include": ["src/**/*.ts"],+ "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]+}diff --git a/packages/pieces/community/google-document-ai/vitest.config.ts b/packages/pieces/community/google-document-ai/vitest.config.tsnew file mode 100644index 00000000000..48da723763b--- /dev/null+++ b/packages/pieces/community/google-document-ai/vitest.config.ts@@ -0,0 +1,20 @@+import path from 'path'+import { defineConfig } from 'vitest/config'++const repoRoot = path.resolve(__dirname, '../../../..')++export default defineConfig({+ root: __dirname,+ test: {+ globals: true,+ environment: 'node',+ include: ['test/**/*.test.ts'],+ setupFiles: ['test/setup.ts'],+ },+ resolve: {+ alias: {+ '@activepieces/pieces-framework': path.resolve(repoRoot, 'packages/pieces/framework/src/index.ts'),+ '@activepieces/pieces-common': path.resolve(repoRoot, 'packages/pieces/common/src/index.ts'),+ },+ },+})diff --git a/tsconfig.base.json b/tsconfig.base.jsonindex 1d8f8104aff..fa16eff4b5b 100644--- a/tsconfig.base.json+++ b/tsconfig.base.json@@ -226,6 +226,9 @@"@activepieces/piece-google-translate": ["packages/pieces/community/google-translate/src/index.ts"],+ "@activepieces/piece-google-document-ai": [+ "packages/pieces/community/google-document-ai/src/index.ts"+ ],"@activepieces/piece-rounded-studio": ["packages/pieces/community/rounded-studio/src/index.ts"],