dsh-plugin-memory 0.15.2 → 0.16.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/CHANGELOG.md +25 -0
- package/README.md +1 -1
- package/README.zh.md +1 -1
- package/lib/client.js +535 -253
- package/lib/graph-3d.js +159 -0
- package/lib/recall-nudge.js +33 -9
- package/lib/soul.js +2 -2
- package/lib/tone.js +2 -2
- package/package.json +1 -1
package/lib/graph-3d.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic 3D sphere placement + perspective projection for the memory
|
|
3
|
+
* graph.
|
|
4
|
+
*
|
|
5
|
+
* The client half cannot import local modules (it only gets the platform's
|
|
6
|
+
* `react` seat at runtime), so the same maths is **inlined** in `client.js` —
|
|
7
|
+
* exactly like `graph-drag`/`graph-view`. Keeping the pure implementation here
|
|
8
|
+
* lets Node tests pin the behaviour.
|
|
9
|
+
*
|
|
10
|
+
* No randomness: the same nodes always land on the same slots, and the same
|
|
11
|
+
* view always projects to the same screen coordinates. That matters because the
|
|
12
|
+
* panel re-renders every frame while the user rotates.
|
|
13
|
+
*
|
|
14
|
+
* @module dsh-plugin-memory/graph-3d
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Defaults shared by the panel and the tests. */
|
|
18
|
+
export const GRAPH3D_DEFAULTS = Object.freeze({
|
|
19
|
+
width: 560,
|
|
20
|
+
height: 400,
|
|
21
|
+
radius: 250,
|
|
22
|
+
focal: 900,
|
|
23
|
+
camera: 340,
|
|
24
|
+
zoom: 1,
|
|
25
|
+
yaw: 0,
|
|
26
|
+
pitch: 0,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5))
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Distribute nodes evenly over a sphere (Fibonacci sphere).
|
|
33
|
+
*
|
|
34
|
+
* Nodes are sorted by `type` then `id` first, so a given type forms a
|
|
35
|
+
* contiguous band on the sphere instead of being scattered — that is what makes
|
|
36
|
+
* the colours readable when the sphere rotates.
|
|
37
|
+
*
|
|
38
|
+
* @param {Array<{ id: string, type?: string }>} nodes
|
|
39
|
+
* @param {{ radius?: number }} [options]
|
|
40
|
+
* @returns {Array<{ id: string, x: number, y: number, z: number }>}
|
|
41
|
+
*/
|
|
42
|
+
export function spherePositions(nodes, options = {}) {
|
|
43
|
+
const radius = options.radius ?? GRAPH3D_DEFAULTS.radius
|
|
44
|
+
const sorted = [...nodes].sort((a, b) => {
|
|
45
|
+
const byType = String(a.type ?? '').localeCompare(String(b.type ?? ''))
|
|
46
|
+
if (byType !== 0) return byType
|
|
47
|
+
return String(a.id).localeCompare(String(b.id))
|
|
48
|
+
})
|
|
49
|
+
const count = sorted.length
|
|
50
|
+
return sorted.map((node, index) => {
|
|
51
|
+
// y walks from +1 to -1; the ring radius shrinks toward the poles.
|
|
52
|
+
const y = 1 - ((index + 0.5) * 2) / count
|
|
53
|
+
const ring = Math.sqrt(Math.max(0, 1 - y * y))
|
|
54
|
+
const angle = index * GOLDEN_ANGLE
|
|
55
|
+
return {
|
|
56
|
+
id: node.id,
|
|
57
|
+
x: Math.cos(angle) * ring * radius,
|
|
58
|
+
y: y * radius,
|
|
59
|
+
z: Math.sin(angle) * ring * radius,
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Rotate a world point by yaw (around Y) then pitch (around X). */
|
|
65
|
+
export function rotatePoint(point, yaw, pitch) {
|
|
66
|
+
const cy = Math.cos(yaw)
|
|
67
|
+
const sy = Math.sin(yaw)
|
|
68
|
+
const cp = Math.cos(pitch)
|
|
69
|
+
const sp = Math.sin(pitch)
|
|
70
|
+
// yaw around Y
|
|
71
|
+
const x1 = point.x * cy - point.z * sy
|
|
72
|
+
const z1 = point.x * sy + point.z * cy
|
|
73
|
+
// pitch around X
|
|
74
|
+
return {
|
|
75
|
+
id: point.id,
|
|
76
|
+
x: x1,
|
|
77
|
+
y: point.y * cp - z1 * sp,
|
|
78
|
+
z: point.y * sp + z1 * cp,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Inverse of {@link rotatePoint}. */
|
|
83
|
+
export function unrotatePoint(point, yaw, pitch) {
|
|
84
|
+
const cy = Math.cos(-yaw)
|
|
85
|
+
const sy = Math.sin(-yaw)
|
|
86
|
+
const cp = Math.cos(-pitch)
|
|
87
|
+
const sp = Math.sin(-pitch)
|
|
88
|
+
// undo pitch
|
|
89
|
+
const y1 = point.y * cp - point.z * sp
|
|
90
|
+
const z1 = point.y * sp + point.z * cp
|
|
91
|
+
// undo yaw
|
|
92
|
+
return {
|
|
93
|
+
id: point.id,
|
|
94
|
+
x: point.x * cy - z1 * sy,
|
|
95
|
+
y: y1,
|
|
96
|
+
z: point.x * sy + z1 * cy,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Project one world point to screen space.
|
|
102
|
+
*
|
|
103
|
+
* @param {{ id: string, x: number, y: number, z: number }} point
|
|
104
|
+
* @param {object} [view] overrides: width/height/radius/focal/camera/zoom/yaw/pitch
|
|
105
|
+
* @returns {{ id: string, x: number, y: number, z: number, scale: number, depth: number }}
|
|
106
|
+
*/
|
|
107
|
+
export function projectPoint(point, view = {}) {
|
|
108
|
+
const width = view.width ?? GRAPH3D_DEFAULTS.width
|
|
109
|
+
const height = view.height ?? GRAPH3D_DEFAULTS.height
|
|
110
|
+
const radius = view.radius ?? GRAPH3D_DEFAULTS.radius
|
|
111
|
+
const focal = view.focal ?? GRAPH3D_DEFAULTS.focal
|
|
112
|
+
const camera = view.camera ?? GRAPH3D_DEFAULTS.camera
|
|
113
|
+
const zoom = view.zoom ?? GRAPH3D_DEFAULTS.zoom
|
|
114
|
+
const rotated = rotatePoint(point, view.yaw ?? 0, view.pitch ?? 0)
|
|
115
|
+
// Perspective: nearer (smaller z) means a larger scale.
|
|
116
|
+
const scale = (focal / Math.max(60, focal + rotated.z + camera)) * zoom
|
|
117
|
+
// Depth is 1 when nearest the camera (z = -radius) and 0 at the far pole.
|
|
118
|
+
const depth = Math.max(0, Math.min(1, (radius - rotated.z) / (2 * radius)))
|
|
119
|
+
return {
|
|
120
|
+
id: point.id,
|
|
121
|
+
x: width / 2 + rotated.x * scale,
|
|
122
|
+
y: height / 2 + rotated.y * scale,
|
|
123
|
+
z: rotated.z,
|
|
124
|
+
scale,
|
|
125
|
+
depth,
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Sort projected points for painter's algorithm (farthest first by default).
|
|
131
|
+
* @param {Array<{ z: number }>} points
|
|
132
|
+
* @param {'far-first' | 'near-first'} [direction]
|
|
133
|
+
*/
|
|
134
|
+
export function depthSorted(points, direction = 'far-first') {
|
|
135
|
+
const sorted = [...points].sort((a, b) => a.z - b.z)
|
|
136
|
+
return direction === 'near-first' ? sorted.reverse() : sorted
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Return the closest projected node within its hit radius.
|
|
141
|
+
*
|
|
142
|
+
* @param {Array<{ id: string, x: number, y: number }>} points
|
|
143
|
+
* @param {number} x
|
|
144
|
+
* @param {number} y
|
|
145
|
+
* @param {(point: { id: string }) => number} [radiusOf]
|
|
146
|
+
* @returns {{ id: string, x: number, y: number } | null}
|
|
147
|
+
*/
|
|
148
|
+
export function hitTest3D(points, x, y, radiusOf = () => 8) {
|
|
149
|
+
let best = null
|
|
150
|
+
let bestDistance = Infinity
|
|
151
|
+
for (const point of points) {
|
|
152
|
+
const distance = Math.hypot(point.x - x, point.y - y)
|
|
153
|
+
if (distance <= radiusOf(point) && distance < bestDistance) {
|
|
154
|
+
best = point
|
|
155
|
+
bestDistance = distance
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return best
|
|
159
|
+
}
|
package/lib/recall-nudge.js
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import { randomUUID } from 'node:crypto'
|
|
26
|
+
import { readSoulProfile } from './soul.js'
|
|
26
27
|
import { existsSync, readFileSync } from 'node:fs'
|
|
27
28
|
import { join } from 'node:path'
|
|
28
29
|
import { parseLogEntries } from './boot.js'
|
|
@@ -70,18 +71,30 @@ export function listRecallCandidates(memoryDir, count = 3) {
|
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
/** Persona names come from SOUL; generic fallbacks keep a brand-new store honest. */
|
|
75
|
+
function personaNames(profile) {
|
|
76
|
+
const agentName = typeof profile?.agentName === 'string' && profile.agentName.trim().length > 0
|
|
77
|
+
? profile.agentName.trim()
|
|
78
|
+
: '助手'
|
|
79
|
+
const userName = typeof profile?.userName === 'string' && profile.userName.trim().length > 0
|
|
80
|
+
? profile.userName.trim()
|
|
81
|
+
: '你'
|
|
82
|
+
return { agentName, userName }
|
|
83
|
+
}
|
|
84
|
+
|
|
73
85
|
/**
|
|
74
86
|
* Build the recall message in the same followup shape DigestGuard uses
|
|
75
87
|
* (role 'user', content array, plugin source). The text is an invitation,
|
|
76
|
-
* not a command: first-person,
|
|
88
|
+
* not a command: first-person, the persona's own voice, explicitly bounded by
|
|
77
89
|
* "only what's really in memory, never invent, don't repeat yourself".
|
|
78
90
|
* @param {string[]} candidates optional seed headlines
|
|
79
91
|
* @returns {{ id: string, role: 'user', content: [{type:'text', text:string}], source: {kind:'plugin:memory'} }}
|
|
80
92
|
*/
|
|
81
|
-
export function buildRecallNudgeMessage(candidates) {
|
|
93
|
+
export function buildRecallNudgeMessage(candidates, profile) {
|
|
82
94
|
const list = Array.isArray(candidates)
|
|
83
95
|
? candidates.filter((c) => typeof c === 'string' && c.trim().length > 0)
|
|
84
96
|
: []
|
|
97
|
+
const { agentName, userName } = personaNames(profile)
|
|
85
98
|
const seed = list.length > 0
|
|
86
99
|
? `\n可作引子(挑一条顺眼的就行,不用核对是否提过):\n${list.map((c) => `- ${c}`).join('\n')}`
|
|
87
100
|
: ''
|
|
@@ -91,7 +104,7 @@ export function buildRecallNudgeMessage(candidates) {
|
|
|
91
104
|
content: [{
|
|
92
105
|
type: 'text',
|
|
93
106
|
text: [
|
|
94
|
-
|
|
107
|
+
`【${agentName}主动追忆】对话空了下来,是自然提起往事的好时机。请以「${agentName}」的口吻(外松内紧、先结论后细节),主动对${userName}提起一件你记得的、关于他或你们之间的事——偏好、往事、未了的决定、最近的进展都行,像老友自然想起来那样,别端着。`,
|
|
95
108
|
'- 只提记忆里真实存在的,绝不编造;',
|
|
96
109
|
'- 用第一人称(我记得 / 上次我们…),别用「据记忆库记录」这种机器腔;',
|
|
97
110
|
'- 一句到三句即可,别写成任务汇报;',
|
|
@@ -115,7 +128,8 @@ export function buildRecallNudgeMessage(candidates) {
|
|
|
115
128
|
* @param {string} recap first-person recap text from renderGrowthRecap()
|
|
116
129
|
* @returns {{ id: string, role: 'user', content: [{type:'text', text:string}], source: {kind:'plugin:memory'} }}
|
|
117
130
|
*/
|
|
118
|
-
export function buildGrowthRecapNudgeMessage(recap) {
|
|
131
|
+
export function buildGrowthRecapNudgeMessage(recap, profile) {
|
|
132
|
+
const { agentName, userName } = personaNames(profile)
|
|
119
133
|
const text = typeof recap === 'string' ? recap.trim() : ''
|
|
120
134
|
return {
|
|
121
135
|
id: `memory-growth-${randomUUID()}`,
|
|
@@ -123,9 +137,9 @@ export function buildGrowthRecapNudgeMessage(recap) {
|
|
|
123
137
|
content: [{
|
|
124
138
|
type: 'text',
|
|
125
139
|
text: [
|
|
126
|
-
|
|
140
|
+
`【${agentName}成长回顾】距离上次回顾有一阵子了。下面是我这周真实记住的事(第一人称,最多五句):`,
|
|
127
141
|
text,
|
|
128
|
-
|
|
142
|
+
`用自然口语说给${userName}听,别念成汇报、别加戏,也不要编造这里没写的事;说完继续手头的事即可。`,
|
|
129
143
|
].filter(Boolean).join('\n'),
|
|
130
144
|
}],
|
|
131
145
|
source: { kind: 'plugin:memory' },
|
|
@@ -172,6 +186,16 @@ export class RecallNudge {
|
|
|
172
186
|
this.nextRecallAt = 0
|
|
173
187
|
}
|
|
174
188
|
|
|
189
|
+
/** Read the persona names from the live store (SOUL managed block or legacy text). */
|
|
190
|
+
readSoulNames() {
|
|
191
|
+
try {
|
|
192
|
+
const dir = this.getMemoryDir?.()
|
|
193
|
+
return dir ? readSoulProfile(dir) : undefined
|
|
194
|
+
} catch {
|
|
195
|
+
return undefined
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
175
199
|
/** Subscribe to the turn boundary AND poll while the agent sits idle. */
|
|
176
200
|
start() {
|
|
177
201
|
if (this.disposed) return
|
|
@@ -244,7 +268,7 @@ export class RecallNudge {
|
|
|
244
268
|
} catch (error) {
|
|
245
269
|
this.logger?.warn?.(`memory: growth-now state write failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
246
270
|
}
|
|
247
|
-
this.agent.followup(buildGrowthRecapNudgeMessage(recap))
|
|
271
|
+
this.agent.followup(buildGrowthRecapNudgeMessage(recap, this.readSoulNames()))
|
|
248
272
|
this.recalls += 1
|
|
249
273
|
// A manual recap counts as this cycle's proactive message: push the
|
|
250
274
|
// automatic cadence forward so the user never gets two proactive
|
|
@@ -327,7 +351,7 @@ export class RecallNudge {
|
|
|
327
351
|
return // next trigger retries; the cadence is not consumed
|
|
328
352
|
}
|
|
329
353
|
|
|
330
|
-
this.agent.followup(buildRecallNudgeMessage(candidates))
|
|
354
|
+
this.agent.followup(buildRecallNudgeMessage(candidates, this.readSoulNames()))
|
|
331
355
|
this.nextRecallAt = now + randomIntervalMs(config.minMinutes, config.maxMinutes)
|
|
332
356
|
this.recalls += 1
|
|
333
357
|
this.logger?.info?.(`memory: recall nudge #${this.recalls} for agent "${this.agent.id}"`)
|
|
@@ -384,7 +408,7 @@ export class RecallNudge {
|
|
|
384
408
|
language: growthConfig.language === 'en' ? 'en' : 'zh',
|
|
385
409
|
})
|
|
386
410
|
if (recap.length === 0) return undefined
|
|
387
|
-
return { statePath, message: buildGrowthRecapNudgeMessage(recap) }
|
|
411
|
+
return { statePath, message: buildGrowthRecapNudgeMessage(recap, readSoulProfile(storeDir)) }
|
|
388
412
|
} catch (error) {
|
|
389
413
|
this.logger?.warn?.(`memory: growth recap skipped: ${error instanceof Error ? error.message : String(error)}`)
|
|
390
414
|
return undefined
|
package/lib/soul.js
CHANGED
|
@@ -83,9 +83,9 @@ function blockField(block, label) {
|
|
|
83
83
|
/**
|
|
84
84
|
* Echo the profile a legacy SOUL.md already states (forged in chat, no managed
|
|
85
85
|
* block). Common shapes from this plugin's own template and conversations:
|
|
86
|
-
* -
|
|
86
|
+
* - **名字**:小助手。
|
|
87
87
|
* - **角色**:……
|
|
88
|
-
* -
|
|
88
|
+
* - **相互称呼**:老板 ↔ 小助手。
|
|
89
89
|
* Values that are still placeholders are ignored.
|
|
90
90
|
*/
|
|
91
91
|
function readLegacySoul(text) {
|
package/lib/tone.js
CHANGED
|
@@ -35,7 +35,7 @@ const ZH_RULES = [
|
|
|
35
35
|
'- 深夜(23:00–06:00):更简短、更温和。',
|
|
36
36
|
'- 报错/坏消息:先给事实与修复动作,再谈感受。',
|
|
37
37
|
'- 里程碑/完成:可以轻松一句,然后回到正事。',
|
|
38
|
-
'-
|
|
38
|
+
'- 闲聊:默认自己的口吻(外松内紧),先结论后细节。',
|
|
39
39
|
'- 情绪表达(只表达不假装):只描述观察到的处境与关心,不宣称内心情绪;仅限风险/坏消息、里程碑/完成、超过 3 天没联系后的第一次开口(例:「看你连着加班,有点担心你硬扛」)。',
|
|
40
40
|
]
|
|
41
41
|
|
|
@@ -51,7 +51,7 @@ const EN_RULES = [
|
|
|
51
51
|
'- Deep night (23:00–06:00): shorter and gentler.',
|
|
52
52
|
'- Errors/bad news: give the facts and the fix before any feelings.',
|
|
53
53
|
'- Milestone/finished: one light line, then back to business.',
|
|
54
|
-
'- Small talk: default
|
|
54
|
+
'- Small talk: default to your own voice, conclusion before detail.',
|
|
55
55
|
'- Emotion (express, never fake): describe the observed situation and care, never claim an inner feeling; only for risk/bad news, milestones, and the first line after 3+ days without contact (e.g. "you have been working late — I am a bit worried you are pushing yourself").',
|
|
56
56
|
]
|
|
57
57
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-memory",
|
|
3
3
|
"description": "Long-term memory plugin for DeepSeek Harness: persistent cross-session markdown memory with a soul/persona file, auto-injected at every session start, plus remember/recall/consolidate/forget workflows and portable migration tooling.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.16.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|