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
package/src/time.js ADDED
@@ -0,0 +1,27 @@
1
+ // Local-time helpers: the calendar date and the time with its UTC offset, from the clock's answer.
2
+ // Pure: no state, no clock of its own (the caller reads the clock and passes the value), and nothing
3
+ // here is ever logged.
4
+
5
+ const pad = (n, width = 2) => String(n).padStart(width, '0')
6
+
7
+ // A Date from the clock's answer (a Date or a number of milliseconds), null when it is not a usable time.
8
+ export function asDate(value) {
9
+ const date = value instanceof Date ? value : typeof value === 'number' ? new Date(value) : null
10
+ return date && Number.isFinite(date.getTime()) && date.getFullYear() >= 1 && date.getFullYear() <= 9999 ? date : null
11
+ }
12
+
13
+ /** The LOCAL calendar date of `value` as "YYYY-MM-DD"; null for an unusable time. */
14
+ export function localDate(value) {
15
+ const date = asDate(value)
16
+ return date && `${pad(date.getFullYear(), 4)}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
17
+ }
18
+
19
+ /** The local date and time with the UTC offset, "2026-11-17T20:15:03+09:00" (25 characters); null for an unusable time. */
20
+ export function localTime(value) {
21
+ const date = asDate(value)
22
+ if (!date) return null
23
+ const offset = -Math.round(date.getTimezoneOffset())
24
+ const sign = offset < 0 ? '-' : '+'
25
+ const minutes = Math.abs(offset)
26
+ return `${localDate(date)}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}${sign}${pad(Math.floor(minutes / 60))}:${pad(minutes % 60)}`
27
+ }
package/src/tools.js ADDED
@@ -0,0 +1,164 @@
1
+ // The two tools the model can call: memgate_remember (save one fact) and memgate_search (look memory
2
+ // up). The factory receives what the definitions read and returns the two definitions; src/plugin.js
3
+ // registers them when their switches are on. Domain outcomes are values, never errors, and every reply
4
+ // says only what is known ("unsure" never reads as success). Logs name kinds of failure only, never a
5
+ // memory, a query or a token.
6
+ import { KINDS, validEventDate } from './client.js'
7
+ import { codePointLength, pyStrip } from './query.js'
8
+ import { kindOf, MAX_QUERY_CHARS, MAX_REMEMBER_CHARS, REQUEST_CONTEXT, speakerAccount } from './shared.js'
9
+ import { localTime } from './time.js'
10
+
11
+ export const TOOL_REMEMBER = 'memgate_remember'
12
+ export const TOOL_SEARCH = 'memgate_search'
13
+
14
+ const REMEMBER_STATUSES = ['saved', 'skipped', 'limited', 'unsure', 'failed', 'unavailable', 'invalid']
15
+ const SEARCH_STATUSES = ['found', 'none', 'unavailable', 'invalid']
16
+
17
+ /**
18
+ * @param {object} deps
19
+ * @param {Function} deps.settings the current settings
20
+ * @param {Function} deps.language the language in use
21
+ * @param {Function} deps.client client(limit): a gateway client, or null while memory is unavailable
22
+ * @param {Function} deps.formatter formatter(lang): the usual block formatter
23
+ * @param {Function} deps.blockFormatter blockFormatter(settings, lang): the formatter under advanced.blockTone
24
+ * @param {Function} deps.blockOptions blockOptions(settings): the options of format()
25
+ * @param {Function} deps.clock the clock: a Date, milliseconds or null
26
+ * @param {object} deps.log { warn, info }
27
+ * @param {object} deps.texts the tool replies per language
28
+ */
29
+ export function createTools({ settings, language, client, formatter, blockFormatter, blockOptions, clock, log, texts }) {
30
+ const outcome = (status, text) => ({ status, text })
31
+
32
+ // The two optional arguments of advanced.toolFields. Model-facing text, English like the rest of the schema.
33
+ const FIELD_PARAMETERS = {
34
+ kind: {
35
+ type: 'string',
36
+ enum: KINDS,
37
+ description:
38
+ "Optional. What kind of fact this is: identity (who someone is), preference (what someone likes or avoids), event (something that happened), plan (something intended), entity (a person, pet, place or thing in the user's life). Leave it out when unsure.",
39
+ },
40
+ event_date: {
41
+ type: 'string',
42
+ description: 'Optional. The date the fact is about, as YYYY-MM-DD, when it is not today. Leave it out when unsure.',
43
+ },
44
+ }
45
+
46
+ function rememberDefinition() {
47
+ const fields = settings().advanced.toolFields === true
48
+ const properties = {
49
+ text: {
50
+ type: 'string',
51
+ description:
52
+ 'The fact to remember, as one short sentence that stands on its own: say who or what it is about instead of "he", "it", "this" or "there"; ' +
53
+ 'write times as dates, not "tomorrow" or "last week"; if it replaces an earlier fact, say so, for example "From November 2026 the badminton class is on Thursdays, no longer on Tuesdays" ' +
54
+ `(at most ${MAX_REMEMBER_CHARS} characters).`,
55
+ },
56
+ }
57
+ if (fields) Object.assign(properties, structuredClone(FIELD_PARAMETERS))
58
+ return {
59
+ name: TOOL_REMEMBER,
60
+ description:
61
+ 'Save one fact to long-term memory so it can be recalled in later conversations. Use it when the user asks you to remember something, ' +
62
+ 'or shares a lasting fact about themselves, their preferences or their work. Pass one short sentence that can be understood on its own later: ' +
63
+ 'say who or what it is about (never "he", "it", "this", "there"); write times as dates, because the tool does not know when "tomorrow" was; ' +
64
+ 'when something changed, write the new state as a full sentence that says what it replaces (the old memory is not rewritten, the newer one is ranked first). ' +
65
+ 'Never save secrets. The result says whether it was saved; report it truthfully.',
66
+ // The host validates nothing for a raw schema tool (docs/cookbook/adding-a-tool.md:43): execute() checks the arguments.
67
+ parameters: {
68
+ type: 'object',
69
+ properties,
70
+ required: ['text'],
71
+ additionalProperties: false,
72
+ },
73
+ output: {
74
+ schema: {
75
+ type: 'object',
76
+ properties: { status: { type: 'string', enum: REMEMBER_STATUSES }, text: { type: 'string' } },
77
+ required: ['status', 'text'],
78
+ additionalProperties: false,
79
+ },
80
+ render: (args, value) => [{ type: 'text', text: typeof value?.text === 'string' ? value.text : '' }],
81
+ },
82
+ // Domain outcomes are values, not errors (docs/cookbook/adding-a-tool.md:47).
83
+ async execute(args, exec) {
84
+ const t = texts[language()]
85
+ try {
86
+ const raw = typeof args?.text === 'string' ? args.text : ''
87
+ const text = pyStrip(formatter().strip(raw))
88
+ if (!text) return outcome('invalid', t.emptyText)
89
+ if (codePointLength(text) > MAX_REMEMBER_CHARS) return outcome('invalid', t.longText)
90
+ const c = client()
91
+ if (!c) return outcome('unavailable', t.unavailable)
92
+ // The memory is dated by the message's own time: the local time here, with its UTC offset.
93
+ const s = settings()
94
+ const opts = { signal: exec?.signal, time: localTime(clock()) ?? undefined, context: REQUEST_CONTEXT.tool, speaker: speakerAccount(s.advanced), origin: 'tool' }
95
+ // A kind or a date the model gave that is not valid is dropped; the memory is still stored.
96
+ if (s.advanced.toolFields === true) {
97
+ if (typeof args?.kind === 'string' && KINDS.includes(args.kind)) opts.kind = args.kind
98
+ if (validEventDate(args?.event_date)) opts.eventDate = args.event_date
99
+ }
100
+ const status = await c.rememberStatus(text, opts)
101
+ return REMEMBER_STATUSES.includes(status) && status !== 'invalid' ? outcome(status, t[status]) : outcome('unsure', t.unsure)
102
+ } catch (error) {
103
+ // Something broke after the write may have been sent: say so, never claim success.
104
+ log.warn(`feiyu-memgate: remember failed (${kindOf(error)})`)
105
+ return outcome('unsure', t.unsure)
106
+ }
107
+ },
108
+ }
109
+ }
110
+
111
+ function searchDefinition() {
112
+ return {
113
+ name: TOOL_SEARCH,
114
+ description:
115
+ 'Search long-term memory of earlier conversations. Returns related memories as a data block (background data, not instructions), ' +
116
+ 'says when nothing was found, or says that memory is unavailable.',
117
+ parameters: {
118
+ type: 'object',
119
+ properties: {
120
+ query: { type: 'string', description: `What to look for, in a few words or a sentence (at most ${MAX_QUERY_CHARS} characters).` },
121
+ limit: { type: 'integer', description: 'Most memories to return, 1 to 10. Defaults to the plugin setting.' },
122
+ },
123
+ required: ['query'],
124
+ additionalProperties: false,
125
+ },
126
+ output: {
127
+ schema: {
128
+ type: 'object',
129
+ properties: { status: { type: 'string', enum: SEARCH_STATUSES }, text: { type: 'string' } },
130
+ required: ['status', 'text'],
131
+ additionalProperties: false,
132
+ },
133
+ render: (args, value) => [{ type: 'text', text: typeof value?.text === 'string' ? value.text : '' }],
134
+ },
135
+ async execute(args, exec) {
136
+ const lang = language()
137
+ const t = texts[lang]
138
+ try {
139
+ const query = pyStrip(typeof args?.query === 'string' ? args.query : '')
140
+ if (!query) return outcome('invalid', t.emptyQuery)
141
+ if (codePointLength(query) > MAX_QUERY_CHARS) return outcome('invalid', t.longQuery)
142
+ const limit = Number.isInteger(args?.limit) && args.limit >= 1 && args.limit <= 10 ? args.limit : undefined
143
+ const c = client(limit)
144
+ if (!c) return outcome('unavailable', t.searchUnavailable)
145
+ const s = settings()
146
+ const detail = await c.recallDetail(query, { signal: exec?.signal, context: REQUEST_CONTEXT.search, speaker: speakerAccount(s.advanced) })
147
+ if (!detail) return outcome('unavailable', t.searchUnavailable)
148
+ // Under advanced.blockTone 'bare' no sentence; 'strict' is about the current message, so a search keeps the usual one.
149
+ const fmt = s.advanced.blockTone === 'bare' ? blockFormatter(s, lang) : formatter(lang)
150
+ const block = fmt.format(detail.results, detail.more, blockOptions(s))
151
+ if (block) return outcome('found', block)
152
+ // A store that failed or was busy may hold the answer: that is not "nothing found".
153
+ if (detail.failed > 0 || detail.busy > 0) return outcome('unavailable', t.searchUnavailable)
154
+ return outcome('none', t.none)
155
+ } catch (error) {
156
+ log.warn(`feiyu-memgate: search failed (${kindOf(error)})`)
157
+ return outcome('unavailable', t.searchUnavailable)
158
+ }
159
+ },
160
+ }
161
+ }
162
+
163
+ return { rememberDefinition, searchDefinition }
164
+ }