@cogitator-ai/core 0.1.0 → 0.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/dist/__tests__/agent.test.js +2 -2
- package/dist/__tests__/agent.test.js.map +1 -1
- package/dist/__tests__/base64.test.js +1 -1
- package/dist/__tests__/base64.test.js.map +1 -1
- package/dist/__tests__/calculator.test.js +1 -1
- package/dist/__tests__/calculator.test.js.map +1 -1
- package/dist/__tests__/cogitator-memory.test.js +2 -2
- package/dist/__tests__/cogitator-memory.test.js.map +1 -1
- package/dist/__tests__/datetime.test.js +1 -1
- package/dist/__tests__/datetime.test.js.map +1 -1
- package/dist/__tests__/exec.test.js +1 -1
- package/dist/__tests__/exec.test.js.map +1 -1
- package/dist/__tests__/filesystem.test.js +1 -1
- package/dist/__tests__/filesystem.test.js.map +1 -1
- package/dist/__tests__/google-backend.test.js +1 -1
- package/dist/__tests__/google-backend.test.js.map +1 -1
- package/dist/__tests__/hash.test.js +1 -1
- package/dist/__tests__/hash.test.js.map +1 -1
- package/dist/__tests__/http.test.js +1 -1
- package/dist/__tests__/http.test.js.map +1 -1
- package/dist/__tests__/json.test.js +1 -1
- package/dist/__tests__/json.test.js.map +1 -1
- package/dist/__tests__/logger.test.js +1 -1
- package/dist/__tests__/logger.test.js.map +1 -1
- package/dist/__tests__/random.test.js +1 -1
- package/dist/__tests__/random.test.js.map +1 -1
- package/dist/__tests__/regex.test.js +1 -1
- package/dist/__tests__/regex.test.js.map +1 -1
- package/dist/__tests__/registry.test.js +2 -2
- package/dist/__tests__/registry.test.js.map +1 -1
- package/dist/__tests__/sleep.test.js +1 -1
- package/dist/__tests__/sleep.test.js.map +1 -1
- package/dist/__tests__/tool.test.js +1 -1
- package/dist/__tests__/tool.test.js.map +1 -1
- package/dist/__tests__/uuid.test.js +1 -1
- package/dist/__tests__/uuid.test.js.map +1 -1
- package/dist/cogitator.d.ts +15 -0
- package/dist/cogitator.d.ts.map +1 -1
- package/dist/cogitator.js +149 -13
- package/dist/cogitator.js.map +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/learning/agent-optimizer.d.ts +42 -0
- package/dist/learning/agent-optimizer.d.ts.map +1 -0
- package/dist/learning/agent-optimizer.js +268 -0
- package/dist/learning/agent-optimizer.js.map +1 -0
- package/dist/learning/demo-selector.d.ts +29 -0
- package/dist/learning/demo-selector.d.ts.map +1 -0
- package/dist/learning/demo-selector.js +235 -0
- package/dist/learning/demo-selector.js.map +1 -0
- package/dist/learning/index.d.ts +12 -0
- package/dist/learning/index.d.ts.map +1 -0
- package/dist/learning/index.js +7 -0
- package/dist/learning/index.js.map +1 -0
- package/dist/learning/instruction-optimizer.d.ts +29 -0
- package/dist/learning/instruction-optimizer.d.ts.map +1 -0
- package/dist/learning/instruction-optimizer.js +179 -0
- package/dist/learning/instruction-optimizer.js.map +1 -0
- package/dist/learning/metrics.d.ts +37 -0
- package/dist/learning/metrics.d.ts.map +1 -0
- package/dist/learning/metrics.js +300 -0
- package/dist/learning/metrics.js.map +1 -0
- package/dist/learning/prompts.d.ts +28 -0
- package/dist/learning/prompts.d.ts.map +1 -0
- package/dist/learning/prompts.js +184 -0
- package/dist/learning/prompts.js.map +1 -0
- package/dist/learning/trace-store.d.ts +26 -0
- package/dist/learning/trace-store.d.ts.map +1 -0
- package/dist/learning/trace-store.js +220 -0
- package/dist/learning/trace-store.js.map +1 -0
- package/dist/llm/google.d.ts.map +1 -1
- package/dist/llm/google.js +1 -2
- package/dist/llm/google.js.map +1 -1
- package/dist/reasoning/branch-evaluator.d.ts +28 -0
- package/dist/reasoning/branch-evaluator.d.ts.map +1 -0
- package/dist/reasoning/branch-evaluator.js +137 -0
- package/dist/reasoning/branch-evaluator.js.map +1 -0
- package/dist/reasoning/branch-generator.d.ts +9 -0
- package/dist/reasoning/branch-generator.d.ts.map +1 -0
- package/dist/reasoning/branch-generator.js +58 -0
- package/dist/reasoning/branch-generator.js.map +1 -0
- package/dist/reasoning/index.d.ts +5 -0
- package/dist/reasoning/index.d.ts.map +1 -0
- package/dist/reasoning/index.js +5 -0
- package/dist/reasoning/index.js.map +1 -0
- package/dist/reasoning/prompts.d.ts +19 -0
- package/dist/reasoning/prompts.d.ts.map +1 -0
- package/dist/reasoning/prompts.js +159 -0
- package/dist/reasoning/prompts.js.map +1 -0
- package/dist/reasoning/thought-tree.d.ts +32 -0
- package/dist/reasoning/thought-tree.d.ts.map +1 -0
- package/dist/reasoning/thought-tree.js +354 -0
- package/dist/reasoning/thought-tree.js.map +1 -0
- package/dist/reflection/index.d.ts +4 -0
- package/dist/reflection/index.d.ts.map +1 -0
- package/dist/reflection/index.js +4 -0
- package/dist/reflection/index.js.map +1 -0
- package/dist/reflection/insight-store.d.ts +19 -0
- package/dist/reflection/insight-store.d.ts.map +1 -0
- package/dist/reflection/insight-store.js +127 -0
- package/dist/reflection/insight-store.js.map +1 -0
- package/dist/reflection/prompts.d.ts +18 -0
- package/dist/reflection/prompts.d.ts.map +1 -0
- package/dist/reflection/prompts.js +152 -0
- package/dist/reflection/prompts.js.map +1 -0
- package/dist/reflection/reflection-engine.d.ts +25 -0
- package/dist/reflection/reflection-engine.d.ts.map +1 -0
- package/dist/reflection/reflection-engine.js +204 -0
- package/dist/reflection/reflection-engine.js.map +1 -0
- package/dist/registry.d.ts +1 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +3 -0
- package/dist/registry.js.map +1 -1
- package/dist/tool.d.ts.map +1 -1
- package/dist/tool.js +2 -0
- package/dist/tool.js.map +1 -1
- package/dist/tools/base64.d.ts.map +1 -1
- package/dist/tools/base64.js +2 -8
- package/dist/tools/base64.js.map +1 -1
- package/dist/tools/datetime.d.ts.map +1 -1
- package/dist/tools/datetime.js.map +1 -1
- package/dist/tools/exec.d.ts.map +1 -1
- package/dist/tools/exec.js +1 -4
- package/dist/tools/exec.js.map +1 -1
- package/dist/tools/filesystem.d.ts.map +1 -1
- package/dist/tools/filesystem.js +4 -1
- package/dist/tools/filesystem.js.map +1 -1
- package/dist/tools/hash.d.ts.map +1 -1
- package/dist/tools/hash.js +1 -4
- package/dist/tools/hash.js.map +1 -1
- package/dist/tools/http.d.ts.map +1 -1
- package/dist/tools/http.js +1 -4
- package/dist/tools/http.js.map +1 -1
- package/dist/tools/regex.d.ts.map +1 -1
- package/dist/tools/regex.js +4 -1
- package/dist/tools/regex.js.map +1 -1
- package/dist/utils/circuit-breaker.d.ts.map +1 -1
- package/dist/utils/circuit-breaker.js.map +1 -1
- package/dist/utils/fallback.d.ts.map +1 -1
- package/dist/utils/fallback.js +1 -4
- package/dist/utils/fallback.js.map +1 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +8 -13
- package/dist/utils/retry.js.map +1 -1
- package/package.json +9 -8
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { buildBranchEvaluationPrompt, parseEvaluationResponse } from './prompts';
|
|
2
|
+
export class BranchEvaluator {
|
|
3
|
+
llm;
|
|
4
|
+
model;
|
|
5
|
+
reflectionEngine;
|
|
6
|
+
weights;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.llm = options.llm;
|
|
9
|
+
this.model = options.model;
|
|
10
|
+
this.reflectionEngine = options.reflectionEngine;
|
|
11
|
+
this.weights = {
|
|
12
|
+
confidence: options.confidenceWeight ?? 0.5,
|
|
13
|
+
progress: options.progressWeight ?? 0.3,
|
|
14
|
+
novelty: options.noveltyWeight ?? 0.2,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async evaluate(branch, goal, context, siblings = []) {
|
|
18
|
+
if (this.reflectionEngine && branch.proposedAction.type === 'tool_call') {
|
|
19
|
+
return this.evaluateWithReflection(branch, goal, context, siblings);
|
|
20
|
+
}
|
|
21
|
+
return this.evaluateWithLLM(branch, goal, siblings);
|
|
22
|
+
}
|
|
23
|
+
async evaluateBatch(branches, goal, context) {
|
|
24
|
+
const results = new Map();
|
|
25
|
+
const evaluations = await Promise.all(branches.map(branch => this.evaluate(branch, goal, context, branches.filter(b => b.id !== branch.id))));
|
|
26
|
+
branches.forEach((branch, i) => {
|
|
27
|
+
results.set(branch.id, evaluations[i]);
|
|
28
|
+
});
|
|
29
|
+
return results;
|
|
30
|
+
}
|
|
31
|
+
async evaluateWithReflection(branch, goal, context, siblings) {
|
|
32
|
+
const action = {
|
|
33
|
+
type: 'tool_call',
|
|
34
|
+
toolName: branch.proposedAction.type === 'tool_call' ? branch.proposedAction.toolName : undefined,
|
|
35
|
+
input: branch.proposedAction.type === 'tool_call' ? branch.proposedAction.arguments : undefined,
|
|
36
|
+
};
|
|
37
|
+
const reflectionContext = {
|
|
38
|
+
...context,
|
|
39
|
+
goal: `Evaluate if "${branch.thought}" is a good approach for: ${goal}`,
|
|
40
|
+
};
|
|
41
|
+
const result = await this.reflectionEngine.reflectOnToolCall(action, reflectionContext);
|
|
42
|
+
const confidence = result.reflection.analysis.confidence;
|
|
43
|
+
const progress = this.estimateProgress(branch, goal);
|
|
44
|
+
const novelty = this.calculateNovelty(branch, siblings);
|
|
45
|
+
return {
|
|
46
|
+
confidence,
|
|
47
|
+
progress,
|
|
48
|
+
novelty,
|
|
49
|
+
composite: this.calculateComposite(confidence, progress, novelty),
|
|
50
|
+
reasoning: result.reflection.analysis.reasoning,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async evaluateWithLLM(branch, goal, siblings) {
|
|
54
|
+
const prompt = buildBranchEvaluationPrompt(branch, goal, siblings);
|
|
55
|
+
const response = await this.llm.chat({
|
|
56
|
+
model: this.model,
|
|
57
|
+
messages: [
|
|
58
|
+
{
|
|
59
|
+
role: 'system',
|
|
60
|
+
content: 'You are an evaluation assistant. Assess approaches objectively. Always respond with valid JSON.',
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
role: 'user',
|
|
64
|
+
content: prompt,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
temperature: 0.3,
|
|
68
|
+
maxTokens: 500,
|
|
69
|
+
});
|
|
70
|
+
const parsed = parseEvaluationResponse(response.content);
|
|
71
|
+
if (!parsed) {
|
|
72
|
+
return this.createFallbackScore(branch, siblings);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
confidence: parsed.confidence,
|
|
76
|
+
progress: parsed.progress,
|
|
77
|
+
novelty: parsed.novelty,
|
|
78
|
+
composite: this.calculateComposite(parsed.confidence, parsed.progress, parsed.novelty),
|
|
79
|
+
reasoning: parsed.reasoning,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
estimateProgress(branch, _goal) {
|
|
83
|
+
if (branch.proposedAction.type === 'response') {
|
|
84
|
+
return 0.8;
|
|
85
|
+
}
|
|
86
|
+
if (branch.proposedAction.type === 'sub_goal') {
|
|
87
|
+
return 0.3;
|
|
88
|
+
}
|
|
89
|
+
return 0.5;
|
|
90
|
+
}
|
|
91
|
+
calculateNovelty(branch, siblings) {
|
|
92
|
+
if (siblings.length === 0)
|
|
93
|
+
return 1.0;
|
|
94
|
+
const branchKey = this.getActionKey(branch);
|
|
95
|
+
const siblingKeys = siblings.map(s => this.getActionKey(s));
|
|
96
|
+
const sameAction = siblingKeys.filter(k => k === branchKey).length;
|
|
97
|
+
const similarThoughts = siblings.filter(s => this.calculateSimilarity(branch.thought, s.thought) > 0.5).length;
|
|
98
|
+
const actionNovelty = 1 - (sameAction / siblings.length);
|
|
99
|
+
const thoughtNovelty = 1 - (similarThoughts / siblings.length);
|
|
100
|
+
return (actionNovelty + thoughtNovelty) / 2;
|
|
101
|
+
}
|
|
102
|
+
getActionKey(branch) {
|
|
103
|
+
const action = branch.proposedAction;
|
|
104
|
+
if (action.type === 'tool_call') {
|
|
105
|
+
return `tool:${action.toolName}`;
|
|
106
|
+
}
|
|
107
|
+
return action.type;
|
|
108
|
+
}
|
|
109
|
+
calculateSimilarity(a, b) {
|
|
110
|
+
const wordsA = new Set(a.toLowerCase().split(/\s+/).filter(w => w.length > 3));
|
|
111
|
+
const wordsB = new Set(b.toLowerCase().split(/\s+/).filter(w => w.length > 3));
|
|
112
|
+
if (wordsA.size === 0 || wordsB.size === 0)
|
|
113
|
+
return 0;
|
|
114
|
+
let overlap = 0;
|
|
115
|
+
for (const word of wordsA) {
|
|
116
|
+
if (wordsB.has(word))
|
|
117
|
+
overlap++;
|
|
118
|
+
}
|
|
119
|
+
return overlap / Math.max(wordsA.size, wordsB.size);
|
|
120
|
+
}
|
|
121
|
+
calculateComposite(confidence, progress, novelty) {
|
|
122
|
+
return (this.weights.confidence * confidence +
|
|
123
|
+
this.weights.progress * progress +
|
|
124
|
+
this.weights.novelty * novelty);
|
|
125
|
+
}
|
|
126
|
+
createFallbackScore(branch, siblings) {
|
|
127
|
+
const novelty = this.calculateNovelty(branch, siblings);
|
|
128
|
+
return {
|
|
129
|
+
confidence: 0.5,
|
|
130
|
+
progress: 0.3,
|
|
131
|
+
novelty,
|
|
132
|
+
composite: this.calculateComposite(0.5, 0.3, novelty),
|
|
133
|
+
reasoning: 'Fallback evaluation - could not parse LLM response',
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=branch-evaluator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"branch-evaluator.js","sourceRoot":"","sources":["../../src/reasoning/branch-evaluator.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,2BAA2B,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAWjF,MAAM,OAAO,eAAe;IAClB,GAAG,CAAa;IAChB,KAAK,CAAS;IACd,gBAAgB,CAAoB;IACpC,OAAO,CAA4D;IAE3E,YAAY,OAA+B;QACzC,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;QACjD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CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;QAE/E,IAAI,MAAM,CAAC,IAAI,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,KAAK,CAAC;YAAE,OAAO,CAAC,CAAC;QAErD,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE,CAAC;YAC1B,IAAI,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;gBAAE,OAAO,EAAE,CAAC;QAClC,CAAC;QAED,OAAO,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IACtD,CAAC;IAEO,kBAAkB,CAAC,UAAkB,EAAE,QAAgB,EAAE,OAAe;QAC9E,OAAO,CACL,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,UAAU;YACpC,IAAI,CAAC,OAAO,CAAC,QAAQ,GAAG,QAAQ;YAChC,IAAI,CAAC,OAAO,CAAC,OAAO,GAAG,OAAO,CAC/B,CAAC;IACJ,CAAC;IAEO,mBAAmB,CAAC,MAAqB,EAAE,QAAyB;QAC1E,MAAM,OAAO,GAAG,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAExD,OAAO;YACL,UAAU,EAAE,GAAG;YACf,QAAQ,EAAE,GAAG;YACb,OAAO;YACP,SAAS,EAAE,IAAI,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC;YACrD,SAAS,EAAE,oDAAoD;SAChE,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { LLMBackend, ThoughtNode, ThoughtBranch, AgentContext } from '@cogitator-ai/types';
|
|
2
|
+
export declare class BranchGenerator {
|
|
3
|
+
private llm;
|
|
4
|
+
private model;
|
|
5
|
+
constructor(llm: LLMBackend, model: string);
|
|
6
|
+
generate(currentNode: ThoughtNode | null, goal: string, k: number, context: AgentContext, exploredThoughts?: string[]): Promise<ThoughtBranch[]>;
|
|
7
|
+
private createFallbackBranches;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=branch-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"branch-generator.d.ts","sourceRoot":"","sources":["../../src/reasoning/branch-generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAOhG,qBAAa,eAAe;IAC1B,OAAO,CAAC,GAAG,CAAa;IACxB,OAAO,CAAC,KAAK,CAAS;gBAEV,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM;IAKpC,QAAQ,CACZ,WAAW,EAAE,WAAW,GAAG,IAAI,EAC/B,IAAI,EAAE,MAAM,EACZ,CAAC,EAAE,MAAM,EACT,OAAO,EAAE,YAAY,EACrB,gBAAgB,GAAE,MAAM,EAAO,GAC9B,OAAO,CAAC,aAAa,EAAE,CAAC;IA2C3B,OAAO,CAAC,sBAAsB;CAmB/B"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { buildBranchGenerationPrompt, parseBranchResponse } from './prompts';
|
|
2
|
+
function generateId() {
|
|
3
|
+
return `branch_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 9)}`;
|
|
4
|
+
}
|
|
5
|
+
export class BranchGenerator {
|
|
6
|
+
llm;
|
|
7
|
+
model;
|
|
8
|
+
constructor(llm, model) {
|
|
9
|
+
this.llm = llm;
|
|
10
|
+
this.model = model;
|
|
11
|
+
}
|
|
12
|
+
async generate(currentNode, goal, k, context, exploredThoughts = []) {
|
|
13
|
+
const prompt = buildBranchGenerationPrompt(goal, currentNode, context.availableTools, k, exploredThoughts);
|
|
14
|
+
const response = await this.llm.chat({
|
|
15
|
+
model: this.model,
|
|
16
|
+
messages: [
|
|
17
|
+
{
|
|
18
|
+
role: 'system',
|
|
19
|
+
content: 'You are a strategic reasoning assistant. Generate diverse approaches to solve problems. Always respond with valid JSON.',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
role: 'user',
|
|
23
|
+
content: prompt,
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
temperature: 0.8,
|
|
27
|
+
maxTokens: 2000,
|
|
28
|
+
});
|
|
29
|
+
const parsed = parseBranchResponse(response.content);
|
|
30
|
+
if (!parsed || parsed.branches.length === 0) {
|
|
31
|
+
return this.createFallbackBranches(goal, k, currentNode);
|
|
32
|
+
}
|
|
33
|
+
const parentId = currentNode?.id ?? null;
|
|
34
|
+
const messagesSnapshot = currentNode?.messages ?? [];
|
|
35
|
+
return parsed.branches.slice(0, k).map(b => ({
|
|
36
|
+
id: generateId(),
|
|
37
|
+
parentId,
|
|
38
|
+
thought: b.thought,
|
|
39
|
+
proposedAction: b.action,
|
|
40
|
+
messagesSnapshot: [...messagesSnapshot],
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
createFallbackBranches(goal, _k, currentNode) {
|
|
44
|
+
const parentId = currentNode?.id ?? null;
|
|
45
|
+
const messagesSnapshot = currentNode?.messages ?? [];
|
|
46
|
+
return [{
|
|
47
|
+
id: generateId(),
|
|
48
|
+
parentId,
|
|
49
|
+
thought: `Attempting to directly address: ${goal}`,
|
|
50
|
+
proposedAction: {
|
|
51
|
+
type: 'response',
|
|
52
|
+
content: `Let me work on: ${goal}`,
|
|
53
|
+
},
|
|
54
|
+
messagesSnapshot: [...messagesSnapshot],
|
|
55
|
+
}];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=branch-generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"branch-generator.js","sourceRoot":"","sources":["../../src/reasoning/branch-generator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,2BAA2B,EAAE,mBAAmB,EAAE,MAAM,WAAW,CA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E,IAAI,IAAI,CAAC;QACzC,MAAM,gBAAgB,GAAG,WAAW,EAAE,QAAQ,IAAI,EAAE,CAAC;QAErD,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YAC3C,EAAE,EAAE,UAAU,EAAE;YAChB,QAAQ;YACR,OAAO,EAAE,CAAC,CAAC,OAAO;YAClB,cAAc,EAAE,CAAC,CAAC,MAAM;YACxB,gBAAgB,EAAE,CAAC,GAAG,gBAAgB,CAAC;SACxC,CAAC,CAAC,CAAC;IACN,CAAC;IAEO,sBAAsB,CAC5B,IAAY,EACZ,EAAU,EACV,WAA+B;QAE/B,MAAM,QAAQ,GAAG,WAAW,EAAE,EAAE,IAAI,IAAI,CAAC;QACzC,MAAM,gBAAgB,GAAG,WAAW,EAAE,QAAQ,IAAI,EAAE,CAAC;QAErD,OAAO,CAAC;gBACN,EAAE,EAAE,UAAU,EAAE;gBAChB,QAAQ;gBACR,OAAO,EAAE,mCAAmC,IAAI,EAAE;gBAClD,cAAc,EAAE;oBACd,IAAI,EAAE,UAAU;oBAChB,OAAO,EAAE,mBAAmB,IAAI,EAAE;iBACnC;gBACD,gBAAgB,EAAE,CAAC,GAAG,gBAAgB,CAAC;aACxC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ThoughtTreeExecutor } from './thought-tree';
|
|
2
|
+
export { BranchGenerator } from './branch-generator';
|
|
3
|
+
export { BranchEvaluator, type BranchEvaluatorOptions } from './branch-evaluator';
|
|
4
|
+
export { buildBranchGenerationPrompt, buildBranchEvaluationPrompt, buildSynthesisPrompt, parseBranchResponse, parseEvaluationResponse, } from './prompts';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/reasoning/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,KAAK,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAClF,OAAO,EACL,2BAA2B,EAC3B,2BAA2B,EAC3B,oBAAoB,EACpB,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ThoughtTreeExecutor } from './thought-tree';
|
|
2
|
+
export { BranchGenerator } from './branch-generator';
|
|
3
|
+
export { BranchEvaluator } from './branch-evaluator';
|
|
4
|
+
export { buildBranchGenerationPrompt, buildBranchEvaluationPrompt, buildSynthesisPrompt, parseBranchResponse, parseEvaluationResponse, } from './prompts';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/reasoning/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,eAAe,EAA+B,MAAM,oBAAoB,CAAC;AAClF,OAAO,EACL,2BAA2B,EAC3B,2BAA2B,EAC3B,oBAAoB,EACpB,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ThoughtNode, ThoughtBranch, ProposedAction } from '@cogitator-ai/types';
|
|
2
|
+
export declare function buildBranchGenerationPrompt(goal: string, currentNode: ThoughtNode | null, availableTools: string[], k: number, exploredThoughts: string[]): string;
|
|
3
|
+
export declare function buildBranchEvaluationPrompt(branch: ThoughtBranch, goal: string, siblings: ThoughtBranch[]): string;
|
|
4
|
+
export declare function buildSynthesisPrompt(goal: string, path: ThoughtNode[]): string;
|
|
5
|
+
export interface ParsedBranches {
|
|
6
|
+
branches: Array<{
|
|
7
|
+
thought: string;
|
|
8
|
+
action: ProposedAction;
|
|
9
|
+
}>;
|
|
10
|
+
}
|
|
11
|
+
export declare function parseBranchResponse(response: string): ParsedBranches | null;
|
|
12
|
+
export interface ParsedEvaluation {
|
|
13
|
+
confidence: number;
|
|
14
|
+
progress: number;
|
|
15
|
+
novelty: number;
|
|
16
|
+
reasoning: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function parseEvaluationResponse(response: string): ParsedEvaluation | null;
|
|
19
|
+
//# sourceMappingURL=prompts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompts.d.ts","sourceRoot":"","sources":["../../src/reasoning/prompts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEtF,wBAAgB,2BAA2B,CACzC,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,WAAW,GAAG,IAAI,EAC/B,cAAc,EAAE,MAAM,EAAE,EACxB,CAAC,EAAE,MAAM,EACT,gBAAgB,EAAE,MAAM,EAAE,GACzB,MAAM,CAoDR;AAED,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,aAAa,EACrB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,aAAa,EAAE,GACxB,MAAM,CA2BR;AAED,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,EAAE,GAClB,MAAM,CAoBR;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,KAAK,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,cAAc,CAAC;KACxB,CAAC,CAAC;CACJ;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,GAAG,IAAI,CAgC3E;AAED,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CA2BjF"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
export function buildBranchGenerationPrompt(goal, currentNode, availableTools, k, exploredThoughts) {
|
|
2
|
+
const stateSection = currentNode
|
|
3
|
+
? `CURRENT STATE:
|
|
4
|
+
Previous thought: ${currentNode.branch.thought}
|
|
5
|
+
Previous action: ${JSON.stringify(currentNode.branch.proposedAction)}
|
|
6
|
+
Result: ${currentNode.result?.response ?? currentNode.result?.error ?? 'pending'}
|
|
7
|
+
Depth: ${currentNode.depth}`
|
|
8
|
+
: 'CURRENT STATE: Starting fresh';
|
|
9
|
+
const exploredSection = exploredThoughts.length > 0
|
|
10
|
+
? `\nALREADY EXPLORED (avoid similar approaches):\n${exploredThoughts.map(t => `- ${t}`).join('\n')}`
|
|
11
|
+
: '';
|
|
12
|
+
return `You are exploring different approaches to solve a problem.
|
|
13
|
+
|
|
14
|
+
GOAL: ${goal}
|
|
15
|
+
|
|
16
|
+
${stateSection}
|
|
17
|
+
${exploredSection}
|
|
18
|
+
|
|
19
|
+
AVAILABLE TOOLS: ${availableTools.join(', ')}
|
|
20
|
+
|
|
21
|
+
Generate exactly ${k} DIFFERENT approaches to make progress toward the goal.
|
|
22
|
+
Each approach should be genuinely distinct - don't just vary parameters slightly.
|
|
23
|
+
|
|
24
|
+
For each approach, provide:
|
|
25
|
+
1. THOUGHT: Your reasoning for this approach (2-3 sentences explaining WHY this might work)
|
|
26
|
+
2. ACTION: One of:
|
|
27
|
+
- tool_call: Use a specific tool
|
|
28
|
+
- response: Provide a direct answer
|
|
29
|
+
- sub_goal: Break down into a smaller goal
|
|
30
|
+
|
|
31
|
+
Respond ONLY with valid JSON (no markdown):
|
|
32
|
+
{
|
|
33
|
+
"branches": [
|
|
34
|
+
{
|
|
35
|
+
"thought": "reasoning for this approach...",
|
|
36
|
+
"action": {
|
|
37
|
+
"type": "tool_call",
|
|
38
|
+
"toolName": "tool_name",
|
|
39
|
+
"arguments": {}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"thought": "different reasoning...",
|
|
44
|
+
"action": {
|
|
45
|
+
"type": "response",
|
|
46
|
+
"content": "direct answer if applicable"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
}`;
|
|
51
|
+
}
|
|
52
|
+
export function buildBranchEvaluationPrompt(branch, goal, siblings) {
|
|
53
|
+
const siblingsSection = siblings.length > 0
|
|
54
|
+
? `\nOTHER APPROACHES BEING CONSIDERED:\n${siblings.map(s => `- ${s.thought}`).join('\n')}`
|
|
55
|
+
: '';
|
|
56
|
+
return `Evaluate this approach for solving a problem.
|
|
57
|
+
|
|
58
|
+
GOAL: ${goal}
|
|
59
|
+
|
|
60
|
+
PROPOSED APPROACH:
|
|
61
|
+
Thought: ${branch.thought}
|
|
62
|
+
Action: ${JSON.stringify(branch.proposedAction)}
|
|
63
|
+
${siblingsSection}
|
|
64
|
+
|
|
65
|
+
Evaluate this approach on these dimensions:
|
|
66
|
+
1. CONFIDENCE (0.0-1.0): How likely is this approach to make progress?
|
|
67
|
+
2. PROGRESS (0.0-1.0): How much of the goal would this step complete?
|
|
68
|
+
3. NOVELTY (0.0-1.0): How different is this from the other approaches?
|
|
69
|
+
4. FEASIBILITY: Can this action actually be executed successfully?
|
|
70
|
+
|
|
71
|
+
Respond ONLY with valid JSON (no markdown):
|
|
72
|
+
{
|
|
73
|
+
"confidence": 0.7,
|
|
74
|
+
"progress": 0.3,
|
|
75
|
+
"novelty": 0.8,
|
|
76
|
+
"reasoning": "brief explanation of the evaluation"
|
|
77
|
+
}`;
|
|
78
|
+
}
|
|
79
|
+
export function buildSynthesisPrompt(goal, path) {
|
|
80
|
+
const pathSummary = path.map((node, i) => {
|
|
81
|
+
const result = node.result?.response ?? node.result?.error ?? 'no result';
|
|
82
|
+
return `Step ${i + 1}:
|
|
83
|
+
Thought: ${node.branch.thought}
|
|
84
|
+
Action: ${JSON.stringify(node.branch.proposedAction)}
|
|
85
|
+
Result: ${result}`;
|
|
86
|
+
}).join('\n\n');
|
|
87
|
+
return `Synthesize a final answer based on the explored path.
|
|
88
|
+
|
|
89
|
+
ORIGINAL GOAL: ${goal}
|
|
90
|
+
|
|
91
|
+
EXPLORATION PATH:
|
|
92
|
+
${pathSummary}
|
|
93
|
+
|
|
94
|
+
Based on this exploration, provide the best possible answer to the original goal.
|
|
95
|
+
Focus on synthesizing the insights from each step into a coherent response.
|
|
96
|
+
|
|
97
|
+
Respond with your final answer:`;
|
|
98
|
+
}
|
|
99
|
+
export function parseBranchResponse(response) {
|
|
100
|
+
try {
|
|
101
|
+
let cleaned = response.trim();
|
|
102
|
+
if (cleaned.startsWith('```json')) {
|
|
103
|
+
cleaned = cleaned.slice(7);
|
|
104
|
+
}
|
|
105
|
+
else if (cleaned.startsWith('```')) {
|
|
106
|
+
cleaned = cleaned.slice(3);
|
|
107
|
+
}
|
|
108
|
+
if (cleaned.endsWith('```')) {
|
|
109
|
+
cleaned = cleaned.slice(0, -3);
|
|
110
|
+
}
|
|
111
|
+
cleaned = cleaned.trim();
|
|
112
|
+
const parsed = JSON.parse(cleaned);
|
|
113
|
+
if (!Array.isArray(parsed.branches)) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
parsed.branches = parsed.branches.filter(b => {
|
|
117
|
+
if (!b || typeof b !== 'object')
|
|
118
|
+
return false;
|
|
119
|
+
if (typeof b.thought !== 'string' || !b.thought)
|
|
120
|
+
return false;
|
|
121
|
+
if (!b.action || typeof b.action !== 'object')
|
|
122
|
+
return false;
|
|
123
|
+
if (!['tool_call', 'response', 'sub_goal'].includes(b.action.type))
|
|
124
|
+
return false;
|
|
125
|
+
return true;
|
|
126
|
+
});
|
|
127
|
+
return parsed;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export function parseEvaluationResponse(response) {
|
|
134
|
+
try {
|
|
135
|
+
let cleaned = response.trim();
|
|
136
|
+
if (cleaned.startsWith('```json')) {
|
|
137
|
+
cleaned = cleaned.slice(7);
|
|
138
|
+
}
|
|
139
|
+
else if (cleaned.startsWith('```')) {
|
|
140
|
+
cleaned = cleaned.slice(3);
|
|
141
|
+
}
|
|
142
|
+
if (cleaned.endsWith('```')) {
|
|
143
|
+
cleaned = cleaned.slice(0, -3);
|
|
144
|
+
}
|
|
145
|
+
cleaned = cleaned.trim();
|
|
146
|
+
const parsed = JSON.parse(cleaned);
|
|
147
|
+
const clamp = (n) => Math.max(0, Math.min(1, n));
|
|
148
|
+
return {
|
|
149
|
+
confidence: typeof parsed.confidence === 'number' ? clamp(parsed.confidence) : 0.5,
|
|
150
|
+
progress: typeof parsed.progress === 'number' ? clamp(parsed.progress) : 0.3,
|
|
151
|
+
novelty: typeof parsed.novelty === 'number' ? clamp(parsed.novelty) : 0.5,
|
|
152
|
+
reasoning: typeof parsed.reasoning === 'string' ? parsed.reasoning : '',
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=prompts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompts.js","sourceRoot":"","sources":["../../src/reasoning/prompts.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,2BAA2B,CACzC,IAAY,EACZ,WAA+B,EAC/B,cAAwB,EACxB,CAAS,EACT,gBAA0B;IAE1B,MAAM,YAAY,GAAG,WAAW;QAC9B,CAAC,CAAC;oBACc,WAAW,CAAC,MAAM,CAAC,OAAO;mBAC3B,IAAI,CAAC,SAAS,CAA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E,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QAEzD,OAAO;YACL,UAAU,EAAE,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG;YAClF,QAAQ,EAAE,OAAO,MAAM,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG;YAC5E,OAAO,EAAE,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG;YACzE,SAAS,EAAE,OAAO,MAAM,CAAC,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE;SACxE,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ToTConfig, ToTResult, ToTRunOptions, Agent } from '@cogitator-ai/types';
|
|
2
|
+
import type { Cogitator } from '../cogitator';
|
|
3
|
+
export declare class ThoughtTreeExecutor {
|
|
4
|
+
private cogitator;
|
|
5
|
+
private config;
|
|
6
|
+
private branchGenerator;
|
|
7
|
+
private branchEvaluator;
|
|
8
|
+
private currentModel;
|
|
9
|
+
private nodes;
|
|
10
|
+
private stats;
|
|
11
|
+
constructor(cogitator: Cogitator, config?: Partial<ToTConfig>);
|
|
12
|
+
explore(agent: Agent, goal: string, options?: ToTRunOptions): Promise<ToTResult>;
|
|
13
|
+
private createRootNode;
|
|
14
|
+
private expandNode;
|
|
15
|
+
private buildNodePrompt;
|
|
16
|
+
private shouldTerminate;
|
|
17
|
+
private backtrack;
|
|
18
|
+
private findBestNode;
|
|
19
|
+
private getPathToNode;
|
|
20
|
+
private getExploredThoughts;
|
|
21
|
+
private createResult;
|
|
22
|
+
private synthesizeOutput;
|
|
23
|
+
private fallbackSynthesis;
|
|
24
|
+
private buildAgentContext;
|
|
25
|
+
private getLLMBackend;
|
|
26
|
+
private cachedLLM?;
|
|
27
|
+
private getLLMBackendFromCache;
|
|
28
|
+
private parseModel;
|
|
29
|
+
private getReflectionEngine;
|
|
30
|
+
private createInitialStats;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=thought-tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thought-tree.d.ts","sourceRoot":"","sources":["../../src/reasoning/thought-tree.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,SAAS,EACT,SAAS,EAET,aAAa,EAIb,KAAK,EAGN,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAU9C,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,MAAM,CAA2O;IACzP,OAAO,CAAC,eAAe,CAAmB;IAC1C,OAAO,CAAC,eAAe,CAAmB;IAC1C,OAAO,CAAC,YAAY,CAAc;IAElC,OAAO,CAAC,KAAK,CAAkC;IAC/C,OAAO,CAAC,KAAK,CAAuC;gBAExC,SAAS,EAAE,SAAS,EAAE,MAAM,GAAE,OAAO,CAAC,SAAS,CAAM;IAK3D,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB,GAAG,OAAO,CAAC,SAAS,CAAC;IAqH1F,OAAO,CAAC,cAAc;YAsBR,UAAU;IAuDxB,OAAO,CAAC,eAAe;IAcvB,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,SAAS;IA6BjB,OAAO,CAAC,YAAY;IAcpB,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,mBAAmB;YAMb,YAAY;YAuDZ,gBAAgB;IAoB9B,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,iBAAiB;YAaX,aAAa;IAK3B,OAAO,CAAC,SAAS,CAAC,CAAa;YACjB,sBAAsB;IAIpC,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,mBAAmB;IAI3B,OAAO,CAAC,kBAAkB;CAY3B"}
|