@yolk_vat-y/dsh-project-memory 0.6.1 → 0.6.2
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 +88 -2
- package/README.md +10 -10
- package/README.zh-CN.md +10 -10
- package/client/client.js +363 -123
- package/client/client.js.map +1 -1
- package/package.json +11 -8
- package/src/auto-inject.js +25 -1
- package/src/client/MemoryView.tsx +157 -44
- package/src/client/TaskPanel.module.css +23 -0
- package/src/client/locales.ts +10 -0
- package/src/client/paging.js +165 -0
- package/src/commands/insight-actions.js +85 -11
- package/src/commands/invocation.js +4 -1
- package/src/index.js +10 -0
- package/src/insight-store.js +361 -35
- package/src/lazy.js +5 -3
- package/src/recall.js +13 -8
- package/src/setup/taskbridge.js +9 -4
- package/src/store.js +72 -119
- package/src/tools/forget.js +26 -6
- package/src/tools/lesson-tools.js +21 -8
- package/src/tools/query-memory.js +10 -4
- package/src/tools/remember.js +30 -14
- package/src/types.js +8 -1
- package/src/util/fs.js +3 -1
- package/src/util/session-dir.js +48 -0
- package/src/vector-index.js +3 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yolk_vat-y/dsh-project-memory",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Persistent project memory for dsh agents: index docs (PDF/Markdown/text) and code symbols into a searchable per-workspace store, recall them with cited sources, and keep experience entries (problems -> solutions) searchable on demand.",
|
|
5
5
|
"author": "00080000 <3388065969@qq.com>",
|
|
6
6
|
"type": "module",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"url": "https://github.com/00080000/dsh-project-memory.git"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
|
-
"test": "node test/run-test.mjs && node test/taskbridge.test.mjs && node test/insight-store.test.mjs && node test/reflection-pipeline.test.mjs && node test/auto-inject.test.mjs && node test/insight-actions.test.mjs && node test/host-contract.test.mjs && node test/llm-route.test.mjs && node test/doc-index.test.mjs && node test/chunker.test.mjs && node test/client-hints.test.mjs && node test/recall.test.mjs && node test/recall-vector-fusion.test.mjs && node test/vector-index.test.mjs && node test/readiness.test.mjs && node test/insight-derive.test.mjs && node test/readiness-eval.test.mjs && node test/ops.test.mjs && node test/injection-audit.test.mjs && node test/injection-budget.test.mjs && node test/injection-scenarios.test.mjs && node test/bugfix-0.5.7.test.mjs && node test/client-icons.test.mjs && node test/client-slash.test.mjs && node test/client-toolview.test.mjs && node test/workflow-command.test.mjs && node test/client-session-id.test.mjs && node test/client-degraded.test.mjs && node test/watch-idle.test.mjs && node test/citation-paths.test.mjs && node test/task-view.test.mjs && node test/root-guards.test.mjs && node test/store-gitignore.test.mjs && node test/atomic-write.test.mjs && node test/store-cache.test.mjs && node test/enhancer.test.mjs",
|
|
22
|
+
"test": "node test/run-test.mjs && node test/taskbridge.test.mjs && node test/insight-store.test.mjs && node test/reflection-pipeline.test.mjs && node test/auto-inject.test.mjs && node test/insight-actions.test.mjs && node test/insight-paging.test.mjs && node test/config-schema.test.mjs && node test/host-contract.test.mjs && node test/session-dir.test.mjs && node test/llm-route.test.mjs && node test/doc-index.test.mjs && node test/chunker.test.mjs && node test/client-hints.test.mjs && node test/client-paging.test.mjs && node test/recall.test.mjs && node test/recall-vector-fusion.test.mjs && node test/vector-index.test.mjs && node test/readiness.test.mjs && node test/insight-derive.test.mjs && node test/readiness-eval.test.mjs && node test/ops.test.mjs && node test/injection-audit.test.mjs && node test/injection-budget.test.mjs && node test/injection-scenarios.test.mjs && node test/bugfix-0.5.7.test.mjs && node test/client-icons.test.mjs && node test/client-slash.test.mjs && node test/client-toolview.test.mjs && node test/workflow-command.test.mjs && node test/client-session-id.test.mjs && node test/client-degraded.test.mjs && node test/watch-idle.test.mjs && node test/citation-paths.test.mjs && node test/task-view.test.mjs && node test/root-guards.test.mjs && node test/store-gitignore.test.mjs && node test/atomic-write.test.mjs && node test/store-cache.test.mjs && node test/enhancer.test.mjs",
|
|
23
23
|
"eval:injection": "node test/injection-scenarios.test.mjs",
|
|
24
24
|
"typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.client.json",
|
|
25
25
|
"selfcheck:triggers": "node test/injection-scenarios.test.mjs --selfcheck",
|
|
@@ -52,12 +52,12 @@
|
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@deepseek-ai/cordis": "^4.0.4",
|
|
55
|
-
"@deepseek-ai/dsh-client-locale": "0.2.
|
|
56
|
-
"@deepseek-ai/dsh-client-store": "0.2.
|
|
57
|
-
"@deepseek-ai/dsh-client-ui-primitives": "0.2.
|
|
58
|
-
"@deepseek-ai/dsh-client-ui-slots": "0.2.
|
|
59
|
-
"@deepseek-ai/dsh-llm": "0.2.
|
|
60
|
-
"@deepseek-ai/dsh-tools": "0.2.
|
|
55
|
+
"@deepseek-ai/dsh-client-locale": "0.2.1-rc.2",
|
|
56
|
+
"@deepseek-ai/dsh-client-store": "0.2.1-rc.2",
|
|
57
|
+
"@deepseek-ai/dsh-client-ui-primitives": "0.2.1-rc.2",
|
|
58
|
+
"@deepseek-ai/dsh-client-ui-slots": "0.2.1-rc.2",
|
|
59
|
+
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
|
|
60
|
+
"@deepseek-ai/dsh-tools": "0.2.1-rc.2",
|
|
61
61
|
"@deepseek-ai/schemastery": "^3.18.2",
|
|
62
62
|
"@tsdown/css": "0.22.14",
|
|
63
63
|
"@types/react": "^18.3.31",
|
|
@@ -91,6 +91,9 @@
|
|
|
91
91
|
}
|
|
92
92
|
},
|
|
93
93
|
"dsh": {
|
|
94
|
+
"engines": {
|
|
95
|
+
"dsh": ">=0.0.1-rc.1 <0.1.0 || >=0.1.0-rc.1 <0.3.0-0"
|
|
96
|
+
},
|
|
94
97
|
"bundle": {
|
|
95
98
|
"patch": "./cordis.patch.yml"
|
|
96
99
|
},
|
package/src/auto-inject.js
CHANGED
|
@@ -14,6 +14,7 @@ import { stepContent } from './util/task-view.js'
|
|
|
14
14
|
import { insightMatchText, normalizedTokenOverlap } from './similarity.js'
|
|
15
15
|
import { createVectorRerank, vectorItemsFrom } from './vector-index.js'
|
|
16
16
|
import { BoundedMap, SessionCache } from './util/session-cache.js'
|
|
17
|
+
import { sessionWorkingDir } from './util/session-dir.js'
|
|
17
18
|
import { cfgInsight, GlobalStore, defaultGlobalFile, recordHit } from './insight-store.js'
|
|
18
19
|
import { projectTags } from './project-profile.js'
|
|
19
20
|
import { rankEntriesMergedScored } from './util/search.js'
|
|
@@ -136,6 +137,27 @@ export function cfgEngine(config) {
|
|
|
136
137
|
}
|
|
137
138
|
}
|
|
138
139
|
|
|
140
|
+
/**
|
|
141
|
+
* 给写入路径(`save_lesson` / `remember`)用的向量打分器:**没装/没配就是 null**。
|
|
142
|
+
*
|
|
143
|
+
* 与注入路径共用一个解析入口(`cfgEngine` + `createVectorRerank`),所以"装了 leanvec 就用、
|
|
144
|
+
* 没装就纯词法"这条规则只有一处实现、一处配置。返回的函数是同步的,供 `saveInsight` 在
|
|
145
|
+
* 写事务里直接调用;任何异常都降级成 null,绝不因为可选的向量层挡住写入。
|
|
146
|
+
* @param {object} config - 插件配置
|
|
147
|
+
* @param {{storeDir: string|null, items: object[]}} o - 持久化位置与候选池(见 vectorItemsFrom)
|
|
148
|
+
* @returns {Promise<((text: string, items: object[]) => Map<string, number>)|null>}
|
|
149
|
+
*/
|
|
150
|
+
export async function createWriteCosine(config, { storeDir = null, items = [] } = {}) {
|
|
151
|
+
const engine = cfgEngine(config)
|
|
152
|
+
try {
|
|
153
|
+
const rerank = await createVectorRerank({ storeDir, items, vectorDir: engine.vectorDir, alpha: engine.vectorAlpha })
|
|
154
|
+
if (!rerank || typeof rerank.score !== 'function') return null
|
|
155
|
+
return (text, cands) => rerank.score(text, cands)
|
|
156
|
+
} catch {
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
139
161
|
export function lastUserText(messages) {
|
|
140
162
|
if (!Array.isArray(messages)) return ''
|
|
141
163
|
let fallback = ''
|
|
@@ -681,7 +703,9 @@ function warnUnsafeSessionRoot(root) {
|
|
|
681
703
|
*/
|
|
682
704
|
async function injectForStep({ payload, decision, config, cfg, audit, shadow, sessions }) {
|
|
683
705
|
const session = payload && payload.agent && payload.agent.session
|
|
684
|
-
|
|
706
|
+
// 与工具侧同一口径的「当前工作目录」:0.2.1+ 走 workingDirectory 投影(cd 后会变),
|
|
707
|
+
// 老宿主回退 header.cwd。少了这一步,cd 之后审计与懒索引会落在两个不同的 store 里。
|
|
708
|
+
const cwd = sessionWorkingDir(session, { agent: payload?.agent })
|
|
685
709
|
const sessionId = session && session.id
|
|
686
710
|
if (!cwd || !sessionId) return null
|
|
687
711
|
// 会话 cwd 是家目录/系统目录时整条链路停在这里:不读 store(历史遗留的超大 store
|
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
* Memory View — 记忆三作用域列表(Task 草稿 / Project / Global)
|
|
3
3
|
* 数据通道与任务面板一致:remote.commands.execute 执行 /insight 命令,取 JSON 载荷。
|
|
4
4
|
* 动作:confirm(草稿审核) / promote(向上提升) / demote(向下降级,需绑定任务) / archive / restore / delete
|
|
5
|
+
*
|
|
6
|
+
* 列表是**增量加载**的:首屏只取 PAGE_SIZE 条,滚到底前 600px 预取下一页追加。
|
|
7
|
+
* 分页的决策逻辑在 ./paging.js(纯函数,可单测),本组件只把它接到渲染上。
|
|
5
8
|
*/
|
|
6
9
|
import { useEffect, useCallback, useRef, useState } from 'react'
|
|
7
10
|
import { Button } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
8
11
|
import { service } from './services.ts'
|
|
12
|
+
import { PAGE_SIZE, PREFETCH_MARGIN, SPINNER_DELAY_MS, appendUnique, createRequestGate, insightListLine, normalizeWindow, progressCounts, reloadWindowRequest } from './paging.js'
|
|
9
13
|
import css from './TaskPanel.module.css'
|
|
10
14
|
|
|
11
15
|
export type InsightScope = 'task' | 'project' | 'global'
|
|
@@ -39,6 +43,9 @@ export interface InsightRow {
|
|
|
39
43
|
interface ListPayload {
|
|
40
44
|
scope: InsightScope
|
|
41
45
|
count: number
|
|
46
|
+
total: number
|
|
47
|
+
offset: number
|
|
48
|
+
nextOffset: number | null
|
|
42
49
|
items: InsightRow[]
|
|
43
50
|
}
|
|
44
51
|
|
|
@@ -55,7 +62,10 @@ export function parseInsightPayloadText(text?: string | null): ListPayload | nul
|
|
|
55
62
|
try {
|
|
56
63
|
const data = JSON.parse(jsonBlock)
|
|
57
64
|
if (!Array.isArray(data.items)) return null
|
|
58
|
-
|
|
65
|
+
// 分页字段走 paging.js 归一:老宿主没有 total/nextOffset 时按"一次给全"降级,
|
|
66
|
+
// 新宿主则给出真实的下一页游标。
|
|
67
|
+
const w = normalizeWindow(data, 0)
|
|
68
|
+
return { scope: data.scope ?? 'project', count: w.total, total: w.total, offset: w.offset, nextOffset: w.nextOffset, items: w.items }
|
|
59
69
|
} catch {
|
|
60
70
|
return null
|
|
61
71
|
}
|
|
@@ -86,14 +96,23 @@ export function MemoryView({
|
|
|
86
96
|
showHints: boolean
|
|
87
97
|
t: any
|
|
88
98
|
}) {
|
|
89
|
-
const [
|
|
99
|
+
const [rows, setRows] = useState<InsightRow[]>([])
|
|
100
|
+
const [total, setTotal] = useState(0)
|
|
101
|
+
const [nextOffset, setNextOffset] = useState<number | null>(null)
|
|
90
102
|
const [error, setError] = useState<string | null>(null)
|
|
91
103
|
const [loading, setLoading] = useState(false)
|
|
104
|
+
const [loadingMore, setLoadingMore] = useState(false)
|
|
105
|
+
// 加载指示延迟显示:本地数据几十毫秒就回来,立刻显示只会"闪一下"(见 paging.js)。
|
|
106
|
+
const [showSpinner, setShowSpinner] = useState(false)
|
|
92
107
|
const [busy, setBusy] = useState(false)
|
|
93
108
|
const [showNew, setShowNew] = useState(false)
|
|
94
109
|
const [editingId, setEditingId] = useState<string | null>(null)
|
|
95
110
|
const [form, setForm] = useState({ title: '', kind: 'lesson', body: '', steps: '', trigger: '' })
|
|
96
111
|
const [savedTip, setSavedTip] = useState<string | null>(null)
|
|
112
|
+
// 滚动容器(IntersectionObserver 的 root):哨兵是否"可见"必须相对这个容器判定,
|
|
113
|
+
// 用视口当 root 会让面板自身的 480px 上限失效。
|
|
114
|
+
const scrollRef = useRef<HTMLDivElement | null>(null)
|
|
115
|
+
const sentinelRef = useRef<HTMLDivElement | null>(null)
|
|
97
116
|
|
|
98
117
|
const runLine = useCallback(
|
|
99
118
|
async (line: string): Promise<{ ok: boolean; text: string }> => {
|
|
@@ -116,46 +135,113 @@ export function MemoryView({
|
|
|
116
135
|
[ctx, sessionId],
|
|
117
136
|
)
|
|
118
137
|
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
const
|
|
138
|
+
// 并发控制走 paging.js 的 createRequestGate():过期响应丢弃、新请求照发。
|
|
139
|
+
// (早期用"有在途请求就 return"的布尔守卫时,切 scope 的新请求会被直接丢掉,
|
|
140
|
+
// 新列表于是一直空着——见 createRequestGate 的注释。)
|
|
141
|
+
const gateRef = useRef<ReturnType<typeof createRequestGate> | null>(null)
|
|
142
|
+
if (gateRef.current === null) gateRef.current = createRequestGate()
|
|
143
|
+
// 作用域变了之后到达的过期响应必须丢弃,否则会把上一个 scope 的条目追加进来。
|
|
144
|
+
const scopeRef = useRef(scope)
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
scopeRef.current = scope
|
|
147
|
+
}, [scope])
|
|
123
148
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
const res = await runLine(`/tasks insight list ${scope}`)
|
|
142
|
-
if (!res.ok) {
|
|
143
|
-
setError(res.text)
|
|
144
|
-
setPayload(null)
|
|
149
|
+
/**
|
|
150
|
+
* 取一页并落到状态里。
|
|
151
|
+
* @param offset 起始下标。
|
|
152
|
+
* @param limit 本页条数。
|
|
153
|
+
* @param append true=追加到已渲染列表;false=替换成这一页(首屏或前缀重放)。
|
|
154
|
+
*/
|
|
155
|
+
const load = useCallback(
|
|
156
|
+
async (offset: number, limit: number, append: boolean): Promise<void> => {
|
|
157
|
+
// 没有活跃会话时不发命令、也不报错:面板顶部已经显示「还没有会话」,
|
|
158
|
+
// 再报一次"同步失败"会让人以为记忆库坏了(0.1.7 上会话快照结构变化时就是这个表象)。
|
|
159
|
+
if (!sessionId) {
|
|
160
|
+
setRows([])
|
|
161
|
+
setTotal(0)
|
|
162
|
+
setNextOffset(null)
|
|
163
|
+
setError(null)
|
|
164
|
+
setLoading(false)
|
|
145
165
|
return
|
|
146
166
|
}
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
167
|
+
const requestedScope = scopeRef.current
|
|
168
|
+
const key = `${requestedScope}|${offset}|${limit}|${append}`
|
|
169
|
+
const ticket = gateRef.current!.begin(key)
|
|
170
|
+
if (ticket === false) return
|
|
171
|
+
if (!append) setLoading(true)
|
|
172
|
+
setLoadingMore(append)
|
|
173
|
+
setError(null)
|
|
174
|
+
const spinner = window.setTimeout(() => setShowSpinner(true), SPINNER_DELAY_MS)
|
|
175
|
+
try {
|
|
176
|
+
const res = await runLine(insightListLine(requestedScope, offset, limit))
|
|
177
|
+
// 已被更新的请求取代,或切了作用域:这份响应属于上一个列表,整份丢弃。
|
|
178
|
+
if (gateRef.current!.isStale(ticket, requestedScope, scopeRef.current)) return
|
|
179
|
+
if (!res.ok) {
|
|
180
|
+
setError(res.text)
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
const parsed = parseInsightPayloadText(res.text)
|
|
184
|
+
if (!parsed) {
|
|
185
|
+
setError('unparsable payload')
|
|
186
|
+
return
|
|
187
|
+
}
|
|
188
|
+
setRows((prev) => (append ? appendUnique(prev, parsed.items) : parsed.items))
|
|
189
|
+
setTotal(parsed.total)
|
|
190
|
+
setNextOffset(parsed.nextOffset)
|
|
191
|
+
} finally {
|
|
192
|
+
window.clearTimeout(spinner)
|
|
193
|
+
// 加载标志只由最后一次请求回落:过期请求提前清掉会让界面"看起来加载完了"。
|
|
194
|
+
if (gateRef.current!.end(key, ticket)) {
|
|
195
|
+
setShowSpinner(false)
|
|
196
|
+
setLoading(false)
|
|
197
|
+
setLoadingMore(false)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
[runLine, sessionId],
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
// load 的身份随 runLine 变(ctx/sessionId 变化);用 ref 持有它,让下面的 effect
|
|
205
|
+
// 只依赖 scope/sessionId 这两个**原始值**——把函数身份拉进依赖会重跑 effect 并
|
|
206
|
+
// 无限刷 /insight 命令(旧实现用 800ms 频控绕开,这里从根上避免)。
|
|
207
|
+
const loadRef = useRef(load)
|
|
208
|
+
useEffect(() => {
|
|
209
|
+
loadRef.current = load
|
|
210
|
+
}, [load])
|
|
155
211
|
|
|
156
212
|
useEffect(() => {
|
|
157
|
-
|
|
158
|
-
|
|
213
|
+
// 换 scope / 换会话 = 换了一个列表:清空并从头取第一页。
|
|
214
|
+
setRows([])
|
|
215
|
+
setTotal(0)
|
|
216
|
+
setNextOffset(null)
|
|
217
|
+
setError(null)
|
|
218
|
+
if (!sessionId) return
|
|
219
|
+
void loadRef.current(0, PAGE_SIZE, false)
|
|
220
|
+
}, [scope, sessionId])
|
|
221
|
+
|
|
222
|
+
// 滚到底之前就取下一页(rootMargin 600px)。哨兵消失(nextOffset=null)即停止。
|
|
223
|
+
useEffect(() => {
|
|
224
|
+
if (nextOffset === null || typeof IntersectionObserver === 'undefined') return
|
|
225
|
+
const sentinel = sentinelRef.current
|
|
226
|
+
if (!sentinel) return
|
|
227
|
+
const observer = new IntersectionObserver(
|
|
228
|
+
(entries) => {
|
|
229
|
+
if (entries.some((e) => e.isIntersecting)) void loadRef.current(nextOffset, PAGE_SIZE, true)
|
|
230
|
+
},
|
|
231
|
+
// root 用面板自己的滚动容器;取不到时退回视口(仍比"不加载"好)。
|
|
232
|
+
{ root: scrollRef.current ?? null, rootMargin: PREFETCH_MARGIN },
|
|
233
|
+
)
|
|
234
|
+
observer.observe(sentinel)
|
|
235
|
+
return () => observer.disconnect()
|
|
236
|
+
}, [nextOffset, rows.length])
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 重载**已加载的整个前缀**(不是重置成第一页):动作之后用户的位置不该跳回顶部。
|
|
240
|
+
*/
|
|
241
|
+
const reloadWindow = useCallback(async (): Promise<void> => {
|
|
242
|
+
const { offset, limit } = reloadWindowRequest(rows.length)
|
|
243
|
+
await loadRef.current(offset, limit, false)
|
|
244
|
+
}, [rows.length])
|
|
159
245
|
|
|
160
246
|
const act = async (action: string, id: string): Promise<void> => {
|
|
161
247
|
if (busy) return
|
|
@@ -165,7 +251,7 @@ export function MemoryView({
|
|
|
165
251
|
const line = `/tasks insight ${action} ${scope} ${id}${action === 'demote' && scope === 'project' && boundTaskId ? ` ${boundTaskId}` : ''}`
|
|
166
252
|
const res = await runLine(line)
|
|
167
253
|
if (!res.ok) setError(res.text)
|
|
168
|
-
else
|
|
254
|
+
else await reloadWindow()
|
|
169
255
|
} finally {
|
|
170
256
|
setBusy(false)
|
|
171
257
|
}
|
|
@@ -249,28 +335,29 @@ export function MemoryView({
|
|
|
249
335
|
setSavedTip(t('mem.saved'))
|
|
250
336
|
closeForm()
|
|
251
337
|
window.setTimeout(() => setSavedTip(null), 2500)
|
|
252
|
-
|
|
338
|
+
await reloadWindow()
|
|
253
339
|
}
|
|
254
340
|
} finally {
|
|
255
341
|
setBusy(false)
|
|
256
342
|
}
|
|
257
343
|
}
|
|
258
344
|
|
|
259
|
-
const items =
|
|
345
|
+
const items = rows
|
|
346
|
+
const progress = progressCounts(items.length, total)
|
|
260
347
|
|
|
261
348
|
return (
|
|
262
|
-
<div className={css.memoryPane}>
|
|
349
|
+
<div className={css.memoryPane} ref={scrollRef}>
|
|
263
350
|
<div className={css.memToolbar}>
|
|
264
351
|
<span className={css.memCount}>
|
|
265
352
|
{scope === 'task' ? t('mem.scope.task') : scope === 'global' ? t('mem.scope.global') : t('mem.scope.project')}
|
|
266
|
-
{
|
|
353
|
+
{total > 0 ? ` · ${total}` : ''}
|
|
267
354
|
</span>
|
|
268
355
|
<span className={css.memToolbarActs}>
|
|
269
356
|
{savedTip && <span className={css.badge}>{savedTip}</span>}
|
|
270
357
|
{scope !== 'task' && !showNew && (
|
|
271
358
|
<Button variant="outline" size="sm" disabled={busy} onClick={() => { setShowNew(true); setEditingId(null); setError(null) }}>{t('mem.new')}</Button>
|
|
272
359
|
)}
|
|
273
|
-
<Button variant="outline" size="sm" onClick={() => void
|
|
360
|
+
<Button variant="outline" size="sm" onClick={() => void reloadWindow()} disabled={loading} title={showHints ? t('panel.refresh') : undefined} aria-label={t('panel.refresh')}>
|
|
274
361
|
↻
|
|
275
362
|
</Button>
|
|
276
363
|
</span>
|
|
@@ -307,7 +394,14 @@ export function MemoryView({
|
|
|
307
394
|
</div>
|
|
308
395
|
</div>
|
|
309
396
|
)}
|
|
310
|
-
{error &&
|
|
397
|
+
{error && (
|
|
398
|
+
<div className={css.notice}>
|
|
399
|
+
{t('mem.error')}: {error}
|
|
400
|
+
{/* 失败必须给出口:增量加载里"卡住不动"和"已经加载完"外观一样,没有重试按钮
|
|
401
|
+
用户只能靠开关面板碰运气。 */}
|
|
402
|
+
<Button variant="outline" size="sm" disabled={loading || loadingMore} onClick={() => void reloadWindow()}>{t('mem.retry')}</Button>
|
|
403
|
+
</div>
|
|
404
|
+
)}
|
|
311
405
|
{loading && !items.length && <div className={css.muted}>{t('mem.loading')}</div>}
|
|
312
406
|
{!loading && !error && items.length === 0 && <div className={css.muted}>{t('mem.empty')}</div>}
|
|
313
407
|
<div className={css.memList}>
|
|
@@ -355,6 +449,25 @@ export function MemoryView({
|
|
|
355
449
|
</div>
|
|
356
450
|
)
|
|
357
451
|
})}
|
|
452
|
+
{/* 增量加载哨兵 + 页脚。预取由上面的 IntersectionObserver 驱动(滚到底前 600px),
|
|
453
|
+
这里只负责:① 放哨兵;② 把"已显示 N / 总数"讲清楚,让无限滚动可预期;
|
|
454
|
+
③ 环境没有 IntersectionObserver 时退化成手动按钮,绝不静默停住。 */}
|
|
455
|
+
{items.length > 0 && (
|
|
456
|
+
<div className={css.memFooter}>
|
|
457
|
+
{nextOffset !== null && <div ref={sentinelRef} className={css.memSentinel} aria-hidden="true" />}
|
|
458
|
+
{nextOffset !== null && typeof IntersectionObserver === 'undefined' && (
|
|
459
|
+
<Button variant="outline" size="sm" disabled={loadingMore} onClick={() => void loadRef.current(nextOffset, PAGE_SIZE, true)}>
|
|
460
|
+
{t('mem.loadMore')}
|
|
461
|
+
</Button>
|
|
462
|
+
)}
|
|
463
|
+
<span className={css.muted}>
|
|
464
|
+
{progress.loaded < progress.total
|
|
465
|
+
? t('mem.progress', { loaded: progress.loaded, total: progress.total })
|
|
466
|
+
: t('mem.allLoaded', { total: progress.total })}
|
|
467
|
+
{showSpinner && loadingMore ? ` · ${t('mem.loadingMore')}` : ''}
|
|
468
|
+
</span>
|
|
469
|
+
</div>
|
|
470
|
+
)}
|
|
358
471
|
</div>
|
|
359
472
|
</div>
|
|
360
473
|
)
|
|
@@ -163,6 +163,10 @@
|
|
|
163
163
|
background: var(--dsw-alias-bg-base, transparent);
|
|
164
164
|
overflow: hidden;
|
|
165
165
|
transition: border-color 0.15s ease;
|
|
166
|
+
/* 与 .memRow 同款的窗口化:任务多/卡片展开后离屏卡片不再参与 layout/paint。
|
|
167
|
+
用 `auto` 记住实测高度,折叠/展开切换不会算错。 */
|
|
168
|
+
content-visibility: auto;
|
|
169
|
+
contain-intrinsic-size: auto 140px;
|
|
166
170
|
}
|
|
167
171
|
.cardBound {
|
|
168
172
|
border-color: var(--dsw-alias-border-l3, rgba(127, 127, 127, 0.35));
|
|
@@ -686,6 +690,20 @@
|
|
|
686
690
|
flex-direction: column;
|
|
687
691
|
gap: 6px;
|
|
688
692
|
}
|
|
693
|
+
/* 增量加载哨兵:0 高、不可见,只作为 IntersectionObserver 的观察目标。 */
|
|
694
|
+
.memSentinel {
|
|
695
|
+
height: 1px;
|
|
696
|
+
flex: none;
|
|
697
|
+
}
|
|
698
|
+
/* 页脚状态行:"已显示 N / 总数" + 加载中/已全部加载。让无限滚动可预期,也便于排查。 */
|
|
699
|
+
.memFooter {
|
|
700
|
+
display: flex;
|
|
701
|
+
flex-direction: column;
|
|
702
|
+
align-items: center;
|
|
703
|
+
gap: 4px;
|
|
704
|
+
padding: 6px 0 2px;
|
|
705
|
+
font-size: 12px;
|
|
706
|
+
}
|
|
689
707
|
.memRow {
|
|
690
708
|
border: 1px solid var(--dsw-border-weak, rgba(128, 128, 128, 0.25));
|
|
691
709
|
border-radius: 6px;
|
|
@@ -694,6 +712,11 @@
|
|
|
694
712
|
flex-direction: column;
|
|
695
713
|
gap: 4px;
|
|
696
714
|
background: color-mix(in srgb, var(--dsw-bg-secondary, #f5f5f5) 45%, transparent);
|
|
715
|
+
/* 窗口化:离屏行跳过 layout/paint,但 DOM 仍在 —— 保留 find-in-page 与无障碍,
|
|
716
|
+
且不必手写虚拟列表(可变行高的 JS 虚拟化是坑)。`auto` 让浏览器记住每行实测高度,
|
|
717
|
+
所以可变行高也准。不支持的宿主自动退化为普通渲染,零风险。 */
|
|
718
|
+
content-visibility: auto;
|
|
719
|
+
contain-intrinsic-size: auto 76px;
|
|
697
720
|
}
|
|
698
721
|
.memRowArchived {
|
|
699
722
|
opacity: 0.55;
|
package/src/client/locales.ts
CHANGED
|
@@ -59,6 +59,11 @@ export const zh = {
|
|
|
59
59
|
'mem.draft': '草稿',
|
|
60
60
|
'mem.archived': '已归档',
|
|
61
61
|
'mem.loading': '加载中…',
|
|
62
|
+
'mem.loadingMore': '加载中…',
|
|
63
|
+
'mem.loadMore': '加载更多',
|
|
64
|
+
'mem.progress': '已显示 {loaded} / {total}',
|
|
65
|
+
'mem.allLoaded': '已全部加载({total} 条)',
|
|
66
|
+
'mem.retry': '重试',
|
|
62
67
|
'mem.error': '同步失败',
|
|
63
68
|
'mem.kind.lesson': '坑',
|
|
64
69
|
'mem.kind.decision': '权衡',
|
|
@@ -143,6 +148,11 @@ export const en = {
|
|
|
143
148
|
'mem.draft': 'draft',
|
|
144
149
|
'mem.archived': 'archived',
|
|
145
150
|
'mem.loading': 'Loading…',
|
|
151
|
+
'mem.loadingMore': 'Loading…',
|
|
152
|
+
'mem.loadMore': 'Load more',
|
|
153
|
+
'mem.progress': 'Showing {loaded} of {total}',
|
|
154
|
+
'mem.allLoaded': 'All {total} loaded',
|
|
155
|
+
'mem.retry': 'Retry',
|
|
146
156
|
'mem.error': 'Sync failed',
|
|
147
157
|
'mem.kind.lesson': 'lesson',
|
|
148
158
|
'mem.kind.decision': 'decision',
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 记忆列表的分页窗口工具(纯函数,无 React 依赖)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要单独成模块:客户端测试跑的是一个**手写的 React 替身**
|
|
5
|
+
* (`useState` 的 setter 是 no-op、`useEffect` 由测试手动触发,见 test/client-toolview.test.mjs),
|
|
6
|
+
* 它验证不了"滚到底 → 追加渲染"这类行为。所以把分页的**决策**收敛成纯函数:
|
|
7
|
+
* 拼命令、算下一页窗口、追加去重、判定是否还有更多——边界由
|
|
8
|
+
* test/client-paging.test.mjs 直接钉住,组件只负责把结果接到渲染上。
|
|
9
|
+
*
|
|
10
|
+
* 协议约定(与宿主 src/commands/insight-actions.js 的 `list` 分支一一对应):
|
|
11
|
+
* `/tasks insight list <scope> <offset> <limit>` → 载荷 `{ scope, count, total, offset, items, nextOffset }`
|
|
12
|
+
* `nextOffset === null` 表示到底;载荷缺这些字段(老宿主)时按"一次性返回全部"降级处理。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** 每页条数。宿主侧 INSIGHT_PAGE_MAX=200 是防滥用的封顶,正常永远走这个值。 */
|
|
16
|
+
export const PAGE_SIZE = 30
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 单次请求的条数上限,必须与宿主 `INSIGHT_PAGE_MAX`(src/commands/insight-actions.js)一致。
|
|
20
|
+
*
|
|
21
|
+
* 客户端 bundle 跑在浏览器里,不能 import 服务端模块,所以这里是第二份常量;
|
|
22
|
+
* test/insight-paging.test.mjs 有一条交叉断言把两边钉在一起。
|
|
23
|
+
*
|
|
24
|
+
* 宿主对超限的 `limit` 是**静默**夹到 200、并照常回报 `nextOffset`。动作后的重放是"替换"
|
|
25
|
+
* (MemoryView 用 `parsed.items` 整份 setRows),于是加载了 500 条的用户点一次动作,
|
|
26
|
+
* 列表会被打回 200 条——请求值必须先在这里夹住,不能指望宿主替我们兜。
|
|
27
|
+
*/
|
|
28
|
+
export const HOST_PAGE_MAX = 200
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 预取提前量:哨兵进入视口**下方 600px** 就开始取下一页,
|
|
32
|
+
* 让用户在真正滚到底之前数据已经到位——这是"无感"的来源,不是滚到底再转圈。
|
|
33
|
+
*/
|
|
34
|
+
export const PREFETCH_MARGIN = '600px 0px'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 加载指示的延迟显示阈值(毫秒)。本地数据通常几十毫秒就回来,
|
|
38
|
+
* 立即显示加载行只会造成"闪一下";超过这个时间才认为用户真的在等。
|
|
39
|
+
*/
|
|
40
|
+
export const SPINNER_DELAY_MS = 150
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 拼一页的请求命令行。数据通道是命令文本,协议只在这里拼一次。
|
|
44
|
+
* @param {string} scope - `task` | `project` | `global`。
|
|
45
|
+
* @param {number} offset - 起始下标。
|
|
46
|
+
* @param {number} limit - 本页条数。
|
|
47
|
+
* @returns {string} 可直接交给 remote.commands.execute 的命令行。
|
|
48
|
+
*/
|
|
49
|
+
export function insightListLine(scope, offset, limit) {
|
|
50
|
+
return `/tasks insight list ${scope} ${offset} ${limit}`
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 追加一页并**按 id 去重**。去重是必需的不是防御性的:前缀重放(见
|
|
55
|
+
* {@link reloadWindowRequest})与并发刷新都可能让同一 id 出现两次,重复行会带上
|
|
56
|
+
* 重复的按钮,点错一个就是误删。
|
|
57
|
+
* @param {ReadonlyArray<{id?: string}>} prev - 已渲染条目。
|
|
58
|
+
* @param {ReadonlyArray<{id?: string}>} page - 新到的一页。
|
|
59
|
+
* @returns {Array} 追加后的数组;无新增时**原样返回** prev(避免无意义的重渲染)。
|
|
60
|
+
*/
|
|
61
|
+
export function appendUnique(prev, page) {
|
|
62
|
+
if (!Array.isArray(page) || page.length === 0) return prev
|
|
63
|
+
const seen = new Set(prev.map((row) => row && row.id))
|
|
64
|
+
const fresh = page.filter((row) => row && !seen.has(row.id))
|
|
65
|
+
if (fresh.length === 0) return prev
|
|
66
|
+
return [...prev, ...fresh]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 动作(archive/delete/promote…)之后的重载窗口:刷新**已加载的整个前缀**,
|
|
71
|
+
* 而不是重置成第一页——后者会把正在看第 5 页的用户直接弹回顶部。
|
|
72
|
+
*
|
|
73
|
+
* 用 offset 分页而不是 keyset 游标,正是为了让这条路可行:游标只能"往后翻",
|
|
74
|
+
* 表达不了"把 0..N 重取一遍"。删除/归档导致的窗口收缩下,前缀重取依然不重不漏。
|
|
75
|
+
*
|
|
76
|
+
* **上限**:宿主单页最多 {@link HOST_PAGE_MAX} 条,超过就静默截断。所以已加载数超过上限时
|
|
77
|
+
* 这一窗只重放前 200 条(`limit` 夹住),载荷回报的 `nextOffset` 会让哨兵继续把 200..N
|
|
78
|
+
* 追加回来——宁可多一轮分页,也不能发一个"请求 500、只回 200"的值把列表打回 200 条。
|
|
79
|
+
* @param {number} loadedCount - 当前已渲染条数。
|
|
80
|
+
* @param {number} [pageSize] - 每页条数下界,保证至少取一页。
|
|
81
|
+
* @returns {{offset: number, limit: number}} 重放请求窗口(limit ≤ HOST_PAGE_MAX)。
|
|
82
|
+
*/
|
|
83
|
+
export function reloadWindowRequest(loadedCount, pageSize = PAGE_SIZE) {
|
|
84
|
+
const count = Number.isFinite(loadedCount) && loadedCount > 0 ? Math.floor(loadedCount) : 0
|
|
85
|
+
return { offset: 0, limit: Math.min(HOST_PAGE_MAX, Math.max(pageSize, count)) }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 把宿主载荷归一成窗口状态,兼容三种形态:
|
|
90
|
+
* - 新宿主分页:有 `total` + `nextOffset`;
|
|
91
|
+
* - 新宿主不分页(省略 limit):`nextOffset === null`;
|
|
92
|
+
* - 老宿主/老 bundle:两个字段都没有 → 视为"一次给全,没有下一页"。
|
|
93
|
+
* @param {any} data - 已解析的 JSON 载荷。
|
|
94
|
+
* @param {number} fallbackOffset - 请求时的 offset(载荷没给 offset 时用它)。
|
|
95
|
+
* @returns {{items: any[], total: number, offset: number, nextOffset: number|null}} 归一化窗口。
|
|
96
|
+
*/
|
|
97
|
+
export function normalizeWindow(data, fallbackOffset = 0) {
|
|
98
|
+
const items = Array.isArray(data && data.items) ? data.items : []
|
|
99
|
+
const rawTotal = data && typeof data.total === 'number' ? data.total : data && typeof data.count === 'number' ? data.count : items.length
|
|
100
|
+
const total = Number.isFinite(rawTotal) && rawTotal >= 0 ? rawTotal : items.length
|
|
101
|
+
const offset = data && Number.isFinite(data.offset) ? data.offset : fallbackOffset
|
|
102
|
+
const next = data && data.nextOffset
|
|
103
|
+
const nextOffset = typeof next === 'number' && Number.isFinite(next) && next >= 0 ? next : null
|
|
104
|
+
return { items, total, offset, nextOffset }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 页脚文案用的"已显示 N / 总数"。
|
|
109
|
+
* @param {number} loaded - 已渲染条数。
|
|
110
|
+
* @param {number} total - 服务端总数。
|
|
111
|
+
* @returns {{loaded: number, total: number}} 归一化后的计数(total 不会小于 loaded)。
|
|
112
|
+
*/
|
|
113
|
+
export function progressCounts(loaded, total) {
|
|
114
|
+
const l = Number.isFinite(loaded) && loaded > 0 ? Math.floor(loaded) : 0
|
|
115
|
+
const t = Number.isFinite(total) && total > 0 ? Math.floor(total) : 0
|
|
116
|
+
return { loaded: l, total: Math.max(l, t) }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 请求闸门:协调首屏 / 追加 / 动作后重放这几类**会重叠**的请求。
|
|
121
|
+
*
|
|
122
|
+
* 这里踩过一个坑,规则就是照着它定的:早期实现用"有在途请求就直接 return"的布尔守卫。
|
|
123
|
+
* 于是"上一页还在飞的时候切 scope(或点动作)"时,新请求被**直接丢掉**,而过期响应又
|
|
124
|
+
* 被作用域检查丢掉 → 新列表永远空白,只能手动刷新才出来。正确语义是两件事分开:
|
|
125
|
+
* - **过期响应丢弃**(靠序号 + 作用域判定);
|
|
126
|
+
* - **新请求照发**(只有同一个 key 正在飞时才去重,避免哨兵连续触发重复取同一页)。
|
|
127
|
+
* @returns {{begin: Function, end: Function, isStale: Function}} 闸门。
|
|
128
|
+
*/
|
|
129
|
+
export function createRequestGate() {
|
|
130
|
+
let seq = 0
|
|
131
|
+
let inflightKey = null
|
|
132
|
+
return {
|
|
133
|
+
/**
|
|
134
|
+
* 申请发起一次请求。
|
|
135
|
+
* @param {string} key - 请求身份(scope|offset|limit|append)。
|
|
136
|
+
* @returns {number|false} 该请求的序号;同一 key 已在飞时返回 false(去重)。
|
|
137
|
+
*/
|
|
138
|
+
begin(key) {
|
|
139
|
+
if (inflightKey === key) return false
|
|
140
|
+
inflightKey = key
|
|
141
|
+
seq += 1
|
|
142
|
+
return seq
|
|
143
|
+
},
|
|
144
|
+
/**
|
|
145
|
+
* 请求结束。
|
|
146
|
+
* @param {string} key - 请求身份。
|
|
147
|
+
* @param {number} ticket - begin 返回的序号。
|
|
148
|
+
* @returns {boolean} 它是否仍是最后一次请求(决定加载标志该不该回落)。
|
|
149
|
+
*/
|
|
150
|
+
end(key, ticket) {
|
|
151
|
+
if (inflightKey === key) inflightKey = null
|
|
152
|
+
return ticket === seq
|
|
153
|
+
},
|
|
154
|
+
/**
|
|
155
|
+
* 该响应是否已过期。
|
|
156
|
+
* @param {number} ticket - 发出时的序号。
|
|
157
|
+
* @param {string} requestedScope - 发出时的作用域。
|
|
158
|
+
* @param {string} currentScope - 当前作用域。
|
|
159
|
+
* @returns {boolean} true=必须整份丢弃。
|
|
160
|
+
*/
|
|
161
|
+
isStale(ticket, requestedScope, currentScope) {
|
|
162
|
+
return ticket !== seq || requestedScope !== currentScope
|
|
163
|
+
},
|
|
164
|
+
}
|
|
165
|
+
}
|