mindcache 3.4.3 → 3.4.4

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 ADDED
@@ -0,0 +1,22 @@
1
+ # MindCache
2
+
3
+ A TypeScript library for managing short-term memory in AI agents through an LLM-friendly key-value repository.
4
+
5
+ ## Documentation
6
+
7
+ * **[API Reference](./docs/mindcache-api.md)**: Detailed method signatures and type definitions. Optimized for AI agents (like Cursor, Claude, etc.) to understand the library's capabilities.
8
+ * **[Full Documentation](https://mindcache.dev/llms-full.md)**: Comprehensive guide with examples and deep dives.
9
+
10
+ ## Quick Start
11
+
12
+ ```typescript
13
+ import { mindcache } from 'mindcache';
14
+
15
+ // Store values
16
+ mindcache.set_value('userName', 'Alice');
17
+
18
+ // Generate tools for Vercel AI SDK
19
+ const tools = mindcache.get_aisdk_tools();
20
+ ```
21
+
22
+ See the [root README](../../README.md) for more details.
@@ -3,18 +3,30 @@ import * as Y from 'yjs';
3
3
  /**
4
4
  * Access level for MindCache operations
5
5
  * - 'user': Can only manage content tags (default)
6
- * - 'system': Can manage both content tags and system tags
6
+ * - 'admin': Can manage both content tags and system tags
7
7
  */
8
- type AccessLevel = 'user' | 'system';
8
+ type AccessLevel = 'user' | 'admin';
9
+ /**
10
+ * Context rules for filtering keys by contentTags.
11
+ * When context is set, only keys matching ALL specified tags are visible.
12
+ * Context is client-local and not persisted.
13
+ */
14
+ interface ContextRules {
15
+ /** Tags that a key must have (AND logic - all tags must match) */
16
+ tags: string[];
17
+ /** Default contentTags added to keys created via create_key() in this context */
18
+ defaultContentTags?: string[];
19
+ /** Default systemTags added to keys created via create_key() in this context */
20
+ defaultSystemTags?: SystemTag[];
21
+ }
9
22
  /**
10
23
  * Known system tags that control key behavior
11
24
  * - 'SystemPrompt': Include in system prompt (visible to LLM context)
12
25
  * - 'LLMRead': LLM can read this key via tools
13
26
  * - 'LLMWrite': LLM can write to this key via tools
14
- * - 'protected': Cannot be deleted
15
27
  * - 'ApplyTemplate': Process value through template injection
16
28
  */
17
- type SystemTag = 'SystemPrompt' | 'LLMRead' | 'LLMWrite' | 'protected' | 'ApplyTemplate';
29
+ type SystemTag = 'SystemPrompt' | 'LLMRead' | 'LLMWrite' | 'ApplyTemplate';
18
30
  /**
19
31
  * Type of value stored in a MindCache key
20
32
  */
@@ -91,8 +103,6 @@ declare const SystemTagHelpers: {
91
103
  isLLMReadable: (attrs: KeyAttributes) => boolean;
92
104
  /** Check if key is included in system prompt */
93
105
  isInSystemPrompt: (attrs: KeyAttributes) => boolean;
94
- /** Check if key is protected from deletion */
95
- isProtected: (attrs: KeyAttributes) => boolean;
96
106
  /** Check if key uses template injection */
97
107
  hasTemplateInjection: (attrs: KeyAttributes) => boolean;
98
108
  };
@@ -134,24 +144,27 @@ interface MindCacheOptions {
134
144
  indexedDB?: MindCacheIndexedDBOptions;
135
145
  /** History tracking options (enabled in IndexedDB and Cloud modes) */
136
146
  history?: HistoryOptions;
137
- /** Access level for tag operations. 'system' allows managing system tags. */
147
+ /** Access level for tag operations. 'admin' allows managing system tags. */
138
148
  accessLevel?: AccessLevel;
139
149
  /** Optional existing Y.Doc instance (for server-side hydration) */
140
150
  doc?: Y.Doc;
151
+ /** Context filtering rules. When set, only keys matching the rules are visible. */
152
+ context?: ContextRules;
141
153
  }
142
154
  type ConnectionState$1 = 'disconnected' | 'connecting' | 'connected' | 'error';
143
155
  declare class MindCache {
144
156
  doc: Y.Doc;
145
- private rootMap;
157
+ rootMap: Y.Map<Y.Map<any>>;
146
158
  private listeners;
147
159
  private globalListeners;
148
160
  readonly version = "3.3.2";
149
- private normalizeSystemTags;
161
+ normalizeSystemTags(tags: SystemTag[]): SystemTag[];
150
162
  private _cloudAdapter;
151
163
  private _connectionState;
152
164
  private _isLoaded;
153
165
  private _cloudConfig;
154
166
  private _accessLevel;
167
+ private _contextRules;
155
168
  private _initPromise;
156
169
  private _idbProvider;
157
170
  private _undoManagers;
@@ -206,6 +219,36 @@ declare class MindCache {
206
219
  restoreToVersion(_versionId: string): boolean;
207
220
  get accessLevel(): AccessLevel;
208
221
  get hasSystemAccess(): boolean;
222
+ /**
223
+ * Check if context filtering is currently active.
224
+ */
225
+ get hasContext(): boolean;
226
+ /**
227
+ * Get current context rules, or null if no context is set.
228
+ */
229
+ get_context(): ContextRules | null;
230
+ /**
231
+ * Set context filtering rules.
232
+ * When context is set, only keys with ALL specified tags are visible.
233
+ *
234
+ * @param rules - Context rules, or array of tags (shorthand for { tags: [...] })
235
+ */
236
+ set_context(rules: ContextRules | string[]): void;
237
+ /**
238
+ * Clear context filtering. All keys become visible again.
239
+ */
240
+ reset_context(): void;
241
+ /**
242
+ * Check if a key matches the current context rules.
243
+ * Returns true if no context is set.
244
+ */
245
+ keyMatchesContext(key: string): boolean;
246
+ /**
247
+ * Create a new key with optional default tags from context.
248
+ *
249
+ * @throws Error if key already exists
250
+ */
251
+ create_key(key: string, value: any, attributes?: Partial<KeyAttributes>): void;
209
252
  private _initCloud;
210
253
  private _initYIndexedDB;
211
254
  private _initIndexedDB;
@@ -245,8 +288,9 @@ declare class MindCache {
245
288
  /**
246
289
  * Update only the attributes of a key without modifying the value.
247
290
  * Useful for updating tags, permissions etc. on document type keys.
291
+ * @returns true if attributes were updated, false if key doesn't exist or is protected
248
292
  */
249
- set_attributes(key: string, attributes: Partial<KeyAttributes>): void;
293
+ set_attributes(key: string, attributes: Partial<KeyAttributes>): boolean;
250
294
  set_value(key: string, value: any, attributes?: Partial<KeyAttributes>): void;
251
295
  /**
252
296
  * LLM-safe method to write a value to a key.
@@ -283,15 +327,15 @@ declare class MindCache {
283
327
  */
284
328
  size(): number;
285
329
  /**
286
- * Get all keys in MindCache (including temporal keys).
330
+ * Get all keys in MindCache.
287
331
  */
288
332
  keys(): string[];
289
333
  /**
290
- * Get all values in MindCache (including temporal values).
334
+ * Get all values in MindCache.
291
335
  */
292
336
  values(): any[];
293
337
  /**
294
- * Get all key-value entries (including temporal entries).
338
+ * Get all key-value entries.
295
339
  */
296
340
  entries(): Array<[string, any]>;
297
341
  /**
@@ -306,7 +350,6 @@ declare class MindCache {
306
350
  getSTM(): string;
307
351
  /**
308
352
  * Get the STM as an object with values directly (no attributes).
309
- * Includes system keys ($date, $time).
310
353
  * @deprecated Use getAll() for full STM format
311
354
  */
312
355
  getSTMObject(): Record<string, any>;
@@ -342,7 +385,7 @@ declare class MindCache {
342
385
  getKeysByTag(tag: string): string[];
343
386
  /**
344
387
  * Add a system tag to a key (requires system access).
345
- * System tags: 'SystemPrompt', 'LLMRead', 'LLMWrite', 'readonly', 'protected', 'ApplyTemplate'
388
+ * System tags: 'SystemPrompt', 'LLMRead', 'LLMWrite', 'ApplyTemplate'
346
389
  */
347
390
  systemAddTag(key: string, tag: SystemTag): boolean;
348
391
  /**
@@ -367,8 +410,9 @@ declare class MindCache {
367
410
  systemGetKeysByTag(tag: SystemTag): string[];
368
411
  /**
369
412
  * Helper to get sorted keys (by zIndex).
413
+ * Respects context filtering when set.
370
414
  */
371
- private getSortedKeys;
415
+ getSortedKeys(): string[];
372
416
  /**
373
417
  * Serialize to JSON string.
374
418
  */
@@ -383,8 +427,10 @@ declare class MindCache {
383
427
  toMarkdown(): string;
384
428
  /**
385
429
  * Import from Markdown format.
430
+ * @param markdown The markdown string to import
431
+ * @param merge If false (default), clears existing data before importing. If true, merges with existing data.
386
432
  */
387
- fromMarkdown(markdown: string): void;
433
+ fromMarkdown(markdown: string, merge?: boolean): void;
388
434
  /**
389
435
  * Set base64 binary data.
390
436
  */
@@ -426,17 +472,17 @@ declare class MindCache {
426
472
  */
427
473
  delete_text(key: string, index: number, length: number): void;
428
474
  /**
429
- * Replace all text in a document key (private - use set_value for public API).
475
+ * Replace all text in a document key.
430
476
  * Uses diff-based updates when changes are < diffThreshold (default 80%).
431
477
  * This preserves concurrent edits and provides better undo granularity.
432
478
  */
433
- private _replaceDocumentText;
479
+ _replaceDocumentText(key: string, newText: string, diffThreshold?: number): void;
434
480
  subscribe(key: string, listener: Listener): () => void;
435
481
  subscribeToAll(listener: GlobalListener): () => void;
436
482
  unsubscribeFromAll(listener: GlobalListener): void;
437
- private notifyGlobalListeners;
438
- private sanitizeKeyForTool;
439
- private findKeyFromSanitizedTool;
483
+ notifyGlobalListeners(): void;
484
+ sanitizeKeyForTool(key: string): string;
485
+ findKeyFromSanitizedTool(sanitizedKey: string): string | undefined;
440
486
  /**
441
487
  * Generate Vercel AI SDK compatible tools for writable keys.
442
488
  * For document type keys, generates additional tools: append_, insert_, edit_
@@ -552,4 +598,4 @@ declare class CloudAdapter {
552
598
  private scheduleReconnect;
553
599
  }
554
600
 
555
- export { type AccessLevel as A, type CloudConfig as C, type DeleteOperation as D, type GlobalListener as G, type HistoryEntry as H, type KeyAttributes as K, type Listener as L, MindCache as M, type Operation as O, type SetOperation as S, CloudAdapter as a, type ConnectionState as b, type CloudAdapterEvents as c, type ClearOperation as d, type MindCacheOptions as e, type KeyType as f, type SystemTag as g, type STM as h, type STMEntry as i, type HistoryOptions as j, type MindCacheCloudOptions as k, type MindCacheIndexedDBOptions as l, DEFAULT_KEY_ATTRIBUTES as m, SystemTagHelpers as n };
601
+ export { type AccessLevel as A, type CloudConfig as C, type DeleteOperation as D, type GlobalListener as G, type HistoryEntry as H, type KeyAttributes as K, type Listener as L, MindCache as M, type Operation as O, type SetOperation as S, CloudAdapter as a, type ConnectionState as b, type CloudAdapterEvents as c, type ClearOperation as d, type MindCacheOptions as e, type KeyType as f, type SystemTag as g, type ContextRules as h, type STM as i, type STMEntry as j, type HistoryOptions as k, type MindCacheCloudOptions as l, type MindCacheIndexedDBOptions as m, DEFAULT_KEY_ATTRIBUTES as n, SystemTagHelpers as o };
@@ -3,18 +3,30 @@ import * as Y from 'yjs';
3
3
  /**
4
4
  * Access level for MindCache operations
5
5
  * - 'user': Can only manage content tags (default)
6
- * - 'system': Can manage both content tags and system tags
6
+ * - 'admin': Can manage both content tags and system tags
7
7
  */
8
- type AccessLevel = 'user' | 'system';
8
+ type AccessLevel = 'user' | 'admin';
9
+ /**
10
+ * Context rules for filtering keys by contentTags.
11
+ * When context is set, only keys matching ALL specified tags are visible.
12
+ * Context is client-local and not persisted.
13
+ */
14
+ interface ContextRules {
15
+ /** Tags that a key must have (AND logic - all tags must match) */
16
+ tags: string[];
17
+ /** Default contentTags added to keys created via create_key() in this context */
18
+ defaultContentTags?: string[];
19
+ /** Default systemTags added to keys created via create_key() in this context */
20
+ defaultSystemTags?: SystemTag[];
21
+ }
9
22
  /**
10
23
  * Known system tags that control key behavior
11
24
  * - 'SystemPrompt': Include in system prompt (visible to LLM context)
12
25
  * - 'LLMRead': LLM can read this key via tools
13
26
  * - 'LLMWrite': LLM can write to this key via tools
14
- * - 'protected': Cannot be deleted
15
27
  * - 'ApplyTemplate': Process value through template injection
16
28
  */
17
- type SystemTag = 'SystemPrompt' | 'LLMRead' | 'LLMWrite' | 'protected' | 'ApplyTemplate';
29
+ type SystemTag = 'SystemPrompt' | 'LLMRead' | 'LLMWrite' | 'ApplyTemplate';
18
30
  /**
19
31
  * Type of value stored in a MindCache key
20
32
  */
@@ -91,8 +103,6 @@ declare const SystemTagHelpers: {
91
103
  isLLMReadable: (attrs: KeyAttributes) => boolean;
92
104
  /** Check if key is included in system prompt */
93
105
  isInSystemPrompt: (attrs: KeyAttributes) => boolean;
94
- /** Check if key is protected from deletion */
95
- isProtected: (attrs: KeyAttributes) => boolean;
96
106
  /** Check if key uses template injection */
97
107
  hasTemplateInjection: (attrs: KeyAttributes) => boolean;
98
108
  };
@@ -134,24 +144,27 @@ interface MindCacheOptions {
134
144
  indexedDB?: MindCacheIndexedDBOptions;
135
145
  /** History tracking options (enabled in IndexedDB and Cloud modes) */
136
146
  history?: HistoryOptions;
137
- /** Access level for tag operations. 'system' allows managing system tags. */
147
+ /** Access level for tag operations. 'admin' allows managing system tags. */
138
148
  accessLevel?: AccessLevel;
139
149
  /** Optional existing Y.Doc instance (for server-side hydration) */
140
150
  doc?: Y.Doc;
151
+ /** Context filtering rules. When set, only keys matching the rules are visible. */
152
+ context?: ContextRules;
141
153
  }
142
154
  type ConnectionState$1 = 'disconnected' | 'connecting' | 'connected' | 'error';
143
155
  declare class MindCache {
144
156
  doc: Y.Doc;
145
- private rootMap;
157
+ rootMap: Y.Map<Y.Map<any>>;
146
158
  private listeners;
147
159
  private globalListeners;
148
160
  readonly version = "3.3.2";
149
- private normalizeSystemTags;
161
+ normalizeSystemTags(tags: SystemTag[]): SystemTag[];
150
162
  private _cloudAdapter;
151
163
  private _connectionState;
152
164
  private _isLoaded;
153
165
  private _cloudConfig;
154
166
  private _accessLevel;
167
+ private _contextRules;
155
168
  private _initPromise;
156
169
  private _idbProvider;
157
170
  private _undoManagers;
@@ -206,6 +219,36 @@ declare class MindCache {
206
219
  restoreToVersion(_versionId: string): boolean;
207
220
  get accessLevel(): AccessLevel;
208
221
  get hasSystemAccess(): boolean;
222
+ /**
223
+ * Check if context filtering is currently active.
224
+ */
225
+ get hasContext(): boolean;
226
+ /**
227
+ * Get current context rules, or null if no context is set.
228
+ */
229
+ get_context(): ContextRules | null;
230
+ /**
231
+ * Set context filtering rules.
232
+ * When context is set, only keys with ALL specified tags are visible.
233
+ *
234
+ * @param rules - Context rules, or array of tags (shorthand for { tags: [...] })
235
+ */
236
+ set_context(rules: ContextRules | string[]): void;
237
+ /**
238
+ * Clear context filtering. All keys become visible again.
239
+ */
240
+ reset_context(): void;
241
+ /**
242
+ * Check if a key matches the current context rules.
243
+ * Returns true if no context is set.
244
+ */
245
+ keyMatchesContext(key: string): boolean;
246
+ /**
247
+ * Create a new key with optional default tags from context.
248
+ *
249
+ * @throws Error if key already exists
250
+ */
251
+ create_key(key: string, value: any, attributes?: Partial<KeyAttributes>): void;
209
252
  private _initCloud;
210
253
  private _initYIndexedDB;
211
254
  private _initIndexedDB;
@@ -245,8 +288,9 @@ declare class MindCache {
245
288
  /**
246
289
  * Update only the attributes of a key without modifying the value.
247
290
  * Useful for updating tags, permissions etc. on document type keys.
291
+ * @returns true if attributes were updated, false if key doesn't exist or is protected
248
292
  */
249
- set_attributes(key: string, attributes: Partial<KeyAttributes>): void;
293
+ set_attributes(key: string, attributes: Partial<KeyAttributes>): boolean;
250
294
  set_value(key: string, value: any, attributes?: Partial<KeyAttributes>): void;
251
295
  /**
252
296
  * LLM-safe method to write a value to a key.
@@ -283,15 +327,15 @@ declare class MindCache {
283
327
  */
284
328
  size(): number;
285
329
  /**
286
- * Get all keys in MindCache (including temporal keys).
330
+ * Get all keys in MindCache.
287
331
  */
288
332
  keys(): string[];
289
333
  /**
290
- * Get all values in MindCache (including temporal values).
334
+ * Get all values in MindCache.
291
335
  */
292
336
  values(): any[];
293
337
  /**
294
- * Get all key-value entries (including temporal entries).
338
+ * Get all key-value entries.
295
339
  */
296
340
  entries(): Array<[string, any]>;
297
341
  /**
@@ -306,7 +350,6 @@ declare class MindCache {
306
350
  getSTM(): string;
307
351
  /**
308
352
  * Get the STM as an object with values directly (no attributes).
309
- * Includes system keys ($date, $time).
310
353
  * @deprecated Use getAll() for full STM format
311
354
  */
312
355
  getSTMObject(): Record<string, any>;
@@ -342,7 +385,7 @@ declare class MindCache {
342
385
  getKeysByTag(tag: string): string[];
343
386
  /**
344
387
  * Add a system tag to a key (requires system access).
345
- * System tags: 'SystemPrompt', 'LLMRead', 'LLMWrite', 'readonly', 'protected', 'ApplyTemplate'
388
+ * System tags: 'SystemPrompt', 'LLMRead', 'LLMWrite', 'ApplyTemplate'
346
389
  */
347
390
  systemAddTag(key: string, tag: SystemTag): boolean;
348
391
  /**
@@ -367,8 +410,9 @@ declare class MindCache {
367
410
  systemGetKeysByTag(tag: SystemTag): string[];
368
411
  /**
369
412
  * Helper to get sorted keys (by zIndex).
413
+ * Respects context filtering when set.
370
414
  */
371
- private getSortedKeys;
415
+ getSortedKeys(): string[];
372
416
  /**
373
417
  * Serialize to JSON string.
374
418
  */
@@ -383,8 +427,10 @@ declare class MindCache {
383
427
  toMarkdown(): string;
384
428
  /**
385
429
  * Import from Markdown format.
430
+ * @param markdown The markdown string to import
431
+ * @param merge If false (default), clears existing data before importing. If true, merges with existing data.
386
432
  */
387
- fromMarkdown(markdown: string): void;
433
+ fromMarkdown(markdown: string, merge?: boolean): void;
388
434
  /**
389
435
  * Set base64 binary data.
390
436
  */
@@ -426,17 +472,17 @@ declare class MindCache {
426
472
  */
427
473
  delete_text(key: string, index: number, length: number): void;
428
474
  /**
429
- * Replace all text in a document key (private - use set_value for public API).
475
+ * Replace all text in a document key.
430
476
  * Uses diff-based updates when changes are < diffThreshold (default 80%).
431
477
  * This preserves concurrent edits and provides better undo granularity.
432
478
  */
433
- private _replaceDocumentText;
479
+ _replaceDocumentText(key: string, newText: string, diffThreshold?: number): void;
434
480
  subscribe(key: string, listener: Listener): () => void;
435
481
  subscribeToAll(listener: GlobalListener): () => void;
436
482
  unsubscribeFromAll(listener: GlobalListener): void;
437
- private notifyGlobalListeners;
438
- private sanitizeKeyForTool;
439
- private findKeyFromSanitizedTool;
483
+ notifyGlobalListeners(): void;
484
+ sanitizeKeyForTool(key: string): string;
485
+ findKeyFromSanitizedTool(sanitizedKey: string): string | undefined;
440
486
  /**
441
487
  * Generate Vercel AI SDK compatible tools for writable keys.
442
488
  * For document type keys, generates additional tools: append_, insert_, edit_
@@ -552,4 +598,4 @@ declare class CloudAdapter {
552
598
  private scheduleReconnect;
553
599
  }
554
600
 
555
- export { type AccessLevel as A, type CloudConfig as C, type DeleteOperation as D, type GlobalListener as G, type HistoryEntry as H, type KeyAttributes as K, type Listener as L, MindCache as M, type Operation as O, type SetOperation as S, CloudAdapter as a, type ConnectionState as b, type CloudAdapterEvents as c, type ClearOperation as d, type MindCacheOptions as e, type KeyType as f, type SystemTag as g, type STM as h, type STMEntry as i, type HistoryOptions as j, type MindCacheCloudOptions as k, type MindCacheIndexedDBOptions as l, DEFAULT_KEY_ATTRIBUTES as m, SystemTagHelpers as n };
601
+ export { type AccessLevel as A, type CloudConfig as C, type DeleteOperation as D, type GlobalListener as G, type HistoryEntry as H, type KeyAttributes as K, type Listener as L, MindCache as M, type Operation as O, type SetOperation as S, CloudAdapter as a, type ConnectionState as b, type CloudAdapterEvents as c, type ClearOperation as d, type MindCacheOptions as e, type KeyType as f, type SystemTag as g, type ContextRules as h, type STM as i, type STMEntry as j, type HistoryOptions as k, type MindCacheCloudOptions as l, type MindCacheIndexedDBOptions as m, DEFAULT_KEY_ATTRIBUTES as n, SystemTagHelpers as o };
@@ -1,5 +1,5 @@
1
- import { M as MindCache, C as CloudConfig, a as CloudAdapter } from '../CloudAdapter-CJS3Sh4f.mjs';
2
- export { d as ClearOperation, c as CloudAdapterEvents, b as ConnectionState, D as DeleteOperation, O as Operation, S as SetOperation } from '../CloudAdapter-CJS3Sh4f.mjs';
1
+ import { M as MindCache, C as CloudConfig, a as CloudAdapter } from '../CloudAdapter-DOvDQswy.mjs';
2
+ export { d as ClearOperation, c as CloudAdapterEvents, b as ConnectionState, D as DeleteOperation, O as Operation, S as SetOperation } from '../CloudAdapter-DOvDQswy.mjs';
3
3
  import 'yjs';
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
- import { M as MindCache, C as CloudConfig, a as CloudAdapter } from '../CloudAdapter-CJS3Sh4f.js';
2
- export { d as ClearOperation, c as CloudAdapterEvents, b as ConnectionState, D as DeleteOperation, O as Operation, S as SetOperation } from '../CloudAdapter-CJS3Sh4f.js';
1
+ import { M as MindCache, C as CloudConfig, a as CloudAdapter } from '../CloudAdapter-DOvDQswy.js';
2
+ export { d as ClearOperation, c as CloudAdapterEvents, b as ConnectionState, D as DeleteOperation, O as Operation, S as SetOperation } from '../CloudAdapter-DOvDQswy.js';
3
3
  import 'yjs';
4
4
 
5
5
  /**