@gotza02/seq-thinking 1.1.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 +149 -0
- package/SYSTEM_INSTRUCTIONS.md +50 -0
- package/agents_test.log +15 -0
- package/dist/__tests__/agents.test.d.ts +2 -0
- package/dist/__tests__/agents.test.d.ts.map +1 -0
- package/dist/__tests__/agents.test.js +673 -0
- package/dist/__tests__/agents.test.js.map +1 -0
- package/dist/__tests__/mcp-server.test.d.ts +2 -0
- package/dist/__tests__/mcp-server.test.d.ts.map +1 -0
- package/dist/__tests__/mcp-server.test.js +315 -0
- package/dist/__tests__/mcp-server.test.js.map +1 -0
- package/dist/__tests__/sequential-thinking.test.d.ts +2 -0
- package/dist/__tests__/sequential-thinking.test.d.ts.map +1 -0
- package/dist/__tests__/sequential-thinking.test.js +545 -0
- package/dist/__tests__/sequential-thinking.test.js.map +1 -0
- package/dist/__tests__/swarm-coordinator.test.d.ts +2 -0
- package/dist/__tests__/swarm-coordinator.test.d.ts.map +1 -0
- package/dist/__tests__/swarm-coordinator.test.js +606 -0
- package/dist/__tests__/swarm-coordinator.test.js.map +1 -0
- package/dist/__tests__/types.test.d.ts +2 -0
- package/dist/__tests__/types.test.d.ts.map +1 -0
- package/dist/__tests__/types.test.js +741 -0
- package/dist/__tests__/types.test.js.map +1 -0
- package/dist/__tests__/utils.test.d.ts +2 -0
- package/dist/__tests__/utils.test.d.ts.map +1 -0
- package/dist/__tests__/utils.test.js +264 -0
- package/dist/__tests__/utils.test.js.map +1 -0
- package/dist/agents/base-agent.d.ts +126 -0
- package/dist/agents/base-agent.d.ts.map +1 -0
- package/dist/agents/base-agent.js +214 -0
- package/dist/agents/base-agent.js.map +1 -0
- package/dist/agents/critic-agent.d.ts +134 -0
- package/dist/agents/critic-agent.d.ts.map +1 -0
- package/dist/agents/critic-agent.js +484 -0
- package/dist/agents/critic-agent.js.map +1 -0
- package/dist/agents/index.d.ts +11 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +11 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/meta-reasoning-agent.d.ts +143 -0
- package/dist/agents/meta-reasoning-agent.d.ts.map +1 -0
- package/dist/agents/meta-reasoning-agent.js +532 -0
- package/dist/agents/meta-reasoning-agent.js.map +1 -0
- package/dist/agents/reasoner-agent.d.ts +75 -0
- package/dist/agents/reasoner-agent.d.ts.map +1 -0
- package/dist/agents/reasoner-agent.js +226 -0
- package/dist/agents/reasoner-agent.js.map +1 -0
- package/dist/agents/synthesizer-agent.d.ts +174 -0
- package/dist/agents/synthesizer-agent.d.ts.map +1 -0
- package/dist/agents/synthesizer-agent.js +583 -0
- package/dist/agents/synthesizer-agent.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp-server.d.ts +823 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +377 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/sequential-thinking.d.ts +91 -0
- package/dist/sequential-thinking.d.ts.map +1 -0
- package/dist/sequential-thinking.js +540 -0
- package/dist/sequential-thinking.js.map +1 -0
- package/dist/swarm-coordinator.d.ts +188 -0
- package/dist/swarm-coordinator.d.ts.map +1 -0
- package/dist/swarm-coordinator.js +627 -0
- package/dist/swarm-coordinator.js.map +1 -0
- package/dist/types/index.d.ts +806 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +279 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.d.ts +421 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +864 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/llm-adapter.d.ts +23 -0
- package/dist/utils/llm-adapter.d.ts.map +1 -0
- package/dist/utils/llm-adapter.js +68 -0
- package/dist/utils/llm-adapter.js.map +1 -0
- package/dist/utils/persistence.d.ts +33 -0
- package/dist/utils/persistence.d.ts.map +1 -0
- package/dist/utils/persistence.js +108 -0
- package/dist/utils/persistence.js.map +1 -0
- package/package.json +41 -0
- package/src/__tests__/agents.test.ts +858 -0
- package/src/__tests__/mcp-server.test.ts +380 -0
- package/src/__tests__/sequential-thinking.test.ts +687 -0
- package/src/__tests__/swarm-coordinator.test.ts +903 -0
- package/src/__tests__/types.test.ts +839 -0
- package/src/__tests__/utils.test.ts +322 -0
- package/src/agents/base-agent.ts +285 -0
- package/src/agents/critic-agent.ts +582 -0
- package/src/agents/index.ts +11 -0
- package/src/agents/meta-reasoning-agent.ts +672 -0
- package/src/agents/reasoner-agent.ts +312 -0
- package/src/agents/synthesizer-agent.ts +758 -0
- package/src/index.ts +118 -0
- package/src/mcp-server.ts +387 -0
- package/src/sequential-thinking.ts +560 -0
- package/src/swarm-coordinator.ts +744 -0
- package/src/types/index.ts +915 -0
- package/src/utils/index.ts +1004 -0
- package/src/utils/llm-adapter.ts +76 -0
- package/src/utils/persistence.ts +108 -0
- package/test_output.log +0 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reasoner Agent Implementation
|
|
3
|
+
* @module agents/reasoner-agent
|
|
4
|
+
* @version 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BaseAgent } from './base-agent.js';
|
|
8
|
+
import {
|
|
9
|
+
AgentType,
|
|
10
|
+
ReasoningStrategy,
|
|
11
|
+
type Task,
|
|
12
|
+
type TaskResult,
|
|
13
|
+
type AgentCapability
|
|
14
|
+
} from '../types/index.js';
|
|
15
|
+
import { LLMAdapter } from '../utils/llm-adapter.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Reasoning step
|
|
19
|
+
*/
|
|
20
|
+
interface ReasoningStep {
|
|
21
|
+
step: number;
|
|
22
|
+
content: string;
|
|
23
|
+
confidence: number;
|
|
24
|
+
isFinal: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Reasoner agent that performs various reasoning strategies
|
|
29
|
+
*/
|
|
30
|
+
export class ReasonerAgent extends BaseAgent {
|
|
31
|
+
/** Reasoning strategy */
|
|
32
|
+
private strategy: ReasoningStrategy;
|
|
33
|
+
|
|
34
|
+
/** Maximum iterations */
|
|
35
|
+
private maxIterations: number;
|
|
36
|
+
|
|
37
|
+
/** Confidence threshold */
|
|
38
|
+
private confidenceThreshold: number;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Create a new reasoner agent
|
|
42
|
+
* @param config - Agent configuration
|
|
43
|
+
*/
|
|
44
|
+
constructor(config: {
|
|
45
|
+
name: string;
|
|
46
|
+
strategy?: ReasoningStrategy;
|
|
47
|
+
maxIterations?: number;
|
|
48
|
+
confidenceThreshold?: number;
|
|
49
|
+
capabilities?: AgentCapability[];
|
|
50
|
+
}) {
|
|
51
|
+
const defaultCapabilities: AgentCapability[] = [
|
|
52
|
+
{
|
|
53
|
+
name: 'chain_of_thought',
|
|
54
|
+
description: 'Step-by-step reasoning',
|
|
55
|
+
confidence: 0.9,
|
|
56
|
+
performanceMetrics: { tasksCompleted: 0, averageQuality: 0, averageTimeMs: 0 }
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'tree_of_thought',
|
|
60
|
+
description: 'Explore multiple reasoning paths',
|
|
61
|
+
confidence: 0.85,
|
|
62
|
+
performanceMetrics: { tasksCompleted: 0, averageQuality: 0, averageTimeMs: 0 }
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: 'analogical_reasoning',
|
|
66
|
+
description: 'Reason by analogy',
|
|
67
|
+
confidence: 0.8,
|
|
68
|
+
performanceMetrics: { tasksCompleted: 0, averageQuality: 0, averageTimeMs: 0 }
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'abductive_reasoning',
|
|
72
|
+
description: 'Inference to best explanation',
|
|
73
|
+
confidence: 0.75,
|
|
74
|
+
performanceMetrics: { tasksCompleted: 0, averageQuality: 0, averageTimeMs: 0 }
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'problem_solving',
|
|
78
|
+
description: 'General problem-solving',
|
|
79
|
+
confidence: 0.8,
|
|
80
|
+
performanceMetrics: { tasksCompleted: 0, averageQuality: 0, averageTimeMs: 0 }
|
|
81
|
+
}
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
super({
|
|
85
|
+
name: config.name,
|
|
86
|
+
type: AgentType.REASONER,
|
|
87
|
+
subtype: config.strategy || ReasoningStrategy.CHAIN_OF_THOUGHT,
|
|
88
|
+
capabilities: config.capabilities || defaultCapabilities,
|
|
89
|
+
maxConcurrentTasks: 3,
|
|
90
|
+
confidenceThreshold: config.confidenceThreshold || 0.7
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
this.strategy = config.strategy || ReasoningStrategy.CHAIN_OF_THOUGHT;
|
|
94
|
+
this.maxIterations = config.maxIterations || 10;
|
|
95
|
+
this.confidenceThreshold = config.confidenceThreshold || 0.7;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get agent type
|
|
100
|
+
* @returns Agent type
|
|
101
|
+
*/
|
|
102
|
+
getType(): string {
|
|
103
|
+
return AgentType.REASONER;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get agent capabilities
|
|
108
|
+
* @returns Array of capabilities
|
|
109
|
+
*/
|
|
110
|
+
getCapabilities(): AgentCapability[] {
|
|
111
|
+
return this.config.capabilities;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Process a task using the configured reasoning strategy
|
|
116
|
+
* @param task - Task to process
|
|
117
|
+
* @returns Task result
|
|
118
|
+
*/
|
|
119
|
+
async process(task: Task): Promise<TaskResult> {
|
|
120
|
+
const startTime = Date.now();
|
|
121
|
+
|
|
122
|
+
switch (this.strategy) {
|
|
123
|
+
case ReasoningStrategy.CHAIN_OF_THOUGHT:
|
|
124
|
+
return this.chainOfThought(task, startTime);
|
|
125
|
+
case ReasoningStrategy.TREE_OF_THOUGHT:
|
|
126
|
+
return this.treeOfThought(task, startTime);
|
|
127
|
+
case ReasoningStrategy.ANALOGICAL:
|
|
128
|
+
return this.analogicalReasoning(task, startTime);
|
|
129
|
+
case ReasoningStrategy.ABDUCTIVE:
|
|
130
|
+
return this.abductiveReasoning(task, startTime);
|
|
131
|
+
default:
|
|
132
|
+
return this.chainOfThought(task, startTime);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Chain of thought reasoning
|
|
138
|
+
* @param task - Task to process
|
|
139
|
+
* @param startTime - Start time
|
|
140
|
+
* @returns Task result
|
|
141
|
+
*/
|
|
142
|
+
private async chainOfThought(task: Task, startTime: number): Promise<TaskResult> {
|
|
143
|
+
const steps: ReasoningStep[] = [];
|
|
144
|
+
const input = task.input as { problem?: string; question?: string };
|
|
145
|
+
const problem = input.problem || input.question || String(task.input);
|
|
146
|
+
|
|
147
|
+
let iteration = 0;
|
|
148
|
+
let currentContext = problem;
|
|
149
|
+
|
|
150
|
+
while (iteration < this.maxIterations) {
|
|
151
|
+
iteration++;
|
|
152
|
+
|
|
153
|
+
const prompt = `Based on the current context, generate the next reasoning step.
|
|
154
|
+
Current Context: ${currentContext}
|
|
155
|
+
Iteration: ${iteration}
|
|
156
|
+
Problem: ${problem}`;
|
|
157
|
+
|
|
158
|
+
const response = await LLMAdapter.call(prompt, "You are a logical reasoner. Provide one clear reasoning step.");
|
|
159
|
+
const stepContent = response.content || `Error: ${response.error}`;
|
|
160
|
+
|
|
161
|
+
const stepConfidence = this.calculateStepConfidence(stepContent, iteration);
|
|
162
|
+
|
|
163
|
+
const step: ReasoningStep = {
|
|
164
|
+
step: iteration,
|
|
165
|
+
content: stepContent,
|
|
166
|
+
confidence: stepConfidence,
|
|
167
|
+
isFinal: iteration >= this.maxIterations - 1 || stepConfidence >= this.confidenceThreshold || !!response.error
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
steps.push(step);
|
|
171
|
+
|
|
172
|
+
if (step.isFinal) {
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
currentContext += `\n${stepContent}`;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const conclusionPrompt = `Based on these reasoning steps, provide a final conclusion for the problem: "${problem}"\n\nSteps:\n${steps.map(s => s.content).join('\n')}`;
|
|
180
|
+
const conclusionResponse = await LLMAdapter.call(conclusionPrompt);
|
|
181
|
+
const conclusion = conclusionResponse.content || "Failed to generate conclusion.";
|
|
182
|
+
|
|
183
|
+
const avgConfidence = steps.length > 0
|
|
184
|
+
? steps.reduce((sum, s) => sum + s.confidence, 0) / steps.length
|
|
185
|
+
: 0;
|
|
186
|
+
|
|
187
|
+
return this.createTaskResult(
|
|
188
|
+
task.id,
|
|
189
|
+
{
|
|
190
|
+
conclusion,
|
|
191
|
+
reasoningChain: steps,
|
|
192
|
+
iterations: iteration,
|
|
193
|
+
strategy: 'chain_of_thought',
|
|
194
|
+
problem
|
|
195
|
+
},
|
|
196
|
+
avgConfidence,
|
|
197
|
+
Date.now() - startTime,
|
|
198
|
+
{
|
|
199
|
+
reasoningSteps: steps.length,
|
|
200
|
+
intermediateResults: steps.map(s => s.content)
|
|
201
|
+
}
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Tree of thought reasoning
|
|
207
|
+
* @param task - Task to process
|
|
208
|
+
* @param startTime - Start time
|
|
209
|
+
* @returns Task result
|
|
210
|
+
*/
|
|
211
|
+
private async treeOfThought(task: Task, startTime: number): Promise<TaskResult> {
|
|
212
|
+
const input = task.input as { problem?: string; question?: string };
|
|
213
|
+
const problem = input.problem || input.question || String(task.input);
|
|
214
|
+
|
|
215
|
+
const numPaths = 3;
|
|
216
|
+
const candidates: Array<{
|
|
217
|
+
path: ReasoningStep[];
|
|
218
|
+
confidence: number;
|
|
219
|
+
}> = [];
|
|
220
|
+
|
|
221
|
+
for (let i = 0; i < numPaths; i++) {
|
|
222
|
+
const prompt = `Solve this problem by exploring one specific reasoning path: "${problem}"\nPath #${i+1}:`;
|
|
223
|
+
const response = await LLMAdapter.call(prompt, "You are a creative reasoner. Explore an alternative path.");
|
|
224
|
+
|
|
225
|
+
const step: ReasoningStep = {
|
|
226
|
+
step: 1,
|
|
227
|
+
content: response.content || "Failed to generate path.",
|
|
228
|
+
confidence: 0.7 + Math.random() * 0.2,
|
|
229
|
+
isFinal: true
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
candidates.push({ path: [step], confidence: step.confidence });
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const best = candidates.reduce((max, c) =>
|
|
236
|
+
c.confidence > max.confidence ? c : max,
|
|
237
|
+
candidates[0]
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
return this.createTaskResult(
|
|
241
|
+
task.id,
|
|
242
|
+
{
|
|
243
|
+
conclusion: best.path[0].content,
|
|
244
|
+
bestPath: best.path,
|
|
245
|
+
pathsExplored: candidates.length,
|
|
246
|
+
strategy: 'tree_of_thought',
|
|
247
|
+
problem
|
|
248
|
+
},
|
|
249
|
+
best.confidence,
|
|
250
|
+
Date.now() - startTime
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Analogical reasoning
|
|
256
|
+
* @param task - Task to process
|
|
257
|
+
* @param startTime - Start time
|
|
258
|
+
* @returns Task result
|
|
259
|
+
*/
|
|
260
|
+
private async analogicalReasoning(task: Task, startTime: number): Promise<TaskResult> {
|
|
261
|
+
const input = task.input as { problem?: string; question?: string; domain?: string };
|
|
262
|
+
const problem = input.problem || input.question || String(task.input);
|
|
263
|
+
|
|
264
|
+
const prompt = `Reason about this problem using analogies from other domains: "${problem}"`;
|
|
265
|
+
const response = await LLMAdapter.call(prompt, "You are an expert at analogical reasoning.");
|
|
266
|
+
const conclusion = response.content || "Failed to generate analogical reasoning.";
|
|
267
|
+
|
|
268
|
+
return this.createTaskResult(
|
|
269
|
+
task.id,
|
|
270
|
+
{
|
|
271
|
+
conclusion,
|
|
272
|
+
strategy: 'analogical',
|
|
273
|
+
problem
|
|
274
|
+
},
|
|
275
|
+
0.85,
|
|
276
|
+
Date.now() - startTime
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Abductive reasoning
|
|
282
|
+
* @param task - Task to process
|
|
283
|
+
* @param startTime - Start time
|
|
284
|
+
* @returns Task result
|
|
285
|
+
*/
|
|
286
|
+
private async abductiveReasoning(task: Task, startTime: number): Promise<TaskResult> {
|
|
287
|
+
const input = task.input as { observation?: string; problem?: string };
|
|
288
|
+
const observation = input.observation || input.problem || String(task.input);
|
|
289
|
+
|
|
290
|
+
const prompt = `Provide the best possible explanation (abductive reasoning) for this observation: "${observation}"`;
|
|
291
|
+
const response = await LLMAdapter.call(prompt, "You are an expert at abductive reasoning.");
|
|
292
|
+
const conclusion = response.content || "Failed to generate explanation.";
|
|
293
|
+
|
|
294
|
+
return this.createTaskResult(
|
|
295
|
+
task.id,
|
|
296
|
+
{
|
|
297
|
+
conclusion,
|
|
298
|
+
strategy: 'abductive',
|
|
299
|
+
observation
|
|
300
|
+
},
|
|
301
|
+
0.8,
|
|
302
|
+
Date.now() - startTime
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private calculateStepConfidence(content: string, step: number): number {
|
|
307
|
+
const baseConfidence = 0.5;
|
|
308
|
+
const stepBonus = Math.min(0.3, step * 0.03);
|
|
309
|
+
const contentBonus = content.length > 50 ? 0.1 : 0;
|
|
310
|
+
return Math.min(0.95, baseConfidence + stepBonus + contentBonus);
|
|
311
|
+
}
|
|
312
|
+
}
|