@samuelchung2010/dshift 0.0.0-stage → 0.3.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.
- package/CHANGELOG.md +97 -0
- package/LICENSE +21 -0
- package/README.en.md +116 -0
- package/README.md +116 -3
- package/assets/dshift-readme.gif +0 -0
- package/client.js +18831 -0
- package/cordis.patch.yml +58 -0
- package/docs/architecture.md +203 -0
- package/docs/configuration.md +103 -0
- package/docs/development.md +125 -0
- package/docs/limitations.md +74 -0
- package/docs/math-boxed-qquad.png +0 -0
- package/docs/math-boxed-single.png +0 -0
- package/docs/memory-integration.md +111 -0
- package/docs/perf/baseline-before.json +4872 -0
- package/docs/perf/baseline-before.summary.json +659 -0
- package/docs/perf/fixed-after.json +2927 -0
- package/docs/perf/fixed-after.summary.json +583 -0
- package/docs/perf/long-chat-performance.md +340 -0
- package/docs/perf/merged-after.json +1953 -0
- package/docs/perf/merged-after.summary.json +382 -0
- package/docs/perf/mount-after.json +82 -0
- package/docs/perf/mount-baseline.json +82 -0
- package/docs/user-guide.md +202 -0
- package/icon.svg +13 -0
- package/index.js +16 -0
- package/lib/api.js +336 -0
- package/lib/artifact/approval-bus.js +94 -0
- package/lib/artifact/prompt.js +99 -0
- package/lib/artifact/service.js +602 -0
- package/lib/artifact/workspace.js +260 -0
- package/lib/import/convert.js +343 -0
- package/lib/import/format.js +262 -0
- package/lib/import/index.js +585 -0
- package/lib/import/media.js +101 -0
- package/lib/import/zip.js +296 -0
- package/lib/memory-store.js +629 -0
- package/lib/memory-text.js +162 -0
- package/lib/memory.js +871 -0
- package/lib/model-config.js +280 -0
- package/lib/protocol.js +593 -0
- package/lib/service.js +2102 -0
- package/lib/store.js +1011 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +49 -6
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure text helpers for long-term memory: normalization, fingerprints, a small
|
|
3
|
+
* lexical similarity used for de-duplication, category inference, and the
|
|
4
|
+
* sensitive-information check that keeps automatic extraction away from
|
|
5
|
+
* personal data. Nothing here touches disk, the network or a model, so every
|
|
6
|
+
* rule is directly testable.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Hard cap for one stored memory, in characters. */
|
|
10
|
+
export const MAX_MEMORY_CHARS = 600
|
|
11
|
+
|
|
12
|
+
/** Stored categories, in display order. */
|
|
13
|
+
export const MEMORY_CATEGORY_IDS = Object.freeze(['instruction', 'identity', 'career', 'projects', 'preferences'])
|
|
14
|
+
|
|
15
|
+
const CATEGORY_ALIASES = Object.freeze({
|
|
16
|
+
instruction: 'instruction',
|
|
17
|
+
instructions: 'instruction',
|
|
18
|
+
指令: 'instruction',
|
|
19
|
+
指示: 'instruction',
|
|
20
|
+
identity: 'identity',
|
|
21
|
+
身份: 'identity',
|
|
22
|
+
profile: 'identity',
|
|
23
|
+
career: 'career',
|
|
24
|
+
职业: 'career',
|
|
25
|
+
work: 'career',
|
|
26
|
+
project: 'projects',
|
|
27
|
+
projects: 'projects',
|
|
28
|
+
项目: 'projects',
|
|
29
|
+
preference: 'preferences',
|
|
30
|
+
preferences: 'preferences',
|
|
31
|
+
偏好: 'preferences',
|
|
32
|
+
preference_style: 'preferences',
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
/** Unknown or absent input returns undefined, never a guess. */
|
|
36
|
+
export function normalizeMemoryCategory(value) {
|
|
37
|
+
if (typeof value !== 'string') return undefined
|
|
38
|
+
return CATEGORY_ALIASES[value.trim().toLowerCase()]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One display-safe memory line: whitespace collapsed, list and date markers
|
|
43
|
+
* stripped, empty input rejected. The result is what the model sees and what
|
|
44
|
+
* the user edits, so it must never carry the export scaffolding.
|
|
45
|
+
* @param value - raw content from a user, a model reply or an import.
|
|
46
|
+
* @param limit - maximum length after normalization.
|
|
47
|
+
*/
|
|
48
|
+
export function normalizeMemoryContent(value, limit = MAX_MEMORY_CHARS) {
|
|
49
|
+
if (typeof value !== 'string') return ''
|
|
50
|
+
let text = value.replace(/\r\n?/g, '\n')
|
|
51
|
+
text = text.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, ' ')
|
|
52
|
+
text = text.replace(/\s+/g, ' ').trim()
|
|
53
|
+
// `[2024-01-02] - memory` and `[unknown] - memory` are export decorations.
|
|
54
|
+
text = text.replace(/^\[(?:\d{4}-\d{2}-\d{2}|unknown|未知)\]\s*[-–—:]?\s*/i, '')
|
|
55
|
+
text = text.replace(/^(?:[-*•·]\s+|\d+[.)、]\s*)/, '')
|
|
56
|
+
text = text.replace(/^[`"'“”‘’]+|[`"'“”‘’]+$/g, '').trim()
|
|
57
|
+
return text.slice(0, Math.max(1, limit))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Comparison key: case, punctuation and spacing differences are not distinct. */
|
|
61
|
+
export function memoryContentKey(content) {
|
|
62
|
+
return normalizeMemoryContent(content)
|
|
63
|
+
.toLowerCase()
|
|
64
|
+
.replace(/[^\p{L}\p{N}]+/gu, '')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Tokens for lexical matching. Latin words stay whole; Chinese runs become
|
|
69
|
+
* character bigrams so no dictionary or segmentation dependency is needed.
|
|
70
|
+
*/
|
|
71
|
+
export function memoryTokens(content) {
|
|
72
|
+
const value = normalizeMemoryContent(content).toLowerCase()
|
|
73
|
+
const tokens = new Set()
|
|
74
|
+
for (const word of value.match(/[a-z0-9][a-z0-9._+#-]{0,}/g) ?? []) {
|
|
75
|
+
if (word.length >= 2) tokens.add(word)
|
|
76
|
+
}
|
|
77
|
+
for (const run of value.match(/[\u3400-\u9fff]+/g) ?? []) {
|
|
78
|
+
if (run.length === 1) tokens.add(run)
|
|
79
|
+
else for (let index = 0; index + 1 < run.length; index += 1) tokens.add(run.slice(index, index + 2))
|
|
80
|
+
}
|
|
81
|
+
return tokens
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Whether a sentence carries a negation marker, used to avoid merging opposites. */
|
|
85
|
+
function hasNegation(text) {
|
|
86
|
+
return /(?:不|没|别|无|never|not|no longer|don't|doesn't|isn't|won't)/i.test(text)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function numbersIn(text) {
|
|
90
|
+
return (String(text).match(/\d+(?:\.\d+)?/g) ?? []).slice(0, 6).sort()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Similarity for de-duplication, in 0..1. Chinese has no word boundaries here,
|
|
95
|
+
* so bigram coverage of the shorter text is the main signal: one fact stated
|
|
96
|
+
* with extra details still covers the original wording. A jaccard term keeps
|
|
97
|
+
* unrelated long sentences apart, and negation/number mismatches are clamped
|
|
98
|
+
* so "likes X" and "does not like X" never merge.
|
|
99
|
+
*/
|
|
100
|
+
export function memorySimilarity(left, right) {
|
|
101
|
+
const a = memoryTokens(left)
|
|
102
|
+
const b = memoryTokens(right)
|
|
103
|
+
if (a.size === 0 || b.size === 0) return 0
|
|
104
|
+
let shared = 0
|
|
105
|
+
for (const token of a) if (b.has(token)) shared += 1
|
|
106
|
+
const coverage = shared / Math.min(a.size, b.size)
|
|
107
|
+
const jaccard = shared / (a.size + b.size - shared)
|
|
108
|
+
let score = coverage * 0.75 + jaccard * 0.25
|
|
109
|
+
if (hasNegation(left) !== hasNegation(right)) score = Math.min(score, 0.5)
|
|
110
|
+
const leftNumbers = numbersIn(left)
|
|
111
|
+
const rightNumbers = numbersIn(right)
|
|
112
|
+
if (leftNumbers.length > 0 && rightNumbers.length > 0 && leftNumbers.join() !== rightNumbers.join()) {
|
|
113
|
+
score = Math.min(score, 0.6)
|
|
114
|
+
}
|
|
115
|
+
return Math.max(0, Math.min(1, Math.round(score * 1000) / 1000))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Category inferred from wording alone, used when an import or a model reply
|
|
120
|
+
* omits one. The order matters: an explicit answer-style instruction is not
|
|
121
|
+
* just another preference.
|
|
122
|
+
*/
|
|
123
|
+
export function inferMemoryCategory(content) {
|
|
124
|
+
const text = normalizeMemoryContent(content)
|
|
125
|
+
if (text === '') return 'preferences'
|
|
126
|
+
if (/^(?:以后|今后|从今|回答|回复|解释|请|不要|别|每次|始终|总是|永远)|回答(?:时|请|要|用)|回复(?:时|请|要|用)|(?:用|使用)(?:中文|英文|english|chinese)|(?:简洁|详细|简短|具体)的?(?:回答|回复|解释)|(?:format|answer|reply|respond).{0,24}(?:concise|brief|detailed|chinese|english)|always|never|from now on/i.test(text)) {
|
|
127
|
+
return 'instruction'
|
|
128
|
+
}
|
|
129
|
+
if (/(?:我叫|我的名字|我的生日|出生于|住在|居住|现居|来自|国籍|学生|高中生|大学生|在.{1,24}(?:学校|中学|高中|大学|学院)|my name|i am|i'm|live in|born|student|school|university)/i.test(text)) {
|
|
130
|
+
return 'identity'
|
|
131
|
+
}
|
|
132
|
+
if (/(?:工作|职业|职位|岗位|公司|实习|工程师|设计师|经理|创业|自由职业|job|career|worked at|working at|role|intern|engineer|designer|founder)/i.test(text)) {
|
|
133
|
+
return 'career'
|
|
134
|
+
}
|
|
135
|
+
if (/(?:项目|插件|仓库|开发|应用|网站|游戏|模组|技术栈|架构|project|plugin|repository|repo|building|developing|codebase|website|game|stack)/i.test(text)) {
|
|
136
|
+
return 'projects'
|
|
137
|
+
}
|
|
138
|
+
if (/(?:喜欢|讨厌|偏好|习惯|爱好|兴趣|prefer|like|love|hate|dislike|favorite|habit|interest)/i.test(text)) {
|
|
139
|
+
return 'preferences'
|
|
140
|
+
}
|
|
141
|
+
return 'preferences'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const SENSITIVE_PATTERNS = [
|
|
145
|
+
/[\w.+-]+@[\w-]+\.[\w.]+/,
|
|
146
|
+
/(?<!\d)1[3-9]\d{9}(?!\d)/,
|
|
147
|
+
/(?<!\d)\d{17}[\dXx](?!\d)/,
|
|
148
|
+
/(?<!\d)(?:\d[ -]?){15,19}(?!\d)/,
|
|
149
|
+
/\+\d{1,3}[\s-]?\d{6,14}/,
|
|
150
|
+
/(?:密码|口令|身份证|银行卡|信用卡|银行账号|家庭住址|详细地址|护照号|api[\s_-]?key|password|passcode|social security|ssn|bank account|credit card|home address|passport number)/i,
|
|
151
|
+
]
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Whether content looks like personal data that automatic extraction must not
|
|
155
|
+
* save. Manual entries and explicit imports are the user's own decision; this
|
|
156
|
+
* gate only stops the assistant from volunteering such facts.
|
|
157
|
+
*/
|
|
158
|
+
export function looksSensitive(value) {
|
|
159
|
+
const text = typeof value === 'string' ? value : ''
|
|
160
|
+
if (text.trim() === '') return false
|
|
161
|
+
return SENSITIVE_PATTERNS.some(pattern => pattern.test(text))
|
|
162
|
+
}
|