dsh-plugin-agent-memory 0.1.6 → 0.1.8

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 CHANGED
@@ -9,7 +9,7 @@ step as an `<agent-memory>` block, so the model sees them without any tool call.
9
9
 
10
10
  The rest of the agent's team assets are available as read-only tools: `am_knowledge_resources`
11
11
  (lists wiki and code-graph resources), `am_knowledge_call` (gets a resource's tool list, then runs
12
- `explore` / `search` / `callers` / `impact` / `read_page` …), `am_skill_search` and `am_skill_read`.
12
+ `explore` / `search` / `callers` / `impact` / `read_page` …), `am_skill_search`, `am_skill_read`, and `am_memory_search` (on-demand L0/L1 lookup).
13
13
 
14
14
  ## Requirements
15
15
 
@@ -47,6 +47,7 @@ Open the plugin's settings page (or the panel's gear button) and set:
47
47
  | `injectMemory` | Recall memories and inject them before each step (default on) |
48
48
  | `writeMemory` | Write each turn's user message back to the expert's memory (default off) |
49
49
  | `topK` | Max memories per recall (1–10, default 4) |
50
+ | `layers` | Memory layers injected each turn (default `L3,L2`, matching the official proxy) |
50
51
  | `timeoutMs` | Per-request timeout (default 6000) |
51
52
  | `kernelUrl` | Kernel API base for wiki / code-graph / skills (default: Hub host on port 8420) |
52
53
  | `tools` | Register the four read-only asset tools (default on) |
package/docs/DETAIL.md CHANGED
@@ -6,8 +6,8 @@
6
6
 
7
7
  1. 在对话框工具行左侧点「选择专家」,展开后点右上角「设置」,直接填 `Hub 地址`、`实例 ID`、`Userkey` 并点「检测连接」;保存后写入本 profile 的 `cordis.patch.yml`(也可以手改该行)。
8
8
  2. 「设置」旁边的「返回列表」回到团队 / agent 列表,先选团队,再选 agent;选中的专家记录在当前会话上。未选择时按钮显示「请选择专家」,且不注入任何记忆——需要默认专家时把 `defaultAgentId` 填上即可。
9
- 3. 每轮对话开始时,Host 半侧会用该 agent 的记忆块做检索(L3 / L2 / L1 / L0 依次尝试),把命中的记忆以 `<agent-memory>` 段落追加到该 step 的消息里,模型因此能看到专家的长期记忆。
10
- 4. 同一份团队资产还能按需取用:插件注册了 4 个只读工具 —— `am_knowledge_resources`(列出 wiki / code-graph 资源)、`am_knowledge_call`(先取该资源的工具清单,再执行 explore / search / callers / impact / read_page 等)、`am_skill_search`(检索技能)、`am_skill_read`(读技能正文)。工具默认绑定当前会话选中的专家所在团队。
9
+ 3. 每轮对话开始时,Host 半侧按配置的 `layers` 检索该 agent 的记忆块(默认 `L3,L2`,与官方 proxy 一致——官方每轮只注入 L3 persona 与 L2 场景索引,L0/L1 改由工具按需取,以保护 prompt cache),把命中的记忆以 `<agent-memory>` 段落追加到该 step 的消息里。
10
+ 4. 同一份团队资产还能按需取用:插件注册了 4 个只读工具 —— `am_knowledge_resources`(列出 wiki / code-graph 资源)、`am_knowledge_call`(先取该资源的工具清单,再执行 explore / search / callers / impact / read_page 等)、`am_skill_search`(检索技能)、`am_skill_read`(读技能正文)、`am_memory_search`(按需查 L1/L0 等分层记忆)。工具默认绑定当前会话选中的专家所在团队。
11
11
 
12
12
  ## 配置项
13
13
 
@@ -20,6 +20,7 @@
20
20
  | `injectMemory` | 关闭后只保留选择器,不注入记忆 |
21
21
  | `writeMemory` | 打开后每轮结束时把该轮用户消息写回专家记忆块(插件默认关闭,本实例安装配置里已开启) |
22
22
  | `topK` | 每层最多取回的记忆条数(1–10) |
23
+ | `layers` | 每轮预注入的记忆层,逗号分隔,默认 `L3,L2`;需要更细的原文可加 `L1,L0`,或用 `am_memory_search` 按需查 |
23
24
  | `timeoutMs` | 单次 Hub 请求超时 |
24
25
  | `kernelUrl` | 内核 API 地址(知识库 / code-graph / 技能),默认按 Hub 主机名 + `8420` 推断;显式填写更稳妥 |
25
26
  | `tools` | 是否注册上述 4 个资产工具(默认开) |
