@toolpack-sdk/knowledge 1.3.0 → 2.0.0-alpha.1

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/dist/index.d.cts CHANGED
@@ -14,6 +14,8 @@ interface QueryOptions {
14
14
  filter?: MetadataFilter;
15
15
  includeMetadata?: boolean;
16
16
  includeVectors?: boolean;
17
+ searchType?: 'semantic' | 'keyword' | 'hybrid';
18
+ semanticWeight?: number;
17
19
  }
18
20
  interface MetadataFilter {
19
21
  [key: string]: string | number | boolean | {
@@ -32,9 +34,11 @@ interface QueryResult {
32
34
  interface KnowledgeProvider {
33
35
  add(chunks: Chunk[]): Promise<void>;
34
36
  query(queryVector: number[], options?: QueryOptions): Promise<QueryResult[]>;
37
+ keywordQuery?(query: string, options?: QueryOptions): Promise<QueryResult[]>;
35
38
  delete(ids: string[]): Promise<void>;
36
39
  clear(): Promise<void>;
37
40
  validateDimensions(dimensions: number): Promise<void>;
41
+ getAllChunks?(): Promise<Chunk[]>;
38
42
  close?(): void;
39
43
  }
40
44
  interface KnowledgeSource {
@@ -81,6 +85,7 @@ interface KnowledgeOptions {
81
85
  onError?: ErrorHandler;
82
86
  onSync?: SyncEventHandler;
83
87
  onEmbeddingProgress?: EmbeddingProgressHandler;
88
+ streamingBatchSize?: number;
84
89
  }
85
90
  type ErrorHandler = (error: Error, context: {
86
91
  file?: string;
@@ -109,8 +114,20 @@ declare class Knowledge {
109
114
  private constructor();
110
115
  static create(options: KnowledgeOptions): Promise<Knowledge>;
111
116
  query(text: string, options?: QueryOptions): Promise<QueryResult[]>;
117
+ private semanticQuery;
118
+ private keywordQuery;
119
+ private combineHybridResults;
120
+ private getAllChunks;
112
121
  sync(): Promise<void>;
113
122
  private embedChunks;
123
+ /**
124
+ * Add a single content item to the knowledge base without triggering a full re-sync.
125
+ * This is useful for runtime additions like conversation history or agent state.
126
+ * @param content The text content to add
127
+ * @param metadata Optional metadata to attach to the chunk
128
+ * @returns The ID of the added chunk
129
+ */
130
+ add(content: string, metadata?: Record<string, unknown>): Promise<string>;
114
131
  stop(): Promise<void>;
115
132
  toTool(): KnowledgeTool;
116
133
  }
@@ -156,8 +173,10 @@ declare class MemoryProvider implements KnowledgeProvider {
156
173
  validateDimensions(dimensions: number): Promise<void>;
157
174
  add(chunks: Chunk[]): Promise<void>;
158
175
  query(queryVector: number[], options?: QueryOptions): Promise<QueryResult[]>;
176
+ keywordQuery(query: string, options?: QueryOptions): Promise<QueryResult[]>;
159
177
  delete(ids: string[]): Promise<void>;
160
178
  clear(): Promise<void>;
179
+ getAllChunks(): Promise<Chunk[]>;
161
180
  }
162
181
 
163
182
  interface PersistentKnowledgeProviderOptions {
@@ -176,8 +195,10 @@ declare class PersistentKnowledgeProvider implements KnowledgeProvider {
176
195
  validateDimensions(dimensions: number): Promise<void>;
177
196
  add(chunks: Chunk[]): Promise<void>;
178
197
  query(queryVector: number[], options?: QueryOptions): Promise<QueryResult[]>;
198
+ keywordQuery(query: string, options?: QueryOptions): Promise<QueryResult[]>;
179
199
  delete(ids: string[]): Promise<void>;
180
200
  clear(): Promise<void>;
201
+ getAllChunks(): Promise<Chunk[]>;
181
202
  shouldReSync(): boolean;
182
203
  close(): void;
183
204
  }
@@ -201,6 +222,140 @@ declare class MarkdownSource implements KnowledgeSource {
201
222
  private generateChunkId;
202
223
  }
203
224
 
225
+ interface WebUrlSourceOptions {
226
+ maxChunkSize?: number;
227
+ chunkOverlap?: number;
228
+ minChunkSize?: number;
229
+ namespace?: string;
230
+ metadata?: Record<string, unknown>;
231
+ maxDepth?: number;
232
+ userAgent?: string;
233
+ delayMs?: number;
234
+ timeoutMs?: number;
235
+ sameDomainOnly?: boolean;
236
+ maxPagesPerDomain?: number;
237
+ }
238
+ declare class WebUrlSource implements KnowledgeSource {
239
+ private urls;
240
+ private options;
241
+ private crawledUrls;
242
+ private domainPageCount;
243
+ private lastRequestTime;
244
+ constructor(urls: string[], options?: WebUrlSourceOptions);
245
+ load(): AsyncIterable<Chunk>;
246
+ private crawlUrls;
247
+ private fetchPage;
248
+ private chunkPage;
249
+ private generateChunkId;
250
+ }
251
+
252
+ interface ApiDataSourceOptions {
253
+ maxChunkSize?: number;
254
+ chunkOverlap?: number;
255
+ minChunkSize?: number;
256
+ namespace?: string;
257
+ metadata?: Record<string, unknown>;
258
+ headers?: Record<string, string>;
259
+ method?: 'GET' | 'POST';
260
+ body?: unknown;
261
+ timeoutMs?: number;
262
+ pagination?: {
263
+ param: string;
264
+ start: number;
265
+ step: number;
266
+ maxPages?: number;
267
+ } | null;
268
+ dataPath?: string;
269
+ contentExtractor?: (item: unknown) => string;
270
+ metadataExtractor?: (item: unknown) => Record<string, unknown>;
271
+ }
272
+ declare class ApiDataSource implements KnowledgeSource {
273
+ private url;
274
+ private options;
275
+ constructor(url: string, options?: ApiDataSourceOptions);
276
+ load(): AsyncIterable<Chunk>;
277
+ private fetchData;
278
+ private buildUrl;
279
+ private fetchPage;
280
+ private extractItems;
281
+ private chunkItem;
282
+ private defaultContentExtractor;
283
+ private defaultMetadataExtractor;
284
+ private generateChunkId;
285
+ }
286
+
287
+ interface JSONSourceOptions {
288
+ namespace?: string;
289
+ metadata?: Record<string, unknown>;
290
+ filter?: (item: unknown) => boolean;
291
+ chunkSize?: number;
292
+ /** Required. Transform each JSON item into a human-readable string for AI embedding. */
293
+ toContent: (item: unknown) => string;
294
+ }
295
+ /**
296
+ * Knowledge source for JSON files.
297
+ * Supports jq-like filtering and chunking of large arrays.
298
+ */
299
+ declare class JSONSource implements KnowledgeSource {
300
+ private filePath;
301
+ private options;
302
+ constructor(filePath: string, options: JSONSourceOptions);
303
+ load(): AsyncIterable<Chunk>;
304
+ }
305
+
306
+ interface SQLiteSourceOptions {
307
+ namespace?: string;
308
+ metadata?: Record<string, unknown>;
309
+ query?: string;
310
+ chunkSize?: number;
311
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
312
+ toContent: (row: Record<string, unknown>) => string;
313
+ preLoadCSV?: {
314
+ tableName: string;
315
+ csvPath: string;
316
+ delimiter?: string;
317
+ headers?: boolean;
318
+ };
319
+ }
320
+ /**
321
+ * Knowledge source for SQLite databases.
322
+ * Supports SQL queries and optional CSV/TSV pre-loading.
323
+ * Note: This requires the 'better-sqlite3' package to be installed.
324
+ */
325
+ declare class SQLiteSource implements KnowledgeSource {
326
+ private dbPath;
327
+ private options;
328
+ constructor(dbPath: string, options: SQLiteSourceOptions);
329
+ load(): AsyncIterable<Chunk>;
330
+ private loadCSV;
331
+ }
332
+
333
+ interface PostgresSourceOptions {
334
+ namespace?: string;
335
+ metadata?: Record<string, unknown>;
336
+ query: string;
337
+ chunkSize?: number;
338
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
339
+ toContent: (row: Record<string, unknown>) => string;
340
+ connectionString?: string;
341
+ host?: string;
342
+ port?: number;
343
+ database?: string;
344
+ user?: string;
345
+ password?: string;
346
+ ssl?: boolean;
347
+ }
348
+ /**
349
+ * Knowledge source for PostgreSQL databases.
350
+ * Supports SQL queries with optional chunking.
351
+ * Note: This requires the 'pg' package to be installed.
352
+ */
353
+ declare class PostgresSource implements KnowledgeSource {
354
+ private options;
355
+ constructor(options: PostgresSourceOptions);
356
+ load(): AsyncIterable<Chunk>;
357
+ }
358
+
204
359
  interface OllamaEmbedderOptions {
205
360
  model: string;
206
361
  baseUrl?: string;
@@ -236,4 +391,26 @@ declare class OpenAIEmbedder implements Embedder {
236
391
  embedBatch(texts: string[]): Promise<number[][]>;
237
392
  }
238
393
 
239
- export { type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, type QueryOptions, type QueryResult, type SyncEvent, type SyncEventHandler };
394
+ interface OpenRouterEmbedderOptions {
395
+ model: string;
396
+ apiKey: string;
397
+ /** Override output dimensions for models not in the built-in map */
398
+ dimensions?: number;
399
+ retries?: number;
400
+ retryDelay?: number;
401
+ timeout?: number;
402
+ }
403
+ declare class OpenRouterEmbedder implements Embedder {
404
+ private options;
405
+ readonly dimensions: number;
406
+ private client;
407
+ constructor(options: OpenRouterEmbedderOptions);
408
+ private getModelDimensions;
409
+ embed(text: string): Promise<number[]>;
410
+ embedBatch(texts: string[]): Promise<number[][]>;
411
+ }
412
+
413
+ declare function keywordSearch(text: string, query: string): number;
414
+ declare function combineScores(semanticScore: number, keywordScore: number, semanticWeight?: number): number;
415
+
416
+ export { ApiDataSource, type ApiDataSourceOptions, type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, JSONSource, type JSONSourceOptions, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, OpenRouterEmbedder, type OpenRouterEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, PostgresSource, type PostgresSourceOptions, type QueryOptions, type QueryResult, SQLiteSource, type SQLiteSourceOptions, type SyncEvent, type SyncEventHandler, WebUrlSource, type WebUrlSourceOptions, combineScores, keywordSearch };
package/dist/index.d.ts CHANGED
@@ -14,6 +14,8 @@ interface QueryOptions {
14
14
  filter?: MetadataFilter;
15
15
  includeMetadata?: boolean;
16
16
  includeVectors?: boolean;
17
+ searchType?: 'semantic' | 'keyword' | 'hybrid';
18
+ semanticWeight?: number;
17
19
  }
18
20
  interface MetadataFilter {
19
21
  [key: string]: string | number | boolean | {
@@ -32,9 +34,11 @@ interface QueryResult {
32
34
  interface KnowledgeProvider {
33
35
  add(chunks: Chunk[]): Promise<void>;
34
36
  query(queryVector: number[], options?: QueryOptions): Promise<QueryResult[]>;
37
+ keywordQuery?(query: string, options?: QueryOptions): Promise<QueryResult[]>;
35
38
  delete(ids: string[]): Promise<void>;
36
39
  clear(): Promise<void>;
37
40
  validateDimensions(dimensions: number): Promise<void>;
41
+ getAllChunks?(): Promise<Chunk[]>;
38
42
  close?(): void;
39
43
  }
40
44
  interface KnowledgeSource {
@@ -81,6 +85,7 @@ interface KnowledgeOptions {
81
85
  onError?: ErrorHandler;
82
86
  onSync?: SyncEventHandler;
83
87
  onEmbeddingProgress?: EmbeddingProgressHandler;
88
+ streamingBatchSize?: number;
84
89
  }
85
90
  type ErrorHandler = (error: Error, context: {
86
91
  file?: string;
@@ -109,8 +114,20 @@ declare class Knowledge {
109
114
  private constructor();
110
115
  static create(options: KnowledgeOptions): Promise<Knowledge>;
111
116
  query(text: string, options?: QueryOptions): Promise<QueryResult[]>;
117
+ private semanticQuery;
118
+ private keywordQuery;
119
+ private combineHybridResults;
120
+ private getAllChunks;
112
121
  sync(): Promise<void>;
113
122
  private embedChunks;
123
+ /**
124
+ * Add a single content item to the knowledge base without triggering a full re-sync.
125
+ * This is useful for runtime additions like conversation history or agent state.
126
+ * @param content The text content to add
127
+ * @param metadata Optional metadata to attach to the chunk
128
+ * @returns The ID of the added chunk
129
+ */
130
+ add(content: string, metadata?: Record<string, unknown>): Promise<string>;
114
131
  stop(): Promise<void>;
115
132
  toTool(): KnowledgeTool;
116
133
  }
@@ -156,8 +173,10 @@ declare class MemoryProvider implements KnowledgeProvider {
156
173
  validateDimensions(dimensions: number): Promise<void>;
157
174
  add(chunks: Chunk[]): Promise<void>;
158
175
  query(queryVector: number[], options?: QueryOptions): Promise<QueryResult[]>;
176
+ keywordQuery(query: string, options?: QueryOptions): Promise<QueryResult[]>;
159
177
  delete(ids: string[]): Promise<void>;
160
178
  clear(): Promise<void>;
179
+ getAllChunks(): Promise<Chunk[]>;
161
180
  }
162
181
 
163
182
  interface PersistentKnowledgeProviderOptions {
@@ -176,8 +195,10 @@ declare class PersistentKnowledgeProvider implements KnowledgeProvider {
176
195
  validateDimensions(dimensions: number): Promise<void>;
177
196
  add(chunks: Chunk[]): Promise<void>;
178
197
  query(queryVector: number[], options?: QueryOptions): Promise<QueryResult[]>;
198
+ keywordQuery(query: string, options?: QueryOptions): Promise<QueryResult[]>;
179
199
  delete(ids: string[]): Promise<void>;
180
200
  clear(): Promise<void>;
201
+ getAllChunks(): Promise<Chunk[]>;
181
202
  shouldReSync(): boolean;
182
203
  close(): void;
183
204
  }
@@ -201,6 +222,140 @@ declare class MarkdownSource implements KnowledgeSource {
201
222
  private generateChunkId;
202
223
  }
203
224
 
225
+ interface WebUrlSourceOptions {
226
+ maxChunkSize?: number;
227
+ chunkOverlap?: number;
228
+ minChunkSize?: number;
229
+ namespace?: string;
230
+ metadata?: Record<string, unknown>;
231
+ maxDepth?: number;
232
+ userAgent?: string;
233
+ delayMs?: number;
234
+ timeoutMs?: number;
235
+ sameDomainOnly?: boolean;
236
+ maxPagesPerDomain?: number;
237
+ }
238
+ declare class WebUrlSource implements KnowledgeSource {
239
+ private urls;
240
+ private options;
241
+ private crawledUrls;
242
+ private domainPageCount;
243
+ private lastRequestTime;
244
+ constructor(urls: string[], options?: WebUrlSourceOptions);
245
+ load(): AsyncIterable<Chunk>;
246
+ private crawlUrls;
247
+ private fetchPage;
248
+ private chunkPage;
249
+ private generateChunkId;
250
+ }
251
+
252
+ interface ApiDataSourceOptions {
253
+ maxChunkSize?: number;
254
+ chunkOverlap?: number;
255
+ minChunkSize?: number;
256
+ namespace?: string;
257
+ metadata?: Record<string, unknown>;
258
+ headers?: Record<string, string>;
259
+ method?: 'GET' | 'POST';
260
+ body?: unknown;
261
+ timeoutMs?: number;
262
+ pagination?: {
263
+ param: string;
264
+ start: number;
265
+ step: number;
266
+ maxPages?: number;
267
+ } | null;
268
+ dataPath?: string;
269
+ contentExtractor?: (item: unknown) => string;
270
+ metadataExtractor?: (item: unknown) => Record<string, unknown>;
271
+ }
272
+ declare class ApiDataSource implements KnowledgeSource {
273
+ private url;
274
+ private options;
275
+ constructor(url: string, options?: ApiDataSourceOptions);
276
+ load(): AsyncIterable<Chunk>;
277
+ private fetchData;
278
+ private buildUrl;
279
+ private fetchPage;
280
+ private extractItems;
281
+ private chunkItem;
282
+ private defaultContentExtractor;
283
+ private defaultMetadataExtractor;
284
+ private generateChunkId;
285
+ }
286
+
287
+ interface JSONSourceOptions {
288
+ namespace?: string;
289
+ metadata?: Record<string, unknown>;
290
+ filter?: (item: unknown) => boolean;
291
+ chunkSize?: number;
292
+ /** Required. Transform each JSON item into a human-readable string for AI embedding. */
293
+ toContent: (item: unknown) => string;
294
+ }
295
+ /**
296
+ * Knowledge source for JSON files.
297
+ * Supports jq-like filtering and chunking of large arrays.
298
+ */
299
+ declare class JSONSource implements KnowledgeSource {
300
+ private filePath;
301
+ private options;
302
+ constructor(filePath: string, options: JSONSourceOptions);
303
+ load(): AsyncIterable<Chunk>;
304
+ }
305
+
306
+ interface SQLiteSourceOptions {
307
+ namespace?: string;
308
+ metadata?: Record<string, unknown>;
309
+ query?: string;
310
+ chunkSize?: number;
311
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
312
+ toContent: (row: Record<string, unknown>) => string;
313
+ preLoadCSV?: {
314
+ tableName: string;
315
+ csvPath: string;
316
+ delimiter?: string;
317
+ headers?: boolean;
318
+ };
319
+ }
320
+ /**
321
+ * Knowledge source for SQLite databases.
322
+ * Supports SQL queries and optional CSV/TSV pre-loading.
323
+ * Note: This requires the 'better-sqlite3' package to be installed.
324
+ */
325
+ declare class SQLiteSource implements KnowledgeSource {
326
+ private dbPath;
327
+ private options;
328
+ constructor(dbPath: string, options: SQLiteSourceOptions);
329
+ load(): AsyncIterable<Chunk>;
330
+ private loadCSV;
331
+ }
332
+
333
+ interface PostgresSourceOptions {
334
+ namespace?: string;
335
+ metadata?: Record<string, unknown>;
336
+ query: string;
337
+ chunkSize?: number;
338
+ /** Required. Transform each database row into a human-readable string for AI embedding. */
339
+ toContent: (row: Record<string, unknown>) => string;
340
+ connectionString?: string;
341
+ host?: string;
342
+ port?: number;
343
+ database?: string;
344
+ user?: string;
345
+ password?: string;
346
+ ssl?: boolean;
347
+ }
348
+ /**
349
+ * Knowledge source for PostgreSQL databases.
350
+ * Supports SQL queries with optional chunking.
351
+ * Note: This requires the 'pg' package to be installed.
352
+ */
353
+ declare class PostgresSource implements KnowledgeSource {
354
+ private options;
355
+ constructor(options: PostgresSourceOptions);
356
+ load(): AsyncIterable<Chunk>;
357
+ }
358
+
204
359
  interface OllamaEmbedderOptions {
205
360
  model: string;
206
361
  baseUrl?: string;
@@ -236,4 +391,26 @@ declare class OpenAIEmbedder implements Embedder {
236
391
  embedBatch(texts: string[]): Promise<number[][]>;
237
392
  }
238
393
 
239
- export { type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, type QueryOptions, type QueryResult, type SyncEvent, type SyncEventHandler };
394
+ interface OpenRouterEmbedderOptions {
395
+ model: string;
396
+ apiKey: string;
397
+ /** Override output dimensions for models not in the built-in map */
398
+ dimensions?: number;
399
+ retries?: number;
400
+ retryDelay?: number;
401
+ timeout?: number;
402
+ }
403
+ declare class OpenRouterEmbedder implements Embedder {
404
+ private options;
405
+ readonly dimensions: number;
406
+ private client;
407
+ constructor(options: OpenRouterEmbedderOptions);
408
+ private getModelDimensions;
409
+ embed(text: string): Promise<number[]>;
410
+ embedBatch(texts: string[]): Promise<number[][]>;
411
+ }
412
+
413
+ declare function keywordSearch(text: string, query: string): number;
414
+ declare function combineScores(semanticScore: number, keywordScore: number, semanticWeight?: number): number;
415
+
416
+ export { ApiDataSource, type ApiDataSourceOptions, type Chunk, ChunkTooLargeError, type ChunkUpdate, DimensionMismatchError, type Embedder, EmbeddingError, type EmbeddingProgressEvent, type EmbeddingProgressHandler, type ErrorHandler, IngestionError, JSONSource, type JSONSourceOptions, Knowledge, KnowledgeError, type KnowledgeOptions, type KnowledgeProvider, KnowledgeProviderError, type KnowledgeSource, type KnowledgeTool, type KnowledgeToolParams, type KnowledgeToolResult, MarkdownSource, type MarkdownSourceOptions, MemoryProvider, type MemoryProviderOptions, type MetadataFilter, OllamaEmbedder, type OllamaEmbedderOptions, OpenAIEmbedder, type OpenAIEmbedderOptions, OpenRouterEmbedder, type OpenRouterEmbedderOptions, PersistentKnowledgeProvider, type PersistentKnowledgeProviderOptions, PostgresSource, type PostgresSourceOptions, type QueryOptions, type QueryResult, SQLiteSource, type SQLiteSourceOptions, type SyncEvent, type SyncEventHandler, WebUrlSource, type WebUrlSourceOptions, combineScores, keywordSearch };