dsh-context-graph 0.3.0 → 0.6.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/lib/client.js CHANGED
@@ -14,16 +14,24 @@ window.__ModuleLoader__.load({
14
14
  const NAMESPACE = 'dsh-context-graph'
15
15
  const en = {
16
16
  stats: 'Context graph',
17
+ on: 'Memory on',
18
+ off: 'Memory off',
19
+ share: 'Share with workspace',
20
+ unshare: 'Stop sharing',
17
21
  build: 'Index this chat',
18
- fold: 'Fold old turns',
22
+ map: 'Memory map',
19
23
  find: 'Find topics',
20
24
  running: 'Working…',
21
25
  failed: 'Failed'
22
26
  }
23
27
  const zh = {
24
28
  stats: '上下文图谱',
29
+ on: '开启记忆',
30
+ off: '关闭记忆',
31
+ share: '共享到工作区',
32
+ unshare: '取消共享',
25
33
  build: '索引当前对话',
26
- fold: '折叠旧轮次',
34
+ map: '记忆地图',
27
35
  find: '查找主题',
28
36
  running: '处理中…',
29
37
  failed: '失败'
@@ -60,9 +68,9 @@ window.__ModuleLoader__.load({
60
68
  ]
61
69
  })
62
70
 
63
- const SUBCOMMANDS = ['stats', 'build', 'fold', 'find']
71
+ const SUBCOMMANDS = ['stats', 'on', 'off', 'share', 'unshare', 'build', 'map', 'find']
64
72
 
65
- /** Title key from the typed arguments (`/context-graph fold` -> fold); no argument means stats. */
73
+ /** Title key from the typed arguments (`/context-graph on` -> on); no argument means stats. */
66
74
  function subcommandOf(args) {
67
75
  const first = String(args ?? '').trim().split(/\s+/)[0] || 'stats'
68
76
  return SUBCOMMANDS.includes(first) ? first : 'stats'
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Context Graph",
4
- "description": "Remembers past chats as a small topic graph and folds old turns of a long chat into a table of contents, so fewer input tokens are sent. Exact text is recalled on demand."
4
+ "description": "Memory for every chat, on by default; switch this plugin off here to stop it everywhere, or run /context-graph off in one chat. Other chats in the workspace recall exact past text by topic, chat or day instead of re-sending it, so fewer input tokens go out."
5
5
  }
6
6
  }
package/locale/zh.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "上下文图谱",
4
- "description": "把过往对话记成一张小型主题图谱,并把长对话的旧轮次折叠成目录,从而减少发送的输入 token;需要时按需取回原文。",
4
+ "description": "每个对话默认开启记忆;在这里关闭本插件即可全部停用,或在某个对话里运行 /context-graph off 只停用该对话。工作区里的其他对话可按主题、对话或日期取回过往原文,而不是把旧消息重复发送,从而减少输入 token。",
5
5
  "detail": "README.zh.md"
6
6
  }
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-context-graph",
3
- "version": "0.3.0",
4
- "description": "DeepSeek Harness plugin: remembers past conversations as a small graph and folds old turns of a long chat into a table of contents, so fewer input tokens are sent; exact text is recalled on demand",
3
+ "version": "0.6.0",
4
+ "description": "DeepSeek Harness plugin: memory graphs for every chat, on by default (switch the plugin off on the Plugins page to stop); other chats in the workspace recall exact past text by topic, chat or day, so fewer input tokens are re-sent",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",
7
7
  "main": "index.js",
@@ -26,7 +26,7 @@
26
26
  "LICENSE"
27
27
  ],
28
28
  "scripts": {
29
- "test": "node --import ./test/resolve-dsh.mjs --test test/graph.test.js test/fold.test.js test/update.test.js test/client.test.js test/plugin.test.js"
29
+ "test": "node --import ./test/resolve-dsh.mjs --test test/graph.test.js test/update.test.js test/client.test.js test/plugin.test.js"
30
30
  },
