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