@deepagents/text2sql 2.2.0 → 2.4.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 +3 -3
- package/dist/index.js +37 -89
- package/dist/index.js.map +3 -3
- package/dist/lib/sql-command.d.ts +7 -3
- package/dist/lib/sql-command.d.ts.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -201,13 +201,13 @@ For in-process or virtual-sandbox usage (without installing the package CLI),
|
|
|
201
201
|
wrap an existing `Text2Sql` instance as a just-bash custom command:
|
|
202
202
|
|
|
203
203
|
```typescript
|
|
204
|
+
import { InMemoryFs, createVirtualSandbox } from '@deepagents/context';
|
|
204
205
|
import {
|
|
205
|
-
Text2Sql,
|
|
206
|
-
createSqlCommand,
|
|
207
206
|
type CreateSqlCommandOptions,
|
|
208
207
|
type CreateSqlCommandResult,
|
|
208
|
+
Text2Sql,
|
|
209
|
+
createSqlCommand,
|
|
209
210
|
} from '@deepagents/text2sql';
|
|
210
|
-
import { InMemoryFs, createVirtualSandbox } from '@deepagents/context';
|
|
211
211
|
|
|
212
212
|
const text2sql = new Text2Sql({ model, adapters: { main: adapter } });
|
|
213
213
|
|
package/dist/index.js
CHANGED
|
@@ -5530,6 +5530,7 @@ import { posix as posix5 } from "node:path";
|
|
|
5530
5530
|
import { parseArgs } from "node:util";
|
|
5531
5531
|
import { v7 } from "uuid";
|
|
5532
5532
|
import {
|
|
5533
|
+
BashException as BashException2,
|
|
5533
5534
|
buildSubcommandRepair as buildSubcommandRepair2,
|
|
5534
5535
|
defineSubcommandGroup,
|
|
5535
5536
|
repairQuotedArg as repairQuotedArg2,
|
|
@@ -5650,12 +5651,22 @@ var Text2Sql = class {
|
|
|
5650
5651
|
|
|
5651
5652
|
// packages/text2sql/src/lib/sql-command.ts
|
|
5652
5653
|
var SQL_VALIDATE_REMINDER2 = 'Always run `sql validate <db> "..."` before `sql run <db> "..."` to catch syntax errors early.';
|
|
5653
|
-
var SqlCommandError = class extends
|
|
5654
|
-
|
|
5655
|
-
|
|
5654
|
+
var SqlCommandError = class extends BashException2 {
|
|
5655
|
+
#subcommand;
|
|
5656
|
+
#exitCode;
|
|
5657
|
+
constructor(subcommand, message, exitCode = 1) {
|
|
5656
5658
|
super(message);
|
|
5657
5659
|
this.name = "SqlCommandError";
|
|
5658
|
-
this
|
|
5660
|
+
this.#subcommand = subcommand;
|
|
5661
|
+
this.#exitCode = exitCode;
|
|
5662
|
+
}
|
|
5663
|
+
format() {
|
|
5664
|
+
return {
|
|
5665
|
+
stdout: "",
|
|
5666
|
+
stderr: `sql ${this.#subcommand}: ${this.message}
|
|
5667
|
+
`,
|
|
5668
|
+
exitCode: this.#exitCode
|
|
5669
|
+
};
|
|
5659
5670
|
}
|
|
5660
5671
|
};
|
|
5661
5672
|
function createSqlCommand(text2Sql, options = {}) {
|
|
@@ -5672,11 +5683,6 @@ function createSqlCommand(text2Sql, options = {}) {
|
|
|
5672
5683
|
description: "Validate query syntax against <db>",
|
|
5673
5684
|
repair: repairDbNameAndQuotedArg,
|
|
5674
5685
|
handler: (args) => handleValidate(text2Sql, args)
|
|
5675
|
-
},
|
|
5676
|
-
index: {
|
|
5677
|
-
usage: "index [adapter ...]",
|
|
5678
|
-
description: "Index adapter schemas and write context artifacts",
|
|
5679
|
-
handler: (args, ctx) => handleIndex(text2Sql, defaultOutputDir, args, ctx)
|
|
5680
5686
|
}
|
|
5681
5687
|
};
|
|
5682
5688
|
return {
|
|
@@ -5684,27 +5690,22 @@ function createSqlCommand(text2Sql, options = {}) {
|
|
|
5684
5690
|
repair: buildSubcommandRepair2("sql", subcommands)
|
|
5685
5691
|
};
|
|
5686
5692
|
}
|
|
5687
|
-
function parseFlags(args) {
|
|
5688
|
-
let parsed;
|
|
5693
|
+
function parseFlags(subcommand, args) {
|
|
5689
5694
|
try {
|
|
5690
|
-
parsed = parseArgs({
|
|
5695
|
+
const parsed = parseArgs({
|
|
5691
5696
|
args,
|
|
5692
|
-
options: {
|
|
5693
|
-
"out-dir": { type: "string" },
|
|
5694
|
-
all: { type: "boolean" }
|
|
5695
|
-
},
|
|
5697
|
+
options: { "out-dir": { type: "string" } },
|
|
5696
5698
|
allowPositionals: true,
|
|
5697
5699
|
strict: true
|
|
5698
5700
|
});
|
|
5701
|
+
return {
|
|
5702
|
+
positional: parsed.positionals,
|
|
5703
|
+
outDir: parsed.values["out-dir"]
|
|
5704
|
+
};
|
|
5699
5705
|
} catch (error) {
|
|
5700
5706
|
const message = error instanceof Error ? error.message : String(error);
|
|
5701
|
-
throw new SqlCommandError(
|
|
5707
|
+
throw new SqlCommandError(subcommand, message);
|
|
5702
5708
|
}
|
|
5703
|
-
return {
|
|
5704
|
-
positional: parsed.positionals,
|
|
5705
|
-
outDir: parsed.values["out-dir"],
|
|
5706
|
-
all: parsed.values.all === true
|
|
5707
|
-
};
|
|
5708
5709
|
}
|
|
5709
5710
|
function resolveOutputDir(ctx, flags, fallback) {
|
|
5710
5711
|
const envOutDir = ctx.env.get("TEXT2SQL_OUT_DIR");
|
|
@@ -5713,10 +5714,10 @@ function resolveOutputDir(ctx, flags, fallback) {
|
|
|
5713
5714
|
}
|
|
5714
5715
|
async function handleRun(text2Sql, defaultOutputDir, args, ctx) {
|
|
5715
5716
|
return runHandler("run", async () => {
|
|
5716
|
-
const flags = parseFlags(args);
|
|
5717
|
+
const flags = parseFlags("run", args);
|
|
5717
5718
|
const meta = useBashMeta2();
|
|
5718
5719
|
meta?.setReminder(SQL_VALIDATE_REMINDER2);
|
|
5719
|
-
const { name, sql } = takeDbAndSql(flags.positional,
|
|
5720
|
+
const { name, sql } = takeDbAndSql("run", flags.positional, text2Sql);
|
|
5720
5721
|
const outputDir = resolveOutputDir(ctx, flags, defaultOutputDir);
|
|
5721
5722
|
const { rows, columns } = await text2Sql.run(name, sql);
|
|
5722
5723
|
const outPath = posix5.join(outputDir, `${v7()}.json`);
|
|
@@ -5734,87 +5735,42 @@ rows: ${rows.length}
|
|
|
5734
5735
|
}
|
|
5735
5736
|
async function handleValidate(text2Sql, args) {
|
|
5736
5737
|
return runHandler("validate", async () => {
|
|
5737
|
-
const flags = parseFlags(args);
|
|
5738
|
-
const { name, sql } = takeDbAndSql(flags.positional,
|
|
5738
|
+
const flags = parseFlags("validate", args);
|
|
5739
|
+
const { name, sql } = takeDbAndSql("validate", flags.positional, text2Sql);
|
|
5739
5740
|
await text2Sql.validate(name, sql);
|
|
5740
5741
|
return { stdout: "valid\n", stderr: "", exitCode: 0 };
|
|
5741
5742
|
});
|
|
5742
5743
|
}
|
|
5743
|
-
async function
|
|
5744
|
-
return runHandler("index", async () => {
|
|
5745
|
-
const flags = parseFlags(args);
|
|
5746
|
-
const available = text2Sql.adapterNames();
|
|
5747
|
-
const requested = flags.all ? [] : dedupe(flags.positional);
|
|
5748
|
-
const names = requested.length === 0 ? available : requested;
|
|
5749
|
-
for (const name of names) {
|
|
5750
|
-
if (!text2Sql.hasAdapter(name)) {
|
|
5751
|
-
throw new SqlCommandError(
|
|
5752
|
-
`unknown adapter "${name}". Available: ${available.join(", ")}`,
|
|
5753
|
-
1
|
|
5754
|
-
);
|
|
5755
|
-
}
|
|
5756
|
-
}
|
|
5757
|
-
const outputDir = resolveOutputDir(ctx, flags, defaultOutputDir);
|
|
5758
|
-
const fragments = await text2Sql.index({ names });
|
|
5759
|
-
const fragmentsPath = posix5.join(outputDir, `index-${v7()}.json`);
|
|
5760
|
-
await ctx.fs.mkdir(outputDir, { recursive: true });
|
|
5761
|
-
await ctx.fs.writeFile(fragmentsPath, JSON.stringify(fragments, null, 2));
|
|
5762
|
-
const manifest = {
|
|
5763
|
-
fragmentsPath,
|
|
5764
|
-
eventsPath: null,
|
|
5765
|
-
adapters: names,
|
|
5766
|
-
fragments: countSchemaFragments(fragments)
|
|
5767
|
-
};
|
|
5768
|
-
return {
|
|
5769
|
-
stdout: JSON.stringify(manifest, null, 2) + "\n",
|
|
5770
|
-
stderr: "",
|
|
5771
|
-
exitCode: 0
|
|
5772
|
-
};
|
|
5773
|
-
});
|
|
5774
|
-
}
|
|
5775
|
-
async function runHandler(name, fn) {
|
|
5744
|
+
async function runHandler(subcommand, fn) {
|
|
5776
5745
|
try {
|
|
5777
5746
|
return await fn();
|
|
5778
5747
|
} catch (error) {
|
|
5779
|
-
if (error instanceof
|
|
5780
|
-
return {
|
|
5781
|
-
stdout: "",
|
|
5782
|
-
stderr: `sql ${name}: ${error.message}
|
|
5783
|
-
`,
|
|
5784
|
-
exitCode: error.exitCode
|
|
5785
|
-
};
|
|
5786
|
-
}
|
|
5748
|
+
if (error instanceof BashException2) return error.format();
|
|
5787
5749
|
if (Text2SqlValidationError.isInstance(error) || Text2SqlUnknownAdapterError.isInstance(error)) {
|
|
5788
|
-
return
|
|
5789
|
-
stdout: "",
|
|
5790
|
-
stderr: `sql ${name}: ${error.message}
|
|
5791
|
-
`,
|
|
5792
|
-
exitCode: 1
|
|
5793
|
-
};
|
|
5750
|
+
return new SqlCommandError(subcommand, error.message).format();
|
|
5794
5751
|
}
|
|
5795
5752
|
const message = error instanceof Error ? error.message : String(error);
|
|
5796
|
-
return
|
|
5797
|
-
`, exitCode: 1 };
|
|
5753
|
+
return new SqlCommandError(subcommand, message).format();
|
|
5798
5754
|
}
|
|
5799
5755
|
}
|
|
5800
|
-
function takeDbAndSql(
|
|
5756
|
+
function takeDbAndSql(subcommand, positional, text2Sql) {
|
|
5801
5757
|
const [db, ...rest] = positional;
|
|
5802
5758
|
const available = text2Sql.adapterNames().join(", ");
|
|
5803
5759
|
if (!db) {
|
|
5804
5760
|
throw new SqlCommandError(
|
|
5805
|
-
|
|
5806
|
-
|
|
5761
|
+
subcommand,
|
|
5762
|
+
`missing database name. Usage: sql ${subcommand} <db> "SELECT ...". Available: ${available}`
|
|
5807
5763
|
);
|
|
5808
5764
|
}
|
|
5809
5765
|
if (!text2Sql.hasAdapter(db)) {
|
|
5810
5766
|
throw new SqlCommandError(
|
|
5811
|
-
|
|
5812
|
-
|
|
5767
|
+
subcommand,
|
|
5768
|
+
`unknown database "${db}". Available: ${available}`
|
|
5813
5769
|
);
|
|
5814
5770
|
}
|
|
5815
5771
|
const sql = rest.join(" ").trim();
|
|
5816
5772
|
if (!sql) {
|
|
5817
|
-
throw new SqlCommandError("no query provided"
|
|
5773
|
+
throw new SqlCommandError(subcommand, "no query provided");
|
|
5818
5774
|
}
|
|
5819
5775
|
return { name: db, sql };
|
|
5820
5776
|
}
|
|
@@ -5829,14 +5785,6 @@ function repairDbNameAndQuotedArg(rawArgs) {
|
|
|
5829
5785
|
if (repaired == null) return null;
|
|
5830
5786
|
return `${dbName} ${repaired}`;
|
|
5831
5787
|
}
|
|
5832
|
-
function dedupe(values) {
|
|
5833
|
-
return [...new Set(values)];
|
|
5834
|
-
}
|
|
5835
|
-
function countSchemaFragments(fragments) {
|
|
5836
|
-
return fragments.reduce((count, adapterFragment) => {
|
|
5837
|
-
return Array.isArray(adapterFragment.data) ? count + adapterFragment.data.length : count + 1;
|
|
5838
|
-
}, 0);
|
|
5839
|
-
}
|
|
5840
5788
|
export {
|
|
5841
5789
|
Adapter,
|
|
5842
5790
|
AdapterIndexer,
|