dsh-engramory 1.0.6 → 1.0.8

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.
Files changed (5) hide show
  1. package/README.en.md +242 -228
  2. package/README.md +141 -135
  3. package/client.js +103 -1
  4. package/index.js +169 -4
  5. package/package.json +79 -79
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
- const contexts = guarded ? [{
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
  '窗口里亲手发过消息),记忆库对它关闭。',
package/package.json CHANGED
@@ -1,79 +1,79 @@
1
- {
2
- "name": "dsh-engramory",
3
- "version": "1.0.6",
4
- "description": "DeepSeek Harness 的长期记忆插件:AI 记住的东西存成普通 markdown 文件,每轮自动带上记忆索引,索引有大小上限。",
5
- "keywords": [
6
- "dsh-plugin",
7
- "deepseek-harness",
8
- "dsh",
9
- "memory",
10
- "agent-memory",
11
- "long-term-memory",
12
- "engramory",
13
- "markdown"
14
- ],
15
- "type": "module",
16
- "main": "index.js",
17
- "dsh": {
18
- "bundle": {
19
- "patch": "./cordis.patch.yml"
20
- },
21
- "client": {
22
- "platform": "web",
23
- "inject": [
24
- "@deepseek-ai/dsh-client-locale",
25
- "@deepseek-ai/dsh-client-ui-plugin-manager"
26
- ]
27
- },
28
- "compatibility": {
29
- "dsh": ">=0.1.0-rc.7 <0.3.0-0",
30
- "dshReleases": {
31
- "0.1.5-rc.3": "compatible",
32
- "0.1.7-alpha.1": "compatible",
33
- "0.1.7-alpha.2": "compatible",
34
- "0.1.7-rc.1": "compatible",
35
- "0.1.7-rc.2": "compatible",
36
- "0.2.1-alpha.5": "compatible",
37
- "0.2.1-rc.1": "compatible",
38
- "0.2.1-rc.2": "compatible",
39
- "0.2.1-rc.3": "compatible",
40
- "0.2.1-rc.4": "compatible",
41
- "0.2.1-rc.5": "compatible",
42
- "0.2.2-alpha.2": "compatible"
43
- }
44
- }
45
- },
46
- "exports": {
47
- ".": "./index.js",
48
- "./client": "./client.js",
49
- "./locale/*.json": "./locale/*.json",
50
- "./icon": "./icon.svg",
51
- "./package.json": "./package.json"
52
- },
53
- "files": [
54
- "index.js",
55
- "client.js",
56
- "cordis.patch.yml",
57
- "locale/*.json",
58
- "icon.svg",
59
- "README.md",
60
- "LICENSE"
61
- ],
62
- "author": "tinqiao-oss",
63
- "license": "MIT",
64
- "repository": {
65
- "type": "git",
66
- "url": "git+https://github.com/tinqiao-oss/engramory.git",
67
- "directory": "adapters/dsh/plugin"
68
- },
69
- "homepage": "https://github.com/tinqiao-oss/engramory/tree/master/adapters/dsh/plugin#readme",
70
- "bugs": {
71
- "url": "https://github.com/tinqiao-oss/engramory/issues"
72
- },
73
- "engines": {
74
- "node": ">=18"
75
- },
76
- "publishConfig": {
77
- "access": "public"
78
- }
79
- }
1
+ {
2
+ "name": "dsh-engramory",
3
+ "version": "1.0.8",
4
+ "description": "DeepSeek Harness 的长期记忆插件:AI 记住的东西存成普通 markdown 文件,每轮自动带上记忆索引,索引有大小上限。",
5
+ "keywords": [
6
+ "dsh-plugin",
7
+ "deepseek-harness",
8
+ "dsh",
9
+ "memory",
10
+ "agent-memory",
11
+ "long-term-memory",
12
+ "engramory",
13
+ "markdown"
14
+ ],
15
+ "type": "module",
16
+ "main": "index.js",
17
+ "dsh": {
18
+ "bundle": {
19
+ "patch": "./cordis.patch.yml"
20
+ },
21
+ "client": {
22
+ "platform": "web",
23
+ "inject": [
24
+ "@deepseek-ai/dsh-client-locale",
25
+ "@deepseek-ai/dsh-client-ui-plugin-manager"
26
+ ]
27
+ },
28
+ "compatibility": {
29
+ "dsh": ">=0.1.0-rc.7 <0.3.0-0",
30
+ "dshReleases": {
31
+ "0.1.5-rc.3": "compatible",
32
+ "0.1.7-alpha.1": "compatible",
33
+ "0.1.7-alpha.2": "compatible",
34
+ "0.1.7-rc.1": "compatible",
35
+ "0.1.7-rc.2": "compatible",
36
+ "0.2.1-alpha.5": "compatible",
37
+ "0.2.1-rc.1": "compatible",
38
+ "0.2.1-rc.2": "compatible",
39
+ "0.2.1-rc.3": "compatible",
40
+ "0.2.1-rc.4": "compatible",
41
+ "0.2.1-rc.5": "compatible",
42
+ "0.2.2-alpha.2": "compatible"
43
+ }
44
+ }
45
+ },
46
+ "exports": {
47
+ ".": "./index.js",
48
+ "./client": "./client.js",
49
+ "./locale/*.json": "./locale/*.json",
50
+ "./icon": "./icon.svg",
51
+ "./package.json": "./package.json"
52
+ },
53
+ "files": [
54
+ "index.js",
55
+ "client.js",
56
+ "cordis.patch.yml",
57
+ "locale/*.json",
58
+ "icon.svg",
59
+ "README.md",
60
+ "LICENSE"
61
+ ],
62
+ "author": "tinqiao-oss",
63
+ "license": "MIT",
64
+ "repository": {
65
+ "type": "git",
66
+ "url": "git+https://github.com/tinqiao-oss/engramory.git",
67
+ "directory": "adapters/dsh/plugin"
68
+ },
69
+ "homepage": "https://github.com/tinqiao-oss/engramory/tree/master/adapters/dsh/plugin#readme",
70
+ "bugs": {
71
+ "url": "https://github.com/tinqiao-oss/engramory/issues"
72
+ },
73
+ "engines": {
74
+ "node": ">=18"
75
+ },
76
+ "publishConfig": {
77
+ "access": "public"
78
+ }
79
+ }