@deepagents/text2sql 0.15.1 → 0.16.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/index.js +82 -40
- package/dist/index.js.map +3 -3
- package/dist/lib/adapters/adapter.d.ts +2 -0
- package/dist/lib/adapters/adapter.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/bigquery.d.ts +1 -0
- package/dist/lib/adapters/bigquery/bigquery.d.ts.map +1 -1
- package/dist/lib/adapters/bigquery/index.js +11 -0
- package/dist/lib/adapters/bigquery/index.js.map +3 -3
- package/dist/lib/adapters/mysql/index.js +11 -0
- package/dist/lib/adapters/mysql/index.js.map +3 -3
- package/dist/lib/adapters/mysql/mysql.d.ts +1 -0
- package/dist/lib/adapters/mysql/mysql.d.ts.map +1 -1
- package/dist/lib/adapters/postgres/index.js +11 -0
- package/dist/lib/adapters/postgres/index.js.map +3 -3
- package/dist/lib/adapters/postgres/postgres.d.ts +1 -0
- package/dist/lib/adapters/postgres/postgres.d.ts.map +1 -1
- package/dist/lib/adapters/spreadsheet/index.js +11 -0
- package/dist/lib/adapters/spreadsheet/index.js.map +3 -3
- package/dist/lib/adapters/sqlite/index.js +11 -0
- package/dist/lib/adapters/sqlite/index.js.map +3 -3
- package/dist/lib/adapters/sqlite/sqlite.d.ts +1 -0
- package/dist/lib/adapters/sqlite/sqlite.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/index.js +11 -0
- package/dist/lib/adapters/sqlserver/index.js.map +3 -3
- package/dist/lib/adapters/sqlserver/sqlserver.d.ts +1 -0
- package/dist/lib/adapters/sqlserver/sqlserver.d.ts.map +1 -1
- package/dist/lib/agents/result-tools.d.ts.map +1 -1
- package/dist/lib/agents/sql.agent.d.ts.map +1 -1
- package/dist/lib/fs/mssql/mssql-fs.d.ts +1 -0
- package/dist/lib/fs/mssql/mssql-fs.d.ts.map +1 -1
- package/dist/lib/sql.d.ts +1 -1
- package/dist/lib/sql.d.ts.map +1 -1
- package/dist/lib/synthesis/index.js +1 -1
- package/dist/lib/synthesis/index.js.map +2 -2
- package/package.json +12 -11
package/dist/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// packages/text2sql/src/lib/adapters/adapter.ts
|
|
2
|
+
import { format as formatSql } from "sql-formatter";
|
|
3
|
+
|
|
1
4
|
// packages/text2sql/src/lib/fragments/schema.ts
|
|
2
5
|
function dialectInfo(input) {
|
|
3
6
|
return {
|
|
@@ -265,6 +268,13 @@ var Adapter = class {
|
|
|
265
268
|
cardinality
|
|
266
269
|
});
|
|
267
270
|
}
|
|
271
|
+
format(sql) {
|
|
272
|
+
try {
|
|
273
|
+
return formatSql(sql, { language: this.formatterLanguage });
|
|
274
|
+
} catch {
|
|
275
|
+
return sql;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
268
278
|
/**
|
|
269
279
|
* Convert unknown database value to number.
|
|
270
280
|
* Handles number, bigint, and string types.
|
|
@@ -546,15 +556,15 @@ function createSqlCommand(adapter) {
|
|
|
546
556
|
usage: 'run "SELECT ..."',
|
|
547
557
|
description: "Execute query and store results",
|
|
548
558
|
handler: async (args, ctx) => {
|
|
549
|
-
const
|
|
550
|
-
if (!
|
|
559
|
+
const rawQuery = args.join(" ").trim();
|
|
560
|
+
if (!rawQuery) {
|
|
551
561
|
return {
|
|
552
562
|
stdout: "",
|
|
553
563
|
stderr: "sql run: no query provided",
|
|
554
564
|
exitCode: 1
|
|
555
565
|
};
|
|
556
566
|
}
|
|
557
|
-
const validation = validateReadOnly(
|
|
567
|
+
const validation = validateReadOnly(rawQuery);
|
|
558
568
|
if (!validation.valid) {
|
|
559
569
|
return {
|
|
560
570
|
stdout: "",
|
|
@@ -562,6 +572,7 @@ function createSqlCommand(adapter) {
|
|
|
562
572
|
exitCode: 1
|
|
563
573
|
};
|
|
564
574
|
}
|
|
575
|
+
const query = adapter.format(rawQuery);
|
|
565
576
|
const syntaxError = await adapter.validate(query);
|
|
566
577
|
if (syntaxError) {
|
|
567
578
|
return {
|
|
@@ -601,15 +612,15 @@ function createSqlCommand(adapter) {
|
|
|
601
612
|
usage: 'validate "SELECT ..."',
|
|
602
613
|
description: "Validate query syntax",
|
|
603
614
|
handler: async (args) => {
|
|
604
|
-
const
|
|
605
|
-
if (!
|
|
615
|
+
const rawQuery = args.join(" ").trim();
|
|
616
|
+
if (!rawQuery) {
|
|
606
617
|
return {
|
|
607
618
|
stdout: "",
|
|
608
619
|
stderr: "sql validate: no query provided",
|
|
609
620
|
exitCode: 1
|
|
610
621
|
};
|
|
611
622
|
}
|
|
612
|
-
const validation = validateReadOnly(
|
|
623
|
+
const validation = validateReadOnly(rawQuery);
|
|
613
624
|
if (!validation.valid) {
|
|
614
625
|
return {
|
|
615
626
|
stdout: "",
|
|
@@ -617,6 +628,7 @@ function createSqlCommand(adapter) {
|
|
|
617
628
|
exitCode: 1
|
|
618
629
|
};
|
|
619
630
|
}
|
|
631
|
+
const query = adapter.format(rawQuery);
|
|
620
632
|
const syntaxError = await adapter.validate(query);
|
|
621
633
|
if (syntaxError) {
|
|
622
634
|
return {
|
|
@@ -798,7 +810,7 @@ async function toSql(options) {
|
|
|
798
810
|
if ("error" in output) {
|
|
799
811
|
throw new UnanswerableSQLError(output.error);
|
|
800
812
|
}
|
|
801
|
-
const sql = extractSql(output.sql);
|
|
813
|
+
const sql = options.adapter.format(extractSql(output.sql));
|
|
802
814
|
const validationError = await options.adapter.validate(sql);
|
|
803
815
|
if (validationError) {
|
|
804
816
|
throw new SQLValidationError(validationError);
|
|
@@ -1856,7 +1868,7 @@ var MssqlFs = class _MssqlFs {
|
|
|
1856
1868
|
#root;
|
|
1857
1869
|
#schema;
|
|
1858
1870
|
#ownsPool;
|
|
1859
|
-
#
|
|
1871
|
+
#isInitialized = false;
|
|
1860
1872
|
constructor(options) {
|
|
1861
1873
|
this.#chunkSize = options.chunkSize ?? 1024 * 1024;
|
|
1862
1874
|
const schema = options.schema ?? "dbo";
|
|
@@ -1874,7 +1886,6 @@ var MssqlFs = class _MssqlFs {
|
|
|
1874
1886
|
this.#pool = new mssql.ConnectionPool(options.pool);
|
|
1875
1887
|
this.#ownsPool = true;
|
|
1876
1888
|
}
|
|
1877
|
-
this.#initialized = this.#initialize();
|
|
1878
1889
|
}
|
|
1879
1890
|
static #requireMssql() {
|
|
1880
1891
|
try {
|
|
@@ -1889,7 +1900,7 @@ var MssqlFs = class _MssqlFs {
|
|
|
1889
1900
|
#t(name) {
|
|
1890
1901
|
return `[${this.#schema}].[${name}]`;
|
|
1891
1902
|
}
|
|
1892
|
-
async
|
|
1903
|
+
async initialize() {
|
|
1893
1904
|
if (this.#ownsPool) {
|
|
1894
1905
|
await this.#pool.connect();
|
|
1895
1906
|
}
|
|
@@ -1931,9 +1942,14 @@ var MssqlFs = class _MssqlFs {
|
|
|
1931
1942
|
);
|
|
1932
1943
|
}
|
|
1933
1944
|
}
|
|
1945
|
+
this.#isInitialized = true;
|
|
1934
1946
|
}
|
|
1935
|
-
|
|
1936
|
-
|
|
1947
|
+
#ensureInitialized() {
|
|
1948
|
+
if (!this.#isInitialized) {
|
|
1949
|
+
throw new Error(
|
|
1950
|
+
"MssqlFs not initialized. Call await fs.initialize() after construction."
|
|
1951
|
+
);
|
|
1952
|
+
}
|
|
1937
1953
|
}
|
|
1938
1954
|
async #createParentDirs(p) {
|
|
1939
1955
|
const segments = p.split("/").filter(Boolean);
|
|
@@ -1969,15 +1985,15 @@ var MssqlFs = class _MssqlFs {
|
|
|
1969
1985
|
return result.rowsAffected[0] ?? 0;
|
|
1970
1986
|
}
|
|
1971
1987
|
async #query(sql, params) {
|
|
1972
|
-
|
|
1988
|
+
this.#ensureInitialized();
|
|
1973
1989
|
return this.#rawQuery(sql, params);
|
|
1974
1990
|
}
|
|
1975
1991
|
async #exec(sql, params) {
|
|
1976
|
-
|
|
1992
|
+
this.#ensureInitialized();
|
|
1977
1993
|
return this.#rawExec(sql, params);
|
|
1978
1994
|
}
|
|
1979
1995
|
async #useTransaction(fn) {
|
|
1980
|
-
|
|
1996
|
+
this.#ensureInitialized();
|
|
1981
1997
|
const mssql = _MssqlFs.#requireMssql();
|
|
1982
1998
|
const transaction = new mssql.Transaction(this.#pool);
|
|
1983
1999
|
try {
|
|
@@ -2109,10 +2125,6 @@ var MssqlFs = class _MssqlFs {
|
|
|
2109
2125
|
return new Uint8Array(Buffer.from(content, enc));
|
|
2110
2126
|
}
|
|
2111
2127
|
async close() {
|
|
2112
|
-
try {
|
|
2113
|
-
await this.#initialized;
|
|
2114
|
-
} catch {
|
|
2115
|
-
}
|
|
2116
2128
|
if (this.#ownsPool) {
|
|
2117
2129
|
await this.#pool.close();
|
|
2118
2130
|
}
|
|
@@ -2242,9 +2254,9 @@ var MssqlFs = class _MssqlFs {
|
|
|
2242
2254
|
isFile: entry.type === "file",
|
|
2243
2255
|
isDirectory: entry.type === "directory",
|
|
2244
2256
|
isSymbolicLink: false,
|
|
2245
|
-
mode: entry.mode,
|
|
2246
|
-
size: entry.size,
|
|
2247
|
-
mtime: new Date(entry.mtime)
|
|
2257
|
+
mode: Number(entry.mode),
|
|
2258
|
+
size: Number(entry.size),
|
|
2259
|
+
mtime: new Date(Number(entry.mtime))
|
|
2248
2260
|
};
|
|
2249
2261
|
}
|
|
2250
2262
|
async lstat(filePath) {
|
|
@@ -2262,9 +2274,9 @@ var MssqlFs = class _MssqlFs {
|
|
|
2262
2274
|
isFile: entry.type === "file",
|
|
2263
2275
|
isDirectory: entry.type === "directory",
|
|
2264
2276
|
isSymbolicLink: entry.type === "symlink",
|
|
2265
|
-
mode: entry.mode,
|
|
2266
|
-
size: entry.size,
|
|
2267
|
-
mtime: new Date(entry.mtime)
|
|
2277
|
+
mode: Number(entry.mode),
|
|
2278
|
+
size: Number(entry.size),
|
|
2279
|
+
mtime: new Date(Number(entry.mtime))
|
|
2268
2280
|
};
|
|
2269
2281
|
}
|
|
2270
2282
|
async mkdir(dirPath, options) {
|
|
@@ -2961,7 +2973,7 @@ function reasoningFramework() {
|
|
|
2961
2973
|
"You are a very strong reasoner and planner. Use these critical instructions to structure your plans, thoughts, and responses."
|
|
2962
2974
|
),
|
|
2963
2975
|
fragment2(
|
|
2964
|
-
"
|
|
2976
|
+
"meta-cognitive-reasoning-framework",
|
|
2965
2977
|
hint2(
|
|
2966
2978
|
"Before taking any action (either tool calls *or* responses to the user), you must proactively, methodically, and independently plan and reason about:"
|
|
2967
2979
|
),
|
|
@@ -3078,7 +3090,7 @@ function guidelines(options = {}) {
|
|
|
3078
3090
|
...reasoningFramework(),
|
|
3079
3091
|
// Prerequisite policies (must do X before Y)
|
|
3080
3092
|
fragment2(
|
|
3081
|
-
"
|
|
3093
|
+
"prerequisite_policies",
|
|
3082
3094
|
policy({
|
|
3083
3095
|
rule: "YOU MUST inspect schema structure and available tables",
|
|
3084
3096
|
before: "generating ANY SQL query",
|
|
@@ -3102,7 +3114,7 @@ function guidelines(options = {}) {
|
|
|
3102
3114
|
),
|
|
3103
3115
|
// Few-shot: Applying reasoning principles
|
|
3104
3116
|
fragment2(
|
|
3105
|
-
"
|
|
3117
|
+
"reasoning-examples",
|
|
3106
3118
|
example({
|
|
3107
3119
|
question: "Show me sales last month",
|
|
3108
3120
|
answer: `Applying Principle 1 (Logical dependencies):
|
|
@@ -3142,7 +3154,7 @@ Action: Ask user: "Top by what metric\u2014total revenue, number of orders, or m
|
|
|
3142
3154
|
),
|
|
3143
3155
|
// Schema adherence - consolidated into clear rules
|
|
3144
3156
|
fragment2(
|
|
3145
|
-
"
|
|
3157
|
+
"schema_adherence",
|
|
3146
3158
|
hint2(
|
|
3147
3159
|
"Use only tables and columns from the schema. For unspecified columns, use SELECT *. When showing related items, include IDs and requested details."
|
|
3148
3160
|
),
|
|
@@ -3346,6 +3358,7 @@ import {
|
|
|
3346
3358
|
InvalidToolInputError,
|
|
3347
3359
|
NoSuchToolError,
|
|
3348
3360
|
ToolCallRepairError,
|
|
3361
|
+
createUIMessageStream,
|
|
3349
3362
|
generateId
|
|
3350
3363
|
} from "ai";
|
|
3351
3364
|
import "just-bash";
|
|
@@ -3436,6 +3449,7 @@ var Text2Sql = class {
|
|
|
3436
3449
|
skillMounts,
|
|
3437
3450
|
filesystem: trackedFs
|
|
3438
3451
|
});
|
|
3452
|
+
const assistantMsgId = generateId();
|
|
3439
3453
|
const chatAgent = agent2({
|
|
3440
3454
|
name: "text2sql",
|
|
3441
3455
|
model: this.#config.model,
|
|
@@ -3451,26 +3465,54 @@ var Text2Sql = class {
|
|
|
3451
3465
|
{},
|
|
3452
3466
|
{ transform: this.#config.transform }
|
|
3453
3467
|
);
|
|
3454
|
-
|
|
3468
|
+
const uiStream = result.toUIMessageStream({
|
|
3455
3469
|
onError: (error) => this.#formatError(error),
|
|
3456
3470
|
sendStart: true,
|
|
3457
3471
|
sendFinish: true,
|
|
3458
3472
|
sendReasoning: true,
|
|
3459
3473
|
sendSources: true,
|
|
3460
3474
|
originalMessages: messages,
|
|
3461
|
-
generateMessageId:
|
|
3475
|
+
generateMessageId: () => assistantMsgId,
|
|
3476
|
+
messageMetadata: ({ part }) => {
|
|
3477
|
+
if (part.type === "finish-step") {
|
|
3478
|
+
return {
|
|
3479
|
+
finishReason: part.finishReason,
|
|
3480
|
+
usage: part.usage
|
|
3481
|
+
};
|
|
3482
|
+
}
|
|
3483
|
+
if (part.type === "finish") {
|
|
3484
|
+
return {
|
|
3485
|
+
finishReason: part.finishReason,
|
|
3486
|
+
totalUsage: part.totalUsage
|
|
3487
|
+
};
|
|
3488
|
+
}
|
|
3489
|
+
return void 0;
|
|
3490
|
+
}
|
|
3491
|
+
});
|
|
3492
|
+
return createUIMessageStream({
|
|
3493
|
+
originalMessages: messages,
|
|
3494
|
+
generateId: () => assistantMsgId,
|
|
3495
|
+
onStepFinish: async ({ responseMessage }) => {
|
|
3496
|
+
context.set(assistant({ ...responseMessage, id: assistantMsgId }));
|
|
3497
|
+
await context.save({ branch: false });
|
|
3498
|
+
},
|
|
3462
3499
|
onFinish: async ({ responseMessage }) => {
|
|
3463
3500
|
const createdFiles = trackedFs.getCreatedFiles();
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3501
|
+
context.set(
|
|
3502
|
+
assistant({
|
|
3503
|
+
...responseMessage,
|
|
3504
|
+
id: assistantMsgId,
|
|
3505
|
+
metadata: {
|
|
3506
|
+
...responseMessage.metadata ?? {},
|
|
3507
|
+
createdFiles
|
|
3508
|
+
}
|
|
3509
|
+
})
|
|
3510
|
+
);
|
|
3511
|
+
await context.save({ branch: false });
|
|
3473
3512
|
await context.trackUsage(await result.totalUsage);
|
|
3513
|
+
},
|
|
3514
|
+
execute: async ({ writer }) => {
|
|
3515
|
+
writer.merge(uiStream);
|
|
3474
3516
|
}
|
|
3475
3517
|
});
|
|
3476
3518
|
}
|