dsh-feiyu-memgate 0.3.2 → 0.5.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.
Files changed (116) hide show
  1. package/CHANGELOG.md +119 -12
  2. package/README.md +130 -15
  3. package/README.zh.md +154 -39
  4. package/client/README.md +279 -0
  5. package/client/art/mascot-asleep.webp +0 -0
  6. package/client/art/mascot-busy.webp +0 -0
  7. package/client/art/mascot-nag.webp +0 -0
  8. package/client/art/mascot-ready.webp +0 -0
  9. package/client/art/mascot-trouble.webp +0 -0
  10. package/client/client.js +5540 -1280
  11. package/client/src/clipboard.cjs +52 -0
  12. package/client/src/constants.cjs +54 -0
  13. package/client/src/fields.cjs +1048 -0
  14. package/client/src/hooks.cjs +495 -0
  15. package/client/src/i18n.cjs +596 -0
  16. package/client/src/index.cjs +76 -0
  17. package/client/src/kit/controls.cjs +206 -0
  18. package/client/src/kit/icons.cjs +41 -0
  19. package/client/src/kit/layout.cjs +124 -0
  20. package/client/src/layout.cjs +154 -0
  21. package/client/src/model/changelog.cjs +29 -0
  22. package/client/src/model/core.cjs +105 -0
  23. package/client/src/model/feedback.cjs +168 -0
  24. package/client/src/model/form.cjs +83 -0
  25. package/client/src/model/memories.cjs +277 -0
  26. package/client/src/model/models.cjs +87 -0
  27. package/client/src/model/overview.cjs +111 -0
  28. package/client/src/model/page.cjs +104 -0
  29. package/client/src/model/status.cjs +57 -0
  30. package/client/src/model/trial.cjs +125 -0
  31. package/client/src/net.cjs +131 -0
  32. package/client/src/react.cjs +11 -0
  33. package/client/src/theme/page.css +1481 -0
  34. package/client/src/theme/styles.cjs +43 -0
  35. package/client/src/view/core.cjs +80 -0
  36. package/client/src/view/feedback.cjs +180 -0
  37. package/client/src/view/field-row.cjs +98 -0
  38. package/client/src/view/hero.cjs +54 -0
  39. package/client/src/view/markdown.cjs +96 -0
  40. package/client/src/view/memories.cjs +181 -0
  41. package/client/src/view/models.cjs +154 -0
  42. package/client/src/view/page.cjs +215 -0
  43. package/client/src/view/settings-tab.cjs +78 -0
  44. package/client/src/view/tabs.cjs +71 -0
  45. package/client/src/view/trial.cjs +106 -0
  46. package/docs/DETAIL.en.md +99 -8
  47. package/docs/DETAIL.md +107 -16
  48. package/locale/zh.json +1 -1
  49. package/package.json +2 -1
  50. package/python/GATEWAY_VERSION.json +47 -33
  51. package/python/feiyu_memgate/__init__.py +1 -1
  52. package/python/feiyu_memgate/backends/_sqlfile.py +430 -0
  53. package/python/feiyu_memgate/backends/base.py +216 -7
  54. package/python/feiyu_memgate/backends/embedding.py +722 -204
  55. package/python/feiyu_memgate/backends/hybrid.py +466 -86
  56. package/python/feiyu_memgate/backends/inmemory.py +245 -34
  57. package/python/feiyu_memgate/backends/openviking.py +7 -10
  58. package/python/feiyu_memgate/backends/sqlite.py +512 -218
  59. package/python/feiyu_memgate/builtins.py +1 -0
  60. package/python/feiyu_memgate/cli.py +869 -24
  61. package/python/feiyu_memgate/client/block.py +8 -2
  62. package/python/feiyu_memgate/client/http.py +339 -27
  63. package/python/feiyu_memgate/client/local.py +16 -5
  64. package/python/feiyu_memgate/config.py +167 -25
  65. package/python/feiyu_memgate/core/dates.py +762 -0
  66. package/python/feiyu_memgate/core/limits.py +2 -1
  67. package/python/feiyu_memgate/core/pool.py +29 -2
  68. package/python/feiyu_memgate/core/recall.py +197 -10
  69. package/python/feiyu_memgate/core/redact.py +4 -2
  70. package/python/feiyu_memgate/core/stages.py +594 -22
  71. package/python/feiyu_memgate/core/tidy.py +247 -28
  72. package/python/feiyu_memgate/core/watch.py +3 -2
  73. package/python/feiyu_memgate/core/write.py +20 -4
  74. package/python/feiyu_memgate/forgetting/archive.py +49 -10
  75. package/python/feiyu_memgate/forgetting/engine.py +78 -9
  76. package/python/feiyu_memgate/forgetting/stage.py +4 -2
  77. package/python/feiyu_memgate/gateway/app.py +841 -276
  78. package/python/feiyu_memgate/gateway/fields.py +126 -0
  79. package/python/feiyu_memgate/gateway/policies.py +266 -0
  80. package/python/feiyu_memgate/gateway/server.py +23 -5
  81. package/python/feiyu_memgate/gateway/tokens.py +137 -0
  82. package/python/feiyu_memgate/gateway/validate.py +173 -0
  83. package/python/feiyu_memgate/identity.py +152 -0
  84. package/python/feiyu_memgate/mcp/__init__.py +64 -0
  85. package/python/feiyu_memgate/mcp/protocol.py +330 -0
  86. package/python/feiyu_memgate/mcp/tools.py +321 -0
  87. package/python/feiyu_memgate/model.py +293 -2
  88. package/python/feiyu_memgate/registry.py +6 -3
  89. package/python/feiyu_memgate/standing/__init__.py +11 -0
  90. package/python/feiyu_memgate/standing/engine.py +98 -0
  91. package/python/feiyu_memgate/standing/select.py +79 -0
  92. package/python/feiyu_memgate/standing/state.py +127 -0
  93. package/python/feiyu_memgate/tools/crypto.py +5 -4
  94. package/python/feiyu_memgate/tools/evaluate.py +2 -1
  95. package/python/feiyu_memgate/tools/tenants.py +2 -0
  96. package/src/block.js +13 -3
  97. package/src/capture.js +98 -0
  98. package/src/catalogue.js +4 -4
  99. package/src/changelog.js +72 -0
  100. package/src/client.js +411 -12
  101. package/src/commands.js +112 -62
  102. package/src/config.js +171 -62
  103. package/src/followup.js +139 -0
  104. package/src/gateway.js +112 -6
  105. package/src/migrate.py +118 -16
  106. package/src/mirror.py +91 -7
  107. package/src/plugin.js +564 -415
  108. package/src/pool.js +369 -0
  109. package/src/recent.js +68 -0
  110. package/src/relevance.js +80 -0
  111. package/src/routes.js +490 -0
  112. package/src/setup.js +36 -36
  113. package/src/shared.js +135 -0
  114. package/src/tiers.js +4 -4
  115. package/src/time.js +27 -0
  116. package/src/tools.js +208 -0
