@bolloon/bolloon-agent 0.4.27 → 0.4.29
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/bin/bolloon.cjs +18 -148
- package/bin/bolloon.cmd +6 -3
- package/dist/agents/execution-supervisor.js +21 -0
- package/dist/agents/pi-sdk-tools.js +3 -1
- package/dist/agents/pi-sdk.js +4 -1
- package/dist/agents/task/local-seller.js +149 -0
- package/dist/agents/task/report-card.js +146 -0
- package/dist/agents/task/resource-advisor.js +182 -0
- package/dist/agents/task/task-budget.js +103 -0
- package/dist/agents/task/task-runner.js +618 -0
- package/dist/agents/x402/goal-run-bridge.js +10 -6
- package/dist/agents/x402/resource-contract.js +12 -1
- package/dist/cli/update-commands.js +349 -0
- package/dist/cli-entry.js +133 -53
- package/dist/electron-build/agents/agent-gateway.js +169 -0
- package/dist/electron-build/agents/agent-gateway.js.map +1 -0
- package/dist/electron-build/agents/agent-identity-store.js +212 -0
- package/dist/electron-build/agents/agent-identity-store.js.map +1 -0
- package/dist/electron-build/agents/agent-identity.js +196 -0
- package/dist/electron-build/agents/agent-identity.js.map +1 -0
- package/dist/electron-build/agents/agent-manifest-protocol.js +151 -0
- package/dist/electron-build/agents/agent-manifest-protocol.js.map +1 -0
- package/dist/electron-build/agents/agent-registry.js +217 -0
- package/dist/electron-build/agents/agent-registry.js.map +1 -0
- package/dist/electron-build/agents/agent-reputation.js +60 -0
- package/dist/electron-build/agents/agent-reputation.js.map +1 -0
- package/dist/electron-build/agents/agent-service-client.js +186 -0
- package/dist/electron-build/agents/agent-service-client.js.map +1 -0
- package/dist/electron-build/agents/browser-cdp.js +765 -0
- package/dist/electron-build/agents/browser-cdp.js.map +1 -0
- package/dist/electron-build/agents/computer-use.js +341 -0
- package/dist/electron-build/agents/computer-use.js.map +1 -0
- package/dist/electron-build/agents/constraint-layer.js +210 -0
- package/dist/electron-build/agents/constraint-layer.js.map +1 -0
- package/dist/electron-build/agents/decision-store.js +292 -0
- package/dist/electron-build/agents/decision-store.js.map +1 -0
- package/dist/electron-build/agents/deny-pipeline.js +125 -0
- package/dist/electron-build/agents/deny-pipeline.js.map +1 -0
- package/dist/electron-build/agents/economic-policy.js +172 -0
- package/dist/electron-build/agents/economic-policy.js.map +1 -0
- package/dist/electron-build/agents/error-classifier.js +125 -0
- package/dist/electron-build/agents/error-classifier.js.map +1 -0
- package/dist/electron-build/agents/execution-supervisor.js +539 -0
- package/dist/electron-build/agents/execution-supervisor.js.map +1 -0
- package/dist/electron-build/agents/external-events.js +171 -0
- package/dist/electron-build/agents/external-events.js.map +1 -0
- package/dist/electron-build/agents/gateway-join.js +315 -0
- package/dist/electron-build/agents/gateway-join.js.map +1 -0
- package/dist/electron-build/agents/gateway-network.js +505 -0
- package/dist/electron-build/agents/gateway-network.js.map +1 -0
- package/dist/electron-build/agents/goal-criteria.js +133 -0
- package/dist/electron-build/agents/goal-criteria.js.map +1 -0
- package/dist/electron-build/agents/goal-resume.js +364 -0
- package/dist/electron-build/agents/goal-resume.js.map +1 -0
- package/dist/electron-build/agents/goal-store.js +587 -0
- package/dist/electron-build/agents/goal-store.js.map +1 -0
- package/dist/electron-build/agents/intent-classifier.js +104 -0
- package/dist/electron-build/agents/intent-classifier.js.map +1 -0
- package/dist/electron-build/agents/iroh-secret.js +70 -0
- package/dist/electron-build/agents/iroh-secret.js.map +1 -0
- package/dist/electron-build/agents/loop-review.js +101 -0
- package/dist/electron-build/agents/loop-review.js.map +1 -0
- package/dist/electron-build/agents/memory-recall.js +154 -0
- package/dist/electron-build/agents/memory-recall.js.map +1 -0
- package/dist/electron-build/agents/p2p-document-tools.js +328 -0
- package/dist/electron-build/agents/p2p-document-tools.js.map +1 -0
- package/dist/electron-build/agents/parse-tool-call.js +564 -0
- package/dist/electron-build/agents/parse-tool-call.js.map +1 -0
- package/dist/electron-build/agents/patch-tool.js +205 -0
- package/dist/electron-build/agents/patch-tool.js.map +1 -0
- package/dist/electron-build/agents/payment-approval.js +183 -0
- package/dist/electron-build/agents/payment-approval.js.map +1 -0
- package/dist/electron-build/agents/payment-gate.js +139 -0
- package/dist/electron-build/agents/payment-gate.js.map +1 -0
- package/dist/electron-build/agents/permission-mode.js +124 -0
- package/dist/electron-build/agents/permission-mode.js.map +1 -0
- package/dist/electron-build/agents/pi-harness.js +268 -0
- package/dist/electron-build/agents/pi-harness.js.map +1 -0
- package/dist/electron-build/agents/pi-sdk-session-factory.js +117 -0
- package/dist/electron-build/agents/pi-sdk-session-factory.js.map +1 -0
- package/dist/electron-build/agents/pi-sdk-session-manager.js +335 -0
- package/dist/electron-build/agents/pi-sdk-session-manager.js.map +1 -0
- package/dist/electron-build/agents/pi-sdk-tools.js +4042 -0
- package/dist/electron-build/agents/pi-sdk-tools.js.map +1 -0
- package/dist/electron-build/agents/pi-sdk-types.js +17 -0
- package/dist/electron-build/agents/pi-sdk-types.js.map +1 -0
- package/dist/electron-build/agents/pi-sdk.js +3325 -0
- package/dist/electron-build/agents/pi-sdk.js.map +1 -0
- package/dist/electron-build/agents/plan-store.js +212 -0
- package/dist/electron-build/agents/plan-store.js.map +1 -0
- package/dist/electron-build/agents/pre-tool-validator.js +200 -0
- package/dist/electron-build/agents/pre-tool-validator.js.map +1 -0
- package/dist/electron-build/agents/process-runner.js +148 -0
- package/dist/electron-build/agents/process-runner.js.map +1 -0
- package/dist/electron-build/agents/react-loop.js +131 -0
- package/dist/electron-build/agents/react-loop.js.map +1 -0
- package/dist/electron-build/agents/resource-store.js +180 -0
- package/dist/electron-build/agents/resource-store.js.map +1 -0
- package/dist/electron-build/agents/resource-token.js +118 -0
- package/dist/electron-build/agents/resource-token.js.map +1 -0
- package/dist/electron-build/agents/resource-wallet.js +84 -0
- package/dist/electron-build/agents/resource-wallet.js.map +1 -0
- package/dist/electron-build/agents/run-store.js +854 -0
- package/dist/electron-build/agents/run-store.js.map +1 -0
- package/dist/electron-build/agents/runner-resolver.js +267 -0
- package/dist/electron-build/agents/runner-resolver.js.map +1 -0
- package/dist/electron-build/agents/session-store.js +294 -0
- package/dist/electron-build/agents/session-store.js.map +1 -0
- package/dist/electron-build/agents/shell-guard.js +465 -0
- package/dist/electron-build/agents/shell-guard.js.map +1 -0
- package/dist/electron-build/agents/shell-tool.js +102 -0
- package/dist/electron-build/agents/shell-tool.js.map +1 -0
- package/dist/electron-build/agents/skill-loader.js +216 -0
- package/dist/electron-build/agents/skill-loader.js.map +1 -0
- package/dist/electron-build/agents/skill-readiness.js +174 -0
- package/dist/electron-build/agents/skill-readiness.js.map +1 -0
- package/dist/electron-build/agents/skill-share.js +394 -0
- package/dist/electron-build/agents/skill-share.js.map +1 -0
- package/dist/electron-build/agents/skill-supervisor-link.js +75 -0
- package/dist/electron-build/agents/skill-supervisor-link.js.map +1 -0
- package/dist/electron-build/agents/skill-writer.js +347 -0
- package/dist/electron-build/agents/skill-writer.js.map +1 -0
- package/dist/electron-build/agents/skills-manager.js +763 -0
- package/dist/electron-build/agents/skills-manager.js.map +1 -0
- package/dist/electron-build/agents/supervisor-host.js +294 -0
- package/dist/electron-build/agents/supervisor-host.js.map +1 -0
- package/dist/electron-build/agents/task/local-seller.js +188 -0
- package/dist/electron-build/agents/task/local-seller.js.map +1 -0
- package/dist/electron-build/agents/task/report-card.js +152 -0
- package/dist/electron-build/agents/task/report-card.js.map +1 -0
- package/dist/electron-build/agents/task/resource-advisor.js +222 -0
- package/dist/electron-build/agents/task/resource-advisor.js.map +1 -0
- package/dist/electron-build/agents/task/task-budget.js +111 -0
- package/dist/electron-build/agents/task/task-budget.js.map +1 -0
- package/dist/electron-build/agents/task/task-runner.js +670 -0
- package/dist/electron-build/agents/task/task-runner.js.map +1 -0
- package/dist/electron-build/agents/task-state.js +266 -0
- package/dist/electron-build/agents/task-state.js.map +1 -0
- package/dist/electron-build/agents/tool-registry.js +141 -0
- package/dist/electron-build/agents/tool-registry.js.map +1 -0
- package/dist/electron-build/agents/treasury-bridge.js +165 -0
- package/dist/electron-build/agents/treasury-bridge.js.map +1 -0
- package/dist/electron-build/agents/user-questions.js +223 -0
- package/dist/electron-build/agents/user-questions.js.map +1 -0
- package/dist/electron-build/agents/verify-recipe.js +90 -0
- package/dist/electron-build/agents/verify-recipe.js.map +1 -0
- package/dist/electron-build/agents/workflow-engine.js +222 -0
- package/dist/electron-build/agents/workflow-engine.js.map +1 -0
- package/dist/electron-build/agents/workflow-pivot-loop.js +988 -0
- package/dist/electron-build/agents/workflow-pivot-loop.js.map +1 -0
- package/dist/electron-build/agents/write-staging.js +133 -0
- package/dist/electron-build/agents/write-staging.js.map +1 -0
- package/dist/electron-build/agents/x402/goal-run-bridge.js +115 -0
- package/dist/electron-build/agents/x402/goal-run-bridge.js.map +1 -0
- package/dist/electron-build/agents/x402/milestone-settlement.js +163 -0
- package/dist/electron-build/agents/x402/milestone-settlement.js.map +1 -0
- package/dist/electron-build/agents/x402/paid-info-protocol.js +274 -0
- package/dist/electron-build/agents/x402/paid-info-protocol.js.map +1 -0
- package/dist/electron-build/agents/x402/paid-info-store.js +409 -0
- package/dist/electron-build/agents/x402/paid-info-store.js.map +1 -0
- package/dist/electron-build/agents/x402/paid-info-tools.js +346 -0
- package/dist/electron-build/agents/x402/paid-info-tools.js.map +1 -0
- package/dist/electron-build/agents/x402/payment-recovery.js +330 -0
- package/dist/electron-build/agents/x402/payment-recovery.js.map +1 -0
- package/dist/electron-build/agents/x402/resource-contract.js +533 -0
- package/dist/electron-build/agents/x402/resource-contract.js.map +1 -0
- package/dist/electron-build/agents/x402/settlement-state.js +432 -0
- package/dist/electron-build/agents/x402/settlement-state.js.map +1 -0
- package/dist/electron-build/agents/x402/trade.js +294 -0
- package/dist/electron-build/agents/x402/trade.js.map +1 -0
- package/dist/electron-build/agents/x402/transaction-protocol.js +109 -0
- package/dist/electron-build/agents/x402/transaction-protocol.js.map +1 -0
- package/dist/electron-build/agents/x402/transaction-store.js +404 -0
- package/dist/electron-build/agents/x402/transaction-store.js.map +1 -0
- package/dist/electron-build/agents/x402/x402Pay.js +377 -0
- package/dist/electron-build/agents/x402/x402Pay.js.map +1 -0
- package/dist/electron-build/bollharness-integration/context-router-judgment.js +269 -0
- package/dist/electron-build/bollharness-integration/context-router-judgment.js.map +1 -0
- package/dist/electron-build/bollharness-integration/context-router.js +584 -0
- package/dist/electron-build/bollharness-integration/context-router.js.map +1 -0
- package/dist/electron-build/bollharness-integration/gate-state-machine.js +362 -0
- package/dist/electron-build/bollharness-integration/gate-state-machine.js.map +1 -0
- package/dist/electron-build/bollharness-integration/gate-transition-hooks.js +136 -0
- package/dist/electron-build/bollharness-integration/gate-transition-hooks.js.map +1 -0
- package/dist/electron-build/bollharness-integration/guard-checker.js +400 -0
- package/dist/electron-build/bollharness-integration/guard-checker.js.map +1 -0
- package/dist/electron-build/bollharness-integration/index.js +123 -0
- package/dist/electron-build/bollharness-integration/index.js.map +1 -0
- package/dist/electron-build/bollharness-integration/integration.js +531 -0
- package/dist/electron-build/bollharness-integration/integration.js.map +1 -0
- package/dist/electron-build/bollharness-integration/skill-adapter.js +608 -0
- package/dist/electron-build/bollharness-integration/skill-adapter.js.map +1 -0
- package/dist/electron-build/bootstrap/bootstrap.js +173 -0
- package/dist/electron-build/bootstrap/bootstrap.js.map +1 -0
- package/dist/electron-build/bootstrap/context-collector.js +358 -0
- package/dist/electron-build/bootstrap/context-collector.js.map +1 -0
- package/dist/electron-build/bootstrap/context-hierarchy.js +262 -0
- package/dist/electron-build/bootstrap/context-hierarchy.js.map +1 -0
- package/dist/electron-build/bootstrap/context-manager.js +211 -0
- package/dist/electron-build/bootstrap/context-manager.js.map +1 -0
- package/dist/electron-build/bootstrap/context-os.js +277 -0
- package/dist/electron-build/bootstrap/context-os.js.map +1 -0
- package/dist/electron-build/bootstrap/event-log.js +203 -0
- package/dist/electron-build/bootstrap/event-log.js.map +1 -0
- package/dist/electron-build/bootstrap/exhaust-scrubber.js +277 -0
- package/dist/electron-build/bootstrap/exhaust-scrubber.js.map +1 -0
- package/dist/electron-build/bootstrap/lifecycle-hooks.js +282 -0
- package/dist/electron-build/bootstrap/lifecycle-hooks.js.map +1 -0
- package/dist/electron-build/bootstrap/memory-compressor.js +377 -0
- package/dist/electron-build/bootstrap/memory-compressor.js.map +1 -0
- package/dist/electron-build/bootstrap/persona-loader.js +219 -0
- package/dist/electron-build/bootstrap/persona-loader.js.map +1 -0
- package/dist/electron-build/bootstrap/project-context.js +162 -0
- package/dist/electron-build/bootstrap/project-context.js.map +1 -0
- package/dist/electron-build/bootstrap/project-state.js +205 -0
- package/dist/electron-build/bootstrap/project-state.js.map +1 -0
- package/dist/electron-build/bootstrap/session-window.js +162 -0
- package/dist/electron-build/bootstrap/session-window.js.map +1 -0
- package/dist/electron-build/bootstrap/vector-index.js +291 -0
- package/dist/electron-build/bootstrap/vector-index.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/cancelOrder.js +22 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/cancelOrder.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/clobShared.js +86 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/clobShared.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/createOrder.js +37 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/createOrder.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/getMarket.js +20 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/getMarket.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/getOrders.js +29 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/getOrders.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/listMarkets.js +28 -0
- package/dist/electron-build/constraint-runtime/src/tools/PolymarketSDK/listMarkets.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/SafeSDK/deploySafe.js +10 -0
- package/dist/electron-build/constraint-runtime/src/tools/SafeSDK/deploySafe.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/autoPay.js +68 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/autoPay.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/createWallet.js +14 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/createWallet.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/getBalance.js +17 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/getBalance.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/importWallet.js +59 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/importWallet.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/sendTransaction.js +29 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/sendTransaction.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/signMessage.js +14 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/signMessage.js.map +1 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/transferToken.js +29 -0
- package/dist/electron-build/constraint-runtime/src/tools/WalletTools/transferToken.js.map +1 -0
- package/dist/electron-build/constraints/index.js +11 -0
- package/dist/electron-build/constraints/index.js.map +1 -0
- package/dist/electron-build/context-compaction/auto-compact.js +182 -0
- package/dist/electron-build/context-compaction/auto-compact.js.map +1 -0
- package/dist/electron-build/context-compaction/budget-gate.js +32 -0
- package/dist/electron-build/context-compaction/budget-gate.js.map +1 -0
- package/dist/electron-build/context-compaction/budget-reduce.js +39 -0
- package/dist/electron-build/context-compaction/budget-reduce.js.map +1 -0
- package/dist/electron-build/context-compaction/context-collapse.js +71 -0
- package/dist/electron-build/context-compaction/context-collapse.js.map +1 -0
- package/dist/electron-build/context-compaction/index.js +38 -0
- package/dist/electron-build/context-compaction/index.js.map +1 -0
- package/dist/electron-build/context-compaction/microcompact.js +55 -0
- package/dist/electron-build/context-compaction/microcompact.js.map +1 -0
- package/dist/electron-build/context-compaction/pipeline.js +128 -0
- package/dist/electron-build/context-compaction/pipeline.js.map +1 -0
- package/dist/electron-build/context-compaction/snip.js +50 -0
- package/dist/electron-build/context-compaction/snip.js.map +1 -0
- package/dist/electron-build/context-compaction/token-estimator.js +40 -0
- package/dist/electron-build/context-compaction/token-estimator.js.map +1 -0
- package/dist/electron-build/context-compaction/types.js +21 -0
- package/dist/electron-build/context-compaction/types.js.map +1 -0
- package/dist/electron-build/cron/tick-lock.js +247 -0
- package/dist/electron-build/cron/tick-lock.js.map +1 -0
- package/dist/electron-build/documents/reader.js +131 -0
- package/dist/electron-build/documents/reader.js.map +1 -0
- package/dist/electron-build/documents/store.js +220 -0
- package/dist/electron-build/documents/store.js.map +1 -0
- package/dist/electron-build/external-engines/delegate-handle.js +76 -0
- package/dist/electron-build/external-engines/delegate-handle.js.map +1 -0
- package/dist/electron-build/external-engines/delegate.js +225 -0
- package/dist/electron-build/external-engines/delegate.js.map +1 -0
- package/dist/electron-build/external-engines/discovery.js +448 -0
- package/dist/electron-build/external-engines/discovery.js.map +1 -0
- package/dist/electron-build/external-engines/types.js +13 -0
- package/dist/electron-build/external-engines/types.js.map +1 -0
- package/dist/electron-build/hooks/hooks-engine.js +367 -0
- package/dist/electron-build/hooks/hooks-engine.js.map +1 -0
- package/dist/electron-build/judgeness/reflect.js +100 -0
- package/dist/electron-build/judgeness/reflect.js.map +1 -0
- package/dist/electron-build/judgeness/store.js +536 -0
- package/dist/electron-build/judgeness/store.js.map +1 -0
- package/dist/electron-build/judgeness/types.js +21 -0
- package/dist/electron-build/judgeness/types.js.map +1 -0
- package/dist/electron-build/llm/config-store.js +551 -0
- package/dist/electron-build/llm/config-store.js.map +1 -0
- package/dist/electron-build/llm/error-lessons.js +76 -0
- package/dist/electron-build/llm/error-lessons.js.map +1 -0
- package/dist/electron-build/llm/pi-ai.js +830 -0
- package/dist/electron-build/llm/pi-ai.js.map +1 -0
- package/dist/electron-build/llm/system-prompt/registry.js +318 -0
- package/dist/electron-build/llm/system-prompt/registry.js.map +1 -0
- package/dist/electron-build/llm/system-prompt/strip-hibsml.js +55 -0
- package/dist/electron-build/llm/system-prompt/strip-hibsml.js.map +1 -0
- package/dist/electron-build/locales/en.js +32 -0
- package/dist/electron-build/locales/en.js.map +1 -0
- package/dist/electron-build/locales/zh.js +30 -0
- package/dist/electron-build/locales/zh.js.map +1 -0
- package/dist/electron-build/lsp/lsp-manager.js +327 -0
- package/dist/electron-build/lsp/lsp-manager.js.map +1 -0
- package/dist/electron-build/lsp/lsp-tools.js +226 -0
- package/dist/electron-build/lsp/lsp-tools.js.map +1 -0
- package/dist/electron-build/migration/external-agent-migrator.js +451 -0
- package/dist/electron-build/migration/external-agent-migrator.js.map +1 -0
- package/dist/electron-build/network/agent-network.js +775 -0
- package/dist/electron-build/network/agent-network.js.map +1 -0
- package/dist/electron-build/network/goal-event-bridge.js +62 -0
- package/dist/electron-build/network/goal-event-bridge.js.map +1 -0
- package/dist/electron-build/network/iroh-integration.js +117 -0
- package/dist/electron-build/network/iroh-integration.js.map +1 -0
- package/dist/electron-build/network/iroh-transport.js +550 -0
- package/dist/electron-build/network/iroh-transport.js.map +1 -0
- package/dist/electron-build/network/p2p.js +1103 -0
- package/dist/electron-build/network/p2p.js.map +1 -0
- package/dist/electron-build/network/storage/adapters/json-adapter.js +416 -0
- package/dist/electron-build/network/storage/adapters/json-adapter.js.map +1 -0
- package/dist/electron-build/network/storage/types.js +18 -0
- package/dist/electron-build/network/storage/types.js.map +1 -0
- package/dist/electron-build/orbitdb/agent-tools.js +402 -0
- package/dist/electron-build/orbitdb/agent-tools.js.map +1 -0
- package/dist/electron-build/orbitdb/cid-database.js +284 -0
- package/dist/electron-build/orbitdb/cid-database.js.map +1 -0
- package/dist/electron-build/orbitdb/context-store.js +99 -0
- package/dist/electron-build/orbitdb/context-store.js.map +1 -0
- package/dist/electron-build/orbitdb/ipfs-node.js +120 -0
- package/dist/electron-build/orbitdb/ipfs-node.js.map +1 -0
- package/dist/electron-build/orbitdb/kanban-store.js +299 -0
- package/dist/electron-build/orbitdb/kanban-store.js.map +1 -0
- package/dist/electron-build/orbitdb/task-store.js +155 -0
- package/dist/electron-build/orbitdb/task-store.js.map +1 -0
- package/dist/electron-build/orbitdb/trajectory-store.js +194 -0
- package/dist/electron-build/orbitdb/trajectory-store.js.map +1 -0
- package/dist/electron-build/orbitdb/ui-cid.js +77 -0
- package/dist/electron-build/orbitdb/ui-cid.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-a2ui/index.js +89 -0
- package/dist/electron-build/pi-ecosystem-a2ui/index.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-goals/index.js +512 -0
- package/dist/electron-build/pi-ecosystem-goals/index.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/adaptive-scan.js +325 -0
- package/dist/electron-build/pi-ecosystem-judgment/adaptive-scan.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/causal-judge.js +492 -0
- package/dist/electron-build/pi-ecosystem-judgment/causal-judge.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/cleanup.js +167 -0
- package/dist/electron-build/pi-ecosystem-judgment/cleanup.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/decision.js +321 -0
- package/dist/electron-build/pi-ecosystem-judgment/decision.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/detect-hook.js +175 -0
- package/dist/electron-build/pi-ecosystem-judgment/detect-hook.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/distill-prompt.js +232 -0
- package/dist/electron-build/pi-ecosystem-judgment/distill-prompt.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/distillation.js +304 -0
- package/dist/electron-build/pi-ecosystem-judgment/distillation.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/evolve-judgment.js +176 -0
- package/dist/electron-build/pi-ecosystem-judgment/evolve-judgment.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/human-value-pipeline.js +65 -0
- package/dist/electron-build/pi-ecosystem-judgment/human-value-pipeline.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/human-value-store.js +808 -0
- package/dist/electron-build/pi-ecosystem-judgment/human-value-store.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/index.js +508 -0
- package/dist/electron-build/pi-ecosystem-judgment/index.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/injection-gate.js +334 -0
- package/dist/electron-build/pi-ecosystem-judgment/injection-gate.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/monitor-gate.js +228 -0
- package/dist/electron-build/pi-ecosystem-judgment/monitor-gate.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-judgment/value-injection.js +598 -0
- package/dist/electron-build/pi-ecosystem-judgment/value-injection.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-mcp/index.js +643 -0
- package/dist/electron-build/pi-ecosystem-mcp/index.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-mcp/ui-tools.js +116 -0
- package/dist/electron-build/pi-ecosystem-mcp/ui-tools.js.map +1 -0
- package/dist/electron-build/pi-ecosystem-subagents/index.js +352 -0
- package/dist/electron-build/pi-ecosystem-subagents/index.js.map +1 -0
- package/dist/electron-build/security/builtin-guards.js +133 -0
- package/dist/electron-build/security/builtin-guards.js.map +1 -0
- package/dist/electron-build/security/context-router-tool.js +111 -0
- package/dist/electron-build/security/context-router-tool.js.map +1 -0
- package/dist/electron-build/security/input-scanner.js +263 -0
- package/dist/electron-build/security/input-scanner.js.map +1 -0
- package/dist/electron-build/security/react-harness.js +148 -0
- package/dist/electron-build/security/react-harness.js.map +1 -0
- package/dist/electron-build/security/tool-gate.js +287 -0
- package/dist/electron-build/security/tool-gate.js.map +1 -0
- package/dist/electron-build/setup/setup-store.js +648 -0
- package/dist/electron-build/setup/setup-store.js.map +1 -0
- package/dist/electron-build/social/channels/ChannelManager.js +444 -0
- package/dist/electron-build/social/channels/ChannelManager.js.map +1 -0
- package/dist/electron-build/social/channels/DiapChannelBridge.js +451 -0
- package/dist/electron-build/social/channels/DiapChannelBridge.js.map +1 -0
- package/dist/electron-build/social/channels/InterestMatcher.js +135 -0
- package/dist/electron-build/social/channels/InterestMatcher.js.map +1 -0
- package/dist/electron-build/social/channels/types.js +21 -0
- package/dist/electron-build/social/channels/types.js.map +1 -0
- package/dist/electron-build/social/global-shared-context.js +368 -0
- package/dist/electron-build/social/global-shared-context.js.map +1 -0
- package/dist/electron-build/social/heartbeat.js +882 -0
- package/dist/electron-build/social/heartbeat.js.map +1 -0
- package/dist/electron-build/storage/did-catalog-bridge.js +314 -0
- package/dist/electron-build/storage/did-catalog-bridge.js.map +1 -0
- package/dist/electron-build/storage/did-catalog.js +272 -0
- package/dist/electron-build/storage/did-catalog.js.map +1 -0
- package/dist/electron-build/utils/auto-update.js +140 -558
- package/dist/electron-build/utils/auto-update.js.map +1 -1
- package/dist/electron-build/utils/module-context.js +98 -0
- package/dist/electron-build/utils/module-context.js.map +1 -0
- package/dist/electron-build/utils/runtime-bootstrap.js +745 -0
- package/dist/electron-build/utils/runtime-bootstrap.js.map +1 -0
- package/dist/electron-build/utils/update-manager.js +770 -0
- package/dist/electron-build/utils/update-manager.js.map +1 -0
- package/dist/electron-build/utils/update-state.js +406 -0
- package/dist/electron-build/utils/update-state.js.map +1 -0
- package/dist/electron-build/utils/version-info.js +562 -0
- package/dist/electron-build/utils/version-info.js.map +1 -0
- package/dist/electron-build/web/i18n.js +146 -0
- package/dist/electron-build/web/i18n.js.map +1 -0
- package/dist/electron-build/web/routes-x402-info.js +232 -0
- package/dist/electron-build/web/routes-x402-info.js.map +1 -0
- package/dist/electron-build/web/server-storage.js +390 -0
- package/dist/electron-build/web/server-storage.js.map +1 -0
- package/dist/electron-build/web/server-types.js +52 -0
- package/dist/electron-build/web/server-types.js.map +1 -0
- package/dist/electron-build/web/task-deps.js +41 -0
- package/dist/electron-build/web/task-deps.js.map +1 -0
- package/dist/index.js +28 -27
- package/dist/llm/system-prompt/registry.js +6 -3
- package/dist/utils/auto-update.js +138 -525
- package/dist/utils/module-context.js +59 -0
- package/dist/utils/runtime-bootstrap.js +682 -0
- package/dist/utils/update-cli.js +82 -0
- package/dist/utils/update-health.js +401 -0
- package/dist/utils/update-manager.js +716 -0
- package/dist/utils/update-state.js +353 -0
- package/dist/utils/version-info.js +510 -0
- package/package.json +1 -1
- package/scripts/build-cli.js +32 -151
- package/scripts/postinstall.js +153 -76
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* browser-cdp.ts — 自包含的真实浏览器自动化模块 (Chrome DevTools Protocol)
|
|
4
|
+
*
|
|
5
|
+
* 设计原则:
|
|
6
|
+
* - 只用 Node 内置能力: child_process.spawn 启动 Chrome, 全局 fetch 轮询
|
|
7
|
+
* /json/version 拿 webSocketDebuggerUrl, 全局 WebSocket 手写轻量 CDP 客户端。
|
|
8
|
+
* - 零 npm 依赖。
|
|
9
|
+
* - 懒启动: 首次调用才拉起 headless Chrome; 后续复用同一浏览器实例。
|
|
10
|
+
* - 空闲 5 分钟自动关闭, 避免 Chrome 常驻吃内存。
|
|
11
|
+
*
|
|
12
|
+
* 导出:
|
|
13
|
+
* - registerBrowserTools(ctx): 注册名为 'browser' 的工具到 ctx.tools
|
|
14
|
+
* - BrowserCdpSession: 可复用会话 (可单独 new 出来直接驱动)
|
|
15
|
+
* - resolveChromePath(): 解析 Chrome 可执行文件路径 (供测试判断是否 skip)
|
|
16
|
+
* - __shutdownBrowserForTest(): 测试收尾清理 (kill Chrome + 关闭 ws)
|
|
17
|
+
*/
|
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.BrowserCdpSession = void 0;
|
|
23
|
+
exports.resolveChromePath = resolveChromePath;
|
|
24
|
+
exports.__shutdownBrowserForTest = __shutdownBrowserForTest;
|
|
25
|
+
exports.registerBrowserTools = registerBrowserTools;
|
|
26
|
+
const node_child_process_1 = require("node:child_process");
|
|
27
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
28
|
+
const node_net_1 = __importDefault(require("node:net"));
|
|
29
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
30
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
31
|
+
/** 会话空闲多久后自动关闭 (5 分钟) */
|
|
32
|
+
const IDLE_SHUTDOWN_MS = 5 * 60 * 1000;
|
|
33
|
+
/** 默认单次请求超时 */
|
|
34
|
+
const DEFAULT_TIMEOUT_MS = 20000;
|
|
35
|
+
/** text/html 输出截断长度 */
|
|
36
|
+
const TRUNCATE_LEN = 8000;
|
|
37
|
+
/** links 最多返回条数 */
|
|
38
|
+
const MAX_LINKS = 50;
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Chrome 可执行文件解析
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
/** 用 `which` 找可执行文件, 找不到返回 null */
|
|
43
|
+
function whichSync(bin) {
|
|
44
|
+
try {
|
|
45
|
+
const out = (0, node_child_process_1.execFileSync)('which', [bin], {
|
|
46
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
47
|
+
encoding: 'utf8'
|
|
48
|
+
}).trim();
|
|
49
|
+
return out && node_fs_1.default.existsSync(out) ? out : null;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 解析 Chrome 可执行文件路径。优先 env BOLLOON_CHROME_PATH, 其次各平台常见位置。
|
|
57
|
+
* 找不到返回 null (调用方据此返回 "未找到 Chrome 可执行文件")。
|
|
58
|
+
*/
|
|
59
|
+
function resolveChromePath() {
|
|
60
|
+
// 1) 环境变量显式指定
|
|
61
|
+
const envPath = process.env.BOLLOON_CHROME_PATH;
|
|
62
|
+
if (envPath && node_fs_1.default.existsSync(envPath))
|
|
63
|
+
return envPath;
|
|
64
|
+
// 2) macOS 标准安装位置
|
|
65
|
+
if (process.platform === 'darwin') {
|
|
66
|
+
const candidates = [
|
|
67
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
68
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
69
|
+
'/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
|
|
70
|
+
node_path_1.default.join(node_os_1.default.homedir(), 'Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
|
|
71
|
+
];
|
|
72
|
+
for (const p of candidates)
|
|
73
|
+
if (node_fs_1.default.existsSync(p))
|
|
74
|
+
return p;
|
|
75
|
+
}
|
|
76
|
+
// 3) Linux/macOS: which google-chrome / chromium / chrome / chromium-browser
|
|
77
|
+
for (const bin of ['google-chrome', 'google-chrome-stable', 'chromium', 'chromium-browser', 'chrome']) {
|
|
78
|
+
const found = whichSync(bin);
|
|
79
|
+
if (found)
|
|
80
|
+
return found;
|
|
81
|
+
}
|
|
82
|
+
// 4) Windows 常见路径
|
|
83
|
+
if (process.platform === 'win32') {
|
|
84
|
+
const pf = process.env.PROGRAMFILES || 'C:\\Program Files';
|
|
85
|
+
const pf86 = process.env['PROGRAMFILES(X86)'] || 'C:\\Program Files (x86)';
|
|
86
|
+
const local = process.env.LOCALAPPDATA || '';
|
|
87
|
+
const winCandidates = [
|
|
88
|
+
node_path_1.default.join(pf, 'Google\\Chrome\\Application\\chrome.exe'),
|
|
89
|
+
node_path_1.default.join(pf86, 'Google\\Chrome\\Application\\chrome.exe'),
|
|
90
|
+
local ? node_path_1.default.join(local, 'Google\\Chrome\\Application\\chrome.exe') : ''
|
|
91
|
+
];
|
|
92
|
+
for (const p of winCandidates)
|
|
93
|
+
if (p && node_fs_1.default.existsSync(p))
|
|
94
|
+
return p;
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
// 工具函数
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
102
|
+
/** 拿一个空闲端口 (bind 0 让系统分配, 取到端口后立刻释放) */
|
|
103
|
+
function getFreePort() {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
const srv = node_net_1.default.createServer();
|
|
106
|
+
srv.unref();
|
|
107
|
+
srv.once('error', reject);
|
|
108
|
+
srv.listen(0, '127.0.0.1', () => {
|
|
109
|
+
const addr = srv.address();
|
|
110
|
+
const port = typeof addr === 'object' && addr ? addr.port : 0;
|
|
111
|
+
srv.close(() => resolve(port));
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/** 轮询 http://127.0.0.1:<port>/json/version, 拿到 webSocketDebuggerUrl */
|
|
116
|
+
async function fetchBrowserWsUrl(port, timeoutMs) {
|
|
117
|
+
const deadline = Date.now() + timeoutMs;
|
|
118
|
+
let lastErr = null;
|
|
119
|
+
while (Date.now() < deadline) {
|
|
120
|
+
try {
|
|
121
|
+
const res = await fetch(`http://127.0.0.1:${port}/json/version`);
|
|
122
|
+
if (res.ok) {
|
|
123
|
+
const j = (await res.json());
|
|
124
|
+
if (j.webSocketDebuggerUrl)
|
|
125
|
+
return j.webSocketDebuggerUrl;
|
|
126
|
+
}
|
|
127
|
+
lastErr = new Error(`/json/version 返回 ${res.status}`);
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
lastErr = e;
|
|
131
|
+
}
|
|
132
|
+
await sleep(150);
|
|
133
|
+
}
|
|
134
|
+
throw new Error(`等待 Chrome DevTools 就绪超时 (端口 ${port}): ${String(lastErr)}`);
|
|
135
|
+
}
|
|
136
|
+
/** 把错误里可能带 connectionLost 标记的异常识别为"连接断开, 可重建" */
|
|
137
|
+
function isConnectionLost(e) {
|
|
138
|
+
return !!(e && typeof e === 'object' && e.connectionLost === true);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 极简 CDP 客户端:
|
|
142
|
+
* - 一个浏览器级 WebSocket 连接 (来自 /json/version)
|
|
143
|
+
* - id → pending 表, 每个请求独立超时
|
|
144
|
+
* - 支持 sessionId 透传 (Target.attachToTarget({flatten:true}) 之后的页面级命令)
|
|
145
|
+
* - 支持等待一次性事件 (如 Page.loadEventFired)
|
|
146
|
+
*/
|
|
147
|
+
class CdpClient {
|
|
148
|
+
wsUrl;
|
|
149
|
+
ws = null;
|
|
150
|
+
nextId = 1;
|
|
151
|
+
pending = new Map();
|
|
152
|
+
eventWaiters = new Map();
|
|
153
|
+
closed = false;
|
|
154
|
+
constructor(wsUrl) {
|
|
155
|
+
this.wsUrl = wsUrl;
|
|
156
|
+
}
|
|
157
|
+
/** 建立 ws 连接 */
|
|
158
|
+
async connect(timeoutMs) {
|
|
159
|
+
await new Promise((resolve, reject) => {
|
|
160
|
+
const ws = new WebSocket(this.wsUrl);
|
|
161
|
+
const timer = setTimeout(() => {
|
|
162
|
+
try {
|
|
163
|
+
ws.close();
|
|
164
|
+
}
|
|
165
|
+
catch { /* ignore */ }
|
|
166
|
+
reject(new Error('WebSocket 连接 CDP 超时'));
|
|
167
|
+
}, timeoutMs);
|
|
168
|
+
ws.addEventListener('open', () => {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
this.ws = ws;
|
|
171
|
+
resolve();
|
|
172
|
+
}, { once: true });
|
|
173
|
+
ws.addEventListener('error', () => {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
reject(new Error('WebSocket 连接 CDP 失败'));
|
|
176
|
+
}, { once: true });
|
|
177
|
+
ws.addEventListener('message', (ev) => this.onMessage(ev));
|
|
178
|
+
ws.addEventListener('close', () => this.onClose());
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
onMessage(ev) {
|
|
182
|
+
let msg;
|
|
183
|
+
try {
|
|
184
|
+
msg = JSON.parse(typeof ev.data === 'string' ? ev.data : String(ev.data));
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// 1) 命令响应 (带 id)
|
|
190
|
+
if (msg.id != null && this.pending.has(msg.id)) {
|
|
191
|
+
const entry = this.pending.get(msg.id);
|
|
192
|
+
this.pending.delete(msg.id);
|
|
193
|
+
clearTimeout(entry.timer);
|
|
194
|
+
if (msg.error) {
|
|
195
|
+
entry.reject(new Error(`CDP ${msg.error.message || 'unknown error'}`));
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
entry.resolve(msg.result);
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
// 2) 事件 (带 method) — 分发给等待者
|
|
203
|
+
if (msg.method) {
|
|
204
|
+
const waiters = this.eventWaiters.get(msg.method);
|
|
205
|
+
if (waiters) {
|
|
206
|
+
for (const w of Array.from(waiters)) {
|
|
207
|
+
if (w.sessionId && msg.sessionId && w.sessionId !== msg.sessionId)
|
|
208
|
+
continue;
|
|
209
|
+
waiters.delete(w);
|
|
210
|
+
w.resolve(msg.params);
|
|
211
|
+
}
|
|
212
|
+
if (waiters.size === 0)
|
|
213
|
+
this.eventWaiters.delete(msg.method);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
onClose() {
|
|
218
|
+
this.closed = true;
|
|
219
|
+
// 连接断开 → 拒绝所有挂起请求, 并打 connectionLost 标记 (触发上层重建)
|
|
220
|
+
for (const [, entry] of this.pending) {
|
|
221
|
+
clearTimeout(entry.timer);
|
|
222
|
+
const err = new Error('CDP 连接已断开');
|
|
223
|
+
err.connectionLost = true;
|
|
224
|
+
entry.reject(err);
|
|
225
|
+
}
|
|
226
|
+
this.pending.clear();
|
|
227
|
+
for (const [, set] of this.eventWaiters)
|
|
228
|
+
for (const w of set)
|
|
229
|
+
w.resolve(null);
|
|
230
|
+
this.eventWaiters.clear();
|
|
231
|
+
}
|
|
232
|
+
get isOpen() {
|
|
233
|
+
return !this.closed && !!this.ws && this.ws.readyState === 1 /* OPEN */;
|
|
234
|
+
}
|
|
235
|
+
/** 发一条 CDP 命令并等结果 */
|
|
236
|
+
request(method, params = {}, sessionId, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
237
|
+
if (!this.isOpen || !this.ws) {
|
|
238
|
+
const err = new Error('CDP 连接未就绪');
|
|
239
|
+
err.connectionLost = true;
|
|
240
|
+
return Promise.reject(err);
|
|
241
|
+
}
|
|
242
|
+
const id = this.nextId++;
|
|
243
|
+
const payload = { id, method, params };
|
|
244
|
+
if (sessionId)
|
|
245
|
+
payload.sessionId = sessionId;
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
const timer = setTimeout(() => {
|
|
248
|
+
this.pending.delete(id);
|
|
249
|
+
reject(new Error(`CDP 命令超时: ${method} (${timeoutMs}ms)`));
|
|
250
|
+
}, timeoutMs);
|
|
251
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
252
|
+
try {
|
|
253
|
+
this.ws.send(JSON.stringify(payload));
|
|
254
|
+
}
|
|
255
|
+
catch (e) {
|
|
256
|
+
clearTimeout(timer);
|
|
257
|
+
this.pending.delete(id);
|
|
258
|
+
const err = new Error(`CDP 发送失败: ${String(e)}`);
|
|
259
|
+
err.connectionLost = true;
|
|
260
|
+
reject(err);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/** 等一次事件; 超时返回 null (不算失败) */
|
|
265
|
+
waitForEvent(method, sessionId, timeoutMs) {
|
|
266
|
+
return new Promise((resolve) => {
|
|
267
|
+
const waiter = { sessionId, resolve };
|
|
268
|
+
let set = this.eventWaiters.get(method);
|
|
269
|
+
if (!set) {
|
|
270
|
+
set = new Set();
|
|
271
|
+
this.eventWaiters.set(method, set);
|
|
272
|
+
}
|
|
273
|
+
set.add(waiter);
|
|
274
|
+
setTimeout(() => {
|
|
275
|
+
const cur = this.eventWaiters.get(method);
|
|
276
|
+
if (cur && cur.delete(waiter))
|
|
277
|
+
resolve(null);
|
|
278
|
+
}, timeoutMs);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
async close() {
|
|
282
|
+
this.closed = true;
|
|
283
|
+
try {
|
|
284
|
+
this.ws?.close();
|
|
285
|
+
}
|
|
286
|
+
catch { /* ignore */ }
|
|
287
|
+
this.ws = null;
|
|
288
|
+
for (const [, entry] of this.pending) {
|
|
289
|
+
clearTimeout(entry.timer);
|
|
290
|
+
entry.reject(new Error('CDP 客户端已关闭'));
|
|
291
|
+
}
|
|
292
|
+
this.pending.clear();
|
|
293
|
+
this.eventWaiters.clear();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
// ---------------------------------------------------------------------------
|
|
297
|
+
// BrowserCdpSession
|
|
298
|
+
// ---------------------------------------------------------------------------
|
|
299
|
+
/**
|
|
300
|
+
* 可复用的 Chrome CDP 会话。
|
|
301
|
+
* - 懒启动: 第一次 execute 时才 spawn Chrome。
|
|
302
|
+
* - 复用: 同一浏览器 + 同一页面 target, 多次调用共享状态 (登录/滚动位置等)。
|
|
303
|
+
* - 空闲 5 分钟自动关闭。
|
|
304
|
+
*/
|
|
305
|
+
class BrowserCdpSession {
|
|
306
|
+
chromeProc = null;
|
|
307
|
+
cdp = null;
|
|
308
|
+
port = 0;
|
|
309
|
+
userDataDir = null;
|
|
310
|
+
sessionId = null;
|
|
311
|
+
targetId = null;
|
|
312
|
+
idleTimer = null;
|
|
313
|
+
reqTimeout = DEFAULT_TIMEOUT_MS;
|
|
314
|
+
/** 当前是否已启动 (供测试断言状态清理) */
|
|
315
|
+
get isRunning() {
|
|
316
|
+
return !!this.chromeProc && this.chromeProc.exitCode === null && !this.chromeProc.killed && this.cdp?.isOpen === true;
|
|
317
|
+
}
|
|
318
|
+
/** 启动 Chrome + 建立 CDP 连接 (幂等) */
|
|
319
|
+
async ensureStarted() {
|
|
320
|
+
if (this.isRunning)
|
|
321
|
+
return;
|
|
322
|
+
// 半启动状态残留 → 先清理
|
|
323
|
+
if (this.chromeProc || this.cdp)
|
|
324
|
+
await this.forceStop();
|
|
325
|
+
const chromePath = resolveChromePath();
|
|
326
|
+
if (!chromePath) {
|
|
327
|
+
throw new Error('未找到 Chrome 可执行文件 (BOLLOON_CHROME_PATH 可指定)');
|
|
328
|
+
}
|
|
329
|
+
// 拿一个空闲端口 (bind 0 由系统分配) 再交给 Chrome, 避免端口冲突
|
|
330
|
+
this.port = await getFreePort();
|
|
331
|
+
this.userDataDir = node_fs_1.default.mkdtempSync(node_path_1.default.join(node_os_1.default.tmpdir(), 'bolloon-chrome-'));
|
|
332
|
+
const args = [
|
|
333
|
+
'--headless=new',
|
|
334
|
+
`--remote-debugging-port=${this.port}`,
|
|
335
|
+
`--user-data-dir=${this.userDataDir}`,
|
|
336
|
+
'--no-first-run',
|
|
337
|
+
'--no-default-browser-check',
|
|
338
|
+
'--disable-gpu',
|
|
339
|
+
'--disable-extensions',
|
|
340
|
+
'--window-size=1280,900',
|
|
341
|
+
'about:blank'
|
|
342
|
+
];
|
|
343
|
+
this.chromeProc = (0, node_child_process_1.spawn)(chromePath, args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
|
344
|
+
// Chrome 的 stderr 噪音很多, 吞掉即可 (不解析)
|
|
345
|
+
this.chromeProc.stderr?.on('data', () => { });
|
|
346
|
+
this.chromeProc.on('exit', () => {
|
|
347
|
+
// 进程退出 → 让客户端知道连接已失效
|
|
348
|
+
this.cdp?.close().catch(() => { });
|
|
349
|
+
});
|
|
350
|
+
// 轮询 /json/version 等 Chrome 就绪
|
|
351
|
+
const wsUrl = await fetchBrowserWsUrl(this.port, this.reqTimeout);
|
|
352
|
+
// 连接浏览器级 ws, 然后创建一个页面 target 并 attach (flatten) 拿 sessionId
|
|
353
|
+
this.cdp = new CdpClient(wsUrl);
|
|
354
|
+
await this.cdp.connect(this.reqTimeout);
|
|
355
|
+
const created = await this.cdp.request('Target.createTarget', { url: 'about:blank' }, undefined, this.reqTimeout);
|
|
356
|
+
this.targetId = created.targetId;
|
|
357
|
+
const attached = await this.cdp.request('Target.attachToTarget', { targetId: this.targetId, flatten: true }, undefined, this.reqTimeout);
|
|
358
|
+
this.sessionId = attached.sessionId;
|
|
359
|
+
// 打开 Page 域, 这样后面才能收到 Page.loadEventFired
|
|
360
|
+
await this.cdp.request('Page.enable', {}, this.sessionId, this.reqTimeout);
|
|
361
|
+
}
|
|
362
|
+
/** 只 kill Chrome + 关闭 ws, 保留会话对象 (可再次 ensureStarted) */
|
|
363
|
+
async forceStop() {
|
|
364
|
+
try {
|
|
365
|
+
await this.cdp?.close();
|
|
366
|
+
}
|
|
367
|
+
catch { /* ignore */ }
|
|
368
|
+
this.cdp = null;
|
|
369
|
+
this.sessionId = null;
|
|
370
|
+
this.targetId = null;
|
|
371
|
+
const proc = this.chromeProc;
|
|
372
|
+
this.chromeProc = null;
|
|
373
|
+
if (proc && proc.exitCode === null && !proc.killed) {
|
|
374
|
+
// 等进程真正退出再删临时目录, 否则 SIGKILL 后 Chrome 仍持有文件锁导致 rmSync 失败
|
|
375
|
+
await new Promise((resolve) => {
|
|
376
|
+
const timer = setTimeout(resolve, 2000);
|
|
377
|
+
proc.once('exit', () => { clearTimeout(timer); resolve(); });
|
|
378
|
+
try {
|
|
379
|
+
proc.kill('SIGKILL');
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
clearTimeout(timer);
|
|
383
|
+
resolve();
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
if (this.userDataDir) {
|
|
388
|
+
// 带重试的目录清理 (文件锁释放有延迟)
|
|
389
|
+
for (let i = 0; i < 3; i++) {
|
|
390
|
+
try {
|
|
391
|
+
node_fs_1.default.rmSync(this.userDataDir, { recursive: true, force: true });
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
catch {
|
|
395
|
+
await sleep(150);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
this.userDataDir = null;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
/** 彻底关闭会话 (kill Chrome + 关 ws + 清理状态 + 停掉空闲计时器) */
|
|
402
|
+
async shutdown() {
|
|
403
|
+
if (this.idleTimer) {
|
|
404
|
+
clearTimeout(this.idleTimer);
|
|
405
|
+
this.idleTimer = null;
|
|
406
|
+
}
|
|
407
|
+
await this.forceStop();
|
|
408
|
+
}
|
|
409
|
+
/** 刷新空闲计时器: 每次调用后重置, 5 分钟无活动自动关闭 */
|
|
410
|
+
resetIdle() {
|
|
411
|
+
if (this.idleTimer)
|
|
412
|
+
clearTimeout(this.idleTimer);
|
|
413
|
+
this.idleTimer = setTimeout(() => {
|
|
414
|
+
this.shutdown().catch(() => { });
|
|
415
|
+
}, IDLE_SHUTDOWN_MS);
|
|
416
|
+
// 不要因为这个计时器把进程挂住
|
|
417
|
+
this.idleTimer.unref?.();
|
|
418
|
+
}
|
|
419
|
+
// -------------------------------------------------------------------------
|
|
420
|
+
// 底层便捷封装
|
|
421
|
+
// -------------------------------------------------------------------------
|
|
422
|
+
/** 页面级 CDP 命令 (自动带 sessionId + 超时) */
|
|
423
|
+
page(method, params = {}) {
|
|
424
|
+
if (!this.cdp || !this.sessionId)
|
|
425
|
+
throw new Error('浏览器会话未启动');
|
|
426
|
+
return this.cdp.request(method, params, this.sessionId, this.reqTimeout);
|
|
427
|
+
}
|
|
428
|
+
/** 在页面里跑一段 JS, returnByValue */
|
|
429
|
+
async evaluate(expression, awaitPromise = false) {
|
|
430
|
+
const r = await this.page('Runtime.evaluate', {
|
|
431
|
+
expression,
|
|
432
|
+
returnByValue: true,
|
|
433
|
+
awaitPromise
|
|
434
|
+
});
|
|
435
|
+
if (r?.exceptionDetails) {
|
|
436
|
+
const desc = r.exceptionDetails.exception?.description || r.exceptionDetails.text || 'JS 执行异常';
|
|
437
|
+
throw new Error(String(desc));
|
|
438
|
+
}
|
|
439
|
+
return r?.result?.value;
|
|
440
|
+
}
|
|
441
|
+
/** 坐标点击 (真实鼠标事件) */
|
|
442
|
+
async dispatchClick(x, y) {
|
|
443
|
+
await this.page('Input.dispatchMouseEvent', { type: 'mousePressed', x, y, button: 'left', clickCount: 1 });
|
|
444
|
+
await this.page('Input.dispatchMouseEvent', { type: 'mouseReleased', x, y, button: 'left', clickCount: 1 });
|
|
445
|
+
}
|
|
446
|
+
// -------------------------------------------------------------------------
|
|
447
|
+
// 主入口
|
|
448
|
+
// -------------------------------------------------------------------------
|
|
449
|
+
/**
|
|
450
|
+
* 执行一次工具调用。
|
|
451
|
+
* 全程 try/catch, 永远返回结构化结果, 不向调用方抛异常。
|
|
452
|
+
*/
|
|
453
|
+
async execute(args) {
|
|
454
|
+
const action = String(args?.action ?? '').trim();
|
|
455
|
+
if (!action)
|
|
456
|
+
return { success: false, error: 'action 必填' };
|
|
457
|
+
// close 不需要启动 Chrome, 直接关
|
|
458
|
+
if (action === 'close') {
|
|
459
|
+
await this.shutdown();
|
|
460
|
+
return { success: true, output: '浏览器会话已关闭' };
|
|
461
|
+
}
|
|
462
|
+
// 解析超时
|
|
463
|
+
const t = parseInt(String(args.timeoutMs ?? ''), 10);
|
|
464
|
+
this.reqTimeout = Number.isFinite(t) && t > 0 ? t : DEFAULT_TIMEOUT_MS;
|
|
465
|
+
this.resetIdle();
|
|
466
|
+
// 最多 2 次: 第一次连接断开/进程崩溃则重建一次
|
|
467
|
+
let rebuilt = false;
|
|
468
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
469
|
+
try {
|
|
470
|
+
await this.ensureStarted();
|
|
471
|
+
return await this.runAction(action, args);
|
|
472
|
+
}
|
|
473
|
+
catch (e) {
|
|
474
|
+
if (!rebuilt && (isConnectionLost(e) || this.chromeProc === null)) {
|
|
475
|
+
rebuilt = true;
|
|
476
|
+
await this.forceStop();
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
return { success: false, error: e instanceof Error ? e.message : String(e) };
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return { success: false, error: '浏览器会话重建后仍失败' };
|
|
483
|
+
}
|
|
484
|
+
/** action 分发 */
|
|
485
|
+
async runAction(action, args) {
|
|
486
|
+
switch (action) {
|
|
487
|
+
case 'open':
|
|
488
|
+
return this.actionOpen(args);
|
|
489
|
+
case 'text':
|
|
490
|
+
return this.actionText(args);
|
|
491
|
+
case 'html':
|
|
492
|
+
return this.actionHtml(args);
|
|
493
|
+
case 'links':
|
|
494
|
+
return this.actionLinks();
|
|
495
|
+
case 'screenshot':
|
|
496
|
+
return this.actionScreenshot(args);
|
|
497
|
+
case 'click':
|
|
498
|
+
return this.actionClick(args);
|
|
499
|
+
case 'type':
|
|
500
|
+
return this.actionType(args);
|
|
501
|
+
case 'key':
|
|
502
|
+
return this.actionKey(args);
|
|
503
|
+
case 'js':
|
|
504
|
+
return this.actionJs(args);
|
|
505
|
+
case 'back':
|
|
506
|
+
return this.actionBack(args);
|
|
507
|
+
default:
|
|
508
|
+
return { success: false, error: `未知 action: ${action}` };
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
/** open: Page.navigate + 等 Page.loadEventFired (超时不算失败, output 注明) */
|
|
512
|
+
async actionOpen(args) {
|
|
513
|
+
const url = String(args.url ?? '').trim();
|
|
514
|
+
if (!url)
|
|
515
|
+
return { success: false, error: 'open 需要 url' };
|
|
516
|
+
// 先挂上 load 事件监听, 再 navigate, 避免竞态丢事件
|
|
517
|
+
const loadWait = this.cdp.waitForEvent('Page.loadEventFired', this.sessionId, this.reqTimeout);
|
|
518
|
+
const nav = await this.page('Page.navigate', { url });
|
|
519
|
+
if (nav?.errorText) {
|
|
520
|
+
return { success: false, error: `导航失败: ${nav.errorText}` };
|
|
521
|
+
}
|
|
522
|
+
const loaded = await loadWait;
|
|
523
|
+
const title = await this.evaluate('document.title').catch(() => '');
|
|
524
|
+
if (loaded === null) {
|
|
525
|
+
return { success: true, output: `已打开 (load 事件等待超时, 页面可能仍在加载): ${url}\n标题: ${title ?? ''}` };
|
|
526
|
+
}
|
|
527
|
+
return { success: true, output: `已打开: ${url}\n标题: ${title ?? ''}` };
|
|
528
|
+
}
|
|
529
|
+
/** text: Runtime.evaluate 取 document.body.innerText (可带 selector), 截断 8000 */
|
|
530
|
+
async actionText(args) {
|
|
531
|
+
const selector = String(args.selector ?? '').trim();
|
|
532
|
+
const expr = selector
|
|
533
|
+
? `(() => { const el = document.querySelector(${JSON.stringify(selector)}); return el ? (el.innerText || '') : ''; })()`
|
|
534
|
+
: `(document.body ? document.body.innerText : '')`;
|
|
535
|
+
const raw = String((await this.evaluate(expr)) ?? '');
|
|
536
|
+
const text = raw.length > TRUNCATE_LEN ? raw.slice(0, TRUNCATE_LEN) + '\n...(已截断)' : raw;
|
|
537
|
+
return { success: true, output: text };
|
|
538
|
+
}
|
|
539
|
+
/** html: 取 documentElement.outerHTML, 截断 8000 */
|
|
540
|
+
async actionHtml(args) {
|
|
541
|
+
const selector = String(args.selector ?? '').trim();
|
|
542
|
+
const expr = selector
|
|
543
|
+
? `(() => { const el = document.querySelector(${JSON.stringify(selector)}); return el ? el.outerHTML : ''; })()`
|
|
544
|
+
: `document.documentElement.outerHTML`;
|
|
545
|
+
const raw = String((await this.evaluate(expr)) ?? '');
|
|
546
|
+
const html = raw.length > TRUNCATE_LEN ? raw.slice(0, TRUNCATE_LEN) + '\n...(已截断)' : raw;
|
|
547
|
+
return { success: true, output: html };
|
|
548
|
+
}
|
|
549
|
+
/** links: 取 a[href] 的 text + href, 最多 50 条 */
|
|
550
|
+
async actionLinks() {
|
|
551
|
+
const value = await this.evaluate(`Array.from(document.querySelectorAll('a[href]')).slice(0, ${MAX_LINKS}).map(a => ({
|
|
552
|
+
text: (a.innerText || a.textContent || '').trim().slice(0, 120),
|
|
553
|
+
href: a.href
|
|
554
|
+
}))`);
|
|
555
|
+
const list = Array.isArray(value) ? value : [];
|
|
556
|
+
if (list.length === 0)
|
|
557
|
+
return { success: true, output: '(未找到链接)' };
|
|
558
|
+
const out = list.map((l, i) => `${i + 1}. ${l.text || '(无文本)'} → ${l.href}`).join('\n');
|
|
559
|
+
return { success: true, output: out };
|
|
560
|
+
}
|
|
561
|
+
/** screenshot: Page.captureScreenshot, fullPage 时用 captureBeyondViewport:true */
|
|
562
|
+
async actionScreenshot(args) {
|
|
563
|
+
const fullPage = String(args.fullPage ?? '').toLowerCase() === 'true';
|
|
564
|
+
const r = await this.page('Page.captureScreenshot', {
|
|
565
|
+
format: 'png',
|
|
566
|
+
captureBeyondViewport: fullPage
|
|
567
|
+
});
|
|
568
|
+
const data = r?.data;
|
|
569
|
+
if (!data)
|
|
570
|
+
return { success: false, error: '截图失败: 未返回 data' };
|
|
571
|
+
const file = node_path_1.default.join(node_os_1.default.tmpdir(), `bolloon-browser-${Date.now()}.png`);
|
|
572
|
+
node_fs_1.default.writeFileSync(file, Buffer.from(data, 'base64'));
|
|
573
|
+
return { success: true, output: `截图已保存: ${file}`, screenshotPath: file };
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* click: 有 selector 时先 scrollIntoView, 取 getBoundingClientRect 中心后用
|
|
577
|
+
* Input.dispatchMouseEvent 发真实鼠标事件; 元素不可见时退回 el.click()。
|
|
578
|
+
* 坐标模式 (x/y) 直接 Input.dispatchMouseEvent。
|
|
579
|
+
*/
|
|
580
|
+
async actionClick(args) {
|
|
581
|
+
const selector = String(args.selector ?? '').trim();
|
|
582
|
+
if (selector) {
|
|
583
|
+
const info = await this.evaluate(`(() => {
|
|
584
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
585
|
+
if (!el) return { found: false };
|
|
586
|
+
el.scrollIntoView({ block: 'center', inline: 'center' });
|
|
587
|
+
const r = el.getBoundingClientRect();
|
|
588
|
+
return { found: true, x: r.left + r.width / 2, y: r.top + r.height / 2, w: r.width, h: r.height, tag: el.tagName };
|
|
589
|
+
})()`);
|
|
590
|
+
if (!info?.found)
|
|
591
|
+
return { success: false, error: `click 找不到元素: ${selector}` };
|
|
592
|
+
if (info.w > 0 && info.h > 0) {
|
|
593
|
+
// 真实鼠标事件 (验证真实点击行为)
|
|
594
|
+
await this.dispatchClick(Math.round(info.x), Math.round(info.y));
|
|
595
|
+
return { success: true, output: `已点击 <${String(info.tag).toLowerCase()}> ${selector} @ (${Math.round(info.x)},${Math.round(info.y)})` };
|
|
596
|
+
}
|
|
597
|
+
// 元素尺寸为 0 (隐藏) → 退回直接 el.click()
|
|
598
|
+
await this.evaluate(`(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (el) el.click(); })()`);
|
|
599
|
+
return { success: true, output: `已 el.click() (元素不可见): ${selector}` };
|
|
600
|
+
}
|
|
601
|
+
// 坐标模式
|
|
602
|
+
const x = Number(args.x);
|
|
603
|
+
const y = Number(args.y);
|
|
604
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
605
|
+
return { success: false, error: 'click 需要 selector 或 x/y 坐标' };
|
|
606
|
+
}
|
|
607
|
+
await this.dispatchClick(Math.round(x), Math.round(y));
|
|
608
|
+
return { success: true, output: `已点击坐标 (${Math.round(x)},${Math.round(y)})` };
|
|
609
|
+
}
|
|
610
|
+
/** type: 有 selector 先聚焦/点击, 再 Input.insertText */
|
|
611
|
+
async actionType(args) {
|
|
612
|
+
const text = String(args.text ?? '');
|
|
613
|
+
const selector = String(args.selector ?? '').trim();
|
|
614
|
+
if (selector) {
|
|
615
|
+
const ok = await this.evaluate(`(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) return false; el.scrollIntoView({block:'center'}); el.focus(); if (el.click) el.click(); return true; })()`);
|
|
616
|
+
if (!ok)
|
|
617
|
+
return { success: false, error: `type 找不到元素: ${selector}` };
|
|
618
|
+
}
|
|
619
|
+
// insertText: 直接把文本插入当前焦点, 不模拟逐键
|
|
620
|
+
await this.page('Input.insertText', { text });
|
|
621
|
+
return { success: true, output: `已输入 ${text.length} 字符${selector ? ` → ${selector}` : ''}` };
|
|
622
|
+
}
|
|
623
|
+
/** key: Input.dispatchKeyEvent (rawKeyDown [+ char] + keyUp) */
|
|
624
|
+
async actionKey(args) {
|
|
625
|
+
const key = String(args.key ?? '').trim();
|
|
626
|
+
if (!key)
|
|
627
|
+
return { success: false, error: 'key 必填' };
|
|
628
|
+
// 常用键 → { key, code, vk, text }
|
|
629
|
+
const table = {
|
|
630
|
+
Enter: { key: 'Enter', code: 'Enter', vk: 13, text: '\r' },
|
|
631
|
+
Tab: { key: 'Tab', code: 'Tab', vk: 9 },
|
|
632
|
+
Escape: { key: 'Escape', code: 'Escape', vk: 27 },
|
|
633
|
+
Esc: { key: 'Escape', code: 'Escape', vk: 27 },
|
|
634
|
+
Backspace: { key: 'Backspace', code: 'Backspace', vk: 8 },
|
|
635
|
+
Delete: { key: 'Delete', code: 'Delete', vk: 46 },
|
|
636
|
+
ArrowUp: { key: 'ArrowUp', code: 'ArrowUp', vk: 38 },
|
|
637
|
+
ArrowDown: { key: 'ArrowDown', code: 'ArrowDown', vk: 40 },
|
|
638
|
+
ArrowLeft: { key: 'ArrowLeft', code: 'ArrowLeft', vk: 37 },
|
|
639
|
+
ArrowRight: { key: 'ArrowRight', code: 'ArrowRight', vk: 39 },
|
|
640
|
+
Space: { key: ' ', code: 'Space', vk: 32, text: ' ' }
|
|
641
|
+
};
|
|
642
|
+
const info = table[key] ?? { key, code: key, vk: 0 };
|
|
643
|
+
// rawKeyDown: 不产生 char 事件 (Enter 单独补 char)
|
|
644
|
+
await this.page('Input.dispatchKeyEvent', {
|
|
645
|
+
type: 'rawKeyDown',
|
|
646
|
+
key: info.key,
|
|
647
|
+
code: info.code,
|
|
648
|
+
windowsVirtualKeyCode: info.vk,
|
|
649
|
+
nativeVirtualKeyCode: info.vk
|
|
650
|
+
});
|
|
651
|
+
// Enter/Space 需要额外 char 事件才能真正生效
|
|
652
|
+
if (info.text) {
|
|
653
|
+
await this.page('Input.dispatchKeyEvent', {
|
|
654
|
+
type: 'char',
|
|
655
|
+
key: info.key,
|
|
656
|
+
code: info.code,
|
|
657
|
+
text: info.text,
|
|
658
|
+
unmodifiedText: info.text,
|
|
659
|
+
windowsVirtualKeyCode: info.vk,
|
|
660
|
+
nativeVirtualKeyCode: info.vk
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
await this.page('Input.dispatchKeyEvent', {
|
|
664
|
+
type: 'keyUp',
|
|
665
|
+
key: info.key,
|
|
666
|
+
code: info.code,
|
|
667
|
+
windowsVirtualKeyCode: info.vk,
|
|
668
|
+
nativeVirtualKeyCode: info.vk
|
|
669
|
+
});
|
|
670
|
+
return { success: true, output: `已按键: ${key}` };
|
|
671
|
+
}
|
|
672
|
+
/** js: Runtime.evaluate 表达式, 返回值 JSON 化; exceptionDetails → error */
|
|
673
|
+
async actionJs(args) {
|
|
674
|
+
const code = String(args.code ?? '').trim();
|
|
675
|
+
if (!code)
|
|
676
|
+
return { success: false, error: 'js 需要 code' };
|
|
677
|
+
const r = await this.page('Runtime.evaluate', { expression: code, returnByValue: true, awaitPromise: true });
|
|
678
|
+
if (r?.exceptionDetails) {
|
|
679
|
+
const desc = r.exceptionDetails.exception?.description || r.exceptionDetails.text || 'JS 执行异常';
|
|
680
|
+
return { success: false, error: String(desc) };
|
|
681
|
+
}
|
|
682
|
+
const v = r?.result?.value;
|
|
683
|
+
let out;
|
|
684
|
+
if (typeof v === 'string')
|
|
685
|
+
out = v;
|
|
686
|
+
else if (v === undefined)
|
|
687
|
+
out = r?.result?.description != null ? String(r.result.description) : 'undefined';
|
|
688
|
+
else
|
|
689
|
+
out = JSON.stringify(v);
|
|
690
|
+
return { success: true, output: out };
|
|
691
|
+
}
|
|
692
|
+
/** back: history.back() + 等一次 load */
|
|
693
|
+
async actionBack(args) {
|
|
694
|
+
const loadWait = this.cdp.waitForEvent('Page.loadEventFired', this.sessionId, this.reqTimeout);
|
|
695
|
+
await this.evaluate('history.back()');
|
|
696
|
+
const loaded = await loadWait;
|
|
697
|
+
const url = await this.evaluate('location.href').catch(() => '');
|
|
698
|
+
return {
|
|
699
|
+
success: true,
|
|
700
|
+
output: loaded === null ? `已后退 (load 超时): ${url ?? ''}` : `已后退: ${url ?? ''}`
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
exports.BrowserCdpSession = BrowserCdpSession;
|
|
705
|
+
// ---------------------------------------------------------------------------
|
|
706
|
+
// 模块级共享会话 + 工具注册
|
|
707
|
+
// ---------------------------------------------------------------------------
|
|
708
|
+
const activeSessions = new Set();
|
|
709
|
+
let sharedSession = null;
|
|
710
|
+
function getSharedSession() {
|
|
711
|
+
if (!sharedSession) {
|
|
712
|
+
sharedSession = new BrowserCdpSession();
|
|
713
|
+
activeSessions.add(sharedSession);
|
|
714
|
+
}
|
|
715
|
+
return sharedSession;
|
|
716
|
+
}
|
|
717
|
+
/** 测试/退出时清理所有会话 (kill Chrome + 关闭 ws) */
|
|
718
|
+
async function __shutdownBrowserForTest() {
|
|
719
|
+
for (const s of Array.from(activeSessions)) {
|
|
720
|
+
try {
|
|
721
|
+
await s.shutdown();
|
|
722
|
+
}
|
|
723
|
+
catch { /* ignore */ }
|
|
724
|
+
}
|
|
725
|
+
activeSessions.clear();
|
|
726
|
+
sharedSession = null;
|
|
727
|
+
}
|
|
728
|
+
/** 注册 'browser' 工具到 ctx.tools */
|
|
729
|
+
function registerBrowserTools(ctx) {
|
|
730
|
+
ctx.tools.set('browser', {
|
|
731
|
+
name: 'browser',
|
|
732
|
+
description: '驱动真实浏览器 (Chrome CDP)。action 可选: open(打开网址) / text(取可见文本) / html(取 DOM) / ' +
|
|
733
|
+
'links(取链接列表) / screenshot(截图存文件) / click(点击选择器或坐标) / type(输入文本) / key(按键) / ' +
|
|
734
|
+
'js(执行 JS) / back / close。首次调用自动启动 headless Chrome, 后续复用同一会话。',
|
|
735
|
+
parameters: {
|
|
736
|
+
action: 'open|text|html|links|screenshot|click|type|key|js|back|close (必填)',
|
|
737
|
+
url: 'open 的网址 (http/https/file/data 均可)',
|
|
738
|
+
selector: 'CSS 选择器 (click/type/text 用)',
|
|
739
|
+
x: 'click 坐标 x (无 selector 时用)',
|
|
740
|
+
y: 'click 坐标 y',
|
|
741
|
+
text: 'type 要输入的文本',
|
|
742
|
+
key: 'key 要按的键 (Enter/Tab/Escape/ArrowDown...)',
|
|
743
|
+
code: 'js 要执行的表达式',
|
|
744
|
+
fullPage: 'screenshot 是否整页, "true"|"false"',
|
|
745
|
+
timeoutMs: '超时毫秒, 默认 20000'
|
|
746
|
+
},
|
|
747
|
+
execute: async (args) => {
|
|
748
|
+
try {
|
|
749
|
+
const session = getSharedSession();
|
|
750
|
+
const result = await session.execute(args);
|
|
751
|
+
// close 之后销毁共享会话, 下次调用重新懒启动
|
|
752
|
+
if (String(args?.action ?? '').trim() === 'close') {
|
|
753
|
+
activeSessions.delete(session);
|
|
754
|
+
sharedSession = null;
|
|
755
|
+
}
|
|
756
|
+
return result;
|
|
757
|
+
}
|
|
758
|
+
catch (e) {
|
|
759
|
+
// 兜底: execute 内部已 try/catch, 这里防止极端情况抛出
|
|
760
|
+
return { success: false, error: e instanceof Error ? e.message : String(e) };
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
//# sourceMappingURL=browser-cdp.js.map
|