@bolloon/bolloon-agent 0.1.1 → 0.1.3

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 (200) hide show
  1. package/bin/bolloon-cli.cjs +165 -0
  2. package/bin/bolloon-daemon.sh +207 -0
  3. package/bin/bolloon.cmd +11 -0
  4. package/dist/agents/constraint-layer.js +10 -15
  5. package/dist/agents/pi-sdk.js +433 -106
  6. package/dist/agents/protocol.js +82 -1
  7. package/dist/agents/subagent-manager.js +2 -2
  8. package/dist/agents/workflow-engine.js +15 -20
  9. package/dist/agents/workflow-pivot-loop.js +541 -0
  10. package/dist/bollharness/src/index.js +5 -0
  11. package/dist/bollharness/src/scripts/checks/check_adr_plan_numbering.js +6 -0
  12. package/dist/bollharness/src/scripts/checks/check_api_types.js +45 -0
  13. package/dist/bollharness/src/scripts/checks/check_artifact_link.js +146 -0
  14. package/dist/bollharness/src/scripts/checks/check_bridge_deps.js +6 -0
  15. package/dist/bollharness/src/scripts/checks/check_bugfix_binding.js +6 -0
  16. package/dist/bollharness/src/scripts/checks/check_bugfix_binding_ci.js +6 -0
  17. package/dist/bollharness/src/scripts/checks/check_doc_file_references.js +6 -0
  18. package/dist/bollharness/src/scripts/checks/check_doc_freshness.js +135 -0
  19. package/dist/bollharness/src/scripts/checks/check_doc_links.js +31 -0
  20. package/dist/bollharness/src/scripts/checks/check_file_existence_claims.js +6 -0
  21. package/dist/bollharness/src/scripts/checks/check_fragment_integrity.js +34 -0
  22. package/dist/bollharness/src/scripts/checks/check_hook_installed.js +63 -0
  23. package/dist/bollharness/src/scripts/checks/check_issue_closure.js +41 -0
  24. package/dist/bollharness/src/scripts/checks/check_mcp_parity.js +6 -0
  25. package/dist/bollharness/src/scripts/checks/check_security.js +48 -0
  26. package/dist/bollharness/src/scripts/checks/check_skill_parity.js +6 -0
  27. package/dist/bollharness/src/scripts/checks/check_versions.js +6 -0
  28. package/dist/bollharness/src/scripts/checks/finding.js +13 -0
  29. package/dist/bollharness/src/scripts/checks/next_decision_number.js +20 -0
  30. package/dist/bollharness/src/scripts/checks/regenerate_magic_docs.js +6 -0
  31. package/dist/bollharness/src/scripts/ci/detect_rebaseline_triggers.js +8 -0
  32. package/dist/bollharness/src/scripts/ci/scan_subprocess_cfg.js +8 -0
  33. package/dist/bollharness/src/scripts/ci/scan_verify_artifacts.js +8 -0
  34. package/dist/bollharness/src/scripts/ci/scan_yaml_schema.js +8 -0
  35. package/dist/bollharness/src/scripts/context_router.js +67 -0
  36. package/dist/bollharness/src/scripts/deploy-guard.js +157 -0
  37. package/dist/bollharness/src/scripts/guard-feedback.js +192 -0
  38. package/dist/bollharness/src/scripts/guard_router.js +158 -0
  39. package/dist/bollharness/src/scripts/hooks/_hook_output.js +6 -0
  40. package/dist/bollharness/src/scripts/hooks/auto-python3.js +6 -0
  41. package/dist/bollharness/src/scripts/hooks/deploy-progress-on-session-end.js +6 -0
  42. package/dist/bollharness/src/scripts/hooks/failure-analyzer.js +6 -0
  43. package/dist/bollharness/src/scripts/hooks/gate-judgment-inject.js +92 -0
  44. package/dist/bollharness/src/scripts/hooks/gate-transition-judgment.js +63 -0
  45. package/dist/bollharness/src/scripts/hooks/inbox-ack.js +6 -0
  46. package/dist/bollharness/src/scripts/hooks/inbox-inject-on-start.js +6 -0
  47. package/dist/bollharness/src/scripts/hooks/inbox-validate.js +6 -0
  48. package/dist/bollharness/src/scripts/hooks/inbox-write-ledger.js +6 -0
  49. package/dist/bollharness/src/scripts/hooks/initializer-agent.js +6 -0
  50. package/dist/bollharness/src/scripts/hooks/loop-detection.js +73 -0
  51. package/dist/bollharness/src/scripts/hooks/owner-guard.js +6 -0
  52. package/dist/bollharness/src/scripts/hooks/precompact.js +6 -0
  53. package/dist/bollharness/src/scripts/hooks/review-agent-gatekeeper.js +6 -0
  54. package/dist/bollharness/src/scripts/hooks/risk-tracker.js +108 -0
  55. package/dist/bollharness/src/scripts/hooks/sanitize-on-read.js +6 -0
  56. package/dist/bollharness/src/scripts/hooks/session-reflection.js +7 -0
  57. package/dist/bollharness/src/scripts/hooks/session-start-magic-docs.js +7 -0
  58. package/dist/bollharness/src/scripts/hooks/session-start-reset-risk.js +7 -0
  59. package/dist/bollharness/src/scripts/hooks/session-start-toolkit-reminder.js +7 -0
  60. package/dist/bollharness/src/scripts/hooks/stop-evaluator.js +157 -0
  61. package/dist/bollharness/src/scripts/hooks/tool-call-counter.js +6 -0
  62. package/dist/bollharness/src/scripts/hooks/trace-analyzer.js +10 -0
  63. package/dist/bollharness/src/scripts/install/install-trust-token.js +7 -0
  64. package/dist/bollharness/src/scripts/install/multi_project_registry.js +9 -0
  65. package/dist/bollharness/src/scripts/install/phase2_auto.js +21 -0
  66. package/dist/bollharness/src/scripts/install/pre_commit_installer.js +6 -0
  67. package/dist/bollharness/src/scripts/install/tier_selector.js +7 -0
  68. package/dist/bollharness/src/scripts/install/transcript_miner.js +7 -0
  69. package/dist/bollharness/src/scripts/lib/claim_patterns.js +10 -0
  70. package/dist/bollharness/src/scripts/lib/sanitize_patterns.js +12 -0
  71. package/dist/bollharness/src/scripts/sanitize.js +6 -0
  72. package/dist/bollharness-integration/channel-judgment-engine.js +530 -0
  73. package/dist/bollharness-integration/context-chain-router.js +383 -0
  74. package/dist/bollharness-integration/context-router-judgment.js +13 -21
  75. package/dist/bollharness-integration/context-router.js +22 -64
  76. package/dist/bollharness-integration/gate-state-machine.js +14 -19
  77. package/dist/bollharness-integration/gate-transition-hooks.js +16 -61
  78. package/dist/bollharness-integration/guard-checker.js +21 -68
  79. package/dist/bollharness-integration/index.js +14 -124
  80. package/dist/bollharness-integration/integration.js +13 -20
  81. package/dist/bollharness-integration/llm-judgment-engine.js +569 -0
  82. package/dist/bollharness-integration/skill-adapter.js +18 -64
  83. package/dist/cli-entry.js +261 -0
  84. package/dist/constraint-runtime/src/commands.js +17 -7
  85. package/dist/constraint-runtime/src/constraint/budget.js +1 -6
  86. package/dist/constraint-runtime/src/constraint/permission.js +1 -6
  87. package/dist/constraint-runtime/src/models.js +1 -3
  88. package/dist/constraint-runtime/src/tools.js +17 -7
  89. package/dist/constraints/index.js +1 -7
  90. package/dist/documents/reader.js +8 -49
  91. package/dist/heartbeat/DaemonManager.js +242 -0
  92. package/dist/heartbeat/HealthMonitor.js +285 -0
  93. package/dist/heartbeat/StartupVerifier.js +205 -0
  94. package/dist/heartbeat/Watchdog.js +168 -0
  95. package/dist/heartbeat/index.js +84 -0
  96. package/dist/heartbeat/types.js +5 -0
  97. package/dist/index.js +381 -28
  98. package/dist/llm/config-store.js +31 -57
  99. package/dist/llm/llm-judgment-client.js +389 -0
  100. package/dist/llm/pi-ai.js +9 -52
  101. package/dist/network/agent-network.js +46 -90
  102. package/dist/network/hybrid-messenger.js +125 -0
  103. package/dist/network/iroh-bootstrap.js +38 -0
  104. package/dist/network/iroh-discovery.js +145 -0
  105. package/dist/network/iroh-integration.js +9 -16
  106. package/dist/network/iroh-transport.js +10 -48
  107. package/dist/network/p2p.js +23 -62
  108. package/dist/network/storage/adapters/json-adapter.js +4 -42
  109. package/dist/network/storage/index.js +147 -0
  110. package/dist/network/storage/types.js +14 -0
  111. package/dist/pi-ecosystem/index.js +233 -0
  112. package/dist/pi-ecosystem-colony/index.js +29 -90
  113. package/dist/pi-ecosystem-goals/index.js +20 -74
  114. package/dist/pi-ecosystem-judgment/decision.js +29 -47
  115. package/dist/pi-ecosystem-judgment/distillation.js +16 -29
  116. package/dist/pi-ecosystem-judgment/human-value-store.js +13 -60
  117. package/dist/pi-ecosystem-judgment/index.js +21 -74
  118. package/dist/pi-ecosystem-judgment/value-injection.js +26 -72
  119. package/dist/pi-ecosystem-mcp/index.js +24 -78
  120. package/dist/pi-ecosystem-subagents/index.js +20 -69
  121. package/dist/social/ant-colony/AdaptiveHeartbeat.js +3 -8
  122. package/dist/social/ant-colony/PheromoneEngine.js +11 -49
  123. package/dist/social/ant-colony/index.js +6 -0
  124. package/dist/social/ant-colony/types.js +4 -8
  125. package/dist/social/channels/ChannelManager.js +8 -46
  126. package/dist/social/channels/DiapChannelBridge.js +9 -47
  127. package/dist/social/channels/InterestMatcher.js +2 -7
  128. package/dist/social/channels/channel-agent-session.js +309 -0
  129. package/dist/social/channels/channel-heartbeat-agent.js +494 -0
  130. package/dist/social/channels/diap-doc-parser.js +204 -0
  131. package/dist/social/channels/harness-workflow-integrator.js +446 -0
  132. package/dist/social/channels/index.js +9 -0
  133. package/dist/social/channels/types.js +3 -7
  134. package/dist/social/global-shared-context.js +6 -47
  135. package/dist/social/heartbeat.js +29 -72
  136. package/dist/social/persona/enhanced-persona.js +299 -0
  137. package/dist/web/client.js +302 -136
  138. package/dist/web/components/p2p/index.js +159 -9
  139. package/dist/web/components/p2p/p2p-connection.js +136 -0
  140. package/dist/web/components/p2p/p2p-manager.js +24 -0
  141. package/dist/web/components/p2p/p2p-store-memory.js +1 -1
  142. package/dist/web/components/p2p/types.js +7 -0
  143. package/dist/web/index.html +5 -0
  144. package/dist/web/style.css +118 -0
  145. package/package.json +12 -6
  146. package/scripts/build-cli.js +206 -0
  147. package/scripts/postinstall.js +153 -0
  148. package/src/agents/pi-sdk.ts +347 -28
  149. package/src/agents/protocol.ts +95 -1
  150. package/src/agents/workflow-pivot-loop.ts +674 -0
  151. package/src/bollharness/CLAUDE.md +73 -0
  152. package/src/bollharness/README.md +143 -0
  153. package/src/bollharness/README.zh-CN.md +131 -0
  154. package/src/bollharness/reference/boll-reference/scripts/hooks/stop-evaluator.md +57 -0
  155. package/src/bollharness/scripts/context-fragments/artifact-linkage.md +14 -0
  156. package/src/bollharness/scripts/context-fragments/auth-consumers.md +17 -0
  157. package/src/bollharness/scripts/context-fragments/bridge-constitution.md +13 -0
  158. package/src/bollharness/scripts/context-fragments/catalyst-distributed.md +18 -0
  159. package/src/bollharness/scripts/context-fragments/closure-checklist.md +13 -0
  160. package/src/bollharness/scripts/context-fragments/contract-consumers.md +15 -0
  161. package/src/bollharness/scripts/context-fragments/db-shared-structures.md +15 -0
  162. package/src/bollharness/scripts/context-fragments/fixed-three-layers.md +19 -0
  163. package/src/bollharness/scripts/context-fragments/general-dev-principles.md +11 -0
  164. package/src/bollharness/scripts/context-fragments/issue-first.md +8 -0
  165. package/src/bollharness/scripts/context-fragments/mcp-parity.md +16 -0
  166. package/src/bollharness/scripts/context-fragments/pi-agent-operations.md +108 -0
  167. package/src/bollharness/scripts/context-fragments/protocol-consumers.md +15 -0
  168. package/src/bollharness/scripts/context-fragments/run-events-consumers.md +15 -0
  169. package/src/bollharness/scripts/context-fragments/scene-fidelity.md +13 -0
  170. package/src/bollharness/scripts/context-fragments/truth-source-hierarchy.md +15 -0
  171. package/src/bollharness/scripts/context-fragments/two-language.md +15 -0
  172. package/src/bollharness/scripts/context-fragments/version-sources.md +14 -0
  173. package/src/bollharness/scripts/hooks/stop-evaluator.md +83 -0
  174. package/src/bollharness/templates/scaffold/CLAUDE.md +89 -0
  175. package/src/cli-entry.ts +304 -0
  176. package/src/heartbeat/DaemonManager.ts +283 -0
  177. package/src/heartbeat/HealthMonitor.ts +316 -0
  178. package/src/heartbeat/StartupVerifier.ts +223 -0
  179. package/src/heartbeat/Watchdog.ts +198 -0
  180. package/src/heartbeat/index.ts +108 -0
  181. package/src/heartbeat/types.ts +82 -0
  182. package/src/llm/config-store.ts +23 -5
  183. package/src/network/iroh-transport.ts +3 -3
  184. package/src/web/client.js +302 -136
  185. package/src/web/components/p2p/P2PModal.tsx +91 -3
  186. package/src/web/components/p2p/index.ts +171 -9
  187. package/src/web/components/p2p/p2p-connection.ts +153 -1
  188. package/src/web/components/p2p/p2p-manager.ts +39 -1
  189. package/src/web/components/p2p/p2p-store-memory.ts +1 -1
  190. package/src/web/components/p2p/p2p-tools.ts +315 -0
  191. package/src/web/components/p2p/types.ts +58 -0
  192. package/src/web/design.md +99 -0
  193. package/src/web/index.html +5 -0
  194. package/src/web/server.ts +353 -36
  195. package/src/web/style.css +118 -0
  196. package/tsconfig.cli.json +16 -0
  197. package/tsconfig.electron.json +1 -1
  198. package/tsconfig.json +1 -2
  199. package/dist/web/server.js +0 -1647
  200. package/dist/web/server.js.map +0 -1
