mindcache 3.4.0 → 3.4.2

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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as MindCache, a as MindCacheOptions } from './CloudAdapter-WvuWM4fD.mjs';
2
- export { A as AccessLevel, C as CloudAdapter, j as CloudAdapterEvents, h as CloudConfig, i as ConnectionState, D as DEFAULT_KEY_ATTRIBUTES, G as GlobalListener, H as HistoryEntry, e as HistoryOptions, K as KeyAttributes, b as KeyType, L as Listener, f as MindCacheCloudOptions, g as MindCacheIndexedDBOptions, c as STM, d as STMEntry, S as SystemTag } from './CloudAdapter-WvuWM4fD.mjs';
1
+ import { M as MindCache, a as MindCacheOptions } from './CloudAdapter-D2xxVv4E.mjs';
2
+ export { A as AccessLevel, C as CloudAdapter, k as CloudAdapterEvents, i as CloudConfig, j as ConnectionState, D as DEFAULT_KEY_ATTRIBUTES, G as GlobalListener, H as HistoryEntry, e as HistoryOptions, K as KeyAttributes, d as KeyEntry, b as KeyType, L as Listener, f as MindCacheCloudOptions, g as MindCacheIndexedDBOptions, c as STM, d as STMEntry, S as SystemTag, h as SystemTagHelpers } from './CloudAdapter-D2xxVv4E.mjs';
3
3
  import 'yjs';
4
4
 
5
5
  interface IndexedDBConfig {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as MindCache, a as MindCacheOptions } from './CloudAdapter-WvuWM4fD.js';
2
- export { A as AccessLevel, C as CloudAdapter, j as CloudAdapterEvents, h as CloudConfig, i as ConnectionState, D as DEFAULT_KEY_ATTRIBUTES, G as GlobalListener, H as HistoryEntry, e as HistoryOptions, K as KeyAttributes, b as KeyType, L as Listener, f as MindCacheCloudOptions, g as MindCacheIndexedDBOptions, c as STM, d as STMEntry, S as SystemTag } from './CloudAdapter-WvuWM4fD.js';
1
+ import { M as MindCache, a as MindCacheOptions } from './CloudAdapter-D2xxVv4E.js';
2
+ export { A as AccessLevel, C as CloudAdapter, k as CloudAdapterEvents, i as CloudConfig, j as ConnectionState, D as DEFAULT_KEY_ATTRIBUTES, G as GlobalListener, H as HistoryEntry, e as HistoryOptions, K as KeyAttributes, d as KeyEntry, b as KeyType, L as Listener, f as MindCacheCloudOptions, g as MindCacheIndexedDBOptions, c as STM, d as STMEntry, S as SystemTag, h as SystemTagHelpers } from './CloudAdapter-D2xxVv4E.js';
3
3
  import 'yjs';
4
4
 
