| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { serveProbes } from "../../bindingHost.ts"; |
| 4 | |
| 5 | /** OCR processor GetProcessor reads and Process runs. */ |
| 6 | export const OcrBind = GCP.DocumentAI.Processor("OcrBind", { |
| 7 | location: "us", |
| 8 | type: "OCR_PROCESSOR", |
| 9 | displayName: "ocr-bind", |
| 10 | }); |
| 11 | |
| 12 | /** Schema GetSchema reads. */ |
| 13 | export const Invoice = GCP.DocumentAI.Schema("Invoice", { |
| 14 | location: "us", |
| 15 | displayName: "invoice-bind", |
| 16 | labels: { env: "test" }, |
| 17 | }); |
| 18 | |
| 19 | /** Schema version GetSchemaVersion reads. */ |
| 20 | export const InvoiceV1 = Effect.gen(function* () { |
| 21 | const schema = yield* Invoice; |
| 22 | return yield* GCP.DocumentAI.SchemasSchemaVersion("V1", { |
| 23 | schema: schema.name, |
| 24 | location: "us", |
| 25 | displayName: "v1", |
| 26 | documentSchema: { |
| 27 | displayName: "invoice", |
| 28 | description: "invoice fields", |
| 29 | entityTypes: [ |
| 30 | { |
| 31 | name: "invoice", |
| 32 | baseTypes: ["document"], |
| 33 | properties: [ |
| 34 | { |
| 35 | name: "invoice_id", |
| 36 | valueType: "string", |
| 37 | occurrenceType: "OPTIONAL_ONCE", |
| 38 | }, |
| 39 | ], |
| 40 | }, |
| 41 | ], |
| 42 | }, |
| 43 | labels: { env: "test" }, |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | /** One-page PDF reading "Hello". */ |
| 48 | export const MINIMAL_PDF = `%PDF-1.1 |
| 49 | 1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj |
| 50 | 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj |
| 51 | 3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 4 0 R/Resources<</Font<</F1 5 0 R>>>>>>endobj |
| 52 | 4 0 obj<</Length 44>>stream |
| 53 | BT /F1 12 Tf 20 100 Td (Hello) Tj ET |
| 54 | endstream |
| 55 | endobj |
| 56 | 5 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj |
| 57 | xref |
| 58 | 0 6 |
| 59 | 0000000000 65535 f |
| 60 | 0000000009 00000 n |
| 61 | 0000000052 00000 n |
| 62 | 0000000101 00000 n |
| 63 | 0000000216 00000 n |
| 64 | 0000000309 00000 n |
| 65 | trailer<</Size 6/Root 1 0 R>> |
| 66 | startxref |
| 67 | 376 |
| 68 | %%EOF |
| 69 | `; |
| 70 | |
| 71 | /** |
| 72 | * Effect-native Cloud Run service exercising every Document AI binding as |
| 73 | * its own runtime service account. Deployed from {@link ../Bindings.test.ts}. |
| 74 | */ |
| 75 | export default class DocumentAIBindingsHost extends GCP.Function<DocumentAIBindingsHost>()( |
| 76 | "DocumentAIBindingsHost", |
| 77 | { main: import.meta.url, invokerIamDisabled: true }, |
| 78 | Effect.gen(function* () { |
| 79 | const getProcessor = yield* GCP.DocumentAI.GetProcessor(OcrBind); |
| 80 | const process = yield* GCP.DocumentAI.Process(OcrBind); |
| 81 | const getSchema = yield* GCP.DocumentAI.GetSchema(Invoice); |
| 82 | const getSchemaVersion = yield* GCP.DocumentAI.GetSchemaVersion(InvoiceV1); |
| 83 | |
| 84 | return { |
| 85 | fetch: serveProbes({ |
| 86 | getProcessor: getProcessor(), |
| 87 | process: process({ |
| 88 | body: { |
| 89 | rawDocument: { |
| 90 | content: btoa(MINIMAL_PDF), |
| 91 | mimeType: "application/pdf", |
| 92 | }, |
| 93 | skipHumanReview: true, |
| 94 | }, |
| 95 | }).pipe( |
| 96 | Effect.map((processed) => ({ |
| 97 | text: processed.document?.text, |
| 98 | pages: processed.document?.pages?.length, |
| 99 | })), |
| 100 | ), |
| 101 | getSchema: getSchema(), |
| 102 | getSchemaVersion: getSchemaVersion(), |
| 103 | }), |
| 104 | }; |
| 105 | }).pipe( |
| 106 | Effect.provide(GCP.DocumentAI.GetProcessorHttp), |
| 107 | Effect.provide(GCP.DocumentAI.ProcessHttp), |
| 108 | Effect.provide(GCP.DocumentAI.GetSchemaHttp), |
| 109 | Effect.provide(GCP.DocumentAI.GetSchemaVersionHttp), |
| 110 | ), |
| 111 | ) {} |