@@ -21,6 +21,7 @@ import calendar
21
21
  import fnmatch
22
22
  import math
23
23
  import time
24
+ from typing import Any
24
25
 
25
26
  DAY = 86400.0
26
27
  REQUIRED = ("refs", "delete", "put")
@@ -43,7 +44,8 @@ def supports(store):
43
44
 
44
45
  class Forgetting:
45
46
  def __init__(self, ledger, archive, curve, *, stores=(), demote_below=0.3, forget_below=0.05,
46
- archive_days=90, pin_refs=(), max_forget_fraction=0.02, max_forget_min=5, wall=time.time):
47
+ archive_days=90, pin_refs=(), max_forget_fraction=0.02, max_forget_min=5, audiences=None,
48
+ wall=time.time):
47
49
  self.ledger = ledger
48
50
  self.archive = archive
49
51
  self.curve = curve
@@ -54,6 +56,9 @@ class Forgetting:
54
56
  self.pin_refs = tuple(pin_refs)
55
57
  self.max_forget_fraction = max_forget_fraction
56
58
  self.max_forget_min = max_forget_min
59
+ # {store or tenants provider name: its declared audience}: what a restored memory that was archived
60
+ # without an audience gets. None (a service built in code) restores such a memory as it was.
61
+ self.audiences = dict(audiences) if audiences else None
57
62
  self.wall = wall
