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.
- package/CHANGELOG.md +73 -12
- package/README.md +68 -11
- package/README.zh.md +93 -36
- package/client/README.md +267 -0
- package/client/art/mascot-asleep.webp +0 -0
- package/client/art/mascot-busy.webp +0 -0
- package/client/art/mascot-nag.webp +0 -0
- package/client/art/mascot-ready.webp +0 -0
- package/client/art/mascot-trouble.webp +0 -0
- package/client/client.js +4226 -1269
- package/client/src/clipboard.cjs +52 -0
- package/client/src/constants.cjs +43 -0
- package/client/src/fields.cjs +960 -0
- package/client/src/hooks.cjs +220 -0
- package/client/src/i18n.cjs +444 -0
- package/client/src/index.cjs +69 -0
- package/client/src/kit/controls.cjs +189 -0
- package/client/src/kit/icons.cjs +40 -0
- package/client/src/kit/layout.cjs +124 -0
- package/client/src/layout.cjs +143 -0
- package/client/src/model/changelog.cjs +29 -0
- package/client/src/model/feedback.cjs +168 -0
- package/client/src/model/form.cjs +83 -0
- package/client/src/model/models.cjs +87 -0
- package/client/src/model/overview.cjs +111 -0
- package/client/src/model/page.cjs +104 -0
- package/client/src/model/status.cjs +57 -0
- package/client/src/model/trial.cjs +111 -0
- package/client/src/net.cjs +101 -0
- package/client/src/react.cjs +11 -0
- package/client/src/theme/page.css +1284 -0
- package/client/src/theme/styles.cjs +43 -0
- package/client/src/view/feedback.cjs +180 -0
- package/client/src/view/field-row.cjs +98 -0
- package/client/src/view/hero.cjs +54 -0
- package/client/src/view/markdown.cjs +96 -0
- package/client/src/view/models.cjs +154 -0
- package/client/src/view/page.cjs +199 -0
- package/client/src/view/settings-tab.cjs +78 -0
- package/client/src/view/tabs.cjs +71 -0
- package/client/src/view/trial.cjs +84 -0
- package/docs/DETAIL.en.md +55 -6
- package/docs/DETAIL.md +63 -14
- package/locale/zh.json +1 -1
- package/package.json +2 -1
- package/python/GATEWAY_VERSION.json +37 -31
- package/python/feiyu_memgate/backends/_sqlfile.py +293 -0
- package/python/feiyu_memgate/backends/base.py +121 -6
- package/python/feiyu_memgate/backends/embedding.py +265 -178
- package/python/feiyu_memgate/backends/hybrid.py +330 -83
- package/python/feiyu_memgate/backends/inmemory.py +115 -24
- package/python/feiyu_memgate/backends/openviking.py +7 -10
- package/python/feiyu_memgate/backends/sqlite.py +129 -145
- package/python/feiyu_memgate/builtins.py +1 -0
- package/python/feiyu_memgate/cli.py +124 -20
- package/python/feiyu_memgate/client/block.py +4 -2
- package/python/feiyu_memgate/client/http.py +106 -19
- package/python/feiyu_memgate/client/local.py +4 -3
- package/python/feiyu_memgate/config.py +118 -24
- package/python/feiyu_memgate/core/limits.py +2 -1
- package/python/feiyu_memgate/core/pool.py +29 -2
- package/python/feiyu_memgate/core/recall.py +179 -10
- package/python/feiyu_memgate/core/redact.py +4 -2
- package/python/feiyu_memgate/core/stages.py +52 -9
- package/python/feiyu_memgate/core/tidy.py +2 -1
- package/python/feiyu_memgate/core/watch.py +3 -2
- package/python/feiyu_memgate/core/write.py +9 -3
- package/python/feiyu_memgate/forgetting/archive.py +38 -10
- package/python/feiyu_memgate/forgetting/engine.py +46 -10
- package/python/feiyu_memgate/forgetting/stage.py +4 -2
- package/python/feiyu_memgate/gateway/app.py +168 -272
- package/python/feiyu_memgate/gateway/fields.py +121 -0
- package/python/feiyu_memgate/gateway/policies.py +162 -0
- package/python/feiyu_memgate/gateway/server.py +12 -4
- package/python/feiyu_memgate/gateway/tokens.py +137 -0
- package/python/feiyu_memgate/gateway/validate.py +120 -0
- package/python/feiyu_memgate/identity.py +152 -0
- package/python/feiyu_memgate/model.py +170 -2
- package/python/feiyu_memgate/registry.py +6 -3
- package/python/feiyu_memgate/tools/crypto.py +5 -4
- package/python/feiyu_memgate/tools/evaluate.py +2 -1
- package/python/feiyu_memgate/tools/tenants.py +2 -0
- package/src/block.js +9 -3
- package/src/capture.js +98 -0
- package/src/catalogue.js +4 -4
- package/src/changelog.js +72 -0
- package/src/client.js +145 -10
- package/src/commands.js +112 -62
- package/src/config.js +139 -62
- package/src/followup.js +139 -0
- package/src/gateway.js +79 -4
- package/src/migrate.py +54 -9
- package/src/mirror.py +44 -7
- package/src/plugin.js +416 -412
- package/src/pool.js +298 -0
- package/src/recent.js +68 -0
- package/src/relevance.js +80 -0
- package/src/routes.js +320 -0
- package/src/setup.js +36 -36
- package/src/shared.js +77 -0
- package/src/tiers.js +4 -4
- package/src/time.js +27 -0
- package/src/tools.js +164 -0
|
@@ -6,15 +6,24 @@ demos and as the smallest example of a backend. Standard library only.
|
|
|
6
6
|
Scoring is the same as the sqlite backend's: IDF-weighted containment of the query's grams
|
|
7
7
|
(character bigrams and single CJK ideographs), where a gram's weight comes from how many of the
|
|
8
8
|
memories held here contain it (the counts are kept up to date by put and delete).
|
|
9
|
+
|
|
10
|
+
The record fields of a memory (model.FIELDS) are kept in a second mapping beside `_items`, which keeps its
|
|
11
|
+
(text, date) shape; `find` returns them in `Hit.fields` and `records()` / `record(ref)` with the memory.
|
|
12
|
+
|
|
13
|
+
Audiences: `stamp_audience(level)` gives every memory without an audience `level` the first time it is called
|
|
14
|
+
and sets a flag; `checks_audience` is True from then on (the `record_audience` recall stage then reads each
|
|
15
|
+
memory's audience, one without counting as "owner"). `set_audience(level, only_missing)` changes it later on
|
|
16
|
+
purpose. Memories put without an audience after the stamp stay without one.
|
|
9
17
|
"""
|
|
10
18
|
import collections
|
|
11
19
|
import threading
|
|
12
|
-
import uuid
|
|
13
20
|
|
|
14
21
|
from ..core.tidy import PreparedQuery, gram_weights, index_grams, normalize
|
|
22
|
+
from ..errors import BackendError
|
|
15
23
|
from ..registry import register
|
|
16
|
-
from ..model import Hit
|
|
17
|
-
from .base import MemoryBackend, message_date
|
|
24
|
+
from ..model import AUDIENCES, Hit, clean_fields
|
|
25
|
+
from .base import (MemoryBackend, message_date, message_fields, new_receipt, new_ref, receipt_ref,
|
|
26
|
+
require_writable)
|
|
18
27
|
|
|
19
28
|
|
|
20
29
|
@register("backend", "inmemory")
|
|
@@ -27,30 +36,40 @@ class InMemoryBackend(MemoryBackend):
|
|
|
27
36
|
default_relative_ratio = 0.75 # see the sqlite backend
|
|
28
37
|
broad_min_score = 0.25 # the "broad" profile, same numbers and rule as the sqlite backend
|
|
29
38
|
broad_relative_ratio = 0.70
|
|
39
|
+
stores_fields = True # the record fields (model.FIELDS) are kept beside text and date
|
|
30
40
|
|
|
31
41
|
def __init__(self, writable=True, memories=()):
|
|
32
42
|
self.writable = bool(writable)
|
|
33
43
|
self._lock = threading.Lock()
|
|
34
44
|
self._items = {} # ref -> (text, date or None)
|
|
35
|
-
self.
|
|
45
|
+
self._fields = {} # ref -> the record fields of that memory (only memories that have some)
|
|
46
|
+
self._frequency: collections.Counter[str] = collections.Counter() # gram -> how many memories hold it
|
|
47
|
+
self._stamped = False # stamp_audience() has run (see checks_audience)
|
|
36
48
|
for text in memories:
|
|
37
49
|
self._store(text) # seeding works for a read-only store too
|
|
38
50
|
|
|
39
|
-
def put(self, text, ref=None, date=None):
|
|
40
|
-
"""Store one memory text as it is and return its ref."""
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return self._store(text, ref, date)
|
|
51
|
+
def put(self, text, ref=None, date=None, fields=None):
|
|
52
|
+
"""Store one memory text as it is and return its ref (an existing ref is replaced, fields included)."""
|
|
53
|
+
require_writable(self)
|
|
54
|
+
return self._store(text, ref, date, clean_fields(fields))
|
|
44
55
|
|
|
45
|
-
def _store(self, text, ref=None, date=None):
|
|
46
|
-
ref = ref or
|
|
56
|
+
def _store(self, text, ref=None, date=None, fields=None):
|
|
57
|
+
ref = ref or new_ref()
|
|
47
58
|
grams = index_grams(normalize(text))
|
|
48
59
|
with self._lock:
|
|
49
60
|
self._forget_grams(ref) # replacing a ref must not count its old text twice
|
|
50
|
-
self.
|
|
61
|
+
self._set(ref, text, date, fields)
|
|
51
62
|
self._frequency.update(grams)
|
|
52
63
|
return ref
|
|
53
64
|
|
|
65
|
+
def _set(self, ref, text, date, fields):
|
|
66
|
+
"""Keep one memory and its fields (a replaced memory loses the old ones). Under the lock."""
|
|
67
|
+
self._items[ref] = (text, date)
|
|
68
|
+
if fields:
|
|
69
|
+
self._fields[ref] = dict(fields)
|
|
70
|
+
else:
|
|
71
|
+
self._fields.pop(ref, None)
|
|
72
|
+
|
|
54
73
|
def _forget_grams(self, ref):
|
|
55
74
|
"""Take the grams of the memory stored under `ref` (if any) out of the counts. Under the lock."""
|
|
56
75
|
old = self._items.get(ref)
|
|
@@ -69,13 +88,13 @@ class InMemoryBackend(MemoryBackend):
|
|
|
69
88
|
|
|
70
89
|
def delete(self, ref):
|
|
71
90
|
"""True when the memory existed and is gone now."""
|
|
72
|
-
|
|
73
|
-
raise PermissionError("this backend is read-only")
|
|
91
|
+
require_writable(self)
|
|
74
92
|
with self._lock:
|
|
75
93
|
if ref not in self._items:
|
|
76
94
|
return False
|
|
77
95
|
self._forget_grams(ref)
|
|
78
96
|
del self._items[ref]
|
|
97
|
+
self._fields.pop(ref, None)
|
|
79
98
|
return True
|
|
80
99
|
|
|
81
100
|
def receipt_refs(self, receipt):
|
|
@@ -90,10 +109,11 @@ class InMemoryBackend(MemoryBackend):
|
|
|
90
109
|
def find(self, query, limit, timeout=None, ctl=None):
|
|
91
110
|
prepared = PreparedQuery(query) # normalised once, not once per memory
|
|
92
111
|
with self._lock:
|
|
93
|
-
items =
|
|
112
|
+
items = [(ref, text, date, self._fields.get(ref)) for ref, (text, date) in self._items.items()]
|
|
94
113
|
if len(prepared.norm) >= 2: # the weights are read under the lock: they match `items`
|
|
95
114
|
prepared = PreparedQuery(query, gram_weights(prepared.norm, self._frequency.get, len(items)))
|
|
96
|
-
hits = [Hit(ref=ref, score=prepared.score(text), text=text, date=date
|
|
115
|
+
hits = [Hit(ref=ref, score=prepared.score(text), text=text, date=date, fields=dict(fields or {}))
|
|
116
|
+
for ref, text, date, fields in items]
|
|
97
117
|
hits = [h for h in hits if h.score > 0]
|
|
98
118
|
hits.sort(key=lambda h: -h.score)
|
|
99
119
|
return hits[:limit]
|
|
@@ -102,14 +122,85 @@ class InMemoryBackend(MemoryBackend):
|
|
|
102
122
|
with self._lock:
|
|
103
123
|
return self._items[ref][0]
|
|
104
124
|
|
|
105
|
-
def
|
|
125
|
+
def records(self):
|
|
126
|
+
"""[{"ref", "text", "date", "fields"}] of every memory, in the order they were stored (a snapshot)."""
|
|
127
|
+
with self._lock:
|
|
128
|
+
return iter([{"ref": ref, "text": text, "date": date, "fields": dict(self._fields.get(ref, {}))}
|
|
129
|
+
for ref, (text, date) in self._items.items()])
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def checks_audience(self):
|
|
133
|
+
"""True once the store was stamped (stamp_audience, set_audience)."""
|
|
134
|
+
return self._stamped
|
|
135
|
+
|
|
136
|
+
@staticmethod
|
|
137
|
+
def _level(level):
|
|
138
|
+
if level not in AUDIENCES:
|
|
139
|
+
raise ValueError("an audience must be one of %s" % ", ".join(AUDIENCES))
|
|
140
|
+
return level
|
|
141
|
+
|
|
142
|
+
def stamp_audience(self, level):
|
|
143
|
+
"""The first time it is called: give every memory without an audience `level` and remember that the
|
|
144
|
+
store was stamped. Later calls do nothing, and a read-only store is not stamped. Returns how many
|
|
145
|
+
memories got the level."""
|
|
146
|
+
self._level(level)
|
|
147
|
+
if self._stamped or not self.writable:
|
|
148
|
+
return 0
|
|
149
|
+
with self._lock:
|
|
150
|
+
if self._stamped:
|
|
151
|
+
return 0
|
|
152
|
+
changed = self._set_audience(level, True)
|
|
153
|
+
self._stamped = True
|
|
154
|
+
return changed
|
|
155
|
+
|
|
156
|
+
def set_audience(self, level, only_missing=True):
|
|
157
|
+
"""Give the memories `level` as their audience: only those without one (`only_missing`), or every
|
|
158
|
+
memory. Also marks the store stamped. Returns how many memories changed (0 on a read-only store)."""
|
|
159
|
+
self._level(level)
|
|
106
160
|
if not self.writable:
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
161
|
+
return 0
|
|
162
|
+
with self._lock:
|
|
163
|
+
changed = self._set_audience(level, only_missing)
|
|
164
|
+
self._stamped = True
|
|
165
|
+
return changed
|
|
166
|
+
|
|
167
|
+
def _set_audience(self, level, only_missing):
|
|
168
|
+
"""Under the lock: set the audience of the memories and count the ones that changed."""
|
|
169
|
+
changed = 0
|
|
170
|
+
for ref in self._items:
|
|
171
|
+
fields = self._fields.setdefault(ref, {})
|
|
172
|
+
current = fields.get("audience")
|
|
173
|
+
if current == level or (only_missing and current is not None):
|
|
174
|
+
continue
|
|
175
|
+
fields["audience"] = level
|
|
176
|
+
changed += 1
|
|
177
|
+
return changed
|
|
178
|
+
|
|
179
|
+
def record(self, ref):
|
|
180
|
+
"""{"ref", "text", "date", "fields"} of one memory; KeyError when it is not stored."""
|
|
181
|
+
with self._lock:
|
|
182
|
+
text, date = self._items[ref]
|
|
183
|
+
return {"ref": ref, "text": text, "date": date, "fields": dict(self._fields.get(ref, {}))}
|
|
184
|
+
|
|
185
|
+
def remember(self, messages, timeout=None, ctl=None):
|
|
186
|
+
"""All or nothing: every row is prepared first, then all are stored under one hold of the
|
|
187
|
+
lock. A failure while preparing, or a caller that has given up (`ctl.cancelled`) when the
|
|
188
|
+
write is about to be applied, stores nothing (BackendError for the cancelled call)."""
|
|
189
|
+
require_writable(self)
|
|
190
|
+
receipt = new_receipt()
|
|
191
|
+
rows = [] # (ref, text, date, grams, fields)
|
|
110
192
|
for index, message in enumerate(messages):
|
|
111
193
|
if message["role"] == "user":
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
194
|
+
text = message["content"]
|
|
195
|
+
rows.append((receipt_ref(receipt, index), text, message_date(message),
|
|
196
|
+
index_grams(normalize(text)), message_fields(message)))
|
|
197
|
+
if not rows:
|
|
198
|
+
return None # nothing kept: no receipt for nothing
|
|
199
|
+
with self._lock:
|
|
200
|
+
if ctl is not None and ctl.cancelled:
|
|
201
|
+
raise BackendError("cancelled")
|
|
202
|
+
for ref, text, date, grams, fields in rows:
|
|
203
|
+
self._forget_grams(ref) # replacing a ref must not count its old text twice
|
|
204
|
+
self._set(ref, text, date, fields)
|
|
205
|
+
self._frequency.update(grams)
|
|
206
|
+
return receipt
|
|
@@ -19,7 +19,7 @@ from ..core.pool import _NoCtl
|
|
|
19
19
|
from ..errors import BackendError
|
|
20
20
|
from ..registry import register
|
|
21
21
|
from ..model import Hit
|
|
22
|
-
from .base import MemoryBackend
|
|
22
|
+
from .base import MemoryBackend, require_writable
|
|
23
23
|
|
|
24
24
|
MAX_RESPONSE = 1024 * 1024
|
|
25
25
|
TARGET_URI = "viking://~/memories"
|
|
@@ -191,7 +191,7 @@ class OpenVikingBackend(MemoryBackend):
|
|
|
191
191
|
parsed = json.loads(b"".join(chunks).decode("utf-8"))
|
|
192
192
|
except BackendError:
|
|
193
193
|
raise
|
|
194
|
-
except Exception:
|
|
194
|
+
except Exception: # noqa: BLE001 - any transport or parse failure becomes a BackendError, no upstream detail leaks
|
|
195
195
|
raise BackendError("upstream request failed") from None
|
|
196
196
|
finally:
|
|
197
197
|
ctl.detach()
|
|
@@ -293,8 +293,7 @@ class OpenVikingBackend(MemoryBackend):
|
|
|
293
293
|
def remember(self, messages, timeout=None, ctl=None):
|
|
294
294
|
"""Submit messages in one session; `timeout` is the total budget for all calls.
|
|
295
295
|
Returns the session id: OpenViking extracts memories from it in the background."""
|
|
296
|
-
|
|
297
|
-
raise PermissionError("this backend is read-only")
|
|
296
|
+
require_writable(self)
|
|
298
297
|
end = None if timeout is None else time.monotonic() + timeout
|
|
299
298
|
|
|
300
299
|
def left():
|
|
@@ -367,8 +366,7 @@ class OpenVikingBackend(MemoryBackend):
|
|
|
367
366
|
|
|
368
367
|
def delete(self, ref, timeout=None):
|
|
369
368
|
"""True when the memory existed and is gone now; False when it was already gone."""
|
|
370
|
-
|
|
371
|
-
raise PermissionError("this backend is read-only")
|
|
369
|
+
require_writable(self)
|
|
372
370
|
uri = self._own_memory_uri(ref)
|
|
373
371
|
quoted = urllib.parse.quote(uri, safe="")
|
|
374
372
|
# DELETE answers 200 for a path that is not there (it never says 404), and its
|
|
@@ -387,8 +385,7 @@ class OpenVikingBackend(MemoryBackend):
|
|
|
387
385
|
def put(self, text, ref=None, date=None, timeout=None):
|
|
388
386
|
"""Write one memory file as it is. `ref` (its URI) is required; `date` is ignored
|
|
389
387
|
because this store dates a memory by where its URI sits."""
|
|
390
|
-
|
|
391
|
-
raise PermissionError("this backend is read-only")
|
|
388
|
+
require_writable(self)
|
|
392
389
|
if ref is None:
|
|
393
390
|
raise ValueError("ref is required for this backend")
|
|
394
391
|
uri = self._own_memory_uri(ref)
|
|
@@ -423,11 +420,11 @@ class OpenVikingBackend(MemoryBackend):
|
|
|
423
420
|
pattern = re.compile(
|
|
424
421
|
r"^viking://(~|user/[^/]+)/sessions/" + re.escape(receipt) + r"/history/archive_[0-9]+/memory_diff\.json$"
|
|
425
422
|
)
|
|
426
|
-
found = []
|
|
423
|
+
found: list[tuple[str, str]] = []
|
|
427
424
|
for entry in listing:
|
|
428
425
|
uri = entry.get("uri") if isinstance(entry, dict) else entry
|
|
429
426
|
m = pattern.match(uri) if isinstance(uri, str) else None
|
|
430
|
-
if m:
|
|
427
|
+
if m and isinstance(uri, str):
|
|
431
428
|
found.append((uri, m.group(1)))
|
|
432
429
|
if len(found) != 1:
|
|
433
430
|
return None
|