@bolloon/bolloon-agent 0.3.44 → 0.3.45

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 (214) hide show
  1. package/README.md +135 -135
  2. package/bin/bolloon-cli.cjs +183 -183
  3. package/bin/bolloon-daemon.sh +207 -0
  4. package/bin/bolloon.cjs +0 -0
  5. package/dist/agents/constraint-layer.js +19 -19
  6. package/dist/agents/judgment-protocol.js +14 -14
  7. package/dist/agents/pi-sdk-types.js +12 -12
  8. package/dist/agents/pi-sdk.js +148 -89
  9. package/dist/agents/skill-writer.js +19 -0
  10. package/dist/agents/workflow-pivot-loop.js +2 -2
  11. package/dist/bollharness/src/scripts/deploy-guard.js +8 -8
  12. package/dist/bollharness-integration/context-router-judgment.js +4 -4
  13. package/dist/bollharness-integration/context-router.js +292 -292
  14. package/dist/bollharness-integration/gate-state-machine.js +13 -13
  15. package/dist/bollharness-integration/llm-judgment-engine.js +134 -134
  16. package/dist/bollharness-integration/skill-adapter.js +21 -21
  17. package/dist/bootstrap/chat-archiver.js +14 -14
  18. package/dist/bootstrap/context-os.js +21 -21
  19. package/dist/bootstrap/memory-compressor.js +29 -13
  20. package/dist/cli-entry.js +36 -36
  21. package/dist/constraint-runtime/reference_data/archive_surface_snapshot.json +63 -0
  22. package/dist/constraint-runtime/reference_data/commands_snapshot.json +1037 -0
  23. package/dist/constraint-runtime/reference_data/subsystems/assistant.json +8 -0
  24. package/dist/constraint-runtime/reference_data/subsystems/bootstrap.json +8 -0
  25. package/dist/constraint-runtime/reference_data/subsystems/bridge.json +32 -0
  26. package/dist/constraint-runtime/reference_data/subsystems/buddy.json +13 -0
  27. package/dist/constraint-runtime/reference_data/subsystems/cli.json +26 -0
  28. package/dist/constraint-runtime/reference_data/subsystems/components.json +32 -0
  29. package/dist/constraint-runtime/reference_data/subsystems/constants.json +28 -0
  30. package/dist/constraint-runtime/reference_data/subsystems/coordinator.json +8 -0
  31. package/dist/constraint-runtime/reference_data/subsystems/entrypoints.json +15 -0
  32. package/dist/constraint-runtime/reference_data/subsystems/hooks.json +32 -0
  33. package/dist/constraint-runtime/reference_data/subsystems/keybindings.json +21 -0
  34. package/dist/constraint-runtime/reference_data/subsystems/memdir.json +15 -0
  35. package/dist/constraint-runtime/reference_data/subsystems/migrations.json +18 -0
  36. package/dist/constraint-runtime/reference_data/subsystems/moreright.json +8 -0
  37. package/dist/constraint-runtime/reference_data/subsystems/native_ts.json +11 -0
  38. package/dist/constraint-runtime/reference_data/subsystems/outputStyles.json +8 -0
  39. package/dist/constraint-runtime/reference_data/subsystems/plugins.json +9 -0
  40. package/dist/constraint-runtime/reference_data/subsystems/remote.json +11 -0
  41. package/dist/constraint-runtime/reference_data/subsystems/schemas.json +8 -0
  42. package/dist/constraint-runtime/reference_data/subsystems/screens.json +10 -0
  43. package/dist/constraint-runtime/reference_data/subsystems/server.json +10 -0
  44. package/dist/constraint-runtime/reference_data/subsystems/services.json +32 -0
  45. package/dist/constraint-runtime/reference_data/subsystems/skills.json +27 -0
  46. package/dist/constraint-runtime/reference_data/subsystems/state.json +13 -0
  47. package/dist/constraint-runtime/reference_data/subsystems/types.json +18 -0
  48. package/dist/constraint-runtime/reference_data/subsystems/upstreamproxy.json +9 -0
  49. package/dist/constraint-runtime/reference_data/subsystems/utils.json +32 -0
  50. package/dist/constraint-runtime/reference_data/subsystems/vim.json +12 -0
  51. package/dist/constraint-runtime/reference_data/subsystems/voice.json +8 -0
  52. package/dist/constraint-runtime/reference_data/tools_snapshot.json +922 -0
  53. package/dist/constraint-runtime/setup.js +1 -1
  54. package/dist/constraint-runtime/tools/PolymarketSDK/cancelOrder.d.ts +13 -3
  55. package/dist/constraint-runtime/tools/PolymarketSDK/cancelOrder.js +16 -4
  56. package/dist/constraint-runtime/tools/PolymarketSDK/clobShared.d.ts +50 -0
  57. package/dist/constraint-runtime/tools/PolymarketSDK/clobShared.js +78 -0
  58. package/dist/constraint-runtime/tools/PolymarketSDK/createOrder.d.ts +24 -3
  59. package/dist/constraint-runtime/tools/PolymarketSDK/createOrder.js +31 -4
  60. package/dist/constraint-runtime/tools/PolymarketSDK/getMarket.d.ts +1 -0
  61. package/dist/constraint-runtime/tools/PolymarketSDK/getMarket.js +14 -3
  62. package/dist/constraint-runtime/tools/PolymarketSDK/getOrders.d.ts +11 -2
  63. package/dist/constraint-runtime/tools/PolymarketSDK/getOrders.js +23 -4
  64. package/dist/constraint-runtime/tools/PolymarketSDK/listMarkets.d.ts +4 -0
  65. package/dist/constraint-runtime/tools/PolymarketSDK/listMarkets.js +22 -2
  66. package/dist/context-compaction/auto-compact.js +7 -7
  67. package/dist/electron/config.js.map +1 -0
  68. package/dist/electron/dialogs.js.map +1 -0
  69. package/dist/electron/first-run.js +54 -54
  70. package/dist/electron/first-run.js.map +1 -0
  71. package/dist/electron/ipc.js.map +1 -0
  72. package/dist/electron/logger.js.map +1 -0
  73. package/dist/electron/main.js.map +1 -0
  74. package/dist/electron/menu.js.map +1 -0
  75. package/dist/electron/paths.js.map +1 -0
  76. package/dist/electron/server.js.map +1 -0
  77. package/dist/electron/tray.js.map +1 -0
  78. package/dist/electron/window.js.map +1 -0
  79. package/dist/electron-build/electron/first-run.js +54 -54
  80. package/dist/electron-preload.js.map +1 -1
  81. package/dist/electron.js.map +1 -1
  82. package/dist/index.js +113 -113
  83. package/dist/llm/llm-judgment-client.js +102 -102
  84. package/dist/llm/pi-ai.js +48 -48
  85. package/dist/llm/system-prompt/layers/channel/human-async.md +41 -41
  86. package/dist/llm/system-prompt/layers/channel/local.md +14 -14
  87. package/dist/llm/system-prompt/layers/channel/p2p-agent.md +18 -18
  88. package/dist/llm/system-prompt/layers/channel/p2p-peer-sync.md +51 -51
  89. package/dist/llm/system-prompt/layers/channel/p2p-proactive.md +43 -43
  90. package/dist/llm/system-prompt/layers/channel/p2p-visitor.md +19 -19
  91. package/dist/llm/system-prompt/layers/channel/session-handoff.md +61 -61
  92. package/dist/llm/system-prompt/layers/core/artifacts_storage.md +89 -89
  93. package/dist/llm/system-prompt/layers/core/evenhandedness.md +21 -21
  94. package/dist/llm/system-prompt/layers/core/external-engagement.md +73 -73
  95. package/dist/llm/system-prompt/layers/core/hibs_reminders.md +15 -15
  96. package/dist/llm/system-prompt/layers/core/identity.md +48 -48
  97. package/dist/llm/system-prompt/layers/core/knowledge.md +17 -17
  98. package/dist/llm/system-prompt/layers/core/memory_system.md +12 -12
  99. package/dist/llm/system-prompt/layers/core/network_filesystem.md +28 -28
  100. package/dist/llm/system-prompt/layers/core/refusal.md +37 -37
  101. package/dist/llm/system-prompt/layers/core/tone.md +31 -31
  102. package/dist/llm/system-prompt/layers/core/tools.thin.md +13 -13
  103. package/dist/llm/system-prompt/layers/core/wellbeing.md +41 -41
  104. package/dist/llm/system-prompt/layers/role/architect.md +20 -20
  105. package/dist/llm/system-prompt/layers/role/expert.md +19 -19
  106. package/dist/llm/system-prompt/layers/role/implementer.md +15 -15
  107. package/dist/llm/system-prompt/layers/role/security.md +15 -15
  108. package/dist/llm/system-prompt/layers/tool/artifacts.md +72 -72
  109. package/dist/llm/system-prompt/layers/tool/bash.md +25 -25
  110. package/dist/llm/system-prompt/layers/tool/goal_handoff.md +77 -77
  111. package/dist/llm/system-prompt/layers/tool/hibs_api.md +171 -171
  112. package/dist/llm/system-prompt/layers/tool/image_search.md +70 -70
  113. package/dist/llm/system-prompt/layers/tool/manifest.md +89 -89
  114. package/dist/llm/system-prompt/layers/tool/mcp_apps.md +53 -53
  115. package/dist/llm/system-prompt/layers/tool/p2p_request.md +61 -61
  116. package/dist/llm/system-prompt/layers/tool/web_search.md +83 -83
  117. package/dist/llm/tool-manifest/ask_user_input.js +1 -1
  118. package/dist/llm/tool-manifest/bash.js +3 -3
  119. package/dist/llm/tool-manifest/create_file.js +4 -4
  120. package/dist/llm/tool-manifest/fetch_sports_data.js +3 -3
  121. package/dist/llm/tool-manifest/image_search.js +2 -2
  122. package/dist/llm/tool-manifest/index.js +59 -0
  123. package/dist/llm/tool-manifest/mcp.js +2 -2
  124. package/dist/llm/tool-manifest/message_compose.js +3 -3
  125. package/dist/llm/tool-manifest/places.js +2 -2
  126. package/dist/llm/tool-manifest/present_files.js +1 -1
  127. package/dist/llm/tool-manifest/recipe.js +2 -2
  128. package/dist/llm/tool-manifest/str_replace.js +5 -5
  129. package/dist/llm/tool-manifest/view.js +3 -3
  130. package/dist/llm/tool-manifest/weather.js +4 -4
  131. package/dist/llm/tool-manifest/web.js +4 -4
  132. package/dist/orbitdb/cid-database.js +41 -0
  133. package/dist/orbitdb/did-catalog-replication.js +199 -0
  134. package/dist/orbitdb/trajectory-store.js +150 -0
  135. package/dist/pi-ecosystem-judgment/causal-judge.js +51 -51
  136. package/dist/pi-ecosystem-judgment/distill-prompt.js +42 -42
  137. package/dist/pi-ecosystem-judgment/distillation.js +14 -14
  138. package/dist/pi-ecosystem-judgment/monitor-gate.js +28 -28
  139. package/dist/pi-ecosystem-judgment/value-injection.js +102 -102
  140. package/dist/security/context-router-tool.js +15 -15
  141. package/dist/storage/did-catalog-bridge.js +271 -0
  142. package/dist/utils/auto-update.js.map +1 -0
  143. package/dist/web/api-config.html +779 -779
  144. package/dist/web/client-hearth.js +10 -10
  145. package/dist/web/client.js.map +7 -0
  146. package/dist/web/components/wallet-viem.mjs +118 -118
  147. package/dist/web/index.html +424 -424
  148. package/dist/web/manifest.json +20 -20
  149. package/dist/web/server.js +165 -13
  150. package/dist/web/style.css +4914 -4914
  151. package/package.json +193 -193
  152. package/scripts/build-cli.js +215 -215
  153. package/scripts/build-web.ts +130 -130
  154. package/scripts/postinstall.js +152 -152
  155. package/bin/bolloon.js +0 -157
  156. package/dist/pi-ecosystem-colony/index.js +0 -365
  157. package/dist/social/ant-colony/AdaptiveHeartbeat.js +0 -101
  158. package/dist/social/ant-colony/PheromoneEngine.js +0 -227
  159. package/dist/social/ant-colony/index.js +0 -6
  160. package/dist/social/ant-colony/types.js +0 -24
  161. package/dist/test/ai-judgment-test.js +0 -80
  162. package/dist/test/bollharness-integration.test.js +0 -318
  163. package/dist/test/channel-agent-multi-dialogue.js +0 -205
  164. package/dist/test/channel-heartbeat-agent-test.js +0 -201
  165. package/dist/test/constraint-layer.test.js +0 -164
  166. package/dist/test/diap-identity-test.js +0 -172
  167. package/dist/test/diap-quick-test.js +0 -62
  168. package/dist/test/global-shared-context.test.js +0 -315
  169. package/dist/test/harness-judgment-injection.test.js +0 -246
  170. package/dist/test/harness-workflow-integrator-test.js +0 -228
  171. package/dist/test/human-value-store.test.js +0 -243
  172. package/dist/test/hybrid-integration-test.js +0 -118
  173. package/dist/test/hybrid-messenger-verify.js +0 -55
  174. package/dist/test/iroh-bistream-debug.js +0 -38
  175. package/dist/test/iroh-communication.test.js +0 -66
  176. package/dist/test/iroh-debug-test.js +0 -57
  177. package/dist/test/iroh-diap-test.js +0 -71
  178. package/dist/test/iroh-direct-connect.js +0 -55
  179. package/dist/test/iroh-e2e-fixed.js +0 -89
  180. package/dist/test/iroh-e2e-same-process.js +0 -63
  181. package/dist/test/iroh-e2e.js +0 -66
  182. package/dist/test/iroh-final-e2e.js +0 -72
  183. package/dist/test/iroh-relay-test.js +0 -37
  184. package/dist/test/iroh-simple-test.js +0 -41
  185. package/dist/test/iroh-transport-verify.js +0 -54
  186. package/dist/test/iroh-transport.test.js +0 -37
  187. package/dist/test/iroh-two-nodes.js +0 -70
  188. package/dist/test/iroh-verify.js +0 -44
  189. package/dist/test/judgment-decision.test.js +0 -219
  190. package/dist/test/llm-judgment-integration.test.js +0 -220
  191. package/dist/test/p2p-agent-complex-dialogue.js +0 -385
  192. package/dist/test/p2p-agent-dialogue.js +0 -341
  193. package/dist/test/p2p-agent-full-bidirectional.js +0 -510
  194. package/dist/test/p2p-agent-harness-flow.js +0 -437
  195. package/dist/test/p2p-agent-harness-single.js +0 -143
  196. package/dist/test/p2p-ai-dialogue-test.js +0 -318
  197. package/dist/test/p2p-cid-connect-test.js +0 -195
  198. package/dist/test/p2p-connect-receiver.js +0 -69
  199. package/dist/test/p2p-doc-transfer.js +0 -110
  200. package/dist/test/p2p-identity-page-test.js +0 -77
  201. package/dist/test/p2p-iroh-test.js +0 -171
  202. package/dist/test/p2p-minimal-test.js +0 -241
  203. package/dist/test/p2p-node-1.js +0 -148
  204. package/dist/test/p2p-node-2.js +0 -148
  205. package/dist/test/p2p-server.js +0 -281
  206. package/dist/test/p2p-two-nodes-test.js +0 -438
  207. package/dist/test/pi-sdk.test.js +0 -44
  208. package/dist/test/set-persona.js +0 -40
  209. package/dist/test/simple.test.js +0 -9
  210. package/dist/test/storage-integration.test.js +0 -150
  211. package/dist/test/subagent-manager.test.js +0 -276
  212. package/dist/test/test-gate-flow.test.js +0 -81
  213. package/dist/test/workflow-engine.test.js +0 -87
  214. package/dist/test/workflow-pivot-loop.test.js +0 -246