5
5
  interface IndexedDBConfig {
package/dist/index.js CHANGED
@@ -74,7 +74,7 @@ var init_IndexedDBAdapter = __esm({
74
74
  await this.initDB();
75
75
  await this.load();
76
76
  const listener = () => {
77
- if (this.mindcache && !this.mindcache.isRemoteUpdate()) {
77
+ if (this.mindcache) {
78
78
  this.scheduleSave();
79
79
  }
80
80
  };
@@ -454,15 +454,21 @@ var init_CloudAdapter = __esm({
454
454
  var DEFAULT_KEY_ATTRIBUTES = {
455
455
  type: "text",
456
456
  contentTags: [],
457
- systemTags: ["SystemPrompt", "LLMWrite"],
458
- // visible in system prompt and writable by LLM by default
459
- zIndex: 0,
460
- // Legacy - derived from systemTags
461
- readonly: false,
462
- visible: true,
463
- hardcoded: false,
464
- template: false,
465
- tags: []
457
+ systemTags: [],
458
+ // Keys are private by default - explicitly add SystemPrompt/LLMRead/LLMWrite to enable LLM access
459
+ zIndex: 0
460
+ };
461
+ var SystemTagHelpers = {
462
+ /** Check if key is writable by LLM */
463
+ isLLMWritable: (attrs) => attrs.systemTags.includes("LLMWrite"),
464
+ /** Check if key is readable by LLM (in context or via tools) */
465
+ isLLMReadable: (attrs) => attrs.systemTags.includes("SystemPrompt") || attrs.systemTags.includes("LLMRead"),
466
+ /** Check if key is included in system prompt */
467
+ isInSystemPrompt: (attrs) => attrs.systemTags.includes("SystemPrompt"),
468
+ /** Check if key is protected from deletion */
469
+ isProtected: (attrs) => attrs.systemTags.includes("protected"),
470
+ /** Check if key uses template injection */
471
+ hasTemplateInjection: (attrs) => attrs.systemTags.includes("ApplyTemplate")
466
472
  };
467
473
 
468
474
  // src/core/MindCache.ts
@@ -478,38 +484,16 @@ var MindCache = class {
478
484
  version = "3.3.2";
479
485
  // Internal flag to prevent sync loops when receiving remote updates
480
486
  // (Less critical with Yjs but kept for API compat)
481
- _isRemoteUpdate = false;
482
487
  normalizeSystemTags(tags) {
483
488
  const normalized = [];
484
- let hasSystemPrompt = false;
485
- let hasLLMRead = false;
486
- let hasLLMWrite = false;
487
- let hasReadonly = false;
489
+ const seen = /* @__PURE__ */ new Set();
488
490
  for (const tag of tags) {
489
- if (tag === "SystemPrompt" || tag === "prompt") {
490
- hasSystemPrompt = true;
491
- } else if (tag === "LLMRead") {
492
- hasLLMRead = true;
493
- } else if (tag === "LLMWrite") {
494
- hasLLMWrite = true;
495
- } else if (tag === "readonly") {
496
- hasReadonly = true;
497
- } else if (tag === "protected") {
498
- normalized.push(tag);
499
- } else if (tag === "ApplyTemplate" || tag === "template") {
500
- normalized.push("ApplyTemplate");
501
- }
502
- }
503
- if (hasSystemPrompt) {
504
- normalized.push("SystemPrompt");
505
- }
506
- if (hasLLMRead) {
507
- normalized.push("LLMRead");
508
- }
509
- if (hasReadonly) {
510
- normalized.push("readonly");
511
- } else if (hasLLMWrite) {
512
- normalized.push("LLMWrite");
491
+ if (["SystemPrompt", "LLMRead", "LLMWrite", "protected", "ApplyTemplate"].includes(tag)) {
492
+ if (!seen.has(tag)) {
493
+ seen.add(tag);
494
+ normalized.push(tag);
495
+ }
496
+ }
513
497
  }
514
498
  return normalized;
515
499
  }
@@ -900,10 +884,6 @@ var MindCache = class {
900
884
  this._idbProvider = null;
901
885
  }
902
886
  }
903
- // Legacy bridge
904
- isRemoteUpdate() {
905
- return false;
906
- }
907
887
  // Serialize state
908
888
  serialize() {
909
889
  const json = {};
@@ -940,16 +920,10 @@ var MindCache = class {
940
920
  const attrs = entry.attributes || {};
941
921
  const normalizedAttrs = {
942
922
  type: attrs.type || "text",
923
+ contentType: attrs.contentType,
943
924
  contentTags: attrs.contentTags || [],
944
- systemTags: attrs.systemTags || this.normalizeSystemTags(attrs.visible !== false ? ["prompt"] : []),
945
- zIndex: attrs.zIndex ?? 0,
946
- // Legacy fields
947
- readonly: attrs.readonly ?? false,
948
- visible: attrs.visible ?? true,
949
- hardcoded: attrs.hardcoded ?? false,
950
- template: attrs.template ?? false,
951
- tags: attrs.tags || [],
952
- contentType: attrs.contentType
925
+ systemTags: this.normalizeSystemTags(attrs.systemTags || []),
926
+ zIndex: attrs.zIndex ?? 0
953
927
  };
954
928
  entryMap.set("attributes", normalizedAttrs);
955
929
  }
@@ -1054,7 +1028,7 @@ var MindCache = class {
1054
1028
  if (_processingStack && _processingStack.has(key)) {
1055
1029
  return `{{${key}}}`;
1056
1030
  }
1057
- if (attributes?.systemTags?.includes("ApplyTemplate") || attributes?.systemTags?.includes("template") || attributes?.template) {
1031
+ if (attributes?.systemTags?.includes("ApplyTemplate")) {
1058
1032
  if (typeof value === "string") {
1059
1033
  const stack = _processingStack || /* @__PURE__ */ new Set();
1060
1034
  stack.add(key);
@@ -1068,13 +1042,8 @@ var MindCache = class {
1068
1042
  return {
1069
1043
  type: "text",
1070
1044
  contentTags: [],
1071
- systemTags: ["prompt", "readonly", "protected"],
1072
- zIndex: 999999,
1073
- readonly: true,
1074
- visible: true,
1075
- hardcoded: true,
1076
- template: false,
1077
- tags: []
1045
+ systemTags: ["SystemPrompt", "protected"],
1046
+ zIndex: 999999
1078
1047
  };
1079
1048
  }
1080
1049
  const entryMap = this.rootMap.get(key);
@@ -1099,6 +1068,14 @@ var MindCache = class {
1099
1068
  mergedAttrs.systemTags = this.normalizeSystemTags(mergedAttrs.systemTags);
1100
1069
  }
1101
1070
  entryMap.set("attributes", mergedAttrs);
1071
+ const currentValue = entryMap.get("value");
1072
+ if (mergedAttrs.type === "document" && !(currentValue instanceof Y__namespace.Text)) {
1073
+ const strValue = typeof currentValue === "string" ? currentValue : String(currentValue ?? "");
1074
+ entryMap.set("value", new Y__namespace.Text(strValue));
1075
+ this.getUndoManager(key);
1076
+ } else if (mergedAttrs.type !== "document" && currentValue instanceof Y__namespace.Text) {
1077
+ entryMap.set("value", currentValue.toString());
1078
+ }
1102
1079
  });
1103
1080
  }
1104
1081
  set_value(key, value, attributes) {
@@ -1109,10 +1086,15 @@ var MindCache = class {
1109
1086
  if (existingEntry) {
1110
1087
  const existingAttrs = existingEntry.get("attributes");
1111
1088
  if (existingAttrs?.type === "document") {
1112
- if (typeof value === "string") {
1113
- this._replaceDocumentText(key, value);
1089
+ if (!attributes?.type || attributes.type === "document") {
1090
+ if (typeof value === "string") {
1091
+ this._replaceDocumentText(key, value);
1092
+ }
1093
+ if (attributes) {
1094
+ this.set_attributes(key, attributes);
1095
+ }
1096
+ return;
1114
1097
  }
1115
- return;
1116
1098
  }
1117
1099
  }
1118
1100
  if (!existingEntry && attributes?.type === "document") {
@@ -1128,26 +1110,55 @@ var MindCache = class {
1128
1110
  this.getUndoManager(key);
1129
1111
  this.doc.transact(() => {
1130
1112
  const oldAttributes = isNewEntry ? {
1131
- ...DEFAULT_KEY_ATTRIBUTES,
1132
- contentTags: [],
1133
- systemTags: ["SystemPrompt", "LLMWrite"],
1134
- tags: [],
1135
- zIndex: 0
1113
+ ...DEFAULT_KEY_ATTRIBUTES
1136
1114
  } : entryMap.get("attributes");
1137
1115
  const finalAttributes = attributes ? { ...oldAttributes, ...attributes } : oldAttributes;
1138
1116
  let normalizedAttributes = { ...finalAttributes };
1139
1117
  if (finalAttributes.systemTags) {
1140
1118
  normalizedAttributes.systemTags = this.normalizeSystemTags(finalAttributes.systemTags);
1141
1119
  }
1142
- if (finalAttributes.template) {
1143
- if (!normalizedAttributes.systemTags.includes("template")) {
1144
- normalizedAttributes.systemTags.push("template");
1145
- }
1120
+ let valueToSet = value;
1121
+ if (normalizedAttributes.type === "document" && !(valueToSet instanceof Y__namespace.Text)) {
1122
+ valueToSet = new Y__namespace.Text(typeof value === "string" ? value : String(value ?? ""));
1123
+ } else if (normalizedAttributes.type !== "document" && valueToSet instanceof Y__namespace.Text) {
1124
+ valueToSet = valueToSet.toString();
1146
1125
  }
1147
- entryMap.set("value", value);
1126
+ entryMap.set("value", valueToSet);
1148
1127
  entryMap.set("attributes", normalizedAttributes);
1149
1128
  });
1150
1129
  }
1130
+ /**
1131
+ * LLM-safe method to write a value to a key.
1132
+ * This method:
1133
+ * - Only updates the value, never modifies attributes/systemTags
1134
+ * - Checks LLMWrite permission before writing
1135
+ * - Returns false if key doesn't exist or lacks LLMWrite permission
1136
+ *
1137
+ * Used by create_vercel_ai_tools() to prevent LLMs from escalating privileges.
1138
+ */
1139
+ llm_set_key(key, value) {
1140
+ if (key === "$date" || key === "$time" || key === "$version") {
1141
+ return false;
1142
+ }
1143
+ const entryMap = this.rootMap.get(key);
1144
+ if (!entryMap) {
1145
+ return false;
1146
+ }
1147
+ const attributes = entryMap.get("attributes");
1148
+ if (!attributes?.systemTags?.includes("LLMWrite")) {
1149
+ return false;
1150
+ }
1151
+ if (attributes.type === "document") {
1152
+ if (typeof value === "string") {
1153
+ this._replaceDocumentText(key, value);
1154
+ }
1155
+ return true;
1156
+ }
1157
+ this.doc.transact(() => {
1158
+ entryMap.set("value", value);
1159
+ });
1160
+ return true;
1161
+ }
1151
1162
  delete_key(key) {
1152
1163
  if (key === "$date" || key === "$time") {
1153
1164
  return;
@@ -1592,15 +1603,13 @@ var MindCache = class {
1592
1603
  }
1593
1604
  const attributes = entryMap.get("attributes");
1594
1605
  const value = entryMap.get("value");
1595
- if (attributes?.hardcoded) {
1606
+ if (attributes?.systemTags?.includes("protected")) {
1596
1607
  return;
1597
1608
  }
1598
1609
  lines.push(`### ${key}`);
1599
1610
  const entryType = attributes?.type || "text";
1600
1611
  lines.push(`- **Type**: \`${entryType}\``);
1601
- lines.push(`- **Readonly**: \`${attributes?.readonly ?? false}\``);
1602
- lines.push(`- **Visible**: \`${attributes?.visible ?? true}\``);
1603
- lines.push(`- **Template**: \`${attributes?.template ?? false}\``);
1612
+ lines.push(`- **System Tags**: \`${attributes?.systemTags?.join(", ") || "none"}\``);
1604
1613
  lines.push(`- **Z-Index**: \`${attributes?.zIndex ?? 0}\``);
1605
1614
  if (attributes?.contentTags && attributes.contentTags.length > 0) {
1606
1615
  lines.push(`- **Tags**: \`${attributes.contentTags.join("`, `")}\``);
@@ -1686,16 +1695,11 @@ var MindCache = class {
1686
1695
  }
1687
1696
  continue;
1688
1697
  }
1689
- if (line.startsWith("- **Readonly**:")) {
1690
- currentAttributes.readonly = line.includes("`true`");
1691
- continue;
1692
- }
1693
- if (line.startsWith("- **Visible**:")) {
1694
- currentAttributes.visible = line.includes("`true`");
1695
- continue;
1696
- }
1697
- if (line.startsWith("- **Template**:")) {
1698
- currentAttributes.template = line.includes("`true`");
1698
+ if (line.startsWith("- **System Tags**:")) {
1699
+ const tagsStr = line.match(/`([^`]+)`/)?.[1] || "";
1700
+ if (tagsStr !== "none") {
1701
+ currentAttributes.systemTags = tagsStr.split(", ").filter((t) => t);
1702
+ }
1699
1703
  continue;
1700
1704
  }
1701
1705
  if (line.startsWith("- **Z-Index**:")) {
@@ -1706,7 +1710,6 @@ var MindCache = class {
1706
1710
  if (line.startsWith("- **Tags**:")) {
1707
1711
  const tags = line.match(/`([^`]+)`/g)?.map((t) => t.slice(1, -1)) || [];
1708
1712
  currentAttributes.contentTags = tags;
1709
- currentAttributes.tags = tags;
1710
1713
  continue;
1711
1714
  }
1712
1715
  if (line.startsWith("- **Content Type**:")) {
@@ -1962,8 +1965,12 @@ var MindCache = class {
1962
1965
  /**
1963
1966
  * Generate Vercel AI SDK compatible tools for writable keys.
1964
1967
  * For document type keys, generates additional tools: append_, insert_, edit_
1968
+ *
1969
+ * Security: All tools use llm_set_key internally which:
1970
+ * - Only modifies VALUES, never attributes/systemTags
1971
+ * - Prevents LLMs from escalating privileges
1965
1972
  */
1966
- get_aisdk_tools() {
1973
+ create_vercel_ai_tools() {
1967
1974
  const tools = {};
1968
1975
  for (const [key, val] of this.rootMap) {
1969
1976
  if (key.startsWith("$")) {
@@ -1971,7 +1978,7 @@ var MindCache = class {
1971
1978
  }
1972
1979
  const entryMap = val;
1973
1980
  const attributes = entryMap.get("attributes");
1974
- const isWritable = !attributes?.readonly && (attributes?.systemTags?.includes("LLMWrite") || !attributes?.systemTags);
1981
+ const isWritable = attributes?.systemTags?.includes("LLMWrite");
1975
1982
  if (!isWritable) {
1976
1983
  continue;
1977
1984
  }
@@ -1987,15 +1994,18 @@ var MindCache = class {
1987
1994
  required: ["value"]
1988
1995
  },
1989
1996
  execute: async ({ value }) => {
1990
- if (isDocument) {
1991
- this._replaceDocumentText(key, value);
1992
- } else {
1993
- this.set_value(key, value);
1997
+ const success = this.llm_set_key(key, value);
1998
+ if (success) {
1999
+ return {
2000
+ result: `Successfully wrote "${value}" to ${key}`,
2001
+ key,
2002
+ value
2003
+ };
1994
2004
  }
1995
2005
  return {
1996
- result: `Successfully wrote "${value}" to ${key}`,
2006
+ result: `Failed to write to ${key} - permission denied or key not found`,
1997
2007
  key,
1998
- value
2008
+ error: true
1999
2009
  };
2000
2010
  }
2001
2011
  };
@@ -2010,6 +2020,9 @@ var MindCache = class {
2010
2020
  required: ["text"]
2011
2021
  },
2012
2022
  execute: async ({ text }) => {
2023
+ if (!attributes?.systemTags?.includes("LLMWrite")) {
2024
+ return { result: `Permission denied for ${key}`, key, error: true };
2025
+ }
2013
2026
  const yText = this.get_document(key);
2014
2027
  if (yText) {
2015
2028
  yText.insert(yText.length, text);
@@ -2033,6 +2046,9 @@ var MindCache = class {
2033
2046
  required: ["index", "text"]
2034
2047
  },
2035
2048
  execute: async ({ index, text }) => {
2049
+ if (!attributes?.systemTags?.includes("LLMWrite")) {
2050
+ return { result: `Permission denied for ${key}`, key, error: true };
2051
+ }
2036
2052
  this.insert_text(key, index, text);
2037
2053
  return {
2038
2054
  result: `Successfully inserted text at position ${index} in ${key}`,
@@ -2053,6 +2069,9 @@ var MindCache = class {
2053
2069
  required: ["find", "replace"]
2054
2070
  },
2055
2071
  execute: async ({ find, replace }) => {
2072
+ if (!attributes?.systemTags?.includes("LLMWrite")) {
2073
+ return { result: `Permission denied for ${key}`, key, error: true };
2074
+ }
2056
2075
  const yText = this.get_document(key);
2057
2076
  if (yText) {
2058
2077
  const text = yText.toString();
@@ -2077,6 +2096,12 @@ var MindCache = class {
2077
2096
  }
2078
2097
  return tools;
2079
2098
  }
2099
+ /**
2100
+ * @deprecated Use create_vercel_ai_tools() instead
2101
+ */
2102
+ get_aisdk_tools() {
2103
+ return this.create_vercel_ai_tools();
2104
+ }
2080
2105
  /**
2081
2106
  * Generate a system prompt containing all visible STM keys and their values.
2082
2107
  * Indicates which tools can be used to modify writable keys.
@@ -2089,13 +2114,13 @@ var MindCache = class {
2089
2114
  }
2090
2115
  const entryMap = val;
2091
2116
  const attributes = entryMap.get("attributes");
2092
- const isVisible = attributes?.visible !== false && (attributes?.systemTags?.includes("prompt") || attributes?.systemTags?.includes("SystemPrompt") || !attributes?.systemTags);
2117
+ const isVisible = attributes?.systemTags?.includes("SystemPrompt") || attributes?.systemTags?.includes("LLMRead");
2093
2118
  if (!isVisible) {
2094
2119
  continue;
2095
2120
  }
2096
2121
  const value = this.get_value(key);
2097
2122
  const displayValue = typeof value === "object" ? JSON.stringify(value) : value;
2098
- const isWritable = !attributes?.readonly && (attributes?.systemTags?.includes("LLMWrite") || !attributes?.systemTags);
2123
+ const isWritable = attributes?.systemTags?.includes("LLMWrite");
2099
2124
  const isDocument = attributes?.type === "document";
2100
2125
  const sanitizedKey = this.sanitizeKeyForTool(key);
2101
2126
  if (isWritable) {
@@ -2136,7 +2161,7 @@ var MindCache = class {
2136
2161
  return null;
2137
2162
  }
2138
2163
  const attributes = entryMap.get("attributes");
2139
- const isWritable = !attributes?.readonly && (attributes?.systemTags?.includes("LLMWrite") || !attributes?.systemTags);
2164
+ const isWritable = attributes?.systemTags?.includes("LLMWrite");
2140
2165
  if (!isWritable) {
2141
2166
  return null;
2142
2167
  }
@@ -2253,6 +2278,7 @@ var mindcache = new MindCache();
2253
2278
 
2254
2279
  exports.DEFAULT_KEY_ATTRIBUTES = DEFAULT_KEY_ATTRIBUTES;
2255
2280
  exports.MindCache = MindCache;
2281
+ exports.SystemTagHelpers = SystemTagHelpers;
2256
2282
  exports.mindcache = mindcache;
2257
2283
  exports.useMindCache = useMindCache;
2258
2284
  //# sourceMappingURL=index.js.map