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,763 @@
1
+ # Configuration Reference
2
+
3
+ Complete reference for all NTTP configuration options.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Environment Variables](#environment-variables)
8
+ - [Configuration Object](#configuration-object)
9
+ - [Database Configuration](#database-configuration)
10
+ - [LLM Configuration](#llm-configuration)
11
+ - [Cache Configuration](#cache-configuration)
12
+ - [Limits Configuration](#limits-configuration)
13
+ - [Configuration Examples](#configuration-examples)
14
+
15
+ ---
16
+
17
+ ## Environment Variables
18
+
19
+ ### Using NTTP.fromEnv()
20
+
21
+ The recommended way to configure NTTP is via environment variables using `NTTP.fromEnv()`.
22
+
23
+ ```typescript
24
+ import { NTTP } from 'nttp';
25
+
26
+ const nttp = await NTTP.fromEnv();
27
+ ```
28
+
29
+ ### Required Environment Variables
30
+
31
+ ```bash
32
+ # Database Configuration
33
+ DATABASE_TYPE=pg # pg | mysql2 | better-sqlite3 | mssql
34
+ DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
35
+
36
+ # For SQLite, use DATABASE_PATH instead of DATABASE_URL
37
+ # DATABASE_TYPE=better-sqlite3
38
+ # DATABASE_PATH=./data.db
39
+
40
+ # LLM Configuration
41
+ LLM_PROVIDER=anthropic # anthropic | openai | cohere | mistral | google
42
+ LLM_MODEL=claude-sonnet-4-5-20250929
43
+ ANTHROPIC_API_KEY=sk-ant-... # Or OPENAI_API_KEY, COHERE_API_KEY, etc.
44
+ ```
45
+
46
+ ### Optional Environment Variables
47
+
48
+ ```bash
49
+ # Redis L1 Cache (recommended for production)
50
+ REDIS_URL=redis://localhost:6379
51
+
52
+ # L2 Semantic Cache (requires OpenAI)
53
+ OPENAI_API_KEY=sk-... # Automatically enables L2 if present
54
+
55
+ # LLM Configuration
56
+ LLM_MAX_TOKENS=2048 # Default: 2048
57
+
58
+ # Cache Configuration
59
+ CACHE_L1_ENABLED=true # Default: true
60
+ CACHE_L1_MAX_SIZE=1000 # Default: 1000
61
+ CACHE_L2_ENABLED=true # Default: false (true if OPENAI_API_KEY set)
62
+ CACHE_L2_MAX_SIZE=500 # Default: 500
63
+ CACHE_L2_SIMILARITY_THRESHOLD=0.85 # Default: 0.85
64
+
65
+ # Query Limits
66
+ MAX_QUERY_LENGTH=500 # Default: 500
67
+ DEFAULT_LIMIT=100 # Default: 100
68
+ MAX_LIMIT=1000 # Default: 1000
69
+ ```
70
+
71
+ ### .env File Example
72
+
73
+ Create a `.env` file in your project root:
74
+
75
+ ```bash
76
+ # Database
77
+ DATABASE_TYPE=pg
78
+ DATABASE_URL=postgresql://user:password@localhost:5432/mydb
79
+
80
+ # LLM
81
+ LLM_PROVIDER=anthropic
82
+ LLM_MODEL=claude-sonnet-4-5-20250929
83
+ ANTHROPIC_API_KEY=sk-ant-api-key-here
84
+
85
+ # Cache (production setup)
86
+ REDIS_URL=redis://localhost:6379
87
+ OPENAI_API_KEY=sk-openai-api-key-here
88
+
89
+ # Limits
90
+ MAX_QUERY_LENGTH=500
91
+ DEFAULT_LIMIT=100
92
+ MAX_LIMIT=1000
93
+ ```
94
+
95
+ Then load with dotenv:
96
+
97
+ ```typescript
98
+ import 'dotenv/config';
99
+ import { NTTP } from 'nttp';
100
+
101
+ const nttp = await NTTP.fromEnv();
102
+ ```
103
+
104
+ ---
105
+
106
+ ## Configuration Object
107
+
108
+ ### Full Configuration Interface
109
+
110
+ ```typescript
111
+ interface NTTPConfig {
112
+ database: DatabaseConfig;
113
+ llm: LLMConfig;
114
+ cache?: CacheConfig;
115
+ limits?: LimitsConfig;
116
+ }
117
+ ```
118
+
119
+ ### Using Manual Configuration
120
+
121
+ ```typescript
122
+ const nttp = new NTTP({
123
+ database: {
124
+ client: 'pg',
125
+ connection: 'postgresql://user:pass@localhost:5432/mydb'
126
+ },
127
+ llm: {
128
+ provider: 'anthropic',
129
+ model: 'claude-sonnet-4-5-20250929',
130
+ apiKey: process.env.ANTHROPIC_API_KEY
131
+ },
132
+ cache: {
133
+ l1: { enabled: true, maxSize: 1000 },
134
+ l2: { enabled: true, provider: 'openai', apiKey: process.env.OPENAI_API_KEY },
135
+ redis: { url: 'redis://localhost:6379' }
136
+ },
137
+ limits: {
138
+ maxQueryLength: 500,
139
+ defaultLimit: 100,
140
+ maxLimit: 1000
141
+ }
142
+ });
143
+
144
+ await nttp.init();
145
+ ```
146
+
147
+ ---
148
+
149
+ ## Database Configuration
150
+
151
+ ### Interface
152
+
153
+ ```typescript
154
+ interface DatabaseConfig {
155
+ client: 'pg' | 'mysql2' | 'better-sqlite3' | 'mssql';
156
+ connection: string | Knex.ConnectionConfig;
157
+ }
158
+ ```
159
+
160
+ ### PostgreSQL
161
+
162
+ ```typescript
163
+ database: {
164
+ client: 'pg',
165
+ connection: 'postgresql://user:password@localhost:5432/database'
166
+ }
167
+
168
+ // Or with detailed config
169
+ database: {
170
+ client: 'pg',
171
+ connection: {
172
+ host: 'localhost',
173
+ port: 5432,
174
+ user: 'myuser',
175
+ password: 'mypassword',
176
+ database: 'mydb',
177
+ ssl: false
178
+ }
179
+ }
180
+ ```
181
+
182
+ **Environment Variables:**
183
+
184
+ ```bash
185
+ DATABASE_TYPE=pg
186
+ DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
187
+ ```
188
+
189
+ ---
190
+
191
+ ### MySQL
192
+
193
+ ```typescript
194
+ database: {
195
+ client: 'mysql2',
196
+ connection: 'mysql://user:password@localhost:3306/database'
197
+ }
198
+
199
+ // Or with detailed config
200
+ database: {
201
+ client: 'mysql2',
202
+ connection: {
203
+ host: 'localhost',
204
+ port: 3306,
205
+ user: 'myuser',
206
+ password: 'mypassword',
207
+ database: 'mydb'
208
+ }
209
+ }
210
+ ```
211
+
212
+ **Environment Variables:**
213
+
214
+ ```bash
215
+ DATABASE_TYPE=mysql2
216
+ DATABASE_URL=mysql://user:pass@localhost:3306/mydb
217
+ ```
218
+
219
+ ---
220
+
221
+ ### SQLite
222
+
223
+ ```typescript
224
+ database: {
225
+ client: 'better-sqlite3',
226
+ connection: {
227
+ filename: './data.db'
228
+ }
229
+ }
230
+ ```
231
+
232
+ **Environment Variables:**
233
+
234
+ ```bash
235
+ DATABASE_TYPE=better-sqlite3
236
+ DATABASE_PATH=./data.db
237
+ ```
238
+
239
+ ---
240
+
241
+ ### SQL Server
242
+
243
+ ```typescript
244
+ database: {
245
+ client: 'mssql',
246
+ connection: {
247
+ server: 'localhost',
248
+ database: 'mydb',
249
+ user: 'sa',
250
+ password: 'password',
251
+ options: {
252
+ encrypt: true,
253
+ trustServerCertificate: true
254
+ }
255
+ }
256
+ }
257
+ ```
258
+
259
+ **Environment Variables:**
260
+
261
+ ```bash
262
+ DATABASE_TYPE=mssql
263
+ DATABASE_URL=Server=localhost;Database=mydb;User Id=sa;Password=pass;
264
+ ```
265
+
266
+ ---
267
+
268
+ ## LLM Configuration
269
+
270
+ ### Interface
271
+
272
+ ```typescript
273
+ interface LLMConfig {
274
+ provider: 'anthropic' | 'openai' | 'cohere' | 'mistral' | 'google';
275
+ model: string;
276
+ apiKey: string;
277
+ maxTokens?: number; // Default: 2048
278
+ }
279
+ ```
280
+
281
+ ### Anthropic (Claude)
282
+
283
+ ```typescript
284
+ llm: {
285
+ provider: 'anthropic',
286
+ model: 'claude-sonnet-4-5-20250929', // Recommended
287
+ apiKey: process.env.ANTHROPIC_API_KEY,
288
+ maxTokens: 2048
289
+ }
290
+ ```
291
+
292
+ **Environment Variables:**
293
+
294
+ ```bash
295
+ LLM_PROVIDER=anthropic
296
+ LLM_MODEL=claude-sonnet-4-5-20250929
297
+ ANTHROPIC_API_KEY=sk-ant-...
298
+ LLM_MAX_TOKENS=2048
299
+ ```
300
+
301
+ **Available Models:**
302
+ - `claude-sonnet-4-5-20250929` (recommended) - Best balance
303
+ - `claude-opus-4-5-20251101` - Most capable, slower
304
+ - `claude-haiku-4-20250514` - Fastest, less capable
305
+
306
+ See [Models Guide](./models.md) for detailed comparison.
307
+
308
+ ---
309
+
310
+ ### OpenAI (GPT)
311
+
312
+ ```typescript
313
+ llm: {
314
+ provider: 'openai',
315
+ model: 'gpt-4o',
316
+ apiKey: process.env.OPENAI_API_KEY,
317
+ maxTokens: 2048
318
+ }
319
+ ```
320
+
321
+ **Environment Variables:**
322
+
323
+ ```bash
324
+ LLM_PROVIDER=openai
325
+ LLM_MODEL=gpt-4o
326
+ OPENAI_API_KEY=sk-...
327
+ ```
328
+
329
+ **Available Models:**
330
+ - `gpt-4o` - Fast and capable
331
+ - `gpt-4-turbo` - Good balance
332
+ - `gpt-3.5-turbo` - Fastest, less accurate
333
+
334
+ ---
335
+
336
+ ### Cohere
337
+
338
+ ```typescript
339
+ llm: {
340
+ provider: 'cohere',
341
+ model: 'command-r-plus',
342
+ apiKey: process.env.COHERE_API_KEY
343
+ }
344
+ ```
345
+
346
+ **Environment Variables:**
347
+
348
+ ```bash
349
+ LLM_PROVIDER=cohere
350
+ LLM_MODEL=command-r-plus
351
+ COHERE_API_KEY=...
352
+ ```
353
+
354
+ ---
355
+
356
+ ### Mistral
357
+
358
+ ```typescript
359
+ llm: {
360
+ provider: 'mistral',
361
+ model: 'mistral-large-latest',
362
+ apiKey: process.env.MISTRAL_API_KEY
363
+ }
364
+ ```
365
+
366
+ **Environment Variables:**
367
+
368
+ ```bash
369
+ LLM_PROVIDER=mistral
370
+ LLM_MODEL=mistral-large-latest
371
+ MISTRAL_API_KEY=...
372
+ ```
373
+
374
+ ---
375
+
376
+ ### Google (Gemini)
377
+
378
+ ```typescript
379
+ llm: {
380
+ provider: 'google',
381
+ model: 'gemini-pro',
382
+ apiKey: process.env.GOOGLE_API_KEY
383
+ }
384
+ ```
385
+
386
+ **Environment Variables:**
387
+
388
+ ```bash
389
+ LLM_PROVIDER=google
390
+ LLM_MODEL=gemini-pro
391
+ GOOGLE_API_KEY=...
392
+ ```
393
+
394
+ ---
395
+
396
+ ## Cache Configuration
397
+
398
+ ### Interface
399
+
400
+ ```typescript
401
+ interface CacheConfig {
402
+ l1?: L1CacheConfig;
403
+ l2?: L2CacheConfig;
404
+ redis?: RedisCacheConfig;
405
+ }
406
+
407
+ interface L1CacheConfig {
408
+ enabled?: boolean; // Default: true
409
+ maxSize?: number; // Default: 1000
410
+ }
411
+
412
+ interface L2CacheConfig {
413
+ enabled?: boolean; // Default: false
414
+ provider?: 'openai'; // Only OpenAI currently
415
+ model?: string; // Default: 'text-embedding-3-small'
416
+ apiKey?: string; // Required if enabled
417
+ maxSize?: number; // Default: 500
418
+ similarityThreshold?: number; // Default: 0.85 (0-1)
419
+ }
420
+
421
+ interface RedisCacheConfig {
422
+ url: string; // Redis connection URL
423
+ }
424
+ ```
425
+
426
+ ### L1 Cache (In-Memory)
427
+
428
+ ```typescript
429
+ cache: {
430
+ l1: {
431
+ enabled: true,
432
+ maxSize: 1000 // Number of queries to cache
433
+ }
434
+ }
435
+ ```
436
+
437
+ **Environment Variables:**
438
+
439
+ ```bash
440
+ CACHE_L1_ENABLED=true
441
+ CACHE_L1_MAX_SIZE=1000
442
+ ```
443
+
444
+ **When to use:**
445
+ - Development
446
+ - Single-instance deployments
447
+ - When cache persistence not needed
448
+
449
+ ---
450
+
451
+ ### L1 Cache (Redis)
452
+
453
+ ```typescript
454
+ cache: {
455
+ l1: {
456
+ enabled: true,
457
+ maxSize: 1000
458
+ },
459
+ redis: {
460
+ url: 'redis://localhost:6379'
461
+ }
462
+ }
463
+ ```
464
+
465
+ **Environment Variables:**
466
+
467
+ ```bash
468
+ CACHE_L1_ENABLED=true
469
+ CACHE_L1_MAX_SIZE=1000
470
+ REDIS_URL=redis://localhost:6379
471
+ ```
472
+
473
+ **Redis URL Formats:**
474
+
475
+ ```bash
476
+ # Basic
477
+ REDIS_URL=redis://localhost:6379
478
+
479
+ # With auth
480
+ REDIS_URL=redis://:password@localhost:6379
481
+
482
+ # With database selection
483
+ REDIS_URL=redis://localhost:6379/0
484
+
485
+ # TLS
486
+ REDIS_URL=rediss://localhost:6380
487
+
488
+ # Cloud (e.g., Upstash)
489
+ REDIS_URL=rediss://default:password@fly-cache.upstash.io:6379
490
+ ```
491
+
492
+ **When to use:**
493
+ - Production deployments
494
+ - CLI tools
495
+ - Multi-instance applications
496
+ - When cache persistence required
497
+
498
+ ---
499
+
500
+ ### L2 Cache (Semantic)
501
+
502
+ ```typescript
503
+ cache: {
504
+ l2: {
505
+ enabled: true,
506
+ provider: 'openai',
507
+ model: 'text-embedding-3-small',
508
+ apiKey: process.env.OPENAI_API_KEY,
509
+ maxSize: 500,
510
+ similarityThreshold: 0.85
511
+ }
512
+ }
513
+ ```
514
+
515
+ **Environment Variables:**
516
+
517
+ ```bash
518
+ CACHE_L2_ENABLED=true
519
+ CACHE_L2_MAX_SIZE=500
520
+ CACHE_L2_SIMILARITY_THRESHOLD=0.85
521
+ OPENAI_API_KEY=sk-...
522
+ ```
523
+
524
+ **Similarity Threshold Guide:**
525
+
526
+ | Threshold | Strictness | Use Case |
527
+ |-----------|------------|----------|
528
+ | 0.95+ | Very strict | Financial, critical queries |
529
+ | 0.85-0.95 | Moderate (recommended) | General purpose |
530
+ | 0.75-0.85 | Loose | High variation, less critical |
531
+ | <0.75 | Too loose | Not recommended |
532
+
533
+ **When to use:**
534
+ - High query variation
535
+ - Customer-facing applications
536
+ - Natural language interfaces
537
+ - When users rephrase queries
538
+
539
+ ---
540
+
541
+ ### Complete Cache Setup
542
+
543
+ ```typescript
544
+ cache: {
545
+ // L1: In-memory + Redis for persistence
546
+ l1: {
547
+ enabled: true,
548
+ maxSize: 1000
549
+ },
550
+ redis: {
551
+ url: 'redis://localhost:6379'
552
+ },
553
+ // L2: Semantic matching
554
+ l2: {
555
+ enabled: true,
556
+ provider: 'openai',
557
+ apiKey: process.env.OPENAI_API_KEY,
558
+ maxSize: 500,
559
+ similarityThreshold: 0.85
560
+ }
561
+ }
562
+ ```
563
+
564
+ ---
565
+
566
+ ## Limits Configuration
567
+
568
+ ### Interface
569
+
570
+ ```typescript
571
+ interface LimitsConfig {
572
+ maxQueryLength?: number; // Default: 500
573
+ defaultLimit?: number; // Default: 100
574
+ maxLimit?: number; // Default: 1000
575
+ }
576
+ ```
577
+
578
+ ### Configuration
579
+
580
+ ```typescript
581
+ limits: {
582
+ maxQueryLength: 500, // Max characters in natural language query
583
+ defaultLimit: 100, // Default LIMIT if not specified in query
584
+ maxLimit: 1000 // Maximum LIMIT allowed
585
+ }
586
+ ```
587
+
588
+ **Environment Variables:**
589
+
590
+ ```bash
591
+ MAX_QUERY_LENGTH=500
592
+ DEFAULT_LIMIT=100
593
+ MAX_LIMIT=1000
594
+ ```
595
+
596
+ ### Field Descriptions
597
+
598
+ **maxQueryLength:**
599
+ - Maximum characters allowed in natural language query
600
+ - Prevents abuse/very long queries
601
+ - Default: 500 characters
602
+
603
+ **defaultLimit:**
604
+ - Default SQL LIMIT if user doesn't specify
605
+ - Applied to queries like "show users" → "LIMIT 100"
606
+ - Default: 100 rows
607
+
608
+ **maxLimit:**
609
+ - Maximum LIMIT allowed even if user requests more
610
+ - "show me 10000 users" → capped at 1000
611
+ - Prevents resource exhaustion
612
+ - Default: 1000 rows
613
+
614
+ ---
615
+
616
+ ## Configuration Examples
617
+
618
+ ### Minimal Development Setup
619
+
620
+ ```typescript
621
+ const nttp = new NTTP({
622
+ database: {
623
+ client: 'better-sqlite3',
624
+ connection: { filename: './dev.db' }
625
+ },
626
+ llm: {
627
+ provider: 'anthropic',
628
+ model: 'claude-haiku-4-20250514', // Fastest for dev
629
+ apiKey: process.env.ANTHROPIC_API_KEY
630
+ }
631
+ });
632
+ ```
633
+
634
+ ---
635
+
636
+ ### Production Setup (Single Instance)
637
+
638
+ ```typescript
639
+ const nttp = new NTTP({
640
+ database: {
641
+ client: 'pg',
642
+ connection: process.env.DATABASE_URL
643
+ },
644
+ llm: {
645
+ provider: 'anthropic',
646
+ model: 'claude-sonnet-4-5-20250929',
647
+ apiKey: process.env.ANTHROPIC_API_KEY
648
+ },
649
+ cache: {
650
+ redis: {
651
+ url: process.env.REDIS_URL
652
+ }
653
+ }
654
+ });
655
+ ```
656
+
657
+ ---
658
+
659
+ ### Production Setup (Multi-Instance with L2)
660
+
661
+ ```typescript
662
+ const nttp = new NTTP({
663
+ database: {
664
+ client: 'pg',
665
+ connection: process.env.DATABASE_URL
666
+ },
667
+ llm: {
668
+ provider: 'anthropic',
669
+ model: 'claude-sonnet-4-5-20250929',
670
+ apiKey: process.env.ANTHROPIC_API_KEY
671
+ },
672
+ cache: {
673
+ l1: { maxSize: 1000 },
674
+ redis: { url: process.env.REDIS_URL },
675
+ l2: {
676
+ enabled: true,
677
+ provider: 'openai',
678
+ apiKey: process.env.OPENAI_API_KEY,
679
+ similarityThreshold: 0.85
680
+ }
681
+ },
682
+ limits: {
683
+ maxQueryLength: 500,
684
+ defaultLimit: 100,
685
+ maxLimit: 1000
686
+ }
687
+ });
688
+ ```
689
+
690
+ ---
691
+
692
+ ### CLI Tool Setup
693
+
694
+ ```bash
695
+ # .env file
696
+ DATABASE_TYPE=pg
697
+ DATABASE_URL=postgresql://...
698
+ LLM_PROVIDER=anthropic
699
+ LLM_MODEL=claude-sonnet-4-5-20250929
700
+ ANTHROPIC_API_KEY=sk-ant-...
701
+ REDIS_URL=redis://localhost:6379
702
+ ```
703
+
704
+ ```typescript
705
+ // Uses NTTP.fromEnv() automatically
706
+ import { NTTP } from 'nttp';
707
+
708
+ const nttp = await NTTP.fromEnv();
709
+ const result = await nttp.query(process.argv[2]);
710
+ console.log(result.data);
711
+ await nttp.close();
712
+ ```
713
+
714
+ ---
715
+
716
+ ### Customer-Facing Application
717
+
718
+ ```typescript
719
+ const nttp = new NTTP({
720
+ database: {
721
+ client: 'pg',
722
+ connection: {
723
+ host: process.env.DB_HOST,
724
+ port: 5432,
725
+ user: process.env.DB_USER,
726
+ password: process.env.DB_PASSWORD,
727
+ database: process.env.DB_NAME,
728
+ ssl: { rejectUnauthorized: false }
729
+ }
730
+ },
731
+ llm: {
732
+ provider: 'anthropic',
733
+ model: 'claude-sonnet-4-5-20250929',
734
+ apiKey: process.env.ANTHROPIC_API_KEY,
735
+ maxTokens: 2048
736
+ },
737
+ cache: {
738
+ l1: { enabled: true, maxSize: 2000 },
739
+ redis: { url: process.env.REDIS_URL },
740
+ l2: {
741
+ enabled: true,
742
+ provider: 'openai',
743
+ apiKey: process.env.OPENAI_API_KEY,
744
+ maxSize: 1000,
745
+ similarityThreshold: 0.85 // Handle query variations
746
+ }
747
+ },
748
+ limits: {
749
+ maxQueryLength: 300, // Reasonable for customer queries
750
+ defaultLimit: 50, // Smaller default for UX
751
+ maxLimit: 500 // Prevent large result sets
752
+ }
753
+ });
754
+ ```
755
+
756
+ ---
757
+
758
+ ## See Also
759
+
760
+ - [Caching Guide](./caching.md) - Deep dive into cache system
761
+ - [Models Guide](./models.md) - LLM selection guide
762
+ - [Production Guide](./production.md) - Deployment best practices
763
+ - [API Reference](./api.md) - Complete API documentation