@intuned/browser-dev 2.2.3-unify-sdks.20
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/.babelrc +21 -0
- package/.eslintignore +10 -0
- package/.eslintrc.js +39 -0
- package/LICENSE +43 -0
- package/dist/ai-extractors/AnthropicClient/index.js +23 -0
- package/dist/ai-extractors/export.d.js +5 -0
- package/dist/ai-extractors/export.d.ts +422 -0
- package/dist/ai-extractors/extractStructuredData.js +79 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/constants.js +7 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/errors.js +42 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/extractStructuredDataUsingClaude.js +149 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/extractStructuredDataUsingGoogle.js +37 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/extractStructuredDataUsingOpenAi.js +144 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/extractStrucutredDataUsingAiInstance.js +123 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/index.js +55 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/isItemTableHeaderOrFooter.js +96 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/screenshotHelpers.js +55 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/types.js +5 -0
- package/dist/ai-extractors/extractionHelpers/extractStructuredDataUsingAi/utils.js +53 -0
- package/dist/ai-extractors/extractionHelpers/types.js +5 -0
- package/dist/ai-extractors/fileExtractors.js +176 -0
- package/dist/ai-extractors/index.js +31 -0
- package/dist/ai-extractors/jsonSchema.d.js +5 -0
- package/dist/ai-extractors/jsonSchema.d.ts +49 -0
- package/dist/ai-extractors/openAiClients/index.js +23 -0
- package/dist/ai-extractors/validators.js +239 -0
- package/dist/browser/ai/export.d.js +3 -0
- package/dist/browser/ai/export.d.ts +587 -0
- package/dist/browser/ai/extractMarkdown.js +15 -0
- package/dist/browser/ai/extractStructuredData.js +231 -0
- package/dist/browser/ai/extractStructuredDataUsingAi.js +140 -0
- package/dist/browser/ai/extractionHelpers/screenshotHelpers.js +55 -0
- package/dist/browser/ai/extractionHelpers/validateSchema.js +148 -0
- package/dist/browser/ai/index.d.ts +587 -0
- package/dist/browser/ai/index.js +19 -0
- package/dist/browser/ai/isPageLoaded.js +67 -0
- package/dist/browser/ai/prompt.js +39 -0
- package/dist/browser/ai/tests/testCheckAllTypesAreStrings.spec.js +143 -0
- package/dist/browser/ai/tests/testExtractStructuredData.spec.js +622 -0
- package/dist/browser/ai/tools/index.js +48 -0
- package/dist/browser/ai/types/errors.js +67 -0
- package/dist/browser/ai/types/models.js +45 -0
- package/dist/browser/ai/types/types.js +48 -0
- package/dist/browser/ai/validators.js +136 -0
- package/dist/common/Logger/index.js +60 -0
- package/dist/common/Logger/types.js +5 -0
- package/dist/common/SdkError.js +50 -0
- package/dist/common/aiModelsValidations.js +50 -0
- package/dist/common/browser_scripts.js +2596 -0
- package/dist/common/ensureBrowserScripts.js +17 -0
- package/dist/common/environmentVariables.js +16 -0
- package/dist/common/eventTracking/getAiTrackingHeaders.js +31 -0
- package/dist/common/eventTracking/getFileTrackingHeaders.js +23 -0
- package/dist/common/extendedTest.js +148 -0
- package/dist/common/extractionHelpers.js +19 -0
- package/dist/common/formatZodError.js +18 -0
- package/dist/common/fuzzySearch/fuzzySearch.test.js +250 -0
- package/dist/common/fuzzySearch/levenshtein-search.js +298 -0
- package/dist/common/fuzzySearch/utils.js +23 -0
- package/dist/common/getModelProvider.js +18 -0
- package/dist/common/getSimplifiedHtml.js +122 -0
- package/dist/common/hashObject.js +32 -0
- package/dist/common/html2markdown/convertElementToMarkdown.js +469 -0
- package/dist/common/html2markdown/index.js +19 -0
- package/dist/common/jwtTokenManager.js +18 -0
- package/dist/common/loadRuntime.js +16 -0
- package/dist/common/locatorHelpers.js +41 -0
- package/dist/common/matching/collectStrings.js +32 -0
- package/dist/common/matching/levenshtein.js +40 -0
- package/dist/common/matching/matching.js +317 -0
- package/dist/common/matching/types.js +1 -0
- package/dist/common/noEmpty.js +9 -0
- package/dist/common/saveSnapshotWithExamples.js +60 -0
- package/dist/common/tests/testEnsureBrowserScript.spec.js +31 -0
- package/dist/common/xpathMapping.js +107 -0
- package/dist/helpers/downloadFile.js +125 -0
- package/dist/helpers/export.d.js +1 -0
- package/dist/helpers/export.d.ts +1294 -0
- package/dist/helpers/extractMarkdown.js +35 -0
- package/dist/helpers/filterEmptyValues.js +54 -0
- package/dist/helpers/gotoUrl.js +93 -0
- package/dist/helpers/index.d.ts +1294 -0
- package/dist/helpers/index.js +115 -0
- package/dist/helpers/processDate.js +25 -0
- package/dist/helpers/resolveUrl.js +63 -0
- package/dist/helpers/sanitizeHtml.js +73 -0
- package/dist/helpers/saveFileToS3.js +46 -0
- package/dist/helpers/scrollToLoadContent.js +50 -0
- package/dist/helpers/tests/extendedTest.js +130 -0
- package/dist/helpers/tests/testDownloadFile.spec.js +197 -0
- package/dist/helpers/tests/testFilterEmptyValues.spec.js +151 -0
- package/dist/helpers/tests/testGoToUrl.spec.js +37 -0
- package/dist/helpers/tests/testIsPageLoaded.spec.js +285 -0
- package/dist/helpers/tests/testProcessDate.spec.js +13 -0
- package/dist/helpers/tests/testResolveUrl.spec.js +341 -0
- package/dist/helpers/tests/testSanitizeHtml.spec.js +330 -0
- package/dist/helpers/tests/testSimplifyHtml.spec.js +251 -0
- package/dist/helpers/tests/testValidateDataUsingSchema.spec.js +380 -0
- package/dist/helpers/tests/testWaitForDomSettled.spec.js +169 -0
- package/dist/helpers/tests/testWaitForNetworkIdle.spec.js +115 -0
- package/dist/helpers/types/Attachment.js +81 -0
- package/dist/helpers/types/CustomTypeRegistry.js +48 -0
- package/dist/helpers/types/RunEnvironment.js +18 -0
- package/dist/helpers/types/ValidationError.js +17 -0
- package/dist/helpers/types/index.js +51 -0
- package/dist/helpers/uploadFileToS3.js +153 -0
- package/dist/helpers/utils/getS3Client.js +21 -0
- package/dist/helpers/utils/index.js +73 -0
- package/dist/helpers/utils/isDownload.js +10 -0
- package/dist/helpers/utils/isGenerateCodeMode.js +9 -0
- package/dist/helpers/utils/isLocator.js +9 -0
- package/dist/helpers/utils/jwtTokenManager.js +18 -0
- package/dist/helpers/validateDataUsingSchema.js +119 -0
- package/dist/helpers/waitForDomSettled.js +182 -0
- package/dist/helpers/waitForNetworkIdle.js +191 -0
- package/dist/index.d.js +82 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +84 -0
- package/dist/intunedServices/ApiGateway/aiApiGateway.js +87 -0
- package/dist/intunedServices/ApiGateway/factory.js +13 -0
- package/dist/intunedServices/ApiGateway/providers/Anthropic.js +26 -0
- package/dist/intunedServices/ApiGateway/providers/Gemini.js +29 -0
- package/dist/intunedServices/ApiGateway/providers/OpenAI.js +29 -0
- package/dist/intunedServices/ApiGateway/tests/testApiGateway.spec.js +221 -0
- package/dist/intunedServices/ApiGateway/types.js +11 -0
- package/dist/intunedServices/cache/cache.js +61 -0
- package/dist/intunedServices/cache/index.js +12 -0
- package/dist/intunedServices/cache/tests/testCache.spec.js +117 -0
- package/dist/optimized-extractors/common/buildExamplesPrompt.js +12 -0
- package/dist/optimized-extractors/common/buildImagesFromPage.js +55 -0
- package/dist/optimized-extractors/common/extractStructuredDataUsingClaude.js +149 -0
- package/dist/optimized-extractors/common/extractStructuredDataUsingGoogle.js +37 -0
- package/dist/optimized-extractors/common/extractStructuredDataUsingOpenAi.js +145 -0
- package/dist/optimized-extractors/common/extractStrucutredDataUsingAiInstance.js +122 -0
- package/dist/optimized-extractors/common/findTableHeaders.js +175 -0
- package/dist/optimized-extractors/common/index.js +55 -0
- package/dist/optimized-extractors/common/isTableHeaderOrFooter.js +97 -0
- package/dist/optimized-extractors/common/matching/matching.js +212 -0
- package/dist/optimized-extractors/common/matching/matching.test.js +655 -0
- package/dist/optimized-extractors/common/matching/types.js +18 -0
- package/dist/optimized-extractors/common/matching/utils.js +184 -0
- package/dist/optimized-extractors/common/utils.js +58 -0
- package/dist/optimized-extractors/export.d.js +5 -0
- package/dist/optimized-extractors/export.d.ts +397 -0
- package/dist/optimized-extractors/extractArray.js +120 -0
- package/dist/optimized-extractors/extractObject.js +104 -0
- package/dist/optimized-extractors/index.d.ts +397 -0
- package/dist/optimized-extractors/index.js +31 -0
- package/dist/optimized-extractors/listExtractionHelpers/__tests__/dynamicListExtractor.spec.js +312 -0
- package/dist/optimized-extractors/listExtractionHelpers/__tests__/findSetOfXpathsToCreateAnArrayExtractor.test.js +22 -0
- package/dist/optimized-extractors/listExtractionHelpers/__tests__/getContainerElement.test.js +21 -0
- package/dist/optimized-extractors/listExtractionHelpers/__tests__/partOfSameArrayXpath.test.js +42 -0
- package/dist/optimized-extractors/listExtractionHelpers/__tests__/verifyThatAllXpathsArePartOfSameArray.test.js +9 -0
- package/dist/optimized-extractors/listExtractionHelpers/dynamicListExtractor.js +152 -0
- package/dist/optimized-extractors/listExtractionHelpers/errors.js +46 -0
- package/dist/optimized-extractors/listExtractionHelpers/getListMatches.js +14 -0
- package/dist/optimized-extractors/listExtractionHelpers/runAiExtraction.js +240 -0
- package/dist/optimized-extractors/listExtractionHelpers/typesAndSchema.js +5 -0
- package/dist/optimized-extractors/listExtractionHelpers/utils/extractPropertiesUsingGPTFromArray.js +277 -0
- package/dist/optimized-extractors/listExtractionHelpers/utils/extractStructuredListUsingAi.js +44 -0
- package/dist/optimized-extractors/listExtractionHelpers/utils/getListContainerXpath.js +94 -0
- package/dist/optimized-extractors/listExtractionHelpers/utils/getRelativeContainerXpathSelector.js +20 -0
- package/dist/optimized-extractors/listExtractionHelpers/utils/getSimplifiedHtmlPerListItem.js +21 -0
- package/dist/optimized-extractors/listExtractionHelpers/utils/tablesUtils.js +48 -0
- package/dist/optimized-extractors/listExtractionHelpers/utils/validateOptions.js +52 -0
- package/dist/optimized-extractors/models/anthropicModel.js +23 -0
- package/dist/optimized-extractors/models/openaiModel.js +23 -0
- package/dist/optimized-extractors/objectExtractionHelpers/AIExtractors.js +73 -0
- package/dist/optimized-extractors/objectExtractionHelpers/__tests__/checksumUtils.test.js +103 -0
- package/dist/optimized-extractors/objectExtractionHelpers/__tests__/testObjectExtractorFromLocator.spec.js +107 -0
- package/dist/optimized-extractors/objectExtractionHelpers/__tests__/testObjectExtractorFromPage.spec.js +107 -0
- package/dist/optimized-extractors/objectExtractionHelpers/calculateObjectExampleHash.js +28 -0
- package/dist/optimized-extractors/objectExtractionHelpers/captureSnapshot.js +26 -0
- package/dist/optimized-extractors/objectExtractionHelpers/checksumUtils.js +32 -0
- package/dist/optimized-extractors/objectExtractionHelpers/constants.js +7 -0
- package/dist/optimized-extractors/objectExtractionHelpers/dynamicObjectExtractor.js +106 -0
- package/dist/optimized-extractors/objectExtractionHelpers/errors.js +42 -0
- package/dist/optimized-extractors/objectExtractionHelpers/findDomMatches.js +54 -0
- package/dist/optimized-extractors/objectExtractionHelpers/getSimplifiedHtml.js +122 -0
- package/dist/optimized-extractors/objectExtractionHelpers/typesAndSchemas.js +5 -0
- package/dist/optimized-extractors/objectExtractionHelpers/validateDynamicObjectExtractorOptions.js +52 -0
- package/dist/optimized-extractors/types/aiModelsValidation.js +45 -0
- package/dist/optimized-extractors/types/errors.js +42 -0
- package/dist/optimized-extractors/types/jsonSchema.d.js +5 -0
- package/dist/optimized-extractors/types/jsonSchema.d.ts +50 -0
- package/dist/optimized-extractors/types/types.js +5 -0
- package/dist/optimized-extractors/validators.js +152 -0
- package/dist/vite-env.d.js +1 -0
- package/dist/vite-env.d.ts +9 -0
- package/docs.md +14 -0
- package/how-to-run-tests.md +10 -0
- package/intuned-runtime-setup.md +13 -0
- package/package.json +124 -0
- package/tsconfig.eslint.json +5 -0
- package/tsconfig.json +26 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getTableHeadersUsingAi = getTableHeadersUsingAi;
|
|
7
|
+
var _anthropicModel = require("../models/anthropicModel");
|
|
8
|
+
var _zod = require("zod");
|
|
9
|
+
var _imageSize = require("image-size");
|
|
10
|
+
var _neverthrow = require("neverthrow");
|
|
11
|
+
var Errors = _interopRequireWildcard(require("../types/errors"));
|
|
12
|
+
var _getAiTrackingHeaders = require("../../common/eventTracking/getAiTrackingHeaders");
|
|
13
|
+
var _environmentVariables = require("../../common/environmentVariables");
|
|
14
|
+
var _loadRuntime = require("../../common/loadRuntime");
|
|
15
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
16
|
+
async function getTableHeadersUsingAi(handle, identifier) {
|
|
17
|
+
var _getExecutionContext, _getExecutionContext2, _getExecutionContext3, _getExecutionContext4, _response$error;
|
|
18
|
+
const getExecutionContext = await (0, _loadRuntime.loadRuntime)();
|
|
19
|
+
let image;
|
|
20
|
+
try {
|
|
21
|
+
image = await handle.screenshot({
|
|
22
|
+
type: "png",
|
|
23
|
+
timeout: 3_000
|
|
24
|
+
});
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return (0, _neverthrow.ok)({
|
|
27
|
+
headers: []
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const {
|
|
31
|
+
width,
|
|
32
|
+
height
|
|
33
|
+
} = (0, _imageSize.imageSize)(image);
|
|
34
|
+
if (!width) return (0, _neverthrow.ok)({
|
|
35
|
+
headers: []
|
|
36
|
+
});
|
|
37
|
+
const imageToUse = height && height > 500 ? await cropImage(image, handle, {
|
|
38
|
+
height: 500,
|
|
39
|
+
width,
|
|
40
|
+
x: 0,
|
|
41
|
+
y: 0
|
|
42
|
+
}) : image;
|
|
43
|
+
if (!imageToUse) return (0, _neverthrow.ok)({
|
|
44
|
+
headers: []
|
|
45
|
+
});
|
|
46
|
+
const anthropic = (0, _anthropicModel.createAnthropicInstance)();
|
|
47
|
+
const response = await (0, _neverthrow.fromPromise)(anthropic.messages.create({
|
|
48
|
+
max_tokens: 4096,
|
|
49
|
+
temperature: 0,
|
|
50
|
+
messages: [{
|
|
51
|
+
role: "user",
|
|
52
|
+
content: [{
|
|
53
|
+
type: "text",
|
|
54
|
+
text: `extract table headers from the image, you must use the get_table_headers tool, make sure to get the headers in order, "column_number" is the number of the table column that has this header. if there's a column with no header that still counts as a column and should be taken into account.`
|
|
55
|
+
}, {
|
|
56
|
+
type: "image",
|
|
57
|
+
source: {
|
|
58
|
+
data: imageToUse.toString("base64"),
|
|
59
|
+
media_type: "image/png",
|
|
60
|
+
type: "base64"
|
|
61
|
+
}
|
|
62
|
+
}]
|
|
63
|
+
}],
|
|
64
|
+
model: "claude-3-haiku-20240307",
|
|
65
|
+
tools: [{
|
|
66
|
+
input_schema: {
|
|
67
|
+
type: "object",
|
|
68
|
+
properties: {
|
|
69
|
+
headers: {
|
|
70
|
+
type: "array",
|
|
71
|
+
items: {
|
|
72
|
+
type: "object",
|
|
73
|
+
description: "Table headers",
|
|
74
|
+
properties: {
|
|
75
|
+
column_number: {
|
|
76
|
+
type: "number",
|
|
77
|
+
description: "The column number of the header"
|
|
78
|
+
},
|
|
79
|
+
header: {
|
|
80
|
+
type: "string"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
required: ["column_number", "header"]
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
required: ["headers"]
|
|
88
|
+
},
|
|
89
|
+
name: "get_table_headers",
|
|
90
|
+
description: `a function to extract table headers from an image, the headers should be in order and the column number should be the number of the column that has this header. if there's a column with no header that still counts as a column and should be taken into account. you should still need to`
|
|
91
|
+
}]
|
|
92
|
+
}, {
|
|
93
|
+
headers: (0, _getAiTrackingHeaders.getAiTrackingHeaders)({
|
|
94
|
+
environment: (0, _environmentVariables.getEnvironmentVariable)("RUN_ENVIRONMENT"),
|
|
95
|
+
type: "DYNAMIC_LIST",
|
|
96
|
+
runId: (_getExecutionContext = getExecutionContext()) === null || _getExecutionContext === void 0 ? void 0 : _getExecutionContext.runId,
|
|
97
|
+
jobId: (_getExecutionContext2 = getExecutionContext()) === null || _getExecutionContext2 === void 0 ? void 0 : _getExecutionContext2.jobId,
|
|
98
|
+
jobRunId: (_getExecutionContext3 = getExecutionContext()) === null || _getExecutionContext3 === void 0 ? void 0 : _getExecutionContext3.jobRunId,
|
|
99
|
+
queueId: (_getExecutionContext4 = getExecutionContext()) === null || _getExecutionContext4 === void 0 ? void 0 : _getExecutionContext4.queueId
|
|
100
|
+
})
|
|
101
|
+
}), error => {
|
|
102
|
+
const typedError = error;
|
|
103
|
+
if (typedError.status === 449) return Errors.insufficientAiCredits(`🔴 ${typedError.error.error}`);
|
|
104
|
+
});
|
|
105
|
+
if (response.isErr() && ((_response$error = response.error) === null || _response$error === void 0 ? void 0 : _response$error.type) === "InsufficientAiCredits") {
|
|
106
|
+
return (0, _neverthrow.err)(response.error);
|
|
107
|
+
}
|
|
108
|
+
if (response.isErr()) {
|
|
109
|
+
return (0, _neverthrow.ok)({
|
|
110
|
+
headers: []
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const unwrappedResponse = response.value;
|
|
114
|
+
const tool = unwrappedResponse.content.find(t => t.type === "tool_use");
|
|
115
|
+
if (!tool) return (0, _neverthrow.ok)({
|
|
116
|
+
headers: []
|
|
117
|
+
});
|
|
118
|
+
const headersParsingResults = _zod.z.object({
|
|
119
|
+
headers: _zod.z.array(_zod.z.object({
|
|
120
|
+
column_number: _zod.z.number(),
|
|
121
|
+
header: _zod.z.string()
|
|
122
|
+
}))
|
|
123
|
+
}).safeParse(tool.input);
|
|
124
|
+
if (!headersParsingResults.success) {
|
|
125
|
+
return (0, _neverthrow.ok)({
|
|
126
|
+
headers: []
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return (0, _neverthrow.ok)({
|
|
130
|
+
headers: headersParsingResults.data.headers.map(i => i.header)
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
async function cropImage(image, handle, {
|
|
134
|
+
height: cropHeight,
|
|
135
|
+
width: cropWidth,
|
|
136
|
+
x,
|
|
137
|
+
y
|
|
138
|
+
}) {
|
|
139
|
+
const base64Image = image.toString("base64");
|
|
140
|
+
const dataUrl = `data:image/jpeg;base64,${base64Image}`;
|
|
141
|
+
const croppedDataUrl = await Promise.race([handle.evaluate((_, {
|
|
142
|
+
cropHeight,
|
|
143
|
+
cropWidth,
|
|
144
|
+
x,
|
|
145
|
+
y,
|
|
146
|
+
url
|
|
147
|
+
}) => {
|
|
148
|
+
return new Promise(resolve => {
|
|
149
|
+
const img = document.createElement("img");
|
|
150
|
+
const canvas = document.createElement("canvas");
|
|
151
|
+
document.body.appendChild(img);
|
|
152
|
+
img.onload = () => {
|
|
153
|
+
canvas.width = cropWidth;
|
|
154
|
+
canvas.height = cropHeight;
|
|
155
|
+
const ctx = canvas.getContext("2d");
|
|
156
|
+
ctx.drawImage(img, x, y, cropWidth, cropHeight, 0, 0, cropWidth, cropHeight);
|
|
157
|
+
const resultImage = canvas.toDataURL().split(";base64,").pop();
|
|
158
|
+
resolve(resultImage);
|
|
159
|
+
img.remove();
|
|
160
|
+
canvas.remove();
|
|
161
|
+
};
|
|
162
|
+
img.src = url;
|
|
163
|
+
});
|
|
164
|
+
}, {
|
|
165
|
+
url: dataUrl,
|
|
166
|
+
x,
|
|
167
|
+
y,
|
|
168
|
+
cropWidth,
|
|
169
|
+
cropHeight
|
|
170
|
+
}), new Promise(resolve => setTimeout(() => {
|
|
171
|
+
resolve(undefined);
|
|
172
|
+
}, 5000))]);
|
|
173
|
+
if (!croppedDataUrl) return undefined;
|
|
174
|
+
return Buffer.from(croppedDataUrl, "base64");
|
|
175
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.extractStructuredDataUsingAi = extractStructuredDataUsingAi;
|
|
7
|
+
exports.isClaudeModel = isClaudeModel;
|
|
8
|
+
exports.isGoogleModel = isGoogleModel;
|
|
9
|
+
exports.isOpenAiModel = isOpenAiModel;
|
|
10
|
+
var _neverthrow = require("neverthrow");
|
|
11
|
+
var _extractStructuredDataUsingClaude = require("./extractStructuredDataUsingClaude");
|
|
12
|
+
var _extractStructuredDataUsingOpenAi = require("./extractStructuredDataUsingOpenAi");
|
|
13
|
+
var _utils = require("./utils");
|
|
14
|
+
var _aiModelsValidations = require("../../common/aiModelsValidations");
|
|
15
|
+
var _extractStructuredDataUsingGoogle = require("./extractStructuredDataUsingGoogle");
|
|
16
|
+
function isClaudeModel(model) {
|
|
17
|
+
return _aiModelsValidations.CLAUDE_MODELS.includes(model);
|
|
18
|
+
}
|
|
19
|
+
function isGoogleModel(model) {
|
|
20
|
+
return _aiModelsValidations.GOOGLE_MODELS.includes(model);
|
|
21
|
+
}
|
|
22
|
+
function isOpenAiModel(model) {
|
|
23
|
+
return _aiModelsValidations.GPT_MODELS.includes(model);
|
|
24
|
+
}
|
|
25
|
+
async function extractStructuredDataUsingAi(input) {
|
|
26
|
+
let extractionResult;
|
|
27
|
+
const {
|
|
28
|
+
model
|
|
29
|
+
} = input;
|
|
30
|
+
if (isOpenAiModel(model)) {
|
|
31
|
+
extractionResult = await (0, _extractStructuredDataUsingOpenAi.extractStructuredDataUsingOpenAi)({
|
|
32
|
+
...input,
|
|
33
|
+
model
|
|
34
|
+
});
|
|
35
|
+
} else if (isGoogleModel(model)) {
|
|
36
|
+
extractionResult = await (0, _extractStructuredDataUsingGoogle.extractStructuredDataUsingGoogle)({
|
|
37
|
+
...input,
|
|
38
|
+
model
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
extractionResult = await (0, _extractStructuredDataUsingClaude.extractStructuredDataUsingClaude)({
|
|
42
|
+
...input,
|
|
43
|
+
model
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (extractionResult.isErr()) {
|
|
47
|
+
return (0, _neverthrow.err)(extractionResult.error);
|
|
48
|
+
}
|
|
49
|
+
const {
|
|
50
|
+
result
|
|
51
|
+
} = extractionResult.value;
|
|
52
|
+
return (0, _neverthrow.ok)({
|
|
53
|
+
result: (0, _utils.cleanupAiResult)(result)
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isTableHeaderOrFooter = isTableHeaderOrFooter;
|
|
7
|
+
var _anthropicModel = require("../models/anthropicModel");
|
|
8
|
+
var _zod = require("zod");
|
|
9
|
+
var _neverthrow = require("neverthrow");
|
|
10
|
+
var Errors = _interopRequireWildcard(require("../types/errors"));
|
|
11
|
+
var _getAiTrackingHeaders = require("../../common/eventTracking/getAiTrackingHeaders");
|
|
12
|
+
var _environmentVariables = require("../../common/environmentVariables");
|
|
13
|
+
var _loadRuntime = require("../../common/loadRuntime");
|
|
14
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
15
|
+
async function isTableHeaderOrFooter(content) {
|
|
16
|
+
var _getExecutionContext, _getExecutionContext2, _getExecutionContext3, _getExecutionContext4, _response$error;
|
|
17
|
+
if (!content) {
|
|
18
|
+
return (0, _neverthrow.ok)({
|
|
19
|
+
isHeader: false
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
const anthropic = (0, _anthropicModel.createAnthropicInstance)();
|
|
23
|
+
const getExecutionContext = await (0, _loadRuntime.loadRuntime)();
|
|
24
|
+
const itemContent = typeof content === "string" ? {
|
|
25
|
+
type: "text",
|
|
26
|
+
text: content
|
|
27
|
+
} : {
|
|
28
|
+
type: "image",
|
|
29
|
+
source: {
|
|
30
|
+
data: content.toString("base64"),
|
|
31
|
+
media_type: "image/png",
|
|
32
|
+
type: "base64"
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const response = await (0, _neverthrow.fromPromise)(anthropic.messages.create({
|
|
36
|
+
max_tokens: 4096,
|
|
37
|
+
temperature: 0,
|
|
38
|
+
messages: [{
|
|
39
|
+
role: "user",
|
|
40
|
+
content: [{
|
|
41
|
+
type: "text",
|
|
42
|
+
text: `given the following content, you need to decide if the content is a html table header or a table footer,
|
|
43
|
+
a table header is a row that contains labels for table columns, and footer usually has pagination information or summary of the table`
|
|
44
|
+
}, itemContent]
|
|
45
|
+
}],
|
|
46
|
+
model: "claude-3-haiku-20240307",
|
|
47
|
+
tools: [{
|
|
48
|
+
input_schema: {
|
|
49
|
+
type: "object",
|
|
50
|
+
properties: {
|
|
51
|
+
isTableHeaderOrFooter: {
|
|
52
|
+
type: "boolean"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
required: ["isTableHeaderOrFooter"]
|
|
56
|
+
},
|
|
57
|
+
name: "is_table_header_or_footer",
|
|
58
|
+
description: `given a text or image content, decide if the content is a table header or footer or not.`
|
|
59
|
+
}]
|
|
60
|
+
}, {
|
|
61
|
+
headers: (0, _getAiTrackingHeaders.getAiTrackingHeaders)({
|
|
62
|
+
environment: (0, _environmentVariables.getEnvironmentVariable)("RUN_ENVIRONMENT"),
|
|
63
|
+
type: "DYNAMIC_LIST",
|
|
64
|
+
runId: (_getExecutionContext = getExecutionContext()) === null || _getExecutionContext === void 0 ? void 0 : _getExecutionContext.runId,
|
|
65
|
+
jobId: (_getExecutionContext2 = getExecutionContext()) === null || _getExecutionContext2 === void 0 ? void 0 : _getExecutionContext2.jobId,
|
|
66
|
+
jobRunId: (_getExecutionContext3 = getExecutionContext()) === null || _getExecutionContext3 === void 0 ? void 0 : _getExecutionContext3.jobRunId,
|
|
67
|
+
queueId: (_getExecutionContext4 = getExecutionContext()) === null || _getExecutionContext4 === void 0 ? void 0 : _getExecutionContext4.queueId
|
|
68
|
+
})
|
|
69
|
+
}), error => {
|
|
70
|
+
const typedError = error;
|
|
71
|
+
if (typedError.status === 449) return Errors.insufficientAiCredits(`🔴 ${typedError.error.error}`);
|
|
72
|
+
});
|
|
73
|
+
if (response.isErr() && ((_response$error = response.error) === null || _response$error === void 0 ? void 0 : _response$error.type) === "InsufficientAiCredits") {
|
|
74
|
+
return (0, _neverthrow.err)(response.error);
|
|
75
|
+
}
|
|
76
|
+
if (response.isErr()) {
|
|
77
|
+
return (0, _neverthrow.ok)({
|
|
78
|
+
isHeader: false
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const unwrappedResponse = response.value;
|
|
82
|
+
const tool = unwrappedResponse.content.find(t => t.type === "tool_use");
|
|
83
|
+
if (!tool) return (0, _neverthrow.ok)({
|
|
84
|
+
isHeader: false
|
|
85
|
+
});
|
|
86
|
+
const headersParsingResults = _zod.z.object({
|
|
87
|
+
isTableHeaderOrFooter: _zod.z.boolean()
|
|
88
|
+
}).safeParse(tool.input);
|
|
89
|
+
if (!headersParsingResults.success) {
|
|
90
|
+
return (0, _neverthrow.ok)({
|
|
91
|
+
isHeader: false
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return (0, _neverthrow.ok)({
|
|
95
|
+
isHeader: headersParsingResults.data.isTableHeaderOrFooter
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MatchSource = exports.MatchMode = void 0;
|
|
7
|
+
exports.filterAndRankMatches = filterAndRankMatches;
|
|
8
|
+
exports.matchStringsWithDomContent = matchStringsWithDomContent;
|
|
9
|
+
exports.matchStringsWithDomContentInBrowser = matchStringsWithDomContentInBrowser;
|
|
10
|
+
exports.normalizeSpacing = normalizeSpacing;
|
|
11
|
+
exports.rankMatch = rankMatch;
|
|
12
|
+
exports.removePunctuationAndSpaces = removePunctuationAndSpaces;
|
|
13
|
+
exports.replaceWithBestMatches = replaceWithBestMatches;
|
|
14
|
+
exports.selectBestMatch = selectBestMatch;
|
|
15
|
+
var _levenshtein = require("../../../common/matching/levenshtein");
|
|
16
|
+
var _ensureBrowserScripts = require("../../../common/ensureBrowserScripts");
|
|
17
|
+
let MatchMode = exports.MatchMode = function (MatchMode) {
|
|
18
|
+
MatchMode["FULL"] = "full";
|
|
19
|
+
MatchMode["PARTIAL"] = "partial";
|
|
20
|
+
MatchMode["FUZZY"] = "fuzzy";
|
|
21
|
+
return MatchMode;
|
|
22
|
+
}({});
|
|
23
|
+
let MatchSource = exports.MatchSource = function (MatchSource) {
|
|
24
|
+
MatchSource["ATTRIBUTE"] = "attribute";
|
|
25
|
+
MatchSource["TEXT_CONTENT"] = "text_content";
|
|
26
|
+
MatchSource["DIRECT_TEXT_NODE"] = "direct_text_node";
|
|
27
|
+
return MatchSource;
|
|
28
|
+
}({});
|
|
29
|
+
function normalizeSpacing(text) {
|
|
30
|
+
let normalized = text.replace(/\n/g, " ").replace(/\t/g, " ");
|
|
31
|
+
normalized = normalized.split(/\s+/).join(" ");
|
|
32
|
+
return normalized.trim();
|
|
33
|
+
}
|
|
34
|
+
function removePunctuationAndSpaces(s) {
|
|
35
|
+
return s.replace(/[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\s]/g, "");
|
|
36
|
+
}
|
|
37
|
+
function rankMatch(original, match) {
|
|
38
|
+
try {
|
|
39
|
+
const normalizedOriginal = normalizeSpacing(original).toLowerCase();
|
|
40
|
+
const normalizedMatch = normalizeSpacing(match).toLowerCase();
|
|
41
|
+
const ratio = (0, _levenshtein.levenshteinRatio)(normalizedOriginal, normalizedMatch);
|
|
42
|
+
const lenOriginal = normalizedOriginal.length;
|
|
43
|
+
if (lenOriginal > 20 && ratio > 0.85) {
|
|
44
|
+
return "HIGH";
|
|
45
|
+
}
|
|
46
|
+
const normalizedOriginalNoPunct = removePunctuationAndSpaces(normalizedOriginal);
|
|
47
|
+
const normalizedMatchNoPunct = removePunctuationAndSpaces(normalizedMatch);
|
|
48
|
+
if (normalizedOriginalNoPunct === normalizedMatchNoPunct) {
|
|
49
|
+
return "HIGH";
|
|
50
|
+
}
|
|
51
|
+
return "LOW";
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.warn("Error in rankMatch, falling back to simple comparison", error);
|
|
54
|
+
const normalizedOriginal = removePunctuationAndSpaces(normalizeSpacing(original).toLowerCase());
|
|
55
|
+
const normalizedMatch = removePunctuationAndSpaces(normalizeSpacing(match).toLowerCase());
|
|
56
|
+
if (normalizedOriginal === normalizedMatch) {
|
|
57
|
+
return "HIGH";
|
|
58
|
+
}
|
|
59
|
+
return "LOW";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function selectBestMatch(original, matches) {
|
|
63
|
+
const exactMatches = matches.filter(match => match.match_mode !== MatchMode.FUZZY);
|
|
64
|
+
if (exactMatches.length > 0) {
|
|
65
|
+
return exactMatches[0].matched_value;
|
|
66
|
+
}
|
|
67
|
+
const fuzzyMatches = matches.filter(match => match.match_mode === MatchMode.FUZZY);
|
|
68
|
+
const rankedFuzzyMatches = fuzzyMatches.map(match => ({
|
|
69
|
+
match,
|
|
70
|
+
rank: rankMatch(original, match.matched_value)
|
|
71
|
+
})).filter(({
|
|
72
|
+
rank
|
|
73
|
+
}) => rank === "HIGH");
|
|
74
|
+
if (rankedFuzzyMatches.length > 0) {
|
|
75
|
+
const sortedFuzzyMatches = rankedFuzzyMatches.sort((a, b) => {
|
|
76
|
+
const distA = a.match.fuzzy_distance ?? Infinity;
|
|
77
|
+
const distB = b.match.fuzzy_distance ?? Infinity;
|
|
78
|
+
return distA - distB;
|
|
79
|
+
});
|
|
80
|
+
return sortedFuzzyMatches[0].match.matched_value;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
async function matchStringsWithDomContent(pageObject, stringsList, container) {
|
|
85
|
+
try {
|
|
86
|
+
await (0, _ensureBrowserScripts.ensureBrowserScripts)(pageObject);
|
|
87
|
+
let handle;
|
|
88
|
+
if (container) {
|
|
89
|
+
handle = container;
|
|
90
|
+
} else {
|
|
91
|
+
handle = await pageObject.locator("html").elementHandle();
|
|
92
|
+
}
|
|
93
|
+
const matches = await pageObject.evaluate(async ([container, searchTexts]) => {
|
|
94
|
+
try {
|
|
95
|
+
if (typeof window.__INTUNED__ !== "undefined" && typeof window.__INTUNED__.matchStringsWithDomContent === "function") {
|
|
96
|
+
return await window.__INTUNED__.matchStringsWithDomContent(container, searchTexts);
|
|
97
|
+
} else {
|
|
98
|
+
return searchTexts.reduce((acc, text) => {
|
|
99
|
+
acc[text] = [];
|
|
100
|
+
return acc;
|
|
101
|
+
}, {});
|
|
102
|
+
}
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.error("Error matching strings with DOM content:", error);
|
|
105
|
+
return searchTexts.reduce((acc, text) => {
|
|
106
|
+
acc[text] = [];
|
|
107
|
+
return acc;
|
|
108
|
+
}, {});
|
|
109
|
+
}
|
|
110
|
+
}, [handle, stringsList]);
|
|
111
|
+
return matches;
|
|
112
|
+
} catch (e) {
|
|
113
|
+
console.warn("Error matching strings with DOM content:", e);
|
|
114
|
+
return stringsList.reduce((acc, string) => {
|
|
115
|
+
acc[string] = [];
|
|
116
|
+
return acc;
|
|
117
|
+
}, {});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function replaceWithBestMatches(stringsToMatch, pageObject, container) {
|
|
121
|
+
const matchesMap = await matchStringsWithDomContent(pageObject, stringsToMatch, container);
|
|
122
|
+
return Object.fromEntries(Object.entries(matchesMap).map(([string, matches]) => [string, matches.length > 0 ? selectBestMatch(string, matches) : string]));
|
|
123
|
+
}
|
|
124
|
+
async function filterAndRankMatches(frame, matches) {
|
|
125
|
+
const filteredMatches = matches.filter(match => {
|
|
126
|
+
const xpath = match.xpath;
|
|
127
|
+
return !(xpath.includes("[name()='svg']/") || xpath.includes("/path") || xpath.includes("/style"));
|
|
128
|
+
});
|
|
129
|
+
async function getVisibility(match) {
|
|
130
|
+
try {
|
|
131
|
+
const locator = frame.locator(`xpath=${match.xpath}`);
|
|
132
|
+
return await locator.isVisible({
|
|
133
|
+
timeout: 100
|
|
134
|
+
});
|
|
135
|
+
} catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const visibilityPromises = filteredMatches.map(match => getVisibility(match));
|
|
140
|
+
const visibilityResults = await Promise.all(visibilityPromises);
|
|
141
|
+
const modeOrder = {
|
|
142
|
+
full: 3,
|
|
143
|
+
partial: 2,
|
|
144
|
+
fuzzy: 1
|
|
145
|
+
};
|
|
146
|
+
const sourceOrder = {
|
|
147
|
+
direct_text_node: 3,
|
|
148
|
+
text_content: 2,
|
|
149
|
+
attribute: 1
|
|
150
|
+
};
|
|
151
|
+
function sortKey(match, visible) {
|
|
152
|
+
const modeKey = modeOrder[match.match_mode.toLowerCase()] || 0;
|
|
153
|
+
const sourceKey = sourceOrder[match.match_source.toLowerCase()] || 0;
|
|
154
|
+
const visibleKey = visible ? 1 : 0;
|
|
155
|
+
let partialScore = 0;
|
|
156
|
+
if (match.match_mode.toLowerCase() === "partial" && match.matched_source_value && match.matched_value) {
|
|
157
|
+
const extraChars = match.matched_source_value.length - match.matched_value.length;
|
|
158
|
+
partialScore = extraChars === 0 ? 1 : 1 / extraChars;
|
|
159
|
+
}
|
|
160
|
+
return modeKey + sourceKey + visibleKey + partialScore;
|
|
161
|
+
}
|
|
162
|
+
const matchesWithVisibility = filteredMatches.map((match, index) => ({
|
|
163
|
+
match,
|
|
164
|
+
visible: visibilityResults[index]
|
|
165
|
+
}));
|
|
166
|
+
const sortedMatchesWithVisibility = matchesWithVisibility.sort((a, b) => sortKey(b.match, b.visible) - sortKey(a.match, a.visible));
|
|
167
|
+
const sortedMatches = sortedMatchesWithVisibility.map(item => item.match);
|
|
168
|
+
const seenXpaths = new Set();
|
|
169
|
+
const uniqueMatches = [];
|
|
170
|
+
for (const match of sortedMatches) {
|
|
171
|
+
if (seenXpaths.has(match.xpath)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
seenXpaths.add(match.xpath);
|
|
175
|
+
uniqueMatches.push(match);
|
|
176
|
+
}
|
|
177
|
+
return uniqueMatches;
|
|
178
|
+
}
|
|
179
|
+
async function matchStringsWithDomContentInBrowser(frame, stringsList, container) {
|
|
180
|
+
let handle;
|
|
181
|
+
if (container) {
|
|
182
|
+
handle = await container.elementHandle();
|
|
183
|
+
} else {
|
|
184
|
+
handle = await frame.locator("html").elementHandle();
|
|
185
|
+
}
|
|
186
|
+
console.info(`Searching for ${stringsList.length} strings in the DOM:`, stringsList);
|
|
187
|
+
const matches = await frame.evaluate(async ([container, searchTexts]) => {
|
|
188
|
+
const result = await window.__INTUNED__.matchStringsWithDomContent(container, searchTexts);
|
|
189
|
+
return result;
|
|
190
|
+
}, [handle, stringsList]);
|
|
191
|
+
let frames = [];
|
|
192
|
+
if ("childFrames" in frame) {
|
|
193
|
+
frames = frame.childFrames();
|
|
194
|
+
} else if ("frames" in frame) {
|
|
195
|
+
frames = frame.frames();
|
|
196
|
+
}
|
|
197
|
+
for (const subframe of frames) {
|
|
198
|
+
try {
|
|
199
|
+
const frameMatches = await matchStringsWithDomContentInBrowser(subframe, stringsList, null);
|
|
200
|
+
for (const [string, stringMatches] of Object.entries(frameMatches)) {
|
|
201
|
+
if (string in matches) {
|
|
202
|
+
matches[string].push(...stringMatches);
|
|
203
|
+
} else {
|
|
204
|
+
matches[string] = stringMatches;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return matches;
|
|
212
|
+
}
|