@sochdb/sochdb 0.4.0 → 0.4.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.
Files changed (69) hide show
  1. package/README.md +220 -33
  2. package/_bin/aarch64-apple-darwin/libsochdb_storage.dylib +0 -0
  3. package/_bin/aarch64-apple-darwin/sochdb-bulk +0 -0
  4. package/_bin/aarch64-apple-darwin/sochdb-grpc-server +0 -0
  5. package/_bin/aarch64-apple-darwin/sochdb-server +0 -0
  6. package/_bin/x86_64-pc-windows-msvc/sochdb-bulk.exe +0 -0
  7. package/_bin/x86_64-pc-windows-msvc/sochdb-grpc-server.exe +0 -0
  8. package/_bin/x86_64-pc-windows-msvc/sochdb_storage.dll +0 -0
  9. package/_bin/x86_64-unknown-linux-gnu/libsochdb_storage.so +0 -0
  10. package/_bin/x86_64-unknown-linux-gnu/sochdb-bulk +0 -0
  11. package/_bin/x86_64-unknown-linux-gnu/sochdb-grpc-server +0 -0
  12. package/_bin/x86_64-unknown-linux-gnu/sochdb-server +0 -0
  13. package/bin/sochdb-bulk.js +1 -1
  14. package/bin/sochdb-grpc-server.js +1 -1
  15. package/bin/sochdb-server.js +1 -1
  16. package/dist/cjs/context-builder.js +280 -0
  17. package/dist/cjs/database.js +2 -2
  18. package/dist/cjs/embedded/database.js +2 -2
  19. package/dist/cjs/errors.js +99 -7
  20. package/dist/cjs/index.js +40 -3
  21. package/dist/cjs/ipc-client.js +2 -2
  22. package/dist/cjs/memory/consolidation.js +202 -0
  23. package/dist/cjs/memory/extraction.js +181 -0
  24. package/dist/cjs/memory/index.js +26 -0
  25. package/dist/cjs/memory/retrieval.js +232 -0
  26. package/dist/cjs/memory/types.js +69 -0
  27. package/dist/cjs/namespace.js +255 -0
  28. package/dist/cjs/queue.js +289 -0
  29. package/dist/cjs/semantic-cache.js +220 -0
  30. package/dist/esm/context-builder.js +280 -0
  31. package/dist/esm/database.js +2 -2
  32. package/dist/esm/embedded/database.js +2 -2
  33. package/dist/esm/errors.js +107 -7
  34. package/dist/esm/index.js +40 -3
  35. package/dist/esm/ipc-client.js +2 -2
  36. package/dist/esm/memory/consolidation.js +206 -0
  37. package/dist/esm/memory/extraction.js +185 -0
  38. package/dist/esm/memory/index.js +26 -0
  39. package/dist/esm/memory/retrieval.js +243 -0
  40. package/dist/esm/memory/types.js +72 -0
  41. package/dist/esm/namespace.js +262 -0
  42. package/dist/esm/queue.js +291 -0
  43. package/dist/esm/semantic-cache.js +223 -0
  44. package/dist/types/context-builder.d.ts +97 -0
  45. package/dist/types/context-builder.d.ts.map +1 -0
  46. package/dist/types/database.d.ts +1 -1
  47. package/dist/types/embedded/database.d.ts +1 -1
  48. package/dist/types/errors.d.ts +57 -1
  49. package/dist/types/errors.d.ts.map +1 -1
  50. package/dist/types/index.d.ts +12 -2
  51. package/dist/types/index.d.ts.map +1 -1
  52. package/dist/types/ipc-client.d.ts +1 -1
  53. package/dist/types/memory/consolidation.d.ts +66 -0
  54. package/dist/types/memory/consolidation.d.ts.map +1 -0
  55. package/dist/types/memory/extraction.d.ts +82 -0
  56. package/dist/types/memory/extraction.d.ts.map +1 -0
  57. package/dist/types/memory/index.d.ts +10 -0
  58. package/dist/types/memory/index.d.ts.map +1 -0
  59. package/dist/types/memory/retrieval.d.ts +46 -0
  60. package/dist/types/memory/retrieval.d.ts.map +1 -0
  61. package/dist/types/memory/types.d.ts +147 -0
  62. package/dist/types/memory/types.d.ts.map +1 -0
  63. package/dist/types/namespace.d.ts +129 -0
  64. package/dist/types/namespace.d.ts.map +1 -0
  65. package/dist/types/queue.d.ts +120 -0
  66. package/dist/types/queue.d.ts.map +1 -0
  67. package/dist/types/semantic-cache.d.ts +84 -0
  68. package/dist/types/semantic-cache.d.ts.map +1 -0
  69. package/package.json +1 -1
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Memory System - Main Export
3
+ *
4
+ * LLM-native memory system with extraction, consolidation, and retrieval.
5
+ */
6
+ export * from './types';
7
+ export * from './extraction';
8
+ export * from './consolidation';
9
+ export * from './retrieval';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/memory/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Hybrid Retriever for Memory System
3
+ *
4
+ * Combines vector and keyword search with RRF (Reciprocal Rank Fusion).
5
+ */
6
+ import { EmbeddedDatabase } from '../embedded';
7
+ import { AllowedSet, RetrievalConfig, RetrievalResponse } from './types';
8
+ /**
9
+ * Hybrid Retriever with RRF fusion
10
+ */
11
+ export declare class HybridRetriever {
12
+ private db;
13
+ private namespace;
14
+ private collection;
15
+ private config;
16
+ private prefix;
17
+ private bm25;
18
+ private indexed;
19
+ constructor(db: EmbeddedDatabase, namespace: string, collection: string, config?: RetrievalConfig);
20
+ /**
21
+ * Create retriever from database
22
+ */
23
+ static fromDatabase(db: EmbeddedDatabase, namespace: string, collection: string, config?: RetrievalConfig): HybridRetriever;
24
+ /**
25
+ * Index documents for retrieval
26
+ */
27
+ indexDocuments(documents: Array<{
28
+ id: string;
29
+ content: string;
30
+ embedding: number[];
31
+ metadata?: Record<string, any>;
32
+ }>): Promise<void>;
33
+ /**
34
+ * Retrieve documents with hybrid search
35
+ */
36
+ retrieve(queryText: string, queryVector: number[], allowed: AllowedSet, k?: number): Promise<RetrievalResponse>;
37
+ /**
38
+ * Explain ranking for a specific document
39
+ */
40
+ explain(queryText: string, queryVector: number[], docId: string): Promise<{
41
+ vectorRank?: number;
42
+ keywordRank?: number;
43
+ expectedRrfScore?: number;
44
+ }>;
45
+ }
46
+ //# sourceMappingURL=retrieval.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrieval.d.ts","sourceRoot":"","sources":["../../../src/memory/retrieval.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CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAyBrF"}
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Memory System Core Types
3
+ *
4
+ * Type definitions for LLM-native memory system with extraction,
5
+ * consolidation, and retrieval capabilities.
6
+ */
7
+ /**
8
+ * Entity extracted from text
9
+ */
10
+ export interface Entity {
11
+ id?: string;
12
+ name: string;
13
+ entityType: string;
14
+ properties?: Record<string, any>;
15
+ confidence?: number;
16
+ provenance?: string;
17
+ timestamp?: number;
18
+ }
19
+ /**
20
+ * Relation between two entities
21
+ */
22
+ export interface Relation {
23
+ id?: string;
24
+ fromEntity: string;
25
+ relationType: string;
26
+ toEntity: string;
27
+ properties?: Record<string, any>;
28
+ confidence?: number;
29
+ provenance?: string;
30
+ timestamp?: number;
31
+ }
32
+ /**
33
+ * Assertion (subject-predicate-object triple)
34
+ */
35
+ export interface Assertion {
36
+ id?: string;
37
+ subject: string;
38
+ predicate: string;
39
+ object: string;
40
+ confidence?: number;
41
+ provenance?: string;
42
+ timestamp?: number;
43
+ }
44
+ /**
45
+ * Raw assertion for consolidation
46
+ */
47
+ export interface RawAssertion {
48
+ id?: string;
49
+ fact: Record<string, any>;
50
+ source: string;
51
+ confidence: number;
52
+ timestamp?: number;
53
+ }
54
+ /**
55
+ * Canonical fact after consolidation
56
+ */
57
+ export interface CanonicalFact {
58
+ id: string;
59
+ mergedFact: Record<string, any>;
60
+ confidence: number;
61
+ sources: string[];
62
+ validFrom: number;
63
+ validUntil?: number;
64
+ }
65
+ /**
66
+ * Extraction result from LLM
67
+ */
68
+ export interface ExtractionResult {
69
+ entities: Entity[];
70
+ relations: Relation[];
71
+ assertions: Assertion[];
72
+ }
73
+ /**
74
+ * Extraction schema for validation
75
+ */
76
+ export interface ExtractionSchema {
77
+ entityTypes?: string[];
78
+ relationTypes?: string[];
79
+ minConfidence?: number;
80
+ requireProvenance?: boolean;
81
+ }
82
+ /**
83
+ * Consolidation configuration
84
+ */
85
+ export interface ConsolidationConfig {
86
+ similarityThreshold?: number;
87
+ useTemporalUpdates?: boolean;
88
+ maxConflictAge?: number;
89
+ }
90
+ /**
91
+ * Retrieval configuration
92
+ */
93
+ export interface RetrievalConfig {
94
+ k?: number;
95
+ alpha?: number;
96
+ enableRerank?: boolean;
97
+ rerankK?: number;
98
+ }
99
+ /**
100
+ * Retrieval result
101
+ */
102
+ export interface RetrievalResult {
103
+ id: string;
104
+ score: number;
105
+ content: string;
106
+ metadata?: Record<string, any>;
107
+ vectorRank?: number;
108
+ keywordRank?: number;
109
+ }
110
+ /**
111
+ * Retrieval response
112
+ */
113
+ export interface RetrievalResponse {
114
+ results: RetrievalResult[];
115
+ queryTime: number;
116
+ totalResults: number;
117
+ }
118
+ /**
119
+ * AllowedSet for pre-filtering
120
+ */
121
+ export declare abstract class AllowedSet {
122
+ abstract contains(id: string, metadata?: Record<string, any>): boolean;
123
+ static fromIds(ids: string[]): AllowedSet;
124
+ static fromNamespace(namespace: string): AllowedSet;
125
+ static fromFilter(filterFn: (id: string, metadata?: Record<string, any>) => boolean): AllowedSet;
126
+ static allowAll(): AllowedSet;
127
+ }
128
+ /**
129
+ * Namespace policy
130
+ */
131
+ export declare enum NamespacePolicy {
132
+ STRICT = "strict",
133
+ EXPLICIT = "explicit",
134
+ PERMISSIVE = "permissive"
135
+ }
136
+ /**
137
+ * Cross-namespace grant
138
+ */
139
+ export interface NamespaceGrant {
140
+ id: string;
141
+ fromNamespace: string;
142
+ toNamespace: string;
143
+ operations: string[];
144
+ expiresAt?: number;
145
+ reason?: string;
146
+ }
147
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/memory/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,EAAE,CAAC,EAAE,MAAM,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CAAC;CACtB;AAED;;GAEG;AACH,8BAAsB,UAAU;IAC9B,QAAQ,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO;IAEtE,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,UAAU;IAIzC,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU;IAInD,MAAM,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,OAAO,GAAG,UAAU;IAIhG,MAAM,CAAC,QAAQ,IAAI,UAAU;CAG9B;AAsCD;;GAEG;AACH,oBAAY,eAAe;IACzB,MAAM,WAAW;IACjB,QAAQ,aAAa;IACrB,UAAU,eAAe;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
@@ -0,0 +1,129 @@
1
+ /**
2
+ * SochDB Namespace API
3
+ *
4
+ * Provides type-safe namespace isolation with first-class namespace handles.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { Database } from '@sochdb/sochdb';
9
+ *
10
+ * const db = await Database.open('./mydb');
11
+ * const ns = await db.createNamespace('tenant_123');
12
+ * const collection = await ns.createCollection('documents', { dimension: 384 });
13
+ * await collection.insert([1.0, 2.0, ...], { source: 'web' });
14
+ * const results = await collection.search(queryVector, 10);
15
+ * ```
16
+ */
17
+ import { SochDBError } from './errors';
18
+ export interface NamespaceConfig {
19
+ name: string;
20
+ displayName?: string;
21
+ labels?: Record<string, string>;
22
+ readOnly?: boolean;
23
+ }
24
+ export declare class NamespaceNotFoundError extends SochDBError {
25
+ constructor(namespace: string);
26
+ }
27
+ export declare class NamespaceExistsError extends SochDBError {
28
+ constructor(namespace: string);
29
+ }
30
+ export declare class CollectionNotFoundError extends SochDBError {
31
+ constructor(collection: string);
32
+ }
33
+ export declare class CollectionExistsError extends SochDBError {
34
+ constructor(collection: string);
35
+ }
36
+ export declare enum DistanceMetric {
37
+ Cosine = "cosine",
38
+ Euclidean = "euclidean",
39
+ DotProduct = "dot"
40
+ }
41
+ export interface CollectionConfig {
42
+ name: string;
43
+ dimension?: number;
44
+ metric?: DistanceMetric;
45
+ indexed?: boolean;
46
+ hnswM?: number;
47
+ hnswEfConstruction?: number;
48
+ metadata?: Record<string, any>;
49
+ }
50
+ export interface SearchRequest {
51
+ queryVector: number[];
52
+ k: number;
53
+ filter?: Record<string, any>;
54
+ includeMetadata?: boolean;
55
+ }
56
+ export interface SearchResult {
57
+ id: string;
58
+ score: number;
59
+ vector?: number[];
60
+ metadata?: Record<string, any>;
61
+ }
62
+ export declare class Collection {
63
+ private db;
64
+ private namespace;
65
+ private name;
66
+ private config;
67
+ constructor(db: any, namespace: string, name: string, config: CollectionConfig);
68
+ /**
69
+ * Insert a vector with optional metadata
70
+ */
71
+ insert(vector: number[], metadata?: Record<string, any>, id?: string): Promise<string>;
72
+ /**
73
+ * Insert multiple vectors
74
+ */
75
+ insertMany(vectors: number[][], metadatas?: Record<string, any>[], ids?: string[]): Promise<string[]>;
76
+ /**
77
+ * Search for similar vectors
78
+ */
79
+ search(request: SearchRequest): Promise<SearchResult[]>;
80
+ /**
81
+ * Get a vector by ID
82
+ */
83
+ get(id: string): Promise<{
84
+ vector: number[];
85
+ metadata?: Record<string, any>;
86
+ } | null>;
87
+ /**
88
+ * Delete a vector by ID
89
+ */
90
+ delete(id: string): Promise<boolean>;
91
+ /**
92
+ * Count vectors in collection
93
+ */
94
+ count(): Promise<number>;
95
+ private vectorKey;
96
+ private vectorKeyPrefix;
97
+ private metadataKey;
98
+ private generateId;
99
+ private cosineSimilarity;
100
+ }
101
+ export declare class Namespace {
102
+ private db;
103
+ private name;
104
+ private config;
105
+ constructor(db: any, name: string, config: NamespaceConfig);
106
+ /**
107
+ * Create a new collection in this namespace
108
+ */
109
+ createCollection(config: CollectionConfig): Promise<Collection>;
110
+ /**
111
+ * Get an existing collection
112
+ */
113
+ collection(name: string): Promise<Collection>;
114
+ /**
115
+ * Get or create a collection
116
+ */
117
+ getOrCreateCollection(config: CollectionConfig): Promise<Collection>;
118
+ /**
119
+ * Delete a collection
120
+ */
121
+ deleteCollection(name: string): Promise<boolean>;
122
+ /**
123
+ * List all collections in this namespace
124
+ */
125
+ listCollections(): Promise<string[]>;
126
+ getName(): string;
127
+ getConfig(): NamespaceConfig;
128
+ }
129
+ //# sourceMappingURL=namespace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"namespace.d.ts","sourceRoot":"","sources":["../../src/namespace.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,WAAW,EAAiB,MAAM,UAAU,CAAC;AAMtD,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,qBAAa,sBAAuB,SAAQ,WAAW;gBACzC,SAAS,EAAE,MAAM;CAI9B;AAED,qBAAa,oBAAqB,SAAQ,WAAW;gBACvC,SAAS,EAAE,MAAM;CAI9B;AAED,qBAAa,uBAAwB,SAAQ,WAAW;gBAC1C,UAAU,EAAE,MAAM;CAI/B;AAED,qBAAa,qBAAsB,SAAQ,WAAW;gBACxC,UAAU,EAAE,MAAM;CAI/B;AAMD,oBAAY,cAAc;IACxB,MAAM,WAAW;IACjB,S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IjB,SAAS,IAAI,eAAe;CAG7B"}
@@ -0,0 +1,120 @@
1
+ /**
2
+ * SochDB Priority Queue
3
+ *
4
+ * First-class queue API with ordered-key task entries, providing efficient
5
+ * priority queue operations without the O(N) blob rewrite anti-pattern.
6
+ *
7
+ * Features:
8
+ * - Ordered-key representation: Each task has its own key, no blob parsing
9
+ * - O(log N) enqueue/dequeue with ordered scans
10
+ * - Atomic claim protocol for concurrent workers
11
+ * - Visibility timeout for crash recovery
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { Database, PriorityQueue } from '@sochdb/sochdb';
16
+ *
17
+ * const db = await Database.open('./queue_db');
18
+ * const queue = PriorityQueue.fromDatabase(db, 'tasks');
19
+ *
20
+ * // Enqueue task
21
+ * await queue.enqueue(1, Buffer.from('high priority task'));
22
+ *
23
+ * // Dequeue and process
24
+ * const task = await queue.dequeue('worker-1');
25
+ * if (task) {
26
+ * // Process task...
27
+ * await queue.ack(task.taskId);
28
+ * }
29
+ * ```
30
+ */
31
+ export declare enum TaskState {
32
+ PENDING = "pending",
33
+ CLAIMED = "claimed",
34
+ COMPLETED = "completed",
35
+ DEAD_LETTERED = "dead_lettered"
36
+ }
37
+ export interface QueueConfig {
38
+ name: string;
39
+ visibilityTimeout?: number;
40
+ maxRetries?: number;
41
+ deadLetterQueue?: string;
42
+ }
43
+ export interface QueueKey {
44
+ queueId: string;
45
+ priority: number;
46
+ readyTs: number;
47
+ sequence: number;
48
+ taskId: string;
49
+ }
50
+ export interface Task {
51
+ taskId: string;
52
+ priority: number;
53
+ payload: Buffer;
54
+ state: TaskState;
55
+ enqueuedAt: number;
56
+ claimedAt?: number;
57
+ claimedBy?: string;
58
+ completedAt?: number;
59
+ retries: number;
60
+ metadata?: Record<string, any>;
61
+ }
62
+ export interface QueueStats {
63
+ pending: number;
64
+ claimed: number;
65
+ completed: number;
66
+ deadLettered: number;
67
+ totalEnqueued: number;
68
+ totalDequeued: number;
69
+ }
70
+ export declare class PriorityQueue {
71
+ private db;
72
+ private config;
73
+ private static sequenceCounter;
74
+ constructor(db: any, config: QueueConfig);
75
+ /**
76
+ * Create queue from embedded database
77
+ */
78
+ static fromDatabase(db: any, name: string, config?: Partial<QueueConfig>): PriorityQueue;
79
+ /**
80
+ * Create queue from gRPC client
81
+ */
82
+ static fromClient(client: any, name: string, config?: Partial<QueueConfig>): PriorityQueue;
83
+ /**
84
+ * Enqueue a task with priority
85
+ * Lower priority number = higher urgency
86
+ */
87
+ enqueue(priority: number, payload: Buffer, metadata?: Record<string, any>): Promise<string>;
88
+ /**
89
+ * Dequeue the highest priority task
90
+ * Returns null if no tasks available
91
+ */
92
+ dequeue(workerId: string): Promise<Task | null>;
93
+ /**
94
+ * Acknowledge task completion
95
+ */
96
+ ack(taskId: string): Promise<void>;
97
+ /**
98
+ * Negative acknowledge - return task to queue
99
+ */
100
+ nack(taskId: string): Promise<void>;
101
+ /**
102
+ * Get queue statistics
103
+ */
104
+ stats(): Promise<QueueStats>;
105
+ /**
106
+ * Purge completed tasks
107
+ */
108
+ purge(): Promise<number>;
109
+ private generateTaskId;
110
+ private getTask;
111
+ private updateTask;
112
+ private getStat;
113
+ private incrementStat;
114
+ private decrementStat;
115
+ }
116
+ /**
117
+ * Create a queue instance
118
+ */
119
+ export declare function createQueue(db: any, name: string, config?: Partial<QueueConfig>): PriorityQueue;
120
+ //# sourceMappingURL=queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../src/queue.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAQH,oBAAY,SAAS;IACnB,OAAO,YAAY;IACnB,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,aAAa,kBAAkB;CAChC;AAMD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe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AO,CAAC,WAAW,CAAC,GAAG,aAAa;IAQ1F;;;OAGG;IACG,OAAO,CACX,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAC7B,OAAO,CAAC,MAAM,CAAC;IAkClB;;;OAGG;IACG,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;IAUrD;;OAEG;IACG,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAsBxC;;OAEG;IACG,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAyBzC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,UAAU,CAAC;IAWlC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IAM9B,OAAO,CAAC,cAAc;YAIR,OAAO;YAKP,UAAU;YAIV,OAAO;YAMP,aAAa;YAMb,aAAa;CAK5B;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,EAAE,EAAE,GAAG,EACP,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,GAC5B,aAAa,CAEf"}
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Semantic Cache for LLM responses
3
+ *
4
+ * Cache LLM responses with similarity-based retrieval for cost savings.
5
+ * Uses database prefix scanning to store and retrieve cached responses.
6
+ */
7
+ import { EmbeddedDatabase } from './embedded';
8
+ export interface CacheEntry {
9
+ key: string;
10
+ value: string;
11
+ embedding: number[];
12
+ timestamp: number;
13
+ ttl?: number;
14
+ metadata?: Record<string, any>;
15
+ }
16
+ export interface CacheHit extends CacheEntry {
17
+ score: number;
18
+ }
19
+ export interface CacheStats {
20
+ count: number;
21
+ hits: number;
22
+ misses: number;
23
+ hitRate: number;
24
+ memoryUsage: number;
25
+ }
26
+ /**
27
+ * Semantic Cache with vector similarity matching
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const cache = new SemanticCache(db, 'llm_responses');
32
+ *
33
+ * // Store response
34
+ * await cache.put(
35
+ * 'What is Python?',
36
+ * 'Python is a high-level programming language...',
37
+ * embedding,
38
+ * 3600 // TTL in seconds
39
+ * );
40
+ *
41
+ * // Check cache
42
+ * const hit = await cache.get(queryEmbedding, 0.85);
43
+ * if (hit) {
44
+ * console.log(`Cache HIT: ${hit.value} (similarity: ${hit.score})`);
45
+ * }
46
+ * ```
47
+ */
48
+ export declare class SemanticCache {
49
+ private db;
50
+ private cacheName;
51
+ private prefix;
52
+ private hits;
53
+ private misses;
54
+ constructor(db: EmbeddedDatabase, cacheName: string);
55
+ /**
56
+ * Store a cached response
57
+ */
58
+ put(key: string, value: string, embedding: number[], ttlSeconds?: number, metadata?: Record<string, any>): Promise<void>;
59
+ /**
60
+ * Retrieve cached response by similarity
61
+ *
62
+ * @param queryEmbedding - Query embedding vector
63
+ * @param threshold - Minimum cosine similarity (0-1)
64
+ * @returns Best matching cache entry or null
65
+ */
66
+ get(queryEmbedding: number[], threshold?: number): Promise<CacheHit | null>;
67
+ /**
68
+ * Delete a specific cache entry
69
+ */
70
+ delete(key: string): Promise<void>;
71
+ /**
72
+ * Clear all entries in this cache
73
+ */
74
+ clear(): Promise<number>;
75
+ /**
76
+ * Get cache statistics
77
+ */
78
+ stats(): Promise<CacheStats>;
79
+ /**
80
+ * Purge expired entries
81
+ */
82
+ purgeExpired(): Promise<number>;
83
+ }
84
+ //# sourceMappingURL=semantic-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"semantic-cache.d.ts","sourceRoot":"","sources":["../../src/semantic-cache.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AA2B9C,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC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AI,OAAO,CAAC,MAAM,CAAC;CA6BtC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sochdb/sochdb",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "SochDB is an AI-native database with token-optimized output, O(|path|) lookups, built-in vector search, and durable transactions.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",