mindcache 3.2.0 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -401,7 +401,7 @@ var MindCache = class {
401
401
  listeners = {};
402
402
  globalListeners = [];
403
403
  // Metadata
404
- version = "3.1.0";
404
+ version = "3.3.1";
405
405
  // Internal flag to prevent sync loops when receiving remote updates
406
406
  // (Less critical with Yjs but kept for API compat)
407
407
  _isRemoteUpdate = false;
@@ -461,8 +461,22 @@ var MindCache = class {
461
461
  constructor(options) {
462
462
  this.doc = new Y.Doc();
463
463
  this.rootMap = this.doc.getMap("mindcache");
464
- this.rootMap.observe((event) => {
465
- event.keysChanged.forEach((key) => {
464
+ this.rootMap.observeDeep((events) => {
465
+ const keysAffected = /* @__PURE__ */ new Set();
466
+ events.forEach((event) => {
467
+ if (event.target === this.rootMap) {
468
+ const mapEvent = event;
469
+ mapEvent.keysChanged.forEach((key) => keysAffected.add(key));
470
+ } else if (event.target.parent === this.rootMap) {
471
+ for (const [key, val] of this.rootMap) {
472
+ if (val === event.target) {
473
+ keysAffected.add(key);
474
+ break;
475
+ }
476
+ }
477
+ }
478
+ });
479
+ keysAffected.forEach((key) => {
466
480
  const entryMap = this.rootMap.get(key);
467
481
  if (entryMap) {
468
482
  const value = entryMap.get("value");
@@ -475,8 +489,6 @@ var MindCache = class {
475
489
  }
476
490
  }
477
491
  });
478
- });
479
- this.rootMap.observeDeep((_events) => {
480
492
  this.notifyGlobalListeners();
481
493
  });
482
494
  this.initGlobalUndoManager();
@@ -801,18 +813,35 @@ var MindCache = class {
801
813
  }
802
814
  // Deserialize state (for IndexedDBAdapter compatibility)
803
815
  deserialize(data) {
816
+ if (!data || typeof data !== "object") {
817
+ return;
818
+ }
804
819
  this.doc.transact(() => {
820
+ for (const key of this.rootMap.keys()) {
821
+ this.rootMap.delete(key);
822
+ }
805
823
  for (const [key, entry] of Object.entries(data)) {
806
824
  if (key.startsWith("$")) {
807
825
  continue;
808
826
  }
809
- let entryMap = this.rootMap.get(key);
810
- if (!entryMap) {
811
- entryMap = new Y.Map();
812
- this.rootMap.set(key, entryMap);
813
- }
827
+ const entryMap = new Y.Map();
828
+ this.rootMap.set(key, entryMap);
814
829
  entryMap.set("value", entry.value);
815
- entryMap.set("attributes", entry.attributes);
830
+ const attrs = entry.attributes || {};
831
+ const normalizedAttrs = {
832
+ type: attrs.type || "text",
833
+ contentTags: attrs.contentTags || [],
834
+ systemTags: attrs.systemTags || this.normalizeSystemTags(attrs.visible !== false ? ["prompt"] : []),
835
+ zIndex: attrs.zIndex ?? 0,
836
+ // Legacy fields
837
+ readonly: attrs.readonly ?? false,
838
+ visible: attrs.visible ?? true,
839
+ hardcoded: attrs.hardcoded ?? false,
840
+ template: attrs.template ?? false,
841
+ tags: attrs.tags || [],
842
+ contentType: attrs.contentType
843
+ };
844
+ entryMap.set("attributes", normalizedAttrs);
816
845
  }
817
846
  });
818
847
  }
@@ -850,16 +879,46 @@ var MindCache = class {
850
879
  }
851
880
  return false;
852
881
  }
853
- // InjectSTM replacement
854
- injectSTM(template, _processingStack) {
882
+ // InjectSTM replacement (private helper)
883
+ _injectSTMInternal(template, _processingStack) {
855
884
  return template.replace(/\{\{([^}]+)\}\}/g, (_, key) => {
856
885
  const val = this.get_value(key.trim(), _processingStack);
857
886
  return val !== void 0 ? String(val) : `{{${key}}}`;
858
887
  });
859
888
  }
889
+ /**
890
+ * Replace {{key}} placeholders in a template string with values from MindCache.
891
+ * @param template The template string with {{key}} placeholders
892
+ * @returns The template with placeholders replaced by values
893
+ */
894
+ injectSTM(template) {
895
+ return this._injectSTMInternal(template, /* @__PURE__ */ new Set());
896
+ }
860
897
  // Public API Methods
861
898
  getAll() {
862
- return this.serialize();
899
+ const result = {};
900
+ for (const [key] of this.rootMap) {
901
+ result[key] = this.get_value(key);
902
+ }
903
+ result["$date"] = this.get_value("$date");
904
+ result["$time"] = this.get_value("$time");
905
+ return result;
906
+ }
907
+ /**
908
+ * Get all entries with their full structure (value + attributes).
909
+ * Use this for UI/admin interfaces that need to display key properties.
910
+ * Unlike serialize(), this format is stable and won't change.
911
+ */
912
+ getAllEntries() {
913
+ const result = {};
914
+ for (const [key] of this.rootMap) {
915
+ const value = this.get_value(key);
916
+ const attributes = this.get_attributes(key);
917
+ if (attributes) {
918
+ result[key] = { value, attributes };
919
+ }
920
+ }
921
+ return result;
863
922
  }
864
923
  get_value(key, _processingStack) {
865
924
  if (key === "$date") {
@@ -889,7 +948,7 @@ var MindCache = class {
889
948
  if (typeof value === "string") {
890
949
  const stack = _processingStack || /* @__PURE__ */ new Set();
891
950
  stack.add(key);
892
- return this.injectSTM(value, stack);
951
+ return this._injectSTMInternal(value, stack);
893
952
  }
894
953
  }
895
954
  return value;
@@ -987,6 +1046,638 @@ var MindCache = class {
987
1046
  keys.forEach((k) => this.rootMap.delete(k));
988
1047
  });
989
1048
  }
1049
+ // ============================================
1050
+ // Restored Methods (from v2.x)
1051
+ // ============================================
1052
+ /**
1053
+ * Check if a key exists in MindCache.
1054
+ */
1055
+ has(key) {
1056
+ if (key === "$date" || key === "$time" || key === "$version") {
1057
+ return true;
1058
+ }
1059
+ return this.rootMap.has(key);
1060
+ }
1061
+ /**
1062
+ * Delete a key from MindCache.
1063
+ * @returns true if the key existed and was deleted
1064
+ */
1065
+ delete(key) {
1066
+ if (key === "$date" || key === "$time" || key === "$version") {
1067
+ return false;
1068
+ }
1069
+ if (!this.rootMap.has(key)) {
1070
+ return false;
1071
+ }
1072
+ this.rootMap.delete(key);
1073
+ this.notifyGlobalListeners();
1074
+ if (this.listeners[key]) {
1075
+ this.listeners[key].forEach((listener) => listener(void 0));
1076
+ }
1077
+ return true;
1078
+ }
1079
+ /** @deprecated Use get_value instead */
1080
+ get(key) {
1081
+ return this.get_value(key);
1082
+ }
1083
+ /** @deprecated Use set_value instead */
1084
+ set(key, value) {
1085
+ this.set_value(key, value);
1086
+ }
1087
+ /**
1088
+ * Update multiple values at once from an object.
1089
+ * @deprecated Use set_value for individual keys
1090
+ */
1091
+ update(data) {
1092
+ this.doc.transact(() => {
1093
+ for (const [key, value] of Object.entries(data)) {
1094
+ if (key !== "$date" && key !== "$time" && key !== "$version") {
1095
+ this.set_value(key, value);
1096
+ }
1097
+ }
1098
+ });
1099
+ this.notifyGlobalListeners();
1100
+ }
1101
+ /**
1102
+ * Get the number of keys in MindCache.
1103
+ */
1104
+ size() {
1105
+ return this.rootMap.size + 2;
1106
+ }
1107
+ /**
1108
+ * Get all keys in MindCache (including temporal keys).
1109
+ */
1110
+ keys() {
1111
+ const keys = Array.from(this.rootMap.keys());
1112
+ keys.push("$date", "$time");
1113
+ return keys;
1114
+ }
1115
+ /**
1116
+ * Get all values in MindCache (including temporal values).
1117
+ */
1118
+ values() {
1119
+ const result = [];
1120
+ for (const [key] of this.rootMap) {
1121
+ result.push(this.get_value(key));
1122
+ }
1123
+ result.push(this.get_value("$date"));
1124
+ result.push(this.get_value("$time"));
1125
+ return result;
1126
+ }
1127
+ /**
1128
+ * Get all key-value entries (including temporal entries).
1129
+ */
1130
+ entries() {
1131
+ const result = [];
1132
+ for (const [key] of this.rootMap) {
1133
+ result.push([key, this.get_value(key)]);
1134
+ }
1135
+ result.push(["$date", this.get_value("$date")]);
1136
+ result.push(["$time", this.get_value("$time")]);
1137
+ return result;
1138
+ }
1139
+ /**
1140
+ * Unsubscribe from key changes.
1141
+ * @deprecated Use the cleanup function returned by subscribe() instead
1142
+ */
1143
+ unsubscribe(key, listener) {
1144
+ if (this.listeners[key]) {
1145
+ this.listeners[key] = this.listeners[key].filter((l) => l !== listener);
1146
+ }
1147
+ }
1148
+ /**
1149
+ * Get the STM as a formatted string for LLM context.
1150
+ * @deprecated Use get_system_prompt() instead
1151
+ */
1152
+ getSTM() {
1153
+ return this.get_system_prompt();
1154
+ }
1155
+ /**
1156
+ * Get the STM as an object with values directly (no attributes).
1157
+ * Includes system keys ($date, $time).
1158
+ * @deprecated Use getAll() for full STM format
1159
+ */
1160
+ getSTMObject() {
1161
+ const result = {};
1162
+ for (const [key] of this.rootMap) {
1163
+ result[key] = this.get_value(key);
1164
+ }
1165
+ result["$date"] = this.get_value("$date");
1166
+ result["$time"] = this.get_value("$time");
1167
+ return result;
1168
+ }
1169
+ /**
1170
+ * Add a content tag to a key.
1171
+ * @returns true if the tag was added, false if key doesn't exist or tag already exists
1172
+ */
1173
+ addTag(key, tag) {
1174
+ if (key === "$date" || key === "$time" || key === "$version") {
1175
+ return false;
1176
+ }
1177
+ const entryMap = this.rootMap.get(key);
1178
+ if (!entryMap) {
1179
+ return false;
1180
+ }
1181
+ const attributes = entryMap.get("attributes");
1182
+ const contentTags = attributes?.contentTags || [];
1183
+ if (contentTags.includes(tag)) {
1184
+ return false;
1185
+ }
1186
+ this.doc.transact(() => {
1187
+ const newContentTags = [...contentTags, tag];
1188
+ entryMap.set("attributes", {
1189
+ ...attributes,
1190
+ contentTags: newContentTags,
1191
+ tags: newContentTags
1192
+ // Sync legacy tags array
1193
+ });
1194
+ });
1195
+ this.notifyGlobalListeners();
1196
+ return true;
1197
+ }
1198
+ /**
1199
+ * Remove a content tag from a key.
1200
+ * @returns true if the tag was removed
1201
+ */
1202
+ removeTag(key, tag) {
1203
+ if (key === "$date" || key === "$time" || key === "$version") {
1204
+ return false;
1205
+ }
1206
+ const entryMap = this.rootMap.get(key);
1207
+ if (!entryMap) {
1208
+ return false;
1209
+ }
1210
+ const attributes = entryMap.get("attributes");
1211
+ const contentTags = attributes?.contentTags || [];
1212
+ const tagIndex = contentTags.indexOf(tag);
1213
+ if (tagIndex === -1) {
1214
+ return false;
1215
+ }
1216
+ this.doc.transact(() => {
1217
+ const newContentTags = contentTags.filter((t) => t !== tag);
1218
+ entryMap.set("attributes", {
1219
+ ...attributes,
1220
+ contentTags: newContentTags,
1221
+ tags: newContentTags
1222
+ // Sync legacy tags array
1223
+ });
1224
+ });
1225
+ this.notifyGlobalListeners();
1226
+ return true;
1227
+ }
1228
+ /**
1229
+ * Get all content tags for a key.
1230
+ */
1231
+ getTags(key) {
1232
+ if (key === "$date" || key === "$time" || key === "$version") {
1233
+ return [];
1234
+ }
1235
+ const entryMap = this.rootMap.get(key);
1236
+ if (!entryMap) {
1237
+ return [];
1238
+ }
1239
+ const attributes = entryMap.get("attributes");
1240
+ return attributes?.contentTags || [];
1241
+ }
1242
+ /**
1243
+ * Get all unique content tags across all keys.
1244
+ */
1245
+ getAllTags() {
1246
+ const allTags = /* @__PURE__ */ new Set();
1247
+ for (const [, val] of this.rootMap) {
1248
+ const entryMap = val;
1249
+ const attributes = entryMap.get("attributes");
1250
+ if (attributes?.contentTags) {
1251
+ attributes.contentTags.forEach((tag) => allTags.add(tag));
1252
+ }
1253
+ }
1254
+ return Array.from(allTags);
1255
+ }
1256
+ /**
1257
+ * Check if a key has a specific content tag.
1258
+ */
1259
+ hasTag(key, tag) {
1260
+ if (key === "$date" || key === "$time" || key === "$version") {
1261
+ return false;
1262
+ }
1263
+ const entryMap = this.rootMap.get(key);
1264
+ if (!entryMap) {
1265
+ return false;
1266
+ }
1267
+ const attributes = entryMap.get("attributes");
1268
+ return attributes?.contentTags?.includes(tag) || false;
1269
+ }
1270
+ /**
1271
+ * Get all keys with a specific content tag as formatted string.
1272
+ */
1273
+ getTagged(tag) {
1274
+ const entries = [];
1275
+ const keys = this.getSortedKeys();
1276
+ keys.forEach((key) => {
1277
+ if (this.hasTag(key, tag)) {
1278
+ entries.push([key, this.get_value(key)]);
1279
+ }
1280
+ });
1281
+ return entries.map(([key, value]) => `${key}: ${value}`).join(", ");
1282
+ }
1283
+ /**
1284
+ * Get array of keys with a specific content tag.
1285
+ */
1286
+ getKeysByTag(tag) {
1287
+ const keys = this.getSortedKeys();
1288
+ return keys.filter((key) => this.hasTag(key, tag));
1289
+ }
1290
+ // ============================================
1291
+ // System Tag Methods (requires system access level)
1292
+ // ============================================
1293
+ /**
1294
+ * Add a system tag to a key (requires system access).
1295
+ * System tags: 'SystemPrompt', 'LLMRead', 'LLMWrite', 'readonly', 'protected', 'ApplyTemplate'
1296
+ */
1297
+ systemAddTag(key, tag) {
1298
+ if (!this.hasSystemAccess) {
1299
+ console.warn("MindCache: systemAddTag requires system access level");
1300
+ return false;
1301
+ }
1302
+ if (key === "$date" || key === "$time" || key === "$version") {
1303
+ return false;
1304
+ }
1305
+ const entryMap = this.rootMap.get(key);
1306
+ if (!entryMap) {
1307
+ return false;
1308
+ }
1309
+ const attributes = entryMap.get("attributes");
1310
+ const systemTags = attributes?.systemTags || [];
1311
+ if (systemTags.includes(tag)) {
1312
+ return false;
1313
+ }
1314
+ this.doc.transact(() => {
1315
+ const newSystemTags = [...systemTags, tag];
1316
+ const normalizedTags = this.normalizeSystemTags(newSystemTags);
1317
+ entryMap.set("attributes", {
1318
+ ...attributes,
1319
+ systemTags: normalizedTags
1320
+ });
1321
+ });
1322
+ this.notifyGlobalListeners();
1323
+ return true;
1324
+ }
1325
+ /**
1326
+ * Remove a system tag from a key (requires system access).
1327
+ */
1328
+ systemRemoveTag(key, tag) {
1329
+ if (!this.hasSystemAccess) {
1330
+ console.warn("MindCache: systemRemoveTag requires system access level");
1331
+ return false;
1332
+ }
1333
+ if (key === "$date" || key === "$time" || key === "$version") {
1334
+ return false;
1335
+ }
1336
+ const entryMap = this.rootMap.get(key);
1337
+ if (!entryMap) {
1338
+ return false;
1339
+ }
1340
+ const attributes = entryMap.get("attributes");
1341
+ const systemTags = attributes?.systemTags || [];
1342
+ const tagIndex = systemTags.indexOf(tag);
1343
+ if (tagIndex === -1) {
1344
+ return false;
1345
+ }
1346
+ this.doc.transact(() => {
1347
+ const newSystemTags = systemTags.filter((t) => t !== tag);
1348
+ entryMap.set("attributes", {
1349
+ ...attributes,
1350
+ systemTags: newSystemTags
1351
+ });
1352
+ });
1353
+ this.notifyGlobalListeners();
1354
+ return true;
1355
+ }
1356
+ /**
1357
+ * Get all system tags for a key (requires system access).
1358
+ */
1359
+ systemGetTags(key) {
1360
+ if (!this.hasSystemAccess) {
1361
+ console.warn("MindCache: systemGetTags requires system access level");
1362
+ return [];
1363
+ }
1364
+ if (key === "$date" || key === "$time" || key === "$version") {
1365
+ return [];
1366
+ }
1367
+ const entryMap = this.rootMap.get(key);
1368
+ if (!entryMap) {
1369
+ return [];
1370
+ }
1371
+ const attributes = entryMap.get("attributes");
1372
+ return attributes?.systemTags || [];
1373
+ }
1374
+ /**
1375
+ * Check if a key has a specific system tag (requires system access).
1376
+ */
1377
+ systemHasTag(key, tag) {
1378
+ if (!this.hasSystemAccess) {
1379
+ console.warn("MindCache: systemHasTag requires system access level");
1380
+ return false;
1381
+ }
1382
+ if (key === "$date" || key === "$time" || key === "$version") {
1383
+ return false;
1384
+ }
1385
+ const entryMap = this.rootMap.get(key);
1386
+ if (!entryMap) {
1387
+ return false;
1388
+ }
1389
+ const attributes = entryMap.get("attributes");
1390
+ return attributes?.systemTags?.includes(tag) || false;
1391
+ }
1392
+ /**
1393
+ * Set all system tags for a key at once (requires system access).
1394
+ */
1395
+ systemSetTags(key, tags) {
1396
+ if (!this.hasSystemAccess) {
1397
+ console.warn("MindCache: systemSetTags requires system access level");
1398
+ return false;
1399
+ }
1400
+ if (key === "$date" || key === "$time" || key === "$version") {
1401
+ return false;
1402
+ }
1403
+ const entryMap = this.rootMap.get(key);
1404
+ if (!entryMap) {
1405
+ return false;
1406
+ }
1407
+ this.doc.transact(() => {
1408
+ const attributes = entryMap.get("attributes");
1409
+ entryMap.set("attributes", {
1410
+ ...attributes,
1411
+ systemTags: [...tags]
1412
+ });
1413
+ });
1414
+ this.notifyGlobalListeners();
1415
+ return true;
1416
+ }
1417
+ /**
1418
+ * Get all keys with a specific system tag (requires system access).
1419
+ */
1420
+ systemGetKeysByTag(tag) {
1421
+ if (!this.hasSystemAccess) {
1422
+ console.warn("MindCache: systemGetKeysByTag requires system access level");
1423
+ return [];
1424
+ }
1425
+ const keys = this.getSortedKeys();
1426
+ return keys.filter((key) => this.systemHasTag(key, tag));
1427
+ }
1428
+ /**
1429
+ * Helper to get sorted keys (by zIndex).
1430
+ */
1431
+ getSortedKeys() {
1432
+ const entries = [];
1433
+ for (const [key, val] of this.rootMap) {
1434
+ const entryMap = val;
1435
+ const attributes = entryMap.get("attributes");
1436
+ entries.push({ key, zIndex: attributes?.zIndex ?? 0 });
1437
+ }
1438
+ return entries.sort((a, b) => a.zIndex - b.zIndex).map((e) => e.key);
1439
+ }
1440
+ /**
1441
+ * Serialize to JSON string.
1442
+ */
1443
+ toJSON() {
1444
+ return JSON.stringify(this.serialize());
1445
+ }
1446
+ /**
1447
+ * Deserialize from JSON string.
1448
+ */
1449
+ fromJSON(jsonString) {
1450
+ try {
1451
+ const data = JSON.parse(jsonString);
1452
+ this.deserialize(data);
1453
+ } catch (error) {
1454
+ console.error("MindCache: Failed to deserialize JSON:", error);
1455
+ }
1456
+ }
1457
+ /**
1458
+ * Export to Markdown format.
1459
+ */
1460
+ toMarkdown() {
1461
+ const now = /* @__PURE__ */ new Date();
1462
+ const lines = [];
1463
+ const appendixEntries = [];
1464
+ let appendixCounter = 0;
1465
+ lines.push("# MindCache STM Export");
1466
+ lines.push("");
1467
+ lines.push(`Export Date: ${now.toISOString().split("T")[0]}`);
1468
+ lines.push("");
1469
+ lines.push("---");
1470
+ lines.push("");
1471
+ lines.push("## STM Entries");
1472
+ lines.push("");
1473
+ const sortedKeys = this.getSortedKeys();
1474
+ sortedKeys.forEach((key) => {
1475
+ const entryMap = this.rootMap.get(key);
1476
+ if (!entryMap) {
1477
+ return;
1478
+ }
1479
+ const attributes = entryMap.get("attributes");
1480
+ const value = entryMap.get("value");
1481
+ if (attributes?.hardcoded) {
1482
+ return;
1483
+ }
1484
+ lines.push(`### ${key}`);
1485
+ const entryType = attributes?.type || "text";
1486
+ lines.push(`- **Type**: \`${entryType}\``);
1487
+ lines.push(`- **Readonly**: \`${attributes?.readonly ?? false}\``);
1488
+ lines.push(`- **Visible**: \`${attributes?.visible ?? true}\``);
1489
+ lines.push(`- **Template**: \`${attributes?.template ?? false}\``);
1490
+ lines.push(`- **Z-Index**: \`${attributes?.zIndex ?? 0}\``);
1491
+ if (attributes?.contentTags && attributes.contentTags.length > 0) {
1492
+ lines.push(`- **Tags**: \`${attributes.contentTags.join("`, `")}\``);
1493
+ }
1494
+ if (attributes?.contentType) {
1495
+ lines.push(`- **Content Type**: \`${attributes.contentType}\``);
1496
+ }
1497
+ if (entryType === "image" || entryType === "file") {
1498
+ const label = String.fromCharCode(65 + appendixCounter);
1499
+ appendixCounter++;
1500
+ lines.push(`- **Value**: [See Appendix ${label}]`);
1501
+ appendixEntries.push({
1502
+ key,
1503
+ type: entryType,
1504
+ contentType: attributes?.contentType || "application/octet-stream",
1505
+ base64: value,
1506
+ label
1507
+ });
1508
+ } else if (entryType === "json") {
1509
+ lines.push("- **Value**:");
1510
+ lines.push("```json");
1511
+ try {
1512
+ const jsonValue = typeof value === "string" ? value : JSON.stringify(value, null, 2);
1513
+ lines.push(jsonValue);
1514
+ } catch {
1515
+ lines.push(String(value));
1516
+ }
1517
+ lines.push("```");
1518
+ } else {
1519
+ lines.push(`- **Value**: ${value}`);
1520
+ }
1521
+ lines.push("");
1522
+ });
1523
+ if (appendixEntries.length > 0) {
1524
+ lines.push("---");
1525
+ lines.push("");
1526
+ lines.push("## Appendix: Binary Data");
1527
+ lines.push("");
1528
+ appendixEntries.forEach((entry) => {
1529
+ lines.push(`### Appendix ${entry.label}: ${entry.key}`);
1530
+ lines.push(`- **Type**: \`${entry.type}\``);
1531
+ lines.push(`- **Content Type**: \`${entry.contentType}\``);
1532
+ lines.push("- **Base64 Data**:");
1533
+ lines.push("```");
1534
+ lines.push(entry.base64);
1535
+ lines.push("```");
1536
+ lines.push("");
1537
+ });
1538
+ }
1539
+ return lines.join("\n");
1540
+ }
1541
+ /**
1542
+ * Import from Markdown format.
1543
+ */
1544
+ fromMarkdown(markdown) {
1545
+ const lines = markdown.split("\n");
1546
+ let currentKey = null;
1547
+ let currentAttributes = {};
1548
+ let currentValue = null;
1549
+ let inCodeBlock = false;
1550
+ let codeBlockContent = [];
1551
+ for (const line of lines) {
1552
+ if (line.startsWith("### ") && !line.startsWith("### Appendix")) {
1553
+ if (currentKey && currentValue !== null) {
1554
+ this.set_value(currentKey, currentValue, currentAttributes);
1555
+ }
1556
+ currentKey = line.substring(4).trim();
1557
+ currentAttributes = {};
1558
+ currentValue = null;
1559
+ continue;
1560
+ }
1561
+ if (line.startsWith("### Appendix ")) {
1562
+ const match = line.match(/### Appendix ([A-Z]): (.+)/);
1563
+ if (match) {
1564
+ currentKey = match[2];
1565
+ }
1566
+ continue;
1567
+ }
1568
+ if (line.startsWith("- **Type**:")) {
1569
+ const type = line.match(/`(.+)`/)?.[1];
1570
+ if (type) {
1571
+ currentAttributes.type = type;
1572
+ }
1573
+ continue;
1574
+ }
1575
+ if (line.startsWith("- **Readonly**:")) {
1576
+ currentAttributes.readonly = line.includes("`true`");
1577
+ continue;
1578
+ }
1579
+ if (line.startsWith("- **Visible**:")) {
1580
+ currentAttributes.visible = line.includes("`true`");
1581
+ continue;
1582
+ }
1583
+ if (line.startsWith("- **Template**:")) {
1584
+ currentAttributes.template = line.includes("`true`");
1585
+ continue;
1586
+ }
1587
+ if (line.startsWith("- **Z-Index**:")) {
1588
+ const zIndex = parseInt(line.match(/`(\d+)`/)?.[1] || "0", 10);
1589
+ currentAttributes.zIndex = zIndex;
1590
+ continue;
1591
+ }
1592
+ if (line.startsWith("- **Tags**:")) {
1593
+ const tags = line.match(/`([^`]+)`/g)?.map((t) => t.slice(1, -1)) || [];
1594
+ currentAttributes.contentTags = tags;
1595
+ currentAttributes.tags = tags;
1596
+ continue;
1597
+ }
1598
+ if (line.startsWith("- **Content Type**:")) {
1599
+ currentAttributes.contentType = line.match(/`(.+)`/)?.[1];
1600
+ continue;
1601
+ }
1602
+ if (line.startsWith("- **Value**:") && !line.includes("[See Appendix")) {
1603
+ currentValue = line.substring(12).trim();
1604
+ continue;
1605
+ }
1606
+ if (line === "```json" || line === "```") {
1607
+ if (inCodeBlock) {
1608
+ inCodeBlock = false;
1609
+ if (currentKey && codeBlockContent.length > 0) {
1610
+ currentValue = codeBlockContent.join("\n");
1611
+ }
1612
+ codeBlockContent = [];
1613
+ } else {
1614
+ inCodeBlock = true;
1615
+ }
1616
+ continue;
1617
+ }
1618
+ if (inCodeBlock) {
1619
+ codeBlockContent.push(line);
1620
+ }
1621
+ }
1622
+ if (currentKey && currentValue !== null) {
1623
+ this.set_value(currentKey, currentValue, currentAttributes);
1624
+ }
1625
+ }
1626
+ /**
1627
+ * Set base64 binary data.
1628
+ */
1629
+ set_base64(key, base64Data, contentType, type = "file", attributes) {
1630
+ if (!this.validateContentType(type, contentType)) {
1631
+ throw new Error(`Invalid content type ${contentType} for type ${type}`);
1632
+ }
1633
+ const fileAttributes = {
1634
+ type,
1635
+ contentType,
1636
+ ...attributes
1637
+ };
1638
+ this.set_value(key, base64Data, fileAttributes);
1639
+ }
1640
+ /**
1641
+ * Add an image from base64 data.
1642
+ */
1643
+ add_image(key, base64Data, contentType = "image/jpeg", attributes) {
1644
+ if (!contentType.startsWith("image/")) {
1645
+ throw new Error(`Invalid image content type: ${contentType}. Must start with 'image/'`);
1646
+ }
1647
+ this.set_base64(key, base64Data, contentType, "image", attributes);
1648
+ }
1649
+ /**
1650
+ * Get the data URL for an image or file key.
1651
+ */
1652
+ get_data_url(key) {
1653
+ const entryMap = this.rootMap.get(key);
1654
+ if (!entryMap) {
1655
+ return void 0;
1656
+ }
1657
+ const attributes = entryMap.get("attributes");
1658
+ if (attributes?.type !== "image" && attributes?.type !== "file") {
1659
+ return void 0;
1660
+ }
1661
+ if (!attributes?.contentType) {
1662
+ return void 0;
1663
+ }
1664
+ const value = entryMap.get("value");
1665
+ return this.createDataUrl(value, attributes.contentType);
1666
+ }
1667
+ /**
1668
+ * Get the base64 data for an image or file key.
1669
+ */
1670
+ get_base64(key) {
1671
+ const entryMap = this.rootMap.get(key);
1672
+ if (!entryMap) {
1673
+ return void 0;
1674
+ }
1675
+ const attributes = entryMap.get("attributes");
1676
+ if (attributes?.type !== "image" && attributes?.type !== "file") {
1677
+ return void 0;
1678
+ }
1679
+ return entryMap.get("value");
1680
+ }
990
1681
  // File methods
991
1682
  async set_file(key, file, attributes) {
992
1683
  const base64 = await this.encodeFileToBase64(file);
@@ -1307,10 +1998,14 @@ var MindCache = class {
1307
1998
  const sanitizedKey = this.sanitizeKeyForTool(key);
1308
1999
  if (isWritable) {
1309
2000
  if (isDocument) {
1310
- lines.push(`${key}: ${displayValue}. Document tools: write_${sanitizedKey}, append_${sanitizedKey}, edit_${sanitizedKey}`);
2001
+ lines.push(
2002
+ `${key}: ${displayValue}. Document tools: write_${sanitizedKey}, append_${sanitizedKey}, edit_${sanitizedKey}`
2003
+ );
1311
2004
  } else {
1312
2005
  const oldValueHint = displayValue ? ` This tool DOES NOT append \u2014 start your response with the old value (${displayValue})` : "";
1313
- lines.push(`${key}: ${displayValue}. You can rewrite "${key}" by using the write_${sanitizedKey} tool.${oldValueHint}`);
2006
+ lines.push(
2007
+ `${key}: ${displayValue}. You can rewrite "${key}" by using the write_${sanitizedKey} tool.${oldValueHint}`
2008
+ );
1314
2009
  }
1315
2010
  } else {
1316
2011
  lines.push(`${key}: ${displayValue}`);
@@ -1451,6 +2146,9 @@ function useMindCache(options) {
1451
2146
  };
1452
2147
  }
1453
2148
 
1454
- export { CloudAdapter, DEFAULT_KEY_ATTRIBUTES, IndexedDBAdapter, MindCache, useMindCache };
2149
+ // src/index.ts
2150
+ var mindcache = new MindCache();
2151
+
2152
+ export { CloudAdapter, DEFAULT_KEY_ATTRIBUTES, IndexedDBAdapter, MindCache, mindcache, useMindCache };
1455
2153
  //# sourceMappingURL=index.mjs.map
1456
2154
  //# sourceMappingURL=index.mjs.map