mycohive-claw 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +222 -0
  3. package/dist/circuit-breaker.d.ts +82 -0
  4. package/dist/circuit-breaker.d.ts.map +1 -0
  5. package/dist/circuit-breaker.js +214 -0
  6. package/dist/circuit-breaker.js.map +1 -0
  7. package/dist/context-tree.d.ts +94 -0
  8. package/dist/context-tree.d.ts.map +1 -0
  9. package/dist/context-tree.js +624 -0
  10. package/dist/context-tree.js.map +1 -0
  11. package/dist/event-bus.d.ts +174 -0
  12. package/dist/event-bus.d.ts.map +1 -0
  13. package/dist/event-bus.js +750 -0
  14. package/dist/event-bus.js.map +1 -0
  15. package/dist/i18n.d.ts +91 -0
  16. package/dist/i18n.d.ts.map +1 -0
  17. package/dist/i18n.js +150 -0
  18. package/dist/i18n.js.map +1 -0
  19. package/dist/index.d.ts +38 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +301 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/intent-analyzer.d.ts +119 -0
  24. package/dist/intent-analyzer.d.ts.map +1 -0
  25. package/dist/intent-analyzer.js +563 -0
  26. package/dist/intent-analyzer.js.map +1 -0
  27. package/dist/logger.d.ts +46 -0
  28. package/dist/logger.d.ts.map +1 -0
  29. package/dist/logger.js +126 -0
  30. package/dist/logger.js.map +1 -0
  31. package/dist/metrics.d.ts +98 -0
  32. package/dist/metrics.d.ts.map +1 -0
  33. package/dist/metrics.js +192 -0
  34. package/dist/metrics.js.map +1 -0
  35. package/dist/openclaw-api.d.ts +122 -0
  36. package/dist/openclaw-api.d.ts.map +1 -0
  37. package/dist/openclaw-api.js +8 -0
  38. package/dist/openclaw-api.js.map +1 -0
  39. package/dist/rate-limiter.d.ts +46 -0
  40. package/dist/rate-limiter.d.ts.map +1 -0
  41. package/dist/rate-limiter.js +134 -0
  42. package/dist/rate-limiter.js.map +1 -0
  43. package/dist/result-cache.d.ts +76 -0
  44. package/dist/result-cache.d.ts.map +1 -0
  45. package/dist/result-cache.js +158 -0
  46. package/dist/result-cache.js.map +1 -0
  47. package/dist/router.d.ts +90 -0
  48. package/dist/router.d.ts.map +1 -0
  49. package/dist/router.js +447 -0
  50. package/dist/router.js.map +1 -0
  51. package/dist/setup-entry.d.ts +18 -0
  52. package/dist/setup-entry.d.ts.map +1 -0
  53. package/dist/setup-entry.js +64 -0
  54. package/dist/setup-entry.js.map +1 -0
  55. package/dist/token-counter.d.ts +57 -0
  56. package/dist/token-counter.d.ts.map +1 -0
  57. package/dist/token-counter.js +125 -0
  58. package/dist/token-counter.js.map +1 -0
  59. package/dist/tool-handlers.d.ts +52 -0
  60. package/dist/tool-handlers.d.ts.map +1 -0
  61. package/dist/tool-handlers.js +317 -0
  62. package/dist/tool-handlers.js.map +1 -0
  63. package/dist/tools/mycohive-tools.d.ts +11 -0
  64. package/dist/tools/mycohive-tools.d.ts.map +1 -0
  65. package/dist/tools/mycohive-tools.js +160 -0
  66. package/dist/tools/mycohive-tools.js.map +1 -0
  67. package/dist/types.d.ts +58 -0
  68. package/dist/types.d.ts.map +1 -0
  69. package/dist/types.js +57 -0
  70. package/dist/types.js.map +1 -0
  71. package/dist/workspace-loader.d.ts +67 -0
  72. package/dist/workspace-loader.d.ts.map +1 -0
  73. package/dist/workspace-loader.js +175 -0
  74. package/dist/workspace-loader.js.map +1 -0
  75. package/package.json +52 -0
  76. package/workspaces/api_dev/CONFIG.md +31 -0
  77. package/workspaces/api_dev/RULES.md +8 -0
  78. package/workspaces/api_dev/SKILL.md +69 -0
  79. package/workspaces/api_dev/SOUL.md +19 -0
  80. package/workspaces/architect/CONFIG.md +32 -0
  81. package/workspaces/architect/RULES.md +8 -0
  82. package/workspaces/architect/SKILL.md +63 -0
  83. package/workspaces/architect/SOUL.md +19 -0
  84. package/workspaces/backend_dev/CONFIG.md +42 -0
  85. package/workspaces/backend_dev/RULES.md +8 -0
  86. package/workspaces/backend_dev/SKILL.md +41 -0
  87. package/workspaces/backend_dev/SOUL.md +19 -0
  88. package/workspaces/browser/CONFIG.md +42 -0
  89. package/workspaces/browser/RULES.md +8 -0
  90. package/workspaces/browser/SKILL.md +57 -0
  91. package/workspaces/browser/SOUL.md +19 -0
  92. package/workspaces/builder/CONFIG.md +31 -0
  93. package/workspaces/builder/RULES.md +8 -0
  94. package/workspaces/builder/SKILL.md +39 -0
  95. package/workspaces/builder/SOUL.md +18 -0
  96. package/workspaces/coder/CONFIG.md +44 -0
  97. package/workspaces/coder/RULES.md +9 -0
  98. package/workspaces/coder/SKILL.md +53 -0
  99. package/workspaces/coder/SOUL.md +19 -0
  100. package/workspaces/coordinator/CONFIG.md +62 -0
  101. package/workspaces/coordinator/RULES.md +8 -0
  102. package/workspaces/coordinator/SKILL.md +77 -0
  103. package/workspaces/coordinator/SOUL.md +20 -0
  104. package/workspaces/data_engineer/CONFIG.md +32 -0
  105. package/workspaces/data_engineer/RULES.md +8 -0
  106. package/workspaces/data_engineer/SKILL.md +44 -0
  107. package/workspaces/data_engineer/SOUL.md +19 -0
  108. package/workspaces/deployer/CONFIG.md +45 -0
  109. package/workspaces/deployer/RULES.md +8 -0
  110. package/workspaces/deployer/SKILL.md +74 -0
  111. package/workspaces/deployer/SOUL.md +19 -0
  112. package/workspaces/dreamer/CONFIG.md +34 -0
  113. package/workspaces/dreamer/RULES.md +8 -0
  114. package/workspaces/dreamer/SKILL.md +48 -0
  115. package/workspaces/dreamer/SOUL.md +19 -0
  116. package/workspaces/evaluator/CONFIG.md +40 -0
  117. package/workspaces/evaluator/RULES.md +21 -0
  118. package/workspaces/evaluator/SKILL.md +65 -0
  119. package/workspaces/evaluator/SOUL.md +20 -0
  120. package/workspaces/front_director/CONFIG.md +54 -0
  121. package/workspaces/front_director/RULES.md +8 -0
  122. package/workspaces/front_director/SKILL.md +52 -0
  123. package/workspaces/front_director/SOUL.md +20 -0
  124. package/workspaces/frontend_dev/CONFIG.md +33 -0
  125. package/workspaces/frontend_dev/RULES.md +8 -0
  126. package/workspaces/frontend_dev/SKILL.md +42 -0
  127. package/workspaces/frontend_dev/SOUL.md +19 -0
  128. package/workspaces/observer/CONFIG.md +60 -0
  129. package/workspaces/observer/RULES.md +7 -0
  130. package/workspaces/observer/SKILL.md +77 -0
  131. package/workspaces/observer/SOUL.md +19 -0
  132. package/workspaces/planner/CONFIG.md +43 -0
  133. package/workspaces/planner/RULES.md +8 -0
  134. package/workspaces/planner/SKILL.md +65 -0
  135. package/workspaces/planner/SOUL.md +20 -0
  136. package/workspaces/qa/CONFIG.md +32 -0
  137. package/workspaces/qa/RULES.md +8 -0
  138. package/workspaces/qa/SKILL.md +52 -0
  139. package/workspaces/qa/SOUL.md +19 -0
  140. package/workspaces/researcher/CONFIG.md +32 -0
  141. package/workspaces/researcher/RULES.md +8 -0
  142. package/workspaces/researcher/SKILL.md +58 -0
  143. package/workspaces/researcher/SOUL.md +20 -0
  144. package/workspaces/reviewer/CONFIG.md +41 -0
  145. package/workspaces/reviewer/RULES.md +17 -0
  146. package/workspaces/reviewer/SKILL.md +56 -0
  147. package/workspaces/reviewer/SOUL.md +20 -0
  148. package/workspaces/router/CONFIG.md +58 -0
  149. package/workspaces/router/RULES.md +8 -0
  150. package/workspaces/router/SKILL.md +97 -0
  151. package/workspaces/router/SOUL.md +20 -0
  152. package/workspaces/scraper/CONFIG.md +41 -0
  153. package/workspaces/scraper/RULES.md +8 -0
  154. package/workspaces/scraper/SKILL.md +51 -0
  155. package/workspaces/scraper/SOUL.md +19 -0
  156. package/workspaces/scripter/CONFIG.md +42 -0
  157. package/workspaces/scripter/RULES.md +8 -0
  158. package/workspaces/scripter/SKILL.md +54 -0
  159. package/workspaces/scripter/SOUL.md +18 -0
  160. package/workspaces/security/CONFIG.md +47 -0
  161. package/workspaces/security/RULES.md +8 -0
  162. package/workspaces/security/SKILL.md +50 -0
  163. package/workspaces/security/SOUL.md +19 -0
  164. package/workspaces/translator/CONFIG.md +43 -0
  165. package/workspaces/translator/RULES.md +8 -0
  166. package/workspaces/translator/SKILL.md +35 -0
  167. package/workspaces/translator/SOUL.md +19 -0
  168. package/workspaces/writer/CONFIG.md +31 -0
  169. package/workspaces/writer/RULES.md +8 -0
  170. package/workspaces/writer/SKILL.md +40 -0
  171. package/workspaces/writer/SOUL.md +19 -0
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Intent Analyzer 模块
3
+ * 版本: v4.0.0
4
+ *
5
+ * LLM 意图分析,自动路由到对应 Agent。
6
+ * 核心模块:取消 @xxx/!! 标识,实现自然语言自动路由。
7
+ */
8
+ import type { WorkspaceLoader } from "./workspace-loader.js";
9
+ export declare enum TaskType {
10
+ CODE = "code",
11
+ CODE_REVIEW = "code_review",
12
+ CODE_DEBUG = "code_debug",
13
+ ARCHITECTURE = "architecture",
14
+ DESIGN = "design",
15
+ DATA_ANALYSIS = "data_analysis",
16
+ RESEARCH = "research",
17
+ WRITING = "writing",
18
+ TRANSLATION = "translation",
19
+ DEPLOYMENT = "deployment",
20
+ SECURITY = "security",
21
+ QA_TESTING = "qa_testing",
22
+ COORDINATION = "coordination",
23
+ PLANNING = "planning",
24
+ QUERY = "query",
25
+ UNKNOWN = "unknown"
26
+ }
27
+ export declare enum Complexity {
28
+ SIMPLE = "simple",
29
+ MEDIUM = "medium",
30
+ COMPLEX = "complex"
31
+ }
32
+ export declare enum Workflow {
33
+ EXECUTE = "EXECUTE",
34
+ PIPELINE = "PIPELINE",
35
+ PARALLEL = "PARALLEL",
36
+ ITERATE = "ITERATE",
37
+ COLLABORATE = "COLLABORATE"
38
+ }
39
+ export interface Intent {
40
+ task_type: TaskType;
41
+ task_desc: string;
42
+ complexity: Complexity;
43
+ required_agents: string[];
44
+ workflow: Workflow;
45
+ acceptance_criteria: string;
46
+ confidence: number;
47
+ reasoning: string;
48
+ suggested_steps: Array<{
49
+ step: number;
50
+ agent: string;
51
+ action: string;
52
+ task_desc: string;
53
+ }>;
54
+ }
55
+ export interface ExecutionPlan {
56
+ intent: Intent;
57
+ steps: Array<{
58
+ step: number;
59
+ agent: string;
60
+ action: string;
61
+ task_desc: string;
62
+ }>;
63
+ topic_id: string;
64
+ estimated_steps: number;
65
+ }
66
+ type LLMClient = (prompt: string, context?: Record<string, unknown>) => Promise<Record<string, unknown> | string>;
67
+ export declare class IntentAnalyzer {
68
+ private llmClient;
69
+ private workspaceLoader;
70
+ private useSemanticMatch;
71
+ private agentCache;
72
+ private agentCacheExpiry;
73
+ private static readonly AGENT_CACHE_TTL_MS;
74
+ setLLMClient(client: LLMClient): void;
75
+ setWorkspaceLoader(loader: WorkspaceLoader): void;
76
+ /**
77
+ * 使用同义词扩展关键词
78
+ */
79
+ private expandWithSynonyms;
80
+ /**
81
+ * 关键词匹配 - 使用简单计数(避免 TF-IDF 在关键词数量不均衡时产生偏差)
82
+ * 支持同义词扩展
83
+ */
84
+ private keywordMatch;
85
+ /**
86
+ * 转义正则特殊字符
87
+ */
88
+ private escapeRegex;
89
+ /**
90
+ * 简单的语义相似度计算(使用字符 n-gram Jaccard 相似度)
91
+ * 用于处理表达方式不同但语义相似的情况
92
+ */
93
+ private simpleSemanticSimilarity;
94
+ private detectTaskType;
95
+ /**
96
+ * 使用语义相似度找到最佳匹配的任务类型
97
+ */
98
+ private findBestSemanticMatch;
99
+ private detectComplexity;
100
+ private inferAgents;
101
+ private inferAgentsFromWorkspace;
102
+ /**
103
+ * 获取任务类型对应的 capabilities
104
+ * 可被子类覆盖或从配置读取
105
+ */
106
+ private getCapabilitiesForTaskType;
107
+ private inferWorkflow;
108
+ /**
109
+ * 同步分析(无 LLM 调用,仅规则匹配)
110
+ * 用于测试和快速判断
111
+ */
112
+ analyzeSync(userMessage: string): Intent;
113
+ private inferAgentsSync;
114
+ analyze(userMessage: string, context?: Record<string, unknown>): Promise<Intent>;
115
+ private buildAnalysisPrompt;
116
+ generateExecutionPlan(intent: Intent, topicId: string): ExecutionPlan;
117
+ }
118
+ export {};
119
+ //# source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MAAM,EACnB,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACpC,OAAO,CAAC,MAAM,CAAC;IAgElB,OAAO,CAAC,mBAAmB;IAmC3B,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,aAAa;CAiEtE"}
@@ -0,0 +1,563 @@
1
+ /**
2
+ * Intent Analyzer 模块
3
+ * 版本: v4.0.0
4
+ *
5
+ * LLM 意图分析,自动路由到对应 Agent。
6
+ * 核心模块:取消 @xxx/!! 标识,实现自然语言自动路由。
7
+ */
8
+ export var TaskType;
9
+ (function (TaskType) {
10
+ TaskType["CODE"] = "code";
11
+ TaskType["CODE_REVIEW"] = "code_review";
12
+ TaskType["CODE_DEBUG"] = "code_debug";
13
+ TaskType["ARCHITECTURE"] = "architecture";
14
+ TaskType["DESIGN"] = "design";
15
+ TaskType["DATA_ANALYSIS"] = "data_analysis";
16
+ TaskType["RESEARCH"] = "research";
17
+ TaskType["WRITING"] = "writing";
18
+ TaskType["TRANSLATION"] = "translation";
19
+ TaskType["DEPLOYMENT"] = "deployment";
20
+ TaskType["SECURITY"] = "security";
21
+ TaskType["QA_TESTING"] = "qa_testing";
22
+ TaskType["COORDINATION"] = "coordination";
23
+ TaskType["PLANNING"] = "planning";
24
+ TaskType["QUERY"] = "query";
25
+ TaskType["UNKNOWN"] = "unknown";
26
+ })(TaskType || (TaskType = {}));
27
+ export var Complexity;
28
+ (function (Complexity) {
29
+ Complexity["SIMPLE"] = "simple";
30
+ Complexity["MEDIUM"] = "medium";
31
+ Complexity["COMPLEX"] = "complex";
32
+ })(Complexity || (Complexity = {}));
33
+ export var Workflow;
34
+ (function (Workflow) {
35
+ Workflow["EXECUTE"] = "EXECUTE";
36
+ Workflow["PIPELINE"] = "PIPELINE";
37
+ Workflow["PARALLEL"] = "PARALLEL";
38
+ Workflow["ITERATE"] = "ITERATE";
39
+ Workflow["COLLABORATE"] = "COLLABORATE";
40
+ })(Workflow || (Workflow = {}));
41
+ // Prompt 版本控制
42
+ const PROMPT_VERSION = "1.0";
43
+ const TASK_TYPE_KEYWORDS = {
44
+ [TaskType.CODE]: [
45
+ "写代码", "编写", "实现", "开发", "coding", "code",
46
+ "写个", "做个", "创建", "添加", "修改", "更新", "function", "class",
47
+ "帮我写", "写一个函数", "实现功能", "程序", "script",
48
+ "写个脚本", "开发个", "做个功能", "新建", "增加功能",
49
+ ],
50
+ [TaskType.CODE_REVIEW]: [
51
+ "审查", "review", "检视", "看看代码", "检查代码",
52
+ "代码审查", "review code", "review", "审核", "评审",
53
+ "看看有没有问题", "检查下代码", "review一下",
54
+ ],
55
+ [TaskType.CODE_DEBUG]: [
56
+ "debug", "调试", "修复bug", "报错", "出问题",
57
+ "错误", "异常", "fix bug", "fix", "bug", "崩溃", "卡死",
58
+ "报错了", "有bug", "修一下", "哪里错了", "解决",
59
+ ],
60
+ [TaskType.ARCHITECTURE]: [
61
+ "架构", "architecture", "设计方案", "技术选型",
62
+ "怎么设计", "如何架构", "架构设计", "系统架构", "微服务",
63
+ "技术架构", "整体设计", "架构方案",
64
+ ],
65
+ [TaskType.DESIGN]: [
66
+ "设计", "UI", "界面", "design", "页面", "组件", "design",
67
+ "前端", "界面设计", "做界面", "画页面", "写页面",
68
+ "样式", "布局", "组件设计",
69
+ ],
70
+ [TaskType.DATA_ANALYSIS]: [
71
+ "分析", "analytics", "数据", "统计", "报表",
72
+ "可视化", "dashboard", "分析", "数据分析", "统计",
73
+ "数据处理", "数据分析", "报表", "图表",
74
+ ],
75
+ [TaskType.RESEARCH]: [
76
+ "研究", "调研", "research", "调查", "搜索",
77
+ "查找", "了解一下", "research", "了解一下",
78
+ "查一下", "搜索", "调研", "研究一下",
79
+ ],
80
+ [TaskType.WRITING]: [
81
+ "写", "写作", "文档", "报告", "撰写",
82
+ "writing", "文章", "写一个", "写篇", "写个文档",
83
+ "写文档", "写报告", "写文章", "撰写",
84
+ ],
85
+ [TaskType.TRANSLATION]: [
86
+ "翻译", "translate", "中译英", "英译中", "translate",
87
+ "翻译成", "英汉", "汉英", "语言转换",
88
+ ],
89
+ [TaskType.DEPLOYMENT]: [
90
+ "部署", "deploy", "发布", "上线", "安装", "配置", "deploy",
91
+ "发布", "上线", "部署到", "安装", "搭建环境",
92
+ "跑起来", "启动", "发布应用",
93
+ ],
94
+ [TaskType.SECURITY]: [
95
+ "安全", "security", "漏洞", "渗透", "审计", "security",
96
+ "安全检查", "漏洞扫描", "渗透测试", "安全审计",
97
+ ],
98
+ [TaskType.QA_TESTING]: [
99
+ "测试", "test", "测试用例", "验证", "QA", "testing",
100
+ "单元测试", "功能测试", "测试一下", "跑测试",
101
+ "自动化测试", "测试用例", "验证",
102
+ ],
103
+ [TaskType.COORDINATION]: [
104
+ "协调", "协作", "coordination", "合作", "collaborate",
105
+ "协作", "配合", "沟通", "协调",
106
+ ],
107
+ [TaskType.PLANNING]: [
108
+ "计划", "规划", "planning", "安排", "排期", "planning",
109
+ "规划", "计划", "安排", "排期", " Roadmap",
110
+ ],
111
+ [TaskType.QUERY]: [
112
+ "查询", "问问", "想知道", "什么是", "怎么", "如何",
113
+ "问下", "请问", "问一下", "帮我查", "查一下",
114
+ "告诉我", "谁能", "哪里", "哪个",
115
+ ],
116
+ [TaskType.UNKNOWN]: [],
117
+ };
118
+ /**
119
+ * 同义词映射表 - 扩展关键词匹配
120
+ */
121
+ const SYNONYMS = {
122
+ "写": ["创建", "新建", "添加", "实现"],
123
+ "代码": ["程序", "脚本", "函数", "逻辑"],
124
+ "查": ["搜索", "查找", "查询", "检索"],
125
+ "分析": ["分析", "处理", "统计", "计算"],
126
+ "测试": ["验证", "检验", "检查", "测"],
127
+ "部署": ["发布", "上线", "安装", "配置"],
128
+ "设计": ["规划", "方案", "蓝图"],
129
+ "错误": ["bug", "异常", "问题", "失败"],
130
+ "修复": ["解决", "改正", "修", "fix"],
131
+ };
132
+ const COMPLEXITY_KEYWORDS = {
133
+ [Complexity.SIMPLE]: ["简单", "一个", "单个", "只是", "quick", "easy"],
134
+ [Complexity.MEDIUM]: ["中等", "几个", "一些", "稍微复杂", "medium"],
135
+ [Complexity.COMPLEX]: ["复杂", "多个", "全面", "整个", "完整", "系统", "complex"],
136
+ };
137
+ // Agent 能力映射
138
+ const AGENT_CAPABILITIES = {
139
+ coder: [TaskType.CODE, TaskType.CODE_DEBUG],
140
+ frontend_dev: [TaskType.CODE, TaskType.DESIGN],
141
+ backend_dev: [TaskType.CODE, TaskType.DEPLOYMENT],
142
+ api_dev: [TaskType.CODE],
143
+ data_engineer: [TaskType.DATA_ANALYSIS],
144
+ scripter: [TaskType.CODE],
145
+ researcher: [TaskType.RESEARCH],
146
+ writer: [TaskType.WRITING],
147
+ translator: [TaskType.TRANSLATION],
148
+ reviewer: [TaskType.CODE_REVIEW, TaskType.CODE_DEBUG],
149
+ qa: [TaskType.QA_TESTING],
150
+ security: [TaskType.SECURITY],
151
+ evaluator: [TaskType.QA_TESTING],
152
+ architect: [TaskType.ARCHITECTURE, TaskType.DESIGN],
153
+ deployer: [TaskType.DEPLOYMENT],
154
+ coordinator: [TaskType.COORDINATION, TaskType.PLANNING],
155
+ planner: [TaskType.PLANNING],
156
+ };
157
+ export class IntentAnalyzer {
158
+ llmClient = null;
159
+ workspaceLoader = null;
160
+ useSemanticMatch = true;
161
+ // Workspace 缓存(减少重复加载)
162
+ agentCache = null;
163
+ agentCacheExpiry = 0;
164
+ static AGENT_CACHE_TTL_MS = 60000; // 1分钟缓存
165
+ setLLMClient(client) {
166
+ this.llmClient = client;
167
+ }
168
+ setWorkspaceLoader(loader) {
169
+ this.workspaceLoader = loader;
170
+ }
171
+ /**
172
+ * 使用同义词扩展关键词
173
+ */
174
+ expandWithSynonyms(keywords) {
175
+ const expanded = new Set(keywords);
176
+ for (const kw of keywords) {
177
+ if (SYNONYMS[kw]) {
178
+ for (const syn of SYNONYMS[kw]) {
179
+ expanded.add(syn);
180
+ }
181
+ }
182
+ }
183
+ return Array.from(expanded);
184
+ }
185
+ /**
186
+ * 关键词匹配 - 使用简单计数(避免 TF-IDF 在关键词数量不均衡时产生偏差)
187
+ * 支持同义词扩展
188
+ */
189
+ keywordMatch(text, keywords) {
190
+ const textLower = text.toLowerCase();
191
+ const expandedKeywords = this.expandWithSynonyms(keywords);
192
+ let score = 0;
193
+ let matchedCount = 0;
194
+ for (const kw of expandedKeywords) {
195
+ const regex = new RegExp(this.escapeRegex(kw.toLowerCase()), "gi");
196
+ const matches = textLower.match(regex);
197
+ if (matches) {
198
+ // 简单计数:匹配次数越多分数越高
199
+ score += matches.length;
200
+ matchedCount++;
201
+ }
202
+ }
203
+ // 返回原始分数(不再用 TF-IDF 归一化,因为不同类型的关键词数量差异很大)
204
+ return score;
205
+ }
206
+ /**
207
+ * 转义正则特殊字符
208
+ */
209
+ escapeRegex(str) {
210
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
211
+ }
212
+ /**
213
+ * 简单的语义相似度计算(使用字符 n-gram Jaccard 相似度)
214
+ * 用于处理表达方式不同但语义相似的情况
215
+ */
216
+ simpleSemanticSimilarity(text1, text2) {
217
+ const normalize = (s) => s.toLowerCase().replace(/[^\w\u4e00-\u9fff]/g, "").split("").sort().join("");
218
+ const getNgrams = (s, n) => {
219
+ const normalized = normalize(s);
220
+ const grams = new Set();
221
+ for (let i = 0; i <= normalized.length - n; i++) {
222
+ grams.add(normalized.slice(i, i + n));
223
+ }
224
+ return grams;
225
+ };
226
+ const bigrams1 = getNgrams(text1, 2);
227
+ const bigrams2 = getNgrams(text2, 2);
228
+ // Jaccard 相似度
229
+ let intersection = 0;
230
+ for (const gram of bigrams1) {
231
+ if (bigrams2.has(gram))
232
+ intersection++;
233
+ }
234
+ const union = bigrams1.size + bigrams2.size - intersection;
235
+ return union > 0 ? intersection / union : 0;
236
+ }
237
+ detectTaskType(message) {
238
+ const scores = {};
239
+ for (const [taskType, keywords] of Object.entries(TASK_TYPE_KEYWORDS)) {
240
+ const score = this.keywordMatch(message, keywords);
241
+ if (score > 0) {
242
+ scores[taskType] = score;
243
+ }
244
+ }
245
+ // 如果没有匹配到,使用语义相似度作为后备
246
+ if (Object.keys(scores).length === 0 || Object.values(scores).every(s => s === 0)) {
247
+ const bestMatch = this.findBestSemanticMatch(message);
248
+ if (bestMatch && bestMatch.similarity > 0.3) {
249
+ return bestMatch.taskType;
250
+ }
251
+ return TaskType.UNKNOWN;
252
+ }
253
+ return Object.entries(scores).sort((a, b) => b[1] - a[1])[0][0];
254
+ }
255
+ /**
256
+ * 使用语义相似度找到最佳匹配的任务类型
257
+ */
258
+ findBestSemanticMatch(message) {
259
+ let best = null;
260
+ for (const [taskType, keywords] of Object.entries(TASK_TYPE_KEYWORDS)) {
261
+ for (const kw of keywords) {
262
+ const similarity = this.simpleSemanticSimilarity(message, kw);
263
+ if (!best || similarity > best.similarity) {
264
+ best = { taskType: taskType, similarity };
265
+ }
266
+ }
267
+ }
268
+ return best;
269
+ }
270
+ detectComplexity(message) {
271
+ for (const [complexity, keywords] of Object.entries(COMPLEXITY_KEYWORDS)) {
272
+ if (this.keywordMatch(message, keywords) > 0) {
273
+ return complexity;
274
+ }
275
+ }
276
+ return Complexity.MEDIUM;
277
+ }
278
+ async inferAgents(taskType) {
279
+ // 如果有 workspace loader,使用它的配置
280
+ if (this.workspaceLoader) {
281
+ return this.inferAgentsFromWorkspace(taskType);
282
+ }
283
+ // 回退到硬编码映射
284
+ const agents = [];
285
+ for (const [agent, capabilities] of Object.entries(AGENT_CAPABILITIES)) {
286
+ if (capabilities.includes(taskType)) {
287
+ agents.push(agent);
288
+ }
289
+ }
290
+ return agents.length > 0 ? agents : ["coder"];
291
+ }
292
+ async inferAgentsFromWorkspace(taskType) {
293
+ if (!this.workspaceLoader)
294
+ return ["coder"];
295
+ const now = Date.now();
296
+ // 检查缓存
297
+ if (this.agentCache && now < this.agentCacheExpiry) {
298
+ const cached = this.agentCache.get(taskType);
299
+ if (cached)
300
+ return cached;
301
+ }
302
+ try {
303
+ const allWorkspaces = await this.workspaceLoader.loadAll();
304
+ const matchingAgents = [];
305
+ // 从所有 workspace 构建 capability → agents 映射
306
+ const capabilityToAgents = new Map();
307
+ for (const [agentId, workspace] of allWorkspaces) {
308
+ const capabilities = workspace.config.capabilities || [];
309
+ for (const cap of capabilities) {
310
+ if (!capabilityToAgents.has(cap)) {
311
+ capabilityToAgents.set(cap, []);
312
+ }
313
+ capabilityToAgents.get(cap).push(agentId);
314
+ }
315
+ }
316
+ // 获取当前 taskType 对应的 capabilities
317
+ const taskTypeCapabilities = this.getCapabilitiesForTaskType(taskType);
318
+ // 查找匹配 agents
319
+ const agentScores = new Map();
320
+ for (const [agentId, workspace] of allWorkspaces) {
321
+ const capabilities = workspace.config.capabilities || [];
322
+ const score = capabilities.filter((c) => taskTypeCapabilities.some((tc) => c.toLowerCase().includes(tc.toLowerCase()))).length;
323
+ if (score > 0) {
324
+ agentScores.set(agentId, score);
325
+ }
326
+ }
327
+ // 按分数排序
328
+ const sorted = Array.from(agentScores.entries())
329
+ .sort((a, b) => b[1] - a[1])
330
+ .map(([agent]) => agent);
331
+ if (sorted.length > 0) {
332
+ // 缓存结果
333
+ if (!this.agentCache)
334
+ this.agentCache = new Map();
335
+ this.agentCache.set(taskType, sorted);
336
+ this.agentCacheExpiry = now + IntentAnalyzer.AGENT_CACHE_TTL_MS;
337
+ return sorted;
338
+ }
339
+ }
340
+ catch (error) {
341
+ console.warn("[IntentAnalyzer] Workspace inference failed:", error);
342
+ }
343
+ return ["coder"];
344
+ }
345
+ /**
346
+ * 获取任务类型对应的 capabilities
347
+ * 可被子类覆盖或从配置读取
348
+ */
349
+ getCapabilitiesForTaskType(taskType) {
350
+ // 默认映射,可被配置覆盖
351
+ const DEFAULT_CAPABILITY_MAP = {
352
+ "code": ["code", "coding", "implementation"],
353
+ "code_review": ["review", "code_review", "inspection"],
354
+ "code_debug": ["debug", "fix", "troubleshooting"],
355
+ "architecture": ["architecture", "design", "system_design"],
356
+ "design": ["design", "ui", "ux", "interface"],
357
+ "data_analysis": ["analytics", "data", "analysis", "statistics"],
358
+ "research": ["research", "investigation", "study"],
359
+ "writing": ["writing", "documentation", "documentation"],
360
+ "translation": ["translation", "localization"],
361
+ "deployment": ["deploy", "deployment", "devops", "release"],
362
+ "security": ["security", "audit", "vulnerability"],
363
+ "qa_testing": ["testing", "qa", "quality_assurance", "test"],
364
+ "coordination": ["coordination", "collaboration", "management"],
365
+ "planning": ["planning", "strategy", "roadmap"],
366
+ "query": ["query", "search", "information"],
367
+ };
368
+ return DEFAULT_CAPABILITY_MAP[taskType] || [taskType];
369
+ }
370
+ inferWorkflow(taskType, complexity, agentCount) {
371
+ if (complexity === Complexity.SIMPLE || agentCount <= 1) {
372
+ return Workflow.EXECUTE;
373
+ }
374
+ if (taskType === TaskType.CODE_REVIEW) {
375
+ return Workflow.PIPELINE;
376
+ }
377
+ if (complexity === Complexity.COMPLEX && agentCount > 1) {
378
+ return Workflow.COLLABORATE;
379
+ }
380
+ return Workflow.EXECUTE;
381
+ }
382
+ /**
383
+ * 同步分析(无 LLM 调用,仅规则匹配)
384
+ * 用于测试和快速判断
385
+ */
386
+ analyzeSync(userMessage) {
387
+ const taskType = this.detectTaskType(userMessage);
388
+ const complexity = this.detectComplexity(userMessage);
389
+ const requiredAgents = this.inferAgentsSync(taskType);
390
+ const workflow = this.inferWorkflow(taskType, complexity, requiredAgents.length);
391
+ return {
392
+ task_type: taskType,
393
+ task_desc: userMessage,
394
+ complexity,
395
+ required_agents: requiredAgents,
396
+ workflow,
397
+ acceptance_criteria: "完成基本功能",
398
+ confidence: 0.7,
399
+ reasoning: "Rule-based analysis (sync)",
400
+ suggested_steps: [],
401
+ };
402
+ }
403
+ inferAgentsSync(taskType) {
404
+ // 回退到硬编码映射(同步版本)
405
+ const agents = [];
406
+ for (const [agent, capabilities] of Object.entries(AGENT_CAPABILITIES)) {
407
+ if (capabilities.includes(taskType)) {
408
+ agents.push(agent);
409
+ }
410
+ }
411
+ return agents.length > 0 ? agents : ["coder"];
412
+ }
413
+ async analyze(userMessage, context = {}) {
414
+ const quickTaskType = this.detectTaskType(userMessage);
415
+ let reasoning = "";
416
+ let llmIntent = null;
417
+ if (this.llmClient) {
418
+ try {
419
+ const rawResult = await this.llmClient(this.buildAnalysisPrompt(userMessage, context), context);
420
+ if (typeof rawResult === "string") {
421
+ try {
422
+ llmIntent = JSON.parse(rawResult);
423
+ }
424
+ catch {
425
+ llmIntent = null;
426
+ }
427
+ }
428
+ else {
429
+ llmIntent = rawResult;
430
+ }
431
+ reasoning = llmIntent?.reasoning || "LLM analysis completed";
432
+ }
433
+ catch (error) {
434
+ reasoning = `LLM analysis failed, using rule-based fallback: ${error}`;
435
+ }
436
+ }
437
+ else {
438
+ reasoning = "Rule-based analysis (no LLM client configured)";
439
+ }
440
+ // 合并结果
441
+ const taskTypeStr = llmIntent?.task_type || quickTaskType;
442
+ const taskType = Object.values(TaskType).includes(taskTypeStr)
443
+ ? taskTypeStr
444
+ : quickTaskType;
445
+ const complexityStr = llmIntent?.complexity || this.detectComplexity(userMessage);
446
+ const complexity = Object.values(Complexity).includes(complexityStr)
447
+ ? complexityStr
448
+ : this.detectComplexity(userMessage);
449
+ const requiredAgents = llmIntent?.required_agents || await this.inferAgents(taskType);
450
+ const workflowStr = llmIntent?.workflow || this.inferWorkflow(taskType, complexity, requiredAgents.length);
451
+ const workflow = Object.values(Workflow).includes(workflowStr)
452
+ ? workflowStr
453
+ : this.inferWorkflow(taskType, complexity, requiredAgents.length);
454
+ const acceptanceCriteria = llmIntent?.acceptance_criteria || "完成基本功能";
455
+ const confidence = llmIntent?.confidence || 0.7;
456
+ const suggestedSteps = llmIntent?.suggested_steps || [];
457
+ return {
458
+ task_type: taskType,
459
+ task_desc: userMessage,
460
+ complexity,
461
+ required_agents: requiredAgents,
462
+ workflow,
463
+ acceptance_criteria: acceptanceCriteria,
464
+ confidence,
465
+ reasoning,
466
+ suggested_steps: suggestedSteps,
467
+ };
468
+ }
469
+ buildAnalysisPrompt(userMessage, context) {
470
+ return `[PROMPT_VERSION=${PROMPT_VERSION}] 分析用户消息,提取以下信息(JSON 格式):
471
+
472
+ 1. task_type: 任务类型
473
+ 可选值: code, code_review, code_debug, architecture, design, data_analysis,
474
+ research, writing, translation, deployment, security, qa_testing,
475
+ coordination, planning, query, unknown
476
+
477
+ 2. complexity: 复杂度
478
+ 可选值: simple, medium, complex
479
+
480
+ 3. required_agents: 需要参与的 Agent 列表
481
+ 可选: coder, frontend_dev, backend_dev, api_dev, data_engineer,
482
+ scripter, researcher, writer, translator, reviewer, qa,
483
+ security, evaluator, architect, deployer, coordinator, planner
484
+
485
+ 4. workflow: 工作流类型
486
+ 可选值: EXECUTE (单步执行), PIPELINE (流水线), PARALLEL (并行),
487
+ ITERATE (迭代), COLLABORATE (协作)
488
+
489
+ 5. acceptance_criteria: 验收标准(中文描述)
490
+
491
+ 6. confidence: 置信度 0-1
492
+
493
+ 7. reasoning: 分析过程说明
494
+
495
+ 8. suggested_steps: 建议执行步骤列表,每步包含 step, agent, action, task_desc
496
+
497
+ 用户消息: ${userMessage}
498
+
499
+ 上下文: ${JSON.stringify(context, null, 2)}
500
+
501
+ 请只返回 JSON,不要有其他文字。`;
502
+ }
503
+ generateExecutionPlan(intent, topicId) {
504
+ const steps = [];
505
+ switch (intent.workflow) {
506
+ case Workflow.EXECUTE:
507
+ steps.push({
508
+ step: 1,
509
+ agent: intent.required_agents[0] || "coder",
510
+ action: "execute",
511
+ task_desc: intent.task_desc,
512
+ });
513
+ break;
514
+ case Workflow.PIPELINE:
515
+ for (let i = 0; i < intent.required_agents.length; i++) {
516
+ steps.push({
517
+ step: i + 1,
518
+ agent: intent.required_agents[i],
519
+ action: "execute",
520
+ task_desc: i === 0 ? intent.task_desc : "处理来自上一步的结果",
521
+ });
522
+ }
523
+ break;
524
+ case Workflow.PARALLEL:
525
+ for (let i = 0; i < intent.required_agents.length; i++) {
526
+ steps.push({
527
+ step: i + 1,
528
+ agent: intent.required_agents[i],
529
+ action: "parallel_execute",
530
+ task_desc: intent.task_desc,
531
+ });
532
+ }
533
+ break;
534
+ case Workflow.ITERATE:
535
+ for (let i = 0; i < Math.min(intent.required_agents.length * 2, 6); i++) {
536
+ steps.push({
537
+ step: i + 1,
538
+ agent: intent.required_agents[i % intent.required_agents.length],
539
+ action: "iterate",
540
+ task_desc: `迭代步骤 ${i + 1}`,
541
+ });
542
+ }
543
+ break;
544
+ case Workflow.COLLABORATE:
545
+ for (let i = 0; i < intent.required_agents.length; i++) {
546
+ steps.push({
547
+ step: i + 1,
548
+ agent: intent.required_agents[i],
549
+ action: "discuss",
550
+ task_desc: `发表意见: ${intent.task_desc.slice(0, 50)}...`,
551
+ });
552
+ }
553
+ break;
554
+ }
555
+ return {
556
+ intent,
557
+ steps,
558
+ topic_id: topicId,
559
+ estimated_steps: steps.length,
560
+ };
561
+ }
562
+ }
563
+ //# sourceMappingURL=intent-analyzer.js.map