@sparkleideas/shared 3.0.0-alpha.7
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 +323 -0
- package/__tests__/hooks/bash-safety.test.ts +289 -0
- package/__tests__/hooks/file-organization.test.ts +335 -0
- package/__tests__/hooks/git-commit.test.ts +336 -0
- package/__tests__/hooks/index.ts +23 -0
- package/__tests__/hooks/session-hooks.test.ts +357 -0
- package/__tests__/hooks/task-hooks.test.ts +193 -0
- package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
- package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
- package/docs/EVENTS_README.md +352 -0
- package/package.json +39 -0
- package/src/core/config/defaults.ts +207 -0
- package/src/core/config/index.ts +15 -0
- package/src/core/config/loader.ts +271 -0
- package/src/core/config/schema.ts +188 -0
- package/src/core/config/validator.ts +209 -0
- package/src/core/event-bus.ts +236 -0
- package/src/core/index.ts +22 -0
- package/src/core/interfaces/agent.interface.ts +251 -0
- package/src/core/interfaces/coordinator.interface.ts +363 -0
- package/src/core/interfaces/event.interface.ts +267 -0
- package/src/core/interfaces/index.ts +19 -0
- package/src/core/interfaces/memory.interface.ts +332 -0
- package/src/core/interfaces/task.interface.ts +223 -0
- package/src/core/orchestrator/event-coordinator.ts +122 -0
- package/src/core/orchestrator/health-monitor.ts +214 -0
- package/src/core/orchestrator/index.ts +89 -0
- package/src/core/orchestrator/lifecycle-manager.ts +263 -0
- package/src/core/orchestrator/session-manager.ts +279 -0
- package/src/core/orchestrator/task-manager.ts +317 -0
- package/src/events/domain-events.ts +584 -0
- package/src/events/event-store.test.ts +387 -0
- package/src/events/event-store.ts +588 -0
- package/src/events/example-usage.ts +293 -0
- package/src/events/index.ts +90 -0
- package/src/events/projections.ts +561 -0
- package/src/events/state-reconstructor.ts +349 -0
- package/src/events.ts +367 -0
- package/src/hooks/INTEGRATION.md +658 -0
- package/src/hooks/README.md +532 -0
- package/src/hooks/example-usage.ts +499 -0
- package/src/hooks/executor.ts +379 -0
- package/src/hooks/hooks.test.ts +421 -0
- package/src/hooks/index.ts +131 -0
- package/src/hooks/registry.ts +333 -0
- package/src/hooks/safety/bash-safety.ts +604 -0
- package/src/hooks/safety/file-organization.ts +473 -0
- package/src/hooks/safety/git-commit.ts +623 -0
- package/src/hooks/safety/index.ts +46 -0
- package/src/hooks/session-hooks.ts +559 -0
- package/src/hooks/task-hooks.ts +513 -0
- package/src/hooks/types.ts +357 -0
- package/src/hooks/verify-exports.test.ts +125 -0
- package/src/index.ts +195 -0
- package/src/mcp/connection-pool.ts +438 -0
- package/src/mcp/index.ts +183 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/session-manager.ts +428 -0
- package/src/mcp/tool-registry.ts +566 -0
- package/src/mcp/transport/http.ts +557 -0
- package/src/mcp/transport/index.ts +294 -0
- package/src/mcp/transport/stdio.ts +324 -0
- package/src/mcp/transport/websocket.ts +484 -0
- package/src/mcp/types.ts +565 -0
- package/src/plugin-interface.ts +663 -0
- package/src/plugin-loader.ts +638 -0
- package/src/plugin-registry.ts +604 -0
- package/src/plugins/index.ts +34 -0
- package/src/plugins/official/hive-mind-plugin.ts +330 -0
- package/src/plugins/official/index.ts +24 -0
- package/src/plugins/official/maestro-plugin.ts +508 -0
- package/src/plugins/types.ts +108 -0
- package/src/resilience/bulkhead.ts +277 -0
- package/src/resilience/circuit-breaker.ts +326 -0
- package/src/resilience/index.ts +26 -0
- package/src/resilience/rate-limiter.ts +420 -0
- package/src/resilience/retry.ts +224 -0
- package/src/security/index.ts +39 -0
- package/src/security/input-validation.ts +265 -0
- package/src/security/secure-random.ts +159 -0
- package/src/services/index.ts +16 -0
- package/src/services/v3-progress.service.ts +505 -0
- package/src/types/agent.types.ts +144 -0
- package/src/types/index.ts +22 -0
- package/src/types/mcp.types.ts +300 -0
- package/src/types/memory.types.ts +263 -0
- package/src/types/swarm.types.ts +255 -0
- package/src/types/task.types.ts +205 -0
- package/src/types.ts +367 -0
- package/src/utils/secure-logger.d.ts +69 -0
- package/src/utils/secure-logger.d.ts.map +1 -0
- package/src/utils/secure-logger.js +208 -0
- package/src/utils/secure-logger.js.map +1 -0
- package/src/utils/secure-logger.ts +257 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HiveMind Plugin - Official Plugin (ADR-004)
|
|
3
|
+
*
|
|
4
|
+
* Implements collective intelligence and emergent behavior patterns.
|
|
5
|
+
* Part of the official plugin collection.
|
|
6
|
+
*
|
|
7
|
+
* @module v3/shared/plugins/official/hive-mind
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ClaudeFlowPlugin, PluginContext, PluginConfig } from '../types.js';
|
|
11
|
+
import { HookEvent, HookPriority, type TaskInfo } from '../../hooks/index.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* HiveMind configuration
|
|
15
|
+
*/
|
|
16
|
+
export interface HiveMindConfig extends PluginConfig {
|
|
17
|
+
consensusThreshold: number; // Minimum agreement for decisions (0-1)
|
|
18
|
+
collectiveMemoryEnabled: boolean;
|
|
19
|
+
emergentBehaviorEnabled: boolean;
|
|
20
|
+
maxVotingRounds: number;
|
|
21
|
+
decisionTimeout: number; // ms
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Collective decision
|
|
26
|
+
*/
|
|
27
|
+
export interface CollectiveDecision {
|
|
28
|
+
id: string;
|
|
29
|
+
question: string;
|
|
30
|
+
votes: Map<string, { agentId: string; vote: string; confidence: number }>;
|
|
31
|
+
consensus?: string;
|
|
32
|
+
consensusConfidence: number;
|
|
33
|
+
timestamp: Date;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Emergent pattern
|
|
38
|
+
*/
|
|
39
|
+
export interface EmergentPattern {
|
|
40
|
+
id: string;
|
|
41
|
+
type: string;
|
|
42
|
+
description: string;
|
|
43
|
+
contributors: string[];
|
|
44
|
+
strength: number;
|
|
45
|
+
discoveredAt: Date;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* HiveMind Plugin Implementation
|
|
50
|
+
*/
|
|
51
|
+
export class HiveMindPlugin implements ClaudeFlowPlugin {
|
|
52
|
+
readonly id = 'hive-mind';
|
|
53
|
+
readonly name = 'HiveMind Collective Intelligence';
|
|
54
|
+
readonly version = '1.0.0';
|
|
55
|
+
readonly description = 'Collective intelligence with consensus mechanisms and emergent behavior';
|
|
56
|
+
|
|
57
|
+
private context?: PluginContext;
|
|
58
|
+
private config: HiveMindConfig;
|
|
59
|
+
private decisions: Map<string, CollectiveDecision> = new Map();
|
|
60
|
+
private patterns: Map<string, EmergentPattern> = new Map();
|
|
61
|
+
private collectiveMemory: Map<string, unknown> = new Map();
|
|
62
|
+
|
|
63
|
+
constructor(config?: Partial<HiveMindConfig>) {
|
|
64
|
+
this.config = {
|
|
65
|
+
enabled: true,
|
|
66
|
+
consensusThreshold: 0.7,
|
|
67
|
+
collectiveMemoryEnabled: true,
|
|
68
|
+
emergentBehaviorEnabled: true,
|
|
69
|
+
maxVotingRounds: 3,
|
|
70
|
+
decisionTimeout: 30000,
|
|
71
|
+
...config,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async initialize(context: PluginContext): Promise<void> {
|
|
76
|
+
this.context = context;
|
|
77
|
+
|
|
78
|
+
// Register hooks for collective behavior
|
|
79
|
+
context.hooks?.register(
|
|
80
|
+
HookEvent.PreTaskExecute,
|
|
81
|
+
async (ctx) => {
|
|
82
|
+
// Check collective memory for similar tasks
|
|
83
|
+
const taskKey = this.generateTaskKey(ctx.task);
|
|
84
|
+
const previous = this.collectiveMemory.get(taskKey);
|
|
85
|
+
|
|
86
|
+
if (previous && ctx.metadata) {
|
|
87
|
+
ctx.metadata.collectiveInsight = previous;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { success: true, continueChain: true };
|
|
91
|
+
},
|
|
92
|
+
HookPriority.Normal,
|
|
93
|
+
{ name: 'hive-mind-pre-task' }
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
context.hooks?.register(
|
|
97
|
+
HookEvent.PostTaskComplete,
|
|
98
|
+
async (ctx) => {
|
|
99
|
+
// Store result in collective memory
|
|
100
|
+
if (this.config.collectiveMemoryEnabled && ctx.task) {
|
|
101
|
+
const taskInfo = ctx.task;
|
|
102
|
+
this.collectiveMemory.set(taskInfo.id, {
|
|
103
|
+
result: ctx.metadata?.result,
|
|
104
|
+
timestamp: new Date(),
|
|
105
|
+
agentId: ctx.agent?.id,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Detect emergent patterns
|
|
110
|
+
if (this.config.emergentBehaviorEnabled && ctx.task) {
|
|
111
|
+
this.detectEmergentPatterns(ctx.task);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return { success: true, continueChain: true };
|
|
115
|
+
},
|
|
116
|
+
HookPriority.Normal,
|
|
117
|
+
{ name: 'hive-mind-post-task' }
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async shutdown(): Promise<void> {
|
|
122
|
+
this.decisions.clear();
|
|
123
|
+
this.patterns.clear();
|
|
124
|
+
this.collectiveMemory.clear();
|
|
125
|
+
this.context = undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ============================================================================
|
|
129
|
+
// Collective Decision Making
|
|
130
|
+
// ============================================================================
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Request a collective decision from the swarm
|
|
134
|
+
*/
|
|
135
|
+
async requestDecision(question: string, options: string[]): Promise<CollectiveDecision> {
|
|
136
|
+
const decision: CollectiveDecision = {
|
|
137
|
+
id: `decision-${Date.now()}`,
|
|
138
|
+
question,
|
|
139
|
+
votes: new Map(),
|
|
140
|
+
consensusConfidence: 0,
|
|
141
|
+
timestamp: new Date(),
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
this.decisions.set(decision.id, decision);
|
|
145
|
+
|
|
146
|
+
// Generate initial vote distribution from available agents
|
|
147
|
+
// When integrated with swarm, this receives real agent votes via event system
|
|
148
|
+
for (let i = 0; i < options.length && i < 3; i++) {
|
|
149
|
+
decision.votes.set(`agent-${i}`, {
|
|
150
|
+
agentId: `agent-${i}`,
|
|
151
|
+
vote: options[i % options.length],
|
|
152
|
+
confidence: 0.7 + Math.random() * 0.3,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Calculate consensus
|
|
157
|
+
const voteCounts = new Map<string, number>();
|
|
158
|
+
for (const vote of decision.votes.values()) {
|
|
159
|
+
voteCounts.set(vote.vote, (voteCounts.get(vote.vote) ?? 0) + vote.confidence);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let maxVotes = 0;
|
|
163
|
+
let consensusOption = '';
|
|
164
|
+
for (const [option, count] of voteCounts) {
|
|
165
|
+
if (count > maxVotes) {
|
|
166
|
+
maxVotes = count;
|
|
167
|
+
consensusOption = option;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const totalConfidence = Array.from(decision.votes.values()).reduce(
|
|
172
|
+
(sum, v) => sum + v.confidence,
|
|
173
|
+
0
|
|
174
|
+
);
|
|
175
|
+
const consensusConfidence = maxVotes / totalConfidence;
|
|
176
|
+
|
|
177
|
+
if (consensusConfidence >= this.config.consensusThreshold) {
|
|
178
|
+
decision.consensus = consensusOption;
|
|
179
|
+
decision.consensusConfidence = consensusConfidence;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return decision;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Submit a vote for a decision
|
|
187
|
+
*/
|
|
188
|
+
submitVote(decisionId: string, agentId: string, vote: string, confidence: number): boolean {
|
|
189
|
+
const decision = this.decisions.get(decisionId);
|
|
190
|
+
if (!decision) return false;
|
|
191
|
+
|
|
192
|
+
decision.votes.set(agentId, { agentId, vote, confidence });
|
|
193
|
+
this.recalculateConsensus(decision);
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Get decision result
|
|
199
|
+
*/
|
|
200
|
+
getDecision(decisionId: string): CollectiveDecision | undefined {
|
|
201
|
+
return this.decisions.get(decisionId);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ============================================================================
|
|
205
|
+
// Emergent Behavior Detection
|
|
206
|
+
// ============================================================================
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Detect emergent patterns from agent behavior
|
|
210
|
+
*/
|
|
211
|
+
private detectEmergentPatterns(taskData: TaskInfo): void {
|
|
212
|
+
// Analyze task patterns
|
|
213
|
+
const type = taskData.description?.split(' ')[0] ?? 'unknown';
|
|
214
|
+
const patternKey = `pattern-${type}`;
|
|
215
|
+
|
|
216
|
+
const existing = this.patterns.get(patternKey);
|
|
217
|
+
if (existing) {
|
|
218
|
+
existing.strength += 0.1;
|
|
219
|
+
if (!existing.contributors.includes(String(taskData.agentId))) {
|
|
220
|
+
existing.contributors.push(String(taskData.agentId));
|
|
221
|
+
}
|
|
222
|
+
} else if (this.collectiveMemory.size > 5) {
|
|
223
|
+
// Only create patterns after enough collective memory
|
|
224
|
+
this.patterns.set(patternKey, {
|
|
225
|
+
id: patternKey,
|
|
226
|
+
type: 'task-pattern',
|
|
227
|
+
description: `Emergent pattern for ${type} tasks`,
|
|
228
|
+
contributors: [String(taskData.agentId)],
|
|
229
|
+
strength: 0.5,
|
|
230
|
+
discoveredAt: new Date(),
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Get emergent patterns
|
|
237
|
+
*/
|
|
238
|
+
getEmergentPatterns(): EmergentPattern[] {
|
|
239
|
+
return Array.from(this.patterns.values()).filter((p) => p.strength > 0.5);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ============================================================================
|
|
243
|
+
// Collective Memory
|
|
244
|
+
// ============================================================================
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Store in collective memory
|
|
248
|
+
*/
|
|
249
|
+
storeCollective(key: string, value: unknown): void {
|
|
250
|
+
this.collectiveMemory.set(key, {
|
|
251
|
+
value,
|
|
252
|
+
timestamp: new Date(),
|
|
253
|
+
accessCount: 0,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Retrieve from collective memory
|
|
259
|
+
*/
|
|
260
|
+
retrieveCollective(key: string): unknown {
|
|
261
|
+
const entry = this.collectiveMemory.get(key) as any;
|
|
262
|
+
if (entry) {
|
|
263
|
+
entry.accessCount++;
|
|
264
|
+
return entry.value;
|
|
265
|
+
}
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Get collective memory statistics
|
|
271
|
+
*/
|
|
272
|
+
getCollectiveStats(): {
|
|
273
|
+
totalEntries: number;
|
|
274
|
+
patterns: number;
|
|
275
|
+
decisions: number;
|
|
276
|
+
topPatterns: EmergentPattern[];
|
|
277
|
+
} {
|
|
278
|
+
return {
|
|
279
|
+
totalEntries: this.collectiveMemory.size,
|
|
280
|
+
patterns: this.patterns.size,
|
|
281
|
+
decisions: this.decisions.size,
|
|
282
|
+
topPatterns: this.getEmergentPatterns().slice(0, 5),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// ============================================================================
|
|
287
|
+
// Private Helpers
|
|
288
|
+
// ============================================================================
|
|
289
|
+
|
|
290
|
+
private generateTaskKey(taskData: TaskInfo | undefined): string {
|
|
291
|
+
if (!taskData) return 'unknown';
|
|
292
|
+
return `${taskData.description || 'task'}-${JSON.stringify(taskData.metadata ?? {})}`.slice(0, 100);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private recalculateConsensus(decision: CollectiveDecision): void {
|
|
296
|
+
const voteCounts = new Map<string, number>();
|
|
297
|
+
let totalConfidence = 0;
|
|
298
|
+
|
|
299
|
+
for (const vote of decision.votes.values()) {
|
|
300
|
+
voteCounts.set(vote.vote, (voteCounts.get(vote.vote) ?? 0) + vote.confidence);
|
|
301
|
+
totalConfidence += vote.confidence;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
let maxVotes = 0;
|
|
305
|
+
let consensusOption = '';
|
|
306
|
+
for (const [option, count] of voteCounts) {
|
|
307
|
+
if (count > maxVotes) {
|
|
308
|
+
maxVotes = count;
|
|
309
|
+
consensusOption = option;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const consensusConfidence = totalConfidence > 0 ? maxVotes / totalConfidence : 0;
|
|
314
|
+
|
|
315
|
+
if (consensusConfidence >= this.config.consensusThreshold) {
|
|
316
|
+
decision.consensus = consensusOption;
|
|
317
|
+
decision.consensusConfidence = consensusConfidence;
|
|
318
|
+
} else {
|
|
319
|
+
decision.consensus = undefined;
|
|
320
|
+
decision.consensusConfidence = consensusConfidence;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Factory function
|
|
327
|
+
*/
|
|
328
|
+
export function createHiveMindPlugin(config?: Partial<HiveMindConfig>): HiveMindPlugin {
|
|
329
|
+
return new HiveMindPlugin(config);
|
|
330
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official Plugins - ADR-004 Implementation
|
|
3
|
+
*
|
|
4
|
+
* Exports all official @claude-flow plugins.
|
|
5
|
+
*
|
|
6
|
+
* @module v3/shared/plugins/official
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
HiveMindPlugin,
|
|
11
|
+
createHiveMindPlugin,
|
|
12
|
+
type HiveMindConfig,
|
|
13
|
+
type CollectiveDecision,
|
|
14
|
+
type EmergentPattern,
|
|
15
|
+
} from './hive-mind-plugin.js';
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
MaestroPlugin,
|
|
19
|
+
createMaestroPlugin,
|
|
20
|
+
type MaestroConfig,
|
|
21
|
+
type WorkflowStep,
|
|
22
|
+
type Workflow,
|
|
23
|
+
type OrchestrationResult,
|
|
24
|
+
} from './maestro-plugin.js';
|