File size: 16,739 Bytes
f316cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSDomControl = void 0;
const tsyringe_1 = require("tsyringe");
const logger_1 = require("./logger");
const readability_1 = require("@mozilla/readability");
const threaded_1 = require("../services/threaded");
const tailwind_classes_1 = require("../utils/tailwind-classes");
const openai_1 = require("../shared/utils/openai");
const async_service_1 = require("civkit/async-service");
const civ_rpc_1 = require("civkit/civ-rpc");
const pLinkedom = import('linkedom');
let JSDomControl = class JSDomControl extends async_service_1.AsyncService {
    constructor(globalLogger) {
        super(...arguments);
        this.globalLogger = globalLogger;
        this.logger = this.globalLogger.child({ service: this.constructor.name });
    }
    async init() {
        await this.dependencyReady();
        this.linkedom = await pLinkedom;
        this.emit('ready');
    }
    async narrowSnapshot(snapshot, options) {
        if (snapshot?.parsed && !options?.targetSelector && !options?.removeSelector && !options?.withIframe && !options?.withShadowDom) {
            return snapshot;
        }
        if (!snapshot?.html) {
            return snapshot;
        }
        try {
            // SideLoad contains native objects that cannot go through thread boundaries.
            return await this.actualNarrowSnapshot(snapshot, { ...options, sideLoad: undefined });
        }
        catch (err) {
            this.logger.warn(`Error narrowing snapshot`, { err });
            if (err instanceof civ_rpc_1.ApplicationError) {
                throw err;
            }
            throw new civ_rpc_1.AssertionFailureError(`Failed to process the page: ${err?.message}`);
        }
    }
    async actualNarrowSnapshot(snapshot, options) {
        const t0 = Date.now();
        let sourceHTML = snapshot.html;
        if (options?.withShadowDom && snapshot.shadowExpanded) {
            sourceHTML = snapshot.shadowExpanded;
        }
        let jsdom = this.linkedom.parseHTML(sourceHTML);
        if (!jsdom.window.document.documentElement) {
            jsdom = this.linkedom.parseHTML(`<html><body>${sourceHTML}</body></html>`);
        }
        const allNodes = [];
        jsdom.window.document.querySelectorAll('svg').forEach((x) => x.innerHTML = '');
        if (options?.withIframe) {
            jsdom.window.document.querySelectorAll('iframe[src],frame[src]').forEach((x) => {
                const src = x.getAttribute('src');
                const thisSnapshot = snapshot.childFrames?.find((f) => f.href === src);
                if (options?.withIframe === 'quoted') {
                    const blockquoteElem = jsdom.window.document.createElement('blockquote');
                    const preElem = jsdom.window.document.createElement('pre');
                    preElem.innerHTML = thisSnapshot?.text || '';
                    blockquoteElem.appendChild(preElem);
                    x.replaceWith(blockquoteElem);
                }
                else if (thisSnapshot?.html) {
                    x.innerHTML = thisSnapshot.html;
                    x.querySelectorAll('script, style').forEach((s) => s.remove());
                    if (src) {
                        x.querySelectorAll('[src]').forEach((el) => {
                            const imgSrc = el.getAttribute('src');
                            if (URL.canParse(imgSrc, src)) {
                                el.setAttribute('src', new URL(imgSrc, src).toString());
                            }
                        });
                        x.querySelectorAll('[href]').forEach((el) => {
                            const linkHref = el.getAttribute('href');
                            if (URL.canParse(linkHref, src)) {
                                el.setAttribute('href', new URL(linkHref, src).toString());
                            }
                        });
                    }
                }
            });
        }
        if (Array.isArray(options?.removeSelector)) {
            for (const rl of options.removeSelector) {
                jsdom.window.document.querySelectorAll(rl).forEach((x) => x.remove());
            }
        }
        else if (options?.removeSelector) {
            jsdom.window.document.querySelectorAll(options.removeSelector).forEach((x) => x.remove());
        }
        let bewareTargetContentDoesNotExist = false;
        if (Array.isArray(options?.targetSelector)) {
            bewareTargetContentDoesNotExist = true;
            for (const x of options.targetSelector.map((x) => jsdom.window.document.querySelectorAll(x))) {
                x.forEach((el) => {
                    if (!allNodes.includes(el)) {
                        allNodes.push(el);
                    }
                });
            }
        }
        else if (options?.targetSelector) {
            bewareTargetContentDoesNotExist = true;
            jsdom.window.document.querySelectorAll(options.targetSelector).forEach((el) => {
                if (!allNodes.includes(el)) {
                    allNodes.push(el);
                }
            });
        }
        else {
            allNodes.push(jsdom.window.document);
        }
        if (!allNodes.length) {
            if (bewareTargetContentDoesNotExist) {
                return undefined;
            }
            return snapshot;
        }
        const textNodes = [];
        let rootDoc;
        if (allNodes.length === 1 && allNodes[0].nodeName === '#document' && allNodes[0].documentElement) {
            rootDoc = allNodes[0];
            if (rootDoc.body?.innerText) {
                textNodes.push(rootDoc.body);
            }
        }
        else {
            rootDoc = this.linkedom.parseHTML('<html><body></body></html>').window.document;
            for (const n of allNodes) {
                rootDoc.body.appendChild(n);
                rootDoc.body.appendChild(rootDoc.createTextNode('\n\n'));
                if (n.innerText) {
                    textNodes.push(n);
                }
            }
        }
        const textChunks = textNodes.map((x) => {
            const clone = x.cloneNode(true);
            clone.querySelectorAll('script,style,link,svg').forEach((s) => s.remove());
            return clone.innerText;
        });
        let parsed;
        try {
            parsed = new readability_1.Readability(rootDoc.cloneNode(true)).parse();
        }
        catch (err) {
            this.logger.warn(`Failed to parse selected element`, { err });
        }
        const imgSet = new Set();
        const rebuiltImgs = [];
        Array.from(rootDoc.querySelectorAll('img[src],img[data-src]'))
            .map((x) => [x.getAttribute('src'), x.getAttribute('data-src'), x.getAttribute('alt')])
            .forEach(([u1, u2, alt]) => {
            let absUrl;
            if (u1) {
                try {
                    const u1Txt = new URL(u1, snapshot.rebase || snapshot.href).toString();
                    imgSet.add(u1Txt);
                    absUrl = u1Txt;
                }
                catch (err) {
                    // void 0;
                }
            }
            if (u2) {
                try {
                    const u2Txt = new URL(u2, snapshot.rebase || snapshot.href).toString();
                    imgSet.add(u2Txt);
                    absUrl = u2Txt;
                }
                catch (err) {
                    // void 0;
                }
            }
            if (absUrl) {
                rebuiltImgs.push({
                    src: absUrl,
                    alt
                });
            }
        });
        const r = {
            ...snapshot,
            title: snapshot.title || jsdom.window.document.title,
            description: snapshot.description ||
                (jsdom.window.document.head?.querySelector('meta[name="description"]')?.getAttribute('content') ?? ''),
            parsed,
            html: rootDoc.documentElement.outerHTML,
            text: textChunks.join('\n'),
            imgs: (snapshot.imgs || rebuiltImgs)?.filter((x) => imgSet.has(x.src)) || [],
        };
        const dt = Date.now() - t0;
        if (dt > 1000) {
            this.logger.warn(`Performance issue: Narrowing snapshot took ${dt}ms`, { url: snapshot.href, dt });
        }
        return r;
    }
    async inferSnapshot(snapshot) {
        const t0 = Date.now();
        const extendedSnapshot = { ...snapshot };
        try {
            const jsdom = this.linkedom.parseHTML(snapshot.html);
            jsdom.window.document.querySelectorAll('svg').forEach((x) => x.innerHTML = '');
            const links = Array.from(jsdom.window.document.querySelectorAll('a[href]'))
                .map((x) => [x.textContent.replace(/\s+/g, ' ').trim(), x.getAttribute('href'),])
                .map(([text, href]) => {
                if (!href) {
                    return undefined;
                }
                try {
                    const parsed = new URL(href, snapshot.rebase || snapshot.href);
                    return [text, parsed.toString()];
                }
                catch (err) {
                    return undefined;
                }
            })
                .filter(Boolean);
            extendedSnapshot.links = links;
            const imgs = Array.from(jsdom.window.document.querySelectorAll('img[src],img[data-src]'))
                .map((x) => {
                let linkPreferredSrc = x.getAttribute('src') || '';
                if (linkPreferredSrc.startsWith('data:')) {
                    const dataSrc = x.getAttribute('data-src') || '';
                    if (dataSrc && !dataSrc.startsWith('data:')) {
                        linkPreferredSrc = dataSrc;
                    }
                }
                return {
                    src: new URL(linkPreferredSrc, snapshot.rebase || snapshot.href).toString(),
                    width: parseInt(x.getAttribute('width') || '0'),
                    height: parseInt(x.getAttribute('height') || '0'),
                    alt: x.getAttribute('alt') || x.getAttribute('title'),
                };
            });
            extendedSnapshot.imgs = imgs;
        }
        catch (_err) {
            void 0;
        }
        const dt = Date.now() - t0;
        if (dt > 1000) {
            this.logger.warn(`Performance issue: Inferring snapshot took ${dt}ms`, { url: snapshot.href, dt });
        }
        return extendedSnapshot;
    }
    cleanRedundantEmptyLines(text) {
        const lines = text.split(/\r?\n/g);
        const mappedFlag = lines.map((line) => Boolean(line.trim()));
        return lines.filter((_line, i) => mappedFlag[i] || mappedFlag[i - 1]).join('\n');
    }
    async cleanHTMLforLMs(sourceHTML, ...discardSelectors) {
        const t0 = Date.now();
        let jsdom = this.linkedom.parseHTML(sourceHTML);
        if (!jsdom.window.document.documentElement) {
            jsdom = this.linkedom.parseHTML(`<html><body>${sourceHTML}</body></html>`);
        }
        for (const rl of discardSelectors) {
            jsdom.window.document.querySelectorAll(rl).forEach((x) => x.remove());
        }
        jsdom.window.document.querySelectorAll('img[src],img[data-src]').forEach((x) => {
            const src = x.getAttribute('src') || x.getAttribute('data-src');
            if (src?.startsWith('data:')) {
                x.setAttribute('src', 'blob:opaque');
            }
            x.removeAttribute('data-src');
            x.removeAttribute('srcset');
        });
        jsdom.window.document.querySelectorAll('[class]').forEach((x) => {
            const classes = x.getAttribute('class')?.split(/\s+/g) || [];
            const newClasses = classes.filter((c) => !tailwind_classes_1.tailwindClasses.has(c));
            x.setAttribute('class', newClasses.join(' '));
        });
        jsdom.window.document.querySelectorAll('[style]').forEach((x) => {
            const style = x.getAttribute('style')?.toLocaleLowerCase() || '';
            if (style.startsWith('display: none')) {
                return;
            }
            x.removeAttribute('style');
        });
        const treeWalker = jsdom.window.document.createTreeWalker(jsdom.window.document, // Start from the root document
        0x80 // Only show comment nodes
        );
        let currentNode;
        while ((currentNode = treeWalker.nextNode())) {
            currentNode.parentNode?.removeChild(currentNode); // Remove each comment node
        }
        jsdom.window.document.querySelectorAll('*').forEach((x) => {
            const attrs = x.getAttributeNames();
            for (const attr of attrs) {
                if (attr.startsWith('data-') || attr.startsWith('aria-')) {
                    x.removeAttribute(attr);
                }
            }
        });
        const final = this.cleanRedundantEmptyLines(jsdom.window.document.documentElement.outerHTML);
        const dt = Date.now() - t0;
        if (dt > 1000) {
            this.logger.warn(`Performance issue: Cleaning HTML for LMs took ${dt}ms`, { dt });
        }
        return final;
    }
    snippetToElement(snippet, url) {
        const parsed = this.linkedom.parseHTML(snippet || '<html><body></body></html>');
        // Hack for turndown gfm table plugin.
        parsed.window.document.querySelectorAll('table').forEach((x) => {
            Object.defineProperty(x, 'rows', { value: Array.from(x.querySelectorAll('tr')), enumerable: true });
        });
        Object.defineProperty(parsed.window.document.documentElement, 'cloneNode', {
            value: function () { return this; },
        });
        return parsed.window.document.documentElement;
    }
    runTurndown(turndownService, html) {
        const t0 = Date.now();
        try {
            return turndownService.turndown(html);
        }
        finally {
            const dt = Date.now() - t0;
            if (dt > 1000) {
                this.logger.warn(`Performance issue: Turndown took ${dt}ms`, { dt });
            }
        }
    }
    async analyzeHTMLTextLite(sourceHTML) {
        let jsdom = this.linkedom.parseHTML(sourceHTML);
        if (!jsdom.window.document.documentElement) {
            jsdom = this.linkedom.parseHTML(`<html><body>${sourceHTML}</body></html>`);
        }
        jsdom.window.document.querySelectorAll('script,style,link,svg').forEach((s) => s.remove());
        const text = jsdom.window.document.body.innerText || '';
        return {
            title: jsdom.window.document.title,
            text,
            tokens: (0, openai_1.countGPTToken)(text.replaceAll(/[\s\r\n\t]+/g, ' ')),
        };
    }
};
exports.JSDomControl = JSDomControl;
__decorate([
    (0, threaded_1.Threaded)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], JSDomControl.prototype, "actualNarrowSnapshot", null);
__decorate([
    (0, threaded_1.Threaded)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], JSDomControl.prototype, "inferSnapshot", null);
__decorate([
    (0, threaded_1.Threaded)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], JSDomControl.prototype, "cleanHTMLforLMs", null);
__decorate([
    (0, threaded_1.Threaded)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], JSDomControl.prototype, "analyzeHTMLTextLite", null);
exports.JSDomControl = JSDomControl = __decorate([
    (0, tsyringe_1.singleton)(),
    __metadata("design:paramtypes", [logger_1.GlobalLogger])
], JSDomControl);
const jsdomControl = tsyringe_1.container.resolve(JSDomControl);
exports.default = jsdomControl;
//# sourceMappingURL=jsdom.js.map