1import * as GCP from "@/GCP";
2import * as Effect from "effect/Effect";
3import { serveProbes } from "../../bindingHost.ts";
4
5/** OCR processor GetProcessor reads and Process runs. */
6export const OcrBind = GCP.DocumentAI.Processor("OcrBind", {
7 location: "us",
8 type: "OCR_PROCESSOR",
9 displayName: "ocr-bind",
10});
11
12/** Schema GetSchema reads. */
13export const Invoice = GCP.DocumentAI.Schema("Invoice", {
14 location: "us",
15 displayName: "invoice-bind",
16 labels: { env: "test" },
17});
18
19/** Schema version GetSchemaVersion reads. */
20export 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". */
48export const MINIMAL_PDF = `%PDF-1.1
491 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
502 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj
513 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 4 0 R/Resources<</Font<</F1 5 0 R>>>>>>endobj
524 0 obj<</Length 44>>stream
53BT /F1 12 Tf 20 100 Td (Hello) Tj ET
54endstream
55endobj
565 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj
57xref
580 6
590000000000 65535 f
600000000009 00000 n
610000000052 00000 n
620000000101 00000 n
630000000216 00000 n
640000000309 00000 n
65trailer<</Size 6/Root 1 0 R>>
66startxref
67376
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 */
75export 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) {}