mindcache 2.0.0 → 2.3.0

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.
@@ -1,20 +1,40 @@
1
+ /**
2
+ * Access level for MindCache operations
3
+ * - 'user': Can only manage content tags (default)
4
+ * - 'system': Can manage both content tags and system tags
5
+ */
6
+ type AccessLevel = 'user' | 'system';
7
+ /**
8
+ * Known system tags that control key behavior
9
+ * - 'prompt': Include in system prompt (replaces visible)
10
+ * - 'readonly': Cannot be modified by AI tools (replaces readonly)
11
+ * - 'protected': Cannot be deleted (replaces hardcoded)
12
+ * - 'template': Process value through template injection
13
+ */
14
+ type SystemTag = 'prompt' | 'readonly' | 'protected' | 'template';
1
15
  /**
2
16
  * Attributes that can be set on a MindCache key
3
17
  */
4
18
  interface KeyAttributes {
5
- /** If true, the key cannot be modified by AI tools */
6
- readonly: boolean;
7
- /** If true, the key is included in system prompts */
8
- visible: boolean;
9
- /** If true, the key is a system key that cannot be deleted */
10
- hardcoded: boolean;
11
- /** If true, the value will be processed through template injection */
12
- template: boolean;
13
19
  /** The type of value stored */
14
20
  type: 'text' | 'image' | 'file' | 'json';
15
21
  /** MIME type for files/images */
16
22
  contentType?: string;
17
- /** Tags for categorizing keys */
23
+ /** User-defined tags for organizing keys */
24
+ contentTags: string[];
25
+ /** System tags that control key behavior (requires system access) */
26
+ systemTags: SystemTag[];
27
+ /** Z-index for ordering keys (lower values appear first) */
28
+ zIndex: number;
29
+ /** @deprecated Use systemTags.includes('readonly') instead */
30
+ readonly: boolean;
31
+ /** @deprecated Use systemTags.includes('prompt') instead */
32
+ visible: boolean;
33
+ /** @deprecated Use systemTags.includes('protected') instead */
34
+ hardcoded: boolean;
35
+ /** @deprecated Use systemTags.includes('template') instead */
36
+ template: boolean;
37
+ /** @deprecated Use contentTags instead */
18
38
  tags?: string[];
19
39
  }
20
40
  /**
@@ -31,9 +51,15 @@ type STM = {
31
51
  [key: string]: STMEntry;
32
52
  };
33
53
  /**
34
- * A function that is called when a key changes
54
+ * Listener callback for key-specific subscriptions
55
+ * Receives the new value when the key changes
56
+ */
57
+ type Listener = (value: unknown) => void;
58
+ /**
59
+ * Global listener callback for all changes
60
+ * Called when any key changes (no parameters - use getAll() to get current state)
35
61
  */
36
- type Listener = () => void;
62
+ type GlobalListener = () => void;
37
63
  /**
38
64
  * Default attributes for new keys
39
65
  */
