dsh-engramory 1.0.6 → 1.0.7
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.en.md +5 -0
- package/README.md +2 -0
- package/index.js +169 -4
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -24,6 +24,10 @@ which is general-purpose agent memory: Claude Code, Codex and others can use the
|
|
|
24
24
|
compact; compaction still passes. Details in [How the cap decides](#how-the-cap-decides).
|
|
25
25
|
- **The rules come with it.** What to save, how, and when to clean up: the rules
|
|
26
26
|
(a skill) are registered by the plugin, with no files to install.
|
|
27
|
+
- **A reminder to save before dsh compacts.** When a conversation grows long, dsh
|
|
28
|
+
compacts what came before into a summary, which need not keep what is worth
|
|
29
|
+
remembering later. As a conversation nears that point, the plugin reminds the AI to
|
|
30
|
+
save that first; the AI is reminded, not you.
|
|
27
31
|
- **A Memory page in the sidebar.** Read-only; it lists the notes by name and opens
|
|
28
32
|
one in your computer's default app, with a check-up that flags broken links in the
|
|
29
33
|
index and notes left out of it.
|
|
@@ -172,6 +176,7 @@ caps in force. A patch replaces the row's whole `config`, so list every key you
|
|
|
172
176
|
| `indexPath` | unset | The one index to use: absolute, or relative to the session's directory. Unset, a `MEMORY.md` in any project is capped as an index; a store outside the default places also needs it to be found. |
|
|
173
177
|
| `registerSkill` | `true` | `false` skips the built-in rules; the cap and the index in context stay. |
|
|
174
178
|
| `injectIndex` | `true` | `false` stops putting the index into context; the AI reads it instead. |
|
|
179
|
+
| `compactionReminder` | `true` | `false` stops the reminder to save before dsh compacts. The plugin works out where dsh will compact from dsh's own compaction settings (on dsh 0.2 with DeepSeek's defaults, about 69% of the window, not 80%) and reminds at 85% of that. A best effort: a conversation that grows past the gap in one step goes unreminded; with no compaction, or automatic compaction off, there is no reminder. |
|
|
175
180
|
| `language` | dsh's | `zh` (or `zh-CN` and other regional forms) writes the store in Chinese: note names, headings such as `## 用户`, the Why/How labels. Any other string is English; a value that is not a string, or is empty, counts as unset. Unset, it follows dsh's language; a change applies once dsh restarts. Frontmatter keys and `type:` values stay English, so every host reads the store. |
|
|
176
181
|
| `companion` | `false` | `true` turns companion mode on. |
|
|
177
182
|
| `audience` | `private` | With companion mode on, `social` turns social mode on. |
|
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
- **每轮自动带上记忆索引。** 索引 `MEMORY.md` 只列笔记名字,插件每一步都把它放进上下文,AI 不用先读文件就知道自己记过什么,要细节再打开那条笔记。写入后下一轮就能看到;超出上限的部分不带上,并提醒 AI 压缩。
|
|
14
14
|
- **索引有硬上限。** 默认 200 行 / 25 KB。AI 用 dsh 的文件工具写索引时,只要插件算得出写完的样子,会超上限就在写入前拒绝,让它先压缩;压缩照样放行。细节见[上限怎么判](#上限怎么判)。
|
|
15
15
|
- **自带记忆规则。** 记什么、怎么记、什么时候清理,这套规则(skill)随插件注册,不用另外装文件。
|
|
16
|
+
- **快压缩时提醒 AI 存记忆。** dsh 会在对话太长时把前面压缩成摘要,摘要不一定留得下以后还用得着的东西。对话快到 dsh 要压缩的地方时,插件提醒 AI 把值得长期记住的先存进记忆;提醒的是 AI,不打扰你。
|
|
16
17
|
- **侧栏多一个「记忆」页。** 只读,列出笔记名字,点名字用电脑上的默认程序打开;顺带体检,标出索引里的坏链接和没进索引的笔记。
|
|
17
18
|
|
|
18
19
|
## 安装
|
|
@@ -98,6 +99,7 @@ agent.followup(createUserMessage({
|
|
|
98
99
|
| `indexPath` | 不设 | 只认这一个索引:绝对路径,或相对会话目录。不设时,任何项目里叫 `MEMORY.md` 的文件都按索引卡上限;记忆库不在默认位置时也要设它,否则不会自动带上。 |
|
|
99
100
|
| `registerSkill` | `true` | `false`:不注册内置的记忆规则;上限和每轮带上索引照旧。 |
|
|
100
101
|
| `injectIndex` | `true` | `false`:不再每轮带上索引,改由 AI 自己读。 |
|
|
102
|
+
| `compactionReminder` | `true` | `false`:快压缩时不再提醒 AI 存记忆。插件照 dsh 的压缩设置算出它会在哪压缩(dsh 0.2 加 DeepSeek 默认约在窗口的 69%,不是 80%),到那一点的 85% 时提醒。尽力而为:一步就涨过这段余量的对话等不到提醒;没装压缩或关了自动压缩时不提醒。 |
|
|
101
103
|
| `language` | 跟随 dsh | `zh`(`zh-CN` 等也算)用中文写记忆(笔记名、`## 用户` 这类标题、**为什么:**/**怎么用:**),其他字符串用英文;不是字符串或为空,当作没设。不设就跟 dsh 的语言,改了要重启 dsh。frontmatter 的键和 `type:` 取值始终是英文,别的宿主照样能读。 |
|
|
102
104
|
| `companion` | `false` | `true` 打开大肥鱼模式。 |
|
|
103
105
|
| `audience` | `private` | 大肥鱼模式下设 `social` 打开社交模式。 |
|
package/index.js
CHANGED
|
@@ -265,9 +265,10 @@ export function apply(ctx, config = {}) {
|
|
|
265
265
|
// the store is closed — seen on a real dsh.
|
|
266
266
|
const injectIndex = settings.injectIndex
|
|
267
267
|
const guarded = settings.companion || settings.social
|
|
268
|
+
const contexts = []
|
|
268
269
|
if (injectIndex || guarded) {
|
|
269
270
|
const index = injectIndex ? indexRecall(settings, sessionCwd) : () => ''
|
|
270
|
-
|
|
271
|
+
contexts.push(...guarded ? [{
|
|
271
272
|
name: 'engramory:memory',
|
|
272
273
|
required: true,
|
|
273
274
|
text: (context) => {
|
|
@@ -293,14 +294,47 @@ export function apply(ctx, config = {}) {
|
|
|
293
294
|
const rules = settings.companion ? settings.text.companion : ''
|
|
294
295
|
return [shown, rules].filter(Boolean).join('\n\n')
|
|
295
296
|
},
|
|
296
|
-
}] : [{ name: 'engramory:memory-index', text: index }]
|
|
297
|
+
}] : [{ name: 'engramory:memory-index', text: index }])
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Before dsh compacts a conversation into a summary (dsh-compaction-basic, before the step's
|
|
301
|
+
// request goes out; where, compactionReminder() works out), the model is told to save what
|
|
302
|
+
// is worth keeping beyond it — the summary keeps what the conversation needs, not what
|
|
303
|
+
// memory would. The model, not the user, is told: saving is its part. A section of its
|
|
304
|
+
// own, so the index's text does not change with it; required where the memory context is
|
|
305
|
+
// (a persona preset may drop optional context), and in social mode only for the user's
|
|
306
|
+
// own conversations, the only ones that may save.
|
|
307
|
+
if (config.compactionReminder !== false) {
|
|
308
|
+
const pressure = {}
|
|
309
|
+
for (const [service, key] of [['tokenMeter', 'meter'], ['sessionProjections', 'projections'],
|
|
310
|
+
['compaction', 'compaction'], ['llm', 'llm']]) {
|
|
311
|
+
ctx.inject([service], (inner) => {
|
|
312
|
+
inner.effect(() => {
|
|
313
|
+
const current = inner[service]
|
|
314
|
+
pressure[key] = current
|
|
315
|
+
return () => {
|
|
316
|
+
if (pressure[key] === current) pressure[key] = undefined
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
}
|
|
321
|
+
const reminder = compactionReminder(settings, pressure)
|
|
322
|
+
contexts.push({
|
|
323
|
+
name: 'engramory:compaction',
|
|
324
|
+
order: INDEX_CONTEXT_ORDER + 1,
|
|
325
|
+
...(guarded ? { required: true } : {}),
|
|
326
|
+
text: (context) => (roleOf(context?.agent) === 'owner' ? reminder(context) : ''),
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (contexts.length > 0) {
|
|
297
331
|
ctx.inject(['systemPrompt'], (inner) => {
|
|
298
332
|
const prompt = inner.systemPrompt
|
|
299
333
|
if (typeof prompt?.context !== 'function') return
|
|
300
|
-
for (const { name, required, text } of contexts) {
|
|
334
|
+
for (const { name, order, required, text } of contexts) {
|
|
301
335
|
inner.effect(() => prompt.context({
|
|
302
336
|
name,
|
|
303
|
-
order: INDEX_CONTEXT_ORDER,
|
|
337
|
+
order: order ?? INDEX_CONTEXT_ORDER,
|
|
304
338
|
// The index is quoted, not templated: dsh interpolates `{{name}}` groups and
|
|
305
339
|
// throws on a malformed one, which would fail the whole request.
|
|
306
340
|
interpolate: false,
|
|
@@ -1623,6 +1657,129 @@ function realpathOr(p) {
|
|
|
1623
1657
|
}
|
|
1624
1658
|
}
|
|
1625
1659
|
|
|
1660
|
+
/**
|
|
1661
|
+
* dsh-compaction-basic's default ratio, for an engine whose config cannot be read. From
|
|
1662
|
+
* dsh 0.1.7 the engine compacts a conversation past `min(window × ratio, window − the
|
|
1663
|
+
* output the request reserves − headroom)` — on dsh 0.2 with DeepSeek's defaults (1M
|
|
1664
|
+
* window, 256K reserved, 64K headroom) about 0.69 of the window, not 0.8; dsh 0.1.5's
|
|
1665
|
+
* past `window × ratio`, its config having no headroom.
|
|
1666
|
+
*/
|
|
1667
|
+
const COMPACT_RATIO = 0.8
|
|
1668
|
+
/** dsh-compaction-basic's default share of the budget a compaction keeps whole at the end. */
|
|
1669
|
+
const COMPACT_RETAIN = 0.16
|
|
1670
|
+
/**
|
|
1671
|
+
* Where the reminder comes on, as a share of the point dsh will compact at — early enough
|
|
1672
|
+
* that a few large steps still leave the model a turn to save — and below which it goes:
|
|
1673
|
+
* the gap keeps a conversation hovering near one figure from switching it on and off, each
|
|
1674
|
+
* switch sending the runtime context, index and all, once more. After a compaction the
|
|
1675
|
+
* conversation is far below either.
|
|
1676
|
+
*/
|
|
1677
|
+
const REMIND_AT = 0.85
|
|
1678
|
+
const REMIND_UNTIL = 0.7
|
|
1679
|
+
/** How long a model's default output cap is waited for, and how soon a failed ask is retried. */
|
|
1680
|
+
const MODEL_INFO_MS = 30000
|
|
1681
|
+
const MODEL_INFO_RETRY_MS = 60000
|
|
1682
|
+
|
|
1683
|
+
/**
|
|
1684
|
+
* A conversation's reminder to save before dsh compacts it, or '' — per step, synchronously,
|
|
1685
|
+
* as dsh asks for runtime context. It works out where dsh-compaction-basic will compact the
|
|
1686
|
+
* conversation as that engine does (resolveCompactSpec): its own threshold ratio and
|
|
1687
|
+
* headroom (from the engine's config, a model's own policy first), the window the latest
|
|
1688
|
+
* request recorded (the `contextPressure` projection), and the output that request
|
|
1689
|
+
* reserves — its own cap, else the model's default, else none; the default is asked of dsh
|
|
1690
|
+
* once per model in the background (llm.resolveModelInfo, as the engine asks), so until
|
|
1691
|
+
* dsh has said there is no reminder. The conversation's size is the engine's own measure
|
|
1692
|
+
* (`tokenMeter.measure()`).
|
|
1693
|
+
*
|
|
1694
|
+
* Where dsh would not compact there is no reminder — the model is never told of a
|
|
1695
|
+
* compaction that will not come: no engine, one that does not compact on its own
|
|
1696
|
+
* (`auto: false`), a conversation with no routed model yet, no measure or window, a window
|
|
1697
|
+
* too small to leave room. The text is one fixed sentence, never a figure: dsh sends the
|
|
1698
|
+
* runtime context again whenever it changes. On dsh 0.1 the context is taken once per step,
|
|
1699
|
+
* before compaction runs, so the step that compacts still carries the reminder it began with.
|
|
1700
|
+
*/
|
|
1701
|
+
function compactionReminder(settings, pressure) {
|
|
1702
|
+
const reminded = new WeakMap()
|
|
1703
|
+
// `provider/model` -> { reserve } once dsh has said, { asked } while it is asked or failed;
|
|
1704
|
+
// kept for one model registry only, so a reloaded one is asked again
|
|
1705
|
+
let reserves = new Map()
|
|
1706
|
+
let askedOf
|
|
1707
|
+
const defaultReserve = (provider, model) => {
|
|
1708
|
+
const llm = pressure.llm
|
|
1709
|
+
if (typeof llm?.resolveModelInfo !== 'function') return undefined
|
|
1710
|
+
if (askedOf !== llm) {
|
|
1711
|
+
askedOf = llm
|
|
1712
|
+
reserves = new Map()
|
|
1713
|
+
}
|
|
1714
|
+
const key = JSON.stringify([provider, model]) // a provider may have a `/` in its name
|
|
1715
|
+
const known = reserves.get(key)
|
|
1716
|
+
if (known?.reserve !== undefined) return known.reserve
|
|
1717
|
+
if (known !== undefined && Date.now() - known.asked < MODEL_INFO_RETRY_MS) return undefined
|
|
1718
|
+
const entry = { asked: Date.now() }
|
|
1719
|
+
const cache = reserves
|
|
1720
|
+
cache.set(key, entry)
|
|
1721
|
+
Promise.resolve()
|
|
1722
|
+
.then(() => llm.resolveModelInfo(provider, model, AbortSignal.timeout(MODEL_INFO_MS)))
|
|
1723
|
+
.then((info) => {
|
|
1724
|
+
// as the engine has it: the model's default, else nothing reserved
|
|
1725
|
+
if (cache.get(key) === entry) entry.reserve = count(info?.defaultMaxTokens) ? info.defaultMaxTokens : 0
|
|
1726
|
+
})
|
|
1727
|
+
.catch(() => {})
|
|
1728
|
+
return undefined
|
|
1729
|
+
}
|
|
1730
|
+
return (context) => {
|
|
1731
|
+
const session = context?.agent?.session
|
|
1732
|
+
if (session === null || typeof session !== 'object') return ''
|
|
1733
|
+
const engine = pressure.compaction
|
|
1734
|
+
let config
|
|
1735
|
+
let route
|
|
1736
|
+
let tokens
|
|
1737
|
+
let window
|
|
1738
|
+
try {
|
|
1739
|
+
config = engine?.config
|
|
1740
|
+
if (engine === undefined || config?.auto === false) return ''
|
|
1741
|
+
route = session.requestHeader?.()?.config
|
|
1742
|
+
// the engine compacts only a conversation routed to a model
|
|
1743
|
+
if (typeof route?.provider !== 'string' || !route.provider ||
|
|
1744
|
+
typeof route.model !== 'string' || !route.model) return ''
|
|
1745
|
+
tokens = pressure.meter?.measure(session)?.totalTokens
|
|
1746
|
+
window = pressure.projections?.stateOf(session, 'contextPressure')?.contextWindow
|
|
1747
|
+
} catch {
|
|
1748
|
+
return ''
|
|
1749
|
+
}
|
|
1750
|
+
if (!Number.isFinite(tokens) || !count(window) || window === 0) return ''
|
|
1751
|
+
const own = Array.isArray(config?.modelPolicies)
|
|
1752
|
+
? config.modelPolicies.find((p) => p?.provider === route.provider && p?.model === route.model)
|
|
1753
|
+
: undefined
|
|
1754
|
+
const ratio = [own?.thresholdRatio, config?.thresholdRatio]
|
|
1755
|
+
.find((r) => Number.isFinite(r) && r > 0 && r <= 1) ?? COMPACT_RATIO
|
|
1756
|
+
// dsh 0.1.7 on: the window less the output reserved and the headroom caps it too
|
|
1757
|
+
const headroom = [own?.headroomTokens, config?.headroomTokens].find(count)
|
|
1758
|
+
let at = window * ratio
|
|
1759
|
+
let budget = window // what the kept tail is a share of: the window, less the output reserved from 0.1.7
|
|
1760
|
+
if (headroom !== undefined) {
|
|
1761
|
+
const reserve = count(route.maxTokens) ? route.maxTokens : defaultReserve(route.provider, route.model)
|
|
1762
|
+
if (reserve === undefined) return ''
|
|
1763
|
+
budget = window - reserve
|
|
1764
|
+
at = Math.min(at, budget - headroom)
|
|
1765
|
+
}
|
|
1766
|
+
at = Math.floor(at)
|
|
1767
|
+
if (!(at > 0)) return ''
|
|
1768
|
+
// the tail a compaction keeps whole (resolveRetention: a model's own first, either kind);
|
|
1769
|
+
// one no smaller than the point to compact at is a budget the engine refuses to work with
|
|
1770
|
+
const retention = [own, config].find((p) => p?.retainTokens !== undefined || p?.retainRatio !== undefined)
|
|
1771
|
+
const retain = count(retention?.retainTokens) ? retention.retainTokens
|
|
1772
|
+
: Math.floor(budget * (Number.isFinite(retention?.retainRatio) ? retention.retainRatio : COMPACT_RETAIN))
|
|
1773
|
+
if (retain >= at) return ''
|
|
1774
|
+
const on = tokens >= at * (reminded.get(session) === true ? REMIND_UNTIL : REMIND_AT)
|
|
1775
|
+
reminded.set(session, on)
|
|
1776
|
+
return on ? settings.text.compactionSoon : ''
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
/** A whole number of tokens: an integer, not negative. */
|
|
1781
|
+
const count = (n) => Number.isInteger(n) && n >= 0
|
|
1782
|
+
|
|
1626
1783
|
/** How long a directory's walk up to its project root, or a shown index, is trusted. */
|
|
1627
1784
|
const RECHECK_MS = 10000
|
|
1628
1785
|
|
|
@@ -2506,6 +2663,11 @@ const TEXT = {
|
|
|
2506
2663
|
'it, or the user never has in dsh\'s own window — so no memory is loaded here and the ' +
|
|
2507
2664
|
'memory store is closed to file tools. Do not recall or save anything about the user ' +
|
|
2508
2665
|
'in it.',
|
|
2666
|
+
compactionSoon:
|
|
2667
|
+
'Engramory: this conversation is close to its context limit, and dsh will soon compact ' +
|
|
2668
|
+
'it into a summary. If anything in it is worth keeping beyond this conversation and is ' +
|
|
2669
|
+
'not in memory yet, save it now, as the Engramory skill describes. No need to tell the ' +
|
|
2670
|
+
'user.',
|
|
2509
2671
|
strangerRefusal:
|
|
2510
2672
|
'This conversation is not the user\'s own (Engramory social mode: someone else has ' +
|
|
2511
2673
|
'written in it, or the user never has in dsh\'s own window), so the memory store is ' +
|
|
@@ -2562,6 +2724,9 @@ const TEXT = {
|
|
|
2562
2724
|
'Engramory:这个会话不是用户本人的——有别人在里面说过话,或者用户从没在 dsh 窗口里' +
|
|
2563
2725
|
'亲手发过消息——所以这里不加载记忆,文件工具也碰不到记忆库。不要在这里回忆或保存关于' +
|
|
2564
2726
|
'用户的任何事。',
|
|
2727
|
+
compactionSoon:
|
|
2728
|
+
'Engramory:这段对话快到上下文上限了,dsh 很快会把它压缩成摘要。里面如果有值得长期' +
|
|
2729
|
+
'记住、还没存进记忆的东西,现在照 Engramory skill 存好。不用特意告诉用户。',
|
|
2565
2730
|
strangerRefusal:
|
|
2566
2731
|
'这个会话不是用户本人的(Engramory 社交模式:有别人在里面说过话,或者用户从没在 dsh ' +
|
|
2567
2732
|
'窗口里亲手发过消息),记忆库对它关闭。',
|