mindcache 1.0.1 → 2.0.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.
package/dist/index.mjs ADDED
@@ -0,0 +1,1299 @@
1
+ import { z } from 'zod';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __esm = (fn, res) => function __init() {
6
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
7
+ };
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+
13
+ // src/cloud/CloudAdapter.ts
14
+ var CloudAdapter_exports = {};
15
+ __export(CloudAdapter_exports, {
16
+ CloudAdapter: () => CloudAdapter
17
+ });
18
+ var DEFAULT_BASE_URL, RECONNECT_DELAY, MAX_RECONNECT_DELAY, CloudAdapter;
19
+ var init_CloudAdapter = __esm({
20
+ "src/cloud/CloudAdapter.ts"() {
21
+ DEFAULT_BASE_URL = "wss://api.mindcache.io";
22
+ RECONNECT_DELAY = 1e3;
23
+ MAX_RECONNECT_DELAY = 3e4;
24
+ CloudAdapter = class {
25
+ constructor(config) {
26
+ this.config = config;
27
+ this.config.baseUrl = config.baseUrl || DEFAULT_BASE_URL;
28
+ }
29
+ ws = null;
30
+ queue = [];
31
+ mindcache = null;
32
+ unsubscribe = null;
33
+ reconnectAttempts = 0;
34
+ reconnectTimeout = null;
35
+ _state = "disconnected";
36
+ listeners = {};
37
+ token = null;
38
+ /**
39
+ * Set auth token (short-lived, from /api/ws-token)
40
+ * Call this before connect() or use setTokenProvider for auto-refresh
41
+ */
42
+ setToken(token) {
43
+ this.token = token;
44
+ }
45
+ /**
46
+ * Set a function that returns a fresh token
47
+ * Used for automatic token refresh on reconnect
48
+ */
49
+ setTokenProvider(provider) {
50
+ this.config.tokenProvider = provider;
51
+ }
52
+ /**
53
+ * Get current connection state
54
+ */
55
+ get state() {
56
+ return this._state;
57
+ }
58
+ /**
59
+ * Attach to a MindCache instance and start syncing
60
+ */
61
+ attach(mc) {
62
+ if (this.mindcache) {
63
+ this.detach();
64
+ }
65
+ this.mindcache = mc;
66
+ const listener = () => {
67
+ if (mc.isRemoteUpdate()) {
68
+ console.log("\u2601\uFE0F CloudAdapter: Skipping remote update");
69
+ return;
70
+ }
71
+ console.log("\u2601\uFE0F CloudAdapter: Local change detected, syncing...");
72
+ this.syncLocalChanges();
73
+ };
74
+ mc.subscribeToAll(listener);
75
+ this.unsubscribe = () => mc.unsubscribeFromAll(listener);
76
+ console.log("\u2601\uFE0F CloudAdapter: Attached to MindCache instance");
77
+ }
78
+ /**
79
+ * Detach from the MindCache instance
80
+ */
81
+ detach() {
82
+ if (this.unsubscribe) {
83
+ this.unsubscribe();
84
+ this.unsubscribe = null;
85
+ }
86
+ this.mindcache = null;
87
+ }
88
+ /**
89
+ * Connect to the cloud service
90
+ */
91
+ async connect() {
92
+ if (this._state === "connecting" || this._state === "connected") {
93
+ return;
94
+ }
95
+ this._state = "connecting";
96
+ try {
97
+ if (this.config.tokenProvider && !this.token) {
98
+ this.token = await this.config.tokenProvider();
99
+ }
100
+ let url = `${this.config.baseUrl}/sync/${this.config.instanceId}`;
101
+ if (this.token) {
102
+ url += `?token=${encodeURIComponent(this.token)}`;
103
+ this.token = null;
104
+ }
105
+ this.ws = new WebSocket(url);
106
+ this.setupWebSocket();
107
+ } catch (error) {
108
+ this._state = "error";
109
+ this.emit("error", error);
110
+ this.scheduleReconnect();
111
+ }
112
+ }
113
+ /**
114
+ * Disconnect from the cloud service
115
+ */
116
+ disconnect() {
117
+ if (this.reconnectTimeout) {
118
+ clearTimeout(this.reconnectTimeout);
119
+ this.reconnectTimeout = null;
120
+ }
121
+ if (this.ws) {
122
+ this.ws.close();
123
+ this.ws = null;
124
+ }
125
+ this._state = "disconnected";
126
+ this.emit("disconnected");
127
+ }
128
+ /**
129
+ * Push an operation to the cloud
130
+ */
131
+ push(op) {
132
+ if (this.ws?.readyState === WebSocket.OPEN) {
133
+ this.ws.send(JSON.stringify(op));
134
+ } else {
135
+ this.queue.push(op);
136
+ }
137
+ }
138
+ /**
139
+ * Add event listener
140
+ */
141
+ on(event, listener) {
142
+ if (!this.listeners[event]) {
143
+ this.listeners[event] = [];
144
+ }
145
+ this.listeners[event].push(listener);
146
+ }
147
+ /**
148
+ * Remove event listener
149
+ */
150
+ off(event, listener) {
151
+ if (this.listeners[event]) {
152
+ this.listeners[event] = this.listeners[event].filter((l) => l !== listener);
153
+ }
154
+ }
155
+ emit(event, ...args) {
156
+ if (this.listeners[event]) {
157
+ this.listeners[event].forEach((listener) => listener(...args));
158
+ }
159
+ }
160
+ setupWebSocket() {
161
+ if (!this.ws) {
162
+ return;
163
+ }
164
+ this.ws.onopen = () => {
165
+ if (this.config.apiKey) {
166
+ this.ws.send(JSON.stringify({
167
+ type: "auth",
168
+ apiKey: this.config.apiKey
169
+ }));
170
+ }
171
+ };
172
+ this.ws.onmessage = (event) => {
173
+ try {
174
+ const msg = JSON.parse(event.data);
175
+ this.handleMessage(msg);
176
+ } catch (error) {
177
+ console.error("MindCache Cloud: Failed to parse message:", error);
178
+ }
179
+ };
180
+ this.ws.onclose = () => {
181
+ this._state = "disconnected";
182
+ this.emit("disconnected");
183
+ this.scheduleReconnect();
184
+ };
185
+ this.ws.onerror = (error) => {
186
+ this._state = "error";
187
+ this.emit("error", new Error("WebSocket error"));
188
+ console.error("MindCache Cloud: WebSocket error:", error);
189
+ };
190
+ }
191
+ handleMessage(msg) {
192
+ switch (msg.type) {
193
+ case "auth_success":
194
+ this._state = "connected";
195
+ this.reconnectAttempts = 0;
196
+ this.emit("connected");
197
+ this.flushQueue();
198
+ break;
199
+ case "auth_error":
200
+ this._state = "error";
201
+ this.emit("error", new Error(msg.error));
202
+ this.disconnect();
203
+ break;
204
+ case "sync":
205
+ if (this.mindcache && msg.data) {
206
+ Object.entries(msg.data).forEach(([key, entry]) => {
207
+ const { value, attributes } = entry;
208
+ this.mindcache._setFromRemote(key, value, attributes);
209
+ });
210
+ this.emit("synced");
211
+ }
212
+ break;
213
+ case "set":
214
+ if (this.mindcache) {
215
+ this.mindcache._setFromRemote(msg.key, msg.value, msg.attributes);
216
+ }
217
+ break;
218
+ case "key_updated":
219
+ if (this.mindcache) {
220
+ this.mindcache._setFromRemote(msg.key, msg.value, msg.attributes);
221
+ }
222
+ break;
223
+ case "delete":
224
+ if (this.mindcache) {
225
+ this.mindcache._deleteFromRemote(msg.key);
226
+ }
227
+ break;
228
+ case "key_deleted":
229
+ if (this.mindcache) {
230
+ this.mindcache._deleteFromRemote(msg.key);
231
+ }
232
+ break;
233
+ case "clear":
234
+ if (this.mindcache) {
235
+ this.mindcache._clearFromRemote();
236
+ }
237
+ break;
238
+ case "cleared":
239
+ if (this.mindcache) {
240
+ this.mindcache._clearFromRemote();
241
+ }
242
+ break;
243
+ case "error":
244
+ this.emit("error", new Error(msg.error));
245
+ break;
246
+ }
247
+ }
248
+ flushQueue() {
249
+ while (this.queue.length > 0 && this.ws?.readyState === WebSocket.OPEN) {
250
+ const op = this.queue.shift();
251
+ this.ws.send(JSON.stringify(op));
252
+ }
253
+ }
254
+ scheduleReconnect() {
255
+ if (this.reconnectTimeout) {
256
+ return;
257
+ }
258
+ const delay = Math.min(
259
+ RECONNECT_DELAY * Math.pow(2, this.reconnectAttempts),
260
+ MAX_RECONNECT_DELAY
261
+ );
262
+ this.reconnectTimeout = setTimeout(async () => {
263
+ this.reconnectTimeout = null;
264
+ this.reconnectAttempts++;
265
+ if (this.config.tokenProvider) {
266
+ try {
267
+ this.token = await this.config.tokenProvider();
268
+ } catch (error) {
269
+ console.error("MindCache Cloud: Failed to get token for reconnect:", error);
270
+ this.emit("error", error);
271
+ return;
272
+ }
273
+ }
274
+ this.connect();
275
+ }, delay);
276
+ }
277
+ syncLocalChanges() {
278
+ if (!this.mindcache) {
279
+ return;
280
+ }
281
+ const entries = this.mindcache.serialize();
282
+ console.log("\u2601\uFE0F CloudAdapter: Syncing local changes:", Object.keys(entries));
283
+ Object.entries(entries).forEach(([key, entry]) => {
284
+ console.log("\u2601\uFE0F CloudAdapter: Pushing key:", key, "=", entry.value);
285
+ this.push({
286
+ type: "set",
287
+ key,
288
+ value: entry.value,
289
+ attributes: entry.attributes,
290
+ timestamp: Date.now()
291
+ });
292
+ });
293
+ }
294
+ };
295
+ }
296
+ });
297
+
298
+ // src/core/types.ts
299
+ var DEFAULT_KEY_ATTRIBUTES = {
300
+ readonly: false,
301
+ visible: true,
302
+ hardcoded: false,
303
+ template: false,
304
+ type: "text",
305
+ tags: []
306
+ };
307
+
308
+ // src/core/MindCache.ts
309
+ var MindCache = class {
310
+ stm = {};
311
+ listeners = {};
312
+ globalListeners = [];
313
+ // Internal flag to prevent sync loops when receiving remote updates
314
+ _isRemoteUpdate = false;
315
+ // Cloud sync state
316
+ _cloudAdapter = null;
317
+ _connectionState = "disconnected";
318
+ _isLoaded = true;
319
+ // Default true for local mode
320
+ _cloudConfig = null;
321
+ constructor(options) {
322
+ if (options?.cloud) {
323
+ this._cloudConfig = options.cloud;
324
+ this._isLoaded = false;
325
+ this._connectionState = "disconnected";
326
+ this._initCloud();
327
+ }
328
+ }
329
+ async _initCloud() {
330
+ if (!this._cloudConfig) {
331
+ return;
332
+ }
333
+ try {
334
+ const { CloudAdapter: CloudAdapter2 } = await Promise.resolve().then(() => (init_CloudAdapter(), CloudAdapter_exports));
335
+ const configUrl = this._cloudConfig.baseUrl || "https://api.mindcache.io";
336
+ const baseUrl = configUrl.replace("https://", "wss://").replace("http://", "ws://");
337
+ const adapter = new CloudAdapter2({
338
+ instanceId: this._cloudConfig.instanceId,
339
+ projectId: this._cloudConfig.projectId || "default",
340
+ baseUrl,
341
+ apiKey: this._cloudConfig.apiKey
342
+ });
343
+ if (this._cloudConfig.tokenEndpoint) {
344
+ const tokenEndpoint = this._cloudConfig.tokenEndpoint;
345
+ const instanceId = this._cloudConfig.instanceId;
346
+ let resolvedBaseUrl;
347
+ if (tokenEndpoint.startsWith("http://") || tokenEndpoint.startsWith("https://")) {
348
+ resolvedBaseUrl = tokenEndpoint;
349
+ } else if (typeof window !== "undefined" && window.location?.origin) {
350
+ resolvedBaseUrl = `${window.location.origin}${tokenEndpoint.startsWith("/") ? "" : "/"}${tokenEndpoint}`;
351
+ } else {
352
+ console.warn("MindCache: Cannot resolve tokenEndpoint to absolute URL - window.location not available");
353
+ resolvedBaseUrl = tokenEndpoint;
354
+ }
355
+ adapter.setTokenProvider(async () => {
356
+ const url = resolvedBaseUrl.includes("?") ? `${resolvedBaseUrl}&instanceId=${instanceId}` : `${resolvedBaseUrl}?instanceId=${instanceId}`;
357
+ const response = await fetch(url);
358
+ if (!response.ok) {
359
+ const error = await response.json().catch(() => ({ error: "Failed to get token" }));
360
+ throw new Error(error.error || "Failed to get token");
361
+ }
362
+ const data = await response.json();
363
+ return data.token;
364
+ });
365
+ }
366
+ adapter.on("connected", () => {
367
+ this._connectionState = "connected";
368
+ this.notifyGlobalListeners();
369
+ });
370
+ adapter.on("disconnected", () => {
371
+ this._connectionState = "disconnected";
372
+ this.notifyGlobalListeners();
373
+ });
374
+ adapter.on("error", () => {
375
+ this._connectionState = "error";
376
+ this.notifyGlobalListeners();
377
+ });
378
+ adapter.on("synced", () => {
379
+ this._isLoaded = true;
380
+ this.notifyGlobalListeners();
381
+ });
382
+ adapter.attach(this);
383
+ this._cloudAdapter = adapter;
384
+ this._connectionState = "connecting";
385
+ adapter.connect();
386
+ } catch (error) {
387
+ console.error("MindCache: Failed to initialize cloud connection:", error);
388
+ this._connectionState = "error";
389
+ this._isLoaded = true;
390
+ }
391
+ }
392
+ /**
393
+ * Get the current cloud connection state
394
+ */
395
+ get connectionState() {
396
+ return this._connectionState;
397
+ }
398
+ /**
399
+ * Check if data is loaded (true for local, true after sync for cloud)
400
+ */
401
+ get isLoaded() {
402
+ return this._isLoaded;
403
+ }
404
+ /**
405
+ * Check if this instance is connected to cloud
406
+ */
407
+ get isCloud() {
408
+ return this._cloudConfig !== null;
409
+ }
410
+ /**
411
+ * Disconnect from cloud (if connected)
412
+ */
413
+ disconnect() {
414
+ if (this._cloudAdapter) {
415
+ this._cloudAdapter.disconnect();
416
+ this._cloudAdapter.detach();
417
+ this._cloudAdapter = null;
418
+ this._connectionState = "disconnected";
419
+ }
420
+ }
421
+ // Helper method to encode file to base64
422
+ encodeFileToBase64(file) {
423
+ return new Promise((resolve, reject) => {
424
+ if (typeof FileReader !== "undefined") {
425
+ const reader = new FileReader();
426
+ reader.onload = () => {
427
+ const result = reader.result;
428
+ const base64Data = result.split(",")[1];
429
+ resolve(base64Data);
430
+ };
431
+ reader.onerror = reject;
432
+ reader.readAsDataURL(file);
433
+ } else {
434
+ reject(new Error("FileReader not available in Node.js environment. Use set_base64() method instead."));
435
+ }
436
+ });
437
+ }
438
+ // Helper method to create data URL from base64 and content type
439
+ createDataUrl(base64Data, contentType) {
440
+ return `data:${contentType};base64,${base64Data}`;
441
+ }
442
+ // Helper method to validate content type for different STM types
443
+ validateContentType(type, contentType) {
444
+ if (type === "text" || type === "json") {
445
+ return true;
446
+ }
447
+ if (!contentType) {
448
+ return false;
449
+ }
450
+ if (type === "image") {
451
+ return contentType.startsWith("image/");
452
+ }
453
+ if (type === "file") {
454
+ return true;
455
+ }
456
+ return false;
457
+ }
458
+ /** @deprecated Use get_value instead */
459
+ get(key) {
460
+ return this.get_value(key);
461
+ }
462
+ // Get a value from the STM with template processing if enabled
463
+ get_value(key, _processingStack) {
464
+ if (key === "$date") {
465
+ const today = /* @__PURE__ */ new Date();
466
+ return today.toISOString().split("T")[0];
467
+ }
468
+ if (key === "$time") {
469
+ const now = /* @__PURE__ */ new Date();
470
+ return now.toTimeString().split(" ")[0];
471
+ }
472
+ const entry = this.stm[key];
473
+ if (!entry) {
474
+ return void 0;
475
+ }
476
+ if (entry.attributes.template) {
477
+ const processingStack = _processingStack || /* @__PURE__ */ new Set();
478
+ if (processingStack.has(key)) {
479
+ return entry.value;
480
+ }
481
+ processingStack.add(key);
482
+ const result = this.injectSTM(entry.value, processingStack);
483
+ processingStack.delete(key);
484
+ return result;
485
+ }
486
+ return entry.value;
487
+ }
488
+ // Get attributes for a key
489
+ get_attributes(key) {
490
+ if (key === "$date" || key === "$time") {
491
+ return {
492
+ readonly: true,
493
+ visible: true,
494
+ hardcoded: true,
495
+ template: false,
496
+ type: "text",
497
+ tags: []
498
+ };
499
+ }
500
+ const entry = this.stm[key];
501
+ return entry ? entry.attributes : void 0;
502
+ }
503
+ // Set a value in the STM with default attributes
504
+ set_value(key, value, attributes) {
505
+ if (key === "$date" || key === "$time") {
506
+ return;
507
+ }
508
+ const existingEntry = this.stm[key];
509
+ const baseAttributes = existingEntry ? existingEntry.attributes : { ...DEFAULT_KEY_ATTRIBUTES };
510
+ const finalAttributes = attributes ? { ...baseAttributes, ...attributes } : baseAttributes;
511
+ if (finalAttributes.hardcoded) {
512
+ finalAttributes.readonly = true;
513
+ finalAttributes.template = false;
514
+ }
515
+ this.stm[key] = {
516
+ value,
517
+ attributes: finalAttributes
518
+ };
519
+ if (this.listeners[key]) {
520
+ this.listeners[key].forEach((listener) => listener());
521
+ }
522
+ this.notifyGlobalListeners();
523
+ }
524
+ // Internal method for setting values from remote (cloud sync)
525
+ // This doesn't trigger the global listener to prevent sync loops
526
+ _setFromRemote(key, value, attributes) {
527
+ if (key === "$date" || key === "$time") {
528
+ return;
529
+ }
530
+ this._isRemoteUpdate = true;
531
+ this.stm[key] = {
532
+ value,
533
+ attributes
534
+ };
535
+ if (this.listeners[key]) {
536
+ this.listeners[key].forEach((listener) => listener());
537
+ }
538
+ this.notifyGlobalListeners();
539
+ this._isRemoteUpdate = false;
540
+ }
541
+ // Check if current update is from remote
542
+ isRemoteUpdate() {
543
+ return this._isRemoteUpdate;
544
+ }
545
+ // Internal method for deleting from remote (cloud sync)
546
+ _deleteFromRemote(key) {
547
+ if (key === "$date" || key === "$time") {
548
+ return;
549
+ }
550
+ this._isRemoteUpdate = true;
551
+ if (key in this.stm) {
552
+ delete this.stm[key];
553
+ if (this.listeners[key]) {
554
+ this.listeners[key].forEach((listener) => listener());
555
+ }
556
+ this.notifyGlobalListeners();
557
+ }
558
+ this._isRemoteUpdate = false;
559
+ }
560
+ // Internal method for clearing from remote (cloud sync)
561
+ _clearFromRemote() {
562
+ this._isRemoteUpdate = true;
563
+ this.stm = {};
564
+ this.notifyGlobalListeners();
565
+ this._isRemoteUpdate = false;
566
+ }
567
+ // Set attributes for an existing key
568
+ set_attributes(key, attributes) {
569
+ if (key === "$date" || key === "$time") {
570
+ return false;
571
+ }
572
+ const entry = this.stm[key];
573
+ if (!entry) {
574
+ return false;
575
+ }
576
+ const { hardcoded: _hardcoded, ...allowedAttributes } = attributes;
577
+ entry.attributes = { ...entry.attributes, ...allowedAttributes };
578
+ if (entry.attributes.hardcoded) {
579
+ entry.attributes.readonly = true;
580
+ entry.attributes.template = false;
581
+ }
582
+ this.notifyGlobalListeners();
583
+ return true;
584
+ }
585
+ set(key, value) {
586
+ this.set_value(key, value);
587
+ }
588
+ async set_file(key, file, attributes) {
589
+ const base64Data = await this.encodeFileToBase64(file);
590
+ const contentType = file.type;
591
+ const fileAttributes = {
592
+ type: contentType.startsWith("image/") ? "image" : "file",
593
+ contentType,
594
+ ...attributes
595
+ };
596
+ this.set_value(key, base64Data, fileAttributes);
597
+ }
598
+ set_base64(key, base64Data, contentType, type = "file", attributes) {
599
+ if (!this.validateContentType(type, contentType)) {
600
+ throw new Error(`Invalid content type ${contentType} for type ${type}`);
601
+ }
602
+ const fileAttributes = {
603
+ type,
604
+ contentType,
605
+ ...attributes
606
+ };
607
+ this.set_value(key, base64Data, fileAttributes);
608
+ }
609
+ add_image(key, base64Data, contentType = "image/jpeg", attributes) {
610
+ if (!contentType.startsWith("image/")) {
611
+ throw new Error(`Invalid image content type: ${contentType}. Must start with 'image/'`);
612
+ }
613
+ this.set_base64(key, base64Data, contentType, "image", attributes);
614
+ this.set_attributes(key, {
615
+ type: "image",
616
+ contentType
617
+ });
618
+ }
619
+ get_data_url(key) {
620
+ const entry = this.stm[key];
621
+ if (!entry || entry.attributes.type !== "image" && entry.attributes.type !== "file") {
622
+ return void 0;
623
+ }
624
+ if (!entry.attributes.contentType) {
625
+ return void 0;
626
+ }
627
+ return this.createDataUrl(entry.value, entry.attributes.contentType);
628
+ }
629
+ get_base64(key) {
630
+ const entry = this.stm[key];
631
+ if (!entry || entry.attributes.type !== "image" && entry.attributes.type !== "file") {
632
+ return void 0;
633
+ }
634
+ return entry.value;
635
+ }
636
+ has(key) {
637
+ if (key === "$date" || key === "$time") {
638
+ return true;
639
+ }
640
+ return key in this.stm;
641
+ }
642
+ delete(key) {
643
+ if (key === "$date" || key === "$time") {
644
+ return false;
645
+ }
646
+ if (!(key in this.stm)) {
647
+ return false;
648
+ }
649
+ const deleted = delete this.stm[key];
650
+ if (deleted) {
651
+ this.notifyGlobalListeners();
652
+ if (this.listeners[key]) {
653
+ this.listeners[key].forEach((listener) => listener());
654
+ }
655
+ }
656
+ return deleted;
657
+ }
658
+ clear() {
659
+ this.stm = {};
660
+ this.notifyGlobalListeners();
661
+ }
662
+ keys() {
663
+ return [...Object.keys(this.stm), "$date", "$time"];
664
+ }
665
+ values() {
666
+ const now = /* @__PURE__ */ new Date();
667
+ const stmValues = Object.values(this.stm).map((entry) => entry.value);
668
+ return [
669
+ ...stmValues,
670
+ now.toISOString().split("T")[0],
671
+ now.toTimeString().split(" ")[0]
672
+ ];
673
+ }
674
+ entries() {
675
+ const now = /* @__PURE__ */ new Date();
676
+ const stmEntries = Object.entries(this.stm).map(
677
+ ([key, entry]) => [key, entry.value]
678
+ );
679
+ return [
680
+ ...stmEntries,
681
+ ["$date", now.toISOString().split("T")[0]],
682
+ ["$time", now.toTimeString().split(" ")[0]]
683
+ ];
684
+ }
685
+ size() {
686
+ return Object.keys(this.stm).length + 2;
687
+ }
688
+ getAll() {
689
+ const now = /* @__PURE__ */ new Date();
690
+ const result = {};
691
+ Object.entries(this.stm).forEach(([key, entry]) => {
692
+ result[key] = entry.value;
693
+ });
694
+ result["$date"] = now.toISOString().split("T")[0];
695
+ result["$time"] = now.toTimeString().split(" ")[0];
696
+ return result;
697
+ }
698
+ update(newValues) {
699
+ Object.entries(newValues).forEach(([key, value]) => {
700
+ if (key !== "$date" && key !== "$time") {
701
+ this.stm[key] = {
702
+ value,
703
+ attributes: { ...DEFAULT_KEY_ATTRIBUTES }
704
+ };
705
+ if (this.listeners[key]) {
706
+ this.listeners[key].forEach((listener) => listener());
707
+ }
708
+ }
709
+ });
710
+ this.notifyGlobalListeners();
711
+ }
712
+ subscribe(key, listener) {
713
+ if (!this.listeners[key]) {
714
+ this.listeners[key] = [];
715
+ }
716
+ this.listeners[key].push(listener);
717
+ }
718
+ unsubscribe(key, listener) {
719
+ if (this.listeners[key]) {
720
+ this.listeners[key] = this.listeners[key].filter((l) => l !== listener);
721
+ }
722
+ }
723
+ subscribeToAll(listener) {
724
+ this.globalListeners.push(listener);
725
+ }
726
+ unsubscribeFromAll(listener) {
727
+ this.globalListeners = this.globalListeners.filter((l) => l !== listener);
728
+ }
729
+ notifyGlobalListeners() {
730
+ this.globalListeners.forEach((listener) => listener());
731
+ }
732
+ injectSTM(template, _processingStack) {
733
+ if (template === null || template === void 0) {
734
+ return String(template);
735
+ }
736
+ const templateStr = String(template);
737
+ const keys = templateStr.match(/\{\{([$\w]+)\}\}/g);
738
+ if (!keys) {
739
+ return templateStr;
740
+ }
741
+ const cleanKeys = keys.map((key) => key.replace(/[{}]/g, ""));
742
+ const inputValues = cleanKeys.reduce((acc, key) => {
743
+ if (key === "$date" || key === "$time") {
744
+ return {
745
+ ...acc,
746
+ [key]: this.get_value(key, _processingStack)
747
+ };
748
+ }
749
+ const attributes = this.get_attributes(key);
750
+ if (_processingStack || attributes && attributes.visible) {
751
+ if (attributes && (attributes.type === "image" || attributes.type === "file")) {
752
+ return acc;
753
+ }
754
+ return {
755
+ ...acc,
756
+ [key]: this.get_value(key, _processingStack)
757
+ };
758
+ }
759
+ return acc;
760
+ }, {});
761
+ return templateStr.replace(/\{\{([$\w]+)\}\}/g, (match, key) => {
762
+ if (inputValues[key] !== void 0) {
763
+ return inputValues[key];
764
+ }
765
+ const attributes = this.get_attributes(key);
766
+ if (attributes && (attributes.type === "image" || attributes.type === "file")) {
767
+ return match;
768
+ }
769
+ return "";
770
+ });
771
+ }
772
+ getSTM() {
773
+ const now = /* @__PURE__ */ new Date();
774
+ const entries = [];
775
+ Object.entries(this.stm).forEach(([key, entry]) => {
776
+ if (entry.attributes.visible) {
777
+ entries.push([key, this.get_value(key)]);
778
+ }
779
+ });
780
+ entries.push(["$date", now.toISOString().split("T")[0]]);
781
+ entries.push(["$time", now.toTimeString().split(" ")[0]]);
782
+ return entries.map(([key, value]) => `${key}: ${value}`).join(", ");
783
+ }
784
+ getSTMObject() {
785
+ return this.getAll();
786
+ }
787
+ getSTMForAPI() {
788
+ const now = /* @__PURE__ */ new Date();
789
+ const apiData = [];
790
+ Object.entries(this.stm).forEach(([key, entry]) => {
791
+ if (entry.attributes.visible) {
792
+ const processedValue = entry.attributes.template ? this.get_value(key) : entry.value;
793
+ apiData.push({
794
+ key,
795
+ value: processedValue,
796
+ type: entry.attributes.type,
797
+ contentType: entry.attributes.contentType
798
+ });
799
+ }
800
+ });
801
+ apiData.push({
802
+ key: "$date",
803
+ value: now.toISOString().split("T")[0],
804
+ type: "text"
805
+ });
806
+ apiData.push({
807
+ key: "$time",
808
+ value: now.toTimeString().split(" ")[0],
809
+ type: "text"
810
+ });
811
+ return apiData;
812
+ }
813
+ getVisibleImages() {
814
+ const imageParts = [];
815
+ Object.entries(this.stm).forEach(([key, entry]) => {
816
+ if (entry.attributes.visible && entry.attributes.type === "image" && entry.attributes.contentType) {
817
+ const dataUrl = this.createDataUrl(entry.value, entry.attributes.contentType);
818
+ imageParts.push({
819
+ type: "file",
820
+ mediaType: entry.attributes.contentType,
821
+ url: dataUrl,
822
+ filename: key
823
+ });
824
+ }
825
+ });
826
+ return imageParts;
827
+ }
828
+ toJSON() {
829
+ return JSON.stringify(this.serialize());
830
+ }
831
+ fromJSON(jsonString) {
832
+ try {
833
+ const data = JSON.parse(jsonString);
834
+ this.deserialize(data);
835
+ } catch (error) {
836
+ console.error("MindCache: Failed to deserialize JSON:", error);
837
+ }
838
+ }
839
+ serialize() {
840
+ const result = {};
841
+ Object.entries(this.stm).forEach(([key, entry]) => {
842
+ if (!entry.attributes.hardcoded) {
843
+ result[key] = {
844
+ value: entry.value,
845
+ attributes: { ...entry.attributes }
846
+ };
847
+ }
848
+ });
849
+ return result;
850
+ }
851
+ deserialize(data) {
852
+ if (typeof data === "object" && data !== null) {
853
+ this.clear();
854
+ Object.entries(data).forEach(([key, entry]) => {
855
+ if (entry && typeof entry === "object" && "value" in entry && "attributes" in entry) {
856
+ this.stm[key] = {
857
+ value: entry.value,
858
+ attributes: {
859
+ ...entry.attributes,
860
+ tags: entry.attributes.tags || []
861
+ }
862
+ };
863
+ }
864
+ });
865
+ this.notifyGlobalListeners();
866
+ }
867
+ }
868
+ get_system_prompt() {
869
+ const now = /* @__PURE__ */ new Date();
870
+ const promptLines = [];
871
+ Object.entries(this.stm).forEach(([key, entry]) => {
872
+ if (entry.attributes.visible) {
873
+ if (entry.attributes.type === "image") {
874
+ promptLines.push(`image ${key} available`);
875
+ return;
876
+ }
877
+ if (entry.attributes.type === "file") {
878
+ if (entry.attributes.readonly) {
879
+ promptLines.push(`${key}: [${entry.attributes.type.toUpperCase()}] - ${entry.attributes.contentType || "unknown format"}`);
880
+ } else {
881
+ const sanitizedKey = key.replace(/[^a-zA-Z0-9_-]/g, "_");
882
+ promptLines.push(`${key}: [${entry.attributes.type.toUpperCase()}] - ${entry.attributes.contentType || "unknown format"}. You can update this ${entry.attributes.type} using the write_${sanitizedKey} tool.`);
883
+ }
884
+ return;
885
+ }
886
+ const value = this.get_value(key);
887
+ const formattedValue = typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
888
+ if (entry.attributes.readonly) {
889
+ promptLines.push(`${key}: ${formattedValue}`);
890
+ } else {
891
+ const sanitizedKey = key.replace(/[^a-zA-Z0-9_-]/g, "_");
892
+ const toolInstruction = `You can rewrite "${key}" by using the write_${sanitizedKey} tool. This tool DOES NOT append \u2014 start your response with the old value (${formattedValue})`;
893
+ promptLines.push(`${key}: ${formattedValue}. ${toolInstruction}`);
894
+ }
895
+ }
896
+ });
897
+ promptLines.push(`$date: ${now.toISOString().split("T")[0]}`);
898
+ promptLines.push(`$time: ${now.toTimeString().split(" ")[0]}`);
899
+ return promptLines.join("\n");
900
+ }
901
+ findKeyFromToolName(toolName) {
902
+ if (!toolName.startsWith("write_")) {
903
+ return void 0;
904
+ }
905
+ const sanitizedKey = toolName.replace("write_", "");
906
+ const allKeys = Object.keys(this.stm);
907
+ return allKeys.find(
908
+ (k) => k.replace(/[^a-zA-Z0-9_-]/g, "_") === sanitizedKey
909
+ );
910
+ }
911
+ get_aisdk_tools() {
912
+ const tools = {};
913
+ const writableKeys = Object.entries(this.stm).filter(([, entry]) => !entry.attributes.readonly).map(([key]) => key);
914
+ writableKeys.forEach((key) => {
915
+ const sanitizedKey = key.replace(/[^a-zA-Z0-9_-]/g, "_");
916
+ const toolName = `write_${sanitizedKey}`;
917
+ const entry = this.stm[key];
918
+ const keyType = entry?.attributes.type || "text";
919
+ let inputSchema;
920
+ let description = `Write a value to the STM key: ${key}`;
921
+ if (keyType === "image" || keyType === "file") {
922
+ description += " (expects base64 encoded data)";
923
+ inputSchema = z.object({
924
+ value: z.string().describe(`Base64 encoded data for ${key}`),
925
+ contentType: z.string().optional().describe(`MIME type for the ${keyType}`)
926
+ });
927
+ } else if (keyType === "json") {
928
+ description += " (expects JSON string)";
929
+ inputSchema = z.object({
930
+ value: z.string().describe(`JSON string value for ${key}`)
931
+ });
932
+ } else {
933
+ inputSchema = z.object({
934
+ value: z.string().describe(`The text value to write to ${key}`)
935
+ });
936
+ }
937
+ tools[toolName] = {
938
+ description,
939
+ inputSchema,
940
+ execute: async (input) => {
941
+ if (keyType === "image" || keyType === "file") {
942
+ if (input.contentType) {
943
+ this.set_base64(key, input.value, input.contentType, keyType);
944
+ } else {
945
+ const existingContentType = entry?.attributes.contentType;
946
+ if (existingContentType) {
947
+ this.set_base64(key, input.value, existingContentType, keyType);
948
+ } else {
949
+ throw new Error(`Content type required for ${keyType} data`);
950
+ }
951
+ }
952
+ } else {
953
+ this.set_value(key, input.value);
954
+ }
955
+ let resultMessage;
956
+ if (keyType === "image") {
957
+ resultMessage = `Successfully saved image to ${key}`;
958
+ } else if (keyType === "file") {
959
+ resultMessage = `Successfully saved file to ${key}`;
960
+ } else if (keyType === "json") {
961
+ resultMessage = `Successfully saved JSON data to ${key}`;
962
+ } else {
963
+ resultMessage = `Successfully wrote "${input.value}" to ${key}`;
964
+ }
965
+ return {
966
+ result: resultMessage,
967
+ key,
968
+ value: input.value,
969
+ type: keyType,
970
+ contentType: input.contentType,
971
+ sanitizedKey
972
+ };
973
+ }
974
+ };
975
+ });
976
+ if (writableKeys.length === 0) {
977
+ return {};
978
+ }
979
+ return tools;
980
+ }
981
+ executeToolCall(toolName, value) {
982
+ const originalKey = this.findKeyFromToolName(toolName);
983
+ if (!originalKey) {
984
+ return null;
985
+ }
986
+ const entry = this.stm[originalKey];
987
+ if (entry && entry.attributes.readonly) {
988
+ return null;
989
+ }
990
+ this.set_value(originalKey, value);
991
+ return {
992
+ result: `Successfully wrote "${value}" to ${originalKey}`,
993
+ key: originalKey,
994
+ value
995
+ };
996
+ }
997
+ addTag(key, tag) {
998
+ if (key === "$date" || key === "$time") {
999
+ return false;
1000
+ }
1001
+ const entry = this.stm[key];
1002
+ if (!entry) {
1003
+ return false;
1004
+ }
1005
+ if (!entry.attributes.tags) {
1006
+ entry.attributes.tags = [];
1007
+ }
1008
+ if (!entry.attributes.tags.includes(tag)) {
1009
+ entry.attributes.tags.push(tag);
1010
+ this.notifyGlobalListeners();
1011
+ return true;
1012
+ }
1013
+ return false;
1014
+ }
1015
+ removeTag(key, tag) {
1016
+ if (key === "$date" || key === "$time") {
1017
+ return false;
1018
+ }
1019
+ const entry = this.stm[key];
1020
+ if (!entry || !entry.attributes.tags) {
1021
+ return false;
1022
+ }
1023
+ const tagIndex = entry.attributes.tags.indexOf(tag);
1024
+ if (tagIndex > -1) {
1025
+ entry.attributes.tags.splice(tagIndex, 1);
1026
+ this.notifyGlobalListeners();
1027
+ return true;
1028
+ }
1029
+ return false;
1030
+ }
1031
+ getTags(key) {
1032
+ if (key === "$date" || key === "$time") {
1033
+ return [];
1034
+ }
1035
+ const entry = this.stm[key];
1036
+ return entry?.attributes.tags || [];
1037
+ }
1038
+ getAllTags() {
1039
+ const allTags = /* @__PURE__ */ new Set();
1040
+ Object.values(this.stm).forEach((entry) => {
1041
+ if (entry.attributes.tags) {
1042
+ entry.attributes.tags.forEach((tag) => allTags.add(tag));
1043
+ }
1044
+ });
1045
+ return Array.from(allTags);
1046
+ }
1047
+ hasTag(key, tag) {
1048
+ if (key === "$date" || key === "$time") {
1049
+ return false;
1050
+ }
1051
+ const entry = this.stm[key];
1052
+ return entry?.attributes.tags?.includes(tag) || false;
1053
+ }
1054
+ getTagged(tag) {
1055
+ const entries = [];
1056
+ Object.entries(this.stm).forEach(([key, entry]) => {
1057
+ if (entry.attributes.tags?.includes(tag)) {
1058
+ entries.push([key, this.get_value(key)]);
1059
+ }
1060
+ });
1061
+ return entries.map(([key, value]) => `${key}: ${value}`).join(", ");
1062
+ }
1063
+ toMarkdown() {
1064
+ const now = /* @__PURE__ */ new Date();
1065
+ const lines = [];
1066
+ const appendixEntries = [];
1067
+ let appendixCounter = 0;
1068
+ lines.push("# MindCache STM Export");
1069
+ lines.push("");
1070
+ lines.push(`Export Date: ${now.toISOString().split("T")[0]}`);
1071
+ lines.push("");
1072
+ lines.push("---");
1073
+ lines.push("");
1074
+ lines.push("## STM Entries");
1075
+ lines.push("");
1076
+ Object.entries(this.stm).forEach(([key, entry]) => {
1077
+ if (entry.attributes.hardcoded) {
1078
+ return;
1079
+ }
1080
+ lines.push(`### ${key}`);
1081
+ const entryType = entry.attributes.type && entry.attributes.type !== "undefined" ? entry.attributes.type : "text";
1082
+ lines.push(`- **Type**: \`${entryType}\``);
1083
+ lines.push(`- **Readonly**: \`${entry.attributes.readonly}\``);
1084
+ lines.push(`- **Visible**: \`${entry.attributes.visible}\``);
1085
+ lines.push(`- **Template**: \`${entry.attributes.template}\``);
1086
+ if (entry.attributes.tags && entry.attributes.tags.length > 0) {
1087
+ lines.push(`- **Tags**: \`${entry.attributes.tags.join("`, `")}\``);
1088
+ }
1089
+ if (entry.attributes.contentType) {
1090
+ lines.push(`- **Content Type**: \`${entry.attributes.contentType}\``);
1091
+ }
1092
+ if (entryType === "image" || entryType === "file") {
1093
+ const label = String.fromCharCode(65 + appendixCounter);
1094
+ appendixCounter++;
1095
+ lines.push(`- **Value**: [See Appendix ${label}]`);
1096
+ appendixEntries.push({
1097
+ key,
1098
+ type: entryType,
1099
+ contentType: entry.attributes.contentType || "application/octet-stream",
1100
+ base64: entry.value,
1101
+ label
1102
+ });
1103
+ } else if (entryType === "json") {
1104
+ lines.push("- **Value**:");
1105
+ lines.push("```json");
1106
+ try {
1107
+ const jsonValue = typeof entry.value === "string" ? entry.value : JSON.stringify(entry.value, null, 2);
1108
+ lines.push(jsonValue);
1109
+ } catch {
1110
+ lines.push(String(entry.value));
1111
+ }
1112
+ lines.push("```");
1113
+ } else {
1114
+ const valueStr = String(entry.value);
1115
+ lines.push("- **Value**:");
1116
+ lines.push("```");
1117
+ lines.push(valueStr);
1118
+ lines.push("```");
1119
+ }
1120
+ lines.push("");
1121
+ lines.push("---");
1122
+ lines.push("");
1123
+ });
1124
+ if (appendixEntries.length > 0) {
1125
+ lines.push("## Appendix: Binary Data");
1126
+ lines.push("");
1127
+ appendixEntries.forEach(({ key, contentType, base64, label }) => {
1128
+ lines.push(`### Appendix ${label}: ${key}`);
1129
+ lines.push(`**Type**: ${contentType}`);
1130
+ lines.push("");
1131
+ lines.push("```");
1132
+ lines.push(base64);
1133
+ lines.push("```");
1134
+ lines.push("");
1135
+ lines.push("---");
1136
+ lines.push("");
1137
+ });
1138
+ }
1139
+ lines.push("*End of MindCache Export*");
1140
+ return lines.join("\n");
1141
+ }
1142
+ fromMarkdown(markdown) {
1143
+ const lines = markdown.split("\n");
1144
+ let currentSection = "header";
1145
+ let currentKey = null;
1146
+ let currentEntry = null;
1147
+ let inCodeBlock = false;
1148
+ let codeBlockContent = [];
1149
+ let codeBlockType = null;
1150
+ const appendixData = {};
1151
+ let currentAppendixKey = null;
1152
+ const pendingEntries = {};
1153
+ this.clear();
1154
+ for (let i = 0; i < lines.length; i++) {
1155
+ const line = lines[i];
1156
+ const trimmed = line.trim();
1157
+ if (trimmed === "## STM Entries") {
1158
+ currentSection = "entries";
1159
+ continue;
1160
+ }
1161
+ if (trimmed === "## Appendix: Binary Data") {
1162
+ currentSection = "appendix";
1163
+ continue;
1164
+ }
1165
+ if (trimmed === "```" || trimmed === "```json") {
1166
+ if (!inCodeBlock) {
1167
+ inCodeBlock = true;
1168
+ codeBlockContent = [];
1169
+ codeBlockType = currentSection === "appendix" ? "base64" : trimmed === "```json" ? "json" : "value";
1170
+ } else {
1171
+ inCodeBlock = false;
1172
+ const content = codeBlockContent.join("\n");
1173
+ if (currentSection === "appendix" && currentAppendixKey) {
1174
+ appendixData[currentAppendixKey].base64 = content;
1175
+ } else if (currentEntry && codeBlockType === "json") {
1176
+ currentEntry.value = content;
1177
+ } else if (currentEntry && codeBlockType === "value") {
1178
+ currentEntry.value = content;
1179
+ }
1180
+ codeBlockContent = [];
1181
+ codeBlockType = null;
1182
+ }
1183
+ continue;
1184
+ }
1185
+ if (inCodeBlock) {
1186
+ codeBlockContent.push(line);
1187
+ continue;
1188
+ }
1189
+ if (currentSection === "entries") {
1190
+ if (trimmed.startsWith("### ")) {
1191
+ if (currentKey && currentEntry && currentEntry.attributes) {
1192
+ pendingEntries[currentKey] = currentEntry;
1193
+ }
1194
+ currentKey = trimmed.substring(4);
1195
+ currentEntry = {
1196
+ value: void 0,
1197
+ attributes: {
1198
+ readonly: false,
1199
+ visible: true,
1200
+ hardcoded: false,
1201
+ template: false,
1202
+ type: "text",
1203
+ tags: []
1204
+ }
1205
+ };
1206
+ } else if (trimmed.startsWith("- **Type**: `")) {
1207
+ const type = trimmed.match(/`([^`]+)`/)?.[1];
1208
+ if (currentEntry && type && type !== "undefined") {
1209
+ currentEntry.attributes.type = type;
1210
+ }
1211
+ } else if (trimmed.startsWith("- **Readonly**: `")) {
1212
+ const value = trimmed.match(/`([^`]+)`/)?.[1] === "true";
1213
+ if (currentEntry) {
1214
+ currentEntry.attributes.readonly = value;
1215
+ }
1216
+ } else if (trimmed.startsWith("- **Visible**: `")) {
1217
+ const value = trimmed.match(/`([^`]+)`/)?.[1] === "true";
1218
+ if (currentEntry) {
1219
+ currentEntry.attributes.visible = value;
1220
+ }
1221
+ } else if (trimmed.startsWith("- **Template**: `")) {
1222
+ const value = trimmed.match(/`([^`]+)`/)?.[1] === "true";
1223
+ if (currentEntry) {
1224
+ currentEntry.attributes.template = value;
1225
+ }
1226
+ } else if (trimmed.startsWith("- **Tags**: `")) {
1227
+ const tagsStr = trimmed.substring(13, trimmed.length - 1);
1228
+ if (currentEntry) {
1229
+ currentEntry.attributes.tags = tagsStr.split("`, `");
1230
+ }
1231
+ } else if (trimmed.startsWith("- **Content Type**: `")) {
1232
+ const contentType = trimmed.match(/`([^`]+)`/)?.[1];
1233
+ if (currentEntry && contentType) {
1234
+ currentEntry.attributes.contentType = contentType;
1235
+ }
1236
+ } else if (trimmed.startsWith("- **Value**: `")) {
1237
+ const value = trimmed.substring(14, trimmed.length - 1);
1238
+ if (currentEntry) {
1239
+ currentEntry.value = value;
1240
+ }
1241
+ } else if (trimmed.startsWith("- **Value**: [See Appendix ")) {
1242
+ const labelMatch = trimmed.match(/Appendix ([A-Z])\]/);
1243
+ if (currentEntry && labelMatch && currentKey) {
1244
+ currentEntry.appendixLabel = labelMatch[1];
1245
+ currentEntry.value = "";
1246
+ }
1247
+ }
1248
+ }
1249
+ if (currentSection === "appendix") {
1250
+ if (trimmed.startsWith("### Appendix ")) {
1251
+ const match = trimmed.match(/### Appendix ([A-Z]): (.+)/);
1252
+ if (match) {
1253
+ const label = match[1];
1254
+ const key = match[2];
1255
+ currentAppendixKey = `${label}:${key}`;
1256
+ appendixData[currentAppendixKey] = { contentType: "", base64: "" };
1257
+ }
1258
+ } else if (trimmed.startsWith("**Type**: ")) {
1259
+ const contentType = trimmed.substring(10);
1260
+ if (currentAppendixKey) {
1261
+ appendixData[currentAppendixKey].contentType = contentType;
1262
+ }
1263
+ }
1264
+ }
1265
+ }
1266
+ if (currentKey && currentEntry && currentEntry.attributes) {
1267
+ pendingEntries[currentKey] = currentEntry;
1268
+ }
1269
+ Object.entries(pendingEntries).forEach(([key, entry]) => {
1270
+ const appendixLabel = entry.appendixLabel;
1271
+ if (appendixLabel) {
1272
+ const appendixKey = `${appendixLabel}:${key}`;
1273
+ const appendixInfo = appendixData[appendixKey];
1274
+ if (appendixInfo && appendixInfo.base64) {
1275
+ entry.value = appendixInfo.base64;
1276
+ if (!entry.attributes.contentType && appendixInfo.contentType) {
1277
+ entry.attributes.contentType = appendixInfo.contentType;
1278
+ }
1279
+ }
1280
+ }
1281
+ if (entry.value !== void 0 && entry.attributes) {
1282
+ this.stm[key] = {
1283
+ value: entry.value,
1284
+ attributes: entry.attributes
1285
+ };
1286
+ }
1287
+ });
1288
+ this.notifyGlobalListeners();
1289
+ }
1290
+ };
1291
+ var mindcache = new MindCache();
1292
+
1293
+ // src/cloud/index.ts
1294
+ init_CloudAdapter();
1295
+ init_CloudAdapter();
1296
+
1297
+ export { CloudAdapter, DEFAULT_KEY_ATTRIBUTES, MindCache, mindcache };
1298
+ //# sourceMappingURL=index.mjs.map
1299
+ //# sourceMappingURL=index.mjs.map