@nahisaho/musubix-core 1.0.9 → 1.0.13
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/dist/cli/commands/codegen.d.ts.map +1 -1
- package/dist/cli/commands/codegen.js +218 -1
- package/dist/cli/commands/codegen.js.map +1 -1
- package/dist/cli/commands/index.d.ts +1 -0
- package/dist/cli/commands/index.d.ts.map +1 -1
- package/dist/cli/commands/index.js +5 -0
- package/dist/cli/commands/index.js.map +1 -1
- package/dist/cli/commands/learn.d.ts +17 -0
- package/dist/cli/commands/learn.d.ts.map +1 -0
- package/dist/cli/commands/learn.js +285 -0
- package/dist/cli/commands/learn.js.map +1 -0
- package/dist/cli/commands/requirements.d.ts +1 -0
- package/dist/cli/commands/requirements.d.ts.map +1 -1
- package/dist/cli/commands/requirements.js +67 -16
- package/dist/cli/commands/requirements.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/learning/feedback-collector.d.ts +108 -0
- package/dist/learning/feedback-collector.d.ts.map +1 -0
- package/dist/learning/feedback-collector.js +231 -0
- package/dist/learning/feedback-collector.js.map +1 -0
- package/dist/learning/index.d.ts +19 -0
- package/dist/learning/index.d.ts.map +1 -0
- package/dist/learning/index.js +19 -0
- package/dist/learning/index.js.map +1 -0
- package/dist/learning/learning-engine.d.ts +140 -0
- package/dist/learning/learning-engine.d.ts.map +1 -0
- package/dist/learning/learning-engine.js +338 -0
- package/dist/learning/learning-engine.js.map +1 -0
- package/dist/learning/pattern-extractor.d.ts +138 -0
- package/dist/learning/pattern-extractor.d.ts.map +1 -0
- package/dist/learning/pattern-extractor.js +400 -0
- package/dist/learning/pattern-extractor.js.map +1 -0
- package/dist/learning/types.d.ts +159 -0
- package/dist/learning/types.d.ts.map +1 -0
- package/dist/learning/types.js +20 -0
- package/dist/learning/types.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Learning Engine
|
|
3
|
+
*
|
|
4
|
+
* Main orchestrator for the self-learning system
|
|
5
|
+
*
|
|
6
|
+
* @see REQ-LEARN-003 - Adaptive Reasoning
|
|
7
|
+
* @module @musubix/core/learning
|
|
8
|
+
*/
|
|
9
|
+
import { FeedbackCollector } from './feedback-collector.js';
|
|
10
|
+
import { PatternExtractor } from './pattern-extractor.js';
|
|
11
|
+
/**
|
|
12
|
+
* Learning Engine class
|
|
13
|
+
*
|
|
14
|
+
* Orchestrates feedback collection, pattern extraction, and adaptive reasoning
|
|
15
|
+
*
|
|
16
|
+
* @see REQ-LEARN-001 - Feedback Collection
|
|
17
|
+
* @see REQ-LEARN-002 - Pattern Extraction
|
|
18
|
+
* @see REQ-LEARN-003 - Adaptive Reasoning
|
|
19
|
+
*/
|
|
20
|
+
export class LearningEngine {
|
|
21
|
+
feedbackCollector;
|
|
22
|
+
patternExtractor;
|
|
23
|
+
config;
|
|
24
|
+
constructor(config = {}) {
|
|
25
|
+
this.config = {
|
|
26
|
+
patternThreshold: config.patternThreshold || 3,
|
|
27
|
+
decayDays: config.decayDays || 30,
|
|
28
|
+
decayRate: config.decayRate || 0.1,
|
|
29
|
+
minConfidence: config.minConfidence || 0.1,
|
|
30
|
+
storagePath: config.storagePath || 'storage/learning',
|
|
31
|
+
};
|
|
32
|
+
this.feedbackCollector = new FeedbackCollector(this.config);
|
|
33
|
+
this.patternExtractor = new PatternExtractor(this.config);
|
|
34
|
+
}
|
|
35
|
+
// ==========================================================================
|
|
36
|
+
// Feedback Management
|
|
37
|
+
// ==========================================================================
|
|
38
|
+
/**
|
|
39
|
+
* Record feedback for an artifact
|
|
40
|
+
*
|
|
41
|
+
* @see REQ-LEARN-001 - Feedback Collection
|
|
42
|
+
*/
|
|
43
|
+
async recordFeedback(input) {
|
|
44
|
+
const feedback = await this.feedbackCollector.recordFeedback(input);
|
|
45
|
+
// Trigger pattern extraction after recording
|
|
46
|
+
await this.tryExtractPatterns();
|
|
47
|
+
return feedback;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Record acceptance of an artifact
|
|
51
|
+
*/
|
|
52
|
+
async acceptArtifact(artifactId, artifactType, context, reason) {
|
|
53
|
+
return this.recordFeedback({
|
|
54
|
+
type: 'accept',
|
|
55
|
+
artifactType,
|
|
56
|
+
artifactId,
|
|
57
|
+
context,
|
|
58
|
+
reason,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Record rejection of an artifact
|
|
63
|
+
*/
|
|
64
|
+
async rejectArtifact(artifactId, artifactType, context, reason) {
|
|
65
|
+
return this.recordFeedback({
|
|
66
|
+
type: 'reject',
|
|
67
|
+
artifactType,
|
|
68
|
+
artifactId,
|
|
69
|
+
context,
|
|
70
|
+
reason,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Record modification of an artifact
|
|
75
|
+
*/
|
|
76
|
+
async modifyArtifact(artifactId, artifactType, original, modified, context, reason) {
|
|
77
|
+
return this.recordFeedback({
|
|
78
|
+
type: 'modify',
|
|
79
|
+
artifactType,
|
|
80
|
+
artifactId,
|
|
81
|
+
context,
|
|
82
|
+
reason,
|
|
83
|
+
original,
|
|
84
|
+
modified,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Query feedback
|
|
89
|
+
*/
|
|
90
|
+
async queryFeedback(query = {}) {
|
|
91
|
+
return this.feedbackCollector.queryFeedback(query);
|
|
92
|
+
}
|
|
93
|
+
// ==========================================================================
|
|
94
|
+
// Pattern Management
|
|
95
|
+
// ==========================================================================
|
|
96
|
+
/**
|
|
97
|
+
* Get all learned patterns
|
|
98
|
+
*/
|
|
99
|
+
async getPatterns() {
|
|
100
|
+
return this.patternExtractor.getPatterns();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get patterns by category
|
|
104
|
+
*/
|
|
105
|
+
async getPatternsByCategory(category) {
|
|
106
|
+
return this.patternExtractor.getPatternsByCategory(category);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Add a manual pattern
|
|
110
|
+
*/
|
|
111
|
+
async addPattern(name, category, trigger, action, confidence = 0.5) {
|
|
112
|
+
return this.patternExtractor.addPattern(name, category, trigger, action, confidence);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Remove a pattern
|
|
116
|
+
*/
|
|
117
|
+
async removePattern(id) {
|
|
118
|
+
return this.patternExtractor.removePattern(id);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Apply decay to unused patterns
|
|
122
|
+
*
|
|
123
|
+
* @see REQ-LEARN-004 - Pattern Decay
|
|
124
|
+
*/
|
|
125
|
+
async applyDecay() {
|
|
126
|
+
return this.patternExtractor.applyDecay(this.config.decayDays, this.config.decayRate, this.config.minConfidence);
|
|
127
|
+
}
|
|
128
|
+
// ==========================================================================
|
|
129
|
+
// Adaptive Reasoning
|
|
130
|
+
// ==========================================================================
|
|
131
|
+
/**
|
|
132
|
+
* Find matching patterns for inference context
|
|
133
|
+
*
|
|
134
|
+
* @see REQ-LEARN-003 - Adaptive Reasoning
|
|
135
|
+
* @param context - Current inference context
|
|
136
|
+
* @returns Matching patterns sorted by relevance
|
|
137
|
+
*/
|
|
138
|
+
async findMatchingPatterns(context) {
|
|
139
|
+
const patterns = await this.patternExtractor.getPatterns();
|
|
140
|
+
const matches = [];
|
|
141
|
+
for (const pattern of patterns) {
|
|
142
|
+
// Check category match
|
|
143
|
+
if (pattern.category !== context.artifactType)
|
|
144
|
+
continue;
|
|
145
|
+
// Calculate match score
|
|
146
|
+
const { score, matchedKeys } = this.calculateMatchScore(pattern, context);
|
|
147
|
+
if (score > 0) {
|
|
148
|
+
matches.push({
|
|
149
|
+
pattern,
|
|
150
|
+
matchScore: score * pattern.confidence,
|
|
151
|
+
matchedKeys,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Sort by combined score (match score * confidence)
|
|
156
|
+
matches.sort((a, b) => b.matchScore - a.matchScore);
|
|
157
|
+
return matches;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get recommendations for current context
|
|
161
|
+
*
|
|
162
|
+
* @see REQ-LEARN-003 - Adaptive Reasoning
|
|
163
|
+
*/
|
|
164
|
+
async getRecommendations(context, limit = 5) {
|
|
165
|
+
const matches = await this.findMatchingPatterns(context);
|
|
166
|
+
const recommendations = {
|
|
167
|
+
prefer: [],
|
|
168
|
+
avoid: [],
|
|
169
|
+
suggest: [],
|
|
170
|
+
};
|
|
171
|
+
for (const match of matches.slice(0, limit * 3)) {
|
|
172
|
+
const actionType = match.pattern.action.type;
|
|
173
|
+
if (recommendations[actionType].length < limit) {
|
|
174
|
+
recommendations[actionType].push(match.pattern);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return recommendations;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Apply pattern to adjust inference
|
|
181
|
+
*/
|
|
182
|
+
async applyPattern(patternId, success) {
|
|
183
|
+
return this.patternExtractor.updatePatternUsage(patternId, success);
|
|
184
|
+
}
|
|
185
|
+
// ==========================================================================
|
|
186
|
+
// Statistics & Export
|
|
187
|
+
// ==========================================================================
|
|
188
|
+
/**
|
|
189
|
+
* Get learning statistics
|
|
190
|
+
*
|
|
191
|
+
* @see REQ-LEARN-005 - Learning Visualization
|
|
192
|
+
*/
|
|
193
|
+
async getStats() {
|
|
194
|
+
const feedbackStats = await this.feedbackCollector.getStats();
|
|
195
|
+
const patterns = await this.patternExtractor.getPatterns();
|
|
196
|
+
const patternsByCategory = {
|
|
197
|
+
code: 0,
|
|
198
|
+
design: 0,
|
|
199
|
+
requirement: 0,
|
|
200
|
+
test: 0,
|
|
201
|
+
};
|
|
202
|
+
let totalConfidence = 0;
|
|
203
|
+
for (const pattern of patterns) {
|
|
204
|
+
patternsByCategory[pattern.category]++;
|
|
205
|
+
totalConfidence += pattern.confidence;
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
totalFeedback: feedbackStats.total,
|
|
209
|
+
feedbackByType: feedbackStats.byType,
|
|
210
|
+
totalPatterns: patterns.length,
|
|
211
|
+
patternsByCategory,
|
|
212
|
+
averageConfidence: patterns.length > 0 ? totalConfidence / patterns.length : 0,
|
|
213
|
+
lastUpdate: new Date(),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Export all learning data
|
|
218
|
+
*/
|
|
219
|
+
async export() {
|
|
220
|
+
return {
|
|
221
|
+
feedback: await this.feedbackCollector.export(),
|
|
222
|
+
patterns: await this.patternExtractor.export(),
|
|
223
|
+
stats: await this.getStats(),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Import learning data
|
|
228
|
+
*/
|
|
229
|
+
async import(data) {
|
|
230
|
+
let feedbackImported = 0;
|
|
231
|
+
let patternsImported = 0;
|
|
232
|
+
if (data.feedback) {
|
|
233
|
+
feedbackImported = await this.feedbackCollector.import(data.feedback);
|
|
234
|
+
}
|
|
235
|
+
if (data.patterns) {
|
|
236
|
+
patternsImported = await this.patternExtractor.import(data.patterns);
|
|
237
|
+
}
|
|
238
|
+
return { feedbackImported, patternsImported };
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Generate status report
|
|
242
|
+
*/
|
|
243
|
+
async generateStatusReport() {
|
|
244
|
+
const stats = await this.getStats();
|
|
245
|
+
const highConfPatterns = await this.patternExtractor.getHighConfidencePatterns(0.7);
|
|
246
|
+
const lines = [
|
|
247
|
+
'# MUSUBIX Learning Status Report',
|
|
248
|
+
'',
|
|
249
|
+
`**Generated**: ${new Date().toISOString()}`,
|
|
250
|
+
'',
|
|
251
|
+
'## Summary',
|
|
252
|
+
'',
|
|
253
|
+
`- Total Feedback: ${stats.totalFeedback}`,
|
|
254
|
+
`- Total Patterns: ${stats.totalPatterns}`,
|
|
255
|
+
`- Average Confidence: ${(stats.averageConfidence * 100).toFixed(1)}%`,
|
|
256
|
+
'',
|
|
257
|
+
'## Feedback by Type',
|
|
258
|
+
'',
|
|
259
|
+
`- Accept: ${stats.feedbackByType.accept}`,
|
|
260
|
+
`- Reject: ${stats.feedbackByType.reject}`,
|
|
261
|
+
`- Modify: ${stats.feedbackByType.modify}`,
|
|
262
|
+
'',
|
|
263
|
+
'## Patterns by Category',
|
|
264
|
+
'',
|
|
265
|
+
`- Code: ${stats.patternsByCategory.code}`,
|
|
266
|
+
`- Design: ${stats.patternsByCategory.design}`,
|
|
267
|
+
`- Requirement: ${stats.patternsByCategory.requirement}`,
|
|
268
|
+
`- Test: ${stats.patternsByCategory.test}`,
|
|
269
|
+
'',
|
|
270
|
+
'## High Confidence Patterns (≥70%)',
|
|
271
|
+
'',
|
|
272
|
+
];
|
|
273
|
+
if (highConfPatterns.length === 0) {
|
|
274
|
+
lines.push('_No high confidence patterns yet._');
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
for (const pattern of highConfPatterns) {
|
|
278
|
+
lines.push(`### ${pattern.name}`);
|
|
279
|
+
lines.push('');
|
|
280
|
+
lines.push(`- **ID**: ${pattern.id}`);
|
|
281
|
+
lines.push(`- **Category**: ${pattern.category}`);
|
|
282
|
+
lines.push(`- **Action**: ${pattern.action.type}`);
|
|
283
|
+
lines.push(`- **Confidence**: ${(pattern.confidence * 100).toFixed(1)}%`);
|
|
284
|
+
lines.push(`- **Occurrences**: ${pattern.occurrences}`);
|
|
285
|
+
lines.push('');
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return lines.join('\n');
|
|
289
|
+
}
|
|
290
|
+
// ==========================================================================
|
|
291
|
+
// Private Methods
|
|
292
|
+
// ==========================================================================
|
|
293
|
+
/**
|
|
294
|
+
* Calculate match score between pattern and context
|
|
295
|
+
*/
|
|
296
|
+
calculateMatchScore(pattern, context) {
|
|
297
|
+
const matchedKeys = [];
|
|
298
|
+
let matchCount = 0;
|
|
299
|
+
let totalKeys = 0;
|
|
300
|
+
const triggerContext = pattern.trigger.context;
|
|
301
|
+
// Check language match
|
|
302
|
+
if (triggerContext.language) {
|
|
303
|
+
totalKeys++;
|
|
304
|
+
if (context.language === triggerContext.language) {
|
|
305
|
+
matchCount++;
|
|
306
|
+
matchedKeys.push('language');
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
// Check framework match
|
|
310
|
+
if (triggerContext.framework) {
|
|
311
|
+
totalKeys++;
|
|
312
|
+
if (context.framework === triggerContext.framework) {
|
|
313
|
+
matchCount++;
|
|
314
|
+
matchedKeys.push('framework');
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// If no specific context required, it's a general match
|
|
318
|
+
if (totalKeys === 0) {
|
|
319
|
+
return { score: 0.5, matchedKeys: ['general'] };
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
score: matchCount / totalKeys,
|
|
323
|
+
matchedKeys,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Try to extract patterns from recent feedback
|
|
328
|
+
*/
|
|
329
|
+
async tryExtractPatterns() {
|
|
330
|
+
const recentFeedback = await this.feedbackCollector.queryFeedback({
|
|
331
|
+
limit: 100,
|
|
332
|
+
});
|
|
333
|
+
if (recentFeedback.length >= this.config.patternThreshold) {
|
|
334
|
+
await this.patternExtractor.extractPatterns(recentFeedback);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
//# sourceMappingURL=learning-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"learning-engine.js","sourceRoot":"","sources":["../../src/learning/learning-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,iBAAiB,EAA0C,MAAM,yBAAyB,CAAC;AACp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AE,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC;QAClD,CAAC;QAED,OAAO;YACL,KAAK,EAAE,UAAU,GAAG,SAAS;YAC7B,WAAW;SACZ,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,kBAAkB;QAC9B,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC;YAChE,KAAK,EAAE,GAAG;SACX,CAAC,CAAC;QAEH,IAAI,cAAc,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,CAAC;YAC1D,MAAM,IAAI,CAAC,gBAAgB,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pattern Extractor
|
|
3
|
+
*
|
|
4
|
+
* Extracts patterns from accumulated feedback
|
|
5
|
+
*
|
|
6
|
+
* @see REQ-LEARN-002 - Pattern Extraction
|
|
7
|
+
* @module @musubix/core/learning
|
|
8
|
+
*/
|
|
9
|
+
import type { Feedback, LearnedPattern, PatternCategory, PatternTrigger, PatternAction, LearningConfig } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Pattern Extractor class
|
|
12
|
+
*
|
|
13
|
+
* Analyzes feedback to extract recurring patterns
|
|
14
|
+
*
|
|
15
|
+
* @see REQ-LEARN-002 - Pattern Extraction
|
|
16
|
+
*/
|
|
17
|
+
export declare class PatternExtractor {
|
|
18
|
+
private patternCache;
|
|
19
|
+
private storagePath;
|
|
20
|
+
private threshold;
|
|
21
|
+
private loaded;
|
|
22
|
+
constructor(config?: Partial<LearningConfig>);
|
|
23
|
+
/**
|
|
24
|
+
* Extract patterns from feedback list
|
|
25
|
+
*
|
|
26
|
+
* @see REQ-LEARN-002 - Pattern Extraction
|
|
27
|
+
* @param feedbackList - List of feedback to analyze
|
|
28
|
+
* @returns Newly created patterns
|
|
29
|
+
*/
|
|
30
|
+
extractPatterns(feedbackList: Feedback[]): Promise<LearnedPattern[]>;
|
|
31
|
+
/**
|
|
32
|
+
* Get all patterns
|
|
33
|
+
*/
|
|
34
|
+
getPatterns(): Promise<LearnedPattern[]>;
|
|
35
|
+
/**
|
|
36
|
+
* Get pattern by ID
|
|
37
|
+
*/
|
|
38
|
+
getPattern(id: string): Promise<LearnedPattern | undefined>;
|
|
39
|
+
/**
|
|
40
|
+
* Add pattern manually
|
|
41
|
+
*
|
|
42
|
+
* @param name - Pattern name
|
|
43
|
+
* @param category - Pattern category
|
|
44
|
+
* @param trigger - Trigger conditions
|
|
45
|
+
* @param action - Action to take
|
|
46
|
+
* @param confidence - Initial confidence (default: 0.5)
|
|
47
|
+
*/
|
|
48
|
+
addPattern(name: string, category: PatternCategory, trigger: PatternTrigger, action: PatternAction, confidence?: number): Promise<LearnedPattern>;
|
|
49
|
+
/**
|
|
50
|
+
* Remove pattern by ID
|
|
51
|
+
*/
|
|
52
|
+
removePattern(id: string): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Update pattern confidence based on usage
|
|
55
|
+
*/
|
|
56
|
+
updatePatternUsage(id: string, success: boolean): Promise<LearnedPattern | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* Apply decay to unused patterns
|
|
59
|
+
*
|
|
60
|
+
* @see REQ-LEARN-004 - Pattern Decay
|
|
61
|
+
* @param days - Days threshold for decay
|
|
62
|
+
* @param rate - Decay rate
|
|
63
|
+
* @param minConfidence - Minimum confidence to keep
|
|
64
|
+
*/
|
|
65
|
+
applyDecay(days?: number, rate?: number, minConfidence?: number): Promise<{
|
|
66
|
+
decayed: number;
|
|
67
|
+
archived: number;
|
|
68
|
+
}>;
|
|
69
|
+
/**
|
|
70
|
+
* Get patterns by category
|
|
71
|
+
*/
|
|
72
|
+
getPatternsByCategory(category: PatternCategory): Promise<LearnedPattern[]>;
|
|
73
|
+
/**
|
|
74
|
+
* Get high confidence patterns
|
|
75
|
+
*/
|
|
76
|
+
getHighConfidencePatterns(threshold?: number): Promise<LearnedPattern[]>;
|
|
77
|
+
/**
|
|
78
|
+
* Export patterns
|
|
79
|
+
*/
|
|
80
|
+
export(): Promise<LearnedPattern[]>;
|
|
81
|
+
/**
|
|
82
|
+
* Import patterns
|
|
83
|
+
*/
|
|
84
|
+
import(patterns: LearnedPattern[]): Promise<number>;
|
|
85
|
+
/**
|
|
86
|
+
* Find pattern candidates from feedback
|
|
87
|
+
*/
|
|
88
|
+
private findCandidates;
|
|
89
|
+
/**
|
|
90
|
+
* Generate a key for grouping similar feedback
|
|
91
|
+
*/
|
|
92
|
+
private generateCandidateKey;
|
|
93
|
+
/**
|
|
94
|
+
* Extract context from feedback
|
|
95
|
+
*/
|
|
96
|
+
private extractContext;
|
|
97
|
+
/**
|
|
98
|
+
* Map feedback type to pattern action
|
|
99
|
+
*/
|
|
100
|
+
private mapFeedbackToAction;
|
|
101
|
+
/**
|
|
102
|
+
* Extract content for pattern
|
|
103
|
+
*/
|
|
104
|
+
private extractContent;
|
|
105
|
+
/**
|
|
106
|
+
* Find similar existing pattern
|
|
107
|
+
*/
|
|
108
|
+
private findSimilarPattern;
|
|
109
|
+
/**
|
|
110
|
+
* Check if contexts match
|
|
111
|
+
*/
|
|
112
|
+
private contextMatches;
|
|
113
|
+
/**
|
|
114
|
+
* Create pattern from candidate
|
|
115
|
+
*/
|
|
116
|
+
private createPattern;
|
|
117
|
+
/**
|
|
118
|
+
* Calculate initial confidence for new pattern
|
|
119
|
+
*/
|
|
120
|
+
private calculateInitialConfidence;
|
|
121
|
+
/**
|
|
122
|
+
* Calculate confidence for existing pattern
|
|
123
|
+
*/
|
|
124
|
+
private calculateConfidence;
|
|
125
|
+
/**
|
|
126
|
+
* Archive pattern to separate file
|
|
127
|
+
*/
|
|
128
|
+
private archivePattern;
|
|
129
|
+
/**
|
|
130
|
+
* Ensure data is loaded from storage
|
|
131
|
+
*/
|
|
132
|
+
private ensureLoaded;
|
|
133
|
+
/**
|
|
134
|
+
* Persist data to storage
|
|
135
|
+
*/
|
|
136
|
+
private persist;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=pattern-extractor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pattern-extractor.d.ts","sourceRoot":"","sources":["../../src/learning/pattern-extractor.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EACV,QAAQ,EACR,cAAc,EACd,eAAe,EAEf,cAAc,EACd,aAAa,EACb,cAAc,EACf,MAAM,YAAY,CAAC;AAepB;;;;;;GAMG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,YAAY,CAA0C;IAC9D,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAkB;gBAEpB,MAAM,GAAE,OAAO,CAAC,cAAc,CAAM;IAKhD;;;;;;OAMG;IACG,eAAe,CAAC,YAAY,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IA6B1E;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAK9C;;OAEG;IACG,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;IAKjE;;;;;;;;OAQG;IACG,UAAU,CACd,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,eAAe,EACzB,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,aAAa,EACrB,UAAU,GAAE,MAAY,GACvB,OAAO,CAAC,cAAc,CAAC;IAsB1B;;OAEG;IACG,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IASjD;;OAEG;IACG,kBAAkB,CACtB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;IAiBtC;;;;;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