mark-improving-agent 2.2.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/README.md +335 -0
- package/VERSION +1 -0
- package/bin/cli.js +12 -0
- package/dist/agent/context.js +78 -0
- package/dist/agent/index.js +6 -0
- package/dist/agent/runtime.js +195 -0
- package/dist/agent/task-graph.js +209 -0
- package/dist/agent/types.js +1 -0
- package/dist/cli/index.js +206 -0
- package/dist/core/cognition/active-inference.js +296 -0
- package/dist/core/cognition/cognitive-architecture.js +263 -0
- package/dist/core/cognition/dual-process.js +102 -0
- package/dist/core/cognition/index.js +13 -0
- package/dist/core/cognition/learning-from-failure.js +184 -0
- package/dist/core/cognition/meta-agent.js +407 -0
- package/dist/core/cognition/metacognition.js +322 -0
- package/dist/core/cognition/react.js +177 -0
- package/dist/core/cognition/retrieval-anchor.js +99 -0
- package/dist/core/cognition/self-evolution.js +294 -0
- package/dist/core/cognition/self-verification.js +190 -0
- package/dist/core/cognition/thought-graph.js +495 -0
- package/dist/core/cognition/tool-augmented-llm.js +188 -0
- package/dist/core/cognition/tool-execution-verifier.js +204 -0
- package/dist/core/collaboration/agentic-loop.js +165 -0
- package/dist/core/collaboration/index.js +3 -0
- package/dist/core/collaboration/multi-agent-system.js +186 -0
- package/dist/core/collaboration/multi-agent.js +110 -0
- package/dist/core/consciousness/emotion-engine.js +101 -0
- package/dist/core/consciousness/flow-machine.js +121 -0
- package/dist/core/consciousness/index.js +4 -0
- package/dist/core/consciousness/personality.js +103 -0
- package/dist/core/consciousness/types.js +1 -0
- package/dist/core/emotional-protocol.js +54 -0
- package/dist/core/evolution/engine.js +194 -0
- package/dist/core/evolution/goal-engine.js +153 -0
- package/dist/core/evolution/index.js +6 -0
- package/dist/core/evolution/meta-learning.js +172 -0
- package/dist/core/evolution/reflection.js +158 -0
- package/dist/core/evolution/self-healer.js +139 -0
- package/dist/core/evolution/types.js +1 -0
- package/dist/core/healing-rl.js +266 -0
- package/dist/core/heartbeat.js +408 -0
- package/dist/core/identity/index.js +3 -0
- package/dist/core/identity/reflexion.js +165 -0
- package/dist/core/identity/self-model.js +274 -0
- package/dist/core/identity/self-verifier.js +158 -0
- package/dist/core/identity/types.js +12 -0
- package/dist/core/lesson-bank.js +301 -0
- package/dist/core/memory/adaptive-rag.js +440 -0
- package/dist/core/memory/archive-store.js +187 -0
- package/dist/core/memory/dream-consolidation.js +366 -0
- package/dist/core/memory/embedder.js +130 -0
- package/dist/core/memory/hopfield-network.js +128 -0
- package/dist/core/memory/index.js +9 -0
- package/dist/core/memory/knowledge-graph.js +151 -0
- package/dist/core/memory/spaced-repetition.js +113 -0
- package/dist/core/memory/store.js +404 -0
- package/dist/core/memory/types.js +1 -0
- package/dist/core/psychology/analysis.js +456 -0
- package/dist/core/psychology/index.js +1 -0
- package/dist/core/rollback-manager.js +191 -0
- package/dist/core/security/index.js +1 -0
- package/dist/core/security/privacy.js +132 -0
- package/dist/core/truth-teller.js +253 -0
- package/dist/core/truthfulness.js +99 -0
- package/dist/core/types.js +2 -0
- package/dist/event/bus.js +47 -0
- package/dist/index.js +8 -0
- package/dist/skills/dag.js +181 -0
- package/dist/skills/index.js +5 -0
- package/dist/skills/registry.js +40 -0
- package/dist/skills/types.js +1 -0
- package/dist/storage/archive.js +77 -0
- package/dist/storage/checkpoint.js +119 -0
- package/dist/storage/types.js +1 -0
- package/dist/utils/config.js +81 -0
- package/dist/utils/logger.js +49 -0
- package/dist/version.js +1 -0
- package/package.json +37 -0
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thought Graph - Framework of Thoughts (FoT)
|
|
3
|
+
*
|
|
4
|
+
* Paper: "Framework of Thoughts: Cognitive Process Integration via Graph of Operations"
|
|
5
|
+
*
|
|
6
|
+
* Key concepts:
|
|
7
|
+
* - Graph of Operations (GoO): DAG of operations representing reasoning process
|
|
8
|
+
* - Thought Graph (TG): Graph representation of thought chains
|
|
9
|
+
* - Operation nodes: Selectors (for/while/if) and actions
|
|
10
|
+
* - Thought nodes: Primitive operations
|
|
11
|
+
* - Meta-cognition through: Replanning, Self-Verification, Reflection
|
|
12
|
+
*
|
|
13
|
+
* Key strategies:
|
|
14
|
+
* - Divide and Conquer: Break complex problems
|
|
15
|
+
* - Multi-Path Exploration: Try multiple approaches
|
|
16
|
+
* - Backtracking: Revert to previous state
|
|
17
|
+
* - Self-Verification: Check intermediate results
|
|
18
|
+
*/
|
|
19
|
+
import { randomUUID } from 'crypto';
|
|
20
|
+
const DEFAULT_OPTIONS = {
|
|
21
|
+
maxDepth: 10,
|
|
22
|
+
maxNodes: 100,
|
|
23
|
+
verificationThreshold: 0.75,
|
|
24
|
+
enableSelfVerification: true,
|
|
25
|
+
enableBacktracking: true,
|
|
26
|
+
enableMultiPath: true,
|
|
27
|
+
};
|
|
28
|
+
export function createThoughtGraph(options) {
|
|
29
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
30
|
+
const nodes = new Map();
|
|
31
|
+
const edges = new Map();
|
|
32
|
+
let rootId = null;
|
|
33
|
+
let nodeCount = 0;
|
|
34
|
+
function generateNodeId() {
|
|
35
|
+
return `node-${Date.now()}-${randomUUID().slice(0, 8)}`;
|
|
36
|
+
}
|
|
37
|
+
function generateEdgeId() {
|
|
38
|
+
return `edge-${Date.now()}-${randomUUID().slice(0, 8)}`;
|
|
39
|
+
}
|
|
40
|
+
function addEdge(sourceId, targetId, relation) {
|
|
41
|
+
const edge = {
|
|
42
|
+
id: generateEdgeId(),
|
|
43
|
+
sourceId,
|
|
44
|
+
targetId,
|
|
45
|
+
relation,
|
|
46
|
+
weight: 1.0,
|
|
47
|
+
};
|
|
48
|
+
edges.set(edge.id, edge);
|
|
49
|
+
// Update parent's children
|
|
50
|
+
const parent = nodes.get(sourceId);
|
|
51
|
+
if (parent) {
|
|
52
|
+
if (!parent.children)
|
|
53
|
+
parent.children = [];
|
|
54
|
+
parent.children.push(targetId);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function addPrimitive(content, parentId) {
|
|
58
|
+
const id = generateNodeId();
|
|
59
|
+
const node = {
|
|
60
|
+
id,
|
|
61
|
+
type: 'primitive',
|
|
62
|
+
content,
|
|
63
|
+
depth: parentId ? (nodes.get(parentId)?.depth ?? 0) + 1 : 0,
|
|
64
|
+
confidence: 0.7,
|
|
65
|
+
verified: false,
|
|
66
|
+
timestamp: Date.now(),
|
|
67
|
+
};
|
|
68
|
+
nodes.set(id, node);
|
|
69
|
+
nodeCount++;
|
|
70
|
+
if (!rootId)
|
|
71
|
+
rootId = id;
|
|
72
|
+
if (parentId) {
|
|
73
|
+
node.parent = parentId;
|
|
74
|
+
addEdge(parentId, id, 'sequence');
|
|
75
|
+
}
|
|
76
|
+
return node;
|
|
77
|
+
}
|
|
78
|
+
function addSelector(type, condition, parentId) {
|
|
79
|
+
const id = generateNodeId();
|
|
80
|
+
const node = {
|
|
81
|
+
id,
|
|
82
|
+
type: 'selector',
|
|
83
|
+
content: `${type.toUpperCase()}: ${condition}`,
|
|
84
|
+
depth: parentId ? (nodes.get(parentId)?.depth ?? 0) + 1 : 0,
|
|
85
|
+
confidence: 0.6,
|
|
86
|
+
verified: false,
|
|
87
|
+
metadata: { selectorType: type, condition },
|
|
88
|
+
timestamp: Date.now(),
|
|
89
|
+
};
|
|
90
|
+
nodes.set(id, node);
|
|
91
|
+
nodeCount++;
|
|
92
|
+
if (!rootId)
|
|
93
|
+
rootId = id;
|
|
94
|
+
if (parentId) {
|
|
95
|
+
node.parent = parentId;
|
|
96
|
+
addEdge(parentId, id, 'sequence');
|
|
97
|
+
}
|
|
98
|
+
return node;
|
|
99
|
+
}
|
|
100
|
+
function addAction(content, parentId) {
|
|
101
|
+
const id = generateNodeId();
|
|
102
|
+
const node = {
|
|
103
|
+
id,
|
|
104
|
+
type: 'action',
|
|
105
|
+
content,
|
|
106
|
+
depth: parentId ? (nodes.get(parentId)?.depth ?? 0) + 1 : 0,
|
|
107
|
+
confidence: 0.8,
|
|
108
|
+
verified: false,
|
|
109
|
+
timestamp: Date.now(),
|
|
110
|
+
};
|
|
111
|
+
nodes.set(id, node);
|
|
112
|
+
nodeCount++;
|
|
113
|
+
if (!rootId)
|
|
114
|
+
rootId = id;
|
|
115
|
+
if (parentId) {
|
|
116
|
+
node.parent = parentId;
|
|
117
|
+
addEdge(parentId, id, 'sequence');
|
|
118
|
+
}
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
function addVerification(nodeId) {
|
|
122
|
+
const parent = nodes.get(nodeId);
|
|
123
|
+
const id = generateNodeId();
|
|
124
|
+
const node = {
|
|
125
|
+
id,
|
|
126
|
+
type: 'verification',
|
|
127
|
+
content: `VERIFY: ${parent?.content ?? nodeId}`,
|
|
128
|
+
depth: parent ? parent.depth + 1 : 0,
|
|
129
|
+
confidence: 0.9,
|
|
130
|
+
verified: true,
|
|
131
|
+
metadata: { verifiedNodeId: nodeId },
|
|
132
|
+
timestamp: Date.now(),
|
|
133
|
+
};
|
|
134
|
+
nodes.set(id, node);
|
|
135
|
+
nodeCount++;
|
|
136
|
+
addEdge(nodeId, id, 'verifies');
|
|
137
|
+
return node;
|
|
138
|
+
}
|
|
139
|
+
function addReflection(content, parentId) {
|
|
140
|
+
const id = generateNodeId();
|
|
141
|
+
const node = {
|
|
142
|
+
id,
|
|
143
|
+
type: 'reflection',
|
|
144
|
+
content: `REFLECT: ${content}`,
|
|
145
|
+
depth: parentId ? (nodes.get(parentId)?.depth ?? 0) + 1 : 0,
|
|
146
|
+
confidence: 0.5,
|
|
147
|
+
verified: false,
|
|
148
|
+
metadata: { isMetaCognitive: true },
|
|
149
|
+
timestamp: Date.now(),
|
|
150
|
+
};
|
|
151
|
+
nodes.set(id, node);
|
|
152
|
+
nodeCount++;
|
|
153
|
+
if (parentId) {
|
|
154
|
+
node.parent = parentId;
|
|
155
|
+
addEdge(parentId, id, 'sequence');
|
|
156
|
+
}
|
|
157
|
+
return node;
|
|
158
|
+
}
|
|
159
|
+
function getNode(id) {
|
|
160
|
+
return nodes.get(id);
|
|
161
|
+
}
|
|
162
|
+
function getPath(fromId, toId) {
|
|
163
|
+
// BFS to find path between nodes
|
|
164
|
+
const visited = new Set();
|
|
165
|
+
const queue = [[fromId]];
|
|
166
|
+
while (queue.length > 0) {
|
|
167
|
+
const path = queue.shift();
|
|
168
|
+
const current = path[path.length - 1];
|
|
169
|
+
if (current === toId)
|
|
170
|
+
return path;
|
|
171
|
+
if (visited.has(current))
|
|
172
|
+
continue;
|
|
173
|
+
visited.add(current);
|
|
174
|
+
const currentNode = nodes.get(current);
|
|
175
|
+
if (currentNode?.children) {
|
|
176
|
+
for (const childId of currentNode.children) {
|
|
177
|
+
if (!visited.has(childId)) {
|
|
178
|
+
queue.push([...path, childId]);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
function getLeaves() {
|
|
186
|
+
const leaves = [];
|
|
187
|
+
for (const node of nodes.values()) {
|
|
188
|
+
if (!node.children || node.children.length === 0) {
|
|
189
|
+
leaves.push(node);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return leaves;
|
|
193
|
+
}
|
|
194
|
+
function verifyNode(nodeId) {
|
|
195
|
+
const node = nodes.get(nodeId);
|
|
196
|
+
if (!node)
|
|
197
|
+
return { passed: false, confidence: 0 };
|
|
198
|
+
// Self-verification logic
|
|
199
|
+
const checks = [];
|
|
200
|
+
// Check if node has acceptable confidence
|
|
201
|
+
checks.push(node.confidence >= opts.verificationThreshold);
|
|
202
|
+
// Check if node content is non-empty
|
|
203
|
+
checks.push(node.content.length > 0);
|
|
204
|
+
// Check if depth is within limits
|
|
205
|
+
checks.push(node.depth <= opts.maxDepth);
|
|
206
|
+
const passed = checks.every(c => c);
|
|
207
|
+
const confidence = passed ? node.confidence : node.confidence * 0.5;
|
|
208
|
+
return { passed, confidence };
|
|
209
|
+
}
|
|
210
|
+
function verifyPath(nodeIds) {
|
|
211
|
+
const issues = [];
|
|
212
|
+
let totalConfidence = 0;
|
|
213
|
+
for (const nodeId of nodeIds) {
|
|
214
|
+
const { passed, confidence } = verifyNode(nodeId);
|
|
215
|
+
totalConfidence += confidence;
|
|
216
|
+
if (!passed) {
|
|
217
|
+
const node = nodes.get(nodeId);
|
|
218
|
+
issues.push(`Node ${nodeId} (${node?.type}) failed verification`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const avgConfidence = nodeIds.length > 0 ? totalConfidence / nodeIds.length : 0;
|
|
222
|
+
return {
|
|
223
|
+
passed: issues.length === 0,
|
|
224
|
+
confidence: avgConfidence,
|
|
225
|
+
issues,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function backtrack(fromId) {
|
|
229
|
+
const node = nodes.get(fromId);
|
|
230
|
+
if (!node)
|
|
231
|
+
return null;
|
|
232
|
+
// Find the parent or sibling path
|
|
233
|
+
if (node.parent) {
|
|
234
|
+
const parent = nodes.get(node.parent);
|
|
235
|
+
if (parent?.parent) {
|
|
236
|
+
return parent.parent;
|
|
237
|
+
}
|
|
238
|
+
return parent?.id ?? null;
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
function getAlternativePaths(nodeId) {
|
|
243
|
+
if (!opts.enableMultiPath)
|
|
244
|
+
return [];
|
|
245
|
+
const alternatives = [];
|
|
246
|
+
const node = nodes.get(nodeId);
|
|
247
|
+
if (!node || !node.children || node.children.length <= 1)
|
|
248
|
+
return alternatives;
|
|
249
|
+
// For selector nodes, get paths through each child
|
|
250
|
+
if (node.type === 'selector' && node.children.length > 1) {
|
|
251
|
+
for (const childId of node.children) {
|
|
252
|
+
const path = getPath(rootId, childId);
|
|
253
|
+
if (path.length > 0)
|
|
254
|
+
alternatives.push(path);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return alternatives;
|
|
258
|
+
}
|
|
259
|
+
function selfVerify() {
|
|
260
|
+
const issues = [];
|
|
261
|
+
let totalConfidence = 0;
|
|
262
|
+
let verifiedCount = 0;
|
|
263
|
+
for (const node of nodes.values()) {
|
|
264
|
+
const { passed, confidence } = verifyNode(node.id);
|
|
265
|
+
totalConfidence += confidence;
|
|
266
|
+
if (passed) {
|
|
267
|
+
verifiedCount++;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
issues.push(`Node ${node.id} (${node.type}): ${node.content.slice(0, 30)}...`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
verified: issues.length === 0,
|
|
275
|
+
issues,
|
|
276
|
+
confidence: totalConfidence / (nodes.size || 1),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
function reflect() {
|
|
280
|
+
const insights = [];
|
|
281
|
+
const improvements = [];
|
|
282
|
+
// Analyze node depth distribution
|
|
283
|
+
const depthCount = new Map();
|
|
284
|
+
for (const node of nodes.values()) {
|
|
285
|
+
depthCount.set(node.depth, (depthCount.get(node.depth) || 0) + 1);
|
|
286
|
+
}
|
|
287
|
+
// Deep thoughts might indicate over-complexity
|
|
288
|
+
const maxDepth = Math.max(...Array.from(depthCount.keys()));
|
|
289
|
+
if (maxDepth > opts.maxDepth * 0.8) {
|
|
290
|
+
insights.push(`Graph is relatively deep (${maxDepth} levels) - consider simplification`);
|
|
291
|
+
improvements.push('Break complex reasoning into smaller sub-problems');
|
|
292
|
+
}
|
|
293
|
+
// Check verification coverage
|
|
294
|
+
const verifiedCount = Array.from(nodes.values()).filter(n => n.verified).length;
|
|
295
|
+
const verificationRatio = verifiedCount / (nodes.size || 1);
|
|
296
|
+
if (verificationRatio < 0.5) {
|
|
297
|
+
insights.push(`Low verification coverage (${(verificationRatio * 100).toFixed(0)}%)`);
|
|
298
|
+
improvements.push('Add more self-verification nodes to critical paths');
|
|
299
|
+
}
|
|
300
|
+
// Check confidence distribution
|
|
301
|
+
const confidences = Array.from(nodes.values()).map(n => n.confidence);
|
|
302
|
+
const avgConfidence = confidences.reduce((a, b) => a + b, 0) / (confidences.length || 1);
|
|
303
|
+
if (avgConfidence < opts.verificationThreshold) {
|
|
304
|
+
insights.push(`Average confidence (${(avgConfidence * 100).toFixed(0)}%) below threshold`);
|
|
305
|
+
improvements.push('Strengthen weak reasoning steps with more evidence');
|
|
306
|
+
}
|
|
307
|
+
// Check for selector balance
|
|
308
|
+
const selectors = Array.from(nodes.values()).filter(n => n.type === 'selector');
|
|
309
|
+
if (selectors.length > 0) {
|
|
310
|
+
insights.push(`Using ${selectors.length} control flow selectors`);
|
|
311
|
+
}
|
|
312
|
+
return { insights, improvements };
|
|
313
|
+
}
|
|
314
|
+
function buildFromThoughts(thoughts) {
|
|
315
|
+
let currentParent;
|
|
316
|
+
for (const thought of thoughts) {
|
|
317
|
+
const node = addPrimitive(thought, currentParent);
|
|
318
|
+
currentParent = node.id;
|
|
319
|
+
}
|
|
320
|
+
return execute();
|
|
321
|
+
}
|
|
322
|
+
function execute() {
|
|
323
|
+
if (!rootId) {
|
|
324
|
+
return {
|
|
325
|
+
graph: { nodes, edges, rootId },
|
|
326
|
+
path: [],
|
|
327
|
+
success: false,
|
|
328
|
+
confidence: 0,
|
|
329
|
+
verifiedPaths: [],
|
|
330
|
+
failedNodes: [],
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
// DFS execution from root
|
|
334
|
+
const path = [];
|
|
335
|
+
const verifiedPaths = [];
|
|
336
|
+
const failedNodes = [];
|
|
337
|
+
function traverse(nodeId) {
|
|
338
|
+
const node = nodes.get(nodeId);
|
|
339
|
+
if (!node)
|
|
340
|
+
return;
|
|
341
|
+
path.push(nodeId);
|
|
342
|
+
// Self-verify if enabled
|
|
343
|
+
if (opts.enableSelfVerification && node.type !== 'verification') {
|
|
344
|
+
const { passed } = verifyNode(nodeId);
|
|
345
|
+
if (passed) {
|
|
346
|
+
verifiedPaths.push(nodeId);
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
failedNodes.push(nodeId);
|
|
350
|
+
// Backtrack if enabled and node failed
|
|
351
|
+
if (opts.enableBacktracking && node.type === 'primitive') {
|
|
352
|
+
const alternative = backtrack(nodeId);
|
|
353
|
+
if (alternative) {
|
|
354
|
+
// Try alternative path
|
|
355
|
+
const alternatives = getAlternativePaths(alternative);
|
|
356
|
+
if (alternatives.length > 0) {
|
|
357
|
+
const altPath = alternatives[0];
|
|
358
|
+
for (const altId of altPath) {
|
|
359
|
+
if (!path.includes(altId)) {
|
|
360
|
+
traverse(altId);
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// Recurse to children
|
|
370
|
+
if (node.children) {
|
|
371
|
+
for (const childId of node.children) {
|
|
372
|
+
if (!path.includes(childId)) {
|
|
373
|
+
traverse(childId);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
traverse(rootId);
|
|
379
|
+
const { passed, confidence } = verifyPath(path);
|
|
380
|
+
return {
|
|
381
|
+
graph: { nodes, edges, rootId },
|
|
382
|
+
path,
|
|
383
|
+
success: passed && failedNodes.length === 0,
|
|
384
|
+
confidence,
|
|
385
|
+
verifiedPaths,
|
|
386
|
+
failedNodes,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
function executeFrom(nodeId) {
|
|
390
|
+
const startNode = nodes.get(nodeId);
|
|
391
|
+
if (!startNode) {
|
|
392
|
+
return {
|
|
393
|
+
graph: { nodes, edges, rootId },
|
|
394
|
+
path: [],
|
|
395
|
+
success: false,
|
|
396
|
+
confidence: 0,
|
|
397
|
+
verifiedPaths: [],
|
|
398
|
+
failedNodes: [],
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
const path = [];
|
|
402
|
+
const verifiedPaths = [];
|
|
403
|
+
const failedNodes = [];
|
|
404
|
+
function traverse(id) {
|
|
405
|
+
const node = nodes.get(id);
|
|
406
|
+
if (!node)
|
|
407
|
+
return;
|
|
408
|
+
path.push(id);
|
|
409
|
+
if (opts.enableSelfVerification) {
|
|
410
|
+
const { passed } = verifyNode(id);
|
|
411
|
+
if (passed) {
|
|
412
|
+
verifiedPaths.push(id);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
failedNodes.push(id);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (node.children) {
|
|
419
|
+
for (const childId of node.children) {
|
|
420
|
+
if (!path.includes(childId)) {
|
|
421
|
+
traverse(childId);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
traverse(nodeId);
|
|
427
|
+
const { passed, confidence } = verifyPath(path);
|
|
428
|
+
return {
|
|
429
|
+
graph: { nodes, edges, rootId },
|
|
430
|
+
path,
|
|
431
|
+
success: passed && failedNodes.length === 0,
|
|
432
|
+
confidence,
|
|
433
|
+
verifiedPaths,
|
|
434
|
+
failedNodes,
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
function serialize() {
|
|
438
|
+
return JSON.stringify({
|
|
439
|
+
nodes: Array.from(nodes.entries()),
|
|
440
|
+
edges: Array.from(edges.entries()),
|
|
441
|
+
rootId,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
function getStats() {
|
|
445
|
+
let maxDepth = 0;
|
|
446
|
+
let verifiedCount = 0;
|
|
447
|
+
let totalConfidence = 0;
|
|
448
|
+
for (const node of nodes.values()) {
|
|
449
|
+
if (node.depth > maxDepth)
|
|
450
|
+
maxDepth = node.depth;
|
|
451
|
+
if (node.verified)
|
|
452
|
+
verifiedCount++;
|
|
453
|
+
totalConfidence += node.confidence;
|
|
454
|
+
}
|
|
455
|
+
return {
|
|
456
|
+
nodeCount: nodes.size,
|
|
457
|
+
edgeCount: edges.size,
|
|
458
|
+
maxDepth,
|
|
459
|
+
verifiedCount,
|
|
460
|
+
avgConfidence: nodes.size > 0 ? totalConfidence / nodes.size : 0,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
buildFromThoughts,
|
|
465
|
+
addPrimitive,
|
|
466
|
+
addSelector,
|
|
467
|
+
addAction,
|
|
468
|
+
addVerification,
|
|
469
|
+
addReflection,
|
|
470
|
+
getNode,
|
|
471
|
+
getPath,
|
|
472
|
+
getLeaves,
|
|
473
|
+
verifyNode,
|
|
474
|
+
verifyPath,
|
|
475
|
+
backtrack,
|
|
476
|
+
getAlternativePaths,
|
|
477
|
+
selfVerify,
|
|
478
|
+
reflect,
|
|
479
|
+
execute,
|
|
480
|
+
executeFrom,
|
|
481
|
+
serialize: () => serialize(),
|
|
482
|
+
getStats,
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
// Static deserialize method
|
|
486
|
+
export function deserializeThoughtGraph(data) {
|
|
487
|
+
const parsed = JSON.parse(data);
|
|
488
|
+
const nodes = new Map(parsed.nodes);
|
|
489
|
+
const edges = new Map(parsed.edges);
|
|
490
|
+
return {
|
|
491
|
+
nodes,
|
|
492
|
+
edges,
|
|
493
|
+
rootId: parsed.rootId,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-Augmented LLM - External Tool Integration
|
|
3
|
+
*
|
|
4
|
+
* Paper: "Tool-Augmented Language Models" (2301.06789)
|
|
5
|
+
* Concept: LLM with ability to use external tools
|
|
6
|
+
*
|
|
7
|
+
* Key mechanisms:
|
|
8
|
+
* - Tool selection with confidence
|
|
9
|
+
* - Sequential tool use for complex tasks
|
|
10
|
+
* - Reflection on tool results
|
|
11
|
+
* - Error handling and recovery
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Predefined tools for mark-improving-agent
|
|
15
|
+
*/
|
|
16
|
+
export const DEFAULT_TOOLS = [
|
|
17
|
+
{
|
|
18
|
+
id: 'memory_search',
|
|
19
|
+
name: 'search_memory',
|
|
20
|
+
description: 'Search long-term memory for relevant information',
|
|
21
|
+
parameters: {
|
|
22
|
+
query: { type: 'string', required: true, description: 'Search query' },
|
|
23
|
+
limit: { type: 'number', required: false, description: 'Max results (default: 5)' },
|
|
24
|
+
},
|
|
25
|
+
confidence: 0.95,
|
|
26
|
+
requiresApproval: false,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: 'lesson_check',
|
|
30
|
+
name: 'check_lesson_bank',
|
|
31
|
+
description: 'Check lesson bank for relevant error patterns',
|
|
32
|
+
parameters: {
|
|
33
|
+
pattern: { type: 'string', required: true, description: 'Error pattern to check' },
|
|
34
|
+
},
|
|
35
|
+
confidence: 0.9,
|
|
36
|
+
requiresApproval: false,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: 'self_verify',
|
|
40
|
+
name: 'verify_reasoning',
|
|
41
|
+
description: 'Self-verify reasoning chain for consistency',
|
|
42
|
+
parameters: {
|
|
43
|
+
reasoning: { type: 'string', required: true, description: 'Reasoning chain to verify' },
|
|
44
|
+
},
|
|
45
|
+
confidence: 0.85,
|
|
46
|
+
requiresApproval: false,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: 'dream_consolidate',
|
|
50
|
+
name: 'dream_consolidation',
|
|
51
|
+
description: 'Run dream consolidation for memory integration',
|
|
52
|
+
parameters: {
|
|
53
|
+
intensity: { type: 'number', required: false, description: 'Consolidation intensity 0-1' },
|
|
54
|
+
},
|
|
55
|
+
confidence: 0.8,
|
|
56
|
+
requiresApproval: false,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: 'knowledge_graph',
|
|
60
|
+
name: 'query_knowledge_graph',
|
|
61
|
+
description: 'Query knowledge graph for structured relationships',
|
|
62
|
+
parameters: {
|
|
63
|
+
query: { type: 'string', required: true, description: 'Query for KG' },
|
|
64
|
+
relation: { type: 'string', required: false, description: 'Filter by relation type' },
|
|
65
|
+
},
|
|
66
|
+
confidence: 0.85,
|
|
67
|
+
requiresApproval: false,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
id: 'security_check',
|
|
71
|
+
name: 'check_security',
|
|
72
|
+
description: 'Check for sensitive information in content',
|
|
73
|
+
parameters: {
|
|
74
|
+
content: { type: 'string', required: true, description: 'Content to check' },
|
|
75
|
+
},
|
|
76
|
+
confidence: 0.95,
|
|
77
|
+
requiresApproval: false,
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: 'reflect',
|
|
81
|
+
name: 'self_reflect',
|
|
82
|
+
description: 'Perform self-reflection on recent actions',
|
|
83
|
+
parameters: {
|
|
84
|
+
focus: { type: 'string', required: false, description: 'Focus area for reflection' },
|
|
85
|
+
},
|
|
86
|
+
confidence: 0.75,
|
|
87
|
+
requiresApproval: false,
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
export function createToolAugmentedLLM(tools = DEFAULT_TOOLS) {
|
|
91
|
+
const toolMap = new Map();
|
|
92
|
+
const executions = [];
|
|
93
|
+
let totalExecutions = 0;
|
|
94
|
+
let successfulExecutions = 0;
|
|
95
|
+
// Register all tools
|
|
96
|
+
for (const tool of tools) {
|
|
97
|
+
toolMap.set(tool.id, tool);
|
|
98
|
+
}
|
|
99
|
+
function registerTool(tool) {
|
|
100
|
+
toolMap.set(tool.id, tool);
|
|
101
|
+
}
|
|
102
|
+
function selectTool(query, context) {
|
|
103
|
+
const queryLower = query.toLowerCase();
|
|
104
|
+
let bestMatch = null;
|
|
105
|
+
let bestScore = 0;
|
|
106
|
+
for (const tool of toolMap.values()) {
|
|
107
|
+
let score = 0;
|
|
108
|
+
// Check name match
|
|
109
|
+
if (tool.name.toLowerCase().includes(queryLower)) {
|
|
110
|
+
score += 0.5;
|
|
111
|
+
}
|
|
112
|
+
// Check description match
|
|
113
|
+
if (tool.description.toLowerCase().includes(queryLower)) {
|
|
114
|
+
score += 0.3;
|
|
115
|
+
}
|
|
116
|
+
// Apply tool confidence as tiebreaker
|
|
117
|
+
score += tool.confidence * 0.2;
|
|
118
|
+
if (score > bestScore) {
|
|
119
|
+
bestScore = score;
|
|
120
|
+
bestMatch = tool;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return bestScore > 0.3 ? bestMatch : null;
|
|
124
|
+
}
|
|
125
|
+
async function execute(tool, params) {
|
|
126
|
+
const start = Date.now();
|
|
127
|
+
totalExecutions++;
|
|
128
|
+
// Simulate tool execution
|
|
129
|
+
const execution = {
|
|
130
|
+
toolId: tool.id,
|
|
131
|
+
parameters: params,
|
|
132
|
+
result: `Executed ${tool.name} with params: ${JSON.stringify(params)}`,
|
|
133
|
+
success: true,
|
|
134
|
+
duration: Date.now() - start,
|
|
135
|
+
timestamp: Date.now(),
|
|
136
|
+
};
|
|
137
|
+
// Simulate occasional failures
|
|
138
|
+
if (Math.random() < 0.05) {
|
|
139
|
+
execution.success = false;
|
|
140
|
+
execution.error = 'Simulated tool failure';
|
|
141
|
+
execution.result = '';
|
|
142
|
+
}
|
|
143
|
+
if (execution.success) {
|
|
144
|
+
successfulExecutions++;
|
|
145
|
+
}
|
|
146
|
+
executions.push(execution);
|
|
147
|
+
return execution;
|
|
148
|
+
}
|
|
149
|
+
async function executeSequence(query, maxTools = 3) {
|
|
150
|
+
const selectedTools = [];
|
|
151
|
+
let remainingQuery = query;
|
|
152
|
+
let toolsUsed = 0;
|
|
153
|
+
// Sequential tool selection
|
|
154
|
+
while (toolsUsed < maxTools) {
|
|
155
|
+
const tool = selectTool(remainingQuery);
|
|
156
|
+
if (!tool)
|
|
157
|
+
break;
|
|
158
|
+
selectedTools.push(tool);
|
|
159
|
+
const execution = await execute(tool, { query: remainingQuery });
|
|
160
|
+
// Update query based on result
|
|
161
|
+
remainingQuery = execution.result || remainingQuery;
|
|
162
|
+
toolsUsed++;
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
output: selectedTools.map(t => t.name).join(' -> '),
|
|
166
|
+
confidence: selectedTools.length > 0
|
|
167
|
+
? selectedTools.reduce((sum, t) => sum + t.confidence, 0) / selectedTools.length
|
|
168
|
+
: 0,
|
|
169
|
+
suggestions: selectedTools.length < maxTools
|
|
170
|
+
? ['Task completed']
|
|
171
|
+
: ['More tools may be needed'],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function getToolStats() {
|
|
175
|
+
return {
|
|
176
|
+
tools: toolMap.size,
|
|
177
|
+
executions: totalExecutions,
|
|
178
|
+
successRate: totalExecutions > 0 ? successfulExecutions / totalExecutions : 0,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
registerTool,
|
|
183
|
+
selectTool,
|
|
184
|
+
execute,
|
|
185
|
+
executeSequence,
|
|
186
|
+
getToolStats,
|
|
187
|
+
};
|
|
188
|
+
}
|