@cogitator-ai/swarms 0.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/LICENSE +21 -0
- package/README.md +90 -0
- package/dist/communication/blackboard.d.ts +24 -0
- package/dist/communication/blackboard.d.ts.map +1 -0
- package/dist/communication/blackboard.js +135 -0
- package/dist/communication/blackboard.js.map +1 -0
- package/dist/communication/event-emitter.d.ts +20 -0
- package/dist/communication/event-emitter.d.ts.map +1 -0
- package/dist/communication/event-emitter.js +87 -0
- package/dist/communication/event-emitter.js.map +1 -0
- package/dist/communication/index.d.ts +7 -0
- package/dist/communication/index.d.ts.map +1 -0
- package/dist/communication/index.js +7 -0
- package/dist/communication/index.js.map +1 -0
- package/dist/communication/message-bus.d.ts +23 -0
- package/dist/communication/message-bus.d.ts.map +1 -0
- package/dist/communication/message-bus.js +128 -0
- package/dist/communication/message-bus.js.map +1 -0
- package/dist/coordinator.d.ts +44 -0
- package/dist/coordinator.d.ts.map +1 -0
- package/dist/coordinator.js +283 -0
- package/dist/coordinator.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/circuit-breaker.d.ts +29 -0
- package/dist/resources/circuit-breaker.d.ts.map +1 -0
- package/dist/resources/circuit-breaker.js +82 -0
- package/dist/resources/circuit-breaker.js.map +1 -0
- package/dist/resources/index.d.ts +7 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/index.js +6 -0
- package/dist/resources/index.js.map +1 -0
- package/dist/resources/tracker.d.ts +30 -0
- package/dist/resources/tracker.d.ts.map +1 -0
- package/dist/resources/tracker.js +84 -0
- package/dist/resources/tracker.js.map +1 -0
- package/dist/strategies/auction.d.ts +17 -0
- package/dist/strategies/auction.d.ts.map +1 -0
- package/dist/strategies/auction.js +204 -0
- package/dist/strategies/auction.js.map +1 -0
- package/dist/strategies/base.d.ts +34 -0
- package/dist/strategies/base.d.ts.map +1 -0
- package/dist/strategies/base.js +74 -0
- package/dist/strategies/base.js.map +1 -0
- package/dist/strategies/consensus.d.ts +20 -0
- package/dist/strategies/consensus.d.ts.map +1 -0
- package/dist/strategies/consensus.js +328 -0
- package/dist/strategies/consensus.js.map +1 -0
- package/dist/strategies/debate.d.ts +15 -0
- package/dist/strategies/debate.d.ts.map +1 -0
- package/dist/strategies/debate.js +171 -0
- package/dist/strategies/debate.js.map +1 -0
- package/dist/strategies/hierarchical.d.ts +13 -0
- package/dist/strategies/hierarchical.d.ts.map +1 -0
- package/dist/strategies/hierarchical.js +86 -0
- package/dist/strategies/hierarchical.js.map +1 -0
- package/dist/strategies/index.d.ts +28 -0
- package/dist/strategies/index.d.ts.map +1 -0
- package/dist/strategies/index.js +106 -0
- package/dist/strategies/index.js.map +1 -0
- package/dist/strategies/pipeline.d.ts +16 -0
- package/dist/strategies/pipeline.d.ts.map +1 -0
- package/dist/strategies/pipeline.js +180 -0
- package/dist/strategies/pipeline.js.map +1 -0
- package/dist/strategies/round-robin.d.ts +27 -0
- package/dist/strategies/round-robin.d.ts.map +1 -0
- package/dist/strategies/round-robin.js +98 -0
- package/dist/strategies/round-robin.js.map +1 -0
- package/dist/swarm.d.ts +113 -0
- package/dist/swarm.d.ts.map +1 -0
- package/dist/swarm.js +280 -0
- package/dist/swarm.js.map +1 -0
- package/dist/tools/blackboard.d.ts +71 -0
- package/dist/tools/blackboard.d.ts.map +1 -0
- package/dist/tools/blackboard.js +149 -0
- package/dist/tools/blackboard.js.map +1 -0
- package/dist/tools/delegation.d.ts +120 -0
- package/dist/tools/delegation.d.ts.map +1 -0
- package/dist/tools/delegation.js +257 -0
- package/dist/tools/delegation.js.map +1 -0
- package/dist/tools/index.d.ts +26 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +65 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/messaging.d.ts +73 -0
- package/dist/tools/messaging.d.ts.map +1 -0
- package/dist/tools/messaging.js +146 -0
- package/dist/tools/messaging.js.map +1 -0
- package/dist/tools/voting.d.ts +110 -0
- package/dist/tools/voting.d.ts.map +1 -0
- package/dist/tools/voting.js +201 -0
- package/dist/tools/voting.js.map +1 -0
- package/dist/workflow/swarm-node.d.ts +43 -0
- package/dist/workflow/swarm-node.d.ts.map +1 -0
- package/dist/workflow/swarm-node.js +111 -0
- package/dist/workflow/swarm-node.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auction strategy - Agents bid for tasks, winner executes
|
|
3
|
+
*/
|
|
4
|
+
import { BaseStrategy } from './base';
|
|
5
|
+
export class AuctionStrategy extends BaseStrategy {
|
|
6
|
+
config;
|
|
7
|
+
constructor(coordinator, config) {
|
|
8
|
+
super(coordinator);
|
|
9
|
+
this.config = {
|
|
10
|
+
minBid: 0,
|
|
11
|
+
...config,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
async execute(options) {
|
|
15
|
+
const agentResults = new Map();
|
|
16
|
+
const agents = this.coordinator.getAgents();
|
|
17
|
+
if (agents.length === 0) {
|
|
18
|
+
throw new Error('Auction strategy requires at least 1 agent');
|
|
19
|
+
}
|
|
20
|
+
this.coordinator.events.emit('auction:start', {
|
|
21
|
+
task: options.input.slice(0, 100),
|
|
22
|
+
participants: agents.map(a => a.agent.name),
|
|
23
|
+
});
|
|
24
|
+
this.coordinator.blackboard.write('auction', {
|
|
25
|
+
task: options.input,
|
|
26
|
+
participants: agents.map(a => a.agent.name),
|
|
27
|
+
bids: [],
|
|
28
|
+
status: 'bidding',
|
|
29
|
+
}, 'system');
|
|
30
|
+
const bids = await this.collectBids(agents, options);
|
|
31
|
+
const validBids = bids.filter(b => b.score >= (this.config.minBid ?? 0));
|
|
32
|
+
if (validBids.length === 0) {
|
|
33
|
+
throw new Error('No valid bids received (all below minimum threshold)');
|
|
34
|
+
}
|
|
35
|
+
const auctionState = this.coordinator.blackboard.read('auction');
|
|
36
|
+
this.coordinator.blackboard.write('auction', {
|
|
37
|
+
...auctionState,
|
|
38
|
+
bids: validBids,
|
|
39
|
+
status: 'selecting',
|
|
40
|
+
}, 'system');
|
|
41
|
+
const winner = this.selectWinner(validBids);
|
|
42
|
+
this.coordinator.events.emit('auction:winner', {
|
|
43
|
+
winner: winner.agentName,
|
|
44
|
+
score: winner.score,
|
|
45
|
+
totalBids: validBids.length,
|
|
46
|
+
});
|
|
47
|
+
const currentAuctionState = this.coordinator.blackboard.read('auction') ?? {};
|
|
48
|
+
this.coordinator.blackboard.write('auction', {
|
|
49
|
+
...currentAuctionState,
|
|
50
|
+
winner: winner.agentName,
|
|
51
|
+
winningBid: winner.score,
|
|
52
|
+
status: 'executing',
|
|
53
|
+
}, 'system');
|
|
54
|
+
const winningAgent = agents.find(a => a.agent.name === winner.agentName);
|
|
55
|
+
const winnerContext = {
|
|
56
|
+
...options.context,
|
|
57
|
+
auctionContext: {
|
|
58
|
+
wonBid: true,
|
|
59
|
+
bidScore: winner.score,
|
|
60
|
+
totalParticipants: agents.length,
|
|
61
|
+
competingBids: validBids.filter(b => b.agentName !== winner.agentName).length,
|
|
62
|
+
},
|
|
63
|
+
auctionInstructions: `You won the bid for this task with a score of ${winner.score.toFixed(2)}. Execute the task to the best of your abilities.`,
|
|
64
|
+
};
|
|
65
|
+
const result = await this.coordinator.runAgent(winningAgent.agent.name, options.input, winnerContext);
|
|
66
|
+
agentResults.set(winningAgent.agent.name, result);
|
|
67
|
+
const finalAuctionState = this.coordinator.blackboard.read('auction') ?? {};
|
|
68
|
+
this.coordinator.blackboard.write('auction', {
|
|
69
|
+
...finalAuctionState,
|
|
70
|
+
status: 'completed',
|
|
71
|
+
result: String(result.output).slice(0, 200),
|
|
72
|
+
}, 'system');
|
|
73
|
+
this.coordinator.events.emit('auction:complete', {
|
|
74
|
+
winner: winner.agentName,
|
|
75
|
+
success: true,
|
|
76
|
+
});
|
|
77
|
+
const bidsMap = new Map();
|
|
78
|
+
for (const bid of validBids) {
|
|
79
|
+
bidsMap.set(bid.agentName, bid.score);
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
output: result.output,
|
|
83
|
+
structured: result.structured,
|
|
84
|
+
agentResults,
|
|
85
|
+
bids: bidsMap,
|
|
86
|
+
auctionWinner: winner.agentName,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async collectBids(agents, options) {
|
|
90
|
+
const bids = [];
|
|
91
|
+
if (this.config.bidding === 'custom' && this.config.bidFunction) {
|
|
92
|
+
for (const agent of agents) {
|
|
93
|
+
try {
|
|
94
|
+
const score = await Promise.resolve(this.config.bidFunction(agent, options.input));
|
|
95
|
+
bids.push({
|
|
96
|
+
agentName: agent.agent.name,
|
|
97
|
+
score,
|
|
98
|
+
capabilities: agent.metadata.expertise ?? [],
|
|
99
|
+
});
|
|
100
|
+
this.coordinator.events.emit('auction:bid', {
|
|
101
|
+
agent: agent.agent.name,
|
|
102
|
+
score,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
bids.push({
|
|
107
|
+
agentName: agent.agent.name,
|
|
108
|
+
score: 0,
|
|
109
|
+
capabilities: [],
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const bidResults = await this.coordinator.runAgentsParallel(agents.map(agent => ({
|
|
116
|
+
name: agent.agent.name,
|
|
117
|
+
input: this.buildBidPrompt(options.input),
|
|
118
|
+
context: {
|
|
119
|
+
...options.context,
|
|
120
|
+
biddingContext: {
|
|
121
|
+
isBidPhase: true,
|
|
122
|
+
expertise: agent.metadata.expertise ?? [],
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
})));
|
|
126
|
+
for (const [agentName, result] of bidResults) {
|
|
127
|
+
const bid = this.parseBidResponse(agentName, result.output, agents.find(a => a.agent.name === agentName));
|
|
128
|
+
bids.push(bid);
|
|
129
|
+
this.coordinator.events.emit('auction:bid', {
|
|
130
|
+
agent: agentName,
|
|
131
|
+
score: bid.score,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return bids;
|
|
136
|
+
}
|
|
137
|
+
buildBidPrompt(task) {
|
|
138
|
+
return `
|
|
139
|
+
You are participating in an auction to determine which agent should handle a task.
|
|
140
|
+
|
|
141
|
+
Task:
|
|
142
|
+
${task}
|
|
143
|
+
|
|
144
|
+
Please assess your capability to handle this task and provide:
|
|
145
|
+
1. A confidence score from 0.0 to 1.0 (where 1.0 means you're perfectly suited)
|
|
146
|
+
2. Your relevant capabilities for this task
|
|
147
|
+
3. Brief reasoning for your bid
|
|
148
|
+
|
|
149
|
+
Respond in this format:
|
|
150
|
+
SCORE: [number between 0.0 and 1.0]
|
|
151
|
+
CAPABILITIES: [comma-separated list]
|
|
152
|
+
REASONING: [brief explanation]
|
|
153
|
+
|
|
154
|
+
Be honest in your assessment. Only bid high if you're truly well-suited for the task.
|
|
155
|
+
`.trim();
|
|
156
|
+
}
|
|
157
|
+
parseBidResponse(agentName, output, agent) {
|
|
158
|
+
const scoreMatch = /SCORE:\s*([\d.]+)/i.exec(output);
|
|
159
|
+
let score = 0.5;
|
|
160
|
+
if (scoreMatch) {
|
|
161
|
+
score = Math.min(1, Math.max(0, parseFloat(scoreMatch[1])));
|
|
162
|
+
if (isNaN(score))
|
|
163
|
+
score = 0.5;
|
|
164
|
+
}
|
|
165
|
+
const capMatch = /CAPABILITIES:\s*([^\n]+)/i.exec(output);
|
|
166
|
+
let capabilities = agent.metadata.expertise ?? [];
|
|
167
|
+
if (capMatch) {
|
|
168
|
+
capabilities = capMatch[1].split(',').map(c => c.trim()).filter(Boolean);
|
|
169
|
+
}
|
|
170
|
+
const reasonMatch = /REASONING:\s*(.+)/is.exec(output);
|
|
171
|
+
const reasoning = reasonMatch ? reasonMatch[1].trim() : undefined;
|
|
172
|
+
return {
|
|
173
|
+
agentName,
|
|
174
|
+
score,
|
|
175
|
+
capabilities,
|
|
176
|
+
reasoning,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
selectWinner(bids) {
|
|
180
|
+
if (bids.length === 0) {
|
|
181
|
+
throw new Error('No bids to select from');
|
|
182
|
+
}
|
|
183
|
+
if (this.config.selection === 'weighted-random') {
|
|
184
|
+
return this.weightedRandomSelect(bids);
|
|
185
|
+
}
|
|
186
|
+
return bids.reduce((best, current) => current.score > best.score ? current : best);
|
|
187
|
+
}
|
|
188
|
+
weightedRandomSelect(bids) {
|
|
189
|
+
const totalWeight = bids.reduce((sum, b) => sum + b.score, 0);
|
|
190
|
+
if (totalWeight === 0) {
|
|
191
|
+
return bids[Math.floor(Math.random() * bids.length)];
|
|
192
|
+
}
|
|
193
|
+
const random = Math.random() * totalWeight;
|
|
194
|
+
let cumulative = 0;
|
|
195
|
+
for (const bid of bids) {
|
|
196
|
+
cumulative += bid.score;
|
|
197
|
+
if (random <= cumulative) {
|
|
198
|
+
return bid;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return bids[bids.length - 1];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
//# sourceMappingURL=auction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auction.js","sourceRoot":"","sources":["../../src/strategies/auction.ts"],"names":[],"mappings":"AAAA;;GAEG;AASH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAUtC,MAAM,OAAO,eAAgB,SAAQ,YAAY;IACvC,MAAM,CAAgB;IAE9B,YAAY,WAA6B,EAAE,MAAqB;QAC9D,KAAK,CAAC,WAAW,CAAC,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG;YACZ,MAAM,EAAE,CAAC;YACT,GAAG,MAAM;SACV,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,OAAwB;QACpC,MAAM,YAAY,GAAG,IAAI,GAAG,EAAqB,CAAC;QAClD,MAAM,MAAM,GAAG,IAAI,CAAC,WA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AM,CAAC,CAAC,CAAC;QACvD,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,WAAW,CAAC;QAC3C,IAAI,UAAU,GAAG,CAAC,CAAC;QAEnB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,UAAU,IAAI,GAAG,CAAC,KAAK,CAAC;YACxB,IAAI,MAAM,IAAI,UAAU,EAAE,CAAC;gBACzB,OAAO,GAAG,CAAC;YACb,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC/B,CAAC;CACF"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base strategy class for swarm coordination
|
|
3
|
+
*/
|
|
4
|
+
import type { SwarmRunOptions, SwarmCoordinatorInterface, IStrategy, StrategyResult, RunResult } from '@cogitator-ai/types';
|
|
5
|
+
export declare abstract class BaseStrategy implements IStrategy {
|
|
6
|
+
protected coordinator: SwarmCoordinatorInterface;
|
|
7
|
+
constructor(coordinator: SwarmCoordinatorInterface);
|
|
8
|
+
abstract execute(options: SwarmRunOptions): Promise<StrategyResult>;
|
|
9
|
+
/**
|
|
10
|
+
* Collect outputs from multiple agents running in parallel
|
|
11
|
+
*/
|
|
12
|
+
protected collectOutputs(agentNames: string[], input: string, context?: Record<string, unknown>): Promise<Map<string, RunResult>>;
|
|
13
|
+
/**
|
|
14
|
+
* Run agents sequentially
|
|
15
|
+
*/
|
|
16
|
+
protected runSequential(agents: {
|
|
17
|
+
name: string;
|
|
18
|
+
input: string | ((prevOutput?: string) => string);
|
|
19
|
+
context?: Record<string, unknown>;
|
|
20
|
+
}[]): Promise<Map<string, RunResult>>;
|
|
21
|
+
/**
|
|
22
|
+
* Synthesize multiple agent outputs into a single string
|
|
23
|
+
*/
|
|
24
|
+
protected synthesizeOutputs(results: Map<string, RunResult>, format?: 'list' | 'sections'): string;
|
|
25
|
+
/**
|
|
26
|
+
* Get the last output from a map of results
|
|
27
|
+
*/
|
|
28
|
+
protected getLastOutput(results: Map<string, RunResult>): string;
|
|
29
|
+
/**
|
|
30
|
+
* Merge all results into a single usage summary
|
|
31
|
+
*/
|
|
32
|
+
protected mergeUsage(results: Map<string, RunResult>): RunResult['usage'];
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/strategies/base.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACV,eAAe,EACf,yBAAyB,EACzB,SAAS,EACT,cAAc,EACd,SAAS,EACV,MAAM,qBAAqB,CAAC;AAE7B,8BAAsB,YAAa,YAAW,SAAS;IACrD,SAAS,CAAC,WAAW,EAAE,yBAAyB,CAAC;gBAErC,WAAW,EAAE,yBAAyB;IAIlD,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC;IAEnE;;OAEG;cACa,cAAc,CAC5B,UAAU,EAAE,MAAM,EAAE,EACpB,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAMlC;;OAEG;cACa,aAAa,CAC3B,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,UAAU,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,EAAE,GAC/G,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAclC;;OAEG;IACH,SAAS,CAAC,iBAAiB,CACzB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,MAAM,GAAE,MAAM,GAAG,UAAuB,GACvC,MAAM;IAYT;;OAEG;IACH,SAAS,CAAC,aAAa,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,MAAM;IAMhE;;OAEG;IACH,SAAS,CAAC,UAAU,CAClB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,GAC9B,SAAS,CAAC,OAAO,CAAC;CAqBtB"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base strategy class for swarm coordination
|
|
3
|
+
*/
|
|
4
|
+
export class BaseStrategy {
|
|
5
|
+
coordinator;
|
|
6
|
+
constructor(coordinator) {
|
|
7
|
+
this.coordinator = coordinator;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Collect outputs from multiple agents running in parallel
|
|
11
|
+
*/
|
|
12
|
+
async collectOutputs(agentNames, input, context) {
|
|
13
|
+
return this.coordinator.runAgentsParallel(agentNames.map((name) => ({ name, input, context })));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Run agents sequentially
|
|
17
|
+
*/
|
|
18
|
+
async runSequential(agents) {
|
|
19
|
+
const results = new Map();
|
|
20
|
+
let prevOutput;
|
|
21
|
+
for (const { name, input, context } of agents) {
|
|
22
|
+
const actualInput = typeof input === 'function' ? input(prevOutput) : input;
|
|
23
|
+
const result = await this.coordinator.runAgent(name, actualInput, context);
|
|
24
|
+
results.set(name, result);
|
|
25
|
+
prevOutput = result.output;
|
|
26
|
+
}
|
|
27
|
+
return results;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Synthesize multiple agent outputs into a single string
|
|
31
|
+
*/
|
|
32
|
+
synthesizeOutputs(results, format = 'sections') {
|
|
33
|
+
if (format === 'list') {
|
|
34
|
+
return Array.from(results.entries())
|
|
35
|
+
.map(([name, result]) => `- ${name}: ${result.output}`)
|
|
36
|
+
.join('\n');
|
|
37
|
+
}
|
|
38
|
+
return Array.from(results.entries())
|
|
39
|
+
.map(([name, result]) => `=== ${name} ===\n${result.output}`)
|
|
40
|
+
.join('\n\n');
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get the last output from a map of results
|
|
44
|
+
*/
|
|
45
|
+
getLastOutput(results) {
|
|
46
|
+
const entries = Array.from(results.entries());
|
|
47
|
+
if (entries.length === 0)
|
|
48
|
+
return '';
|
|
49
|
+
return entries[entries.length - 1][1].output;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Merge all results into a single usage summary
|
|
53
|
+
*/
|
|
54
|
+
mergeUsage(results) {
|
|
55
|
+
let inputTokens = 0;
|
|
56
|
+
let outputTokens = 0;
|
|
57
|
+
let cost = 0;
|
|
58
|
+
let duration = 0;
|
|
59
|
+
for (const result of results.values()) {
|
|
60
|
+
inputTokens += result.usage.inputTokens;
|
|
61
|
+
outputTokens += result.usage.outputTokens;
|
|
62
|
+
cost += result.usage.cost;
|
|
63
|
+
duration += result.usage.duration;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
inputTokens,
|
|
67
|
+
outputTokens,
|
|
68
|
+
totalTokens: inputTokens + outputTokens,
|
|
69
|
+
cost,
|
|
70
|
+
duration,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/strategies/base.ts"],"names":[],"mappings":"AAAA;;GAEG;AAUH,MAAM,OAAgB,YAAY;IACtB,WAAW,CAA4B;IAEjD,YAAY,WAAsC;QAChD,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACjC,CAAC;IAID;;OAEG;IACO,KAAK,CAAC,cAAc,CAC5B,UAAoB,EACpB,KAAa,EACb,OAAiC;QAEjC,OAAO,IAAI,CAAC,WAAW,CAAC,iBAAiB,CACvC,UAAU,CAAC,GAA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AAE,CAAC;aAC5D,IAAI,CAAC,MAAM,CAAC,CAAC;IAClB,CAAC;IAED;;OAEG;IACO,aAAa,CAAC,OAA+B;QACrD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,EAAE,CAAC;QACpC,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAC/C,CAAC;IAED;;OAEG;IACO,UAAU,CAClB,OAA+B;QAE/B,IAAI,WAAW,GAAG,CAAC,CAAC;QACpB,IAAI,YAAY,GAAG,CAAC,CAAC;QACrB,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,IAAI,QAAQ,GAAG,CAAC,CAAC;QAEjB,KAAK,MAAM,MAAM,IAAI,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;YACtC,WAAW,IAAI,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC;YACxC,YAAY,IAAI,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC;YAC1C,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;YAC1B,QAAQ,IAAI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC;QACpC,CAAC;QAED,OAAO;YACL,WAAW;YACX,YAAY;YACZ,WAAW,EAAE,WAAW,GAAG,YAAY;YACvC,IAAI;YACJ,QAAQ;SACT,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Consensus strategy - Agents vote and reach agreement
|
|
3
|
+
*/
|
|
4
|
+
import type { SwarmRunOptions, StrategyResult, ConsensusConfig } from '@cogitator-ai/types';
|
|
5
|
+
import { BaseStrategy } from './base';
|
|
6
|
+
import type { SwarmCoordinator } from '../coordinator';
|
|
7
|
+
export declare class ConsensusStrategy extends BaseStrategy {
|
|
8
|
+
private config;
|
|
9
|
+
constructor(coordinator: SwarmCoordinator, config: ConsensusConfig);
|
|
10
|
+
execute(options: SwarmRunOptions): Promise<StrategyResult>;
|
|
11
|
+
private extractVote;
|
|
12
|
+
private getAgentWeight;
|
|
13
|
+
private checkConsensus;
|
|
14
|
+
private summarizeVotes;
|
|
15
|
+
private getPreviousDiscussion;
|
|
16
|
+
private buildConsensusInstructions;
|
|
17
|
+
private runSupervisorDecision;
|
|
18
|
+
private buildConsensusOutput;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=consensus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consensus.d.ts","sourceRoot":"","sources":["../../src/strategies/consensus.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,eAAe,EAIhB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAiBvD,qBAAa,iBAAkB,SAAQ,YAAY;IACjD,OAAO,CAAC,MAAM,CAAkB;gBAEpB,WAAW,EAAE,gBAAgB,EAAE,MAAM,EAAE,eAAe;IAK5D,OAAO,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC;IA2JhE,OAAO,CAAC,WAAW;IAwBnB,OAAO,CAAC,cAAc;IAUtB,OAAO,CAAC,cAAc;IAiEtB,OAAO,CAAC,cAAc;IAkBtB,OAAO,CAAC,qBAAqB;IAY7B,OAAO,CAAC,0BAA0B;YA0BpB,qBAAqB;IAoCnC,OAAO,CAAC,oBAAoB;CA0C7B"}
|