@deepagents/text2sql 0.8.1 → 0.10.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 +10 -12
- package/dist/index.js.map +4 -4
- package/dist/lib/adapters/groundings/index.js +42 -44
- package/dist/lib/adapters/groundings/index.js.map +2 -2
- package/dist/lib/adapters/groundings/report.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/mysql/index.js +46 -45
- package/dist/lib/adapters/mysql/index.js.map +2 -2
- package/dist/lib/adapters/mysql/info.mysql.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/postgres/index.js +46 -45
- package/dist/lib/adapters/postgres/index.js.map +2 -2
- package/dist/lib/adapters/postgres/info.postgres.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/spreadsheet/index.d.ts +4 -0
- package/dist/lib/adapters/spreadsheet/index.d.ts.map +1 -0
- package/dist/lib/adapters/spreadsheet/parser.d.ts +41 -0
- package/dist/lib/adapters/spreadsheet/parser.d.ts.map +1 -0
- package/dist/lib/adapters/spreadsheet/spreadsheet.d.ts +52 -0
- package/dist/lib/adapters/spreadsheet/spreadsheet.d.ts.map +1 -0
- package/dist/lib/adapters/sqlite/index.js +46 -45
- package/dist/lib/adapters/sqlite/index.js.map +2 -2
- package/dist/lib/adapters/sqlite/info.sqlite.grounding.d.ts.map +1 -1
- package/dist/lib/adapters/sqlserver/index.js +46 -45
- package/dist/lib/adapters/sqlserver/index.js.map +2 -2
- package/dist/lib/adapters/sqlserver/info.sqlserver.grounding.d.ts.map +1 -1
- package/dist/lib/sql.d.ts.map +1 -1
- package/dist/lib/synthesis/extractors/last-query-extractor.d.ts +1 -1
- package/dist/lib/synthesis/index.js.map +1 -1
- package/package.json +12 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"info.postgres.grounding.d.ts","sourceRoot":"","sources":["../../../../src/lib/adapters/postgres/info.postgres.grounding.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EACL,aAAa,EACb,KAAK,mBAAmB,EACzB,MAAM,iCAAiC,CAAC;AAEzC;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,aAAa;;gBAG1C,OAAO,EAAE,OAAO,EAAE,MAAM,GAAE,mBAAwB;cAKrC,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"info.postgres.grounding.d.ts","sourceRoot":"","sources":["../../../../src/lib/adapters/postgres/info.postgres.grounding.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EACL,aAAa,EACb,KAAK,mBAAmB,EACzB,MAAM,iCAAiC,CAAC;AAEzC;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,aAAa;;gBAG1C,OAAO,EAAE,OAAO,EAAE,MAAM,GAAE,mBAAwB;cAKrC,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;CAiB7D"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { columnStats, columnValues, info, rowCount, tables, } from '../sqlite/index.ts';
|
|
2
|
+
export { Spreadsheet, type SpreadsheetOptions } from './spreadsheet.ts';
|
|
3
|
+
export { parseFile, sanitizeIdentifier, sanitizeTableName, type Column, type ColumnType, type ParsedSheet, } from './parser.ts';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/adapters/spreadsheet/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,YAAY,EACZ,IAAI,EACJ,QAAQ,EACR,MAAM,GACP,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAGxE,OAAO,EACL,SAAS,EACT,kBAAkB,EAClB,iBAAiB,EACjB,KAAK,MAAM,EACX,KAAK,UAAU,EACf,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column type for SQLite.
|
|
3
|
+
*/
|
|
4
|
+
export type ColumnType = 'TEXT' | 'INTEGER' | 'REAL';
|
|
5
|
+
/**
|
|
6
|
+
* Column definition with name and inferred type.
|
|
7
|
+
*/
|
|
8
|
+
export interface Column {
|
|
9
|
+
/** Sanitized column name for SQL */
|
|
10
|
+
name: string;
|
|
11
|
+
/** Original column name from spreadsheet (for data access) */
|
|
12
|
+
originalKey: string;
|
|
13
|
+
/** Inferred SQLite type */
|
|
14
|
+
type: ColumnType;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Parsed sheet with table name, columns, and row data.
|
|
18
|
+
*/
|
|
19
|
+
export interface ParsedSheet {
|
|
20
|
+
name: string;
|
|
21
|
+
columns: Column[];
|
|
22
|
+
rows: Record<string, unknown>[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parse an Excel or CSV/TSV file into sheets.
|
|
26
|
+
*
|
|
27
|
+
* - Excel files: each sheet becomes a ParsedSheet
|
|
28
|
+
* - CSV/TSV files: single ParsedSheet with filename as table name
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseFile(filePath: string): ParsedSheet[];
|
|
31
|
+
/**
|
|
32
|
+
* Sanitize a name to be a valid SQL table/column identifier.
|
|
33
|
+
* - Lowercase for consistency
|
|
34
|
+
* - Replace invalid chars with underscores
|
|
35
|
+
* - Ensure it doesn't start with a number
|
|
36
|
+
* - Trim and collapse multiple underscores
|
|
37
|
+
* - Truncate to 64 characters
|
|
38
|
+
*/
|
|
39
|
+
export declare function sanitizeIdentifier(name: string): string;
|
|
40
|
+
export declare const sanitizeTableName: typeof sanitizeIdentifier;
|
|
41
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../../../src/lib/adapters/spreadsheet/parser.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,8DAA8D;IAC9D,WAAW,EAAE,MAAM,CAAC;IACpB,2BAA2B;IAC3B,IAAI,EAAE,UAAU,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;CACjC;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,WAAW,EAAE,CAmDzD;AAWD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAyBvD;AAGD,eAAO,MAAM,iBAAiB,2BAAqB,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { GroundingFn } from '../adapter.ts';
|
|
2
|
+
import { Sqlite } from '../sqlite/sqlite.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Options for creating a Spreadsheet adapter.
|
|
5
|
+
*/
|
|
6
|
+
export interface SpreadsheetOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Path to the spreadsheet file (Excel .xlsx/.xls or CSV/TSV).
|
|
9
|
+
*/
|
|
10
|
+
file: string;
|
|
11
|
+
/**
|
|
12
|
+
* Optional path to persist the SQLite database.
|
|
13
|
+
* If not provided, uses in-memory database (':memory:').
|
|
14
|
+
*/
|
|
15
|
+
database?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Grounding functions to use for schema introspection.
|
|
18
|
+
*/
|
|
19
|
+
grounding: GroundingFn[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Spreadsheet adapter that loads Excel/CSV files into SQLite.
|
|
23
|
+
*
|
|
24
|
+
* This adapter:
|
|
25
|
+
* 1. Parses the spreadsheet file (Excel or CSV/TSV)
|
|
26
|
+
* 2. Creates a SQLite database (in-memory or file-based)
|
|
27
|
+
* 3. Creates tables from sheets and loads data
|
|
28
|
+
* 4. Delegates all SQL operations to the SQLite adapter
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { Spreadsheet, tables, info } from '@deepagents/text2sql/spreadsheet';
|
|
33
|
+
*
|
|
34
|
+
* const adapter = new Spreadsheet({
|
|
35
|
+
* file: './sales.xlsx',
|
|
36
|
+
* grounding: [tables(), info()]
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* const schema = await adapter.introspect();
|
|
40
|
+
* const results = await adapter.execute('SELECT * FROM Customers');
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare class Spreadsheet extends Sqlite {
|
|
44
|
+
#private;
|
|
45
|
+
constructor(options: SpreadsheetOptions);
|
|
46
|
+
/**
|
|
47
|
+
* Close the underlying SQLite database.
|
|
48
|
+
* Call this when done to release resources.
|
|
49
|
+
*/
|
|
50
|
+
close(): void;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=spreadsheet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spreadsheet.d.ts","sourceRoot":"","sources":["../../../../src/lib/adapters/spreadsheet/spreadsheet.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAG7C;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,SAAS,EAAE,WAAW,EAAE,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,WAAY,SAAQ,MAAM;;gBAGzB,OAAO,EAAE,kBAAkB;IAwBvC;;;OAGG;IACH,KAAK,IAAI,IAAI;CAGd"}
|
|
@@ -344,6 +344,47 @@ import {
|
|
|
344
344
|
toState,
|
|
345
345
|
user
|
|
346
346
|
} from "@deepagents/agent";
|
|
347
|
+
var reportAgent = agent({
|
|
348
|
+
name: "db-report-agent",
|
|
349
|
+
model: groq("openai/gpt-oss-20b"),
|
|
350
|
+
prompt: () => dedent`
|
|
351
|
+
<identity>
|
|
352
|
+
You are a database analyst expert. Your job is to understand what
|
|
353
|
+
a database represents and provide business context about it.
|
|
354
|
+
You have READ-ONLY access to the database.
|
|
355
|
+
</identity>
|
|
356
|
+
|
|
357
|
+
<instructions>
|
|
358
|
+
Write a business context that helps another agent answer questions accurately.
|
|
359
|
+
|
|
360
|
+
For EACH table, do queries ONE AT A TIME:
|
|
361
|
+
1. SELECT COUNT(*) to get row count
|
|
362
|
+
2. SELECT * LIMIT 3 to see sample data
|
|
363
|
+
|
|
364
|
+
Then write a report with:
|
|
365
|
+
- What business this database is for
|
|
366
|
+
- For each table: purpose, row count, and example of what the data looks like
|
|
367
|
+
|
|
368
|
+
Include concrete examples like "Track prices are $0.99",
|
|
369
|
+
"Customer names like 'Luís Gonçalves'", etc.
|
|
370
|
+
|
|
371
|
+
Keep it 400-600 words, conversational style.
|
|
372
|
+
</instructions>
|
|
373
|
+
`,
|
|
374
|
+
tools: {
|
|
375
|
+
query_database: tool({
|
|
376
|
+
description: "Execute a SELECT query to explore the database and gather insights.",
|
|
377
|
+
inputSchema: z.object({
|
|
378
|
+
sql: z.string().describe("The SELECT query to execute"),
|
|
379
|
+
purpose: z.string().describe("What insight you are trying to gather with this query")
|
|
380
|
+
}),
|
|
381
|
+
execute: ({ sql }, options) => {
|
|
382
|
+
const state = toState(options);
|
|
383
|
+
return state.adapter.execute(sql);
|
|
384
|
+
}
|
|
385
|
+
})
|
|
386
|
+
}
|
|
387
|
+
});
|
|
347
388
|
var ReportGrounding = class extends AbstractGrounding {
|
|
348
389
|
#adapter;
|
|
349
390
|
#model;
|
|
@@ -372,51 +413,8 @@ var ReportGrounding = class extends AbstractGrounding {
|
|
|
372
413
|
return () => report2;
|
|
373
414
|
}
|
|
374
415
|
async #generateReport() {
|
|
375
|
-
const reportAgent = agent({
|
|
376
|
-
name: "db-report-agent",
|
|
377
|
-
model: this.#model,
|
|
378
|
-
prompt: () => dedent`
|
|
379
|
-
<identity>
|
|
380
|
-
You are a database analyst expert. Your job is to understand what
|
|
381
|
-
a database represents and provide business context about it.
|
|
382
|
-
You have READ-ONLY access to the database.
|
|
383
|
-
</identity>
|
|
384
|
-
|
|
385
|
-
<instructions>
|
|
386
|
-
Write a business context that helps another agent answer questions accurately.
|
|
387
|
-
|
|
388
|
-
For EACH table, do queries ONE AT A TIME:
|
|
389
|
-
1. SELECT COUNT(*) to get row count
|
|
390
|
-
2. SELECT * LIMIT 3 to see sample data
|
|
391
|
-
|
|
392
|
-
Then write a report with:
|
|
393
|
-
- What business this database is for
|
|
394
|
-
- For each table: purpose, row count, and example of what the data looks like
|
|
395
|
-
|
|
396
|
-
Include concrete examples like "Track prices are $0.99",
|
|
397
|
-
"Customer names like 'Luís Gonçalves'", etc.
|
|
398
|
-
|
|
399
|
-
Keep it 400-600 words, conversational style.
|
|
400
|
-
</instructions>
|
|
401
|
-
`,
|
|
402
|
-
tools: {
|
|
403
|
-
query_database: tool({
|
|
404
|
-
description: "Execute a SELECT query to explore the database and gather insights.",
|
|
405
|
-
inputSchema: z.object({
|
|
406
|
-
sql: z.string().describe("The SELECT query to execute"),
|
|
407
|
-
purpose: z.string().describe(
|
|
408
|
-
"What insight you are trying to gather with this query"
|
|
409
|
-
)
|
|
410
|
-
}),
|
|
411
|
-
execute: ({ sql }, options) => {
|
|
412
|
-
const state = toState(options);
|
|
413
|
-
return state.adapter.execute(sql);
|
|
414
|
-
}
|
|
415
|
-
})
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
416
|
const { text } = await generate(
|
|
419
|
-
reportAgent,
|
|
417
|
+
reportAgent.clone({ model: this.#model }),
|
|
420
418
|
[
|
|
421
419
|
user(
|
|
422
420
|
"Please analyze the database and write a contextual report about what this database represents."
|
|
@@ -961,7 +959,10 @@ var SqliteInfoGrounding = class extends InfoGrounding {
|
|
|
961
959
|
);
|
|
962
960
|
return {
|
|
963
961
|
dialect: "sqlite",
|
|
964
|
-
version: rows[0]?.version
|
|
962
|
+
version: rows[0]?.version,
|
|
963
|
+
details: {
|
|
964
|
+
parameterPlaceholder: "?"
|
|
965
|
+
}
|
|
965
966
|
};
|
|
966
967
|
}
|
|
967
968
|
};
|