nttp 1.4.11 → 1.4.15

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.
@@ -0,0 +1,694 @@
1
+ # Troubleshooting Guide
2
+
3
+ Common issues and solutions for NTTP.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Setup Issues](#setup-issues)
8
+ - [Query Issues](#query-issues)
9
+ - [Cache Issues](#cache-issues)
10
+ - [Connection Issues](#connection-issues)
11
+ - [Performance Issues](#performance-issues)
12
+ - [Error Messages](#error-messages)
13
+
14
+ ---
15
+
16
+ ## Setup Issues
17
+
18
+ ### "Cannot find package 'nttp'"
19
+
20
+ **Problem:** Import fails after running setup wizard.
21
+
22
+ **Cause:** Dependencies not installed.
23
+
24
+ **Solution:**
25
+
26
+ ```bash
27
+ npm install nttp dotenv
28
+ ```
29
+
30
+ The setup wizard should do this automatically, but manual installation may be needed.
31
+
32
+ ---
33
+
34
+ ### "Cannot find package 'dotenv'"
35
+
36
+ **Problem:** Environment variable loading fails.
37
+
38
+ **Cause:** `dotenv` package not installed.
39
+
40
+ **Solution:**
41
+
42
+ ```bash
43
+ npm install dotenv
44
+ ```
45
+
46
+ Then import at the top of your file:
47
+
48
+ ```typescript
49
+ import 'dotenv/config';
50
+ ```
51
+
52
+ ---
53
+
54
+ ### "DATABASE_URL is required"
55
+
56
+ **Problem:** `NTTP.fromEnv()` fails with missing environment variable error.
57
+
58
+ **Cause:** `.env` file not loaded or incorrect.
59
+
60
+ **Solution:**
61
+
62
+ 1. Verify `.env` file exists in project root
63
+ 2. Check `.env` has required variables:
64
+
65
+ ```bash
66
+ DATABASE_TYPE=pg
67
+ DATABASE_URL=postgresql://...
68
+ LLM_PROVIDER=anthropic
69
+ LLM_MODEL=claude-sonnet-4-5-20250929
70
+ ANTHROPIC_API_KEY=sk-ant-...
71
+ ```
72
+
73
+ 3. Ensure `dotenv` is loaded:
74
+
75
+ ```typescript
76
+ import 'dotenv/config'; // Add this FIRST
77
+ import { NTTP } from 'nttp';
78
+ ```
79
+
80
+ ---
81
+
82
+ ## Query Issues
83
+
84
+ ### Query Returns Empty Results
85
+
86
+ **Problem:** Query executes but returns `[]`.
87
+
88
+ **Possible Causes:**
89
+
90
+ 1. **No matching data** - Query filters exclude all rows
91
+ 2. **Table doesn't exist** - Entity name incorrect
92
+ 3. **Schema mismatch** - Database structure changed
93
+
94
+ **Debug Steps:**
95
+
96
+ ```typescript
97
+ // 1. Check what SQL was generated
98
+ const explanation = await nttp.explain("your query");
99
+ console.log('SQL:', explanation.sql);
100
+ console.log('Params:', explanation.params);
101
+
102
+ // 2. Verify table exists
103
+ const tables = await nttp.getTables();
104
+ console.log('Available tables:', tables);
105
+
106
+ // 3. Check table schema
107
+ const schema = await nttp.getTableSchema('users');
108
+ console.log('Columns:', schema);
109
+
110
+ // 4. Try simpler query
111
+ const all = await nttp.query("show all users");
112
+ console.log('Total rows:', all.data.length);
113
+ ```
114
+
115
+ **Solutions:**
116
+
117
+ - Simplify query: `"show all users"` instead of `"active premium users from California"`
118
+ - Verify table name matches database
119
+ - Check filters aren't too restrictive
120
+
121
+ ---
122
+
123
+ ### "Failed to understand query" (IntentParseError)
124
+
125
+ **Problem:** LLM cannot parse natural language query.
126
+
127
+ **Example Error:**
128
+
129
+ ```
130
+ IntentParseError: Failed to understand query
131
+
132
+ Suggested fixes:
133
+ • Simplify your query (e.g., "show users" instead of complex phrasing)
134
+ • Ensure table/field names match your database schema
135
+ • Try a more explicit query (e.g., "list all products")
136
+ • Check if LLM API key is valid and has quota available
137
+ ```
138
+
139
+ **Solutions:**
140
+
141
+ **1. Simplify query:**
142
+
143
+ ```typescript
144
+ // ❌ Too complex
145
+ "give me all the users who are currently active and have premium subscriptions"
146
+
147
+ // ✅ Simpler
148
+ "active premium users"
149
+ ```
150
+
151
+ **2. Use explicit table names:**
152
+
153
+ ```typescript
154
+ // ❌ Ambiguous
155
+ "show me the data"
156
+
157
+ // ✅ Explicit
158
+ "show me all users"
159
+ ```
160
+
161
+ **3. Check API key:**
162
+
163
+ ```bash
164
+ # Verify key is set
165
+ echo $ANTHROPIC_API_KEY
166
+
167
+ # Test with explain (faster than full query)
168
+ const test = await nttp.explain("show users");
169
+ ```
170
+
171
+ ---
172
+
173
+ ### "Could not generate SQL" (SQLGenerationError)
174
+
175
+ **Problem:** Intent parsed but SQL generation failed.
176
+
177
+ **Possible Causes:**
178
+
179
+ 1. Complex query requires table relationships not in schema
180
+ 2. Schema description incomplete
181
+ 3. LLM model not capable enough
182
+
183
+ **Solutions:**
184
+
185
+ **1. Check schema is complete:**
186
+
187
+ ```typescript
188
+ const description = nttp.getSchemaDescription();
189
+ console.log(description);
190
+ // Ensure all tables and relationships are present
191
+ ```
192
+
193
+ **2. Try simpler query:**
194
+
195
+ ```typescript
196
+ // ❌ Complex join
197
+ "show users with their order counts and average order values"
198
+
199
+ // ✅ Simpler
200
+ "show users"
201
+ ```
202
+
203
+ **3. Use more capable model:**
204
+
205
+ ```typescript
206
+ llm: {
207
+ provider: 'anthropic',
208
+ model: 'claude-opus-4-5-20251101' // More capable
209
+ }
210
+ ```
211
+
212
+ ---
213
+
214
+ ### "Query failed" (SQLExecutionError)
215
+
216
+ **Problem:** Generated SQL fails when executed.
217
+
218
+ **Example Error:**
219
+
220
+ ```
221
+ SQLExecutionError: Query failed: column "status" does not exist
222
+
223
+ Generated SQL:
224
+ SELECT * FROM users WHERE status = ? LIMIT ?
225
+
226
+ Suggested fixes:
227
+ • Verify database connection is active (check DATABASE_URL)
228
+ • Ensure schema matches actual database structure
229
+ • Check database user has SELECT permissions on the table
230
+ • Examine the generated SQL for syntax errors
231
+ • Try regenerating with forceNewSchema: true option
232
+ ```
233
+
234
+ **Solutions:**
235
+
236
+ **1. Verify column exists:**
237
+
238
+ ```typescript
239
+ const schema = await nttp.getTableSchema('users');
240
+ console.log('Columns:', schema.columns.map(c => c.name));
241
+ // Check if 'status' column exists
242
+ ```
243
+
244
+ **2. Force schema refresh:**
245
+
246
+ ```typescript
247
+ // Clear cache and regenerate
248
+ const result = await nttp.query("show users", {
249
+ forceNewSchema: true
250
+ });
251
+ ```
252
+
253
+ **3. Check database connection:**
254
+
255
+ ```typescript
256
+ try {
257
+ const tables = await nttp.getTables();
258
+ console.log('✓ Database connected');
259
+ } catch (error) {
260
+ console.error('✗ Database connection failed:', error.message);
261
+ }
262
+ ```
263
+
264
+ ---
265
+
266
+ ## Cache Issues
267
+
268
+ ### Cache Always Shows MISS
269
+
270
+ **Problem:** Every query is L3 MISS even for repeated queries.
271
+
272
+ **Possible Causes:**
273
+
274
+ 1. Redis not configured (for production)
275
+ 2. Redis connection failed
276
+ 3. Cache disabled
277
+ 4. Query variations (need L2 cache)
278
+
279
+ **Debug Steps:**
280
+
281
+ ```typescript
282
+ // 1. Check cache config
283
+ console.log('Redis URL:', process.env.REDIS_URL);
284
+
285
+ // 2. Try exact same query twice
286
+ const result1 = await nttp.query("show active users");
287
+ console.log('First query:', result1.meta);
288
+
289
+ const result2 = await nttp.query("show active users");
290
+ console.log('Second query:', result2.meta);
291
+ // Should be L1 HIT on second query
292
+
293
+ // 3. Check cache stats
294
+ const stats = await nttp.getCacheStats();
295
+ console.log('Cache stats:', stats);
296
+ ```
297
+
298
+ **Solutions:**
299
+
300
+ **1. Enable Redis:**
301
+
302
+ ```bash
303
+ # Add to .env
304
+ REDIS_URL=redis://localhost:6379
305
+ ```
306
+
307
+ **2. Verify Redis is running:**
308
+
309
+ ```bash
310
+ # Test Redis connection
311
+ redis-cli ping
312
+ # Should return: PONG
313
+ ```
314
+
315
+ **3. Enable L2 for query variations:**
316
+
317
+ ```bash
318
+ # Add to .env
319
+ OPENAI_API_KEY=sk-...
320
+ ```
321
+
322
+ ---
323
+
324
+ ### Redis Connection Failed
325
+
326
+ **Problem:** "Redis connection failed" or cache not persisting.
327
+
328
+ **Error Example:**
329
+
330
+ ```
331
+ CacheError: Redis connection failed: ECONNREFUSED
332
+
333
+ Suggested fixes:
334
+ • Verify Redis server is running (if using Redis)
335
+ • Check REDIS_URL format: redis://host:port
336
+ • Ensure Redis authentication credentials are correct
337
+ ```
338
+
339
+ **Solutions:**
340
+
341
+ **1. Verify Redis is running:**
342
+
343
+ ```bash
344
+ # Check if Redis is running
345
+ redis-cli ping
346
+
347
+ # Start Redis if not running
348
+ redis-server
349
+ ```
350
+
351
+ **2. Check URL format:**
352
+
353
+ ```bash
354
+ # Correct formats
355
+ REDIS_URL=redis://localhost:6379
356
+ REDIS_URL=redis://:password@localhost:6379
357
+ REDIS_URL=redis://localhost:6379/0
358
+
359
+ # Wrong
360
+ REDIS_URL=localhost:6379 # Missing redis://
361
+ ```
362
+
363
+ **3. Check authentication:**
364
+
365
+ ```bash
366
+ # If Redis requires password
367
+ REDIS_URL=redis://:your-password@localhost:6379
368
+ ```
369
+
370
+ **4. Test connection manually:**
371
+
372
+ ```typescript
373
+ import Redis from 'ioredis';
374
+
375
+ const redis = new Redis(process.env.REDIS_URL);
376
+
377
+ redis.ping().then(() => {
378
+ console.log('✓ Redis connected');
379
+ }).catch((error) => {
380
+ console.error('✗ Redis connection failed:', error.message);
381
+ });
382
+ ```
383
+
384
+ ---
385
+
386
+ ## Connection Issues
387
+
388
+ ### Database Connection Failed
389
+
390
+ **Problem:** Cannot connect to database.
391
+
392
+ **Error Example:**
393
+
394
+ ```
395
+ Error: connect ECONNREFUSED 127.0.0.1:5432
396
+ ```
397
+
398
+ **Solutions:**
399
+
400
+ **1. Verify database is running:**
401
+
402
+ ```bash
403
+ # PostgreSQL
404
+ pg_isready
405
+
406
+ # MySQL
407
+ mysqladmin ping
408
+ ```
409
+
410
+ **2. Check connection URL:**
411
+
412
+ ```bash
413
+ # PostgreSQL
414
+ DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
415
+
416
+ # MySQL
417
+ DATABASE_URL=mysql://user:pass@localhost:3306/mydb
418
+
419
+ # SQLite
420
+ DATABASE_PATH=./data.db
421
+ ```
422
+
423
+ **3. Test connection:**
424
+
425
+ ```typescript
426
+ import knex from 'knex';
427
+
428
+ const db = knex({
429
+ client: 'pg',
430
+ connection: process.env.DATABASE_URL
431
+ });
432
+
433
+ db.raw('SELECT 1').then(() => {
434
+ console.log('✓ Database connected');
435
+ }).catch((error) => {
436
+ console.error('✗ Database connection failed:', error.message);
437
+ });
438
+ ```
439
+
440
+ **4. Check firewall/network:**
441
+
442
+ ```bash
443
+ # Test if port is accessible
444
+ telnet localhost 5432
445
+ ```
446
+
447
+ ---
448
+
449
+ ### LLM API Failed
450
+
451
+ **Problem:** LLM API calls failing.
452
+
453
+ **Error Example:**
454
+
455
+ ```
456
+ LLMError: LLM API failed after 3 attempts: Invalid API key
457
+
458
+ Suggested fixes:
459
+ • Verify API key is correct (check ANTHROPIC_API_KEY, OPENAI_API_KEY, etc.)
460
+ • Check API quota and rate limits with your provider
461
+ • Ensure network connectivity to the LLM provider
462
+ ```
463
+
464
+ **Solutions:**
465
+
466
+ **1. Verify API key:**
467
+
468
+ ```bash
469
+ # Check key is set
470
+ echo $ANTHROPIC_API_KEY
471
+
472
+ # Test with curl
473
+ curl https://api.anthropic.com/v1/messages \
474
+ -H "x-api-key: $ANTHROPIC_API_KEY" \
475
+ -H "anthropic-version: 2023-06-01" \
476
+ -H "content-type: application/json" \
477
+ -d '{"model":"claude-sonnet-4-5-20250929","messages":[{"role":"user","content":"test"}],"max_tokens":10}'
478
+ ```
479
+
480
+ **2. Check quota:**
481
+
482
+ - Visit your provider's dashboard
483
+ - Anthropic: https://console.anthropic.com/
484
+ - OpenAI: https://platform.openai.com/usage
485
+
486
+ **3. Check rate limits:**
487
+
488
+ ```typescript
489
+ // Add delay between requests if hitting rate limits
490
+ async function queryWithDelay(query: string) {
491
+ await new Promise(resolve => setTimeout(resolve, 1000)); // 1s delay
492
+ return nttp.query(query);
493
+ }
494
+ ```
495
+
496
+ ---
497
+
498
+ ## Performance Issues
499
+
500
+ ### Queries Too Slow
501
+
502
+ **Problem:** Queries taking 10+ seconds.
503
+
504
+ **Possible Causes:**
505
+
506
+ 1. No caching (first query)
507
+ 2. Very complex query
508
+ 3. Large result set
509
+ 4. Slow LLM model
510
+ 5. Database slow
511
+
512
+ **Debug Steps:**
513
+
514
+ ```typescript
515
+ const result = await nttp.query("your query");
516
+
517
+ console.log('Cache hit:', result.cacheHit);
518
+ console.log('Cache layer:', result.meta?.cacheLayer);
519
+ console.log('Latency:', result.meta?.latency);
520
+ console.log('Row count:', result.data.length);
521
+ console.log('SQL:', result.sql);
522
+ ```
523
+
524
+ **Solutions:**
525
+
526
+ **1. Enable caching:**
527
+
528
+ ```bash
529
+ # Add Redis for persistent cache
530
+ REDIS_URL=redis://localhost:6379
531
+
532
+ # Add L2 for query variations
533
+ OPENAI_API_KEY=sk-...
534
+ ```
535
+
536
+ **2. Pre-warm cache:**
537
+
538
+ ```typescript
539
+ // Warm cache on startup
540
+ const common = ["show users", "count orders", "show products"];
541
+ for (const q of common) {
542
+ await nttp.query(q);
543
+ }
544
+ ```
545
+
546
+ **3. Use faster model:**
547
+
548
+ ```typescript
549
+ llm: {
550
+ model: 'claude-haiku-4-20250514' // Faster
551
+ }
552
+ ```
553
+
554
+ **4. Reduce result size:**
555
+
556
+ ```typescript
557
+ limits: {
558
+ defaultLimit: 50, // Smaller default
559
+ maxLimit: 200 // Lower max
560
+ }
561
+ ```
562
+
563
+ **5. Add database indexes:**
564
+
565
+ ```sql
566
+ -- Index frequently filtered columns
567
+ CREATE INDEX idx_users_status ON users(status);
568
+ CREATE INDEX idx_orders_created_at ON orders(created_at);
569
+ ```
570
+
571
+ ---
572
+
573
+ ## Error Messages
574
+
575
+ ### "ECONNREFUSED"
576
+
577
+ **Meaning:** Cannot connect to service (database, Redis, etc.)
578
+
579
+ **Check:**
580
+ - Service is running
581
+ - Correct host/port
582
+ - Firewall rules
583
+
584
+ ---
585
+
586
+ ### "ETIMEDOUT"
587
+
588
+ **Meaning:** Connection timeout
589
+
590
+ **Check:**
591
+ - Network connectivity
592
+ - Firewall blocking connection
593
+ - Service is responsive
594
+
595
+ ---
596
+
597
+ ### "Invalid API key"
598
+
599
+ **Meaning:** LLM API key incorrect or expired
600
+
601
+ **Check:**
602
+ - API key is correct
603
+ - API key has proper permissions
604
+ - API key not expired
605
+
606
+ ---
607
+
608
+ ### "Rate limit exceeded"
609
+
610
+ **Meaning:** Too many API requests
611
+
612
+ **Solutions:**
613
+ - Wait and retry
614
+ - Upgrade API plan
615
+ - Add delays between requests
616
+ - Improve cache hit rate
617
+
618
+ ---
619
+
620
+ ### "Out of quota"
621
+
622
+ **Meaning:** API usage quota exhausted
623
+
624
+ **Solutions:**
625
+ - Upgrade API plan
626
+ - Wait for quota reset
627
+ - Use caching to reduce API calls
628
+
629
+ ---
630
+
631
+ ## Getting Help
632
+
633
+ ### Debugging Checklist
634
+
635
+ Before asking for help, try:
636
+
637
+ 1. ✅ Check this troubleshooting guide
638
+ 2. ✅ Run with verbose logging
639
+ 3. ✅ Test with minimal example
640
+ 4. ✅ Verify all services are running
641
+ 5. ✅ Check environment variables
642
+ 6. ✅ Try `npx nttp docs` for quick reference
643
+
644
+ ---
645
+
646
+ ### Collecting Debug Info
647
+
648
+ ```typescript
649
+ // Collect diagnostic information
650
+ console.log('NTTP Debug Info:');
651
+ console.log('- Node version:', process.version);
652
+ console.log('- DATABASE_TYPE:', process.env.DATABASE_TYPE);
653
+ console.log('- LLM_PROVIDER:', process.env.LLM_PROVIDER);
654
+ console.log('- LLM_MODEL:', process.env.LLM_MODEL);
655
+ console.log('- REDIS_URL set:', !!process.env.REDIS_URL);
656
+ console.log('- OPENAI_API_KEY set:', !!process.env.OPENAI_API_KEY);
657
+
658
+ try {
659
+ const tables = await nttp.getTables();
660
+ console.log('- Database tables:', tables.length);
661
+ } catch (error) {
662
+ console.log('- Database connection: FAILED');
663
+ }
664
+
665
+ try {
666
+ const stats = await nttp.getCacheStats();
667
+ console.log('- Cache schemas:', stats.totalSchemas);
668
+ } catch (error) {
669
+ console.log('- Cache: FAILED');
670
+ }
671
+ ```
672
+
673
+ ---
674
+
675
+ ### Reporting Issues
676
+
677
+ When reporting issues, include:
678
+
679
+ 1. **Error message** (full stack trace)
680
+ 2. **Debug info** (from above)
681
+ 3. **Minimal reproducible example**
682
+ 4. **Expected vs actual behavior**
683
+ 5. **NTTP version** (`npm list nttp`)
684
+
685
+ **Report at:** https://github.com/tylergibbs/nttp/issues
686
+
687
+ ---
688
+
689
+ ## See Also
690
+
691
+ - [Configuration](./configuration.md) - Configuration reference
692
+ - [API Reference](./api.md) - Complete API documentation
693
+ - [Production Guide](./production.md) - Production best practices
694
+ - [Examples](./examples.md) - Usage examples
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nttp",
3
- "version": "1.4.11",
3
+ "version": "1.4.15",
4
4
  "description": "natural text to query - Query databases with natural language",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "files": [
18
18
  "dist",
19
+ "docs",
19
20
  "README.md"
20
21
  ],
21
22
  "scripts": {