@@ -60,6 +86,8 @@ interface MindCacheCloudOptions {
60
86
  interface MindCacheOptions {
61
87
  /** Cloud sync configuration. If omitted, runs in local-only mode. */
62
88
  cloud?: MindCacheCloudOptions;
89
+ /** Access level for tag operations. 'system' allows managing system tags. */
90
+ accessLevel?: AccessLevel;
63
91
  }
64
92
  type ConnectionState$1 = 'disconnected' | 'connecting' | 'connected' | 'error';
65
93
  declare class MindCache {
@@ -71,7 +99,17 @@ declare class MindCache {
71
99
  private _connectionState;
72
100
  private _isLoaded;
73
101
  private _cloudConfig;
102
+ private _accessLevel;
103
+ private _initPromise;
74
104
  constructor(options?: MindCacheOptions);
105
+ /**
106
+ * Get the current access level
107
+ */
108
+ get accessLevel(): AccessLevel;
109
+ /**
110
+ * Check if this instance has system-level access
111
+ */
112
+ get hasSystemAccess(): boolean;
75
113
  private _initCloud;
76
114
  /**
77
115
  * Get the current cloud connection state
@@ -81,10 +119,20 @@ declare class MindCache {
81
119
  * Check if data is loaded (true for local, true after sync for cloud)
82
120
  */
83
121
  get isLoaded(): boolean;
122
+ /**
123
+ * Protected method to load CloudAdapter class.
124
+ * Can be overridden/mocked for testing.
125
+ */
126
+ protected _getCloudAdapterClass(): Promise<any>;
84
127
  /**
85
128
  * Check if this instance is connected to cloud
86
129
  */
87
130
  get isCloud(): boolean;
131
+ /**
132
+ * Wait for initial sync to complete (or resolve immediately if already synced/local).
133
+ * Useful for scripts or linear execution flows.
134
+ */
135
+ waitForSync(): Promise<void>;
88
136
  /**
89
137
  * Disconnect from cloud (if connected)
90
138
  */
@@ -111,6 +159,10 @@ declare class MindCache {
111
159
  has(key: string): boolean;
112
160
  delete(key: string): boolean;
113
161
  clear(): void;
162
+ /**
163
+ * Get keys sorted by zIndex (ascending), then by key name
164
+ */
165
+ private getSortedKeys;
114
166
  keys(): string[];
115
167
  values(): any[];
116
168
  entries(): [string, any][];
@@ -119,8 +171,8 @@ declare class MindCache {
119
171
  update(newValues: Record<string, any>): void;
120
172
  subscribe(key: string, listener: Listener): void;
121
173
  unsubscribe(key: string, listener: Listener): void;
122
- subscribeToAll(listener: Listener): void;
123
- unsubscribeFromAll(listener: Listener): void;
174
+ subscribeToAll(listener: GlobalListener): void;
175
+ unsubscribeFromAll(listener: GlobalListener): void;
124
176
  private notifyGlobalListeners;
125
177
  injectSTM(template: string, _processingStack?: Set<string>): string;
126
178
  getSTM(): string;
@@ -149,12 +201,63 @@ declare class MindCache {
149
201
  key: string;
150
202
  value: any;
151
203
  } | null;
204
+ /**
205
+ * Add a content tag to a key (user-level organization)
206
+ */
152
207
  addTag(key: string, tag: string): boolean;
208
+ /**
209
+ * Remove a content tag from a key
210
+ */
153
211
  removeTag(key: string, tag: string): boolean;
212
+ /**
213
+ * Get all content tags for a key
214
+ */
154
215
  getTags(key: string): string[];
216
+ /**
217
+ * Get all unique content tags across all keys
218
+ */
155
219
  getAllTags(): string[];
220
+ /**
221
+ * Check if a key has a specific content tag
222
+ */
156
223
  hasTag(key: string, tag: string): boolean;
224
+ /**
225
+ * Get all keys with a specific content tag as formatted string
226
+ */
157
227
  getTagged(tag: string): string;
228
+ /**
229
+ * Get all keys with a specific content tag
230
+ */
231
+ getKeysByTag(tag: string): string[];
232
+ /**
233
+ * Add a system tag to a key (requires system access)
234
+ * System tags: 'prompt', 'readonly', 'protected', 'template'
235
+ */
236
+ systemAddTag(key: string, tag: SystemTag): boolean;
237
+ /**
238
+ * Remove a system tag from a key (requires system access)
239
+ */
240
+ systemRemoveTag(key: string, tag: SystemTag): boolean;
241
+ /**
242
+ * Get all system tags for a key (requires system access)
243
+ */
244
+ systemGetTags(key: string): SystemTag[];
245
+ /**
246
+ * Check if a key has a specific system tag (requires system access)
247
+ */
248
+ systemHasTag(key: string, tag: SystemTag): boolean;
249
+ /**
250
+ * Set all system tags for a key at once (requires system access)
251
+ */
252
+ systemSetTags(key: string, tags: SystemTag[]): boolean;
253
+ /**
254
+ * Get all keys with a specific system tag (requires system access)
255
+ */
256
+ systemGetKeysByTag(tag: SystemTag): string[];
257
+ /**
258
+ * Helper to sync legacy boolean attributes from system tags
259
+ */
260
+ private syncLegacyFromSystemTags;
158
261
  toMarkdown(): string;
159
262
  fromMarkdown(markdown: string): void;
160
263
  }
@@ -210,9 +313,15 @@ interface CloudAdapterEvents {
210
313
  * CloudAdapter connects a MindCache instance to the cloud service
211
314
  * for real-time sync and persistence.
212
315
  *
213
- * Auth modes:
214
- * 1. Token (recommended): Pass a short-lived token from /api/ws-token
215
- * 2. API Key (server-to-server): Pass apiKey for direct connections
316
+ * Auth flow:
317
+ * 1. SDK calls POST /api/ws-token with apiKey to get short-lived token
318
+ * 2. SDK connects to WebSocket with token in query string
319
+ * 3. Server validates token and upgrades to WebSocket
320
+ *
321
+ * Usage patterns:
322
+ * - apiKey: SDK fetches tokens automatically (simple, good for demos)
323
+ * - tokenEndpoint: Your backend fetches tokens (secure, apiKey stays server-side)
324
+ * - tokenProvider: Custom token logic (advanced)
216
325
  */
217
326
  declare class CloudAdapter {
218
327
  private config;
@@ -248,6 +357,16 @@ declare class CloudAdapter {
248
357
  * Detach from the MindCache instance
249
358
  */
250
359
  detach(): void;
360
+ /**
361
+ * Fetch a short-lived WebSocket token from the API using the API key.
362
+ * This keeps the API key secure by only using it for a single HTTPS request,
363
+ * then using the short-lived token for the WebSocket connection.
364
+ *
365
+ * Supports two key formats:
366
+ * - API keys: mc_live_xxx or mc_test_xxx → Bearer token
367
+ * - Delegate keys: del_xxx:sec_xxx → ApiKey format
368
+ */
369
+ private fetchTokenWithApiKey;
251
370
  /**
252
371
  * Connect to the cloud service
253
372
  */
@@ -1,20 +1,40 @@
1
+ /**
2
+ * Access level for MindCache operations
3
+ * - 'user': Can only manage content tags (default)
4
+ * - 'system': Can manage both content tags and system tags
5
+ */
6
+ type AccessLevel = 'user' | 'system';
7
+ /**
8
+ * Known system tags that control key behavior
9
+ * - 'prompt': Include in system prompt (replaces visible)
10
+ * - 'readonly': Cannot be modified by AI tools (replaces readonly)
11
+ * - 'protected': Cannot be deleted (replaces hardcoded)
12
+ * - 'template': Process value through template injection
13
+ */
14
+ type SystemTag = 'prompt' | 'readonly' | 'protected' | 'template';
1
15
  /**
2
16
  * Attributes that can be set on a MindCache key
3
17
  */
4
18
  interface KeyAttributes {
5
- /** If true, the key cannot be modified by AI tools */
6
- readonly: boolean;
7
- /** If true, the key is included in system prompts */
8
- visible: boolean;
9
- /** If true, the key is a system key that cannot be deleted */
10
- hardcoded: boolean;
11
- /** If true, the value will be processed through template injection */
12
- template: boolean;
13
19
  /** The type of value stored */
14
20
  type: 'text' | 'image' | 'file' | 'json';
15
21
  /** MIME type for files/images */
16
22
  contentType?: string;
17
- /** Tags for categorizing keys */
23
+ /** User-defined tags for organizing keys */
24
+ contentTags: string[];
25
+ /** System tags that control key behavior (requires system access) */
26
+ systemTags: SystemTag[];
27
+ /** Z-index for ordering keys (lower values appear first) */
28
+ zIndex: number;
29
+ /** @deprecated Use systemTags.includes('readonly') instead */
30
+ readonly: boolean;
31
+ /** @deprecated Use systemTags.includes('prompt') instead */
32
+ visible: boolean;
33
+ /** @deprecated Use systemTags.includes('protected') instead */
34
+ hardcoded: boolean;
35
+ /** @deprecated Use systemTags.includes('template') instead */
36
+ template: boolean;
37
+ /** @deprecated Use contentTags instead */
18
38
  tags?: string[];
19
39
  }
20
40
  /**
@@ -31,9 +51,15 @@ type STM = {
31
51
  [key: string]: STMEntry;
32
52
  };
33
53
  /**
34
- * A function that is called when a key changes
54
+ * Listener callback for key-specific subscriptions
55
+ * Receives the new value when the key changes
56
+ */
57
+ type Listener = (value: unknown) => void;
58
+ /**
59
+ * Global listener callback for all changes
60
+ * Called when any key changes (no parameters - use getAll() to get current state)
35
61
  */
36
- type Listener = () => void;
62
+ type GlobalListener = () => void;
37
63
  /**
38
64
  * Default attributes for new keys
39
65
  */
@@ -60,6 +86,8 @@ interface MindCacheCloudOptions {
60
86
  interface MindCacheOptions {
61
87
  /** Cloud sync configuration. If omitted, runs in local-only mode. */
62
88
  cloud?: MindCacheCloudOptions;
89
+ /** Access level for tag operations. 'system' allows managing system tags. */
90
+ accessLevel?: AccessLevel;
63
91
  }
64
92
  type ConnectionState$1 = 'disconnected' | 'connecting' | 'connected' | 'error';
65
93
  declare class MindCache {
@@ -71,7 +99,17 @@ declare class MindCache {
71
99
  private _connectionState;
72
100
  private _isLoaded;
73
101
  private _cloudConfig;
102
+ private _accessLevel;
103
+ private _initPromise;
74
104
  constructor(options?: MindCacheOptions);
105
+ /**
106
+ * Get the current access level
107
+ */
108
+ get accessLevel(): AccessLevel;
109
+ /**
110
+ * Check if this instance has system-level access
111
+ */
112
+ get hasSystemAccess(): boolean;
75
113
  private _initCloud;
76
114
  /**
77
115
  * Get the current cloud connection state
@@ -81,10 +119,20 @@ declare class MindCache {
81
119
  * Check if data is loaded (true for local, true after sync for cloud)
82
120
  */
83
121
  get isLoaded(): boolean;
122
+ /**
123
+ * Protected method to load CloudAdapter class.
124
+ * Can be overridden/mocked for testing.
125
+ */
126
+ protected _getCloudAdapterClass(): Promise<any>;
84
127
  /**
85
128
  * Check if this instance is connected to cloud
86
129
  */
87
130
  get isCloud(): boolean;
131
+ /**
132
+ * Wait for initial sync to complete (or resolve immediately if already synced/local).
133
+ * Useful for scripts or linear execution flows.
134
+ */
135
+ waitForSync(): Promise<void>;
88
136
  /**
89
137
  * Disconnect from cloud (if connected)
90
138
  */
@@ -111,6 +159,10 @@ declare class MindCache {
111
159
  has(key: string): boolean;
112
160
  delete(key: string): boolean;
113
161
  clear(): void;
162
+ /**
163
+ * Get keys sorted by zIndex (ascending), then by key name
164
+ */
165
+ private getSortedKeys;
114
166
  keys(): string[];
115
167
  values(): any[];
116
168
  entries(): [string, any][];
@@ -119,8 +171,8 @@ declare class MindCache {
119
171
  update(newValues: Record<string, any>): void;
120
172
  subscribe(key: string, listener: Listener): void;
121
173
  unsubscribe(key: string, listener: Listener): void;
122
- subscribeToAll(listener: Listener): void;
123
- unsubscribeFromAll(listener: Listener): void;
174
+ subscribeToAll(listener: GlobalListener): void;
175
+ unsubscribeFromAll(listener: GlobalListener): void;
124
176
  private notifyGlobalListeners;
125
177
  injectSTM(template: string, _processingStack?: Set<string>): string;
126
178
  getSTM(): string;
@@ -149,12 +201,63 @@ declare class MindCache {
149
201
  key: string;
150
202
  value: any;
151
203
  } | null;
204
+ /**
205
+ * Add a content tag to a key (user-level organization)
206
+ */
152
207
  addTag(key: string, tag: string): boolean;
208
+ /**
209
+ * Remove a content tag from a key
210
+ */
153
211
  removeTag(key: string, tag: string): boolean;
212
+ /**
213
+ * Get all content tags for a key
214
+ */
154
215
  getTags(key: string): string[];
216
+ /**
217
+ * Get all unique content tags across all keys
218
+ */
155
219
  getAllTags(): string[];
220
+ /**
221
+ * Check if a key has a specific content tag
222
+ */
156
223
  hasTag(key: string, tag: string): boolean;
224
+ /**
225
+ * Get all keys with a specific content tag as formatted string
226
+ */
157
227
  getTagged(tag: string): string;
228
+ /**
229
+ * Get all keys with a specific content tag
230
+ */
231
+ getKeysByTag(tag: string): string[];
232
+ /**
233
+ * Add a system tag to a key (requires system access)
234
+ * System tags: 'prompt', 'readonly', 'protected', 'template'
235
+ */
236
+ systemAddTag(key: string, tag: SystemTag): boolean;
237
+ /**
238
+ * Remove a system tag from a key (requires system access)
239
+ */
240
+ systemRemoveTag(key: string, tag: SystemTag): boolean;
241
+ /**
242
+ * Get all system tags for a key (requires system access)
243
+ */
244
+ systemGetTags(key: string): SystemTag[];
245
+ /**
246
+ * Check if a key has a specific system tag (requires system access)
247
+ */
248
+ systemHasTag(key: string, tag: SystemTag): boolean;
249
+ /**
250
+ * Set all system tags for a key at once (requires system access)
251
+ */
252
+ systemSetTags(key: string, tags: SystemTag[]): boolean;
253
+ /**
254
+ * Get all keys with a specific system tag (requires system access)
255
+ */
256
+ systemGetKeysByTag(tag: SystemTag): string[];
257
+ /**
258
+ * Helper to sync legacy boolean attributes from system tags
259
+ */
260
+ private syncLegacyFromSystemTags;
158
261
  toMarkdown(): string;
159
262
  fromMarkdown(markdown: string): void;
160
263
  }
@@ -210,9 +313,15 @@ interface CloudAdapterEvents {
210
313
  * CloudAdapter connects a MindCache instance to the cloud service
211
314
  * for real-time sync and persistence.
212
315
  *
213
- * Auth modes:
214
- * 1. Token (recommended): Pass a short-lived token from /api/ws-token
215
- * 2. API Key (server-to-server): Pass apiKey for direct connections
316
+ * Auth flow:
317
+ * 1. SDK calls POST /api/ws-token with apiKey to get short-lived token
318
+ * 2. SDK connects to WebSocket with token in query string
319
+ * 3. Server validates token and upgrades to WebSocket
320
+ *
321
+ * Usage patterns:
322
+ * - apiKey: SDK fetches tokens automatically (simple, good for demos)
323
+ * - tokenEndpoint: Your backend fetches tokens (secure, apiKey stays server-side)
324
+ * - tokenProvider: Custom token logic (advanced)
216
325
  */
217
326
  declare class CloudAdapter {
218
327
  private config;
@@ -248,6 +357,16 @@ declare class CloudAdapter {
248
357
  * Detach from the MindCache instance
249
358
  */
250
359
  detach(): void;
360
+ /**
361
+ * Fetch a short-lived WebSocket token from the API using the API key.
362
+ * This keeps the API key secure by only using it for a single HTTPS request,
363
+ * then using the short-lived token for the WebSocket connection.
364
+ *
365
+ * Supports two key formats:
366
+ * - API keys: mc_live_xxx or mc_test_xxx → Bearer token
367
+ * - Delegate keys: del_xxx:sec_xxx → ApiKey format
368
+ */
369
+ private fetchTokenWithApiKey;
251
370
  /**
252
371
  * Connect to the cloud service
253
372
  */
package/dist/index.d.mts CHANGED
@@ -1 +1 @@
1
- export { a as CloudAdapter, c as CloudAdapterEvents, C as CloudConfig, b as ConnectionState, i as DEFAULT_KEY_ATTRIBUTES, K as KeyAttributes, L as Listener, M as MindCache, h as MindCacheCloudOptions, g as MindCacheOptions, e as STM, f as STMEntry, m as mindcache } from './index-CFJtj3DL.mjs';
1
+ export { a as CloudAdapter, c as CloudAdapterEvents, C as CloudConfig, b as ConnectionState, i as DEFAULT_KEY_ATTRIBUTES, K as KeyAttributes, L as Listener, M as MindCache, h as MindCacheCloudOptions, g as MindCacheOptions, e as STM, f as STMEntry, m as mindcache } from './index-DXb0fL3e.mjs';
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { a as CloudAdapter, c as CloudAdapterEvents, C as CloudConfig, b as ConnectionState, i as DEFAULT_KEY_ATTRIBUTES, K as KeyAttributes, L as Listener, M as MindCache, h as MindCacheCloudOptions, g as MindCacheOptions, e as STM, f as STMEntry, m as mindcache } from './index-CFJtj3DL.js';
1
+ export { a as CloudAdapter, c as CloudAdapterEvents, C as CloudConfig, b as ConnectionState, i as DEFAULT_KEY_ATTRIBUTES, K as KeyAttributes, L as Listener, M as MindCache, h as MindCacheCloudOptions, g as MindCacheOptions, e as STM, f as STMEntry, m as mindcache } from './index-DXb0fL3e.js';