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.
- package/CHANGELOG.md +119 -12
- package/README.md +130 -15
- package/README.zh.md +154 -39
- package/client/README.md +279 -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 +5540 -1280
- package/client/src/clipboard.cjs +52 -0
- package/client/src/constants.cjs +54 -0
- package/client/src/fields.cjs +1048 -0
- package/client/src/hooks.cjs +495 -0
- package/client/src/i18n.cjs +596 -0
- package/client/src/index.cjs +76 -0
- package/client/src/kit/controls.cjs +206 -0
- package/client/src/kit/icons.cjs +41 -0
- package/client/src/kit/layout.cjs +124 -0
- package/client/src/layout.cjs +154 -0
- package/client/src/model/changelog.cjs +29 -0
- package/client/src/model/core.cjs +105 -0
- package/client/src/model/feedback.cjs +168 -0
- package/client/src/model/form.cjs +83 -0
- package/client/src/model/memories.cjs +277 -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 +125 -0
- package/client/src/net.cjs +131 -0
- package/client/src/react.cjs +11 -0
- package/client/src/theme/page.css +1481 -0
- package/client/src/theme/styles.cjs +43 -0
- package/client/src/view/core.cjs +80 -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/memories.cjs +181 -0
- package/client/src/view/models.cjs +154 -0
- package/client/src/view/page.cjs +215 -0
- package/client/src/view/settings-tab.cjs +78 -0
- package/client/src/view/tabs.cjs +71 -0
- package/client/src/view/trial.cjs +106 -0
- package/docs/DETAIL.en.md +99 -8
- package/docs/DETAIL.md +107 -16
- package/locale/zh.json +1 -1
- package/package.json +2 -1
- package/python/GATEWAY_VERSION.json +47 -33
- package/python/feiyu_memgate/__init__.py +1 -1
- package/python/feiyu_memgate/backends/_sqlfile.py +430 -0
- package/python/feiyu_memgate/backends/base.py +216 -7
- package/python/feiyu_memgate/backends/embedding.py +722 -204
- package/python/feiyu_memgate/backends/hybrid.py +466 -86
- package/python/feiyu_memgate/backends/inmemory.py +245 -34
- package/python/feiyu_memgate/backends/openviking.py +7 -10
- package/python/feiyu_memgate/backends/sqlite.py +512 -218
- package/python/feiyu_memgate/builtins.py +1 -0
- package/python/feiyu_memgate/cli.py +869 -24
- package/python/feiyu_memgate/client/block.py +8 -2
- package/python/feiyu_memgate/client/http.py +339 -27
- package/python/feiyu_memgate/client/local.py +16 -5
- package/python/feiyu_memgate/config.py +167 -25
- package/python/feiyu_memgate/core/dates.py +762 -0
- 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 +197 -10
- package/python/feiyu_memgate/core/redact.py +4 -2
- package/python/feiyu_memgate/core/stages.py +594 -22
- package/python/feiyu_memgate/core/tidy.py +247 -28
- package/python/feiyu_memgate/core/watch.py +3 -2
- package/python/feiyu_memgate/core/write.py +20 -4
- package/python/feiyu_memgate/forgetting/archive.py +49 -10
- package/python/feiyu_memgate/forgetting/engine.py +78 -9
- package/python/feiyu_memgate/forgetting/stage.py +4 -2
- package/python/feiyu_memgate/gateway/app.py +841 -276
- package/python/feiyu_memgate/gateway/fields.py +126 -0
- package/python/feiyu_memgate/gateway/policies.py +266 -0
- package/python/feiyu_memgate/gateway/server.py +23 -5
- package/python/feiyu_memgate/gateway/tokens.py +137 -0
- package/python/feiyu_memgate/gateway/validate.py +173 -0
- package/python/feiyu_memgate/identity.py +152 -0
- package/python/feiyu_memgate/mcp/__init__.py +64 -0
- package/python/feiyu_memgate/mcp/protocol.py +330 -0
- package/python/feiyu_memgate/mcp/tools.py +321 -0
- package/python/feiyu_memgate/model.py +293 -2
- package/python/feiyu_memgate/registry.py +6 -3
- package/python/feiyu_memgate/standing/__init__.py +11 -0
- package/python/feiyu_memgate/standing/engine.py +98 -0
- package/python/feiyu_memgate/standing/select.py +79 -0
- package/python/feiyu_memgate/standing/state.py +127 -0
- 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 +13 -3
- package/src/capture.js +98 -0
- package/src/catalogue.js +4 -4
- package/src/changelog.js +72 -0
- package/src/client.js +411 -12
- package/src/commands.js +112 -62
- package/src/config.js +171 -62
- package/src/followup.js +139 -0
- package/src/gateway.js +112 -6
- package/src/migrate.py +118 -16
- package/src/mirror.py +91 -7
- package/src/plugin.js +564 -415
- package/src/pool.js +369 -0
- package/src/recent.js +68 -0
- package/src/relevance.js +80 -0
- package/src/routes.js +490 -0
- package/src/setup.js +36 -36
- package/src/shared.js +135 -0
- package/src/tiers.js +4 -4
- package/src/time.js +27 -0
- package/src/tools.js +208 -0
package/src/client.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
//
|
|
3
3
|
// A port of the gateway's Python client (client/http.py). Out of scope in this version:
|
|
4
4
|
// tenants (tenant_id, the tenant token, tenant-bound clients) and from_env. A plugin talks to
|
|
5
|
-
// one gateway with one token.
|
|
5
|
+
// one gateway with one token. recall and remember take an optional "context" (a small flat
|
|
6
|
+
// object of labels) and send it as the request's "context" when it passes the gateway's own
|
|
7
|
+
// check; one that does not is dropped with a warning, and the request goes out without it.
|
|
6
8
|
//
|
|
7
9
|
// Logs go through logger.warn and name only the kind of failure ("http 500 busy", "timeout"):
|
|
8
10
|
// never the query, a memory text, an item id or the token. The error code of an answer is
|
|
@@ -19,14 +21,23 @@
|
|
|
19
21
|
// - JSON.parse reads 3.0 as the integer 3 and rejects NaN/Infinity, which Python accepts or
|
|
20
22
|
// treats as a float; the gateway sends neither.
|
|
21
23
|
import { codePointLength, pyStrip } from './query.js'
|
|
24
|
+
import { MAX_CUES, cleanCues, cleanExcerpt, cleanReplaces } from './shared.js'
|
|
22
25
|
|
|
23
26
|
const MAX_QUERY_CHARS = 500 // gateway limit for /v1/recall
|
|
24
27
|
const MAX_REMEMBER_CHARS = 4000 // gateway limit per message for /v1/remember
|
|
25
28
|
const MAX_TOTAL_CHARS = 16000 // gateway limit for all messages of one /v1/remember
|
|
26
29
|
const MAX_MESSAGES = 20 // gateway limit of messages per /v1/remember
|
|
27
30
|
const MAX_TIME_CHARS = 40 // gateway limit of a message's optional "time"
|
|
28
|
-
const MAX_SPEAKER_CHARS = 64 // gateway limit of a message's optional "speaker"
|
|
31
|
+
const MAX_SPEAKER_CHARS = 64 // gateway limit of a message's optional "speaker" and "about"
|
|
29
32
|
const MAX_USED = 20 // gateway limit of items per /v1/used
|
|
33
|
+
const MAX_CONTEXT_KEYS = 16 // gateway limit of keys in a request's optional "context"
|
|
34
|
+
const MAX_CONTEXT_KEY_CHARS = 64 // gateway limit of a context key's length
|
|
35
|
+
const MAX_CONTEXT_VALUE_CHARS = 200 // gateway limit of a context string value's length
|
|
36
|
+
// The optional message keys that become record fields of the memory (the gateway's REQUEST_FIELDS).
|
|
37
|
+
const CHANNEL = /^[a-z0-9][a-z0-9_.:-]{0,63}$/
|
|
38
|
+
const ORIGINS = ['said', 'tool', 'curated', 'derived']
|
|
39
|
+
export const KINDS = ['identity', 'preference', 'event', 'plan', 'entity']
|
|
40
|
+
const FIELD_KEYS = ['speaker', 'about', 'channel', 'audience', 'origin', 'event_date', 'kind', 'superseded_by'] // the keys of a row's "fields"
|
|
30
41
|
const MAX_RESPONSE_BYTES = 256 * 1024
|
|
31
42
|
const MAX_ERROR_BYTES = 4096
|
|
32
43
|
|
|
@@ -58,6 +69,44 @@ function failure(status, code) {
|
|
|
58
69
|
return status >= 400 && status < 500 ? REJECTED : UNSURE
|
|
59
70
|
}
|
|
60
71
|
|
|
72
|
+
// Mirrors the gateway's _valid_context: a plain object of at most MAX_CONTEXT_KEYS keys, each key
|
|
73
|
+
// a non-empty string of at most MAX_CONTEXT_KEY_CHARS characters, each value a boolean, a finite
|
|
74
|
+
// number or a string of at most MAX_CONTEXT_VALUE_CHARS characters.
|
|
75
|
+
export function validContext(value) {
|
|
76
|
+
if (!isObject(value)) return false
|
|
77
|
+
const proto = Object.getPrototypeOf(value)
|
|
78
|
+
if (proto !== Object.prototype && proto !== null) return false
|
|
79
|
+
const keys = Object.keys(value)
|
|
80
|
+
if (keys.length > MAX_CONTEXT_KEYS) return false
|
|
81
|
+
for (const key of keys) {
|
|
82
|
+
if (!key || codePointLength(key) > MAX_CONTEXT_KEY_CHARS) return false
|
|
83
|
+
const item = value[key]
|
|
84
|
+
if (typeof item === 'boolean') continue
|
|
85
|
+
if (typeof item === 'number') {
|
|
86
|
+
if (!Number.isFinite(item)) return false
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
if (typeof item !== 'string' || codePointLength(item) > MAX_CONTEXT_VALUE_CHARS) return false
|
|
90
|
+
}
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// A real calendar date written YYYY-MM-DD and nothing else (the gateway's _valid_event_date).
|
|
95
|
+
export function validEventDate(value) {
|
|
96
|
+
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return false
|
|
97
|
+
const [year, month, day] = value.split('-').map(Number)
|
|
98
|
+
if (year < 1 || month < 1 || month > 12 || day < 1) return false
|
|
99
|
+
return day <= new Date(Date.UTC(2000 + (year % 400), month, 0)).getUTCDate()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Whether `value` is acceptable as the optional message key `key` (the gateway's _valid_field).
|
|
103
|
+
function validField(key, value) {
|
|
104
|
+
if (typeof value !== 'string') return false
|
|
105
|
+
if (key === 'channel') return CHANNEL.test(value)
|
|
106
|
+
if (key === 'event_date') return validEventDate(value)
|
|
107
|
+
return (key === 'origin' ? ORIGINS : KINDS).includes(value)
|
|
108
|
+
}
|
|
109
|
+
|
|
61
110
|
const count = (value) => (Number.isInteger(value) && value > 0 ? value : 0)
|
|
62
111
|
|
|
63
112
|
// The messages as the gateway takes them, or null when one breaks a gateway limit.
|
|
@@ -80,11 +129,120 @@ function cleanMessages(messages) {
|
|
|
80
129
|
if (typeof value !== 'string' || codePointLength(value) > cap) return null
|
|
81
130
|
item[key] = value
|
|
82
131
|
}
|
|
132
|
+
for (const key of ['channel', 'origin', 'event_date', 'kind']) {
|
|
133
|
+
const value = m[key]
|
|
134
|
+
if (value === null || value === undefined) continue
|
|
135
|
+
if (!validField(key, value)) return null
|
|
136
|
+
item[key] = value
|
|
137
|
+
}
|
|
138
|
+
const about = m.about
|
|
139
|
+
if (about !== null && about !== undefined) {
|
|
140
|
+
if (typeof about !== 'string' || codePointLength(about) > MAX_SPEAKER_CHARS) return null
|
|
141
|
+
item.about = about
|
|
142
|
+
}
|
|
143
|
+
// Recall cues: unusable ones are dropped here (the gateway would refuse the whole list); none left, no key.
|
|
144
|
+
const cues = cleanCues(m.cues)
|
|
145
|
+
if (cues.length > 0) item.cues = cues
|
|
146
|
+
// Replaced memories: the same, unusable quotes are dropped. Only advanced.toolReplaces ever puts this key in a message.
|
|
147
|
+
const replaces = cleanReplaces(m.replaces)
|
|
148
|
+
if (replaces.length > 0) item.replaces = replaces
|
|
83
149
|
clean.push(item)
|
|
84
150
|
}
|
|
85
151
|
return clean
|
|
86
152
|
}
|
|
87
153
|
|
|
154
|
+
const MAX_CORE_ITEMS = 100 // the most items of one list of a core memory answer the plugin keeps
|
|
155
|
+
export const MAX_SOURCE_CHARS = 128 // gateway limit of a source name in /v1/used and /v1/standing/core
|
|
156
|
+
export const MAX_ID_CHARS = 512 // gateway limit of a memory id there
|
|
157
|
+
export const CORE_MARKS = Object.freeze(['add', 'remove', 'none'])
|
|
158
|
+
const HOW = /^[a-z_]{1,24}$/ // how an item came into the core ('added', 'rule'): a short plain word or nothing
|
|
159
|
+
|
|
160
|
+
const OUTCOMES = new Set(['delivered', 'dropped', 'deferred', 'over_limit', 'not_reached'])
|
|
161
|
+
|
|
162
|
+
// One item of a core memory list as the plugin may rely on it, or null: only the listed keys, strings only,
|
|
163
|
+
// id and source not empty. A text is required unless `textOptional` (a removed memory that is gone has none).
|
|
164
|
+
function cleanCoreItem(c, { textOptional = false, how = false } = {}) {
|
|
165
|
+
if (!isObject(c) || typeof c.id !== 'string' || !c.id || typeof c.source !== 'string' || !c.source) return null
|
|
166
|
+
if (typeof c.text !== 'string' && !textOptional) return null
|
|
167
|
+
const out = { id: c.id, source: c.source }
|
|
168
|
+
if (typeof c.text === 'string') out.text = c.text
|
|
169
|
+
if (typeof c.date === 'string') out.date = c.date
|
|
170
|
+
if (how && typeof c.how === 'string' && HOW.test(c.how)) out.how = c.how
|
|
171
|
+
return out
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function cleanCoreList(list, options) {
|
|
175
|
+
const out = []
|
|
176
|
+
for (const item of list) {
|
|
177
|
+
const clean = cleanCoreItem(item, options)
|
|
178
|
+
if (clean) out.push(clean)
|
|
179
|
+
if (out.length >= MAX_CORE_ITEMS) break
|
|
180
|
+
}
|
|
181
|
+
return out
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const MAX_LIST_QUERY_CHARS = 200 // gateway limit of the search text of /v1/memories/list, after trimming
|
|
185
|
+
const MAX_LIST_OFFSET = 1000000 // gateway limit of its offset
|
|
186
|
+
const MAX_LIST_LIMIT = 50 // gateway limit of its page size
|
|
187
|
+
|
|
188
|
+
// One memory of the owner's view as the page may rely on it, or null: source, id and text are strings (source and id not
|
|
189
|
+
// empty); only the documented keys pass. `length` is the gateway's count of the whole text (the text's own length when
|
|
190
|
+
// the gateway gave none); the cues pass cleanCues; fields only holds the known keys, as strings.
|
|
191
|
+
function cleanMemory(m) {
|
|
192
|
+
if (!isObject(m) || typeof m.source !== 'string' || !m.source || typeof m.id !== 'string' || !m.id || typeof m.text !== 'string') return null
|
|
193
|
+
const out = { source: m.source, id: m.id, text: m.text, length: Number.isInteger(m.length) && m.length >= 0 ? m.length : codePointLength(m.text) }
|
|
194
|
+
if (typeof m.date === 'string') out.date = m.date
|
|
195
|
+
if (isObject(m.fields)) {
|
|
196
|
+
const fields = {}
|
|
197
|
+
for (const key of FIELD_KEYS) if (typeof m.fields[key] === 'string') fields[key] = m.fields[key]
|
|
198
|
+
if (Object.keys(fields).length) out.fields = fields
|
|
199
|
+
}
|
|
200
|
+
const cues = cleanCues(m.cues)
|
|
201
|
+
if (cues.length > 0) out.cues = cues
|
|
202
|
+
return out
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// One store of the owner's list: the count (null when the store could not be read), whether it lists records, whether it keeps cues.
|
|
206
|
+
function cleanStore(s) {
|
|
207
|
+
if (!isObject(s) || typeof s.source !== 'string' || !s.source) return null
|
|
208
|
+
return { source: s.source, count: Number.isInteger(s.count) && s.count >= 0 ? s.count : null, listable: s.listable === true, cues: s.cues === true }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// One entry of an explained recall as the page may rely on it, or null when it is not well formed.
|
|
212
|
+
function cleanCandidate(c) {
|
|
213
|
+
if (!isObject(c) || typeof c.source !== 'string' || typeof c.id !== 'string') return null
|
|
214
|
+
if (!Number.isFinite(c.found) || !Number.isFinite(c.score) || !OUTCOMES.has(c.outcome)) return null
|
|
215
|
+
const out = {
|
|
216
|
+
source: c.source,
|
|
217
|
+
id: c.id,
|
|
218
|
+
found: c.found,
|
|
219
|
+
score: c.score,
|
|
220
|
+
outcome: c.outcome,
|
|
221
|
+
stage: typeof c.stage === 'string' ? c.stage : null,
|
|
222
|
+
addedBy: typeof c.added_by === 'string' ? c.added_by : null,
|
|
223
|
+
}
|
|
224
|
+
if (typeof c.text === 'string') out.text = c.text
|
|
225
|
+
if (typeof c.date === 'string') out.date = c.date
|
|
226
|
+
if (c.cut === true) out.cut = true
|
|
227
|
+
const excerpt = cleanExcerpt(c.excerpt)
|
|
228
|
+
if (excerpt) out.excerpt = excerpt
|
|
229
|
+
if (c.via === 'cue') out.via = 'cue' // found through a recall cue of the memory (only this value is kept)
|
|
230
|
+
if (isObject(c.fields)) {
|
|
231
|
+
const fields = {}
|
|
232
|
+
for (const key of FIELD_KEYS) if (typeof c.fields[key] === 'string') fields[key] = c.fields[key]
|
|
233
|
+
if (Object.keys(fields).length) out.fields = fields
|
|
234
|
+
}
|
|
235
|
+
return out
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// A result with its excerpt checked: a well formed one is kept (as the three numbers), a malformed one is dropped.
|
|
239
|
+
function withExcerpt(r) {
|
|
240
|
+
if (!('excerpt' in r)) return r
|
|
241
|
+
const { excerpt, ...rest } = r
|
|
242
|
+
const clean = cleanExcerpt(excerpt)
|
|
243
|
+
return clean ? { ...rest, excerpt: clean } : rest
|
|
244
|
+
}
|
|
245
|
+
|
|
88
246
|
function normaliseBase(url) {
|
|
89
247
|
if (typeof url !== 'string') return null
|
|
90
248
|
const trimmed = pyStrip(url).replace(/\/+$/, '')
|
|
@@ -140,7 +298,7 @@ async function errorCode(response) {
|
|
|
140
298
|
}
|
|
141
299
|
}
|
|
142
300
|
|
|
143
|
-
export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch, logger, recallLimit =
|
|
301
|
+
export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch, logger, recallLimit = 8 } = {}) {
|
|
144
302
|
const base = normaliseBase(url)
|
|
145
303
|
const secret = typeof token === 'string' ? pyStrip(token) : ''
|
|
146
304
|
const configured = Boolean(base && secret)
|
|
@@ -153,7 +311,35 @@ export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch,
|
|
|
153
311
|
}
|
|
154
312
|
}
|
|
155
313
|
|
|
156
|
-
//
|
|
314
|
+
// The optional "speaker" of a recall: a string of at most MAX_SPEAKER_CHARS characters goes into the body,
|
|
315
|
+
// anything else is left out (a label never makes a call fail, as with the context).
|
|
316
|
+
const validSpeaker = (speaker) => typeof speaker === 'string' && speaker !== '' && codePointLength(speaker) <= MAX_SPEAKER_CHARS
|
|
317
|
+
const validAddress = (source, id) => typeof source === 'string' && source !== '' && codePointLength(source) <= MAX_SOURCE_CHARS
|
|
318
|
+
&& typeof id === 'string' && id !== '' && codePointLength(id) <= MAX_ID_CHARS
|
|
319
|
+
|
|
320
|
+
// A request that is not sent: one plain warning naming the call and the kind of problem, never a value.
|
|
321
|
+
function notSent(what, why) {
|
|
322
|
+
warn(`memory ${what} not sent: ${why}`)
|
|
323
|
+
return { ok: false, code: 'failed' }
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function withSpeaker(body, speaker) {
|
|
327
|
+
return typeof speaker === 'string' && speaker && codePointLength(speaker) <= MAX_SPEAKER_CHARS ? { ...body, speaker } : body
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// { context } to add to a body: nothing when absent, nothing (and one warning that does not show
|
|
331
|
+
// it) when invalid. A label never makes a call fail.
|
|
332
|
+
function withContext(body, context, what) {
|
|
333
|
+
if (context === undefined || context === null) return body
|
|
334
|
+
if (!validContext(context)) {
|
|
335
|
+
warn(`memory ${what}: context dropped, it breaks the gateway's limits`)
|
|
336
|
+
return body
|
|
337
|
+
}
|
|
338
|
+
return { ...body, context: { ...context } }
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// One request until an absolute deadline (timeoutMs from now). Returns [object|null, outcome, status, code]; status and
|
|
342
|
+
// code are the HTTP status and the error code of an error answer (null when it had none), else absent.
|
|
157
343
|
async function request(method, path, payload, timeoutMs, what, signal, withToken) {
|
|
158
344
|
if (!base || (withToken && !secret)) return [null, NOT_SENT]
|
|
159
345
|
if (!(Number.isFinite(timeoutMs) && timeoutMs > 0)) {
|
|
@@ -200,7 +386,7 @@ export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch,
|
|
|
200
386
|
const shown = code !== null && /^[a-z0-9_]{1,40}$/.test(code) ? code : '-'
|
|
201
387
|
warn(`memory ${what} failed: http ${response.status} ${shown}`)
|
|
202
388
|
await response.body?.cancel().catch(() => {})
|
|
203
|
-
return [null, failure(response.status, code)]
|
|
389
|
+
return [null, failure(response.status, code), response.status, code]
|
|
204
390
|
}
|
|
205
391
|
// One deadline for the whole call: the abort signal also ends a body that drips in slowly.
|
|
206
392
|
const data = JSON.parse(new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(await readBody(response, MAX_RESPONSE_BYTES)))
|
|
@@ -218,18 +404,18 @@ export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch,
|
|
|
218
404
|
}
|
|
219
405
|
}
|
|
220
406
|
|
|
221
|
-
async function recallDetail(query, { timeoutMs = 8500, signal } = {}) {
|
|
407
|
+
async function recallDetail(query, { timeoutMs = 8500, signal, context, speaker } = {}) {
|
|
222
408
|
try {
|
|
223
409
|
if (typeof query !== 'string' || !pyStrip(query)) return null
|
|
224
410
|
if (codePointLength(query) > MAX_QUERY_CHARS) {
|
|
225
411
|
warn(`memory recall not sent: query over ${MAX_QUERY_CHARS} characters`)
|
|
226
412
|
return null
|
|
227
413
|
}
|
|
228
|
-
const [data] = await request('POST', '/v1/recall', { query, limit: recallLimit }, timeoutMs, 'recall', signal, true)
|
|
414
|
+
const [data] = await request('POST', '/v1/recall', withContext(withSpeaker({ query, limit: recallLimit }, speaker), context, 'recall'), timeoutMs, 'recall', signal, true)
|
|
229
415
|
const results = data?.results
|
|
230
416
|
if (!Array.isArray(results)) return null
|
|
231
417
|
return {
|
|
232
|
-
results: results.filter((r) => isObject(r) && typeof r.text === 'string'),
|
|
418
|
+
results: results.filter((r) => isObject(r) && typeof r.text === 'string').map(withExcerpt),
|
|
233
419
|
more: count(data.more),
|
|
234
420
|
failed: count(data.failed),
|
|
235
421
|
busy: count(data.busy),
|
|
@@ -239,15 +425,47 @@ export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch,
|
|
|
239
425
|
}
|
|
240
426
|
}
|
|
241
427
|
|
|
242
|
-
|
|
428
|
+
// POST /v1/explain: a recall with the reasons. { ok: true, stages, candidates, more, failed, busy,
|
|
429
|
+
// cut } or { ok: false, code }: 'unsupported' when the gateway answers 403, 404 or 405 (an older
|
|
430
|
+
// gateway, or explain is off for this client), 'failed' for anything else.
|
|
431
|
+
async function explainRecall(query, { timeoutMs = 8500, signal, context, limit = recallLimit, speaker } = {}) {
|
|
432
|
+
try {
|
|
433
|
+
if (typeof query !== 'string' || !pyStrip(query)) return { ok: false, code: 'failed' }
|
|
434
|
+
if (codePointLength(query) > MAX_QUERY_CHARS) {
|
|
435
|
+
warn(`memory explain not sent: query over ${MAX_QUERY_CHARS} characters`)
|
|
436
|
+
return { ok: false, code: 'failed' }
|
|
437
|
+
}
|
|
438
|
+
const [data, , status] = await request('POST', '/v1/explain', withContext(withSpeaker({ query, limit }, speaker), context, 'explain'), timeoutMs, 'explain', signal, true)
|
|
439
|
+
if (status === 403 || status === 404 || status === 405) return { ok: false, code: 'unsupported' }
|
|
440
|
+
if (!data || !Array.isArray(data.candidates)) return { ok: false, code: 'failed' }
|
|
441
|
+
return {
|
|
442
|
+
ok: true,
|
|
443
|
+
stages: Array.isArray(data.stages) ? data.stages.filter((s) => typeof s === 'string') : [],
|
|
444
|
+
candidates: data.candidates.map(cleanCandidate).filter((c) => c !== null),
|
|
445
|
+
more: count(data.more),
|
|
446
|
+
failed: count(data.failed),
|
|
447
|
+
busy: count(data.busy),
|
|
448
|
+
cut: data.candidates_cut === true,
|
|
449
|
+
}
|
|
450
|
+
} catch {
|
|
451
|
+
return { ok: false, code: 'failed' }
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// opts.onReplaced(n): called with the answer's "replaced" (how many stored memories were marked as replaced, 0 or more)
|
|
456
|
+
// when the write was taken and the answer has the count; never otherwise.
|
|
457
|
+
async function rememberMessages(messages, { timeoutMs = 12000, signal, context, onReplaced } = {}) {
|
|
243
458
|
try {
|
|
244
459
|
const clean = cleanMessages(messages)
|
|
245
460
|
if (clean === null) {
|
|
246
461
|
warn("memory remember not sent: messages break the gateway's limits")
|
|
247
462
|
return 'failed'
|
|
248
463
|
}
|
|
249
|
-
const [data, outcome] = await request('POST', '/v1/remember', { messages: clean }, timeoutMs, 'remember', signal, true)
|
|
250
|
-
if (outcome === OK)
|
|
464
|
+
const [data, outcome] = await request('POST', '/v1/remember', withContext({ messages: clean }, context, 'remember'), timeoutMs, 'remember', signal, true)
|
|
465
|
+
if (outcome === OK) {
|
|
466
|
+
if (data.status === 'ok' && typeof onReplaced === 'function' && Number.isInteger(data.replaced) && data.replaced >= 0) onReplaced(data.replaced)
|
|
467
|
+
return data.status === 'ok' ? 'saved' : data.status === 'skipped' ? 'skipped' : 'unsure'
|
|
468
|
+
}
|
|
251
469
|
return outcome === UNSURE ? 'unsure' : outcome === LIMITED ? 'limited' : 'failed'
|
|
252
470
|
} catch {
|
|
253
471
|
return 'failed'
|
|
@@ -256,10 +474,23 @@ export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch,
|
|
|
256
474
|
|
|
257
475
|
// opts.time: the message's own time ("2026-11-17T20:15:03+09:00", at most 40 characters), which the
|
|
258
476
|
// gateway uses to date the memory. Not a Python option: the Python client's remember() takes no time.
|
|
477
|
+
// opts.speaker, opts.origin, opts.kind, opts.eventDate: the message's own "speaker", "origin", "kind" and
|
|
478
|
+
// "event_date" when given; the gateway checks them as cleanMessages does. opts.cues: a list of recall cues
|
|
479
|
+
// (shared.js cleanCues), sent as the message's "cues" when any is usable. opts.replaces: quotes of stored memories this
|
|
480
|
+
// one makes untrue (shared.js cleanReplaces), sent as "replaces"; opts.onReplaced is told the count the gateway answers.
|
|
259
481
|
async function rememberStatus(text, opts) {
|
|
260
482
|
if (typeof text !== 'string' || !pyStrip(text)) return 'failed'
|
|
261
483
|
const message = { role: 'user', content: text }
|
|
262
484
|
if (typeof opts?.time === 'string') message.time = opts.time
|
|
485
|
+
if (typeof opts?.speaker === 'string') message.speaker = opts.speaker
|
|
486
|
+
if (typeof opts?.origin === 'string') message.origin = opts.origin
|
|
487
|
+
if (typeof opts?.kind === 'string') message.kind = opts.kind
|
|
488
|
+
if (typeof opts?.eventDate === 'string') message.event_date = opts.eventDate
|
|
489
|
+
// Recall cues: cleaned here (unusable ones dropped, the first six kept); none left, no key.
|
|
490
|
+
const cues = cleanCues(opts?.cues)
|
|
491
|
+
if (cues.length > 0) message.cues = cues
|
|
492
|
+
const replaces = cleanReplaces(opts?.replaces)
|
|
493
|
+
if (replaces.length > 0) message.replaces = replaces
|
|
263
494
|
return rememberMessages([message], opts)
|
|
264
495
|
}
|
|
265
496
|
|
|
@@ -286,6 +517,174 @@ export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch,
|
|
|
286
517
|
}
|
|
287
518
|
}
|
|
288
519
|
|
|
520
|
+
// POST /v1/standing: the owner's core memory. { ok: true, day, core: [{ id, source, text, date?, how? }], stale?,
|
|
521
|
+
// leftOut?, removed?, characters?, budget? } or { ok: false, code }: 'unsupported' when the gateway answers 403, 404
|
|
522
|
+
// or 405 (a gateway without core memory, or one that does not let this client or this speaker have it), 'failed'
|
|
523
|
+
// for anything else, a speaker the gateway would refuse included (nothing is sent then). The detail keys come only
|
|
524
|
+
// with `detail: true` (the management view: it needs the gateway's standing_edit) and only when the answer has them.
|
|
525
|
+
async function standing({ timeoutMs = 7000, signal, context, speaker, detail = false } = {}) {
|
|
526
|
+
try {
|
|
527
|
+
if (typeof speaker !== 'string' || !speaker || codePointLength(speaker) > MAX_SPEAKER_CHARS) {
|
|
528
|
+
warn('memory standing not sent: bad speaker')
|
|
529
|
+
return { ok: false, code: 'failed' }
|
|
530
|
+
}
|
|
531
|
+
const body = withContext({ speaker }, context, 'standing')
|
|
532
|
+
const [data, , status] = await request('POST', '/v1/standing', detail === true ? { ...body, detail: true } : body, timeoutMs, 'standing', signal, true)
|
|
533
|
+
if (status === 403 || status === 404 || status === 405) return { ok: false, code: 'unsupported' }
|
|
534
|
+
if (!data || typeof data.day !== 'string' || !Array.isArray(data.core)) return { ok: false, code: 'failed' }
|
|
535
|
+
const out = { ok: true, day: data.day, core: cleanCoreList(data.core, { how: true }) }
|
|
536
|
+
if (data.stale === true) out.stale = true
|
|
537
|
+
if (detail === true) {
|
|
538
|
+
if (Array.isArray(data.left_out)) out.leftOut = cleanCoreList(data.left_out)
|
|
539
|
+
if (Array.isArray(data.removed)) out.removed = cleanCoreList(data.removed, { textOptional: true })
|
|
540
|
+
if (Number.isInteger(data.characters) && data.characters >= 0) out.characters = data.characters
|
|
541
|
+
if (Number.isInteger(data.budget) && data.budget >= 0) out.budget = data.budget
|
|
542
|
+
if (data.rule === 'none' || data.rule === 'identity') out.rule = data.rule
|
|
543
|
+
}
|
|
544
|
+
return out
|
|
545
|
+
} catch {
|
|
546
|
+
return { ok: false, code: 'failed' }
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// POST /v1/standing/core: the owner adds, removes or restores core memories by hand. entries: 1 to 20 of
|
|
551
|
+
// { source, id, mark: 'add' | 'remove' | 'none' }; other keys are not sent. { ok: true, changed } (how many marks
|
|
552
|
+
// changed) or { ok: false, code }: 'unsupported' (403, 404 or 405 of the route), 'unknown' (the gateway's 404
|
|
553
|
+
// unknown_memory: a memory that is not there; nothing was changed), 'failed'. A list the gateway would refuse
|
|
554
|
+
// is not sent.
|
|
555
|
+
async function markCore(entries, { timeoutMs = 7000, signal, speaker } = {}) {
|
|
556
|
+
try {
|
|
557
|
+
if (typeof speaker !== 'string' || !speaker || codePointLength(speaker) > MAX_SPEAKER_CHARS) {
|
|
558
|
+
warn('memory core-mark not sent: bad speaker')
|
|
559
|
+
return { ok: false, code: 'failed' }
|
|
560
|
+
}
|
|
561
|
+
if (!Array.isArray(entries) || entries.length === 0 || entries.length > MAX_USED) {
|
|
562
|
+
warn(`memory core-mark not sent: need 1 to ${MAX_USED} entries`)
|
|
563
|
+
return { ok: false, code: 'failed' }
|
|
564
|
+
}
|
|
565
|
+
const set = []
|
|
566
|
+
for (const entry of entries) {
|
|
567
|
+
const source = isObject(entry) ? entry.source : null
|
|
568
|
+
const id = isObject(entry) ? entry.id : null
|
|
569
|
+
const mark = isObject(entry) ? entry.mark : null
|
|
570
|
+
if (typeof source !== 'string' || !source || codePointLength(source) > MAX_SOURCE_CHARS
|
|
571
|
+
|| typeof id !== 'string' || !id || codePointLength(id) > MAX_ID_CHARS || !CORE_MARKS.includes(mark)) {
|
|
572
|
+
warn('memory core-mark not sent: an entry has no source, id or valid mark')
|
|
573
|
+
return { ok: false, code: 'failed' }
|
|
574
|
+
}
|
|
575
|
+
set.push({ source, id, mark })
|
|
576
|
+
}
|
|
577
|
+
const [data, , status, code] = await request('POST', '/v1/standing/core', { speaker, set }, timeoutMs, 'core-mark', signal, true)
|
|
578
|
+
if (status === 404 && code === 'unknown_memory') return { ok: false, code: 'unknown' }
|
|
579
|
+
if (status === 403 || status === 404 || status === 405) return { ok: false, code: 'unsupported' }
|
|
580
|
+
if (!data || data.status !== 'ok' || !Number.isInteger(data.changed) || data.changed < 0) return { ok: false, code: 'failed' }
|
|
581
|
+
return { ok: true, changed: data.changed }
|
|
582
|
+
} catch {
|
|
583
|
+
return { ok: false, code: 'failed' }
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// POST /v1/memories/list: the owner pages through, or searches, the stored memories (the client needs the gateway's
|
|
588
|
+
// `manage`). { ok: true, memories: [{ source, id, text, length, cut, date?, fields?, cues? }], total, offset,
|
|
589
|
+
// stores: [{ source, count, listable, cues }] } or { ok: false, code }: 'unsupported' when the gateway answers 403, 404
|
|
590
|
+
// or 405 (an older gateway, no `manage`, or a speaker that is not the owner), 'failed' for anything else, a request the
|
|
591
|
+
// gateway would refuse included (nothing is sent then). Only the keys that were given go out: `query` is trimmed and an
|
|
592
|
+
// empty one is no key at all.
|
|
593
|
+
async function listMemories({ speaker, source, query, offset, limit, timeoutMs = 7000, signal } = {}) {
|
|
594
|
+
try {
|
|
595
|
+
if (!validSpeaker(speaker)) return notSent('list', 'bad speaker')
|
|
596
|
+
const body = { speaker }
|
|
597
|
+
if (source !== undefined && source !== null) {
|
|
598
|
+
if (typeof source !== 'string' || !source || codePointLength(source) > MAX_SOURCE_CHARS) return notSent('list', 'bad source')
|
|
599
|
+
body.source = source
|
|
600
|
+
}
|
|
601
|
+
if (query !== undefined && query !== null) {
|
|
602
|
+
if (typeof query !== 'string') return notSent('list', 'bad query')
|
|
603
|
+
const text = pyStrip(query)
|
|
604
|
+
if (codePointLength(text) > MAX_LIST_QUERY_CHARS) return notSent('list', 'bad query')
|
|
605
|
+
if (text) body.query = text
|
|
606
|
+
}
|
|
607
|
+
if (offset !== undefined && offset !== null) {
|
|
608
|
+
if (!Number.isInteger(offset) || offset < 0 || offset > MAX_LIST_OFFSET) return notSent('list', 'bad offset')
|
|
609
|
+
body.offset = offset
|
|
610
|
+
}
|
|
611
|
+
if (limit !== undefined && limit !== null) {
|
|
612
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > MAX_LIST_LIMIT) return notSent('list', 'bad limit')
|
|
613
|
+
body.limit = limit
|
|
614
|
+
}
|
|
615
|
+
const [data, , status] = await request('POST', '/v1/memories/list', body, timeoutMs, 'list', signal, true)
|
|
616
|
+
if (status === 403 || status === 404 || status === 405) return { ok: false, code: 'unsupported' }
|
|
617
|
+
if (!data || !Array.isArray(data.memories) || !Array.isArray(data.stores)) return { ok: false, code: 'failed' }
|
|
618
|
+
if (!Number.isInteger(data.total) || data.total < 0 || !Number.isInteger(data.offset) || data.offset < 0) return { ok: false, code: 'failed' }
|
|
619
|
+
const memories = []
|
|
620
|
+
for (const item of data.memories) {
|
|
621
|
+
const clean = cleanMemory(item)
|
|
622
|
+
if (clean) memories.push({ ...clean, cut: isObject(item) && item.cut === true })
|
|
623
|
+
}
|
|
624
|
+
return { ok: true, memories, total: data.total, offset: data.offset, stores: data.stores.map(cleanStore).filter((s) => s !== null) }
|
|
625
|
+
} catch {
|
|
626
|
+
return { ok: false, code: 'failed' }
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// POST /v1/memories/get: one memory whole. { ok: true, memory: { source, id, text, length, date?, fields?, cues? } } or
|
|
631
|
+
// { ok: false, code }: 'unknown' (the gateway's 404 unknown_memory: not there, or not visible), 'unsupported' (403, 404
|
|
632
|
+
// or 405 of the route), 'failed'. A source or id the gateway would refuse is not sent.
|
|
633
|
+
async function getMemory({ speaker, source, id, timeoutMs = 7000, signal } = {}) {
|
|
634
|
+
try {
|
|
635
|
+
if (!validSpeaker(speaker)) return notSent('get', 'bad speaker')
|
|
636
|
+
if (!validAddress(source, id)) return notSent('get', 'bad source or id')
|
|
637
|
+
const [data, , status, code] = await request('POST', '/v1/memories/get', { speaker, source, id }, timeoutMs, 'get', signal, true)
|
|
638
|
+
if (status === 404 && code === 'unknown_memory') return { ok: false, code: 'unknown' }
|
|
639
|
+
if (status === 403 || status === 404 || status === 405) return { ok: false, code: 'unsupported' }
|
|
640
|
+
const memory = data ? cleanMemory(data.memory) : null
|
|
641
|
+
return memory ? { ok: true, memory } : { ok: false, code: 'failed' }
|
|
642
|
+
} catch {
|
|
643
|
+
return { ok: false, code: 'failed' }
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// POST /v1/memories/cues: replaces the recall cues of one memory; an empty list removes them. { ok: true, cues } (what is
|
|
648
|
+
// stored now) or { ok: false, code }: 'unknown' (404 unknown_memory), 'cannot' (the gateway's 409 unsupported: a store
|
|
649
|
+
// that cannot keep cues), 'unsupported' (403, 404 or 405 of the route), 'failed'. A list the gateway would refuse is not
|
|
650
|
+
// sent, and neither is one with a cue that cleanCues would drop: leaving it out quietly could erase cues the caller meant
|
|
651
|
+
// to keep (a list of unusable cues must never turn into the empty list).
|
|
652
|
+
async function setCues({ speaker, source, id, cues, timeoutMs = 12000, signal } = {}) {
|
|
653
|
+
try {
|
|
654
|
+
if (!validSpeaker(speaker)) return notSent('cues', 'bad speaker')
|
|
655
|
+
if (!validAddress(source, id)) return notSent('cues', 'bad source or id')
|
|
656
|
+
if (!Array.isArray(cues) || cues.length > MAX_CUES || cues.some((cue) => cleanCues([cue]).length !== 1)) return notSent('cues', 'bad cues')
|
|
657
|
+
const [data, , status, code] = await request('POST', '/v1/memories/cues', { speaker, source, id, cues: cleanCues(cues) }, timeoutMs, 'cues', signal, true)
|
|
658
|
+
if (status === 404 && code === 'unknown_memory') return { ok: false, code: 'unknown' }
|
|
659
|
+
if (status === 409 && code === 'unsupported') return { ok: false, code: 'cannot' }
|
|
660
|
+
if (status === 403 || status === 404 || status === 405) return { ok: false, code: 'unsupported' }
|
|
661
|
+
if (!data || data.status !== 'ok' || !Array.isArray(data.cues)) return { ok: false, code: 'failed' }
|
|
662
|
+
return { ok: true, cues: cleanCues(data.cues) }
|
|
663
|
+
} catch {
|
|
664
|
+
return { ok: false, code: 'failed' }
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// POST /v1/memories/replaced: marks one memory as replaced by another of the same store (`by`: its address), or takes the
|
|
669
|
+
// mark off (`by: null`). { ok: true, by } (the mark now stored: an address or null) or { ok: false, code }: 'unknown' (404
|
|
670
|
+
// unknown_memory), 'cannot' (the gateway's 409 unsupported: a store that keeps no fields), 'unsupported' (403, 404 or 405
|
|
671
|
+
// of the route), 'failed'. A request the gateway would refuse is not sent. The Memories tab only ever takes a mark off.
|
|
672
|
+
async function setReplaced({ speaker, source, id, by, timeoutMs = 12000, signal } = {}) {
|
|
673
|
+
try {
|
|
674
|
+
if (!validSpeaker(speaker)) return notSent('replaced', 'bad speaker')
|
|
675
|
+
if (!validAddress(source, id)) return notSent('replaced', 'bad source or id')
|
|
676
|
+
if (by !== null && (typeof by !== 'string' || !by || codePointLength(by) > MAX_ID_CHARS || by === id)) return notSent('replaced', 'bad by')
|
|
677
|
+
const [data, , status, code] = await request('POST', '/v1/memories/replaced', { speaker, source, id, by }, timeoutMs, 'replaced', signal, true)
|
|
678
|
+
if (status === 404 && code === 'unknown_memory') return { ok: false, code: 'unknown' }
|
|
679
|
+
if (status === 409 && code === 'unsupported') return { ok: false, code: 'cannot' }
|
|
680
|
+
if (status === 403 || status === 404 || status === 405) return { ok: false, code: 'unsupported' }
|
|
681
|
+
if (!data || data.status !== 'ok' || !(data.superseded_by === null || typeof data.superseded_by === 'string')) return { ok: false, code: 'failed' }
|
|
682
|
+
return { ok: true, by: data.superseded_by }
|
|
683
|
+
} catch {
|
|
684
|
+
return { ok: false, code: 'failed' }
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
289
688
|
// GET /healthz needs no token and none is sent. null when the gateway does not answer 200.
|
|
290
689
|
async function health({ deep = false, timeoutMs = 2000, signal } = {}) {
|
|
291
690
|
try {
|
|
@@ -296,5 +695,5 @@ export function createClient({ url, token, fetch: fetchImpl = globalThis.fetch,
|
|
|
296
695
|
}
|
|
297
696
|
}
|
|
298
697
|
|
|
299
|
-
return { configured, recallDetail, rememberStatus, rememberMessages, reportUsed, health }
|
|
698
|
+
return { configured, recallDetail, explainRecall, rememberStatus, rememberMessages, reportUsed, standing, markCore, listMemories, getMemory, setCues, setReplaced, health }
|
|
300
699
|
}
|