@@ -0,0 +1,207 @@
1
+ #!/bin/bash
2
+ # Bolloon Daemon - 守护进程启动脚本
3
+ # 支持自动重启、日志轮转、PID 管理
4
+
5
+ set -e
6
+
7
+ APP_NAME="bolloon"
8
+ APP_DIR="$(cd "$(dirname "$0")/.." && pwd)"
9
+ PID_FILE="$HOME/.bolloon/daemon.pid"
10
+ LOG_FILE="$HOME/.bolloon/daemon.log"
11
+ MAX_RESTARTS=5
12
+ RESTART_DELAY=5
13
+
14
+ # 颜色输出
15
+ RED='\033[0;31m'
16
+ GREEN='\033[0;32m'
17
+ YELLOW='\033[1;33m'
18
+ NC='\033[0m' # No Color
19
+
20
+ log() {
21
+ echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1"
22
+ }
23
+
24
+ warn() {
25
+ echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] WARN:${NC} $1"
26
+ }
27
+
28
+ error() {
29
+ echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1"
30
+ }
31
+
32
+ # 获取 PID
33
+ get_pid() {
34
+ if [ -f "$PID_FILE" ]; then
35
+ cat "$PID_FILE"
36
+ fi
37
+ }
38
+
39
+ # 检查进程是否运行
40
+ is_running() {
41
+ local pid=$(get_pid)
42
+ if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
43
+ return 0
44
+ fi
45
+ return 1
46
+ }
47
+
48
+ # 启动守护进程
49
+ start() {
50
+ if is_running; then
51
+ log "Bolloon 已在运行 (PID: $(get_pid))"
52
+ return 0
53
+ fi
54
+
55
+ log "启动 Bolloon 守护进程..."
56
+
57
+ # 确保日志目录存在
58
+ mkdir -p "$(dirname "$LOG_FILE")"
59
+
60
+ # 启动进程
61
+ cd "$APP_DIR"
62
+ nohup npx tsx src/index.ts >> "$LOG_FILE" 2>&1 &
63
+ local pid=$!
64
+
65
+ # 保存 PID
66
+ echo "$pid" > "$PID_FILE"
67
+ log "Bolloon 已启动 (PID: $pid)"
68
+ log "日志文件: $LOG_FILE"
69
+ }
70
+
71
+ # 停止守护进程
72
+ stop() {
73
+ local pid=$(get_pid)
74
+
75
+ if [ -z "$pid" ]; then
76
+ warn "Bolloon 未运行"
77
+ return 0
78
+ fi
79
+
80
+ log "停止 Bolloon (PID: $pid)..."
81
+
82
+ # 发送 SIGTERM
83
+ kill -TERM "$pid" 2>/dev/null || true
84
+
85
+ # 等待进程退出
86
+ local count=0
87
+ while kill -0 "$pid" 2>/dev/null && [ $count -lt 10 ]; do
88
+ sleep 1
89
+ count=$((count + 1))
90
+ done
91
+
92
+ # 如果还在运行,强制终止
93
+ if kill -0 "$pid" 2>/dev/null; then
94
+ warn "进程未响应,发送 SIGKILL..."
95
+ kill -KILL "$pid" 2>/dev/null || true
96
+ fi
97
+
98
+ # 清理 PID 文件
99
+ rm -f "$PID_FILE"
100
+ log "Bolloon 已停止"
101
+ }
102
+
103
+ # 重启守护进程
104
+ restart() {
105
+ log "重启 Bolloon..."
106
+ stop
107
+ sleep 2
108
+ start
109
+ }
110
+
111
+ # 查看状态
112
+ status() {
113
+ if is_running; then
114
+ log "Bolloon 运行中 (PID: $(get_pid))"
115
+ # 显示资源使用
116
+ local pid=$(get_pid)
117
+ if [ -n "$pid" ]; then
118
+ ps -p "$pid" -o %cpu,%mem,etime 2>/dev/null | tail -1 || true
119
+ fi
120
+ else
121
+ warn "Bolloon 未运行"
122
+ fi
123
+ }
124
+
125
+ # 查看日志
126
+ logs() {
127
+ if [ -f "$LOG_FILE" ]; then
128
+ tail -100 "$LOG_FILE"
129
+ else
130
+ warn "日志文件不存在: $LOG_FILE"
131
+ fi
132
+ }
133
+
134
+ # 健康检查
135
+ health() {
136
+ local port=${PORT:-54188}
137
+ local response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:$port/api/health" 2>/dev/null || echo "000")
138
+
139
+ if [ "$response" = "200" ]; then
140
+ log "健康检查通过"
141
+ curl -s "http://localhost:$port/api/health" 2>/dev/null | head -5
142
+ else
143
+ error "健康检查失败 (HTTP $response)"
144
+ return 1
145
+ fi
146
+ }
147
+
148
+ # 清理日志
149
+ clean_logs() {
150
+ if [ -f "$LOG_FILE" ]; then
151
+ local size=$(du -h "$LOG_FILE" | cut -f1)
152
+ log "当前日志大小: $size"
153
+ > "$LOG_FILE"
154
+ log "日志已清空"
155
+ fi
156
+ }
157
+
158
+ # 显示帮助
159
+ show_help() {
160
+ echo "Bolloon Daemon 守护进程管理脚本"
161
+ echo ""
162
+ echo "用法: $0 [命令]"
163
+ echo ""
164
+ echo "命令:"
165
+ echo " start 启动 Bolloon 守护进程"
166
+ echo " stop 停止 Bolloon 守护进程"
167
+ echo " restart 重启 Bolloon 守护进程"
168
+ echo " status 查看运行状态"
169
+ echo " logs 查看最近日志"
170
+ echo " health 健康检查"
171
+ echo " clean 清空日志"
172
+ echo " help 显示帮助"
173
+ echo ""
174
+ }
175
+
176
+ # 主逻辑
177
+ case "${1:-help}" in
178
+ start)
179
+ start
180
+ ;;
181
+ stop)
182
+ stop
183
+ ;;
184
+ restart)
185
+ restart
186
+ ;;
187
+ status)
188
+ status
189
+ ;;
190
+ logs)
191
+ logs
192
+ ;;
193
+ health)
194
+ health
195
+ ;;
196
+ clean)
197
+ clean_logs
198
+ ;;
199
+ help|--help|-h)
200
+ show_help
201
+ ;;
202
+ *)
203
+ error "未知命令: $1"
204
+ show_help
205
+ exit 1
206
+ ;;
207
+ esac
package/bin/bolloon.cjs CHANGED
File without changes
@@ -7,25 +7,25 @@ import { ToolPermissionContext, BudgetTracker } from '@bolloon/constraint-runtim
7
7
  * System prompt for constraint layer
