dsh-memu-memory 1.2.6 → 1.2.8

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 (68) hide show
  1. package/AGENTS.md +1 -1
  2. package/README.md +10 -15
  3. package/README.zh.md +10 -15
  4. package/docs/contributing.md +1 -1
  5. package/docs/host-adaptation.md +1 -1
  6. package/docs/memory-semantics.md +26 -4
  7. package/docs/troubleshooting.md +3 -1
  8. package/lib/backup.js +1 -1
  9. package/lib/binding-diagnostics.js +2 -2
  10. package/lib/budget.js +31 -0
  11. package/lib/client.js +276 -28
  12. package/lib/commitment.js +138 -0
  13. package/lib/consolidation-receipt.js +2 -0
  14. package/lib/consolidation-worker.js +3 -0
  15. package/lib/curator-policy.js +98 -0
  16. package/lib/fork-model.js +312 -0
  17. package/lib/freshness.js +24 -0
  18. package/lib/inject.js +117 -10
  19. package/lib/lexicon.js +1 -1
  20. package/lib/local-vector-service.js +4 -1
  21. package/lib/memory.js +600 -43
  22. package/lib/memu.js +4 -0
  23. package/lib/private.js +49 -8
  24. package/lib/prompts/private-consolidation.mixed.md +3 -2
  25. package/lib/prompts/private-consolidation.rp.md +7 -1
  26. package/lib/prompts/private-policy.mixed.md +1 -0
  27. package/lib/prompts/private-policy.rp.md +5 -1
  28. package/lib/prompts/system-guide.rp.md +3 -0
  29. package/lib/recall-cache.js +212 -0
  30. package/lib/rpc.js +44 -6
  31. package/lib/snippet.js +25 -0
  32. package/lib/state.js +43 -1
  33. package/lib/temporal.js +329 -0
  34. package/lib/text-retrieval.js +292 -28
  35. package/lib/tokenizer-v2.js +295 -0
  36. package/lib/tools.js +81 -4
  37. package/lib/webui/standalone.css +17 -1
  38. package/package.json +1 -1
  39. package/scripts/bench-recall.mjs +142 -0
  40. package/scripts/check-seed-compat.mjs +118 -0
  41. package/tests/backup.test.mjs +2 -2
  42. package/tests/commitment.test.mjs +144 -0
  43. package/tests/consolidation-fork.test.mjs +122 -5
  44. package/tests/curator-clusters.test.mjs +7 -1
  45. package/tests/curator-pairing.test.mjs +52 -8
  46. package/tests/disclosure.test.mjs +34 -17
  47. package/tests/dynamic-rrf.test.mjs +179 -0
  48. package/tests/fact-version.test.mjs +128 -0
  49. package/tests/fork-model.test.mjs +345 -0
  50. package/tests/help-page.test.mjs +1 -1
  51. package/tests/inject-conflict.test.mjs +94 -0
  52. package/tests/inject-guide.test.mjs +5 -3
  53. package/tests/inject.test.mjs +2 -2
  54. package/tests/injection-intent-budget.test.mjs +92 -0
  55. package/tests/lexicon-frequency.test.mjs +1 -1
  56. package/tests/loader.mjs +3 -0
  57. package/tests/memory-chain.test.mjs +140 -0
  58. package/tests/memory-fields.test.mjs +134 -0
  59. package/tests/memu-resident.test.mjs +33 -0
  60. package/tests/negation-polarity.test.mjs +135 -0
  61. package/tests/prompt-semantics.test.mjs +51 -0
  62. package/tests/query-decompose.test.mjs +62 -0
  63. package/tests/recall-cache.test.mjs +292 -0
  64. package/tests/recall-intent.test.mjs +100 -0
  65. package/tests/speaker-provenance.test.mjs +120 -0
  66. package/tests/stubs/dsh-llm-pi-ai.js +13 -0
  67. package/tests/temporal.test.mjs +124 -0
  68. package/tests/tokenizer-v2.test.mjs +142 -0
