-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.js
More file actions
196 lines (168 loc) · 7.04 KB
/
Copy pathmemory.js
File metadata and controls
196 lines (168 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Simple CAD Memory System for v0
// Uses basic keyword matching and in-memory storage
class SimpleCADMemory {
constructor() {
// Simple in-memory storage (upgrade to persistent later)
this.designDecisions = [];
this.nextId = 1;
}
// Store a design decision
storeDecision(decision) {
const entry = {
id: this.nextId++,
timestamp: new Date().toISOString(),
documentId: decision.documentId,
userId: decision.userId || 'anonymous',
// User input
userIntent: decision.userIntent,
originalRequest: decision.originalRequest,
// AI reasoning
aiReasoning: decision.aiReasoning || '',
proposedChanges: decision.proposedChanges || {},
// Context used for this decision
memoryContext: decision.memoryContext || [],
// Outcome (updated later)
outcome: 'pending', // 'accepted', 'rejected', 'modified'
userFeedback: null
};
this.designDecisions.push(entry);
console.log(`💾 Stored memory entry ${entry.id}: "${entry.userIntent}"`);
return entry;
}
// Find similar past decisions using simple keyword matching
findSimilarDecisions(userIntent, documentId = null, limit = 3) {
const queryKeywords = this.extractKeywords(userIntent);
const candidates = documentId
? this.designDecisions.filter(d => d.documentId === documentId)
: this.designDecisions;
const scored = candidates.map(decision => {
const score = this.calculateSimilarity(queryKeywords, decision.userIntent);
return {
...decision,
similarityScore: score,
matchedKeywords: this.getMatchedKeywords(queryKeywords, decision.userIntent)
};
});
// Filter and sort by similarity
const relevant = scored
.filter(d => d.similarityScore > 0.2) // Basic threshold
.sort((a, b) => {
// Prioritize similarity, then successful outcomes, then recency
if (Math.abs(a.similarityScore - b.similarityScore) > 0.1) {
return b.similarityScore - a.similarityScore;
}
if (a.outcome !== b.outcome) {
if (a.outcome === 'accepted') return -1;
if (b.outcome === 'accepted') return 1;
}
return new Date(b.timestamp) - new Date(a.timestamp);
})
.slice(0, limit);
console.log(`🔍 Found ${relevant.length} similar decisions for: "${userIntent}"`);
return relevant;
}
// Update outcome when user accepts/rejects AI suggestion
updateOutcome(decisionId, outcome, feedback = null) {
const decision = this.designDecisions.find(d => d.id === decisionId);
if (decision) {
decision.outcome = outcome;
decision.userFeedback = feedback;
decision.resolvedAt = new Date().toISOString();
console.log(`✅ Updated decision ${decisionId} outcome: ${outcome}`);
return decision;
}
console.log(`❌ Decision ${decisionId} not found`);
return null;
}
// Get memory context for AI reasoning
getMemoryContext(userIntent, documentId) {
const similarDecisions = this.findSimilarDecisions(userIntent, documentId);
if (similarDecisions.length === 0) {
return {
hasContext: false,
message: "No similar past decisions found",
context: []
};
}
const context = similarDecisions.map(decision => ({
summary: this.summarizeDecision(decision),
outcome: decision.outcome,
reasoning: decision.aiReasoning,
similarity: decision.similarityScore,
matchedKeywords: decision.matchedKeywords
}));
return {
hasContext: true,
message: `Found ${context.length} similar past decisions`,
context: context
};
}
// Helper methods
extractKeywords(text) {
const stopWords = ['the', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'this', 'that', 'it', 'make', 'change'];
return text.toLowerCase()
.replace(/[^\w\s]/g, '') // Remove punctuation
.split(/\s+/)
.filter(word => word.length > 2 && !stopWords.includes(word));
}
calculateSimilarity(keywords1, text2) {
const keywords2 = this.extractKeywords(text2);
const intersection = keywords1.filter(word => keywords2.includes(word));
const union = [...new Set([...keywords1, ...keywords2])];
return union.length > 0 ? intersection.length / union.length : 0;
}
getMatchedKeywords(keywords1, text2) {
const keywords2 = this.extractKeywords(text2);
return keywords1.filter(word => keywords2.includes(word));
}
summarizeDecision(decision) {
const outcome = decision.outcome === 'accepted' ? '✅' :
decision.outcome === 'rejected' ? '❌' : '⏳';
return `${outcome} "${decision.userIntent}" (${this.timeAgo(decision.timestamp)})`;
}
timeAgo(timestamp) {
const now = new Date();
const past = new Date(timestamp);
const diffMs = now - past;
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMs / 3600000);
const diffDays = Math.floor(diffMs / 86400000);
if (diffMins < 1) return 'just now';
if (diffMins < 60) return `${diffMins}m ago`;
if (diffHours < 24) return `${diffHours}h ago`;
return `${diffDays}d ago`;
}
// Get stats for debugging
getStats() {
const total = this.designDecisions.length;
const accepted = this.designDecisions.filter(d => d.outcome === 'accepted').length;
const rejected = this.designDecisions.filter(d => d.outcome === 'rejected').length;
const pending = this.designDecisions.filter(d => d.outcome === 'pending').length;
return {
total,
accepted,
rejected,
pending,
successRate: total > 0 ? Math.round((accepted / total) * 100) : 0
};
}
// Export data (useful for upgrading to external service later)
exportData() {
return {
decisions: this.designDecisions,
stats: this.getStats(),
exportedAt: new Date().toISOString()
};
}
// Import data (useful when migrating)
importData(data) {
if (data.decisions && Array.isArray(data.decisions)) {
this.designDecisions = data.decisions;
this.nextId = Math.max(...this.designDecisions.map(d => d.id), 0) + 1;
console.log(`📥 Imported ${data.decisions.length} decisions`);
}
}
}
// Create singleton instance
const cadMemory = new SimpleCADMemory();
module.exports = cadMemory;