|
| 1 | +import { TextSplitter } from 'langchain/text_splitter' |
| 2 | +import { Document, DocumentInterface } from '@langchain/core/documents' |
| 3 | +import { BaseDocumentLoader } from 'langchain/document_loaders/base' |
| 4 | +import { INode, INodeData, INodeParams, ICommonObject } from '../../../src/Interface' |
| 5 | +import { getCredentialData, getCredentialParam } from '../../../src/utils' |
| 6 | +import SpiderApp from './SpiderApp' |
| 7 | + |
| 8 | +interface SpiderLoaderParameters { |
| 9 | + url: string |
| 10 | + apiKey?: string |
| 11 | + mode?: 'crawl' | 'scrape' |
| 12 | + params?: Record<string, unknown> |
| 13 | +} |
| 14 | + |
| 15 | +class SpiderLoader extends BaseDocumentLoader { |
| 16 | + private apiKey: string |
| 17 | + private url: string |
| 18 | + private mode: 'crawl' | 'scrape' |
| 19 | + private params?: Record<string, unknown> |
| 20 | + |
| 21 | + constructor(loaderParams: SpiderLoaderParameters) { |
| 22 | + super() |
| 23 | + const { apiKey, url, mode = 'crawl', params } = loaderParams |
| 24 | + if (!apiKey) { |
| 25 | + throw new Error('Spider API key not set. You can set it as SPIDER_API_KEY in your .env file, or pass it to Spider.') |
| 26 | + } |
| 27 | + |
| 28 | + this.apiKey = apiKey |
| 29 | + this.url = url |
| 30 | + this.mode = mode |
| 31 | + this.params = params |
| 32 | + } |
| 33 | + |
| 34 | + public async load(): Promise<DocumentInterface[]> { |
| 35 | + const app = new SpiderApp({ apiKey: this.apiKey }) |
| 36 | + let spiderDocs: any[] |
| 37 | + |
| 38 | + if (this.mode === 'scrape') { |
| 39 | + const response = await app.scrapeUrl(this.url, this.params) |
| 40 | + if (!response.success) { |
| 41 | + throw new Error(`Spider: Failed to scrape URL. Error: ${response.error}`) |
| 42 | + } |
| 43 | + spiderDocs = [response.data] |
| 44 | + } else if (this.mode === 'crawl') { |
| 45 | + const response = await app.crawlUrl(this.url, this.params) |
| 46 | + if (!response.success) { |
| 47 | + throw new Error(`Spider: Failed to crawl URL. Error: ${response.error}`) |
| 48 | + } |
| 49 | + spiderDocs = response.data |
| 50 | + } else { |
| 51 | + throw new Error(`Unrecognized mode '${this.mode}'. Expected one of 'crawl', 'scrape'.`) |
| 52 | + } |
| 53 | + |
| 54 | + return spiderDocs.map( |
| 55 | + (doc) => |
| 56 | + new Document({ |
| 57 | + pageContent: doc.content || '', |
| 58 | + metadata: { source: doc.url } |
| 59 | + }) |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class Spider_DocumentLoaders implements INode { |
| 65 | + label: string |
| 66 | + name: string |
| 67 | + description: string |
| 68 | + type: string |
| 69 | + icon: string |
| 70 | + version: number |
| 71 | + category: string |
| 72 | + baseClasses: string[] |
| 73 | + inputs: INodeParams[] |
| 74 | + credential: INodeParams |
| 75 | + |
| 76 | + constructor() { |
| 77 | + this.label = 'Spider Document Loaders' |
| 78 | + this.name = 'spiderDocumentLoaders' |
| 79 | + this.version = 1.0 |
| 80 | + this.type = 'Document' |
| 81 | + this.icon = 'spider.svg' |
| 82 | + this.category = 'Document Loaders' |
| 83 | + this.description = 'Scrape & Crawl the web with Spider' |
| 84 | + this.baseClasses = [this.type] |
| 85 | + this.inputs = [ |
| 86 | + { |
| 87 | + label: 'Text Splitter', |
| 88 | + name: 'textSplitter', |
| 89 | + type: 'TextSplitter', |
| 90 | + optional: true |
| 91 | + }, |
| 92 | + { |
| 93 | + label: 'Mode', |
| 94 | + name: 'mode', |
| 95 | + type: 'options', |
| 96 | + options: [ |
| 97 | + { |
| 98 | + label: 'Scrape', |
| 99 | + name: 'scrape', |
| 100 | + description: 'Scrape a single page' |
| 101 | + }, |
| 102 | + { |
| 103 | + label: 'Crawl', |
| 104 | + name: 'crawl', |
| 105 | + description: 'Crawl a website and extract pages within the same domain' |
| 106 | + } |
| 107 | + ], |
| 108 | + default: 'scrape' |
| 109 | + }, |
| 110 | + { |
| 111 | + label: 'Web Page URL', |
| 112 | + name: 'url', |
| 113 | + type: 'string', |
| 114 | + placeholder: 'https://spider.cloud' |
| 115 | + }, |
| 116 | + { |
| 117 | + label: 'Additional Parameters', |
| 118 | + name: 'params', |
| 119 | + description: |
| 120 | + 'Find all the available parameters in the <a _target="blank" href="https://spider.cloud/docs/api">Spider API documentation</a>', |
| 121 | + additionalParams: true, |
| 122 | + placeholder: '{ "anti_bot": true }', |
| 123 | + type: 'json', |
| 124 | + optional: true |
| 125 | + } |
| 126 | + ] |
| 127 | + this.credential = { |
| 128 | + label: 'Credential', |
| 129 | + name: 'credential', |
| 130 | + type: 'credential', |
| 131 | + credentialNames: ['spiderApi'] |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 136 | + const textSplitter = nodeData.inputs?.textSplitter as TextSplitter |
| 137 | + const url = nodeData.inputs?.url as string |
| 138 | + const mode = nodeData.inputs?.mode as 'crawl' | 'scrape' |
| 139 | + let params = nodeData.inputs?.params || {} |
| 140 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 141 | + const spiderApiKey = getCredentialParam('spiderApiKey', credentialData, nodeData) |
| 142 | + |
| 143 | + if (typeof params === 'string') { |
| 144 | + try { |
| 145 | + params = JSON.parse(params) |
| 146 | + } catch (e) { |
| 147 | + throw new Error('Invalid JSON string provided for params') |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Ensure return_format is set to markdown |
| 152 | + params.return_format = 'markdown' |
| 153 | + |
| 154 | + const input: SpiderLoaderParameters = { |
| 155 | + url, |
| 156 | + mode: mode as 'crawl' | 'scrape', |
| 157 | + apiKey: spiderApiKey, |
| 158 | + params: params as Record<string, unknown> |
| 159 | + } |
| 160 | + |
| 161 | + const loader = new SpiderLoader(input) |
| 162 | + |
| 163 | + let docs = [] |
| 164 | + |
| 165 | + if (textSplitter) { |
| 166 | + docs = await loader.loadAndSplit(textSplitter) |
| 167 | + } else { |
| 168 | + docs = await loader.load() |
| 169 | + } |
| 170 | + |
| 171 | + return docs |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +module.exports = { nodeClass: Spider_DocumentLoaders } |
0 commit comments