@librechat/agents 2.4.31 → 2.4.33
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/cjs/events.cjs +3 -3
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +2 -1
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/main.cjs +5 -2
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/ids.cjs +23 -0
- package/dist/cjs/messages/ids.cjs.map +1 -0
- package/dist/cjs/splitStream.cjs +2 -1
- package/dist/cjs/splitStream.cjs.map +1 -1
- package/dist/cjs/stream.cjs +87 -154
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +14 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +144 -0
- package/dist/cjs/tools/handlers.cjs.map +1 -0
- package/dist/cjs/tools/search/content.cjs +140 -0
- package/dist/cjs/tools/search/content.cjs.map +1 -0
- package/dist/cjs/tools/search/firecrawl.cjs +23 -41
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/format.cjs +161 -74
- package/dist/cjs/tools/search/format.cjs.map +1 -1
- package/dist/cjs/tools/search/highlights.cjs +64 -12
- package/dist/cjs/tools/search/highlights.cjs.map +1 -1
- package/dist/cjs/tools/search/rerankers.cjs +43 -36
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/schema.cjs +70 -0
- package/dist/cjs/tools/search/schema.cjs.map +1 -0
- package/dist/cjs/tools/search/search.cjs +150 -69
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +247 -58
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +66 -0
- package/dist/cjs/tools/search/utils.cjs.map +1 -0
- package/dist/esm/events.mjs +1 -1
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +2 -1
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/main.mjs +3 -1
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/ids.mjs +21 -0
- package/dist/esm/messages/ids.mjs.map +1 -0
- package/dist/esm/splitStream.mjs +2 -1
- package/dist/esm/splitStream.mjs.map +1 -1
- package/dist/esm/stream.mjs +87 -152
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +14 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +141 -0
- package/dist/esm/tools/handlers.mjs.map +1 -0
- package/dist/esm/tools/search/content.mjs +119 -0
- package/dist/esm/tools/search/content.mjs.map +1 -0
- package/dist/esm/tools/search/firecrawl.mjs +24 -41
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/format.mjs +161 -74
- package/dist/esm/tools/search/format.mjs.map +1 -1
- package/dist/esm/tools/search/highlights.mjs +64 -12
- package/dist/esm/tools/search/highlights.mjs.map +1 -1
- package/dist/esm/tools/search/rerankers.mjs +43 -36
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/schema.mjs +61 -0
- package/dist/esm/tools/search/schema.mjs.map +1 -0
- package/dist/esm/tools/search/search.mjs +150 -69
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +246 -57
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +61 -0
- package/dist/esm/tools/search/utils.mjs.map +1 -0
- package/dist/types/graphs/Graph.d.ts +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/messages/ids.d.ts +3 -0
- package/dist/types/messages/index.d.ts +1 -0
- package/dist/types/stream.d.ts +0 -8
- package/dist/types/tools/ToolNode.d.ts +6 -0
- package/dist/types/tools/example.d.ts +23 -3
- package/dist/types/tools/handlers.d.ts +8 -0
- package/dist/types/tools/search/content.d.ts +4 -0
- package/dist/types/tools/search/firecrawl.d.ts +7 -86
- package/dist/types/tools/search/format.d.ts +4 -1
- package/dist/types/tools/search/highlights.d.ts +1 -1
- package/dist/types/tools/search/rerankers.d.ts +8 -4
- package/dist/types/tools/search/schema.d.ts +16 -0
- package/dist/types/tools/search/search.d.ts +2 -2
- package/dist/types/tools/search/test.d.ts +1 -0
- package/dist/types/tools/search/tool.d.ts +25 -4
- package/dist/types/tools/search/types.d.ts +443 -53
- package/dist/types/tools/search/utils.d.ts +10 -0
- package/package.json +9 -7
- package/src/events.ts +49 -15
- package/src/graphs/Graph.ts +6 -2
- package/src/index.ts +1 -0
- package/src/messages/ids.ts +26 -0
- package/src/messages/index.ts +1 -0
- package/src/scripts/search.ts +8 -3
- package/src/splitStream.test.ts +132 -71
- package/src/splitStream.ts +2 -1
- package/src/stream.ts +94 -183
- package/src/tools/ToolNode.ts +37 -14
- package/src/tools/handlers.ts +167 -0
- package/src/tools/search/content.test.ts +173 -0
- package/src/tools/search/content.ts +147 -0
- package/src/tools/search/firecrawl.ts +36 -148
- package/src/tools/search/format.ts +205 -74
- package/src/tools/search/highlights.ts +99 -16
- package/src/tools/search/output.md +2775 -0
- package/src/tools/search/rerankers.ts +57 -36
- package/src/tools/search/schema.ts +63 -0
- package/src/tools/search/search.ts +230 -117
- package/src/tools/search/test.html +884 -0
- package/src/tools/search/test.md +643 -0
- package/src/tools/search/test.ts +159 -0
- package/src/tools/search/tool.ts +363 -87
- package/src/tools/search/types.ts +503 -61
- package/src/tools/search/utils.ts +79 -0
- package/src/utils/llmConfig.ts +1 -1
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
// content.test.ts
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import { processContent } from './content';
|
|
6
|
+
|
|
7
|
+
describe('Link Processor', () => {
|
|
8
|
+
afterAll(() => {
|
|
9
|
+
if (fs.existsSync('./temp.html')) {
|
|
10
|
+
fs.unlinkSync('./temp.html');
|
|
11
|
+
}
|
|
12
|
+
if (fs.existsSync('./temp.md')) {
|
|
13
|
+
fs.unlinkSync('./temp.md');
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
// Basic functionality tests
|
|
17
|
+
test('should replace basic links with references', () => {
|
|
18
|
+
const html = `
|
|
19
|
+
<p>Test with <a href="https://example.com/link" title="Example">a link</a></p>
|
|
20
|
+
<p>And an <img src="https://example.com/img.jpg" alt="image"></p>
|
|
21
|
+
<p>Plus a <video src="https://example.com/video.mp4"></video></p>
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
const markdown = `
|
|
25
|
+
Test with [a link](https://example.com/link "Example")
|
|
26
|
+
And an 
|
|
27
|
+
Plus a [video](https://example.com/video.mp4)
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
const result = processContent(html, markdown);
|
|
31
|
+
|
|
32
|
+
expect(result.links.length).toBe(1);
|
|
33
|
+
expect(result.images.length).toBe(1);
|
|
34
|
+
expect(result.videos.length).toBe(1);
|
|
35
|
+
expect(result.markdown).toContain('link#1');
|
|
36
|
+
expect(result.markdown).toContain('image#1');
|
|
37
|
+
expect(result.markdown).toContain('video#1');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Edge case tests
|
|
41
|
+
test('should handle links with parentheses and special characters', () => {
|
|
42
|
+
const html = `
|
|
43
|
+
<a href="https://example.com/page(1).html" title="Parens">Link with parens</a>
|
|
44
|
+
<a href="https://example.com/path?query=test¶m=value">Link with query</a>
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const markdown = `
|
|
48
|
+
[Link with parens](https://example.com/page(1).html "Parens")
|
|
49
|
+
[Link with query](https://example.com/path?query=test¶m=value)
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
const result = processContent(html, markdown);
|
|
53
|
+
|
|
54
|
+
expect(result.links.length).toBe(2);
|
|
55
|
+
expect(result.markdown).toContain('link#1');
|
|
56
|
+
expect(result.markdown).toContain('link#2');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Performance test with large files
|
|
60
|
+
test('should process large files efficiently', () => {
|
|
61
|
+
const html = fs.readFileSync('src/tools/search/test.html', 'utf-8');
|
|
62
|
+
const markdown = fs.readFileSync('src/tools/search/test.md', 'utf-8');
|
|
63
|
+
|
|
64
|
+
// const largeHtml = generateLargeHtml(1000); // 1000 links
|
|
65
|
+
// fs.writeFileSync('./temp.html', largeHtml);
|
|
66
|
+
|
|
67
|
+
// const largeMd = generateLargeMarkdown(1000); // 1000 links
|
|
68
|
+
// fs.writeFileSync('./temp.md', largeMd);
|
|
69
|
+
|
|
70
|
+
// const html = fs.readFileSync('./temp.html', 'utf-8');
|
|
71
|
+
// const markdown = fs.readFileSync('./temp.md', 'utf-8');
|
|
72
|
+
|
|
73
|
+
// Measure time taken to process
|
|
74
|
+
const startTime = process.hrtime();
|
|
75
|
+
const result = processContent(html, markdown);
|
|
76
|
+
const elapsed = process.hrtime(startTime);
|
|
77
|
+
const timeInMs = elapsed[0] * 1000 + elapsed[1] / 1000000;
|
|
78
|
+
|
|
79
|
+
console.log(
|
|
80
|
+
`Processed ${result.links.length} links, ${result.images.length} images, and ${result.videos.length} videos in ${timeInMs.toFixed(2)}ms`
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// Basic validations for large file processing
|
|
84
|
+
expect(result.links.length).toBeGreaterThan(0);
|
|
85
|
+
expect(result.markdown).toContain('link#');
|
|
86
|
+
|
|
87
|
+
// Check if all links were replaced (sample check)
|
|
88
|
+
expect(result.markdown).not.toContain('https://example.com/link');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Memory usage test
|
|
92
|
+
test('should have reasonable memory usage', () => {
|
|
93
|
+
const html = fs.readFileSync('src/tools/search/test.html', 'utf-8');
|
|
94
|
+
const markdown = fs.readFileSync('src/tools/search/test.md', 'utf-8');
|
|
95
|
+
|
|
96
|
+
const beforeMem = process.memoryUsage();
|
|
97
|
+
processContent(html, markdown);
|
|
98
|
+
const afterMem = process.memoryUsage();
|
|
99
|
+
|
|
100
|
+
const heapUsed = (afterMem.heapUsed - beforeMem.heapUsed) / 1024 / 1024; // MB
|
|
101
|
+
|
|
102
|
+
console.log(`Memory used: ${heapUsed.toFixed(2)} MB`);
|
|
103
|
+
|
|
104
|
+
// This is a loose check - actual thresholds depend on your environment
|
|
105
|
+
expect(heapUsed).toBeLessThan(100); // Should use less than 100MB additional heap
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Real-world file test (if available)
|
|
109
|
+
test('should process real-world Wikipedia content', () => {
|
|
110
|
+
// Try to find real-world test files if they exist
|
|
111
|
+
const wikiHtml = 'src/tools/search/test.html';
|
|
112
|
+
const wikiMd = 'src/tools/search/test.md';
|
|
113
|
+
|
|
114
|
+
if (fs.existsSync(wikiHtml) && fs.existsSync(wikiMd)) {
|
|
115
|
+
const html = fs.readFileSync(wikiHtml, 'utf-8');
|
|
116
|
+
const markdown = fs.readFileSync(wikiMd, 'utf-8');
|
|
117
|
+
|
|
118
|
+
const result = processContent(html, markdown);
|
|
119
|
+
|
|
120
|
+
console.log(
|
|
121
|
+
`Processed ${result.links.length} Wikipedia links, ${result.images.length} images, and ${result.videos.length} videos`
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(result.links.length).toBeGreaterThan(10); // Wikipedia articles typically have many links
|
|
125
|
+
expect(result.markdown).not.toMatch(/\]\(https?:\/\/[^\s")]+\)/); // No regular URLs should remain
|
|
126
|
+
} else {
|
|
127
|
+
console.log('Wikipedia test files not found, skipping this test');
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Helper function to generate large HTML test data
|
|
133
|
+
function generateLargeHtml(linkCount: number): string {
|
|
134
|
+
let html = '<html><body>';
|
|
135
|
+
|
|
136
|
+
for (let i = 1; i <= linkCount; i++) {
|
|
137
|
+
html += `<p>Paragraph ${i} with <a href="https://example.com/link${i}" title="Link ${i}">link ${i}</a>`;
|
|
138
|
+
|
|
139
|
+
if (i % 10 === 0) {
|
|
140
|
+
html += ` and <img src="https://example.com/image${i / 10}.jpg" alt="Image ${i / 10}">`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (i % 50 === 0) {
|
|
144
|
+
html += ` and <video src="https://example.com/video${i / 50}.mp4" title="Video ${i / 50}"></video>`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
html += '</p>';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
html += '</body></html>';
|
|
151
|
+
return html;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Helper function to generate large Markdown test data */
|
|
155
|
+
function generateLargeMarkdown(linkCount: number): string {
|
|
156
|
+
let markdown = '# Test Document\n\n';
|
|
157
|
+
|
|
158
|
+
for (let i = 1; i <= linkCount; i++) {
|
|
159
|
+
markdown += `Paragraph ${i} with [link ${i}](https://example.com/link${i} "Link ${i}")`;
|
|
160
|
+
|
|
161
|
+
if (i % 10 === 0) {
|
|
162
|
+
markdown += ` and `;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (i % 50 === 0) {
|
|
166
|
+
markdown += ` and [Video ${i / 50}](https://example.com/video${i / 50}.mp4 "Video ${i / 50}")`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
markdown += '\n\n';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return markdown;
|
|
173
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import * as cheerio from 'cheerio';
|
|
2
|
+
import type { References, MediaReference } from './types';
|
|
3
|
+
|
|
4
|
+
export function processContent(
|
|
5
|
+
html: string,
|
|
6
|
+
markdown: string
|
|
7
|
+
): {
|
|
8
|
+
markdown: string;
|
|
9
|
+
} & References {
|
|
10
|
+
const linkMap = new Map<string, MediaReference>();
|
|
11
|
+
const imageMap = new Map<string, MediaReference>();
|
|
12
|
+
const videoMap = new Map<string, MediaReference>();
|
|
13
|
+
const iframeMap = new Map<string, MediaReference>();
|
|
14
|
+
|
|
15
|
+
const $ = cheerio.load(html, {
|
|
16
|
+
xmlMode: false,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Extract all media references
|
|
20
|
+
$('a[href]').each((_, el) => {
|
|
21
|
+
const href = $(el).attr('href');
|
|
22
|
+
if (href != null && href) {
|
|
23
|
+
linkMap.set(href, {
|
|
24
|
+
originalUrl: href,
|
|
25
|
+
title: $(el).attr('title'),
|
|
26
|
+
text: $(el).text().trim(),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
$('img[src]').each((_, el) => {
|
|
32
|
+
const src = $(el).attr('src');
|
|
33
|
+
if (src != null && src) {
|
|
34
|
+
imageMap.set(src, {
|
|
35
|
+
originalUrl: src,
|
|
36
|
+
title: $(el).attr('alt') ?? $(el).attr('title'),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Handle videos (dedicated video elements and video platforms in iframes)
|
|
42
|
+
$('video[src], iframe[src*="youtube"], iframe[src*="vimeo"]').each(
|
|
43
|
+
(_, el) => {
|
|
44
|
+
const src = $(el).attr('src');
|
|
45
|
+
if (src != null && src) {
|
|
46
|
+
videoMap.set(src, {
|
|
47
|
+
originalUrl: src,
|
|
48
|
+
title: $(el).attr('title'),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// Handle all other generic iframes that aren't already captured as videos
|
|
55
|
+
$('iframe').each((_, el) => {
|
|
56
|
+
const src = $(el).attr('src');
|
|
57
|
+
if (
|
|
58
|
+
src != null &&
|
|
59
|
+
src &&
|
|
60
|
+
!src.includes('youtube') &&
|
|
61
|
+
!src.includes('vimeo')
|
|
62
|
+
) {
|
|
63
|
+
iframeMap.set(src, {
|
|
64
|
+
originalUrl: src,
|
|
65
|
+
title: $(el).attr('title'),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Create lookup maps with indices
|
|
71
|
+
const linkIndexMap = new Map<string, number>();
|
|
72
|
+
const imageIndexMap = new Map<string, number>();
|
|
73
|
+
const videoIndexMap = new Map<string, number>();
|
|
74
|
+
const iframeIndexMap = new Map<string, number>();
|
|
75
|
+
|
|
76
|
+
Array.from(linkMap.keys()).forEach((url, i) => linkIndexMap.set(url, i + 1));
|
|
77
|
+
Array.from(imageMap.keys()).forEach((url, i) =>
|
|
78
|
+
imageIndexMap.set(url, i + 1)
|
|
79
|
+
);
|
|
80
|
+
Array.from(videoMap.keys()).forEach((url, i) =>
|
|
81
|
+
videoIndexMap.set(url, i + 1)
|
|
82
|
+
);
|
|
83
|
+
Array.from(iframeMap.keys()).forEach((url, i) =>
|
|
84
|
+
iframeIndexMap.set(url, i + 1)
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// Process the markdown
|
|
88
|
+
let result = markdown;
|
|
89
|
+
|
|
90
|
+
// Replace each URL one by one, starting with the longest URLs first to avoid partial matches
|
|
91
|
+
const allUrls = [
|
|
92
|
+
...Array.from(imageMap.keys()).map((url) => ({
|
|
93
|
+
url,
|
|
94
|
+
type: 'image',
|
|
95
|
+
idx: imageIndexMap.get(url),
|
|
96
|
+
})),
|
|
97
|
+
...Array.from(videoMap.keys()).map((url) => ({
|
|
98
|
+
url,
|
|
99
|
+
type: 'video',
|
|
100
|
+
idx: videoIndexMap.get(url),
|
|
101
|
+
})),
|
|
102
|
+
...Array.from(iframeMap.keys()).map((url) => ({
|
|
103
|
+
url,
|
|
104
|
+
type: 'iframe',
|
|
105
|
+
idx: iframeIndexMap.get(url),
|
|
106
|
+
})),
|
|
107
|
+
...Array.from(linkMap.keys()).map((url) => ({
|
|
108
|
+
url,
|
|
109
|
+
type: 'link',
|
|
110
|
+
idx: linkIndexMap.get(url),
|
|
111
|
+
})),
|
|
112
|
+
].sort((a, b) => b.url.length - a.url.length);
|
|
113
|
+
|
|
114
|
+
// Create a function to escape special characters in URLs for regex
|
|
115
|
+
function escapeRegex(string: string): string {
|
|
116
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Replace each URL in the markdown
|
|
120
|
+
for (const { url, type, idx } of allUrls) {
|
|
121
|
+
// Create a regex that captures URLs in markdown links
|
|
122
|
+
const regex = new RegExp(`\\(${escapeRegex(url)}(?:\\s+"[^"]*")?\\)`, 'g');
|
|
123
|
+
|
|
124
|
+
result = result.replace(regex, (match) => {
|
|
125
|
+
// Keep any title attribute that might exist
|
|
126
|
+
const titleMatch = match.match(/\s+"([^"]*)"/);
|
|
127
|
+
const titlePart = titleMatch ? ` "${titleMatch[1]}"` : '';
|
|
128
|
+
|
|
129
|
+
return `(${type}#${idx}${titlePart})`;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
iframeMap.clear();
|
|
134
|
+
const links = Array.from(linkMap.values());
|
|
135
|
+
linkMap.clear();
|
|
136
|
+
const images = Array.from(imageMap.values());
|
|
137
|
+
imageMap.clear();
|
|
138
|
+
const videos = Array.from(videoMap.values());
|
|
139
|
+
videoMap.clear();
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
markdown: result,
|
|
143
|
+
links,
|
|
144
|
+
images,
|
|
145
|
+
videos,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
@@ -1,136 +1,7 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
1
|
import axios from 'axios';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
includeTags?: string[];
|
|
7
|
-
excludeTags?: string[];
|
|
8
|
-
headers?: Record<string, string>;
|
|
9
|
-
waitFor?: number;
|
|
10
|
-
timeout?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface ScrapeMetadata {
|
|
14
|
-
// Core source information
|
|
15
|
-
sourceURL?: string;
|
|
16
|
-
url?: string;
|
|
17
|
-
scrapeId?: string;
|
|
18
|
-
statusCode?: number;
|
|
19
|
-
// Basic metadata
|
|
20
|
-
title?: string;
|
|
21
|
-
description?: string;
|
|
22
|
-
language?: string;
|
|
23
|
-
favicon?: string;
|
|
24
|
-
viewport?: string;
|
|
25
|
-
robots?: string;
|
|
26
|
-
'theme-color'?: string;
|
|
27
|
-
// Open Graph metadata
|
|
28
|
-
'og:url'?: string;
|
|
29
|
-
'og:title'?: string;
|
|
30
|
-
'og:description'?: string;
|
|
31
|
-
'og:type'?: string;
|
|
32
|
-
'og:image'?: string;
|
|
33
|
-
'og:image:width'?: string;
|
|
34
|
-
'og:image:height'?: string;
|
|
35
|
-
'og:site_name'?: string;
|
|
36
|
-
ogUrl?: string;
|
|
37
|
-
ogTitle?: string;
|
|
38
|
-
ogDescription?: string;
|
|
39
|
-
ogImage?: string;
|
|
40
|
-
ogSiteName?: string;
|
|
41
|
-
// Article metadata
|
|
42
|
-
'article:author'?: string;
|
|
43
|
-
'article:published_time'?: string;
|
|
44
|
-
'article:modified_time'?: string;
|
|
45
|
-
'article:section'?: string;
|
|
46
|
-
'article:tag'?: string;
|
|
47
|
-
'article:publisher'?: string;
|
|
48
|
-
publishedTime?: string;
|
|
49
|
-
modifiedTime?: string;
|
|
50
|
-
// Twitter metadata
|
|
51
|
-
'twitter:site'?: string;
|
|
52
|
-
'twitter:creator'?: string;
|
|
53
|
-
'twitter:card'?: string;
|
|
54
|
-
'twitter:image'?: string;
|
|
55
|
-
'twitter:dnt'?: string;
|
|
56
|
-
'twitter:app:name:iphone'?: string;
|
|
57
|
-
'twitter:app:id:iphone'?: string;
|
|
58
|
-
'twitter:app:url:iphone'?: string;
|
|
59
|
-
'twitter:app:name:ipad'?: string;
|
|
60
|
-
'twitter:app:id:ipad'?: string;
|
|
61
|
-
'twitter:app:url:ipad'?: string;
|
|
62
|
-
'twitter:app:name:googleplay'?: string;
|
|
63
|
-
'twitter:app:id:googleplay'?: string;
|
|
64
|
-
'twitter:app:url:googleplay'?: string;
|
|
65
|
-
// Facebook metadata
|
|
66
|
-
'fb:app_id'?: string;
|
|
67
|
-
// App links
|
|
68
|
-
'al:ios:url'?: string;
|
|
69
|
-
'al:ios:app_name'?: string;
|
|
70
|
-
'al:ios:app_store_id'?: string;
|
|
71
|
-
// Allow for additional properties that might be present
|
|
72
|
-
[key: string]: string | number | boolean | null | undefined;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface FirecrawlScrapeResponse {
|
|
76
|
-
success: boolean;
|
|
77
|
-
data?: {
|
|
78
|
-
markdown?: string;
|
|
79
|
-
html?: string;
|
|
80
|
-
rawHtml?: string;
|
|
81
|
-
screenshot?: string;
|
|
82
|
-
links?: string[];
|
|
83
|
-
metadata?: ScrapeMetadata;
|
|
84
|
-
};
|
|
85
|
-
error?: string;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export interface FirecrawlScraperConfig {
|
|
89
|
-
apiKey?: string;
|
|
90
|
-
apiUrl?: string;
|
|
91
|
-
formats?: string[];
|
|
92
|
-
timeout?: number;
|
|
93
|
-
}
|
|
94
|
-
const getDomainName = (
|
|
95
|
-
link: string,
|
|
96
|
-
metadata?: ScrapeMetadata
|
|
97
|
-
): string | undefined => {
|
|
98
|
-
try {
|
|
99
|
-
const url = metadata?.sourceURL ?? metadata?.url ?? (link || '');
|
|
100
|
-
const domain = new URL(url).hostname.replace(/^www\./, '');
|
|
101
|
-
if (domain) {
|
|
102
|
-
return domain;
|
|
103
|
-
}
|
|
104
|
-
} catch (e) {
|
|
105
|
-
// URL parsing failed
|
|
106
|
-
console.error('Error parsing URL:', e);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export function getAttribution(
|
|
113
|
-
link: string,
|
|
114
|
-
metadata?: ScrapeMetadata
|
|
115
|
-
): string | undefined {
|
|
116
|
-
if (!metadata) return getDomainName(link, metadata);
|
|
117
|
-
|
|
118
|
-
const possibleAttributions = [
|
|
119
|
-
metadata.ogSiteName,
|
|
120
|
-
metadata['og:site_name'],
|
|
121
|
-
metadata.title?.split('|').pop()?.trim(),
|
|
122
|
-
metadata['twitter:site']?.replace(/^@/, ''),
|
|
123
|
-
];
|
|
124
|
-
|
|
125
|
-
const attribution = possibleAttributions.find(
|
|
126
|
-
(attr) => attr != null && typeof attr === 'string' && attr.trim() !== ''
|
|
127
|
-
);
|
|
128
|
-
if (attribution != null) {
|
|
129
|
-
return attribution;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return getDomainName(link, metadata);
|
|
133
|
-
}
|
|
2
|
+
import { processContent } from './content';
|
|
3
|
+
import type * as t from './types';
|
|
4
|
+
import { createDefaultLogger } from './utils';
|
|
134
5
|
|
|
135
6
|
/**
|
|
136
7
|
* Firecrawl scraper implementation
|
|
@@ -141,8 +12,9 @@ export class FirecrawlScraper {
|
|
|
141
12
|
private apiUrl: string;
|
|
142
13
|
private defaultFormats: string[];
|
|
143
14
|
private timeout: number;
|
|
15
|
+
private logger: t.Logger;
|
|
144
16
|
|
|
145
|
-
constructor(config: FirecrawlScraperConfig = {}) {
|
|
17
|
+
constructor(config: t.FirecrawlScraperConfig = {}) {
|
|
146
18
|
this.apiKey = config.apiKey ?? process.env.FIRECRAWL_API_KEY ?? '';
|
|
147
19
|
|
|
148
20
|
const baseUrl =
|
|
@@ -152,13 +24,17 @@ export class FirecrawlScraper {
|
|
|
152
24
|
this.apiUrl = `${baseUrl.replace(/\/+$/, '')}/v1/scrape`;
|
|
153
25
|
|
|
154
26
|
this.defaultFormats = config.formats ?? ['markdown', 'html'];
|
|
155
|
-
this.timeout = config.timeout ??
|
|
27
|
+
this.timeout = config.timeout ?? 15000;
|
|
28
|
+
|
|
29
|
+
this.logger = config.logger || createDefaultLogger();
|
|
156
30
|
|
|
157
31
|
if (!this.apiKey) {
|
|
158
|
-
|
|
32
|
+
this.logger.warn('FIRECRAWL_API_KEY is not set. Scraping will not work.');
|
|
159
33
|
}
|
|
160
34
|
|
|
161
|
-
|
|
35
|
+
this.logger.debug(
|
|
36
|
+
`Firecrawl scraper initialized with API URL: ${this.apiUrl}`
|
|
37
|
+
);
|
|
162
38
|
}
|
|
163
39
|
|
|
164
40
|
/**
|
|
@@ -169,8 +45,8 @@ export class FirecrawlScraper {
|
|
|
169
45
|
*/
|
|
170
46
|
async scrapeUrl(
|
|
171
47
|
url: string,
|
|
172
|
-
options: FirecrawlScrapeOptions = {}
|
|
173
|
-
): Promise<[string, FirecrawlScrapeResponse]> {
|
|
48
|
+
options: t.FirecrawlScrapeOptions = {}
|
|
49
|
+
): Promise<[string, t.FirecrawlScrapeResponse]> {
|
|
174
50
|
if (!this.apiKey) {
|
|
175
51
|
return [
|
|
176
52
|
url,
|
|
@@ -221,27 +97,39 @@ export class FirecrawlScraper {
|
|
|
221
97
|
* @param response Scrape response
|
|
222
98
|
* @returns Extracted content or empty string if not available
|
|
223
99
|
*/
|
|
224
|
-
extractContent(
|
|
100
|
+
extractContent(
|
|
101
|
+
response: t.FirecrawlScrapeResponse
|
|
102
|
+
): [string, undefined | t.References] {
|
|
225
103
|
if (!response.success || !response.data) {
|
|
226
|
-
return '';
|
|
104
|
+
return ['', undefined];
|
|
227
105
|
}
|
|
228
106
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
107
|
+
if (response.data.markdown != null && response.data.html != null) {
|
|
108
|
+
try {
|
|
109
|
+
const { markdown, ...rest } = processContent(
|
|
110
|
+
response.data.html,
|
|
111
|
+
response.data.markdown
|
|
112
|
+
);
|
|
113
|
+
return [markdown, rest];
|
|
114
|
+
} catch (error) {
|
|
115
|
+
this.logger.error('Error processing content:', error);
|
|
116
|
+
return [response.data.markdown, undefined];
|
|
117
|
+
}
|
|
118
|
+
} else if (response.data.markdown != null) {
|
|
119
|
+
return [response.data.markdown, undefined];
|
|
232
120
|
}
|
|
233
121
|
|
|
234
122
|
// Fall back to HTML content
|
|
235
123
|
if (response.data.html != null) {
|
|
236
|
-
return response.data.html;
|
|
124
|
+
return [response.data.html, undefined];
|
|
237
125
|
}
|
|
238
126
|
|
|
239
127
|
// Fall back to raw HTML content
|
|
240
128
|
if (response.data.rawHtml != null) {
|
|
241
|
-
return response.data.rawHtml;
|
|
129
|
+
return [response.data.rawHtml, undefined];
|
|
242
130
|
}
|
|
243
131
|
|
|
244
|
-
return '';
|
|
132
|
+
return ['', undefined];
|
|
245
133
|
}
|
|
246
134
|
|
|
247
135
|
/**
|
|
@@ -249,7 +137,7 @@ export class FirecrawlScraper {
|
|
|
249
137
|
* @param response Scrape response
|
|
250
138
|
* @returns Metadata object
|
|
251
139
|
*/
|
|
252
|
-
extractMetadata(response: FirecrawlScrapeResponse): ScrapeMetadata {
|
|
140
|
+
extractMetadata(response: t.FirecrawlScrapeResponse): t.ScrapeMetadata {
|
|
253
141
|
if (!response.success || !response.data || !response.data.metadata) {
|
|
254
142
|
return {};
|
|
255
143
|
}
|
|
@@ -264,7 +152,7 @@ export class FirecrawlScraper {
|
|
|
264
152
|
* @returns Firecrawl scraper instance
|
|
265
153
|
*/
|
|
266
154
|
export const createFirecrawlScraper = (
|
|
267
|
-
config: FirecrawlScraperConfig = {}
|
|
155
|
+
config: t.FirecrawlScraperConfig = {}
|
|
268
156
|
): FirecrawlScraper => {
|
|
269
157
|
return new FirecrawlScraper(config);
|
|
270
158
|
};
|