@stackmemoryai/stackmemory 1.10.5 → 1.14.0
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/LICENSE +131 -64
- package/README.md +107 -24
- package/bin/claude-sm +16 -1
- package/bin/claude-smd +16 -1
- package/bin/codex-smd +16 -1
- package/bin/gemini-sm +16 -1
- package/bin/hermes-sm +21 -0
- package/bin/hermes-smd +21 -0
- package/bin/opencode-sm +16 -1
- package/dist/src/cli/claude-sm.js +266 -84
- package/dist/src/cli/codex-sm.js +225 -33
- package/dist/src/cli/commands/bench.js +209 -2
- package/dist/src/cli/commands/brain.js +206 -0
- package/dist/src/cli/commands/cache.js +126 -0
- package/dist/src/cli/commands/company-os.js +184 -0
- package/dist/src/cli/commands/context.js +5 -0
- package/dist/src/cli/commands/daemon.js +41 -0
- package/dist/src/cli/commands/handoff.js +40 -9
- package/dist/src/cli/commands/onboard.js +70 -3
- package/dist/src/cli/commands/operator.js +127 -0
- package/dist/src/cli/commands/optimize.js +117 -0
- package/dist/src/cli/commands/orchestrate.js +232 -5
- package/dist/src/cli/commands/orchestrator.js +315 -26
- package/dist/src/cli/commands/pack.js +322 -0
- package/dist/src/cli/commands/patterns.js +254 -0
- package/dist/src/cli/commands/portal.js +161 -0
- package/dist/src/cli/commands/scaffold.js +92 -0
- package/dist/src/cli/commands/search.js +40 -1
- package/dist/src/cli/commands/setup.js +178 -11
- package/dist/src/cli/commands/skills.js +10 -1
- package/dist/src/cli/commands/state.js +265 -0
- package/dist/src/cli/commands/sync.js +253 -0
- package/dist/src/cli/commands/tasks.js +130 -1
- package/dist/src/cli/commands/vision.js +221 -0
- package/dist/src/cli/gemini-sm.js +19 -29
- package/dist/src/cli/hermes-sm.js +224 -0
- package/dist/src/cli/index.js +105 -39
- package/dist/src/cli/opencode-sm.js +38 -21
- package/dist/src/cli/utils/determinism-watcher.js +66 -0
- package/dist/src/cli/utils/real-cli-bin.js +116 -0
- package/dist/src/core/brain/brain-store.js +187 -0
- package/dist/src/core/brain/brain-sync.js +193 -0
- package/dist/src/core/brain/index.js +78 -0
- package/dist/src/core/brain/types.js +10 -0
- package/dist/src/core/cache/content-cache.js +238 -0
- package/dist/src/{integrations/diffmem → core/cache}/index.js +5 -5
- package/dist/src/core/cache/token-estimator.js +39 -0
- package/dist/src/core/config/feature-flags.js +2 -6
- package/dist/src/core/context/frame-database.js +79 -27
- package/dist/src/core/context/recursive-context-manager.js +1 -1
- package/dist/src/core/context/rehydration.js +2 -1
- package/dist/src/core/cross-search/cross-project-search.js +269 -0
- package/dist/src/core/cross-search/index.js +10 -0
- package/dist/src/core/database/sqlite-adapter.js +14 -84
- package/dist/src/core/extensions/provider-adapter.js +5 -0
- package/dist/src/core/models/model-router.js +54 -2
- package/dist/src/core/models/provider-pricing.js +58 -4
- package/dist/src/core/monitoring/logger.js +2 -1
- package/dist/src/core/optimization/trace-optimizer.js +413 -0
- package/dist/src/core/patterns/index.js +22 -0
- package/dist/src/core/patterns/pattern-applier.js +39 -0
- package/dist/src/core/patterns/pattern-observer.js +157 -0
- package/dist/src/core/patterns/pattern-store.js +259 -0
- package/dist/src/core/patterns/types.js +19 -0
- package/dist/src/core/provenance/confidence-scorer.js +128 -0
- package/dist/src/core/provenance/index.js +40 -0
- package/dist/src/core/provenance/provenance-store.js +194 -0
- package/dist/src/core/provenance/types.js +82 -0
- package/dist/src/core/retrieval/llm-context-retrieval.js +5 -4
- package/dist/src/core/retrieval/unified-context-assembler.js +11 -66
- package/dist/src/core/session/project-handoff.js +64 -0
- package/dist/src/core/session/session-manager.js +28 -0
- package/dist/src/core/shared-state/canonical-store.js +564 -0
- package/dist/src/core/skill-packs/index.js +18 -0
- package/dist/src/core/skill-packs/parser.js +42 -0
- package/dist/src/core/skill-packs/registry.js +224 -0
- package/dist/src/core/skill-packs/types.js +79 -0
- package/dist/src/core/storage/cloud-sync-manager.js +116 -0
- package/dist/src/core/storage/cloud-sync.js +574 -0
- package/dist/src/core/storage/two-tier-storage.js +5 -1
- package/dist/src/core/tasks/master-tasks-template.js +43 -0
- package/dist/src/core/tasks/md-task-parser.js +138 -0
- package/dist/src/core/trace/trace-event-store.js +282 -0
- package/dist/src/core/vision/index.js +27 -0
- package/dist/src/core/vision/signals.js +79 -0
- package/dist/src/core/vision/types.js +22 -0
- package/dist/src/core/vision/vision-file.js +146 -0
- package/dist/src/core/vision/vision-loop.js +220 -0
- package/dist/src/core/wiki/wiki-compiler.js +103 -1
- package/dist/src/daemon/daemon-config.js +52 -0
- package/dist/src/daemon/services/desire-path-service.js +566 -0
- package/dist/src/daemon/services/github-service.js +126 -0
- package/dist/src/daemon/services/research-stream-service.js +320 -0
- package/dist/src/daemon/services/telemetry-service.js +192 -0
- package/dist/src/daemon/unified-daemon.js +58 -1
- package/dist/src/features/browser/cli-browser-agent.js +417 -0
- package/dist/src/features/browser/stagehand-workflows.js +578 -0
- package/dist/src/features/operator/adapter-factory.js +62 -0
- package/dist/src/features/operator/browser-adapter.js +109 -0
- package/dist/src/features/operator/desktop-adapter.js +125 -0
- package/dist/src/features/operator/index.js +39 -0
- package/dist/src/features/operator/llm-decision.js +137 -0
- package/dist/src/features/operator/operator-logger.js +92 -0
- package/dist/src/features/operator/overnight-runner.js +327 -0
- package/dist/src/features/operator/screen-adapter.js +91 -0
- package/dist/src/features/operator/session-manager.js +127 -0
- package/dist/src/features/operator/state-machine.js +227 -0
- package/dist/src/features/operator/task-queue.js +81 -0
- package/dist/src/features/portal/index.js +26 -0
- package/dist/src/features/portal/server.js +240 -0
- package/dist/src/features/portal/types.js +14 -0
- package/dist/src/features/portal/ui.js +195 -0
- package/dist/src/features/sweep/pty-wrapper.js +13 -5
- package/dist/src/features/tasks/task-aware-context.js +2 -1
- package/dist/src/features/tui/simple-monitor.js +0 -23
- package/dist/src/features/tui/swarm-monitor.js +8 -66
- package/dist/src/features/web/client/hooks/use-socket.js +12 -0
- package/dist/src/{core/merge/index.js → features/web/client/lib/utils.js} +8 -4
- package/dist/src/features/web/client/next-env.d.js +4 -0
- package/dist/src/features/web/client/stores/session-store.js +12 -0
- package/dist/src/features/web/server/gcp-billing.js +76 -0
- package/dist/src/features/web/server/index.js +10 -0
- package/dist/src/features/web/server/spend-calculator.js +228 -0
- package/dist/src/hooks/schemas.js +6 -1
- package/dist/src/integrations/anthropic/client.js +3 -2
- package/dist/src/integrations/claude-code/agent-bridge.js +0 -3
- package/dist/src/integrations/claude-code/subagent-client.js +307 -11
- package/dist/src/integrations/claude-code/task-coordinator.js +2 -1
- package/dist/src/integrations/github/pr-state.js +158 -0
- package/dist/src/integrations/linear/client.js +4 -1
- package/dist/src/integrations/linear/webhook-retry.js +196 -0
- package/dist/src/integrations/linear/webhook-server.js +18 -22
- package/dist/src/integrations/mcp/handlers/cloud-sync-handlers.js +101 -0
- package/dist/src/integrations/mcp/handlers/index.js +40 -84
- package/dist/src/integrations/mcp/server.js +542 -641
- package/dist/src/integrations/mcp/tool-alias-registry.js +297 -0
- package/dist/src/integrations/mcp/tool-definitions.js +152 -682
- package/dist/src/mcp/stackmemory-mcp-server.js +571 -231
- package/dist/src/orchestrators/multimodal/determinism.js +244 -0
- package/dist/src/orchestrators/multimodal/harness.js +149 -78
- package/dist/src/orchestrators/multimodal/providers.js +44 -3
- package/dist/src/skills/recursive-agent-orchestrator.js +2 -4
- package/dist/src/utils/hook-installer.js +0 -8
- package/dist/src/utils/process-cleanup.js +1 -7
- package/docs/README.md +42 -0
- package/docs/guides/README_INSTALL.md +208 -0
- package/package.json +27 -9
- package/packs/coding/python-fastapi/instructions.md +60 -0
- package/packs/coding/python-fastapi/pack.yaml +28 -0
- package/packs/coding/typescript-react/instructions.md +47 -0
- package/packs/coding/typescript-react/pack.yaml +28 -0
- package/packs/core/commands/capture.md +32 -0
- package/packs/core/commands/learn.md +73 -0
- package/packs/core/commands/next.md +36 -0
- package/packs/core/commands/restart.md +58 -0
- package/packs/core/commands/restore.md +29 -0
- package/packs/core/commands/start.md +57 -0
- package/packs/core/commands/stop.md +65 -0
- package/packs/core/commands/summary.md +40 -0
- package/packs/core/manifest.json +24 -0
- package/packs/ops/decision-recovery/instructions.md +65 -0
- package/packs/ops/decision-recovery/pack.yaml +89 -0
- package/scripts/claude-code-wrapper.sh +11 -0
- package/scripts/claude-sm-setup.sh +12 -1
- package/scripts/codex-wrapper.sh +11 -0
- package/scripts/git-hooks/branch-context-manager.sh +11 -0
- package/scripts/git-hooks/post-checkout-stackmemory.sh +11 -0
- package/scripts/git-hooks/post-commit-stackmemory.sh +11 -0
- package/scripts/git-hooks/pre-commit-stackmemory.sh +11 -0
- package/scripts/hooks/cleanup-shell.sh +12 -1
- package/scripts/hooks/task-complete.sh +12 -1
- package/scripts/install-code-execution-hooks.sh +12 -1
- package/scripts/install-sweep-hook.sh +12 -0
- package/scripts/install.sh +11 -0
- package/scripts/opencode-wrapper.sh +11 -0
- package/scripts/portal/cloud-init.yaml +69 -0
- package/scripts/portal/setup.sh +69 -0
- package/scripts/portal/stackmemory-portal.service +34 -0
- package/scripts/setup-claude-integration.sh +12 -1
- package/scripts/smoke-init-db.sh +23 -0
- package/scripts/stackmemory-daemon.sh +11 -0
- package/scripts/verify-dist.cjs +11 -4
- package/dist/src/cli/commands/ralph.js +0 -1053
- package/dist/src/cli/commands/team.js +0 -168
- package/dist/src/core/context/shared-context-layer.js +0 -620
- package/dist/src/core/context/stack-merge-resolver.js +0 -748
- package/dist/src/core/merge/conflict-detector.js +0 -430
- package/dist/src/core/merge/resolution-engine.js +0 -557
- package/dist/src/core/merge/stack-diff.js +0 -531
- package/dist/src/core/merge/unified-merge-resolver.js +0 -302
- package/dist/src/hooks/diffmem-hooks.js +0 -376
- package/dist/src/integrations/diffmem/client.js +0 -208
- package/dist/src/integrations/diffmem/config.js +0 -14
- package/dist/src/integrations/greptile/client.js +0 -101
- package/dist/src/integrations/greptile/config.js +0 -14
- package/dist/src/integrations/greptile/index.js +0 -11
- package/dist/src/integrations/mcp/handlers/cord-handlers.js +0 -397
- package/dist/src/integrations/mcp/handlers/diffmem-handlers.js +0 -455
- package/dist/src/integrations/mcp/handlers/greptile-handlers.js +0 -456
- package/dist/src/integrations/mcp/handlers/provider-handlers.js +0 -227
- package/dist/src/integrations/mcp/handlers/team-handlers.js +0 -211
- package/dist/src/integrations/ralph/bridge/ralph-stackmemory-bridge.js +0 -863
- package/dist/src/integrations/ralph/context/context-budget-manager.js +0 -308
- package/dist/src/integrations/ralph/context/stackmemory-context-loader.js +0 -391
- package/dist/src/integrations/ralph/index.js +0 -17
- package/dist/src/integrations/ralph/learning/pattern-learner.js +0 -435
- package/dist/src/integrations/ralph/lifecycle/iteration-lifecycle.js +0 -448
- package/dist/src/integrations/ralph/loopmax.js +0 -488
- package/dist/src/integrations/ralph/monitoring/swarm-dashboard.js +0 -293
- package/dist/src/integrations/ralph/monitoring/swarm-registry.js +0 -107
- package/dist/src/integrations/ralph/orchestration/multi-loop-orchestrator.js +0 -508
- package/dist/src/integrations/ralph/patterns/compounding-engineering-pattern.js +0 -407
- package/dist/src/integrations/ralph/patterns/extended-coherence-sessions.js +0 -495
- package/dist/src/integrations/ralph/patterns/oracle-worker-pattern.js +0 -387
- package/dist/src/integrations/ralph/performance/performance-optimizer.js +0 -357
- package/dist/src/integrations/ralph/recovery/crash-recovery.js +0 -461
- package/dist/src/integrations/ralph/state/state-reconciler.js +0 -420
- package/dist/src/integrations/ralph/swarm/git-workflow-manager.js +0 -444
- package/dist/src/integrations/ralph/swarm/swarm-coordinator.js +0 -1007
- package/dist/src/integrations/ralph/visualization/ralph-debugger.js +0 -635
- package/scripts/ralph-loop-implementation.js +0 -404
- /package/dist/src/core/{merge → cache}/types.js +0 -0
- /package/dist/src/{integrations/diffmem/types.js → core/storage/cloud-sync-types.js} +0 -0
- /package/dist/src/{integrations/greptile/types.js → core/trace/trace-event.js} +0 -0
- /package/dist/src/{integrations/ralph → features/operator}/types.js +0 -0
|
@@ -1,455 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath as __fileURLToPath } from 'url';
|
|
2
|
-
import { dirname as __pathDirname } from 'path';
|
|
3
|
-
const __filename = __fileURLToPath(import.meta.url);
|
|
4
|
-
const __dirname = __pathDirname(__filename);
|
|
5
|
-
import {
|
|
6
|
-
DEFAULT_DIFFMEM_CONFIG
|
|
7
|
-
} from "../../diffmem/config.js";
|
|
8
|
-
import { logger } from "../../../core/monitoring/logger.js";
|
|
9
|
-
class DiffMemHandlers {
|
|
10
|
-
config;
|
|
11
|
-
cache = /* @__PURE__ */ new Map();
|
|
12
|
-
cacheTTL = 5 * 60 * 1e3;
|
|
13
|
-
// 5 minutes
|
|
14
|
-
constructor(deps) {
|
|
15
|
-
this.config = { ...DEFAULT_DIFFMEM_CONFIG, ...deps?.config };
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Get tool definitions for DiffMem tools
|
|
19
|
-
*/
|
|
20
|
-
getToolDefinitions() {
|
|
21
|
-
return [
|
|
22
|
-
{
|
|
23
|
-
name: "diffmem_get_user_context",
|
|
24
|
-
description: "Fetch user knowledge and preferences from memory. Use to personalize responses based on learned user patterns.",
|
|
25
|
-
inputSchema: {
|
|
26
|
-
type: "object",
|
|
27
|
-
properties: {
|
|
28
|
-
categories: {
|
|
29
|
-
type: "array",
|
|
30
|
-
items: {
|
|
31
|
-
type: "string",
|
|
32
|
-
enum: [
|
|
33
|
-
"preference",
|
|
34
|
-
"expertise",
|
|
35
|
-
"project_knowledge",
|
|
36
|
-
"pattern",
|
|
37
|
-
"correction"
|
|
38
|
-
]
|
|
39
|
-
},
|
|
40
|
-
description: "Filter by memory categories"
|
|
41
|
-
},
|
|
42
|
-
limit: {
|
|
43
|
-
type: "number",
|
|
44
|
-
default: 10,
|
|
45
|
-
description: "Maximum memories to return"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
name: "diffmem_store_learning",
|
|
52
|
-
description: "Store a new insight about the user (preference, expertise, pattern, or correction)",
|
|
53
|
-
inputSchema: {
|
|
54
|
-
type: "object",
|
|
55
|
-
properties: {
|
|
56
|
-
content: {
|
|
57
|
-
type: "string",
|
|
58
|
-
description: "The insight to store"
|
|
59
|
-
},
|
|
60
|
-
category: {
|
|
61
|
-
type: "string",
|
|
62
|
-
enum: [
|
|
63
|
-
"preference",
|
|
64
|
-
"expertise",
|
|
65
|
-
"project_knowledge",
|
|
66
|
-
"pattern",
|
|
67
|
-
"correction"
|
|
68
|
-
],
|
|
69
|
-
description: "Category of the insight"
|
|
70
|
-
},
|
|
71
|
-
confidence: {
|
|
72
|
-
type: "number",
|
|
73
|
-
minimum: 0,
|
|
74
|
-
maximum: 1,
|
|
75
|
-
default: 0.7,
|
|
76
|
-
description: "Confidence level (0-1)"
|
|
77
|
-
},
|
|
78
|
-
context: {
|
|
79
|
-
type: "object",
|
|
80
|
-
description: "Additional context for the insight"
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
required: ["content", "category"]
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
name: "diffmem_search",
|
|
88
|
-
description: "Semantic search across user memories. Find relevant past insights and preferences.",
|
|
89
|
-
inputSchema: {
|
|
90
|
-
type: "object",
|
|
91
|
-
properties: {
|
|
92
|
-
query: {
|
|
93
|
-
type: "string",
|
|
94
|
-
description: "Search query"
|
|
95
|
-
},
|
|
96
|
-
timeRange: {
|
|
97
|
-
type: "string",
|
|
98
|
-
enum: ["day", "week", "month", "all"],
|
|
99
|
-
default: "all",
|
|
100
|
-
description: "Time range filter"
|
|
101
|
-
},
|
|
102
|
-
minConfidence: {
|
|
103
|
-
type: "number",
|
|
104
|
-
minimum: 0,
|
|
105
|
-
maximum: 1,
|
|
106
|
-
default: 0.5,
|
|
107
|
-
description: "Minimum confidence threshold"
|
|
108
|
-
},
|
|
109
|
-
limit: {
|
|
110
|
-
type: "number",
|
|
111
|
-
default: 10,
|
|
112
|
-
description: "Maximum results"
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
required: ["query"]
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
name: "diffmem_status",
|
|
120
|
-
description: "Check DiffMem connection status and memory statistics",
|
|
121
|
-
inputSchema: {
|
|
122
|
-
type: "object",
|
|
123
|
-
properties: {}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
];
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Fetch user context/memories with optional category filter
|
|
130
|
-
*/
|
|
131
|
-
async handleGetUserContext(args) {
|
|
132
|
-
const { categories, limit = 10 } = args;
|
|
133
|
-
try {
|
|
134
|
-
const cacheKey = `context:${JSON.stringify(categories)}:${limit}`;
|
|
135
|
-
const cached = this.getFromCache(cacheKey);
|
|
136
|
-
if (cached) {
|
|
137
|
-
logger.debug("DiffMem cache hit", { cacheKey });
|
|
138
|
-
return this.formatMemoriesResponse(cached, true);
|
|
139
|
-
}
|
|
140
|
-
const query = { limit };
|
|
141
|
-
if (categories?.length) {
|
|
142
|
-
query.categories = categories;
|
|
143
|
-
}
|
|
144
|
-
const memories = await this.fetchMemories(query);
|
|
145
|
-
this.setCache(cacheKey, memories);
|
|
146
|
-
return this.formatMemoriesResponse(memories, false);
|
|
147
|
-
} catch (error) {
|
|
148
|
-
return this.handleError("getUserContext", error);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Store a new learning/insight
|
|
153
|
-
*/
|
|
154
|
-
async handleStoreLearning(args) {
|
|
155
|
-
const { content, category, confidence = 0.7, context } = args;
|
|
156
|
-
if (!content) {
|
|
157
|
-
return {
|
|
158
|
-
content: [{ type: "text", text: "Error: content is required" }],
|
|
159
|
-
metadata: { error: true }
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
if (!category) {
|
|
163
|
-
return {
|
|
164
|
-
content: [{ type: "text", text: "Error: category is required" }],
|
|
165
|
-
metadata: { error: true }
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
try {
|
|
169
|
-
const insight = {
|
|
170
|
-
content,
|
|
171
|
-
category,
|
|
172
|
-
confidence,
|
|
173
|
-
source: "stackmemory",
|
|
174
|
-
timestamp: Date.now(),
|
|
175
|
-
context
|
|
176
|
-
};
|
|
177
|
-
await this.storeInsight(insight);
|
|
178
|
-
this.invalidateCacheByPrefix("context:");
|
|
179
|
-
logger.info("Stored DiffMem insight", { category, confidence });
|
|
180
|
-
return {
|
|
181
|
-
content: [
|
|
182
|
-
{
|
|
183
|
-
type: "text",
|
|
184
|
-
text: `Stored ${category} insight: "${content.substring(0, 50)}${content.length > 50 ? "..." : ""}"`
|
|
185
|
-
}
|
|
186
|
-
],
|
|
187
|
-
metadata: {
|
|
188
|
-
stored: true,
|
|
189
|
-
category,
|
|
190
|
-
confidence
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
} catch (error) {
|
|
194
|
-
return this.handleError("storeLearning", error);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Semantic search across memories
|
|
199
|
-
*/
|
|
200
|
-
async handleSearch(args) {
|
|
201
|
-
const { query, timeRange = "all", minConfidence = 0.5, limit = 10 } = args;
|
|
202
|
-
if (!query) {
|
|
203
|
-
return {
|
|
204
|
-
content: [{ type: "text", text: "Error: query is required" }],
|
|
205
|
-
metadata: { error: true }
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
try {
|
|
209
|
-
const searchQuery = {
|
|
210
|
-
query,
|
|
211
|
-
timeRange,
|
|
212
|
-
minConfidence,
|
|
213
|
-
limit
|
|
214
|
-
};
|
|
215
|
-
const results = await this.searchMemories(searchQuery);
|
|
216
|
-
if (results.length === 0) {
|
|
217
|
-
return {
|
|
218
|
-
content: [
|
|
219
|
-
{
|
|
220
|
-
type: "text",
|
|
221
|
-
text: `No memories found matching "${query}"`
|
|
222
|
-
}
|
|
223
|
-
],
|
|
224
|
-
metadata: {
|
|
225
|
-
query,
|
|
226
|
-
resultCount: 0
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
const formattedResults = results.map(
|
|
231
|
-
(m, i) => `${i + 1}. [${m.category}] ${m.content} (confidence: ${(m.confidence * 100).toFixed(0)}%)`
|
|
232
|
-
).join("\n");
|
|
233
|
-
return {
|
|
234
|
-
content: [
|
|
235
|
-
{
|
|
236
|
-
type: "text",
|
|
237
|
-
text: `Search results for "${query}":
|
|
238
|
-
${formattedResults}`
|
|
239
|
-
}
|
|
240
|
-
],
|
|
241
|
-
metadata: {
|
|
242
|
-
query,
|
|
243
|
-
resultCount: results.length,
|
|
244
|
-
results: results.map((m) => ({
|
|
245
|
-
id: m.id,
|
|
246
|
-
category: m.category,
|
|
247
|
-
confidence: m.confidence
|
|
248
|
-
}))
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
} catch (error) {
|
|
252
|
-
return this.handleError("search", error);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Get DiffMem connection status
|
|
257
|
-
*/
|
|
258
|
-
async handleStatus() {
|
|
259
|
-
try {
|
|
260
|
-
const status = await this.getStatus();
|
|
261
|
-
const statusText = status.connected ? `DiffMem Status:
|
|
262
|
-
- Connected: Yes
|
|
263
|
-
- Memories: ${status.memoryCount}
|
|
264
|
-
- Last sync: ${status.lastSync ? new Date(status.lastSync).toISOString() : "Never"}
|
|
265
|
-
- Version: ${status.version || "Unknown"}` : `DiffMem Status:
|
|
266
|
-
- Connected: No
|
|
267
|
-
- Endpoint: ${this.config.endpoint}
|
|
268
|
-
- Enabled: ${this.config.enabled}`;
|
|
269
|
-
return {
|
|
270
|
-
content: [{ type: "text", text: statusText }],
|
|
271
|
-
metadata: status
|
|
272
|
-
};
|
|
273
|
-
} catch (error) {
|
|
274
|
-
return this.handleError("status", error);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
// Private helper methods
|
|
278
|
-
formatMemoriesResponse(memories, fromCache) {
|
|
279
|
-
if (memories.length === 0) {
|
|
280
|
-
return {
|
|
281
|
-
content: [
|
|
282
|
-
{
|
|
283
|
-
type: "text",
|
|
284
|
-
text: "No user context memories found."
|
|
285
|
-
}
|
|
286
|
-
],
|
|
287
|
-
metadata: { fromCache, count: 0 }
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
const byCategory = memories.reduce(
|
|
291
|
-
(acc, m) => {
|
|
292
|
-
if (!acc[m.category]) {
|
|
293
|
-
acc[m.category] = [];
|
|
294
|
-
}
|
|
295
|
-
acc[m.category].push(m);
|
|
296
|
-
return acc;
|
|
297
|
-
},
|
|
298
|
-
{}
|
|
299
|
-
);
|
|
300
|
-
const sections = Object.entries(byCategory).map(([category, mems]) => {
|
|
301
|
-
const items = mems.map(
|
|
302
|
-
(m) => ` - ${m.content} (${(m.confidence * 100).toFixed(0)}% confidence)`
|
|
303
|
-
).join("\n");
|
|
304
|
-
return `${category.toUpperCase()}:
|
|
305
|
-
${items}`;
|
|
306
|
-
}).join("\n\n");
|
|
307
|
-
return {
|
|
308
|
-
content: [
|
|
309
|
-
{
|
|
310
|
-
type: "text",
|
|
311
|
-
text: `User Context:
|
|
312
|
-
|
|
313
|
-
${sections}`
|
|
314
|
-
}
|
|
315
|
-
],
|
|
316
|
-
metadata: {
|
|
317
|
-
fromCache,
|
|
318
|
-
count: memories.length,
|
|
319
|
-
categories: Object.keys(byCategory)
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
handleError(operation, error) {
|
|
324
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
325
|
-
const isConnectionError = errorMessage.includes("ECONNREFUSED") || errorMessage.includes("fetch failed") || errorMessage.includes("network");
|
|
326
|
-
logger.warn(`DiffMem ${operation} failed`, { error: errorMessage });
|
|
327
|
-
if (isConnectionError) {
|
|
328
|
-
return {
|
|
329
|
-
content: [
|
|
330
|
-
{
|
|
331
|
-
type: "text",
|
|
332
|
-
text: `DiffMem unavailable (${operation}). Session continues without user memory.`
|
|
333
|
-
}
|
|
334
|
-
],
|
|
335
|
-
metadata: {
|
|
336
|
-
error: true,
|
|
337
|
-
unavailable: true,
|
|
338
|
-
operation
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
return {
|
|
343
|
-
content: [
|
|
344
|
-
{
|
|
345
|
-
type: "text",
|
|
346
|
-
text: `DiffMem ${operation} error: ${errorMessage}`
|
|
347
|
-
}
|
|
348
|
-
],
|
|
349
|
-
metadata: {
|
|
350
|
-
error: true,
|
|
351
|
-
operation,
|
|
352
|
-
message: errorMessage
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
getFromCache(key) {
|
|
357
|
-
const entry = this.cache.get(key);
|
|
358
|
-
if (!entry) return void 0;
|
|
359
|
-
if (Date.now() - entry.timestamp > this.cacheTTL) {
|
|
360
|
-
this.cache.delete(key);
|
|
361
|
-
return void 0;
|
|
362
|
-
}
|
|
363
|
-
return entry.data;
|
|
364
|
-
}
|
|
365
|
-
setCache(key, data) {
|
|
366
|
-
this.cache.set(key, { data, timestamp: Date.now() });
|
|
367
|
-
}
|
|
368
|
-
invalidateCacheByPrefix(prefix) {
|
|
369
|
-
for (const key of this.cache.keys()) {
|
|
370
|
-
if (key.startsWith(prefix)) {
|
|
371
|
-
this.cache.delete(key);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
// API interaction methods (with graceful degradation)
|
|
376
|
-
async fetchMemories(query) {
|
|
377
|
-
if (!this.config.enabled) {
|
|
378
|
-
return [];
|
|
379
|
-
}
|
|
380
|
-
const response = await fetch(`${this.config.endpoint}/memories`, {
|
|
381
|
-
method: "POST",
|
|
382
|
-
headers: { "Content-Type": "application/json" },
|
|
383
|
-
body: JSON.stringify(query),
|
|
384
|
-
signal: AbortSignal.timeout(this.config.timeout)
|
|
385
|
-
});
|
|
386
|
-
if (!response.ok) {
|
|
387
|
-
throw new Error(`DiffMem API error: ${response.status}`);
|
|
388
|
-
}
|
|
389
|
-
const data = await response.json();
|
|
390
|
-
return data.memories || [];
|
|
391
|
-
}
|
|
392
|
-
async searchMemories(query) {
|
|
393
|
-
if (!this.config.enabled) {
|
|
394
|
-
return [];
|
|
395
|
-
}
|
|
396
|
-
const response = await fetch(`${this.config.endpoint}/search`, {
|
|
397
|
-
method: "POST",
|
|
398
|
-
headers: { "Content-Type": "application/json" },
|
|
399
|
-
body: JSON.stringify(query),
|
|
400
|
-
signal: AbortSignal.timeout(this.config.timeout)
|
|
401
|
-
});
|
|
402
|
-
if (!response.ok) {
|
|
403
|
-
throw new Error(`DiffMem API error: ${response.status}`);
|
|
404
|
-
}
|
|
405
|
-
const data = await response.json();
|
|
406
|
-
return data.results || [];
|
|
407
|
-
}
|
|
408
|
-
async storeInsight(insight) {
|
|
409
|
-
if (!this.config.enabled) {
|
|
410
|
-
logger.debug("DiffMem disabled, skipping store");
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
const response = await fetch(`${this.config.endpoint}/insights`, {
|
|
414
|
-
method: "POST",
|
|
415
|
-
headers: { "Content-Type": "application/json" },
|
|
416
|
-
body: JSON.stringify(insight),
|
|
417
|
-
signal: AbortSignal.timeout(this.config.timeout)
|
|
418
|
-
});
|
|
419
|
-
if (!response.ok) {
|
|
420
|
-
throw new Error(`DiffMem API error: ${response.status}`);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
async getStatus() {
|
|
424
|
-
if (!this.config.enabled) {
|
|
425
|
-
return {
|
|
426
|
-
connected: false,
|
|
427
|
-
memoryCount: 0,
|
|
428
|
-
lastSync: null
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
try {
|
|
432
|
-
const response = await fetch(`${this.config.endpoint}/status`, {
|
|
433
|
-
method: "GET",
|
|
434
|
-
signal: AbortSignal.timeout(this.config.timeout)
|
|
435
|
-
});
|
|
436
|
-
if (!response.ok) {
|
|
437
|
-
return {
|
|
438
|
-
connected: false,
|
|
439
|
-
memoryCount: 0,
|
|
440
|
-
lastSync: null
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
return await response.json();
|
|
444
|
-
} catch {
|
|
445
|
-
return {
|
|
446
|
-
connected: false,
|
|
447
|
-
memoryCount: 0,
|
|
448
|
-
lastSync: null
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
export {
|
|
454
|
-
DiffMemHandlers
|
|
455
|
-
};
|