8
8
  * Defines boundaries and autonomous permissions
9
9
  */
10
- export const SYSTEM_PROMPT = `
11
- 你是一个文档处理Agent,在以下规则下运行:
12
-
13
- 【边界规则】
14
- 1. 只处理:txt, md, pdf, docx 格式文档
15
- 2. 不修改原始文件,只输出改进版本
16
- 3. 发送前必须记录操作日志
17
-
18
- 【自主权限】
19
- - ✅ 自主决定:摘要详细程度、chunk分块策略
20
- - ✅ 自主决定:重试次数(最多3次)
21
- - ✅ 自主决定:是否需要补充信息
22
- - ❌ 必须确认:首次向新对等节点发送文档
23
- - ❌ 必须确认:删除操作
24
-
25
- 【敏感操作拦截】
26
- if (操作 === '发送文档' && 对等节点不在已知列表) {
27
- 拦截 → 记录 → 等待确认
28
- }
10
+ export const SYSTEM_PROMPT = `
11
+ 你是一个文档处理Agent,在以下规则下运行:
12
+
13
+ 【边界规则】
14
+ 1. 只处理:txt, md, pdf, docx 格式文档
15
+ 2. 不修改原始文件,只输出改进版本
16
+ 3. 发送前必须记录操作日志
17
+
18
+ 【自主权限】
19
+ - ✅ 自主决定:摘要详细程度、chunk分块策略
20
+ - ✅ 自主决定:重试次数(最多3次)
21
+ - ✅ 自主决定:是否需要补充信息
22
+ - ❌ 必须确认:首次向新对等节点发送文档
23
+ - ❌ 必须确认:删除操作
24
+
25
+ 【敏感操作拦截】
26
+ if (操作 === '发送文档' && 对等节点不在已知列表) {
27
+ 拦截 → 记录 → 等待确认
28
+ }
29
29
  `;
30
30
  /**
31
31
  * Actions that can be executed autonomously without confirmation
@@ -148,20 +148,20 @@ export async function distillDissent(ask) {
148
148
  .slice(-10)
149
149
  .flatMap((j) => j.reasons || [])
150
150
  .slice(0, 20);
151
- const prompt = `你是用户的判断力代理. 用户发起了一个决定: "${ask.decision}"
152
- ${ask.context ? `\n补充: ${ask.context}` : ''}
153
-
154
- [用户价值画像] ${valueHint}
155
- [相关历史判断 (${relevant.length} 条)] ${relevant.slice(0, 3).map((j) => j.decision || '').join(' | ').slice(0, 300)}
156
- [历史理由样本] ${recentReasons.join(' | ').slice(0, 400) || '(无)'}
157
-
158
- 请基于上述判断力, 生成正好 3 个"反方观点" (用户应该考虑但可能忽略的风险/盲点).
159
- 要求:
160
- - 每个反方 1-2 句, 总长 < 80 字
161
- - 必须是这个特定决定的"反对声音", 不是通用建议
162
- - 第 1 个: 历史/经验类风险
163
- - 第 2 个: 关系/信任类风险
164
- - 第 3 个: 隐藏代价/时间类风险
151
+ const prompt = `你是用户的判断力代理. 用户发起了一个决定: "${ask.decision}"
152
+ ${ask.context ? `\n补充: ${ask.context}` : ''}
153
+
154
+ [用户价值画像] ${valueHint}
155
+ [相关历史判断 (${relevant.length} 条)] ${relevant.slice(0, 3).map((j) => j.decision || '').join(' | ').slice(0, 300)}
156
+ [历史理由样本] ${recentReasons.join(' | ').slice(0, 400) || '(无)'}
157
+
158
+ 请基于上述判断力, 生成正好 3 个"反方观点" (用户应该考虑但可能忽略的风险/盲点).
159
+ 要求:
160
+ - 每个反方 1-2 句, 总长 < 80 字
161
+ - 必须是这个特定决定的"反对声音", 不是通用建议
162
+ - 第 1 个: 历史/经验类风险
163
+ - 第 2 个: 关系/信任类风险
164
+ - 第 3 个: 隐藏代价/时间类风险
165
165
  - 用 JSON 数组返回: ["反方1", "反方2", "反方3"]`;
166
166
  // LLM 调用 (OpenAI 兼容, 复用 p2p-chat-tools 的方式)
167
167
  let dissents = [];
@@ -1,13 +1,13 @@
1
- export const TOOL_DEFINITIONS = `
2
- 可用工具:
3
- 1. read_document(path) - 读取文档内容,支持 .txt, .md, .pdf, .docx
4
- 2. summarize_document(path, context?) - 总结文档内容,可选提供上下文
5
- 3. improve_document(path, requirements) - 改进文档,需提供文件路径和改进要求
6
- 4. list_peers() - 列出已连接的对等节点
7
- 5. send_message(peer_id, message) - 向指定对等节点发送消息
8
- 6. broadcast_message(message) - 向所有对等节点广播消息
9
- 7. get_identity() - 获取当前智能体身份信息
10
- 8. set_persona(persona_json) - 更新智能体 persona,包含 name、description、personality、greeting 等
11
- 9. run_workflow(steps) - 执行预定义工作流
12
- 10. get_operation_logs() - 获取操作日志
1
+ export const TOOL_DEFINITIONS = `
2
+ 可用工具:
3
+ 1. read_document(path) - 读取文档内容,支持 .txt, .md, .pdf, .docx
4
+ 2. summarize_document(path, context?) - 总结文档内容,可选提供上下文
5
+ 3. improve_document(path, requirements) - 改进文档,需提供文件路径和改进要求
6
+ 4. list_peers() - 列出已连接的对等节点
7
+ 5. send_message(peer_id, message) - 向指定对等节点发送消息
8
+ 6. broadcast_message(message) - 向所有对等节点广播消息
9
+ 7. get_identity() - 获取当前智能体身份信息
10
+ 8. set_persona(persona_json) - 更新智能体 persona,包含 name、description、personality、greeting 等
11
+ 9. run_workflow(steps) - 执行预定义工作流
12
+ 10. get_operation_logs() - 获取操作日志
13
13
  `;
@@ -596,9 +596,57 @@ export class PiAgentSession {
596
596
  }
597
597
  catch { /* 非致命 */ }
598
598
  }
599
+ // ============================================================
600
+ // 2026-08-08: 运行轨迹采集 (trajectory) — 每轮运行 → 落盘 + OrbitDB, 失败静默
601
+ // ============================================================
602
+ async createTrajectoryRecorder(input, channelId) {
603
+ try {
604
+ const { TrajectoryRecorder } = await import('../orbitdb/trajectory-store.js');
605
+ const { resolveUserDid } = await import('../storage/did-catalog-bridge.js');
606
+ const did = await resolveUserDid();
607
+ const model = this.llmConfig?.model || this.model || '';
608
+ return new TrajectoryRecorder({
609
+ agentId: this.currentAgentId || 'default',
610
+ input,
611
+ channelId: channelId || this.currentChannelId,
612
+ did: did || undefined,
613
+ model: typeof model === 'string' && model ? model : undefined,
614
+ });
615
+ }
616
+ catch {
617
+ return null; // 轨迹采集失败静默 (增强层)
618
+ }
619
+ }
620
+ wrapTrajectoryStream(onStream, rec) {
621
+ return (ev) => {
622
+ try {
623
+ rec?.recordStep?.(ev);
624
+ }
625
+ catch { /* 记录失败不影响转发 */ }
626
+ onStream(ev);
627
+ };
628
+ }
629
+ finishTrajectory(rec, reply, status = 'ok') {
630
+ if (!rec)
631
+ return;
632
+ // fire-and-forget: 不阻塞回复返回
633
+ (async () => {
634
+ try {
635
+ const { recordTrajectory } = await import('../orbitdb/trajectory-store.js');
636
+ const run = rec.endRun(reply, status);
637
+ await recordTrajectory(run, {});
638
+ }
639
+ catch { /* 静默 */ }
640
+ })();
641
+ }
599
642
  async prompt(input, options) {
600
643
  this.minimaxAvailable = this.checkMinimax();
601
644
  this.currentChannelId = options?.channelId ?? this.currentChannelId;
645
+ // 2026-08-08: 运行轨迹采集 (落盘 + OrbitDB, 失败静默) — 包裹 onStream 收集步骤事件
646
+ const trajRec = await this.createTrajectoryRecorder(input, options?.channelId);
647
+ if (trajRec && options?.onStream) {
648
+ options = { ...options, onStream: this.wrapTrajectoryStream(options.onStream, trajRec) };
649
+ }
602
650
  this.messageHistory.push({
603
651
  role: 'user',
604
652
  content: input
@@ -607,6 +655,7 @@ export class PiAgentSession {
607
655
  const response = await this.handleFallback(input);
608
656
  this.messageHistory.push({ role: 'assistant', content: response });
609
657
  this.reportUsageToContextManager();
658
+ this.finishTrajectory(trajRec, response);
610
659
  return response;
611
660
  }
612
661
  // P0 注入门
@@ -638,6 +687,7 @@ export class PiAgentSession {
638
687
  if (this.usePivotLoop) {
639
688
  try {
640
689
  const lr = await this.promptWithPivotLoop(input, undefined, options?.channelId);
690
+ this.finishTrajectory(trajRec, lr.response || '');
641
691
  return lr.response || '';
642
692
  }
643
693
  finally {
@@ -653,6 +703,7 @@ export class PiAgentSession {
653
703
  try {
654
704
  // 2026-06-16: runReActLoop 现在返回 { reply, aiFailed, aiFailureReason } — 这里只需 reply 字符串
655
705
  const loopResult = await this.runReActLoop(this.currentOnStream ?? undefined, options?.signal);
706
+ this.finishTrajectory(trajRec, loopResult.reply, loopResult.aiFailed ? 'error' : 'ok');
656
707
  return loopResult.reply;
657
708
  }
658
709
  finally {
@@ -673,6 +724,10 @@ export class PiAgentSession {
673
724
  this.minimaxAvailable = this.checkMinimax();
674
725
  console.log(`[PiAgent.promptStream] minimaxAvailable=${this.minimaxAvailable}`);
675
726
  this.currentChannelId = channelId ?? this.currentChannelId;
727
+ // 2026-08-08: 运行轨迹采集 (落盘 + OrbitDB, 失败静默) — 包裹 onStream 收集步骤事件
728
+ const trajRec = await this.createTrajectoryRecorder(input, channelId);
729
+ if (trajRec)
730
+ onStream = this.wrapTrajectoryStream(onStream, trajRec);
676
731
  // 2026-06-18 (supervisor): web server 把 46K markedPrompt 喂过来
677
732
  // (【本轮用户请求】\n<text>\n【请求结束】\n\n<contextHint>).
678
733
  // 整个 input 走下游, pivot loop 之前拿 47K buildContext 当 user message 发出去,
@@ -693,6 +748,7 @@ export class PiAgentSession {
693
748
  this.messageHistory.push({ role: 'assistant', content: response });
694
749
  onStream({ type: 'done', content: '' });
695
750
  this.reportUsageToContextManager();
751
+ this.finishTrajectory(trajRec, response);
696
752
  return response;
697
753
  }
698
754
  // P0 注入门: 缓存 onStream + signal, computeJudgmentGate 用 currentOnStream 广播 phase
@@ -840,6 +896,7 @@ export class PiAgentSession {
840
896
  this.promptStartTime = 0;
841
897
  this.reportUsageToContextManager();
842
898
  }
899
+ this.finishTrajectory(trajRec, pivotResult);
843
900
  return pivotResult;
844
901
  }
845
902
  // 2026-06-16: loop 自动重试 — runReActLoop 内部遇到 [AI 服务调用失败] sentinel 时,
@@ -929,6 +986,8 @@ export class PiAgentSession {
929
986
  this.currentSignal = null;
930
987
  this.bootstrapAddition = '';
931
988
  this.promptStartTime = 0;
989
+ // 2026-08-08: 轨迹收尾 — 落盘 + OrbitDB (失败静默, 不阻塞回复)
990
+ this.finishTrajectory(trajRec, result);
932
991
  return result;
933
992
  }
934
993
  async promptWithPivotLoop(input, config, channelId) {
@@ -972,38 +1031,38 @@ export class PiAgentSession {
972
1031
  }
973
1032
  // M2.4: persona 缓存
974
1033
  if (!this.cachedPersonaSection && this.persona) {
975
- this.cachedPersonaSection = `
976
- 角色描述: ${this.persona.description || '无'}
977
- 性格特点: ${this.persona.personality || '无'}
978
- 问候语: ${this.persona.greeting || '无'}
1034
+ this.cachedPersonaSection = `
1035
+ 角色描述: ${this.persona.description || '无'}
1036
+ 性格特点: ${this.persona.personality || '无'}
1037
+ 问候语: ${this.persona.greeting || '无'}
979
1038
  `;
980
1039
  }
981
- const systemPrompt = `${this.bootstrapAddition}你是 ${this.identity.name},基于ReAct (Reasoning + Acting)模式工作。${this.cachedPersonaSection}
982
- 当前工作目录: ${this.cwd}
983
- 当前身份: ${this.identity.name} (${this.identity.did})
984
- ${this.currentIntentHint}
985
-
986
- ${this.getToolDefinitions()}
987
-
988
- 工作模式:
989
- 1. 理解用户自然语言请求
990
- 2. 分析需要哪些工具来完成
991
- 3. 按顺序调用工具并观察结果
992
- 4. 根据观察结果决定下一步
993
- 5. 最终给出完整回答
994
-
995
- 重要 (一次命中要求):
996
- - 每次只调用一个工具
997
- - 仔细分析工具返回结果
998
- - 当任务完成时,必须在回答末尾添加 <final gen> 标记表示结束
999
- - 如果需要更多信息,继续调用工具
1000
-
1001
- 【工具调用格式 (严格遵守, 否则系统无法解析)】
1002
- - 你只能输出**一个**工具调用, 不要堆叠多个 invoke
1003
- - 工具调用格式: {"name":"<tool_name>","input":{"arg1":"value1"}}
1004
- - 用 markdown json code block 包裹: \`\`\`json\n{"name":"X","input":{...}}\n\`\`\`
1005
- - 工具调用前可以简短思考 (1-2 句话), 但**不要写长篇 thinking** (会撞 max_tokens)
1006
- - 工具调用后必须等结果, 不要在同一个回复里继续输出
1040
+ const systemPrompt = `${this.bootstrapAddition}你是 ${this.identity.name},基于ReAct (Reasoning + Acting)模式工作。${this.cachedPersonaSection}
1041
+ 当前工作目录: ${this.cwd}
1042
+ 当前身份: ${this.identity.name} (${this.identity.did})
1043
+ ${this.currentIntentHint}
1044
+
1045
+ ${this.getToolDefinitions()}
1046
+
1047
+ 工作模式:
1048
+ 1. 理解用户自然语言请求
1049
+ 2. 分析需要哪些工具来完成
1050
+ 3. 按顺序调用工具并观察结果
1051
+ 4. 根据观察结果决定下一步
1052
+ 5. 最终给出完整回答
1053
+
1054
+ 重要 (一次命中要求):
1055
+ - 每次只调用一个工具
1056
+ - 仔细分析工具返回结果
1057
+ - 当任务完成时,必须在回答末尾添加 <final gen> 标记表示结束
1058
+ - 如果需要更多信息,继续调用工具
1059
+
1060
+ 【工具调用格式 (严格遵守, 否则系统无法解析)】
1061
+ - 你只能输出**一个**工具调用, 不要堆叠多个 invoke
1062
+ - 工具调用格式: {"name":"<tool_name>","input":{"arg1":"value1"}}
1063
+ - 用 markdown json code block 包裹: \`\`\`json\n{"name":"X","input":{...}}\n\`\`\`
1064
+ - 工具调用前可以简短思考 (1-2 句话), 但**不要写长篇 thinking** (会撞 max_tokens)
1065
+ - 工具调用后必须等结果, 不要在同一个回复里继续输出
1007
1066
  - <final gen> 只在**真完成所有任务**时输出, 不要在工具调用前/中输出${this.judgmentGateAddition}${this.contextHintAddition}`;
