dsh-feiyu-memgate 0.3.2 → 0.4.0

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 (103) hide show
  1. package/CHANGELOG.md +73 -12
  2. package/README.md +68 -11
  3. package/README.zh.md +93 -36
  4. package/client/README.md +267 -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 +4226 -1269
  11. package/client/src/clipboard.cjs +52 -0
  12. package/client/src/constants.cjs +43 -0
  13. package/client/src/fields.cjs +960 -0
  14. package/client/src/hooks.cjs +220 -0
  15. package/client/src/i18n.cjs +444 -0
  16. package/client/src/index.cjs +69 -0
  17. package/client/src/kit/controls.cjs +189 -0
  18. package/client/src/kit/icons.cjs +40 -0
  19. package/client/src/kit/layout.cjs +124 -0
  20. package/client/src/layout.cjs +143 -0
  21. package/client/src/model/changelog.cjs +29 -0
  22. package/client/src/model/feedback.cjs +168 -0
  23. package/client/src/model/form.cjs +83 -0
  24. package/client/src/model/models.cjs +87 -0
  25. package/client/src/model/overview.cjs +111 -0
  26. package/client/src/model/page.cjs +104 -0
  27. package/client/src/model/status.cjs +57 -0
  28. package/client/src/model/trial.cjs +111 -0
  29. package/client/src/net.cjs +101 -0
  30. package/client/src/react.cjs +11 -0
  31. package/client/src/theme/page.css +1284 -0
  32. package/client/src/theme/styles.cjs +43 -0
  33. package/client/src/view/feedback.cjs +180 -0
  34. package/client/src/view/field-row.cjs +98 -0
  35. package/client/src/view/hero.cjs +54 -0
  36. package/client/src/view/markdown.cjs +96 -0
  37. package/client/src/view/models.cjs +154 -0
  38. package/client/src/view/page.cjs +199 -0
  39. package/client/src/view/settings-tab.cjs +78 -0
  40. package/client/src/view/tabs.cjs +71 -0
  41. package/client/src/view/trial.cjs +84 -0
  42. package/docs/DETAIL.en.md +55 -6
  43. package/docs/DETAIL.md +63 -14
  44. package/locale/zh.json +1 -1
  45. package/package.json +2 -1
  46. package/python/GATEWAY_VERSION.json +37 -31
  47. package/python/feiyu_memgate/backends/_sqlfile.py +293 -0
  48. package/python/feiyu_memgate/backends/base.py +121 -6
  49. package/python/feiyu_memgate/backends/embedding.py +265 -178
  50. package/python/feiyu_memgate/backends/hybrid.py +330 -83
  51. package/python/feiyu_memgate/backends/inmemory.py +115 -24
  52. package/python/feiyu_memgate/backends/openviking.py +7 -10
  53. package/python/feiyu_memgate/backends/sqlite.py +129 -145
  54. package/python/feiyu_memgate/builtins.py +1 -0
  55. package/python/feiyu_memgate/cli.py +124 -20
  56. package/python/feiyu_memgate/client/block.py +4 -2
  57. package/python/feiyu_memgate/client/http.py +106 -19
  58. package/python/feiyu_memgate/client/local.py +4 -3
  59. package/python/feiyu_memgate/config.py +118 -24
  60. package/python/feiyu_memgate/core/limits.py +2 -1
  61. package/python/feiyu_memgate/core/pool.py +29 -2
  62. package/python/feiyu_memgate/core/recall.py +179 -10
  63. package/python/feiyu_memgate/core/redact.py +4 -2
  64. package/python/feiyu_memgate/core/stages.py +52 -9
  65. package/python/feiyu_memgate/core/tidy.py +2 -1
  66. package/python/feiyu_memgate/core/watch.py +3 -2
  67. package/python/feiyu_memgate/core/write.py +9 -3
  68. package/python/feiyu_memgate/forgetting/archive.py +38 -10
  69. package/python/feiyu_memgate/forgetting/engine.py +46 -10
  70. package/python/feiyu_memgate/forgetting/stage.py +4 -2
  71. package/python/feiyu_memgate/gateway/app.py +168 -272
  72. package/python/feiyu_memgate/gateway/fields.py +121 -0
  73. package/python/feiyu_memgate/gateway/policies.py +162 -0
  74. package/python/feiyu_memgate/gateway/server.py +12 -4
  75. package/python/feiyu_memgate/gateway/tokens.py +137 -0
  76. package/python/feiyu_memgate/gateway/validate.py +120 -0
  77. package/python/feiyu_memgate/identity.py +152 -0
  78. package/python/feiyu_memgate/model.py +170 -2
  79. package/python/feiyu_memgate/registry.py +6 -3
  80. package/python/feiyu_memgate/tools/crypto.py +5 -4
  81. package/python/feiyu_memgate/tools/evaluate.py +2 -1
  82. package/python/feiyu_memgate/tools/tenants.py +2 -0
  83. package/src/block.js +9 -3
  84. package/src/capture.js +98 -0
  85. package/src/catalogue.js +4 -4
  86. package/src/changelog.js +72 -0
  87. package/src/client.js +145 -10
  88. package/src/commands.js +112 -62
  89. package/src/config.js +139 -62
  90. package/src/followup.js +139 -0
  91. package/src/gateway.js +79 -4
  92. package/src/migrate.py +54 -9
  93. package/src/mirror.py +44 -7
  94. package/src/plugin.js +416 -412
  95. package/src/pool.js +298 -0
  96. package/src/recent.js +68 -0
  97. package/src/relevance.js +80 -0
  98. package/src/routes.js +320 -0
  99. package/src/setup.js +36 -36
  100. package/src/shared.js +77 -0
  101. package/src/tiers.js +4 -4
  102. package/src/time.js +27 -0
  103. package/src/tools.js +164 -0