package/index.js CHANGED
@@ -36,6 +36,10 @@ export const Config = z.object({
36
36
  injectMemory: z.boolean().default(true).description('每轮对话前召回该专家的记忆并注入上下文'),
37
37
  writeMemory: z.boolean().default(false).description('每轮对话结束后把用户消息写回记忆库'),
38
38
  topK: z.number().min(1).max(10).step(1).default(4).description('每次召回的记忆条数'),
39
+ layers: z
40
+ .string()
41
+ .default('L3,L2')
42
+ .description('每轮预注入的记忆层,逗号分隔按顺序查;默认 L3,L2(与官方一致)。L0/L1 更适合用 am_memory_search 按需取'),
39
43
  timeoutMs: z.number().min(1000).step(500).default(6000).description('单次 Hub 请求超时(毫秒)'),
40
44
  refreshMs: z
41
45
  .number()
@@ -92,6 +96,19 @@ function writeBackText(text) {
92
96
  .trim();
93
97
  }
94
98
 
99
+ /**
100
+ * The per-turn injection layers. Official proxy injects L3 (persona) + L2 (scene index)
101
+ * only; L0/L1 stay on demand through tools, which protects the prompt cache.
102
+ */
103
+ function parseLayers(value) {
104
+ const allowed = new Set(LAYERS);
105
+ const picked = String(value ?? '')
106
+ .split(/[,\s]+/)
107
+ .map((item) => item.trim().toUpperCase())
108
+ .filter((item) => allowed.has(item));
109
+ return picked.length > 0 ? picked : ['L3', 'L2'];
110
+ }
111
+
95
112
  function normalizeConfig(config) {
96
113
  const value = config ?? {};
97
114
  return {
@@ -102,6 +119,7 @@ function normalizeConfig(config) {
102
119
  injectMemory: value.injectMemory !== false,
103
120
  writeMemory: value.writeMemory === true,
104
121
  topK: Number.isFinite(value.topK) ? Math.max(1, Math.min(10, value.topK)) : 4,
122
+ layers: parseLayers(value.layers),
105
123
  timeoutMs: Number.isFinite(value.timeoutMs) ? Math.max(1000, value.timeoutMs) : 6000,
106
124
  refreshMs: Number.isFinite(value.refreshMs) ? Math.max(3000, Math.min(600000, value.refreshMs)) : DEFAULT_REFRESH_MS,
107
125
  kernelUrl: typeof value.kernelUrl === 'string' ? value.kernelUrl.trim().replace(/\/+$/, '') : '',
@@ -242,18 +260,20 @@ export function apply(ctx, config) {
242
260
  return match.id;
243
261
  }
244
262
 
245
- async function recall(selection, query) {
263
+ async function recall(selection, query, overrides = {}) {
246
264
  const block = await blockId(selection.teamId, selection.agentId);
265
+ const layers = overrides.layers ?? settings.layers;
266
+ const limit = overrides.limit ?? settings.topK;
247
267
  const collected = [];
248
268
  const seen = new Set();
249
- for (const layer of LAYERS) {
269
+ for (const layer of layers) {
250
270
  let data;
251
271
  try {
252
272
  data = await chatMemory('search', {
253
273
  block_id: block,
254
274
  layer,
255
275
  query,
256
- limit: settings.topK,
276
+ limit,
257
277
  });
258
278
  } catch {
259
279
  continue;
@@ -264,9 +284,9 @@ export function apply(ctx, config) {
264
284
  if (body === '' || seen.has(body)) continue;
265
285
  seen.add(body);
266
286
  collected.push({ layer, body });
267
- if (collected.length >= settings.topK * 2) break;
287
+ if (collected.length >= limit * 2) break;
268
288
  }
269
- if (collected.length >= settings.topK) break;
289
+ if (collected.length >= limit) break;
270
290
  }
271
291
  return collected;
272
292
  }
@@ -326,6 +346,20 @@ export function apply(ctx, config) {
326
346
  return payload.data ?? {};
327
347
  }
328
348
 
349
+ async function knowledgeResourcesAll() {
350
+ const known = await teams();
351
+ const merged = [];
352
+ for (const team of known) {
353
+ try {
354
+ const items = await knowledgeResources(team.team_id);
355
+ for (const item of items) merged.push(item);
356
+ } catch {
357
+ /* one unreachable team must not hide the rest */
358
+ }
359
+ }
360
+ return merged;
361
+ }
362
+
329
363
  async function knowledgeResources(teamId) {
330
364
  const key = `knowledge:${teamId}`;
331
365
  const cached = state.assets.get(key);
@@ -364,7 +398,8 @@ export function apply(ctx, config) {
364
398
  const known = await teams();
365
399
  const first = known[0];
366
400
  if (first === undefined) throw new Error('当前 userkey 下没有可见团队');
367
- return { teamId: first.team_id, teamName: first.name, agentId: settings.defaultAgentId, agentName: '' };
401
+ // No expert picked yet: team-level assets still work, memory does not (it needs an agent).
402
+ return { teamId: first.team_id, teamName: first.name, agentId: '', agentName: '', unscoped: true };
368
403
  }
369
404
 
370
405
  const TOOL_TEXT_LIMIT = 12000;
@@ -541,7 +576,7 @@ export function apply(ctx, config) {
541
576
  isConcurrencySafe: () => true,
542
577
  async execute(args, exec) {
543
578
  const scope = await toolScope(exec);
544
- const items = await knowledgeResources(scope.teamId);
579
+ const items = await knowledgeResourcesAll();
545
580
  return knowledgeListText(scope, items, args ?? {});
546
581
  },
547
582
  }),
@@ -567,7 +602,7 @@ export function apply(ctx, config) {
567
602
  const scope = await toolScope(exec);
568
603
  const knowledgeId = String(args.knowledge_id ?? '').trim();
569
604
  if (knowledgeId === '') throw new Error('knowledge_id 不能为空');
570
- const items = await knowledgeResources(scope.teamId);
605
+ const items = await knowledgeResourcesAll();
571
606
  const resource = items.find((item) => item.knowledge_id === knowledgeId);
572
607
  if (resource === undefined) {
573
608
  throw new Error(`未知的 knowledge_id:${knowledgeId}(先用 am_knowledge_resources 查看)`);
@@ -589,6 +624,40 @@ export function apply(ctx, config) {
589
624
  },
590
625
  }),
591
626
  );
627
+ disposers.push(
628
+ tools.register({
629
+ name: 'am_memory_search',
630
+ description:
631
+ '按需检索当前专家的分层记忆。默认查 L1(原子记忆)与 L0(原始对话);需要稳定结论时也可传 L2/L3。每轮预注入会使用的层由配置 layers 决定(默认 L3,L2),这里用于补充更细的细节。',
632
+ parameters: {
633
+ type: 'object',
634
+ properties: {
635
+ query: { type: 'string', description: '检索问题或关键词。' },
636
+ layers: { type: 'string', description: '层,逗号分隔,默认 L1,L0;可用 L3,L2,L1,L0。' },
637
+ limit: { type: 'number', description: '每层返回条数,1-10,默认 5。' },
638
+ },
639
+ required: ['query'],
640
+ additionalProperties: false,
641
+ },
642
+ output: textOutput,
643
+ isConcurrencySafe: () => true,
644
+ async execute(args, exec) {
645
+ const query = String(args.query ?? '').trim();
646
+ if (query === '') throw new Error('query 不能为空');
647
+ const layers = parseLayers(
648
+ typeof args.layers === 'string' && args.layers.trim() !== '' ? args.layers : 'L1,L0',
649
+ );
650
+ const limit = Number.isFinite(args.limit) ? Math.max(1, Math.min(10, args.limit)) : 5;
651
+ const scope = await toolScope(exec);
652
+ if (!scope.agentId) throw new Error('当前会话还没有选专家:请先在「请选择专家」里选一个,再检索它的记忆');
653
+ const hits = (await recall(scope, query, { layers, limit })).slice(0, limit);
654
+ if (hits.length === 0) {
655
+ return `在「${scope.teamName}」/ ${scope.agentId || '默认 agent'} 的 ${layers.join('/')} 层没有命中。`;
656
+ }
657
+ return hits.map((hit, index) => `${index + 1}. (${hit.layer}) ${hit.body}`).join('\n');
658
+ },
659
+ }),
660
+ );
592
661
  disposers.push(
593
662
  tools.register({
594
663
  name: 'am_skill_search',
@@ -772,6 +841,7 @@ export function apply(ctx, config) {
772
841
  injectMemory: settings.injectMemory,
773
842
  writeMemory: settings.writeMemory,
774
843
  topK: settings.topK,
844
+ layers: settings.layers.join(','),
775
845
  refreshMs: settings.refreshMs,
776
846
  kernelUrl: kernelBase(),
777
847
  tools: settings.tools,
@@ -870,6 +940,7 @@ export function apply(ctx, config) {
870
940
  if (typeof body.injectMemory === 'boolean') patch.injectMemory = body.injectMemory;
871
941
  if (typeof body.writeMemory === 'boolean') patch.writeMemory = body.writeMemory;
872
942
  if (Number.isFinite(body.topK)) patch.topK = Math.max(1, Math.min(10, body.topK));
943
+ if (typeof body.layers === 'string') patch.layers = parseLayers(body.layers).join(',');
873
944
  if (Number.isFinite(body.timeoutMs)) patch.timeoutMs = Math.max(1000, body.timeoutMs);
874
945
  if (Number.isFinite(body.refreshMs)) {
875
946
  patch.refreshMs = Math.max(3000, Math.min(600000, body.refreshMs));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-agent-memory",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "type": "module",
5
5
  "description": "Tencent agent-memory integration: pick a team agent as an expert at the composer, recall its memories into each turn, and expose its wiki, code-graph and skill assets as tools.",
6
6
  "license": "MIT",