pi-web-ui 0.117.0 → 0.118.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/CHANGELOG.md +48 -2
- package/README.md +69 -138
- package/README.zh-CN.md +70 -179
- package/README.zh-TW.md +3 -1
- package/bin/device-hub-cli.mjs +147 -43
- package/bin/frp-manager.mjs +4 -80
- package/bin/hub-upgrade.mjs +561 -0
- package/bin/pi-device-hub.mjs +0 -1
- package/bin/pi-web-ui.mjs +56 -444
- package/deploy/nginx-subpath.conf +0 -33
- package/deploy/pi-web-ui.service +0 -20
- package/dist/server/agent-formatters.js +4 -38
- package/dist/server/agent-service-daemon.js +160 -374
- package/dist/server/agent-service.js +875 -2978
- package/dist/server/approval-audit.js +2 -23
- package/dist/server/approval-default-rules.js +0 -6
- package/dist/server/approval-rules.js +7 -105
- package/dist/server/ask-user-question-tool.js +1 -12
- package/dist/server/atomic-file.js +33 -5
- package/dist/server/attachment-store.js +0 -36
- package/dist/server/attachments.js +3 -112
- package/dist/server/auth-cookie.js +0 -21
- package/dist/server/bash-tool.js +1 -32
- package/dist/server/bg-servers.js +0 -96
- package/dist/server/browser-page-tool.js +11 -62
- package/dist/server/builtin-modules.js +7 -0
- package/dist/server/canvas/canvas-agent-tool.js +58 -47
- package/dist/server/canvas/canvas-cards.js +0 -14
- package/dist/server/canvas/canvas-dispatch.js +7 -4
- package/dist/server/canvas/canvas-orchestrator.js +167 -181
- package/dist/server/canvas/canvas-runtime-messages.js +30 -0
- package/dist/server/canvas/canvas-scheduled-task.js +0 -3
- package/dist/server/canvas/canvas-scheduler.js +5 -7
- package/dist/server/canvas/canvas-store.js +24 -35
- package/dist/server/canvas/canvas-validation.js +17 -14
- package/dist/server/canvas/canvas-workspace.js +26 -30
- package/dist/server/canvas/pstack-skills-data.js +0 -5
- package/dist/server/claim-files-tool.js +1 -15
- package/dist/server/claim-store.js +5 -49
- package/dist/server/client-id-utils.js +0 -16
- package/dist/server/client-state.js +6 -213
- package/dist/server/communicate-tool.js +0 -3
- package/dist/server/compact-context-tool.js +0 -30
- package/dist/server/compacted-history.js +0 -35
- package/dist/server/compaction-instructions.js +0 -13
- package/dist/server/compaction-markers.js +3 -76
- package/dist/server/compaction-state.js +0 -21
- package/dist/server/composer-drafts.js +0 -34
- package/dist/server/context-budget-runner.js +0 -11
- package/dist/server/context-budget.js +1 -70
- package/dist/server/context-evidence.js +0 -14
- package/dist/server/context-usage.js +0 -12
- package/dist/server/control-socket.js +4 -48
- package/dist/server/conversation-communication.js +0 -10
- package/dist/server/conversation-identity.js +0 -1
- package/dist/server/conversation-read-tool.js +0 -58
- package/dist/server/conversation-tool-names.js +0 -1
- package/dist/server/conversation-tool.js +0 -1
- package/dist/server/conversation-touches.js +4 -75
- package/dist/server/dangling-tools.js +0 -82
- package/dist/server/delegate-mode.js +0 -36
- package/dist/server/delegate-task.js +2 -28
- package/dist/server/device-center/approval-summary.js +0 -1
- package/dist/server/device-center/bot-catalog.js +0 -3
- package/dist/server/device-center/bot-execution.js +0 -8
- package/dist/server/device-center/bot-presets.js +0 -1
- package/dist/server/device-center/bots.js +0 -1
- package/dist/server/device-center/collab-service.js +1 -3
- package/dist/server/device-center/collab-tool.js +0 -10
- package/dist/server/device-center/connector.js +21 -11
- package/dist/server/device-center/delivery-authority.js +0 -3
- package/dist/server/device-center/delivery-subject.js +0 -1
- package/dist/server/device-center/desktop-linux.js +3 -15
- package/dist/server/device-center/desktop-native.js +1 -10
- package/dist/server/device-center/desktop-page.js +2 -3
- package/dist/server/device-center/desktop-setup.js +1 -3
- package/dist/server/device-center/desktop.js +1 -7
- package/dist/server/device-center/device-integration.js +1 -27
- package/dist/server/device-center/device-lan-access.js +403 -0
- package/dist/server/device-center/device-lan.js +484 -0
- package/dist/server/device-center/device-portals.js +0 -3
- package/dist/server/device-center/execution-failure.js +0 -2
- package/dist/server/device-center/execution-guides.js +0 -1
- package/dist/server/device-center/execution-instructions.js +0 -1
- package/dist/server/device-center/execution-settings.js +0 -1
- package/dist/server/device-center/execution-system-prompt.js +0 -3
- package/dist/server/device-center/execution-workspace.js +1 -4
- package/dist/server/device-center/executor.js +0 -27
- package/dist/server/device-center/file-sync-paths.js +0 -5
- package/dist/server/device-center/file-sync.js +0 -10
- package/dist/server/device-center/group-context-delta.js +0 -8
- package/dist/server/device-center/group-context-message.js +0 -14
- package/dist/server/device-center/group-context.js +0 -19
- package/dist/server/device-center/group-events.js +0 -2
- package/dist/server/device-center/group-plan-guard.js +1 -35
- package/dist/server/device-center/group-router-runtime.js +182 -0
- package/dist/server/device-center/group-router-worker.js +178 -0
- package/dist/server/device-center/group-router.js +359 -0
- package/dist/server/device-center/hub-auth.js +24 -2
- package/dist/server/device-center/hub-conversations.js +0 -12
- package/dist/server/device-center/hub-deliveries.js +0 -1
- package/dist/server/device-center/hub-integration.js +0 -8
- package/dist/server/device-center/hub-locks.js +1 -5
- package/dist/server/device-center/hub-mobile.js +147 -0
- package/dist/server/device-center/hub-orchestration.js +400 -0
- package/dist/server/device-center/hub-page.js +25 -7
- package/dist/server/device-center/hub-setup.js +0 -6
- package/dist/server/device-center/hub-transaction-log.js +0 -22
- package/dist/server/device-center/hub-upgrade.js +162 -0
- package/dist/server/device-center/hub-webhooks.js +1 -3
- package/dist/server/device-center/hub.js +210 -86
- package/dist/server/device-center/human-interaction.js +1 -4
- package/dist/server/device-center/integration-errors.js +0 -2
- package/dist/server/device-center/integration-verification.js +0 -2
- package/dist/server/device-center/local-hub.js +0 -2
- package/dist/server/device-center/local-publication.js +1 -8
- package/dist/server/device-center/local-verification-job.js +0 -2
- package/dist/server/device-center/local-verification.js +0 -1
- package/dist/server/device-center/local.js +267 -39
- package/dist/server/device-center/main.js +14 -1
- package/dist/server/device-center/mobile-notification-forwarder.js +292 -0
- package/dist/server/device-center/native-desktop/macos.js +0 -1
- package/dist/server/device-center/native-desktop/windows.js +0 -1
- package/dist/server/device-center/partition-log.js +0 -31
- package/dist/server/device-center/path-portal.js +42 -12
- package/dist/server/device-center/publication-journal.js +0 -7
- package/dist/server/device-center/publish-intent.js +0 -5
- package/dist/server/device-center/relay.js +85 -30
- package/dist/server/device-center/report-upload.js +0 -1
- package/dist/server/device-center/runtime.js +1 -18
- package/dist/server/device-center/store.js +13 -33
- package/dist/server/device-center/task-context-budget.js +0 -3
- package/dist/server/device-center/task-error.js +0 -1
- package/dist/server/device-center/task-journal.js +6 -23
- package/dist/server/device-center/task-outcome.js +0 -13
- package/dist/server/device-center/task-plan.js +0 -1
- package/dist/server/device-center/task-progress.js +0 -16
- package/dist/server/device-center/task-recovery.js +5 -88
- package/dist/server/device-center/task-report-outbox.js +0 -4
- package/dist/server/device-center/task-report.js +2 -10
- package/dist/server/device-center/task-scheduler.js +1 -35
- package/dist/server/device-center/task-settlement.js +0 -18
- package/dist/server/device-center/task-state.js +0 -12
- package/dist/server/device-center/task-workspace.js +0 -50
- package/dist/server/device-center/transfer-stream.js +5 -45
- package/dist/server/device-center/transfers.js +2 -10
- package/dist/server/device-center/verification-limits.js +0 -1
- package/dist/server/device-center/work-contract.js +1 -73
- package/dist/server/device-center/work-items.js +317 -0
- package/dist/server/device-center/workspaces.js +0 -1
- package/dist/server/dispatch-domain-handlers.js +190 -32
- package/dist/server/dispatch-plugin-handlers.js +2 -19
- package/dist/server/dsh/dsh-agent-service.js +69 -599
- package/dist/server/dsh/dsh-client.js +7 -111
- package/dist/server/dsh/dsh-market-service.js +4 -25
- package/dist/server/dsh/dsh-serialize.js +0 -49
- package/dist/server/dsh/dsh-sessions.js +0 -37
- package/dist/server/dsh/dsh-usage.js +0 -37
- package/dist/server/dsh/preset-clones.js +1 -65
- package/dist/server/dsh/runtime/custom-prompt.mjs +0 -14
- package/dist/server/dsh/runtime/goal-rpc.mjs +7 -176
- package/dist/server/dsh/runtime/launcher.mjs +2 -53
- package/dist/server/dsh/runtime/override.patch.yml +0 -29
- package/dist/server/dsh/runtime/runtime-root.mjs +0 -54
- package/dist/server/durable/durable-agent-service.js +5 -23
- package/dist/server/durable/durable-driver.js +12 -73
- package/dist/server/durable/durable-event-projector.js +1 -50
- package/dist/server/durable/durable-loader.js +0 -12
- package/dist/server/durable/durable-models.js +11 -80
- package/dist/server/durable/durable-storage.js +1 -17
- package/dist/server/durable/durable-tools.js +2 -20
- package/dist/server/durable/index.js +0 -3
- package/dist/server/edit-soft-tool.js +2 -81
- package/dist/server/engine-capabilities.js +0 -13
- package/dist/server/engine-catalog.js +74 -0
- package/dist/server/engine-cli.js +89 -0
- package/dist/server/engine-probe.js +35 -217
- package/dist/server/ensure-bash.js +2 -28
- package/dist/server/eval-tool.js +1 -50
- package/dist/server/evidence-tool-hooks.js +0 -2
- package/dist/server/file-archives.js +0 -10
- package/dist/server/file-save.js +1 -12
- package/dist/server/file-transfer-routes.js +0 -11
- package/dist/server/files-service.js +18 -271
- package/dist/server/global-loader.js +9 -89
- package/dist/server/goal-evidence.js +1 -35
- package/dist/server/goal-review-gate.js +0 -58
- package/dist/server/goal-service.js +19 -422
- package/dist/server/hashline-engine.js +8 -112
- package/dist/server/host-guard.js +0 -24
- package/dist/server/host-metrics.js +0 -6
- package/dist/server/http-proxy.js +1 -7
- package/dist/server/i18n.js +5 -72
- package/dist/server/icon-svg.js +0 -15
- package/dist/server/index.js +177 -680
- package/dist/server/json-utils.js +0 -12
- package/dist/server/launch-origin.js +0 -44
- package/dist/server/left-panel-running.js +0 -9
- package/dist/server/load-timing.js +1 -4
- package/dist/server/load-tools-tool.js +0 -20
- package/dist/server/locales.js +1 -46
- package/dist/server/lsp-tool.js +13 -126
- package/dist/server/managed.js +0 -48
- package/dist/server/marker-service.js +1 -22
- package/dist/server/markers/builtins/action.js +0 -19
- package/dist/server/markers/builtins/notify.js +1 -9
- package/dist/server/markers/builtins/rename.js +0 -13
- package/dist/server/markers/index.js +0 -3
- package/dist/server/markers/marker.js +0 -13
- package/dist/server/markers/registry.js +0 -3
- package/dist/server/markers/store.js +1 -12
- package/dist/server/market-service.js +7 -43
- package/dist/server/mcp-agent-tool.js +0 -10
- package/dist/server/mcp-bridge.js +6 -138
- package/dist/server/mcp-hot-reload.js +0 -25
- package/dist/server/mcp-manager.js +2 -44
- package/dist/server/mcp-oauth-login.js +0 -2
- package/dist/server/mcp-oauth-ui.js +1 -4
- package/dist/server/mcp-oauth.js +5 -79
- package/dist/server/mcp-project-runtime.js +0 -4
- package/dist/server/message-deletion.js +0 -2
- package/dist/server/message-entry-resolver.js +0 -8
- package/dist/server/message-window.js +0 -71
- package/dist/server/mobile-notification-events.js +16 -0
- package/dist/server/mobile-origin.js +79 -0
- package/dist/server/mobile-push.js +523 -0
- package/dist/server/model-admin.js +19 -358
- package/dist/server/model-enrich.js +0 -19
- package/dist/server/model-resolver.js +2 -61
- package/dist/server/native-cli-discovery.js +115 -0
- package/dist/server/native-cli-model.js +71 -0
- package/dist/server/native-cli-references.js +214 -0
- package/dist/server/native-cli-remote-cleanup.js +58 -0
- package/dist/server/native-cli-remote-files.js +282 -0
- package/dist/server/native-cli-routes.js +68 -0
- package/dist/server/native-cli-runtime.js +1 -0
- package/dist/server/native-cli-session-hooks.js +170 -0
- package/dist/server/native-cli-session.js +585 -0
- package/dist/server/native-cli-state.js +82 -0
- package/dist/server/native-cli.js +210 -0
- package/dist/server/net-guard.js +9 -71
- package/dist/server/office-parse.js +0 -50
- package/dist/server/orphan-manager.js +0 -18
- package/dist/server/patch-bash-process.js +0 -4
- package/dist/server/patch-executable-lookup.js +0 -77
- package/dist/server/patch-node-pty.js +3 -43
- package/dist/server/patch-remote-catalog.js +1 -32
- package/dist/server/patch-tool.js +0 -15
- package/dist/server/payload-limits.js +0 -5
- package/dist/server/pending-question.js +0 -5
- package/dist/server/permission-preset.js +4 -33
- package/dist/server/persona-chain.js +0 -26
- package/dist/server/pkg-info.js +1 -17
- package/dist/server/plan-manager.js +2 -49
- package/dist/server/plan-mode.js +0 -97
- package/dist/server/plan-update-tool.js +1 -15
- package/dist/server/plugin-api-catalog.js +6 -4
- package/dist/server/plugin-assets.js +1 -35
- package/dist/server/plugin-bundled.js +68 -0
- package/dist/server/plugin-catalog-sync.js +0 -37
- package/dist/server/plugin-catalog.js +13 -67
- package/dist/server/plugin-chat-attachments.js +0 -1
- package/dist/server/plugin-conversation-view.js +0 -20
- package/dist/server/plugin-dom.js +1 -23
- package/dist/server/plugin-facilities.js +4 -115
- package/dist/server/plugin-grants.js +2 -90
- package/dist/server/plugin-install-spec.js +0 -43
- package/dist/server/plugin-installer.js +34 -75
- package/dist/server/plugin-llm.js +4 -38
- package/dist/server/plugin-manifest-validate.js +0 -31
- package/dist/server/plugin-permissions.js +1 -24
- package/dist/server/plugin-project.js +7 -129
- package/dist/server/plugin-removal.js +1 -8
- package/dist/server/plugin-schedule.js +1 -38
- package/dist/server/plugin-secrets.js +3 -16
- package/dist/server/plugin-tool-guard.js +1 -28
- package/dist/server/plugin-ui-slots.js +0 -4
- package/dist/server/plugin-updater.js +15 -90
- package/dist/server/plugins.js +71 -619
- package/dist/server/present-files-tool.js +0 -57
- package/dist/server/preset-fields.js +9 -62
- package/dist/server/preset-plugin-dependencies.js +0 -2
- package/dist/server/preset-share.js +3 -172
- package/dist/server/process-utils.js +4 -80
- package/dist/server/prompt-assembler.js +2 -30
- package/dist/server/prompt-base-store.js +0 -47
- package/dist/server/prompt-composer.js +21 -98
- package/dist/server/prompt-templates.js +0 -2
- package/dist/server/protocol-version.js +1 -11
- package/dist/server/protocol.js +0 -5
- package/dist/server/queue-modes.js +0 -3
- package/dist/server/queue-utils.js +0 -40
- package/dist/server/quick-ask-context.js +384 -0
- package/dist/server/quick-ask-settings.js +34 -1
- package/dist/server/quick-ask.js +123 -27
- package/dist/server/read-tool.js +0 -65
- package/dist/server/reasoning-registry.js +1 -29
- package/dist/server/remote-cli-persistence.js +373 -0
- package/dist/server/remote-native-cli.js +78 -0
- package/dist/server/remote-ssh-service.js +481 -190
- package/dist/server/request-context.js +0 -9
- package/dist/server/resolve-global-sdk.js +0 -26
- package/dist/server/sandbox-data.js +0 -1
- package/dist/server/sandbox-engines.js +0 -1
- package/dist/server/sandbox-runtime-gate.js +0 -3
- package/dist/server/sandbox-service.js +0 -41
- package/dist/server/sandbox-tool.js +0 -20
- package/dist/server/schedule-agent-tool.js +2 -31
- package/dist/server/scheduler-tasks.js +3 -54
- package/dist/server/scm-commitmsg.js +0 -53
- package/dist/server/scm.js +3 -48
- package/dist/server/sdk-builtin-extensions.js +2 -29
- package/dist/server/sdk-origin.js +0 -45
- package/dist/server/serialize.js +2 -92
- package/dist/server/server-config.js +0 -11
- package/dist/server/session-branch.js +1 -87
- package/dist/server/session-list-scan.js +17 -25
- package/dist/server/session-open.js +2 -26
- package/dist/server/session-parser.js +1 -55
- package/dist/server/session-scan.js +1 -17
- package/dist/server/session-search.js +0 -56
- package/dist/server/settings-service.js +11 -126
- package/dist/server/setup-config.js +0 -2
- package/dist/server/setup-page.js +0 -9
- package/dist/server/skill-content-filler.js +0 -17
- package/dist/server/skill-discovery.js +5 -92
- package/dist/server/skill-maintenance.js +1 -28
- package/dist/server/skill-tool.js +0 -30
- package/dist/server/slash-commands.js +5 -50
- package/dist/server/soft-cap.js +0 -42
- package/dist/server/startup-banner.js +0 -8
- package/dist/server/subagent-handoff.js +0 -11
- package/dist/server/subagent-helpers.js +2 -17
- package/dist/server/subagent-notify.js +0 -19
- package/dist/server/subagent-security-gate.js +0 -50
- package/dist/server/subagent-task.js +0 -9
- package/dist/server/subagent-templates-hant.js +0 -10
- package/dist/server/subagent-templates.js +3 -103
- package/dist/server/subagents.js +1 -87
- package/dist/server/system-screenshot-windows.js +259 -0
- package/dist/server/system-screenshot.js +77 -0
- package/dist/server/tabs.js +3 -48
- package/dist/server/terminals.js +559 -586
- package/dist/server/text-quote.js +0 -12
- package/dist/server/text-sniff.js +2 -52
- package/dist/server/themes.js +2 -41
- package/dist/server/tool-approval.js +0 -47
- package/dist/server/tool-guards.js +0 -90
- package/dist/server/tool-host-factories.js +1 -16
- package/dist/server/tool-info.js +0 -13
- package/dist/server/tool-manager.js +1 -229
- package/dist/server/tool-overrides.js +1 -46
- package/dist/server/tool-prompt-overrides.js +1 -30
- package/dist/server/tool-prompts.js +0 -14
- package/dist/server/tool-signature.js +0 -25
- package/dist/server/transcript-prepare.js +1 -19
- package/dist/server/tts-routes.js +58 -0
- package/dist/server/tts-service.js +106 -0
- package/dist/server/tts-system.js +160 -0
- package/dist/server/turn-timing.js +0 -1
- package/dist/server/unauthorized-page.js +0 -8
- package/dist/server/universal/driver-base.js +2 -31
- package/dist/server/universal/drivers/claude-code.js +12 -172
- package/dist/server/universal/drivers/codex.js +12 -35
- package/dist/server/universal/drivers/mock-driver.js +1 -11
- package/dist/server/universal/index.js +0 -3
- package/dist/server/universal/session-store.js +4 -21
- package/dist/server/universal/universal-attachments.js +2 -36
- package/dist/server/universal/universal-service.js +5 -24
- package/dist/server/universal/universal-session.js +26 -148
- package/dist/server/universal/universal-types.js +0 -3
- package/dist/server/update-check.js +6 -182
- package/dist/server/uploads.js +4 -43
- package/dist/server/virtual-tui.js +2 -27
- package/dist/server/vision-bridge.js +0 -27
- package/dist/server/wait-subscription-scan.js +13 -115
- package/dist/server/webui-context.js +2 -87
- package/dist/server/workspace-handle.js +1 -56
- package/dist/server/workspace-snapshot.js +7 -113
- package/dist/server/worktree-service.js +2 -53
- package/dist/server/worktree-tool.js +0 -11
- package/dist/server/ws-client-id.js +0 -18
- package/dist/server/ws-pending-queue.js +0 -7
- package/dist/server/ws-unknown-types.js +0 -17
- package/docs/README.md +24 -17
- package/docs/architecture-plugins.md +37 -1
- package/docs/device-center.md +28 -2
- package/docs/device-desktop.md +3 -3
- package/docs/group-orchestration.md +142 -0
- package/docs/hub-upgrade-release-testing.md +46 -0
- package/extensions/webui.ts +3 -74
- package/package.json +7 -3
- package/plugin-sdk/README.md +6 -0
- package/plugin-sdk/client-utils.mjs +0 -7
- package/plugin-sdk/client.d.ts +0 -1
- package/plugin-sdk/client.mjs +1 -31
- package/plugin-sdk/import-check.mjs +4 -51
- package/plugin-sdk/index.d.ts +120 -119
- package/plugin-sdk/index.mjs +0 -6
- package/plugin-sdk/ssh-host-trust.mjs +0 -12
- package/plugin-sdk/testing.d.ts +0 -1
- package/plugin-sdk/testing.mjs +31 -86
- package/plugins/catalog.json +52 -4
- package/plugins/superagent/README.md +110 -0
- package/plugins/superagent/client/entry.mjs +21 -0
- package/plugins/superagent/index.mjs +8 -0
- package/plugins/superagent/main.mjs +218 -0
- package/plugins/superagent/manifest.json +15 -0
- package/plugins/superagent/server.mjs +572 -0
- package/plugins/superagent/style.css +167 -0
- package/themes/aetheris.css +1 -31
- package/themes/ayu-light.css +0 -1
- package/themes/bases/flow-base.css +2 -83
- package/themes/bases/obsidian-base.css +2 -37
- package/themes/bases/statusbar-flat-base.css +0 -1
- package/themes/bases/zhujian-base.css +0 -63
- package/themes/bases/zhupi-base.css +0 -64
- package/themes/catppuccin-latte.css +0 -1
- package/themes/catppuccin.css +0 -1
- package/themes/claude-code-dark.css +0 -1
- package/themes/codex.css +0 -1
- package/themes/dark-teal.css +22 -54
- package/themes/everforest-light.css +0 -1
- package/themes/flow-cristallo.css +1 -9
- package/themes/flow-ink.css +1 -10
- package/themes/flow-light.css +1 -10
- package/themes/flow-obsidian.css +1 -10
- package/themes/flow-zurich.css +1 -10
- package/themes/flow.css +1 -9
- package/themes/geist.css +0 -1
- package/themes/gruvbox-light.css +0 -1
- package/themes/kanagawa-lotus.css +0 -1
- package/themes/md-preview.css +1 -2
- package/themes/mist.css +0 -1
- package/themes/nord.css +0 -1
- package/themes/one-dark.css +0 -1
- package/themes/paper.css +0 -1
- package/themes/rose-pine-dawn.css +0 -1
- package/themes/sakura.css +0 -1
- package/themes/solarized-light.css +0 -1
- package/themes/tokyo-night.css +0 -1
- package/themes/translucent.css +4 -35
- package/themes/transparent.css +4 -41
- package/themes/vs-code-dark.css +0 -1
- package/themes/wallpaper-flow.css +3 -17
- package/themes/wallpaper-obsidian.css +1 -4
- package/themes/white.css +0 -1
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/{BgTasksModal-BjNRIUIc.js → BgTasksModal-D-x7kA63.js} +1 -1
- package/web/dist/assets/CanvasStudio-CAWcs9tm.js +1579 -0
- package/web/dist/assets/DeviceCenter-Dc0whRcu.js +3 -0
- package/web/dist/assets/DeviceExecutionSettings-BoX7-0h7.js +1 -0
- package/web/dist/assets/DeviceGroups-C_fZMNeK.js +1 -0
- package/web/dist/assets/{Dropdown-CQtstPGv.js → Dropdown-CTgVLUbp.js} +1 -1
- package/web/dist/assets/FilePreview-C6W0PvaK.js +2 -0
- package/web/dist/assets/GlobalSearchModal-HR9LSspC.js +1 -0
- package/web/dist/assets/GroupConversation-C81YjF2C.js +61 -0
- package/web/dist/assets/GroupWorkItems-BIvZtr10.js +1 -0
- package/web/dist/assets/{HintTip-ChTW8o1m.js → HintTip-Da6qc566.js} +1 -1
- package/web/dist/assets/ModelConfigModal-DhSyuIuV.js +2 -0
- package/web/dist/assets/NativeCliPanel-CU1rZmyu.js +5 -0
- package/web/dist/assets/{PiSetupModal-BX5rUuoI.js → PiSetupModal-DZKY5Wiw.js} +1 -1
- package/web/dist/assets/ProviderOAuthControls-CIL85nA7.js +1 -0
- package/web/dist/assets/{SCMPanel-DPAyY2sW.js → SCMPanel-CuEgzAlO.js} +2 -2
- package/web/dist/assets/SettingsModal-Cdu5h29p.js +33 -0
- package/web/dist/assets/TermXterm-Bt5WKAxP.js +3 -0
- package/web/dist/assets/TerminalPanel-BCIBV8se.js +1 -0
- package/web/dist/assets/TuiOverlayModal-3NDd9T97.js +1 -0
- package/web/dist/assets/app-globals-CZjFtI4F.js +129 -0
- package/web/dist/assets/canvas-client-WkhM55mR.js +1 -0
- package/web/dist/assets/composer-bridge-CImnjDHq.js +1 -0
- package/web/dist/assets/desktop-B8NZ3KW-.js +1 -0
- package/web/dist/assets/index-CQc3VrWa.css +1 -0
- package/web/dist/assets/index-D_AGu4Zv.js +404 -0
- package/web/dist/assets/plugin-host-uqZGpvCs.js +1 -0
- package/web/dist/assets/{remote-ssh-client-Dzi-eEyE.js → remote-ssh-client-Cdf-cTUe.js} +1 -1
- package/web/dist/assets/theme-DGL7hLUs.js +1 -0
- package/web/dist/index.html +11 -10
- package/web/dist/sw.js +15 -61
- package/web/public/sw.js +14 -60
- package/web/dist/assets/BgTasksModal-C0zuU8FE.js +0 -1
- package/web/dist/assets/CanvasStudio-BWvdmJ0M.js +0 -1579
- package/web/dist/assets/CanvasStudio-VQFjmTGq.js +0 -1579
- package/web/dist/assets/DeviceCenter-DI9_0aBB.js +0 -3
- package/web/dist/assets/DeviceCenter-DsNtXqzY.js +0 -3
- package/web/dist/assets/DeviceExecutionSettings-BLP1Su5G.js +0 -1
- package/web/dist/assets/DeviceGroups-2Pvk0Cds.js +0 -1
- package/web/dist/assets/DeviceGroups-CP7CcyPD.js +0 -1
- package/web/dist/assets/FilePreview-BMB2tNyT.js +0 -2
- package/web/dist/assets/FilePreview-RGFQUbJo.js +0 -2
- package/web/dist/assets/GlobalSearchModal-BM5wxiqU.js +0 -1
- package/web/dist/assets/GlobalSearchModal-DGv_WINq.js +0 -1
- package/web/dist/assets/GroupConversation-B7SnuXj1.js +0 -61
- package/web/dist/assets/GroupConversation-Dda69q-2.js +0 -61
- package/web/dist/assets/ModelConfigModal-C-0Z4hUZ.js +0 -2
- package/web/dist/assets/ModelConfigModal-yqeadyab.js +0 -2
- package/web/dist/assets/ProviderOAuthControls-DY4SCfXg.js +0 -1
- package/web/dist/assets/SCMPanel-C4KbQvvK.js +0 -2
- package/web/dist/assets/SettingsModal-DxZp6D_g.js +0 -32
- package/web/dist/assets/SettingsModal-WhQzaOPt.js +0 -32
- package/web/dist/assets/TerminalPanel-D4By_RVc.js +0 -3
- package/web/dist/assets/TerminalPanel-lZw8-i3j.js +0 -3
- package/web/dist/assets/TuiOverlayModal-C3pAVuC8.js +0 -1
- package/web/dist/assets/TuiOverlayModal-D7_rEXcJ.js +0 -1
- package/web/dist/assets/app-globals-KqOVtmp1.js +0 -123
- package/web/dist/assets/canvas-client-BY6ojmle.js +0 -1
- package/web/dist/assets/desktop-CV6KKp9R.js +0 -1
- package/web/dist/assets/index-BawF7ZP5.js +0 -397
- package/web/dist/assets/index-BoGCoq5X.js +0 -397
- package/web/dist/assets/index-CVW9FoY2.css +0 -1
- package/web/dist/assets/plugin-host-Dn8Dd6SC.js +0 -1
- package/web/dist/assets/theme-D_bihpdN.js +0 -1
package/dist/server/plugins.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { registerMarker, unregisterMarker, isBuiltinMarker } from "./markers/index.js";
|
|
2
2
|
import { ensurePluginAsset, getPluginAssetStatus, } from "./plugin-assets.js";
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
+
import { ttsService } from "./tts-service.js";
|
|
4
5
|
import { mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
5
6
|
import { cpSync, existsSync, lstatSync, readFileSync, readdirSync, watch as fsWatch, writeFileSync } from "node:fs";
|
|
6
7
|
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
@@ -19,12 +20,12 @@ import { readCatalog, addCustomEntry, removeCustomEntry } from "./plugin-catalog
|
|
|
19
20
|
import { PluginGrantsStore, normalizeGrantPath } from "./plugin-grants.js";
|
|
20
21
|
import { normalizeIconSvg } from "./icon-svg.js";
|
|
21
22
|
import { cleanupRemovedPlugins } from "./plugin-removal.js";
|
|
23
|
+
import { prepareBundledPlugins } from "./plugin-bundled.js";
|
|
22
24
|
import { PluginDomConsent, declarationWantsDom } from "./plugin-dom.js";
|
|
23
25
|
import { validatePluginManifest, formatManifestIssue, isKnownPermission } from "./plugin-manifest-validate.js";
|
|
24
26
|
import { buildPluginApiCatalog } from "./plugin-api-catalog.js";
|
|
25
27
|
import { AGENT_TOOL_CATALOG } from "./tool-manager.js";
|
|
26
28
|
import { applyPostEdit, denialText, freezeParams, isBlockingDecision, normalizePostEdit, normalizePreDecision, withGuardTimeout, } from "./plugin-tool-guard.js";
|
|
27
|
-
// 工作区根的归一化与 client-state 共用一份(同一份语义:只收绝对路径 / 去重 / 上限)。
|
|
28
29
|
import { normalizeWorkspaceRoots } from "./client-state.js";
|
|
29
30
|
import { compareVersions, ensureBackup } from "./plugin-updater.js";
|
|
30
31
|
import { createProject, isInsideRoot, realPathOfNearest, } from "./plugin-project.js";
|
|
@@ -32,16 +33,8 @@ import { listAllMcpServers, checkMcpServer, installMcpServer, } from "./mcp-mana
|
|
|
32
33
|
import { createHash } from "node:crypto";
|
|
33
34
|
import { execFile } from "node:child_process";
|
|
34
35
|
import { promisify } from "node:util";
|
|
35
|
-
/** 合法插件 id:字母/数字/下划线/连字符,防路径穿越(同 themes.ts 的做法)。 */
|
|
36
36
|
const ID_RE = /^[A-Za-z0-9_-]+$/;
|
|
37
|
-
/** 合法服务名:字母/数字/下划线/点/连字符,1-64 字符。 */
|
|
38
37
|
const SERVICE_NAME_RE = /^[A-Za-z0-9_.-]+$/;
|
|
39
|
-
/** 交给插件前的防御性拷贝:provider 回的是快照管线的**活引用** —— messages 数组与
|
|
40
|
-
* 元素对象都被 60ms 推送管线缓存复用(lastMessagesArray / uiMessageCache),插件原地
|
|
41
|
-
* 改一个字段/挪一个元素,污染的就是推给真实客户端的快照。浅拷数组 + 元素对象
|
|
42
|
-
* structuredClone;元素是纯 JSON 结构(details 已过 JSON.stringify 闸),structuredClone
|
|
43
|
-
* 不会失败,JSON 往返只是万一携带不可克隆值时的兜底。
|
|
44
|
-
* `getActiveConversation` 与 `emitClientModelChanged` 共用(两处都要发快照给插件)。 */
|
|
45
38
|
function copySnapshotForPlugins(snap) {
|
|
46
39
|
return {
|
|
47
40
|
...snap,
|
|
@@ -54,24 +47,18 @@ function copySnapshotForPlugins(snap) {
|
|
|
54
47
|
return JSON.parse(JSON.stringify(m));
|
|
55
48
|
}
|
|
56
49
|
catch {
|
|
57
|
-
return { ...m };
|
|
50
|
+
return { ...m };
|
|
58
51
|
}
|
|
59
52
|
}
|
|
60
53
|
}),
|
|
61
54
|
};
|
|
62
55
|
}
|
|
63
|
-
/** 每插件保留最近 N 条运行时日志(内存封顶,防刷爆)。 */
|
|
64
56
|
export const PLUGIN_LOG_CAP = 200;
|
|
65
|
-
/** 单条日志文本截断上限(字符)。 */
|
|
66
57
|
export const PLUGIN_LOG_TEXT_MAX = 500;
|
|
67
58
|
const PLUGIN_LOG_LEVELS = new Set(["debug", "info", "warn", "error"]);
|
|
68
|
-
/** host.log 首参归一:是合法级别即当级别,否则缺省 info
|
|
69
|
-
* (老插件 host.log(...args) 的兼容口径)。纯函数,可单测。 */
|
|
70
59
|
export function normalizePluginLogLevel(first) {
|
|
71
60
|
return typeof first === "string" && PLUGIN_LOG_LEVELS.has(first) ? first : "info";
|
|
72
61
|
}
|
|
73
|
-
/** host.log 参数 → 文本(string 原样,其余 JSON/String 化,空格拼接,截断封顶)。
|
|
74
|
-
* 纯函数,可单测。 */
|
|
75
62
|
export function formatPluginLogText(args) {
|
|
76
63
|
const parts = args.map((a) => {
|
|
77
64
|
if (typeof a === "string")
|
|
@@ -92,43 +79,22 @@ export function formatPluginLogText(args) {
|
|
|
92
79
|
const text = parts.join(" ");
|
|
93
80
|
return text.length > PLUGIN_LOG_TEXT_MAX ? text.slice(0, PLUGIN_LOG_TEXT_MAX) : text;
|
|
94
81
|
}
|
|
95
|
-
/** 宿主保留的日志通道键(plugin_message 上行 / plugin_data 下行共用)。
|
|
96
|
-
* 插件自己的 onMessage 收不到它(handleMessage 顶部拦截),插件视图侧同理
|
|
97
|
-
* 由前端 ingestPluginLogsData 拦截——插件协议与宿主通道互不干扰。 */
|
|
98
82
|
export const PLUGIN_LOGS_HOST_KEY = "__host";
|
|
99
|
-
/** 是否为宿主保留的日志拉取请求(op 缺省按 get 容错)。 */
|
|
100
83
|
export function isPluginLogsRequest(p) {
|
|
101
84
|
if (!p || typeof p !== "object")
|
|
102
85
|
return false;
|
|
103
86
|
const o = p;
|
|
104
87
|
return o[PLUGIN_LOGS_HOST_KEY] === "logs" && (o.op === "get" || o.op === "clear" || o.op === undefined);
|
|
105
88
|
}
|
|
106
|
-
/**
|
|
107
|
-
* per-plugin 可逆副作用栈(effect 栈)。
|
|
108
|
-
*
|
|
109
|
-
* 宿主每个注册面(事件订阅 / AI 工具 / 命令 / HTTP 路由 / 反向代理 / fs.watch /
|
|
110
|
-
* 定时任务 / 后台任务 / UI 条目 / 设置回调…)在内部 `add(label, dispose)` 登记一条
|
|
111
|
-
* disposer,反激活时**逆序**回卷 —— 插件不必记得注销,也不会因为漏掉一处就留下
|
|
112
|
-
* 孤儿订阅、定时器、路由或事件处理器(热重载后事件双触发、定时器叠加、watcher
|
|
113
|
-
* 堆积都是这个漏法的症状)。
|
|
114
|
-
*
|
|
115
|
-
* 与「每个注册函数自己返回注销函数」(插件主动调用)是**同一件事的两面**:
|
|
116
|
-
* 返回给插件的注销函数 = `remove(item)`(只撤这一条、幂等),反激活 = `release()`。
|
|
117
|
-
* 插件自建的副作用(自己的 setInterval、EventEmitter 监听…)可经 `host.effect()`
|
|
118
|
-
* 挂进来,同样享受逆序回卷 + 泄漏归因。
|
|
119
|
-
*/
|
|
120
89
|
export class PluginEffectStack {
|
|
121
90
|
pluginId;
|
|
122
91
|
diag;
|
|
123
92
|
items = [];
|
|
124
93
|
released = false;
|
|
125
|
-
constructor(pluginId,
|
|
126
|
-
/** 记录一条诊断(cleanup 抛错时调用;不抛错、不阻断回卷)。 */
|
|
127
|
-
diag) {
|
|
94
|
+
constructor(pluginId, diag) {
|
|
128
95
|
this.pluginId = pluginId;
|
|
129
96
|
this.diag = diag;
|
|
130
97
|
}
|
|
131
|
-
/** 登记一条副作用;返回「只撤这一条」的注销函数(幂等,重复调用无副作用)。 */
|
|
132
98
|
add(label, dispose) {
|
|
133
99
|
const item = { label, dispose };
|
|
134
100
|
if (this.released) {
|
|
@@ -146,15 +112,12 @@ export class PluginEffectStack {
|
|
|
146
112
|
get isReleased() {
|
|
147
113
|
return this.released;
|
|
148
114
|
}
|
|
149
|
-
/** 当前未回卷的副作用数(单测断言「activate → deactivate 后栈空」)。 */
|
|
150
115
|
get size() {
|
|
151
116
|
return this.items.length;
|
|
152
117
|
}
|
|
153
|
-
/** 未回卷的副作用标签(按登记顺序,排障用)。 */
|
|
154
118
|
labels() {
|
|
155
119
|
return this.items.map((i) => i.label);
|
|
156
120
|
}
|
|
157
|
-
/** 撤销单条:先从栈里摘掉,再跑它的 dispose(幂等 —— 摘不到说明已撤过)。 */
|
|
158
121
|
remove(item) {
|
|
159
122
|
const i = this.items.indexOf(item);
|
|
160
123
|
if (i < 0)
|
|
@@ -167,7 +130,6 @@ export class PluginEffectStack {
|
|
|
167
130
|
this.report(item.label, err);
|
|
168
131
|
}
|
|
169
132
|
}
|
|
170
|
-
/** 逆序回卷全部副作用;返回**失败**(dispose 抛错)的标签列表,绝不抛出。 */
|
|
171
133
|
release() {
|
|
172
134
|
if (this.released)
|
|
173
135
|
return [];
|
|
@@ -190,24 +152,13 @@ export class PluginEffectStack {
|
|
|
190
152
|
this.diag?.(`effect "${label}" cleanup failed: ${err?.message ?? String(err)}`);
|
|
191
153
|
}
|
|
192
154
|
}
|
|
193
|
-
/** 宿主提供的插件设施版本——manifest 声明的 apiVersion 高于此值则拒绝激活,
|
|
194
|
-
* 插件能拿到明确的「请升级 pi-web-ui」而不是在新接口上莫名 undefined。
|
|
195
|
-
* 1 = 初始:storage/secrets/命令/HTTP 路由/工具注册/受限 fs/后台任务。
|
|
196
|
-
* 2 = issue #146:UI 扩展点(manifest "ui" + host.ui.*)、跨目录 fs(requestAccess /
|
|
197
|
-
* *Path 族)、项目组装(host.project.create)、多根工作区(set_workspace_roots)。
|
|
198
|
-
* 同时把「未声明 permissions」从旧全权模式改为**默认拒绝**(versionGuard 前移)。 */
|
|
199
155
|
export const PLUGIN_API_VERSION = 2;
|
|
200
|
-
/** 消息处理器超时:仅作为不再等待的日志阈值(响应由 handler 自己发出)。 */
|
|
201
156
|
const MESSAGE_TIMEOUT_MS = 30_000;
|
|
202
|
-
/** host.fs 被能力门控拒绝时的共享 rejected promise(类型对齐用)。 */
|
|
203
157
|
const NO_FS_PROMISE = Promise.reject(new Error('插件未声明能力 "fs"(manifest.permissions)——请求被拒'));
|
|
204
|
-
NO_FS_PROMISE.catch(() => { });
|
|
205
|
-
/** 只读插件(仅声明 fs:read)撞到写操作时的共享 rejected promise:提示缺 fs/fs:write。 */
|
|
158
|
+
NO_FS_PROMISE.catch(() => { });
|
|
206
159
|
const NO_FS_WRITE_PROMISE = Promise.reject(new Error('缺少写能力:需要 "fs"/"fs:write"(manifest.permissions)——请求被拒'));
|
|
207
|
-
NO_FS_WRITE_PROMISE.catch(() => { });
|
|
208
|
-
/** host.bash 的执行体:execFile 直跑(不过 shell),与宿主其它 git/scm 查询同口径。 */
|
|
160
|
+
NO_FS_WRITE_PROMISE.catch(() => { });
|
|
209
161
|
const execFileAsync = promisify(execFile);
|
|
210
|
-
/** 简单版本号三元组(x.y.z,忽略 -prerelease/+build 后缀)。 */
|
|
211
162
|
function parseVer(v) {
|
|
212
163
|
const m = String(v ?? "")
|
|
213
164
|
.trim()
|
|
@@ -223,8 +174,6 @@ function cmpVerTuple(a, b) {
|
|
|
223
174
|
}
|
|
224
175
|
return 0;
|
|
225
176
|
}
|
|
226
|
-
/** manifest engines["pi-web-ui"] 约束判定(支持 >=x.y.z / ^x.y.z / =x.y.z/裸 x.y.z)。
|
|
227
|
-
* 返回 null = 约束或当前版本解析失败(调用方警告放行,不阻断激活)。 */
|
|
228
177
|
export function satisfiesEnginesConstraint(constraint, current) {
|
|
229
178
|
const c = String(constraint ?? "").trim();
|
|
230
179
|
let kind, req;
|
|
@@ -252,30 +201,15 @@ export function satisfiesEnginesConstraint(constraint, current) {
|
|
|
252
201
|
return cmpVerTuple(cur, r) >= 0;
|
|
253
202
|
if (kind === "=")
|
|
254
203
|
return cmpVerTuple(cur, r) === 0;
|
|
255
|
-
return cur[0] === r[0] && cmpVerTuple(cur, r) >= 0;
|
|
204
|
+
return cur[0] === r[0] && cmpVerTuple(cur, r) >= 0;
|
|
256
205
|
}
|
|
257
|
-
/** 宿主自身版本(package.json version,读一次缓存;读不到回 null——调用方放行)。 */
|
|
258
206
|
export function readHostVersion() {
|
|
259
207
|
const ver = getAppVersion();
|
|
260
208
|
return ver ? ver : null;
|
|
261
209
|
}
|
|
262
|
-
// ---------------------------------------------------------------------------
|
|
263
|
-
// 声明式设置 schema(manifest "settings")
|
|
264
|
-
// ---------------------------------------------------------------------------
|
|
265
210
|
const SETTING_TYPES = new Set(["text", "password", "number", "boolean", "select", "secret"]);
|
|
266
|
-
/** select 字段可由宿主现算的候选数据源(manifest `optionsFrom`):
|
|
267
|
-
* models = 已配置鉴权的模型;thinkingLevels = SDK 思考强度档位。
|
|
268
|
-
* 清单在浏览器侧现算(模型配置会变,静态表会过期),服务端不做候选值校验。 */
|
|
269
211
|
const SETTING_OPTIONS_FROM = new Set(["models", "thinkingLevels"]);
|
|
270
|
-
/**
|
|
271
|
-
* 解析 manifest "ui" 的某个 slot 数组 → 规范化条目(issue #146 完整版)。
|
|
272
|
-
*
|
|
273
|
-
* 宽容但不放任:坏字段跳过、id 非法或重复跳过、children 只收一层、文本截断;
|
|
274
|
-
* 不认识的 slot / kind / when 直接丢弃(旧宿主读到新字段也不会崩,新宿主读到旧字段同理)。
|
|
275
|
-
* 归属由宿主决定:全局 id = `<pluginId>:<itemId>`。
|
|
276
|
-
*/
|
|
277
212
|
export { UI_SLOTS };
|
|
278
|
-
/** manifest 里可以写更自然的简写(作者少踩坑):解析时映射到完整 slot 名。 */
|
|
279
213
|
export const UI_SLOT_ALIASES = {
|
|
280
214
|
topbar: "topbar.primary",
|
|
281
215
|
"topbar.more": "topbar.overflow",
|
|
@@ -296,16 +230,7 @@ export const UI_SLOT_ALIASES = {
|
|
|
296
230
|
bottom: "bottombar",
|
|
297
231
|
footer: "bottombar",
|
|
298
232
|
};
|
|
299
|
-
/**
|
|
300
|
-
* 宿主在 manifest.ui 下认识的结构性元数据扩展键(非挂载点 Slot,不作为未知 slot 报警丢弃):
|
|
301
|
-
* - items: 平铺声明项列表
|
|
302
|
-
* - arrange: 槽位整理与排序意图
|
|
303
|
-
* - views: 插件自定义视图元信息声明
|
|
304
|
-
* - commands: 插件动作与命令元信息声明
|
|
305
|
-
* - menus: 插件菜单声明
|
|
306
|
-
*/
|
|
307
233
|
export const UI_STRUCTURAL_KEYS = new Set(["items", "arrange", "views", "commands", "menus"]);
|
|
308
|
-
/** 合法的条目种类(缺省 action;settings.pages 缺省 page)。 */
|
|
309
234
|
export const UI_KINDS = new Set([
|
|
310
235
|
"view",
|
|
311
236
|
"action",
|
|
@@ -319,23 +244,16 @@ export const UI_KINDS = new Set([
|
|
|
319
244
|
"progress",
|
|
320
245
|
"select",
|
|
321
246
|
]);
|
|
322
|
-
/** 合法的对齐取值(UiAlign):起首/居中/行尾,非法值丢弃(条目保留,align 回缺省)。 */
|
|
323
247
|
const UI_ALIGNS = new Set(["start", "center", "end"]);
|
|
324
|
-
/** 原始值 → 合法 UiAlign(非法/缺失一律 undefined,调用方填缺省)。 */
|
|
325
248
|
export function parseUiAlign(raw) {
|
|
326
249
|
return typeof raw === "string" && UI_ALIGNS.has(raw) ? raw : undefined;
|
|
327
250
|
}
|
|
328
251
|
function trimStr(v, max = 60) {
|
|
329
252
|
return typeof v === "string" && v.trim() ? v.trim().slice(0, max) : undefined;
|
|
330
253
|
}
|
|
331
|
-
/** 原字符串 trim 后是否超出上限(截断诊断用)。 */
|
|
332
254
|
function wasTruncated(v, max) {
|
|
333
255
|
return typeof v === "string" && v.trim().length > max;
|
|
334
256
|
}
|
|
335
|
-
/** 规范化一个条目;非法返回 null(slot 由调用方给)。
|
|
336
|
-
*
|
|
337
|
-
* 第三个参数可选:传了就把“为什么丢弃/为什么被改”(未知 kind 回落、字段截断、
|
|
338
|
-
* children 嵌套被清、when/options 丢项等)逐条 push 进去;不传则行为与原来完全一致。 */
|
|
339
257
|
export function parseUiItem(raw, slot, diagnostics) {
|
|
340
258
|
const diag = (m) => {
|
|
341
259
|
diagnostics?.push(m);
|
|
@@ -347,7 +265,6 @@ export function parseUiItem(raw, slot, diagnostics) {
|
|
|
347
265
|
const o = raw;
|
|
348
266
|
const rawIdHint = typeof o.id === "string" && o.id.trim() ? o.id.trim().slice(0, 32) : "?";
|
|
349
267
|
const id = trimStr(o.id, 64);
|
|
350
|
-
// id 必须匹配插件 id 字符集(它与 pluginId 拼成全局 key,直接进 DOM 的 data 属性)
|
|
351
268
|
if (!id || !ID_RE.test(id)) {
|
|
352
269
|
diag(`ui item in slot "${slot}": invalid id "${rawIdHint}", dropped`);
|
|
353
270
|
return null;
|
|
@@ -363,7 +280,6 @@ export function parseUiItem(raw, slot, diagnostics) {
|
|
|
363
280
|
diag(`ui item "${id}": label truncated to 60 chars`);
|
|
364
281
|
const kindRaw = trimStr(o.kind, 16);
|
|
365
282
|
let kind = kindRaw && UI_KINDS.has(kindRaw) ? kindRaw : undefined;
|
|
366
|
-
// 兼容 action 字段写成 { type: "view", viewId: ... } 的历史/异形结构(推断 kind="view")
|
|
367
283
|
let actionStr;
|
|
368
284
|
let viewFromAction;
|
|
369
285
|
if (typeof o.action === "string") {
|
|
@@ -392,7 +308,6 @@ export function parseUiItem(raw, slot, diagnostics) {
|
|
|
392
308
|
for (let i = 0; i < sliced.length; i++) {
|
|
393
309
|
const before = diagnostics?.length ?? 0;
|
|
394
310
|
const child = parseUiItem(sliced[i], slot, diagnostics);
|
|
395
|
-
// 子项不再递归(一层够用):清掉它自己的 children 防嵌套刷栈
|
|
396
311
|
if (child) {
|
|
397
312
|
if (child.children?.length)
|
|
398
313
|
diag(`ui item "${id}": child "${child.id}" nested children cleared (only one level kept)`);
|
|
@@ -428,8 +343,6 @@ export function parseUiItem(raw, slot, diagnostics) {
|
|
|
428
343
|
const align = parseUiAlign(o.align);
|
|
429
344
|
if (o.align !== undefined && !align)
|
|
430
345
|
diag(`ui item "${id}": unknown align, default used`);
|
|
431
|
-
// kind="select" 的候选项(最多 32;value 必填,label 缺省回落 value)。
|
|
432
|
-
// 没写 kind 但给了合法 options = 视为 select(少让作者踩坑)。
|
|
433
346
|
let options;
|
|
434
347
|
if ((kind === "select" || !kindRaw) && Array.isArray(o.options)) {
|
|
435
348
|
if (o.options.length > 32)
|
|
@@ -516,14 +429,6 @@ export function parseUiItem(raw, slot, diagnostics) {
|
|
|
516
429
|
...(options ? { options } : {}),
|
|
517
430
|
};
|
|
518
431
|
}
|
|
519
|
-
/**
|
|
520
|
-
* 解析 manifest "ui" → 规范化贡献。
|
|
521
|
-
*
|
|
522
|
-
* 形状两种都收(都是为了少让插件作者踩坑):
|
|
523
|
-
* "ui": { "topbar": [...] } // 按 slot 分组(推荐)
|
|
524
|
-
* "ui": { "items": [{ slot, ... }, ...] } // 平铺(运行时注册同形,便于两边复用)
|
|
525
|
-
* 单条目上限 32、arrange 上限 64 —— 防一份 manifest 把前端顶爆。
|
|
526
|
-
*/
|
|
527
432
|
export function parseUiContributions(raw, diagnostics) {
|
|
528
433
|
const diag = (m) => {
|
|
529
434
|
diagnostics?.push(m);
|
|
@@ -551,8 +456,6 @@ export function parseUiContributions(raw, diagnostics) {
|
|
|
551
456
|
else
|
|
552
457
|
noteCap();
|
|
553
458
|
};
|
|
554
|
-
// 入参级截断也要记一笔(total cap 消息只在“有效条目溢出”时出现,
|
|
555
|
-
// 这里记的是“写法层面的切片”——两者正交)。
|
|
556
459
|
if (o.items !== undefined && !Array.isArray(o.items))
|
|
557
460
|
diag('ui: "items" is not an array, ignored');
|
|
558
461
|
if (Array.isArray(o.items)) {
|
|
@@ -603,9 +506,6 @@ export function parseUiContributions(raw, diagnostics) {
|
|
|
603
506
|
}
|
|
604
507
|
return { items, arrange };
|
|
605
508
|
}
|
|
606
|
-
/** 规范化整理意图(对内置/其它插件的条目)。非法/越界形状丢弃。
|
|
607
|
-
*
|
|
608
|
-
* 第三个参数可选:传了就把丢弃/忽略原因逐条 push 进去;不传则行为与原来完全一致。 */
|
|
609
509
|
export function parseUiArrange(raw, diagnostics) {
|
|
610
510
|
const diag = (m) => {
|
|
611
511
|
diagnostics?.push(m);
|
|
@@ -627,7 +527,6 @@ export function parseUiArrange(raw, diagnostics) {
|
|
|
627
527
|
continue;
|
|
628
528
|
}
|
|
629
529
|
const o = it;
|
|
630
|
-
// 目标 id:`host:<name>` 或 `<pluginId>:<itemId>`
|
|
631
530
|
const id = trimStr(o.id, 96);
|
|
632
531
|
if (!id || !/^[A-Za-z0-9_-]+:[A-Za-z0-9_.:-]+$/.test(id)) {
|
|
633
532
|
const hint = typeof o.id === "string" && o.id.trim() ? o.id.trim().slice(0, 32) : "?";
|
|
@@ -671,8 +570,6 @@ export function parseUiArrange(raw, diagnostics) {
|
|
|
671
570
|
}
|
|
672
571
|
return out;
|
|
673
572
|
}
|
|
674
|
-
/** 解析 manifest.requires → 归一化硬依赖声明(P2-8,坏形状宽容忽略:
|
|
675
|
-
* 形状错在 P1-6 校验层已拒,这里只管把合法部分抽出来供展示 + activate 判定)。 */
|
|
676
573
|
export function parseRequires(raw) {
|
|
677
574
|
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
678
575
|
return undefined;
|
|
@@ -698,9 +595,6 @@ export function parseRequires(raw) {
|
|
|
698
595
|
}
|
|
699
596
|
return out.hostApi !== undefined || out.families !== undefined || out.plugins !== undefined ? out : undefined;
|
|
700
597
|
}
|
|
701
|
-
/** 依赖拓扑排序(P2-8):按 requires.plugins 把被依赖者排前面;同层保持扫描顺序。
|
|
702
|
-
* 返回 { order }(可激活顺序)+ { cyclic }(环/挂在环上的,调用方直接拒)。
|
|
703
|
-
* 边只连「本次扫描里存在」的对等端;缺失的对端由 activate 判定点名拒绝。 */
|
|
704
598
|
export function sortByRequires(ids, depsOf) {
|
|
705
599
|
const indeg = new Map();
|
|
706
600
|
const edges = new Map();
|
|
@@ -734,7 +628,6 @@ export function sortByRequires(ids, depsOf) {
|
|
|
734
628
|
const ordered = new Set(order);
|
|
735
629
|
return { order, cyclic: ids.filter((id) => !ordered.has(id)) };
|
|
736
630
|
}
|
|
737
|
-
/** 合并 manifest 基线与运行时注册:运行时同 id 覆盖,removed 里的删除;arrange 追加。 */
|
|
738
631
|
function mergeUiPluginUi(base, rt) {
|
|
739
632
|
const items = new Map();
|
|
740
633
|
for (const it of base?.items ?? [])
|
|
@@ -748,7 +641,6 @@ function mergeUiPluginUi(base, rt) {
|
|
|
748
641
|
return undefined;
|
|
749
642
|
return { items: [...items.values()], arrange };
|
|
750
643
|
}
|
|
751
|
-
/** 解析 manifest.settings → 合法 schema(坏字段跳过,最多 32 个)。 */
|
|
752
644
|
function parseSettingsSchema(raw) {
|
|
753
645
|
if (!Array.isArray(raw))
|
|
754
646
|
return [];
|
|
@@ -758,8 +650,6 @@ function parseSettingsSchema(raw) {
|
|
|
758
650
|
continue;
|
|
759
651
|
const o = f;
|
|
760
652
|
const key = typeof o.key === "string" ? o.key.trim() : "";
|
|
761
|
-
// 宿主数据源(models / thinkingLevels):合法值才认,非法当成没写(回落静态 options)。
|
|
762
|
-
// 只写 optionsFrom 没写 type = 视为 select(少让作者踩坑,与 options 的写法一致)。
|
|
763
653
|
const optionsFrom = typeof o.optionsFrom === "string" && SETTING_OPTIONS_FROM.has(o.optionsFrom)
|
|
764
654
|
? o.optionsFrom
|
|
765
655
|
: undefined;
|
|
@@ -805,13 +695,9 @@ function parseSettingsSchema(raw) {
|
|
|
805
695
|
}
|
|
806
696
|
return out;
|
|
807
697
|
}
|
|
808
|
-
/** secret 设置在 secrets 里的键(与插件手搓的机密键冲突概率极低的前缀)。 */
|
|
809
698
|
function secretSettingKey(key) {
|
|
810
699
|
return `setting:${key}`;
|
|
811
700
|
}
|
|
812
|
-
/** 用户 overlay 原文件(P2-9):`<dataDir>/plugin-overrides/<id>.json` 的 settings 节。
|
|
813
|
-
* 缺文件 = 没写(不警告);坏形状 → 警告并整体忽略(用户自己的文件,不连坐插件)。
|
|
814
|
-
* 顶层未知键忽略(前向兼容);每键校验在 cleanOverlaySettings 里按 schema 做。 */
|
|
815
701
|
function readSettingsOverlayFile(dataDir, pluginId) {
|
|
816
702
|
const empty = { raw: {}, warnings: [] };
|
|
817
703
|
if (!ID_RE.test(pluginId))
|
|
@@ -834,8 +720,6 @@ function readSettingsOverlayFile(dataDir, pluginId) {
|
|
|
834
720
|
}
|
|
835
721
|
return { raw: o.settings, warnings: [] };
|
|
836
722
|
}
|
|
837
|
-
/** 按 schema 清洗 overlay 值(P2-9):secret 一律拒绝(明文永不进加密 store)+
|
|
838
|
-
* 诊断;类型不对/越界/非法候选 → 警告并丢该键;schema 外的键 → 警告并忽略(拼写检查)。 */
|
|
839
723
|
function cleanOverlaySettings(schema, raw, lang) {
|
|
840
724
|
const values = {};
|
|
841
725
|
const warnings = [];
|
|
@@ -859,9 +743,6 @@ function cleanOverlaySettings(schema, raw, lang) {
|
|
|
859
743
|
}
|
|
860
744
|
return { values, warnings };
|
|
861
745
|
}
|
|
862
|
-
/** 三层合并(P2-9):schema 默认 < overlay(用户钉住的新默认,不 fork)< storage.json
|
|
863
|
-
* (面板保存,最高)。stored 不清洗(保存路径已洗过,可信);overlay 已预洗。
|
|
864
|
-
* secret 走加密 store,overlay 永不参与。 */
|
|
865
746
|
function resolveSettingsValues(schema, stored, overlay, secrets) {
|
|
866
747
|
const values = {};
|
|
867
748
|
const sources = {};
|
|
@@ -899,9 +780,6 @@ function resolveSettingsValues(schema, stored, overlay, secrets) {
|
|
|
899
780
|
}
|
|
900
781
|
return { values, sources };
|
|
901
782
|
}
|
|
902
|
-
/** 从 <pluginDir>/storage.json 读 settings 存值,按 schema 并默认值。
|
|
903
|
-
* secret 字段不返回明文:有 secrets 时返回有无(布尔),调用方是浏览器;
|
|
904
|
-
* 插件运行时要真值请用 runtimeSettingsValues。 */
|
|
905
783
|
function readStoredSettings(dir) {
|
|
906
784
|
try {
|
|
907
785
|
const parsed = JSON.parse(readFileSync(join(dir, "storage.json"), "utf8"));
|
|
@@ -909,19 +787,12 @@ function readStoredSettings(dir) {
|
|
|
909
787
|
return parsed.settings;
|
|
910
788
|
}
|
|
911
789
|
}
|
|
912
|
-
catch {
|
|
913
|
-
/* 无存储文件 = 全默认 */
|
|
914
|
-
}
|
|
790
|
+
catch { }
|
|
915
791
|
return {};
|
|
916
792
|
}
|
|
917
|
-
/** 插件运行时视角的设置值:非 secret 与 storedSettingsValues 同口径;secret 返回真值
|
|
918
|
-
* (无则回落默认值/空串)。只给插件服务端代码用,绝不下发浏览器。 */
|
|
919
793
|
function runtimeSettingsValues(dir, schema, secrets, overlay = {}) {
|
|
920
794
|
return resolveSettingsValues(schema, readStoredSettings(dir), overlay, { get: (k) => secrets.get(k) }).values;
|
|
921
795
|
}
|
|
922
|
-
/** 清洗单个非 secret 设置值(P2-9 抽出:保存路径与 overlay 复用同一套口径)。
|
|
923
|
-
* v === undefined → 回落 schema 默认;类型不对/越界/非法候选 → error(调用方丢弃该键)。
|
|
924
|
-
* secret 字段不在这里处理(overlay 直接拒绝,明文永不进加密 store)。 */
|
|
925
796
|
function cleanNonSecretSettingsField(f, v, lang) {
|
|
926
797
|
if (f.type === "number") {
|
|
927
798
|
const n = v === undefined ? Number(f.default ?? 0) : Number(v);
|
|
@@ -939,8 +810,6 @@ function cleanNonSecretSettingsField(f, v, lang) {
|
|
|
939
810
|
}
|
|
940
811
|
if (f.type === "select") {
|
|
941
812
|
const s = v === undefined ? "" : String(v);
|
|
942
|
-
// optionsFrom(宿主数据源):候选值在浏览器侧现算,服务端无从校验,
|
|
943
|
-
// 只做个长度护栏;非法值由用的时候(如 host.chat 切模型)报错。
|
|
944
813
|
if (f.optionsFrom) {
|
|
945
814
|
if (s.length > 200) {
|
|
946
815
|
return {
|
|
@@ -963,12 +832,7 @@ function cleanNonSecretSettingsField(f, v, lang) {
|
|
|
963
832
|
}
|
|
964
833
|
return { value: v === undefined ? (f.default ?? "") : String(v) };
|
|
965
834
|
}
|
|
966
|
-
|
|
967
|
-
* secret 字段加密后与 settings 同文件原子提交(明文永不落盘):空串/缺省 = 不改;
|
|
968
|
-
* 返回的 clean 含 secret 真值(给 onSettingsChanged 用),调用方不得下发浏览器。 */
|
|
969
|
-
function saveSettingsValues(dir, schema, values,
|
|
970
|
-
/** 错误文案语言(默认英文);调用方可传 () => getLang() 实现跟随。 */
|
|
971
|
-
lang, secrets) {
|
|
835
|
+
function saveSettingsValues(dir, schema, values, lang, secrets) {
|
|
972
836
|
const l = lang?.() ?? "en";
|
|
973
837
|
const clean = {};
|
|
974
838
|
const secretUpdates = {};
|
|
@@ -982,7 +846,6 @@ lang, secrets) {
|
|
|
982
846
|
clean[f.key] = r.value;
|
|
983
847
|
}
|
|
984
848
|
else if (f.type === "secret") {
|
|
985
|
-
// 空串/缺省 = 不改(浏览器侧回显的本来就是有无布尔,前端把“没碰”发成空串)。
|
|
986
849
|
if (v === undefined || v === "") {
|
|
987
850
|
clean[f.key] = secrets?.get(secretSettingKey(f.key)) ?? f.default ?? "";
|
|
988
851
|
}
|
|
@@ -1004,16 +867,12 @@ lang, secrets) {
|
|
|
1004
867
|
clean[f.key] = v === undefined ? (f.default ?? "") : String(v);
|
|
1005
868
|
}
|
|
1006
869
|
}
|
|
1007
|
-
// 保留 storage.json 里其它键(插件自己的数据),只动 settings。
|
|
1008
|
-
// settings 只存普通值,secret 密文由 commitSettings 写入宿主保留容器。
|
|
1009
870
|
const secretKeys = new Set(schema.filter((f) => f.type === "secret").map((f) => f.key));
|
|
1010
871
|
const persist = {};
|
|
1011
872
|
for (const [k, v] of Object.entries(clean))
|
|
1012
873
|
if (!secretKeys.has(k))
|
|
1013
874
|
persist[k] = v;
|
|
1014
875
|
const file = join(dir, "storage.json");
|
|
1015
|
-
// 与 PluginStorage.set/delete 同一把按文件路径的 RMW 锁(见 plugin-facilities.ts
|
|
1016
|
-
// 的 withFileRmwLock):两个「读-改-写」者串行,谁也不会拿旧快照抹掉对方刚写的键。
|
|
1017
876
|
withFileRmwLockSync(file, () => {
|
|
1018
877
|
if (secrets)
|
|
1019
878
|
secrets.commitSettings(persist, secretUpdates);
|
|
@@ -1043,40 +902,26 @@ export class PluginManager {
|
|
|
1043
902
|
dataDir;
|
|
1044
903
|
builtinCatalogPath;
|
|
1045
904
|
loaded = new Map();
|
|
1046
|
-
/** 已 import 过但无入口/失败的目录——避免重复 import 与重复报错。 */
|
|
1047
905
|
attempted = new Set();
|
|
1048
906
|
senders = new Set();
|
|
1049
907
|
messageHandlers = new Map();
|
|
1050
|
-
/** 插件注册的 AI 工具:pluginId → (name → 定义)。宿主经 agentTools() 读取。 */
|
|
1051
908
|
agentTools = new Map();
|
|
1052
|
-
/** AI 工具集合变化回调(index.ts 接到 AgentService,把新工具推入活跃会话)。 */
|
|
1053
909
|
onAgentToolsChanged = undefined;
|
|
1054
|
-
/** 插件斜杠命令注册表:pluginId → (name → 定义)。宿主经 listCommands() 读取。 */
|
|
1055
910
|
pluginCommands = new Map();
|
|
1056
|
-
/** 命令集合变化回调(index.ts 接到 AgentService,刷新各客户端命令目录)。 */
|
|
1057
911
|
onCommandsChanged = undefined;
|
|
1058
|
-
/** 插件常驻任务:pluginId → Map<taskId, PluginBgTask>。宿主经 bgTasks() 读取。 */
|
|
1059
912
|
pluginBgTasks = new Map();
|
|
1060
|
-
/** 任务集合变化回调(index.ts 接到 AgentService,重推 bg_servers)。 */
|
|
1061
913
|
onBgTasksChanged = undefined;
|
|
1062
|
-
/** 插件注册的内联标记:pluginId → (name → 定义)。 */
|
|
1063
914
|
pluginMarkers = new Map();
|
|
1064
|
-
/** 标记工具集合变化回调(index.ts 接到 AgentService,刷新各客户端 markers)。 */
|
|
1065
915
|
onMarkersChanged = undefined;
|
|
1066
|
-
/** P3:左栏插件运行条目(见 left-panel-running.ts)。 */
|
|
1067
916
|
panelRunning = new LeftPanelRunningStore();
|
|
1068
917
|
panelPushTimer = null;
|
|
1069
918
|
panelSweepTimer = null;
|
|
1070
|
-
/** 服务端重载纪元:每次 reload() +1,前端用作 import 缓存击穿参数。 */
|
|
1071
919
|
epochCounter = 0;
|
|
1072
|
-
/** 生命周期代次独立于客户端缓存 epoch(授权变化不会取消激活)。 */
|
|
1073
920
|
lifecycleGeneration = 0;
|
|
1074
921
|
loading;
|
|
1075
922
|
reloading;
|
|
1076
923
|
activatingEffects = new Set();
|
|
1077
|
-
/** 插件市场列表纪元:每次 add/remove +1,前端据此重渲。 */
|
|
1078
924
|
catalogEpoch = 0;
|
|
1079
|
-
/** 当前全局工作区(host.cwd 的背后存储)——随 notifyCwd 更新。 */
|
|
1080
925
|
globalCwd;
|
|
1081
926
|
httpWorkspace = new AsyncLocalStorage();
|
|
1082
927
|
get cwdValue() {
|
|
@@ -1085,9 +930,6 @@ export class PluginManager {
|
|
|
1085
930
|
set cwdValue(value) {
|
|
1086
931
|
this.globalCwd = value;
|
|
1087
932
|
}
|
|
1088
|
-
/** 当前项目的**额外工作区根**(宿主侧多根,见 protocol 的 set_workspace_roots)——
|
|
1089
|
-
* 由 index.ts 在 set_cwd / set_workspace_roots 后调 notifyWorkspaceRoots 同步。
|
|
1090
|
-
* 它们只影响「哪些路径算工作区内」(免授权的受支持路径),不改变 cwd 本身。 */
|
|
1091
933
|
globalWorkspaceRoots = [];
|
|
1092
934
|
get workspaceRoots() {
|
|
1093
935
|
return this.httpWorkspace.getStore()?.roots ?? this.globalWorkspaceRoots;
|
|
@@ -1095,40 +937,20 @@ export class PluginManager {
|
|
|
1095
937
|
set workspaceRoots(value) {
|
|
1096
938
|
this.globalWorkspaceRoots = value;
|
|
1097
939
|
}
|
|
1098
|
-
/** 插件目录授权表(<dataDir>/plugin-grants.json,issue #146)。 */
|
|
1099
940
|
grants;
|
|
1100
|
-
/** 特权 DOM 授权表(<dataDir>/plugin-dom.json):wantsDom 插件的 bundle 门禁。 */
|
|
1101
941
|
domConsentStore;
|
|
1102
|
-
/** scan() 顺手维护的 wantsDom 快照:静态门禁查它,不必每次 readdir。未知 id = 不拦。 */
|
|
1103
942
|
domWants = new Map();
|
|
1104
|
-
/** 由 index.ts 注入:向浏览器请求「插件要访问这个目录」的用户确认。 */
|
|
1105
943
|
pathAccessRequester = undefined;
|
|
1106
|
-
/** 由 index.ts 注入:授权表**变了**(新授权落表)时触发 —— 设置面板的「已授权目录」
|
|
1107
|
-
* 靠它即时刷新(以前只在 attach / 撤销时推,「点了允许但列表里还没出现」很难不被当成 bug)。 */
|
|
1108
944
|
onGrantsChanged = undefined;
|
|
1109
|
-
/** 插件运行时注册的 UI 贡献(host.ui.register/arrange),随 plugins 清单推送。 */
|
|
1110
945
|
uiRuntime = new Map();
|
|
1111
|
-
/** 通用反向代理注册表:归一化前缀 → { 插件 id, 回环目标 }(index.ts 按最长前缀命中透传)。 */
|
|
1112
946
|
proxyRoutes = new Map();
|
|
1113
|
-
/** manifest "ui" 基线(每次 scan 刷新;host.ui.list 与合并都读它)。 */
|
|
1114
947
|
uiBase = new Map();
|
|
1115
|
-
/** manifest 解析诊断(每次 scan 重算;合法插件无诊断时记空数组)。 */
|
|
1116
948
|
manifestDiags = new Map();
|
|
1117
|
-
/** 运行时诊断(工具/命令/路由/门控拒绝、激活失败等;反激活时随插件一起清)。
|
|
1118
|
-
* 只做可观测性:存一份 console.error/console.warn 之外的摘要,随 UiPluginInfo
|
|
1119
|
-
* 下发给设置面板“界面插件”页展开查看,不改变任何隔离/权限语义。 */
|
|
1120
949
|
runtimeDiags = new Map();
|
|
1121
|
-
/** 运行时分级日志(host.log 写入):内存环形缓冲,每插件最近 200 条。
|
|
1122
|
-
* 不落盘、随进程走;只经 plugin_data 按需拉取,绝不进 60ms 快照
|
|
1123
|
-
* (与 diagnostics 的“只读诊断随清单下发”正交,互不冲突)。 */
|
|
1124
950
|
pluginLogs = new Map();
|
|
1125
|
-
/** 插件间发布的服务表:serviceName → { pluginId: string; service: unknown } */
|
|
1126
951
|
pluginServices = new Map();
|
|
1127
|
-
/** 服务监听者表:serviceName → Set<(service: unknown | undefined) => void> */
|
|
1128
952
|
serviceListeners = new Map();
|
|
1129
|
-
constructor(dataDir, cwd,
|
|
1130
|
-
/** 随包发布的默认插件列表(<pkgRoot>/plugins/catalog.json)。缺省 = 无内置列表。 */
|
|
1131
|
-
builtinCatalogPath) {
|
|
953
|
+
constructor(dataDir, cwd, builtinCatalogPath) {
|
|
1132
954
|
this.dataDir = dataDir;
|
|
1133
955
|
this.builtinCatalogPath = builtinCatalogPath;
|
|
1134
956
|
cleanupRemovedPlugins(dataDir);
|
|
@@ -1137,12 +959,10 @@ export class PluginManager {
|
|
|
1137
959
|
this.permGrants = new PluginPermissionStore(dataDir);
|
|
1138
960
|
this.domConsentStore = new PluginDomConsent(dataDir);
|
|
1139
961
|
}
|
|
1140
|
-
/** index.ts 在客户端 set_cwd 成功后调用:更新全局工作区并扇出给
|
|
1141
|
-
* 所有已激活插件的 onCwdChange 钩子(异常隔离,不炸主进程)。 */
|
|
1142
962
|
notifyCwd(next) {
|
|
1143
963
|
const abs = resolve(next);
|
|
1144
964
|
if (abs === this.cwdValue)
|
|
1145
|
-
return;
|
|
965
|
+
return;
|
|
1146
966
|
this.cwdValue = abs;
|
|
1147
967
|
for (const [id, p] of this.loaded) {
|
|
1148
968
|
for (const h of p.cwdHandlers) {
|
|
@@ -1155,9 +975,6 @@ export class PluginManager {
|
|
|
1155
975
|
}
|
|
1156
976
|
}
|
|
1157
977
|
}
|
|
1158
|
-
/** index.ts 在客户端改动「额外工作区根」后调用(新增/移除/切项目都算):归一化后存下,
|
|
1159
|
-
* 同一份就是 no-op。刻意**不发** onCwdChange 钩子:工作区根变化不动 cwd,那个钩子的
|
|
1160
|
-
* 语义就是「当前目录变了」(插件该切根的时机)。 */
|
|
1161
978
|
notifyWorkspaceRoots(roots) {
|
|
1162
979
|
const next = normalizeWorkspaceRoots(roots ?? []);
|
|
1163
980
|
if (next.length === this.workspaceRoots.length && next.every((p, i) => p === this.workspaceRoots[i]))
|
|
@@ -1167,7 +984,6 @@ export class PluginManager {
|
|
|
1167
984
|
get pluginsDir() {
|
|
1168
985
|
return join(this.dataDir, "plugins");
|
|
1169
986
|
}
|
|
1170
|
-
/** 全部插件注册的斜杠命令(按插件 id 稳定排序)。 */
|
|
1171
987
|
listCommands() {
|
|
1172
988
|
const out = [];
|
|
1173
989
|
for (const id of [...this.pluginCommands.keys()].sort()) {
|
|
@@ -1175,7 +991,6 @@ export class PluginManager {
|
|
|
1175
991
|
}
|
|
1176
992
|
return out;
|
|
1177
993
|
}
|
|
1178
|
-
/** 按名查找命令(供 prompt() 拦截执行;找不到返回 null)。 */
|
|
1179
994
|
findCommand(name) {
|
|
1180
995
|
for (const [pluginId, table] of this.pluginCommands) {
|
|
1181
996
|
if (table.has(name))
|
|
@@ -1183,7 +998,6 @@ export class PluginManager {
|
|
|
1183
998
|
}
|
|
1184
999
|
return null;
|
|
1185
1000
|
}
|
|
1186
|
-
/** 全部插件注册的常驻后台任务(扁平化为 BgServer 形状)。 */
|
|
1187
1001
|
bgTasks() {
|
|
1188
1002
|
const out = [];
|
|
1189
1003
|
for (const [pluginId, table] of this.pluginBgTasks) {
|
|
@@ -1199,7 +1013,6 @@ export class PluginManager {
|
|
|
1199
1013
|
}
|
|
1200
1014
|
return out;
|
|
1201
1015
|
}
|
|
1202
|
-
/** 停止一个插件任务(kill_background_server with taskId);返回是否命中。 */
|
|
1203
1016
|
stopPluginBgTask(taskId) {
|
|
1204
1017
|
for (const [pluginId, table] of this.pluginBgTasks) {
|
|
1205
1018
|
const t = table.get(taskId);
|
|
@@ -1217,19 +1030,12 @@ export class PluginManager {
|
|
|
1217
1030
|
try {
|
|
1218
1031
|
this.onBgTasksChanged?.();
|
|
1219
1032
|
}
|
|
1220
|
-
catch {
|
|
1221
|
-
// best-effort:可选 UI 刷新,失败忽略。
|
|
1222
|
-
}
|
|
1033
|
+
catch { }
|
|
1223
1034
|
return true;
|
|
1224
1035
|
}
|
|
1225
1036
|
return false;
|
|
1226
1037
|
}
|
|
1227
|
-
|
|
1228
|
-
* 原子写 storage.json 的 settings 键、通知插件 onSettingsChanged、重推清单
|
|
1229
|
-
* 让前端回显。返回错误信息或 null(成功)。 */
|
|
1230
|
-
savePluginSettings(pluginId, values,
|
|
1231
|
-
/** 错误文案语言(默认英文);调用方可传 () => getLang() 实现跟随。 */
|
|
1232
|
-
lang) {
|
|
1038
|
+
savePluginSettings(pluginId, values, lang) {
|
|
1233
1039
|
const l = lang?.() ?? "en";
|
|
1234
1040
|
if (!ID_RE.test(pluginId))
|
|
1235
1041
|
return { error: pick(l, "非法的插件 id", "Invalid plugin id", "plugins.id.invalid") };
|
|
@@ -1250,7 +1056,6 @@ export class PluginManager {
|
|
|
1250
1056
|
const { error, clean } = result;
|
|
1251
1057
|
if (error)
|
|
1252
1058
|
return { error };
|
|
1253
|
-
// 通知插件(异常隔离)
|
|
1254
1059
|
for (const h of this.loaded.get(pluginId)?.settingsHandlers ?? []) {
|
|
1255
1060
|
try {
|
|
1256
1061
|
h(clean);
|
|
@@ -1259,42 +1064,32 @@ export class PluginManager {
|
|
|
1259
1064
|
console.error(`[plugin:${pluginId}] onSettingsChanged handler failed:`, err);
|
|
1260
1065
|
}
|
|
1261
1066
|
}
|
|
1262
|
-
// 重推 plugins 清单(含新 settingsValues),前端回显。
|
|
1263
1067
|
void this.pushToAll().catch(() => { });
|
|
1264
1068
|
return {};
|
|
1265
1069
|
}
|
|
1266
|
-
/** 当前重载纪元(随 plugins 消息下发)。 */
|
|
1267
1070
|
get epoch() {
|
|
1268
1071
|
return this.epochCounter;
|
|
1269
1072
|
}
|
|
1270
|
-
/** 用户自定义插件列表文件(<dataDir>/plugin-catalog.json)。 */
|
|
1271
1073
|
get customCatalogPath() {
|
|
1272
1074
|
return join(this.dataDir, "plugin-catalog.json");
|
|
1273
1075
|
}
|
|
1274
|
-
/** 官方社区生态插件清单文件(<dataDir>/community-catalog.json)。 */
|
|
1275
1076
|
get communityCatalogPath() {
|
|
1276
1077
|
return join(this.dataDir, "community-catalog.json");
|
|
1277
1078
|
}
|
|
1278
|
-
/** 合并后的插件市场列表(builtin + community + 用户自定义,同 id 用户覆盖)。 */
|
|
1279
1079
|
catalog() {
|
|
1280
1080
|
return this.builtinCatalogPath
|
|
1281
1081
|
? readCatalog(this.builtinCatalogPath, this.customCatalogPath, this.communityCatalogPath)
|
|
1282
1082
|
: [];
|
|
1283
1083
|
}
|
|
1284
|
-
/** 插件市场列表纪元(随 plugin_catalog 消息下发)。 */
|
|
1285
1084
|
get catalogEpochValue() {
|
|
1286
1085
|
return this.catalogEpoch;
|
|
1287
1086
|
}
|
|
1288
|
-
/** 把插件市场列表推给所有 socket。 */
|
|
1289
1087
|
async pushCatalog() {
|
|
1290
1088
|
this.deliverAll({ type: "plugin_catalog", entries: this.catalog(), epoch: this.catalogEpoch });
|
|
1291
1089
|
}
|
|
1292
|
-
/** 把能力授权表推给所有 socket(attach 推 + 授权/撤销后重推)。 */
|
|
1293
1090
|
async pushPermissions() {
|
|
1294
1091
|
this.deliverAll({ type: "plugin_permissions", grants: this.permGrants.list() });
|
|
1295
1092
|
}
|
|
1296
|
-
/** 往用户自定义列表加一条(同 id 覆盖);返回错误信息或 null(成功)。
|
|
1297
|
-
* 成功后 epoch+1 并重推列表。 */
|
|
1298
1093
|
addCatalogEntry(input, lang) {
|
|
1299
1094
|
try {
|
|
1300
1095
|
addCustomEntry(this.customCatalogPath, input, lang);
|
|
@@ -1306,7 +1101,6 @@ export class PluginManager {
|
|
|
1306
1101
|
return { error: err.message };
|
|
1307
1102
|
}
|
|
1308
1103
|
}
|
|
1309
|
-
/** 移除一条用户自定义条目(builtin / 社区条目不可经此删除);返回错误信息或 null。 */
|
|
1310
1104
|
removeCatalogEntry(id, lang) {
|
|
1311
1105
|
const l = lang?.() ?? "en";
|
|
1312
1106
|
try {
|
|
@@ -1328,8 +1122,6 @@ export class PluginManager {
|
|
|
1328
1122
|
this.senders.add(s);
|
|
1329
1123
|
return () => this.senders.delete(s);
|
|
1330
1124
|
}
|
|
1331
|
-
/** 当前在线(hello 过)的 clientId 去重集合。插件授权弹窗广播后按它做应答
|
|
1332
|
-
* 来源绑定——广播后才连上的端没见过弹窗,不许代答(index.ts 三类授权应答校验)。 */
|
|
1333
1125
|
onlineClientIds() {
|
|
1334
1126
|
const out = new Set();
|
|
1335
1127
|
for (const s of this.senders) {
|
|
@@ -1339,14 +1131,9 @@ export class PluginManager {
|
|
|
1339
1131
|
}
|
|
1340
1132
|
return [...out];
|
|
1341
1133
|
}
|
|
1342
|
-
/** 客户端上行:路由给对应插件的处理器;未知/未激活的插件静默丢弃。
|
|
1343
|
-
* 插件代码不可信——同步抛错与返回的 Promise rejection 都必须隔离在
|
|
1344
|
-
* 这里,绝不能炸主进程。 */
|
|
1345
1134
|
handleMessage(pluginId, payload, from) {
|
|
1346
1135
|
if (!ID_RE.test(pluginId))
|
|
1347
1136
|
return;
|
|
1348
|
-
// 宿主保留通道:运行时日志按需拉取/清空(directed plugin_data 回包)。
|
|
1349
|
-
// 不进插件 onMessage(插件不可见),from 缺席时无法定向回包则静默丢弃。
|
|
1350
1137
|
if (isPluginLogsRequest(payload)) {
|
|
1351
1138
|
if (!from)
|
|
1352
1139
|
return;
|
|
@@ -1371,8 +1158,6 @@ export class PluginManager {
|
|
|
1371
1158
|
ret.catch((err) => {
|
|
1372
1159
|
console.error(`[plugin:${pluginId}] async message handler failed:`, err);
|
|
1373
1160
|
});
|
|
1374
|
-
// 超时护栏:响应由 handler 自己 sendTo/broadcast 发出,超时只是记
|
|
1375
|
-
// 日志不再等待——绝不能让单条消息把客户端 pending 管线无限拖死。
|
|
1376
1161
|
const timer = setTimeout(() => {
|
|
1377
1162
|
console.error(`[plugin:${pluginId}] message handler 超时(>${MESSAGE_TIMEOUT_MS}ms),已不再等待`);
|
|
1378
1163
|
}, MESSAGE_TIMEOUT_MS);
|
|
@@ -1384,7 +1169,6 @@ export class PluginManager {
|
|
|
1384
1169
|
}
|
|
1385
1170
|
}
|
|
1386
1171
|
}
|
|
1387
|
-
/** 首次安装/能力变更时提醒在线用户(marker 文件记录上次激活时的声明)。 */
|
|
1388
1172
|
async maybeConsentNotice(info, dir, perms) {
|
|
1389
1173
|
try {
|
|
1390
1174
|
const markerFile = join(dir, ".pi-approved");
|
|
@@ -1393,11 +1177,9 @@ export class PluginManager {
|
|
|
1393
1177
|
try {
|
|
1394
1178
|
prev = JSON.parse(readFileSync(markerFile, "utf8"))?.key ?? "";
|
|
1395
1179
|
}
|
|
1396
|
-
catch {
|
|
1397
|
-
/* 无 marker = 首次安装 */
|
|
1398
|
-
}
|
|
1180
|
+
catch { }
|
|
1399
1181
|
if (prev === key)
|
|
1400
|
-
return;
|
|
1182
|
+
return;
|
|
1401
1183
|
const list = perms.length ? perms.join(", ") : "无";
|
|
1402
1184
|
this.notifyAll(perms.length ? "warning" : "info", `插件「${info.name}」已激活(${prev ? "能力清单变更" : "首次安装"};声明能力:${list})——请确认来源可信`, `Plugin "${info.name}" activated (${prev ? "capability list changed" : "first install"}; declared: ${list}) — verify the source is trusted`);
|
|
1403
1185
|
writeFileSync(markerFile, JSON.stringify({ v: 1, key, perms }), "utf8");
|
|
@@ -1406,8 +1188,6 @@ export class PluginManager {
|
|
|
1406
1188
|
console.error(`[plugin:${info.id}] consent notice failed:`, err);
|
|
1407
1189
|
}
|
|
1408
1190
|
}
|
|
1409
|
-
/** index.ts 的 /plugins-api/:id/* 挂载点转发到这里:找到对应插件的已注册
|
|
1410
|
-
* 路由并执行;未知插件/路径 → 404,handler 抛错 → 500(不炸进程)。 */
|
|
1411
1191
|
handleHttp(pluginId, method, pathIn, req, res, workspace) {
|
|
1412
1192
|
const context = {
|
|
1413
1193
|
cwd: resolve(workspace?.cwd ?? this.globalCwd),
|
|
@@ -1425,7 +1205,6 @@ export class PluginManager {
|
|
|
1425
1205
|
let handler = table?.get(`${method.toUpperCase()} ${path}`);
|
|
1426
1206
|
if (!handler && table) {
|
|
1427
1207
|
const m = method.toUpperCase();
|
|
1428
|
-
// 通配符路由支持(例如 host.route("GET", "/api/web/*", ...) 或以 "/*" 结尾的前缀)
|
|
1429
1208
|
for (const [key, fn] of table.entries()) {
|
|
1430
1209
|
if (!key.startsWith(`${m} `))
|
|
1431
1210
|
continue;
|
|
@@ -1445,10 +1224,6 @@ export class PluginManager {
|
|
|
1445
1224
|
}
|
|
1446
1225
|
try {
|
|
1447
1226
|
req.pluginPath = path;
|
|
1448
|
-
// 异步 handler(`async (req, res) => …`)的 rejection 不会被这里的 try 接住,
|
|
1449
|
-
// 会变成 unhandledRejection 直接杀掉整个服务(插件读文件失败、host.fs 越界
|
|
1450
|
-
// 拒绝、上游超时…都会走到这条路上)——用 Promise.resolve().catch 兜住,
|
|
1451
|
-
// 与同步抛错同样转 500。
|
|
1452
1227
|
void Promise.resolve(handler(req, res)).catch((err) => {
|
|
1453
1228
|
console.error(`[plugin:${pluginId}] http ${method} ${path} failed:`, err);
|
|
1454
1229
|
if (!res.headersSent)
|
|
@@ -1465,8 +1240,6 @@ export class PluginManager {
|
|
|
1465
1240
|
res.end();
|
|
1466
1241
|
}
|
|
1467
1242
|
}
|
|
1468
|
-
/** 注册通用代理前缀(host.registerProxy 的本体,index.ts 只读 findProxy)。
|
|
1469
|
-
* 成功返回归一化前缀;前缀非法/目标非法/被其它插件占用返回 null(调用方记诊断)。 */
|
|
1470
1243
|
registerProxy(pluginId, prefix, target) {
|
|
1471
1244
|
const p = normalizeProxyPrefix(prefix);
|
|
1472
1245
|
const t = normalizeProxyTarget(target);
|
|
@@ -1478,7 +1251,6 @@ export class PluginManager {
|
|
|
1478
1251
|
this.proxyRoutes.set(p, { pluginId, host: t.host, port: t.port });
|
|
1479
1252
|
return p;
|
|
1480
1253
|
}
|
|
1481
|
-
/** 注销代理前缀(同插件才能注销自己的;返回是否真删掉了)。 */
|
|
1482
1254
|
unregisterProxy(pluginId, prefix) {
|
|
1483
1255
|
const p = normalizeProxyPrefix(prefix);
|
|
1484
1256
|
if (!p)
|
|
@@ -1487,11 +1259,6 @@ export class PluginManager {
|
|
|
1487
1259
|
return false;
|
|
1488
1260
|
return this.proxyRoutes.delete(p);
|
|
1489
1261
|
}
|
|
1490
|
-
/**
|
|
1491
|
-
* 插件发布一个命名服务。
|
|
1492
|
-
* 同名且被其它插件占有时抛错;同一插件重复 provide 时覆盖。
|
|
1493
|
-
* 返回注销函数(撤销自身发布并广播 undefined)。
|
|
1494
|
-
*/
|
|
1495
1262
|
provideService(pluginId, name, service) {
|
|
1496
1263
|
const trimmed = String(name ?? "").trim();
|
|
1497
1264
|
if (!trimmed || !SERVICE_NAME_RE.test(trimmed) || trimmed.length > 64) {
|
|
@@ -1576,8 +1343,6 @@ export class PluginManager {
|
|
|
1576
1343
|
}
|
|
1577
1344
|
};
|
|
1578
1345
|
}
|
|
1579
|
-
/** index.ts 转发/upgrade 共用:请求路径的最长前缀命中(无命中返回 undefined)。
|
|
1580
|
-
* 查表前小写化:注册前缀统一小写归一,大小写混写也命中同一条。 */
|
|
1581
1346
|
findProxy(path) {
|
|
1582
1347
|
const p = matchProxyPrefix(String(path ?? "").toLowerCase(), this.proxyRoutes.keys());
|
|
1583
1348
|
if (!p)
|
|
@@ -1590,11 +1355,9 @@ export class PluginManager {
|
|
|
1590
1355
|
broadcast(pluginId, payload) {
|
|
1591
1356
|
this.deliverAll({ type: "plugin_data", pluginId, payload });
|
|
1592
1357
|
}
|
|
1593
|
-
/** 系统通知:发给所有 socket(复用 notice 消息,前端 toast 展示)。 */
|
|
1594
1358
|
notifyAll(level, text, textEn) {
|
|
1595
1359
|
this.deliverAll({ type: "notice", level, text, textEn });
|
|
1596
1360
|
}
|
|
1597
|
-
/** 给指定客户端定向发一条插件消息;找不到该 socket 时静默忽略。 */
|
|
1598
1361
|
sendTo(clientId, pluginId, payload) {
|
|
1599
1362
|
for (const s of this.senders) {
|
|
1600
1363
|
if (s.cid() !== clientId)
|
|
@@ -1602,31 +1365,21 @@ export class PluginManager {
|
|
|
1602
1365
|
try {
|
|
1603
1366
|
s.send({ type: "plugin_data", pluginId, payload });
|
|
1604
1367
|
}
|
|
1605
|
-
catch {
|
|
1606
|
-
/* dead socket */
|
|
1607
|
-
}
|
|
1368
|
+
catch { }
|
|
1608
1369
|
}
|
|
1609
1370
|
}
|
|
1610
|
-
/** 目录清单 + 当前 epoch 推给所有 socket。 */
|
|
1611
1371
|
async pushToAll() {
|
|
1612
1372
|
const list = await this.scan();
|
|
1613
1373
|
this.deliverAll({ type: "plugins", plugins: list, epoch: this.epochCounter });
|
|
1614
1374
|
}
|
|
1615
|
-
/** index.ts 静态门禁查这个:wantsDom 且未授权 → 403。 */
|
|
1616
1375
|
isDomBundleBlocked(pluginId) {
|
|
1617
1376
|
if (!this.domWants.get(pluginId))
|
|
1618
1377
|
return false;
|
|
1619
1378
|
return !this.domConsentStore.has(pluginId);
|
|
1620
1379
|
}
|
|
1621
|
-
/** plugin_dom_consent 两步握手的预检(index.ts 用):只有声明了 dom 能力的
|
|
1622
|
-
* 插件才允许生成在途 consent 请求——任意 pluginId 进不来,在途表不膨胀。 */
|
|
1623
1380
|
isDomPlugin(pluginId) {
|
|
1624
1381
|
return this.domWants.get(pluginId) === true;
|
|
1625
1382
|
}
|
|
1626
|
-
/** 特权 DOM 授权/撤销(设置面板 plugin_dom_consent)。
|
|
1627
|
-
* 返回 { changed }:变了才 epoch+1 重推(浏览器按新 epoch 重拉 bundle,
|
|
1628
|
-
* 失败缓存随 syncPluginViews 的 epoch 切换清掉);目标不是 wantsDom 插件
|
|
1629
|
-
* 或 id 非法 → { changed: false, error }。 */
|
|
1630
1383
|
async setDomConsent(pluginId, granted) {
|
|
1631
1384
|
const id = String(pluginId ?? "").trim();
|
|
1632
1385
|
if (!ID_RE.test(id))
|
|
@@ -1636,14 +1389,10 @@ export class PluginManager {
|
|
|
1636
1389
|
const changed = this.domConsentStore.set(id, granted);
|
|
1637
1390
|
if (!changed)
|
|
1638
1391
|
return { changed: false };
|
|
1639
|
-
// 授权前后 bundle 的 403/200 状态翻转:epoch+1 让浏览器丢掉旧模块缓存重拉。
|
|
1640
1392
|
this.epochCounter += 1;
|
|
1641
1393
|
void this.pushToAll().catch(() => { });
|
|
1642
1394
|
return { changed: true };
|
|
1643
1395
|
}
|
|
1644
|
-
/** 服务端热重载:反激活全部 → 清缓存 → 重扫重激活 → epoch+1。
|
|
1645
|
-
* 返回新目录清单(含激活结果)。重激活后的插件实例是新模块,
|
|
1646
|
-
* 内存状态为初始值——逐个客户端触发 onAttach 让它们重推自身状态。 */
|
|
1647
1396
|
reload(lang) {
|
|
1648
1397
|
if (this.reloading)
|
|
1649
1398
|
return this.reloading;
|
|
@@ -1668,8 +1417,6 @@ export class PluginManager {
|
|
|
1668
1417
|
this.reloading = pending;
|
|
1669
1418
|
return pending;
|
|
1670
1419
|
}
|
|
1671
|
-
/** 每个客户端 attach 后调用:让各插件向该客户端推送自身完整状态。
|
|
1672
|
-
* 异常隔离——单个插件钩子报错不影响其他插件与其他钩子。 */
|
|
1673
1420
|
notifyAttach(clientId) {
|
|
1674
1421
|
for (const [id, p] of this.loaded) {
|
|
1675
1422
|
for (const h of p.attachHandlers) {
|
|
@@ -1682,7 +1429,6 @@ export class PluginManager {
|
|
|
1682
1429
|
}
|
|
1683
1430
|
}
|
|
1684
1431
|
}
|
|
1685
|
-
/** agent-service 调:把 SDK 工具执行事件扇出给所有插件(异常隔离)。 */
|
|
1686
1432
|
emitToolEvent(ev) {
|
|
1687
1433
|
for (const p of this.loaded.values()) {
|
|
1688
1434
|
for (const h of p.toolHandlers) {
|
|
@@ -1695,17 +1441,12 @@ export class PluginManager {
|
|
|
1695
1441
|
}
|
|
1696
1442
|
}
|
|
1697
1443
|
}
|
|
1698
|
-
/** agent-service 调(P1-5):bash/read 执行前的守卫求值。首个 deny/ask 胜出;
|
|
1699
|
-
* 守卫抛错/超时一律按弃权(不阻断)。无守卫时回 allow,调用方可直接放行。 */
|
|
1700
1444
|
async evaluateToolPre(req, _lang = "en") {
|
|
1701
1445
|
const frozen = {
|
|
1702
1446
|
toolName: req.toolName,
|
|
1703
1447
|
params: freezeParams(req.params),
|
|
1704
1448
|
conversationId: req.conversationId,
|
|
1705
1449
|
};
|
|
1706
|
-
// #489:对 this.loaded 取快照再迭代——下面的 withGuardTimeout 是 await 点,
|
|
1707
|
-
// 期间插件 reload/uninstall 会原地 clear 同一个 Map(实例不变),Map 迭代器
|
|
1708
|
-
// 立即 done → 剩余插件的 pre 守卫一次都不被求值 → fail-open 直接放行。
|
|
1709
1450
|
const loadedSnapshot = [...this.loaded.values()];
|
|
1710
1451
|
for (const p of loadedSnapshot) {
|
|
1711
1452
|
for (const h of p.preGuards ?? []) {
|
|
@@ -1729,8 +1470,6 @@ export class PluginManager {
|
|
|
1729
1470
|
}
|
|
1730
1471
|
return { verdict: { decision: "allow" } };
|
|
1731
1472
|
}
|
|
1732
|
-
/** agent-service 调(P1-5):bash/read 执行后的守卫合并。逐个顺序合进结果;
|
|
1733
|
-
* 抛错/超时的那个被跳过(其余继续)。返回合并后的 content(无编辑回 undefined)。 */
|
|
1734
1473
|
async evaluateToolPost(req, lang = "en") {
|
|
1735
1474
|
let content = Array.isArray(req.result?.content)
|
|
1736
1475
|
? [...req.result.content]
|
|
@@ -1744,8 +1483,6 @@ export class PluginManager {
|
|
|
1744
1483
|
result: req.result,
|
|
1745
1484
|
conversationId: req.conversationId,
|
|
1746
1485
|
};
|
|
1747
|
-
// #489:同 evaluateToolPre——await 点期间 reload 原地 clear Map 会让迭代器
|
|
1748
|
-
// 提前终止,取快照迭代保证本轮请求遍历的是发起时的插件集合。
|
|
1749
1486
|
const postLoadedSnapshot = [...this.loaded.values()];
|
|
1750
1487
|
for (const p of postLoadedSnapshot) {
|
|
1751
1488
|
for (const h of p.postGuards ?? []) {
|
|
@@ -1773,54 +1510,28 @@ export class PluginManager {
|
|
|
1773
1510
|
return undefined;
|
|
1774
1511
|
return { content: base.content, pluginIds };
|
|
1775
1512
|
}
|
|
1776
|
-
/** 阻断时给模型看的一句话(pre 守卫命中后由 agent-service 取用)。 */
|
|
1777
1513
|
guardDenialText(verdict, pluginId, lang = "en") {
|
|
1778
1514
|
return denialText(verdict, pluginId, lang);
|
|
1779
1515
|
}
|
|
1780
|
-
/** index.ts 注入:读取对话快照(轨迹类插件经 host.getActiveConversation 调用)。
|
|
1781
|
-
* #542:按 clientId 取「该标签页正在看的对话」;缺省回落「最近活跃的非子代理」。 */
|
|
1782
1516
|
conversationProvider = undefined;
|
|
1783
|
-
/** index.ts 注入:插件无头调用 agent(微信通道等经 host.chat 调用)。 */
|
|
1784
1517
|
chatProvider = undefined;
|
|
1785
|
-
|
|
1786
|
-
* 无注入回 {ok:false}(如 DSH 引擎),绝不抛错。 */
|
|
1518
|
+
workItemsProvider = undefined;
|
|
1787
1519
|
llmProvider = undefined;
|
|
1788
|
-
/** 由 index.ts 接入 agent-service:对话列表(host.conversations.list 的底层)。
|
|
1789
|
-
* 同步数组与 Promise 都收(index.ts 接线是 async 的),host 侧归一化。无注入回 []。 */
|
|
1790
1520
|
conversationLister = undefined;
|
|
1791
|
-
/** 由 index.ts 接入 agent-service:对话搜索(host.conversations.search 的底层)。无注入时
|
|
1792
|
-
* host 回退用 conversationLister 做标题过滤;两者都无回 []。 */
|
|
1793
1521
|
conversationSearcher = undefined;
|
|
1794
|
-
/** 由 index.ts 接入 agent-service:向指定对话发用户消息(host.prompt 的底层)。
|
|
1795
|
-
* 无注入回 {ok:false},绝不抛错。 */
|
|
1796
1522
|
conversationWriter = undefined;
|
|
1797
|
-
/** 由 index.ts 接入 agent-service:插队指定对话的运行(host.steer 的底层)。无注入回 {ok:false}。 */
|
|
1798
1523
|
runSteerer = undefined;
|
|
1799
|
-
/** 由 index.ts 接入 agent-service:中止指定对话的运行(host.abortRun 的底层)。无注入回 {ok:false}。 */
|
|
1800
1524
|
runAborter = undefined;
|
|
1801
|
-
/** 由 index.ts 接入 agent-service:模型列表(host.models.list 的底层)。无注入回 []。 */
|
|
1802
1525
|
modelLister = undefined;
|
|
1803
|
-
/** 由 index.ts 接入 McpBridge:供 host.mcp 读取实时运行状态与热重载。 */
|
|
1804
1526
|
mcpProjects = undefined;
|
|
1805
|
-
/** 插件能力动态授权表(host.requestPermission 的底层,见 server/plugin-permissions.ts)。 */
|
|
1806
1527
|
permGrants;
|
|
1807
|
-
/** 由 index.ts 注入:向浏览器请求能力授权的用户确认({ok, remember})。
|
|
1808
|
-
* 未注入(DSH/无浏览器)时 requestPermission 直接回 false。 */
|
|
1809
1528
|
permissionRequester = undefined;
|
|
1810
|
-
/** 授权表**变了**(新授权落盘/撤销)时触发 —— 设置面板「已授权能力」即时刷新。 */
|
|
1811
1529
|
onPermGrantsChanged = undefined;
|
|
1812
|
-
/** 会话统计订阅(host.onStats 注册到这里;emitStats 异常隔离扇出)。 */
|
|
1813
1530
|
statsHandlers = new Set();
|
|
1814
|
-
/** 流式增量订阅(host.onStreaming 注册到这里;emitStreaming 只透传,发送方负责节流)。 */
|
|
1815
1531
|
streamingHandlers = new Set();
|
|
1816
|
-
/** 插件间总线订阅:topic → 处理器集合(host.events.on 注册;反激活时清理该插件全部订阅)。 */
|
|
1817
1532
|
busHandlers = new Map();
|
|
1818
|
-
/** host.chatWait 的 run_end 等待者(emitRunEvent 里按 runId / conversationId 唤醒)。 */
|
|
1819
1533
|
chatWaiters = [];
|
|
1820
|
-
/** 激活期插件的能力门控快照(activate 开头写入、落盘 loaded 或失败即删):
|
|
1821
|
-
* canUse 在 host 对象可用之前(loaded.set 之前)也要能判定。 */
|
|
1822
1534
|
activatingGates = new Map();
|
|
1823
|
-
/** 会话统计扇出(异常隔离;订阅者崩了只记日志)。发送方(如定期推送快照统计处)负责节流。 */
|
|
1824
1535
|
emitStats(s) {
|
|
1825
1536
|
for (const h of this.statsHandlers) {
|
|
1826
1537
|
try {
|
|
@@ -1831,7 +1542,6 @@ export class PluginManager {
|
|
|
1831
1542
|
}
|
|
1832
1543
|
}
|
|
1833
1544
|
}
|
|
1834
|
-
/** 流式增量透传(异常隔离;宿主不做节流,由发送方保证频率)。 */
|
|
1835
1545
|
emitStreaming(ev) {
|
|
1836
1546
|
for (const h of this.streamingHandlers) {
|
|
1837
1547
|
try {
|
|
@@ -1842,11 +1552,6 @@ export class PluginManager {
|
|
|
1842
1552
|
}
|
|
1843
1553
|
}
|
|
1844
1554
|
}
|
|
1845
|
-
/** 能力门控统一入口(原 activate 内联 can() 的抽出):
|
|
1846
|
-
* - 声明 "fs" = 读写全开(向后兼容);仅声明 "fs:read" 时 "fs" 全量不算拥有
|
|
1847
|
-
* (读放行、写与 project.create 被拒,错误提示缺 fs/fs:write);
|
|
1848
|
-
* - 其它族精确匹配;"dom:anchor" 与 "dom" 互不相干(各管各的族);
|
|
1849
|
-
* - 未声明 permissions 且 apiVersion<2 = 旧全权模式(警告一次后放行)。 */
|
|
1850
1555
|
canUse(pluginId, family) {
|
|
1851
1556
|
const rec = this.activatingGates.get(pluginId) ?? this.loaded.get(pluginId);
|
|
1852
1557
|
const fam = String(family ?? "");
|
|
@@ -1883,8 +1588,6 @@ export class PluginManager {
|
|
|
1883
1588
|
return true;
|
|
1884
1589
|
return legacy(fam);
|
|
1885
1590
|
}
|
|
1886
|
-
/** host.chatWait 等待指定对话的 run_end(默认 120s 超时由调用方钳制后传入)。
|
|
1887
|
-
* runId 优先:过户换持有方 / 撞 id 改名后 conversationId 已经对不上了。 */
|
|
1888
1591
|
waitRunEnd(conversationId, timeoutMs, runId) {
|
|
1889
1592
|
return new Promise((resolve) => {
|
|
1890
1593
|
const entry = {
|
|
@@ -1902,9 +1605,6 @@ export class PluginManager {
|
|
|
1902
1605
|
this.chatWaiters.push(entry);
|
|
1903
1606
|
});
|
|
1904
1607
|
}
|
|
1905
|
-
/** 当前打开对话的快照(无提供者/暂无对话时返回 null)。
|
|
1906
|
-
* #542:options.clientId 取该标签页正在看的对话;缺省 = 全局回落
|
|
1907
|
-
* (最近活跃的**非子代理**会话,子代理不再把用户正在看的对话挤出去)。 */
|
|
1908
1608
|
getActiveConversation(options) {
|
|
1909
1609
|
let snap;
|
|
1910
1610
|
try {
|
|
@@ -1916,8 +1616,6 @@ export class PluginManager {
|
|
|
1916
1616
|
}
|
|
1917
1617
|
return snap ? copySnapshotForPlugins(snap) : null;
|
|
1918
1618
|
}
|
|
1919
|
-
/** agent-service 调:某客户端的对话模型切换成功(#542)—— 订阅
|
|
1920
|
-
* host.onClientModelChanged 的插件立即收到新快照(异常隔离,不会拖慢切换)。 */
|
|
1921
1619
|
emitClientModelChanged(snap) {
|
|
1922
1620
|
const copy = copySnapshotForPlugins(snap);
|
|
1923
1621
|
for (const p of this.loaded.values()) {
|
|
@@ -1934,8 +1632,6 @@ export class PluginManager {
|
|
|
1934
1632
|
}
|
|
1935
1633
|
}
|
|
1936
1634
|
}
|
|
1937
|
-
/** agent-service 调:当前打开对话变了(切历史会话/切 running 对话/新对话)——
|
|
1938
|
-
* 轨迹类插件靠它重拉时间线(异常隔离)。 */
|
|
1939
1635
|
emitConversationChanged() {
|
|
1940
1636
|
for (const p of this.loaded.values()) {
|
|
1941
1637
|
if (p.convChangeHandlers.size === 0)
|
|
@@ -1950,9 +1646,6 @@ export class PluginManager {
|
|
|
1950
1646
|
}
|
|
1951
1647
|
}
|
|
1952
1648
|
}
|
|
1953
|
-
/** agent-service 调:把运行轨迹事件扇出给所有插件(异常隔离,
|
|
1954
|
-
* 与 emitToolEvent 同级;订阅者崩了只记日志,不影响主流程)。
|
|
1955
|
-
* run_end 顺带唤醒 host.chatWait 的等待者(按 conversationId 匹配)。 */
|
|
1956
1649
|
emitRunEvent(ev) {
|
|
1957
1650
|
if (ev.type === "run_end" && (ev.conversationId || ev.runId)) {
|
|
1958
1651
|
for (const w of this.chatWaiters) {
|
|
@@ -1981,8 +1674,6 @@ export class PluginManager {
|
|
|
1981
1674
|
}
|
|
1982
1675
|
}
|
|
1983
1676
|
}
|
|
1984
|
-
/** 某插件的 AI 工具展示快照(设置面板用;按工具名稳定排序;无工具回 [])。
|
|
1985
|
-
* 纯展示字段,不含 execute。 */
|
|
1986
1677
|
agentToolsSnapshot(pluginId) {
|
|
1987
1678
|
const table = this.agentTools.get(pluginId);
|
|
1988
1679
|
if (!table)
|
|
@@ -1995,7 +1686,6 @@ export class PluginManager {
|
|
|
1995
1686
|
}))
|
|
1996
1687
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
1997
1688
|
}
|
|
1998
|
-
/** 全部分组快照(设置面板“按插件分组”展示用;无工具的插件不出现;按插件 id 排序)。 */
|
|
1999
1689
|
getAgentToolsGrouped() {
|
|
2000
1690
|
const out = [];
|
|
2001
1691
|
for (const pid of [...this.agentTools.keys()].sort()) {
|
|
@@ -2005,7 +1695,6 @@ export class PluginManager {
|
|
|
2005
1695
|
}
|
|
2006
1696
|
return out;
|
|
2007
1697
|
}
|
|
2008
|
-
/** 当前全部插件注册的 AI 工具(扁平化,按插件 id 稳定排序,附带所属 pluginId)。 */
|
|
2009
1698
|
getAgentTools() {
|
|
2010
1699
|
const out = [];
|
|
2011
1700
|
for (const [pid, table] of [...this.agentTools.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
@@ -2015,10 +1704,6 @@ export class PluginManager {
|
|
|
2015
1704
|
}
|
|
2016
1705
|
return out;
|
|
2017
1706
|
}
|
|
2018
|
-
/** 注册一个供 AI 调用的工具;重名拒绝并返回空操作注销函数。
|
|
2019
|
-
* 跨插件重名同样拒绝(与 registerCommand 同口径):工具进会话时按名字合入
|
|
2020
|
-
* (syncPluginToolsIntoSession 的 byName 覆盖),重名意味着后注册者静默抢注
|
|
2021
|
-
* 前者的工具,必须在这里挡下。 */
|
|
2022
1707
|
registerAgentTool(pluginId, tool) {
|
|
2023
1708
|
if (!tool || typeof tool.execute !== "function" || !tool.name || !tool.description) {
|
|
2024
1709
|
console.error(`[plugin:${pluginId}] registerAgentTool: 缺少 name/description/execute,忽略`);
|
|
@@ -2055,13 +1740,10 @@ export class PluginManager {
|
|
|
2055
1740
|
try {
|
|
2056
1741
|
this.onAgentToolsChanged?.();
|
|
2057
1742
|
}
|
|
2058
|
-
catch {
|
|
2059
|
-
/* shutting down */
|
|
2060
|
-
}
|
|
1743
|
+
catch { }
|
|
2061
1744
|
}
|
|
2062
1745
|
};
|
|
2063
1746
|
}
|
|
2064
|
-
/** 注册内联标记(Inline Marker):跨插件重名拒绝,系统内置标记冲突拒绝。 */
|
|
2065
1747
|
registerMarker(pluginId, marker) {
|
|
2066
1748
|
if (!marker || typeof marker !== "object") {
|
|
2067
1749
|
console.error(`[plugin:${pluginId}] registerMarker: 缺少 marker 参数,忽略`);
|
|
@@ -2131,15 +1813,12 @@ export class PluginManager {
|
|
|
2131
1813
|
try {
|
|
2132
1814
|
this.onMarkersChanged?.();
|
|
2133
1815
|
}
|
|
2134
|
-
catch {
|
|
2135
|
-
/* shutting down */
|
|
2136
|
-
}
|
|
1816
|
+
catch { }
|
|
2137
1817
|
}
|
|
2138
1818
|
};
|
|
2139
1819
|
}
|
|
2140
|
-
/** 注册斜杠命令:跨插件重名拒绝(先注册者胜出),onCommandsChanged 通知目录刷新。 */
|
|
2141
1820
|
registerCommand(pluginId, cmd) {
|
|
2142
|
-
const name = String(cmd?.name ?? "").replace(/^\/+/, "");
|
|
1821
|
+
const name = String(cmd?.name ?? "").replace(/^\/+/, "");
|
|
2143
1822
|
if (!/^[a-zA-Z][a-zA-Z0-9:_-]*$/.test(name)) {
|
|
2144
1823
|
console.error(`[plugin:${pluginId}] registerCommand: 非法名称「${cmd?.name}」(需字母开头,允许字母数字:_-),忽略`);
|
|
2145
1824
|
this.pushRuntimeDiag(pluginId, `registerCommand: invalid name "${String(cmd?.name ?? "").slice(0, 32)}", ignored`);
|
|
@@ -2181,28 +1860,21 @@ export class PluginManager {
|
|
|
2181
1860
|
try {
|
|
2182
1861
|
this.onCommandsChanged?.();
|
|
2183
1862
|
}
|
|
2184
|
-
catch {
|
|
2185
|
-
/* shutting down */
|
|
2186
|
-
}
|
|
1863
|
+
catch { }
|
|
2187
1864
|
}
|
|
2188
1865
|
};
|
|
2189
1866
|
}
|
|
2190
|
-
/** 取(或建)某插件的运行时 UI 状态。 */
|
|
2191
1867
|
uiRuntimeFor(pluginId) {
|
|
2192
1868
|
let rt = this.uiRuntime.get(pluginId);
|
|
2193
1869
|
if (!rt)
|
|
2194
1870
|
this.uiRuntime.set(pluginId, (rt = { items: new Map(), removed: new Set(), arrange: [] }));
|
|
2195
1871
|
return rt;
|
|
2196
1872
|
}
|
|
2197
|
-
/** 移除一个条目(运行时注册的或 manifest 声明的都记进 removed,保证合并时不复活)。 */
|
|
2198
1873
|
removeUiItem(pluginId, itemId) {
|
|
2199
1874
|
const rt = this.uiRuntimeFor(pluginId);
|
|
2200
1875
|
rt.items.delete(itemId);
|
|
2201
1876
|
rt.removed.add(itemId);
|
|
2202
1877
|
}
|
|
2203
|
-
/** 该绝对路径是否落在当前工作区(或其额外根)内:工作区内的路径本来就能访问,
|
|
2204
|
-
* 不必走授权。多根语义见 protocol 的 set_workspace_roots —— 用户把一个目录加成
|
|
2205
|
-
* 工作区根,就是「我认它是我工作区的一部分」,插件读它无需再问。 */
|
|
2206
1878
|
isInsideWorkspace(abs) {
|
|
2207
1879
|
for (const root of [this.cwdValue, ...this.workspaceRoots]) {
|
|
2208
1880
|
const rel = relative(root, abs);
|
|
@@ -2211,12 +1883,9 @@ export class PluginManager {
|
|
|
2211
1883
|
}
|
|
2212
1884
|
return false;
|
|
2213
1885
|
}
|
|
2214
|
-
/** 某插件当前生效的 UI 贡献 = manifest 基线 + 运行时注册(同 id 覆盖、removed 删除)。 */
|
|
2215
1886
|
uiOf(pluginId) {
|
|
2216
1887
|
return mergeUiPluginUi(this.uiBase.get(pluginId), this.uiRuntime.get(pluginId));
|
|
2217
1888
|
}
|
|
2218
|
-
/** 追加一条运行时诊断(去重 + 100 条封顶),并同步到已加载条目的 info 快照。
|
|
2219
|
-
* 去重是因为门控拒绝(canUse)会在每次调用时触发,重复调用不应刷屏。 */
|
|
2220
1889
|
pushRuntimeDiag(pluginId, msg) {
|
|
2221
1890
|
if (!ID_RE.test(pluginId))
|
|
2222
1891
|
return;
|
|
@@ -2236,8 +1905,6 @@ export class PluginManager {
|
|
|
2236
1905
|
delete p.info.diagnostics;
|
|
2237
1906
|
}
|
|
2238
1907
|
}
|
|
2239
|
-
/** 合并某插件的诊断(manifest 解析 + 运行时),无诊断返回 undefined。
|
|
2240
|
-
* 返回的是快照拷贝,调用方可直接挂到 UiPluginInfo 上。 */
|
|
2241
1908
|
diagnosticsOf(pluginId) {
|
|
2242
1909
|
const m = this.manifestDiags.get(pluginId) ?? [];
|
|
2243
1910
|
const r = this.runtimeDiags.get(pluginId) ?? [];
|
|
@@ -2245,7 +1912,6 @@ export class PluginManager {
|
|
|
2245
1912
|
return undefined;
|
|
2246
1913
|
return [...m, ...r].slice(0, 100);
|
|
2247
1914
|
}
|
|
2248
|
-
/** 追加一条运行时日志(封顶丢弃最旧的;文本截断封顶)。 */
|
|
2249
1915
|
appendPluginLog(pluginId, level, text) {
|
|
2250
1916
|
if (!ID_RE.test(pluginId))
|
|
2251
1917
|
return;
|
|
@@ -2260,11 +1926,9 @@ export class PluginManager {
|
|
|
2260
1926
|
if (arr.length > PLUGIN_LOG_CAP)
|
|
2261
1927
|
arr.splice(0, arr.length - PLUGIN_LOG_CAP);
|
|
2262
1928
|
}
|
|
2263
|
-
/** 读某插件的运行时日志(快照拷贝;未知插件回 [])。 */
|
|
2264
1929
|
getPluginLogs(pluginId) {
|
|
2265
1930
|
return (this.pluginLogs.get(pluginId) ?? []).map((e) => ({ ...e }));
|
|
2266
1931
|
}
|
|
2267
|
-
/** 清空某插件的运行时日志(设置面板“清空”按钮用)。 */
|
|
2268
1932
|
clearPluginLogs(pluginId) {
|
|
2269
1933
|
if (!ID_RE.test(pluginId))
|
|
2270
1934
|
return;
|
|
@@ -2275,12 +1939,9 @@ export class PluginManager {
|
|
|
2275
1939
|
try {
|
|
2276
1940
|
s.send(msg);
|
|
2277
1941
|
}
|
|
2278
|
-
catch {
|
|
2279
|
-
/* dead socket — index.ts cleans it up */
|
|
2280
|
-
}
|
|
1942
|
+
catch { }
|
|
2281
1943
|
}
|
|
2282
1944
|
}
|
|
2283
|
-
/** P3:节流广播左栏运行条目(同一窗口内的多次变更合并成一条,约 500ms)。 */
|
|
2284
1945
|
schedulePanelPush() {
|
|
2285
1946
|
if (this.panelPushTimer)
|
|
2286
1947
|
return;
|
|
@@ -2290,7 +1951,6 @@ export class PluginManager {
|
|
|
2290
1951
|
}, 500);
|
|
2291
1952
|
this.panelPushTimer.unref?.();
|
|
2292
1953
|
}
|
|
2293
|
-
/** P3:心跳扫描(有条目才跑,每 5 秒):过期即清空并广播;条目全无则停表。 */
|
|
2294
1954
|
ensurePanelSweep() {
|
|
2295
1955
|
if (this.panelSweepTimer)
|
|
2296
1956
|
return;
|
|
@@ -2305,16 +1965,12 @@ export class PluginManager {
|
|
|
2305
1965
|
}, 5000);
|
|
2306
1966
|
this.panelSweepTimer.unref?.();
|
|
2307
1967
|
}
|
|
2308
|
-
/** P3:attach 时补发的全量(index.ts 用)。 */
|
|
2309
1968
|
panelGroups() {
|
|
2310
1969
|
return this.panelRunning.groups();
|
|
2311
1970
|
}
|
|
2312
|
-
/** 当前目录清单(重扫 manifest,不重新 import)。 */
|
|
2313
1971
|
async list(lang) {
|
|
2314
1972
|
return this.scan(lang);
|
|
2315
1973
|
}
|
|
2316
|
-
/** 机器可读的注册面目录(P2-7):slot 别名/kind/例子 + 工具目录 +
|
|
2317
|
-
* 宿主方法表 + 当前占用者(现算)。只读装配,无副作用。 */
|
|
2318
1974
|
getApiCatalog() {
|
|
2319
1975
|
const counts = new Map();
|
|
2320
1976
|
const add = (pluginId, slot) => {
|
|
@@ -2347,10 +2003,6 @@ export class PluginManager {
|
|
|
2347
2003
|
},
|
|
2348
2004
|
});
|
|
2349
2005
|
}
|
|
2350
|
-
/**
|
|
2351
|
-
* attach 时调用:重扫目录 + 激活尚未加载的新插件。
|
|
2352
|
-
* 返回给浏览器的目录(含激活失败的条目,前端显示为不可用)。
|
|
2353
|
-
*/
|
|
2354
2006
|
ensureLoaded(lang) {
|
|
2355
2007
|
if (this.loading)
|
|
2356
2008
|
return this.loading;
|
|
@@ -2368,8 +2020,6 @@ export class PluginManager {
|
|
|
2368
2020
|
return [];
|
|
2369
2021
|
const byId = new Map(found.map((f) => [f.id, f]));
|
|
2370
2022
|
const scannedIds = new Set(byId.keys());
|
|
2371
|
-
// P2-8:依赖拓扑排序激活(被依赖者先行;决定启动时机的是依赖,不是目录顺序)。
|
|
2372
|
-
// 边只连本次扫描里存在的对端;缺失的对端由 activate 点名拒绝。环上节点直接拒。
|
|
2373
2023
|
const { order, cyclic } = sortByRequires(found.map((f) => f.id), (id) => (byId.get(id)?.requires?.plugins ?? []).filter((d) => scannedIds.has(d)));
|
|
2374
2024
|
for (const cycId of cyclic) {
|
|
2375
2025
|
const info = byId.get(cycId);
|
|
@@ -2383,24 +2033,20 @@ export class PluginManager {
|
|
|
2383
2033
|
if (this.loaded.has(info.id) || this.attempted.has(info.id))
|
|
2384
2034
|
continue;
|
|
2385
2035
|
if (!existsSync(join(this.pluginsDir, info.id, "index.mjs")))
|
|
2386
|
-
continue;
|
|
2036
|
+
continue;
|
|
2387
2037
|
await this.activate(info, lang);
|
|
2388
2038
|
if (generation !== this.lifecycleGeneration)
|
|
2389
2039
|
return [];
|
|
2390
2040
|
}
|
|
2391
|
-
// 已被删除的插件:调用 deactivate 并移出缓存
|
|
2392
2041
|
// eslint-disable-next-line unicorn/no-useless-spread -- snapshot: handlers may unsubscribe mid-emit
|
|
2393
2042
|
for (const [id, p] of [...this.loaded]) {
|
|
2394
2043
|
if (!found.some((f) => f.id === id)) {
|
|
2395
2044
|
this.deactivateEntry(id, p);
|
|
2396
2045
|
}
|
|
2397
2046
|
}
|
|
2398
|
-
// P2-8 级联:提供方刚坏(被删/失败/被拒)→ 仍在跑的消费方一并反激活 + 教学占位。
|
|
2399
2047
|
this.cascadeRequiresRefusals(found, lang);
|
|
2400
2048
|
return found.map((f) => {
|
|
2401
2049
|
const base = this.loaded.get(f.id)?.info ?? f;
|
|
2402
|
-
// 运行相位(设置面板清单用):宿主持有实例即 active(含激活失败的占位行;
|
|
2403
|
-
// 纯前端插件无 index.mjs、从未进 loaded 表,保持非 active)。
|
|
2404
2050
|
const withPhase = { ...base, active: this.loaded.has(f.id) };
|
|
2405
2051
|
const agentTools = this.agentToolsSnapshot(f.id);
|
|
2406
2052
|
if (agentTools.length)
|
|
@@ -2409,24 +2055,14 @@ export class PluginManager {
|
|
|
2409
2055
|
return rest;
|
|
2410
2056
|
});
|
|
2411
2057
|
}
|
|
2412
|
-
/** 反激活清理:把该插件名下全部订阅/注册一次收完。
|
|
2413
|
-
*
|
|
2414
|
-
* 两段:① 副作用栈逆序回卷(插件注册的全部可逆副作用都在里面 —— 工具/命令/路由/
|
|
2415
|
-
* 代理/watch/定时/后台任务/事件订阅/UI 条目…;跑不干净的记一条诊断);
|
|
2416
|
-
* ② 兜底回收全局表里按 pluginId 索引的残留(老宿主激活的实例没有栈、或
|
|
2417
|
-
* 插件绕过 host 自己往表里塞过东西:代理前缀按 id 过滤清一遍,幂等)。 */
|
|
2418
2058
|
releaseEntry(p) {
|
|
2419
|
-
// ① 逆序回卷。失败项已在栈内打过诊断,这里不再重复。
|
|
2420
2059
|
const failed = p.effects?.release() ?? [];
|
|
2421
2060
|
if (failed.length > 0)
|
|
2422
2061
|
console.warn(`[plugin:${p.info.id}] ${failed.length} effect(s) failed to clean up: ${failed.join(", ")}`);
|
|
2423
|
-
// ② 兜底:代理前缀随反激活一起回收(全局表按 pluginId 过滤;
|
|
2424
|
-
// Map 迭代中删除是良定义的:删过的条目不会再被访问到)。
|
|
2425
2062
|
for (const [prefix, hit] of this.proxyRoutes) {
|
|
2426
2063
|
if (hit.pluginId === p.info.id)
|
|
2427
2064
|
this.proxyRoutes.delete(prefix);
|
|
2428
2065
|
}
|
|
2429
|
-
// ③ 兜底:回收该插件发布的服务
|
|
2430
2066
|
for (const [name, hit] of this.pluginServices) {
|
|
2431
2067
|
if (hit.pluginId === p.info.id) {
|
|
2432
2068
|
this.pluginServices.delete(name);
|
|
@@ -2442,7 +2078,6 @@ export class PluginManager {
|
|
|
2442
2078
|
}
|
|
2443
2079
|
}
|
|
2444
2080
|
}
|
|
2445
|
-
/** 插件可返回 async cleanup:观察拒绝,避免旧代清理变成未处理的 rejection。 */
|
|
2446
2081
|
runDeactivate(id, deactivate) {
|
|
2447
2082
|
try {
|
|
2448
2083
|
void Promise.resolve(deactivate?.()).catch((err) => {
|
|
@@ -2453,34 +2088,20 @@ export class PluginManager {
|
|
|
2453
2088
|
console.error(`[plugin:${id}] deactivate failed:`, err);
|
|
2454
2089
|
}
|
|
2455
2090
|
}
|
|
2456
|
-
/** 反激活单个插件:deactivate + 注销 AI 工具 + 清缓存。
|
|
2457
|
-
*
|
|
2458
|
-
* 注意这里必须把 id 从 attempted 里摘掉:目录一时不在(`pi-web-ui install --force`
|
|
2459
|
-
* 先 rm 再 cp,扫描正好撞上窗口期)只是「暂时看成卸载」,目录回来后还要能重新激活;
|
|
2460
|
-
* 留在 attempted 里 = 本进程内永远不再激活,插件的 HTTP 路由 / AI 工具全没了,
|
|
2461
|
-
* 前端只会看到「代理请求失败 404 <url>」(插件的 /proxy 路由不存在),且 CLI 承诺的
|
|
2462
|
-
* 「刷新浏览器即可加载」失效,必须重启服务才能恢复。 */
|
|
2463
2091
|
deactivateEntry(id, p) {
|
|
2464
2092
|
this.runDeactivate(id, p.deactivate);
|
|
2465
2093
|
this.releaseEntry(p);
|
|
2466
2094
|
this.loaded.delete(id);
|
|
2467
2095
|
this.messageHandlers.delete(id);
|
|
2468
2096
|
this.attempted.delete(id);
|
|
2469
|
-
// 运行时 UI 注册随插件一起消失(manifest 基线留着,重扫时会重算)。
|
|
2470
2097
|
this.uiRuntime.delete(id);
|
|
2471
2098
|
this.uiBase.delete(id);
|
|
2472
|
-
// 诊断随插件一起清:目录回来重新激活时会重算(旧诊断留着会误导)。
|
|
2473
2099
|
this.manifestDiags.delete(id);
|
|
2474
2100
|
this.runtimeDiags.delete(id);
|
|
2475
|
-
// 运行时日志同样随插件走(内存缓冲,不落盘)。
|
|
2476
2101
|
this.pluginLogs.delete(id);
|
|
2477
|
-
// 重新激活时会 import 磁盘上的 index.mjs:Node 的 ESM 缓存按 URL(含 ?e=)
|
|
2478
|
-
// 命中,epoch 不变就会拿到旧模块(更新插件后还是旧代码)——所以这里也 +1,
|
|
2479
|
-
// 顺带让浏览器端 ?e= 变化、重拉插件的 client bundle。
|
|
2480
2102
|
this.epochCounter += 1;
|
|
2481
2103
|
console.log(`[plugin:${id}] removed`);
|
|
2482
2104
|
}
|
|
2483
|
-
/** 关机 / reload 时反激活全部插件(dispose 后 loaded 为空,后续 releaseEntry 是 no-op)。 */
|
|
2484
2105
|
dispose() {
|
|
2485
2106
|
this.lifecycleGeneration += 1;
|
|
2486
2107
|
this.loading = undefined;
|
|
@@ -2495,29 +2116,22 @@ export class PluginManager {
|
|
|
2495
2116
|
pluginIds.add(id);
|
|
2496
2117
|
this.runDeactivate(id, p.deactivate);
|
|
2497
2118
|
}
|
|
2498
|
-
// ① 先回卷全部 effect 栈(含 bgTask 停表、工具/命令/路由/watch/UI 条目注销),
|
|
2499
|
-
// 并把 loaded 清掉 —— 之后逐个走 releaseEntry 的兜底回收。
|
|
2500
2119
|
for (const [id, p] of this.loaded) {
|
|
2501
2120
|
p.effects?.release();
|
|
2502
2121
|
this.loaded.delete(id);
|
|
2503
2122
|
}
|
|
2504
|
-
// ② 兜底:停掉任何没经 effect 栈登记的常驻后台任务,不留孤儿计时器。
|
|
2505
2123
|
for (const id of pluginIds) {
|
|
2506
2124
|
for (const t of this.pluginBgTasks.get(id)?.values() ?? []) {
|
|
2507
2125
|
try {
|
|
2508
2126
|
t.stop?.();
|
|
2509
2127
|
}
|
|
2510
|
-
catch {
|
|
2511
|
-
// best-effort:反激活清理,单个任务失败不阻断。
|
|
2512
|
-
}
|
|
2128
|
+
catch { }
|
|
2513
2129
|
}
|
|
2514
2130
|
}
|
|
2515
|
-
// ③ 兜底:清掉插件名下的代理前缀(该插件名下但未经栈登记的残留)。
|
|
2516
2131
|
for (const prefix of this.proxyRoutes.keys()) {
|
|
2517
2132
|
if (pluginIds.has(this.proxyRoutes.get(prefix).pluginId))
|
|
2518
2133
|
this.proxyRoutes.delete(prefix);
|
|
2519
2134
|
}
|
|
2520
|
-
// ④ 兜底:清理插件间服务
|
|
2521
2135
|
this.pluginServices.clear();
|
|
2522
2136
|
this.serviceListeners.clear();
|
|
2523
2137
|
if (this.panelPushTimer) {
|
|
@@ -2537,31 +2151,31 @@ export class PluginManager {
|
|
|
2537
2151
|
this.pluginBgTasks.clear();
|
|
2538
2152
|
this.loaded.clear();
|
|
2539
2153
|
this.messageHandlers.clear();
|
|
2540
|
-
// reload() 走 dispose→ensureLoaded:诊断清掉重算,不跨代累积。
|
|
2541
2154
|
this.manifestDiags.clear();
|
|
2542
2155
|
this.runtimeDiags.clear();
|
|
2543
2156
|
this.pluginLogs.clear();
|
|
2544
2157
|
}
|
|
2545
|
-
/**
|
|
2546
|
-
* 检查随包内置官方插件(<pkgRoot>/plugins/)与用户数据目录(<dataDir>/plugins/)的版本。
|
|
2547
|
-
* 若用户已安装的官方插件版本落后于当前随包版本,自动备份旧版本并增量同步更新代码(保留用户数据/设置)。
|
|
2548
|
-
*/
|
|
2549
2158
|
syncBuiltinPlugins() {
|
|
2550
2159
|
if (!this.builtinCatalogPath || !existsSync(this.builtinCatalogPath))
|
|
2551
2160
|
return;
|
|
2552
2161
|
const builtinDir = dirname(this.builtinCatalogPath);
|
|
2553
2162
|
if (!existsSync(builtinDir))
|
|
2554
2163
|
return;
|
|
2555
|
-
// 确保数据目录下同步最新 plugin-sdk(供官方与自定义插件共享纯 ESM 工具层)
|
|
2556
2164
|
const builtinSdkDir = join(dirname(builtinDir), "plugin-sdk");
|
|
2557
2165
|
const targetSdkDir = join(this.dataDir, "plugin-sdk");
|
|
2558
2166
|
if (existsSync(builtinSdkDir)) {
|
|
2559
2167
|
try {
|
|
2560
2168
|
cpSync(builtinSdkDir, targetSdkDir, { recursive: true });
|
|
2561
2169
|
}
|
|
2562
|
-
catch {
|
|
2563
|
-
|
|
2564
|
-
|
|
2170
|
+
catch { }
|
|
2171
|
+
}
|
|
2172
|
+
try {
|
|
2173
|
+
prepareBundledPlugins(this.dataDir, this.builtinCatalogPath, new Set(this.catalog()
|
|
2174
|
+
.filter((entry) => entry.origin === "custom")
|
|
2175
|
+
.map((entry) => entry.id)));
|
|
2176
|
+
}
|
|
2177
|
+
catch (err) {
|
|
2178
|
+
console.warn("[plugins] Preparing bundled modules failed:", err);
|
|
2565
2179
|
}
|
|
2566
2180
|
let names;
|
|
2567
2181
|
try {
|
|
@@ -2570,7 +2184,6 @@ export class PluginManager {
|
|
|
2570
2184
|
catch {
|
|
2571
2185
|
return;
|
|
2572
2186
|
}
|
|
2573
|
-
// 只有在官方 catalog.json 中登记的官方插件,才属于随宿主自动同步维护的对象(避免误覆盖测试或非 catalog 目录)
|
|
2574
2187
|
let builtinCatalogIds;
|
|
2575
2188
|
try {
|
|
2576
2189
|
const catalogEntries = JSON.parse(readFileSync(this.builtinCatalogPath, "utf8"));
|
|
@@ -2586,13 +2199,10 @@ export class PluginManager {
|
|
|
2586
2199
|
const sourceDir = join(builtinDir, name);
|
|
2587
2200
|
try {
|
|
2588
2201
|
if (lstatSync(targetDir).isSymbolicLink()) {
|
|
2589
|
-
// 处于源码开发软链接模式(如 npm run plugins:link),直接使用源码,跳过随包文件覆盖
|
|
2590
2202
|
continue;
|
|
2591
2203
|
}
|
|
2592
2204
|
}
|
|
2593
|
-
catch {
|
|
2594
|
-
/* lstat 失败走普通流程 */
|
|
2595
|
-
}
|
|
2205
|
+
catch { }
|
|
2596
2206
|
const targetManifest = join(targetDir, "manifest.json");
|
|
2597
2207
|
const sourceManifest = join(sourceDir, "manifest.json");
|
|
2598
2208
|
if (!existsSync(sourceDir) || !existsSync(sourceManifest) || !existsSync(targetManifest)) {
|
|
@@ -2617,7 +2227,6 @@ export class PluginManager {
|
|
|
2617
2227
|
}
|
|
2618
2228
|
}
|
|
2619
2229
|
}
|
|
2620
|
-
/** 读 manifest 清单;坏目录(无 manifest/id 非法)直接跳过。 */
|
|
2621
2230
|
async scan(lang) {
|
|
2622
2231
|
const generation = this.lifecycleGeneration;
|
|
2623
2232
|
this.syncBuiltinPlugins();
|
|
@@ -2629,7 +2238,7 @@ export class PluginManager {
|
|
|
2629
2238
|
return [];
|
|
2630
2239
|
}
|
|
2631
2240
|
catch {
|
|
2632
|
-
return [];
|
|
2241
|
+
return [];
|
|
2633
2242
|
}
|
|
2634
2243
|
const out = [];
|
|
2635
2244
|
for (const name of names.sort()) {
|
|
@@ -2644,20 +2253,16 @@ export class PluginManager {
|
|
|
2644
2253
|
continue;
|
|
2645
2254
|
}
|
|
2646
2255
|
catch {
|
|
2647
|
-
// 目录不存在(如失效的软链接)或非目录/不可访问,跳过
|
|
2648
2256
|
continue;
|
|
2649
2257
|
}
|
|
2650
2258
|
try {
|
|
2651
2259
|
const raw = await readFile(join(dir, "manifest.json"), "utf8");
|
|
2652
2260
|
if (generation !== this.lifecycleGeneration)
|
|
2653
2261
|
return [];
|
|
2654
|
-
// wantsDom 快照供静态门禁用(manifest 坏了 → JSON.parse 抛 → catch 跳过,旧快照残留无害:门禁只拦「快照里要且没授权」的)。
|
|
2655
2262
|
const m = JSON.parse(raw);
|
|
2656
2263
|
const wantsDom = declarationWantsDom(m.permissions);
|
|
2657
2264
|
this.domWants.set(name, wantsDom);
|
|
2658
|
-
// 本轮 manifest 解析的诊断(重算覆盖;运行时诊断另存在 runtimeDiags)。
|
|
2659
2265
|
const uiDiags = [];
|
|
2660
|
-
// P1-6:manifest 本体先校验,失败即拒(坏配置不进运行时)。
|
|
2661
2266
|
const mv = validatePluginManifest(m, name);
|
|
2662
2267
|
for (const w of mv.warnings)
|
|
2663
2268
|
uiDiags.push(formatManifestIssue(w));
|
|
@@ -2665,7 +2270,7 @@ export class PluginManager {
|
|
|
2665
2270
|
for (const e of mv.errors)
|
|
2666
2271
|
uiDiags.push(formatManifestIssue(e));
|
|
2667
2272
|
const first = mv.errors[0];
|
|
2668
|
-
const msg = pick(l, `manifest 校验失败:${first.message}(${name}/)—— 修复后点“重新扫描”`, `manifest validation failed: ${first.messageEn} (${name}/) — fix it, then hit Rescan`, "plugins.manifest.invalid", { name, path: first.path });
|
|
2273
|
+
const msg = pick(l, `manifest 校验失败:${first.message}(${name}/)—— 修复后点“重新扫描”`, `manifest validation failed: ${first.messageEn} (${name}/) — fix it, then hit Rescan`, "plugins.manifest.invalid", { name, path: first.path, message: first.messageEn });
|
|
2669
2274
|
console.error(`[plugin:${name}] ${msg}`);
|
|
2670
2275
|
this.manifestDiags.set(name, uiDiags);
|
|
2671
2276
|
const refused = [...uiDiags, ...(this.runtimeDiags.get(name) ?? [])].slice(0, 100);
|
|
@@ -2686,8 +2291,6 @@ export class PluginManager {
|
|
|
2686
2291
|
}
|
|
2687
2292
|
continue;
|
|
2688
2293
|
}
|
|
2689
|
-
// P2-9:声明式设置的三层合并(schema 默认 < 用户 overlay < 面板保存值)。
|
|
2690
|
-
// overlay 先读先洗,警告进诊断(英文短句,随清单下发);secret 永不来自 overlay。
|
|
2691
2294
|
const settingsSchemaForScan = parseSettingsSchema(m.settings);
|
|
2692
2295
|
const overlayFileForScan = readSettingsOverlayFile(this.dataDir, name);
|
|
2693
2296
|
const overlayForScan = cleanOverlaySettings(settingsSchemaForScan, overlayFileForScan.raw, "en");
|
|
@@ -2705,33 +2308,24 @@ export class PluginManager {
|
|
|
2705
2308
|
iconSvg: normalizeIconSvg(m.iconSvg),
|
|
2706
2309
|
hasClient,
|
|
2707
2310
|
error: this.loaded.get(name)?.info.error,
|
|
2708
|
-
// manifest 声明的能力清单(fs/net/tools…)——设置面板展示用
|
|
2709
2311
|
permissions: Array.isArray(m.permissions)
|
|
2710
2312
|
? m.permissions.filter((p) => typeof p === "string" && p.length > 0).slice(0, 16)
|
|
2711
2313
|
: undefined,
|
|
2712
|
-
// 出站网络白名单(permissions 含 "net" 时生效;空/缺省 = 全拒,fail-closed)。
|
|
2713
2314
|
netAllowlist: Array.isArray(m.netAllowlist)
|
|
2714
2315
|
? m.netAllowlist
|
|
2715
2316
|
.filter((x) => typeof x === "string" && x.trim().length > 0)
|
|
2716
2317
|
.map((x) => x.trim().toLowerCase())
|
|
2717
2318
|
.slice(0, 32)
|
|
2718
2319
|
: undefined,
|
|
2719
|
-
// 引擎约束({ "pi-web-ui": ">=x.y.z" },不满足即拒绝激活,见 activate)。
|
|
2720
2320
|
engines: m.engines && typeof m.engines === "object" && !Array.isArray(m.engines)
|
|
2721
2321
|
? Object.fromEntries(Object.entries(m.engines)
|
|
2722
2322
|
.filter(([, v]) => typeof v === "string")
|
|
2723
2323
|
.slice(0, 8))
|
|
2724
2324
|
: undefined,
|
|
2725
|
-
// 可选对等依赖(其它插件 id,缺失只警告不断活,见 activate)。
|
|
2726
2325
|
peerPlugins: Array.isArray(m.peerPlugins)
|
|
2727
2326
|
? m.peerPlugins.filter((x) => typeof x === "string" && ID_RE.test(x)).slice(0, 16)
|
|
2728
2327
|
: undefined,
|
|
2729
|
-
// 硬依赖声明(manifest "requires",P2-8):形状坏的在 P1-6 校验层已拒,
|
|
2730
|
-
// 这里只做宽容解析供展示 + activate 语义判定(缺失/失败/环/版本/能力即拒)。
|
|
2731
2328
|
requires: parseRequires(m.requires),
|
|
2732
|
-
// 特权 DOM:permissions 含 dom 族 → bundle 默认 403,需用户逐个授权。
|
|
2733
|
-
// wantsDom 缓进 domWants(静态门禁查它,不必每次 readdir);error 在未授权时
|
|
2734
|
-
// 直接写明原因(tab 置灰 + 设置行提示),授权后下次 scan 自动清除。
|
|
2735
2329
|
...(wantsDom
|
|
2736
2330
|
? {
|
|
2737
2331
|
wantsDom: true,
|
|
@@ -2743,16 +2337,13 @@ export class PluginManager {
|
|
|
2743
2337
|
}),
|
|
2744
2338
|
}
|
|
2745
2339
|
: {}),
|
|
2746
|
-
// 声明式设置 schema + 当前存值(⚙ 面板自动渲染表单用)
|
|
2747
2340
|
settingsSchema: settingsSchemaForScan,
|
|
2748
2341
|
settingsValues: resolvedSettingsForScan.values,
|
|
2749
2342
|
settingsSources: resolvedSettingsForScan.sources,
|
|
2750
2343
|
isolated: m.isolated === true || m.shadow === true ? true : undefined,
|
|
2751
|
-
// 可渲染的 fenced-code 语言(manifest "renderers")——前端据此按需加载
|
|
2752
2344
|
renderers: Array.isArray(m.renderers)
|
|
2753
2345
|
? m.renderers.filter((r) => typeof r === "string" && r.length > 0).slice(0, 32)
|
|
2754
2346
|
: undefined,
|
|
2755
|
-
// 自定义消息部件 / 附件卡 / 输入框补全源(与 renderers 同一过滤口径)
|
|
2756
2347
|
messageWidgets: Array.isArray(m.messageWidgets)
|
|
2757
2348
|
? m.messageWidgets.filter((r) => typeof r === "string" && r.length > 0).slice(0, 32)
|
|
2758
2349
|
: undefined,
|
|
@@ -2762,8 +2353,6 @@ export class PluginManager {
|
|
|
2762
2353
|
composerProviders: Array.isArray(m.composerProviders)
|
|
2763
2354
|
? m.composerProviders.filter((r) => typeof r === "string" && r.length > 0).slice(0, 32)
|
|
2764
2355
|
: undefined,
|
|
2765
|
-
// 文件单击查看器声明:只下发扩展名/处理器 id/优先级,实际 DOM
|
|
2766
|
-
// 渲染仍由客户端 bundle 提供,避免 manifest 携带可执行内容。
|
|
2767
2356
|
fileHandlers: (() => {
|
|
2768
2357
|
if (!Array.isArray(m.fileHandlers))
|
|
2769
2358
|
return undefined;
|
|
@@ -2795,14 +2384,8 @@ export class PluginManager {
|
|
|
2795
2384
|
.filter((x) => x !== null);
|
|
2796
2385
|
return rows.length ? rows : undefined;
|
|
2797
2386
|
})(),
|
|
2798
|
-
// 是否有独立视图 tab(manifest "view",缺省为是否有客户端 entry.mjs);纯 renderer 插件写 false
|
|
2799
2387
|
view: typeof m.view === "boolean" ? m.view : hasClient,
|
|
2800
|
-
// 客户端 bundle 是否常驻加载(manifest "preload",缺省 false):无视图
|
|
2801
|
-
// (view:false)却要顶层代码一直跑(提醒轮询/快捷键/常驻浮窗…)的插件用它。
|
|
2802
2388
|
preload: m.preload === true,
|
|
2803
|
-
// 插件对宿主 UI 的贡献(manifest "ui":slot 框架 + 整理意图,issue #146)。
|
|
2804
|
-
// 权限:与 activate 的 can("ui") **同一口径**(严格模式 = 声明了 permissions
|
|
2805
|
-
// 或 apiVersion>=2):严格模式下必须含 "ui" 族,否则整份忽略;旧全权格式放行。
|
|
2806
2389
|
ui: (() => {
|
|
2807
2390
|
const perms = Array.isArray(m.permissions)
|
|
2808
2391
|
? m.permissions.filter((x) => typeof x === "string")
|
|
@@ -2822,8 +2405,6 @@ export class PluginManager {
|
|
|
2822
2405
|
this.uiBase.delete(name);
|
|
2823
2406
|
return this.uiOf(name);
|
|
2824
2407
|
})(),
|
|
2825
|
-
// 安装来源(pi-web-ui install 写入的 .pi-source.json)——
|
|
2826
|
-
// 设置面板据此显示「更新」按钮;手工拷入的插件没有此文件。
|
|
2827
2408
|
source: await readFile(join(dir, ".pi-source.json"), "utf8")
|
|
2828
2409
|
.then((raw) => {
|
|
2829
2410
|
try {
|
|
@@ -2838,7 +2419,6 @@ export class PluginManager {
|
|
|
2838
2419
|
});
|
|
2839
2420
|
if (generation !== this.lifecycleGeneration)
|
|
2840
2421
|
return [];
|
|
2841
|
-
// 诊断随清单下发:manifest 解析 + 到目前为止的运行时诊断。
|
|
2842
2422
|
this.manifestDiags.set(name, uiDiags);
|
|
2843
2423
|
const agentTools = this.agentToolsSnapshot(name);
|
|
2844
2424
|
if (agentTools.length)
|
|
@@ -2857,8 +2437,6 @@ export class PluginManager {
|
|
|
2857
2437
|
catch {
|
|
2858
2438
|
if (generation !== this.lifecycleGeneration)
|
|
2859
2439
|
return [];
|
|
2860
|
-
// 无 manifest.json / JSON 解析失败 —— 占位行展示(坏插件不跳过,
|
|
2861
|
-
// 设置面板清单标红 + 给原因;ensureLoaded 照例跳过激活)。
|
|
2862
2440
|
const msg = pick(l, `manifest.json 缺失或解析失败(${name}/),不是有效插件 —— 修复或移走该目录后点“重新扫描”`, `manifest.json missing or unparsable (${name}/), not a valid plugin — fix or remove the directory, then hit Rescan`, "plugins.manifest.broken", { name });
|
|
2863
2441
|
console.error(`[plugin:${name}] ${msg}`);
|
|
2864
2442
|
const brokenDiags = this.diagnosticsOf(name);
|
|
@@ -2872,7 +2450,6 @@ export class PluginManager {
|
|
|
2872
2450
|
});
|
|
2873
2451
|
}
|
|
2874
2452
|
}
|
|
2875
|
-
// 删掉的目录不同时清快照:门禁会把不存在的 id 当 403,正确的应该是 404。
|
|
2876
2453
|
for (const key of this.domWants.keys()) {
|
|
2877
2454
|
if (!out.some((p) => p.id === key))
|
|
2878
2455
|
this.domWants.delete(key);
|
|
@@ -2886,8 +2463,6 @@ export class PluginManager {
|
|
|
2886
2463
|
}
|
|
2887
2464
|
return out;
|
|
2888
2465
|
}
|
|
2889
|
-
/** 非 activate 上下文的拒绝占位(P2-8 环/级联):空集合 + error + 诊断,
|
|
2890
|
-
* 与各门控的占位行同形(ensureLoaded 照例跳过,reload 重算刷新)。 */
|
|
2891
2466
|
setFrozenRefusal(info, msg, diagEn) {
|
|
2892
2467
|
console.error(`[plugin:${info.id}] ${msg}`);
|
|
2893
2468
|
this.pushRuntimeDiag(info.id, diagEn);
|
|
@@ -2902,9 +2477,6 @@ export class PluginManager {
|
|
|
2902
2477
|
settingsHandlers: new Set(),
|
|
2903
2478
|
});
|
|
2904
2479
|
}
|
|
2905
|
-
/** 级联(P2-8):提供方刚坏掉(被删/失败/被拒)→ 仍在跑的消费方一并反激活 +
|
|
2906
|
-
* 留教学占位。循环直到一轮无变化(链式依赖一轮收敛),返回受影响的 id。
|
|
2907
|
-
* hostApi/families 运行时不变,这里只查 plugins 对端(目录在 + 后端 peer 激活成功)。 */
|
|
2908
2480
|
cascadeRequiresRefusals(found, lang) {
|
|
2909
2481
|
const l = lang?.() ?? "en";
|
|
2910
2482
|
const byId = new Map(found.map((f) => [f.id, f]));
|
|
@@ -2954,14 +2526,13 @@ export class PluginManager {
|
|
|
2954
2526
|
const l = lang?.() ?? "en";
|
|
2955
2527
|
this.attempted.add(info.id);
|
|
2956
2528
|
const dir = join(this.pluginsDir, info.id);
|
|
2957
|
-
// P1-6:manifest 本体失败即拒(scan 缓存可能过期,这里重读重判,半途注册的副作用不留)。
|
|
2958
2529
|
try {
|
|
2959
2530
|
const rawManifest = readFileSync(join(dir, "manifest.json"), "utf8");
|
|
2960
2531
|
const parsedManifest = JSON.parse(rawManifest);
|
|
2961
2532
|
const mvActivate = validatePluginManifest(parsedManifest, info.id);
|
|
2962
2533
|
if (mvActivate.errors.length > 0) {
|
|
2963
2534
|
const first = mvActivate.errors[0];
|
|
2964
|
-
const msg = pick(l, `manifest 校验失败:${first.message} —— 修复后点“重新扫描”`, `manifest validation failed: ${first.messageEn} — fix it, then hit Rescan`, "plugins.manifest.invalid", { name: info.id, path: first.path });
|
|
2535
|
+
const msg = pick(l, `manifest 校验失败:${first.message} —— 修复后点“重新扫描”`, `manifest validation failed: ${first.messageEn} — fix it, then hit Rescan`, "plugins.manifest.invalid", { name: info.id, path: first.path, message: first.messageEn });
|
|
2965
2536
|
console.error(`[plugin:${info.id}] ${msg}`);
|
|
2966
2537
|
for (const e of mvActivate.errors)
|
|
2967
2538
|
this.pushRuntimeDiag(info.id, formatManifestIssue(e));
|
|
@@ -2980,9 +2551,7 @@ export class PluginManager {
|
|
|
2980
2551
|
return;
|
|
2981
2552
|
}
|
|
2982
2553
|
}
|
|
2983
|
-
catch {
|
|
2984
|
-
// 读不到/解析失败 → 走原有的 broken 占位逻辑(scan 里已产出,不在这里重复拒绝)。
|
|
2985
|
-
}
|
|
2554
|
+
catch { }
|
|
2986
2555
|
const handlers = new Set();
|
|
2987
2556
|
this.messageHandlers.set(info.id, handlers);
|
|
2988
2557
|
const toolHandlers = new Set();
|
|
@@ -2997,25 +2566,17 @@ export class PluginManager {
|
|
|
2997
2566
|
const bgTaskTable = new Map();
|
|
2998
2567
|
let leftPanelOwned = false;
|
|
2999
2568
|
const settingsHandlers = new Set();
|
|
3000
|
-
// 可逆副作用栈:本插件的全部注册面(工具/命令/路由/代理/watch/定时/后台任务/
|
|
3001
|
-
// UI 条目/事件订阅…)都经 effects.add 登记,反激活时**逆序**回卷 —— 插件忘写
|
|
3002
|
-
// 注销也不会留下孤儿订阅/定时器/路由(热重载后事件双触发、定时器叠加、watcher
|
|
3003
|
-
// 堆积都是这个漏法的症状)。cleanup 抛错只记诊断,不阻断回卷。
|
|
3004
2569
|
const generation = this.lifecycleGeneration;
|
|
3005
2570
|
const effects = new PluginEffectStack(info.id, (m) => {
|
|
3006
2571
|
if (generation === this.lifecycleGeneration)
|
|
3007
2572
|
this.pushRuntimeDiag(info.id, m);
|
|
3008
2573
|
});
|
|
3009
2574
|
const isCurrent = () => generation === this.lifecycleGeneration && !effects.isReleased;
|
|
3010
|
-
// 宿主 API 版本协商:插件要的比宿主新 → 明确拒绝(而不是让它在运行期
|
|
3011
|
-
// 撞 undefined 接口莫名其妙地坏)。与激活失败同一处理:error 字段 + 置灰。
|
|
3012
2575
|
let apiVersion = 1;
|
|
3013
2576
|
try {
|
|
3014
2577
|
apiVersion = Number(JSON.parse(readFileSync(join(dir, "manifest.json"), "utf8")).apiVersion ?? 1) || 1;
|
|
3015
2578
|
}
|
|
3016
|
-
catch {
|
|
3017
|
-
// best-effort:无 manifest/JSON 坏 → 按 apiVersion 1 处理。
|
|
3018
|
-
}
|
|
2579
|
+
catch { }
|
|
3019
2580
|
if (apiVersion > PLUGIN_API_VERSION) {
|
|
3020
2581
|
const msg = pick(l, `插件要求宿主 API v${apiVersion},当前宿主 v${PLUGIN_API_VERSION} —— 请升级 pi-web-ui`, `Plugin requires host API v${apiVersion} but the host is v${PLUGIN_API_VERSION} — please upgrade pi-web-ui`, "plugins.host.api.mismatch", { apiVersion, PLUGIN_API_VERSION });
|
|
3021
2582
|
console.error(`[plugin:${info.id}] ${msg}`);
|
|
@@ -3032,14 +2593,9 @@ export class PluginManager {
|
|
|
3032
2593
|
});
|
|
3033
2594
|
return;
|
|
3034
2595
|
}
|
|
3035
|
-
// 能力声明:写了 permissions → 严格模式(受控宿主 API 按声明族强制执行);
|
|
3036
|
-
// 未写且 apiVersion < 2 → 旧全权模式(首次使用受控 API 时警告一次,v2 起默认拒绝)。
|
|
3037
2596
|
const permsDeclared = (info.permissions ?? []).slice();
|
|
3038
2597
|
const strict = permsDeclared.length > 0 || apiVersion >= 2;
|
|
3039
2598
|
const permFamilies = new Set(permsDeclared.map((x) => x.split(":")[0]));
|
|
3040
|
-
// 硬依赖声明(manifest "requires",P2-8):任一条不满足即拒绝激活 + 教学式错误
|
|
3041
|
-
// (peerPlugins 仍是缺失只警告的软依赖)。形状坏的在 P1-6 校验层已提前拒,
|
|
3042
|
-
// 这里只做语义判定:hostApi 下限、能力族已知+已声明、对等插件已安装且激活成功。
|
|
3043
2599
|
const req = info.requires;
|
|
3044
2600
|
if (req) {
|
|
3045
2601
|
const refuseRequires = (zh, en, key, vars) => {
|
|
@@ -3102,13 +2658,10 @@ export class PluginManager {
|
|
|
3102
2658
|
break;
|
|
3103
2659
|
}
|
|
3104
2660
|
}
|
|
3105
|
-
// 纯前端对等端(无 index.mjs):目录存在即满足,它没有激活态。
|
|
3106
2661
|
}
|
|
3107
2662
|
if (refused)
|
|
3108
2663
|
return;
|
|
3109
2664
|
}
|
|
3110
|
-
// 引擎约束(manifest engines["pi-web-ui"]):不满足即拒绝激活(与 apiVersion 超前同级处理)。
|
|
3111
|
-
// 解析失败 → 警告放行不阻断;对等依赖缺失 → 只警告不断活。
|
|
3112
2665
|
const enginesReq = info.engines?.["pi-web-ui"];
|
|
3113
2666
|
if (typeof enginesReq === "string" && enginesReq.trim()) {
|
|
3114
2667
|
const hostVer = readHostVersion();
|
|
@@ -3140,17 +2693,10 @@ export class PluginManager {
|
|
|
3140
2693
|
this.pushRuntimeDiag(info.id, `peer plugin missing: ${peer} (warn only, activation continues)`);
|
|
3141
2694
|
}
|
|
3142
2695
|
}
|
|
3143
|
-
// 新增订阅的取消函数(反激活时经 effects 栈统一逆序释放)。
|
|
3144
|
-
// 出站网络白名单(scan 解析的 manifest netAllowlist 快照)。
|
|
3145
2696
|
const netAllow = info.netAllowlist ?? [];
|
|
3146
|
-
// 每插件的私有设施:KV 存储 + 加密 secrets + 依赖自动补装(单飞)。
|
|
3147
2697
|
const storage = new PluginStorage(join(dir, "storage.json"));
|
|
3148
2698
|
const secrets = new PluginSecrets(this.dataDir, dir);
|
|
3149
|
-
// 受限工作区文件访问(能力 "fs" 门控;根随 set_cwd 活值移动)。
|
|
3150
2699
|
const workspaceFs = new WorkspaceFS(() => self.cwdValue);
|
|
3151
|
-
/** 跨目录读写(issue #146):每次操作都要求路径已在授权表里(或落在工作区内)。
|
|
3152
|
-
* 与 workspaceFs 的分工:那个锚定当前工作区、越界拒绝;这个锚定「用户点过头的目录」。
|
|
3153
|
-
* 两者都不允许插件无告知地碰任意路径 —— 这就是「受支持路径」与裸 node:fs 的差别。 */
|
|
3154
2700
|
const allowAbs = (p) => {
|
|
3155
2701
|
const abs = normalizeGrantPath(p);
|
|
3156
2702
|
if (!abs)
|
|
@@ -3159,11 +2705,6 @@ export class PluginManager {
|
|
|
3159
2705
|
return abs;
|
|
3160
2706
|
throw new Error(`目录未授权:先 await host.fs.requestAccess(dir)(${abs})`);
|
|
3161
2707
|
};
|
|
3162
|
-
/** 写类操作(write/remove)的 realpath 复核:allowAbs 是纯字符串判定,
|
|
3163
|
-
* 授权目录里的符号链接/junction 能把写入/递归删除引到授权范围之外。
|
|
3164
|
-
* 取目标最近已存在祖先的 realpath,要求它仍落在(工作区或该插件任一
|
|
3165
|
-
* 已授权目录)的 realpath 内。读/list/stat 保持字符串校验(高频路径,
|
|
3166
|
-
* 且读不存在「把内容写到别处」的风险)。 */
|
|
3167
2708
|
const assertRealInsideGrant = async (abs) => {
|
|
3168
2709
|
const targetReal = realPathOfNearest(abs);
|
|
3169
2710
|
if (!targetReal)
|
|
@@ -3246,8 +2787,6 @@ export class PluginManager {
|
|
|
3246
2787
|
if (walked++ >= 2000 || out.length >= 500)
|
|
3247
2788
|
break;
|
|
3248
2789
|
const abs = join(dir, e.name);
|
|
3249
|
-
// 跨目录返回绝对路径(调用方直接可用);匹配仍按相对 base 的部分。
|
|
3250
|
-
// 先转分隔符再去前导斜杠(顺序反了会留下 "/n.txt" 导致匹配失败)。
|
|
3251
2790
|
const rel = abs
|
|
3252
2791
|
.slice(base.length)
|
|
3253
2792
|
.replace(/\\/g, "/")
|
|
@@ -3267,18 +2806,11 @@ export class PluginManager {
|
|
|
3267
2806
|
return out;
|
|
3268
2807
|
},
|
|
3269
2808
|
};
|
|
3270
|
-
const self = this;
|
|
3271
|
-
// 能力门控快照:host 对象可用之前(loaded.set 之前)canUse 也要能判定。
|
|
2809
|
+
const self = this;
|
|
3272
2810
|
self.activatingGates.set(info.id, { permsDeclared, permFamilies, strictMode: strict, legacyWarned: false });
|
|
3273
|
-
/** 能力门控:统一走 canUse(fs 读写分级/旧全权警告都在里面)。
|
|
3274
|
-
* 返回 false = 已记日志,调用方应拒绝。 */
|
|
3275
2811
|
const can = (family) => self.canUse(info.id, family);
|
|
3276
|
-
/** 读门:"fs" 全开,或精确声明 "fs:read"。 */
|
|
3277
2812
|
const canRead = () => self.canUse(info.id, "fs:read");
|
|
3278
|
-
/** 写门:"fs" 全开,或精确声明 "fs:write"(只读插件写即拒,提示缺 fs/fs:write)。 */
|
|
3279
2813
|
const canWrite = () => self.canUse(info.id, "fs:write");
|
|
3280
|
-
/** 无头调用落地(host.chat 与 host.chatWait 共用):能力 "chat" 门控 +
|
|
3281
|
-
* 文本校验 + chatProvider 投递。宿主未接入时拒绝(不抛到插件侧,由调用方包 {ok:false})。 */
|
|
3282
2814
|
const sendChat = (req) => {
|
|
3283
2815
|
if (!can("chat"))
|
|
3284
2816
|
return Promise.reject(new Error(`插件未声明能力 "chat"(manifest.permissions)——请求被拒`));
|
|
@@ -3291,7 +2823,6 @@ export class PluginManager {
|
|
|
3291
2823
|
if (text.length > 8000)
|
|
3292
2824
|
return Promise.reject(new Error("chat: text 超长(>8000 字),请裁剪后重发"));
|
|
3293
2825
|
const accountId = String(req?.accountId ?? "default").slice(0, 64);
|
|
3294
|
-
// issue #226:透传定时任务对齐的四件套(各按长度封顶,语义校验归宿主 chatFromPlugin)。
|
|
3295
2826
|
const r = (req ?? {});
|
|
3296
2827
|
const passthrough = { text, accountId };
|
|
3297
2828
|
try {
|
|
@@ -3316,6 +2847,13 @@ export class PluginManager {
|
|
|
3316
2847
|
passthrough.thinkingLevel = thinkingLevel.slice(0, 64);
|
|
3317
2848
|
return self.chatProvider(info.id, passthrough);
|
|
3318
2849
|
};
|
|
2850
|
+
const workItemsCall = async (method, args) => {
|
|
2851
|
+
if (!can("chat"))
|
|
2852
|
+
throw new Error('Work items require the "chat" capability');
|
|
2853
|
+
if (!self.workItemsProvider)
|
|
2854
|
+
throw new Error("Work item service unavailable");
|
|
2855
|
+
return (await self.workItemsProvider(info.id, method, args));
|
|
2856
|
+
};
|
|
3319
2857
|
let pluginAbort = null;
|
|
3320
2858
|
const host = {
|
|
3321
2859
|
broadcast: (payload) => this.broadcast(info.id, payload),
|
|
@@ -3378,13 +2916,22 @@ export class PluginManager {
|
|
|
3378
2916
|
},
|
|
3379
2917
|
getActiveConversation: (opts) => self.getActiveConversation(opts),
|
|
3380
2918
|
chat: (req) => sendChat(req),
|
|
2919
|
+
workItems: {
|
|
2920
|
+
rooms: () => workItemsCall("rooms", {}),
|
|
2921
|
+
ingest: (input) => workItemsCall("ingest", { ...input }),
|
|
2922
|
+
list: (opts) => workItemsCall("list", { ...opts }),
|
|
2923
|
+
get: (id) => workItemsCall("get", { id }),
|
|
2924
|
+
update: (id, patch) => workItemsCall("update", { id, patch }),
|
|
2925
|
+
claimDelivery: (opts) => workItemsCall("claimDelivery", { ...opts }),
|
|
2926
|
+
completeDelivery: (id, result) => workItemsCall("completeDelivery", { id, ...result }),
|
|
2927
|
+
failDelivery: (id, result) => workItemsCall("failDelivery", { id, ...result }),
|
|
2928
|
+
},
|
|
3381
2929
|
chatWait: async (req, opts) => {
|
|
3382
2930
|
try {
|
|
3383
2931
|
const sent = await sendChat(req);
|
|
3384
2932
|
const cid = sent?.conversationId ?? "";
|
|
3385
2933
|
if (!cid)
|
|
3386
2934
|
return { ok: false, error: "无头调用未返回 conversationId" };
|
|
3387
|
-
// 默认 120s 超时;上下钳制防 100ms 误杀与无限等待。
|
|
3388
2935
|
const timeoutMs = Math.max(1000, Math.min(Number(opts?.timeoutMs ?? 120_000) || 120_000, 600_000));
|
|
3389
2936
|
const ended = await self.waitRunEnd(cid, timeoutMs, sent?.runId);
|
|
3390
2937
|
if (!ended)
|
|
@@ -3400,7 +2947,6 @@ export class PluginManager {
|
|
|
3400
2947
|
complete: async (req) => {
|
|
3401
2948
|
if (!can("llm"))
|
|
3402
2949
|
return { ok: false, error: `插件未声明能力 "llm"(manifest.permissions)——请求被拒` };
|
|
3403
|
-
// 有模型作用域授权时收紧到批准的模型(无授权=声明即全开,向后兼容)。
|
|
3404
2950
|
const model = typeof req?.model === "string" ? req.model.trim() : "";
|
|
3405
2951
|
if (model && !self.permGrants.modelAllowed(info.id, model))
|
|
3406
2952
|
return {
|
|
@@ -3421,7 +2967,6 @@ export class PluginManager {
|
|
|
3421
2967
|
const family = req?.family;
|
|
3422
2968
|
if (family !== "net" && family !== "llm")
|
|
3423
2969
|
throw new Error(`requestPermission: 不支持的能力族「${String(family)}」(目前只收 net/llm)`);
|
|
3424
|
-
// 基础族必须已声明(与 requestAccess 要求 fs:read 同口径,fail-closed)。
|
|
3425
2970
|
if (!can(family))
|
|
3426
2971
|
return false;
|
|
3427
2972
|
const hosts = family === "net" && Array.isArray(req.hosts)
|
|
@@ -3441,7 +2986,6 @@ export class PluginManager {
|
|
|
3441
2986
|
const reason = typeof req.reason === "string"
|
|
3442
2987
|
? String(req.reason).slice(0, 200)
|
|
3443
2988
|
: undefined;
|
|
3444
|
-
// 已有授权(静态白名单算在执行期,动态表在这里):直接通过,不打扰用户。
|
|
3445
2989
|
if (family === "net" && hosts.every((h) => netAllow.some((entry) => h === entry || h.endsWith(`.${entry}`))))
|
|
3446
2990
|
return true;
|
|
3447
2991
|
if (family === "net" && hosts.every((h) => self.permGrants.has(info.id, "net", { host: h })))
|
|
@@ -3474,9 +3018,7 @@ export class PluginManager {
|
|
|
3474
3018
|
try {
|
|
3475
3019
|
self.onPermGrantsChanged?.();
|
|
3476
3020
|
}
|
|
3477
|
-
catch {
|
|
3478
|
-
/* 推送失败不影响已完成的授权 */
|
|
3479
|
-
}
|
|
3021
|
+
catch { }
|
|
3480
3022
|
return true;
|
|
3481
3023
|
},
|
|
3482
3024
|
conversations: {
|
|
@@ -3498,7 +3040,6 @@ export class PluginManager {
|
|
|
3498
3040
|
},
|
|
3499
3041
|
get: (id) => {
|
|
3500
3042
|
try {
|
|
3501
|
-
// conversationProvider 只给当前打开对话:id 对上才回,否则 null。
|
|
3502
3043
|
const s = self.getActiveConversation();
|
|
3503
3044
|
return s && s.conversationId === String(id) ? s : null;
|
|
3504
3045
|
}
|
|
@@ -3516,7 +3057,6 @@ export class PluginManager {
|
|
|
3516
3057
|
return r.catch(() => []);
|
|
3517
3058
|
return r ?? [];
|
|
3518
3059
|
}
|
|
3519
|
-
// 无 searcher 注入时退化为 lister 标题过滤;两者都无回 []。
|
|
3520
3060
|
const lq = q.trim().toLowerCase();
|
|
3521
3061
|
if (!lq)
|
|
3522
3062
|
return [];
|
|
@@ -3609,6 +3149,12 @@ export class PluginManager {
|
|
|
3609
3149
|
return effects.add(`service_listener:${name}`, off);
|
|
3610
3150
|
},
|
|
3611
3151
|
},
|
|
3152
|
+
tts: {
|
|
3153
|
+
registerEngine: (engine) => effects.add(`tts:${engine.id}`, ttsService.registerEngine(info.id, engine)),
|
|
3154
|
+
listEngines: () => ttsService.listEngines(),
|
|
3155
|
+
listVoices: (engine, options) => ttsService.listVoices(engine, options),
|
|
3156
|
+
synthesize: (request) => ttsService.synthesize(request),
|
|
3157
|
+
},
|
|
3612
3158
|
provide: (name, service) => host.services.provide(name, service),
|
|
3613
3159
|
get: (name) => host.services.get(name),
|
|
3614
3160
|
onService: (name, handler) => host.services.on(name, handler),
|
|
@@ -3643,7 +3189,6 @@ export class PluginManager {
|
|
|
3643
3189
|
self.unregisterProxy(info.id, p);
|
|
3644
3190
|
});
|
|
3645
3191
|
},
|
|
3646
|
-
// 包一层:插件反激活时自动注销它注册的全部 AI 工具,不留悬挂项。
|
|
3647
3192
|
registerAgentTool: (tool) => {
|
|
3648
3193
|
if (!can("tools"))
|
|
3649
3194
|
return () => { };
|
|
@@ -3702,7 +3247,6 @@ export class PluginManager {
|
|
|
3702
3247
|
const abs = normalizeGrantPath(String(dir ?? ""));
|
|
3703
3248
|
if (!abs)
|
|
3704
3249
|
return false;
|
|
3705
|
-
// 工作区内的路径本来就能用,不必打扰用户。
|
|
3706
3250
|
if (self.isInsideWorkspace(abs))
|
|
3707
3251
|
return true;
|
|
3708
3252
|
if (self.grants.has(info.id, abs))
|
|
@@ -3712,13 +3256,10 @@ export class PluginManager {
|
|
|
3712
3256
|
const ok = await self.pathAccessRequester(info.id, abs, reason);
|
|
3713
3257
|
if (ok) {
|
|
3714
3258
|
self.grants.grant(info.id, abs);
|
|
3715
|
-
// 授权表变了 → 通知宿主重推(设置面板即时可见)。 throws 不能拖塔授权本身。
|
|
3716
3259
|
try {
|
|
3717
3260
|
self.onGrantsChanged?.();
|
|
3718
3261
|
}
|
|
3719
|
-
catch {
|
|
3720
|
-
/* 推送失败不影响已完成的授权 */
|
|
3721
|
-
}
|
|
3262
|
+
catch { }
|
|
3722
3263
|
}
|
|
3723
3264
|
return ok;
|
|
3724
3265
|
},
|
|
@@ -3737,7 +3278,6 @@ export class PluginManager {
|
|
|
3737
3278
|
throw new Error('插件未声明读能力 "fs"/"fs:read"(manifest.permissions)——请求被拒');
|
|
3738
3279
|
if (typeof handler !== "function")
|
|
3739
3280
|
throw new Error("watch: handler 必须是函数");
|
|
3740
|
-
// 锚定活 cwd 根:目标必须在工作区内(复用 WorkspaceFS 的越界校验思想)。
|
|
3741
3281
|
const target = resolve(self.cwdValue, String(relPath ?? ""));
|
|
3742
3282
|
if (!self.isInsideWorkspace(target))
|
|
3743
3283
|
throw new Error(`路径越界:${String(relPath)}`);
|
|
@@ -3754,16 +3294,13 @@ export class PluginManager {
|
|
|
3754
3294
|
try {
|
|
3755
3295
|
watcher.close();
|
|
3756
3296
|
}
|
|
3757
|
-
catch {
|
|
3758
|
-
/* already closed */
|
|
3759
|
-
}
|
|
3297
|
+
catch { }
|
|
3760
3298
|
};
|
|
3761
3299
|
return effects.add(`watch:${String(relPath)}`, off);
|
|
3762
3300
|
},
|
|
3763
3301
|
},
|
|
3764
3302
|
project: {
|
|
3765
3303
|
create: async (spec) => {
|
|
3766
|
-
// 写门:只读插件(仅 fs:read)与无 fs 声明的一律拒绝,错误提示缺 fs/fs:write。
|
|
3767
3304
|
if (!canWrite())
|
|
3768
3305
|
return {
|
|
3769
3306
|
ok: false,
|
|
@@ -3826,12 +3363,9 @@ export class PluginManager {
|
|
|
3826
3363
|
try {
|
|
3827
3364
|
this.onBgTasksChanged?.();
|
|
3828
3365
|
}
|
|
3829
|
-
catch {
|
|
3830
|
-
// best-effort:可选 UI 刷新,失败忽略。
|
|
3831
|
-
}
|
|
3366
|
+
catch { }
|
|
3832
3367
|
};
|
|
3833
3368
|
fire();
|
|
3834
|
-
// 统一心跳探活循环调度
|
|
3835
3369
|
let heartbeatTimer = null;
|
|
3836
3370
|
const stopHeartbeat = () => {
|
|
3837
3371
|
if (heartbeatTimer) {
|
|
@@ -3934,9 +3468,6 @@ export class PluginManager {
|
|
|
3934
3468
|
for (let idx = 0; idx < sliced.length; idx++) {
|
|
3935
3469
|
const raw = sliced[idx];
|
|
3936
3470
|
const slotRaw = typeof raw?.slot === "string" ? String(raw.slot) : "";
|
|
3937
|
-
// 与 manifest 解析同口径:先查别名(topbar → topbar.primary)、再校枚举。
|
|
3938
|
-
// 运行时注册不校验的话,插件给个别名(或写错)会得到一个前端不认识的 slot
|
|
3939
|
-
// —— buildUiSlots 会静默丢掉它,表现为「注册了但界面上没有」,最难排。
|
|
3940
3471
|
const slot = UI_SLOT_ALIASES[slotRaw] ?? slotRaw;
|
|
3941
3472
|
if (!slot || !UI_SLOTS.has(slot)) {
|
|
3942
3473
|
self.pushRuntimeDiag(info.id, `ui.register[#${idx}]: unknown slot "${slotRaw || "(missing)"}", item dropped`);
|
|
@@ -3968,16 +3499,11 @@ export class PluginManager {
|
|
|
3968
3499
|
update: (id, patch) => {
|
|
3969
3500
|
if (!can("ui"))
|
|
3970
3501
|
return;
|
|
3971
|
-
// 只能更新"当前生效"的条目:manifest 声明的与运行时注册的都算,
|
|
3972
|
-
// 不存在的一律忽略(避免插件凭空造条目绕过声明审查)。
|
|
3973
3502
|
const base = self.uiOf(info.id)?.items.find((x) => x.id === id);
|
|
3974
3503
|
if (!base) {
|
|
3975
3504
|
self.pushRuntimeDiag(info.id, `ui.update: unknown id "${String(id).slice(0, 32)}", ignored`);
|
|
3976
3505
|
return;
|
|
3977
3506
|
}
|
|
3978
|
-
// 与注册同一条校验管线:patch 合进 base 后整体过 parseUiItem,
|
|
3979
|
-
// 非法字段按 parse 语义丢弃/回落(未知键清掉、iconSvg 重新归一化、
|
|
3980
|
-
// 超长截断、progress 夹取),id 与 slot 不可被 patch 改写。
|
|
3981
3507
|
const diags = [];
|
|
3982
3508
|
const mergedRaw = typeof patch === "object" && patch !== null && !Array.isArray(patch)
|
|
3983
3509
|
? { ...base, ...patch, id, slot: base.slot }
|
|
@@ -3986,8 +3512,6 @@ export class PluginManager {
|
|
|
3986
3512
|
for (const m of diags)
|
|
3987
3513
|
self.pushRuntimeDiag(info.id, `ui.update: ${m}`);
|
|
3988
3514
|
if (!parsed) {
|
|
3989
|
-
// 合并结果不合法(典型:patch 把 label 改成空)——按现有 parse 语义
|
|
3990
|
-
// 视为无效更新,保留原条目。
|
|
3991
3515
|
self.pushRuntimeDiag(info.id, `ui.update: merged item invalid, update ignored`);
|
|
3992
3516
|
return;
|
|
3993
3517
|
}
|
|
@@ -4040,7 +3564,6 @@ export class PluginManager {
|
|
|
4040
3564
|
list: () => self.uiOf(info.id) ?? { items: [], arrange: [] },
|
|
4041
3565
|
},
|
|
4042
3566
|
getSettings: () => {
|
|
4043
|
-
// P2-9:运行时视角同样走三层合并(警告已在 scan 落诊断,这里静默用值)。
|
|
4044
3567
|
const schema = info.settingsSchema ?? [];
|
|
4045
3568
|
const overlayFile = readSettingsOverlayFile(self.dataDir, info.id);
|
|
4046
3569
|
const overlay = cleanOverlaySettings(schema, overlayFile.raw, "en");
|
|
@@ -4077,7 +3600,6 @@ export class PluginManager {
|
|
|
4077
3600
|
},
|
|
4078
3601
|
},
|
|
4079
3602
|
bash: async (cmd, opts) => {
|
|
4080
|
-
// 门控语义沿用 registerAgentTool:无 "tools" 声明即拒绝(结果对象形态,不断路抛错)。
|
|
4081
3603
|
if (!can("tools"))
|
|
4082
3604
|
return { ok: false, output: "", error: '插件未声明能力 "tools"(manifest.permissions)——请求被拒' };
|
|
4083
3605
|
try {
|
|
@@ -4127,7 +3649,6 @@ export class PluginManager {
|
|
|
4127
3649
|
.trim()
|
|
4128
3650
|
.slice(0, 60)
|
|
4129
3651
|
: undefined;
|
|
4130
|
-
// 归一化声明:毫秒间隔 或 全 5 字段 cron(旧的 "*/N * * * *" 是它的子集,照常工作)。
|
|
4131
3652
|
let ms = 0;
|
|
4132
3653
|
let parts;
|
|
4133
3654
|
let specText;
|
|
@@ -4135,7 +3656,7 @@ export class PluginManager {
|
|
|
4135
3656
|
ms = Math.floor(cronOrMs) || 0;
|
|
4136
3657
|
if (!(ms > 0))
|
|
4137
3658
|
throw new Error("schedule: 间隔毫秒数必须大于 0");
|
|
4138
|
-
ms = Math.min(ms, 2_147_483_647);
|
|
3659
|
+
ms = Math.min(ms, 2_147_483_647);
|
|
4139
3660
|
ms = Math.max(ms, persistent ? 60_000 : 10_000);
|
|
4140
3661
|
specText = String(ms);
|
|
4141
3662
|
}
|
|
@@ -4149,7 +3670,6 @@ export class PluginManager {
|
|
|
4149
3670
|
else {
|
|
4150
3671
|
throw new Error("schedule: 参数必须是间隔毫秒数或 cron 字符串");
|
|
4151
3672
|
}
|
|
4152
|
-
// 持久化:声明落盘(幂等——activate 重调时保留 lastRun/createdAt,只更新声明)。
|
|
4153
3673
|
let sid = "";
|
|
4154
3674
|
if (persistent) {
|
|
4155
3675
|
sid =
|
|
@@ -4172,7 +3692,6 @@ export class PluginManager {
|
|
|
4172
3692
|
let timer;
|
|
4173
3693
|
let grace;
|
|
4174
3694
|
let cancelled = false;
|
|
4175
|
-
/** 下一次触发时刻的展示串;`null` = 一年内没有下一次(如 2 月 31 日)。 */
|
|
4176
3695
|
const nextText = () => {
|
|
4177
3696
|
if (parts) {
|
|
4178
3697
|
try {
|
|
@@ -4189,7 +3708,6 @@ export class PluginManager {
|
|
|
4189
3708
|
const next = nextText();
|
|
4190
3709
|
return next === null ? "不再触发(表达式在一年内不会命中)" : `下次 ${next}`;
|
|
4191
3710
|
};
|
|
4192
|
-
// 后台面板条目(持久任务独有):看得见下次时间,停止=删声明(不再复活)。
|
|
4193
3711
|
let bgRefresh;
|
|
4194
3712
|
let bgUnreg;
|
|
4195
3713
|
if (persistent) {
|
|
@@ -4208,9 +3726,7 @@ export class PluginManager {
|
|
|
4208
3726
|
try {
|
|
4209
3727
|
self.onBgTasksChanged?.();
|
|
4210
3728
|
}
|
|
4211
|
-
catch {
|
|
4212
|
-
/* 推送失败不影响定时本身 */
|
|
4213
|
-
}
|
|
3729
|
+
catch { }
|
|
4214
3730
|
};
|
|
4215
3731
|
bgRefresh = () => {
|
|
4216
3732
|
if (!bgTaskTable.has(taskId))
|
|
@@ -4249,14 +3765,6 @@ export class PluginManager {
|
|
|
4249
3765
|
}
|
|
4250
3766
|
bgRefresh?.();
|
|
4251
3767
|
};
|
|
4252
|
-
/**
|
|
4253
|
-
* 排下一次触发。两个要点:
|
|
4254
|
-
* - `nextCronFire` 回 null = 一年内没有下一次(如 `0 0 31 2 *`)→ 不再排;
|
|
4255
|
-
* - 延迟走 `armDelay` 分片(默认 ≤6 小时),因为 Node 的 setTimeout 延迟超过
|
|
4256
|
-
* 2^31-1ms(≈24.8 天)会**溢出成 1ms**,配上「下次在 42 天/一年后」的
|
|
4257
|
-
* 合法 cron 就是「1ms 后再触发」的死循环(触发还会回调插件 → 写盘 + 广播)。
|
|
4258
|
-
* 分片保持 dueAt,真到点才 fire,然后计算下一次。
|
|
4259
|
-
*/
|
|
4260
3768
|
let dueAt = parts ? nextCronFire(parts, Date.now()) : null;
|
|
4261
3769
|
const armCron = () => {
|
|
4262
3770
|
if (cancelled || !parts || dueAt === null)
|
|
@@ -4278,17 +3786,15 @@ export class PluginManager {
|
|
|
4278
3786
|
timer = setInterval(fire, ms);
|
|
4279
3787
|
timer.unref?.();
|
|
4280
3788
|
}
|
|
4281
|
-
// 漏跑补跑:以上次触发(没跑过按创建时间)为锚,下一次已在过去=漏了。
|
|
4282
3789
|
if (persistent && catchUp === "once") {
|
|
4283
3790
|
const refTime = loadScheduleRecords(dir)[sid]?.lastRun ?? loadScheduleRecords(dir)[sid]?.createdAt ?? Date.now();
|
|
4284
3791
|
const missed = parts
|
|
4285
3792
|
? (() => {
|
|
4286
3793
|
const at = nextCronFire(parts, refTime);
|
|
4287
|
-
return at !== null && at <= Date.now();
|
|
3794
|
+
return at !== null && at <= Date.now();
|
|
4288
3795
|
})()
|
|
4289
3796
|
: refTime + ms <= Date.now();
|
|
4290
3797
|
if (missed) {
|
|
4291
|
-
// 15s 缓冲:刚启动时模型/网络可能还没就绪,补跑不等那 15 秒可能白跑。
|
|
4292
3798
|
grace = setTimeout(() => fire(), 15_000);
|
|
4293
3799
|
grace.unref?.();
|
|
4294
3800
|
}
|
|
@@ -4315,7 +3821,6 @@ export class PluginManager {
|
|
|
4315
3821
|
}
|
|
4316
3822
|
}
|
|
4317
3823
|
};
|
|
4318
|
-
// 反激活只停表、不断持久化:下次 activate 重调 schedule() 即按落盘声明重建。
|
|
4319
3824
|
effects.add(`schedule:${sid}`, cancelTimer);
|
|
4320
3825
|
return off;
|
|
4321
3826
|
},
|
|
@@ -4399,9 +3904,7 @@ export class PluginManager {
|
|
|
4399
3904
|
try {
|
|
4400
3905
|
self.onAgentToolsChanged?.();
|
|
4401
3906
|
}
|
|
4402
|
-
catch {
|
|
4403
|
-
/* ignore */
|
|
4404
|
-
}
|
|
3907
|
+
catch { }
|
|
4405
3908
|
}
|
|
4406
3909
|
return res;
|
|
4407
3910
|
}
|
|
@@ -4479,8 +3982,6 @@ export class PluginManager {
|
|
|
4479
3982
|
if (!can("net"))
|
|
4480
3983
|
return { ok: false, error: '插件未声明能力 "net"(manifest.permissions)——请求被拒' };
|
|
4481
3984
|
return pluginNetFetch(url, init, {
|
|
4482
|
-
// 白名单:主机相等或 .后缀匹配;空表即全拒(fail-closed)。
|
|
4483
|
-
// 用户动态批准的主机(host.requestPermission)同样放行,免改 manifest 重装。
|
|
4484
3985
|
hostAllowed: (hostname) => netAllow.some((entry) => hostname === entry || hostname.endsWith(`.${entry}`)) ||
|
|
4485
3986
|
self.permGrants.has(info.id, "net", { host: hostname }),
|
|
4486
3987
|
fetchImpl: (input, reqInit) => globalThis.fetch(input, reqInit),
|
|
@@ -4492,7 +3993,6 @@ export class PluginManager {
|
|
|
4492
3993
|
const t = String(topic ?? "");
|
|
4493
3994
|
if (!t)
|
|
4494
3995
|
return;
|
|
4495
|
-
// 载荷 JSON 往返截断:4KB 内还原对象,超了取截断后的字符串化形态。
|
|
4496
3996
|
let p;
|
|
4497
3997
|
try {
|
|
4498
3998
|
const s = JSON.stringify(payload);
|
|
@@ -4536,11 +4036,9 @@ export class PluginManager {
|
|
|
4536
4036
|
},
|
|
4537
4037
|
log: (levelOrArg, ...args) => {
|
|
4538
4038
|
const level = normalizePluginLogLevel(levelOrArg);
|
|
4539
|
-
// 首参是级别即剥掉(不进文本);否则全部参数都是日志内容(老插件兼容)。
|
|
4540
4039
|
const text = formatPluginLogText(levelOrArg === level ? args : [levelOrArg, ...args]);
|
|
4541
4040
|
self.appendPluginLog(info.id, level, text);
|
|
4542
4041
|
const line = `[plugin:${info.id}]${text ? ` ${text}` : ""}`;
|
|
4543
|
-
// console 既有行为保留:级别只决定走哪个 console 方法。
|
|
4544
4042
|
if (level === "error")
|
|
4545
4043
|
console.error(line);
|
|
4546
4044
|
else if (level === "warn")
|
|
@@ -4601,8 +4099,6 @@ export class PluginManager {
|
|
|
4601
4099
|
status: (spec) => getPluginAssetStatus(dir, spec),
|
|
4602
4100
|
},
|
|
4603
4101
|
};
|
|
4604
|
-
// 每个 facade 方法都绑定本次激活;旧异步 continuation 不得借同 id 操作新注册表。
|
|
4605
|
-
// effect 是例外:过期后仍立即执行 cleanup,回收插件刚创建的外部资源。
|
|
4606
4102
|
const facades = new WeakMap();
|
|
4607
4103
|
const guard = (target) => {
|
|
4608
4104
|
const cached = facades.get(target);
|
|
@@ -4630,8 +4126,6 @@ export class PluginManager {
|
|
|
4630
4126
|
const guardedHost = guard(host);
|
|
4631
4127
|
this.activatingEffects.add(effects);
|
|
4632
4128
|
try {
|
|
4633
|
-
// Node 对同一 URL 的 import() 永远返回缓存模块——追加 epoch 作查询串
|
|
4634
|
-
// 击穿缓存,让 plugins_reload 后的重新激活能拿到磁盘上的新代码。
|
|
4635
4129
|
const mod = (await import(pathToFileURL(join(dir, "index.mjs")).href + `?e=${this.epochCounter}`));
|
|
4636
4130
|
if (!isCurrent())
|
|
4637
4131
|
return;
|
|
@@ -4644,8 +4138,6 @@ export class PluginManager {
|
|
|
4644
4138
|
}
|
|
4645
4139
|
const gateWarned = self.activatingGates.get(info.id)?.legacyWarned;
|
|
4646
4140
|
self.activatingGates.delete(info.id);
|
|
4647
|
-
// activate() 执行期间经 host 注册产生的运行时诊断(重复工具/命令、ui 丢弃等)
|
|
4648
|
-
// 已经进了 runtimeDiags,这里合并进快照一起下发。
|
|
4649
4141
|
const combined = this.diagnosticsOf(info.id);
|
|
4650
4142
|
this.loaded.set(info.id, {
|
|
4651
4143
|
info: { ...info, ...(combined ? { diagnostics: combined } : {}) },
|
|
@@ -4667,14 +4159,9 @@ export class PluginManager {
|
|
|
4667
4159
|
settingsHandlers,
|
|
4668
4160
|
});
|
|
4669
4161
|
console.log(`[plugin:${info.id}] activated (v${info.version ?? "?"})`);
|
|
4670
|
-
// 首次安装/能力变更提醒(尽力而为):<dir>/.pi-approved 记录上次激活时
|
|
4671
|
-
// 的能力清单——新装或 permissions 变更后向在线客户端推一条警告通知,
|
|
4672
|
-
// 用户装前可见、日常启动不打扰。
|
|
4673
4162
|
void this.maybeConsentNotice(info, dir, permsDeclared);
|
|
4674
4163
|
}
|
|
4675
4164
|
catch (err) {
|
|
4676
|
-
// 激活失败:先把这一轮已经登记的可逆副作用逆序回卷(半途注册的工具/路由/
|
|
4677
|
-
// 定时器不能留着 —— 插件已经坏了,谁也不会来撤它们),再留一条错误占位行。
|
|
4678
4165
|
const failed = effects.release();
|
|
4679
4166
|
httpRoutes.clear();
|
|
4680
4167
|
if (generation !== this.lifecycleGeneration)
|
|
@@ -4700,21 +4187,15 @@ export class PluginManager {
|
|
|
4700
4187
|
}
|
|
4701
4188
|
}
|
|
4702
4189
|
}
|
|
4703
|
-
/**
|
|
4704
|
-
* 把 /plugins/:id/client/<rest> 安全映射到 <pluginsDir>/<id>/client/<rest>。
|
|
4705
|
-
* 返回绝对路径;任何越界/非法 id 返回 null(调用方回 404)。
|
|
4706
|
-
*/
|
|
4707
4190
|
export function resolvePluginClientFile(pluginsDir, id, rest) {
|
|
4708
4191
|
if (!ID_RE.test(id))
|
|
4709
4192
|
return null;
|
|
4710
4193
|
const root = resolve(join(pluginsDir, id, "client"));
|
|
4711
|
-
// rest 由 express 路由保证不带 "..",但双保险:resolve 后必须仍在 root 内
|
|
4712
4194
|
const abs = resolve(root, rest);
|
|
4713
4195
|
if (abs !== root && !abs.startsWith(root + sep))
|
|
4714
4196
|
return null;
|
|
4715
4197
|
return abs;
|
|
4716
4198
|
}
|
|
4717
|
-
/** 解析插件公共模块静态文件绝对路径(防止路径穿越)。 */
|
|
4718
4199
|
export function resolvePluginSharedFile(sharedDir, rest) {
|
|
4719
4200
|
const root = resolve(sharedDir);
|
|
4720
4201
|
const abs = resolve(root, rest);
|
|
@@ -4722,7 +4203,6 @@ export function resolvePluginSharedFile(sharedDir, rest) {
|
|
|
4722
4203
|
return null;
|
|
4723
4204
|
return abs;
|
|
4724
4205
|
}
|
|
4725
|
-
/** 通用代理前缀的保留字:命中即拒绝注册(宿主自用路径,代理抢了会吞掉主站功能)。 */
|
|
4726
4206
|
export const PROXY_RESERVED_PREFIXES = [
|
|
4727
4207
|
"/api",
|
|
4728
4208
|
"/ws",
|
|
@@ -4733,14 +4213,12 @@ export const PROXY_RESERVED_PREFIXES = [
|
|
|
4733
4213
|
"/icons",
|
|
4734
4214
|
"/themes",
|
|
4735
4215
|
];
|
|
4736
|
-
/** 校验并归一化代理前缀:合法返回去尾斜杠的小写形式,否则返回 null(纯函数,单测覆盖)。 */
|
|
4737
4216
|
export function normalizeProxyPrefix(prefix) {
|
|
4738
4217
|
if (typeof prefix !== "string")
|
|
4739
4218
|
return null;
|
|
4740
4219
|
let p = prefix.trim();
|
|
4741
4220
|
if (!p.startsWith("/") || p.length < 2)
|
|
4742
4221
|
return null;
|
|
4743
|
-
// 去尾斜杠("/liveserver/" → "/liveserver")
|
|
4744
4222
|
while (p.length > 1 && p.endsWith("/"))
|
|
4745
4223
|
p = p.slice(0, -1);
|
|
4746
4224
|
if (!/^[A-Za-z0-9/_-]+$/.test(p) || p.includes("//"))
|
|
@@ -4752,7 +4230,6 @@ export function normalizeProxyPrefix(prefix) {
|
|
|
4752
4230
|
}
|
|
4753
4231
|
return lower;
|
|
4754
4232
|
}
|
|
4755
|
-
/** 校验代理目标:只收 127.0.0.1/localhost + 合法端口(纯函数,单测覆盖)。 */
|
|
4756
4233
|
export function normalizeProxyTarget(target) {
|
|
4757
4234
|
const port = typeof target === "number" ? target : target?.port;
|
|
4758
4235
|
const hostRaw = typeof target === "number" ? "127.0.0.1" : String(target?.host ?? "127.0.0.1");
|
|
@@ -4763,7 +4240,6 @@ export function normalizeProxyTarget(target) {
|
|
|
4763
4240
|
return null;
|
|
4764
4241
|
return { host: "127.0.0.1", port: port };
|
|
4765
4242
|
}
|
|
4766
|
-
/** 在请求路径上做最长前缀匹配(边界对齐:prefix 本身或 prefix + "/" 开头才算命中)。 */
|
|
4767
4243
|
export function matchProxyPrefix(path, prefixes) {
|
|
4768
4244
|
let best;
|
|
4769
4245
|
for (const p of prefixes) {
|
|
@@ -4774,14 +4250,6 @@ export function matchProxyPrefix(path, prefixes) {
|
|
|
4774
4250
|
}
|
|
4775
4251
|
return best;
|
|
4776
4252
|
}
|
|
4777
|
-
/**
|
|
4778
|
-
* 把插件 AI 工具定义同步进一个「会话状对象」(SDK AgentSession 的结构子集:
|
|
4779
|
-
* 内部 _customTools 数组 + _refreshToolRegistry()——refresh 会重读数组,且新
|
|
4780
|
-
* 工具名自动加入活跃集)。新增/更新/移除三向 diff;对象不兼容(SDK 改名)返回
|
|
4781
|
-
* null 由调用方静默降级。返回新的已注入名单。
|
|
4782
|
-
*
|
|
4783
|
-
* 纯函数、不 import SDK —— vitest 直接测(tests/unit/plugin-tools.test.ts)。
|
|
4784
|
-
*/
|
|
4785
4253
|
export function syncPluginToolsIntoSession(session, defs, prevNames) {
|
|
4786
4254
|
if (!Array.isArray(session._customTools) || typeof session._refreshToolRegistry !== "function")
|
|
4787
4255
|
return null;
|
|
@@ -4805,20 +4273,7 @@ export function syncPluginToolsIntoSession(session, defs, prevNames) {
|
|
|
4805
4273
|
session._refreshToolRegistry();
|
|
4806
4274
|
return new Set(defs.map((d) => d.name));
|
|
4807
4275
|
}
|
|
4808
|
-
/** net.fetch 的手动重定向跳数上限。 */
|
|
4809
4276
|
const NET_FETCH_MAX_REDIRECTS = 5;
|
|
4810
|
-
/**
|
|
4811
|
-
* net.fetch 的白名单 + 手动重定向循环(抽出为纯可注入函数,vitest 直测)。
|
|
4812
|
-
*
|
|
4813
|
-
* 为什么不用 redirect:"follow":跟随重定向不复查白名单,会把「只授权了 A 主机」
|
|
4814
|
-
* 变成「A 主机一跳把你带到任意主机/内网地址」(经典白名单绕过跳板)。所以:
|
|
4815
|
-
* - 每一跳(含首次)的目标 host 都重新过 allowHost(manifest 白名单 + 用户动态批准);
|
|
4816
|
-
* - redirect:"manual",至多 NET_FETCH_MAX_REDIRECTS 跳;
|
|
4817
|
-
* - 跨宿主重定向剥掉 authorization/cookie 头(浏览器同款语义,防凭据外带);
|
|
4818
|
-
* - 303 一律转 GET 并丢 body;其余 3xx 保持原 method/body(307/308 语义);
|
|
4819
|
-
* - 响应文本整体回给插件的现状保留(512KB 截断不变);
|
|
4820
|
-
* - timeout 是整条请求链(含所有跳)共享的墙钟预算(与原实现 15s 同量级)。
|
|
4821
|
-
*/
|
|
4822
4277
|
export async function pluginNetFetch(url, init, deps) {
|
|
4823
4278
|
const deadline = Date.now() + Math.max(1000, Number(deps.timeoutMs ?? 15_000));
|
|
4824
4279
|
let current = String(url ?? "");
|
|
@@ -4836,7 +4291,6 @@ export async function pluginNetFetch(url, init, deps) {
|
|
|
4836
4291
|
if (u.protocol !== "http:" && u.protocol !== "https:") {
|
|
4837
4292
|
return { ok: false, error: `net: 不支持的协议 ${u.protocol}` };
|
|
4838
4293
|
}
|
|
4839
|
-
// 白名单:主机相等或 .后缀匹配;空表即全拒(fail-closed)。每一跳都查。
|
|
4840
4294
|
if (!deps.hostAllowed(u.hostname.toLowerCase())) {
|
|
4841
4295
|
return {
|
|
4842
4296
|
ok: false,
|
|
@@ -4859,7 +4313,6 @@ export async function pluginNetFetch(url, init, deps) {
|
|
|
4859
4313
|
catch (err) {
|
|
4860
4314
|
return { ok: false, error: err.message };
|
|
4861
4315
|
}
|
|
4862
|
-
// 3xx 且带 location → 手动跳下一跳(目标 host 重新过上面的白名单)。
|
|
4863
4316
|
if (res.status >= 300 && res.status < 400) {
|
|
4864
4317
|
const loc = res.headers.get("location");
|
|
4865
4318
|
if (loc) {
|
|
@@ -4874,7 +4327,6 @@ export async function pluginNetFetch(url, init, deps) {
|
|
|
4874
4327
|
return { ok: false, error: `net: 重定向目标无效 ${loc}` };
|
|
4875
4328
|
}
|
|
4876
4329
|
if (next.hostname.toLowerCase() !== u.hostname.toLowerCase() && headers) {
|
|
4877
|
-
// 跨宿主:凭据类头不外带。
|
|
4878
4330
|
const stripped = {};
|
|
4879
4331
|
for (const [k, v] of Object.entries(headers)) {
|
|
4880
4332
|
const key = k.toLowerCase();
|