@gethmy/mcp 2.0.0 → 2.1.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.
Files changed (62) hide show
  1. package/README.md +6 -1
  2. package/dist/cli.js +711 -59
  3. package/dist/index.js +5 -3
  4. package/dist/lib/__tests__/active-learning.test.js +386 -0
  5. package/dist/lib/__tests__/agent-performance-profiles.test.js +325 -0
  6. package/dist/lib/__tests__/auto-session.test.js +661 -0
  7. package/dist/lib/__tests__/context-assembly.test.js +362 -0
  8. package/dist/lib/__tests__/graph-expansion.test.js +150 -0
  9. package/dist/lib/__tests__/integration-memory-crud.test.js +797 -0
  10. package/dist/lib/__tests__/integration-memory-system.test.js +281 -0
  11. package/dist/lib/__tests__/lifecycle-maintenance.test.js +207 -0
  12. package/dist/lib/__tests__/pattern-detection.test.js +295 -0
  13. package/dist/lib/__tests__/prompt-builder.test.js +418 -0
  14. package/dist/lib/active-learning.js +878 -0
  15. package/dist/lib/api-client.js +550 -0
  16. package/dist/lib/auto-session.js +173 -0
  17. package/dist/lib/cli.js +127 -0
  18. package/dist/lib/config.js +205 -0
  19. package/dist/lib/consolidation.js +243 -0
  20. package/dist/lib/context-assembly.js +606 -0
  21. package/dist/lib/graph-expansion.js +163 -0
  22. package/dist/lib/http.js +174 -0
  23. package/dist/lib/index.js +7 -0
  24. package/dist/lib/lifecycle-maintenance.js +88 -0
  25. package/dist/lib/prompt-builder.js +483 -0
  26. package/dist/lib/remote.js +166 -0
  27. package/dist/lib/server.js +3132 -0
  28. package/dist/lib/tui/agents.js +116 -0
  29. package/dist/lib/tui/docs.js +744 -0
  30. package/dist/lib/tui/setup.js +1068 -0
  31. package/dist/lib/tui/theme.js +95 -0
  32. package/dist/lib/tui/writer.js +200 -0
  33. package/package.json +15 -6
  34. package/src/__tests__/active-learning.test.ts +483 -0
  35. package/src/__tests__/agent-performance-profiles.test.ts +468 -0
  36. package/src/__tests__/auto-session.test.ts +912 -0
  37. package/src/__tests__/context-assembly.test.ts +506 -0
  38. package/src/__tests__/graph-expansion.test.ts +285 -0
  39. package/src/__tests__/integration-memory-crud.test.ts +948 -0
  40. package/src/__tests__/integration-memory-system.test.ts +321 -0
  41. package/src/__tests__/lifecycle-maintenance.test.ts +238 -0
  42. package/src/__tests__/pattern-detection.test.ts +438 -0
  43. package/src/__tests__/prompt-builder.test.ts +505 -0
  44. package/src/active-learning.ts +1227 -0
  45. package/src/api-client.ts +969 -0
  46. package/src/auto-session.ts +218 -0
  47. package/src/cli.ts +166 -0
  48. package/src/config.ts +285 -0
  49. package/src/consolidation.ts +314 -0
  50. package/src/context-assembly.ts +842 -0
  51. package/src/graph-expansion.ts +234 -0
  52. package/src/http.ts +265 -0
  53. package/src/index.ts +8 -0
  54. package/src/lifecycle-maintenance.ts +120 -0
  55. package/src/prompt-builder.ts +681 -0
  56. package/src/remote.ts +227 -0
  57. package/src/server.ts +3858 -0
  58. package/src/tui/agents.ts +154 -0
  59. package/src/tui/docs.ts +863 -0
  60. package/src/tui/setup.ts +1281 -0
  61. package/src/tui/theme.ts +114 -0
  62. package/src/tui/writer.ts +260 -0
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Smart Memory Consolidation
3
+ *
4
+ * Clusters similar draft/episode memories and merges them into
5
+ * consolidated reference entities to reduce noise and improve retrieval.
6
+ */
7
+ import { findSimilarEntities } from "./graph-expansion.js";
8
+ /**
9
+ * Consolidate similar draft/episode memories into reference entities.
10
+ *
11
+ * 1. Lists all draft and episode tier entities in scope
12
+ * 2. Groups by entity type
13
+ * 3. For each type group, finds clusters via embedding similarity
14
+ * 4. Merges clusters into new reference entities with part_of relations
15
+ */
16
+ export async function consolidateMemories(client, workspaceId, projectId, options) {
17
+ const dryRun = options?.dryRun !== false; // default true
18
+ const minClusterSize = options?.minClusterSize ?? 2;
19
+ const result = {
20
+ consolidated: 0,
21
+ clustersFound: 0,
22
+ entitiesProcessed: 0,
23
+ details: [],
24
+ };
25
+ // Step 1: Fetch all draft and episode entities
26
+ const listResult = await client.listMemoryEntities({
27
+ workspace_id: workspaceId,
28
+ project_id: projectId,
29
+ limit: 100,
30
+ });
31
+ const allEntities = (listResult.entities || []).filter((e) => e.memory_tier === "draft" || e.memory_tier === "episode");
32
+ result.entitiesProcessed = allEntities.length;
33
+ if (allEntities.length < minClusterSize)
34
+ return result;
35
+ // Step 2: Group by type
36
+ const typeGroups = new Map();
37
+ for (const entity of allEntities) {
38
+ const group = typeGroups.get(entity.type) || [];
39
+ group.push(entity);
40
+ typeGroups.set(entity.type, group);
41
+ }
42
+ // Step 3: Find clusters within each type group
43
+ for (const [type, entities] of typeGroups) {
44
+ if (entities.length < minClusterSize)
45
+ continue;
46
+ const clustered = new Set();
47
+ const clusters = [];
48
+ for (const entity of entities) {
49
+ if (clustered.has(entity.id))
50
+ continue;
51
+ // Search for similar entities using embedding-based search
52
+ const similar = await findSimilarEntities(client, entity.title, entity.content, workspaceId, {
53
+ projectId,
54
+ limit: 20,
55
+ minRrfScore: 0.01,
56
+ excludeIds: [...clustered],
57
+ });
58
+ // Filter to only entities in our current type group that aren't yet clustered
59
+ const entityIdSet = new Set(entities.map((e) => e.id));
60
+ const clusterMembers = similar.filter((s) => entityIdSet.has(s.id) &&
61
+ !clustered.has(s.id) &&
62
+ s.id !== entity.id &&
63
+ s.type === type);
64
+ if (clusterMembers.length >= minClusterSize - 1) {
65
+ const cluster = [
66
+ entity,
67
+ ...clusterMembers.slice(0, 5).map((s) => {
68
+ // Map back to full entity from our list
69
+ return entities.find((e) => e.id === s.id) || entity;
70
+ }),
71
+ ];
72
+ // Deduplicate by id
73
+ const uniqueCluster = [];
74
+ const seen = new Set();
75
+ for (const member of cluster) {
76
+ if (!seen.has(member.id)) {
77
+ seen.add(member.id);
78
+ uniqueCluster.push(member);
79
+ }
80
+ }
81
+ if (uniqueCluster.length >= minClusterSize) {
82
+ clusters.push(uniqueCluster);
83
+ for (const member of uniqueCluster) {
84
+ clustered.add(member.id);
85
+ }
86
+ }
87
+ }
88
+ }
89
+ // Step 4: Create consolidated entities for each cluster
90
+ for (const cluster of clusters) {
91
+ result.clustersFound++;
92
+ // Derive title from most common words across cluster titles
93
+ const mergedTitle = deriveClusterTitle(cluster, type);
94
+ const memberTitles = cluster.map((e) => e.title);
95
+ // Merge content as bullet points
96
+ const mergedContent = [
97
+ `Consolidated from ${cluster.length} ${type} memories:\n`,
98
+ ...cluster.map((e) => `- **${e.title}**: ${e.content.slice(0, 200)}`),
99
+ ].join("\n");
100
+ // Max confidence from cluster members
101
+ const maxConfidence = Math.max(...cluster.map((e) => e.confidence));
102
+ // Union of all tags (deduped)
103
+ const allTags = [...new Set(cluster.flatMap((e) => e.tags || []))];
104
+ const detail = {
105
+ clusterSize: cluster.length,
106
+ mergedTitle,
107
+ memberTitles,
108
+ };
109
+ if (!dryRun) {
110
+ try {
111
+ // Create consolidated reference entity
112
+ const createResult = await client.createMemoryEntity({
113
+ workspace_id: workspaceId,
114
+ project_id: projectId,
115
+ type,
116
+ scope: "project",
117
+ memory_tier: "reference",
118
+ title: mergedTitle,
119
+ content: mergedContent,
120
+ confidence: maxConfidence,
121
+ tags: [...allTags.slice(0, 15), "consolidated"],
122
+ metadata: {
123
+ source: "consolidation",
124
+ member_ids: cluster.map((e) => e.id),
125
+ consolidated_at: new Date().toISOString(),
126
+ },
127
+ });
128
+ const newEntity = createResult.entity;
129
+ if (newEntity?.id) {
130
+ detail.entityId = newEntity.id;
131
+ // Create part_of relations from members → consolidated entity
132
+ for (const member of cluster) {
133
+ try {
134
+ await client.createMemoryRelation({
135
+ source_id: member.id,
136
+ target_id: newEntity.id,
137
+ relation_type: "part_of",
138
+ confidence: 0.8,
139
+ });
140
+ }
141
+ catch {
142
+ // Skip duplicate relations
143
+ }
144
+ }
145
+ // Downgrade member confidence by 0.3 (min 0.1)
146
+ for (const member of cluster) {
147
+ try {
148
+ const newConf = Math.max(member.confidence - 0.3, 0.1);
149
+ await client.updateMemoryEntity(member.id, {
150
+ confidence: newConf,
151
+ metadata: {
152
+ consolidated_into: newEntity.id,
153
+ original_confidence: member.confidence,
154
+ },
155
+ });
156
+ }
157
+ catch {
158
+ // Non-fatal
159
+ }
160
+ }
161
+ result.consolidated++;
162
+ }
163
+ }
164
+ catch {
165
+ // Non-fatal: consolidation failure for one cluster shouldn't block others
166
+ }
167
+ }
168
+ else {
169
+ result.consolidated++;
170
+ }
171
+ result.details.push(detail);
172
+ }
173
+ }
174
+ return result;
175
+ }
176
+ /**
177
+ * Derive a cluster title from the most common meaningful words across member titles.
178
+ */
179
+ function deriveClusterTitle(cluster, type) {
180
+ const stopWords = new Set([
181
+ "the",
182
+ "a",
183
+ "an",
184
+ "is",
185
+ "are",
186
+ "was",
187
+ "were",
188
+ "be",
189
+ "been",
190
+ "being",
191
+ "have",
192
+ "has",
193
+ "had",
194
+ "do",
195
+ "does",
196
+ "did",
197
+ "will",
198
+ "shall",
199
+ "would",
200
+ "should",
201
+ "may",
202
+ "might",
203
+ "can",
204
+ "could",
205
+ "of",
206
+ "in",
207
+ "to",
208
+ "for",
209
+ "with",
210
+ "on",
211
+ "at",
212
+ "from",
213
+ "by",
214
+ "and",
215
+ "or",
216
+ "but",
217
+ "not",
218
+ "session",
219
+ "blocker",
220
+ "pattern",
221
+ "solution",
222
+ "error",
223
+ "task",
224
+ "mid-session",
225
+ ]);
226
+ const wordCounts = new Map();
227
+ for (const entity of cluster) {
228
+ const words = entity.title
229
+ .toLowerCase()
230
+ .split(/\W+/)
231
+ .filter((w) => w.length > 2 && !stopWords.has(w));
232
+ for (const word of words) {
233
+ wordCounts.set(word, (wordCounts.get(word) || 0) + 1);
234
+ }
235
+ }
236
+ // Sort by frequency, take top 3
237
+ const topWords = [...wordCounts.entries()]
238
+ .sort((a, b) => b[1] - a[1])
239
+ .slice(0, 3)
240
+ .map(([word]) => word);
241
+ const suffix = topWords.length > 0 ? topWords.join(", ") : "various";
242
+ return `Consolidated ${type}: ${suffix}`;
243
+ }