@deepagents/text2sql 2.1.0 → 2.3.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/dist/bin/sql.js CHANGED
@@ -3,91 +3,8 @@
3
3
  // packages/text2sql/src/bin/sql.ts
4
4
  import { cac } from "cac";
5
5
 
6
- // packages/text2sql/src/bin/command.ts
7
- import * as path from "node:path";
8
- var OUT_DIR_OPTION = {
9
- flag: "--out-dir <path>",
10
- description: "Output directory (default: $TEXT2SQL_OUT_DIR or ./sql)"
11
- };
12
- function resolveOutputDir(ctx, options) {
13
- const explicit = typeof options.outDir === "string" ? options.outDir : void 0;
14
- return path.resolve(ctx.cwd, explicit ?? ctx.env.TEXT2SQL_OUT_DIR ?? "./sql");
15
- }
16
- var CommandError = class extends Error {
17
- command;
18
- exitCode;
19
- constructor(command, message, exitCode = 1) {
20
- super(message);
21
- this.name = "CommandError";
22
- this.command = command;
23
- this.exitCode = exitCode;
24
- }
25
- };
26
- function renderCommandError(error, stderr) {
27
- stderr.write(`sql ${error.command}: ${error.message}
28
- `);
29
- }
30
- function errorMessage(error) {
31
- return error instanceof Error ? error.message : String(error);
32
- }
33
- var SqlCommand = class {
34
- options = [];
35
- helpDisplay;
36
- fail(message, exitCode = 1) {
37
- throw new CommandError(this.name, message, exitCode);
38
- }
39
- requireAdapter(adapters, db) {
40
- const available = Object.keys(adapters).join(", ") || "(none configured)";
41
- if (!db) {
42
- this.fail(
43
- `missing database name. Usage: sql ${this.name} ${this.usage}. Available: ${available}`
44
- );
45
- }
46
- const adapter = adapters[db];
47
- if (!adapter) {
48
- this.fail(`unknown database "${db}". Available: ${available}`);
49
- }
50
- return adapter;
51
- }
52
- };
53
- var SqlQueryCommand = class extends SqlCommand {
54
- args = "<db> <...sql>";
55
- async execute(ctx, args, options) {
56
- const [db, sqlParts] = args;
57
- const adapter = this.requireAdapter(ctx.adapters, db);
58
- const sql = (sqlParts ?? []).join(" ").trim();
59
- if (!sql) this.fail("no query provided");
60
- const formatted = await this.formatAndValidate(adapter, sql);
61
- return this.afterValidation(ctx, adapter, formatted, options);
62
- }
63
- async formatAndValidate(adapter, sql) {
64
- let formatted;
65
- try {
66
- formatted = adapter.format(sql);
67
- } catch (error) {
68
- this.fail(errorMessage(error));
69
- }
70
- try {
71
- const syntaxError = await adapter.validate(formatted);
72
- if (syntaxError) this.fail(syntaxError);
73
- } catch (error) {
74
- this.fail(errorMessage(error));
75
- }
76
- return formatted;
77
- }
78
- };
79
-
80
- // packages/text2sql/src/bin/commands/index.ts
81
- import { constants, createWriteStream } from "node:fs";
82
- import {
83
- mkdir,
84
- open,
85
- stat,
86
- writeFile as writeFile2
87
- } from "node:fs/promises";
88
- import * as path3 from "node:path";
89
- import { finished } from "node:stream/promises";
90
- import { v7 } from "uuid";
6
+ // packages/text2sql/src/lib/sql.ts
7
+ import "@deepagents/agent";
91
8
 
92
9
  // packages/text2sql/src/lib/adapter-index.ts
93
10
  import { fragment } from "@deepagents/context";
