hostlet 0.0.0-stage → 0.1.0-alpha.2
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/LICENSE +21 -0
- package/README.md +233 -2
- package/bin/hostlet.js +61 -0
- package/console/app/api.js +1223 -0
- package/console/app/boot.js +165 -0
- package/console/app/cards.js +222 -0
- package/console/app/compose-quote.js +127 -0
- package/console/app/config-form.js +727 -0
- package/console/app/document-drafts.js +62 -0
- package/console/app/document-tools.mjs +67 -0
- package/console/app/documents.js +106 -0
- package/console/app/pages.js +6483 -0
- package/console/app/plugins.js +509 -0
- package/console/app/preview-tools.js +172 -0
- package/console/app/reference-labels.js +118 -0
- package/console/app/selection.js +372 -0
- package/console/app/store.js +57 -0
- package/console/app/text-anchor.js +26 -0
- package/console/app/topic-local.js +322 -0
- package/console/app/topic-nav.js +414 -0
- package/console/app/topic-status.js +109 -0
- package/console/app/views.js +43 -0
- package/console/apple-touch-icon.png +0 -0
- package/console/avatars/agy.svg +34 -0
- package/console/avatars/alhena.svg +36 -0
- package/console/avatars/castor.svg +38 -0
- package/console/avatars/claude.svg +34 -0
- package/console/avatars/codex.svg +34 -0
- package/console/avatars/devin.svg +34 -0
- package/console/avatars/echo.svg +34 -0
- package/console/avatars/kimi.svg +33 -0
- package/console/avatars/opencode.svg +33 -0
- package/console/avatars/pinion.svg +34 -0
- package/console/avatars/sprocket.svg +34 -0
- package/console/avatars/vela.svg +44 -0
- package/console/avatars/wasat.svg +38 -0
- package/console/clients/agy.svg +1 -0
- package/console/clients/claude.svg +1 -0
- package/console/clients/codex.svg +1 -0
- package/console/clients/devin.svg +1 -0
- package/console/clients/kimi.svg +1 -0
- package/console/clients/opencode.svg +1 -0
- package/console/favicon.ico +0 -0
- package/console/favicon.svg +16 -0
- package/console/index.html +99 -0
- package/console/pdl/engine/README.md +13 -0
- package/console/pdl/engine/anchor.js +195 -0
- package/console/pdl/engine/core.js +1340 -0
- package/console/pdl/engine/frame.js +1252 -0
- package/console/pdl/engine/i18n.js +67 -0
- package/console/pdl/engine/pdl-check.js +102 -0
- package/console/pdl/engine/pdl-doc.js +353 -0
- package/console/pdl/engine/pdl-view.js +280 -0
- package/console/pdl/engine/pdl.js +1064 -0
- package/console/pdl/engine/platform.js +18 -0
- package/console/pdl/engine/proj.js +279 -0
- package/console/pdl/engine/theme.js +176 -0
- package/console/pdl/engine/tree.js +171 -0
- package/console/pdl/engine/units.js +546 -0
- package/console/pdl/library/README.md +28 -0
- package/console/pdl/library/blocks.js +1512 -0
- package/console/pdl/library/buttons.js +86 -0
- package/console/pdl/library/capsules.js +130 -0
- package/console/pdl/library/charts.js +287 -0
- package/console/pdl/library/code.js +98 -0
- package/console/pdl/library/css/base.css +12 -0
- package/console/pdl/library/css/blocks.css +98 -0
- package/console/pdl/library/css/capsules.css +42 -0
- package/console/pdl/library/css/catalog-page.css +24 -0
- package/console/pdl/library/css/chat.css +100 -0
- package/console/pdl/library/css/composer-top.css +145 -0
- package/console/pdl/library/css/controls.css +195 -0
- package/console/pdl/library/css/drag.css +29 -0
- package/console/pdl/library/css/edit-layout.css +93 -0
- package/console/pdl/library/css/floating-window.css +30 -0
- package/console/pdl/library/css/hover.css +50 -0
- package/console/pdl/library/css/label-search.css +37 -0
- package/console/pdl/library/css/layers.css +43 -0
- package/console/pdl/library/css/library-parts.css +33 -0
- package/console/pdl/library/css/library.css +117 -0
- package/console/pdl/library/css/member-select.css +34 -0
- package/console/pdl/library/css/message.css +72 -0
- package/console/pdl/library/css/page.css +104 -0
- package/console/pdl/library/css/parts.css +338 -0
- package/console/pdl/library/css/responsive.css +40 -0
- package/console/pdl/library/css/roles.css +28 -0
- package/console/pdl/library/css/rows.css +231 -0
- package/console/pdl/library/css/settings-rows.css +37 -0
- package/console/pdl/library/css/shell.css +59 -0
- package/console/pdl/library/css/stream.css +296 -0
- package/console/pdl/library/css/theme-calculator.css +18 -0
- package/console/pdl/library/css/thumb-choice.css +69 -0
- package/console/pdl/library/css/tweaks.css +16 -0
- package/console/pdl/library/css/widgets.css +211 -0
- package/console/pdl/library/css/workbench.css +59 -0
- package/console/pdl/library/feedback.js +83 -0
- package/console/pdl/library/floating-window.mjs +100 -0
- package/console/pdl/library/fold.js +238 -0
- package/console/pdl/library/format.js +53 -0
- package/console/pdl/library/frames.js +123 -0
- package/console/pdl/library/icons.js +165 -0
- package/console/pdl/library/identity.js +149 -0
- package/console/pdl/library/inputs.js +801 -0
- package/console/pdl/library/label-search.js +214 -0
- package/console/pdl/library/markdown-text.js +115 -0
- package/console/pdl/library/markdown.js +150 -0
- package/console/pdl/library/media-viewer.js +293 -0
- package/console/pdl/library/member-select.mjs +32 -0
- package/console/pdl/library/message.js +105 -0
- package/console/pdl/library/motion.js +56 -0
- package/console/pdl/library/overlays.js +165 -0
- package/console/pdl/library/page.js +142 -0
- package/console/pdl/library/plugin-form.js +154 -0
- package/console/pdl/library/registry.js +198 -0
- package/console/pdl/library/rows.js +523 -0
- package/console/pdl/library/shell.js +151 -0
- package/console/pdl/library/stream.js +58 -0
- package/console/pdl/library/tabs.js +259 -0
- package/console/pdl/library/theme-calculator.js +300 -0
- package/console/pdl/library/widgets.js +114 -0
- package/console/pdl/load-order.json +74 -0
- package/console/pdl/platform/README.md +3 -0
- package/console/pdl/platform/rich-markdown.mjs +17 -0
- package/console/pdl/platform/web-links.js +26 -0
- package/console/pdl/platform/web.js +80 -0
- package/console/sdk-browser/mentions.d.mts +16 -0
- package/console/sdk-browser/mentions.mjs +84 -0
- package/console/theme/assets.js +21 -0
- package/console/theme/theme.json +9417 -0
- package/console/theme/tokens.json +444 -0
- package/dist/core/a2a.d.ts +288 -0
- package/dist/core/a2a.js +2197 -0
- package/dist/core/address.d.ts +34 -0
- package/dist/core/address.js +33 -0
- package/dist/core/approval-inputs.d.ts +17 -0
- package/dist/core/approval-inputs.js +70 -0
- package/dist/core/archive.d.ts +19 -0
- package/dist/core/archive.js +135 -0
- package/dist/core/attachments.d.ts +17 -0
- package/dist/core/attachments.js +143 -0
- package/dist/core/blocked-handoffs.d.ts +132 -0
- package/dist/core/blocked-handoffs.js +349 -0
- package/dist/core/config-schema.d.ts +124 -0
- package/dist/core/config-schema.js +192 -0
- package/dist/core/config.d.ts +117 -0
- package/dist/core/config.js +638 -0
- package/dist/core/disk-usage.d.ts +26 -0
- package/dist/core/disk-usage.js +83 -0
- package/dist/core/documents.d.ts +9 -0
- package/dist/core/documents.js +48 -0
- package/dist/core/entry.d.ts +316 -0
- package/dist/core/entry.js +711 -0
- package/dist/core/home.d.ts +76 -0
- package/dist/core/home.js +449 -0
- package/dist/core/i18n.d.ts +36 -0
- package/dist/core/i18n.js +458 -0
- package/dist/core/instances.d.ts +224 -0
- package/dist/core/instances.js +1272 -0
- package/dist/core/members.d.ts +190 -0
- package/dist/core/members.js +358 -0
- package/dist/core/mentions.d.ts +12 -0
- package/dist/core/mentions.js +49 -0
- package/dist/core/messages.d.ts +876 -0
- package/dist/core/messages.js +916 -0
- package/dist/core/notices.d.ts +88 -0
- package/dist/core/notices.js +54 -0
- package/dist/core/ops.d.ts +117 -0
- package/dist/core/ops.js +146 -0
- package/dist/core/package.d.ts +2 -0
- package/dist/core/package.js +14 -0
- package/dist/core/parts.d.ts +25 -0
- package/dist/core/parts.js +119 -0
- package/dist/core/plugins/catalog.d.ts +14 -0
- package/dist/core/plugins/catalog.js +54 -0
- package/dist/core/plugins/external.d.ts +7 -0
- package/dist/core/plugins/external.js +921 -0
- package/dist/core/plugins/index.d.ts +5 -0
- package/dist/core/plugins/index.js +51 -0
- package/dist/core/plugins/npm.d.ts +12 -0
- package/dist/core/plugins/npm.js +99 -0
- package/dist/core/plugins/plugin-manifest.d.ts +12 -0
- package/dist/core/plugins/plugin-manifest.js +231 -0
- package/dist/core/push.d.ts +22 -0
- package/dist/core/push.js +63 -0
- package/dist/core/resource.d.ts +77 -0
- package/dist/core/resource.js +48 -0
- package/dist/core/spawn.d.ts +12 -0
- package/dist/core/spawn.js +30 -0
- package/dist/core/storage-inventory.d.ts +118 -0
- package/dist/core/storage-inventory.js +279 -0
- package/dist/core/storage.d.ts +45 -0
- package/dist/core/storage.js +602 -0
- package/dist/core/store.d.ts +15 -0
- package/dist/core/store.js +33 -0
- package/dist/core/topic-index.d.ts +19 -0
- package/dist/core/topic-index.js +70 -0
- package/dist/core/topic-ops.d.ts +139 -0
- package/dist/core/topic-ops.js +507 -0
- package/dist/core/topic-status.d.ts +32 -0
- package/dist/core/topic-status.js +105 -0
- package/dist/core/topics.d.ts +327 -0
- package/dist/core/topics.js +786 -0
- package/dist/core/turn-clients.d.ts +1 -0
- package/dist/core/turn-clients.js +5 -0
- package/dist/core/types.d.ts +2866 -0
- package/dist/core/types.js +166 -0
- package/dist/core/util.d.ts +10 -0
- package/dist/core/util.js +47 -0
- package/dist/main.d.ts +12 -0
- package/dist/main.js +82 -0
- package/dist/plugins/adapter-acp/index.d.ts +3 -0
- package/dist/plugins/adapter-acp/index.js +57 -0
- package/dist/plugins/adapter-acp/plugin.yaml +100 -0
- package/dist/plugins/adapter-agy/command.d.ts +9 -0
- package/dist/plugins/adapter-agy/command.js +34 -0
- package/dist/plugins/adapter-agy/index.d.ts +5 -0
- package/dist/plugins/adapter-agy/index.js +65 -0
- package/dist/plugins/adapter-agy/quota.d.ts +2 -0
- package/dist/plugins/adapter-agy/quota.js +155 -0
- package/dist/plugins/adapter-agy/terminal.d.ts +6 -0
- package/dist/plugins/adapter-agy/terminal.js +132 -0
- package/dist/plugins/adapter-agy/version.d.ts +9 -0
- package/dist/plugins/adapter-agy/version.js +17 -0
- package/dist/plugins/adapter-claude/index.d.ts +43 -0
- package/dist/plugins/adapter-claude/index.js +412 -0
- package/dist/plugins/adapter-claude/quota.d.ts +14 -0
- package/dist/plugins/adapter-claude/quota.js +103 -0
- package/dist/plugins/adapter-codex/index.d.ts +4 -0
- package/dist/plugins/adapter-codex/index.js +688 -0
- package/dist/plugins/adapter-codex/responses-model.d.ts +9 -0
- package/dist/plugins/adapter-codex/responses-model.js +82 -0
- package/dist/plugins/adapter-kimi/index.d.ts +4 -0
- package/dist/plugins/adapter-kimi/index.js +51 -0
- package/dist/plugins/adapter-kimi/quota.d.ts +3 -0
- package/dist/plugins/adapter-kimi/quota.js +171 -0
- package/dist/plugins/adapter-opencode/index.d.ts +12 -0
- package/dist/plugins/adapter-opencode/index.js +64 -0
- package/dist/plugins/annotations/console/cards.css +189 -0
- package/dist/plugins/annotations/console/cards.mjs +439 -0
- package/dist/plugins/annotations/console/pdl.json +52 -0
- package/dist/plugins/annotations/console/view.mjs +241 -0
- package/dist/plugins/annotations/console/window.mjs +219 -0
- package/dist/plugins/annotations/followups.d.ts +51 -0
- package/dist/plugins/annotations/followups.js +112 -0
- package/dist/plugins/annotations/index.d.ts +3 -0
- package/dist/plugins/annotations/index.js +298 -0
- package/dist/plugins/annotations/plugin.yaml +3 -0
- package/dist/plugins/ask-choice/index.d.ts +9 -0
- package/dist/plugins/ask-choice/index.js +24 -0
- package/dist/plugins/ask-choice/plugin.yaml +2 -0
- package/dist/plugins/background/index.d.ts +2 -0
- package/dist/plugins/background/index.js +398 -0
- package/dist/plugins/background/plugin.yaml +7 -0
- package/dist/plugins/builtin.json +37 -0
- package/dist/plugins/cards/console/pdl.json +516 -0
- package/dist/plugins/cards/console/view.mjs +67 -0
- package/dist/plugins/cards/index.d.ts +8 -0
- package/dist/plugins/cards/index.js +627 -0
- package/dist/plugins/cards/plugin.yaml +3 -0
- package/dist/plugins/console/browser.d.ts +2 -0
- package/dist/plugins/console/browser.js +115 -0
- package/dist/plugins/console/console/pdl.json +38 -0
- package/dist/plugins/console/console/view.mjs +26 -0
- package/dist/plugins/console/index.d.ts +4 -0
- package/dist/plugins/console/index.js +94 -0
- package/dist/plugins/console/picker.d.ts +2 -0
- package/dist/plugins/console/picker.js +48 -0
- package/dist/plugins/console/plugin.yaml +3 -0
- package/dist/plugins/console/topic-view.d.ts +3 -0
- package/dist/plugins/console/topic-view.js +388 -0
- package/dist/plugins/handoff/index.d.ts +2 -0
- package/dist/plugins/handoff/index.js +147 -0
- package/dist/plugins/handoff/plugin.yaml +8 -0
- package/dist/plugins/host-api/index.d.ts +7 -0
- package/dist/plugins/host-api/index.js +310 -0
- package/dist/plugins/host-api/plugin.yaml +2 -0
- package/dist/plugins/host-api/signing.d.ts +63 -0
- package/dist/plugins/host-api/signing.js +427 -0
- package/dist/plugins/host-api/skills/host-ops/SKILL.md +28 -0
- package/dist/plugins/host-api/skills/hostlet-plugin-dev/SKILL.md +38 -0
- package/dist/plugins/inbox/index.d.ts +15 -0
- package/dist/plugins/inbox/index.js +161 -0
- package/dist/plugins/inbox/plugin.yaml +2 -0
- package/dist/plugins/keys/account-directories.d.ts +18 -0
- package/dist/plugins/keys/account-directories.js +86 -0
- package/dist/plugins/keys/account-storage.d.ts +18 -0
- package/dist/plugins/keys/account-storage.js +112 -0
- package/dist/plugins/keys/console/pdl.json +710 -0
- package/dist/plugins/keys/console/view.mjs +619 -0
- package/dist/plugins/keys/index.d.ts +7 -0
- package/dist/plugins/keys/index.js +842 -0
- package/dist/plugins/keys/login-link.d.ts +2 -0
- package/dist/plugins/keys/login-link.js +15 -0
- package/dist/plugins/keys/plugin.yaml +3 -0
- package/dist/plugins/keys/quota-history.d.ts +91 -0
- package/dist/plugins/keys/quota-history.js +420 -0
- package/dist/plugins/mcp/console/pdl.json +412 -0
- package/dist/plugins/mcp/console/view.mjs +207 -0
- package/dist/plugins/mcp/index.d.ts +7 -0
- package/dist/plugins/mcp/index.js +494 -0
- package/dist/plugins/mcp/plugin.yaml +3 -0
- package/dist/plugins/member-health/index.d.ts +2 -0
- package/dist/plugins/member-health/index.js +478 -0
- package/dist/plugins/member-health/plugin.yaml +2 -0
- package/dist/plugins/memory/capabilities.d.ts +10 -0
- package/dist/plugins/memory/capabilities.js +70 -0
- package/dist/plugins/memory/console/pdl.json +459 -0
- package/dist/plugins/memory/console/view.mjs +254 -0
- package/dist/plugins/memory/corpus.d.ts +60 -0
- package/dist/plugins/memory/corpus.js +521 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.d.ts +8 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.js +49 -0
- package/dist/plugins/memory/engines/fts-tree-text.d.ts +12 -0
- package/dist/plugins/memory/engines/fts-tree-text.js +93 -0
- package/dist/plugins/memory/engines/fts-tree.d.ts +4 -0
- package/dist/plugins/memory/engines/fts-tree.js +387 -0
- package/dist/plugins/memory/engines/search.d.ts +9 -0
- package/dist/plugins/memory/engines/search.js +35 -0
- package/dist/plugins/memory/index.d.ts +3 -0
- package/dist/plugins/memory/index.js +465 -0
- package/dist/plugins/memory/notes.d.ts +22 -0
- package/dist/plugins/memory/notes.js +140 -0
- package/dist/plugins/memory/plugin.yaml +23 -0
- package/dist/plugins/model-profiles/index.d.ts +2 -0
- package/dist/plugins/model-profiles/index.js +919 -0
- package/dist/plugins/model-profiles/plugin.yaml +13 -0
- package/dist/plugins/model-profiles/review-source.d.ts +11 -0
- package/dist/plugins/model-profiles/review-source.js +640 -0
- package/dist/plugins/model-profiles/rules.d.ts +132 -0
- package/dist/plugins/model-profiles/rules.js +767 -0
- package/dist/plugins/model-profiles/skills/model-profile/SKILL.md +60 -0
- package/dist/plugins/model-profiles/sources.d.ts +61 -0
- package/dist/plugins/model-profiles/sources.js +375 -0
- package/dist/plugins/model-profiles/storage.d.ts +104 -0
- package/dist/plugins/model-profiles/storage.js +352 -0
- package/dist/plugins/model-profiles/types.d.ts +187 -0
- package/dist/plugins/model-profiles/types.js +12 -0
- package/dist/plugins/model-profiles/wakeup.d.ts +29 -0
- package/dist/plugins/model-profiles/wakeup.js +390 -0
- package/dist/plugins/models/index.d.ts +2 -0
- package/dist/plugins/models/index.js +63 -0
- package/dist/plugins/models/plugin.yaml +8 -0
- package/dist/plugins/ops/collector.d.ts +22 -0
- package/dist/plugins/ops/collector.js +48 -0
- package/dist/plugins/ops/config.d.ts +11 -0
- package/dist/plugins/ops/config.js +13 -0
- package/dist/plugins/ops/console/pdl.json +676 -0
- package/dist/plugins/ops/console/view.mjs +700 -0
- package/dist/plugins/ops/index.d.ts +3 -0
- package/dist/plugins/ops/index.js +20 -0
- package/dist/plugins/ops/journal.d.ts +7 -0
- package/dist/plugins/ops/journal.js +109 -0
- package/dist/plugins/ops/metrics.d.ts +3 -0
- package/dist/plugins/ops/metrics.js +81 -0
- package/dist/plugins/ops/plugin.yaml +63 -0
- package/dist/plugins/ops/samples.d.ts +54 -0
- package/dist/plugins/ops/samples.js +100 -0
- package/dist/plugins/ops/trace.d.ts +3 -0
- package/dist/plugins/ops/trace.js +104 -0
- package/dist/plugins/ops/usage.d.ts +2 -0
- package/dist/plugins/ops/usage.js +152 -0
- package/dist/plugins/plans/acceptance.d.ts +284 -0
- package/dist/plugins/plans/acceptance.js +247 -0
- package/dist/plugins/plans/blocked.d.ts +25 -0
- package/dist/plugins/plans/blocked.js +66 -0
- package/dist/plugins/plans/console/pdl.json +639 -0
- package/dist/plugins/plans/console/view.d.mts +2 -0
- package/dist/plugins/plans/console/view.mjs +855 -0
- package/dist/plugins/plans/dependencies.d.ts +5 -0
- package/dist/plugins/plans/dependencies.js +21 -0
- package/dist/plugins/plans/index.d.ts +72 -0
- package/dist/plugins/plans/index.js +2057 -0
- package/dist/plugins/plans/plugin.yaml +44 -0
- package/dist/plugins/plans/progress.d.ts +82 -0
- package/dist/plugins/plans/progress.js +516 -0
- package/dist/plugins/prompt/index.d.ts +8 -0
- package/dist/plugins/prompt/index.js +361 -0
- package/dist/plugins/prompt/plugin.yaml +2 -0
- package/dist/plugins/recovery/console/pdl.json +50 -0
- package/dist/plugins/recovery/console/view.mjs +46 -0
- package/dist/plugins/recovery/index.d.ts +9 -0
- package/dist/plugins/recovery/index.js +454 -0
- package/dist/plugins/recovery/plugin.yaml +9 -0
- package/dist/plugins/recovery/probe.d.ts +11 -0
- package/dist/plugins/recovery/probe.js +128 -0
- package/dist/plugins/restart/console/pdl.json +112 -0
- package/dist/plugins/restart/console/view.mjs +85 -0
- package/dist/plugins/restart/index.d.ts +10 -0
- package/dist/plugins/restart/index.js +669 -0
- package/dist/plugins/restart/plugin.yaml +9 -0
- package/dist/plugins/restart/scope.d.ts +6 -0
- package/dist/plugins/restart/scope.js +44 -0
- package/dist/plugins/schedule/console/pdl.json +696 -0
- package/dist/plugins/schedule/console/view.mjs +398 -0
- package/dist/plugins/schedule/events.d.ts +76 -0
- package/dist/plugins/schedule/events.js +158 -0
- package/dist/plugins/schedule/index.d.ts +32 -0
- package/dist/plugins/schedule/index.js +1199 -0
- package/dist/plugins/schedule/plugin.yaml +26 -0
- package/dist/plugins/schedule/when.d.ts +14 -0
- package/dist/plugins/schedule/when.js +83 -0
- package/dist/plugins/sessions/console/pdl.json +259 -0
- package/dist/plugins/sessions/console/view.mjs +355 -0
- package/dist/plugins/sessions/index.d.ts +3 -0
- package/dist/plugins/sessions/index.js +126 -0
- package/dist/plugins/sessions/participants.d.ts +28 -0
- package/dist/plugins/sessions/participants.js +83 -0
- package/dist/plugins/sessions/plugin.yaml +3 -0
- package/dist/plugins/signals/console/pdl.json +802 -0
- package/dist/plugins/signals/console/view.mjs +284 -0
- package/dist/plugins/signals/content.d.ts +31 -0
- package/dist/plugins/signals/content.js +151 -0
- package/dist/plugins/signals/index.d.ts +2 -0
- package/dist/plugins/signals/index.js +601 -0
- package/dist/plugins/signals/plugin.yaml +32 -0
- package/dist/plugins/signals/skills/signal-sources/SKILL.md +27 -0
- package/dist/plugins/skills/console/pdl.json +254 -0
- package/dist/plugins/skills/console/view.mjs +204 -0
- package/dist/plugins/skills/index.d.ts +11 -0
- package/dist/plugins/skills/index.js +567 -0
- package/dist/plugins/skills/install.d.ts +69 -0
- package/dist/plugins/skills/install.js +265 -0
- package/dist/plugins/skills/plugin.yaml +24 -0
- package/dist/plugins/skills/safety-scan.d.ts +17 -0
- package/dist/plugins/skills/safety-scan.js +78 -0
- package/dist/plugins/skills/skills/skill-ops/SKILL.md +29 -0
- package/dist/plugins/terminal/console/pdl.json +115 -0
- package/dist/plugins/terminal/console/view.mjs +178 -0
- package/dist/plugins/terminal/index.d.ts +3 -0
- package/dist/plugins/terminal/index.js +204 -0
- package/dist/plugins/terminal/plugin.yaml +2 -0
- package/dist/plugins/tools/index.d.ts +3 -0
- package/dist/plugins/tools/index.js +458 -0
- package/dist/plugins/tools/plugin.yaml +2 -0
- package/dist/plugins/tools/token.d.ts +61 -0
- package/dist/plugins/tools/token.js +179 -0
- package/dist/plugins/topic-tools/index.d.ts +9 -0
- package/dist/plugins/topic-tools/index.js +29 -0
- package/dist/plugins/topic-tools/plugin.yaml +2 -0
- package/dist/plugins/topic-tools/skills/topic-tidy/SKILL.md +108 -0
- package/dist/plugins/works/console/pdl.json +120 -0
- package/dist/plugins/works/console/view.mjs +91 -0
- package/dist/plugins/works/document-words.d.ts +50 -0
- package/dist/plugins/works/document-words.js +27 -0
- package/dist/plugins/works/editors.d.ts +3 -0
- package/dist/plugins/works/editors.js +162 -0
- package/dist/plugins/works/events.d.ts +58 -0
- package/dist/plugins/works/events.js +82 -0
- package/dist/plugins/works/files.d.ts +23 -0
- package/dist/plugins/works/files.js +53 -0
- package/dist/plugins/works/index.d.ts +9 -0
- package/dist/plugins/works/index.js +1088 -0
- package/dist/plugins/works/plugin.yaml +9 -0
- package/dist/plugins/works/preview.d.ts +10 -0
- package/dist/plugins/works/preview.js +30 -0
- package/dist/plugins/works/project.d.ts +3 -0
- package/dist/plugins/works/project.js +245 -0
- package/dist/plugins/works/resource.d.ts +28 -0
- package/dist/plugins/works/resource.js +71 -0
- package/dist/plugins/works/retention.d.ts +36 -0
- package/dist/plugins/works/retention.js +295 -0
- package/dist/plugins/works/skills/works-ops/SKILL.md +14 -0
- package/dist/plugins/works/viewers.d.ts +13 -0
- package/dist/plugins/works/viewers.js +87 -0
- package/dist/plugins/works/web/anchors.js +94 -0
- package/dist/plugins/works/web/document-edit.js +245 -0
- package/dist/plugins/works/web/preview-bridge.js +142 -0
- package/dist/plugins/works/web/reading-position.js +74 -0
- package/dist/plugins/works/web/view.html +1118 -0
- package/dist/runtime/runtime.d.ts +210 -0
- package/dist/runtime/runtime.js +338 -0
- package/dist/sdk/adapter-kit.d.ts +1 -0
- package/dist/sdk/adapter-kit.js +1 -0
- package/dist/sdk/client.d.ts +163 -0
- package/dist/sdk/client.js +43 -0
- package/dist/sdk/contracts.d.ts +1263 -0
- package/dist/sdk/contracts.js +1 -0
- package/dist/sdk/index.d.ts +57 -0
- package/dist/sdk/index.js +27 -0
- package/dist/sdk/source-choice.d.ts +40 -0
- package/dist/sdk/source-choice.js +217 -0
- package/dist/sdk/topic-text.d.ts +5 -0
- package/dist/sdk/topic-text.js +26 -0
- package/dist/sdk/transport.d.ts +12 -0
- package/dist/sdk/transport.js +34 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.d.ts +88 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.js +674 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.d.ts +18 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.js +39 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.d.ts +8 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.js +6 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.js +108 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.d.ts +3 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.js +4 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.js +84 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.d.ts +5 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.js +37 -0
- package/node_modules/@hostlet/adapter-kit/package.json +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.d.ts +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.js +276 -0
- package/node_modules/@hostlet/platform/dist/files.d.ts +24 -0
- package/node_modules/@hostlet/platform/dist/files.js +113 -0
- package/node_modules/@hostlet/platform/dist/index.d.ts +6 -0
- package/node_modules/@hostlet/platform/dist/index.js +4 -0
- package/node_modules/@hostlet/platform/dist/paths.d.ts +4 -0
- package/node_modules/@hostlet/platform/dist/paths.js +9 -0
- package/node_modules/@hostlet/platform/dist/process.d.ts +45 -0
- package/node_modules/@hostlet/platform/dist/process.js +297 -0
- package/node_modules/@hostlet/platform/package.json +14 -0
- package/package.json +60 -4
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
import { createReadStream, existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { hostname, networkInterfaces } from 'node:os';
|
|
4
|
+
import { extname, join, normalize, sep } from 'node:path';
|
|
5
|
+
import { OWNER_ID } from "./members.js";
|
|
6
|
+
import { check, httpArgs, memberArgs, object, openapi } from "./ops.js";
|
|
7
|
+
import { prepareInputs } from "./approval-inputs.js";
|
|
8
|
+
import { errorText, log } from "./util.js";
|
|
9
|
+
/** Returned by a handler that has written the response itself (a stream). */
|
|
10
|
+
export const HANDLED = Symbol('handled');
|
|
11
|
+
export class HttpError extends Error {
|
|
12
|
+
status;
|
|
13
|
+
detail;
|
|
14
|
+
i18n;
|
|
15
|
+
constructor(status, message, detail, i18n) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.detail = detail;
|
|
19
|
+
this.i18n = i18n;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** The arguments do not fit what the operation declares. */
|
|
23
|
+
export class ArgsError extends HttpError {
|
|
24
|
+
constructor(message, detail, i18n) {
|
|
25
|
+
super(400, message, detail, i18n);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const entryOwners = new WeakMap();
|
|
29
|
+
/** Host-internal lookup of which plugin registered an operation through `ctx.get('entry')`. */
|
|
30
|
+
export const entryOpOwner = (entry, name) => entryOwners.get(entry)?.get(name);
|
|
31
|
+
const TYPES = {
|
|
32
|
+
'.html': 'text/html; charset=utf-8', '.htm': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8',
|
|
33
|
+
'.json': 'application/json; charset=utf-8', '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp',
|
|
34
|
+
'.ico': 'image/x-icon', '.woff2': 'font/woff2', '.yaml': 'text/yaml; charset=utf-8', '.md': 'text/markdown; charset=utf-8',
|
|
35
|
+
'.wasm': 'application/wasm', '.mjs': 'text/javascript; charset=utf-8',
|
|
36
|
+
'.pdf': 'application/pdf', '.avif': 'image/avif',
|
|
37
|
+
'.mp4': 'video/mp4', '.webm': 'video/webm', '.mov': 'video/quicktime', '.mp3': 'audio/mpeg', '.wav': 'audio/wav', '.m4a': 'audio/mp4', '.ogg': 'audio/ogg',
|
|
38
|
+
'.txt': 'text/plain; charset=utf-8', '.csv': 'text/csv; charset=utf-8', '.tsv': 'text/tab-separated-values; charset=utf-8',
|
|
39
|
+
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
40
|
+
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
41
|
+
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
42
|
+
};
|
|
43
|
+
export const fileMime = (file) => TYPES[extname(file).toLowerCase()] ?? 'application/octet-stream';
|
|
44
|
+
/** Stream a file. A byte range applies only when serving the file unchanged. */
|
|
45
|
+
export function sendFile(res, file, raw, tail) {
|
|
46
|
+
const headers = {
|
|
47
|
+
'content-type': fileMime(file),
|
|
48
|
+
'cache-control': 'no-cache',
|
|
49
|
+
...(raw && tail === undefined ? { 'accept-ranges': 'bytes' } : {}),
|
|
50
|
+
};
|
|
51
|
+
const size = statSync(file).size;
|
|
52
|
+
const range = tail === undefined ? /^bytes=(\d+)-(\d*)$/i.exec(raw?.headers.range ?? '') : null;
|
|
53
|
+
const start = Number(range?.[1] ?? 0);
|
|
54
|
+
const end = Math.min(range?.[2] ? Number(range[2]) : size - 1, size - 1);
|
|
55
|
+
if (range && start > end) {
|
|
56
|
+
res.writeHead(416, { ...headers, 'content-range': `bytes */${size}`, 'content-length': 0 }).end();
|
|
57
|
+
return HANDLED;
|
|
58
|
+
}
|
|
59
|
+
res.writeHead(range ? 206 : 200, {
|
|
60
|
+
...headers, 'content-length': end - start + 1 + (tail === undefined ? 0 : Buffer.byteLength(tail)),
|
|
61
|
+
...(range ? { 'content-range': `bytes ${start}-${end}/${size}` } : {}),
|
|
62
|
+
});
|
|
63
|
+
if (size === 0)
|
|
64
|
+
res.end(tail);
|
|
65
|
+
else {
|
|
66
|
+
const stream = createReadStream(file, { start, end });
|
|
67
|
+
res.once('close', () => stream.destroy());
|
|
68
|
+
if (tail !== undefined)
|
|
69
|
+
stream.once('end', () => res.end(tail));
|
|
70
|
+
stream.on('error', (error) => res.destroy(error)).pipe(res, { end: tail === undefined });
|
|
71
|
+
}
|
|
72
|
+
return HANDLED;
|
|
73
|
+
}
|
|
74
|
+
/** A topic can contain active HTML and SVG. A document from it must have an opaque browser origin. */
|
|
75
|
+
export function sandboxUserContent(res) {
|
|
76
|
+
res.setHeader('content-security-policy', 'sandbox allow-scripts allow-forms allow-downloads');
|
|
77
|
+
res.setHeader('x-content-type-options', 'nosniff');
|
|
78
|
+
}
|
|
79
|
+
export function sendUserFile(res, file, raw) {
|
|
80
|
+
sandboxUserContent(res);
|
|
81
|
+
return sendFile(res, file, raw);
|
|
82
|
+
}
|
|
83
|
+
/** "This cannot be done now", said by the code that was asked: not a bug, and not one of Node's own errors. */
|
|
84
|
+
const said = (error) => error instanceof Error && Object.getPrototypeOf(error) === Error.prototype && !('code' in error);
|
|
85
|
+
const refuse = (res, text) => void res.writeHead(403, { 'content-type': 'application/json; charset=utf-8' }).end(JSON.stringify({ error: text }));
|
|
86
|
+
export function entryPlugin(home) {
|
|
87
|
+
let server;
|
|
88
|
+
return {
|
|
89
|
+
name: 'entry',
|
|
90
|
+
core: true,
|
|
91
|
+
needs: ['push'],
|
|
92
|
+
async start(ctx) {
|
|
93
|
+
const routes = [];
|
|
94
|
+
const mounts = [];
|
|
95
|
+
const host = process.env.HOSTLET_HOST ?? home.env.HOSTLET_HOST ?? '127.0.0.1';
|
|
96
|
+
const port = Number(process.env.HOSTLET_PORT ?? home.env.HOSTLET_PORT ?? 4170);
|
|
97
|
+
const match = (method, path) => {
|
|
98
|
+
const segments = path.split('/').filter(Boolean);
|
|
99
|
+
for (const route of routes) {
|
|
100
|
+
if (route.method !== method || route.parts.length !== segments.length)
|
|
101
|
+
continue;
|
|
102
|
+
const params = {};
|
|
103
|
+
if (route.parts.every((part, i) => (part.startsWith(':') ? ((params[part.slice(1)] = decodeURIComponent(segments[i])), true) : part === segments[i]))) {
|
|
104
|
+
return { route, params };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
};
|
|
109
|
+
const serveStatic = (path, res, raw) => {
|
|
110
|
+
for (const mount of [...mounts].sort((a, b) => b.prefix.length - a.prefix.length)) {
|
|
111
|
+
if (!path.startsWith(mount.prefix))
|
|
112
|
+
continue;
|
|
113
|
+
const suffix = path.slice(mount.prefix.length);
|
|
114
|
+
const target = typeof mount.source === 'string' ? { dir: mount.source, path: suffix } : mount.source(suffix);
|
|
115
|
+
if (!target)
|
|
116
|
+
continue;
|
|
117
|
+
const send = (file) => {
|
|
118
|
+
if (mount.userContent)
|
|
119
|
+
sandboxUserContent(res);
|
|
120
|
+
sendFile(res, file, raw, target.append?.(file, fileMime(file)));
|
|
121
|
+
return true;
|
|
122
|
+
};
|
|
123
|
+
const { dir } = target;
|
|
124
|
+
let decoded;
|
|
125
|
+
try {
|
|
126
|
+
decoded = decodeURIComponent(target.path);
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
continue;
|
|
130
|
+
} // malformed encoding in a request path
|
|
131
|
+
const rel = normalize(decoded).replace(/^([/\\])+/, '');
|
|
132
|
+
const file = join(dir, rel);
|
|
133
|
+
const prefix = (path) => path.endsWith(sep) ? path : path + sep;
|
|
134
|
+
if (!file.startsWith(prefix(dir)) && file !== dir)
|
|
135
|
+
continue;
|
|
136
|
+
let realRoot;
|
|
137
|
+
// Apply the same containment check to the asset and to any index page.
|
|
138
|
+
const safeFile = (file) => existsSync(file) && statSync(file).isFile() &&
|
|
139
|
+
realpathSync(file).startsWith(realRoot ??= prefix(realpathSync(dir)));
|
|
140
|
+
if (safeFile(file))
|
|
141
|
+
return send(file);
|
|
142
|
+
const directoryIndex = join(file, 'index.html');
|
|
143
|
+
if (safeFile(directoryIndex))
|
|
144
|
+
return send(directoryIndex);
|
|
145
|
+
// A directory is its index page (a second app under its own prefix), and so is any address under it.
|
|
146
|
+
const top = join(dir, rel.split(/[/\\]/)[0] ?? '', 'index.html');
|
|
147
|
+
if (rel && !extname(path) && safeFile(top))
|
|
148
|
+
return send(top);
|
|
149
|
+
// A console address like /topic/<id> is the app's own route: give it the page.
|
|
150
|
+
const index = join(dir, 'index.html');
|
|
151
|
+
if (mount.prefix === '/' && !extname(path) && safeFile(index))
|
|
152
|
+
return send(index);
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
};
|
|
156
|
+
// Who the host is, as a browser names it: a request has to be meant for one of these (not a name that only
|
|
157
|
+
// resolves here), and one that changes something has to come from a page of the host's own.
|
|
158
|
+
const own = new Set();
|
|
159
|
+
server = createServer(async (raw, res) => {
|
|
160
|
+
if (!own.has(raw.headers.host ?? ''))
|
|
161
|
+
return refuse(res, `这个宿主不叫 ${raw.headers.host ?? '(没有 Host)'}`);
|
|
162
|
+
const url = new URL(raw.url ?? '/', `http://${raw.headers.host}`);
|
|
163
|
+
if (url.pathname.startsWith('/api/') && raw.method !== 'GET' && raw.method !== 'HEAD' && !own.has(String(raw.headers.origin ?? '').replace(/^http:\/\//, ''))) {
|
|
164
|
+
return refuse(res, '改动宿主的请求只收它自己的页面发来的(Origin 要是宿主自己的地址)');
|
|
165
|
+
}
|
|
166
|
+
const found = match(raw.method ?? 'GET', url.pathname);
|
|
167
|
+
const attributes = { method: raw.method ?? 'GET', route: found?.route.pattern ?? '(unmatched)',
|
|
168
|
+
workload: found?.route.workload ?? 'unclassified', status: 0 };
|
|
169
|
+
return ctx.runtime.span('http.server', attributes, async () => {
|
|
170
|
+
try {
|
|
171
|
+
if (!found) {
|
|
172
|
+
if (raw.method === 'GET' && !url.pathname.startsWith('/api/') && serveStatic(url.pathname, res, raw))
|
|
173
|
+
return;
|
|
174
|
+
res.writeHead(404, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'not found' }));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (found.route.localPage) {
|
|
178
|
+
const peer = raw.socket.remoteAddress?.replace(/^::ffff:/, '');
|
|
179
|
+
const address = new URL(`http://${raw.headers.host}`);
|
|
180
|
+
if ((peer !== '::1' && !peer?.startsWith('127.')) || !['127.0.0.1', 'localhost', '[::1]'].includes(address.hostname) || raw.headers.origin !== address.origin) {
|
|
181
|
+
return refuse(res, '只收本机回环地址的同源控制台请求');
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const body = raw.method === 'GET' || raw.method === 'DELETE' ? undefined : await readBody(raw, found.route.maxBody);
|
|
185
|
+
const result = await found.route.handler({ params: found.params, query: url.searchParams, body, raw, res });
|
|
186
|
+
if (result === HANDLED)
|
|
187
|
+
return;
|
|
188
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' }).end(JSON.stringify(result ?? { ok: true }));
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
const status = error instanceof HttpError ? error.status : 500;
|
|
192
|
+
if (status === 500)
|
|
193
|
+
log('entry', `${raw.method} ${url.pathname} failed`, error);
|
|
194
|
+
if (!res.headersSent)
|
|
195
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
|
|
196
|
+
const payload = { error: errorText(error) };
|
|
197
|
+
if (error instanceof HttpError && error.i18n)
|
|
198
|
+
payload.i18n = error.i18n;
|
|
199
|
+
res.end(JSON.stringify(payload));
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
attributes.status = res.statusCode;
|
|
203
|
+
}
|
|
204
|
+
}, typeof raw.headers.traceparent === 'string' ? raw.headers.traceparent : undefined);
|
|
205
|
+
});
|
|
206
|
+
await new Promise((resolve, reject) => {
|
|
207
|
+
// A taken port is the usual first-run stumble: name it and the way out.
|
|
208
|
+
server.once('error', (error) => {
|
|
209
|
+
if (error.code !== 'EADDRINUSE')
|
|
210
|
+
return reject(error);
|
|
211
|
+
const env = join(home.root, '.env');
|
|
212
|
+
reject(new Error(`${host}:${port} is already in use (another hostlet may be running). Start on another port with HOSTLET_PORT=<port>, or set it in ${env}. / 端口 ${port} 已被占用(可能已有一个 hostlet 在运行)。用 HOSTLET_PORT=<端口> 换一个端口启动,或写进 ${env}。`, { cause: error }));
|
|
213
|
+
});
|
|
214
|
+
server.listen(port, host, resolve);
|
|
215
|
+
});
|
|
216
|
+
const address = server.address();
|
|
217
|
+
const listening = typeof address === 'object' && address ? address.port : port;
|
|
218
|
+
const url = `http://${host}:${listening}`;
|
|
219
|
+
const everywhere = host === '0.0.0.0' || host === '::';
|
|
220
|
+
const names = ['127.0.0.1', 'localhost', '[::1]', host, ...(everywhere ? [hostname(), ...Object.values(networkInterfaces()).flatMap((list) => (list ?? []).filter((a) => a.family === 'IPv4').map((a) => a.address))] : [])];
|
|
221
|
+
for (const name of names)
|
|
222
|
+
for (const one of listening === 80 ? [name, `${name}:80`] : [`${name}:${listening}`])
|
|
223
|
+
own.add(one.toLowerCase());
|
|
224
|
+
const groups = new Map();
|
|
225
|
+
const table = new Map();
|
|
226
|
+
const opOwners = new Map();
|
|
227
|
+
const available = (op, topicId) => !topicId || !ctx.runtime.has('topics') || ctx.get('topics').pluginEnabled(topicId, op.plugin);
|
|
228
|
+
let approver;
|
|
229
|
+
let policy;
|
|
230
|
+
const TIER_RANK = { owner: 0, sign: 1, free: 2 };
|
|
231
|
+
const declaredTier = (op) => op.agent === true ? 'free' : op.sign ? 'sign' : 'owner';
|
|
232
|
+
const maxTier = (op) => {
|
|
233
|
+
const base = declaredTier(op);
|
|
234
|
+
const loose = op.agent === 'approve' ? (op.loosen ?? (op.signers ? 'sign' : 'free')) : base;
|
|
235
|
+
return TIER_RANK[loose] > TIER_RANK[base] ? loose : base;
|
|
236
|
+
};
|
|
237
|
+
const minSignMembers = (op) => op.sign?.members ?? (op.signers ? 1 : 2);
|
|
238
|
+
const minSignWait = (op) => op.sign?.wait ?? 0;
|
|
239
|
+
const resolveSign = (op, entries) => {
|
|
240
|
+
const named = !!op.signers;
|
|
241
|
+
const floor = minSignMembers(op);
|
|
242
|
+
const fallback = named ? floor : Math.max(floor, policy?.defaultSignMembers?.() ?? 2);
|
|
243
|
+
const members = entries.reduce((n, one) => Math.max(n, one.members ?? fallback), fallback);
|
|
244
|
+
const wait = entries.reduce((w, one) => Math.max(w, one.wait ?? minSignWait(op)), minSignWait(op));
|
|
245
|
+
return {
|
|
246
|
+
members, wait, onReject: op.sign?.onReject ?? 'block',
|
|
247
|
+
...(op.sign?.fields ? { fields: op.sign.fields } : {}),
|
|
248
|
+
...(op.sign?.ownerInbox === false ? { ownerInbox: false } : {}),
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
/** Why an override cannot take effect as written; `undefined` when it does (or names nothing declared now). */
|
|
252
|
+
const problemOf = (entry) => {
|
|
253
|
+
const op = table.get(entry.op);
|
|
254
|
+
if (!op)
|
|
255
|
+
return undefined;
|
|
256
|
+
if (!op.agent)
|
|
257
|
+
return '只能人做的操作不能覆盖';
|
|
258
|
+
const tier = entry.tier ?? 'free';
|
|
259
|
+
if (tier === 'owner')
|
|
260
|
+
return undefined;
|
|
261
|
+
if (TIER_RANK[tier] > TIER_RANK[maxTier(op)])
|
|
262
|
+
return '这个操作不能放宽';
|
|
263
|
+
if (tier === 'sign') {
|
|
264
|
+
if (!op.subjects)
|
|
265
|
+
return '未声明 subjects,不能进入会签,按需机主批准处理';
|
|
266
|
+
const minM = minSignMembers(op);
|
|
267
|
+
if (entry.members !== undefined && (!Number.isInteger(entry.members) || entry.members < minM)) {
|
|
268
|
+
return `会签票数不能低于 ${minM}`;
|
|
269
|
+
}
|
|
270
|
+
const minW = minSignWait(op);
|
|
271
|
+
if (entry.wait !== undefined && (!Number.isInteger(entry.wait) || entry.wait < minW)) {
|
|
272
|
+
return `公示时间不能低于 ${minW} 分钟`;
|
|
273
|
+
}
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
if (op.agent !== 'approve')
|
|
277
|
+
return undefined;
|
|
278
|
+
if (op.grant && !entry.key)
|
|
279
|
+
return '这个操作只能按授权对象放行,需要填 key';
|
|
280
|
+
if (op.grant && op.grantKey && !op.grantKey.test(entry.key))
|
|
281
|
+
return '授权对象不完整';
|
|
282
|
+
if (!op.grant && entry.key)
|
|
283
|
+
return '这个操作没有授权对象,不能带 key';
|
|
284
|
+
};
|
|
285
|
+
/** Tightening always applies; loosening only while someone can still ask the owner (an approver is registered). */
|
|
286
|
+
const overrides = () => (policy?.read() ?? []).filter((entry) => {
|
|
287
|
+
const op = table.get(entry.op);
|
|
288
|
+
if (!op || !op.agent)
|
|
289
|
+
return false;
|
|
290
|
+
const tier = entry.tier ?? 'free';
|
|
291
|
+
// Tightening a free op to `sign` without `subjects` still tightens to `owner` (never silently left `free`).
|
|
292
|
+
const downgradeOnly = tier === 'sign' && op.agent === true && !op.subjects;
|
|
293
|
+
if (!downgradeOnly && problemOf(entry))
|
|
294
|
+
return false;
|
|
295
|
+
const loosening = TIER_RANK[tier] > TIER_RANK[declaredTier(op)];
|
|
296
|
+
return !loosening || approver !== undefined;
|
|
297
|
+
});
|
|
298
|
+
const tightened = (op, key) => (policy?.read() ?? []).some((entry) => entry.op === op && entry.tier === 'owner' && (entry.key === undefined || entry.key === key) && !problemOf(entry));
|
|
299
|
+
const grantOf = (op, args) => {
|
|
300
|
+
if (!op.grant)
|
|
301
|
+
return undefined;
|
|
302
|
+
try {
|
|
303
|
+
return op.grant(args);
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
const bindOf = (op, args) => {
|
|
310
|
+
if (!op.bind)
|
|
311
|
+
return undefined;
|
|
312
|
+
try {
|
|
313
|
+
return op.bind(args);
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
const signPolicy = (opName, opts) => {
|
|
320
|
+
const op = table.get(opName);
|
|
321
|
+
if (!op || !op.agent || opts?.outside)
|
|
322
|
+
return undefined;
|
|
323
|
+
const own = overrides().filter((entry) => entry.op === op.name);
|
|
324
|
+
const key = opts?.grantKey;
|
|
325
|
+
if (own.some((entry) => entry.tier === 'owner' && (entry.key === undefined || entry.key === key)))
|
|
326
|
+
return undefined;
|
|
327
|
+
const signEntries = own.filter((entry) => entry.tier === 'sign' && (entry.key === undefined || entry.key === key));
|
|
328
|
+
if (op.agent === true && !signEntries.length)
|
|
329
|
+
return undefined;
|
|
330
|
+
const eligible = op.sign !== undefined || signEntries.length > 0;
|
|
331
|
+
if (!eligible || !op.subjects)
|
|
332
|
+
return undefined;
|
|
333
|
+
return resolveSign(op, signEntries);
|
|
334
|
+
};
|
|
335
|
+
/**
|
|
336
|
+
* Whether a member's call has to be approved by a person or co-signers: what the operation declares, tightened
|
|
337
|
+
* by any matching override, loosened only within `loosen` and only by an override for this very grant key.
|
|
338
|
+
*/
|
|
339
|
+
const approvalNeeded = (op, grant, outside) => {
|
|
340
|
+
const own = overrides().filter((entry) => entry.op === op.name);
|
|
341
|
+
if (own.some((entry) => entry.tier === 'owner' && (entry.key === undefined || entry.key === grant?.key)))
|
|
342
|
+
return { needed: true };
|
|
343
|
+
const signEntries = own.filter((entry) => entry.tier === 'sign' && (entry.key === undefined || entry.key === grant?.key));
|
|
344
|
+
if (op.agent !== 'approve') {
|
|
345
|
+
if (!signEntries.length)
|
|
346
|
+
return { needed: false };
|
|
347
|
+
return { needed: true, ...(!outside && op.subjects ? { sign: resolveSign(op, signEntries) } : {}) };
|
|
348
|
+
}
|
|
349
|
+
const canFree = !outside && maxTier(op) === 'free' && !(op.grant && (!grant || grant.once));
|
|
350
|
+
if (canFree && own.some((entry) => (entry.tier ?? 'free') === 'free' && entry.key === grant?.key))
|
|
351
|
+
return { needed: false };
|
|
352
|
+
const standing = canFree ? (grant ?? { label: `所有 ${op.name} 调用` }) : undefined;
|
|
353
|
+
const sign = !outside && op.subjects && (op.sign || signEntries.length > 0) ? resolveSign(op, signEntries) : undefined;
|
|
354
|
+
return { needed: true, ...(standing ? { standing } : {}), ...(sign ? { sign } : {}) };
|
|
355
|
+
};
|
|
356
|
+
const route = (method, pattern, handler, limits) => {
|
|
357
|
+
const made = { method, pattern, parts: pattern.split('/').filter(Boolean), handler, ...limits };
|
|
358
|
+
routes.push(made);
|
|
359
|
+
return () => void routes.splice(routes.indexOf(made), 1);
|
|
360
|
+
};
|
|
361
|
+
const outsideOf = (ids, own) => {
|
|
362
|
+
for (const id of ids) {
|
|
363
|
+
const path = ctx.get('topics').path(id, true);
|
|
364
|
+
if (path.length && !path.some((one) => one.id === own))
|
|
365
|
+
return path.at(-1);
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
/**
|
|
369
|
+
* The topic a member's call is done to. Its own, when the operation is about one topic: the member does not
|
|
370
|
+
* say which, so what it does cannot land in another topic by a wrong id. Where the declaration lets it point
|
|
371
|
+
* further it may name one. A target outside the direct reach needs approval.
|
|
372
|
+
*/
|
|
373
|
+
const placed = (op, args, own, resolved) => {
|
|
374
|
+
const field = op.topic;
|
|
375
|
+
const named = args[field];
|
|
376
|
+
if (!op.reach) {
|
|
377
|
+
if (!resolved && named !== undefined)
|
|
378
|
+
throw new ArgsError(`${op.name} 做的是你所在的主题,不带 ${field}`, { kind: 'topic_own', op: op.name, field });
|
|
379
|
+
if (resolved && typeof named === 'string' && named !== own)
|
|
380
|
+
return { args, outside: ctx.get('topics').get(named) };
|
|
381
|
+
return { args: { ...args, [field]: own } };
|
|
382
|
+
}
|
|
383
|
+
if (named === undefined)
|
|
384
|
+
return { args: op.named ? args : { ...args, [field]: own } };
|
|
385
|
+
const outside = typeof named !== 'string' ? undefined
|
|
386
|
+
: op.reach === 'below' ? outsideOf([named], own)
|
|
387
|
+
: op.reach === 'own' && named !== own ? ctx.get('topics').get(named) : undefined;
|
|
388
|
+
return { args, outside };
|
|
389
|
+
};
|
|
390
|
+
const allMembers = () => {
|
|
391
|
+
const members = ctx.get('members');
|
|
392
|
+
return [...members.list(), ...members.people()];
|
|
393
|
+
};
|
|
394
|
+
const memberId = (handle) => {
|
|
395
|
+
const name = handle.replace(/^@/, '');
|
|
396
|
+
const all = allMembers();
|
|
397
|
+
const found = all.find((one) => one.handle === name);
|
|
398
|
+
if (!found) {
|
|
399
|
+
const members = ctx.get('members');
|
|
400
|
+
const handles = all.filter((member) => members.acceptsDelivery(member.id)).map((member) => `@${member.handle}`);
|
|
401
|
+
throw new ArgsError(`没有 ${handle} 这个 @ 名。可以用 ${handles.join('、')}`);
|
|
402
|
+
}
|
|
403
|
+
return found.id;
|
|
404
|
+
};
|
|
405
|
+
const knownMember = (id) => {
|
|
406
|
+
if (!ctx.get('members').get(id))
|
|
407
|
+
throw new ArgsError(`成员已经不在了:${id}`);
|
|
408
|
+
return id;
|
|
409
|
+
};
|
|
410
|
+
const prepare = (name, args, caller, opts) => {
|
|
411
|
+
const op = table.get(name);
|
|
412
|
+
if (!op)
|
|
413
|
+
throw new HttpError(404, `没有 ${name} 这个接口`);
|
|
414
|
+
if (caller.member && !available(op, caller.topicId))
|
|
415
|
+
throw new HttpError(403, `${name} 所属插件已在本主题关闭`);
|
|
416
|
+
if (caller.member && !op.agent)
|
|
417
|
+
throw new HttpError(403, `${name} 不给成员:请人在控制台里做`);
|
|
418
|
+
const asked = args ?? {};
|
|
419
|
+
const unapproved = caller.member && !caller.approved;
|
|
420
|
+
const resolved = !!opts?.resolved;
|
|
421
|
+
const placement = unapproved && caller.topicId && op.topic && typeof asked === 'object' && !Array.isArray(asked)
|
|
422
|
+
? placed(op, asked, caller.topicId, resolved) : undefined;
|
|
423
|
+
const addressed = placement?.args ?? asked;
|
|
424
|
+
const publicArgs = unapproved && !resolved
|
|
425
|
+
? memberArgs(op.input, addressed, opts?.resolvedMembers ? knownMember : memberId)
|
|
426
|
+
: addressed;
|
|
427
|
+
if (opts?.values && (caller.approved !== OWNER_ID || !opts.fill?.length))
|
|
428
|
+
throw new ArgsError('补填值只用于机主已批准的字段');
|
|
429
|
+
const form = opts?.fill?.length ? prepareInputs(op.input, publicArgs, opts.fill, op.topic, op.fillField && ((keys) => op.fillField(publicArgs, keys)), opts.values) : undefined;
|
|
430
|
+
const parsed = form?.args ?? publicArgs;
|
|
431
|
+
const problem = check(form?.schema ?? op.input, parsed, '', !!caller.member);
|
|
432
|
+
if (problem)
|
|
433
|
+
throw new ArgsError(problem);
|
|
434
|
+
const full = op.normalize ? op.normalize(parsed, caller, form?.inputs) : parsed;
|
|
435
|
+
op.validate?.(full, caller, form && { inputs: form.inputs, submitted: opts?.values !== undefined });
|
|
436
|
+
const targets = unapproved || caller.approved ? op.targets?.(full) : undefined;
|
|
437
|
+
if (caller.approved && (targets || caller.approvedTargets)) {
|
|
438
|
+
const reviewed = new Set(caller.approvedTargets);
|
|
439
|
+
if (!targets || targets.length !== reviewed.size || targets.some((id) => !reviewed.has(id))) {
|
|
440
|
+
throw new HttpError(409, '受影响的主题名单已变化,请按新名单重新提请审批');
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const outside = placement?.outside ?? (unapproved && caller.topicId && targets
|
|
444
|
+
? op.reach === 'below' ? outsideOf(targets, caller.topicId)
|
|
445
|
+
: !op.reach || op.reach === 'own' ? targets.filter((id) => id !== caller.topicId).map((id) => ctx.get('topics').get(id)).find(Boolean)
|
|
446
|
+
: undefined : undefined);
|
|
447
|
+
const note = outside && `对象是「${outside.title}」(${outside.id}),超出 ${caller.member.name} 所在主题的直接操作范围`;
|
|
448
|
+
const grant = caller.member ? grantOf(op, full) : undefined;
|
|
449
|
+
const binding = caller.member ? (grant?.key ?? bindOf(op, full)) : undefined;
|
|
450
|
+
if (caller.approved && (op.grant || op.bind || caller.grant !== undefined) && caller.grant !== (binding ?? null)) {
|
|
451
|
+
throw new HttpError(409, '批准的对象已经变了(插件重装、代码或状态变化),这次没有执行。请重新提请,让机主按现在的对象确认');
|
|
452
|
+
}
|
|
453
|
+
const subjects = op.subjects?.(full);
|
|
454
|
+
const signers = unapproved && !form ? op.signers?.(full) : undefined;
|
|
455
|
+
const decision = unapproved ? approvalNeeded(op, grant, outside) : undefined;
|
|
456
|
+
const standing = !outside && !form ? decision?.standing : undefined;
|
|
457
|
+
const sign = !outside && !form ? decision?.sign : undefined;
|
|
458
|
+
const needsApproval = unapproved && (form || decision?.needed || outside !== undefined) ? true : undefined;
|
|
459
|
+
return {
|
|
460
|
+
op, asked, args: full, targets, outside, note,
|
|
461
|
+
...(form && { inputs: form.inputs }),
|
|
462
|
+
...(subjects ? { subjects } : {}),
|
|
463
|
+
...(signers ? { signers } : {}),
|
|
464
|
+
...(grant ? { grant } : {}),
|
|
465
|
+
...(binding !== undefined ? { binding } : {}),
|
|
466
|
+
...(standing ? { standing } : {}),
|
|
467
|
+
...(sign ? { sign } : {}),
|
|
468
|
+
...(needsApproval ? { needsApproval } : {}),
|
|
469
|
+
};
|
|
470
|
+
};
|
|
471
|
+
const call = (name, args, caller, opts) => {
|
|
472
|
+
const declaration = table.get(name);
|
|
473
|
+
const attributes = { operation: declaration?.name ?? '(unknown)',
|
|
474
|
+
...(declaration?.workload === 'telemetry' && { workload: declaration.workload }) };
|
|
475
|
+
return ctx.runtime.span('entry.invoke', attributes, async () => {
|
|
476
|
+
// What members ask for is written down, one line a call: which, who, and how it went.
|
|
477
|
+
const who = caller.member ? `${name} by @${caller.member.handle} in ${caller.topicId}` : undefined;
|
|
478
|
+
try {
|
|
479
|
+
const request = prepare(name, args, caller, opts);
|
|
480
|
+
const { op, args: full, outside, grant } = request;
|
|
481
|
+
const target = outside && `「${outside.title}」(${outside.id})`;
|
|
482
|
+
const pending = request.needsApproval;
|
|
483
|
+
if (pending && !approver) {
|
|
484
|
+
if (outside && !approvalNeeded(op, grant, outside).needed) {
|
|
485
|
+
const scope = op.reach === 'own' ? '不是你所在的主题' : '不是你所在的主题,也不在它下面';
|
|
486
|
+
throw new HttpError(403, `${target}${scope},${op.name} 你只能对这个范围里的主题做:请人在控制台里做`, {
|
|
487
|
+
kind: 'topic_reach', op: op.name, target: { id: outside.id, title: outside.title }, needsApproval: false,
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
throw new HttpError(403, `${name} 要人批准才执行,现在没有地方提请:请人在控制台里做`);
|
|
491
|
+
}
|
|
492
|
+
if (outside && !caller.why?.trim()) {
|
|
493
|
+
throw new HttpError(403, `${target}超出你的直接操作范围。对它执行 ${op.name} 要人批准`, {
|
|
494
|
+
kind: 'topic_reach', op: op.name, target: { id: outside.id, title: outside.title }, needsApproval: true,
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
if (pending) {
|
|
498
|
+
const result = approver({ ...request, caller });
|
|
499
|
+
log('op', `${who}: asked`);
|
|
500
|
+
return { op, args: full, result, pending: true };
|
|
501
|
+
}
|
|
502
|
+
// Allowed by a standing permission: the run is held to the key it was allowed for.
|
|
503
|
+
const bound = caller.member && !caller.approved && op.agent === 'approve' && grant ? { ...caller, grant: grant.key } : caller;
|
|
504
|
+
const result = await op.run(full, bound);
|
|
505
|
+
if (who)
|
|
506
|
+
log('op', `${who}: ok`);
|
|
507
|
+
return { op, args: full, result };
|
|
508
|
+
}
|
|
509
|
+
catch (error) {
|
|
510
|
+
// A target can include input in a diagnostic. Keep it out of spans, logs and the approval receipt.
|
|
511
|
+
const report = opts?.values && Object.keys(opts.values).length
|
|
512
|
+
? new HttpError(error instanceof HttpError ? error.status : 400, '操作未成功,请核对目标和补填参数') : error;
|
|
513
|
+
if (who)
|
|
514
|
+
log('op', `${who}: ${report instanceof ArgsError ? 'wrong arguments' : 'failed'}`, errorText(report));
|
|
515
|
+
throw report;
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
};
|
|
519
|
+
const invoke = async (name, args, caller = {}, opts) => (await call(name, args, caller, opts)).result;
|
|
520
|
+
const registerOp = (decl, owner) => {
|
|
521
|
+
if (!/^[a-z_][a-z0-9_]*\.[a-z_]+$/.test(decl.name))
|
|
522
|
+
throw new Error(`an operation is named group.name: ${decl.name}`);
|
|
523
|
+
if (table.has(decl.name))
|
|
524
|
+
throw new Error(`operation already declared: ${decl.name}`);
|
|
525
|
+
const op = { ...decl, input: decl.input ?? object() };
|
|
526
|
+
Object.defineProperty(op, 'plugin', {
|
|
527
|
+
value: owner?.core === false ? owner.plugin : undefined, configurable: false, enumerable: false, writable: false,
|
|
528
|
+
});
|
|
529
|
+
table.set(op.name, op);
|
|
530
|
+
if (owner)
|
|
531
|
+
opOwners.set(op.name, owner);
|
|
532
|
+
const [method, pattern] = op.http;
|
|
533
|
+
const undo = route(method, pattern, async ({ params, query, body }) => {
|
|
534
|
+
try {
|
|
535
|
+
return await invoke(op.name, httpArgs(op.input, params, query, body));
|
|
536
|
+
}
|
|
537
|
+
catch (error) {
|
|
538
|
+
throw said(error) ? new HttpError(400, error.message) : error;
|
|
539
|
+
}
|
|
540
|
+
}, { localPage: op.localPage, maxBody: op.maxBody, workload: op.workload });
|
|
541
|
+
return () => {
|
|
542
|
+
table.delete(op.name);
|
|
543
|
+
opOwners.delete(op.name);
|
|
544
|
+
undo();
|
|
545
|
+
};
|
|
546
|
+
};
|
|
547
|
+
let currentOpOwner;
|
|
548
|
+
const entry = {
|
|
549
|
+
url,
|
|
550
|
+
route,
|
|
551
|
+
group(name, about) {
|
|
552
|
+
if (groups.has(name))
|
|
553
|
+
throw new Error(`group already declared: ${name}`);
|
|
554
|
+
groups.set(name, about);
|
|
555
|
+
return () => void groups.delete(name);
|
|
556
|
+
},
|
|
557
|
+
op: (decl) => registerOp(decl, currentOpOwner),
|
|
558
|
+
approver(fn) {
|
|
559
|
+
if (approver)
|
|
560
|
+
throw new Error('an approver is already registered');
|
|
561
|
+
approver = fn;
|
|
562
|
+
return () => { approver = undefined; };
|
|
563
|
+
},
|
|
564
|
+
policy(source) {
|
|
565
|
+
if (policy)
|
|
566
|
+
throw new Error('an approval policy is already registered');
|
|
567
|
+
policy = source;
|
|
568
|
+
return () => { if (policy === source)
|
|
569
|
+
policy = undefined; };
|
|
570
|
+
},
|
|
571
|
+
remember({ op, key, label, card }) {
|
|
572
|
+
if (!policy)
|
|
573
|
+
throw new Error('现在没有保存审批例外的地方');
|
|
574
|
+
const entry = { op, tier: 'free', ...(key === undefined ? {} : { key }), label, card, at: new Date().toISOString() };
|
|
575
|
+
const problem = problemOf(entry);
|
|
576
|
+
if (problem || !table.has(op))
|
|
577
|
+
throw new Error(`不能把 ${op} 设为以后都允许:${problem ?? '没有这个操作'}`);
|
|
578
|
+
if (tightened(op, key))
|
|
579
|
+
throw new Error(`审批例外里 ${op} 现在要求机主批准`);
|
|
580
|
+
policy.add(entry);
|
|
581
|
+
},
|
|
582
|
+
overrideProblems: () => (policy?.read() ?? []).flatMap((entry) => {
|
|
583
|
+
const problem = problemOf(entry);
|
|
584
|
+
return problem ? [`${entry.op}${entry.key ? `(${entry.key})` : ''}:${problem}`] : [];
|
|
585
|
+
}),
|
|
586
|
+
signPolicy,
|
|
587
|
+
prepare,
|
|
588
|
+
invoke,
|
|
589
|
+
async say(name, args, caller, opts) {
|
|
590
|
+
const { op, args: full, result, pending } = await call(name, args, caller, opts);
|
|
591
|
+
if (pending)
|
|
592
|
+
return result;
|
|
593
|
+
if (op.text)
|
|
594
|
+
return op.text(result, full, caller);
|
|
595
|
+
if (result == null)
|
|
596
|
+
return '做了。';
|
|
597
|
+
// A member knows members by their @ names: what is a member's id in the result is said as that.
|
|
598
|
+
const handles = new Map(allMembers().map(({ id, handle }) => [id, `@${handle}`]));
|
|
599
|
+
const show = (value) => (typeof value === 'string' ? handles.get(value) ?? value : value);
|
|
600
|
+
return typeof result === 'string' ? show(result) : JSON.stringify(result, (_key, value) => show(value), 2);
|
|
601
|
+
},
|
|
602
|
+
ops: (topicId) => ({
|
|
603
|
+
groups: [...groups].map(([name, about]) => ({ name, about })),
|
|
604
|
+
ops: [...table.values()].filter((op) => available(op, topicId)),
|
|
605
|
+
}),
|
|
606
|
+
files(prefix, source, opts) {
|
|
607
|
+
const mount = { prefix, source, ...opts };
|
|
608
|
+
mounts.push(mount);
|
|
609
|
+
return () => void mounts.splice(mounts.indexOf(mount), 1);
|
|
610
|
+
},
|
|
611
|
+
};
|
|
612
|
+
entryOwners.set(entry, opOwners);
|
|
613
|
+
ctx.provide('entry', entry, (caller) => {
|
|
614
|
+
const owner = { plugin: caller.name, core: caller.core };
|
|
615
|
+
const view = new Proxy(entry, {
|
|
616
|
+
get(target, prop, receiver) {
|
|
617
|
+
if (prop === 'op') {
|
|
618
|
+
const current = target.op;
|
|
619
|
+
return (decl) => {
|
|
620
|
+
const prev = currentOpOwner;
|
|
621
|
+
if (!prev)
|
|
622
|
+
currentOpOwner = owner;
|
|
623
|
+
try {
|
|
624
|
+
return current.call(target, decl);
|
|
625
|
+
}
|
|
626
|
+
finally {
|
|
627
|
+
currentOpOwner = prev;
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
return Reflect.get(target, prop, receiver);
|
|
632
|
+
},
|
|
633
|
+
});
|
|
634
|
+
entryOwners.set(view, opOwners);
|
|
635
|
+
return view;
|
|
636
|
+
});
|
|
637
|
+
ctx.effect(entry.group('host', '宿主本身:现状和路径、发生过什么、重启、插件、给成员的工具和注入点、配置项'));
|
|
638
|
+
ctx.effect(registerOp({ name: 'host.hooks', about: '会话和回合的每个注入点:哪些插件在那里给成员加了什么、什么条件下加。', http: ['GET', '/api/hooks'], agent: true, run: () => ctx.runtime.injections() }, { plugin: 'entry', core: true }));
|
|
639
|
+
ctx.effect(registerOp({
|
|
640
|
+
name: 'host.approval_ops',
|
|
641
|
+
about: '列出所有允许成员调用的操作的默认审批档位、可放宽上限、会签与授权对象要求,以及当前审批例外里每条不生效的原因。',
|
|
642
|
+
http: ['GET', '/api/approvals/ops'], agent: true,
|
|
643
|
+
run: () => ({
|
|
644
|
+
ops: [...table.values()].filter((op) => !!op.agent).map((op) => ({
|
|
645
|
+
name: op.name,
|
|
646
|
+
group: op.name.split('.')[0],
|
|
647
|
+
about: op.about,
|
|
648
|
+
defaultTier: declaredTier(op),
|
|
649
|
+
maxTier: maxTier(op),
|
|
650
|
+
canSign: !!op.subjects,
|
|
651
|
+
minSignMembers: minSignMembers(op),
|
|
652
|
+
minSignWait: minSignWait(op),
|
|
653
|
+
hasGrant: !!op.grant,
|
|
654
|
+
...(op.grantKey ? { grantKey: op.grantKey.source } : {}),
|
|
655
|
+
})),
|
|
656
|
+
problems: (policy?.read() ?? []).map((item) => problemOf(item) ?? null),
|
|
657
|
+
}),
|
|
658
|
+
}, { plugin: 'entry', core: true }));
|
|
659
|
+
const { version } = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
|
|
660
|
+
const push = ctx.get('push');
|
|
661
|
+
route('GET', '/api/events', ({ res }) => {
|
|
662
|
+
push.connect(res);
|
|
663
|
+
return HANDLED;
|
|
664
|
+
});
|
|
665
|
+
route('GET', '/api/openapi.json', () => openapi([...groups].map(([name, about]) => ({ name, about })), [...table.values()], { title: 'hostlet', version }));
|
|
666
|
+
log('entry', `listening on ${url}`);
|
|
667
|
+
},
|
|
668
|
+
async stop() {
|
|
669
|
+
server?.closeAllConnections();
|
|
670
|
+
await new Promise((resolve) => (server ? server.close(() => resolve()) : resolve()));
|
|
671
|
+
},
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
const MAX_BODY = 100 * 1024 * 1024;
|
|
675
|
+
/** JSON for everything except a file being given, which arrives as it is (`application/octet-stream`) and is handed over as a Buffer. */
|
|
676
|
+
async function readBody(raw, limit = MAX_BODY) {
|
|
677
|
+
const tooLarge = () => new HttpError(413, `太大了:一次最多 ${limit === MAX_BODY ? '100 MB' : `${limit / 1024} KB`}`);
|
|
678
|
+
if (Number(raw.headers['content-length']) > limit)
|
|
679
|
+
throw tooLarge();
|
|
680
|
+
const chunks = [];
|
|
681
|
+
let size = 0;
|
|
682
|
+
for await (const chunk of raw) {
|
|
683
|
+
size += chunk.length;
|
|
684
|
+
if (size > limit)
|
|
685
|
+
throw tooLarge();
|
|
686
|
+
chunks.push(chunk);
|
|
687
|
+
}
|
|
688
|
+
if (raw.headers['content-type']?.startsWith('application/octet-stream'))
|
|
689
|
+
return Buffer.concat(chunks);
|
|
690
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
691
|
+
if (!text)
|
|
692
|
+
return {};
|
|
693
|
+
try {
|
|
694
|
+
return JSON.parse(text);
|
|
695
|
+
}
|
|
696
|
+
catch {
|
|
697
|
+
throw new HttpError(400, 'request body is not JSON');
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
/** Give an MCP caller a tool it can use to act on the host's refusal. */
|
|
701
|
+
export function operationErrorText(error) {
|
|
702
|
+
const message = errorText(error);
|
|
703
|
+
const detail = error instanceof HttpError ? error.detail : undefined;
|
|
704
|
+
if (!detail)
|
|
705
|
+
return message;
|
|
706
|
+
if (detail.kind === 'topic_own') {
|
|
707
|
+
return `${message}。要把事情交给别的主题里的成员用 post_to_topic,要看别的主题用 topic_about`;
|
|
708
|
+
}
|
|
709
|
+
const approval = detail.needsApproval ? ',请用 host_invoke 调用此操作并带上 why 提请' : '';
|
|
710
|
+
return `${message}${approval},或用 post_to_topic 交给那个主题里的成员做`;
|
|
711
|
+
}
|