@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
@@ -1,49 +1,12 @@
1
- "use strict";
2
1
  /**
3
2
  * LLM API Configuration Store
4
3
  * 支持多供应商 API 配置管理
5
4
  */
6
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
- if (k2 === undefined) k2 = k;
8
- var desc = Object.getOwnPropertyDescriptor(m, k);
9
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
- desc = { enumerable: true, get: function() { return m[k]; } };
11
- }
12
- Object.defineProperty(o, k2, desc);
13
- }) : (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- o[k2] = m[k];
16
- }));
17
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
- Object.defineProperty(o, "default", { enumerable: true, value: v });
19
- }) : function(o, v) {
20
- o["default"] = v;
21
- });
22
- var __importStar = (this && this.__importStar) || (function () {
23
- var ownKeys = function(o) {
24
- ownKeys = Object.getOwnPropertyNames || function (o) {
25
- var ar = [];
26
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
- return ar;
28
- };
29
- return ownKeys(o);
30
- };
31
- return function (mod) {
32
- if (mod && mod.__esModule) return mod;
33
- var result = {};
34
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
- __setModuleDefault(result, mod);
36
- return result;
37
- };
38
- })();
39
- Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.llmConfigStore = exports.PROVIDER_INFO = exports.DEFAULT_PROVIDER_CONFIGS = void 0;
41
- exports.getPiSDKConfig = getPiSDKConfig;
42
- const fs = __importStar(require("fs/promises"));
43
- const path = __importStar(require("path"));
5
+ import * as fs from 'fs/promises';
6
+ import * as path from 'path';
44
7
  const CONFIG_DIR = path.join(process.env.HOME || '/tmp', '.bolloon');
45
8
  const CONFIG_PATH = path.join(CONFIG_DIR, 'llm-config.json');
