@deepagents/text2sql 0.26.0 → 0.28.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/README.md +1 -1
  2. package/dist/index.js +72 -53
  3. package/dist/index.js.map +2 -2
  4. package/dist/lib/adapters/bigquery/index.js +2 -1
  5. package/dist/lib/adapters/bigquery/index.js.map +2 -2
  6. package/dist/lib/adapters/groundings/abstract.grounding.d.ts +2 -1
  7. package/dist/lib/adapters/groundings/abstract.grounding.d.ts.map +1 -1
  8. package/dist/lib/adapters/groundings/index.js.map +2 -2
  9. package/dist/lib/adapters/mysql/index.js +2 -1
  10. package/dist/lib/adapters/mysql/index.js.map +2 -2
  11. package/dist/lib/adapters/postgres/index.js +2 -1
  12. package/dist/lib/adapters/postgres/index.js.map +2 -2
  13. package/dist/lib/adapters/runtime-scope.d.ts +14 -0
  14. package/dist/lib/adapters/runtime-scope.d.ts.map +1 -0
  15. package/dist/lib/adapters/spreadsheet/index.js +2 -1
  16. package/dist/lib/adapters/spreadsheet/index.js.map +2 -2
  17. package/dist/lib/adapters/sqlite/index.js +2 -1
  18. package/dist/lib/adapters/sqlite/index.js.map +2 -2
  19. package/dist/lib/adapters/sqlserver/index.js +2 -1
  20. package/dist/lib/adapters/sqlserver/index.js.map +2 -2
  21. package/dist/lib/agents/exceptions.d.ts +22 -0
  22. package/dist/lib/agents/exceptions.d.ts.map +1 -1
  23. package/dist/lib/fragments/schema.d.ts +2 -1
  24. package/dist/lib/fragments/schema.d.ts.map +1 -1
  25. package/dist/lib/instructions.d.ts +1 -9
  26. package/dist/lib/instructions.d.ts.map +1 -1
  27. package/dist/lib/sql.d.ts +0 -3
  28. package/dist/lib/sql.d.ts.map +1 -1
  29. package/dist/lib/synthesis/index.js +428 -621
  30. package/dist/lib/synthesis/index.js.map +4 -4
  31. package/dist/lib/synthesis/synthesizers/index.d.ts +1 -2
  32. package/dist/lib/synthesis/synthesizers/index.d.ts.map +1 -1
  33. package/package.json +7 -6
  34. package/dist/lib/agents/teachables.agent.d.ts +0 -10
  35. package/dist/lib/agents/teachables.agent.d.ts.map +0 -1
  36. package/dist/lib/synthesis/synthesizers/teachings-generator.d.ts +0 -20
  37. package/dist/lib/synthesis/synthesizers/teachings-generator.d.ts.map +0 -1