58
63
 
59
64
  # ---------------------------------------------------------------- keys and scores
@@ -137,7 +142,7 @@ class Forgetting:
137
142
  def record_use(self, key, items, now=None):
138
143
  """items: (ref, date) of the memories that were returned to a caller."""
139
144
  now = self.wall() if now is None else now
140
- seen = {}
145
+ seen: dict[Any, float | None] = {}
141
146
  for ref, date in items:
142
147
  seen.setdefault(ref, date_to_epoch(date))
143
148
  if seen:
@@ -145,7 +150,7 @@ class Forgetting:
145
150
 
146
151
  def _sync_listing(self, key, listing, now):
147
152
  known = {entry.ref for entry in self.ledger.entries(key)}
148
- present = {}
153
+ present: dict[Any, Any] = {}
149
154
  for ref, date in listing:
150
155
  present.setdefault(ref, date)
151
156
  added = [(ref, date_to_epoch(date)) for ref, date in present.items() if ref not in known]
@@ -196,7 +201,7 @@ class Forgetting:
196
201
  listings but writes nothing anywhere.
197
202
  """
198
203
  now = self.wall() if now is None else now
199
- report = {"unsupported": []}
204
+ report: dict[str, Any] = {"unsupported": []}
200
205
  for key, store in targets.items():
201
206
  if not supports(store):
202
207
  report["unsupported"].append(key)
@@ -213,7 +218,7 @@ class Forgetting:
213
218
  except Exception: # noqa: BLE001 - a store that cannot list is retried next time
214
219
  row["failed"] = 1
215
220
  return row
216
- dates = {}
221
+ dates: dict[Any, Any] = {}
217
222
  for ref, date in listing:
218
223
  dates.setdefault(ref, date)
219
224
  if dry_run:
@@ -244,17 +249,59 @@ class Forgetting:
244
249
  row["failed"] += 1
245
250
  return row
246
251
 
252
+ def forget_now(self, key, store, refs, now=None):
253
+ """Forget these memories at once, whatever their retention and even when pinned: the order and the state
254
+ left behind are a sweep's (read the record, write the archive, delete from the store, drop the ledger row;
255
+ a failure leaves that memory where it is), so `restore` brings one back. Returns numbers only:
256
+ {"forgotten": n, "missing": refs the store does not hold, "failed": refs left in place}. ValueError for a
257
+ store without the capabilities forgetting needs (see `supports`)."""
258
+ if not supports(store):
259
+ raise ValueError("the store lacks refs, delete or put")
260
+ now = self.wall() if now is None else now
261
+ dates: dict[Any, Any] = {}
262
+ for ref, date in store.refs():
263
+ dates.setdefault(ref, date)
264
+ report = {"forgotten": 0, "missing": 0, "failed": 0}
265
+ refs = list(dict.fromkeys(refs))
266
+ entries = self.ledger.get_many(key, refs) if refs else {}
267
+ for ref in refs:
268
+ if ref not in dates:
269
+ report["missing"] += 1
270
+ elif self._forget(key, store, ref, dates[ref], entries.get(ref), now):
271
+ report["forgotten"] += 1
272
+ else:
273
+ report["failed"] += 1
274
+ return report
275
+
247
276
  def forget_limit(self, size):
248
277
  """How many memories one sweep may forget in a store holding `size` memories."""
249
278
  return max(int(self.max_forget_min), int(math.floor(self.max_forget_fraction * size + 1e-9)))
250
279
 
251
280
  def _forget(self, key, store, ref, date, entry, now):
252
281
  try:
253
- text = store.read(ref)
282
+ fields: dict[str, Any] = {}
283
+ cues: list[str] = []
284
+ record = getattr(store, "record", None)
285
+ if callable(record):
286
+ try:
287
+ row = record(ref)
288
+ text, fields = row["text"], dict(row.get("fields") or {})
289
+ cues = [cue for cue in (row.get("cues") or ()) if isinstance(cue, str)]
290
+ except Exception: # noqa: BLE001 - a store that cannot give the record is archived as before
291
+ text, fields, cues = store.read(ref), {}, []
292
+ else:
293
+ text = store.read(ref)
254
294
  if not isinstance(text, str):
255
295
  return False
296
+ # Only what the memory has is named: an archive written for memories without fields or cues is
297
+ # called exactly as before.
298
+ extra: dict[str, Any] = {}
299
+ if fields:
300
+ extra["fields"] = fields
301
+ if cues:
302
+ extra["cues"] = cues
256
303
  self.archive.add(key, ref, text, date, entry.uses if entry else 0, now,
257
- now + self.archive_days * DAY)
304
+ now + self.archive_days * DAY, **extra)
258
305
  except Exception: # noqa: BLE001 - nothing was deleted, so nothing is lost
259
306
  return False
260
307
  try:
@@ -274,12 +321,34 @@ class Forgetting:
274
321
  pass
275
322
  return True
276
323
 
324
+ def audience_of(self, key):
325
+ """The declared audience of the store behind the ledger key ("name" or "provider:tenant"), or None
326
+ when this service was not told the levels."""
327
+ if not self.audiences:
328
+ return None
329
+ level = self.audiences.get(key)
330
+ return level if level is not None else self.audiences.get(key.partition(":")[0])
331
+
277
332
  def restore(self, key, ref, store):
278
- """Write an archived memory back to its store and address. False when it is not archived."""
333
+ """Write an archived memory back to its store and address. False when it is not archived.
334
+
335
+ A store that keeps record fields gets the memory's archived fields back; one archived before records
336
+ had an audience is restored with the store's declared audience (never with none). A store that keeps
337
+ recall cues gets the archived cues back."""
279
338
  record = self.archive.get(key, ref)
280
339
  if record is None:
281
340
  return False
282
- store.put(record.text, ref=ref, date=record.date)
341
+ keeps = getattr(store, "stores_fields", False) is True
342
+ fields = dict(record.fields)
343
+ if keeps and "audience" not in fields and self.audience_of(key) is not None:
344
+ fields["audience"] = self.audience_of(key)
345
+ extra: dict[str, Any] = {}
346
+ if fields and keeps:
347
+ extra["fields"] = fields
348
+ cues = list(getattr(record, "cues", None) or ())
349
+ if cues and getattr(store, "stores_cues", False) is True:
350
+ extra["cues"] = cues
351
+ store.put(record.text, ref=ref, date=record.date, **extra)
283
352
  self.ledger.revive(key, ref, self.wall())
284
353
  self.archive.remove(key, ref)
285
354
  return True
@@ -7,6 +7,8 @@ and the access log gets the note "ledger-failed".
7
7
 
8
8
  Standard library only.
9
9
  """
10
+ from typing import Any
11
+
10
12
  from ..core.recall import RecallStage
11
13
  from ..errors import ConfigError
12
14
  from ..registry import register
@@ -63,7 +65,7 @@ class Freshness(RecallStage):
63
65
  before = [(c, c.score) for c in managed]
64
66
  try:
65
67
  now = f.now()
66
- by_key = {}
68
+ by_key: dict[Any, list[Any]] = {}
67
69
  for c in managed:
68
70
  by_key.setdefault(_key(f, c, ctx), []).append(c)
69
71
  for key, group in by_key.items():
@@ -96,7 +98,7 @@ class Freshness(RecallStage):
96
98
  return
97
99
  f = self.forgetting
98
100
  managed = [c for c in ctx.accepted if f.manages(c.source)]
99
- by_key = {}
101
+ by_key: dict[Any, list[Any]] = {}
100
102
  for c in self._credited(managed):
101
103
  by_key.setdefault(_key(f, c, ctx), []).append((c.ref, c.date))
102
104
  if not by_key: