@bolloon/bolloon-agent 0.1.1 → 0.1.2

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 +157 -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,674 @@
1
+ /**
2
+ * WorkflowPivotLoop - Robust Agent Loop with Adaptive Iteration Control
3
+ *
4
+ * Based on the architecture pattern:
5
+ * 1. Loop interrupted by max iterations
6
+ * 2. Model decides via pending_tool_uses (empty = normal completion)
7
+ * 3. Conditional routing based on tool call presence
8
+ *
9
+ * Key improvements over simple ReAct:
10
+ * - Dynamic loop length based on task complexity
11
+ * - Multi-dimensional exit conditions
12
+ * - Consecutive invalid iteration detection
13
+ * - Token budget awareness
14
+ */
15
+
16
+ import type { Tool, ToolResult, StreamCallback, StreamEvent } from './pi-sdk.js';
17
+
18
+ export interface PivotLoopConfig {
19
+ maxIterations: number;
20
+ minIterations?: number;
21
+ qualityThreshold?: number;
22
+ maxConsecutiveNoProgress?: number;
23
+ maxTokenBudget?: number;
24
+ complexity?: TaskComplexity;
25
+ }
26
+
27
+ export interface PivotLoopState {
28
+ iteration: number;
29
+ totalTokens: number;
30
+ toolCallsCount: number;
31
+ consecutiveNoProgress: number;
32
+ qualityScores: number[];
33
+ pendingToolUses: ToolDefinition[];
34
+ lastMeaningfulWork: number;
35
+ }
36
+
37
+ export interface ToolDefinition {
38
+ name: string;
39
+ description: string;
40
+ parameters: Record<string, string>;
41
+ }
42
+
43
+ export interface LoopResult {
44
+ success: boolean;
45
+ response: string;
46
+ iterations: number;
47
+ toolCalls: number;
48
+ qualityScore: number;
49
+ exitReason: ExitReason;
50
+ state: PivotLoopState;
51
+ }
52
+
53
+ export type ExitReason =
54
+ | 'max_iterations'
55
+ | 'no_pending_tools'
56
+ | 'quality_threshold_met'
57
+ | 'no_progress_exhausted'
58
+ | 'token_budget_exceeded'
59
+ | 'min_iterations_not_met'
60
+ | 'error';
61
+
62
+ export type TaskComplexity = 'simple' | 'moderate' | 'complex';
63
+
64
+ export interface TaskProfile {
65
+ complexity: TaskComplexity;
66
+ estimatedSteps: number;
67
+ suggestedMaxIterations: number;
68
+ tokenBudget: number;
69
+ }
70
+
71
+ /**
72
+ * Analyze input to determine task complexity
73
+ */
74
+ function analyzeTaskComplexity(input: string): TaskProfile {
75
+ const inputLower = input.toLowerCase();
76
+ const inputLength = input.length;
77
+
78
+ // Simple task indicators
79
+ const simpleIndicators = [
80
+ '读取', '查看', '显示', '列出', '获取', 'what is', 'show me',
81
+ 'list', 'get', 'show', 'read', 'view', 'display'
82
+ ];
83
+
84
+ // Complex task indicators
85
+ const complexIndicators = [
86
+ '分析', '比较', '改进', '优化', '重构', '实现', '设计', '创建',
87
+ 'analyze', 'compare', 'improve', 'optimize', 'refactor', 'implement',
88
+ 'design', 'create', 'build', 'develop'
89
+ ];
90
+
91
+ // Question patterns suggest moderate complexity
92
+ const questionPatterns = [
93
+ '如何', '怎么', '为什么', '什么', 'which', 'how', 'why', 'what', '?'
94
+ ];
95
+
96
+ const simpleCount = simpleIndicators.filter(i => inputLower.includes(i)).length;
97
+ const complexCount = complexIndicators.filter(i => inputLower.includes(i)).length;
98
+ const questionCount = questionPatterns.filter(p => inputLower.includes(p)).length;
99
+
100
+ let complexity: TaskComplexity;
101
+ let estimatedSteps: number;
102
+
103
+ if (complexCount > simpleCount && complexCount > 1) {
104
+ complexity = 'complex';
105
+ estimatedSteps = 5 + complexCount * 2;
106
+ } else if (questionCount > 2 || (simpleCount > 0 && complexCount > 0)) {
107
+ complexity = 'moderate';
108
+ estimatedSteps = 3 + questionCount;
109
+ } else if (inputLength < 50 && simpleCount > 0) {
110
+ complexity = 'simple';
111
+ estimatedSteps = 1 + simpleCount;
112
+ } else if (complexCount > 0) {
113
+ complexity = 'complex';
114
+ estimatedSteps = 4 + complexCount;
115
+ } else {
116
+ complexity = 'moderate';
117
+ estimatedSteps = 3;
118
+ }
119
+
120
+ // Adjust based on input length (longer inputs often mean more complex tasks)
121
+ if (inputLength > 500 && complexity !== 'complex') {
122
+ complexity = 'moderate';
123
+ estimatedSteps = Math.max(estimatedSteps, 4);
124
+ }
125
+
126
+ // Suggested max iterations: 2-3x estimated steps for safety margin
127
+ const suggestedMaxIterations = Math.min(Math.max(estimatedSteps * 3, 10), 100);
128
+ const tokenBudget = estimatedSteps * 800; // ~800 tokens per step estimate
129
+
130
+ return { complexity, estimatedSteps, suggestedMaxIterations, tokenBudget };
131
+ }
132
+
133
+ /**
134
+ * WorkflowPivotLoop - Main loop controller
135
+ */
136
+ export class WorkflowPivotLoop {
137
+ private config: Required<PivotLoopConfig>;
138
+ private state: PivotLoopState;
139
+ private tools: Map<string, Tool>;
140
+ private messageHistory: Array<{ role: string; content: string; toolCall?: { name: string; args: Record<string, string> }; toolResult?: ToolResult }>;
141
+ private streamCallback?: StreamCallback;
142
+
143
+ constructor(config: PivotLoopConfig) {
144
+ this.tools = new Map();
145
+
146
+ // Default configuration based on task complexity if not provided
147
+ const defaults: Required<PivotLoopConfig> = {
148
+ maxIterations: config.maxIterations || 50,
149
+ minIterations: config.minIterations || 2,
150
+ qualityThreshold: config.qualityThreshold || 0.7,
151
+ maxConsecutiveNoProgress: config.maxConsecutiveNoProgress || 5,
152
+ maxTokenBudget: config.maxTokenBudget || 50000,
153
+ complexity: config.complexity || 'moderate'
154
+ };
155
+
156
+ this.config = defaults;
157
+
158
+ this.state = this.createInitialState();
159
+ this.messageHistory = [];
160
+ }
161
+
162
+ private createInitialState(): PivotLoopState {
163
+ return {
164
+ iteration: 0,
165
+ totalTokens: 0,
166
+ toolCallsCount: 0,
167
+ consecutiveNoProgress: 0,
168
+ qualityScores: [],
169
+ pendingToolUses: [],
170
+ lastMeaningfulWork: 0
171
+ };
172
+ }
173
+
174
+ /**
175
+ * Register a tool for use in the loop
176
+ */
177
+ registerTool(tool: Tool): void {
178
+ this.tools.set(tool.name, tool);
179
+ }
180
+
181
+ /**
182
+ * Register multiple tools at once
183
+ */
184
+ registerTools(tools: Tool[]): void {
185
+ for (const tool of tools) {
186
+ this.registerTool(tool);
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Execute the pivot loop
192
+ */
193
+ async execute(
194
+ input: string,
195
+ llm: LLMInterface,
196
+ systemPrompt: string,
197
+ streamCallback?: StreamCallback
198
+ ): Promise<LoopResult> {
199
+ this.streamCallback = streamCallback;
200
+ this.state = this.createInitialState();
201
+ this.messageHistory = [{ role: 'user', content: input }];
202
+
203
+ // Analyze task complexity and adapt config
204
+ const taskProfile = analyzeTaskComplexity(input);
205
+ const effectiveConfig = this.adaptConfigForTask(taskProfile);
206
+
207
+ this.emit({
208
+ type: 'status',
209
+ content: `🔍 任务复杂度: ${taskProfile.complexity} (预估 ${taskProfile.estimatedSteps} 步)`,
210
+ tool: 'system'
211
+ });
212
+
213
+ this.emit({
214
+ type: 'status',
215
+ content: `⚙️ 动态配置: maxIterations=${effectiveConfig.maxIterations}, tokenBudget=${effectiveConfig.maxTokenBudget}`,
216
+ tool: 'system'
217
+ });
218
+
219
+ let response = '';
220
+
221
+ while (this.shouldContinue(effectiveConfig)) {
222
+ this.state.iteration++;
223
+
224
+ this.emit({
225
+ type: 'status',
226
+ content: `🔄 循环 ${this.state.iteration}/${effectiveConfig.maxIterations}`,
227
+ tool: 'loop'
228
+ });
229
+
230
+ // Build context for LLM
231
+ const context = this.buildContext();
232
+ const fullPrompt = `${systemPrompt}\n\n${context}`;
233
+
234
+ try {
235
+ // Call LLM
236
+ const llmResponse = await llm.chat(context, systemPrompt);
237
+ const reply = llmResponse.reply.trim();
238
+
239
+ this.emit({ type: 'token', content: reply.substring(0, 100) });
240
+
241
+ // Estimate token usage
242
+ this.state.totalTokens += this.estimateTokens(fullPrompt) + this.estimateTokens(reply);
243
+
244
+ // Check token budget
245
+ if (this.state.totalTokens > effectiveConfig.maxTokenBudget) {
246
+ this.emit({
247
+ type: 'error',
248
+ content: '⚠️ Token 预算超支,中断循环'
249
+ });
250
+ return this.createResult(false, response, 'token_budget_exceeded');
251
+ }
252
+
253
+ // Check if this is a final response (no tool calls)
254
+ const pendingTools = this.extractPendingToolUses(reply);
255
+
256
+ if (pendingTools.length === 0) {
257
+ // No pending tool uses - this is a normal completion
258
+ this.state.pendingToolUses = [];
259
+
260
+ // Evaluate quality before accepting
261
+ const quality = this.evaluateQuality(reply);
262
+ this.state.qualityScores.push(quality);
263
+
264
+ this.emit({
265
+ type: 'status',
266
+ content: `✅ 检测到最终回复 (质量: ${(quality * 10).toFixed(1)}/10)`,
267
+ tool: 'system'
268
+ });
269
+
270
+ // Check if quality threshold met
271
+ if (quality >= effectiveConfig.qualityThreshold) {
272
+ response = reply;
273
+ return this.createResult(true, reply, 'quality_threshold_met');
274
+ }
275
+
276
+ // Quality not met but no more tools to call
277
+ // Accept response if we've done minimum iterations
278
+ if (this.state.iteration >= effectiveConfig.minIterations) {
279
+ response = reply;
280
+ return this.createResult(true, reply, 'no_pending_tools');
281
+ }
282
+
283
+ // Too early, continue to see if we can improve
284
+ this.state.consecutiveNoProgress++;
285
+ this.emit({
286
+ type: 'status',
287
+ content: `📊 质量未达标 (${(quality * 10).toFixed(1)}/${(effectiveConfig.qualityThreshold * 10).toFixed(1)}),继续循环`,
288
+ tool: 'system'
289
+ });
290
+ continue;
291
+ }
292
+
293
+ // We have pending tool uses - execute them
294
+ this.state.pendingToolUses = pendingTools;
295
+ this.state.lastMeaningfulWork = this.state.iteration;
296
+ this.state.consecutiveNoProgress = 0;
297
+
298
+ for (const toolCall of pendingTools) {
299
+ this.state.toolCallsCount++;
300
+
301
+ const tool = this.tools.get(toolCall.name);
302
+ if (!tool) {
303
+ this.emit({
304
+ type: 'error',
305
+ content: `❌ 未知工具: ${toolCall.name}`
306
+ });
307
+ this.messageHistory.push({
308
+ role: 'tool',
309
+ content: JSON.stringify({ success: false, error: `Unknown tool: ${toolCall.name}` })
310
+ });
311
+ continue;
312
+ }
313
+
314
+ this.emit({
315
+ type: 'tool',
316
+ content: `🔧 执行: ${toolCall.name}`,
317
+ tool: toolCall.name
318
+ });
319
+
320
+ try {
321
+ const result = await tool.execute(toolCall.args);
322
+
323
+ this.emit({
324
+ type: result.success ? 'status' : 'error',
325
+ content: result.success
326
+ ? `✅ ${toolCall.name} 成功`
327
+ : `❌ ${toolCall.name} 失败: ${result.error}`
328
+ });
329
+
330
+ this.messageHistory.push({
331
+ role: 'assistant',
332
+ content: reply,
333
+ toolCall,
334
+ toolResult: result
335
+ });
336
+
337
+ // Record quality from tool result
338
+ const toolQuality = this.evaluateToolResult(result);
339
+ this.state.qualityScores.push(toolQuality);
340
+
341
+ } catch (execError) {
342
+ this.emit({
343
+ type: 'error',
344
+ content: `❌ 工具执行异常: ${execError}`
345
+ });
346
+ this.messageHistory.push({
347
+ role: 'tool',
348
+ content: JSON.stringify({ success: false, error: String(execError) })
349
+ });
350
+ }
351
+ }
352
+
353
+ } catch (error) {
354
+ this.emit({
355
+ type: 'error',
356
+ content: `❌ 循环异常: ${error}`
357
+ });
358
+ return this.createResult(false, response, 'error');
359
+ }
360
+ }
361
+
362
+ // Loop exited - determine reason
363
+ const exitReason = this.determineExitReason(effectiveConfig);
364
+
365
+ if (!response && this.messageHistory.length > 0) {
366
+ const lastAssistant = this.messageHistory
367
+ .filter(m => m.role === 'assistant')
368
+ .pop();
369
+ response = lastAssistant?.content || '任务处理超时';
370
+ }
371
+
372
+ return this.createResult(
373
+ exitReason !== 'error',
374
+ response,
375
+ exitReason
376
+ );
377
+ }
378
+
379
+ /**
380
+ * Determine if loop should continue
381
+ */
382
+ private shouldContinue(config: Required<PivotLoopConfig>): boolean {
383
+ // Hard stop: max iterations reached
384
+ if (this.state.iteration >= config.maxIterations) {
385
+ this.emit({
386
+ type: 'status',
387
+ content: `🛑 达到最大迭代次数 ${config.maxIterations}`
388
+ });
389
+ return false;
390
+ }
391
+
392
+ // Soft stop: consecutive no progress
393
+ if (this.state.consecutiveNoProgress >= config.maxConsecutiveNoProgress) {
394
+ this.emit({
395
+ type: 'status',
396
+ content: `🛑 连续 ${config.maxConsecutiveNoProgress} 次无进展`
397
+ });
398
+ return false;
399
+ }
400
+
401
+ return true;
402
+ }
403
+
404
+ /**
405
+ * Adapt configuration based on task profile
406
+ */
407
+ private adaptConfigForTask(profile: TaskProfile): Required<PivotLoopConfig> {
408
+ return {
409
+ ...this.config,
410
+ maxIterations: Math.min(this.config.maxIterations, profile.suggestedMaxIterations),
411
+ maxTokenBudget: Math.min(this.config.maxTokenBudget, profile.tokenBudget),
412
+ complexity: profile.complexity
413
+ };
414
+ }
415
+
416
+ /**
417
+ * Extract pending tool uses from LLM response
418
+ */
419
+ private extractPendingToolUses(content: string): Array<{ name: string; args: Record<string, string> }> {
420
+ const pending: Array<{ name: string; args: Record<string, string> }> = [];
421
+
422
+ // Pattern 1: Chinese format "调用工具: tool_name(args)"
423
+ const pattern1 = /调用工具[::]\s*(\w+)\s*\(([^)]*)\)/g;
424
+ let match;
425
+ while ((match = pattern1.exec(content)) !== null) {
426
+ const name = match[1];
427
+ const argsStr = match[2];
428
+ const args = this.parseArgs(argsStr);
429
+ if (this.tools.has(name)) {
430
+ pending.push({ name, args });
431
+ }
432
+ }
433
+
434
+ // Pattern 2: tool_name(args) format
435
+ const pattern2 = /(\w+)\s*\(\s*([^)]*)\s*\)/g;
436
+ while ((match = pattern2.exec(content)) !== null) {
437
+ const name = match[1];
438
+ const argsStr = match[2];
439
+ // Skip if already matched or doesn't look like a tool call
440
+ if (pending.some(p => p.name === name)) continue;
441
+ if (!this.tools.has(name)) continue;
442
+
443
+ const args = this.parseArgs(argsStr);
444
+ pending.push({ name, args });
445
+ }
446
+
447
+ // Pattern 3: JSON format tool calls
448
+ try {
449
+ const jsonMatch = content.match(/\{[\s\S]*"tool_calls"[\s\S]*\}/);
450
+ if (jsonMatch) {
451
+ const parsed = JSON.parse(jsonMatch[0]);
452
+ if (Array.isArray(parsed.tool_calls)) {
453
+ for (const tc of parsed.tool_calls) {
454
+ if (this.tools.has(tc.name)) {
455
+ pending.push({ name: tc.name, args: tc.args || {} });
456
+ }
457
+ }
458
+ }
459
+ }
460
+ } catch {
461
+ // JSON parsing failed, ignore
462
+ }
463
+
464
+ return pending;
465
+ }
466
+
467
+ /**
468
+ * Parse tool arguments from string
469
+ */
470
+ private parseArgs(argsStr: string): Record<string, string> {
471
+ const args: Record<string, string> = {};
472
+ if (!argsStr || !argsStr.trim()) return args;
473
+
474
+ const pairs = argsStr.split(',').map(s => s.trim()).filter(Boolean);
475
+ for (const pair of pairs) {
476
+ const colonIdx = pair.indexOf(':');
477
+ if (colonIdx > 0) {
478
+ const key = pair.substring(0, colonIdx).trim();
479
+ const value = pair.substring(colonIdx + 1).trim().replace(/^['"]|['"]$/g, '');
480
+ args[key] = value;
481
+ } else {
482
+ // No colon, try to parse as positional
483
+ const parts = pair.split(/\s+/);
484
+ if (parts.length >= 2) {
485
+ args[parts[0]] = parts.slice(1).join(' ');
486
+ }
487
+ }
488
+ }
489
+ return args;
490
+ }
491
+
492
+ /**
493
+ * Build context from message history
494
+ */
495
+ private buildContext(): string {
496
+ return this.messageHistory.map(m => {
497
+ if (m.role === 'user') return `用户: ${m.content}`;
498
+ if (m.role === 'assistant') return `助手: ${m.content}`;
499
+ if (m.role === 'tool' && m.toolResult) {
500
+ return `工具结果: ${JSON.stringify(m.toolResult)}`;
501
+ }
502
+ return '';
503
+ }).filter(Boolean).join('\n');
504
+ }
505
+
506
+ /**
507
+ * Evaluate response quality
508
+ */
509
+ private evaluateQuality(response: string): number {
510
+ let score = 0.5;
511
+
512
+ // Length-based scoring
513
+ if (response.length > 100) score += 0.1;
514
+ if (response.length > 500) score += 0.1;
515
+ if (response.length < 30) score -= 0.2;
516
+
517
+ // Structure indicators
518
+ if (response.includes('\n')) score += 0.05;
519
+ if (response.includes('-') || response.includes('•')) score += 0.05;
520
+ if (response.includes('```')) score += 0.1;
521
+
522
+ // Content quality indicators
523
+ const conclusionWords = ['完成', '结果', '总结', '所以', '因此', '答案', '推荐', '建议'];
524
+ if (conclusionWords.some(w => response.includes(w))) score += 0.1;
525
+
526
+ // Negative indicators
527
+ if (response.includes('调用工具') || response.includes('tool(')) score -= 0.15;
528
+ if (response.includes('??') || response.includes('未知')) score -= 0.1;
529
+
530
+ return Math.max(0, Math.min(1, score));
531
+ }
532
+
533
+ /**
534
+ * Evaluate tool result quality
535
+ */
536
+ private evaluateToolResult(result: ToolResult): number {
537
+ if (!result.success) return 0.2;
538
+
539
+ let score = 0.6;
540
+ if (result.output) {
541
+ score += 0.2;
542
+ if (result.output.length > 100) score += 0.1;
543
+ if (result.output.includes('error') || result.output.includes('❌')) score -= 0.2;
544
+ if (result.output.includes('success') || result.output.includes('✅')) score += 0.1;
545
+ }
546
+ if (result.error) score -= 0.3;
547
+
548
+ return Math.max(0, Math.min(1, score));
549
+ }
550
+
551
+ /**
552
+ * Estimate token count (rough approximation)
553
+ */
554
+ private estimateTokens(text: string): number {
555
+ // Rough estimate: ~4 characters per token for Chinese/English mix
556
+ return Math.ceil(text.length / 4);
557
+ }
558
+
559
+ /**
560
+ * Create result object
561
+ */
562
+ private createResult(success: boolean, response: string, exitReason: ExitReason): LoopResult {
563
+ const avgQuality = this.state.qualityScores.length > 0
564
+ ? this.state.qualityScores.reduce((a, b) => a + b, 0) / this.state.qualityScores.length
565
+ : 0;
566
+
567
+ return {
568
+ success,
569
+ response,
570
+ iterations: this.state.iteration,
571
+ toolCalls: this.state.toolCallsCount,
572
+ qualityScore: avgQuality,
573
+ exitReason,
574
+ state: { ...this.state }
575
+ };
576
+ }
577
+
578
+ /**
579
+ * Determine why loop exited
580
+ */
581
+ private determineExitReason(config: Required<PivotLoopConfig>): ExitReason {
582
+ if (this.state.iteration >= config.maxIterations) {
583
+ return 'max_iterations';
584
+ }
585
+ if (this.state.consecutiveNoProgress >= config.maxConsecutiveNoProgress) {
586
+ return 'no_progress_exhausted';
587
+ }
588
+ if (this.state.pendingToolUses.length === 0 && this.state.iteration >= config.minIterations) {
589
+ return 'no_pending_tools';
590
+ }
591
+ if (this.state.totalTokens > config.maxTokenBudget) {
592
+ return 'token_budget_exceeded';
593
+ }
594
+ return 'max_iterations';
595
+ }
596
+
597
+ /**
598
+ * Emit stream event
599
+ */
600
+ private emit(event: StreamEvent): void {
601
+ if (this.streamCallback) {
602
+ this.streamCallback(event);
603
+ }
604
+ }
605
+
606
+ /**
607
+ * Get current state
608
+ */
609
+ getState(): PivotLoopState {
610
+ return { ...this.state };
611
+ }
612
+
613
+ /**
614
+ * Reset the loop state
615
+ */
616
+ reset(): void {
617
+ this.state = this.createInitialState();
618
+ this.messageHistory = [];
619
+ }
620
+ }
621
+
622
+ /**
623
+ * Interface for LLM chat capability
624
+ */
625
+ export interface LLMInterface {
626
+ chat(context: string, systemPrompt: string): Promise<{ reply: string; tokens?: number }>;
627
+ }
628
+
629
+ /**
630
+ * Factory to create a default pivot loop configuration
631
+ */
632
+ export function createDefaultPivotConfig(complexity?: TaskComplexity): PivotLoopConfig {
633
+ const profiles: Record<TaskComplexity, PivotLoopConfig> = {
634
+ simple: {
635
+ maxIterations: 15,
636
+ minIterations: 1,
637
+ qualityThreshold: 0.6,
638
+ maxConsecutiveNoProgress: 3,
639
+ maxTokenBudget: 10000
640
+ },
641
+ moderate: {
642
+ maxIterations: 30,
643
+ minIterations: 2,
644
+ qualityThreshold: 0.7,
645
+ maxConsecutiveNoProgress: 5,
646
+ maxTokenBudget: 30000
647
+ },
648
+ complex: {
649
+ maxIterations: 60,
650
+ minIterations: 3,
651
+ qualityThreshold: 0.75,
652
+ maxConsecutiveNoProgress: 8,
653
+ maxTokenBudget: 60000
654
+ }
655
+ };
656
+
657
+ return complexity ? profiles[complexity] : profiles.moderate;
658
+ }
659
+
660
+ /**
661
+ * Helper to run a simple prompt through the loop
662
+ */
663
+ export async function runPivotLoop(
664
+ input: string,
665
+ llm: LLMInterface,
666
+ tools: Tool[],
667
+ systemPrompt: string,
668
+ config?: PivotLoopConfig,
669
+ streamCallback?: StreamCallback
670
+ ): Promise<LoopResult> {
671
+ const loop = new WorkflowPivotLoop(config || createDefaultPivotConfig());
672
+ loop.registerTools(tools);
673
+ return loop.execute(input, llm, systemPrompt, streamCallback);
674
+ }