@bolloon/bolloon-agent 0.2.11 → 0.2.13
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.
- package/README.md +495 -467
- package/bin/bolloon-cli.cjs +183 -183
- package/bin/bolloon-daemon.sh +207 -0
- package/bin/bolloon.cjs +0 -0
- package/bin/ipfs +0 -0
- package/dist/agents/agent-manifest-protocol.js +52 -0
- package/dist/agents/chat-segmenter.js +298 -0
- package/dist/agents/constraint-layer.js +19 -19
- package/dist/agents/judgment-protocol.js +479 -0
- package/dist/agents/parse-tool-call.js +304 -0
- package/dist/agents/peer-manifest-loader.js +210 -0
- package/dist/agents/pi-sdk-session-factory.js +110 -0
- package/dist/agents/pi-sdk-session-manager.js +297 -0
- package/dist/agents/pi-sdk-tools.js +1288 -0
- package/dist/agents/pi-sdk-types.js +13 -0
- package/dist/agents/pi-sdk.js +553 -1306
- package/dist/agents/react-loop.js +120 -0
- package/dist/agents/session-store.js +171 -0
- package/dist/agents/shell-guard.js +2 -2
- package/dist/agents/tool-registry.js +136 -0
- package/dist/agents/workflow-pivot-loop.js +200 -22
- package/dist/bollharness/src/scripts/context_router.js +7 -0
- package/dist/bollharness/src/scripts/deploy-guard.js +8 -8
- package/dist/bollharness-integration/context-router-judgment.js +4 -4
- package/dist/bollharness-integration/context-router.js +292 -292
- package/dist/bollharness-integration/gate-state-machine.js +26 -13
- package/dist/bollharness-integration/integration.js +71 -0
- package/dist/bollharness-integration/llm-judgment-engine.js +134 -134
- package/dist/bollharness-integration/skill-adapter.js +57 -127
- package/dist/bootstrap/chat-archiver.js +276 -0
- package/dist/bootstrap/context-collector.js +6 -3
- package/dist/bootstrap/event-log.js +160 -0
- package/dist/bootstrap/lifecycle-hooks.js +15 -1
- package/dist/bootstrap/memory-compressor.js +170 -0
- package/dist/bootstrap/persona-loader.js +94 -0
- package/dist/bootstrap/project-state.js +159 -0
- package/dist/bootstrap/remote-mirror.js +97 -0
- package/dist/bootstrap/session-window.js +121 -0
- package/dist/bootstrap/vector-index.js +248 -0
- package/dist/cli/loading-tui.js +40 -0
- package/dist/cli-entry.js +41 -37
- package/dist/context-compaction/auto-compact.js +7 -7
- package/dist/electron/config.js +21 -0
- package/dist/electron/config.js.map +1 -0
- package/dist/electron/dialogs.js +108 -0
- package/dist/electron/dialogs.js.map +1 -0
- package/dist/electron/first-run.js +170 -0
- package/dist/electron/first-run.js.map +1 -0
- package/dist/electron/ipc.js +20 -0
- package/dist/electron/ipc.js.map +1 -0
- package/dist/electron/logger.js +114 -0
- package/dist/electron/logger.js.map +1 -0
- package/dist/electron/main.js +63 -0
- package/dist/electron/main.js.map +1 -0
- package/dist/electron/menu.js +145 -0
- package/dist/electron/menu.js.map +1 -0
- package/dist/electron/paths.js +75 -0
- package/dist/electron/paths.js.map +1 -0
- package/dist/electron/server.js +119 -0
- package/dist/electron/server.js.map +1 -0
- package/dist/electron/tray.js +111 -0
- package/dist/electron/tray.js.map +1 -0
- package/dist/electron/window.js +111 -0
- package/dist/electron/window.js.map +1 -0
- package/dist/electron-preload.js +26 -6
- package/dist/electron-preload.js.map +1 -1
- package/dist/electron.js +6 -168
- package/dist/electron.js.map +1 -1
- package/dist/index.js +323 -266
- package/dist/llm/config-store.js +24 -8
- package/dist/llm/llm-judgment-client.js +102 -102
- package/dist/llm/pi-ai.js +137 -85
- package/dist/llm/system-prompt/layers/channel/local.md +14 -14
- package/dist/llm/system-prompt/layers/channel/p2p-agent.md +18 -18
- package/dist/llm/system-prompt/layers/channel/p2p-visitor.md +19 -19
- package/dist/llm/system-prompt/layers/core/artifacts_storage.md +89 -89
- package/dist/llm/system-prompt/layers/core/evenhandedness.md +21 -21
- package/dist/llm/system-prompt/layers/core/hibs_reminders.md +15 -15
- package/dist/llm/system-prompt/layers/core/identity.md +37 -37
- package/dist/llm/system-prompt/layers/core/knowledge.md +17 -17
- package/dist/llm/system-prompt/layers/core/memory_system.md +12 -12
- package/dist/llm/system-prompt/layers/core/network_filesystem.md +28 -28
- package/dist/llm/system-prompt/layers/core/refusal.md +37 -37
- package/dist/llm/system-prompt/layers/core/tone.md +31 -31
- package/dist/llm/system-prompt/layers/core/tools.thin.md +13 -13
- package/dist/llm/system-prompt/layers/core/wellbeing.md +41 -41
- package/dist/llm/system-prompt/layers/role/architect.md +20 -20
- package/dist/llm/system-prompt/layers/role/expert.md +19 -19
- package/dist/llm/system-prompt/layers/role/implementer.md +15 -15
- package/dist/llm/system-prompt/layers/role/security.md +15 -15
- package/dist/llm/system-prompt/layers/tool/artifacts.md +72 -72
- package/dist/llm/system-prompt/layers/tool/bash.md +25 -25
- package/dist/llm/system-prompt/layers/tool/hibs_api.md +171 -171
- package/dist/llm/system-prompt/layers/tool/image_search.md +70 -70
- package/dist/llm/system-prompt/layers/tool/manifest.md +89 -89
- package/dist/llm/system-prompt/layers/tool/mcp_apps.md +53 -53
- package/dist/llm/system-prompt/layers/tool/web_search.md +83 -83
- package/dist/llm/tool-manifest/ask_user_input.js +1 -1
- package/dist/llm/tool-manifest/bash.js +3 -3
- package/dist/llm/tool-manifest/create_file.js +4 -4
- package/dist/llm/tool-manifest/fetch_sports_data.js +3 -3
- package/dist/llm/tool-manifest/image_search.js +2 -2
- package/dist/llm/tool-manifest/mcp.js +2 -2
- package/dist/llm/tool-manifest/message_compose.js +3 -3
- package/dist/llm/tool-manifest/places.js +2 -2
- package/dist/llm/tool-manifest/present_files.js +1 -1
- package/dist/llm/tool-manifest/recipe.js +2 -2
- package/dist/llm/tool-manifest/str_replace.js +5 -5
- package/dist/llm/tool-manifest/view.js +3 -3
- package/dist/llm/tool-manifest/weather.js +4 -4
- package/dist/llm/tool-manifest/web.js +4 -4
- package/dist/network/local-inbox-bus.js +73 -0
- package/dist/network/p2p-outbox.js +161 -0
- package/dist/network/peer-fs.js +420 -0
- package/dist/network/peer-resource-bridge.js +216 -0
- package/dist/pi-ecosystem-judgment/causal-judge.js +51 -51
- package/dist/pi-ecosystem-judgment/cleanup.js +126 -0
- package/dist/pi-ecosystem-judgment/distill-prompt.js +42 -42
- package/dist/pi-ecosystem-judgment/distillation.js +14 -14
- package/dist/pi-ecosystem-judgment/human-value-store.js +45 -11
- package/dist/pi-ecosystem-judgment/monitor-gate.js +28 -28
- package/dist/pi-ecosystem-judgment/value-injection.js +102 -102
- package/dist/security/context-router-tool.js +15 -15
- package/dist/security/tool-gate.js +11 -0
- package/dist/web/api-config.html +520 -520
- package/dist/web/client-loop-status.js +220 -0
- package/dist/web/client.js +1677 -244
- package/dist/web/client.js.map +7 -0
- package/dist/web/components/wallet-viem.mjs +118 -118
- package/dist/web/index.html +375 -374
- package/dist/web/input-validator.js +103 -0
- package/dist/web/manifest.json +20 -20
- package/dist/web/routes-judgments.js +738 -0
- package/dist/web/routes-llm-config.js +279 -0
- package/dist/web/routes-tasks.js +222 -0
- package/dist/web/server-sse.js +121 -0
- package/dist/web/server-storage.js +154 -0
- package/dist/web/server-types.js +15 -0
- package/dist/web/server-v3-p2p.js +224 -0
- package/dist/web/server.js +1070 -1307
- package/dist/web/style.css +4742 -4737
- package/dist/web/util/safe-name.js +32 -0
- package/package.json +162 -160
- package/scripts/build-cli.js +215 -215
- package/scripts/build-web.ts +130 -125
- package/scripts/postinstall.js +152 -152
- package/bin/bolloon.js +0 -157
- package/dist/constraint-runtime/src/_archive_helper.js +0 -9
- package/dist/constraint-runtime/src/agent/coordinator.js +0 -48
- package/dist/constraint-runtime/src/agent/index.js +0 -1
- package/dist/constraint-runtime/src/assistant/index.js +0 -12
- package/dist/constraint-runtime/src/bootstrap/index.js +0 -12
- package/dist/constraint-runtime/src/bootstrap_graph.js +0 -13
- package/dist/constraint-runtime/src/bridge/index.js +0 -12
- package/dist/constraint-runtime/src/buddy/index.js +0 -12
- package/dist/constraint-runtime/src/cli/index.js +0 -12
- package/dist/constraint-runtime/src/command_graph.js +0 -10
- package/dist/constraint-runtime/src/commands.js +0 -60
- package/dist/constraint-runtime/src/components/index.js +0 -12
- package/dist/constraint-runtime/src/constants/index.js +0 -12
- package/dist/constraint-runtime/src/constraint/budget.js +0 -22
- package/dist/constraint-runtime/src/constraint/index.js +0 -2
- package/dist/constraint-runtime/src/constraint/permission.js +0 -20
- package/dist/constraint-runtime/src/context.js +0 -30
- package/dist/constraint-runtime/src/coordinator/index.js +0 -12
- package/dist/constraint-runtime/src/cost_hook.js +0 -4
- package/dist/constraint-runtime/src/cost_tracker.js +0 -8
- package/dist/constraint-runtime/src/deferred_init.js +0 -10
- package/dist/constraint-runtime/src/direct_modes.js +0 -6
- package/dist/constraint-runtime/src/dynamic-tool-loader.js +0 -85
- package/dist/constraint-runtime/src/entrypoints/index.js +0 -12
- package/dist/constraint-runtime/src/execution_registry.js +0 -44
- package/dist/constraint-runtime/src/history.js +0 -9
- package/dist/constraint-runtime/src/hooks/index.js +0 -12
- package/dist/constraint-runtime/src/index.js +0 -26
- package/dist/constraint-runtime/src/ink.js +0 -4
- package/dist/constraint-runtime/src/keybindings/index.js +0 -12
- package/dist/constraint-runtime/src/memdir/index.js +0 -12
- package/dist/constraint-runtime/src/migrations/index.js +0 -12
- package/dist/constraint-runtime/src/models.js +0 -1
- package/dist/constraint-runtime/src/moreright/index.js +0 -12
- package/dist/constraint-runtime/src/native_ts/index.js +0 -12
- package/dist/constraint-runtime/src/output_styles/index.js +0 -12
- package/dist/constraint-runtime/src/parity_audit.js +0 -12
- package/dist/constraint-runtime/src/plugins/index.js +0 -12
- package/dist/constraint-runtime/src/port_manifest.js +0 -11
- package/dist/constraint-runtime/src/prefetch.js +0 -9
- package/dist/constraint-runtime/src/query.js +0 -1
- package/dist/constraint-runtime/src/remote/index.js +0 -12
- package/dist/constraint-runtime/src/remote_runtime.js +0 -9
- package/dist/constraint-runtime/src/runtime/index.js +0 -1
- package/dist/constraint-runtime/src/runtime/session.js +0 -35
- package/dist/constraint-runtime/src/schemas/index.js +0 -12
- package/dist/constraint-runtime/src/screens/index.js +0 -12
- package/dist/constraint-runtime/src/server/index.js +0 -12
- package/dist/constraint-runtime/src/services/index.js +0 -12
- package/dist/constraint-runtime/src/session_store.js +0 -22
- package/dist/constraint-runtime/src/setup.js +0 -30
- package/dist/constraint-runtime/src/skills/index.js +0 -1
- package/dist/constraint-runtime/src/skills/skill-registry.js +0 -28
- package/dist/constraint-runtime/src/state/index.js +0 -12
- package/dist/constraint-runtime/src/system_init.js +0 -20
- package/dist/constraint-runtime/src/thinking/engine.js +0 -42
- package/dist/constraint-runtime/src/thinking/index.js +0 -1
- package/dist/constraint-runtime/src/tool_pool.js +0 -8
- package/dist/constraint-runtime/src/tools/OpenCLI/execAdapter.js +0 -7
- package/dist/constraint-runtime/src/tools/OpenCLI/listAdapters.js +0 -7
- package/dist/constraint-runtime/src/tools/OpenCLI/runCommand.js +0 -7
- package/dist/constraint-runtime/src/tools/SafeSDK/confirmTransaction.js +0 -6
- package/dist/constraint-runtime/src/tools/SafeSDK/createTransaction.js +0 -8
- package/dist/constraint-runtime/src/tools/SafeSDK/executeTransaction.js +0 -6
- package/dist/constraint-runtime/src/tools/SafeSDK/getBalance.js +0 -6
- package/dist/constraint-runtime/src/tools/SafeSDK/getPendingTransactions.js +0 -6
- package/dist/constraint-runtime/src/tools/SafeSDK/proposeTransaction.js +0 -6
- package/dist/constraint-runtime/src/tools.js +0 -80
- package/dist/constraint-runtime/src/transcript.js +0 -19
- package/dist/constraint-runtime/src/types/index.js +0 -12
- package/dist/constraint-runtime/src/upstream_proxy/index.js +0 -12
- package/dist/constraint-runtime/src/utils/index.js +0 -12
- package/dist/constraint-runtime/src/vim/index.js +0 -12
- package/dist/constraint-runtime/src/voice/index.js +0 -12
- package/dist/constraint-runtime/tests/agent.test.js +0 -16
- package/dist/constraint-runtime/tests/constraint.test.js +0 -41
- package/dist/constraint-runtime/tests/skill.test.js +0 -19
- package/dist/constraint-runtime/tests/thinking.test.js +0 -22
- package/dist/pi-ecosystem-colony/index.js +0 -365
- package/dist/social/ant-colony/AdaptiveHeartbeat.js +0 -101
- package/dist/social/ant-colony/PheromoneEngine.js +0 -227
- package/dist/social/ant-colony/index.js +0 -6
- package/dist/social/ant-colony/types.js +0 -24
- package/dist/test/ai-judgment-test.js +0 -80
- package/dist/test/bollharness-integration.test.js +0 -318
- package/dist/test/channel-agent-multi-dialogue.js +0 -205
- package/dist/test/channel-heartbeat-agent-test.js +0 -201
- package/dist/test/constraint-layer.test.js +0 -164
- package/dist/test/diap-identity-test.js +0 -172
- package/dist/test/diap-quick-test.js +0 -62
- package/dist/test/global-shared-context.test.js +0 -315
- package/dist/test/harness-judgment-injection.test.js +0 -246
- package/dist/test/harness-workflow-integrator-test.js +0 -228
- package/dist/test/human-value-store.test.js +0 -243
- package/dist/test/hybrid-integration-test.js +0 -118
- package/dist/test/hybrid-messenger-verify.js +0 -55
- package/dist/test/iroh-bistream-debug.js +0 -38
- package/dist/test/iroh-communication.test.js +0 -66
- package/dist/test/iroh-debug-test.js +0 -57
- package/dist/test/iroh-diap-test.js +0 -71
- package/dist/test/iroh-direct-connect.js +0 -55
- package/dist/test/iroh-e2e-fixed.js +0 -89
- package/dist/test/iroh-e2e-same-process.js +0 -63
- package/dist/test/iroh-e2e.js +0 -66
- package/dist/test/iroh-final-e2e.js +0 -72
- package/dist/test/iroh-relay-test.js +0 -37
- package/dist/test/iroh-simple-test.js +0 -41
- package/dist/test/iroh-transport-verify.js +0 -54
- package/dist/test/iroh-transport.test.js +0 -37
- package/dist/test/iroh-two-nodes.js +0 -70
- package/dist/test/iroh-verify.js +0 -44
- package/dist/test/judgment-decision.test.js +0 -219
- package/dist/test/llm-judgment-integration.test.js +0 -220
- package/dist/test/p2p-agent-complex-dialogue.js +0 -385
- package/dist/test/p2p-agent-dialogue.js +0 -341
- package/dist/test/p2p-agent-full-bidirectional.js +0 -510
- package/dist/test/p2p-agent-harness-flow.js +0 -437
- package/dist/test/p2p-agent-harness-single.js +0 -143
- package/dist/test/p2p-ai-dialogue-test.js +0 -318
- package/dist/test/p2p-cid-connect-test.js +0 -195
- package/dist/test/p2p-connect-receiver.js +0 -69
- package/dist/test/p2p-doc-transfer.js +0 -110
- package/dist/test/p2p-identity-page-test.js +0 -77
- package/dist/test/p2p-iroh-test.js +0 -171
- package/dist/test/p2p-minimal-test.js +0 -241
- package/dist/test/p2p-node-1.js +0 -148
- package/dist/test/p2p-node-2.js +0 -148
- package/dist/test/p2p-server.js +0 -281
- package/dist/test/p2p-two-nodes-test.js +0 -438
- package/dist/test/pi-sdk.test.js +0 -44
- package/dist/test/set-persona.js +0 -40
- package/dist/test/simple.test.js +0 -9
- package/dist/test/storage-integration.test.js +0 -150
- package/dist/test/subagent-manager.test.js +0 -276
- package/dist/test/test-gate-flow.test.js +0 -81
- package/dist/test/workflow-engine.test.js +0 -87
- package/dist/test/workflow-pivot-loop.test.js +0 -246
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AdaptiveHeartbeat - 动态心跳策略
|
|
3
|
-
*
|
|
4
|
-
* 基于活跃度、信息素密度等因子动态调整心跳间隔
|
|
5
|
-
*/
|
|
6
|
-
import { DEFAULT_ADAPTIVE_CONFIG, } from './types.js';
|
|
7
|
-
export class AdaptiveHeartbeat {
|
|
8
|
-
config;
|
|
9
|
-
lastActivityTime = Date.now();
|
|
10
|
-
activityHistory = [];
|
|
11
|
-
recentDiscoveries = 0;
|
|
12
|
-
pheromoneDensity = 0;
|
|
13
|
-
discoveryBoost = 0;
|
|
14
|
-
activityTimer = null;
|
|
15
|
-
constructor(config) {
|
|
16
|
-
this.config = { ...DEFAULT_ADAPTIVE_CONFIG, ...config };
|
|
17
|
-
this.startActivityTracking();
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* 获取下一次心跳的决策
|
|
21
|
-
*/
|
|
22
|
-
decide() {
|
|
23
|
-
const now = Date.now();
|
|
24
|
-
const timeSinceLastActivity = now - this.lastActivityTime;
|
|
25
|
-
const activityRate = this.calculateActivityRate();
|
|
26
|
-
const pheromoneFactor = this.pheromoneDensity;
|
|
27
|
-
const discoveryFactor = this.calculateDiscoveryFactor();
|
|
28
|
-
const combinedFactor = activityRate * 0.2 +
|
|
29
|
-
pheromoneFactor * this.config.pheromoneBoostFactor +
|
|
30
|
-
discoveryFactor * this.config.discoveryBoostFactor;
|
|
31
|
-
const interval = Math.max(this.config.minInterval, Math.min(this.config.maxInterval, this.config.baseInterval * (1 - combinedFactor * 0.7)));
|
|
32
|
-
let priority = 'normal';
|
|
33
|
-
if (discoveryFactor > 0.5 || this.discoveryBoost > 0.6) {
|
|
34
|
-
priority = 'high';
|
|
35
|
-
}
|
|
36
|
-
else if (this.discoveryBoost > 0.8) {
|
|
37
|
-
priority = 'urgent';
|
|
38
|
-
}
|
|
39
|
-
else if (activityRate < this.config.lowActivityThreshold) {
|
|
40
|
-
priority = 'low';
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
interval: Math.round(interval),
|
|
44
|
-
shouldExplore: discoveryFactor > 0.3 || pheromoneFactor > 0.5,
|
|
45
|
-
shouldBroadcast: priority !== 'low' || timeSinceLastActivity > this.config.baseInterval,
|
|
46
|
-
priorityLevel: priority,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* 记录活跃事件
|
|
51
|
-
*/
|
|
52
|
-
recordActivity(type) {
|
|
53
|
-
const now = Date.now();
|
|
54
|
-
this.lastActivityTime = now;
|
|
55
|
-
this.activityHistory.push(now);
|
|
56
|
-
if (type === 'discovery') {
|
|
57
|
-
this.recentDiscoveries++;
|
|
58
|
-
this.discoveryBoost = Math.min(1, this.discoveryBoost + 0.2);
|
|
59
|
-
}
|
|
60
|
-
this.discoveryBoost = Math.max(0, this.discoveryBoost - 0.05);
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* 设置信息素密度
|
|
64
|
-
*/
|
|
65
|
-
setPheromoneDensity(density) {
|
|
66
|
-
this.pheromoneDensity = Math.max(0, Math.min(1, density));
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* 获取当前配置
|
|
70
|
-
*/
|
|
71
|
-
getConfig() {
|
|
72
|
-
return { ...this.config };
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* 更新配置
|
|
76
|
-
*/
|
|
77
|
-
updateConfig(updates) {
|
|
78
|
-
this.config = { ...this.config, ...updates };
|
|
79
|
-
}
|
|
80
|
-
calculateActivityRate() {
|
|
81
|
-
const now = Date.now();
|
|
82
|
-
const recentActivity = this.activityHistory.filter((t) => now - t < 60000).length;
|
|
83
|
-
return recentActivity / 60;
|
|
84
|
-
}
|
|
85
|
-
calculateDiscoveryFactor() {
|
|
86
|
-
return Math.min(1, this.recentDiscoveries / 5);
|
|
87
|
-
}
|
|
88
|
-
startActivityTracking() {
|
|
89
|
-
this.activityTimer = setInterval(() => {
|
|
90
|
-
const cutoff = Date.now() - 5 * 60 * 1000;
|
|
91
|
-
this.activityHistory = this.activityHistory.filter((t) => t > cutoff);
|
|
92
|
-
this.recentDiscoveries = Math.max(0, this.recentDiscoveries - 1);
|
|
93
|
-
this.discoveryBoost = Math.max(0, this.discoveryBoost - 0.1);
|
|
94
|
-
}, 60000);
|
|
95
|
-
}
|
|
96
|
-
shutdown() {
|
|
97
|
-
if (this.activityTimer) {
|
|
98
|
-
clearInterval(this.activityTimer);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PheromoneEngine - 信息素管理引擎
|
|
3
|
-
*
|
|
4
|
-
* 模拟蚂蚁的信息素系统,用于引导智能体发现和路由决策
|
|
5
|
-
*/
|
|
6
|
-
import * as fs from 'fs/promises';
|
|
7
|
-
import * as path from 'path';
|
|
8
|
-
import { PheromoneType, DEFAULT_PHEROMONE_CONFIG, } from './types.js';
|
|
9
|
-
export { PheromoneType };
|
|
10
|
-
const PHEROMONE_DB_PATH = path.join(process.env.HOME || '/tmp', '.bolloon', 'ant-colony', 'pheromones.json');
|
|
11
|
-
export class PheromoneEngine {
|
|
12
|
-
trails = new Map();
|
|
13
|
-
fields = new Map();
|
|
14
|
-
evaporationTimer = null;
|
|
15
|
-
saveTimer = null;
|
|
16
|
-
dirty = false;
|
|
17
|
-
async initialize() {
|
|
18
|
-
await fs.mkdir(path.dirname(PHEROMONE_DB_PATH), { recursive: true });
|
|
19
|
-
await this.load();
|
|
20
|
-
this.startEvaporation();
|
|
21
|
-
this.startAutoSave();
|
|
22
|
-
}
|
|
23
|
-
startEvaporation() {
|
|
24
|
-
this.evaporationTimer = setInterval(() => {
|
|
25
|
-
this.evaporate();
|
|
26
|
-
}, DEFAULT_PHEROMONE_CONFIG.evaporationInterval);
|
|
27
|
-
}
|
|
28
|
-
startAutoSave() {
|
|
29
|
-
this.saveTimer = setInterval(() => {
|
|
30
|
-
if (this.dirty) {
|
|
31
|
-
this.save().catch(console.error);
|
|
32
|
-
this.dirty = false;
|
|
33
|
-
}
|
|
34
|
-
}, 30000);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* 放置信息素
|
|
38
|
-
*/
|
|
39
|
-
async deposit(type, sourceDid, targetDid, strength = 0.5, options) {
|
|
40
|
-
const key = this.getTrailKey(type, sourceDid, targetDid);
|
|
41
|
-
const existing = this.trails.get(key);
|
|
42
|
-
const newStrength = existing
|
|
43
|
-
? Math.min(1, existing.strength + strength * 0.3)
|
|
44
|
-
: Math.min(1, strength);
|
|
45
|
-
const trail = {
|
|
46
|
-
id: key,
|
|
47
|
-
type,
|
|
48
|
-
sourceDid,
|
|
49
|
-
targetDid,
|
|
50
|
-
strength: newStrength,
|
|
51
|
-
decayRate: DEFAULT_PHEROMONE_CONFIG.decayRate,
|
|
52
|
-
createdAt: existing?.createdAt || Date.now(),
|
|
53
|
-
lastUpdate: Date.now(),
|
|
54
|
-
capability: options?.capability,
|
|
55
|
-
qualityScore: options?.qualityScore,
|
|
56
|
-
};
|
|
57
|
-
this.trails.set(key, trail);
|
|
58
|
-
this.dirty = true;
|
|
59
|
-
if (options?.capability) {
|
|
60
|
-
this.updateField(type, targetDid, options.capability);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* 读取某方向的信息素强度
|
|
65
|
-
*/
|
|
66
|
-
getStrength(type, targetDid) {
|
|
67
|
-
let totalStrength = 0;
|
|
68
|
-
let count = 0;
|
|
69
|
-
for (const trail of this.trails.values()) {
|
|
70
|
-
if (trail.type === type && trail.targetDid === targetDid) {
|
|
71
|
-
totalStrength += trail.strength;
|
|
72
|
-
count++;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return count > 0 ? totalStrength / count : 0;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* 查询特定能力的最佳节点
|
|
79
|
-
*/
|
|
80
|
-
queryByCapability(capability, limit = 5) {
|
|
81
|
-
const field = this.fields.get(capability);
|
|
82
|
-
if (!field)
|
|
83
|
-
return [];
|
|
84
|
-
const candidates = [];
|
|
85
|
-
for (const trail of this.trails.values()) {
|
|
86
|
-
if (trail.type === PheromoneType.CAPABILITY &&
|
|
87
|
-
trail.capability?.includes(capability)) {
|
|
88
|
-
const score = trail.strength * (trail.qualityScore || 0.5);
|
|
89
|
-
candidates.push({ did: trail.targetDid, strength: score });
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
candidates.sort((a, b) => b.strength - a.strength);
|
|
93
|
-
return candidates.slice(0, limit).map((c) => c.did);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* 获取到达某节点的最佳下一跳
|
|
97
|
-
*/
|
|
98
|
-
getNextHop(targetDid, excludeDids = []) {
|
|
99
|
-
let bestHop = null;
|
|
100
|
-
let bestStrength = 0;
|
|
101
|
-
for (const trail of this.trails.values()) {
|
|
102
|
-
if (trail.targetDid === targetDid && trail.strength > bestStrength) {
|
|
103
|
-
if (!excludeDids.includes(trail.sourceDid)) {
|
|
104
|
-
bestHop = trail.sourceDid;
|
|
105
|
-
bestStrength = trail.strength;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return bestHop;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* 获取所有已知节点及其信息素强度
|
|
113
|
-
*/
|
|
114
|
-
getAllNodeStrengths() {
|
|
115
|
-
const result = new Map();
|
|
116
|
-
for (const trail of this.trails.values()) {
|
|
117
|
-
const current = result.get(trail.targetDid) || 0;
|
|
118
|
-
result.set(trail.targetDid, Math.max(current, trail.strength));
|
|
119
|
-
}
|
|
120
|
-
return result;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* 获取信息素统计
|
|
124
|
-
*/
|
|
125
|
-
getStats() {
|
|
126
|
-
let totalStrength = 0;
|
|
127
|
-
for (const trail of this.trails.values()) {
|
|
128
|
-
totalStrength += trail.strength;
|
|
129
|
-
}
|
|
130
|
-
return {
|
|
131
|
-
totalTrails: this.trails.size,
|
|
132
|
-
avgStrength: this.trails.size > 0 ? totalStrength / this.trails.size : 0,
|
|
133
|
-
capabilityCount: this.fields.size,
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
getTrailKey(type, source, target) {
|
|
137
|
-
return `${type}:${source}:${target}`;
|
|
138
|
-
}
|
|
139
|
-
updateField(type, targetDid, capability) {
|
|
140
|
-
if (!capability)
|
|
141
|
-
return;
|
|
142
|
-
for (const cap of capability) {
|
|
143
|
-
let field = this.fields.get(cap);
|
|
144
|
-
if (!field) {
|
|
145
|
-
field = {
|
|
146
|
-
capability: cap,
|
|
147
|
-
avgStrength: 0,
|
|
148
|
-
nodeCount: 0,
|
|
149
|
-
lastUpdate: Date.now(),
|
|
150
|
-
};
|
|
151
|
-
this.fields.set(cap, field);
|
|
152
|
-
}
|
|
153
|
-
const trails = Array.from(this.trails.values()).filter((t) => t.type === type && t.capability?.includes(cap));
|
|
154
|
-
field.avgStrength =
|
|
155
|
-
trails.reduce((sum, t) => sum + t.strength, 0) / (trails.length || 1);
|
|
156
|
-
field.nodeCount = new Set(trails.map((t) => t.targetDid)).size;
|
|
157
|
-
field.lastUpdate = Date.now();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
evaporate() {
|
|
161
|
-
const now = Date.now();
|
|
162
|
-
const toDelete = [];
|
|
163
|
-
for (const [key, trail] of this.trails.entries()) {
|
|
164
|
-
const ageHours = (now - trail.lastUpdate) / (1000 * 60 * 60);
|
|
165
|
-
const decayFactor = Math.pow(1 - trail.decayRate, ageHours);
|
|
166
|
-
trail.strength = trail.strength * decayFactor;
|
|
167
|
-
if (trail.strength < DEFAULT_PHEROMONE_CONFIG.minStrength ||
|
|
168
|
-
now - trail.createdAt > DEFAULT_PHEROMONE_CONFIG.maxTrailAge) {
|
|
169
|
-
toDelete.push(key);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
trail.lastUpdate = now;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
for (const key of toDelete) {
|
|
176
|
-
this.trails.delete(key);
|
|
177
|
-
}
|
|
178
|
-
if (toDelete.length > 0) {
|
|
179
|
-
this.dirty = true;
|
|
180
|
-
this.save().catch(console.error);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
async save() {
|
|
184
|
-
try {
|
|
185
|
-
const data = {
|
|
186
|
-
trails: Array.from(this.trails.values()),
|
|
187
|
-
fields: Array.from(this.fields.entries()),
|
|
188
|
-
};
|
|
189
|
-
await fs.writeFile(PHEROMONE_DB_PATH, JSON.stringify(data, null, 2));
|
|
190
|
-
}
|
|
191
|
-
catch (e) {
|
|
192
|
-
console.warn('[PheromoneEngine] Save failed:', e);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
async load() {
|
|
196
|
-
try {
|
|
197
|
-
const data = await fs.readFile(PHEROMONE_DB_PATH, 'utf-8');
|
|
198
|
-
const parsed = JSON.parse(data);
|
|
199
|
-
this.trails.clear();
|
|
200
|
-
for (const t of parsed.trails || []) {
|
|
201
|
-
this.trails.set(t.id, t);
|
|
202
|
-
}
|
|
203
|
-
this.fields.clear();
|
|
204
|
-
for (const [cap, field] of parsed.fields || []) {
|
|
205
|
-
this.fields.set(cap, field);
|
|
206
|
-
}
|
|
207
|
-
console.log(`[PheromoneEngine] Loaded ${this.trails.size} trails, ${this.fields.size} fields`);
|
|
208
|
-
}
|
|
209
|
-
catch {
|
|
210
|
-
console.log('[PheromoneEngine] No existing pheromone data, starting fresh');
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
shutdown() {
|
|
214
|
-
if (this.evaporationTimer) {
|
|
215
|
-
clearInterval(this.evaporationTimer);
|
|
216
|
-
}
|
|
217
|
-
if (this.saveTimer) {
|
|
218
|
-
clearInterval(this.saveTimer);
|
|
219
|
-
}
|
|
220
|
-
if (this.dirty) {
|
|
221
|
-
this.save()
|
|
222
|
-
.then(() => console.log('[PheromoneEngine] Saved on shutdown'))
|
|
223
|
-
.catch(console.error);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
export const pheromoneEngine = new PheromoneEngine();
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Ant Colony Module - 蚁群心跳系统核心模块
|
|
3
|
-
*/
|
|
4
|
-
export { PheromoneType, DEFAULT_PHEROMONE_CONFIG, DEFAULT_ADAPTIVE_CONFIG, } from './types.js';
|
|
5
|
-
export { PheromoneEngine, pheromoneEngine } from './PheromoneEngine.js';
|
|
6
|
-
export { AdaptiveHeartbeat } from './AdaptiveHeartbeat.js';
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Ant Colony Types - 信息素系统类型定义
|
|
3
|
-
*/
|
|
4
|
-
export var PheromoneType;
|
|
5
|
-
(function (PheromoneType) {
|
|
6
|
-
PheromoneType["DISCOVERY"] = "discovery";
|
|
7
|
-
PheromoneType["CAPABILITY"] = "capability";
|
|
8
|
-
})(PheromoneType || (PheromoneType = {}));
|
|
9
|
-
export const DEFAULT_PHEROMONE_CONFIG = {
|
|
10
|
-
decayRate: 0.05,
|
|
11
|
-
evaporationInterval: 5 * 60 * 1000,
|
|
12
|
-
maxTrailAge: 24 * 60 * 60 * 1000,
|
|
13
|
-
minStrength: 0.05,
|
|
14
|
-
maxStrength: 1.0,
|
|
15
|
-
};
|
|
16
|
-
export const DEFAULT_ADAPTIVE_CONFIG = {
|
|
17
|
-
minInterval: 10 * 1000,
|
|
18
|
-
maxInterval: 120 * 1000,
|
|
19
|
-
baseInterval: 30 * 1000,
|
|
20
|
-
lowActivityThreshold: 0.5,
|
|
21
|
-
highActivityThreshold: 5,
|
|
22
|
-
pheromoneBoostFactor: 0.3,
|
|
23
|
-
discoveryBoostFactor: 0.4,
|
|
24
|
-
};
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AI + 判断力注入 测试
|
|
3
|
-
* 验证 AI 调用和价值观注入功能
|
|
4
|
-
*/
|
|
5
|
-
import { createAgentSession } from '../agents/pi-sdk.js';
|
|
6
|
-
import { generateValueInjection } from '../pi-ecosystem-judgment/value-injection.js';
|
|
7
|
-
import { storeHumanJudgment, initializeValueStore, getValueStats } from '../pi-ecosystem-judgment/human-value-store.js';
|
|
8
|
-
async function main() {
|
|
9
|
-
console.log('╔═══════════════════════════════════════════════════╗');
|
|
10
|
-
console.log('║ AI + 判断力注入测试 ║');
|
|
11
|
-
console.log('╚═══════════════════════════════════════════════════╝');
|
|
12
|
-
// 1. 初始化
|
|
13
|
-
console.log('\n[1] 初始化...');
|
|
14
|
-
await initializeValueStore();
|
|
15
|
-
console.log(' ✅ 判断力存储就绪');
|
|
16
|
-
// 2. 存储一些测试判断
|
|
17
|
-
console.log('\n[2] 存储测试判断...');
|
|
18
|
-
await storeHumanJudgment({
|
|
19
|
-
decision: '代码安全性比性能更重要',
|
|
20
|
-
decision_type: 'approve',
|
|
21
|
-
reasons: ['防止被攻击', '用户数据保护'],
|
|
22
|
-
values_derived: [
|
|
23
|
-
{ category: 'safety', value: 'security-first', weight: 0.9 }
|
|
24
|
-
],
|
|
25
|
-
context: {
|
|
26
|
-
domain: 'code',
|
|
27
|
-
complexity: 'moderate',
|
|
28
|
-
stakes: 'high',
|
|
29
|
-
time_pressure: 'low'
|
|
30
|
-
},
|
|
31
|
-
outcome: { approved: true },
|
|
32
|
-
metadata: {
|
|
33
|
-
source: 'explicit',
|
|
34
|
-
confidence: 0.85,
|
|
35
|
-
revisable: true
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
console.log(' ✅ 判断已存储');
|
|
39
|
-
// 3. 查看统计
|
|
40
|
-
console.log('\n[3] 判断力统计...');
|
|
41
|
-
const stats = await getValueStats();
|
|
42
|
-
console.log(` 总判断数: ${stats.total_judgments}`);
|
|
43
|
-
console.log(` 顶级价值观: ${stats.top_values.slice(0, 3).map(v => v.value).join(', ')}`);
|
|
44
|
-
// 4. 生成价值观注入
|
|
45
|
-
console.log('\n[4] 生成价值观注入...');
|
|
46
|
-
const injection = await generateValueInjection('代码安全问题', {
|
|
47
|
-
mode: 'standard',
|
|
48
|
-
maxTokens: 400,
|
|
49
|
-
includeExamples: true,
|
|
50
|
-
includeRules: true
|
|
51
|
-
});
|
|
52
|
-
console.log(` 注入内容长度: ${injection.length} 字符`);
|
|
53
|
-
if (injection) {
|
|
54
|
-
console.log(' ✅ 价值观注入已生成');
|
|
55
|
-
}
|
|
56
|
-
// 5. 调用 AI
|
|
57
|
-
console.log('\n[5] 调用 AI...');
|
|
58
|
-
const session = await createAgentSession({ cwd: process.cwd() });
|
|
59
|
-
const prompt = injection
|
|
60
|
-
? `${injection}
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
【当前问题】
|
|
64
|
-
用户问:如何确保 API 接口的安全性?
|
|
65
|
-
|
|
66
|
-
请基于上面的价值观回复用户。`
|
|
67
|
-
: '用户问:如何确保 API 接口的安全性?请给出建议。';
|
|
68
|
-
console.log('\n[6] 发送请求到 AI...');
|
|
69
|
-
const response = await session.prompt(prompt);
|
|
70
|
-
console.log('\n═══════════════════════════════════════════════════════');
|
|
71
|
-
console.log(' AI 回复:');
|
|
72
|
-
console.log('═══════════════════════════════════════════════════════');
|
|
73
|
-
console.log(response);
|
|
74
|
-
console.log('═══════════════════════════════════════════════════════\n');
|
|
75
|
-
console.log('✅ 测试完成!');
|
|
76
|
-
}
|
|
77
|
-
main().catch(e => {
|
|
78
|
-
console.error('❌ 测试失败:', e);
|
|
79
|
-
process.exit(1);
|
|
80
|
-
});
|