@deepagents/text2sql 7.12.0 → 7.13.0
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/README.md +57 -1
- package/dist/bin/sql.js +10 -13
- package/dist/index.js +31 -15
- package/dist/index.js.map +2 -2
- package/dist/lib/adapters/adapter.d.ts +17 -0
- package/dist/lib/adapters/adapter.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/bigquery.d.ts +2 -2
- package/dist/lib/adapters/bigquery/bigquery.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/bigquery.sql-policy.d.ts +3 -8
- package/dist/lib/adapters/bigquery/bigquery.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/index.d.ts +1 -1
- package/dist/lib/adapters/bigquery/index.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/index.js +230 -207
- package/dist/lib/adapters/bigquery/index.js.map +4 -4
- package/dist/lib/adapters/clickhouse/clickhouse.d.ts +2 -2
- package/dist/lib/adapters/clickhouse/clickhouse.d.ts.map +1 -1
- package/dist/lib/adapters/clickhouse/clickhouse.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/clickhouse/index.js +32 -8
- package/dist/lib/adapters/clickhouse/index.js.map +3 -3
- package/dist/lib/adapters/duckdb/duckdb.d.ts +2 -2
- package/dist/lib/adapters/duckdb/duckdb.d.ts.map +1 -1
- package/dist/lib/adapters/duckdb/duckdb.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/duckdb/index.js +32 -8
- package/dist/lib/adapters/duckdb/index.js.map +3 -3
- package/dist/lib/adapters/mysql/index.d.ts +1 -1
- package/dist/lib/adapters/mysql/index.d.ts.map +1 -1
- package/dist/lib/adapters/mysql/index.js +256 -221
- package/dist/lib/adapters/mysql/index.js.map +4 -4
- package/dist/lib/adapters/mysql/mysql.d.ts +2 -2
- package/dist/lib/adapters/mysql/mysql.d.ts.map +1 -1
- package/dist/lib/adapters/mysql/mysql.sql-policy.d.ts +3 -10
- package/dist/lib/adapters/mysql/mysql.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/policy/denied-calls.d.ts +17 -0
- package/dist/lib/adapters/policy/denied-calls.d.ts.map +1 -0
- package/dist/lib/adapters/policy/facts.d.ts +24 -0
- package/dist/lib/adapters/policy/facts.d.ts.map +1 -0
- package/dist/lib/adapters/policy/forbid.d.ts +9 -0
- package/dist/lib/adapters/policy/forbid.d.ts.map +1 -0
- package/dist/lib/adapters/policy/in-scope.d.ts +25 -0
- package/dist/lib/adapters/policy/in-scope.d.ts.map +1 -0
- package/dist/lib/adapters/policy/index.d.ts +8 -0
- package/dist/lib/adapters/policy/index.d.ts.map +1 -0
- package/dist/lib/adapters/policy/index.js +368 -0
- package/dist/lib/adapters/policy/index.js.map +7 -0
- package/dist/lib/adapters/policy/one-select.d.ts +7 -0
- package/dist/lib/adapters/policy/one-select.d.ts.map +1 -0
- package/dist/lib/adapters/policy/read-only.d.ts +4 -0
- package/dist/lib/adapters/policy/read-only.d.ts.map +1 -0
- package/dist/lib/adapters/policy/source-policy.d.ts +45 -0
- package/dist/lib/adapters/policy/source-policy.d.ts.map +1 -0
- package/dist/lib/adapters/policy/sql-text.d.ts +43 -0
- package/dist/lib/adapters/policy/sql-text.d.ts.map +1 -0
- package/dist/lib/adapters/postgres/index.d.ts +1 -1
- package/dist/lib/adapters/postgres/index.d.ts.map +1 -1
- package/dist/lib/adapters/postgres/index.js +318 -223
- package/dist/lib/adapters/postgres/index.js.map +4 -4
- package/dist/lib/adapters/postgres/postgres.d.ts +2 -2
- package/dist/lib/adapters/postgres/postgres.d.ts.map +1 -1
- package/dist/lib/adapters/postgres/postgres.sql-policy.d.ts +3 -9
- package/dist/lib/adapters/postgres/postgres.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/posthog/index.js +24 -5
- package/dist/lib/adapters/posthog/index.js.map +2 -2
- package/dist/lib/adapters/posthog/posthog.d.ts +2 -2
- package/dist/lib/adapters/posthog/posthog.d.ts.map +1 -1
- package/dist/lib/adapters/spreadsheet/index.js +237 -207
- package/dist/lib/adapters/spreadsheet/index.js.map +4 -4
- package/dist/lib/adapters/spreadsheet/spreadsheet.d.ts +2 -2
- package/dist/lib/adapters/spreadsheet/spreadsheet.d.ts.map +1 -1
- package/dist/lib/adapters/sqlite/index.d.ts +1 -1
- package/dist/lib/adapters/sqlite/index.d.ts.map +1 -1
- package/dist/lib/adapters/sqlite/index.js +236 -207
- package/dist/lib/adapters/sqlite/index.js.map +4 -4
- package/dist/lib/adapters/sqlite/sqlite.d.ts +2 -2
- package/dist/lib/adapters/sqlite/sqlite.d.ts.map +1 -1
- package/dist/lib/adapters/sqlite/sqlite.sql-policy.d.ts +3 -7
- package/dist/lib/adapters/sqlite/sqlite.sql-policy.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/index.d.ts +1 -1
- package/dist/lib/adapters/sqlserver/index.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/index.js +244 -214
- package/dist/lib/adapters/sqlserver/index.js.map +4 -4
- package/dist/lib/adapters/sqlserver/sqlserver.d.ts +2 -2
- package/dist/lib/adapters/sqlserver/sqlserver.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/sqlserver.sql-policy.d.ts +6 -10
- package/dist/lib/adapters/sqlserver/sqlserver.sql-policy.d.ts.map +1 -1
- package/dist/lib/sql-scope-error.d.ts +1 -0
- package/dist/lib/sql-scope-error.d.ts.map +1 -1
- package/dist/lib/sql.d.ts.map +1 -1
- package/dist/lib/synthesis/index.js.map +1 -1
- package/package.json +10 -4
- package/dist/lib/adapters/parser-sql-policy.d.ts +0 -23
- package/dist/lib/adapters/parser-sql-policy.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -233,7 +233,12 @@ provided.
|
|
|
233
233
|
|
|
234
234
|
Install the package inside the sandbox and set `TEXT2SQL_ADAPTERS` to a module
|
|
235
235
|
whose default export is `Record<string, Adapter>`. Missing `sql` means the
|
|
236
|
-
sandbox was not prepared correctly.
|
|
236
|
+
sandbox was not prepared correctly. The CLI also loads a plain object with the
|
|
237
|
+
methods its commands call: `format(sql)`, `validate(sql)` for `sql validate`,
|
|
238
|
+
and `run(sql)` for `sql run`. `sql run` gives the formatted SQL to `run` and
|
|
239
|
+
never calls `validate`, so a plain object's `run` checks its own SQL and gives
|
|
240
|
+
`{ result: rows }` or `{ error: message }`. `sql index` also calls
|
|
241
|
+
`introspect()`.
|
|
237
242
|
|
|
238
243
|
`sql index` output details:
|
|
239
244
|
|
|
@@ -337,6 +342,57 @@ after validation, which is useful for application-owned filters added outside
|
|
|
337
342
|
the model loop. Shell-escaped dollar-prefixed PostHog properties such as
|
|
338
343
|
`properties['\$device_type']` are decoded before validation and execution.
|
|
339
344
|
|
|
345
|
+
## SQL policy
|
|
346
|
+
|
|
347
|
+
Each adapter checks SQL with a policy before its validator or executor runs.
|
|
348
|
+
`Text2Sql.run` checks once, then validates and executes. A policy is a
|
|
349
|
+
`SqlPolicyAnalyzer`, and every adapter takes one as its `policy` option in
|
|
350
|
+
place of its default.
|
|
351
|
+
|
|
352
|
+
The building blocks are in `@deepagents/text2sql/policy`. A fact source reads
|
|
353
|
+
the SQL into facts, and rules check the facts in order; the first violation
|
|
354
|
+
wins, and a rule that is not reached reads no fact. `SqlText` reads the SQL
|
|
355
|
+
text with node-sql-parser and gives `Statements`, `Relations`, `Calls` and
|
|
356
|
+
`Constructs`. A rule names the facts it reads, so a rule that the source cannot
|
|
357
|
+
serve does not compile.
|
|
358
|
+
|
|
359
|
+
| Rule | Reads | Refuses |
|
|
360
|
+
| ------------- | ------------ | ---------------------------------------------------------------- |
|
|
361
|
+
| `OneSelect` | `Statements` | anything but one SELECT, `SELECT INTO` included |
|
|
362
|
+
| `DeniedCalls` | `Calls` | calls by name, by name prefix or by qualified name |
|
|
363
|
+
| `Forbid` | `Constructs` | table hints, linked-server relations, locking reads, assignments |
|
|
364
|
+
| `InScope` | `Relations` | relations outside the grounded tables and views |
|
|
365
|
+
|
|
366
|
+
`InScope(new GroundedNames())` matches names without case, and it matches an
|
|
367
|
+
unqualified name by the last part of an allowed entity. Each dialect exports
|
|
368
|
+
its default policy: `policy()` of `sqlite`, `postgres`, `mysql` and `bigquery`,
|
|
369
|
+
and `textPolicy()` of `sqlserver`. `FirstViolation` runs policies in order,
|
|
370
|
+
and the first violation wins:
|
|
371
|
+
|
|
372
|
+
```ts
|
|
373
|
+
import {
|
|
374
|
+
DeniedCalls,
|
|
375
|
+
FirstViolation,
|
|
376
|
+
SourcePolicy,
|
|
377
|
+
SqlText,
|
|
378
|
+
} from '@deepagents/text2sql/policy';
|
|
379
|
+
import * as sqlserver from '@deepagents/text2sql/sqlserver';
|
|
380
|
+
|
|
381
|
+
const adapter = new sqlserver.SqlServer({
|
|
382
|
+
execute,
|
|
383
|
+
grounding: [sqlserver.tables()],
|
|
384
|
+
policy: new FirstViolation(
|
|
385
|
+
sqlserver.textPolicy(),
|
|
386
|
+
new SourcePolicy(new SqlText({ dialects: ['transactsql'] }), [
|
|
387
|
+
new DeniedCalls({ names: ['HASHBYTES'] }),
|
|
388
|
+
]),
|
|
389
|
+
),
|
|
390
|
+
});
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
A source that cannot read the SQL throws `UnreadableSql`, and the policy gives
|
|
394
|
+
`SQL_SCOPE_PARSE_ERROR`. Any other error of a source rejects the check.
|
|
395
|
+
|
|
340
396
|
## Fragments
|
|
341
397
|
|
|
342
398
|
Inject domain knowledge by setting fragments on the `ContextEngine` you build
|
package/dist/bin/sql.js
CHANGED
|
@@ -460,27 +460,24 @@ var Text2Sql = class {
|
|
|
460
460
|
}
|
|
461
461
|
async validate(name, sql) {
|
|
462
462
|
const adapter = this.#requireAdapter(name);
|
|
463
|
-
const
|
|
464
|
-
|
|
463
|
+
const formatted = adapter.format(sql);
|
|
464
|
+
const error = await adapter.validate(formatted);
|
|
465
|
+
if (error) throw new Text2SqlValidationError(error);
|
|
466
|
+
return formatted;
|
|
465
467
|
}
|
|
466
468
|
async run(name, sql) {
|
|
467
469
|
const adapter = this.#requireAdapter(name);
|
|
468
|
-
const
|
|
469
|
-
|
|
470
|
+
const outcome = await adapter.run(adapter.format(sql));
|
|
471
|
+
if ("error" in outcome) throw new Text2SqlValidationError(outcome.error);
|
|
472
|
+
const { result } = outcome;
|
|
470
473
|
if (!Array.isArray(result)) {
|
|
471
|
-
throw new Error("adapter.
|
|
474
|
+
throw new Error("adapter.run must give an array of rows");
|
|
472
475
|
}
|
|
473
476
|
const rows = result;
|
|
474
477
|
const [first] = rows;
|
|
475
478
|
const columns = typeof first === "object" && first !== null ? Object.keys(first) : [];
|
|
476
479
|
return { rows, columns };
|
|
477
480
|
}
|
|
478
|
-
async #validateWith(adapter, sql) {
|
|
479
|
-
const formatted = adapter.format(sql);
|
|
480
|
-
const error = await adapter.validate(formatted);
|
|
481
|
-
if (error) throw new Text2SqlValidationError(error);
|
|
482
|
-
return formatted;
|
|
483
|
-
}
|
|
484
481
|
async index(options = {}) {
|
|
485
482
|
return this.#indexer.index({
|
|
486
483
|
adapterNames: options.names,
|
|
@@ -822,7 +819,7 @@ async function loadAdapters() {
|
|
|
822
819
|
for (const [name, value] of entries) {
|
|
823
820
|
if (!isAdapterShape(value)) {
|
|
824
821
|
throw new Error(
|
|
825
|
-
`TEXT2SQL_ADAPTERS=${target}: adapter "${name}" is missing one of the required methods (format, validate,
|
|
822
|
+
`TEXT2SQL_ADAPTERS=${target}: adapter "${name}" is missing one of the required methods (format, validate, run).`
|
|
826
823
|
);
|
|
827
824
|
}
|
|
828
825
|
adapters[name] = value;
|
|
@@ -837,7 +834,7 @@ async function loadAdapters() {
|
|
|
837
834
|
return adapters;
|
|
838
835
|
}
|
|
839
836
|
function isAdapterShape(value) {
|
|
840
|
-
return typeof value === "object" && value !== null && "format" in value && typeof value.format === "function" && "validate" in value && typeof value.validate === "function" && "
|
|
837
|
+
return typeof value === "object" && value !== null && "format" in value && typeof value.format === "function" && "validate" in value && typeof value.validate === "function" && "run" in value && typeof value.run === "function";
|
|
841
838
|
}
|
|
842
839
|
function describe(value) {
|
|
843
840
|
if (value === null) return "null";
|
package/dist/index.js
CHANGED
|
@@ -438,16 +438,35 @@ var Adapter = class {
|
|
|
438
438
|
resolveAllowedEntities: () => this.#resolveScope()
|
|
439
439
|
});
|
|
440
440
|
}
|
|
441
|
-
|
|
441
|
+
/** The decoded SQL when the policy allows it, or the policy's message. */
|
|
442
|
+
async #admit(sql) {
|
|
442
443
|
const decoded = this.#decodeShellEscapes(sql);
|
|
443
444
|
const violation = await this.#analyzePolicy(decoded);
|
|
444
|
-
if (violation?.kind === "read-only") return violation.message;
|
|
445
|
-
if (violation?.kind === "scope")
|
|
446
|
-
|
|
445
|
+
if (violation?.kind === "read-only") return { error: violation.message };
|
|
446
|
+
if (violation?.kind === "scope") {
|
|
447
|
+
return { error: JSON.stringify(violation.payload) };
|
|
448
|
+
}
|
|
449
|
+
return { sql: decoded };
|
|
450
|
+
}
|
|
451
|
+
async validate(sql) {
|
|
452
|
+
const admitted = await this.#admit(sql);
|
|
453
|
+
if ("error" in admitted) return admitted.error;
|
|
454
|
+
return this.validateImpl(admitted.sql);
|
|
447
455
|
}
|
|
448
456
|
async execute(sql) {
|
|
449
457
|
return this.executeImpl(await this.enforceExecutionPolicy(sql));
|
|
450
458
|
}
|
|
459
|
+
/**
|
|
460
|
+
* Validates and then executes SQL, checking it against the policy once.
|
|
461
|
+
* Gives the policy's or the validator's error in place of a result.
|
|
462
|
+
*/
|
|
463
|
+
async run(sql) {
|
|
464
|
+
const admitted = await this.#admit(sql);
|
|
465
|
+
if ("error" in admitted) return admitted;
|
|
466
|
+
const error = await this.validateImpl(admitted.sql);
|
|
467
|
+
if (error) return { error };
|
|
468
|
+
return { result: await this.executeImpl(admitted.sql) };
|
|
469
|
+
}
|
|
451
470
|
async enforceExecutionPolicy(sql) {
|
|
452
471
|
const decoded = this.#decodeShellEscapes(sql);
|
|
453
472
|
const violation = await this.#analyzePolicy(decoded);
|
|
@@ -5616,27 +5635,24 @@ var Text2Sql = class {
|
|
|
5616
5635
|
}
|
|
5617
5636
|
async validate(name, sql) {
|
|
5618
5637
|
const adapter = this.#requireAdapter(name);
|
|
5619
|
-
const
|
|
5620
|
-
|
|
5638
|
+
const formatted = adapter.format(sql);
|
|
5639
|
+
const error = await adapter.validate(formatted);
|
|
5640
|
+
if (error) throw new Text2SqlValidationError(error);
|
|
5641
|
+
return formatted;
|
|
5621
5642
|
}
|
|
5622
5643
|
async run(name, sql) {
|
|
5623
5644
|
const adapter = this.#requireAdapter(name);
|
|
5624
|
-
const
|
|
5625
|
-
|
|
5645
|
+
const outcome = await adapter.run(adapter.format(sql));
|
|
5646
|
+
if ("error" in outcome) throw new Text2SqlValidationError(outcome.error);
|
|
5647
|
+
const { result } = outcome;
|
|
5626
5648
|
if (!Array.isArray(result)) {
|
|
5627
|
-
throw new Error("adapter.
|
|
5649
|
+
throw new Error("adapter.run must give an array of rows");
|
|
5628
5650
|
}
|
|
5629
5651
|
const rows = result;
|
|
5630
5652
|
const [first] = rows;
|
|
5631
5653
|
const columns = typeof first === "object" && first !== null ? Object.keys(first) : [];
|
|
5632
5654
|
return { rows, columns };
|
|
5633
5655
|
}
|
|
5634
|
-
async #validateWith(adapter, sql) {
|
|
5635
|
-
const formatted = adapter.format(sql);
|
|
5636
|
-
const error = await adapter.validate(formatted);
|
|
5637
|
-
if (error) throw new Text2SqlValidationError(error);
|
|
5638
|
-
return formatted;
|
|
5639
|
-
}
|
|
5640
5656
|
async index(options = {}) {
|
|
5641
5657
|
return this.#indexer.index({
|
|
5642
5658
|
adapterNames: options.names,
|