@bolloon/bolloon-agent 0.1.0
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 +462 -0
- package/dist/agents/constraint-layer.js +211 -0
- package/dist/agents/constraint-layer.js.map +1 -0
- package/dist/agents/pi-sdk.js +1199 -0
- package/dist/agents/pi-sdk.js.map +1 -0
- package/dist/agents/protocol.js +229 -0
- package/dist/agents/subagent-manager.js +361 -0
- package/dist/agents/workflow-engine.js +222 -0
- package/dist/agents/workflow-engine.js.map +1 -0
- package/dist/bollharness-integration/bollharness-integration/context-router-judgment.d.ts +48 -0
- package/dist/bollharness-integration/bollharness-integration/context-router-judgment.js +261 -0
- package/dist/bollharness-integration/bollharness-integration/context-router.d.ts +110 -0
- package/dist/bollharness-integration/bollharness-integration/context-router.js +542 -0
- package/dist/bollharness-integration/bollharness-integration/gate-state-machine.d.ts +87 -0
- package/dist/bollharness-integration/bollharness-integration/gate-state-machine.js +231 -0
- package/dist/bollharness-integration/bollharness-integration/gate-transition-hooks.d.ts +30 -0
- package/dist/bollharness-integration/bollharness-integration/gate-transition-hooks.js +91 -0
- package/dist/bollharness-integration/bollharness-integration/guard-checker.d.ts +105 -0
- package/dist/bollharness-integration/bollharness-integration/guard-checker.js +353 -0
- package/dist/bollharness-integration/bollharness-integration/index.d.ts +66 -0
- package/dist/bollharness-integration/bollharness-integration/index.js +32 -0
- package/dist/bollharness-integration/bollharness-integration/integration.d.ts +219 -0
- package/dist/bollharness-integration/bollharness-integration/integration.js +420 -0
- package/dist/bollharness-integration/bollharness-integration/skill-adapter.d.ts +151 -0
- package/dist/bollharness-integration/bollharness-integration/skill-adapter.js +518 -0
- package/dist/bollharness-integration/context-router-judgment.js +269 -0
- package/dist/bollharness-integration/context-router-judgment.js.map +1 -0
- package/dist/bollharness-integration/context-router.js +584 -0
- package/dist/bollharness-integration/context-router.js.map +1 -0
- package/dist/bollharness-integration/gate-state-machine.js +349 -0
- package/dist/bollharness-integration/gate-state-machine.js.map +1 -0
- package/dist/bollharness-integration/gate-transition-hooks.js +136 -0
- package/dist/bollharness-integration/gate-transition-hooks.js.map +1 -0
- package/dist/bollharness-integration/guard-checker.js +400 -0
- package/dist/bollharness-integration/guard-checker.js.map +1 -0
- package/dist/bollharness-integration/index.js +142 -0
- package/dist/bollharness-integration/index.js.map +1 -0
- package/dist/bollharness-integration/integration.js +427 -0
- package/dist/bollharness-integration/integration.js.map +1 -0
- package/dist/bollharness-integration/llm/pi-ai.d.ts +60 -0
- package/dist/bollharness-integration/llm/pi-ai.js +397 -0
- package/dist/bollharness-integration/pi-ecosystem-colony/index.d.ts +153 -0
- package/dist/bollharness-integration/pi-ecosystem-colony/index.js +365 -0
- package/dist/bollharness-integration/pi-ecosystem-goals/index.d.ts +136 -0
- package/dist/bollharness-integration/pi-ecosystem-goals/index.js +458 -0
- package/dist/bollharness-integration/pi-ecosystem-judgment/decision.d.ts +117 -0
- package/dist/bollharness-integration/pi-ecosystem-judgment/decision.js +300 -0
- package/dist/bollharness-integration/pi-ecosystem-judgment/distillation.d.ts +78 -0
- package/dist/bollharness-integration/pi-ecosystem-judgment/distillation.js +291 -0
- package/dist/bollharness-integration/pi-ecosystem-judgment/index.d.ts +139 -0
- package/dist/bollharness-integration/pi-ecosystem-judgment/index.js +445 -0
- package/dist/bollharness-integration/pi-ecosystem-mcp/index.d.ts +127 -0
- package/dist/bollharness-integration/pi-ecosystem-mcp/index.js +331 -0
- package/dist/bollharness-integration/pi-ecosystem-subagents/index.d.ts +95 -0
- package/dist/bollharness-integration/pi-ecosystem-subagents/index.js +303 -0
- package/dist/bollharness-integration/skill-adapter.js +564 -0
- package/dist/bollharness-integration/skill-adapter.js.map +1 -0
- package/dist/cli/interface.js +181 -0
- package/dist/constraint-runtime/src/_archive_helper.js +9 -0
- package/dist/constraint-runtime/src/agent/coordinator.js +48 -0
- package/dist/constraint-runtime/src/agent/index.js +1 -0
- package/dist/constraint-runtime/src/assistant/index.js +12 -0
- package/dist/constraint-runtime/src/bootstrap/index.js +12 -0
- package/dist/constraint-runtime/src/bootstrap_graph.js +13 -0
- package/dist/constraint-runtime/src/bridge/index.js +12 -0
- package/dist/constraint-runtime/src/buddy/index.js +12 -0
- package/dist/constraint-runtime/src/cli/index.js +12 -0
- package/dist/constraint-runtime/src/command_graph.js +10 -0
- package/dist/constraint-runtime/src/commands.js +50 -0
- package/dist/constraint-runtime/src/components/index.js +12 -0
- package/dist/constraint-runtime/src/constants/index.js +12 -0
- package/dist/constraint-runtime/src/constraint/budget.js +27 -0
- package/dist/constraint-runtime/src/constraint/budget.js.map +1 -0
- package/dist/constraint-runtime/src/constraint/index.js +2 -0
- package/dist/constraint-runtime/src/constraint/permission.js +25 -0
- package/dist/constraint-runtime/src/constraint/permission.js.map +1 -0
- package/dist/constraint-runtime/src/context.js +30 -0
- package/dist/constraint-runtime/src/coordinator/index.js +12 -0
- package/dist/constraint-runtime/src/cost_hook.js +4 -0
- package/dist/constraint-runtime/src/cost_tracker.js +8 -0
- package/dist/constraint-runtime/src/deferred_init.js +10 -0
- package/dist/constraint-runtime/src/direct_modes.js +6 -0
- package/dist/constraint-runtime/src/dynamic-tool-loader.js +85 -0
- package/dist/constraint-runtime/src/entrypoints/index.js +12 -0
- package/dist/constraint-runtime/src/execution_registry.js +44 -0
- package/dist/constraint-runtime/src/history.js +9 -0
- package/dist/constraint-runtime/src/hooks/index.js +12 -0
- package/dist/constraint-runtime/src/index.js +26 -0
- package/dist/constraint-runtime/src/ink.js +4 -0
- package/dist/constraint-runtime/src/keybindings/index.js +12 -0
- package/dist/constraint-runtime/src/memdir/index.js +12 -0
- package/dist/constraint-runtime/src/migrations/index.js +12 -0
- package/dist/constraint-runtime/src/models.js +3 -0
- package/dist/constraint-runtime/src/models.js.map +1 -0
- package/dist/constraint-runtime/src/moreright/index.js +12 -0
- package/dist/constraint-runtime/src/native_ts/index.js +12 -0
- package/dist/constraint-runtime/src/output_styles/index.js +12 -0
- package/dist/constraint-runtime/src/parity_audit.js +12 -0
- package/dist/constraint-runtime/src/plugins/index.js +12 -0
- package/dist/constraint-runtime/src/port_manifest.js +11 -0
- package/dist/constraint-runtime/src/prefetch.js +9 -0
- package/dist/constraint-runtime/src/query.js +1 -0
- package/dist/constraint-runtime/src/remote/index.js +12 -0
- package/dist/constraint-runtime/src/remote_runtime.js +9 -0
- package/dist/constraint-runtime/src/runtime/index.js +1 -0
- package/dist/constraint-runtime/src/runtime/session.js +35 -0
- package/dist/constraint-runtime/src/schemas/index.js +12 -0
- package/dist/constraint-runtime/src/screens/index.js +12 -0
- package/dist/constraint-runtime/src/server/index.js +12 -0
- package/dist/constraint-runtime/src/services/index.js +12 -0
- package/dist/constraint-runtime/src/session_store.js +22 -0
- package/dist/constraint-runtime/src/setup.js +30 -0
- package/dist/constraint-runtime/src/skills/index.js +1 -0
- package/dist/constraint-runtime/src/skills/skill-registry.js +28 -0
- package/dist/constraint-runtime/src/state/index.js +12 -0
- package/dist/constraint-runtime/src/system_init.js +20 -0
- package/dist/constraint-runtime/src/thinking/engine.js +42 -0
- package/dist/constraint-runtime/src/thinking/index.js +1 -0
- package/dist/constraint-runtime/src/tool_pool.js +8 -0
- package/dist/constraint-runtime/src/tools/OpenCLI/execAdapter.js +7 -0
- package/dist/constraint-runtime/src/tools/OpenCLI/listAdapters.js +7 -0
- package/dist/constraint-runtime/src/tools/OpenCLI/runCommand.js +7 -0
- package/dist/constraint-runtime/src/tools/PolymarketSDK/cancelOrder.js +6 -0
- package/dist/constraint-runtime/src/tools/PolymarketSDK/createOrder.js +6 -0
- package/dist/constraint-runtime/src/tools/PolymarketSDK/getMarket.js +5 -0
- package/dist/constraint-runtime/src/tools/PolymarketSDK/getOrders.js +6 -0
- package/dist/constraint-runtime/src/tools/PolymarketSDK/listMarkets.js +4 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/confirmTransaction.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/createTransaction.js +8 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/deploySafe.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/executeTransaction.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/getBalance.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/getPendingTransactions.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/proposeTransaction.js +6 -0
- package/dist/constraint-runtime/src/tools/WalletTools/autoPay.js +29 -0
- package/dist/constraint-runtime/src/tools/WalletTools/createWallet.js +10 -0
- package/dist/constraint-runtime/src/tools/WalletTools/getBalance.js +13 -0
- package/dist/constraint-runtime/src/tools/WalletTools/importWallet.js +22 -0
- package/dist/constraint-runtime/src/tools/WalletTools/sendTransaction.js +25 -0
- package/dist/constraint-runtime/src/tools/WalletTools/signMessage.js +10 -0
- package/dist/constraint-runtime/src/tools/WalletTools/transferToken.js +25 -0
- package/dist/constraint-runtime/src/tools.js +70 -0
- package/dist/constraint-runtime/src/transcript.js +19 -0
- package/dist/constraint-runtime/src/types/index.js +12 -0
- package/dist/constraint-runtime/src/upstream_proxy/index.js +12 -0
- package/dist/constraint-runtime/src/utils/index.js +12 -0
- package/dist/constraint-runtime/src/vim/index.js +12 -0
- package/dist/constraint-runtime/src/voice/index.js +12 -0
- package/dist/constraint-runtime/tests/agent.test.js +16 -0
- package/dist/constraint-runtime/tests/constraint.test.js +41 -0
- package/dist/constraint-runtime/tests/skill.test.js +19 -0
- package/dist/constraint-runtime/tests/thinking.test.js +22 -0
- package/dist/constraints/commands.js +100 -0
- package/dist/constraints/index.js +11 -0
- package/dist/constraints/index.js.map +1 -0
- package/dist/constraints/permissions.js +37 -0
- package/dist/constraints/runtime.js +135 -0
- package/dist/constraints/session.js +48 -0
- package/dist/constraints/system-init.js +51 -0
- package/dist/constraints/tools.js +104 -0
- package/dist/documents/reader.js +104 -0
- package/dist/documents/reader.js.map +1 -0
- package/dist/electron-preload.js +15 -0
- package/dist/electron-preload.js.map +1 -0
- package/dist/electron.js +206 -0
- package/dist/electron.js.map +1 -0
- package/dist/index.js +1053 -0
- package/dist/llm/config-store.js +301 -0
- package/dist/llm/config-store.js.map +1 -0
- package/dist/llm/minimax-provider.js +46 -0
- package/dist/llm/minimax.js +45 -0
- package/dist/llm/pi-ai.js +471 -0
- package/dist/llm/pi-ai.js.map +1 -0
- package/dist/network/agent-network.js +692 -0
- package/dist/network/agent-network.js.map +1 -0
- package/dist/network/iroh-integration.js +117 -0
- package/dist/network/iroh-integration.js.map +1 -0
- package/dist/network/iroh-transport.js +506 -0
- package/dist/network/iroh-transport.js.map +1 -0
- package/dist/network/p2p.js +864 -0
- package/dist/network/p2p.js.map +1 -0
- package/dist/network/storage/adapters/json-adapter.js +401 -0
- package/dist/network/storage/adapters/json-adapter.js.map +1 -0
- package/dist/pi-ecosystem-colony/index.js +426 -0
- package/dist/pi-ecosystem-colony/index.js.map +1 -0
- package/dist/pi-ecosystem-goals/index.js +512 -0
- package/dist/pi-ecosystem-goals/index.js.map +1 -0
- package/dist/pi-ecosystem-judgment/decision.js +318 -0
- package/dist/pi-ecosystem-judgment/decision.js.map +1 -0
- package/dist/pi-ecosystem-judgment/distillation.js +304 -0
- package/dist/pi-ecosystem-judgment/distillation.js.map +1 -0
- package/dist/pi-ecosystem-judgment/human-value-store.js +463 -0
- package/dist/pi-ecosystem-judgment/human-value-store.js.map +1 -0
- package/dist/pi-ecosystem-judgment/index.js +581 -0
- package/dist/pi-ecosystem-judgment/index.js.map +1 -0
- package/dist/pi-ecosystem-judgment/value-injection.js +592 -0
- package/dist/pi-ecosystem-judgment/value-injection.js.map +1 -0
- package/dist/pi-ecosystem-mcp/index.js +385 -0
- package/dist/pi-ecosystem-mcp/index.js.map +1 -0
- package/dist/pi-ecosystem-subagents/index.js +352 -0
- package/dist/pi-ecosystem-subagents/index.js.map +1 -0
- package/dist/runtime/context/minimax-prompt.js +178 -0
- package/dist/runtime/context/sys-prompt.js +1 -0
- package/dist/social/ant-colony/AdaptiveHeartbeat.js +106 -0
- package/dist/social/ant-colony/AdaptiveHeartbeat.js.map +1 -0
- package/dist/social/ant-colony/PheromoneEngine.js +265 -0
- package/dist/social/ant-colony/PheromoneEngine.js.map +1 -0
- package/dist/social/ant-colony/types.js +28 -0
- package/dist/social/ant-colony/types.js.map +1 -0
- package/dist/social/channels/ChannelManager.js +444 -0
- package/dist/social/channels/ChannelManager.js.map +1 -0
- package/dist/social/channels/DiapChannelBridge.js +451 -0
- package/dist/social/channels/DiapChannelBridge.js.map +1 -0
- package/dist/social/channels/InterestMatcher.js +135 -0
- package/dist/social/channels/InterestMatcher.js.map +1 -0
- package/dist/social/channels/types.js +21 -0
- package/dist/social/channels/types.js.map +1 -0
- package/dist/social/global-shared-context.js +368 -0
- package/dist/social/global-shared-context.js.map +1 -0
- package/dist/social/heartbeat.js +865 -0
- package/dist/social/heartbeat.js.map +1 -0
- package/dist/test/constraint-layer.test.js +164 -0
- package/dist/test/global-shared-context.test.js +315 -0
- package/dist/test/pi-sdk.test.js +47 -0
- package/dist/test/set-persona.test.js +38 -0
- package/dist/test/subagent-manager.test.js +276 -0
- package/dist/test/workflow-engine.test.js +87 -0
- package/dist/web/api-config.html +265 -0
- package/dist/web/client.js +1401 -0
- package/dist/web/components/p2p/index.js +357 -0
- package/dist/web/components/p2p/p2p-connection.js +161 -0
- package/dist/web/components/p2p/p2p-identity.js +78 -0
- package/dist/web/components/p2p/p2p-manager.js +82 -0
- package/dist/web/components/p2p/p2p-messages.js +186 -0
- package/dist/web/components/p2p/p2p-store-memory.js +129 -0
- package/dist/web/components/p2p/types.js +23 -0
- package/dist/web/index.html +192 -0
- package/dist/web/server.js +1647 -0
- package/dist/web/server.js.map +1 -0
- package/dist/web/style.css +3260 -0
- package/dist/workflows/collaboration.js +374 -0
- package/dist/workflows/index.js +54 -0
- package/docs/agent-communication.md +333 -0
- package/docs/plans/2026-05-15-document-agent-design.md +479 -0
- package/docs/plans/2026-05-15-document-agent-implementation-plan.md +792 -0
- package/docs/plans/2026-05-16-chat-ui-design.md +86 -0
- package/docs/plans/2026-05-16-constraint-runtime-design.md +106 -0
- package/docs/plans/2026-05-16-constraint-runtime-implementation.md +441 -0
- package/docs//346/225/260/345/255/246/350/276/205/345/212/251/346/231/272/350/203/275/344/275/223-/346/240/270/345/277/203/346/225/210/346/236/234/345/256/232/344/271/211.md +287 -0
- package/package.json +121 -0
- package/scripts/build-web.ts +61 -0
- package/src/agents/constraint-layer.ts +309 -0
- package/src/agents/pi-sdk.ts +1591 -0
- package/src/agents/protocol.ts +304 -0
- package/src/agents/subagent-manager.ts +553 -0
- package/src/agents/workflow-engine.ts +332 -0
- package/src/bollharness/.boll/CLAUDE.md.template +34 -0
- package/src/bollharness/.boll/MANIFEST.yaml +213 -0
- package/src/bollharness/.boll/active-review-agents/.gitkeep +0 -0
- package/src/bollharness/.boll/agents/review-base.yaml +108 -0
- package/src/bollharness/.boll/deploy-allowlist.yaml +38 -0
- package/src/bollharness/.boll/inbox/schema/message-v1.json +99 -0
- package/src/bollharness/.boll/install-staging/.gitkeep +0 -0
- package/src/bollharness/.boll/issue-adapter.yaml +31 -0
- package/src/bollharness/.boll/plugins/boll-mode-toolkit/contracts/mode-contract.md +85 -0
- package/src/bollharness/.boll/plugins/boll-review-toolkit/contracts/evidence-packet-schema.json +102 -0
- package/src/bollharness/.boll/plugins/boll-review-toolkit/contracts/review-contract.yaml +247 -0
- package/src/bollharness/.boll/rules/backend-routes.md +31 -0
- package/src/bollharness/.boll/rules/closure-semantics.md +30 -0
- package/src/bollharness/.boll/rules/env-vars.md +32 -0
- package/src/bollharness/.boll/rules/hanis-protocol.md +145 -0
- package/src/bollharness/.boll/rules/repo-structure.md +42 -0
- package/src/bollharness/.boll/rules/review-agent-isolation.md +73 -0
- package/src/bollharness/.boll/rules/source-of-truth.md +33 -0
- package/src/bollharness/.boll/settings.json +180 -0
- package/src/bollharness/.boll/settings.json.template +31 -0
- package/src/bollharness/.boll/skills/arch/SKILL.md +372 -0
- package/src/bollharness/.boll/skills/bug-pipeline/SKILL.md +168 -0
- package/src/bollharness/.boll/skills/bug-triage/SKILL.md +161 -0
- package/src/bollharness/.boll/skills/context-chains/SKILL.md +250 -0
- package/src/bollharness/.boll/skills/context-chains/context-chain-index.md +48 -0
- package/src/bollharness/.boll/skills/context-chains/work-type-extractors/code_change_extractor.ts +142 -0
- package/src/bollharness/.boll/skills/context-chains/work-type-extractors/debugging_extractor.ts +126 -0
- package/src/bollharness/.boll/skills/context-chains/work-type-extractors/design_extractor.ts +148 -0
- package/src/bollharness/.boll/skills/context-chains/work-type-extractors/planning_extractor.ts +162 -0
- package/src/bollharness/.boll/skills/context-chains/work-type-extractors/question_extractor.ts +116 -0
- package/src/bollharness/.boll/skills/context-chains/work-type-extractors/review_extractor.ts +136 -0
- package/src/bollharness/.boll/skills/crystal-learn/SKILL.md +93 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-0.md +34 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-1.md +34 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-2.md +35 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-3.md +34 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-4.md +43 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-5.md +34 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-6.md +37 -0
- package/src/bollharness/.boll/skills/crystal-learn/invariants/INV-7.md +46 -0
- package/src/bollharness/.boll/skills/guardian-fixer/PROMPT.md +415 -0
- package/src/bollharness/.boll/skills/guardian-fixer/SKILL.md +320 -0
- package/src/bollharness/.boll/skills/harness-dev/SKILL.md +93 -0
- package/src/bollharness/.boll/skills/harness-dev/examples/README.md +227 -0
- package/src/bollharness/.boll/skills/harness-dev-handoff/SKILL.md +165 -0
- package/src/bollharness/.boll/skills/harness-eng/SKILL.md +110 -0
- package/src/bollharness/.boll/skills/harness-eng-test/SKILL.md +79 -0
- package/src/bollharness/.boll/skills/harness-lab/SKILL.md +170 -0
- package/src/bollharness/.boll/skills/harness-ops/SKILL.md +57 -0
- package/src/bollharness/.boll/skills/harness-voice/SKILL.md +183 -0
- package/src/bollharness/.boll/skills/judgment/SKILL.md +115 -0
- package/src/bollharness/.boll/skills/lead/SKILL.md +245 -0
- package/src/bollharness/.boll/skills/lead/install-wow-harness.md +77 -0
- package/src/bollharness/.boll/skills/lead/ref-review-sop.md +91 -0
- package/src/bollharness/.boll/skills/lead/ref-stages.md +129 -0
- package/src/bollharness/.boll/skills/skill-discovery/SKILL.md +169 -0
- package/src/bollharness/.boll/skills/task-arch/SKILL.md +106 -0
- package/src/bollharness/.boll/skills/toolkit/SKILL.md +57 -0
- package/src/bollharness/.boll/tasks/.gitkeep +0 -0
- package/src/bollharness/.boll/toolkit-index.yaml +112 -0
- package/src/bollharness/.claude/agents/review-base.yaml +108 -0
- package/src/bollharness/.claude/plugins/boll-mode-toolkit/.claude-plugin/plugin.json +44 -0
- package/src/bollharness/.claude/plugins/boll-review-toolkit/.claude-plugin/plugin.json +24 -0
- package/src/bollharness/.claude/plugins/boll-review-toolkit/contracts/evidence-packet-schema.json +102 -0
- package/src/bollharness/.claude/plugins/boll-review-toolkit/contracts/review-contract.yaml +247 -0
- package/src/bollharness/.claude/settings.json +157 -0
- package/src/bollharness/.claude/skills/arch/SKILL.md +64 -0
- package/src/bollharness/.claude/skills/crystal-learn/SKILL.md +93 -0
- package/src/bollharness/.claude/skills/guardian-fixer/PROMPT.md +44 -0
- package/src/bollharness/.claude/skills/guardian-fixer/SKILL.md +324 -0
- package/src/bollharness/.claude/skills/harness-dev/SKILL.md +93 -0
- package/src/bollharness/.claude/skills/harness-dev/examples/README.md +17 -0
- package/src/bollharness/.claude/skills/harness-dev-handoff/SKILL.md +165 -0
- package/src/bollharness/.claude/skills/harness-eng/SKILL.md +183 -0
- package/src/bollharness/.claude/skills/harness-eng-test/SKILL.md +57 -0
- package/src/bollharness/.claude/skills/harness-ops/SKILL.md +57 -0
- package/src/bollharness/.claude/skills/harness-voice/SKILL.md +84 -0
- package/src/bollharness/.claude/skills/lead/INDEX.md +28 -0
- package/src/bollharness/.claude/skills/lead/SKILL.md +24 -0
- package/src/bollharness/.claude/skills/lead/install-wow-harness.md +77 -0
- package/src/bollharness/.claude/skills/lead/ref-review-sop.md +48 -0
- package/src/bollharness/.claude/skills/lead/ref-stages.md +58 -0
- package/src/bollharness/.claude/skills/plan-lock/SKILL.md +74 -0
- package/src/bollharness/.claude/skills/skill-discovery/SKILL.md +120 -0
- package/src/bollharness/.claude/skills/task-arch/SKILL.md +106 -0
- package/src/bollharness/.claude/skills/toolkit/SKILL.md +57 -0
- package/src/bollharness/.claude/skills/toolkit/list.sh +92 -0
- package/src/bollharness/.githooks/pre-commit +21 -0
- package/src/bollharness/.github/workflows/ci.yml +88 -0
- package/src/bollharness/.sanitize-report.json +13 -0
- package/src/bollharness/CLAUDE.md +73 -0
- package/src/bollharness/LICENSE +21 -0
- package/src/bollharness/README.md +143 -0
- package/src/bollharness/README.zh-CN.md +131 -0
- package/src/bollharness/docs/decisions/ADR-030-guard-signal-protocol-and-governance-reload.md +1076 -0
- package/src/bollharness/docs/decisions/ADR-038-harness-optimization-strategy.md +2039 -0
- package/src/bollharness/docs/decisions/ADR-041-codex-claude-code-division-of-labor.md +128 -0
- package/src/bollharness/docs/decisions/ADR-H1-crystal-learn-revival.md +188 -0
- package/src/bollharness/docs/decisions/ADR-H2-identity-isolation.md +183 -0
- package/src/bollharness/docs/decisions/ADR-H3-memory-scope.md +133 -0
- package/src/bollharness/docs/decisions/ADR-H4-prompt-governance.md +146 -0
- package/src/bollharness/docs/decisions/ADR-H5-gate-quantization.md +212 -0
- package/src/bollharness/docs/decisions/ADR-H6-state-file-health.md +211 -0
- package/src/bollharness/docs/decisions/ADR-H8-issue-and-doc-compliance.md +202 -0
- package/src/bollharness/docs/decisions/ADR-H9-mailbox.md +231 -0
- package/src/bollharness/docs/decisions/PLAN-H1-crystal-learn-revival.md +270 -0
- package/src/bollharness/docs/decisions/PLAN-H2-identity-isolation.md +291 -0
- package/src/bollharness/docs/decisions/PLAN-H3-memory-scope.md +228 -0
- package/src/bollharness/docs/decisions/PLAN-H4-prompt-governance.md +227 -0
- package/src/bollharness/docs/decisions/PLAN-H5-gate-quantization.md +239 -0
- package/src/bollharness/docs/decisions/PLAN-H6-state-file-health.md +325 -0
- package/src/bollharness/docs/decisions/PLAN-H8-issue-and-doc-compliance.md +242 -0
- package/src/bollharness/docs/decisions/PLAN-H9-mailbox.md +378 -0
- package/src/bollharness/docs/launch-article-en.md +276 -0
- package/src/bollharness/docs/launch-article-zh.md +305 -0
- package/src/bollharness/docs/practice.html +356 -0
- package/src/bollharness/docs/practice.md +82 -0
- package/src/bollharness/docs/research/round-1/README.md +11 -0
- package/src/bollharness/docs/research/round-2/README.md +11 -0
- package/src/bollharness/docs/research/round-3/README.md +11 -0
- package/src/bollharness/docs/research/round-4/README.md +11 -0
- package/src/bollharness/docs/research/round-5/README.md +11 -0
- package/src/bollharness/docs/research/round-6/README.md +11 -0
- package/src/bollharness/package-lock.json +48 -0
- package/src/bollharness/package.json +20 -0
- package/src/bollharness/reference/SOURCE-COMMIT.txt +3 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/backend-routes.md +268 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/bridge.md +20 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/closure-semantics.md +30 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/coaching.md +13 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/env-vars.md +50 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/hackathon.md +12 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/repo-structure.md +184 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/review-agent-isolation.md +112 -0
- package/src/bollharness/reference/boll-reference/.claude/rules/scenes.md +12 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/arch/SKILL.md +551 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-animation/SKILL.md +26 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-bridge/SKILL.md +227 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-bridge/agents/openai.yaml +4 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-bridge/references/bridge-failure-taxonomy.md +142 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-bridge/references/bridge-validation-ladder.md +107 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-crystal/SKILL.md +893 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-crystal-learn/SKILL.md +89 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-dev/SKILL.md +93 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-dev/examples/README.md +209 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-dev-handoff/SKILL.md +165 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-eng/SKILL.md +110 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-eng-frontend/SKILL.md +203 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-eng-hdc/SKILL.md +27 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-eng-orchestrator/SKILL.md +28 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-eng-prompt/SKILL.md +27 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-eng-test/SKILL.md +79 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-lab/SKILL.md +372 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-run/SKILL.md +437 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-ux-appstore/SKILL.md +27 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/boll-voice/SKILL.md +442 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/guardian-fixer/PROMPT.md +421 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/guardian-fixer/SKILL.md +326 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/lead/SKILL.md +155 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/lead/ref-review-sop.md +91 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/lead/ref-stages.md +129 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/nature-designer/output/skill-map-preview.png +0 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/nature-designer/output/skill-map-v2.png +0 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/nature-designer/output/skill-map-v3.png +0 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/nature-designer/output/skill-map-v4.png +0 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/plan-lock/SKILL.md +425 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/plan-lock/ref-three-checks.md +62 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/plan-lock/ref-wp-templates.md +78 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/task-arch/SKILL.md +76 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/vibedevteam-graph/SKILL.md +57 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/vibedevteam-graph/beads-graph.sh +153 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/vibedevteam-init/SKILL.md +52 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/vibedevteam-init/beads-auto-link.sh +76 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/vibedevteam-sync/SKILL.md +50 -0
- package/src/bollharness/reference/boll-reference/.claude/skills/vibedevteam-sync/beads-sync-proj.sh +108 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/AGENT-PROFILE.md +151 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/COST-STRUCTURE.md +56 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/INDEX.md +76 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/MODULE1-INTENT-FIELD.md +116 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/MODULE2-CRYSTALLIZATION.md +200 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/PRINCIPLES.md +84 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/PROTOCOL-CORE.md +209 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/VISION.md +181 -0
- package/src/bollharness/reference/boll-reference/docs/architecture/discussions/D-01-MARKET-SCENE-PROTOCOL.md +754 -0
- package/src/bollharness/reference/boll-reference/scripts/hooks/.sanitize-report.json +12 -0
- package/src/bollharness/reference/boll-reference/scripts/hooks/find-boll-root.sh +27 -0
- package/src/bollharness/reference/boll-reference/scripts/hooks/precompact.sh +57 -0
- package/src/bollharness/reference/boll-reference/scripts/hooks/stop-evaluator.md +57 -0
- package/src/bollharness/schemas/metrics-jsonl-allowlist.json +67 -0
- package/src/bollharness/scripts/checks/next_decision_number.sh +48 -0
- package/src/bollharness/scripts/ci/count-components.sh +65 -0
- package/src/bollharness/scripts/context-fragments/artifact-linkage.md +14 -0
- package/src/bollharness/scripts/context-fragments/auth-consumers.md +17 -0
- package/src/bollharness/scripts/context-fragments/bridge-constitution.md +13 -0
- package/src/bollharness/scripts/context-fragments/catalyst-distributed.md +18 -0
- package/src/bollharness/scripts/context-fragments/closure-checklist.md +13 -0
- package/src/bollharness/scripts/context-fragments/contract-consumers.md +15 -0
- package/src/bollharness/scripts/context-fragments/db-shared-structures.md +15 -0
- package/src/bollharness/scripts/context-fragments/fixed-three-layers.md +19 -0
- package/src/bollharness/scripts/context-fragments/general-dev-principles.md +11 -0
- package/src/bollharness/scripts/context-fragments/issue-first.md +8 -0
- package/src/bollharness/scripts/context-fragments/mcp-parity.md +16 -0
- package/src/bollharness/scripts/context-fragments/pi-agent-operations.md +74 -0
- package/src/bollharness/scripts/context-fragments/protocol-consumers.md +15 -0
- package/src/bollharness/scripts/context-fragments/run-events-consumers.md +15 -0
- package/src/bollharness/scripts/context-fragments/scene-fidelity.md +13 -0
- package/src/bollharness/scripts/context-fragments/truth-source-hierarchy.md +15 -0
- package/src/bollharness/scripts/context-fragments/two-language.md +15 -0
- package/src/bollharness/scripts/context-fragments/version-sources.md +14 -0
- package/src/bollharness/scripts/hooks/find-project-root.sh +47 -0
- package/src/bollharness/scripts/hooks/inbox-poll.sh +78 -0
- package/src/bollharness/scripts/hooks/precompact.sh +56 -0
- package/src/bollharness/scripts/hooks/stop-evaluator.md +83 -0
- package/src/bollharness/scripts/sync-from-upstream.sh +281 -0
- package/src/bollharness/src/index.ts +5 -0
- package/src/bollharness/src/scripts/checks/check_adr_plan_numbering.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_api_types.ts +52 -0
- package/src/bollharness/src/scripts/checks/check_artifact_link.ts +156 -0
- package/src/bollharness/src/scripts/checks/check_bridge_deps.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_bugfix_binding.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_bugfix_binding_ci.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_doc_file_references.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_doc_freshness.ts +141 -0
- package/src/bollharness/src/scripts/checks/check_doc_links.ts +36 -0
- package/src/bollharness/src/scripts/checks/check_file_existence_claims.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_fragment_integrity.ts +40 -0
- package/src/bollharness/src/scripts/checks/check_hook_installed.ts +70 -0
- package/src/bollharness/src/scripts/checks/check_issue_closure.ts +51 -0
- package/src/bollharness/src/scripts/checks/check_mcp_parity.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_security.ts +54 -0
- package/src/bollharness/src/scripts/checks/check_skill_parity.ts +11 -0
- package/src/bollharness/src/scripts/checks/check_versions.ts +11 -0
- package/src/bollharness/src/scripts/checks/finding.ts +35 -0
- package/src/bollharness/src/scripts/checks/next_decision_number.ts +24 -0
- package/src/bollharness/src/scripts/checks/regenerate_magic_docs.ts +11 -0
- package/src/bollharness/src/scripts/ci/detect_rebaseline_triggers.ts +14 -0
- package/src/bollharness/src/scripts/ci/scan_subprocess_cfg.ts +14 -0
- package/src/bollharness/src/scripts/ci/scan_verify_artifacts.ts +14 -0
- package/src/bollharness/src/scripts/ci/scan_yaml_schema.ts +14 -0
- package/src/bollharness/src/scripts/context_router.ts +76 -0
- package/src/bollharness/src/scripts/deploy-guard.ts +182 -0
- package/src/bollharness/src/scripts/guard-feedback.ts +215 -0
- package/src/bollharness/src/scripts/guard_router.ts +194 -0
- package/src/bollharness/src/scripts/hooks/_hook_output.js +3 -0
- package/src/bollharness/src/scripts/hooks/_hook_output.ts +11 -0
- package/src/bollharness/src/scripts/hooks/auto-python3.ts +10 -0
- package/src/bollharness/src/scripts/hooks/deploy-progress-on-session-end.ts +10 -0
- package/src/bollharness/src/scripts/hooks/failure-analyzer.ts +10 -0
- package/src/bollharness/src/scripts/hooks/gate-judgment-inject.ts +111 -0
- package/src/bollharness/src/scripts/hooks/gate-transition-judgment.ts +74 -0
- package/src/bollharness/src/scripts/hooks/inbox-ack.ts +10 -0
- package/src/bollharness/src/scripts/hooks/inbox-inject-on-start.ts +10 -0
- package/src/bollharness/src/scripts/hooks/inbox-validate.ts +10 -0
- package/src/bollharness/src/scripts/hooks/inbox-write-ledger.ts +10 -0
- package/src/bollharness/src/scripts/hooks/initializer-agent.ts +10 -0
- package/src/bollharness/src/scripts/hooks/loop-detection.ts +83 -0
- package/src/bollharness/src/scripts/hooks/owner-guard.ts +10 -0
- package/src/bollharness/src/scripts/hooks/precompact.ts +10 -0
- package/src/bollharness/src/scripts/hooks/review-agent-gatekeeper.ts +10 -0
- package/src/bollharness/src/scripts/hooks/risk-tracker.ts +121 -0
- package/src/bollharness/src/scripts/hooks/sanitize-on-read.ts +10 -0
- package/src/bollharness/src/scripts/hooks/session-reflection.ts +12 -0
- package/src/bollharness/src/scripts/hooks/session-start-magic-docs.ts +12 -0
- package/src/bollharness/src/scripts/hooks/session-start-reset-risk.ts +12 -0
- package/src/bollharness/src/scripts/hooks/session-start-toolkit-reminder.ts +12 -0
- package/src/bollharness/src/scripts/hooks/stop-evaluator.ts +164 -0
- package/src/bollharness/src/scripts/hooks/tool-call-counter.ts +10 -0
- package/src/bollharness/src/scripts/hooks/trace-analyzer.ts +14 -0
- package/src/bollharness/src/scripts/install/install-trust-token.ts +13 -0
- package/src/bollharness/src/scripts/install/multi_project_registry.ts +13 -0
- package/src/bollharness/src/scripts/install/phase2_auto.ts +28 -0
- package/src/bollharness/src/scripts/install/pre_commit_installer.ts +10 -0
- package/src/bollharness/src/scripts/install/tier_selector.ts +10 -0
- package/src/bollharness/src/scripts/install/transcript_miner.ts +13 -0
- package/src/bollharness/src/scripts/lib/claim_patterns.ts +11 -0
- package/src/bollharness/src/scripts/lib/sanitize_patterns.ts +13 -0
- package/src/bollharness/src/scripts/sanitize.ts +9 -0
- package/src/bollharness/templates/persona/default.json +19 -0
- package/src/bollharness/templates/scaffold/.boll/guard/.gitkeep +0 -0
- package/src/bollharness/templates/scaffold/.boll/metrics/.gitkeep +0 -0
- package/src/bollharness/templates/scaffold/.boll/state/.gitkeep +0 -0
- package/src/bollharness/templates/scaffold/.gitignore.append +16 -0
- package/src/bollharness/templates/scaffold/CLAUDE.md +89 -0
- package/src/bollharness/templates/scaffold/docs/INDEX.md +3 -0
- package/src/bollharness/templates/scaffold/docs/decisions/ADR_TEMPLATE.md +38 -0
- package/src/bollharness/templates/scaffold/docs/decisions/PLAN_TEMPLATE.md +45 -0
- package/src/bollharness/templates/scaffold/docs/decisions/tasks/.gitkeep +2 -0
- package/src/bollharness/templates/scaffold/docs/issues/.gitkeep +0 -0
- package/src/bollharness/templates/scaffold/docs/issues/GUARD_ISSUE_TEMPLATE.md +35 -0
- package/src/bollharness/templates/scaffold/docs/issues/ISSUE_TEMPLATE.md +51 -0
- package/src/bollharness/tsconfig.json +26 -0
- package/src/bollharness-integration/channel-judgment-engine.ts +634 -0
- package/src/bollharness-integration/context-chain-router.ts +474 -0
- package/src/bollharness-integration/context-router-judgment.ts +339 -0
- package/src/bollharness-integration/context-router.ts +583 -0
- package/src/bollharness-integration/gate-state-machine.ts +444 -0
- package/src/bollharness-integration/gate-transition-hooks.ts +137 -0
- package/src/bollharness-integration/guard-checker.ts +451 -0
- package/src/bollharness-integration/index.ts +219 -0
- package/src/bollharness-integration/integration.ts +538 -0
- package/src/bollharness-integration/judgment-prompts.yaml +535 -0
- package/src/bollharness-integration/llm-judgment-engine.ts +712 -0
- package/src/bollharness-integration/skill-adapter.ts +646 -0
- package/src/cli/interface.ts +211 -0
- package/src/constraint-runtime/package-lock.json +48 -0
- package/src/constraint-runtime/package.json +34 -0
- package/src/constraint-runtime/src/_archive_helper.ts +16 -0
- package/src/constraint-runtime/src/agent/coordinator.ts +71 -0
- package/src/constraint-runtime/src/agent/index.ts +1 -0
- package/src/constraint-runtime/src/assistant/index.ts +15 -0
- package/src/constraint-runtime/src/bootstrap/index.ts +15 -0
- package/src/constraint-runtime/src/bootstrap_graph.ts +17 -0
- package/src/constraint-runtime/src/bridge/index.ts +15 -0
- package/src/constraint-runtime/src/buddy/index.ts +15 -0
- package/src/constraint-runtime/src/cli/index.ts +15 -0
- package/src/constraint-runtime/src/command_graph.ts +20 -0
- package/src/constraint-runtime/src/commands.ts +83 -0
- package/src/constraint-runtime/src/components/index.ts +15 -0
- package/src/constraint-runtime/src/constants/index.ts +15 -0
- package/src/constraint-runtime/src/constraint/budget.ts +25 -0
- package/src/constraint-runtime/src/constraint/index.ts +3 -0
- package/src/constraint-runtime/src/constraint/permission.ts +28 -0
- package/src/constraint-runtime/src/context.ts +45 -0
- package/src/constraint-runtime/src/coordinator/index.ts +15 -0
- package/src/constraint-runtime/src/cost_hook.ts +6 -0
- package/src/constraint-runtime/src/cost_tracker.ts +9 -0
- package/src/constraint-runtime/src/deferred_init.ts +18 -0
- package/src/constraint-runtime/src/direct_modes.ts +13 -0
- package/src/constraint-runtime/src/dynamic-tool-loader.ts +115 -0
- package/src/constraint-runtime/src/entrypoints/index.ts +15 -0
- package/src/constraint-runtime/src/execution_registry.ts +41 -0
- package/src/constraint-runtime/src/history.ts +16 -0
- package/src/constraint-runtime/src/hooks/index.ts +15 -0
- package/src/constraint-runtime/src/index.ts +28 -0
- package/src/constraint-runtime/src/ink.ts +4 -0
- package/src/constraint-runtime/src/keybindings/index.ts +15 -0
- package/src/constraint-runtime/src/memdir/index.ts +15 -0
- package/src/constraint-runtime/src/migrations/index.ts +15 -0
- package/src/constraint-runtime/src/models.ts +49 -0
- package/src/constraint-runtime/src/moreright/index.ts +15 -0
- package/src/constraint-runtime/src/native_ts/index.ts +15 -0
- package/src/constraint-runtime/src/output_styles/index.ts +15 -0
- package/src/constraint-runtime/src/parity_audit.ts +23 -0
- package/src/constraint-runtime/src/plugins/index.ts +15 -0
- package/src/constraint-runtime/src/port_manifest.ts +20 -0
- package/src/constraint-runtime/src/prefetch.ts +17 -0
- package/src/constraint-runtime/src/query.ts +7 -0
- package/src/constraint-runtime/src/reference_data/archive_surface_snapshot.json +63 -0
- package/src/constraint-runtime/src/reference_data/commands_snapshot.json +1037 -0
- package/src/constraint-runtime/src/reference_data/subsystems/OpenCLI.json +10 -0
- package/src/constraint-runtime/src/reference_data/subsystems/PolymarketSDK.json +12 -0
- package/src/constraint-runtime/src/reference_data/subsystems/SafeSDK.json +14 -0
- package/src/constraint-runtime/src/reference_data/subsystems/assistant.json +8 -0
- package/src/constraint-runtime/src/reference_data/subsystems/bootstrap.json +8 -0
- package/src/constraint-runtime/src/reference_data/subsystems/bridge.json +32 -0
- package/src/constraint-runtime/src/reference_data/subsystems/buddy.json +13 -0
- package/src/constraint-runtime/src/reference_data/subsystems/cli.json +26 -0
- package/src/constraint-runtime/src/reference_data/subsystems/components.json +32 -0
- package/src/constraint-runtime/src/reference_data/subsystems/constants.json +28 -0
- package/src/constraint-runtime/src/reference_data/subsystems/coordinator.json +8 -0
- package/src/constraint-runtime/src/reference_data/subsystems/entrypoints.json +15 -0
- package/src/constraint-runtime/src/reference_data/subsystems/hooks.json +32 -0
- package/src/constraint-runtime/src/reference_data/subsystems/keybindings.json +21 -0
- package/src/constraint-runtime/src/reference_data/subsystems/memdir.json +15 -0
- package/src/constraint-runtime/src/reference_data/subsystems/migrations.json +18 -0
- package/src/constraint-runtime/src/reference_data/subsystems/moreright.json +8 -0
- package/src/constraint-runtime/src/reference_data/subsystems/native_ts.json +11 -0
- package/src/constraint-runtime/src/reference_data/subsystems/outputStyles.json +8 -0
- package/src/constraint-runtime/src/reference_data/subsystems/plugins.json +9 -0
- package/src/constraint-runtime/src/reference_data/subsystems/remote.json +11 -0
- package/src/constraint-runtime/src/reference_data/subsystems/schemas.json +8 -0
- package/src/constraint-runtime/src/reference_data/subsystems/screens.json +10 -0
- package/src/constraint-runtime/src/reference_data/subsystems/server.json +10 -0
- package/src/constraint-runtime/src/reference_data/subsystems/services.json +32 -0
- package/src/constraint-runtime/src/reference_data/subsystems/skills.json +27 -0
- package/src/constraint-runtime/src/reference_data/subsystems/state.json +13 -0
- package/src/constraint-runtime/src/reference_data/subsystems/types.json +18 -0
- package/src/constraint-runtime/src/reference_data/subsystems/upstreamproxy.json +9 -0
- package/src/constraint-runtime/src/reference_data/subsystems/utils.json +32 -0
- package/src/constraint-runtime/src/reference_data/subsystems/vim.json +12 -0
- package/src/constraint-runtime/src/reference_data/subsystems/voice.json +8 -0
- package/src/constraint-runtime/src/reference_data/tools_snapshot.json +1042 -0
- package/src/constraint-runtime/src/remote/index.ts +15 -0
- package/src/constraint-runtime/src/remote_runtime.ts +17 -0
- package/src/constraint-runtime/src/runtime/index.ts +1 -0
- package/src/constraint-runtime/src/runtime/session.ts +42 -0
- package/src/constraint-runtime/src/schemas/index.ts +15 -0
- package/src/constraint-runtime/src/screens/index.ts +15 -0
- package/src/constraint-runtime/src/server/index.ts +15 -0
- package/src/constraint-runtime/src/services/index.ts +15 -0
- package/src/constraint-runtime/src/session_store.ts +32 -0
- package/src/constraint-runtime/src/setup.ts +50 -0
- package/src/constraint-runtime/src/skills/index.ts +1 -0
- package/src/constraint-runtime/src/skills/skill-registry.ts +40 -0
- package/src/constraint-runtime/src/state/index.ts +15 -0
- package/src/constraint-runtime/src/system_init.ts +21 -0
- package/src/constraint-runtime/src/thinking/engine.ts +61 -0
- package/src/constraint-runtime/src/thinking/index.ts +1 -0
- package/src/constraint-runtime/src/tool_pool.ts +20 -0
- package/src/constraint-runtime/src/tools/OpenCLI/execAdapter.ts +12 -0
- package/src/constraint-runtime/src/tools/OpenCLI/listAdapters.ts +12 -0
- package/src/constraint-runtime/src/tools/OpenCLI/runCommand.ts +13 -0
- package/src/constraint-runtime/src/tools/PolymarketSDK/cancelOrder.ts +10 -0
- package/src/constraint-runtime/src/tools/PolymarketSDK/createOrder.ts +13 -0
- package/src/constraint-runtime/src/tools/PolymarketSDK/getMarket.ts +14 -0
- package/src/constraint-runtime/src/tools/PolymarketSDK/getOrders.ts +10 -0
- package/src/constraint-runtime/src/tools/PolymarketSDK/listMarkets.ts +24 -0
- package/src/constraint-runtime/src/tools/SafeSDK/confirmTransaction.ts +13 -0
- package/src/constraint-runtime/src/tools/SafeSDK/createTransaction.ts +23 -0
- package/src/constraint-runtime/src/tools/SafeSDK/deploySafe.ts +12 -0
- package/src/constraint-runtime/src/tools/SafeSDK/executeTransaction.ts +12 -0
- package/src/constraint-runtime/src/tools/SafeSDK/getBalance.ts +10 -0
- package/src/constraint-runtime/src/tools/SafeSDK/getPendingTransactions.ts +10 -0
- package/src/constraint-runtime/src/tools/SafeSDK/proposeTransaction.ts +14 -0
- package/src/constraint-runtime/src/tools/WalletTools/autoPay.ts +58 -0
- package/src/constraint-runtime/src/tools/WalletTools/createWallet.ts +19 -0
- package/src/constraint-runtime/src/tools/WalletTools/getBalance.ts +28 -0
- package/src/constraint-runtime/src/tools/WalletTools/importWallet.ts +34 -0
- package/src/constraint-runtime/src/tools/WalletTools/sendTransaction.ts +50 -0
- package/src/constraint-runtime/src/tools/WalletTools/signMessage.ts +23 -0
- package/src/constraint-runtime/src/tools/WalletTools/transferToken.ts +49 -0
- package/src/constraint-runtime/src/tools.ts +100 -0
- package/src/constraint-runtime/src/transcript.ts +23 -0
- package/src/constraint-runtime/src/types/index.ts +15 -0
- package/src/constraint-runtime/src/upstream_proxy/index.ts +15 -0
- package/src/constraint-runtime/src/utils/index.ts +15 -0
- package/src/constraint-runtime/src/vim/index.ts +15 -0
- package/src/constraint-runtime/src/voice/index.ts +15 -0
- package/src/constraint-runtime/tests/agent.test.ts +20 -0
- package/src/constraint-runtime/tests/constraint.test.ts +47 -0
- package/src/constraint-runtime/tests/skill.test.ts +23 -0
- package/src/constraint-runtime/tests/thinking.test.ts +28 -0
- package/src/constraint-runtime/tsconfig.json +13 -0
- package/src/constraints/index.ts +6 -0
- package/src/documents/reader.ts +80 -0
- package/src/electron-preload.ts +25 -0
- package/src/electron.ts +200 -0
- package/src/index.ts +1606 -0
- package/src/llm/config-store.ts +312 -0
- package/src/llm/llm-judgment-client.ts +470 -0
- package/src/llm/pi-ai.ts +522 -0
- package/src/network/agent-network.ts +800 -0
- package/src/network/hybrid-messenger.ts +199 -0
- package/src/network/iroh-bootstrap.ts +57 -0
- package/src/network/iroh-discovery.ts +207 -0
- package/src/network/iroh-integration.ts +158 -0
- package/src/network/iroh-transport.ts +594 -0
- package/src/network/p2p.ts +963 -0
- package/src/network/storage/adapters/json-adapter.ts +439 -0
- package/src/network/storage/index.ts +187 -0
- package/src/network/storage/types.ts +166 -0
- package/src/pi-ecosystem/index.ts +453 -0
- package/src/pi-ecosystem-colony/index.ts +482 -0
- package/src/pi-ecosystem-goals/index.ts +585 -0
- package/src/pi-ecosystem-judgment/decision.ts +431 -0
- package/src/pi-ecosystem-judgment/distillation.ts +398 -0
- package/src/pi-ecosystem-judgment/human-value-store.ts +580 -0
- package/src/pi-ecosystem-judgment/index.ts +678 -0
- package/src/pi-ecosystem-judgment/value-injection.ts +744 -0
- package/src/pi-ecosystem-mcp/index.ts +427 -0
- package/src/pi-ecosystem-subagents/index.ts +408 -0
- package/src/social/ant-colony/AdaptiveHeartbeat.ts +131 -0
- package/src/social/ant-colony/PheromoneEngine.ts +302 -0
- package/src/social/ant-colony/index.ts +18 -0
- package/src/social/ant-colony/types.ts +94 -0
- package/src/social/channels/ChannelManager.ts +485 -0
- package/src/social/channels/DiapChannelBridge.ts +501 -0
- package/src/social/channels/InterestMatcher.ts +189 -0
- package/src/social/channels/agent-workflow-config.json +214 -0
- package/src/social/channels/agent-workflow-config.yaml +334 -0
- package/src/social/channels/channel-agent-session.ts +407 -0
- package/src/social/channels/channel-heartbeat-agent.ts +622 -0
- package/src/social/channels/diap-doc-parser.ts +282 -0
- package/src/social/channels/harness-workflow-integrator.ts +594 -0
- package/src/social/channels/index.ts +22 -0
- package/src/social/channels/types.ts +115 -0
- package/src/social/global-shared-context.ts +506 -0
- package/src/social/heartbeat.ts +1041 -0
- package/src/social/persona/enhanced-persona.ts +359 -0
- package/src/test/ai-judgment-test.ts +92 -0
- package/src/test/bollharness-integration.test.ts +398 -0
- package/src/test/channel-agent-multi-dialogue.ts +265 -0
- package/src/test/channel-heartbeat-agent-test.ts +244 -0
- package/src/test/constraint-layer.test.ts +191 -0
- package/src/test/diap-identity-test.ts +222 -0
- package/src/test/diap-quick-test.ts +73 -0
- package/src/test/global-shared-context.test.ts +393 -0
- package/src/test/harness-judgment-injection.test.ts +353 -0
- package/src/test/harness-workflow-integrator-test.ts +285 -0
- package/src/test/human-value-store.test.ts +316 -0
- package/src/test/hybrid-integration-test.ts +126 -0
- package/src/test/hybrid-messenger-verify.ts +68 -0
- package/src/test/iroh-bistream-debug.ts +50 -0
- package/src/test/iroh-communication.test.ts +81 -0
- package/src/test/iroh-debug-test.ts +69 -0
- package/src/test/iroh-diap-test.ts +90 -0
- package/src/test/iroh-direct-connect.ts +65 -0
- package/src/test/iroh-e2e-fixed.ts +106 -0
- package/src/test/iroh-e2e-same-process.ts +83 -0
- package/src/test/iroh-e2e.ts +83 -0
- package/src/test/iroh-final-e2e.ts +84 -0
- package/src/test/iroh-relay-test.ts +46 -0
- package/src/test/iroh-simple-test.ts +49 -0
- package/src/test/iroh-transport-verify.ts +60 -0
- package/src/test/iroh-transport.test.ts +47 -0
- package/src/test/iroh-two-nodes.ts +87 -0
- package/src/test/iroh-verify.ts +55 -0
- package/src/test/judgment-decision.test.ts +373 -0
- package/src/test/llm-judgment-integration.test.ts +257 -0
- package/src/test/p2p-agent-complex-dialogue.ts +490 -0
- package/src/test/p2p-agent-dialogue.ts +423 -0
- package/src/test/p2p-agent-full-bidirectional.ts +686 -0
- package/src/test/p2p-agent-harness-flow.ts +562 -0
- package/src/test/p2p-agent-harness-single.ts +175 -0
- package/src/test/p2p-ai-dialogue-test.ts +374 -0
- package/src/test/p2p-cid-connect-test.ts +245 -0
- package/src/test/p2p-connect-receiver.ts +85 -0
- package/src/test/p2p-iroh-test.ts +214 -0
- package/src/test/p2p-minimal-test.ts +264 -0
- package/src/test/p2p-node-1.ts +172 -0
- package/src/test/p2p-node-2.ts +172 -0
- package/src/test/p2p-server.ts +335 -0
- package/src/test/p2p-two-nodes-test.ts +542 -0
- package/src/test/pi-sdk.test.ts +47 -0
- package/src/test/set-persona.ts +56 -0
- package/src/test/simple.test.ts +11 -0
- package/src/test/storage-integration.test.ts +191 -0
- package/src/test/subagent-manager.test.ts +392 -0
- package/src/test/test-gate-flow.test.ts +92 -0
- package/src/test/workflow-engine.test.ts +101 -0
- package/src/web/api-config.html +265 -0
- package/src/web/client.js +1401 -0
- package/src/web/components/p2p/P2PModal.tsx +328 -0
- package/src/web/components/p2p/index.ts +404 -0
- package/src/web/components/p2p/index.tsx +297 -0
- package/src/web/components/p2p/p2p-connection.ts +186 -0
- package/src/web/components/p2p/p2p-identity.ts +92 -0
- package/src/web/components/p2p/p2p-manager.ts +109 -0
- package/src/web/components/p2p/p2p-messages.ts +219 -0
- package/src/web/components/p2p/p2p-modal.ts +685 -0
- package/src/web/components/p2p/p2p-store-memory.ts +162 -0
- package/src/web/components/p2p/types.ts +112 -0
- package/src/web/design.md +99 -0
- package/src/web/index.html +192 -0
- package/src/web/server.ts +1878 -0
- package/src/web/style.css +3260 -0
- package/src/workflows/collaboration.ts +455 -0
- package/src/workflows/index.ts +64 -0
- package/tsconfig.electron.json +20 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +12 -0
- package//346/203/263/346/263/225.md +79 -0
|
@@ -0,0 +1,963 @@
|
|
|
1
|
+
import { createLibp2p } from 'libp2p';
|
|
2
|
+
import { tcp } from '@libp2p/tcp';
|
|
3
|
+
import { multiaddr as createMultiaddr } from '@multiformats/multiaddr';
|
|
4
|
+
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2';
|
|
5
|
+
import { autoNAT } from '@libp2p/autonat';
|
|
6
|
+
import { uPnPNAT } from '@libp2p/upnp-nat';
|
|
7
|
+
import * as fs from 'fs/promises';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
|
|
10
|
+
const PEER_STORE_PATH = path.join(process.env.HOME || '/tmp', '.bolloon', 'peer-store.json');
|
|
11
|
+
const RECONNECT_DELAY_MS = 5000;
|
|
12
|
+
const MAX_RECONNECT_ATTEMPTS = 3;
|
|
13
|
+
|
|
14
|
+
export interface PersistentPeerInfo {
|
|
15
|
+
peerId: string;
|
|
16
|
+
multiaddrs: string[];
|
|
17
|
+
did?: string;
|
|
18
|
+
lastConnected?: number;
|
|
19
|
+
lastAttempt?: number;
|
|
20
|
+
name?: string;
|
|
21
|
+
relayAddr?: string;
|
|
22
|
+
canRelay?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface P2PNode {
|
|
26
|
+
peerId: string;
|
|
27
|
+
multiaddrs: string[];
|
|
28
|
+
relayAddr?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface NatStatus {
|
|
32
|
+
reachable: boolean;
|
|
33
|
+
type?: 'public' | 'cone' | 'symmetric' | 'unknown';
|
|
34
|
+
externalAddr?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface PendingRequest {
|
|
38
|
+
id: string;
|
|
39
|
+
type: string;
|
|
40
|
+
payload: string;
|
|
41
|
+
timestamp: number;
|
|
42
|
+
resolve: (response: Uint8Array) => void;
|
|
43
|
+
reject: (error: Error) => void;
|
|
44
|
+
timeout: ReturnType<typeof setTimeout>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface PendingRequestInfo {
|
|
48
|
+
requestId: string;
|
|
49
|
+
type: string;
|
|
50
|
+
payload: string;
|
|
51
|
+
fromPeerId: string;
|
|
52
|
+
did?: string;
|
|
53
|
+
timestamp: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ResponseHandler {
|
|
57
|
+
(payload: string, from: string, did?: string): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class RequestResponseManager {
|
|
61
|
+
private pendingRequests: Map<string, PendingRequest> = new Map();
|
|
62
|
+
private responseHandlers: Map<string, ResponseHandler> = new Map();
|
|
63
|
+
private requestTimeoutMs: number = 30000;
|
|
64
|
+
private cleanupInterval: ReturnType<typeof setInterval> | null = null;
|
|
65
|
+
|
|
66
|
+
constructor() {
|
|
67
|
+
this.startCleanup();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private startCleanup(): void {
|
|
71
|
+
this.cleanupInterval = setInterval(() => {
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
for (const [id, request] of this.pendingRequests) {
|
|
74
|
+
if (now - request.timestamp > this.requestTimeoutMs) {
|
|
75
|
+
clearTimeout(request.timeout);
|
|
76
|
+
request.reject(new Error(`Request ${id} timed out`));
|
|
77
|
+
this.pendingRequests.delete(id);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}, 10000);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async sendRequest(
|
|
84
|
+
peerId: string,
|
|
85
|
+
type: string,
|
|
86
|
+
payload: string,
|
|
87
|
+
sendFn: (peerId: string, data: Uint8Array) => Promise<void>,
|
|
88
|
+
onResponse?: (peerId: string) => Promise<Uint8Array | null>
|
|
89
|
+
): Promise<Uint8Array | null> {
|
|
90
|
+
const requestId = crypto.randomUUID();
|
|
91
|
+
const data = new TextEncoder().encode(
|
|
92
|
+
`REQ:${requestId}|${type}:${payload}`
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
const timeout = setTimeout(() => {
|
|
97
|
+
this.pendingRequests.delete(requestId);
|
|
98
|
+
reject(new Error(`Request ${requestId} timed out after ${this.requestTimeoutMs}ms`));
|
|
99
|
+
}, this.requestTimeoutMs);
|
|
100
|
+
|
|
101
|
+
this.pendingRequests.set(requestId, {
|
|
102
|
+
id: requestId,
|
|
103
|
+
type,
|
|
104
|
+
payload,
|
|
105
|
+
timestamp: Date.now(),
|
|
106
|
+
resolve,
|
|
107
|
+
reject,
|
|
108
|
+
timeout
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
sendFn(peerId, data).catch(err => {
|
|
112
|
+
clearTimeout(timeout);
|
|
113
|
+
this.pendingRequests.delete(requestId);
|
|
114
|
+
reject(err);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
handleResponse(requestId: string, responseData: Uint8Array): void {
|
|
120
|
+
const request = this.pendingRequests.get(requestId);
|
|
121
|
+
if (request) {
|
|
122
|
+
clearTimeout(request.timeout);
|
|
123
|
+
request.resolve(responseData);
|
|
124
|
+
this.pendingRequests.delete(requestId);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
registerResponseHandler(type: string, handler: ResponseHandler): void {
|
|
129
|
+
this.responseHandlers.set(type, handler);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getResponseHandler(type: string): ResponseHandler | undefined {
|
|
133
|
+
return this.responseHandlers.get(type);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
setRequestTimeout(ms: number): void {
|
|
137
|
+
this.requestTimeoutMs = ms;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
getPendingCount(): number {
|
|
141
|
+
return this.pendingRequests.size;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
shutdown(): void {
|
|
145
|
+
if (this.cleanupInterval) {
|
|
146
|
+
clearInterval(this.cleanupInterval);
|
|
147
|
+
}
|
|
148
|
+
for (const request of this.pendingRequests.values()) {
|
|
149
|
+
clearTimeout(request.timeout);
|
|
150
|
+
request.reject(new Error('Shutting down'));
|
|
151
|
+
}
|
|
152
|
+
this.pendingRequests.clear();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export class P2PNetwork {
|
|
157
|
+
private node: any = null;
|
|
158
|
+
private messageHandlers: Map<string, (msg: Uint8Array, from: string, did?: string) => void> = new Map();
|
|
159
|
+
private offlineMessages: Map<string, Uint8Array[]> = new Map();
|
|
160
|
+
private persistentPeers: Map<string, PersistentPeerInfo> = new Map();
|
|
161
|
+
private reconnectTimers: Map<string, ReturnType<typeof setTimeout>> = new Map();
|
|
162
|
+
private ownDid: string | null = null;
|
|
163
|
+
private peerStorePath: string;
|
|
164
|
+
private natStatus: NatStatus = { reachable: false };
|
|
165
|
+
private relayServerAddr: string | null = null;
|
|
166
|
+
private requestResponseManager: RequestResponseManager = new RequestResponseManager();
|
|
167
|
+
private pendingResponseHandlers: Map<string, (response: string, from: string) => void> = new Map();
|
|
168
|
+
private pendingRequests: Map<string, PendingRequestInfo> = new Map();
|
|
169
|
+
private requestTimeoutMs: number = 30000;
|
|
170
|
+
|
|
171
|
+
// 新增: 消息存储层
|
|
172
|
+
private messageStore: any = null;
|
|
173
|
+
private offlineDeliveryInterval: ReturnType<typeof setInterval> | null = null;
|
|
174
|
+
|
|
175
|
+
constructor() {
|
|
176
|
+
this.peerStorePath = PEER_STORE_PATH;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async enablePersistence(): Promise<void> {
|
|
180
|
+
try {
|
|
181
|
+
const { JsonMessageStore } = await import('./storage/adapters/json-adapter.js');
|
|
182
|
+
const baseDir = path.join(process.env.HOME || '/tmp', '.bolloon', 'messages-libp2p');
|
|
183
|
+
this.messageStore = new JsonMessageStore({ baseDir });
|
|
184
|
+
await this.messageStore.initialize();
|
|
185
|
+
this.startOfflineDeliveryLoop();
|
|
186
|
+
console.log('[P2P] Persistence enabled');
|
|
187
|
+
} catch (e) {
|
|
188
|
+
console.warn('[P2P] Failed to enable persistence:', e);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private startOfflineDeliveryLoop(): void {
|
|
193
|
+
if (!this.messageStore) return;
|
|
194
|
+
|
|
195
|
+
this.offlineDeliveryInterval = setInterval(async () => {
|
|
196
|
+
for (const peerId of this.getPeers()) {
|
|
197
|
+
const offlineMsgs = await this.messageStore.getOfflineMessages(peerId);
|
|
198
|
+
|
|
199
|
+
for (const msg of offlineMsgs) {
|
|
200
|
+
if (msg.retryCount >= 10) {
|
|
201
|
+
await this.messageStore.dequeueOfflineMessage(msg.id);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
const payload = Uint8Array.from(atob(msg.payload), c => c.charCodeAt(0));
|
|
207
|
+
const success = await this.sendMessageDirect(peerId, msg.type, payload);
|
|
208
|
+
|
|
209
|
+
if (success) {
|
|
210
|
+
await this.messageStore.dequeueOfflineMessage(msg.id);
|
|
211
|
+
console.log(`[P2P] Delivered offline message to ${peerId.substring(0, 12)}...`);
|
|
212
|
+
}
|
|
213
|
+
} catch {
|
|
214
|
+
await this.messageStore.incrementOfflineRetry(msg.id);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}, 5000);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private async sendMessageDirect(peerId: string, type: string, payload: string | Uint8Array): Promise<boolean> {
|
|
222
|
+
if (!this.node) return false;
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
const data = typeof payload === 'string'
|
|
226
|
+
? new TextEncoder().encode(`${type}:${payload}`)
|
|
227
|
+
: payload;
|
|
228
|
+
|
|
229
|
+
const ma = createMultiaddr(`/p2p/${peerId}`);
|
|
230
|
+
const { stream } = await this.node.dialProtocol(ma, '/agent/message');
|
|
231
|
+
stream.send(data);
|
|
232
|
+
return true;
|
|
233
|
+
} catch {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async createNode(config?: {
|
|
239
|
+
bootstrapPeers?: string[];
|
|
240
|
+
ownDid?: string;
|
|
241
|
+
enableRelay?: boolean;
|
|
242
|
+
enableAutoNat?: boolean;
|
|
243
|
+
enableUPnP?: boolean;
|
|
244
|
+
relayPeers?: string[];
|
|
245
|
+
}): Promise<P2PNode> {
|
|
246
|
+
this.ownDid = config?.ownDid || null;
|
|
247
|
+
const enableRelay = config?.enableRelay ?? true;
|
|
248
|
+
const enableAutoNat = config?.enableAutoNat ?? true;
|
|
249
|
+
const enableUPnP = config?.enableUPnP ?? true;
|
|
250
|
+
|
|
251
|
+
const transports = [tcp()];
|
|
252
|
+
const services: any = {};
|
|
253
|
+
|
|
254
|
+
if (enableRelay) {
|
|
255
|
+
try {
|
|
256
|
+
const relayTransport = circuitRelayTransport();
|
|
257
|
+
transports.push(relayTransport as any);
|
|
258
|
+
} catch (e) {
|
|
259
|
+
console.warn(`[P2P] Failed to add circuit relay transport:`, e);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (enableAutoNat) {
|
|
264
|
+
try {
|
|
265
|
+
services.autonat = autoNAT();
|
|
266
|
+
} catch (e) {
|
|
267
|
+
console.warn(`[P2P] Failed to setup AutoNAT:`, e);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (enableUPnP) {
|
|
272
|
+
try {
|
|
273
|
+
services.upnpNAT = uPnPNAT();
|
|
274
|
+
} catch (e) {
|
|
275
|
+
console.warn(`[P2P] Failed to setup UPnP:`, e);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this.node = await createLibp2p({
|
|
280
|
+
addresses: {
|
|
281
|
+
listen: ['/ip4/0.0.0.0/tcp/0']
|
|
282
|
+
},
|
|
283
|
+
transports,
|
|
284
|
+
services
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
this.node.addEventListener('peer-relay-registry', (evt: any) => {
|
|
288
|
+
if (evt.detail?.relayAddr) {
|
|
289
|
+
this.relayServerAddr = evt.detail.relayAddr;
|
|
290
|
+
console.log(`[P2P] Relay address available: ${this.relayServerAddr}`);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
await this.node.start();
|
|
295
|
+
|
|
296
|
+
const peerId = this.node.peerId.toString();
|
|
297
|
+
const multiaddrs = this.node.getMultiaddrs().map((addr: any) => addr.toString());
|
|
298
|
+
|
|
299
|
+
if (config?.relayPeers && config.relayPeers.length > 0) {
|
|
300
|
+
for (const relayAddr of config.relayPeers) {
|
|
301
|
+
try {
|
|
302
|
+
const ma = createMultiaddr(relayAddr);
|
|
303
|
+
await this.node.dial(ma);
|
|
304
|
+
console.log(`[P2P] Connected to relay peer: ${relayAddr}`);
|
|
305
|
+
} catch (e) {
|
|
306
|
+
console.warn(`[P2P] Failed to connect to relay peer ${relayAddr}:`, e);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
await this.loadPersistentPeers();
|
|
312
|
+
|
|
313
|
+
if (config?.bootstrapPeers) {
|
|
314
|
+
await this.connectToBootstrapPeers(config.bootstrapPeers);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
await this.reconnectPersistentPeers();
|
|
318
|
+
|
|
319
|
+
this.checkNatStatus();
|
|
320
|
+
|
|
321
|
+
this.setupMessageHandler();
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
peerId,
|
|
325
|
+
multiaddrs,
|
|
326
|
+
relayAddr: this.relayServerAddr || undefined
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private async checkNatStatus(): Promise<void> {
|
|
331
|
+
if (!this.node) return;
|
|
332
|
+
|
|
333
|
+
try {
|
|
334
|
+
const connections = this.node.getConnections();
|
|
335
|
+
if (connections.length > 0) {
|
|
336
|
+
for (const conn of connections) {
|
|
337
|
+
const addr = conn.remoteAddr?.toString();
|
|
338
|
+
if (addr && addr.includes('127.0.0.1')) continue;
|
|
339
|
+
this.natStatus = {
|
|
340
|
+
reachable: true,
|
|
341
|
+
type: 'unknown',
|
|
342
|
+
externalAddr: addr
|
|
343
|
+
};
|
|
344
|
+
console.log(`[P2P] NAT status: reachable, external address: ${addr}`);
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
} catch (e) {
|
|
349
|
+
console.warn(`[P2P] NAT status check failed:`, e);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
getNatStatus(): NatStatus {
|
|
354
|
+
return this.natStatus;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
getRelayAddress(): string | null {
|
|
358
|
+
return this.relayServerAddr;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async createRelayReservation(): Promise<string | null> {
|
|
362
|
+
if (!this.node) return null;
|
|
363
|
+
|
|
364
|
+
try {
|
|
365
|
+
const relayService = this.node.services.get('circuit-relay');
|
|
366
|
+
if (!relayService) {
|
|
367
|
+
console.log(`[P2P] Circuit relay service not available`);
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const reservation = await relayService.reserve();
|
|
372
|
+
if (reservation) {
|
|
373
|
+
this.relayServerAddr = `/p2p/${this.node.peerId.toString()}/p2p-circuit`;
|
|
374
|
+
console.log(`[P2P] Created relay reservation: ${this.relayServerAddr}`);
|
|
375
|
+
return this.relayServerAddr;
|
|
376
|
+
}
|
|
377
|
+
} catch (e) {
|
|
378
|
+
console.warn(`[P2P] Failed to create relay reservation:`, e);
|
|
379
|
+
}
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async dialViaRelay(relayAddr: string, targetPeerId: string): Promise<boolean> {
|
|
384
|
+
if (!this.node) return false;
|
|
385
|
+
|
|
386
|
+
try {
|
|
387
|
+
const ma = createMultiaddr(`${relayAddr}/p2p/${targetPeerId}`);
|
|
388
|
+
await this.node.dial(ma);
|
|
389
|
+
console.log(`[P2P] Dialed ${targetPeerId} via relay ${relayAddr}`);
|
|
390
|
+
return true;
|
|
391
|
+
} catch (e) {
|
|
392
|
+
console.warn(`[P2P] Failed to dial via relay:`, e);
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private async loadPersistentPeers(): Promise<void> {
|
|
398
|
+
try {
|
|
399
|
+
const data = await fs.readFile(this.peerStorePath, 'utf-8');
|
|
400
|
+
const peers: PersistentPeerInfo[] = JSON.parse(data);
|
|
401
|
+
for (const peer of peers) {
|
|
402
|
+
this.persistentPeers.set(peer.peerId, peer);
|
|
403
|
+
}
|
|
404
|
+
console.log(`[P2P] Loaded ${peers.length} persistent peers`);
|
|
405
|
+
} catch {
|
|
406
|
+
console.log(`[P2P] No existing peer store found, starting fresh`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
private async savePersistentPeers(): Promise<void> {
|
|
411
|
+
try {
|
|
412
|
+
const dir = path.dirname(this.peerStorePath);
|
|
413
|
+
await fs.mkdir(dir, { recursive: true });
|
|
414
|
+
const peers = Array.from(this.persistentPeers.values());
|
|
415
|
+
await fs.writeFile(this.peerStorePath, JSON.stringify(peers, null, 2));
|
|
416
|
+
} catch (e) {
|
|
417
|
+
console.warn(`[P2P] Failed to save peer store:`, e);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
addPersistentPeer(peerInfo: PersistentPeerInfo): void {
|
|
422
|
+
peerInfo.lastConnected = Date.now();
|
|
423
|
+
this.persistentPeers.set(peerInfo.peerId, peerInfo);
|
|
424
|
+
this.savePersistentPeers();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
removePersistentPeer(peerId: string): void {
|
|
428
|
+
this.persistentPeers.delete(peerId);
|
|
429
|
+
this.savePersistentPeers();
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
getPersistentPeers(): PersistentPeerInfo[] {
|
|
433
|
+
return Array.from(this.persistentPeers.values());
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private async reconnectPersistentPeers(): Promise<void> {
|
|
437
|
+
const now = Date.now();
|
|
438
|
+
for (const [peerId, peerInfo] of this.persistentPeers) {
|
|
439
|
+
if (peerInfo.lastAttempt && now - peerInfo.lastAttempt < RECONNECT_DELAY_MS) {
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (peerInfo.multiaddrs && peerInfo.multiaddrs.length > 0) {
|
|
443
|
+
await this.attemptReconnect(peerId, peerInfo);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private async attemptReconnect(peerId: string, peerInfo: PersistentPeerInfo, attempt = 0): Promise<void> {
|
|
449
|
+
if (!this.node) return;
|
|
450
|
+
|
|
451
|
+
try {
|
|
452
|
+
const hasConn = this.node.getConnections?.(peerInfo.peerId);
|
|
453
|
+
if (hasConn && hasConn.length > 0) {
|
|
454
|
+
console.log(`[P2P] Already connected to ${peerId}`);
|
|
455
|
+
await this.deliverOfflineMessages(peerId);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
} catch {}
|
|
459
|
+
|
|
460
|
+
console.log(`[P2P] Reconnecting to ${peerId} (attempt ${attempt + 1})...`);
|
|
461
|
+
|
|
462
|
+
for (const addr of peerInfo.multiaddrs) {
|
|
463
|
+
try {
|
|
464
|
+
const ma = createMultiaddr(addr);
|
|
465
|
+
await this.node.dial(ma);
|
|
466
|
+
peerInfo.lastConnected = Date.now();
|
|
467
|
+
peerInfo.lastAttempt = undefined;
|
|
468
|
+
this.persistentPeers.set(peerId, peerInfo);
|
|
469
|
+
await this.savePersistentPeers();
|
|
470
|
+
console.log(`[P2P] Reconnected to ${peerId} at ${addr}`);
|
|
471
|
+
await this.deliverOfflineMessages(peerId);
|
|
472
|
+
return;
|
|
473
|
+
} catch (e) {
|
|
474
|
+
console.warn(`[P2P] Failed to reconnect to ${addr}:`, e);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
peerInfo.lastAttempt = Date.now();
|
|
479
|
+
this.persistentPeers.set(peerId, peerInfo);
|
|
480
|
+
|
|
481
|
+
if (attempt < MAX_RECONNECT_ATTEMPTS - 1) {
|
|
482
|
+
const timer = setTimeout(() => {
|
|
483
|
+
this.reconnectTimers.delete(peerId);
|
|
484
|
+
this.attemptReconnect(peerId, peerInfo, attempt + 1);
|
|
485
|
+
}, RECONNECT_DELAY_MS * (attempt + 1));
|
|
486
|
+
this.reconnectTimers.set(peerId, timer);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
private async deliverOfflineMessages(peerId: string): Promise<void> {
|
|
491
|
+
const messages = this.offlineMessages.get(peerId) || [];
|
|
492
|
+
this.offlineMessages.delete(peerId);
|
|
493
|
+
|
|
494
|
+
for (const data of messages) {
|
|
495
|
+
try {
|
|
496
|
+
await this.sendRawMessage(peerId, data);
|
|
497
|
+
} catch (e) {
|
|
498
|
+
console.warn(`[P2P] Failed to deliver offline message to ${peerId}:`, e);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (messages.length > 0) {
|
|
503
|
+
console.log(`[P2P] Delivered ${messages.length} offline messages to ${peerId}`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
private async connectToBootstrapPeers(peers: string[]): Promise<void> {
|
|
508
|
+
if (!this.node) return;
|
|
509
|
+
|
|
510
|
+
for (const addr of peers) {
|
|
511
|
+
try {
|
|
512
|
+
const ma = createMultiaddr(addr);
|
|
513
|
+
await this.node.dial(ma);
|
|
514
|
+
console.log(`[P2P] Connected to bootstrap peer: ${addr}`);
|
|
515
|
+
const peerId = (ma as any).getPeerId?.() || (ma as any).peerId;
|
|
516
|
+
if (peerId) {
|
|
517
|
+
this.addPersistentPeer({ peerId, multiaddrs: [addr] });
|
|
518
|
+
}
|
|
519
|
+
} catch (e) {
|
|
520
|
+
console.warn(`[P2P] Failed to connect to bootstrap peer ${addr}:`, e);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
private setupMessageHandler(): void {
|
|
526
|
+
if (!this.node) return;
|
|
527
|
+
|
|
528
|
+
const network = this;
|
|
529
|
+
|
|
530
|
+
this.node.handle('/agent/message', async ({ stream, connection }: any) => {
|
|
531
|
+
try {
|
|
532
|
+
const chunks: Uint8Array[] = [];
|
|
533
|
+
for await (const chunk of stream) {
|
|
534
|
+
chunks.push(chunk instanceof Uint8Array ? chunk : chunk.subarray());
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const data = new Uint8Array(chunks.reduce((acc, c) => acc + c.length, 0));
|
|
538
|
+
let offset = 0;
|
|
539
|
+
for (const chunk of chunks) {
|
|
540
|
+
data.set(chunk, offset);
|
|
541
|
+
offset += chunk.length;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const fromPeerId = connection.remotePeer.toString();
|
|
545
|
+
const messageStr = new TextDecoder().decode(data);
|
|
546
|
+
const colonIdx = messageStr.indexOf(':');
|
|
547
|
+
const didMarker = 'DID:';
|
|
548
|
+
let did: string | undefined;
|
|
549
|
+
let type: string;
|
|
550
|
+
let payload: string;
|
|
551
|
+
let requestId: string | undefined = undefined;
|
|
552
|
+
|
|
553
|
+
if (messageStr.startsWith(didMarker)) {
|
|
554
|
+
const didEndIdx = messageStr.indexOf('|');
|
|
555
|
+
if (didEndIdx > 0) {
|
|
556
|
+
did = messageStr.substring(didMarker.length, didEndIdx);
|
|
557
|
+
const afterDid = messageStr.substring(didEndIdx + 1);
|
|
558
|
+
const payloadColonIdx = afterDid.indexOf(':');
|
|
559
|
+
if (payloadColonIdx > 0) {
|
|
560
|
+
type = afterDid.substring(0, payloadColonIdx);
|
|
561
|
+
payload = afterDid.substring(payloadColonIdx + 1);
|
|
562
|
+
} else {
|
|
563
|
+
type = afterDid;
|
|
564
|
+
payload = '';
|
|
565
|
+
}
|
|
566
|
+
} else {
|
|
567
|
+
type = 'message';
|
|
568
|
+
payload = messageStr.substring(didMarker.length);
|
|
569
|
+
}
|
|
570
|
+
} else if (messageStr.startsWith('REQ:')) {
|
|
571
|
+
// Request message format: REQ:<requestId>|<optional DID>|type:payload
|
|
572
|
+
const reqMatch = messageStr.match(/^REQ:([^|]+)\|(.*)$/);
|
|
573
|
+
if (reqMatch) {
|
|
574
|
+
requestId = reqMatch[1];
|
|
575
|
+
const afterReq = reqMatch[2];
|
|
576
|
+
// Check if it has DID prefix
|
|
577
|
+
if (afterReq.startsWith('DID:')) {
|
|
578
|
+
const didEndIdx = afterReq.indexOf('|');
|
|
579
|
+
if (didEndIdx > 0) {
|
|
580
|
+
did = afterReq.substring(4, didEndIdx);
|
|
581
|
+
const afterDid = afterReq.substring(didEndIdx + 1);
|
|
582
|
+
const payloadColonIdx = afterDid.indexOf(':');
|
|
583
|
+
if (payloadColonIdx > 0) {
|
|
584
|
+
type = afterDid.substring(0, payloadColonIdx);
|
|
585
|
+
payload = afterDid.substring(payloadColonIdx + 1);
|
|
586
|
+
} else {
|
|
587
|
+
type = afterDid;
|
|
588
|
+
payload = '';
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
} else {
|
|
592
|
+
const payloadColonIdx = afterReq.indexOf(':');
|
|
593
|
+
if (payloadColonIdx > 0) {
|
|
594
|
+
type = afterReq.substring(0, payloadColonIdx);
|
|
595
|
+
payload = afterReq.substring(payloadColonIdx + 1);
|
|
596
|
+
} else {
|
|
597
|
+
type = afterReq;
|
|
598
|
+
payload = '';
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
} else {
|
|
602
|
+
type = 'message';
|
|
603
|
+
payload = messageStr.substring(4);
|
|
604
|
+
}
|
|
605
|
+
} else if (messageStr.startsWith('RESP:')) {
|
|
606
|
+
// Response message format: RESP:<requestId>|type:payload
|
|
607
|
+
const respMatch = messageStr.match(/^RESP:([^|]+)\|(.*)$/);
|
|
608
|
+
if (respMatch) {
|
|
609
|
+
requestId = respMatch[1];
|
|
610
|
+
const afterResp = respMatch[2];
|
|
611
|
+
const payloadColonIdx = afterResp.indexOf(':');
|
|
612
|
+
if (payloadColonIdx > 0) {
|
|
613
|
+
type = afterResp.substring(0, payloadColonIdx);
|
|
614
|
+
payload = afterResp.substring(payloadColonIdx + 1);
|
|
615
|
+
} else {
|
|
616
|
+
type = afterResp;
|
|
617
|
+
payload = '';
|
|
618
|
+
}
|
|
619
|
+
} else {
|
|
620
|
+
type = 'response';
|
|
621
|
+
payload = messageStr.substring(5);
|
|
622
|
+
}
|
|
623
|
+
} else {
|
|
624
|
+
type = colonIdx > 0 ? messageStr.substring(0, colonIdx) : messageStr;
|
|
625
|
+
payload = colonIdx > 0 ? messageStr.substring(colonIdx + 1) : '';
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
if (!network.persistentPeers.has(fromPeerId)) {
|
|
629
|
+
network.addPersistentPeer({
|
|
630
|
+
peerId: fromPeerId,
|
|
631
|
+
multiaddrs: connection.remoteAddr ? [connection.remoteAddr.toString()] : [],
|
|
632
|
+
did
|
|
633
|
+
});
|
|
634
|
+
} else {
|
|
635
|
+
const existing = network.persistentPeers.get(fromPeerId)!;
|
|
636
|
+
if (did && !existing.did) {
|
|
637
|
+
existing.did = did;
|
|
638
|
+
network.persistentPeers.set(fromPeerId, existing);
|
|
639
|
+
network.savePersistentPeers();
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const handler = network.messageHandlers.get(type);
|
|
644
|
+
if (handler) {
|
|
645
|
+
handler(data, fromPeerId, did);
|
|
646
|
+
} else if (type === 'RESP' && requestId) {
|
|
647
|
+
// Handle response message - resolve the pending request
|
|
648
|
+
const pending = network.pendingResponses.get(requestId);
|
|
649
|
+
if (pending) {
|
|
650
|
+
clearTimeout(pending.timeout);
|
|
651
|
+
pending.resolve(payload);
|
|
652
|
+
network.pendingResponses.delete(requestId);
|
|
653
|
+
} else {
|
|
654
|
+
console.log(`[P2P] Response for unknown request ${requestId}`);
|
|
655
|
+
}
|
|
656
|
+
} else if (type === 'REQ' && requestId) {
|
|
657
|
+
// Handle request message - store for later response
|
|
658
|
+
network.storePendingRequest(requestId, { type: payload.split(':')[0] || 'request', payload, fromPeerId, did });
|
|
659
|
+
} else {
|
|
660
|
+
network.storeOfflineMessage(fromPeerId, data);
|
|
661
|
+
}
|
|
662
|
+
} catch (e) {
|
|
663
|
+
console.error(`[P2P] Message handler error:`, e);
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
private async sendRawMessage(peerId: string, data: Uint8Array): Promise<void> {
|
|
669
|
+
if (!this.node) throw new Error('Node not initialized');
|
|
670
|
+
|
|
671
|
+
const ma = createMultiaddr(`/p2p/${peerId}`);
|
|
672
|
+
const { stream } = await this.node.dialProtocol(ma, '/agent/message');
|
|
673
|
+
stream.send(data);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
private storeOfflineMessage(peerId: string, data: Uint8Array): void {
|
|
677
|
+
const messages = this.offlineMessages.get(peerId) || [];
|
|
678
|
+
messages.push(data);
|
|
679
|
+
this.offlineMessages.set(peerId, messages);
|
|
680
|
+
console.log(`[P2P] Stored offline message for ${peerId}, total: ${messages.length}`);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
getOfflineMessages(peerId: string): Uint8Array[] {
|
|
684
|
+
const messages = this.offlineMessages.get(peerId) || [];
|
|
685
|
+
this.offlineMessages.delete(peerId);
|
|
686
|
+
return messages;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
private storePendingRequest(requestId: string, info: Omit<PendingRequestInfo, 'requestId' | 'timestamp'>): void {
|
|
690
|
+
this.pendingRequests.set(requestId, {
|
|
691
|
+
...info,
|
|
692
|
+
requestId,
|
|
693
|
+
timestamp: Date.now()
|
|
694
|
+
});
|
|
695
|
+
console.log(`[P2P] Stored pending request ${requestId} from ${info.fromPeerId}`);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
getPendingRequest(requestId: string): PendingRequestInfo | undefined {
|
|
699
|
+
return this.pendingRequests.get(requestId);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
removePendingRequest(requestId: string): void {
|
|
703
|
+
this.pendingRequests.delete(requestId);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
getPendingRequests(): PendingRequestInfo[] {
|
|
707
|
+
return Array.from(this.pendingRequests.values());
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
onMessage(type: string, handler: (msg: Uint8Array, from: string, did?: string) => void): void {
|
|
711
|
+
this.messageHandlers.set(type, handler);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Send a message and wait for a response (request-response pattern)
|
|
716
|
+
*/
|
|
717
|
+
async sendRequest(
|
|
718
|
+
peerId: string,
|
|
719
|
+
type: string,
|
|
720
|
+
payload: string,
|
|
721
|
+
timeoutMs: number = 30000
|
|
722
|
+
): Promise<string | null> {
|
|
723
|
+
if (!this.node) {
|
|
724
|
+
throw new Error('Node not initialized');
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const requestId = crypto.randomUUID();
|
|
728
|
+
|
|
729
|
+
let data: Uint8Array;
|
|
730
|
+
if (this.ownDid) {
|
|
731
|
+
data = new TextEncoder().encode(`REQ:${requestId}|DID:${this.ownDid}|${type}:${payload}`);
|
|
732
|
+
} else {
|
|
733
|
+
data = new TextEncoder().encode(`REQ:${requestId}|${type}:${payload}`);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
return new Promise((resolve, reject) => {
|
|
737
|
+
const timeout = setTimeout(() => {
|
|
738
|
+
this.pendingResponses.delete(requestId);
|
|
739
|
+
reject(new Error(`Request to ${peerId} timed out after ${timeoutMs}ms`));
|
|
740
|
+
}, timeoutMs);
|
|
741
|
+
|
|
742
|
+
// Store the pending response handler
|
|
743
|
+
this.pendingResponses.set(requestId, {
|
|
744
|
+
resolve: (response: string) => {
|
|
745
|
+
clearTimeout(timeout);
|
|
746
|
+
resolve(response);
|
|
747
|
+
},
|
|
748
|
+
reject: (err: Error) => {
|
|
749
|
+
clearTimeout(timeout);
|
|
750
|
+
reject(err);
|
|
751
|
+
},
|
|
752
|
+
timeout
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
this.sendRawMessage(peerId, data).catch(err => {
|
|
756
|
+
clearTimeout(timeout);
|
|
757
|
+
this.pendingResponses.delete(requestId);
|
|
758
|
+
reject(err);
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
private pendingResponses: Map<string, {
|
|
764
|
+
resolve: (response: string) => void;
|
|
765
|
+
reject: (error: Error) => void;
|
|
766
|
+
timeout: ReturnType<typeof setTimeout>;
|
|
767
|
+
}> = new Map();
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* Register a handler for responses (used by the receiving side)
|
|
771
|
+
*/
|
|
772
|
+
onResponse(type: string, handler: (payload: string, from: string, did?: string, requestId?: string) => void): void {
|
|
773
|
+
this.messageHandlers.set(type, handler);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Send a response back to a peer
|
|
778
|
+
*/
|
|
779
|
+
async sendResponse(peerId: string, requestId: string, type: string, responsePayload: string): Promise<void> {
|
|
780
|
+
if (!this.node) {
|
|
781
|
+
throw new Error('Node not initialized');
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const data = new TextEncoder().encode(`RESP:${requestId}|${type}:${responsePayload}`);
|
|
785
|
+
|
|
786
|
+
try {
|
|
787
|
+
await this.sendRawMessage(peerId, data);
|
|
788
|
+
} catch (e) {
|
|
789
|
+
console.warn(`[P2P] Failed to send response to ${peerId}:`, e);
|
|
790
|
+
throw e;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Handle incoming request messages and route to appropriate handlers
|
|
796
|
+
*/
|
|
797
|
+
private handleRequest(type: string, payload: string, requestId: string, fromPeerId: string, did?: string): void {
|
|
798
|
+
const handler = this.messageHandlers.get(type);
|
|
799
|
+
if (handler) {
|
|
800
|
+
// Create a wrapper that sends the response
|
|
801
|
+
const originalHandler = handler;
|
|
802
|
+
handler = (msg: Uint8Array, from: string, didParam?: string) => {
|
|
803
|
+
originalHandler(msg, from, didParam);
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// Check if there's a response handler registered
|
|
808
|
+
const responseHandler = this.pendingResponseHandlers.get(type);
|
|
809
|
+
if (responseHandler) {
|
|
810
|
+
responseHandler(payload, fromPeerId);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Register a pending response handler for a specific message type
|
|
816
|
+
*/
|
|
817
|
+
registerResponseHandler(type: string, handler: (response: string, from: string) => void): void {
|
|
818
|
+
this.pendingResponseHandlers.set(type, handler);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
async sendMessage(peerId: string, type: string, payload: string): Promise<void> {
|
|
822
|
+
if (!this.node) throw new Error('Node not initialized');
|
|
823
|
+
|
|
824
|
+
let data: Uint8Array;
|
|
825
|
+
if (this.ownDid) {
|
|
826
|
+
data = new TextEncoder().encode(`DID:${this.ownDid}|${type}:${payload}`);
|
|
827
|
+
} else {
|
|
828
|
+
data = new TextEncoder().encode(`${type}:${payload}`);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
try {
|
|
832
|
+
await this.sendRawMessage(peerId, data);
|
|
833
|
+
} catch (e) {
|
|
834
|
+
console.warn(`[P2P] Failed to send to ${peerId}, storing offline`);
|
|
835
|
+
this.storeOfflineMessage(peerId, data);
|
|
836
|
+
this.scheduleReconnect(peerId);
|
|
837
|
+
|
|
838
|
+
// 如果有持久化存储,也存入离线队列
|
|
839
|
+
if (this.messageStore) {
|
|
840
|
+
const payloadBase64 = btoa(String.fromCharCode(...data));
|
|
841
|
+
this.messageStore.enqueueOfflineMessage({
|
|
842
|
+
targetNodeId: peerId,
|
|
843
|
+
type,
|
|
844
|
+
payload: payloadBase64,
|
|
845
|
+
createdAt: Date.now(),
|
|
846
|
+
transport: 'libp2p',
|
|
847
|
+
retryCount: 0,
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
private scheduleReconnect(peerId: string): void {
|
|
854
|
+
if (this.reconnectTimers.has(peerId)) return;
|
|
855
|
+
|
|
856
|
+
const peerInfo = this.persistentPeers.get(peerId);
|
|
857
|
+
if (!peerInfo || !peerInfo.multiaddrs || peerInfo.multiaddrs.length === 0) {
|
|
858
|
+
console.log(`[P2P] No stored addresses for ${peerId}, cannot reconnect`);
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
console.log(`[P2P] Scheduling reconnect for ${peerId} in ${RECONNECT_DELAY_MS}ms`);
|
|
863
|
+
const timer = setTimeout(() => {
|
|
864
|
+
this.reconnectTimers.delete(peerId);
|
|
865
|
+
if (this.node) {
|
|
866
|
+
this.attemptReconnect(peerId, peerInfo);
|
|
867
|
+
}
|
|
868
|
+
}, RECONNECT_DELAY_MS);
|
|
869
|
+
this.reconnectTimers.set(peerId, timer);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
async broadcast(type: string, payload: string): Promise<void> {
|
|
873
|
+
if (!this.node) throw new Error('Node not initialized');
|
|
874
|
+
|
|
875
|
+
const peers = this.node.getPeers();
|
|
876
|
+
let data: Uint8Array;
|
|
877
|
+
if (this.ownDid) {
|
|
878
|
+
data = new TextEncoder().encode(`DID:${this.ownDid}|${type}:${payload}`);
|
|
879
|
+
} else {
|
|
880
|
+
data = new TextEncoder().encode(`${type}:${payload}`);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
for (const peer of peers) {
|
|
884
|
+
const peerIdStr = peer.toString();
|
|
885
|
+
try {
|
|
886
|
+
await this.sendRawMessage(peerIdStr, data);
|
|
887
|
+
} catch (e) {
|
|
888
|
+
console.warn(`[P2P] Failed to broadcast to ${peerIdStr}:`, e);
|
|
889
|
+
this.scheduleReconnect(peerIdStr);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
getPeers(): string[] {
|
|
895
|
+
if (!this.node) return [];
|
|
896
|
+
return this.node.getPeers().map((p: any) => p.toString());
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
getConnectedPeers(): { peerId: string; did?: string; name?: string }[] {
|
|
900
|
+
if (!this.node) return [];
|
|
901
|
+
const result: { peerId: string; did?: string; name?: string }[] = [];
|
|
902
|
+
for (const peer of this.node.getPeers()) {
|
|
903
|
+
const peerIdStr = peer.toString();
|
|
904
|
+
const peerInfo = this.persistentPeers.get(peerIdStr);
|
|
905
|
+
result.push({
|
|
906
|
+
peerId: peerIdStr,
|
|
907
|
+
did: peerInfo?.did,
|
|
908
|
+
name: peerInfo?.name
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
return result;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
setOwnDid(did: string): void {
|
|
915
|
+
this.ownDid = did;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
getNode(): any {
|
|
919
|
+
return this.node;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
async shutdown(): Promise<void> {
|
|
923
|
+
for (const timer of this.reconnectTimers.values()) {
|
|
924
|
+
clearTimeout(timer);
|
|
925
|
+
}
|
|
926
|
+
this.reconnectTimers.clear();
|
|
927
|
+
|
|
928
|
+
if (this.offlineDeliveryInterval) {
|
|
929
|
+
clearInterval(this.offlineDeliveryInterval);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
if (this.messageStore) {
|
|
933
|
+
await this.messageStore.shutdown();
|
|
934
|
+
this.messageStore = null;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
if (this.node) {
|
|
938
|
+
await this.node.stop();
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* 获取消息历史
|
|
944
|
+
*/
|
|
945
|
+
async getMessageHistory(options?: {
|
|
946
|
+
direction?: 'sent' | 'received';
|
|
947
|
+
type?: string;
|
|
948
|
+
limit?: number;
|
|
949
|
+
}): Promise<any[]> {
|
|
950
|
+
if (!this.messageStore) return [];
|
|
951
|
+
return this.messageStore.getMessages(options);
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* 获取待投递消息数量
|
|
956
|
+
*/
|
|
957
|
+
async getPendingOfflineCount(): Promise<number> {
|
|
958
|
+
if (!this.messageStore) return 0;
|
|
959
|
+
return this.messageStore.getPendingOfflineCount();
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
export const p2pNetwork = new P2PNetwork();
|