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
@@ -0,0 +1,220 @@
1
+ 'use strict'
2
+
3
+ // The React hooks that talk to the host half: useStatus (polled), useChangelog (read once) and
4
+ // useModels (polled, plus the clicks of the model list); useCopy, which talks to the clipboard;
5
+ // useSend, which posts a feedback report to the project's receiver; and useTrial, which asks for one
6
+ // recall to be explained.
7
+ // They own the timers and the "still mounted" flag; they know nothing about how the data is drawn.
8
+
9
+ const { useEffect, useRef, useState } = require('./react.cjs')
10
+ const { STATUS_POLL_MS, MODELS_POLL_MS, MODELS_BUSY_POLL_MS, SAVED_NOTE_MS } = require('./constants.cjs')
11
+ const { copyText } = require('./clipboard.cjs')
12
+ const { fetchStatus, fetchChangelog, fetchModels, postModels, sendFeedback } = require('./net.cjs')
13
+
14
+ /** The changelog, read once when the page mounts: { loading, data }; data stays null when the read failed. */
15
+ function useChangelog(fetchImpl) {
16
+ const [state, setState] = useState({ loading: true, data: null })
17
+ useEffect(() => {
18
+ let alive = true
19
+ fetchChangelog(fetchImpl).then(
20
+ (data) => { if (alive) setState({ loading: false, data }) },
21
+ () => { if (alive) setState({ loading: false, data: null }) },
22
+ )
23
+ return () => { alive = false }
24
+ }, [])
25
+ return state
26
+ }
27
+
28
+ /**
29
+ * The status, read on mount and every STATUS_POLL_MS: { loading, data, error, refresh, refreshSoon }.
30
+ * A failed read keeps the last data and only sets `error` (a code such as 'HTTP 403', or 'network').
31
+ */
32
+ function useStatus(fetchImpl) {
33
+ const [state, setState] = useState({ loading: true, data: null, error: null })
34
+ const alive = useRef(true)
35
+ const load = useRef(null)
36
+ load.current = () => fetchStatus(fetchImpl).then(
37
+ (data) => { if (alive.current) setState({ loading: false, data, error: null }) },
38
+ (error) => { if (alive.current) setState((previous) => ({ loading: false, data: previous.data, error: (error && error.code) || 'network' })) },
39
+ )
40
+ useEffect(() => {
41
+ alive.current = true
42
+ load.current()
43
+ const timer = setInterval(() => load.current(), STATUS_POLL_MS)
44
+ return () => {
45
+ alive.current = false
46
+ clearInterval(timer)
47
+ }
48
+ }, [])
49
+ return Object.assign({}, state, {
50
+ refresh: () => load.current(),
51
+ // The gateway restarts after most changes: read again once it has had a moment.
52
+ refreshSoon: () => setTimeout(() => { if (alive.current) load.current() }, 1500),
53
+ })
54
+ }
55
+
56
+ /**
57
+ * The local model list and its clicks: { view, error, pending, refusal, reload, send }.
58
+ * view what GET feiyu-memgate/models last answered (src/setup.js view()), or null
59
+ * error the code of the last failed read or click, or null; a failure keeps the last view
60
+ * pending a click is on its way
61
+ * refusal the problem the host half gave for the last click it refused, or null
62
+ * send post one click ({ action, id?, ... }); the answer carries the new view
63
+ * The list is read more often while a download or an installation runs. `onDone` is called once when
64
+ * a setup reaches its end, because the retrieval tier follows a model that became active.
65
+ */
66
+ function useModels(fetchImpl, onDone) {
67
+ const [state, setState] = useState({ view: null, error: null })
68
+ const [pending, setPending] = useState(false)
69
+ const [refusal, setRefusal] = useState(null)
70
+ const alive = useRef(true)
71
+ const load = useRef(null)
72
+ const lastPhase = useRef(null)
73
+ load.current = () => fetchModels(fetchImpl).then(
74
+ (view) => { if (alive.current) setState({ view, error: null }) },
75
+ (error) => { if (alive.current) setState((previous) => ({ view: previous.view, error: (error && error.code) || 'network' })) },
76
+ )
77
+ const busy = Boolean(state.view && state.view.busy)
78
+ useEffect(() => {
79
+ alive.current = true
80
+ load.current()
81
+ return () => { alive.current = false }
82
+ }, [])
83
+ useEffect(() => {
84
+ const timer = setInterval(() => load.current(), busy ? MODELS_BUSY_POLL_MS : MODELS_POLL_MS)
85
+ return () => clearInterval(timer)
86
+ }, [busy])
87
+ const phase = state.view && state.view.chain ? state.view.chain.phase : null
88
+ useEffect(() => {
89
+ if (phase === 'done' && lastPhase.current && lastPhase.current !== 'done' && onDone) onDone()
90
+ lastPhase.current = phase
91
+ }, [phase])
92
+ const send = (body) => {
93
+ setPending(true)
94
+ setRefusal(null)
95
+ postModels(fetchImpl, body).then((answer) => {
96
+ if (!alive.current) return
97
+ setPending(false)
98
+ if (answer && answer.view) setState({ view: answer.view, error: null })
99
+ if (answer && answer.result && !answer.result.ok && answer.result.problem) setRefusal(answer.result.problem)
100
+ }, (error) => {
101
+ if (!alive.current) return
102
+ setPending(false)
103
+ setState((previous) => ({ view: previous.view, error: (error && error.code) || 'network' }))
104
+ })
105
+ }
106
+ return { view: state.view, error: state.error, pending, refusal, reload: () => load.current(), send }
107
+ }
108
+
109
+ /**
110
+ * Copying to the clipboard, with what became of the last copy: { state, copy }.
111
+ * state null | 'copied' | 'failed'; goes back to null by itself after SAVED_NOTE_MS
112
+ * copy put a text on the clipboard; returns the promise of whether it arrived
113
+ * @param {(text: string) => Promise<boolean>} [copyImpl] replaces the clipboard in tests
114
+ */
115
+ function useCopy(copyImpl) {
116
+ const [state, setState] = useState(null)
117
+ const alive = useRef(true)
118
+ useEffect(() => {
119
+ alive.current = true
120
+ return () => { alive.current = false }
121
+ }, [])
122
+ useEffect(() => {
123
+ if (state === null) return undefined
124
+ const timer = setTimeout(() => setState(null), SAVED_NOTE_MS)
125
+ return () => clearTimeout(timer)
126
+ }, [state])
127
+ const copy = (text) => Promise.resolve((copyImpl || copyText)(text)).then(
128
+ (copied) => {
129
+ if (alive.current) setState(copied ? 'copied' : 'failed')
130
+ return Boolean(copied)
131
+ },
132
+ () => {
133
+ if (alive.current) setState('failed')
134
+ return false
135
+ },
136
+ )
137
+ return { state, copy }
138
+ }
139
+
140
+ /**
141
+ * Sending a feedback report, with what became of the last one: { phase, code, send }.
142
+ * phase 'ready' | 'sending' | 'sent' | 'failed'
143
+ * code why the last one failed ('HTTP 429', 'timeout', 'network', ...), or null
144
+ * send post a report (model/feedback.cjs payload()); returns the promise of whether it was taken.
145
+ * A second call while one is on its way does nothing and answers false.
146
+ * @param {string} endpoint the receiver's address
147
+ * @param {Function} [fetchImpl] replaces the browser's fetch in tests and in the preview
148
+ */
149
+ function useSend(endpoint, fetchImpl) {
150
+ const [state, setState] = useState({ phase: 'ready', code: null })
151
+ const alive = useRef(true)
152
+ const busy = useRef(false)
153
+ useEffect(() => {
154
+ alive.current = true
155
+ return () => { alive.current = false }
156
+ }, [])
157
+ const send = (report) => {
158
+ if (busy.current || !endpoint) return Promise.resolve(false)
159
+ busy.current = true
160
+ setState({ phase: 'sending', code: null })
161
+ return sendFeedback(fetchImpl || ((...args) => fetch(...args)), endpoint, report).then(
162
+ () => {
163
+ busy.current = false
164
+ if (alive.current) setState({ phase: 'sent', code: null })
165
+ return true
166
+ },
167
+ (error) => {
168
+ busy.current = false
169
+ if (alive.current) setState({ phase: 'failed', code: (error && error.code) || 'network' })
170
+ return false
171
+ },
172
+ )
173
+ }
174
+ return { phase: state.phase, code: state.code, send }
175
+ }
176
+
177
+ /**
178
+ * One trial recall at a time, with what became of the last: { state, answer, code, start }.
179
+ * state 'idle' | 'busy' | 'done' | 'failed'
180
+ * answer the host half's answer while `state` is 'done'
181
+ * code why it failed ('unavailable', 'unsupported', 'no-recall', 'failed', 'network'), or null
182
+ * start try a sentence; a second call while one is on its way does nothing
183
+ * What became of a trial is also handed to `remember`, so that the page can keep it while a person
184
+ * looks at another tab; `saved` is what it kept.
185
+ * @param {(query: string) => Promise<object>} run asks the host half (the panel passes net.cjs tryRecall)
186
+ * @param {{ state: string, answer: object | null, code: string | null }} [saved]
187
+ * @param {(result: { state: string, answer: object | null, code: string | null }) => void} [remember]
188
+ */
189
+ function useTrial(run, saved, remember) {
190
+ const [result, setResult] = useState(saved || { state: 'idle', answer: null, code: null })
191
+ const busy = useRef(false)
192
+ const start = (query) => {
193
+ if (busy.current) return Promise.resolve(false)
194
+ busy.current = true
195
+ setResult({ state: 'busy', answer: null, code: null })
196
+ const finish = (next) => {
197
+ busy.current = false
198
+ setResult(next)
199
+ if (remember) remember(next)
200
+ return next.state === 'done'
201
+ }
202
+ let asked
203
+ try {
204
+ asked = Promise.resolve(run(query))
205
+ } catch (error) {
206
+ asked = Promise.reject(error)
207
+ }
208
+ return asked.then(
209
+ (answer) => {
210
+ if (answer && answer.ok === true) return finish({ state: 'done', answer, code: null })
211
+ return finish({ state: 'failed', answer: null, code: answer && typeof answer.code === 'string' ? answer.code : 'failed' })
212
+ },
213
+ // A refusal of the route reads as a failed lookup; a call that did not arrive as a network error.
214
+ (error) => finish({ state: 'failed', answer: null, code: error && typeof error.code === 'string' && error.code.indexOf('HTTP') === 0 ? 'failed' : 'network' }),
215
+ )
216
+ }
217
+ return { state: result.state, answer: result.answer, code: result.code, start }
218
+ }
219
+
220
+ module.exports = { useChangelog, useStatus, useModels, useCopy, useSend, useTrial }