diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts index b74c4ffa2e..4688846695 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts @@ -2591,6 +2591,141 @@ export const hooksWorkerDetect: MCPTool = { }, }; +// Model router - lazy loaded +let modelRouterInstance: Awaited> | null = null; +async function getModelRouterInstance() { + if (!modelRouterInstance) { + try { + const { getModelRouter } = await import('../ruvector/model-router.js'); + modelRouterInstance = getModelRouter(); + } catch { + modelRouterInstance = null; + } + } + return modelRouterInstance; +} + +// Model route tool - intelligent model selection +export const hooksModelRoute: MCPTool = { + name: 'hooks/model-route', + description: 'Route task to optimal Claude model (haiku/sonnet/opus) based on complexity', + inputSchema: { + type: 'object', + properties: { + task: { type: 'string', description: 'Task description to analyze' }, + preferSpeed: { type: 'boolean', description: 'Prefer faster models when possible' }, + preferCost: { type: 'boolean', description: 'Prefer cheaper models when possible' }, + }, + required: ['task'], + }, + handler: async (params: Record) => { + const task = params.task as string; + const router = await getModelRouterInstance(); + + if (!router) { + // Fallback to simple heuristic + const complexity = analyzeComplexityFallback(task); + return { + model: complexity > 0.7 ? 'opus' : complexity > 0.4 ? 'sonnet' : 'haiku', + confidence: 0.7, + complexity, + reasoning: 'Fallback heuristic (model router not available)', + implementation: 'fallback', + }; + } + + const result = await router.route(task); + return { + model: result.model, + confidence: result.confidence, + uncertainty: result.uncertainty, + complexity: result.complexity, + reasoning: result.reasoning, + alternatives: result.alternatives, + inferenceTimeUs: result.inferenceTimeUs, + costMultiplier: result.costMultiplier, + implementation: 'tiny-dancer-neural', + }; + }, +}; + +// Model route outcome - record outcome for learning +export const hooksModelOutcome: MCPTool = { + name: 'hooks/model-outcome', + description: 'Record model routing outcome for learning', + inputSchema: { + type: 'object', + properties: { + task: { type: 'string', description: 'Original task' }, + model: { type: 'string', enum: ['haiku', 'sonnet', 'opus'], description: 'Model used' }, + outcome: { type: 'string', enum: ['success', 'failure', 'escalated'], description: 'Task outcome' }, + }, + required: ['task', 'model', 'outcome'], + }, + handler: async (params: Record) => { + const task = params.task as string; + const model = params.model as 'haiku' | 'sonnet' | 'opus'; + const outcome = params.outcome as 'success' | 'failure' | 'escalated'; + + const router = await getModelRouterInstance(); + if (router) { + router.recordOutcome(task, model, outcome); + } + + return { + recorded: true, + task: task.slice(0, 50), + model, + outcome, + timestamp: new Date().toISOString(), + }; + }, +}; + +// Model router stats +export const hooksModelStats: MCPTool = { + name: 'hooks/model-stats', + description: 'Get model routing statistics', + inputSchema: { + type: 'object', + properties: {}, + }, + handler: async () => { + const router = await getModelRouterInstance(); + if (!router) { + return { + available: false, + message: 'Model router not initialized', + }; + } + + const stats = router.getStats(); + return { + available: true, + ...stats, + timestamp: new Date().toISOString(), + }; + }, +}; + +// Simple fallback complexity analyzer +function analyzeComplexityFallback(task: string): number { + const taskLower = task.toLowerCase(); + + // High complexity indicators + const highIndicators = ['architect', 'design', 'refactor', 'security', 'audit', 'complex', 'analyze']; + const highCount = highIndicators.filter(ind => taskLower.includes(ind)).length; + + // Low complexity indicators + const lowIndicators = ['simple', 'typo', 'format', 'rename', 'comment']; + const lowCount = lowIndicators.filter(ind => taskLower.includes(ind)).length; + + // Base on length + const lengthScore = Math.min(1, task.length / 200); + + return Math.min(1, Math.max(0, 0.3 + highCount * 0.2 - lowCount * 0.15 + lengthScore * 0.2)); +} + // Worker cancel tool export const hooksWorkerCancel: MCPTool = { name: 'hooks/worker-cancel',