📄 router.ts  •  4554 bytes
/**
 * 意图识别 - 路由分发器
 * Phase 1: 根据意图类型选择处理方式
 */
import { classifyIntent, classifyIntentAsync, type IntentResult } from './classifier'

/** 路由决策 */
export interface RouteDecision {
  handler: 'chat' | 'planner' | 'skill' | 'direct'
  priority: number
  message?: string
}

/** 路由规则 */
interface RouteRule {
  match: (intent: IntentResult) => boolean
  handler: RouteDecision['handler']
  priority: number
  message?: string
}

/** 路由规则列表 - 按优先级排序 */
const ROUTE_RULES: RouteRule[] = [
  // 技能执行 - 最高优先级
  {
    match: (i) => i.type === 'skill',
    handler: 'skill',
    priority: 100,
    message: '🎯 检测到技能执行请求',
  },
  // 调试修复 - 次高优先级
  {
    match: (i) => i.type === 'debug',
    handler: 'planner',
    priority: 90,
    message: '🔧 检测到调试请求,进入计划模式',
  },
  // 解释说明 - 走快速通道
  {
    match: (i) => i.type === 'explain',
    handler: 'chat',
    priority: 75,
    message: '📖 解释模式',
  },
  // 闲聊问答 - 走快速通道
  {
    match: (i) => i.type === 'chat' || i.type === 'unknown',
    handler: 'chat',
    priority: 70,
    message: '💬 闲聊模式',
  },
  // 简单任务(高置信度+快速通道)走直接执行
  {
    match: (i) => i.fastPath && i.confidence > 0.5,
    handler: 'direct',
    priority: 60,
    message: '⚡ 快速执行',
  },
  // 复杂任务走计划模式
  {
    match: (i) => i.confidence > 0.3,
    handler: 'planner',
    priority: 50,
    message: '📋 进入计划模式',
  },
  // 默认走直接执行
  {
    match: () => true,
    handler: 'direct',
    priority: 10,
    message: '⚡ 直接执行',
  },
]

/**
 * 路由决策函数
 * @param input 用户输入
 * @returns 路由决策
 */
export function routeIntent(input: string): {
  intent: IntentResult
  decision: RouteDecision
} {
  // 1. 意图分类(同步,仅关键词)
  const intent = classifyIntent(input)
  
  // 2. 选择路由
  let bestRule: RouteRule | null = null
  for (const rule of ROUTE_RULES) {
    if (rule.match(intent)) {
      bestRule = rule
      break
    }
  }
  
  // 3. 构建决策
  const decision: RouteDecision = {
    handler: bestRule?.handler || 'direct',
    priority: bestRule?.priority || 10,
    message: bestRule?.message,
  }
  
  return { intent, decision }
}

/**
 * 路由决策函数(异步版本,含向量语义搜索)
 * 当关键词匹配置信度低时,使用向量搜索补充
 */
export async function routeIntentAsync(input: string): Promise<{
  intent: IntentResult
  decision: RouteDecision
}> {
  // 1. 意图分类(异步,关键词 + 向量)
  const intent = await classifyIntentAsync(input)
  
  // 2. 选择路由
  let bestRule: RouteRule | null = null
  for (const rule of ROUTE_RULES) {
    if (rule.match(intent)) {
      bestRule = rule
      break
    }
  }
  
  // 3. 构建决策
  const decision: RouteDecision = {
    handler: bestRule?.handler || 'direct',
    priority: bestRule?.priority || 10,
    message: bestRule?.message,
  }
  
  return { intent, decision }
}

/**
 * 获取路由处理器名称
 */
export function getHandlerName(handler: RouteDecision['handler']): string {
  const names: Record<RouteDecision['handler'], string> = {
    chat: '💬 对话处理器',
    planner: '📋 计划处理器',
    skill: '🎯 技能处理器',
    direct: '⚡ 直接执行',
  }
  return names[handler]
}

/**
 * 获取处理建议
 */
export function getProcessingAdvice(intent: IntentResult, decision: RouteDecision): string[] {
  const advice: string[] = []
  
  // 基于意图的建议
  switch (intent.type) {
    case 'code':
      advice.push('• 确认代码语言和框架')
      advice.push('• 考虑错误处理和边界条件')
      break
    case 'debug':
      advice.push('• 先复现问题')
      advice.push('• 分析错误堆栈')
      advice.push('• 逐层排查')
      break
    case 'file':
      advice.push('• 确认文件路径')
      advice.push('• 检查目录是否存在')
      advice.push('• 备份重要文件')
      break
  }
  
  // 基于路由的建议
  if (decision.handler === 'planner') {
    advice.push('• 生成详细执行计划')
    advice.push('• 等待用户确认')
    advice.push('• 分步执行验证')
  } else if (decision.handler === 'direct') {
    advice.push('• 直接执行工具调用')
    advice.push('• 快速返回结果')
  }
  
  return advice
}