@yolk_vat-y/dsh-project-memory 0.6.12 → 0.7.1
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 +68 -20
- package/README.md +14 -105
- package/README.zh-CN.md +14 -105
- package/client/client.js +311 -163
- package/client/client.js.map +1 -1
- package/package.json +8 -8
- package/scripts/file-peak.mjs +1 -1
- package/scripts/tool-schema-size.mjs +1 -1
- package/skills/project-memory/SKILL.md +1 -1
- package/skills/project-memory/scripts/pm-digest.mjs +20 -6
- package/src/audit.js +10 -2
- package/src/auto-inject.js +124 -38
- package/src/client/MemoryView.tsx +121 -51
- package/src/client/TaskCommandNode.tsx +6 -4
- package/src/client/TaskComponents.tsx +14 -26
- package/src/client/TaskPanel.tsx +44 -23
- package/src/client/client.ts +4 -7
- package/src/client/i18n.ts +29 -0
- package/src/client/kind-label.js +38 -0
- package/src/client/paging.js +1 -14
- package/src/client/settings-card.tsx +9 -0
- package/src/client/slash.ts +4 -15
- package/src/client/task-data-store.ts +57 -7
- package/src/client/task-hooks.ts +0 -2
- package/src/client/task-scope.js +16 -1
- package/src/client/task-ui-store.ts +1 -1
- package/src/commands/insight-actions.js +85 -60
- package/src/commands/task-actions.js +7 -5
- package/src/commands/tasks.js +34 -10
- package/src/doc-index.js +3 -1
- package/src/enhancer.js +0 -12
- package/src/index-failures.js +1 -1
- package/src/index-pipeline.js +11 -2
- package/src/index.js +60 -14
- package/src/inject-visible.js +282 -0
- package/src/insight-store.js +129 -32
- package/src/lazy.js +29 -23
- package/src/llm-route.js +1 -1
- package/src/ops.js +18 -5
- package/src/parsers/pdfjs-parser.js +3 -33
- package/src/project-memory-service.js +195 -0
- package/src/project-profile.js +0 -4
- package/src/setup/taskbridge.js +38 -6
- package/src/store.js +638 -135
- package/src/symbols.js +2 -4
- package/src/tools/forget.js +3 -6
- package/src/tools/index-doc.js +61 -18
- package/src/tools/index-repo.js +16 -12
- package/src/tools/lesson-tools.js +5 -5
- package/src/tools/output.js +28 -0
- package/src/tools/query-memory.js +6 -9
- package/src/tools/remember.js +3 -6
- package/src/tools/stats.js +3 -6
- package/src/tools/task-tools.js +15 -8
- package/src/tools/watch-repo.js +15 -12
- package/src/util/flush.js +1 -1
- package/src/util/fs.js +135 -20
- package/src/util/message-source.js +20 -0
- package/src/util/relative-time.js +24 -0
- package/src/util/search.js +0 -1
- package/src/util/task-view.js +8 -15
- package/src/vector-index.js +2 -2
- package/src/watch.js +25 -8
- package/client/client.d.ts +0 -27
- package/client/client.d.ts.map +0 -1
- package/client/style.css +0 -395
|
@@ -5,7 +5,6 @@ import { pathToFileURL } from 'node:url'
|
|
|
5
5
|
const WORKER_SPECIFIER = 'pdfjs-dist/legacy/build/pdf.worker.mjs'
|
|
6
6
|
|
|
7
7
|
let pdfjsPromise
|
|
8
|
-
let configuredWorkerSrc
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
10
|
* 解析 pdf.js 的 worker 模块位置,返回 file:// URL;解析不到返回 undefined。
|
|
@@ -33,10 +32,10 @@ function loadPdfjs() {
|
|
|
33
32
|
if (!pdfjsPromise) {
|
|
34
33
|
pdfjsPromise = import('pdfjs-dist/legacy/build/pdf.mjs')
|
|
35
34
|
.then((mod) => {
|
|
36
|
-
//
|
|
35
|
+
// pdf.js 自己已设的默认值 > 按文件位置解析出来的 URL。
|
|
37
36
|
// 必须用 `||` 而不是 `??`:GlobalWorkerOptions.workerSrc 的初始值是空字符串
|
|
38
37
|
// (GlobalWorkerOptions.#src = ""),`??` 会停在 "" 上直接跳过兜底解析。
|
|
39
|
-
const workerSrc =
|
|
38
|
+
const workerSrc = mod.GlobalWorkerOptions.workerSrc || resolveWorkerSrc()
|
|
40
39
|
if (workerSrc) mod.GlobalWorkerOptions.workerSrc = workerSrc
|
|
41
40
|
return mod
|
|
42
41
|
})
|
|
@@ -85,7 +84,7 @@ function extractPageText(items) {
|
|
|
85
84
|
return lines.join('\n')
|
|
86
85
|
}
|
|
87
86
|
|
|
88
|
-
export async function parsePdf(filePath, {
|
|
87
|
+
export async function parsePdf(filePath, { maxPages = 1000 } = {}) {
|
|
89
88
|
const data = new Uint8Array(await readFile(filePath))
|
|
90
89
|
const { getDocument } = await loadPdfjs()
|
|
91
90
|
const loadingTask = getDocument({ data, ...PDFJS_OPTIONS })
|
|
@@ -97,50 +96,21 @@ export async function parsePdf(filePath, { pages = null, maxPages = 1000, backen
|
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
const pageList = []
|
|
100
|
-
let imageCount = 0
|
|
101
|
-
let sampledPages = 0
|
|
102
99
|
for (let n = 1; n <= total; n++) {
|
|
103
|
-
if (pages && !pages.has(n)) continue
|
|
104
100
|
const page = await doc.getPage(n)
|
|
105
101
|
const content = await page.getTextContent()
|
|
106
102
|
pageList.push({ page: n, text: extractPageText(content.items) })
|
|
107
|
-
|
|
108
|
-
if (collectLayoutStats && sampledPages < 5) {
|
|
109
|
-
const { OPS } = await loadPdfjs()
|
|
110
|
-
const opList = await page.getOperatorList()
|
|
111
|
-
for (const fn of opList.fnArray) {
|
|
112
|
-
if (fn === OPS.paintImageXObject || fn === OPS.paintInlineImageXObject) imageCount++
|
|
113
|
-
}
|
|
114
|
-
sampledPages++
|
|
115
|
-
}
|
|
116
103
|
}
|
|
117
104
|
if (pageList.length === 0) {
|
|
118
105
|
throw new Error('No pages matched the selection, or the PDF contains no extractable text')
|
|
119
106
|
}
|
|
120
107
|
|
|
121
|
-
const totalTextChars = pageList.reduce((sum, p) => sum + p.text.length, 0)
|
|
122
108
|
return {
|
|
123
109
|
pageCount: pageList.length,
|
|
124
110
|
pages: pageList,
|
|
125
111
|
markdown: buildMarkdown(pageList),
|
|
126
|
-
backend,
|
|
127
|
-
stats: collectLayoutStats
|
|
128
|
-
? {
|
|
129
|
-
totalTextChars,
|
|
130
|
-
avgImagesPerPage: sampledPages ? imageCount / sampledPages : 0,
|
|
131
|
-
}
|
|
132
|
-
: undefined,
|
|
133
112
|
}
|
|
134
113
|
} finally {
|
|
135
114
|
await loadingTask.destroy()
|
|
136
115
|
}
|
|
137
116
|
}
|
|
138
|
-
|
|
139
|
-
export function configurePdfjsWorker(workerSrc) {
|
|
140
|
-
configuredWorkerSrc = workerSrc
|
|
141
|
-
if (pdfjsPromise) {
|
|
142
|
-
pdfjsPromise.then((mod) => {
|
|
143
|
-
mod.GlobalWorkerOptions.workerSrc = workerSrc
|
|
144
|
-
})
|
|
145
|
-
}
|
|
146
|
-
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 原生服务 seam(D3-1):`ctx.provide('projectMemory', api)`,v1 只有四个动词
|
|
3
|
+
* `recall / remember / forget / stats`。纯加法、全同步、零 LLM;
|
|
4
|
+
* `remember/forget` 是写入口,先过 `assertSafeRoot`,`recall/stats` 只读。
|
|
5
|
+
*/
|
|
6
|
+
import path from 'node:path'
|
|
7
|
+
import { assertIndexRoot, assertSafeRoot, memoryRootFor, normalizeCitationFiles } from './util/fs.js'
|
|
8
|
+
import { ProjectMemoryStore, storeOverview } from './store.js'
|
|
9
|
+
import { cfgInsight, defaultGlobalFile, GlobalStore, removeMemory, saveInsight } from './insight-store.js'
|
|
10
|
+
import { insightBodyText, recallItems } from './recall.js'
|
|
11
|
+
import { forgetVectorRows } from './vector-index.js'
|
|
12
|
+
|
|
13
|
+
/** 默认四层与 `recallItems` 的默认值保持一致。 */
|
|
14
|
+
const DEFAULT_LAYERS = ['doc', 'symbol', 'experience', 'insight']
|
|
15
|
+
|
|
16
|
+
/** `snippet` 是展示用摘要,不追求全文;正文兜底用 `insightBodyText`(经验条目没有 summary)。 */
|
|
17
|
+
const SNIPPET_CHARS = 300
|
|
18
|
+
|
|
19
|
+
function resolveRoot(root) {
|
|
20
|
+
return path.resolve(root || process.cwd())
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 项目/global 两个 store 都由各自类做进程内缓存。 */
|
|
24
|
+
function openStores(root, config) {
|
|
25
|
+
const cfg = cfgInsight(config)
|
|
26
|
+
const memoryDir = memoryRootFor(root, config && config.memoryDir)
|
|
27
|
+
const store = new ProjectMemoryStore(memoryDir).load()
|
|
28
|
+
// 与工具/自动注入同一个 global 解析顺序:显式配置优先,否则用户家目录下的默认文件。
|
|
29
|
+
const globalStore = new GlobalStore(cfg.globalFile || defaultGlobalFile()).load()
|
|
30
|
+
return { cfg, memoryDir, store, globalStore }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 召回条目的来源路径:insight 适配器给 `sourcePath`,原始经验条目给 `files[0]` / `sourceFile`。 */
|
|
34
|
+
function sourcePathOf(item) {
|
|
35
|
+
if (!item || typeof item !== 'object') return ''
|
|
36
|
+
if (typeof item.sourcePath === 'string' && item.sourcePath) return item.sourcePath
|
|
37
|
+
if (Array.isArray(item.files) && typeof item.files[0] === 'string' && item.files[0]) return item.files[0]
|
|
38
|
+
if (typeof item.sourceFile === 'string' && item.sourceFile) return item.sourceFile
|
|
39
|
+
return ''
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `path:line`;无行号只有路径,无来源为空串。 */
|
|
43
|
+
function citationOf(root, item) {
|
|
44
|
+
const rel = sourcePathOf(item)
|
|
45
|
+
if (!rel) return ''
|
|
46
|
+
const abs = path.isAbsolute(rel) ? rel : path.join(root, rel)
|
|
47
|
+
return typeof item?.sourceLine === 'number' && item.sourceLine > 0 ? `${abs}:${item.sourceLine}` : abs
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function snippetOf(item) {
|
|
51
|
+
if (!item) return ''
|
|
52
|
+
// doc/symbol 用 summary/text;insight 适配器用 summary;经验条目回到 insightBodyText。
|
|
53
|
+
const text = item.summary || item.text || (item.kind ? insightBodyText(item) : '') || ''
|
|
54
|
+
return String(text).slice(0, SNIPPET_CHARS)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 只构造 api,不负责 provide;`ctx` 留给将来需要宿主能力时用。 */
|
|
58
|
+
export function createProjectMemoryService(ctx, config) {
|
|
59
|
+
return {
|
|
60
|
+
recall(query, opts = {}) {
|
|
61
|
+
const q = String(query ?? '')
|
|
62
|
+
// 空查询不是“全部”:ranker 会把前 limit 条当命中(分数 0)。
|
|
63
|
+
if (!q.trim()) return { results: [] }
|
|
64
|
+
if (opts.layers !== undefined && !Array.isArray(opts.layers)) {
|
|
65
|
+
throw new Error('projectMemory.recall: layers must be an array')
|
|
66
|
+
}
|
|
67
|
+
const root = resolveRoot(opts.root)
|
|
68
|
+
// 只读动词也要过根护栏:被 deny 的根不能借 recall 加载(超大 store 的 load 本身就危险),
|
|
69
|
+
// 更不能在加载时顺手建出 `.dsh-project-memory` / `.gitignore`。
|
|
70
|
+
assertSafeRoot(root, { config })
|
|
71
|
+
assertIndexRoot(root, opts.root)
|
|
72
|
+
const limit = Number.isFinite(opts.limit) && opts.limit > 0 ? Math.min(50, Math.floor(opts.limit)) : 8
|
|
73
|
+
const layers = Array.isArray(opts.layers) && opts.layers.length ? opts.layers : DEFAULT_LAYERS
|
|
74
|
+
const { store, globalStore } = openStores(root, config)
|
|
75
|
+
const recalled = recallItems({
|
|
76
|
+
store,
|
|
77
|
+
globalStore,
|
|
78
|
+
queries: [q],
|
|
79
|
+
layers,
|
|
80
|
+
limit,
|
|
81
|
+
// task 级条目只在绑定任务时可见;remember(scope:'task') 写进去的必须能读回来。
|
|
82
|
+
boundTaskId: opts.taskId || null,
|
|
83
|
+
})
|
|
84
|
+
const results = (recalled.flat || []).map((hit) => {
|
|
85
|
+
const item = hit.item || {}
|
|
86
|
+
return {
|
|
87
|
+
id: item.insightId || item.id || '',
|
|
88
|
+
title: String(item.title || item.problem || '(untitled)'),
|
|
89
|
+
snippet: snippetOf(item),
|
|
90
|
+
citation: citationOf(root, item),
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
return { results }
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
remember(entry, opts = {}) {
|
|
97
|
+
const root = resolveRoot(opts.root)
|
|
98
|
+
const scope = opts.scope || 'project'
|
|
99
|
+
const taskId = opts.taskId
|
|
100
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
101
|
+
throw new Error('projectMemory.remember: entry must be an object')
|
|
102
|
+
}
|
|
103
|
+
// 调用方错误:在打开 store 之前就报。
|
|
104
|
+
if (scope === 'task' && !taskId) {
|
|
105
|
+
throw new Error("projectMemory.remember: scope='task' requires taskId")
|
|
106
|
+
}
|
|
107
|
+
// 写入口先过根护栏,拒绝发生在 store 目录被建出来之前。
|
|
108
|
+
assertSafeRoot(root, { config })
|
|
109
|
+
// 只过 assertSafeRoot 不够:拼错的 root(不存在)也会被 store 的 mkdir 建出来。
|
|
110
|
+
assertIndexRoot(root, opts.root)
|
|
111
|
+
// 与 save_lesson 同一条写入期路径约束:只收项目内相对路径。
|
|
112
|
+
const citations = normalizeCitationFiles(entry.files, root)
|
|
113
|
+
const raw = { ...entry, files: citations.files }
|
|
114
|
+
const { cfg, store, globalStore } = openStores(root, config)
|
|
115
|
+
let saved
|
|
116
|
+
// 与 save_lesson 同一条写入路径与落盘时机。
|
|
117
|
+
store.commit((s) => {
|
|
118
|
+
saved = saveInsight({ store: s, globalStore, raw, scope, taskId, cfg, source: 'api' })
|
|
119
|
+
return saved
|
|
120
|
+
})
|
|
121
|
+
globalStore.commit()
|
|
122
|
+
if (!saved || !saved.ok) {
|
|
123
|
+
throw new Error(`projectMemory.remember failed: ${saved && saved.error ? saved.error : 'unknown error'}`)
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
id: saved.id,
|
|
127
|
+
action: saved.action,
|
|
128
|
+
// project 与 global 任一侧没落盘都算未持久化:只查 store 会把 global 写失败报成成功。
|
|
129
|
+
...(store.lastSaveOk === false || globalStore.lastCommitOk === false ? { persisted: false } : {}),
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
// v1 契约只返回 `{ deleted }`:命中数超过 `maxForgetMatches` 时额外带 `tooMany/matched`,
|
|
134
|
+
// 其余失败与"没有命中"同形(`deleted: []`),保持四动词返回形状稳定。
|
|
135
|
+
forget(idOrQuery, opts = {}) {
|
|
136
|
+
const root = resolveRoot(opts.root)
|
|
137
|
+
// 删除也是写盘入口:先过根护栏,拒绝发生在 store / 删除留痕文件被写出之前。
|
|
138
|
+
assertSafeRoot(root, { config })
|
|
139
|
+
assertIndexRoot(root, opts.root)
|
|
140
|
+
const { cfg, memoryDir, store, globalStore } = openStores(root, config)
|
|
141
|
+
let removed
|
|
142
|
+
store.commit((s) => {
|
|
143
|
+
removed = removeMemory({ store: s, globalStore, idOrQuery, maxMatches: cfg.maxForgetMatches })
|
|
144
|
+
return removed
|
|
145
|
+
})
|
|
146
|
+
globalStore.commit()
|
|
147
|
+
const deleted = ((removed && removed.removed) || []).map((it) => it.id)
|
|
148
|
+
// 向量缓存也是"这条记忆"的一部分:与 forget 工具同一承诺,留着就不算真删。
|
|
149
|
+
// 缓存清理失败不影响删除承诺(条目已经从真相源移除)。
|
|
150
|
+
try {
|
|
151
|
+
forgetVectorRows({ dir: memoryDir, ids: deleted })
|
|
152
|
+
} catch {
|
|
153
|
+
/* ignore:vectors.json 是缓存,不是真相源 */
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
deleted,
|
|
157
|
+
// 命中超 cap 时没有任何删除:调用方需要能区分“没命中”与“命中太多、拒绝删”。
|
|
158
|
+
...(removed && removed.tooMany ? { tooMany: true, matched: (removed.matches || []).length } : {}),
|
|
159
|
+
...(store.lastSaveOk === false || globalStore.lastCommitOk === false ? { persisted: false } : {}),
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
stats(opts = {}) {
|
|
164
|
+
const root = resolveRoot(opts.root)
|
|
165
|
+
assertSafeRoot(root, { config })
|
|
166
|
+
assertIndexRoot(root, opts.root)
|
|
167
|
+
const { store, globalStore } = openStores(root, config)
|
|
168
|
+
const overview = storeOverview(store)
|
|
169
|
+
return {
|
|
170
|
+
files: overview.files,
|
|
171
|
+
entries: overview.entries,
|
|
172
|
+
experience: overview.experience,
|
|
173
|
+
latest: overview.latest,
|
|
174
|
+
insights: store.insightItems().length,
|
|
175
|
+
// task 级条目不在 insights.json 里:不单列会让 remember(scope:'task') 的写入在 stats 里消失。
|
|
176
|
+
taskInsights: store.getTasks().reduce((n, t) => n + (Array.isArray(t?.insights) ? t.insights.length : 0), 0),
|
|
177
|
+
global: globalStore.items().length,
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 安装服务:老宿主/测试桩没有 `ctx.provide` 时**安全跳过**(返回 null,不抛),
|
|
185
|
+
* 否则 `ctx.provide('projectMemory', api)` 并返回同一个 api。
|
|
186
|
+
* @param {object} ctx - cordis 上下文
|
|
187
|
+
* @param {object} config - 插件 Config
|
|
188
|
+
* @returns {object|null} 已注册的 api;宿主无 provide 时为 null
|
|
189
|
+
*/
|
|
190
|
+
export function installProjectMemoryService(ctx, config) {
|
|
191
|
+
if (typeof ctx?.provide !== 'function') return null
|
|
192
|
+
const api = createProjectMemoryService(ctx, config)
|
|
193
|
+
ctx.provide('projectMemory', api)
|
|
194
|
+
return api
|
|
195
|
+
}
|
package/src/project-profile.js
CHANGED
package/src/setup/taskbridge.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// 事实依据:dsh session/event 签名 (session, event);todo/write data.todos;
|
|
3
3
|
// tool/call data.arguments 为 JSON 字符串;fs 工具名 read/write/edit/read_image,参数 file_path。
|
|
4
4
|
import path from 'node:path'
|
|
5
|
+
import { existsSync, statSync } from 'node:fs'
|
|
5
6
|
import { createHash, randomUUID } from 'node:crypto'
|
|
6
7
|
import { memoryRootFor, resolveProjectMemoryRoot } from '../util/fs.js'
|
|
7
8
|
import { sessionWorkingDir } from '../util/session-dir.js'
|
|
@@ -9,6 +10,7 @@ import { ProjectMemoryStore } from '../store.js'
|
|
|
9
10
|
import { BoundedMap } from '../util/session-cache.js'
|
|
10
11
|
import { stepContent, stepStatus } from '../util/task-view.js'
|
|
11
12
|
import { patchFilePaths } from '../util/patch.js'
|
|
13
|
+
import { flushDirtyStores } from '../util/flush.js'
|
|
12
14
|
|
|
13
15
|
// apply_patch 的参数是整段补丁文本,路径在正文里(见 util/patch.js),单独分支处理。
|
|
14
16
|
const FS_FILE_TOOLS = new Set(['read', 'write', 'edit', 'read_image', 'str_replace_editor', 'apply_patch'])
|
|
@@ -54,6 +56,13 @@ export function projectRootFor(cwd, config) {
|
|
|
54
56
|
export function taskStoreFor(cwd, config) {
|
|
55
57
|
const root = projectRootFor(cwd, config)
|
|
56
58
|
if (!root) return { root: null, store: null }
|
|
59
|
+
// 会话 cwd 可能已被删除/改名:此时不能 load(save 的 mkdirSync 会把项目根重新造出来)。
|
|
60
|
+
// 与 index_repo / watch_repo 的同一条护栏。
|
|
61
|
+
try {
|
|
62
|
+
if (!existsSync(root) || !statSync(root).isDirectory()) return { root: null, store: null }
|
|
63
|
+
} catch {
|
|
64
|
+
return { root: null, store: null }
|
|
65
|
+
}
|
|
57
66
|
return { root, store: new ProjectMemoryStore(memoryRootFor(root, config.memoryDir)).load() }
|
|
58
67
|
}
|
|
59
68
|
|
|
@@ -83,11 +92,13 @@ export function firstTextOf(content) {
|
|
|
83
92
|
* 规则:任务没有步骤时不推(避免误清模型手头正在用的宿主清单)。
|
|
84
93
|
* @returns {boolean} 是否实际推送
|
|
85
94
|
*/
|
|
86
|
-
export function adoptStepsToSession(session, task) {
|
|
95
|
+
export function adoptStepsToSession(session, task, { force = false } = {}) {
|
|
87
96
|
if (!session || typeof session.append !== 'function') return false
|
|
88
97
|
const steps = Array.isArray(task?.steps) ? task.steps : null
|
|
89
|
-
|
|
90
|
-
|
|
98
|
+
// 隐式路径(例如工具顺手更新文件热点)不推空,避免清掉模型手头的清单;显式切换/清空必须推,
|
|
99
|
+
// 否则面板绑到 B、输入框里仍是 A 的步骤。
|
|
100
|
+
if ((!steps || steps.length === 0) && !force) return false
|
|
101
|
+
const todos = (steps || []).map((s) => ({ content: String(stepContent(s)), status: stepStatus(s) }))
|
|
91
102
|
session.append('todo/write', { todos })
|
|
92
103
|
return true
|
|
93
104
|
}
|
|
@@ -172,6 +183,19 @@ export function touchTaskFile(task, rel, kind, now) {
|
|
|
172
183
|
}
|
|
173
184
|
}
|
|
174
185
|
|
|
186
|
+
/** 判据与 auto-inject 的 lastUserText 一致:缺 source 或 kind==='user' 才是人话;
|
|
187
|
+
* 本插件注入(含旧 {plugin} 形状)与 agent-message 等一律排除。 */
|
|
188
|
+
function isHumanUserMessage(data) {
|
|
189
|
+
if (!data || typeof data !== 'object') return false
|
|
190
|
+
if (data.role != null && data.role !== 'user') return false
|
|
191
|
+
const source = data.source
|
|
192
|
+
if (!source || typeof source !== 'object') return true
|
|
193
|
+
if (source.plugin === 'dsh-project-memory') return false
|
|
194
|
+
const kind = source.kind
|
|
195
|
+
// 空 kind 与缺 kind 同义:auto-inject 的判据是 `if (kind && kind !== 'user') continue`。
|
|
196
|
+
return !kind || kind === 'user'
|
|
197
|
+
}
|
|
198
|
+
|
|
175
199
|
/**
|
|
176
200
|
* 单条会话事件处理(导出便于测试):user/message 记首条真人文本并刷新 lastHumanAt;
|
|
177
201
|
* todo/write → 已绑定则覆盖 steps,未绑定则自动新建任务并绑定(子代理会话除外,
|
|
@@ -189,7 +213,7 @@ export function onSessionEvent(config, session, event, meta, ctx) {
|
|
|
189
213
|
|
|
190
214
|
// 人类消息:记首条真人文本 + 若已绑定任务则刷新 lastHumanAt(供“模型自维护不回声任务卡”判断)
|
|
191
215
|
if (type === 'user/message') {
|
|
192
|
-
if (event.data
|
|
216
|
+
if (isHumanUserMessage(event.data)) {
|
|
193
217
|
const text = firstTextOf(event.data?.content)
|
|
194
218
|
const m = meta.get(sessionId) || {}
|
|
195
219
|
if (!m.firstHuman && text) m.firstHuman = text
|
|
@@ -301,7 +325,9 @@ export function onSessionEvent(config, session, event, meta, ctx) {
|
|
|
301
325
|
? args.file_path
|
|
302
326
|
: typeof args?.path === 'string' ? args.path : null
|
|
303
327
|
if (!raw) return
|
|
304
|
-
|
|
328
|
+
// 相对路径以会话当前工作目录为基准,与宿主 read/write 一致;项目根只在 cwd 缺失时兜底。
|
|
329
|
+
const base = typeof cwd === 'string' && cwd ? cwd : root
|
|
330
|
+
const rel = normalizeRelFile(root, path.isAbsolute(raw) ? raw : path.resolve(base, raw))
|
|
305
331
|
if (!rel) return
|
|
306
332
|
let kind = null
|
|
307
333
|
if (data.name === 'str_replace_editor') {
|
|
@@ -323,7 +349,8 @@ export function flushTaskStore(cwd, config) {
|
|
|
323
349
|
if (!cwd) return
|
|
324
350
|
try {
|
|
325
351
|
const { store } = taskStoreFor(cwd, config)
|
|
326
|
-
|
|
352
|
+
// 回合结束的 flush 不能按默认 15s 等锁:写路径已经有同步等待,这里只做一次短兜底。
|
|
353
|
+
if (store) store.save({ timeoutMs: 1500 })
|
|
327
354
|
} catch (err) {
|
|
328
355
|
console.error('[dsh-project-memory] taskbridge flush:', err?.message || err)
|
|
329
356
|
}
|
|
@@ -351,10 +378,15 @@ export function setupTaskbridge(ctx, config) {
|
|
|
351
378
|
// 文件热点/人类消息戳记是延迟写:回合结束补一次落盘,崩溃时最多丢一个回合的计数。
|
|
352
379
|
if (event?.type === 'turn/end') {
|
|
353
380
|
flushSessionTaskStore(session, config, ctx)
|
|
381
|
+
// 本回合可能 cd 过:按“当时的 cwd”找根会漏掉绑定根。把所有标脏的 store 一起落盘。
|
|
382
|
+
flushDirtyStores({ timeoutMs: 1500 })
|
|
354
383
|
}
|
|
355
384
|
})
|
|
356
385
|
ctx.on('session/disposed', (session) => {
|
|
357
386
|
flushSessionTaskStore(session, config, ctx)
|
|
387
|
+
// 会话可能 cd 过:disposed 时按当前 cwd 只能找到最后一个根,之前根里的热点
|
|
388
|
+
// (tool/call 用 save:false 延迟写)会一直留到进程退出。与 turn/end 一样兜底。
|
|
389
|
+
flushDirtyStores({ timeoutMs: 1500 })
|
|
358
390
|
if (session?.id) meta.delete(session.id)
|
|
359
391
|
})
|
|
360
392
|
ctx.effect(() => () => meta.clear())
|