1008
1067
  // 2026-06-15: 把 currentOnStream 传给 loop, 让 step-timeline 在 pivot 循环里也能 emit step_start/done
1009
1068
  // 之前 loop.execute() 不接 streamCallback, 导致 step-timeline 只能看到老 runReActLoop 路径
@@ -1186,32 +1245,32 @@ ${this.getToolDefinitions()}
1186
1245
  }
1187
1246
  // M2.4: persona section 缓存 — persona 在 loadPersona() 时一次设定, 此后不变
1188
1247
  if (!this.cachedPersonaSection && this.persona) {
1189
- this.cachedPersonaSection = `
1190
- 角色描述: ${this.persona.description || '无'}
1191
- 性格特点: ${this.persona.personality || '无'}
1192
- 问候语: ${this.persona.greeting || '无'}
1248
+ this.cachedPersonaSection = `
1249
+ 角色描述: ${this.persona.description || '无'}
1250
+ 性格特点: ${this.persona.personality || '无'}
1251
+ 问候语: ${this.persona.greeting || '无'}
1193
1252
  `;
1194
1253
  }
1195
1254
  const personaSection = this.cachedPersonaSection;
1196
- const systemPrompt = `${this.bootstrapAddition}你是 ${this.identity.name},基于ReAct (Reasoning + Acting)模式工作。${personaSection}
1197
- 当前工作目录: ${this.cwd}
1198
- 当前身份: ${this.identity.name} (${this.identity.did})
1199
- ${refineContext}
1200
- ${this.currentIntentHint}
1201
-
1202
- ${toolDefs}
1203
-
1204
- 工作模式:
1205
- 1. 理解用户自然语言请求
1206
- 2. 分析需要哪些工具来完成
1207
- 3. 按顺序调用工具并观察结果
1208
- 4. 根据观察结果决定下一步
1209
- 5. 最终给出完整回答
1210
-
1211
- 重要:
1212
- - 每次只调用一个工具
1213
- - 仔细分析工具返回结果
1214
- - 当任务完成时,必须在回答末尾添加 <final gen> 标记表示结束
1255
+ const systemPrompt = `${this.bootstrapAddition}你是 ${this.identity.name},基于ReAct (Reasoning + Acting)模式工作。${personaSection}
1256
+ 当前工作目录: ${this.cwd}
1257
+ 当前身份: ${this.identity.name} (${this.identity.did})
1258
+ ${refineContext}
1259
+ ${this.currentIntentHint}
1260
+
1261
+ ${toolDefs}
1262
+
1263
+ 工作模式:
1264
+ 1. 理解用户自然语言请求
1265
+ 2. 分析需要哪些工具来完成
1266
+ 3. 按顺序调用工具并观察结果
1267
+ 4. 根据观察结果决定下一步
1268
+ 5. 最终给出完整回答
1269
+
1270
+ 重要:
1271
+ - 每次只调用一个工具
1272
+ - 仔细分析工具返回结果
1273
+ - 当任务完成时,必须在回答末尾添加 <final gen> 标记表示结束
1215
1274
  - 如果需要更多信息,继续调用工具${this.judgmentGateAddition}${this.contextHintAddition}`;
1216
1275
  // 3 个恢复机制 (Claude Code 论文 9-step pipeline 内部):
1217
1276
  // 1. max output token 升级 (最多 3 次, 每次 maxOutputTokens 翻倍)
@@ -2248,37 +2307,37 @@ ${toolDefs}
2248
2307
  getDefaultResponse(input) {
2249
2308
  const operationsRef = PiAgentSession.getOperationsReference();
2250
2309
  if (operationsRef) {
2251
- return `收到了: "${input}"
2252
-
2253
- 我是一个判断力处理智能体,支持自然语言交互。
2254
-
2255
- 可用操作(直接说出即可):
2256
- ${this.extractOperationsFromRef(operationsRef)}
2257
-
2258
- 示例请求:
2259
- - "读取 src/index.ts 文件"
2260
- - "总结一下 README.md"
2261
- - "查看当前连接了哪些节点"
2310
+ return `收到了: "${input}"
2311
+
2312
+ 我是一个判断力处理智能体,支持自然语言交互。
2313
+
2314
+ 可用操作(直接说出即可):
2315
+ ${this.extractOperationsFromRef(operationsRef)}
2316
+
2317
+ 示例请求:
2318
+ - "读取 src/index.ts 文件"
2319
+ - "总结一下 README.md"
2320
+ - "查看当前连接了哪些节点"
2262
2321
  - "向 QmABC... 发送测试消息"`;
2263
2322
  }
2264
- return `收到了: "${input}"
2265
-
2266
- 我是一个判断力处理智能体,支持自然语言交互。
2267
-
2268
- 可用操作(直接说出即可):
2269
- - "读取 README.md" - 读取并分析文档
2270
- - "总结文档" - 总结文档内容
2271
- - "改进文档,按照X要求" - 改进文档
2272
- - "查看节点" - 查看已连接的对等节点
2273
- - "向X发送消息Y" - 向对等节点发送消息
2274
- - "广播消息X" - 广播消息到所有节点
2275
- - "查看身份" - 查看当前智能体身份
2276
- - "查看日志" - 查看最近操作日志
2277
-
2278
- 示例请求:
2279
- - "读取 src/index.ts 文件"
2280
- - "总结一下 README.md"
2281
- - "查看当前连接了哪些节点"
2323
+ return `收到了: "${input}"
2324
+
2325
+ 我是一个判断力处理智能体,支持自然语言交互。
2326
+
2327
+ 可用操作(直接说出即可):
2328
+ - "读取 README.md" - 读取并分析文档
2329
+ - "总结文档" - 总结文档内容
2330
+ - "改进文档,按照X要求" - 改进文档
2331
+ - "查看节点" - 查看已连接的对等节点
2332
+ - "向X发送消息Y" - 向对等节点发送消息
2333
+ - "广播消息X" - 广播消息到所有节点
2334
+ - "查看身份" - 查看当前智能体身份
2335
+ - "查看日志" - 查看最近操作日志
2336
+
2337
+ 示例请求:
2338
+ - "读取 src/index.ts 文件"
2339
+ - "总结一下 README.md"
2340
+ - "查看当前连接了哪些节点"
2282
2341
  - "向 QmABC... 发送测试消息"`;
2283
2342
  }
2284
2343
  extractOperationsFromRef(ref) {
@@ -2303,13 +2362,13 @@ ${this.extractOperationsFromRef(operationsRef)}
2303
2362
  }
2304
2363
  }
2305
2364
  return operationLines.length > 0 ? operationLines.join('\n') :
2306
- ` - "读取 README.md" - 读取并分析文档
2307
- - "总结文档" - 总结文档内容
2308
- - "改进文档,按照X要求" - 改进文档
2309
- - "查看节点" - 查看已连接的对等节点
2310
- - "向X发送消息Y" - 向对等节点发送消息
2311
- - "广播消息X" - 广播消息到所有节点
2312
- - "查看身份" - 查看当前智能体身份
2365
+ ` - "读取 README.md" - 读取并分析文档
2366
+ - "总结文档" - 总结文档内容
2367
+ - "改进文档,按照X要求" - 改进文档
2368
+ - "查看节点" - 查看已连接的对等节点
2369
+ - "向X发送消息Y" - 向对等节点发送消息
2370
+ - "广播消息X" - 广播消息到所有节点
2371
+ - "查看身份" - 查看当前智能体身份
2313
2372
  - "查看日志" - 查看最近操作日志`;
2314
2373
  }
2315
2374
  async suggestRename(messages) {