alphe-redis-mcp-server
Version:
The most comprehensive Redis MCP Server for Alphe.AI - Optimized for sub-5 second response times with multi-layer caching
457 lines (399 loc) • 14.2 kB
text/typescript
/**
* 🧠 OPENROUTER COGNITIVE ORCHESTRATOR
* Manages 6 parallel cognitive agents through OpenRouter API for Vercel deployment
*/
interface CognitiveAgent {
name: string;
model: string;
role: string;
apiEndpoint: string;
status: 'idle' | 'busy' | 'error';
latency: number;
requests: number;
lastUsed: number;
}
interface CognitiveResponse {
agent: string;
result: string;
latency: number;
tokens?: number;
error?: string;
}
interface QueryContext {
domain?: string;
urgency?: number;
complexity?: number;
useCache?: boolean;
}
export class OpenRouterCognitiveOrchestrator {
private agents: Map<string, CognitiveAgent> = new Map();
private apiKey: string;
private baseURL = 'https://openrouter.ai/api/v1';
private warmingInterval?: NodeJS.Timeout;
private readonly COGNITIVE_AGENTS: Omit<CognitiveAgent, 'status' | 'latency' | 'requests' | 'lastUsed'>[] = [
{
name: 'perception_agent',
model: 'google/gemma-2-9b-it:free',
role: 'Intent & entity extraction',
apiEndpoint: '/chat/completions'
},
{
name: 'context_engineer',
model: 'microsoft/phi-3-mini-128k-instruct:free',
role: 'Query optimization',
apiEndpoint: '/chat/completions'
},
{
name: 'planning_agent',
model: 'qwen/qwen-2.5-coder-32b-instruct',
role: 'Execution planning',
apiEndpoint: '/chat/completions'
},
{
name: 'reasoning_agent',
model: 'deepseek/deepseek-r1-lite-base',
role: 'Logical analysis',
apiEndpoint: '/chat/completions'
},
{
name: 'reflection_agent',
model: 'meta-llama/llama-3.3-70b-instruct',
role: 'Quality improvement',
apiEndpoint: '/chat/completions'
},
{
name: 'orchestrator_agent',
model: 'mistralai/mixtral-8x7b-instruct:nitro',
role: 'Final synthesis',
apiEndpoint: '/chat/completions'
}
];
constructor(apiKey?: string) {
this.apiKey = apiKey || process.env.OPENROUTER_API_KEY || '';
if (!this.apiKey) {
throw new Error('OPENROUTER_API_KEY is required');
}
this.initializeAgents();
}
private initializeAgents() {
this.COGNITIVE_AGENTS.forEach(agentConfig => {
this.agents.set(agentConfig.name, {
...agentConfig,
status: 'idle',
latency: 0,
requests: 0,
lastUsed: 0
});
});
}
async activateAllAgents(): Promise<{ successful: number; failed: number; details: any[] }> {
console.log(`🚀 Activating ${this.agents.size} cognitive agents via OpenRouter...`);
const results = await Promise.allSettled(
Array.from(this.agents.values()).map(agent => this.testAgent(agent))
);
const successful = results.filter(r => r.status === 'fulfilled' && r.value.success).length;
const failed = results.length - successful;
console.log(`✅ Activated: ${successful}/${this.agents.size} agents`);
console.log(`❌ Failed: ${failed}/${this.agents.size} agents`);
if (successful > 0) {
this.startAgentWarming();
}
return {
successful,
failed,
details: results.map((r, i) => ({
agent: Array.from(this.agents.keys())[i],
...((r.status === 'fulfilled') ? r.value : { success: false, error: r.reason })
}))
};
}
private async testAgent(agent: CognitiveAgent): Promise<{ success: boolean; latency?: number; error?: string }> {
try {
const startTime = Date.now();
const response = await fetch(`${this.baseURL}${agent.apiEndpoint}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://alphe.ai',
'X-Title': 'Alphe.AI Redis MCP'
},
body: JSON.stringify({
model: agent.model,
messages: [
{ role: 'system', content: `You are ${agent.name} specialized in ${agent.role}.` },
{ role: 'user', content: 'Test activation - respond with just "ready"' }
],
max_tokens: 5,
temperature: 0.1
})
});
const latency = Date.now() - startTime;
if (response.ok) {
const data = await response.json();
// Update agent status
agent.status = 'idle';
agent.latency = latency;
agent.lastUsed = Date.now();
console.log(`✅ ${agent.name} activated - ${latency}ms - ${agent.role}`);
return { success: true, latency };
} else {
const errorText = await response.text();
console.log(`❌ ${agent.name} failed: HTTP ${response.status} - ${errorText}`);
agent.status = 'error';
return { success: false, error: `HTTP ${response.status}: ${errorText}` };
}
} catch (error: any) {
console.log(`❌ ${agent.name} error: ${error.message}`);
agent.status = 'error';
return { success: false, error: error.message };
}
}
private startAgentWarming() {
console.log('🔥 Starting agent warming system...');
this.warmingInterval = setInterval(async () => {
const activeAgents = Array.from(this.agents.values()).filter(a => a.status === 'idle');
if (activeAgents.length === 0) return;
// Pick random agent to warm
const randomAgent = activeAgents[Math.floor(Math.random() * activeAgents.length)];
try {
const response = await fetch(`${this.baseURL}${randomAgent.apiEndpoint}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://alphe.ai',
'X-Title': 'Alphe.AI Redis MCP'
},
body: JSON.stringify({
model: randomAgent.model,
messages: [{ role: 'user', content: 'ping' }],
max_tokens: 1,
temperature: 0.1
}),
signal: AbortSignal.timeout(3000)
});
if (response.ok) {
randomAgent.lastUsed = Date.now();
randomAgent.status = 'idle';
console.log(`🔥 Warmed ${randomAgent.name}`);
}
} catch (error) {
console.log(`⚠️ Failed to warm ${randomAgent.name}`);
randomAgent.status = 'error';
}
}, 15000); // Every 15 seconds
}
async processQuery(
query: string,
context: QueryContext = {},
useAgents = true
): Promise<{
response: string;
agentsUsed: string[];
latency: number;
details: CognitiveResponse[];
}> {
if (!useAgents || this.agents.size === 0) {
return {
response: `Direct response: ${query}`,
agentsUsed: [],
latency: 0,
details: []
};
}
console.log(`🧠 Processing query with ${this.agents.size} agents: "${query}"`);
const startTime = Date.now();
// Select best agents based on context
const selectedAgents = this.selectOptimalAgents(context);
console.log(`🚀 Using ${selectedAgents.length} agents in parallel...`);
const results = await Promise.allSettled(
selectedAgents.map(agent => this.queryAgent(agent, query, context))
);
const successfulResults = results
.filter(r => r.status === 'fulfilled')
.map(r => (r as PromiseFulfilledResult<CognitiveResponse>).value)
.filter(v => !v.error);
const totalLatency = Date.now() - startTime;
// Synthesize responses using orchestrator
const finalResponse = await this.synthesizeResponse(query, successfulResults);
console.log(`✅ Query processed in ${totalLatency}ms with ${successfulResults.length} agents`);
return {
response: finalResponse,
agentsUsed: successfulResults.map(r => r.agent),
latency: totalLatency,
details: successfulResults
};
}
private selectOptimalAgents(context: QueryContext): CognitiveAgent[] {
const activeAgents = Array.from(this.agents.values())
.filter(a => a.status === 'idle')
.sort((a, b) => a.latency - b.latency);
const urgency = context.urgency || 5;
const complexity = context.complexity || 5;
// Select agents based on context
if (urgency >= 8) {
// High urgency: use 3 fastest agents
return activeAgents.slice(0, 3);
} else if (complexity >= 8) {
// High complexity: use all available agents
return activeAgents;
} else {
// Normal: use 4 agents
return activeAgents.slice(0, 4);
}
}
private async queryAgent(
agent: CognitiveAgent,
query: string,
context: QueryContext
): Promise<CognitiveResponse> {
const startTime = Date.now();
agent.status = 'busy';
try {
const prompt = this.generatePrompt(agent.name, query, context);
const response = await fetch(`${this.baseURL}${agent.apiEndpoint}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://alphe.ai',
'X-Title': 'Alphe.AI Redis MCP'
},
body: JSON.stringify({
model: agent.model,
messages: [
{ role: 'system', content: `You are ${agent.name} specialized in ${agent.role}.` },
{ role: 'user', content: prompt }
],
max_tokens: 200,
temperature: 0.3,
top_p: 0.9
}),
signal: AbortSignal.timeout(10000)
});
const latency = Date.now() - startTime;
if (response.ok) {
const data = await response.json();
agent.status = 'idle';
agent.requests++;
agent.lastUsed = Date.now();
agent.latency = (agent.latency + latency) / 2; // Moving average
return {
agent: agent.name,
result: data.choices[0]?.message?.content || '',
latency,
tokens: data.usage?.total_tokens || 0
};
} else {
throw new Error(`HTTP ${response.status}`);
}
} catch (error: any) {
agent.status = 'error';
return {
agent: agent.name,
result: '',
latency: Date.now() - startTime,
error: error.message
};
}
}
private generatePrompt(agentName: string, query: string, context: QueryContext): string {
const base = `Context: ${JSON.stringify(context)}\nQuery: "${query}"\n\n`;
switch (agentName) {
case 'perception_agent':
return base + 'Extract key intent and important entities. Be concise and specific.';
case 'context_engineer':
return base + 'Optimize this query for clarity. What context might be missing?';
case 'planning_agent':
return base + 'Create a step-by-step execution plan to address this query.';
case 'reasoning_agent':
return base + 'Apply logical reasoning. What are the key considerations?';
case 'reflection_agent':
return base + 'Reflect on potential improvements or alternative approaches.';
case 'orchestrator_agent':
return base + 'Provide a comprehensive, helpful response.';
default:
return base + 'Process this query helpfully and concisely.';
}
}
private async synthesizeResponse(query: string, results: CognitiveResponse[]): Promise<string> {
if (results.length === 0) {
return `Processed query: ${query}`;
}
// Use orchestrator agent to synthesize if available
const orchestrator = this.agents.get('orchestrator_agent');
if (orchestrator && orchestrator.status === 'idle') {
try {
const synthesisPrompt = `
Original Query: "${query}"
Agent Responses:
${results.map(r => `${r.agent}: ${r.result}`).join('\n')}
Synthesize these responses into a comprehensive, helpful answer:`;
const response = await fetch(`${this.baseURL}${orchestrator.apiEndpoint}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://alphe.ai',
'X-Title': 'Alphe.AI Redis MCP'
},
body: JSON.stringify({
model: orchestrator.model,
messages: [
{ role: 'system', content: 'You are the orchestrator agent. Synthesize responses from other agents.' },
{ role: 'user', content: synthesisPrompt }
],
max_tokens: 300,
temperature: 0.2
}),
signal: AbortSignal.timeout(5000)
});
if (response.ok) {
const data = await response.json();
return data.choices[0]?.message?.content || results[0].result;
}
} catch (error) {
console.log('⚠️ Synthesis failed, using first result');
}
}
return `Enhanced by ${results.length} cognitive agents: ${results[0].result}`;
}
getAgentStatus(): Record<string, any> {
const status: Record<string, any> = {};
this.agents.forEach((agent, name) => {
status[name] = {
model: agent.model,
role: agent.role,
status: agent.status,
latency: agent.latency,
requests: agent.requests,
lastUsed: agent.lastUsed ? new Date(agent.lastUsed).toISOString() : null
};
});
return status;
}
getPerformanceMetrics() {
const agents = Array.from(this.agents.values());
const active = agents.filter(a => a.status === 'idle').length;
const busy = agents.filter(a => a.status === 'busy').length;
const errors = agents.filter(a => a.status === 'error').length;
const avgLatency = agents.reduce((sum, a) => sum + a.latency, 0) / agents.length;
const totalRequests = agents.reduce((sum, a) => sum + a.requests, 0);
return {
totalAgents: agents.length,
activeAgents: active,
busyAgents: busy,
errorAgents: errors,
averageLatency: Math.round(avgLatency),
totalRequests,
uptime: process.uptime()
};
}
shutdown() {
if (this.warmingInterval) {
clearInterval(this.warmingInterval);
}
console.log('👋 Cognitive orchestrator shutdown complete');
}
}