mirror of
https://github.com/documenso/documenso
synced 2026-04-21 13:27:18 +00:00
Use Gemini to handle detection of recipients and fields within documents. Opt in using organisation or team settings. Replaces #2128 since the branch was cursed and would include dependencies that weren't even in the lock file. https://github.com/user-attachments/assets/e6cbb58f-62b9-4079-a9ae-7af5c4f2e4ec
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import { FieldType } from '@prisma/client';
|
|
import z from 'zod';
|
|
|
|
export const DETECTABLE_FIELD_TYPES = [
|
|
FieldType.SIGNATURE,
|
|
FieldType.INITIALS,
|
|
FieldType.NAME,
|
|
FieldType.EMAIL,
|
|
FieldType.DATE,
|
|
FieldType.TEXT,
|
|
FieldType.NUMBER,
|
|
FieldType.RADIO,
|
|
FieldType.CHECKBOX,
|
|
] as const;
|
|
|
|
export const ZDetectableFieldType = z.enum(DETECTABLE_FIELD_TYPES);
|
|
|
|
export const ZConfidenceLevel = z.enum(['low', 'medium-low', 'medium', 'medium-high', 'high']);
|
|
|
|
export type TConfidenceLevel = z.infer<typeof ZConfidenceLevel>;
|
|
|
|
/**
|
|
* Schema for a detected field's bounding box.
|
|
* All values are normalized to a 0-1000 scale relative to the page dimensions.
|
|
*/
|
|
const ZBox2DSchema = z.array(z.number().min(0).max(1000)).length(4);
|
|
|
|
/**
|
|
* Schema for a detected field.
|
|
*/
|
|
export const ZDetectedFieldSchema = z.object({
|
|
type: ZDetectableFieldType.describe(
|
|
`The field type based on nearby labels and visual appearance`,
|
|
),
|
|
recipientKey: z
|
|
.string()
|
|
.describe(
|
|
'Recipient identifier from nearby labels (e.g., "Tenant", "Landlord", "Buyer", "Seller"). Empty string if no recipient indicated.',
|
|
),
|
|
box2d: ZBox2DSchema.describe(
|
|
'Box2D [yMin, xMin, yMax, xMax] coordinates of the FILLABLE AREA only (exclude labels).',
|
|
),
|
|
confidence: ZConfidenceLevel.describe('The confidence in the detection'),
|
|
});
|
|
|
|
export type DetectedField = z.infer<typeof ZDetectedFieldSchema>;
|
|
|
|
export const ZSubmitDetectedFieldsInputSchema = z.object({
|
|
fields: z
|
|
.array(ZDetectedFieldSchema)
|
|
.describe('List of detected EMPTY fillable fields. Exclude pre-filled content and label text.'),
|
|
});
|
|
|
|
export type SubmitDetectedFieldsInput = z.infer<typeof ZSubmitDetectedFieldsInputSchema>;
|