whale-memory-core 0.2.0 → 0.2.1
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/lib/self-experience.js +27 -9
- package/package.json +1 -1
package/lib/self-experience.js
CHANGED
|
@@ -34,6 +34,15 @@ function writeAll(dir, rows) {
|
|
|
34
34
|
atomicWrite(dir, rows)
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// 字段上限:写的时候超了就报错,不静默截断
|
|
38
|
+
export const LIMITS = { text: 2000, scene: 500, thought: 1000, unfinished: 300, feelings: 8, kind: 20 }
|
|
39
|
+
|
|
40
|
+
function tooLong(name, value, max) {
|
|
41
|
+
const n = String(value || '').length
|
|
42
|
+
if (n > max) throw new Error(`${name} 太长(${n}/${max} 字),删一些或拆成两条再写。`)
|
|
43
|
+
return String(value || '')
|
|
44
|
+
}
|
|
45
|
+
|
|
37
46
|
function normalize(r) {
|
|
38
47
|
if (!r) return null
|
|
39
48
|
if (r.then && typeof r.then === 'object') {
|
|
@@ -49,10 +58,10 @@ function normalize(r) {
|
|
|
49
58
|
kind: r.kind || '其它',
|
|
50
59
|
weight: WEIGHTS.has(r.weight) ? r.weight : 'normal',
|
|
51
60
|
then: {
|
|
52
|
-
text: String(r.text || '')
|
|
61
|
+
text: String(r.text || ''),
|
|
53
62
|
scene: '',
|
|
54
63
|
feeling: [],
|
|
55
|
-
thought: String(r.thought || '')
|
|
64
|
+
thought: String(r.thought || ''),
|
|
56
65
|
unfinished: '',
|
|
57
66
|
quote: null,
|
|
58
67
|
state: null
|
|
@@ -69,6 +78,15 @@ export function loadAll(dir) {
|
|
|
69
78
|
|
|
70
79
|
// 写下时的"当时":她写的 + 系统能自动带的状态(情绪/时间/场合)
|
|
71
80
|
export function addExperience(dir, { text, scene = '', feeling = [], thought = '', unfinished = '', quote = null, weight = 'normal', kind = '其它', state = null, source = 'own-time' }) {
|
|
81
|
+
if (!String(text || '').trim()) throw new Error('text 不能是空的。')
|
|
82
|
+
const safeText = tooLong('text', text, LIMITS.text)
|
|
83
|
+
const safeScene = tooLong('scene', scene, LIMITS.scene)
|
|
84
|
+
const safeThought = tooLong('thought', thought, LIMITS.thought)
|
|
85
|
+
const safeUnfinished = tooLong('unfinished', unfinished, LIMITS.unfinished)
|
|
86
|
+
const safeKind = tooLong('kind', kind || '其它', LIMITS.kind)
|
|
87
|
+
const feelings = (Array.isArray(feeling) ? feeling : [feeling]).map((f) => String(f || '').trim()).filter(Boolean)
|
|
88
|
+
if (feelings.length > LIMITS.feelings) throw new Error(`feeling 最多 ${LIMITS.feelings} 个,现在是 ${feelings.length} 个。`)
|
|
89
|
+
|
|
72
90
|
fs.mkdirSync(dir, { recursive: true })
|
|
73
91
|
const now = new Date()
|
|
74
92
|
const id = 'exp_' + now.getTime().toString(36) + Math.random().toString(36).slice(2, 6)
|
|
@@ -78,15 +96,15 @@ export function addExperience(dir, { text, scene = '', feeling = [], thought = '
|
|
|
78
96
|
day: now.toLocaleDateString('sv-SE'),
|
|
79
97
|
author: 'self',
|
|
80
98
|
source,
|
|
81
|
-
kind:
|
|
99
|
+
kind: safeKind,
|
|
82
100
|
weight: WEIGHTS.has(weight) ? weight : 'normal',
|
|
83
101
|
then: {
|
|
84
|
-
text:
|
|
85
|
-
scene:
|
|
86
|
-
feeling:
|
|
87
|
-
thought:
|
|
88
|
-
unfinished:
|
|
89
|
-
quote: quote && quote.text ? { text:
|
|
102
|
+
text: safeText,
|
|
103
|
+
scene: safeScene,
|
|
104
|
+
feeling: feelings,
|
|
105
|
+
thought: safeThought,
|
|
106
|
+
unfinished: safeUnfinished,
|
|
107
|
+
quote: quote && quote.text ? { text: tooLong('quote', quote.text, 500), source: tooLong('quote.source', quote.source || '', 120) } : null,
|
|
90
108
|
state: state && typeof state === 'object' ? { at: now.toISOString(), ...state } : { at: now.toISOString() }
|
|
91
109
|
},
|
|
92
110
|
later: [],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Self-layer autobiographical memory core: then / later, append-only. Vault, evidence and consolidation live in the QQ Agent plugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|