@@ -191,18 +108,29 @@ function createGroundingContext(options = {}) {
191
108
  import nodeSqlParser from "node-sql-parser";
192
109
  var { Parser } = nodeSqlParser;
193
110
  var parser = new Parser();
111
+ function buildScopeParseErrorPayload(sql, dialect, error) {
112
+ const parserError = error instanceof Error ? error.message : String(error ?? "Unknown error");
113
+ return {
114
+ error: `SQL scope analysis failed before validation/execution: ${parserError}`,
115
+ error_type: "SQL_SCOPE_PARSE_ERROR",
116
+ suggestion: "Rewrite the query into simpler SQL that can be analyzed safely, or extend parser coverage for this dialect feature.",
117
+ sql_attempted: sql,
118
+ parser_dialect: dialect,
119
+ parser_error: parserError
120
+ };
121
+ }
194
122
 
195
123
  // packages/text2sql/src/lib/file-cache.ts
196
124
  import { createHash } from "node:crypto";
197
125
  import { existsSync } from "node:fs";
198
126
  import { readFile, writeFile } from "node:fs/promises";
199
127
  import { tmpdir } from "node:os";
200
- import path2 from "node:path";
128
+ import path from "node:path";
201
129
  var FileCache = class {
202
130
  path;
203
131
  constructor(watermark, extension = ".txt") {
204
132
  const hash = createHash("md5").update(watermark).digest("hex");
205
- this.path = path2.join(tmpdir(), `text2sql-${hash}${extension}`);
133
+ this.path = path.join(tmpdir(), `text2sql-${hash}${extension}`);
206
134
  }
207
135
  async get() {
208
136
  if (existsSync(this.path)) {
@@ -351,7 +279,7 @@ var AdapterIndexer = class {
351
279
  });
352
280
  return fragments;
353
281
  } catch (error) {
354
- const reason = errorMessage2(error);
282
+ const reason = errorMessage(error);
355
283
  progress({
356
284
  type: "adapter:error",
357
285
  adapter: name,
@@ -398,9 +326,647 @@ function adapterProgressEvent(adapter, progress) {
398
326
  timestampMs: progress.timestampMs
399
327
  };
400
328
  }
329
+ function errorMessage(error) {
330
+ return error instanceof Error ? error.message : String(error);
331
+ }
332
+
333
+ // packages/text2sql/src/lib/agents/sql.agent.ts
334
+ import { groq } from "@ai-sdk/groq";
335
+ import {
336
+ APICallError,
337
+ JSONParseError,
338
+ NoContentGeneratedError,
339
+ NoObjectGeneratedError,
340
+ NoOutputGeneratedError,
341
+ TypeValidationError,
342
+ defaultSettingsMiddleware,
343
+ wrapLanguageModel
344
+ } from "ai";
345
+ import dedent from "dedent";
346
+ import pRetry from "p-retry";
347
+ import z from "zod";
348
+ import "@deepagents/agent";
349
+ import {
350
+ ContextEngine,
351
+ InMemoryContextStore,
352
+ example,
353
+ fragment as fragment2,
354
+ guardrail,
355
+ hint,
356
+ persona,
357
+ policy,
358
+ structuredOutput,
359
+ user,
360
+ workflow
361
+ } from "@deepagents/context";
362
+ var RETRY_TEMPERATURES = [0, 0.2, 0.3];
363
+ var SQL_AGENT_ROLE = "Expert SQL query generator.";
364
+ var SQL_AGENT_OBJECTIVE = "Generate precise SQL grounded in provided schema.";
365
+ var FORMATTER_SCOPE_DIALECTS = /* @__PURE__ */ new Map([
366
+ ["sqlite", "sqlite"],
367
+ ["postgresql", "postgresql"],
368
+ ["bigquery", "bigquery"],
369
+ ["transactsql", "transactsql"],
370
+ ["mysql", "mysql"]
371
+ ]);
372
+ var SQL_AGENT_POLICIES = [
373
+ fragment2(
374
+ "schema_mapping",
375
+ policy({
376
+ rule: "Translate natural language into precise SQL grounded in available schema entities."
377
+ }),
378
+ hint("Preserve schema spelling exactly, including typos in column names.")
379
+ ),
380
+ fragment2(
381
+ "projection_minimality",
382
+ policy({
383
+ rule: "Return only columns requested by the question; do not add helper columns unless explicitly requested."
384
+ }),
385
+ policy({
386
+ rule: 'For requests of the form "X sorted/ordered by Y", project X only unless Y is explicitly requested as an output field.'
387
+ }),
388
+ policy({
389
+ rule: "Prefer selecting schema columns directly without derived expressions when direct selection answers the request."
390
+ }),
391
+ hint(
392
+ "Do not include ORDER BY, GROUP BY, or JOIN helper columns in SELECT output unless the question explicitly asks for them."
393
+ ),
394
+ policy({
395
+ rule: "Use DISTINCT only when uniqueness is explicitly requested (for example distinct/unique/different/no duplicates)."
396
+ }),
397
+ hint(
398
+ 'Do not infer DISTINCT from generic wording such as "some", plural nouns, or entity-set phrasing; for transactional/attendance-style tables, default to raw rows unless uniqueness is explicitly requested.'
399
+ )
400
+ ),
401
+ fragment2(
402
+ "date_transform_safety",
403
+ policy({
404
+ rule: "Do not assume VARCHAR/TEXT values are parseable dates. Avoid date extraction functions on text columns by default."
405
+ }),
406
+ policy({
407
+ rule: "Use date-part extraction only when both conditions hold: the question explicitly asks for transformation and schema values require transformation to produce that unit."
408
+ }),
409
+ hint(
410
+ "Do not apply SUBSTR, STRFTIME, DATE_PART, YEAR, or similar extraction functions unless the question explicitly asks for transformation and schema values require it."
411
+ ),
412
+ hint(
413
+ "If a column already represents the requested concept (for example a stored year-like value), use the column as-is."
414
+ )
415
+ ),
416
+ fragment2(
417
+ "sql_minimality",
418
+ guardrail({
419
+ rule: "Never hallucinate tables or columns.",
420
+ reason: "Schema fidelity is required.",
421
+ action: "Use only available schema entities."
422
+ }),
423
+ guardrail({
424
+ rule: "Avoid unnecessary transformations and derived projections.",
425
+ reason: "Extra transformations frequently change semantics and reduce correctness.",
426
+ action: "Do not add date parsing, substring extraction, or derived columns unless explicitly required by the question or schema."
427
+ })
428
+ ),
429
+ fragment2(
430
+ "preflight_checklist",
431
+ workflow({
432
+ task: "Final SQL preflight before returning output",
433
+ steps: [
434
+ "Verify selected columns match the question and remove unrequested helper projections.",
435
+ "If aggregate values are used only for ranking/filtering, keep them out of SELECT unless explicitly requested.",
436
+ "Prefer raw schema columns over derived expressions when raw columns already satisfy the request.",
437
+ "If a candidate query uses STRFTIME, SUBSTR, DATE_PART, YEAR, or similar extraction on text-like columns, remove that transformation unless explicitly required by the question.",
438
+ "Return only schema-grounded SQL using existing tables and columns."
439
+ ]
440
+ })
441
+ ),
442
+ fragment2(
443
+ "set_semantics",
444
+ policy({
445
+ rule: "For questions asking where both condition A and condition B hold over an attribute, compute the intersection of qualifying sets for that attribute."
446
+ }),
447
+ policy({
448
+ rule: "Do not force the same entity instance to satisfy both conditions unless the question explicitly requests the same person/row/entity."
449
+ }),
450
+ hint(
451
+ "Prefer INTERSECT (or logically equivalent set-based shape) over requiring the same physical row/entity to satisfy both conditions unless explicitly requested."
452
+ ),
453
+ hint(
454
+ "When two conditions describe different row groups whose shared attribute is requested, build each group separately and intersect the attribute values."
455
+ ),
456
+ hint(
457
+ "Do not collapse cross-group conditions into a single-row AND predicate when the intent is shared values across groups."
458
+ ),
459
+ policy({
460
+ rule: "If two predicates on the same field cannot both be true for one row, do not combine them with AND; use set operations across separate filtered subsets when shared values are requested."
461
+ })
462
+ ),
463
+ fragment2(
464
+ "predicate_column_alignment",
465
+ policy({
466
+ rule: "Match literal values to semantically compatible columns. Do not compare descriptive names to identifier columns."
467
+ }),
468
+ hint(
469
+ "When a filter value is a descriptive label (for example a department name), join through the lookup table and filter on its name/title column, not on *_id columns."
470
+ ),
471
+ hint(
472
+ "When relation roles are explicit in wording (for example host/home/source/destination), prefer foreign keys with matching role qualifiers over generic similarly named columns."
473
+ ),
474
+ policy({
475
+ rule: "When multiple foreign-key candidates exist, select the column whose qualifier best matches the relationship described in the question."
476
+ }),
477
+ policy({
478
+ rule: "For hosting/held semantics, prefer host_* relationship columns when available over generic *_id alternatives."
479
+ }),
480
+ hint(
481
+ 'Interpret wording like "held/hosted a competition or event" as a hosting relationship and map to host_* foreign keys when present.'
482
+ ),
483
+ policy({
484
+ rule: "Do not compare descriptive labels or names to *_id columns; join to the table containing the descriptive field and filter there."
485
+ }),
486
+ policy({
487
+ rule: "Keep numeric identifiers unquoted when used as numeric equality filters unless schema indicates text identifiers."
488
+ }),
489
+ policy({
490
+ rule: "When filtering by a descriptive label value and a related table exposes a corresponding *_name or title column, join to that table and filter on the descriptive column."
491
+ })
492
+ ),
493
+ fragment2(
494
+ "ordering_semantics",
495
+ policy({
496
+ rule: "Respect explicit sort direction terms. If direction is not specified, use ascending order unless a superlative intent (most/least/highest/lowest) implies direction."
497
+ }),
498
+ policy({
499
+ rule: "When ranking categories by frequency, use COUNT for ordering but keep output focused on requested category fields unless counts are explicitly requested."
500
+ }),
501
+ policy({
502
+ rule: "Do not use DESC unless descending direction is explicit or a superlative intent requires descending ranking."
503
+ }),
504
+ policy({
505
+ rule: 'For "most common/frequent <attribute>" requests, return the attribute value(s) only; use counts only for ordering/filtering unless the question explicitly asks to return counts.'
506
+ }),
507
+ hint(
508
+ 'Use DESC with LIMIT 1 for "most/highest/largest"; use ASC with LIMIT 1 for "least/lowest/smallest".'
509
+ )
510
+ ),
511
+ fragment2(
512
+ "negative_membership_queries",
513
+ policy({
514
+ rule: "For requests asking entities that did not participate/host/appear in related records, prefer NOT IN or NOT EXISTS against the related foreign-key set."
515
+ }),
516
+ hint(
517
+ "Map role-bearing relationship columns carefully (for example host_* foreign keys for hosting relationships) instead of generic IDs when role wording is explicit."
518
+ ),
519
+ hint(
520
+ 'For "never had/never exceeded" conditions over history tables, exclude entities via NOT IN/NOT EXISTS against the disqualifying entity-id set (often built with GROUP BY/HAVING MAX(...)).'
521
+ )
522
+ ),
523
+ fragment2(
524
+ "join_completeness",
525
+ policy({
526
+ rule: "Preserve entity-restricting joins implied by the question. Do not widen results by querying only a broader attribute table when a subset entity table is available."
527
+ }),
528
+ policy({
529
+ rule: "If an entity term in the question maps to a table, keep that table in query scope and join to attribute tables rather than dropping the entity table."
530
+ }),
531
+ hint(
532
+ "If the question targets a specific entity group, include that entity table and its join conditions even when selected columns come from a related table."
533
+ ),
534
+ hint(
535
+ "When the question names an entity type and a relation table links to that entity via *_id, include the entity table in scope instead of counting only relation rows."
536
+ ),
537
+ hint(
538
+ "Prefer INNER JOIN by default; use LEFT JOIN only when the question explicitly requests including unmatched rows or zero-related entities."
539
+ )
540
+ ),
541
+ fragment2(
542
+ "aggregation_exactness",
543
+ policy({
544
+ rule: "Preserve requested aggregation semantics exactly: use COUNT(*) by default for total rows, use COUNT(DISTINCT ...) only when uniqueness is explicitly requested, and group by stable entity keys when computing per-entity aggregates."
545
+ }),
546
+ policy({
547
+ rule: "For questions asking which entity has lowest/highest average of a metric, compute AVG(metric) per entity (GROUP BY entity) and rank those aggregates."
548
+ }),
549
+ hint(
550
+ 'For "how many <entities>" questions over relation records, default to COUNT(*) on qualifying rows unless explicit uniqueness language is present.'
551
+ )
552
+ ),
553
+ fragment2(
554
+ "query_shape_examples",
555
+ example({
556
+ question: "List categories ordered by how many records belong to each category.",
557
+ answer: "SELECT category FROM records GROUP BY category ORDER BY COUNT(*)"
558
+ }),
559
+ example({
560
+ question: "Show labels shared by rows with metric > 100 and rows with metric < 10.",
561
+ answer: "SELECT label FROM records WHERE metric > 100 INTERSECT SELECT label FROM records WHERE metric < 10"
562
+ }),
563
+ example({
564
+ question: "List locations that have not hosted any event.",
565
+ answer: "SELECT location_name FROM locations WHERE location_id NOT IN (SELECT host_location_id FROM events)"
566
+ }),
567
+ example({
568
+ question: "List the most common category across records.",
569
+ answer: "SELECT category FROM records GROUP BY category ORDER BY COUNT(*) DESC LIMIT 1"
570
+ })
571
+ )
572
+ ];
573
+ function extractSql(output) {
574
+ const match = output.match(/```sql\n?([\s\S]*?)```/);
575
+ return match ? match[1].trim() : output.trim();
576
+ }
577
+ function scopeDialectFor(adapter) {
578
+ const dialect = FORMATTER_SCOPE_DIALECTS.get(adapter.formatterLanguage);
579
+ if (!dialect) {
580
+ throw new TypeError(
581
+ `No scope dialect mapping for formatter language "${adapter.formatterLanguage}".`
582
+ );
583
+ }
584
+ return dialect;
585
+ }
586
+ function fencedMarkdownParseError(sql, adapter) {
587
+ return new SQLValidationError(
588
+ JSON.stringify(
589
+ buildScopeParseErrorPayload(
590
+ sql,
591
+ scopeDialectFor(adapter),
592
+ new Error("SQL response was fenced markdown, not executable SQL.")
593
+ )
594
+ )
595
+ );
596
+ }
597
+ async function toSql(options) {
598
+ const { maxRetries = 3 } = options;
599
+ return withRetry(
600
+ async (attemptNumber, errors, attempts) => {
601
+ const context = new ContextEngine({
602
+ store: new InMemoryContextStore(),
603
+ chatId: `sql-gen-${crypto.randomUUID()}`,
604
+ userId: "system"
605
+ });
606
+ context.set(
607
+ persona({
608
+ name: "Freya",
609
+ role: SQL_AGENT_ROLE,
610
+ objective: SQL_AGENT_OBJECTIVE
611
+ // role: `You are a data science expert that provides well-reasoned and detailed responses.`,
612
+ // objective: `Your task is to understand the schema and generate a valid SQL query to answer the question. You first think about the reasoning process as an internal monologue and then provide the user with the answer.`,
613
+ }),
614
+ ...SQL_AGENT_POLICIES,
615
+ ...options.fragments
616
+ );
617
+ if (errors.length) {
618
+ const lastError = errors.at(-1);
619
+ context.set(
620
+ user(dedent`
621
+ Answer the following question with the SQL code. Use the piece of evidence and base your answer on the database schema.
622
+ Given the question, the evidence and the database schema, return the SQL script that addresses the question.
623
+
624
+ Question: ${options.input}
625
+ `),
626
+ UnanswerableSQLError.isInstance(lastError) ? user(
627
+ `<retry_instruction>Your previous response marked the task as unanswerable. Re-evaluate using best-effort schema mapping. If the core intent is answerable with existing tables/columns, return SQL. Return error only when required core intent cannot be mapped without inventing schema elements.</retry_instruction>`
628
+ ) : user(
629
+ `<validation_error>Your previous SQL query had the following error: ${lastError?.message}. Please fix the query.</validation_error>`
630
+ )
631
+ );
632
+ } else {
633
+ context.set(
634
+ user(dedent`
635
+ Answer the following question with the SQL code. Use the piece of evidence and base your answer on the database schema.
636
+ Given the question, the evidence and the database schema, return the SQL script that addresses the question.
637
+
638
+ Question: ${options.input}
639
+ `)
640
+ );
641
+ }
642
+ const temperature = RETRY_TEMPERATURES[attemptNumber - 1] ?? RETRY_TEMPERATURES[RETRY_TEMPERATURES.length - 1];
643
+ const baseModel = options.model ?? groq("openai/gpt-oss-20b");
644
+ const model = wrapLanguageModel({
645
+ model: baseModel,
646
+ middleware: defaultSettingsMiddleware({ settings: { temperature } })
647
+ });
648
+ const sqlOutput = structuredOutput({
649
+ model,
650
+ context,
651
+ schema: z.object({
652
+ result: z.union([
653
+ z.object({
654
+ sql: z.string().describe("The SQL query that answers the question"),
655
+ reasoning: z.string().describe("The reasoning steps taken to generate the SQL")
656
+ }),
657
+ z.object({
658
+ error: z.string().describe(
659
+ "Error message explaining why the question cannot be answered with the given schema"
660
+ )
661
+ })
662
+ ])
663
+ })
664
+ });
665
+ const { result: output } = await sqlOutput.generate();
666
+ const finalizeSql = async (rawSql) => {
667
+ const extractedSql = extractSql(rawSql);
668
+ const errorsOrUndefined = errors.length ? errors.map(formatErrorMessage) : void 0;
669
+ if (extractedSql === "") {
670
+ return {
671
+ attempts,
672
+ sql: "",
673
+ errors: errorsOrUndefined
674
+ };
675
+ }
676
+ const sql = options.adapter.format(extractedSql);
677
+ if (rawSql.trimStart().startsWith("```") && extractedSql === rawSql.trim()) {
678
+ throw fencedMarkdownParseError(sql, options.adapter);
679
+ }
680
+ const validationError = await options.adapter.validate(sql);
681
+ if (validationError) {
682
+ throw new SQLValidationError(validationError);
683
+ }
684
+ return {
685
+ attempts,
686
+ sql,
687
+ errors: errorsOrUndefined
688
+ };
689
+ };
690
+ if ("error" in output) {
691
+ context.set(
692
+ user(
693
+ "<best_effort_fallback>Do not return unanswerable. Produce the best valid SQL query that answers the core intent using only available schema entities.</best_effort_fallback>"
694
+ )
695
+ );
696
+ const forcedSqlOutput = structuredOutput({
697
+ model,
698
+ context,
699
+ schema: z.object({
700
+ sql: z.string().describe(
701
+ "Best-effort SQL query that answers the core intent using only available schema entities."
702
+ ),
703
+ reasoning: z.string().describe("Reasoning steps for best-effort schema mapping.")
704
+ })
705
+ });
706
+ try {
707
+ const forced = await forcedSqlOutput.generate();
708
+ return await finalizeSql(forced.sql);
709
+ } catch (error) {
710
+ if (SQLValidationError.isInstance(error) || APICallError.isInstance(error) || JSONParseError.isInstance(error) || TypeValidationError.isInstance(error) || NoObjectGeneratedError.isInstance(error) || NoOutputGeneratedError.isInstance(error) || NoContentGeneratedError.isInstance(error)) {
711
+ throw error;
712
+ }
713
+ throw new UnanswerableSQLError(output.error);
714
+ }
715
+ }
716
+ return await finalizeSql(output.sql);
717
+ },
718
+ { retries: maxRetries - 1 }
719
+ );
720
+ }
721
+ function formatErrorMessage(error) {
722
+ if (APICallError.isInstance(error)) {
723
+ if (error.message.startsWith("Failed to validate JSON")) {
724
+ return `Schema validation failed: ${error.message}`;
725
+ }
726
+ return error.message;
727
+ }
728
+ if (SQLValidationError.isInstance(error)) {
729
+ return `SQL Validation Error: ${error.message}`;
730
+ }
731
+ return error.message;
732
+ }
733
+ function isModelUnavailableError(error) {
734
+ if (!APICallError.isInstance(error)) {
735
+ return false;
736
+ }
737
+ const message = error.message.toLowerCase();
738
+ const responseBody = (error.responseBody ?? "").toLowerCase();
739
+ const is404ModelError = error.statusCode === 404 && (message.includes("model") || responseBody.includes("model_not_found"));
740
+ const errorCode = typeof error.data === "object" && error.data !== null && "error" in error.data && typeof error.data.error === "object" && error.data.error !== null && "code" in error.data.error && typeof error.data.error.code === "string" ? error.data.error.code.toLowerCase() : void 0;
741
+ return is404ModelError || errorCode === "model_not_found" || responseBody.includes('"code":"model_not_found"') || message.includes("model") && message.includes("does not exist or you do not have access to it");
742
+ }
743
+ async function withRetry(computation, options = { retries: 3 }) {
744
+ const errors = [];
745
+ let attempts = 0;
746
+ return pRetry(
747
+ (attemptNumber) => {
748
+ return computation(attemptNumber, errors, ++attempts);
749
+ },
750
+ {
751
+ retries: options.retries,
752
+ shouldRetry: (context) => {
753
+ if (UnanswerableSQLError.isInstance(context.error)) {
754
+ return false;
755
+ }
756
+ if (isModelUnavailableError(context.error)) {
757
+ return false;
758
+ }
759
+ if (SQLValidationError.isInstance(context.error)) {
760
+ return true;
761
+ }
762
+ console.log({
763
+ NoObjectGeneratedError: NoObjectGeneratedError.isInstance(
764
+ context.error
765
+ ),
766
+ NoOutputGeneratedError: NoOutputGeneratedError.isInstance(
767
+ context.error
768
+ ),
769
+ APICallError: APICallError.isInstance(context.error),
770
+ JSONParseError: JSONParseError.isInstance(context.error),
771
+ TypeValidationError: TypeValidationError.isInstance(context.error),
772
+ NoContentGeneratedError: NoContentGeneratedError.isInstance(
773
+ context.error
774
+ )
775
+ });
776
+ return APICallError.isInstance(context.error) || JSONParseError.isInstance(context.error) || TypeValidationError.isInstance(context.error) || NoObjectGeneratedError.isInstance(context.error) || NoOutputGeneratedError.isInstance(context.error) || NoContentGeneratedError.isInstance(context.error);
777
+ },
778
+ onFailedAttempt(context) {
779
+ errors.push(context.error);
780
+ }
781
+ }
782
+ );
783
+ }
784
+
785
+ // packages/text2sql/src/lib/sql.ts
786
+ var text2SqlValidationMarker = Symbol("Text2SqlValidationError");
787
+ var text2SqlUnknownAdapterMarker = Symbol("Text2SqlUnknownAdapterError");
788
+ var Text2SqlValidationError = class _Text2SqlValidationError extends Error {
789
+ [text2SqlValidationMarker];
790
+ constructor(message) {
791
+ super(message);
792
+ this.name = "Text2SqlValidationError";
793
+ this[text2SqlValidationMarker] = true;
794
+ }
795
+ static isInstance(error) {
796
+ return error instanceof _Text2SqlValidationError && error[text2SqlValidationMarker] === true;
797
+ }
798
+ };
799
+ var Text2SqlUnknownAdapterError = class _Text2SqlUnknownAdapterError extends Error {
800
+ [text2SqlUnknownAdapterMarker];
801
+ adapter;
802
+ available;
803
+ constructor(adapter, available) {
804
+ const list = available.join(", ") || "(none configured)";
805
+ super(`Unknown adapter "${adapter}". Available: ${list}`);
806
+ this.name = "Text2SqlUnknownAdapterError";
807
+ this.adapter = adapter;
808
+ this.available = available;
809
+ this[text2SqlUnknownAdapterMarker] = true;
810
+ }
811
+ static isInstance(error) {
812
+ return error instanceof _Text2SqlUnknownAdapterError && error[text2SqlUnknownAdapterMarker] === true;
813
+ }
814
+ };
815
+ var Text2Sql = class {
816
+ #config;
817
+ #indexer;
818
+ constructor(config) {
819
+ const adapterNames = Object.keys(config.adapters);
820
+ if (adapterNames.length === 0) {
821
+ throw new Error("Text2Sql requires at least one adapter");
822
+ }
823
+ validateAdapterNames(adapterNames);
824
+ this.#config = config;
825
+ this.#indexer = new AdapterIndexer({
826
+ adapters: config.adapters,
827
+ version: config.version
828
+ });
829
+ }
830
+ #requireAdapter(name) {
831
+ const adapter = this.#config.adapters[name];
832
+ if (!adapter) {
833
+ throw new Text2SqlUnknownAdapterError(name, this.adapterNames());
834
+ }
835
+ return adapter;
836
+ }
837
+ #requireModel(op) {
838
+ const model = this.#config.model;
839
+ if (!model) {
840
+ throw new Error(`Text2Sql.${op}() requires a model in Text2SqlConfig`);
841
+ }
842
+ return model;
843
+ }
844
+ adapterNames() {
845
+ return Object.keys(this.#config.adapters);
846
+ }
847
+ hasAdapter(name) {
848
+ return name in this.#config.adapters;
849
+ }
850
+ async validate(name, sql) {
851
+ const adapter = this.#requireAdapter(name);
852
+ const result = this.#validateWith(adapter, sql);
853
+ return result;
854
+ }
855
+ async run(name, sql) {
856
+ const adapter = this.#requireAdapter(name);
857
+ const formatted = await this.#validateWith(adapter, sql);
858
+ const result = await adapter.execute(formatted);
859
+ if (!Array.isArray(result)) {
860
+ throw new Error("adapter.execute must return an array of rows");
861
+ }
862
+ const columns = result.length > 0 ? Object.keys(result[0]) : [];
863
+ return { rows: result, columns };
864
+ }
865
+ async #validateWith(adapter, sql) {
866
+ const formatted = adapter.format(sql);
867
+ const error = await adapter.validate(formatted);
868
+ if (error) throw new Text2SqlValidationError(error);
869
+ return formatted;
870
+ }
871
+ async index(options = {}) {
872
+ return this.#indexer.index({
873
+ adapterNames: options.names,
874
+ onProgress: options.onProgress
875
+ });
876
+ }
877
+ async toSql(input, adapterName) {
878
+ const adapter = this.#requireAdapter(adapterName);
879
+ const model = this.#requireModel("toSql");
880
+ const fragments = await this.#indexer.indexAdapter(adapterName);
881
+ const result = await toSql({
882
+ input,
883
+ adapter,
884
+ fragments,
885
+ model
886
+ });
887
+ return result.sql;
888
+ }
889
+ async toPairs(adapterName, factory) {
890
+ const adapter = this.#requireAdapter(adapterName);
891
+ const producer = factory(adapter);
892
+ return producer.toPairs();
893
+ }
894
+ };
895
+
896
+ // packages/text2sql/src/bin/command.ts
897
+ import * as path2 from "node:path";
898
+ var OUT_DIR_OPTION = {
899
+ flag: "--out-dir <path>",
900
+ description: "Output directory (default: $TEXT2SQL_OUT_DIR or ./sql)"
901
+ };
902
+ function resolveOutputDir(ctx, options) {
903
+ const explicit = typeof options.outDir === "string" ? options.outDir : void 0;
904
+ return path2.resolve(ctx.cwd, explicit ?? ctx.env.TEXT2SQL_OUT_DIR ?? "./sql");
905
+ }
906
+ var CommandError = class extends Error {
907
+ command;
908
+ exitCode;
909
+ constructor(command, message, exitCode = 1) {
910
+ super(message);
911
+ this.name = "CommandError";
912
+ this.command = command;
913
+ this.exitCode = exitCode;
914
+ }
915
+ };
916
+ function renderCommandError(error, stderr) {
917
+ stderr.write(`sql ${error.command}: ${error.message}
918
+ `);
919
+ }
401
920
  function errorMessage2(error) {
402
921
  return error instanceof Error ? error.message : String(error);
403
922
  }
