@reasoningco/infer 0.0.2
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/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/catalog.d.ts +39 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +249 -0
- package/dist/catalog.js.map +1 -0
- package/dist/connectors.d.ts +63 -0
- package/dist/connectors.d.ts.map +1 -0
- package/dist/connectors.js +263 -0
- package/dist/connectors.js.map +1 -0
- package/dist/dates.d.ts +35 -0
- package/dist/dates.d.ts.map +1 -0
- package/dist/dates.js +293 -0
- package/dist/dates.js.map +1 -0
- package/dist/entity-resolver.d.ts +29 -0
- package/dist/entity-resolver.d.ts.map +1 -0
- package/dist/entity-resolver.js +91 -0
- package/dist/entity-resolver.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +28 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +240 -0
- package/dist/index.js.map +1 -0
- package/dist/llm.d.ts +50 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +311 -0
- package/dist/llm.js.map +1 -0
- package/dist/query-validator.d.ts +6 -0
- package/dist/query-validator.d.ts.map +1 -0
- package/dist/query-validator.js +136 -0
- package/dist/query-validator.js.map +1 -0
- package/dist/schemas.d.ts +170 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +44 -0
- package/dist/schemas.js.map +1 -0
- package/dist/security.d.ts +9 -0
- package/dist/security.d.ts.map +1 -0
- package/dist/security.js +97 -0
- package/dist/security.js.map +1 -0
- package/dist/sql-builder.d.ts +5 -0
- package/dist/sql-builder.d.ts.map +1 -0
- package/dist/sql-builder.js +121 -0
- package/dist/sql-builder.js.map +1 -0
- package/dist/sql-validator.d.ts +8 -0
- package/dist/sql-validator.d.ts.map +1 -0
- package/dist/sql-validator.js +160 -0
- package/dist/sql-validator.js.map +1 -0
- package/dist/testing.d.ts +22 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +78 -0
- package/dist/testing.js.map +1 -0
- package/dist/types.d.ts +214 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +27 -0
- package/dist/types.js.map +1 -0
- package/package.json +72 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { InferError } from './errors.js';
|
|
2
|
+
export class PostgresConnector {
|
|
3
|
+
pool; // pg.Pool - lazy loaded
|
|
4
|
+
config;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
// Browser environment guard
|
|
7
|
+
if (typeof window !== 'undefined') {
|
|
8
|
+
throw new Error('PostgresConnector cannot run in a browser. Use HTTPConnector or a server-side API proxy.');
|
|
9
|
+
}
|
|
10
|
+
this.config = config;
|
|
11
|
+
}
|
|
12
|
+
async getPool() {
|
|
13
|
+
if (!this.pool) {
|
|
14
|
+
try {
|
|
15
|
+
// Dynamic import - pg is an optional peer dependency
|
|
16
|
+
const pg = await (Function('return import("pg")')());
|
|
17
|
+
const Pool = pg.default?.Pool || pg.Pool;
|
|
18
|
+
this.pool = new Pool({
|
|
19
|
+
connectionString: this.config.connectionString,
|
|
20
|
+
host: this.config.host,
|
|
21
|
+
port: this.config.port,
|
|
22
|
+
database: this.config.database,
|
|
23
|
+
user: this.config.user,
|
|
24
|
+
password: this.config.password,
|
|
25
|
+
ssl: this.config.ssl,
|
|
26
|
+
max: this.config.max ?? 10,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
throw new InferError('CONNECTION_ERROR', 'executor', 'pg package is not installed. Run: npm install pg');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return this.pool;
|
|
34
|
+
}
|
|
35
|
+
async execute(sql, params) {
|
|
36
|
+
const pool = await this.getPool();
|
|
37
|
+
const client = await pool.connect();
|
|
38
|
+
try {
|
|
39
|
+
// Secure execution: read-only transaction with timeout
|
|
40
|
+
await client.query('BEGIN');
|
|
41
|
+
await client.query('SET TRANSACTION READ ONLY');
|
|
42
|
+
if (this.config.statementTimeout) {
|
|
43
|
+
const timeout = String(this.config.statementTimeout);
|
|
44
|
+
if (!/^\d+(ms|s|min)?$/.test(timeout)) {
|
|
45
|
+
throw new Error(`Invalid statementTimeout value: ${timeout}`);
|
|
46
|
+
}
|
|
47
|
+
await client.query(`SET LOCAL statement_timeout = '${timeout}'`);
|
|
48
|
+
}
|
|
49
|
+
const result = await client.query(sql, params);
|
|
50
|
+
await client.query('COMMIT');
|
|
51
|
+
const columns = result.fields?.map((f) => ({
|
|
52
|
+
name: f.name,
|
|
53
|
+
type: pgTypeToColumnType(f.dataTypeID),
|
|
54
|
+
})) ?? [];
|
|
55
|
+
return { rows: result.rows, columns };
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
await client.query('ROLLBACK').catch(() => { });
|
|
59
|
+
if (error?.code === '57014') {
|
|
60
|
+
throw new InferError('QUERY_TIMEOUT', 'executor', 'Statement timeout exceeded');
|
|
61
|
+
}
|
|
62
|
+
if (error instanceof InferError)
|
|
63
|
+
throw error;
|
|
64
|
+
throw new InferError('QUERY_ERROR', 'executor', 'Database query failed');
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
client.release();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async introspect() {
|
|
71
|
+
const pool = await this.getPool();
|
|
72
|
+
const client = await pool.connect();
|
|
73
|
+
try {
|
|
74
|
+
// Phase 1: Structure
|
|
75
|
+
const structureQuery = `
|
|
76
|
+
SELECT
|
|
77
|
+
n.nspname AS schema_name, c.relname AS table_name,
|
|
78
|
+
a.attname AS column_name,
|
|
79
|
+
pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type,
|
|
80
|
+
t.typcategory,
|
|
81
|
+
a.attnotnull AS not_null,
|
|
82
|
+
pg_get_expr(d.adbin, d.adrelid) AS default_value,
|
|
83
|
+
col_description(a.attrelid, a.attnum) AS column_comment,
|
|
84
|
+
c.reltuples::bigint AS estimated_rows
|
|
85
|
+
FROM pg_catalog.pg_class c
|
|
86
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
|
87
|
+
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
|
|
88
|
+
JOIN pg_catalog.pg_type t ON t.oid = a.atttypid
|
|
89
|
+
LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid = c.oid AND d.adnum = a.attnum
|
|
90
|
+
WHERE c.relkind IN ('r','v','m')
|
|
91
|
+
AND n.nspname NOT IN ('pg_catalog','information_schema','pg_toast')
|
|
92
|
+
AND a.attnum > 0 AND NOT a.attisdropped
|
|
93
|
+
AND has_table_privilege(c.oid, 'SELECT')
|
|
94
|
+
ORDER BY n.nspname, c.relname, a.attnum;
|
|
95
|
+
`;
|
|
96
|
+
const { rows } = await client.query(structureQuery);
|
|
97
|
+
// Phase 1b: Foreign keys
|
|
98
|
+
const fkQuery = `
|
|
99
|
+
SELECT
|
|
100
|
+
n.nspname AS schema_name,
|
|
101
|
+
c.relname AS table_name,
|
|
102
|
+
a.attname AS column_name,
|
|
103
|
+
fn.nspname AS foreign_schema,
|
|
104
|
+
fc.relname AS foreign_table,
|
|
105
|
+
fa.attname AS foreign_column
|
|
106
|
+
FROM pg_catalog.pg_constraint con
|
|
107
|
+
JOIN pg_catalog.pg_class c ON c.oid = con.conrelid
|
|
108
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
|
109
|
+
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(con.conkey)
|
|
110
|
+
JOIN pg_catalog.pg_class fc ON fc.oid = con.confrelid
|
|
111
|
+
JOIN pg_catalog.pg_namespace fn ON fn.oid = fc.relnamespace
|
|
112
|
+
JOIN pg_catalog.pg_attribute fa ON fa.attrelid = fc.oid AND fa.attnum = ANY(con.confkey)
|
|
113
|
+
WHERE con.contype = 'f'
|
|
114
|
+
AND n.nspname NOT IN ('pg_catalog','information_schema');
|
|
115
|
+
`;
|
|
116
|
+
const fkResult = await client.query(fkQuery);
|
|
117
|
+
// Phase 1c: Enums
|
|
118
|
+
const enumQuery = `
|
|
119
|
+
SELECT t.typname, e.enumlabel
|
|
120
|
+
FROM pg_catalog.pg_type t
|
|
121
|
+
JOIN pg_catalog.pg_enum e ON t.oid = e.enumtypid
|
|
122
|
+
ORDER BY t.typname, e.enumsortorder;
|
|
123
|
+
`;
|
|
124
|
+
const enumResult = await client.query(enumQuery);
|
|
125
|
+
// Build schema
|
|
126
|
+
const tableMap = new Map();
|
|
127
|
+
for (const row of rows) {
|
|
128
|
+
const key = `${row.schema_name}.${row.table_name}`;
|
|
129
|
+
if (!tableMap.has(key)) {
|
|
130
|
+
tableMap.set(key, {
|
|
131
|
+
schema: row.schema_name,
|
|
132
|
+
name: row.table_name,
|
|
133
|
+
columns: [],
|
|
134
|
+
estimatedRows: Number(row.estimated_rows) || 0,
|
|
135
|
+
foreignKeys: [],
|
|
136
|
+
indexes: [],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
tableMap.get(key).columns.push({
|
|
140
|
+
name: row.column_name,
|
|
141
|
+
dataType: row.data_type,
|
|
142
|
+
typeCategory: row.typcategory,
|
|
143
|
+
notNull: row.not_null,
|
|
144
|
+
defaultValue: row.default_value,
|
|
145
|
+
comment: row.column_comment,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// Add foreign keys
|
|
149
|
+
for (const fk of fkResult.rows) {
|
|
150
|
+
const key = `${fk.schema_name}.${fk.table_name}`;
|
|
151
|
+
const table = tableMap.get(key);
|
|
152
|
+
if (table) {
|
|
153
|
+
table.foreignKeys.push({
|
|
154
|
+
column: fk.column_name,
|
|
155
|
+
foreignTable: `${fk.foreign_schema}.${fk.foreign_table}`,
|
|
156
|
+
foreignColumn: fk.foreign_column,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Build enums
|
|
161
|
+
const enums = {};
|
|
162
|
+
for (const row of enumResult.rows) {
|
|
163
|
+
if (!enums[row.typname])
|
|
164
|
+
enums[row.typname] = [];
|
|
165
|
+
enums[row.typname].push(row.enumlabel);
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
tables: Array.from(tableMap.values()),
|
|
169
|
+
enums,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
client.release();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
async close() {
|
|
177
|
+
if (this.pool) {
|
|
178
|
+
await this.pool.end();
|
|
179
|
+
this.pool = null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
export class HTTPConnector {
|
|
184
|
+
config;
|
|
185
|
+
constructor(config) {
|
|
186
|
+
this.config = config;
|
|
187
|
+
}
|
|
188
|
+
async execute(sql, params) {
|
|
189
|
+
const response = await fetch(this.config.endpoint, {
|
|
190
|
+
method: 'POST',
|
|
191
|
+
headers: {
|
|
192
|
+
'Content-Type': 'application/json',
|
|
193
|
+
...this.config.headers,
|
|
194
|
+
},
|
|
195
|
+
body: JSON.stringify({ sql, params }),
|
|
196
|
+
});
|
|
197
|
+
if (!response.ok) {
|
|
198
|
+
throw new InferError('QUERY_ERROR', 'executor', `HTTP ${response.status}: ${response.statusText}`);
|
|
199
|
+
}
|
|
200
|
+
return response.json();
|
|
201
|
+
}
|
|
202
|
+
async introspect() {
|
|
203
|
+
const response = await fetch(`${this.config.endpoint}/introspect`, {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
headers: {
|
|
206
|
+
'Content-Type': 'application/json',
|
|
207
|
+
...this.config.headers,
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
if (!response.ok) {
|
|
211
|
+
throw new InferError('CONNECTION_ERROR', 'executor', 'Introspection failed');
|
|
212
|
+
}
|
|
213
|
+
return response.json();
|
|
214
|
+
}
|
|
215
|
+
async close() { }
|
|
216
|
+
}
|
|
217
|
+
// ── SupabaseConnector (stub) ──
|
|
218
|
+
export class SupabaseConnector {
|
|
219
|
+
constructor(_config) {
|
|
220
|
+
// Stub — full implementation in future release
|
|
221
|
+
}
|
|
222
|
+
async execute() {
|
|
223
|
+
throw new InferError('CONNECTION_ERROR', 'executor', 'SupabaseConnector is not yet implemented');
|
|
224
|
+
}
|
|
225
|
+
async introspect() {
|
|
226
|
+
throw new InferError('CONNECTION_ERROR', 'executor', 'SupabaseConnector is not yet implemented');
|
|
227
|
+
}
|
|
228
|
+
async close() { }
|
|
229
|
+
}
|
|
230
|
+
// ── LambdaConnector (stub) ──
|
|
231
|
+
export class LambdaConnector {
|
|
232
|
+
constructor(_config) {
|
|
233
|
+
// Stub — full implementation in future release
|
|
234
|
+
}
|
|
235
|
+
async execute() {
|
|
236
|
+
throw new InferError('CONNECTION_ERROR', 'executor', 'LambdaConnector is not yet implemented');
|
|
237
|
+
}
|
|
238
|
+
async introspect() {
|
|
239
|
+
throw new InferError('CONNECTION_ERROR', 'executor', 'LambdaConnector is not yet implemented');
|
|
240
|
+
}
|
|
241
|
+
async close() { }
|
|
242
|
+
}
|
|
243
|
+
// ── Helpers ──
|
|
244
|
+
function pgTypeToColumnType(oid) {
|
|
245
|
+
// Common PostgreSQL OIDs
|
|
246
|
+
const numericTypes = new Set([20, 21, 23, 26, 700, 701, 790, 1700]);
|
|
247
|
+
const dateTypes = new Set([1082]);
|
|
248
|
+
const timestampTypes = new Set([1114, 1184]);
|
|
249
|
+
const boolType = 16;
|
|
250
|
+
const jsonTypes = new Set([114, 3802]);
|
|
251
|
+
if (numericTypes.has(oid))
|
|
252
|
+
return 'numeric';
|
|
253
|
+
if (dateTypes.has(oid))
|
|
254
|
+
return 'date';
|
|
255
|
+
if (timestampTypes.has(oid))
|
|
256
|
+
return 'timestamp';
|
|
257
|
+
if (oid === boolType)
|
|
258
|
+
return 'boolean';
|
|
259
|
+
if (jsonTypes.has(oid))
|
|
260
|
+
return 'json';
|
|
261
|
+
return 'text';
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=connectors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connectors.js","sourceRoot":"","sources":["../src/connectors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAgBzC,MAAM,OAAO,iBAAiB;IACpB,IAAI,CAAM,CAAC,wBAAwB;IACnC,MAAM,CAA0B;IAExC,YAAY,MAA+B;QACzC,4BAA4B;QAC5B,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,0FAA0F,CAAC,CAAC;QAC9G,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEO,KAAK,CAAC,OAAO;QACnB,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YACf,IAAI,CAAC;gBACH,qDAAqD;gBACrD,MAAM,EAAE,GAAQ,MAAM,CAAC,QAAQ,CAAC,qBAAqB,CAAC,EAAE,CAAC,CAAC;gBAC1D,MAAM,IAAI,GAAG,EAAE,CAAC,OAAO,EAAE,IAAI,IAAI,EAAE,CAAC,IAAI,CAAC;gBACzC,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,CAAC;oBACnB,gBAAgB,EAAE,IAAI,CAAC,MAAM,CAAC,gBAAgB;oBAC9C,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI;oBACtB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI;oBACtB,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ;oBAC9B,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI;oBACtB,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ;oBAC9B,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG;oBACpB,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,EAAE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I,UAAU,CAAC,kBAAkB,EAAE,UAAU,EAAE,wCAAwC,CAAC,CAAC;IACjG,CAAC;IACD,KAAK,CAAC,UAAU;QACd,MAAM,IAAI,UAAU,CAAC,kBAAkB,EAAE,UAAU,EAAE,wCAAwC,CAAC,CAAC;IACjG,CAAC;IACD,KAAK,CAAC,KAAK,KAAmB,CAAC;CAChC;AAED,gBAAgB;AAChB,SAAS,kBAAkB,CAAC,GAAW;IACrC,yBAAyB;IACzB,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;IACpE,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAClC,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IAC7C,MAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;IAEvC,IAAI,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC;IAC5C,IAAI,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,MAAM,CAAC;IACtC,IAAI,cAAc,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,WAAW,CAAC;IAChD,IAAI,GAAG,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IACvC,IAAI,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,MAAM,CAAC;IACtC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/dates.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare const RELATIVE_PHRASES: readonly ["today", "yesterday", "tomorrow", "this week", "last week", "next week", "last 7 days", "next 7 days", "last 30 days", "next 30 days", "this month", "last month", "next month", "this year", "last year"];
|
|
2
|
+
export type RelativeDatePhrase = (typeof RELATIVE_PHRASES)[number] | `last ${number} days` | `next ${number} days` | `last ${number} weeks` | `next ${number} weeks` | `last ${number} months` | `next ${number} months`;
|
|
3
|
+
export declare function formatDateISO(d: Date): string;
|
|
4
|
+
export declare function startOfLocalDay(d: Date): Date;
|
|
5
|
+
export declare function addDays(d: Date, n: number): Date;
|
|
6
|
+
export declare function startOfWeekMonday(d: Date): Date;
|
|
7
|
+
export declare function startOfMonth(d: Date): Date;
|
|
8
|
+
export declare function endOfMonth(d: Date): Date;
|
|
9
|
+
export declare function tryDynamicMatch(phrase: string, now: Date): {
|
|
10
|
+
start: Date;
|
|
11
|
+
end: Date;
|
|
12
|
+
} | null;
|
|
13
|
+
export declare function detectRelativePhrase(text: string): RelativeDatePhrase | null;
|
|
14
|
+
export type ComputedRange = {
|
|
15
|
+
startISO: string;
|
|
16
|
+
endISO: string;
|
|
17
|
+
start: Date;
|
|
18
|
+
end: Date;
|
|
19
|
+
};
|
|
20
|
+
export declare function computeRelativeRange(phrase: RelativeDatePhrase | string, now: Date): ComputedRange;
|
|
21
|
+
export declare function resolveDateRange(options: {
|
|
22
|
+
startDate?: string;
|
|
23
|
+
endDate?: string;
|
|
24
|
+
phrase?: string;
|
|
25
|
+
now?: Date;
|
|
26
|
+
}): {
|
|
27
|
+
startDate: string;
|
|
28
|
+
endDate: string;
|
|
29
|
+
} | null;
|
|
30
|
+
export declare function parseRelativeDateRange(question: string, now?: Date): {
|
|
31
|
+
startDate: string;
|
|
32
|
+
endDate: string;
|
|
33
|
+
phrase: string;
|
|
34
|
+
} | null;
|
|
35
|
+
//# sourceMappingURL=dates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dates.d.ts","sourceRoot":"","sources":["../src/dates.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,gBAAgB,sNAgBnB,CAAC;AA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AAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAYhD;AAID,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,IAAI,GACT;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAK/D"}
|
package/dist/dates.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// ── Relative date phrases ──
|
|
2
|
+
export const RELATIVE_PHRASES = [
|
|
3
|
+
"today",
|
|
4
|
+
"yesterday",
|
|
5
|
+
"tomorrow",
|
|
6
|
+
"this week",
|
|
7
|
+
"last week",
|
|
8
|
+
"next week",
|
|
9
|
+
"last 7 days",
|
|
10
|
+
"next 7 days",
|
|
11
|
+
"last 30 days",
|
|
12
|
+
"next 30 days",
|
|
13
|
+
"this month",
|
|
14
|
+
"last month",
|
|
15
|
+
"next month",
|
|
16
|
+
"this year",
|
|
17
|
+
"last year",
|
|
18
|
+
];
|
|
19
|
+
// ── Helper functions ──
|
|
20
|
+
export function formatDateISO(d) {
|
|
21
|
+
const y = d.getFullYear();
|
|
22
|
+
const m = String(d.getMonth() + 1).padStart(2, "0");
|
|
23
|
+
const day = String(d.getDate()).padStart(2, "0");
|
|
24
|
+
return `${y}-${m}-${day}`;
|
|
25
|
+
}
|
|
26
|
+
export function startOfLocalDay(d) {
|
|
27
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
28
|
+
}
|
|
29
|
+
export function addDays(d, n) {
|
|
30
|
+
const result = new Date(d);
|
|
31
|
+
result.setDate(result.getDate() + n);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
export function startOfWeekMonday(d) {
|
|
35
|
+
const day = d.getDay();
|
|
36
|
+
const diff = day === 0 ? -6 : 1 - day;
|
|
37
|
+
return addDays(startOfLocalDay(d), diff);
|
|
38
|
+
}
|
|
39
|
+
export function startOfMonth(d) {
|
|
40
|
+
return new Date(d.getFullYear(), d.getMonth(), 1);
|
|
41
|
+
}
|
|
42
|
+
export function endOfMonth(d) {
|
|
43
|
+
return new Date(d.getFullYear(), d.getMonth() + 1, 0);
|
|
44
|
+
}
|
|
45
|
+
const RELATIVE_RULES = [
|
|
46
|
+
{
|
|
47
|
+
phrase: "today",
|
|
48
|
+
compute: (now) => {
|
|
49
|
+
const s = startOfLocalDay(now);
|
|
50
|
+
return { start: s, end: s };
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
phrase: "yesterday",
|
|
55
|
+
compute: (now) => {
|
|
56
|
+
const s = addDays(startOfLocalDay(now), -1);
|
|
57
|
+
return { start: s, end: s };
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
phrase: "tomorrow",
|
|
62
|
+
compute: (now) => {
|
|
63
|
+
const s = addDays(startOfLocalDay(now), 1);
|
|
64
|
+
return { start: s, end: s };
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
phrase: "this week",
|
|
69
|
+
compute: (now) => {
|
|
70
|
+
const s = startOfWeekMonday(now);
|
|
71
|
+
const e = addDays(s, 6);
|
|
72
|
+
return { start: s, end: e };
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
phrase: "last week",
|
|
77
|
+
compute: (now) => {
|
|
78
|
+
const s = addDays(startOfWeekMonday(now), -7);
|
|
79
|
+
const e = addDays(s, 6);
|
|
80
|
+
return { start: s, end: e };
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
phrase: "next week",
|
|
85
|
+
compute: (now) => {
|
|
86
|
+
const s = addDays(startOfWeekMonday(now), 7);
|
|
87
|
+
const e = addDays(s, 6);
|
|
88
|
+
return { start: s, end: e };
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
phrase: "last 7 days",
|
|
93
|
+
compute: (now) => {
|
|
94
|
+
const today = startOfLocalDay(now);
|
|
95
|
+
return { start: addDays(today, -6), end: today };
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
phrase: "next 7 days",
|
|
100
|
+
compute: (now) => {
|
|
101
|
+
const today = startOfLocalDay(now);
|
|
102
|
+
return { start: today, end: addDays(today, 6) };
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
phrase: "last 30 days",
|
|
107
|
+
compute: (now) => {
|
|
108
|
+
const today = startOfLocalDay(now);
|
|
109
|
+
return { start: addDays(today, -29), end: today };
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
phrase: "next 30 days",
|
|
114
|
+
compute: (now) => {
|
|
115
|
+
const today = startOfLocalDay(now);
|
|
116
|
+
return { start: today, end: addDays(today, 29) };
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
phrase: "this month",
|
|
121
|
+
compute: (now) => {
|
|
122
|
+
return { start: startOfMonth(now), end: endOfMonth(now) };
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
phrase: "last month",
|
|
127
|
+
compute: (now) => {
|
|
128
|
+
const prev = new Date(now.getFullYear(), now.getMonth() - 1, 1);
|
|
129
|
+
return { start: startOfMonth(prev), end: endOfMonth(prev) };
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
phrase: "next month",
|
|
134
|
+
compute: (now) => {
|
|
135
|
+
const next = new Date(now.getFullYear(), now.getMonth() + 1, 1);
|
|
136
|
+
return { start: startOfMonth(next), end: endOfMonth(next) };
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
phrase: "this year",
|
|
141
|
+
compute: (now) => {
|
|
142
|
+
return {
|
|
143
|
+
start: new Date(now.getFullYear(), 0, 1),
|
|
144
|
+
end: new Date(now.getFullYear(), 11, 31),
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
phrase: "last year",
|
|
150
|
+
compute: (now) => {
|
|
151
|
+
const y = now.getFullYear() - 1;
|
|
152
|
+
return { start: new Date(y, 0, 1), end: new Date(y, 11, 31) };
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
];
|
|
156
|
+
// ── Dynamic patterns ──
|
|
157
|
+
const DYNAMIC_PAST_DAYS = /^last (\d+) days?$/i;
|
|
158
|
+
const DYNAMIC_FUTURE_DAYS = /^next (\d+) days?$/i;
|
|
159
|
+
const DYNAMIC_PAST_WEEKS = /^last (\d+) weeks?$/i;
|
|
160
|
+
const DYNAMIC_FUTURE_WEEKS = /^next (\d+) weeks?$/i;
|
|
161
|
+
const DYNAMIC_PAST_MONTHS = /^last (\d+) months?$/i;
|
|
162
|
+
const DYNAMIC_FUTURE_MONTHS = /^next (\d+) months?$/i;
|
|
163
|
+
const DYNAMIC_PATTERNS = [
|
|
164
|
+
{
|
|
165
|
+
regex: DYNAMIC_PAST_DAYS,
|
|
166
|
+
compute: (n, now) => {
|
|
167
|
+
const today = startOfLocalDay(now);
|
|
168
|
+
return { start: addDays(today, -(n - 1)), end: today };
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
regex: DYNAMIC_FUTURE_DAYS,
|
|
173
|
+
compute: (n, now) => {
|
|
174
|
+
const today = startOfLocalDay(now);
|
|
175
|
+
return { start: today, end: addDays(today, n - 1) };
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
regex: DYNAMIC_PAST_WEEKS,
|
|
180
|
+
compute: (n, now) => {
|
|
181
|
+
const monday = startOfWeekMonday(now);
|
|
182
|
+
return { start: addDays(monday, -7 * n), end: addDays(monday, -1) };
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
regex: DYNAMIC_FUTURE_WEEKS,
|
|
187
|
+
compute: (n, now) => {
|
|
188
|
+
const monday = startOfWeekMonday(now);
|
|
189
|
+
const nextMonday = addDays(monday, 7);
|
|
190
|
+
return { start: nextMonday, end: addDays(nextMonday, 7 * n - 1) };
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
regex: DYNAMIC_PAST_MONTHS,
|
|
195
|
+
compute: (n, now) => {
|
|
196
|
+
const s = new Date(now.getFullYear(), now.getMonth() - n, 1);
|
|
197
|
+
const e = new Date(now.getFullYear(), now.getMonth(), 0);
|
|
198
|
+
return { start: s, end: e };
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
regex: DYNAMIC_FUTURE_MONTHS,
|
|
203
|
+
compute: (n, now) => {
|
|
204
|
+
const s = new Date(now.getFullYear(), now.getMonth() + 1, 1);
|
|
205
|
+
const e = new Date(now.getFullYear(), now.getMonth() + 1 + n, 0);
|
|
206
|
+
return { start: s, end: e };
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
];
|
|
210
|
+
// ── Dynamic match ──
|
|
211
|
+
export function tryDynamicMatch(phrase, now) {
|
|
212
|
+
for (const { regex, compute } of DYNAMIC_PATTERNS) {
|
|
213
|
+
const m = phrase.match(regex);
|
|
214
|
+
if (m) {
|
|
215
|
+
const n = parseInt(m[1], 10);
|
|
216
|
+
if (n > 0)
|
|
217
|
+
return compute(n, now);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
// ── Detect a relative phrase in freeform text ──
|
|
223
|
+
export function detectRelativePhrase(text) {
|
|
224
|
+
const lower = text.toLowerCase().trim();
|
|
225
|
+
// Check static phrases (longest first to prefer "last 30 days" over "last 7 days")
|
|
226
|
+
const sorted = [...RELATIVE_PHRASES].sort((a, b) => b.length - a.length);
|
|
227
|
+
for (const phrase of sorted) {
|
|
228
|
+
if (lower.includes(phrase)) {
|
|
229
|
+
return phrase;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// Check dynamic patterns
|
|
233
|
+
for (const { regex } of DYNAMIC_PATTERNS) {
|
|
234
|
+
const m = lower.match(regex);
|
|
235
|
+
if (m) {
|
|
236
|
+
return m[0];
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
export function computeRelativeRange(phrase, now) {
|
|
242
|
+
const lower = phrase.toLowerCase().trim();
|
|
243
|
+
// Check static rules first
|
|
244
|
+
for (const rule of RELATIVE_RULES) {
|
|
245
|
+
if (rule.phrase === lower) {
|
|
246
|
+
const { start, end } = rule.compute(now);
|
|
247
|
+
return {
|
|
248
|
+
start,
|
|
249
|
+
end,
|
|
250
|
+
startISO: formatDateISO(start),
|
|
251
|
+
endISO: formatDateISO(end),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
// Check dynamic patterns
|
|
256
|
+
const dynamic = tryDynamicMatch(lower, now);
|
|
257
|
+
if (dynamic) {
|
|
258
|
+
return {
|
|
259
|
+
start: dynamic.start,
|
|
260
|
+
end: dynamic.end,
|
|
261
|
+
startISO: formatDateISO(dynamic.start),
|
|
262
|
+
endISO: formatDateISO(dynamic.end),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
// Fallback: treat as today
|
|
266
|
+
const today = startOfLocalDay(now);
|
|
267
|
+
return {
|
|
268
|
+
start: today,
|
|
269
|
+
end: today,
|
|
270
|
+
startISO: formatDateISO(today),
|
|
271
|
+
endISO: formatDateISO(today),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
// ── Resolve a date range from explicit dates or a phrase ──
|
|
275
|
+
export function resolveDateRange(options) {
|
|
276
|
+
if (options.startDate && options.endDate) {
|
|
277
|
+
return { startDate: options.startDate, endDate: options.endDate };
|
|
278
|
+
}
|
|
279
|
+
if (options.phrase) {
|
|
280
|
+
const range = computeRelativeRange(options.phrase, options.now ?? new Date());
|
|
281
|
+
return { startDate: range.startISO, endDate: range.endISO };
|
|
282
|
+
}
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
// ── Parse a relative date range from a natural-language question ──
|
|
286
|
+
export function parseRelativeDateRange(question, now) {
|
|
287
|
+
const phrase = detectRelativePhrase(question);
|
|
288
|
+
if (!phrase)
|
|
289
|
+
return null;
|
|
290
|
+
const range = computeRelativeRange(phrase, now ?? new Date());
|
|
291
|
+
return { startDate: range.startISO, endDate: range.endISO, phrase };
|
|
292
|
+
}
|
|
293
|
+
//# sourceMappingURL=dates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dates.js","sourceRoot":"","sources":["../src/dates.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAE9B,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,OAAO;IACP,WAAW;IACX,UAAU;IACV,WAAW;IACX,WAAW;IACX,WAAW;IACX,aAAa;IACb,aAAa;IACb,cAAc;IACd,cAAc;IACd,YAAY;IACZ,YAAY;IACZ,YAAY;IACZ,WAAW;IACX,WAAW;CACH,CAAC;AAWX,yBAAyB;AAEzB,MAAM,UAAU,aAAa,CAAC,CAAO;IACnC,MAAM,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;IAC1B,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACpD,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACjD,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;AAC5B,CAAC;AAED,MAAM,U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AI,OAAO,CAAC,OAAO,EAAE,CAAC;QACzC,OAAO,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC;IACpE,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACnB,MAAM,KAAK,GAAG,oBAAoB,CAChC,OAAO,CAAC,MAA4B,EACpC,OAAO,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAC1B,CAAC;QACF,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;IAC9D,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,qEAAqE;AAErE,MAAM,UAAU,sBAAsB,CACpC,QAAgB,EAChB,GAAU;IAEV,MAAM,MAAM,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAC9C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzB,MAAM,KAAK,GAAG,oBAAoB,CAAC,MAAM,EAAE,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC;IAC9D,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;AACtE,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { SmartEntityFilter, ResolvedEntity, InferConnector } from './types.js';
|
|
2
|
+
export interface EntityResolver {
|
|
3
|
+
resolve(input: string, tenantId: string): Promise<ResolvedEntity>;
|
|
4
|
+
}
|
|
5
|
+
export declare class EntityResolverRegistry {
|
|
6
|
+
private resolvers;
|
|
7
|
+
register(name: string, resolver: EntityResolver): void;
|
|
8
|
+
get(name: string): EntityResolver | undefined;
|
|
9
|
+
has(name: string): boolean;
|
|
10
|
+
resolveAll(entityFilters: SmartEntityFilter[], tenantId: string): Promise<ResolvedEntity[]>;
|
|
11
|
+
}
|
|
12
|
+
export declare class DatabaseEntityResolver implements EntityResolver {
|
|
13
|
+
private connector;
|
|
14
|
+
private tableName;
|
|
15
|
+
private idColumn;
|
|
16
|
+
private nameColumn;
|
|
17
|
+
private tenantScoped;
|
|
18
|
+
private resolverName;
|
|
19
|
+
constructor(config: {
|
|
20
|
+
connector: InferConnector;
|
|
21
|
+
tableName: string;
|
|
22
|
+
idColumn: string;
|
|
23
|
+
nameColumn: string;
|
|
24
|
+
resolverName: string;
|
|
25
|
+
tenantScoped?: boolean;
|
|
26
|
+
});
|
|
27
|
+
resolve(input: string, tenantId: string): Promise<ResolvedEntity>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=entity-resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-resolver.d.ts","sourceRoot":"","sources":["../src/entity-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAIpF,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CACnE;AAGD,qBAAa,sBAAsB;IACjC,OAAO,CAAC,SAAS,CAAqC;IAEtD,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,GAAG,IAAI;IAItD,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAI7C,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIpB,UAAU,CACd,aAAa,EAAE,iBAAiB,EAAE,EAClC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,cAAc,EAAE,CAAC;CAa7B;AAGD,qBAAa,sBAAuB,YAAW,cAAc;IAC3D,OAAO,CAAC,SAAS,CAAiB;IAClC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,QA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