dsh-memu-memory 1.2.7 → 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.
- package/AGENTS.md +1 -1
- package/README.md +10 -15
- package/README.zh.md +10 -15
- package/docs/memory-semantics.md +23 -3
- package/lib/backup.js +1 -1
- package/lib/budget.js +31 -0
- package/lib/commitment.js +138 -0
- package/lib/consolidation-receipt.js +2 -0
- package/lib/consolidation-worker.js +3 -0
- package/lib/freshness.js +24 -0
- package/lib/inject.js +117 -10
- package/lib/lexicon.js +1 -1
- package/lib/local-vector-service.js +4 -1
- package/lib/memory.js +565 -32
- package/lib/memu.js +4 -0
- package/lib/private.js +5 -3
- package/lib/prompts/private-consolidation.mixed.md +2 -2
- package/lib/prompts/private-consolidation.rp.md +6 -1
- package/lib/prompts/private-policy.rp.md +4 -0
- package/lib/prompts/system-guide.rp.md +3 -0
- package/lib/recall-cache.js +10 -2
- package/lib/rpc.js +9 -2
- package/lib/snippet.js +25 -0
- package/lib/temporal.js +329 -0
- package/lib/text-retrieval.js +71 -0
- package/lib/tools.js +81 -4
- package/package.json +1 -1
- package/scripts/bench-recall.mjs +2 -2
- package/scripts/check-seed-compat.mjs +1 -1
- package/tests/backup.test.mjs +2 -2
- package/tests/commitment.test.mjs +144 -0
- package/tests/consolidation-fork.test.mjs +3 -3
- package/tests/disclosure.test.mjs +34 -17
- package/tests/fact-version.test.mjs +128 -0
- package/tests/help-page.test.mjs +1 -1
- package/tests/inject-conflict.test.mjs +94 -0
- package/tests/inject-guide.test.mjs +5 -3
- package/tests/inject.test.mjs +2 -2
- package/tests/injection-intent-budget.test.mjs +92 -0
- package/tests/lexicon-frequency.test.mjs +1 -1
- package/tests/memory-chain.test.mjs +140 -0
- package/tests/memory-fields.test.mjs +134 -0
- package/tests/memu-resident.test.mjs +33 -0
- package/tests/prompt-semantics.test.mjs +51 -0
- package/tests/query-decompose.test.mjs +62 -0
- package/tests/recall-cache.test.mjs +4 -2
- package/tests/recall-intent.test.mjs +100 -0
- package/tests/speaker-provenance.test.mjs +120 -0
- package/tests/temporal.test.mjs +124 -0
|
@@ -34,7 +34,7 @@ const hooks = registerHooks({
|
|
|
34
34
|
},
|
|
35
35
|
})
|
|
36
36
|
|
|
37
|
-
const { createRecallCache, recallCache, recallCacheKey } = await import('../lib/recall-cache.js')
|
|
37
|
+
const { createRecallCache, recallCache, recallCacheKey, recallCacheStats } = await import('../lib/recall-cache.js')
|
|
38
38
|
const state = await import('../lib/state.js')
|
|
39
39
|
const vp = await import('../lib/vector-profile.js')
|
|
40
40
|
const paths = await import('../lib/paths.js')
|
|
@@ -65,6 +65,7 @@ test('TTL 过期:注入假时钟,到点即失效并计逐出', () => {
|
|
|
65
65
|
now = 1_100
|
|
66
66
|
assert.equal(cache.get('k'), null, 'now >= 存入时刻+ttl 即过期')
|
|
67
67
|
assert.equal(cache.stats().evictions, 1, '过期即逐出')
|
|
68
|
+
assert.equal(cache.stats().staleSkipped, 1, '过期同时计为 staleSkipped')
|
|
68
69
|
assert.equal(cache.stats().misses, 1)
|
|
69
70
|
})
|
|
70
71
|
|
|
@@ -177,7 +178,8 @@ test('模块级单例存在且具备全部公开方法', () => {
|
|
|
177
178
|
assert.equal(typeof recallCache[method], 'function', method)
|
|
178
179
|
}
|
|
179
180
|
assert.deepEqual(Object.keys(recallCache.stats()).sort(),
|
|
180
|
-
['errors', 'evictions', 'generations', 'hits', 'inflight', 'lastError', 'misses', 'size'])
|
|
181
|
+
['errors', 'evictions', 'generations', 'hits', 'inflight', 'lastError', 'misses', 'size', 'staleSkipped'])
|
|
182
|
+
assert.deepEqual(recallCacheStats(), recallCache.stats(), 'the stats reader is a snapshot of the singleton')
|
|
181
183
|
})
|
|
182
184
|
|
|
183
185
|
// ---------------------------------------------------------------- 接线部分
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-recall-intent-'))
|
|
8
|
+
process.env.DSH_MEMU_HOME = home
|
|
9
|
+
|
|
10
|
+
const state = await import('../lib/state.js')
|
|
11
|
+
const memory = await import('../lib/memory.js')
|
|
12
|
+
|
|
13
|
+
test.after(() => {
|
|
14
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
let seq = 0
|
|
18
|
+
function session(name) {
|
|
19
|
+
seq += 1
|
|
20
|
+
const cwd = path.join(home, 'ws', `${name}-${seq}`)
|
|
21
|
+
const sessionId = `${name}-${seq}`
|
|
22
|
+
state.ensureSession(sessionId, { cwd, agentPreset: 'rp' })
|
|
23
|
+
return {
|
|
24
|
+
sessionId,
|
|
25
|
+
exec: { agent: { session: { header: { id: sessionId, cwd, agentPreset: 'rp' } } } },
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
test('a date question raises the candidate count; a general question keeps the requested topK', async () => {
|
|
30
|
+
const { exec } = session('intent-topk')
|
|
31
|
+
const topics = ['灯光', '投影仪', '白板', '咖啡机', '门禁', '椅子', '窗帘', '空调', '插座', '网络', '打印纸', '绿植']
|
|
32
|
+
for (const [index, topic] of topics.entries()) {
|
|
33
|
+
await memory.privateRemember({
|
|
34
|
+
exec,
|
|
35
|
+
title: `会议记录 ${index + 1}`,
|
|
36
|
+
content: `会议室第 ${index + 1} 项:${topic}的情况需要跟进。`,
|
|
37
|
+
absoluteDate: `2026-11-${String(index + 1).padStart(2, '0')}`,
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
const general = await memory.privateRecall({ exec, query: '会议', topK: 3, relevanceThreshold: 0 })
|
|
41
|
+
assert.equal(general.files.length, 3, 'general respects the requested three')
|
|
42
|
+
|
|
43
|
+
const dated = await memory.privateRecall({ exec, query: '会议什么时候开', topK: 3, relevanceThreshold: 0 })
|
|
44
|
+
assert.ok(dated.files.length > general.files.length, 'a date question asks for a bigger timeline')
|
|
45
|
+
assert.ok(dated.files.length <= 8)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('one-hop expansion follows an explicit link to a memory the query never matched', async () => {
|
|
49
|
+
const { exec, sessionId } = session('intent-multihop')
|
|
50
|
+
const a = await memory.privateRemember({
|
|
51
|
+
exec, title: '林眠去了码头', content: '林眠傍晚一个人去了码头。',
|
|
52
|
+
})
|
|
53
|
+
const b = await memory.privateRemember({
|
|
54
|
+
exec, title: '她带了一把旧伞', content: '那天她带着一把旧伞。',
|
|
55
|
+
})
|
|
56
|
+
const binding = state.getSessionBinding(sessionId)
|
|
57
|
+
state.updatePrivateIndex(binding.libraryId, (index) => {
|
|
58
|
+
index.entries[a.id].links = [{ to: b.id, kind: 'event', note: 'same evening', confidence: 0.9 }]
|
|
59
|
+
index.entries[b.id].links = [{ to: a.id, kind: 'event', note: 'same evening', confidence: 0.9 }]
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const recalled = await memory.privateRecall({ exec, query: '林眠到码头', topK: 2, relevanceThreshold: 0 })
|
|
63
|
+
const ids = recalled.files.map((file) => file.name)
|
|
64
|
+
assert.ok(ids.includes(a.id), 'the direct hit is recalled')
|
|
65
|
+
assert.ok(ids.includes(b.id), 'the linked clue rides along one hop')
|
|
66
|
+
assert.deepEqual(recalled.expanded, [{ from: a.id, to: b.id, kind: 'event' }])
|
|
67
|
+
assert.ok(recalled.files.find((file) => file.name === b.id).relatedTo === a.id)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('automatic recall hides never_say and only_if_asked; boundary questions release only_if_asked', async () => {
|
|
71
|
+
const { exec } = session('intent-disclosure')
|
|
72
|
+
const open = await memory.privateRemember({
|
|
73
|
+
exec, title: '账本放在书房', content: '账本放在书房第二个抽屉。',
|
|
74
|
+
})
|
|
75
|
+
const ask = await memory.privateRemember({
|
|
76
|
+
exec, title: '账本记着一笔旧账', content: '账本里记着一笔他不愿提的旧账。', disclosure: 'only_if_asked',
|
|
77
|
+
})
|
|
78
|
+
const never = await memory.privateRemember({
|
|
79
|
+
exec, title: '账本的真正来历', content: '账本其实是他父亲留下的。', disclosure: 'never_say',
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const automatic = await memory.privateRecall({
|
|
83
|
+
exec, query: '账本', topK: 10, relevanceThreshold: 0, purpose: 'automatic',
|
|
84
|
+
})
|
|
85
|
+
const autoIds = automatic.files.map((file) => file.name)
|
|
86
|
+
assert.ok(autoIds.includes(open.id))
|
|
87
|
+
assert.ok(!autoIds.includes(ask.id), 'only_if_asked waits for a direct question')
|
|
88
|
+
assert.ok(!autoIds.includes(never.id), 'never_say never auto-surfaces')
|
|
89
|
+
|
|
90
|
+
const explicit = await memory.privateRecall({ exec, query: '账本', topK: 10, relevanceThreshold: 0 })
|
|
91
|
+
const explicitIds = explicit.files.map((file) => file.name)
|
|
92
|
+
assert.ok(explicitIds.includes(ask.id) && explicitIds.includes(never.id), 'an explicit recall may still read them')
|
|
93
|
+
|
|
94
|
+
const boundary = await memory.privateRecall({
|
|
95
|
+
exec, query: '账本的秘密是什么', topK: 10, relevanceThreshold: 0, purpose: 'automatic',
|
|
96
|
+
})
|
|
97
|
+
const boundaryIds = boundary.files.map((file) => file.name)
|
|
98
|
+
assert.ok(boundaryIds.includes(ask.id), 'a boundary question is the "asked" case')
|
|
99
|
+
assert.ok(!boundaryIds.includes(never.id), 'never_say stays out even then')
|
|
100
|
+
})
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
|
|
7
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-speaker-'))
|
|
8
|
+
process.env.DSH_MEMU_HOME = home
|
|
9
|
+
|
|
10
|
+
const state = await import('../lib/state.js')
|
|
11
|
+
const memory = await import('../lib/memory.js')
|
|
12
|
+
const inject = await import('../lib/inject.js')
|
|
13
|
+
|
|
14
|
+
test.after(() => {
|
|
15
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
let seq = 0
|
|
19
|
+
function session(name) {
|
|
20
|
+
seq += 1
|
|
21
|
+
const cwd = path.join(home, 'ws', `${name}-${seq}`)
|
|
22
|
+
const sessionId = `${name}-${seq}`
|
|
23
|
+
state.ensureSession(sessionId, { cwd, agentPreset: 'rp' })
|
|
24
|
+
return {
|
|
25
|
+
sessionId,
|
|
26
|
+
exec: { agent: { session: { header: { id: sessionId, cwd, agentPreset: 'rp' } } } },
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
test('speaker and source_kind are stored and default honestly', async () => {
|
|
31
|
+
const { exec } = session('speaker-fields')
|
|
32
|
+
const said = await memory.privateRemember({
|
|
33
|
+
exec, title: '用户提到旅行', content: '我想去 Iceland 看极光。', source: 'user_said',
|
|
34
|
+
})
|
|
35
|
+
assert.equal(said.entry.speaker_id, 'user')
|
|
36
|
+
assert.equal(said.entry.source_kind, 'user_said')
|
|
37
|
+
|
|
38
|
+
const promised = await memory.privateRemember({
|
|
39
|
+
exec, title: '助手答应查机票', content: '我明天帮你查去冰岛的机票。',
|
|
40
|
+
sourceKind: 'assistant_said', speakerId: 'assistant',
|
|
41
|
+
quoteSpan: { seq: 42, turn: 7, textHash: 'abc123' },
|
|
42
|
+
storylineId: 'iceland-trip', branchId: 'branch-a', knownBy: ['user', 'assistant'],
|
|
43
|
+
})
|
|
44
|
+
assert.equal(promised.entry.source, 'assistant_said')
|
|
45
|
+
assert.equal(promised.entry.source_kind, 'assistant_said')
|
|
46
|
+
assert.equal(promised.entry.speaker_id, 'assistant')
|
|
47
|
+
assert.deepEqual(promised.entry.quote_span, { seq: 42, turn: 7, textHash: 'abc123' })
|
|
48
|
+
assert.equal(promised.entry.storyline_id, 'iceland-trip')
|
|
49
|
+
assert.equal(promised.entry.branch_id, 'branch-a')
|
|
50
|
+
assert.deepEqual(promised.entry.known_by, ['user', 'assistant'])
|
|
51
|
+
|
|
52
|
+
const imagined = await memory.privateRemember({
|
|
53
|
+
exec, title: '想象里的雪', content: '她想象过冰岛的雪落在窗台上。', source: 'imagined',
|
|
54
|
+
})
|
|
55
|
+
assert.equal(imagined.entry.source_kind, 'imagined')
|
|
56
|
+
assert.equal(imagined.entry.speaker_id, 'unknown', 'an imagined memory cannot claim a speaker')
|
|
57
|
+
|
|
58
|
+
const contextSpeaker = await memory.privateRemember({
|
|
59
|
+
exec, title: '角色说过的话', content: '林说冰岛的冬天太长了。',
|
|
60
|
+
source: 'user_said', context: { speakerId: 'character:lin' },
|
|
61
|
+
})
|
|
62
|
+
assert.equal(contextSpeaker.entry.speaker_id, 'character:lin', 'a bound speaker identity wins over the default')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('a conflicting source/source_kind pair is refused, and so is a bad quote_span', async () => {
|
|
66
|
+
const { exec } = session('speaker-invalid')
|
|
67
|
+
await assert.rejects(
|
|
68
|
+
() => memory.privateRemember({ exec, title: '矛盾来源', content: '测试。', source: 'user_said', sourceKind: 'assistant_said' }),
|
|
69
|
+
/source and source_kind disagree/,
|
|
70
|
+
)
|
|
71
|
+
await assert.rejects(
|
|
72
|
+
() => memory.privateRemember({ exec, title: '坏 span', content: '测试。', sourceKind: 'assistant_said', quoteSpan: { seq: -1 } }),
|
|
73
|
+
/quote_span\.seq/,
|
|
74
|
+
)
|
|
75
|
+
await assert.rejects(
|
|
76
|
+
() => memory.privateRemember({ exec, title: '坏说话人', content: '测试。', sourceKind: 'user_said', speakerId: ' ' }),
|
|
77
|
+
/speaker_id/,
|
|
78
|
+
)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('a query that asks who said it filters recall by speaker', async () => {
|
|
82
|
+
const { exec } = session('speaker-filter')
|
|
83
|
+
const userSide = await memory.privateRemember({
|
|
84
|
+
exec, title: '旅行计划 A', content: '旅行要去冰岛看极光。', source: 'user_said',
|
|
85
|
+
})
|
|
86
|
+
const assistantSide = await memory.privateRemember({
|
|
87
|
+
exec, title: '旅行计划 B', content: '旅行去冰岛看极光,我来订票。', sourceKind: 'assistant_said',
|
|
88
|
+
})
|
|
89
|
+
const unknownSide = await memory.privateRemember({
|
|
90
|
+
exec, title: '旅行计划 C', content: '旅行去冰岛看极光是旧念头。',
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const mine = await memory.privateRecall({ exec, query: '我说过旅行去冰岛看极光', topK: 10, relevanceThreshold: 0 })
|
|
94
|
+
const mineIds = mine.files.map((file) => file.name)
|
|
95
|
+
assert.ok(mineIds.includes(userSide.id))
|
|
96
|
+
assert.ok(!mineIds.includes(assistantSide.id), 'the assistant statement is not returned for 我说过')
|
|
97
|
+
assert.ok(mineIds.includes(unknownSide.id), 'an entry without a speaker stays recallable')
|
|
98
|
+
|
|
99
|
+
const yours = await memory.privateRecall({ exec, query: '你说过旅行去冰岛看极光', topK: 10, relevanceThreshold: 0 })
|
|
100
|
+
const yourIds = yours.files.map((file) => file.name)
|
|
101
|
+
assert.ok(yourIds.includes(assistantSide.id))
|
|
102
|
+
assert.ok(!yourIds.includes(userSide.id), 'the user statement is not returned for 你说过')
|
|
103
|
+
|
|
104
|
+
assert.equal(memory.detectSpeakerQuery('我说过要早点睡'), 'user')
|
|
105
|
+
assert.equal(memory.detectSpeakerQuery('你说过会帮我查机票'), 'assistant')
|
|
106
|
+
assert.equal(memory.detectSpeakerQuery('他说过要早点睡'), 'third')
|
|
107
|
+
assert.equal(memory.detectSpeakerQuery('冰岛在哪里'), null)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('the injected title card names a non-default speaker', async () => {
|
|
111
|
+
const { exec, sessionId } = session('speaker-inject')
|
|
112
|
+
await memory.privateRemember({
|
|
113
|
+
exec, title: '助手答应订票', content: '助手答应明天订机票。', sourceKind: 'assistant_said',
|
|
114
|
+
})
|
|
115
|
+
const binding = state.getSessionBinding(sessionId)
|
|
116
|
+
const recall = await inject.buildRecall(binding, '订票', 1, { ...state.getSettings(), relevanceThreshold: 0, privateTopK: 5 }, exec, sessionId)
|
|
117
|
+
assert.deepEqual(recall.errors, [])
|
|
118
|
+
assert.match(recall.text, /source=assistant_said/)
|
|
119
|
+
assert.match(recall.text, /speaker=assistant/)
|
|
120
|
+
})
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
parseTemporalExpression,
|
|
6
|
+
extractTemporalExpressions,
|
|
7
|
+
resolveQueryDates,
|
|
8
|
+
parseAbsoluteDate,
|
|
9
|
+
hasTemporalHint,
|
|
10
|
+
} from '../lib/temporal.js'
|
|
11
|
+
|
|
12
|
+
// 2026-11-06 12:00 Asia/Shanghai (Friday).
|
|
13
|
+
const NOW = new Date('2026-11-06T04:00:00Z')
|
|
14
|
+
|
|
15
|
+
const parse = (text, options = {}) => parseTemporalExpression(text, { now: NOW, ...options })
|
|
16
|
+
const dateOf = (text, options = {}) => parse(text, options)?.absoluteDate ?? null
|
|
17
|
+
|
|
18
|
+
test('relative day words resolve against the scene time', () => {
|
|
19
|
+
assert.equal(dateOf('今天'), '2026-11-06')
|
|
20
|
+
assert.equal(dateOf('明天'), '2026-11-07')
|
|
21
|
+
assert.equal(dateOf('后天'), '2026-11-08')
|
|
22
|
+
assert.equal(dateOf('大后天'), '2026-11-09')
|
|
23
|
+
assert.equal(dateOf('昨天'), '2026-11-05')
|
|
24
|
+
assert.equal(dateOf('前天'), '2026-11-04')
|
|
25
|
+
assert.equal(dateOf('大前天'), '2026-11-03')
|
|
26
|
+
// A longer term wins over the shorter one inside it.
|
|
27
|
+
const daHoutian = parse('大后天')
|
|
28
|
+
assert.equal(daHoutian.sourceText, '大后天')
|
|
29
|
+
assert.equal(daHoutian.precision, 'day')
|
|
30
|
+
assert.equal(daHoutian.dateSource, 'relative')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('relative days cross month, year and leap-day boundaries', () => {
|
|
34
|
+
assert.equal(dateOf('明天', { now: new Date('2026-01-31T04:00:00Z') }), '2026-02-01')
|
|
35
|
+
assert.equal(dateOf('明天', { now: new Date('2026-12-31T04:00:00Z') }), '2027-01-01')
|
|
36
|
+
// 2028 is a leap year: Feb 28 + 1 = Feb 29, Feb 29 + 1 = Mar 1.
|
|
37
|
+
assert.equal(dateOf('明天', { now: new Date('2028-02-28T04:00:00Z') }), '2028-02-29')
|
|
38
|
+
assert.equal(dateOf('明天', { now: new Date('2028-02-29T04:00:00Z') }), '2028-03-01')
|
|
39
|
+
assert.equal(dateOf('昨天', { now: new Date('2028-03-01T04:00:00Z') }), '2028-02-29')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('week phrases use Monday-based weeks and never guess a bare weekday', () => {
|
|
43
|
+
// 2026-11-06 is a Friday; this week's Monday is 2026-11-02.
|
|
44
|
+
assert.equal(dateOf('这周三'), '2026-11-04')
|
|
45
|
+
assert.equal(dateOf('本周日'), '2026-11-08')
|
|
46
|
+
assert.equal(dateOf('下周一'), '2026-11-09')
|
|
47
|
+
assert.equal(dateOf('下周三'), '2026-11-11')
|
|
48
|
+
assert.equal(dateOf('下个星期天'), '2026-11-15')
|
|
49
|
+
assert.equal(dateOf('上周六'), '2026-10-31')
|
|
50
|
+
assert.equal(dateOf('上上礼拜一'), '2026-10-19')
|
|
51
|
+
// Bare "周三" is ambiguous between this week and next; the module must not pick one.
|
|
52
|
+
assert.equal(dateOf('周三见'), null)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('month, month-end and year boundaries resolve to a concrete day', () => {
|
|
56
|
+
assert.equal(dateOf('下个月15号'), '2026-12-15')
|
|
57
|
+
assert.equal(dateOf('本月6号'), '2026-11-06')
|
|
58
|
+
const monthEnd = parse('月底')
|
|
59
|
+
assert.equal(monthEnd.absoluteDate, '2026-11-30')
|
|
60
|
+
assert.equal(monthEnd.precision, 'month')
|
|
61
|
+
assert.equal(dateOf('下月底'), '2026-12-31')
|
|
62
|
+
const yearStart = parse('年初')
|
|
63
|
+
assert.equal(yearStart.absoluteDate, '2026-01-01')
|
|
64
|
+
assert.equal(yearStart.precision, 'year')
|
|
65
|
+
assert.equal(dateOf('年底'), '2026-12-31')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('impossible dates are refused instead of guessed', () => {
|
|
69
|
+
assert.equal(dateOf('2026-02-30'), null)
|
|
70
|
+
assert.equal(dateOf('2026-13-01'), null)
|
|
71
|
+
// "下个月30号" said in January points at February 30th, which does not exist.
|
|
72
|
+
assert.equal(dateOf('下个月30号', { now: new Date('2026-01-15T04:00:00Z') }), null)
|
|
73
|
+
assert.equal(parseAbsoluteDate('2026-02-29'), null)
|
|
74
|
+
assert.equal(parseAbsoluteDate('2028-02-29'), '2028-02-29')
|
|
75
|
+
assert.equal(dateOf('下周'), null, 'a week without a weekday is not a day')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('explicit dates win over relative words and keep their precision', () => {
|
|
79
|
+
assert.equal(dateOf('2026-11-09 见面'), '2026-11-09')
|
|
80
|
+
assert.equal(dateOf('约定在2026年12月1日'), '2026-12-01')
|
|
81
|
+
assert.equal(dateOf('昨天说好下周三'), '2026-11-05', 'the first trustworthy expression is used for the entry date')
|
|
82
|
+
const explicit = parse('2026-11-09')
|
|
83
|
+
assert.equal(explicit.dateSource, 'explicit')
|
|
84
|
+
assert.equal(explicit.precision, 'day')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('offset days and English relative words', () => {
|
|
88
|
+
assert.equal(dateOf('3天后'), '2026-11-09')
|
|
89
|
+
assert.equal(dateOf('10天前'), '2026-10-27')
|
|
90
|
+
assert.equal(dateOf('tomorrow'), '2026-11-07')
|
|
91
|
+
assert.equal(dateOf('the day after tomorrow'), '2026-11-08')
|
|
92
|
+
assert.equal(dateOf('yesterday'), '2026-11-05')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('the time zone changes what "today" means', () => {
|
|
96
|
+
// 16:30Z is already Nov 6 in Shanghai but still Nov 5 in New York.
|
|
97
|
+
const instant = new Date('2026-11-05T16:30:00Z')
|
|
98
|
+
assert.equal(dateOf('明天', { now: instant, timeZone: 'Asia/Shanghai' }), '2026-11-07')
|
|
99
|
+
assert.equal(dateOf('明天', { now: instant, timeZone: 'America/New_York' }), '2026-11-06')
|
|
100
|
+
// An unknown zone falls back instead of throwing: a bad setting must not fail a write.
|
|
101
|
+
assert.equal(dateOf('明天', { now: instant, timeZone: 'Not/AZone' }), '2026-11-07')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('all expressions can be extracted for query expansion', () => {
|
|
105
|
+
const all = extractTemporalExpressions('昨天她说下周三见面,月底再确认', { now: NOW })
|
|
106
|
+
assert.deepEqual(all.map((item) => item.sourceText), ['昨天', '下周三', '月底'])
|
|
107
|
+
assert.deepEqual(resolveQueryDates('昨天她说下周三见面,月底再确认', { now: NOW }), ['2026-11-05', '2026-11-11', '2026-11-30'])
|
|
108
|
+
assert.deepEqual(resolveQueryDates('没有日期的句子', { now: NOW }), [])
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('hasTemporalHint distinguishes a relative hint from ordinary text', () => {
|
|
112
|
+
assert.equal(hasTemporalHint('下周再说'), true)
|
|
113
|
+
assert.equal(hasTemporalHint('月底前给答复'), true)
|
|
114
|
+
assert.equal(hasTemporalHint('tomorrow morning'), true)
|
|
115
|
+
assert.equal(hasTemporalHint('窗外的雨停了'), false)
|
|
116
|
+
assert.equal(hasTemporalHint(undefined), false)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('empty and non-string input never throw', () => {
|
|
120
|
+
for (const value of [null, undefined, '', ' ', 123, {}]) {
|
|
121
|
+
assert.equal(parseTemporalExpression(value, { now: NOW }), null)
|
|
122
|
+
assert.deepEqual(extractTemporalExpressions(value, { now: NOW }), [])
|
|
123
|
+
}
|
|
124
|
+
})
|