claude-flow-novice
Version:
Claude Flow Novice - Advanced orchestration platform for multi-agent AI workflows with CFN Loop architecture Includes Local RuVector Accelerator and all CFN skills for complete functionality.
123 lines (122 loc) • 4.32 kB
JavaScript
/**
* Agent Lifecycle State Management
* Handles agent state transitions, memory persistence, and lifecycle hooks
* Integrated with dependency tracking to prevent premature completion
*/ import { EventEmitter } from 'node:events';
import { DependencyType } from '../lifecycle/dependency-tracker.js';
// Import the function directly
import { getDependencyTracker } from '../lifecycle/dependency-tracker.js';
export class AgentLifecycleManager extends EventEmitter {
agents = new Map();
memoryStorage = new Map();
dependencyTracker = getDependencyTracker('lifecycle-manager');
isInitialized = false;
constructor(){
super();
this.setupDependencyEventHandlers();
}
setupDependencyEventHandlers() {
this.dependencyTracker.on('agent:completion_approved', (event)=>{
this.handleCompletionApproved(event.agentId);
});
this.dependencyTracker.on('agent:completion_blocked', (event)=>{
this.handleCompletionBlocked(event.agentId, event.blockerInfo);
});
this.dependencyTracker.on('dependency:violation', (violation)=>{
this.handleDependencyViolation(violation);
});
}
handleCompletionApproved(agentId) {
// Placeholder implementation
}
handleCompletionBlocked(agentId, blockerInfo) {
// Placeholder implementation
}
handleDependencyViolation(violation) {
// Placeholder implementation
}
async initializeAgent(agentId, agentDefinition, taskId) {
const context = {
agentId,
agentDefinition,
state: 'initializing',
startTime: new Date(),
lastActivity: new Date(),
taskId,
memory: new Map(),
retryCount: 0,
maxRetries: agentDefinition.lifecycle?.max_retries ?? 3,
errorHistory: [],
stateHistory: [
{
state: 'initializing',
timestamp: new Date(),
reason: 'Agent initialization'
}
]
};
this.agents.set(agentId, context);
return context;
}
async transitionState(agentId, newState, reason) {
return true;
}
async handleTaskComplete(agentId, taskResult, success) {
return {
success: true
};
}
async handleRerunRequest(agentId, reason) {
return {
success: true
};
}
async cleanupAgent(agentId) {
return true;
}
getAgentContext(agentId) {
return this.agents.get(agentId);
}
updateAgentMemory(agentId, key, value) {
const context = this.agents.get(agentId);
if (!context) return false;
context.memory.set(key, value);
context.lastActivity = new Date();
return true;
}
getAgentMemory(agentId, key) {
const context = this.agents.get(agentId);
return context?.memory.get(key);
}
async registerAgentDependency(dependentAgentId, providerAgentId, type, options) {
return this.dependencyTracker.registerDependency(dependentAgentId, providerAgentId, type || DependencyType.COMPLETION, options);
}
async removeAgentDependency(dependencyId) {
return this.dependencyTracker.removeDependency(dependencyId);
}
async forceAgentCompletion(agentId, reason) {
return this.dependencyTracker.forceAgentCompletion(agentId, reason);
}
getAgentDependencyStatus(agentId) {
const context = this.agents.get(agentId);
return {
canComplete: context?.pendingCompletion === false,
dependencies: context?.dependencies ?? [],
dependentAgents: context?.dependentAgents ?? [],
pendingCompletion: !!context?.pendingCompletion,
blockerInfo: context?.completionBlocker
};
}
async initialize() {
if (this.isInitialized) return;
await this.dependencyTracker.initialize();
this.isInitialized = true;
}
async shutdown() {
if (!this.isInitialized) return;
await this.dependencyTracker.shutdown();
this.isInitialized = false;
}
}
export const lifecycleManager = new AgentLifecycleManager();
//# sourceMappingURL=lifecycle-manager.js.map