@@ -0,0 +1,329 @@
1
+ // 相对日期 → 绝对日期。
2
+ //
3
+ // 相对日期不能靠模型心算。写入时按这一轮的逻辑时间(sceneTime)解析,
4
+ // 召回时也先把查询里的相对日期解析成绝对日期再拼检索词。这是个纯函数模块:
5
+ // 不做 IO、不读系统时钟(除非调用方不传 now),失败返回 null,绝不猜。
6
+
7
+ const DAY_MS = 86_400_000
8
+ const DEFAULT_TIME_ZONE = 'Asia/Shanghai'
9
+ const YEAR_MIN = 1900
10
+ const YEAR_MAX = 2999
11
+
12
+ const WEEKDAY_INDEX = new Map([
13
+ ['一', 0], ['二', 1], ['三', 2], ['四', 3], ['五', 4], ['六', 5], ['日', 6], ['天', 6],
14
+ ['1', 0], ['2', 1], ['3', 2], ['4', 3], ['5', 4], ['6', 5], ['7', 6],
15
+ ])
16
+
17
+ const RELATIVE_DAY_TERMS = [
18
+ ['大后天', 3], ['大前天', -3],
19
+ ['后天', 2], ['前天', -2],
20
+ ['明天', 1], ['昨天', -1], ['今天', 0],
21
+ ['明日', 1], ['昨日', -1], ['今日', 0],
22
+ ].sort((a, b) => b[0].length - a[0].length)
23
+
24
+ const EN_RELATIVE_DAYS = new Map([
25
+ ['today', 0], ['tomorrow', 1], ['yesterday', -1],
26
+ ['the day after tomorrow', 2], ['the day before yesterday', -2],
27
+ ])
28
+
29
+ const formatterCache = new Map()
30
+
31
+ function pad2(value) {
32
+ return String(value).padStart(2, '0')
33
+ }
34
+
35
+ function formatDate({ y, m, d }) {
36
+ return `${String(y).padStart(4, '0')}-${pad2(m)}-${pad2(d)}`
37
+ }
38
+
39
+ function daysInMonth(y, m) {
40
+ return new Date(Date.UTC(y, m, 0)).getUTCDate()
41
+ }
42
+
43
+ function validCalendar(y, m, d) {
44
+ if (!Number.isInteger(y) || !Number.isInteger(m) || !Number.isInteger(d)) return false
45
+ if (y < YEAR_MIN || y > YEAR_MAX || m < 1 || m > 12 || d < 1) return false
46
+ return d <= daysInMonth(y, m)
47
+ }
48
+
49
+ function addDays({ y, m, d }, days) {
50
+ const at = new Date(Date.UTC(y, m - 1, d) + Number(days) * DAY_MS)
51
+ return { y: at.getUTCFullYear(), m: at.getUTCMonth() + 1, d: at.getUTCDate() }
52
+ }
53
+
54
+ function weekdayMondayIndex({ y, m, d }) {
55
+ return (new Date(Date.UTC(y, m - 1, d)).getUTCDay() + 6) % 7
56
+ }
57
+
58
+ function mondayOfWeek({ y, m, d }) {
59
+ return addDays({ y, m, d }, -weekdayMondayIndex({ y, m, d }))
60
+ }
61
+
62
+ function addMonths({ y, m }, months) {
63
+ const at = new Date(Date.UTC(y, m - 1 + Number(months), 1))
64
+ return { y: at.getUTCFullYear(), m: at.getUTCMonth() + 1 }
65
+ }
66
+
67
+ function formatterFor(timeZone) {
68
+ const key = String(timeZone || '')
69
+ if (formatterCache.has(key)) return formatterCache.get(key)
70
+ let formatter
71
+ try {
72
+ formatter = new Intl.DateTimeFormat('en-US', {
73
+ timeZone: key || DEFAULT_TIME_ZONE,
74
+ year: 'numeric', month: '2-digit', day: '2-digit',
75
+ })
76
+ } catch {
77
+ formatter = new Intl.DateTimeFormat('en-US', {
78
+ timeZone: DEFAULT_TIME_ZONE,
79
+ year: 'numeric', month: '2-digit', day: '2-digit',
80
+ })
81
+ }
82
+ formatterCache.set(key, formatter)
83
+ return formatter
84
+ }
85
+
86
+ function instantOf(now) {
87
+ if (now instanceof Date) return Number.isFinite(now.getTime()) ? now : new Date()
88
+ if (typeof now === 'number') {
89
+ const at = new Date(now)
90
+ return Number.isFinite(at.getTime()) ? at : new Date()
91
+ }
92
+ if (typeof now === 'string' && now.trim()) {
93
+ const at = new Date(now)
94
+ if (Number.isFinite(at.getTime())) return at
95
+ }
96
+ return new Date()
97
+ }
98
+
99
+ function zonedCalendar(instant, timeZone) {
100
+ const parts = formatterFor(timeZone).formatToParts(instant)
101
+ const values = {}
102
+ for (const part of parts) {
103
+ if (part.type === 'year' || part.type === 'month' || part.type === 'day') values[part.type] = Number(part.value)
104
+ }
105
+ if (!validCalendar(values.year, values.month, values.day)) {
106
+ return { y: instant.getUTCFullYear(), m: instant.getUTCMonth() + 1, d: instant.getUTCDate() }
107
+ }
108
+ return { y: values.year, m: values.month, d: values.day }
109
+ }
110
+
111
+ function candidate(index, sourceText, calendar, precision, dateSource, priority) {
112
+ if (!validCalendar(calendar.y, calendar.m, calendar.d)) return null
113
+ return {
114
+ absoluteDate: formatDate(calendar),
115
+ precision,
116
+ sourceText: String(sourceText || '').trim(),
117
+ dateSource,
118
+ index: Number(index) || 0,
119
+ priority,
120
+ calendar,
121
+ }
122
+ }
123
+
124
+ function matchesOf(re, text, build) {
125
+ const out = []
126
+ re.lastIndex = 0
127
+ let match
128
+ while ((match = re.exec(text)) !== null) {
129
+ const item = build(match)
130
+ if (item) out.push(item)
131
+ if (match[0].length === 0) re.lastIndex += 1
132
+ }
133
+ return out
134
+ }
135
+
136
+ function explicitCandidates(text, today) {
137
+ // 只认完整年月日;不带年份的 MM-DD 是周年日,不是某一天。
138
+ const re = /(?<![\d])(\d{4})\s*[-/年]\s*(\d{1,2})\s*[-/月]\s*(\d{1,2})\s*[日号]?(?![\d])/g
139
+ return matchesOf(re, text, (match) => candidate(
140
+ match.index,
141
+ match[0],
142
+ { y: Number(match[1]), m: Number(match[2]), d: Number(match[3]) },
143
+ 'day',
144
+ 'explicit',
145
+ 0,
146
+ ))
147
+ }
148
+
149
+ function relativeDayCandidates(text, today) {
150
+ const out = []
151
+ for (const [term, offset] of RELATIVE_DAY_TERMS) {
152
+ let from = 0
153
+ while (from < text.length) {
154
+ const at = text.indexOf(term, from)
155
+ if (at < 0) break
156
+ // "大后天" 里的 "后天" 不单独算一次:长度更长的词优先。
157
+ const longerBefore = at > 0 && RELATIVE_DAY_TERMS.some(([other]) => other.length > term.length
158
+ && text.slice(Math.max(0, at - (other.length - term.length)), at + term.length) === other)
159
+ const longerAt = RELATIVE_DAY_TERMS.some(([other]) => other.length > term.length
160
+ && text.slice(at, at + other.length) === other)
161
+ if (!longerBefore && !longerAt) {
162
+ const item = candidate(at, term, addDays(today, offset), 'day', 'relative', 10)
163
+ if (item) out.push(item)
164
+ }
165
+ from = at + term.length
166
+ }
167
+ }
168
+ const en = /(?<![a-z])(the day after tomorrow|the day before yesterday|today|tomorrow|yesterday)(?![a-z])/gi
169
+ for (const match of matchesOf(en, text, (item) => item)) {
170
+ const offset = EN_RELATIVE_DAYS.get(match[0].toLowerCase())
171
+ if (offset === undefined) continue
172
+ const item = candidate(match.index, match[0], addDays(today, offset), 'day', 'relative', 10)
173
+ if (item) out.push(item)
174
+ }
175
+ return out
176
+ }
177
+
178
+ function weekCandidates(text, today) {
179
+ const re = /(上上|上|下个|下|这个|这|本)\s*(?:周|星期|礼拜)\s*([一二三四五六日天1-7])/g
180
+ return matchesOf(re, text, (match) => {
181
+ const prefix = match[1]
182
+ const weekday = WEEKDAY_INDEX.get(match[2])
183
+ if (weekday === undefined) return null
184
+ let weeks = 0
185
+ if (prefix === '下' || prefix === '下个') weeks = 1
186
+ else if (prefix === '上') weeks = -1
187
+ else if (prefix === '上上') weeks = -2
188
+ const monday = mondayOfWeek(today)
189
+ return candidate(match.index, match[0], addDays(monday, weeks * 7 + weekday), 'day', 'relative', 20)
190
+ })
191
+ }
192
+
193
+ function monthDayCandidates(text, today) {
194
+ const re = /(下个?月|下月份?|本月|这个月份?)\s*(\d{1,2})\s*[号日]/g
195
+ return matchesOf(re, text, (match) => {
196
+ const day = Number(match[2])
197
+ const month = match[1].startsWith('下') ? addMonths(today, 1) : { y: today.y, m: today.m }
198
+ if (day < 1 || day > daysInMonth(month.y, month.m)) return null
199
+ return candidate(match.index, match[0], { y: month.y, m: month.m, d: day }, 'day', 'relative', 30)
200
+ })
201
+ }
202
+
203
+ function monthBoundaryCandidates(text, today) {
204
+ const re = /(下个?月|下月份?|本月|这个月份?|月)\s*(?:底|末)/g
205
+ return matchesOf(re, text, (match) => {
206
+ const prefix = match[1]
207
+ const month = prefix.startsWith('下') ? addMonths(today, 1) : { y: today.y, m: today.m }
208
+ return candidate(match.index, match[0], { y: month.y, m: month.m, d: daysInMonth(month.y, month.m) }, 'month', 'relative', 40)
209
+ })
210
+ }
211
+
212
+ function yearBoundaryCandidates(text, today) {
213
+ const out = []
214
+ for (const match of text.matchAll(/年初/g)) {
215
+ const item = candidate(match.index, match[0], { y: today.y, m: 1, d: 1 }, 'year', 'relative', 50)
216
+ if (item) out.push(item)
217
+ }
218
+ for (const match of text.matchAll(/(?:年底|年末)/g)) {
219
+ const item = candidate(match.index, match[0], { y: today.y, m: 12, d: 31 }, 'year', 'relative', 50)
220
+ if (item) out.push(item)
221
+ }
222
+ return out
223
+ }
224
+
225
+ function offsetDayCandidates(text, today) {
226
+ const out = []
227
+ const after = /(\d{1,3})\s*天\s*(?:之|以)?后/g
228
+ for (const match of text.matchAll(after)) {
229
+ const days = Number(match[1])
230
+ if (!Number.isFinite(days)) continue
231
+ const item = candidate(match.index, match[0], addDays(today, days), 'day', 'relative', 12)
232
+ if (item) out.push(item)
233
+ }
234
+ const before = /(\d{1,3})\s*天\s*(?:之|以)?前/g
235
+ for (const match of text.matchAll(before)) {
236
+ const days = Number(match[1])
237
+ if (!Number.isFinite(days)) continue
238
+ const item = candidate(match.index, match[0], addDays(today, -days), 'day', 'relative', 12)
239
+ if (item) out.push(item)
240
+ }
241
+ return out
242
+ }
243
+
244
+ function allCandidates(text, { now, timeZone } = {}) {
245
+ const normalized = String(text ?? '').normalize('NFKC')
246
+ if (!normalized.trim()) return []
247
+ const today = zonedCalendar(instantOf(now), timeZone)
248
+ const found = [
249
+ ...explicitCandidates(normalized, today),
250
+ ...relativeDayCandidates(normalized, today),
251
+ ...weekCandidates(normalized, today),
252
+ ...monthDayCandidates(normalized, today),
253
+ ...monthBoundaryCandidates(normalized, today),
254
+ ...yearBoundaryCandidates(normalized, today),
255
+ ...offsetDayCandidates(normalized, today),
256
+ ]
257
+ // 同一位置同一句只留最具体的一条(显式日期 > 相对日 > 周 > 月日 > 月底 > 年)。
258
+ const best = new Map()
259
+ for (const item of found) {
260
+ const key = `${item.index}:${item.sourceText}`
261
+ const current = best.get(key)
262
+ if (!current || item.priority < current.priority) best.set(key, item)
263
+ }
264
+ return [...best.values()].sort((a, b) => a.index - b.index || a.priority - b.priority)
265
+ }
266
+
267
+ /**
268
+ * 解析文本里最可信的一个日期表达。
269
+ * @returns {{absoluteDate: string, precision: 'day'|'month'|'year', sourceText: string,
270
+ * dateSource: 'explicit'|'relative', index: number}|null}
271
+ */
272
+ export function parseTemporalExpression(text, { now = new Date(), timeZone = DEFAULT_TIME_ZONE } = {}) {
273
+ const found = allCandidates(text, { now, timeZone })
274
+ if (found.length === 0) return null
275
+ const ordered = found.slice().sort((a, b) => a.priority - b.priority || a.index - b.index)
276
+ const chosen = ordered[0]
277
+ return {
278
+ absoluteDate: chosen.absoluteDate,
279
+ precision: chosen.precision,
280
+ sourceText: chosen.sourceText,
281
+ dateSource: chosen.dateSource,
282
+ index: chosen.index,
283
+ }
284
+ }
285
+
286
+ /** 文本里所有可解析的日期表达,按出现顺序。 */
287
+ export function extractTemporalExpressions(text, options = {}) {
288
+ return allCandidates(text, options).map((item) => ({
289
+ absoluteDate: item.absoluteDate,
290
+ precision: item.precision,
291
+ sourceText: item.sourceText,
292
+ dateSource: item.dateSource,
293
+ index: item.index,
294
+ }))
295
+ }
296
+
297
+ /** 查询侧:文本里所有绝对日期,去重后按出现顺序。 */
298
+ export function resolveQueryDates(text, options = {}) {
299
+ const dates = []
300
+ for (const item of extractTemporalExpressions(text, options)) {
301
+ if (!dates.includes(item.absoluteDate)) dates.push(item.absoluteDate)
302
+ }
303
+ return dates
304
+ }
305
+
306
+ /** 有相对时间词、但可能解析不出来(例如只说“下周”)。 */
307
+ export function hasTemporalHint(text) {
308
+ const value = String(text ?? '').normalize('NFKC')
309
+ return /大后天|大前天|后天|前天|明天|昨天|今天|今日|明日|昨日|下周|这周|本周|下周|上周|下个?月|本月|这个月|月底|月末|年初|年底|年末|\d\s*天\s*(?:之|以)?[前后]|(?<![a-z])(today|tomorrow|yesterday)(?![a-z])/i.test(value)
310
+ }
311
+
312
+ /** 严格 YYYY-MM-DD;非法日期返回 null(不猜)。 */
313
+ export function parseAbsoluteDate(value) {
314
+ const text = String(value ?? '').trim()
315
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text)
316
+ if (!match) return null
317
+ const y = Number(match[1])
318
+ const m = Number(match[2])
319
+ const d = Number(match[3])
320
+ if (!validCalendar(y, m, d)) return null
321
+ return formatDate({ y, m, d })
322
+ }
323
+
324
+ /** 某个瞬间在指定时区里的本地日期(YYYY-MM-DD)。 */
325
+ export function localDateKeyInZone(value = new Date(), timeZone = DEFAULT_TIME_ZONE) {
326
+ return formatDate(zonedCalendar(instantOf(value), timeZone))
327
+ }
328
+
329
+ export { DEFAULT_TIME_ZONE, DAY_MS }