@yolk_vat-y/dsh-project-memory 0.6.8 → 0.6.10
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 +59 -0
- package/README.md +11 -9
- package/README.zh-CN.md +11 -9
- package/client/client.js +411 -14
- package/client/client.js.map +1 -1
- package/cordis.patch.yml +4 -0
- package/package.json +4 -4
- package/skills/project-memory/SKILL.md +5 -1
- package/skills/project-memory/scripts/pm-digest.mjs +67 -5
- package/src/auto-inject.js +10 -5
- package/src/client/SettingsCard.module.css +185 -0
- package/src/client/client.ts +4 -1
- package/src/client/locales.ts +31 -0
- package/src/client/settings-card.tsx +346 -0
- package/src/commands/insight-actions.js +29 -7
- package/src/commands/tasks.js +1 -1
- package/src/enhancer.js +7 -3
- package/src/index-failures.js +46 -0
- package/src/index.js +36 -11
- package/src/insight-store.js +10 -3
- package/src/lazy.js +25 -5
- package/src/ops.js +13 -0
- package/src/parsers/pdfjs-parser.js +33 -4
- package/src/recall.js +2 -1
- package/src/setup/taskbridge.js +18 -5
- package/src/store.js +190 -78
- package/src/tools/index-doc.js +13 -3
- package/src/tools/index-repo.js +13 -4
- package/src/tools/lesson-tools.js +4 -2
- package/src/tools/query-memory.js +20 -8
- package/src/tools/stats.js +8 -0
- package/src/tools/task-tools.js +4 -0
- package/src/tools/watch-repo.js +14 -8
- package/src/util/config.js +65 -0
- package/src/util/fs.js +121 -17
- package/src/util/search.js +17 -13
- package/src/watch.js +92 -10
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件详情页上的「常用设置」卡片。
|
|
3
|
+
*
|
|
4
|
+
* 座位是 `plugins.bundle.config`(key = 包名),外形照宿主自己的插件卡片长:
|
|
5
|
+
* 一行标题 + 说明 + 折叠箭头,**默认收起**;展开后是一列开关,底部右对齐一个保存。
|
|
6
|
+
*
|
|
7
|
+
* 数据面**完全走宿主设置**,不自建第二份配置:
|
|
8
|
+
* Config 里标了 `.volatile()` 的字段 → 宿主 settings 文档的一个命名空间(ns = 条目 id
|
|
9
|
+
* `project-memory`)→ 客户端 `ctx.configForms.get('project-memory')` 读写 → 宿主校验 +
|
|
10
|
+
* revision fence + volatile 原地更新(不重启插件)。这里既不写 profile 的 yml,也不缓存
|
|
11
|
+
* 值:卡片永远是宿主那份配置的投影,命令行改配置和这里改配置是同一条链路。
|
|
12
|
+
*
|
|
13
|
+
* 只放**用户真的会想调、且调了不会让记忆失真**的旋钮(见 SETTINGS_FIELDS 的注释)。
|
|
14
|
+
* memoryDir / roots / insight.* 这类一改就换数据位置或需要理解上下文的,仍留在 profile。
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
Button, SettingsFormModel, Switch, settingsNumberField,
|
|
18
|
+
} from '@deepseek-ai/dsh-client-ui-primitives'
|
|
19
|
+
import type {
|
|
20
|
+
SettingsFieldSpec, SettingsFieldState, SettingsFormActions, SettingsFormScope, SettingsFormShell,
|
|
21
|
+
} from '@deepseek-ai/dsh-client-ui-primitives'
|
|
22
|
+
import type { SnapshotStore } from '@deepseek-ai/dsh-client-store'
|
|
23
|
+
import { useEffect, type ReactNode } from 'react'
|
|
24
|
+
import { IconChevronDownOutline14 } from './icons.ts'
|
|
25
|
+
import { createTranslate, en, zh, type LocaleDict } from './locales.ts'
|
|
26
|
+
import css from './SettingsCard.module.css'
|
|
27
|
+
|
|
28
|
+
/** bundle 包名:`plugins.bundle.config` 的 key 必须与 profile 里安装的包名一致。 */
|
|
29
|
+
export const SETTINGS_PACKAGE_NAME = '@yolk_vat-y/dsh-project-memory'
|
|
30
|
+
/** 本插件在 profile 的 loader 条目 id(bundle 的 cordis.patch.yml 定死的那个),也是 settings 命名空间。 */
|
|
31
|
+
export const SETTINGS_ENTRY_ID = 'project-memory'
|
|
32
|
+
/** 卡片文案的 locale 命名空间。与面板共用一份词典,但独立注册,互不影响。 */
|
|
33
|
+
export const SETTINGS_NS = 'dsh-project-memory.settings'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 卡片编辑的字段;顺序即渲染顺序。
|
|
37
|
+
*
|
|
38
|
+
* 入选标准只有两条:**用户会主动想调**,且**调坏了不会让记忆失真**。
|
|
39
|
+
* - `autoIndexOnFirstUse`:换项目时最常见的诉求("启动就把这里索引好"),默认关。
|
|
40
|
+
* - `watchInterval`:后台刷新唯一值得调的成本旋钮(频率)。监视本身**不提供关闭**——
|
|
41
|
+
* 关掉之后记忆只会越来越旧,那不是一个选项,是一个坑。
|
|
42
|
+
* - `llmQueryExpansion`:唯一直接花 token 的开关,该由用户自己决定。
|
|
43
|
+
* 刻意**没有**的:lazyIndexing(默认开,关掉只会让断言变旧)、enableTypeScript(内部实现
|
|
44
|
+
* 细节,用户无从判断)、watch(见上)、memoryDir / roots / 扫描上限(改的是数据落在哪)。
|
|
45
|
+
*/
|
|
46
|
+
export const SETTINGS_FIELDS = [
|
|
47
|
+
'autoIndexOnFirstUse',
|
|
48
|
+
'watchInterval',
|
|
49
|
+
'llmQueryExpansion',
|
|
50
|
+
] as const
|
|
51
|
+
export type SettingsFieldName = typeof SETTINGS_FIELDS[number]
|
|
52
|
+
|
|
53
|
+
/** 卡片渲染的一份状态:表单级状态 + 展开状态 + 每个字段的草稿。 */
|
|
54
|
+
export interface SettingsCardState extends SettingsFormShell {
|
|
55
|
+
/** 卡片是否展开;默认收起,与宿主的插件卡片一致。 */
|
|
56
|
+
readonly expanded: boolean
|
|
57
|
+
readonly fields: Readonly<Record<SettingsFieldName, SettingsFieldState>>
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 槽位注册侧注入给组件的动作与快照钩子。 */
|
|
61
|
+
export interface SettingsCardFace extends SettingsFormActions {
|
|
62
|
+
/** 收起 / 展开卡片。 */
|
|
63
|
+
toggleExpanded: () => void
|
|
64
|
+
hooks: {
|
|
65
|
+
/** 渲染器绑成 `useSettingsCard`。 */
|
|
66
|
+
settingsCard: SnapshotStore<SettingsCardState>
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 渲染器交给组件的 props。`t` 由槽位的 `locale` 选项解析。 */
|
|
71
|
+
export interface SettingsCardProps {
|
|
72
|
+
/** 宿主只对 bundle 页要 `page`;`summary` 在本卡片没有对应物。 */
|
|
73
|
+
readonly view?: 'summary' | 'page'
|
|
74
|
+
readonly t?: (key: keyof LocaleDict, params?: Record<string, string | number>) => string
|
|
75
|
+
readonly useSettingsCard?: <S>(selector: (state: SettingsCardState) => S) => S
|
|
76
|
+
readonly toggleExpanded?: () => void
|
|
77
|
+
readonly edit?: (field: string, text: string) => void
|
|
78
|
+
readonly save?: () => void
|
|
79
|
+
readonly discard?: () => void
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 布尔字段:草稿是 `'true'` / `'false'`,其它文本判为非法(挡住保存,而不是吞掉)。 */
|
|
83
|
+
function settingsBooleanField(field: string): SettingsFieldSpec {
|
|
84
|
+
return {
|
|
85
|
+
field,
|
|
86
|
+
format: value => typeof value === 'boolean' ? String(value) : '',
|
|
87
|
+
parse: text => text === 'true'
|
|
88
|
+
? { kind: 'set', value: true }
|
|
89
|
+
: text === 'false'
|
|
90
|
+
? { kind: 'set', value: false }
|
|
91
|
+
: undefined,
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 把 `project-memory` 命名空间桥到卡片的状态上。 */
|
|
96
|
+
export class SettingsCardController {
|
|
97
|
+
private readonly form: SettingsFormModel<Record<string, unknown>>
|
|
98
|
+
private readonly store: SnapshotStore<SettingsCardState>
|
|
99
|
+
private expanded = false
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param scope - 宿主共享配置表单(`ctx.configForms.get('project-memory')`)
|
|
103
|
+
*/
|
|
104
|
+
constructor(scope: SettingsFormScope<Record<string, unknown>>) {
|
|
105
|
+
this.form = new SettingsFormModel(scope, [
|
|
106
|
+
settingsBooleanField('autoIndexOnFirstUse'),
|
|
107
|
+
settingsNumberField('watchInterval'),
|
|
108
|
+
settingsBooleanField('llmQueryExpansion'),
|
|
109
|
+
])
|
|
110
|
+
this.store = this.form.bind(() => this.projection())
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 构造槽位注入面。
|
|
115
|
+
* @returns 快照钩子、展开动作与表单动作
|
|
116
|
+
*/
|
|
117
|
+
inject(): SettingsCardFace {
|
|
118
|
+
return {
|
|
119
|
+
hooks: { settingsCard: this.store },
|
|
120
|
+
toggleExpanded: () => {
|
|
121
|
+
this.expanded = !this.expanded
|
|
122
|
+
// 展开状态不进表单模型;投影自己推一次,别等 scope 变化。
|
|
123
|
+
this.store.set(this.projection())
|
|
124
|
+
},
|
|
125
|
+
...this.form.actions(),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 释放对宿主命名空间的订阅。 */
|
|
130
|
+
dispose(): void {
|
|
131
|
+
this.form.dispose()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private projection(): SettingsCardState {
|
|
135
|
+
const fields = {} as Record<SettingsFieldName, SettingsFieldState>
|
|
136
|
+
for (const field of SETTINGS_FIELDS) fields[field] = this.form.field(field)
|
|
137
|
+
return { ...this.form.shell(), expanded: this.expanded, fields }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 一行:左侧标题 + 说明,右侧控件(与宿主插件卡片的行同构)。 */
|
|
142
|
+
function SettingRow(props: {
|
|
143
|
+
readonly title: string
|
|
144
|
+
readonly hint: string
|
|
145
|
+
readonly children: ReactNode
|
|
146
|
+
}) {
|
|
147
|
+
return (
|
|
148
|
+
<div className={css.row}>
|
|
149
|
+
<div className={css.labelBox}>
|
|
150
|
+
<span className={css.label}>{props.title}</span>
|
|
151
|
+
<span className={css.hint}>{props.hint}</span>
|
|
152
|
+
</div>
|
|
153
|
+
<div className={css.control}>{props.children}</div>
|
|
154
|
+
</div>
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** 宿主导出的"可渲染组件":普通函数组件,或 `forwardRef` / `memo` 返回的对象。 */
|
|
159
|
+
function isComponent(value: unknown): boolean {
|
|
160
|
+
return typeof value === 'function' || (typeof value === 'object' && value !== null)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 渲染器缺钩子时的占位状态:卡片只显示标题,不读任何宿主值。 */
|
|
164
|
+
const UNAVAILABLE_STATE: SettingsCardState = {
|
|
165
|
+
available: false,
|
|
166
|
+
writable: false,
|
|
167
|
+
dirty: false,
|
|
168
|
+
invalid: false,
|
|
169
|
+
saving: false,
|
|
170
|
+
failed: false,
|
|
171
|
+
expanded: false,
|
|
172
|
+
fields: Object.fromEntries(
|
|
173
|
+
SETTINGS_FIELDS.map(field => [field, { text: '', overridden: false, invalid: false }]),
|
|
174
|
+
) as Record<SettingsFieldName, SettingsFieldState>,
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 非 Hook 的稳定回退:hook 数量在两次渲染之间不变,避免条件调用 Hooks。 */
|
|
178
|
+
const fallbackHook = <S,>(selector: (state: SettingsCardState) => S): S => selector(UNAVAILABLE_STATE)
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 渲染本插件的常用设置。
|
|
182
|
+
* @param props - 表单快照与动作(由槽位注入)
|
|
183
|
+
* @returns 折叠卡片;`summary` 视图不渲染
|
|
184
|
+
*/
|
|
185
|
+
export function SettingsCard(props: SettingsCardProps) {
|
|
186
|
+
const t = props.t ?? createTranslate(zh)
|
|
187
|
+
const useSettingsCard = props.useSettingsCard ?? fallbackHook
|
|
188
|
+
// Hook 必须无条件调用:`summary` 只影响后面的返回,不影响 hook 顺序。
|
|
189
|
+
const state = useSettingsCard(snapshot => snapshot)
|
|
190
|
+
const discard = props.discard
|
|
191
|
+
// 离开页面时丢掉未保存的草稿(与宿主设置表单的约定一致;收起卡片不算离开)。
|
|
192
|
+
useEffect(() => () => { discard?.() }, [discard])
|
|
193
|
+
if (props.view === 'summary') return null
|
|
194
|
+
const edit = props.edit
|
|
195
|
+
const save = props.save
|
|
196
|
+
if (edit === undefined || save === undefined) return null
|
|
197
|
+
const disabled = !state.writable || state.saving
|
|
198
|
+
const on = (field: SettingsFieldName): boolean => state.fields[field]?.text === 'true'
|
|
199
|
+
const toggle = (field: SettingsFieldName, next: boolean): void => { edit(field, next ? 'true' : 'false') }
|
|
200
|
+
const expanded = state.expanded === true
|
|
201
|
+
const saveBlocked = !state.dirty || state.invalid || state.saving || !state.writable
|
|
202
|
+
|
|
203
|
+
return (
|
|
204
|
+
<div className={expanded ? `${css.card} ${css.cardOpen}` : css.card} data-project-memory-settings>
|
|
205
|
+
<button
|
|
206
|
+
type="button"
|
|
207
|
+
className={css.header}
|
|
208
|
+
aria-expanded={expanded}
|
|
209
|
+
onClick={props.toggleExpanded}
|
|
210
|
+
>
|
|
211
|
+
<span className={css.headText}>
|
|
212
|
+
<span className={css.name}>{t('settings.title')}</span>
|
|
213
|
+
<span className={css.desc}>{t('settings.subtitle')}</span>
|
|
214
|
+
</span>
|
|
215
|
+
{state.dirty ? <span className={css.unsaved}>{t('settings.unsaved')}</span> : null}
|
|
216
|
+
<span className={expanded ? `${css.chevron} ${css.chevronOpen}` : css.chevron}>
|
|
217
|
+
<IconChevronDownOutline14 />
|
|
218
|
+
</span>
|
|
219
|
+
</button>
|
|
220
|
+
{expanded
|
|
221
|
+
? (
|
|
222
|
+
<div className={css.body}>
|
|
223
|
+
{state.available
|
|
224
|
+
? (
|
|
225
|
+
<>
|
|
226
|
+
{!state.writable ? <p className={css.notice}>{t('settings.form.readOnly')}</p> : null}
|
|
227
|
+
<SettingRow title={t('settings.autoIndexOnFirstUse.title')} hint={t('settings.autoIndexOnFirstUse.hint')}>
|
|
228
|
+
<Switch
|
|
229
|
+
checked={on('autoIndexOnFirstUse')} disabled={disabled}
|
|
230
|
+
label={t('settings.autoIndexOnFirstUse.title')}
|
|
231
|
+
onChange={(next) => { toggle('autoIndexOnFirstUse', next) }}
|
|
232
|
+
/>
|
|
233
|
+
</SettingRow>
|
|
234
|
+
<SettingRow title={t('settings.watchInterval.title')} hint={t('settings.watchInterval.hint')}>
|
|
235
|
+
<input
|
|
236
|
+
className={css.number}
|
|
237
|
+
type="number"
|
|
238
|
+
min={5}
|
|
239
|
+
max={600}
|
|
240
|
+
step={1}
|
|
241
|
+
inputMode="numeric"
|
|
242
|
+
aria-label={t('settings.watchInterval.title')}
|
|
243
|
+
value={state.fields.watchInterval.text}
|
|
244
|
+
disabled={disabled}
|
|
245
|
+
aria-invalid={state.fields.watchInterval.invalid || undefined}
|
|
246
|
+
onChange={(event) => { edit('watchInterval', event.target.value) }}
|
|
247
|
+
/>
|
|
248
|
+
<span className={css.unit}>{t('settings.unit.seconds')}</span>
|
|
249
|
+
</SettingRow>
|
|
250
|
+
<SettingRow title={t('settings.llmQueryExpansion.title')} hint={t('settings.llmQueryExpansion.hint')}>
|
|
251
|
+
<Switch
|
|
252
|
+
checked={on('llmQueryExpansion')} disabled={disabled}
|
|
253
|
+
label={t('settings.llmQueryExpansion.title')}
|
|
254
|
+
onChange={(next) => { toggle('llmQueryExpansion', next) }}
|
|
255
|
+
/>
|
|
256
|
+
</SettingRow>
|
|
257
|
+
<div className={css.actions}>
|
|
258
|
+
{state.failed ? <p className={css.failed} role="status">{t('settings.form.saveFailed')}</p> : null}
|
|
259
|
+
<Button
|
|
260
|
+
variant="primary"
|
|
261
|
+
size="sm"
|
|
262
|
+
disabled={saveBlocked}
|
|
263
|
+
onClick={save}
|
|
264
|
+
>
|
|
265
|
+
{state.saving ? t('settings.form.saving') : t('settings.form.save')}
|
|
266
|
+
</Button>
|
|
267
|
+
</div>
|
|
268
|
+
</>
|
|
269
|
+
)
|
|
270
|
+
: <p className={css.notice}>{t('settings.form.unavailable')}</p>}
|
|
271
|
+
</div>
|
|
272
|
+
)
|
|
273
|
+
: null}
|
|
274
|
+
</div>
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 注册卡片(软依赖,永不抛出)。
|
|
280
|
+
*
|
|
281
|
+
* 两层门控:
|
|
282
|
+
* 1. 嵌套 `inject(['slots','configForms','locale'])` —— 宿主没有设置服务时这里根本不跑;
|
|
283
|
+
* 2. `configForms.whileServed(['project-memory'])` + `slots.inject('plugins.bundle.config')`
|
|
284
|
+
* —— 命名空间没被宿主提供、或该版本没有这个槽时,不留下任何注册痕迹。
|
|
285
|
+
* @param ctx - client 根上下文
|
|
286
|
+
*/
|
|
287
|
+
export function registerSettingsCard(ctx: any): void {
|
|
288
|
+
if (typeof ctx?.inject !== 'function') return
|
|
289
|
+
try {
|
|
290
|
+
ctx.inject(['slots', 'configForms', 'locale'], (scope: any) => {
|
|
291
|
+
try {
|
|
292
|
+
mountSettingsCard(scope)
|
|
293
|
+
} catch (err) {
|
|
294
|
+
console.warn(`[${SETTINGS_NS}] settings card registration failed:`, err)
|
|
295
|
+
}
|
|
296
|
+
})
|
|
297
|
+
} catch (err) {
|
|
298
|
+
console.warn(`[${SETTINGS_NS}] settings card injection failed:`, err)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* 在已就绪的 scope 上挂载卡片。
|
|
304
|
+
* @param scope - 三个服务齐备的嵌套上下文
|
|
305
|
+
*/
|
|
306
|
+
function mountSettingsCard(scope: any): void {
|
|
307
|
+
const slots = scope?.slots
|
|
308
|
+
const configForms = scope?.configForms
|
|
309
|
+
const locale = scope?.locale
|
|
310
|
+
if (typeof slots?.inject !== 'function' || typeof configForms?.get !== 'function'
|
|
311
|
+
|| typeof configForms?.whileServed !== 'function' || typeof locale?.register !== 'function') return
|
|
312
|
+
// 卡片的保存按钮 / 开关来自宿主 ui-primitives:缺任何一个都别注册,避免渲染时才炸。
|
|
313
|
+
//
|
|
314
|
+
// ⚠ 组件不等于函数:宿主用 `forwardRef` 包了 Button(导出是对象 `{ $$typeof, render }`),
|
|
315
|
+
// 用 `typeof x === 'function'` 判定会把它当成"不存在",卡片在真实宿主上整块消失。
|
|
316
|
+
// 这就是当初"测试全绿、界面没东西"的原因 —— 判定必须接受函数与对象两种导出形状。
|
|
317
|
+
if (typeof SettingsFormModel !== 'function' || !isComponent(Switch) || !isComponent(Button)) {
|
|
318
|
+
console.warn(`[${SETTINGS_NS}] host ui-primitives has no settings controls — visual settings disabled`)
|
|
319
|
+
return
|
|
320
|
+
}
|
|
321
|
+
const effect = typeof scope.effect === 'function' ? scope.effect.bind(scope) : null
|
|
322
|
+
const keep = (dispose: unknown): void => {
|
|
323
|
+
if (typeof dispose !== 'function' || effect === null) return
|
|
324
|
+
effect(() => () => {
|
|
325
|
+
try {
|
|
326
|
+
dispose()
|
|
327
|
+
} catch {
|
|
328
|
+
// 宿主已经拆掉这层:忽略
|
|
329
|
+
}
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
try {
|
|
333
|
+
keep(locale.register(SETTINGS_NS, { zh, en }))
|
|
334
|
+
} catch (err) {
|
|
335
|
+
// 同 namespace 已注册(HMR 没等到 disposer):沿用既有词典,卡片照常挂。
|
|
336
|
+
console.warn(`[${SETTINGS_NS}] locale dictionary already registered:`, err)
|
|
337
|
+
}
|
|
338
|
+
const controller = new SettingsCardController(configForms.get(SETTINGS_ENTRY_ID))
|
|
339
|
+
keep(() => controller.dispose())
|
|
340
|
+
keep(configForms.whileServed([SETTINGS_ENTRY_ID], () => slots.inject('plugins.bundle.config', () => slots.register({
|
|
341
|
+
name: 'plugins.bundle.config',
|
|
342
|
+
key: SETTINGS_PACKAGE_NAME,
|
|
343
|
+
locale: SETTINGS_NS,
|
|
344
|
+
inject: () => controller.inject(),
|
|
345
|
+
}, SettingsCard))))
|
|
346
|
+
}
|
|
@@ -4,10 +4,15 @@
|
|
|
4
4
|
// (命令 handler:store.commit + gs.commit)统一负责,便于单测。
|
|
5
5
|
import { ProjectMemoryStore } from '../store.js'
|
|
6
6
|
import { memoryRootFor, normalizeCitationFiles } from '../util/fs.js'
|
|
7
|
-
import { cfgInsight, GlobalStore, normalizeInsight, defaultGlobalFile, saveInsight, recordDeleted, INSIGHT_KINDS } from '../insight-store.js'
|
|
7
|
+
import { cfgInsight, GlobalStore, normalizeInsight, defaultGlobalFile, saveInsight, recordDeleted, carryUsageFields, pruneItems, INSIGHT_KINDS } from '../insight-store.js'
|
|
8
8
|
import { NO_PROJECT_ROOT_NOTE, projectRootFor } from '../setup/taskbridge.js'
|
|
9
9
|
import { fencedJson, invocationContext } from './invocation.js'
|
|
10
10
|
|
|
11
|
+
/** 容量旋钮的防御性取值:cfg 可能只带显式测试字段。 */
|
|
12
|
+
function capOf(value, fallback) {
|
|
13
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback
|
|
14
|
+
}
|
|
15
|
+
|
|
11
16
|
/**
|
|
12
17
|
* 记忆列表单页上限。面板每页只请求 30 条(客户端 PAGE_SIZE),远小于它——
|
|
13
18
|
* 封顶只用于挡住畸形/滥用调用,避免单个响应被抓成超大载荷。
|
|
@@ -163,16 +168,22 @@ export function actInsight({ store, gs, action, scope, id, taskId, cfg }) {
|
|
|
163
168
|
return { ok: true, text: `已${act === 'archive' ? '归档' : '恢复'}` }
|
|
164
169
|
}
|
|
165
170
|
if (act === 'promote') {
|
|
166
|
-
// task → project
|
|
171
|
+
// task → project(手动提升,无视自动阈值)。必须和自动提升一样保留使用痕迹:
|
|
172
|
+
// normalizeInsight 只复制白名单字段,triggerDerived/prev/lastHitAt 要显式带回。
|
|
173
|
+
const now = new Date().toISOString()
|
|
167
174
|
const items = store.insightItems()
|
|
168
|
-
const copy =
|
|
175
|
+
const copy = carryUsageFields(
|
|
176
|
+
normalizeInsight(insight, { scope: 'project', source: insight.source, nowIso: now }),
|
|
177
|
+
insight,
|
|
178
|
+
)
|
|
169
179
|
copy.id = id
|
|
170
180
|
copy.draft = false
|
|
171
181
|
copy.confidence = Math.max(insight.confidence || 0, (cfg && cfg.promoteConfidence) || 0.7)
|
|
172
|
-
copy.movedFrom = { scope: 'task', id, at:
|
|
182
|
+
copy.movedFrom = { scope: 'task', id, at: now }
|
|
173
183
|
copy.createdAt = insight.createdAt || copy.createdAt
|
|
174
184
|
copy.hitCount = insight.hitCount || 0
|
|
175
185
|
items.push(copy)
|
|
186
|
+
pruneItems(items, capOf(cfg?.maxProject, 100), now)
|
|
176
187
|
store.replaceInsightItems(items)
|
|
177
188
|
task.insights = (task.insights || []).filter((i) => i.id !== id)
|
|
178
189
|
store.markTasksDirty()
|
|
@@ -202,13 +213,17 @@ export function actInsight({ store, gs, action, scope, id, taskId, cfg }) {
|
|
|
202
213
|
// project → global
|
|
203
214
|
if (!gs) return { ok: false, text: 'global 存储不可用' }
|
|
204
215
|
const now = new Date().toISOString()
|
|
205
|
-
const copy =
|
|
216
|
+
const copy = carryUsageFields(
|
|
217
|
+
normalizeInsight(ins, { scope: 'global', source: ins.source, nowIso: now }),
|
|
218
|
+
ins,
|
|
219
|
+
)
|
|
206
220
|
copy.id = id
|
|
207
221
|
copy.draft = false
|
|
208
222
|
copy.movedFrom = { scope: 'project', id, at: now }
|
|
209
223
|
copy.createdAt = ins.createdAt || copy.createdAt
|
|
210
224
|
copy.hitCount = ins.hitCount || 0
|
|
211
225
|
gs.items().push(copy)
|
|
226
|
+
pruneItems(gs.items(), capOf(cfg?.maxGlobalProcedures, 200), now)
|
|
212
227
|
gs.markDirty()
|
|
213
228
|
items.splice(idx, 1)
|
|
214
229
|
store.replaceInsightItems(items)
|
|
@@ -219,7 +234,10 @@ export function actInsight({ store, gs, action, scope, id, taskId, cfg }) {
|
|
|
219
234
|
const task = store.getTask(taskId)
|
|
220
235
|
if (!task) return { ok: false, text: '降级到任务需要 taskId' }
|
|
221
236
|
const now = new Date().toISOString()
|
|
222
|
-
const copy =
|
|
237
|
+
const copy = carryUsageFields(
|
|
238
|
+
normalizeInsight(ins, { scope: 'task', source: ins.source, nowIso: now }),
|
|
239
|
+
ins,
|
|
240
|
+
)
|
|
223
241
|
copy.id = `${id}_d${Date.now().toString(36)}`
|
|
224
242
|
copy.movedFrom = { scope: 'project', id, at: now }
|
|
225
243
|
copy.createdAt = ins.createdAt || copy.createdAt
|
|
@@ -255,11 +273,15 @@ export function actInsight({ store, gs, action, scope, id, taskId, cfg }) {
|
|
|
255
273
|
// global → project
|
|
256
274
|
const now = new Date().toISOString()
|
|
257
275
|
const pjItems = store.insightItems()
|
|
258
|
-
const copy =
|
|
276
|
+
const copy = carryUsageFields(
|
|
277
|
+
normalizeInsight(ins, { scope: 'project', source: ins.source, nowIso: now }),
|
|
278
|
+
ins,
|
|
279
|
+
)
|
|
259
280
|
copy.id = `${id}_d${Date.now().toString(36)}`
|
|
260
281
|
copy.movedFrom = { scope: 'global', id, at: now }
|
|
261
282
|
copy.createdAt = ins.createdAt || copy.createdAt
|
|
262
283
|
pjItems.push(copy)
|
|
284
|
+
pruneItems(pjItems, capOf(cfg?.maxProject, 100), now)
|
|
263
285
|
store.replaceInsightItems(pjItems)
|
|
264
286
|
items.splice(idx, 1)
|
|
265
287
|
gs.markDirty()
|
package/src/commands/tasks.js
CHANGED
|
@@ -42,7 +42,7 @@ export function renderTaskSnapshot(config, cwd, sid) {
|
|
|
42
42
|
const bound = boundId ? tasks.find((t) => t.id === boundId) : null
|
|
43
43
|
|
|
44
44
|
if (!active.length) {
|
|
45
|
-
const payload = buildTaskPayload([], boundId, archived)
|
|
45
|
+
const payload = buildTaskPayload([], boundId, archived, root)
|
|
46
46
|
const note = `项目 ${root}\n任务记录: 0 套${archived ? `(归档 ${archived})` : ''}。让模型开始干活并维护 todo 清单后会自动建档。`
|
|
47
47
|
return { kind: 'success', text: fencedJson(note, payload) }
|
|
48
48
|
}
|
package/src/enhancer.js
CHANGED
|
@@ -359,7 +359,7 @@ export function enqueueEnhance(store, relPath, filePath, priority = PRIORITY.BAT
|
|
|
359
359
|
enhanceQueue.splice(existingIdx, 1)
|
|
360
360
|
}
|
|
361
361
|
|
|
362
|
-
const task = { store, relPath, filePath, priority, cacheKey, promise: null, resolve: null }
|
|
362
|
+
const task = { store, relPath, filePath, priority, cacheKey, promise: null, resolve: null, hadRecord: Boolean(store?.files?.[relPath]) }
|
|
363
363
|
task.promise = new Promise((resolve) => { task.resolve = resolve })
|
|
364
364
|
|
|
365
365
|
if (enhanceQueue.length >= enhanceQueueMax) {
|
|
@@ -421,7 +421,7 @@ async function runEnhance(task) {
|
|
|
421
421
|
try {
|
|
422
422
|
const enhanced = deepParseWithTS(task.filePath, content)
|
|
423
423
|
if (enhanced?.length) {
|
|
424
|
-
await task.store.commit(fn => applyEnhancedSymbols(fn, task.relPath, enhanced))
|
|
424
|
+
await task.store.commit(fn => applyEnhancedSymbols(fn, task.relPath, enhanced, { hadRecord: task.hadRecord }))
|
|
425
425
|
}
|
|
426
426
|
} catch (err) {
|
|
427
427
|
console.warn(`[dsh-project-memory] enhance failed for ${task.relPath}: ${err.message}`)
|
|
@@ -452,7 +452,11 @@ function scheduleProcess() {
|
|
|
452
452
|
processNext()
|
|
453
453
|
}
|
|
454
454
|
|
|
455
|
-
function applyEnhancedSymbols(fn, relPath, enhanced) {
|
|
455
|
+
function applyEnhancedSymbols(fn, relPath, enhanced, { hadRecord = false } = {}) {
|
|
456
|
+
// 入队时这个文件还有记录、轮到执行时却没了:说明它已被删除/移出索引。此时再 setEntries
|
|
457
|
+
// 会写下**没有 files 记录**的孤儿符号,unseen 清理看不到它(只遍历 files),查询却会一直命它。
|
|
458
|
+
// `hadRecord` 为 false 的调用(测试 / 直接对未索引文件增强)保持旧行为:从零建条目。
|
|
459
|
+
if (hadRecord && !fn.files[relPath]) return
|
|
456
460
|
// Store entries in fn.entries[relPath], not fn.files[relPath].entries
|
|
457
461
|
const existingEntries = fn.entries[relPath] || []
|
|
458
462
|
const nameOf = e => (e.title || '').replace(/\s*\([^)]*\)\s*$/, '')
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 后台索引失败的进程内账本:只负责"把静默失败报出来",不落盘。
|
|
3
|
+
*
|
|
4
|
+
* watch 的失败过去只打一行日志,懒索引的单文件失败甚至整段吞掉(`catch {}`),于是
|
|
5
|
+
* "某些文件一直没进语料"没有任何可观察面。这里按根记住最近失败的 rel 与原因,
|
|
6
|
+
* 由 index_repo / memory_stats 一并报出。
|
|
7
|
+
*
|
|
8
|
+
* 不持久化是刻意的:watch 每轮都会重试,问题仍在时失败会重新登记;重启后立刻又能看见。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const MAX_PER_ROOT = 50
|
|
12
|
+
const byRoot = new Map()
|
|
13
|
+
|
|
14
|
+
export function recordIndexFailure(root, rel, message) {
|
|
15
|
+
let failures = byRoot.get(root)
|
|
16
|
+
if (!failures) {
|
|
17
|
+
failures = new Map()
|
|
18
|
+
byRoot.set(root, failures)
|
|
19
|
+
}
|
|
20
|
+
failures.set(rel, { message: message || 'index failed', at: Date.now() })
|
|
21
|
+
// 上限只防失控(一次坏掉上千个文件时别把内存吃掉);超出就丢最早登记的。
|
|
22
|
+
if (failures.size > MAX_PER_ROOT) failures.delete(failures.keys().next().value)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function clearIndexFailure(root, rel) {
|
|
26
|
+
const failures = byRoot.get(root)
|
|
27
|
+
if (!failures) return
|
|
28
|
+
failures.delete(rel)
|
|
29
|
+
if (failures.size === 0) byRoot.delete(root)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 按最近失败时间倒序返回 `{ count, items }`;`items` 至多 limit 条。 */
|
|
33
|
+
export function listIndexFailures(root, limit = 5) {
|
|
34
|
+
const failures = byRoot.get(root)
|
|
35
|
+
if (!failures) return { count: 0, items: [] }
|
|
36
|
+
const items = [...failures.entries()]
|
|
37
|
+
.sort((a, b) => b[1].at - a[1].at)
|
|
38
|
+
.slice(0, Math.max(0, limit))
|
|
39
|
+
.map(([rel, value]) => ({ rel, message: value.message }))
|
|
40
|
+
return { count: failures.size, items }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 测试用:账本是模块级状态,用例之间要能清干净。 */
|
|
44
|
+
export function _resetIndexFailuresForTest() {
|
|
45
|
+
byRoot.clear()
|
|
46
|
+
}
|
package/src/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { rememberTool } from './tools/remember.js'
|
|
|
7
7
|
import { forgetTool } from './tools/forget.js'
|
|
8
8
|
import { watchRepoTool } from './tools/watch-repo.js'
|
|
9
9
|
import { statsTool } from './tools/stats.js'
|
|
10
|
-
import { WatchManager } from './watch.js'
|
|
10
|
+
import { WatchManager, applyWatchSettings } from './watch.js'
|
|
11
11
|
import { setupLazyIndexing } from './lazy.js'
|
|
12
12
|
import { initTypeScript } from './enhancer.js'
|
|
13
13
|
import { setupTaskbridge } from './setup/taskbridge.js'
|
|
@@ -18,6 +18,7 @@ import { rememberRoute } from './llm-route.js'
|
|
|
18
18
|
import { workflowCommandDefinition } from './commands/workflow.js'
|
|
19
19
|
import { setupSkill } from './skill.js'
|
|
20
20
|
import { flushDirtyStores } from './util/flush.js'
|
|
21
|
+
import { cfgValue, changedConfigKeys, asVolatile } from './util/config.js'
|
|
21
22
|
|
|
22
23
|
export const name = 'dsh-project-memory'
|
|
23
24
|
export const inject = ['llm', 'tools']
|
|
@@ -32,7 +33,7 @@ export const Config = Schema.object({
|
|
|
32
33
|
maxFileSizeMb: Schema.number().default(10),
|
|
33
34
|
maxOutputChars: Schema.number().default(8000),
|
|
34
35
|
maxPdfPages: Schema.number().default(1000),
|
|
35
|
-
llmQueryExpansion: Schema.boolean().default(false),
|
|
36
|
+
llmQueryExpansion: asVolatile(Schema.boolean().default(false)),
|
|
36
37
|
expansionCount: Schema.number().default(6),
|
|
37
38
|
// 辅助 LLM 调用的路由覆写 —— **索引期零 LLM**,只服务召回期查询扩展与反思期。
|
|
38
39
|
// 未设置时取当前会话 header 的 provider/model,后台路径取最近一次会话路由;都拿不到时
|
|
@@ -41,10 +42,16 @@ export const Config = Schema.object({
|
|
|
41
42
|
provider: Schema.string(),
|
|
42
43
|
model: Schema.string(),
|
|
43
44
|
}).default({}),
|
|
45
|
+
// 以下三个键在 web 的插件页上有可视化设置(`plugins.bundle.config`,见 src/client/settings-card.tsx)。
|
|
46
|
+
// `.volatile()` 是宿主设置页的准入条件:只有 volatile 字段能经 settings 接口写入,且写入后**不重启**
|
|
47
|
+
// 插件、以 ref 形式原地更新。读取一律走 util/config.js 的 cfgValue()(ref 是对象,直接判真会读错)。
|
|
48
|
+
//
|
|
49
|
+
// 其余的键**刻意不标 volatile**:`lazyIndexing` / `watch` / `enableTypeScript` 关掉只会让记忆变旧,
|
|
50
|
+
// 不属于"用户偏好";它们仍可在 profile 里改,但不出现在卡片上(关掉监视等于放任索引过期)。
|
|
44
51
|
lazyIndexing: Schema.boolean().default(true),
|
|
45
|
-
autoIndexOnFirstUse: Schema.boolean().default(false),
|
|
52
|
+
autoIndexOnFirstUse: asVolatile(Schema.boolean().default(false)),
|
|
46
53
|
watch: Schema.boolean().default(true),
|
|
47
|
-
watchInterval: Schema.number().default(30),
|
|
54
|
+
watchInterval: asVolatile(Schema.number().default(30)),
|
|
48
55
|
// 危险根护栏:家目录 / 文件系统根 / 系统目录 / 包管理器前缀(/opt/homebrew …)整体扫描会吃满
|
|
49
56
|
// 内存,默认一律拒绝。`roots.*` 是配置档级例外表;`allowUnsafeRoots` 只放开内置名单,deny/exclude 仍优先。
|
|
50
57
|
allowUnsafeRoots: Schema.boolean().default(false),
|
|
@@ -159,18 +166,34 @@ export function apply(ctx, config) {
|
|
|
159
166
|
}
|
|
160
167
|
|
|
161
168
|
const watchManager = new WatchManager(ctx, config)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
169
|
+
// watchInterval 是 volatile 字段:设置页改完要当场生效(重排定时器),而不是等下次启动。
|
|
170
|
+
// watch 本身不提供界面开关(关掉监视等于放任索引过期),但仍可在 profile 里设 false。
|
|
171
|
+
// 持久化的根清单只恢复一次;先关后开时补一次(否则"打开监视"要等到下次启动才真的开始轮询)。
|
|
172
|
+
let watchRestored = false
|
|
173
|
+
const syncWatch = () => {
|
|
174
|
+
if (cfgValue(config, 'watch') !== false && !watchRestored) {
|
|
175
|
+
watchRestored = true
|
|
176
|
+
watchManager.restorePersisted()
|
|
177
|
+
}
|
|
178
|
+
return applyWatchSettings(watchManager, config)
|
|
168
179
|
}
|
|
180
|
+
ctx.effect(() => {
|
|
181
|
+
syncWatch()
|
|
182
|
+
return () => watchManager.stop()
|
|
183
|
+
})
|
|
169
184
|
|
|
170
185
|
if (config.lazyIndexing) {
|
|
171
186
|
setupLazyIndexing(ctx, config, watchManager)
|
|
172
187
|
}
|
|
173
188
|
|
|
189
|
+
// 设置页保存(宿主 volatile 更新)后:只有 watchInterval 会影响运行中的定时器,
|
|
190
|
+
// 其余 volatile 字段都在各自的读取点当场生效(llmQueryExpansion 每次查询读、autoIndex 只认启动值)。
|
|
191
|
+
// paths 为 `[[]]` 时表示整份 Config 都可能变了 → changedConfigKeys 返回 null,按"全部重读"处理。
|
|
192
|
+
ctx.on('loader/volatile-update', (paths) => {
|
|
193
|
+
const changed = changedConfigKeys(paths)
|
|
194
|
+
if (changed === null || changed.has('watchInterval')) syncWatch()
|
|
195
|
+
})
|
|
196
|
+
|
|
174
197
|
// 卸载/退出前把还脏着的 store 同步写一次(后台重试只是兜底,不能当正确性保证)。
|
|
175
198
|
if (typeof ctx.effect === 'function') {
|
|
176
199
|
ctx.effect(() => () => flushDirtyStores({ timeoutMs: 1500 }))
|
|
@@ -218,7 +241,9 @@ export function apply(ctx, config) {
|
|
|
218
241
|
// 注册 agent/pre-step 监听,向每步请求追加记忆消息(默认开;异常/无会话 cwd → 交回默认决策)。
|
|
219
242
|
installAutoInject(ctx, config)
|
|
220
243
|
|
|
221
|
-
|
|
244
|
+
// 启动时全量索引是**一次性**动作:设置页把它打开只影响下一次启动,不会当场扫全树
|
|
245
|
+
// (卡片上的说明也这么写)。所以这里读的是本次启动时的值,不订阅后续变化。
|
|
246
|
+
if (cfgValue(config, 'autoIndexOnFirstUse')) {
|
|
222
247
|
// effect 回调必须同步并返回 disposer;异步体独立执行,用 cancelled 标志避免卸载后再写日志
|
|
223
248
|
ctx.effect(() => {
|
|
224
249
|
let cancelled = false
|
package/src/insight-store.js
CHANGED
|
@@ -7,7 +7,7 @@ import { readFileSync, renameSync } from 'node:fs'
|
|
|
7
7
|
import path from 'node:path'
|
|
8
8
|
import os from 'node:os'
|
|
9
9
|
import { normalizedTokenOverlap, findBestOverlapMatch, insightMatchText } from './similarity.js'
|
|
10
|
-
import { acquireDirLock, writeJsonAtomic } from './util/fs.js'
|
|
10
|
+
import { acquireDirLock, storeKey, writeJsonAtomic } from './util/fs.js'
|
|
11
11
|
import { debugPersistence } from './util/persist.js'
|
|
12
12
|
import { trackDirty, untrackDirty } from './util/flush.js'
|
|
13
13
|
import { backfillDerivedTriggers } from './readiness.js'
|
|
@@ -481,7 +481,9 @@ function readJson(file, fallback) {
|
|
|
481
481
|
function normalizeDoc(doc) {
|
|
482
482
|
if (doc && typeof doc === 'object' && Array.isArray(doc.items)) {
|
|
483
483
|
if (typeof doc.version !== 'number') doc.version = 1
|
|
484
|
-
|
|
484
|
+
// 手改/旧版本可能把 null 混进 items:调用方大量使用 `it.id` / `it.archived`,一个 null
|
|
485
|
+
// 就能让全局召回或容量裁剪整段抛错。与 project 级 `_loadInsights` 的过滤保持一致。
|
|
486
|
+
doc.items = doc.items.filter((it) => it && typeof it === 'object' && !Array.isArray(it))
|
|
485
487
|
return doc
|
|
486
488
|
}
|
|
487
489
|
return emptyInsightsDoc()
|
|
@@ -507,7 +509,9 @@ export class GlobalStore {
|
|
|
507
509
|
load() {
|
|
508
510
|
// 整份文档读改写:同一路径必须共用实例,否则两个工具/会话交错时后写覆盖先写。
|
|
509
511
|
// 与 ProjectMemoryStore 的 storeCache 同语义,这里只按路径缓存。
|
|
510
|
-
|
|
512
|
+
// Windows/macOS 大小写不敏感:同一个 global.json 的两种写法不能并存两个实例。
|
|
513
|
+
// 项目 store 已有同名归一(storeCacheAlias),这里此前漏了。
|
|
514
|
+
const key = storeKey(path.resolve(this.file))
|
|
511
515
|
const hot = globalStoreCache.get(key)
|
|
512
516
|
if (hot) {
|
|
513
517
|
this.doc = hot.doc
|
|
@@ -528,6 +532,9 @@ export class GlobalStore {
|
|
|
528
532
|
|
|
529
533
|
markDirty() {
|
|
530
534
|
this._dirty = true
|
|
535
|
+
// 退出/卸载时 flushDirtyStores() 只遍历 tracked 集合:不在这里登记,query_memory 这类
|
|
536
|
+
// "只改内存、不立刻 commit"的路径(命中记账、派生 trigger 回填)就会在进程退出时丢掉。
|
|
537
|
+
trackDirty(this)
|
|
531
538
|
}
|
|
532
539
|
|
|
533
540
|
/**
|