@deepagents/text2sql 0.37.1 → 0.38.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.
Files changed (37) hide show
  1. package/dist/index.js +159 -40
  2. package/dist/index.js.map +2 -2
  3. package/dist/lib/adapters/adapter.d.ts +4 -4
  4. package/dist/lib/adapters/adapter.d.ts.map +1 -1
  5. package/dist/lib/adapters/bigquery/constraint.bigquery.grounding.d.ts.map +1 -1
  6. package/dist/lib/adapters/bigquery/index.js +160 -31
  7. package/dist/lib/adapters/bigquery/index.js.map +3 -3
  8. package/dist/lib/adapters/bigquery/indexes.bigquery.grounding.d.ts.map +1 -1
  9. package/dist/lib/adapters/bigquery/row-count.bigquery.grounding.d.ts.map +1 -1
  10. package/dist/lib/adapters/groundings/abstract.grounding.d.ts +3 -1
  11. package/dist/lib/adapters/groundings/abstract.grounding.d.ts.map +1 -1
  12. package/dist/lib/adapters/groundings/column-stats.grounding.d.ts.map +1 -1
  13. package/dist/lib/adapters/groundings/column-values.grounding.d.ts.map +1 -1
  14. package/dist/lib/adapters/groundings/constraint.grounding.d.ts.map +1 -1
  15. package/dist/lib/adapters/groundings/context.d.ts +6 -2
  16. package/dist/lib/adapters/groundings/context.d.ts.map +1 -1
  17. package/dist/lib/adapters/groundings/index.js +96 -20
  18. package/dist/lib/adapters/groundings/index.js.map +2 -2
  19. package/dist/lib/adapters/groundings/indexes.grounding.d.ts.map +1 -1
  20. package/dist/lib/adapters/groundings/row-count.grounding.d.ts.map +1 -1
  21. package/dist/lib/adapters/groundings/table.grounding.d.ts.map +1 -1
  22. package/dist/lib/adapters/groundings/view.grounding.d.ts.map +1 -1
  23. package/dist/lib/adapters/mysql/index.js +145 -35
  24. package/dist/lib/adapters/mysql/index.js.map +3 -3
  25. package/dist/lib/adapters/postgres/index.js +151 -35
  26. package/dist/lib/adapters/postgres/index.js.map +3 -3
  27. package/dist/lib/adapters/postgres/postgres.d.ts.map +1 -1
  28. package/dist/lib/adapters/spreadsheet/index.js +109 -27
  29. package/dist/lib/adapters/spreadsheet/index.js.map +3 -3
  30. package/dist/lib/adapters/sqlite/index.js +145 -35
  31. package/dist/lib/adapters/sqlite/index.js.map +3 -3
  32. package/dist/lib/adapters/sqlserver/index.js +151 -35
  33. package/dist/lib/adapters/sqlserver/index.js.map +3 -3
  34. package/dist/lib/adapters/sqlserver/sqlserver.d.ts.map +1 -1
  35. package/dist/lib/sql.d.ts +14 -2
  36. package/dist/lib/sql.d.ts.map +1 -1
  37. package/package.json +4 -4
@@ -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
- await grounding.execute(ctx);
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 scopeDialect = scopeDialects[dialect];
516
- if (!scopeDialect) {
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
- try {
523
- references = extractBaseEntityReferences(sql, scopeDialect);
524
- } catch (error) {
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
- error
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
- constructor(name) {
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
- for (const container of allContainers) {
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
- for (const container of allContainers) {
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
- for (const table2 of ctx.tables) {
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
- for (const table2 of ctx.tables) {
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
- for (const table2 of ctx.tables) {
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
- async (name) => applyColumnFilter(await this.getTable(name), this.#columns)
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
@@ -1629,7 +1731,9 @@ var SqlServer = class extends Adapter {
1629
1731
  const table2 = tables2[i];
1630
1732
  const tableIdentifier = this.#formatQualifiedTableName(table2);
1631
1733
  onProgress?.({
1734
+ type: "phase:progress",
1632
1735
  phase: "row_counts",
1736
+ table: table2.name,
1633
1737
  message: `Counting rows in ${table2.name}...`,
1634
1738
  current: i + 1,
1635
1739
  total
@@ -1724,7 +1828,9 @@ var SqlServer = class extends Adapter {
1724
1828
  const table2 = tables2[i];
1725
1829
  const tableIdentifier = this.#formatQualifiedTableName(table2);
1726
1830
  onProgress?.({
1831
+ type: "phase:progress",
1727
1832
  phase: "column_stats",
1833
+ table: table2.name,
1728
1834
  message: `Collecting stats for ${table2.name}...`,
1729
1835
  current: i + 1,
1730
1836
  total
@@ -1766,7 +1872,9 @@ var SqlServer = class extends Adapter {
1766
1872
  const table2 = tables2[i];
1767
1873
  const tableIdentifier = this.#formatQualifiedTableName(table2);
1768
1874
  onProgress?.({
1875
+ type: "phase:progress",
1769
1876
  phase: "low_cardinality",
1877
+ table: table2.name,
1770
1878
  message: `Analyzing cardinality in ${table2.name}...`,
1771
1879
  current: i + 1,
1772
1880
  total
@@ -2053,7 +2161,7 @@ var ViewGrounding = class extends AbstractGrounding {
2053
2161
  #columns;
2054
2162
  includeDefinition;
2055
2163
  constructor(config = {}) {
2056
- super("view");
2164
+ super("view", "views");
2057
2165
  this.#filter = config.filter;
2058
2166
  this.#columns = config.columns;
2059
2167
  this.includeDefinition = config.includeDefinition ?? true;
@@ -2065,9 +2173,17 @@ var ViewGrounding = class extends AbstractGrounding {
2065
2173
  async execute(ctx) {
2066
2174
  const viewNames = await this.applyFilter();
2067
2175
  const views2 = await Promise.all(
2068
- viewNames.map(
2069
- async (name) => applyColumnFilter(await this.getView(name), this.#columns)
2070
- )
2176
+ viewNames.map(async (name, index2) => {
2177
+ ctx.onProgress?.({
2178
+ type: "phase:progress",
2179
+ phase: "views",
2180
+ table: name,
2181
+ message: `Loading view ${name}...`,
2182
+ current: index2 + 1,
2183
+ total: viewNames.length
2184
+ });
2185
+ return applyColumnFilter(await this.getView(name), this.#columns);
2186
+ })
2071
2187
  );
2072
2188
  ctx.views.push(...views2);
2073
2189
  }