📄 patterns.ts  •  6013 bytes
/**
 * 意图识别 - 关键词模式匹配
 * Phase 1: 基础关键词 + 正则模式
 */

/** 意图类型枚举 */
export type IntentType = 
  | 'code'      // 代码生成
  | 'debug'      // 调试修复
  | 'explain'   // 解释说明
  | 'refactor'  // 重构优化
  | 'test'      // 测试相关
  | 'chat'      // 闲聊问答
  | 'skill'     // 技能执行
  | 'file'      // 文件操作
  | 'search'    // 搜索查询
  | 'config'    // 配置相关
  | 'unknown'   // 未知

/** 意图配置 */
export interface IntentPattern {
  type: IntentType
  keywords: string[]
  regex?: RegExp[]
  priority: number // 优先级,数字越大优先级越高
  fastPath: boolean // 是否走快速通道(直接执行)
}

/**
 * 所有意图模式 — 2026-04-30 扩展关键词库
 * 来源:研发日常用语高频词分析,覆盖「写→调→改→优→查」完整链路
 */
export const INTENT_PATTERNS: IntentPattern[] = [
  // ── 技能执行 - 最高优先级 ──
  {
    type: 'skill',
    keywords: ['技能', 'skill', '执行', 'run', 'doc-push', '文档推送'],
    priority: 100,
    fastPath: true,
  },

  // ── 调试修复 - 优先级仅次于技能 ──
  {
    type: 'debug',
    keywords: [
      // 问题报告(用户原话)
      '报错', '错误', 'bug', '崩溃', '不行', '没反应', '不能用', '卡住', '超时',
      '程序崩溃',
      // 调试动作
      '调试', '修复', '修正', '改个', '修个', '处理', '修', '改',
      // 技术异常
      'debug', 'fix', 'exception', 'crash', '失败', '不对',
      '出问题了', '有问题', '解决', '排查', '定位',
      'is not a function', 'undefined', 'null', 'TypeError', 'ReferenceError',
    ],
    priority: 85,
    fastPath: false,
  },

  // ── 文件操作 ──
  {
    type: 'file',
    keywords: [
      '创建文件', '新建文件', 'write', '写入', 'read', '读取',
      'edit', '编辑', 'delete', '删除', 'copy', '移动', '重命名',
      '文件', 'folder', '目录', '文件夹',
    ],
    priority: 60,
    fastPath: false,
  },

  // ── 代码生成 — 编码实现 + 任务特征 ──
  {
    type: 'code',
    keywords: [
      // 编码实现动作
      '写', '编', '生成', '创建', '构建', '搭建', '实现', '开发', '完成', '重写', '移植',
      '写个', '编个', '做个', '写一个', '生成一个', '做一个',
      '帮我写', '帮我编', '帮我做', '帮我用', '帮我生成',
      '编写', '编程', '编程实现', '开发一个', '开发程序',
      'create', 'generate', 'function', 'class', 'module', 'script',
      // 任务特征名词(出现即暗示要写代码)
      '程序', '代码', '脚本', '功能', '模块', '组件', '页面', '网站',
      'APP', '应用', '接口', 'API', '算法', '爬虫', '自动化', '数据', '项目',
      'Demo', 'demo', '原型', '小程序',
      '写个函数', '写个类', '写个组件', '写个接口', '写个程序', '写个应用',
      'algorithm', '排序', '链表', '树',
    ],
    priority: 55,
    fastPath: false,
  },

  // ── 重构优化 ──
  {
    type: 'refactor',
    keywords: [
      '优化', '改进', '提升', '加速', '重构', '精简', '整理', '美化',
      'refactor', 'improve', 'enhance', 'better', 'clean', '简化', '重写',
    ],
    priority: 55,
    fastPath: false,
  },

  // ── 测试相关 ──
  {
    type: 'test',
    keywords: [
      'test', '测试', 'unit', '单元测试', '用例', 'case',
      '验证', 'verify', 'check',
    ],
    priority: 50,
    fastPath: false,
  },

  // ── 配置相关 ──
  {
    type: 'config',
    keywords: [
      'config', '配置', 'setting', 'set up', '安装', 'install',
      'setup', '环境', 'env', 'deploy', '部署',
    ],
    priority: 50,
    fastPath: false,
  },

  // ── 搜索查询 ──
  {
    type: 'search',
    keywords: [
      'search', 'grep', 'find', '查找', '搜索', '搜索代码',
      'look for', '在哪', '找找',
    ],
    priority: 45,
    fastPath: false,
  },

  // ── 解释说明 + 分析建议 + 文档转换 ──
  {
    type: 'explain',
    keywords: [
      '解释', '说明', '是什么', '什么意思', '什么是',
      'why', '为什么', 'how', '如何', '原理', '概念',
      '讲讲', '介绍', '介绍一下', '通俗地', '简单说',
      '分析', '检查', '审查', '总结', '建议', '教我',
      '帮我看看', '帮我检查', '帮我分析',
      'explain', 'review',
      '格式化', '转换', '翻译', '注释', '文档', 'README', 'readme',
    ],
    regex: [
      // 写X文档/README/注释 → 文档转换
      /写.{0,5}(readme|文档|说明|注释|说明文档)/i,
      /生成.{0,5}(readme|文档|说明)/i,
    ],
    priority: 45,
    fastPath: true,
  },

  // ── 闲聊问答 - 最低非unknown优先级 ──
  {
    type: 'chat',
    keywords: [
      '你好', 'hi', 'hello', 'hey', '在吗', '在不在',
      '谢谢', 'thanks', 'ok', '好的', '嗯', '好吧',
      '天气', 'news', '新闻', '今天', 'what', 'who', 'which',
    ],
    priority: 10,
    fastPath: true,
  },
]

/**
 * 从文本中提取关键信息
 */
export function extractParams(text: string): Record<string, any> {
  const params: Record<string, any> = {}
  
  // 提取文件路径
  const filePathRegex = /[\w\-\./]+\.[a-zA-Z]+/g
  const filePaths = text.match(filePathRegex)
  if (filePaths) {
    params.filePaths = filePaths
  }
  
  // 提取编程语言
  const langRegex = /(?:typescript|javascript|python|rust|go|java|c\+\+|c#)/gi
  const languages = text.match(langRegex)
  if (languages) {
    params.languages = [...new Set(languages.map(l => l.toLowerCase()))]
  }
  
  // 提取命令
  const cmdRegex = /(?:bun|npm|pnpm|yarn|node|python|pip|git|cargo)[\s\S]+?(?=\s|$|[,,。])/gi
  const commands = text.match(cmdRegex)
  if (commands) {
    params.commands = commands
  }
  
  return params
}