mindcache 3.4.1 → 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-C0UyG6OY.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-C0UyG6OY.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-C0UyG6OY.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-C0UyG6OY.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
@@ -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
@@ -480,35 +486,14 @@ var MindCache = class {
480
486
  // (Less critical with Yjs but kept for API compat)
481
487
  normalizeSystemTags(tags) {
482
488
  const normalized = [];
483
- let hasSystemPrompt = false;
484
- let hasLLMRead = false;
485
- let hasLLMWrite = false;
486
- let hasReadonly = false;
489
+ const seen = /* @__PURE__ */ new Set();
487
490
  for (const tag of tags) {
488
- if (tag === "SystemPrompt" || tag === "prompt") {
489
- hasSystemPrompt = true;
490
- } else if (tag === "LLMRead") {
491
- hasLLMRead = true;
492
- } else if (tag === "LLMWrite") {
493
- hasLLMWrite = true;
494
- } else if (tag === "readonly") {
495
- hasReadonly = true;
496
- } else if (tag === "protected") {
497
- normalized.push(tag);
498
- } else if (tag === "ApplyTemplate" || tag === "template") {
499
- normalized.push("ApplyTemplate");
500
- }
501
- }
502
- if (hasSystemPrompt) {
503
- normalized.push("SystemPrompt");
504
- }
505
- if (hasLLMRead) {
506
- normalized.push("LLMRead");
507
- }
508
- if (hasReadonly) {
509
- normalized.push("readonly");
510
- } else if (hasLLMWrite) {
511
- 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
+ }
512
497
  }
513
498
  return normalized;
514
499
  }
@@ -935,16 +920,10 @@ var MindCache = class {
935
920
  const attrs = entry.attributes || {};
936
921
  const normalizedAttrs = {
937
922
  type: attrs.type || "text",
923
+ contentType: attrs.contentType,
938
924
  contentTags: attrs.contentTags || [],
939
- systemTags: attrs.systemTags || this.normalizeSystemTags(attrs.visible !== false ? ["prompt"] : []),
940
- zIndex: attrs.zIndex ?? 0,
941
- // Legacy fields
942
- readonly: attrs.readonly ?? false,
943
- visible: attrs.visible ?? true,
944
- hardcoded: attrs.hardcoded ?? false,
945
- template: attrs.template ?? false,
946
- tags: attrs.tags || [],
947
- contentType: attrs.contentType
925
+ systemTags: this.normalizeSystemTags(attrs.systemTags || []),
926
+ zIndex: attrs.zIndex ?? 0
948
927
  };
949
928
  entryMap.set("attributes", normalizedAttrs);
950
929
  }