package/README.md CHANGED
@@ -97,7 +97,7 @@ const text2sql = new Text2Sql({
97
97
 
98
98
  **Domain fragments** (11 types): `term`, `hint`, `guardrail`, `example`, `explain`, `clarification`, `workflow`, `quirk`, `styleGuide`, `analogy`, `glossary`.
99
99
 
100
- **User fragments** (6 types): `identity`, `persona`, `alias`, `preference`, `userContext`, `correction`.
100
+ **User fragments** (5 types): `identity`, `persona`, `alias`, `preference`, `correction`.
101
101
 
102
102
  See [@deepagents/context](../context/README.md) for full fragment documentation.
103
103
 
package/dist/index.js CHANGED
@@ -8,7 +8,8 @@ function dialectInfo(input) {
8
8
  data: {
9
9
  dialect: input.dialect,
10
10
  ...input.version && { version: input.version },
11
- ...input.database && { database: input.database }
11
+ ...input.database && { database: input.database },
12
+ ...input.details && { details: input.details }
12
13
  }
13
14
  };
14
15
  }
@@ -415,6 +416,7 @@ function getTablesWithRelated(allTables, relationships, filter) {
415
416
  // packages/text2sql/src/lib/agents/exceptions.ts
416
417
  var sqlValidationMarker = Symbol("SQLValidationError");
417
418
  var unanswerableSqlMarker = Symbol("UnanswerableSQLError");
419
+ var sqlScopeMarker = Symbol("SQLScopeError");
418
420
  var SQLValidationError = class _SQLValidationError extends Error {
419
421
  [sqlValidationMarker];
420
422
  constructor(message) {
@@ -437,6 +439,21 @@ var UnanswerableSQLError = class _UnanswerableSQLError extends Error {
437
439
  return error instanceof _UnanswerableSQLError && error[unanswerableSqlMarker] === true;
438
440
  }
439
441
  };
442
+ var SQLScopeError = class _SQLScopeError extends Error {
443
+ [sqlScopeMarker];
444
+ payload;
445
+ errorType;
446
+ constructor(payload) {
447
+ super(JSON.stringify(payload));
448
+ this.name = "SQLScopeError";
449
+ this.payload = payload;
450
+ this.errorType = payload.error_type;
451
+ this[sqlScopeMarker] = true;
452
+ }
453
+ static isInstance(error) {
454
+ return error instanceof _SQLScopeError && error[sqlScopeMarker] === true;
455
+ }
456
+ };
440
457
 
441
458
  // packages/text2sql/src/lib/agents/result-tools.ts
442
459
  import { tool } from "ai";
@@ -4742,7 +4759,7 @@ function reasoningFramework() {
4742
4759
  "You are a very strong reasoner and planner. Use these critical instructions to structure your plans, thoughts, and responses."
4743
4760
  ),
4744
4761
  fragment2(
4745
- "meta-cognitive-reasoning-framework",
4762
+ "meta_cognitive_reasoning_framework",
4746
4763
  hint2(
4747
4764
  "Before taking any action (either tool calls *or* responses to the user), you must proactively, methodically, and independently plan and reason about:"
4748
4765
  ),
@@ -4852,8 +4869,7 @@ function reasoningFramework() {
4852
4869
  )
4853
4870
  ];
4854
4871
  }
4855
- function guidelines(options = {}) {
4856
- const { date = "strict" } = options;
4872
+ function guidelines() {
4857
4873
  const baseTeachings = [
4858
4874
  // Include the meta-cognitive reasoning framework
4859
4875
  ...reasoningFramework(),
@@ -4868,7 +4884,7 @@ function guidelines(options = {}) {
4868
4884
  policy2({
4869
4885
  rule: "YOU MUST resolve ambiguous business terms with the user",
4870
4886
  before: "making ANY assumptions about terminology meaning",
4871
- reason: "NEVER guess domain-specific language\u2014ask for clarification"
4887
+ reason: "NEVER guess domain-specific language, instead ask for clarification"
4872
4888
  }),
4873
4889
  policy2({
4874
4890
  rule: "YOU MUST validate SQL syntax",
@@ -4883,7 +4899,7 @@ function guidelines(options = {}) {
4883
4899
  ),
4884
4900
  // Few-shot: Applying reasoning principles
4885
4901
  fragment2(
4886
- "reasoning-examples",
4902
+ "reasoning_examples",
4887
4903
  example2({
4888
4904
  question: "Show me sales last month",
4889
4905
  answer: `Applying Principle 1 (Logical dependencies):
@@ -4924,15 +4940,19 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
4924
4940
  // Schema adherence - consolidated into clear rules
4925
4941
  fragment2(
4926
4942
  "schema_adherence",
4927
- hint2(
4928
- "Use only tables and columns from the schema. For unspecified columns, use SELECT *. When showing related items, include IDs and requested details."
4929
- ),
4930
- hint2(
4931
- '"Show" means list items; "count" or "total" means aggregate. Use canonical values verbatim for filtering.'
4932
- )
4943
+ guardrail2({
4944
+ rule: "Use only tables and columns that exist in the schema.",
4945
+ reason: "Inventing tables or columns produces invalid SQL and breaks schema grounding.",
4946
+ action: "If the user requests unspecified fields, use SELECT *. When showing related items, include IDs and requested details."
4947
+ }),
4948
+ explain({
4949
+ concept: "query intent words",
4950
+ explanation: '"Show" asks for listing rows, while "count" or "total" asks for aggregation.',
4951
+ therefore: 'Use listing queries for "show" requests, aggregate queries for "count" or "total", and use canonical schema values verbatim in filters.'
4952
+ })
4933
4953
  ),
4934
4954
  fragment2(
4935
- "Column statistics",
4955
+ "column_statistics",
4936
4956
  explain({
4937
4957
  concept: "nDistinct in column stats",
4938
4958
  explanation: "Positive values are the estimated count of distinct values. Negative values represent the fraction of unique rows (e.g., -1 means all rows are unique, -0.5 means 50% unique)",
@@ -4953,7 +4973,7 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
4953
4973
  ),
4954
4974
  // Aggregations - explain the concepts
4955
4975
  fragment2(
4956
- "Aggregations",
4976
+ "aggregations",
4957
4977
  hint2(
4958
4978
  "Apply COUNT, SUM, AVG when the question implies summarization. Use window functions for ranking, running totals, or row comparisons."
4959
4979
  ),
@@ -4965,7 +4985,7 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
4965
4985
  ),
4966
4986
  // Query semantics - explain concepts and document quirks
4967
4987
  fragment2(
4968
- "Query interpretation",
4988
+ "query_interpretation",
4969
4989
  explain({
4970
4990
  concept: "threshold language",
4971
4991
  explanation: 'Words like "reach", "hit", "exceed" with a value imply a threshold being met or passed',
@@ -4993,7 +5013,7 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
4993
5013
  }),
4994
5014
  // Safety guardrails - consolidated
4995
5015
  fragment2(
4996
- "Query safety",
5016
+ "query_safety",
4997
5017
  guardrail2({
4998
5018
  rule: "Generate only valid, executable SELECT/WITH statements.",
4999
5019
  reason: "Read-only access prevents data modification.",
@@ -5008,18 +5028,8 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
5008
5028
  rule: "Preserve query semantics.",
5009
5029
  reason: "Arbitrary modifications change results.",
5010
5030
  action: 'Only add LIMIT for explicit "top N" requests. Add ORDER BY for deterministic results.'
5011
- }),
5012
- guardrail2({
5013
- rule: "Seek clarification for genuine ambiguity.",
5014
- reason: "Prevents incorrect assumptions.",
5015
- action: "Ask a focused question before guessing."
5016
5031
  })
5017
5032
  ),
5018
- clarification({
5019
- when: "Ambiguous ranking language (top, best, active) without a metric.",
5020
- ask: "Clarify the ranking metric or definition.",
5021
- reason: "Ensures correct aggregation and ordering."
5022
- }),
5023
5033
  hint2(
5024
5034
  'Use sample cell values from schema hints to match exact casing and format in WHERE conditions (e.g., "Male" vs "male" vs "M").'
5025
5035
  ),
@@ -5079,7 +5089,7 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
5079
5089
  notes: "If reference is ambiguous, ask which previous result or entity the user means."
5080
5090
  }),
5081
5091
  fragment2(
5082
- "Bash tool usage",
5092
+ "bash_tool_usage",
5083
5093
  workflow2({
5084
5094
  task: "Query execution",
5085
5095
  steps: [
@@ -5089,35 +5099,44 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
5089
5099
  "For large results, slice first: cat <path> | jq '.[:10]'"
5090
5100
  ]
5091
5101
  }),
5092
- hint2(
5093
- `You cannot access sql through a tool, it'll fail so the proper way to access it is through the bash tool using "sql run" and "sql validate" commands.`
5094
- ),
5095
- hint2(
5096
- "The sql command outputs: file path, column names (comma-separated), and row count. Use column names to construct precise jq queries."
5097
- ),
5098
- hint2(
5099
- 'This is virtual bash environment and "sql" commands proxy to the database hence you cannot access sql files directly.'
5100
- ),
5101
- hint2(
5102
- "If a query fails, the sql command returns an error message in stderr."
5103
- )
5104
- )
5105
- ];
5106
- if (date === "strict") {
5107
- baseTeachings.push(
5102
+ guardrail2({
5103
+ rule: "Do not attempt SQL access through non-bash tools.",
5104
+ reason: "SQL access is only available through the virtual bash environment.",
5105
+ action: 'Use "sql run" and "sql validate" through bash.'
5106
+ }),
5107
+ explain({
5108
+ concept: "sql command output format",
5109
+ explanation: "The sql command returns a file path, comma-separated column names, and a row count.",
5110
+ therefore: "Use the returned column names to build precise jq queries against the output file."
5111
+ }),
5112
+ quirk({
5113
+ issue: "This is a virtual bash environment, so you cannot access underlying SQL files directly.",
5114
+ workaround: "Treat the returned result path as the artifact to inspect, rather than trying to access SQL files themselves."
5115
+ }),
5116
+ quirk({
5117
+ issue: "If a query fails, the sql command reports the error on stderr.",
5118
+ workaround: "Read stderr first and classify the failure before retrying or changing the query."
5119
+ })
5120
+ ),
5121
+ fragment2(
5122
+ "clarifications",
5123
+ guardrail2({
5124
+ rule: "Do not invent an answer when the available schema, results, or user request are insufficient to determine it.",
5125
+ reason: "Prevents hallucinations and improves trustworthiness.",
5126
+ action: "State that you do not have enough information to determine the answer and ask a focused clarification question."
5127
+ }),
5128
+ clarification({
5129
+ when: "Ambiguous ranking language (top, best, active) without a metric.",
5130
+ ask: "Clarify the ranking metric or definition.",
5131
+ reason: "Ensures correct aggregation and ordering."
5132
+ }),
5108
5133
  clarification({
5109
5134
  when: "The request targets time-based data without a date range.",
5110
5135
  ask: "Confirm the intended timeframe (e.g., last 30/90 days, YTD, specific year).",
5111
5136
  reason: "Prevents large scans and irrelevant results."
5112
5137
  })
5113
- );
5114
- } else {
5115
- baseTeachings.push(
5116
- hint2(
5117
- 'When a month, day, or time period is mentioned without a year (e.g., "in August", "on Monday"), assume ALL occurrences of that period in the data. Do not ask for year clarification.'
5118
- )
5119
- );
5120
- }
5138
+ )
5139
+ ];
5121
5140
  return baseTeachings;
5122
5141
  }
5123
5142
 
@@ -5143,7 +5162,6 @@ var Text2Sql = class {
5143
5162
  #config;
5144
5163
  constructor(config) {
5145
5164
  this.#config = {
5146
- teachingsOptions: config.teachingsOptions,
5147
5165
  adapter: config.adapter,
5148
5166
  context: config.context,
5149
5167
  tools: config.tools ?? {},
@@ -5207,7 +5225,7 @@ var Text2Sql = class {
5207
5225
  }
5208
5226
  const trackedFs = new TrackedFs(this.#config.filesystem);
5209
5227
  const context = this.#config.context(
5210
- ...guidelines(this.#config.teachingsOptions),
5228
+ ...guidelines(),
5211
5229
  ...await this.index()
5212
5230
  );
5213
5231
  const lastItem = messages[messages.length - 1];
@@ -5318,6 +5336,7 @@ export {
5318
5336
  MssqlFs,
5319
5337
  Point,
5320
5338
  PostgresFs,
5339
+ SQLScopeError,
5321
5340
  SQLValidationError,
5322
5341
  ScopedFs,
5323
5342
  SqliteFs,