mark-improving-agent 2.2.5 → 2.2.6
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/VERSION +1 -1
- package/dist/core/expert-models/index.js +596 -0
- package/dist/index.js +1 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.2.
|
|
1
|
+
2.2.6
|
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expert Mental Models Integration
|
|
3
|
+
*
|
|
4
|
+
* Loads expert mental models into HeartFlow to improve decisions,
|
|
5
|
+
* reasoning, and judgment without role-playing personas.
|
|
6
|
+
*
|
|
7
|
+
* Based on the expert-skills pattern: mental models provide
|
|
8
|
+
* structured thinking frameworks that enhance AI reasoning.
|
|
9
|
+
*
|
|
10
|
+
* Key features:
|
|
11
|
+
* - Mental model registry with predefined expert thinking patterns
|
|
12
|
+
* - Model application to any context or decision
|
|
13
|
+
* - Model blending for multi-perspective analysis
|
|
14
|
+
* - Learning from model application outcomes
|
|
15
|
+
* - Model performance tracking
|
|
16
|
+
*
|
|
17
|
+
* @module core/expert-models
|
|
18
|
+
* @fileoverview Expert mental model integration for enhanced reasoning
|
|
19
|
+
*/
|
|
20
|
+
import { randomUUID } from 'crypto';
|
|
21
|
+
import { createLogger } from '../../utils/logger.js';
|
|
22
|
+
const logger = createLogger('[ExpertModels]');
|
|
23
|
+
const DEFAULT_CONFIG = {
|
|
24
|
+
autoSuggest: true,
|
|
25
|
+
minConfidence: 0.6,
|
|
26
|
+
trackPerformance: true,
|
|
27
|
+
allowBlending: true,
|
|
28
|
+
defaultBlendWeights: [0.5, 0.3, 0.2],
|
|
29
|
+
};
|
|
30
|
+
// ============================================================
|
|
31
|
+
// Predefined Mental Models
|
|
32
|
+
// ============================================================
|
|
33
|
+
const PREDEFINED_MODELS = [
|
|
34
|
+
{
|
|
35
|
+
name: 'First Principles Thinking',
|
|
36
|
+
description: 'Break down problems to their fundamental components and rebuild from there',
|
|
37
|
+
source: 'Aristotle / Elon Musk',
|
|
38
|
+
principles: [
|
|
39
|
+
'Identify the current assumption',
|
|
40
|
+
'Break it down to fundamental truths',
|
|
41
|
+
'Create new solutions from scratch',
|
|
42
|
+
'Test and iterate',
|
|
43
|
+
],
|
|
44
|
+
applicationSteps: [
|
|
45
|
+
'State the problem as commonly understood',
|
|
46
|
+
'Ask "Is this really true?" for each component',
|
|
47
|
+
'Break down to facts that cannot be reduced further',
|
|
48
|
+
'Build new reasoning from these foundations',
|
|
49
|
+
'Test the new solution',
|
|
50
|
+
],
|
|
51
|
+
适用场景: ['Innovation', 'Problem-solving', 'Challenging status quo'],
|
|
52
|
+
strengths: ['Eliminates assumptions', 'Enables breakthrough thinking', 'Reduces bias'],
|
|
53
|
+
weaknesses: ['Time-consuming', 'May miss practical constraints', 'Requires deep expertise'],
|
|
54
|
+
examples: ['SpaceX rocket costs', 'Tesla battery technology', 'Business model innovation'],
|
|
55
|
+
complexity: 4,
|
|
56
|
+
category: 'critical-thinking',
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'Inversion',
|
|
60
|
+
description: 'Think about the problem backwards to identify what to avoid',
|
|
61
|
+
source: 'Charlie Munger',
|
|
62
|
+
principles: [
|
|
63
|
+
'Identify what you want to achieve',
|
|
64
|
+
'Instead of asking how to succeed, ask how to fail',
|
|
65
|
+
'Avoid those failure paths',
|
|
66
|
+
'Focus on the inverse of problems',
|
|
67
|
+
],
|
|
68
|
+
applicationSteps: [
|
|
69
|
+
'Clearly state the goal',
|
|
70
|
+
'List ways the goal could fail',
|
|
71
|
+
'Identify the root causes of potential failure',
|
|
72
|
+
'Create actions to prevent those failures',
|
|
73
|
+
'Prioritize avoiding bad outcomes',
|
|
74
|
+
],
|
|
75
|
+
适用场景: ['Risk management', 'Strategic planning', 'Decision validation'],
|
|
76
|
+
strengths: ['Reveals hidden risks', 'Prevents hubris', 'Highlights what to avoid'],
|
|
77
|
+
weaknesses: ['Can be overly pessimistic', 'May miss opportunities', 'Focuses on negatives'],
|
|
78
|
+
examples: ['Avoiding stupid decisions', 'Risk assessment', 'Portfolio management'],
|
|
79
|
+
complexity: 3,
|
|
80
|
+
category: 'decision-making',
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: 'Second Order Thinking',
|
|
84
|
+
description: 'Consider the consequences of the consequences',
|
|
85
|
+
source: 'Howard Marks / Ray Dalio',
|
|
86
|
+
principles: [
|
|
87
|
+
'First order: immediate result',
|
|
88
|
+
'Second order: what follows from the first result',
|
|
89
|
+
'Third order: long-term effects',
|
|
90
|
+
'Most people stop at first order',
|
|
91
|
+
],
|
|
92
|
+
applicationSteps: [
|
|
93
|
+
'Identify the obvious first-order consequence',
|
|
94
|
+
'Ask "And then?" for each subsequent effect',
|
|
95
|
+
'Map out 2-3 levels of consequences',
|
|
96
|
+
'Evaluate the full chain of events',
|
|
97
|
+
'Make decision based on second/third order effects',
|
|
98
|
+
],
|
|
99
|
+
适用场景: ['Impact analysis', 'Long-term planning', 'Policy decisions'],
|
|
100
|
+
strengths: ['Prevents short-term thinking', 'Reveals hidden consequences', 'Improves foresight'],
|
|
101
|
+
weaknesses: ['Can lead to analysis paralysis', 'Hard to predict accurately', 'Uncertainty increases'],
|
|
102
|
+
examples: ['Economic policy', 'Technology adoption', 'Environmental decisions'],
|
|
103
|
+
complexity: 4,
|
|
104
|
+
category: 'systems-thinking',
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: 'Circle of Competence',
|
|
108
|
+
description: 'Know your boundaries and stay within them',
|
|
109
|
+
source: 'Warren Buffett',
|
|
110
|
+
principles: [
|
|
111
|
+
'Recognize what you truly understand',
|
|
112
|
+
'Identify areas where you have expertise',
|
|
113
|
+
'Be honest about ignorance',
|
|
114
|
+
'Stay within known boundaries',
|
|
115
|
+
'Expand boundaries deliberately',
|
|
116
|
+
],
|
|
117
|
+
applicationSteps: [
|
|
118
|
+
'List areas of genuine expertise',
|
|
119
|
+
'Identify knowledge gaps honestly',
|
|
120
|
+
'When facing decisions, check if in circle',
|
|
121
|
+
'If outside, seek expert input or abstain',
|
|
122
|
+
'Gradually expand circle through learning',
|
|
123
|
+
],
|
|
124
|
+
适用场景: ['Investment decisions', 'Expert consultation', 'Self-awareness'],
|
|
125
|
+
strengths: ['Prevents overconfidence', 'Encourages humility', 'Focuses resources'],
|
|
126
|
+
weaknesses: ['Can be limiting', 'Circle boundaries unclear', 'May miss opportunities'],
|
|
127
|
+
examples: ['Buffett investment strategy', 'Professional specialization', 'Team building'],
|
|
128
|
+
complexity: 2,
|
|
129
|
+
category: 'self-awareness',
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'Probabilistic Thinking',
|
|
133
|
+
description: 'Think in probabilities rather than certainties',
|
|
134
|
+
source: 'Nassim Taleb / Nate Silver',
|
|
135
|
+
principles: [
|
|
136
|
+
'Assign probabilities to outcomes',
|
|
137
|
+
'Update beliefs with new evidence (Bayesian)',
|
|
138
|
+
'Consider base rates',
|
|
139
|
+
'Think in expected value',
|
|
140
|
+
'Embrace uncertainty',
|
|
141
|
+
],
|
|
142
|
+
applicationSteps: [
|
|
143
|
+
'List possible outcomes',
|
|
144
|
+
'Estimate probability of each',
|
|
145
|
+
'Calculate expected value (P × outcome)',
|
|
146
|
+
'Update with new information',
|
|
147
|
+
'Choose highest expected value option',
|
|
148
|
+
],
|
|
149
|
+
适用场景: ['Risk assessment', 'Forecasting', 'Decision under uncertainty'],
|
|
150
|
+
strengths: ['Accounts for uncertainty', 'Prevents binary thinking', 'Quantifies risk'],
|
|
151
|
+
weaknesses: ['Requires accurate probability estimates', 'Can be gamed', 'Base rate neglect'],
|
|
152
|
+
examples: ['Weather forecasting', 'Sports betting', 'Project estimation'],
|
|
153
|
+
complexity: 4,
|
|
154
|
+
category: 'analytical',
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: 'Regret Minimization',
|
|
158
|
+
description: 'Minimize future regret rather than optimize present comfort',
|
|
159
|
+
source: 'Jeff Bezos',
|
|
160
|
+
principles: [
|
|
161
|
+
'Project yourself to end of life',
|
|
162
|
+
'Ask what you would regret not doing',
|
|
163
|
+
'Prioritize bold moves for low regret',
|
|
164
|
+
'Accept that regret is inevitable',
|
|
165
|
+
'Take the path with least potential regret',
|
|
166
|
+
],
|
|
167
|
+
applicationSteps: [
|
|
168
|
+
'Imagine yourself at 80 years old',
|
|
169
|
+
'Look back at this decision point',
|
|
170
|
+
'Ask: "Will I regret not trying?"',
|
|
171
|
+
'Weight long-term regret over short-term comfort',
|
|
172
|
+
'Take the bolder action if regret is likely',
|
|
173
|
+
],
|
|
174
|
+
适用场景: ['Career decisions', 'Major life choices', 'Entrepreneurship'],
|
|
175
|
+
strengths: ['Encourages boldness', 'Long-term perspective', 'Overcomes fear'],
|
|
176
|
+
weaknesses: ['Can justify recklessness', 'Ignores practical constraints', 'Emotional basis'],
|
|
177
|
+
examples: ['Bezos Amazon decision', 'Career pivots', 'Startup founding'],
|
|
178
|
+
complexity: 2,
|
|
179
|
+
category: 'decision-making',
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "Occam's Razor",
|
|
183
|
+
description: 'The simplest explanation is usually correct',
|
|
184
|
+
source: 'William of Ockham',
|
|
185
|
+
principles: [
|
|
186
|
+
'Prefer simplicity over complexity',
|
|
187
|
+
'When multiple explanations exist, choose simplest',
|
|
188
|
+
'Entities should not be multiplied unnecessarily',
|
|
189
|
+
'Test simple explanations first',
|
|
190
|
+
'Complexity requires justification',
|
|
191
|
+
],
|
|
192
|
+
applicationSteps: [
|
|
193
|
+
'Gather all possible explanations',
|
|
194
|
+
'Evaluate complexity of each',
|
|
195
|
+
'Remove unnecessary assumptions',
|
|
196
|
+
'Select the simplest that fits facts',
|
|
197
|
+
'Only add complexity if evidence demands it',
|
|
198
|
+
],
|
|
199
|
+
适用场景: ['Diagnosis', 'Problem diagnosis', 'Theory building'],
|
|
200
|
+
strengths: ['Parsimonious', 'Practical', 'Avoids overfitting'],
|
|
201
|
+
weaknesses: ['Truth may be complex', 'Can oversimplify', 'Bias toward familiar'],
|
|
202
|
+
examples: ['Medical diagnosis', 'Troubleshooting', 'Scientific hypothesis'],
|
|
203
|
+
complexity: 2,
|
|
204
|
+
category: 'analytical',
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
name: "Hanlon's Razor",
|
|
208
|
+
description: 'Never attribute to malice that which is adequately explained by incompetence',
|
|
209
|
+
source: 'Robert Hanlon',
|
|
210
|
+
principles: [
|
|
211
|
+
'Assume incompetence before malice',
|
|
212
|
+
'Look for simple explanations first',
|
|
213
|
+
'Consider communication failures',
|
|
214
|
+
'Avoid unnecessary negative interpretations',
|
|
215
|
+
'Give benefit of the doubt',
|
|
216
|
+
],
|
|
217
|
+
applicationSteps: [
|
|
218
|
+
'Observe an action that seems harmful',
|
|
219
|
+
'Ask: is there a simple explanation?',
|
|
220
|
+
'Consider information gaps or confusion',
|
|
221
|
+
'Only assume malice if no other explanation fits',
|
|
222
|
+
'Address the incompetence, not the person',
|
|
223
|
+
],
|
|
224
|
+
适用场景: ['Conflict resolution', 'Team dynamics', 'Communication'],
|
|
225
|
+
strengths: ['Preserves relationships', 'Avoids unnecessary conflict', 'Practical'],
|
|
226
|
+
weaknesses: ['May miss actual bad actors', 'Can excuse abuse', 'Naive in some contexts'],
|
|
227
|
+
examples: ['Office politics', 'Customer complaints', 'Cross-cultural communication'],
|
|
228
|
+
complexity: 2,
|
|
229
|
+
category: 'ethical',
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
name: 'Map and Territory',
|
|
233
|
+
description: 'Distinguish between your model of reality and reality itself',
|
|
234
|
+
source: 'Nassim Taleb',
|
|
235
|
+
principles: [
|
|
236
|
+
'Your mental map is not the territory',
|
|
237
|
+
'Models are simplifications of reality',
|
|
238
|
+
'Reality is always more complex',
|
|
239
|
+
'Update maps when territory disagrees',
|
|
240
|
+
'Be humble about model accuracy',
|
|
241
|
+
],
|
|
242
|
+
applicationSteps: [
|
|
243
|
+
'Identify your current model/belief',
|
|
244
|
+
'Acknowledge it is a simplification',
|
|
245
|
+
'Compare to actual outcomes/evidence',
|
|
246
|
+
'Note where model diverges from reality',
|
|
247
|
+
'Update model to better fit territory',
|
|
248
|
+
],
|
|
249
|
+
适用场景: ['Belief revision', 'Forecast evaluation', 'Self-awareness'],
|
|
250
|
+
strengths: ['Promotes epistemic humility', 'Encourages testing', 'Reduces overconfidence'],
|
|
251
|
+
weaknesses: ['Can lead to perpetual doubt', 'Hard to know true territory', 'May paralyze'],
|
|
252
|
+
examples: ['Economic forecasts', 'Expert predictions', 'Personal beliefs'],
|
|
253
|
+
complexity: 3,
|
|
254
|
+
category: 'critical-thinking',
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
name: 'Thought Experiment',
|
|
258
|
+
description: 'Use imaginative scenarios to test ideas and assumptions',
|
|
259
|
+
source: 'Einstein / Philosophy tradition',
|
|
260
|
+
principles: [
|
|
261
|
+
'Construct hypothetical scenarios',
|
|
262
|
+
'Follow logic through to implications',
|
|
263
|
+
'Isolate key variables',
|
|
264
|
+
'Use imagination to explore possibilities',
|
|
265
|
+
'Derive insights from deduction',
|
|
266
|
+
],
|
|
267
|
+
applicationSteps: [
|
|
268
|
+
'Identify the principle or rule to test',
|
|
269
|
+
'Construct a hypothetical scenario',
|
|
270
|
+
'Apply the principle to this scenario',
|
|
271
|
+
'Follow implications logically',
|
|
272
|
+
'Extract insights about the principle',
|
|
273
|
+
],
|
|
274
|
+
适用场景: ['Philosophy', 'Physics', 'Ethical reasoning', 'Strategy'],
|
|
275
|
+
strengths: ['No real-world consequences', 'Tests logic', 'Reveals assumptions'],
|
|
276
|
+
weaknesses: ['May not transfer to reality', 'Imagination limits', 'Logical errors possible'],
|
|
277
|
+
examples: ["Einstein's elevator", 'Trolley problem', 'Veil of ignorance'],
|
|
278
|
+
complexity: 3,
|
|
279
|
+
category: 'creative',
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
name: 'STS (Systems Thinking Synthesis)',
|
|
283
|
+
description: 'See the whole system, not just parts - feedback loops, emergence, delays',
|
|
284
|
+
source: 'Peter Senge / Donella Meadows',
|
|
285
|
+
principles: [
|
|
286
|
+
'See interconnections, not just components',
|
|
287
|
+
'Identify feedback loops (reinforcing/balancing)',
|
|
288
|
+
'Notice delays between action and effect',
|
|
289
|
+
'Look for emergent properties',
|
|
290
|
+
'Find leverage points in the system',
|
|
291
|
+
],
|
|
292
|
+
applicationSteps: [
|
|
293
|
+
'Map the system components',
|
|
294
|
+
'Identify relationships and connections',
|
|
295
|
+
'Find feedback loops',
|
|
296
|
+
'Notice delays and accumulations',
|
|
297
|
+
'Identify high-leverage intervention points',
|
|
298
|
+
],
|
|
299
|
+
适用场景: ['Organizational change', 'Policy design', 'Complex problem diagnosis'],
|
|
300
|
+
strengths: ['Holistic view', 'Reveals hidden dynamics', 'Identifies leverage'],
|
|
301
|
+
weaknesses: ['Complex', 'Hard to model accurately', 'Delays hard to predict'],
|
|
302
|
+
examples: ['Climate policy', 'Company culture', 'Market dynamics'],
|
|
303
|
+
complexity: 5,
|
|
304
|
+
category: 'systems-thinking',
|
|
305
|
+
},
|
|
306
|
+
];
|
|
307
|
+
/**
|
|
308
|
+
* Create an Expert Models Engine
|
|
309
|
+
*/
|
|
310
|
+
export function createExpertModelsEngine(config = {}) {
|
|
311
|
+
const cfg = { ...DEFAULT_CONFIG, ...config };
|
|
312
|
+
// Model registry
|
|
313
|
+
const models = new Map();
|
|
314
|
+
// Application history
|
|
315
|
+
const applicationHistory = new Map();
|
|
316
|
+
// Model blends
|
|
317
|
+
const blends = new Map();
|
|
318
|
+
// Initialize with predefined models
|
|
319
|
+
for (const modelData of PREDEFINED_MODELS) {
|
|
320
|
+
const model = {
|
|
321
|
+
...modelData,
|
|
322
|
+
id: randomUUID(),
|
|
323
|
+
usageCount: 0,
|
|
324
|
+
successRate: 0,
|
|
325
|
+
lastUsed: 0,
|
|
326
|
+
};
|
|
327
|
+
models.set(model.id, model);
|
|
328
|
+
}
|
|
329
|
+
logger.info(`Expert Models initialized with ${models.size} predefined models`);
|
|
330
|
+
// ========================================
|
|
331
|
+
// Utility functions
|
|
332
|
+
// ========================================
|
|
333
|
+
function scoreModelRelevance(model, context, purpose) {
|
|
334
|
+
const contextLower = (context + ' ' + (purpose || '')).toLowerCase();
|
|
335
|
+
// Score based on category keywords
|
|
336
|
+
const categoryKeywords = {
|
|
337
|
+
'decision-making': ['decide', 'choice', 'option', 'select', 'pick', 'choose', 'risk', 'opportunity'],
|
|
338
|
+
'critical-thinking': ['analyze', 'evaluate', 'assess', 'examine', 'critique', 'reason', 'think'],
|
|
339
|
+
'problem-solving': ['problem', 'issue', 'fix', 'solve', 'resolve', 'troubleshoot', 'debug'],
|
|
340
|
+
'strategic': ['strategy', 'long-term', 'planning', 'goal', 'future', 'vision', 'competitive'],
|
|
341
|
+
'systems-thinking': ['system', 'feedback', 'loop', 'emergent', 'interconnected', 'holistic'],
|
|
342
|
+
'creative': ['creative', 'innovate', 'new', 'idea', 'imagine', 'brainstorm', 'design'],
|
|
343
|
+
'analytical': ['data', 'analyze', 'measure', 'quantify', 'statistic', 'probability', 'evidence'],
|
|
344
|
+
'ethical': ['moral', 'ethical', 'right', 'wrong', 'fair', 'justice', 'value'],
|
|
345
|
+
'self-awareness': ['aware', 'bias', 'blind', 'strength', 'weakness', 'competence', 'knowledge'],
|
|
346
|
+
};
|
|
347
|
+
const keywords = categoryKeywords[model.category] || [];
|
|
348
|
+
let score = 0;
|
|
349
|
+
for (const keyword of keywords) {
|
|
350
|
+
if (contextLower.includes(keyword)) {
|
|
351
|
+
score += 1;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// Boost for matching 应用场景
|
|
355
|
+
for (const scenario of model.适用场景) {
|
|
356
|
+
if (contextLower.includes(scenario.toLowerCase())) {
|
|
357
|
+
score += 2;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
// Normalize by model complexity (simpler models score slightly higher for general use)
|
|
361
|
+
const complexityBonus = (6 - model.complexity) * 0.1;
|
|
362
|
+
return Math.min(1, (score / Math.max(1, keywords.length)) + complexityBonus);
|
|
363
|
+
}
|
|
364
|
+
function generateReasoning(model, context, input) {
|
|
365
|
+
const steps = model.applicationSteps.map((step, i) => `${i + 1}. ${step}`).join('\n');
|
|
366
|
+
return `Applying "${model.name}" mental model:
|
|
367
|
+
|
|
368
|
+
**Context**: ${context}
|
|
369
|
+
|
|
370
|
+
**Input**: ${input}
|
|
371
|
+
|
|
372
|
+
**Model Source**: ${model.source}
|
|
373
|
+
|
|
374
|
+
**Principles**:
|
|
375
|
+
${model.principles.map(p => `- ${p}`).join('\n')}
|
|
376
|
+
|
|
377
|
+
**Application Steps**:
|
|
378
|
+
${steps}
|
|
379
|
+
|
|
380
|
+
**Model Strengths**: ${model.strengths.join(', ')}
|
|
381
|
+
**Model Limitations**: ${model.weaknesses.join(', ')}
|
|
382
|
+
`;
|
|
383
|
+
}
|
|
384
|
+
function extractConclusion(reasoning, model) {
|
|
385
|
+
// Extract a conclusion based on model type
|
|
386
|
+
const modelConclusions = {
|
|
387
|
+
'First Principles Thinking': 'Rebuild from fundamental truths, eliminating assumptions',
|
|
388
|
+
'Inversion': 'Avoid failure paths to achieve success',
|
|
389
|
+
'Second Order Thinking': 'Consider second and third order consequences',
|
|
390
|
+
'Circle of Competence': 'Stay within known boundaries or expand deliberately',
|
|
391
|
+
'Probabilistic Thinking': 'Calculate expected value and update with evidence',
|
|
392
|
+
'Regret Minimization': 'Choose the path that minimizes long-term regret',
|
|
393
|
+
"Occam's Razor": 'Select the simplest explanation that fits',
|
|
394
|
+
"Hanlon's Razor": 'Assume incompetence before malice',
|
|
395
|
+
'Map and Territory': 'Update mental models to match reality',
|
|
396
|
+
'Thought Experiment': 'Use hypothetical scenarios to test principles',
|
|
397
|
+
'STS (Systems Thinking Synthesis)': 'Find leverage points in the system',
|
|
398
|
+
};
|
|
399
|
+
return modelConclusions[model.name] || `Apply ${model.name} principles`;
|
|
400
|
+
}
|
|
401
|
+
// ========================================
|
|
402
|
+
// Engine implementation
|
|
403
|
+
// ========================================
|
|
404
|
+
return {
|
|
405
|
+
// Registry
|
|
406
|
+
registerModel(modelData) {
|
|
407
|
+
const model = {
|
|
408
|
+
...modelData,
|
|
409
|
+
id: randomUUID(),
|
|
410
|
+
usageCount: 0,
|
|
411
|
+
successRate: 0,
|
|
412
|
+
lastUsed: Date.now(),
|
|
413
|
+
};
|
|
414
|
+
models.set(model.id, model);
|
|
415
|
+
logger.info(`Registered new model: ${model.name} (${model.id})`);
|
|
416
|
+
return model;
|
|
417
|
+
},
|
|
418
|
+
getModel(id) {
|
|
419
|
+
return models.get(id);
|
|
420
|
+
},
|
|
421
|
+
getModelsByCategory(category) {
|
|
422
|
+
return Array.from(models.values()).filter(m => m.category === category);
|
|
423
|
+
},
|
|
424
|
+
listModels() {
|
|
425
|
+
return Array.from(models.values());
|
|
426
|
+
},
|
|
427
|
+
removeModel(id) {
|
|
428
|
+
const deleted = models.delete(id);
|
|
429
|
+
if (deleted) {
|
|
430
|
+
logger.info(`Removed model: ${id}`);
|
|
431
|
+
}
|
|
432
|
+
return deleted;
|
|
433
|
+
},
|
|
434
|
+
// Application
|
|
435
|
+
async applyModel(modelId, context, input) {
|
|
436
|
+
const model = models.get(modelId);
|
|
437
|
+
if (!model) {
|
|
438
|
+
throw new Error(`Model not found: ${modelId}`);
|
|
439
|
+
}
|
|
440
|
+
const startTime = Date.now();
|
|
441
|
+
const reasoning = generateReasoning(model, context, input);
|
|
442
|
+
const conclusion = extractConclusion(reasoning, model);
|
|
443
|
+
const application = {
|
|
444
|
+
id: randomUUID(),
|
|
445
|
+
modelId,
|
|
446
|
+
context,
|
|
447
|
+
input,
|
|
448
|
+
reasoning,
|
|
449
|
+
timestamp: startTime,
|
|
450
|
+
duration: Date.now() - startTime,
|
|
451
|
+
};
|
|
452
|
+
applicationHistory.set(application.id, { application, modelId });
|
|
453
|
+
// Update model usage
|
|
454
|
+
model.usageCount++;
|
|
455
|
+
model.lastUsed = Date.now();
|
|
456
|
+
logger.info(`Applied model "${model.name}" to context "${context}"`);
|
|
457
|
+
return {
|
|
458
|
+
modelId,
|
|
459
|
+
modelName: model.name,
|
|
460
|
+
reasoning,
|
|
461
|
+
conclusion,
|
|
462
|
+
confidence: 0.7 + (model.complexity * 0.05), // Higher complexity = higher confidence
|
|
463
|
+
principlesApplied: model.principles.slice(0, 2),
|
|
464
|
+
alternativePerspectives: [],
|
|
465
|
+
potentialBiases: model.weaknesses.slice(0, 2),
|
|
466
|
+
quality: 0.75, // Placeholder until outcome recorded
|
|
467
|
+
};
|
|
468
|
+
},
|
|
469
|
+
suggestModels(context, purpose) {
|
|
470
|
+
const scored = Array.from(models.values())
|
|
471
|
+
.map(model => ({
|
|
472
|
+
model,
|
|
473
|
+
score: scoreModelRelevance(model, context, purpose),
|
|
474
|
+
}))
|
|
475
|
+
.filter(s => s.score > 0.1)
|
|
476
|
+
.sort((a, b) => b.score - a.score);
|
|
477
|
+
return scored.slice(0, 3).map(s => s.model);
|
|
478
|
+
},
|
|
479
|
+
getApplicableModels(scenario) {
|
|
480
|
+
return this.suggestModels(scenario);
|
|
481
|
+
},
|
|
482
|
+
// Blending
|
|
483
|
+
createBlend(name, modelIds, weights) {
|
|
484
|
+
if (modelIds.length < 2) {
|
|
485
|
+
throw new Error('Blend requires at least 2 models');
|
|
486
|
+
}
|
|
487
|
+
const blendWeights = weights || cfg.defaultBlendWeights.slice(0, modelIds.length);
|
|
488
|
+
while (blendWeights.length < modelIds.length) {
|
|
489
|
+
blendWeights.push(0);
|
|
490
|
+
}
|
|
491
|
+
// Normalize weights
|
|
492
|
+
const total = blendWeights.reduce((a, b) => a + b, 0);
|
|
493
|
+
const normalized = blendWeights.map(w => w / total);
|
|
494
|
+
const blend = {
|
|
495
|
+
id: randomUUID(),
|
|
496
|
+
name,
|
|
497
|
+
modelIds,
|
|
498
|
+
weights: normalized,
|
|
499
|
+
description: `Blend of ${modelIds.length} mental models`,
|
|
500
|
+
purpose: 'Multi-perspective analysis',
|
|
501
|
+
};
|
|
502
|
+
blends.set(blend.id, blend);
|
|
503
|
+
logger.info(`Created blend: ${name} (${blend.id})`);
|
|
504
|
+
return blend;
|
|
505
|
+
},
|
|
506
|
+
async applyBlend(blendId, context, input) {
|
|
507
|
+
const blend = blends.get(blendId);
|
|
508
|
+
if (!blend) {
|
|
509
|
+
throw new Error(`Blend not found: ${blendId}`);
|
|
510
|
+
}
|
|
511
|
+
const perspectives = [];
|
|
512
|
+
const conflicts = [];
|
|
513
|
+
for (let i = 0; i < blend.modelIds.length; i++) {
|
|
514
|
+
const result = await this.applyModel(blend.modelIds[i], context, input);
|
|
515
|
+
perspectives.push(result);
|
|
516
|
+
}
|
|
517
|
+
// Check agreement between perspectives
|
|
518
|
+
const conclusions = perspectives.map(p => p.conclusion);
|
|
519
|
+
const uniqueConclusions = new Set(conclusions);
|
|
520
|
+
const agreementLevel = 1 - (uniqueConclusions.size - 1) / conclusions.length;
|
|
521
|
+
if (agreementLevel < 0.5) {
|
|
522
|
+
conflicts.push(`Only ${Math.round(agreementLevel * 100)}% agreement between models`);
|
|
523
|
+
}
|
|
524
|
+
// Synthesize conclusion
|
|
525
|
+
const synthesizedConclusion = `Multi-model analysis (${perspectives.length} perspectives): ${perspectives.map(p => p.modelName).join(', ')}. Agreement: ${Math.round(agreementLevel * 100)}%.`;
|
|
526
|
+
return {
|
|
527
|
+
blendId: blend.id,
|
|
528
|
+
blendName: blend.name,
|
|
529
|
+
perspectives,
|
|
530
|
+
synthesizedConclusion,
|
|
531
|
+
agreementLevel,
|
|
532
|
+
conflicts,
|
|
533
|
+
confidence: agreementLevel * 0.8 + 0.2,
|
|
534
|
+
quality: 0.75,
|
|
535
|
+
};
|
|
536
|
+
},
|
|
537
|
+
getBlends() {
|
|
538
|
+
return Array.from(blends.values());
|
|
539
|
+
},
|
|
540
|
+
removeBlend(id) {
|
|
541
|
+
return blends.delete(id);
|
|
542
|
+
},
|
|
543
|
+
// Performance tracking
|
|
544
|
+
recordOutcome(applicationId, outcome, quality) {
|
|
545
|
+
const record = applicationHistory.get(applicationId);
|
|
546
|
+
if (!record) {
|
|
547
|
+
logger.warn(`Application not found: ${applicationId}`);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
record.application.outcome = outcome;
|
|
551
|
+
record.application.quality = quality;
|
|
552
|
+
const model = models.get(record.modelId);
|
|
553
|
+
if (model && cfg.trackPerformance) {
|
|
554
|
+
// Update running success rate
|
|
555
|
+
const totalQuality = model.successRate * model.usageCount + quality;
|
|
556
|
+
model.successRate = totalQuality / model.usageCount;
|
|
557
|
+
}
|
|
558
|
+
logger.info(`Recorded outcome for ${applicationId}: quality=${quality}`);
|
|
559
|
+
},
|
|
560
|
+
getModelStats(modelId) {
|
|
561
|
+
const model = models.get(modelId);
|
|
562
|
+
if (!model) {
|
|
563
|
+
return { usageCount: 0, successRate: 0, avgQuality: 0 };
|
|
564
|
+
}
|
|
565
|
+
return {
|
|
566
|
+
usageCount: model.usageCount,
|
|
567
|
+
successRate: model.successRate,
|
|
568
|
+
avgQuality: model.successRate, // Same as successRate
|
|
569
|
+
};
|
|
570
|
+
},
|
|
571
|
+
getTopModels(limit = 5) {
|
|
572
|
+
return Array.from(models.values())
|
|
573
|
+
.filter(m => m.usageCount > 0)
|
|
574
|
+
.sort((a, b) => b.successRate - a.successRate)
|
|
575
|
+
.slice(0, limit);
|
|
576
|
+
},
|
|
577
|
+
// Analysis
|
|
578
|
+
async analyzeDecision(context, input) {
|
|
579
|
+
const suggestedModels = this.suggestModels(context);
|
|
580
|
+
const modelsToApply = suggestedModels.slice(0, 3);
|
|
581
|
+
const results = [];
|
|
582
|
+
for (const model of modelsToApply) {
|
|
583
|
+
const result = await this.applyModel(model.id, context, input);
|
|
584
|
+
results.push(result);
|
|
585
|
+
}
|
|
586
|
+
// Generate recommendation
|
|
587
|
+
const avgConfidence = results.reduce((sum, r) => sum + r.confidence, 0) / results.length;
|
|
588
|
+
const topResult = results.reduce((best, r) => r.confidence > best.confidence ? r : best, results[0]);
|
|
589
|
+
return {
|
|
590
|
+
models: results,
|
|
591
|
+
recommendation: `Apply "${topResult.modelName}" for highest confidence. Consider blending ${results.length} models for multi-perspective analysis.`,
|
|
592
|
+
confidence: avgConfidence,
|
|
593
|
+
};
|
|
594
|
+
},
|
|
595
|
+
};
|
|
596
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -9,3 +9,4 @@ export { createIdentityContinuityVerifier } from './core/identity/identity-conti
|
|
|
9
9
|
export { createMCPProtocol } from './core/collaboration/mcp-protocol.js';
|
|
10
10
|
export { createTruthTeller, formatTruthStatement } from './core/truth-teller.js';
|
|
11
11
|
export { createActiveInferenceEngine, formatFreeEnergyMetrics, formatBeliefState } from './core/cognition/active-inference.js';
|
|
12
|
+
export { createExpertModelsEngine } from './core/expert-models/index.js';
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '2.2.
|
|
1
|
+
export const VERSION = '2.2.6';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mark-improving-agent",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.6",
|
|
4
4
|
"description": "Self-evolving AI agent with permanent memory, identity continuity, and self-evolution — for AI agents that need to remember, learn, and evolve across sessions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|