dsh-context-graph 0.6.2 → 0.11.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/CHANGELOG.md +563 -0
- package/README.md +252 -97
- package/README.zh.md +82 -32
- package/cordis.patch.yml +10 -6
- package/index.js +7 -432
- package/package.json +11 -12
- package/src/client/client.js +880 -0
- package/src/core/builder.js +210 -0
- package/src/core/chat.js +34 -0
- package/src/core/graph.js +134 -0
- package/src/core/index.js +14 -0
- package/src/core/ledger.js +108 -0
- package/src/core/match.js +377 -0
- package/src/core/render.js +195 -0
- package/src/core/session.js +446 -0
- package/src/core/text.js +220 -0
- package/src/core/time.js +147 -0
- package/src/core/tree.js +39 -0
- package/src/host/builder.js +416 -0
- package/src/host/command.js +155 -0
- package/src/host/config.js +50 -0
- package/src/host/memory.js +175 -0
- package/src/host/meter.js +75 -0
- package/src/host/page.js +186 -0
- package/src/host/plugin.js +46 -0
- package/src/host/recall.js +227 -0
- package/src/host/runtime.js +599 -0
- package/src/store/chats.js +236 -0
- package/src/store/files.js +181 -0
- package/src/store/ledger.js +65 -0
- package/src/store/messages.js +88 -0
- package/src/store/migrate.js +143 -0
- package/graph.js +0 -496
- package/lib/client.js +0 -126
- package/store.js +0 -81
- /package/{update.js → src/host/update.js} +0 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Upgrades of stored memory from older versions. Non-destructive by design (#12, #17 and the
|
|
3
|
+
* cycle-1 review):
|
|
4
|
+
*
|
|
5
|
+
* v0.4-v0.6 chat folders hold a version-1 `graph.json` whose topics each own `src/<id>.md`: a list
|
|
6
|
+
* of sections "## <at> session <sid> turn <turn>" with "**user** (seq N):" / "**assistant** (seq N):"
|
|
7
|
+
* messages. The upgrade writes the version-2 graph to `topics.json` and the messages, each once, to
|
|
8
|
+
* `msgs.json`. It never writes `graph.json` or `src/`: they stay as a read-only backup, so an older
|
|
9
|
+
* copy of the plugin still running in another dsh process keeps its own file instead of wiping the
|
|
10
|
+
* new one, and topics it adds later are merged in on the next load. The backup is removed once it has
|
|
11
|
+
* been quiet for BACKUP_DAYS.
|
|
12
|
+
*
|
|
13
|
+
* Seqs are positions in one dsh session's log, so they only identify a message within that session.
|
|
14
|
+
* Messages from any other session (a v0.1-v0.3 workspace graph covered many) get their own keys from
|
|
15
|
+
* FOREIGN_BASE up, remembered in msgs.json under `k` so a second run reuses them.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { readFile, readdir, rm, stat } from 'node:fs/promises'
|
|
19
|
+
import { join } from 'node:path'
|
|
20
|
+
|
|
21
|
+
import { GRAPH_VERSION, emptyGraph, has, normalizeGraph } from '../core/graph.js'
|
|
22
|
+
import { locked, readJson, writeJson } from './files.js'
|
|
23
|
+
|
|
24
|
+
export const TOPICS = 'topics.json'
|
|
25
|
+
export const BACKUP_DAYS = 7
|
|
26
|
+
const FOREIGN_BASE = 1_000_000_000
|
|
27
|
+
|
|
28
|
+
const MESSAGE = /^\*\*(user|assistant)\*\* \(seq (\d+)\):\n\n/gm
|
|
29
|
+
const SECTION = /^## \S+ session (\S+) turn \S+$/gm
|
|
30
|
+
const TRAILING_SECTION = /\n*## [^\n]* session [^\n]* turn [^\n]*\s*$/
|
|
31
|
+
|
|
32
|
+
/** The messages of one v1 source file: `[{ seq, role, text, session }]`, in file order, duplicates kept. */
|
|
33
|
+
export function parseSourceFile(text) {
|
|
34
|
+
const s = String(text ?? '')
|
|
35
|
+
const sections = [...s.matchAll(SECTION)].map((m) => ({ at: m.index, session: m[1] }))
|
|
36
|
+
const marks = [...s.matchAll(MESSAGE)]
|
|
37
|
+
return marks.map((mark, i) => {
|
|
38
|
+
const end = i + 1 < marks.length ? marks[i + 1].index : s.length
|
|
39
|
+
const body = s.slice(mark.index + mark[0].length, end).replace(TRAILING_SECTION, '').trim()
|
|
40
|
+
const section = sections.filter((x) => x.at < mark.index).at(-1)
|
|
41
|
+
return { seq: Number(mark[2]), role: mark[1], text: body, session: section?.session }
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const labelKey = (label) => String(label ?? '').replace(/\s+/g, ' ').trim().toLowerCase()
|
|
46
|
+
const exists = (path) => stat(path).then(() => true, () => false)
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Bring a chat folder's version-1 data into topics.json and msgs.json. Merges, so it is safe to run
|
|
50
|
+
* any number of times; returns true when it changed anything.
|
|
51
|
+
* @param chat the chat folder; its name is the chat's own session id (`home`).
|
|
52
|
+
*/
|
|
53
|
+
export function upgradeChat(chat, { now = new Date() } = {}) {
|
|
54
|
+
return locked(chat, async () => {
|
|
55
|
+
const v1File = join(chat, 'graph.json')
|
|
56
|
+
let v1Stat
|
|
57
|
+
try { v1Stat = await stat(v1File) } catch { return false }
|
|
58
|
+
const topics = await readJson(join(chat, TOPICS))
|
|
59
|
+
if (topics?.version === GRAPH_VERSION && topics.upgradedFrom >= v1Stat.mtimeMs) {
|
|
60
|
+
await dropOldBackup(chat, topics, v1Stat, now)
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
const v1 = await readJson(v1File)
|
|
64
|
+
if (!v1 || v1.version !== 1 || typeof v1.nodes !== 'object' || v1.nodes === null) return false
|
|
65
|
+
const home = chat.split(/[\\/]/).at(-1)
|
|
66
|
+
const graph = topics?.version === GRAPH_VERSION ? normalizeGraph(topics) : emptyGraph()
|
|
67
|
+
const store = (await readJson(join(chat, 'msgs.json'))) ?? { v: 1, m: {} }
|
|
68
|
+
if (typeof store.m !== 'object' || store.m === null) store.m = {}
|
|
69
|
+
if (typeof store.k !== 'object' || store.k === null) store.k = {}
|
|
70
|
+
let foreign = Object.keys(store.m).reduce((max, key) => Math.max(max, Number(key) || 0), FOREIGN_BASE - 1)
|
|
71
|
+
|
|
72
|
+
const keyOf = (m) => {
|
|
73
|
+
const session = m.session ?? home
|
|
74
|
+
if (session === home) return m.seq
|
|
75
|
+
const name = `${session}:${m.seq}`
|
|
76
|
+
if (!Object.hasOwn(store.k, name)) store.k[name] = ++foreign
|
|
77
|
+
return store.k[name]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let files = []
|
|
81
|
+
try { files = await readdir(join(chat, 'src')) } catch { /* no sources stored */ }
|
|
82
|
+
const byLabel = new Map(Object.entries(graph.nodes).map(([id, node]) => [labelKey(node.label), id]))
|
|
83
|
+
const ids = new Map()
|
|
84
|
+
for (const [oldId, node] of Object.entries(v1.nodes)) {
|
|
85
|
+
const refs = new Set()
|
|
86
|
+
if (files.includes(`${oldId}.md`)) {
|
|
87
|
+
for (const m of parseSourceFile(await readFile(join(chat, 'src', `${oldId}.md`), 'utf8'))) {
|
|
88
|
+
const key = keyOf(m)
|
|
89
|
+
// Migrated text is kept in full: for old workspaces it can be the only copy left.
|
|
90
|
+
if (!Object.hasOwn(store.m, String(key))) store.m[String(key)] = { r: m.role, x: m.text }
|
|
91
|
+
refs.add(key)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const at = (Array.isArray(node.src) ? node.src : []).map((s) => s?.at).filter(Boolean).sort()[0] ?? node.updated
|
|
95
|
+
const same = has(graph, oldId) && labelKey(graph.nodes[oldId].label) === labelKey(node.label) ? oldId : byLabel.get(labelKey(node.label))
|
|
96
|
+
if (same) {
|
|
97
|
+
const target = graph.nodes[same]
|
|
98
|
+
target.refs = [...new Set([...(target.refs ?? []), ...refs])].sort((a, b) => a - b)
|
|
99
|
+
if (String(node.updated ?? '') > String(target.updated ?? '')) Object.assign(target, { gist: String(node.gist ?? target.gist), updated: node.updated })
|
|
100
|
+
ids.set(oldId, same)
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
const id = has(graph, oldId) ? `n${Math.max(graph.nextId, Number(v1.nextId) || 0)}` : oldId
|
|
104
|
+
graph.nodes[id] = {
|
|
105
|
+
label: String(node.label ?? ''),
|
|
106
|
+
gist: String(node.gist ?? ''),
|
|
107
|
+
aliases: (Array.isArray(node.aliases) ? node.aliases : []).map(String),
|
|
108
|
+
kind: node.kind,
|
|
109
|
+
refs: [...refs].sort((a, b) => a - b),
|
|
110
|
+
at,
|
|
111
|
+
updated: node.updated ?? at
|
|
112
|
+
}
|
|
113
|
+
normalizeGraph(graph)
|
|
114
|
+
byLabel.set(labelKey(node.label), id)
|
|
115
|
+
ids.set(oldId, id)
|
|
116
|
+
}
|
|
117
|
+
for (const [a, b, relation] of Array.isArray(v1.edges) ? v1.edges : []) {
|
|
118
|
+
const from = ids.get(a)
|
|
119
|
+
const to = ids.get(b)
|
|
120
|
+
if (from && to && from !== to && !graph.edges.some(([x, y, r]) => x === from && y === to && r === relation)) graph.edges.push([from, to, relation])
|
|
121
|
+
}
|
|
122
|
+
normalizeGraph(graph)
|
|
123
|
+
graph.nextId = Math.max(graph.nextId, Number(v1.nextId) || 0)
|
|
124
|
+
graph.upgradedFrom = v1Stat.mtimeMs
|
|
125
|
+
graph.upgradedAt ??= now.toISOString()
|
|
126
|
+
// Text first, then the graph that points at it. graph.json and src/ are not touched.
|
|
127
|
+
await writeJson(join(chat, 'msgs.json'), store)
|
|
128
|
+
await writeJson(join(chat, TOPICS), graph)
|
|
129
|
+
return true
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Remove the version-1 backup once it has been quiet for BACKUP_DAYS: counted from the later of the
|
|
135
|
+
* upgrade and the last write to it, so an older copy that wrote it on day 6 keeps it a week more (#56).
|
|
136
|
+
*/
|
|
137
|
+
async function dropOldBackup(chat, topics, v1Stat, now) {
|
|
138
|
+
const since = Math.max(Date.parse(topics.upgradedAt ?? ''), Number(topics.upgradedFrom) || 0, v1Stat.mtimeMs)
|
|
139
|
+
if (!Number.isFinite(since) || now - since < BACKUP_DAYS * 86400000) return
|
|
140
|
+
if (v1Stat.mtimeMs > topics.upgradedFrom) return
|
|
141
|
+
await rm(join(chat, 'graph.json'), { force: true })
|
|
142
|
+
if (await exists(join(chat, 'src'))) await rm(join(chat, 'src'), { recursive: true, force: true })
|
|
143
|
+
}
|
package/graph.js
DELETED
|
@@ -1,496 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* context-graph core. Pure: no I/O, no dsh imports, so `node --test` covers it without a harness.
|
|
3
|
-
*
|
|
4
|
-
* One graph per chat: `{ version, nextId, nodes: { [id]: Node }, edges: [[from, to, relation], ...] }`.
|
|
5
|
-
* A Node is `{ label, gist, aliases, kind, src: [{ session, turn, at }], updated, hits }`.
|
|
6
|
-
* Ids inside a graph are `n12`; across chats they are prefixed by the chat's number: `c3n12`.
|
|
7
|
-
* The verbatim source text of a node lives outside the graph (see store.js); `src` is provenance.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export const GRAPH_VERSION = 1
|
|
11
|
-
export const SOURCE_KIND = 'context-graph'
|
|
12
|
-
export const RELATIONS = ['uses', 'part-of', 'supersedes', 'caused-by', 'relates-to']
|
|
13
|
-
export const KINDS = ['decision', 'fact', 'task', 'artifact', 'open-question']
|
|
14
|
-
|
|
15
|
-
const STOPWORDS = new Set((
|
|
16
|
-
'a an the and or but if then so of to in on at for with by from as is are was were be been being ' +
|
|
17
|
-
'do does did done have has had can could should would will shall may might must not no yes ' +
|
|
18
|
-
'i me my we our you your he she it its they them this that these those there here what which who ' +
|
|
19
|
-
'how when where why also just like get got make made use used using want need let please ok okay ' +
|
|
20
|
-
'now new one two about into over under again more most some any all each other than too very ' +
|
|
21
|
-
'的 了 是 在 我 你 他 她 它 们 这 那 个 和 与 或 吗 呢 吧 啊 把 被 给 让 就 都 也 还 很 不 没 有 要 会 能 可以 什么 怎么 这个 那个 一下 一个'
|
|
22
|
-
).split(/\s+/))
|
|
23
|
-
|
|
24
|
-
export function emptyGraph() {
|
|
25
|
-
return { version: GRAPH_VERSION, nextId: 1, nodes: {}, edges: [] }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Rough token estimate for budget caps; CJK is close to one token per char. */
|
|
29
|
-
export function estimateTokens(text) {
|
|
30
|
-
let cjk = 0
|
|
31
|
-
for (const ch of text) if (/[-ヿ㐀-鿿가-]/.test(ch)) cjk++
|
|
32
|
-
return Math.ceil(cjk + (text.length - cjk) / 3.5)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Lowercased word tokens plus CJK bigrams, stopwords removed.
|
|
37
|
-
* "clear_conversation tool" -> ["clear_conversation", "clear", "conversation", "tool"]
|
|
38
|
-
* "清除上下文" -> ["清除", "除上", "上下", "下文"]
|
|
39
|
-
*/
|
|
40
|
-
export function tokenize(text) {
|
|
41
|
-
const out = new Set()
|
|
42
|
-
const lower = String(text ?? '').toLowerCase()
|
|
43
|
-
for (const run of lower.match(/[-ヿ㐀-鿿가-]+/g) ?? []) {
|
|
44
|
-
if (run.length === 1) { if (!STOPWORDS.has(run)) out.add(run); continue }
|
|
45
|
-
for (let i = 0; i + 1 < run.length; i++) {
|
|
46
|
-
const bigram = run.slice(i, i + 2)
|
|
47
|
-
if (!STOPWORDS.has(bigram)) out.add(bigram)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
for (const word of lower.replace(/[-ヿ㐀-鿿가-]+/g, ' ').match(/[a-z0-9_./-]{2,}/g) ?? []) {
|
|
51
|
-
if (STOPWORDS.has(word)) continue
|
|
52
|
-
out.add(word)
|
|
53
|
-
if (/[_./-]/.test(word)) for (const part of word.split(/[_./-]+/)) if (part.length >= 2 && !STOPWORDS.has(part)) out.add(part)
|
|
54
|
-
}
|
|
55
|
-
return [...out]
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function nodeTokens(node) {
|
|
59
|
-
return {
|
|
60
|
-
strong: new Set(tokenize([node.label, ...(node.aliases ?? [])].join(' '))),
|
|
61
|
-
weak: new Set(tokenize(node.gist ?? ''))
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function degree(graph, id) {
|
|
66
|
-
let n = 0
|
|
67
|
-
for (const [a, b] of graph.edges) if (a === id || b === id) n++
|
|
68
|
-
return n
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Edges touching `id`, oriented from its point of view. */
|
|
72
|
-
export function neighbors(graph, id) {
|
|
73
|
-
const out = []
|
|
74
|
-
for (const [a, b, relation] of graph.edges) {
|
|
75
|
-
if (a === id && graph.nodes[b]) out.push({ id: b, relation, direction: 'out' })
|
|
76
|
-
else if (b === id && graph.nodes[a]) out.push({ id: a, relation, direction: 'in' })
|
|
77
|
-
}
|
|
78
|
-
return out
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Follow `supersedes` edges forward so a stale decision resolves to its replacement. */
|
|
82
|
-
export function resolveSuperseded(graph, id) {
|
|
83
|
-
const seen = new Set()
|
|
84
|
-
let current = id
|
|
85
|
-
while (!seen.has(current)) {
|
|
86
|
-
seen.add(current)
|
|
87
|
-
const next = graph.edges.find(([from, to, rel]) => rel === 'supersedes' && to === current && graph.nodes[from])
|
|
88
|
-
if (!next) return current
|
|
89
|
-
current = next[0]
|
|
90
|
-
}
|
|
91
|
-
return current
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Lexical match of a user message against one graph: labels/aliases weigh 2, gists 1, plus a small
|
|
96
|
-
* boost for well-connected nodes. Only nodes with a strong (label/alias) hit count.
|
|
97
|
-
* @returns [{ id, node, score, via }] best first, at most `max`.
|
|
98
|
-
*/
|
|
99
|
-
export function matchNodes(graph, query, { max = 5 } = {}) {
|
|
100
|
-
const q = tokenize(query)
|
|
101
|
-
if (q.length === 0) return []
|
|
102
|
-
const scored = []
|
|
103
|
-
for (const [rawId, node] of Object.entries(graph.nodes)) {
|
|
104
|
-
const { strong, weak } = nodeTokens(node)
|
|
105
|
-
let strongHits = 0
|
|
106
|
-
let weakHits = 0
|
|
107
|
-
for (const t of q) {
|
|
108
|
-
if (strong.has(t)) strongHits++
|
|
109
|
-
else if (weak.has(t)) weakHits++
|
|
110
|
-
}
|
|
111
|
-
if (strongHits === 0) continue
|
|
112
|
-
const score = strongHits * 2 + weakHits + 0.25 * Math.log1p(degree(graph, rawId))
|
|
113
|
-
const id = resolveSuperseded(graph, rawId)
|
|
114
|
-
scored.push({ id, node: graph.nodes[id], score, via: id === rawId ? undefined : rawId })
|
|
115
|
-
}
|
|
116
|
-
scored.sort((a, b) => b.score - a.score || (b.node.updated ?? '').localeCompare(a.node.updated ?? ''))
|
|
117
|
-
const seen = new Set()
|
|
118
|
-
return scored.filter(({ id }) => !seen.has(id) && seen.add(id)).slice(0, max)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** God nodes: most connected first, newest first among equals. */
|
|
122
|
-
export function surfaceNodes(graph, max = 40) {
|
|
123
|
-
return Object.keys(graph.nodes)
|
|
124
|
-
.map((id) => ({ id, d: degree(graph, id), u: graph.nodes[id].updated ?? '' }))
|
|
125
|
-
.sort((a, b) => b.d - a.d || b.u.localeCompare(a.u))
|
|
126
|
-
.slice(0, max)
|
|
127
|
-
.map(({ id }) => id)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// ---------- time: the first filter when a message names a day ----------
|
|
131
|
-
|
|
132
|
-
const DAY = 86400000
|
|
133
|
-
const dayStart = (d) => { const x = new Date(d); x.setHours(0, 0, 0, 0); return x }
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* A from/to range (to exclusive) when the text names a time: yesterday, today, N days ago, last/this
|
|
137
|
-
* week or month, 2026-11-01, 11月1日, 昨天, 上周 ... `undefined` otherwise. No model call.
|
|
138
|
-
*/
|
|
139
|
-
export function parseTimeRange(text, now = new Date()) {
|
|
140
|
-
const s = String(text ?? '').toLowerCase()
|
|
141
|
-
const today = dayStart(now)
|
|
142
|
-
const days = (offset, label, span = 1) => ({ from: new Date(+today + offset * DAY), to: new Date(+today + (offset + span) * DAY), label })
|
|
143
|
-
let m
|
|
144
|
-
if ((m = /\b(\d{4})-(\d{2})-(\d{2})\b/.exec(s))) {
|
|
145
|
-
const d = new Date(+m[1], +m[2] - 1, +m[3])
|
|
146
|
-
return { from: d, to: new Date(+d + DAY), label: m[0] }
|
|
147
|
-
}
|
|
148
|
-
if ((m = /(\d{1,2})月(\d{1,2})[日号]/.exec(s))) {
|
|
149
|
-
const d = new Date(now.getFullYear(), +m[1] - 1, +m[2])
|
|
150
|
-
if (d > now) d.setFullYear(d.getFullYear() - 1)
|
|
151
|
-
return { from: d, to: new Date(+d + DAY), label: m[0] }
|
|
152
|
-
}
|
|
153
|
-
if (/day before yesterday|前天/.test(s)) return days(-2, 'the day before yesterday')
|
|
154
|
-
if (/\byesterday\b|昨天|昨日/.test(s)) return days(-1, 'yesterday')
|
|
155
|
-
if (/\btoday\b|this morning|今天|今早|今日/.test(s)) return days(0, 'today')
|
|
156
|
-
if ((m = /\b(\d+) days? ago\b/.exec(s)) || (m = /(\d+)天前/.exec(s))) return days(-Number(m[1]), m[0])
|
|
157
|
-
const monday = new Date(+today - ((today.getDay() + 6) % 7) * DAY)
|
|
158
|
-
if (/\blast week\b|上周|上星期|上个星期/.test(s)) return { from: new Date(+monday - 7 * DAY), to: monday, label: 'last week' }
|
|
159
|
-
if (/\bthis week\b|本周|这周|这星期|这个星期/.test(s)) return { from: monday, to: new Date(+monday + 7 * DAY), label: 'this week' }
|
|
160
|
-
if (/\blast month\b|上个月|上月/.test(s)) {
|
|
161
|
-
return { from: new Date(now.getFullYear(), now.getMonth() - 1, 1), to: new Date(now.getFullYear(), now.getMonth(), 1), label: 'last month' }
|
|
162
|
-
}
|
|
163
|
-
if (/\bthis month\b|本月|这个月/.test(s)) {
|
|
164
|
-
return { from: new Date(now.getFullYear(), now.getMonth(), 1), to: new Date(now.getFullYear(), now.getMonth() + 1, 1), label: 'this month' }
|
|
165
|
-
}
|
|
166
|
-
return undefined
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const inRange = (iso, range) => Boolean(iso) && new Date(iso) >= range.from && new Date(iso) < range.to
|
|
170
|
-
/** A chat overlaps the range when it was active at any point inside it. */
|
|
171
|
-
export const chatInRange = (meta, range) => Boolean(meta?.startedAt) && new Date(meta.startedAt) < range.to && new Date(meta.lastActiveAt ?? meta.startedAt) >= range.from
|
|
172
|
-
|
|
173
|
-
// ---------- views: one chat's graph seen from another chat ----------
|
|
174
|
-
|
|
175
|
-
/** A view is `{ sid, cid, meta, graph, own }`; `own` is the chat the request belongs to. */
|
|
176
|
-
export const globalId = (view, id) => `c${view.cid}${id}`
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Match across every view except the own chat (its text is still in context). With a time range,
|
|
180
|
-
* try chats active in that range first, then topics updated in it, then everything.
|
|
181
|
-
* @returns { hits: [{ id, node, score, via, view }], slice }
|
|
182
|
-
*/
|
|
183
|
-
export function matchAcross(views, query, { max = 5, range, skip } = {}) {
|
|
184
|
-
const others = views.filter((v) => !v.own)
|
|
185
|
-
const run = (vs, keep = () => true) => vs
|
|
186
|
-
.flatMap((view) => matchNodes(view.graph, query, { max: max + (skip?.size ?? 0) }).map((h) => ({ ...h, id: globalId(view, h.id), via: h.via && globalId(view, h.via), view })))
|
|
187
|
-
.filter((h) => keep(h) && !skip?.has(h.id))
|
|
188
|
-
.sort((a, b) => b.score - a.score)
|
|
189
|
-
.slice(0, max)
|
|
190
|
-
if (range) {
|
|
191
|
-
const byChat = run(others.filter((v) => chatInRange(v.meta, range)))
|
|
192
|
-
if (byChat.length > 0) return { hits: byChat, slice: `${range.label} (by chat)` }
|
|
193
|
-
const byTopic = run(others, (h) => inRange(h.node.updated, range))
|
|
194
|
-
if (byTopic.length > 0) return { hits: byTopic, slice: `${range.label} (by topic)` }
|
|
195
|
-
}
|
|
196
|
-
return { hits: run(others), slice: undefined }
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** Local calendar day (YYYY-MM-DD) of a timestamp, the same day recall("YYYY-MM-DD") and "today" search. */
|
|
200
|
-
export function day(iso) {
|
|
201
|
-
const d = new Date(iso ?? NaN)
|
|
202
|
-
if (Number.isNaN(+d)) return ''
|
|
203
|
-
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
|
204
|
-
}
|
|
205
|
-
const topicLine = (id, node) => `- ${id} ${node.label}: ${node.gist}${node.updated ? ` (${day(node.updated)})` : ''}`
|
|
206
|
-
const chatLine = (view) => `- c${view.cid} ${day(view.meta.lastActiveAt ?? view.meta.startedAt)} ${view.meta.title ?? '(untitled)'} (${Object.keys(view.graph.nodes).length} topics${view.own ? ', this chat' : ''})`
|
|
207
|
-
const newestFirst = (views) => [...views].sort((a, b) => String(b.meta.lastActiveAt ?? '').localeCompare(String(a.meta.lastActiveAt ?? '')))
|
|
208
|
-
|
|
209
|
-
/** The per-turn block appended to the user's message. `inline` carries one node's verbatim text when it is short. */
|
|
210
|
-
export function renderHits(hits, { inline, slice } = {}) {
|
|
211
|
-
if (hits.length === 0) return ''
|
|
212
|
-
const lines = [`[${SOURCE_KIND}] Related past context${slice ? ` from ${slice}` : ''}. Call recall(id) for the verbatim source.`]
|
|
213
|
-
for (const { id, node, via } of hits) lines.push(topicLine(id, node) + (via ? ` [replaces ${via}]` : ''))
|
|
214
|
-
if (inline) lines.push('', `Verbatim source of ${inline.id}:`, inline.text.trim())
|
|
215
|
-
return lines.join('\n')
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Sent once after memory is switched on: the chats this chat can draw on, newest first. */
|
|
219
|
-
export function renderChats(views, { max = 20 } = {}) {
|
|
220
|
-
const shared = newestFirst(views.filter((v) => !v.own))
|
|
221
|
-
if (shared.length === 0) return ''
|
|
222
|
-
const lines = [
|
|
223
|
-
`[${SOURCE_KIND}] Memory is on. Earlier chats in this workspace, newest first. ` +
|
|
224
|
-
'Call recall("c3") for a chat\'s topics, recall("2026-11-01") for everything from a day, recall("c3n12") for one topic\'s exact text. ' +
|
|
225
|
-
'Treat this as background; do not restate it.'
|
|
226
|
-
]
|
|
227
|
-
for (const view of shared.slice(0, max)) lines.push(chatLine(view))
|
|
228
|
-
if (shared.length > max) lines.push(`- ... ${shared.length - max} older chats; name a day to see them`)
|
|
229
|
-
return lines.join('\n')
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/** One chat's topics, most connected first. */
|
|
233
|
-
export function renderChatIndex(view, { max = 40 } = {}) {
|
|
234
|
-
const lines = [`# c${view.cid} ${view.meta.title ?? '(untitled)'} (${day(view.meta.startedAt)} → ${day(view.meta.lastActiveAt)})`]
|
|
235
|
-
const ids = surfaceNodes(view.graph, max)
|
|
236
|
-
if (ids.length === 0) lines.push('(no topics indexed yet)')
|
|
237
|
-
for (const id of ids) lines.push(topicLine(globalId(view, id), view.graph.nodes[id]))
|
|
238
|
-
return lines.join('\n')
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Everything that happened in a time range: each chat active then, with its topics from that range
|
|
243
|
-
* first. Whatever the caps cut is named, with the recall id that shows it, so nothing is hidden.
|
|
244
|
-
*/
|
|
245
|
-
export function renderRange(views, range, { maxChats = 10, maxTopics = 10 } = {}) {
|
|
246
|
-
const chats = newestFirst(views.filter((v) => chatInRange(v.meta, range)))
|
|
247
|
-
if (chats.length === 0) return `[${SOURCE_KIND}] No chats from ${range.label}.`
|
|
248
|
-
const lines = [`[${SOURCE_KIND}] Chats from ${range.label}:`]
|
|
249
|
-
for (const view of chats.slice(0, maxChats)) {
|
|
250
|
-
lines.push(chatLine(view))
|
|
251
|
-
const ids = Object.keys(view.graph.nodes).sort((a, b) => Number(inRange(view.graph.nodes[b].updated, range)) - Number(inRange(view.graph.nodes[a].updated, range)))
|
|
252
|
-
for (const id of ids.slice(0, maxTopics)) lines.push(' ' + topicLine(globalId(view, id), view.graph.nodes[id]))
|
|
253
|
-
if (ids.length > maxTopics) lines.push(` - ... ${ids.length - maxTopics} more topics: recall("c${view.cid}")`)
|
|
254
|
-
}
|
|
255
|
-
if (chats.length > maxChats) {
|
|
256
|
-
const rest = chats.slice(maxChats).map((v) => `c${v.cid}`)
|
|
257
|
-
lines.push(`- ... ${rest.length} more chats (${rest.slice(0, 10).join(', ')}${rest.length > 10 ? ', ...' : ''}): recall a chat id or a day (YYYY-MM-DD)`)
|
|
258
|
-
}
|
|
259
|
-
return lines.join('\n')
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/** Memory is on in every chat unless `/context-graph off` was run there; the Plugins page switch turns the whole plugin off. */
|
|
263
|
-
export const memoryOn = (meta) => meta?.memoryOff !== true
|
|
264
|
-
|
|
265
|
-
/** Chats are shared with the rest of the workspace unless `/context-graph unshare` was run there. */
|
|
266
|
-
export const sharedOn = (meta) => meta?.private !== true
|
|
267
|
-
|
|
268
|
-
/** The /context-graph map: workspace → chats (newest first) → topics. */
|
|
269
|
-
export function renderMap(views, { maxTopics = 8 } = {}) {
|
|
270
|
-
if (views.length === 0) return 'No chats indexed in this workspace yet. Chats index themselves every few turns; /context-graph build indexes this one now.'
|
|
271
|
-
const lines = []
|
|
272
|
-
for (const view of newestFirst(views)) {
|
|
273
|
-
const meta = view.meta
|
|
274
|
-
const flags = [view.own ? 'this chat' : undefined, memoryOn(meta) ? 'memory on' : 'memory off', sharedOn(meta) ? 'shared' : 'private'].filter(Boolean).join(', ')
|
|
275
|
-
lines.push(`c${view.cid} ${day(meta.startedAt)} ${meta.title ?? '(untitled)'} · ${Object.keys(view.graph.nodes).length} topics · ${flags}`)
|
|
276
|
-
for (const id of surfaceNodes(view.graph, maxTopics)) lines.push(' ' + topicLine(globalId(view, id), view.graph.nodes[id]))
|
|
277
|
-
}
|
|
278
|
-
return lines.join('\n')
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// ---------- builder ----------
|
|
282
|
-
|
|
283
|
-
/** `[m0 user] ...` transcript the builder reads; each message capped so one huge paste cannot eat the call. */
|
|
284
|
-
export function transcriptForBuilder(messages, maxChars = 2000) {
|
|
285
|
-
return messages.map((m, i) => {
|
|
286
|
-
const text = m.text.length > maxChars ? `${m.text.slice(0, maxChars)}\n…[${m.text.length - maxChars} more chars]` : m.text
|
|
287
|
-
return `[m${i} ${m.role}]\n${text}`
|
|
288
|
-
}).join('\n\n')
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
export function builderInstruction(graph) {
|
|
292
|
-
const existing = Object.entries(graph.nodes).map(([id, n]) => `${id}: ${n.label}`).join('\n')
|
|
293
|
-
return [
|
|
294
|
-
'You are indexing the conversation span above into a memory graph for later sessions. Output ONE JSON object and nothing else.',
|
|
295
|
-
'',
|
|
296
|
-
'Shape: {"nodes":[{"id":"n12"|null,"label":"...","gist":"...","aliases":["..."],"kind":"decision|fact|task|artifact|open-question","msgs":[0,2]}],"edges":[["n12","n3","uses"]]}',
|
|
297
|
-
'',
|
|
298
|
-
'Rules:',
|
|
299
|
-
'- One node per decision, fact, task, artifact (file, tool, repo) or open question worth remembering later. Skip chit-chat and anything already obvious from the labels below unless it changed.',
|
|
300
|
-
'- label: at most 6 words, the name someone would search for. gist: one line under 120 characters, what is true now and why.',
|
|
301
|
-
'- aliases: other words a future message might use for this node, including the other language if the conversation mixes languages. Be precise; matching is by word overlap.',
|
|
302
|
-
'- msgs: indices of the [mN] messages the node is drawn from. Required, never empty.',
|
|
303
|
-
'- id: reuse an existing id below when the node is the same thing (update its gist); null for a new node.',
|
|
304
|
-
`- edges: [from, to, relation] with relation in ${RELATIONS.join(', ')}. For a changed decision add [new, old, "supersedes"]. Use existing ids where possible; refer to new nodes by their label in place of an id.`,
|
|
305
|
-
'- Keep file paths, commands, identifiers and numbers exact.',
|
|
306
|
-
'',
|
|
307
|
-
existing ? `Existing nodes:\n${existing}` : 'Existing nodes: (none)'
|
|
308
|
-
].join('\n')
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/** Complete top-level `{...}` objects inside the array that opens at `open`; an unfinished last one is dropped. */
|
|
312
|
-
function objectsInArray(text, open) {
|
|
313
|
-
const out = []
|
|
314
|
-
let depth = 0
|
|
315
|
-
let start = -1
|
|
316
|
-
let inString = false
|
|
317
|
-
for (let i = open + 1; i < text.length; i++) {
|
|
318
|
-
const ch = text[i]
|
|
319
|
-
if (inString) {
|
|
320
|
-
if (ch === '\\') i++
|
|
321
|
-
else if (ch === '"') inString = false
|
|
322
|
-
continue
|
|
323
|
-
}
|
|
324
|
-
if (ch === '"') inString = true
|
|
325
|
-
else if (ch === '{' || ch === '[') { if (depth++ === 0 && ch === '{') start = i }
|
|
326
|
-
else if (ch === '}' || ch === ']') {
|
|
327
|
-
if (depth === 0) break
|
|
328
|
-
if (--depth === 0 && ch === '}' && start >= 0) { out.push(text.slice(start, i + 1)); start = -1 }
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
return out
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Tolerant extraction of the builder's JSON. Handles code fences, prose around it, `<think>` blocks,
|
|
336
|
-
* trailing commas, and output cut off mid-way (every complete node object is kept).
|
|
337
|
-
* `undefined` when there is no `"nodes"` array at all.
|
|
338
|
-
*/
|
|
339
|
-
export function parseBuilderOutput(text) {
|
|
340
|
-
const clean = String(text ?? '').replace(/<think>[\s\S]*?(?:<\/think>|$)/g, '')
|
|
341
|
-
const at = clean.search(/"nodes"\s*:\s*\[/)
|
|
342
|
-
if (at < 0) return undefined
|
|
343
|
-
const nodes = []
|
|
344
|
-
for (const raw of objectsInArray(clean, clean.indexOf('[', at))) {
|
|
345
|
-
try { nodes.push(JSON.parse(raw.replace(/,\s*([}\]])/g, '$1'))) } catch { /* skip a malformed node */ }
|
|
346
|
-
}
|
|
347
|
-
const edgesAt = clean.search(/"edges"\s*:/)
|
|
348
|
-
const edges = edgesAt < 0 ? [] : [...clean.slice(edgesAt).matchAll(/\[\s*"([^"]+)"\s*,\s*"([^"]+)"\s*,\s*"([^"]+)"\s*\]/g)].map((m) => [m[1], m[2], m[3]])
|
|
349
|
-
return {
|
|
350
|
-
nodes: nodes.filter((n) => n && typeof n.label === 'string' && n.label.trim() && Array.isArray(n.msgs) && n.msgs.length > 0),
|
|
351
|
-
edges
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* Apply a builder delta. New nodes get ids; existing ones are updated in place (gist replaced,
|
|
357
|
-
* aliases unioned, provenance appended). Edges may name new nodes by label. Returns the ids
|
|
358
|
-
* touched with the message indices they cite, so the caller can store verbatim text.
|
|
359
|
-
*/
|
|
360
|
-
export function mergeDelta(graph, delta, { session, turn, now }) {
|
|
361
|
-
const byLabel = new Map()
|
|
362
|
-
const touched = []
|
|
363
|
-
for (const n of delta.nodes) {
|
|
364
|
-
let id = typeof n.id === 'string' && graph.nodes[n.id] ? n.id : undefined
|
|
365
|
-
if (!id) {
|
|
366
|
-
const existing = Object.entries(graph.nodes).find(([, x]) => x.label.toLowerCase() === n.label.trim().toLowerCase())
|
|
367
|
-
id = existing ? existing[0] : `n${graph.nextId++}`
|
|
368
|
-
}
|
|
369
|
-
const prev = graph.nodes[id]
|
|
370
|
-
const aliases = [...new Set([...(prev?.aliases ?? []), ...(Array.isArray(n.aliases) ? n.aliases : [])].map((a) => String(a).trim()).filter(Boolean))]
|
|
371
|
-
graph.nodes[id] = {
|
|
372
|
-
label: n.label.trim(),
|
|
373
|
-
gist: String(n.gist ?? prev?.gist ?? '').trim().slice(0, 200),
|
|
374
|
-
aliases,
|
|
375
|
-
kind: KINDS.includes(n.kind) ? n.kind : prev?.kind ?? 'fact',
|
|
376
|
-
src: [...(prev?.src ?? []), { session, turn, at: now }],
|
|
377
|
-
updated: now,
|
|
378
|
-
hits: prev?.hits ?? 0
|
|
379
|
-
}
|
|
380
|
-
byLabel.set(n.label.trim().toLowerCase(), id)
|
|
381
|
-
if (typeof n.id === 'string') byLabel.set(n.id, id)
|
|
382
|
-
touched.push({ id, msgs: n.msgs.filter((i) => Number.isInteger(i) && i >= 0), isNew: !prev })
|
|
383
|
-
}
|
|
384
|
-
const resolve = (ref) => graph.nodes[ref] ? ref : byLabel.get(ref) ?? byLabel.get(String(ref).toLowerCase())
|
|
385
|
-
for (const [from, to, relation] of delta.edges) {
|
|
386
|
-
const a = resolve(from)
|
|
387
|
-
const b = resolve(to)
|
|
388
|
-
if (!a || !b || a === b || !RELATIONS.includes(relation)) continue
|
|
389
|
-
if (!graph.edges.some(([x, y, r]) => x === a && y === b && r === relation)) graph.edges.push([a, b, relation])
|
|
390
|
-
}
|
|
391
|
-
return touched
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
// ---------- session events ----------
|
|
395
|
-
|
|
396
|
-
const isHuman = (event) => event?.type === 'user/message' && event.data?.source?.kind === 'user'
|
|
397
|
-
|
|
398
|
-
function textOf(content) {
|
|
399
|
-
if (!Array.isArray(content)) return ''
|
|
400
|
-
return content.filter((b) => b?.type === 'text' && typeof b.text === 'string').map((b) => b.text).join('\n').trim()
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* Pull the human/assistant exchange out of dsh session events in surface order.
|
|
405
|
-
* @param nodes surface seqs; @param eventAt session lookup; @param afterSeq only events past this seq.
|
|
406
|
-
* @returns { messages: [{ seq, role, text }], lastSeq, userTurns }
|
|
407
|
-
*/
|
|
408
|
-
export function collectSpan(nodes, eventAt, afterSeq = -1) {
|
|
409
|
-
const messages = []
|
|
410
|
-
let lastSeq = afterSeq
|
|
411
|
-
let userTurns = 0
|
|
412
|
-
for (const seq of nodes) {
|
|
413
|
-
if (seq <= afterSeq) continue
|
|
414
|
-
const event = eventAt(seq)
|
|
415
|
-
if (!event) continue
|
|
416
|
-
lastSeq = Math.max(lastSeq, seq)
|
|
417
|
-
if (isHuman(event)) {
|
|
418
|
-
const text = textOf(event.data.content)
|
|
419
|
-
if (text) { messages.push({ seq, role: 'user', text }); userTurns++ }
|
|
420
|
-
} else if (event.type === 'assistant/message') {
|
|
421
|
-
const text = textOf(event.data?.message?.content)
|
|
422
|
-
if (text) messages.push({ seq, role: 'assistant', text })
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
return { messages, lastSeq, userTurns }
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* What this chat has already been sent by the plugin and still holds in context: topic ids
|
|
430
|
-
* (c3n12) and whole block texts. Read from the surface, so after compaction drops a block its
|
|
431
|
-
* topics can be sent again.
|
|
432
|
-
*/
|
|
433
|
-
export function shownContext(nodes, eventAt) {
|
|
434
|
-
const ids = new Set()
|
|
435
|
-
const texts = new Set()
|
|
436
|
-
for (const seq of nodes) {
|
|
437
|
-
const event = eventAt(seq)
|
|
438
|
-
if (event?.type !== 'user/message' || event.data?.source?.kind !== SOURCE_KIND) continue
|
|
439
|
-
const text = textOf(event.data.content)
|
|
440
|
-
texts.add(text)
|
|
441
|
-
for (const m of text.matchAll(/\bc\d+n\d+\b/g)) ids.add(m[0])
|
|
442
|
-
}
|
|
443
|
-
return { ids, texts }
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/** The newest messages whose builder transcript fits in `maxChars` (each message counts at most `perMessage`). */
|
|
447
|
-
export function tailWithin(messages, maxChars, perMessage = 2000) {
|
|
448
|
-
let used = 0
|
|
449
|
-
let start = messages.length
|
|
450
|
-
while (start > 0) {
|
|
451
|
-
const cost = Math.min(messages[start - 1].text.length, perMessage) + 20
|
|
452
|
-
if (start < messages.length && used + cost > maxChars) break
|
|
453
|
-
used += cost
|
|
454
|
-
start--
|
|
455
|
-
}
|
|
456
|
-
return messages.slice(start)
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/** First human prompt of the log, shortened: the chat's title until dsh gives us a better one. */
|
|
460
|
-
export function titleOf(eventAt, width = 60) {
|
|
461
|
-
for (let seq = 0; ; seq++) {
|
|
462
|
-
const event = eventAt(seq)
|
|
463
|
-
if (!event) return undefined
|
|
464
|
-
if (isHuman(event)) {
|
|
465
|
-
const text = textOf(event.data.content).replace(/\s+/g, ' ')
|
|
466
|
-
return text.length > width ? `${text.slice(0, width - 1)}…` : text
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/** Verbatim text of turn `n` (its prompt through the step before the next human prompt). */
|
|
472
|
-
export function turnTranscript(eventAt, n, { maxToolChars = 2000 } = {}) {
|
|
473
|
-
let count = 0
|
|
474
|
-
let seq = 0
|
|
475
|
-
for (; ; seq++) {
|
|
476
|
-
const event = eventAt(seq)
|
|
477
|
-
if (!event) return undefined
|
|
478
|
-
if (isHuman(event) && ++count === n) break
|
|
479
|
-
}
|
|
480
|
-
const out = []
|
|
481
|
-
for (let s = seq; ; s++) {
|
|
482
|
-
const event = eventAt(s)
|
|
483
|
-
if (!event || (s > seq && isHuman(event))) break
|
|
484
|
-
if (isHuman(event)) out.push(`**user:**\n${textOf(event.data.content)}`)
|
|
485
|
-
else if (event.type === 'assistant/message') {
|
|
486
|
-
const blocks = event.data?.message?.content ?? []
|
|
487
|
-
const text = textOf(blocks)
|
|
488
|
-
if (text) out.push(`**assistant:**\n${text}`)
|
|
489
|
-
for (const b of blocks) if (b.type === 'tool-call') out.push(`**tool call** ${b.name} ${String(b.arguments ?? '').slice(0, 300)}`)
|
|
490
|
-
} else if (event.type === 'tool/result') {
|
|
491
|
-
const text = textOf(event.data?.message?.content)
|
|
492
|
-
out.push(`**tool result:**\n${text.length > maxToolChars ? `${text.slice(0, maxToolChars)}\n…[${text.length - maxToolChars} more chars]` : text}`)
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
return out.join('\n\n')
|
|
496
|
-
}
|