@rickcedwhat/playwright-smart-vision 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/configure.d.ts +21 -0
- package/dist/configure.d.ts.map +1 -0
- package/dist/configure.js +77 -0
- package/dist/configure.js.map +1 -0
- package/dist/element.d.ts +177 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +433 -0
- package/dist/element.js.map +1 -0
- package/dist/element.test.d.ts +2 -0
- package/dist/element.test.d.ts.map +1 -0
- package/dist/element.test.js +207 -0
- package/dist/element.test.js.map +1 -0
- package/dist/field-extractor.d.ts +63 -0
- package/dist/field-extractor.d.ts.map +1 -0
- package/dist/field-extractor.js +438 -0
- package/dist/field-extractor.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/ocr-overlay.d.ts +21 -0
- package/dist/ocr-overlay.d.ts.map +1 -0
- package/dist/ocr-overlay.js +102 -0
- package/dist/ocr-overlay.js.map +1 -0
- package/dist/ocr-step.d.ts +15 -0
- package/dist/ocr-step.d.ts.map +1 -0
- package/dist/ocr-step.js +41 -0
- package/dist/ocr-step.js.map +1 -0
- package/dist/ocr-test.d.ts +11 -0
- package/dist/ocr-test.d.ts.map +1 -0
- package/dist/ocr-test.js +35 -0
- package/dist/ocr-test.js.map +1 -0
- package/dist/playwright-helper.d.ts +64 -0
- package/dist/playwright-helper.d.ts.map +1 -0
- package/dist/playwright-helper.js +142 -0
- package/dist/playwright-helper.js.map +1 -0
- package/dist/screen-config.d.ts +91 -0
- package/dist/screen-config.d.ts.map +1 -0
- package/dist/screen-config.js +203 -0
- package/dist/screen-config.js.map +1 -0
- package/dist/screen-result.d.ts +103 -0
- package/dist/screen-result.d.ts.map +1 -0
- package/dist/screen-result.js +313 -0
- package/dist/screen-result.js.map +1 -0
- package/dist/typed-screen.d.ts +44 -0
- package/dist/typed-screen.d.ts.map +1 -0
- package/dist/typed-screen.js +72 -0
- package/dist/typed-screen.js.map +1 -0
- package/dist/types.d.ts +204 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/ocr.d.ts +52 -0
- package/dist/utils/ocr.d.ts.map +1 -0
- package/dist/utils/ocr.js +252 -0
- package/dist/utils/ocr.js.map +1 -0
- package/dist/utils/ocr.test.d.ts +2 -0
- package/dist/utils/ocr.test.d.ts.map +1 -0
- package/dist/utils/ocr.test.js +145 -0
- package/dist/utils/ocr.test.js.map +1 -0
- package/dist/utils/vision.d.ts +116 -0
- package/dist/utils/vision.d.ts.map +1 -0
- package/dist/utils/vision.js +340 -0
- package/dist/utils/vision.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ScreenComparison, ElementConfig } from './types.js';
|
|
2
|
+
import { ScreenResult } from './screen-result.js';
|
|
3
|
+
import type { Page } from '@playwright/test';
|
|
4
|
+
/**
|
|
5
|
+
* Type-safe screen configuration
|
|
6
|
+
* Extracts element names as literal types for compile-time safety
|
|
7
|
+
*/
|
|
8
|
+
export interface TypedScreenConfig<TElements extends readonly ElementConfig[]> {
|
|
9
|
+
name: string;
|
|
10
|
+
blankScreenPath: string;
|
|
11
|
+
elementConfigs: TElements;
|
|
12
|
+
baseDir?: string | undefined;
|
|
13
|
+
debug?: boolean | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Extract element names from config as literal union type
|
|
17
|
+
*/
|
|
18
|
+
type ExtractElementNames<T extends readonly ElementConfig[]> = T[number]['name'];
|
|
19
|
+
/**
|
|
20
|
+
* Type-safe screen result wrapper
|
|
21
|
+
* Provides compile-time checking of element names
|
|
22
|
+
*/
|
|
23
|
+
export declare class TypedScreenResult<TElements extends readonly ElementConfig[]> extends ScreenResult {
|
|
24
|
+
private elementNames;
|
|
25
|
+
constructor(comparison: ScreenComparison, elementNames: Set<string>, page?: Page);
|
|
26
|
+
/**
|
|
27
|
+
* Get an element by name with type safety
|
|
28
|
+
* TypeScript will error if you use a name that doesn't exist in the config
|
|
29
|
+
*/
|
|
30
|
+
element<TName extends ExtractElementNames<TElements>>(name: TName): ReturnType<ScreenResult['element']>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a type-safe screen configuration
|
|
34
|
+
* Use this instead of defineScreen for full type safety
|
|
35
|
+
*/
|
|
36
|
+
export declare function defineTypedScreen<const TElements extends readonly ElementConfig[]>(config: {
|
|
37
|
+
name: string;
|
|
38
|
+
baseDir: string;
|
|
39
|
+
blankScreen?: string;
|
|
40
|
+
elements: TElements;
|
|
41
|
+
debug?: boolean;
|
|
42
|
+
}): TypedScreenConfig<TElements>;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=typed-screen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-screen.d.ts","sourceRoot":"","sources":["../src/typed-screen.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;GAGG;AACH,MAAM,WAAW,iBAAiB,CAAC,SAAS,SAAS,SAAS,aAAa,EAAE;IAC3E,IAAI,EAAE,MAAM,CAAC;IACb,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,SAAS,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC7B;AAED;;GAEG;AACH,KAAK,mBAAmB,CAAC,CAAC,SAAS,SAAS,aAAa,EAAE,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjF;;;GAGG;AACH,qBAAa,iBAAiB,CAAC,SAAS,SAAS,SAAS,aAAa,EAAE,CAAE,SAAQ,YAAY;IAG3F,OAAO,CAAC,YAAY;gBADpB,UAAU,EAAE,gBAAgB,EACpB,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,EACjC,IAAI,CAAC,EAAE,IAAI;IAKb;;;OAGG;IACH,OAAO,CAAC,KAAK,SAAS,mBAAmB,CAAC,SAAS,CAAC,EAClD,IAAI,EAAE,KAAK,GACV,UAAU,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;CAGvC;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,CAAC,SAAS,SAAS,SAAS,aAAa,EAAE,EAChD,MAAM,EAAE;IACR,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,SAAS,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,GAAG,iBAAiB,CAAC,SAAS,CAAC,CA0D/B"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ScreenResult } from './screen-result.js';
|
|
2
|
+
/**
|
|
3
|
+
* Type-safe screen result wrapper
|
|
4
|
+
* Provides compile-time checking of element names
|
|
5
|
+
*/
|
|
6
|
+
export class TypedScreenResult extends ScreenResult {
|
|
7
|
+
elementNames;
|
|
8
|
+
constructor(comparison, elementNames, page) {
|
|
9
|
+
super(comparison, page);
|
|
10
|
+
this.elementNames = elementNames;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Get an element by name with type safety
|
|
14
|
+
* TypeScript will error if you use a name that doesn't exist in the config
|
|
15
|
+
*/
|
|
16
|
+
element(name) {
|
|
17
|
+
return super.element(name);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create a type-safe screen configuration
|
|
22
|
+
* Use this instead of defineScreen for full type safety
|
|
23
|
+
*/
|
|
24
|
+
export function defineTypedScreen(config) {
|
|
25
|
+
const path = require('path');
|
|
26
|
+
const blankScreenPath = path.join(config.baseDir, config.blankScreen || 'blank.png');
|
|
27
|
+
const elementConfigs = config.elements.map(el => {
|
|
28
|
+
const elementConfig = {
|
|
29
|
+
name: el.name,
|
|
30
|
+
type: el.type,
|
|
31
|
+
};
|
|
32
|
+
if ('template' in el && el.template) {
|
|
33
|
+
elementConfig.templatePath = path.join(config.baseDir, 'templates', el.template);
|
|
34
|
+
}
|
|
35
|
+
else if ('variants' in el && el.variants) {
|
|
36
|
+
elementConfig.variants = {};
|
|
37
|
+
for (const [variantName, variantConfig] of Object.entries(el.variants)) {
|
|
38
|
+
if (variantConfig && typeof variantConfig === 'object' && 'template' in variantConfig) {
|
|
39
|
+
elementConfig.variants[variantName] = {
|
|
40
|
+
template: path.join(config.baseDir, 'templates', variantConfig.template),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if ('section' in el && el.section) {
|
|
46
|
+
elementConfig.sectionTemplatePath = path.join(config.baseDir, 'templates', el.section);
|
|
47
|
+
}
|
|
48
|
+
else if (el.sectionTemplatePath) {
|
|
49
|
+
elementConfig.sectionTemplatePath = el.sectionTemplatePath;
|
|
50
|
+
}
|
|
51
|
+
if ('animated' in el && el.animated) {
|
|
52
|
+
elementConfig.animated = el.animated;
|
|
53
|
+
}
|
|
54
|
+
if ('options' in el && Array.isArray(el.options) && el.options.length) {
|
|
55
|
+
elementConfig.options = el.options;
|
|
56
|
+
}
|
|
57
|
+
return elementConfig;
|
|
58
|
+
});
|
|
59
|
+
const result = {
|
|
60
|
+
name: config.name,
|
|
61
|
+
blankScreenPath,
|
|
62
|
+
elementConfigs,
|
|
63
|
+
};
|
|
64
|
+
if (config.baseDir !== undefined) {
|
|
65
|
+
result.baseDir = config.baseDir;
|
|
66
|
+
}
|
|
67
|
+
if (config.debug !== undefined) {
|
|
68
|
+
result.debug = config.debug;
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=typed-screen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-screen.js","sourceRoot":"","sources":["../src/typed-screen.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAoBlD;;;GAGG;AACH,MAAM,OAAO,iBAA8D,SAAQ,YAAY;IAGnF;IAFV,YACE,UAA4B,EACpB,YAAyB,EACjC,IAAW;QAEX,KAAK,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QAHhB,iBAAY,GAAZ,YAAY,CAAa;IAInC,CAAC;IAED;;;OAGG;IACH,OAAO,CACL,IAAW;QAEX,OAAO,KAAK,CAAC,OAAO,CAAC,IAAc,CAAC,CAAC;IACvC,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAE/B,MAMD;IACC,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE7B,MAAM,eAAe,GAAG,IAAI,CAAC,IAAI,CAC/B,MAAM,CAAC,OAAO,EACd,MAAM,CAAC,WAAW,IAAI,WAAW,CAClC,CAAC;IAEF,MAAM,cAAc,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;QAC9C,MAAM,aAAa,GAAQ;YACzB,IAAI,EAAE,EAAE,CAAC,IAAI;YACb,IAAI,EAAE,EAAE,CAAC,IAAI;SACd,CAAC;QAEF,IAAI,UAAU,IAAI,EAAE,IAAI,EAAE,CAAC,QAAQ,EAAE,CAAC;YACpC,aAAa,CAAC,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,WAAW,EAAE,EAAE,CAAC,QAAkB,CAAC,CAAC;QAC7F,CAAC;aAAM,IAAI,UAAU,IAAI,EAAE,IAAI,EAAE,CAAC,QAAQ,EAAE,CAAC;YAC3C,aAAa,CAAC,QAA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IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAClC,CAAC;IAED,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;QAC/B,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IAC9B,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { FieldRead, OcrOverflow, OcrSwaps } from './utils/ocr.js';
|
|
2
|
+
/**
|
|
3
|
+
* Types of UI elements that can be extracted
|
|
4
|
+
*/
|
|
5
|
+
export declare enum ElementType {
|
|
6
|
+
FIELD = "field",// Text input, textarea
|
|
7
|
+
BUTTON = "button",// Button, submit button
|
|
8
|
+
CHECKBOX = "checkbox",// Checkbox
|
|
9
|
+
RADIO = "radio",// Radio button
|
|
10
|
+
LINK = "link",// Hyperlink
|
|
11
|
+
ICON = "icon",// Icon, status indicator
|
|
12
|
+
LABEL = "label",// Text label
|
|
13
|
+
DROPDOWN = "dropdown",// Select dropdown
|
|
14
|
+
TAB = "tab",// Tab navigation
|
|
15
|
+
TOGGLE = "toggle",// Toggle switch
|
|
16
|
+
MESSAGE = "message",// Error/success message
|
|
17
|
+
OTHER = "other"
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Variant configuration for elements with multiple states
|
|
21
|
+
*/
|
|
22
|
+
export interface ElementVariant {
|
|
23
|
+
/** Path to template for this variant */
|
|
24
|
+
template: string;
|
|
25
|
+
/** Optional: Additional metadata for this variant */
|
|
26
|
+
metadata?: Record<string, any>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Context provided to custom matcher functions
|
|
30
|
+
*/
|
|
31
|
+
export interface CustomMatcherContext {
|
|
32
|
+
/** Region from blank form/screen */
|
|
33
|
+
blankROI: any;
|
|
34
|
+
/** Region from filled form/screen */
|
|
35
|
+
filledROI: any;
|
|
36
|
+
/** Template that was matched to find this location */
|
|
37
|
+
templateROI: any;
|
|
38
|
+
/** Location where element was found */
|
|
39
|
+
location: Rect;
|
|
40
|
+
/** Element configuration */
|
|
41
|
+
config: ElementConfig;
|
|
42
|
+
/** Utility functions */
|
|
43
|
+
utils: {
|
|
44
|
+
/** Create difference image between two regions */
|
|
45
|
+
createDiffImage: (roi1: any, roi2: any, threshold?: number) => any;
|
|
46
|
+
/** Keep filled pixels that changed vs blank; unchanged pixels become white */
|
|
47
|
+
isolateChangedForOcr: (filled: any, blank: any, threshold?: number) => any;
|
|
48
|
+
/** Convert Mat to buffer for OCR */
|
|
49
|
+
matToBuffer: (mat: any) => Buffer;
|
|
50
|
+
/** Compare two regions and get pixel difference stats */
|
|
51
|
+
compareRegions: (roi1: any, roi2: any, threshold?: number, minDiffPixels?: number) => {
|
|
52
|
+
different: boolean;
|
|
53
|
+
diffPixelCount: number;
|
|
54
|
+
diffPercentage: number;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Result from custom matcher function
|
|
60
|
+
*/
|
|
61
|
+
export interface CustomMatcherResult {
|
|
62
|
+
/** Extracted value */
|
|
63
|
+
value: string;
|
|
64
|
+
/** Confidence score (0-1) */
|
|
65
|
+
confidence: number;
|
|
66
|
+
/** Whether element is in empty/default state */
|
|
67
|
+
isEmpty: boolean;
|
|
68
|
+
/** Optional: Additional metadata */
|
|
69
|
+
metadata?: Record<string, any> | undefined;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Custom matcher function type
|
|
73
|
+
*/
|
|
74
|
+
export type CustomMatcherFunction = (context: CustomMatcherContext) => Promise<CustomMatcherResult> | CustomMatcherResult;
|
|
75
|
+
/**
|
|
76
|
+
* Configuration for a single UI element to extract
|
|
77
|
+
*/
|
|
78
|
+
export interface ElementConfig {
|
|
79
|
+
/** Unique identifier for this element */
|
|
80
|
+
name: string;
|
|
81
|
+
/** Path to the template image for this element (if no variants) */
|
|
82
|
+
templatePath?: string | undefined;
|
|
83
|
+
/** Variants of this element (e.g., enabled/disabled button) */
|
|
84
|
+
variants?: Record<string, ElementVariant> | undefined;
|
|
85
|
+
/** Optional: Path to section template if element is within a specific section */
|
|
86
|
+
sectionTemplatePath?: string | undefined;
|
|
87
|
+
/** Type of UI element */
|
|
88
|
+
type?: ElementType | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Whether this element is animated/changes frequently
|
|
91
|
+
* Animated elements use looser matching and skip exact pixel comparison
|
|
92
|
+
*/
|
|
93
|
+
animated?: boolean | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Custom matcher function for advanced use cases
|
|
96
|
+
* Provides full control over element matching and value extraction
|
|
97
|
+
* Example: Extract progress percentage from progress bar
|
|
98
|
+
*/
|
|
99
|
+
customMatcher?: CustomMatcherFunction | undefined;
|
|
100
|
+
/** Deprecated: Use type: ElementType.CHECKBOX instead */
|
|
101
|
+
isCheckbox?: boolean | undefined;
|
|
102
|
+
/** Optional dropdown choices. When set, OCR is snapped to the closest option. */
|
|
103
|
+
options?: string[] | undefined;
|
|
104
|
+
/** Value box inside the match crop (label stays in the template, OCR reads this). */
|
|
105
|
+
ocrRect?: Rect | undefined;
|
|
106
|
+
/** Character whitelist preset: auto, text, digits, alnum, email, vin. */
|
|
107
|
+
charset?: string | undefined;
|
|
108
|
+
/** Expected glyph → OCR glyphs allowed in its place. */
|
|
109
|
+
swaps?: OcrSwaps | undefined;
|
|
110
|
+
/**
|
|
111
|
+
* Value may clip in the box. `end` = prefix plus up to 2 garbage glyphs at the cut.
|
|
112
|
+
*/
|
|
113
|
+
overflow?: OcrOverflow | undefined;
|
|
114
|
+
/** How to read the value. Default `ocr`. `clipboard` is click / select-all / copy. */
|
|
115
|
+
read?: FieldRead | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* Inner boxes to OCR separately after one parent match.
|
|
118
|
+
* Rects are relative to the match crop (same space as ocrRect).
|
|
119
|
+
*/
|
|
120
|
+
parts?: FieldPart[] | undefined;
|
|
121
|
+
}
|
|
122
|
+
/** One inner value box on a shared-label row (name, city/state, phones). */
|
|
123
|
+
export interface FieldPart extends Rect {
|
|
124
|
+
name: string;
|
|
125
|
+
charset?: string | undefined;
|
|
126
|
+
swaps?: OcrSwaps | undefined;
|
|
127
|
+
overflow?: OcrOverflow | undefined;
|
|
128
|
+
read?: FieldRead | undefined;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Result from extracting a single element
|
|
132
|
+
*/
|
|
133
|
+
export interface ElementResult {
|
|
134
|
+
/** Element name/identifier */
|
|
135
|
+
name: string;
|
|
136
|
+
/** Extracted value (text for fields, "checked"/"unchecked" for checkboxes, etc.) */
|
|
137
|
+
value: string;
|
|
138
|
+
/** Match confidence score (0-1) */
|
|
139
|
+
confidence?: number | undefined;
|
|
140
|
+
/** Location of the element on screen */
|
|
141
|
+
location: Rect;
|
|
142
|
+
/** Value box in screen space (label crop stays on `location`). Used by fill(). */
|
|
143
|
+
ocrLocation?: Rect | undefined;
|
|
144
|
+
/** Template match on blank.png. Differs from `location` when the window moved. */
|
|
145
|
+
blankLocation?: Rect | undefined;
|
|
146
|
+
/** Whether the element is in its empty/default state */
|
|
147
|
+
isEmpty: boolean;
|
|
148
|
+
/** Type of element */
|
|
149
|
+
type?: ElementType | undefined;
|
|
150
|
+
/** Which variant matched (if element has variants) */
|
|
151
|
+
variant?: string | undefined;
|
|
152
|
+
/** Custom metadata from custom matcher (e.g., { percentage: 47 }) */
|
|
153
|
+
metadata?: Record<string, any> | undefined;
|
|
154
|
+
/** Per-box OCR when the element was authored with parts. */
|
|
155
|
+
parts?: ElementResult[] | undefined;
|
|
156
|
+
/** Crop PNG to attach on the assertion step, not on waitFor. */
|
|
157
|
+
traceImage?: Buffer | undefined;
|
|
158
|
+
/** Attachment title for `traceImage`. */
|
|
159
|
+
traceName?: string | undefined;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Result from comparing a form/screen
|
|
163
|
+
*/
|
|
164
|
+
export interface ScreenComparison {
|
|
165
|
+
/** All extracted elements */
|
|
166
|
+
elements: ElementResult[];
|
|
167
|
+
/** Total elements checked */
|
|
168
|
+
totalElements: number;
|
|
169
|
+
/** Number of elements with content/active state */
|
|
170
|
+
filledElements: number;
|
|
171
|
+
/** Number of elements in empty/default state */
|
|
172
|
+
emptyElements: number;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Rectangle coordinates
|
|
176
|
+
*/
|
|
177
|
+
export interface Rect {
|
|
178
|
+
x: number;
|
|
179
|
+
y: number;
|
|
180
|
+
width: number;
|
|
181
|
+
height: number;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Point coordinates
|
|
185
|
+
*/
|
|
186
|
+
export interface Point {
|
|
187
|
+
x: number;
|
|
188
|
+
y: number;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Template matching result
|
|
192
|
+
*/
|
|
193
|
+
export interface MatchResult {
|
|
194
|
+
location: Point;
|
|
195
|
+
confidence: number;
|
|
196
|
+
rect: Rect;
|
|
197
|
+
}
|
|
198
|
+
/** @deprecated Use ElementConfig instead */
|
|
199
|
+
export type FieldConfig = ElementConfig;
|
|
200
|
+
/** @deprecated Use ElementResult instead */
|
|
201
|
+
export type FieldResult = ElementResult;
|
|
202
|
+
/** @deprecated Use ScreenComparison instead */
|
|
203
|
+
export type FormComparison = ScreenComparison;
|
|
204
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAEvE;;GAEG;AACH,oBAAY,WAAW;IACrB,KAAK,UAAU,CAAY,uBAAuB;IAClD,MAAM,WAAW,CAAU,wB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AAE,GAAG,CAAC,GAAG,SAAS,CAAC;IAE3C,4DAA4D;IAC5D,KAAK,CAAC,EAAE,aAAa,EAAE,GAAG,SAAS,CAAC;IAEpC,gEAAgE;IAChE,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAEhC,yCAAyC;IACzC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,6BAA6B;IAC7B,QAAQ,EAAE,aAAa,EAAE,CAAC;IAE1B,6BAA6B;IAC7B,aAAa,EAAE,MAAM,CAAC;IAEtB,mDAAmD;IACnD,cAAc,EAAE,MAAM,CAAC;IAEvB,gDAAgD;IAChD,aAAa,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,IAAI;IACnB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,KAAK,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,IAAI,CAAC;CACZ;AAGD,4CAA4C;AAC5C,MAAM,MAAM,WAAW,GAAG,aAAa,CAAC;AAExC,4CAA4C;AAC5C,MAAM,MAAM,WAAW,GAAG,aAAa,CAAC;AAExC,+CAA+C;AAC/C,MAAM,MAAM,cAAc,GAAG,gBAAgB,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types of UI elements that can be extracted
|
|
3
|
+
*/
|
|
4
|
+
export var ElementType;
|
|
5
|
+
(function (ElementType) {
|
|
6
|
+
ElementType["FIELD"] = "field";
|
|
7
|
+
ElementType["BUTTON"] = "button";
|
|
8
|
+
ElementType["CHECKBOX"] = "checkbox";
|
|
9
|
+
ElementType["RADIO"] = "radio";
|
|
10
|
+
ElementType["LINK"] = "link";
|
|
11
|
+
ElementType["ICON"] = "icon";
|
|
12
|
+
ElementType["LABEL"] = "label";
|
|
13
|
+
ElementType["DROPDOWN"] = "dropdown";
|
|
14
|
+
ElementType["TAB"] = "tab";
|
|
15
|
+
ElementType["TOGGLE"] = "toggle";
|
|
16
|
+
ElementType["MESSAGE"] = "message";
|
|
17
|
+
ElementType["OTHER"] = "other";
|
|
18
|
+
})(ElementType || (ElementType = {}));
|
|
19
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAN,IAAY,WAaX;AAbD,WAAY,WAAW;IACrB,8BAAe,CAAA;IACf,gCAAiB,CAAA;IACjB,oCAAqB,CAAA;IACrB,8BAAe,CAAA;IACf,4BAAa,CAAA;IACb,4BAAa,CAAA;IACb,8BAAe,CAAA;IACf,oCAAqB,CAAA;IACrB,0BAAW,CAAA;IACX,gCAAiB,CAAA;IACjB,kCAAmB,CAAA;IACnB,8BAAe,CAAA;AACjB,CAAC,EAbW,WAAW,KAAX,WAAW,QAatB"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export declare const CHARSET_PRESETS: Record<string, string>;
|
|
2
|
+
export declare function charsetForField(name?: string, type?: string, preset?: string): string | undefined;
|
|
3
|
+
export declare function normalizeOcrText(text: string): string;
|
|
4
|
+
export declare function pickFromOptions(text: string, options?: string[]): string;
|
|
5
|
+
/** Expected glyph → OCR glyphs allowed in its place. `{ '@': ['Q', 'C'], '5': 'S' }` */
|
|
6
|
+
export type OcrSwaps = Record<string, string | readonly string[]>;
|
|
7
|
+
/** How a field is allowed to clip when the value does not fit the box. */
|
|
8
|
+
export type OcrOverflow = 'start' | 'end' | 'both';
|
|
9
|
+
/** How to read a field's value. `clipboard` is click / select-all / copy. */
|
|
10
|
+
export type FieldRead = 'ocr' | 'clipboard';
|
|
11
|
+
export declare function ocrTextMatches(actual: string, expected: string | RegExp, options?: {
|
|
12
|
+
swaps?: OcrSwaps;
|
|
13
|
+
exact?: boolean;
|
|
14
|
+
overflow?: OcrOverflow;
|
|
15
|
+
overflowSlop?: number;
|
|
16
|
+
}): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* OCR utility for extracting text from images using Tesseract.js
|
|
19
|
+
*/
|
|
20
|
+
export declare class OCRUtil {
|
|
21
|
+
private worker;
|
|
22
|
+
private language;
|
|
23
|
+
initialize(language?: string): Promise<void>;
|
|
24
|
+
private ocrParams;
|
|
25
|
+
private ensureWorker;
|
|
26
|
+
/**
|
|
27
|
+
* Extract text from an image buffer
|
|
28
|
+
*/
|
|
29
|
+
extractText(imageBuffer: Buffer, options?: {
|
|
30
|
+
charset?: string;
|
|
31
|
+
psm?: string;
|
|
32
|
+
}): Promise<string>;
|
|
33
|
+
/**
|
|
34
|
+
* Extract text with confidence scores and bounding boxes
|
|
35
|
+
*/
|
|
36
|
+
extractDetailedText(imageBuffer: Buffer): Promise<{
|
|
37
|
+
text: string;
|
|
38
|
+
confidence: number;
|
|
39
|
+
words: any;
|
|
40
|
+
lines: any;
|
|
41
|
+
}>;
|
|
42
|
+
terminate(): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get or create a shared OCR utility instance
|
|
46
|
+
*/
|
|
47
|
+
export declare function getOCRUtil(language?: string): Promise<OCRUtil>;
|
|
48
|
+
/**
|
|
49
|
+
* Clean up the shared OCR utility
|
|
50
|
+
*/
|
|
51
|
+
export declare function cleanupOCR(): Promise<void>;
|
|
52
|
+
//# sourceMappingURL=ocr.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ocr.d.ts","sourceRoot":"","sources":["../../src/utils/ocr.ts"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AC/C,OAAO,CAAC,MAAM,CAAC;IAOlB;;OAEG;IACG,mBAAmB,CAAC,WAAW,EAAE,MAAM;;;;;;IAoBvC,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;CAMjC;AAKD;;GAEG;AACH,wBAAsB,UAAU,CAAC,QAAQ,SAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAMnE;AAED;;GAEG;AACH,wBAAsB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAKhD"}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { createWorker } from 'tesseract.js';
|
|
2
|
+
const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
3
|
+
const LOWER = UPPER.toLowerCase();
|
|
4
|
+
const DIGITS = '0123456789';
|
|
5
|
+
export const CHARSET_PRESETS = {
|
|
6
|
+
text: `${UPPER}${LOWER}${DIGITS} ./ -`,
|
|
7
|
+
digits: `${DIGITS} / `,
|
|
8
|
+
alnum: `${UPPER}${LOWER}${DIGITS}`,
|
|
9
|
+
email: `${UPPER}${LOWER}${DIGITS}@._-`,
|
|
10
|
+
vin: `${UPPER}${LOWER}${DIGITS}`,
|
|
11
|
+
};
|
|
12
|
+
export function charsetForField(name = '', type = '', preset = 'auto') {
|
|
13
|
+
if (type === 'checkbox')
|
|
14
|
+
return undefined;
|
|
15
|
+
if (preset && preset !== 'auto' && CHARSET_PRESETS[preset])
|
|
16
|
+
return CHARSET_PRESETS[preset];
|
|
17
|
+
const key = `${name} ${type}`.toLowerCase();
|
|
18
|
+
if (key.includes('email'))
|
|
19
|
+
return CHARSET_PRESETS.email;
|
|
20
|
+
if (key.includes('vin'))
|
|
21
|
+
return CHARSET_PRESETS.vin;
|
|
22
|
+
if (key.includes('contact') && key.includes('method'))
|
|
23
|
+
return CHARSET_PRESETS.text;
|
|
24
|
+
if (key.includes('phone') || key.includes('odometer') || key.includes('year') || key.includes('zip')) {
|
|
25
|
+
return CHARSET_PRESETS.digits;
|
|
26
|
+
}
|
|
27
|
+
return CHARSET_PRESETS.text;
|
|
28
|
+
}
|
|
29
|
+
export function normalizeOcrText(text) {
|
|
30
|
+
return String(text ?? '').replace(/\n+/g, ' ').trim();
|
|
31
|
+
}
|
|
32
|
+
export function pickFromOptions(text, options) {
|
|
33
|
+
if (!text || !options?.length)
|
|
34
|
+
return text;
|
|
35
|
+
const compact = (value) => value.toUpperCase().replace(/[^A-Z0-9]/g, '');
|
|
36
|
+
const tokens = (value) => value.toUpperCase().match(/[A-Z0-9]+/g) || [];
|
|
37
|
+
const got = compact(text);
|
|
38
|
+
const gotTokens = tokens(text);
|
|
39
|
+
let best = null;
|
|
40
|
+
let bestScore = 0;
|
|
41
|
+
for (const option of options) {
|
|
42
|
+
const want = compact(option);
|
|
43
|
+
if (!want)
|
|
44
|
+
continue;
|
|
45
|
+
if (got === want)
|
|
46
|
+
return option;
|
|
47
|
+
let score = 0;
|
|
48
|
+
const shorter = got.length < want.length ? got : want;
|
|
49
|
+
const longer = got.length < want.length ? want : got;
|
|
50
|
+
if (longer.includes(shorter))
|
|
51
|
+
score = shorter.length / longer.length;
|
|
52
|
+
const wantTokens = tokens(option);
|
|
53
|
+
if (gotTokens.length && gotTokens.length === wantTokens.length) {
|
|
54
|
+
const prefixes = gotTokens.every((token, i) => {
|
|
55
|
+
const w = wantTokens[i];
|
|
56
|
+
return w !== undefined && (w.startsWith(token) || token.startsWith(w));
|
|
57
|
+
});
|
|
58
|
+
if (prefixes)
|
|
59
|
+
score = Math.max(score, 0.55);
|
|
60
|
+
}
|
|
61
|
+
const initials = wantTokens.map((token) => token[0] ?? '').join('');
|
|
62
|
+
if (initials && (got === initials || gotTokens.join('') === initials)) {
|
|
63
|
+
score = Math.max(score, 0.6);
|
|
64
|
+
}
|
|
65
|
+
if (score > bestScore) {
|
|
66
|
+
best = option;
|
|
67
|
+
bestScore = score;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return bestScore >= 0.4 ? best : text;
|
|
71
|
+
}
|
|
72
|
+
const DEFAULT_OVERFLOW_SLOP = 2;
|
|
73
|
+
function allowedActuals(expectedChar, swaps) {
|
|
74
|
+
const allowed = new Set([expectedChar]);
|
|
75
|
+
const extra = swaps?.[expectedChar];
|
|
76
|
+
if (extra == null)
|
|
77
|
+
return allowed;
|
|
78
|
+
const values = typeof extra === 'string' ? [extra] : extra;
|
|
79
|
+
for (const value of values) {
|
|
80
|
+
for (const ch of value)
|
|
81
|
+
allowed.add(ch);
|
|
82
|
+
}
|
|
83
|
+
return allowed;
|
|
84
|
+
}
|
|
85
|
+
function charsMatch(actual, expected, swaps) {
|
|
86
|
+
const got = [...actual];
|
|
87
|
+
const want = [...expected];
|
|
88
|
+
if (got.length !== want.length)
|
|
89
|
+
return false;
|
|
90
|
+
return got.every((ch, i) => allowedActuals(want[i] ?? '', swaps).has(ch));
|
|
91
|
+
}
|
|
92
|
+
function charEq(actual, expected, swaps) {
|
|
93
|
+
return allowedActuals(expected, swaps).has(actual);
|
|
94
|
+
}
|
|
95
|
+
function minKeep(wantLen) {
|
|
96
|
+
return Math.min(wantLen, Math.max(3, Math.ceil(wantLen * 0.5)));
|
|
97
|
+
}
|
|
98
|
+
function containsSlice(want, slice, swaps) {
|
|
99
|
+
if (slice.length > want.length)
|
|
100
|
+
return false;
|
|
101
|
+
for (let i = 0; i <= want.length - slice.length; i++) {
|
|
102
|
+
if (slice.every((ch, j) => charEq(ch, want[i + j] ?? '', swaps)))
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
function overflowMatches(actual, expected, overflow, swaps, slop = DEFAULT_OVERFLOW_SLOP) {
|
|
108
|
+
const got = [...actual];
|
|
109
|
+
const want = [...expected];
|
|
110
|
+
if (!want.length)
|
|
111
|
+
return !got.length;
|
|
112
|
+
const keep = minKeep(want.length);
|
|
113
|
+
if (overflow === 'end') {
|
|
114
|
+
let matched = 0;
|
|
115
|
+
while (matched < got.length && matched < want.length && charEq(got[matched] ?? '', want[matched] ?? '', swaps)) {
|
|
116
|
+
matched++;
|
|
117
|
+
}
|
|
118
|
+
return matched >= keep && (got.length - matched) <= slop;
|
|
119
|
+
}
|
|
120
|
+
if (overflow === 'start') {
|
|
121
|
+
let matched = 0;
|
|
122
|
+
while (matched < got.length
|
|
123
|
+
&& matched < want.length
|
|
124
|
+
&& charEq(got[got.length - 1 - matched] ?? '', want[want.length - 1 - matched] ?? '', swaps)) {
|
|
125
|
+
matched++;
|
|
126
|
+
}
|
|
127
|
+
return matched >= keep && (got.length - matched) <= slop;
|
|
128
|
+
}
|
|
129
|
+
for (let left = 0; left <= slop; left++) {
|
|
130
|
+
for (let right = 0; right <= slop; right++) {
|
|
131
|
+
if (left + right >= got.length)
|
|
132
|
+
continue;
|
|
133
|
+
const slice = got.slice(left, got.length - right);
|
|
134
|
+
if (slice.length < keep)
|
|
135
|
+
continue;
|
|
136
|
+
if (containsSlice(want, slice, swaps))
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
export function ocrTextMatches(actual, expected, options = {}) {
|
|
143
|
+
if (expected instanceof RegExp)
|
|
144
|
+
return expected.test(actual);
|
|
145
|
+
if (options.overflow) {
|
|
146
|
+
return overflowMatches(actual, expected, options.overflow, options.swaps, options.overflowSlop ?? DEFAULT_OVERFLOW_SLOP);
|
|
147
|
+
}
|
|
148
|
+
if (options.exact)
|
|
149
|
+
return charsMatch(actual, expected, options.swaps);
|
|
150
|
+
if (actual.includes(expected))
|
|
151
|
+
return true;
|
|
152
|
+
if (!options.swaps || !Object.keys(options.swaps).length)
|
|
153
|
+
return false;
|
|
154
|
+
const got = [...actual];
|
|
155
|
+
const want = [...expected];
|
|
156
|
+
if (want.length > got.length)
|
|
157
|
+
return false;
|
|
158
|
+
for (let i = 0; i <= got.length - want.length; i++) {
|
|
159
|
+
if (charsMatch(got.slice(i, i + want.length).join(''), expected, options.swaps))
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* OCR utility for extracting text from images using Tesseract.js
|
|
166
|
+
*/
|
|
167
|
+
export class OCRUtil {
|
|
168
|
+
worker = null;
|
|
169
|
+
language = 'eng';
|
|
170
|
+
async initialize(language = 'eng') {
|
|
171
|
+
if (this.worker && this.language === language)
|
|
172
|
+
return;
|
|
173
|
+
await this.terminate();
|
|
174
|
+
this.language = language;
|
|
175
|
+
this.worker = await createWorker(language);
|
|
176
|
+
}
|
|
177
|
+
ocrParams(options = {}) {
|
|
178
|
+
return {
|
|
179
|
+
tessedit_pageseg_mode: options.psm ?? '7',
|
|
180
|
+
tessedit_char_whitelist: options.charset
|
|
181
|
+
|| 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@._- /',
|
|
182
|
+
user_defined_dpi: '300',
|
|
183
|
+
preserve_interword_spaces: '1',
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
async ensureWorker() {
|
|
187
|
+
if (!this.worker)
|
|
188
|
+
await this.initialize(this.language);
|
|
189
|
+
if (!this.worker)
|
|
190
|
+
throw new Error('OCR worker not initialized. Call initialize() first.');
|
|
191
|
+
return this.worker;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Extract text from an image buffer
|
|
195
|
+
*/
|
|
196
|
+
async extractText(imageBuffer, options = {}) {
|
|
197
|
+
const worker = await this.ensureWorker();
|
|
198
|
+
await worker.setParameters(this.ocrParams(options));
|
|
199
|
+
const { data } = await worker.recognize(imageBuffer);
|
|
200
|
+
return normalizeOcrText(data.text);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Extract text with confidence scores and bounding boxes
|
|
204
|
+
*/
|
|
205
|
+
async extractDetailedText(imageBuffer) {
|
|
206
|
+
const worker = await this.ensureWorker();
|
|
207
|
+
await worker.setParameters(this.ocrParams());
|
|
208
|
+
const { data } = await worker.recognize(imageBuffer);
|
|
209
|
+
return {
|
|
210
|
+
text: data.text.trim(),
|
|
211
|
+
confidence: data.confidence,
|
|
212
|
+
words: data.words?.map((word) => ({
|
|
213
|
+
text: word.text,
|
|
214
|
+
confidence: word.confidence,
|
|
215
|
+
bbox: word.bbox,
|
|
216
|
+
})) || [],
|
|
217
|
+
lines: data.lines?.map((line) => ({
|
|
218
|
+
text: line.text,
|
|
219
|
+
confidence: line.confidence,
|
|
220
|
+
bbox: line.bbox,
|
|
221
|
+
})) || [],
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
async terminate() {
|
|
225
|
+
if (this.worker) {
|
|
226
|
+
await this.worker.terminate();
|
|
227
|
+
this.worker = null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// Singleton instance for reuse across tests
|
|
232
|
+
let sharedOCRUtil = null;
|
|
233
|
+
/**
|
|
234
|
+
* Get or create a shared OCR utility instance
|
|
235
|
+
*/
|
|
236
|
+
export async function getOCRUtil(language = 'eng') {
|
|
237
|
+
if (!sharedOCRUtil) {
|
|
238
|
+
sharedOCRUtil = new OCRUtil();
|
|
239
|
+
await sharedOCRUtil.initialize(language);
|
|
240
|
+
}
|
|
241
|
+
return sharedOCRUtil;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Clean up the shared OCR utility
|
|
245
|
+
*/
|
|
246
|
+
export async function cleanupOCR() {
|
|
247
|
+
if (sharedOCRUtil) {
|
|
248
|
+
await sharedOCRUtil.terminate();
|
|
249
|
+
sharedOCRUtil = null;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
//# sourceMappingURL=ocr.js.map
|