46
- exports.DEFAULT_PROVIDER_CONFIGS = {
9
+ export const DEFAULT_PROVIDER_CONFIGS = {
47
10
  openai: {
48
11
  enabled: false,
49
12
  apiKey: '',
@@ -108,7 +71,7 @@ exports.DEFAULT_PROVIDER_CONFIGS = {
108
71
  requiresApiKey: false
109
72
  }
110
73
  };
111
- exports.PROVIDER_INFO = {
74
+ export const PROVIDER_INFO = {
112
75
  openai: { name: 'OpenAI', description: 'GPT-4, GPT-3.5 等模型', requiresApiKey: true },
113
76
  anthropic: { name: 'Anthropic', description: 'Claude 3.5 系列模型', requiresApiKey: true },
114
77
  openrouter: { name: 'OpenRouter', description: '聚合多个 AI 供应商', requiresApiKey: true },
@@ -120,22 +83,22 @@ exports.PROVIDER_INFO = {
120
83
  function getDefaultConfig() {
121
84
  const envConfigs = {};
122
85
  if (process.env.OPENAI_API_KEY) {
123
- envConfigs.openai = { ...exports.DEFAULT_PROVIDER_CONFIGS.openai, enabled: true, apiKey: process.env.OPENAI_API_KEY };
86
+ envConfigs.openai = { ...DEFAULT_PROVIDER_CONFIGS.openai, enabled: true, apiKey: process.env.OPENAI_API_KEY };
124
87
  }
125
88
  if (process.env.ANTHROPIC_API_KEY) {
126
- envConfigs.anthropic = { ...exports.DEFAULT_PROVIDER_CONFIGS.anthropic, enabled: true, apiKey: process.env.ANTHROPIC_API_KEY };
89
+ envConfigs.anthropic = { ...DEFAULT_PROVIDER_CONFIGS.anthropic, enabled: true, apiKey: process.env.ANTHROPIC_API_KEY };
127
90
  }
128
91
  if (process.env.OPENROUTER_API_KEY) {
129
- envConfigs.openrouter = { ...exports.DEFAULT_PROVIDER_CONFIGS.openrouter, enabled: true, apiKey: process.env.OPENROUTER_API_KEY };
92
+ envConfigs.openrouter = { ...DEFAULT_PROVIDER_CONFIGS.openrouter, enabled: true, apiKey: process.env.OPENROUTER_API_KEY };
130
93
  }
131
94
  if (process.env.GEMINI_API_KEY) {
132
- envConfigs.gemini = { ...exports.DEFAULT_PROVIDER_CONFIGS.gemini, enabled: true, apiKey: process.env.GEMINI_API_KEY };
95
+ envConfigs.gemini = { ...DEFAULT_PROVIDER_CONFIGS.gemini, enabled: true, apiKey: process.env.GEMINI_API_KEY };
133
96
  }
134
97
  if (process.env.MINIMAX_API_KEY) {
135
- envConfigs.minimax = { ...exports.DEFAULT_PROVIDER_CONFIGS.minimax, enabled: true, apiKey: process.env.MINIMAX_API_KEY };
98
+ envConfigs.minimax = { ...DEFAULT_PROVIDER_CONFIGS.minimax, enabled: true, apiKey: process.env.MINIMAX_API_KEY };
136
99
  }
137
100
  if (process.env.OLLAMA_BASE_URL) {
138
- envConfigs.ollama = { ...exports.DEFAULT_PROVIDER_CONFIGS.ollama, enabled: true, baseUrl: process.env.OLLAMA_BASE_URL };
101
+ envConfigs.ollama = { ...DEFAULT_PROVIDER_CONFIGS.ollama, enabled: true, baseUrl: process.env.OLLAMA_BASE_URL };
139
102
  }
140
103
  let activeProvider = 'ollama';
141
104
  if (process.env.OPENAI_API_KEY)
@@ -150,7 +113,7 @@ function getDefaultConfig() {
150
113
  activeProvider = 'minimax';
151
114
  else if (process.env.OLLAMA_BASE_URL)
152
115
  activeProvider = 'ollama';
153
- const providers = { ...exports.DEFAULT_PROVIDER_CONFIGS };
116
+ const providers = { ...DEFAULT_PROVIDER_CONFIGS };
154
117
  for (const [provider, config] of Object.entries(envConfigs)) {
155
118
  if (config) {
156
119
  providers[provider] = config;
@@ -171,7 +134,19 @@ class LLMConfigStore {
171
134
  try {
172
135
  await fs.mkdir(CONFIG_DIR, { recursive: true });
173
136
  const data = await fs.readFile(CONFIG_PATH, 'utf-8');
174
- this.config = JSON.parse(data);
137
+ const loadedConfig = JSON.parse(data);
138
+ // 确保加载的配置包含所有默认供应商,缺失的用默认值补充
139
+ const defaultProviders = Object.keys(DEFAULT_PROVIDER_CONFIGS);
140
+ for (const provider of defaultProviders) {
141
+ if (!loadedConfig.providers[provider]) {
142
+ loadedConfig.providers[provider] = { ...DEFAULT_PROVIDER_CONFIGS[provider] };
143
+ }
144
+ }
145
+ // 确保有 activeProvider
146
+ if (!loadedConfig.activeProvider || !DEFAULT_PROVIDER_CONFIGS[loadedConfig.activeProvider]) {
147
+ loadedConfig.activeProvider = 'ollama';
148
+ }
149
+ this.config = loadedConfig;
175
150
  }
176
151
  catch {
177
152
  this.config = getDefaultConfig();
@@ -278,15 +253,15 @@ class LLMConfigStore {
278
253
  return headers;
279
254
  }
280
255
  getProviderInfo(provider) {
281
- return exports.PROVIDER_INFO[provider];
256
+ return PROVIDER_INFO[provider];
282
257
  }
283
258
  getAllProviderInfo() {
284
- return exports.PROVIDER_INFO;
259
+ return PROVIDER_INFO;
285
260
  }
286
261
  }
287
- exports.llmConfigStore = new LLMConfigStore();
288
- function getPiSDKConfig() {
289
- const config = exports.llmConfigStore.config;
262
+ export const llmConfigStore = new LLMConfigStore();
263
+ export function getPiSDKConfig() {
264
+ const config = llmConfigStore.config;
290
265
  if (!config) {
291
266
  return { provider: 'ollama' };
292
267
  }
@@ -294,8 +269,7 @@ function getPiSDKConfig() {
294
269
  return {
295
270
  provider: config.activeProvider,
296
271
  apiKey: activeConfig.apiKey || undefined,
297
- baseUrl: activeConfig.baseUrl !== exports.DEFAULT_PROVIDER_CONFIGS[config.activeProvider].baseUrl ? activeConfig.baseUrl : undefined,
298
- model: activeConfig.model !== exports.DEFAULT_PROVIDER_CONFIGS[config.activeProvider].model ? activeConfig.model : undefined
272
+ baseUrl: activeConfig.baseUrl !== DEFAULT_PROVIDER_CONFIGS[config.activeProvider].baseUrl ? activeConfig.baseUrl : undefined,
273
+ model: activeConfig.model !== DEFAULT_PROVIDER_CONFIGS[config.activeProvider].model ? activeConfig.model : undefined
299
274
  };
300
275
  }
301
- //# sourceMappingURL=config-store.js.map
@@ -0,0 +1,389 @@
1
+ /**
2
+ * LLM-as-Judge Client
3
+ *
4
+ * 集成 LLMConfigStore + PiAIModel + Prompt 配置
5
+ * 实现真正的 LLM 原生判断力
6
+ */
7
+ import { llmConfigStore } from './config-store.js';
8
+ import { PiAIModel } from './pi-ai.js';
9
+ // 预定义的判断 Prompt 配置
10
+ export const JUDGMENT_PROMPTS = {
11
+ // 默认判断 Prompt
12
+ default: {
13
+ systemPrompt: `你是一个专业的 AI 任务分析专家。你的职责是深入理解用户的问题,并给出精准的判断。
14
+
15
+ 核心原则:
16
+ 1. 第一性原理:追问问题的本质,而非表面现象
17
+ 2. 多角度思考:考虑不同视角和影响因素
18
+ 3. 上下文感知:理解对话历史和隐含意图
19
+ 4. 动态评估:根据实际情况判断复杂性
20
+
21
+ 你的判断会影响后续的智能体协作方式,请认真分析。`,
22
+ userPromptTemplate: `【当前输入】
23
+ {user_input}
24
+
25
+ 【对话历史】
26
+ {history}
27
+
28
+ 【发送者】
29
+ {sender_name}
30
+
31
+ 请分析以上输入,判断:
32
+
33
+ ## 理解
34
+ 1. 问题本质是什么?(how-to / why / what / should / feasibility)
35
+ 2. 核心需求是什么?
36
+ 3. 有哪些隐含信息需要注意?
37
+
38
+ ## 评估
39
+ 1. 复杂性等级(simple / moderate / complex / profound)
40
+ 2. 为什么是这个等级?
41
+ 3. 需要多深的理解?(surface / deeper / fundamental)
42
+
43
+ ## 决策
44
+ 1. 推荐处理方式(answer / analyze / design / implement / coordinate)
45
+ 2. 为什么这样处理?
46
+
47
+ ## 路由
48
+ 1. 应该调用哪些 Skills?(arch / guardian-fixer / harness-eng / harness-dev / task-arch / crystal-learn)
49
+ 2. 需要什么协作模式?(solo / pair / team)
50
+
51
+ 请用 JSON 格式输出,格式如下:
52
+ {
53
+ "understanding": {
54
+ "essence": "问题本质",
55
+ "coreNeed": "核心需求",
56
+ "implicit": ["隐含信息1", "隐含信息2"]
57
+ },
58
+ "assessment": {
59
+ "complexity": "complex",
60
+ "complexityReason": "评估理由",
61
+ "depth": "deeper"
62
+ },
63
+ "decision": {
64
+ "approach": "design",
65
+ "reasoning": "决策理由"
66
+ },
67
+ "routing": {
68
+ "skills": ["arch", "guardian-fixer"],
69
+ "collaboration": "pair"
70
+ }
71
+ }`,
72
+ outputFormat: 'json'
73
+ },
74
+ // 架构相关问题
75
+ architecture: {
76
+ systemPrompt: `你是一个经验丰富的系统架构师。你擅长:
77
+ - 从需求中提取本质问题
78
+ - 设计可扩展的系统架构
79
+ - 权衡技术方案
80
+ - 识别架构风险
81
+
82
+ 你相信:
83
+ - 本质和实现必须分离
84
+ - 好的架构从简单规则中生长
85
+ - 复杂性应该被控制而非消除`,
86
+ userPromptTemplate: `【架构设计问题】
87
+ {user_input}
88
+
89
+ 【上下文】
90
+ {context}
91
+
92
+ 请进行架构分析,判断是否需要 Harness 介入。
93
+
94
+ 分析维度:
95
+ 1. 涉及哪些架构维度?(分层、微服务、事件驱动等)
96
+ 2. 需要什么级别的架构设计?(概念性、逻辑性、物理性)
97
+ 3. 有什么架构风险需要考虑?
98
+
99
+ 输出 JSON:
100
+ {
101
+ "needsHarness": true/false,
102
+ "gate": 0-8,
103
+ "skills": ["arch", "harness-eng"],
104
+ "architectureType": "microservices/layered/event-driven/etc",
105
+ "concerns": ["concern1", "concern2"]
106
+ }`,
107
+ outputFormat: 'json'
108
+ },
109
+ // 代码相关问题
110
+ code: {
111
+ systemPrompt: `你是一个高效的代码实现专家。你擅长:
112
+ - 快速理解和实现需求
113
+ - 编写清晰、可维护的代码
114
+ - 遵循最佳实践
115
+ - 处理边界情况`,
116
+ userPromptTemplate: `【代码问题】
117
+ {user_input}
118
+
119
+ 请判断这个问题:
120
+
121
+ 1. 是新功能实现还是修复问题?
122
+ 2. 涉及哪些代码层面?(业务逻辑、数据访问、接口设计)
123
+ 3. 需要什么测试覆盖?
124
+
125
+ 输出 JSON:
126
+ {
127
+ "needsHarness": true/false,
128
+ "gate": 0-8,
129
+ "skills": ["harness-dev", "guardian-fixer"],
130
+ "codeType": "feature/fix/refactor",
131
+ "testCoverage": "unit/integration/e2e"
132
+ }`,
133
+ outputFormat: 'json'
134
+ },
135
+ // 安全相关问题
136
+ security: {
137
+ systemPrompt: `你是一个严格的安全专家。你擅长:
138
+ - 识别安全风险
139
+ - 评估威胁模型
140
+ - 设计安全方案
141
+ - 遵循安全最佳实践`,
142
+ userPromptTemplate: `【安全问题】
143
+ {user_input}
144
+
145
+ 请分析安全问题:
146
+
147
+ 1. 涉及哪些安全维度?(认证、授权、数据保护、传输安全)
148
+ 2. 风险等级?(低/中/高/严重)
149
+ 3. 需要什么安全措施?
150
+
151
+ 输出 JSON:
152
+ {
153
+ "needsHarness": true,
154
+ "gate": 4,
155
+ "skills": ["guardian-fixer", "arch"],
156
+ "securityDimensions": ["authentication", "authorization"],
157
+ "riskLevel": "high",
158
+ "recommendations": ["rec1", "rec2"]
159
+ }`,
160
+ outputFormat: 'json'
161
+ }
162
+ };
163
+ /**
164
+ * LLM-as-Judge 客户端
165
+ */
166
+ export class LLMJudgmentClient {
167
+ model = null;
168
+ useLLM = false;
169
+ promptConfig = JUDGMENT_PROMPTS.default;
170
+ constructor(options) {
171
+ if (options?.promptConfig && JUDGMENT_PROMPTS[options.promptConfig]) {
172
+ this.promptConfig = JUDGMENT_PROMPTS[options.promptConfig];
173
+ }
174
+ this.useLLM = options?.useLLM ?? false;
175
+ }
176
+ /**
177
+ * 初始化 LLM 模型
178
+ */
179
+ async initialize() {
180
+ if (this.useLLM) {
181
+ const config = await llmConfigStore.getActiveProviderConfig();
182
+ if (config && config.enabled) {
183
+ this.model = new PiAIModel({
184
+ provider: await llmConfigStore.getActiveProvider(),
185
+ apiKey: config.apiKey,
186
+ baseUrl: config.baseUrl,
187
+ model: config.model
188
+ });
189
+ console.log('[LLM-as-Judge] Initialized with LLM');
190
+ }
191
+ else {
192
+ console.warn('[LLM-as-Judge] No LLM provider configured, using quick judgment');
193
+ this.useLLM = false;
194
+ }
195
+ }
196
+ }
197
+ /**
198
+ * 设置 Prompt 配置
199
+ */
200
+ setPromptConfig(configName) {
201
+ if (JUDGMENT_PROMPTS[configName]) {
202
+ this.promptConfig = JUDGMENT_PROMPTS[configName];
203
+ }
204
+ }
205
+ /**
206
+ * 执行 LLM 判断
207
+ */
208
+ async judge(userInput, options) {
209
+ // 构建 prompt
210
+ const historyStr = options?.history
211
+ ? options.history.slice(-5).map((m, i) => `[${i + 1}] ${m}`).join('\n')
212
+ : '无';
213
+ const userPrompt = this.promptConfig.userPromptTemplate
214
+ .replace('{user_input}', userInput)
215
+ .replace('{history}', historyStr)
216
+ .replace('{sender_name}', options?.senderName || 'Unknown')
217
+ .replace('{context}', options?.context || '无');
218
+ // 如果不使用 LLM 或模型未初始化,返回默认结果
219
+ if (!this.useLLM || !this.model) {
220
+ return this.quickJudgment(userInput);
221
+ }
222
+ try {
223
+ // 调用 LLM
224
+ const messages = [
225
+ { role: 'system', content: this.promptConfig.systemPrompt },
226
+ { role: 'user', content: userPrompt }
227
+ ];
228
+ const response = await this.model.chat(userPrompt);
229
+ // 解析 JSON 输出
230
+ return this.parseResponse(response.reply);
231
+ }
232
+ catch (error) {
233
+ console.error('[LLM-as-Judge] LLM call failed:', error);
234
+ return this.quickJudgment(userInput);
235
+ }
236
+ }
237
+ /**
238
+ * 解析 LLM 输出
239
+ */
240
+ parseResponse(response) {
241
+ try {
242
+ // 尝试提取 JSON
243
+ const jsonMatch = response.match(/\{[\s\S]*\}/);
244
+ if (jsonMatch) {
245
+ const parsed = JSON.parse(jsonMatch[0]);
246
+ return {
247
+ understanding: {
248
+ essence: parsed.understanding?.essence || parsed.essence || 'unknown',
249
+ coreNeed: parsed.understanding?.coreNeed || parsed.coreNeed || 'unknown',
250
+ implicit: parsed.understanding?.implicit || parsed.implicit || []
251
+ },
252
+ assessment: {
253
+ complexity: this.normalizeComplexity(parsed.assessment?.complexity || parsed.complexity),
254
+ complexityReason: parsed.assessment?.complexityReason || parsed.complexityReason || '',
255
+ depth: this.normalizeDepth(parsed.assessment?.depth || parsed.depth)
256
+ },
257
+ decision: {
258
+ approach: this.normalizeApproach(parsed.decision?.approach || parsed.approach),
259
+ reasoning: parsed.decision?.reasoning || parsed.reasoning || ''
260
+ },
261
+ routing: {
262
+ skills: parsed.routing?.skills || parsed.skills || [],
263
+ collaboration: this.normalizeCollaboration(parsed.routing?.collaboration || parsed.collaboration)
264
+ },
265
+ raw: response
266
+ };
267
+ }
268
+ }
269
+ catch (e) {
270
+ console.warn('[LLM-as-Judge] JSON parse failed, using fallback');
271
+ }
272
+ // 解析失败,返回默认结果
273
+ return this.quickJudgment(response);
274
+ }
275
+ /**
276
+ * 快速判断(fallback)
277
+ */
278
+ quickJudgment(input) {
279
+ const lower = input.toLowerCase();
280
+ // 简化判断逻辑
281
+ let complexity = 'moderate';
282
+ let skills = [];
283
+ let collaboration = 'solo';
284
+ if (lower.includes('架构') || lower.includes('设计')) {
285
+ skills = ['arch'];
286
+ complexity = 'complex';
287
+ collaboration = 'pair';
288
+ }
289
+ if (lower.includes('review') || lower.includes('审查')) {
290
+ skills = ['guardian-fixer'];
291
+ }
292
+ if (lower.includes('安全') || lower.includes('权限')) {
293
+ skills = ['guardian-fixer', 'arch'];
294
+ complexity = 'complex';
295
+ }
296
+ if (lower.includes('任务') || lower.includes('分解')) {
297
+ skills = ['task-arch'];
298
+ }
299
+ return {
300
+ understanding: {
301
+ essence: this.extractEssence(lower),
302
+ coreNeed: this.extractCoreNeed(lower),
303
+ implicit: []
304
+ },
305
+ assessment: {
306
+ complexity,
307
+ complexityReason: '基于快速模式判断',
308
+ depth: 'surface'
309
+ },
310
+ decision: {
311
+ approach: skills.length > 0 ? 'analyze' : 'answer',
312
+ reasoning: '快速判断'
313
+ },
314
+ routing: {
315
+ skills,
316
+ collaboration
317
+ }
318
+ };
319
+ }
320
+ extractEssence(input) {
321
+ if (input.includes('为什么'))
322
+ return 'why';
323
+ if (input.includes('怎么') || input.includes('如何'))
324
+ return 'how-to';
325
+ if (input.includes('应该'))
326
+ return 'should';
327
+ return 'general';
328
+ }
329
+ extractCoreNeed(input) {
330
+ if (input.includes('设计') || input.includes('架构'))
331
+ return '设计方案';
332
+ if (input.includes('实现') || input.includes('写'))
333
+ return '实现代码';
334
+ if (input.includes('review') || input.includes('审查'))
335
+ return '审查代码';
336
+ return '获得帮助';
337
+ }
338
+ normalizeComplexity(c) {
339
+ const lower = c.toLowerCase();
340
+ if (lower.includes('simple'))
341
+ return 'simple';
342
+ if (lower.includes('moderate'))
343
+ return 'moderate';
344
+ if (lower.includes('complex'))
345
+ return 'complex';
346
+ return 'profound';
347
+ }
348
+ normalizeDepth(d) {
349
+ const lower = d.toLowerCase();
350
+ if (lower.includes('surface'))
351
+ return 'surface';
352
+ if (lower.includes('deeper'))
353
+ return 'deeper';
354
+ return 'fundamental';
355
+ }
356
+ normalizeApproach(a) {
357
+ const lower = a.toLowerCase();
358
+ if (lower.includes('answer'))
359
+ return 'answer';
360
+ if (lower.includes('analyze'))
361
+ return 'analyze';
362
+ if (lower.includes('design'))
363
+ return 'design';
364
+ if (lower.includes('implement'))
365
+ return 'implement';
366
+ return 'coordinate';
367
+ }
368
+ normalizeCollaboration(c) {
369
+ const lower = c.toLowerCase();
370
+ if (lower.includes('solo'))
371
+ return 'solo';
372
+ if (lower.includes('pair'))
373
+ return 'pair';
374
+ return 'team';
375
+ }
376
+ }
377
+ // 工厂函数
378
+ export function createLLMJudgmentClient(options) {
379
+ return new LLMJudgmentClient(options);
380
+ }
381
+ // 默认实例
382
+ let defaultClient = null;
383
+ export async function getDefaultJudgmentClient() {
384
+ if (!defaultClient) {
385
+ defaultClient = createLLMJudgmentClient({ useLLM: true });
386
+ await defaultClient.initialize();
387
+ }
388
+ return defaultClient;
389
+ }
package/dist/llm/pi-ai.js CHANGED
@@ -1,47 +1,6 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.MinimaxLLM = exports.PiAIModel = void 0;
37
- exports.initPiAI = initPiAI;
38
- exports.getModel = getModel;
39
- exports.isModelAvailable = isModelAvailable;
40
- exports.getMinimax = getMinimax;
41
- exports.initMinimax = initMinimax;
42
- const path = __importStar(require("path"));
43
- const fs = __importStar(require("fs"));
44
- class PiAIModel {
1
+ import * as path from 'path';
2
+ import * as fs from 'fs';
3
+ export class PiAIModel {
45
4
  config;
46
5
  provider;
47
6
  constructor(config) {
@@ -383,8 +342,6 @@ ${prompt}`;
383
342
  return qualityScore >= threshold;
384
343
  }
385
344
  }
386
- exports.PiAIModel = PiAIModel;
387
- exports.MinimaxLLM = PiAIModel;
388
345
  let modelInstance = null;
389
346
  function detectProvider() {
390
347
  // 首先检查配置文件(优先级最高)
@@ -424,7 +381,7 @@ function detectModel(provider) {
424
381
  };
425
382
  return defaults[provider];
426
383
  }
427
- function initPiAI(config = {}) {
384
+ export function initPiAI(config = {}) {
428
385
  const provider = config.provider || detectProvider();
429
386
  const model = config.model || detectModel(provider);
430
387
  console.log('[PiAIModel] Initializing with provider:', provider, 'model:', model);
@@ -453,19 +410,19 @@ function initPiAI(config = {}) {
453
410
  console.log('[PiAIModel] Model instance created, provider:', provider);
454
411
  return modelInstance;
455
412
  }
456
- function getModel() {
413
+ export function getModel() {
457
414
  if (!modelInstance) {
458
415
  throw new Error('PiAI not initialized. Call initPiAI first.');
459
416
  }
460
417
  return modelInstance;
461
418
  }
462
- function isModelAvailable() {
419
+ export function isModelAvailable() {
463
420
  return modelInstance !== null;
464
421
  }
465
- function getMinimax() {
422
+ export function getMinimax() {
466
423
  return getModel();
467
424
  }
468
- function initMinimax(config = {}) {
425
+ export function initMinimax(config = {}) {
469
426
  return initPiAI(config);
470
427
  }
471
- //# sourceMappingURL=pi-ai.js.map
428
+ export { PiAIModel as MinimaxLLM };