31
31
  "keywords": [
32
32
  "dsh-plugin",
package/store.js CHANGED
@@ -1,44 +1,79 @@
1
1
  /**
2
- * On-disk layout under `<dir>` (default `<workspace>/.context-graph`):
3
- * graph.json the graph (see graph.js)
4
- * src/<id>.md verbatim source text of one node, one section appended per build
2
+ * On-disk layout under `<dir>` (default `<workspace>/.context-graph`), one folder per chat:
3
+ * chats/<sessionId>/meta.json { cid, title, workspace, startedAt, lastActiveAt, turns, memoryOff?, private? }
4
+ * chats/<sessionId>/graph.json the chat's graph (see graph.js)
5
+ * chats/<sessionId>/src/<id>.md verbatim source text of one node, one section appended per build
6
+ * Nothing is merged or copied between chats; the list of chats is the list of folders.
5
7
  * ponytail: last writer wins across concurrent sessions; add a lock file if two agents share a workspace.
6
8
  */
7
9
 
8
- import { mkdir, readFile, rename, writeFile, appendFile } from 'node:fs/promises'
10
+ import { mkdir, readdir, readFile, rename, writeFile, appendFile } from 'node:fs/promises'
9
11
  import { join } from 'node:path'
10
12
 
11
13
  import { emptyGraph, GRAPH_VERSION } from './graph.js'
12
14
 
13
- export async function loadGraph(dir) {
15
+ export const chatDir = (dir, sid) => join(dir, 'chats', String(sid))
16
+
17
+ async function readJson(path) {
14
18
  try {
15
- const graph = JSON.parse(await readFile(join(dir, 'graph.json'), 'utf8'))
16
- if (graph?.version !== GRAPH_VERSION || typeof graph.nodes !== 'object' || !Array.isArray(graph.edges)) return emptyGraph()
17
- return graph
19
+ return JSON.parse(await readFile(path, 'utf8'))
18
20
  } catch (error) {
19
- if (error?.code === 'ENOENT') return emptyGraph()
21
+ if (error?.code === 'ENOENT') return undefined
20
22
  throw error
21
23
  }
22
24
  }
23
25
 
24
- export async function saveGraph(dir, graph) {
25
- await mkdir(dir, { recursive: true })
26
- const target = join(dir, 'graph.json')
27
- const tmp = `${target}.${process.pid}.tmp`
28
- await writeFile(tmp, JSON.stringify(graph, null, 1), 'utf8')
29
- await rename(tmp, target)
26
+ async function writeJson(path, value) {
27
+ await mkdir(join(path, '..'), { recursive: true })
28
+ const tmp = `${path}.${process.pid}.tmp`
29
+ await writeFile(tmp, JSON.stringify(value, null, 1), 'utf8')
30
+ await rename(tmp, path)
31
+ }
32
+
33
+ export async function loadGraph(chat) {
34
+ const graph = await readJson(join(chat, 'graph.json'))
35
+ if (graph?.version !== GRAPH_VERSION || typeof graph.nodes !== 'object' || !Array.isArray(graph.edges)) return emptyGraph()
36
+ return graph
37
+ }
38
+
39
+ export const saveGraph = (chat, graph) => writeJson(join(chat, 'graph.json'), graph)
40
+
41
+ export const loadMeta = (dir, sid) => readJson(join(chatDir(dir, sid), 'meta.json'))
42
+
43
+ export const saveMeta = (dir, sid, meta) => writeJson(join(chatDir(dir, sid), 'meta.json'), meta)
44
+
45
+ /** Every chat folder with a readable meta.json, newest activity first. */
46
+ export async function listChats(dir) {
47
+ let names = []
48
+ try { names = await readdir(join(dir, 'chats')) } catch (error) { if (error?.code !== 'ENOENT') throw error }
49
+ const chats = []
50
+ for (const sid of names) {
51
+ const meta = await loadMeta(dir, sid)
52
+ if (meta) chats.push({ sid, meta })
53
+ }
54
+ return chats.sort((a, b) => String(b.meta.lastActiveAt ?? '').localeCompare(String(a.meta.lastActiveAt ?? '')))
55
+ }
56
+
57
+ /** The chat's meta, created on first sight with the next free chat number. */
58
+ export async function ensureMeta(dir, sid, { title, workspace, now }) {
59
+ const existing = await loadMeta(dir, sid)
60
+ if (existing) return existing
61
+ const cid = (await listChats(dir)).reduce((max, c) => Math.max(max, c.meta.cid ?? 0), 0) + 1
62
+ const meta = { cid, title, workspace, startedAt: now, lastActiveAt: now, turns: 0 }
63
+ await saveMeta(dir, sid, meta)
64
+ return meta
30
65
  }
31
66
 
32
67
  /** Append one build's verbatim messages to the node's source file. */
33
- export async function appendSource(dir, id, { session, turn, at, messages }) {
34
- await mkdir(join(dir, 'src'), { recursive: true })
68
+ export async function appendSource(chat, id, { session, turn, at, messages }) {
69
+ await mkdir(join(chat, 'src'), { recursive: true })
35
70
  const body = messages.map((m) => `**${m.role}** (seq ${m.seq}):\n\n${m.text}`).join('\n\n')
36
- await appendFile(join(dir, 'src', `${id}.md`), `## ${at} session ${session} turn ${turn}\n\n${body}\n\n`, 'utf8')
71
+ await appendFile(join(chat, 'src', `${id}.md`), `## ${at} session ${session} turn ${turn}\n\n${body}\n\n`, 'utf8')
37
72
  }
38
73
 
39
- export async function readSource(dir, id) {
74
+ export async function readSource(chat, id) {
40
75
  try {
41
- return await readFile(join(dir, 'src', `${id}.md`), 'utf8')
76
+ return await readFile(join(chat, 'src', `${id}.md`), 'utf8')
42
77
  } catch (error) {
43
78
  if (error?.code === 'ENOENT') return undefined
44
79
  throw error