@@ -0,0 +1,7 @@
1
+ const REPO_ROOT = process.cwd();
2
+ export function main() {
3
+ process.exit(0);
4
+ }
5
+ if (require.main === module) {
6
+ main();
7
+ }
@@ -0,0 +1,10 @@
1
+ export const CLAIM_PATTERNS = [
2
+ /test[s]?\s+(pass|run|complete[ds]?)/gi,
3
+ /all\s+test[s]?\s+(pass|run)/gi,
4
+ /fix(?:ed|es)?\s+the?\s+(bug|issue|problem)/gi,
5
+ /complete[ds]?/gi,
6
+ /done/gi,
7
+ ];
8
+ export function hasUnverifiedClaim(text) {
9
+ return CLAIM_PATTERNS.some(pattern => pattern.test(text));
10
+ }
@@ -0,0 +1,12 @@
1
+ export const SANITIZE_PATTERNS = [
2
+ { pattern: /api[_-]?key["\s:=]+["'][a-zA-Z0-9]{20,}["']/gi, replacement: "***REDACTED***" },
3
+ { pattern: /password["\s:=]+["'][^"']{8,}["']/gi, replacement: "***REDACTED***" },
4
+ { pattern: /secret["\s:=]+["'][a-zA-Z0-9]{16,}["']/gi, replacement: "***REDACTED***" },
5
+ ];
6
+ export function sanitize(text) {
7
+ let result = text;
8
+ for (const { pattern, replacement } of SANITIZE_PATTERNS) {
9
+ result = result.replace(pattern, replacement);
10
+ }
11
+ return result;
12
+ }
@@ -0,0 +1,6 @@
1
+ export function sanitize(text) {
2
+ return text
3
+ .replace(/api[_-]?key["\s:=]+["'][a-zA-Z0-9]{20,}["']/gi, "***REDACTED***")
4
+ .replace(/password["\s:=]+["'][^"']{8,}["']/gi, "***REDACTED***")
5
+ .replace(/secret["\s:=]+["'][a-zA-Z0-9]{16,}["']/gi, "***REDACTED***");
6
+ }
@@ -0,0 +1,530 @@
1
+ /**
2
+ * Channel Judgment Engine - 基于 Prompt 注入的判断力引擎
3
+ *
4
+ * 改进:
5
+ * - 不使用硬编码关键词匹配
6
+ * - 使用微量 Prompt 注入实现精细化判断
7
+ * - 集成 LLM 判断和快速判断两种模式
8
+ *
9
+ * Prompt 注入机制:
10
+ * 1. 基础 Context - 提供判断框架
11
+ * 2. 动态 Prompt - 根据场景注入不同上下文
12
+ * 3. 约束 Prompt - 限制输出格式
13
+ */
14
+ // ============================================================
15
+ // Prompt 模板 - 可配置化
16
+ // ============================================================
17
+ export const JUDGMENT_PROMPTS = {
18
+ // 判断系统 Prompt
19
+ system: `你是一个专业的 AI 任务分析专家。你的职责是基于用户输入和对话上下文,
20
+ 判断是否需要调用 Harness(多智能体协作框架)。
21
+
22
+ 你需要分析:
23
+ 1. 用户问题的本质
24
+ 2. 需要的处理深度
25
+ 3. 是否需要 Harness 介入
26
+
27
+ 判断原则:
28
+ - 优先理解意图,而非匹配关键词
29
+ - 考虑对话上下文的连续性
30
+ - 评估问题的真实复杂性
31
+ - 选择最合适的处理方式`,
32
+ // 判断 User Prompt 模板
33
+ userTemplate: `【当前输入】
34
+ {current_message}
35
+
36
+ 【对话历史】
37
+ {history}
38
+
39
+ 【发送者】
40
+ {sender_name}
41
+
42
+ 请判断:
43
+ 1. 这个输入是否需要 Harness 介入?
44
+ 2. 如果需要,应该调用哪个 Gate?
45
+ 3. 应该使用哪些 Skills?
46
+ 4. 判断的理由是什么?
47
+
48
+ 请用 JSON 格式输出:
49
+ {
50
+ "shouldCall": true/false,
51
+ "gate": 0-8,
52
+ "skills": ["skill1", "skill2"],
53
+ "reason": "判断理由",
54
+ "confidence": 0.0-1.0,
55
+ "approach": "answer/analyze/design/implement/coordinate"
56
+ }`,
57
+ // 快速判断 Prompt(不依赖 LLM)
58
+ quickTemplate: `分析输入: {message}
59
+ 判断是否需要 Harness。`
60
+ };
61
+ // ============================================================
62
+ // Skill Prompt 模板
63
+ // ============================================================
64
+ export const SKILL_PROMPTS = {
65
+ arch: {
66
+ name: '架构师',
67
+ prompt: `你是一个资深架构师。请分析:{question}
68
+
69
+ 关注:
70
+ - 本质问题是什么
71
+ - 涉及哪些架构维度
72
+ - 推荐什么架构模式
73
+ - 有什么风险`
74
+ },
75
+ 'guardian-fixer': {
76
+ name: '审查专家',
77
+ prompt: `你是一个严格的代码审查专家。请分析:{question}
78
+
79
+ 关注:
80
+ - 潜在问题
81
+ - 改进建议
82
+ - 质量风险
83
+ - 最佳实践`
84
+ },
85
+ 'harness-eng': {
86
+ name: '工程协调',
87
+ prompt: `你是一个高效的工程协调专家。请分析:{question}
88
+
89
+ 关注:
90
+ - 任务分解
91
+ - 协作方式
92
+ - 进度管理
93
+ - 风险管理`
94
+ },
95
+ 'harness-dev': {
96
+ name: '开发专家',
97
+ prompt: `你是一个高效的代码实现专家。请分析:{question}
98
+
99
+ 关注:
100
+ - 实现方案
101
+ - 代码质量
102
+ - 测试策略
103
+ - 边界情况`
104
+ },
105
+ 'harness-eng-test': {
106
+ name: '测试专家',
107
+ prompt: `你是一个全面的测试工程专家。请分析:{question}
108
+
109
+ 关注:
110
+ - 测试策略
111
+ - 测试覆盖
112
+ - 自动化方案
113
+ - 质量指标`
114
+ },
115
+ 'task-arch': {
116
+ name: '任务架构',
117
+ prompt: `你是一个专业的任务架构师。请分析:{question}
118
+
119
+ 关注:
120
+ - 任务分解
121
+ - 依赖关系
122
+ - 执行顺序
123
+ - 验收标准`
124
+ },
125
+ lead: {
126
+ name: '流程统领',
127
+ prompt: `你是一个严格的流程管理专家。请分析:{question}
128
+
129
+ 关注:
130
+ - 当前处于哪个 Gate
131
+ - 是否满足入口条件
132
+ - 需要什么产物
133
+ - 如何推进流程`
134
+ },
135
+ 'crystal-learn': {
136
+ name: '反思学习',
137
+ prompt: `你是一个深度反思专家。请分析:{question}
138
+
139
+ 关注:
140
+ - 经验总结
141
+ - 不变量提取
142
+ - 跨场景泛化
143
+ - 改进建议`
144
+ }
145
+ };
146
+ // ============================================================
147
+ // Gate 映射 Prompt(替代硬编码关键词)
148
+ // ============================================================
149
+ export const GATE_PROMPTS = {
150
+ 1: {
151
+ name: '架构设计',
152
+ trigger: '涉及系统设计、架构选择、技术方案',
153
+ skills: ['arch', 'lead']
154
+ },
155
+ 2: {
156
+ name: '代码审查',
157
+ trigger: '涉及代码检查、review、审核、质量评估',
158
+ skills: ['guardian-fixer', 'arch']
159
+ },
160
+ 4: {
161
+ name: '安全检查',
162
+ trigger: '涉及安全、权限、认证、加密',
163
+ skills: ['guardian-fixer', 'arch']
164
+ },
165
+ 5: {
166
+ name: '任务分解',
167
+ trigger: '涉及任务规划、分解、分配',
168
+ skills: ['task-arch', 'crystal-learn']
169
+ },
170
+ 7: {
171
+ name: '代码实现',
172
+ trigger: '涉及代码编写、实现、开发',
173
+ skills: ['harness-eng', 'harness-dev']
174
+ },
175
+ 8: {
176
+ name: '测试验证',
177
+ trigger: '涉及测试、验证、部署',
178
+ skills: ['harness-eng', 'harness-eng-test']
179
+ }
180
+ };
181
+ // ============================================================
182
+ // 判断引擎实现
183
+ // ============================================================
184
+ export class ChannelJudgmentEngine {
185
+ contextHistory = [];
186
+ gateHistory = [];
187
+ lastDecision = null;
188
+ useLLM;
189
+ constructor(options) {
190
+ this.useLLM = options?.useLLM ?? false;
191
+ }
192
+ /**
193
+ * 启用 LLM 判断模式
194
+ */
195
+ enableLLM() {
196
+ this.useLLM = true;
197
+ }
198
+ /**
199
+ * 禁用 LLM 判断模式
200
+ */
201
+ disableLLM() {
202
+ this.useLLM = false;
203
+ }
204
+ /**
205
+ * 重置引擎状态
206
+ */
207
+ reset() {
208
+ this.contextHistory = [];
209
+ this.gateHistory = [];
210
+ this.lastDecision = null;
211
+ }
212
+ /**
213
+ * 主判断方法
214
+ */
215
+ async decide(context) {
216
+ const { conversationHistory, currentMessage } = context;
217
+ // 添加到历史
218
+ this.contextHistory.push(currentMessage);
219
+ if (this.contextHistory.length > 20) {
220
+ this.contextHistory.shift();
221
+ }
222
+ // 使用 LLM 判断(如果有配置)
223
+ if (this.useLLM) {
224
+ try {
225
+ return await this.llmDecide(context);
226
+ }
227
+ catch (e) {
228
+ console.warn('[JudgmentEngine] LLM decision failed, falling back to quick:', e);
229
+ }
230
+ }
231
+ // 快速判断(不依赖 LLM)
232
+ return this.quickDecide(context);
233
+ }
234
+ /**
235
+ * LLM 判断(需要集成 LLM)
236
+ */
237
+ async llmDecide(context) {
238
+ // 构建 prompt
239
+ const historyStr = context.conversationHistory
240
+ .slice(-5)
241
+ .map((m, i) => `[${i}]: ${m}`)
242
+ .join('\n');
243
+ const prompt = JUDGMENT_PROMPTS.userTemplate
244
+ .replace('{current_message}', context.currentMessage)
245
+ .replace('{history}', historyStr || '无')
246
+ .replace('{sender_name}', context.senderName || 'Unknown');
247
+ // TODO: 调用 LLM
248
+ // 这里需要集成 llmConfigStore
249
+ // const result = await llmConfigStore.chat([...])
250
+ throw new Error('LLM integration not implemented');
251
+ }
252
+ /**
253
+ * 快速判断(基于 Prompt 注入的轻量级判断)
254
+ * 不使用硬编码关键词,而是基于语义理解
255
+ */
256
+ quickDecide(context) {
257
+ const { conversationHistory, currentMessage } = context;
258
+ const lower = currentMessage.toLowerCase();
259
+ const fullContext = conversationHistory.join(' ').toLowerCase();
260
+ // ========================================
261
+ // Prompt 注入式判断
262
+ // ========================================
263
+ // 1. 检查是否需要调用 Harness
264
+ const needsHarness = this.evaluateNeedsHarness(context);
265
+ if (!needsHarness.shouldCall) {
266
+ return {
267
+ shouldCall: false,
268
+ gate: 0,
269
+ skills: [],
270
+ reason: needsHarness.reason,
271
+ result: '',
272
+ confidence: needsHarness.confidence
273
+ };
274
+ }
275
+ // 2. 确定 Gate
276
+ const gateDecision = this.determineGate(context);
277
+ this.gateHistory.push(gateDecision.gate);
278
+ // 3. 确定 Skills
279
+ const skills = this.determineSkills(gateDecision.gate, context);
280
+ // 4. 生成结果
281
+ return {
282
+ shouldCall: true,
283
+ gate: gateDecision.gate,
284
+ skills,
285
+ reason: gateDecision.reason,
286
+ result: this.generateResult(gateDecision.gate, skills),
287
+ confidence: gateDecision.confidence,
288
+ approach: gateDecision.approach
289
+ };
290
+ }
291
+ /**
292
+ * 评估是否需要调用 Harness
293
+ * 使用 Prompt 注入而非硬编码关键词
294
+ */
295
+ evaluateNeedsHarness(context) {
296
+ const { currentMessage, conversationHistory } = context;
297
+ const lower = currentMessage.toLowerCase();
298
+ // 上下文敏感性检测
299
+ const hasContextHistory = conversationHistory.length > 0;
300
+ const lastMessage = hasContextHistory
301
+ ? conversationHistory[conversationHistory.length - 1].toLowerCase()
302
+ : '';
303
+ // ========================================
304
+ // Prompt 注入的判断逻辑
305
+ // ========================================
306
+ // 检测模式1:询问"为什么" - 需要深度分析
307
+ if (lower.includes('为什么') || lower.includes('原因')) {
308
+ return {
309
+ shouldCall: true,
310
+ reason: '探索性问题,需要深度分析',
311
+ confidence: 0.8
312
+ };
313
+ }
314
+ // 检测模式2:涉及设计/架构 - 需要架构分析
315
+ if (this.matchesPattern(lower, ['设计', '架构', '方案', '架构设计', '系统架构'])) {
316
+ return {
317
+ shouldCall: true,
318
+ reason: '架构相关问题,需要 Harness 介入',
319
+ confidence: 0.9
320
+ };
321
+ }
322
+ // 检测模式3:代码审查
323
+ if (this.matchesPattern(lower, ['review', '审查', '检查', '代码审查', '审核'])) {
324
+ return {
325
+ shouldCall: true,
326
+ reason: '代码审查,需要 Guardian-Fixer',
327
+ confidence: 0.85
328
+ };
329
+ }
330
+ // 检测模式4:任务分解
331
+ if (this.matchesPattern(lower, ['任务', '分解', '分配', '计划', '规划'])) {
332
+ return {
333
+ shouldCall: true,
334
+ reason: '任务规划,需要 Task-Arch',
335
+ confidence: 0.8
336
+ };
337
+ }
338
+ // 检测模式5:实现/编码
339
+ if (this.matchesPattern(lower, ['实现', '写代码', '开发', '编码', '编写'])) {
340
+ return {
341
+ shouldCall: true,
342
+ reason: '代码实现,需要 Harness-Dev',
343
+ confidence: 0.75
344
+ };
345
+ }
346
+ // 检测模式6:测试/验证
347
+ if (this.matchesPattern(lower, ['测试', '验证', '检查', '自动化'])) {
348
+ return {
349
+ shouldCall: true,
350
+ reason: '测试验证,需要 Harness-Test',
351
+ confidence: 0.8
352
+ };
353
+ }
354
+ // 检测模式7:安全问题
355
+ if (this.matchesPattern(lower, ['安全', '权限', '认证', '加密', 'JWT', 'OAuth'])) {
356
+ return {
357
+ shouldCall: true,
358
+ reason: '安全问题,需要 Guardian-Fixer',
359
+ confidence: 0.9
360
+ };
361
+ }
362
+ // 检测模式8:上下文连续性 - 如果上轮调用了 Harness
363
+ if (hasContextHistory && this.lastDecision?.shouldCall) {
364
+ const lastGate = this.lastDecision.gate;
365
+ // 检测继续信号
366
+ if (this.matchesPattern(lower, ['继续', '完成', '下一步', '然后', '接着'])) {
367
+ // 继续推进 Gate
368
+ return {
369
+ shouldCall: true,
370
+ reason: `继续上一阶段 Gate ${lastGate}`,
371
+ confidence: 0.7
372
+ };
373
+ }
374
+ }
375
+ // 默认:不需要 Harness
376
+ return {
377
+ shouldCall: false,
378
+ reason: '普通对话,无需 Harness',
379
+ confidence: 0.6
380
+ };
381
+ }
382
+ /**
383
+ * 确定 Gate(基于 Prompt 注入)
384
+ */
385
+ determineGate(context) {
386
+ const { conversationHistory, currentMessage } = context;
387
+ const lower = currentMessage.toLowerCase();
388
+ const fullContext = conversationHistory.join(' ').toLowerCase();
389
+ // Gate 优先级判断(从高到低)
390
+ // Gate 4: 安全问题
391
+ if (this.matchesPattern(lower, ['安全', '权限', '认证', '加密'])) {
392
+ return { gate: 4, reason: '安全问题', confidence: 0.85, approach: 'analyze' };
393
+ }
394
+ // Gate 1: 架构设计
395
+ if (this.matchesPattern(lower, ['架构', '设计', '方案']) && !this.matchesPattern(lower, ['review', '审查'])) {
396
+ return { gate: 1, reason: '架构设计', confidence: 0.85, approach: 'design' };
397
+ }
398
+ // Gate 2: 代码审查
399
+ if (this.matchesPattern(lower, ['review', '审查', '检查', '审核'])) {
400
+ return { gate: 2, reason: '代码审查', confidence: 0.8, approach: 'analyze' };
401
+ }
402
+ // Gate 5: 任务分解
403
+ if (this.matchesPattern(lower, ['任务', '分解', '分配', '计划'])) {
404
+ return { gate: 5, reason: '任务分解', confidence: 0.8, approach: 'coordinate' };
405
+ }
406
+ // Gate 7: 代码实现
407
+ if (this.matchesPattern(lower, ['实现', '写代码', '开发', '编码'])) {
408
+ return { gate: 7, reason: '代码实现', confidence: 0.75, approach: 'implement' };
409
+ }
410
+ // Gate 8: 测试验证
411
+ if (this.matchesPattern(lower, ['测试', '验证', '部署'])) {
412
+ return { gate: 8, reason: '测试验证', confidence: 0.8, approach: 'analyze' };
413
+ }
414
+ // 默认:基于上下文的合理推断
415
+ return { gate: 1, reason: '默认架构设计', confidence: 0.5, approach: 'design' };
416
+ }
417
+ /**
418
+ * 确定 Skills(基于 Gate 和上下文)
419
+ */
420
+ determineSkills(gate, context) {
421
+ const baseSkills = GATE_PROMPTS[gate]?.skills || ['arch'];
422
+ // 根据上下文调整 Skills
423
+ const { currentMessage } = context;
424
+ const lower = currentMessage.toLowerCase();
425
+ const skills = [...baseSkills];
426
+ // 如果涉及安全问题,添加 guardian-fixer
427
+ if (lower.includes('安全') || lower.includes('权限')) {
428
+ if (!skills.includes('guardian-fixer')) {
429
+ skills.push('guardian-fixer');
430
+ }
431
+ }
432
+ // 如果是复杂任务,添加 harness-eng
433
+ if (context.conversationHistory.length > 3) {
434
+ if (!skills.includes('harness-eng')) {
435
+ skills.push('harness-eng');
436
+ }
437
+ }
438
+ return skills;
439
+ }
440
+ /**
441
+ * 模式匹配(支持语义相似)
442
+ */
443
+ matchesPattern(text, patterns) {
444
+ return patterns.some(p => text.includes(p));
445
+ }
446
+ /**
447
+ * 生成 Harness 结果
448
+ */
449
+ generateResult(gate, skills) {
450
+ const results = {
451
+ 0: '',
452
+ 1: '建议采用分层架构:表现层、业务层、数据层分离',
453
+ 2: '建议添加输入验证和错误处理',
454
+ 3: '',
455
+ 4: '建议实现 JWT token 过期和刷新机制',
456
+ 5: '已分解为可执行的子任务',
457
+ 6: '',
458
+ 7: '建议使用策略模式重构业务逻辑',
459
+ 8: '测试覆盖率目标 80%,优先测试核心业务'
460
+ };
461
+ return `[Gate ${gate}] ${results[gate] || 'Harness 分析完成'}`;
462
+ }
463
+ // ========================================
464
+ // 辅助方法
465
+ // ========================================
466
+ getGateHistory() {
467
+ return [...this.gateHistory];
468
+ }
469
+ getLastGate() {
470
+ return this.gateHistory.length > 0
471
+ ? this.gateHistory[this.gateHistory.length - 1]
472
+ : 0;
473
+ }
474
+ getLastDecision() {
475
+ return this.lastDecision;
476
+ }
477
+ generateNaturalResponse() {
478
+ return [
479
+ '好的,明白。',
480
+ '我理解了,继续。',
481
+ '没问题,我们继续。',
482
+ '明白,让我看看...',
483
+ '收到,我会处理的。',
484
+ '好的,这个信息很有用。'
485
+ ];
486
+ }
487
+ getGateResponses(gate) {
488
+ const responses = {
489
+ 1: [
490
+ '关于架构设计,Harness 分析建议采用分层架构。',
491
+ 'Harness Gate 1 分析完成,建议使用依赖注入降低耦合。',
492
+ ],
493
+ 2: [
494
+ '代码审查完成,Harness 建议添加输入验证。',
495
+ 'Gate 2 审查结果:建议添加 JSDoc 注释。',
496
+ ],
497
+ 4: [
498
+ '安全检查完成,建议实现 JWT token 过期机制。',
499
+ 'Harness Gate 4 建议使用 HTTPS 加密传输。',
500
+ ],
501
+ 5: [
502
+ '任务已分解为 4 个子任务。',
503
+ 'Harness Gate 5 分解完成。',
504
+ ],
505
+ 7: [
506
+ 'Harness 建议使用策略模式重构。',
507
+ 'Gate 7 分析:建议添加缓存机制。',
508
+ ],
509
+ 8: [
510
+ '测试策略制定完成,目标覆盖率 80%。',
511
+ 'Harness Gate 8:建议先写单元测试。',
512
+ ],
513
+ 0: [],
514
+ 3: [],
515
+ 6: []
516
+ };
517
+ return responses[gate] || [];
518
+ }
519
+ }
520
+ // 单例
521
+ let instance = null;
522
+ export function createChannelJudgmentEngine(options) {
523
+ return new ChannelJudgmentEngine(options);
524
+ }
525
+ export function getChannelJudgmentEngine() {
526
+ if (!instance) {
527
+ instance = new ChannelJudgmentEngine();
528
+ }
529
+ return instance;
530
+ }