@@ -1049,7 +1028,7 @@ var MindCache = class {
1049
1028
  if (_processingStack && _processingStack.has(key)) {
1050
1029
  return `{{${key}}}`;
1051
1030
  }
1052
- if (attributes?.systemTags?.includes("ApplyTemplate") || attributes?.systemTags?.includes("template") || attributes?.template) {
1031
+ if (attributes?.systemTags?.includes("ApplyTemplate")) {
1053
1032
  if (typeof value === "string") {
1054
1033
  const stack = _processingStack || /* @__PURE__ */ new Set();
1055
1034
  stack.add(key);
@@ -1063,13 +1042,8 @@ var MindCache = class {
1063
1042
  return {
1064
1043
  type: "text",
1065
1044
  contentTags: [],
1066
- systemTags: ["prompt", "readonly", "protected"],
1067
- zIndex: 999999,
1068
- readonly: true,
1069
- visible: true,
1070
- hardcoded: true,
1071
- template: false,
1072
- tags: []
1045
+ systemTags: ["SystemPrompt", "protected"],
1046
+ zIndex: 999999
1073
1047
  };
1074
1048
  }
1075
1049
  const entryMap = this.rootMap.get(key);
@@ -1136,22 +1110,13 @@ var MindCache = class {
1136
1110
  this.getUndoManager(key);
1137
1111
  this.doc.transact(() => {
1138
1112
  const oldAttributes = isNewEntry ? {
1139
- ...DEFAULT_KEY_ATTRIBUTES,
1140
- contentTags: [],
1141
- systemTags: ["SystemPrompt", "LLMWrite"],
1142
- tags: [],
1143
- zIndex: 0
1113
+ ...DEFAULT_KEY_ATTRIBUTES
1144
1114
  } : entryMap.get("attributes");
1145
1115
  const finalAttributes = attributes ? { ...oldAttributes, ...attributes } : oldAttributes;
1146
1116
  let normalizedAttributes = { ...finalAttributes };
1147
1117
  if (finalAttributes.systemTags) {
1148
1118
  normalizedAttributes.systemTags = this.normalizeSystemTags(finalAttributes.systemTags);
1149
1119
  }
1150
- if (finalAttributes.template) {
1151
- if (!normalizedAttributes.systemTags.includes("template")) {
1152
- normalizedAttributes.systemTags.push("template");
1153
- }
1154
- }
1155
1120
  let valueToSet = value;
1156
1121
  if (normalizedAttributes.type === "document" && !(valueToSet instanceof Y__namespace.Text)) {
1157
1122
  valueToSet = new Y__namespace.Text(typeof value === "string" ? value : String(value ?? ""));
@@ -1162,6 +1127,38 @@ var MindCache = class {
1162
1127
  entryMap.set("attributes", normalizedAttributes);
1163
1128
  });
1164
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
+ }
1165
1162
  delete_key(key) {
1166
1163
  if (key === "$date" || key === "$time") {
1167
1164
  return;
@@ -1606,15 +1603,13 @@ var MindCache = class {
1606
1603
  }
1607
1604
  const attributes = entryMap.get("attributes");
1608
1605
  const value = entryMap.get("value");
1609
- if (attributes?.hardcoded) {
1606
+ if (attributes?.systemTags?.includes("protected")) {
1610
1607
  return;
1611
1608
  }
1612
1609
  lines.push(`### ${key}`);
1613
1610
  const entryType = attributes?.type || "text";
1614
1611
  lines.push(`- **Type**: \`${entryType}\``);
1615
- lines.push(`- **Readonly**: \`${attributes?.readonly ?? false}\``);
1616
- lines.push(`- **Visible**: \`${attributes?.visible ?? true}\``);
1617
- lines.push(`- **Template**: \`${attributes?.template ?? false}\``);
1612
+ lines.push(`- **System Tags**: \`${attributes?.systemTags?.join(", ") || "none"}\``);
1618
1613
  lines.push(`- **Z-Index**: \`${attributes?.zIndex ?? 0}\``);
1619
1614
  if (attributes?.contentTags && attributes.contentTags.length > 0) {
1620
1615
  lines.push(`- **Tags**: \`${attributes.contentTags.join("`, `")}\``);
@@ -1700,16 +1695,11 @@ var MindCache = class {
1700
1695
  }
1701
1696
  continue;
1702
1697
  }
1703
- if (line.startsWith("- **Readonly**:")) {
1704
- currentAttributes.readonly = line.includes("`true`");
1705
- continue;
1706
- }
1707
- if (line.startsWith("- **Visible**:")) {
1708
- currentAttributes.visible = line.includes("`true`");
1709
- continue;
1710
- }
1711
- if (line.startsWith("- **Template**:")) {
1712
- 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
+ }
1713
1703
  continue;
1714
1704
  }
1715
1705
  if (line.startsWith("- **Z-Index**:")) {
@@ -1720,7 +1710,6 @@ var MindCache = class {
1720
1710
  if (line.startsWith("- **Tags**:")) {
1721
1711
  const tags = line.match(/`([^`]+)`/g)?.map((t) => t.slice(1, -1)) || [];
1722
1712
  currentAttributes.contentTags = tags;
1723
- currentAttributes.tags = tags;
1724
1713
  continue;
1725
1714
  }
1726
1715
  if (line.startsWith("- **Content Type**:")) {
@@ -1976,8 +1965,12 @@ var MindCache = class {
1976
1965
  /**
1977
1966
  * Generate Vercel AI SDK compatible tools for writable keys.
1978
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
1979
1972
  */
1980
- get_aisdk_tools() {
1973
+ create_vercel_ai_tools() {
1981
1974
  const tools = {};
1982
1975
  for (const [key, val] of this.rootMap) {
1983
1976
  if (key.startsWith("$")) {
@@ -1985,7 +1978,7 @@ var MindCache = class {
1985
1978
  }
1986
1979
  const entryMap = val;
1987
1980
  const attributes = entryMap.get("attributes");
1988
- const isWritable = !attributes?.readonly && (attributes?.systemTags?.includes("LLMWrite") || !attributes?.systemTags);
1981
+ const isWritable = attributes?.systemTags?.includes("LLMWrite");
1989
1982
  if (!isWritable) {
1990
1983
  continue;
1991
1984
  }
@@ -2001,15 +1994,18 @@ var MindCache = class {
2001
1994
  required: ["value"]
2002
1995
  },
2003
1996
  execute: async ({ value }) => {
2004
- if (isDocument) {
2005
- this._replaceDocumentText(key, value);
2006
- } else {
2007
- 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
+ };
2008
2004
  }
2009
2005
  return {
2010
- result: `Successfully wrote "${value}" to ${key}`,
2006
+ result: `Failed to write to ${key} - permission denied or key not found`,
2011
2007
  key,
2012
- value
2008
+ error: true
2013
2009
  };
2014
2010
  }
2015
2011
  };
@@ -2024,6 +2020,9 @@ var MindCache = class {
2024
2020
  required: ["text"]
2025
2021
  },
2026
2022
  execute: async ({ text }) => {
2023
+ if (!attributes?.systemTags?.includes("LLMWrite")) {
2024
+ return { result: `Permission denied for ${key}`, key, error: true };
2025
+ }
2027
2026
  const yText = this.get_document(key);
2028
2027
  if (yText) {
2029
2028
  yText.insert(yText.length, text);
@@ -2047,6 +2046,9 @@ var MindCache = class {
2047
2046
  required: ["index", "text"]
2048
2047
  },
2049
2048
  execute: async ({ index, text }) => {
2049
+ if (!attributes?.systemTags?.includes("LLMWrite")) {
2050
+ return { result: `Permission denied for ${key}`, key, error: true };
2051
+ }
2050
2052
  this.insert_text(key, index, text);
2051
2053
  return {
2052
2054
  result: `Successfully inserted text at position ${index} in ${key}`,
@@ -2067,6 +2069,9 @@ var MindCache = class {
2067
2069
  required: ["find", "replace"]
2068
2070
  },
2069
2071
  execute: async ({ find, replace }) => {
2072
+ if (!attributes?.systemTags?.includes("LLMWrite")) {
2073
+ return { result: `Permission denied for ${key}`, key, error: true };
2074
+ }
2070
2075
  const yText = this.get_document(key);
2071
2076
  if (yText) {
2072
2077
  const text = yText.toString();
@@ -2091,6 +2096,12 @@ var MindCache = class {
2091
2096
  }
2092
2097
  return tools;
2093
2098
  }
2099
+ /**
2100
+ * @deprecated Use create_vercel_ai_tools() instead
2101
+ */
2102
+ get_aisdk_tools() {
2103
+ return this.create_vercel_ai_tools();
2104
+ }
2094
2105
  /**
2095
2106
  * Generate a system prompt containing all visible STM keys and their values.
2096
2107
  * Indicates which tools can be used to modify writable keys.
@@ -2103,13 +2114,13 @@ var MindCache = class {
2103
2114
  }
2104
2115
  const entryMap = val;
2105
2116
  const attributes = entryMap.get("attributes");
2106
- 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");
2107
2118
  if (!isVisible) {
2108
2119
  continue;
2109
2120
  }
2110
2121
  const value = this.get_value(key);
2111
2122
  const displayValue = typeof value === "object" ? JSON.stringify(value) : value;
2112
- const isWritable = !attributes?.readonly && (attributes?.systemTags?.includes("LLMWrite") || !attributes?.systemTags);
2123
+ const isWritable = attributes?.systemTags?.includes("LLMWrite");
2113
2124
  const isDocument = attributes?.type === "document";
2114
2125
  const sanitizedKey = this.sanitizeKeyForTool(key);
2115
2126
  if (isWritable) {
@@ -2150,7 +2161,7 @@ var MindCache = class {
2150
2161
  return null;
2151
2162
  }
2152
2163
  const attributes = entryMap.get("attributes");
2153
- const isWritable = !attributes?.readonly && (attributes?.systemTags?.includes("LLMWrite") || !attributes?.systemTags);
2164
+ const isWritable = attributes?.systemTags?.includes("LLMWrite");
2154
2165
  if (!isWritable) {
2155
2166
  return null;
2156
2167
  }
@@ -2267,6 +2278,7 @@ var mindcache = new MindCache();
2267
2278
 
2268
2279
  exports.DEFAULT_KEY_ATTRIBUTES = DEFAULT_KEY_ATTRIBUTES;
2269
2280
  exports.MindCache = MindCache;
2281
+ exports.SystemTagHelpers = SystemTagHelpers;
2270
2282
  exports.mindcache = mindcache;
2271
2283
  exports.useMindCache = useMindCache;
2272
2284
  //# sourceMappingURL=index.js.map