@@ -8,6 +8,13 @@ Standard library only.
8
8
  """
9
9
  import abc
10
10
  import datetime
11
+ import uuid
12
+
13
+ from ..model import Hit, clean_fields
14
+
15
+ DEFAULT_BUDGET = 10.0 # seconds for one find() when the caller gives none (the backends that use it)
16
+
17
+ HAS_META = "meta" in getattr(Hit, "__dataclass_fields__", {})
11
18
 
12
19
 
13
20
  def message_date(message, default=None):
@@ -32,6 +39,91 @@ def message_date(message, default=None):
32
39
  return head
33
40
 
34
41
 
42
+ def today():
43
+ """Today's date in UTC as "YYYY-MM-DD"."""
44
+ return datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d")
45
+
46
+
47
+ def make_hit(ref, score, text, date, meta, fields=None):
48
+ """A Hit, with `meta` (a dict) when this version of Hit has a meta field, and with a copy of `fields`
49
+ (the memory's recorded facts) when there are any."""
50
+ extra = {"fields": dict(fields)} if fields else {}
51
+ if HAS_META:
52
+ try:
53
+ return Hit(ref=ref, score=score, text=text, date=date, meta=meta, **extra)
54
+ except TypeError: # pragma: no cover - a Hit whose meta has another shape
55
+ pass
56
+ return Hit(ref=ref, score=score, text=text, date=date, **extra)
57
+
58
+
59
+ def require_writable(backend):
60
+ """PermissionError unless `backend.writable` is true."""
61
+ if not backend.writable:
62
+ raise PermissionError("this backend is read-only")
63
+
64
+
65
+ def as_roles(roles):
66
+ """`roles` as given, except that one role name (a string) becomes a one-element tuple."""
67
+ if isinstance(roles, str):
68
+ roles = (roles,)
69
+ return roles
70
+
71
+
72
+ def clamp01(value):
73
+ """`value` held to the range 0..1."""
74
+ return min(1.0, max(0.0, value))
75
+
76
+
77
+ def new_ref():
78
+ """A fresh ref for a memory stored without one."""
79
+ return "mem-" + uuid.uuid4().hex
80
+
81
+
82
+ def new_receipt():
83
+ """A fresh receipt id for one write."""
84
+ return "w-" + uuid.uuid4().hex
85
+
86
+
87
+ def receipt_ref(receipt, index):
88
+ """The ref of message number `index` of the write `receipt`."""
89
+ return "%s-%d" % (receipt, index)
90
+
91
+
92
+ def kept_messages(messages, roles, today):
93
+ """[(index, content, date)] of the messages whose role is in `roles`; a message without a date of
94
+ its own is dated `today`."""
95
+ return [(i, m["content"], message_date(m, today)) for i, m in enumerate(messages) if m["role"] in roles]
96
+
97
+
98
+ def message_fields(message):
99
+ """The cleaned `fields` of a message (ValueError when they are not valid); {} when it has none."""
100
+ return clean_fields(message.get("fields")) if isinstance(message, dict) else {}
101
+
102
+
103
+ def parse_record(item):
104
+ """(text, ref, date, fields) of one item given to put_many(): a text, a (text, ref, date) tuple (ref
105
+ and date optional) or a dict with "text" and optionally "ref", "date" and "fields" (a dict, see
106
+ model.FIELDS). The ref is made when missing; only a dict item can carry fields. ValueError when the
107
+ text is not a non-empty string or the fields are not valid."""
108
+ if isinstance(item, str):
109
+ item = (item,)
110
+ fields = {}
111
+ if isinstance(item, dict):
112
+ text, ref, date = item.get("text"), item.get("ref"), item.get("date")
113
+ fields = clean_fields(item.get("fields"))
114
+ else:
115
+ text, ref, date = (tuple(item) + (None, None))[:3]
116
+ if not isinstance(text, str) or not text.strip():
117
+ raise ValueError("a memory text must be a non-empty string")
118
+ return text, ref or new_ref(), date, fields
119
+
120
+
121
+ def parse_item(item):
122
+ """(text, ref, date) of one item given to put_many(): see parse_record, which also gives the fields."""
123
+ text, ref, date, _ = parse_record(item)
124
+ return text, ref, date
125
+
126
+
35
127
  class MemoryBackend(abc.ABC):
