feat: Add limit parameter to Spider tool (#2762)
* feat: Add limit parameter to Spider tool * fix pnpm lint
This commit is contained in:
parent
656f6cad81
commit
cacbfa8162
|
|
@ -9,6 +9,7 @@ interface SpiderLoaderParameters {
|
||||||
url: string
|
url: string
|
||||||
apiKey?: string
|
apiKey?: string
|
||||||
mode?: 'crawl' | 'scrape'
|
mode?: 'crawl' | 'scrape'
|
||||||
|
limit?: number
|
||||||
params?: Record<string, unknown>
|
params?: Record<string, unknown>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -16,11 +17,12 @@ class SpiderLoader extends BaseDocumentLoader {
|
||||||
private apiKey: string
|
private apiKey: string
|
||||||
private url: string
|
private url: string
|
||||||
private mode: 'crawl' | 'scrape'
|
private mode: 'crawl' | 'scrape'
|
||||||
|
private limit?: number
|
||||||
private params?: Record<string, unknown>
|
private params?: Record<string, unknown>
|
||||||
|
|
||||||
constructor(loaderParams: SpiderLoaderParameters) {
|
constructor(loaderParams: SpiderLoaderParameters) {
|
||||||
super()
|
super()
|
||||||
const { apiKey, url, mode = 'crawl', params } = loaderParams
|
const { apiKey, url, mode = 'crawl', limit, params } = loaderParams
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
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.')
|
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.')
|
||||||
}
|
}
|
||||||
|
|
@ -28,6 +30,7 @@ class SpiderLoader extends BaseDocumentLoader {
|
||||||
this.apiKey = apiKey
|
this.apiKey = apiKey
|
||||||
this.url = url
|
this.url = url
|
||||||
this.mode = mode
|
this.mode = mode
|
||||||
|
this.limit = Number(limit)
|
||||||
this.params = params
|
this.params = params
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,6 +45,9 @@ class SpiderLoader extends BaseDocumentLoader {
|
||||||
}
|
}
|
||||||
spiderDocs = [response.data]
|
spiderDocs = [response.data]
|
||||||
} else if (this.mode === 'crawl') {
|
} else if (this.mode === 'crawl') {
|
||||||
|
if (this.params) {
|
||||||
|
this.params.limit = this.limit
|
||||||
|
}
|
||||||
const response = await app.crawlUrl(this.url, this.params)
|
const response = await app.crawlUrl(this.url, this.params)
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new Error(`Spider: Failed to crawl URL. Error: ${response.error}`)
|
throw new Error(`Spider: Failed to crawl URL. Error: ${response.error}`)
|
||||||
|
|
@ -113,6 +119,12 @@ class Spider_DocumentLoaders implements INode {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
placeholder: 'https://spider.cloud'
|
placeholder: 'https://spider.cloud'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: 'Limit',
|
||||||
|
name: 'limit',
|
||||||
|
type: 'number',
|
||||||
|
default: 25
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: 'Additional Parameters',
|
label: 'Additional Parameters',
|
||||||
name: 'params',
|
name: 'params',
|
||||||
|
|
@ -136,6 +148,7 @@ class Spider_DocumentLoaders implements INode {
|
||||||
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter
|
const textSplitter = nodeData.inputs?.textSplitter as TextSplitter
|
||||||
const url = nodeData.inputs?.url as string
|
const url = nodeData.inputs?.url as string
|
||||||
const mode = nodeData.inputs?.mode as 'crawl' | 'scrape'
|
const mode = nodeData.inputs?.mode as 'crawl' | 'scrape'
|
||||||
|
const limit = nodeData.inputs?.limit as number
|
||||||
let params = nodeData.inputs?.params || {}
|
let params = nodeData.inputs?.params || {}
|
||||||
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
||||||
const spiderApiKey = getCredentialParam('spiderApiKey', credentialData, nodeData)
|
const spiderApiKey = getCredentialParam('spiderApiKey', credentialData, nodeData)
|
||||||
|
|
@ -155,6 +168,7 @@ class Spider_DocumentLoaders implements INode {
|
||||||
url,
|
url,
|
||||||
mode: mode as 'crawl' | 'scrape',
|
mode: mode as 'crawl' | 'scrape',
|
||||||
apiKey: spiderApiKey,
|
apiKey: spiderApiKey,
|
||||||
|
limit: limit as number,
|
||||||
params: params as Record<string, unknown>
|
params: params as Record<string, unknown>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue