nttp 1.0.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 +245 -0
- package/dist/NTTP.d.ts +121 -0
- package/dist/NTTP.d.ts.map +1 -0
- package/dist/NTTP.js +229 -0
- package/dist/NTTP.js.map +1 -0
- package/dist/cache/exact-cache.d.ts +46 -0
- package/dist/cache/exact-cache.d.ts.map +1 -0
- package/dist/cache/exact-cache.js +104 -0
- package/dist/cache/exact-cache.js.map +1 -0
- package/dist/cache/index.d.ts +8 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +7 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/semantic-cache.d.ts +82 -0
- package/dist/cache/semantic-cache.d.ts.map +1 -0
- package/dist/cache/semantic-cache.js +243 -0
- package/dist/cache/semantic-cache.js.map +1 -0
- package/dist/cache/types.d.ts +135 -0
- package/dist/cache/types.d.ts.map +1 -0
- package/dist/cache/types.js +5 -0
- package/dist/cache/types.js.map +1 -0
- package/dist/cache.d.ts +71 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +129 -0
- package/dist/cache.js.map +1 -0
- package/dist/errors.d.ts +35 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +55 -0
- package/dist/errors.js.map +1 -0
- package/dist/executor.d.ts +82 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +395 -0
- package/dist/executor.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/intent.d.ts +55 -0
- package/dist/intent.d.ts.map +1 -0
- package/dist/intent.js +183 -0
- package/dist/intent.js.map +1 -0
- package/dist/llm.d.ts +60 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +171 -0
- package/dist/llm.js.map +1 -0
- package/dist/types.d.ts +280 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +61 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +19 -0
- package/dist/utils.js.map +1 -0
- package/package.json +94 -0
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory schema cache for nttp.
|
|
3
|
+
* Provides async-safe operations for storing and retrieving schemas.
|
|
4
|
+
*/
|
|
5
|
+
import type { SchemaDefinition } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Schema cache statistics
|
|
8
|
+
*/
|
|
9
|
+
export interface SchemaStats {
|
|
10
|
+
total_schemas: number;
|
|
11
|
+
pinned_schemas: number;
|
|
12
|
+
total_uses: number;
|
|
13
|
+
average_uses: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* In-memory cache for schema definitions.
|
|
17
|
+
*
|
|
18
|
+
* Node.js is single-threaded, so no mutex needed for basic operations.
|
|
19
|
+
* Async signatures maintained for API compatibility with Python version.
|
|
20
|
+
*/
|
|
21
|
+
export declare class SchemaCache {
|
|
22
|
+
private cache;
|
|
23
|
+
/**
|
|
24
|
+
* Get schema by ID.
|
|
25
|
+
*/
|
|
26
|
+
get(schemaId: string): Promise<SchemaDefinition | undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Store schema in cache.
|
|
29
|
+
*/
|
|
30
|
+
set(schemaId: string, schema: SchemaDefinition): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Check if schema exists in cache.
|
|
33
|
+
*/
|
|
34
|
+
exists(schemaId: string): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Delete schema from cache.
|
|
37
|
+
* Throws error if schema is pinned.
|
|
38
|
+
*/
|
|
39
|
+
delete(schemaId: string): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* List all cached schemas.
|
|
42
|
+
*/
|
|
43
|
+
listAll(): Promise<SchemaDefinition[]>;
|
|
44
|
+
/**
|
|
45
|
+
* Update usage statistics for a schema.
|
|
46
|
+
*/
|
|
47
|
+
updateUsage(schemaId: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Pin schema to prevent eviction.
|
|
50
|
+
*/
|
|
51
|
+
pin(schemaId: string): Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Unpin schema to allow eviction.
|
|
54
|
+
*/
|
|
55
|
+
unpin(schemaId: string): Promise<boolean>;
|
|
56
|
+
/**
|
|
57
|
+
* Add an example query to a schema.
|
|
58
|
+
* Keeps only the last 10 examples.
|
|
59
|
+
*/
|
|
60
|
+
addExampleQuery(schemaId: string, query: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Clear all non-pinned schemas from cache.
|
|
63
|
+
* Returns number of schemas cleared.
|
|
64
|
+
*/
|
|
65
|
+
clear(): Promise<number>;
|
|
66
|
+
/**
|
|
67
|
+
* Get cache statistics.
|
|
68
|
+
*/
|
|
69
|
+
getStats(): Promise<SchemaStats>;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAGnD;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;;;;GAKG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,KAAK,CAA4C;IAEzD;;OAEG;IACG,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;IAIlE;;OAEG;IACG,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;IAIpE;;OAEG;IACG,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIhD;;;OAGG;IACG,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAgBhD;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC;IAI5C;;OAEG;IACG,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlD;;OAEG;IACG,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAS7C;;OAEG;IACG,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAS/C;;;OAGG;IACG,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWrE;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IAa9B;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,WAAW,CAAC;CAavC"}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory schema cache for nttp.
|
|
3
|
+
* Provides async-safe operations for storing and retrieving schemas.
|
|
4
|
+
*/
|
|
5
|
+
import { CacheError } from './errors.js';
|
|
6
|
+
/**
|
|
7
|
+
* In-memory cache for schema definitions.
|
|
8
|
+
*
|
|
9
|
+
* Node.js is single-threaded, so no mutex needed for basic operations.
|
|
10
|
+
* Async signatures maintained for API compatibility with Python version.
|
|
11
|
+
*/
|
|
12
|
+
export class SchemaCache {
|
|
13
|
+
cache = new Map();
|
|
14
|
+
/**
|
|
15
|
+
* Get schema by ID.
|
|
16
|
+
*/
|
|
17
|
+
async get(schemaId) {
|
|
18
|
+
return this.cache.get(schemaId);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Store schema in cache.
|
|
22
|
+
*/
|
|
23
|
+
async set(schemaId, schema) {
|
|
24
|
+
this.cache.set(schemaId, schema);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Check if schema exists in cache.
|
|
28
|
+
*/
|
|
29
|
+
async exists(schemaId) {
|
|
30
|
+
return this.cache.has(schemaId);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Delete schema from cache.
|
|
34
|
+
* Throws error if schema is pinned.
|
|
35
|
+
*/
|
|
36
|
+
async delete(schemaId) {
|
|
37
|
+
const schema = this.cache.get(schemaId);
|
|
38
|
+
if (!schema) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
// Block deletion of pinned schemas
|
|
42
|
+
if (schema.pinned) {
|
|
43
|
+
throw new CacheError(`Cannot delete pinned schema: ${schemaId}. Unpin it first.`);
|
|
44
|
+
}
|
|
45
|
+
return this.cache.delete(schemaId);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* List all cached schemas.
|
|
49
|
+
*/
|
|
50
|
+
async listAll() {
|
|
51
|
+
return Array.from(this.cache.values());
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Update usage statistics for a schema.
|
|
55
|
+
*/
|
|
56
|
+
async updateUsage(schemaId) {
|
|
57
|
+
const schema = this.cache.get(schemaId);
|
|
58
|
+
if (schema) {
|
|
59
|
+
schema.use_count += 1;
|
|
60
|
+
schema.last_used_at = new Date();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Pin schema to prevent eviction.
|
|
65
|
+
*/
|
|
66
|
+
async pin(schemaId) {
|
|
67
|
+
const schema = this.cache.get(schemaId);
|
|
68
|
+
if (schema) {
|
|
69
|
+
schema.pinned = true;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Unpin schema to allow eviction.
|
|
76
|
+
*/
|
|
77
|
+
async unpin(schemaId) {
|
|
78
|
+
const schema = this.cache.get(schemaId);
|
|
79
|
+
if (schema) {
|
|
80
|
+
schema.pinned = false;
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Add an example query to a schema.
|
|
87
|
+
* Keeps only the last 10 examples.
|
|
88
|
+
*/
|
|
89
|
+
async addExampleQuery(schemaId, query) {
|
|
90
|
+
const schema = this.cache.get(schemaId);
|
|
91
|
+
if (schema) {
|
|
92
|
+
if (!schema.example_queries.includes(query)) {
|
|
93
|
+
schema.example_queries.push(query);
|
|
94
|
+
// Keep only last 10 examples
|
|
95
|
+
schema.example_queries = schema.example_queries.slice(-10);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Clear all non-pinned schemas from cache.
|
|
101
|
+
* Returns number of schemas cleared.
|
|
102
|
+
*/
|
|
103
|
+
async clear() {
|
|
104
|
+
const initialCount = this.cache.size;
|
|
105
|
+
// Keep only pinned schemas
|
|
106
|
+
for (const [schemaId, schema] of this.cache.entries()) {
|
|
107
|
+
if (!schema.pinned) {
|
|
108
|
+
this.cache.delete(schemaId);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return initialCount - this.cache.size;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get cache statistics.
|
|
115
|
+
*/
|
|
116
|
+
async getStats() {
|
|
117
|
+
const schemas = Array.from(this.cache.values());
|
|
118
|
+
const total = schemas.length;
|
|
119
|
+
const pinned = schemas.filter((s) => s.pinned).length;
|
|
120
|
+
const totalUses = schemas.reduce((sum, s) => sum + s.use_count, 0);
|
|
121
|
+
return {
|
|
122
|
+
total_schemas: total,
|
|
123
|
+
pinned_schemas: pinned,
|
|
124
|
+
total_uses: totalUses,
|
|
125
|
+
average_uses: total > 0 ? totalUses / total : 0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es for NTTP application.
|
|
3
|
+
* Mirrors Python exception classes for API compatibility.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Error thrown when intent parsing fails.
|
|
7
|
+
*/
|
|
8
|
+
export declare class IntentParseError extends Error {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when SQL generation fails.
|
|
13
|
+
*/
|
|
14
|
+
export declare class SQLGenerationError extends Error {
|
|
15
|
+
constructor(message: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Error thrown when SQL execution fails.
|
|
19
|
+
*/
|
|
20
|
+
export declare class SQLExecutionError extends Error {
|
|
21
|
+
constructor(message: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Error thrown when LLM API calls fail.
|
|
25
|
+
*/
|
|
26
|
+
export declare class LLMError extends Error {
|
|
27
|
+
constructor(message: string);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Error thrown when cache operations fail.
|
|
31
|
+
*/
|
|
32
|
+
export declare class CacheError extends Error {
|
|
33
|
+
constructor(message: string);
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,MAAM;CAK5B;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;gBAC/B,OAAO,EAAE,MAAM;CAK5B;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;gBAC9B,OAAO,EAAE,MAAM;CAK5B;AAED;;GAEG;AACH,qBAAa,QAAS,SAAQ,KAAK;gBACrB,OAAO,EAAE,MAAM;CAK5B;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAK5B"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error classes for NTTP application.
|
|
3
|
+
* Mirrors Python exception classes for API compatibility.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Error thrown when intent parsing fails.
|
|
7
|
+
*/
|
|
8
|
+
export class IntentParseError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'IntentParseError';
|
|
12
|
+
Object.setPrototypeOf(this, IntentParseError.prototype);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Error thrown when SQL generation fails.
|
|
17
|
+
*/
|
|
18
|
+
export class SQLGenerationError extends Error {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = 'SQLGenerationError';
|
|
22
|
+
Object.setPrototypeOf(this, SQLGenerationError.prototype);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Error thrown when SQL execution fails.
|
|
27
|
+
*/
|
|
28
|
+
export class SQLExecutionError extends Error {
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = 'SQLExecutionError';
|
|
32
|
+
Object.setPrototypeOf(this, SQLExecutionError.prototype);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown when LLM API calls fail.
|
|
37
|
+
*/
|
|
38
|
+
export class LLMError extends Error {
|
|
39
|
+
constructor(message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = 'LLMError';
|
|
42
|
+
Object.setPrototypeOf(this, LLMError.prototype);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Error thrown when cache operations fail.
|
|
47
|
+
*/
|
|
48
|
+
export class CacheError extends Error {
|
|
49
|
+
constructor(message) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.name = 'CacheError';
|
|
52
|
+
Object.setPrototypeOf(this, CacheError.prototype);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC;IAC1D,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAC5D,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAC3D,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IAClD,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query execution, SQL generation, and schema inference.
|
|
3
|
+
* Main orchestration pipeline for NTTP.
|
|
4
|
+
*/
|
|
5
|
+
import type { Knex } from 'knex';
|
|
6
|
+
import type { Intent, QueryResultMeta } from './types.js';
|
|
7
|
+
import type { LLMService } from './llm.js';
|
|
8
|
+
import type { SchemaCache } from './cache.js';
|
|
9
|
+
import type { ExactCache, SemanticCache } from './cache/index.js';
|
|
10
|
+
import type { JsonObject, JsonValue } from './utils.js';
|
|
11
|
+
export interface ExecuteOptions {
|
|
12
|
+
query: string;
|
|
13
|
+
intent: Intent;
|
|
14
|
+
useCache: boolean;
|
|
15
|
+
forceNewSchema: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Result from query execution with type-safe cache metadata.
|
|
19
|
+
* Uses discriminated union to ensure layer-specific fields are correctly typed.
|
|
20
|
+
*/
|
|
21
|
+
export interface ExecuteResult {
|
|
22
|
+
query: string;
|
|
23
|
+
data: JsonObject[];
|
|
24
|
+
schemaId: string;
|
|
25
|
+
cacheHit: boolean;
|
|
26
|
+
intent: Intent;
|
|
27
|
+
sql?: string;
|
|
28
|
+
params?: JsonValue[];
|
|
29
|
+
meta?: QueryResultMeta;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Service for executing queries and managing SQL generation.
|
|
33
|
+
*/
|
|
34
|
+
export declare class QueryExecutor {
|
|
35
|
+
private db;
|
|
36
|
+
private llm;
|
|
37
|
+
private cache;
|
|
38
|
+
private l1Cache?;
|
|
39
|
+
private l2Cache?;
|
|
40
|
+
private defaultLimit;
|
|
41
|
+
constructor(db: Knex, llm: LLMService, cache: SchemaCache, l1Cache?: ExactCache | undefined, l2Cache?: SemanticCache | undefined);
|
|
42
|
+
/**
|
|
43
|
+
* Execute query with v2 3-layer caching.
|
|
44
|
+
*/
|
|
45
|
+
execute(options: ExecuteOptions): Promise<ExecuteResult>;
|
|
46
|
+
/**
|
|
47
|
+
* Execute query with v1 caching (legacy).
|
|
48
|
+
*/
|
|
49
|
+
executeV1(options: ExecuteOptions): Promise<ExecuteResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Generate SQL query from intent.
|
|
52
|
+
*/
|
|
53
|
+
generateSql(intent: Intent): Promise<{
|
|
54
|
+
sql: string;
|
|
55
|
+
params: JsonValue[];
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Execute raw SQL query.
|
|
59
|
+
*/
|
|
60
|
+
private executeRaw;
|
|
61
|
+
/**
|
|
62
|
+
* Validate SQL safety (read-only).
|
|
63
|
+
*/
|
|
64
|
+
private validateSqlSafety;
|
|
65
|
+
/**
|
|
66
|
+
* Infer JSON schema from query results.
|
|
67
|
+
*/
|
|
68
|
+
private inferSchemaFromResults;
|
|
69
|
+
/**
|
|
70
|
+
* Infer type of a field value.
|
|
71
|
+
*/
|
|
72
|
+
private inferFieldType;
|
|
73
|
+
/**
|
|
74
|
+
* Get schema description for LLM.
|
|
75
|
+
*/
|
|
76
|
+
private getSchemaDescription;
|
|
77
|
+
/**
|
|
78
|
+
* Generate schema ID from intent (SHA256 hash).
|
|
79
|
+
*/
|
|
80
|
+
private generateSchemaId;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../src/executor.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,KAAK,EACX,MAAM,EAEN,eAA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