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.
@@ -428,7 +428,7 @@ var MindCache = class {
428
428
  listeners = {};
429
429
  globalListeners = [];
430
430
  // Metadata
431
- version = "3.1.0";
431
+ version = "3.3.1";
432
432
  // Internal flag to prevent sync loops when receiving remote updates
433
433
  // (Less critical with Yjs but kept for API compat)
434
434
  _isRemoteUpdate = false;
@@ -488,8 +488,22 @@ var MindCache = class {
488
488
  constructor(options) {
489
489
  this.doc = new Y__namespace.Doc();
490
490
  this.rootMap = this.doc.getMap("mindcache");
491
- this.rootMap.observe((event) => {
492
- event.keysChanged.forEach((key) => {
491
+ this.rootMap.observeDeep((events) => {
492
+ const keysAffected = /* @__PURE__ */ new Set();
493
+ events.forEach((event) => {
494
+ if (event.target === this.rootMap) {
495
+ const mapEvent = event;
496
+ mapEvent.keysChanged.forEach((key) => keysAffected.add(key));
497
+ } else if (event.target.parent === this.rootMap) {
498
+ for (const [key, val] of this.rootMap) {
499
+ if (val === event.target) {
500
+ keysAffected.add(key);
501
+ break;
502
+ }
503
+ }
504
+ }
505
+ });
506
+ keysAffected.forEach((key) => {
493
507
  const entryMap = this.rootMap.get(key);
494
508
  if (entryMap) {
495
509
  const value = entryMap.get("value");
@@ -502,8 +516,6 @@ var MindCache = class {
502
516
  }
503
517
  }
504
518
  });
505
- });
506
- this.rootMap.observeDeep((_events) => {
507
519
  this.notifyGlobalListeners();
508
520
  });
509
521
  this.initGlobalUndoManager();
@@ -828,18 +840,35 @@ var MindCache = class {
828
840
  }
829
841
  // Deserialize state (for IndexedDBAdapter compatibility)
830
842
  deserialize(data) {
843
+ if (!data || typeof data !== "object") {
844
+ return;
845
+ }
831
846
  this.doc.transact(() => {
847
+ for (const key of this.rootMap.keys()) {
848
+ this.rootMap.delete(key);
849
+ }
832
850
  for (const [key, entry] of Object.entries(data)) {
833
851
  if (key.startsWith("$")) {
834
852
  continue;
835
853
  }
836
- let entryMap = this.rootMap.get(key);
837
- if (!entryMap) {
838
- entryMap = new Y__namespace.Map();
839
- this.rootMap.set(key, entryMap);
840
- }
854
+ const entryMap = new Y__namespace.Map();
855
+ this.rootMap.set(key, entryMap);
841
856
  entryMap.set("value", entry.value);
842
- entryMap.set("attributes", entry.attributes);
857
+ const attrs = entry.attributes || {};
858
+ const normalizedAttrs = {
859
+ type: attrs.type || "text",
860
+ contentTags: attrs.contentTags || [],
861
+ systemTags: attrs.systemTags || this.normalizeSystemTags(attrs.visible !== false ? ["prompt"] : []),
862
+ zIndex: attrs.zIndex ?? 0,
863
+ // Legacy fields
864
+ readonly: attrs.readonly ?? false,
865
+ visible: attrs.visible ?? true,
866
+ hardcoded: attrs.hardcoded ?? false,
867
+ template: attrs.template ?? false,
868
+ tags: attrs.tags || [],
869
+ contentType: attrs.contentType
870
+ };
871
+ entryMap.set("attributes", normalizedAttrs);
843
872
  }
844
873
  });
845
874
  }
@@ -877,16 +906,46 @@ var MindCache = class {
877
906
  }
878
907
  return false;
879
908
  }
880
- // InjectSTM replacement
881
- injectSTM(template, _processingStack) {
909
+ // InjectSTM replacement (private helper)
910
+ _injectSTMInternal(template, _processingStack) {
882
911
  return template.replace(/\{\{([^}]+)\}\}/g, (_, key) => {
883
912
  const val = this.get_value(key.trim(), _processingStack);
884
913
  return val !== void 0 ? String(val) : `{{${key}}}`;
885
914
  });
886
915
  }
916
+ /**
917
+ * Replace {{key}} placeholders in a template string with values from MindCache.
918
+ * @param template The template string with {{key}} placeholders
919
+ * @returns The template with placeholders replaced by values
920
+ */
921
+ injectSTM(template) {
922
+ return this._injectSTMInternal(template, /* @__PURE__ */ new Set());
923
+ }
887
924
  // Public API Methods
888
925
  getAll() {
889
- return this.serialize();
926
+ const result = {};
927
+ for (const [key] of this.rootMap) {
928
+ result[key] = this.get_value(key);
929
+ }
930
+ result["$date"] = this.get_value("$date");
931
+ result["$time"] = this.get_value("$time");
932
+ return result;
933
+ }
934
+ /**
935
+ * Get all entries with their full structure (value + attributes).
936
+ * Use this for UI/admin interfaces that need to display key properties.
937
+ * Unlike serialize(), this format is stable and won't change.
938
+ */
939
+ getAllEntries() {
940
+ const result = {};
941
+ for (const [key] of this.rootMap) {
942
+ const value = this.get_value(key);
943
+ const attributes = this.get_attributes(key);
944
+ if (attributes) {
945
+ result[key] = { value, attributes };
946
+ }
947
+ }
948
+ return result;
890
949
  }
891
950
  get_value(key, _processingStack) {
892
951
  if (key === "$date") {
@@ -916,7 +975,7 @@ var MindCache = class {
916
975
  if (typeof value === "string") {
917
976
  const stack = _processingStack || /* @__PURE__ */ new Set();
918
977
  stack.add(key);
919
- return this.injectSTM(value, stack);
978
+ return this._injectSTMInternal(value, stack);
920
979
  }
921
980
  }
922
981
  return value;
@@ -1014,6 +1073,638 @@ var MindCache = class {
1014
1073
  keys.forEach((k) => this.rootMap.delete(k));
1015
1074
  });
1016
1075
  }
1076
+ // ============================================
1077
+ // Restored Methods (from v2.x)
1078
+ // ============================================
1079
+ /**
1080
+ * Check if a key exists in MindCache.
1081
+ */
1082
+ has(key) {
1083
+ if (key === "$date" || key === "$time" || key === "$version") {
1084
+ return true;
1085
+ }
1086
+ return this.rootMap.has(key);
1087
+ }
1088
+ /**
1089
+ * Delete a key from MindCache.
1090
+ * @returns true if the key existed and was deleted
1091
+ */
1092
+ delete(key) {
1093
+ if (key === "$date" || key === "$time" || key === "$version") {
1094
+ return false;
1095
+ }
1096
+ if (!this.rootMap.has(key)) {
1097
+ return false;
1098
+ }
1099
+ this.rootMap.delete(key);
1100
+ this.notifyGlobalListeners();
1101
+ if (this.listeners[key]) {
1102
+ this.listeners[key].forEach((listener) => listener(void 0));
1103
+ }
1104
+ return true;
1105
+ }
1106
+ /** @deprecated Use get_value instead */
1107
+ get(key) {
1108
+ return this.get_value(key);
1109
+ }
1110
+ /** @deprecated Use set_value instead */
1111
+ set(key, value) {
1112
+ this.set_value(key, value);
1113
+ }
1114
+ /**
1115
+ * Update multiple values at once from an object.
1116
+ * @deprecated Use set_value for individual keys
1117
+ */
1118
+ update(data) {
1119
+ this.doc.transact(() => {
1120
+ for (const [key, value] of Object.entries(data)) {
1121
+ if (key !== "$date" && key !== "$time" && key !== "$version") {
1122
+ this.set_value(key, value);
1123
+ }
1124
+ }
1125
+ });
1126
+ this.notifyGlobalListeners();
1127
+ }
1128
+ /**
1129
+ * Get the number of keys in MindCache.
1130
+ */
1131
+ size() {
1132
+ return this.rootMap.size + 2;
1133
+ }
1134
+ /**
1135
+ * Get all keys in MindCache (including temporal keys).
1136
+ */
1137
+ keys() {
1138
+ const keys = Array.from(this.rootMap.keys());
1139
+ keys.push("$date", "$time");
1140
+ return keys;
1141
+ }
1142
+ /**
1143
+ * Get all values in MindCache (including temporal values).
1144
+ */
1145
+ values() {
1146
+ const result = [];
1147
+ for (const [key] of this.rootMap) {
1148
+ result.push(this.get_value(key));
1149
+ }
1150
+ result.push(this.get_value("$date"));
1151
+ result.push(this.get_value("$time"));
1152
+ return result;
1153
+ }
1154
+ /**
1155
+ * Get all key-value entries (including temporal entries).
1156
+ */
1157
+ entries() {
1158
+ const result = [];
1159
+ for (const [key] of this.rootMap) {
1160
+ result.push([key, this.get_value(key)]);
1161
+ }
1162
+ result.push(["$date", this.get_value("$date")]);
1163
+ result.push(["$time", this.get_value("$time")]);
1164
+ return result;
1165
+ }
1166
+ /**
1167
+ * Unsubscribe from key changes.
1168
+ * @deprecated Use the cleanup function returned by subscribe() instead
1169
+ */
1170
+ unsubscribe(key, listener) {
1171
+ if (this.listeners[key]) {
1172
+ this.listeners[key] = this.listeners[key].filter((l) => l !== listener);
1173
+ }
1174
+ }
1175
+ /**
1176
+ * Get the STM as a formatted string for LLM context.
1177
+ * @deprecated Use get_system_prompt() instead
1178
+ */
1179
+ getSTM() {
1180
+ return this.get_system_prompt();
1181
+ }
1182
+ /**
1183
+ * Get the STM as an object with values directly (no attributes).
1184
+ * Includes system keys ($date, $time).
1185
+ * @deprecated Use getAll() for full STM format
1186
+ */
1187
+ getSTMObject() {
1188
+ const result = {};
1189
+ for (const [key] of this.rootMap) {
1190
+ result[key] = this.get_value(key);
1191
+ }
1192
+ result["$date"] = this.get_value("$date");
1193
+ result["$time"] = this.get_value("$time");
1194
+ return result;
1195
+ }
1196
+ /**
1197
+ * Add a content tag to a key.
1198
+ * @returns true if the tag was added, false if key doesn't exist or tag already exists
1199
+ */
1200
+ addTag(key, tag) {
1201
+ if (key === "$date" || key === "$time" || key === "$version") {
1202
+ return false;
1203
+ }
1204
+ const entryMap = this.rootMap.get(key);
1205
+ if (!entryMap) {
1206
+ return false;
1207
+ }
1208
+ const attributes = entryMap.get("attributes");
1209
+ const contentTags = attributes?.contentTags || [];
1210
+ if (contentTags.includes(tag)) {
1211
+ return false;
1212
+ }
1213
+ this.doc.transact(() => {
1214
+ const newContentTags = [...contentTags, tag];
1215
+ entryMap.set("attributes", {
1216
+ ...attributes,
1217
+ contentTags: newContentTags,
1218
+ tags: newContentTags
1219
+ // Sync legacy tags array
1220
+ });
1221
+ });
1222
+ this.notifyGlobalListeners();
1223
+ return true;
1224
+ }
1225
+ /**
1226
+ * Remove a content tag from a key.
1227
+ * @returns true if the tag was removed
1228
+ */
1229
+ removeTag(key, tag) {
1230
+ if (key === "$date" || key === "$time" || key === "$version") {
1231
+ return false;
1232
+ }
1233
+ const entryMap = this.rootMap.get(key);
1234
+ if (!entryMap) {
1235
+ return false;
1236
+ }
1237
+ const attributes = entryMap.get("attributes");
1238
+ const contentTags = attributes?.contentTags || [];
1239
+ const tagIndex = contentTags.indexOf(tag);
1240
+ if (tagIndex === -1) {
1241
+ return false;
1242
+ }
1243
+ this.doc.transact(() => {
1244
+ const newContentTags = contentTags.filter((t) => t !== tag);
1245
+ entryMap.set("attributes", {
1246
+ ...attributes,
1247
+ contentTags: newContentTags,
1248
+ tags: newContentTags
1249
+ // Sync legacy tags array
1250
+ });
1251
+ });
1252
+ this.notifyGlobalListeners();
1253
+ return true;
1254
+ }
1255
+ /**
1256
+ * Get all content tags for a key.
1257
+ */
1258
+ getTags(key) {
1259
+ if (key === "$date" || key === "$time" || key === "$version") {
1260
+ return [];
1261
+ }
1262
+ const entryMap = this.rootMap.get(key);
1263
+ if (!entryMap) {
1264
+ return [];
1265
+ }
1266
+ const attributes = entryMap.get("attributes");
1267
+ return attributes?.contentTags || [];
1268
+ }
1269
+ /**
1270
+ * Get all unique content tags across all keys.
1271
+ */
1272
+ getAllTags() {
1273
+ const allTags = /* @__PURE__ */ new Set();
1274
+ for (const [, val] of this.rootMap) {
1275
+ const entryMap = val;
1276
+ const attributes = entryMap.get("attributes");
1277
+ if (attributes?.contentTags) {
1278
+ attributes.contentTags.forEach((tag) => allTags.add(tag));
1279
+ }
1280
+ }
1281
+ return Array.from(allTags);
1282
+ }
1283
+ /**
1284
+ * Check if a key has a specific content tag.
1285
+ */
1286
+ hasTag(key, tag) {
1287
+ if (key === "$date" || key === "$time" || key === "$version") {
1288
+ return false;
1289
+ }
1290
+ const entryMap = this.rootMap.get(key);
1291
+ if (!entryMap) {
1292
+ return false;
1293
+ }
1294
+ const attributes = entryMap.get("attributes");
1295
+ return attributes?.contentTags?.includes(tag) || false;
1296
+ }
1297
+ /**
1298
+ * Get all keys with a specific content tag as formatted string.
1299
+ */
1300
+ getTagged(tag) {
1301
+ const entries = [];
1302
+ const keys = this.getSortedKeys();
1303
+ keys.forEach((key) => {
1304
+ if (this.hasTag(key, tag)) {
1305
+ entries.push([key, this.get_value(key)]);
1306
+ }
1307
+ });
1308
+ return entries.map(([key, value]) => `${key}: ${value}`).join(", ");
1309
+ }
1310
+ /**
1311
+ * Get array of keys with a specific content tag.
1312
+ */
1313
+ getKeysByTag(tag) {
1314
+ const keys = this.getSortedKeys();
1315
+ return keys.filter((key) => this.hasTag(key, tag));
1316
+ }
1317
+ // ============================================
1318
+ // System Tag Methods (requires system access level)
1319
+ // ============================================
1320
+ /**
1321
+ * Add a system tag to a key (requires system access).
1322
+ * System tags: 'SystemPrompt', 'LLMRead', 'LLMWrite', 'readonly', 'protected', 'ApplyTemplate'
1323
+ */
1324
+ systemAddTag(key, tag) {
1325
+ if (!this.hasSystemAccess) {
1326
+ console.warn("MindCache: systemAddTag requires system access level");
1327
+ return false;
1328
+ }
1329
+ if (key === "$date" || key === "$time" || key === "$version") {
1330
+ return false;
1331
+ }
1332
+ const entryMap = this.rootMap.get(key);
1333
+ if (!entryMap) {
1334
+ return false;
1335
+ }
1336
+ const attributes = entryMap.get("attributes");
1337
+ const systemTags = attributes?.systemTags || [];
1338
+ if (systemTags.includes(tag)) {
1339
+ return false;
1340
+ }
1341
+ this.doc.transact(() => {
1342
+ const newSystemTags = [...systemTags, tag];
1343
+ const normalizedTags = this.normalizeSystemTags(newSystemTags);
1344
+ entryMap.set("attributes", {
1345
+ ...attributes,
1346
+ systemTags: normalizedTags
1347
+ });
1348
+ });
1349
+ this.notifyGlobalListeners();
1350
+ return true;
1351
+ }
1352
+ /**
1353
+ * Remove a system tag from a key (requires system access).
1354
+ */
1355
+ systemRemoveTag(key, tag) {
1356
+ if (!this.hasSystemAccess) {
1357
+ console.warn("MindCache: systemRemoveTag requires system access level");
1358
+ return false;
1359
+ }
1360
+ if (key === "$date" || key === "$time" || key === "$version") {
1361
+ return false;
1362
+ }
1363
+ const entryMap = this.rootMap.get(key);
1364
+ if (!entryMap) {
1365
+ return false;
1366
+ }
1367
+ const attributes = entryMap.get("attributes");
1368
+ const systemTags = attributes?.systemTags || [];
1369
+ const tagIndex = systemTags.indexOf(tag);
1370
+ if (tagIndex === -1) {
1371
+ return false;
1372
+ }
1373
+ this.doc.transact(() => {
1374
+ const newSystemTags = systemTags.filter((t) => t !== tag);
1375
+ entryMap.set("attributes", {
1376
+ ...attributes,
1377
+ systemTags: newSystemTags
1378
+ });
1379
+ });
1380
+ this.notifyGlobalListeners();
1381
+ return true;
1382
+ }
1383
+ /**
1384
+ * Get all system tags for a key (requires system access).
1385
+ */
1386
+ systemGetTags(key) {
1387
+ if (!this.hasSystemAccess) {
1388
+ console.warn("MindCache: systemGetTags requires system access level");
1389
+ return [];
1390
+ }
1391
+ if (key === "$date" || key === "$time" || key === "$version") {
1392
+ return [];
1393
+ }
1394
+ const entryMap = this.rootMap.get(key);
1395
+ if (!entryMap) {
1396
+ return [];
1397
+ }
1398
+ const attributes = entryMap.get("attributes");
1399
+ return attributes?.systemTags || [];
1400
+ }
1401
+ /**
1402
+ * Check if a key has a specific system tag (requires system access).
1403
+ */
1404
+ systemHasTag(key, tag) {
1405
+ if (!this.hasSystemAccess) {
1406
+ console.warn("MindCache: systemHasTag requires system access level");
1407
+ return false;
1408
+ }
1409
+ if (key === "$date" || key === "$time" || key === "$version") {
1410
+ return false;
1411
+ }
1412
+ const entryMap = this.rootMap.get(key);
1413
+ if (!entryMap) {
1414
+ return false;
1415
+ }
1416
+ const attributes = entryMap.get("attributes");
1417
+ return attributes?.systemTags?.includes(tag) || false;
1418
+ }
1419
+ /**
1420
+ * Set all system tags for a key at once (requires system access).
1421
+ */
1422
+ systemSetTags(key, tags) {
1423
+ if (!this.hasSystemAccess) {
1424
+ console.warn("MindCache: systemSetTags requires system access level");
1425
+ return false;
1426
+ }
1427
+ if (key === "$date" || key === "$time" || key === "$version") {
1428
+ return false;
1429
+ }
1430
+ const entryMap = this.rootMap.get(key);
1431
+ if (!entryMap) {
1432
+ return false;
1433
+ }
1434
+ this.doc.transact(() => {
1435
+ const attributes = entryMap.get("attributes");
1436
+ entryMap.set("attributes", {
1437
+ ...attributes,
1438
+ systemTags: [...tags]
1439
+ });
1440
+ });
1441
+ this.notifyGlobalListeners();
1442
+ return true;
1443
+ }
1444
+ /**
1445
+ * Get all keys with a specific system tag (requires system access).
1446
+ */
1447
+ systemGetKeysByTag(tag) {
1448
+ if (!this.hasSystemAccess) {
1449
+ console.warn("MindCache: systemGetKeysByTag requires system access level");
1450
+ return [];
1451
+ }
1452
+ const keys = this.getSortedKeys();
1453
+ return keys.filter((key) => this.systemHasTag(key, tag));
1454
+ }
1455
+ /**
1456
+ * Helper to get sorted keys (by zIndex).
1457
+ */
1458
+ getSortedKeys() {
1459
+ const entries = [];
1460
+ for (const [key, val] of this.rootMap) {
1461
+ const entryMap = val;
1462
+ const attributes = entryMap.get("attributes");
1463
+ entries.push({ key, zIndex: attributes?.zIndex ?? 0 });
1464
+ }
1465
+ return entries.sort((a, b) => a.zIndex - b.zIndex).map((e) => e.key);
1466
+ }
1467
+ /**
1468
+ * Serialize to JSON string.
1469
+ */
1470
+ toJSON() {
1471
+ return JSON.stringify(this.serialize());
1472
+ }
1473
+ /**
1474
+ * Deserialize from JSON string.
1475
+ */
1476
+ fromJSON(jsonString) {
1477
+ try {
1478
+ const data = JSON.parse(jsonString);
1479
+ this.deserialize(data);
1480
+ } catch (error) {
1481
+ console.error("MindCache: Failed to deserialize JSON:", error);
1482
+ }
1483
+ }
1484
+ /**
1485
+ * Export to Markdown format.
1486
+ */
1487
+ toMarkdown() {
1488
+ const now = /* @__PURE__ */ new Date();
1489
+ const lines = [];
1490
+ const appendixEntries = [];
1491
+ let appendixCounter = 0;
1492
+ lines.push("# MindCache STM Export");
1493
+ lines.push("");
1494
+ lines.push(`Export Date: ${now.toISOString().split("T")[0]}`);
1495
+ lines.push("");
1496
+ lines.push("---");
1497
+ lines.push("");
1498
+ lines.push("## STM Entries");
1499
+ lines.push("");
1500
+ const sortedKeys = this.getSortedKeys();
1501
+ sortedKeys.forEach((key) => {
1502
+ const entryMap = this.rootMap.get(key);
1503
+ if (!entryMap) {
1504
+ return;
1505
+ }
1506
+ const attributes = entryMap.get("attributes");
1507
+ const value = entryMap.get("value");
1508
+ if (attributes?.hardcoded) {
1509
+ return;
1510
+ }
1511
+ lines.push(`### ${key}`);
1512
+ const entryType = attributes?.type || "text";
1513
+ lines.push(`- **Type**: \`${entryType}\``);
1514
+ lines.push(`- **Readonly**: \`${attributes?.readonly ?? false}\``);
1515
+ lines.push(`- **Visible**: \`${attributes?.visible ?? true}\``);
1516
+ lines.push(`- **Template**: \`${attributes?.template ?? false}\``);
1517
+ lines.push(`- **Z-Index**: \`${attributes?.zIndex ?? 0}\``);
1518
+ if (attributes?.contentTags && attributes.contentTags.length > 0) {
1519
+ lines.push(`- **Tags**: \`${attributes.contentTags.join("`, `")}\``);
1520
+ }
1521
+ if (attributes?.contentType) {
1522
+ lines.push(`- **Content Type**: \`${attributes.contentType}\``);
1523
+ }
1524
+ if (entryType === "image" || entryType === "file") {
1525
+ const label = String.fromCharCode(65 + appendixCounter);
1526
+ appendixCounter++;
1527
+ lines.push(`- **Value**: [See Appendix ${label}]`);
1528
+ appendixEntries.push({
1529
+ key,
1530
+ type: entryType,
1531
+ contentType: attributes?.contentType || "application/octet-stream",
1532
+ base64: value,
1533
+ label
1534
+ });
1535
+ } else if (entryType === "json") {
1536
+ lines.push("- **Value**:");
1537
+ lines.push("```json");
1538
+ try {
1539
+ const jsonValue = typeof value === "string" ? value : JSON.stringify(value, null, 2);
1540
+ lines.push(jsonValue);
1541
+ } catch {
1542
+ lines.push(String(value));
1543
+ }
1544
+ lines.push("```");
1545
+ } else {
1546
+ lines.push(`- **Value**: ${value}`);
1547
+ }
1548
+ lines.push("");
1549
+ });
1550
+ if (appendixEntries.length > 0) {
1551
+ lines.push("---");
1552
+ lines.push("");
1553
+ lines.push("## Appendix: Binary Data");
1554
+ lines.push("");
1555
+ appendixEntries.forEach((entry) => {
1556
+ lines.push(`### Appendix ${entry.label}: ${entry.key}`);
1557
+ lines.push(`- **Type**: \`${entry.type}\``);
1558
+ lines.push(`- **Content Type**: \`${entry.contentType}\``);
1559
+ lines.push("- **Base64 Data**:");
1560
+ lines.push("```");
1561
+ lines.push(entry.base64);
1562
+ lines.push("```");
1563
+ lines.push("");
1564
+ });
1565
+ }
1566
+ return lines.join("\n");
1567
+ }
1568
+ /**
1569
+ * Import from Markdown format.
1570
+ */
1571
+ fromMarkdown(markdown) {
1572
+ const lines = markdown.split("\n");
1573
+ let currentKey = null;
1574
+ let currentAttributes = {};
1575
+ let currentValue = null;
1576
+ let inCodeBlock = false;
1577
+ let codeBlockContent = [];
1578
+ for (const line of lines) {
1579
+ if (line.startsWith("### ") && !line.startsWith("### Appendix")) {
1580
+ if (currentKey && currentValue !== null) {
1581
+ this.set_value(currentKey, currentValue, currentAttributes);
1582
+ }
1583
+ currentKey = line.substring(4).trim();
1584
+ currentAttributes = {};
1585
+ currentValue = null;
1586
+ continue;
1587
+ }
1588
+ if (line.startsWith("### Appendix ")) {
1589
+ const match = line.match(/### Appendix ([A-Z]): (.+)/);
1590
+ if (match) {
1591
+ currentKey = match[2];
1592
+ }
1593
+ continue;
1594
+ }
1595
+ if (line.startsWith("- **Type**:")) {
1596
+ const type = line.match(/`(.+)`/)?.[1];
1597
+ if (type) {
1598
+ currentAttributes.type = type;
1599
+ }
1600
+ continue;
1601
+ }
1602
+ if (line.startsWith("- **Readonly**:")) {
1603
+ currentAttributes.readonly = line.includes("`true`");
1604
+ continue;
1605
+ }
1606
+ if (line.startsWith("- **Visible**:")) {
1607
+ currentAttributes.visible = line.includes("`true`");
1608
+ continue;
1609
+ }
1610
+ if (line.startsWith("- **Template**:")) {
1611
+ currentAttributes.template = line.includes("`true`");
1612
+ continue;
1613
+ }
1614
+ if (line.startsWith("- **Z-Index**:")) {
1615
+ const zIndex = parseInt(line.match(/`(\d+)`/)?.[1] || "0", 10);
1616
+ currentAttributes.zIndex = zIndex;
1617
+ continue;
1618
+ }
1619
+ if (line.startsWith("- **Tags**:")) {
1620
+ const tags = line.match(/`([^`]+)`/g)?.map((t) => t.slice(1, -1)) || [];
1621
+ currentAttributes.contentTags = tags;
1622
+ currentAttributes.tags = tags;
1623
+ continue;
1624
+ }
1625
+ if (line.startsWith("- **Content Type**:")) {
1626
+ currentAttributes.contentType = line.match(/`(.+)`/)?.[1];
1627
+ continue;
1628
+ }
1629
+ if (line.startsWith("- **Value**:") && !line.includes("[See Appendix")) {
1630
+ currentValue = line.substring(12).trim();
1631
+ continue;
1632
+ }
1633
+ if (line === "```json" || line === "```") {
1634
+ if (inCodeBlock) {
1635
+ inCodeBlock = false;
1636
+ if (currentKey && codeBlockContent.length > 0) {
1637
+ currentValue = codeBlockContent.join("\n");
1638
+ }
1639
+ codeBlockContent = [];
1640
+ } else {
1641
+ inCodeBlock = true;
1642
+ }
1643
+ continue;
1644
+ }
1645
+ if (inCodeBlock) {
1646
+ codeBlockContent.push(line);
1647
+ }
1648
+ }
1649
+ if (currentKey && currentValue !== null) {
1650
+ this.set_value(currentKey, currentValue, currentAttributes);
1651
+ }
1652
+ }
1653
+ /**
1654
+ * Set base64 binary data.
1655
+ */
1656
+ set_base64(key, base64Data, contentType, type = "file", attributes) {
1657
+ if (!this.validateContentType(type, contentType)) {
1658
+ throw new Error(`Invalid content type ${contentType} for type ${type}`);
1659
+ }
1660
+ const fileAttributes = {
1661
+ type,
1662
+ contentType,
1663
+ ...attributes
1664
+ };
1665
+ this.set_value(key, base64Data, fileAttributes);
1666
+ }
1667
+ /**
1668
+ * Add an image from base64 data.
1669
+ */
1670
+ add_image(key, base64Data, contentType = "image/jpeg", attributes) {
1671
+ if (!contentType.startsWith("image/")) {
1672
+ throw new Error(`Invalid image content type: ${contentType}. Must start with 'image/'`);
1673
+ }
1674
+ this.set_base64(key, base64Data, contentType, "image", attributes);
1675
+ }
1676
+ /**
1677
+ * Get the data URL for an image or file key.
1678
+ */
1679
+ get_data_url(key) {
1680
+ const entryMap = this.rootMap.get(key);
1681
+ if (!entryMap) {
1682
+ return void 0;
1683
+ }
1684
+ const attributes = entryMap.get("attributes");
1685
+ if (attributes?.type !== "image" && attributes?.type !== "file") {
1686
+ return void 0;
1687
+ }
1688
+ if (!attributes?.contentType) {
1689
+ return void 0;
1690
+ }
1691
+ const value = entryMap.get("value");
1692
+ return this.createDataUrl(value, attributes.contentType);
1693
+ }
1694
+ /**
1695
+ * Get the base64 data for an image or file key.
1696
+ */
1697
+ get_base64(key) {
1698
+ const entryMap = this.rootMap.get(key);
1699
+ if (!entryMap) {
1700
+ return void 0;
1701
+ }
1702
+ const attributes = entryMap.get("attributes");
1703
+ if (attributes?.type !== "image" && attributes?.type !== "file") {
1704
+ return void 0;
1705
+ }
1706
+ return entryMap.get("value");
1707
+ }
1017
1708
  // File methods
1018
1709
  async set_file(key, file, attributes) {
1019
1710
  const base64 = await this.encodeFileToBase64(file);
@@ -1334,10 +2025,14 @@ var MindCache = class {
1334
2025
  const sanitizedKey = this.sanitizeKeyForTool(key);
1335
2026
  if (isWritable) {
1336
2027
  if (isDocument) {
1337
- lines.push(`${key}: ${displayValue}. Document tools: write_${sanitizedKey}, append_${sanitizedKey}, edit_${sanitizedKey}`);
2028
+ lines.push(
2029
+ `${key}: ${displayValue}. Document tools: write_${sanitizedKey}, append_${sanitizedKey}, edit_${sanitizedKey}`
2030
+ );
1338
2031
  } else {
1339
2032
  const oldValueHint = displayValue ? ` This tool DOES NOT append \u2014 start your response with the old value (${displayValue})` : "";
1340
- lines.push(`${key}: ${displayValue}. You can rewrite "${key}" by using the write_${sanitizedKey} tool.${oldValueHint}`);
2033
+ lines.push(
2034
+ `${key}: ${displayValue}. You can rewrite "${key}" by using the write_${sanitizedKey} tool.${oldValueHint}`
2035
+ );
1341
2036
  }
1342
2037
  } else {
1343
2038
  lines.push(`${key}: ${displayValue}`);