musubi-sdd 5.0.0 → 5.6.1
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.ja.md +106 -48
- package/README.md +110 -32
- package/bin/musubi-analyze.js +74 -67
- package/bin/musubi-browser.js +27 -26
- package/bin/musubi-change.js +48 -47
- package/bin/musubi-checkpoint.js +10 -7
- package/bin/musubi-convert.js +25 -25
- package/bin/musubi-costs.js +27 -10
- package/bin/musubi-gui.js +52 -46
- package/bin/musubi-init.js +1952 -10
- package/bin/musubi-orchestrate.js +327 -239
- package/bin/musubi-remember.js +69 -56
- package/bin/musubi-resolve.js +53 -45
- package/bin/musubi-trace.js +51 -22
- package/bin/musubi-validate.js +39 -30
- package/bin/musubi-workflow.js +33 -34
- package/bin/musubi.js +39 -2
- package/package.json +1 -1
- package/src/agents/agent-loop.js +94 -95
- package/src/agents/agentic/code-generator.js +119 -109
- package/src/agents/agentic/code-reviewer.js +105 -108
- package/src/agents/agentic/index.js +4 -4
- package/src/agents/browser/action-executor.js +13 -13
- package/src/agents/browser/ai-comparator.js +11 -10
- package/src/agents/browser/context-manager.js +6 -6
- package/src/agents/browser/index.js +5 -5
- package/src/agents/browser/nl-parser.js +31 -46
- package/src/agents/browser/screenshot.js +2 -2
- package/src/agents/browser/test-generator.js +6 -4
- package/src/agents/function-tool.js +71 -65
- package/src/agents/index.js +7 -7
- package/src/agents/schema-generator.js +98 -94
- package/src/analyzers/ast-extractor.js +164 -145
- package/src/analyzers/codegraph-auto-update.js +858 -0
- package/src/analyzers/complexity-analyzer.js +536 -0
- package/src/analyzers/context-optimizer.js +247 -125
- package/src/analyzers/impact-analyzer.js +1 -1
- package/src/analyzers/large-project-analyzer.js +766 -0
- package/src/analyzers/repository-map.js +83 -80
- package/src/analyzers/security-analyzer.js +19 -11
- package/src/analyzers/stuck-detector.js +19 -17
- package/src/converters/index.js +78 -57
- package/src/converters/ir/types.js +12 -12
- package/src/converters/parsers/musubi-parser.js +134 -126
- package/src/converters/parsers/openapi-parser.js +70 -53
- package/src/converters/parsers/speckit-parser.js +239 -175
- package/src/converters/writers/musubi-writer.js +123 -118
- package/src/converters/writers/speckit-writer.js +124 -113
- package/src/generators/rust-migration-generator.js +512 -0
- package/src/gui/public/index.html +1365 -1211
- package/src/gui/server.js +41 -40
- package/src/gui/services/file-watcher.js +23 -8
- package/src/gui/services/project-scanner.js +26 -20
- package/src/gui/services/replanning-service.js +27 -23
- package/src/gui/services/traceability-service.js +8 -8
- package/src/gui/services/workflow-service.js +14 -7
- package/src/index.js +151 -0
- package/src/integrations/cicd.js +90 -104
- package/src/integrations/codegraph-mcp.js +643 -0
- package/src/integrations/documentation.js +142 -103
- package/src/integrations/examples.js +95 -80
- package/src/integrations/github-client.js +17 -17
- package/src/integrations/index.js +5 -5
- package/src/integrations/mcp/index.js +21 -21
- package/src/integrations/mcp/mcp-context-provider.js +76 -78
- package/src/integrations/mcp/mcp-discovery.js +74 -72
- package/src/integrations/mcp/mcp-tool-registry.js +99 -94
- package/src/integrations/mcp-connector.js +70 -66
- package/src/integrations/platforms.js +50 -49
- package/src/integrations/tool-discovery.js +37 -31
- package/src/llm-providers/anthropic-provider.js +11 -11
- package/src/llm-providers/base-provider.js +16 -18
- package/src/llm-providers/copilot-provider.js +22 -19
- package/src/llm-providers/index.js +26 -25
- package/src/llm-providers/ollama-provider.js +11 -11
- package/src/llm-providers/openai-provider.js +12 -12
- package/src/managers/agent-memory.js +36 -24
- package/src/managers/checkpoint-manager.js +4 -8
- package/src/managers/delta-spec.js +19 -19
- package/src/managers/index.js +13 -4
- package/src/managers/memory-condenser.js +35 -45
- package/src/managers/repo-skill-manager.js +57 -31
- package/src/managers/skill-loader.js +25 -22
- package/src/managers/skill-tools.js +36 -72
- package/src/managers/workflow.js +30 -22
- package/src/monitoring/cost-tracker.js +53 -44
- package/src/monitoring/incident-manager.js +123 -103
- package/src/monitoring/index.js +144 -134
- package/src/monitoring/observability.js +82 -59
- package/src/monitoring/quality-dashboard.js +51 -39
- package/src/monitoring/release-manager.js +70 -50
- package/src/orchestration/agent-skill-binding.js +39 -47
- package/src/orchestration/error-handler.js +65 -107
- package/src/orchestration/guardrails/base-guardrail.js +26 -24
- package/src/orchestration/guardrails/guardrail-rules.js +50 -64
- package/src/orchestration/guardrails/index.js +5 -5
- package/src/orchestration/guardrails/input-guardrail.js +58 -45
- package/src/orchestration/guardrails/output-guardrail.js +104 -81
- package/src/orchestration/guardrails/safety-check.js +79 -79
- package/src/orchestration/index.js +38 -55
- package/src/orchestration/mcp-tool-adapters.js +96 -99
- package/src/orchestration/orchestration-engine.js +21 -21
- package/src/orchestration/pattern-registry.js +60 -45
- package/src/orchestration/patterns/auto.js +34 -47
- package/src/orchestration/patterns/group-chat.js +59 -65
- package/src/orchestration/patterns/handoff.js +67 -65
- package/src/orchestration/patterns/human-in-loop.js +51 -72
- package/src/orchestration/patterns/nested.js +25 -40
- package/src/orchestration/patterns/sequential.js +35 -34
- package/src/orchestration/patterns/swarm.js +63 -56
- package/src/orchestration/patterns/triage.js +150 -109
- package/src/orchestration/reasoning/index.js +9 -9
- package/src/orchestration/reasoning/planning-engine.js +143 -140
- package/src/orchestration/reasoning/reasoning-engine.js +206 -144
- package/src/orchestration/reasoning/self-correction.js +121 -128
- package/src/orchestration/replanning/adaptive-goal-modifier.js +107 -112
- package/src/orchestration/replanning/alternative-generator.js +37 -42
- package/src/orchestration/replanning/config.js +63 -59
- package/src/orchestration/replanning/goal-progress-tracker.js +98 -100
- package/src/orchestration/replanning/index.js +24 -20
- package/src/orchestration/replanning/plan-evaluator.js +49 -50
- package/src/orchestration/replanning/plan-monitor.js +32 -28
- package/src/orchestration/replanning/proactive-path-optimizer.js +175 -178
- package/src/orchestration/replanning/replan-history.js +33 -26
- package/src/orchestration/replanning/replanning-engine.js +106 -108
- package/src/orchestration/skill-executor.js +107 -109
- package/src/orchestration/skill-registry.js +85 -89
- package/src/orchestration/workflow-examples.js +228 -231
- package/src/orchestration/workflow-executor.js +65 -68
- package/src/orchestration/workflow-orchestrator.js +72 -73
- package/src/phase4-integration.js +47 -40
- package/src/phase5-integration.js +89 -30
- package/src/reporters/coverage-report.js +82 -30
- package/src/reporters/hierarchical-reporter.js +498 -0
- package/src/reporters/traceability-matrix-report.js +29 -20
- package/src/resolvers/issue-resolver.js +43 -31
- package/src/steering/advanced-validation.js +133 -124
- package/src/steering/auto-updater.js +60 -73
- package/src/steering/index.js +6 -6
- package/src/steering/quality-metrics.js +41 -35
- package/src/steering/steering-auto-update.js +83 -86
- package/src/steering/steering-validator.js +98 -106
- package/src/steering/template-constraints.js +53 -54
- package/src/templates/agents/claude-code/CLAUDE.md +32 -32
- package/src/templates/agents/claude-code/skills/agent-assistant/SKILL.md +13 -5
- package/src/templates/agents/claude-code/skills/ai-ml-engineer/mlops-guide.md +23 -23
- package/src/templates/agents/claude-code/skills/ai-ml-engineer/model-card-template.md +60 -41
- package/src/templates/agents/claude-code/skills/api-designer/api-patterns.md +27 -19
- package/src/templates/agents/claude-code/skills/api-designer/openapi-template.md +11 -7
- package/src/templates/agents/claude-code/skills/bug-hunter/SKILL.md +4 -3
- package/src/templates/agents/claude-code/skills/bug-hunter/root-cause-analysis.md +37 -15
- package/src/templates/agents/claude-code/skills/change-impact-analyzer/dependency-graph-patterns.md +36 -42
- package/src/templates/agents/claude-code/skills/change-impact-analyzer/impact-analysis-template.md +69 -60
- package/src/templates/agents/claude-code/skills/cloud-architect/aws-patterns.md +31 -38
- package/src/templates/agents/claude-code/skills/cloud-architect/azure-patterns.md +28 -23
- package/src/templates/agents/claude-code/skills/code-reviewer/SKILL.md +61 -0
- package/src/templates/agents/claude-code/skills/code-reviewer/best-practices.md +27 -0
- package/src/templates/agents/claude-code/skills/code-reviewer/review-checklist.md +29 -10
- package/src/templates/agents/claude-code/skills/code-reviewer/review-standards.md +29 -24
- package/src/templates/agents/claude-code/skills/constitution-enforcer/SKILL.md +8 -6
- package/src/templates/agents/claude-code/skills/constitution-enforcer/constitutional-articles.md +62 -26
- package/src/templates/agents/claude-code/skills/constitution-enforcer/phase-minus-one-gates.md +35 -16
- package/src/templates/agents/claude-code/skills/database-administrator/backup-recovery.md +27 -17
- package/src/templates/agents/claude-code/skills/database-administrator/tuning-guide.md +25 -20
- package/src/templates/agents/claude-code/skills/database-schema-designer/schema-patterns.md +39 -22
- package/src/templates/agents/claude-code/skills/devops-engineer/ci-cd-templates.md +25 -22
- package/src/templates/agents/claude-code/skills/issue-resolver/SKILL.md +24 -21
- package/src/templates/agents/claude-code/skills/orchestrator/SKILL.md +148 -63
- package/src/templates/agents/claude-code/skills/orchestrator/patterns.md +35 -16
- package/src/templates/agents/claude-code/skills/orchestrator/selection-matrix.md +69 -64
- package/src/templates/agents/claude-code/skills/performance-engineer/optimization-playbook.md +47 -47
- package/src/templates/agents/claude-code/skills/performance-optimizer/SKILL.md +69 -0
- package/src/templates/agents/claude-code/skills/performance-optimizer/benchmark-template.md +63 -45
- package/src/templates/agents/claude-code/skills/performance-optimizer/optimization-patterns.md +33 -35
- package/src/templates/agents/claude-code/skills/project-manager/SKILL.md +7 -6
- package/src/templates/agents/claude-code/skills/project-manager/agile-ceremonies.md +47 -28
- package/src/templates/agents/claude-code/skills/project-manager/project-templates.md +94 -78
- package/src/templates/agents/claude-code/skills/quality-assurance/SKILL.md +20 -17
- package/src/templates/agents/claude-code/skills/quality-assurance/qa-plan-template.md +63 -49
- package/src/templates/agents/claude-code/skills/release-coordinator/SKILL.md +5 -5
- package/src/templates/agents/claude-code/skills/release-coordinator/feature-flag-guide.md +30 -26
- package/src/templates/agents/claude-code/skills/release-coordinator/release-plan-template.md +67 -35
- package/src/templates/agents/claude-code/skills/requirements-analyst/ears-format.md +54 -42
- package/src/templates/agents/claude-code/skills/requirements-analyst/validation-rules.md +36 -33
- package/src/templates/agents/claude-code/skills/security-auditor/SKILL.md +77 -19
- package/src/templates/agents/claude-code/skills/security-auditor/audit-checklists.md +24 -24
- package/src/templates/agents/claude-code/skills/security-auditor/owasp-top-10.md +61 -20
- package/src/templates/agents/claude-code/skills/security-auditor/vulnerability-patterns.md +43 -11
- package/src/templates/agents/claude-code/skills/site-reliability-engineer/SKILL.md +1 -0
- package/src/templates/agents/claude-code/skills/site-reliability-engineer/incident-response-template.md +55 -25
- package/src/templates/agents/claude-code/skills/site-reliability-engineer/observability-patterns.md +78 -68
- package/src/templates/agents/claude-code/skills/site-reliability-engineer/slo-sli-guide.md +73 -53
- package/src/templates/agents/claude-code/skills/software-developer/solid-principles.md +83 -37
- package/src/templates/agents/claude-code/skills/software-developer/test-first-workflow.md +38 -31
- package/src/templates/agents/claude-code/skills/steering/SKILL.md +1 -0
- package/src/templates/agents/claude-code/skills/steering/auto-update-rules.md +31 -0
- package/src/templates/agents/claude-code/skills/system-architect/adr-template.md +25 -7
- package/src/templates/agents/claude-code/skills/system-architect/c4-model-guide.md +74 -61
- package/src/templates/agents/claude-code/skills/technical-writer/doc-templates/documentation-templates.md +70 -52
- package/src/templates/agents/claude-code/skills/test-engineer/SKILL.md +2 -0
- package/src/templates/agents/claude-code/skills/test-engineer/ears-test-mapping.md +75 -71
- package/src/templates/agents/claude-code/skills/test-engineer/test-types.md +85 -63
- package/src/templates/agents/claude-code/skills/traceability-auditor/coverage-matrix-template.md +39 -36
- package/src/templates/agents/claude-code/skills/traceability-auditor/gap-detection-rules.md +22 -17
- package/src/templates/agents/claude-code/skills/ui-ux-designer/SKILL.md +1 -0
- package/src/templates/agents/claude-code/skills/ui-ux-designer/accessibility-guidelines.md +49 -75
- package/src/templates/agents/claude-code/skills/ui-ux-designer/design-system-components.md +71 -59
- package/src/templates/agents/codex/AGENTS.md +74 -42
- package/src/templates/agents/cursor/AGENTS.md +74 -42
- package/src/templates/agents/gemini-cli/GEMINI.md +74 -42
- package/src/templates/agents/github-copilot/AGENTS.md +83 -51
- package/src/templates/agents/qwen-code/QWEN.md +74 -42
- package/src/templates/agents/windsurf/AGENTS.md +74 -42
- package/src/templates/architectures/README.md +41 -0
- package/src/templates/architectures/clean-architecture/README.md +113 -0
- package/src/templates/architectures/event-driven/README.md +162 -0
- package/src/templates/architectures/hexagonal/README.md +130 -0
- package/src/templates/index.js +6 -1
- package/src/templates/locale-manager.js +16 -16
- package/src/templates/shared/delta-spec-template.md +20 -13
- package/src/templates/shared/github-actions/musubi-issue-resolver.yml +5 -5
- package/src/templates/shared/github-actions/musubi-security-check.yml +3 -3
- package/src/templates/shared/github-actions/musubi-validate.yml +4 -4
- package/src/templates/shared/steering/structure.md +95 -0
- package/src/templates/skills/browser-agent.md +21 -16
- package/src/templates/skills/web-gui.md +8 -0
- package/src/templates/template-constraints.js +50 -53
- package/src/validators/advanced-validation.js +30 -36
- package/src/validators/constitutional-validator.js +77 -73
- package/src/validators/critic-system.js +49 -59
- package/src/validators/delta-format.js +59 -55
- package/src/validators/traceability-validator.js +7 -11
|
@@ -17,7 +17,7 @@ const STRATEGY = {
|
|
|
17
17
|
TREE_OF_THOUGHT: 'tree-of-thought',
|
|
18
18
|
REFLECTION: 'reflection',
|
|
19
19
|
DECOMPOSITION: 'decomposition',
|
|
20
|
-
ANALOGY: 'analogy'
|
|
20
|
+
ANALOGY: 'analogy',
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
/**
|
|
@@ -30,7 +30,7 @@ const STEP_TYPE = {
|
|
|
30
30
|
PLAN: 'plan',
|
|
31
31
|
ACT: 'act',
|
|
32
32
|
REFLECT: 'reflect',
|
|
33
|
-
REFINE: 'refine'
|
|
33
|
+
REFINE: 'refine',
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
/**
|
|
@@ -75,20 +75,20 @@ class ReasoningEngine extends EventEmitter {
|
|
|
75
75
|
*/
|
|
76
76
|
constructor(options = {}) {
|
|
77
77
|
super();
|
|
78
|
-
|
|
78
|
+
|
|
79
79
|
this.strategy = options.strategy || STRATEGY.CHAIN_OF_THOUGHT;
|
|
80
80
|
this.maxSteps = options.maxSteps ?? 20;
|
|
81
81
|
this.minConfidence = options.minConfidence ?? 0.6;
|
|
82
82
|
this.enableReflection = options.enableReflection ?? true;
|
|
83
83
|
this.enableBacktracking = options.enableBacktracking ?? true;
|
|
84
84
|
this.timeout = options.timeout ?? 60000;
|
|
85
|
-
|
|
85
|
+
|
|
86
86
|
// State
|
|
87
87
|
this.traces = new Map();
|
|
88
88
|
this.currentTrace = null;
|
|
89
89
|
this.stepCounter = 0;
|
|
90
90
|
}
|
|
91
|
-
|
|
91
|
+
|
|
92
92
|
/**
|
|
93
93
|
* Start reasoning about a problem
|
|
94
94
|
* @param {string} problem - Problem statement
|
|
@@ -98,7 +98,7 @@ class ReasoningEngine extends EventEmitter {
|
|
|
98
98
|
async reason(problem, context = {}) {
|
|
99
99
|
const traceId = this.generateId();
|
|
100
100
|
const startTime = Date.now();
|
|
101
|
-
|
|
101
|
+
|
|
102
102
|
const trace = {
|
|
103
103
|
id: traceId,
|
|
104
104
|
problem,
|
|
@@ -106,14 +106,14 @@ class ReasoningEngine extends EventEmitter {
|
|
|
106
106
|
steps: [],
|
|
107
107
|
conclusion: null,
|
|
108
108
|
successful: false,
|
|
109
|
-
totalTime: 0
|
|
109
|
+
totalTime: 0,
|
|
110
110
|
};
|
|
111
|
-
|
|
111
|
+
|
|
112
112
|
this.currentTrace = trace;
|
|
113
113
|
this.traces.set(traceId, trace);
|
|
114
|
-
|
|
114
|
+
|
|
115
115
|
this.emit('reasoning:start', { traceId, problem, strategy: this.strategy });
|
|
116
|
-
|
|
116
|
+
|
|
117
117
|
try {
|
|
118
118
|
// Select reasoning strategy
|
|
119
119
|
let result;
|
|
@@ -136,23 +136,27 @@ class ReasoningEngine extends EventEmitter {
|
|
|
136
136
|
default:
|
|
137
137
|
result = await this.chainOfThought(problem, context);
|
|
138
138
|
}
|
|
139
|
-
|
|
139
|
+
|
|
140
140
|
trace.conclusion = result.conclusion;
|
|
141
141
|
trace.successful = result.successful;
|
|
142
|
-
|
|
143
142
|
} catch (error) {
|
|
144
|
-
this.addStep(
|
|
143
|
+
this.addStep(
|
|
144
|
+
STEP_TYPE.REFLECT,
|
|
145
|
+
`Reasoning failed: ${error.message}`,
|
|
146
|
+
{ error: error.message },
|
|
147
|
+
0
|
|
148
|
+
);
|
|
145
149
|
trace.successful = false;
|
|
146
150
|
}
|
|
147
|
-
|
|
151
|
+
|
|
148
152
|
trace.totalTime = Date.now() - startTime;
|
|
149
153
|
this.currentTrace = null;
|
|
150
|
-
|
|
154
|
+
|
|
151
155
|
this.emit('reasoning:complete', { trace });
|
|
152
|
-
|
|
156
|
+
|
|
153
157
|
return trace;
|
|
154
158
|
}
|
|
155
|
-
|
|
159
|
+
|
|
156
160
|
/**
|
|
157
161
|
* Chain of Thought reasoning
|
|
158
162
|
* @private
|
|
@@ -160,39 +164,54 @@ class ReasoningEngine extends EventEmitter {
|
|
|
160
164
|
async chainOfThought(problem, context) {
|
|
161
165
|
// Step 1: Observe
|
|
162
166
|
this.addStep(STEP_TYPE.OBSERVE, `Analyzing problem: ${problem}`, { context }, 0.9);
|
|
163
|
-
|
|
167
|
+
|
|
164
168
|
// Step 2: Decompose into sub-problems
|
|
165
169
|
const subProblems = this.decomposeProlem(problem);
|
|
166
|
-
this.addStep(
|
|
167
|
-
|
|
170
|
+
this.addStep(
|
|
171
|
+
STEP_TYPE.THINK,
|
|
172
|
+
`Identified ${subProblems.length} sub-problems:\n${subProblems.map((p, i) => `${i + 1}. ${p}`).join('\n')}`,
|
|
173
|
+
{ subProblems },
|
|
174
|
+
0.8
|
|
175
|
+
);
|
|
176
|
+
|
|
168
177
|
// Step 3: Process each sub-problem
|
|
169
178
|
const solutions = [];
|
|
170
179
|
for (const subProblem of subProblems) {
|
|
171
180
|
this.addStep(STEP_TYPE.THINK, `Working on: ${subProblem}`, {}, 0.7);
|
|
172
|
-
|
|
181
|
+
|
|
173
182
|
const solution = await this.solveSubProblem(subProblem, context);
|
|
174
183
|
solutions.push({ problem: subProblem, solution });
|
|
175
|
-
|
|
184
|
+
|
|
176
185
|
this.addStep(STEP_TYPE.ACT, `Solution: ${solution}`, { subProblem, solution }, 0.8);
|
|
177
186
|
}
|
|
178
|
-
|
|
187
|
+
|
|
179
188
|
// Step 4: Synthesize
|
|
180
189
|
const conclusion = this.synthesize(solutions);
|
|
181
|
-
this.addStep(
|
|
182
|
-
|
|
190
|
+
this.addStep(
|
|
191
|
+
STEP_TYPE.PLAN,
|
|
192
|
+
`Synthesizing solutions into final answer`,
|
|
193
|
+
{ solutionCount: solutions.length },
|
|
194
|
+
0.85
|
|
195
|
+
);
|
|
196
|
+
|
|
183
197
|
// Step 5: Reflect if enabled
|
|
184
198
|
if (this.enableReflection) {
|
|
185
199
|
const reflection = this.reflect(problem, conclusion, solutions);
|
|
186
|
-
this.addStep(
|
|
187
|
-
|
|
200
|
+
this.addStep(
|
|
201
|
+
STEP_TYPE.REFLECT,
|
|
202
|
+
reflection.content,
|
|
203
|
+
{ confidence: reflection.confidence },
|
|
204
|
+
reflection.confidence
|
|
205
|
+
);
|
|
206
|
+
|
|
188
207
|
if (reflection.confidence < this.minConfidence && this.enableBacktracking) {
|
|
189
208
|
return this.backtrack(problem, context, reflection);
|
|
190
209
|
}
|
|
191
210
|
}
|
|
192
|
-
|
|
211
|
+
|
|
193
212
|
return { conclusion, successful: true };
|
|
194
213
|
}
|
|
195
|
-
|
|
214
|
+
|
|
196
215
|
/**
|
|
197
216
|
* Tree of Thought reasoning - explore multiple paths
|
|
198
217
|
* @private
|
|
@@ -200,43 +219,58 @@ class ReasoningEngine extends EventEmitter {
|
|
|
200
219
|
async treeOfThought(problem, context) {
|
|
201
220
|
const branches = [];
|
|
202
221
|
const maxBranches = 3;
|
|
203
|
-
|
|
222
|
+
|
|
204
223
|
this.addStep(STEP_TYPE.OBSERVE, `Exploring multiple reasoning paths for: ${problem}`, {}, 0.9);
|
|
205
|
-
|
|
224
|
+
|
|
206
225
|
// Generate multiple initial thoughts
|
|
207
226
|
const initialThoughts = this.generateThoughts(problem, maxBranches);
|
|
208
|
-
this.addStep(
|
|
209
|
-
|
|
227
|
+
this.addStep(
|
|
228
|
+
STEP_TYPE.THINK,
|
|
229
|
+
`Generated ${initialThoughts.length} reasoning branches`,
|
|
230
|
+
{ branches: initialThoughts },
|
|
231
|
+
0.8
|
|
232
|
+
);
|
|
233
|
+
|
|
210
234
|
// Explore each branch
|
|
211
235
|
for (const thought of initialThoughts) {
|
|
212
236
|
const branch = {
|
|
213
237
|
thought,
|
|
214
238
|
steps: [],
|
|
215
239
|
conclusion: null,
|
|
216
|
-
score: 0
|
|
240
|
+
score: 0,
|
|
217
241
|
};
|
|
218
|
-
|
|
242
|
+
|
|
219
243
|
this.addStep(STEP_TYPE.PLAN, `Exploring branch: ${thought}`, {}, 0.7);
|
|
220
|
-
|
|
244
|
+
|
|
221
245
|
// Develop this branch
|
|
222
246
|
const developed = await this.developBranch(thought, problem, context);
|
|
223
247
|
branch.steps = developed.steps;
|
|
224
248
|
branch.conclusion = developed.conclusion;
|
|
225
249
|
branch.score = this.evaluateBranch(branch);
|
|
226
|
-
|
|
250
|
+
|
|
227
251
|
branches.push(branch);
|
|
228
|
-
this.addStep(
|
|
252
|
+
this.addStep(
|
|
253
|
+
STEP_TYPE.REFLECT,
|
|
254
|
+
`Branch score: ${branch.score.toFixed(2)}`,
|
|
255
|
+
{ conclusion: branch.conclusion },
|
|
256
|
+
branch.score
|
|
257
|
+
);
|
|
229
258
|
}
|
|
230
|
-
|
|
259
|
+
|
|
231
260
|
// Select best branch
|
|
232
261
|
branches.sort((a, b) => b.score - a.score);
|
|
233
262
|
const best = branches[0];
|
|
234
|
-
|
|
235
|
-
this.addStep(
|
|
236
|
-
|
|
263
|
+
|
|
264
|
+
this.addStep(
|
|
265
|
+
STEP_TYPE.ACT,
|
|
266
|
+
`Selected best reasoning path with score ${best.score.toFixed(2)}`,
|
|
267
|
+
{ conclusion: best.conclusion },
|
|
268
|
+
best.score
|
|
269
|
+
);
|
|
270
|
+
|
|
237
271
|
return { conclusion: best.conclusion, successful: best.score >= this.minConfidence };
|
|
238
272
|
}
|
|
239
|
-
|
|
273
|
+
|
|
240
274
|
/**
|
|
241
275
|
* Reflective reasoning - iterate and improve
|
|
242
276
|
* @private
|
|
@@ -245,81 +279,111 @@ class ReasoningEngine extends EventEmitter {
|
|
|
245
279
|
let currentSolution = null;
|
|
246
280
|
let iterations = 0;
|
|
247
281
|
const maxIterations = 3;
|
|
248
|
-
|
|
282
|
+
|
|
249
283
|
this.addStep(STEP_TYPE.OBSERVE, `Starting reflective reasoning for: ${problem}`, {}, 0.9);
|
|
250
|
-
|
|
284
|
+
|
|
251
285
|
while (iterations < maxIterations) {
|
|
252
286
|
iterations++;
|
|
253
|
-
|
|
287
|
+
|
|
254
288
|
// Generate or refine solution
|
|
255
289
|
if (!currentSolution) {
|
|
256
290
|
currentSolution = await this.generateInitialSolution(problem, context);
|
|
257
|
-
this.addStep(
|
|
291
|
+
this.addStep(
|
|
292
|
+
STEP_TYPE.THINK,
|
|
293
|
+
`Initial solution: ${currentSolution}`,
|
|
294
|
+
{ iteration: iterations },
|
|
295
|
+
0.7
|
|
296
|
+
);
|
|
258
297
|
} else {
|
|
259
298
|
currentSolution = await this.refineSolution(currentSolution, problem, context);
|
|
260
|
-
this.addStep(
|
|
299
|
+
this.addStep(
|
|
300
|
+
STEP_TYPE.REFINE,
|
|
301
|
+
`Refined solution: ${currentSolution}`,
|
|
302
|
+
{ iteration: iterations },
|
|
303
|
+
0.8
|
|
304
|
+
);
|
|
261
305
|
}
|
|
262
|
-
|
|
306
|
+
|
|
263
307
|
// Reflect on solution
|
|
264
308
|
const reflection = this.reflect(problem, currentSolution, []);
|
|
265
|
-
this.addStep(
|
|
266
|
-
|
|
309
|
+
this.addStep(
|
|
310
|
+
STEP_TYPE.REFLECT,
|
|
311
|
+
reflection.content,
|
|
312
|
+
{ confidence: reflection.confidence },
|
|
313
|
+
reflection.confidence
|
|
314
|
+
);
|
|
315
|
+
|
|
267
316
|
if (reflection.confidence >= this.minConfidence) {
|
|
268
317
|
return { conclusion: currentSolution, successful: true };
|
|
269
318
|
}
|
|
270
319
|
}
|
|
271
|
-
|
|
320
|
+
|
|
272
321
|
return { conclusion: currentSolution, successful: false };
|
|
273
322
|
}
|
|
274
|
-
|
|
323
|
+
|
|
275
324
|
/**
|
|
276
325
|
* Decomposition reasoning - divide and conquer
|
|
277
326
|
* @private
|
|
278
327
|
*/
|
|
279
328
|
async decompositionReasoning(problem, context) {
|
|
280
329
|
this.addStep(STEP_TYPE.OBSERVE, `Decomposing problem: ${problem}`, {}, 0.9);
|
|
281
|
-
|
|
330
|
+
|
|
282
331
|
// Hierarchical decomposition
|
|
283
332
|
const decomposition = this.hierarchicalDecompose(problem);
|
|
284
|
-
this.addStep(
|
|
285
|
-
|
|
333
|
+
this.addStep(
|
|
334
|
+
STEP_TYPE.PLAN,
|
|
335
|
+
`Problem decomposition:\n${this.formatDecomposition(decomposition)}`,
|
|
336
|
+
{ decomposition },
|
|
337
|
+
0.85
|
|
338
|
+
);
|
|
339
|
+
|
|
286
340
|
// Solve bottom-up
|
|
287
341
|
const solved = await this.solveBottomUp(decomposition, context);
|
|
288
|
-
this.addStep(
|
|
289
|
-
|
|
342
|
+
this.addStep(
|
|
343
|
+
STEP_TYPE.ACT,
|
|
344
|
+
`Solved ${solved.solvedCount} sub-problems`,
|
|
345
|
+
{ solved: solved.results },
|
|
346
|
+
0.8
|
|
347
|
+
);
|
|
348
|
+
|
|
290
349
|
// Compose solution
|
|
291
350
|
const conclusion = this.composeSolution(solved.results);
|
|
292
351
|
this.addStep(STEP_TYPE.THINK, `Composed final solution`, { conclusion }, 0.85);
|
|
293
|
-
|
|
352
|
+
|
|
294
353
|
return { conclusion, successful: true };
|
|
295
354
|
}
|
|
296
|
-
|
|
355
|
+
|
|
297
356
|
/**
|
|
298
357
|
* Analogy reasoning - use similar cases
|
|
299
358
|
* @private
|
|
300
359
|
*/
|
|
301
360
|
async analogyReasoning(problem, context) {
|
|
302
361
|
this.addStep(STEP_TYPE.OBSERVE, `Finding analogies for: ${problem}`, {}, 0.9);
|
|
303
|
-
|
|
362
|
+
|
|
304
363
|
// Find similar cases
|
|
305
364
|
const analogies = this.findAnalogies(problem, context);
|
|
306
365
|
this.addStep(STEP_TYPE.THINK, `Found ${analogies.length} similar cases`, { analogies }, 0.8);
|
|
307
|
-
|
|
366
|
+
|
|
308
367
|
if (analogies.length === 0) {
|
|
309
|
-
this.addStep(
|
|
368
|
+
this.addStep(
|
|
369
|
+
STEP_TYPE.REFLECT,
|
|
370
|
+
'No suitable analogies found, falling back to chain of thought',
|
|
371
|
+
{},
|
|
372
|
+
0.5
|
|
373
|
+
);
|
|
310
374
|
return this.chainOfThought(problem, context);
|
|
311
375
|
}
|
|
312
|
-
|
|
376
|
+
|
|
313
377
|
// Apply best analogy
|
|
314
378
|
const best = analogies[0];
|
|
315
379
|
this.addStep(STEP_TYPE.PLAN, `Applying analogy: ${best.description}`, { analogy: best }, 0.75);
|
|
316
|
-
|
|
380
|
+
|
|
317
381
|
const conclusion = this.applyAnalogy(best, problem, context);
|
|
318
382
|
this.addStep(STEP_TYPE.ACT, `Derived solution from analogy`, { conclusion }, 0.8);
|
|
319
|
-
|
|
383
|
+
|
|
320
384
|
return { conclusion, successful: true };
|
|
321
385
|
}
|
|
322
|
-
|
|
386
|
+
|
|
323
387
|
/**
|
|
324
388
|
* Add a reasoning step
|
|
325
389
|
* @param {string} type - Step type
|
|
@@ -329,25 +393,25 @@ class ReasoningEngine extends EventEmitter {
|
|
|
329
393
|
*/
|
|
330
394
|
addStep(type, content, metadata = {}, confidence = 0.5) {
|
|
331
395
|
if (!this.currentTrace) return;
|
|
332
|
-
|
|
396
|
+
|
|
333
397
|
const step = {
|
|
334
398
|
id: `step-${++this.stepCounter}`,
|
|
335
399
|
type,
|
|
336
400
|
content,
|
|
337
401
|
metadata,
|
|
338
402
|
confidence,
|
|
339
|
-
timestamp: Date.now()
|
|
403
|
+
timestamp: Date.now(),
|
|
340
404
|
};
|
|
341
|
-
|
|
405
|
+
|
|
342
406
|
this.currentTrace.steps.push(step);
|
|
343
407
|
this.emit('reasoning:step', { step, traceId: this.currentTrace.id });
|
|
344
|
-
|
|
408
|
+
|
|
345
409
|
// Check step limit
|
|
346
410
|
if (this.currentTrace.steps.length >= this.maxSteps) {
|
|
347
411
|
throw new Error('Maximum reasoning steps exceeded');
|
|
348
412
|
}
|
|
349
413
|
}
|
|
350
|
-
|
|
414
|
+
|
|
351
415
|
/**
|
|
352
416
|
* Decompose problem into sub-problems
|
|
353
417
|
* @private
|
|
@@ -355,11 +419,11 @@ class ReasoningEngine extends EventEmitter {
|
|
|
355
419
|
decomposeProlem(problem) {
|
|
356
420
|
// Simple keyword-based decomposition
|
|
357
421
|
const parts = [];
|
|
358
|
-
|
|
422
|
+
|
|
359
423
|
// Look for conjunction patterns
|
|
360
424
|
const conjunctions = ['and', 'then', 'also', 'additionally', 'furthermore'];
|
|
361
425
|
let remaining = problem;
|
|
362
|
-
|
|
426
|
+
|
|
363
427
|
for (const conj of conjunctions) {
|
|
364
428
|
const regex = new RegExp(`\\s+${conj}\\s+`, 'gi');
|
|
365
429
|
if (regex.test(remaining)) {
|
|
@@ -369,26 +433,24 @@ class ReasoningEngine extends EventEmitter {
|
|
|
369
433
|
break;
|
|
370
434
|
}
|
|
371
435
|
}
|
|
372
|
-
|
|
436
|
+
|
|
373
437
|
if (remaining) {
|
|
374
438
|
parts.push(remaining);
|
|
375
439
|
}
|
|
376
|
-
|
|
440
|
+
|
|
377
441
|
// Clean up and filter
|
|
378
|
-
return parts
|
|
379
|
-
.map(p => p.trim())
|
|
380
|
-
.filter(p => p.length > 0);
|
|
442
|
+
return parts.map(p => p.trim()).filter(p => p.length > 0);
|
|
381
443
|
}
|
|
382
|
-
|
|
444
|
+
|
|
383
445
|
/**
|
|
384
446
|
* Solve a sub-problem
|
|
385
447
|
* @private
|
|
386
448
|
*/
|
|
387
|
-
async solveSubProblem(subProblem,
|
|
449
|
+
async solveSubProblem(subProblem, _context) {
|
|
388
450
|
// Simplified solution generation
|
|
389
451
|
return `Resolved: ${subProblem.substring(0, 50)}...`;
|
|
390
452
|
}
|
|
391
|
-
|
|
453
|
+
|
|
392
454
|
/**
|
|
393
455
|
* Synthesize solutions
|
|
394
456
|
* @private
|
|
@@ -396,10 +458,10 @@ class ReasoningEngine extends EventEmitter {
|
|
|
396
458
|
synthesize(solutions) {
|
|
397
459
|
if (solutions.length === 0) return 'No solutions found';
|
|
398
460
|
if (solutions.length === 1) return solutions[0].solution;
|
|
399
|
-
|
|
461
|
+
|
|
400
462
|
return solutions.map(s => s.solution).join(' → ');
|
|
401
463
|
}
|
|
402
|
-
|
|
464
|
+
|
|
403
465
|
/**
|
|
404
466
|
* Reflect on solution
|
|
405
467
|
* @private
|
|
@@ -408,40 +470,45 @@ class ReasoningEngine extends EventEmitter {
|
|
|
408
470
|
// Simple reflection logic
|
|
409
471
|
const hasConclusion = conclusion && conclusion.length > 0;
|
|
410
472
|
const hasMultipleSolutions = solutions.length > 1;
|
|
411
|
-
|
|
473
|
+
|
|
412
474
|
let confidence = 0.5;
|
|
413
475
|
let content = 'Analyzing solution quality...';
|
|
414
|
-
|
|
476
|
+
|
|
415
477
|
if (hasConclusion) {
|
|
416
478
|
confidence += 0.2;
|
|
417
479
|
content = 'Solution generated successfully.';
|
|
418
480
|
}
|
|
419
|
-
|
|
481
|
+
|
|
420
482
|
if (hasMultipleSolutions) {
|
|
421
483
|
confidence += 0.1;
|
|
422
484
|
content += ' Multiple approaches considered.';
|
|
423
485
|
}
|
|
424
|
-
|
|
486
|
+
|
|
425
487
|
// Check if conclusion addresses the problem
|
|
426
488
|
const problemWords = problem.toLowerCase().split(/\s+/);
|
|
427
489
|
const conclusionWords = (conclusion || '').toLowerCase().split(/\s+/);
|
|
428
490
|
const overlap = problemWords.filter(w => conclusionWords.includes(w)).length;
|
|
429
|
-
|
|
491
|
+
|
|
430
492
|
if (overlap > 2) {
|
|
431
493
|
confidence += 0.1;
|
|
432
494
|
content += ' Conclusion appears relevant to problem.';
|
|
433
495
|
}
|
|
434
|
-
|
|
496
|
+
|
|
435
497
|
return { confidence: Math.min(confidence, 1), content };
|
|
436
498
|
}
|
|
437
|
-
|
|
499
|
+
|
|
438
500
|
/**
|
|
439
501
|
* Backtrack and try alternative approach
|
|
440
502
|
* @private
|
|
441
503
|
*/
|
|
442
504
|
async backtrack(problem, context, reflection) {
|
|
443
|
-
this.addStep(
|
|
444
|
-
|
|
505
|
+
this.addStep(
|
|
506
|
+
STEP_TYPE.REFLECT,
|
|
507
|
+
`Confidence too low (${reflection.confidence.toFixed(2)}), backtracking...`,
|
|
508
|
+
{},
|
|
509
|
+
reflection.confidence
|
|
510
|
+
);
|
|
511
|
+
|
|
445
512
|
// Try decomposition as fallback
|
|
446
513
|
const decomposition = this.decomposeProlem(problem);
|
|
447
514
|
if (decomposition.length > 1) {
|
|
@@ -453,44 +520,40 @@ class ReasoningEngine extends EventEmitter {
|
|
|
453
520
|
const conclusion = this.synthesize(solutions);
|
|
454
521
|
return { conclusion, successful: true };
|
|
455
522
|
}
|
|
456
|
-
|
|
523
|
+
|
|
457
524
|
return { conclusion: 'Unable to reach confident solution', successful: false };
|
|
458
525
|
}
|
|
459
|
-
|
|
526
|
+
|
|
460
527
|
/**
|
|
461
528
|
* Generate multiple initial thoughts
|
|
462
529
|
* @private
|
|
463
530
|
*/
|
|
464
531
|
generateThoughts(problem, count) {
|
|
465
532
|
const thoughts = [];
|
|
466
|
-
const approaches = [
|
|
467
|
-
|
|
468
|
-
'Step-by-step: ',
|
|
469
|
-
'Alternative perspective: '
|
|
470
|
-
];
|
|
471
|
-
|
|
533
|
+
const approaches = ['Direct approach: ', 'Step-by-step: ', 'Alternative perspective: '];
|
|
534
|
+
|
|
472
535
|
for (let i = 0; i < Math.min(count, approaches.length); i++) {
|
|
473
536
|
thoughts.push(`${approaches[i]}${problem.substring(0, 50)}`);
|
|
474
537
|
}
|
|
475
|
-
|
|
538
|
+
|
|
476
539
|
return thoughts;
|
|
477
540
|
}
|
|
478
|
-
|
|
541
|
+
|
|
479
542
|
/**
|
|
480
543
|
* Develop a reasoning branch
|
|
481
544
|
* @private
|
|
482
545
|
*/
|
|
483
|
-
async developBranch(thought,
|
|
546
|
+
async developBranch(thought, _problem, _context) {
|
|
484
547
|
const steps = [];
|
|
485
|
-
|
|
548
|
+
|
|
486
549
|
steps.push({ content: `Developing: ${thought}`, type: 'develop' });
|
|
487
550
|
steps.push({ content: `Analyzing implications`, type: 'analyze' });
|
|
488
|
-
|
|
551
|
+
|
|
489
552
|
const conclusion = `Branch conclusion for: ${thought.substring(0, 30)}`;
|
|
490
|
-
|
|
553
|
+
|
|
491
554
|
return { steps, conclusion };
|
|
492
555
|
}
|
|
493
|
-
|
|
556
|
+
|
|
494
557
|
/**
|
|
495
558
|
* Evaluate a reasoning branch
|
|
496
559
|
* @private
|
|
@@ -498,30 +561,30 @@ class ReasoningEngine extends EventEmitter {
|
|
|
498
561
|
evaluateBranch(branch) {
|
|
499
562
|
// Simple scoring based on step count and conclusion
|
|
500
563
|
let score = 0.5;
|
|
501
|
-
|
|
564
|
+
|
|
502
565
|
if (branch.conclusion) score += 0.2;
|
|
503
566
|
if (branch.steps.length > 0) score += 0.1;
|
|
504
567
|
if (branch.steps.length > 2) score += 0.1;
|
|
505
|
-
|
|
568
|
+
|
|
506
569
|
return Math.min(score, 1);
|
|
507
570
|
}
|
|
508
|
-
|
|
571
|
+
|
|
509
572
|
/**
|
|
510
573
|
* Generate initial solution
|
|
511
574
|
* @private
|
|
512
575
|
*/
|
|
513
|
-
async generateInitialSolution(problem,
|
|
576
|
+
async generateInitialSolution(problem, _context) {
|
|
514
577
|
return `Initial approach to: ${problem.substring(0, 50)}`;
|
|
515
578
|
}
|
|
516
|
-
|
|
579
|
+
|
|
517
580
|
/**
|
|
518
581
|
* Refine existing solution
|
|
519
582
|
* @private
|
|
520
583
|
*/
|
|
521
|
-
async refineSolution(solution,
|
|
584
|
+
async refineSolution(solution, _problem, _context) {
|
|
522
585
|
return `Refined: ${solution}`;
|
|
523
586
|
}
|
|
524
|
-
|
|
587
|
+
|
|
525
588
|
/**
|
|
526
589
|
* Hierarchical decomposition
|
|
527
590
|
* @private
|
|
@@ -531,11 +594,11 @@ class ReasoningEngine extends EventEmitter {
|
|
|
531
594
|
root: problem,
|
|
532
595
|
children: this.decomposeProlem(problem).map(p => ({
|
|
533
596
|
content: p,
|
|
534
|
-
children: []
|
|
535
|
-
}))
|
|
597
|
+
children: [],
|
|
598
|
+
})),
|
|
536
599
|
};
|
|
537
600
|
}
|
|
538
|
-
|
|
601
|
+
|
|
539
602
|
/**
|
|
540
603
|
* Format decomposition for display
|
|
541
604
|
* @private
|
|
@@ -543,23 +606,23 @@ class ReasoningEngine extends EventEmitter {
|
|
|
543
606
|
formatDecomposition(decomposition, indent = 0) {
|
|
544
607
|
const prefix = ' '.repeat(indent);
|
|
545
608
|
let result = `${prefix}└ ${decomposition.root || decomposition.content}`;
|
|
546
|
-
|
|
609
|
+
|
|
547
610
|
if (decomposition.children) {
|
|
548
611
|
for (const child of decomposition.children) {
|
|
549
612
|
result += '\n' + this.formatDecomposition(child, indent + 1);
|
|
550
613
|
}
|
|
551
614
|
}
|
|
552
|
-
|
|
615
|
+
|
|
553
616
|
return result;
|
|
554
617
|
}
|
|
555
|
-
|
|
618
|
+
|
|
556
619
|
/**
|
|
557
620
|
* Solve problems bottom-up
|
|
558
621
|
* @private
|
|
559
622
|
*/
|
|
560
623
|
async solveBottomUp(decomposition, context) {
|
|
561
624
|
const results = [];
|
|
562
|
-
|
|
625
|
+
|
|
563
626
|
// Solve leaf nodes first
|
|
564
627
|
if (decomposition.children) {
|
|
565
628
|
for (const child of decomposition.children) {
|
|
@@ -567,10 +630,10 @@ class ReasoningEngine extends EventEmitter {
|
|
|
567
630
|
results.push({ problem: child.content, solution });
|
|
568
631
|
}
|
|
569
632
|
}
|
|
570
|
-
|
|
633
|
+
|
|
571
634
|
return { results, solvedCount: results.length };
|
|
572
635
|
}
|
|
573
|
-
|
|
636
|
+
|
|
574
637
|
/**
|
|
575
638
|
* Compose solution from parts
|
|
576
639
|
* @private
|
|
@@ -579,25 +642,25 @@ class ReasoningEngine extends EventEmitter {
|
|
|
579
642
|
if (results.length === 0) return 'No solution components';
|
|
580
643
|
return results.map(r => r.solution).join('; ');
|
|
581
644
|
}
|
|
582
|
-
|
|
645
|
+
|
|
583
646
|
/**
|
|
584
647
|
* Find analogies for problem
|
|
585
648
|
* @private
|
|
586
649
|
*/
|
|
587
|
-
findAnalogies(
|
|
650
|
+
findAnalogies(_problem, _context) {
|
|
588
651
|
// Placeholder for analogy retrieval
|
|
589
652
|
// In real implementation, would search knowledge base
|
|
590
653
|
return [];
|
|
591
654
|
}
|
|
592
|
-
|
|
655
|
+
|
|
593
656
|
/**
|
|
594
657
|
* Apply analogy to derive solution
|
|
595
658
|
* @private
|
|
596
659
|
*/
|
|
597
|
-
applyAnalogy(analogy,
|
|
660
|
+
applyAnalogy(analogy, _problem, _context) {
|
|
598
661
|
return `Applied ${analogy.description} to derive solution`;
|
|
599
662
|
}
|
|
600
|
-
|
|
663
|
+
|
|
601
664
|
/**
|
|
602
665
|
* Generate unique ID
|
|
603
666
|
* @private
|
|
@@ -605,7 +668,7 @@ class ReasoningEngine extends EventEmitter {
|
|
|
605
668
|
generateId() {
|
|
606
669
|
return `trace-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
607
670
|
}
|
|
608
|
-
|
|
671
|
+
|
|
609
672
|
/**
|
|
610
673
|
* Get reasoning trace by ID
|
|
611
674
|
* @param {string} traceId - Trace identifier
|
|
@@ -614,7 +677,7 @@ class ReasoningEngine extends EventEmitter {
|
|
|
614
677
|
getTrace(traceId) {
|
|
615
678
|
return this.traces.get(traceId) || null;
|
|
616
679
|
}
|
|
617
|
-
|
|
680
|
+
|
|
618
681
|
/**
|
|
619
682
|
* Get all traces
|
|
620
683
|
* @returns {ReasoningTrace[]}
|
|
@@ -622,7 +685,7 @@ class ReasoningEngine extends EventEmitter {
|
|
|
622
685
|
getAllTraces() {
|
|
623
686
|
return Array.from(this.traces.values());
|
|
624
687
|
}
|
|
625
|
-
|
|
688
|
+
|
|
626
689
|
/**
|
|
627
690
|
* Clear all traces
|
|
628
691
|
*/
|
|
@@ -630,7 +693,7 @@ class ReasoningEngine extends EventEmitter {
|
|
|
630
693
|
this.traces.clear();
|
|
631
694
|
this.stepCounter = 0;
|
|
632
695
|
}
|
|
633
|
-
|
|
696
|
+
|
|
634
697
|
/**
|
|
635
698
|
* Get statistics
|
|
636
699
|
* @returns {Object}
|
|
@@ -639,20 +702,19 @@ class ReasoningEngine extends EventEmitter {
|
|
|
639
702
|
const traces = this.getAllTraces();
|
|
640
703
|
const successful = traces.filter(t => t.successful).length;
|
|
641
704
|
const totalSteps = traces.reduce((sum, t) => sum + t.steps.length, 0);
|
|
642
|
-
const avgTime =
|
|
643
|
-
? traces.reduce((sum, t) => sum + t.totalTime, 0) / traces.length
|
|
644
|
-
|
|
645
|
-
|
|
705
|
+
const avgTime =
|
|
706
|
+
traces.length > 0 ? traces.reduce((sum, t) => sum + t.totalTime, 0) / traces.length : 0;
|
|
707
|
+
|
|
646
708
|
return {
|
|
647
709
|
totalTraces: traces.length,
|
|
648
710
|
successfulTraces: successful,
|
|
649
711
|
successRate: traces.length > 0 ? successful / traces.length : 0,
|
|
650
712
|
totalSteps,
|
|
651
713
|
averageStepsPerTrace: traces.length > 0 ? totalSteps / traces.length : 0,
|
|
652
|
-
averageTime: avgTime
|
|
714
|
+
averageTime: avgTime,
|
|
653
715
|
};
|
|
654
716
|
}
|
|
655
|
-
|
|
717
|
+
|
|
656
718
|
/**
|
|
657
719
|
* Export trace to readable format
|
|
658
720
|
* @param {string} traceId - Trace identifier
|
|
@@ -661,22 +723,22 @@ class ReasoningEngine extends EventEmitter {
|
|
|
661
723
|
exportTrace(traceId) {
|
|
662
724
|
const trace = this.getTrace(traceId);
|
|
663
725
|
if (!trace) return '';
|
|
664
|
-
|
|
726
|
+
|
|
665
727
|
let output = `# Reasoning Trace: ${trace.id}\n\n`;
|
|
666
728
|
output += `**Problem:** ${trace.problem}\n\n`;
|
|
667
729
|
output += `**Strategy:** ${trace.strategy}\n\n`;
|
|
668
730
|
output += `## Steps\n\n`;
|
|
669
|
-
|
|
731
|
+
|
|
670
732
|
for (const step of trace.steps) {
|
|
671
733
|
output += `### ${step.type.toUpperCase()} (confidence: ${step.confidence.toFixed(2)})\n`;
|
|
672
734
|
output += `${step.content}\n\n`;
|
|
673
735
|
}
|
|
674
|
-
|
|
736
|
+
|
|
675
737
|
output += `## Conclusion\n\n`;
|
|
676
738
|
output += `${trace.conclusion || 'No conclusion reached'}\n\n`;
|
|
677
739
|
output += `**Successful:** ${trace.successful ? 'Yes' : 'No'}\n`;
|
|
678
740
|
output += `**Total Time:** ${trace.totalTime}ms\n`;
|
|
679
|
-
|
|
741
|
+
|
|
680
742
|
return output;
|
|
681
743
|
}
|
|
682
744
|
}
|
|
@@ -706,5 +768,5 @@ module.exports = {
|
|
|
706
768
|
createReasoningEngine,
|
|
707
769
|
reason,
|
|
708
770
|
STRATEGY,
|
|
709
|
-
STEP_TYPE
|
|
771
|
+
STEP_TYPE,
|
|
710
772
|
};
|