923
+ var SqlCommand = class {
924
+ options = [];
925
+ helpDisplay;
926
+ fail(message, exitCode = 1) {
927
+ throw new CommandError(this.name, message, exitCode);
928
+ }
929
+ requireAdapterName(text2Sql, db) {
930
+ const available = text2Sql.adapterNames().join(", ") || "(none configured)";
931
+ if (!db) {
932
+ this.fail(
933
+ `missing database name. Usage: sql ${this.name} ${this.usage}. Available: ${available}`
934
+ );
935
+ }
936
+ if (!text2Sql.hasAdapter(db)) {
937
+ this.fail(`unknown database "${db}". Available: ${available}`);
938
+ }
939
+ return db;
940
+ }
941
+ };
942
+ var SqlQueryCommand = class extends SqlCommand {
943
+ args = "<db> <...sql>";
944
+ async execute(ctx, args, options) {
945
+ const [db, sqlParts] = args;
946
+ const name = this.requireAdapterName(ctx.text2Sql, db);
947
+ const sql = (sqlParts ?? []).join(" ").trim();
948
+ if (!sql) this.fail("no query provided");
949
+ try {
950
+ return await this.runQuery(ctx, name, sql, options);
951
+ } catch (error) {
952
+ if (error instanceof CommandError) throw error;
953
+ if (Text2SqlValidationError.isInstance(error)) this.fail(error.message);
954
+ this.fail(errorMessage2(error));
955
+ }
956
+ }
957
+ };
958
+
959
+ // packages/text2sql/src/bin/commands/index.ts
960
+ import { constants, createWriteStream } from "node:fs";
961
+ import {
962
+ mkdir,
963
+ open,
964
+ stat,
965
+ writeFile as writeFile2
966
+ } from "node:fs/promises";
967
+ import * as path3 from "node:path";
968
+ import { finished } from "node:stream/promises";
969
+ import { v7 } from "uuid";
404
970
 
