Repository navigation
Support provider/capability shorthand in firecrawl scrape #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
77b0b74
Support Alexandria tool paths as scrape shorthand
developersdigest 90c0c81
chore: bump Alexandria CLI prerelease to beta.18
developersdigest 715bdac
fix: validate scrape targets consistently
developersdigest 0f51406
style: format terms command tests
developersdigest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { spawn } from 'node:child_process'; | ||
| import { createServer } from 'node:http'; | ||
| import { resolve } from 'node:path'; | ||
| import { expect, it } from 'vitest'; | ||
|
|
||
| it('sends shorthand and explicit calls identically, rejects bare names locally, and preserves tool errors', async () => { | ||
| const requests: { path: string; body: any }[] = []; | ||
| const server = createServer(async (request, response) => { | ||
| const chunks: Buffer[] = []; | ||
| for await (const chunk of request) chunks.push(Buffer.from(chunk)); | ||
| const body = JSON.parse(Buffer.concat(chunks).toString()); | ||
| requests.push({ path: request.url!, body }); | ||
| const call = body.alexandria[0]; | ||
| const result = | ||
| call.provider === 'benzing' | ||
| ? { | ||
| ...call, | ||
| error: { | ||
| code: 'unknown_provider', | ||
| message: 'Unknown provider "benzing".', | ||
| status: 404, | ||
| }, | ||
| } | ||
| : { ...call, data: { results: [] } }; | ||
| response.setHeader('content-type', 'application/json'); | ||
| response.end( | ||
| JSON.stringify({ | ||
| success: true, | ||
| data: { alexandria: [result], creditsCost: 0 }, | ||
| }) | ||
| ); | ||
| }); | ||
| await new Promise<void>((done) => server.listen(0, '127.0.0.1', done)); | ||
| const address = server.address() as { port: number }; | ||
| const run = (args: string[]) => | ||
| new Promise<{ code: number | null; stdout: string; stderr: string }>( | ||
| (done, reject) => { | ||
| const child = spawn( | ||
| process.execPath, | ||
| [resolve('dist/index.js'), 'scrape', ...args], | ||
| { | ||
| env: { | ||
| ...process.env, | ||
| FIRECRAWL_API_URL: `http://127.0.0.1:${address.port}`, | ||
| FIRECRAWL_API_KEY: 'fc-local-test', | ||
| FIRECRAWL_NO_UPDATE_CHECK: '1', | ||
| FIRECRAWL_NO_TELEMETRY: '1', | ||
| }, | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| } | ||
| ); | ||
| let stdout = '', | ||
| stderr = ''; | ||
| child.stdout.on('data', (chunk) => { | ||
| stdout += chunk; | ||
| }); | ||
| child.stderr.on('data', (chunk) => { | ||
| stderr += chunk; | ||
| }); | ||
| child.on('error', reject); | ||
| child.on('close', (code) => done({ code, stdout, stderr })); | ||
| } | ||
| ); | ||
| try { | ||
| const common = [ | ||
| '--options', | ||
| '{"pageSize":1}', | ||
| '--request-id', | ||
| 'local-test', | ||
| ]; | ||
| expect((await run(['benzinga/news/search', ...common])).code).toBe(0); | ||
| expect( | ||
| (await run(['--alexandria', 'benzinga/news/search', ...common])).code | ||
| ).toBe(0); | ||
| expect(requests).toHaveLength(2); | ||
| expect(requests[0]).toEqual(requests[1]); | ||
| expect(requests[0].body.url).toBeUndefined(); | ||
| expect(requests[1].body.url).toBeUndefined(); | ||
| expect(requests[0].path).toBe('/v2/scrape'); | ||
| expect(requests[0].body.alexandria).toEqual([ | ||
| { | ||
| provider: 'benzinga', | ||
| capability: 'news/search', | ||
| options: { pageSize: 1 }, | ||
| }, | ||
| ]); | ||
| const bare = await run(['amazon']); | ||
| expect(bare.code).toBe(1); | ||
| expect(bare.stderr).toContain('https://amazon.com (suggestion only)'); | ||
| expect(bare.stderr).toContain('firecrawl list'); | ||
| expect(requests).toHaveLength(2); | ||
| const bareUrl = await run(['--url', 'amazon']); | ||
| expect(bareUrl.code).toBe(1); | ||
| expect(bareUrl.stderr).toContain('https://amazon.com (suggestion only)'); | ||
| expect(requests).toHaveLength(2); | ||
| const typo = await run(['benzing/news/search', ...common]); | ||
| expect(typo.code).toBe(1); | ||
| expect(typo.stdout).toContain('unknown_provider'); | ||
| expect(requests).toHaveLength(3); | ||
| expect(requests[2].body.url).toBeUndefined(); | ||
| } finally { | ||
| await new Promise<void>((done, reject) => | ||
| server.close((error) => (error ? reject(error) : done())) | ||
| ); | ||
| } | ||
| }, 15_000); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { resolveScrapeTarget } from '../../utils/scrape-target'; | ||
|
|
||
| describe('scrape target routing', () => { | ||
| it('routes shorthand and explicit tools through identical Alexandria calls', () => { | ||
| const options = ['{"query":"test","k":4}']; | ||
| const shorthand = resolveScrapeTarget(['firecrawl-research-index/read'], { | ||
| options, | ||
| }); | ||
| expect(shorthand).toEqual( | ||
| resolveScrapeTarget([], { | ||
| alexandria: ['firecrawl-research-index/read'], | ||
| options, | ||
| }) | ||
| ); | ||
| expect(shorthand).toMatchObject({ | ||
| kind: 'alexandria', | ||
| calls: [ | ||
| { | ||
| provider: 'firecrawl-research-index', | ||
| capability: 'read', | ||
| options: { query: 'test', k: 4 }, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(resolveScrapeTarget(['benzing/news/serch'], {}).kind).toBe( | ||
| 'alexandria' | ||
| ); | ||
| }); | ||
|
|
||
| it.each([ | ||
| 'https://example.com/a', | ||
| 'http://internal/a', | ||
| 'example.com/a', | ||
| 'example.com:8080/a?x=1', | ||
| 'localhost:3000/a', | ||
| 'localhost', | ||
| '127.0.0.1:3000/a', | ||
| '[::1]:3000/a', | ||
| ])('keeps %s on URL scraping', (target) => { | ||
| expect(resolveScrapeTarget([target], {}).kind).toBe('url'); | ||
| expect(resolveScrapeTarget([], { url: target }).kind).toBe('url'); | ||
| }); | ||
|
|
||
| it.each(['provider_name/search', 'provider~name/search', '_provider/search'])( | ||
| 'accepts supported address characters in %s', | ||
| (address) => { | ||
| expect(resolveScrapeTarget([address], {})).toEqual( | ||
| resolveScrapeTarget([], { alexandria: [address] }) | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| it.each(['amazon', 'provider/search', 'https://', ''])( | ||
| 'rejects invalid --url %s locally', | ||
| (url) => { | ||
| expect(() => resolveScrapeTarget([], { url })).toThrow('firecrawl list'); | ||
| } | ||
| ); | ||
|
|
||
| it('preserves multiple URLs and positional output formats', () => { | ||
| expect( | ||
| resolveScrapeTarget( | ||
| ['example.com/a', 'example.org', 'Markdown, links'], | ||
| {} | ||
| ) | ||
| ).toMatchObject({ | ||
| kind: 'url', | ||
| urls: ['https://example.com/a', 'https://example.org'], | ||
| positionalFormats: ['Markdown, links'], | ||
| }); | ||
| }); | ||
|
|
||
| it('pairs multiple tool addresses with their options in order', () => { | ||
| expect( | ||
| resolveScrapeTarget( | ||
| ['benzinga/news/search', 'firecrawl-research-index/search'], | ||
| { options: ['{"pageSize":1}', '{"query":"attention"}'] } | ||
| ) | ||
| ).toMatchObject({ | ||
| calls: [ | ||
| { provider: 'benzinga', options: { pageSize: 1 } }, | ||
| { | ||
| provider: 'firecrawl-research-index', | ||
| options: { query: 'attention' }, | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| 'amazon', | ||
| 'amazon/', | ||
| '/news/search', | ||
| 'https://', | ||
| 'provider//search', | ||
| ])('rejects ambiguous or malformed %s locally', (value) => { | ||
| expect(() => resolveScrapeTarget([value], {})).toThrow('firecrawl list'); | ||
| }); | ||
|
|
||
| it('suggests both URL and Alexandria paths for a bare name', () => { | ||
| expect(() => resolveScrapeTarget(['amazon'], {})).toThrow( | ||
| 'https://amazon.com (suggestion only)' | ||
| ); | ||
| expect(() => resolveScrapeTarget(['amazon'], {})).toThrow('--alexandria'); | ||
| }); | ||
|
|
||
| it('refuses mixed modes and malformed options', () => { | ||
| expect(() => | ||
| resolveScrapeTarget(['example.com', 'benzinga/news/search'], {}) | ||
| ).toThrow('cannot be combined'); | ||
| expect(() => | ||
| resolveScrapeTarget(['benzinga/news/search'], { | ||
| alexandria: ['benzinga/news/search'], | ||
| }) | ||
| ).toThrow('not both'); | ||
| expect(() => | ||
| resolveScrapeTarget(['amazon'], { alexandria: ['benzinga/news/search'] }) | ||
| ).toThrow('firecrawl list'); | ||
| expect(() => | ||
| resolveScrapeTarget(['benzinga/news/search'], { domainTools: true }) | ||
| ).toThrow('cannot be combined'); | ||
| expect(() => | ||
| resolveScrapeTarget(['benzinga/news/search'], { options: ['[]'] }) | ||
| ).toThrow('JSON object'); | ||
| expect(() => | ||
| resolveScrapeTarget(['example.com'], { options: ['{}'] }) | ||
| ).toThrow('require a provider/capability'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.