@lmzhen/dsh-evolution-learning-graph 0.1.0-rc.4 → 0.1.0-rc.41

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/lib/index.js CHANGED
@@ -1,10 +1,20 @@
1
+ import { SKILL_NAME_RE, SkillLibrary, evolutionIoAdapter } from "@lmzhen/dsh-evolution-core";
1
2
  //#region lib/types/index.js
2
3
  /**
3
- * Learning graph over skills and memory.
4
+ * Learning graph over skills and memory, plus node-level commands
5
+ * (`/evolution graph [detail|edit|delete] <nodeId>`) aligned with the Hermes
6
+ * journey surface: a skill node is its name, a memory node is
7
+ * `memory:<source>:<index>` (source = memory|user, index = position in that
8
+ * file's entries).
4
9
  * @module @lmzhen/dsh-evolution-learning-graph
5
10
  */
6
- /** Build a small deterministic graph from usage records and memory entries. */
7
- function buildLearningGraph(usage, memoryEntries) {
11
+ /**
12
+ * Build a small deterministic graph from usage records and memory entries.
13
+ * Memory node ids follow `memory:<source>:<index>` (source = memory|user,
14
+ * index = position in that file's entries) — the SAME rule the parser
15
+ * `parseGraphNodeId` accepts, so graph detail/edit/delete round-trips.
16
+ */
17
+ function buildLearningGraph(usage, memoryEntries, userEntries = []) {
8
18
  const nodes = [...usage.keys()].map((name) => ({
9
19
  id: name,
10
20
  kind: "skill",
@@ -21,43 +31,153 @@ function buildLearningGraph(usage, memoryEntries) {
21
31
  type: "related"
22
32
  });
23
33
  }
24
- memoryEntries.forEach((entry, index) => {
25
- const id = `memory:${index}`;
26
- nodes.push({
27
- id,
28
- kind: "memory",
29
- label: entry.split("\n")[0]?.slice(0, 80) ?? id
30
- });
31
- const token = entry.toLowerCase();
32
- for (const name of sorted) if (name && token.includes(name.toLowerCase())) edges.push({
33
- from: id,
34
- to: name,
35
- type: "memory_skill"
34
+ const appendMemory = (source, entries) => {
35
+ entries.forEach((entry, index) => {
36
+ const id = `memory:${source}:${index}`;
37
+ nodes.push({
38
+ id,
39
+ kind: "memory",
40
+ label: entry.split("\n")[0]?.slice(0, 80) ?? id
41
+ });
42
+ const token = entry.toLowerCase();
43
+ for (const name of sorted) if (name && token.includes(name.toLowerCase())) edges.push({
44
+ from: id,
45
+ to: name,
46
+ type: "memory_skill"
47
+ });
36
48
  });
37
- });
49
+ };
50
+ appendMemory("memory", memoryEntries);
51
+ appendMemory("user", userEntries);
38
52
  return {
39
53
  nodes,
40
54
  edges
41
55
  };
42
56
  }
57
+ /** Parse a graph node id: skill names pass through; `memory:<source>:<index>` becomes a memory node. */
58
+ function parseGraphNodeId(id) {
59
+ const memory = /^memory:(memory|user):(\d+)$/.exec(id);
60
+ if (memory) return {
61
+ kind: "memory",
62
+ source: memory[1],
63
+ index: Number(memory[2])
64
+ };
65
+ if (SKILL_NAME_RE.test(id)) return {
66
+ kind: "skill",
67
+ name: id
68
+ };
69
+ return null;
70
+ }
71
+ /** Resolve a parsed node to its current content (read-only). */
72
+ async function resolveGraphNode(parsed, repository) {
73
+ if (parsed.kind === "skill") {
74
+ const content = await repository.readSkill(parsed.name);
75
+ return content === null ? {
76
+ ok: false,
77
+ message: `Skill "${parsed.name}" not found.`
78
+ } : {
79
+ ok: true,
80
+ message: content
81
+ };
82
+ }
83
+ const entries = await repository.readMemory(parsed.source);
84
+ const entry = entries[parsed.index];
85
+ return entry === void 0 ? {
86
+ ok: false,
87
+ message: `Memory ${parsed.source}[${parsed.index}] does not exist (${entries.length} entries).`
88
+ } : {
89
+ ok: true,
90
+ message: entry
91
+ };
92
+ }
43
93
  const name = "evolution-learning-graph";
44
94
  function apply(ctx) {
45
95
  ctx.inject(["commands"], (commandCtx) => {
46
96
  commandCtx.commands.register({
47
- name: "evolution graph",
48
- description: "Show the current learning graph",
97
+ name: "graph",
98
+ description: "Show the learning graph, or act on a node: graph [detail|edit|delete] <nodeId>",
49
99
  recordInput: false,
50
- handler: async () => {
51
- const usage = ctx.get("skillUsage");
52
- const memory = ctx.get("memory");
53
- if (!usage || !memory) return { text: "skill-usage or memory service is not mounted." };
54
- const graph = buildLearningGraph(await usage.report(), await memory.read("memory"));
55
- const lines = graph.nodes.map((node) => (node.kind === "memory" ? "◆" : "●") + " " + node.label);
56
- const edges = graph.edges.map((edge) => edge.from + " --" + edge.type + "--> " + edge.to);
57
- return { text: lines.join("\n") + "\n\n" + edges.join("\n") };
100
+ handler: async (invocation) => {
101
+ const ok = (text) => ({
102
+ kind: "success",
103
+ text
104
+ });
105
+ const err = (text) => ({
106
+ kind: "error",
107
+ text
108
+ });
109
+ const usageService = ctx.get("skillUsage");
110
+ const memoryService = ctx.get("memory");
111
+ const ioService = ctx.get("evolutionIo");
112
+ if (!usageService || !memoryService || !ioService) return err("skill-usage, memory or evolution-io service is not mounted.");
113
+ const usage = usageService;
114
+ const memory = memoryService;
115
+ const io = ioService;
116
+ const input = (invocation.rawInput ?? "").trim();
117
+ const detail = /^detail\s+(\S+)$/.exec(input);
118
+ if (detail && detail[1]) return await nodeDetail(detail[1]);
119
+ const edit = /^edit\s+(\S+)\s+([\s\S]+)$/.exec(input);
120
+ if (edit && edit[1] && edit[2] !== void 0) return await nodeEdit(edit[1], edit[2]);
121
+ const remove = /^delete\s+(\S+)$/.exec(input);
122
+ if (remove && remove[1]) return await nodeDelete(remove[1]);
123
+ const directory = await renderGraph();
124
+ if (input !== "") return err(`Unknown graph subcommand "${input.split(" ")[0]}". ${directory}`);
125
+ return ok(directory);
126
+ async function renderGraph() {
127
+ const graph = buildLearningGraph(await usage.report(), await memory.read("memory"), await memory.read("user"));
128
+ const lines = graph.nodes.map((node) => (node.kind === "memory" ? "◆" : "●") + " " + node.label);
129
+ const edges = graph.edges.map((edge) => edge.from + " --" + edge.type + "--> " + edge.to);
130
+ return lines.join("\n") + "\n\n" + edges.join("\n");
131
+ }
132
+ function withSkills() {
133
+ return new SkillLibrary(void 0, evolutionIoAdapter(() => io.provider()));
134
+ }
135
+ async function nodeDetail(id) {
136
+ const parsed = parseGraphNodeId(id);
137
+ if (parsed === null) return err(`Invalid node id "${id}". Skill names or memory:<source>:<index> expected.`);
138
+ const resolved = await resolveGraphNode(parsed, {
139
+ readSkill: (name) => withSkills().read(name),
140
+ readMemory: (target) => memory.read(target)
141
+ });
142
+ return resolved.ok ? ok(resolved.message) : err(resolved.message);
143
+ }
144
+ async function nodeEdit(id, content) {
145
+ const parsed = parseGraphNodeId(id);
146
+ if (parsed === null) return err(`Invalid node id "${id}". Skill names or memory:<source>:<index> expected.`);
147
+ if (parsed.kind === "skill") {
148
+ const result = await withSkills().update(parsed.name, content, "foreground");
149
+ return result.ok ? ok(result.message) : err(result.message);
150
+ }
151
+ const entries = await memory.read(parsed.source);
152
+ const entry = entries[parsed.index];
153
+ if (entry === void 0) return err(`Memory ${parsed.source}[${parsed.index}] does not exist (${entries.length} entries).`);
154
+ const result = await memory.applyBatch(parsed.source, [{
155
+ action: "replace",
156
+ old_text: entry,
157
+ facts: content
158
+ }]);
159
+ return result.ok ? ok(result.message) : err(result.message);
160
+ }
161
+ async function nodeDelete(id) {
162
+ const parsed = parseGraphNodeId(id);
163
+ if (parsed === null) return err(`Invalid node id "${id}". Skill names or memory:<source>:<index> expected.`);
164
+ if (parsed.kind === "skill") {
165
+ const result = await withSkills().archive(parsed.name);
166
+ if (result.ok) await usageService?.markArchived?.(parsed.name);
167
+ return result.ok ? ok(result.message) : err(result.message);
168
+ }
169
+ const entries = await memory.read(parsed.source);
170
+ const entry = entries[parsed.index];
171
+ if (entry === void 0) return err(`Memory ${parsed.source}[${parsed.index}] does not exist (${entries.length} entries).`);
172
+ const result = await memory.applyBatch(parsed.source, [{
173
+ action: "remove",
174
+ old_text: entry
175
+ }]);
176
+ return result.ok ? ok(result.message) : err(result.message);
177
+ }
58
178
  }
59
179
  });
60
180
  });
61
181
  }
62
182
  //#endregion
63
- export { apply, buildLearningGraph, name };
183
+ export { apply, buildLearningGraph, name, parseGraphNodeId, resolveGraphNode };
@@ -1,5 +1,9 @@
1
1
  /**
2
- * Learning graph over skills and memory.
2
+ * Learning graph over skills and memory, plus node-level commands
3
+ * (`/evolution graph [detail|edit|delete] <nodeId>`) aligned with the Hermes
4
+ * journey surface: a skill node is its name, a memory node is
5
+ * `memory:<source>:<index>` (source = memory|user, index = position in that
6
+ * file's entries).
3
7
  * @module @deepseek-ai/dsh-evolution-learning-graph
4
8
  */
5
9
  import type { Context } from '@deepseek-ai/cordis';
@@ -17,11 +21,34 @@ export interface LearningGraph {
17
21
  nodes: GraphNode[];
18
22
  edges: GraphEdge[];
19
23
  }
20
- /** Build a small deterministic graph from usage records and memory entries. */
24
+ /**
25
+ * Build a small deterministic graph from usage records and memory entries.
26
+ * Memory node ids follow `memory:<source>:<index>` (source = memory|user,
27
+ * index = position in that file's entries) — the SAME rule the parser
28
+ * `parseGraphNodeId` accepts, so graph detail/edit/delete round-trips.
29
+ */
21
30
  export declare function buildLearningGraph(usage: ReadonlyMap<string, {
22
31
  use_count?: number;
23
32
  pinned?: boolean;
24
- }>, memoryEntries: readonly string[]): LearningGraph;
33
+ }>, memoryEntries: readonly string[], userEntries?: readonly string[]): LearningGraph;
34
+ export type GraphNodeId = {
35
+ kind: 'skill';
36
+ name: string;
37
+ } | {
38
+ kind: 'memory';
39
+ source: 'memory' | 'user';
40
+ index: number;
41
+ };
42
+ /** Parse a graph node id: skill names pass through; `memory:<source>:<index>` becomes a memory node. */
43
+ export declare function parseGraphNodeId(id: string): GraphNodeId | null;
44
+ /** Resolve a parsed node to its current content (read-only). */
45
+ export declare function resolveGraphNode(parsed: GraphNodeId, repository: {
46
+ readSkill(name: string): Promise<string | null>;
47
+ readMemory(target: 'memory' | 'user'): Promise<string[]>;
48
+ }): Promise<{
49
+ ok: boolean;
50
+ message: string;
51
+ }>;
25
52
  export declare const name = "evolution-learning-graph";
26
53
  export declare function apply(ctx: Context): void;
27
54
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lmzhen/dsh-evolution-learning-graph",
3
3
  "description": "Learning graph over skills and memory (community build)",
4
- "version": "0.1.0-rc.4",
4
+ "version": "0.1.0-rc.41",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -32,7 +32,8 @@
32
32
  ],
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@deepseek-ai/schemastery": "^3.18.1"
35
+ "@deepseek-ai/schemastery": "^3.18.1",
36
+ "@lmzhen/dsh-evolution-core": "^0.1.0-rc.41"
36
37
  },
37
38
  "peerDependencies": {
38
39
  "@deepseek-ai/dsh-invariants": "^0.1.0-rc.6",