405
971
  // packages/text2sql/src/bin/commands/indexing-formatter.ts
406
972
  var REDUNDANT_MESSAGE_TYPES = /* @__PURE__ */ new Set([
@@ -438,7 +1004,7 @@ var IndexCommand = class extends SqlCommand {
438
1004
  async execute(ctx, args, options) {
439
1005
  const adapterNames = args[0] ?? [];
440
1006
  const requested = options.all ? [] : adapterNames;
441
- const names = this.resolveNames(ctx.adapters, requested);
1007
+ const names = this.resolveNames(ctx.text2Sql, requested);
442
1008
  const verbose = this.resolveVerbose(options.verbose);
443
1009
  const outputDir = resolveOutputDir(ctx, options);
444
1010
  const id = v7();
@@ -456,12 +1022,8 @@ var IndexCommand = class extends SqlCommand {
456
1022
  ctx.stderr
457
1023
  );
458
1024
  try {
459
- const indexer = new AdapterIndexer({
460
- adapters: ctx.adapters,
461
- version: ctx.env.TEXT2SQL_INDEX_VERSION
462
- });
463
- const fragments = await indexer.index({
464
- adapterNames: names,
1025
+ const fragments = await ctx.text2Sql.index({
1026
+ names,
465
1027
  onProgress: createProgressHandler(eventsStream, verbose, ctx.stderr)
466
1028
  });
467
1029
  await writeFile2(fragmentsPath, JSON.stringify(fragments, null, 2));
@@ -479,15 +1041,15 @@ var IndexCommand = class extends SqlCommand {
479
1041
  });
480
1042
  }
481
1043
  } catch (error) {
482
- this.fail(errorMessage(error));
1044
+ this.fail(errorMessage2(error));
483
1045
  }
484
1046
  }
485
- resolveNames(adapters, requested) {
486
- const availableNames = Object.keys(adapters);
1047
+ resolveNames(text2Sql, requested) {
1048
+ const availableNames = text2Sql.adapterNames();
487
1049
  const available = availableNames.join(", ") || "(none configured)";
488
1050
  const names = requested.length === 0 ? availableNames : dedupe(requested);
489
1051
  for (const name of names) {
490
- if (!adapters[name]) {
1052
+ if (!text2Sql.hasAdapter(name)) {
491
1053
  this.fail(`unknown adapter "${name}". Available: ${available}`);
492
1054
  }
493
1055
  }
@@ -592,25 +1154,16 @@ var RunCommand = class extends SqlQueryCommand {
592
1154
  usage = '<db> "SELECT ..." [--out-dir <path>]';
593
1155
  helpDisplay = '<db> "SELECT ..."';
594
1156
  options = [OUT_DIR_OPTION];
595
- async afterValidation(ctx, adapter, sql, options) {
1157
+ async runQuery(ctx, name, sql, options) {
596
1158
  const outputDir = resolveOutputDir(ctx, options);
597
- let rows;
598
- try {
599
- rows = await adapter.execute(sql);
600
- } catch (error) {
601
- this.fail(errorMessage(error));
602
- }
603
- if (!Array.isArray(rows)) {
604
- this.fail("adapter.execute must return an array of rows");
605
- }
1159
+ const { rows, columns } = await ctx.text2Sql.run(name, sql);
606
1160
  const outPath = path4.join(outputDir, `${v72()}.json`);
607
1161
  try {
608
1162
  await mkdir2(outputDir, { recursive: true });
609
1163
  await writeFile3(outPath, JSON.stringify(rows, null, 2));
610
1164
  } catch (error) {
611
- this.fail(errorMessage(error));
1165
+ this.fail(errorMessage2(error));
612
1166
  }
613
- const columns = rows.length > 0 ? Object.keys(rows[0]) : [];
614
1167
  ctx.stdout.write(`results stored in ${outPath}
615
1168
  `);
616
1169
  ctx.stdout.write(`columns: ${columns.join(", ") || "(none)"}
@@ -627,7 +1180,8 @@ var ValidateCommand = class extends SqlQueryCommand {
627
1180
  description = "Validate query syntax against <db>";
628
1181
  usage = '<db> "SELECT ..."';
629
1182
  helpDisplay = '<db> "SELECT ..."';
630
- async afterValidation(ctx) {
1183
+ async runQuery(ctx, name, sql) {
1184
+ await ctx.text2Sql.validate(name, sql);
631
1185
  ctx.stdout.write("valid\n");
632
1186
  return 0;
633
1187
  }
@@ -656,7 +1210,7 @@ async function loadAdapters() {
656
1210
  mod = await import(specifier);
657
1211
  } catch (cause) {
658
1212
  throw new Error(
659
- `TEXT2SQL_ADAPTERS=${target}: failed to import module - ${errorMessage(cause)}`
1213
+ `TEXT2SQL_ADAPTERS=${target}: failed to import module - ${errorMessage2(cause)}`
660
1214
  );
661
1215
  }
662
1216
  const exported = mod.default;
@@ -681,7 +1235,7 @@ async function loadAdapters() {
681
1235
  try {
682
1236
  validateAdapterNames(entries.map(([name]) => name));
683
1237
  } catch (cause) {
684
- throw new Error(`TEXT2SQL_ADAPTERS=${target}: ${errorMessage(cause)}`);
1238
+ throw new Error(`TEXT2SQL_ADAPTERS=${target}: ${errorMessage2(cause)}`);
685
1239
  }
686
1240
  return exported;
687
1241
  }
@@ -745,15 +1299,19 @@ function registerCommand(cli2, command) {
745
1299
  async function runCommand(command, positional, options) {
746
1300
  let ctx;
747
1301
  try {
748
- ctx = {
1302
+ const text2Sql = new Text2Sql({
749
1303
  adapters: await loadAdapters(),
1304
+ version: process.env.TEXT2SQL_INDEX_VERSION
1305
+ });
1306
+ ctx = {
1307
+ text2Sql,
750
1308
  cwd: process.cwd(),
751
1309
  env: process.env,
752
1310
  stdout: process.stdout,
753
1311
  stderr: process.stderr
754
1312
  };
755
1313
  } catch (error) {
756
- process.stderr.write(`${errorMessage(error)}
1314
+ process.stderr.write(`${errorMessage2(error)}
757
1315
  `);
758
1316
  return 2;
759
1317
  }