@zhangfeiyue/dsh-long-term-memory 0.0.0-stage → 1.1.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/README.md +96 -2
- package/client.js +580 -0
- package/cordis.patch.yml +9 -0
- package/docs/DETAIL.md +226 -0
- package/icon.svg +18 -0
- package/index.js +483 -0
- package/lib/board.js +178 -0
- package/lib/config.js +133 -0
- package/lib/consolidate.js +162 -0
- package/lib/dashboard.js +101 -0
- package/lib/domains.js +185 -0
- package/lib/extract.js +198 -0
- package/lib/profile.js +167 -0
- package/lib/records.js +197 -0
- package/lib/render.js +181 -0
- package/lib/retrieval.js +217 -0
- package/lib/store.js +338 -0
- package/lib/tools.js +422 -0
- package/lib/util.js +94 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +53 -4
package/lib/tools.js
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing tools.
|
|
3
|
+
*
|
|
4
|
+
* Five tools, deliberately few: one write, one read, one correction, one
|
|
5
|
+
* deletion, one maintenance. A memory system earns its prompt budget only when
|
|
6
|
+
* the model can see the whole surface at a glance.
|
|
7
|
+
*
|
|
8
|
+
* Tool definitions are plain objects — the same shape the registry accepts —
|
|
9
|
+
* rather than instances of a harness helper class, so the plugin keeps working
|
|
10
|
+
* across DSH versions without a build step or a peer dependency.
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-long-term-memory/tools
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { consolidate, dueForConsolidation } from './consolidate.js'
|
|
16
|
+
import { DOMAINS, LAYERS, normalizeDomain } from './domains.js'
|
|
17
|
+
import { buildProfile, profileSummary } from './profile.js'
|
|
18
|
+
import { KINDS, TASK_STATUSES, markUsed, recordLabel, saveMemory } from './records.js'
|
|
19
|
+
import { renderRecordMarkdown } from './render.js'
|
|
20
|
+
import { rankRecords } from './retrieval.js'
|
|
21
|
+
import { normalizeWorkspace, truncate, workspaceKey, workspaceLabel } from './util.js'
|
|
22
|
+
|
|
23
|
+
/** Text block helper for tool rendering. */
|
|
24
|
+
function text(value) {
|
|
25
|
+
return [{ type: 'text', text: value }]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Absolute workspace directory of the calling session, when there is one. */
|
|
29
|
+
export function workspaceOf(exec) {
|
|
30
|
+
const cwd = exec?.agent?.session?.header?.cwd
|
|
31
|
+
return typeof cwd === 'string' && cwd.length > 0 ? cwd : null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Source attribution recorded on every model-authored memory. */
|
|
35
|
+
function sourceOf(exec) {
|
|
36
|
+
return {
|
|
37
|
+
kind: 'agent-tool',
|
|
38
|
+
sessionId: exec?.agent?.session?.id ?? null,
|
|
39
|
+
agentName: exec?.agent?.options?.name ?? null,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Object-rooted output schema with all fields required. */
|
|
44
|
+
function outputSchema(properties) {
|
|
45
|
+
return { type: 'object', properties, required: Object.keys(properties), additionalProperties: false }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Compile an author-friendly parameter map into the raw JSON Schema the tool
|
|
50
|
+
* registry expects.
|
|
51
|
+
*
|
|
52
|
+
* The registry stores `parameters` verbatim as an object-rooted JSON Schema, so
|
|
53
|
+
* requiredness is a root-level `required` array — not a per-property
|
|
54
|
+
* `required: true`, which is only the authoring shorthand of the harness's own
|
|
55
|
+
* `defineTool` helper. Emitting the shorthand here made every parameter optional
|
|
56
|
+
* from the provider's point of view, and `memory_forget` was called with an empty
|
|
57
|
+
* `ids` list because the model saw no required `ids` property.
|
|
58
|
+
*/
|
|
59
|
+
function parameterSchema(spec) {
|
|
60
|
+
const properties = {}
|
|
61
|
+
const required = []
|
|
62
|
+
for (const [name, node] of Object.entries(spec)) {
|
|
63
|
+
const { required: isRequired, ...rest } = node
|
|
64
|
+
properties[name] = rest
|
|
65
|
+
if (isRequired === true) required.push(name)
|
|
66
|
+
}
|
|
67
|
+
return required.length > 0
|
|
68
|
+
? { type: 'object', properties, required }
|
|
69
|
+
: { type: 'object', properties }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Build the tool definitions.
|
|
74
|
+
*
|
|
75
|
+
* @param {object} store - the memory store.
|
|
76
|
+
* @param {object} settings - resolved plugin settings.
|
|
77
|
+
* @param {object} hooks - plugin hooks (`onChanged`, `log`).
|
|
78
|
+
* @returns {object[]} tool definitions ready for `ctx.tools.register`.
|
|
79
|
+
*/
|
|
80
|
+
export function createTools(store, settings, hooks) { const contextFor = (exec) => {
|
|
81
|
+
const workspace = workspaceOf(exec)
|
|
82
|
+
return { workspace, workspaceLabelFallback: workspace === null ? null : workspaceLabel(workspace) }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const saveTool = {
|
|
86
|
+
name: 'memory_save',
|
|
87
|
+
description: 'Save one durable memory for future sessions and workspaces: a user preference, project knowledge, '
|
|
88
|
+
+ 'a technical decision, a solved problem, a stable fact, or an unfinished task. '
|
|
89
|
+
+ 'Save proactively when the user states a lasting preference, when a decision is made, or when a problem '
|
|
90
|
+
+ 'was solved in a way worth reusing. Do not save transient state, secrets, or long code blocks. '
|
|
91
|
+
+ 'Near-identical memories of the same kind are merged instead of duplicated.',
|
|
92
|
+
parameters: {
|
|
93
|
+
text: { type: 'string', description: 'The memory itself, written so it is intelligible without this conversation.', required: true },
|
|
94
|
+
kind: { type: 'string', enum: KINDS, description: 'preference (user/agent working style), project (workspace knowledge), decision (architecture or process choice), solution (a solved problem), fact (stable reference), task (work to resume), capability (what the agent can do), note (misc).' },
|
|
95
|
+
title: { type: 'string', description: 'Short label; derived from the text when omitted.' },
|
|
96
|
+
tags: { type: 'array', items: { type: 'string' }, description: 'Optional lowercase keywords for retrieval.' },
|
|
97
|
+
scope: { type: 'string', enum: ['auto', 'global', 'workspace'], description: 'auto (default) keeps project/task/solution in this workspace and everything else global; global is visible in every workspace; workspace limits it to the current working directory.' },
|
|
98
|
+
importance: { type: 'integer', description: '1..5, where 5 is a hard constraint. Default 3.' },
|
|
99
|
+
status: { type: 'string', enum: ['active', 'open', 'blocked', 'done', 'dropped'], description: 'Lifecycle state; tasks default to open, everything else to active.' },
|
|
100
|
+
layer: { type: 'string', enum: LAYERS, description: 'Which profile the memory belongs to: persona (the user as a person), work (how a field of work is done), project (this workspace only), episodic (a piece of work in flight). Inferred from kind, scope and domain when omitted.' },
|
|
101
|
+
domain: { type: 'string', enum: DOMAINS, description: 'Work domain, for layer work. Omit to let the workspace decide.' },
|
|
102
|
+
facet: { type: 'string', description: 'Which aspect of the profile this describes, such as communication, constraints, goals, stack, conventions, quality, workflow, gotchas or decisions. Defaulted from the kind when omitted.' },
|
|
103
|
+
},
|
|
104
|
+
output: {
|
|
105
|
+
schema: outputSchema({
|
|
106
|
+
saved: { type: 'boolean' },
|
|
107
|
+
merged: { type: 'boolean' },
|
|
108
|
+
id: { type: 'string' },
|
|
109
|
+
kind: { type: 'string' },
|
|
110
|
+
scope: { type: 'string' },
|
|
111
|
+
status: { type: 'string' },
|
|
112
|
+
message: { type: 'string' },
|
|
113
|
+
}),
|
|
114
|
+
render: (_args, value) => text(value.message),
|
|
115
|
+
},
|
|
116
|
+
async execute(args, exec) {
|
|
117
|
+
const { record, merged } = saveMemory(store, { ...args, source: sourceOf(exec) }, contextFor(exec))
|
|
118
|
+
hooks.onChanged?.('save')
|
|
119
|
+
return {
|
|
120
|
+
saved: true,
|
|
121
|
+
merged,
|
|
122
|
+
id: record.id,
|
|
123
|
+
kind: record.kind,
|
|
124
|
+
scope: record.scope,
|
|
125
|
+
status: record.status,
|
|
126
|
+
message: `${merged ? 'Merged into existing' : 'Saved'} ${record.kind} memory ${record.id} (${record.scope}).`,
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const searchTool = {
|
|
132
|
+
name: 'memory_search',
|
|
133
|
+
description: 'Search long-term memory for entries relevant to a task, keyword, file, decision, or open work. '
|
|
134
|
+
+ 'Use it before asking the user to repeat context, before re-deriving an earlier decision, and whenever '
|
|
135
|
+
+ 'the conversation touches something that may have been decided in another session or workspace. '
|
|
136
|
+
+ 'An empty query returns the most durable memories. Results include ids for memory_update and memory_forget.',
|
|
137
|
+
parameters: {
|
|
138
|
+
query: { type: 'string', description: 'Keywords or a sentence. Leave empty to list the most important memories.' },
|
|
139
|
+
kind: { type: 'string', enum: KINDS, description: 'Restrict to one memory kind.' },
|
|
140
|
+
layer: { type: 'string', enum: LAYERS, description: 'Restrict to one profile layer.' },
|
|
141
|
+
domain: { type: 'string', enum: DOMAINS, description: 'Restrict to one work domain.' },
|
|
142
|
+
status: { type: 'string', enum: ['active', 'open', 'blocked', 'done', 'superseded', 'dropped', 'archived'], description: 'Restrict to one lifecycle state.' },
|
|
143
|
+
limit: { type: 'integer', description: 'Maximum results (default 8).' },
|
|
144
|
+
includeOtherWorkspaces: { type: 'boolean', description: 'Also return memories that belong to a different workspace.' },
|
|
145
|
+
},
|
|
146
|
+
output: {
|
|
147
|
+
schema: outputSchema({
|
|
148
|
+
count: { type: 'integer' },
|
|
149
|
+
results: {
|
|
150
|
+
type: 'array',
|
|
151
|
+
items: {
|
|
152
|
+
type: 'object',
|
|
153
|
+
additionalProperties: false,
|
|
154
|
+
properties: {
|
|
155
|
+
id: { type: 'string' },
|
|
156
|
+
kind: { type: 'string' },
|
|
157
|
+
layer: { type: 'string' },
|
|
158
|
+
domain: { type: 'string' },
|
|
159
|
+
facet: { type: 'string' },
|
|
160
|
+
scope: { type: 'string' },
|
|
161
|
+
status: { type: 'string' },
|
|
162
|
+
title: { type: 'string' },
|
|
163
|
+
text: { type: 'string' },
|
|
164
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
165
|
+
workspace: { type: 'string' },
|
|
166
|
+
score: { type: 'number' },
|
|
167
|
+
updatedAt: { type: 'string' },
|
|
168
|
+
},
|
|
169
|
+
required: ['id', 'kind', 'layer', 'domain', 'facet', 'scope', 'status', 'title', 'text', 'tags', 'workspace', 'score', 'updatedAt'],
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
message: { type: 'string' },
|
|
173
|
+
}),
|
|
174
|
+
render: (_args, value) => text(value.message),
|
|
175
|
+
},
|
|
176
|
+
async execute(args, exec) {
|
|
177
|
+
const workspace = workspaceOf(exec)
|
|
178
|
+
const limit = Math.max(1, Math.min(settings.searchLimit, Number(args.limit) || 8))
|
|
179
|
+
let pool = store.all()
|
|
180
|
+
if (typeof args.layer === 'string') pool = pool.filter(record => record.layer === args.layer)
|
|
181
|
+
if (typeof args.domain === 'string') {
|
|
182
|
+
const wanted = normalizeDomain(args.domain)
|
|
183
|
+
pool = pool.filter(record => record.layer === 'work' && normalizeDomain(record.domain) === wanted)
|
|
184
|
+
}
|
|
185
|
+
const results = rankRecords(pool, {
|
|
186
|
+
query: args.query ?? '',
|
|
187
|
+
workspaceKey: workspaceKey(workspace),
|
|
188
|
+
kinds: args.kind === undefined ? undefined : [args.kind],
|
|
189
|
+
statuses: args.status === undefined ? undefined : [args.status],
|
|
190
|
+
limit,
|
|
191
|
+
includeOtherWorkspaces: args.includeOtherWorkspaces === true,
|
|
192
|
+
})
|
|
193
|
+
markUsed(store, results.map(entry => entry.id))
|
|
194
|
+
hooks.onChanged?.('search')
|
|
195
|
+
return {
|
|
196
|
+
count: results.length,
|
|
197
|
+
results: results.map(entry => ({
|
|
198
|
+
id: entry.id,
|
|
199
|
+
kind: entry.kind,
|
|
200
|
+
layer: entry.layer ?? '',
|
|
201
|
+
domain: entry.domain ?? '',
|
|
202
|
+
facet: entry.facet ?? '',
|
|
203
|
+
scope: entry.scope,
|
|
204
|
+
status: entry.status ?? 'active',
|
|
205
|
+
title: recordLabel(entry),
|
|
206
|
+
text: entry.text ?? '',
|
|
207
|
+
tags: Array.isArray(entry.tags) ? entry.tags : [],
|
|
208
|
+
workspace: entry.workspace ?? '',
|
|
209
|
+
score: Math.round(entry.score * 1000) / 1000,
|
|
210
|
+
updatedAt: new Date(entry.updatedAt ?? 0).toISOString(),
|
|
211
|
+
})),
|
|
212
|
+
message: results.length === 0
|
|
213
|
+
? 'No memory matched. Save a new one with memory_save if this is worth remembering.'
|
|
214
|
+
: results.map(entry => `${entry.id} [${entry.kind}/${entry.status ?? 'active'}] ${truncate(entry.text ?? '', 240)}`).join('\n'),
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const updateTool = {
|
|
220
|
+
name: 'memory_update',
|
|
221
|
+
description: 'Correct or advance one stored memory. Use it when a decision changes, when a preference is '
|
|
222
|
+
+ 'refined, or when a task starts, finishes, or is blocked. Prefer updating over saving a duplicate.',
|
|
223
|
+
parameters: {
|
|
224
|
+
id: { type: 'string', description: 'Id of the memory to update.', required: true },
|
|
225
|
+
text: { type: 'string', description: 'Replacement text.' },
|
|
226
|
+
title: { type: 'string', description: 'Replacement title.' },
|
|
227
|
+
kind: { type: 'string', enum: KINDS, description: 'Replacement kind.' },
|
|
228
|
+
status: { type: 'string', enum: ['active', 'open', 'blocked', 'done', 'dropped', 'superseded', 'archived'], description: 'Replacement lifecycle state.' },
|
|
229
|
+
importance: { type: 'integer', description: 'Replacement importance 1..5.' },
|
|
230
|
+
tags: { type: 'array', items: { type: 'string' }, description: 'Replacement tag list.' },
|
|
231
|
+
},
|
|
232
|
+
output: {
|
|
233
|
+
schema: outputSchema({
|
|
234
|
+
updated: { type: 'boolean' },
|
|
235
|
+
id: { type: 'string' },
|
|
236
|
+
message: { type: 'string' },
|
|
237
|
+
}),
|
|
238
|
+
render: (_args, value) => text(value.message),
|
|
239
|
+
},
|
|
240
|
+
async execute(args) {
|
|
241
|
+
const patch = {}
|
|
242
|
+
if (typeof args.text === 'string' && args.text.trim().length > 0) patch.text = truncate(args.text.trim(), 4000)
|
|
243
|
+
if (typeof args.title === 'string' && args.title.trim().length > 0) patch.title = truncate(args.title.trim(), 120)
|
|
244
|
+
if (KINDS.includes(args.kind)) patch.kind = args.kind
|
|
245
|
+
if (typeof args.status === 'string') patch.status = args.status
|
|
246
|
+
if (Number.isFinite(Number(args.importance))) patch.importance = Math.max(1, Math.min(5, Number(args.importance)))
|
|
247
|
+
if (Array.isArray(args.tags)) patch.tags = args.tags
|
|
248
|
+
const updated = store.update(args.id, patch)
|
|
249
|
+
hooks.onChanged?.('update')
|
|
250
|
+
return {
|
|
251
|
+
updated: updated !== undefined,
|
|
252
|
+
id: args.id,
|
|
253
|
+
message: updated === undefined
|
|
254
|
+
? `No memory with id ${args.id}.`
|
|
255
|
+
: `Updated ${args.id} (${Object.keys(patch).join(', ') || 'no fields'}).`,
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const forgetTool = {
|
|
261
|
+
name: 'memory_forget',
|
|
262
|
+
description: 'Permanently delete memories the user asked to remove, or clearly obsolete entries. '
|
|
263
|
+
+ 'Pass explicit ids from memory_search. Deletion is irreversible, so confirm with the user when the '
|
|
264
|
+
+ 'intent is not explicit.',
|
|
265
|
+
parameters: {
|
|
266
|
+
ids: { type: 'array', items: { type: 'string' }, description: 'Ids to delete.', required: true },
|
|
267
|
+
},
|
|
268
|
+
output: {
|
|
269
|
+
schema: outputSchema({
|
|
270
|
+
deleted: { type: 'integer' },
|
|
271
|
+
missing: { type: 'array', items: { type: 'string' } },
|
|
272
|
+
message: { type: 'string' },
|
|
273
|
+
}),
|
|
274
|
+
render: (_args, value) => text(value.message),
|
|
275
|
+
},
|
|
276
|
+
async execute(args) {
|
|
277
|
+
const ids = Array.isArray(args.ids) ? args.ids.filter(id => typeof id === 'string') : []
|
|
278
|
+
let deleted = 0
|
|
279
|
+
const missing = []
|
|
280
|
+
for (const id of ids) {
|
|
281
|
+
if (store.remove(id)) deleted += 1
|
|
282
|
+
else missing.push(id)
|
|
283
|
+
}
|
|
284
|
+
hooks.onChanged?.('forget')
|
|
285
|
+
return {
|
|
286
|
+
deleted,
|
|
287
|
+
missing,
|
|
288
|
+
message: `Deleted ${deleted} memories${missing.length > 0 ? `; not found: ${missing.join(', ')}` : ''}.`,
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const profileTool = {
|
|
294
|
+
name: 'memory_profile',
|
|
295
|
+
description: 'Read or correct a long-term profile. The personal profile (layer persona) describes the user as a person; a work profile (layer work) describes how one field of work is done. '
|
|
296
|
+
+ 'Use show to read a profile, pin or unpin to force one entry into or out of it, and rebuild to recompute. '
|
|
297
|
+
+ 'Pin an entry when the user states a durable fact about themselves; unpin one that no longer applies.',
|
|
298
|
+
parameters: {
|
|
299
|
+
action: { type: 'string', enum: ['show', 'pin', 'unpin', 'rebuild'], description: 'show reads the profile; pin/unpin force one entry in or out; rebuild recomputes and reports.', required: true },
|
|
300
|
+
layer: { type: 'string', enum: ['persona', 'work'], description: 'Which profile: persona (default) or work.' },
|
|
301
|
+
domain: { type: 'string', enum: DOMAINS, description: 'Work domain, required when layer is work.' },
|
|
302
|
+
id: { type: 'string', description: 'Record id to pin or unpin.' },
|
|
303
|
+
},
|
|
304
|
+
output: {
|
|
305
|
+
schema: outputSchema({
|
|
306
|
+
ok: { type: 'boolean' },
|
|
307
|
+
layer: { type: 'string' },
|
|
308
|
+
domain: { type: 'string' },
|
|
309
|
+
count: { type: 'integer' },
|
|
310
|
+
text: { type: 'string' },
|
|
311
|
+
message: { type: 'string' },
|
|
312
|
+
}),
|
|
313
|
+
render: (_args, value) => text(value.message),
|
|
314
|
+
},
|
|
315
|
+
async execute(args) {
|
|
316
|
+
const layer = args.layer === 'work' ? 'work' : 'persona'
|
|
317
|
+
const domain = layer === 'work' ? normalizeDomain(args.domain) : null
|
|
318
|
+
const title = layer === 'persona' ? 'Personal profile' : `Work profile — ${domain}`
|
|
319
|
+
if (args.action === 'pin' || args.action === 'unpin') {
|
|
320
|
+
if (typeof args.id !== 'string' || args.id.length === 0) {
|
|
321
|
+
return { ok: false, layer, domain: domain ?? '', count: 0, text: '', message: 'pin and unpin need an id from memory_search.' }
|
|
322
|
+
}
|
|
323
|
+
const existing = store.get(args.id)
|
|
324
|
+
if (existing === undefined) {
|
|
325
|
+
return { ok: false, layer, domain: domain ?? '', count: 0, text: '', message: `No memory with id ${args.id}.` }
|
|
326
|
+
}
|
|
327
|
+
store.update(args.id, { pinned: args.action === 'pin' })
|
|
328
|
+
hooks.onChanged?.('profile')
|
|
329
|
+
return {
|
|
330
|
+
ok: true,
|
|
331
|
+
layer,
|
|
332
|
+
domain: domain ?? '',
|
|
333
|
+
count: 0,
|
|
334
|
+
text: '',
|
|
335
|
+
message: `${args.action === 'pin' ? 'Pinned' : 'Unpinned'} ${args.id} ${args.action === 'pin' ? 'into' : 'from'} the ${title.toLowerCase()}.`,
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const built = buildProfile(store, { layer, domain, settings, maxChars: 4000 })
|
|
339
|
+
return {
|
|
340
|
+
ok: true,
|
|
341
|
+
layer,
|
|
342
|
+
domain: domain ?? '',
|
|
343
|
+
count: built.count,
|
|
344
|
+
text: built.text,
|
|
345
|
+
message: built.count === 0
|
|
346
|
+
? `${title} is empty: no record meets the admission rule yet (importance >= ${layer === 'persona' ? settings.personaMinImportance : settings.workMinImportance}, two confirmations, or an explicit pin).`
|
|
347
|
+
: `${title} — ${built.count} entries:\n${built.text}`,
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const adminTool = {
|
|
353
|
+
name: 'memory_admin',
|
|
354
|
+
description: 'Inspect and maintain the long-term memory store: stats (counts and location), '
|
|
355
|
+
+ 'consolidate (deduplicate, resolve conflicts, archive stale entries, enforce the size cap), '
|
|
356
|
+
+ 'backup (copy the log), or export (dump memories as markdown or json). '
|
|
357
|
+
+ 'Reserved for maintenance; normal recall uses memory_search.',
|
|
358
|
+
parameters: {
|
|
359
|
+
action: { type: 'string', enum: ['stats', 'consolidate', 'backup', 'export', 'profiles'], description: 'Maintenance action; profiles reports every profile and its entries.', required: true },
|
|
360
|
+
format: { type: 'string', enum: ['markdown', 'json'], description: 'Export format (default markdown).' },
|
|
361
|
+
status: { type: 'string', enum: ['active', 'open', 'blocked', 'done', 'superseded', 'dropped', 'archived', 'all'], description: 'Export filter (default all).' },
|
|
362
|
+
limit: { type: 'integer', description: 'Maximum records to export (default 50).' },
|
|
363
|
+
},
|
|
364
|
+
output: {
|
|
365
|
+
schema: outputSchema({
|
|
366
|
+
action: { type: 'string' },
|
|
367
|
+
ok: { type: 'boolean' },
|
|
368
|
+
value: { type: 'string' },
|
|
369
|
+
}),
|
|
370
|
+
render: (_args, value) => text(value.value),
|
|
371
|
+
},
|
|
372
|
+
async execute(args, exec) {
|
|
373
|
+
const action = args.action
|
|
374
|
+
if (action === 'stats') {
|
|
375
|
+
const stats = store.stats()
|
|
376
|
+
return { action, ok: true, value: JSON.stringify(stats, null, 2) }
|
|
377
|
+
}
|
|
378
|
+
if (action === 'consolidate') {
|
|
379
|
+
const summary = consolidate(store, settings)
|
|
380
|
+
hooks.onChanged?.('consolidate')
|
|
381
|
+
return { action, ok: true, value: `Consolidation complete: merged ${summary.merged}, superseded ${summary.superseded}, archived ${summary.archived}, trimmed ${summary.trimmed}; ${summary.recordsBefore} → ${summary.recordsAfter} records.` }
|
|
382
|
+
}
|
|
383
|
+
if (action === 'profiles') {
|
|
384
|
+
const rows = profileSummary(store, settings, DOMAINS)
|
|
385
|
+
return {
|
|
386
|
+
action,
|
|
387
|
+
ok: true,
|
|
388
|
+
value: rows.length === 0 ? 'No profile has any eligible entry yet.' : JSON.stringify(rows, null, 2),
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (action === 'backup') {
|
|
392
|
+
const result = store.compact(true)
|
|
393
|
+
return { action, ok: result.compacted === true, value: result.compacted ? `Backup written: ${result.backup ?? '(no prior log)'}` : `Backup failed: ${result.error ?? 'unknown error'}` }
|
|
394
|
+
}
|
|
395
|
+
const workspace = workspaceOf(exec)
|
|
396
|
+
const statusFilter = args.status === undefined || args.status === 'all' ? undefined : [args.status]
|
|
397
|
+
const rows = rankRecords(store.all(), {
|
|
398
|
+
query: '',
|
|
399
|
+
workspaceKey: workspaceKey(workspace),
|
|
400
|
+
statuses: statusFilter,
|
|
401
|
+
limit: Math.max(1, Math.min(200, Number(args.limit) || 50)),
|
|
402
|
+
includeOtherWorkspaces: true,
|
|
403
|
+
})
|
|
404
|
+
const markdown = rows.map(record => renderRecordMarkdown(record)).join('\n\n')
|
|
405
|
+
return {
|
|
406
|
+
action,
|
|
407
|
+
ok: true,
|
|
408
|
+
value: args.format === 'json' ? JSON.stringify(rows, null, 2) : (markdown.length > 0 ? markdown : 'No memories matched the export filter.'),
|
|
409
|
+
}
|
|
410
|
+
},
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Compile every author-friendly parameter map into raw JSON Schema here, so the
|
|
414
|
+
// definitions above can keep the readable `required: true` shorthand.
|
|
415
|
+
return [saveTool, searchTool, updateTool, forgetTool, profileTool, adminTool].map(tool => ({
|
|
416
|
+
...tool,
|
|
417
|
+
parameters: parameterSchema(tool.parameters),
|
|
418
|
+
}))
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Expose consolidation timing so the entry point can schedule it. */
|
|
422
|
+
export { dueForConsolidation, normalizeWorkspace, TASK_STATUSES }
|
package/lib/util.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small shared helpers for the long-term memory plugin.
|
|
3
|
+
*
|
|
4
|
+
* This module is deliberately dependency-free: it imports only `node:` builtins
|
|
5
|
+
* so the plugin never binds to a DSH internal package version. Everything the
|
|
6
|
+
* plugin needs from the harness arrives through `ctx` at runtime.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-long-term-memory/util
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
12
|
+
|
|
13
|
+
/** Current epoch milliseconds. */
|
|
14
|
+
export function now() {
|
|
15
|
+
return Date.now()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Clamp a number into an inclusive range. */
|
|
19
|
+
export function clamp(value, low, high) {
|
|
20
|
+
return Math.min(high, Math.max(low, value))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Stable SHA-1 hex digest of a string. */
|
|
24
|
+
export function sha1(value) {
|
|
25
|
+
return createHash('sha1').update(String(value), 'utf8').digest('hex')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Mint a short, collision-resistant record id. */
|
|
29
|
+
export function newId(prefix = 'm') {
|
|
30
|
+
return `${prefix}_${randomUUID().replace(/-/g, '').slice(0, 16)}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Canonical form of a workspace directory, used as the memory partition key.
|
|
35
|
+
*
|
|
36
|
+
* Windows paths are case-insensitive, so the key lowercases and forward-slashes
|
|
37
|
+
* the path and drops any trailing separator. Two spellings of the same
|
|
38
|
+
* directory therefore land in one memory partition.
|
|
39
|
+
*/
|
|
40
|
+
export function normalizeWorkspace(directory) {
|
|
41
|
+
if (typeof directory !== 'string' || directory.length === 0) return null
|
|
42
|
+
return directory
|
|
43
|
+
.replace(/\\/g, '/')
|
|
44
|
+
.replace(/\/+$/, '')
|
|
45
|
+
.toLowerCase()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Short, filesystem-safe partition key for one workspace directory. */
|
|
49
|
+
export function workspaceKey(directory) {
|
|
50
|
+
const normalized = normalizeWorkspace(directory)
|
|
51
|
+
return normalized === null ? null : sha1(normalized).slice(0, 12)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Human-facing last path segment of a workspace directory. */
|
|
55
|
+
export function workspaceLabel(directory) {
|
|
56
|
+
if (typeof directory !== 'string' || directory.length === 0) return null
|
|
57
|
+
const parts = directory.replace(/\\/g, '/').replace(/\/+$/, '').split('/')
|
|
58
|
+
return parts[parts.length - 1] || directory
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Collapse whitespace and lowercase — the comparison form of memory text. */
|
|
62
|
+
export function normalizeText(value) {
|
|
63
|
+
return String(value ?? '')
|
|
64
|
+
.toLowerCase()
|
|
65
|
+
.replace(/\s+/g, ' ')
|
|
66
|
+
.trim()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Truncate with an ellipsis, never exceeding `max` characters. */
|
|
70
|
+
export function truncate(value, max) {
|
|
71
|
+
const text = String(value ?? '')
|
|
72
|
+
return text.length <= max ? text : `${text.slice(0, Math.max(0, max - 1))}…`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Whether a code point belongs to a script written without spaces. */
|
|
76
|
+
export function isCjkCodePoint(codePoint) {
|
|
77
|
+
return (codePoint >= 0x3400 && codePoint <= 0x4dbf)
|
|
78
|
+
|| (codePoint >= 0x4e00 && codePoint <= 0x9fff)
|
|
79
|
+
|| (codePoint >= 0x3040 && codePoint <= 0x30ff)
|
|
80
|
+
|| (codePoint >= 0xac00 && codePoint <= 0xd7af)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Whether a value is a plain object (not null, not an array). */
|
|
84
|
+
export function isPlainObject(value) {
|
|
85
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Read the first present string among candidate values. */
|
|
89
|
+
export function firstString(...candidates) {
|
|
90
|
+
for (const candidate of candidates) {
|
|
91
|
+
if (typeof candidate === 'string' && candidate.trim().length > 0) return candidate
|
|
92
|
+
}
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Long-term memory",
|
|
4
|
+
"description": "Gives every session a persistent memory shared across workspaces, organised as a personal profile of the user and a work profile per field of work: it recalls relevant preferences, conventions, decisions and unfinished tasks automatically, records explicit instructions as they are given, and consolidates the store in the background.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhangfeiyue/dsh-long-term-memory",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Global persistent long-term memory for DeepSeek Harness: automatic extraction, proactive retrieval, dynamic context injection, task resume, periodic consolidation, persona and per-domain work profiles, and a live memory board.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "zhangfeiyue",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://gitlab.wcszfn.com/talent-tech/frontend/dsh-long-term-memory.git",
|
|
12
|
+
"directory": "packages/dsh-long-term-memory"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=22.19.0"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./index.js",
|
|
19
|
+
"./client": "./client.js",
|
|
20
|
+
"./locale/*.json": "./locale/*.json",
|
|
21
|
+
"./icon": "./icon.svg",
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"index.js",
|
|
26
|
+
"client.js",
|
|
27
|
+
"lib",
|
|
28
|
+
"cordis.patch.yml",
|
|
29
|
+
"locale/*.json",
|
|
30
|
+
"docs",
|
|
31
|
+
"icon.svg",
|
|
32
|
+
"README.md"
|
|
33
|
+
],
|
|
34
|
+
"dsh": {
|
|
35
|
+
"bundle": {
|
|
36
|
+
"patch": "./cordis.patch.yml"
|
|
37
|
+
},
|
|
38
|
+
"client": {
|
|
39
|
+
"platform": "web",
|
|
40
|
+
"inject": [
|
|
41
|
+
"@deepseek-ai/dsh-client-runtime",
|
|
42
|
+
"@deepseek-ai/dsh-client-ui-slots"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"deepseek-harness",
|
|
48
|
+
"dsh",
|
|
49
|
+
"cordis",
|
|
50
|
+
"plugin",
|
|
51
|
+
"memory",
|
|
52
|
+
"long-term-memory"
|
|
53
|
+
],
|
|
54
|
+
"license": "MIT"
|
|
55
|
+
}
|