36
128
  """Subclass this (or just provide the same methods) and register it:
37
129
 
@@ -66,7 +158,25 @@ class MemoryBackend(abc.ABC):
66
158
  delete(ref) -> True when the memory existed and is gone now.
67
159
  Needs writable=True (PermissionError otherwise).
68
160
  put(text, ref=None, date=None) -> ref. Stores one memory text as it is (creates or
69
- replaces). Needs writable=True.
161
+ replaces). Needs writable=True. A backend with
162
+ `stores_fields` True also takes `fields=None` (see below).
163
+ records() -> iterable of {"ref", "text", "date", "fields"} dicts for every
164
+ memory, in the order of items() (only backends that keep fields
165
+ and have items() offer it).
166
+ record(ref) -> that dict for one memory; KeyError when it does not exist.
167
+ stamp_audience(level) -> how many memories got `level`. Only for backends that keep
168
+ fields. The first call gives every memory without an audience
169
+ `level` and remembers that the store was stamped; later calls do
170
+ nothing; a read-only store is not stamped. The gateway calls it
171
+ with the store's declared level when it is built (a tenant's
172
+ store each time the provider hands it out).
173
+ checks_audience -> read-only property, True once the store was stamped. The
174
+ `record_audience` recall stage then drops a memory whose
175
+ audience is narrower than the client's (none counts as "owner").
176
+ set_audience(level, only_missing=True)
177
+ -> how many memories changed: give the memories `level`, only the
178
+ ones without an audience or all of them (the `restamp` command).
179
+ Also marks the store stamped.
70
180
  receipt_refs(receipt) -> list of the refs the write `receipt` produced,
71
181
  or None when not known (yet) or not supported.
72
182
  neighbours(ref, limit, timeout=None, ctl=None)
@@ -80,6 +190,10 @@ class MemoryBackend(abc.ABC):
80
190
  best ones; a backend without it is simply left out of that.
81
191
  """
82
192
 
193
+ # True when the backend keeps the `fields` of a memory (put(..., fields=...), a dict item of put_many(), a
194
+ # message key "fields" of remember(); see model.FIELDS) and returns them in the Hit.fields of find() and
195
+ # neighbours() and in records() / record(). A backend without it is never given fields.
196
+ stores_fields = False
83
197
  # False makes remember() refuse; the gateway also checks it before writing.
84
198
  writable = False
85
199
  # True when write_state() can tell what became of a write. The gateway then
@@ -87,16 +201,17 @@ class MemoryBackend(abc.ABC):
87
201
  tracks_writes = False
88
202
  # The min_score a recall should use for this backend when the policy names none (the gateway's
89
203
  # `min_score` stage reads it). The `default_` prefix is historical: it names the line of the "precise"
90
- # profile, not the default profile (the default profile is "broad", see broad_* below). Scores of different backends mean different things (keyword
91
- # overlap, semantic similarity), so a backend states a sensible floor for its own scale.
204
+ # profile, not the default profile (the default profile is "broad", see broad_* below). Scores of different
205
+ # backends mean different things (keyword overlap, semantic similarity), so a backend states a sensible floor
206
+ # for its own scale.
92
207
  # Optional: a backend that does not set it gets the gateway's own default.
93
- default_min_score = None
208
+ default_min_score: float | None = None
94
209
  # The recall-leaning counterparts, used by the `min_score` / `relative_score` stages when their option
95
210
  # `profile` is "broad", which is the stages' default (they trade precision and silence for more answered
96
211
  # questions). None means this backend has no distinct broad setting: the stages then use the default_ line
97
212
  # above under either profile.
98
- broad_min_score = None
99
- broad_relative_ratio = None
213
+ broad_min_score: float | None = None
214
+ broad_relative_ratio: float | None = None
100
215
 
101
216
  @abc.abstractmethod
102
217
  def find(self, query, limit, timeout=None, ctl=None):