nttp 1.4.11 → 1.4.13
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 +19 -19
- package/dist/cli.js +1 -1
- package/docs/README.md +31 -0
- package/docs/api.md +559 -0
- package/docs/caching.md +579 -0
- package/docs/configuration.md +763 -0
- package/docs/examples.md +615 -0
- package/docs/models.md +423 -0
- package/docs/production.md +681 -0
- package/docs/troubleshooting.md +694 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -85,17 +85,17 @@ Most queries hit L1 or L2. Only novel queries reach the LLM.
|
|
|
85
85
|
## Documentation
|
|
86
86
|
|
|
87
87
|
- **Quick Reference:** `npx nttp docs [topic]`
|
|
88
|
-
- **Full Guides:** [/docs](/docs)
|
|
88
|
+
- **Full Guides:** [/docs](https://github.com/Tylerbryy/nttp_typescript/tree/main/packages/nttp/docs)
|
|
89
89
|
|
|
90
90
|
### Guides
|
|
91
91
|
|
|
92
|
-
- [API Reference](docs/api.md) - Complete API documentation
|
|
93
|
-
- [Caching System](docs/caching.md) - 3-layer cache deep dive
|
|
94
|
-
- [Configuration](docs/configuration.md) - All config options
|
|
95
|
-
- [LLM Models](docs/models.md) - Model selection guide
|
|
96
|
-
- [Examples](docs/examples.md) - Usage examples
|
|
97
|
-
- [Production](docs/production.md) - Deployment best practices
|
|
98
|
-
- [Troubleshooting](docs/troubleshooting.md) - Common issues
|
|
92
|
+
- [API Reference](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/api.md) - Complete API documentation
|
|
93
|
+
- [Caching System](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/caching.md) - 3-layer cache deep dive
|
|
94
|
+
- [Configuration](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/configuration.md) - All config options
|
|
95
|
+
- [LLM Models](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/models.md) - Model selection guide
|
|
96
|
+
- [Examples](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/examples.md) - Usage examples
|
|
97
|
+
- [Production](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/production.md) - Deployment best practices
|
|
98
|
+
- [Troubleshooting](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/troubleshooting.md) - Common issues
|
|
99
99
|
|
|
100
100
|
---
|
|
101
101
|
|
|
@@ -162,7 +162,7 @@ const nttp = new NTTP({
|
|
|
162
162
|
await nttp.init();
|
|
163
163
|
```
|
|
164
164
|
|
|
165
|
-
See [Configuration Guide](docs/configuration.md) for all options.
|
|
165
|
+
See [Configuration Guide](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/configuration.md) for all options.
|
|
166
166
|
|
|
167
167
|
---
|
|
168
168
|
|
|
@@ -194,7 +194,7 @@ See [Configuration Guide](docs/configuration.md) for all options.
|
|
|
194
194
|
- **Mistral** - Open-source preference
|
|
195
195
|
- **Google Gemini** - Multimodal capabilities
|
|
196
196
|
|
|
197
|
-
See [Model Selection Guide](docs/models.md) for detailed comparison.
|
|
197
|
+
See [Model Selection Guide](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/models.md) for detailed comparison.
|
|
198
198
|
|
|
199
199
|
---
|
|
200
200
|
|
|
@@ -246,7 +246,7 @@ await nttp.pinSchema(schemaId);
|
|
|
246
246
|
await nttp.close();
|
|
247
247
|
```
|
|
248
248
|
|
|
249
|
-
See [API Reference](docs/api.md) for complete documentation.
|
|
249
|
+
See [API Reference](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/api.md) for complete documentation.
|
|
250
250
|
|
|
251
251
|
---
|
|
252
252
|
|
|
@@ -269,7 +269,7 @@ try {
|
|
|
269
269
|
}
|
|
270
270
|
```
|
|
271
271
|
|
|
272
|
-
All errors include helpful suggestions. See [Troubleshooting Guide](docs/troubleshooting.md).
|
|
272
|
+
All errors include helpful suggestions. See [Troubleshooting Guide](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/troubleshooting.md).
|
|
273
273
|
|
|
274
274
|
---
|
|
275
275
|
|
|
@@ -291,19 +291,19 @@ import type {
|
|
|
291
291
|
|
|
292
292
|
## Examples
|
|
293
293
|
|
|
294
|
-
- [Basic Usage](docs/examples.md#basic-queries)
|
|
295
|
-
- [Express Integration](docs/examples.md#using-with-express)
|
|
296
|
-
- [Next.js Integration](docs/examples.md#using-with-nextjs)
|
|
297
|
-
- [CLI Tools](docs/examples.md#cli-integration)
|
|
294
|
+
- [Basic Usage](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/examples.md#basic-queries)
|
|
295
|
+
- [Express Integration](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/examples.md#using-with-express)
|
|
296
|
+
- [Next.js Integration](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/examples.md#using-with-nextjs)
|
|
297
|
+
- [CLI Tools](https://github.com/Tylerbryy/nttp_typescript/blob/main/packages/nttp/docs/examples.md#cli-integration)
|
|
298
298
|
|
|
299
299
|
---
|
|
300
300
|
|
|
301
301
|
## Links
|
|
302
302
|
|
|
303
|
-
- [GitHub](https://github.com/
|
|
303
|
+
- [GitHub](https://github.com/Tylerbryy/nttp_typescript)
|
|
304
304
|
- [npm](https://www.npmjs.com/package/nttp)
|
|
305
|
-
- [Issues](https://github.com/
|
|
306
|
-
- [Documentation](/docs)
|
|
305
|
+
- [Issues](https://github.com/Tylerbryy/nttp_typescript/issues)
|
|
306
|
+
- [Documentation](https://github.com/Tylerbryy/nttp_typescript/tree/main/packages/nttp/docs)
|
|
307
307
|
|
|
308
308
|
---
|
|
309
309
|
|
package/dist/cli.js
CHANGED
|
@@ -11,7 +11,7 @@ const program = new Command();
|
|
|
11
11
|
program
|
|
12
12
|
.name('nttp')
|
|
13
13
|
.description('Query databases with natural language')
|
|
14
|
-
.version('1.4.
|
|
14
|
+
.version('1.4.13');
|
|
15
15
|
program
|
|
16
16
|
.command('setup')
|
|
17
17
|
.description('Interactive setup wizard (or use --non-interactive for agents)')
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# NTTP Documentation
|
|
2
|
+
|
|
3
|
+
Complete documentation for Natural Text Transfer Protocol.
|
|
4
|
+
|
|
5
|
+
## Quick Links
|
|
6
|
+
|
|
7
|
+
- [API Reference](./api.md) - Complete API documentation
|
|
8
|
+
- [Caching System](./caching.md) - 3-layer cache architecture
|
|
9
|
+
- [Configuration](./configuration.md) - All configuration options
|
|
10
|
+
- [LLM Models](./models.md) - Model selection guide
|
|
11
|
+
- [Examples](./examples.md) - Comprehensive usage examples
|
|
12
|
+
- [Production Guide](./production.md) - Best practices and deployment
|
|
13
|
+
- [Troubleshooting](./troubleshooting.md) - Common issues and solutions
|
|
14
|
+
|
|
15
|
+
## Getting Started
|
|
16
|
+
|
|
17
|
+
If you're new to NTTP, start here:
|
|
18
|
+
|
|
19
|
+
1. **Installation**: `npm install nttp`
|
|
20
|
+
2. **Setup**: `npx nttp setup`
|
|
21
|
+
3. **First Query**: `npx nttp query "show me 5 users"`
|
|
22
|
+
|
|
23
|
+
## Documentation Versions
|
|
24
|
+
|
|
25
|
+
- **CLI Docs**: Run `npx nttp docs [topic]` for searchable inline documentation
|
|
26
|
+
- **Full Guides**: Browse this `/docs` folder for comprehensive guides
|
|
27
|
+
- **Quick Reference**: See main [README.md](../README.md) for quick start
|
|
28
|
+
|
|
29
|
+
## Contributing
|
|
30
|
+
|
|
31
|
+
Found an issue or want to improve the docs? [Open an issue](https://github.com/tylergibbs/nttp/issues) or submit a PR.
|
package/docs/api.md
ADDED
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
Complete API documentation for NTTP.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [NTTP Class](#nttp-class)
|
|
8
|
+
- [Constructor](#constructor)
|
|
9
|
+
- [Static Methods](#static-methods)
|
|
10
|
+
- [Instance Methods](#instance-methods)
|
|
11
|
+
- [Types](#types)
|
|
12
|
+
- [Errors](#errors)
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## NTTP Class
|
|
17
|
+
|
|
18
|
+
### Constructor
|
|
19
|
+
|
|
20
|
+
#### `new NTTP(config: NTTPConfig)`
|
|
21
|
+
|
|
22
|
+
Creates a new NTTP instance with manual configuration.
|
|
23
|
+
|
|
24
|
+
**Parameters:**
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
interface NTTPConfig {
|
|
28
|
+
database: DatabaseConfig;
|
|
29
|
+
llm: LLMConfig;
|
|
30
|
+
cache?: CacheConfig;
|
|
31
|
+
limits?: LimitsConfig;
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Example:**
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { NTTP } from 'nttp';
|
|
39
|
+
|
|
40
|
+
const nttp = new NTTP({
|
|
41
|
+
database: {
|
|
42
|
+
client: 'pg',
|
|
43
|
+
connection: 'postgresql://user:pass@localhost:5432/mydb'
|
|
44
|
+
},
|
|
45
|
+
llm: {
|
|
46
|
+
provider: 'anthropic',
|
|
47
|
+
model: 'claude-sonnet-4-5-20250929',
|
|
48
|
+
apiKey: process.env.ANTHROPIC_API_KEY
|
|
49
|
+
},
|
|
50
|
+
cache: {
|
|
51
|
+
l1: { enabled: true, maxSize: 1000 },
|
|
52
|
+
l2: { enabled: true, provider: 'openai', apiKey: process.env.OPENAI_API_KEY },
|
|
53
|
+
redis: { url: 'redis://localhost:6379' }
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
See [Configuration](./configuration.md) for all config options.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
### Static Methods
|
|
63
|
+
|
|
64
|
+
#### `NTTP.fromEnv(): Promise<NTTP>`
|
|
65
|
+
|
|
66
|
+
Creates an NTTP instance from environment variables. This is the recommended way to initialize NTTP.
|
|
67
|
+
|
|
68
|
+
**Environment Variables Required:**
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
DATABASE_TYPE=pg
|
|
72
|
+
DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
|
|
73
|
+
LLM_PROVIDER=anthropic
|
|
74
|
+
LLM_MODEL=claude-sonnet-4-5-20250929
|
|
75
|
+
ANTHROPIC_API_KEY=sk-ant-...
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Optional Environment Variables:**
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
REDIS_URL=redis://localhost:6379
|
|
82
|
+
OPENAI_API_KEY=sk-... # For L2 semantic cache
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Example:**
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { NTTP } from 'nttp';
|
|
89
|
+
|
|
90
|
+
// Automatically reads from .env file (using dotenv)
|
|
91
|
+
const nttp = await NTTP.fromEnv();
|
|
92
|
+
const result = await nttp.query("show me users");
|
|
93
|
+
await nttp.close();
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Returns:** `Promise<NTTP>` - Initialized NTTP instance (already called `init()`)
|
|
97
|
+
|
|
98
|
+
**Throws:** `Error` if required environment variables are missing
|
|
99
|
+
|
|
100
|
+
See [Configuration](./configuration.md#environment-variables) for complete env var reference.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
### Instance Methods
|
|
105
|
+
|
|
106
|
+
#### `init(): Promise<void>`
|
|
107
|
+
|
|
108
|
+
Initializes the NTTP instance by connecting to the database and building the schema cache.
|
|
109
|
+
|
|
110
|
+
**Required:** Must be called before any queries (unless using `fromEnv()`, which calls this automatically).
|
|
111
|
+
|
|
112
|
+
**Example:**
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const nttp = new NTTP({ /* config */ });
|
|
116
|
+
await nttp.init(); // Connect to database, build schema cache
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Throws:** `Error` if database connection fails
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
#### `query(query: string, options?: QueryOptions): Promise<QueryResult>`
|
|
124
|
+
|
|
125
|
+
Execute a natural language query against the database.
|
|
126
|
+
|
|
127
|
+
**Parameters:**
|
|
128
|
+
|
|
129
|
+
- `query` (string): Natural language query (e.g., "show me active users")
|
|
130
|
+
- `options` (optional):
|
|
131
|
+
```typescript
|
|
132
|
+
interface QueryOptions {
|
|
133
|
+
useCache?: boolean; // Default: true
|
|
134
|
+
forceNewSchema?: boolean; // Default: false
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**Returns:**
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
interface QueryResult {
|
|
142
|
+
query: string; // Original natural language query
|
|
143
|
+
data: any[]; // Query results (array of objects)
|
|
144
|
+
schemaId: string; // Cache key (16-char hash)
|
|
145
|
+
cacheHit: boolean; // true if result came from cache
|
|
146
|
+
intent: Intent; // Parsed intent structure
|
|
147
|
+
sql?: string; // Generated SQL (for debugging)
|
|
148
|
+
params?: any[]; // SQL parameters (for debugging)
|
|
149
|
+
meta?: { // Cache metadata (v2 only)
|
|
150
|
+
cacheLayer: 1 | 2 | 3; // Which cache layer was hit
|
|
151
|
+
cost: number; // Estimated cost in USD
|
|
152
|
+
latency: number; // Query latency in ms
|
|
153
|
+
similarity?: number; // Semantic similarity (L2 only)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**Examples:**
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
// Basic query
|
|
162
|
+
const result = await nttp.query("show me 5 users");
|
|
163
|
+
console.log(result.data); // Array of user objects
|
|
164
|
+
|
|
165
|
+
// Filtered query
|
|
166
|
+
const active = await nttp.query("active users from California");
|
|
167
|
+
|
|
168
|
+
// Aggregation
|
|
169
|
+
const count = await nttp.query("count pending orders");
|
|
170
|
+
|
|
171
|
+
// With options
|
|
172
|
+
const fresh = await nttp.query("show products", {
|
|
173
|
+
useCache: false, // Skip cache, always generate new SQL
|
|
174
|
+
forceNewSchema: true // Force new schema generation
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Check cache performance
|
|
178
|
+
const result = await nttp.query("show users");
|
|
179
|
+
if (result.meta) {
|
|
180
|
+
console.log(`Cache: L${result.meta.cacheLayer}, Cost: $${result.meta.cost}, Latency: ${result.meta.latency}ms`);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
**Throws:**
|
|
185
|
+
- `IntentParseError` - Failed to parse natural language query
|
|
186
|
+
- `SQLGenerationError` - Failed to generate SQL from intent
|
|
187
|
+
- `SQLExecutionError` - Database query execution failed
|
|
188
|
+
|
|
189
|
+
See [Examples](./examples.md) for comprehensive query examples.
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
#### `explain(query: string): Promise<ExplanationResult>`
|
|
194
|
+
|
|
195
|
+
Explains what SQL would be generated for a query without executing it. Useful for debugging and understanding how NTTP interprets queries.
|
|
196
|
+
|
|
197
|
+
**Parameters:**
|
|
198
|
+
|
|
199
|
+
- `query` (string): Natural language query to explain
|
|
200
|
+
|
|
201
|
+
**Returns:**
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
interface ExplanationResult {
|
|
205
|
+
query: string; // Original query
|
|
206
|
+
intent: Intent; // Parsed intent
|
|
207
|
+
sql: string; // Generated SQL
|
|
208
|
+
params: any[]; // SQL parameters
|
|
209
|
+
schemaId: string; // Cache key
|
|
210
|
+
cachedSchema: SchemaDefinition | null; // Cached schema if exists
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
**Example:**
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
const explanation = await nttp.explain("top 10 expensive products");
|
|
218
|
+
|
|
219
|
+
console.log('Intent:', explanation.intent);
|
|
220
|
+
// { entity: 'products', operation: 'list', sort: 'price:desc', limit: 10, ... }
|
|
221
|
+
|
|
222
|
+
console.log('SQL:', explanation.sql);
|
|
223
|
+
// SELECT * FROM products ORDER BY price DESC LIMIT ?
|
|
224
|
+
|
|
225
|
+
console.log('Params:', explanation.params);
|
|
226
|
+
// [10]
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
#### `close(): Promise<void>`
|
|
232
|
+
|
|
233
|
+
Closes the database connection and cleans up resources. Always call this when done using NTTP.
|
|
234
|
+
|
|
235
|
+
**Example:**
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
const nttp = await NTTP.fromEnv();
|
|
239
|
+
try {
|
|
240
|
+
const result = await nttp.query("show users");
|
|
241
|
+
console.log(result.data);
|
|
242
|
+
} finally {
|
|
243
|
+
await nttp.close(); // Clean up
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
### Schema Management
|
|
250
|
+
|
|
251
|
+
#### `listSchemas(): Promise<SchemaDefinition[]>`
|
|
252
|
+
|
|
253
|
+
Lists all cached schemas (query patterns).
|
|
254
|
+
|
|
255
|
+
**Returns:** Array of schema definitions with metadata
|
|
256
|
+
|
|
257
|
+
**Example:**
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
const schemas = await nttp.listSchemas();
|
|
261
|
+
schemas.forEach(schema => {
|
|
262
|
+
console.log(`${schema.schema_id}: ${schema.intent_pattern}`);
|
|
263
|
+
console.log(` Used ${schema.use_count} times`);
|
|
264
|
+
console.log(` Example: ${schema.example_queries[0]}`);
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
#### `getSchema(schemaId: string): Promise<SchemaDefinition | null>`
|
|
271
|
+
|
|
272
|
+
Retrieves a specific cached schema by ID.
|
|
273
|
+
|
|
274
|
+
**Parameters:**
|
|
275
|
+
|
|
276
|
+
- `schemaId` (string): 16-character schema hash
|
|
277
|
+
|
|
278
|
+
**Returns:** Schema definition or null if not found
|
|
279
|
+
|
|
280
|
+
**Example:**
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
const schema = await nttp.getSchema('a1b2c3d4e5f6g7h8');
|
|
284
|
+
if (schema) {
|
|
285
|
+
console.log('SQL:', schema.generated_sql);
|
|
286
|
+
console.log('Used:', schema.use_count);
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
#### `deleteSchema(schemaId: string): Promise<void>`
|
|
293
|
+
|
|
294
|
+
Deletes a cached schema.
|
|
295
|
+
|
|
296
|
+
**Parameters:**
|
|
297
|
+
|
|
298
|
+
- `schemaId` (string): Schema to delete
|
|
299
|
+
|
|
300
|
+
**Example:**
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
await nttp.deleteSchema('a1b2c3d4e5f6g7h8');
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
#### `pinSchema(schemaId: string): Promise<void>`
|
|
309
|
+
|
|
310
|
+
Pins a schema to prevent it from being evicted from cache.
|
|
311
|
+
|
|
312
|
+
**Parameters:**
|
|
313
|
+
|
|
314
|
+
- `schemaId` (string): Schema to pin
|
|
315
|
+
|
|
316
|
+
**Example:**
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
// Pin frequently used query pattern
|
|
320
|
+
await nttp.pinSchema('a1b2c3d4e5f6g7h8');
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
#### `unpinSchema(schemaId: string): Promise<void>`
|
|
326
|
+
|
|
327
|
+
Unpins a previously pinned schema.
|
|
328
|
+
|
|
329
|
+
**Parameters:**
|
|
330
|
+
|
|
331
|
+
- `schemaId` (string): Schema to unpin
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
#### `getCacheStats(): Promise<CacheStats>`
|
|
336
|
+
|
|
337
|
+
Gets cache statistics and performance metrics.
|
|
338
|
+
|
|
339
|
+
**Returns:**
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
interface CacheStats {
|
|
343
|
+
totalSchemas: number;
|
|
344
|
+
pinnedSchemas: number;
|
|
345
|
+
totalUseCount: number;
|
|
346
|
+
averageUseCount: number;
|
|
347
|
+
oldestSchema: Date;
|
|
348
|
+
newestSchema: Date;
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Example:**
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
const stats = await nttp.getCacheStats();
|
|
356
|
+
console.log(`Total cached patterns: ${stats.totalSchemas}`);
|
|
357
|
+
console.log(`Average uses per pattern: ${stats.averageUseCount}`);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
### Database Inspection
|
|
363
|
+
|
|
364
|
+
#### `getTables(): Promise<string[]>`
|
|
365
|
+
|
|
366
|
+
Gets list of all tables in the database.
|
|
367
|
+
|
|
368
|
+
**Returns:** Array of table names
|
|
369
|
+
|
|
370
|
+
**Example:**
|
|
371
|
+
|
|
372
|
+
```typescript
|
|
373
|
+
const tables = await nttp.getTables();
|
|
374
|
+
console.log('Available tables:', tables);
|
|
375
|
+
// ['users', 'products', 'orders', ...]
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
---
|
|
379
|
+
|
|
380
|
+
#### `getTableSchema(tableName: string): Promise<TableSchema>`
|
|
381
|
+
|
|
382
|
+
Gets the schema (columns, types, etc.) for a specific table.
|
|
383
|
+
|
|
384
|
+
**Parameters:**
|
|
385
|
+
|
|
386
|
+
- `tableName` (string): Name of table to inspect
|
|
387
|
+
|
|
388
|
+
**Returns:** Table schema with column definitions
|
|
389
|
+
|
|
390
|
+
**Example:**
|
|
391
|
+
|
|
392
|
+
```typescript
|
|
393
|
+
const schema = await nttp.getTableSchema('users');
|
|
394
|
+
console.log('Columns:', schema.columns);
|
|
395
|
+
// [{ name: 'id', type: 'integer' }, { name: 'email', type: 'string' }, ...]
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
---
|
|
399
|
+
|
|
400
|
+
#### `getSchemaDescription(): string`
|
|
401
|
+
|
|
402
|
+
Gets a human-readable description of the database schema. This is what's sent to the LLM for context.
|
|
403
|
+
|
|
404
|
+
**Returns:** Formatted schema description string
|
|
405
|
+
|
|
406
|
+
**Example:**
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
const description = nttp.getSchemaDescription();
|
|
410
|
+
console.log(description);
|
|
411
|
+
// Tables:
|
|
412
|
+
// - users (id: integer, email: string, name: string, ...)
|
|
413
|
+
// - products (id: integer, name: string, price: decimal, ...)
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
## Types
|
|
419
|
+
|
|
420
|
+
### Intent
|
|
421
|
+
|
|
422
|
+
Structured representation of parsed natural language query.
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
interface Intent {
|
|
426
|
+
entity: string; // Table name
|
|
427
|
+
operation: OperationType; // 'list' | 'count' | 'aggregate' | 'filter'
|
|
428
|
+
filters: Record<string, any>; // Filter conditions
|
|
429
|
+
limit?: number | null; // Result limit
|
|
430
|
+
fields?: string[] | null; // Specific fields to select
|
|
431
|
+
sort?: string | null; // Sort specification (e.g., 'price:desc')
|
|
432
|
+
normalized_text: string; // Normalized representation for caching
|
|
433
|
+
}
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
---
|
|
437
|
+
|
|
438
|
+
### SchemaDefinition
|
|
439
|
+
|
|
440
|
+
Cached query pattern.
|
|
441
|
+
|
|
442
|
+
```typescript
|
|
443
|
+
interface SchemaDefinition {
|
|
444
|
+
schema_id: string; // 16-char hash
|
|
445
|
+
intent_pattern: string; // Normalized intent
|
|
446
|
+
generated_sql: string; // Cached SQL
|
|
447
|
+
sql_params: any[]; // SQL parameters
|
|
448
|
+
result_schema: Record<string, any>; // Result structure
|
|
449
|
+
use_count: number; // Times this pattern was used
|
|
450
|
+
created_at: Date; // When cached
|
|
451
|
+
last_used_at: Date; // Last access time
|
|
452
|
+
example_queries: string[]; // Example natural language queries
|
|
453
|
+
pinned: boolean; // Prevent eviction?
|
|
454
|
+
}
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
---
|
|
458
|
+
|
|
459
|
+
## Errors
|
|
460
|
+
|
|
461
|
+
All NTTP errors include helpful suggestions for resolving issues.
|
|
462
|
+
|
|
463
|
+
### IntentParseError
|
|
464
|
+
|
|
465
|
+
Thrown when the LLM cannot parse the natural language query.
|
|
466
|
+
|
|
467
|
+
```typescript
|
|
468
|
+
try {
|
|
469
|
+
await nttp.query("ambiguous query");
|
|
470
|
+
} catch (error) {
|
|
471
|
+
if (error instanceof IntentParseError) {
|
|
472
|
+
console.error(error.message);
|
|
473
|
+
console.log('Suggestions:', error.suggestions);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
**Common causes:**
|
|
479
|
+
- Query references unknown tables/fields
|
|
480
|
+
- Query is too vague or ambiguous
|
|
481
|
+
- LLM API unavailable or quota exceeded
|
|
482
|
+
|
|
483
|
+
---
|
|
484
|
+
|
|
485
|
+
### SQLGenerationError
|
|
486
|
+
|
|
487
|
+
Thrown when SQL generation fails or violates safety rules.
|
|
488
|
+
|
|
489
|
+
```typescript
|
|
490
|
+
try {
|
|
491
|
+
await nttp.query("complex query");
|
|
492
|
+
} catch (error) {
|
|
493
|
+
if (error instanceof SQLGenerationError) {
|
|
494
|
+
console.error(error.message);
|
|
495
|
+
console.log('Suggestions:', error.suggestions);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
**Common causes:**
|
|
501
|
+
- Complex query requires missing table relationships
|
|
502
|
+
- Generated SQL violates safety rules
|
|
503
|
+
- Schema description incomplete
|
|
504
|
+
|
|
505
|
+
---
|
|
506
|
+
|
|
507
|
+
### SQLExecutionError
|
|
508
|
+
|
|
509
|
+
Thrown when the database rejects the generated SQL.
|
|
510
|
+
|
|
511
|
+
```typescript
|
|
512
|
+
try {
|
|
513
|
+
await nttp.query("show users");
|
|
514
|
+
} catch (error) {
|
|
515
|
+
if (error instanceof SQLExecutionError) {
|
|
516
|
+
console.error(error.message);
|
|
517
|
+
console.log('Generated SQL:', error.sql);
|
|
518
|
+
console.log('Suggestions:', error.suggestions);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
**Common causes:**
|
|
524
|
+
- Database connection issues
|
|
525
|
+
- Table/column doesn't exist (schema mismatch)
|
|
526
|
+
- Type mismatch in WHERE clause
|
|
527
|
+
- Insufficient permissions
|
|
528
|
+
|
|
529
|
+
---
|
|
530
|
+
|
|
531
|
+
### LLMError
|
|
532
|
+
|
|
533
|
+
Thrown when LLM API calls fail.
|
|
534
|
+
|
|
535
|
+
**Common causes:**
|
|
536
|
+
- Invalid or expired API key
|
|
537
|
+
- Rate limit or quota exceeded
|
|
538
|
+
- Network connectivity issues
|
|
539
|
+
- Provider service outage
|
|
540
|
+
|
|
541
|
+
---
|
|
542
|
+
|
|
543
|
+
### CacheError
|
|
544
|
+
|
|
545
|
+
Thrown when cache operations fail.
|
|
546
|
+
|
|
547
|
+
**Common causes:**
|
|
548
|
+
- Redis connection failed
|
|
549
|
+
- Redis authentication error
|
|
550
|
+
- Embedding API failure (L2 cache)
|
|
551
|
+
- Out of memory
|
|
552
|
+
|
|
553
|
+
---
|
|
554
|
+
|
|
555
|
+
## See Also
|
|
556
|
+
|
|
557
|
+
- [Configuration](./configuration.md) - Complete configuration reference
|
|
558
|
+
- [Examples](./examples.md) - Comprehensive usage examples
|
|
559
|
+
- [Troubleshooting](./troubleshooting.md) - Common issues and solutions
|