dsh-context-graph 0.2.3 → 0.4.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 +148 -132
- package/README.zh.md +63 -0
- package/cordis.patch.yml +15 -18
- package/graph.js +144 -109
- package/icon.svg +15 -0
- package/index.js +410 -421
- package/lib/client.js +126 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +7 -0
- package/package.json +17 -3
- package/store.js +55 -20
package/index.js
CHANGED
|
@@ -1,421 +1,410 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-context-graph: memory of past
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* - `agent/turn-stopping`: every few human turns, one
|
|
6
|
-
*
|
|
7
|
-
* - `agent/pre-step`: on a human turn,
|
|
8
|
-
* short `[context-graph]` block
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* - `
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} from './graph.js'
|
|
27
|
-
import { appendSource, loadGraph, readSource, saveGraph } from './store.js'
|
|
28
|
-
import { startUpdateCheck } from './update.js'
|
|
29
|
-
|
|
30
|
-
export const name = 'context-graph'
|
|
31
|
-
export const inject = ['tools']
|
|
32
|
-
|
|
33
|
-
const PURPOSE = 'context-graph'
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
function
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
const {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
st.building
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
if (sub === '
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
})
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
`Folds: ${meter.folds}; ~${meter.foldSaved} input tokens removed from every later request`,
|
|
412
|
-
`Injected blocks: ${meter.injected} (~${meter.injectedTokens} tokens); recalls: ${meter.recalls}`,
|
|
413
|
-
`Main requests: ${m.requests}; input ${m.input} uncached + ${m.cacheRead} cache-read + ${m.cacheWrite} cache-write; cache-hit ${hitRate}%`
|
|
414
|
-
].join('\n')
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
})
|
|
418
|
-
}, 'context-graph command')
|
|
419
|
-
}
|
|
420
|
-
})
|
|
421
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-context-graph: memory of past chats as small per-chat graphs, switched on per chat.
|
|
3
|
+
*
|
|
4
|
+
* Nothing happens in a chat until `/context-graph on`. Then:
|
|
5
|
+
* - `agent/turn-stopping`: every few human turns, one auxiliary model call indexes the new span
|
|
6
|
+
* into `<workspace>/.context-graph/chats/<session>/graph.json` and stores the cited text verbatim.
|
|
7
|
+
* - `agent/pre-step`: on a human turn, match the prompt against the other shared chats of this
|
|
8
|
+
* workspace (a named day is searched first) and append one short `[context-graph]` block at the
|
|
9
|
+
* end of the request, so the provider's prefix cache survives. Right after `on`, one block lists
|
|
10
|
+
* the shared chats, newest first.
|
|
11
|
+
* - Tool `recall`: a topic (c3n12), a chat (c3), a day (2026-11-01) or a turn of this chat (t7).
|
|
12
|
+
* - `llm/stream`: meters main vs builder tokens and cache reads for `/context-graph stats`.
|
|
13
|
+
* Nothing is merged between chats; sharing is a flag on the chat. Stock compaction is untouched.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { isAbsolute, join } from 'node:path'
|
|
17
|
+
import { readFileSync } from 'node:fs'
|
|
18
|
+
import { mkdir, writeFile } from 'node:fs/promises'
|
|
19
|
+
|
|
20
|
+
import { BlockAssembler, createUserMessage } from '@deepseek-ai/dsh-llm'
|
|
21
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
builderInstruction, collectSpan, estimateTokens, matchAcross, mergeDelta, neighbors, parseBuilderOutput, parseTimeRange,
|
|
25
|
+
renderChatIndex, renderChats, renderHits, renderMap, renderRange, SOURCE_KIND, titleOf, transcriptForBuilder, turnTranscript
|
|
26
|
+
} from './graph.js'
|
|
27
|
+
import { appendSource, chatDir, ensureMeta, listChats, loadGraph, loadMeta, readSource, saveGraph, saveMeta } from './store.js'
|
|
28
|
+
import { startUpdateCheck } from './update.js'
|
|
29
|
+
|
|
30
|
+
export const name = 'context-graph'
|
|
31
|
+
export const inject = ['tools']
|
|
32
|
+
|
|
33
|
+
const PURPOSE = 'context-graph'
|
|
34
|
+
const USAGE = '[on|off|share|unshare|stats|build|map|find <words>]'
|
|
35
|
+
const { version } = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
|
|
36
|
+
|
|
37
|
+
const DEFAULTS = {
|
|
38
|
+
dir: '.context-graph',
|
|
39
|
+
buildEveryTurns: 4,
|
|
40
|
+
buildMinChars: 400,
|
|
41
|
+
maxHits: 5,
|
|
42
|
+
inlineMaxChars: 1200,
|
|
43
|
+
surfaceMaxChats: 20,
|
|
44
|
+
builderMaxTokens: 8192,
|
|
45
|
+
provider: '',
|
|
46
|
+
model: ''
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const textOf = (content) => (Array.isArray(content) ? content : []).filter((b) => b?.type === 'text').map((b) => b.text).join('\n').trim()
|
|
50
|
+
|
|
51
|
+
export function apply(ctx, config = {}) {
|
|
52
|
+
const { _updates, ...rest } = config
|
|
53
|
+
const cfg = { ...DEFAULTS, ...rest }
|
|
54
|
+
// `_updates` lets tests stand in for the network check.
|
|
55
|
+
const updates = _updates ?? startUpdateCheck(version)
|
|
56
|
+
let noticeShown = false
|
|
57
|
+
const sessions = new Map()
|
|
58
|
+
const meter = {
|
|
59
|
+
main: { requests: 0, input: 0, cacheRead: 0, cacheWrite: 0, output: 0 },
|
|
60
|
+
builder: { requests: 0, input: 0, cacheRead: 0, cacheWrite: 0, output: 0 },
|
|
61
|
+
builds: 0,
|
|
62
|
+
injected: 0,
|
|
63
|
+
injectedTokens: 0,
|
|
64
|
+
recalls: 0
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const state = (sid) => {
|
|
68
|
+
let st = sessions.get(sid)
|
|
69
|
+
if (!st) { st = { lastBuiltSeq: -1, building: false, announce: false }; sessions.set(sid, st) }
|
|
70
|
+
return st
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const sidOf = (agent) => String(agent.session.id)
|
|
74
|
+
const isSubagent = (agent) => agent.options?.origin === 'subagent' || agent.session.header?.origin === 'subagent'
|
|
75
|
+
|
|
76
|
+
function dirFor(agent) {
|
|
77
|
+
const base = agent.options?.cwd ?? agent.session.header?.cwd ?? process.cwd()
|
|
78
|
+
return isAbsolute(cfg.dir) ? cfg.dir : join(base, cfg.dir)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const metaFor = (agent) => loadMeta(dirFor(agent), sidOf(agent))
|
|
82
|
+
|
|
83
|
+
async function setFlags(agent, flags) {
|
|
84
|
+
const dir = dirFor(agent)
|
|
85
|
+
const session = agent.session
|
|
86
|
+
const now = new Date().toISOString()
|
|
87
|
+
const meta = await ensureMeta(dir, sidOf(agent), { title: titleOf((seq) => session.eventAt(seq)), workspace: dir, now })
|
|
88
|
+
Object.assign(meta, flags, { lastActiveAt: now })
|
|
89
|
+
await saveMeta(dir, sidOf(agent), meta)
|
|
90
|
+
return meta
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Own chat plus every shared chat of the workspace, graphs loaded. ponytail: re-read per turn; cache by mtime if a workspace grows large. */
|
|
94
|
+
async function loadViews(agent) {
|
|
95
|
+
const dir = dirFor(agent)
|
|
96
|
+
const sid = sidOf(agent)
|
|
97
|
+
const views = []
|
|
98
|
+
for (const { sid: other, meta } of await listChats(dir)) {
|
|
99
|
+
const own = other === sid
|
|
100
|
+
if (!own && !meta.shared) continue
|
|
101
|
+
views.push({ sid: other, cid: meta.cid, meta, own, graph: await loadGraph(chatDir(dir, other)) })
|
|
102
|
+
}
|
|
103
|
+
return views
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function resolveTarget(agent) {
|
|
107
|
+
if (cfg.provider && cfg.model) return { provider: cfg.provider, model: cfg.model }
|
|
108
|
+
const routed = agent.session.requestHeader()?.config
|
|
109
|
+
if (routed?.provider && routed?.model) return { provider: routed.provider, model: routed.model }
|
|
110
|
+
const { provider, model } = agent.options ?? {}
|
|
111
|
+
return provider && model ? { provider, model } : undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---------- builder ----------
|
|
115
|
+
async function build(agent, { force = false, turn = null } = {}) {
|
|
116
|
+
const session = agent.session
|
|
117
|
+
const sid = sidOf(agent)
|
|
118
|
+
const st = state(sid)
|
|
119
|
+
if (st.building) return { status: 'busy' }
|
|
120
|
+
// ponytail: subagent chatter stays out of the graph; revisit if delegated work holds decisions worth keeping.
|
|
121
|
+
if (isSubagent(agent)) return { status: 'skipped', reason: 'subagent session' }
|
|
122
|
+
st.building = true
|
|
123
|
+
try {
|
|
124
|
+
const meta = await metaFor(agent)
|
|
125
|
+
if (!meta?.memory) return { status: 'off', reason: 'memory is off for this chat; run /context-graph on' }
|
|
126
|
+
const span = collectSpan(session.surface.nodes, (seq) => session.eventAt(seq), st.lastBuiltSeq)
|
|
127
|
+
const chars = span.messages.reduce((n, m) => n + m.text.length, 0)
|
|
128
|
+
if (span.messages.length === 0) return { status: 'nothing', reason: 'no new messages since the last build' }
|
|
129
|
+
if (!force && (span.userTurns < cfg.buildEveryTurns || chars < cfg.buildMinChars)) {
|
|
130
|
+
return { status: 'skipped', reason: `${span.userTurns}/${cfg.buildEveryTurns} human turns, ${chars}/${cfg.buildMinChars} chars` }
|
|
131
|
+
}
|
|
132
|
+
const llm = ctx.get('llm')
|
|
133
|
+
if (!llm) return { status: 'unavailable', reason: 'no llm service in this composition' }
|
|
134
|
+
const target = resolveTarget(agent)
|
|
135
|
+
if (!target) return { status: 'unavailable', reason: 'no provider/model routed yet' }
|
|
136
|
+
const chat = chatDir(dirFor(agent), sid)
|
|
137
|
+
const graph = await loadGraph(chat)
|
|
138
|
+
const prompt = `${transcriptForBuilder(span.messages)}\n\n${builderInstruction(graph)}`
|
|
139
|
+
const assembler = new BlockAssembler()
|
|
140
|
+
for await (const chunk of llm.stream({
|
|
141
|
+
provider: target.provider,
|
|
142
|
+
model: target.model,
|
|
143
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: prompt }] }],
|
|
144
|
+
maxTokens: cfg.builderMaxTokens,
|
|
145
|
+
sessionId: session.id,
|
|
146
|
+
purpose: PURPOSE
|
|
147
|
+
})) assembler.push(chunk)
|
|
148
|
+
const finish = assembler.finish
|
|
149
|
+
if (finish?.kind === 'error' || finish?.kind === 'aborted') throw new Error(finish.failure?.message ?? finish.kind)
|
|
150
|
+
const blocks = assembler.blocks()
|
|
151
|
+
const answer = textOf(blocks)
|
|
152
|
+
// Some thinking models put everything, JSON included, into reasoning blocks.
|
|
153
|
+
const reasoning = blocks.filter((b) => b?.type === 'reasoning').map((b) => b.text).join('\n')
|
|
154
|
+
const delta = parseBuilderOutput(answer) ?? parseBuilderOutput(reasoning)
|
|
155
|
+
if (!delta || (delta.nodes.length === 0 && finish?.kind === 'max-tokens')) {
|
|
156
|
+
const saved = join(chat, 'last-builder-output.txt')
|
|
157
|
+
await mkdir(chat, { recursive: true })
|
|
158
|
+
await writeFile(saved, `finish: ${finish?.kind}\n\n--- answer ---\n${answer}\n\n--- reasoning ---\n${reasoning}\n`, 'utf8')
|
|
159
|
+
const why = finish?.kind === 'max-tokens'
|
|
160
|
+
? `the model ran out of output tokens (builderMaxTokens ${cfg.builderMaxTokens}) before finishing`
|
|
161
|
+
: 'builder output was not the expected JSON'
|
|
162
|
+
return { status: 'failed', reason: `${why}; raw output saved to ${saved}` }
|
|
163
|
+
}
|
|
164
|
+
// Reversibility rule: a node is written only when it cites text we can store verbatim.
|
|
165
|
+
delta.nodes = delta.nodes.filter((n) => n.msgs.some((i) => span.messages[i]))
|
|
166
|
+
const now = new Date().toISOString()
|
|
167
|
+
const touched = mergeDelta(graph, delta, { session: sid, turn, now })
|
|
168
|
+
for (const { id, msgs } of touched) {
|
|
169
|
+
const cited = msgs.map((i) => span.messages[i]).filter(Boolean)
|
|
170
|
+
if (cited.length > 0) await appendSource(chat, id, { session: sid, turn, at: now, messages: cited })
|
|
171
|
+
}
|
|
172
|
+
await saveGraph(chat, graph)
|
|
173
|
+
await saveMeta(dirFor(agent), sid, { ...meta, title: meta.title ?? titleOf((seq) => session.eventAt(seq)), lastActiveAt: now, turns: (meta.turns ?? 0) + span.userTurns })
|
|
174
|
+
st.lastBuiltSeq = span.lastSeq
|
|
175
|
+
meter.builds++
|
|
176
|
+
return { status: 'built', nodes: touched.length, newNodes: touched.filter((t) => t.isNew).length, totalNodes: Object.keys(graph.nodes).length, edges: graph.edges.length, dir: chat }
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error('context-graph: build failed (non-fatal)', error)
|
|
179
|
+
return { status: 'failed', reason: String(error?.message ?? error) }
|
|
180
|
+
} finally {
|
|
181
|
+
st.building = false
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
ctx.on('agent/turn-stopping', (payload) => {
|
|
186
|
+
// Fire and forget: the build must never delay the turn's close.
|
|
187
|
+
void build(payload.agent, { turn: payload.turn })
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
// ---------- matcher ----------
|
|
191
|
+
ctx.on('agent/pre-step', async (payload, next) => {
|
|
192
|
+
const agent = payload.agent
|
|
193
|
+
const prompt = payload.messages.filter((m) => m.source?.kind === 'user').map((m) => textOf(m.content)).join('\n')
|
|
194
|
+
const decision = await next()
|
|
195
|
+
if (decision.kind !== 'enter') return decision
|
|
196
|
+
const result = await addMemory(agent, prompt, decision)
|
|
197
|
+
return prompt && !isSubagent(agent) ? addUpdateNotice(result) : result
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
/** Once per app start, ask the model to pass an available update on to the user. */
|
|
201
|
+
function addUpdateNotice(decision) {
|
|
202
|
+
const notice = updates.current()
|
|
203
|
+
if (!notice || noticeShown) return decision
|
|
204
|
+
noticeShown = true
|
|
205
|
+
const message = createUserMessage({
|
|
206
|
+
content: [{ type: 'text', text: `[${SOURCE_KIND}] ${notice} At the end of your reply, tell the user this in one short line.` }],
|
|
207
|
+
source: { kind: SOURCE_KIND, notice: true }
|
|
208
|
+
})
|
|
209
|
+
return { ...decision, messages: [...decision.messages, message] }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function addMemory(agent, prompt, decision) {
|
|
213
|
+
try {
|
|
214
|
+
if (isSubagent(agent) || !prompt) return decision
|
|
215
|
+
if (!(await metaFor(agent))?.memory) return decision
|
|
216
|
+
const st = state(sidOf(agent))
|
|
217
|
+
const views = await loadViews(agent)
|
|
218
|
+
const parts = []
|
|
219
|
+
let layer
|
|
220
|
+
if (st.announce) {
|
|
221
|
+
st.announce = false
|
|
222
|
+
const list = renderChats(views, { max: cfg.surfaceMaxChats })
|
|
223
|
+
if (list) { parts.push(list); layer = 'surface' }
|
|
224
|
+
}
|
|
225
|
+
const range = parseTimeRange(prompt)
|
|
226
|
+
const { hits, slice } = matchAcross(views, prompt, { max: cfg.maxHits, range })
|
|
227
|
+
if (hits.length === 0 && range) {
|
|
228
|
+
parts.push(renderRange(views.filter((v) => !v.own), range))
|
|
229
|
+
} else {
|
|
230
|
+
let inline
|
|
231
|
+
if (hits.length === 1 || (hits.length > 1 && hits[0].score >= 2 * hits[1].score)) {
|
|
232
|
+
const top = hits[0]
|
|
233
|
+
const text = await readSource(chatDir(dirFor(agent), top.view.sid), top.id.slice(top.id.indexOf('n')))
|
|
234
|
+
if (text && text.length <= cfg.inlineMaxChars) inline = { id: top.id, text }
|
|
235
|
+
}
|
|
236
|
+
const block = renderHits(hits, { inline, slice })
|
|
237
|
+
if (block) parts.push(block)
|
|
238
|
+
}
|
|
239
|
+
if (parts.length === 0) return decision
|
|
240
|
+
const text = parts.join('\n\n')
|
|
241
|
+
meter.injected++
|
|
242
|
+
meter.injectedTokens += estimateTokens(text)
|
|
243
|
+
const message = createUserMessage({ content: [{ type: 'text', text }], source: layer ? { kind: SOURCE_KIND, layer } : { kind: SOURCE_KIND } })
|
|
244
|
+
return { ...decision, messages: [...decision.messages, message] }
|
|
245
|
+
} catch (error) {
|
|
246
|
+
console.error('context-graph: matcher failed (non-fatal)', error)
|
|
247
|
+
return decision
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ---------- meter ----------
|
|
252
|
+
ctx.on('llm/stream', (options, next) => {
|
|
253
|
+
const inner = next()
|
|
254
|
+
const bucket = options.purpose === PURPOSE ? meter.builder : meter.main
|
|
255
|
+
async function* tracked() {
|
|
256
|
+
try {
|
|
257
|
+
for await (const chunk of inner) {
|
|
258
|
+
if (chunk?.type === 'usage' && chunk.usage) {
|
|
259
|
+
bucket.input += chunk.usage.inputTokens ?? 0
|
|
260
|
+
bucket.cacheRead += chunk.usage.cacheReadTokens ?? 0
|
|
261
|
+
bucket.cacheWrite += chunk.usage.cacheWriteTokens ?? 0
|
|
262
|
+
bucket.output += chunk.usage.outputTokens ?? 0
|
|
263
|
+
}
|
|
264
|
+
yield chunk
|
|
265
|
+
}
|
|
266
|
+
} finally {
|
|
267
|
+
bucket.requests++
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return tracked()
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
// ---------- recall tool ----------
|
|
274
|
+
ctx.tools.register(defineTool({
|
|
275
|
+
name: 'recall',
|
|
276
|
+
description: [
|
|
277
|
+
'Fetch the exact text behind an id shown in a [context-graph] block. A topic id (c3n12) returns its verbatim source and linked topics;',
|
|
278
|
+
'a chat id (c3) returns that chat\'s topics; a day (2026-11-01) returns every chat and topic from that day; a turn id (t7) returns turn 7 of this chat word for word.',
|
|
279
|
+
'Call it when a gist is relevant but not precise enough to answer. Never invent an id; only use ids you were shown.'
|
|
280
|
+
].join(' '),
|
|
281
|
+
parameters: {
|
|
282
|
+
id: { type: 'string', required: true, description: 'c3n12 (topic), c3 (chat), 2026-11-01 (day) or t7 (turn of this chat).' },
|
|
283
|
+
depth: { type: 'integer', description: '0 (default): this topic only. 1: also include each linked topic\'s gist.' }
|
|
284
|
+
},
|
|
285
|
+
output: {
|
|
286
|
+
schema: {
|
|
287
|
+
type: 'object',
|
|
288
|
+
additionalProperties: false,
|
|
289
|
+
properties: {
|
|
290
|
+
status: { type: 'string', enum: ['ok', 'unknown'], required: true },
|
|
291
|
+
text: { type: 'string', required: true }
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
render: (_args, value) => [{ type: 'text', text: value.text }]
|
|
295
|
+
},
|
|
296
|
+
async execute(args, exec) {
|
|
297
|
+
const agent = exec.agent
|
|
298
|
+
if (!agent?.session) return { status: 'unknown', text: 'recall is unavailable: no session attached to this call.' }
|
|
299
|
+
const id = String(args.id ?? '').trim()
|
|
300
|
+
const ok = (text) => { meter.recalls++; return { status: 'ok', text } }
|
|
301
|
+
const turn = /^t(\d+)$/.exec(id)
|
|
302
|
+
if (turn) {
|
|
303
|
+
const text = turnTranscript((seq) => agent.session.eventAt(seq), Number(turn[1]))
|
|
304
|
+
return text === undefined ? { status: 'unknown', text: `No turn ${id} in this chat.` } : ok(`# ${id} (verbatim)\n\n${text}`)
|
|
305
|
+
}
|
|
306
|
+
const views = await loadViews(agent)
|
|
307
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(id)) return ok(renderRange(views.filter((v) => !v.own), parseTimeRange(id)))
|
|
308
|
+
const m = /^(?:c(\d+))?(n\d+)?$/.exec(id)
|
|
309
|
+
if (!m || (!m[1] && !m[2])) return { status: 'unknown', text: `"${id}" is not an id. Use c3n12, c3, 2026-11-01 or t7, and only ids you were shown.` }
|
|
310
|
+
const view = m[1] ? views.find((v) => String(v.cid) === m[1]) : views.find((v) => v.own)
|
|
311
|
+
if (!view) return { status: 'unknown', text: `No chat c${m[1]} shared in this workspace.` }
|
|
312
|
+
if (!m[2]) return ok(renderChatIndex(view))
|
|
313
|
+
const node = view.graph.nodes[m[2]]
|
|
314
|
+
if (!node) return { status: 'unknown', text: `No topic ${id}. Only use ids shown in a [context-graph] block.` }
|
|
315
|
+
node.hits = (node.hits ?? 0) + 1
|
|
316
|
+
const chat = chatDir(dirFor(agent), view.sid)
|
|
317
|
+
saveGraph(chat, view.graph).catch((error) => console.error('context-graph: hit count save failed', error))
|
|
318
|
+
const lines = [`# ${id} ${node.label}`, `${node.kind}. ${node.gist}`, '']
|
|
319
|
+
const source = await readSource(chat, m[2])
|
|
320
|
+
lines.push(source ? source.trim() : '(no verbatim source stored for this topic)')
|
|
321
|
+
const links = neighbors(view.graph, m[2])
|
|
322
|
+
if (links.length > 0) {
|
|
323
|
+
lines.push('', 'Linked topics:')
|
|
324
|
+
for (const link of links) {
|
|
325
|
+
const other = view.graph.nodes[link.id]
|
|
326
|
+
const otherId = `c${view.cid}${link.id}`
|
|
327
|
+
const arrow = link.direction === 'out' ? `${id} ${link.relation} ${otherId}` : `${otherId} ${link.relation} ${id}`
|
|
328
|
+
lines.push(`- ${arrow}: ${other.label}${args.depth >= 1 ? ` — ${other.gist}` : ''}`)
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return ok(lines.join('\n'))
|
|
332
|
+
}
|
|
333
|
+
}))
|
|
334
|
+
|
|
335
|
+
// ---------- /context-graph command (nested: only where a human-command surface exists) ----------
|
|
336
|
+
ctx.plugin({
|
|
337
|
+
name: 'context-graph-command',
|
|
338
|
+
inject: ['commands'],
|
|
339
|
+
apply(cmdCtx) {
|
|
340
|
+
cmdCtx.effect(function* () {
|
|
341
|
+
yield cmdCtx.commands.register({
|
|
342
|
+
name: 'context-graph',
|
|
343
|
+
description: `Chat memory graph: /context-graph ${USAGE}`,
|
|
344
|
+
// Without `input`, dsh clients run the bare name and send any typed arguments to the model as chat.
|
|
345
|
+
input: { hint: USAGE },
|
|
346
|
+
async handler(invocation) {
|
|
347
|
+
const [sub = 'stats', ...rest] = invocation.rawInput.trim().split(/\s+/).filter(Boolean)
|
|
348
|
+
const agent = invocation.agent
|
|
349
|
+
const sid = sidOf(agent)
|
|
350
|
+
if (sub === 'on') {
|
|
351
|
+
await setFlags(agent, { memory: true })
|
|
352
|
+
state(sid).announce = true
|
|
353
|
+
const result = await agent.runMaintenance(() => build(agent, { force: true }))
|
|
354
|
+
const shared = (await loadViews(agent)).filter((v) => !v.own).length
|
|
355
|
+
const indexed = result.status === 'built' ? `${result.totalNodes} topics in this chat` : `nothing indexed yet (${result.reason ?? result.status})`
|
|
356
|
+
return { kind: 'success', text: `Memory on · ${indexed} · ${shared} shared ${shared === 1 ? 'chat' : 'chats'} to draw on\nThis chat indexes itself every ${cfg.buildEveryTurns} turns. /context-graph share lets other chats use it; /context-graph off stops it.` }
|
|
357
|
+
}
|
|
358
|
+
if (sub === 'off') {
|
|
359
|
+
await setFlags(agent, { memory: false })
|
|
360
|
+
return { kind: 'success', text: 'Memory off · nothing is added to requests\nThe graph file stays; /context-graph on resumes.' }
|
|
361
|
+
}
|
|
362
|
+
if (sub === 'share' || sub === 'unshare') {
|
|
363
|
+
const meta = await setFlags(agent, { shared: sub === 'share' })
|
|
364
|
+
const topics = Object.keys((await loadGraph(chatDir(dirFor(agent), sid))).nodes).length
|
|
365
|
+
const hint = meta.memory ? '' : '\nMemory is off for this chat, so it has nothing new to share until /context-graph on.'
|
|
366
|
+
return { kind: 'success', text: sub === 'share' ? `Shared with the workspace · ${topics} topics\nOther chats with memory on can now match and recall this chat.${hint}` : `No longer shared · ${topics} topics stay private to this chat` }
|
|
367
|
+
}
|
|
368
|
+
if (sub === 'build') {
|
|
369
|
+
const result = await agent.runMaintenance(() => build(agent, { force: true }))
|
|
370
|
+
if (result.status !== 'built') return { kind: 'error', text: `Build ${result.status}: ${result.reason ?? ''}`.trim() }
|
|
371
|
+
return { kind: 'success', text: `${result.nodes} topics indexed (${result.newNodes} new) · this chat has ${result.totalNodes} topics\nLinks: ${result.edges}\nFolder: ${result.dir}` }
|
|
372
|
+
}
|
|
373
|
+
const views = await loadViews(agent)
|
|
374
|
+
if (sub === 'map') {
|
|
375
|
+
const all = []
|
|
376
|
+
for (const { sid: other, meta } of await listChats(dirFor(agent))) all.push({ sid: other, cid: meta.cid, meta, own: other === sid, graph: await loadGraph(chatDir(dirFor(agent), other)) })
|
|
377
|
+
return { kind: 'success', text: `${all.length} ${all.length === 1 ? 'chat' : 'chats'} in ${dirFor(agent)}\n${renderMap(all)}` }
|
|
378
|
+
}
|
|
379
|
+
if (sub === 'find') {
|
|
380
|
+
const query = rest.join(' ')
|
|
381
|
+
const { hits } = matchAcross(views, query, { max: cfg.maxHits, range: parseTimeRange(query) })
|
|
382
|
+
if (hits.length === 0) return { kind: 'success', text: 'No matching topics in shared chats' }
|
|
383
|
+
return { kind: 'success', text: [`${hits.length} matching ${hits.length === 1 ? 'topic' : 'topics'}`, ...hits.map(({ id, node }) => `${id} ${node.label}: ${node.gist}`)].join('\n') }
|
|
384
|
+
}
|
|
385
|
+
if (sub !== 'stats') return { kind: 'error', text: `Usage: /context-graph ${USAGE}` }
|
|
386
|
+
const own = views.find((v) => v.own)
|
|
387
|
+
const m = meter.main
|
|
388
|
+
const cached = m.cacheRead + m.cacheWrite
|
|
389
|
+
const hitRate = m.input + cached > 0 ? Math.round((m.cacheRead / (m.input + cached)) * 100) : 0
|
|
390
|
+
const session = agent.session
|
|
391
|
+
const pending = collectSpan(session.surface.nodes, (seq) => session.eventAt(seq), state(sid).lastBuiltSeq).userTurns
|
|
392
|
+
const shared = views.filter((v) => !v.own).length
|
|
393
|
+
return {
|
|
394
|
+
kind: 'success',
|
|
395
|
+
text: [
|
|
396
|
+
`Memory ${own?.meta.memory ? 'on' : 'off'} · ${Object.keys(own?.graph.nodes ?? {}).length} topics in this chat · ${own?.meta.shared ? 'shared' : 'private'} · ${shared} shared ${shared === 1 ? 'chat' : 'chats'} · ${pending} turns waiting to be indexed${updates.current() ? ' · update available' : ''}`,
|
|
397
|
+
`Version ${version}: ${updates.current() ?? 'no newer version found'}`,
|
|
398
|
+
`Graph folder: ${dirFor(agent)}`,
|
|
399
|
+
'Counters below are since the app started:',
|
|
400
|
+
`Builds: ${meter.builds}; builder tokens in/out: ${meter.builder.input + meter.builder.cacheRead + meter.builder.cacheWrite}/${meter.builder.output}`,
|
|
401
|
+
`Injected blocks: ${meter.injected} (~${meter.injectedTokens} tokens); recalls: ${meter.recalls}`,
|
|
402
|
+
`Main requests: ${m.requests}; input ${m.input} uncached + ${m.cacheRead} cache-read + ${m.cacheWrite} cache-write; cache-hit ${hitRate}%`
|
|
403
|
+
].join('\n')
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
})
|
|
407
|
+
}, 'context-graph command')
|
|
408
|
+
}
|
|
409
|
+
})
|
|
410
|
+
}
|