-
Notifications
You must be signed in to change notification settings - Fork 109
Experimental Alexandria syntax #283
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
+144
−0
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { afterEach, expect, it, vi } from 'vitest'; | ||
| import { Readable } from 'node:stream'; | ||
| import { mkdtemp, writeFile, rm } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { createSqlCommand, readSqlInput } from '../../commands/sql'; | ||
| import { handleAlexandria } from '../../commands/alexandria'; | ||
|
|
||
| vi.mock('../../commands/alexandria', () => ({ handleAlexandria: vi.fn() })); | ||
| afterEach(() => vi.clearAllMocks()); | ||
|
|
||
| it.each([false, true])('forwards raw SQL with execute=%s', async (execute) => { | ||
| const query = 'SELECT * FROM "example/tool" WHERE name = \'Nike\' LIMIT 1'; | ||
| await createSqlCommand().parseAsync( | ||
| [ | ||
| query, | ||
| ...(execute ? ['--execute'] : []), | ||
| '--pretty', | ||
| '--request-id', | ||
| 'request', | ||
| ], | ||
| { from: 'user' } | ||
| ); | ||
| expect(handleAlexandria).toHaveBeenCalledWith( | ||
| [{ provider: 'firecrawl', capability: 'sql', options: { query, execute } }], | ||
| expect.objectContaining({ pretty: true, requestId: 'request' }) | ||
| ); | ||
| }); | ||
|
|
||
| it('reads multiline stdin without changing quotes', async () => { | ||
| const query = | ||
| 'WITH a AS (\n SELECT * FROM "a/b" LIMIT 1\n) SELECT * FROM a LIMIT 1'; | ||
| expect(await readSqlInput(undefined, undefined, Readable.from([query]))).toBe( | ||
| query | ||
| ); | ||
| }); | ||
|
|
||
| it('reads a file and forwards it to the existing handler', async () => { | ||
| const dir = await mkdtemp(join(tmpdir(), 'sql-cli-')); | ||
| try { | ||
| const path = join(dir, 'query.sql'); | ||
| await writeFile(path, 'SHOW TABLES LIMIT 1'); | ||
| await createSqlCommand().parseAsync(['--file', path], { from: 'user' }); | ||
| expect(handleAlexandria).toHaveBeenCalledWith( | ||
| [ | ||
| { | ||
| provider: 'firecrawl', | ||
| capability: 'sql', | ||
| options: { query: 'SHOW TABLES LIMIT 1', execute: false }, | ||
| }, | ||
| ], | ||
| expect.anything() | ||
| ); | ||
| } finally { | ||
| await rm(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects ambiguous sources without dispatch', async () => { | ||
| await expect( | ||
| createSqlCommand().parseAsync(['HELP', '--file', 'query.sql'], { | ||
| from: 'user', | ||
| }) | ||
| ).rejects.toThrow('either'); | ||
| expect(handleAlexandria).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects missing, empty and oversized input', async () => { | ||
| await expect( | ||
| readSqlInput( | ||
| undefined, | ||
| undefined, | ||
| Object.assign(Readable.from([]), { isTTY: true }) | ||
| ) | ||
| ).rejects.toThrow('Provide'); | ||
| await expect( | ||
| readSqlInput(undefined, undefined, Readable.from([' '])) | ||
| ).rejects.toThrow('empty'); | ||
| await expect(readSqlInput('x'.repeat(16001), undefined)).rejects.toThrow( | ||
| '16,000' | ||
| ); | ||
| await expect( | ||
| readSqlInput(undefined, undefined, Readable.from(['x'.repeat(16001)])) | ||
| ).rejects.toThrow('16,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,57 @@ | ||
| import { Command } from 'commander'; | ||
| import { readFile } from 'node:fs/promises'; | ||
| import { handleAlexandria } from './alexandria'; | ||
|
|
||
| export async function readSqlInput( | ||
| query: string | undefined, | ||
| file: string | undefined, | ||
| stdin: AsyncIterable<string | Buffer> & { isTTY?: boolean } = process.stdin | ||
| ): Promise<string> { | ||
| if (query !== undefined && file !== undefined) | ||
| throw new Error('Use either a query or --file.'); | ||
| if (file !== undefined) query = await readFile(file, 'utf8'); | ||
| if (query === undefined) { | ||
| if (stdin.isTTY) throw new Error('Provide a query, --file, or stdin.'); | ||
| query = ''; | ||
| for await (const chunk of stdin) { | ||
| query += chunk.toString(); | ||
| if (query.length > 16000) | ||
| throw new Error('SQL must be at most 16,000 characters.'); | ||
| } | ||
| } | ||
| if (!query.trim()) throw new Error('SQL must not be empty.'); | ||
| if (query.length > 16000) | ||
| throw new Error('SQL must be at most 16,000 characters.'); | ||
| return query; | ||
| } | ||
|
|
||
| export function createSqlCommand(): Command { | ||
| return new Command('sql') | ||
| .description('Experimental Alexandria syntax') | ||
| .argument('[query]', 'SQL statement') | ||
| .option('-f, --file <path>', 'Read SQL from a file') | ||
| .option( | ||
| '--execute', | ||
| 'Execute paid provider calls; defaults to preview', | ||
| false | ||
| ) | ||
| .option('-k, --api-key <key>', 'Firecrawl API key') | ||
| .option('--api-url <url>', 'Firecrawl API URL') | ||
| .option('--request-id <id>', 'Request ID') | ||
| .option('-o, --output <path>', 'Output file') | ||
| .option('--json', 'Output JSON') | ||
| .option('--pretty', 'Format JSON') | ||
| .action(async (query, options) => { | ||
| const statement = await readSqlInput(query, options.file); | ||
| await handleAlexandria( | ||
| [ | ||
| { | ||
| provider: 'firecrawl', | ||
| capability: 'sql', | ||
| options: { query: statement, execute: options.execute }, | ||
| }, | ||
| ], | ||
| options | ||
| ); | ||
| }); | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
--fileloads the entire file before enforcing the 16,000-character limit, so a large path can exhaust memory or hang instead of being rejected. Read at most 16,001 characters from the file and reject oversized input before accumulating it.Prompt for AI agents