@deepagents/text2sql 0.37.1 → 0.38.1
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 +10 -1
- package/dist/index.js +165 -44
- package/dist/index.js.map +2 -2
- package/dist/lib/adapters/adapter.d.ts +4 -4
- package/dist/lib/adapters/adapter.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/constraint.bigquery.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/index.js +160 -31
- package/dist/lib/adapters/bigquery/index.js.map +3 -3
- package/dist/lib/adapters/bigquery/indexes.bigquery.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/row-count.bigquery.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/abstract.grounding.d.ts +3 -1
- package/dist/lib/adapters/groundings/abstract.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/column-stats.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/column-values.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/constraint.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/context.d.ts +6 -2
- package/dist/lib/adapters/groundings/context.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/index.js +96 -20
- package/dist/lib/adapters/groundings/index.js.map +2 -2
- package/dist/lib/adapters/groundings/indexes.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/row-count.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/table.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/groundings/view.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/mysql/index.js +145 -35
- package/dist/lib/adapters/mysql/index.js.map +3 -3
- package/dist/lib/adapters/postgres/index.js +151 -35
- package/dist/lib/adapters/postgres/index.js.map +3 -3
- package/dist/lib/adapters/postgres/postgres.d.ts.map +1 -1
- package/dist/lib/adapters/spreadsheet/index.js +109 -27
- package/dist/lib/adapters/spreadsheet/index.js.map +3 -3
- package/dist/lib/adapters/sqlite/index.js +145 -35
- package/dist/lib/adapters/sqlite/index.js.map +3 -3
- package/dist/lib/adapters/sqlserver/index.js +151 -35
- package/dist/lib/adapters/sqlserver/index.js.map +3 -3
- package/dist/lib/adapters/sqlserver/sqlserver.d.ts.map +1 -1
- package/dist/lib/sql.d.ts +22 -5
- package/dist/lib/sql.d.ts.map +1 -1
- package/package.json +6 -5
|
@@ -148,13 +148,14 @@ function relationship(input) {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
// packages/text2sql/src/lib/adapters/groundings/context.ts
|
|
151
|
-
function createGroundingContext() {
|
|
151
|
+
function createGroundingContext(options = {}) {
|
|
152
152
|
return {
|
|
153
153
|
tables: [],
|
|
154
154
|
views: [],
|
|
155
155
|
relationships: [],
|
|
156
156
|
info: void 0,
|
|
157
|
-
cache: /* @__PURE__ */ new Map()
|
|
157
|
+
cache: /* @__PURE__ */ new Map(),
|
|
158
|
+
onProgress: options.onProgress
|
|
158
159
|
};
|
|
159
160
|
}
|
|
160
161
|
|
|
@@ -300,7 +301,24 @@ var Adapter = class {
|
|
|
300
301
|
async introspect(ctx = createGroundingContext()) {
|
|
301
302
|
for (const fn of this.grounding) {
|
|
302
303
|
const grounding = fn(this);
|
|
303
|
-
|
|
304
|
+
if (grounding.phase) {
|
|
305
|
+
ctx.onProgress?.({
|
|
306
|
+
type: "phase:start",
|
|
307
|
+
phase: grounding.phase,
|
|
308
|
+
message: `Starting ${grounding.name} introspection...`
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
await grounding.execute(ctx);
|
|
313
|
+
} finally {
|
|
314
|
+
if (grounding.phase) {
|
|
315
|
+
ctx.onProgress?.({
|
|
316
|
+
type: "phase:end",
|
|
317
|
+
phase: grounding.phase,
|
|
318
|
+
message: `Finished ${grounding.name} introspection.`
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
304
322
|
}
|
|
305
323
|
return this.#toSchemaFragments(ctx);
|
|
306
324
|
}
|
|
@@ -506,26 +524,33 @@ var Adapter = class {
|
|
|
506
524
|
async #checkScope(sql, allowedEntities) {
|
|
507
525
|
const dialect = this.formatterLanguage;
|
|
508
526
|
const scopeDialects = {
|
|
509
|
-
sqlite: "mysql",
|
|
510
|
-
postgresql: "postgresql",
|
|
511
|
-
bigquery: "bigquery",
|
|
512
|
-
transactsql: "transactsql",
|
|
513
|
-
mysql: "mysql"
|
|
527
|
+
sqlite: ["sqlite", "mysql"],
|
|
528
|
+
postgresql: ["postgresql"],
|
|
529
|
+
bigquery: ["bigquery"],
|
|
530
|
+
transactsql: ["transactsql"],
|
|
531
|
+
mysql: ["mysql"]
|
|
514
532
|
};
|
|
515
|
-
const
|
|
516
|
-
if (!
|
|
533
|
+
const candidates = scopeDialects[dialect];
|
|
534
|
+
if (!candidates) {
|
|
517
535
|
throw new TypeError(
|
|
518
536
|
`No scope dialect mapping for formatter language "${dialect}". Add it to the scopeDialects map in Adapter.#checkScope.`
|
|
519
537
|
);
|
|
520
538
|
}
|
|
521
|
-
let references;
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
539
|
+
let references = null;
|
|
540
|
+
let lastError;
|
|
541
|
+
for (const candidate of candidates) {
|
|
542
|
+
try {
|
|
543
|
+
references = extractBaseEntityReferences(sql, candidate);
|
|
544
|
+
break;
|
|
545
|
+
} catch (error) {
|
|
546
|
+
lastError = error;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (references === null) {
|
|
525
550
|
return buildScopeParseErrorPayload(
|
|
526
551
|
sql,
|
|
527
552
|
dialect,
|
|
528
|
-
|
|
553
|
+
lastError
|
|
529
554
|
);
|
|
530
555
|
}
|
|
531
556
|
if (references.length === 0) return null;
|
|
@@ -648,15 +673,17 @@ var AbstractGrounding = class {
|
|
|
648
673
|
* Grounding identifier for debugging/logging.
|
|
649
674
|
*/
|
|
650
675
|
name;
|
|
651
|
-
|
|
676
|
+
phase;
|
|
677
|
+
constructor(name, phase) {
|
|
652
678
|
this.name = name;
|
|
679
|
+
this.phase = phase;
|
|
653
680
|
}
|
|
654
681
|
};
|
|
655
682
|
|
|
656
683
|
// packages/text2sql/src/lib/adapters/groundings/column-stats.grounding.ts
|
|
657
684
|
var ColumnStatsGrounding = class extends AbstractGrounding {
|
|
658
685
|
constructor(config = {}) {
|
|
659
|
-
super("columnStats");
|
|
686
|
+
super("columnStats", "column_stats");
|
|
660
687
|
}
|
|
661
688
|
/**
|
|
662
689
|
* Execute the grounding process.
|
|
@@ -664,7 +691,17 @@ var ColumnStatsGrounding = class extends AbstractGrounding {
|
|
|
664
691
|
*/
|
|
665
692
|
async execute(ctx) {
|
|
666
693
|
const allContainers = [...ctx.tables, ...ctx.views];
|
|
667
|
-
|
|
694
|
+
const total = allContainers.length;
|
|
695
|
+
for (let i = 0; i < allContainers.length; i++) {
|
|
696
|
+
const container = allContainers[i];
|
|
697
|
+
ctx.onProgress?.({
|
|
698
|
+
type: "phase:progress",
|
|
699
|
+
phase: "column_stats",
|
|
700
|
+
table: container.name,
|
|
701
|
+
message: `Collecting stats for ${container.name}...`,
|
|
702
|
+
current: i + 1,
|
|
703
|
+
total
|
|
704
|
+
});
|
|
668
705
|
for (const column2 of container.columns) {
|
|
669
706
|
try {
|
|
670
707
|
const stats = await this.collectStats(container.name, column2);
|
|
@@ -689,7 +726,7 @@ var ColumnValuesGrounding = class extends AbstractGrounding {
|
|
|
689
726
|
lowCardinalityLimit;
|
|
690
727
|
maxValueLength;
|
|
691
728
|
constructor(config = {}) {
|
|
692
|
-
super("columnValues");
|
|
729
|
+
super("columnValues", "column_values");
|
|
693
730
|
this.lowCardinalityLimit = config.lowCardinalityLimit ?? 20;
|
|
694
731
|
this.maxValueLength = config.maxValueLength ?? 100;
|
|
695
732
|
}
|
|
@@ -772,7 +809,17 @@ var ColumnValuesGrounding = class extends AbstractGrounding {
|
|
|
772
809
|
*/
|
|
773
810
|
async execute(ctx) {
|
|
774
811
|
const allContainers = [...ctx.tables, ...ctx.views];
|
|
775
|
-
|
|
812
|
+
const total = allContainers.length;
|
|
813
|
+
for (let i = 0; i < allContainers.length; i++) {
|
|
814
|
+
const container = allContainers[i];
|
|
815
|
+
ctx.onProgress?.({
|
|
816
|
+
type: "phase:progress",
|
|
817
|
+
phase: "column_values",
|
|
818
|
+
table: container.name,
|
|
819
|
+
message: `Collecting column values for ${container.name}...`,
|
|
820
|
+
current: i + 1,
|
|
821
|
+
total
|
|
822
|
+
});
|
|
776
823
|
const table2 = this.getTable(ctx, container.name);
|
|
777
824
|
for (const column2 of container.columns) {
|
|
778
825
|
try {
|
|
@@ -826,14 +873,24 @@ var ColumnValuesGrounding = class extends AbstractGrounding {
|
|
|
826
873
|
// packages/text2sql/src/lib/adapters/groundings/constraint.grounding.ts
|
|
827
874
|
var ConstraintGrounding = class extends AbstractGrounding {
|
|
828
875
|
constructor(config = {}) {
|
|
829
|
-
super("constraint");
|
|
876
|
+
super("constraint", "constraints");
|
|
830
877
|
}
|
|
831
878
|
/**
|
|
832
879
|
* Execute the grounding process.
|
|
833
880
|
* Annotates tables in ctx.tables with their constraints.
|
|
834
881
|
*/
|
|
835
882
|
async execute(ctx) {
|
|
836
|
-
|
|
883
|
+
const total = ctx.tables.length;
|
|
884
|
+
for (let i = 0; i < ctx.tables.length; i++) {
|
|
885
|
+
const table2 = ctx.tables[i];
|
|
886
|
+
ctx.onProgress?.({
|
|
887
|
+
type: "phase:progress",
|
|
888
|
+
phase: "constraints",
|
|
889
|
+
table: table2.name,
|
|
890
|
+
message: `Loading constraints for ${table2.name}...`,
|
|
891
|
+
current: i + 1,
|
|
892
|
+
total
|
|
893
|
+
});
|
|
837
894
|
try {
|
|
838
895
|
table2.constraints = await this.getConstraints(table2.name);
|
|
839
896
|
} catch (error) {
|
|
@@ -846,14 +903,24 @@ var ConstraintGrounding = class extends AbstractGrounding {
|
|
|
846
903
|
// packages/text2sql/src/lib/adapters/groundings/indexes.grounding.ts
|
|
847
904
|
var IndexesGrounding = class extends AbstractGrounding {
|
|
848
905
|
constructor(config = {}) {
|
|
849
|
-
super("index");
|
|
906
|
+
super("index", "indexes");
|
|
850
907
|
}
|
|
851
908
|
/**
|
|
852
909
|
* Execute the grounding process.
|
|
853
910
|
* Annotates tables in ctx.tables with their indexes and marks indexed columns.
|
|
854
911
|
*/
|
|
855
912
|
async execute(ctx) {
|
|
856
|
-
|
|
913
|
+
const total = ctx.tables.length;
|
|
914
|
+
for (let i = 0; i < ctx.tables.length; i++) {
|
|
915
|
+
const table2 = ctx.tables[i];
|
|
916
|
+
ctx.onProgress?.({
|
|
917
|
+
type: "phase:progress",
|
|
918
|
+
phase: "indexes",
|
|
919
|
+
table: table2.name,
|
|
920
|
+
message: `Loading indexes for ${table2.name}...`,
|
|
921
|
+
current: i + 1,
|
|
922
|
+
total
|
|
923
|
+
});
|
|
857
924
|
table2.indexes = await this.getIndexes(table2.name);
|
|
858
925
|
for (const index2 of table2.indexes ?? []) {
|
|
859
926
|
for (const colName of index2.columns) {
|
|
@@ -870,7 +937,7 @@ var IndexesGrounding = class extends AbstractGrounding {
|
|
|
870
937
|
// packages/text2sql/src/lib/adapters/groundings/info.grounding.ts
|
|
871
938
|
var InfoGrounding = class extends AbstractGrounding {
|
|
872
939
|
constructor(config = {}) {
|
|
873
|
-
super("dialectInfo");
|
|
940
|
+
super("dialectInfo", "info");
|
|
874
941
|
}
|
|
875
942
|
/**
|
|
876
943
|
* Execute the grounding process.
|
|
@@ -884,14 +951,24 @@ var InfoGrounding = class extends AbstractGrounding {
|
|
|
884
951
|
// packages/text2sql/src/lib/adapters/groundings/row-count.grounding.ts
|
|
885
952
|
var RowCountGrounding = class extends AbstractGrounding {
|
|
886
953
|
constructor(config = {}) {
|
|
887
|
-
super("rowCount");
|
|
954
|
+
super("rowCount", "row_counts");
|
|
888
955
|
}
|
|
889
956
|
/**
|
|
890
957
|
* Execute the grounding process.
|
|
891
958
|
* Annotates tables in ctx.tables with row counts and size hints.
|
|
892
959
|
*/
|
|
893
960
|
async execute(ctx) {
|
|
894
|
-
|
|
961
|
+
const total = ctx.tables.length;
|
|
962
|
+
for (let i = 0; i < ctx.tables.length; i++) {
|
|
963
|
+
const table2 = ctx.tables[i];
|
|
964
|
+
ctx.onProgress?.({
|
|
965
|
+
type: "phase:progress",
|
|
966
|
+
phase: "row_counts",
|
|
967
|
+
table: table2.name,
|
|
968
|
+
message: `Counting rows in ${table2.name}...`,
|
|
969
|
+
current: i + 1,
|
|
970
|
+
total
|
|
971
|
+
});
|
|
895
972
|
const count = await this.getRowCount(table2.name);
|
|
896
973
|
if (count != null) {
|
|
897
974
|
table2.rowCount = count;
|
|
@@ -918,7 +995,7 @@ var TableGrounding = class extends AbstractGrounding {
|
|
|
918
995
|
#forward;
|
|
919
996
|
#backward;
|
|
920
997
|
constructor(config = {}) {
|
|
921
|
-
super("table");
|
|
998
|
+
super("table", "tables");
|
|
922
999
|
this.#filter = config.filter;
|
|
923
1000
|
this.#columns = config.columns;
|
|
924
1001
|
this.#forward = config.forward;
|
|
@@ -934,9 +1011,17 @@ var TableGrounding = class extends AbstractGrounding {
|
|
|
934
1011
|
const backward = this.#backward;
|
|
935
1012
|
if (!forward && !backward) {
|
|
936
1013
|
const tables3 = await Promise.all(
|
|
937
|
-
seedTables.map(
|
|
938
|
-
|
|
939
|
-
|
|
1014
|
+
seedTables.map(async (name, index2) => {
|
|
1015
|
+
ctx.onProgress?.({
|
|
1016
|
+
type: "phase:progress",
|
|
1017
|
+
phase: "tables",
|
|
1018
|
+
table: name,
|
|
1019
|
+
message: `Loading table ${name}...`,
|
|
1020
|
+
current: index2 + 1,
|
|
1021
|
+
total: seedTables.length
|
|
1022
|
+
});
|
|
1023
|
+
return applyColumnFilter(await this.getTable(name), this.#columns);
|
|
1024
|
+
})
|
|
940
1025
|
);
|
|
941
1026
|
ctx.tables.push(...tables3);
|
|
942
1027
|
return;
|
|
@@ -948,6 +1033,7 @@ var TableGrounding = class extends AbstractGrounding {
|
|
|
948
1033
|
const backwardQueue = [];
|
|
949
1034
|
const forwardVisited = /* @__PURE__ */ new Set();
|
|
950
1035
|
const backwardVisited = /* @__PURE__ */ new Set();
|
|
1036
|
+
let loadedCount = 0;
|
|
951
1037
|
for (const name of seedTables) {
|
|
952
1038
|
if (forward) forwardQueue.push({ name, depth: 0 });
|
|
953
1039
|
if (backward) backwardQueue.push({ name, depth: 0 });
|
|
@@ -960,6 +1046,14 @@ var TableGrounding = class extends AbstractGrounding {
|
|
|
960
1046
|
if (forwardVisited.has(name)) continue;
|
|
961
1047
|
forwardVisited.add(name);
|
|
962
1048
|
if (!tables2[name]) {
|
|
1049
|
+
loadedCount++;
|
|
1050
|
+
ctx.onProgress?.({
|
|
1051
|
+
type: "phase:progress",
|
|
1052
|
+
phase: "tables",
|
|
1053
|
+
table: name,
|
|
1054
|
+
message: `Loading table ${name}...`,
|
|
1055
|
+
current: loadedCount
|
|
1056
|
+
});
|
|
963
1057
|
tables2[name] = applyColumnFilter(
|
|
964
1058
|
await this.getTable(name),
|
|
965
1059
|
this.#columns
|
|
@@ -984,6 +1078,14 @@ var TableGrounding = class extends AbstractGrounding {
|
|
|
984
1078
|
if (backwardVisited.has(name)) continue;
|
|
985
1079
|
backwardVisited.add(name);
|
|
986
1080
|
if (!tables2[name]) {
|
|
1081
|
+
loadedCount++;
|
|
1082
|
+
ctx.onProgress?.({
|
|
1083
|
+
type: "phase:progress",
|
|
1084
|
+
phase: "tables",
|
|
1085
|
+
table: name,
|
|
1086
|
+
message: `Loading table ${name}...`,
|
|
1087
|
+
current: loadedCount
|
|
1088
|
+
});
|
|
987
1089
|
tables2[name] = applyColumnFilter(
|
|
988
1090
|
await this.getTable(name),
|
|
989
1091
|
this.#columns
|
|
@@ -1544,7 +1646,7 @@ var ViewGrounding = class extends AbstractGrounding {
|
|
|
1544
1646
|
#columns;
|
|
1545
1647
|
includeDefinition;
|
|
1546
1648
|
constructor(config = {}) {
|
|
1547
|
-
super("view");
|
|
1649
|
+
super("view", "views");
|
|
1548
1650
|
this.#filter = config.filter;
|
|
1549
1651
|
this.#columns = config.columns;
|
|
1550
1652
|
this.includeDefinition = config.includeDefinition ?? true;
|
|
@@ -1556,9 +1658,17 @@ var ViewGrounding = class extends AbstractGrounding {
|
|
|
1556
1658
|
async execute(ctx) {
|
|
1557
1659
|
const viewNames = await this.applyFilter();
|
|
1558
1660
|
const views2 = await Promise.all(
|
|
1559
|
-
viewNames.map(
|
|
1560
|
-
|
|
1561
|
-
|
|
1661
|
+
viewNames.map(async (name, index2) => {
|
|
1662
|
+
ctx.onProgress?.({
|
|
1663
|
+
type: "phase:progress",
|
|
1664
|
+
phase: "views",
|
|
1665
|
+
table: name,
|
|
1666
|
+
message: `Loading view ${name}...`,
|
|
1667
|
+
current: index2 + 1,
|
|
1668
|
+
total: viewNames.length
|
|
1669
|
+
});
|
|
1670
|
+
return applyColumnFilter(await this.getView(name), this.#columns);
|
|
1671
|
+
})
|
|
1562
1672
|
);
|
|
1563
1673
|
ctx.views.push(...views2);
|
|
1564
1674
|
}
|